netconsole.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2, or (at your option)
  23. * any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. *
  34. ****************************************************************/
  35. #include <linux/mm.h>
  36. #include <linux/init.h>
  37. #include <linux/module.h>
  38. #include <linux/slab.h>
  39. #include <linux/console.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/string.h>
  42. #include <linux/netpoll.h>
  43. #include <linux/inet.h>
  44. #include <linux/configfs.h>
  45. MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
  46. MODULE_DESCRIPTION("Console driver for network interfaces");
  47. MODULE_LICENSE("GPL");
  48. #define MAX_PARAM_LENGTH 256
  49. #define MAX_PRINT_CHUNK 1000
  50. static char config[MAX_PARAM_LENGTH];
  51. module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
  52. MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
  53. #ifndef MODULE
  54. static int __init option_setup(char *opt)
  55. {
  56. strlcpy(config, opt, MAX_PARAM_LENGTH);
  57. return 1;
  58. }
  59. __setup("netconsole=", option_setup);
  60. #endif /* MODULE */
  61. /* Linked list of all configured targets */
  62. static LIST_HEAD(target_list);
  63. /* This needs to be a spinlock because write_msg() cannot sleep */
  64. static DEFINE_SPINLOCK(target_list_lock);
  65. /**
  66. * struct netconsole_target - Represents a configured netconsole target.
  67. * @list: Links this target into the target_list.
  68. * @item: Links us into the configfs subsystem hierarchy.
  69. * @enabled: On / off knob to enable / disable target.
  70. * Visible from userspace (read-write).
  71. * We maintain a strict 1:1 correspondence between this and
  72. * whether the corresponding netpoll is active or inactive.
  73. * Also, other parameters of a target may be modified at
  74. * runtime only when it is disabled (enabled == 0).
  75. * @np: The netpoll structure for this target.
  76. * Contains the other userspace visible parameters:
  77. * dev_name (read-write)
  78. * local_port (read-write)
  79. * remote_port (read-write)
  80. * local_ip (read-write)
  81. * remote_ip (read-write)
  82. * local_mac (read-only)
  83. * remote_mac (read-write)
  84. */
  85. struct netconsole_target {
  86. struct list_head list;
  87. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  88. struct config_item item;
  89. #endif
  90. int enabled;
  91. struct netpoll np;
  92. };
  93. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  94. static struct configfs_subsystem netconsole_subsys;
  95. static int __init dynamic_netconsole_init(void)
  96. {
  97. config_group_init(&netconsole_subsys.su_group);
  98. mutex_init(&netconsole_subsys.su_mutex);
  99. return configfs_register_subsystem(&netconsole_subsys);
  100. }
  101. static void __exit dynamic_netconsole_exit(void)
  102. {
  103. configfs_unregister_subsystem(&netconsole_subsys);
  104. }
  105. /*
  106. * Targets that were created by parsing the boot/module option string
  107. * do not exist in the configfs hierarchy (and have NULL names) and will
  108. * never go away, so make these a no-op for them.
  109. */
  110. static void netconsole_target_get(struct netconsole_target *nt)
  111. {
  112. if (config_item_name(&nt->item))
  113. config_item_get(&nt->item);
  114. }
  115. static void netconsole_target_put(struct netconsole_target *nt)
  116. {
  117. if (config_item_name(&nt->item))
  118. config_item_put(&nt->item);
  119. }
  120. #else /* !CONFIG_NETCONSOLE_DYNAMIC */
  121. static int __init dynamic_netconsole_init(void)
  122. {
  123. return 0;
  124. }
  125. static void __exit dynamic_netconsole_exit(void)
  126. {
  127. }
  128. /*
  129. * No danger of targets going away from under us when dynamic
  130. * reconfigurability is off.
  131. */
  132. static void netconsole_target_get(struct netconsole_target *nt)
  133. {
  134. }
  135. static void netconsole_target_put(struct netconsole_target *nt)
  136. {
  137. }
  138. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  139. /* Allocate new target (from boot/module param) and setup netpoll for it */
  140. static struct netconsole_target *alloc_param_target(char *target_config)
  141. {
  142. int err = -ENOMEM;
  143. struct netconsole_target *nt;
  144. /*
  145. * Allocate and initialize with defaults.
  146. * Note that these targets get their config_item fields zeroed-out.
  147. */
  148. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  149. if (!nt) {
  150. printk(KERN_ERR "netconsole: failed to allocate memory\n");
  151. goto fail;
  152. }
  153. nt->np.name = "netconsole";
  154. strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  155. nt->np.local_port = 6665;
  156. nt->np.remote_port = 6666;
  157. memset(nt->np.remote_mac, 0xff, ETH_ALEN);
  158. /* Parse parameters and setup netpoll */
  159. err = netpoll_parse_options(&nt->np, target_config);
  160. if (err)
  161. goto fail;
  162. err = netpoll_setup(&nt->np);
  163. if (err)
  164. goto fail;
  165. nt->enabled = 1;
  166. return nt;
  167. fail:
  168. kfree(nt);
  169. return ERR_PTR(err);
  170. }
  171. /* Cleanup netpoll for given target (from boot/module param) and free it */
  172. static void free_param_target(struct netconsole_target *nt)
  173. {
  174. netpoll_cleanup(&nt->np);
  175. kfree(nt);
  176. }
  177. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  178. /*
  179. * Our subsystem hierarchy is:
  180. *
  181. * /sys/kernel/config/netconsole/
  182. * |
  183. * <target>/
  184. * | enabled
  185. * | dev_name
  186. * | local_port
  187. * | remote_port
  188. * | local_ip
  189. * | remote_ip
  190. * | local_mac
  191. * | remote_mac
  192. * |
  193. * <target>/...
  194. */
  195. struct netconsole_target_attr {
  196. struct configfs_attribute attr;
  197. ssize_t (*show)(struct netconsole_target *nt,
  198. char *buf);
  199. ssize_t (*store)(struct netconsole_target *nt,
  200. const char *buf,
  201. size_t count);
  202. };
  203. static struct netconsole_target *to_target(struct config_item *item)
  204. {
  205. return item ?
  206. container_of(item, struct netconsole_target, item) :
  207. NULL;
  208. }
  209. /*
  210. * Attribute operations for netconsole_target.
  211. */
  212. static ssize_t show_enabled(struct netconsole_target *nt, char *buf)
  213. {
  214. return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled);
  215. }
  216. static ssize_t show_dev_name(struct netconsole_target *nt, char *buf)
  217. {
  218. return snprintf(buf, PAGE_SIZE, "%s\n", nt->np.dev_name);
  219. }
  220. static ssize_t show_local_port(struct netconsole_target *nt, char *buf)
  221. {
  222. return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.local_port);
  223. }
  224. static ssize_t show_remote_port(struct netconsole_target *nt, char *buf)
  225. {
  226. return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.remote_port);
  227. }
  228. static ssize_t show_local_ip(struct netconsole_target *nt, char *buf)
  229. {
  230. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip);
  231. }
  232. static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf)
  233. {
  234. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip);
  235. }
  236. static ssize_t show_local_mac(struct netconsole_target *nt, char *buf)
  237. {
  238. struct net_device *dev = nt->np.dev;
  239. static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  240. return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);
  241. }
  242. static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf)
  243. {
  244. return snprintf(buf, PAGE_SIZE, "%pM\n", nt->np.remote_mac);
  245. }
  246. /*
  247. * This one is special -- targets created through the configfs interface
  248. * are not enabled (and the corresponding netpoll activated) by default.
  249. * The user is expected to set the desired parameters first (which
  250. * would enable him to dynamically add new netpoll targets for new
  251. * network interfaces as and when they come up).
  252. */
  253. static ssize_t store_enabled(struct netconsole_target *nt,
  254. const char *buf,
  255. size_t count)
  256. {
  257. int enabled;
  258. int err;
  259. err = kstrtoint(buf, 10, &enabled);
  260. if (err < 0)
  261. return err;
  262. if (enabled < 0 || enabled > 1)
  263. return -EINVAL;
  264. if (enabled) { /* 1 */
  265. /*
  266. * Skip netpoll_parse_options() -- all the attributes are
  267. * already configured via configfs. Just print them out.
  268. */
  269. netpoll_print_options(&nt->np);
  270. err = netpoll_setup(&nt->np);
  271. if (err)
  272. return err;
  273. printk(KERN_INFO "netconsole: network logging started\n");
  274. } else { /* 0 */
  275. netpoll_cleanup(&nt->np);
  276. }
  277. nt->enabled = enabled;
  278. return strnlen(buf, count);
  279. }
  280. static ssize_t store_dev_name(struct netconsole_target *nt,
  281. const char *buf,
  282. size_t count)
  283. {
  284. size_t len;
  285. if (nt->enabled) {
  286. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  287. "disable to update parameters\n",
  288. config_item_name(&nt->item));
  289. return -EINVAL;
  290. }
  291. strlcpy(nt->np.dev_name, buf, IFNAMSIZ);
  292. /* Get rid of possible trailing newline from echo(1) */
  293. len = strnlen(nt->np.dev_name, IFNAMSIZ);
  294. if (nt->np.dev_name[len - 1] == '\n')
  295. nt->np.dev_name[len - 1] = '\0';
  296. return strnlen(buf, count);
  297. }
  298. static ssize_t store_local_port(struct netconsole_target *nt,
  299. const char *buf,
  300. size_t count)
  301. {
  302. int rv;
  303. if (nt->enabled) {
  304. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  305. "disable to update parameters\n",
  306. config_item_name(&nt->item));
  307. return -EINVAL;
  308. }
  309. rv = kstrtou16(buf, 10, &nt->np.local_port);
  310. if (rv < 0)
  311. return rv;
  312. return strnlen(buf, count);
  313. }
  314. static ssize_t store_remote_port(struct netconsole_target *nt,
  315. const char *buf,
  316. size_t count)
  317. {
  318. int rv;
  319. if (nt->enabled) {
  320. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  321. "disable to update parameters\n",
  322. config_item_name(&nt->item));
  323. return -EINVAL;
  324. }
  325. rv = kstrtou16(buf, 10, &nt->np.remote_port);
  326. if (rv < 0)
  327. return rv;
  328. return strnlen(buf, count);
  329. }
  330. static ssize_t store_local_ip(struct netconsole_target *nt,
  331. const char *buf,
  332. size_t count)
  333. {
  334. if (nt->enabled) {
  335. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  336. "disable to update parameters\n",
  337. config_item_name(&nt->item));
  338. return -EINVAL;
  339. }
  340. nt->np.local_ip = in_aton(buf);
  341. return strnlen(buf, count);
  342. }
  343. static ssize_t store_remote_ip(struct netconsole_target *nt,
  344. const char *buf,
  345. size_t count)
  346. {
  347. if (nt->enabled) {
  348. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  349. "disable to update parameters\n",
  350. config_item_name(&nt->item));
  351. return -EINVAL;
  352. }
  353. nt->np.remote_ip = in_aton(buf);
  354. return strnlen(buf, count);
  355. }
  356. static ssize_t store_remote_mac(struct netconsole_target *nt,
  357. const char *buf,
  358. size_t count)
  359. {
  360. u8 remote_mac[ETH_ALEN];
  361. char *p = (char *) buf;
  362. int i;
  363. if (nt->enabled) {
  364. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  365. "disable to update parameters\n",
  366. config_item_name(&nt->item));
  367. return -EINVAL;
  368. }
  369. for (i = 0; i < ETH_ALEN - 1; i++) {
  370. remote_mac[i] = simple_strtoul(p, &p, 16);
  371. if (*p != ':')
  372. goto invalid;
  373. p++;
  374. }
  375. remote_mac[ETH_ALEN - 1] = simple_strtoul(p, &p, 16);
  376. if (*p && (*p != '\n'))
  377. goto invalid;
  378. memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
  379. return strnlen(buf, count);
  380. invalid:
  381. printk(KERN_ERR "netconsole: invalid input\n");
  382. return -EINVAL;
  383. }
  384. /*
  385. * Attribute definitions for netconsole_target.
  386. */
  387. #define NETCONSOLE_TARGET_ATTR_RO(_name) \
  388. static struct netconsole_target_attr netconsole_target_##_name = \
  389. __CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL)
  390. #define NETCONSOLE_TARGET_ATTR_RW(_name) \
  391. static struct netconsole_target_attr netconsole_target_##_name = \
  392. __CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
  393. NETCONSOLE_TARGET_ATTR_RW(enabled);
  394. NETCONSOLE_TARGET_ATTR_RW(dev_name);
  395. NETCONSOLE_TARGET_ATTR_RW(local_port);
  396. NETCONSOLE_TARGET_ATTR_RW(remote_port);
  397. NETCONSOLE_TARGET_ATTR_RW(local_ip);
  398. NETCONSOLE_TARGET_ATTR_RW(remote_ip);
  399. NETCONSOLE_TARGET_ATTR_RO(local_mac);
  400. NETCONSOLE_TARGET_ATTR_RW(remote_mac);
  401. static struct configfs_attribute *netconsole_target_attrs[] = {
  402. &netconsole_target_enabled.attr,
  403. &netconsole_target_dev_name.attr,
  404. &netconsole_target_local_port.attr,
  405. &netconsole_target_remote_port.attr,
  406. &netconsole_target_local_ip.attr,
  407. &netconsole_target_remote_ip.attr,
  408. &netconsole_target_local_mac.attr,
  409. &netconsole_target_remote_mac.attr,
  410. NULL,
  411. };
  412. /*
  413. * Item operations and type for netconsole_target.
  414. */
  415. static void netconsole_target_release(struct config_item *item)
  416. {
  417. kfree(to_target(item));
  418. }
  419. static ssize_t netconsole_target_attr_show(struct config_item *item,
  420. struct configfs_attribute *attr,
  421. char *buf)
  422. {
  423. ssize_t ret = -EINVAL;
  424. struct netconsole_target *nt = to_target(item);
  425. struct netconsole_target_attr *na =
  426. container_of(attr, struct netconsole_target_attr, attr);
  427. if (na->show)
  428. ret = na->show(nt, buf);
  429. return ret;
  430. }
  431. static ssize_t netconsole_target_attr_store(struct config_item *item,
  432. struct configfs_attribute *attr,
  433. const char *buf,
  434. size_t count)
  435. {
  436. ssize_t ret = -EINVAL;
  437. struct netconsole_target *nt = to_target(item);
  438. struct netconsole_target_attr *na =
  439. container_of(attr, struct netconsole_target_attr, attr);
  440. if (na->store)
  441. ret = na->store(nt, buf, count);
  442. return ret;
  443. }
  444. static struct configfs_item_operations netconsole_target_item_ops = {
  445. .release = netconsole_target_release,
  446. .show_attribute = netconsole_target_attr_show,
  447. .store_attribute = netconsole_target_attr_store,
  448. };
  449. static struct config_item_type netconsole_target_type = {
  450. .ct_attrs = netconsole_target_attrs,
  451. .ct_item_ops = &netconsole_target_item_ops,
  452. .ct_owner = THIS_MODULE,
  453. };
  454. /*
  455. * Group operations and type for netconsole_subsys.
  456. */
  457. static struct config_item *make_netconsole_target(struct config_group *group,
  458. const char *name)
  459. {
  460. unsigned long flags;
  461. struct netconsole_target *nt;
  462. /*
  463. * Allocate and initialize with defaults.
  464. * Target is disabled at creation (enabled == 0).
  465. */
  466. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  467. if (!nt) {
  468. printk(KERN_ERR "netconsole: failed to allocate memory\n");
  469. return ERR_PTR(-ENOMEM);
  470. }
  471. nt->np.name = "netconsole";
  472. strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  473. nt->np.local_port = 6665;
  474. nt->np.remote_port = 6666;
  475. memset(nt->np.remote_mac, 0xff, ETH_ALEN);
  476. /* Initialize the config_item member */
  477. config_item_init_type_name(&nt->item, name, &netconsole_target_type);
  478. /* Adding, but it is disabled */
  479. spin_lock_irqsave(&target_list_lock, flags);
  480. list_add(&nt->list, &target_list);
  481. spin_unlock_irqrestore(&target_list_lock, flags);
  482. return &nt->item;
  483. }
  484. static void drop_netconsole_target(struct config_group *group,
  485. struct config_item *item)
  486. {
  487. unsigned long flags;
  488. struct netconsole_target *nt = to_target(item);
  489. spin_lock_irqsave(&target_list_lock, flags);
  490. list_del(&nt->list);
  491. spin_unlock_irqrestore(&target_list_lock, flags);
  492. /*
  493. * The target may have never been enabled, or was manually disabled
  494. * before being removed so netpoll may have already been cleaned up.
  495. */
  496. if (nt->enabled)
  497. netpoll_cleanup(&nt->np);
  498. config_item_put(&nt->item);
  499. }
  500. static struct configfs_group_operations netconsole_subsys_group_ops = {
  501. .make_item = make_netconsole_target,
  502. .drop_item = drop_netconsole_target,
  503. };
  504. static struct config_item_type netconsole_subsys_type = {
  505. .ct_group_ops = &netconsole_subsys_group_ops,
  506. .ct_owner = THIS_MODULE,
  507. };
  508. /* The netconsole configfs subsystem */
  509. static struct configfs_subsystem netconsole_subsys = {
  510. .su_group = {
  511. .cg_item = {
  512. .ci_namebuf = "netconsole",
  513. .ci_type = &netconsole_subsys_type,
  514. },
  515. },
  516. };
  517. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  518. /* Handle network interface device notifications */
  519. static int netconsole_netdev_event(struct notifier_block *this,
  520. unsigned long event,
  521. void *ptr)
  522. {
  523. unsigned long flags;
  524. struct netconsole_target *nt;
  525. struct net_device *dev = ptr;
  526. bool stopped = false;
  527. if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
  528. event == NETDEV_BONDING_DESLAVE || event == NETDEV_GOING_DOWN))
  529. goto done;
  530. spin_lock_irqsave(&target_list_lock, flags);
  531. restart:
  532. list_for_each_entry(nt, &target_list, list) {
  533. netconsole_target_get(nt);
  534. if (nt->np.dev == dev) {
  535. switch (event) {
  536. case NETDEV_CHANGENAME:
  537. strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  538. break;
  539. case NETDEV_UNREGISTER:
  540. /*
  541. * rtnl_lock already held
  542. */
  543. if (nt->np.dev) {
  544. spin_unlock_irqrestore(
  545. &target_list_lock,
  546. flags);
  547. __netpoll_cleanup(&nt->np);
  548. spin_lock_irqsave(&target_list_lock,
  549. flags);
  550. dev_put(nt->np.dev);
  551. nt->np.dev = NULL;
  552. netconsole_target_put(nt);
  553. goto restart;
  554. }
  555. /* Fall through */
  556. case NETDEV_GOING_DOWN:
  557. case NETDEV_BONDING_DESLAVE:
  558. nt->enabled = 0;
  559. stopped = true;
  560. break;
  561. }
  562. }
  563. netconsole_target_put(nt);
  564. }
  565. spin_unlock_irqrestore(&target_list_lock, flags);
  566. if (stopped && (event == NETDEV_UNREGISTER || event == NETDEV_BONDING_DESLAVE))
  567. printk(KERN_INFO "netconsole: network logging stopped on "
  568. "interface %s as it %s\n", dev->name,
  569. event == NETDEV_UNREGISTER ? "unregistered" : "released slaves");
  570. done:
  571. return NOTIFY_DONE;
  572. }
  573. static struct notifier_block netconsole_netdev_notifier = {
  574. .notifier_call = netconsole_netdev_event,
  575. };
  576. static void write_msg(struct console *con, const char *msg, unsigned int len)
  577. {
  578. int frag, left;
  579. unsigned long flags;
  580. struct netconsole_target *nt;
  581. const char *tmp;
  582. /* Avoid taking lock and disabling interrupts unnecessarily */
  583. if (list_empty(&target_list))
  584. return;
  585. spin_lock_irqsave(&target_list_lock, flags);
  586. list_for_each_entry(nt, &target_list, list) {
  587. netconsole_target_get(nt);
  588. if (nt->enabled && netif_running(nt->np.dev)) {
  589. /*
  590. * We nest this inside the for-each-target loop above
  591. * so that we're able to get as much logging out to
  592. * at least one target if we die inside here, instead
  593. * of unnecessarily keeping all targets in lock-step.
  594. */
  595. tmp = msg;
  596. for (left = len; left;) {
  597. frag = min(left, MAX_PRINT_CHUNK);
  598. netpoll_send_udp(&nt->np, tmp, frag);
  599. tmp += frag;
  600. left -= frag;
  601. }
  602. }
  603. netconsole_target_put(nt);
  604. }
  605. spin_unlock_irqrestore(&target_list_lock, flags);
  606. }
  607. static struct console netconsole = {
  608. .name = "netcon",
  609. .flags = CON_ENABLED,
  610. .write = write_msg,
  611. };
  612. static int __init init_netconsole(void)
  613. {
  614. int err;
  615. struct netconsole_target *nt, *tmp;
  616. unsigned long flags;
  617. char *target_config;
  618. char *input = config;
  619. if (strnlen(input, MAX_PARAM_LENGTH)) {
  620. while ((target_config = strsep(&input, ";"))) {
  621. nt = alloc_param_target(target_config);
  622. if (IS_ERR(nt)) {
  623. err = PTR_ERR(nt);
  624. goto fail;
  625. }
  626. /* Dump existing printks when we register */
  627. netconsole.flags |= CON_PRINTBUFFER;
  628. spin_lock_irqsave(&target_list_lock, flags);
  629. list_add(&nt->list, &target_list);
  630. spin_unlock_irqrestore(&target_list_lock, flags);
  631. }
  632. }
  633. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  634. if (err)
  635. goto fail;
  636. err = dynamic_netconsole_init();
  637. if (err)
  638. goto undonotifier;
  639. register_console(&netconsole);
  640. printk(KERN_INFO "netconsole: network logging started\n");
  641. return err;
  642. undonotifier:
  643. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  644. fail:
  645. printk(KERN_ERR "netconsole: cleaning up\n");
  646. /*
  647. * Remove all targets and destroy them (only targets created
  648. * from the boot/module option exist here). Skipping the list
  649. * lock is safe here, and netpoll_cleanup() will sleep.
  650. */
  651. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  652. list_del(&nt->list);
  653. free_param_target(nt);
  654. }
  655. return err;
  656. }
  657. static void __exit cleanup_netconsole(void)
  658. {
  659. struct netconsole_target *nt, *tmp;
  660. unregister_console(&netconsole);
  661. dynamic_netconsole_exit();
  662. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  663. /*
  664. * Targets created via configfs pin references on our module
  665. * and would first be rmdir(2)'ed from userspace. We reach
  666. * here only when they are already destroyed, and only those
  667. * created from the boot/module option are left, so remove and
  668. * destroy them. Skipping the list lock is safe here, and
  669. * netpoll_cleanup() will sleep.
  670. */
  671. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  672. list_del(&nt->list);
  673. free_param_target(nt);
  674. }
  675. }
  676. module_init(init_netconsole);
  677. module_exit(cleanup_netconsole);