mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mike Waychison <mikew@google.com>
To: simon.kagstrom@netinsight.net, davem@davemloft.net,
	nhorman@tuxdriver.com, Matt Mackall <mpm@selenic.com>
Cc: adurbin@google.com, linux-kernel@vger.kernel.org,
	chavey@google.com, "Greg KH" <greg@kroah.com>,
	netdev@vger.kernel.org, "Américo Wang" <xiyou.wangcong@gmail.com>,
	akpm@linux-foundation.org, linux-api@vger.kernel.org
Subject: [PATCH v3 19/22] netoops: add core functionality
Date: Tue, 14 Dec 2010 13:30:38 -0800	[thread overview]
Message-ID: <20101214213037.17022.1096.stgit@mike.mtv.corp.google.com> (raw)
In-Reply-To: <20101214212846.17022.64836.stgit@mike.mtv.corp.google.com>

The kernel network dumper provides information about a crashed machine
on the network.

On a crash, the kernel spits out the contents of the kernel message buffer
along with a few other useful tidbits of information via netpoll UDP.
Each packet is sent a total of three times to deal with packet loss on the
connection. Furthermore a small amount critical data is present in every
packet, so even if only a single packet gets through, we still witness the
crash.  In the same vein, we send packet in reverse order to handle cases where
the kernel fatally crashes before transmission can be completed because often
the most interesting bits of a crash can be found in the tail of the log.

Configuration of the netoops device currently uses the same mechanism as
netpoll, that is, it uses a directory in configfs called "netpoll" that
users can create new targets.  It also supports targets as a module
parameter and as a kernel command line when built in.

Signed-off-by: Mike Waychison <mikew@google.com>
---
Changelog:
- v3
   - Bumped version to 3 to signify that this is different than the
     versions already used in-house.
   - Reserved fields for packet header have been removed in this version
     of the patch.  They were useful when I was trying to make a packet
     header that was compatible with our in-house protocol, but are no
     longer neccesary now that I am restructuring the packet header
     format to work better for other architectures.
   - Packet now has a section called 'arch_data' for per-arch specific
     bits to be handled.  The offset is also explicitly declared in the
     packet header.
   - Fields in the packet are explicitly little-endian.
- v2
   - Now uses netpoll_targets abstracted from netconsole.
   - As a side effect, we now don't have to hardcode port numbers; they
     are user overridable.
   - Compiles as a module in this patch.
---
 drivers/net/Kconfig   |   10 ++
 drivers/net/Makefile  |    1 
 drivers/net/netoops.c |  244 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 255 insertions(+), 0 deletions(-)
 create mode 100644 drivers/net/netoops.c

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 2ae9818..0e9fae3 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -3394,6 +3394,16 @@ config NETCONSOLE_DYNAMIC
 	  at runtime through a userspace interface exported using configfs.
 	  See <file:Documentation/networking/netconsole.txt> for details.
 
+config NETOOPS
+	tristate "Network oops support"
+	select NETPOLL_TARGETS
+	select NETPOLL_TARGETS_DYNAMIC
+	help
+	  This option enables the ability to have the kernel logs emitted on
+	  the network when a machine Oopses or Panics.  Configuration of this
+	  option is done at runtime by configuring a destination IP address.
+	  If unsure, say N.
+
 config NETPOLL
 	def_bool false
 
diff --git a/drivers/net/Makefile b/drivers/net/Makefile
index b90738d..58d7181 100644
--- a/drivers/net/Makefile
+++ b/drivers/net/Makefile
@@ -288,6 +288,7 @@ obj-$(CONFIG_ETRAX_ETHERNET) += cris/
 obj-$(CONFIG_ENP2611_MSF_NET) += ixp2000/
 
 obj-$(CONFIG_NETCONSOLE) += netconsole.o
+obj-$(CONFIG_NETOOPS) += netoops.o
 
 obj-$(CONFIG_FS_ENET) += fs_enet/
 
