Skip to content

Commit e2f15f9

Browse files
htejuntorvalds
authored andcommitted
netconsole: implement extended console support
printk logbuf keeps various metadata and optional key=value dictionary for structured messages, both of which are stripped when messages are handed to regular console drivers. It can be useful to have this metadata and dictionary available to netconsole consumers. This obviously makes logging via netconsole more complete and the sequence number in particular is useful in environments where messages may be lost or reordered in transit - e.g. when netconsole is used to collect messages in a large cluster where packets may have to travel congested hops to reach the aggregator. The lost and reordered messages can easily be identified and handled accordingly using the sequence numbers. printk recently added extended console support which can be selected by setting CON_EXTENDED flag. From console driver side, not much changes. The only difference is that the text passed to the write callback is formatted the same way as /dev/kmsg. This patch implements extended console support for netconsole which can be enabled by either prepending "+" to a netconsole boot param entry or echoing 1 to "extended" file in configfs. When enabled, netconsole transmits extended log messages with headers identical to /dev/kmsg output. There's one complication due to message fragments. netconsole limits the maximum message size to 1k and messages longer than that are split into multiple fragments. As all extended console messages should carry matching headers and be uniquely identifiable, each extended message fragment carries full copy of the metadata and an extra header field to identify the specific fragment. The optional header is of the form "ncfrag=OFF/LEN" where OFF is the byte offset into the message body and LEN is the total length. To avoid unnecessarily making printk format extended messages, Extended netconsole is registered with printk when the first extended netconsole is configured. Signed-off-by: Tejun Heo <tj@kernel.org> Cc: David Miller <davem@davemloft.net> Cc: Kay Sievers <kay@vrfy.org> Cc: Petr Mladek <pmladek@suse.cz> Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 369e5a8 commit e2f15f9

File tree

2 files changed

+173
-3
lines changed

2 files changed

+173
-3
lines changed

Documentation/networking/netconsole.txt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
started by Ingo Molnar <mingo@redhat.com>, 2001.09.17
33
2.6 port and netpoll api by Matt Mackall <mpm@selenic.com>, Sep 9 2003
44
IPv6 support by Cong Wang <xiyou.wangcong@gmail.com>, Jan 1 2013
5+
Extended console support by Tejun Heo <tj@kernel.org>, May 1 2015
56

67
Please send bug reports to Matt Mackall <mpm@selenic.com>
78
Satyam Sharma <satyam.sharma@gmail.com>, and Cong Wang <xiyou.wangcong@gmail.com>
@@ -24,9 +25,10 @@ Sender and receiver configuration:
2425
It takes a string configuration parameter "netconsole" in the
2526
following format:
2627

27-
netconsole=[src-port]@[src-ip]/[<dev>],[tgt-port]@<tgt-ip>/[tgt-macaddr]
28+
netconsole=[+][src-port]@[src-ip]/[<dev>],[tgt-port]@<tgt-ip>/[tgt-macaddr]
2829

2930
where
31+
+ if present, enable extended console support
3032
src-port source for UDP packets (defaults to 6665)
3133
src-ip source IP to use (interface address)
3234
dev network interface (eth0)
@@ -107,6 +109,7 @@ To remove a target:
107109
The interface exposes these parameters of a netconsole target to userspace:
108110

109111
enabled Is this target currently enabled? (read-write)
112+
extended Extended mode enabled (read-write)
110113
dev_name Local network interface name (read-write)
111114
local_port Source UDP port to use (read-write)
112115
remote_port Remote agent's UDP port (read-write)
@@ -132,6 +135,36 @@ You can also update the local interface dynamically. This is especially
132135
useful if you want to use interfaces that have newly come up (and may not
133136
have existed when netconsole was loaded / initialized).
134137

138+
Extended console:
139+
=================
140+
141+
If '+' is prefixed to the configuration line or "extended" config file
142+
is set to 1, extended console support is enabled. An example boot
143+
param follows.
144+
145+
linux netconsole=+4444@10.0.0.1/eth1,9353@10.0.0.2/12:34:56:78:9a:bc
146+
147+
Log messages are transmitted with extended metadata header in the
148+
following format which is the same as /dev/kmsg.
149+
150+
<level>,<sequnum>,<timestamp>,<contflag>;<message text>
151+
152+
Non printable characters in <message text> are escaped using "\xff"
153+
notation. If the message contains optional dictionary, verbatim
154+
newline is used as the delimeter.
155+
156+
If a message doesn't fit in certain number of bytes (currently 1000),
157+
the message is split into multiple fragments by netconsole. These
158+
fragments are transmitted with "ncfrag" header field added.
159+
160+
ncfrag=<byte-offset>/<total-bytes>
161+
162+
For example, assuming a lot smaller chunk size, a message "the first
163+
chunk, the 2nd chunk." may be split as follows.
164+
165+
6,416,1758426,-,ncfrag=0/31;the first chunk,
166+
6,416,1758426,-,ncfrag=16/31; the 2nd chunk.
167+
135168
Miscellaneous notes:
136169
====================
137170

