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

本文介绍如何基于实际网卡驱动(cs89x0.c)编写虚拟网卡驱动,实现自发自收的数据包模拟,适用于ping测试。文章详细描述了驱动程序的入口函数,包括net_device的分配、设置和注册过程,以及模拟收发数据包的功能函数。

驱动程序之_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,7365151xx@qq.com");
MODULE_LICENSE("GPL");

 

 

虚拟网卡驱动源代码(原版): /* * snull.c -- the Simple Network Utility * * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet * Copyright (C) 2001 O&#39;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&#39;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 #include #include #include #include #include /* printk() */ #include /* kmalloc() */ #include /* error codes */ #include /* size_t */ #include /* mark_bh */ #include #include /* struct device, and other headers */ #include /* eth_type_trans */ #include /* struct iphdr */ #include /* struct tcphdr */ #include #include "snull.h" #include #include MODULE_AUTHOR("Alessandro Rubini, Jonathan Corbet"); MODULE_LICENSE("Dual BSD/GPL"); /* * Transmitter lockup simulation, normally disabled. */ static int lockup = 0; module_param(lockup, int, 0); static int timeout = SNULL_TIMEOUT; module_param(timeout, int, 0); /* * Do we run in NAPI mode? */ static int use_napi = 0; 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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值