netconsole.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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. static struct netconsole_target *to_target(struct config_item *item)
  211. {
  212. return item ?
  213. container_of(item, struct netconsole_target, item) :
  214. NULL;
  215. }
  216. /*
  217. * Attribute operations for netconsole_target.
  218. */
  219. static ssize_t enabled_show(struct config_item *item, char *buf)
  220. {
  221. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->enabled);
  222. }
  223. static ssize_t extended_show(struct config_item *item, char *buf)
  224. {
  225. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->extended);
  226. }
  227. static ssize_t dev_name_show(struct config_item *item, char *buf)
  228. {
  229. return snprintf(buf, PAGE_SIZE, "%s\n", to_target(item)->np.dev_name);
  230. }
  231. static ssize_t local_port_show(struct config_item *item, char *buf)
  232. {
  233. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.local_port);
  234. }
  235. static ssize_t remote_port_show(struct config_item *item, char *buf)
  236. {
  237. return snprintf(buf, PAGE_SIZE, "%d\n", to_target(item)->np.remote_port);
  238. }
  239. static ssize_t local_ip_show(struct config_item *item, char *buf)
  240. {
  241. struct netconsole_target *nt = to_target(item);
  242. if (nt->np.ipv6)
  243. return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.local_ip.in6);
  244. else
  245. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.local_ip);
  246. }
  247. static ssize_t remote_ip_show(struct config_item *item, char *buf)
  248. {
  249. struct netconsole_target *nt = to_target(item);
  250. if (nt->np.ipv6)
  251. return snprintf(buf, PAGE_SIZE, "%pI6c\n", &nt->np.remote_ip.in6);
  252. else
  253. return snprintf(buf, PAGE_SIZE, "%pI4\n", &nt->np.remote_ip);
  254. }
  255. static ssize_t local_mac_show(struct config_item *item, char *buf)
  256. {
  257. struct net_device *dev = to_target(item)->np.dev;
  258. static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  259. return snprintf(buf, PAGE_SIZE, "%pM\n", dev ? dev->dev_addr : bcast);
  260. }
  261. static ssize_t remote_mac_show(struct config_item *item, char *buf)
  262. {
  263. return snprintf(buf, PAGE_SIZE, "%pM\n", to_target(item)->np.remote_mac);
  264. }
  265. /*
  266. * This one is special -- targets created through the configfs interface
  267. * are not enabled (and the corresponding netpoll activated) by default.
  268. * The user is expected to set the desired parameters first (which
  269. * would enable him to dynamically add new netpoll targets for new
  270. * network interfaces as and when they come up).
  271. */
  272. static ssize_t enabled_store(struct config_item *item,
  273. const char *buf, size_t count)
  274. {
  275. struct netconsole_target *nt = to_target(item);
  276. unsigned long flags;
  277. int enabled;
  278. int err;
  279. mutex_lock(&dynamic_netconsole_mutex);
  280. err = kstrtoint(buf, 10, &enabled);
  281. if (err < 0)
  282. goto out_unlock;
  283. err = -EINVAL;
  284. if (enabled < 0 || enabled > 1)
  285. goto out_unlock;
  286. if ((bool)enabled == nt->enabled) {
  287. pr_info("network logging has already %s\n",
  288. nt->enabled ? "started" : "stopped");
  289. goto out_unlock;
  290. }
  291. if (enabled) { /* true */
  292. if (nt->extended && !(netconsole_ext.flags & CON_ENABLED)) {
  293. netconsole_ext.flags |= CON_ENABLED;
  294. register_console(&netconsole_ext);
  295. }
  296. /*
  297. * Skip netpoll_parse_options() -- all the attributes are
  298. * already configured via configfs. Just print them out.
  299. */
  300. netpoll_print_options(&nt->np);
  301. err = netpoll_setup(&nt->np);
  302. if (err)
  303. goto out_unlock;
  304. pr_info("netconsole: network logging started\n");
  305. } else { /* false */
  306. /* We need to disable the netconsole before cleaning it up
  307. * otherwise we might end up in write_msg() with
  308. * nt->np.dev == NULL and nt->enabled == true
  309. */
  310. spin_lock_irqsave(&target_list_lock, flags);
  311. nt->enabled = false;
  312. spin_unlock_irqrestore(&target_list_lock, flags);
  313. netpoll_cleanup(&nt->np);
  314. }
  315. nt->enabled = enabled;
  316. mutex_unlock(&dynamic_netconsole_mutex);
  317. return strnlen(buf, count);
  318. out_unlock:
  319. mutex_unlock(&dynamic_netconsole_mutex);
  320. return err;
  321. }
  322. static ssize_t extended_store(struct config_item *item, const char *buf,
  323. size_t count)
  324. {
  325. struct netconsole_target *nt = to_target(item);
  326. int extended;
  327. int err;
  328. mutex_lock(&dynamic_netconsole_mutex);
  329. if (nt->enabled) {
  330. pr_err("target (%s) is enabled, disable to update parameters\n",
  331. config_item_name(&nt->item));
  332. err = -EINVAL;
  333. goto out_unlock;
  334. }
  335. err = kstrtoint(buf, 10, &extended);
  336. if (err < 0)
  337. goto out_unlock;
  338. if (extended < 0 || extended > 1) {
  339. err = -EINVAL;
  340. goto out_unlock;
  341. }
  342. nt->extended = extended;
  343. mutex_unlock(&dynamic_netconsole_mutex);
  344. return strnlen(buf, count);
  345. out_unlock:
  346. mutex_unlock(&dynamic_netconsole_mutex);
  347. return err;
  348. }
  349. static ssize_t dev_name_store(struct config_item *item, const char *buf,
  350. size_t count)
  351. {
  352. struct netconsole_target *nt = to_target(item);
  353. size_t len;
  354. mutex_lock(&dynamic_netconsole_mutex);
  355. if (nt->enabled) {
  356. pr_err("target (%s) is enabled, disable to update parameters\n",
  357. config_item_name(&nt->item));
  358. mutex_unlock(&dynamic_netconsole_mutex);
  359. return -EINVAL;
  360. }
  361. strlcpy(nt->np.dev_name, buf, IFNAMSIZ);
  362. /* Get rid of possible trailing newline from echo(1) */
  363. len = strnlen(nt->np.dev_name, IFNAMSIZ);
  364. if (nt->np.dev_name[len - 1] == '\n')
  365. nt->np.dev_name[len - 1] = '\0';
  366. mutex_unlock(&dynamic_netconsole_mutex);
  367. return strnlen(buf, count);
  368. }
  369. static ssize_t local_port_store(struct config_item *item, const char *buf,
  370. size_t count)
  371. {
  372. struct netconsole_target *nt = to_target(item);
  373. int rv = -EINVAL;
  374. mutex_lock(&dynamic_netconsole_mutex);
  375. if (nt->enabled) {
  376. pr_err("target (%s) is enabled, disable to update parameters\n",
  377. config_item_name(&nt->item));
  378. goto out_unlock;
  379. }
  380. rv = kstrtou16(buf, 10, &nt->np.local_port);
  381. if (rv < 0)
  382. goto out_unlock;
  383. mutex_unlock(&dynamic_netconsole_mutex);
  384. return strnlen(buf, count);
  385. out_unlock:
  386. mutex_unlock(&dynamic_netconsole_mutex);
  387. return rv;
  388. }
  389. static ssize_t remote_port_store(struct config_item *item,
  390. const char *buf, size_t count)
  391. {
  392. struct netconsole_target *nt = to_target(item);
  393. int rv = -EINVAL;
  394. mutex_lock(&dynamic_netconsole_mutex);
  395. if (nt->enabled) {
  396. pr_err("target (%s) is enabled, disable to update parameters\n",
  397. config_item_name(&nt->item));
  398. goto out_unlock;
  399. }
  400. rv = kstrtou16(buf, 10, &nt->np.remote_port);
  401. if (rv < 0)
  402. goto out_unlock;
  403. mutex_unlock(&dynamic_netconsole_mutex);
  404. return strnlen(buf, count);
  405. out_unlock:
  406. mutex_unlock(&dynamic_netconsole_mutex);
  407. return rv;
  408. }
  409. static ssize_t local_ip_store(struct config_item *item, const char *buf,
  410. size_t count)
  411. {
  412. struct netconsole_target *nt = to_target(item);
  413. mutex_lock(&dynamic_netconsole_mutex);
  414. if (nt->enabled) {
  415. pr_err("target (%s) is enabled, disable to update parameters\n",
  416. config_item_name(&nt->item));
  417. goto out_unlock;
  418. }
  419. if (strnchr(buf, count, ':')) {
  420. const char *end;
  421. if (in6_pton(buf, count, nt->np.local_ip.in6.s6_addr, -1, &end) > 0) {
  422. if (*end && *end != '\n') {
  423. pr_err("invalid IPv6 address at: <%c>\n", *end);
  424. goto out_unlock;
  425. }
  426. nt->np.ipv6 = true;
  427. } else
  428. goto out_unlock;
  429. } else {
  430. if (!nt->np.ipv6) {
  431. nt->np.local_ip.ip = in_aton(buf);
  432. } else
  433. goto out_unlock;
  434. }
  435. mutex_unlock(&dynamic_netconsole_mutex);
  436. return strnlen(buf, count);
  437. out_unlock:
  438. mutex_unlock(&dynamic_netconsole_mutex);
  439. return -EINVAL;
  440. }
  441. static ssize_t remote_ip_store(struct config_item *item, const char *buf,
  442. size_t count)
  443. {
  444. struct netconsole_target *nt = to_target(item);
  445. mutex_lock(&dynamic_netconsole_mutex);
  446. if (nt->enabled) {
  447. pr_err("target (%s) is enabled, disable to update parameters\n",
  448. config_item_name(&nt->item));
  449. goto out_unlock;
  450. }
  451. if (strnchr(buf, count, ':')) {
  452. const char *end;
  453. if (in6_pton(buf, count, nt->np.remote_ip.in6.s6_addr, -1, &end) > 0) {
  454. if (*end && *end != '\n') {
  455. pr_err("invalid IPv6 address at: <%c>\n", *end);
  456. goto out_unlock;
  457. }
  458. nt->np.ipv6 = true;
  459. } else
  460. goto out_unlock;
  461. } else {
  462. if (!nt->np.ipv6) {
  463. nt->np.remote_ip.ip = in_aton(buf);
  464. } else
  465. goto out_unlock;
  466. }
  467. mutex_unlock(&dynamic_netconsole_mutex);
  468. return strnlen(buf, count);
  469. out_unlock:
  470. mutex_unlock(&dynamic_netconsole_mutex);
  471. return -EINVAL;
  472. }
  473. static ssize_t remote_mac_store(struct config_item *item, const char *buf,
  474. size_t count)
  475. {
  476. struct netconsole_target *nt = to_target(item);
  477. u8 remote_mac[ETH_ALEN];
  478. mutex_lock(&dynamic_netconsole_mutex);
  479. if (nt->enabled) {
  480. pr_err("target (%s) is enabled, disable to update parameters\n",
  481. config_item_name(&nt->item));
  482. goto out_unlock;
  483. }
  484. if (!mac_pton(buf, remote_mac))
  485. goto out_unlock;
  486. if (buf[3 * ETH_ALEN - 1] && buf[3 * ETH_ALEN - 1] != '\n')
  487. goto out_unlock;
  488. memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
  489. mutex_unlock(&dynamic_netconsole_mutex);
  490. return strnlen(buf, count);
  491. out_unlock:
  492. mutex_unlock(&dynamic_netconsole_mutex);
  493. return -EINVAL;
  494. }
  495. CONFIGFS_ATTR(, enabled);
  496. CONFIGFS_ATTR(, extended);
  497. CONFIGFS_ATTR(, dev_name);
  498. CONFIGFS_ATTR(, local_port);
  499. CONFIGFS_ATTR(, remote_port);
  500. CONFIGFS_ATTR(, local_ip);
  501. CONFIGFS_ATTR(, remote_ip);
  502. CONFIGFS_ATTR_RO(, local_mac);
  503. CONFIGFS_ATTR(, remote_mac);
  504. static struct configfs_attribute *netconsole_target_attrs[] = {
  505. &attr_enabled,
  506. &attr_extended,
  507. &attr_dev_name,
  508. &attr_local_port,
  509. &attr_remote_port,
  510. &attr_local_ip,
  511. &attr_remote_ip,
  512. &attr_local_mac,
  513. &attr_remote_mac,
  514. NULL,
  515. };
  516. /*
  517. * Item operations and type for netconsole_target.
  518. */
  519. static void netconsole_target_release(struct config_item *item)
  520. {
  521. kfree(to_target(item));
  522. }
  523. static struct configfs_item_operations netconsole_target_item_ops = {
  524. .release = netconsole_target_release,
  525. };
  526. static struct config_item_type netconsole_target_type = {
  527. .ct_attrs = netconsole_target_attrs,
  528. .ct_item_ops = &netconsole_target_item_ops,
  529. .ct_owner = THIS_MODULE,
  530. };
  531. /*
  532. * Group operations and type for netconsole_subsys.
  533. */
  534. static struct config_item *make_netconsole_target(struct config_group *group,
  535. const char *name)
  536. {
  537. unsigned long flags;
  538. struct netconsole_target *nt;
  539. /*
  540. * Allocate and initialize with defaults.
  541. * Target is disabled at creation (!enabled).
  542. */
  543. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  544. if (!nt)
  545. return ERR_PTR(-ENOMEM);
  546. nt->np.name = "netconsole";
  547. strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  548. nt->np.local_port = 6665;
  549. nt->np.remote_port = 6666;
  550. eth_broadcast_addr(nt->np.remote_mac);
  551. /* Initialize the config_item member */
  552. config_item_init_type_name(&nt->item, name, &netconsole_target_type);
  553. /* Adding, but it is disabled */
  554. spin_lock_irqsave(&target_list_lock, flags);
  555. list_add(&nt->list, &target_list);
  556. spin_unlock_irqrestore(&target_list_lock, flags);
  557. return &nt->item;
  558. }
  559. static void drop_netconsole_target(struct config_group *group,
  560. struct config_item *item)
  561. {
  562. unsigned long flags;
  563. struct netconsole_target *nt = to_target(item);
  564. spin_lock_irqsave(&target_list_lock, flags);
  565. list_del(&nt->list);
  566. spin_unlock_irqrestore(&target_list_lock, flags);
  567. /*
  568. * The target may have never been enabled, or was manually disabled
  569. * before being removed so netpoll may have already been cleaned up.
  570. */
  571. if (nt->enabled)
  572. netpoll_cleanup(&nt->np);
  573. config_item_put(&nt->item);
  574. }
  575. static struct configfs_group_operations netconsole_subsys_group_ops = {
  576. .make_item = make_netconsole_target,
  577. .drop_item = drop_netconsole_target,
  578. };
  579. static struct config_item_type netconsole_subsys_type = {
  580. .ct_group_ops = &netconsole_subsys_group_ops,
  581. .ct_owner = THIS_MODULE,
  582. };
  583. /* The netconsole configfs subsystem */
  584. static struct configfs_subsystem netconsole_subsys = {
  585. .su_group = {
  586. .cg_item = {
  587. .ci_namebuf = "netconsole",
  588. .ci_type = &netconsole_subsys_type,
  589. },
  590. },
  591. };
  592. #endif /* CONFIG_NETCONSOLE_DYNAMIC */
  593. /* Handle network interface device notifications */
  594. static int netconsole_netdev_event(struct notifier_block *this,
  595. unsigned long event, void *ptr)
  596. {
  597. unsigned long flags;
  598. struct netconsole_target *nt;
  599. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  600. bool stopped = false;
  601. if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
  602. event == NETDEV_RELEASE || event == NETDEV_JOIN))
  603. goto done;
  604. spin_lock_irqsave(&target_list_lock, flags);
  605. restart:
  606. list_for_each_entry(nt, &target_list, list) {
  607. netconsole_target_get(nt);
  608. if (nt->np.dev == dev) {
  609. switch (event) {
  610. case NETDEV_CHANGENAME:
  611. strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  612. break;
  613. case NETDEV_RELEASE:
  614. case NETDEV_JOIN:
  615. case NETDEV_UNREGISTER:
  616. /* rtnl_lock already held
  617. * we might sleep in __netpoll_cleanup()
  618. */
  619. spin_unlock_irqrestore(&target_list_lock, flags);
  620. __netpoll_cleanup(&nt->np);
  621. spin_lock_irqsave(&target_list_lock, flags);
  622. dev_put(nt->np.dev);
  623. nt->np.dev = NULL;
  624. nt->enabled = false;
  625. stopped = true;
  626. netconsole_target_put(nt);
  627. goto restart;
  628. }
  629. }
  630. netconsole_target_put(nt);
  631. }
  632. spin_unlock_irqrestore(&target_list_lock, flags);
  633. if (stopped) {
  634. const char *msg = "had an event";
  635. switch (event) {
  636. case NETDEV_UNREGISTER:
  637. msg = "unregistered";
  638. break;
  639. case NETDEV_RELEASE:
  640. msg = "released slaves";
  641. break;
  642. case NETDEV_JOIN:
  643. msg = "is joining a master device";
  644. break;
  645. }
  646. pr_info("network logging stopped on interface %s as it %s\n",
  647. dev->name, msg);
  648. }
  649. done:
  650. return NOTIFY_DONE;
  651. }
  652. static struct notifier_block netconsole_netdev_notifier = {
  653. .notifier_call = netconsole_netdev_event,
  654. };
  655. /**
  656. * send_ext_msg_udp - send extended log message to target
  657. * @nt: target to send message to
  658. * @msg: extended log message to send
  659. * @msg_len: length of message
  660. *
  661. * Transfer extended log @msg to @nt. If @msg is longer than
  662. * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
  663. * ncfrag header field added to identify them.
  664. */
  665. static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
  666. int msg_len)
  667. {
  668. static char buf[MAX_PRINT_CHUNK]; /* protected by target_list_lock */
  669. const char *header, *body;
  670. int offset = 0;
  671. int header_len, body_len;
  672. if (msg_len <= MAX_PRINT_CHUNK) {
  673. netpoll_send_udp(&nt->np, msg, msg_len);
  674. return;
  675. }
  676. /* need to insert extra header fields, detect header and body */
  677. header = msg;
  678. body = memchr(msg, ';', msg_len);
  679. if (WARN_ON_ONCE(!body))
  680. return;
  681. header_len = body - header;
  682. body_len = msg_len - header_len - 1;
  683. body++;
  684. /*
  685. * Transfer multiple chunks with the following extra header.
  686. * "ncfrag=<byte-offset>/<total-bytes>"
  687. */
  688. memcpy(buf, header, header_len);
  689. while (offset < body_len) {
  690. int this_header = header_len;
  691. int this_chunk;
  692. this_header += scnprintf(buf + this_header,
  693. sizeof(buf) - this_header,
  694. ",ncfrag=%d/%d;", offset, body_len);
  695. this_chunk = min(body_len - offset,
  696. MAX_PRINT_CHUNK - this_header);
  697. if (WARN_ON_ONCE(this_chunk <= 0))
  698. return;
  699. memcpy(buf + this_header, body + offset, this_chunk);
  700. netpoll_send_udp(&nt->np, buf, this_header + this_chunk);
  701. offset += this_chunk;
  702. }
  703. }
  704. static void write_ext_msg(struct console *con, const char *msg,
  705. unsigned int len)
  706. {
  707. struct netconsole_target *nt;
  708. unsigned long flags;
  709. if ((oops_only && !oops_in_progress) || list_empty(&target_list))
  710. return;
  711. spin_lock_irqsave(&target_list_lock, flags);
  712. list_for_each_entry(nt, &target_list, list)
  713. if (nt->extended && nt->enabled && netif_running(nt->np.dev))
  714. send_ext_msg_udp(nt, msg, len);
  715. spin_unlock_irqrestore(&target_list_lock, flags);
  716. }
  717. static void write_msg(struct console *con, const char *msg, unsigned int len)
  718. {
  719. int frag, left;
  720. unsigned long flags;
  721. struct netconsole_target *nt;
  722. const char *tmp;
  723. if (oops_only && !oops_in_progress)
  724. return;
  725. /* Avoid taking lock and disabling interrupts unnecessarily */
  726. if (list_empty(&target_list))
  727. return;
  728. spin_lock_irqsave(&target_list_lock, flags);
  729. list_for_each_entry(nt, &target_list, list) {
  730. if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
  731. /*
  732. * We nest this inside the for-each-target loop above
  733. * so that we're able to get as much logging out to
  734. * at least one target if we die inside here, instead
  735. * of unnecessarily keeping all targets in lock-step.
  736. */
  737. tmp = msg;
  738. for (left = len; left;) {
  739. frag = min(left, MAX_PRINT_CHUNK);
  740. netpoll_send_udp(&nt->np, tmp, frag);
  741. tmp += frag;
  742. left -= frag;
  743. }
  744. }
  745. }
  746. spin_unlock_irqrestore(&target_list_lock, flags);
  747. }
  748. static struct console netconsole_ext = {
  749. .name = "netcon_ext",
  750. .flags = CON_EXTENDED, /* starts disabled, registered on first use */
  751. .write = write_ext_msg,
  752. };
  753. static struct console netconsole = {
  754. .name = "netcon",
  755. .flags = CON_ENABLED,
  756. .write = write_msg,
  757. };
  758. static int __init init_netconsole(void)
  759. {
  760. int err;
  761. struct netconsole_target *nt, *tmp;
  762. unsigned long flags;
  763. char *target_config;
  764. char *input = config;
  765. if (strnlen(input, MAX_PARAM_LENGTH)) {
  766. while ((target_config = strsep(&input, ";"))) {
  767. nt = alloc_param_target(target_config);
  768. if (IS_ERR(nt)) {
  769. err = PTR_ERR(nt);
  770. goto fail;
  771. }
  772. /* Dump existing printks when we register */
  773. if (nt->extended)
  774. netconsole_ext.flags |= CON_PRINTBUFFER |
  775. CON_ENABLED;
  776. else
  777. netconsole.flags |= CON_PRINTBUFFER;
  778. spin_lock_irqsave(&target_list_lock, flags);
  779. list_add(&nt->list, &target_list);
  780. spin_unlock_irqrestore(&target_list_lock, flags);
  781. }
  782. }
  783. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  784. if (err)
  785. goto fail;
  786. err = dynamic_netconsole_init();
  787. if (err)
  788. goto undonotifier;
  789. if (netconsole_ext.flags & CON_ENABLED)
  790. register_console(&netconsole_ext);
  791. register_console(&netconsole);
  792. pr_info("network logging started\n");
  793. return err;
  794. undonotifier:
  795. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  796. fail:
  797. pr_err("cleaning up\n");
  798. /*
  799. * Remove all targets and destroy them (only targets created
  800. * from the boot/module option exist here). Skipping the list
  801. * lock is safe here, and netpoll_cleanup() will sleep.
  802. */
  803. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  804. list_del(&nt->list);
  805. free_param_target(nt);
  806. }
  807. return err;
  808. }
  809. static void __exit cleanup_netconsole(void)
  810. {
  811. struct netconsole_target *nt, *tmp;
  812. unregister_console(&netconsole_ext);
  813. unregister_console(&netconsole);
  814. dynamic_netconsole_exit();
  815. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  816. /*
  817. * Targets created via configfs pin references on our module
  818. * and would first be rmdir(2)'ed from userspace. We reach
  819. * here only when they are already destroyed, and only those
  820. * created from the boot/module option are left, so remove and
  821. * destroy them. Skipping the list lock is safe here, and
  822. * netpoll_cleanup() will sleep.
  823. */
  824. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  825. list_del(&nt->list);
  826. free_param_target(nt);
  827. }
  828. }
  829. /*
  830. * Use late_initcall to ensure netconsole is
  831. * initialized after network device driver if built-in.
  832. *
  833. * late_initcall() and module_init() are identical if built as module.
  834. */
  835. late_initcall(init_netconsole);
  836. module_exit(cleanup_netconsole);