TCP stick packet processing-ringbuf Method

Source: Internet
Author: User

A tcp packet sticking refers to the packet data sent by the sender to the receiver when it is received. From the receiving buffer, the header of the next packet is followed by the end of the previous packet data. The adhesive package may be caused by the sender or the receiver. To improve transmission efficiency, the sender often needs to collect enough data before sending a packet of data, resulting in the adhesion of multiple data packets. If the receiving process does not receive data in time, the received data is placed in the system receiving buffer, and the user process may read multiple data packets at the same time. Because the data transmitted by the system is structured data, subcontracting is required.

In order to adapt to high-speed and complex network conditions, we designed and implemented the stick packet processing module. The receiver processes the packets received through the pre-processing process and processes them separately. To facilitate the processing of sticking packets and improve processing efficiency, a circular buffer is used in the receiving process to store received data. The structure is shown in table 1.

Table 1 circular Buffer Structure

Field name

type

meaning

CS

Critical_section

Protection of ring buffer Critical Zone

pringbuf

uint8 *

buffer start position

pread

uint8 *

Start position of the currently unprocessed data

pwrite

uint8 *

end position of the currently unprocessed data

Plastwrite

Uint8 *

End position of the Current Buffer

The ring buffer is bound to each TCP socket. When each socket_obj of TCP is created, a pringbuffer structure is created and initialized. At this time, pringbuf points to the first address of the memory in the ring buffer, and the pread and pwrite pointers also point to it. The plastwrite pointer has no practical significance at this time. The initialized structure 1 is shown in.

Figure1Initialized ring Buffer

When a TCP receiving operation is shipped each time, the memory obtained from the ringbuffer is used as the receiving buffer. Generally, a maximum L1 value is specified as the maximum data size that can be written. In this case, the pwrite value is assigned to the Buf field of buffer_obj, and L1 is assigned to the buflen field. In this way, the data received each time is written into the buffer zone starting with pwrite, and a maximum of two L1 bytes can be written.

Figure 2 annular buffer after Buffer Allocation

If the length of the Pend from pwrite to the end of the buffer during a certain allocation process is not equal to the minimum allocation length L1, in order to improve the receiving efficiency, the last part of memory will be discarded and plastwrite will be marked as pwrite. Then the memory is allocated from pringbuf, 3.

Figure3Use the ring buffer to the end

In special cases, if the Processing Package speed is too slow or the receiving speed is too fast, the unprocessed package may occupy most of the buffer, and there is not enough buffer allocated to the new receiving operation, 4. Report the error directly.

Figure 4 insufficient ring buffer for receiving Buffering

When receiving a packet with a length of L, You need to modify the pointer of the buffer. At this time, the location where data has been written is changed to (pwrite + l), 5.

Figure 5 receive the ring buffer of data with a length of L

After analyzing the process of using the ring buffer, the data received can be summarized into two types: pwrite> pread, And the received but not processed data is located in the buffer zone between pread and pwrite; pwrite <pread, at this time, the data is located between pread to plastwrite and pringbuf to pwrite. The two situations correspond to figure 6 and Figure 7 respectively.

First, analyze figure 6. In this case, pread is the starting position of a package. If L1 is sufficient for a packet header length, the length of the package is obtained and recorded as l. If L1> L, it indicates that a data packet has been received. The pread pointer is modified according to the package type, pointing to the starting position of the next package (pread + l ). At this time, it is still similar to the previous state, so the unpackage continues until L1 is less than the length of one package or less than the header length. Then exit the unpacking process and wait for the data to arrive.

Figure6. Ring buffering with unprocessed data (1)

Figure7. Annular buffering with unprocessed data (2)

Figure 8 is slightly complicated. First, process L1 according to the above process. In one case, after several packets are processed, L1 cannot have one or more packets. If (l1 + l2) is long enough for a package, you need to continue processing. Apply for a memory partition ptemp with the maximum package length, copy the L1 and L2 parts to ptemp, and then perform the unpacking process.

After unpacking, pread switches to a certain position between pringbuf and pwrite, and then returns to figure 6 and continues to unpackage according to Figure 6.

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.