[PATCH] netconsole: Append kernel version to message

From: leitao
Date: Mon Jul 03 2023 - 11:42:10 EST


From: Breno Leitao <leitao@xxxxxxxxxx>

Create a new netconsole Kconfig option that prepends the kernel version in
the netconsole message. This is useful to map kernel messages to kernel
version in a simple way, i.e., without checking somewhere which kernel
version the host that sent the message is using.

If this option is selected, then the "<uname>;" is prepended before the
netconsole message. This is an example of a netcons output, with this
feature enabled:

6.4.0-01762-ga1ba2ffe946e;12,426,112883998,-;this is a test

Calvin Owens send a RFC about this problem in 2016[1], but his
approach was a bit more intrusive, changing the printk subsystem. This
approach is lighter, and just append the information in the last mile,
just before netconsole push the message to netpoll.

[1] Link: https://lore.kernel.org/all/51047c0f6e86abcb9ee13f60653b6946f8fcfc99.1463172791.git.calvinowens@xxxxxx/

Signed-off-by: Breno Leitao <leitao@xxxxxxxxxx>
Cc: Dave Jones <davej@xxxxxxxxxxxxxxxxx>
---
drivers/net/Kconfig | 10 ++++++++++
drivers/net/netconsole.c | 35 ++++++++++++++++++++++++++++++++++-
2 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index d0a1ed216d15..df50fdb6c794 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -332,6 +332,16 @@ config NETCONSOLE_DYNAMIC
at runtime through a userspace interface exported using configfs.
See <file:Documentation/networking/netconsole.rst> for details.

+config NETCONSOLE_UNAME
+ bool "Add the kernel version to netconsole lines"
+ depends on NETCONSOLE
+ default n
+ help
+ This option causes extended netcons messages to be prepended with
+ kernel uname version. This can be useful for monitoring a large
+ deployment of servers, so, you can easily map outputs to kernel
+ versions.
+
config NETPOLL
def_bool NETCONSOLE

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 4f4f79532c6c..7edc5b033e14 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -36,6 +36,7 @@
#include <linux/inet.h>
#include <linux/configfs.h>
#include <linux/etherdevice.h>
+#include <linux/utsname.h>

MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@xxxxxxxxxxx>");
MODULE_DESCRIPTION("Console driver for network interfaces");
@@ -815,6 +816,38 @@ static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
}
}

+#ifdef CONFIG_NETCONSOLE_UNAME
+static void send_ext_msg_udp_uname(struct netconsole_target *nt,
+ const char *msg, unsigned int len)
+{
+ unsigned int newlen;
+ char *newmsg;
+ char *uname;
+
+ uname = init_utsname()->release;
+
+ newmsg = kasprintf(GFP_KERNEL, "%s;%s", uname, msg);
+ if (!newmsg)
+ /* In case of ENOMEM, just ignore this entry */
+ return;
+ newlen = strlen(uname) + len + 1;
+
+ send_ext_msg_udp(nt, newmsg, newlen);
+
+ kfree(newmsg);
+}
+#endif
+
+static inline void send_msg_udp(struct netconsole_target *nt,
+ const char *msg, unsigned int len)
+{
+#ifdef CONFIG_NETCONSOLE_UNAME
+ send_ext_msg_udp_uname(nt, msg, len);
+#else
+ send_ext_msg_udp(nt, msg, len);
+#endif
+}
+
static void write_ext_msg(struct console *con, const char *msg,
unsigned int len)
{
@@ -827,7 +860,7 @@ static void write_ext_msg(struct console *con, const char *msg,
spin_lock_irqsave(&target_list_lock, flags);
list_for_each_entry(nt, &target_list, list)
if (nt->extended && nt->enabled && netif_running(nt->np.dev))
- send_ext_msg_udp(nt, msg, len);
+ send_msg_udp(nt, msg, len);
spin_unlock_irqrestore(&target_list_lock, flags);
}

--
2.34.1