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        if (!is_json_context())
 377                printf("\n  Queue size: ");
 378
 379        print_size(PRINT_ANY, "qave", "average %s ", qopt->qave);
 380        print_size(PRINT_ANY, "backlog", "current %s ", qopt->backlog);
 381
 382        if (!is_json_context())
 383                printf("\n  Dropped packets: ");
 384
 385        if (info) {
 386                print_uint(PRINT_ANY, "forced_drop", "forced %u ",
 387                           info->forced_drop);
 388                print_uint(PRINT_ANY, "prob_drop", "early %u ",
 389                           info->prob_drop);
 390                print_uint(PRINT_ANY, "pdrop", "pdrop %u ", info->pdrop);
 391                print_uint(PRINT_ANY, "other", "other %u ", info->other);
 392
 393                if (!is_json_context())
 394                        printf("\n  Marked packets: ");
 395                print_uint(PRINT_ANY, "forced_mark", "forced %u ",
 396                           info->forced_mark);
 397                print_uint(PRINT_ANY, "prob_mark", "early %u ",
 398                           info->prob_mark);
 399        } else {
 400                print_uint(PRINT_ANY, "forced_drop", "forced %u ",
 401                           qopt->forced);
 402                print_uint(PRINT_ANY, "prob_drop", "early %u ", qopt->early);
 403                print_uint(PRINT_ANY, "pdrop", "pdrop %u ", qopt->pdrop);
 404                print_uint(PRINT_ANY, "other", "other %u ", qopt->other);
 405        }
 406
 407        if (!is_json_context())
 408                printf("\n  Total packets: ");
 409
 410        print_uint(PRINT_ANY, "packets", "%u ", qopt->packets);
 411        print_size(PRINT_ANY, "bytes", "(%s) ", bytes);
 412}
 413
 414static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 415{
 416        struct tc_gred_info infos[MAX_DPs] = {};
 417        struct rtattr *tb[TCA_GRED_MAX + 1];
 418        struct tc_gred_sopt *sopt;
 419        struct tc_gred_qopt *qopt;
 420        bool vq_info = false;
 421        __u32 *max_p = NULL;
 422        __u32 *limit = NULL;
 423        unsigned int i;
 424
 425        if (opt == NULL)
 426                return 0;
 427
 428        parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
 429
 430        if (tb[TCA_GRED_PARMS] == NULL)
 431                return -1;
 432
 433        if (tb[TCA_GRED_MAX_P] &&
 434            RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
 435                max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
 436
 437        if (tb[TCA_GRED_LIMIT] &&
 438            RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
 439                limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
 440
 441        sopt = RTA_DATA(tb[TCA_GRED_DPS]);
 442        qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
 443        if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
 444            RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
 445                fprintf(f, "\n GRED received message smaller than expected\n");
 446                return -1;
 447        }
 448
 449        if (tb[TCA_GRED_VQ_LIST]) {
 450                gred_parse_vqs(infos, tb[TCA_GRED_VQ_LIST]);
 451                vq_info = true;
 452        }
 453
 454        print_uint(PRINT_ANY, "dp_cnt", "vqs %u ", sopt->DPs);
 455        print_uint(PRINT_ANY, "dp_default", "default %u ", sopt->def_DP);
 456
 457        if (sopt->grio)
 458                print_bool(PRINT_ANY, "grio", "grio ", true);
 459        else
 460                print_bool(PRINT_ANY, "grio", NULL, false);
 461
 462        if (limit)
 463                print_size(PRINT_ANY, "limit", "limit %s ", *limit);
 464
 465        tc_red_print_flags(sopt->flags);
 466
 467        open_json_array(PRINT_JSON, "vqs");
 468        for (i = 0; i < MAX_DPs; i++, qopt++) {
 469                if (qopt->DP >= MAX_DPs)
 470                        continue;
 471
 472                open_json_object(NULL);
 473
 474                print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP);
 475                print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio);
 476                print_size(PRINT_ANY, "limit", "limit %s ", qopt->limit);
 477                print_size(PRINT_ANY, "min", "min %s ", qopt->qth_min);
 478                print_size(PRINT_ANY, "max", "max %s ", qopt->qth_max);
 479
 480                if (infos[i].flags_present)
 481                        tc_red_print_flags(infos[i].flags);
 482
 483                if (show_details) {
 484                        print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
 485                        if (max_p)
 486                                print_float(PRINT_ANY, "probability",
 487                                            "probability %lg ",
 488                                            max_p[i] / pow(2, 32));
 489                        else
 490                                print_uint(PRINT_ANY, "Plog", "Plog %u ",
 491                                           qopt->Plog);
 492                        print_uint(PRINT_ANY, "Scell_log", "Scell_log %u ",
 493                                   qopt->Scell_log);
 494                }
 495                if (show_stats)
 496                        gred_print_stats(vq_info ? &infos[i] : NULL, qopt);
 497                close_json_object();
 498        }
 499        close_json_array(PRINT_JSON, "vqs");
 500        return 0;
 501}
 502
 503struct qdisc_util gred_qdisc_util = {
 504        .id             = "gred",
 505        .parse_qopt     = gred_parse_opt,
 506        .print_qopt     = gred_print_opt,
 507};
 508