netconsole.c 21 KB

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