iproute2/tc/q_cake.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2
   3/*
   4 * Common Applications Kept Enhanced  --  CAKE
   5 *
   6 *  Copyright (C) 2014-2018 Jonathan Morton <chromatix99@gmail.com>
   7 *  Copyright (C) 2017-2018 Toke Høiland-Jørgensen <toke@toke.dk>
   8 */
   9
  10#include <stddef.h>
  11#include <stdio.h>
  12#include <stdlib.h>
  13#include <unistd.h>
  14#include <syslog.h>
  15#include <fcntl.h>
  16#include <sys/socket.h>
  17#include <netinet/in.h>
  18#include <arpa/inet.h>
  19#include <string.h>
  20#include <inttypes.h>
  21
  22#include "utils.h"
  23#include "tc_util.h"
  24
  25struct cake_preset {
  26        char *name;
  27        unsigned int target;
  28        unsigned int interval;
  29};
  30
  31static struct cake_preset presets[] = {
  32        {"datacentre",          5,              100},
  33        {"lan",                 50,             1000},
  34        {"metro",               500,            10000},
  35        {"regional",            1500,           30000},
  36        {"internet",            5000,           100000},
  37        {"oceanic",             15000,          300000},
  38        {"satellite",           50000,          1000000},
  39        {"interplanetary",      50000000,       1000000000},
  40};
  41
  42static const char * diffserv_names[CAKE_DIFFSERV_MAX] = {
  43        [CAKE_DIFFSERV_DIFFSERV3] = "diffserv3",
  44        [CAKE_DIFFSERV_DIFFSERV4] = "diffserv4",
  45        [CAKE_DIFFSERV_DIFFSERV8] = "diffserv8",
  46        [CAKE_DIFFSERV_BESTEFFORT] = "besteffort",
  47        [CAKE_DIFFSERV_PRECEDENCE] = "precedence",
  48};
  49
  50static const char * flowmode_names[CAKE_FLOW_MAX] = {
  51        [CAKE_FLOW_NONE] = "flowblind",
  52        [CAKE_FLOW_SRC_IP] = "srchost",
  53        [CAKE_FLOW_DST_IP] = "dsthost",
  54        [CAKE_FLOW_HOSTS] = "hosts",
  55        [CAKE_FLOW_FLOWS] = "flows",
  56        [CAKE_FLOW_DUAL_SRC] = "dual-srchost",
  57        [CAKE_FLOW_DUAL_DST] = "dual-dsthost",
  58        [CAKE_FLOW_TRIPLE] = "triple-isolate",
  59};
  60
  61static struct cake_preset *find_preset(char *argv)
  62{
  63        int i;
  64
  65        for (i = 0; i < ARRAY_SIZE(presets); i++)
  66                if (!strcmp(argv, presets[i].name))
  67                        return &presets[i];
  68        return NULL;
  69}
  70
  71static void explain(void)
  72{
  73        fprintf(stderr,
  74                "Usage: ... cake [ bandwidth RATE | unlimited* | autorate-ingress ]\n"
  75                "                [ rtt TIME | datacentre | lan | metro | regional |\n"
  76                "                  internet* | oceanic | satellite | interplanetary ]\n"
  77                "                [ besteffort | diffserv8 | diffserv4 | diffserv3* ]\n"
  78                "                [ flowblind | srchost | dsthost | hosts | flows |\n"
  79                "                  dual-srchost | dual-dsthost | triple-isolate* ]\n"
  80                "                [ nat | nonat* ]\n"
  81                "                [ wash | nowash* ]\n"
  82                "                [ split-gso* | no-split-gso ]\n"
  83                "                [ ack-filter | ack-filter-aggressive | no-ack-filter* ]\n"
  84                "                [ memlimit LIMIT ]\n"
  85                "                [ fwmark MASK ]\n"
  86                "                [ ptm | atm | noatm* ] [ overhead N | conservative | raw* ]\n"
  87                "                [ mpu N ] [ ingress | egress* ]\n"
  88                "                (* marks defaults)\n");
  89}
  90
  91static int cake_parse_opt(struct qdisc_util *qu, int argc, char **argv,
  92                          struct nlmsghdr *n, const char *dev)
  93{
  94        struct cake_preset *preset, *preset_set = NULL;
  95        bool overhead_override = false;
  96        bool overhead_set = false;
  97        unsigned int interval = 0;
  98        unsigned int diffserv = 0;
  99        unsigned int memlimit = 0;
 100        unsigned int fwmark = 0;
 101        unsigned int target = 0;
 102        __u64 bandwidth = 0;
 103        int ack_filter = -1;
 104        struct rtattr *tail;
 105        int split_gso = -1;
 106        int unlimited = 0;
 107        int flowmode = -1;
 108        int autorate = -1;
 109        int ingress = -1;
 110        int overhead = 0;
 111        int wash = -1;
 112        int nat = -1;
 113        int atm = -1;
 114        int mpu = 0;
 115
 116        while (argc > 0) {
 117                if (strcmp(*argv, "bandwidth") == 0) {
 118                        NEXT_ARG();
 119                        if (get_rate64(&bandwidth, *argv)) {
 120                                fprintf(stderr, "Illegal \"bandwidth\"\n");
 121                                return -1;
 122                        }
 123                        unlimited = 0;
 124                        autorate = 0;
 125                } else if (strcmp(*argv, "unlimited") == 0) {
 126                        bandwidth = 0;
 127                        unlimited = 1;
 128                        autorate = 0;
 129                } else if (strcmp(*argv, "autorate-ingress") == 0) {
 130                        autorate = 1;
 131                } else if (strcmp(*argv, "rtt") == 0) {
 132                        NEXT_ARG();
 133                        if (get_time(&interval, *argv)) {
 134                                fprintf(stderr, "Illegal \"rtt\"\n");
 135                                return -1;
 136                        }
 137                        target = interval / 20;
 138                        if (!target)
 139                                target = 1;
 140                } else if ((preset = find_preset(*argv))) {
 141                        if (preset_set)
 142                                duparg(*argv, preset_set->name);
 143                        preset_set = preset;
 144                        target = preset->target;
 145                        interval = preset->interval;
 146                } else if (strcmp(*argv, "besteffort") == 0) {
 147                        diffserv = CAKE_DIFFSERV_BESTEFFORT;
 148                } else if (strcmp(*argv, "precedence") == 0) {
 149                        diffserv = CAKE_DIFFSERV_PRECEDENCE;
 150                } else if (strcmp(*argv, "diffserv8") == 0) {
 151                        diffserv = CAKE_DIFFSERV_DIFFSERV8;
 152                } else if (strcmp(*argv, "diffserv4") == 0) {
 153                        diffserv = CAKE_DIFFSERV_DIFFSERV4;
 154                } else if (strcmp(*argv, "diffserv") == 0) {
 155                        diffserv = CAKE_DIFFSERV_DIFFSERV4;
 156                } else if (strcmp(*argv, "diffserv3") == 0) {
 157                        diffserv = CAKE_DIFFSERV_DIFFSERV3;
 158                } else if (strcmp(*argv, "nowash") == 0) {
 159                        wash = 0;
 160                } else if (strcmp(*argv, "wash") == 0) {
 161                        wash = 1;
 162                } else if (strcmp(*argv, "split-gso") == 0) {
 163                        split_gso = 1;
 164                } else if (strcmp(*argv, "no-split-gso") == 0) {
 165                        split_gso = 0;
 166                } else if (strcmp(*argv, "flowblind") == 0) {
 167                        flowmode = CAKE_FLOW_NONE;
 168                } else if (strcmp(*argv, "srchost") == 0) {
 169                        flowmode = CAKE_FLOW_SRC_IP;
 170                } else if (strcmp(*argv, "dsthost") == 0) {
 171                        flowmode = CAKE_FLOW_DST_IP;
 172                } else if (strcmp(*argv, "hosts") == 0) {
 173                        flowmode = CAKE_FLOW_HOSTS;
 174                } else if (strcmp(*argv, "flows") == 0) {
 175                        flowmode = CAKE_FLOW_FLOWS;
 176                } else if (strcmp(*argv, "dual-srchost") == 0) {
 177                        flowmode = CAKE_FLOW_DUAL_SRC;
 178                } else if (strcmp(*argv, "dual-dsthost") == 0) {
 179                        flowmode = CAKE_FLOW_DUAL_DST;
 180                } else if (strcmp(*argv, "triple-isolate") == 0) {
 181                        flowmode = CAKE_FLOW_TRIPLE;
 182                } else if (strcmp(*argv, "nat") == 0) {
 183                        nat = 1;
 184                } else if (strcmp(*argv, "nonat") == 0) {
 185                        nat = 0;
 186                } else if (strcmp(*argv, "ptm") == 0) {
 187                        atm = CAKE_ATM_PTM;
 188                } else if (strcmp(*argv, "atm") == 0) {
 189                        atm = CAKE_ATM_ATM;
 190                } else if (strcmp(*argv, "noatm") == 0) {
 191                        atm = CAKE_ATM_NONE;
 192                } else if (strcmp(*argv, "raw") == 0) {
 193                        atm = CAKE_ATM_NONE;
 194                        overhead = 0;
 195                        overhead_set = true;
 196                        overhead_override = true;
 197                } else if (strcmp(*argv, "conservative") == 0) {
 198                        /*
 199                         * Deliberately over-estimate overhead:
 200                         * one whole ATM cell plus ATM framing.
 201                         * A safe choice if the actual overhead is unknown.
 202                         */
 203                        atm = CAKE_ATM_ATM;
 204                        overhead = 48;
 205                        overhead_set = true;
 206
 207                /* Various ADSL framing schemes, all over ATM cells */
 208                } else if (strcmp(*argv, "ipoa-vcmux") == 0) {
 209                        atm = CAKE_ATM_ATM;
 210                        overhead += 8;
 211                        overhead_set = true;
 212                } else if (strcmp(*argv, "ipoa-llcsnap") == 0) {
 213                        atm = CAKE_ATM_ATM;
 214                        overhead += 16;
 215                        overhead_set = true;
 216                } else if (strcmp(*argv, "bridged-vcmux") == 0) {
 217                        atm = CAKE_ATM_ATM;
 218                        overhead += 24;
 219                        overhead_set = true;
 220                } else if (strcmp(*argv, "bridged-llcsnap") == 0) {
 221                        atm = CAKE_ATM_ATM;
 222                        overhead += 32;
 223                        overhead_set = true;
 224                } else if (strcmp(*argv, "pppoa-vcmux") == 0) {
 225                        atm = CAKE_ATM_ATM;
 226                        overhead += 10;
 227                        overhead_set = true;
 228                } else if (strcmp(*argv, "pppoa-llc") == 0) {
 229                        atm = CAKE_ATM_ATM;
 230                        overhead += 14;
 231                        overhead_set = true;
 232                } else if (strcmp(*argv, "pppoe-vcmux") == 0) {
 233                        atm = CAKE_ATM_ATM;
 234                        overhead += 32;
 235                        overhead_set = true;
 236                } else if (strcmp(*argv, "pppoe-llcsnap") == 0) {
 237                        atm = CAKE_ATM_ATM;
 238                        overhead += 40;
 239                        overhead_set = true;
 240
 241                /* Typical VDSL2 framing schemes, both over PTM */
 242                /* PTM has 64b/65b coding which absorbs some bandwidth */
 243                } else if (strcmp(*argv, "pppoe-ptm") == 0) {
 244                        /* 2B PPP + 6B PPPoE + 6B dest MAC + 6B src MAC
 245                         * + 2B ethertype + 4B Frame Check Sequence
 246                         * + 1B Start of Frame (S) + 1B End of Frame (Ck)
 247                         * + 2B TC-CRC (PTM-FCS) = 30B
 248                         */
 249                        atm = CAKE_ATM_PTM;
 250                        overhead += 30;
 251                        overhead_set = true;
 252                } else if (strcmp(*argv, "bridged-ptm") == 0) {
 253                        /* 6B dest MAC + 6B src MAC + 2B ethertype
 254                         * + 4B Frame Check Sequence
 255                         * + 1B Start of Frame (S) + 1B End of Frame (Ck)
 256                         * + 2B TC-CRC (PTM-FCS) = 22B
 257                         */
 258                        atm = CAKE_ATM_PTM;
 259                        overhead += 22;
 260                        overhead_set = true;
 261                } else if (strcmp(*argv, "via-ethernet") == 0) {
 262                        /*
 263                         * We used to use this flag to manually compensate for
 264                         * Linux including the Ethernet header on Ethernet-type
 265                         * interfaces, but not on IP-type interfaces.
 266                         *
 267                         * It is no longer needed, because Cake now adjusts for
 268                         * that automatically, and is thus ignored.
 269                         *
 270                         * It would be deleted entirely, but it appears in the
 271                         * stats output when the automatic compensation is
 272                         * active.
 273                         */
 274                } else if (strcmp(*argv, "ethernet") == 0) {
 275                        /* ethernet pre-amble & interframe gap & FCS
 276                         * you may need to add vlan tag
 277                         */
 278                        overhead += 38;
 279                        overhead_set = true;
 280                        mpu = 84;
 281
 282                /* Additional Ethernet-related overhead used by some ISPs */
 283                } else if (strcmp(*argv, "ether-vlan") == 0) {
 284                        /* 802.1q VLAN tag - may be repeated */
 285                        overhead += 4;
 286                        overhead_set = true;
 287
 288                /*
 289                 * DOCSIS cable shapers account for Ethernet frame with FCS,
 290                 * but not interframe gap or preamble.
 291                 */
 292                } else if (strcmp(*argv, "docsis") == 0) {
 293                        atm = CAKE_ATM_NONE;
 294                        overhead += 18;
 295                        overhead_set = true;
 296                        mpu = 64;
 297                } else if (strcmp(*argv, "overhead") == 0) {
 298                        char *p = NULL;
 299
 300                        NEXT_ARG();
 301                        overhead = strtol(*argv, &p, 10);
 302                        if (!p || *p || overhead < -64 || overhead > 256) {
 303                                fprintf(stderr,
 304                                        "Illegal \"overhead\", valid range is -64 to 256\\n");
 305                                return -1;
 306                        }
 307                        overhead_set = true;
 308
 309                } else if (strcmp(*argv, "mpu") == 0) {
 310                        char *p = NULL;
 311
 312                        NEXT_ARG();
 313                        mpu = strtol(*argv, &p, 10);
 314                        if (!p || *p || mpu < 0 || mpu > 256) {
 315                                fprintf(stderr,
 316                                        "Illegal \"mpu\", valid range is 0 to 256\\n");
 317                                return -1;
 318                        }
 319                } else if (strcmp(*argv, "ingress") == 0) {
 320                        ingress = 1;
 321                } else if (strcmp(*argv, "egress") == 0) {
 322                        ingress = 0;
 323                } else if (strcmp(*argv, "no-ack-filter") == 0) {
 324                        ack_filter = CAKE_ACK_NONE;
 325                } else if (strcmp(*argv, "ack-filter") == 0) {
 326                        ack_filter = CAKE_ACK_FILTER;
 327                } else if (strcmp(*argv, "ack-filter-aggressive") == 0) {
 328                        ack_filter = CAKE_ACK_AGGRESSIVE;
 329                } else if (strcmp(*argv, "memlimit") == 0) {
 330                        NEXT_ARG();
 331                        if (get_size(&memlimit, *argv)) {
 332                                fprintf(stderr,
 333                                        "Illegal value for \"memlimit\": \"%s\"\n", *argv);
 334                                return -1;
 335                        }
 336                } else if (strcmp(*argv, "fwmark") == 0) {
 337                        NEXT_ARG();
 338                        if (get_u32(&fwmark, *argv, 0)) {
 339                                fprintf(stderr,
 340                                        "Illegal value for \"fwmark\": \"%s\"\n", *argv);
 341                                return -1;
 342                        }
 343                } else if (strcmp(*argv, "help") == 0) {
 344                        explain();
 345                        return -1;
 346                } else {
 347                        fprintf(stderr, "What is \"%s\"?\n", *argv);
 348                        explain();
 349                        return -1;
 350                }
 351                argc--; argv++;
 352        }
 353
 354        tail = NLMSG_TAIL(n);
 355        addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
 356        if (bandwidth || unlimited)
 357                addattr_l(n, 1024, TCA_CAKE_BASE_RATE64, &bandwidth,
 358                          sizeof(bandwidth));
 359        if (diffserv)
 360                addattr_l(n, 1024, TCA_CAKE_DIFFSERV_MODE, &diffserv,
 361                          sizeof(diffserv));
 362        if (atm != -1)
 363                addattr_l(n, 1024, TCA_CAKE_ATM, &atm, sizeof(atm));
 364        if (flowmode != -1)
 365                addattr_l(n, 1024, TCA_CAKE_FLOW_MODE, &flowmode,
 366                          sizeof(flowmode));
 367        if (overhead_set)
 368                addattr_l(n, 1024, TCA_CAKE_OVERHEAD, &overhead,
 369                          sizeof(overhead));
 370        if (overhead_override) {
 371                unsigned int zero = 0;
 372
 373                addattr_l(n, 1024, TCA_CAKE_RAW, &zero, sizeof(zero));
 374        }
 375        if (mpu > 0)
 376                addattr_l(n, 1024, TCA_CAKE_MPU, &mpu, sizeof(mpu));
 377        if (interval)
 378                addattr_l(n, 1024, TCA_CAKE_RTT, &interval, sizeof(interval));
 379        if (target)
 380                addattr_l(n, 1024, TCA_CAKE_TARGET, &target, sizeof(target));
 381        if (autorate != -1)
 382                addattr_l(n, 1024, TCA_CAKE_AUTORATE, &autorate,
 383                          sizeof(autorate));
 384        if (memlimit)
 385                addattr_l(n, 1024, TCA_CAKE_MEMORY, &memlimit,
 386                          sizeof(memlimit));
 387        if (fwmark)
 388                addattr_l(n, 1024, TCA_CAKE_FWMARK, &fwmark,
 389                          sizeof(fwmark));
 390        if (nat != -1)
 391                addattr_l(n, 1024, TCA_CAKE_NAT, &nat, sizeof(nat));
 392        if (wash != -1)
 393                addattr_l(n, 1024, TCA_CAKE_WASH, &wash, sizeof(wash));
 394        if (split_gso != -1)
 395                addattr_l(n, 1024, TCA_CAKE_SPLIT_GSO, &split_gso,
 396                          sizeof(split_gso));
 397        if (ingress != -1)
 398                addattr_l(n, 1024, TCA_CAKE_INGRESS, &ingress, sizeof(ingress));
 399        if (ack_filter != -1)
 400                addattr_l(n, 1024, TCA_CAKE_ACK_FILTER, &ack_filter,
 401                          sizeof(ack_filter));
 402
 403        tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
 404        return 0;
 405}
 406
 407static void cake_print_mode(unsigned int value, unsigned int max,
 408                            const char *key, const char **table)
 409{
 410        if (value < max && table[value]) {
 411                print_string(PRINT_ANY, key, "%s ", table[value]);
 412        } else {
 413                print_string(PRINT_JSON, key, NULL, "unknown");
 414                print_string(PRINT_FP, NULL, "(?%s?)", key);
 415        }
 416}
 417
 418static int cake_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 419{
 420        struct rtattr *tb[TCA_CAKE_MAX + 1];
 421        unsigned int interval = 0;
 422        unsigned int memlimit = 0;
 423        unsigned int fwmark = 0;
 424        __u64 bandwidth = 0;
 425        int ack_filter = 0;
 426        int split_gso = 0;
 427        int overhead = 0;
 428        int autorate = 0;
 429        int ingress = 0;
 430        int wash = 0;
 431        int raw = 0;
 432        int mpu = 0;
 433        int atm = 0;
 434        int nat = 0;
 435
 436        SPRINT_BUF(b2);
 437
 438        if (opt == NULL)
 439                return 0;
 440
 441        parse_rtattr_nested(tb, TCA_CAKE_MAX, opt);
 442
 443        if (tb[TCA_CAKE_BASE_RATE64] &&
 444            RTA_PAYLOAD(tb[TCA_CAKE_BASE_RATE64]) >= sizeof(bandwidth)) {
 445                bandwidth = rta_getattr_u64(tb[TCA_CAKE_BASE_RATE64]);
 446                if (bandwidth)
 447                        tc_print_rate(PRINT_ANY, "bandwidth", "bandwidth %s ",
 448                                      bandwidth);
 449                else
 450                        print_string(PRINT_ANY, "bandwidth", "bandwidth %s ",
 451                                     "unlimited");
 452        }
 453        if (tb[TCA_CAKE_AUTORATE] &&
 454                RTA_PAYLOAD(tb[TCA_CAKE_AUTORATE]) >= sizeof(__u32)) {
 455                autorate = rta_getattr_u32(tb[TCA_CAKE_AUTORATE]);
 456                if (autorate == 1)
 457                        print_string(PRINT_ANY, "autorate", "%s ",
 458                                     "autorate-ingress");
 459                else if (autorate)
 460                        print_string(PRINT_ANY, "autorate", "(?autorate?) ",
 461                                     "unknown");
 462        }
 463        if (tb[TCA_CAKE_DIFFSERV_MODE] &&
 464            RTA_PAYLOAD(tb[TCA_CAKE_DIFFSERV_MODE]) >= sizeof(__u32)) {
 465                cake_print_mode(rta_getattr_u32(tb[TCA_CAKE_DIFFSERV_MODE]),
 466                                CAKE_DIFFSERV_MAX, "diffserv", diffserv_names);
 467        }
 468        if (tb[TCA_CAKE_FLOW_MODE] &&
 469            RTA_PAYLOAD(tb[TCA_CAKE_FLOW_MODE]) >= sizeof(__u32)) {
 470                cake_print_mode(rta_getattr_u32(tb[TCA_CAKE_FLOW_MODE]),
 471                                CAKE_FLOW_MAX, "flowmode", flowmode_names);
 472        }
 473
 474        if (tb[TCA_CAKE_NAT] &&
 475            RTA_PAYLOAD(tb[TCA_CAKE_NAT]) >= sizeof(__u32)) {
 476                nat = rta_getattr_u32(tb[TCA_CAKE_NAT]);
 477        }
 478
 479        if (nat)
 480                print_string(PRINT_FP, NULL, "nat ", NULL);
 481        else
 482                print_string(PRINT_FP, NULL, "nonat ", NULL);
 483        print_bool(PRINT_JSON, "nat", NULL, nat);
 484
 485        if (tb[TCA_CAKE_WASH] &&
 486            RTA_PAYLOAD(tb[TCA_CAKE_WASH]) >= sizeof(__u32)) {
 487                wash = rta_getattr_u32(tb[TCA_CAKE_WASH]);
 488        }
 489        if (tb[TCA_CAKE_ATM] &&
 490            RTA_PAYLOAD(tb[TCA_CAKE_ATM]) >= sizeof(__u32)) {
 491                atm = rta_getattr_u32(tb[TCA_CAKE_ATM]);
 492        }
 493        if (tb[TCA_CAKE_OVERHEAD] &&
 494            RTA_PAYLOAD(tb[TCA_CAKE_OVERHEAD]) >= sizeof(__s32)) {
 495                overhead = *(__s32 *) RTA_DATA(tb[TCA_CAKE_OVERHEAD]);
 496        }
 497        if (tb[TCA_CAKE_MPU] &&
 498            RTA_PAYLOAD(tb[TCA_CAKE_MPU]) >= sizeof(__u32)) {
 499                mpu = rta_getattr_u32(tb[TCA_CAKE_MPU]);
 500        }
 501        if (tb[TCA_CAKE_INGRESS] &&
 502            RTA_PAYLOAD(tb[TCA_CAKE_INGRESS]) >= sizeof(__u32)) {
 503                ingress = rta_getattr_u32(tb[TCA_CAKE_INGRESS]);
 504        }
 505        if (tb[TCA_CAKE_ACK_FILTER] &&
 506            RTA_PAYLOAD(tb[TCA_CAKE_ACK_FILTER]) >= sizeof(__u32)) {
 507                ack_filter = rta_getattr_u32(tb[TCA_CAKE_ACK_FILTER]);
 508        }
 509        if (tb[TCA_CAKE_SPLIT_GSO] &&
 510            RTA_PAYLOAD(tb[TCA_CAKE_SPLIT_GSO]) >= sizeof(__u32)) {
 511                split_gso = rta_getattr_u32(tb[TCA_CAKE_SPLIT_GSO]);
 512        }
 513        if (tb[TCA_CAKE_RAW]) {
 514                raw = 1;
 515        }
 516        if (tb[TCA_CAKE_RTT] &&
 517            RTA_PAYLOAD(tb[TCA_CAKE_RTT]) >= sizeof(__u32)) {
 518                interval = rta_getattr_u32(tb[TCA_CAKE_RTT]);
 519        }
 520        if (tb[TCA_CAKE_MEMORY] &&
 521                RTA_PAYLOAD(tb[TCA_CAKE_MEMORY]) >= sizeof(__u32)) {
 522                memlimit = rta_getattr_u32(tb[TCA_CAKE_MEMORY]);
 523        }
 524        if (tb[TCA_CAKE_FWMARK] &&
 525            RTA_PAYLOAD(tb[TCA_CAKE_FWMARK]) >= sizeof(__u32)) {
 526                fwmark = rta_getattr_u32(tb[TCA_CAKE_FWMARK]);
 527        }
 528
 529        if (wash)
 530                print_string(PRINT_FP, NULL, "wash ", NULL);
 531        else
 532                print_string(PRINT_FP, NULL, "nowash ", NULL);
 533        print_bool(PRINT_JSON, "wash", NULL, wash);
 534
 535        if (ingress)
 536                print_string(PRINT_FP, NULL, "ingress ", NULL);
 537        print_bool(PRINT_JSON, "ingress", NULL, ingress);
 538
 539        if (ack_filter == CAKE_ACK_AGGRESSIVE)
 540                print_string(PRINT_ANY, "ack-filter", "ack-filter-%s ",
 541                             "aggressive");
 542        else if (ack_filter == CAKE_ACK_FILTER)
 543                print_string(PRINT_ANY, "ack-filter", "ack-filter ", "enabled");
 544        else
 545                print_string(PRINT_ANY, "ack-filter", "no-ack-filter ", "disabled");
 546
 547        if (split_gso)
 548                print_string(PRINT_FP, NULL, "split-gso ", NULL);
 549        else
 550                print_string(PRINT_FP, NULL, "no-split-gso ", NULL);
 551        print_bool(PRINT_JSON, "split_gso", NULL, split_gso);
 552
 553        if (interval)
 554                print_string(PRINT_FP, NULL, "rtt %s ",
 555                             sprint_time(interval, b2));
 556        print_uint(PRINT_JSON, "rtt", NULL, interval);
 557
 558        if (raw)
 559                print_string(PRINT_FP, NULL, "raw ", NULL);
 560        print_bool(PRINT_JSON, "raw", NULL, raw);
 561
 562        if (atm == CAKE_ATM_ATM)
 563                print_string(PRINT_ANY, "atm", "%s ", "atm");
 564        else if (atm == CAKE_ATM_PTM)
 565                print_string(PRINT_ANY, "atm", "%s ", "ptm");
 566        else if (!raw)
 567                print_string(PRINT_ANY, "atm", "%s ", "noatm");
 568
 569        print_int(PRINT_ANY, "overhead", "overhead %d ", overhead);
 570
 571        if (mpu)
 572                print_uint(PRINT_ANY, "mpu", "mpu %u ", mpu);
 573
 574        if (memlimit)
 575                print_size(PRINT_ANY, "memlimit", "memlimit %s ", memlimit);
 576
 577        if (fwmark)
 578                print_uint(PRINT_FP, NULL, "fwmark 0x%x ", fwmark);
 579        print_0xhex(PRINT_JSON, "fwmark", NULL, fwmark);
 580
 581        return 0;
 582}
 583
 584static void cake_print_json_tin(struct rtattr **tstat)
 585{
 586#define PRINT_TSTAT_JSON(type, name, attr) if (tstat[TCA_CAKE_TIN_STATS_ ## attr]) \
 587                print_u64(PRINT_JSON, name, NULL,                       \
 588                        rta_getattr_ ## type((struct rtattr *)          \
 589                                             tstat[TCA_CAKE_TIN_STATS_ ## attr]))
 590
 591        open_json_object(NULL);
 592        PRINT_TSTAT_JSON(u64, "threshold_rate", THRESHOLD_RATE64);
 593        PRINT_TSTAT_JSON(u64, "sent_bytes", SENT_BYTES64);
 594        PRINT_TSTAT_JSON(u32, "backlog_bytes", BACKLOG_BYTES);
 595        PRINT_TSTAT_JSON(u32, "target_us", TARGET_US);
 596        PRINT_TSTAT_JSON(u32, "interval_us", INTERVAL_US);
 597        PRINT_TSTAT_JSON(u32, "peak_delay_us", PEAK_DELAY_US);
 598        PRINT_TSTAT_JSON(u32, "avg_delay_us", AVG_DELAY_US);
 599        PRINT_TSTAT_JSON(u32, "base_delay_us", BASE_DELAY_US);
 600        PRINT_TSTAT_JSON(u32, "sent_packets", SENT_PACKETS);
 601        PRINT_TSTAT_JSON(u32, "way_indirect_hits", WAY_INDIRECT_HITS);
 602        PRINT_TSTAT_JSON(u32, "way_misses", WAY_MISSES);
 603        PRINT_TSTAT_JSON(u32, "way_collisions", WAY_COLLISIONS);
 604        PRINT_TSTAT_JSON(u32, "drops", DROPPED_PACKETS);
 605        PRINT_TSTAT_JSON(u32, "ecn_mark", ECN_MARKED_PACKETS);
 606        PRINT_TSTAT_JSON(u32, "ack_drops", ACKS_DROPPED_PACKETS);
 607        PRINT_TSTAT_JSON(u32, "sparse_flows", SPARSE_FLOWS);
 608        PRINT_TSTAT_JSON(u32, "bulk_flows", BULK_FLOWS);
 609        PRINT_TSTAT_JSON(u32, "unresponsive_flows", UNRESPONSIVE_FLOWS);
 610        PRINT_TSTAT_JSON(u32, "max_pkt_len", MAX_SKBLEN);
 611        PRINT_TSTAT_JSON(u32, "flow_quantum", FLOW_QUANTUM);
 612        close_json_object();
 613
 614#undef PRINT_TSTAT_JSON
 615}
 616
 617static int cake_print_xstats(struct qdisc_util *qu, FILE *f,
 618                             struct rtattr *xstats)
 619{
 620        struct rtattr *st[TCA_CAKE_STATS_MAX + 1];
 621        SPRINT_BUF(b1);
 622        int i;
 623
 624        if (xstats == NULL)
 625                return 0;
 626
 627#define GET_STAT_U32(attr) rta_getattr_u32(st[TCA_CAKE_STATS_ ## attr])
 628#define GET_STAT_S32(attr) (*(__s32 *)RTA_DATA(st[TCA_CAKE_STATS_ ## attr]))
 629#define GET_STAT_U64(attr) rta_getattr_u64(st[TCA_CAKE_STATS_ ## attr])
 630
 631        parse_rtattr_nested(st, TCA_CAKE_STATS_MAX, xstats);
 632
 633        if (st[TCA_CAKE_STATS_MEMORY_USED] &&
 634            st[TCA_CAKE_STATS_MEMORY_LIMIT]) {
 635                print_size(PRINT_FP, NULL, " memory used: %s",
 636                           GET_STAT_U32(MEMORY_USED));
 637
 638                print_size(PRINT_FP, NULL, " of %s\n",
 639                           GET_STAT_U32(MEMORY_LIMIT));
 640
 641                print_uint(PRINT_JSON, "memory_used", NULL,
 642                        GET_STAT_U32(MEMORY_USED));
 643                print_uint(PRINT_JSON, "memory_limit", NULL,
 644                        GET_STAT_U32(MEMORY_LIMIT));
 645        }
 646
 647        if (st[TCA_CAKE_STATS_CAPACITY_ESTIMATE64])
 648                tc_print_rate(PRINT_ANY, "capacity_estimate",
 649                              " capacity estimate: %s\n",
 650                              GET_STAT_U64(CAPACITY_ESTIMATE64));
 651
 652        if (st[TCA_CAKE_STATS_MIN_NETLEN] &&
 653            st[TCA_CAKE_STATS_MAX_NETLEN]) {
 654                print_uint(PRINT_ANY, "min_network_size",
 655                           " min/max network layer size: %12u",
 656                           GET_STAT_U32(MIN_NETLEN));
 657                print_uint(PRINT_ANY, "max_network_size",
 658                           " /%8u\n", GET_STAT_U32(MAX_NETLEN));
 659        }
 660
 661        if (st[TCA_CAKE_STATS_MIN_ADJLEN] &&
 662            st[TCA_CAKE_STATS_MAX_ADJLEN]) {
 663                print_uint(PRINT_ANY, "min_adj_size",
 664                           " min/max overhead-adjusted size: %8u",
 665                           GET_STAT_U32(MIN_ADJLEN));
 666                print_uint(PRINT_ANY, "max_adj_size",
 667                           " /%8u\n", GET_STAT_U32(MAX_ADJLEN));
 668        }
 669
 670        if (st[TCA_CAKE_STATS_AVG_NETOFF])
 671                print_uint(PRINT_ANY, "avg_hdr_offset",
 672                           " average network hdr offset: %12u\n\n",
 673                           GET_STAT_U32(AVG_NETOFF));
 674
 675        /* class stats */
 676        if (st[TCA_CAKE_STATS_DEFICIT])
 677                print_int(PRINT_ANY, "deficit", "  deficit %d",
 678                          GET_STAT_S32(DEFICIT));
 679        if (st[TCA_CAKE_STATS_COBALT_COUNT])
 680                print_uint(PRINT_ANY, "count", " count %u",
 681                           GET_STAT_U32(COBALT_COUNT));
 682
 683        if (st[TCA_CAKE_STATS_DROPPING] && GET_STAT_U32(DROPPING)) {
 684                print_bool(PRINT_ANY, "dropping", " dropping", true);
 685                if (st[TCA_CAKE_STATS_DROP_NEXT_US]) {
 686                        int drop_next = GET_STAT_S32(DROP_NEXT_US);
 687
 688                        if (drop_next < 0) {
 689                                print_string(PRINT_FP, NULL, " drop_next -%s",
 690                                        sprint_time(-drop_next, b1));
 691                        } else {
 692                                print_uint(PRINT_JSON, "drop_next", NULL,
 693                                        drop_next);
 694                                print_string(PRINT_FP, NULL, " drop_next %s",
 695                                        sprint_time(drop_next, b1));
 696                        }
 697                }
 698        }
 699
 700        if (st[TCA_CAKE_STATS_P_DROP]) {
 701                print_uint(PRINT_ANY, "blue_prob", " blue_prob %u",
 702                           GET_STAT_U32(P_DROP));
 703                if (st[TCA_CAKE_STATS_BLUE_TIMER_US]) {
 704                        int blue_timer = GET_STAT_S32(BLUE_TIMER_US);
 705
 706                        if (blue_timer < 0) {
 707                                print_string(PRINT_FP, NULL, " blue_timer -%s",
 708                                        sprint_time(blue_timer, b1));
 709                        } else {
 710                                print_uint(PRINT_JSON, "blue_timer", NULL,
 711                                        blue_timer);
 712                                print_string(PRINT_FP, NULL, " blue_timer %s",
 713                                        sprint_time(blue_timer, b1));
 714                        }
 715                }
 716        }
 717
 718#undef GET_STAT_U32
 719#undef GET_STAT_S32
 720#undef GET_STAT_U64
 721
 722        if (st[TCA_CAKE_STATS_TIN_STATS]) {
 723                struct rtattr *tstat[TC_CAKE_MAX_TINS][TCA_CAKE_TIN_STATS_MAX + 1];
 724                struct rtattr *tins[TC_CAKE_MAX_TINS + 1];
 725                int num_tins = 0;
 726
 727                parse_rtattr_nested(tins, TC_CAKE_MAX_TINS,
 728                                    st[TCA_CAKE_STATS_TIN_STATS]);
 729
 730                for (i = 1; i <= TC_CAKE_MAX_TINS && tins[i]; i++) {
 731                        parse_rtattr_nested(tstat[i-1], TCA_CAKE_TIN_STATS_MAX,
 732                                            tins[i]);
 733                        num_tins++;
 734                }
 735
 736                if (!num_tins)
 737                        return 0;
 738
 739                if (is_json_context()) {
 740                        open_json_array(PRINT_JSON, "tins");
 741                        for (i = 0; i < num_tins; i++)
 742                                cake_print_json_tin(tstat[i]);
 743                        close_json_array(PRINT_JSON, NULL);
 744
 745                        return 0;
 746                }
 747
 748
 749                switch (num_tins) {
 750                case 3:
 751                        fprintf(f, "                   Bulk  Best Effort        Voice\n");
 752                        break;
 753
 754                case 4:
 755                        fprintf(f, "                   Bulk  Best Effort        Video        Voice\n");
 756                        break;
 757
 758                default:
 759                        fprintf(f, "          ");
 760                        for (i = 0; i < num_tins; i++)
 761                                fprintf(f, "        Tin %u", i);
 762                        fprintf(f, "%s", _SL_);
 763                };
 764
 765#define GET_TSTAT(i, attr) (tstat[i][TCA_CAKE_TIN_STATS_ ## attr])
 766#define PRINT_TSTAT(name, attr, fmts, val)      do {            \
 767                        if (GET_TSTAT(0, attr)) {               \
 768                                fprintf(f, name);               \
 769                                for (i = 0; i < num_tins; i++)  \
 770                                        fprintf(f, " %12" fmts, val);   \
 771                                fprintf(f, "%s", _SL_);                 \
 772                        }                                               \
 773                } while (0)
 774
 775#define SPRINT_TSTAT(pfunc, type, name, attr) PRINT_TSTAT(              \
 776                        name, attr, "s", sprint_ ## pfunc(              \
 777                                rta_getattr_ ## type(GET_TSTAT(i, attr)), b1))
 778
 779#define PRINT_TSTAT_U32(name, attr)     PRINT_TSTAT(                    \
 780                        name, attr, "u", rta_getattr_u32(GET_TSTAT(i, attr)))
 781
 782#define PRINT_TSTAT_U64(name, attr)     PRINT_TSTAT(                    \
 783                        name, attr, "llu", rta_getattr_u64(GET_TSTAT(i, attr)))
 784
 785                if (GET_TSTAT(0, THRESHOLD_RATE64)) {
 786                        fprintf(f, "  thresh  ");
 787                        for (i = 0; i < num_tins; i++)
 788                                tc_print_rate(PRINT_FP, NULL, " %12s",
 789                                              rta_getattr_u64(GET_TSTAT(i, THRESHOLD_RATE64)));
 790                        fprintf(f, "%s", _SL_);
 791                }
 792
 793                SPRINT_TSTAT(time, u32, "  target  ", TARGET_US);
 794                SPRINT_TSTAT(time, u32, "  interval", INTERVAL_US);
 795                SPRINT_TSTAT(time, u32, "  pk_delay", PEAK_DELAY_US);
 796                SPRINT_TSTAT(time, u32, "  av_delay", AVG_DELAY_US);
 797                SPRINT_TSTAT(time, u32, "  sp_delay", BASE_DELAY_US);
 798                SPRINT_TSTAT(size, u32, "  backlog ", BACKLOG_BYTES);
 799
 800                PRINT_TSTAT_U32("  pkts    ", SENT_PACKETS);
 801                PRINT_TSTAT_U64("  bytes   ", SENT_BYTES64);
 802
 803                PRINT_TSTAT_U32("  way_inds", WAY_INDIRECT_HITS);
 804                PRINT_TSTAT_U32("  way_miss", WAY_MISSES);
 805                PRINT_TSTAT_U32("  way_cols", WAY_COLLISIONS);
 806                PRINT_TSTAT_U32("  drops   ", DROPPED_PACKETS);
 807                PRINT_TSTAT_U32("  marks   ", ECN_MARKED_PACKETS);
 808                PRINT_TSTAT_U32("  ack_drop", ACKS_DROPPED_PACKETS);
 809                PRINT_TSTAT_U32("  sp_flows", SPARSE_FLOWS);
 810                PRINT_TSTAT_U32("  bk_flows", BULK_FLOWS);
 811                PRINT_TSTAT_U32("  un_flows", UNRESPONSIVE_FLOWS);
 812                PRINT_TSTAT_U32("  max_len ", MAX_SKBLEN);
 813                PRINT_TSTAT_U32("  quantum ", FLOW_QUANTUM);
 814
 815#undef GET_STAT
 816#undef PRINT_TSTAT
 817#undef SPRINT_TSTAT
 818#undef PRINT_TSTAT_U32
 819#undef PRINT_TSTAT_U64
 820        }
 821        return 0;
 822}
 823
 824struct qdisc_util cake_qdisc_util = {
 825        .id             = "cake",
 826        .parse_qopt     = cake_parse_opt,
 827        .print_qopt     = cake_print_opt,
 828        .print_xstats   = cake_print_xstats,
 829};
 830