Interview classic: what's the matter with TCP sticky packets?

Posted by zohab on Sun, 02 Jan 2022 06:39:13 +0100

preface

Questions about TCP protocol are often asked in the interview, especially in the interview of fresh students.

TCP protocol is a connection oriented reliability protocol. To say that it is reliable does not mean that the data information will be accepted by the opposite end, but will give up the retransmission mechanism and interrupt the connection to notify the user after the transmission fails. It only provides reliable data transmission and fault notification.

Back to the problem of TCP packet sticking, TCP is data stream transmission. Data stream is a byte data sequence with only starting point and ending point, and only input stream and output stream. There is no concept of "package".

What do you mean by the "bag" of sticky bag?

In fact, we are talking about the application layer package. The application layer protocol specifies the structure and size of the package, which is essentially a data message.

Specific packages may look like this

//Simple custom application layer protocol
struct Message {
    int packSize;
    int type;
    char buf[100];
};

//http request
GET /hello.txt HTTP/1.1
User-Agent: curl/7.16.3 libcurl/7.16.3 OpenSSL/0.9.7l zlib/1.2.3
Host: www.example.com
Accept-Language: en, mi

Causes of TCP packet sticking

Before talking about the reasons, let me talk about two cases of sticking bags:

Suppose there are two packages A and B, and package A precedes package B

1. The receiving end accepts A part of A packet in the buffer

2. The receiving end accepts A part of packet A and packet B in the buffer

The above situations belong to sticking package.

Sender reason:

TCP connection enables Nagle algorithm optimization by default. When the data to be confirmed on a connection (that is, the sender does not receive the ACK sent back by the receiver), send is called to send the data, only filling the data into the transmission buffer. It is sent together after receiving the ACK.

Receiver reason:

The data is not processed in time, and the data is accumulated in the receiving buffer, resulting in the connection of AB packets.

How to solve sticking package

1. Fixed bag length. Each package is the same size.
2. Set specific end flag.
3. Give the package size in the package.
For example: (fix the first 4 bytes as the packet size)
struct Message {
    int packSize;
    int type;
    char buf[100];
};

The http protocol uses the above methods 2 and 3. http uses' \ r\n 'as the end identifier. When it is a post request, content length identifies the length of the request body.

About Nagle algorithm

Let me talk about the nagle algorithm again. It's relatively simple.

The purpose of Nagle algorithm is to reduce the number of small packets in Wan. In other words, send as much data as possible in one transmission, so as to reduce frequent network interaction.

/* 
* Return 0, if packet can be sent now without violation Nagle's rules:
* 1. It is full sized      
* 2. Or it contains FIN. (already checked by caller)       
* 3. Or TCP_CORK is not set, and TCP_NODELAY is set.        
* 4. Or TCP_CORK is not set, and all sent packets are ACKed.        
* With Minshall's modification: all sent small packets are ACKed.  
*/   
static inline int tcp_nagle_check(const struct tcp_sock *tp,
                                        const struct sk_buff *skb,
                                     unsigned mss_now, int nonagle)
{
  return skb->len < mss_now &&((nonagle & TCP_NAGLE_CORK) ||
  (!nonagle && tp->packets_out && tcp_minshall_check(tp)));
}    

According to the linux source code, you can see under which conditions the data will be sent immediately:

1. When data buffer data > = MSS. (MSS is the maximum message length in TCP connection, and the maximum data length that each message segment can carry when the sender and the receiver negotiate communication)

2. The packet contains FIN options

3.TCP_CORK not set, TCP_NODELAY option settings

4.TCP_CORK is not set and receives ack confirmation from the receiver

summary

TCP sticky packet is the case that the application layer specifies that two or more data packets exist in the receiving buffer at the same time, resulting in subcontracting or combination at the receiving end.

Sticky packets are common and are usually distinguished by specifying packet size or end flag.

Some scenarios are not suitable for using Nagle algorithm. Services such as games that require high real-time do not need to be started.

You can use the TCP option, TCP_NODELAY turns off the Nagle algorithm

Recently, I chatted with several younger college students. They are often asked this question when they are looking for an internship interview,

These are classic interview questions. We are going to make an interview series to explain some real interview questions encountered by fresh students when looking for a job or internship. I hope to make progress with you.

 

Topics: Interview