This article mainly talks about the transport layer of two important protocols TCP and UDP, although in Android development, do not need to understand such a low level, but with theoretical support, writing code is always very confident. Theory guides practice, and practice is the only standard to test theory. Standing on the shoulders of giants and spying on the Internet world.
User Datagram Protocol (UDP)
User datagram protocol (UDP) only adds the function of reuse and division of IP datagram service and error detection. Only for connectionless packets, unreliable transmission characteristics. UDP adds only the header of the data handed over by the application layer and performs special processing before handing it over to the network layer. The user datagram sent from the network layer is unpacked and delivered to the application layer intact.
1, UDP header format
User datagram UDP is divided into two fields: data field and header field. You can analyze the header format of user datagram UDP from the graph.
- Source Port Indicates the original port number, which can be set to 0 when a reply is required
- Destination Port The destination port number must be used when delivering packets at the destination. Otherwise, to whom does the data go?
- Length Indicates the UDP length. The minimum value is 8 bytes. Only the header is available
- Verify and detect whether there are any errors in the user datagram during transmission, and discard any errors.
During transmission, if the destination port of the received UDP packet does not exist, the UDP receiver disclaims the packet and sends an ICMP error packet indicating that the port is unreachable to the sender.
2, pseudo header
When calculating the checksum, add a 12-byte pseudo-header before UDP. This header is not the actual header of the user datagram. The pseudo header does not travel across the network, but is computed to check the sum and temporarily added to the UDP user datagram to get a temporary user datagram.
The UDP checksum is a general step for the sender to calculate the checksum by checking the header and the data part together:
- Fill the header checksum field with 0 (zero)
- Concatenate the dummy header with the user datagram UDP to see a 16-bit string
- If the data section is not an even number of bytes, an all-zero byte is filled (this byte is not sent to the network layer)
- Calculate the sum of these 16 bit words in binary notation
- The sum is then written to the checksum field and can be sent to the network layer.
After receiving the user datagram, the receiver, together with the pseudo header, calculates the sum of these 16-bit words according to the binary inverse code. Without error, the result should all be 1. Otherwise, an error occurs and the packet is discarded.
TCP protocol
TCP protocol as one of the main protocols of the transport layer, has connection oriented, end to end, reliable full duplex communication, byte stream oriented data transmission protocol.
1. TCP packet segment
Although TCP is byte stream oriented, the data unit transmitted by TCP is packet segment. The TCP segment consists of the TCP header and the data segment. The first 20 bytes of the TCP segment header are fixed, followed by 4 x N bytes that can be dynamically added as required. The maximum length is 40 bytes.
- The source port and destination port occupy two bytes each. The TCP splitting function is implemented through ports.
- The sequence number is 4 bytes in the range of [0,232]. TCP is byte stream oriented, and each byte is numbered sequentially. For example, if the sequence number field of a segment is 201 and the length of carried data is 100, the sequence number of the first data segment is 201 and that of the last data segment is 300. When it reaches the maximum range, it starts at 0 again.
- The confirmation number contains four bytes and is the number of the first byte of the next packet segment expected from the recipient. If the confirmation number =N, all data before the number N has been received correctly.
- The data offset is four bits, indicating the distance between the start position of the data part of the packet segment and the start position of the entire packet segment. Indirectly indicate the length of the head.
- Reserved 6, reserved use, currently 0.
- URG (emergency) When URG=1, it indicates that the emergency pointer field is valid and the packet segment has emergency data and should be sent as soon as possible.
- ACK(Acknowledgement) The acknowledgement number is valid only when ACK=1. After a connection is established, all segment ACK values are 1.
- PSH(push) After receiving the packet segment with PSH=1, the recipient delivers the packet to the receiving application as soon as possible, instead of waiting for the entire cache to be filled. Rarely used in practice.
- RST(reset) If RST is 1, a serious error occurs in the TCP connection. You must check whether the connection is established and reconnect the TCP connection.
- SYN(synchronization) is used to synchronize the serial number when establishing a connection. When SYN=1 and ACK=0, it indicates a connection request segment. If SYN=1 and ACK=1, the other party agrees to connect. TCP to establish a connection.
- FIN(terminate) is used to release a connection window. When FIN=1, it indicates that the sender of this segment no longer sends data and requests to release the unidirectional connection. TCP disconnected.
- The window is 2 bytes and represents the receiving window of the sender. The window value is used to tell the sender how much data is allowed to be sent.
- The checksum is 2 bytes, and the validation and field validation range includes the header and the data section.
- The emergency pointer occupies 2 bytes. When URG=1, the emergency pointer indicates the number of bytes of the emergency data in the text segment (after the emergency bytes, the number of ordinary bytes).
- The options are variable in length, up to 40 bytes. For example, MSS. MSS refers to the length of the data segment rather than the length of the entire TCP packet segment. The default LENGTH of MSS is 536 bytes. Window expansion, timestamp options, etc.
2. TCP Establishes a connection – three-way handshake
The legend of the three-way handshake is shown below, and it is better to use it with text explanation.
For the first time,
Second time: After receiving the packet, the server sends back an acknowledgement packet with ACK=1 and ACK= x+1. Because the client needs to acknowledge the packet, the packet also contains SYN=1 and SEq =y. After the message is sent, it enters the SYN_RCVD state.
Third time: After receiving the packet, the client sends an acknowledgement packet with ACK=1 and ACK= y+1. After sending the packet, the client enters the ESTABLISHED state. After receiving the packet, the server enters the ESTABLISHED state. At this point, the connection is established.
Reasons for three handshakes
Avoid wasting resources. If the acknowledgement packet cannot reach the client in time due to network delay during the second handshake, the client considers the first handshake failed and sends the connection request again. After receiving the handshake, the server sends the acknowledgement packet again. In this case, the server has already created two connections and is waiting for two clients to send data when only one client is actually sending data.
3. TCP Disconnect – Four waves
Four-wave indicates that the client and server each send a packet to terminate the connection, and both sides respond to each other’s request. Four wave legend as follows, please configure text explanation to use oh.
First wave
FIN_WAIT_1
Second wave: After receiving the request packet, the server sends back ACK=1 and ACK= x+1 to confirm the disconnection. The server enters the CLOSE_WAIT state. After receiving the packet, the client enters the FIN_WAIT_2 state. The data connection between the client and the server is disconnected.
Third wave: The server sends a packet with FIN=1 and SEq =y to the client, indicating that it has no data to send to the client. After the packet is sent, the system enters the LAST_ACK state and waits for the client to confirm the packet.
Fourth wave: After receiving the request packet, the client sends ACK=1 and ACK= y+1 to the server and enters the TIME_WAIT state. It may want to retransmit the acknowledgement packet. After receiving the confirmation packet, the server enters the CLOSED state and the connection between the server and client is disconnected. The client also enters the CLOSED state after a period of time.
Because TCP connections are full-duplex, both parties can actively transmit data. The disconnection of one party needs to be notified to the other party so that the other party can perform related operations and act responsibly.
The TCP protocols include FTP (file transfer protocol), Telnet (remote login protocol), SMTP (Simple mail transfer protocol), POP3 (as opposed to SMTP, used for receiving mails), and HTTP
4. TCP flow control
Sliding window protocol
TCP sliding window protocol is mainly used to solve the problem of inconsistent data transmission rates between sender and receiver during data transmission on the network, so as to ensure the reliability of data transmission and achieve the effect of flow control. There are three types of data in the sender:
- Send confirmed
- Send without confirmation
- Did not send
The receiver data is divided into three types:
- Received and acknowledged but not read by the upper layer
- Unconfirmed receipt
In the sliding window of the sender, it can be divided into sending window and available window. The data in the sending window has been sent to the receiver, but has not received the acknowledgement from the receiver; The available window indicates how much more data the sender can send. The size of the sender window changes as the receiver window changes.
After the data is read by the upper layer, the receiver can receive A total of 100 bytes from 501 to 600. Therefore, notify A. The size of the receiving window is 100 and the serial number starts with 501…. In the whole process of the figure above, A receives three times of flow control from B.
TCP segment sending mechanism
After the application layer passes the data to the transmission cache of TCP at the transport layer, TCP controls the sending time of packet segments through different mechanisms. There are three main mechanisms:
- TCP maintains a variable equal to the maximum packet segment length (MSS). When the data stored in the cache reaches MSS bytes, it is sent as a packet segment.
- The send application layer specifies the required packet segment, that is, the push operation supported by TCP.
- When the timer expires, the cached data should be sent as a packet segment, provided that the length cannot exceed MSS.
The TCP transmission efficiency is faulty
Different transmission mechanisms may cause efficiency problems. For example, when a user sends a single character plus a 20-byte header, the TCP packet is 21 bytes long. When the IP header is 21 bytes long, the IP packet is 41 bytes long. Sending one byte requires the line to send a 41-byte IP datagram, and waiting for the receiver to acknowledge it adds another 40-byte datagram to the thread. So when the thread bandwidth is not rich, this transmission efficiency is very low. Therefore, you should delay sending acknowledgements and use piggy-back acknowledgements whenever possible.
Negle algorithm
Negle algorithm is mainly used to solve the transmission efficiency of TCP. According to the Negle algorithm, to cache the sent data byte by byte, the sender needs to send the first byte and then cache the subsequent bytes. After receiving the acknowledgement of the first byte from the receiver, all bytes in the existing cache are composed of a packet segment and sent to cache the subsequent data. Subsequent data is sent only after an acknowledgement from the previous packet is received. This is to reduce the bandwidth used. When the sent data reaches half of the TCP sending window or reaches the maximum length of the packet segment, the packet segment is immediately sent instead of waiting for the acknowledgement from the receiver. This is to improve network throughput.
Confused window syndrome
The TCP receiver’s cache is full. If the upper layer reads one byte at a time from the cache, the receiver can continue to accept a one-byte window and then send an acknowledgement to the sender setting the window to one byte (as mentioned above, IP datagrams are 41 bytes long). If this continues, then the network is very inefficient.
Therefore, the effective solution is to make the receiver wait for a certain period of time to allow the cache space to accept the longest segment, or wait for half of the cache space to be free, and then send an acknowledgement message and inform the current window size.
5. TCP congestion control
congestion
What is congestion? If the demand for a resource in a network exceeds the available portion of the resource for a certain period of time, the network performance deteriorates. This situation is called congestion. Network resources often refer to network link capacity (bandwidth), cache in switching nodes and switching processors.
When congestion occurs, it is usually possible to add network resources, such as bandwidth for more, but this does not cure the symptoms, and may not always be useful. Network congestion is often caused by a number of factors, so the need for congestion control.
Congestion control
Congestion control refers to the prevention of excessive data injection into the network, so that the routers or links in the network can not be loaded. The congestion mechanism is a global process involving all hosts, all routers, and all factors associated with reducing network traffic performance.
The flow control of the sliding window protocol is point-to-point traffic control. It is an end-to-end problem.
TCP congestion control method
TCP implements four congestion control algorithms: slow start, congestion avoidance, fast retransmission, and fast recovery.
Congestion control is based on a congestion window, and the sender maintains a status variable of a congestion window CWND. The window size depends on the congestion level of the network and changes dynamically. The sender will make its sending window equal to the congestion window. The basis for determining network congestion is whether the acknowledgement packet sent by the sender and the receiver times out.
1. Slow start algorithm
Slow start indicates that the host gradually increases the sending window from small to large, that is, increases the value of the congestion window. The initial congestion window CWND is set to a value that does not exceed 2 to 4 maximum segments SMSS.
- If SMSS is less than or equal to 1095 bytes, the value of CWND is 4 x SMSS bytes.
- If SMSS is greater than 1095 but less than or equal to 2190 bytes, the value of CWND is 3 X SMSS bytes and the number of segments cannot exceed three.
- If SMSS is greater than 2190 bytes, then CWND =2 x SMSS bytes, and the number of segments cannot exceed two.
The above regulation limits the size of the initial congestion window.
Slow start CWND can increment up to one SMSS value each time it receives an acknowledgement of a new segment.
- When CWND < SSthRESH, use the slow start algorithm
- When CWND > SSthRESH, use the congestion avoidance algorithm
- When CWND = SSthRESH, slow start or congestion avoidance algorithm
2. Congestion avoidance algorithm
The congestion avoidance algorithm allows the CWND to grow slowly, increasing the CWND by 1 per round, rather than doubling as the slow start algorithm does. It is important to note that the congestion avoidance algorithm only makes the network less congested, rather than avoiding congestion.
As mentioned above, whether the network is congested depends on whether the packet times out. When the network is congested, ssthRESH is set to half of the original value, and then the slow start algorithm is started. As shown in the figure below:
3. Fast retransmission algorithm
The fast retransmission algorithm lets the sender know that an individual segment has been lost. Fast retransmission algorithms require recipients not to wait for their own data to send a piggy-back acknowledgement, but to send an acknowledgement immediately. That is to say, in the case of packet loss, the receiver will repeatedly send the acknowledgement number of the previous segment of the lost packet when receiving new data. After receiving the acknowledgement number for three times, the sender determines that the lost packet segment is indeed lost and immediately retransmits the packet (fast retransmission).
4. Fast recovery algorithm
As mentioned above, the sender will adjust the SSthresh to half of the original after knowing that only the packet segment is lost rather than the network congestion, and then continue to carry out the congestion avoidance algorithm, which is called the fast open recovery algorithm.
5, summary
It can be seen that the four algorithms of TCP congestion control complement each other, and none of them can work together to maintain the congestion control mechanism. Here is the overall flow chart.
The upper limit of the send window should be the minimum between the CWND congestion window and RWND receive window
GitHub
Like it