Fresh pull from upstream
[librecmc/librecmc.git] / package / network / utils / iproute2 / patches / 950-add-cake-to-tc.patch
1 --- a/include/linux/pkt_sched.h
2 +++ b/include/linux/pkt_sched.h
3 @@ -850,4 +850,59 @@ struct tc_pie_xstats {
4         __u32 maxq;             /* maximum queue size */
5         __u32 ecn_mark;         /* packets marked with ecn*/
6  };
7 +
8 +/* CAKE */
9 +enum {
10 +       TCA_CAKE_UNSPEC,
11 +       TCA_CAKE_BASE_RATE,
12 +       TCA_CAKE_DIFFSERV_MODE,
13 +       TCA_CAKE_ATM,
14 +       TCA_CAKE_FLOW_MODE,
15 +       TCA_CAKE_OVERHEAD,
16 +       TCA_CAKE_RTT,
17 +       TCA_CAKE_TARGET,
18 +       TCA_CAKE_AUTORATE,
19 +       TCA_CAKE_MEMORY,
20 +       TCA_CAKE_NAT,
21 +       TCA_CAKE_ETHERNET,
22 +       TCA_CAKE_WASH,
23 +       __TCA_CAKE_MAX
24 +};
25 +#define TCA_CAKE_MAX   (__TCA_CAKE_MAX - 1)
26 +
27 +struct tc_cake_traffic_stats {
28 +       __u32 packets;
29 +       __u32 link_ms;
30 +       __u64 bytes;
31 +};
32 +
33 +#define TC_CAKE_MAX_TINS (8)
34 +struct tc_cake_xstats {
35 +       __u16 version;  /* == 4, increments when struct extended */
36 +       __u8  max_tins; /* == TC_CAKE_MAX_TINS */
37 +       __u8  tin_cnt;  /* <= TC_CAKE_MAX_TINS */
38 +
39 +       __u32 threshold_rate   [TC_CAKE_MAX_TINS];
40 +       __u32 target_us        [TC_CAKE_MAX_TINS];
41 +       struct tc_cake_traffic_stats sent      [TC_CAKE_MAX_TINS];
42 +       struct tc_cake_traffic_stats dropped   [TC_CAKE_MAX_TINS];
43 +       struct tc_cake_traffic_stats ecn_marked[TC_CAKE_MAX_TINS];
44 +       struct tc_cake_traffic_stats backlog   [TC_CAKE_MAX_TINS];
45 +       __u32 interval_us      [TC_CAKE_MAX_TINS];
46 +       __u32 way_indirect_hits[TC_CAKE_MAX_TINS];
47 +       __u32 way_misses       [TC_CAKE_MAX_TINS];
48 +       __u32 way_collisions   [TC_CAKE_MAX_TINS];
49 +       __u32 peak_delay_us    [TC_CAKE_MAX_TINS]; /* ~= delay to bulk flows */
50 +       __u32 avge_delay_us    [TC_CAKE_MAX_TINS];
51 +       __u32 base_delay_us    [TC_CAKE_MAX_TINS]; /* ~= delay to sparse flows */
52 +       __u16 sparse_flows     [TC_CAKE_MAX_TINS];
53 +       __u16 bulk_flows       [TC_CAKE_MAX_TINS];
54 +       __u16 unresponse_flows [TC_CAKE_MAX_TINS]; /* v4 - was u32 last_len  */
55 +       __u16 spare            [TC_CAKE_MAX_TINS]; /* v4 - split last_len */
56 +       __u32 max_skblen       [TC_CAKE_MAX_TINS];
57 +       __u32 capacity_estimate;  /* version 2 */
58 +       __u32 memory_limit;       /* version 3 */
59 +       __u32 memory_used;        /* version 3 */
60 +};
61 +
62  #endif
63 --- a/tc/Makefile
64 +++ b/tc/Makefile
65 @@ -63,6 +63,7 @@ TCMODULES += q_codel.o
66  TCMODULES += q_fq_codel.o
67  TCMODULES += q_fq.o
68  TCMODULES += q_pie.o
69 +TCMODULES += q_cake.o
70  TCMODULES += q_hhf.o
71  TCMODULES += e_bpf.o
72  
73 --- /dev/null
74 +++ b/tc/q_cake.c
75 @@ -0,0 +1,663 @@
76 +/*
77 + * Common Applications Kept Enhanced  --  CAKE
78 + *
79 + *  Copyright (C) 2014-2015 Jonathan Morton <chromatix99@gmail.com>
80 + *
81 + * Redistribution and use in source and binary forms, with or without
82 + * modification, are permitted provided that the following conditions
83 + * are met:
84 + * 1. Redistributions of source code must retain the above copyright
85 + *    notice, this list of conditions, and the following disclaimer,
86 + *    without modification.
87 + * 2. Redistributions in binary form must reproduce the above copyright
88 + *    notice, this list of conditions and the following disclaimer in the
89 + *    documentation and/or other materials provided with the distribution.
90 + * 3. The names of the authors may not be used to endorse or promote products
91 + *    derived from this software without specific prior written permission.
92 + *
93 + * Alternatively, provided that this notice is retained in full, this
94 + * software may be distributed under the terms of the GNU General
95 + * Public License ("GPL") version 2, in which case the provisions of the
96 + * GPL apply INSTEAD OF those given above.
97 + *
98 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
99 + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
100 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
101 + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
102 + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
103 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
104 + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
105 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
106 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
107 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
108 + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
109 + * DAMAGE.
110 + *
111 + */
112 +
113 +#include <stddef.h>
114 +#include <stdio.h>
115 +#include <stdlib.h>
116 +#include <unistd.h>
117 +#include <syslog.h>
118 +#include <fcntl.h>
119 +#include <sys/socket.h>
120 +#include <netinet/in.h>
121 +#include <arpa/inet.h>
122 +#include <string.h>
123 +
124 +#include "utils.h"
125 +#include "tc_util.h"
126 +
127 +static void explain(void)
128 +{
129 +       fprintf(stderr, "Usage: ... cake [ bandwidth RATE | unlimited* | autorate_ingress ]\n"
130 +                       "                [ rtt TIME | datacentre | lan | metro | regional | internet* | oceanic | satellite | interplanetary ]\n"
131 +                       "                [ besteffort | precedence | diffserv8 | diffserv4 | diffserv-llt | diffserv3* ]\n"
132 +                       "                [ flowblind | srchost | dsthost | hosts | flows | dual-srchost | dual-dsthost | triple-isolate* ] [ nat | nonat* ]\n"
133 +                       "                [ ptm | atm | noatm* ] [ overhead N | conservative | raw* ]\n"
134 +                       "                [ wash | nowash* ]\n"
135 +                       "                [ memlimit LIMIT ]\n"
136 +                       "    (* marks defaults)\n");
137 +}
138 +
139 +static int cake_parse_opt(struct qdisc_util *qu, int argc, char **argv,
140 +                             struct nlmsghdr *n)
141 +{
142 +       int unlimited = 0;
143 +       unsigned bandwidth = 0;
144 +       unsigned interval = 0;
145 +       unsigned target = 0;
146 +       unsigned diffserv = 0;
147 +       unsigned memlimit = 0;
148 +       int  overhead = 0;
149 +       bool overhead_set = false;
150 +       bool overhead_override = false;
151 +       int wash = -1;
152 +       int flowmode = -1;
153 +       int nat = -1;
154 +       int atm = -1;
155 +       int autorate = -1;
156 +       struct rtattr *tail;
157 +
158 +       while (argc > 0) {
159 +               if (strcmp(*argv, "bandwidth") == 0) {
160 +                       NEXT_ARG();
161 +                       if (get_rate(&bandwidth, *argv)) {
162 +                               fprintf(stderr, "Illegal \"bandwidth\"\n");
163 +                               return -1;
164 +                       }
165 +                       unlimited = 0;
166 +                       autorate = 0;
167 +               } else if (strcmp(*argv, "unlimited") == 0) {
168 +                       bandwidth = 0;
169 +                       unlimited = 1;
170 +                       autorate = 0;
171 +               } else if (strcmp(*argv, "autorate_ingress") == 0) {
172 +                       autorate = 1;
173 +
174 +               } else if (strcmp(*argv, "rtt") == 0) {
175 +                       NEXT_ARG();
176 +                       if (get_time(&interval, *argv)) {
177 +                               fprintf(stderr, "Illegal \"rtt\"\n");
178 +                               return -1;
179 +                       }
180 +                       target = interval / 20;
181 +                       if(!target)
182 +                               target = 1;
183 +               } else if (strcmp(*argv, "datacentre") == 0) {
184 +                       interval = 100;
185 +                       target   =   5;
186 +               } else if (strcmp(*argv, "lan") == 0) {
187 +                       interval = 1000;
188 +                       target   =   50;
189 +               } else if (strcmp(*argv, "metro") == 0) {
190 +                       interval = 10000;
191 +                       target   =   500;
192 +               } else if (strcmp(*argv, "regional") == 0) {
193 +                       interval = 30000;
194 +                       target    = 1500;
195 +               } else if (strcmp(*argv, "internet") == 0) {
196 +                       interval = 100000;
197 +                       target   =   5000;
198 +               } else if (strcmp(*argv, "oceanic") == 0) {
199 +                       interval = 300000;
200 +                       target   =  15000;
201 +               } else if (strcmp(*argv, "satellite") == 0) {
202 +                       interval = 1000000;
203 +                       target   =   50000;
204 +               } else if (strcmp(*argv, "interplanetary") == 0) {
205 +                       interval = 3600000000U;
206 +                       target   =       5000;
207 +
208 +               } else if (strcmp(*argv, "besteffort") == 0) {
209 +                       diffserv = 1;
210 +               } else if (strcmp(*argv, "precedence") == 0) {
211 +                       diffserv = 2;
212 +               } else if (strcmp(*argv, "diffserv8") == 0) {
213 +                       diffserv = 3;
214 +               } else if (strcmp(*argv, "diffserv4") == 0) {
215 +                       diffserv = 4;
216 +               } else if (strcmp(*argv, "diffserv") == 0) {
217 +                       diffserv = 4;
218 +               } else if (strcmp(*argv, "diffserv-llt") == 0) {
219 +                       diffserv = 5;
220 +               } else if (strcmp(*argv, "diffserv3") == 0) {
221 +                       diffserv = 6;
222 +
223 +               } else if (strcmp(*argv, "nowash") == 0) {
224 +                       wash = 0;
225 +               } else if (strcmp(*argv, "wash") == 0) {
226 +                       wash = 1;
227 +
228 +               } else if (strcmp(*argv, "flowblind") == 0) {
229 +                       flowmode = 0;
230 +               } else if (strcmp(*argv, "srchost") == 0) {
231 +                       flowmode = 1;
232 +               } else if (strcmp(*argv, "dsthost") == 0) {
233 +                       flowmode = 2;
234 +               } else if (strcmp(*argv, "hosts") == 0) {
235 +                       flowmode = 3;
236 +               } else if (strcmp(*argv, "flows") == 0) {
237 +                       flowmode = 4;
238 +               } else if (strcmp(*argv, "dual-srchost") == 0) {
239 +                       flowmode = 5;
240 +               } else if (strcmp(*argv, "dual-dsthost") == 0) {
241 +                       flowmode = 6;
242 +               } else if (strcmp(*argv, "triple-isolate") == 0) {
243 +                       flowmode = 7;
244 +
245 +               } else if (strcmp(*argv, "nat") == 0) {
246 +                       nat = 1;
247 +               } else if (strcmp(*argv, "nonat") == 0) {
248 +                       nat = 0;
249 +
250 +               } else if (strcmp(*argv, "ptm") == 0) {
251 +                       atm = 2;
252 +               } else if (strcmp(*argv, "atm") == 0) {
253 +                       atm = 1;
254 +               } else if (strcmp(*argv, "noatm") == 0) {
255 +                       atm = 0;
256 +
257 +               } else if (strcmp(*argv, "raw") == 0) {
258 +                       atm = 0;
259 +                       overhead = 0;
260 +                       overhead_set = true;
261 +                       overhead_override = true;
262 +               } else if (strcmp(*argv, "conservative") == 0) {
263 +                       /*
264 +                        * Deliberately over-estimate overhead:
265 +                        * one whole ATM cell plus ATM framing.
266 +                        * A safe choice if the actual overhead is unknown.
267 +                        */
268 +                       atm = 1;
269 +                       overhead = 48;
270 +                       overhead_set = true;
271 +
272 +               /* Various ADSL framing schemes, all over ATM cells */
273 +               } else if (strcmp(*argv, "ipoa-vcmux") == 0) {
274 +                       atm = 1;
275 +                       overhead += 8;
276 +                       overhead_set = true;
277 +               } else if (strcmp(*argv, "ipoa-llcsnap") == 0) {
278 +                       atm = 1;
279 +                       overhead += 16;
280 +                       overhead_set = true;
281 +               } else if (strcmp(*argv, "bridged-vcmux") == 0) {
282 +                       atm = 1;
283 +                       overhead += 24;
284 +                       overhead_set = true;
285 +               } else if (strcmp(*argv, "bridged-llcsnap") == 0) {
286 +                       atm = 1;
287 +                       overhead += 32;
288 +                       overhead_set = true;
289 +               } else if (strcmp(*argv, "pppoa-vcmux") == 0) {
290 +                       atm = 1;
291 +                       overhead += 10;
292 +                       overhead_set = true;
293 +               } else if (strcmp(*argv, "pppoa-llc") == 0) {
294 +                       atm = 1;
295 +                       overhead += 14;
296 +                       overhead_set = true;
297 +               } else if (strcmp(*argv, "pppoe-vcmux") == 0) {
298 +                       atm = 1;
299 +                       overhead += 32;
300 +                       overhead_set = true;
301 +               } else if (strcmp(*argv, "pppoe-llcsnap") == 0) {
302 +                       atm = 1;
303 +                       overhead += 40;
304 +                       overhead_set = true;
305 +
306 +               /* Typical VDSL2 framing schemes, both over PTM */
307 +               /* PTM has 64b/65b coding which absorbs some bandwidth */
308 +               } else if (strcmp(*argv, "pppoe-ptm") == 0) {
309 +                       atm = 2;
310 +                       overhead += 27;
311 +                       overhead_set = true;
312 +               } else if (strcmp(*argv, "bridged-ptm") == 0) {
313 +                       atm = 2;
314 +                       overhead += 19;
315 +                       overhead_set = true;
316 +
317 +               } else if (strcmp(*argv, "via-ethernet") == 0) {
318 +                       /*
319 +                        * We used to use this flag to manually compensate for
320 +                        * Linux including the Ethernet header on Ethernet-type
321 +                        * interfaces, but not on IP-type interfaces.
322 +                        *
323 +                        * It is no longer needed, because Cake now adjusts for
324 +                        * that automatically, and is thus ignored.
325 +                        *
326 +                        * It would be deleted entirely, but it appears in the
327 +                        * stats output when the automatic compensation is active.
328 +                        */
329 +
330 +               } else if (strcmp(*argv, "ethernet") == 0) {
331 +                       /* ethernet pre-amble & interframe gap & FCS
332 +                        * you may need to add vlan tag */
333 +                       overhead += 38;
334 +                       overhead_set = true;
335 +
336 +               /* Additional Ethernet-related overhead used by some ISPs */
337 +               } else if (strcmp(*argv, "ether-vlan") == 0) {
338 +                       /* 802.1q VLAN tag - may be repeated */
339 +                       overhead += 4;
340 +                       overhead_set = true;
341 +
342 +               /*
343 +                * DOCSIS cable shapers account for Ethernet frame with FCS,
344 +                * but not interframe gap nor preamble.
345 +                */
346 +               } else if (strcmp(*argv, "docsis") == 0) {
347 +                       atm = 0;
348 +                       overhead += 18;
349 +                       overhead_set = true;
350 +
351 +               } else if (strcmp(*argv, "overhead") == 0) {
352 +                       char* p = NULL;
353 +                       NEXT_ARG();
354 +                       overhead = strtol(*argv, &p, 10);
355 +                       if(!p || *p || !*argv || overhead < -64 || overhead > 256) {
356 +                               fprintf(stderr, "Illegal \"overhead\", valid range is -64 to 256\\n");
357 +                               return -1;
358 +                       }
359 +                       overhead_set = true;
360 +
361 +               } else if (strcmp(*argv, "memlimit") == 0) {
362 +                       NEXT_ARG();
363 +                       if(get_size(&memlimit, *argv)) {
364 +                               fprintf(stderr, "Illegal value for \"memlimit\": \"%s\"\n", *argv);
365 +                               return -1;
366 +                       }
367 +
368 +               } else if (strcmp(*argv, "help") == 0) {
369 +                       explain();
370 +                       return -1;
371 +               } else {
372 +                       fprintf(stderr, "What is \"%s\"?\n", *argv);
373 +                       explain();
374 +                       return -1;
375 +               }
376 +               argc--; argv++;
377 +       }
378 +
379 +       tail = NLMSG_TAIL(n);
380 +       addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
381 +       if (bandwidth || unlimited)
382 +               addattr_l(n, 1024, TCA_CAKE_BASE_RATE, &bandwidth, sizeof(bandwidth));
383 +       if (diffserv)
384 +               addattr_l(n, 1024, TCA_CAKE_DIFFSERV_MODE, &diffserv, sizeof(diffserv));
385 +       if (atm != -1)
386 +               addattr_l(n, 1024, TCA_CAKE_ATM, &atm, sizeof(atm));
387 +       if (flowmode != -1)
388 +               addattr_l(n, 1024, TCA_CAKE_FLOW_MODE, &flowmode, sizeof(flowmode));
389 +       if (overhead_set)
390 +               addattr_l(n, 1024, TCA_CAKE_OVERHEAD, &overhead, sizeof(overhead));
391 +       if (overhead_override) {
392 +               unsigned zero = 0;
393 +               addattr_l(n, 1024, TCA_CAKE_ETHERNET, &zero, sizeof(zero));
394 +       }
395 +       if (interval)
396 +               addattr_l(n, 1024, TCA_CAKE_RTT, &interval, sizeof(interval));
397 +       if (target)
398 +               addattr_l(n, 1024, TCA_CAKE_TARGET, &target, sizeof(target));
399 +       if (autorate != -1)
400 +               addattr_l(n, 1024, TCA_CAKE_AUTORATE, &autorate, sizeof(autorate));
401 +       if (memlimit)
402 +               addattr_l(n, 1024, TCA_CAKE_MEMORY, &memlimit, sizeof(memlimit));
403 +       if (nat != -1)
404 +               addattr_l(n, 1024, TCA_CAKE_NAT, &nat, sizeof(nat));
405 +       if (wash != -1)
406 +               addattr_l(n, 1024, TCA_CAKE_WASH, &wash, sizeof(wash));
407 +
408 +       tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
409 +       return 0;
410 +}
411 +
412 +
413 +static int cake_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
414 +{
415 +       struct rtattr *tb[TCA_CAKE_MAX + 1];
416 +       unsigned bandwidth = 0;
417 +       unsigned diffserv = 0;
418 +       unsigned flowmode = 0;
419 +       unsigned interval = 0;
420 +       unsigned memlimit = 0;
421 +       int overhead = 0;
422 +       int ethernet = 0;
423 +       int atm = 0;
424 +       int nat = 0;
425 +       int autorate = 0;
426 +       int wash = 0;
427 +       SPRINT_BUF(b1);
428 +       SPRINT_BUF(b2);
429 +
430 +       if (opt == NULL)
431 +               return 0;
432 +
433 +       parse_rtattr_nested(tb, TCA_CAKE_MAX, opt);
434 +
435 +       if (tb[TCA_CAKE_BASE_RATE] &&
436 +           RTA_PAYLOAD(tb[TCA_CAKE_BASE_RATE]) >= sizeof(__u32)) {
437 +               bandwidth = rta_getattr_u32(tb[TCA_CAKE_BASE_RATE]);
438 +               if(bandwidth)
439 +                       fprintf(f, "bandwidth %s ", sprint_rate(bandwidth, b1));
440 +               else
441 +                       fprintf(f, "unlimited ");
442 +       }
443 +       if (tb[TCA_CAKE_AUTORATE] &&
444 +               RTA_PAYLOAD(tb[TCA_CAKE_AUTORATE]) >= sizeof(__u32)) {
445 +               autorate = rta_getattr_u32(tb[TCA_CAKE_AUTORATE]);
446 +               if(autorate == 1)
447 +                       fprintf(f, "autorate_ingress ");
448 +               else if(autorate)
449 +                       fprintf(f, "(?autorate?) ");
450 +       }
451 +       if (tb[TCA_CAKE_DIFFSERV_MODE] &&
452 +           RTA_PAYLOAD(tb[TCA_CAKE_DIFFSERV_MODE]) >= sizeof(__u32)) {
453 +               diffserv = rta_getattr_u32(tb[TCA_CAKE_DIFFSERV_MODE]);
454 +               switch(diffserv) {
455 +               case 1:
456 +                       fprintf(f, "besteffort ");
457 +                       break;
458 +               case 2:
459 +                       fprintf(f, "precedence ");
460 +                       break;
461 +               case 3:
462 +                       fprintf(f, "diffserv8 ");
463 +                       break;
464 +               case 4:
465 +                       fprintf(f, "diffserv4 ");
466 +                       break;
467 +               case 5:
468 +                       fprintf(f, "diffserv-llt ");
469 +                       break;
470 +               case 6:
471 +                       fprintf(f, "diffserv3 ");
472 +                       break;
473 +               default:
474 +                       fprintf(f, "(?diffserv?) ");
475 +                       break;
476 +               };
477 +       }
478 +       if (tb[TCA_CAKE_FLOW_MODE] &&
479 +           RTA_PAYLOAD(tb[TCA_CAKE_FLOW_MODE]) >= sizeof(__u32)) {
480 +               flowmode = rta_getattr_u32(tb[TCA_CAKE_FLOW_MODE]);
481 +               nat = !!(flowmode & 64);
482 +               flowmode &= ~64;
483 +               switch(flowmode) {
484 +               case 0:
485 +                       fprintf(f, "flowblind ");
486 +                       break;
487 +               case 1:
488 +                       fprintf(f, "srchost ");
489 +                       break;
490 +               case 2:
491 +                       fprintf(f, "dsthost ");
492 +                       break;
493 +               case 3:
494 +                       fprintf(f, "hosts ");
495 +                       break;
496 +               case 4:
497 +                       fprintf(f, "flows ");
498 +                       break;
499 +               case 5:
500 +                       fprintf(f, "dual-srchost ");
501 +                       break;
502 +               case 6:
503 +                       fprintf(f, "dual-dsthost ");
504 +                       break;
505 +               case 7:
506 +                       fprintf(f, "triple-isolate ");
507 +                       break;
508 +               default:
509 +                       fprintf(f, "(?flowmode?) ");
510 +                       break;
511 +               };
512 +
513 +               if(nat)
514 +                       fprintf(f, "nat ");
515 +       }
516 +       if (tb[TCA_CAKE_WASH] &&
517 +           RTA_PAYLOAD(tb[TCA_CAKE_WASH]) >= sizeof(__u32)) {
518 +               wash = rta_getattr_u32(tb[TCA_CAKE_WASH]);
519 +       }
520 +       if (tb[TCA_CAKE_ATM] &&
521 +           RTA_PAYLOAD(tb[TCA_CAKE_ATM]) >= sizeof(__u32)) {
522 +               atm = rta_getattr_u32(tb[TCA_CAKE_ATM]);
523 +       }
524 +       if (tb[TCA_CAKE_OVERHEAD] &&
525 +           RTA_PAYLOAD(tb[TCA_CAKE_OVERHEAD]) >= sizeof(__u32)) {
526 +               overhead = rta_getattr_u32(tb[TCA_CAKE_OVERHEAD]);
527 +       }
528 +       if (tb[TCA_CAKE_ETHERNET] &&
529 +           RTA_PAYLOAD(tb[TCA_CAKE_ETHERNET]) >= sizeof(__u32)) {
530 +               ethernet = rta_getattr_u32(tb[TCA_CAKE_ETHERNET]);
531 +       }
532 +       if (tb[TCA_CAKE_RTT] &&
533 +           RTA_PAYLOAD(tb[TCA_CAKE_RTT]) >= sizeof(__u32)) {
534 +               interval = rta_getattr_u32(tb[TCA_CAKE_RTT]);
535 +       }
536 +
537 +       if (wash)
538 +               fprintf(f,"wash ");
539 +
540 +       if (interval)
541 +               fprintf(f, "rtt %s ", sprint_time(interval, b2));
542 +
543 +       if (!atm && overhead == ethernet) {
544 +               fprintf(f, "raw ");
545 +       } else {
546 +               if (atm == 1)
547 +                       fprintf(f, "atm ");
548 +               else if (atm == 2)
549 +                       fprintf(f, "ptm ");
550 +               else
551 +                       fprintf(f, "noatm ");
552 +
553 +               fprintf(f, "overhead %d ", overhead);
554 +
555 +               // This is actually the *amount* of automatic compensation, but we only report
556 +               // its presence as a boolean for now.
557 +               if (ethernet)
558 +                       fprintf(f, "via-ethernet ");
559 +       }
560 +
561 +       if (memlimit)
562 +               fprintf(f, "memlimit %s", sprint_size(memlimit, b1));
563 +
564 +       return 0;
565 +}
566 +
567 +static int cake_print_xstats(struct qdisc_util *qu, FILE *f,
568 +                                struct rtattr *xstats)
569 +{
570 +       /* fq_codel stats format borrowed */
571 +       struct tc_fq_codel_xstats *st;
572 +       struct tc_cake_xstats     *stnc;
573 +       SPRINT_BUF(b1);
574 +       SPRINT_BUF(b2);
575 +
576 +       if (xstats == NULL)
577 +               return 0;
578 +
579 +       if (RTA_PAYLOAD(xstats) < sizeof(st->type))
580 +               return -1;
581 +
582 +       st   = RTA_DATA(xstats);
583 +       stnc = RTA_DATA(xstats);
584 +
585 +       if (st->type == TCA_FQ_CODEL_XSTATS_QDISC && RTA_PAYLOAD(xstats) >= sizeof(*st)) {
586 +               fprintf(f, "  maxpacket %u drop_overlimit %u new_flow_count %u ecn_mark %u",
587 +                       st->qdisc_stats.maxpacket,
588 +                       st->qdisc_stats.drop_overlimit,
589 +                       st->qdisc_stats.new_flow_count,
590 +                       st->qdisc_stats.ecn_mark);
591 +               fprintf(f, "\n  new_flows_len %u old_flows_len %u",
592 +                       st->qdisc_stats.new_flows_len,
593 +                       st->qdisc_stats.old_flows_len);
594 +       } else if (st->type == TCA_FQ_CODEL_XSTATS_CLASS && RTA_PAYLOAD(xstats) >= sizeof(*st)) {
595 +               fprintf(f, "  deficit %d count %u lastcount %u ldelay %s",
596 +                       st->class_stats.deficit,
597 +                       st->class_stats.count,
598 +                       st->class_stats.lastcount,
599 +                       sprint_time(st->class_stats.ldelay, b1));
600 +               if (st->class_stats.dropping) {
601 +                       fprintf(f, " dropping");
602 +                       if (st->class_stats.drop_next < 0)
603 +                               fprintf(f, " drop_next -%s",
604 +                                       sprint_time(-st->class_stats.drop_next, b1));
605 +                       else
606 +                               fprintf(f, " drop_next %s",
607 +                                       sprint_time(st->class_stats.drop_next, b1));
608 +               }
609 +       } else if (stnc->version >= 1 && stnc->version < 0xFF
610 +                               && stnc->max_tins == TC_CAKE_MAX_TINS
611 +                               && RTA_PAYLOAD(xstats) >= offsetof(struct tc_cake_xstats, capacity_estimate))
612 +       {
613 +               int i;
614 +
615 +               if(stnc->version >= 3)
616 +                       fprintf(f, " memory used: %s of %s\n", sprint_size(stnc->memory_used, b1), sprint_size(stnc->memory_limit, b2));
617 +
618 +               if(stnc->version >= 2)
619 +                       fprintf(f, " capacity estimate: %s\n", sprint_rate(stnc->capacity_estimate, b1));
620 +
621 +               switch(stnc->tin_cnt) {
622 +               case 3:
623 +                       fprintf(f, "                 Bulk   Best Effort      Voice\n");
624 +                       break;
625 +
626 +               case 4:
627 +                       fprintf(f, "                 Bulk   Best Effort      Video       Voice\n");
628 +                       break;
629 +
630 +               case 5:
631 +                       fprintf(f, "              Low Loss  Best Effort   Low Delay       Bulk  Net Control\n");
632 +                       break;
633 +
634 +               default:
635 +                       fprintf(f, "          ");
636 +                       for(i=0; i < stnc->tin_cnt; i++)
637 +                               fprintf(f, "       Tin %u", i);
638 +                       fprintf(f, "\n");
639 +               };
640 +
641 +               fprintf(f, "  thresh  ");
642 +               for(i=0; i < stnc->tin_cnt; i++)
643 +                       fprintf(f, "%12s", sprint_rate(stnc->threshold_rate[i], b1));
644 +               fprintf(f, "\n");
645 +
646 +               fprintf(f, "  target  ");
647 +               for(i=0; i < stnc->tin_cnt; i++)
648 +                       fprintf(f, "%12s", sprint_time(stnc->target_us[i], b1));
649 +               fprintf(f, "\n");
650 +
651 +               fprintf(f, "  interval");
652 +               for(i=0; i < stnc->tin_cnt; i++)
653 +                       fprintf(f, "%12s", sprint_time(stnc->interval_us[i], b1));
654 +               fprintf(f, "\n");
655 +
656 +               fprintf(f, "  pk_delay");
657 +               for(i=0; i < stnc->tin_cnt; i++)
658 +                       fprintf(f, "%12s", sprint_time(stnc->peak_delay_us[i], b1));
659 +               fprintf(f, "\n");
660 +
661 +               fprintf(f, "  av_delay");
662 +               for(i=0; i < stnc->tin_cnt; i++)
663 +                       fprintf(f, "%12s", sprint_time(stnc->avge_delay_us[i], b1));
664 +               fprintf(f, "\n");
665 +
666 +               fprintf(f, "  sp_delay");
667 +               for(i=0; i < stnc->tin_cnt; i++)
668 +                       fprintf(f, "%12s", sprint_time(stnc->base_delay_us[i], b1));
669 +               fprintf(f, "\n");
670 +
671 +               fprintf(f, "  pkts    ");
672 +               for(i=0; i < stnc->tin_cnt; i++)
673 +                       fprintf(f, "%12u", stnc->sent[i].packets);
674 +               fprintf(f, "\n");
675 +
676 +               fprintf(f, "  bytes   ");
677 +               for(i=0; i < stnc->tin_cnt; i++)
678 +                       fprintf(f, "%12llu", stnc->sent[i].bytes);
679 +               fprintf(f, "\n");
680 +
681 +               fprintf(f, "  way_inds");
682 +               for(i=0; i < stnc->tin_cnt; i++)
683 +                       fprintf(f, "%12u", stnc->way_indirect_hits[i]);
684 +               fprintf(f, "\n");
685 +
686 +               fprintf(f, "  way_miss");
687 +               for(i=0; i < stnc->tin_cnt; i++)
688 +                       fprintf(f, "%12u", stnc->way_misses[i]);
689 +               fprintf(f, "\n");
690 +
691 +               fprintf(f, "  way_cols");
692 +               for(i=0; i < stnc->tin_cnt; i++)
693 +                       fprintf(f, "%12u", stnc->way_collisions[i]);
694 +               fprintf(f, "\n");
695 +
696 +               fprintf(f, "  drops   ");
697 +               for(i=0; i < stnc->tin_cnt; i++)
698 +                       fprintf(f, "%12u", stnc->dropped[i].packets);
699 +               fprintf(f, "\n");
700 +
701 +               fprintf(f, "  marks   ");
702 +               for(i=0; i < stnc->tin_cnt; i++)
703 +                       fprintf(f, "%12u", stnc->ecn_marked[i].packets);
704 +               fprintf(f, "\n");
705 +
706 +               fprintf(f, "  sp_flows");
707 +               for(i=0; i < stnc->tin_cnt; i++)
708 +                       fprintf(f, "%12u", stnc->sparse_flows[i]);
709 +               fprintf(f, "\n");
710 +
711 +               fprintf(f, "  bk_flows");
712 +               for(i=0; i < stnc->tin_cnt; i++)
713 +                       fprintf(f, "%12u", stnc->bulk_flows[i]);
714 +               fprintf(f, "\n");
715 +
716 +               if(stnc->version >= 4) {
717 +                       fprintf(f, "  un_flows");
718 +                       for(i=0; i < stnc->tin_cnt; i++)
719 +                               fprintf(f, "%12u", stnc->unresponse_flows[i]);
720 +                       fprintf(f, "\n");
721 +               }
722 +
723 +               fprintf(f, "  max_len ");
724 +               for(i=0; i < stnc->tin_cnt; i++)
725 +                       fprintf(f, "%12u", stnc->max_skblen[i]);
726 +               fprintf(f, "\n");
727 +       } else {
728 +               return -1;
729 +       }
730 +       return 0;
731 +}
732 +
733 +struct qdisc_util cake_qdisc_util = {
734 +       .id             = "cake",
735 +       .parse_qopt     = cake_parse_opt,
736 +       .print_qopt     = cake_print_opt,
737 +       .print_xstats   = cake_print_xstats,
738 +};