网卡驱动程序框架:
app: socket
--------------------------------------------------
---------------
--------------- 若干层网络协议--纯软件
---------------
---------------
hard_start_xmit || /
/ || netif_rx sk_buff
-----------------------------------------------------------------------
硬件相关的驱动程序(要提供hard_start_xmit, 有数据时要用netif_rx上报)
--------------------------------------------------
硬件
怎么写网卡驱动程序?
1. 分配一个net_device结构体
2. 设置:
2.1 发包函数: hard_start_xmit
2.2 收到数据时(在中断处理函数里)用netif_rx上报数据
2.3 其他设置
3. 注册: register_netdevice
-----------------------------------------------------------------------------------------------------------
1. 分配一个net_device结构体
/* 1. 分配一个net_device结构体 */
vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);; /* alloc_etherdev */
------------------------------------------------------------------------------------------------------------
2. 设置:
2.1 发包函数: hard_start_xmit
vnet_dev->hard_start_xmit = virt_net_send_packet;
2.2 收到数据时(在中断处理函数里)用netif_rx上报数据
/* 构造一个假的sk_buff,上报 数据给上一层 */
emulator_rx_packet(skb, dev);
{
/ 从硬件读出/保存数据
/* 对调"源/目的"的mac地址 */
/* 对调"源/目的"的ip地址 */
// 构造一个sk_buff
// 提交sk_buff
netif_rx(rx_skb);
}
2.3 其他设置
/* 设置MAC地址 */
vnet_dev->dev_addr[0] = 0x08;
vnet_dev->dev_addr[1] = 0x89;
vnet_dev->dev_addr[2] = 0x89;
vnet_dev->dev_addr[3] = 0x89;
vnet_dev->dev_addr[4] = 0x89;
vnet_dev->dev_addr[5] = 0x11;
/* 设置下面两项才能ping通 */
vnet_dev->flags |= IFF_NOARP;
vnet_dev->features |= NETIF_F_NO_CSUM;
--------------------------------------------------------------------------------------------------------------
3. 注册: register_netdevice
register_netdev(vnet_dev);
测试1th/2th:
1. insmod virt_net.ko
2. ifconfig vnet0 3.3.3.3
ifconfig // 查看
3. ping 3.3.3.3 // 成功
ping 3.3.3.4 // 死机
测试DM9000C驱动程序:
1. 把dm9dev9000c.c放到内核的drivers/net目录下
2. 修改drivers/net/Makefile
把
obj-$(CONFIG_DM9000) += dm9000.o
改为
obj-$(CONFIG_DM9000) += dm9dev9000c.o
3. make uImage
使用新内核启动
4.
使用NFS启动
或
ifconfig eth0 192.168.1.17
ping 192.168.1.1
-------------------------------------------------------------------------------------------------------------------------
相关网卡参考驱动代码
virt_net.c
/* * 参考 drivers etcs89x0.c */ #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> static struct net_device *vnet_dev; 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 ", *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 int virt_net_send_packet(struct sk_buff *skb, struct net_device *dev) { static int cnt = 0; printk("virt_net_send_packet cnt = %d ", ++cnt); /* 对于真实的网卡, 把skb里的数据通过网卡发送出去 */ netif_stop_queue(dev); /* 停止该网卡的队列 */ /* ...... */ /* 把skb的数据写入网卡 */ /* 构造一个假的sk_buff,上报 */ emulator_rx_packet(skb, dev); dev_kfree_skb (skb); /* 释放skb */ netif_wake_queue(dev); /* 数据全部发送出去后,唤醒网卡的队列 */ /* 更新统计信息 */ dev->stats.tx_packets++; dev->stats.tx_bytes += skb->len; return 0; } static int virt_net_init(void) { /* 1. 分配一个net_device结构体 */ vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);; /* alloc_etherdev */ /* 2. 设置 */ vnet_dev->hard_start_xmit = virt_net_send_packet; /* 设置MAC地址 */ vnet_dev->dev_addr[0] = 0x08; vnet_dev->dev_addr[1] = 0x89; vnet_dev->dev_addr[2] = 0x89; vnet_dev->dev_addr[3] = 0x89; vnet_dev->dev_addr[4] = 0x89; vnet_dev->dev_addr[5] = 0x11; /* 设置下面两项才能ping通 */ vnet_dev->flags |= IFF_NOARP; vnet_dev->features |= NETIF_F_NO_CSUM; /* 3. 注册 */ //register_netdevice(vnet_dev); register_netdev(vnet_dev); return 0; } static void virt_net_exit(void) { unregister_netdev(vnet_dev); free_netdev(vnet_dev); } module_init(virt_net_init); module_exit(virt_net_exit); MODULE_AUTHOR("thisway.diy@163.com,17653039@qq.com"); MODULE_LICENSE("GPL");
Makefile
KERN_DIR = /work/system/linux-2.6.22.6 all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.order obj-m += virt_net.o