diff -urN 2.4.10pre12/drivers/net/netconsole.c netconsole/drivers/net/netconsole.c --- 2.4.10pre12/drivers/net/netconsole.c Thu Jan 1 01:00:00 1970 +++ netconsole/drivers/net/netconsole.c Thu Sep 20 06:55:44 2001 @@ -0,0 +1,255 @@ +/* + * linux/drivers/net/netconsole.c + * + * Copyright (C) 2001 Ingo Molnar + * + * 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. + */ + +/**************************************************************** + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct net_device *netconsole_dev; +static u16 source_port, target_port; +static u32 source_ip, target_ip; +static unsigned char daddr[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} ; + +static void send_netconsole_skb (struct net_device *dev, const char *msg, unsigned int msg_len) +{ + int total_len, eth_len, ip_len, udp_len; + struct sk_buff *skb; + struct udphdr *udph; + struct iphdr *iph; + struct ethhdr *eth; + + udp_len = msg_len + sizeof(*udph); + ip_len = eth_len = udp_len + sizeof(*iph); + total_len = eth_len + ETH_HLEN; + + skb = alloc_skb(total_len, GFP_ATOMIC); + if (!skb) + return; + + atomic_set(&skb->users, 1); + skb_reserve(skb, total_len - msg_len); + memcpy(skb->data, msg, msg_len); + skb->len += msg_len; + + udph = (struct udphdr *) skb_push(skb, sizeof(*udph)); + udph->source = source_port; + udph->dest = target_port; + udph->len = htons(udp_len); + udph->check = 0; + + iph = (struct iphdr *)skb_push(skb, sizeof(*iph)); + + iph->version = 4; + iph->ihl = 5; + iph->tos = 0; + iph->tot_len = htons(ip_len); + iph->id = 0; + iph->frag_off = 0; + iph->ttl = 64; + iph->protocol = IPPROTO_UDP; + iph->check = 0; + iph->saddr = source_ip; + iph->daddr = target_ip; + iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); + + eth = (struct ethhdr *) skb_push(skb, ETH_HLEN); + + eth->h_proto = htons(ETH_P_IP); + memcpy(eth->h_source, dev->dev_addr, dev->addr_len); + memcpy(eth->h_dest, daddr, dev->addr_len); + +repeat: + spin_lock(&dev->xmit_lock); + dev->xmit_lock_owner = smp_processor_id(); + + if (netif_queue_stopped(dev)) { + dev->xmit_lock_owner = -1; + spin_unlock(&dev->xmit_lock); + + dev->poll_controller(dev); + goto repeat; + } + + dev->hard_start_xmit(skb, dev); + + dev->xmit_lock_owner = -1; + spin_unlock(&dev->xmit_lock); +} + +#define MAX_UDP_CHUNK 1500 + +static void write_netconsole_msg(struct console *con, const char *msg, unsigned int msg_len) +{ + int len, left; + struct net_device *dev; + + dev = netconsole_dev; + if (!dev) + return; + + if (dev->poll_controller && netif_running(dev)) { + unsigned long flags; + + __save_flags(flags); + __cli(); + left = msg_len; +repeat: + if (left > MAX_UDP_CHUNK) + len = MAX_UDP_CHUNK; + else + len = left; + send_netconsole_skb(dev, msg, len); + msg += len; + left -= len; + if (left) + goto repeat; + __restore_flags(flags); + } +} + +static char *dev; +static int target_eth_byte0 = 255; +static int target_eth_byte1 = 255; +static int target_eth_byte2 = 255; +static int target_eth_byte3 = 255; +static int target_eth_byte4 = 255; +static int target_eth_byte5 = 255; + +MODULE_PARM(target_ip, "i"); +MODULE_PARM(target_eth_byte0, "i"); +MODULE_PARM(target_eth_byte1, "i"); +MODULE_PARM(target_eth_byte2, "i"); +MODULE_PARM(target_eth_byte3, "i"); +MODULE_PARM(target_eth_byte4, "i"); +MODULE_PARM(target_eth_byte5, "i"); +MODULE_PARM(source_port, "h"); +MODULE_PARM(target_port, "h"); +MODULE_PARM(dev, "s"); + +static struct console netconsole = + { flags: CON_ENABLED, write: write_netconsole_msg }; + +static int init_netconsole (void) +{ + struct net_device *ndev = NULL; + struct in_device *in_dev; + + printk(KERN_INFO "netconsole: using network device <%s>\n", dev); + // this will be valid once the device goes up. + if (dev) + ndev = dev_get_by_name(dev); + if (!ndev) { + printk(KERN_ERR "netconsole: network device %s does not exist, aborting.\n", dev); + return -1; + } + if (!ndev->poll_controller) { + printk(KERN_ERR "netconsole: %s's network driver does not implement netlogging yet, aborting.\n", dev); + return -1; + } + in_dev = in_dev_get(ndev); + if (!in_dev) { + printk(KERN_ERR "netconsole: network device %s is not an IP protocol device, aborting.\n", dev); + return -1; + } + source_ip = ntohl(in_dev->ifa_list->ifa_local); + if (!source_ip) { + printk(KERN_ERR "netconsole: network device %s has no local address, aborting.\n", dev); + return -1; + } +#define IP(x) ((char *)&source_ip)[x] + printk(KERN_INFO "netconsole: using source IP %u.%u.%u.%u\n", + IP(3), IP(2), IP(1), IP(0)); +#undef IP + source_ip = htonl(source_ip); + if (!target_ip) { + printk(KERN_ERR "netconsole: target_ip parameter not specified, aborting.\n"); + return -1; + } +#define IP(x) ((char *)&target_ip)[x] + printk(KERN_INFO "netconsole: using target IP %u.%u.%u.%u\n", + IP(3), IP(2), IP(1), IP(0)); +#undef IP + target_ip = htonl(target_ip); + if (!source_port) { + printk(KERN_ERR "netconsole: source_port parameter not specified, aborting.\n"); + return -1; + } + printk(KERN_INFO "netconsole: using source UDP port: %d\n", source_port); + source_port = htons(source_port); + if (!target_port) { + printk(KERN_ERR "netconsole: target_port parameter not specified, aborting.\n"); + return -1; + } + printk(KERN_INFO "netconsole: using target UDP port: %d\n", target_port); + target_port = htons(target_port); + + daddr[0] = target_eth_byte0; + daddr[1] = target_eth_byte1; + daddr[2] = target_eth_byte2; + daddr[3] = target_eth_byte3; + daddr[4] = target_eth_byte4; + daddr[5] = target_eth_byte5; + + if ((daddr[0] & daddr[1] & daddr[2] & daddr[3] & daddr[4] & daddr[5]) == 255) + printk(KERN_INFO "netconsole: using broadcast ethernet frames to send packets.\n"); + else + printk(KERN_INFO "netconsole: using target ethernet address %02x:%02x:%02x:%02x:%02x:%02x.\n", daddr[0], daddr[1], daddr[2], daddr[3], daddr[4], daddr[5]); + + netconsole_dev = ndev; +#define STARTUP_MSG "[...network console startup...]\n" + write_netconsole_msg(NULL, STARTUP_MSG, strlen(STARTUP_MSG)); + + register_console(&netconsole); + printk(KERN_INFO "netconsole: network logging started up successfully!\n"); + return 0; +} + +static void cleanup_netconsole (void) +{ + printk(KERN_INFO "netconsole: network logging shut down.\n"); + unregister_console(&netconsole); + +#define SHUTDOWN_MSG "[...network console shutdown...]\n" + write_netconsole_msg(NULL, SHUTDOWN_MSG, strlen(SHUTDOWN_MSG)); + netconsole_dev = NULL; +} + +module_init(init_netconsole); +module_exit(cleanup_netconsole); +