/
sch_cake.c
1394 lines (1160 loc) · 34.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-2015 Jonathan Morton <chromatix99@gmail.com>
*
* 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"
#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
#include <net/flow_keys.h>
#else
#include <net/flow_dissector.h>
#endif
#include "codel5.h"
/* 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_MAX_TINS (8)
#ifndef CAKE_VERSION
#define CAKE_VERSION "unknown"
#endif
static char *cake_version __attribute__((used)) = "Cake version: "
CAKE_VERSION;
struct cake_flow {
struct sk_buff *head;
struct sk_buff *tail;
struct list_head flowchain;
s32 deficit;
u32 dropped; /* Drops (or ECN marks) on this flow */
struct codel_vars cvars;
}; /* please try to keep this structure <= 64 bytes */
struct cake_tin_data {
struct cake_flow *flows;/* Flows table [flows_cnt] */
u32 *backlogs; /* backlog table [flows_cnt] */
u32 flows_cnt; /* number of flows - must be multiple of
* CAKE_SET_WAYS
*/
u32 perturbation;/* hash perturbation */
u16 quantum; /* psched_mtu(qdisc_dev(sch)); */
u16 bulk_flow_count;
u32 drop_overlimit;
struct list_head new_flows; /* list of new flows */
struct list_head old_flows; /* list of old 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;
}; /* number of tins is small, so size of this struct doesn't matter much */
struct cake_sched_data {
struct cake_tin_data *tins;
struct codel_params cparams;
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;
u32 interval;
u32 target;
/* resource tracking */
u32 buffer_used;
u32 buffer_limit;
u32 buffer_config_limit;
/* indices for dequeue */
u16 cur_tin;
u16 cur_flow;
struct qdisc_watchdog watchdog;
u8 tin_index[64];
};
enum {
CAKE_MODE_BESTEFFORT = 1,
CAKE_MODE_PRECEDENCE,
CAKE_MODE_DIFFSERV8,
CAKE_MODE_DIFFSERV4,
CAKE_MODE_MAX
};
enum {
CAKE_FLAG_ATM = 0x0001,
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_DUAL, /* = CAKE_FLOW_HOSTS | CAKE_FLOW_FLOWS */
CAKE_FLOW_MAX
};
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, reduced_hash;
if (unlikely(flow_mode == CAKE_FLOW_NONE))
return 0;
#if KERNEL_VERSION(4, 2, 0) > LINUX_VERSION_CODE
skb_flow_dissect(skb, &keys);
flow_hash = jhash_3words(
(__force u32)((flow_mode & CAKE_FLOW_DST_IP) ? keys.dst : 0),
(__force u32)((flow_mode & CAKE_FLOW_SRC_IP) ? keys.src : 0),
(__force u32)0, 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
flow_hash = flow_hash_from_keys(&keys);
#endif
reduced_hash = reciprocal_scale(flow_hash, q->flows_cnt);
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;
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_flags & CAKE_FLAG_ATM) {
out += 47;
out /= 48;
out *= 53;
}
return out;
}
static inline codel_time_t cake_ewma(codel_time_t avg, codel_time_t sample,
u32 shift)
{
avg -= avg >> shift;
avg += sample >> shift;
return avg;
}
/* FIXME: In terms of speed this is a real hit and could be easily
* replaced with tail drop... BUT it's a slow-path routine.
*/
static unsigned int cake_drop(struct Qdisc *sch)
{
struct cake_sched_data *q = qdisc_priv(sch);
struct sk_buff *skb;
u32 maxbacklog = 0, idx = 0, tin = 0, i, j, len;
struct cake_tin_data *b;
struct cake_flow *flow;
/* Queue is full; check across tins in use and
* find the fat flow and drop a packet.
*/
for (j = 0; j < q->tin_cnt; j++) {
b = &q->tins[j];
list_for_each_entry(flow, &b->old_flows, flowchain) {
i = flow - b->flows;
if (b->backlogs[i] > maxbacklog) {
maxbacklog = b->backlogs[i];
idx = i;
tin = j;
}
}
list_for_each_entry(flow, &b->new_flows, flowchain) {
i = flow - b->flows;
if (b->backlogs[i] > maxbacklog) {
maxbacklog = b->backlogs[i];
idx = i;
tin = j;
}
}
}
b = &q->tins[tin];
flow = &b->flows[idx];
skb = dequeue_head(flow);
len = qdisc_pkt_len(skb);
q->buffer_used -= skb->truesize;
b->backlogs[idx] -= len;
b->tin_backlog -= len;
sch->qstats.backlog -= len;
b->tin_dropped++;
sch->qstats.drops++;
flow->dropped++;
kfree_skb(skb);
sch->q.qlen--;
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 u32 cake_handle_diffserv(struct sk_buff *skb, u16 wash)
{
u32 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;
default:
/* If there is no Diffserv field, treat as bulk */
return 0;
};
}
static void cake_reconfigure(struct Qdisc *sch);
static s32 cake_enqueue(struct sk_buff *skb, struct Qdisc *sch)
{
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 = codel_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;
}
/* Split GSO aggregates if they're likely to impair flow isolation
* or if we need to know individual packet sizes for framing overhead.
*/
if (unlikely(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))
return qdisc_reshape_fail(skb, sch);
while (segs) {
nskb = segs->next;
segs->next = NULL;
qdisc_skb_cb(segs)->pkt_len = segs->len;
get_codel_cb(segs)->enqueue_time = 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;
qdisc_tree_decrease_qlen(sch, 1);
consume_skb(skb);
} else {
/* not splitting */
get_codel_cb(skb)->enqueue_time = 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->buffer_used += skb->truesize;
}
/* flowchain */
if (list_empty(&flow->flowchain)) {
list_add_tail(&flow->flowchain, &b->new_flows);
flow->deficit = b->quantum;
flow->dropped = 0;
}
if (q->buffer_used > q->buffer_limit) {
u32 dropped = 0;
while (q->buffer_used > q->buffer_limit) {
dropped++;
cake_drop(sch);
}
b->drop_overlimit += dropped;
qdisc_tree_decrease_qlen(sch, dropped);
}
return NET_XMIT_SUCCESS;
}
/* Callback from codel_dequeue(); sch->qstats.backlog is already handled. */
static struct sk_buff *custom_dequeue(struct codel_vars *vars,
struct Qdisc *sch)
{
struct cake_sched_data *q = qdisc_priv(sch);
struct cake_tin_data *b = &q->tins[q->cur_tin];
struct cake_flow *flow = &b->flows[q->cur_flow];
struct sk_buff *skb = NULL;
u32 len;
/* WARN_ON(flow != container_of(vars, struct cake_flow, cvars)); */
if (flow->head) {
skb = dequeue_head(flow);
len = qdisc_pkt_len(skb);
b->backlogs[q->cur_flow] -= len;
b->tin_backlog -= len;
q->buffer_used -= skb->truesize;
sch->q.qlen--;
}
return skb;
}
/* Discard leftover packets from a tin no longer in use. */
static void cake_clear_tin(struct Qdisc *sch, u16 tin)
{
struct cake_sched_data *q = qdisc_priv(sch);
struct cake_tin_data *b = &q->tins[tin];
q->cur_tin = tin;
for (q->cur_flow = 0; q->cur_flow < b->flows_cnt; q->cur_flow++)
while (custom_dequeue(NULL, sch))
;
}
static struct sk_buff *cake_dequeue(struct Qdisc *sch)
{
struct cake_sched_data *q = qdisc_priv(sch);
struct sk_buff *skb;
struct cake_tin_data *b = &q->tins[q->cur_tin];
struct cake_flow *flow;
struct list_head *head;
u16 prev_drop_count, prev_ecn_mark;
u32 len;
codel_time_t now = ktime_get_ns();
s32 i;
begin:
if (!sch->q.qlen)
return NULL;
/* global hard shaper */
if (q->time_next_packet > now) {
sch->qstats.overlimits++;
codel_watchdog_schedule_ns(&q->watchdog, q->time_next_packet,
true);
return NULL;
}
/* Choose a class to work on. */
while (!b->tin_backlog || b->tin_deficit <= 0) {
/* this is the priority soft-shaper magic */
if (b->tin_deficit <= 0)
b->tin_deficit +=
b->tin_time_next_packet > now ?
b->tin_quantum_band :
b->tin_quantum_prio;
q->cur_tin++;
b++;
if (q->cur_tin >= q->tin_cnt) {
q->cur_tin = 0;
b = q->tins;
}
}
retry:
/* service this class */
head = &b->new_flows;
if (list_empty(head)) {
head = &b->old_flows;
if (unlikely(list_empty(head))) {
/* shouldn't ever happen */
WARN_ON(b->tin_backlog);
b->tin_backlog = 0;
goto begin;
}
}
flow = list_first_entry(head, struct cake_flow, flowchain);
q->cur_flow = flow - b->flows;
if (flow->deficit <= 0) {
flow->deficit += b->quantum;
list_move_tail(&flow->flowchain, &b->old_flows);
if (head == &b->new_flows) {
b->bulk_flow_count++;
}
goto retry;
}
prev_drop_count = flow->cvars.drop_count;
prev_ecn_mark = flow->cvars.ecn_mark;
skb = codel_dequeue(sch, &flow->cvars, &q->cparams, now,
q->buffer_used >
(q->buffer_limit >> 2) + (q->buffer_limit >> 1));
b->tin_dropped += flow->cvars.drop_count - prev_drop_count;
b->tin_ecn_mark += flow->cvars.ecn_mark - prev_ecn_mark;
flow->cvars.ecn_mark = 0;
flow->dropped += flow->cvars.drop_count - prev_drop_count;
if (!skb) {
/* codel dropped the last packet in this queue; try again */
if ((head == &b->new_flows) &&
!list_empty(&b->old_flows)) {
list_move_tail(&flow->flowchain, &b->old_flows);
b->bulk_flow_count++;
} else {
list_del_init(&flow->flowchain);
if (!(head == &b->new_flows))
b->bulk_flow_count--;
}
goto begin;
}
qdisc_bstats_update(sch, skb);
if (flow->cvars.drop_count && sch->q.qlen) {
qdisc_tree_decrease_qlen(sch, flow->cvars.drop_count);
flow->cvars.drop_count = 0;
}
len = cake_overhead(q, qdisc_pkt_len(skb));
flow->deficit -= len;
b->tin_deficit -= len;
/* charge packet bandwidth to this and all lower tins, and
* to the global shaper.
*/
for (i = q->cur_tin; i >= 0; i--, b--)
b->tin_time_next_packet +=
(len * (u64)b->tin_rate_ns) >> b->tin_rate_shft;
q->time_next_packet += (len * (u64)q->rate_ns) >> q->rate_shft;
return skb;
}
static void cake_reset(struct Qdisc *sch)
{
u32 c;
for (c = 0; c < CAKE_MAX_TINS; c++)
cake_clear_tin(sch, c);
}
static const struct nla_policy cake_policy[TCA_CAKE_MAX + 1] = {
[TCA_CAKE_BASE_RATE] = { .type = NLA_U32 },
[TCA_CAKE_DIFFSERV_MODE] = { .type = NLA_U32 },
[TCA_CAKE_ATM] = { .type = NLA_U32 },
[TCA_CAKE_FLOW_MODE] = { .type = NLA_U32 },
[TCA_CAKE_OVERHEAD] = { .type = NLA_S32 },
[TCA_CAKE_RTT] = { .type = NLA_U32 },
[TCA_CAKE_TARGET] = { .type = NLA_U32 },
[TCA_CAKE_MEMORY] = { .type = NLA_U32 },
[TCA_CAKE_WASH] = { .type = NLA_U32 },
};
static void cake_set_rate(struct cake_tin_data *b, u64 rate)
{
/* convert byte-rate into time-per-byte
* so it will always unwedge in reasonable time.
*/
static const u64 MIN_RATE = 64;
u64 rate_ns = 0;
u8 rate_shft = 0;
if (rate) {
rate_shft = 32;
rate_ns = ((u64) NSEC_PER_SEC) << rate_shft;
do_div(rate_ns, max(MIN_RATE, rate));
while (!!(rate_ns >> 32)) {
rate_ns >>= 1;
rate_shft--;
}
} /* else unlimited, ie. zero delay */
b->tin_rate_bps = rate;
b->tin_rate_ns = rate_ns;
b->tin_rate_shft = rate_shft;
b->quantum = max(min(rate >> 12, 1514ULL), 300ULL);
}
static void cake_config_besteffort(struct Qdisc *sch)
{
struct cake_sched_data *q = qdisc_priv(sch);
struct cake_tin_data *b = &q->tins[0];
u64 rate = q->rate_bps;
u32 i;
q->tin_cnt = 1;
for (i = 0; i < 64; i++)
q->tin_index[i] = 0;
cake_set_rate(b, rate);
b->tin_quantum_band = 65535;
b->tin_quantum_prio = 65535;
}
static void cake_config_precedence(struct Qdisc *sch)
{
/* convert high-level (user visible) parameters into internal format */
struct cake_sched_data *q = qdisc_priv(sch);
u64 rate = q->rate_bps;
u32 quantum1 = 256;
u32 quantum2 = 256;
u32 i;
q->tin_cnt = 8;
for (i = 0; i < 64; i++)
q->tin_index[i] = min((u32)(i >> 3), (u32)(q->tin_cnt));
for (i = 0; i < q->tin_cnt; i++) {
struct cake_tin_data *b = &q->tins[i];
cake_set_rate(b, rate);
b->tin_quantum_prio = max_t(u16, 1U, quantum1);
b->tin_quantum_band = max_t(u16, 1U, quantum2);
/* calculate next class's parameters */
rate *= 7;
rate >>= 3;
quantum1 *= 3;
quantum1 >>= 1;
quantum2 *= 7;
quantum2 >>= 3;
}
}
/* List of known Diffserv codepoints:
*
* Least Effort (CS1)
* Best Effort (CS0)
* Max Reliability (TOS1)
* Max Throughput (TOS2)
* Min Delay (TOS4)
* Assured Forwarding 1 (AF1x) - x3
* Assured Forwarding 2 (AF2x) - x3
* Assured Forwarding 3 (AF3x) - x3
* Assured Forwarding 4 (AF4x) - x3
* Precedence Class 2 (CS2)
* Precedence Class 3 (CS3)
* Precedence Class 4 (CS4)
* Precedence Class 5 (CS5)
* Precedence Class 6 (CS6)
* Precedence Class 7 (CS7)
* Voice Admit (VA)
* Expedited Forwarding (EF)
* Total 25 codepoints.
*/
/* List of traffic classes in RFC 4594:
* (roughly descending order of contended priority)
* (roughly ascending order of uncontended throughput)
*
* Network Control (CS6,CS7) - routing traffic
* Telephony (EF,VA) - aka. VoIP streams
* Signalling (CS5) - VoIP setup
* Multimedia Conferencing (AF4x) - aka. video calls
* Realtime Interactive (CS4) - eg. games
* Multimedia Streaming (AF3x) - eg. YouTube, NetFlix, Twitch
* Broadcast Video (CS3)
* Low Latency Data (AF2x,TOS4) - eg. database
* Ops, Admin, Management (CS2,TOS1) - eg. ssh
* Standard Service (CS0 & unrecognised codepoints)
* High Throughput Data (AF1x,TOS2) - eg. web traffic
* Low Priority Data (CS1) - eg. BitTorrent
* Total 12 traffic classes.
*/
static void cake_config_diffserv8(struct Qdisc *sch)
{
/* Pruned list of traffic classes for typical applications:
*
* Network Control (CS6, CS7)
* Minimum Latency (EF, VA, CS5, CS4)
* Interactive Shell (CS2, TOS1)
* Low Latency Transactions (AF2x, TOS4)
* Video Streaming (AF4x, AF3x, CS3)
* Bog Standard (CS0 etc.)
* High Throughput (AF1x, TOS2)
* Background Traffic (CS1)
*
* Total 8 traffic classes.
*/
struct cake_sched_data *q = qdisc_priv(sch);
u64 rate = q->rate_bps;
u32 quantum1 = 256;
u32 quantum2 = 256;
u32 i;
q->tin_cnt = 8;
/* codepoint to class mapping */
for (i = 0; i < 64; i++)
q->tin_index[i] = 2; /* default to best-effort */
q->tin_index[0x08] = 0; /* CS1 */
q->tin_index[0x02] = 1; /* TOS2 */
q->tin_index[0x18] = 3; /* CS3 */
q->tin_index[0x04] = 4; /* TOS4 */
q->tin_index[0x01] = 5; /* TOS1 */
q->tin_index[0x10] = 5; /* CS2 */
q->tin_index[0x20] = 6; /* CS4 */
q->tin_index[0x28] = 6; /* CS5 */
q->tin_index[0x2c] = 6; /* VA */
q->tin_index[0x2e] = 6; /* EF */
q->tin_index[0x30] = 7; /* CS6 */
q->tin_index[0x38] = 7; /* CS7 */
for (i = 2; i <= 6; i += 2) {
q->tin_index[0x08 + i] = 1; /* AF1x */
q->tin_index[0x10 + i] = 4; /* AF2x */
q->tin_index[0x18 + i] = 3; /* AF3x */
q->tin_index[0x20 + i] = 3; /* AF4x */
}
/* class characteristics */
for (i = 0; i < q->tin_cnt; i++) {
struct cake_tin_data *b = &q->tins[i];
cake_set_rate(b, rate);
b->tin_quantum_prio = max_t(u16, 1U, quantum1);
b->tin_quantum_band = max_t(u16, 1U, quantum2);
/* calculate next class's parameters */
rate *= 7;
rate >>= 3;
quantum1 *= 3;
quantum1 >>= 1;
quantum2 *= 7;
quantum2 >>= 3;
}
}
static void cake_config_diffserv4(struct Qdisc *sch)
{
/* Further pruned list of traffic classes for four-class system:
*
* Latency Sensitive (CS7, CS6, EF, VA, CS5, CS4)
* Streaming Media (AF4x, AF3x, CS3, AF2x, TOS4, CS2, TOS1)
* Best Effort (CS0, AF1x, TOS2, and those not specified)
* Background Traffic (CS1)
*
* Total 4 traffic classes.
*/
struct cake_sched_data *q = qdisc_priv(sch);
u64 rate = q->rate_bps;
u32 quantum = 256;
u32 i;
q->tin_cnt = 4;
/* codepoint to class mapping */
for (i = 0; i < 64; i++)
q->tin_index[i] = 1; /* default to best-effort */
q->tin_index[0x08] = 0; /* CS1 */
q->tin_index[0x18] = 2; /* CS3 */
q->tin_index[0x04] = 2; /* TOS4 */
q->tin_index[0x01] = 2; /* TOS1 */
q->tin_index[0x10] = 2; /* CS2 */
q->tin_index[0x20] = 3; /* CS4 */
q->tin_index[0x28] = 3; /* CS5 */
q->tin_index[0x2c] = 3; /* VA */
q->tin_index[0x2e] = 3; /* EF */
q->tin_index[0x30] = 3; /* CS6 */
q->tin_index[0x38] = 3; /* CS7 */
for (i = 2; i <= 6; i += 2) {
q->tin_index[0x10 + i] = 2; /* AF2x */
q->tin_index[0x18 + i] = 2; /* AF3x */
q->tin_index[0x20 + i] = 2; /* AF4x */
}
/* class characteristics */
cake_set_rate(&q->tins[0], rate);
cake_set_rate(&q->tins[1], rate - (rate >> 4));
cake_set_rate(&q->tins[2], rate - (rate >> 2));
cake_set_rate(&q->tins[3], rate >> 2);
/* priority weights */
q->tins[0].tin_quantum_prio = quantum >> 4;
q->tins[1].tin_quantum_prio = quantum;
q->tins[2].tin_quantum_prio = quantum << 2;
q->tins[3].tin_quantum_prio = quantum << 4;
/* bandwidth-sharing weights */
q->tins[0].tin_quantum_band = (quantum >> 4);
q->tins[1].tin_quantum_band = (quantum >> 3) + (quantum >> 4);
q->tins[2].tin_quantum_band = (quantum >> 1);
q->tins[3].tin_quantum_band = (quantum >> 2);
}
static void cake_reconfigure(struct Qdisc *sch)
{
struct cake_sched_data *q = qdisc_priv(sch);
int c;
switch (q->tin_mode) {
case CAKE_MODE_BESTEFFORT:
default:
cake_config_besteffort(sch);
break;
case CAKE_MODE_PRECEDENCE:
cake_config_precedence(sch);
break;
case CAKE_MODE_DIFFSERV8:
cake_config_diffserv8(sch);
break;
case CAKE_MODE_DIFFSERV4:
cake_config_diffserv4(sch);
break;
};
BUG_ON(q->tin_cnt > CAKE_MAX_TINS);
for (c = q->tin_cnt; c < CAKE_MAX_TINS; c++)
cake_clear_tin(sch, c);
q->rate_ns = q->tins[0].tin_rate_ns;
q->rate_shft = q->tins[0].tin_rate_shft;
if (q->buffer_config_limit) {
q->buffer_limit = q->buffer_config_limit;
} else if (q->rate_bps) {
u64 t = (u64) q->rate_bps * q->interval;
do_div(t, USEC_PER_SEC / 4);
q->buffer_limit = max_t(u32, t, 65536U);
} else {
q->buffer_limit = ~0;
}
q->cparams.target = max_t(u64,US2TIME(q->target),0);
q->cparams.interval = US2TIME(q->interval);
if (q->rate_bps)
sch->flags &= ~TCQ_F_CAN_BYPASS;
else
sch->flags |= TCQ_F_CAN_BYPASS;
q->buffer_limit = min(q->buffer_limit,
max(sch->limit * psched_mtu(qdisc_dev(sch)),
q->buffer_config_limit));
}
static int cake_change(struct Qdisc *sch, struct nlattr *opt)
{
struct cake_sched_data *q = qdisc_priv(sch);
struct nlattr *tb[TCA_CAKE_MAX + 1];
int err;
if (!opt)
return -EINVAL;
err = nla_parse_nested(tb, TCA_CAKE_MAX, opt, cake_policy);
if (err < 0)
return err;
if (tb[TCA_CAKE_BASE_RATE])
q->rate_bps = nla_get_u32(tb[TCA_CAKE_BASE_RATE]);
if (tb[TCA_CAKE_DIFFSERV_MODE])
q->tin_mode = nla_get_u32(tb[TCA_CAKE_DIFFSERV_MODE]);
if (tb[TCA_CAKE_ATM]) {
if (!!nla_get_u32(tb[TCA_CAKE_ATM]))
q->rate_flags |= CAKE_FLAG_ATM;
else
q->rate_flags &= ~CAKE_FLAG_ATM;