linux驅動摸索 --網絡驅動(虛擬網卡)

內核版本:linux-2.6.32.2
開發板:mini2440


網絡驅動程序框架:

1. 分配一個net_device結構體
2. 設置:
2.1 發包函數: hard_start_xmit
2.2 收到數據時(在中斷處理函數裏)用netif_rx上報數據
2.3 其他設置
3. 註冊: register_netdevice

測試源碼:

#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>

static struct net_device * virt_net_dev;
static netdev_tx_t virt_net_send_packet(struct sk_buff *skb,struct net_device *dev);



static const struct net_device_ops virt_net_ops = {
	.ndo_start_xmit 	= virt_net_send_packet,
};

static void emulator_rx_packet(struct sk_buff *skb, struct net_device *dev)
{
	/* 參考LDD3 */
	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;
		
	// 從硬件讀出/保存數據
	/* 對調"源/目的"的mac地址 */
	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);

	/* 對調"源/目的"的ip地址 */    
	ih = (struct iphdr *)(skb->data + sizeof(struct ethhdr));
	saddr = &ih->saddr;
	daddr = &ih->daddr;

	tmp = *saddr;
	*saddr = *daddr;
	*daddr = tmp;
	
	//((u8 *)saddr)[2] ^= 1; /* change the third octet (class C) */
	//((u8 *)daddr)[2] ^= 1;
	type = skb->data + sizeof(struct ethhdr) + sizeof(struct iphdr);
	//printk("tx package type = %02x\n", *type);
	// 修改類型, 原來0x8表示ping
	*type = 0; /* 0表示reply */
	
	ih->check = 0;		   /* and rebuild the checksum (ip needs it) */
	ih->check = ip_fast_csum((unsigned char *)ih,ih->ihl);
	
	// 構造一個sk_buff
	rx_skb = dev_alloc_skb(skb->len + 2);
	skb_reserve(rx_skb, 2); /* align IP on 16B boundary */	
	memcpy(skb_put(rx_skb, skb->len), skb->data, skb->len);

	/* Write metadata, and then pass to the receive level */
	rx_skb->dev = dev;
	rx_skb->protocol = eth_type_trans(rx_skb, dev);
	rx_skb->ip_summed = CHECKSUM_UNNECESSARY; /* don't check it */
	dev->stats.rx_packets++;
	dev->stats.rx_bytes += skb->len;

	// 提交sk_buff
	netif_rx(rx_skb);
}

static netdev_tx_t virt_net_send_packet(struct sk_buff *skb,struct net_device *dev)
{
	//static int cnt;
	//printk("virt_net has send %d\n",++cnt);
	/* 對於真實的網卡, 把skb裏的數據通過網卡發送出去 */
	netif_stop_queue(dev); /* 停止該網卡的隊列 */
    /* ...... */           /* 把skb的數據寫入網卡 */

	/* 構造一個假的sk_buff,上報 */
	emulator_rx_packet(skb, dev);

	dev_kfree_skb (skb);   /* 釋放skb */
	netif_wake_queue(dev); /* 數據全部發送出去後,喚醒網卡的隊列 */
	virt_net_dev->stats.tx_packets++;
	virt_net_dev->stats.tx_bytes +=skb->len;
	return 0;
}

void virt_net_init(struct net_device *dev)
{
	ether_setup(dev);
	dev->netdev_ops= &virt_net_ops;
	

 	/* 2. 設置 */
	/* 設置MAC地址 */
    dev->dev_addr[0] = 0x08;
    dev->dev_addr[1] = 0x89;
    dev->dev_addr[2] = 0x89;
    dev->dev_addr[3] = 0x89;
    dev->dev_addr[4] = 0x89;
    dev->dev_addr[5] = 0x11;

    /* 設置下面兩項才能ping通 */
	dev->flags           |= IFF_NOARP;
	dev->features        |= NETIF_F_NO_CSUM;
}



static int __init s3c_virt_net_init(void)
{
	int ret;

	/* Init network device */
	virt_net_dev = alloc_netdev(0, "vnet%d", virt_net_init);
	if (!virt_net_dev) {
		printk("could not allocate device.\n");
		return -ENOMEM;
	}

	
	ret = register_netdev(virt_net_dev);

		
	return ret;

}



static void s3c_virt_net_exit(void)
{
	unregister_netdev(virt_net_dev);
	free_netdev(virt_net_dev);
	
}



module_init(s3c_virt_net_init);
module_exit(s3c_virt_net_exit);

MODULE_LICENSE("GPL");

分析:

首先是分配一個net_device結構體,用函數alloc_netdev分配,其原型爲:

struct net_device *alloc_netdev(
	int sizeof_priv,
	const char *mask,
	void (*setup)(struct net_device *))
sizeof_priv表示私有空間大小,如果不需要的話,設置爲0,mask是接口名字,其在用戶空間可見,這個名字使用printf中%d的格式,內核將下一個可用接口號代替%d,setup是一個初始化函數,用來設置net_devicess結構剩餘部分。

在參考cs89x0.c的代碼中,我們發現他用struct net_device *dev = alloc_etherdev(sizeof(struct net_local));來分配結構,在linux內核中已經爲不同的接口封裝了許多接口,進一步,我們發現他最後還是會調用alloc_netdev,默認使用“eth%d”作爲網卡名字,並調用ether_setup函數,在ether_setup函數中,已經設置了許多默認的參數。

在我的代碼中,用virt_net_init函數作爲setup的初始化函數,簡要介紹下函數的操作。首先調用ether_setup函數,配置內核網卡的一些默認參數。之後dev->netdev_ops= &virt_net_ops,在我使用的內核版本中,dev_devices結構體中對於處理函數函數成員,專門定義了const struct net_device_ops *netdev_ops;原型如下:

struct net_device_ops {
	int			(*ndo_init)(struct net_device *dev);
	void			(*ndo_uninit)(struct net_device *dev);
	int			(*ndo_open)(struct net_device *dev);
	int			(*ndo_stop)(struct net_device *dev);
	netdev_tx_t		(*ndo_start_xmit) (struct sk_buff *skb,
						   struct net_device *dev);
	u16			(*ndo_select_queue)(struct net_device *dev,
						    struct sk_buff *skb);
..............

於是爲hard_start_xmit分配了調用函數virt_net_send_packet。

接着設置了虛擬網卡的MAC地址。最後看到有dev->flags  |= IFF_NOARP;   表明接口不能使用地址解析協議(ARP)ARP是以太網底層協議,他的作用是將IP地址轉化爲以太網的MAC地址,由於我們只是模擬網卡,沒必要應答ARP請求。

初始化全部結束後,調用 register_netdevice,就可以調用驅動程序操作設備了。

測試的話,如下圖:









發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章