netconsole.c 25 KB

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