Computer Network - Summary of TCP Protocol

TCP protocol summary

1. Main features

  1. TCP is a connection-oriented transport layer protocol;

  2. Each TCP connection can only have two endpoints ;

  3. TCP provides services that are reliably delivered ;

  4. TCP provides full-duplex communication ;

  5. Oriented to byte streams .

    Endpoint : that is, socket (socket), socket socket = (IP address: port number) TCP connection::= {socket1, socket2} = {(IP1: port1), (IP2: port2)} stream:
    flow into A sequence of bytes streamed from or to a process.
    Oriented to byte streams : TCP only regards the data handed over by the application program as a series of unstructured byte streams, and does not know the meaning of the transmitted byte streams.
    TCP byte stream-oriented concept

2. The difference between TCP and UDP

UDP TCP
Is there a connection? no connection connection oriented
Is it reliable? Unreliable transmission, no use of flow control and congestion control Reliable delivery, using flow control and congestion control
Number of connection objects Support one-to-one, one-to-many, and many-to-one communication Only one-to-one communication
transfer method Datagram byte stream oriented
Initial overhead Low overhead, 8 bytes 20 ~ 60 bytes
Applicable scene Suitable for real-time applications (video conferencing, live broadcast, etc.) Suitable for applications requiring reliable transmission, such as file transfer

3. TCP header

TCP header format
The meanings of the main fields in the header are as follows:

  1. Source port and destination port

    Each occupies 2 bytes, and the source port number and destination port number are written respectively.

  2. serial number

    Occupying 4 bytes, the range is [0, 2 32 -1]. The serial number uses mod 2 32 operation. When the serial number increases to 2 32 -1, the next serial number will return to 0.

  3. Confirmation Number

    Occupying 4 bytes, it is the sequence number of the first data byte expected to be received from the other party's next message segment , usually represented by ack .

  4. data offset

    Occupying 4 bits, it usually indicates the header length of the TCP message segment .

  5. control bit

    1. Urgent URG (URGent)
    When URG = 1, it indicates that this field is valid. The sender TCP will insert emergency data into the front of the data in this segment, usually used in conjunction with the emergency pointer field in the header .
    2. Acknowledgment ACK (ACKnowledgment)
    When ACK = 1, it indicates that this field is valid. TCP stipulates that all transmitted message segments must set ACK to 1 after the connection is established .
    3. Reset RST (ReSeT)
    When RST = 1, it indicates that a serious error occurs in the TCP connection (such as host crash, etc.) and the connection must be released and then the transmission connection is re-established. RST set to 1 is also used to reject an illegal segment or refuse to open a connection.
    4. Synchronous SYN (SYNchronization)
    When SYN = 1 and ACK = 0, it indicates a connection request segment. If the other party agrees to establish a connection, it will use SYN = 1 and ACK = 1 in the response segment.
    5. Abort FIN (FINis)
    When FIN = 1, it indicates that the sender's data has been sent and the connection is required to be released.

  6. window

    Occupying 2 bytes, the window value is an integer between [0, 2 16 -1], which is used as the basis for the receiver to let the sender set its sending window .

  7. Options

    The length is variable, up to 40 bytes, including maximum message segment length (MSS) options, window expansion options, timestamp options, selection confirmation (SACK) options, etc.

4. Three-way handshake - establishing connection

TCP three-way handshake

[ Notes ]:

  1. The TCP client process and server process will first create a transmission control block (TCB) ;

    Transfer Control Block (TCB: Transfer Control Block) : stores some important information in each connection, such as: TCP connection table, pointers to send and receive buffers, pointers to retransmission queues, current send and receive sequence numbers, etc. wait.

  2. TCP stipulates that the SYN segment cannot carry data, but it must consume a sequence number (seq) ;

  3. TCP stipulates that the ACK segment can carry data , but if it does not carry data, the sequence number will not be consumed ;

  4. Why does the client process send the last confirmation?

    Reason : Assume that the first connection request sent by A is stuck at some network nodes, and then A retransmits the connection request. After receiving the confirmation message segment from B, the connection is established and data is transmitted. If the first connection request is received by B at some time after the connection is released, B mistakenly thinks that A has sent a new connection request and sends a message segment to A to agree to establish the connection. However, A did not issue a request to establish a connection, so it ignored B's confirmation and did not send data to B. However, B thought that a new connection was established and kept waiting for A to send data, thus causing a waste of resources.

5. Wave four times - release the connection

TCP four waves

