码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-09-16 11:46:41      阅读:849      评论:0      收藏:0      [点我收藏+]

标签:led   mem   col   signed   cti   end   head   fun   mount   

四个操作函数直接的区别,如下图:

技术分享

 

 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

标签:led   mem   col   signed   cti   end   head   fun   mount   

原文地址:http://www.cnblogs.com/wanpengcoder/p/7529512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!