gpt4 book ai didi

c - linux 内核中 skbuff.h 中的 mac 相关函数如何工作?

转载 作者:行者123 更新时间:2023-11-30 15:35:49 25 4
gpt4 key购买 nike

我读了源代码,但它确实让我困惑。例如函数 sib_mac_header 函数,它是下面代码中的第三个函数,sib -> head 是指向分配的缓冲区头的指针,sib -> mac_header 是指向该缓冲区中 mac_header 地址的指针。为什么两个地址相加的结果是指向MAC以太结构的指针?

skbuff.h中的源代码如下:

1609 static inline void skb_reset_network_header(struct sk_buff *skb)
1610 {
1611 skb->network_header = skb->data - skb->head;
1612 }
1613
1614 static inline void skb_set_network_header(struct sk_buff *skb, const int offset)
1615 {
1616 skb_reset_network_header(skb);
1617 skb->network_header += offset;
1618 }
1619
1620 static inline unsigned char *skb_mac_header(const struct sk_buff *skb)
1621 {
1622 return skb->head + skb->mac_header;
1623 }

最佳答案

您误读了代码。 skb->mac_header 不是一个直接指针...它存储 mac 头相对于 skb->head 的偏移量。事实上,在 64 位架构上,sk_buff_data_t 是 unsigned int,并且 struct sk_buff 的许多成员都是显式偏移的(此技巧用于通过将信息存储在 4 字节字段而不是 8 字节指针中来节省内存) .

关于c - linux 内核中 skbuff.h 中的 mac 相关函数如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com