netconsole.c 21 KB

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