-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_frcc.c
More file actions
1280 lines (1139 loc) · 43.9 KB
/
tcp_frcc.c
File metadata and controls
1280 lines (1139 loc) · 43.9 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
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
// FRCC: A provably fair and robust congestion controller
#include <net/tcp.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/version.h>
#include <linux/random.h>
#define FRCC_LOG_INFO
// #define FRCC_LOG_DEBUG
// #define FRCC_LOG_TRACE
#define P_SCALE 8 /* scaling factor for fractions (e.g. gains) */
#define P_UNIT (1 << P_SCALE)
// enum log_level {
// LOG_INFO,
// LOG_DEBUG,
// LOG_TRACE,
// };
// static enum log_level static_log_level = LOG_INFO;
// Assumptions about network scenarios
static u32 static_p_ub_rtprop_us = 100000; // 100 ms
static u32 static_p_ub_rtterr_us = 10000; // 10 ms
static u32 static_p_ub_flow_count = 3;
// Design parameters
// TODO: check if these floating values make sense given the UNIT. Should we
// change the unit?
static u32 static_p_lb_cwnd_pkts = 4;
// ^^ this should be such that, p_cwnd_clamp_hi increases this by at least 1,
// otherwise even at the maximum increase, the cwnd will not increase due to
// integer arithmetic.
static u32 static_p_contract_min_qdel_us = 10000; // static_p_ub_rtprop_us / 2;
// for stability, static_p_contract_min_qdel_us >= rtprop / ground_truth_flow_count,
// for error, we need static_p_contract_min_qdel_us >= 2 * static_p_ub_rtterr_us
static u32 static_p_probe_duration_us = 10000; // 10 ms. How should this be set?
static u32 static_p_probe_multiplier_unit = P_UNIT * 4; // gamma in the paper
static u32 static_p_cwnd_averaging_factor_unit =
P_UNIT * 1; // alpha = 1/2 for non-stable design, otherwise 1.
static u32 static_p_cwnd_clamp_hi_unit = P_UNIT * 13 / 10;
static u32 static_p_cwnd_clamp_lo_unit = P_UNIT * 10 / 13;
static u32 static_p_slot_load_factor_unit = P_UNIT * 2;
static u32 static_p_ub_slots_per_round = 20;
static u32 static_p_rprobe_interval_us = 30000000; // 30 seconds
static u32 static_p_probe_wait_rtts = 2; // number of rtts to wait after probe
// Design features
static bool static_f_use_rtprop_probe = true;
static bool static_f_wait_rtt_after_probe = true;
static bool static_f_use_stable_cwnd_update = true;
static bool static_f_probe_wait_in_max_rtts = true;
static bool static_f_probe_duration_max_rtt = true;
static bool static_f_drain_over_rtt = true;
static bool static_f_probe_over_rtt = true;
static bool static_f_slot_greater_than_rtprop = true;
static bool static_f_slot_exactly_rtprop = true;
// Make all parameters runtime configurable
// https://devarea.com/linux-kernel-development-kernel-module-parameters/
module_param(static_p_ub_rtprop_us, uint, 0660);
module_param(static_p_ub_rtterr_us, uint, 0660);
module_param(static_p_ub_flow_count, uint, 0660);
module_param(static_p_lb_cwnd_pkts, uint, 0660);
module_param(static_p_contract_min_qdel_us, uint, 0660);
module_param(static_p_probe_duration_us, uint, 0660);
module_param(static_p_probe_multiplier_unit, uint, 0660);
module_param(static_p_cwnd_averaging_factor_unit, uint, 0660);
module_param(static_p_cwnd_clamp_hi_unit, uint, 0660);
module_param(static_p_cwnd_clamp_lo_unit, uint, 0660);
module_param(static_p_slot_load_factor_unit, uint, 0660);
module_param(static_p_ub_slots_per_round, uint, 0660);
module_param(static_p_rprobe_interval_us, uint, 0660);
module_param(static_p_probe_wait_rtts, uint, 0660);
module_param(static_f_use_rtprop_probe, bool, 0660);
module_param(static_f_wait_rtt_after_probe, bool, 0660);
module_param(static_f_use_stable_cwnd_update, bool, 0660);
module_param(static_f_probe_wait_in_max_rtts, bool, 0660);
module_param(static_f_probe_duration_max_rtt, bool, 0660);
module_param(static_f_drain_over_rtt, bool, 0660);
module_param(static_f_probe_over_rtt, bool, 0660);
module_param(static_f_slot_greater_than_rtprop, bool, 0660);
module_param(static_f_slot_exactly_rtprop, bool, 0660);
// module_param(static_log_level, uint, 0660);
static u32 id = 0;
struct param_data {
// Priors about network
u32 p_ub_rtprop_us;
u32 p_ub_rtterr_us;
u32 p_ub_flow_count;
// Design parameters
u32 p_lb_cwnd_pkts;
u32 p_contract_min_qdel_us;
u32 p_probe_duration_us;
u32 p_probe_multiplier_unit;
u32 p_cwnd_averaging_factor_unit;
u32 p_inv_cwnd_averaging_factor_unit;
u32 p_cwnd_clamp_hi_unit;
u32 p_cwnd_clamp_lo_unit;
u32 p_slot_load_factor_unit;
u32 p_ub_slots_per_round;
u32 p_rprobe_interval_us;
u32 p_probe_wait_rtts;
// Design features
bool f_use_rtprop_probe;
bool f_wait_rtt_after_probe;
bool f_use_stable_cwnd_update;
bool f_probe_wait_in_max_rtts;
bool f_probe_duration_max_rtt;
bool f_drain_over_rtt;
bool f_probe_over_rtt;
bool f_slot_greater_than_rtprop;
bool f_slot_exactly_rtprop;
};
struct probe_data {
bool s_probe_ongoing;
u32 s_probe_min_rtt_before_us;
u32 s_probe_min_rtt_us; // for logging only
u32 s_probe_min_excess_delay_us;
u32 s_probe_prev_cwnd_pkts;
u32 s_probe_excess_pkts;
bool s_probe_end_initiated;
u32 s_probe_drain_pkts_unit;
u32 s_probe_drain_pkts_rem_unit;
u64 s_probe_start_time_us;
u64 s_probe_start_seq;
u64 s_probe_inflightmatch_seq;
u64 s_probe_first_seq;
u64 s_probe_last_seq;
u64 s_probe_first_time_us;
};
struct rprobe_data {
bool s_rprobe_ongoing;
u64 s_rprobe_prev_start_time_us;
u64 s_rprobe_start_time_us;
u64 s_rprobe_init_end_time_us;
u32 s_rprobe_prev_cwnd_pkts;
bool s_rprobe_end_initiated;
};
struct round_data {
u32 s_round_slots_till_now;
u32 s_round_min_rtt_us;
u32 s_round_max_rate_pps;
u32 s_round_probe_slot_idx;
bool s_round_probed;
u32 s_round_slots_total;
// pps = packets per second, supports range: [1500 bytes per sec to
// 6.44 terabytes per second]
};
enum cwnd_event {
SLOW_START,
SLOW_START_END,
PROBE_GAIN,
PROBE_DRAIN,
PROBE_UPDATE,
RPROBE_DRAIN,
RPROBE_REFILL,
};
struct frcc_data {
struct param_data *p_params;
u32 id;
u64 last_log_time_us;
// TODO: need to make space in this struct
// enum log_level log_level;
// State variables
u32 s_min_rtprop_us;
bool s_ss_done;
bool s_ss_end_initiated;
u64 s_ss_last_seq;
u32 s_slot_max_qdel_us;
u64 s_slot_start_time_us;
u32 s_slot_max_rate_pps; // for logging only
u32 s_slot_min_rtt_us; // for logging only
u32 s_slot_max_rtt_us; // for logging only
struct probe_data *s_probe;
struct rprobe_data *s_rprobe;
struct round_data *s_round;
};
#define BASE_FMT \
"flow %u now %llu cwnd %u pacing %lu rtt %u mss %u min_rtprop_us %u " \
"inflight %u "
#define BASE_VARS \
frcc->id, now_us, tsk->snd_cwnd, sk->sk_pacing_rate, rtt_us, \
tsk->mss_cache, frcc->s_min_rtprop_us, tsk->packets_out
#define ROUND_FMT \
"round_min_rtt_us %u round_max_rate_pps %u " \
"round_slots_till_now %u round_probe_slot_idx %u round_slots_total %u "
#define ROUND_VARS \
frcc->s_round->s_round_min_rtt_us, \
frcc->s_round->s_round_max_rate_pps, \
frcc->s_round->s_round_slots_till_now, \
frcc->s_round->s_round_probe_slot_idx, \
frcc->s_round->s_round_slots_total
#define SLOT_FMT \
"slot_max_qdel_us %u slot_max_rate_pps %u " \
"slot_min_rtt_us %u slot_max_rtt_us %u probe_ongoing %u "
#define SLOT_VARS \
frcc->s_slot_max_qdel_us, frcc->s_slot_max_rate_pps, \
frcc->s_slot_min_rtt_us, frcc->s_slot_max_rtt_us, \
frcc->s_probe->s_probe_ongoing
#define PROBE_FMT \
"probe_ongoing %u probe_min_rtt_before_us %u probe_min_rtt_us %u " \
"probe_min_excess_delay_us %u " \
"probe_prev_cwnd_pkts %u probe_excess_pkts %u probe_end_initiated %u "
#define PROBE_VARS \
frcc->s_probe->s_probe_ongoing, \
frcc->s_probe->s_probe_min_rtt_before_us, \
frcc->s_probe->s_probe_min_rtt_us, \
frcc->s_probe->s_probe_min_excess_delay_us, \
frcc->s_probe->s_probe_prev_cwnd_pkts, \
frcc->s_probe->s_probe_excess_pkts, \
frcc->s_probe->s_probe_end_initiated
#define PROBE_DEBUG_FMT \
"probe_start_seq %llu probe_inflightmatch_seq %llu " \
"probe_first_seq %llu probe_last_seq %llu " \
"probe_first_seq_snd_time %llu part_of_probe %u "
#define PROBE_DEBUG_VARS \
frcc->s_probe->s_probe_start_seq, \
frcc->s_probe->s_probe_inflightmatch_seq, \
frcc->s_probe->s_probe_first_seq, \
frcc->s_probe->s_probe_last_seq, \
frcc->s_probe->s_probe_first_seq_snd_time, \
part_of_probe(frcc, tsk)
#define SEQ_FMT "last_snd_seq %llu last_rcv_seq %llu "
#define SEQ_VARS get_last_snd_seq(tsk), get_last_rcv_seq(tsk)
static u64 get_last_snd_seq(struct tcp_sock *tsk)
{
// return tsk->segs_out;
return tsk->snd_nxt;
}
static u64 get_last_rcv_seq(struct tcp_sock *tsk)
{
// return tsk->segs_in;
return tsk->snd_una;
}
// void my_log(struct frcc_data *frcc, struct tcp_sock *tsk, u32 rtt_us, u64 now_us,
// enum log_level level, const char *log_type, const char *fmt, ...)
// {
// #ifdef FRCC_LOG
// va_list args;
// if (level > static_log_level) { // frcc->log_level) {
// return;
// }
// printk(KERN_INFO
// "frcc %s flow %u now %llu cwnd %u rtt %u mss %u min_rtprop_us %u "
// "inflight %u last_snd_seq %llu last_rcv_seq %llu ",
// log_type, frcc->id, now_us, tsk->snd_cwnd, rtt_us, tsk->mss_cache,
// frcc->s_min_rtprop_us, tsk->packets_out, get_last_snd_seq(tsk),
// get_last_rcv_seq(tsk));
// va_start(args, fmt);
// vprintk(fmt, args);
// va_end(args);
// #endif
// }
void log_params(struct sock *sk, struct frcc_data *frcc, struct tcp_sock *tsk,
u64 now_us)
{
#ifdef FRCC_LOG_INFO
struct param_data *p = frcc->p_params;
u32 rtt_us = U32_MAX;
printk(KERN_INFO
"frcc params " BASE_FMT
"ub_rtprop_us %u ub_rtterr_us %u ub_flow_count %u "
"p_lb_cwnd_pkts %u p_contract_min_qdel_us %u p_probe_duration_us %u "
"p_probe_multiplier_unit %u p_cwnd_averaging_factor_unit %u "
"p_inv_cwnd_averaging_factor_unit %u p_cwnd_clamp_hi_unit %u "
"p_cwnd_clamp_lo_unit %u p_slot_load_factor_unit %u "
"p_ub_slots_per_round %u p_rprobe_interval_us %u "
"f_use_rtprop_probe %u f_wait_rtt_after_probe %u f_use_stable_cwnd_update %u ",
BASE_VARS, p->p_ub_rtprop_us, p->p_ub_rtterr_us,
p->p_ub_flow_count, p->p_lb_cwnd_pkts, p->p_contract_min_qdel_us,
p->p_probe_duration_us, p->p_probe_multiplier_unit,
p->p_cwnd_averaging_factor_unit,
p->p_inv_cwnd_averaging_factor_unit, p->p_cwnd_clamp_hi_unit,
p->p_cwnd_clamp_lo_unit, p->p_slot_load_factor_unit,
p->p_ub_slots_per_round, p->p_rprobe_interval_us,
p->f_use_rtprop_probe, p->f_wait_rtt_after_probe,
p->f_use_stable_cwnd_update);
#endif
}
static void init_params(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u64 now_us)
{
struct param_data *p = frcc->p_params;
p->p_ub_rtprop_us = static_p_ub_rtprop_us;
p->p_ub_rtterr_us = static_p_ub_rtterr_us;
p->p_ub_flow_count = static_p_ub_flow_count;
p->p_lb_cwnd_pkts = static_p_lb_cwnd_pkts;
p->p_contract_min_qdel_us = static_p_contract_min_qdel_us;
p->p_probe_duration_us = static_p_probe_duration_us;
p->p_probe_multiplier_unit = static_p_probe_multiplier_unit;
p->p_cwnd_averaging_factor_unit = static_p_cwnd_averaging_factor_unit;
p->p_inv_cwnd_averaging_factor_unit =
P_UNIT * 1 - p->p_cwnd_averaging_factor_unit;
p->p_cwnd_clamp_hi_unit = static_p_cwnd_clamp_hi_unit;
p->p_cwnd_clamp_lo_unit = static_p_cwnd_clamp_lo_unit;
p->p_slot_load_factor_unit = static_p_slot_load_factor_unit;
p->p_ub_slots_per_round = static_p_ub_slots_per_round;
p->p_rprobe_interval_us = static_p_rprobe_interval_us;
p->p_probe_wait_rtts = static_p_probe_wait_rtts;
p->f_use_rtprop_probe = static_f_use_rtprop_probe;
p->f_wait_rtt_after_probe = static_f_wait_rtt_after_probe;
p->f_use_stable_cwnd_update = static_f_use_stable_cwnd_update;
p->f_probe_wait_in_max_rtts = static_f_probe_wait_in_max_rtts;
p->f_probe_duration_max_rtt = static_f_probe_duration_max_rtt;
p->f_drain_over_rtt = static_f_drain_over_rtt;
p->f_probe_over_rtt = static_f_probe_over_rtt;
p->f_slot_greater_than_rtprop = static_f_slot_greater_than_rtprop;
p->f_slot_exactly_rtprop = static_f_slot_exactly_rtprop;
log_params(sk, frcc, tsk, now_us);
}
static u32 get_target_flow_count_unit(struct frcc_data *frcc)
{
struct param_data *p = frcc->p_params;
u32 round_qdel_us;
u32 target_flow_count_unit;
round_qdel_us =
frcc->s_round->s_round_min_rtt_us - frcc->s_min_rtprop_us;
target_flow_count_unit = P_UNIT;
target_flow_count_unit *= round_qdel_us;
do_div(target_flow_count_unit, p->p_contract_min_qdel_us);
return target_flow_count_unit;
}
static u32 get_slots_per_round(struct frcc_data *frcc)
{
struct param_data *p = frcc->p_params;
u32 p_lb_slots_per_round =
(p->p_ub_flow_count * p->p_slot_load_factor_unit) >> P_SCALE;
u32 target_flow_count_unit = get_target_flow_count_unit(frcc);
u32 p_slots_per_round =
target_flow_count_unit * p->p_slot_load_factor_unit;
// remove P_UNIT^2
p_slots_per_round = p_slots_per_round >> P_SCALE;
p_slots_per_round = p_slots_per_round >> P_SCALE;
p_slots_per_round = max_t(u32, p_slots_per_round, p_lb_slots_per_round);
p_slots_per_round =
min_t(u32, p_slots_per_round, p->p_ub_slots_per_round);
if (frcc->s_round->s_round_min_rtt_us == U32_MAX ||
frcc->s_min_rtprop_us == U32_MAX) {
return p_lb_slots_per_round;
}
return p_slots_per_round;
}
static void reset_round_state(struct frcc_data *frcc)
{
frcc->s_round->s_round_slots_total = get_slots_per_round(frcc);
// ^^ Note: we need to compute this before resetting round min rtt.
frcc->s_round->s_round_slots_till_now = 0;
frcc->s_round->s_round_min_rtt_us = U32_MAX;
frcc->s_round->s_round_max_rate_pps = 0;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 2, 0)
frcc->s_round->s_round_probe_slot_idx =
1 +
get_random_u32_below(frcc->s_round->s_round_slots_total - 1);
#else
frcc->s_round->s_round_probe_slot_idx =
1 + prandom_u32_max(frcc->s_round->s_round_slots_total - 1);
#endif
frcc->s_round->s_round_probed = false;
// Rationale for -1 in the input to prandom_u32_max: If there are 6
// slots: 0 to 5, we want the slot idx to be in range [1,5]. Note, slot
// 0 is not in the range because we do not probe in slot 0 to be able
// to obtain some information in the round.
}
static void reset_probe_state(struct frcc_data *frcc)
{
frcc->s_probe->s_probe_ongoing = false;
frcc->s_probe->s_probe_min_rtt_before_us = U32_MAX;
frcc->s_probe->s_probe_min_rtt_us = U32_MAX;
frcc->s_probe->s_probe_min_excess_delay_us = U32_MAX;
frcc->s_probe->s_probe_prev_cwnd_pkts = 0; // should not be read anyway.
frcc->s_probe->s_probe_excess_pkts = 0; // should not be read anyway.
frcc->s_probe->s_probe_end_initiated = false;
frcc->s_probe->s_probe_drain_pkts_unit = 0;
frcc->s_probe->s_probe_drain_pkts_rem_unit = 0;
frcc->s_probe->s_probe_start_time_us = 0;
frcc->s_probe->s_probe_start_seq = 0;
frcc->s_probe->s_probe_inflightmatch_seq = 0;
frcc->s_probe->s_probe_first_seq = 0;
frcc->s_probe->s_probe_last_seq = 0;
frcc->s_probe->s_probe_first_time_us = 0;
}
static void start_new_slot(struct frcc_data *frcc, u64 now_us)
{
frcc->s_slot_max_qdel_us = 0;
frcc->s_slot_start_time_us = now_us;
frcc->s_slot_max_rate_pps = 0;
frcc->s_slot_min_rtt_us = U32_MAX;
frcc->s_slot_max_rtt_us = 0;
}
static u64 get_rprobe_time(struct frcc_data *frcc, u64 time_us)
{
// Round down to the nearest multiple of rprobe_interval_us
struct param_data *p = frcc->p_params;
return (time_us / p->p_rprobe_interval_us) * p->p_rprobe_interval_us;
}
static void reset_rprobe_state(struct frcc_data *frcc,
u64 current_rprobe_start_time_us)
{
frcc->s_rprobe->s_rprobe_ongoing = false;
frcc->s_rprobe->s_rprobe_prev_start_time_us =
get_rprobe_time(frcc, current_rprobe_start_time_us);
frcc->s_rprobe->s_rprobe_start_time_us = 0;
frcc->s_rprobe->s_rprobe_init_end_time_us = 0;
frcc->s_rprobe->s_rprobe_end_initiated = false;
frcc->s_rprobe->s_rprobe_prev_cwnd_pkts = 0;
}
static void frcc_init(struct sock *sk)
{
struct frcc_data *frcc = inet_csk_ca(sk);
struct tcp_sock *tsk = tcp_sk(sk);
u64 now_us = tsk->tcp_mstamp;
frcc->s_probe = kzalloc(sizeof(struct probe_data), GFP_KERNEL);
frcc->s_rprobe = kzalloc(sizeof(struct rprobe_data), GFP_KERNEL);
frcc->s_round = kzalloc(sizeof(struct round_data), GFP_KERNEL);
frcc->p_params = kzalloc(sizeof(struct param_data), GFP_KERNEL);
// Convention, the order in struct is same as order of initialization
// so that we ensure everything is initialized and in the right order.
// param initialization should be first as these are used in other
// initializations, for others, the order does not matter much.
init_params(sk, frcc, tsk, now_us);
++id;
frcc->id = id;
frcc->last_log_time_us = 0;
// frcc->log_level = static_log_level;
// TODO: we should reset this at some time to accommodate path changes.
frcc->s_min_rtprop_us = U32_MAX;
frcc->s_ss_done = false;
frcc->s_ss_end_initiated = false;
frcc->s_ss_last_seq = 0;
reset_round_state(frcc);
start_new_slot(frcc, now_us);
reset_probe_state(frcc);
reset_rprobe_state(frcc, now_us);
cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
}
static u32 frcc_get_mss(struct tcp_sock *tsk)
{
// TODO: Figure out if mss_cache is the one to use
return tsk->mss_cache;
}
/* was the frcc struct fully inited */
static bool frcc_valid(struct frcc_data *frcc)
{
return (frcc);
}
static bool part_of_probe(struct frcc_data *frcc, struct tcp_sock *tsk)
{
// Because last_rcv_seq is the next seq we expect to receive, i.e.,
// unacked seq, the current convention here is that first seq - 1 is
// included in the acks part of probe and last - 1 is the last included
// sequence. We really do want last to be excluded as that is sent
// after the cwnd update.
u64 last_rcv_seq = get_last_rcv_seq(tsk);
if (frcc->s_probe->s_probe_first_seq > 0) {
if (frcc->s_probe->s_probe_last_seq > 0) {
return !before(last_rcv_seq,
frcc->s_probe->s_probe_first_seq) &&
!after(last_rcv_seq,
frcc->s_probe->s_probe_last_seq);
} else {
return !before(last_rcv_seq,
frcc->s_probe->s_probe_first_seq);
}
}
return false;
}
static u32 get_initial_rtt(struct tcp_sock *tsk)
{
// TODO: Should we be calling this every packet?
// Get initial RTT - as measured by SYN -> SYN-ACK. If information
// does not exist - use U32_MAX
u32 init_rtt_us;
if (tsk->srtt_us) {
init_rtt_us = max_t(u32, tsk->srtt_us >> 3, 1U);
} else {
init_rtt_us = U32_MAX;
}
return init_rtt_us;
}
static void update_pacing_rate(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us,
bool probe_gain)
{
u64 next_rate_bps =
2 * tsk->snd_cwnd * frcc_get_mss(tsk) * USEC_PER_SEC;
do_div(next_rate_bps, frcc->s_min_rtprop_us);
if (probe_gain) {
// Just after cwnd increase for probe, set pacing rate to new
// (probing) cwnd / old RTT, so that the new inflight builds up
// slowly over an RTT in an attempt to avoid self induced
// oscillations.
next_rate_bps =
tsk->snd_cwnd * frcc_get_mss(tsk) * USEC_PER_SEC;
do_div(next_rate_bps, rtt_us);
}
sk->sk_pacing_rate = next_rate_bps;
}
static void update_estimates(struct frcc_data *frcc, struct tcp_sock *tsk,
const struct rate_sample *rs, u32 rtt_us)
{
u32 this_qdel;
u32 init_rtt_us = get_initial_rtt(tsk);
// TODO: Should we use the rs->delivered instead of snd_cwnd?
u64 this_rate_pps = tsk->snd_cwnd * USEC_PER_SEC;
do_div(this_rate_pps, rtt_us);
frcc->s_min_rtprop_us = min_t(u32, frcc->s_min_rtprop_us, init_rtt_us);
frcc->s_min_rtprop_us = min_t(u32, frcc->s_min_rtprop_us, rtt_us);
frcc->s_round->s_round_min_rtt_us =
min_t(u32, frcc->s_round->s_round_min_rtt_us, rtt_us);
this_qdel = rtt_us - frcc->s_min_rtprop_us;
if (frcc->s_probe->s_probe_ongoing && part_of_probe(frcc, tsk)) {
u32 this_excess_delay_us =
rtt_us - frcc->s_probe->s_probe_min_rtt_before_us;
frcc->s_probe->s_probe_min_excess_delay_us =
min_t(u32, frcc->s_probe->s_probe_min_excess_delay_us,
this_excess_delay_us);
frcc->s_probe->s_probe_min_rtt_us =
min_t(u32, frcc->s_probe->s_probe_min_rtt_us, rtt_us);
} else if (!frcc->s_probe->s_probe_ongoing) {
frcc->s_round->s_round_max_rate_pps =
max_t(u64, frcc->s_round->s_round_max_rate_pps,
this_rate_pps);
}
frcc->s_slot_max_qdel_us =
max_t(u32, frcc->s_slot_max_qdel_us, this_qdel);
frcc->s_slot_max_rate_pps =
max_t(u64, frcc->s_slot_max_rate_pps, this_rate_pps);
frcc->s_slot_max_rtt_us = max_t(u32, frcc->s_slot_max_rtt_us, rtt_us);
frcc->s_slot_min_rtt_us = min_t(u32, frcc->s_slot_min_rtt_us, rtt_us);
}
static bool probe_ended(struct frcc_data *frcc, struct tcp_sock *tsk)
{
// part of probe is first -1 and last -1, so here, we are checking if
// una is >= last so that last -1 is definitely acked.
u64 last_rcv_seq = get_last_rcv_seq(tsk);
return frcc->s_probe->s_probe_last_seq > 0 &&
!before(last_rcv_seq, frcc->s_probe->s_probe_last_seq);
}
static bool cruise_ended(struct frcc_data *frcc, u64 now_us)
{
struct param_data *p = frcc->p_params;
u32 max_rtprop_us, max_rtt_us, probe_duration_us, drain_duration_us,
slot_duration_us;
u64 slot_end_us;
max_rtprop_us = p->p_ub_rtprop_us;
if (p->f_slot_greater_than_rtprop) {
max_rtprop_us =
max_t(u32, frcc->s_min_rtprop_us, p->p_ub_rtprop_us);
}
if (p->f_slot_exactly_rtprop) {
max_rtprop_us = frcc->s_min_rtprop_us;
}
max_rtt_us = max_rtprop_us + frcc->s_slot_max_qdel_us;
probe_duration_us = p->p_probe_duration_us;
if (p->f_probe_duration_max_rtt) {
probe_duration_us = max_rtt_us;
}
drain_duration_us = frcc->s_slot_max_qdel_us;
if (p->f_drain_over_rtt) {
drain_duration_us = max_rtt_us;
}
slot_duration_us = max_rtt_us + probe_duration_us + drain_duration_us;
if (p->f_wait_rtt_after_probe) {
slot_duration_us += max_rtt_us;
}
if (p->f_probe_wait_in_max_rtts) {
slot_duration_us =
max_t(u32, slot_duration_us,
max_rtt_us * p->p_probe_wait_rtts +
probe_duration_us + drain_duration_us);
}
slot_end_us = frcc->s_slot_start_time_us + slot_duration_us;
return time_after64(now_us, slot_end_us);
}
static bool should_init_probe_end(struct frcc_data *frcc, struct tcp_sock *tsk)
{
// since last-1 is the last seq part of probe, we are chekcing whether
// next_snd is >= last, this means last-1 has been sent.
u32 last_snd_seq = get_last_snd_seq(tsk);
return frcc->s_probe->s_probe_last_seq > 0 &&
!before(last_snd_seq, frcc->s_probe->s_probe_last_seq) &&
!frcc->s_probe->s_probe_end_initiated;
}
static bool round_ended(struct frcc_data *frcc)
{
return frcc->s_round->s_round_slots_till_now >=
get_slots_per_round(frcc);
}
static bool should_probe(struct frcc_data *frcc)
{
return frcc->s_round->s_round_slots_till_now >=
frcc->s_round->s_round_probe_slot_idx;
}
static u32 get_probe_excess(struct frcc_data *frcc)
{
struct param_data *p = frcc->p_params;
u32 target_flow_count_unit;
u64 excess_pkts;
target_flow_count_unit = get_target_flow_count_unit(frcc);
excess_pkts = p->p_probe_multiplier_unit;
excess_pkts *= target_flow_count_unit;
excess_pkts >>= P_SCALE;
excess_pkts *= frcc->s_round->s_round_max_rate_pps;
excess_pkts *= p->p_ub_rtterr_us;
excess_pkts >>= P_SCALE;
excess_pkts = DIV_ROUND_UP_ULL(excess_pkts, USEC_PER_SEC);
excess_pkts = max_t(u64, excess_pkts, 1);
return excess_pkts;
}
void log_cwnd(enum cwnd_event reason, struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us)
{
#ifdef FRCC_LOG_INFO
printk(KERN_INFO
"frcc cwnd_event reason %u " BASE_FMT ROUND_FMT SLOT_FMT
"last_snd_seq %llu last_rcv_seq %llu",
reason, BASE_VARS, ROUND_VARS, SLOT_VARS, get_last_snd_seq(tsk),
get_last_rcv_seq(tsk));
#endif
}
static void log_probe_start(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us)
{
#ifdef FRCC_LOG_INFO
printk(KERN_INFO "frcc probe_start " BASE_FMT PROBE_FMT, BASE_VARS,
PROBE_VARS);
#endif
}
static void start_probe(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us,
u32 s_slot_min_rtt_us)
{
frcc->s_probe->s_probe_ongoing = true;
frcc->s_probe->s_probe_min_rtt_before_us = s_slot_min_rtt_us;
frcc->s_probe->s_probe_min_rtt_us = U32_MAX;
frcc->s_probe->s_probe_min_excess_delay_us = U32_MAX;
frcc->s_probe->s_probe_prev_cwnd_pkts = tsk->snd_cwnd;
frcc->s_probe->s_probe_excess_pkts = get_probe_excess(frcc);
frcc->s_probe->s_probe_end_initiated = false;
frcc->s_probe->s_probe_drain_pkts_unit = 0;
frcc->s_probe->s_probe_drain_pkts_rem_unit = 0;
frcc->s_probe->s_probe_start_time_us = now_us;
frcc->s_probe->s_probe_start_seq = get_last_snd_seq(tsk);
frcc->s_probe->s_probe_inflightmatch_seq = 0;
frcc->s_probe->s_probe_first_seq = 0;
frcc->s_probe->s_probe_last_seq = 0;
frcc->s_probe->s_probe_first_time_us = 0;
tsk->snd_cwnd = tsk->snd_cwnd + frcc->s_probe->s_probe_excess_pkts;
update_pacing_rate(sk, frcc, tsk, rtt_us, true);
log_probe_start(sk, frcc, tsk, rtt_us, now_us);
log_cwnd(PROBE_GAIN, sk, frcc, tsk, rtt_us, now_us);
}
static void update_cwnd_drain(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us,
const struct rate_sample *rs)
{
struct param_data *p = frcc->p_params;
u32 this_drain_pkts;
if (!p->f_drain_over_rtt) {
return;
}
// TODO: increase precision here as cwnd is larger than P_UNIT. Ideally
// we want P_UNIT > the denominator = cwnd.
frcc->s_probe->s_probe_drain_pkts_rem_unit +=
frcc->s_probe->s_probe_drain_pkts_unit * rs->acked_sacked;
this_drain_pkts = frcc->s_probe->s_probe_drain_pkts_rem_unit >> P_SCALE;
frcc->s_probe->s_probe_drain_pkts_rem_unit &= P_UNIT - 1;
tsk->snd_cwnd = tsk->snd_cwnd - this_drain_pkts;
update_pacing_rate(sk, frcc, tsk, rtt_us, false);
log_cwnd(PROBE_DRAIN, sk, frcc, tsk, rtt_us, now_us);
}
static void initiate_probe_end(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us,
const struct rate_sample *rs)
{
struct param_data *p = frcc->p_params;
frcc->s_probe->s_probe_end_initiated = true;
if (!p->f_drain_over_rtt) {
tsk->snd_cwnd = frcc->s_probe->s_probe_prev_cwnd_pkts;
update_pacing_rate(sk, frcc, tsk, rtt_us, false);
log_cwnd(PROBE_DRAIN, sk, frcc, tsk, rtt_us, now_us);
} else {
// We are amortizing the decrease over a window, so every ack,
// we will reduce by drain_pkts pkts.
frcc->s_probe->s_probe_drain_pkts_unit =
frcc->s_probe->s_probe_excess_pkts << P_SCALE;
do_div(frcc->s_probe->s_probe_drain_pkts_unit, tsk->snd_cwnd);
frcc->s_probe->s_probe_drain_pkts_rem_unit = 0;
update_cwnd_drain(sk, frcc, tsk, rtt_us, now_us, rs);
}
}
static void update_probe_state(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk,
const struct rate_sample *rs, u32 rtt_us,
u64 now_us)
{
// TODO: use inflight = cwnd style checks for start and end of probe.
// The RTT style checks are upper bounds.
struct param_data *p = frcc->p_params;
u64 last_rcv_seq = get_last_rcv_seq(tsk);
u64 last_snd_seq = get_last_snd_seq(tsk);
u64 end_seq_snd_time;
u32 max_rtprop_us =
max_t(u32, p->p_ub_rtprop_us, frcc->s_min_rtprop_us);
// Note, in cruise ended, we check if we are using
// f_slot_greater_than_rtprop, but here for our own flow we definitely
// want the probe to be bigger.
if (p->f_slot_exactly_rtprop) {
max_rtprop_us = frcc->s_min_rtprop_us;
}
u32 max_rtt_us = max_rtprop_us + frcc->s_slot_max_qdel_us;
u32 wait_time_us = max_rtt_us * p->p_probe_wait_rtts;
u64 wait_until_us = frcc->s_probe->s_probe_start_time_us + wait_time_us;
u32 probe_duration_us = p->p_probe_duration_us;
if (p->f_probe_duration_max_rtt) {
probe_duration_us = max_rtt_us;
}
#ifdef FRCC_LOG_TRACE
printk(KERN_INFO "frcc probe_state " BASE_FMT PROBE_DEBUG_FMT SEQ_FMT,
BASE_VARS, PROBE_DEBUG_VARS, SEQ_VARS);
#endif
// last_snd_seq is what we hope to snd next. Checking una > seq is as
// good as checking acked >= seq, which is what we want.
if (frcc->s_probe->s_probe_inflightmatch_seq == 0) {
// The inflight match will happen after half pre-probe-RTT (old
// RTT) under our pacing rate = 2 * new cwnd / old_rtt, i.e.,
// last packet of new cwnd sent at time old_rtt/2.
// Conservatively, we wait a full (packet timed) new RTT.
// Alternatively, we can just check inflight = cwnd.
if (after(last_rcv_seq, frcc->s_probe->s_probe_start_seq)) {
update_pacing_rate(sk, frcc, tsk, rtt_us, false);
frcc->s_probe->s_probe_inflightmatch_seq = last_snd_seq;
if (!p->f_wait_rtt_after_probe) {
frcc->s_probe->s_probe_first_seq = last_snd_seq;
frcc->s_probe->s_probe_first_time_us = now_us;
}
#ifdef FRCC_LOG_DEBUG
printk(KERN_INFO
"frcc probe_inflightmatch flow %u "
"probe_inflightmatch_seq %llu probe_first_seq %llu "
"last_snd_seq %llu last_rcv_seq %llu ",
frcc->id,
frcc->s_probe->s_probe_inflightmatch_seq,
frcc->s_probe->s_probe_first_seq, last_snd_seq,
last_rcv_seq);
#endif
}
} else if (frcc->s_probe->s_probe_first_seq == 0) {
if (after(last_rcv_seq,
frcc->s_probe->s_probe_inflightmatch_seq)) {
if (!p->f_probe_wait_in_max_rtts ||
now_us >= wait_until_us) {
frcc->s_probe->s_probe_first_seq = last_snd_seq;
frcc->s_probe->s_probe_first_time_us = now_us;
#ifdef FRCC_LOG_DEBUG
printk(KERN_INFO
"frcc probe_first_seq flow %u probe_first_seq %llu "
"last_snd_seq %llu last_rcv_seq %llu now %llu ",
frcc->id,
frcc->s_probe->s_probe_first_seq,
last_snd_seq, last_rcv_seq, now_us);
#endif
}
}
} else if (frcc->s_probe->s_probe_last_seq == 0) {
end_seq_snd_time = frcc->s_probe->s_probe_first_time_us +
probe_duration_us;
if (time_after64(now_us, end_seq_snd_time)) {
frcc->s_probe->s_probe_last_seq = last_snd_seq;
#ifdef FRCC_LOG_DEBUG
printk(KERN_INFO
"frcc probe_last_seq flow %u probe_last_seq %llu "
"last_snd_seq %llu last_rcv_seq %llu now %llu ",
frcc->id, frcc->s_probe->s_probe_last_seq,
last_snd_seq, last_rcv_seq, now_us);
#endif
}
}
}
static void log_cwnd_update(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us,
u64 bw_estimate_pps, u64 flow_count_belief_unit,
u64 target_flow_count_unit, u64 target_cwnd_unit,
u64 next_cwnd_unit, u64 now_us)
{
#ifdef FRCC_LOG_INFO
printk(KERN_INFO
"frcc cwnd_update " BASE_FMT ROUND_FMT SLOT_FMT PROBE_FMT
"bw_estimate_pps %llu "
"target_flow_count_unit %llu flow_count_belief_unit %llu "
"target_cwnd_unit %llu next_cwnd_unit %llu ",
BASE_VARS, ROUND_VARS, SLOT_VARS, PROBE_VARS, bw_estimate_pps,
target_flow_count_unit, flow_count_belief_unit, target_cwnd_unit,
next_cwnd_unit);
#endif
}
static void log_round_reset(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us)
{
#ifdef FRCC_LOG_INFO
printk(KERN_INFO "frcc round_reset " BASE_FMT ROUND_FMT, BASE_VARS,
ROUND_VARS);
#endif
}
static void log_slot_end(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us)
{
#ifdef FRCC_LOG_INFO
u32 slot_duration_us =
tcp_stamp_us_delta(now_us, frcc->s_slot_start_time_us);
printk(KERN_INFO "frcc slot_end " BASE_FMT ROUND_FMT SLOT_FMT
"slot_duration_us %u ",
BASE_VARS, ROUND_VARS, SLOT_VARS, slot_duration_us);
#endif
}
static void update_cwnd(struct sock *sk, struct frcc_data *frcc,
struct tcp_sock *tsk, u32 rtt_us, u64 now_us)
{
struct param_data *p = frcc->p_params;
u32 prev_cwnd = frcc->s_probe->s_probe_prev_cwnd_pkts; // tsk->snd_cwnd;
u64 bw_estimate_pps;
u64 flow_count_belief_unit;
u64 target_flow_count_unit;
u64 target_cwnd_unit;
u64 next_cwnd_unit;
u32 next_cwnd;
u64 tcwnd_hi_clamp_unit = prev_cwnd * p->p_cwnd_clamp_hi_unit;
u64 tcwnd_lo_clamp_unit = prev_cwnd * p->p_cwnd_clamp_lo_unit;
u64 tcwnd_num;
u64 tcwnd_den;
bw_estimate_pps = U64_MAX;
if (frcc->s_probe->s_probe_min_excess_delay_us > 0) {
bw_estimate_pps = frcc->s_probe->s_probe_excess_pkts;
bw_estimate_pps *= USEC_PER_SEC;
do_div(bw_estimate_pps,
frcc->s_probe->s_probe_min_excess_delay_us);
}
flow_count_belief_unit = p->p_ub_flow_count << P_SCALE;
// ^^ this can be anything as this will never be read.
if (frcc->s_round->s_round_max_rate_pps > 0) {
flow_count_belief_unit = P_UNIT;
flow_count_belief_unit *= bw_estimate_pps;
do_div(flow_count_belief_unit,
frcc->s_round->s_round_max_rate_pps);
}
flow_count_belief_unit = max_t(u64, flow_count_belief_unit, P_UNIT);
target_flow_count_unit = get_target_flow_count_unit(frcc);
// Even though in the proof we apply the clamps on Ni, we implement
// clamps on target_cwnd because of integer arithmetic
// (target_flow_count_unit might be small, so clamps times this might
// be same as target_flow_count_unit). We expect cwnd to be large so
// that the clamp times cwnd is a different value.
if (target_flow_count_unit == 0 ||
frcc->s_round->s_round_max_rate_pps == 0) {
target_cwnd_unit = prev_cwnd * p->p_cwnd_clamp_hi_unit;
} else {
if (p->f_use_stable_cwnd_update) {
target_cwnd_unit = prev_cwnd << P_SCALE;
tcwnd_num = (frcc->s_min_rtprop_us << P_SCALE) +
p->p_contract_min_qdel_us *
flow_count_belief_unit;
tcwnd_den = (frcc->s_min_rtprop_us << P_SCALE) +
p->p_contract_min_qdel_us *
target_flow_count_unit;
target_cwnd_unit *= tcwnd_num;
do_div(target_cwnd_unit, tcwnd_den);
// TODO: should we convert to unit after, given there
// is already scaling due to num, and den being in us?
// Will unit^2 overflow?
// target_cwnd_unit = target_cwnd_unit << P_SCALE;
} else {
target_cwnd_unit = prev_cwnd << P_SCALE;
target_cwnd_unit *= flow_count_belief_unit;
do_div(target_cwnd_unit, target_flow_count_unit);
}
}
target_cwnd_unit = max_t(u64, target_cwnd_unit, tcwnd_lo_clamp_unit);
target_cwnd_unit = min_t(u64, target_cwnd_unit, tcwnd_hi_clamp_unit);
next_cwnd_unit =
p->p_inv_cwnd_averaging_factor_unit * prev_cwnd +
((p->p_cwnd_averaging_factor_unit * target_cwnd_unit) >>
P_SCALE);
#ifdef FRCC_LOG_DEBUG
printk(KERN_INFO
"frcc cwnd_update_debug flow %u alpha %u 1-alpha %u cwnd %u "
"target_cwnd %llu next_cwnd %llu ",
frcc->id, p->p_cwnd_averaging_factor_unit,
p->p_inv_cwnd_averaging_factor_unit, prev_cwnd target_cwnd_unit,
next_cwnd_unit);
#endif
next_cwnd = DIV_ROUND_UP_ULL(next_cwnd_unit, P_UNIT);
next_cwnd = max_t(u32, next_cwnd, p->p_lb_cwnd_pkts);
tsk->snd_cwnd = next_cwnd;
update_pacing_rate(sk, frcc, tsk, rtt_us, false);
log_cwnd_update(sk, frcc, tsk, rtt_us, bw_estimate_pps,
flow_count_belief_unit, target_flow_count_unit,
target_cwnd_unit, next_cwnd_unit, now_us);
log_cwnd(PROBE_UPDATE, sk, frcc, tsk, rtt_us, now_us);
}