netconsole.c 22 KB

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