netconsole.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/console.h>
  39. #include <linux/moduleparam.h>
  40. #include <linux/string.h>
  41. #include <linux/netpoll.h>
  42. MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
  43. MODULE_DESCRIPTION("Console driver for network interfaces");
  44. MODULE_LICENSE("GPL");
  45. #define MAX_PARAM_LENGTH 256
  46. #define MAX_PRINT_CHUNK 1000
  47. static char config[MAX_PARAM_LENGTH];
  48. module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
  49. MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]\n");
  50. #ifndef MODULE
  51. static int __init option_setup(char *opt)
  52. {
  53. strlcpy(config, opt, MAX_PARAM_LENGTH);
  54. return 1;
  55. }
  56. __setup("netconsole=", option_setup);
  57. #endif /* MODULE */
  58. /* Linked list of all configured targets */
  59. static LIST_HEAD(target_list);
  60. /* This needs to be a spinlock because write_msg() cannot sleep */
  61. static DEFINE_SPINLOCK(target_list_lock);
  62. /**
  63. * struct netconsole_target - Represents a configured netconsole target.
  64. * @list: Links this target into the target_list.
  65. * @np: The netpoll structure for this target.
  66. */
  67. struct netconsole_target {
  68. struct list_head list;
  69. struct netpoll np;
  70. };
  71. /* Allocate new target and setup netpoll for it */
  72. static struct netconsole_target *alloc_target(char *target_config)
  73. {
  74. int err = -ENOMEM;
  75. struct netconsole_target *nt;
  76. /* Allocate and initialize with defaults */
  77. nt = kzalloc(sizeof(*nt), GFP_KERNEL);
  78. if (!nt) {
  79. printk(KERN_ERR "netconsole: failed to allocate memory\n");
  80. goto fail;
  81. }
  82. nt->np.name = "netconsole";
  83. strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
  84. nt->np.local_port = 6665;
  85. nt->np.remote_port = 6666;
  86. memset(nt->np.remote_mac, 0xff, ETH_ALEN);
  87. /* Parse parameters and setup netpoll */
  88. err = netpoll_parse_options(&nt->np, target_config);
  89. if (err)
  90. goto fail;
  91. err = netpoll_setup(&nt->np);
  92. if (err)
  93. goto fail;
  94. return nt;
  95. fail:
  96. kfree(nt);
  97. return ERR_PTR(err);
  98. }
  99. /* Cleanup netpoll for given target and free it */
  100. static void free_target(struct netconsole_target *nt)
  101. {
  102. netpoll_cleanup(&nt->np);
  103. kfree(nt);
  104. }
  105. /* Handle network interface device notifications */
  106. static int netconsole_netdev_event(struct notifier_block *this,
  107. unsigned long event,
  108. void *ptr)
  109. {
  110. unsigned long flags;
  111. struct netconsole_target *nt;
  112. struct net_device *dev = ptr;
  113. if (!(event == NETDEV_CHANGEADDR || event == NETDEV_CHANGENAME))
  114. goto done;
  115. spin_lock_irqsave(&target_list_lock, flags);
  116. list_for_each_entry(nt, &target_list, list) {
  117. if (nt->np.dev == dev) {
  118. switch (event) {
  119. case NETDEV_CHANGEADDR:
  120. memcpy(nt->np.local_mac, dev->dev_addr, ETH_ALEN);
  121. break;
  122. case NETDEV_CHANGENAME:
  123. strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  124. break;
  125. }
  126. }
  127. }
  128. spin_unlock_irqrestore(&target_list_lock, flags);
  129. done:
  130. return NOTIFY_DONE;
  131. }
  132. static struct notifier_block netconsole_netdev_notifier = {
  133. .notifier_call = netconsole_netdev_event,
  134. };
  135. static void write_msg(struct console *con, const char *msg, unsigned int len)
  136. {
  137. int frag, left;
  138. unsigned long flags;
  139. struct netconsole_target *nt;
  140. const char *tmp;
  141. /* Avoid taking lock and disabling interrupts unnecessarily */
  142. if (list_empty(&target_list))
  143. return;
  144. spin_lock_irqsave(&target_list_lock, flags);
  145. list_for_each_entry(nt, &target_list, list) {
  146. if (netif_running(nt->np.dev)) {
  147. /*
  148. * We nest this inside the for-each-target loop above
  149. * so that we're able to get as much logging out to
  150. * at least one target if we die inside here, instead
  151. * of unnecessarily keeping all targets in lock-step.
  152. */
  153. tmp = msg;
  154. for (left = len; left;) {
  155. frag = min(left, MAX_PRINT_CHUNK);
  156. netpoll_send_udp(&nt->np, tmp, frag);
  157. tmp += frag;
  158. left -= frag;
  159. }
  160. }
  161. }
  162. spin_unlock_irqrestore(&target_list_lock, flags);
  163. }
  164. static struct console netconsole = {
  165. .name = "netcon",
  166. .flags = CON_ENABLED | CON_PRINTBUFFER,
  167. .write = write_msg,
  168. };
  169. static int __init init_netconsole(void)
  170. {
  171. int err = 0;
  172. struct netconsole_target *nt, *tmp;
  173. unsigned long flags;
  174. char *target_config;
  175. char *input = config;
  176. if (!strnlen(input, MAX_PARAM_LENGTH)) {
  177. printk(KERN_INFO "netconsole: not configured, aborting\n");
  178. goto out;
  179. }
  180. while ((target_config = strsep(&input, ";"))) {
  181. nt = alloc_target(target_config);
  182. if (IS_ERR(nt)) {
  183. err = PTR_ERR(nt);
  184. goto fail;
  185. }
  186. spin_lock_irqsave(&target_list_lock, flags);
  187. list_add(&nt->list, &target_list);
  188. spin_unlock_irqrestore(&target_list_lock, flags);
  189. }
  190. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  191. if (err)
  192. goto fail;
  193. register_console(&netconsole);
  194. printk(KERN_INFO "netconsole: network logging started\n");
  195. out:
  196. return err;
  197. fail:
  198. printk(KERN_ERR "netconsole: cleaning up\n");
  199. /*
  200. * Remove all targets and destroy them. Skipping the list
  201. * lock is safe here, and netpoll_cleanup() will sleep.
  202. */
  203. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  204. list_del(&nt->list);
  205. free_target(nt);
  206. }
  207. return err;
  208. }
  209. static void __exit cleanup_netconsole(void)
  210. {
  211. struct netconsole_target *nt, *tmp;
  212. unregister_console(&netconsole);
  213. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  214. /*
  215. * Remove all targets and destroy them. Skipping the list
  216. * lock is safe here, and netpoll_cleanup() will sleep.
  217. */
  218. list_for_each_entry_safe(nt, tmp, &target_list, list) {
  219. list_del(&nt->list);
  220. free_target(nt);
  221. }
  222. }
  223. module_init(init_netconsole);
  224. module_exit(cleanup_netconsole);