iproute2/tc/q_gred.c
<<
>>
Prefs
   1/*
   2 * q_gred.c             GRED.
   3 *
   4 *              This program is free software; you can redistribute it and/or
   5 *              modify it under the terms of the GNU General Public License
   6 *              as published by the Free Software Foundation; either version
   7 *              2 of the License, or (at your option) any later version.
   8 *
   9 * Authors:    J Hadi Salim(hadi@nortelnetworks.com)
  10 *             code ruthlessly ripped from
  11 *             Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  12 *
  13 */
  14
  15#include <stdio.h>
  16#include <stdlib.h>
  17#include <unistd.h>
  18#include <fcntl.h>
  19#include <sys/socket.h>
  20#include <netinet/in.h>
  21#include <arpa/inet.h>
  22#include <string.h>
  23#include <math.h>
  24
  25#include "utils.h"
  26#include "tc_util.h"
  27
  28#include "tc_red.h"
  29
  30
  31#if 0
  32#define DPRINTF(format, args...) fprintf(stderr, format, ##args)
  33#else
  34#define DPRINTF(format, args...)
  35#endif
  36
  37static void explain(void)
  38{
  39        fprintf(stderr,
  40                "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n"
  41                "           default DEFAULT_VQ [ grio ] [ limit BYTES ] [ecn] [harddrop]\n"
  42                "       tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n"
  43                "           min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n"
  44                "           [ probability PROBABILITY ] [ bandwidth KBPS ] [ecn] [harddrop]\n");
  45}
  46
  47static int init_gred(struct qdisc_util *qu, int argc, char **argv,
  48                     struct nlmsghdr *n)
  49{
  50
  51        struct rtattr *tail;
  52        struct tc_gred_sopt opt = { 0 };
  53        __u32 limit = 0;
  54
  55        opt.def_DP = MAX_DPs;
  56
  57        while (argc > 0) {
  58                DPRINTF(stderr, "init_gred: invoked with %s\n", *argv);
  59                if (strcmp(*argv, "vqs") == 0 ||
  60                    strcmp(*argv, "DPs") == 0) {
  61                        NEXT_ARG();
  62                        if (get_unsigned(&opt.DPs, *argv, 10)) {
  63                                fprintf(stderr, "Illegal \"vqs\"\n");
  64                                return -1;
  65                        } else if (opt.DPs > MAX_DPs) {
  66                                fprintf(stderr, "GRED: only %u VQs are currently supported\n",
  67                                        MAX_DPs);
  68                                return -1;
  69                        }
  70                } else if (strcmp(*argv, "default") == 0) {
  71                        if (opt.DPs == 0) {
  72                                fprintf(stderr, "\"default\" must be defined after \"vqs\"\n");
  73                                return -1;
  74                        }
  75                        NEXT_ARG();
  76                        if (get_unsigned(&opt.def_DP, *argv, 10)) {
  77                                fprintf(stderr, "Illegal \"default\"\n");
  78                                return -1;
  79                        } else if (opt.def_DP >= opt.DPs) {
  80                                fprintf(stderr, "\"default\" must be less than \"vqs\"\n");
  81                                return -1;
  82                        }
  83                } else if (strcmp(*argv, "grio") == 0) {
  84                        opt.grio = 1;
  85                } else if (strcmp(*argv, "limit") == 0) {
  86                        NEXT_ARG();
  87                        if (get_size(&limit, *argv)) {
  88                                fprintf(stderr, "Illegal \"limit\"\n");
  89                                return -1;
  90                        }
  91                } else if (strcmp(*argv, "ecn") == 0) {
  92                        opt.flags |= TC_RED_ECN;
  93                } else if (strcmp(*argv, "harddrop") == 0) {
  94                        opt.flags |= TC_RED_HARDDROP;
  95                } else if (strcmp(*argv, "help") == 0) {
  96                        explain();
  97                        return -1;
  98                } else {
  99                        fprintf(stderr, "What is \"%s\"?\n", *argv);
 100                        explain();
 101                        return -1;
 102                }
 103                argc--; argv++;
 104        }
 105
 106        if (!opt.DPs || opt.def_DP == MAX_DPs) {
 107                fprintf(stderr, "Illegal gred setup parameters\n");
 108                return -1;
 109        }
 110
 111        DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt.DPs, opt.def_DP);
 112        n->nlmsg_flags |= NLM_F_CREATE;
 113        tail = addattr_nest(n, 1024, TCA_OPTIONS);
 114        addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
 115        if (limit)
 116                addattr32(n, 1024, TCA_GRED_LIMIT, limit);
 117        addattr_nest_end(n, tail);
 118        return 0;
 119}
 120/*
 121^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 122*/
 123static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
 124{
 125        struct rtattr *tail, *entry, *vqs;
 126        int ok = 0;
 127        struct tc_gred_qopt opt = { 0 };
 128        unsigned int burst = 0;
 129        unsigned int avpkt = 0;
 130        unsigned int flags = 0;
 131        double probability = 0.02;
 132        unsigned int rate = 0;
 133        int parm;
 134        __u8 sbuf[256];
 135        __u32 max_P;
 136
 137        opt.DP = MAX_DPs;
 138
 139        while (argc > 0) {
 140                if (strcmp(*argv, "limit") == 0) {
 141                        NEXT_ARG();
 142                        if (get_size(&opt.limit, *argv)) {
 143                                fprintf(stderr, "Illegal \"limit\"\n");
 144                                return -1;
 145                        }
 146                        ok++;
 147                } else if (strcmp(*argv, "setup") == 0) {
 148                        if (ok) {
 149                                fprintf(stderr, "Illegal \"setup\"\n");
 150                                return -1;
 151                        }
 152                        return init_gred(qu, argc-1, argv+1, n);
 153                } else if (strcmp(*argv, "min") == 0) {
 154                        NEXT_ARG();
 155                        if (get_size(&opt.qth_min, *argv)) {
 156                                fprintf(stderr, "Illegal \"min\"\n");
 157                                return -1;
 158                        }
 159                        ok++;
 160                } else if (strcmp(*argv, "max") == 0) {
 161                        NEXT_ARG();
 162                        if (get_size(&opt.qth_max, *argv)) {
 163                                fprintf(stderr, "Illegal \"max\"\n");
 164                                return -1;
 165                        }
 166                        ok++;
 167                } else if (strcmp(*argv, "vq") == 0 ||
 168                           strcmp(*argv, "DP") == 0) {
 169                        NEXT_ARG();
 170                        if (get_unsigned(&opt.DP, *argv, 10)) {
 171                                fprintf(stderr, "Illegal \"vq\"\n");
 172                                return -1;
 173                        } else if (opt.DP >= MAX_DPs) {
 174                                fprintf(stderr, "GRED: only %u VQs are currently supported\n",
 175                                        MAX_DPs);
 176                                return -1;
 177                        } /* need a better error check */
 178                        ok++;
 179                } else if (strcmp(*argv, "burst") == 0) {
 180                        NEXT_ARG();
 181                        if (get_unsigned(&burst, *argv, 0)) {
 182                                fprintf(stderr, "Illegal \"burst\"\n");
 183                                return -1;
 184                        }
 185                        ok++;
 186                } else if (strcmp(*argv, "avpkt") == 0) {
 187                        NEXT_ARG();
 188                        if (get_size(&avpkt, *argv)) {
 189                                fprintf(stderr, "Illegal \"avpkt\"\n");
 190                                return -1;
 191                        }
 192                        ok++;
 193                } else if (strcmp(*argv, "probability") == 0) {
 194                        NEXT_ARG();
 195                        if (sscanf(*argv, "%lg", &probability) != 1) {
 196                                fprintf(stderr, "Illegal \"probability\"\n");
 197                                return -1;
 198                        }
 199                        ok++;
 200                } else if (strcmp(*argv, "prio") == 0) {
 201                        NEXT_ARG();
 202                        opt.prio = strtol(*argv, (char **)NULL, 10);
 203                        /* some error check here */
 204                        ok++;
 205                } else if (strcmp(*argv, "bandwidth") == 0) {
 206                        NEXT_ARG();
 207                        if (strchr(*argv, '%')) {
 208                                if (get_percent_rate(&rate, *argv, dev)) {
 209                                        fprintf(stderr, "Illegal \"bandwidth\"\n");
 210                                        return -1;
 211                                }
 212                        } else if (get_rate(&rate, *argv)) {
 213                                fprintf(stderr, "Illegal \"bandwidth\"\n");
 214                                return -1;
 215                        }
 216                        ok++;
 217                } else if (strcmp(*argv, "ecn") == 0) {
 218                        flags |= TC_RED_ECN;
 219                } else if (strcmp(*argv, "harddrop") == 0) {
 220                        flags |= TC_RED_HARDDROP;
 221                } else if (strcmp(*argv, "help") == 0) {
 222                        explain();
 223                        return -1;
 224                } else {
 225                        fprintf(stderr, "What is \"%s\"?\n", *argv);
 226                        explain();
 227                        return -1;
 228                }
 229                argc--; argv++;
 230        }
 231
 232        if (!ok) {
 233                explain();
 234                return -1;
 235        }
 236        if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
 237            !avpkt) {
 238                fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
 239                return -1;
 240        }
 241        if (!burst) {
 242                burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
 243                fprintf(stderr, "GRED: set burst to %u\n", burst);
 244        }
 245        if (!rate) {
 246                get_rate(&rate, "10Mbit");
 247                fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
 248        }
 249        if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
 250                fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
 251                return -1;
 252        }
 253        if (parm >= 10)
 254                fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
 255                    burst);
 256        opt.Wlog = parm;
 257        if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
 258                fprintf(stderr, "GRED: failed to calculate probability.\n");
 259                return -1;
 260        }
 261        opt.Plog = parm;
 262        if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
 263            {
 264                fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
 265                return -1;
 266        }
 267        opt.Scell_log = parm;
 268
 269        tail = addattr_nest(n, 1024, TCA_OPTIONS);
 270        addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
 271        addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
 272        max_P = probability * pow(2, 32);
 273        addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
 274
 275        vqs = addattr_nest(n, 1024, TCA_GRED_VQ_LIST);
 276        entry = addattr_nest(n, 1024, TCA_GRED_VQ_ENTRY);
 277        addattr32(n, 1024, TCA_GRED_VQ_DP, opt.DP);
 278        addattr32(n, 1024, TCA_GRED_VQ_FLAGS, flags);
 279        addattr_nest_end(n, entry);
 280        addattr_nest_end(n, vqs);
 281
 282        addattr_nest_end(n, tail);
 283        return 0;
 284}
 285
 286struct tc_gred_info {
 287        bool    flags_present;
 288        __u64   bytes;
 289        __u32   packets;
 290        __u32   backlog;
 291        __u32   prob_drop;
 292        __u32   prob_mark;
 293        __u32   forced_drop;
 294        __u32   forced_mark;
 295        __u32   pdrop;
 296        __u32   other;
 297        __u32   flags;
 298};
 299
 300static void
 301gred_parse_vqs(struct tc_gred_info *info, struct rtattr *vqs)
 302{
 303        int rem = RTA_PAYLOAD(vqs);
 304        unsigned int offset = 0;
 305
 306        while (rem > offset) {
 307                struct rtattr *tb_entry[TCA_GRED_VQ_ENTRY_MAX + 1] = {};
 308                struct rtattr *tb[TCA_GRED_VQ_MAX + 1] = {};
 309                struct rtattr *entry;
 310                unsigned int len;
 311                unsigned int dp;
 312
 313                entry = RTA_DATA(vqs) + offset;
 314
 315                parse_rtattr(tb_entry, TCA_GRED_VQ_ENTRY_MAX, entry,
 316                             rem - offset);
 317                len = RTA_LENGTH(RTA_PAYLOAD(entry));
 318                offset += len;
 319
 320                if (!tb_entry[TCA_GRED_VQ_ENTRY]) {
 321                        fprintf(stderr,
 322                                "ERROR: Failed to parse Virtual Queue entry\n");
 323                        continue;
 324                }
 325
 326                parse_rtattr_nested(tb, TCA_GRED_VQ_MAX,
 327                                    tb_entry[TCA_GRED_VQ_ENTRY]);
 328
 329                if (!tb[TCA_GRED_VQ_DP]) {
 330                        fprintf(stderr,
 331                                "ERROR: Virtual Queue without DP attribute\n");
 332                        continue;
 333                }
 334
 335                dp = rta_getattr_u32(tb[TCA_GRED_VQ_DP]);
 336
 337                if (tb[TCA_GRED_VQ_STAT_BYTES])
 338                        info[dp].bytes =
 339                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BYTES]);
 340                if (tb[TCA_GRED_VQ_STAT_PACKETS])
 341                        info[dp].packets =
 342                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PACKETS]);
 343                if (tb[TCA_GRED_VQ_STAT_BACKLOG])
 344                        info[dp].backlog =
 345                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_BACKLOG]);
 346                if (tb[TCA_GRED_VQ_STAT_PROB_DROP])
 347                        info[dp].prob_drop =
 348                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PROB_DROP]);
 349                if (tb[TCA_GRED_VQ_STAT_PROB_MARK])
 350                        info[dp].prob_mark =
 351                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PROB_MARK]);
 352                if (tb[TCA_GRED_VQ_STAT_FORCED_DROP])
 353                        info[dp].forced_drop =
 354                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_FORCED_DROP]);
 355                if (tb[TCA_GRED_VQ_STAT_FORCED_MARK])
 356                        info[dp].forced_mark =
 357                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_FORCED_MARK]);
 358                if (tb[TCA_GRED_VQ_STAT_PDROP])
 359                        info[dp].pdrop =
 360                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_PDROP]);
 361                if (tb[TCA_GRED_VQ_STAT_OTHER])
 362                        info[dp].other =
 363                                rta_getattr_u32(tb[TCA_GRED_VQ_STAT_OTHER]);
 364                info[dp].flags_present = !!tb[TCA_GRED_VQ_FLAGS];
 365                if (tb[TCA_GRED_VQ_FLAGS])
 366                        info[dp].flags =
 367                                rta_getattr_u32(tb[TCA_GRED_VQ_FLAGS]);
 368        }
 369}
 370
 371static void
 372gred_print_stats(struct tc_gred_info *info, struct tc_gred_qopt *qopt)
 373{
 374        __u64 bytes = info ? info->bytes : qopt->bytesin;
 375
 376        SPRINT_BUF(b1);
 377
 378        if (!is_json_context())
 379                printf("\n  Queue size: ");
 380
 381        print_uint(PRINT_JSON, "qave", NULL, qopt->qave);
 382        print_string(PRINT_FP, NULL, "average %s ",
 383                     sprint_size(qopt->qave, b1));
 384
 385        print_uint(PRINT_JSON, "backlog", NULL, qopt->backlog);
 386        print_string(PRINT_FP, NULL, "current %s ",
 387                     sprint_size(qopt->backlog, b1));
 388
 389        if (!is_json_context())
 390                printf("\n  Dropped packets: ");
 391
 392        if (info) {
 393                print_uint(PRINT_ANY, "forced_drop", "forced %u ",
 394                           info->forced_drop);
 395                print_uint(PRINT_ANY, "prob_drop", "early %u ",
 396                           info->prob_drop);
 397                print_uint(PRINT_ANY, "pdrop", "pdrop %u ", info->pdrop);
 398                print_uint(PRINT_ANY, "other", "other %u ", info->other);
 399
 400                if (!is_json_context())
 401                        printf("\n  Marked packets: ");
 402                print_uint(PRINT_ANY, "forced_mark", "forced %u ",
 403                           info->forced_mark);
 404                print_uint(PRINT_ANY, "prob_mark", "early %u ",
 405                           info->prob_mark);
 406        } else {
 407                print_uint(PRINT_ANY, "forced_drop", "forced %u ",
 408                           qopt->forced);
 409                print_uint(PRINT_ANY, "prob_drop", "early %u ", qopt->early);
 410                print_uint(PRINT_ANY, "pdrop", "pdrop %u ", qopt->pdrop);
 411                print_uint(PRINT_ANY, "other", "other %u ", qopt->other);
 412        }
 413
 414        if (!is_json_context())
 415                printf("\n  Total packets: ");
 416
 417        print_uint(PRINT_ANY, "packets", "%u ", qopt->packets);
 418
 419        print_uint(PRINT_JSON, "bytes", NULL, bytes);
 420        print_string(PRINT_FP, NULL, "(%s) ", sprint_size(bytes, b1));
 421}
 422
 423static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 424{
 425        struct tc_gred_info infos[MAX_DPs] = {};
 426        struct rtattr *tb[TCA_GRED_MAX + 1];
 427        struct tc_gred_sopt *sopt;
 428        struct tc_gred_qopt *qopt;
 429        bool vq_info = false;
 430        __u32 *max_p = NULL;
 431        __u32 *limit = NULL;
 432        unsigned int i;
 433
 434        SPRINT_BUF(b1);
 435
 436        if (opt == NULL)
 437                return 0;
 438
 439        parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
 440
 441        if (tb[TCA_GRED_PARMS] == NULL)
 442                return -1;
 443
 444        if (tb[TCA_GRED_MAX_P] &&
 445            RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
 446                max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
 447
 448        if (tb[TCA_GRED_LIMIT] &&
 449            RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
 450                limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
 451
 452        sopt = RTA_DATA(tb[TCA_GRED_DPS]);
 453        qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
 454        if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
 455            RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
 456                fprintf(f, "\n GRED received message smaller than expected\n");
 457                return -1;
 458        }
 459
 460        if (tb[TCA_GRED_VQ_LIST]) {
 461                gred_parse_vqs(infos, tb[TCA_GRED_VQ_LIST]);
 462                vq_info = true;
 463        }
 464
 465        print_uint(PRINT_ANY, "dp_cnt", "vqs %u ", sopt->DPs);
 466        print_uint(PRINT_ANY, "dp_default", "default %u ", sopt->def_DP);
 467
 468        if (sopt->grio)
 469                print_bool(PRINT_ANY, "grio", "grio ", true);
 470        else
 471                print_bool(PRINT_ANY, "grio", NULL, false);
 472
 473        if (limit) {
 474                print_uint(PRINT_JSON, "limit", NULL, *limit);
 475                print_string(PRINT_FP, NULL, "limit %s ",
 476                             sprint_size(*limit, b1));
 477        }
 478
 479        tc_red_print_flags(sopt->flags);
 480
 481        open_json_array(PRINT_JSON, "vqs");
 482        for (i = 0; i < MAX_DPs; i++, qopt++) {
 483                if (qopt->DP >= MAX_DPs)
 484                        continue;
 485
 486                open_json_object(NULL);
 487
 488                print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP);
 489                print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio);
 490
 491                print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
 492                print_string(PRINT_FP, NULL, "limit %s ",
 493                             sprint_size(qopt->limit, b1));
 494
 495                print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
 496                print_string(PRINT_FP, NULL, "min %s ",
 497                             sprint_size(qopt->qth_min, b1));
 498
 499                print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
 500                print_string(PRINT_FP, NULL, "max %s ",
 501                             sprint_size(qopt->qth_max, b1));
 502
 503                if (infos[i].flags_present)
 504                        tc_red_print_flags(infos[i].flags);
 505
 506                if (show_details) {
 507                        print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
 508                        if (max_p)
 509                                print_float(PRINT_ANY, "probability",
 510                                            "probability %lg ",
 511                                            max_p[i] / pow(2, 32));
 512                        else
 513                                print_uint(PRINT_ANY, "Plog", "Plog %u ",
 514                                           qopt->Plog);
 515                        print_uint(PRINT_ANY, "Scell_log", "Scell_log %u ",
 516                                   qopt->Scell_log);
 517                }
 518                if (show_stats)
 519                        gred_print_stats(vq_info ? &infos[i] : NULL, qopt);
 520                close_json_object();
 521        }
 522        close_json_array(PRINT_JSON, "vqs");
 523        return 0;
 524}
 525
 526struct qdisc_util gred_qdisc_util = {
 527        .id             = "gred",
 528        .parse_qopt     = gred_parse_opt,
 529        .print_qopt     = gred_print_opt,
 530};
 531