Connection establishment and disconnection of 1.tcp protocol
Second, the timeout retransmission of tcp protocol
Thirdly, the window management of tcp protocol
4. Congestion control of 4.tcp protocol
TCP is a connection-oriented protocol, that is, data can be sent only when the other party exists. TCP realizes reliable transmission through mechanisms such as checksum, sequence number, acknowledgement, retransmission control, connection management and window control.
1. Purpose: TCP three-way handshake means that the client and server always send three packets, which are used to confirm whether the active sending ability and passive receiving ability are normal.
2. Essence: TCP connection is established through the specified quadruple (source address, source port, destination address and destination port), and the synchronization parties send sequence number seq and acknowledgement number ACK respectively, and also exchange window size information.
Three-way handshake process is realized by exchanging sequence numbers seq.
Just find an address on the internet. If you want to view the ip address through the domain name, you can ping it to view the connection.
(1)192.168.3.7 sends [SYN] message segment to 222. 169.228.6438+046, informing that the serial number is 0.
② 222. 169.228. 146 sends the [SYN, ACK] segment to 192. 168.3.7, with the notification serial number y being 0 and the confirmation number ACK being x+1=.
③ 192. 168.3.7 send [ACK] to 222. 169.228. 146, and inform that the confirmation number is y+ 1= 1.
Other parameters in the message segment:
MSS= 1460: the maximum message segment allowed to be received from the other party, which is 1460 bytes in the figure (referring to the data carried, excluding the message segment header).
Win=8 192: the size of the sliding window is 8 192 bytes.
SACK_PERM= 1: Open the selection confirmation. Why SACK is used: TCP acknowledgement mode is not segment-by-segment acknowledgement, but cumulative acknowledgement mode. The message segments received by the server are out of order, so the sequence numbers are also discontinuous, and the receiving queue of the server will be empty. In order to understand the hole, know the current hole in advance, deal with the leakage and take retransmission. The way to know in advance is through SACK option information, which contains the serial number range of the data block that the receiver has successfully received. The SACK_PERM field is 1, indicating that SACK function is turned on.
The network layer may have problems of loss, duplication and disorder. Tcp provides reliable data transmission service. In order to ensure the correctness of the data, tcp will retransmit the packets it thinks have been lost. There are two retransmission mechanisms: one is based on time retransmission, and the other is based on the information provided by the acknowledgement segment.
RTT: The time (round trip time) from the completion of data transmission (the last bit is pushed onto the data link) to the receipt of the acknowledgement signal.
RTO: retransmission timeout (tcp sets a timer when sending data, and when the timer does not receive data confirmation information, it will cause overtime retransmission, and the criterion for judging is RTO).
Thinking: Send four segments with serial numbers 1, 2, 3 and 4, and the segment with serial number 2 is lost. What should I do?
After receiving the confirmation message of seq 1 (ACK=2), the sender waits for the confirmation message of seq=2.
When the receiver receives the message with sequence number 3 (2 has been lost), it will send an acknowledgement message with ack 4 and an acknowledgement message with ack 2. Facing the skipped message, the sender will wait until the specified time is exceeded, and resend message 2.
Why not jump out and confirm?
Tcp is a cumulative acknowledgement method. If message 3 is acknowledged, it means that message 1 and message 2 have been successfully received.
Timeout processing mode:
Thinking: The timer above is based on time retransmission, so retransmission can be determined by the number of times the message is acknowledged.
After receiving the confirmation message of seq 1 (ACK=2), the sender waits for the confirmation message of seq=2.
The receiver receives messages 3, 4 and 5, but does not receive message 2, so the receiver sends three acknowledgement messages with an ACK of 2, and the sender receives these three acknowledgement messages and resends message 2.
Thinking: If there are many lost packets (message 2, message 7, message 9, message 30, message 300 ...) during the fast retransmission, it is very painful to retransmit them from beginning to end.
Thinking: SACK retransmission has no clear rules on how to operate the received repeated data segments. DSACK retransmission can let the sender know which data is repeatedly received and find out why.
The sender did not receive the ACK packet of 100- 199, and sent the message again after the specified time.
The receiver has received the message sent on 200-299, and 100- 199 is a duplicate message. Then send an ACK message to the sender, and set SACK 100- 199 to inform the sender that the 100- 199 pACKet has been received, just in response to the loss of the ack packet.
Due to network delay, the data packet 100- 199 sent by the sender never reached the receiver.
The receiver sends three ACK 200 acknowledgement messages in succession, triggering fast retransmission, and the sender receives ACK 500 acknowledgement messages, indicating that all previous messages have been successfully delivered.
The receiver receives the delayed message 100- 199 and sends the ACK 500 message of SACK 100- 199 to the sender again.
The sender found that this was a duplicate message and judged that it was caused by network delay.
Timer retransmission: according to timeout, retransmission.
Fast retransmission: according to the same ACK message received three times, it is retransmitted once.
Select Confirm Retransmission: Retransmission according to the SACK information provided by the receiving end.
DSACK retransmission: According to the repeated messages, it can be clear whether the ACK message is lost or the network is delayed.
Category 1: send and confirm (ACK message data received).
Category 2: Sent but not confirmed.
Category 3: It will be sent soon.
Category 4: Move the window before sending.
Available window: 46-5 1 byte.
Send window: 32-5 1 byte.
RCV。 NXT: left boundary
RCV。 WND: receive window
RCV。 NXT+RCV。 WND: right boundary
The serial number received by the receiver is smaller than the left boundary, so it is considered as duplicate data and discarded.
The serial number received by the receiver is greater than the right boundary, so it is considered out of the processing range and discarded.
Note: tcp protocol is a cumulative ACK structure. Only when the data sequence number is equal to the left boundary, the data will not be discarded.
If the ACK of the window update is lost, the left boundary of the window moves to the right for the sender. After the transmitted data is acknowledged by ACK, the distance between the left and right boundaries becomes smaller and the sender's window becomes smaller. When the left and right boundaries are equal, it is called zero window. After zero window: When the receiver sends a window update, the window update ACK will be lost.
A detailed description of<& lttcp/ip >; & gt explanation:
TCP realizes flow control through the notification window at the receiving end, and the notification window indicates the amount of data that the receiving end can receive.
When the window value becomes 0, it can effectively prevent the sender from continuing to send until the window size returns to a non-zero value.
When the receiver regains the available space, it will send a window update to the sender to inform it that it can continue to send data. Such window update usually does not contain data (pure ACK), and the ACK of window update sent by the receiver to the sender may be lost. As a result, the two sides were in a waiting state and there was a deadlock.
Solution:
The sender will use a persistent timer to intermittently query the receiver to see if its window has been increased. Trigger window detection and force the receiver to return ACK. Send several probes, the window size is still 0, and then disconnect.
In case of service water system:
① The notification window at the receiving end is too small.
② The data sent by the sender is too small.
Solution:
① For the receiver: Do not publish the small window value.
[RFC 1 122] Description: Before the window can be increased to a full-length message segment (MSS at the receiving end) or half of the buffer space at the receiving end (whichever is smaller), the window value larger than the current window cannot be published. Standard: min(MSS, cache space /2).
② For the sender: Don't send short messages.
Meet at least one of the following requirements:
(1) You can send MSS byte messages.
Window size > = MSS or data size >; =MSS
(2) the length of the data segment >; = half of the maximum window value announced by the receiver before sending.
Receive the ack packet of the data sent before, and then send the data, otherwise, keep the data.
(3) There is no unconfirmed data in-1transmission or -2 connection disables Nagle algorithm.
Tcp realizes flow control according to the size field of the notification window in the ACK packet.
When the network is paralyzed due to large-scale communication load, the default network enters the congestion state, which slows down the transmission of tcp. When the sender and receiver need to undertake overloaded communication tasks, the method of reducing the sending rate or eventually discarding some data is adopted.
The variable reflecting the network transmission capacity is called the congestion window (cwnd).
Announcement window (awnd).
Send window swnd=min(cwnd, awnd)
Objective: tcp can obtain cwnd value before using congestion avoidance algorithm, explore more available bandwidth, and help tcp establish ACK clock.
[RFC568 1]: At the initial stage of transmission, due to the unknown network transmission capacity, it is necessary to slowly detect the available transmission resources to prevent congestion caused by massive data injection in a short time. The slow start algorithm aims to solve this problem. It is necessary to perform a slow start at the beginning of data transmission or after the retransmission timer detects packet loss.
Congestion window value: cwnd is doubled every time an ACK value is received. Therefore, assuming that there are no packet losses and each packet has a corresponding ack value, then swnd= increases exponentially after K rounds.
SMSS is the maximum segment size of the sender.
In the slow start stage, cwnd will increase exponentially, which will help to establish the slow start threshold (ssthresh) soon. With the threshold, tcp will enter the congestion avoidance stage, and cwnd will increase linearly every time, which is similar to the size of the successfully transmitted data segment.
Implementation formula: cwnd+=SMSS*SMSS/cwnd.
When the connection is just established, the slow start algorithm is used. The initial window is 4. After receiving the ACK, cwnd becomes 8. After receiving another ACK, cwnd becomes 16, and then goes down in turn, 32 and 64, reaching the threshold value of ssthresh 64.
Start using congestion avoidance algorithm, and set ssthresh to ssthresh/2 with a value of 32. From the initial window 4, the linearity increases to ssthresh=32.
When cwnd
When cwnd > Ssthresh, congestion avoidance algorithm is used.
The time to apply the fast recovery algorithm: before the fast retransmission starts and the normal out-of-order ACK segment arrives. Start the fast recovery algorithm.
Implementation process:
① set ssthresh to 1/2 cwnd, and set cwnd to ssthresh+3*SMSS.
② Every time a duplicate ACK is received, the cwnd value is temporarily increased by 1 SMSS.
③ After receiving the new data ACK, set cwnd to ssthresh.
Reference:; & gt