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

本文详细介绍了Linux内核中用于网络数据包处理的skb管理函数,包括skb_put、skb_push、skb_pull和skb_reserve的功能和区别,帮助读者理解这些函数在数据包操作中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 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 }

 

转载于:https://www.cnblogs.com/wanpengcoder/p/7529512.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值