In-depth understanding of Linux Network Technology-key data structures (I)

Source: Internet
Author: User

The Linux network subsystem has some important data structures throughout the entire subsystem, mainly including the following:

Struct sk_buff: Data encapsulation structure. All network layers use this structure to store their headers, user data, and other internal information for coordinating other work. Struct net_device: in the Linux kernel, each network device uses this data structure, including hardware and software configuration information.
I. Socket buffer: The sk_buff structure may be the most important data structure in Linux network code, indicating data packets. This structure is defined in the header file <include/linux/skbuff. h>. It consists of a huge variable heap, trying to meet all the requirements of everyone. The fields of this structure are roughly divided into the following types: general fields of the network layer function dedicated management functions will use this structure at different network layers of the network system, when this structure is transferred from one layer to another, different fields change accordingly. For example, the L4 layer will append a header before being passed to L3, and its own header will be added before generic L3 to L2. Additional headers are more efficient than copying data from one layer to another. The kernel provides the skb_reserve function to add space (that is, to modify the pointer to the buffer header) at the beginning of a buffer. Therefore, when the buffer is passed down to each network layer, the Protocol at each layer must first call the skb_reserve function to reserve space for the protocol header. When the buffer is passed up to the upper-layer network, the header of this layer is not deleted from the buffer. The second is to move the pointer of valid data in a straight line to the header position of the upper layer. Because network code provides a large number of option functions, it is not always necessary, such as firewall, multicast, and connection tracking. These functions are all appended with fields in the sk_buff structure. Therefore, the sk_buff structure contains many fields appended by the C preprocessing # ifdef command. Generally, any option that causes a change in the kernel data structure is not suitable for compiling into a module for dynamic loading. Some fields in sk_buff are used to organize the data structure itself:


  1. Struct sk_buff {
  2. /* These two members must be
    First .*/
  3. Struct sk_buff * next;
  4. Struct sk_buff * prev;

At the same time, in order to quickly locate the entire table header, an additional sk_buff_head structure is added at the beginning of the table as a dummy element. The sk_buff_head structure is:


  1. Struct sk_buff_head {
  2. /* These two members must be first .*/
  3. Struct sk_buff * next;
  4. Struct sk_buff * prev;

  5. _ U32 qlen;
  6. Spinlock_t lock;
  7. };

Qlen is the number of elements in the table, and lock is used to prevent concurrent access to the table. The first two elements of the sk_buff and sk_buff_head structures are the same, so the same function can be used to operate sk_buff and sk_buff_head. The list field in the sk_buff structure points to other fields in the header: sk_buff:
Struct sock * sk: indicates the sock data structure of the socket that owns the buffer zone. This pointer is required when data is generated locally or is being received by a local process, because the data and socket-related information are generated by L4 layer (TCP or UDP) and user applications. When the buffer is only forwarded, the pointer is NULL.
Unsigned int len: the size of the pig data block in the buffer zone. This length includes data in the primary buffer (indicated by the head) and fragment data. When a buffer is transferred from a network layer to the next network layer, its value changes. Because when the protocol stack is moved up, the header is discarded. But when moving down, the header is added, and len calculates the length of the protocol header.
Unsigned int data_len: Unlike len, data_len only calculates the data size in the fragment.
Unsigned int mac_len: size of the MAC header
Atomic_t users: the reference count, or the number of instances using this sk_buff buffer. This parameter is mainly used to prevent the structure from being released by another instance when it is still in use. Users sometimes directly use atimic_inc and atomic_dec functions to increase and decrease, but in most cases, skb_get and kfree_skb are used for processing.
Unsigned int truesize: indicates the total size of the buffer, including the sk_buff structure itself. When this buffer gets the allocated len bytes of data request space, the initialization of this field is set by the alloc_skb function to len + sizeof (sk_buff ). This field is updated whenever the value of skb-> len is increased.
Sk_buff_data_t tailsk_buff_data_t endunsigned char * headunsigned char * data these fields represent the boundary of the buffer zone and the data in it. When each layer prepares a buffer for its work, it may allocate more space for a header or more data. Head and end point to the beginning and end of the allocated space, and data and tail point to the beginning and end of the actual data. Therefore, you can directly fill the header with head and data to add new data between tail and end. Here, tail and end determine whether to use the offset address or pointer based on whether the system uses NET_SKBUFF_DATA_USES_OFFSET.

  1. # Ifdef NET_SKBUFF_DATA_USES_OFFSET
  2. Typedef unsigned int sk_buff_data_t;
  3. # Else
  4. Typedef unsigned char * sk_buff_data_t;
  5. # Endif

Void (* destructor) (struct sk_buff * skb): This function pointer refers to the function that completes some work when the buffer is deleted. When this buffer does not belong to a socket, destructor is usually not initialized. However, a socket is usually set to sock_rfree or sock_wfree. These two functions can be used to update the memory held by the socket queue pig.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.