]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/net/netconsole.c | |
3 | * | |
4 | * Copyright (C) 2001 Ingo Molnar <[email protected]> | |
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> | |
1da177e4 LT |
38 | #include <linux/init.h> |
39 | #include <linux/module.h> | |
40 | #include <linux/console.h> | |
1da177e4 LT |
41 | #include <linux/moduleparam.h> |
42 | #include <linux/string.h> | |
1da177e4 | 43 | #include <linux/netpoll.h> |
0bcc1816 SS |
44 | #include <linux/inet.h> |
45 | #include <linux/configfs.h> | |
1da177e4 LT |
46 | |
47 | MODULE_AUTHOR("Maintainer: Matt Mackall <[email protected]>"); | |
48 | MODULE_DESCRIPTION("Console driver for network interfaces"); | |
49 | MODULE_LICENSE("GPL"); | |
50 | ||
d39badf0 SS |
51 | #define MAX_PARAM_LENGTH 256 |
52 | #define MAX_PRINT_CHUNK 1000 | |
53 | ||
54 | static char config[MAX_PARAM_LENGTH]; | |
55 | module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0); | |
61a2d07d | 56 | MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]"); |
1da177e4 | 57 | |
d2b60881 SS |
58 | #ifndef MODULE |
59 | static 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 | ||
b5427c27 SS |
67 | /* Linked list of all configured targets */ |
68 | static LIST_HEAD(target_list); | |
69 | ||
70 | /* This needs to be a spinlock because write_msg() cannot sleep */ | |
71 | static DEFINE_SPINLOCK(target_list_lock); | |
72 | ||
df180e36 SS |
73 | /** |
74 | * struct netconsole_target - Represents a configured netconsole target. | |
b5427c27 | 75 | * @list: Links this target into the target_list. |
0bcc1816 SS |
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). | |
df180e36 | 83 | * @np: The netpoll structure for this target. |
0bcc1816 SS |
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) | |
df180e36 SS |
92 | */ |
93 | struct netconsole_target { | |
b5427c27 | 94 | struct list_head list; |
0bcc1816 SS |
95 | #ifdef CONFIG_NETCONSOLE_DYNAMIC |
96 | struct config_item item; | |
97 | #endif | |
98 | int enabled; | |
df180e36 SS |
99 | struct netpoll np; |
100 | }; | |
101 | ||
0bcc1816 SS |
102 | #ifdef CONFIG_NETCONSOLE_DYNAMIC |
103 | ||
104 | static struct configfs_subsystem netconsole_subsys; | |
105 | ||
106 | static 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 | ||
113 | static 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 | */ | |
123 | static void netconsole_target_get(struct netconsole_target *nt) | |
124 | { | |
125 | if (config_item_name(&nt->item)) | |
126 | config_item_get(&nt->item); | |
127 | } | |
128 | ||
129 | static 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 | ||
137 | static int __init dynamic_netconsole_init(void) | |
138 | { | |
139 | return 0; | |
140 | } | |
141 | ||
142 | static 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 | */ | |
150 | static void netconsole_target_get(struct netconsole_target *nt) | |
151 | { | |
152 | } | |
153 | ||
154 | static 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 */ | |
161 | static struct netconsole_target *alloc_param_target(char *target_config) | |
b5427c27 SS |
162 | { |
163 | int err = -ENOMEM; | |
164 | struct netconsole_target *nt; | |
165 | ||
0bcc1816 SS |
166 | /* |
167 | * Allocate and initialize with defaults. | |
168 | * Note that these targets get their config_item fields zeroed-out. | |
169 | */ | |
b5427c27 SS |
170 | 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 | ||
0bcc1816 SS |
191 | nt->enabled = 1; |
192 | ||
b5427c27 SS |
193 | return nt; |
194 | ||
195 | fail: | |
196 | kfree(nt); | |
197 | return ERR_PTR(err); | |
198 | } | |
199 | ||
0bcc1816 SS |
200 | /* Cleanup netpoll for given target (from boot/module param) and free it */ |
201 | static void free_param_target(struct netconsole_target *nt) | |
b5427c27 SS |
202 | { |
203 | netpoll_cleanup(&nt->np); | |
204 | kfree(nt); | |
205 | } | |
1da177e4 | 206 | |
0bcc1816 SS |
207 | #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 | ||
227 | struct 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 | ||
236 | static 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 | */ | |
248 | static 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 | ||
275 | static ssize_t show_enabled(struct netconsole_target *nt, char *buf) | |
276 | { | |
277 | return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled); | |
278 | } | |
279 | ||
280 | static 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 | ||
285 | static 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 | ||
290 | static 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 | ||
295 | static ssize_t show_local_ip(struct netconsole_target *nt, char *buf) | |
296 | { | |
e7557af5 | 297 | return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip); |
0bcc1816 SS |
298 | } |
299 | ||
300 | static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf) | |
301 | { | |
e7557af5 | 302 | return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip); |
0bcc1816 SS |
303 | } |
304 | ||
305 | static ssize_t show_local_mac(struct netconsole_target *nt, char *buf) | |
306 | { | |
09538641 | 307 | struct net_device *dev = nt->np.dev; |
e174961c | 308 | static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
09538641 | 309 | |
e174961c | 310 | return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast); |
0bcc1816 SS |
311 | } |
312 | ||
313 | static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf) | |
314 | { | |
e174961c | 315 | return snprintf(buf, PAGE_SIZE, "%pM\n", nt->np.remote_mac); |
0bcc1816 SS |
316 | } |
317 | ||
318 | /* | |
319 | * This one is special -- targets created through the configfs interface | |
320 | * are not enabled (and the corresponding netpoll activated) by default. | |
321 | * The user is expected to set the desired parameters first (which | |
322 | * would enable him to dynamically add new netpoll targets for new | |
323 | * network interfaces as and when they come up). | |
324 | */ | |
325 | static ssize_t store_enabled(struct netconsole_target *nt, | |
326 | const char *buf, | |
327 | size_t count) | |
328 | { | |
329 | int err; | |
330 | long enabled; | |
331 | ||
332 | enabled = strtol10_check_range(buf, 0, 1); | |
333 | if (enabled < 0) | |
334 | return enabled; | |
335 | ||
336 | if (enabled) { /* 1 */ | |
337 | ||
338 | /* | |
339 | * Skip netpoll_parse_options() -- all the attributes are | |
340 | * already configured via configfs. Just print them out. | |
341 | */ | |
342 | netpoll_print_options(&nt->np); | |
343 | ||
344 | err = netpoll_setup(&nt->np); | |
345 | if (err) | |
346 | return err; | |
347 | ||
348 | printk(KERN_INFO "netconsole: network logging started\n"); | |
349 | ||
350 | } else { /* 0 */ | |
351 | netpoll_cleanup(&nt->np); | |
352 | } | |
353 | ||
354 | nt->enabled = enabled; | |
355 | ||
356 | return strnlen(buf, count); | |
357 | } | |
358 | ||
359 | static ssize_t store_dev_name(struct netconsole_target *nt, | |
360 | const char *buf, | |
361 | size_t count) | |
362 | { | |
363 | size_t len; | |
364 | ||
365 | if (nt->enabled) { | |
366 | printk(KERN_ERR "netconsole: target (%s) is enabled, " | |
367 | "disable to update parameters\n", | |
368 | config_item_name(&nt->item)); | |
369 | return -EINVAL; | |
370 | } | |
371 | ||
372 | strlcpy(nt->np.dev_name, buf, IFNAMSIZ); | |
373 | ||
374 | /* Get rid of possible trailing newline from echo(1) */ | |
375 | len = strnlen(nt->np.dev_name, IFNAMSIZ); | |
376 | if (nt->np.dev_name[len - 1] == '\n') | |
377 | nt->np.dev_name[len - 1] = '\0'; | |
378 | ||
379 | return strnlen(buf, count); | |
380 | } | |
381 | ||
382 | static ssize_t store_local_port(struct netconsole_target *nt, | |
383 | const char *buf, | |
384 | size_t count) | |
385 | { | |
386 | long local_port; | |
387 | #define __U16_MAX ((__u16) ~0U) | |
388 | ||
389 | if (nt->enabled) { | |
390 | printk(KERN_ERR "netconsole: target (%s) is enabled, " | |
391 | "disable to update parameters\n", | |
392 | config_item_name(&nt->item)); | |
393 | return -EINVAL; | |
394 | } | |
395 | ||
396 | local_port = strtol10_check_range(buf, 0, __U16_MAX); | |
397 | if (local_port < 0) | |
398 | return local_port; | |
399 | ||
400 | nt->np.local_port = local_port; | |
401 | ||
402 | return strnlen(buf, count); | |
403 | } | |
404 | ||
405 | static ssize_t store_remote_port(struct netconsole_target *nt, | |
406 | const char *buf, | |
407 | size_t count) | |
408 | { | |
409 | long remote_port; | |
410 | #define __U16_MAX ((__u16) ~0U) | |
411 | ||
412 | if (nt->enabled) { | |
413 | printk(KERN_ERR "netconsole: target (%s) is enabled, " | |
414 | "disable to update parameters\n", | |
415 | config_item_name(&nt->item)); | |
416 | return -EINVAL; | |
417 | } | |
418 | ||
419 | remote_port = strtol10_check_range(buf, 0, __U16_MAX); | |
420 | if (remote_port < 0) | |
421 | return remote_port; | |
422 | ||
423 | nt->np.remote_port = remote_port; | |
424 | ||
425 | return strnlen(buf, count); | |
426 | } | |
427 | ||
428 | static ssize_t store_local_ip(struct netconsole_target *nt, | |
429 | const char *buf, | |
430 | size_t count) | |
431 | { | |
432 | if (nt->enabled) { | |
433 | printk(KERN_ERR "netconsole: target (%s) is enabled, " | |
434 | "disable to update parameters\n", | |
435 | config_item_name(&nt->item)); | |
436 | return -EINVAL; | |
437 | } | |
438 | ||
e7557af5 | 439 | nt->np.local_ip = in_aton(buf); |
0bcc1816 SS |
440 | |
441 | return strnlen(buf, count); | |
442 | } | |
443 | ||
444 | static ssize_t store_remote_ip(struct netconsole_target *nt, | |
445 | const char *buf, | |
446 | size_t count) | |
447 | { | |
448 | if (nt->enabled) { | |
449 | printk(KERN_ERR "netconsole: target (%s) is enabled, " | |
450 | "disable to update parameters\n", | |
451 | config_item_name(&nt->item)); | |
452 | return -EINVAL; | |
453 | } | |
454 | ||
e7557af5 | 455 | nt->np.remote_ip = in_aton(buf); |
0bcc1816 SS |
456 | |
457 | return strnlen(buf, count); | |
458 | } | |
459 | ||
460 | static ssize_t store_remote_mac(struct netconsole_target *nt, | |
461 | const char *buf, | |
462 | size_t count) | |
463 | { | |
464 | u8 remote_mac[ETH_ALEN]; | |
465 | char *p = (char *) buf; | |
466 | int i; | |
467 | ||
468 | if (nt->enabled) { | |
469 | printk(KERN_ERR "netconsole: target (%s) is enabled, " | |
470 | "disable to update parameters\n", | |
471 | config_item_name(&nt->item)); | |
472 | return -EINVAL; | |
473 | } | |
474 | ||
475 | for (i = 0; i < ETH_ALEN - 1; i++) { | |
476 | remote_mac[i] = simple_strtoul(p, &p, 16); | |
477 | if (*p != ':') | |
478 | goto invalid; | |
479 | p++; | |
480 | } | |
481 | remote_mac[ETH_ALEN - 1] = simple_strtoul(p, &p, 16); | |
482 | if (*p && (*p != '\n')) | |
483 | goto invalid; | |
484 | ||
485 | memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN); | |
486 | ||
487 | return strnlen(buf, count); | |
488 | ||
489 | invalid: | |
490 | printk(KERN_ERR "netconsole: invalid input\n"); | |
491 | return -EINVAL; | |
492 | } | |
493 | ||
494 | /* | |
495 | * Attribute definitions for netconsole_target. | |
496 | */ | |
497 | ||
498 | #define NETCONSOLE_TARGET_ATTR_RO(_name) \ | |
499 | static struct netconsole_target_attr netconsole_target_##_name = \ | |
500 | __CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL) | |
501 | ||
502 | #define NETCONSOLE_TARGET_ATTR_RW(_name) \ | |
503 | static struct netconsole_target_attr netconsole_target_##_name = \ | |
504 | __CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name) | |
505 | ||
506 | NETCONSOLE_TARGET_ATTR_RW(enabled); | |
507 | NETCONSOLE_TARGET_ATTR_RW(dev_name); | |
508 | NETCONSOLE_TARGET_ATTR_RW(local_port); | |
509 | NETCONSOLE_TARGET_ATTR_RW(remote_port); | |
510 | NETCONSOLE_TARGET_ATTR_RW(local_ip); | |
511 | NETCONSOLE_TARGET_ATTR_RW(remote_ip); | |
512 | NETCONSOLE_TARGET_ATTR_RO(local_mac); | |
513 | NETCONSOLE_TARGET_ATTR_RW(remote_mac); | |
514 | ||
515 | static struct configfs_attribute *netconsole_target_attrs[] = { | |
516 | &netconsole_target_enabled.attr, | |
517 | &netconsole_target_dev_name.attr, | |
518 | &netconsole_target_local_port.attr, | |
519 | &netconsole_target_remote_port.attr, | |
520 | &netconsole_target_local_ip.attr, | |
521 | &netconsole_target_remote_ip.attr, | |
522 | &netconsole_target_local_mac.attr, | |
523 | &netconsole_target_remote_mac.attr, | |
524 | NULL, | |
525 | }; | |
526 | ||
527 | /* | |
528 | * Item operations and type for netconsole_target. | |
529 | */ | |
530 | ||
531 | static void netconsole_target_release(struct config_item *item) | |
532 | { | |
533 | kfree(to_target(item)); | |
534 | } | |
535 | ||
536 | static ssize_t netconsole_target_attr_show(struct config_item *item, | |
537 | struct configfs_attribute *attr, | |
538 | char *buf) | |
539 | { | |
540 | ssize_t ret = -EINVAL; | |
541 | struct netconsole_target *nt = to_target(item); | |
542 | struct netconsole_target_attr *na = | |
543 | container_of(attr, struct netconsole_target_attr, attr); | |
544 | ||
545 | if (na->show) | |
546 | ret = na->show(nt, buf); | |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | static ssize_t netconsole_target_attr_store(struct config_item *item, | |
552 | struct configfs_attribute *attr, | |
553 | const char *buf, | |
554 | size_t count) | |
555 | { | |
556 | ssize_t ret = -EINVAL; | |
557 | struct netconsole_target *nt = to_target(item); | |
558 | struct netconsole_target_attr *na = | |
559 | container_of(attr, struct netconsole_target_attr, attr); | |
560 | ||
561 | if (na->store) | |
562 | ret = na->store(nt, buf, count); | |
563 | ||
564 | return ret; | |
565 | } | |
566 | ||
567 | static struct configfs_item_operations netconsole_target_item_ops = { | |
568 | .release = netconsole_target_release, | |
569 | .show_attribute = netconsole_target_attr_show, | |
570 | .store_attribute = netconsole_target_attr_store, | |
571 | }; | |
572 | ||
573 | static struct config_item_type netconsole_target_type = { | |
574 | .ct_attrs = netconsole_target_attrs, | |
575 | .ct_item_ops = &netconsole_target_item_ops, | |
576 | .ct_owner = THIS_MODULE, | |
577 | }; | |
578 | ||
579 | /* | |
580 | * Group operations and type for netconsole_subsys. | |
581 | */ | |
582 | ||
f89ab861 JB |
583 | static struct config_item *make_netconsole_target(struct config_group *group, |
584 | const char *name) | |
0bcc1816 SS |
585 | { |
586 | unsigned long flags; | |
587 | struct netconsole_target *nt; | |
588 | ||
589 | /* | |
590 | * Allocate and initialize with defaults. | |
591 | * Target is disabled at creation (enabled == 0). | |
592 | */ | |
593 | nt = kzalloc(sizeof(*nt), GFP_KERNEL); | |
594 | if (!nt) { | |
595 | printk(KERN_ERR "netconsole: failed to allocate memory\n"); | |
a6795e9e | 596 | return ERR_PTR(-ENOMEM); |
0bcc1816 SS |
597 | } |
598 | ||
599 | nt->np.name = "netconsole"; | |
600 | strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ); | |
601 | nt->np.local_port = 6665; | |
602 | nt->np.remote_port = 6666; | |
603 | memset(nt->np.remote_mac, 0xff, ETH_ALEN); | |
604 | ||
605 | /* Initialize the config_item member */ | |
606 | config_item_init_type_name(&nt->item, name, &netconsole_target_type); | |
607 | ||
608 | /* Adding, but it is disabled */ | |
609 | spin_lock_irqsave(&target_list_lock, flags); | |
610 | list_add(&nt->list, &target_list); | |
611 | spin_unlock_irqrestore(&target_list_lock, flags); | |
612 | ||
f89ab861 | 613 | return &nt->item; |
0bcc1816 SS |
614 | } |
615 | ||
616 | static void drop_netconsole_target(struct config_group *group, | |
617 | struct config_item *item) | |
618 | { | |
619 | unsigned long flags; | |
620 | struct netconsole_target *nt = to_target(item); | |
621 | ||
622 | spin_lock_irqsave(&target_list_lock, flags); | |
623 | list_del(&nt->list); | |
624 | spin_unlock_irqrestore(&target_list_lock, flags); | |
625 | ||
626 | /* | |
627 | * The target may have never been enabled, or was manually disabled | |
628 | * before being removed so netpoll may have already been cleaned up. | |
629 | */ | |
630 | if (nt->enabled) | |
631 | netpoll_cleanup(&nt->np); | |
632 | ||
633 | config_item_put(&nt->item); | |
634 | } | |
635 | ||
636 | static struct configfs_group_operations netconsole_subsys_group_ops = { | |
637 | .make_item = make_netconsole_target, | |
638 | .drop_item = drop_netconsole_target, | |
639 | }; | |
640 | ||
641 | static struct config_item_type netconsole_subsys_type = { | |
642 | .ct_group_ops = &netconsole_subsys_group_ops, | |
643 | .ct_owner = THIS_MODULE, | |
644 | }; | |
645 | ||
646 | /* The netconsole configfs subsystem */ | |
647 | static struct configfs_subsystem netconsole_subsys = { | |
648 | .su_group = { | |
649 | .cg_item = { | |
650 | .ci_namebuf = "netconsole", | |
651 | .ci_type = &netconsole_subsys_type, | |
652 | }, | |
653 | }, | |
654 | }; | |
655 | ||
656 | #endif /* CONFIG_NETCONSOLE_DYNAMIC */ | |
657 | ||
17951f34 SS |
658 | /* Handle network interface device notifications */ |
659 | static int netconsole_netdev_event(struct notifier_block *this, | |
660 | unsigned long event, | |
661 | void *ptr) | |
662 | { | |
b5427c27 SS |
663 | unsigned long flags; |
664 | struct netconsole_target *nt; | |
17951f34 | 665 | struct net_device *dev = ptr; |
17951f34 | 666 | |
2382b15b | 667 | if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER)) |
b5427c27 SS |
668 | goto done; |
669 | ||
670 | spin_lock_irqsave(&target_list_lock, flags); | |
671 | list_for_each_entry(nt, &target_list, list) { | |
0bcc1816 | 672 | netconsole_target_get(nt); |
b5427c27 SS |
673 | if (nt->np.dev == dev) { |
674 | switch (event) { | |
b5427c27 SS |
675 | case NETDEV_CHANGENAME: |
676 | strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ); | |
677 | break; | |
2382b15b BP |
678 | case NETDEV_UNREGISTER: |
679 | if (!nt->enabled) | |
680 | break; | |
681 | netpoll_cleanup(&nt->np); | |
682 | nt->enabled = 0; | |
683 | printk(KERN_INFO "netconsole: network logging stopped" | |
684 | ", interface %s unregistered\n", | |
685 | dev->name); | |
686 | break; | |
b5427c27 | 687 | } |
17951f34 | 688 | } |
0bcc1816 | 689 | netconsole_target_put(nt); |
17951f34 | 690 | } |
b5427c27 | 691 | spin_unlock_irqrestore(&target_list_lock, flags); |
17951f34 | 692 | |
b5427c27 | 693 | done: |
17951f34 SS |
694 | return NOTIFY_DONE; |
695 | } | |
696 | ||
697 | static struct notifier_block netconsole_netdev_notifier = { | |
698 | .notifier_call = netconsole_netdev_event, | |
699 | }; | |
700 | ||
1da177e4 LT |
701 | static void write_msg(struct console *con, const char *msg, unsigned int len) |
702 | { | |
703 | int frag, left; | |
704 | unsigned long flags; | |
b5427c27 SS |
705 | struct netconsole_target *nt; |
706 | const char *tmp; | |
707 | ||
708 | /* Avoid taking lock and disabling interrupts unnecessarily */ | |
709 | if (list_empty(&target_list)) | |
710 | return; | |
711 | ||
712 | spin_lock_irqsave(&target_list_lock, flags); | |
713 | list_for_each_entry(nt, &target_list, list) { | |
0bcc1816 SS |
714 | netconsole_target_get(nt); |
715 | if (nt->enabled && netif_running(nt->np.dev)) { | |
b5427c27 SS |
716 | /* |
717 | * We nest this inside the for-each-target loop above | |
718 | * so that we're able to get as much logging out to | |
719 | * at least one target if we die inside here, instead | |
720 | * of unnecessarily keeping all targets in lock-step. | |
721 | */ | |
722 | tmp = msg; | |
723 | for (left = len; left;) { | |
724 | frag = min(left, MAX_PRINT_CHUNK); | |
725 | netpoll_send_udp(&nt->np, tmp, frag); | |
726 | tmp += frag; | |
727 | left -= frag; | |
728 | } | |
0cc120be | 729 | } |
0bcc1816 | 730 | netconsole_target_put(nt); |
1da177e4 | 731 | } |
b5427c27 | 732 | spin_unlock_irqrestore(&target_list_lock, flags); |
1da177e4 LT |
733 | } |
734 | ||
735 | static struct console netconsole = { | |
d39badf0 | 736 | .name = "netcon", |
0517deed | 737 | .flags = CON_ENABLED, |
d39badf0 | 738 | .write = write_msg, |
1da177e4 LT |
739 | }; |
740 | ||
d39badf0 | 741 | static int __init init_netconsole(void) |
1da177e4 | 742 | { |
0bcc1816 | 743 | int err; |
b5427c27 SS |
744 | struct netconsole_target *nt, *tmp; |
745 | unsigned long flags; | |
746 | char *target_config; | |
747 | char *input = config; | |
b41848b6 | 748 | |
0bcc1816 SS |
749 | if (strnlen(input, MAX_PARAM_LENGTH)) { |
750 | while ((target_config = strsep(&input, ";"))) { | |
751 | nt = alloc_param_target(target_config); | |
752 | if (IS_ERR(nt)) { | |
753 | err = PTR_ERR(nt); | |
754 | goto fail; | |
755 | } | |
0517deed ME |
756 | /* Dump existing printks when we register */ |
757 | netconsole.flags |= CON_PRINTBUFFER; | |
758 | ||
0bcc1816 SS |
759 | spin_lock_irqsave(&target_list_lock, flags); |
760 | list_add(&nt->list, &target_list); | |
761 | spin_unlock_irqrestore(&target_list_lock, flags); | |
b5427c27 | 762 | } |
b5427c27 | 763 | } |
1da177e4 | 764 | |
17951f34 SS |
765 | err = register_netdevice_notifier(&netconsole_netdev_notifier); |
766 | if (err) | |
b5427c27 | 767 | goto fail; |
17951f34 | 768 | |
0bcc1816 SS |
769 | err = dynamic_netconsole_init(); |
770 | if (err) | |
771 | goto undonotifier; | |
772 | ||
1da177e4 LT |
773 | register_console(&netconsole); |
774 | printk(KERN_INFO "netconsole: network logging started\n"); | |
d39badf0 | 775 | |
d39badf0 | 776 | return err; |
b5427c27 | 777 | |
0bcc1816 SS |
778 | undonotifier: |
779 | unregister_netdevice_notifier(&netconsole_netdev_notifier); | |
780 | ||
b5427c27 SS |
781 | fail: |
782 | printk(KERN_ERR "netconsole: cleaning up\n"); | |
783 | ||
784 | /* | |
0bcc1816 SS |
785 | * Remove all targets and destroy them (only targets created |
786 | * from the boot/module option exist here). Skipping the list | |
b5427c27 SS |
787 | * lock is safe here, and netpoll_cleanup() will sleep. |
788 | */ | |
789 | list_for_each_entry_safe(nt, tmp, &target_list, list) { | |
790 | list_del(&nt->list); | |
0bcc1816 | 791 | free_param_target(nt); |
b5427c27 SS |
792 | } |
793 | ||
794 | return err; | |
1da177e4 LT |
795 | } |
796 | ||
d39badf0 | 797 | static void __exit cleanup_netconsole(void) |
1da177e4 | 798 | { |
b5427c27 | 799 | struct netconsole_target *nt, *tmp; |
df180e36 | 800 | |
1da177e4 | 801 | unregister_console(&netconsole); |
0bcc1816 | 802 | dynamic_netconsole_exit(); |
17951f34 | 803 | unregister_netdevice_notifier(&netconsole_netdev_notifier); |
b5427c27 SS |
804 | |
805 | /* | |
0bcc1816 SS |
806 | * Targets created via configfs pin references on our module |
807 | * and would first be rmdir(2)'ed from userspace. We reach | |
808 | * here only when they are already destroyed, and only those | |
809 | * created from the boot/module option are left, so remove and | |
810 | * destroy them. Skipping the list lock is safe here, and | |
811 | * netpoll_cleanup() will sleep. | |
b5427c27 SS |
812 | */ |
813 | list_for_each_entry_safe(nt, tmp, &target_list, list) { | |
814 | list_del(&nt->list); | |
0bcc1816 | 815 | free_param_target(nt); |
b5427c27 | 816 | } |
1da177e4 LT |
817 | } |
818 | ||
819 | module_init(init_netconsole); | |
820 | module_exit(cleanup_netconsole); |