驱动程序之_3_网络设备_2_虚拟网卡驱动编写

驱动程序之_3_网络设备_2_虚拟网卡驱动编写

参考drivers/net/cs89x0.c(实际网卡驱动)编写一个虚拟网卡驱动,在ping其他地址时,虚拟网卡驱动需要模拟一个数据包,实现自发自收

入口函数中

1、分配net_device

2、设置net_device(mac地址、发包函数等)

3、注册net_device

完善功能函数

模拟收发数据包(由于是虚拟网卡,没有实际硬件,需要软件模拟发送,参考《Linux设备驱动开发详解》编写)

测试方法:

1、设置虚拟网卡

ifconfig vnet0 1.1.1.1

2、查看网卡信息

ifconfig

3、ping自己

ping 1.1.1.1

ifconfig

4、ping同一网段其他地址

ping 1.1.1.5

ifconfig

附上完整程序

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/init.h>
#include <linux/bitops.h>
#include <linux/delay.h>
#include <linux/ip.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>


struct net_device *vnet_dev;

static void emulator_rx_packet(struct sk_buff *skb, struct net_device *dev)
{
	unsigned char *type;
	struct iphdr *ih;
	__be32 *saddr, *daddr, tmp;
	unsigned char	tmp_dev_addr[ETH_ALEN];
	struct ethhdr *ethhdr;
	
	struct sk_buff *rx_skb;
		
	ethhdr = (struct ethhdr *)skb->data;
	memcpy(tmp_dev_addr, ethhdr->h_dest, ETH_ALEN);
	memcpy(ethhdr->h_dest, ethhdr->h_source, ETH_ALEN);
	memcpy(ethhdr->h_source, tmp_dev_addr, ETH_ALEN);

	ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
	saddr = &ih->saddr;
	daddr = &ih->daddr;

	tmp = *saddr;
	*saddr = *daddr;
	*daddr = tmp;
	
	type = skb->data + sizeof(struct ethhdr) + sizeof(struct iphdr);
	
	*type = 0; 
	
	ih->check = 0;		  
	ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
	
	rx_skb = dev_alloc_skb(skb->len + 2);
	skb_reserve(rx_skb, 2); 
	memcpy(skb_put(rx_skb, skb->len), skb->data, skb->len);

	rx_skb->dev = dev;
	rx_skb->protocol = eth_type_trans(rx_skb, dev);
	rx_skb->ip_summed = CHECKSUM_UNNECESSARY;
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += skb->len;
	
	netif_rx(rx_skb);
}

static int	vnet_send_packet(struct sk_buff *skb, struct net_device *dev)
{
	static int cnt;

	printk("vnet_send_packet cnt = %d\r\n",++cnt);

	netif_stop_queue(dev);
	
	emulator_rx_packet(skb, dev);

	dev_kfree_skb (skb);
	netif_wake_queue(dev);
	
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;

	
	return 0;
}

static int vnet_init(void)
{
	int error;

	vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);	
	if(!vnet_dev)
	{
		printk("alloc_netdev error\r\n");
		return -1;
	}
	vnet_dev->hard_start_xmit 	= vnet_send_packet;		
	vnet_dev->dev_addr[0] = 0x6;
	vnet_dev->dev_addr[1] = 0x5;
	vnet_dev->dev_addr[2] = 0x4;
	vnet_dev->dev_addr[3] = 0x3;
	vnet_dev->dev_addr[4] = 0x2;
	vnet_dev->dev_addr[5] = 0x1;
		
	error = register_netdev(vnet_dev);
	if(error)
	{
		printk("register_netdev error\r\n");	
		free_netdev(vnet_dev);
		return -1;
	}

	return 0;
}

static void vnet_exit(void)
{
	unregister_netdev(vnet_dev);
	free_netdev(vnet_dev);
}

module_init(vnet_init);
module_exit(vnet_exit);
MODULE_AUTHOR("Long,[email protected]");
MODULE_LICENSE("GPL");
发布了71 篇原创文章 · 获赞 4 · 访问量 7239

猜你喜欢

转载自blog.csdn.net/floatinglong/article/details/86592687