netconsole.c 21 KB

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