netconsole.rst 16 KB

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