forked from dileepramesh/Reliable-UDP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrudp.c
executable file
·1777 lines (1514 loc) · 52.9 KB
/
rudp.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* rudp.c
*
* This implements a reliability layer on top of UDP. Both the server and
* client programs use this protocol layer to achieve file transfers reliably.
*
* October 2012
*/
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <pthread.h>
#include <math.h>
#include <assert.h>
#include <setjmp.h>
#include <sys/time.h>
#include "rudp.h"
/* Globals */
rudp_srv_state_t *srv_state;
rudp_cli_state_t *cli_state;
static struct rtt_info rttinfo;
static int rttinit = 0;
static struct msghdr msgsend, msgrecv;
static sigjmp_buf jmpbuf;
/* Header prepended to each UDP datagram - For reliability */
static struct hdr {
uint32_t msg_type;
uint32_t seq;
uint32_t ts;
uint32_t window_size;
} sendhdr, recvhdr;
/* Defines */
#define FILE_PAYLOAD_SIZE (RUDP_PAYLOAD_SIZE - sizeof(struct hdr))
#define GET_MIN(A, B) ((A) < (B) ? (A) : (B))
/****************************************************************************/
/* Common routines related to both Server and Client */
/****************************************************************************/
/*
* sigalarm_handler
*
* Handler for SIGALRM. We just set the jmpbuf here inorder to avoid race
* conditions.
*/
static void
sigalarm_handler (int signo)
{
siglongjmp(jmpbuf, 1);
}
/*
* rudp_start_timer
*
* Start a timer with the specified interval
*/
static void
rudp_start_timer (uint32_t ivl)
{
struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = (ivl / USEC_IN_SEC);
timer.it_value.tv_usec = (ivl % USEC_IN_SEC);
setitimer(ITIMER_REAL, &timer, 0);
}
/*
* rudp_stop_timer
*
* Stop a currently running timer
*/
static void
rudp_stop_timer (void)
{
struct itimerval timer;
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = 0;
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 0;
setitimer(ITIMER_REAL, &timer, 0);
}
/****************************************************************************/
/* Server related routines */
/****************************************************************************/
/*
* rudp_srv_init
*
* Initializes the RUDP layer for the server
*/
int
rudp_srv_init (rudp_srv_state_t *state)
{
int i;
/* Initialize the parameters based on what is sent by the server */
srv_state = (rudp_srv_state_t *)malloc(sizeof(rudp_srv_state_t));
if (!srv_state) {
return -1;
}
bzero(srv_state, sizeof(rudp_srv_state_t));
/* Initialize the state parameters */
srv_state->max_cwnd_size = state->max_cwnd_size;
srv_state->cwnd_size = 1; /* CW starts with size 1 in slow start phase */
srv_state->cwnd_free = srv_state->cwnd_size;
srv_state->ss_thresh = RUDP_DEFAULT_SSTHRESH;
srv_state->cwnd_start = 0;
srv_state->cwnd_end = 0;
srv_state->expected_ack = 1; /* First packet sent has sequence number 0 */
srv_state->num_acks = 0;
srv_state->num_dup_acks = 0;
srv_state->last_dup_ack = 0;
srv_state->rudp_state = CONGESTION_STATE_SLOW_START;
/* Initialize the congestion window */
srv_state->cwnd =
(rudp_payload_t *)malloc(srv_state->max_cwnd_size *
sizeof(rudp_payload_t));
if (!srv_state->cwnd) {
return -1;
}
bzero(srv_state->cwnd, srv_state->max_cwnd_size * sizeof(rudp_payload_t));
for (i = 0; i < srv_state->max_cwnd_size; i++) {
srv_state->cwnd[i].data = (char *)malloc(RUDP_PAYLOAD_SIZE);
if (!srv_state->cwnd[i].data) {
return -1;
}
bzero(srv_state->cwnd[i].data, RUDP_PAYLOAD_SIZE);
}
/* Register for SIGALRM to handle timeouts */
signal(SIGALRM, sigalarm_handler);
return 0;
}
/*
* rudp_srv_destroy
*
* Cleans up the resources allocated to the RUDP layer on the server
*/
int
rudp_srv_destroy (void)
{
int i;
/* Free the allocated resources */
if (srv_state) {
if (srv_state->cwnd) {
for (i = 0; i < srv_state->max_cwnd_size; i++) {
if (srv_state->cwnd[i].data) {
free(srv_state->cwnd[i].data);
}
}
}
free(srv_state->cwnd);
free(srv_state);
}
return 0;
}
/*
* rudp_srv_conn_send
*
* This is used by the server during the initial connection setup phase.
* The server uses this to send the ephimeral port to client and to receive
* the acknowledgement for that.
*/
int
rudp_srv_conn_send (int fd1, int fd2, struct sockaddr_in *peer_addr,
void *data, int size)
{
int n, timeout = 0, len = sizeof(struct sockaddr);
struct iovec iovsend[2], iovrecv[1];
/* Initialize the RTT library if we are coming here for the first time */
if (rttinit == 0) {
rtt_init(&rttinfo);
rttinit = 1;
rtt_d_flag = 1;
}
bzero(&msgsend, sizeof(struct msghdr));
bzero(&msgrecv, sizeof(struct msghdr));
/* Send the connection port to the client */
sendhdr.msg_type = htonl(MSG_TYPE_CONNECTION_PORT);
msgsend.msg_name = peer_addr;
msgsend.msg_namelen = len;
msgsend.msg_iov = iovsend;
msgsend.msg_iovlen = 2;
iovsend[0].iov_base = (void *)&sendhdr;
iovsend[0].iov_len = sizeof(struct hdr);
iovsend[1].iov_base = data;
iovsend[1].iov_len = size;
/* Initialize the receive parameters for getting the acknowledgement */
msgrecv.msg_name = peer_addr;
msgrecv.msg_namelen = len;
msgrecv.msg_iov = iovrecv;
msgrecv.msg_iovlen = 1;
iovrecv[0].iov_base = (void *)&recvhdr;
iovrecv[0].iov_len = sizeof(struct hdr);
/* Initialize the retransmission count to 0 */
rtt_newpack(&rttinfo);
printf("\nSending connection port to client\n");
/* Send it till we exceed the number of retries */
send_again:
/*
* Send it out on both connection and listening sockets in case of a
* timeout
*/
sendhdr.ts = htonl(rtt_ts(&rttinfo));
msgsend.msg_name = peer_addr;
msgsend.msg_namelen = len;
sendmsg(fd1, &msgsend, 0);
if (timeout == 1) {
/* Recepient should be NULL if socket is connected */
msgsend.msg_name = NULL;
msgsend.msg_namelen = 0;
sendmsg(fd2, &msgsend, 0);
}
/* Start the timer */
rudp_start_timer(rtt_start(&rttinfo));
if (sigsetjmp(jmpbuf, 1) != 0) {
if (rtt_timeout(&rttinfo) < 0) {
printf("rudp_serv_conn_send: no response from peer. giving up.\n");
rttinit = 0;
errno = ETIMEDOUT;
return -1;
}
printf("rudp_serv_conn_send: request timed out. retransmitting..\n");
timeout = 1;
goto send_again;
}
/* We will receive the acknowledgement on the connection socket */
do {
n = recvmsg(fd2, &msgrecv, 0);
} while (ntohl(recvhdr.msg_type) != MSG_TYPE_ACKNOWLEDGEMENT);
/* Store the window size advertised by the client */
srv_state->advw_size = ntohl(recvhdr.window_size);
srv_state->ss_thresh = srv_state->advw_size;
/* Stop the timer */
rudp_stop_timer();
/* Calculate & store new RTT estimator values */
rtt_stop(&rttinfo, rtt_ts(&rttinfo) - ntohl(recvhdr.ts));
/* Return the actual number of bytes read */
return (n - sizeof(struct hdr));
}
/*
* rudp_srv_conn_recv
*
* This is called during the initial connection setup phase. This is called by
* the server to receive the filename from the client.
*/
int
rudp_srv_conn_recv (int fd, void *buf, int size, struct sockaddr *src_addr,
int *src_len)
{
int bytes_read;
struct iovec iovrecv[2];
/* Initialize the RTT library if we are coming here for the first time */
if (rttinit == 0) {
rtt_init(&rttinfo);
rttinit = 1;
rtt_d_flag = 1;
}
bzero(&msgrecv, sizeof(struct msghdr));
/* Initialize the receive buffer */
msgrecv.msg_name = src_addr;
msgrecv.msg_namelen = *src_len;
msgrecv.msg_iov = iovrecv;
msgrecv.msg_iovlen = 2;
iovrecv[0].iov_base = (void *)&recvhdr;
iovrecv[0].iov_len = sizeof(struct hdr);
iovrecv[1].iov_base = buf;
iovrecv[1].iov_len = size;
/* Block till we get a response */
do {
bytes_read = recvmsg(fd, &msgrecv, 0);
} while (ntohl(recvhdr.msg_type) != MSG_TYPE_FILENAME);
/* Return the actual number of bytes read to the caller */
return (bytes_read - sizeof(struct hdr) - *src_len);
}
/*
* rudp_send_ctrl_packet
*
* This function is called when we want to send control messages to the
* receiver, like window probes, fin or some error.
*/
int
rudp_send_ctrl_packet (int fd, int msg_type)
{
struct iovec iovsend[1];
sendhdr.msg_type = htonl(msg_type);
msgsend.msg_name = NULL;
msgsend.msg_namelen = 0;
msgsend.msg_iov = iovsend;
msgsend.msg_iovlen = 1;
iovsend[0].iov_base = (void *)&sendhdr;
iovsend[0].iov_len = sizeof(struct hdr);
sendmsg(fd, &msgsend, 0);
return 0;
}
/*
* rudp_send_data_packet
*
* Form a packet from the given parameters and send it to the peer
*/
static int
rudp_send_data_packet (int fd, struct hdr *packet_hdr, char *buffer, int len,
int retransmit)
{
struct iovec iovsend[2];
int ret = 0;
if (retransmit == 0) {
printf("sending packet with seq %d\n", ntohl(packet_hdr->seq));
} else {
printf("resending packet with seq %d\n", ntohl(packet_hdr->seq));
}
bzero(&msgsend, sizeof(struct msghdr));
msgsend.msg_name = NULL;
msgsend.msg_namelen = 0;
msgsend.msg_iov = iovsend;
msgsend.msg_iovlen = 2;
iovsend[0].iov_base = (void *)packet_hdr;
iovsend[0].iov_len = sizeof(struct hdr);
iovsend[1].iov_base = (void *)buffer;
iovsend[1].iov_len = len - sizeof(struct hdr);
ret = sendmsg(fd, &msgsend, 0);
if (ret < 0 ) {
perror("rudp_send_data_packet: sendmsg failed : ");
return ret;
}
return len;
}
/*
* add_packet_to_cwnd
*
* Form the packet from the given parameters and add it to the congestion
* window
*/
static int
add_packet_to_cwnd (struct hdr *packet_hdr, char * buffer, int len)
{
char *ptr = NULL;
/* Sanity check */
assert(srv_state->cwnd[srv_state->cwnd_end].valid == 0);
/* New packet always goes to where cwnd_end is pointing */
srv_state->cwnd[srv_state->cwnd_end].valid = 1;
srv_state->cwnd[srv_state->cwnd_end].data_size = len;
ptr = (char*)srv_state->cwnd[srv_state->cwnd_end].data;
memcpy(ptr, packet_hdr, sizeof(struct hdr));
memcpy(ptr + sizeof(struct hdr), buffer, len - sizeof(struct hdr));
srv_state->cwnd_free--;
srv_state->cwnd_end = (srv_state->cwnd_end + 1) % srv_state->cwnd_size;
return 0;
}
/*
* find_packet_count
*
* Determine the number of packets that can be sent based on the congestion
* window and advertised window state.
*/
static inline int
find_packet_count (void)
{
int num = GET_MIN(srv_state->advw_size, srv_state->cwnd_free);
return num;
}
/*
* update_cwnd_after_timeout
*
* This is called when a sent packet is lost or the corresponding
* acknowledgement is lost. Here, we shrink the congestion window to the new
* size passed and update the window parameters accordingly.
*/
static void
update_cwnd_after_timeout (int new_cwnd_size, int new_ss_thresh,
int new_state, int *buffer_ptr,
int *bytes_remaining,
int *seq, int *pending_acks)
{
int i, idx, bytes, start, old_window_size, valid;
rudp_payload_t *new_cwnd;
/* Allocate a new window copy */
new_cwnd = (rudp_payload_t *)malloc(srv_state->max_cwnd_size *
sizeof(rudp_payload_t));
if (!new_cwnd) {
printf("update_cwnd_after_timeout: no memory\n");
assert(0);
}
bzero(new_cwnd, srv_state->max_cwnd_size * sizeof(rudp_payload_t));
for (i = 0; i < srv_state->max_cwnd_size; i++) {
new_cwnd[i].data = (char *)malloc(RUDP_PAYLOAD_SIZE);
if (!new_cwnd[i].data) {
printf("update_cwnd_after_timeout: no memory\n");
assert(0);
}
bzero(new_cwnd[i].data, RUDP_PAYLOAD_SIZE);
}
/* Copy relevant packets from the old copy */
idx = srv_state->cwnd_start;
valid = 0;
for (i = 0; i < new_cwnd_size; i++) {
if (srv_state->cwnd[idx].valid == 0) {
break;
}
new_cwnd[i].valid = srv_state->cwnd[idx].valid;
new_cwnd[i].data_size = srv_state->cwnd[idx].data_size;
memcpy(new_cwnd[i].data, srv_state->cwnd[idx].data,
RUDP_PAYLOAD_SIZE);
idx = (idx + 1) % srv_state->cwnd_size;
valid++;
}
start = srv_state->cwnd_start;
*seq = ntohl(((struct hdr *)(srv_state->cwnd[start].data))->seq) + valid;
srv_state->cwnd_free = new_cwnd_size - valid;
/*
* Compute how many data bytes we have to copy back from the buffer
* again and update the buffer pointer accordingly.
*/
bytes = 0;
while (srv_state->cwnd[idx].valid) {
bytes += (srv_state->cwnd[idx].data_size - sizeof(struct hdr));
idx = (idx + 1) % srv_state->cwnd_size;
if (idx == srv_state->cwnd_start) {
break;
}
}
*buffer_ptr -= bytes;
*bytes_remaining += bytes;
/* Free the old window and replace it with the new one */
for (i = 0; i < srv_state->max_cwnd_size; i++) {
if (srv_state->cwnd[i].data) {
free(srv_state->cwnd[i].data);
}
}
free(srv_state->cwnd);
srv_state->cwnd = new_cwnd;
/* Update the window parameters */
old_window_size = srv_state->cwnd_size;
srv_state->ss_thresh = new_ss_thresh;
srv_state->cwnd_size = new_cwnd_size;
srv_state->cwnd_start = 0;
srv_state->cwnd_end = 0;
srv_state->num_acks = 0;
srv_state->num_dup_acks = 0;
srv_state->rudp_state = new_state;
/*
* Update pending acks count. TODO see if we can update it intelligently
* instead of walking the whole window.
*/
idx = 0;
*pending_acks = 0;
for (i = 0; i < srv_state->cwnd_size; i++) {
if (srv_state->cwnd[i].valid == 0) {
idx = i;
break;
}
*pending_acks = *pending_acks + 1;
}
srv_state->cwnd_end = idx;
/* Debug */
if (new_state == CONGESTION_STATE_SLOW_START) {
printf("ENTERED SLOW START PHASE\n");
} else {
printf("ENTERED CONGESTION AVOIDANCE PHASE\n");
}
printf("shrinking congestion window from %d to %d. new ss_thresh: %d\n",
old_window_size, srv_state->cwnd_size, srv_state->ss_thresh);
}
/*
* update_cwnd_after_valid_ack
*
* This is called to update the congestion window parameters whenever we
* receive a valid ack.
*/
static void
update_cwnd_after_valid_ack (void)
{
int i, src_idx, dest_idx;
rudp_payload_t *new_cwnd;
/* Allocate a new copy of the congestion window */
new_cwnd = (rudp_payload_t *)malloc(srv_state->max_cwnd_size *
sizeof(rudp_payload_t));
if (!new_cwnd) {
printf("update_cwnd_after_valid_ack: no memory\n");
assert(0);
}
bzero(new_cwnd, srv_state->max_cwnd_size * sizeof(rudp_payload_t));
for (i = 0; i < srv_state->max_cwnd_size; i++) {
new_cwnd[i].data = (char *)malloc(RUDP_PAYLOAD_SIZE);
if (!new_cwnd[i].data) {
printf("update_cwnd_after_valid_ack: no memory\n");
assert(0);
}
new_cwnd[i].valid = 0;
bzero(new_cwnd[i].data, RUDP_PAYLOAD_SIZE);
}
/* Copy relevant packets from old to new copy */
src_idx = srv_state->cwnd_start;
dest_idx = 0;
while (srv_state->cwnd[src_idx].valid) {
new_cwnd[dest_idx].valid = srv_state->cwnd[src_idx].valid;
new_cwnd[dest_idx].data_size = srv_state->cwnd[src_idx].data_size;
memcpy(new_cwnd[dest_idx].data, srv_state->cwnd[src_idx].data,
RUDP_PAYLOAD_SIZE);
src_idx = (src_idx + 1) % srv_state->cwnd_size;
dest_idx = (dest_idx + 1) % srv_state->cwnd_size;
}
/* Free the old copy and replace it with the new one */
for (i = 0; i < srv_state->max_cwnd_size; i++) {
if (srv_state->cwnd[i].data) {
free(srv_state->cwnd[i].data);
}
}
free(srv_state->cwnd);
srv_state->cwnd = new_cwnd;
/* Update the free window slots and update the window parameters */
srv_state->cwnd_free = 0;
for (i = 0; i < srv_state->cwnd_size; i++) {
if (srv_state->cwnd[i].valid == 0) {
srv_state->cwnd_free++;
}
}
srv_state->cwnd_start = 0;
srv_state->cwnd_end = dest_idx;
}
/*
* update_cwnd_size_after_valid_ack
*
* This routine updates the size of the congestion window based on the number
* of acknowledgements received.
*/
static void
update_cwnd_size_after_valid_ack (uint32_t num_rcvd_acks)
{
if (srv_state->rudp_state == CONGESTION_STATE_SLOW_START) {
if (srv_state->cwnd_size < srv_state->max_cwnd_size) {
srv_state->cwnd_size += num_rcvd_acks;
if (srv_state->cwnd_size > srv_state->max_cwnd_size) {
srv_state->cwnd_size = srv_state->max_cwnd_size;
}
}
} else {
if (srv_state->num_acks >= srv_state->cwnd_size) {
if (srv_state->cwnd_size < srv_state->max_cwnd_size) {
srv_state->cwnd_size += num_rcvd_acks;
if (srv_state->cwnd_size > srv_state->max_cwnd_size) {
srv_state->cwnd_size = srv_state->max_cwnd_size;
}
}
srv_state->num_acks = 0;
}
}
if (srv_state->rudp_state == CONGESTION_STATE_SLOW_START &&
srv_state->cwnd_size > srv_state->ss_thresh) {
printf("ENTERED CONGESTION AVOIDANCE\n");
srv_state->rudp_state = CONGESTION_STATE_AVOIDANCE;
}
}
/*
* print_cwnd
*
* Print the congestion window
*/
static void
print_cwnd (void)
{
int i;
printf("start %d end %d size %d\n",
srv_state->cwnd_start, srv_state->cwnd_end, srv_state->cwnd_size);
for (i = 0; i < srv_state->cwnd_size; i++) {
printf("%d ", ntohl(((struct hdr *)(srv_state->cwnd[i].data))->seq));
}
printf("\n");
}
/*
* rudp_handle_ack
*
* This is called when we receive acks for multiple packets. This can happen
* during network congestion when packets arrive out of order. Depending on
* what state our congestion window is in and how many acks are received, we
* need to update our window state carefully.
*/
static void
rudp_handle_ack (uint32_t received_ack, uint32_t num_acks,
int *pending_acks)
{
int i, idx;
uint32_t start, end, free, size, exp_ack, start_seq, last_seq;
start = srv_state->cwnd_start;
end = srv_state->cwnd_end;
free = srv_state->cwnd_free;
size = srv_state->cwnd_size;
exp_ack = srv_state->expected_ack;
if (end == 0) {
end = size - 1;
} else {
end--;
}
last_seq = ntohl(((struct hdr *)(srv_state->cwnd[end].data))->seq);
start_seq = ntohl(((struct hdr *)(srv_state->cwnd[start].data))->seq);
/* Case 1: We have received a single ack */
if (num_acks == 1) {
srv_state->cwnd[srv_state->cwnd_start].valid = 0;
srv_state->cwnd_free++;
srv_state->num_acks++;
srv_state->cwnd_start =
(srv_state->cwnd_start + 1) % srv_state->cwnd_size;
*pending_acks = *pending_acks - 1;
srv_state->expected_ack += 1;
return;
}
/* Case 2: All the acked packets are in the congestion window */
if ((start_seq + num_acks) <= (last_seq + 1)) {
printf("cum_ack: case 1\n");
for (i = 0; i < num_acks; i++) {
srv_state->cwnd[srv_state->cwnd_start].valid = 0;
srv_state->cwnd_free++;
srv_state->num_acks++;
srv_state->cwnd_start =
(srv_state->cwnd_start + 1) % srv_state->cwnd_size;
*pending_acks = *pending_acks - 1;
srv_state->expected_ack += 1;
}
return;
}
/*
* Case 3: The packet corresponding to the received ack is not in the
* congestion window. This can happen during congestion when we have
* shrunk the congestion window. In this case, all the packets in our
* congestion window has been acked.
*/
if (received_ack >= (last_seq + 1)) {
printf("cum_ack: case 2\n");
idx = start;
while (1) {
if (srv_state->cwnd[idx].valid == 0) {
break;
}
srv_state->cwnd[idx].valid = 0;
srv_state->cwnd_free++;
srv_state->num_acks++;
*pending_acks = *pending_acks - 1;
srv_state->expected_ack += 1;
idx = (idx + 1) % srv_state->cwnd_size;
}
return;
}
}
/*
* retransmit_first_unacked_packet
*
* This routine retransmits the packet pointed to by cwnd_start. This is
* invoked in case of a timeout or fast retransmit.
*/
static void
retransmit_first_unacked_packet (int fd)
{
rudp_payload_t *payload;
payload = &srv_state->cwnd[srv_state->cwnd_start];
rudp_send_data_packet(fd, (struct hdr *)payload->data,
(void *)(payload->data + sizeof(struct hdr)),
payload->data_size, 1);
}
/*
* retransmit_unacked_packets
*
* This function takes the start and end indices of the congestion window and
* retransmits all the packets in that range. This is invoked when we are
* coming out of the persist mode.
*/
static void
retransmit_unacked_packets (int fd, uint32_t recv_win_size)
{
int i, idx;
uint32_t start, end, start_seq, end_seq, num_packets;
rudp_payload_t *payload;
start = srv_state->cwnd_start;
end = srv_state->cwnd_end; /* end points to the first free slot */
if (end == 0) {
end = srv_state->cwnd_size - 1;
} else {
end -= 1;
}
start_seq = ntohl(((struct hdr *)(srv_state->cwnd[start].data))->seq);
end_seq = ntohl(((struct hdr *)(srv_state->cwnd[end].data))->seq);
num_packets = end_seq - start_seq + 1;
num_packets = GET_MIN(num_packets, recv_win_size);
idx = srv_state->cwnd_start;
for (i = 0; i < num_packets; i++) {
assert(srv_state->cwnd[idx].valid == 1);
payload = &srv_state->cwnd[idx];
rudp_send_data_packet(fd, (struct hdr *)payload->data,
(void *)(payload->data + sizeof(struct hdr)),
payload->data_size, 1);
idx = (idx + 1) % srv_state->cwnd_size;
}
}
/*
* rudp_send
*
* This functions sends a chunk of data to the remote end reliably by
* implementing TCP like flow-control and congestion-control techniques. We
* return from this function only when we have successfully transmitted all
* the 'size' bytes of the passed buffer.
*/
int
rudp_send (int fd, void *filebuf, int size)
{
static int current_sequence_number;
int bytes_remaining = size;
struct hdr packet_hdr;
uint8_t timeout, retransmit, persist_mode, num_extra_free;
uint32_t num_packets = 0;
char buffer[FILE_PAYLOAD_SIZE];
struct iovec iovrecv[1];
int i, j, n, ret, len, offset = 0;
int num_slots, num_rcvd_acks, ss_thresh, idx, pending_acks = 0;
/*
* Keep sending in chunks of RUDP_PAYLOAD_SIZE bytes till we finish
* sending everything reliably.
*/
while (bytes_remaining || pending_acks) {
/* Reset state parameters */
timeout = 0;
retransmit = 0;
persist_mode = 0;
/* Figure out how many packets to send */
num_packets = find_packet_count();
/* Do we have enough data to send num_packets ? */
num_slots = (int)ceil((double)bytes_remaining / FILE_PAYLOAD_SIZE);
num_packets = GET_MIN(num_packets, num_slots);
printf("num_packets: %d cwnd_free %d slots %d\n",
num_packets, srv_state->cwnd_free, num_slots);
/* Initialize the retransmission count to 0 */
rtt_newpack(&rttinfo);
/* Send all the packets */
for (i = 0; i < num_packets; i++) {
/* Construct the header */
packet_hdr.msg_type = htonl(MSG_TYPE_FILE_DATA);
packet_hdr.ts = htonl(rtt_ts(&rttinfo));
packet_hdr.seq = htonl(current_sequence_number);
current_sequence_number++;
packet_hdr.window_size = htons(0);
/*
* Copy appropriate number of bytes from user buffer to send
* buffer
*/
len = (bytes_remaining >= FILE_PAYLOAD_SIZE) ? FILE_PAYLOAD_SIZE :
bytes_remaining;
memcpy(buffer, filebuf + offset, len);
offset += len;
bytes_remaining -= len;
pending_acks++;
/*
* Now add this packet to cwnd.
* Note : this function should not return error.
*/
ret = add_packet_to_cwnd(&packet_hdr, buffer,
len + sizeof(struct hdr));
assert(ret == 0);
/* Now send the packet */
rudp_send_data_packet(fd, &packet_hdr, buffer,
len + sizeof(struct hdr), 0);
}
/*
* If we don't have anything to send and we have pending
* acknowledgements, just retransmit the first unacknowledged packet
* instead of waiting for the timeout and shrinking the congestion
* window. Not doing this optimization for now.
*/
send_again:
/* Did we timeout */
if (timeout == 1) {
/* Are we in persist mode */
if (persist_mode) {
/* Send a probe message */
printf("rudp_send: sending window probe message\n");
rudp_send_ctrl_packet(fd, MSG_TYPE_WINDOW_PROBE);
} else {
/* Retransmit the last packet sent */
retransmit_first_unacked_packet(fd);
/*
* We have to reduce congestion window to ss_thresh. Compute
* the new ss_thresh. Also update the window parameters and
* buffer pointer.
*/
if (srv_state->cwnd_size > 1) {
ss_thresh = srv_state->cwnd_size / 2;
update_cwnd_after_timeout(1, ss_thresh,
CONGESTION_STATE_SLOW_START,
&offset, &bytes_remaining,
¤t_sequence_number,
&pending_acks);
}
//print_cwnd();
retransmit = 1;
}
timeout = 0;
}
/* Start the timer */
if (persist_mode) {
rudp_start_timer(RUDP_PERSIST_TIMER_INTERVAL * USEC_IN_SEC);
} else {
rudp_start_timer(rtt_start(&rttinfo));
}
if (sigsetjmp(jmpbuf, 1) != 0) {
if (rtt_timeout(&rttinfo) < 0) {
printf("rudp_send: no response from peer. giving up.\n");
rttinit = 0;
errno = ETIMEDOUT;
return -1;
}
printf("rudp_send: request timed out. retransmitting..\n");
timeout = 1;
goto send_again;
}
/* Initialize the receive parameters for getting the acknowledgement */
msgrecv.msg_name = NULL;
msgrecv.msg_namelen = 0;
msgrecv.msg_iov = iovrecv;
msgrecv.msg_iovlen = 1;
iovrecv[0].iov_base = (void *)&recvhdr;
iovrecv[0].iov_len = sizeof(struct hdr);
/* Block till we get a response */
n = recvmsg(fd, &msgrecv, 0);
/* Check for duplicate acknowledgements */
if (ntohl(recvhdr.seq) < srv_state->expected_ack) {
if (persist_mode == 0) {
srv_state->num_dup_acks++;
if (srv_state->num_dup_acks == 3) {
/* Stop the timer */
rudp_stop_timer();
srv_state->num_dup_acks = 0;
if (srv_state->last_dup_ack != ntohl(recvhdr.seq)) {
printf("rudp_send: received dup ack %d (%d) window %d. "
"retransmitting\n", ntohl(recvhdr.seq),
srv_state->expected_ack,
ntohl(recvhdr.window_size));
//print_cwnd();
/* Fast retransmit: Retransmit the last packet sent */
retransmit_first_unacked_packet(fd);
retransmit = 1;
srv_state->num_dup_acks = 0;
srv_state->last_dup_ack = ntohl(recvhdr.seq);
/*
* We have to reduce congestion window to ss_thresh.
* Compute the new ss_thresh. Also update the window
* parameters and buffer pointer.
*/
if (srv_state->cwnd_size > 1) {
ss_thresh = srv_state->cwnd_size / 2;
update_cwnd_after_timeout(ss_thresh, ss_thresh,
CONGESTION_STATE_AVOIDANCE,
&offset, &bytes_remaining,
¤t_sequence_number,
&pending_acks);
}
//print_cwnd();
retransmit = 1;
}
}
goto send_again;
} else {
/* We are in persist mode. Has the window opened up? */
if (ntohl(recvhdr.window_size) > 0) {
printf("rudp_send: coming out of persist mode. "
"new win: %d\n", ntohl(recvhdr.window_size));
srv_state->advw_size = ntohl(recvhdr.window_size);
persist_mode = 0;
if (pending_acks == 0) {
rudp_stop_timer();
rtt_stop(&rttinfo,
rtt_ts(&rttinfo) - ntohl(recvhdr.ts));
continue;
} else {
retransmit_unacked_packets(fd,