iproute2/tc/q_fq_pie.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Flow Queue PIE
   4 *
   5 * Copyright (C) 2019 Mohit P. Tahiliani <tahiliani@nitk.edu.in>
   6 * Copyright (C) 2019 Sachin D. Patil <sdp.sachin@gmail.com>
   7 * Copyright (C) 2019 V. Saicharan <vsaicharan1998@gmail.com>
   8 * Copyright (C) 2019 Mohit Bhasi <mohitbhasi1998@gmail.com>
   9 * Copyright (C) 2019 Leslie Monis <lesliemonis@gmail.com>
  10 * Copyright (C) 2019 Gautam Ramakrishnan <gautamramk@gmail.com>
  11 */
  12
  13#include <stdio.h>
  14#include <stdlib.h>
  15#include <unistd.h>
  16#include <fcntl.h>
  17#include <sys/socket.h>
  18#include <netinet/in.h>
  19#include <arpa/inet.h>
  20#include <string.h>
  21
  22#include "utils.h"
  23#include "tc_util.h"
  24
  25static void explain(void)
  26{
  27        fprintf(stderr,
  28                "Usage: ... fq_pie [ limit PACKETS ] [ flows NUMBER ]\n"
  29                "                  [ target TIME ] [ tupdate TIME ]\n"
  30                "                  [ alpha NUMBER ] [ beta NUMBER ]\n"
  31                "                  [ quantum BYTES ] [ memory_limit BYTES ]\n"
  32                "                  [ ecn_prob PERCENTAGE ] [ [no]ecn ]\n"
  33                "                  [ [no]bytemode ] [ [no_]dq_rate_estimator ]\n");
  34}
  35
  36#define ALPHA_MAX 32
  37#define BETA_MAX 32
  38
  39static int fq_pie_parse_opt(struct qdisc_util *qu, int argc, char **argv,
  40                            struct nlmsghdr *n, const char *dev)
  41{
  42        unsigned int limit = 0;
  43        unsigned int flows = 0;
  44        unsigned int target = 0;
  45        unsigned int tupdate = 0;
  46        unsigned int alpha = 0;
  47        unsigned int beta = 0;
  48        unsigned int quantum = 0;
  49        unsigned int memory_limit = 0;
  50        unsigned int ecn_prob = 0;
  51        int ecn = -1;
  52        int bytemode = -1;
  53        int dq_rate_estimator = -1;
  54        struct rtattr *tail;
  55
  56        while (argc > 0) {
  57                if (strcmp(*argv, "limit") == 0) {
  58                        NEXT_ARG();
  59                        if (get_unsigned(&limit, *argv, 0)) {
  60                                fprintf(stderr, "Illegal \"limit\"\n");
  61                                return -1;
  62                        }
  63                } else if (strcmp(*argv, "flows") == 0) {
  64                        NEXT_ARG();
  65                        if (get_unsigned(&flows, *argv, 0)) {
  66                                fprintf(stderr, "Illegal \"flows\"\n");
  67                                return -1;
  68                        }
  69                } else if (strcmp(*argv, "target") == 0) {
  70                        NEXT_ARG();
  71                        if (get_time(&target, *argv)) {
  72                                fprintf(stderr, "Illegal \"target\"\n");
  73                                return -1;
  74                        }
  75                } else if (strcmp(*argv, "tupdate") == 0) {
  76                        NEXT_ARG();
  77                        if (get_time(&tupdate, *argv)) {
  78                                fprintf(stderr, "Illegal \"tupdate\"\n");
  79                                return -1;
  80                        }
  81                } else if (strcmp(*argv, "alpha") == 0) {
  82                        NEXT_ARG();
  83                        if (get_unsigned(&alpha, *argv, 0) ||
  84                            alpha > ALPHA_MAX) {
  85                                fprintf(stderr, "Illegal \"alpha\"\n");
  86                                return -1;
  87                        }
  88                } else if (strcmp(*argv, "beta") == 0) {
  89                        NEXT_ARG();
  90                        if (get_unsigned(&beta, *argv, 0) ||
  91                            beta > BETA_MAX) {
  92                                fprintf(stderr, "Illegal \"beta\"\n");
  93                                return -1;
  94                        }
  95                } else if (strcmp(*argv, "quantum") == 0) {
  96                        NEXT_ARG();
  97                        if (get_size(&quantum, *argv)) {
  98                                fprintf(stderr, "Illegal \"quantum\"\n");
  99                                return -1;
 100                        }
 101                } else if (strcmp(*argv, "memory_limit") == 0) {
 102                        NEXT_ARG();
 103                        if (get_size(&memory_limit, *argv)) {
 104                                fprintf(stderr, "Illegal \"memory_limit\"\n");
 105                                return -1;
 106                        }
 107                } else if (strcmp(*argv, "ecn_prob") == 0) {
 108                        NEXT_ARG();
 109                        if (get_unsigned(&ecn_prob, *argv, 0) ||
 110                            ecn_prob >= 100) {
 111                                fprintf(stderr, "Illegal \"ecn_prob\"\n");
 112                                return -1;
 113                        }
 114                } else if (strcmp(*argv, "ecn") == 0) {
 115                        ecn = 1;
 116                } else if (strcmp(*argv, "noecn") == 0) {
 117                        ecn = 0;
 118                } else if (strcmp(*argv, "bytemode") == 0) {
 119                        bytemode = 1;
 120                } else if (strcmp(*argv, "nobytemode") == 0) {
 121                        bytemode = 0;
 122                } else if (strcmp(*argv, "dq_rate_estimator") == 0) {
 123                        dq_rate_estimator = 1;
 124                } else if (strcmp(*argv, "no_dq_rate_estimator") == 0) {
 125                        dq_rate_estimator = 0;
 126                } else if (strcmp(*argv, "help") == 0) {
 127                        explain();
 128                        return -1;
 129                } else {
 130                        fprintf(stderr, "What is \"%s\"?\n", *argv);
 131                        explain();
 132                        return -1;
 133                }
 134
 135                argc--;
 136                argv++;
 137        }
 138
 139        tail = addattr_nest(n, 1024, TCA_OPTIONS | NLA_F_NESTED);
 140        if (limit)
 141                addattr_l(n, 1024, TCA_FQ_PIE_LIMIT, &limit, sizeof(limit));
 142        if (flows)
 143                addattr_l(n, 1024, TCA_FQ_PIE_FLOWS, &flows, sizeof(flows));
 144        if (target)
 145                addattr_l(n, 1024, TCA_FQ_PIE_TARGET, &target, sizeof(target));
 146        if (tupdate)
 147                addattr_l(n, 1024, TCA_FQ_PIE_TUPDATE, &tupdate,
 148                          sizeof(tupdate));
 149        if (alpha)
 150                addattr_l(n, 1024, TCA_FQ_PIE_ALPHA, &alpha, sizeof(alpha));
 151        if (beta)
 152                addattr_l(n, 1024, TCA_FQ_PIE_BETA, &beta, sizeof(beta));
 153        if (quantum)
 154                addattr_l(n, 1024, TCA_FQ_PIE_QUANTUM, &quantum,
 155                          sizeof(quantum));
 156        if (memory_limit)
 157                addattr_l(n, 1024, TCA_FQ_PIE_MEMORY_LIMIT, &memory_limit,
 158                          sizeof(memory_limit));
 159        if (ecn_prob)
 160                addattr_l(n, 1024, TCA_FQ_PIE_ECN_PROB, &ecn_prob,
 161                          sizeof(ecn_prob));
 162        if (ecn != -1)
 163                addattr_l(n, 1024, TCA_FQ_PIE_ECN, &ecn, sizeof(ecn));
 164        if (bytemode != -1)
 165                addattr_l(n, 1024, TCA_FQ_PIE_BYTEMODE, &bytemode,
 166                          sizeof(bytemode));
 167        if (dq_rate_estimator != -1)
 168                addattr_l(n, 1024, TCA_FQ_PIE_DQ_RATE_ESTIMATOR,
 169                          &dq_rate_estimator, sizeof(dq_rate_estimator));
 170        addattr_nest_end(n, tail);
 171
 172        return 0;
 173}
 174
 175static int fq_pie_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 176{
 177        struct rtattr *tb[TCA_FQ_PIE_MAX + 1];
 178        unsigned int limit = 0;
 179        unsigned int flows = 0;
 180        unsigned int target = 0;
 181        unsigned int tupdate = 0;
 182        unsigned int alpha = 0;
 183        unsigned int beta = 0;
 184        unsigned int quantum = 0;
 185        unsigned int memory_limit = 0;
 186        unsigned int ecn_prob = 0;
 187        int ecn = -1;
 188        int bytemode = -1;
 189        int dq_rate_estimator = -1;
 190
 191        SPRINT_BUF(b1);
 192
 193        if (opt == NULL)
 194                return 0;
 195
 196        parse_rtattr_nested(tb, TCA_FQ_PIE_MAX, opt);
 197
 198        if (tb[TCA_FQ_PIE_LIMIT] &&
 199            RTA_PAYLOAD(tb[TCA_FQ_PIE_LIMIT]) >= sizeof(__u32)) {
 200                limit = rta_getattr_u32(tb[TCA_FQ_PIE_LIMIT]);
 201                print_uint(PRINT_ANY, "limit", "limit %up ", limit);
 202        }
 203        if (tb[TCA_FQ_PIE_FLOWS] &&
 204            RTA_PAYLOAD(tb[TCA_FQ_PIE_FLOWS]) >= sizeof(__u32)) {
 205                flows = rta_getattr_u32(tb[TCA_FQ_PIE_FLOWS]);
 206                print_uint(PRINT_ANY, "flows", "flows %u ", flows);
 207        }
 208        if (tb[TCA_FQ_PIE_TARGET] &&
 209            RTA_PAYLOAD(tb[TCA_FQ_PIE_TARGET]) >= sizeof(__u32)) {
 210                target = rta_getattr_u32(tb[TCA_FQ_PIE_TARGET]);
 211                print_uint(PRINT_JSON, "target", NULL, target);
 212                print_string(PRINT_FP, NULL, "target %s ",
 213                             sprint_time(target, b1));
 214        }
 215        if (tb[TCA_FQ_PIE_TUPDATE] &&
 216            RTA_PAYLOAD(tb[TCA_FQ_PIE_TUPDATE]) >= sizeof(__u32)) {
 217                tupdate = rta_getattr_u32(tb[TCA_FQ_PIE_TUPDATE]);
 218                print_uint(PRINT_JSON, "tupdate", NULL, tupdate);
 219                print_string(PRINT_FP, NULL, "tupdate %s ",
 220                             sprint_time(tupdate, b1));
 221        }
 222        if (tb[TCA_FQ_PIE_ALPHA] &&
 223            RTA_PAYLOAD(tb[TCA_FQ_PIE_ALPHA]) >= sizeof(__u32)) {
 224                alpha = rta_getattr_u32(tb[TCA_FQ_PIE_ALPHA]);
 225                print_uint(PRINT_ANY, "alpha", "alpha %u ", alpha);
 226        }
 227        if (tb[TCA_FQ_PIE_BETA] &&
 228            RTA_PAYLOAD(tb[TCA_FQ_PIE_BETA]) >= sizeof(__u32)) {
 229                beta = rta_getattr_u32(tb[TCA_FQ_PIE_BETA]);
 230                print_uint(PRINT_ANY, "beta", "beta %u ", beta);
 231        }
 232        if (tb[TCA_FQ_PIE_QUANTUM] &&
 233            RTA_PAYLOAD(tb[TCA_FQ_PIE_QUANTUM]) >= sizeof(__u32)) {
 234                quantum = rta_getattr_u32(tb[TCA_FQ_PIE_QUANTUM]);
 235                print_uint(PRINT_JSON, "quantum", NULL, quantum);
 236                print_string(PRINT_FP, NULL, "quantum %s ",
 237                             sprint_size(quantum, b1));
 238        }
 239        if (tb[TCA_FQ_PIE_MEMORY_LIMIT] &&
 240            RTA_PAYLOAD(tb[TCA_FQ_PIE_MEMORY_LIMIT]) >= sizeof(__u32)) {
 241                memory_limit = rta_getattr_u32(tb[TCA_FQ_PIE_MEMORY_LIMIT]);
 242                print_uint(PRINT_JSON, "memory_limit", NULL, memory_limit);
 243                print_string(PRINT_FP, NULL, "memory_limit %s ",
 244                             sprint_size(memory_limit, b1));
 245        }
 246        if (tb[TCA_FQ_PIE_ECN_PROB] &&
 247            RTA_PAYLOAD(tb[TCA_FQ_PIE_ECN_PROB]) >= sizeof(__u32)) {
 248                ecn_prob = rta_getattr_u32(tb[TCA_FQ_PIE_ECN_PROB]);
 249                print_uint(PRINT_ANY, "ecn_prob", "ecn_prob %u ", ecn_prob);
 250        }
 251        if (tb[TCA_FQ_PIE_ECN] &&
 252            RTA_PAYLOAD(tb[TCA_FQ_PIE_ECN]) >= sizeof(__u32)) {
 253                ecn = rta_getattr_u32(tb[TCA_FQ_PIE_ECN]);
 254                if (ecn)
 255                        print_bool(PRINT_ANY, "ecn", "ecn ", true);
 256        }
 257        if (tb[TCA_FQ_PIE_BYTEMODE] &&
 258            RTA_PAYLOAD(tb[TCA_FQ_PIE_BYTEMODE]) >= sizeof(__u32)) {
 259                bytemode = rta_getattr_u32(tb[TCA_FQ_PIE_BYTEMODE]);
 260                if (bytemode)
 261                        print_bool(PRINT_ANY, "bytemode", "bytemode ", true);
 262        }
 263        if (tb[TCA_FQ_PIE_DQ_RATE_ESTIMATOR] &&
 264            RTA_PAYLOAD(tb[TCA_FQ_PIE_DQ_RATE_ESTIMATOR]) >= sizeof(__u32)) {
 265                dq_rate_estimator =
 266                        rta_getattr_u32(tb[TCA_FQ_PIE_DQ_RATE_ESTIMATOR]);
 267                if (dq_rate_estimator)
 268                        print_bool(PRINT_ANY, "dq_rate_estimator",
 269                                   "dq_rate_estimator ", true);
 270        }
 271
 272        return 0;
 273}
 274
 275static int fq_pie_print_xstats(struct qdisc_util *qu, FILE *f,
 276                               struct rtattr *xstats)
 277{
 278        struct tc_fq_pie_xstats _st = {}, *st;
 279
 280        if (xstats == NULL)
 281                return 0;
 282
 283        st = RTA_DATA(xstats);
 284        if (RTA_PAYLOAD(xstats) < sizeof(*st)) {
 285                memcpy(&_st, st, RTA_PAYLOAD(xstats));
 286                st = &_st;
 287        }
 288
 289        print_uint(PRINT_ANY, "pkts_in", "  pkts_in %u",
 290                   st->packets_in);
 291        print_uint(PRINT_ANY, "overlimit", " overlimit %u",
 292                   st->overlimit);
 293        print_uint(PRINT_ANY, "overmemory", " overmemory %u",
 294                   st->overmemory);
 295        print_uint(PRINT_ANY, "dropped", " dropped %u",
 296                   st->dropped);
 297        print_uint(PRINT_ANY, "ecn_mark", " ecn_mark %u",
 298                   st->ecn_mark);
 299        print_nl();
 300        print_uint(PRINT_ANY, "new_flow_count", "  new_flow_count %u",
 301                   st->new_flow_count);
 302        print_uint(PRINT_ANY, "new_flows_len", " new_flows_len %u",
 303                   st->new_flows_len);
 304        print_uint(PRINT_ANY, "old_flows_len", " old_flows_len %u",
 305                   st->old_flows_len);
 306        print_uint(PRINT_ANY, "memory_used", " memory_used %u",
 307                   st->memory_usage);
 308
 309        return 0;
 310
 311}
 312
 313struct qdisc_util fq_pie_qdisc_util = {
 314        .id             = "fq_pie",
 315        .parse_qopt     = fq_pie_parse_opt,
 316        .print_qopt     = fq_pie_print_opt,
 317        .print_xstats   = fq_pie_print_xstats,
 318};
 319