Transport layer protocols - TCP, UDP

Table of contents

1. UDP protocol (User Datagram Protocol)

Protocol features

Message header format

2. TCP protocol (Transmission Control Protocol)

Protocol features

Message header format

Three-way handshake when establishing TCP connection

The four waves of TCP tearing down a connection

TCP flow control

TCP congestion control

3. Transport layer port number

Type III port number

Some well-known port numbers


agreement type Is it connection-oriented? Is it reliable? Transmission efficiency overhead way of communication
UDP no no high Small Single point/multipoint to single point/multipoint
TCP yes yes Low big point to point

1. UDP protocol (User Datagram Protocol)

Protocol features

  • no connection
  • Message-oriented
  • Small header overhead (8 bytes)
  • Best effort delivery, no guarantees of reliability, no congestion control provided
  • Support one or many to one or many communication

Message header format

2. TCP protocol (Transmission Control Protocol)

Protocol features

  • Oriented to byte stream (process the data handed over by the application layer as bytes and number them accordingly)
  • connection oriented
  • Each TCP connection has only two endpoints (one-to-one connection)
  • Guaranteed reliable delivery
  • Full duplex communication possible

Message header format

The source port and destination port range from 0 to 65535;

The sequence number (seq) represents the sequence number of the first byte of the data in this segment, and the value is 0 ~ 2^{32}- 1;

The acknowledgment number (ack) indicates the sequence number of the first data byte expected to be received from the other party's next message segment, and the value is 0 ~ - 2^{32}1;

Data offset represents the length of the TCP header, which can easily identify the starting position of the data segment. The minimum length is 5 (0101), and the unit is bytes. The minimum length of the header is 20 bytes;

Urgent pointer When the URG bit is 1, this field is valid and will indicate the data that needs to be sent urgently;

The padding field has a variable length and is used to ensure that the header length of the TCP message is an integer multiple of 4 bytes;

Field Placeholder illustrate
control field URG (1bit) Emergency bit ; when it is 1, it means there is data to be sent urgently
ACK(1bit) Confirmation bit ; it is uppercase here to distinguish it from the confirmation number (lowercase ack); when it is 1, the confirmation field is valid, indicating a positive response
PSH(1bit) Push bit ; when set to 1, TCP packets are sent immediately
RST(1bit) Reset bit ; when set to 1, it indicates that there is a serious error in the TCP connection and the connection needs to be re-established; it can also be used to reject a connection
SYN(1bit) Synchronization bit ; when set to 1, it indicates that a TCP connection is being established
FIN(1bit) Termination bit ; when set to 1, it indicates that a TCP connection is being released
window 16 bit Receives the idle window value for TCP flow control

Three-way handshake when establishing TCP connection

The role of the three-way handshake is to avoid incorrect connections

        Host A sends SYN to server B to request the establishment of a TCP connection. Even if there is no data, it will consume one seq. At this time, host A is in the SYN-SENT state until it does not receive the first response from the target server.

        Server B is in the listening state of LISTEN before receiving a TCP connection request.

        The server responds to host A with SYN, indicating that it also requests host A to establish a connection. ACK=1 indicates that it has agreed to the connection request of host A. Server B also consumes a seq, seq = y, ack = x + 1, indicating confirmation. Receive the data content just sent by the host, and put forward the expectation that the seq when host A sends a message to server B next time is x + 1. At this time, server B needs to wait for the response from host A, and it also changes to the SYN-SENT state.

        Host A receives the response from server B and then responds to server B. ACK=1 indicates that it has confirmed server B's TCP establishment request and responds to service B's expectations. Even if there is no data, it sends a seq, seq = x + 1, At the same time, ack = y + 1 is also a confirmation of the content just sent by server B. It also raises expectations that the seq of the next time the service type sends a message is y + 1. At the same time, the status is set to ESTABLISHED, indicating that the connection has been established.

        Server B receives the response from host A and sets the status to ESTABLISHED after confirming that it is correct, indicating that the TCP connection is successfully established.

        At this time, host A and server B have established a TCP connection, and both parties can transmit data normally.

state illustrate
SYN-SENT A SYN request is sent, requesting the first step in establishing a TCP connection.
LISTEN Monitor the status of SYN messages
SYN-RCVD Status after receiving SYN request message
ESTABLISHED TCP connection established successfully status

The four waves of TCP tearing down a connection

        When host A sends the FIN = 1 (disconnect) flag, it will enter the FIN-WAIT-1 state and wait for the other party to confirm the shutdown. When the host receives the ACK = 1 (confirmation) flag from the other party, host A will enter the FIN-WAIT-2 state. At this time, host A has disconnected the connection to the other party, but has not completely disconnected the TCP connection. It is still waiting for the other party to send a FIN flag to confirm the other party's closure.

        Only after host A receives the other party's FIN flag and sends an ACK flag to confirm, it will enter the TIME-WAIT state. At this time, the host actually closes the TCP connection.

