/**
* skb_share_check - check if buffer is shared and if so clone it
* @skb: buffer to check
* @pri: priority for memory allocation
*
* If the buffer is shared the buffer is cloned and the old copy
* drops a reference. A new clone with a single reference is returned.
* If the buffer is not shared the original buffer is returned. When
* being called from interrupt status or with spinlocks held pri must
* be GFP_ATOMIC.
*
* NULL is returned on a memory allocation failure.
*
*/
static inline struct sk_buff *skb_share_check(struct sk_buff *skb,
gfp_t pri)
{
might_sleep_if(pri & __GFP_WAIT);
if (skb_shared(skb)) {
struct sk_buff *nskb = skb_clone(skb, pri);
kfree_skb(skb);
skb = nskb;
}
return skb;
}
其实这个函数挺简单的,但是刚刚开始想不通,为什么需要克隆一份,然后释放原skb,思考了半天,现在才有些明白。
首先,这个函数主要被TX和RX函数调用,以ip_rcv为例,其原型如下:
int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
(1)调用ip_rcv的函数把数据交给ip_rcv处理,自己撒手了,该skb由ip_rcv负责打理!
(2)但原来的skb可能被共享,如果需要修改skb,则会影响共享该sbk的其他函数,因此如果被共享,则克隆一份,再调用kfree_skb(实际只是skb->users--,相关数据并没有释放)