/
sch_cake.c
2115 lines (1779 loc) · 54.2 KB
/
sch_cake.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
/*
* COMMON Applications Kept Enhanced (CAKE) discipline - version 3
*
* Copyright (C) 2014-2017 Jonathan Morton <chromatix99@gmail.com>
* Copyright (C) 2015-2017 Toke Høiland-Jørgensen <toke@toke.dk>
* Copyright (C) 2014-2017 Dave Täht <dave+github@taht.net>
* Copyright (C) 2015-2017 Sebastian Moeller <moeller0@gmx.de>
* Copyright (C) 2015-2017 Kevin Darbyshire-Bryant <kevin@darbyshire-bryant.me.uk>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* Alternatively, provided that this notice is retained in full, this
* software may be distributed under the terms of the GNU General
* Public License ("GPL") version 2, in which case the provisions of the
* GPL apply INSTEAD OF those given above.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*/
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/jiffies.h>
#include <linux/string.h>
#include <linux/in.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/skbuff.h>
#include <linux/jhash.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/reciprocal_div.h>
#include <net/netlink.h>
#include <linux/version.h>
#include "pkt_sched.h"
#include <linux/if_vlan.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
#include <net/flow_keys.h>
#else
#include <net/flow_dissector.h>
#endif
#include "cobalt.c"
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
#include <net/netfilter/nf_conntrack_core.h>
#include <net/netfilter/nf_conntrack_zones.h>
#include <net/netfilter/nf_conntrack.h>
#endif
#if (KERNEL_VERSION(4,4,11) > LINUX_VERSION_CODE) || ((KERNEL_VERSION(4,5,0) <= LINUX_VERSION_CODE) && (KERNEL_VERSION(4,5,5) > LINUX_VERSION_CODE))
#define qdisc_tree_reduce_backlog(_a,_b,_c) qdisc_tree_decrease_qlen(_a,_b)
#endif
/* The CAKE Principles:
* (or, how to have your cake and eat it too)
*
* This is a combination of several shaping, AQM and FQ
* techniques into one easy-to-use package:
*
* - An overall bandwidth shaper, to move the bottleneck away
* from dumb CPE equipment and bloated MACs. This operates
* in deficit mode (as in sch_fq), eliminating the need for
* any sort of burst parameter (eg. token bucket depth).
* Burst support is limited to that necessary to overcome
* scheduling latency.
*
* - A Diffserv-aware priority queue, giving more priority to
* certain classes, up to a specified fraction of bandwidth.
* Above that bandwidth threshold, the priority is reduced to
* avoid starving other tins.
*
* - Each priority tin has a separate Flow Queue system, to
* isolate traffic flows from each other. This prevents a
* burst on one flow from increasing the delay to another.
* Flows are distributed to queues using a set-associative
* hash function.
*
* - Each queue is actively managed by Codel. This serves
* flows fairly, and signals congestion early via ECN
* (if available) and/or packet drops, to keep latency low.
* The codel parameters are auto-tuned based on the bandwidth
* setting, as is necessary at low bandwidths.
*
* The configuration parameters are kept deliberately simple
* for ease of use. Everything has sane defaults. Complete
* generality of configuration is *not* a goal.
*
* The priority queue operates according to a weighted DRR
* scheme, combined with a bandwidth tracker which reuses the
* shaper logic to detect which side of the bandwidth sharing
* threshold the tin is operating. This determines whether
* a priority-based weight (high) or a bandwidth-based weight
* (low) is used for that tin in the current pass.
*
* This qdisc incorporates much of Eric Dumazet's fq_codel code, which
* he kindly granted us permission to use, which we customised for use as an
* integrated subordinate. See sch_fq_codel.c for details of
* operation.
*/
#define CAKE_SET_WAYS (8)
#define CAKE_MAX_TINS (8)
#define CAKE_QUEUES (1024)
#ifndef CAKE_VERSION
#define CAKE_VERSION "unknown"
#endif
static char *cake_version __attribute__((used)) = "Cake version: "
CAKE_VERSION;
enum {
CAKE_SET_NONE = 0,
CAKE_SET_SPARSE,
CAKE_SET_SPARSE_WAIT, // counted in SPARSE, actually in BULK
CAKE_SET_BULK,
CAKE_SET_DECAYING
};
struct cake_flow {
/* this stuff is all needed per-flow at dequeue time */
struct sk_buff *head;
struct sk_buff *tail;
struct list_head flowchain;
s32 deficit;
struct cobalt_vars cvars;
u16 srchost; /* index into cake_host table */
u16 dsthost;
u8 set;
}; /* please try to keep this structure <= 64 bytes */
struct cake_host {
u32 srchost_tag;
s16 srchost_deficit;
u16 srchost_refcnt;
u32 dsthost_tag;
s16 dsthost_deficit;
u16 dsthost_refcnt;
};
struct cake_heap_entry {
u16 t:3, b:10;
};
struct cake_tin_data {
struct cake_flow flows[CAKE_QUEUES];
u32 backlogs[CAKE_QUEUES];
u32 tags[CAKE_QUEUES]; /* for set association */
u16 overflow_idx[CAKE_QUEUES];
struct cake_host hosts[CAKE_QUEUES]; /* for triple isolation */
u32 perturbation;
u16 flow_quantum;
struct cobalt_params cparams;
u32 drop_overlimit;
u16 bulk_flow_count;
u16 sparse_flow_count;
u16 decaying_flow_count;
u16 unresponsive_flow_count;
u16 max_skblen;
struct list_head new_flows;
struct list_head old_flows;
struct list_head decaying_flows;
/* time_next = time_this + ((len * rate_ns) >> rate_shft) */
u64 tin_time_next_packet;
u32 tin_rate_ns;
u32 tin_rate_bps;
u16 tin_rate_shft;
u16 tin_quantum_prio;
u16 tin_quantum_band;
s32 tin_deficit;
u32 tin_backlog;
u32 tin_dropped;
u32 tin_ecn_mark;
u32 packets;
u64 bytes;
/* moving averages */
cobalt_time_t avge_delay;
cobalt_time_t peak_delay;
cobalt_time_t base_delay;
/* hash function stats */
u32 way_directs;
u32 way_hits;
u32 way_misses;
u32 way_collisions;
}; /* number of tins is small, so size of this struct doesn't matter much */
struct cake_sched_data {
struct cake_tin_data *tins;
struct cake_heap_entry overflow_heap[CAKE_QUEUES * CAKE_MAX_TINS];
u16 overflow_timeout;
u16 tin_cnt;
u8 tin_mode;
u8 flow_mode;
/* time_next = time_this + ((len * rate_ns) >> rate_shft) */
u16 rate_shft;
u64 time_next_packet;
u32 rate_ns;
u32 rate_bps;
u16 rate_flags;
s16 rate_overhead;
u16 rate_mpu;
u32 interval;
u32 target;
/* resource tracking */
u32 buffer_used;
u32 buffer_max_used;
u32 buffer_limit;
u32 buffer_config_limit;
/* indices for dequeue */
u16 cur_tin;
u16 cur_flow;
struct qdisc_watchdog watchdog;
const u8 *tin_index;
/* bandwidth capacity estimate */
u64 last_packet_time;
u64 avg_packet_interval;
u64 avg_window_begin;
u32 avg_window_bytes;
u32 avg_peak_bandwidth;
u64 last_reconfig_time;
};
enum {
CAKE_MODE_BESTEFFORT = 1,
CAKE_MODE_PRECEDENCE,
CAKE_MODE_DIFFSERV8,
CAKE_MODE_DIFFSERV4,
CAKE_MODE_LLT,
CAKE_MODE_DIFFSERV3,
CAKE_MODE_MAX
};
enum {
CAKE_FLAG_ATM = 0x0001,
CAKE_FLAG_PTM = 0x0002,
CAKE_FLAG_AUTORATE_INGRESS = 0x0010,
CAKE_FLAG_WASH = 0x0100
};
enum {
CAKE_FLOW_NONE = 0,
CAKE_FLOW_SRC_IP,
CAKE_FLOW_DST_IP,
CAKE_FLOW_HOSTS, /* = CAKE_FLOW_SRC_IP | CAKE_FLOW_DST_IP */
CAKE_FLOW_FLOWS,
CAKE_FLOW_DUAL_SRC, /* = CAKE_FLOW_SRC_IP | CAKE_FLOW_FLOWS */
CAKE_FLOW_DUAL_DST, /* = CAKE_FLOW_DST_IP | CAKE_FLOW_FLOWS */
CAKE_FLOW_TRIPLE, /* = CAKE_FLOW_HOSTS | CAKE_FLOW_FLOWS */
CAKE_FLOW_MAX,
CAKE_FLOW_NAT_FLAG = 64
};
static u16 quantum_div[CAKE_QUEUES+1] = {0};
/* Diffserv lookup tables */
static const u8 precedence[] = {0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7,
};
static const u8 diffserv_llt[] = {1, 0, 0, 1, 2, 2, 1, 1,
3, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 1, 2, 1,
4, 1, 1, 1, 1, 1, 1, 1,
4, 1, 1, 1, 1, 1, 1, 1,
};
static const u8 diffserv8[] = {2, 5, 1, 2, 4, 2, 2, 2,
0, 2, 1, 2, 1, 2, 1, 2,
5, 2, 4, 2, 4, 2, 4, 2,
3, 2, 3, 2, 3, 2, 3, 2,
6, 2, 3, 2, 3, 2, 3, 2,
6, 2, 2, 2, 6, 2, 6, 2,
7, 2, 2, 2, 2, 2, 2, 2,
7, 2, 2, 2, 2, 2, 2, 2,
};
static const u8 diffserv4[] = {1, 2, 1, 1, 2, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1,
2, 1, 2, 1, 2, 1, 2, 1,
2, 1, 2, 1, 2, 1, 2, 1,
3, 1, 2, 1, 2, 1, 2, 1,
3, 1, 1, 1, 3, 1, 3, 1,
3, 1, 1, 1, 1, 1, 1, 1,
3, 1, 1, 1, 1, 1, 1, 1,
};
static const u8 diffserv3[] = {1, 1, 1, 1, 2, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 1, 2, 1,
2, 1, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1, 1,
};
static const u8 besteffort[] = {0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
};
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
#if KERNEL_VERSION(4, 0, 0) > LINUX_VERSION_CODE
#define tc_skb_protocol(_skb) \
(vlan_tx_tag_present(_skb) ? _skb->vlan_proto : _skb->protocol)
#endif
static inline void cake_update_flowkeys(struct flow_keys *keys, const struct sk_buff *skb)
{
enum ip_conntrack_info ctinfo;
bool reverse = false;
struct nf_conn *ct;
const struct nf_conntrack_tuple *tuple;
if (tc_skb_protocol(skb) != htons(ETH_P_IP))
return;
ct = nf_ct_get(skb, &ctinfo);
if (ct != NULL) {
tuple = nf_ct_tuple(ct, CTINFO2DIR(ctinfo));
} else {
const struct nf_conntrack_tuple_hash *hash;
struct nf_conntrack_tuple srctuple;
#if KERNEL_VERSION(4, 4, 0) > LINUX_VERSION_CODE
if (! nf_ct_get_tuplepr(skb, skb_network_offset(skb),
NFPROTO_IPV4, &srctuple))
#else
if (! nf_ct_get_tuplepr(skb, skb_network_offset(skb),
NFPROTO_IPV4, dev_net(skb->dev), &srctuple))
#endif
return;
#if KERNEL_VERSION(4, 3, 0) > LINUX_VERSION_CODE
hash = nf_conntrack_find_get(dev_net(skb->dev),
NF_CT_DEFAULT_ZONE, &srctuple);
#else
hash = nf_conntrack_find_get(dev_net(skb->dev),
&nf_ct_zone_dflt, &srctuple);
#endif
if (hash == NULL)
return;
reverse = true;
ct = nf_ct_tuplehash_to_ctrack(hash);
tuple = nf_ct_tuple(ct, !hash->tuple.dst.dir);
}
#if KERNEL_VERSION(4, 2, 0) > LINUX_VERSION_CODE
keys->src = ( reverse ? tuple->dst.u3.ip : tuple->src.u3.ip );
keys->dst = ( reverse ? tuple->src.u3.ip : tuple->dst.u3.ip );
#else
keys->addrs.v4addrs.src = ( reverse ? tuple->dst.u3.ip : tuple->src.u3.ip );
keys->addrs.v4addrs.dst = ( reverse ? tuple->src.u3.ip : tuple->dst.u3.ip );
#endif
#if KERNEL_VERSION(4, 2, 0) > LINUX_VERSION_CODE
if (keys->ports) {
keys->port16[0] = ( reverse ? tuple->dst.u.all : tuple->src.u.all );
keys->port16[1] = ( reverse ? tuple->src.u.all : tuple->dst.u.all );
}
#else
if (keys->ports.ports) {
keys->ports.src = ( reverse ? tuple->dst.u.all : tuple->src.u.all );
keys->ports.dst = ( reverse ? tuple->src.u.all : tuple->dst.u.all );
}
#endif
if (reverse)
nf_ct_put(ct);
return;
}
#else
static inline void cake_update_flowkeys(struct flow_keys *keys, const struct sk_buff *skb)
{
/* There is nothing we can do here without CONNTRACK */
return;
}
#endif
static inline u32
cake_hash(struct cake_tin_data *q, const struct sk_buff *skb, int flow_mode)
{
#if KERNEL_VERSION(4, 2, 0) > LINUX_VERSION_CODE
struct flow_keys keys;
#else
struct flow_keys keys, host_keys;
#endif
u32 flow_hash=0, srchost_hash, dsthost_hash;
u16 reduced_hash, srchost_idx, dsthost_idx;
if (unlikely(flow_mode == CAKE_FLOW_NONE))
return 0;
#if KERNEL_VERSION(4, 2, 0) > LINUX_VERSION_CODE
skb_flow_dissect(skb, &keys);
if(flow_mode & CAKE_FLOW_NAT_FLAG)
cake_update_flowkeys(&keys, skb);
srchost_hash = jhash_1word(
(__force u32) keys.src, q->perturbation);
dsthost_hash = jhash_1word(
(__force u32) keys.dst, q->perturbation);
if (flow_mode & CAKE_FLOW_FLOWS) {
flow_hash = jhash_3words(
(__force u32)keys.dst,
(__force u32)keys.src ^ keys.ip_proto,
(__force u32)keys.ports, q->perturbation);
}
#else
/* Linux kernel 4.2.x have skb_flow_dissect_flow_keys which takes only 2
* arguments
*/
#if (KERNEL_VERSION(4, 2, 0) <= LINUX_VERSION_CODE) && (KERNEL_VERSION(4, 3, 0) > LINUX_VERSION_CODE)
skb_flow_dissect_flow_keys(skb, &keys);
#else
skb_flow_dissect_flow_keys(skb, &keys,
FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
#endif
if(flow_mode & CAKE_FLOW_NAT_FLAG)
cake_update_flowkeys(&keys, skb);
/* flow_hash_from_keys() sorts the addresses by value, so we have
* to preserve their order in a separate data structure to treat
* src and dst host addresses as independently selectable.
*/
host_keys = keys;
host_keys.ports.ports = 0;
host_keys.basic.ip_proto = 0;
host_keys.keyid.keyid = 0;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
host_keys.tags.vlan_id = 0;
#endif
host_keys.tags.flow_label = 0;
switch (host_keys.control.addr_type) {
case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
host_keys.addrs.v4addrs.src = 0;
dsthost_hash = flow_hash_from_keys(&host_keys);
host_keys.addrs.v4addrs.src = keys.addrs.v4addrs.src;
host_keys.addrs.v4addrs.dst = 0;
srchost_hash = flow_hash_from_keys(&host_keys);
break;
case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
memset(&host_keys.addrs.v6addrs.src, 0,
sizeof(host_keys.addrs.v6addrs.src));
dsthost_hash = flow_hash_from_keys(&host_keys);
host_keys.addrs.v6addrs.src = keys.addrs.v6addrs.src;
memset(&host_keys.addrs.v6addrs.dst, 0,
sizeof(host_keys.addrs.v6addrs.dst));
srchost_hash = flow_hash_from_keys(&host_keys);
break;
default:
dsthost_hash = srchost_hash = 0;
};
/* This *must* be after the above switch, since as a
* side-effect it sorts the src and dst addresses.
*/
if (flow_mode & CAKE_FLOW_FLOWS)
flow_hash = flow_hash_from_keys(&keys);
#endif
if (!(flow_mode & CAKE_FLOW_FLOWS)) {
if(flow_mode & CAKE_FLOW_SRC_IP)
flow_hash ^= srchost_hash;
if(flow_mode & CAKE_FLOW_DST_IP)
flow_hash ^= dsthost_hash;
}
reduced_hash = flow_hash % CAKE_QUEUES;
srchost_idx = srchost_hash % CAKE_QUEUES;
dsthost_idx = dsthost_hash % CAKE_QUEUES;
/* set-associative hashing */
/* fast path if no hash collision (direct lookup succeeds) */
if (likely(q->tags[reduced_hash] == flow_hash && q->flows[reduced_hash].set)) {
q->way_directs++;
} else {
u32 inner_hash = reduced_hash % CAKE_SET_WAYS;
u32 outer_hash = reduced_hash - inner_hash;
u32 i, k;
bool need_allocate_src = false;
bool need_allocate_dst = false;
/* check if any active queue in the set is reserved for
* this flow.
*/
for (i = 0, k = inner_hash; i < CAKE_SET_WAYS;
i++, k = (k + 1) % CAKE_SET_WAYS) {
if (q->tags[outer_hash + k] == flow_hash) {
if(i)
q->way_hits++;
if(!q->flows[outer_hash + k].set) {
/* need to increment host refcnts */
need_allocate_src = true;
need_allocate_dst = true;
}
goto found;
}
}
/* no queue is reserved for this flow, look for an
* empty one.
*/
for (i = 0; i < CAKE_SET_WAYS;
i++, k = (k + 1) % CAKE_SET_WAYS) {
if (!q->flows[outer_hash + k].set) {
q->way_misses++;
need_allocate_src = true;
need_allocate_dst = true;
goto found;
}
}
/* With no empty queues, default to the original
* queue, accept the collision, update the host tags.
*/
q->way_collisions++;
q->hosts[q->flows[reduced_hash].srchost].srchost_refcnt--;
q->hosts[q->flows[reduced_hash].dsthost].dsthost_refcnt--;
need_allocate_src = true;
need_allocate_dst = true;
found:
/* reserve queue for future packets in same flow */
reduced_hash = outer_hash + k;
q->tags[reduced_hash] = flow_hash;
if(need_allocate_src) {
inner_hash = srchost_idx % CAKE_SET_WAYS;
outer_hash = srchost_idx - inner_hash;
for(i = 0, k = inner_hash; i < CAKE_SET_WAYS;
i++, k = (k + 1) % CAKE_SET_WAYS) {
if(q->hosts[outer_hash + k].srchost_tag == srchost_hash)
goto found_src;
}
for(i = 0; i < CAKE_SET_WAYS;
i++, k = (k + 1) % CAKE_SET_WAYS) {
if(!q->hosts[outer_hash + k].srchost_refcnt)
break;
}
q->hosts[outer_hash + k].srchost_tag = srchost_hash;
found_src:
srchost_idx = outer_hash + k;
q->hosts[srchost_idx].srchost_refcnt++;
q->flows[reduced_hash].srchost = srchost_idx;
}
if(need_allocate_dst) {
inner_hash = dsthost_idx % CAKE_SET_WAYS;
outer_hash = dsthost_idx - inner_hash;
for(i = 0, k = inner_hash; i < CAKE_SET_WAYS;
i++, k = (k + 1) % CAKE_SET_WAYS) {
if(q->hosts[outer_hash + k].dsthost_tag == dsthost_hash)
goto found_dst;
}
for(i = 0; i < CAKE_SET_WAYS;
i++, k = (k + 1) % CAKE_SET_WAYS) {
if(!q->hosts[outer_hash + k].dsthost_refcnt)
break;
}
q->hosts[outer_hash + k].dsthost_tag = dsthost_hash;
found_dst:
dsthost_idx = outer_hash + k;
q->hosts[dsthost_idx].dsthost_refcnt++;
q->flows[reduced_hash].dsthost = dsthost_idx;
}
}
return reduced_hash;
}
/* helper functions : might be changed when/if skb use a standard list_head */
/* remove one skb from head of slot queue */
static inline struct sk_buff *dequeue_head(struct cake_flow *flow)
{
struct sk_buff *skb = flow->head;
if(skb) {
flow->head = skb->next;
skb->next = NULL;
}
return skb;
}
/* add skb to flow queue (tail add) */
static inline void
flow_queue_add(struct cake_flow *flow, struct sk_buff *skb)
{
if (!flow->head)
flow->head = skb;
else
flow->tail->next = skb;
flow->tail = skb;
skb->next = NULL;
}
static inline u32 cake_overhead(struct cake_sched_data *q, u32 in)
{
u32 out = in + q->rate_overhead;
if (q->rate_mpu && out < q->rate_mpu) {
out = q->rate_mpu;
}
if (q->rate_flags & CAKE_FLAG_ATM) {
out += 47;
out /= 48;
out *= 53;
} else if(q->rate_flags & CAKE_FLAG_PTM) {
// the following adds one byte per 64 bytes or part thereof
// this is conservative and easier to calculate than the precise value
out += (out / 64) + !!(out % 64);
}
return out;
}
static inline cobalt_time_t cake_ewma(cobalt_time_t avg, cobalt_time_t sample,
u32 shift)
{
avg -= avg >> shift;
avg += sample >> shift;
return avg;
}
static inline void cake_heap_swap(struct cake_sched_data *q, u16 i, u16 j)
{
struct cake_heap_entry ii = q->overflow_heap[i];
struct cake_heap_entry jj = q->overflow_heap[j];
q->overflow_heap[i] = jj;
q->overflow_heap[j] = ii;
q->tins[ii.t].overflow_idx[ii.b] = j;
q->tins[jj.t].overflow_idx[jj.b] = i;
}
static inline u32 cake_heap_get_backlog(const struct cake_sched_data *q, u16 i)
{
struct cake_heap_entry ii = q->overflow_heap[i];
return q->tins[ii.t].backlogs[ii.b];
}
static void cake_heapify(struct cake_sched_data *q, u16 i)
{
static const u32 a = CAKE_MAX_TINS * CAKE_QUEUES;
u32 m = i;
u32 mb = cake_heap_get_backlog(q,m);
while(m < a) {
u32 l = m+m+1;
u32 r = l+1;
if(l < a) {
u32 lb = cake_heap_get_backlog(q,l);
if(lb > mb) {
m = l;
mb = lb;
}
}
if(r < a) {
u32 rb = cake_heap_get_backlog(q,r);
if(rb > mb) {
m = r;
mb = rb;
}
}
if(m != i) {
cake_heap_swap(q,i,m);
i = m;
} else {
break;
}
}
}
static void cake_heapify_up(struct cake_sched_data *q, u16 i)
{
while(i > 0 && i < CAKE_MAX_TINS * CAKE_QUEUES) {
u16 p = (i-1) >> 1;
u32 ib = cake_heap_get_backlog(q,i);
u32 pb = cake_heap_get_backlog(q,p);
if(ib > pb) {
cake_heap_swap(q,i,p);
i = p;
} else {
break;
}
}
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
static unsigned int cake_drop(struct Qdisc *sch)
#else
static unsigned int cake_drop(struct Qdisc *sch, struct sk_buff **to_free)
#endif
{
struct cake_sched_data *q = qdisc_priv(sch);
struct sk_buff *skb;
u32 idx = 0, tin = 0, len;
struct cake_tin_data *b;
struct cake_flow *flow;
struct cake_heap_entry qq;
if(!q->overflow_timeout) {
int i;
/* Build fresh max-heap */
for(i = CAKE_MAX_TINS * CAKE_QUEUES / 2; i >= 0; i--)
cake_heapify(q,i);
}
q->overflow_timeout = 65535;
/* select longest queue for pruning */
qq = q->overflow_heap[0];
tin = qq.t;
idx = qq.b;
b = &q->tins[tin];
flow = &b->flows[idx];
skb = dequeue_head(flow);
if(unlikely(!skb)) {
/* heap has gone wrong, rebuild it next time */
q->overflow_timeout = 0;
return idx + (tin << 16);
}
if(cobalt_queue_full(&flow->cvars, &b->cparams, cobalt_get_time()))
b->unresponsive_flow_count++;
len = qdisc_pkt_len(skb);
q->buffer_used -= skb->truesize;
b->backlogs[idx] -= len;
b->tin_backlog -= len;
sch->qstats.backlog -= len;
qdisc_tree_reduce_backlog(sch, 1, len);
b->tin_dropped++;
sch->qstats.drops++;
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
kfree_skb(skb);
#else
__qdisc_drop(skb, to_free);
#endif
sch->q.qlen--;
cake_heapify(q,0);
return idx + (tin << 16);
}
static inline void cake_wash_diffserv(struct sk_buff *skb)
{
switch (skb->protocol) {
case htons(ETH_P_IP):
ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, 0);
break;
case htons(ETH_P_IPV6):
ipv6_change_dsfield(ipv6_hdr(skb), INET_ECN_MASK, 0);
break;
default:
break;
};
}
static inline u8 cake_handle_diffserv(struct sk_buff *skb, u16 wash)
{
u8 dscp;
switch (skb->protocol) {
case htons(ETH_P_IP):
dscp = ipv4_get_dsfield(ip_hdr(skb)) >> 2;
if (wash && dscp)
ipv4_change_dsfield(ip_hdr(skb), INET_ECN_MASK, 0);
return dscp;
case htons(ETH_P_IPV6):
dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> 2;
if (wash && dscp)
ipv6_change_dsfield(ipv6_hdr(skb), INET_ECN_MASK, 0);
return dscp;
case htons(ETH_P_ARP):
return 0x38; // CS7 - Net Control
default:
/* If there is no Diffserv field, treat as best-effort */
return 0;
};
}
static void cake_reconfigure(struct Qdisc *sch);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch)
#else
static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch, struct sk_buff **to_free)
#endif
{
struct cake_sched_data *q = qdisc_priv(sch);
u32 idx, tin;
struct cake_tin_data *b;
struct cake_flow *flow;
u32 len = qdisc_pkt_len(skb);
u64 now = cobalt_get_time();
/* extract the Diffserv Precedence field, if it exists */
/* and clear DSCP bits if washing */
if (q->tin_mode != CAKE_MODE_BESTEFFORT) {
tin = q->tin_index[cake_handle_diffserv(skb,
q->rate_flags & CAKE_FLAG_WASH)];
if (unlikely(tin >= q->tin_cnt))
tin = 0;
} else {
tin = 0;
if (q->rate_flags & CAKE_FLAG_WASH)
cake_wash_diffserv(skb);
}
b = &q->tins[tin];
/* choose flow to insert into */
idx = cake_hash(b, skb, q->flow_mode);
flow = &b->flows[idx];
/* ensure shaper state isn't stale */
if (!b->tin_backlog) {
if (b->tin_time_next_packet < now)
b->tin_time_next_packet = now;
if (!sch->q.qlen)
if (q->time_next_packet < now)
q->time_next_packet = now;
}
if (unlikely(len > b->max_skblen))
b->max_skblen = len;
/* Split GSO aggregates if they're likely to impair flow isolation
* or if we need to know individual packet sizes for framing overhead.
*/
if (skb_is_gso(skb)) {
struct sk_buff *segs, *nskb;
netdev_features_t features = netif_skb_features(skb);
u32 slen = 0;
segs = skb_gso_segment(skb, features & ~NETIF_F_GSO_MASK);
if (IS_ERR_OR_NULL(segs))
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 8, 0)
return qdisc_reshape_fail(skb, sch);
#else
return qdisc_drop(skb, sch, to_free);
#endif
while (segs) {
nskb = segs->next;
segs->next = NULL;
qdisc_skb_cb(segs)->pkt_len = segs->len;
cobalt_set_enqueue_time(segs, now);
flow_queue_add(flow, segs);
/* stats */
sch->q.qlen++;
b->packets++;
slen += segs->len;
q->buffer_used += segs->truesize;
segs = nskb;
}
b->bytes += slen;
b->backlogs[idx] += slen;
b->tin_backlog += slen;
sch->qstats.backlog += slen;
q->avg_window_bytes += slen;
qdisc_tree_reduce_backlog(sch, 1, len);
consume_skb(skb);
} else {
/* not splitting */
cobalt_set_enqueue_time(skb, now);
flow_queue_add(flow, skb);
/* stats */
sch->q.qlen++;
b->packets++;
b->bytes += len;
b->backlogs[idx] += len;
b->tin_backlog += len;
sch->qstats.backlog += len;
q->avg_window_bytes += len;
q->buffer_used += skb->truesize;
}
if(q->overflow_timeout)
cake_heapify_up(q, b->overflow_idx[idx]);
/* incoming bandwidth capacity estimate */
if (q->rate_flags & CAKE_FLAG_AUTORATE_INGRESS)
{
u64 packet_interval = now - q->last_packet_time;
if (packet_interval > NSEC_PER_SEC)
packet_interval = NSEC_PER_SEC;
/* filter out short-term bursts, eg. wifi aggregation */
q->avg_packet_interval = cake_ewma(q->avg_packet_interval,
packet_interval,
packet_interval > q->avg_packet_interval ? 2 : 8);
q->last_packet_time = now;
if (packet_interval > q->avg_packet_interval) {
u64 window_interval = now - q->avg_window_begin;
u64 b = q->avg_window_bytes * (u64) NSEC_PER_SEC;
do_div(b, window_interval);
q->avg_peak_bandwidth =
cake_ewma(q->avg_peak_bandwidth, b,
b > q->avg_peak_bandwidth ? 2 : 8);
q->avg_window_bytes = 0;
q->avg_window_begin = now;
if (q->rate_flags & CAKE_FLAG_AUTORATE_INGRESS &&
now - q->last_reconfig_time >
(NSEC_PER_SEC / 4)) {
q->rate_bps = (q->avg_peak_bandwidth * 15) >> 4;
cake_reconfigure(sch);