state illustrate
FIN-WAIT-1 Sent the first FIN request, waiting for confirmation from the other party
FIN-WAIT-2 Waiting for the other party's FIN request
CLOSE-WAIT Wait for the own process to issue a disconnect command
LAST-ACK Wait for the other party’s last confirmation
TIME-WAIT Wait enough time to ensure the other party receives the confirmation
CLOSED Disabled

TCP flow control

        Flow control is for communication between two ends . What is considered is the difference between the data transfer rate between the sender and the receiver.

        TCP flow control is implemented through a variable-size sliding window mechanism . That is, the transmission rate of the data sending end should not be too fast, so that the data receiving end can receive it in time.
    

        The data transfer window win is agreed upon by both parties when the connection is established. However, during the communication process, the data receiving end can dynamically adjust the other party's transmission window size at any time according to its own resource conditions. (After negotiation above, the window size is 3)

        The receiving end places this window value in the window field in the header of the TCP message and transmits it to the sending end.

        The receiver usually sends an ACK message immediately after receiving the data to confirm receipt of the data and notify the sender of the window size at this time. If the receiver's buffer is full, it sets the window size to 0 and the sender stops sending data. When the receiver finishes processing the data, it will set the window size to a value greater than 0 and send an ACK message to notify the sender so that the sender can continue to send data.

TCP congestion control

        Congestion control is for the communication of the entire network. It considers the resource utilization of each router and line in the network to avoid network congestion caused by excessive data traffic.

        Four common congestion control methods:

  • slow start
  • congestion avoidance
  • Fast retransmission
  • Quick recovery

Congestion window (cwnd) : It is a state variable maintained by the sender. Its size depends on the degree of network congestion and will change dynamically;

Slow start threshold (ssthresh) : A threshold set to prevent the congestion window cwnd from growing too large and causing network congestion;

cwnd < ssthresh Perform slow start algorithm
cwnd = ssthresh You can use the slow start algorithm or the congestion avoidance algorithm
cwnd > ssthresh Use congestion avoidance algorithm

Slow start : after each transmission round, the value of cwnd will double , increasing cwnd from small to large;

Congestion avoidance : After each transmission round, the value of cwnd will be increased by 1 , and cwnd will slowly increase from small to large;

Fast retransmission : The receiver will immediately send a duplicate acknowledgment ACK after receiving an out-of-sequence segment. If the sender receives three duplicate acknowledgments in a row , it will immediately retransmit the segment that the other party has not yet received . Therefore, unacknowledged messages can be retransmitted as early as possible instead of waiting for timeout;

Fast recovery : When the sender receives three consecutive retransmission confirmation ACKs , it performs a multiplicative reduction algorithm , halving the threshold value (half of the current cwnd value), and setting the cwnd value to the halved threshold. value and perform the congestion avoidance algorithm;

Multiplicative reduction : When a timeout occurs (possibly during congestion), ssthresh is halved (half of the current cwnd value), and the slow start algorithm is executed at the same time;

Additive increase : execute congestion avoidance algorithm to increase cwnd;

 

        As shown in the figure above, the initial slow start threshold is 16, and cwnd starts from 1. Whenever the sender receives an ACK confirmation message, cwnd will increase (the slow start algorithm is used to increase before reaching the threshold).

        [① ~ ②] When the threshold value is reached, the congestion avoidance algorithm is used to slowly increase cwnd;

        [② ~ ③] If a timeout occurs when cwnd = 24, the threshold is halved, that is, ssthresh = 12. Then the next time an ACK confirmation message is received, cwnd starts from 1 and increases using the slow start algorithm;

        [③ ~ ④] When cwnd = 12, when the threshold is reached, the congestion avoidance algorithm is used to slowly increase;

        [④ ~ ⑤] When ④ reaches ④, cwnd = 16, and the sender receives three duplicate confirmations in a row, the threshold is reduced to half of the current cwnd, that is, ssthresh = 8, and cwnd becomes consistent with the threshold value;

        [⑤ ~] Since cwnd = ssthresh, start using the congestion avoidance algorithm to increase;

3. Transport layer port number

Type III port number

  • Well-known ports 0 ~ 1023
  • Register port 1024 ~ 49151
  • Client port number 49152 ~ 65535

Some well-known port numbers

The port number Service type
TCP 20 File Transfer Protocol (Data)
TCP 21 File Transfer Protocol (Control)
TCP 23 Telnet terminal emulation protocol
TCP 25 SMTP Simple Mailing Protocol
TCP 80 Hypertext Transfer Protocol (HTTP)
TCP 110 POP3 server (mailbox receiving server)
TCP 443 Hypertext Transfer Protocol Secure (HTTPS)

 

The port number Service type
UDP 53 Domain Name Server (DNS)
UDP 67 DHCP (server)
UDP 68 DHCP (client)
UDP 69 Simple Hypertext Transfer Protocol (TFTP)
UDP 161 SNMP (polling)
UDP 162 SNMP (trap)
UDP 520 RIP routing protocol

Guess you like

Origin blog.csdn.net/hao_13/article/details/133522962