9.3.2.2 Network principle (transport layer TCP)

For full details of TCP, refer to the RFC standard document

1. TCP features:

There is a connection, reliable transmission, byte stream oriented, full duplex.

2. TCP datagram:

1. The port number is an important concept of the transport layer.

2. The header of TCP is variable length (UDP is a fixed 8 bytes), the size exists in the 4-bit header length, and 4 bits (0~15) are used to indicate that the length unit is 4 bytes. (The maximum length of the TCP header It is 60 bytes, the first 20 bytes are fixed, the "option" part is optional, large or small)

3.TCP core features:

1. Mechanisms to ensure reliability:

a) Acknowledgment response (core)

1) To label each byte, as long as the first byte is numbered, combined with the length of the message, the number of each byte is determined at this time.

2) The confirmation sequence number indicates that all the data before the sequence number have been received.

 

3) ACK: If ACK is 0, it means that it is an ordinary message. At this time, the confirmation sequence number is invalid. If ACK is 1, the confirmation sequence number is valid at this time, but the sequence number and the confirmation sequence number have nothing to do (the sequence number is the number of this data, and the confirmation sequence number related to the received data).

4) The length of the TCP payload can be known at the IP layer, TCP payload = IP payload - TCP header. 

b) Timeout retransmission: It is equivalent to an important supplement for the confirmation response.

1) After receiving the data, the data should be deduplicated (according to the serial number of TCP as the basis).

2) In the kernel, TCP will arrange a memory space for each socket object, which is equivalent to a queue, called the receiving buffer. Whether the received data will be placed in the buffer or not will be arranged according to the serial number (queue head data The previous serial number must have been read. If it is retransmitted, it will be automatically removed. If the situation of post-sent first occurs, it will be lined up in the queue according to the serial number. If the first sent data is late, it will be blocked and waited. ).

3) Whether the sent message itself is lost or the response message is lost, it will be retransmitted.

4) If multiple retransmissions fail, the TCP connection will be reset.

RST is 1 means reset message. 

c) Three-way wave and four-way handshake (connection management)

1) Three-way handshake:

2) The purpose of the three-way handshake: to verify whether the network is unobstructed, and whether the sending and receiving capabilities of each host are normal, to carry out message negotiation, so that the client and server can transmit data with the same parameters.

3) Why is the three-way handshake three times?

Exactly three times, it can be verified that the sending and receiving functions of both parties are normal, and such information is synchronized to both parties, so twice is not allowed, although two handshakes can also verify the correctness of the communication ability, but the server side I don’t know the information that has been verified in this way. Four times is possible, but it is not necessary. The middle two times can be combined. If you have to disassemble it, it will reduce efficiency.

4) Wave four times:

FIN: Ends the segment. 

  

5) Four-character wave, why is it four times, is it okay to do it three times?

Sometimes it can be three times, sometimes it must be four times, and the middle two times may not be merged. FIN is triggered and controlled by the program code. FIN is triggered only when the socket.close() method is called, or the process ends, and ACK is the system Controlled by the kernel, ACK will be returned immediately after receiving FIN.


6) Data loss when waving four times: the first three data losses will be retransmitted, but the last ACK loss will occur, so the client cannot release resources in between after the last ACK occurs, but should wait for 2 MSL time (1 MSL time is the maximum time required for network communication, a total of 2 times for one round trip).

d) Flow control: According to the processing capacity of the server, control the sending speed of the client (window size)

1) Use the remaining size of the server's receive buffer to measure the processing power of the server.

2) If the ACK sent by the server indicates that the free size of the receiving buffer is 0, the client will suspend sending data (the sending is also packet loss), and after a period of time, the client will send a window detection packet (without carrying any specific data), in order Trigger the server's ACK, and consider the next step based on the ACK returned by the server.

e) Congestion control: overall network transmission capacity

1) Mechanism: Use an experimental method to dynamically adjust the window size. Use a smaller window for transmission, and if the transmission is smooth, increase the window; use a larger window for transmission, and if the transmission is congested (packet loss), adjust the window to a smaller size .

2) Process:

Slow start: just start testing the waters with very small windows.

Exponential growth: In the case of smooth transmission, the congestion window will grow exponentially.

Linear growth: When the exponential growth reaches a certain threshold, linear growth will be triggered.

Regression to small window: In the process of window growth, if packet loss occurs, it is considered that the network is congested, and it will return to the original small window at this time.

Repeat the above process, but adjust the threshold.

Note: actual sending window = min(flow control window, congestion window)

2. Improve transmission efficiency:

a) Sliding window: send a set of data at one time.

1) If you send one by one, most of the time is wasted waiting for ACK, use a sliding window, send multiple data at one time, use a waiting time, and wait for multiple ACKs.

2) The meaning of the window: the number of messages that do not need to wait for ACK to be sent at one time.

3) As shown in the figure below, when 2001 arrives at the client, the client will immediately send the next data, and the client will always keep the number of ACKs waiting for the same.

4) Packet loss processing:

If the ACK is lost, there is no need to do any processing. For example, if the confirmation number 2001 is lost, but the confirmation number 3001 will be returned next time, indicating that the data before 3001 has been received (including 2001).

Fast retransmission: If ordinary data is lost, it must be retransmitted. For example, if the data 2001~3000 is lost, the client continues to send data, and the server will always send the confirmation number 2001. At this time, the client knows the data 2001~3000 Lost. After receiving the same confirmation sequence number multiple times, the client will retransmit.

Note: The fast retransmission here does not conflict with the timeout retransmission above. When the amount of data is small, TCP may not use the sliding window mechanism. In the case of packet loss, the timeout retransmission mechanism will be used. When the amount of data is large, TCP uses a sliding window mechanism and uses a fast retransmission mechanism in case of packet loss.

b) Delayed response:

Mechanism: When the server returns ACK, it delays for a while, so that the server consumes a little data first, so that the receiving buffer will be larger and the client's window will be larger.

c) Piggybacking:

Mechanism: Response and ACK combined into one.

Example: Four waves can be combined into three.

3. Sticky bag problem:

a) The sticky packet problem is that the receiver fetches data in the buffer and cannot distinguish the boundary of a piece of data.

b) To solve the problem, we need to stand at the perspective of the application layer.

1) In the application layer protocol, introduce separators, such as \n.

2) In the application layer protocol, the packet length is introduced

4. TCP exception handling:

a) Process crashes:

The process is gone => the PCB is gone => the file descriptor is released (equivalent to calling the socket, close() method) => send FIN, triggering a four-way handshake.

No difference from the normal situation.

b) Normal shutdown of the host:

A normal shutdown kills all processes, just like a process crash.

c) Host power off:

1. The receiver is powered off: the sender fails to receive the ACK, and the sender will trigger a timeout retransmission. After multiple failures, it will send a reset message (RST field

1) to reset the connection, and release the connection after failure.

2. The sender is powered off: the receiver will send a heartbeat packet (without carrying business data) to trigger ACK, the purpose is to confirm whether the sender is working and whether the network is smooth.

d) The network cable is disconnected:

Refer to c) Host power off.

5. Summary:

Guess you like

Origin blog.csdn.net/m0_73345579/article/details/132253372