In this tutorial, you will learn the basic concepts of TCP Flow Control. After reading this tutorial, you will know the different types of mechanisms to control flow and error in TCP.
Contents:
- Reliability of TCP
- Sequence and Acknowledgement Number
- Data Loss and Retransmission
- Selective Acknowledgement
- TCP Flow Control
- Maximum Segment Size (MSS)
- TCP Congestion Control
- Checksum, Acknowledgement, Time-out
Reliability of TCP
TCP is superior to UDP in some applications because it resends lost packets and gives packets a sequence number to indicate their property. Flow control is the major problem on networks, but TCP provides a way to manage the flow of data so that devices don’t overload.
- It may be possible that the TCP segment did not reach the destination or arrived out-of-order. To solve this problem, sequence numbers are assigned to the header of each packet. This is the first byte of the TCP segment.
- When two devices establish a connection using a three-way handshake, the initial sequence number is set, which is the starting value of the segment transmitted to the receiver’s process.
- The sequence number is continuously incremented by the number of bytes that have been transmitted so that the segment can be uniquely identified by the receiver.
- The sequence number is set randomly to prevent the segment from different malicious attacks.
Sequence and Acknowledgement Number
The sender sends the data in bytes along with the sequence number, and when the receiver receives the data, it acknowledges the sender by sending acknowledgment bytes that it has received the data. The acknowledgment number defines the next expected byte, and the sequence number is used to reassemble the data on the receiver side.
- The window size is the field in the TCP segment, which must be set before the acknowledgment can be received.
- Devices can also achieve larger window sizes for larger communications by scaling up the window size.
- When the receiver receives the TCP segment, it sends an acknowledgment to the sender and frees its buffer.
The below diagram explains the use of window size, sequence number, and acknowledgement number in the TCP segment.
- As shown in the figure, PC-1 has to send data. The size of the data is 100 bytes, which is the size of the window. Nowadays, the window size is usually 64 MB or larger. The data contains a sequence number, which uniquely defines the segment.
- PC-2 is sending an acknowledgment that I have now received 100 bytes, I want bytes starting with 101.
- When the sender receives the acknowledgment, it sets the bytes starting with 101 as sequence number 101 and the acknowledgment number as the last sequence number + 1.
Data Loss and Retransmission
On a network, it may be possible that the message is changed during transmission and discarded by the receiver or it may be a loss. TCP has methods for retransmission of lost segments and segments for unacknowledged data.
- When a segment is lost on a network, the sequence number and the acknowledgment number are used for retransmission.
- The receiver sends an ACK used by TCP to indicate the sender about the next byte that the receiver expects to receive. This is known as an exceptional acknowledgment. This means that TCP on the sender side only accepts the expected next byte.
The below diagram explains the exceptional acknowledgement for retransmission.
- As shown in the figure, the sender sends 1 to 6 segments. Segment-1, Segment-2, Segment-5, and Segment-6 successfully reached the receiver but Segment-3 and Segment-4 lost during transmission.
- The receiver sends an acknowledgment ACK-3 stating that it has not received segment-3. When the sender receives ACK-3, it sends all segments from 3 to 6.
- Segment-3 and Segment-4 successfully reach the receiver but Segment-5 and Segment-6 also reach the receiver again, resulting in duplication of segments. Due to this, the network faces delays, congestion, and inefficiencies.
- To avoid this problem, TCP uses the Selective Acknowledgment feature (SACK).
Selective Acknowledgement
If both the sender and the receiver support SACK, the Selective Acknowledgment (SACK) feature is used. Using this feature, the receiver sends an acknowledgment for the lost segments, including the received segments. The sender transmits only those segments that were lost during transmission.
The below diagram explains the selective acknowledgement mechanism.
- As shown in the figure, the sender sends segments 1 to 6. Segment-3 and Segment-4 are lost during transmission.
- Therefore, the receiver sends an acknowledgment to the sender that he has not received Segment-3 and Segment-4, but has received Segment 1, 2, 5, 6. Here, ACK-3 and SACK 5-6 mean that the receiver has received segments 1, 2, and segments 5, 6, respectively.
- The sender only sends segment-3 and segment-4 to the receiver.
- TCP uses a timer to check how long the sender has to wait to resend the segment.
TCP Flow Control
Using a flow control mechanism, the destination receives the amount of data that is reliably processed. In short, flow control is used to maintain the reliability of TCP transmissions by adjusting the flow of data. To achieve flow control, the window size field of the TCP header is used, which is 16-bit.
- It may be possible that the sender sends a large amount of data. Therefore, there is a limit to how much data the sender can carry before receiving the acknowledgment from the receiver. This amount is known as the size of the window, which helps to control the flow.
- The window size is the amount of data that the receiver TCP session can accept and process at once.
The below diagram explains the flow control in TCP.
- As shown in the figure, the sender has set the window size for the TCP session as 5000 bytes.
- The window size is present in each TCP segment, so if the window size needs to be modified at any time, the destination buffer can modify the window size based on availability.
- The initial window size is defined during the three-way handshake mechanism.
- First, the sender sends the first 1250 bytes to the receiver, where the sequence number is 1. Immediately after that, the sender again sends the next 1250 bytes to the receiver.
- The receiver receives a total of 2500 bytes, now it sets 2501 as the acknowledgment number and sends it to the sender that it now wants the bytes starting with 2501. It also sends window size as 5000.
- Increases the sender window size to 2500, which becomes 7500. On receiving ACK 2501 from the receiver, the sender sends the next 1250 bytes to the receiver.This process continues until all the bytes from the sender are received to the receiver.
- If the buffer space is reduced, the window size is reduced on the receiver side.
- In today’s networks, the sliding window protocol is used in which the receiver sends an acknowledgment after receiving two segments.
- The main advantage of the sliding window protocol is that it allows the sender to continuously send data, as long as the receiver acknowledges the segments it receives previously.
Maximum Segment Size (MSS)
As shown in Figure 4, the sender is transmitting 1250 bytes of data with each TCP segment. This is known as the Maximum Segment Size (MSS), which indicates the amount of data the receiver can receive at a time.
- Basically, the option field of the TCP segment defines the MSS, which is used to describe the size of the data.
- MSS is defined when two devices establish communication using a three-way handshake mechanism.
- In IPv4 networks, typically, the size of the MSS is 1460 bytes because it is encapsulated in the data link layer Ethernet frame size of 1500 bytes. In an Ethernet frame, 20 bytes are of the IPv4 header and 20 bytes are of the TCP header and the remaining 1460 bytes are of the payload.
- In Ethernet networks, the default Maximum Transmission Unit (MTU) is 1500 bytes.
The below diagram shows the MSS field in Ethernet Frame.
- As shown in the figure, the Ethernet frame is shown, in which the Ethernet header and the frame check sequence are added to the IP packet.
- IPv4 and TCP header size is 20 bytes. The payload field is the TCP Maximum Segment Size (MSS), which is 1460 bytes.
TCP Congestion Control
When network congestion occurs, the packets are discarded by the router and because of this, they are lost in the network. If segments are lost during transmission, they will not reach the receiver and remain unacknowledged. The sender learns about network congestion by determining the lost packets and the rate at which TCP segments are transmitted.
- Segments losses are caused by error or traffic. The sender retransmits the lost segments to the receiver.
- But if the retransmission is not done properly by the sender and he is not aware of the congestion, then the existing congestion can get worse.
- The transport layer has mechanisms to avoid congestion. For that, it implements a congestion control algorithm and timer.
- If TCP segment acknowledgment is not received, the sender reduces the flow of data.
- When the sender sends a TCP segment, it starts the timer. If an acknowledgment is received before the timer expires, the sender stops the timer. If the acknowledgment is not received from the receiver, the sender waits for the acknowledgment until the timeout and retransmits the TCP segment.
Checksum, Acknowledgement, Time-out
TCP is a reliable protocol which means that it is responsible for transmitting data without error. It uses error control mechanisms to detect corrupted segments, out-of-order segments, and duplicate segments. Checksum, acknowledgment, and time-out are used for error detection and correction in TCP.
Checksum: The checksum is an error detection method and is present in the TCP header to check for a corrupted segment.
- It is 16 bytes and is mandatory in every TCP segment. If the receiver detects that the segment sent is corrupt, it discards the segment, which is considered to be a lost segment.
Acknowledgment: The acknowledgment number is used to confirm that the recipient has received the data sent by the sender. Control segments that consume sequence numbers will be acknowledged, but acknowledgment segments are never acknowledged.
Time-out: Timer is set for retransmission of segments lost during transmission. If the timer expires or the sender receives three duplicate acknowledgments, retransmission occurs.
- TCP maintains RTO (retransmission time-out) for all segments that are sent but not acknowledged.
- RTO value is updated dynamically. The first segment is retransmitted when the timer expires because the sender did not receive the acknowledgment, which may be due to a delayed segment or ACK, or a lost acknowledgment.
Key Points to Remember
Here is the list of key points we need to remember about “TCP Flow Control”.
- Flow control is the major problem on networks, but TCP provides a way to manage the flow of data so that devices don’t overload.
- When a segment is lost on a network, the sequence number and the acknowledgment number are used for retransmission.
- If we use exceptional acknowledgment, the duplicate segment may be received by the receiver. To solve this problem, we use selective acknowledgment.
- Flow control is used to maintain the reliability of TCP transmissions by adjusting the flow of data. To achieve flow control, the window size field of the TCP header is used, which is 16-bit.
- If TCP segment acknowledgment is not received, the sender reduces the flow of data.
- Checksum, acknowledgment, and time-out are used for error detection and correction in TCP.