2 * linux/drivers/net/netconsole.c
4 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
6 * This file contains the implementation of an IRQ-safe, crash-safe
7 * kernel console implementation that outputs kernel messages to the
10 * Modification history:
12 * 2001-09-17 started by Ingo Molnar.
13 * 2003-08-11 2.6 port by Matt Mackall
17 * 2003-09-07 rewritten with netpoll api
20 /****************************************************************
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2, or (at your option)
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 ****************************************************************/
38 #include <linux/init.h>
39 #include <linux/module.h>
40 #include <linux/console.h>
41 #include <linux/moduleparam.h>
42 #include <linux/string.h>
43 #include <linux/netpoll.h>
44 #include <linux/inet.h>
45 #include <linux/configfs.h>
47 MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
48 MODULE_DESCRIPTION("Console driver for network interfaces");
49 MODULE_LICENSE("GPL");
51 #define MAX_PARAM_LENGTH 256
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]\n");
59 static int __init
option_setup(char *opt
)
61 strlcpy(config
, opt
, MAX_PARAM_LENGTH
);
64 __setup("netconsole=", option_setup
);
67 /* Linked list of all configured targets */
68 static LIST_HEAD(target_list
);
70 /* This needs to be a spinlock because write_msg() cannot sleep */
71 static DEFINE_SPINLOCK(target_list_lock
);
74 * struct netconsole_target - Represents a configured netconsole target.
75 * @list: Links this target into the target_list.
76 * @item: Links us into the configfs subsystem hierarchy.
77 * @enabled: On / off knob to enable / disable target.
78 * Visible from userspace (read-write).
79 * We maintain a strict 1:1 correspondence between this and
80 * whether the corresponding netpoll is active or inactive.
81 * Also, other parameters of a target may be modified at
82 * runtime only when it is disabled (enabled == 0).
83 * @np: The netpoll structure for this target.
84 * Contains the other userspace visible parameters:
85 * dev_name (read-write)
86 * local_port (read-write)
87 * remote_port (read-write)
88 * local_ip (read-write)
89 * remote_ip (read-write)
90 * local_mac (read-only)
91 * remote_mac (read-write)
93 struct netconsole_target
{
94 struct list_head list
;
95 #ifdef CONFIG_NETCONSOLE_DYNAMIC
96 struct config_item item
;
102 #ifdef CONFIG_NETCONSOLE_DYNAMIC
104 static struct configfs_subsystem netconsole_subsys
;
106 static int __init
dynamic_netconsole_init(void)
108 config_group_init(&netconsole_subsys
.su_group
);
109 mutex_init(&netconsole_subsys
.su_mutex
);
110 return configfs_register_subsystem(&netconsole_subsys
);
113 static void __exit
dynamic_netconsole_exit(void)
115 configfs_unregister_subsystem(&netconsole_subsys
);
119 * Targets that were created by parsing the boot/module option string
120 * do not exist in the configfs hierarchy (and have NULL names) and will
121 * never go away, so make these a no-op for them.
123 static void netconsole_target_get(struct netconsole_target
*nt
)
125 if (config_item_name(&nt
->item
))
126 config_item_get(&nt
->item
);
129 static void netconsole_target_put(struct netconsole_target
*nt
)
131 if (config_item_name(&nt
->item
))
132 config_item_put(&nt
->item
);
135 #else /* !CONFIG_NETCONSOLE_DYNAMIC */
137 static int __init
dynamic_netconsole_init(void)
142 static void __exit
dynamic_netconsole_exit(void)
147 * No danger of targets going away from under us when dynamic
148 * reconfigurability is off.
150 static void netconsole_target_get(struct netconsole_target
*nt
)
154 static void netconsole_target_put(struct netconsole_target
*nt
)
158 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
160 /* Allocate new target (from boot/module param) and setup netpoll for it */
161 static struct netconsole_target
*alloc_param_target(char *target_config
)
164 struct netconsole_target
*nt
;
167 * Allocate and initialize with defaults.
168 * Note that these targets get their config_item fields zeroed-out.
170 nt
= kzalloc(sizeof(*nt
), GFP_KERNEL
);
172 printk(KERN_ERR
"netconsole: failed to allocate memory\n");
176 nt
->np
.name
= "netconsole";
177 strlcpy(nt
->np
.dev_name
, "eth0", IFNAMSIZ
);
178 nt
->np
.local_port
= 6665;
179 nt
->np
.remote_port
= 6666;
180 memset(nt
->np
.remote_mac
, 0xff, ETH_ALEN
);
182 /* Parse parameters and setup netpoll */
183 err
= netpoll_parse_options(&nt
->np
, target_config
);
187 err
= netpoll_setup(&nt
->np
);
200 /* Cleanup netpoll for given target (from boot/module param) and free it */
201 static void free_param_target(struct netconsole_target
*nt
)
203 netpoll_cleanup(&nt
->np
);
207 #ifdef CONFIG_NETCONSOLE_DYNAMIC
210 * Our subsystem hierarchy is:
212 * /sys/kernel/config/netconsole/
227 struct netconsole_target_attr
{
228 struct configfs_attribute attr
;
229 ssize_t (*show
)(struct netconsole_target
*nt
,
231 ssize_t (*store
)(struct netconsole_target
*nt
,
236 static struct netconsole_target
*to_target(struct config_item
*item
)
239 container_of(item
, struct netconsole_target
, item
) :
244 * Wrapper over simple_strtol (base 10) with sanity and range checking.
245 * We return (signed) long only because we may want to return errors.
246 * Do not use this to convert numbers that are allowed to be negative.
248 static long strtol10_check_range(const char *cp
, long min
, long max
)
251 char *p
= (char *) cp
;
256 ret
= simple_strtol(p
, &p
, 10);
258 if (*p
&& (*p
!= '\n')) {
259 printk(KERN_ERR
"netconsole: invalid input\n");
262 if ((ret
< min
) || (ret
> max
)) {
263 printk(KERN_ERR
"netconsole: input %ld must be between "
264 "%ld and %ld\n", ret
, min
, max
);
272 * Attribute operations for netconsole_target.
275 static ssize_t
show_enabled(struct netconsole_target
*nt
, char *buf
)
277 return snprintf(buf
, PAGE_SIZE
, "%d\n", nt
->enabled
);
280 static ssize_t
show_dev_name(struct netconsole_target
*nt
, char *buf
)
282 return snprintf(buf
, PAGE_SIZE
, "%s\n", nt
->np
.dev_name
);
285 static ssize_t
show_local_port(struct netconsole_target
*nt
, char *buf
)
287 return snprintf(buf
, PAGE_SIZE
, "%d\n", nt
->np
.local_port
);
290 static ssize_t
show_remote_port(struct netconsole_target
*nt
, char *buf
)
292 return snprintf(buf
, PAGE_SIZE
, "%d\n", nt
->np
.remote_port
);
295 static ssize_t
show_local_ip(struct netconsole_target
*nt
, char *buf
)
297 return snprintf(buf
, PAGE_SIZE
, "%d.%d.%d.%d\n",
298 HIPQUAD(nt
->np
.local_ip
));
301 static ssize_t
show_remote_ip(struct netconsole_target
*nt
, char *buf
)
303 return snprintf(buf
, PAGE_SIZE
, "%d.%d.%d.%d\n",
304 HIPQUAD(nt
->np
.remote_ip
));
307 static ssize_t
show_local_mac(struct netconsole_target
*nt
, char *buf
)
309 struct net_device
*dev
= nt
->np
.dev
;
311 DECLARE_MAC_BUF(mac
);
312 return snprintf(buf
, PAGE_SIZE
, "%s\n",
313 print_mac(mac
, dev
->dev_addr
));
316 static ssize_t
show_remote_mac(struct netconsole_target
*nt
, char *buf
)
318 DECLARE_MAC_BUF(mac
);
319 return snprintf(buf
, PAGE_SIZE
, "%s\n",
320 print_mac(mac
, nt
->np
.remote_mac
));
324 * This one is special -- targets created through the configfs interface
325 * are not enabled (and the corresponding netpoll activated) by default.
326 * The user is expected to set the desired parameters first (which
327 * would enable him to dynamically add new netpoll targets for new
328 * network interfaces as and when they come up).
330 static ssize_t
store_enabled(struct netconsole_target
*nt
,
337 enabled
= strtol10_check_range(buf
, 0, 1);
341 if (enabled
) { /* 1 */
344 * Skip netpoll_parse_options() -- all the attributes are
345 * already configured via configfs. Just print them out.
347 netpoll_print_options(&nt
->np
);
349 err
= netpoll_setup(&nt
->np
);
353 printk(KERN_INFO
"netconsole: network logging started\n");
356 netpoll_cleanup(&nt
->np
);
359 nt
->enabled
= enabled
;
361 return strnlen(buf
, count
);
364 static ssize_t
store_dev_name(struct netconsole_target
*nt
,
371 printk(KERN_ERR
"netconsole: target (%s) is enabled, "
372 "disable to update parameters\n",
373 config_item_name(&nt
->item
));
377 strlcpy(nt
->np
.dev_name
, buf
, IFNAMSIZ
);
379 /* Get rid of possible trailing newline from echo(1) */
380 len
= strnlen(nt
->np
.dev_name
, IFNAMSIZ
);
381 if (nt
->np
.dev_name
[len
- 1] == '\n')
382 nt
->np
.dev_name
[len
- 1] = '\0';
384 return strnlen(buf
, count
);
387 static ssize_t
store_local_port(struct netconsole_target
*nt
,
392 #define __U16_MAX ((__u16) ~0U)
395 printk(KERN_ERR
"netconsole: target (%s) is enabled, "
396 "disable to update parameters\n",
397 config_item_name(&nt
->item
));
401 local_port
= strtol10_check_range(buf
, 0, __U16_MAX
);
405 nt
->np
.local_port
= local_port
;
407 return strnlen(buf
, count
);
410 static ssize_t
store_remote_port(struct netconsole_target
*nt
,
415 #define __U16_MAX ((__u16) ~0U)
418 printk(KERN_ERR
"netconsole: target (%s) is enabled, "
419 "disable to update parameters\n",
420 config_item_name(&nt
->item
));
424 remote_port
= strtol10_check_range(buf
, 0, __U16_MAX
);
428 nt
->np
.remote_port
= remote_port
;
430 return strnlen(buf
, count
);
433 static ssize_t
store_local_ip(struct netconsole_target
*nt
,
438 printk(KERN_ERR
"netconsole: target (%s) is enabled, "
439 "disable to update parameters\n",
440 config_item_name(&nt
->item
));
444 nt
->np
.local_ip
= ntohl(in_aton(buf
));
446 return strnlen(buf
, count
);
449 static ssize_t
store_remote_ip(struct netconsole_target
*nt
,
454 printk(KERN_ERR
"netconsole: target (%s) is enabled, "
455 "disable to update parameters\n",
456 config_item_name(&nt
->item
));
460 nt
->np
.remote_ip
= ntohl(in_aton(buf
));
462 return strnlen(buf
, count
);
465 static ssize_t
store_remote_mac(struct netconsole_target
*nt
,
469 u8 remote_mac
[ETH_ALEN
];
470 char *p
= (char *) buf
;
474 printk(KERN_ERR
"netconsole: target (%s) is enabled, "
475 "disable to update parameters\n",
476 config_item_name(&nt
->item
));
480 for (i
= 0; i
< ETH_ALEN
- 1; i
++) {
481 remote_mac
[i
] = simple_strtoul(p
, &p
, 16);
486 remote_mac
[ETH_ALEN
- 1] = simple_strtoul(p
, &p
, 16);
487 if (*p
&& (*p
!= '\n'))
490 memcpy(nt
->np
.remote_mac
, remote_mac
, ETH_ALEN
);
492 return strnlen(buf
, count
);
495 printk(KERN_ERR
"netconsole: invalid input\n");
500 * Attribute definitions for netconsole_target.
503 #define NETCONSOLE_TARGET_ATTR_RO(_name) \
504 static struct netconsole_target_attr netconsole_target_##_name = \
505 __CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL)
507 #define NETCONSOLE_TARGET_ATTR_RW(_name) \
508 static struct netconsole_target_attr netconsole_target_##_name = \
509 __CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
511 NETCONSOLE_TARGET_ATTR_RW(enabled
);
512 NETCONSOLE_TARGET_ATTR_RW(dev_name
);
513 NETCONSOLE_TARGET_ATTR_RW(local_port
);
514 NETCONSOLE_TARGET_ATTR_RW(remote_port
);
515 NETCONSOLE_TARGET_ATTR_RW(local_ip
);
516 NETCONSOLE_TARGET_ATTR_RW(remote_ip
);
517 NETCONSOLE_TARGET_ATTR_RO(local_mac
);
518 NETCONSOLE_TARGET_ATTR_RW(remote_mac
);
520 static struct configfs_attribute
*netconsole_target_attrs
[] = {
521 &netconsole_target_enabled
.attr
,
522 &netconsole_target_dev_name
.attr
,
523 &netconsole_target_local_port
.attr
,
524 &netconsole_target_remote_port
.attr
,
525 &netconsole_target_local_ip
.attr
,
526 &netconsole_target_remote_ip
.attr
,
527 &netconsole_target_local_mac
.attr
,
528 &netconsole_target_remote_mac
.attr
,
533 * Item operations and type for netconsole_target.
536 static void netconsole_target_release(struct config_item
*item
)
538 kfree(to_target(item
));
541 static ssize_t
netconsole_target_attr_show(struct config_item
*item
,
542 struct configfs_attribute
*attr
,
545 ssize_t ret
= -EINVAL
;
546 struct netconsole_target
*nt
= to_target(item
);
547 struct netconsole_target_attr
*na
=
548 container_of(attr
, struct netconsole_target_attr
, attr
);
551 ret
= na
->show(nt
, buf
);
556 static ssize_t
netconsole_target_attr_store(struct config_item
*item
,
557 struct configfs_attribute
*attr
,
561 ssize_t ret
= -EINVAL
;
562 struct netconsole_target
*nt
= to_target(item
);
563 struct netconsole_target_attr
*na
=
564 container_of(attr
, struct netconsole_target_attr
, attr
);
567 ret
= na
->store(nt
, buf
, count
);
572 static struct configfs_item_operations netconsole_target_item_ops
= {
573 .release
= netconsole_target_release
,
574 .show_attribute
= netconsole_target_attr_show
,
575 .store_attribute
= netconsole_target_attr_store
,
578 static struct config_item_type netconsole_target_type
= {
579 .ct_attrs
= netconsole_target_attrs
,
580 .ct_item_ops
= &netconsole_target_item_ops
,
581 .ct_owner
= THIS_MODULE
,
585 * Group operations and type for netconsole_subsys.
588 static struct config_item
*make_netconsole_target(struct config_group
*group
,
592 struct netconsole_target
*nt
;
595 * Allocate and initialize with defaults.
596 * Target is disabled at creation (enabled == 0).
598 nt
= kzalloc(sizeof(*nt
), GFP_KERNEL
);
600 printk(KERN_ERR
"netconsole: failed to allocate memory\n");
604 nt
->np
.name
= "netconsole";
605 strlcpy(nt
->np
.dev_name
, "eth0", IFNAMSIZ
);
606 nt
->np
.local_port
= 6665;
607 nt
->np
.remote_port
= 6666;
608 memset(nt
->np
.remote_mac
, 0xff, ETH_ALEN
);
610 /* Initialize the config_item member */
611 config_item_init_type_name(&nt
->item
, name
, &netconsole_target_type
);
613 /* Adding, but it is disabled */
614 spin_lock_irqsave(&target_list_lock
, flags
);
615 list_add(&nt
->list
, &target_list
);
616 spin_unlock_irqrestore(&target_list_lock
, flags
);
621 static void drop_netconsole_target(struct config_group
*group
,
622 struct config_item
*item
)
625 struct netconsole_target
*nt
= to_target(item
);
627 spin_lock_irqsave(&target_list_lock
, flags
);
629 spin_unlock_irqrestore(&target_list_lock
, flags
);
632 * The target may have never been enabled, or was manually disabled
633 * before being removed so netpoll may have already been cleaned up.
636 netpoll_cleanup(&nt
->np
);
638 config_item_put(&nt
->item
);
641 static struct configfs_group_operations netconsole_subsys_group_ops
= {
642 .make_item
= make_netconsole_target
,
643 .drop_item
= drop_netconsole_target
,
646 static struct config_item_type netconsole_subsys_type
= {
647 .ct_group_ops
= &netconsole_subsys_group_ops
,
648 .ct_owner
= THIS_MODULE
,
651 /* The netconsole configfs subsystem */
652 static struct configfs_subsystem netconsole_subsys
= {
655 .ci_namebuf
= "netconsole",
656 .ci_type
= &netconsole_subsys_type
,
661 #endif /* CONFIG_NETCONSOLE_DYNAMIC */
663 /* Handle network interface device notifications */
664 static int netconsole_netdev_event(struct notifier_block
*this,
669 struct netconsole_target
*nt
;
670 struct net_device
*dev
= ptr
;
672 if (!(event
== NETDEV_CHANGENAME
))
675 spin_lock_irqsave(&target_list_lock
, flags
);
676 list_for_each_entry(nt
, &target_list
, list
) {
677 netconsole_target_get(nt
);
678 if (nt
->np
.dev
== dev
) {
680 case NETDEV_CHANGENAME
:
681 strlcpy(nt
->np
.dev_name
, dev
->name
, IFNAMSIZ
);
685 netconsole_target_put(nt
);
687 spin_unlock_irqrestore(&target_list_lock
, flags
);
693 static struct notifier_block netconsole_netdev_notifier
= {
694 .notifier_call
= netconsole_netdev_event
,
697 static void write_msg(struct console
*con
, const char *msg
, unsigned int len
)
701 struct netconsole_target
*nt
;
704 /* Avoid taking lock and disabling interrupts unnecessarily */
705 if (list_empty(&target_list
))
708 spin_lock_irqsave(&target_list_lock
, flags
);
709 list_for_each_entry(nt
, &target_list
, list
) {
710 netconsole_target_get(nt
);
711 if (nt
->enabled
&& netif_running(nt
->np
.dev
)) {
713 * We nest this inside the for-each-target loop above
714 * so that we're able to get as much logging out to
715 * at least one target if we die inside here, instead
716 * of unnecessarily keeping all targets in lock-step.
719 for (left
= len
; left
;) {
720 frag
= min(left
, MAX_PRINT_CHUNK
);
721 netpoll_send_udp(&nt
->np
, tmp
, frag
);
726 netconsole_target_put(nt
);
728 spin_unlock_irqrestore(&target_list_lock
, flags
);
731 static struct console netconsole
= {
733 .flags
= CON_ENABLED
| CON_PRINTBUFFER
,
737 static int __init
init_netconsole(void)
740 struct netconsole_target
*nt
, *tmp
;
743 char *input
= config
;
745 if (strnlen(input
, MAX_PARAM_LENGTH
)) {
746 while ((target_config
= strsep(&input
, ";"))) {
747 nt
= alloc_param_target(target_config
);
752 spin_lock_irqsave(&target_list_lock
, flags
);
753 list_add(&nt
->list
, &target_list
);
754 spin_unlock_irqrestore(&target_list_lock
, flags
);
758 err
= register_netdevice_notifier(&netconsole_netdev_notifier
);
762 err
= dynamic_netconsole_init();
766 register_console(&netconsole
);
767 printk(KERN_INFO
"netconsole: network logging started\n");
772 unregister_netdevice_notifier(&netconsole_netdev_notifier
);
775 printk(KERN_ERR
"netconsole: cleaning up\n");
778 * Remove all targets and destroy them (only targets created
779 * from the boot/module option exist here). Skipping the list
780 * lock is safe here, and netpoll_cleanup() will sleep.
782 list_for_each_entry_safe(nt
, tmp
, &target_list
, list
) {
784 free_param_target(nt
);
790 static void __exit
cleanup_netconsole(void)
792 struct netconsole_target
*nt
, *tmp
;
794 unregister_console(&netconsole
);
795 dynamic_netconsole_exit();
796 unregister_netdevice_notifier(&netconsole_netdev_notifier
);
799 * Targets created via configfs pin references on our module
800 * and would first be rmdir(2)'ed from userspace. We reach
801 * here only when they are already destroyed, and only those
802 * created from the boot/module option are left, so remove and
803 * destroy them. Skipping the list lock is safe here, and
804 * netpoll_cleanup() will sleep.
806 list_for_each_entry_safe(nt
, tmp
, &target_list
, list
) {
808 free_param_target(nt
);
812 module_init(init_netconsole
);
813 module_exit(cleanup_netconsole
);