[ Notes ]:

  1. TCP stipulates that a FIN message segment consumes a sequence number even if it does not carry data ;

  2. The sequence numbers u, v, and w are all the sequence number of the last byte of the transmitted data plus 1 ;

  3. MSL (Maximum Segment Lifetime) : The maximum segment life, generally set to 2 minutes . TCP allows the use of smaller MSL values ​​based on specific circumstances;

  4. Why does A have to wait for 2MSL when it is in the time-wait (TIME-WAIT) state ?

    a. In order to ensure that B can enter the CLOSED state normally (that is, the TCP connection is closed normally).

    If B in the LAST-ACK (last acknowledgment) state does not receive the last ACK segment sent by A, it will time out and retransmit the FIN + ACK segment. Within 2MSL, A can receive the segment and retransmit it. Transmit the ACK segment and restart the 2MSL timer until both A and B enter the CLOSED state normally.

    b. Prevent connection request segments from being invalidated due to network lag.

    A After sending the last ACK segment and 2MSL, all the segments generated by this connection will be cleared, so no timeout retransmission will be performed.

  5. The keepalive timer can prevent a sudden failure after the client and server establish a TCP connection, resulting in a waste of server resources.

    The server resets the keep-alive timer every time it receives data from the client, and the duration is generally 2 hours. If no data from the client is received after an interval of 2 hours, the server will send a probe segment and then send it every 75 seconds. If the client still does not respond after sending 10 consecutive probe segments, the server will close the connection.

6. Flow control

Purpose : Control the sending rate of the sender to ensure that the receiver has time to receive.

Note: Flow control often refers to the control of point-to-point traffic, which is an end-to-end problem.

Method : Sliding window mechanism

Both sides of a TCP session maintain a sending window and a receiving window. The size of the receiving window depends on the limitations of the application, system, and hardware, while the size of the sending window depends on the receiving window of the peer communication. The TCP header contains a window field, 16 bits, which represents the byte capacity of the window, with a maximum of 65535. The receiver controls the size of the sending window by sending the window field in the header of the confirmation message. If it is set to 0, the sender cannot send. data.

Note: TCP’s window unit is bytes, not segments.

TCP sliding window

7. Congestion control

Purpose : To prevent excessive data injection into the network and avoid overloading routers or links.

Note: Congestion control is a global process.

Method :

  1. slow-start

    The sender maintains a congestion window cwnd (congestion window) state variable, which is initially set to a value of 1 SMSS. After receiving an acknowledgment of a new message segment, the congestion window is increased to a value of at most one SMSS. After each transmission round, the congestion window is doubled. In order to prevent network congestion caused by excessive growth of cwnd, a slow start threshold ssthresh state variable must be set.

    • When cwnd < ssthresh, use the slow start algorithm;
    • When cwnd = ssthresh, use the slow start algorithm or congestion avoidance algorithm;
    • When cwnd > ssthresh, stop using the slow start algorithm and use the congestion avoidance algorithm instead;
  2. congestion avoidance

    Every time a round trip time RTT passes, the congestion window cwnd of the sender is increased by 1, which slowly increases according to a linear rule and has the characteristics of "Additive Increase" (AI: Additive Increase).

    Regardless of the slow start phase or the congestion avoidance phase, as long as the sender determines that the network is congested (the acknowledgment message is not received on time ), the slow start threshold ssthresh must be set to half of the sending window value (but not less than 2), and then Reset the congestion window to 1 and execute the slow start algorithm. The purpose of this is to quickly reduce the number of packets sent by the host to the network, so that the router experiencing congestion has enough time to process the backlog of packets in the queue .

  3. fast retransmit (fast retransmit)

    The receiver is required not to wait for itself to send data before making a piggyback confirmation, but to send a confirmation immediately , even if an out-of-order message segment is received. It is stipulated that as long as the sender receives three consecutive duplicate confirmations M2, it will immediately retransmit M3. Fast retransmission allows the sender to know as early as possible that a segment has been lost.

  4. fast recovery

    When the sender receives three consecutive repeated acknowledgments, it will halve the slow start threshold ssthresh, then set the value of cwnd to the value of ssthresh after halving, and then start executing the congestion avoidance algorithm. The fast recovery algorithm has the characteristics of "Multiplicative Decrease" (MD: Multiplicative Decrease), and together with the congestion avoidance algorithm is called the AIMD algorithm.

    Note: When using the fast recovery algorithm, the slow start algorithm is only used when the TCP connection is established and the network times out.

TCP congestion control

Guess you like

Origin blog.csdn.net/Alan_Walker688/article/details/126175881