1. 程式人生 > >skb管理函數之skb_put、skb_push、skb_pull、skb_reserve

skb管理函數之skb_put、skb_push、skb_pull、skb_reserve

color panic star header remove ear 函數 rst extra

四個操作函數直接的區別,如下圖:

技術分享

 1 /**
 2  *    skb_put - add data to a buffer
 3  *    @skb: buffer to use
 4  *    @len: amount of data to add
 5  *
 6  *    This function extends the used data area of the buffer. If this would
 7  *    exceed the total buffer size the kernel will panic. A pointer to the
8 * first byte of the extra data is returned. 9 */ 10 /* 11 向skb尾部添加數據 12 */ 13 unsigned char *skb_put(struct sk_buff *skb, unsigned int len) 14 { 15 /* 獲取當前skb->tail */ 16 unsigned char *tmp = skb_tail_pointer(skb); 17 18 /* 要求skb數據區必須為線性 */ 19 SKB_LINEAR_ASSERT(skb);
20 21 /* skb尾部增加len字節 */ 22 skb->tail += len; 23 /* skb數據總長度增加len字節 */ 24 skb->len += len; 25 26 /* 如果增加之後的tail > end ,則panic */ 27 if (unlikely(skb->tail > skb->end)) 28 skb_over_panic(skb, len, __builtin_return_address(0)); 29 30 //返回添加數據的第一個字節位置
31 return tmp; 32 }

/**
 *    skb_push - add data to the start of a buffer
 *    @skb: buffer to use
 *    @len: amount of data to add
 *
 *    This function extends the used data area of the buffer at the buffer
 *    start. If this would exceed the total buffer headroom the kernel will
 *    panic. A pointer to the first byte of the extra data is returned.
 */
/*
    向skb數據區頭部添加數據
*/
unsigned char *skb_push(struct sk_buff *skb, unsigned int len)
{
    /* 數據區data指針前移len字節 */
    skb->data -= len;
    /* 數據總長度增加len字節 */
    skb->len  += len;

    /* 添加數據長度溢出過header ,panic*/
    if (unlikely(skb->data<skb->head))
        skb_under_panic(skb, len, __builtin_return_address(0));

    /* 返回新的data指針 */
    return skb->data;
}

 1 /**
 2  *    skb_pull - remove data from the start of a buffer
 3  *    @skb: buffer to use
 4  *    @len: amount of data to remove
 5  *
 6  *    This function removes data from the start of a buffer, returning
 7  *    the memory to the headroom. A pointer to the next data in the buffer
 8  *    is returned. Once the data has been pulled future pushes will overwrite
 9  *    the old data.
10  */
11 /*
12     從數據區頭部移除數據
13 */
14 unsigned char *skb_pull(struct sk_buff *skb, unsigned int len)
15 {
16     return skb_pull_inline(skb, len);
17 }

 1 /*
 2     保留頭部空間,只能對空的skb使用
 3 */
 4 static inline void skb_reserve(struct sk_buff *skb, int len)
 5 {
 6     /* 數據區data指針增加len字節*/
 7     skb->data += len;
 8     /* 數據區tail指針增加len字節 */
 9     skb->tail += len;
10 }

skb管理函數之skb_put、skb_push、skb_pull、skb_reserve