網路協議棧深入分析(二)--sk_buff的操作函式
阿新 • • 發佈:2019-01-13
1、alloc_skb()函式
該函式的作用是在上層協議要傳送資料包的時候或網路裝置準備接收資料包的時候會呼叫alloc_skb()函式分配sk_buff結構體,需要釋放時呼叫kfree_skb()函式。
- staticinlinestruct sk_buff *alloc_skb(unsigned int size,
- gfp_t priority)
- {
- return __alloc_skb(size, priority, 0, NUMA_NO_NODE);
- }
這裡使用行內函數,非行內函數呼叫會進堆疊的切換,造成額外的開銷,而行內函數可以解決這一點,可以提高執行效率,只是增加了程式的空間開銷。
函式呼叫需要時間和空間開銷,呼叫函式實際上將程式執行流程轉移到被調函式中,被調函式的程式碼執行完後,再返回到呼叫的地方。這種呼叫操作要求呼叫前保護好現場並記憶執行的地址,返回後恢復現場,並按原來儲存的地址繼續執行。對於較長的函式這種開銷可以忽略不計,但對於一些函式體程式碼很短,又被頻繁呼叫的函式,就不能忽視這種開銷。引入行內函數正是為了解決這個問題,提高程式的執行效率。
- /* Allocate a new skbuff. We do this ourselves so we can fill in a few
-
* 'private' fields and also do memory statistics to find all the
- * [BEEP] leaks.
- *
- */
- /**
- * __alloc_skb - allocate a network buffer
- * @size: size to allocate
- * @gfp_mask: allocation mask
- * @fclone: allocate from fclone cache instead of head cache
- * and allocate a cloned (child) skb
- * @node: numa node to allocate memory on
-
*
- * Allocate a new &sk_buff. The returned buffer has no headroom and a
- * tail room of size bytes. The object has a reference count of one.
- * The return is the buffer. On a failure the return is %NULL.
- *
- * Buffers may only be allocated from interrupts using a @gfp_mask of
- * %GFP_ATOMIC.
- */
- struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
- int fclone, int node)
- {
- struct kmem_cache *cache;
- struct skb_shared_info *shinfo;
- struct sk_buff *skb;
- u8 *data;
- cache = fclone ? skbuff_fclone_cache : skbuff_head_cache;
- /* Get the HEAD */
- skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node);//分配儲存空間
- if (!skb)
- goto out;//分配失敗,返回NULL
- prefetchw(skb);
- /* We do our best to align skb_shared_info on a separate cache
- * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives
- * aligned memory blocks, unless SLUB/SLAB debug is enabled.
- * Both skb->head and skb_shared_info are cache line aligned.
- */
- size = SKB_DATA_ALIGN(size);//調整skb大小
- size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
- data = kmalloc_node_track_caller(size, gfp_mask, node);//分配資料區
- if (!data)
- goto nodata;
- /* kmalloc(size) might give us more room than requested.
- * Put skb_shared_info exactly at the end of allocated zone,
- * to allow max possible filling before reallocation.
- */
- size = SKB_WITH_OVERHEAD(ksize(data));
- prefetchw(data + size);
- /*
- * Only clear those fields we need to clear, not those that we will
- * actually initialise below. Hence, don't put any more fields after
- * the tail pointer in struct sk_buff!
- */
- //sk_buff結構體中最後6個屬性不能改變位置,只能在最後
- memset(skb, 0, offsetof(struct sk_buff, tail));//將sk_buff結構體中tail屬性之前的屬性清零
- /* Account for allocated memory : skb + skb->head */
- skb->truesize = SKB_TRUESIZE(size);//計算緩衝區的尺寸
- atomic_set(&skb->users, 1);
- //初始化資料區的指標
- skb->head = data;
- skb->data = data;
- skb_reset_tail_pointer(skb);
- skb->end = skb->tail + size;
- #ifdef NET_SKBUFF_DATA_USES_OFFSET
- skb->mac_header = ~0U;
- #endif
- /* make sure we initialize shinfo sequentially */
- //初始化skb_shared_info
- shinfo = skb_shinfo(skb);
- memset(shinfo, 0, offsetof(struct skb_shared_info, dataref));
- atomic_set(&shinfo->dataref, 1);
- kmemcheck_annotate_variable(shinfo->destructor_arg);
- if (fclone) {
- struct sk_buff *child = skb + 1;
- atomic_t *fclone_ref = (atomic_t *) (child + 1);
- kmemcheck_annotate_bitfield(child, flags1);
- kmemcheck_annotate_bitfield(child, flags2);
- skb->fclone = SKB_FCLONE_ORIG;
- atomic_set(fclone_ref, 1);
- child->fclone = SKB_FCLONE_UNAVAILABLE;
- }
- out:
- return skb;
- nodata:
- kmem_cache_free(cache, skb);
- skb = NULL;
- goto out;
- }
2、kfree_skb()函式
該函式就是釋放不被使用的sk_buff結構
- /**
- * kfree_skb - free an sk_buff
- * @skb: buffer to free
- *
- * Drop a reference to the buffer and free it if the usage count has
- * hit zero.
- */
- void kfree_skb(struct sk_buff *skb)
- {
- if (unlikely(!skb))
- return;
- if (likely(atomic_read(&skb->users) == 1))
- smp_rmb();
- elseif (likely(!atomic_dec_and_test(&skb->users)))
- return;
- trace_kfree_skb(skb, __builtin_return_address(0));
- __kfree_skb(skb);
- }
- void __kfree_skb(struct sk_buff *skb)
- {
- skb_release_all(skb);//釋放除了skb本身佔用的記憶體
- kfree_skbmem(skb);
-
相關推薦
no