Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

3.1 TCP Flow and Congestion Control: The State Machine

Lesson 15 of 34 in the free High Speed Networks notes on Siksha Sarovar, written by Rohit Jangra.

3.1.1 The Sliding Window and Flow Control

TCP Flow Control uses the Sliding Window Protocol.

Study Deep: The AIMD Principle

TCP uses Additive Increase / Multiplicative Decrease:

  • Increase: $CWND = CWND + 1$ every RTT (Linear).
  • Decrease: $CWND = CWND / 2$ on loss (Exponential).
  • Why?: This is mathematically proven to be Fair and Stable. If two users compete, the one using more bandwidth gets a larger "cut" during congestion, eventually leading both to an equal share of the link.
  • The Problem: A fast sender (10Gbps) talks to a slow receiver (10Mbps).
  • The Solution: The receiver advertises a "Window" ($RWND$).
  • Math: $TargetWindow = min(CWND, RWND)$.

Pseudocode: Zero Window Persist Timer

void handle_zero_window(TCP_State *s) {
    if (s->advertised_window == 0) {
        s->persist_timer = RTO;
        while (s->advertised_window == 0) {
            wait_for(s->persist_timer);
            send_one_byte_probe(s);
            s->persist_timer = min(MAX_PERSIST, s->persist_timer * 2);
        }
    }
}

3.1.2 TCP Congestion Control Algorithms (SS/CA)

  • Slow Start: $CWND = CWND + 1$ (Exponential).
  • Congestion Avoidance: $CWND = CWND + 1/CWND$ (Linear).

Algorithmic Logic: Congestion State Machine

void on_ack_received(TCP_State *s, int ack_num) {
    if (s->cwnd < s->ssthresh) {
        // Slow Start Phase
        s->cwnd += 1; 
    } else {
        // Congestion Avoidance Phase
        s->cwnd += (1.0 / s->cwnd);
    }
}

void on_loss_detected(TCP_State *s, Event e) {
    if (e == TIMEOUT) {
        s->ssthresh = s->cwnd / 2;
        s->cwnd = 1;
    } else if (e == THREE_DUP_ACKS) {
        s->ssthresh = s->cwnd / 2;
        s->cwnd = s->ssthresh + 3; // Fast Recovery
    }
}

3.1.3 Advanced Extensions (SACK/FACK)

Selective Acknowledgments allow the receiver to report non-contiguous blocks.

  • SACK Block Format: [Left Edge (32 bit), Right Edge (32 bit)].
  • FACK Logic: Tracks $SND.FACK$ (the highest seq SACKed).

SACK Processing Pseudocode:

void process_sack_blocks(TCP_State *s, SackBlock blocks[], int num_blocks) {
    for (int i = 0; i < num_blocks; i++) {
        mark_as_received(s->scoreboard, blocks[i].left, blocks[i].right);
        if (blocks[i].right > s->fack_val) {
            s->fack_val = blocks[i].right;
        }
    }
    s->packets_in_flight = s->max_sent - s->last_ack - total_sacked(s->scoreboard);
}

3.1.4 Jain's Fairness Index

AIMD (Additive Increase Multiplicative Decrease) is mathematically proven to converge to fair sharing. $$J(x_1...x_n) = rac{(sum x_i)^2}{n sum x_i^2}$$