blob: 9681618c32321912c522e9953cd71eac4db06443 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/net/netconsole.c
3 *
4 * Copyright (C) 2001 Ingo Molnar <mingo@redhat.com>
5 *
6 * This file contains the implementation of an IRQ-safe, crash-safe
7 * kernel console implementation that outputs kernel messages to the
8 * network.
9 *
10 * Modification history:
11 *
12 * 2001-09-17 started by Ingo Molnar.
13 * 2003-08-11 2.6 port by Matt Mackall
14 * simplified options
15 * generic card hooks
16 * works non-modular
17 * 2003-09-07 rewritten with netpoll api
18 */
19
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)
24 * any later version.
25 *
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.
30 *
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.
34 *
35 ****************************************************************/
36
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/module.h>
40#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/moduleparam.h>
42#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/netpoll.h>
Satyam Sharma0bcc1812007-08-10 15:35:05 -070044#include <linux/inet.h>
45#include <linux/configfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
48MODULE_DESCRIPTION("Console driver for network interfaces");
49MODULE_LICENSE("GPL");
50
Satyam Sharmad39badf2007-08-10 15:27:24 -070051#define MAX_PARAM_LENGTH 256
52#define MAX_PRINT_CHUNK 1000
53
54static char config[MAX_PARAM_LENGTH];
55module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
Niels de Vos61a2d072008-07-31 00:07:23 -070056MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Satyam Sharmad2b60882007-08-10 15:29:47 -070058#ifndef MODULE
59static int __init option_setup(char *opt)
60{
61 strlcpy(config, opt, MAX_PARAM_LENGTH);
62 return 1;
63}
64__setup("netconsole=", option_setup);
65#endif /* MODULE */
66
Satyam Sharmab5427c22007-08-10 15:33:40 -070067/* Linked list of all configured targets */
68static LIST_HEAD(target_list);
69
70/* This needs to be a spinlock because write_msg() cannot sleep */
71static DEFINE_SPINLOCK(target_list_lock);
72
Satyam Sharmadf180e32007-08-10 15:32:14 -070073/**
74 * struct netconsole_target - Represents a configured netconsole target.
Satyam Sharmab5427c22007-08-10 15:33:40 -070075 * @list: Links this target into the target_list.
Satyam Sharma0bcc1812007-08-10 15:35:05 -070076 * @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).
Satyam Sharmadf180e32007-08-10 15:32:14 -070083 * @np: The netpoll structure for this target.
Satyam Sharma0bcc1812007-08-10 15:35:05 -070084 * 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)
Satyam Sharmadf180e32007-08-10 15:32:14 -070092 */
93struct netconsole_target {
Satyam Sharmab5427c22007-08-10 15:33:40 -070094 struct list_head list;
Satyam Sharma0bcc1812007-08-10 15:35:05 -070095#ifdef CONFIG_NETCONSOLE_DYNAMIC
96 struct config_item item;
97#endif
98 int enabled;
Satyam Sharmadf180e32007-08-10 15:32:14 -070099 struct netpoll np;
100};
101
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700102#ifdef CONFIG_NETCONSOLE_DYNAMIC
103
104static struct configfs_subsystem netconsole_subsys;
105
106static int __init dynamic_netconsole_init(void)
107{
108 config_group_init(&netconsole_subsys.su_group);
109 mutex_init(&netconsole_subsys.su_mutex);
110 return configfs_register_subsystem(&netconsole_subsys);
111}
112
113static void __exit dynamic_netconsole_exit(void)
114{
115 configfs_unregister_subsystem(&netconsole_subsys);
116}
117
118/*
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.
122 */
123static void netconsole_target_get(struct netconsole_target *nt)
124{
125 if (config_item_name(&nt->item))
126 config_item_get(&nt->item);
127}
128
129static void netconsole_target_put(struct netconsole_target *nt)
130{
131 if (config_item_name(&nt->item))
132 config_item_put(&nt->item);
133}
134
135#else /* !CONFIG_NETCONSOLE_DYNAMIC */
136
137static int __init dynamic_netconsole_init(void)
138{
139 return 0;
140}
141
142static void __exit dynamic_netconsole_exit(void)
143{
144}
145
146/*
147 * No danger of targets going away from under us when dynamic
148 * reconfigurability is off.
149 */
150static void netconsole_target_get(struct netconsole_target *nt)
151{
152}
153
154static void netconsole_target_put(struct netconsole_target *nt)
155{
156}
157
158#endif /* CONFIG_NETCONSOLE_DYNAMIC */
159
160/* Allocate new target (from boot/module param) and setup netpoll for it */
161static struct netconsole_target *alloc_param_target(char *target_config)
Satyam Sharmab5427c22007-08-10 15:33:40 -0700162{
163 int err = -ENOMEM;
164 struct netconsole_target *nt;
165
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700166 /*
167 * Allocate and initialize with defaults.
168 * Note that these targets get their config_item fields zeroed-out.
169 */
Satyam Sharmab5427c22007-08-10 15:33:40 -0700170 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
171 if (!nt) {
172 printk(KERN_ERR "netconsole: failed to allocate memory\n");
173 goto fail;
174 }
175
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);
181
182 /* Parse parameters and setup netpoll */
183 err = netpoll_parse_options(&nt->np, target_config);
184 if (err)
185 goto fail;
186
187 err = netpoll_setup(&nt->np);
188 if (err)
189 goto fail;
190
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700191 nt->enabled = 1;
192
Satyam Sharmab5427c22007-08-10 15:33:40 -0700193 return nt;
194
195fail:
196 kfree(nt);
197 return ERR_PTR(err);
198}
199
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700200/* Cleanup netpoll for given target (from boot/module param) and free it */
201static void free_param_target(struct netconsole_target *nt)
Satyam Sharmab5427c22007-08-10 15:33:40 -0700202{
203 netpoll_cleanup(&nt->np);
204 kfree(nt);
205}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700207#ifdef CONFIG_NETCONSOLE_DYNAMIC
208
209/*
210 * Our subsystem hierarchy is:
211 *
212 * /sys/kernel/config/netconsole/
213 * |
214 * <target>/
215 * | enabled
216 * | dev_name
217 * | local_port
218 * | remote_port
219 * | local_ip
220 * | remote_ip
221 * | local_mac
222 * | remote_mac
223 * |
224 * <target>/...
225 */
226
227struct netconsole_target_attr {
228 struct configfs_attribute attr;
229 ssize_t (*show)(struct netconsole_target *nt,
230 char *buf);
231 ssize_t (*store)(struct netconsole_target *nt,
232 const char *buf,
233 size_t count);
234};
235
236static struct netconsole_target *to_target(struct config_item *item)
237{
238 return item ?
239 container_of(item, struct netconsole_target, item) :
240 NULL;
241}
242
243/*
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.
247 */
248static long strtol10_check_range(const char *cp, long min, long max)
249{
250 long ret;
251 char *p = (char *) cp;
252
253 WARN_ON(min < 0);
254 WARN_ON(max < min);
255
256 ret = simple_strtol(p, &p, 10);
257
258 if (*p && (*p != '\n')) {
259 printk(KERN_ERR "netconsole: invalid input\n");
260 return -EINVAL;
261 }
262 if ((ret < min) || (ret > max)) {
263 printk(KERN_ERR "netconsole: input %ld must be between "
264 "%ld and %ld\n", ret, min, max);
265 return -EINVAL;
266 }
267
268 return ret;
269}
270
271/*
272 * Attribute operations for netconsole_target.
273 */
274
275static ssize_t show_enabled(struct netconsole_target *nt, char *buf)
276{
277 return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled);
278}
279
280static ssize_t show_dev_name(struct netconsole_target *nt, char *buf)
281{
282 return snprintf(buf, PAGE_SIZE, "%s\n", nt->np.dev_name);
283}
284
285static ssize_t show_local_port(struct netconsole_target *nt, char *buf)
286{
287 return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.local_port);
288}
289
290static ssize_t show_remote_port(struct netconsole_target *nt, char *buf)
291{
292 return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.remote_port);
293}
294
295static ssize_t show_local_ip(struct netconsole_target *nt, char *buf)
296{
297 return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
298 HIPQUAD(nt->np.local_ip));
299}
300
301static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf)
302{
303 return snprintf(buf, PAGE_SIZE, "%d.%d.%d.%d\n",
304 HIPQUAD(nt->np.remote_ip));
305}
306
307static ssize_t show_local_mac(struct netconsole_target *nt, char *buf)
308{
Stephen Hemminger09538642007-11-19 19:23:29 -0800309 struct net_device *dev = nt->np.dev;
310
Joe Perches0795af52007-10-03 17:59:30 -0700311 DECLARE_MAC_BUF(mac);
Keiichi KII69c36832008-02-15 02:01:58 -0800312 return snprintf(buf, PAGE_SIZE, "%s\n", dev ?
313 print_mac(mac, dev->dev_addr) : "ff:ff:ff:ff:ff:ff");
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700314}
315
316static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf)
317{
Joe Perches0795af52007-10-03 17:59:30 -0700318 DECLARE_MAC_BUF(mac);
319 return snprintf(buf, PAGE_SIZE, "%s\n",
320 print_mac(mac, nt->np.remote_mac));
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700321}
322
323/*
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).
329 */
330static ssize_t store_enabled(struct netconsole_target *nt,
331 const char *buf,
332 size_t count)
333{
334 int err;
335 long enabled;
336
337 enabled = strtol10_check_range(buf, 0, 1);
338 if (enabled < 0)
339 return enabled;
340
341 if (enabled) { /* 1 */
342
343 /*
344 * Skip netpoll_parse_options() -- all the attributes are
345 * already configured via configfs. Just print them out.
346 */
347 netpoll_print_options(&nt->np);
348
349 err = netpoll_setup(&nt->np);
350 if (err)
351 return err;
352
353 printk(KERN_INFO "netconsole: network logging started\n");
354
355 } else { /* 0 */
356 netpoll_cleanup(&nt->np);
357 }
358
359 nt->enabled = enabled;
360
361 return strnlen(buf, count);
362}
363
364static ssize_t store_dev_name(struct netconsole_target *nt,
365 const char *buf,
366 size_t count)
367{
368 size_t len;
369
370 if (nt->enabled) {
371 printk(KERN_ERR "netconsole: target (%s) is enabled, "
372 "disable to update parameters\n",
373 config_item_name(&nt->item));
374 return -EINVAL;
375 }
376
377 strlcpy(nt->np.dev_name, buf, IFNAMSIZ);
378
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';
383
384 return strnlen(buf, count);
385}
386
387static ssize_t store_local_port(struct netconsole_target *nt,
388 const char *buf,
389 size_t count)
390{
391 long local_port;
392#define __U16_MAX ((__u16) ~0U)
393
394 if (nt->enabled) {
395 printk(KERN_ERR "netconsole: target (%s) is enabled, "
396 "disable to update parameters\n",
397 config_item_name(&nt->item));
398 return -EINVAL;
399 }
400
401 local_port = strtol10_check_range(buf, 0, __U16_MAX);
402 if (local_port < 0)
403 return local_port;
404
405 nt->np.local_port = local_port;
406
407 return strnlen(buf, count);
408}
409
410static ssize_t store_remote_port(struct netconsole_target *nt,
411 const char *buf,
412 size_t count)
413{
414 long remote_port;
415#define __U16_MAX ((__u16) ~0U)
416
417 if (nt->enabled) {
418 printk(KERN_ERR "netconsole: target (%s) is enabled, "
419 "disable to update parameters\n",
420 config_item_name(&nt->item));
421 return -EINVAL;
422 }
423
424 remote_port = strtol10_check_range(buf, 0, __U16_MAX);
425 if (remote_port < 0)
426 return remote_port;
427
428 nt->np.remote_port = remote_port;
429
430 return strnlen(buf, count);
431}
432
433static ssize_t store_local_ip(struct netconsole_target *nt,
434 const char *buf,
435 size_t count)
436{
437 if (nt->enabled) {
438 printk(KERN_ERR "netconsole: target (%s) is enabled, "
439 "disable to update parameters\n",
440 config_item_name(&nt->item));
441 return -EINVAL;
442 }
443
444 nt->np.local_ip = ntohl(in_aton(buf));
445
446 return strnlen(buf, count);
447}
448
449static ssize_t store_remote_ip(struct netconsole_target *nt,
450 const char *buf,
451 size_t count)
452{
453 if (nt->enabled) {
454 printk(KERN_ERR "netconsole: target (%s) is enabled, "
455 "disable to update parameters\n",
456 config_item_name(&nt->item));
457 return -EINVAL;
458 }
459
460 nt->np.remote_ip = ntohl(in_aton(buf));
461
462 return strnlen(buf, count);
463}
464
465static ssize_t store_remote_mac(struct netconsole_target *nt,
466 const char *buf,
467 size_t count)
468{
469 u8 remote_mac[ETH_ALEN];
470 char *p = (char *) buf;
471 int i;
472
473 if (nt->enabled) {
474 printk(KERN_ERR "netconsole: target (%s) is enabled, "
475 "disable to update parameters\n",
476 config_item_name(&nt->item));
477 return -EINVAL;
478 }
479
480 for (i = 0; i < ETH_ALEN - 1; i++) {
481 remote_mac[i] = simple_strtoul(p, &p, 16);
482 if (*p != ':')
483 goto invalid;
484 p++;
485 }
486 remote_mac[ETH_ALEN - 1] = simple_strtoul(p, &p, 16);
487 if (*p && (*p != '\n'))
488 goto invalid;
489
490 memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
491
492 return strnlen(buf, count);
493
494invalid:
495 printk(KERN_ERR "netconsole: invalid input\n");
496 return -EINVAL;
497}
498
499/*
500 * Attribute definitions for netconsole_target.
501 */
502
503#define NETCONSOLE_TARGET_ATTR_RO(_name) \
504static struct netconsole_target_attr netconsole_target_##_name = \
505 __CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL)
506
507#define NETCONSOLE_TARGET_ATTR_RW(_name) \
508static struct netconsole_target_attr netconsole_target_##_name = \
509 __CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
510
511NETCONSOLE_TARGET_ATTR_RW(enabled);
512NETCONSOLE_TARGET_ATTR_RW(dev_name);
513NETCONSOLE_TARGET_ATTR_RW(local_port);
514NETCONSOLE_TARGET_ATTR_RW(remote_port);
515NETCONSOLE_TARGET_ATTR_RW(local_ip);
516NETCONSOLE_TARGET_ATTR_RW(remote_ip);
517NETCONSOLE_TARGET_ATTR_RO(local_mac);
518NETCONSOLE_TARGET_ATTR_RW(remote_mac);
519
520static 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,
529 NULL,
530};
531
532/*
533 * Item operations and type for netconsole_target.
534 */
535
536static void netconsole_target_release(struct config_item *item)
537{
538 kfree(to_target(item));
539}
540
541static ssize_t netconsole_target_attr_show(struct config_item *item,
542 struct configfs_attribute *attr,
543 char *buf)
544{
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);
549
550 if (na->show)
551 ret = na->show(nt, buf);
552
553 return ret;
554}
555
556static ssize_t netconsole_target_attr_store(struct config_item *item,
557 struct configfs_attribute *attr,
558 const char *buf,
559 size_t count)
560{
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);
565
566 if (na->store)
567 ret = na->store(nt, buf, count);
568
569 return ret;
570}
571
572static 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,
576};
577
578static 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,
582};
583
584/*
585 * Group operations and type for netconsole_subsys.
586 */
587
Joel Beckerf89ab862008-07-17 14:53:48 -0700588static struct config_item *make_netconsole_target(struct config_group *group,
589 const char *name)
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700590{
591 unsigned long flags;
592 struct netconsole_target *nt;
593
594 /*
595 * Allocate and initialize with defaults.
596 * Target is disabled at creation (enabled == 0).
597 */
598 nt = kzalloc(sizeof(*nt), GFP_KERNEL);
599 if (!nt) {
600 printk(KERN_ERR "netconsole: failed to allocate memory\n");
Joel Beckera6795e92008-07-17 15:21:29 -0700601 return ERR_PTR(-ENOMEM);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700602 }
603
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);
609
610 /* Initialize the config_item member */
611 config_item_init_type_name(&nt->item, name, &netconsole_target_type);
612
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);
617
Joel Beckerf89ab862008-07-17 14:53:48 -0700618 return &nt->item;
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700619}
620
621static void drop_netconsole_target(struct config_group *group,
622 struct config_item *item)
623{
624 unsigned long flags;
625 struct netconsole_target *nt = to_target(item);
626
627 spin_lock_irqsave(&target_list_lock, flags);
628 list_del(&nt->list);
629 spin_unlock_irqrestore(&target_list_lock, flags);
630
631 /*
632 * The target may have never been enabled, or was manually disabled
633 * before being removed so netpoll may have already been cleaned up.
634 */
635 if (nt->enabled)
636 netpoll_cleanup(&nt->np);
637
638 config_item_put(&nt->item);
639}
640
641static struct configfs_group_operations netconsole_subsys_group_ops = {
642 .make_item = make_netconsole_target,
643 .drop_item = drop_netconsole_target,
644};
645
646static struct config_item_type netconsole_subsys_type = {
647 .ct_group_ops = &netconsole_subsys_group_ops,
648 .ct_owner = THIS_MODULE,
649};
650
651/* The netconsole configfs subsystem */
652static struct configfs_subsystem netconsole_subsys = {
653 .su_group = {
654 .cg_item = {
655 .ci_namebuf = "netconsole",
656 .ci_type = &netconsole_subsys_type,
657 },
658 },
659};
660
661#endif /* CONFIG_NETCONSOLE_DYNAMIC */
662
Satyam Sharma17951f32007-08-10 15:33:01 -0700663/* Handle network interface device notifications */
664static int netconsole_netdev_event(struct notifier_block *this,
665 unsigned long event,
666 void *ptr)
667{
Satyam Sharmab5427c22007-08-10 15:33:40 -0700668 unsigned long flags;
669 struct netconsole_target *nt;
Satyam Sharma17951f32007-08-10 15:33:01 -0700670 struct net_device *dev = ptr;
Satyam Sharma17951f32007-08-10 15:33:01 -0700671
Stephen Hemminger09538642007-11-19 19:23:29 -0800672 if (!(event == NETDEV_CHANGENAME))
Satyam Sharmab5427c22007-08-10 15:33:40 -0700673 goto done;
Satyam Sharma17951f32007-08-10 15:33:01 -0700674
Satyam Sharmab5427c22007-08-10 15:33:40 -0700675 spin_lock_irqsave(&target_list_lock, flags);
676 list_for_each_entry(nt, &target_list, list) {
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700677 netconsole_target_get(nt);
Satyam Sharmab5427c22007-08-10 15:33:40 -0700678 if (nt->np.dev == dev) {
679 switch (event) {
Satyam Sharmab5427c22007-08-10 15:33:40 -0700680 case NETDEV_CHANGENAME:
681 strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
682 break;
683 }
Satyam Sharma17951f32007-08-10 15:33:01 -0700684 }
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700685 netconsole_target_put(nt);
Satyam Sharma17951f32007-08-10 15:33:01 -0700686 }
Satyam Sharmab5427c22007-08-10 15:33:40 -0700687 spin_unlock_irqrestore(&target_list_lock, flags);
Satyam Sharma17951f32007-08-10 15:33:01 -0700688
Satyam Sharmab5427c22007-08-10 15:33:40 -0700689done:
Satyam Sharma17951f32007-08-10 15:33:01 -0700690 return NOTIFY_DONE;
691}
692
693static struct notifier_block netconsole_netdev_notifier = {
694 .notifier_call = netconsole_netdev_event,
695};
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697static void write_msg(struct console *con, const char *msg, unsigned int len)
698{
699 int frag, left;
700 unsigned long flags;
Satyam Sharmab5427c22007-08-10 15:33:40 -0700701 struct netconsole_target *nt;
702 const char *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Satyam Sharmab5427c22007-08-10 15:33:40 -0700704 /* Avoid taking lock and disabling interrupts unnecessarily */
705 if (list_empty(&target_list))
706 return;
707
708 spin_lock_irqsave(&target_list_lock, flags);
709 list_for_each_entry(nt, &target_list, list) {
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700710 netconsole_target_get(nt);
711 if (nt->enabled && netif_running(nt->np.dev)) {
Satyam Sharmab5427c22007-08-10 15:33:40 -0700712 /*
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.
717 */
718 tmp = msg;
719 for (left = len; left;) {
720 frag = min(left, MAX_PRINT_CHUNK);
721 netpoll_send_udp(&nt->np, tmp, frag);
722 tmp += frag;
723 left -= frag;
724 }
Satyam Sharma0cc120be2007-08-10 15:30:31 -0700725 }
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700726 netconsole_target_put(nt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Satyam Sharmab5427c22007-08-10 15:33:40 -0700728 spin_unlock_irqrestore(&target_list_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static struct console netconsole = {
Satyam Sharmad39badf2007-08-10 15:27:24 -0700732 .name = "netcon",
Michael Ellerman0517dee2008-04-15 00:49:04 -0700733 .flags = CON_ENABLED,
Satyam Sharmad39badf2007-08-10 15:27:24 -0700734 .write = write_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735};
736
Satyam Sharmad39badf2007-08-10 15:27:24 -0700737static int __init init_netconsole(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700739 int err;
Satyam Sharmab5427c22007-08-10 15:33:40 -0700740 struct netconsole_target *nt, *tmp;
741 unsigned long flags;
742 char *target_config;
743 char *input = config;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700744
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700745 if (strnlen(input, MAX_PARAM_LENGTH)) {
746 while ((target_config = strsep(&input, ";"))) {
747 nt = alloc_param_target(target_config);
748 if (IS_ERR(nt)) {
749 err = PTR_ERR(nt);
750 goto fail;
751 }
Michael Ellerman0517dee2008-04-15 00:49:04 -0700752 /* Dump existing printks when we register */
753 netconsole.flags |= CON_PRINTBUFFER;
754
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700755 spin_lock_irqsave(&target_list_lock, flags);
756 list_add(&nt->list, &target_list);
757 spin_unlock_irqrestore(&target_list_lock, flags);
Satyam Sharmab5427c22007-08-10 15:33:40 -0700758 }
Satyam Sharmab5427c22007-08-10 15:33:40 -0700759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Satyam Sharma17951f32007-08-10 15:33:01 -0700761 err = register_netdevice_notifier(&netconsole_netdev_notifier);
762 if (err)
Satyam Sharmab5427c22007-08-10 15:33:40 -0700763 goto fail;
Satyam Sharma17951f32007-08-10 15:33:01 -0700764
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700765 err = dynamic_netconsole_init();
766 if (err)
767 goto undonotifier;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 register_console(&netconsole);
770 printk(KERN_INFO "netconsole: network logging started\n");
Satyam Sharmad39badf2007-08-10 15:27:24 -0700771
Satyam Sharmad39badf2007-08-10 15:27:24 -0700772 return err;
Satyam Sharmab5427c22007-08-10 15:33:40 -0700773
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700774undonotifier:
775 unregister_netdevice_notifier(&netconsole_netdev_notifier);
776
Satyam Sharmab5427c22007-08-10 15:33:40 -0700777fail:
778 printk(KERN_ERR "netconsole: cleaning up\n");
779
780 /*
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700781 * Remove all targets and destroy them (only targets created
782 * from the boot/module option exist here). Skipping the list
Satyam Sharmab5427c22007-08-10 15:33:40 -0700783 * lock is safe here, and netpoll_cleanup() will sleep.
784 */
785 list_for_each_entry_safe(nt, tmp, &target_list, list) {
786 list_del(&nt->list);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700787 free_param_target(nt);
Satyam Sharmab5427c22007-08-10 15:33:40 -0700788 }
789
790 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Satyam Sharmad39badf2007-08-10 15:27:24 -0700793static void __exit cleanup_netconsole(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Satyam Sharmab5427c22007-08-10 15:33:40 -0700795 struct netconsole_target *nt, *tmp;
Satyam Sharmadf180e32007-08-10 15:32:14 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 unregister_console(&netconsole);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700798 dynamic_netconsole_exit();
Satyam Sharma17951f32007-08-10 15:33:01 -0700799 unregister_netdevice_notifier(&netconsole_netdev_notifier);
Satyam Sharmab5427c22007-08-10 15:33:40 -0700800
801 /*
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700802 * Targets created via configfs pin references on our module
803 * and would first be rmdir(2)'ed from userspace. We reach
804 * here only when they are already destroyed, and only those
805 * created from the boot/module option are left, so remove and
806 * destroy them. Skipping the list lock is safe here, and
807 * netpoll_cleanup() will sleep.
Satyam Sharmab5427c22007-08-10 15:33:40 -0700808 */
809 list_for_each_entry_safe(nt, tmp, &target_list, list) {
810 list_del(&nt->list);
Satyam Sharma0bcc1812007-08-10 15:35:05 -0700811 free_param_target(nt);
Satyam Sharmab5427c22007-08-10 15:33:40 -0700812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815module_init(init_netconsole);
816module_exit(cleanup_netconsole);