Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Linux TCP/IP Protocol Stack

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the example analysis of the Linux TCP/IP protocol stack. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Characteristics of TCP

We all know very well that the original intention of TCP protocol design is to ensure that the data transmission is fast, orderly and correct. So the characteristics are summarized as follows:

1. Connection-oriented, you can use a quintuple to represent a connection (remote ip, remote port, local ip, local port, transport layer protocol).

2. The data is full-duplex

3. The data is orderly, that is, the accepted data must be in the order in which it was sent.

4. Flow control, the sender can adjust the size of the transmitted data dynamically by sliding the window size of the receiver.

5. Congestion control, the sender calculates the window size comprehensively through the state of ACK and congestion algorithm.

After understanding the characteristic words of TCP, let's really take a look at the process of data transmission.

Data transmission

First of all, let's look at a picture:

The above figure shows the process of data flow in hardware, and the following figure shows the process of data in the protocol stack:

The whole process is divided into three large areas: user area, kernel area and equipment. The equipment mentioned here is the network card. The process is as follows:

1. User application calls write system calls

2. Confirm the file descriptor

3. Copy data to socket buffer

4. Create tcp fragments and calculate checksum

5. Add IP headers, perform ip routing, and calculate checksum

6. Add Ethernet protocol header and execute ARP

7. Tell the network card chip to send data

8. The network card gets the data from memory and sends it, and tells CPU when the interrupt is completed.

Data reception

Look directly at the hardware data flow diagram:

First of all, the network card writes the received data packets into its memory. Then it is checked and sent to the main memory of the host. The buffer in the main memory is assigned by the driver, and the driver will give the assigned buffer description to the network card. If there is not enough buffer to accept the data packet from the network card, the network card will discard the packet. Once the packet has been copied to main memory, the Nic will inform the host OS through an interrupt.

The driver then checks to see if it can handle the new package. If it can handle it, the driver wraps the packet in a linux sk_buffer recognized by OS and pushes it to the upper layer. After receiving the frame, the link layer will decode the frame according to the protocol and push it to the IP layer.

After unpacking, the IP layer decides whether to push to the upper layer or forward to other IP based on the IP information contained in the packet. If it is determined that it needs to be pushed to the upper layer, the IP header will be removed and pushed to the TCP layer.

After decoding, TCP will find the corresponding TCB according to its quad, and then process the message through the TCP protocol. After receiving the message, the message is added to the accept message, and then an ACK is sent to the peer according to the status of the TCP.

Of course, the above process will be affected by NAT and other Netfilter, which is not discussed here, nor has it been studied in depth. Of course, for the sake of performance, Daniel has also made a lot of efforts in many aspects, such as RDMA, DPDK and other software and hardware technologies, as small as zero-copy, checksum offload and so on.

Modern hardware and software TCP/IP protocol stack single link transmission rate to 1~2GiB/s without any problem (measured). If you want to explore better performance, you can try technologies such as RMDA, which optimize performance by bypassing the kernel to reduce copies and, of course, rely on hardware.

This is the end of the sample analysis of the Linux TCP/IP protocol stack. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report