歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux內核 >> Linux內核--網絡協議棧深入分析(二)--sk_buff的操作函數

Linux內核--網絡協議棧深入分析(二)--sk_buff的操作函數

日期:2017/3/1 10:10:04   编辑:Linux內核

本文分析基於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);
  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. * __alloc_skb - allocate a network buffer
  8. * @size: size to allocate
  9. * @gfp_mask: allocation mask
  10. * @fclone: allocate from fclone cache instead of head cache
  11. * and allocate a cloned (child) skb
  12. * @node: numa node to allocate memory on
  13. *
  14. * Allocate a new &sk_buff. The returned buffer has no headroom and a
  15. * tail room of size bytes. The object has a reference count of one.
  16. * The return is the buffer. On a failure the return is %NULL.
  17. *
  18. * Buffers may only be allocated from interrupts using a @gfp_mask of
  19. * %GFP_ATOMIC.
  20. */
  21. struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
  22. int fclone, int node)
  23. {
  24. struct kmem_cache *cache;
  25. struct skb_shared_info *shinfo;
  26. struct sk_buff *skb;
  27. u8 *data;
  28. cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
  29. /* Get the HEAD */
  30. skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);//分配存儲空間
  31. if (!skb)
  32. goto out;//分配失敗,返回NULL
  33. prefetchw(skb);
  34. /* We do our best to align skb_shared_info on a separate cache
  35. * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
  36. * aligned memory blocks, unless SLUB/SLAB debug is enabled.
  37. * Both skb->head and skb_shared_info are cache line aligned.
  38. */
  39. size = SKB_DATA_ALIGN(size);//調整skb大小
  40. size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  41. data = kmalloc_node_track_caller(size, gfp_mask, node);//分配數據區
  42. if (!data)
  43. goto nodata;
  44. /* kmalloc(size) might give us more room than requested.
  45. * Put skb_shared_info exactly at the end of allocated zone,
  46. * to allow max possible filling before reallocation.
  47. */
  48. size = SKB_WITH_OVERHEAD(ksize(data));
  49. prefetchw(data + size);
  50. /*
  51. * Only clear those fields we need to clear, not those that we will
  52. * actually initialise below. Hence, don't put any more fields after
  53. * the tail pointer in struct sk_buff!
  54. */
  55. //sk_buff結構體中最後6個屬性不能改變位置,只能在最後
  56. memset(skb, 0, offsetof(struct sk_buff, tail));//將sk_buff結構體中tail屬性之前的屬性清零
  57. /* Account for allocated memory : skb + skb->head */
  58. skb->truesize = SKB_TRUESIZE(size);//計算緩沖區的尺寸
  59. atomic_set(&skb->users, 1);
  60. //初始化數據區的指針
  61. skb->head = data;
  62. skb->data = data;
  63. skb_reset_tail_pointer(skb);
  64. skb->end = skb->tail + size;
  65. #ifdef NET_SKBUFF_DATA_USES_OFFSET
  66. skb->mac_header = ~0U;
  67. #endif
  68. /* make sure we initialize shinfo sequentially */
  69. //初始化skb_shared_info
  70. shinfo = skb_shinfo(skb);
  71. memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
  72. atomic_set(&shinfo->dataref, 1);
  73. kmemcheck_annotate_variable(shinfo->destructor_arg);
  74. if (fclone) {
  75. struct sk_buff *child = skb + 1;
  76. atomic_t *fclone_ref = (atomic_t *) (child + 1);
  77. kmemcheck_annotate_bitfield(child, flags1);
  78. kmemcheck_annotate_bitfield(child, flags2);
  79. skb->fclone = SKB_FCLONE_ORIG;
  80. atomic_set(fclone_ref, 1);
  81. child->fclone = SKB_FCLONE_UNAVAILABLE;
  82. }
  83. out:
  84. return skb;
  85. nodata:
  86. kmem_cache_free(cache, skb);
  87. skb = NULL;
  88. goto out;
  89. }
Copyright © Linux教程網 All Rights Reserved