zoukankan      html  css  js  c++  java
  • Linux内核网络协议栈深入分析(二)sk_buff的操作函数

    本文分析基于Linux Kernel 3.2.1

    原创作品,转载请标明http://blog.csdn.net/yming0221/article/details/7972647

    更多请查看网络栈分析专栏http://blog.csdn.net/column/details/linux-kernel-net.html

    作者:闫明


    1、alloc_skb()函数

    该函数的作用是在上层协议要发送数据包的时候或网络设备准备接收数据包的时候会调用alloc_skb()函数分配sk_buff结构体,需要释放时调用kfree_skb()函数。

    1. static inline struct sk_buff *alloc_skb(unsigned int size,  
    2.                     gfp_t priority)  
    3. {  
    4.     return __alloc_skb(size, priority, 0, NUMA_NO_NODE);  
    5. }  

    这里使用内联函数,非内联函数调用会进堆栈的切换,造成额外的开销,而内联函数可以解决这一点,可以提高执行效率,只是增加了程序的空间开销。

            函数调用需要时间和空间开销,调用函数实际上将程序执行流程转移到被调函数中,被调函数的代码执行完后,再返回到调用的地方。这种调用操作要求调用前保护好现场并记忆执行的地址,返回后恢复现场,并按原来保存的地址继续执行。对于较长的函数这种开销可以忽略不计,但对于一些函数体代码很短,又被频繁调用的函数,就不能忽视这种开销。引入内联函数正是为了解决这个问题,提高程序的运行效率。

    1. /*  Allocate a new skbuff. We do this ourselves so we can fill in a few 
    2.  *  'private' fields and also do memory statistics to find all the 
    3.  *  [BEEP] leaks. 
    4.  * 
    5.  */  
    6.   
    7. /** 
    8.  *  __alloc_skb -   allocate a network buffer 
    9.  *  @size: size to allocate 
    10.  *  @gfp_mask: allocation mask 
    11.  *  @fclone: allocate from fclone cache instead of head cache 
    12.  *      and allocate a cloned (child) skb 
    13.  *  @node: numa node to allocate memory on 
    14.  * 
    15.  *  Allocate a new &sk_buff. The returned buffer has no headroom and a 
    16.  *  tail room of size bytes. The object has a reference count of one. 
    17.  *  The return is the buffer. On a failure the return is %NULL. 
    18.  * 
    19.  *  Buffers may only be allocated from interrupts using a @gfp_mask of 
    20.  *  %GFP_ATOMIC. 
    21.  */  
    22. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,  
    23.                 int fclone, int node)  
    24. {  
    25.     struct kmem_cache *cache;  
    26.     struct skb_shared_info *shinfo;  
    27.     struct sk_buff *skb;  
    28.     u8 *data;  
    29.   
    30.     cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;  
    31.   
    32.     /* Get the HEAD */  
    33.     skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);//分配存储空间  
    34.     if (!skb)  
    35.         goto out;//分配失败,返回NULL  
    36.     prefetchw(skb);  
    37.   
    38.     /* We do our best to align skb_shared_info on a separate cache 
    39.      * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 
    40.      * aligned memory blocks, unless SLUB/SLAB debug is enabled. 
    41.      * Both skb->head and skb_shared_info are cache line aligned. 
    42.      */  
    43.     size = SKB_DATA_ALIGN(size);//调整skb大小  
    44.     size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));  
    45.     data = kmalloc_node_track_caller(size, gfp_mask, node);//分配数据区  
    46.     if (!data)  
    47.         goto nodata;  
    48.     /* kmalloc(size) might give us more room than requested. 
    49.      * Put skb_shared_info exactly at the end of allocated zone, 
    50.      * to allow max possible filling before reallocation. 
    51.      */  
    52.     size = SKB_WITH_OVERHEAD(ksize(data));  
    53.     prefetchw(data + size);  
    54.   
    55.     /* 
    56.      * Only clear those fields we need to clear, not those that we will 
    57.      * actually initialise below. Hence, don't put any more fields after 
    58.      * the tail pointer in struct sk_buff! 
    59.      */  
    60.      //sk_buff结构体中最后6个属性不能改变位置,只能在最后  
    61.     memset(skb, 0, offsetof(struct sk_buff, tail));//将sk_buff结构体中tail属性之前的属性清零  
    62.     /* Account for allocated memory : skb + skb->head */  
    63.     skb->truesize = SKB_TRUESIZE(size);//计算缓冲区的尺寸  
    64.     atomic_set(&skb->users, 1);  
    65.     //初始化数据区的指针  
    66.     skb->head = data;  
    67.     skb->data = data;  
    68.     skb_reset_tail_pointer(skb);  
    69.     skb->end = skb->tail + size;  
    70. #ifdef NET_SKBUFF_DATA_USES_OFFSET  
    71.     skb->mac_header = ~0U;  
    72. #endif  
    73.   
    74.     /* make sure we initialize shinfo sequentially */  
    75.     //初始化skb_shared_info  
    76.     shinfo = skb_shinfo(skb);  
    77.     memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));  
    78.     atomic_set(&shinfo->dataref, 1);  
    79.     kmemcheck_annotate_variable(shinfo->destructor_arg);  
    80.   
    81.     if (fclone) {  
    82.         struct sk_buff *child = skb + 1;  
    83.         atomic_t *fclone_ref = (atomic_t *) (child + 1);  
    84.   
    85.         kmemcheck_annotate_bitfield(child, flags1);  
    86.         kmemcheck_annotate_bitfield(child, flags2);  
    87.         skb->fclone = SKB_FCLONE_ORIG;  
    88.         atomic_set(fclone_ref, 1);  
    89.   
    90.         child->fclone = SKB_FCLONE_UNAVAILABLE;  
    91.     }  
    92. out:  
    93.     return skb;  
    94. nodata:  
    95.     kmem_cache_free(cache, skb);  
    96.     skb = NULL;  
    97.     goto out;  
    98. }  
    函数执行完成后,sk_buff的数据指针的形式如下:



    2、kfree_skb()函数

    该函数就是释放不被使用的sk_buff结构

    1. /** 
    2.  *  kfree_skb - free an sk_buff 
    3.  *  @skb: buffer to free 
    4.  * 
    5.  *  Drop a reference to the buffer and free it if the usage count has 
    6.  *  hit zero. 
    7.  */  
    8. void kfree_skb(struct sk_buff *skb)  
    9. {  
    10.     if (unlikely(!skb))  
    11.         return;  
    12.     if (likely(atomic_read(&skb->users) == 1))  
    13.         smp_rmb();  
    14.     else if (likely(!atomic_dec_and_test(&skb->users)))  
    15.         return;  
    16.     trace_kfree_skb(skb, __builtin_return_address(0));  
    17.     __kfree_skb(skb);  
    18. }  
    再调用__kfree_skb函数
    1. void __kfree_skb(struct sk_buff *skb)  
    2. {  
    3.     skb_release_all(skb);//释放除了skb本身占用的内存  
    4.     kfree_skbmem(skb);  
    5. }  
    这里不再向深层函数探究,以后再说。


    3、skb_put()函数

    该函数是在数据区的末端添加某协议的尾部

    1. /** 
    2.  *  skb_put - add data to a buffer 
    3.  *  @skb: buffer to use 
    4.  *  @len: amount of data to add 
    5.  * 
    6.  *  This function extends the used data area of the buffer. If this would 
    7.  *  exceed the total buffer size the kernel will panic. A pointer to the 
    8.  *  first byte of the extra data is returned. 
    9.  */  
    10. unsigned char *skb_put(struct sk_buff *skb, unsigned int len)  
    11. {  
    12.     unsigned char *tmp = skb_tail_pointer(skb);  
    13.     SKB_LINEAR_ASSERT(skb);  
    14.     skb->tail += len;//尾部后移len  
    15.     skb->len  += len;//长度增加len  
    16.     if (unlikely(skb->tail > skb->end))//panic  
    17.         skb_over_panic(skb, len, __builtin_return_address(0));  
    18.     return tmp;  
    19. }  

    执行前后的示意图如下:



    4、skb_push()函数

    该函数的作用是在数据区的前端添加某协议的头部,和skb_put类似。

    只不过这里移动的数据指针的是data前移len个单位。

    1. unsigned char *skb_push(struct sk_buff *skb, unsigned int len)  
    2. {  
    3.     skb->data -= len;  
    4.     skb->len  += len;  
    5.     if (unlikely(skb->data<skb->head))  
    6.         skb_under_panic(skb, len, __builtin_return_address(0));  
    7.     return skb->data;  
    8. }  



    5、skb_pull和skb_trim函数正好和上面两个函数的功能相反,是去掉相应的部分,不再赘述。

    6、skb_reverse()函数

    该函数的作用是在数据区创建存储协议头部的空间,函数实现很简单。

    1. static inline void skb_reserve(struct sk_buff *skb, int len)  
    2. {  
    3.     skb->data += len;  
    4.     skb->tail += len;  
    5. }  

    7、sk_buff缓冲区链表的操作函数

    skb_orphan()函数是将一个缓冲结构体变成孤立的skb

    1. static inline void skb_orphan(struct sk_buff *skb)  
    2. {  
    3.     if (skb->destructor)  
    4.         skb->destructor(skb);  
    5.     skb->destructor = NULL;  
    6.     skb->sk      = NULL;  
    7. }  

    skb_queue_head_init()函数将初始化sk_buff_head结构体

    1. static inline void skb_queue_head_init(struct sk_buff_head *list)  
    2. {  
    3.     spin_lock_init(&list->lock);  
    4.     __skb_queue_head_init(list);  
    5. }  

    skb_queue_head()在链表头添加一个sk_buff结构

    1. void skb_queue_head(struct sk_buff_head *list, struct sk_buff *newsk)  
    2. {  
    3.     unsigned long flags;  
    4.   
    5.     spin_lock_irqsave(&list->lock, flags);  
    6.     __skb_queue_head(list, newsk);  
    7.     spin_unlock_irqrestore(&list->lock, flags);  
    8. }  

    调用__skb_queue_head()函数实现功能

    1. static inline void __skb_queue_head(struct sk_buff_head *list,  
    2.                     struct sk_buff *newsk)  
    3. {  
    4.     __skb_queue_after(list, (struct sk_buff *)list, newsk);  
    5. }  
    6. static inline void __skb_queue_after(struct sk_buff_head *list,  
    7.                      struct sk_buff *prev,  
    8.                      struct sk_buff *newsk)  
    9. {  
    10.     __skb_insert(newsk, prev, prev->next, list);  
    11. }  

    最后用函数__skb_insert操作双链表

    1. static inline void __skb_insert(struct sk_buff *newsk,  
    2.                 struct sk_buff *prev, struct sk_buff *next,  
    3.                 struct sk_buff_head *list)  
    4. {  
    5.     newsk->next = next;  
    6.     newsk->prev = prev;  
    7.     next->prev  = prev->next = newsk;  
    8.     list->qlen++;  
    9. }  

    函数skb_queue_tail()  在缓冲区链表尾部添加新的skb

    函数skb_dequeue()   在链表头部移走一个skb

    函数skb_dequeue_tail()  在链表尾部移走一个skb

    函数skb_queue_purge()  清空一个由sk_buff_head管理的缓冲区链表,具体操作如下:

    1. void skb_queue_purge(struct sk_buff_head *list)  
    2. {  
    3.     struct sk_buff *skb;  
    4.     while ((skb = skb_dequeue(list)) != NULL)  
    5.         kfree_skb(skb);  
    6. }  
    函数skb_append()  在指定的skb后附加一个缓冲区,最终还是调用__skb_insert()函数完成的链表操作
  • 相关阅读:
    【hdu 2569】ACM程序设计期末考试081230
    【信息安全111班暑期学习工作任务】
    【hdu 1698 Just a Hook(被搞死)】
    Win8下安装 .net framework 3.5.1 无需连网安装方法,证实有效
    【UVA 488 Triangle Wave】
    【As Easy As A+B 专题训练排序】
    【hdu 1787 GCD Again (数论、欧拉函数)】
    【hdu 2602 Bone Collector(动态规划、01背包)】
    【poj 1953 World Cup Noise】
    【poj 2478 Farey Sequence (欧拉函数、数论)】
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6332905.html
Copyright © 2011-2022 走看看