linux驱动摸索 --网络驱动(虚拟网卡)
2014-07-26 10:36
246 查看
内核版本: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,就可以调用驱动程序操作设备了。
测试的话,如下图:
开发板: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,就可以调用驱动程序操作设备了。
测试的话,如下图:
相关文章推荐
- 【Linux device driver】网络设备驱动注意的问题(三)
- 第16章 Linux网络设备驱动
- linux 网络设备驱动-DM9000
- Linux网络设备驱动结构
- Linux网络驱动源码分析(二)
- NAPI 技术在 Linux 网络驱动上的应用和完善
- linux网络设备应用与驱动编程学习4——模板与实例(A)
- 浅谈 Linux 内核开发之网络设备驱动
- 浅谈 Linux 内核开发之网络设备驱动[转]
- linux网络设备应用与驱动编程学习笔记(2)——网络驱动基础
- NAPI 技术在 Linux 网络驱动上的应用和完善
- 浅谈 Linux 内核开发之网络设备驱动
- 解决Linux网络问题,Linux安装网卡驱动(转:http://www.158558.cn/jianzhanchangshi/2010-04-16/33.html)
- Linux网络驱动开发
- Linux网络设备驱动编程
- Linux网络设备驱动结构概述
- Linux网络设备驱动学习笔记(-)
- [转] NAPI 技术在 Linux 网络驱动上的应用和完善
- linux网络设备应用与驱动编程学习3——lpc3250以太网控制器