Skip to content

feat: add congestion control to TCP implementation #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
ad30bad
feat: initial TcpState implementation
MegaRedHand Apr 20, 2025
20d1af9
feat: implement connection and segmentation
MegaRedHand Apr 20, 2025
a8adedc
refactor: move TcpSocket to new implementation
MegaRedHand Apr 22, 2025
c19e42a
refactor: rewrite the handleSegment function
MegaRedHand Apr 24, 2025
f982290
fix
MegaRedHand Apr 24, 2025
9ec0f58
fix: use correct value for read's return
MegaRedHand Apr 24, 2025
b64d4c3
chore: fix lint errors
MegaRedHand Apr 24, 2025
9551c54
chore: use readAll
MegaRedHand Apr 24, 2025
4824ffd
fix: cleanup port handler when stopping server
MegaRedHand Apr 25, 2025
b85c612
fix: cleanup before retrying when listening
MegaRedHand Apr 25, 2025
a9b2889
fix: make timeout scale with simspeed
MegaRedHand Apr 25, 2025
b46c7d8
feat: add packet drop animation
MegaRedHand Apr 25, 2025
aa23092
fix: take into account overflows
MegaRedHand Apr 25, 2025
84a433c
fix: compute windows correctly
MegaRedHand Apr 25, 2025
682a2d8
fix: reset flag after sending packet
MegaRedHand Apr 26, 2025
b5c73e6
chore: added logging for dropped packets
MegaRedHand Apr 26, 2025
1fe984e
chore: fix lints
MegaRedHand Apr 26, 2025
0009390
feat: set seqnum = 0 at start
MegaRedHand Apr 26, 2025
482fb88
fix: add missing return
MegaRedHand Apr 26, 2025
2b05a67
fix: handle syn increasing sequence number
MegaRedHand Apr 26, 2025
9d3a47c
chore: remove unneeded TODOs
MegaRedHand Apr 27, 2025
dd57481
feat: allow choosing resource size in client program
MegaRedHand Apr 27, 2025
3bb288f
fix: make write wait until buffer has space
MegaRedHand Apr 27, 2025
697a8bd
feat: add delay between sent packets
MegaRedHand Apr 28, 2025
7afdac3
feat: add a fixed congestion window
MegaRedHand Apr 28, 2025
f993f8a
feat: increase cwnd for each received ACK
MegaRedHand Apr 28, 2025
dc6481e
feat: implement congestion control state machine
MegaRedHand May 1, 2025
e9c4dba
feat: retransmit first window segment on third duplicate ACK
MegaRedHand May 1, 2025
ea773fc
chore: fix lints
MegaRedHand May 1, 2025
b4d26f3
fix: retransmit without triggering timeout
MegaRedHand May 2, 2025
b127b0e
feat: estimate RTT of connection
MegaRedHand May 2, 2025
b9b5361
feat: use estimated RTT for timeouts
MegaRedHand May 2, 2025
6dd5b7a
fix: remove bytes from buffer once ACKed
MegaRedHand May 3, 2025
910561f
Merge branch 'main' into congestion-control
MegaRedHand May 4, 2025
f2510a3
fix: set initial timeout to 60s
MegaRedHand May 4, 2025
ded6de3
Merge branch 'main' into congestion-control
MegaRedHand May 10, 2025
528666e
fix: use different flag for write closed
MegaRedHand May 10, 2025
05fabb4
fix: use single timer for retransmission timeout
MegaRedHand May 10, 2025
fff8fcf
chore: remove unused functions
MegaRedHand May 10, 2025
326330b
chore: address TODOs
MegaRedHand May 10, 2025
d533926
Merge branch 'main' into congestion-control
MegaRedHand May 10, 2025
2580522
fix: invert if condition
MegaRedHand May 11, 2025
7e6bd1b
refactor: move repeated logic to helper
MegaRedHand May 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/packets/ethernet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export class EthernetFrame {
type: number;
// 46-1500 bytes
// If the payload is smaller than 46 bytes, it is padded.
// TODO: make this an interface
// The payload
payload: FramePayload;
// 4 bytes
Expand Down
86 changes: 69 additions & 17 deletions src/programs/http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@ import { TcpSocket } from "../types/network-modules/tcpModule";
import { ViewHost } from "../types/view-devices";
import { ProgramBase } from "./program_base";

const RESOURCE_MAP = new Map([
["/small", generateResource(1024)],
["/medium", generateResource(102400)],
["/large", generateResource(10485760)],
]);

function generateResource(size: number): Uint8Array {
const resource = new Uint8Array(size);
for (let i = 0; i < size; i++) {
resource[i] = Math.floor(Math.random() * 256);
}
return resource;
}

export class HttpClient extends ProgramBase {
static readonly PROGRAM_NAME = "Send HTTP request";

private dstId: DeviceId;
private resource: string;

protected _parseInputs(inputs: string[]): void {
if (inputs.length !== 1) {
if (inputs.length !== 2) {
console.error(
"HttpClient requires 1 input. " + inputs.length + " were given.",
"HttpClient requires 2 input. " + inputs.length + " were given.",
);
return;
}
this.dstId = parseInt(inputs[0]);
this.resource = inputs[1];
}

protected _run() {
Expand Down Expand Up @@ -50,19 +66,19 @@ export class HttpClient extends ProgramBase {
return;
}

// Encode dummy HTTP request
const httpRequest = "GET / HTTP/1.1\r\nHost: " + dstDevice.ip + "\r\n\r\n";
const content = new TextEncoder().encode(httpRequest);
// Encode HTTP request
const httpRequest = getContentRequest(
this.runner.ip.toString(),
this.resource,
);

// Write request
const socket = await this.runner.tcpConnect(this.dstId);
if (!socket) {
console.warn(
"HttpClient failed to connect to socket. Program cancelled.",
);
console.warn("HttpClient failed to connect");
return;
}
const wrote = await socket.write(content);
const wrote = await socket.write(httpRequest);
if (wrote < 0) {
console.error("HttpClient failed to write to socket");
return;
Expand All @@ -73,20 +89,39 @@ export class HttpClient extends ProgramBase {

// Read response
const buffer = new Uint8Array(1024);
const readLength = await socket.readAll(buffer);
if (readLength < 0) {
console.error("HttpClient failed to read from socket");
return;
const expectedLength = RESOURCE_MAP.get(this.resource)?.length || 0;
let totalRead = 0;
while (totalRead < expectedLength) {
const readLength = await socket.read(buffer);
if (readLength < 0) {
console.error("HttpClient failed to read from socket");
return;
}
totalRead += readLength;
}
}

static getProgramInfo(viewgraph: ViewGraph, srcId: DeviceId): ProgramInfo {
const sizeOptions = [
{ value: "/small", text: "1 KB" },
{ value: "/medium", text: "100 KB" },
{ value: "/large", text: "10 MB" },
];

const programInfo = new ProgramInfo(this.PROGRAM_NAME);
programInfo.withDestinationDropdown(viewgraph, srcId, Layer.App);
programInfo.withDropdown("Size of requested resource", sizeOptions);
return programInfo;
}
}

function getContentRequest(host: string, resource: string): Uint8Array {
const httpRequest =
"GET " + resource + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n";
const content = new TextEncoder().encode(httpRequest);
return content;
}

export class HttpServer extends ProgramBase {
static readonly PROGRAM_NAME = "Serve HTTP requests";

Expand Down Expand Up @@ -155,20 +190,37 @@ export class HttpServer extends ProgramBase {
const buffer = new Uint8Array(1024).fill(0);
const readLength = await socket.readAll(buffer);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const readContents = buffer.slice(0, readLength);
if (readLength < 0) {
console.error("HttpServer failed to read from socket");
return;
}

// TODO: validate request
const requestContents = new TextDecoder().decode(readContents);
const matches = requestContents.match(/GET (.+) HTTP\/1.1/);
if (!matches || matches.length < 2) {
console.error("HttpServer failed to parse request");
return;
}
const resourceContents = RESOURCE_MAP.get(matches[1]);
if (!resourceContents) {
console.error("HttpServer failed to find requested resource");
return;
}

// Encode dummy HTTP response
const httpResponse = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n";
const httpResponse =
"HTTP/1.1 200 OK\r\nContent-Length: " +
resourceContents.length +
"\r\n\r\n";
const content = new TextEncoder().encode(httpResponse);
const wrote = await socket.write(content);
if (wrote < 0) {
if (wrote <= 0) {
console.error("HttpServer failed to write to socket");
return;
}
const wrote2 = await socket.write(resourceContents);
if (wrote2 <= 0) {
console.error("HttpServer failed to write to socket");
return;
}
Expand Down
Loading