netconsole.c 6.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*
 *  linux/drivers/net/netconsole.c
 *
 *  Copyright (C) 2001  Ingo Molnar <mingo@redhat.com>
 *
 *  This file contains the implementation of an IRQ-safe, crash-safe
 *  kernel console implementation that outputs kernel messages to the
 *  network.
 *
 * Modification history:
 *
 * 2001-09-17    started by Ingo Molnar.
 * 2003-08-11    2.6 port by Matt Mackall
 *               simplified options
 *               generic card hooks
 *               works non-modular
 * 2003-09-07    rewritten with netpoll api
 */

/****************************************************************
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2, or (at your option)
 *      any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 ****************************************************************/

#include <linux/mm.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/console.h>
#include <linux/moduleparam.h>
#include <linux/string.h>
#include <linux/netpoll.h>

MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
MODULE_DESCRIPTION("Console driver for network interfaces");
MODULE_LICENSE("GPL");

49 50 51 52 53
#define MAX_PARAM_LENGTH	256
#define MAX_PRINT_CHUNK		1000

static char config[MAX_PARAM_LENGTH];
module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
L
Linus Torvalds 已提交
54 55
MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]\n");

56 57 58 59 60 61 62 63 64
#ifndef	MODULE
static int __init option_setup(char *opt)
{
	strlcpy(config, opt, MAX_PARAM_LENGTH);
	return 1;
}
__setup("netconsole=", option_setup);
#endif	/* MODULE */

65 66 67 68 69 70
/* Linked list of all configured targets */
static LIST_HEAD(target_list);

/* This needs to be a spinlock because write_msg() cannot sleep */
static DEFINE_SPINLOCK(target_list_lock);

71 72
/**
 * struct netconsole_target - Represents a configured netconsole target.
73
 * @list:	Links this target into the target_list.
74 75 76
 * @np:		The netpoll structure for this target.
 */
struct netconsole_target {
77
	struct list_head	list;
78 79 80
	struct netpoll		np;
};

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/* Allocate new target and setup netpoll for it */
static struct netconsole_target *alloc_target(char *target_config)
{
	int err = -ENOMEM;
	struct netconsole_target *nt;

	/* Allocate and initialize with defaults */
	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
	if (!nt) {
		printk(KERN_ERR "netconsole: failed to allocate memory\n");
		goto fail;
	}

	nt->np.name = "netconsole";
	strlcpy(nt->np.dev_name, "eth0", IFNAMSIZ);
	nt->np.local_port = 6665;
	nt->np.remote_port = 6666;
	memset(nt->np.remote_mac, 0xff, ETH_ALEN);

	/* Parse parameters and setup netpoll */
	err = netpoll_parse_options(&nt->np, target_config);
	if (err)
		goto fail;

	err = netpoll_setup(&nt->np);
	if (err)
		goto fail;

	return nt;

fail:
	kfree(nt);
	return ERR_PTR(err);
}

/* Cleanup netpoll for given target and free it */
static void free_target(struct netconsole_target *nt)
{
	netpoll_cleanup(&nt->np);
	kfree(nt);
}
L
Linus Torvalds 已提交
122

123 124 125 126 127
/* Handle network interface device notifications */
static int netconsole_netdev_event(struct notifier_block *this,
				   unsigned long event,
				   void *ptr)
{
128 129
	unsigned long flags;
	struct netconsole_target *nt;
130 131
	struct net_device *dev = ptr;

132 133 134 135 136 137 138 139 140 141
	if (!(event == NETDEV_CHANGEADDR || event == NETDEV_CHANGENAME))
		goto done;

	spin_lock_irqsave(&target_list_lock, flags);
	list_for_each_entry(nt, &target_list, list) {
		if (nt->np.dev == dev) {
			switch (event) {
			case NETDEV_CHANGEADDR:
				memcpy(nt->np.local_mac, dev->dev_addr, ETH_ALEN);
				break;
142

143 144 145 146
			case NETDEV_CHANGENAME:
				strlcpy(nt->np.dev_name, dev->name, IFNAMSIZ);
				break;
			}
147 148
		}
	}
149
	spin_unlock_irqrestore(&target_list_lock, flags);
150

151
done:
152 153 154 155 156 157 158
	return NOTIFY_DONE;
}

