xref: /linux/Documentation/networking/netconsole.rst (revision 99d76b4da88f21edd14b169f65de33e8df1b7804)
1.. SPDX-License-Identifier: GPL-2.0
2
3==========
4Netconsole
5==========
6
7
8started by Ingo Molnar <mingo@redhat.com>, 2001.09.17
9
102.6 port and netpoll api by Matt Mackall <mpm@selenic.com>, Sep 9 2003
11
12IPv6 support by Cong Wang <xiyou.wangcong@gmail.com>, Jan 1 2013
13
14Extended console support by Tejun Heo <tj@kernel.org>, May 1 2015
15
16Release prepend support by Breno Leitao <leitao@debian.org>, Jul 7 2023
17
18Userdata append support by Matthew Wood <thepacketgeek@gmail.com>, Jan 22 2024
19
20Sysdata append support by Breno Leitao <leitao@debian.org>, Jan 15 2025
21
22Introduction:
23=============
24
25This module logs kernel printk messages over UDP allowing debugging of
26problem where disk logging fails and serial consoles are impractical.
27
28It can be used either built-in or as a module. As a built-in,
29netconsole initializes immediately after NIC cards and will bring up
30the specified interface as soon as possible. While this doesn't allow
31capture of early kernel panics, it does capture most of the boot
32process.
33
34Sender and receiver configuration:
35==================================
36
37It takes a string configuration parameter "netconsole" in the
38following format::
39
40 netconsole=[+][r][src-port]@[src-ip]/[<dev>],[tgt-port]@<tgt-ip>/[tgt-macaddr]
41
42   where
43	+             if present, enable extended console support
44	r             if present, prepend kernel version (release) to the message
45	src-port      source for UDP packets (defaults to 6665)
46	src-ip        source IP to use (interface address)
47	dev           network interface name (eth0) or MAC address
48	tgt-port      port for logging agent (6666)
49	tgt-ip        IP address for logging agent
50	tgt-macaddr   ethernet MAC address for logging agent (broadcast)
51
52Examples::
53
54 linux netconsole=4444@10.0.0.1/eth1,9353@10.0.0.2/12:34:56:78:9a:bc
55
56or::
57
58 insmod netconsole netconsole=@/,@10.0.0.2/
59
60or using IPv6::
61
62 insmod netconsole netconsole=@/,@fd00:1:2:3::1/
63
64or using a MAC address to select the egress interface::
65
66   linux netconsole=4444@10.0.0.1/22:33:44:55:66:77,9353@10.0.0.2/12:34:56:78:9a:bc
67
68It also supports logging to multiple remote agents by specifying
69parameters for the multiple agents separated by semicolons and the
70complete string enclosed in "quotes", thusly::
71
72 modprobe netconsole netconsole="@/,@10.0.0.2/;@/eth1,6892@10.0.0.3/"
73
74Built-in netconsole starts immediately after the TCP stack is
75initialized and attempts to bring up the supplied dev at the supplied
76address.
77
78The remote host has several options to receive the kernel messages,
79for example:
80
811) syslogd
82
832) netcat
84
85   On distributions using a BSD-based netcat version (e.g. Fedora,
86   openSUSE and Ubuntu) the listening port must be specified without
87   the -p switch::
88
89	nc -u -l -p <port>' / 'nc -u -l <port>
90
91   or::
92
93	netcat -u -l -p <port>' / 'netcat -u -l <port>
94
953) socat
96
97::
98
99   socat udp-recv:<port> -
100
101Dynamic reconfiguration:
102========================
103
104Dynamic reconfigurability is a useful addition to netconsole that enables
105remote logging targets to be dynamically added, removed, or have their
106parameters reconfigured at runtime from a configfs-based userspace interface.
107
108To include this feature, select CONFIG_NETCONSOLE_DYNAMIC when building the
109netconsole module (or kernel, if netconsole is built-in).
110
111Some examples follow (where configfs is mounted at the /sys/kernel/config
112mountpoint).
113
114To add a remote logging target (target names can be arbitrary)::
115
116 cd /sys/kernel/config/netconsole/
117 mkdir target1
118
119Note that newly created targets have default parameter values (as mentioned
120above) and are disabled by default -- they must first be enabled by writing
121"1" to the "enabled" attribute (usually after setting parameters accordingly)
122as described below.
123
124To remove a target::
125
126 rmdir /sys/kernel/config/netconsole/othertarget/
127
128The interface exposes these parameters of a netconsole target to userspace:
129
130	=============== =================================       ============
131	enabled		Is this target currently enabled?	(read-write)
132	extended	Extended mode enabled			(read-write)
133	release		Prepend kernel release to message	(read-write)
134	dev_name	Local network interface name		(read-write)
135	local_port	Source UDP port to use			(read-write)
136	remote_port	Remote agent's UDP port			(read-write)
137	local_ip	Source IP address to use		(read-write)
138	remote_ip	Remote agent's IP address		(read-write)
139	local_mac	Local interface's MAC address		(read-only)
140	remote_mac	Remote agent's MAC address		(read-write)
141	transmit_errors	Number of packet send errors		(read-only)
142	=============== =================================       ============
143
144local_ip is optional: if it is left unset, netconsole derives a source
145address from the egress interface when the target is enabled. Writing any
146value to it, including 0.0.0.0, uses that value verbatim and disables the
147automatic selection.
148
149The "enabled" attribute is also used to control whether the parameters of
150a target can be updated or not -- you can modify the parameters of only
151disabled targets (i.e. if "enabled" is 0).
152
153To update a target's parameters::
154
155 cat enabled				# check if enabled is 1
156 echo 0 > enabled			# disable the target (if required)
157 echo eth2 > dev_name			# set local interface
158 echo 10.0.0.4 > remote_ip		# update some parameter
159 echo cb:a9:87:65:43:21 > remote_mac	# update more parameters
160 echo 1 > enabled			# enable target again
161
162You can also update the local interface dynamically. This is especially
163useful if you want to use interfaces that have newly come up (and may not
164have existed when netconsole was loaded / initialized).
165
166Netconsole targets defined at boot time (or module load time) with the
167`netconsole=` param are assigned the name `cmdline<index>`.  For example, the
168first target in the parameter is named `cmdline0`.  You can control and modify
169these targets by creating configfs directories with the matching name.
170
171Let's suppose you have two netconsole targets defined at boot time::
172
173 netconsole=4444@10.0.0.1/eth1,9353@10.0.0.2/12:34:56:78:9a:bc;4444@10.0.0.1/eth1,9353@10.0.0.3/12:34:56:78:9a:bc
174
175You can modify these targets in runtime by creating the following targets::
176
177 mkdir cmdline0
178 cat cmdline0/remote_ip
179 10.0.0.2
180
181 mkdir cmdline1
182 cat cmdline1/remote_ip
183 10.0.0.3
184
185Append User Data
186----------------
187
188Custom user data can be appended to the end of messages with netconsole
189dynamic configuration enabled. User data entries can be modified without
190changing the "enabled" attribute of a target.
191
192Directories (keys) under `userdata` are limited to 53 character length, and
193data in `userdata/<key>/value` are limited to 200 bytes::
194
195 cd /sys/kernel/config/netconsole && mkdir cmdline0
196 cd cmdline0
197 mkdir userdata/foo
198 echo bar > userdata/foo/value
199 mkdir userdata/qux
200 echo baz > userdata/qux/value
201
202Messages will now include this additional user data::
203
204 echo "This is a message" > /dev/kmsg
205
206Sends::
207
208 12,607,22085407756,-;This is a message
209  foo=bar
210  qux=baz
211
212Preview the userdata that will be appended with::
213
214 cd /sys/kernel/config/netconsole/cmdline0/userdata
215 for f in `ls userdata`; do echo $f=$(cat userdata/$f/value); done
216
217If a `userdata` entry is created but no data is written to the `value` file,
218the entry will be omitted from netconsole messages::
219
220 cd /sys/kernel/config/netconsole && mkdir cmdline0
221 cd cmdline0
222 mkdir userdata/foo
223 echo bar > userdata/foo/value
224 mkdir userdata/qux
225
226The `qux` key is omitted since it has no value::
227
228 echo "This is a message" > /dev/kmsg
229 12,607,22085407756,-;This is a message
230  foo=bar
231
232Delete `userdata` entries with `rmdir`::
233
234 rmdir /sys/kernel/config/netconsole/cmdline0/userdata/qux
235
236.. warning::
237   When writing strings to user data values, input is broken up per line in
238   configfs store calls and this can cause confusing behavior::
239
240     mkdir userdata/testing
241     printf "val1\nval2" > userdata/testing/value
242     # userdata store value is called twice, first with "val1\n" then "val2"
243     # so "val2" is stored, being the last value stored
244     cat userdata/testing/value
245     val2
246
247   It is recommended to not write user data values with newlines.
248
249Task name auto population in userdata
250-------------------------------------
251
252Inside the netconsole configfs hierarchy, there is a file called
253`taskname_enabled` under the `userdata` directory. This file is used to enable
254or disable the automatic task name population feature. This feature
255automatically populates the current task name that is scheduled in the CPU
256sneding the message.
257
258To enable task name auto-population::
259
260  echo 1 > /sys/kernel/config/netconsole/target1/userdata/taskname_enabled
261
262When this option is enabled, the netconsole messages will include an additional
263line in the userdata field with the format `taskname=<task name>`. This allows
264the receiver of the netconsole messages to easily find which application was
265currently scheduled when that message was generated, providing extra context
266for kernel messages and helping to categorize them.
267
268Example::
269
270  echo "This is a message" > /dev/kmsg
271  12,607,22085407756,-;This is a message
272   taskname=echo
273
274In this example, the message was generated while "echo" was the current
275scheduled process.
276
277Kernel release auto population in userdata
278------------------------------------------
279
280Within the netconsole configfs hierarchy, there is a file named `release_enabled`
281located in the `userdata` directory. This file controls the kernel release
282(version) auto-population feature, which appends the kernel release information
283to userdata dictionary in every message sent.
284
285To enable the release auto-population::
286
287  echo 1 > /sys/kernel/config/netconsole/target1/userdata/release_enabled
288
289Example::
290
291  echo "This is a message" > /dev/kmsg
292  12,607,22085407756,-;This is a message
293   release=6.14.0-rc6-01219-g3c027fbd941d
294
295.. note::
296
297   This feature provides the same data as the "release prepend" feature.
298   However, in this case, the release information is appended to the userdata
299   dictionary rather than being included in the message header.
300
301
302CPU number auto population in userdata
303--------------------------------------
304
305Inside the netconsole configfs hierarchy, there is a file called
306`cpu_nr` under the `userdata` directory. This file is used to enable or disable
307the automatic CPU number population feature. This feature automatically
308populates the CPU number that is sending the message.
309
310To enable the CPU number auto-population::
311
312  echo 1 > /sys/kernel/config/netconsole/target1/userdata/cpu_nr
313
314When this option is enabled, the netconsole messages will include an additional
315line in the userdata field with the format `cpu=<cpu_number>`. This allows the
316receiver of the netconsole messages to easily differentiate and demultiplex
317messages originating from different CPUs, which is particularly useful when
318dealing with parallel log output.
319
320Example::
321
322  echo "This is a message" > /dev/kmsg
323  12,607,22085407756,-;This is a message
324   cpu=42
325
326In this example, the message was sent by CPU 42.
327
328.. note::
329
330   If the user has set a conflicting `cpu` key in the userdata dictionary,
331   both keys will be reported, with the kernel-populated entry appearing after
332   the user one. For example::
333
334     # User-defined CPU entry
335     mkdir -p /sys/kernel/config/netconsole/target1/userdata/cpu
336     echo "1" > /sys/kernel/config/netconsole/target1/userdata/cpu/value
337
338   Output might look like::
339
340     12,607,22085407756,-;This is a message
341      cpu=1
342      cpu=42    # kernel-populated value
343
344
345Message ID auto population in userdata
346--------------------------------------
347
348Within the netconsole configfs hierarchy, there is a file named `msgid_enabled`
349located in the `userdata` directory. This file controls the message ID
350auto-population feature, which assigns a numeric id to each message sent to a
351given target and appends the ID to userdata dictionary in every message sent.
352
353The message ID is generated using a per-target 32 bit counter that is
354incremented for every message sent to the target. Note that this counter will
355eventually wrap around after reaching uint32_t max value, so the message ID is
356not globally unique over time. However, it can still be used by the target to
357detect if messages were dropped before reaching the target by identifying gaps
358in the sequence of IDs.
359
360It is important to distinguish message IDs from the message <sequnum> field.
361Some kernel messages may never reach netconsole (for example, due to printk
362rate limiting). Thus, a gap in <sequnum> cannot be solely relied upon to
363indicate that a message was dropped during transmission, as it may never have
364been sent via netconsole. The message ID, on the other hand, is only assigned
365to messages that are actually transmitted via netconsole.
366
367Example::
368
369  echo "This is message #1" > /dev/kmsg
370  echo "This is message #2" > /dev/kmsg
371  13,434,54928466,-;This is message #1
372   msgid=1
373  13,435,54934019,-;This is message #2
374   msgid=2
375
376
377Extended console:
378=================
379
380If '+' is prefixed to the configuration line or "extended" config file
381is set to 1, extended console support is enabled. An example boot
382param follows::
383
384 linux netconsole=+4444@10.0.0.1/eth1,9353@10.0.0.2/12:34:56:78:9a:bc
385
386Log messages are transmitted with extended metadata header in the
387following format which is the same as /dev/kmsg::
388
389 <level>,<sequnum>,<timestamp>,<contflag>;<message text>
390
391If 'r' (release) feature is enabled, the kernel release version is
392prepended to the start of the message. Example::
393
394 6.4.0,6,444,501151268,-;netconsole: network logging started
395
396Non printable characters in <message text> are escaped using "\xff"
397notation. If the message contains optional dictionary, verbatim
398newline is used as the delimiter.
399
400If a message doesn't fit in certain number of bytes (currently 1000),
401the message is split into multiple fragments by netconsole. These
402fragments are transmitted with "ncfrag" header field added::
403
404 ncfrag=<byte-offset>/<total-bytes>
405
406For example, assuming a lot smaller chunk size, a message "the first
407chunk, the 2nd chunk." may be split as follows::
408
409 6,416,1758426,-,ncfrag=0/31;the first chunk,
410 6,416,1758426,-,ncfrag=16/31; the 2nd chunk.
411
412Miscellaneous notes:
413====================
414
415.. Warning::
416
417   the default target ethernet setting uses the broadcast
418   ethernet address to send packets, which can cause increased load on
419   other systems on the same ethernet segment.
420
421.. Tip::
422
423   some LAN switches may be configured to suppress ethernet broadcasts
424   so it is advised to explicitly specify the remote agents' MAC addresses
425   from the config parameters passed to netconsole.
426
427.. Tip::
428
429   to find out the MAC address of, say, 10.0.0.2, you may try using::
430
431	ping -c 1 10.0.0.2 ; /sbin/arp -n | grep 10.0.0.2
432
433.. Tip::
434
435   in case the remote logging agent is on a separate LAN subnet than
436   the sender, it is suggested to try specifying the MAC address of the
437   default gateway (you may use /sbin/route -n to find it out) as the
438   remote MAC address instead.
439
440.. note::
441
442   the network device (eth1 in the above case) can run any kind
443   of other network traffic, netconsole is not intrusive. Netconsole
444   might cause slight delays in other traffic if the volume of kernel
445   messages is high, but should have no other impact.
446
447.. note::
448
449   if you find that the remote logging agent is not receiving or
450   printing all messages from the sender, it is likely that you have set
451   the "console_loglevel" parameter (on the sender) to only send high
452   priority messages to the console. You can change this at runtime using::
453
454	dmesg -n 8
455
456   or by specifying "debug" on the kernel command line at boot, to send
457   all kernel messages to the console. A specific value for this parameter
458   can also be set using the "loglevel" kernel boot option. See the
459   dmesg(8) man page and Documentation/admin-guide/kernel-parameters.rst
460   for details.
461
462Netconsole was designed to be as instantaneous as possible, to
463enable the logging of even the most critical kernel bugs. It works
464from IRQ contexts as well, and does not enable interrupts while
465sending packets. Due to these unique needs, configuration cannot
466be more automatic, and some fundamental limitations will remain:
467only IP networks, UDP packets and ethernet devices are supported.
468