alloc_skb--分配skb
dev_alloc_skb--分配skb,通常被设备驱动用在中断上下文中,它是alloc_skb的封装函数,因为在中断处理函数中被调用,因此要求原子操作(GFP_ATOMIC)
kfree_skb--减少skb引用,为0则释放,用于出错丢包时释放skb使用;
dev_kfree_skb==consume_skb--减少skb引用,为0则释放,成功状态下释放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 /** 2 * __alloc_skb - allocate a network buffer 3 * @size: size to allocate 4 * @gfp_mask: allocation mask 5 * @flags: If SKB_ALLOC_FCLONE is set, allocate from fclone cache 6 * instead of head cache and allocate a cloned (child) skb. 7 * If SKB_ALLOC_RX is set, __GFP_MEMALLOC will be used for 8 * allocations in case the data is required for writeback 9 * @node: numa node to allocate memory on 10 * 11 * Allocate a new &sk_buff. The returned buffer has no headroom and a 12 * tail room of at least size bytes. The object has a reference count 13 * of one. The return is the buffer. On a failure the return is %NULL. 14 * 15 * Buffers may only be allocated from interrupts using a @gfp_mask of 16 * %GFP_ATOMIC. 17 */ 18 struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask, 19 int flags, int node) 20 { 21 struct kmem_cache *cache; 22 struct skb_shared_info *shinfo; 23 struct sk_buff *skb; 24 u8 *data; 25 bool pfmemalloc; 26 27 /* 得到分配使用的高速缓存 */ 28 cache = (flags & SKB_ALLOC_FCLONE) 29 ? skbuff_fclone_cache : skbuff_head_cache; 30 31 if (sk_memalloc_socks() && (flags & SKB_ALLOC_RX)) 32 gfp_mask |= __GFP_MEMALLOC; 33 34 /* Get the HEAD */ 35 /* 分配skb */ 36 skb = kmem_cache_alloc_node(cache, gfp_mask & ~__GFP_DMA, node); 37 if (!skb) 38 goto out; 39 prefetchw(skb); 40 41 /* We do our best to align skb_shared_info on a separate cache 42 * line. It usually works because kmalloc(X > SMP_CACHE_BYTES) gives 43 * aligned memory blocks, unless SLUB/SLAB debug is enabled. 44 * Both skb->head and skb_shared_info are cache line aligned. 45 */ 46 /* 数据对齐 */ 47 size = SKB_DATA_ALIGN(size); 48 /* 对齐后的数据加上skb_shared_info对齐后的大小 */ 49 size += SKB_DATA_ALIGN(sizeof(struct skb_shared_info));