/ ************************************************** ******************************************
*本文是个人学习记录。如果有任何错误,请纠正我。
*本文参考:
*
*
*************************************************** ****************************************** /
1.网络设备驱动程序框架概述
Linux网络设备驱动程序体系结构分为四层:网络协议接口层,网络设备接口层,提供实际功能的设备驱动程序层以及网络设备和媒体层。
([1)网络协议接口层
网络协议接口层为网络层协议提供了统一的数据包接收和发送接口。无论高层协议是ARP还是IP,它都通过dev_queue_xmit()函数发送数据,并通过netif_rx()函数接收数据。该层的存在使上层协议独立于特定设备。
([2)网络设备接口层
由网络设备接口层提供给协议接口层的结构net_device用于描述特定网络设备的属性和操作。此结构是设备驱动程序功能层的功能的容器。
([3)设备驱动程序功能层
设备驱动程序功能层的每个功能都是网络设备接口层的net_device数据结构的特定成员。它是驱动网络设备硬件完成相应操作的程序。它通过nto_start_xmit()函数开始发送操作,并在网络设备上传递中断。触发接收操作。
([4)网络设备和媒体层
网络设备和媒体层完成数据包的发送和接收的物理实体,包括网络适配器和特定的传输媒体。网络适配器由设备驱动程序功能层中的功能物理驱动。
驱动程序工程师的工作:在设计特定的网络设备驱动程序时,需要完成的主要工作是编写设备驱动程序功能层的相关功能,以填充net_device数据结构的内容并注册。 net_device进入内核。

2.相关数据结构
([1) struct sk_buff
sk_buff是网络驱动框架中的信息载体,并且是网络分层模型中数据的逐层打包和解包载体。
427 struct sk_buff { 428 /* These two members must be first. */ 429 struct sk_buff *next; //sk_buff是双向链表,所以有前去后继,这是指向后面的sk_buff结构体指针 430 struct sk_buff *prev; //这是指向前一个sk_buff结构体指针 432 ktime_t tstamp; 434 struct sock *sk; 435 struct net_device *dev; //对应的net_device 443 char cb[48] __aligned(8); 445 unsigned long _skb_refdst; 449 unsigned int len, //表示数据区的长度(tail-data)与分片结构体数据区的长度之和 450 data_len;//只表示分片结构体数据区的长度,所以len=(tail - data) + data_len 451 __u16 mac_len, //mac报头的长度 452 hdr_len; 473 __be16 protocol;//包的协议类型,标识是IP包还是ARP包还是其他数据包 534 __u16 inner_transport_header; 535 __u16 inner_network_header; 536 __u16 inner_mac_header; 537 __u16 transport_header; //指向传输包头 538 __u16 network_header; //指向传输层包头 539 __u16 mac_header; //指向链路层包头 540 /* These elements must be at the end, see alloc_skb() for details. */ 541 sk_buff_data_t tail; //指向当前数据包的尾地址, 随着各个网络层的加工而变化 542 sk_buff_data_t end; //数据缓冲区的结束地址 543 unsigned char *head, //数据缓冲区的开始地址 544 *data; //data指向当前数据包的首地址, 随着各个网路层的加工而变化 545 unsigned int truesize; 546 atomic_t users; 547 };

struct sk_buff
([2) struct net_device
在Linux内核中,net_device用于描述网络设备。 net_device是设备接口层的核心,也是编写网络驱动程序核心的对象。
1160 struct net_device { 1167 char name[IFNAMSIZ];//网络设备的名称, 网络设备被载入后会出现在ifconfig中, 比如默认的eth0就是这个 1179 unsigned long mem_end; //网络设备所使用的共享内存起始地址 1180 unsigned long mem_start; //网络设备所使用的共享内存结束地址 1181 unsigned long base_addr; //表示网络设备的IO基地址 1182 int irq; //设备使用的中断号 1189 unsigned long state; 1190 1191 struct list_head dev_list; 1192 struct list_head napi_list; 1193 struct list_head unreg_list; 1194 struct list_head close_list; 1210 netdev_features_t features; //用户层可以修改的特征 1212 netdev_features_t hw_features; //用户层不能修改的特征 1214 netdev_features_t wanted_features; 1243 const struct net_device_ops *netdev_ops;//网络设备的操作方法集 1244 const struct ethtool_ops *ethtool_ops; //ethtool的方法集 1245 const struct forwarding_accel_ops *fwd_ops; 1248 const struct header_ops *header_ops; //协议头操作集 1250 unsigned int flags; /* interface flags (a la BSD) */ 1251 unsigned int priv_flags; /* Like 'flags' but invisible to userspace. 1252 * See if.h for definitions. */ 1253 unsigned short gflags; 1254 unsigned short padded; /* How much padding added by alloc_netdev() */ 1256 unsigned char operstate; /* RFC2863 operstate */ 1257 unsigned char link_mode; /* mapping policy to operstate */ 1259 unsigned char if_port; /* Selectable AUI, TP,..*/ 1260 unsigned char dma; /* DMA channel */ 1262 unsigned int mtu; /* interface MTU value */ 1263 unsigned short type; /* interface hardware type */ 1264 unsigned short hard_header_len; /* hardware hdr length */ 1270 unsigned short needed_headroom; 1271 unsigned short needed_tailroom; 1274 unsigned char perm_addr[MAX_ADDR_LEN]; /* permanent hw address */ 1275 unsigned char addr_assign_type; /* hw address assignment type */ 1276 unsigned char addr_len; /* hardware address length */ 1289 struct kset *queues_kset; 1386 int watchdog_timeo; /* used by dev_watchdog() */ 1480 };
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-372582-1.html