netconsole.c 20 KB

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