diff --git a/drivers/net/netoops.c b/drivers/net/netoops.c
new file mode 100644
index 0000000..1940ea5
--- /dev/null
+++ b/drivers/net/netoops.c
@@ -0,0 +1,244 @@
+/*
+ * drivers/net/netoops.c
+ * Copyright (C) 2004 and beyond Google Inc.
+ *
+ * Original Author Ross Biro
+ * Revisions Rebecca Schultz
+ * Cleaned up by Mike Waychison <mikew@google.com>
+ *
+ * This is very simple code to use the polling
+ * mode of the network drivers to send the
+ * contents of the printk buffer via udp w/o
+ * checksum to a unicast address.
+ */
+
+#include <linux/in.h>
+#include <linux/notifier.h>
+#include <linux/kernel.h>
+#include <linux/netpoll.h>
+#include <linux/nmi.h>
+#include <linux/utsname.h>
+#include <linux/watchdog.h>
+#include <linux/kobject.h>
+#include <linux/sysfs.h>
+#include <linux/kmsg_dump.h>
+#include <linux/netpoll_targets.h>
+
+#define NETOOPS_TYPE_PRINTK_BUFFER 1
+#define NETOOPS_TYPE_PRINTK_BUFFER_SOFT 3
+#define NETOOPS_VERSION 0x0003
+#define NETOOPS_PORT 2004
+#define NETOOPS_RETRANSMIT_COUNT 3
+
+static DEFINE_NETPOLL_TARGETS(targets);
+
+#define MAX_PARAM_LENGTH	256
+static char __initdata config[MAX_PARAM_LENGTH];
+module_param_string(netoops, config, MAX_PARAM_LENGTH, 0);
+MODULE_PARM_DESC(netoops, " netoops=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
+
+#ifndef	MODULE
+static int __init option_setup(char *opt)
+{
+	strlcpy(config, opt, MAX_PARAM_LENGTH);
+	return 1;
+}
+__setup("netoops=", option_setup);
+#endif	/* MODULE */
+
+/*
+ * Architecture specific support.
+ */
+#if defined(__i386__) || defined(__x86_64__)
+#define NETOOPS_ARCH 2
+struct netoops_arch_data {
+
+} __attribute__((packed));
+#else
+#error "unsupported architecture"
+#endif
+
+/*
+ * Architecture independent support.
+ */
+#define NETOOPS_DATA_BYTES 1024
+struct netoops_msg {
+	struct {
+		__le16 version;    /* MUST be @ offset 0 */
+		__le16 dump_id;
+		/* Offset into packet before data[] starts.  */
+		__le16 data_offset;
+		__le16 arch;
+		/* Offset into packet before struct arch_data starts. */
+		__le16 arch_offset;
+		__le16 type;
+		__le32 packet_count;
+		__le32 packet_no;
+		/*
+		 * NOTE: fixed length strings for a packet.  NULL
+		 * termination not required.
+		 */
+		char kernel_version[64];
+	} __attribute__ ((packed)) header;
+	struct netoops_arch_data arch_data;
+	char data[NETOOPS_DATA_BYTES];
+} __attribute__ ((packed));
+
+static struct netoops_msg msg;
+
+static void setup_packet_header(int packet_count, int soft_dump)
+{
+	typeof(msg.header) *h = &msg.header;
+
+	h->version = cpu_to_le16(NETOOPS_VERSION);
+	h->data_offset = cpu_to_le16(offsetof(struct netoops_msg, data));
+	h->arch = cpu_to_le16(NETOOPS_ARCH);
+	h->arch_offset = cpu_to_le16(offsetof(struct netoops_msg, arch_data));
+	h->dump_id = cpu_to_le16((jiffies/HZ) & 0xffff);
+	h->type = cpu_to_le16(soft_dump ? NETOOPS_TYPE_PRINTK_BUFFER_SOFT :
+					  NETOOPS_TYPE_PRINTK_BUFFER);
+	h->packet_count = cpu_to_le32(packet_count);
+	strncpy(h->kernel_version, utsname()->release,
+		min(sizeof(msg.header.kernel_version),
+		    sizeof(utsname()->release)));
+}
+
+static int packet_count_from_length(unsigned long l)
+{
+	return (l + NETOOPS_DATA_BYTES - 1) / NETOOPS_DATA_BYTES;
+}
+
+/* Send the packet to all targets */
+static void netoops_send_packet(int packet_nr)
+{
+	struct netpoll_target *nt;
+
+	msg.header.packet_no = cpu_to_le32(packet_nr);
+
+	list_for_each_entry(nt, &targets.list, list) {
+		if (nt->np_state == NETPOLL_ENABLED
+		    && netif_running(nt->np.dev)) {
+			netpoll_send_udp(&nt->np, (char *)&msg, sizeof(msg));
+		}
+	}
+
+}
+
+/*
+ * Send the passed in segment of kmsg via netpoll.  Packets are sent in reverse
+ * order, with the tail packet (the first one transmitted) zero-padded.
+ */
+static void netoops_send_segment(int packet_offset,
+				 const char *s, unsigned long l)
+{
+	int packet_count = packet_count_from_length(l);
+	size_t data_length;
+	int i;
+
+	for (i = packet_count - 1; i >= 0; i--) {
+		/* Usually messages completely fill the data field */
+		data_length = NETOOPS_DATA_BYTES;
+		if (i == packet_count - 1) {
+			/* Except the tail packet, which is zero-padded */
+			data_length = l % NETOOPS_DATA_BYTES;
+			memset(msg.data + data_length, 0,
+			       NETOOPS_DATA_BYTES - data_length);
+		}
+		BUG_ON(data_length > NETOOPS_DATA_BYTES);
+
+		/* Copy the payload into the packet and send */
+		memcpy(msg.data, s + (i * NETOOPS_DATA_BYTES), data_length);
+		netoops_send_packet((packet_count - i - 1) + packet_offset);
+
+		touch_nmi_watchdog();
+	}
+}
+
+/*
+ * Callback used by the kmsg_dumper.
+ *
+ * Called with interrupts disabled locally.
+ */
+static void netoops(struct kmsg_dumper *dumper, enum kmsg_dump_reason reason,
+		    struct pt_regs *regs,
+		    const char *s1, unsigned long l1,
+		    const char *s2, unsigned long l2) {
+	unsigned long flags;
+	int packet_count_1, packet_count_2;
+	int soft_dump = 0;
+	int i;
+
+	/* Only handle fatal problems */
+	if (reason != KMSG_DUMP_OOPS
+	 && reason != KMSG_DUMP_PANIC
+	 && reason != KMSG_DUMP_SOFT)
+		return;
+
+	if (reason == KMSG_DUMP_SOFT)
+		soft_dump = 1;
+
+	spin_lock_irqsave(&targets.lock, flags);
+
+	/* compute total length of the message we are going to send */
+	packet_count_1 = packet_count_from_length(l1);
+	packet_count_2 = packet_count_from_length(l2);
+
+	/* setup the non varying parts of the message */
+	memset(&msg, 0, sizeof(msg));
+	setup_packet_header(packet_count_1 + packet_count_2, soft_dump);
+
+	/* Transmission loop */
+	for (i = 0; i < NETOOPS_RETRANSMIT_COUNT; i++) {
+		/* Send the full packets from the second segment */
+		netoops_send_segment(0, s2, l2);
+		netoops_send_segment(packet_count_2, s1, l1);
+	}
+
+	spin_unlock_irqrestore(&targets.lock, flags);
+}
+
+static struct kmsg_dumper netoops_dumper = {
+	.dump = netoops,
+};
+
+static int __init netoops_init(void)
+{
+	int retval = -EINVAL;
+
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.version) != 0);
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.dump_id) != 2);
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.data_offset) != 4);
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.arch) != 6);
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.arch_offset) != 8);
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.type) != 10);
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.packet_count) != 12);
+	BUILD_BUG_ON(offsetof(struct netoops_msg, header.packet_no) != 16);
+
+	targets.default_local_port = NETOOPS_PORT;
+	targets.default_remote_port = NETOOPS_PORT;
+
+	config[MAX_PARAM_LENGTH - 1] = '\0';
+	retval = register_netpoll_targets("netoops", &targets, config);
+	if (retval)
+		goto out;
+
+	retval = kmsg_dump_register(&netoops_dumper);
+	if (retval)
+		goto out_targets;
+
+	return 0;
+out_targets:
+	unregister_netpoll_targets(&targets);
+out:
+	return retval;
+}
+
+static void __exit netoops_exit(void)
+{
+	kmsg_dump_unregister(&netoops_dumper);
+	unregister_netpoll_targets(&targets);
+}
+
+module_init(netoops_init);
+module_exit(netoops_exit);
+MODULE_LICENSE("GPL");


  parent reply	other threads:[~2010-12-14 21:32 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-14 21:28 [PATCH v3 00/22] netoops support Mike Waychison
2010-12-14 21:28 ` [PATCH v3 01/22] netconsole: Remove unneeded reference counting Mike Waychison
2010-12-14 21:29 ` [PATCH v3 02/22] netconsole: Introduce locking over the netpoll fields Mike Waychison
2010-12-14 23:15   ` Michał Mirosław
2010-12-14 23:30     ` Mike Waychison
2010-12-14 21:29 ` [PATCH v3 03/22] netconsole: Introduce 'enabled' state-machine Mike Waychison
2010-12-14 23:21   ` Michał Mirosław
2010-12-14 23:33     ` Mike Waychison
2010-12-17 20:52   ` Neil Horman
2010-12-17 21:20     ` Mike Waychison
2010-12-14 21:29 ` [PATCH v3 04/22] netconsole: Call netpoll_cleanup() in process context Mike Waychison
2010-12-14 21:29 ` [PATCH v3 05/22] netconsole: Wrap the list and locking in a structure Mike Waychison
2010-12-14 21:29 ` [PATCH v3 06/22] netconsole: Push configfs_subsystem into netpoll_targets Mike Waychison
2010-12-14 21:29 ` [PATCH v3 07/22] netconsole: Move netdev_notifier " Mike Waychison
2010-12-14 21:29 ` [PATCH v3 08/22] netconsole: Split out netpoll_targets init/exit Mike Waychison
2010-12-14 21:29 ` [PATCH v3 09/22] netconsole: Add pointer to netpoll_targets Mike Waychison
2010-12-14 21:29 ` [PATCH v3 10/22] netconsole: Rename netconsole_target -> netpoll_target Mike Waychison
2010-12-14 21:29 ` [PATCH v3 11/22] netconsole: Abstract away the subsystem name Mike Waychison
2010-12-14 21:29 ` [PATCH v3 12/22] netconsole: Move setting of default ports Mike Waychison
2010-12-14 21:30 ` [PATCH v3 13/22] netpoll: Introduce netpoll_target configs Mike Waychison
2010-12-14 21:30 ` [PATCH v3 14/22] netpoll: Move target code into netpoll_targets.c Mike Waychison
2010-12-15  0:07   ` Mike Waychison
2010-12-14 21:30 ` [PATCH v3 15/22] Oops: Pass regs to oops_exit() Mike Waychison
2010-12-14 21:30 ` [PATCH v3 16/22] kmsg_dumper: Pass pt_regs along to dumpers Mike Waychison
2010-12-14 21:30 ` [PATCH v3 17/22] kmsg_dumper: Introduce a new 'SOFT' dump reason Mike Waychison
2010-12-14 21:30 ` [PATCH v3 18/22] sys-rq: Add option to soft dump Mike Waychison
2010-12-14 21:30 ` Mike Waychison [this message]
2010-12-14 21:30 ` [PATCH v3 20/22] netoops: Add x86 specific bits to packet headers Mike Waychison
2010-12-14 21:30 ` [PATCH v3 21/22] netoops: Add user-programmable boot_id Mike Waychison
2010-12-14 21:42   ` Matt Mackall
2010-12-14 21:59     ` Mike Waychison
2010-12-14 22:06       ` Matt Mackall
2010-12-14 22:33         ` Mike Waychison
2010-12-14 22:47           ` Matt Mackall
2010-12-14 23:19             ` Mike Waychison
2010-12-15  2:16               ` Ted Ts'o
2010-12-14 21:30 ` [PATCH v3 22/22] netoops: Add a user programmable blob to the netoops packet Mike Waychison

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20101214213037.17022.1096.stgit@mike.mtv.corp.google.com \
    --to=mikew@google.com \
    --cc=adurbin@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=chavey@google.com \
    --cc=davem@davemloft.net \
    --cc=greg@kroah.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpm@selenic.com \
    --cc=netdev@vger.kernel.org \
    --cc=nhorman@tuxdriver.com \
    --cc=simon.kagstrom@netinsight.net \
    --cc=xiyou.wangcong@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®