netconsole.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  59. * struct netconsole_target - Represents a configured netconsole target.
  60. * @np: The netpoll structure for this target.
  61. */
  62. struct netconsole_target {
  63. struct netpoll np;
  64. };
  65. static struct netconsole_target default_target = {
  66. .np = {
  67. .name = "netconsole",
  68. .dev_name = "eth0",
  69. .local_port = 6665,
  70. .remote_port = 6666,
  71. .remote_mac = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
  72. },
  73. };
  74. /* Handle network interface device notifications */
  75. static int netconsole_netdev_event(struct notifier_block *this,
  76. unsigned long event,
  77. void *ptr)
  78. {
  79. struct net_device *dev = ptr;
  80. struct netconsole_target *nt = &default_target;
  81. if (nt->np.dev == dev) {
  82. switch (event) {
  83. case NETDEV_CHANGEADDR:
  84. memcpy(nt->np.local_mac, dev->dev_addr, ETH_ALEN);
  85. break;
  86. case NETDEV_CHANGENAME:
  87. strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
  88. break;
  89. }
  90. }
  91. return NOTIFY_DONE;
  92. }
  93. static struct notifier_block netconsole_netdev_notifier = {
  94. .notifier_call = netconsole_netdev_event,
  95. };
  96. static void write_msg(struct console *con, const char *msg, unsigned int len)
  97. {
  98. int frag, left;
  99. unsigned long flags;
  100. struct netconsole_target *nt = &default_target;
  101. if (netif_running(nt->np.dev)) {
  102. local_irq_save(flags);
  103. for (left = len; left;) {
  104. frag = min(left, MAX_PRINT_CHUNK);
  105. netpoll_send_udp(&nt->np, msg, frag);
  106. msg += frag;
  107. left -= frag;
  108. }
  109. local_irq_restore(flags);
  110. }
  111. }
  112. static struct console netconsole = {
  113. .name = "netcon",
  114. .flags = CON_ENABLED | CON_PRINTBUFFER,
  115. .write = write_msg,
  116. };
  117. static int __init init_netconsole(void)
  118. {
  119. int err = 0;
  120. struct netconsole_target *nt = &default_target;
  121. if (!strnlen(config, MAX_PARAM_LENGTH)) {
  122. printk(KERN_INFO "netconsole: not configured, aborting\n");
  123. goto out;
  124. }
  125. err = netpoll_parse_options(&nt->np, config);
  126. if (err)
  127. goto out;
  128. err = netpoll_setup(&nt->np);
  129. if (err)
  130. goto out;
  131. err = register_netdevice_notifier(&netconsole_netdev_notifier);
  132. if (err)
  133. goto out;
  134. register_console(&netconsole);
  135. printk(KERN_INFO "netconsole: network logging started\n");
  136. out:
  137. return err;
  138. }
  139. static void __exit cleanup_netconsole(void)
  140. {
  141. struct netconsole_target *nt = &default_target;
  142. unregister_console(&netconsole);
  143. unregister_netdevice_notifier(&netconsole_netdev_notifier);
  144. netpoll_cleanup(&nt->np);
  145. }
  146. module_init(init_netconsole);
  147. module_exit(cleanup_netconsole);