static struct notifier_block netconsole_netdev_notifier = {
	.notifier_call  = netconsole_netdev_event,
};

L
Linus Torvalds 已提交
159 160 161 162
static void write_msg(struct console *con, const char *msg, unsigned int len)
{
	int frag, left;
	unsigned long flags;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
	struct netconsole_target *nt;
	const char *tmp;

	/* Avoid taking lock and disabling interrupts unnecessarily */
	if (list_empty(&target_list))
		return;

	spin_lock_irqsave(&target_list_lock, flags);
	list_for_each_entry(nt, &target_list, list) {
		if (netif_running(nt->np.dev)) {
			/*
			 * We nest this inside the for-each-target loop above
			 * so that we're able to get as much logging out to
			 * at least one target if we die inside here, instead
			 * of unnecessarily keeping all targets in lock-step.
			 */
			tmp = msg;
			for (left = len; left;) {
				frag = min(left, MAX_PRINT_CHUNK);
				netpoll_send_udp(&nt->np, tmp, frag);
				tmp += frag;
				left -= frag;
			}
186
		}
L
Linus Torvalds 已提交
187
	}
188
	spin_unlock_irqrestore(&target_list_lock, flags);
L
Linus Torvalds 已提交
189 190 191
}

static struct console netconsole = {
192 193 194
	.name	= "netcon",
	.flags	= CON_ENABLED | CON_PRINTBUFFER,
	.write	= write_msg,
L
Linus Torvalds 已提交
195 196
};

197
static int __init init_netconsole(void)
L
Linus Torvalds 已提交
198
{
199
	int err = 0;
200 201 202 203
	struct netconsole_target *nt, *tmp;
	unsigned long flags;
	char *target_config;
	char *input = config;
S
Stephen Hemminger 已提交
204

205
	if (!strnlen(input, MAX_PARAM_LENGTH)) {
206 207
		printk(KERN_INFO "netconsole: not configured, aborting\n");
		goto out;
L
Linus Torvalds 已提交
208 209
	}

210 211 212 213 214 215 216 217 218 219
	while ((target_config = strsep(&input, ";"))) {
		nt = alloc_target(target_config);
		if (IS_ERR(nt)) {
			err = PTR_ERR(nt);
			goto fail;
		}
		spin_lock_irqsave(&target_list_lock, flags);
		list_add(&nt->list, &target_list);
		spin_unlock_irqrestore(&target_list_lock, flags);
	}
L
Linus Torvalds 已提交
220

221 222
	err = register_netdevice_notifier(&netconsole_netdev_notifier);
	if (err)
223
		goto fail;
224

L
Linus Torvalds 已提交
225 226
	register_console(&netconsole);
	printk(KERN_INFO "netconsole: network logging started\n");
227 228 229

out:
	return err;
230 231 232 233 234 235 236 237 238 239 240 241 242 243

fail:
	printk(KERN_ERR "netconsole: cleaning up\n");

	/*
	 * Remove all targets and destroy them. Skipping the list
	 * lock is safe here, and netpoll_cleanup() will sleep.
	 */
	list_for_each_entry_safe(nt, tmp, &target_list, list) {
		list_del(&nt->list);
		free_target(nt);
	}

	return err;
L
Linus Torvalds 已提交
244 245
}

246
static void __exit cleanup_netconsole(void)
L
Linus Torvalds 已提交
247
{
248
	struct netconsole_target *nt, *tmp;
249

L
Linus Torvalds 已提交
250
	unregister_console(&netconsole);
251
	unregister_netdevice_notifier(&netconsole_netdev_notifier);
252 253 254 255 256 257 258 259 260

	/*
	 * Remove all targets and destroy them. Skipping the list
	 * lock is safe here, and netpoll_cleanup() will sleep.
	 */
	list_for_each_entry_safe(nt, tmp, &target_list, list) {
		list_del(&nt->list);
		free_target(nt);
	}
L
Linus Torvalds 已提交
261 262 263 264
}

module_init(init_netconsole);
module_exit(cleanup_netconsole);