Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /**************************************************************** |
| 21 | * This program is free software; you can redistribute it and/or modify |
| 22 | * it under the terms of the GNU General Public License as published by |
| 23 | * the Free Software Foundation; either version 2, or (at your option) |
| 24 | * any later version. |
| 25 | * |
| 26 | * This program is distributed in the hope that it will be useful, |
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | * GNU General Public License for more details. |
| 30 | * |
| 31 | * You should have received a copy of the GNU General Public License |
| 32 | * along with this program; if not, write to the Free Software |
| 33 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 34 | * |
| 35 | ****************************************************************/ |
| 36 | |
| 37 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/init.h> |
| 39 | #include <linux/module.h> |
| 40 | #include <linux/console.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/moduleparam.h> |
| 42 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/netpoll.h> |
| 44 | |
| 45 | MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>"); |
| 46 | MODULE_DESCRIPTION("Console driver for network interfaces"); |
| 47 | MODULE_LICENSE("GPL"); |
| 48 | |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 49 | #define MAX_PARAM_LENGTH 256 |
| 50 | #define MAX_PRINT_CHUNK 1000 |
| 51 | |
| 52 | static char config[MAX_PARAM_LENGTH]; |
| 53 | module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]\n"); |
| 55 | |
Satyam Sharma | d2b6088 | 2007-08-10 15:29:47 -0700 | [diff] [blame] | 56 | #ifndef MODULE |
| 57 | static int __init option_setup(char *opt) |
| 58 | { |
| 59 | strlcpy(config, opt, MAX_PARAM_LENGTH); |
| 60 | return 1; |
| 61 | } |
| 62 | __setup("netconsole=", option_setup); |
| 63 | #endif /* MODULE */ |
| 64 | |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 65 | /** |
| 66 | * struct netconsole_target - Represents a configured netconsole target. |
| 67 | * @np: The netpoll structure for this target. |
| 68 | */ |
| 69 | struct netconsole_target { |
| 70 | struct netpoll np; |
| 71 | }; |
| 72 | |
| 73 | static struct netconsole_target default_target = { |
| 74 | .np = { |
| 75 | .name = "netconsole", |
| 76 | .dev_name = "eth0", |
| 77 | .local_port = 6665, |
| 78 | .remote_port = 6666, |
| 79 | .remote_mac = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, |
| 80 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame^] | 83 | /* Handle network interface device notifications */ |
| 84 | static int netconsole_netdev_event(struct notifier_block *this, |
| 85 | unsigned long event, |
| 86 | void *ptr) |
| 87 | { |
| 88 | struct net_device *dev = ptr; |
| 89 | struct netconsole_target *nt = &default_target; |
| 90 | |
| 91 | if (nt->np.dev == dev) { |
| 92 | switch (event) { |
| 93 | case NETDEV_CHANGEADDR: |
| 94 | memcpy(nt->np.local_mac, dev->dev_addr, ETH_ALEN); |
| 95 | break; |
| 96 | |
| 97 | case NETDEV_CHANGENAME: |
| 98 | strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ); |
| 99 | break; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return NOTIFY_DONE; |
| 104 | } |
| 105 | |
| 106 | static struct notifier_block netconsole_netdev_notifier = { |
| 107 | .notifier_call = netconsole_netdev_event, |
| 108 | }; |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | static void write_msg(struct console *con, const char *msg, unsigned int len) |
| 111 | { |
| 112 | int frag, left; |
| 113 | unsigned long flags; |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 114 | struct netconsole_target *nt = &default_target; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 116 | if (netif_running(nt->np.dev)) { |
Satyam Sharma | 0cc120be | 2007-08-10 15:30:31 -0700 | [diff] [blame] | 117 | local_irq_save(flags); |
| 118 | for (left = len; left;) { |
| 119 | frag = min(left, MAX_PRINT_CHUNK); |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 120 | netpoll_send_udp(&nt->np, msg, frag); |
Satyam Sharma | 0cc120be | 2007-08-10 15:30:31 -0700 | [diff] [blame] | 121 | msg += frag; |
| 122 | left -= frag; |
| 123 | } |
| 124 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static struct console netconsole = { |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 129 | .name = "netcon", |
| 130 | .flags = CON_ENABLED | CON_PRINTBUFFER, |
| 131 | .write = write_msg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 134 | static int __init init_netconsole(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | { |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 136 | int err = 0; |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 137 | struct netconsole_target *nt = &default_target; |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 138 | |
Satyam Sharma | d2b6088 | 2007-08-10 15:29:47 -0700 | [diff] [blame] | 139 | if (!strnlen(config, MAX_PARAM_LENGTH)) { |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 140 | printk(KERN_INFO "netconsole: not configured, aborting\n"); |
| 141 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 144 | err = netpoll_parse_options(&nt->np, config); |
Satyam Sharma | d2b6088 | 2007-08-10 15:29:47 -0700 | [diff] [blame] | 145 | if (err) |
| 146 | goto out; |
| 147 | |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 148 | err = netpoll_setup(&nt->np); |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 149 | if (err) |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 150 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame^] | 152 | err = register_netdevice_notifier(&netconsole_netdev_notifier); |
| 153 | if (err) |
| 154 | goto out; |
| 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | register_console(&netconsole); |
| 157 | printk(KERN_INFO "netconsole: network logging started\n"); |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 158 | |
| 159 | out: |
| 160 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 163 | static void __exit cleanup_netconsole(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 165 | struct netconsole_target *nt = &default_target; |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | unregister_console(&netconsole); |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame^] | 168 | unregister_netdevice_notifier(&netconsole_netdev_notifier); |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 169 | netpoll_cleanup(&nt->np); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | module_init(init_netconsole); |
| 173 | module_exit(cleanup_netconsole); |