最近翻阅了一些linux网络协议方面的资料,发现在sk_buff的相关结构上有些细节上的不一致。于是,埋头啃代码,整理相关细节如下。(内核版本2.6.22)
根据sk_buff结构和__alloc_skb(),sk_buff结构及数据内存区初始状态为:
 
sk_buff结构
__alloc_skb()的功能和参数为
/**
 * __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)
其中,图中size并不完全等于参数传入的size,在函数中经过对齐
size = SKB_DATA_ALIGN(size);
另外,skb->len在经过memset(skb, 0, offsetof(struct sk_buff, tail));后已巧妙地设置为0.
skb->truesize = size + sizeof(struct sk_buff);可见,truesize为sk_buff结构本身大小与数据区长度size之和,不包括skb_shared_info.
data = kmalloc_node_track_caller(size + sizeof(struct skb_shared_info), gfp_mask, node);此句亦可证明。