网卡的驱动很简单,就是填充net_device结构体,其应用层到网络协议层内核已经完成了,我们的工作就是填写这个net_device,然后注册就可以了。
修正一下:上面第三步应该是:register_netdev
下面代码实现一个虚拟网卡,这里没有实际的网卡,只是把发出去的数据模拟远程回复过来,把数据转发到接收缓存里面,也就是回环操作。
-
-
-
- #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)
- {
-
- 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 virt_net_send_packet(struct sk_buff *skb, struct net_device *dev)
- {
- static int cnt = 0;
- printk("virt_net_send_packet cnt = %d\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 virt_net_init(void)
- {
-
- vnet_dev = alloc_netdev(0, "vnet%d", ether_setup);;
-
-
- vnet_dev->hard_start_xmit = virt_net_send_packet;
-
-
- 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;
-
-
- vnet_dev->flags |= IFF_NOARP;
- vnet_dev->features |= NETIF_F_NO_CSUM;
-
-
- 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_LICENSE("GPL");
上面代码里面主要工作在于实现回环,把发送的数据转给接收,再上报。数据放在net_device里面的data,那么这段数据的格式是什么?
MAC头:
- struct ethhdr {
- unsigned char h_dest[ETH_ALEN];
- unsigned char h_source[ETH_ALEN];
- __be16 h_proto;
- } __attribute__((packed));
IP头:
- struct iphdr {
- #if defined(__LITTLE_ENDIAN_BITFIELD)
- __u8 ihl:4,
- version:4;
- #elif defined (__BIG_ENDIAN_BITFIELD)
- __u8 version:4,
- ihl:4;
- #else
- #error "Please fix <asm/byteorder.h>"
- #endif
- __u8 tos;
- __be16 tot_len;
- __be16 id;
- __be16 frag_off;
- __u8 ttl;
- __u8 protocol;
- __sum16 check;
- __be32 saddr;
- __be32 daddr;
-
- };
上面代码编译,insmod做实验:
ifconfig vnet0 3.3.3.3 up
ping 3.3.3.3 :发现可以ping通
ping 3.3.3.4 :发现也可以ping通,而且收包状态和发包状态都有变化
综合上面可以知道,网络驱动核心就是两个函数:hard_start_xmit和netifrx
这两个函数里面需要操作实际网卡发送和接收数据,一般网络芯片都会提供驱动程序,我们的核心工作就是移植这个驱动。