1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * linux/drivers/net/netconsole.c
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/u64_stats_sync.h>
40 #include <linux/utsname.h>
41 #include <linux/rtnetlink.h>
44 MODULE_DESCRIPTION("Console driver for network interfaces");
45 MODULE_LICENSE("GPL");
47 #define MAX_PARAM_LENGTH 256
48 #define MAX_USERDATA_ENTRY_LENGTH 256
49 #define MAX_USERDATA_VALUE_LENGTH 200
50 /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
51 #define MAX_USERDATA_NAME_LENGTH (MAX_USERDATA_ENTRY_LENGTH - \
52 MAX_USERDATA_VALUE_LENGTH - 3)
53 #define MAX_USERDATA_ITEMS 16
54 #define MAX_PRINT_CHUNK 1000
56 static char config[MAX_PARAM_LENGTH];
57 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
58 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
60 static bool oops_only;
61 module_param(oops_only, bool, 0600);
62 MODULE_PARM_DESC(oops_only, "Only log oops messages");
64 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
67 static int __init option_setup(char *opt)
69 strscpy(config, opt, MAX_PARAM_LENGTH);
72 __setup("netconsole=", option_setup);
75 /* Linked list of all configured targets */
76 static LIST_HEAD(target_list);
77 /* target_cleanup_list is used to track targets that need to be cleaned outside
78 * of target_list_lock. It should be cleaned in the same function it is
81 static LIST_HEAD(target_cleanup_list);
83 /* This needs to be a spinlock because write_msg() cannot sleep */
84 static DEFINE_SPINLOCK(target_list_lock);
85 /* This needs to be a mutex because netpoll_cleanup might sleep */
86 static DEFINE_MUTEX(target_cleanup_list_lock);
89 * Console driver for extended netconsoles. Registered on the first use to
90 * avoid unnecessarily enabling ext message formatting.
92 static struct console netconsole_ext;
94 struct netconsole_target_stats {
95 u64_stats_t xmit_drop_count;
96 u64_stats_t enomem_count;
97 struct u64_stats_sync syncp;
101 * struct netconsole_target - Represents a configured netconsole target.
102 * @list: Links this target into the target_list.
103 * @group: Links us into the configfs subsystem hierarchy.
104 * @userdata_group: Links to the userdata configfs hierarchy
105 * @userdata_complete: Cached, formatted string of append
106 * @userdata_length: String length of userdata_complete
107 * @stats: Packet send stats for the target. Used for debugging.
108 * @enabled: On / off knob to enable / disable target.
109 * Visible from userspace (read-write).
110 * We maintain a strict 1:1 correspondence between this and
111 * whether the corresponding netpoll is active or inactive.
112 * Also, other parameters of a target may be modified at
113 * runtime only when it is disabled (enabled == 0).
114 * @extended: Denotes whether console is extended or not.
115 * @release: Denotes whether kernel release version should be prepended
116 * to the message. Depends on extended console.
117 * @np: The netpoll structure for this target.
118 * Contains the other userspace visible parameters:
119 * dev_name (read-write)
120 * local_port (read-write)
121 * remote_port (read-write)
122 * local_ip (read-write)
123 * remote_ip (read-write)
124 * local_mac (read-only)
125 * remote_mac (read-write)
127 struct netconsole_target {
128 struct list_head list;
129 #ifdef CONFIG_NETCONSOLE_DYNAMIC
130 struct config_group group;
131 struct config_group userdata_group;
132 char userdata_complete[MAX_USERDATA_ENTRY_LENGTH * MAX_USERDATA_ITEMS];
133 size_t userdata_length;
135 struct netconsole_target_stats stats;
142 #ifdef CONFIG_NETCONSOLE_DYNAMIC
144 static struct configfs_subsystem netconsole_subsys;
145 static DEFINE_MUTEX(dynamic_netconsole_mutex);
147 static int __init dynamic_netconsole_init(void)
149 config_group_init(&netconsole_subsys.su_group);
150 mutex_init(&netconsole_subsys.su_mutex);
151 return configfs_register_subsystem(&netconsole_subsys);
154 static void __exit dynamic_netconsole_exit(void)
156 configfs_unregister_subsystem(&netconsole_subsys);
160 * Targets that were created by parsing the boot/module option string
161 * do not exist in the configfs hierarchy (and have NULL names) and will
162 * never go away, so make these a no-op for them.
164 static void netconsole_target_get(struct netconsole_target *nt)
166 if (config_item_name(&nt->group.cg_item))
167 config_group_get(&nt->group);
170 static void netconsole_target_put(struct netconsole_target *nt)
172 if (config_item_name(&nt->group.cg_item))
173 config_group_put(&nt->group);
176 #else /* !CONFIG_NETCONSOLE_DYNAMIC */
178 static int __init dynamic_netconsole_init(void)
183 static void __exit dynamic_netconsole_exit(void)
188 * No danger of targets going away from under us when dynamic
189 * reconfigurability is off.
191 static void netconsole_target_get(struct netconsole_target *nt)
195 static void netconsole_target_put(struct netconsole_target *nt)
199 static void populate_configfs_item(struct netconsole_target *nt,
203 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
205 /* Allocate and initialize with defaults.
206 * Note that these targets get their config_item fields zeroed-out.
208 static struct netconsole_target *alloc_and_init(void)
210 struct netconsole_target *nt;
212 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
216 if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
218 if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
221 nt->np.name = "netconsole";
222 strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
223 nt->np.local_port = 6665;
224 nt->np.remote_port = 6666;
225 eth_broadcast_addr(nt->np.remote_mac);
230 /* Clean up every target in the cleanup_list and move the clean targets back to
231 * the main target_list.
233 static void netconsole_process_cleanups_core(void)
235 struct netconsole_target *nt, *tmp;
238 /* The cleanup needs RTNL locked */
241 mutex_lock(&target_cleanup_list_lock);
242 list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {
243 /* all entries in the cleanup_list needs to be disabled */
244 WARN_ON_ONCE(nt->enabled);
245 do_netpoll_cleanup(&nt->np);
246 /* moved the cleaned target to target_list. Need to hold both
249 spin_lock_irqsave(&target_list_lock, flags);
250 list_move(&nt->list, &target_list);
251 spin_unlock_irqrestore(&target_list_lock, flags);
253 WARN_ON_ONCE(!list_empty(&target_cleanup_list));
254 mutex_unlock(&target_cleanup_list_lock);
257 #ifdef CONFIG_NETCONSOLE_DYNAMIC
260 * Our subsystem hierarchy is:
262 * /sys/kernel/config/netconsole/
283 static struct netconsole_target *to_target(struct config_item *item)
285 struct config_group *cfg_group;
287 cfg_group = to_config_group(item);
290 return container_of(to_config_group(item),
291 struct netconsole_target, group);
294 /* Do the list cleanup with the rtnl lock hold. rtnl lock is necessary because
295 * netdev might be cleaned-up by calling __netpoll_cleanup(),
297 static void netconsole_process_cleanups(void)
299 /* rtnl lock is called here, because it has precedence over
300 * target_cleanup_list_lock mutex and target_cleanup_list
303 netconsole_process_cleanups_core();
307 /* Get rid of possible trailing newline, returning the new length */
308 static void trim_newline(char *s, size_t maxlen)
312 len = strnlen(s, maxlen);
313 if (s[len - 1] == '\n')
318 * Attribute operations for netconsole_target.
321 static ssize_t enabled_show(struct config_item *item, char *buf)
323 return sysfs_emit(buf, "%d\n", to_target(item)->enabled);
326 static ssize_t extended_show(struct config_item *item, char *buf)
328 return sysfs_emit(buf, "%d\n", to_target(item)->extended);
331 static ssize_t release_show(struct config_item *item, char *buf)
333 return sysfs_emit(buf, "%d\n", to_target(item)->release);
336 static ssize_t dev_name_show(struct config_item *item, char *buf)
338 return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
341 static ssize_t local_port_show(struct config_item *item, char *buf)
343 return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
346 static ssize_t remote_port_show(struct config_item *item, char *buf)
348 return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
351 static ssize_t local_ip_show(struct config_item *item, char *buf)
353 struct netconsole_target *nt = to_target(item);
356 return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
358 return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
361 static ssize_t remote_ip_show(struct config_item *item, char *buf)
363 struct netconsole_target *nt = to_target(item);
366 return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
368 return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
371 static ssize_t local_mac_show(struct config_item *item, char *buf)
373 struct net_device *dev = to_target(item)->np.dev;
374 static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
376 return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
379 static ssize_t remote_mac_show(struct config_item *item, char *buf)
381 return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
384 static ssize_t transmit_errors_show(struct config_item *item, char *buf)
386 struct netconsole_target *nt = to_target(item);
387 u64 xmit_drop_count, enomem_count;
391 start = u64_stats_fetch_begin(&nt->stats.syncp);
392 xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count);
393 enomem_count = u64_stats_read(&nt->stats.enomem_count);
394 } while (u64_stats_fetch_retry(&nt->stats.syncp, start));
396 return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count);
400 * This one is special -- targets created through the configfs interface
401 * are not enabled (and the corresponding netpoll activated) by default.
402 * The user is expected to set the desired parameters first (which
403 * would enable him to dynamically add new netpoll targets for new
404 * network interfaces as and when they come up).
406 static ssize_t enabled_store(struct config_item *item,
407 const char *buf, size_t count)
409 struct netconsole_target *nt = to_target(item);
414 mutex_lock(&dynamic_netconsole_mutex);
415 ret = kstrtobool(buf, &enabled);
420 if (enabled == nt->enabled) {
421 pr_info("network logging has already %s\n",
422 nt->enabled ? "started" : "stopped");
426 if (enabled) { /* true */
427 if (nt->release && !nt->extended) {
428 pr_err("Not enabling netconsole. Release feature requires extended log message");
432 if (nt->extended && !console_is_registered(&netconsole_ext))
433 register_console(&netconsole_ext);
436 * Skip netpoll_parse_options() -- all the attributes are
437 * already configured via configfs. Just print them out.
439 netpoll_print_options(&nt->np);
441 ret = netpoll_setup(&nt->np);
446 pr_info("network logging started\n");
448 /* We need to disable the netconsole before cleaning it up
449 * otherwise we might end up in write_msg() with
450 * nt->np.dev == NULL and nt->enabled == true
452 mutex_lock(&target_cleanup_list_lock);
453 spin_lock_irqsave(&target_list_lock, flags);
455 /* Remove the target from the list, while holding
458 list_move(&nt->list, &target_cleanup_list);
459 spin_unlock_irqrestore(&target_list_lock, flags);
460 mutex_unlock(&target_cleanup_list_lock);
463 ret = strnlen(buf, count);
464 /* Deferred cleanup */
465 netconsole_process_cleanups();
467 mutex_unlock(&dynamic_netconsole_mutex);
471 static ssize_t release_store(struct config_item *item, const char *buf,
474 struct netconsole_target *nt = to_target(item);
478 mutex_lock(&dynamic_netconsole_mutex);
480 pr_err("target (%s) is enabled, disable to update parameters\n",
481 config_item_name(&nt->group.cg_item));
486 ret = kstrtobool(buf, &release);
490 nt->release = release;
492 ret = strnlen(buf, count);
494 mutex_unlock(&dynamic_netconsole_mutex);
498 static ssize_t extended_store(struct config_item *item, const char *buf,
501 struct netconsole_target *nt = to_target(item);
505 mutex_lock(&dynamic_netconsole_mutex);
507 pr_err("target (%s) is enabled, disable to update parameters\n",
508 config_item_name(&nt->group.cg_item));
513 ret = kstrtobool(buf, &extended);
517 nt->extended = extended;
518 ret = strnlen(buf, count);
520 mutex_unlock(&dynamic_netconsole_mutex);
524 static ssize_t dev_name_store(struct config_item *item, const char *buf,
527 struct netconsole_target *nt = to_target(item);
529 mutex_lock(&dynamic_netconsole_mutex);
531 pr_err("target (%s) is enabled, disable to update parameters\n",
532 config_item_name(&nt->group.cg_item));
533 mutex_unlock(&dynamic_netconsole_mutex);
537 strscpy(nt->np.dev_name, buf, IFNAMSIZ);
538 trim_newline(nt->np.dev_name, IFNAMSIZ);
540 mutex_unlock(&dynamic_netconsole_mutex);
541 return strnlen(buf, count);
544 static ssize_t local_port_store(struct config_item *item, const char *buf,
547 struct netconsole_target *nt = to_target(item);
548 ssize_t ret = -EINVAL;
550 mutex_lock(&dynamic_netconsole_mutex);
552 pr_err("target (%s) is enabled, disable to update parameters\n",
553 config_item_name(&nt->group.cg_item));
557 ret = kstrtou16(buf, 10, &nt->np.local_port);
560 ret = strnlen(buf, count);
562 mutex_unlock(&dynamic_netconsole_mutex);
566 static ssize_t remote_port_store(struct config_item *item,
567 const char *buf, size_t count)
569 struct netconsole_target *nt = to_target(item);
570 ssize_t ret = -EINVAL;
572 mutex_lock(&dynamic_netconsole_mutex);
574 pr_err("target (%s) is enabled, disable to update parameters\n",
575 config_item_name(&nt->group.cg_item));
579 ret = kstrtou16(buf, 10, &nt->np.remote_port);
582 ret = strnlen(buf, count);
584 mutex_unlock(&dynamic_netconsole_mutex);
588 static ssize_t local_ip_store(struct config_item *item, const char *buf,
591 struct netconsole_target *nt = to_target(item);
592 ssize_t ret = -EINVAL;
594 mutex_lock(&dynamic_netconsole_mutex);
596 pr_err("target (%s) is enabled, disable to update parameters\n",
597 config_item_name(&nt->group.cg_item));
601 if (strnchr(buf, count, ':')) {
604 if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
605 if (*end && *end != '\n') {
606 pr_err("invalid IPv6 address at: <%c>\n", *end);
614 nt->np.local_ip.ip = in_aton(buf);
619 ret = strnlen(buf, count);
621 mutex_unlock(&dynamic_netconsole_mutex);
625 static ssize_t remote_ip_store(struct config_item *item, const char *buf,
628 struct netconsole_target *nt = to_target(item);
629 ssize_t ret = -EINVAL;
631 mutex_lock(&dynamic_netconsole_mutex);
633 pr_err("target (%s) is enabled, disable to update parameters\n",
634 config_item_name(&nt->group.cg_item));
638 if (strnchr(buf, count, ':')) {
641 if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
642 if (*end && *end != '\n') {
643 pr_err("invalid IPv6 address at: <%c>\n", *end);
651 nt->np.remote_ip.ip = in_aton(buf);
656 ret = strnlen(buf, count);
658 mutex_unlock(&dynamic_netconsole_mutex);
662 static ssize_t remote_mac_store(struct config_item *item, const char *buf,
665 struct netconsole_target *nt = to_target(item);
666 u8 remote_mac[ETH_ALEN];
667 ssize_t ret = -EINVAL;
669 mutex_lock(&dynamic_netconsole_mutex);
671 pr_err("target (%s) is enabled, disable to update parameters\n",
672 config_item_name(&nt->group.cg_item));
676 if (!mac_pton(buf, remote_mac))
678 if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
680 memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
682 ret = strnlen(buf, count);
684 mutex_unlock(&dynamic_netconsole_mutex);
689 struct config_item item;
690 char value[MAX_USERDATA_VALUE_LENGTH];
693 static struct userdatum *to_userdatum(struct config_item *item)
695 return container_of(item, struct userdatum, item);
699 struct config_group group;
702 static struct userdata *to_userdata(struct config_item *item)
704 return container_of(to_config_group(item), struct userdata, group);
707 static struct netconsole_target *userdata_to_target(struct userdata *ud)
709 struct config_group *netconsole_group;
711 netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
712 return to_target(&netconsole_group->cg_item);
715 static ssize_t userdatum_value_show(struct config_item *item, char *buf)
717 return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
720 static void update_userdata(struct netconsole_target *nt)
722 int complete_idx = 0, child_count = 0;
723 struct list_head *entry;
725 /* Clear the current string in case the last userdatum was deleted */
726 nt->userdata_length = 0;
727 nt->userdata_complete[0] = 0;
729 list_for_each(entry, &nt->userdata_group.cg_children) {
730 struct userdatum *udm_item;
731 struct config_item *item;
733 if (WARN_ON_ONCE(child_count >= MAX_USERDATA_ITEMS))
737 item = container_of(entry, struct config_item, ci_entry);
738 udm_item = to_userdatum(item);
740 /* Skip userdata with no value set */
741 if (strnlen(udm_item->value, MAX_USERDATA_VALUE_LENGTH) == 0)
744 /* This doesn't overflow userdata_complete since it will write
745 * one entry length (1/MAX_USERDATA_ITEMS long), entry count is
746 * checked to not exceed MAX items with child_count above
748 complete_idx += scnprintf(&nt->userdata_complete[complete_idx],
749 MAX_USERDATA_ENTRY_LENGTH, " %s=%s\n",
750 item->ci_name, udm_item->value);
752 nt->userdata_length = strnlen(nt->userdata_complete,
753 sizeof(nt->userdata_complete));
756 static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
759 struct userdatum *udm = to_userdatum(item);
760 struct netconsole_target *nt;
764 if (count > MAX_USERDATA_VALUE_LENGTH)
767 mutex_lock(&dynamic_netconsole_mutex);
769 ret = strscpy(udm->value, buf, sizeof(udm->value));
772 trim_newline(udm->value, sizeof(udm->value));
774 ud = to_userdata(item->ci_parent);
775 nt = userdata_to_target(ud);
779 mutex_unlock(&dynamic_netconsole_mutex);
783 CONFIGFS_ATTR(userdatum_, value);
785 static struct configfs_attribute *userdatum_attrs[] = {
786 &userdatum_attr_value,
790 static void userdatum_release(struct config_item *item)
792 kfree(to_userdatum(item));
795 static struct configfs_item_operations userdatum_ops = {
796 .release = userdatum_release,
799 static const struct config_item_type userdatum_type = {
800 .ct_item_ops = &userdatum_ops,
801 .ct_attrs = userdatum_attrs,
802 .ct_owner = THIS_MODULE,
805 static struct config_item *userdatum_make_item(struct config_group *group,
808 struct netconsole_target *nt;
809 struct userdatum *udm;
813 if (strlen(name) > MAX_USERDATA_NAME_LENGTH)
814 return ERR_PTR(-ENAMETOOLONG);
816 ud = to_userdata(&group->cg_item);
817 nt = userdata_to_target(ud);
818 child_count = list_count_nodes(&nt->userdata_group.cg_children);
819 if (child_count >= MAX_USERDATA_ITEMS)
820 return ERR_PTR(-ENOSPC);
822 udm = kzalloc(sizeof(*udm), GFP_KERNEL);
824 return ERR_PTR(-ENOMEM);
826 config_item_init_type_name(&udm->item, name, &userdatum_type);
830 static void userdatum_drop(struct config_group *group, struct config_item *item)
832 struct netconsole_target *nt;
835 ud = to_userdata(&group->cg_item);
836 nt = userdata_to_target(ud);
838 mutex_lock(&dynamic_netconsole_mutex);
840 config_item_put(item);
841 mutex_unlock(&dynamic_netconsole_mutex);
844 static struct configfs_attribute *userdata_attrs[] = {
848 static struct configfs_group_operations userdata_ops = {
849 .make_item = userdatum_make_item,
850 .drop_item = userdatum_drop,
853 static const struct config_item_type userdata_type = {
854 .ct_item_ops = &userdatum_ops,
855 .ct_group_ops = &userdata_ops,
856 .ct_attrs = userdata_attrs,
857 .ct_owner = THIS_MODULE,
860 CONFIGFS_ATTR(, enabled);
861 CONFIGFS_ATTR(, extended);
862 CONFIGFS_ATTR(, dev_name);
863 CONFIGFS_ATTR(, local_port);
864 CONFIGFS_ATTR(, remote_port);
865 CONFIGFS_ATTR(, local_ip);
866 CONFIGFS_ATTR(, remote_ip);
867 CONFIGFS_ATTR_RO(, local_mac);
868 CONFIGFS_ATTR(, remote_mac);
869 CONFIGFS_ATTR(, release);
870 CONFIGFS_ATTR_RO(, transmit_errors);
872 static struct configfs_attribute *netconsole_target_attrs[] = {
883 &attr_transmit_errors,
888 * Item operations and type for netconsole_target.
891 static void netconsole_target_release(struct config_item *item)
893 kfree(to_target(item));
896 static struct configfs_item_operations netconsole_target_item_ops = {
897 .release = netconsole_target_release,
900 static const struct config_item_type netconsole_target_type = {
901 .ct_attrs = netconsole_target_attrs,
902 .ct_item_ops = &netconsole_target_item_ops,
903 .ct_owner = THIS_MODULE,
906 static void init_target_config_group(struct netconsole_target *nt,
909 config_group_init_type_name(&nt->group, name, &netconsole_target_type);
910 config_group_init_type_name(&nt->userdata_group, "userdata",
912 configfs_add_default_group(&nt->userdata_group, &nt->group);
915 static struct netconsole_target *find_cmdline_target(const char *name)
917 struct netconsole_target *nt, *ret = NULL;
920 spin_lock_irqsave(&target_list_lock, flags);
921 list_for_each_entry(nt, &target_list, list) {
922 if (!strcmp(nt->group.cg_item.ci_name, name)) {
927 spin_unlock_irqrestore(&target_list_lock, flags);
933 * Group operations and type for netconsole_subsys.
936 static struct config_group *make_netconsole_target(struct config_group *group,
939 struct netconsole_target *nt;
942 /* Checking if a target by this name was created at boot time. If so,
943 * attach a configfs entry to that target. This enables dynamic
946 if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
947 strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
948 nt = find_cmdline_target(name);
950 init_target_config_group(nt, name);
955 nt = alloc_and_init();
957 return ERR_PTR(-ENOMEM);
959 /* Initialize the config_group member */
960 init_target_config_group(nt, name);
962 /* Adding, but it is disabled */
963 spin_lock_irqsave(&target_list_lock, flags);
964 list_add(&nt->list, &target_list);
965 spin_unlock_irqrestore(&target_list_lock, flags);
970 static void drop_netconsole_target(struct config_group *group,
971 struct config_item *item)
974 struct netconsole_target *nt = to_target(item);
976 spin_lock_irqsave(&target_list_lock, flags);
978 spin_unlock_irqrestore(&target_list_lock, flags);
981 * The target may have never been enabled, or was manually disabled
982 * before being removed so netpoll may have already been cleaned up.
985 netpoll_cleanup(&nt->np);
987 config_item_put(&nt->group.cg_item);
990 static struct configfs_group_operations netconsole_subsys_group_ops = {
991 .make_group = make_netconsole_target,
992 .drop_item = drop_netconsole_target,
995 static const struct config_item_type netconsole_subsys_type = {
996 .ct_group_ops = &netconsole_subsys_group_ops,
997 .ct_owner = THIS_MODULE,
1000 /* The netconsole configfs subsystem */
1001 static struct configfs_subsystem netconsole_subsys = {
1004 .ci_namebuf = "netconsole",
1005 .ci_type = &netconsole_subsys_type,
1010 static void populate_configfs_item(struct netconsole_target *nt,
1013 char target_name[16];
1015 snprintf(target_name, sizeof(target_name), "%s%d",
1016 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1017 init_target_config_group(nt, target_name);
1020 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
1022 /* Handle network interface device notifications */
1023 static int netconsole_netdev_event(struct notifier_block *this,
1024 unsigned long event, void *ptr)
1026 unsigned long flags;
1027 struct netconsole_target *nt, *tmp;
1028 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1029 bool stopped = false;
1031 if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
1032 event == NETDEV_RELEASE || event == NETDEV_JOIN))
1035 mutex_lock(&target_cleanup_list_lock);
1036 spin_lock_irqsave(&target_list_lock, flags);
1037 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1038 netconsole_target_get(nt);
1039 if (nt->np.dev == dev) {
1041 case NETDEV_CHANGENAME:
1042 strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
1044 case NETDEV_RELEASE:
1046 case NETDEV_UNREGISTER:
1047 nt->enabled = false;
1048 list_move(&nt->list, &target_cleanup_list);
1052 netconsole_target_put(nt);
1054 spin_unlock_irqrestore(&target_list_lock, flags);
1055 mutex_unlock(&target_cleanup_list_lock);
1058 const char *msg = "had an event";
1061 case NETDEV_UNREGISTER:
1062 msg = "unregistered";
1064 case NETDEV_RELEASE:
1065 msg = "released slaves";
1068 msg = "is joining a master device";
1071 pr_info("network logging stopped on interface %s as it %s\n",
1075 /* Process target_cleanup_list entries. By the end, target_cleanup_list
1078 netconsole_process_cleanups_core();
1084 static struct notifier_block netconsole_netdev_notifier = {
1085 .notifier_call = netconsole_netdev_event,
1089 * send_udp - Wrapper for netpoll_send_udp that counts errors
1090 * @nt: target to send message to
1091 * @msg: message to send
1092 * @len: length of message
1094 * Calls netpoll_send_udp and classifies the return value. If an error
1095 * occurred it increments statistics in nt->stats accordingly.
1096 * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled.
1098 static void send_udp(struct netconsole_target *nt, const char *msg, int len)
1100 int result = netpoll_send_udp(&nt->np, msg, len);
1102 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) {
1103 if (result == NET_XMIT_DROP) {
1104 u64_stats_update_begin(&nt->stats.syncp);
1105 u64_stats_inc(&nt->stats.xmit_drop_count);
1106 u64_stats_update_end(&nt->stats.syncp);
1107 } else if (result == -ENOMEM) {
1108 u64_stats_update_begin(&nt->stats.syncp);
1109 u64_stats_inc(&nt->stats.enomem_count);
1110 u64_stats_update_end(&nt->stats.syncp);
1115 static void send_msg_no_fragmentation(struct netconsole_target *nt,
1120 static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
1121 const char *userdata = NULL;
1122 const char *release;
1124 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1125 userdata = nt->userdata_complete;
1129 release = init_utsname()->release;
1131 scnprintf(buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
1132 msg_len += release_len;
1134 memcpy(buf, msg, msg_len);
1138 msg_len += scnprintf(&buf[msg_len],
1139 MAX_PRINT_CHUNK - msg_len,
1142 send_udp(nt, buf, msg_len);
1145 static void append_release(char *buf)
1147 const char *release;
1149 release = init_utsname()->release;
1150 scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
1153 static void send_fragmented_body(struct netconsole_target *nt, char *buf,
1154 const char *msgbody, int header_len,
1157 const char *userdata = NULL;
1158 int body_len, offset = 0;
1159 int userdata_len = 0;
1161 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1162 userdata = nt->userdata_complete;
1163 userdata_len = nt->userdata_length;
1166 /* body_len represents the number of bytes that will be sent. This is
1167 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple
1170 body_len = msgbody_len + userdata_len;
1172 /* In each iteration of the while loop below, we send a packet
1173 * containing the header and a portion of the body. The body is
1174 * composed of two parts: msgbody and userdata. We keep track of how
1175 * many bytes have been sent so far using the offset variable, which
1176 * ranges from 0 to the total length of the body.
1178 while (offset < body_len) {
1179 int this_header = header_len;
1180 bool msgbody_written = false;
1181 int this_offset = 0;
1184 this_header += scnprintf(buf + this_header,
1185 MAX_PRINT_CHUNK - this_header,
1186 ",ncfrag=%d/%d;", offset,
1189 /* Not all msgbody data has been written yet */
1190 if (offset < msgbody_len) {
1191 this_chunk = min(msgbody_len - offset,
1192 MAX_PRINT_CHUNK - this_header);
1193 if (WARN_ON_ONCE(this_chunk <= 0))
1195 memcpy(buf + this_header, msgbody + offset, this_chunk);
1196 this_offset += this_chunk;
1199 /* msgbody was finally written, either in the previous
1200 * messages and/or in the current buf. Time to write
1203 msgbody_written |= offset + this_offset >= msgbody_len;
1205 /* Msg body is fully written and there is pending userdata to
1206 * write, append userdata in this chunk
1208 if (msgbody_written && offset + this_offset < body_len) {
1209 /* Track how much user data was already sent. First
1210 * time here, sent_userdata is zero
1212 int sent_userdata = (offset + this_offset) - msgbody_len;
1213 /* offset of bytes used in current buf */
1214 int preceding_bytes = this_chunk + this_header;
1216 if (WARN_ON_ONCE(sent_userdata < 0))
1219 this_chunk = min(userdata_len - sent_userdata,
1220 MAX_PRINT_CHUNK - preceding_bytes);
1221 if (WARN_ON_ONCE(this_chunk < 0))
1222 /* this_chunk could be zero if all the previous
1223 * message used all the buffer. This is not a
1224 * problem, userdata will be sent in the next
1229 memcpy(buf + this_header + this_offset,
1230 userdata + sent_userdata,
1232 this_offset += this_chunk;
1235 send_udp(nt, buf, this_header + this_offset);
1236 offset += this_offset;
1240 static void send_msg_fragmented(struct netconsole_target *nt,
1245 static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
1246 int header_len, msgbody_len;
1247 const char *msgbody;
1249 /* need to insert extra header fields, detect header and msgbody */
1250 msgbody = memchr(msg, ';', msg_len);
1251 if (WARN_ON_ONCE(!msgbody))
1254 header_len = msgbody - msg;
1255 msgbody_len = msg_len - header_len - 1;
1259 * Transfer multiple chunks with the following extra header.
1260 * "ncfrag=<byte-offset>/<total-bytes>"
1263 append_release(buf);
1265 /* Copy the header into the buffer */
1266 memcpy(buf + release_len, msg, header_len);
1267 header_len += release_len;
1269 /* for now on, the header will be persisted, and the msgbody
1272 send_fragmented_body(nt, buf, msgbody, header_len, msgbody_len);
1276 * send_ext_msg_udp - send extended log message to target
1277 * @nt: target to send message to
1278 * @msg: extended log message to send
1279 * @msg_len: length of message
1281 * Transfer extended log @msg to @nt. If @msg is longer than
1282 * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
1283 * ncfrag header field added to identify them.
1285 static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
1288 int userdata_len = 0;
1289 int release_len = 0;
1291 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1292 userdata_len = nt->userdata_length;
1296 release_len = strlen(init_utsname()->release) + 1;
1298 if (msg_len + release_len + userdata_len <= MAX_PRINT_CHUNK)
1299 return send_msg_no_fragmentation(nt, msg, msg_len, release_len);
1301 return send_msg_fragmented(nt, msg, msg_len, release_len);
1304 static void write_ext_msg(struct console *con, const char *msg,
1307 struct netconsole_target *nt;
1308 unsigned long flags;
1310 if ((oops_only && !oops_in_progress) || list_empty(&target_list))
1313 spin_lock_irqsave(&target_list_lock, flags);
1314 list_for_each_entry(nt, &target_list, list)
1315 if (nt->extended && nt->enabled && netif_running(nt->np.dev))
1316 send_ext_msg_udp(nt, msg, len);
1317 spin_unlock_irqrestore(&target_list_lock, flags);
1320 static void write_msg(struct console *con, const char *msg, unsigned int len)
1323 unsigned long flags;
1324 struct netconsole_target *nt;
1327 if (oops_only && !oops_in_progress)
1329 /* Avoid taking lock and disabling interrupts unnecessarily */
1330 if (list_empty(&target_list))
1333 spin_lock_irqsave(&target_list_lock, flags);
1334 list_for_each_entry(nt, &target_list, list) {
1335 if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
1337 * We nest this inside the for-each-target loop above
1338 * so that we're able to get as much logging out to
1339 * at least one target if we die inside here, instead
1340 * of unnecessarily keeping all targets in lock-step.
1343 for (left = len; left;) {
1344 frag = min(left, MAX_PRINT_CHUNK);
1345 send_udp(nt, tmp, frag);
1351 spin_unlock_irqrestore(&target_list_lock, flags);
1354 /* Allocate new target (from boot/module param) and setup netpoll for it */
1355 static struct netconsole_target *alloc_param_target(char *target_config,
1358 struct netconsole_target *nt;
1361 nt = alloc_and_init();
1367 if (*target_config == '+') {
1368 nt->extended = true;
1372 if (*target_config == 'r') {
1373 if (!nt->extended) {
1374 pr_err("Netconsole configuration error. Release feature requires extended log message");
1382 /* Parse parameters and setup netpoll */
1383 err = netpoll_parse_options(&nt->np, target_config);
1387 err = netpoll_setup(&nt->np);
1389 pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
1390 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1391 if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1392 /* only fail if dynamic reconfiguration is set,
1393 * otherwise, keep the target in the list, but disabled.
1399 populate_configfs_item(nt, cmdline_count);
1405 return ERR_PTR(err);
1408 /* Cleanup netpoll for given target (from boot/module param) and free it */
1409 static void free_param_target(struct netconsole_target *nt)
1411 netpoll_cleanup(&nt->np);
1415 static struct console netconsole_ext = {
1416 .name = "netcon_ext",
1417 .flags = CON_ENABLED | CON_EXTENDED,
1418 .write = write_ext_msg,
1421 static struct console netconsole = {
1423 .flags = CON_ENABLED,
1427 static int __init init_netconsole(void)
1430 struct netconsole_target *nt, *tmp;
1431 unsigned int count = 0;
1432 bool extended = false;
1433 unsigned long flags;
1434 char *target_config;
1435 char *input = config;
1437 if (strnlen(input, MAX_PARAM_LENGTH)) {
1438 while ((target_config = strsep(&input, ";"))) {
1439 nt = alloc_param_target(target_config, count);
1441 if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1446 /* Dump existing printks when we register */
1449 netconsole_ext.flags |= CON_PRINTBUFFER;
1451 netconsole.flags |= CON_PRINTBUFFER;
1454 spin_lock_irqsave(&target_list_lock, flags);
1455 list_add(&nt->list, &target_list);
1456 spin_unlock_irqrestore(&target_list_lock, flags);
1461 err = register_netdevice_notifier(&netconsole_netdev_notifier);
1465 err = dynamic_netconsole_init();
1470 register_console(&netconsole_ext);
1471 register_console(&netconsole);
1472 pr_info("network logging started\n");
1477 unregister_netdevice_notifier(&netconsole_netdev_notifier);
1480 pr_err("cleaning up\n");
1483 * Remove all targets and destroy them (only targets created
1484 * from the boot/module option exist here). Skipping the list
1485 * lock is safe here, and netpoll_cleanup() will sleep.
1487 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1488 list_del(&nt->list);
1489 free_param_target(nt);
1495 static void __exit cleanup_netconsole(void)
1497 struct netconsole_target *nt, *tmp;
1499 if (console_is_registered(&netconsole_ext))
1500 unregister_console(&netconsole_ext);
1501 unregister_console(&netconsole);
1502 dynamic_netconsole_exit();
1503 unregister_netdevice_notifier(&netconsole_netdev_notifier);
1506 * Targets created via configfs pin references on our module
1507 * and would first be rmdir(2)'ed from userspace. We reach
1508 * here only when they are already destroyed, and only those
1509 * created from the boot/module option are left, so remove and
1510 * destroy them. Skipping the list lock is safe here, and
1511 * netpoll_cleanup() will sleep.
1513 list_for_each_entry_safe(nt, tmp, &target_list, list) {
1514 list_del(&nt->list);
1515 free_param_target(nt);
1520 * Use late_initcall to ensure netconsole is
1521 * initialized after network device driver if built-in.
1523 * late_initcall() and module_init() are identical if built as module.
1525 late_initcall(init_netconsole);
1526 module_exit(cleanup_netconsole);