blob: be15ca6205a85aae2164243f674bb5a7c27bd030 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/module.h>
40#include <linux/console.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/moduleparam.h>
42#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/netpoll.h>
44
45MODULE_AUTHOR("Maintainer: Matt Mackall <mpm@selenic.com>");
46MODULE_DESCRIPTION("Console driver for network interfaces");
47MODULE_LICENSE("GPL");
48
Satyam Sharmad39badf2007-08-10 15:27:24 -070049#define MAX_PARAM_LENGTH 256
50#define MAX_PRINT_CHUNK 1000
51
52static char config[MAX_PARAM_LENGTH];
53module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]\n");
55
Satyam Sharmad2b60882007-08-10 15:29:47 -070056#ifndef MODULE
57static 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 Sharmadf180e32007-08-10 15:32:14 -070065/**
66 * struct netconsole_target - Represents a configured netconsole target.
67 * @np: The netpoll structure for this target.
68 */
69struct netconsole_target {
70 struct netpoll np;
71};
72
73static 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 Torvalds1da177e2005-04-16 15:20:36 -070081};
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static void write_msg(struct console *con, const char *msg, unsigned int len)
84{
85 int frag, left;
86 unsigned long flags;
Satyam Sharmadf180e32007-08-10 15:32:14 -070087 struct netconsole_target *nt = &default_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Satyam Sharmadf180e32007-08-10 15:32:14 -070089 if (netif_running(nt->np.dev)) {
Satyam Sharma0cc120be2007-08-10 15:30:31 -070090 local_irq_save(flags);
91 for (left = len; left;) {
92 frag = min(left, MAX_PRINT_CHUNK);
Satyam Sharmadf180e32007-08-10 15:32:14 -070093 netpoll_send_udp(&nt->np, msg, frag);
Satyam Sharma0cc120be2007-08-10 15:30:31 -070094 msg += frag;
95 left -= frag;
96 }
97 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static struct console netconsole = {
Satyam Sharmad39badf2007-08-10 15:27:24 -0700102 .name = "netcon",
103 .flags = CON_ENABLED | CON_PRINTBUFFER,
104 .write = write_msg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Satyam Sharmad39badf2007-08-10 15:27:24 -0700107static int __init init_netconsole(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Satyam Sharmad39badf2007-08-10 15:27:24 -0700109 int err = 0;
Satyam Sharmadf180e32007-08-10 15:32:14 -0700110 struct netconsole_target *nt = &default_target;
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700111
Satyam Sharmad2b60882007-08-10 15:29:47 -0700112 if (!strnlen(config, MAX_PARAM_LENGTH)) {
Satyam Sharmad39badf2007-08-10 15:27:24 -0700113 printk(KERN_INFO "netconsole: not configured, aborting\n");
114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
Satyam Sharmadf180e32007-08-10 15:32:14 -0700117 err = netpoll_parse_options(&nt->np, config);
Satyam Sharmad2b60882007-08-10 15:29:47 -0700118 if (err)
119 goto out;
120
Satyam Sharmadf180e32007-08-10 15:32:14 -0700121 err = netpoll_setup(&nt->np);
Stephen Hemmingerb41848b2006-10-26 15:46:52 -0700122 if (err)
Satyam Sharmad39badf2007-08-10 15:27:24 -0700123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 register_console(&netconsole);
126 printk(KERN_INFO "netconsole: network logging started\n");
Satyam Sharmad39badf2007-08-10 15:27:24 -0700127
128out:
129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Satyam Sharmad39badf2007-08-10 15:27:24 -0700132static void __exit cleanup_netconsole(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Satyam Sharmadf180e32007-08-10 15:32:14 -0700134 struct netconsole_target *nt = &default_target;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 unregister_console(&netconsole);
Satyam Sharmadf180e32007-08-10 15:32:14 -0700137 netpoll_cleanup(&nt->np);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140module_init(init_netconsole);
141module_exit(cleanup_netconsole);