sk_buff Structure

The structure has changed many times in the history of the kernel,both to add new options and to reorganize existing fields into a cleaner layout.Its fields can be classified roughly into the following categories:

  • Layout
  • General
  • Feature-specific
  • Management functions

Layout Fields

Interesting fields of sk_buff:

  • struct sock *sk:This is a pointer to a sock data structure of the socket that owns this buffer.
  • unsigned int len:This is the size of the block of data in the buffer.This length include both the data in the main buffer and the data in the fragments.
  • unsigned int data_len:Unlike len,data_len accounts only for the size of the data in the fragments.
  • unsigned int mac_len:This is the size of the MAC header
  • atomic_t users:This is the reference count.
  • unsigned int truesize:This field represent the total size of the buffer,including the sk_buff structure itself.
  • unsigned char head,end,data,tail:head and end point to the beginning and end of the space allocated to the buffer,and data and tail point to the beginning and end of the actual data.

General Fields

  • struct timeval stamp:This is usually meaningful only for a received packet.It is a timestamp that represents when a packet was received or when one is scheduled for transmission.
  • struce net_device * dev:describes a network device.
  • struct dst_entry dst:This is used by the routing subsystem.
  • char cb[40]:This is a "control buffer",or storage for private information,maintained by each layer for internal use
  • unsigned char clone:indicates that this structure is a clone of another sk_buff buffer.
  • unsigned char pkt_type:This field classifies the type of frame based on its L2 destination address.
  • unsigned short protocol:This is the protocol used at the next-higher layer from the perspective of the device driver at L2.

Feature-Specific Fields

The Linux kernel is modular.allowing you to select what to include and waht to leave out.Thus some fields are included in the sk_buff data structure only if the kernel is compiled with support for particular features such as firewalling or QoS

  • struct nf_conntrack *nfct
  • struct nf_bridge_info *nf_bridge;
  • u8 nfctinfo
  • u8 nf_trace

    These parameters are used by Netfilter

  • u16 tc_index
  • u16 tc_verd

    These parameters are used by the Traffic Control

  • struct sec_path *sp

    This is used by the IPsec protocol suite to keep track of transformations

Management Functions

Before and After:(a)skb_put,(b)skb_push,(c)skb_pull,(d)skb_reserve

image

Allocating memory:alloc_skb and dev_alloc_skb

creating a single buffer involves two allocations of memory(one for the buffer and one for the sk_buff struct)

The skb_shared_info structure and the skb_shinfo function

skb_shared_info at the end of the data buffer that keeps additional information about the data block(Fragment).

Note that there is no field inside sk_buff structure pointing at the skb_shared_info data structure.To access that structure,functions need to use the skb_shinfo macro,which simply returns the end pointer.

List management functions

skb_queue_head_init: initializes an sk_buff_head with an empty queue of elements.

skb_queue_head,skb_queue_tail:Adds one buffer to the head or to the tail of a queue,respectively

skb_dequeue,skb_dequeue_tail:Dequeues an element from the head or from the tail.

skb_queue_purge:Empties a queue.

skb_queue_walk:Runs a loop on each element of a queue in turn.

转载于:https://www.cnblogs.com/r1ng0/p/9940205.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
struct sk_buff是Linux内核中定义的网络数据包缓冲区的结构体。它包含了各种用于存储和操作网络数据包的信息和属性。 struct sk_buff的定义位于`include/linux/skbuff.h`头文件中,其结构如下: ```c struct sk_buff { struct sk_buff *next; struct sk_buff *prev; struct sk_buff_head *list; struct sock *sk; struct net_device *dev; struct net_device *real_dev; unsigned char *head; unsigned char *data; unsigned char *tail; unsigned char *end; unsigned int len; unsigned int data_len; unsigned int truesize; unsigned int csum; unsigned int ip_summed; // ... }; ``` 下面是一些常用的struct sk_buff成员变量的解释: - next和prev:用于将多个sk_buff连接成链表,方便管理和处理。 - list:指向所属链表的头部,用于链表操作。 - sk:指向关联的socket结构,用于与网络套接字相关的操作。 - dev:指向接收/发送该数据包的网络设备。 - head、data、tail和end:指针,用于定位数据包的不同部分,如头部、数据、尾部和缓冲区末尾。 - len:数据包的总长度(包括头部和数据)。 - data_len:数据长度,即实际有效数据的长度。 - truesize:sk_buff实际占用的内存大小。 - csum:数据包的校验和。 - ip_summed:用于指示是否进行IP层校验和计算。 除了上述成员变量外,struct sk_buff还包含其他一些用于网络协议处理的字段,如协议类型、标志位等。 通过使用struct sk_buff,Linux内核可以高效地传递、处理和管理网络数据包。它提供了丰富的属性和方法,使得网络协议栈能够对数据包进行灵活的操作和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值