1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/net/netconsole.c
5 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
7 * This file contains the implementation of an IRQ-safe, crash-safe
8 * kernel console implementation that outputs kernel messages to the
11 * Modification history:
13 * 2001-09-17 started by Ingo Molnar.
14 * 2003-08-11 2.6 port by Matt Mackall
18 * 2003-09-07 rewritten with netpoll api
21 /****************************************************************
23 ****************************************************************/
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/console.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/netpoll.h>
36 #include <linux/inet.h>
37 #include <linux/configfs.h>
38 #include <linux/etherdevice.h>
39 #include <linux/utsname.h>
41 MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
42 MODULE_DESCRIPTION("Console driver for network interfaces");
43 MODULE_LICENSE("GPL");
45 #define MAX_PARAM_LENGTH 256
46 #define MAX_USERDATA_ENTRY_LENGTH 256
47 #define MAX_USERDATA_VALUE_LENGTH 200
48 /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
49 #define MAX_USERDATA_NAME_LENGTH (MAX_USERDATA_ENTRY_LENGTH - \
50 MAX_USERDATA_VALUE_LENGTH - 3)
51 #define MAX_USERDATA_ITEMS 16
52 #define MAX_PRINT_CHUNK 1000
54 static char config[MAX_PARAM_LENGTH];
55 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
56 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
58 static bool oops_only;
59 module_param(oops_only, bool, 0600);
60 MODULE_PARM_DESC(oops_only, "Only log oops messages");
62 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
65 static int __init option_setup(char *opt)
67 strscpy(config, opt, MAX_PARAM_LENGTH);
70 __setup("netconsole=", option_setup);
73 /* Linked list of all configured targets */
74 static LIST_HEAD(target_list);
76 /* This needs to be a spinlock because write_msg() cannot sleep */
77 static DEFINE_SPINLOCK(target_list_lock);
80 * Console driver for extended netconsoles. Registered on the first use to
81 * avoid unnecessarily enabling ext message formatting.
83 static struct console netconsole_ext;
86 * struct netconsole_target - Represents a configured netconsole target.
87 * @list: Links this target into the target_list.
88 * @group: Links us into the configfs subsystem hierarchy.
89 * @userdata_group: Links to the userdata configfs hierarchy
90 * @userdata_complete: Cached, formatted string of append
91 * @userdata_length: String length of userdata_complete
92 * @enabled: On / off knob to enable / disable target.
93 * Visible from userspace (read-write).
94 * We maintain a strict 1:1 correspondence between this and
95 * whether the corresponding netpoll is active or inactive.
96 * Also, other parameters of a target may be modified at
97 * runtime only when it is disabled (enabled == 0).
98 * @extended: Denotes whether console is extended or not.
99 * @release: Denotes whether kernel release version should be prepended
100 * to the message. Depends on extended console.
101 * @np: The netpoll structure for this target.
102 * Contains the other userspace visible parameters:
103 * dev_name (read-write)
104 * local_port (read-write)
105 * remote_port (read-write)
106 * local_ip (read-write)
107 * remote_ip (read-write)
108 * local_mac (read-only)
109 * remote_mac (read-write)
111 struct netconsole_target {
112 struct list_head list;
113 #ifdef CONFIG_NETCONSOLE_DYNAMIC
114 struct config_group group;
115 struct config_group userdata_group;
116 char userdata_complete[MAX_USERDATA_ENTRY_LENGTH * MAX_USERDATA_ITEMS];
117 size_t userdata_length;
125 #ifdef CONFIG_NETCONSOLE_DYNAMIC
127 static struct configfs_subsystem netconsole_subsys;
128 static DEFINE_MUTEX(dynamic_netconsole_mutex);
130 static int __init dynamic_netconsole_init(void)
132 config_group_init(&netconsole_subsys.su_group);
133 mutex_init(&netconsole_subsys.su_mutex);
134 return configfs_register_subsystem(&netconsole_subsys);
137 static void __exit dynamic_netconsole_exit(void)
139 configfs_unregister_subsystem(&netconsole_subsys);
143 * Targets that were created by parsing the boot/module option string
144 * do not exist in the configfs hierarchy (and have NULL names) and will
145 * never go away, so make these a no-op for them.
147 static void netconsole_target_get(struct netconsole_target *nt)
149 if (config_item_name(&nt->group.cg_item))
150 config_group_get(&nt->group);
153 static void netconsole_target_put(struct netconsole_target *nt)
155 if (config_item_name(&nt->group.cg_item))
156 config_group_put(&nt->group);
159 #else /* !CONFIG_NETCONSOLE_DYNAMIC */
161 static int __init dynamic_netconsole_init(void)
166 static void __exit dynamic_netconsole_exit(void)
171 * No danger of targets going away from under us when dynamic
172 * reconfigurability is off.
174 static void netconsole_target_get(struct netconsole_target *nt)
178 static void netconsole_target_put(struct netconsole_target *nt)
182 static void populate_configfs_item(struct netconsole_target *nt,
186 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
188 /* Allocate and initialize with defaults.
189 * Note that these targets get their config_item fields zeroed-out.
191 static struct netconsole_target *alloc_and_init(void)
193 struct netconsole_target *nt;
195 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
199 if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
201 if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
204 nt->np.name = "netconsole";
205 strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
206 nt->np.local_port = 6665;
207 nt->np.remote_port = 6666;
208 eth_broadcast_addr(nt->np.remote_mac);
213 #ifdef CONFIG_NETCONSOLE_DYNAMIC
216 * Our subsystem hierarchy is:
218 * /sys/kernel/config/netconsole/
238 static struct netconsole_target *to_target(struct config_item *item)
240 struct config_group *cfg_group;
242 cfg_group = to_config_group(item);
245 return container_of(to_config_group(item),
246 struct netconsole_target, group);
249 /* Get rid of possible trailing newline, returning the new length */
250 static void trim_newline(char *s, size_t maxlen)
254 len = strnlen(s, maxlen);
255 if (s[len - 1] == '\n')
260 * Attribute operations for netconsole_target.
263 static ssize_t enabled_show(struct config_item *item, char *buf)
265 return sysfs_emit(buf, "%d\n", to_target(item)->enabled);
268 static ssize_t extended_show(struct config_item *item, char *buf)
270 return sysfs_emit(buf, "%d\n", to_target(item)->extended);
273 static ssize_t release_show(struct config_item *item, char *buf)
275 return sysfs_emit(buf, "%d\n", to_target(item)->release);
278 static ssize_t dev_name_show(struct config_item *item, char *buf)
280 return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
283 static ssize_t local_port_show(struct config_item *item, char *buf)
285 return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
288 static ssize_t remote_port_show(struct config_item *item, char *buf)
290 return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
293 static ssize_t local_ip_show(struct config_item *item, char *buf)
295 struct netconsole_target *nt = to_target(item);
298 return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
300 return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
303 static ssize_t remote_ip_show(struct config_item *item, char *buf)
305 struct netconsole_target *nt = to_target(item);
308 return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
310 return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
313 static ssize_t local_mac_show(struct config_item *item, char *buf)
315 struct net_device *dev = to_target(item)->np.dev;
316 static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
318 return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
321 static ssize_t remote_mac_show(struct config_item *item, char *buf)
323 return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
327 * This one is special -- targets created through the configfs interface
328 * are not enabled (and the corresponding netpoll activated) by default.
329 * The user is expected to set the desired parameters first (which
330 * would enable him to dynamically add new netpoll targets for new
331 * network interfaces as and when they come up).
333 static ssize_t enabled_store(struct config_item *item,
334 const char *buf, size_t count)
336 struct netconsole_target *nt = to_target(item);
341 mutex_lock(&dynamic_netconsole_mutex);
342 err = kstrtobool(buf, &enabled);
347 if ((bool)enabled == nt->enabled) {
348 pr_info("network logging has already %s\n",
349 nt->enabled ? "started" : "stopped");
353 if (enabled) { /* true */
354 if (nt->release && !nt->extended) {
355 pr_err("Not enabling netconsole. Release feature requires extended log message");
359 if (nt->extended && !console_is_registered(&netconsole_ext))
360 register_console(&netconsole_ext);
363 * Skip netpoll_parse_options() -- all the attributes are
364 * already configured via configfs. Just print them out.
366 netpoll_print_options(&nt->np);
368 err = netpoll_setup(&nt->np);
372 pr_info("network logging started\n");
374 /* We need to disable the netconsole before cleaning it up
375 * otherwise we might end up in write_msg() with
376 * nt->np.dev == NULL and nt->enabled == true
378 spin_lock_irqsave(&target_list_lock, flags);
380 spin_unlock_irqrestore(&target_list_lock, flags);
381 netpoll_cleanup(&nt->np);
384 nt->enabled = enabled;
386 mutex_unlock(&dynamic_netconsole_mutex);
387 return strnlen(buf, count);
389 mutex_unlock(&dynamic_netconsole_mutex);
393 static ssize_t release_store(struct config_item *item, const char *buf,
396 struct netconsole_target *nt = to_target(item);
400 mutex_lock(&dynamic_netconsole_mutex);
402 pr_err("target (%s) is enabled, disable to update parameters\n",
403 config_item_name(&nt->group.cg_item));
408 err = kstrtobool(buf, &release);
412 nt->release = release;
414 mutex_unlock(&dynamic_netconsole_mutex);
415 return strnlen(buf, count);
417 mutex_unlock(&dynamic_netconsole_mutex);
421 static ssize_t extended_store(struct config_item *item, const char *buf,
424 struct netconsole_target *nt = to_target(item);
428 mutex_lock(&dynamic_netconsole_mutex);
430 pr_err("target (%s) is enabled, disable to update parameters\n",
431 config_item_name(&nt->group.cg_item));
436 err = kstrtobool(buf, &extended);
440 nt->extended = extended;
442 mutex_unlock(&dynamic_netconsole_mutex);
443 return strnlen(buf, count);
445 mutex_unlock(&dynamic_netconsole_mutex);
449 static ssize_t dev_name_store(struct config_item *item, const char *buf,
452 struct netconsole_target *nt = to_target(item);
454 mutex_lock(&dynamic_netconsole_mutex);
456 pr_err("target (%s) is enabled, disable to update parameters\n",
457 config_item_name(&nt->group.cg_item));
458 mutex_unlock(&dynamic_netconsole_mutex);
462 strscpy(nt->np.dev_name, buf, IFNAMSIZ);
463 trim_newline(nt->np.dev_name, IFNAMSIZ);
465 mutex_unlock(&dynamic_netconsole_mutex);
466 return strnlen(buf, count);
469 static ssize_t local_port_store(struct config_item *item, const char *buf,
472 struct netconsole_target *nt = to_target(item);
475 mutex_lock(&dynamic_netconsole_mutex);
477 pr_err("target (%s) is enabled, disable to update parameters\n",
478 config_item_name(&nt->group.cg_item));
482 rv = kstrtou16(buf, 10, &nt->np.local_port);
485 mutex_unlock(&dynamic_netconsole_mutex);
486 return strnlen(buf, count);
488 mutex_unlock(&dynamic_netconsole_mutex);
492 static ssize_t remote_port_store(struct config_item *item,
493 const char *buf, size_t count)
495 struct netconsole_target *nt = to_target(item);
498 mutex_lock(&dynamic_netconsole_mutex);
500 pr_err("target (%s) is enabled, disable to update parameters\n",
501 config_item_name(&nt->group.cg_item));
505 rv = kstrtou16(buf, 10, &nt->np.remote_port);
508 mutex_unlock(&dynamic_netconsole_mutex);
509 return strnlen(buf, count);
511 mutex_unlock(&dynamic_netconsole_mutex);
515 static ssize_t local_ip_store(struct config_item *item, const char *buf,
518 struct netconsole_target *nt = to_target(item);
520 mutex_lock(&dynamic_netconsole_mutex);
522 pr_err("target (%s) is enabled, disable to update parameters\n",
523 config_item_name(&nt->group.cg_item));
527 if (strnchr(buf, count, ':')) {
530 if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
531 if (*end && *end != '\n') {
532 pr_err("invalid IPv6 address at: <%c>\n", *end);
540 nt->np.local_ip.ip = in_aton(buf);
545 mutex_unlock(&dynamic_netconsole_mutex);
546 return strnlen(buf, count);
548 mutex_unlock(&dynamic_netconsole_mutex);
552 static ssize_t remote_ip_store(struct config_item *item, const char *buf,
555 struct netconsole_target *nt = to_target(item);
557 mutex_lock(&dynamic_netconsole_mutex);
559 pr_err("target (%s) is enabled, disable to update parameters\n",
560 config_item_name(&nt->group.cg_item));
564 if (strnchr(buf, count, ':')) {
567 if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
568 if (*end && *end != '\n') {
569 pr_err("invalid IPv6 address at: <%c>\n", *end);
577 nt->np.remote_ip.ip = in_aton(buf);
582 mutex_unlock(&dynamic_netconsole_mutex);
583 return strnlen(buf, count);
585 mutex_unlock(&dynamic_netconsole_mutex);
589 static ssize_t remote_mac_store(struct config_item *item, const char *buf,
592 struct netconsole_target *nt = to_target(item);
593 u8 remote_mac[ETH_ALEN];
595 mutex_lock(&dynamic_netconsole_mutex);
597 pr_err("target (%s) is enabled, disable to update parameters\n",
598 config_item_name(&nt->group.cg_item));
602 if (!mac_pton(buf, remote_mac))
604 if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
606 memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
608 mutex_unlock(&dynamic_netconsole_mutex);
609 return strnlen(buf, count);
611 mutex_unlock(&dynamic_netconsole_mutex);
616 struct config_item item;
617 char value[MAX_USERDATA_VALUE_LENGTH];
620 static struct userdatum *to_userdatum(struct config_item *item)
622 return container_of(item, struct userdatum, item);
626 struct config_group group;
629 static struct userdata *to_userdata(struct config_item *item)
631 return container_of(to_config_group(item), struct userdata, group);
634 static struct netconsole_target *userdata_to_target(struct userdata *ud)
636 struct config_group *netconsole_group;
638 netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
639 return to_target(&netconsole_group->cg_item);
642 static ssize_t userdatum_value_show(struct config_item *item, char *buf)
644 return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
647 static void update_userdata(struct netconsole_target *nt)
649 int complete_idx = 0, child_count = 0;
650 struct list_head *entry;
652 /* Clear the current string in case the last userdatum was deleted */
653 nt->userdata_length = 0;
654 nt->userdata_complete[0] = 0;
656 list_for_each(entry, &nt->userdata_group.cg_children) {
657 struct userdatum *udm_item;
658 struct config_item *item;
660 if (child_count >= MAX_USERDATA_ITEMS)
664 item = container_of(entry, struct config_item, ci_entry);
665 udm_item = to_userdatum(item);
667 /* Skip userdata with no value set */
668 if (strnlen(udm_item->value, MAX_USERDATA_VALUE_LENGTH) == 0)
671 /* This doesn't overflow userdata_complete since it will write
672 * one entry length (1/MAX_USERDATA_ITEMS long), entry count is
673 * checked to not exceed MAX items with child_count above
675 complete_idx += scnprintf(&nt->userdata_complete[complete_idx],
676 MAX_USERDATA_ENTRY_LENGTH, " %s=%s\n",
677 item->ci_name, udm_item->value);
679 nt->userdata_length = strnlen(nt->userdata_complete,
680 sizeof(nt->userdata_complete));
683 static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
686 struct userdatum *udm = to_userdatum(item);
687 struct netconsole_target *nt;
691 if (count > MAX_USERDATA_VALUE_LENGTH)
694 mutex_lock(&dynamic_netconsole_mutex);
696 ret = strscpy(udm->value, buf, sizeof(udm->value));
699 trim_newline(udm->value, sizeof(udm->value));
701 ud = to_userdata(item->ci_parent);
702 nt = userdata_to_target(ud);
705 mutex_unlock(&dynamic_netconsole_mutex);
708 mutex_unlock(&dynamic_netconsole_mutex);
712 CONFIGFS_ATTR(userdatum_, value);
714 static struct configfs_attribute *userdatum_attrs[] = {
715 &userdatum_attr_value,
719 static void userdatum_release(struct config_item *item)
721 kfree(to_userdatum(item));
724 static struct configfs_item_operations userdatum_ops = {
725 .release = userdatum_release,
728 static const struct config_item_type userdatum_type = {
729 .ct_item_ops = &userdatum_ops,
730 .ct_attrs = userdatum_attrs,
731 .ct_owner = THIS_MODULE,
734 static struct config_item *userdatum_make_item(struct config_group *group,
737 struct netconsole_target *nt;
738 struct userdatum *udm;
742 if (strlen(name) > MAX_USERDATA_NAME_LENGTH)
743 return ERR_PTR(-ENAMETOOLONG);
745 ud = to_userdata(&group->cg_item);
746 nt = userdata_to_target(ud);
747 child_count = list_count_nodes(&nt->userdata_group.cg_children);
748 if (child_count >= MAX_USERDATA_ITEMS)
749 return ERR_PTR(-ENOSPC);
751 udm = kzalloc(sizeof(*udm), GFP_KERNEL);
753 return ERR_PTR(-ENOMEM);
755 config_item_init_type_name(&udm->item, name, &userdatum_type);
759 static void userdatum_drop(struct config_group *group, struct config_item *item)
761 struct netconsole_target *nt;
764 ud = to_userdata(&group->cg_item);
765 nt = userdata_to_target(ud);
767 mutex_lock(&dynamic_netconsole_mutex);
769 config_item_put(item);
770 mutex_unlock(&dynamic_netconsole_mutex);
773 static struct configfs_attribute *userdata_attrs[] = {
777 static struct configfs_group_operations userdata_ops = {
778 .make_item = userdatum_make_item,
779 .drop_item = userdatum_drop,
782 static struct config_item_type userdata_type = {
783 .ct_item_ops = &userdatum_ops,
784 .ct_group_ops = &userdata_ops,
785 .ct_attrs = userdata_attrs,
786 .ct_owner = THIS_MODULE,
789 CONFIGFS_ATTR(, enabled);
790 CONFIGFS_ATTR(, extended);
791 CONFIGFS_ATTR(, dev_name);
792 CONFIGFS_ATTR(, local_port);
793 CONFIGFS_ATTR(, remote_port);
794 CONFIGFS_ATTR(, local_ip);
795 CONFIGFS_ATTR(, remote_ip);
796 CONFIGFS_ATTR_RO(, local_mac);
797 CONFIGFS_ATTR(, remote_mac);
798 CONFIGFS_ATTR(, release);
800 static struct configfs_attribute *netconsole_target_attrs[] = {
815 * Item operations and type for netconsole_target.
818 static void netconsole_target_release(struct config_item *item)
820 kfree(to_target(item));
823 static struct configfs_item_operations netconsole_target_item_ops = {
824 .release = netconsole_target_release,
827 static const struct config_item_type netconsole_target_type = {
828 .ct_attrs = netconsole_target_attrs,
829 .ct_item_ops = &netconsole_target_item_ops,
830 .ct_owner = THIS_MODULE,
833 static void init_target_config_group(struct netconsole_target *nt,
836 config_group_init_type_name(&nt->group, name, &netconsole_target_type);
837 config_group_init_type_name(&nt->userdata_group, "userdata",
839 configfs_add_default_group(&nt->userdata_group, &nt->group);
842 static struct netconsole_target *find_cmdline_target(const char *name)
844 struct netconsole_target *nt, *ret = NULL;
847 spin_lock_irqsave(&target_list_lock, flags);
848 list_for_each_entry(nt, &target_list, list) {
849 if (!strcmp(nt->group.cg_item.ci_name, name)) {
854 spin_unlock_irqrestore(&target_list_lock, flags);
860 * Group operations and type for netconsole_subsys.
863 static struct config_group *make_netconsole_target(struct config_group *group,
866 struct netconsole_target *nt;
869 /* Checking if a target by this name was created at boot time. If so,
870 * attach a configfs entry to that target. This enables dynamic
873 if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
874 strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
875 nt = find_cmdline_target(name);
877 init_target_config_group(nt, name);
882 nt = alloc_and_init();
884 return ERR_PTR(-ENOMEM);
886 /* Initialize the config_group member */
887 init_target_config_group(nt, name);
889 /* Adding, but it is disabled */
890 spin_lock_irqsave(&target_list_lock, flags);
891 list_add(&nt->list, &target_list);
892 spin_unlock_irqrestore(&target_list_lock, flags);
897 static void drop_netconsole_target(struct config_group *group,
898 struct config_item *item)
901 struct netconsole_target *nt = to_target(item);
903 spin_lock_irqsave(&target_list_lock, flags);
905 spin_unlock_irqrestore(&target_list_lock, flags);
908 * The target may have never been enabled, or was manually disabled
909 * before being removed so netpoll may have already been cleaned up.
912 netpoll_cleanup(&nt->np);
914 config_item_put(&nt->group.cg_item);
917 static struct configfs_group_operations netconsole_subsys_group_ops = {
918 .make_group = make_netconsole_target,
919 .drop_item = drop_netconsole_target,
922 static const struct config_item_type netconsole_subsys_type = {
923 .ct_group_ops = &netconsole_subsys_group_ops,
924 .ct_owner = THIS_MODULE,
927 /* The netconsole configfs subsystem */
928 static struct configfs_subsystem netconsole_subsys = {
931 .ci_namebuf = "netconsole",
932 .ci_type = &netconsole_subsys_type,
937 static void populate_configfs_item(struct netconsole_target *nt,
940 char target_name[16];
942 snprintf(target_name, sizeof(target_name), "%s%d",
943 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
944 init_target_config_group(nt, target_name);
947 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
949 /* Handle network interface device notifications */
950 static int netconsole_netdev_event(struct notifier_block *this,
951 unsigned long event, void *ptr)
954 struct netconsole_target *nt;
955 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
956 bool stopped = false;
958 if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
959 event == NETDEV_RELEASE || event == NETDEV_JOIN))
962 spin_lock_irqsave(&target_list_lock, flags);
964 list_for_each_entry(nt, &target_list, list) {
965 netconsole_target_get(nt);
966 if (nt->np.dev == dev) {
968 case NETDEV_CHANGENAME:
969 strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
973 case NETDEV_UNREGISTER:
974 /* rtnl_lock already held
975 * we might sleep in __netpoll_cleanup()
977 spin_unlock_irqrestore(&target_list_lock, flags);
979 __netpoll_cleanup(&nt->np);
981 spin_lock_irqsave(&target_list_lock, flags);
982 netdev_put(nt->np.dev, &nt->np.dev_tracker);
986 netconsole_target_put(nt);
990 netconsole_target_put(nt);
992 spin_unlock_irqrestore(&target_list_lock, flags);
994 const char *msg = "had an event";
997 case NETDEV_UNREGISTER:
998 msg = "unregistered";
1000 case NETDEV_RELEASE:
1001 msg = "released slaves";
1004 msg = "is joining a master device";
1007 pr_info("network logging stopped on interface %s as it %s\n",
1015 static struct notifier_block netconsole_netdev_notifier = {
1016 .notifier_call = netconsole_netdev_event,
1020 * send_ext_msg_udp - send extended log message to target
1021 * @nt: target to send message to
1022 * @msg: extended log message to send
1023 * @msg_len: length of message
1025 * Transfer extended log @msg to @nt. If @msg is longer than
1026 * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
1027 * ncfrag header field added to identify them.
1029 static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
1032 static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
1033 const char *header, *body;
1035 int header_len, body_len;
1036 const char *msg_ready = msg;
1037 const char *release;
1038 int release_len = 0;
1039 int userdata_len = 0;
1040 char *userdata = NULL;
1042 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1043 userdata = nt->userdata_complete;
1044 userdata_len = nt->userdata_length;
1048 release = init_utsname()->release;
1049 release_len = strlen(release) + 1;
1052 if (msg_len + release_len + userdata_len <= MAX_PRINT_CHUNK) {
1053 /* No fragmentation needed */
1055 scnprintf(buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
1056 msg_len += release_len;
1058 memcpy(buf, msg, msg_len);
1062 msg_len += scnprintf(&buf[msg_len],
1063 MAX_PRINT_CHUNK - msg_len,
1067 netpoll_send_udp(&nt->np, msg_ready, msg_len);
1071 /* need to insert extra header fields, detect header and body */
1073 body = memchr(msg, ';', msg_len);
1074 if (WARN_ON_ONCE(!body))
1077 header_len = body - header;
1078 body_len = msg_len - header_len - 1;
1082 * Transfer multiple chunks with the following extra header.
1083 * "ncfrag=<byte-offset>/<total-bytes>"
1086 scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
1087 memcpy(buf + release_len, header, header_len);
1088 header_len += release_len;
1090 while (offset < body_len + userdata_len) {
1091 int this_header = header_len;
1092 int this_offset = 0;
1095 this_header += scnprintf(buf + this_header,
1096 sizeof(buf) - this_header,
1097 ",ncfrag=%d/%d;", offset,
1098 body_len + userdata_len);
1100 /* Not all body data has been written yet */
1101 if (offset < body_len) {
1102 this_chunk = min(body_len - offset,
1103 MAX_PRINT_CHUNK - this_header);
1104 if (WARN_ON_ONCE(this_chunk <= 0))
1106 memcpy(buf + this_header, body + offset, this_chunk);
1107 this_offset += this_chunk;
1109 /* Body is fully written and there is pending userdata to write,
1110 * append userdata in this chunk
1112 if (offset + this_offset >= body_len &&
1113 offset + this_offset < userdata_len + body_len) {
1114 int sent_userdata = (offset + this_offset) - body_len;
1115 int preceding_bytes = this_chunk + this_header;
1117 if (WARN_ON_ONCE(sent_userdata < 0))
1120 this_chunk = min(userdata_len - sent_userdata,
1121 MAX_PRINT_CHUNK - preceding_bytes);
1122 if (WARN_ON_ONCE(this_chunk <= 0))
1124 memcpy(buf + this_header + this_offset,
1125 userdata + sent_userdata,
1127 this_offset += this_chunk;
1130 netpoll_send_udp(&nt->np, buf, this_header + this_offset);
1131 offset += this_offset;
1135 static void write_ext_msg(struct console *con, const char *msg,
1138 struct netconsole_target *nt;
1139 unsigned long flags;
1141 if ((oops_only && !oops_in_progress) || list_empty(&target_list))
1144 spin_lock_irqsave(&target_list_lock, flags);
1145 list_for_each_entry(nt, &target_list, list)
1146 if (nt->extended && nt->enabled && netif_running(nt->np.dev))
1147 send_ext_msg_udp(nt, msg, len);
1148 spin_unlock_irqrestore(&target_list_lock, flags);
1151 static void write_msg(struct console *con, const char *msg, unsigned int len)
1154 unsigned long flags;
1155 struct netconsole_target *nt;
1158 if (oops_only && !oops_in_progress)
1160 /* Avoid taking lock and disabling interrupts unnecessarily */
1161 if (list_empty(&target_list))
1164 spin_lock_irqsave(&target_list_lock, flags);
1165 list_for_each_entry(nt, &target_list, list) {
1166 if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
1168 * We nest this inside the for-each-target loop above
1169 * so that we're able to get as much logging out to
1170 * at least one target if we die inside here, instead
1171 * of unnecessarily keeping all targets in lock-step.
1174 for (left = len; left;) {
1175 frag = min(left, MAX_PRINT_CHUNK);
1176 netpoll_send_udp(&nt->np, tmp, frag);
1182 spin_unlock_irqrestore(&target_list_lock, flags);
1185 /* Allocate new target (from boot/module param) and setup netpoll for it */
1186 static struct netconsole_target *alloc_param_target(char *target_config,
1189 struct netconsole_target *nt;
1192 nt = alloc_and_init();
1198 if (*target_config == '+') {
1199 nt->extended = true;
1203 if (*target_config == 'r') {
1204 if (!nt->extended) {
1205 pr_err("Netconsole configuration error. Release feature requires extended log message");
1213 /* Parse parameters and setup netpoll */
1214 err = netpoll_parse_options(&nt->np, target_config);
1218 err = netpoll_setup(&nt->np);
1222 populate_configfs_item(nt, cmdline_count);
1229 return ERR_PTR(err);
1232 /* Cleanup netpoll for given target (from boot/module param) and free it */
1233 static void free_param_target(struct netconsole_target *nt)
1235 netpoll_cleanup(&nt->np);
1239 static struct console netconsole_ext = {
1240 .name = "netcon_ext",
1241 .flags = CON_ENABLED | CON_EXTENDED,
1242 .write = write_ext_msg,
1245 static struct console netconsole = {
1247 .flags = CON_ENABLED,
1251 static int __init init_netconsole(void)
1254 struct netconsole_target *nt, *tmp;
1255 unsigned int count = 0;
1256 bool extended = false;
1257 unsigned long flags;
1258 char *target_config;
1259 char *input = config;
1261 if (strnlen(input, MAX_PARAM_LENGTH)) {
1262 while ((target_config = strsep(&input, ";"))) {
1263 nt = alloc_param_target(target_config, count);
1268 /* Dump existing printks when we register */
1271 netconsole_ext.flags |= CON_PRINTBUFFER;
1273 netconsole.flags |= CON_PRINTBUFFER;
1276 spin_lock_irqsave(&target_list_lock, flags);
1277 list_add(&nt->list, &target_list);
1278 spin_unlock_irqrestore(&target_list_lock, flags);
1283 err = register_netdevice_notifier(&netconsole_netdev_notifier);
1287 err = dynamic_netconsole_init();
1292 register_console(&netconsole_ext);
1293 register_console(&netconsole);
1294 pr_info("network logging started\n");
1299 unregister_netdevice_notifier(&netconsole_netdev_notifier);
1302 pr_err("cleaning up\n");
1305 * Remove all targets and destroy them (only targets created
1306 * from the boot/module option exist here). Skipping the list
1307 * lock is safe here, and netpoll_cleanup() will sleep.
1309 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1310 list_del(&nt->list);
1311 free_param_target(nt);
1317 static void __exit cleanup_netconsole(void)
1319 struct netconsole_target *nt, *tmp;
1321 if (console_is_registered(&netconsole_ext))
1322 unregister_console(&netconsole_ext);
1323 unregister_console(&netconsole);
1324 dynamic_netconsole_exit();
1325 unregister_netdevice_notifier(&netconsole_netdev_notifier);
1328 * Targets created via configfs pin references on our module
1329 * and would first be rmdir(2)'ed from userspace. We reach
1330 * here only when they are already destroyed, and only those
1331 * created from the boot/module option are left, so remove and
1332 * destroy them. Skipping the list lock is safe here, and
1333 * netpoll_cleanup() will sleep.
1335 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1336 list_del(&nt->list);
1337 free_param_target(nt);
1342 * Use late_initcall to ensure netconsole is
1343 * initialized after network device driver if built-in.
1345 * late_initcall() and module_init() are identical if built as module.
1347 late_initcall(init_netconsole);
1348 module_exit(cleanup_netconsole);