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/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. static bool oops_only = false;
  54. module_param(oops_only, bool, 0600);
  55. MODULE_PARM_DESC(oops_only, "Only log oops messages");
  56. #ifndef MODULE
  57. static int __init option_setup(char *opt)
  58. {
  59. strlcpy(config, opt, MAX_PARAM_LENGTH);
  60. return 1;
  61. }
  62. __setup("netconsole=", option_setup);
  63. #endif /* MODULE */
  64. /* Linked list of all configured targets */
  65. static LIST_HEAD(target_list);
  66. /* This needs to be a spinlock because write_msg() cannot sleep */
  67. static DEFINE_SPINLOCK(target_list_lock);
  68. /**
  69. * struct netconsole_target - Represents a configured netconsole target.
  70. * @list: Links this target into the target_list.
  71. * @item: Links us into the configfs subsystem hierarchy.
  72. * @enabled: On / off knob to enable / disable target.
  73. * Visible from userspace (read-write).
  74. * We maintain a strict 1:1 correspondence between this and
  75. * whether the corresponding netpoll is active or inactive.
  76. * Also, other parameters of a target may be modified at
  77. * runtime only when it is disabled (enabled == 0).
  78. * @np: The netpoll structure for this target.
  79. * Contains the other userspace visible parameters:
  80. * dev_name (read-write)
  81. * local_port (read-write)
  82. * remote_port (read-write)
  83. * local_ip (read-write)
  84. * remote_ip (read-write)
  85. * local_mac (read-only)
  86. * remote_mac (read-write)
  87. */
  88. struct netconsole_target {
  89. struct list_head list;
  90. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  91. struct config_item item;
  92. #endif
  93. int enabled;
  94. struct netpoll np;
  95. };
  96. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  97. static struct configfs_subsystem netconsole_subsys;
  98. static int __init dynamic_netconsole_init(void)
  99. {
  100. config_group_init(&netconsole_subsys.su_group);
  101. mutex_init(&netconsole_subsys.su_mutex);
  102. return configfs_register_subsystem(&netconsole_subsys);
  103. }
  104. static void __exit dynamic_netconsole_exit(void)
  105. {
  106. configfs_unregister_subsystem(&netconsole_subsys);
  107. }
  108. /*
  109. * Targets that were created by parsing the boot/module option string
  110. * do not exist in the configfs hierarchy (and have NULL names) and will
  111. * never go away, so make these a no-op for them.
  112. */
  113. static void netconsole_target_get(struct netconsole_target *nt)
  114. {
  115. if (config_item_name(&nt->item))
  116. config_item_get(&nt->item);
  117. }
  118. static void netconsole_target_put(struct netconsole_target *nt)
  119. {
  120. if (config_item_name(&nt->item))
  121. config_item_put(&nt->item);
  122. }
  123. #else /* !CONFIG_NETCONSOLE_DYNAMIC */
  124. static int __init dynamic_netconsole_init(void)
  125. {
  126. return 0;
  127. }
  128. static void __exit dynamic_netconsole_exit(void)
  129. {
  130. }
  131. /*
  132. * No danger of targets going away from under us when dynamic
  133. * reconfigurability is off.
  134. */
  135. static void netconsole_target_get(struct netconsole_target *nt)
  136. {
  137. }
  138. static void netconsole_target_put(struct netconsole_target *nt)
  139. {
  140. }
  141. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  142. /* Allocate new target (from boot/module param) and setup netpoll for it */
  143. static struct netconsole_target *alloc_param_target(char *target_config)
  144. {
  145. int err = -ENOMEM;
  146. struct netconsole_target *nt;
  147. /*
  148. * Allocate and initialize with defaults.
  149. * Note that these targets get their config_item fields zeroed-out.
  150. */
  151. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  152. if (!nt)
  153. goto fail;
  154. nt->np.name = "netconsole";
  155. strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  156. nt->np.local_port = 6665;
  157. nt->np.remote_port = 6666;
  158. memset(nt->np.remote_mac, 0xff, ETH_ALEN);
  159. /* Parse parameters and setup netpoll */
  160. err = netpoll_parse_options(&nt->np, target_config);
  161. if (err)
  162. goto fail;
  163. err = netpoll_setup(&nt->np);
  164. if (err)
  165. goto fail;
  166. nt->enabled = 1;
  167. return nt;
  168. fail:
  169. kfree(nt);
  170. return ERR_PTR(err);
  171. }
  172. /* Cleanup netpoll for given target (from boot/module param) and free it */
  173. static void free_param_target(struct netconsole_target *nt)
  174. {
  175. netpoll_cleanup(&nt->np);
  176. kfree(nt);
  177. }
  178. #ifdef CONFIG_NETCONSOLE_DYNAMIC
  179. /*
  180. * Our subsystem hierarchy is:
  181. *
  182. * /sys/kernel/config/netconsole/
  183. * |
  184. * <target>/
  185. * | enabled
  186. * | dev_name
  187. * | local_port
  188. * | remote_port
  189. * | local_ip
  190. * | remote_ip
  191. * | local_mac
  192. * | remote_mac
  193. * |
  194. * <target>/...
  195. */
  196. struct netconsole_target_attr {
  197. struct configfs_attribute attr;
  198. ssize_t (*show)(struct netconsole_target *nt,
  199. char *buf);
  200. ssize_t (*store)(struct netconsole_target *nt,
  201. const char *buf,
  202. size_t count);
  203. };
  204. static struct netconsole_target *to_target(struct config_item *item)
  205. {
  206. return item ?
  207. container_of(item, struct netconsole_target, item) :
  208. NULL;
  209. }
  210. /*
  211. * Attribute operations for netconsole_target.
  212. */
  213. static ssize_t show_enabled(struct netconsole_target *nt, char *buf)
  214. {
  215. return snprintf(buf, PAGE_SIZE, "%d\n", nt->enabled);
  216. }
  217. static ssize_t show_dev_name(struct netconsole_target *nt, char *buf)
  218. {
  219. return snprintf(buf, PAGE_SIZE, "%s\n", nt->np.dev_name);
  220. }
  221. static ssize_t show_local_port(struct netconsole_target *nt, char *buf)
  222. {
  223. return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.local_port);
  224. }
  225. static ssize_t show_remote_port(struct netconsole_target *nt, char *buf)
  226. {
  227. return snprintf(buf, PAGE_SIZE, "%d\n", nt->np.remote_port);
  228. }
  229. static ssize_t show_local_ip(struct netconsole_target *nt, char *buf)
  230. {
  231. if (nt->np.ipv6)
  232. return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6);
  233. else
  234. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip);
  235. }
  236. static ssize_t show_remote_ip(struct netconsole_target *nt, char *buf)
  237. {
  238. if (nt->np.ipv6)
  239. return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6);
  240. else
  241. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip);
  242. }
  243. static ssize_t show_local_mac(struct netconsole_target *nt, char *buf)
  244. {
  245. struct net_device *dev = nt->np.dev;
  246. static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  247. return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);
  248. }
  249. static ssize_t show_remote_mac(struct netconsole_target *nt, char *buf)
  250. {
  251. return snprintf(buf, PAGE_SIZE, "%pM\n", nt->np.remote_mac);
  252. }
  253. /*
  254. * This one is special -- targets created through the configfs interface
  255. * are not enabled (and the corresponding netpoll activated) by default.
  256. * The user is expected to set the desired parameters first (which
  257. * would enable him to dynamically add new netpoll targets for new
  258. * network interfaces as and when they come up).
  259. */
  260. static ssize_t store_enabled(struct netconsole_target *nt,
  261. const char *buf,
  262. size_t count)
  263. {
  264. int enabled;
  265. int err;
  266. err = kstrtoint(buf, 10, &enabled);
  267. if (err < 0)
  268. return err;
  269. if (enabled < 0 || enabled > 1)
  270. return -EINVAL;
  271. if (enabled == nt->enabled) {
  272. printk(KERN_INFO "netconsole: network logging has already %s\n",
  273. nt->enabled ? "started" : "stopped");
  274. return -EINVAL;
  275. }
  276. if (enabled) { /* 1 */
  277. /*
  278. * Skip netpoll_parse_options() -- all the attributes are
  279. * already configured via configfs. Just print them out.
  280. */
  281. netpoll_print_options(&nt->np);
  282. err = netpoll_setup(&nt->np);
  283. if (err)
  284. return err;
  285. printk(KERN_INFO "netconsole: network logging started\n");
  286. } else { /* 0 */
  287. netpoll_cleanup(&nt->np);
  288. }
  289. nt->enabled = enabled;
  290. return strnlen(buf, count);
  291. }
  292. static ssize_t store_dev_name(struct netconsole_target *nt,
  293. const char *buf,
  294. size_t count)
  295. {
  296. size_t len;
  297. if (nt->enabled) {
  298. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  299. "disable to update parameters\n",
  300. config_item_name(&nt->item));
  301. return -EINVAL;
  302. }
  303. strlcpy(nt->np.dev_name, buf, IFNAMSIZ);
  304. /* Get rid of possible trailing newline from echo(1) */
  305. len = strnlen(nt->np.dev_name, IFNAMSIZ);
  306. if (nt->np.dev_name[len - 1] == '\n')
  307. nt->np.dev_name[len - 1] = '\0';
  308. return strnlen(buf, count);
  309. }
  310. static ssize_t store_local_port(struct netconsole_target *nt,
  311. const char *buf,
  312. size_t count)
  313. {
  314. int rv;
  315. if (nt->enabled) {
  316. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  317. "disable to update parameters\n",
  318. config_item_name(&nt->item));
  319. return -EINVAL;
  320. }
  321. rv = kstrtou16(buf, 10, &nt->np.local_port);
  322. if (rv < 0)
  323. return rv;
  324. return strnlen(buf, count);
  325. }
  326. static ssize_t store_remote_port(struct netconsole_target *nt,
  327. const char *buf,
  328. size_t count)
  329. {
  330. int rv;
  331. if (nt->enabled) {
  332. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  333. "disable to update parameters\n",
  334. config_item_name(&nt->item));
  335. return -EINVAL;
  336. }
  337. rv = kstrtou16(buf, 10, &nt->np.remote_port);
  338. if (rv < 0)
  339. return rv;
  340. return strnlen(buf, count);
  341. }
  342. static ssize_t store_local_ip(struct netconsole_target *nt,
  343. const char *buf,
  344. size_t count)
  345. {
  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. if (strnchr(buf, count, ':')) {
  353. const char *end;
  354. if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
  355. if (*end && *end != '\n') {
  356. printk(KERN_ERR "netconsole: invalid IPv6 address at: <%c>\n", *end);
  357. return -EINVAL;
  358. }
  359. nt->np.ipv6 = true;
  360. } else
  361. return -EINVAL;
  362. } else {
  363. if (!nt->np.ipv6) {
  364. nt->np.local_ip.ip = in_aton(buf);
  365. } else
  366. return -EINVAL;
  367. }
  368. return strnlen(buf, count);
  369. }
  370. static ssize_t store_remote_ip(struct netconsole_target *nt,
  371. const char *buf,
  372. size_t count)
  373. {
  374. if (nt->enabled) {
  375. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  376. "disable to update parameters\n",
  377. config_item_name(&nt->item));
  378. return -EINVAL;
  379. }
  380. if (strnchr(buf, count, ':')) {
  381. const char *end;
  382. if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
  383. if (*end && *end != '\n') {
  384. printk(KERN_ERR "netconsole: invalid IPv6 address at: <%c>\n", *end);
  385. return -EINVAL;
  386. }
  387. nt->np.ipv6 = true;
  388. } else
  389. return -EINVAL;
  390. } else {
  391. if (!nt->np.ipv6) {
  392. nt->np.remote_ip.ip = in_aton(buf);
  393. } else
  394. return -EINVAL;
  395. }
  396. return strnlen(buf, count);
  397. }
  398. static ssize_t store_remote_mac(struct netconsole_target *nt,
  399. const char *buf,
  400. size_t count)
  401. {
  402. u8 remote_mac[ETH_ALEN];
  403. if (nt->enabled) {
  404. printk(KERN_ERR "netconsole: target (%s) is enabled, "
  405. "disable to update parameters\n",
  406. config_item_name(&nt->item));
  407. return -EINVAL;
  408. }
  409. if (!mac_pton(buf, remote_mac))
  410. return -EINVAL;
  411. if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
  412. return -EINVAL;
  413. memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
  414. return strnlen(buf, count);
  415. }
  416. /*
  417. * Attribute definitions for netconsole_target.
  418. */
  419. #define NETCONSOLE_TARGET_ATTR_RO(_name) \
  420. static struct netconsole_target_attr netconsole_target_##_name = \
  421. __CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL)
  422. #define NETCONSOLE_TARGET_ATTR_RW(_name) \
  423. static struct netconsole_target_attr netconsole_target_##_name = \
  424. __CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
  425. NETCONSOLE_TARGET_ATTR_RW(enabled);
  426. NETCONSOLE_TARGET_ATTR_RW(dev_name);
  427. NETCONSOLE_TARGET_ATTR_RW(local_port);
  428. NETCONSOLE_TARGET_ATTR_RW(remote_port);
  429. NETCONSOLE_TARGET_ATTR_RW(local_ip);
  430. NETCONSOLE_TARGET_ATTR_RW(remote_ip);
  431. NETCONSOLE_TARGET_ATTR_RO(local_mac);
  432. NETCONSOLE_TARGET_ATTR_RW(remote_mac);
  433. static struct configfs_attribute *netconsole_target_attrs[] = {
  434. &netconsole_target_enabled.attr,
  435. &netconsole_target_dev_name.attr,
  436. &netconsole_target_local_port.attr,
  437. &netconsole_target_remote_port.attr,
  438. &netconsole_target_local_ip.attr,
  439. &netconsole_target_remote_ip.attr,
  440. &netconsole_target_local_mac.attr,
  441. &netconsole_target_remote_mac.attr,
  442. NULL,
  443. };
  444. /*
  445. * Item operations and type for netconsole_target.
  446. */
  447. static void netconsole_target_release(struct config_item *item)
  448. {
  449. kfree(to_target(item));
  450. }
  451. static ssize_t netconsole_target_attr_show(struct config_item *item,
  452. struct configfs_attribute *attr,
  453. char *buf)
  454. {
  455. ssize_t ret = -EINVAL;
  456. struct netconsole_target *nt = to_target(item);
  457. struct netconsole_target_attr *na =
  458. container_of(attr, struct netconsole_target_attr, attr);
  459. if (na->show)
  460. ret = na->show(nt, buf);
  461. return ret;
  462. }
  463. static ssize_t netconsole_target_attr_store(struct config_item *item,
  464. struct configfs_attribute *attr,
  465. const char *buf,
  466. size_t count)
  467. {
  468. ssize_t ret = -EINVAL;
  469. struct netconsole_target *nt = to_target(item);
  470. struct netconsole_target_attr *na =
  471. container_of(attr, struct netconsole_target_attr, attr);
  472. if (na->store)
  473. ret = na->store(nt, buf, count);
  474. return ret;
  475. }
  476. static struct configfs_item_operations netconsole_target_item_ops = {
  477. .release = netconsole_target_release,
  478. .show_attribute = netconsole_target_attr_show,
  479. .store_attribute = netconsole_target_attr_store,
  480. };
  481. static struct config_item_type netconsole_target_type = {
  482. .ct_attrs = netconsole_target_attrs,
  483. .ct_item_ops = &netconsole_target_item_ops,
  484. .ct_owner = THIS_MODULE,
  485. };
  486. /*
  487. * Group operations and type for netconsole_subsys.
  488. */
  489. static struct config_item *make_netconsole_target(struct config_group *group,
  490. const char *name)
  491. {
  492. unsigned long flags;
  493. struct netconsole_target *nt;
  494. /*
  495. * Allocate and initialize with defaults.
  496. * Target is disabled at creation (enabled == 0).
  497. */
  498. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  499. if (!nt)
  500. return ERR_PTR(-ENOMEM);
  501. nt->np.name = "netconsole";
  502. strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  503. nt->np.local_port = 6665;
  504. nt->np.remote_port = 6666;
  505. memset(nt->np.remote_mac, 0xff, ETH_ALEN);
  506. /* Initialize the config_item member */
  507. config_item_init_type_name(&nt->item, name, &netconsole_target_type);
  508. /* Adding, but it is disabled */
  509. spin_lock_irqsave(&target_list_lock, flags);
  510. list_add(&nt->list, &target_list);
  511. spin_unlock_irqrestore(&target_list_lock, flags);
  512. return &nt->item;
  513. }
  514. static void drop_netconsole_target(struct config_group *group,
  515. struct config_item *item)
  516. {
  517. unsigned long flags;
  518. struct netconsole_target *nt = to_target(item);
  519. spin_lock_irqsave(&target_list_lock, flags);
  520. list_del(&nt->list);
  521. spin_unlock_irqrestore(&target_list_lock, flags);
  522. /*
  523. * The target may have never been enabled, or was manually disabled
  524. * before being removed so netpoll may have already been cleaned up.
  525. */
  526. if (nt->enabled)
  527. netpoll_cleanup(&nt->np);
  528. config_item_put(&nt->item);
  529. }
  530. static struct configfs_group_operations netconsole_subsys_group_ops = {
  531. .make_item = make_netconsole_target,
  532. .drop_item = drop_netconsole_target,
  533. };
  534. static struct config_item_type netconsole_subsys_type = {
  535. .ct_group_ops = &netconsole_subsys_group_ops,
  536. .ct_owner = THIS_MODULE,
  537. };
  538. /* The netconsole configfs subsystem */
  539. static struct configfs_subsystem netconsole_subsys = {
  540. .su_group = {
  541. .cg_item = {
  542. .ci_namebuf = "netconsole",
  543. .ci_type = &netconsole_subsys_type,
  544. },
  545. },
  546. };
  547. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  548. /* Handle network interface device notifications */
  549. static int netconsole_netdev_event(struct notifier_block *this,
  550. unsigned long event,
  551. void *ptr)
  552. {
  553. unsigned long flags;
  554. struct netconsole_target *nt;
  555. struct net_device *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);