-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_pk3c_main.c
executable file
·1802 lines (1331 loc) · 50.9 KB
/
tcp_pk3c_main.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
/* Pacing Kernel 3 Tcp congestion control algorithm (PK3C 0.908) that is originally based on "Performance-oriented Congestion Control (PCC)" (GPLv2/BSD):
*
* that is rate-based congestion control algorithm that chooses its sending rate
* based on an explicit utility function computed over rtt-length intervals and
* on throughput (calculated speed of packets acked/receive) and on few other ideas:
* 1. Calculated throughput becomes much lower than real when coming close to congestion (means can calculate correctly throughput only when optimal rate);
* 2. Rtt often non reliable for Kernel 3 (so calculating avg rtt in different way than usually for skipping max/min incorrect values);
* 3. The utility function (taken originally from PCC) based on RTT and Lose works Ok only for rates higher than 25000 (so
* this current version of PK3C doesn't work for less than 250KBits/sec links, but can think to use later regular Cubic or Reno when < 25000,
* and similer way new Cubic did that using from 2006 new Cubic for higher rate links and keeping previous TCP/IP behaviour for lower rate links)
* - the reason that original PCC does every "experiment" (originally one "experiment" is 50 packets measurement, but later upgraded, see below)
* and then measures results after this experiment (looking the way how utility function changes or simpler to say of amount of loss and rtt increases or decreases),
* but for lower rates original PCC cannot react on input signals good enough, because of noise being stronger than such input signal
* (and both reaction to previous experiment too slow for seeing it fast enough), so the original idea by PCC can work good only with fast enough connections;
* second problem (apart that original PCC doesn't work good with low rates) is that it doesn't work at all for low latency
* (if latency less than about 5ms, means ping gives 4/1000sec or less, then utility function gives something random and as result PCC doesn't converge);
* - however, did some experiments with addition of minrate/maxrate (exists in this PK3C) and addition of random changes UP/DOWN (for seeing it find in this source all
* the usage of minrate and maxrate), and with these additions (minrate/maxrate) together with original PCC logic the PK3C starts working reasonably with very low latencies too,
* and more important that PK3C uses connections fair enough at least when PK3C competitive connections with similar PK3C connections (and original PCC often didn't utilize channel well
* like one connection could be much faster than other or slower than other);
* 4. The amount of loss calculated differently comparing to original PCC (the idea is to calculate it using info about current sending buffer
* and info about acked packets for marking packets as loss if both exists in resend buffer and both hole in acked, but before RTO happened,
* so before socket value "lost-out" increased);
* 5. Non obvious part of PK3C is slow-start:
* a. Originally PCC did double rate during slow-start until loss detected (similar like Cubic does), but for some cases like local direct wire connections
* loss didn't happen at all (for example because flow control buffers too small and max pacing rate then controlled by receiver), so need some
* additional condition to stop slow-start (the addition condition is based on throughput for comparing current rate with thoughput and if current
* rate much higher than throughput or throughput started decrease, then stop slow-start to some optimal point 1/3 higher than detected throughput);
* The formula to calculate utility func during slow start is
* "util = rate - (rate * (loss_penalty * loss_ratio)) / PCC_SCALE;" (see in code below)
* and loss_penalty is const==25;
* and original PCC did stop slow-start when next calculated util less than prev (this condition kept too as additional OR possibility for end of slow-start);
* also tried to use rtt inside this formula too some time before, but it didn't help at all (mostly because of noise when rtt can change both directions
* both before optimal rate found during slow-start and after);
* so for PK3C using both loss-based slow-start stop (similar to PCC) and additionaly stop slow start on throughput detection (that is required for very low rates,
* because PK3C start from quite high probing rate and can jump imideatelly to optimal throughput detected if lower rate before channel overflow happen);
* The original PCC started from much lower rate, than PK3C, and as result original PCC can take few times longer period of time for slow-start, then PK3C.
* b. The other problem of original PCC is that it often goes too high during slow-start (like it can overflow the buffers too much before it understand that need to stop slow-start),
* so first in PK3C did decrease amount of packets during slow-start (can be about 15 per experiment, see const MIN_PACKETS_PER_INTERVAL, means per interval when slow-start) and
* second start from high rate as first step of slow-start (like rate == PCC_RATE_MIN * 512 * 6 that is about 300000 that is ~3MBs/sec),
* and as result slow-start (for rates from 256KBits to very high like 10Gb/s) finishes the same fast (or faster) like Cubic does. And as result in PK3C no need
* to wait about 20 seconds during slow-start like in original PCC was (and in PK3C now slow-start happens in much less than 1 second in most cases and you can feel it well when browsing
* Internet with PK3C, so it becomes useful for Workstations too).
* 6. The original PCC tried to work the same way both for high rates and low rates connections.
* As result, the higher rate connections utilized all the channel and the lower rate worked few times slower, and for solving this issue:
* a. Added fast-moving mode (so low-rate connections knows that it is low-rate and increases rate very fast based on small few packets intervals
* until some condition);
* b. Did some differentiation in amount of packets per interval (so for high speed connection can be 300 packets per interval and for lower rate 50 packets
* and if slow-start or moving-fast, then 15 packets for slow-start or even lower amount of packets than 15 for moving-fast).
* c. On some additional conditions (based on minrate/maxrate), can switch to fast-moving again (from regular PCC kind mode) even after converge happened.
*/
#include <linux/version.h>
#include <linux/math64.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <net/tcp.h>
#include <linux/types.h>
#include <linux/atomic.h>
#include <linux/moduleparam.h>
#include <linux/delay.h>
#include "tcp_pk3c.h"
#include "commdefs.h"
#include "proc_commonconsts.h"
#include "proc_interface.h"
#include "pk3c_proc_version.h"
#define PK3C_PROBING_EPS 5
#define PK3C_PROBING_EPS_PART 100
#define PK3C_SCALE 1000
#define PK3C_RATE_MIN 128ull // in PK3C was 1024u instead
#define PK3C_RATE_MIN_PACKETS_PER_RTT 2
#define PK3C_INVALID_INTERVAL -1
#define PK3C_IGNORE_PACKETS 10
#define PK3C_INTERVAL_MIN_PACKETS 20
#define PK3C_MIN_INTERVALS_BEFORE_UPDOWN_HANDBRAKE 40
#define MIN_PACKETS_PER_INTERVAL 15
#define PK3C_ALPHA 100
#define PK3C_GRAD_STEP_SIZE 100
#define PK3C_MAX_SWING_BUFFER 2
#define PK3C_LAT_INFL_FILTER 10
#define PK3C_MIN_RATE_DIFF_RATIO_FOR_GRAD 20
#define PK3C_MIN_CHANGE_BOUND 50
#define PK3C_CHANGE_BOUND_STEP 1
#define PK3C_AMP_MIN 2
#define USE_PROBING
#define NUM_OF_PCKTS_FOR_AVG 10
#define MAX_RATE_LIMIT 128000000LL
#define MIN_RATE_LIMIT 25000LL
#define MIN_X_RELIABLE_VALUE 15
#define THE_T_VALUE 25
enum PK3C_LAST_CALL_NAMES {
LCALL_UNKNOWN,
LCALL_INIT,
LCALL_STATE,
LCALL_PROCESS,
LCALL_FINISH,
};
static int log_level = 7; // KERN_DEBUG
static int agent_communication = 1;
static int loss_penalty = 25;
static int rtt_penalty = 25;
static int slow_start_start = PK3C_RATE_MIN * 512 * 6;
module_param(log_level,int,0);
MODULE_PARM_DESC(log_level, "logging level; 7 (default, KERN_DEBUG) - log everything, 0 - log nothing");
module_param(agent_communication,int,0);
MODULE_PARM_DESC(agent_communication, "send messages to agent 1 (default) - yes, 0 - no");
module_param(loss_penalty,int,0);
MODULE_PARM_DESC(loss_penalty, "penalty for loss occured");
module_param(rtt_penalty,int,0);
MODULE_PARM_DESC(rtt_penalty, "penalty for RTT inflation");
module_param(slow_start_start,int,0);
MODULE_PARM_DESC(slow_start_start, "slow start inital rate");
static atomic_t id_ = ATOMIC_INIT(0);
static atomic_t con_cnt_ = ATOMIC_INIT(0);
static unsigned int bufsize __read_mostly = 256;
static struct {
spinlock_t lock;
wait_queue_head_t wait;
unsigned long head, tail;
struct tcp_log *log;
} tcp_probe_pk3c;
int cprintk(const char *buffer, ...)
{
va_list args;
int result = 0;
if (buffer[0] == KERN_SOH_ASCII && buffer[1]) {
switch (buffer[1]) {
case '0' ... '7':
if(buffer[1] -'0' <= log_level) {
va_start(args, buffer);
result = vprintk(buffer, args);
va_end(args);
}
}
}
return result;
}
static inline u64 ktime_u64(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,0,0)
return ktime_get_real().tv64;
#else
return ktime_get_real();
#endif
}
static u32 pk3c_get_rtt(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,0,0)
struct pk3c_data *pk3c = inet_csk_ca(sk);
if (pk3c && pk3c->lrtt) {
return max(pk3c->lrtt, 1U);
#else
if (tp->srtt_us) {
return max(tp->srtt_us >> 3, 1U);
#endif
} else {
return USEC_PER_MSEC;
}
}
static void pk3c_set_cwnd(struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
u64 cwnd = sk->sk_pacing_rate;
cwnd *= pk3c_get_rtt(sk);
cwnd /= tp->mss_cache;
cwnd /= USEC_PER_SEC;
cwnd *= 2;
cwnd = max(4ULL, cwnd);
cwnd = min((u32)cwnd, tp->snd_cwnd_clamp);
tp->snd_cwnd = cwnd;
}
bool pk3c_valid(struct pk3c_data *pk3c)
{
return (pk3c && pk3c->intervals && pk3c->intervals[0].rate);
}
static void pk3c_setup_intervals_probing(struct pk3c_data *pk3c)
{
s64 rate_low, rate_high;
char rand;
int i;
get_random_bytes(&rand, 1);
rate_high = pk3c->nums->rate * (PK3C_PROBING_EPS_PART + PK3C_PROBING_EPS * pk3c->nums->probing_cnt );
rate_low = pk3c->nums->rate * (PK3C_PROBING_EPS_PART - PK3C_PROBING_EPS * pk3c->nums->probing_cnt );
rate_high /= PK3C_PROBING_EPS_PART;
rate_low /= PK3C_PROBING_EPS_PART;
if(rate_low < MIN_RATE_LIMIT/2)
rate_low = MIN_RATE_LIMIT/2;
if(rate_high > MAX_RATE_LIMIT)
rate_high = MAX_RATE_LIMIT;
for (i = 0; i < PK3C_INTERVALS; i += 2) {
if ((rand >> (i / 2)) & 1) {
pk3c->intervals[i].rate = rate_low;
pk3c->intervals[i + 1].rate = rate_high;
} else {
pk3c->intervals[i].rate = rate_high;
pk3c->intervals[i + 1].rate = rate_low;
}
pk3c->intervals[i].packets_sent_base = 0;
pk3c->intervals[i + 1].packets_sent_base = 0;
pk3c->intervals[i].total_snt = 0;
pk3c->intervals[i + 1].total_snt = 0;
}
pk3c->send_index = 0;
pk3c->receive_index = 0;
pk3c->wait = false;
cprintk(KERN_DEBUG "setup_intervals_probing, so set pk3c->wait = false\n");
}
static void pk3c_setup_intervals_moving(struct pk3c_data *pk3c, u32 bytes_acked)
{
pk3c->intervals[0].packets_sent_base = 0;
pk3c->intervals[0].min_rtt = BIG_VAL_S32;
pk3c->intervals[0].max_rtt = 0;
pk3c->intervals[0].rate = pk3c->nums->rate;
pk3c->send_index = 0;
pk3c->receive_index = 0;
pk3c->wait = false;
}
static void start_interval(struct sock *sk, struct pk3c_data *pk3c)
{
u64 rate;
s64 max_thr;
struct tcp_sock *tp = tcp_sk(sk);
bool hb = false;
struct pk3c_interval *interval;
rate = pk3c->nums->rate;
max_thr = pk3c->nums->thr_prev;
if(pk3c->nums->thr > max_thr)
rate = max_thr;
if(pk3c->start_mode && max_thr && max_thr > rate / 10000 &&
max_thr < rate / 3 &&
max_thr*2 >= MIN_RATE_LIMIT &&
max_thr <= MAX_RATE_LIMIT
) {
rate = max_thr*3/2;
if(rate < MIN_RATE_LIMIT) {
rate = MIN_RATE_LIMIT;
pk3c->nums->rate = rate;
}
hb = true;
cprintk(KERN_DEBUG "%i HANDBRAKE DURING SLOWSTART: reinit rate from %llu to %llu based on thr_prev %llu\n", pk3c->nums->id, pk3c->nums->rate, rate, pk3c->nums->thr_prev);
pk3c->nums->rate = rate;
pk3c->nums->handbrake = rate;
}
if (!pk3c->wait) {
if(pk3c->start_mode)
cprintk(KERN_DEBUG "Start mode and !pk3c->wait\n");
interval = &pk3c->intervals[pk3c->send_index];
interval->packets_ended = 0;
interval->lost = 0;
interval->delivered = 0;
interval->cnt_min_pkts = 0;
interval->min_rtt = BIG_VAL_S32;
interval->max_rtt = 0;
interval->packets_sent_base = pk3c->nums->pcktscnt;
interval->packets_out_base = tcp_sk(sk)->packets_out;
interval->packets_sent_base = max(interval->packets_sent_base, 1U);
interval->send_start = ktime_u64()/1000;
interval->send_end = 0;
if(!hb)
rate = interval->rate;
interval->recv_start = 0;
interval->recv_end = 0;
}
rate = max(rate, MIN_RATE_LIMIT);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,9,0)
rate = min(rate, (u64)sk->sk_max_pacing_rate);
sk->sk_pacing_rate = rate;
pk3c_set_cwnd(sk);
cprintk(KERN_DEBUG "kernel 4.9+: id=%i set rate to %llu minrate %llu maxrate %llu\n", pk3c->nums->id, rate, pk3c->nums->minrate, pk3c->nums->maxrate);
#else
cprintk(KERN_DEBUG "kernel 3.x: id=%i set rate to %llu minrate %llu maxrate %llu\n", pk3c->nums->id, rate, pk3c->nums->minrate, pk3c->nums->maxrate);
sk->sk_pacing_rate = rate;
sk->sk_max_pacing_rate = rate;
pk3c_set_cwnd(sk);
#endif
if(pk3c->nums->minrate + pk3c->nums->minrate / 300 < pk3c->nums->maxrate / 2)
pk3c->nums->minrate += pk3c->nums->minrate / 300;
if(pk3c->nums->maxrate - pk3c->nums->maxrate / 10 > pk3c->nums->minrate * 2)
pk3c->nums->maxrate -= pk3c->nums->maxrate / 10;
if(rate < pk3c->nums->minrate)
pk3c->nums->minrate = rate;
if(rate > pk3c->nums->maxrate)
pk3c->nums->maxrate = rate;
}
#define PK3C_LOSS_MARGIN 5
#define PK3C_MAX_LOSS 10
static u32 pk3c_exp(s32 x)
{
s64 temp = PK3C_SCALE;
s64 e = PK3C_SCALE;
int i;
for (i = 1; temp != 0; i++) {
temp *= x;
temp /= i;
temp /= PK3C_SCALE;
e += temp;
}
return e;
}
static s64 pk3c_calc_util_grad(s64 rate_1, s64 util_1, s64 rate_2, s64 util_2) {
s64 rate_diff_ratio = (PK3C_SCALE * (rate_2 - rate_1)) / rate_1;
if (rate_diff_ratio < PK3C_MIN_RATE_DIFF_RATIO_FOR_GRAD &&
rate_diff_ratio > -1 * PK3C_MIN_RATE_DIFF_RATIO_FOR_GRAD)
return 0;
return (PK3C_SCALE * PK3C_SCALE * (util_2 - util_1)) / (rate_2 - rate_1);
}
static void init_monitor(struct sock *sk, struct pk3c_data *pk3c)
{
struct tcp_sock *tp = tcp_sk(sk);
pk3c->monitor_lost->valid = 0;
pk3c->monitor_lost->start_time = ktime_u64();
pk3c->monitor_lost->snd_start_seq = tp->snd_nxt;
pk3c->monitor_lost->snd_end_seq = 0;
if(!pk3c->monitor_lost->last_acked_seq)
pk3c->monitor_lost->last_acked_seq = tp->snd_nxt;
pk3c->monitor_lost->bytes_lost = 0;
}
static u64 calc_T_formula(u64 rate, u32 lrtt, struct sock *sk)
{
s64 res;
struct tcp_sock *tp = tcp_sk(sk);
res = rate/tp->mss_cache*(u64)lrtt/1000000;
if(res < 1)
res = 1;
if(res > 500)
res = 500;
return res;
}
static void pk3c_calc_utility_vivace(struct pk3c_data *pk3c, struct pk3c_interval *interval, struct sock *sk) {
s64 loss_ratio, delivered, received, lost, snt, mss, rate, throughput, util;
s64 lat_infl = 0;
s64 rtt_diff;
s64 rtt_diff_thresh = 0;
s64 send_dur;
s64 recv_dur;
s64 recv_bytes;
int vals;
bool handbrake = false;
snt = (interval->packets_ended - interval->packets_sent_base);
rtt_diff = 0;
recv_dur = interval->recv_end - interval->recv_start;
recv_bytes = interval->recv_end_acked_bytes - interval->recv_start_acked_bytes;
send_dur = interval->send_end - interval->send_start;
lost = interval->lost;
delivered = interval->delivered;
mss = tcp_sk(sk)->mss_cache;
received = recv_bytes / mss;
if(received > delivered)
received = delivered;
rate = interval->rate;
throughput = 0;
if (recv_dur > 0 && recv_bytes > 0) {
throughput = (USEC_PER_SEC * (recv_bytes) ) / recv_dur;
if(throughput < MIN_RATE_LIMIT)
throughput = rate / 2;
if(!pk3c->nums->thr_prev || !pk3c->nums->lost || pk3c->nums->thr_prev_cnt<2) {
if(pk3c->nums->thr > pk3c->nums->thr_prev || pk3c->nums->thr_prev_cnt > 10)
pk3c->nums->thr_prev = pk3c->nums->thr;
if(pk3c->nums->thr_prev_cnt < 2 && throughput > pk3c->nums->thr_prev)
pk3c->nums->thr_prev = throughput;
pk3c->nums->thr_prev_cnt++;
}
pk3c->nums->thr = throughput;
pk3c->nums->lost = lost;
} else {
throughput = 1;
}
if (delivered == 0) {
cprintk(KERN_DEBUG "No packets delivered\n");
interval->utility = -BIG_VAL_S32;
return;
}
interval->avg_start_rtt = 0;
interval->avg_end_rtt = 0;
vals = 0;
if(interval->cnt_min_pkts > 0) {
for(vals = 0; vals < interval->cnt_min_pkts && vals < NUM_OF_PCKTS_FOR_AVG; vals++) {
interval->avg_start_rtt += interval->start_rtt[vals];
interval->avg_end_rtt += interval->end_rtt[vals];
}
}
if(vals > 0) {
interval->avg_start_rtt /= vals;
interval->avg_end_rtt /= vals;
}
if(pk3c->start_mode && pk3c->nums->slow_start_prev_rtt) {
rtt_diff = interval->avg_end_rtt - pk3c->nums->slow_start_prev_rtt;
}
else if(pk3c->start_mode)
rtt_diff = 0;
else
rtt_diff = interval->avg_end_rtt - interval->avg_start_rtt;
if(!pk3c->nums->handbrake) {
if(lost > 0)
pk3c->nums->lostcnt = 10;
else
if(pk3c->nums->lostcnt)
pk3c->nums->lostcnt--;
if(!pk3c->start_mode && lost && !pk3c->nums->cnt_no_lost_intervals &&
!pk3c->moving_fast &&
snt > 12
) {
if(lost > snt/20) {
handbrake = true;
if(rate >= MIN_RATE_LIMIT) {
if(pk3c->nums->maxrate && pk3c->nums->rate > (pk3c->nums->maxrate+pk3c->nums->minrate)/2 - (pk3c->nums->maxrate+pk3c->nums->minrate)/4 )
pk3c->nums->handbrake = pk3c->nums->rate * 3 / 4;
else {
pk3c->nums->handbrake = pk3c->nums->rate * 8 / 9;
}
} else
cprintk(KERN_DEBUG "%i new DEBUG HANDBRAKE (also based on %llu > 10) inited handbrake in _vivace, based lost=%llu > 10%%=%llu (updated rate from %llu to %llu)\n", pk3c->nums->id, calc_T_formula(rate, pk3c->lrtt, sk), lost, snt, pk3c->nums->rate, pk3c->nums->handbrake);
pk3c->nums->cnt_no_lost_intervals = PK3C_MIN_INTERVALS_BEFORE_UPDOWN_HANDBRAKE;
if(pk3c->nums->maxrate && rate < (pk3c->nums->maxrate+pk3c->nums->minrate)/2 - (pk3c->nums->maxrate+pk3c->nums->minrate)/4 )
pk3c->nums->cnt_no_lost_intervals /= 10;
if(pk3c->nums->handbrake && pk3c->nums->handbrake < MIN_RATE_LIMIT)
pk3c->nums->handbrake = MIN_RATE_LIMIT;
}
}
if(pk3c->nums->cnt_no_lost_intervals)
pk3c->nums->cnt_no_lost_intervals--;
}
interval->cnt_min_pkts = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,0,0)
if (throughput > 0)
rtt_diff_thresh = (2 * mss) / throughput;
if (send_dur > 0)
lat_infl = (PK3C_SCALE * rtt_diff) / send_dur;
#else
if (throughput > 0)
rtt_diff_thresh = (2 * USEC_PER_SEC * mss) / throughput;
if (send_dur > 0) {
lat_infl = (PK3C_SCALE * rtt_diff) / send_dur;
}
#endif
pk3c->nums->slow_start_prev_rtt = interval->avg_start_rtt;
if (lat_infl < PK3C_LAT_INFL_FILTER && lat_infl > -1 * PK3C_LAT_INFL_FILTER) {
lat_infl = 0;
}
if (lat_infl < 0 && pk3c->start_mode) {
lat_infl = 0;
}
loss_ratio = (lost * PK3C_SCALE) / (lost + snt);
if (pk3c->start_mode && loss_ratio < 100)
loss_ratio = 0;
if(!pk3c->nums->thr && rate)
pk3c->nums->thr = rate;
if(pk3c->start_mode) {
util = rate - (rate * (loss_penalty * loss_ratio)) / PK3C_SCALE;
} else {
if(pk3c->moving_fast)
util = rate - rate * (rtt_penalty * lat_infl + loss_penalty * loss_ratio) / PK3C_SCALE;
else
util = pk3c->nums->thr - pk3c->nums->thr * (rtt_penalty * lat_infl + loss_penalty * loss_ratio) / PK3C_SCALE;
}
cprintk(KERN_INFO
"%d ucalc: rate %lld sent %u delv %lld ~recv %lld lost %lld lat (%lld->%lld) util %lld thpt %lld\n",
pk3c->nums->id, rate, snt,
delivered, received, lost, (interval->avg_start_rtt / USEC_PER_MSEC), (interval->avg_end_rtt / USEC_PER_MSEC),
util, throughput);
if(!pk3c->nums->handbrake) {
if(handbrake) {
interval->utility = 0;
if(!pk3c->nums->handbrake) {
if(pk3c->nums->rate >= MIN_RATE_LIMIT) {
pk3c->nums->handbrake = pk3c->nums->rate;
}
else {
pk3c->nums->handbrake = MIN_RATE_LIMIT;
}
}
}
}
interval->utility = util;
}
static enum PK3C_DECISION pk3c_get_decision(struct pk3c_data *pk3c, u32 new_rate)
{
if (new_rate >= MAX_RATE_LIMIT)
return PK3C_RATE_DOWN;
if (pk3c->nums->rate == new_rate)
return PK3C_RATE_STAY;
return pk3c->nums->rate < new_rate ? PK3C_RATE_UP : PK3C_RATE_DOWN;
}
static u32 pk3c_decide_rate(struct pk3c_data *pk3c)
{
bool run_1_res_, run_2_res_, did_agree_;
int i2 = 2;
int i3 = 3;
u32 ret;
if(pk3c->intervals[0].rate != pk3c->intervals[2].rate) {
i3 = 2;
i2 = 3;
}
run_1_res_ = pk3c->intervals[0].utility > pk3c->intervals[1].utility;
run_2_res_ = pk3c->intervals[i2].utility > pk3c->intervals[i3].utility;
did_agree_ = run_1_res_ == run_2_res_;
if(did_agree_) {
if (run_2_res_) {
pk3c->intervals[0].utility = pk3c->intervals[i2].utility;
ret = pk3c->intervals[i2].rate;
} else {
pk3c->intervals[0].utility = pk3c->intervals[i3].utility;
ret = pk3c->intervals[i3].rate;
}
} else {
ret = pk3c->nums->rate;
}
return ret;
}
static void pk3c_decide(struct pk3c_data *pk3c, struct sock *sk)
{
struct tcp_sock *tp = tcp_sk(sk);
struct pk3c_interval *interval;
u32 new_rate;
int i;
bool can_enter_fastmoving_based_on_throughput;
u32 total_snt;
u64 curtime;
u32 total_packets_lost = 0;
u32 total_delivered = 0;
s64 total_utility = 0;
s64 total_rate = 0;
u32 min_rtt = BIG_VAL_S32;
u32 max_rtt = 0;
u32 total_avg_rtt = 0;
u32 total_lost = 0;
for (i = 0; i < PK3C_INTERVALS; i++ ) {
interval = &pk3c->intervals[i];
(*pk3c->util_func)(pk3c, interval, sk);
total_packets_lost += interval->lost;
total_delivered += interval->delivered;
total_utility += interval->utility;
total_avg_rtt += (pk3c->intervals[i].avg_start_rtt + pk3c->intervals[i].avg_end_rtt)/2;
total_rate += interval->rate;
total_lost += interval->lost;
if(pk3c->intervals[i].max_rtt > max_rtt)
max_rtt = pk3c->intervals[i].max_rtt;
if(pk3c->intervals[i].min_rtt < min_rtt)
min_rtt = pk3c->intervals[i].min_rtt;
}
total_snt = tp->bytes_acked - pk3c->nums->bytesacked_tot;
if(total_snt > BIG_VAL_S32)
interval->total_snt = 0;
pk3c->nums->bytesacked_tot = tp->bytes_acked;
curtime = ktime_u64()/1000;
pk3c->nums->bytesacked_tot_send_start = curtime+1;
pk3c->monitor_lost->total_bytes_lost = 0;
init_monitor(sk, pk3c);
pk3c->monitor_lost->valid = 1;
if(!pk3c->nums->handbrake)
new_rate = pk3c_decide_rate(pk3c);
else
new_rate = pk3c->nums->handbrake;
new_rate = min(new_rate, MAX_RATE_LIMIT);
new_rate = max(new_rate, MIN_RATE_LIMIT);
if (new_rate == MIN_RATE_LIMIT || pk3c->nums->rate == MIN_RATE_LIMIT || new_rate != pk3c->nums->rate || pk3c->nums->handbrake) {
cprintk(KERN_DEBUG "%d decide: on new rate %d %d (%d)\n",
pk3c->nums->id, pk3c->nums->rate < new_rate, new_rate,
pk3c->nums->decisions_count);
pk3c->moving = true;
pk3c->nums->last_rate = pk3c->nums->rate;
can_enter_fastmoving_based_on_throughput = false;
if(pk3c->nums->thr && abs(new_rate - pk3c->nums->thr) < pk3c->nums->thr*5 && new_rate*2 < pk3c->nums->thr*3 && new_rate > ((pk3c->nums->maxrate+pk3c->nums->minrate)/2 - (pk3c->nums->maxrate+pk3c->nums->minrate)/4) )
can_enter_fastmoving_based_on_throughput = true;
if(new_rate < MIN_RATE_LIMIT*2)
can_enter_fastmoving_based_on_throughput = true;
if(calc_T_formula(new_rate, pk3c->lrtt, sk) < MIN_X_RELIABLE_VALUE && can_enter_fastmoving_based_on_throughput) {
cprintk(KERN_DEBUG "%i start moving fast. The rate/mss * rtt/1000000 = %llu\n", pk3c->nums->id, calc_T_formula(new_rate, pk3c->lrtt, sk));
pk3c->last_decision = PK3C_RATE_UP;
pk3c->moving_fast = true;
}
pk3c->nums->rate = new_rate;
pk3c_setup_intervals_moving(pk3c, tp->bytes_acked);
pk3c->nums->prev_last_rate = pk3c->nums->last_rate;
pk3c->nums->probing_cnt = 1;
} else {
cprintk(KERN_DEBUG "%d decide: stay %lld (%d)\n", pk3c->nums->id,
pk3c->nums->rate, pk3c->nums->decisions_count);
if(pk3c->nums->probing_cnt < 126)
pk3c->nums->probing_cnt++;
pk3c_setup_intervals_probing(pk3c);
}
pk3c->nums->rate = new_rate;
start_interval(sk, pk3c);
pk3c->nums->decisions_count++;
}
static void pk3c_update_step_params(struct pk3c_data *pk3c, s64 step) {
if ((step > 0) == (pk3c->nums->rate > pk3c->nums->last_rate)) {
if (pk3c->nums->swing_buffer > 0)
pk3c->nums->swing_buffer--;
else
pk3c->nums->amplifier++;
} else {
pk3c->nums->swing_buffer = min(pk3c->nums->swing_buffer + 1, PK3C_MAX_SWING_BUFFER);
pk3c->nums->amplifier = PK3C_AMP_MIN;
pk3c->nums->change_bound = PK3C_MIN_CHANGE_BOUND;
}
}
static s64 pk3c_apply_change_bound(struct pk3c_data *pk3c, s64 step) {
s32 step_sign;
s64 change_ratio;
if (pk3c->nums->rate == 0)
return step;
step_sign = step > 0 ? 1 : -1;
step *= step_sign;
change_ratio = (PK3C_SCALE * step) / pk3c->nums->rate;
if (change_ratio > pk3c->nums->change_bound) {
step = (pk3c->nums->rate * pk3c->nums->change_bound) / PK3C_SCALE;
pk3c->nums->change_bound += PK3C_CHANGE_BOUND_STEP;
} else {
pk3c->nums->change_bound = PK3C_MIN_CHANGE_BOUND;
}
return step_sign * step;
}
static u32 pk3c_decide_rate_moving(struct sock *sk, struct pk3c_data *pk3c)
{
struct pk3c_interval *interval = &pk3c->intervals[0];
s64 utility, prev_utility;
s64 grad, step, min_step;
struct tcp_sock *tp = tcp_sk(sk);
prev_utility = interval->utility;
if(!pk3c->nums->handbrake) {
(*pk3c->util_func)(pk3c, interval, sk);
interval->total_snt = tp->bytes_acked - pk3c->nums->bytesacked_tot;
if(interval->total_snt > BIG_VAL_S32)
interval->total_snt = 0;
pk3c->nums->bytesacked_tot = tp->bytes_acked;
interval->send_start = pk3c->nums->bytesacked_tot_send_start;
interval->recv_end = ktime_u64()/1000;
pk3c->monitor_lost->total_bytes_lost = 0;
pk3c->nums->bytesacked_tot_send_start = interval->recv_end+1;
utility = interval->utility;
} else {
utility = 0;
}
init_monitor(sk, pk3c);
pk3c->monitor_lost->valid = 1;
if(pk3c->nums->handbrake) {
if(pk3c->nums->handbrake>=MIN_RATE_LIMIT && pk3c->nums->handbrake<=MAX_RATE_LIMIT) {
cprintk(KERN_DEBUG "%i EMERGENCY-IN-WORK DEBUG HANDBRAKE update rate to %lld, because of previous handbrake\n", pk3c->nums->id, pk3c->nums->handbrake);
pk3c->nums->rate = pk3c->nums->handbrake;
pk3c->nums->handbrake = 0;
return pk3c->nums->rate;
} else {
pk3c->nums->rate = pk3c->nums->last_rate;
pk3c->nums->rate = min(pk3c->nums->rate, MAX_RATE_LIMIT);
pk3c->nums->rate = max(pk3c->nums->rate, MIN_RATE_LIMIT);
pk3c->nums->handbrake = 0;
return pk3c->nums->rate;
}
}
cprintk(KERN_DEBUG "%d mv: pr %lld pu %lld nr %llu nu %lld\n",
pk3c->nums->id, pk3c->nums->last_rate, prev_utility, pk3c->nums->rate, utility);
grad = pk3c_calc_util_grad(pk3c->nums->rate, utility, pk3c->nums->last_rate, prev_utility);
step = grad * PK3C_GRAD_STEP_SIZE;
pk3c_update_step_params(pk3c, step);
step *= pk3c->nums->amplifier;
step /= PK3C_SCALE;
step = pk3c_apply_change_bound(pk3c, step);
min_step = (pk3c->nums->rate * PK3C_MIN_RATE_DIFF_RATIO_FOR_GRAD) / PK3C_SCALE;
min_step *= 11;
min_step /= 10;
if (step >= 0 && step < min_step)
step = min_step;
else if (step < 0 && step > -1 * min_step)
step = -1 * min_step;
cprintk(KERN_DEBUG "%d mv: grad %lld STEP %lld amp %d min_step %lld\n",
pk3c->nums->id, grad, step, pk3c->nums->amplifier, min_step);
if(pk3c->nums->rate + step > MAX_RATE_LIMIT) {
return MAX_RATE_LIMIT;
}
if(pk3c->nums->rate + step < MIN_RATE_LIMIT) {
return pk3c->nums->rate;
}
return pk3c->nums->rate + step;
}
static void pk3ctcp_init(struct sock* sk)
{
struct pk3c_data *pk3c = inet_csk_ca(sk);
struct tcp_sock *tp = tcp_sk(sk);
pk3c->intervals = kzalloc(sizeof(struct pk3c_interval) * PK3C_INTERVALS,
GFP_ATOMIC);
if (!pk3c->intervals) {
cprintk(KERN_ERR "init fails (cannot allocate intervals)\n");
return;
}
pk3c->nums = kzalloc(sizeof(struct pk3c_nums), GFP_ATOMIC);
if (!pk3c->nums) {
kfree(pk3c->intervals);
pk3c->intervals = NULL;
cprintk(KERN_ERR "init fails (cannot allocate nums)\n");
return;
}
pk3c->monitor_lost = kzalloc(sizeof(struct pk3c_monitor), GFP_ATOMIC);
if (!pk3c->monitor_lost) {
kfree(pk3c->nums);
pk3c->nums = NULL;
kfree(pk3c->intervals);
pk3c->intervals = NULL;
cprintk(KERN_ERR "init fails (cannot allocate monitor_lost)\n");
return;
}
cprintk(KERN_DEBUG "Called pk3ctcp_init\n");
init_monitor(sk, pk3c);
pk3c->monitor_lost->total_bytes_lost = 0;
pk3c->lrtt = 0;
pk3c->nums->minrtt = BIG_VAL_S32;
pk3c->nums->minrtt_rate = MIN_RATE_LIMIT;
pk3c->nums->handbrake = 0;
pk3c->nums->pcktscnt = 1;
pk3c->nums->bytesacked_tot = tp->bytes_acked;
pk3c->nums->lostpckts = 0;
pk3c->nums->thr = 0;
pk3c->nums->thr_prev = 0;
pk3c->nums->thr_prev_cnt = 0;
pk3c->nums->spare = 0;
pk3c->nums->id = atomic_inc_return(&id_);
pk3c->nums->amplifier = PK3C_AMP_MIN;
pk3c->nums->swing_buffer = 0;
pk3c->nums->change_bound = PK3C_MIN_CHANGE_BOUND;
pk3c->nums->pcktscnt_prev = 0;
pk3c->last_decision = PK3C_RATE_STAY;
pk3c->nums->probing_cnt = 1;
pk3c->nums->decisions_count = 0;
pk3c->nums->lostcnt = 0;
pk3c->nums->minrate = BIG_VAL_S32;
pk3c->nums->maxrate = 0;
pk3c->nums->snd_count = 0;
pk3c->nums->rate = slow_start_start;
pk3c->nums->last_rate = 0;
pk3c->nums->prev_last_rate = 0;
pk3c->nums->lost_base = 0;
pk3c->nums->delivered_base = 0;
pk3c->nums->cnt_no_lost_intervals = PK3C_MIN_INTERVALS_BEFORE_UPDOWN_HANDBRAKE;
pk3c->nums->slow_start_prev_rtt = 0;
pk3c->nums->last_rate = PK3C_RATE_MIN*512;
tcp_sk(sk)->snd_ssthresh = TCP_INFINITE_SSTHRESH;
pk3c->start_mode = true;
pk3c->wait = false;
cprintk(KERN_DEBUG "init, so set pk3c->wait = false\n");
pk3c->moving = false;
pk3c->intervals[0].utility = S64_MIN;
pk3c->util_func = &pk3c_calc_utility_vivace;
pk3c_setup_intervals_probing(pk3c);
start_interval(sk,pk3c);
set_con_cnt(atomic_inc_return(&con_cnt_));
pk3c->prev_call = LCALL_INIT;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4,9,0)
cmpxchg(&sk->sk_pacing_status, SK_PACING_NONE, SK_PACING_NEEDED);
#endif
}
static void pk3ctcp_release(struct sock *sk)
{
struct pk3c_data* pk3c = inet_csk_ca(sk);
if (pk3c->prev_call == LCALL_INIT) {
pk3c->prev_call = LCALL_FINISH;
cprintk(KERN_DEBUG "Kfree intervals\n");
if (pk3c->monitor_lost)
kfree(pk3c->monitor_lost);
pk3c->monitor_lost = NULL;
if (pk3c->intervals)
kfree(pk3c->intervals);
pk3c->intervals = NULL;