dev_alloc_skb(len+16) skb_reserve(skb,2) skb_put(skb,len)

/**
 *      dev_alloc_skb - allocate an skbuff for receiving
 *      @length: length to allocate
 *
 *      Allocate a new &sk_buff and assign it a usage count of one. The
 *      buffer has unspecified headroom built in. Users should allocate
 *      the headroom they think they need without accounting for the
 *      built in space. The built in space is used for optimisations.
 *
 *      %NULL is returned if there is no free memory. Although this function
 *      allocates memory it can be called from an interrupt.
 */
static inline struct sk_buff *dev_alloc_skb(unsigned int length)

{
        return __dev_alloc_skb(length, GFP_ATOMIC);

    首先L2的地址一共是14(6+6+2)个BYTE,而MAC+IP+TCP/UDP结构为:L2+L3(IP addr)+L4。

    按照常理L2地址是在L3后加, 所以在ALLOCSKB的时候要留14个BYTE,为了以后给L2用.但是计算机一般是4字节对齐的, 如果留14个BYTE那么IP只能排在15个byte位子, ip头要经常访问所以这样效率似乎不好.于是在预留2个(14+2 = 16) 正好让IP头4字节对齐. 

/**
 *      skb_reserve - adjust headroom
 *      @skb: buffer to alter
 *      @len: bytes to move
 *
 *      Increase the headroom of an empty &sk_buff by reducing the tail
 *      room. This is only allowed for an empty buffer.
 */
static inline void skb_reserve(struct sk_buff *skb, int len)
{
        skb->data += len;
        skb->tail += len;
}

 

skb_reserve(skb,2);

The networking layer currently aligns IP headers in rx packets. It does this via skb_reserve(,2).skb_put 于 skb_push:

skb_put() 增长数据区的长度来为memcpy准备空间. 许多的网络操作需要加入一些桢头, 这可以使用skb_push来将数据区向后推, 为头留出空间. 
---------------------------------------- 
| head | data | | 
---------------------------------------- 
skb_put 
----------------------------------------- 
| head | data | put_data | | 
----------------------------------------- 
skb_push
------------------------------------------ 
| head | push_data | data | put_data | | 
------------------------------------------ 

/**
* skb_put - add data to a buffer 
* @skb: buffer to use
* @len: amount of data to add 
*
* This function extends the used data area of the buffer. If this would
* exceed the total buffer size the kernel will panic. A pointer to the
* first byte of the extra data is returned. 
*/
static inline unsigned char *skb_put(struct sk_buff *skb, unsigned int len)

     unsigned char *tmp = skb->tail; 
     SKB_LINEAR_ASSERT(skb); 
     skb->tail += len; 
     skb->len += len; 
     if (unlikely(skb->tail>skb->end)) 
           skb_over_panic(skb, len, current_text_addr()); 
     return tmp; 
}

原文地址:https://www.cnblogs.com/zxc2man/p/6437512.html