[Computer Network] More than 10,000 words to clearly explain "Transmission Layer"

Transport layer of computer network

The network layer only sends the packet to the destination host, but the real communication is not the host but the processes in the host. The transport layer provides logical communication between processes, and the transport layer shields the core details of the lower network layer from high-level users, making the application look like there is an end-to-end logical communication channel between two transport layer entities.

Features of UDP and TCP

  • User Datagram Protocol UDP (User Datagram Protocol) is connectionless, it is delivered as much as possible, without congestion control, and is message-oriented (the messages sent from the application are not merged or split, but the UDP header is added), support One-to-one, one-to-many, many-to-one and many-to-many interactive communication.

  • Transmission Control Protocol TCP (Transmission Control Protocol) is connection-oriented, provides reliable delivery, has flow control, congestion control, provides full-duplex communication, and is byte-oriented (the message sent from the application layer is regarded as a byte stream, Organize the byte stream into data blocks of different sizes), and each TCP connection can only be point-to-point (one-to-one).

UDP header format

Insert picture description here

The header field has only 8 bytes, including source port, destination port, length, checksum. The 12-byte pseudo header is temporarily added to calculate the checksum.

TCP header format

Insert picture description here

  • Sequence number : used to number the byte stream. For example, the sequence number is 301, which means that the number of the first byte is 301. If the length of the data carried is 100 bytes, then the sequence number of the next segment should be 401.

  • Acknowledgement number : the sequence number of the next segment expected to be received. For example, B correctly receives a message segment sent by A, the sequence number is 501, and the length of the data carried is 200 bytes. Therefore, B expects the sequence number of the next message segment to be 701, and B sends the confirmation message segment to A The confirmation number is 701.

  • Data offset : refers to the offset of the data part from the beginning of the message segment, in fact it refers to the length of the header.

  • Confirm ACK : When ACK=1, the confirmation number field is valid, otherwise it is invalid. TCP stipulates that ACK must be set to 1 in all message segments transmitted after the connection is established.

  • Synchronization SYN : Used to synchronize the serial number when the connection is established. When SYN=1 and ACK=0, it means that this is a connection request segment. If the other party agrees to establish a connection, SYN=1 and ACK=1 in the response message.

  • Terminate FIN : Used to release a connection. When FIN=1, it means that the data of the sender of this segment has been sent, and the connection is required to be released.

  • Window : The window value serves as the basis for the receiver to let the sender set its sending window. The reason for this limitation is that the receiver's data buffer space is limited.

TCP three-way handshake

Insert picture description here

Suppose A is the client and B is the server.

  • First, B is in the LISTEN (listening) state, waiting for the client's connection request.

  • A sends a connection request message to B, SYN=1, ACK=0, and an initial sequence number x is selected.

  • B receives the connection request message, and if it agrees to establish a connection, it sends a connection confirmation message to A, SYN=1, ACK=1, the confirmation number is x+1, and an initial sequence number y is also selected.

  • After A receives B's connection confirmation message, it also sends a confirmation to B. The confirmation number is y+1 and the sequence number is x+1.

  • After B receives A's confirmation, the connection is established.

Reasons for the three-way handshake

The third handshake is to prevent invalid connection requests from reaching the server, allowing the server to open the connection by mistake.

If the connection request sent by the client stays in the network, it will take a long time to receive the connection confirmation from the server. After the client waits for a timeout and retransmission time, it will request a connection again. But this stranded connection request will eventually reach the server. If the three-way handshake is not performed, the server will open two connections. If there is a third handshake, the client will ignore the connection confirmation sent by the server to the stranded connection request, and will not perform the third handshake, so the connection will not be opened again.

Four waves of TCP

Insert picture description here

The following description does not discuss serial numbers and confirmation numbers, because the rules for serial numbers and confirmation numbers are relatively simple. And do not discuss ACK, because ACK is 1 after the connection is established.

  • A sends a connection release message, FIN=1.

  • B sends an acknowledgment after receiving it. At this time, TCP is in a half-closed state. B can send data to A but A cannot send data to B.

  • When B no longer needs the connection, it sends a connection release message with FIN=1.

  • A sends an acknowledgment after receiving it, enters the TIME-WAIT state, waits for 2 MSL (maximum message survival time) and then releases the connection.

  • B releases the connection after receiving A's confirmation.

Reasons to wave four times

After the client sends the FIN connection release message, the server receives the message and enters the CLOSE-WAIT state. This state is for the server to send the data that has not yet been transmitted. After the transmission is completed, the server will send a FIN connection release message.

TIME_WAIT

