Chinaunix首页 | 论坛 | 博客
  • 博客访问: 227175
  • 博文数量: 29
  • 博客积分: 878
  • 博客等级: 上士
  • 技术积分: 1114
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-12 14:49
文章分类

全部博文(29)

文章存档

2012年(29)

分类: LINUX

2012-04-21 15:01:24

linux网络设备结构中最重要的两个数据结构struct sk_buff和struct net_device。

对于这两个结构体,要了解他们的作用是什么,以及内核何时以何种方式操作他们。

sk_buff位于网络协议接口层,用于在linux网络子系统各层次之间传递数据。定义在include/linux/skbuff.h中。
发送数据包时,将要发送的数据存入 sk_buff中,传递给下层,通过添加相应的协议头交给网络设备发送。
接收数据包时,想将数据保存在sk_buff中,并传递到上层,上层通过剥去协议头直至交给用户。
部分源代码:

点击(此处)折叠或打开

  1. struct sk_buff {
  2.     /* These two members must be first. */
  3.     struct sk_buff        *next;
  4.     struct sk_buff        *prev;
  5.     struct net_device *dev; //处理该数据包的设备
  6.         unsigned int        len, //有效数据长度
  7.                             data_len;
  8.     __u16            mac_len,
  9.                 hdr_len;
  10. sk_buff_data_t        transport_header;
  11.     sk_buff_data_t        network_header;
  12.     sk_buff_data_t        mac_header;
  13.     /* These elements must be at the end, see alloc_skb() for details. */
  14.     sk_buff_data_t        tail; //有效数据结束
  15.     sk_buff_data_t        end;
  16.     unsigned char        *head, //分配空间的开始
  17.                          *data; //有效数据开始     
  18.     unsigned int        truesize;
  19.     atomic_t        users;
  20. }

内核对sk_buff的操作有分配、释放、变更等。
(1)分配
内核分配套接字缓冲区的函数
struct sk_buff *alloc_skb(unsigned int len,gfp_t priority);
分配一个套接字缓冲区和一个数据缓冲区,并初始化data、tail、head成员。由内核协议栈分配。
struct sk_buff *dev_alloc_skb(unsigned int len);
以GFP_ATOMIC优先级调用alloc_skb()分配。由驱动程序中收到数据分配使用。

(2)释放
void kfree_skb(struct sk_buff *skb);
内核协议栈调用。
void dev_kfree_skb(struct sk_buff *skb);
驱动程序中释放缓冲区。用于非中断缓冲区。
dev_kfree_skb_irq()用于中断上下文。

(3)变更
unsigned char *skb_put(struct sk_buff *skb,unsigned int len);
skb-->tail后移len字节。
unsigned char *skb_push(struct sk_buff *skb,unsigned int len);
skb-->data前移len字节。

net_device结构体位于网络设备接口层,用于描述一个设备。定义与include/linux/netdevice.h
内部成员包含了网络设备的属性描述和接口操作。
部分源代码:

点击(此处)折叠或打开

  1. struct net_device {

  2.     /*
  3.      * This is the first field of the "visible" part of this structure
  4.      * (i.e. as seen by users in the "Space.c" file). It is the name
  5.      * of the interface.
  6.      */
  7.     char            name[IFNAMSIZ];
  8. ...
  9. unsigned long        mem_end;    /* shared mem end    */
  10.     unsigned long        mem_start;    /* shared mem start    */
  11.     unsigned long        base_addr;    /* device I/O address    */
  12.     unsigned int        irq;        /* device IRQ number    */
  13. ...
包括了对设备的操作函数打开、关闭等,具体函数实现在设备驱动程序中。


阅读(3821) | 评论(0) | 转发(7) |
给主人留下些什么吧!~~