网络设备驱动 和 DM9000 驱动程序分析


分析内核版本:Linux 2.6.36.2。 分析网卡:DM9000

一、网络设备驱动程序分析。

1、Linux 网络设备驱动程序 分层:

Linux 网络设备驱动程序从上到下可分为4层,依次为:网络协议接口层、网络设备接口层、提供实际功能的设备 驱动功能层、以及  网络设备与媒介层。这4层作用如下:

1) 网络协议接口层:网络协议接口层想网络协议提供统一的数据包收发接口,不论是上层ARP,还是IP,都通过dev_queue_xmit() 函数收发数据,并通过netif_rx 函数

接收数据。这一层的存在使得上层协议独立于具体的设备。

2)网络设备接口层:网络设备接口层向协议层接口层提供统一的用于描述具体网络设备属性 和 操作的结构体 net_device,该结构体是设备驱动功能层中各函数的容器,

实际上,网络设备接口层从宏观上规划了具体操作硬件的设备驱动功能层的结构。

3)设备功能层:该层的各个函数是网络设备接口层net_device 数据结构具体成员,是驱使网络设备硬件完成相应动作的程序,它通过har_start_xmit() 函数启动发送操作,

并通过网络设备上的中断触发接收操作。

4)网络设备 与 媒介层: 是完成数据包发送和接收的物理实体,包括网络适配器和具体的传输媒介,网络适配器被设备驱动功能层中的函数物理上驱动,对于Linux而言,

网络设备和媒介都是可以虚拟的。

在设计具体的网络设备驱动程序是,我们需要完成的主要工作是编写设备驱动功能层 的 相关函数以以填充net_device 数据结构的内容并将net_device 注册入内核。

2、网络协议接口层程序分析:

1) int netif_xmit (struct sk_buff *skb);// 发送一个sk_buff 数据包

int netif_rx (struct sk_buff *skb);   // 接收一个数据包

以上函数定义在 net/core/dev.c 中

sk_buff 结构体定义在 include/linux/skbuff.h 文件, 它的含义为“套接字缓冲区”,用于在Linux 网络子系统中个层之间传递数据,是Linux 网络子系统数据传递的“中枢神经”

2) 套接字缓冲区 sk_buff 结构体

  1. struct sk_buff {  
  2.     /* These two members must be first. */  
  3.     struct sk_buff      *next;  
  4.     struct sk_buff      *prev;  
  5.   
  6.     ktime_t         tstamp;  
  7.   
  8.     struct sock     *sk;  
  9.     struct net_device   *dev;  
  10.   
  11.     /* 
  12.      * This is the control buffer. It is free to use for every 
  13.      * layer. Please put your private variables there. If you 
  14.      * want to keep them across layers you have to do a skb_clone() 
  15.      * first. This is owned by whoever has the skb queued ATM. 
  16.      */  
  17.     char            cb[48] __aligned(8);  
  18.   
  19.     unsigned long       _skb_refdst;  
  20. #ifdef CONFIG_XFRM   
  21.     struct  sec_path    *sp;  
  22. #endif   
  23.     unsigned int        len,  
  24.                 data_len;  
  25.     __u16           mac_len,  
  26.                 hdr_len;  
  27.     union {  
  28.         __wsum      csum;  
  29.         struct {  
  30.             __u16   csum_start;  
  31.             __u16   csum_offset;  
  32.         };  
  33.     };  
  34.     __u32           priority;  
  35.     kmemcheck_bitfield_begin(flags1);  
  36.     __u8            local_df:1,  
  37.                 cloned:1,  
  38.                 ip_summed:2,  
  39.                 nohdr:1,  
  40.                 nfctinfo:3;  
  41.     __u8            pkt_type:3,  
  42.                 fclone:2,  
  43.                 ipvs_property:1,  
  44.                 peeked:1,  
  45.                 nf_trace:1;  
  46.     kmemcheck_bitfield_end(flags1);  
  47.     __be16          protocol;  
  48.   
  49.     void            (*destructor)(struct sk_buff *skb);  
  50. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)   
  51.     struct nf_conntrack *nfct;  
  52.     struct sk_buff      *nfct_reasm;  
  53. #endif   
  54. #ifdef CONFIG_BRIDGE_NETFILTER   
  55.     struct nf_bridge_info   *nf_bridge;  
  56. #endif   
  57.   
  58.     int         skb_iif;  
  59. #ifdef CONFIG_NET_SCHED   
  60.     __u16           tc_index;   /* traffic control index */  
  61. #ifdef CONFIG_NET_CLS_ACT   
  62.     __u16           tc_verd;    /* traffic control verdict */  
  63. #endif   
  64. #endif   
  65.   
  66.     __u32           rxhash;  
  67.   
  68.     kmemcheck_bitfield_begin(flags2);  
  69.     __u16           queue_mapping:16;  
  70. #ifdef CONFIG_IPV6_NDISC_NODETYPE   
  71.     __u8            ndisc_nodetype:2,  
  72.                 deliver_no_wcard:1;  
  73. #else   
  74.     __u8            deliver_no_wcard:1;  
  75. #endif   
  76.     kmemcheck_bitfield_end(flags2);  
  77.   
  78.     /* 0/14 bit hole */  
  79.   
  80. #ifdef CONFIG_NET_DMA   
  81.     dma_cookie_t        dma_cookie;  
  82. #endif   
  83. #ifdef CONFIG_NETWORK_SECMARK   
  84.     __u32           secmark;  
  85. #endif   
  86.     union {  
  87.         __u32       mark;  
  88.         __u32       dropcount;  
  89.     };  
  90.   
  91.     __u16           vlan_tci;  
  92.   
  93.     sk_buff_data_t      transport_header;    // 传输层   
  94.     sk_buff_data_t      network_header;      // 网络层   
  95.     sk_buff_data_t      mac_header;          // MAC 层   
  96.     /* These elements must be at the end, see alloc_skb() for details.  */  
  97.     sk_buff_data_t      tail;  
  98.     sk_buff_data_t      end;  
  99.     unsigned char       *head,      // 缓冲区头指针   
  100.                 *data;      // 有效数据头指针   
  101.     unsigned int        truesize;  
  102.     atomic_t        users;  
  103. };  
  • 1
  • 2
  • 3
  • 4
  • 下一页

相关内容