What mechanisms does TCP use to implement reliable data transmission, traffic control, and congestion control?
1. Connect-oriented three-way handshakes are used for reliable object transmission. 2. Use the data window mechanism to negotiate the queue size for data queue transmission. 3. TCP provides reliable transmission stream and virtual connection services for applications by serializing responses and resending data packets when necessary. The following is a good article in the long article: 1. TCP protocol 1. TCP provides reliability through the following methods: ◆ the application is split into data blocks that TCP considers to be the most suitable for sending. The unit of information transmitted by TCP to the IP address is called the packet segment. ◆ When TCP sends a packet segment, it starts a timer and waits for the destination end to confirm receipt of the packet segment. If you cannot remember to receive a confirmation message, it resends the message segment. ◆ When TCP receives data from the other end of the TCP connection, it sends a confirmation message. This confirmation is not sent immediately. Generally, the delay is several seconds. ◆ TCP will maintain the test of its header and data. This is an end-to-end test. The purpose is to detect any changes in data during transmission. If the packet segment is checked and there is an error, TCP will discard the packet segment and will not confirm receipt of the packet segment. ◆ Since the TCP packet segment is transmitted as an IP datagram, the arrival of the IP datagram may be out of order, so the arrival of the TCP packet segment may also be out of order. If necessary, TCP sorts the received data and delivers the received data to the application layer in the correct order. ◆ Since the IP datagram is repeated, the TCP connection must discard the duplicate data. ◆ TCP also provides traffic control. Each side of the TCP connection has a fixed buffer space. The TCP receiving end only allows the other end to send data that can be accepted by the receiving end buffer. This will prevent the buffer overflow of the slow host caused by the fast host. In addition, TCP does not explain the content of the byte stream. 2. TCP Header: TCP data is encapsulated in an IP datagram. The format is as follows: IP header 20 TCP Header 20 TCP Header the format is as follows: 16-bit source port number 16-bit destination port number 32-bit Confirmation No. 4-bit header length keep 6-bit URG ack PSH rst syn fin 16-bit window size 16-bit inspection 16-bit emergency pointer option data note: (1) each TCP segment includes the source and destination port numbers, which are used to find the application processes at the sender and receiver. These two values Add the source IP address of the IP header and the destination IP address to uniquely identify a TCP connection. (2) The sequence number is used to identify the Data byte stream sent from the TCP sending end to the receiving end. It indicates the First Data byte in the packet segment. If byte streams are seen as unidirectional flows between two applications, TCP uses the serial number to count each byte. (3) When a new connection is established, the SYN flag changes to 1. The serial number field contains the initial ISN of the connection selected by the host. The serial number of the first byte of the data to be sent by the host is the ISN plus 1, because the SYN mark uses a serial number. (4) Since each transmitted byte is counted, the validation sequence number includes the next sequence number expected to be received by one end of the sending confirmation. Therefore, the confirmation sequence number should have been successfully received at that time, and the Data byte sequence number should be added with 1. The serial number field is valid only when the ACK flag is 1. (5) Ack is sent at no cost, because the 32-bit validation serial number field is the same as the ACK flag and is always part of the TCP header. Therefore, once a connection is established, this field is always set, and the ACK mark is always set to 1. (6) TCP provides full-duplex services for the application layer. Therefore, each end of the connection must maintain the serial number transmitted in each direction. (7) TCP can be expressed as an East China Window Protocol without confirmation or rejection. Therefore, the validation sequence number in the TCP header indicates that the sender has successfully received the byte, but does not include the byte indicated by the validation sequence number. Currently, the selected parts of the data stream cannot be confirmed. (8) The length of the header must be set because the length of any field is variable. The TCP header can contain up to 60 bytes. (9) multiple of the six flag bits can be set to 1 ◆ URG-Emergency pointer valid ◆ ack-confirm Serial Number Valid ◆ Psh-the receiver should send this packet segment to the application layer as soon as possible ◆ RST-Reconstruction connection ◆ syn-synchronous sequence number is used to initiate a connection ◆ fin-the sender completes the sending task (10) TCP traffic control is provided by the declared window size at each end of the connection. The window size is the number of bytes, starting from the value specified in the validation serial number field. This value is the number of bytes that the receiver expects to receive. The window size is a 16-byte field, so the window size is up to 65535 bytes. (11) test and cover the entire TCP packet end: TCP header and TCP data. This is a mandatory field, which must be calculated and stored by the sender and verified by the receiver. The calculation of the TCP test and the UDP header test is the same as that of the calculation. The pseudo header is also used. (12) The emergency pointer is a positive offset. The sum of values in the yellow Rong serial number field indicates the sequence number of the last byte of the emergency data. The TCP emergency mode is a method in which the sender sends emergency data to the other end. (13) The most common optional field is the maximum Message Size mms. Each connection usually specifies this option in the first packet segment of the communication. It specifies the maximum length of packets that can be received by the local end. Ii. Establishment and termination of TCP Connections 1. Establish a connection protocol (1) the requester sends a SYN segment to indicate the port of the server to which the customer intends to connect, and the initial serial number of the hidden disease (ISN ), the Syn packet segment is 1. (2) the server sends back the SYN packet segment (packet segment 2) containing the server's initial serial number as the response. At the same time, set the validation sequence number to the customer's ISN plus 1 to confirm the customer's Syn packet segment. A syn occupies a sequence number. (3) The customer must set the validation serial number to ISN plus 1 of the server to confirm the SYN packet segment of the server (packet segment 3 ). The three segments are called three-way handshakes. One end of the first SYN is automatically opened, and the other end of the next SYN is passively opened. 2. Connection termination protocol because the TCP connection is full-duplex, each direction must be closed separately. This principle is that when one party completes its data sending task, it can send a fin to terminate the connection in this direction. Receiving a fin only means that there is no data flow between the two parties. a tcp connection can still send data after receiving a fin. First, the party that closes the service will take the initiative to close the service, and the other party will passively close the service. (1) The tcp client sends a fin to disable data transmission from the client to the server (packet segment 4 ). (2) When the server receives the fin, it sends back an ACK and confirms that the serial number is 1 (packet segment 5 ). Like SYN, a fin occupies a sequence number. (3) The server closes the client connection and sends a fin to the client (packet segment 6 ). (4) The customer segment is sent back for confirmation, and the serial number is set to receive the serial number plus 1 (packet segment 7 ). 3. Connection establishment timeout if a connection cannot be established with the server, the client sends a connection request to the server three times. If the server does not respond within the specified time, the connection fails. 4. Maximum message segment length the maximum message segment length of MSs indicates the maximum data block length transmitted from TCP to the other end. When a connection is established, both parties must notify their respective MSS. Generally, the larger the MSS, the better. The larger the packet segment, the more data each packet segment can transmit, and the higher the network utilization compared with the IP address and the TCP header. When TCP sends a SYN packet, it can set the MSS value to the MTU length of the outbound interface minus the IP header and TCP Header Length. For Ethernet, the MSS value can be up to 1460. If the destination address is not local, the MSS value is usually 536 by default. Whether the local address is located depends on the network number. MSS allows the host to limit the length of the data packet sent by the other end, and the host can also control the length of the data packet sent by the host. This will enable a smaller MTU to connect to the host on a network to avoid segmentation. 5. TCP semi-closing TCP provides the ability of one end of the connection to receive data from the other end after sending the connection. This is the half-closing of TCP. The client sends the fin, and the other end sends the ACK packet segment to the fin. When the half-closed end receives the data transfer, it sends the fin to close the connection in this direction. The client then confirms the fin and closes the connection completely. 6. The 2msl connection time_wait status is also called the 2msl wait status. You must select a maximum message segment survival time (MSL) for each TCP ). It is the longest time before any packet segment is discarded on the network. Processing Principle: when TCP executes an active shutdown and returns the last ACK, the connection must stay in the time_wait status for 2msl. This allows TCP to send the last ack again to avoid this ack loss (the other end times out and resends the final fin ). Another result of 2msl wait is that the plug-in defining this connection cannot be used during 2msl wait. 7. The Calm time TCP cannot establish any connection within the MSL seconds of the restart, which is the calm time. 8. The fin_wait_2 status is in the fin_wait_2 status. We have sent the fin and the other end has confirmed it. Only when the process at the other end completes this shutdown will the fin_wait_2 status change to the time_wait status. This means that this end may always be in this state, and the other end will also be in the close_wait state, and will remain in this State until the application layer decides to close it. 9. The RST bit of the TCP Header of the reset packet segment is used for resetting. Generally, TCP sends a reset packet segment no matter an error occurs when a packet is sent to the relevant connection. Major situations: (1) Connection Request to a non-existent port; (2) an exception occurs when a connection is terminated. 10. Simultaneously open in order to process and open at the same time, only one connection is established for simultaneously open, instead of two connections. Both ENDS send SYN almost simultaneously and enter the syn_sent state. When each end receives SYN, the state changes to syn_rcvd, and they send SYN again and confirm the received syn. When both parties receive SYN and corresponding ACK, the status changes to established. A connection that is opened at the same time needs to exchange four packet segments, which is one more time than a normal three-way handshake. 11. When the application layer issues a close command, both ends change from established to fin_wait_1. This will cause both parties to send one fin, and the two fin reach the other end after the network transmission. After receiving the fin, the status changes from fin_wait_1 to closing and the last Ack is sent. When the last Ack is received, the status changes to time_wait. The number of CIDR blocks to be disabled is the same as that of the normally disabled CIDR blocks. 12. For TCP options, each option starts with a 1-byte kind field, indicating the option type. Kind = 1: Option table end (1 byte) kind = 1: No operation (1 byte) kind = 2: Maximum packet segment length (4 bytes) kind = 3: window expansion factor (4 bytes) kind = 8: Timestamp (10 bytes) 3. TCP timeout and retransmission for each TCP connection, TCP manages four different timers. (1) The retransmission timer is used when you want to receive confirmation from the other end. (2) stick to the timer to keep the window size information flowing, even if the other end closes its receiving window. (3) The timer can detect when the other end of an idle connection will crash or restart. (4) The 2msl timer measures the time when a connection is in the time_wait state. 1. The most important measure of TCP timeout and retransmission is the round-trip time (RTT) of a given connection. Because the router and network traffic change, TCP should track these changes and change the timeout accordingly. First, TCP must measure the RTT between sending a byte with a special sequence number and receiving a confirmation containing this byte. 2. Congestion Avoidance algorithm this algorithm assumes that the loss caused by damage to the group is very small, so the loss of the group means that the network between the source host and the target host is blocked. There are two types of group loss indicators: timeout and repeated confirmation. The congestion avoidance algorithm needs to maintain two variables for each connection: A congestion window cwnd and a slow start threshold ssthresh. (1) for a given connection, the initial cwnd is 1 packet segment, and the ssthresh is 65535 bytes. (2) the output of the TCP output routine cannot exceed the size of the cwnd and receiver announcement window. Congestion avoidance is the traffic control used by the sender. The former is the estimation of network congestion felt by the sender, and the latter is related to the available cache size of the receiver on the connection. (3) When congestion occurs, ssthresh is set to the average size of the current window (the minimum size of the window advertised by the cwnd and receiver, but the minimum value is 2 packet segments ). In addition, if timeout causes congestion, cwnd is set to one packet segment. (4) When the new data is confirmed by the other party, the cwnd is added, but the method dependency and whether the process is being started slowly or the congestion is avoided. If cwnd is less than or equal to ssthresh, a slow start is underway; otherwise, congestion is being avoided. 3. Fast retransmission and fast recovery algorithms if we receive 3 or more duplicate ACK packets in a series, it is very likely that a packet segment is lost. So we re-transmit the lost data packet segment without waiting for the timeout timer to overflow. (1) When 3rd duplicate ACK packets are received, set ssthresh to half of the cwnd in the current congestion window and re-transmit the lost packet segment, set cwnd to ssthresh plus a 3-fold packet segment size. (2) When receiving another duplicate ACK, cwnd increases the size of one packet segment and sends one packet group. If this is allowed. (3) When the next ack for confirming the new data arrives, set cwnd to ssthresh. This ack should be used to confirm the re-transmission of step 1 during a round-trip time after retransmission. In addition, this ack should also be used to confirm all intermediate packet segments between the lost group and the first duplicate ack received. 4. ICMP error how does TCP handle the ICMP error returned by a given connection. The most common ICMP errors that TCP can encounter are Origin Site suppression, host unavailability, and network unavailability. (1) A blocked source station causes the congestion window cwnd to be set to the size of one packet segment to initiate a slow start. However, the slow start threshold ssthresh remains unchanged, so the window will open until it opens all the channels or is congested. (2) A received host is not reachable or the network is not reachable, because both errors are considered to be transient. TCP tries to send the data that causes this error, although it may eventually time out. 5. Regrouping: when TCP times out and re-transmits, it does not have to re-transmit the same packet segment. On the contrary, TCP allows re-grouping and sends a large packet segment. This is allowed because TCP uses the byte serial number instead of the segment serial number to identify and confirm the data to be sent. 4. TCP's persistence timer ack transmission is not reliable. That is to say, TCP does not confirm the ACK packet segment. TCP only confirms the ACK packet segments containing data. To prevent both parties from waiting because of the loss of the ACK packet segment, the sender uses a timer to periodically query the receiver. These messages are called Window probes. 5. TCP retention timer if a given connection does not take any action within two hours, the server will send a probe packet segment to the customer. The customer host must be in one of the following four States. (1) The customer's host is still operating normally and accessible from the server. The customer's TCP response is normal, and the server knows that the other party is working normally. The server resets the active timer within two hours. (2) The customer's host has crashed and is shut down or is restarting. In any case, the customer's TCP does not respond. The server will not be able to receive a response to the probe and time out after 75 seconds. A total of 10 probes are sent, with an interval of 75 seconds. (3) The client host crashes and has been restarted. This is a response that the server will receive for its active detection, but this response is a reset that causes the server to terminate the connection. (4) The client host runs normally, but the slave server is inaccessible. Vi. TCP Performance 1. Path MTU discovery: TCP path MTU discovery is performed as follows, TCP uses the output interface or the bottom MTU of the MSS block declared as the actual packet segment size. The MTU path does not allow TCP to exceed the MSS declared by the peer. If no MSS is specified for the peer end, the default value is 536. Once the starting packet segment size is selected, all IP datagram sent by TCP on the connection will be set to DF bit. If the intermediate router needs to shard a data packet with the DF flag, it discards the data packet and generates an ICMP "fragment not allowed" error. If this ICMP error is received, TCP reduces the segment size and retransmits the packet. If the router generates a new ICMP error, the packet segment size is set to the next hop MTU minus the IP address and the TCP Header Length. If an earlier ICMP error occurs, you must try the next possible minimum MTU. 2. The capacity of a persistent fat pipe connection = bandwidth x latency (RTT ). A network with a high bandwidth latency product is called a persistent fat Network (LFN ). A TCP connection running on LFN is called a long fat pipe. The pipeline can be horizontally stretched (a long RTT), vertically increased (higher bandwidth), or stretched in two directions. 3. Window expansion option: the window expansion option increases the TCP Window definition from 16 bits to 32 bits. This is not achieved by modifying the TCP Header, but the TCP Header still uses 16 bits, instead, a 16-bit extended operation is implemented by defining an option. 4. Timestamp Option: The timestamp option enables the sender to place a timestamp value in each segment. The recipient returns this value in confirmation, allowing the sender to calculate the RTT for each received ack.