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 | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 65 | /* Linked list of all configured targets */ |
| 66 | static LIST_HEAD(target_list); |
| 67 | |
| 68 | /* This needs to be a spinlock because write_msg() cannot sleep */ |
| 69 | static DEFINE_SPINLOCK(target_list_lock); |
| 70 | |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 71 | /** |
| 72 | * struct netconsole_target - Represents a configured netconsole target. |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 73 | * @list: Links this target into the target_list. |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 74 | * @np: The netpoll structure for this target. |
| 75 | */ |
| 76 | struct netconsole_target { |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 77 | struct list_head list; |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 78 | struct netpoll np; |
| 79 | }; |
| 80 | |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 81 | /* Allocate new target and setup netpoll for it */ |
| 82 | static struct netconsole_target *alloc_target(char *target_config) |
| 83 | { |
| 84 | int err = -ENOMEM; |
| 85 | struct netconsole_target *nt; |
| 86 | |
| 87 | /* Allocate and initialize with defaults */ |
| 88 | nt = kzalloc(sizeof(*nt), GFP_KERNEL); |
| 89 | if (!nt) { |
| 90 | printk(KERN_ERR "netconsole: failed to allocate memory\n"); |
| 91 | goto fail; |
| 92 | } |
| 93 | |
| 94 | nt->np.name = "netconsole"; |
| 95 | strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ); |
| 96 | nt->np.local_port = 6665; |
| 97 | nt->np.remote_port = 6666; |
| 98 | memset(nt->np.remote_mac, 0xff, ETH_ALEN); |
| 99 | |
| 100 | /* Parse parameters and setup netpoll */ |
| 101 | err = netpoll_parse_options(&nt->np, target_config); |
| 102 | if (err) |
| 103 | goto fail; |
| 104 | |
| 105 | err = netpoll_setup(&nt->np); |
| 106 | if (err) |
| 107 | goto fail; |
| 108 | |
| 109 | return nt; |
| 110 | |
| 111 | fail: |
| 112 | kfree(nt); |
| 113 | return ERR_PTR(err); |
| 114 | } |
| 115 | |
| 116 | /* Cleanup netpoll for given target and free it */ |
| 117 | static void free_target(struct netconsole_target *nt) |
| 118 | { |
| 119 | netpoll_cleanup(&nt->np); |
| 120 | kfree(nt); |
| 121 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 123 | /* Handle network interface device notifications */ |
| 124 | static int netconsole_netdev_event(struct notifier_block *this, |
| 125 | unsigned long event, |
| 126 | void *ptr) |
| 127 | { |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 128 | unsigned long flags; |
| 129 | struct netconsole_target *nt; |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 130 | struct net_device *dev = ptr; |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 131 | |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 132 | if (!(event == NETDEV_CHANGEADDR || event == NETDEV_CHANGENAME)) |
| 133 | goto done; |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 134 | |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 135 | spin_lock_irqsave(&target_list_lock, flags); |
| 136 | list_for_each_entry(nt, &target_list, list) { |
| 137 | if (nt->np.dev == dev) { |
| 138 | switch (event) { |
| 139 | case NETDEV_CHANGEADDR: |
| 140 | memcpy(nt->np.local_mac, dev->dev_addr, ETH_ALEN); |
| 141 | break; |
| 142 | |
| 143 | case NETDEV_CHANGENAME: |
| 144 | strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ); |
| 145 | break; |
| 146 | } |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 149 | spin_unlock_irqrestore(&target_list_lock, flags); |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 150 | |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 151 | done: |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 152 | return NOTIFY_DONE; |
| 153 | } |
| 154 | |
| 155 | static struct notifier_block netconsole_netdev_notifier = { |
| 156 | .notifier_call = netconsole_netdev_event, |
| 157 | }; |
| 158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | static void write_msg(struct console *con, const char *msg, unsigned int len) |
| 160 | { |
| 161 | int frag, left; |
| 162 | unsigned long flags; |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 163 | struct netconsole_target *nt; |
| 164 | const char *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 166 | /* Avoid taking lock and disabling interrupts unnecessarily */ |
| 167 | if (list_empty(&target_list)) |
| 168 | return; |
| 169 | |
| 170 | spin_lock_irqsave(&target_list_lock, flags); |
| 171 | list_for_each_entry(nt, &target_list, list) { |
| 172 | if (netif_running(nt->np.dev)) { |
| 173 | /* |
| 174 | * We nest this inside the for-each-target loop above |
| 175 | * so that we're able to get as much logging out to |
| 176 | * at least one target if we die inside here, instead |
| 177 | * of unnecessarily keeping all targets in lock-step. |
| 178 | */ |
| 179 | tmp = msg; |
| 180 | for (left = len; left;) { |
| 181 | frag = min(left, MAX_PRINT_CHUNK); |
| 182 | netpoll_send_udp(&nt->np, tmp, frag); |
| 183 | tmp += frag; |
| 184 | left -= frag; |
| 185 | } |
Satyam Sharma | 0cc120be | 2007-08-10 15:30:31 -0700 | [diff] [blame] | 186 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 188 | spin_unlock_irqrestore(&target_list_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static struct console netconsole = { |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 192 | .name = "netcon", |
| 193 | .flags = CON_ENABLED | CON_PRINTBUFFER, |
| 194 | .write = write_msg, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 197 | static int __init init_netconsole(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 199 | int err = 0; |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 200 | struct netconsole_target *nt, *tmp; |
| 201 | unsigned long flags; |
| 202 | char *target_config; |
| 203 | char *input = config; |
Stephen Hemminger | b41848b | 2006-10-26 15:46:52 -0700 | [diff] [blame] | 204 | |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 205 | if (!strnlen(input, MAX_PARAM_LENGTH)) { |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 206 | printk(KERN_INFO "netconsole: not configured, aborting\n"); |
| 207 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 210 | while ((target_config = strsep(&input, ";"))) { |
| 211 | nt = alloc_target(target_config); |
| 212 | if (IS_ERR(nt)) { |
| 213 | err = PTR_ERR(nt); |
| 214 | goto fail; |
| 215 | } |
| 216 | spin_lock_irqsave(&target_list_lock, flags); |
| 217 | list_add(&nt->list, &target_list); |
| 218 | spin_unlock_irqrestore(&target_list_lock, flags); |
| 219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 221 | err = register_netdevice_notifier(&netconsole_netdev_notifier); |
| 222 | if (err) |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 223 | goto fail; |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | register_console(&netconsole); |
| 226 | printk(KERN_INFO "netconsole: network logging started\n"); |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 227 | |
| 228 | out: |
| 229 | return err; |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 230 | |
| 231 | fail: |
| 232 | printk(KERN_ERR "netconsole: cleaning up\n"); |
| 233 | |
| 234 | /* |
| 235 | * Remove all targets and destroy them. Skipping the list |
| 236 | * lock is safe here, and netpoll_cleanup() will sleep. |
| 237 | */ |
| 238 | list_for_each_entry_safe(nt, tmp, &target_list, list) { |
| 239 | list_del(&nt->list); |
| 240 | free_target(nt); |
| 241 | } |
| 242 | |
| 243 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Satyam Sharma | d39badf | 2007-08-10 15:27:24 -0700 | [diff] [blame] | 246 | static void __exit cleanup_netconsole(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | { |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 248 | struct netconsole_target *nt, *tmp; |
Satyam Sharma | df180e3 | 2007-08-10 15:32:14 -0700 | [diff] [blame] | 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | unregister_console(&netconsole); |
Satyam Sharma | 17951f3 | 2007-08-10 15:33:01 -0700 | [diff] [blame] | 251 | unregister_netdevice_notifier(&netconsole_netdev_notifier); |
Satyam Sharma | b5427c2 | 2007-08-10 15:33:40 -0700 | [diff] [blame^] | 252 | |
| 253 | /* |
| 254 | * Remove all targets and destroy them. Skipping the list |
| 255 | * lock is safe here, and netpoll_cleanup() will sleep. |
| 256 | */ |
| 257 | list_for_each_entry_safe(nt, tmp, &target_list, list) { |
| 258 | list_del(&nt->list); |
| 259 | free_target(nt); |
| 260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | module_init(init_netconsole); |
| 264 | module_exit(cleanup_netconsole); |