netconsole.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. unsigned long flags;
  269. int enabled;
  270. int err;
  271. err = kstrtoint(buf, 10, &enabled);
  272. if (err < 0)
  273. return err;
  274. if (enabled < 0 || enabled > 1)
  275. return -EINVAL;
  276. if (enabled == nt->enabled) {
  277. pr_info("network logging has already %s\n",
  278. nt->enabled ? "started" : "stopped");
  279. return -EINVAL;
  280. }
  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. return err;
  290. pr_info("netconsole: network logging started\n");
  291. } else { /* 0 */
  292. /* We need to disable the netconsole before cleaning it up
  293. * otherwise we might end up in write_msg() with
  294. * nt->np.dev == NULL and nt->enabled == 1
  295. */
  296. spin_lock_irqsave(&target_list_lock, flags);
  297. nt->enabled = 0;
  298. spin_unlock_irqrestore(&target_list_lock, flags);
  299. netpoll_cleanup(&nt->np);
  300. }
  301. nt->enabled = enabled;
  302. return strnlen(buf, count);
  303. }
  304. static ssize_t store_dev_name(struct netconsole_target *nt,
  305. const char *buf,
  306. size_t count)
  307. {
  308. size_t len;
  309. if (nt->enabled) {
  310. pr_err("target (%s) is enabled, disable to update parameters\n",
  311. config_item_name(&nt->item));
  312. return -EINVAL;
  313. }
  314. strlcpy(nt->np.dev_name, buf, IFNAMSIZ);
  315. /* Get rid of possible trailing newline from echo(1) */
  316. len = strnlen(nt->np.dev_name, IFNAMSIZ);
  317. if (nt->np.dev_name[len - 1] == '\n')
  318. nt->np.dev_name[len - 1] = '\0';
  319. return strnlen(buf, count);
  320. }
  321. static ssize_t store_local_port(struct netconsole_target *nt,
  322. const char *buf,
  323. size_t count)
  324. {
  325. int rv;
  326. if (nt->enabled) {
  327. pr_err("target (%s) is enabled, disable to update parameters\n",
  328. config_item_name(&nt->item));
  329. return -EINVAL;
  330. }
  331. rv = kstrtou16(buf, 10, &nt->np.local_port);
  332. if (rv < 0)
  333. return rv;
  334. return strnlen(buf, count);
  335. }
  336. static ssize_t store_remote_port(struct netconsole_target *nt,
  337. const char *buf,
  338. size_t count)
  339. {
  340. int rv;
  341. if (nt->enabled) {
  342. pr_err("target (%s) is enabled, disable to update parameters\n",
  343. config_item_name(&nt->item));
  344. return -EINVAL;
  345. }
  346. rv = kstrtou16(buf, 10, &nt->np.remote_port);
  347. if (rv < 0)
  348. return rv;
  349. return strnlen(buf, count);
  350. }
  351. static ssize_t store_local_ip(struct netconsole_target *nt,
  352. const char *buf,
  353. size_t count)
  354. {
  355. if (nt->enabled) {
  356. pr_err("target (%s) is enabled, disable to update parameters\n",
  357. config_item_name(&nt->item));
  358. return -EINVAL;
  359. }
  360. if (strnchr(buf, count, ':')) {
  361. const char *end;
  362. if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
  363. if (*end && *end != '\n') {
  364. pr_err("invalid IPv6 address at: <%c>\n", *end);
  365. return -EINVAL;
  366. }
  367. nt->np.ipv6 = true;
  368. } else
  369. return -EINVAL;
  370. } else {
  371. if (!nt->np.ipv6) {
  372. nt->np.local_ip.ip = in_aton(buf);
  373. } else
  374. return -EINVAL;
  375. }
  376. return strnlen(buf, count);
  377. }
  378. static ssize_t store_remote_ip(struct netconsole_target *nt,
  379. const char *buf,
  380. size_t count)
  381. {
  382. if (nt->enabled) {
  383. pr_err("target (%s) is enabled, 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. pr_err("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. pr_err("target (%s) is enabled, disable to update parameters\n",
  412. config_item_name(&nt->item));
  413. return -EINVAL;
  414. }
  415. if (!mac_pton(buf, remote_mac))
  416. return -EINVAL;
  417. if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
  418. return -EINVAL;
  419. memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
  420. return strnlen(buf, count);
  421. }
  422. /*
  423. * Attribute definitions for netconsole_target.
  424. */
  425. #define NETCONSOLE_TARGET_ATTR_RO(_name) \
  426. static struct netconsole_target_attr netconsole_target_##_name = \
  427. __CONFIGFS_ATTR(_name, S_IRUGO, show_##_name, NULL)
  428. #define NETCONSOLE_TARGET_ATTR_RW(_name) \
  429. static struct netconsole_target_attr netconsole_target_##_name = \
  430. __CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, show_##_name, store_##_name)
  431. NETCONSOLE_TARGET_ATTR_RW(enabled);
  432. NETCONSOLE_TARGET_ATTR_RW(dev_name);
  433. NETCONSOLE_TARGET_ATTR_RW(local_port);
  434. NETCONSOLE_TARGET_ATTR_RW(remote_port);
  435. NETCONSOLE_TARGET_ATTR_RW(local_ip);
  436. NETCONSOLE_TARGET_ATTR_RW(remote_ip);
  437. NETCONSOLE_TARGET_ATTR_RO(local_mac);
  438. NETCONSOLE_TARGET_ATTR_RW(remote_mac);
  439. static struct configfs_attribute *netconsole_target_attrs[] = {
  440. &netconsole_target_enabled.attr,
  441. &netconsole_target_dev_name.attr,
  442. &netconsole_target_local_port.attr,
  443. &netconsole_target_remote_port.attr,
  444. &netconsole_target_local_ip.attr,
  445. &netconsole_target_remote_ip.attr,
  446. &netconsole_target_local_mac.attr,
  447. &netconsole_target_remote_mac.attr,
  448. NULL,
  449. };
  450. /*
  451. * Item operations and type for netconsole_target.
  452. */
  453. static void netconsole_target_release(struct config_item *item)
  454. {
  455. kfree(to_target(item));
  456. }
  457. static ssize_t netconsole_target_attr_show(struct config_item *item,
  458. struct configfs_attribute *attr,
  459. char *buf)
  460. {
  461. ssize_t ret = -EINVAL;
  462. struct netconsole_target *nt = to_target(item);
  463. struct netconsole_target_attr *na =
  464. container_of(attr, struct netconsole_target_attr, attr);
  465. if (na->show)
  466. ret = na->show(nt, buf);
  467. return ret;
  468. }
  469. static ssize_t netconsole_target_attr_store(struct config_item *item,
  470. struct configfs_attribute *attr,
  471. const char *buf,
  472. size_t count)
  473. {
  474. ssize_t ret = -EINVAL;
  475. struct netconsole_target *nt = to_target(item);
  476. struct netconsole_target_attr *na =
  477. container_of(attr, struct netconsole_target_attr, attr);
  478. mutex_lock(&nt->mutex);
  479. if (na->store)
  480. ret = na->store(nt, buf, count);
  481. mutex_unlock(&nt->mutex);
  482. return ret;
  483. }
  484. static struct configfs_item_operations netconsole_target_item_ops = {
  485. .release = netconsole_target_release,
  486. .show_attribute = netconsole_target_attr_show,
  487. .store_attribute = netconsole_target_attr_store,
  488. };
  489. static struct config_item_type netconsole_target_type = {
  490. .ct_attrs = netconsole_target_attrs,
  491. .ct_item_ops = &netconsole_target_item_ops,
  492. .ct_owner = THIS_MODULE,
  493. };
  494. /*
  495. * Group operations and type for netconsole_subsys.
  496. */
  497. static struct config_item *make_netconsole_target(struct config_group *group,
  498. const char *name)
  499. {
  500. unsigned long flags;
  501. struct netconsole_target *nt;
  502. /*
  503. * Allocate and initialize with defaults.
  504. * Target is disabled at creation (enabled == 0).
  505. */
  506. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  507. if (!nt)
  508. return ERR_PTR(-ENOMEM);
  509. nt->np.name = "netconsole";
  510. strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  511. nt->np.local_port = 6665;
  512. nt->np.remote_port = 6666;
  513. mutex_init(&nt->mutex);
  514. memset(nt->np.remote_mac, 0xff, ETH_ALEN);
  515. /* Initialize the config_item member */
  516. config_item_init_type_name(&nt->item, name, &netconsole_target_type);
  517. /* Adding, but it is disabled */
  518. spin_lock_irqsave(&target_list_lock, flags);
  519. list_add(&nt->list, &target_list);
  520. spin_unlock_irqrestore(&target_list_lock, flags);
  521. return &nt->item;
  522. }
  523. static void drop_netconsole_target(struct config_group *group,
  524. struct config_item *item)
  525. {
  526. unsigned long flags;
  527. struct netconsole_target *nt = to_target(item);
  528. spin_lock_irqsave(&target_list_lock, flags);
  529. list_del(&nt->list);
  530. spin_unlock_irqrestore(&target_list_lock, flags);
  531. /*
  532. * The target may have never been enabled, or was manually disabled
  533. * before being removed so netpoll may have already been cleaned up.
  534. */
  535. if (nt->enabled)
  536. netpoll_cleanup(&nt->np);
  537. config_item_put(&nt->item);
  538. }
  539. static struct configfs_group_operations netconsole_subsys_group_ops = {
  540. .make_item = make_netconsole_target,
  541. .drop_item = drop_netconsole_target,
  542. };
  543. static struct config_item_type netconsole_subsys_type = {
  544. .ct_group_ops = &netconsole_subsys_group_ops,
  545. .ct_owner = THIS_MODULE,
  546. };
  547. /* The netconsole configfs subsystem */
  548. static struct configfs_subsystem netconsole_subsys = {
  549. .su_group = {
  550. .cg_item = {
  551. .ci_namebuf = "netconsole",
  552. .ci_type = &netconsole_subsys_type,
  553. },
  554. },
  555. };
  556. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  557. /* Handle network interface device notifications */
  558. static int netconsole_netdev_event(struct notifier_block *this,
  559. unsigned long event, void *ptr)
  560. {
  561. unsigned long flags;
  562. struct netconsole_target *nt;
  563. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  564. bool stopped = false;
  565. if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
  566. event == NETDEV_RELEASE || event == NETDEV_JOIN))
  567. goto done;
  568. spin_lock_irqsave(&target_list_lock, flags);
  569. restart:
  570. list_for_each_entry(nt, &target_list, list) {
  571. netconsole_target_get(nt);
  572. if (nt->np.dev == dev) {
  573. switch (event) {
  574. case NETDEV_CHANGENAME:
  575. strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  576. break;
  577. case NETDEV_RELEASE:
  578. case NETDEV_JOIN:
  579. case NETDEV_UNREGISTER:
  580. /* rtnl_lock already held
  581. * we might sleep in __netpoll_cleanup()
  582. */
  583. spin_unlock_irqrestore(&target_list_lock, flags);
  584. __netpoll_cleanup(&nt->np);
  585. spin_lock_irqsave(&target_list_lock, flags);
  586. dev_put(nt->np.dev);
  587. nt->np.dev = NULL;
  588. nt->enabled = 0;
  589. stopped = true;
  590. netconsole_target_put(nt);
  591. goto restart;
  592. }
  593. }
  594. netconsole_target_put(nt);
  595. }
  596. spin_unlock_irqrestore(&target_list_lock, flags);
  597. if (stopped) {
  598. const char *msg = "had an event";
  599. switch (event) {
  600. case NETDEV_UNREGISTER:
  601. msg = "unregistered";
  602. break;
  603. case NETDEV_RELEASE:
  604. msg = "released slaves";
  605. break;
  606. case NETDEV_JOIN:
  607. msg = "is joining a master device";
  608. break;
  609. }
  610. pr_info("network logging stopped on interface %s as it %s\n",
  611. dev->name, msg);
  612. }
  613. done:
  614. return NOTIFY_DONE;
  615. }
  616. static struct notifier_block netconsole_netdev_notifier = {
  617. .notifier_call = netconsole_netdev_event,
  618. };
  619. static void write_msg(struct console *con, const char *msg, unsigned int len)
  620. {
  621. int frag, left;
  622. unsigned long flags;
  623. struct netconsole_target *nt;
  624. const char *tmp;
  625. if (oops_only && !oops_in_progress)
  626. return;
  627. /* Avoid taking lock and disabling interrupts unnecessarily */
  628. if (list_empty(&target_list))
  629. return;
  630. spin_lock_irqsave(&target_list_lock, flags);
  631. list_for_each_entry(nt, &target_list, list) {
  632. netconsole_target_get(nt);
  633. if (nt->enabled && netif_running(nt->np.dev)) {
  634. /*
  635. * We nest this inside the for-each-target loop above
  636. * so that we're able to get as much logging out to
  637. * at least one target if we die inside here, instead
  638. * of unnecessarily keeping all targets in lock-step.
  639. */
  640. tmp = msg;
  641. for (left = len; left;) {
  642. frag = min(left, MAX_PRINT_CHUNK);
  643. netpoll_send_udp(&nt->np, tmp, frag);
  644. tmp += frag;
  645. left -= frag;
  646. }
  647. }
  648. netconsole_target_put(nt);
  649. }
  650. spin_unlock_irqrestore(&target_list_lock, flags);
  651. }
  652. static struct console netconsole = {
  653. .name = "netcon",
  654. .flags = CON_ENABLED,
  655. .write = write_msg,
  656. };
  657. static int __init init_netconsole(void)
  658. {
  659. int err;
  660. struct netconsole_target *nt, *tmp;
  661. unsigned long flags;
  662. char *target_config;
  663. char *input = config;
  664. if (strnlen(input, MAX_PARAM_LENGTH)) {
  665. while ((target_config = strsep(&input, ";"))) {
  666. nt = alloc_param_target(target_config);
  667. if (IS_ERR(nt)) {
  668. err = PTR_ERR(nt);
  669. goto fail;
  670. }
  671. /* Dump existing printks when we register */
  672. netconsole.flags |= CON_PRINTBUFFER;
  673. spin_lock_irqsave(&target_list_lock, flags);
  674. list_add(&nt->list, &target_list);
  675. spin_unlock_irqrestore(&target_list_lock, flags);
  676. }
  677. }
  678. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  679. if (err)
  680. goto fail;
  681. err = dynamic_netconsole_init();
  682. if (err)
  683. goto undonotifier;
  684. register_console(&netconsole);
  685. pr_info("network logging started\n");
  686. return err;
  687. undonotifier:
  688. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  689. fail:
  690. pr_err("cleaning up\n");
  691. /*
  692. * Remove all targets and destroy them (only targets created
  693. * from the boot/module option exist here). Skipping the list
  694. * lock is safe here, and netpoll_cleanup() will sleep.
  695. */
  696. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  697. list_del(&nt->list);
  698. free_param_target(nt);
  699. }
  700. return err;
  701. }
  702. static void __exit cleanup_netconsole(void)
  703. {
  704. struct netconsole_target *nt, *tmp;
  705. unregister_console(&netconsole);
  706. dynamic_netconsole_exit();
  707. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  708. /*
  709. * Targets created via configfs pin references on our module
  710. * and would first be rmdir(2)'ed from userspace. We reach
  711. * here only when they are already destroyed, and only those
  712. * created from the boot/module option are left, so remove and
  713. * destroy them. Skipping the list lock is safe here, and
  714. * netpoll_cleanup() will sleep.
  715. */
  716. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  717. list_del(&nt->list);
  718. free_param_target(nt);
  719. }
  720. }
  721. /*
  722. * Use late_initcall to ensure netconsole is
  723. * initialized after network device driver if built-in.
  724. *
  725. * late_initcall() and module_init() are identical if built as module.
  726. */
  727. late_initcall(init_netconsole);
  728. module_exit(cleanup_netconsole);