# End to end protocols

   An **end-to-end protocol** refers to a communication protocol in computer networks that ensures reliable and efficient data transfer between two endpoints, typically in the  transport layer of the network model. Examples include **TCP (Transmission Control Protocol)** and **UDP (User Datagram Protocol)** in the TCP/IP suite.

### Characteristics of End-to-End Protocols:

1. **Reliability**:
    
    - Ensures data is delivered without errors, loss, or duplication.
    - Uses mechanisms like acknowledgments (ACKs) and retransmissions.
2. **Error Checking**:

    - Incorporates checksums or CRCs (Cyclic Redundancy Checks) to detect and correct errors in transmitted data.
3. **Flow Control**:
    
    - Manages the  data transmission rate to prevent overwhelming the receiver.
    - Common techniques include sliding window protocols.
4. **Congestion Control**:
    
    - Adjusts the rate of data transfer  to avoid network congestion.
5. **Ordered Data Delivery**:
    
    - Ensures that data packets are delivered in the order they were sent, even if they arrive out of order due to network routing.
6. **Connection Management**:
    
    - Establishes, maintains, and terminates connections as needed (e.g., TCP's three-way handshake).
7. **Transparency**:
    
    - Abstracts the complexities of network internals from applications, providing a seamless interface for data communication.
8. **End-to-End Communication**:
    
    - Operates between the source and destination systems, ensuring data is delivered directly to the intended application or process.
### Examples of End-to-End Protocols:

- **TCP**: Ensures reliable and ordered delivery of data.
- **UDP**: Provides faster, connectionless communication, suitable for applications like video streaming where minor losses are acceptable.
- **SCTP (Stream Control Transmission Protocol)**: Combines features of TCP and UDP for certain specialized applications.

# UDP (User datagram protocol)

![[Pasted image 20250106195318.png]] ![[Pasted image 20250106200606.png]]


UDP is a lightweight, connectionless transport layer protocol in the **TCP/IP model**. It facilitates fast data transmission without establishing a connection between the sender and receiver. Unlike TCP, UDP does not provide reliability, ordered delivery, or error recovery, making it ideal for real-time applications where speed is more critical than reliability.

---

### **Characteristics of UDP**:

1. **Connectionless Protocol**:
    
    - Communication happens without establishing a connection between the sender and receiver.
2. **Unreliable Data Transfer**:
    
    - No guarantee of delivery, order, or duplicate protection. Data may be lost or arrive out of sequence.
3. **Lightweight**:
    
    - Minimal protocol overhead due to the absence of connection management and reliability mechanisms.
4. **Fast Transmission**:
    
    - Suitable for scenarios where low latency is crucial.
5. **No Congestion Control**:
    
    - Does not throttle transmission rates based on network conditions, unlike TCP.
6. **Message-Oriented**:
    
    - Treats data as discrete packets or datagrams, with no segmentation or reassembly.
7. **Broadcast and Multicast Support**:
    
    - Can send data to multiple recipients using broadcast or multicast.
8. **Checksum for Error Detection**:
    
    - Includes a checksum for basic error detection, but no correction mechanism is provided.

# TCP (Transmission Control Protocol)

TCP is a **connection-oriented transport layer protocol** in the TCP/IP model. It ensures reliable, ordered, and error-checked delivery of data between sender and receiver, making it suitable for applications requiring accuracy, such as file transfers, emails, and web browsing.

### **Characteristics of TCP**:

1. **Connection-Oriented Protocol**:
    
    - Establishes a reliable connection between sender and receiver before transmitting data (via a **three-way handshake**).
2. **Reliable Data Transfer**:
    
    - Ensures all data is delivered without loss or duplication, using acknowledgments and retransmissions.
3. **Ordered Delivery**:
    
    - Guarantees that packets are reassembled in the correct order at the destination, even if they arrive out of order.
4. **Error Detection and Recovery**:
    
    - Detects errors using checksums and recovers lost or corrupted data through retransmission.
5. **Flow Control**:
    
    - Manages the rate of data transfer to prevent overwhelming the receiver (using mechanisms like **sliding window protocols**).
6. **Congestion Control**:
    
    - Adjusts the sending rate(on the fly, dynamically) based on network congestion to avoid overloading the network.
7. **Stream-Oriented**:
    
    - Treats data as a continuous stream, rather than discrete packets (unlike UDP).
8. **Heavyweight Protocol**:
    
    - Includes additional features for reliability, ordering, and error correction, leading to higher overhead compared to UDP.



![[Pasted image 20250106200806.png]] 
1. **SrcPort (Source Port)**
    
    - Identifies the port of the application sending the data.
    - Helps the receiver determine where to send responses.
2. **DstPort (Destination Port)**
    
    - Specifies the port on the receiving device for the intended service.
    - Commonly used for applications like HTTP (port 80) or HTTPS (port 443).
3. **SequenceNum (Sequence Number)**
    
    - Indicates the position of the first byte in this segment relative to the data stream.
    - Essential for ordering segments and detecting losses.
4. **Acknowledgment (Acknowledgment Number)**
    
    - Specifies the next byte expected by the receiver.
    - Used to confirm the successful reception of data.
5. **HdrLen (Header Length)**
    
    - Specifies the size of the TCP header in 32-bit words.
    - Determines where the actual data begins.
6. **Flags**
    
    - Contains upto 9 control bits(6 usually), such as SYN (connection initiation) and ACK (acknowledgment).
    - Controls connection management and data flow.
7. **AdvertisedWindow (Window Size)**
    
    - Indicates the amount of buffer space available for incoming data.
    - Used for flow control to prevent overwhelming the receiver.
8. **Checksum**
    
    - Ensures the integrity of the header and data.
    - Validates the data for transmission errors.
9. **UrgPtr (Urgent Pointer)**
    
    - Points to the end of urgent data when the URG flag is set.
    - Otherwise, this field is unused and set to 0.
10. **Options**
    - Provides optional parameters like Maximum Segment Size (MSS).
    - Extends the functionality of TCP, increasing the header size.

11. **Data**

    - Contains the actual payload being transmitted.
    - The content varies depending on the application or protocol.

![[Pasted image 20250106201233.png]]

Flow control VS Congestion control

- Flow control involves preventing senders from
	overrunning the capacity of the receivers.

- Congestion control involves preventing too much
	data from being injected into the network,
	thereby causing switches or links to become
	overloaded.

### Silly window syndrome(SWS)

   Silly Window Syndrome (SWS) is an inefficiency in TCP data transmission that occurs when **small amounts of data** are sent or received, leading to **poor utilization of network resources**.

#### **How It Happens:**

1. **On the Sender's Side**:
    
    - The sender transmits very small segments of data repeatedly because the receiver advertises a very small **window size** (e.g., 1 or 2 bytes).
    - This happens if the receiver’s buffer fills up too slowly, allowing tiny increments of free space.
2. **On the Receiver's Side**:
    
    - The receiver advertises a very small window size as soon as a little space becomes available in its buffer, rather than waiting for the buffer to free up significantly.
    - The sender, in turn, sends tiny packets of data to fill this small advertised window, causing inefficient use of bandwidth.

**Consequences of SWS**:

- **Inefficient Network Utilization**:
    - Small packets consume bandwidth but carry very little data, leading to high overhead.
- **Increased Latency**:
    - Tiny packets require frequent acknowledgments, increasing the number of round trips and delaying the overall transmission.


 **Solutions to Avoid SWS**:

1. **Sender-Side Solution (Nagle's Algorithm)**:
    
    - Combines small outgoing packets into larger ones before sending.
    - Ensures no new segment is sent until the previous one is acknowledged, except under specific conditions (e.g., the full-sized segment is ready).
2. **Receiver-Side Solution (Clark's Solution)**:
    
    - The receiver delays advertising window updates until there is enough buffer space available (e.g., a substantial portion of the buffer or a specific threshold like Maximum Segment Size).
    - Prevents the receiver from advertising small, incremental increases in the window size.

### **Flow Control**

Flow control ensures that the **sender does not overwhelm the receiver** by sending more data than the receiver can handle.

1. **Context in the TCP Header**:
    
    - The **Advertised Window** (Window Size) field in the header is the primary mechanism for flow control.
    - The receiver uses this field to inform the sender how much buffer space it has available to receive data.
2. **Mechanism**:
    
    - If the receiver's buffer is almost full, it advertises a smaller window size (even down to zero, signaling "pause transmission").
    - The sender adjusts the amount of data it sends based on the advertised window size.
3. **Objective**:
    
    - Protect the receiver from being overwhelmed by too much data.
    - Ensure smooth data transfer between the sender and receiver.

---

### **Congestion Control**

Congestion control ensures that the **network itself is not overwhelmed**, preventing packet loss and delays caused by excessive traffic.

1. **Context in the TCP Header**:
    
    - Although no single TCP header field explicitly represents congestion control, it uses the **Sequence Number**, **Acknowledgment Number**, and **Window Size** fields indirectly in its algorithms.
    - TCP congestion control algorithms (like Slow Start and Congestion Avoidance) adjust the sender's transmission rate dynamically based on network feedback (e.g., packet loss or delays).
2. **Mechanism**:
    
    - If packet loss is detected (via missing acknowledgments or retransmissions), TCP reduces its sending rate.
    - The sender initially increases its sending rate cautiously (Slow Start) and adapts dynamically based on network conditions.
3. **Objective**:
    
    - Prevent excessive traffic on the network.
    - Achieve fair usage of available bandwidth among all connections.

# TCP congestion control
![[Pasted image 20250106202618.png]]


TCP Congestion Control ensures efficient data transmission while preventing network congestion. It operates through three key phases: **Slow Start**, **Congestion Avoidance**, and **Congestion Detection**. Here's a detailed breakdown:



### 1. **Slow Start**
![[Pasted image 20250106202650.png]]

- **Initial Stage:** At the beginning of a connection, the sender initializes the **congestion window** size to 1 Maximum Segment Size (MSS).
- **Congestion Window Growth:** Each time a new round-trip time (RTT) is completed, the congestion window increases exponentially. After each RTT, the window size is incremented by 1 MSS. This process continues, allowing the sender to quickly probe the network capacity.
- **Formula:**  
    Congestion Window Size=Congestion Window Size+MSS

### 2. **Congestion Avoidance**
![[Pasted image 20250106202700.png]]

- **Threshold Reached:** When the congestion window size reaches a certain threshold, the exponential growth in the window size is replaced by linear growth to avoid overwhelming the network.
- **Linear Growth:** After crossing the slow-start threshold, the sender increments the congestion window by 1 MSS after each RTT, which helps to prevent congestion while still increasing the window size gradually.
- **Formula:**  
    Congestion Window Size=Congestion Window Size+1 MSS
- The sender enters **congestion avoidance** and continues to expand the window size at a controlled pace until congestion is detected.

### 3. **Congestion Detection and Reaction**

TCP has built-in mechanisms for reacting to network congestion, primarily through detecting packet losses.

- **Mild Congestion (Three Duplicate ACKs):**
    - When the sender receives three duplicate ACKs from the receiver (indicating that packets are lost but some have arrived), it detects mild congestion.
    - **Reaction:** The congestion window is halved to reduce traffic on the network, and the **slow-start threshold** is updated to the new window size. This reduces the window and avoids further congestion.
    - The sender then continues in **congestion avoidance** with the reduced window size until another loss is detected.
- **Severe Congestion (Retransmission Timeout):**
    - If the retransmission timer expires (indicating that packets are lost and have not been acknowledged), it signals severe congestion.
    - **Reaction:** The sender immediately reduces the congestion window size to 1 MSS and enters **slow start** again. This is an aggressive recovery method, where the sender gradually probes the network capacity again.
    - **Slow-Start Threshold Adjustment:** The slow-start threshold is updated to half of the current window size. This allows the sender to restart the congestion control process with a smaller window, reducing the risk of congestion.
    - The sender will remain in **slow start** until the congestion window size reaches the new slow-start threshold, after which it switches back to **congestion avoidance** to increase the window size more cautiously.