-
Notifications
You must be signed in to change notification settings - Fork 9
/
encoding.c
517 lines (428 loc) · 19.6 KB
/
encoding.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* Copyright 2013 Gregoire Delannoy
*
* This file is a part of TCPeP.
*
* TCPeP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "encoding.h"
void onWindowUpdate(encoderstate* state);
struct timeval sentAtTime(packetsentinfo* table, int nPacketSent, uint32_t seqNo);
uint16_t sentBlock(packetsentinfo* table, int nPacketSent, uint32_t seqNo);
void addToPacketSentInfos(packetsentinfo** table, int* nPacketSent, uint32_t seqNo, uint16_t blockNo, struct timeval sentAtTime);
void removeFromPacketSentInfos(packetsentinfo** table, int* nPacketSent, uint32_t seqNo);
block blockCreate();
void blockFree(block b);
void sendFromBlock(encoderstate* state, int blockNo);
void generateEncodedPayload(matrix data, int nPackets, uint32_t seed, uint8_t* buffer, int* bufLen);
int isMoreDataOk(encoderstate state){
return (state.numBlock < MAX_BLOCKS);
}
void onWindowUpdate(encoderstate* state){
//printf("in onWindowUpdate\n");
if(!state->isOutstandingData){
printf("No outstanding data. Return\n");
return;
}
int sentInThisRound = true, totalInFlight = 0, i;
int* nPacketsInFlight = calloc(state->numBlock, sizeof(int));
struct timeval timeOfArrival, currentTime;
uint16_t sentBlockNumber;
// ~~ Count packets in flight (total, and for each block) ~~
gettimeofday(¤tTime, NULL);
//do_debug("Current Time : (%d,%d)\n", (int)currentTime.tv_sec, (int)currentTime.tv_usec);
for(i = state->seqNo_Una; i < state->seqNo_Next; i++){
timeOfArrival = sentAtTime(*(state->packetSentInfos), *(state->nPacketSent), i);
if(timeOfArrival.tv_sec == 0){
continue;
}
if(state->shortTermRttAverage != 0){
addUSec(&timeOfArrival, (state->shortTermRttAverage * INFLIGHT_FACTOR) + COMPUTING_DELAY);
} else {
addUSec(&timeOfArrival, 10000000); // Add 10 seconds, if RTT = 0
}
if(isSooner(currentTime, timeOfArrival)){ // All packets
//printf("Packet #%d might still be in flight\n", i);
sentBlockNumber = sentBlock(*(state->packetSentInfos), *(state->nPacketSent), i);
if(sentBlockNumber - state->currBlock >= 0){
nPacketsInFlight[sentBlockNumber - state->currBlock] ++;
}
totalInFlight++;
}
}
do_debug("%d packets in flight, state->nDataToSend = %d\n", totalInFlight, state->nDataToSend);
totalInFlight = max(totalInFlight, state->nDataToSend); // Ensure that you don't send more than congestionWindow on a single round, even with really low RTT.
// ~~ If we're allowed to send, find a block that would be worth it ~~
//printf("\nBefore while statement, totalInFlight = %d, congWin = %f\n", totalInFlight, state->congestionWindow);
while((totalInFlight < ((int)state->congestionWindow)) && (sentInThisRound)){
sentInThisRound = false;
for(i = 0; i < state->numBlock; i++){
//printf("Block %d should receive ~%f packets while %d are known and %u dofs have been ack-ed\n", i, (1 - state->p) * nPacketsInFlight[i], state->blocks[i].nPackets, state->blocks[i].dofs);
if((ceilf(((1 - state->p) * nPacketsInFlight[i])) < (state->blocks[i].nPackets - state->blocks[i].dofs))){
//printf("Sending from block #%d\n", i);
sendFromBlock(state, i);
totalInFlight ++;
nPacketsInFlight[i]++;
sentInThisRound = true;
break;
}
}
if(totalInFlight == 0){
//printf("TotalInFlight = 0 while win = %f => All available data has been transfered to the other side\n", state->congestionWindow);
state->isOutstandingData = false;
}
}
//printf("After : totalInFlight = %d\n", totalInFlight);
free(nPacketsInFlight);
}
void handleInClear(encoderstate* state, uint8_t* buffer, int size){
do_debug("in handleInClear\n");
// We just have to put data in the next available packet
int sizeAllocated = 0, i;
uint16_t currentWriteSize, tmp16;
while(sizeAllocated < size){
for(i = 0; (i < state->numBlock) && (sizeAllocated < size); i++){ // Look in already allocated blocks
if(state->blocks[i].nPackets < BLKSIZE){
currentWriteSize = min(PACKETSIZE - 2, size - sizeAllocated);
tmp16 = htons(currentWriteSize); // Write the size on a uint16.
memcpy(state->blocks[i].dataMatrix->data[state->blocks[i].nPackets], &tmp16, 2);
memcpy(state->blocks[i].dataMatrix->data[state->blocks[i].nPackets] + 2, buffer + sizeAllocated, currentWriteSize);
sizeAllocated += currentWriteSize;
state->blocks[i].nPackets ++;
do_debug("Appended %d bytes in the block #%d which now contains %d packets\n", currentWriteSize, i, state->blocks[i].nPackets);
i = -1;
}
}
if(sizeAllocated < size){ // Not all data fit in the already allocated blocks => create one
state->blocks = realloc(state->blocks, (state->numBlock + 1) * sizeof(block));
state->blocks[state->numBlock] = blockCreate();
state->numBlock++;
}
}
state->isOutstandingData = true;
onWindowUpdate(state);
}
void onTimeOut(encoderstate* state){
printf("in onTimeOut\n");
state->slowStartMode = true;
state->congestionWindow = BASE_WINDOW;
state->timeOutCounter++;
// ~~ Set time for the next timeOut event ~~
if(state->isOutstandingData){
gettimeofday(&(state->nextTimeout), NULL);
addUSec(&(state->nextTimeout), (state->timeOutCounter * (COMPUTING_DELAY + (TIMEOUT_FACTOR * state->longTermRttAverage))));
} else { // No data left to send... let the TO be ~long !
state->nextTimeout.tv_sec = 0;
state->nextTimeout.tv_usec = 0;
addUSec(&(state->nextTimeout), (1 + state->timeOutCounter) * TIMEOUT_INCREMENT);
}
onWindowUpdate(state);
}
void onAck(encoderstate* state, uint8_t* buffer, int size){
do_debug("in onAck :\n");
ackpacket* ack = bufferToAck(buffer, size);
int i, currentRTT;
float delta;
//printf("ACK received :\n");
//ackPacketPrint(*ack);
// Arbitrary idea : if ACK < seqNo_una (= sequence already ACKed somehow) => Do not consider it
if(ack->ack_seqNo < state->seqNo_Una){
do_debug("Outdated ACK (n = %d while una = %d), Drop !\n", ack->ack_seqNo, state->seqNo_Una);
free(ack->ack_dofs);
free(ack);
return;
}
// ~~ Estimate network parameters ~~
gettimeofday(&(state->time_lastAck), NULL);
struct timeval sentAt = sentAtTime(*(state->packetSentInfos), *(state->nPacketSent), ack->ack_seqNo);
if(sentAt.tv_sec == 0){
// The specified sequence number is unknown... better ignore this ACK !
do_debug("Unknown/outdated sequence number, do not refresh parameters !\n");
state->seqNo_Una = max(state->seqNo_Una, ack->ack_seqNo + 1);
free(ack->ack_dofs);
free(ack);
return;
}
currentRTT = 1000000 * (state->time_lastAck.tv_sec - sentAt.tv_sec) + (state->time_lastAck.tv_usec - sentAt.tv_usec);
//printf("RTT for current ACK = %d\n", currentRTT);
// Actualize the RTT average
if(state->longTermRttAverage != 0){
state->longTermRttAverage = ((1 - SMOOTHING_FACTOR_LONG) * state->longTermRttAverage) + (SMOOTHING_FACTOR_LONG * currentRTT);
state->shortTermRttAverage = ((1 - SMOOTHING_FACTOR_SHORT) * state->shortTermRttAverage) + (SMOOTHING_FACTOR_SHORT * currentRTT);
} else { // We are initializing it
state->longTermRttAverage = currentRTT;
state->shortTermRttAverage = currentRTT;
}
//printf("Short-term RTT : %f, long-term :%f\n", state->shortTermRttAverage, state->longTermRttAverage);
// Actualize the packet loss ratio
state->p = 1.0 * ack->ack_loss / ack->ack_total;
// ~~ Adjust current block ~~
while(ack->ack_currBlock > state->currBlock){
// Free acknowldeged blocks (and forget about packets sent for them !)
blockFree(state->blocks[0]);
for(i = 0; i < state->numBlock - 1; i++){
(state->blocks)[i] = (state->blocks)[i+1];
}
state->blocks = realloc(state->blocks, (state->numBlock - 1) * sizeof(block));
state->numBlock --;
for(i = 0; i < *(state->nPacketSent); i++){
uint32_t seqNo = (*(state->packetSentInfos))[i].seqNo;
if(sentBlock(*(state->packetSentInfos), *(state->nPacketSent), seqNo) == state->currBlock){
removeFromPacketSentInfos(state->packetSentInfos, state->nPacketSent, seqNo);
i--;
}
}
state->currBlock++;
}
for(i = 0; i < DOFS_LENGTH; i++){
if(state->numBlock > i){
state->blocks[i].dofs = max(state->blocks[i].dofs, ack->ack_dofs[i]);
}
}
// ~~ Update Congestion window ~~
do_debug("Congestion Window before actualizing = %f\n", state->congestionWindow);
if(state->slowStartMode){
state->congestionWindow += 1;
if(state->congestionWindow > SS_THRESHOLD){
state->slowStartMode = false;
}
} else {// Congestion avoidance mode
delta = 1 - (state->longTermRttAverage / state->shortTermRttAverage);
if(delta < ALPHA){
// Increase the window :
state->congestionWindow += (INCREMENT / state->congestionWindow);
} else if(delta > BETA) {
// Decrease the window
state->congestionWindow -= (INCREMENT / state->congestionWindow);
}
// If delta is in between, do not update the window
// Avoid floating-point errors ; the window should never get lower than the BASE !
if(state->congestionWindow < BASE_WINDOW){
state->congestionWindow = BASE_WINDOW;
}
}
do_debug("Congestion Window after actualizing = %f\n", state->congestionWindow);
state->seqNo_Una = max(state->seqNo_Una, ack->ack_seqNo + 1);
free(ack->ack_dofs);
free(ack);
if(state->congestionWindow > MAX_WINDOW){
printf("Window reached maximum... DIE !\n");
exit(1);
}
onWindowUpdate(state);
// ~~ Set time for the next timeOut event ~~
if(state->isOutstandingData){
state->nextTimeout.tv_sec = state->time_lastAck.tv_sec;
state->nextTimeout.tv_usec = state->time_lastAck.tv_usec;
addUSec(&(state->nextTimeout), TIMEOUT_FACTOR * state->shortTermRttAverage);
} else { // No data left to send... let the TO be infinite !
state->nextTimeout.tv_sec = 0;
state->nextTimeout.tv_usec = 0;
}
state->timeOutCounter = 0;
}
encoderstate* encoderStateInit(){
encoderstate* ret = malloc(sizeof(encoderstate));
ret->blocks = 0;
ret->numBlock = 0;
ret->packetSentInfos = malloc(sizeof(packetsentinfo*));
*(ret->packetSentInfos) = 0;
ret->nPacketSent = malloc(sizeof(int));
*(ret->nPacketSent) = 0;
ret->p = 0.0;
ret->longTermRttAverage = 0;
ret->shortTermRttAverage = 0;
ret->seqNo_Next = 0;
ret->seqNo_Una = 0;
ret->congestionWindow = BASE_WINDOW;
ret->currBlock = 0;
ret->slowStartMode = true;
ret->dataToSend = 0;
ret->dataToSendSize = 0;
ret->nDataToSend = 0;
ret->time_lastAck.tv_sec = 0;
ret->time_lastAck.tv_usec = 0;
ret->nextTimeout.tv_sec = 0;
ret->nextTimeout.tv_usec = 0;
ret->isOutstandingData = false;
ret->timeOutCounter = 0;
return ret;
}
void encoderStateFree(encoderstate* state){
int i;
for(i = 0; i < state->numBlock; i++){
mFree(state->blocks[i].dataMatrix);
}
if(state->numBlock > 0){
free(state->blocks);
}
if(*(state->nPacketSent) > 0){
free(*(state->packetSentInfos));
}
free(state->packetSentInfos);
free(state->nPacketSent);
for(i = 0; i < state->nDataToSend; i++){
free(state->dataToSend[i]);
}
if(state->nDataToSend > 0){
free(state->dataToSend);
free(state->dataToSendSize);
}
free(state);
}
struct timeval sentAtTime(packetsentinfo* table, int nPacketSent, uint32_t seqNo){
int i;
for(i = 0; i<nPacketSent; i++){
if(table[i].seqNo == seqNo){
return table[i].sentAt;
}
}
do_debug("sentAtTime queried for unknown seqNo : %u. *should not happen*\n", seqNo);
struct timeval ret; // Note : this line will never be reached... Just for gcc not to complain
ret.tv_sec = 0;
ret.tv_usec = 0;
return ret;
}
uint16_t sentBlock(packetsentinfo* table, int nPacketSent, uint32_t seqNo){
int i;
for(i = 0; i<nPacketSent; i++){
if(table[i].seqNo == seqNo){
return table[i].blockNo;
}
}
do_debug("sentBlock queried for unexisting seqNo : %u. *should not happen*\n", seqNo);
return 0;
}
void addToPacketSentInfos(packetsentinfo** table, int* nPacketSent, uint32_t seqNo, uint16_t blockNo, struct timeval sentAtTime){
*table = realloc(*table, (*nPacketSent + 1) * sizeof(packetsentinfo));
(*table)[*nPacketSent].seqNo = seqNo;
(*table)[*nPacketSent].blockNo = blockNo;
(*table)[*nPacketSent].sentAt.tv_sec = sentAtTime.tv_sec;
(*table)[*nPacketSent].sentAt.tv_usec = sentAtTime.tv_usec;
//printf("Added packet #%u fro block %d to the sent table\n", seqNo, blockNo);
(*nPacketSent) ++;
}
void removeFromPacketSentInfos(packetsentinfo** table, int* nPacketSent, uint32_t seqNo){
int indexOccurence, i;
for(i = 0; i < *nPacketSent; i++){
if((*table)[i].seqNo == seqNo){
for(indexOccurence=i; indexOccurence < (*nPacketSent - 1); indexOccurence++){
(*table)[indexOccurence].seqNo = (*table)[indexOccurence + 1].seqNo;
(*table)[indexOccurence].blockNo = (*table)[indexOccurence + 1].blockNo;
(*table)[indexOccurence].sentAt = (*table)[indexOccurence + 1].sentAt;
}
*table = realloc(*table, (*nPacketSent -1) * sizeof(packetsentinfo));
(*nPacketSent) --;
return;
}
}
do_debug("removeFromPacketSent queried for unexisting seqNo. DIE.");
exit(1);
}
block blockCreate(){
block b;
int i;
b.dataMatrix = mCreate(BLKSIZE, PACKETSIZE);
b.nPackets = 0;
for(i = 0; i<BLKSIZE; i++){
b.isSentPacket[i] = false;
}
b.dofs = 0;
return b;
}
void blockFree(block b){
mFree(b.dataMatrix);
}
void sendFromBlock(encoderstate* state, int blockNo){
do_debug("in sendFromBlock\n");
int i, bufLen;
datapacket packet;
uint16_t tmp16;
uint8_t buffer[PACKETSIZE + 100];
struct timeval currentTime;
gettimeofday(¤tTime, NULL);
// Actualize sent at & sent from block tables
addToPacketSentInfos(state->packetSentInfos, state->nPacketSent, state->seqNo_Next, blockNo + state->currBlock, currentTime);
// First, look for an unsent packet
for(i = 0; i < state->blocks[blockNo].nPackets; i++){
if( !(state->blocks[blockNo].isSentPacket[i])){
// Generate the packet
packet.blockNo = blockNo + state->currBlock;
packet.packetNumber = (BITMASK_NO & i) | FLAG_CLEAR;
packet.seqNo = state->seqNo_Next;
memcpy(&tmp16, state->blocks[blockNo].dataMatrix->data[i], 2);
packet.size = ntohs(tmp16) + 2;
packet.payloadAndSize = malloc(packet.size * sizeof(uint8_t));
memcpy(packet.payloadAndSize, state->blocks[blockNo].dataMatrix->data[i], packet.size);
//printf("Data to send :\n");
//dataPacketPrint(packet);
// Marshall it
dataPacketToBuffer(packet, buffer, &bufLen);
// Append it to the data to send buffer
state->dataToSend = realloc(state->dataToSend, (state->nDataToSend + 1) * sizeof(uint8_t*));
state->dataToSendSize = realloc(state->dataToSendSize, (state->nDataToSend + 1) * sizeof(int));
state->dataToSend[state->nDataToSend] = malloc(bufLen * sizeof(uint8_t));
memcpy(state->dataToSend[state->nDataToSend], buffer, bufLen);
state->dataToSendSize[state->nDataToSend] = bufLen;
state->nDataToSend ++;
state->blocks[blockNo].isSentPacket[i] = true;
state->seqNo_Next ++;
free(packet.payloadAndSize);
return;
}
}
// If not found, send an encoded packet, comprising every packet know in the block
packet.blockNo = blockNo + state->currBlock;
packet.packetNumber = (BITMASK_NO & (state->blocks[blockNo].nPackets)) | FLAG_CODED;
packet.seqNo = state->seqNo_Next;
generateEncodedPayload(*(state->blocks[blockNo].dataMatrix), state->blocks[blockNo].nPackets, state->seqNo_Next, buffer, &bufLen);
packet.size = bufLen;
packet.payloadAndSize = malloc(bufLen);
memcpy(packet.payloadAndSize, buffer, bufLen);
//printf("Data to send :\n");
//dataPacketPrint(packet);
// Marshall it
dataPacketToBuffer(packet, buffer, &bufLen);
// Append it to the data to send buffer
state->dataToSend = realloc(state->dataToSend, (state->nDataToSend + 1) * sizeof(uint8_t*));
state->dataToSendSize = realloc(state->dataToSendSize, (state->nDataToSend + 1) * sizeof(int));
state->dataToSend[state->nDataToSend] = malloc(bufLen * sizeof(uint8_t));
memcpy(state->dataToSend[state->nDataToSend], buffer, bufLen);
state->dataToSendSize[state->nDataToSend] = bufLen;
state->nDataToSend ++;
state->seqNo_Next ++;
free(packet.payloadAndSize);
}
/* Using nPackets from data, generate the coefficients and write the encoded information in buffer */
void generateEncodedPayload(matrix data, int nPackets, uint32_t seed, uint8_t* buffer, int* bufLen){
srandom(seed); // Initialize the PRNG with seed value
matrix* coeffs = getRandomMatrix(1, nPackets); // Generate the coefficients
int origDataNRows = data.nRows; // Save the original state from data
data.nRows = nPackets; // Alter it artificially for the multiplication to be ok
matrix* result = mMul(*coeffs, data); // Perform the multiplication
// Copy the result to the buffer
memcpy(buffer, result->data[0], result->nColumns);
*bufLen = result->nColumns;
data.nRows = origDataNRows; // Restore data state
// Free the newly allocated matrices
mFree(coeffs);
mFree(result);
}
void encoderStatePrint(encoderstate state){
printf("Encoder state : \n");
printf("\tCurrent block = %u\n", state.currBlock);
printf("\tNumber of blocks = %d\n", state.numBlock);
printf("\tEncoded data to send = %d\n", state.nDataToSend);
printf("\tCongestion window = %f\n", state.congestionWindow);
printf("\tlong-term RTT = %f\n", state.longTermRttAverage);
printf("\tshort-term RTT = %f\n", state.shortTermRttAverage);
printf("\tLoss estimation = %f\n", state.p);
}