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