The client enters this state after receiving the FIN message from the server. At this time, it does not directly enter the CLOSED state. It also needs to wait for the time 2MSL set by a time timer. There are two reasons for this:

  • Ensure that the last confirmation message can arrive. If B does not receive the confirmation message sent by A, then it will resend the connection release request message. A waits for a period of time to deal with this situation.

  • Waiting for a period of time is to make all the messages generated during the duration of this connection disappear from the network, so that the next new connection will not appear in the old connection request message.

TCP reliable transmission

TCP uses timeout retransmission to achieve reliable transmission: if a message segment that has been sent does not receive an acknowledgment within the timeout period, then the message segment is retransmitted.

The elapsed time from transmission to reception of a message segment is called round-trip time RTT, and the weighted average round-trip time RTTs is calculated as follows:


Among them, 0 ≤ a < 1, RTTs are more susceptible to the influence of RTT as a increases.

The timeout period RTO should be slightly greater than RTTs. The timeout period used by TCP is calculated as follows:


Where RTT d is the weighted average of the deviations.

TCP sliding window

The window is part of the cache and is used to temporarily store the byte stream. The sender and the receiver each have a window. The receiver tells the sender its own window size through the window field in the TCP message segment, and the sender sets its own window size based on this value and other information.

All bytes in the sending window are allowed to be sent, and all bytes in the receiving window are allowed to be received. If the byte on the left of the sending window has been sent and the confirmation is received, then slide the sending window to the right for a certain distance, until the first byte on the left is not sent and confirmed; the sliding of the receiving window is similar to receiving If the byte on the left of the window has been sent to confirm and delivered to the host, slide the receiving window to the right.

The receiving window will only confirm the last byte in the window that arrives in order. For example, the bytes received in the receiving window are {31, 34, 35}, where {31} arrives in order, and {34, 35} No, so only byte 31 is acknowledged. After the sender gets a byte confirmation, it knows that all the bytes before this byte have been received.

TCP flow control

Flow control is to control the sending rate of the sender to ensure that the receiver has time to receive.

The window field in the confirmation message sent by the receiver can be used to control the window size of the sender, thereby affecting the sending rate of the sender. If the window field is set to 0, the sender cannot send data.

TCP congestion control

If the network is congested, packets will be lost, and the sender will continue to retransmit at this time, resulting in higher network congestion. Therefore, when congestion occurs, the sender's rate should be controlled. This is very similar to flow control, but the starting point is different. Flow control is to allow the receiver to have time to receive, and congestion control is to reduce the congestion of the entire network.

TCP mainly uses four algorithms to control congestion: slow start, congestion avoidance, fast retransmission, and fast recovery.

The sender needs to maintain a state variable called the congestion window (cwnd). Pay attention to the difference between the congestion window and the sender window: the congestion window is just a state variable, and it is the sender window that actually determines how much data the sender can send.

In order to facilitate the discussion, make the following assumptions:

  • The receiver has a large enough receiving buffer, so no flow control will occur;
  • Although the TCP window is based on bytes, the size unit of the window is set as a message segment here.

1. Slow start and congestion avoidance

The initial execution of sending starts slowly, let cwnd = 1, the sender can only send 1 segment; when the confirmation is received, cwnd is doubled, so the number of segments that the sender can send afterwards is: 2, 4, 8 …

Note that the slow start will double cwnd every round, which will make cwnd grow very fast, so that the sending speed of the sender increases too fast, and the possibility of network congestion is higher. Set a slow start threshold ssthresh. When cwnd >= ssthresh, congestion avoidance is entered, and cwnd is only increased by 1 in each round.

If a timeout occurs, set ssthresh = cwnd / 2, and then re-execute the slow start.

2. Fast retransmission and fast recovery

On the receiving side, it is required that every time a message segment is received, the last ordered message segment that has been received should be confirmed. For example, M 1 and M 2 have been received, and M 4 is received at this time , and an acknowledgment for M 2 should be sent .

On the sender, if three repeated acknowledgments are received, then it can be known that the next segment is lost, and fast retransmission is performed at this time, and the next segment is retransmitted immediately. For example , if three M 2 are received , M 3 is lost, and M 3 is retransmitted immediately .

In this case, only individual segments are lost, not network congestion. Therefore, perform quick recovery, set ssthresh = cwnd / 2 and cwnd = ssthresh, and notice that congestion avoidance is directly entered at this time.

The speed of slow start and fast recovery refers to the set value of cwnd, not the growth rate of cwnd. The slow start cwnd is set to 1, and the fast recovery cwnd is set to ssthresh.

Guess you like

Origin blog.csdn.net/weixin_54707168/article/details/115024230