Repository navigation
Expand file tree
/
Copy pathdocumentation.txt
More file actions
178 lines (168 loc) · 16.1 KB
/
Copy pathdocumentation.txt
File metadata and controls
178 lines (168 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
=====================================
Mini Project 1 Submission
Group Details:
Member 1 Name: Payidiparthy Hadwik
Member 1 Roll Number: 23CS10052
Member 2 Name: Thota Rithvik
Member 2 Roll Number: 23CS10072
=====================================
=======================================================================
SECTION 1: RESULTS TABLE
=======================================================================
File: send.txt (105.7 KB) | Message size: 512 bytes | T: 5s | Window: 10
Total messages per trial: 208 (207 chunks + 1 END marker)
Avg Tx/Msg = Total Transmissions / 208
Expected value = 1/(1-p), from geometric series for independent drops.
|------|---------------------|------------|--------------------|
| p | Total Transmissions | Avg Tx/Msg | Expected (1/(1-p)) |
|------|---------------------|------------|--------------------|
| 0.00 | 208 | 1.000 | 1.000 |
|------|---------------------|------------|--------------------|
| 0.05 | 267 | 1.284 | 1.053 |
|------|---------------------|------------|--------------------|
| 0.10 | 286 | 1.375 | 1.111 |
|------|---------------------|------------|--------------------|
| 0.15 | 397 | 1.909 | 1.176 |
|------|---------------------|------------|--------------------|
| 0.20 | 463 | 2.226 | 1.250 |
|------|---------------------|------------|--------------------|
| 0.25 | 525 | 2.524 | 1.333 |
|------|---------------------|------------|--------------------|
| 0.30 | 560 | 2.692 | 1.429 |
|------|---------------------|------------|--------------------|
| 0.35 | 616 | 2.962 | 1.538 |
|------|---------------------|------------|--------------------|
| 0.40 | 727 | 3.495 | 1.667 |
|------|---------------------|------------|--------------------|
| 0.45 | 859 | 4.130 | 1.818 |
|------|---------------------|------------|--------------------|
| 0.50 | 1043 | 5.014 | 2.000 |
|------|---------------------|------------|--------------------|
Note: Measured Avg Tx/Msg is higher than expected because the expected
value assumes per-packet independent drops, while our implementation
retransmits the ENTIRE sender window on timeout (go-back-N style),
not just the lost packet. This causes more retransmissions than the
theoretical minimum, especially at higher drop probabilities.
=======================================================================
SECTION 2: DATA STRUCTURES (ksocket.h)
=======================================================================
|--------------|-------------------|----------------|----------------------------------------------------------|
| Structure | Field | Type | Description |
|--------------|-------------------|----------------|----------------------------------------------------------|
| KTP_header | seq_num | uint8_t | Sequence number of packet, ranges 1-254 |
| |-------------------|----------------|----------------------------------------------------------|
| | is_ack | uint8_t | 1 for ACK packet, 0 for data packet |
| |-------------------|----------------|----------------------------------------------------------|
| | rwnd_size | uint8_t | Piggybacked receiver window size sent with every ACK |
|--------------|-------------------|----------------|----------------------------------------------------------|
| swnd_struct | window_size | int | Free slots available to send without waiting for ACK |
| |-------------------|----------------|----------------------------------------------------------|
| | seq_num[] | uint8_t array | Seq nums of in-flight unacked messages. 0 = empty slot |
| |-------------------|----------------|----------------------------------------------------------|
| | send_time[] | time_t array | Last send timestamp per slot. 0 = not yet sent |
| |-------------------|----------------|----------------------------------------------------------|
| | next_seq_to_send | uint8_t | Next sequence number to assign to a new message |
|--------------|-------------------|----------------|----------------------------------------------------------|
| rwnd_struct | window_size | int | Free slots in receive buffer, sent back as rwnd_size |
| |-------------------|----------------|----------------------------------------------------------|
| | expected_seq_num | uint8_t | Next in-order sequence number expected |
| |-------------------|----------------|----------------------------------------------------------|
| | seq_num[] | uint8_t array | Seq nums of buffered messages. 0 = empty slot |
| |-------------------|----------------|----------------------------------------------------------|
| | read_seq_num | uint8_t | Next seq num the application will read via k_recvfrom() |
| |-------------------|----------------|----------------------------------------------------------|
| | last_acked_seq | uint8_t | Last cumulative seq num ACKed, piggybacked in ACK |
|--------------|-------------------|----------------|----------------------------------------------------------|
| KTP_sockinfo | is_free | bool | True if this slot is unallocated |
| |-------------------|----------------|----------------------------------------------------------|
| | pid | pid_t | Owning process PID, used by garbage collector |
| |-------------------|----------------|----------------------------------------------------------|
| | udp_socket | int | Underlying UDP socket file descriptor |
| |-------------------|----------------|----------------------------------------------------------|
| | ktp_id | int | Slot index, returned as socket descriptor to user |
| |-------------------|----------------|----------------------------------------------------------|
| | needs_init | int | Flag requesting Thread R to call socket() |
| |-------------------|----------------|----------------------------------------------------------|
| | needs_bind | int | Flag requesting Thread R to call bind() |
| |-------------------|----------------|----------------------------------------------------------|
| | bind_result | int | Result of bind() written back by Thread R |
| |-------------------|----------------|----------------------------------------------------------|
| | needs_close | int | Flag requesting Thread R to close and free slot |
| |-------------------|----------------|----------------------------------------------------------|
| | domain | int | Address family stored for Thread R to use in socket() |
| |-------------------|----------------|----------------------------------------------------------|
| | protocol | int | Protocol stored for Thread R to use in socket() |
| |-------------------|----------------|----------------------------------------------------------|
| | src_addr | sockaddr_in | Bound local IP and port |
| |-------------------|----------------|----------------------------------------------------------|
| | dest_addr | sockaddr_in | Bound remote IP and port |
| |-------------------|----------------|----------------------------------------------------------|
| | send_buffer | char array | Outgoing buffer, MAX_MESSAGES * MSG_SIZE bytes |
| |-------------------|----------------|----------------------------------------------------------|
| | recv_buffer | char array | Incoming buffer, MAX_MESSAGES * MSG_SIZE bytes |
| |-------------------|----------------|----------------------------------------------------------|
| | swnd | swnd_struct | Sender window state for this socket |
| |-------------------|----------------|----------------------------------------------------------|
| | rwnd | rwnd_struct | Receiver window state for this socket |
| |-------------------|----------------|----------------------------------------------------------|
| | nospace | int | Set when rwnd hits 0, triggers dup-ACK on recovery |
|--------------|-------------------|----------------|----------------------------------------------------------|
=======================================================================
SECTION 3: FUNCTIONS IN ksocket.c
=======================================================================
|----------------|--------------------------------------------------------------------------------------------------|
| Function | Description |
|----------------|--------------------------------------------------------------------------------------------------|
| attach_shm() | Lazily attaches to shared memory and semaphore (SHM_KEY=1234, SEM_KEY=5678). |
|----------------|--------------------------------------------------------------------------------------------------|
| get_sm_index() | Finds SM slot by ktp_id. Returns index or -1 if not found. |
|----------------|--------------------------------------------------------------------------------------------------|
| lock_sem() | P operation on semaphore. Blocks until lock is acquired. |
|----------------|--------------------------------------------------------------------------------------------------|
| unlock_sem() | V operation on semaphore. Releases the lock. |
|----------------|--------------------------------------------------------------------------------------------------|
| k_socket() | Allocates SM slot, sets needs_init=1, waits for Thread R to create UDP socket. |
| | Returns slot index as sockfd. Errors: ENOSPACE, EINVAL. |
|----------------|--------------------------------------------------------------------------------------------------|
| k_bind() | Stores src/dest addresses in SM, sets needs_bind=1, waits for Thread R to bind UDP socket. |
| | Returns 0 on success. Errors: EADDRINUSE, EBADF. |
|----------------|--------------------------------------------------------------------------------------------------|
| k_sendto() | Validates destination matches bound address, writes message to send_buffer, assigns seq num, |
| | decrements swnd. Returns bytes copied. Errors: ENOTBOUND, ENOSPACE. |
|----------------|--------------------------------------------------------------------------------------------------|
| k_recvfrom() | Returns next in-order message from recv_buffer matched by read_seq_num. Increments window_size. |
| | Returns -1 with ENOMESSAGE if no message available yet (caller retries with usleep). |
|----------------|--------------------------------------------------------------------------------------------------|
| k_close() | Sets needs_close=1, busy-waits until Thread R marks slot as free. |
|----------------|--------------------------------------------------------------------------------------------------|
| dropMessage(p) | Returns 1 with probability p to simulate packet loss. Called in Thread R after every recvfrom().|
|----------------|--------------------------------------------------------------------------------------------------|
=======================================================================
SECTION 4: FUNCTIONS IN init_ksocket.c
=======================================================================
|-------------------------|-----------------------------------------------------------------------------------------------|
| Function | Description |
|-------------------------|-----------------------------------------------------------------------------------------------|
| lock_sem() | P operation on semaphore. Used by Thread R, Thread S, and GC. |
|-------------------------|-----------------------------------------------------------------------------------------------|
| unlock_sem() | V operation on semaphore. |
|-------------------------|-----------------------------------------------------------------------------------------------|
| thread_R() | Receive thread. On each loop: handles needs_init/needs_bind/needs_close flags, then builds |
| | fd_set of all active UDP sockets and calls select() with 1s timeout. |
| | On data: calls dropMessage(), checks stale/duplicate/out-of-window, buffers valid messages, |
| | sends cumulative ACK with current rwnd_size. Sets nospace=1 if rwnd hits 0. |
| | On ACK: slides swnd using modular arithmetic, updates swnd.window_size from piggybacked rwnd.|
| | On timeout: if nospace set but rwnd has freed up, sends dup-ACK and clears nospace. |
|-------------------------|-----------------------------------------------------------------------------------------------|
| thread_S() | Send/retransmit thread. Wakes every T/2 seconds. For each active slot and send buffer slot: |
| | sends new messages (send_time==0) and retransmits timed-out ones (now - send_time >= T). |
| | Increments total_transmissions on every sendto() call. |
|-------------------------|-----------------------------------------------------------------------------------------------|
| run_garbage_collector() | Forked child process. Wakes every T seconds. Checks if owning PID is alive via kill(pid,0). |
| | Sets needs_close=1 for dead processes to trigger cleanup by Thread R. |
|-------------------------|-----------------------------------------------------------------------------------------------|
| print_stats() | SIGINT handler. Prints total_transmissions and exits. Triggered by Ctrl+C. |
|-------------------------|-----------------------------------------------------------------------------------------------|
| main() | Creates shared memory (SHM_KEY=1234) and semaphore (SEM_KEY=5678). Initializes all SM slots.|
| | Registers SIGINT handler. Spawns Thread R, Thread S, and GC child process. |
|-------------------------|-----------------------------------------------------------------------------------------------|