Linux驱动----网络驱动开发实例
详细原理不介绍, 参照《Linux 设备驱动 Edition 3》中第17章网络驱动. 本文把Linux设备驱动示例代码snull移植到2.6.32版本,对应于发行版redhat6,centos6。
为了提高高端 Linux 系统性能的方法, 目前网络子系统开发一般采用基于查询的接口,所以把snull代码中接收中断去掉了,只使用NAPI。由于对snull比较复杂,便于理解,进行相应裁剪,去掉对功能没有影响代码,接下来对改动地方进行说明。对于修改原因,下面做了简单说明,具体原因测试过程,可以不修改(你觉得无关紧要或者无法确定代码),看看会出现什么问题,这样会理解更深。
注意:
1.本人内核版本:
# uname -r
2.6.32-71.el6
2.发行版:
# cat /etc/redhat-release
CentOS Linux release 6.0 (Final)
一.修改说明:
1.snull_open函数
添加napi_enable(&vi->napi);语句,表示启动napi接受
2.snull_release函数
添加
napi_disable(&vi->napi);表示关闭napi接受
netif_carrier_off(dev);表示关闭载波
3.snull_napi_interrupt函数
由于内核接口,发生变化,以前代码改为如下:
if (napi_schedule_prep(&priv->napi)) {
snull_rx_ints(dev, 0); /* Disable further interrupts */
printk("napi_schedule_prep\n");
__napi_schedule(&priv->napi);
}
4.内核接口t struct net_device_ops发生改变,所以下面进行修改:
static const struct net_device_ops virtnet_netdev = {
.ndo_open = snull_open,
.ndo_stop = snull_release,
.ndo_start_xmit = snull_tx,
.ndo_get_stats = snull_stats,
.ndo_set_rx_mode = snull_set_rx_mode,
};
static const struct header_ops header_op = {
.create = snull_header,
.rebuild = snull_rebuild_header,
};
5.snull_init_module函数改动较大
为了简单,直接用了以太网接口,所以网络接口不是sn0,sn1.
snull_devs[0] = alloc_etherdev(sizeof(struct snull_priv));
snull_devs[1] = alloc_etherdev(sizeof(struct snull_priv));
for (i = 0; i < 2; i++) {
snull_devs[i]->flags |= IFF_NOARP;
snull_devs[i]->features |= NETIF_F_NO_CSUM;
snull_devs[i]->header_ops = &header_op;
snull_rx_ints(snull_devs[i], 1);
snull_setup_pool(snull_devs[i]);
snull_devs[i]->netdev_ops = &virtnet_netdev;
vi = netdev_priv(snull_devs[i]);
vi->dev = snull_devs[i];
netif_napi_add(snull_devs[i], &vi->napi, snull_poll,2);
}
二.测试
1.编译,运行,加载内核模块,查看接口是否创建
#insmod snull.ko
#ifconfig -a
eth1 Link encap:Ethernet HWaddr 00:00:00:00:00:00
BROADCAST NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
eth2 Link encap:Ethernet HWaddr 00:00:00:00:00:00
BROADCAST NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
2.编辑相应文件
#vi /etc/networks
//添加下面
snullnet0 192.168.0.0
snullnet1 192.168.1.0
/etc/hosts 里面:
#vi /etc/hosts
192.168.0.1 local0
192.168.0.2 remote0
192.168.1.2 local1
192.168.1.1 remote1
3.设置网络接口ip
#ifconfig eth1 local0
#ifconfig eth2 local1
eth1 Link encap:Ethernet HWaddr 00:53:4E:55:4C:30
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c30/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:210 (210.0 b) TX bytes:210 (210.0 b)
eth2 Link encap:Ethernet HWaddr 00:53:4E:55:4C:31
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c31/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:210 (210.0 b) TX bytes:210 (210.0 b)
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
说明接口正常运行了。
4.数据包发送与接受
查看包接受情况
#ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:53:4E:55:4C:30
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c30/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:602 (602.0 b) TX bytes:798 (798.0 b)
[root@localhost ~]# ifconfig eth2
eth2 Link encap:Ethernet HWaddr 00:53:4E:55:4C:31
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c31/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:798 (798.0 b) TX bytes:602 (602.0 b)
发送数据包
# ping -c 2 remote0
PING remote0 (192.168.0.2) 56(84) bytes of data.
64 bytes from remote0 (192.168.0.2): icmp_seq=1 ttl=64 time=0.125 ms
64 bytes from remote0 (192.168.0.2): icmp_seq=2 ttl=64 time=0.211 ms
--- remote0 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.125/0.168/0.211/0.043 ms
# ping -c 2 remote1
PING remote1 (192.168.1.1) 56(84) bytes of data.
64 bytes from remote1 (192.168.1.1): icmp_seq=1 ttl=64 time=0.310 ms
64 bytes from remote1 (192.168.1.1): icmp_seq=2 ttl=64 time=0.216 ms
--- remote1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.216/0.263/0.310/0.047 ms
#再查看包接受情况
eth1 Link encap:Ethernet HWaddr 00:53:4E:55:4C:30
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c30/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:11 errors:0 dropped:0 overruns:0 frame:0
TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:994 (994.0 b) TX bytes:1190 (1.1 KiB)
eth2 Link encap:Ethernet HWaddr 00:53:4E:55:4C:31
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c31/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1190 (1.1 KiB) TX bytes:994 (994.0 b)
5.卸载模块
# rmmod snull
# ifconfig -a
注意地方:
1.网卡接口名称发生变化
sn----eth
2.包发送情况统计 ndo_get_stats
.ndo_get_stats = snull_stats,
如果该函数不实现,包统计数据无法显示
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
3.ARP地址解析与填充
static const struct header_ops header_op = {
.create = snull_header,
.rebuild = snull_rebuild_header,
};
如果该函数不实现,包无法进行通信的,即两个接口之间。因为包的以太网包头MAC地址不存在或者不正确。
4.网络测试时,要保证 192.168.0.0 ,192.168.1.0两个网段没有使用。
三.代码
/*
* snull.c -- the Simple Network Utility
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*
* $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/interrupt.h> /* mark_bh */
#include <linux/in.h>
#include <linux/netdevice.h> /* struct device, and other headers */
#include <linux/etherdevice.h> /* eth_type_trans */
#include <linux/ip.h> /* struct iphdr */
#include <linux/tcp.h> /* struct tcphdr */
#include <linux/skbuff.h>
#include "snull.h"
#include <linux/in6.h>
#include <asm/checksum.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
/*
* Do we run in NAPI mode?
*/
static int use_napi = 1;
module_param(use_napi, int, 0);
/*
* A structure representing an in-flight packet.
*/
struct snull_packet {
struct snull_packet *next;
struct net_device *dev;
int datalen;
u8 data[ETH_DATA_LEN];
};
int pool_size = 8;
module_param(pool_size, int, 0);
/*
* This structure is private to each device. It is used to pass
* packets in and out, so there is place for a packet
*/
struct snull_priv {
struct net_device_stats stats;
struct net_device *dev;
struct napi_struct napi;
int status;
struct snull_packet *ppool;
struct snull_packet *rx_queue; /* List of incoming packets */
int rx_int_enabled;
int tx_packetlen;
u8 *tx_packetdata;
struct sk_buff *skb;
spinlock_t lock;
};
static void (*snull_interrupt)(int, void *, struct pt_regs *);
/*
* Set up a device's packet pool.
*/
void snull_setup_pool(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
int i;
struct snull_packet *pkt;
priv->ppool = NULL;
for (i = 0; i < pool_size; i++) {
printk("%s\n",__func__);
pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL);
if (pkt == NULL) {
printk (KERN_NOTICE "Ran out of memory allocating packet pool\n");
return;
}
pkt->dev = dev;
pkt->next = priv->ppool;
priv->ppool = pkt;
}
printk("%p,%p\n",dev,priv->ppool);
}
void snull_teardown_pool(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
struct snull_packet *pkt;
while ((pkt = priv->ppool)) {
priv->ppool = pkt->next;
kfree (pkt);
/* FIXME - in-flight packets ? */
}
}
/*
* Buffer/pool management.
*/
struct snull_packet *snull_get_tx_buffer(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
unsigned long flags;
struct snull_packet *pkt;
int i=0;
spin_lock_irqsave(&priv->lock, flags);
pkt = priv->ppool;
priv->ppool = pkt->next;
if (priv->ppool == NULL) {
printk (KERN_INFO "Pool empty %p\n",dev);
for(i=0;i<ETH_ALEN;i++)
printk("netif_stop_queue:%x\n",dev->dev_addr[i]);
netif_stop_queue(dev);
}
spin_unlock_irqrestore(&priv->lock, flags);
return pkt;
}
void snull_release_buffer(struct snull_packet *pkt)
{
unsigned long flags;
struct snull_priv *priv = netdev_priv(pkt->dev);
spin_lock_irqsave(&priv->lock, flags);
pkt->next = priv->ppool;
priv->ppool = pkt;
spin_unlock_irqrestore(&priv->lock, flags);
if (netif_queue_stopped(pkt->dev) && pkt->next == NULL) {
printk("netif_wake_queue\n");
netif_wake_queue(pkt->dev);
}
}
void snull_enqueue_buf(struct net_device *dev, struct snull_packet *pkt)
{
unsigned long flags;
struct snull_priv *priv = netdev_priv(dev);
spin_lock_irqsave(&priv->lock, flags);
pkt->next = priv->rx_queue; /* FIXME - misorders packets */
priv->rx_queue = pkt;
spin_unlock_irqrestore(&priv->lock, flags);
}
struct snull_packet *snull_dequeue_buf(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
struct snull_packet *pkt;
unsigned long flags;
spin_lock_irqsave(&priv->lock, flags);
pkt = priv->rx_queue;
if (pkt != NULL)
priv->rx_queue = pkt->next;
spin_unlock_irqrestore(&priv->lock, flags);
return pkt;
}
/*
* * Enable and disable receive interrupts.
* */
static void snull_rx_ints(struct net_device *dev, int enable)
{
struct snull_priv *priv = netdev_priv(dev);
priv->rx_int_enabled = enable;
}
/*
* Open and close
*/
int snull_open(struct net_device *dev)
{
/* request_region(), request_irq(), .... (like fops->open) */
/*
* Assign the hardware address of the board: use "\0SNULx", where
* x is 0 or 1. The first byte is '\0' to avoid being a multicast
* address (the first byte of multicast addrs is odd).
*/
struct snull_priv *vi = netdev_priv(dev);
netif_carrier_on(dev);
memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN);
if (dev == snull_devs[1])
dev->dev_addr[ETH_ALEN-1]++;
/* \0SNUL1 */
printk("########################\n");
napi_enable(&vi->napi);
//if (napi_schedule_prep(&vi->napi)) {
// __napi_schedule(&vi->napi);
//}
netif_start_queue(dev);
return 0;
}
int snull_release(struct net_device *dev)
{
/* release ports, irq and such -- like fops->close */
struct snull_priv *vi = netdev_priv(dev);
napi_disable(&vi->napi);
netif_stop_queue(dev);/* can't transmit any more */
netif_carrier_off(dev);
return 0;
}
/*
* The poll implementation.
*/
static int snull_poll(struct napi_struct *napi, int budget)
{
struct snull_priv *priv = container_of(napi, struct snull_priv, napi);
struct sk_buff *skb;
unsigned int received = 0;
struct snull_packet *pkt;
//while (received < budget && priv->rx_queue ) {
while ( priv->rx_queue ) {
printk("snull_dequeue_buf:%p@@@@@@@\n", priv->dev);
pkt = snull_dequeue_buf(priv->dev);
skb = dev_alloc_skb(pkt->datalen + 2);
if (! skb) {
if (printk_ratelimit())
printk(KERN_NOTICE "snull: packet dropped\n");
priv->stats.rx_dropped++;
snull_release_buffer(pkt);
continue;
}
skb_reserve(skb, 2); /* align IP on 16B boundary */
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
skb->dev = priv->dev;
skb->protocol = eth_type_trans(skb, priv->dev);
skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
netif_receive_skb(skb);
/* Maintain stats */
priv->stats.rx_packets++;
priv->stats.rx_bytes += pkt->datalen;
snull_release_buffer(pkt);
received++;
}
//if (received < budget || ! priv->rx_queue) {
if ( ! priv->rx_queue) {
printk("napi_complete\n");
napi_complete(napi);
snull_rx_ints(priv->dev, 1);
}
// netif_start_queue(priv->dev);
return received;
}
/*
* A NAPI interrupt handler.
*/
static void snull_napi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
int statusword;
struct snull_priv *priv;
/*
* As usual, check the "device" pointer for shared handlers.
* Then assign "struct device *dev"
*/
struct net_device *dev = (struct net_device *)dev_id;
/* ... and check with hw if it's really ours */
/* paranoid */
if (!dev)
return;
/* Lock the device */
priv = netdev_priv(dev);
spin_lock(&priv->lock);
/* retrieve statusword: real netdevices use I/O instructions */
statusword = priv->status;
priv->status = 0;
if (statusword & SNULL_RX_INTR) {
printk("napi_schedule\n");
if (napi_schedule_prep(&priv->napi)) {
snull_rx_ints(dev, 0); /* Disable further interrupts */
printk("napi_schedule_prep\n");
__napi_schedule(&priv->napi);
}
}
if (statusword & SNULL_TX_INTR) {
/* a transmission is over: free the skb */
priv->stats.tx_packets++;
priv->stats.tx_bytes += priv->tx_packetlen;
dev_kfree_skb(priv->skb);
}
/* Unlock the device and we are done */
spin_unlock(&priv->lock);
return;
}
/*
* Transmit a packet (low level interface)
*/
static void snull_hw_tx(char *buf, int len, struct net_device *dev)
{
/*
* This function deals with hw details. This interface loops
* back the packet to the other snull interface (if any).
* In other words, this function implements the snull behaviour,
* while all other procedures are rather device-independent
*/
struct iphdr *ih;
struct net_device *dest;
struct snull_priv *priv;
u32 *saddr, *daddr;
struct snull_packet *tx_buffer;
/* I am paranoid. Ain't I? */
if (len < sizeof(struct ethhdr) + sizeof(struct iphdr)) {
printk("snull: Hmm... packet too short (%i octets)\n",
len);
return;
}
if (1) { /* enable this conditional to look at the data */
int i;
//PDEBUG("len is %i\n" KERN_DEBUG "data:",len);
printk("len is %i\n" KERN_DEBUG "data:",len);
//for (i=14 ; i<len; i++)
for (i=0 ; i<len; i++)
printk(" %02x",buf[i]&0xff);
printk("\n");
}
/*
* Ethhdr is 14 bytes, but the kernel arranges for iphdr
* to be aligned (i.e., ethhdr is unaligned)
*/
ih = (struct iphdr *)(buf+sizeof(struct ethhdr));
saddr = &ih->saddr;
daddr = &ih->daddr;
//printk("###%08x --> %08x\n", ntohl(ih->saddr), ntohl(ih->daddr));
((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
((u8 *)daddr)[2] ^= 1;
ih->check = 0; /* and rebuild the checksum (ip needs it) */
ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
if (dev == snull_devs[0]) {
printk("dev0:%08x --> %08x\n", ntohl(ih->saddr), ntohl(ih->daddr));
}
//PDEBUGG("%08x:%05i --> %08x:%05i\n",
//printk("%08x:%05i --> %08x:%05i\n",
// ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),
// ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));
else {
printk("dev1:%08x <-- %08x\n", ntohl(ih->daddr), ntohl(ih->saddr));
//printk("%08x:%05i <-- %08x:%05i\n",
// ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest),
// ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source));
}
/*
* Ok, now the packet is ready for transmission: first simulate a
* receive interrupt on the twin device, then a
* transmission-done on the transmitting device
*/
tx_buffer = snull_get_tx_buffer(dev);
tx_buffer->datalen = len;
memcpy(tx_buffer->data, buf, len);
dest = snull_devs[dev == snull_devs[0] ? 1 : 0];
priv = netdev_priv(dest);
snull_enqueue_buf(dest, tx_buffer);
printk("snull_enqueue_buf\n");
if (priv->rx_int_enabled) {
priv->status |= SNULL_RX_INTR;
snull_interrupt(0, dest, NULL);
}
priv = netdev_priv(dev);
priv->tx_packetlen = len;
priv->tx_packetdata = buf;
priv->status |= SNULL_TX_INTR;
snull_interrupt(0, dev, NULL);
}
/*
* Transmit a packet (called by the kernel)
*/
int snull_tx(struct sk_buff *skb, struct net_device *dev)
{
int len;
char *data, shortpkt[ETH_ZLEN];
struct snull_priv *priv = netdev_priv(dev);
data = skb->data;
len = skb->len;
if (len < ETH_ZLEN) {
memset(shortpkt, 0, ETH_ZLEN);
memcpy(shortpkt, skb->data, skb->len);
len = ETH_ZLEN;
data = shortpkt;
}
dev->trans_start = jiffies; /* save the timestamp */
/* Remember the skb, so we can free it at interrupt time */
priv->skb = skb;
/* actual deliver of data is device-specific, and not shown here */
snull_hw_tx(data, len, dev);
return 0; /* Our simple device can not fail */
}
/*
* * Return statistics to the caller
* */
struct net_device_stats *snull_stats(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
return &priv->stats;
}
/*
* * This function is called to fill up an eth header, since arp is not
* * available on the interface
* */
int snull_rebuild_header(struct sk_buff *skb)
{
struct ethhdr *eth = (struct ethhdr *) skb->data;
struct net_device *dev = skb->dev;
memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */
return 0;
}
int snull_header(struct sk_buff *skb, struct net_device *dev,
unsigned short type, void *daddr, void *saddr,
unsigned int len)
{
struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN);
eth->h_proto = htons(type);
memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */
return (dev->hard_header_len);
}
static void snull_set_rx_mode(struct net_device *dev)
{
}
static const struct net_device_ops virtnet_netdev = {
.ndo_open = snull_open,
.ndo_stop = snull_release,
.ndo_start_xmit = snull_tx,
.ndo_get_stats = snull_stats,
.ndo_set_rx_mode = snull_set_rx_mode,
};
static const struct header_ops header_op = {
.create = snull_header,
.rebuild = snull_rebuild_header,
};
/*
* The devices
*/
struct net_device *snull_devs[2];
/*
* Finally, the module stuff
*/
void snull_cleanup(void)
{
int i;
for (i = 0; i < 2; i++) {
if (snull_devs[i]) {
unregister_netdev(snull_devs[i]);
snull_teardown_pool(snull_devs[i]);
free_netdev(snull_devs[i]);
}
}
return;
}
int snull_init_module(void)
{
int result, i, ret = -ENOMEM;
struct snull_priv *vi;
snull_interrupt = snull_napi_interrupt ;
/* Allocate the devices */
snull_devs[0] = alloc_etherdev(sizeof(struct snull_priv));
snull_devs[1] = alloc_etherdev(sizeof(struct snull_priv));
if (!snull_devs[0] || !snull_devs[1])
return -ENOMEM;
for (i = 0; i < 2; i++) {
snull_devs[i]->flags |= IFF_NOARP;
snull_devs[i]->features |= NETIF_F_NO_CSUM;
snull_devs[i]->header_ops = &header_op;
snull_rx_ints(snull_devs[i], 1);
snull_setup_pool(snull_devs[i]);
snull_devs[i]->netdev_ops = &virtnet_netdev;
vi = netdev_priv(snull_devs[i]);
vi->dev = snull_devs[i];
netif_napi_add(snull_devs[i], &vi->napi, snull_poll,2);
}
for (i = 0; i < 2; i++)
if ((result = register_netdev(snull_devs[i])))
printk("snull: error %i registering device \"%s\"\n",
result, snull_devs[i]->name);
else {
ret = 0;
}
if (ret)
snull_cleanup();
return ret;
}
/*
* snull.h -- definitions for the network module
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*/
/*
* Macros to help debugging
*/
#undef PDEBUG /* undef it, just in case */
#ifdef SNULL_DEBUG
# ifdef __KERNEL__
/* This one if debugging is on, and kernel space */
# define PDEBUG(fmt, args...) printk( KERN_DEBUG "snull: " fmt, ## args)
# else
/* This one for user space */
# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
# endif
#else
# define PDEBUG(fmt, args...) /* not debugging: nothing */
#endif
#undef PDEBUGG
#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
/* These are the flags in the statusword */
#define SNULL_RX_INTR 0x0001
#define SNULL_TX_INTR 0x0002
/* Default timeout period */
#define SNULL_TIMEOUT 5 /* In jiffies */
extern struct net_device *snull_devs[];
module_init(snull_init_module);
module_exit(snull_cleanup);
详细原理不介绍, 参照《Linux 设备驱动 Edition 3》中第17章网络驱动. 本文把Linux设备驱动示例代码snull移植到2.6.32版本,对应于发行版redhat6,centos6。
为了提高高端 Linux 系统性能的方法, 目前网络子系统开发一般采用基于查询的接口,所以把snull代码中接收中断去掉了,只使用NAPI。由于对snull比较复杂,便于理解,进行相应裁剪,去掉对功能没有影响代码,接下来对改动地方进行说明。对于修改原因,下面做了简单说明,具体原因测试过程,可以不修改(你觉得无关紧要或者无法确定代码),看看会出现什么问题,这样会理解更深。
注意:
1.本人内核版本:
# uname -r
2.6.32-71.el6
2.发行版:
# cat /etc/redhat-release
CentOS Linux release 6.0 (Final)
一.修改说明:
1.snull_open函数
添加napi_enable(&vi->napi);语句,表示启动napi接受
2.snull_release函数
添加
napi_disable(&vi->napi);表示关闭napi接受
netif_carrier_off(dev);表示关闭载波
3.snull_napi_interrupt函数
由于内核接口,发生变化,以前代码改为如下:
if (napi_schedule_prep(&priv->napi)) {
snull_rx_ints(dev, 0); /* Disable further interrupts */
printk("napi_schedule_prep\n");
__napi_schedule(&priv->napi);
}
4.内核接口t struct net_device_ops发生改变,所以下面进行修改:
static const struct net_device_ops virtnet_netdev = {
.ndo_open = snull_open,
.ndo_stop = snull_release,
.ndo_start_xmit = snull_tx,
.ndo_get_stats = snull_stats,
.ndo_set_rx_mode = snull_set_rx_mode,
};
static const struct header_ops header_op = {
.create = snull_header,
.rebuild = snull_rebuild_header,
};
5.snull_init_module函数改动较大
为了简单,直接用了以太网接口,所以网络接口不是sn0,sn1.
snull_devs[0] = alloc_etherdev(sizeof(struct snull_priv));
snull_devs[1] = alloc_etherdev(sizeof(struct snull_priv));
for (i = 0; i < 2; i++) {
snull_devs[i]->flags |= IFF_NOARP;
snull_devs[i]->features |= NETIF_F_NO_CSUM;
snull_devs[i]->header_ops = &header_op;
snull_rx_ints(snull_devs[i], 1);
snull_setup_pool(snull_devs[i]);
snull_devs[i]->netdev_ops = &virtnet_netdev;
vi = netdev_priv(snull_devs[i]);
vi->dev = snull_devs[i];
netif_napi_add(snull_devs[i], &vi->napi, snull_poll,2);
}
二.测试
1.编译,运行,加载内核模块,查看接口是否创建
#insmod snull.ko
#ifconfig -a
eth1 Link encap:Ethernet HWaddr 00:00:00:00:00:00
BROADCAST NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
eth2 Link encap:Ethernet HWaddr 00:00:00:00:00:00
BROADCAST NOARP MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
2.编辑相应文件
#vi /etc/networks
//添加下面
snullnet0 192.168.0.0
snullnet1 192.168.1.0
/etc/hosts 里面:
#vi /etc/hosts
192.168.0.1 local0
192.168.0.2 remote0
192.168.1.2 local1
192.168.1.1 remote1
3.设置网络接口ip
#ifconfig eth1 local0
#ifconfig eth2 local1
eth1 Link encap:Ethernet HWaddr 00:53:4E:55:4C:30
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c30/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:210 (210.0 b) TX bytes:210 (210.0 b)
eth2 Link encap:Ethernet HWaddr 00:53:4E:55:4C:31
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c31/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:3 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:210 (210.0 b) TX bytes:210 (210.0 b)
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
说明接口正常运行了。
4.数据包发送与接受
查看包接受情况
#ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:53:4E:55:4C:30
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c30/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:7 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:602 (602.0 b) TX bytes:798 (798.0 b)
[root@localhost ~]# ifconfig eth2
eth2 Link encap:Ethernet HWaddr 00:53:4E:55:4C:31
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c31/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:7 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:798 (798.0 b) TX bytes:602 (602.0 b)
发送数据包
# ping -c 2 remote0
PING remote0 (192.168.0.2) 56(84) bytes of data.
64 bytes from remote0 (192.168.0.2): icmp_seq=1 ttl=64 time=0.125 ms
64 bytes from remote0 (192.168.0.2): icmp_seq=2 ttl=64 time=0.211 ms
--- remote0 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.125/0.168/0.211/0.043 ms
# ping -c 2 remote1
PING remote1 (192.168.1.1) 56(84) bytes of data.
64 bytes from remote1 (192.168.1.1): icmp_seq=1 ttl=64 time=0.310 ms
64 bytes from remote1 (192.168.1.1): icmp_seq=2 ttl=64 time=0.216 ms
--- remote1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.216/0.263/0.310/0.047 ms
#再查看包接受情况
eth1 Link encap:Ethernet HWaddr 00:53:4E:55:4C:30
inet addr:192.168.0.1 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c30/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:11 errors:0 dropped:0 overruns:0 frame:0
TX packets:13 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:994 (994.0 b) TX bytes:1190 (1.1 KiB)
eth2 Link encap:Ethernet HWaddr 00:53:4E:55:4C:31
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::253:4eff:fe55:4c31/64 Scope:Link
UP BROADCAST RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1190 (1.1 KiB) TX bytes:994 (994.0 b)
5.卸载模块
# rmmod snull
# ifconfig -a
注意地方:
1.网卡接口名称发生变化
sn----eth
2.包发送情况统计 ndo_get_stats
.ndo_get_stats = snull_stats,
如果该函数不实现,包统计数据无法显示
RX packets:13 errors:0 dropped:0 overruns:0 frame:0
TX packets:11 errors:0 dropped:0 overruns:0 carrier:0
3.ARP地址解析与填充
static const struct header_ops header_op = {
.create = snull_header,
.rebuild = snull_rebuild_header,
};
如果该函数不实现,包无法进行通信的,即两个接口之间。因为包的以太网包头MAC地址不存在或者不正确。
4.网络测试时,要保证 192.168.0.0 ,192.168.1.0两个网段没有使用。
三.代码
/*
* snull.c -- the Simple Network Utility
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*
* $Id: snull.c,v 1.21 2004/11/05 02:36:03 rubini Exp $
*/
#include <linux/module.h>
#include <linux/init.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/kernel.h> /* printk() */
#include <linux/slab.h> /* kmalloc() */
#include <linux/errno.h> /* error codes */
#include <linux/types.h> /* size_t */
#include <linux/interrupt.h> /* mark_bh */
#include <linux/in.h>
#include <linux/netdevice.h> /* struct device, and other headers */
#include <linux/etherdevice.h> /* eth_type_trans */
#include <linux/ip.h> /* struct iphdr */
#include <linux/tcp.h> /* struct tcphdr */
#include <linux/skbuff.h>
#include "snull.h"
#include <linux/in6.h>
#include <asm/checksum.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/ethtool.h>
MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet");
MODULE_LICENSE("Dual BSD/GPL");
/*
* Do we run in NAPI mode?
*/
static int use_napi = 1;
module_param(use_napi, int, 0);
/*
* A structure representing an in-flight packet.
*/
struct snull_packet {
struct snull_packet *next;
struct net_device *dev;
int datalen;
u8 data[ETH_DATA_LEN];
};
int pool_size = 8;
module_param(pool_size, int, 0);
/*
* This structure is private to each device. It is used to pass
* packets in and out, so there is place for a packet
*/
struct snull_priv {
struct net_device_stats stats;
struct net_device *dev;
struct napi_struct napi;
int status;
struct snull_packet *ppool;
struct snull_packet *rx_queue; /* List of incoming packets */
int rx_int_enabled;
int tx_packetlen;
u8 *tx_packetdata;
struct sk_buff *skb;
spinlock_t lock;
};
static void (*snull_interrupt)(int, void *, struct pt_regs *);
/*
* Set up a device's packet pool.
*/
void snull_setup_pool(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
int i;
struct snull_packet *pkt;
priv->ppool = NULL;
for (i = 0; i < pool_size; i++) {
printk("%s\n",__func__);
pkt = kmalloc (sizeof (struct snull_packet), GFP_KERNEL);
if (pkt == NULL) {
printk (KERN_NOTICE "Ran out of memory allocating packet pool\n");
return;
}
pkt->dev = dev;
pkt->next = priv->ppool;
priv->ppool = pkt;
}
printk("%p,%p\n",dev,priv->ppool);
}
void snull_teardown_pool(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
struct snull_packet *pkt;
while ((pkt = priv->ppool)) {
priv->ppool = pkt->next;
kfree (pkt);
/* FIXME - in-flight packets ? */
}
}
/*
* Buffer/pool management.
*/
struct snull_packet *snull_get_tx_buffer(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
unsigned long flags;
struct snull_packet *pkt;
int i=0;
spin_lock_irqsave(&priv->lock, flags);
pkt = priv->ppool;
priv->ppool = pkt->next;
if (priv->ppool == NULL) {
printk (KERN_INFO "Pool empty %p\n",dev);
for(i=0;i<ETH_ALEN;i++)
printk("netif_stop_queue:%x\n",dev->dev_addr[i]);
netif_stop_queue(dev);
}
spin_unlock_irqrestore(&priv->lock, flags);
return pkt;
}
void snull_release_buffer(struct snull_packet *pkt)
{
unsigned long flags;
struct snull_priv *priv = netdev_priv(pkt->dev);
spin_lock_irqsave(&priv->lock, flags);
pkt->next = priv->ppool;
priv->ppool = pkt;
spin_unlock_irqrestore(&priv->lock, flags);
if (netif_queue_stopped(pkt->dev) && pkt->next == NULL) {
printk("netif_wake_queue\n");
netif_wake_queue(pkt->dev);
}
}
void snull_enqueue_buf(struct net_device *dev, struct snull_packet *pkt)
{
unsigned long flags;
struct snull_priv *priv = netdev_priv(dev);
spin_lock_irqsave(&priv->lock, flags);
pkt->next = priv->rx_queue; /* FIXME - misorders packets */
priv->rx_queue = pkt;
spin_unlock_irqrestore(&priv->lock, flags);
}
struct snull_packet *snull_dequeue_buf(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
struct snull_packet *pkt;
unsigned long flags;
spin_lock_irqsave(&priv->lock, flags);
pkt = priv->rx_queue;
if (pkt != NULL)
priv->rx_queue = pkt->next;
spin_unlock_irqrestore(&priv->lock, flags);
return pkt;
}
/*
* * Enable and disable receive interrupts.
* */
static void snull_rx_ints(struct net_device *dev, int enable)
{
struct snull_priv *priv = netdev_priv(dev);
priv->rx_int_enabled = enable;
}
/*
* Open and close
*/
int snull_open(struct net_device *dev)
{
/* request_region(), request_irq(), .... (like fops->open) */
/*
* Assign the hardware address of the board: use "\0SNULx", where
* x is 0 or 1. The first byte is '\0' to avoid being a multicast
* address (the first byte of multicast addrs is odd).
*/
struct snull_priv *vi = netdev_priv(dev);
netif_carrier_on(dev);
memcpy(dev->dev_addr, "\0SNUL0", ETH_ALEN);
if (dev == snull_devs[1])
dev->dev_addr[ETH_ALEN-1]++;
/* \0SNUL1 */
printk("########################\n");
napi_enable(&vi->napi);
//if (napi_schedule_prep(&vi->napi)) {
// __napi_schedule(&vi->napi);
//}
netif_start_queue(dev);
return 0;
}
int snull_release(struct net_device *dev)
{
/* release ports, irq and such -- like fops->close */
struct snull_priv *vi = netdev_priv(dev);
napi_disable(&vi->napi);
netif_stop_queue(dev);/* can't transmit any more */
netif_carrier_off(dev);
return 0;
}
/*
* The poll implementation.
*/
static int snull_poll(struct napi_struct *napi, int budget)
{
struct snull_priv *priv = container_of(napi, struct snull_priv, napi);
struct sk_buff *skb;
unsigned int received = 0;
struct snull_packet *pkt;
//while (received < budget && priv->rx_queue ) {
while ( priv->rx_queue ) {
printk("snull_dequeue_buf:%p@@@@@@@\n", priv->dev);
pkt = snull_dequeue_buf(priv->dev);
skb = dev_alloc_skb(pkt->datalen + 2);
if (! skb) {
if (printk_ratelimit())
printk(KERN_NOTICE "snull: packet dropped\n");
priv->stats.rx_dropped++;
snull_release_buffer(pkt);
continue;
}
skb_reserve(skb, 2); /* align IP on 16B boundary */
memcpy(skb_put(skb, pkt->datalen), pkt->data, pkt->datalen);
skb->dev = priv->dev;
skb->protocol = eth_type_trans(skb, priv->dev);
skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
netif_receive_skb(skb);
/* Maintain stats */
priv->stats.rx_packets++;
priv->stats.rx_bytes += pkt->datalen;
snull_release_buffer(pkt);
received++;
}
//if (received < budget || ! priv->rx_queue) {
if ( ! priv->rx_queue) {
printk("napi_complete\n");
napi_complete(napi);
snull_rx_ints(priv->dev, 1);
}
// netif_start_queue(priv->dev);
return received;
}
/*
* A NAPI interrupt handler.
*/
static void snull_napi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
{
int statusword;
struct snull_priv *priv;
/*
* As usual, check the "device" pointer for shared handlers.
* Then assign "struct device *dev"
*/
struct net_device *dev = (struct net_device *)dev_id;
/* ... and check with hw if it's really ours */
/* paranoid */
if (!dev)
return;
/* Lock the device */
priv = netdev_priv(dev);
spin_lock(&priv->lock);
/* retrieve statusword: real netdevices use I/O instructions */
statusword = priv->status;
priv->status = 0;
if (statusword & SNULL_RX_INTR) {
printk("napi_schedule\n");
if (napi_schedule_prep(&priv->napi)) {
snull_rx_ints(dev, 0); /* Disable further interrupts */
printk("napi_schedule_prep\n");
__napi_schedule(&priv->napi);
}
}
if (statusword & SNULL_TX_INTR) {
/* a transmission is over: free the skb */
priv->stats.tx_packets++;
priv->stats.tx_bytes += priv->tx_packetlen;
dev_kfree_skb(priv->skb);
}
/* Unlock the device and we are done */
spin_unlock(&priv->lock);
return;
}
/*
* Transmit a packet (low level interface)
*/
static void snull_hw_tx(char *buf, int len, struct net_device *dev)
{
/*
* This function deals with hw details. This interface loops
* back the packet to the other snull interface (if any).
* In other words, this function implements the snull behaviour,
* while all other procedures are rather device-independent
*/
struct iphdr *ih;
struct net_device *dest;
struct snull_priv *priv;
u32 *saddr, *daddr;
struct snull_packet *tx_buffer;
/* I am paranoid. Ain't I? */
if (len < sizeof(struct ethhdr) + sizeof(struct iphdr)) {
printk("snull: Hmm... packet too short (%i octets)\n",
len);
return;
}
if (1) { /* enable this conditional to look at the data */
int i;
//PDEBUG("len is %i\n" KERN_DEBUG "data:",len);
printk("len is %i\n" KERN_DEBUG "data:",len);
//for (i=14 ; i<len; i++)
for (i=0 ; i<len; i++)
printk(" %02x",buf[i]&0xff);
printk("\n");
}
/*
* Ethhdr is 14 bytes, but the kernel arranges for iphdr
* to be aligned (i.e., ethhdr is unaligned)
*/
ih = (struct iphdr *)(buf+sizeof(struct ethhdr));
saddr = &ih->saddr;
daddr = &ih->daddr;
//printk("###%08x --> %08x\n", ntohl(ih->saddr), ntohl(ih->daddr));
((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
((u8 *)daddr)[2] ^= 1;
ih->check = 0; /* and rebuild the checksum (ip needs it) */
ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
if (dev == snull_devs[0]) {
printk("dev0:%08x --> %08x\n", ntohl(ih->saddr), ntohl(ih->daddr));
}
//PDEBUGG("%08x:%05i --> %08x:%05i\n",
//printk("%08x:%05i --> %08x:%05i\n",
// ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source),
// ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest));
else {
printk("dev1:%08x <-- %08x\n", ntohl(ih->daddr), ntohl(ih->saddr));
//printk("%08x:%05i <-- %08x:%05i\n",
// ntohl(ih->daddr),ntohs(((struct tcphdr *)(ih+1))->dest),
// ntohl(ih->saddr),ntohs(((struct tcphdr *)(ih+1))->source));
}
/*
* Ok, now the packet is ready for transmission: first simulate a
* receive interrupt on the twin device, then a
* transmission-done on the transmitting device
*/
tx_buffer = snull_get_tx_buffer(dev);
tx_buffer->datalen = len;
memcpy(tx_buffer->data, buf, len);
dest = snull_devs[dev == snull_devs[0] ? 1 : 0];
priv = netdev_priv(dest);
snull_enqueue_buf(dest, tx_buffer);
printk("snull_enqueue_buf\n");
if (priv->rx_int_enabled) {
priv->status |= SNULL_RX_INTR;
snull_interrupt(0, dest, NULL);
}
priv = netdev_priv(dev);
priv->tx_packetlen = len;
priv->tx_packetdata = buf;
priv->status |= SNULL_TX_INTR;
snull_interrupt(0, dev, NULL);
}
/*
* Transmit a packet (called by the kernel)
*/
int snull_tx(struct sk_buff *skb, struct net_device *dev)
{
int len;
char *data, shortpkt[ETH_ZLEN];
struct snull_priv *priv = netdev_priv(dev);
data = skb->data;
len = skb->len;
if (len < ETH_ZLEN) {
memset(shortpkt, 0, ETH_ZLEN);
memcpy(shortpkt, skb->data, skb->len);
len = ETH_ZLEN;
data = shortpkt;
}
dev->trans_start = jiffies; /* save the timestamp */
/* Remember the skb, so we can free it at interrupt time */
priv->skb = skb;
/* actual deliver of data is device-specific, and not shown here */
snull_hw_tx(data, len, dev);
return 0; /* Our simple device can not fail */
}
/*
* * Return statistics to the caller
* */
struct net_device_stats *snull_stats(struct net_device *dev)
{
struct snull_priv *priv = netdev_priv(dev);
return &priv->stats;
}
/*
* * This function is called to fill up an eth header, since arp is not
* * available on the interface
* */
int snull_rebuild_header(struct sk_buff *skb)
{
struct ethhdr *eth = (struct ethhdr *) skb->data;
struct net_device *dev = skb->dev;
memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
memcpy(eth->h_dest, dev->dev_addr, dev->addr_len);
eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */
return 0;
}
int snull_header(struct sk_buff *skb, struct net_device *dev,
unsigned short type, void *daddr, void *saddr,
unsigned int len)
{
struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN);
eth->h_proto = htons(type);
memcpy(eth->h_source, saddr ? saddr : dev->dev_addr, dev->addr_len);
memcpy(eth->h_dest, daddr ? daddr : dev->dev_addr, dev->addr_len);
eth->h_dest[ETH_ALEN-1] ^= 0x01; /* dest is us xor 1 */
return (dev->hard_header_len);
}
static void snull_set_rx_mode(struct net_device *dev)
{
}
static const struct net_device_ops virtnet_netdev = {
.ndo_open = snull_open,
.ndo_stop = snull_release,
.ndo_start_xmit = snull_tx,
.ndo_get_stats = snull_stats,
.ndo_set_rx_mode = snull_set_rx_mode,
};
static const struct header_ops header_op = {
.create = snull_header,
.rebuild = snull_rebuild_header,
};
/*
* The devices
*/
struct net_device *snull_devs[2];
/*
* Finally, the module stuff
*/
void snull_cleanup(void)
{
int i;
for (i = 0; i < 2; i++) {
if (snull_devs[i]) {
unregister_netdev(snull_devs[i]);
snull_teardown_pool(snull_devs[i]);
free_netdev(snull_devs[i]);
}
}
return;
}
int snull_init_module(void)
{
int result, i, ret = -ENOMEM;
struct snull_priv *vi;
snull_interrupt = snull_napi_interrupt ;
/* Allocate the devices */
snull_devs[0] = alloc_etherdev(sizeof(struct snull_priv));
snull_devs[1] = alloc_etherdev(sizeof(struct snull_priv));
if (!snull_devs[0] || !snull_devs[1])
return -ENOMEM;
for (i = 0; i < 2; i++) {
snull_devs[i]->flags |= IFF_NOARP;
snull_devs[i]->features |= NETIF_F_NO_CSUM;
snull_devs[i]->header_ops = &header_op;
snull_rx_ints(snull_devs[i], 1);
snull_setup_pool(snull_devs[i]);
snull_devs[i]->netdev_ops = &virtnet_netdev;
vi = netdev_priv(snull_devs[i]);
vi->dev = snull_devs[i];
netif_napi_add(snull_devs[i], &vi->napi, snull_poll,2);
}
for (i = 0; i < 2; i++)
if ((result = register_netdev(snull_devs[i])))
printk("snull: error %i registering device \"%s\"\n",
result, snull_devs[i]->name);
else {
ret = 0;
}
if (ret)
snull_cleanup();
return ret;
}
/*
* snull.h -- definitions for the network module
*
* Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2001 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*/
/*
* Macros to help debugging
*/
#undef PDEBUG /* undef it, just in case */
#ifdef SNULL_DEBUG
# ifdef __KERNEL__
/* This one if debugging is on, and kernel space */
# define PDEBUG(fmt, args...) printk( KERN_DEBUG "snull: " fmt, ## args)
# else
/* This one for user space */
# define PDEBUG(fmt, args...) fprintf(stderr, fmt, ## args)
# endif
#else
# define PDEBUG(fmt, args...) /* not debugging: nothing */
#endif
#undef PDEBUGG
#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
/* These are the flags in the statusword */
#define SNULL_RX_INTR 0x0001
#define SNULL_TX_INTR 0x0002
/* Default timeout period */
#define SNULL_TIMEOUT 5 /* In jiffies */
extern struct net_device *snull_devs[];
module_init(snull_init_module);
module_exit(snull_cleanup);