Linux内核--网络协议栈深入分析(二)--sk_buff的操作函数


本文分析基于Linux Kernel 3.2.1

更多请查看 Linux内核--网络内核实现分析

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);  

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

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

  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. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容

    暂无相关文章