drivers/net/netconsole.c

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ static LIST_HEAD(target_list);
7979
/* This needs to be a spinlock because write_msg() cannot sleep */
8080
static DEFINE_SPINLOCK(target_list_lock);
8181

82+
/*
83+
* Console driver for extended netconsoles. Registered on the first use to
84+
* avoid unnecessarily enabling ext message formatting.
85+
*/
86+
static struct console netconsole_ext;
87+
8288
/**
8389
* struct netconsole_target - Represents a configured netconsole target.
8490
* @list: Links this target into the target_list.
@@ -105,6 +111,7 @@ struct netconsole_target {
105111
struct config_item item;
106112
#endif
107113
bool enabled;
114+
bool extended;
108115
struct netpoll np;
109116
};
110117

@@ -187,6 +194,11 @@ static struct netconsole_target *alloc_param_target(char *target_config)
187194
nt->np.remote_port = 6666;
188195
eth_broadcast_addr(nt->np.remote_mac);
189196

197+
if (*target_config == '+') {
198+
nt->extended = true;
199+
target_config++;
200+
}
201+
190202
/* Parse parameters and setup netpoll */
191203
err = netpoll_parse_options(&nt->np, target_config);
192204
if (err)
@@ -257,6 +269,11 @@ static ssize_t show_enabled(struct netconsole_target *nt, char *buf)
257269
return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled);
258270
}
259271

