[Transport layer] Transport Control Protocol (TCP) 1. Main Features of TCP 1) connection oriented 2) point-to-point 3) reliable delivery, error-free, no packet loss, no duplication, on-demand arrival 4) full-duplex communication. Both the receiving end and the sending end have the sending cache and the receiving cache 5) for byte streams: TCP regards the data delivered by the application as "a series of unstructured byte streams", but TCP does not know the meaning of the byte stream. TCP does not guarantee that the data block received by the receiver and the data block sent by the sender application have a corresponding size. Applications should be able to identify received byte streams and restore them to meaningful data. Ii. Details 1) TCP does not care how long a packet is sent to the TCP cache for an application, TCP determines the number of bytes of a packet segment based on the window value and network congestion level provided by the other party. The length of the UDP message is provided by the application. 2) Each TCP link is uniquely identified by the two sockets of the two endpoints. 3. reliable transmission working principle ideal transmission conditions: the transmission channel does not produce errors, no matter how fast the sender is transmitted, the receiver has time to Exit 1) Stop waiting for protocol errors: after sending a piece of data, wait for confirmation. After receiving the confirmation, send the next piece of data. Error: timeout retransmission (greater than A back-and-forth time) A sends the group, saves the copy of the sent group, and discards it after receiving confirmation. Group B and the validation group are numbered so that we can know which validation C has A timeout retransmission time greater than A back-and-forth time to confirm the loss and confirm the delay: A confirms the loss and times out the retransmission, if the recipient clearly sends a confirmation message, but still receives the duplicate data, the recipient discards the duplicate data and sends a confirmation message. B is similar to the loss of confirmation, but when the sender receives a delayed confirmation again, discard and do nothing. 2) Automatic Repeat-ReQuest Sender: moves the window forward to a group every time a group is confirmed. Recipient: Send confirmation to the last group that arrives in sequence (no more than all) by means of accumulated sending confirmation. disadvantage: the message of all groups accepted cannot be reflected to the sender. Go-Back-N: five groups are sent, and the third group is lost. The receiver can only confirm the second packet. In this way, the sender can only re-transmit the last three packets. IV. Implementation of TCP reliable transmission 1) Sliding Window in bytes
2) Sending cache and receiving cache sending cache and receiving cache (1) Sending cache for temporary storage: a) data sent to the sender by the sending application over TCP; B) TCP has sent data but has not received confirmation. (2) the receiving cache is used for temporary storage: a) data that arrives in order but has not been read by the receiving application; B) data that does not arrive in order. 3) Note: (1) the sending window of A is not always as big as the receiving window of B (because of A certain time lag ). (2) The TCP standard does not specify how data that does not arrive in order should be processed. Generally, it is first temporarily stored in the receiving window, and then delivered to the upper-layer application process in order after the missing bytes in the byte stream are received. (3) TCP requires the receiver to have a cumulative validation function, which can reduce the transmission overhead. The recipient can include the confirmation message when a message is sent. However, validation cannot be overly delayed to avoid re-transmission waste. 4) Selection of retransmission mechanism for timeout retransmission is one of the most important and complex problems in TCP. Each time TCP sends a packet segment, a timer is set for this packet segment. As long as the retransmission time set by the timer is reached but no confirmation is received, the packet segment will be retransmitted. 1. The variance of round-trip latency is large because the lower layer of TCP is an Internet environment, and the route selected for IP datagram changes greatly. Therefore, the variance of the round-trip time on the transport layer is also large. 2. Weighted average round-trip time TCP retains a weighted average round-trip RTTS (this is also called a smooth round-trip time) of the RTT ). When the RTT sample is measured for the first time, the RTTS value is taken as the measured RTT sample value. Every time a new RTT sample is measured, the RTTS is recalculated as follows: The New RTTS = (1-a) * (old RTTS) + in the * (New RTT sample) formula, 0 <= a <1. If a is close to zero, the RTT value is updated slowly. If a is set to close to 1, the RTT value is updated quickly. (RFC 2988 recommends a value of 1/8, that is, 0.125) 3 timeout retransmission time RTO (RetransmissionTime-Out) (1) RTO should be slightly greater than the weighted average round-trip time RTTS. (2) RFC 2988 we recommend that you use the following formula to calculate RTO: RTO = RTTS + 4 * RTTD (3) RTTD is the weighted average value of RTT deviation. (4) we recommend that you calculate RTTD in RFC 2988. During the first measurement, the RTTD value is half of the tested RTT sample value. In future measurements, the following formula is used to calculate the weighted average RTTD: New RTTD = (1-B) * (old RTTD) + B * | RTTS-New RTT sample | in formula, B is a coefficient smaller than 1, and its recommended value is 1/4, that is, 0.25. 4. Round-trip time measurement the round-trip time measurement is quite complex. TCP packet segment 1 was not received for confirmation. After retransmission (that is, packet segment 2), The ACK of the validation packet segment is received. However, how can we determine whether the validation message segment is the validation of the original message segment 1 or the validation of the retransmission message Segment 2? The following algorithms provide the following method: When the Karn algorithm calculates the average round-trip time RTT, as long as the packet segment is re-transmitted, it does not use its round-trip time sample. The obtained weighted average round-trip time RTTS and timeout retransmission time RTO are more accurate. Modify the packet segment of the Karn algorithm to increase the RTO value every time it is re-transmitted, as shown in the following formula: In the new RTO = y * (old RTO) formula, the typical value of coefficient y is 2. When the message segment is no longer re-transmitted, the average round-trip delay RTT and timeout retransmission time RTO values are updated based on the round-trip delay of the message segment. Practice has proved that this strategy is more reasonable. 5. Select to confirm the SACK (Selective ACK) when the receiver receives two bytes that are not consecutive with the previous byte stream. If the serial numbers of these bytes are within the receiving window, the receiver will first accept the data, but the information must be accurate to the sender, this prevents the sender from repeatedly sending the received data. RFC 2018 (1) If you want to confirm the selection, you must add the "allow SACK" option in the option of the TCP header when establishing a TCP connection, both parties must agree in advance. (2) If you select "Confirm", the usage of "Confirm number field" in the original header remains unchanged. In the future, the SACK option will be added to the header of the TCP packet segment to report the boundaries of the received discontinuous byte blocks. (3) because the length of the header option is only 40 bytes at most, it takes 4 bytes to specify a boundary. Therefore, the option can only specify the boundary information of up to 4 bytes.