Simulating Congestion Control in a Network
Congestion control is a crucial aspect of network communication, preventing network overload and ensuring fair resource allocation. This challenge asks you to implement a simplified version of a congestion control algorithm (specifically, TCP Tahoe) in Python to simulate how a sender adjusts its transmission rate based on network feedback (packet loss). Successfully completing this challenge will give you a foundational understanding of how congestion control mechanisms work.
Problem Description
You are tasked with creating a Python simulation of a TCP Tahoe sender attempting to transmit data over a network. The network has a limited bandwidth and experiences packet loss due to congestion. Your simulation should model the sender's behavior in adjusting its congestion window (cwnd) based on observed packet loss.
What needs to be achieved:
- Implement a
Senderclass that simulates a TCP Tahoe sender. - The sender should maintain a congestion window (
cwnd) representing the maximum amount of data it can have in flight (unacknowledged). - The sender should transmit data in segments up to the size of the
cwnd. - The network is simulated by a
Networkclass that introduces packet loss based on a given loss probability. - The sender should react to packet loss by reducing its
cwndaccording to the TCP Tahoe algorithm.
Key Requirements:
- TCP Tahoe Algorithm: When a packet loss is detected (indicated by a timeout or duplicate ACK), the sender should halve its
cwnd(slow start phase is not required for this simplified version). - Network Simulation: The
Networkclass should simulate packet loss based on a given probability. - Data Transmission: The sender should transmit data in segments and track acknowledgements.
- Congestion Window Management: The sender must correctly manage its
cwndbased on packet loss.
Expected Behavior:
The simulation should demonstrate the sender's ability to adapt its transmission rate in response to packet loss. The cwnd should decrease when packet loss is detected, and the sender should transmit data at a rate proportional to the current cwnd.
Edge Cases to Consider:
cwndreaching zero: The sender should stop transmitting data. (For simplicity, you can just setcwndto 1 if it reaches 0).- Loss probability of 0: No packet loss should occur.
- Loss probability of 1: All packets should be lost.
- Initial
cwndvalue.
Examples
Example 1:
Input:
Sender(cwnd=10, segment_size=1000)
Network(loss_probability=0.1)
Data size = 5000
Number of simulations = 1
Output:
cwnd values over time: [10, 10, 10, 5, 5, 5, 5, 5, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Total data transmitted: 4990
Explanation: The sender starts with a cwnd of 10. Packet loss occurs several times, causing the cwnd to halve. The total data transmitted is less than the requested 5000 due to packet loss and cwnd reduction.
Example 2:
Input:
Sender(cwnd=5, segment_size=500)
Network(loss_probability=0.0)
Data size = 2000
Number of simulations = 1
Output:
cwnd values over time: [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
Total data transmitted: 2000
Explanation: With a loss probability of 0, no packets are lost, and the cwnd remains at 5. The sender transmits all 2000 bytes of data.
Example 3: (Edge Case)
Input:
Sender(cwnd=2, segment_size=1000)
Network(loss_probability=1.0)
Data size = 1000
Number of simulations = 1
Output:
cwnd values over time: [2, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Total data transmitted: 0
Explanation: With a loss probability of 1, every packet is lost. The cwnd is halved repeatedly, eventually reaching 1, and then remains at 1. No data is successfully transmitted.
Constraints
cwndmust be an integer.segment_sizemust be a positive integer.loss_probabilitymust be a float between 0.0 and 1.0 (inclusive).- The simulation should run for a fixed number of time steps (e.g., 20).
- The
Data sizemust be a positive integer. - The
Number of simulationsmust be a positive integer.
Notes
- This is a simplified simulation. Real-world TCP Tahoe implementations are more complex.
- Focus on correctly implementing the TCP Tahoe congestion control algorithm.
- You can use random number generation to simulate packet loss.
- Consider using a loop to simulate the transmission process over multiple time steps.
- The
Networkclass is responsible for simulating packet loss. TheSenderclass should not directly control packet loss. - The simulation should output the
cwndvalues at each time step and the total data transmitted. - You don't need to implement acknowledgements explicitly; just track packet loss.
- Assume a fixed segment size.
- The number of simulations is just for testing purposes. For this challenge, you only need to run one simulation.