blob: d01e37a61b9384aae0e19d05c305989f64e8b8ec [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Allan Stephensf5e75262010-12-31 18:59:24 +00002 * net/tipc/log.c: TIPC print buffer routines for debugging
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1996-2006, Ericsson AB
Allan Stephensfb98ec72008-05-05 01:20:42 -07005 * Copyright (c) 2005-2007, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
Allan Stephensf5e75262010-12-31 18:59:24 +000039#include "log.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040
Allan Stephensc8903982008-05-05 01:22:30 -070041/*
42 * TIPC pre-defines the following print buffers:
43 *
44 * TIPC_NULL : null buffer (i.e. print nowhere)
45 * TIPC_CONS : system console
46 * TIPC_LOG : TIPC log buffer
47 *
48 * Additional user-defined print buffers are also permitted.
49 */
Allan Stephensc8903982008-05-05 01:22:30 -070050static struct print_buf null_buf = { NULL, 0, NULL, 0 };
Allan Stephens7d3aa712008-05-05 01:22:59 -070051struct print_buf *const TIPC_NULL = &null_buf;
Allan Stephens29ede242006-10-16 21:42:04 -070052
Allan Stephensc8903982008-05-05 01:22:30 -070053static struct print_buf cons_buf = { NULL, 0, NULL, 1 };
Allan Stephens6e7e3092010-12-31 18:59:28 +000054struct print_buf *const TIPC_CONS = &cons_buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +010055
Allan Stephensc8903982008-05-05 01:22:30 -070056static struct print_buf log_buf = { NULL, 0, NULL, 1 };
Allan Stephens7d3aa712008-05-05 01:22:59 -070057struct print_buf *const TIPC_LOG = &log_buf;
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Allan Stephensc8903982008-05-05 01:22:30 -070059/*
60 * Locking policy when using print buffers.
61 *
62 * 1) tipc_printf() uses 'print_lock' to protect against concurrent access to
63 * 'print_string' when writing to a print buffer. This also protects against
64 * concurrent writes to the print buffer being written to.
65 *
Allan Stephens7ced6892010-12-31 18:59:26 +000066 * 2) tipc_log_XXX() leverages the aforementioned use of 'print_lock' to
67 * protect against all types of concurrent operations on their associated
68 * print buffer (not just write operations).
Allan Stephensc8903982008-05-05 01:22:30 -070069 *
70 * Note: All routines of the form tipc_printbuf_XXX() are lock-free, and rely
71 * on the caller to prevent simultaneous use of the print buffer(s) being
72 * manipulated.
73 */
Allan Stephensc8903982008-05-05 01:22:30 -070074static DEFINE_SPINLOCK(print_lock);
75
stephen hemminger31e3c3f2010-10-13 13:20:35 +000076static void tipc_printbuf_move(struct print_buf *pb_to,
77 struct print_buf *pb_from);
Per Lidenb97bf3f2006-01-02 19:04:38 +010078
Per Lidenb97bf3f2006-01-02 19:04:38 +010079/**
Per Liden4323add2006-01-18 00:38:21 +010080 * tipc_printbuf_init - initialize print buffer to empty
Allan Stephens29ede242006-10-16 21:42:04 -070081 * @pb: pointer to print buffer structure
82 * @raw: pointer to character array used by print buffer
83 * @size: size of character array
84 *
Allan Stephensc8903982008-05-05 01:22:30 -070085 * Note: If the character array is too small (or absent), the print buffer
86 * becomes a null device that discards anything written to it.
Per Lidenb97bf3f2006-01-02 19:04:38 +010087 */
Allan Stephens29ede242006-10-16 21:42:04 -070088void tipc_printbuf_init(struct print_buf *pb, char *raw, u32 size)
Per Lidenb97bf3f2006-01-02 19:04:38 +010089{
Allan Stephens29ede242006-10-16 21:42:04 -070090 pb->buf = raw;
91 pb->crs = raw;
92 pb->size = size;
Allan Stephensc8903982008-05-05 01:22:30 -070093 pb->echo = 0;
Allan Stephens29ede242006-10-16 21:42:04 -070094
95 if (size < TIPC_PB_MIN_SIZE) {
96 pb->buf = NULL;
97 } else if (raw) {
98 pb->buf[0] = 0;
Allan Stephens7d3aa712008-05-05 01:22:59 -070099 pb->buf[size - 1] = ~0;
Allan Stephens29ede242006-10-16 21:42:04 -0700100 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101}
102
103/**
Per Liden4323add2006-01-18 00:38:21 +0100104 * tipc_printbuf_reset - reinitialize print buffer to empty state
Allan Stephens29ede242006-10-16 21:42:04 -0700105 * @pb: pointer to print buffer structure
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000107static void tipc_printbuf_reset(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108{
Allan Stephens7d3aa712008-05-05 01:22:59 -0700109 if (pb->buf) {
Allan Stephensc8903982008-05-05 01:22:30 -0700110 pb->crs = pb->buf;
111 pb->buf[0] = 0;
112 pb->buf[pb->size - 1] = ~0;
113 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114}
115
116/**
Per Liden4323add2006-01-18 00:38:21 +0100117 * tipc_printbuf_empty - test if print buffer is in empty state
Allan Stephens29ede242006-10-16 21:42:04 -0700118 * @pb: pointer to print buffer structure
119 *
120 * Returns non-zero if print buffer is empty.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100121 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000122static int tipc_printbuf_empty(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123{
Eric Dumazeta02cec22010-09-22 20:43:57 +0000124 return !pb->buf || (pb->crs == pb->buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125}
126
127/**
Per Liden4323add2006-01-18 00:38:21 +0100128 * tipc_printbuf_validate - check for print buffer overflow
Allan Stephens29ede242006-10-16 21:42:04 -0700129 * @pb: pointer to print buffer structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900130 *
131 * Verifies that a print buffer has captured all data written to it.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 * If data has been lost, linearize buffer and prepend an error message
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900133 *
Allan Stephens29ede242006-10-16 21:42:04 -0700134 * Returns length of print buffer data string (including trailing NUL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100135 */
Per Liden4323add2006-01-18 00:38:21 +0100136int tipc_printbuf_validate(struct print_buf *pb)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900138 char *err = "\n\n*** PRINT BUFFER OVERFLOW ***\n\n";
139 char *cp_buf;
140 struct print_buf cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141
Allan Stephens29ede242006-10-16 21:42:04 -0700142 if (!pb->buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 return 0;
144
Allan Stephens29ede242006-10-16 21:42:04 -0700145 if (pb->buf[pb->size - 1] == 0) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900146 cp_buf = kmalloc(pb->size, GFP_ATOMIC);
Allan Stephens7d3aa712008-05-05 01:22:59 -0700147 if (cp_buf) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900148 tipc_printbuf_init(&cb, cp_buf, pb->size);
149 tipc_printbuf_move(&cb, pb);
150 tipc_printbuf_move(pb, &cb);
151 kfree(cp_buf);
152 memcpy(pb->buf, err, strlen(err));
153 } else {
154 tipc_printbuf_reset(pb);
155 tipc_printf(pb, err);
156 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000158 return pb->crs - pb->buf + 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159}
160
161/**
Per Liden4323add2006-01-18 00:38:21 +0100162 * tipc_printbuf_move - move print buffer contents to another print buffer
Allan Stephens29ede242006-10-16 21:42:04 -0700163 * @pb_to: pointer to destination print buffer structure
164 * @pb_from: pointer to source print buffer structure
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900165 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100166 * Current contents of destination print buffer (if any) are discarded.
167 * Source print buffer becomes empty if a successful move occurs.
168 */
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000169static void tipc_printbuf_move(struct print_buf *pb_to,
170 struct print_buf *pb_from)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171{
172 int len;
173
174 /* Handle the cases where contents can't be moved */
Allan Stephens29ede242006-10-16 21:42:04 -0700175 if (!pb_to->buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 return;
177
Allan Stephens29ede242006-10-16 21:42:04 -0700178 if (!pb_from->buf) {
Per Liden4323add2006-01-18 00:38:21 +0100179 tipc_printbuf_reset(pb_to);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 return;
181 }
182
183 if (pb_to->size < pb_from->size) {
Allan Stephens93758c32008-05-05 01:21:12 -0700184 strcpy(pb_to->buf, "*** PRINT BUFFER MOVE ERROR ***");
185 pb_to->buf[pb_to->size - 1] = ~0;
186 pb_to->crs = strchr(pb_to->buf, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 return;
188 }
189
190 /* Copy data from char after cursor to end (if used) */
191 len = pb_from->buf + pb_from->size - pb_from->crs - 2;
Allan Stephens7d3aa712008-05-05 01:22:59 -0700192 if ((pb_from->buf[pb_from->size - 1] == 0) && (len > 0)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100193 strcpy(pb_to->buf, pb_from->crs + 1);
194 pb_to->crs = pb_to->buf + len;
195 } else
196 pb_to->crs = pb_to->buf;
197
198 /* Copy data from start to cursor (always) */
199 len = pb_from->crs - pb_from->buf;
200 strcpy(pb_to->crs, pb_from->buf);
201 pb_to->crs += len;
202
Per Liden4323add2006-01-18 00:38:21 +0100203 tipc_printbuf_reset(pb_from);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204}
205
206/**
Allan Stephensc8903982008-05-05 01:22:30 -0700207 * tipc_printf - append formatted output to print buffer
208 * @pb: pointer to print buffer
Allan Stephens29ede242006-10-16 21:42:04 -0700209 * @fmt: formatted info to be printed
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211void tipc_printf(struct print_buf *pb, const char *fmt, ...)
212{
Erik Hugnee2dbd602012-06-29 00:50:22 -0400213 int i;
214 va_list args;
215 char *buf;
216 int len;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217
Erik Hugnee2dbd602012-06-29 00:50:22 -0400218 buf = pb->crs;
219 len = pb->buf + pb->size - pb->crs;
220 va_start(args, fmt);
221 i = vscnprintf(buf, len, fmt, args);
222 va_end(args);
223 pb->crs += i;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224}
225
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226/**
Allan Stephens025adbe2008-05-05 01:20:04 -0700227 * tipc_log_resize - change the size of the TIPC log buffer
228 * @log_size: print buffer size to use
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 */
Allan Stephensfb98ec72008-05-05 01:20:42 -0700230int tipc_log_resize(int log_size)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231{
Allan Stephensfb98ec72008-05-05 01:20:42 -0700232 int res = 0;
233
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 spin_lock_bh(&print_lock);
Allan Stephensf5e75262010-12-31 18:59:24 +0000235 kfree(TIPC_LOG->buf);
236 TIPC_LOG->buf = NULL;
Allan Stephens025adbe2008-05-05 01:20:04 -0700237 if (log_size) {
238 if (log_size < TIPC_PB_MIN_SIZE)
239 log_size = TIPC_PB_MIN_SIZE;
Allan Stephensc8903982008-05-05 01:22:30 -0700240 res = TIPC_LOG->echo;
Allan Stephens025adbe2008-05-05 01:20:04 -0700241 tipc_printbuf_init(TIPC_LOG, kmalloc(log_size, GFP_ATOMIC),
242 log_size);
Allan Stephensc8903982008-05-05 01:22:30 -0700243 TIPC_LOG->echo = res;
Allan Stephensfb98ec72008-05-05 01:20:42 -0700244 res = !TIPC_LOG->buf;
Allan Stephens025adbe2008-05-05 01:20:04 -0700245 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 spin_unlock_bh(&print_lock);
Allan Stephensfb98ec72008-05-05 01:20:42 -0700247
248 return res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249}
250
251/**
Allan Stephens025adbe2008-05-05 01:20:04 -0700252 * tipc_log_resize_cmd - reconfigure size of TIPC log buffer
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 */
Allan Stephens025adbe2008-05-05 01:20:04 -0700254struct sk_buff *tipc_log_resize_cmd(const void *req_tlv_area, int req_tlv_space)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255{
256 u32 value;
257
258 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_UNSIGNED))
Per Liden4323add2006-01-18 00:38:21 +0100259 return tipc_cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260
Al Viro3e6c8cd2006-11-08 00:19:09 -0800261 value = ntohl(*(__be32 *)TLV_DATA(req_tlv_area));
Paul Gortmaker732efba2012-02-23 15:44:08 -0500262 if (value > 32768)
Per Liden4323add2006-01-18 00:38:21 +0100263 return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
264 " (log size must be 0-32768)");
Allan Stephensfb98ec72008-05-05 01:20:42 -0700265 if (tipc_log_resize(value))
266 return tipc_cfg_reply_error_string(
267 "unable to create specified log (log size is now 0)");
Per Liden4323add2006-01-18 00:38:21 +0100268 return tipc_cfg_reply_none();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269}
270
271/**
Per Liden4323add2006-01-18 00:38:21 +0100272 * tipc_log_dump - capture TIPC log buffer contents in configuration message
Per Lidenb97bf3f2006-01-02 19:04:38 +0100273 */
Per Liden4323add2006-01-18 00:38:21 +0100274struct sk_buff *tipc_log_dump(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100275{
276 struct sk_buff *reply;
277
278 spin_lock_bh(&print_lock);
Allan Stephens93758c32008-05-05 01:21:12 -0700279 if (!TIPC_LOG->buf) {
280 spin_unlock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100281 reply = tipc_cfg_reply_ultra_string("log not activated\n");
Allan Stephens93758c32008-05-05 01:21:12 -0700282 } else if (tipc_printbuf_empty(TIPC_LOG)) {
283 spin_unlock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100284 reply = tipc_cfg_reply_ultra_string("log is empty\n");
Allan Stephensf5e75262010-12-31 18:59:24 +0000285 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 struct tlv_desc *rep_tlv;
287 struct print_buf pb;
288 int str_len;
289
Per Liden4323add2006-01-18 00:38:21 +0100290 str_len = min(TIPC_LOG->size, 32768u);
Allan Stephens93758c32008-05-05 01:21:12 -0700291 spin_unlock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100292 reply = tipc_cfg_reply_alloc(TLV_SPACE(str_len));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100293 if (reply) {
294 rep_tlv = (struct tlv_desc *)reply->data;
Per Liden4323add2006-01-18 00:38:21 +0100295 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), str_len);
Allan Stephens93758c32008-05-05 01:21:12 -0700296 spin_lock_bh(&print_lock);
Per Liden4323add2006-01-18 00:38:21 +0100297 tipc_printbuf_move(&pb, TIPC_LOG);
Allan Stephens93758c32008-05-05 01:21:12 -0700298 spin_unlock_bh(&print_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 str_len = strlen(TLV_DATA(rep_tlv)) + 1;
300 skb_put(reply, TLV_SPACE(str_len));
301 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
302 }
303 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100304 return reply;
305}