272+
static ssize_t show_extended(struct netconsole_target *nt, char *buf)
273+
{
274+
return snprintf(buf, PAGE_SIZE, "%d\n", nt->extended);
275+
}
276+
260277
static ssize_t show_dev_name(struct netconsole_target *nt, char *buf)
261278
{
262279
return snprintf(buf, PAGE_SIZE, "%s\n", nt->np.dev_name);
@@ -328,6 +345,11 @@ static ssize_t store_enabled(struct netconsole_target *nt,
328345
}
329346

330347
if (enabled) { /* true */
348+
if (nt->extended && !(netconsole_ext.flags & CON_ENABLED)) {
349+
netconsole_ext.flags |= CON_ENABLED;
350+
register_console(&netconsole_ext);
351+
}
352+
331353
/*
332354
* Skip netpoll_parse_options() -- all the attributes are
333355
* already configured via configfs. Just print them out.
@@ -355,6 +377,30 @@ static ssize_t store_enabled(struct netconsole_target *nt,
355377
return strnlen(buf, count);
356378
}
357379

380+
static ssize_t store_extended(struct netconsole_target *nt,
381+
const char *buf,
382+
size_t count)
383+
{
384+
int extended;
385+
int err;
386+
387+
if (nt->enabled) {
388+
pr_err("target (%s) is enabled, disable to update parameters\n",
389+
config_item_name(&nt->item));
390+
return -EINVAL;
391+
}
392+
393+
err = kstrtoint(buf, 10, &extended);
394+
if (err < 0)
395+
return err;
396+
if (extended < 0 || extended > 1)
397+
return -EINVAL;
398+
399+
nt->extended = extended;
400+
401+
return strnlen(buf, count);
402+
}
403+
358404
static ssize_t store_dev_name(struct netconsole_target *nt,
359405
const char *buf,
360406
size_t count)
@@ -507,6 +553,7 @@ static struct netconsole_target_attr netconsole_target_##_name = \
507553
__CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
508554

509555
NETCONSOLE_TARGET_ATTR_RW(enabled);
556+
NETCONSOLE_TARGET_ATTR_RW(extended);
510557
NETCONSOLE_TARGET_ATTR_RW(dev_name);
511558
NETCONSOLE_TARGET_ATTR_RW(local_port);
512559
NETCONSOLE_TARGET_ATTR_RW(remote_port);
@@ -517,6 +564,7 @@ NETCONSOLE_TARGET_ATTR_RW(remote_mac);
517564

518565
static struct configfs_attribute *netconsole_target_attrs[] = {
519566
&netconsole_target_enabled.attr,
567+
&netconsole_target_extended.attr,
520568
&netconsole_target_dev_name.attr,
521569
&netconsole_target_local_port.attr,
522570
&netconsole_target_remote_port.attr,
@@ -727,6 +775,82 @@ static struct notifier_block netconsole_netdev_notifier = {
727775
.notifier_call = netconsole_netdev_event,
728776
};
729777

778+
/**
779+
* send_ext_msg_udp - send extended log message to target
780+
* @nt: target to send message to
781+
* @msg: extended log message to send
782+
* @msg_len: length of message
783+
*
784+
* Transfer extended log @msg to @nt. If @msg is longer than
785+
* MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
786+
* ncfrag header field added to identify them.
787+
*/
788+
static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
789+
int msg_len)
790+
{
791+
static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
792+
const char *header, *body;
793+
int offset = 0;
794+
int header_len, body_len;
795+
796+
if (msg_len <= MAX_PRINT_CHUNK) {
797+
netpoll_send_udp(&nt->np, msg, msg_len);
798+
return;
799+
}
800+
801+
/* need to insert extra header fields, detect header and body */
802+
header = msg;
803+
body = memchr(msg, ';', msg_len);
804+
if (WARN_ON_ONCE(!body))
805+
return;
806+
807+
header_len = body - header;
808+
body_len = msg_len - header_len - 1;
809+
body++;
810+
811+
/*
812+
* Transfer multiple chunks with the following extra header.
813+
* "ncfrag=<byte-offset>/<total-bytes>"
814+
*/
815+
memcpy(buf, header, header_len);
816+
817+
while (offset < body_len) {
818+
int this_header = header_len;
819+
int this_chunk;
820+
821+
this_header += scnprintf(buf + this_header,
822+
sizeof(buf) - this_header,
823+
",ncfrag=%d/%d;", offset, body_len);
824+
825+
this_chunk = min(body_len - offset,
826+
MAX_PRINT_CHUNK - this_header);
827+
if (WARN_ON_ONCE(this_chunk <= 0))
828+
return;
829+
830+
memcpy(buf + this_header, body + offset, this_chunk);
831+
832+
netpoll_send_udp(&nt->np, buf, this_header + this_chunk);
833+
834+
offset += this_chunk;
835+
}
836+
}
837+
838+
static void write_ext_msg(struct console *con, const char *msg,
839+
unsigned int len)
840+
{
841+
struct netconsole_target *nt;
842+
unsigned long flags;
843+
844+
if ((oops_only && !oops_in_progress) || list_empty(&target_list))
845+
return;
846+
847+
spin_lock_irqsave(&target_list_lock, flags);
848+
list_for_each_entry(nt, &target_list, list)
849+
if (nt->extended && nt->enabled && netif_running(nt->np.dev))
850+
send_ext_msg_udp(nt, msg, len);
851+
spin_unlock_irqrestore(&target_list_lock, flags);
852+
}
853+
730854
static void write_msg(struct console *con, const char *msg, unsigned int len)
731855
{
732856
int frag, left;
@@ -742,7 +866,7 @@ static void write_msg(struct console *con, const char *msg, unsigned int len)
742866

743867
spin_lock_irqsave(&target_list_lock, flags);
744868
list_for_each_entry(nt, &target_list, list) {
745-
if (nt->enabled && netif_running(nt->np.dev)) {
869+
if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
746870
/*
747871
* We nest this inside the for-each-target loop above
748872
* so that we're able to get as much logging out to
@@ -761,6 +885,12 @@ static void write_msg(struct console *con, const char *msg, unsigned int len)
761885
spin_unlock_irqrestore(&target_list_lock, flags);
762886
}
763887

888+
static struct console netconsole_ext = {
889+
.name = "netcon_ext",
890+
.flags = CON_EXTENDED, /* starts disabled, registered on first use */
891+
.write = write_ext_msg,
892+
};
893+
764894
static struct console netconsole = {
765895
.name = "netcon",
766896
.flags = CON_ENABLED,
@@ -783,7 +913,11 @@ static int __init init_netconsole(void)
783913
goto fail;
784914
}
785915
/* Dump existing printks when we register */
786-
netconsole.flags |= CON_PRINTBUFFER;
916+
if (nt->extended)
917+
netconsole_ext.flags |= CON_PRINTBUFFER |
918+
CON_ENABLED;
919+
else
920+
netconsole.flags |= CON_PRINTBUFFER;
787921

788922
spin_lock_irqsave(&target_list_lock, flags);
789923
list_add(&nt->list, &target_list);
@@ -799,6 +933,8 @@ static int __init init_netconsole(void)
799933
if (err)
800934
goto undonotifier;
801935

936+
if (netconsole_ext.flags & CON_ENABLED)
937+
register_console(&netconsole_ext);
802938
register_console(&netconsole);
803939
pr_info("network logging started\n");
804940

@@ -827,6 +963,7 @@ static void __exit cleanup_netconsole(void)
827963
{
828964
struct netconsole_target *nt, *tmp;
829965

966+
unregister_console(&netconsole_ext);
830967
unregister_console(&netconsole);
831968
dynamic_netconsole_exit();
832969
unregister_netdevice_notifier(&netconsole_netdev_notifier);

0 commit comments

Comments
 (0)