]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - tc/q_netem.c
man: tc-taprio.8: fix syntax error
[mirror_iproute2.git] / tc / q_netem.c
index b1fd45297c0810b3bfdcdfd8bae67afddd242f8a..6e0e8a8cbfde508a59318812103b991b3fd093e3 100644 (file)
@@ -15,8 +15,8 @@
 #include <math.h>
 #include <ctype.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
+#include <stdint.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 static void explain(void)
 {
        fprintf(stderr,
-"Usage: ... netem [ limit PACKETS ] \n" \
+"Usage: ... netem [ limit PACKETS ]\n" \
 "                 [ delay TIME [ JITTER [CORRELATION]]]\n" \
 "                 [ distribution {uniform|normal|pareto|paretonormal} ]\n" \
-"                 [ corrupt PERCENT [CORRELATION]] \n" \
+"                 [ corrupt PERCENT [CORRELATION]]\n" \
 "                 [ duplicate PERCENT [CORRELATION]]\n" \
 "                 [ loss random PERCENT [CORRELATION]]\n" \
 "                 [ loss state P13 [P31 [P32 [P23 P14]]]\n" \
 "                 [ loss gemodel PERCENT [R [1-H [1-K]]]\n" \
-"                 [ reorder PERCENT [CORRELATION] [ gap DISTANCE ]]\n");
+"                 [ ecn ]\n" \
+"                 [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n" \
+"                 [ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]\n" \
+"                 [ slot MIN_DELAY [MAX_DELAY] [packets MAX_PACKETS]" \
+" [bytes MAX_BYTES]]\n" \
+"                 [ slot distribution" \
+" {uniform|normal|pareto|paretonormal|custom} DELAY JITTER" \
+" [packets MAX_PACKETS] [bytes MAX_BYTES]]\n");
 }
 
 static void explain1(const char *arg)
@@ -51,27 +58,10 @@ static void explain1(const char *arg)
  */
 #define MAX_DIST       (16*1024)
 
-static const double max_percent_value = 0xffffffff;
-
 /* scaled value used to percent of maximum. */
 static void set_percent(__u32 *percent, double per)
 {
-       *percent = (unsigned) rint(per * max_percent_value);
-}
-
-
-/* Parse either a fraction '.3' or percent '30%
- * return: 0 = ok, -1 = error, 1 = out of range
- */
-static int parse_percent(double *val, const char *str)
-{
-       char *p;
-
-       *val = strtod(str, &p) / 100.;
-       if (*p && strcmp(p, "%") )
-               return -1;
-
-       return 0;
+       *percent = rint(per * UINT32_MAX);
 }
 
 static int get_percent(__u32 *percent, const char *str)
@@ -85,12 +75,12 @@ static int get_percent(__u32 *percent, const char *str)
        return 0;
 }
 
-void print_percent(char *buf, int len, __u32 per)
+static void print_percent(char *buf, int len, __u32 per)
 {
-       snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value);
+       snprintf(buf, len, "%g%%", (100. * per) / UINT32_MAX);
 }
 
-char * sprint_percent(__u32 per, char *buf)
+static char *sprint_percent(__u32 per, char *buf)
 {
        print_percent(buf, SPRINT_BSIZE-1, per);
        return buf;
@@ -121,6 +111,7 @@ static int get_distribution(const char *type, __s16 *data, int maxdata)
        n = 0;
        while (getline(&line, &len, f) != -1) {
                char *p, *endp;
+
                if (*line == '\n' || *line == '#')
                        continue;
 
@@ -145,14 +136,16 @@ static int get_distribution(const char *type, __s16 *data, int maxdata)
 }
 
 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isdigit(argv[1][0]))
+#define NEXT_IS_SIGNED_NUMBER() \
+       (NEXT_ARG_OK() && (isdigit(argv[1][0]) || argv[1][0] == '-'))
 
 /* Adjust for the fact that psched_ticks aren't always usecs
    (based on kernel PSCHED_CLOCK configuration */
 static int get_ticks(__u32 *ticks, const char *str)
 {
-       unsigned t;
+       unsigned int t;
 
-       if(get_time(&t, str))
+       if (get_time(&t, str))
                return -1;
 
        if (tc_core_time2big(t)) {
@@ -165,26 +158,26 @@ static int get_ticks(__u32 *ticks, const char *str)
 }
 
 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
-                          struct nlmsghdr *n)
+                          struct nlmsghdr *n, const char *dev)
 {
        int dist_size = 0;
+       int slot_dist_size = 0;
        struct rtattr *tail;
        struct tc_netem_qopt opt = { .limit = 1000 };
-       struct tc_netem_corr cor;
-       struct tc_netem_reorder reorder;
-       struct tc_netem_corrupt corrupt;
+       struct tc_netem_corr cor = {};
+       struct tc_netem_reorder reorder = {};
+       struct tc_netem_corrupt corrupt = {};
        struct tc_netem_gimodel gimodel;
        struct tc_netem_gemodel gemodel;
+       struct tc_netem_rate rate = {};
+       struct tc_netem_slot slot = {};
        __s16 *dist_data = NULL;
+       __s16 *slot_dist_data = NULL;
        __u16 loss_type = NETEM_LOSS_UNSPEC;
-       int present[__TCA_NETEM_MAX];
-
-       memset(&cor, 0, sizeof(cor));
-       memset(&reorder, 0, sizeof(reorder));
-       memset(&corrupt, 0, sizeof(corrupt));
-       memset(present, 0, sizeof(present));
+       int present[__TCA_NETEM_MAX] = {};
+       __u64 rate64 = 0;
 
-       for( ; argc > 0; --argc, ++argv) {
+       for ( ; argc > 0; --argc, ++argv) {
                if (matches(*argv, "limit") == 0) {
                        NEXT_ARG();
                        if (get_size(&opt.limit, *argv)) {
@@ -229,7 +222,7 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
 
                        if (!strcmp(*argv, "random")) {
                                NEXT_ARG();
-       random_loss_model:
+                       random_loss_model:
                                if (get_percent(&opt.loss, *argv)) {
                                        explain1("loss percent");
                                        return -1;
@@ -256,6 +249,7 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                                set_percent(&gimodel.p31, 1. - p13);
                                set_percent(&gimodel.p32, 0);
                                set_percent(&gimodel.p23, 1.);
+                               set_percent(&gimodel.p14, 0);
                                loss_type = NETEM_LOSS_GI;
 
                                if (!NEXT_IS_NUMBER())
@@ -281,6 +275,13 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                                        explain1("loss p23");
                                        return -1;
                                }
+                               if (!NEXT_IS_NUMBER())
+                                       continue;
+                               NEXT_ARG();
+                               if (get_percent(&gimodel.p14, *argv)) {
+                                       explain1("loss p14");
+                                       return -1;
+                               }
 
                        } else if (!strcmp(*argv, "gemodel")) {
                                NEXT_ARG();
@@ -292,7 +293,7 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                                /* set defaults */
                                set_percent(&gemodel.r, 1.);
                                set_percent(&gemodel.h, 0);
-                               set_percent(&gemodel.k1, 1.);
+                               set_percent(&gemodel.k1, 0);
                                loss_type = NETEM_LOSS_GE;
 
                                if (!NEXT_IS_NUMBER())
@@ -310,6 +311,10 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                                        explain1("loss gemodel h");
                                        return -1;
                                }
+                               /* netem option is "1-h" but kernel
+                                * expects "h".
+                                */
+                               gemodel.h = UINT32_MAX - gemodel.h;
 
                                if (!NEXT_IS_NUMBER())
                                        continue;
@@ -323,6 +328,8 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                                        *argv);
                                return -1;
                        }
+               } else if (matches(*argv, "ecn") == 0) {
+                       present[TCA_NETEM_ECN] = 1;
                } else if (matches(*argv, "reorder") == 0) {
                        NEXT_ARG();
                        present[TCA_NETEM_REORDER] = 1;
@@ -380,6 +387,112 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                                free(dist_data);
                                return -1;
                        }
+               } else if (matches(*argv, "rate") == 0) {
+                       ++present[TCA_NETEM_RATE];
+                       NEXT_ARG();
+                       if (strchr(*argv, '%')) {
+                               if (get_percent_rate64(&rate64, *argv, dev)) {
+                                       explain1("rate");
+                                       return -1;
+                               }
+                       } else if (get_rate64(&rate64, *argv)) {
+                               explain1("rate");
+                               return -1;
+                       }
+                       if (NEXT_IS_SIGNED_NUMBER()) {
+                               NEXT_ARG();
+                               if (get_s32(&rate.packet_overhead, *argv, 0)) {
+                                       explain1("rate");
+                                       return -1;
+                               }
+                       }
+                       if (NEXT_IS_NUMBER()) {
+                               NEXT_ARG();
+                               if (get_u32(&rate.cell_size, *argv, 0)) {
+                                       explain1("rate");
+                                       return -1;
+                               }
+                       }
+                       if (NEXT_IS_SIGNED_NUMBER()) {
+                               NEXT_ARG();
+                               if (get_s32(&rate.cell_overhead, *argv, 0)) {
+                                       explain1("rate");
+                                       return -1;
+                               }
+                       }
+               } else if (matches(*argv, "slot") == 0) {
+                       if (NEXT_IS_NUMBER()) {
+                               NEXT_ARG();
+                               present[TCA_NETEM_SLOT] = 1;
+                               if (get_time64(&slot.min_delay, *argv)) {
+                                       explain1("slot min_delay");
+                                       return -1;
+                               }
+                               if (NEXT_IS_NUMBER()) {
+                                       NEXT_ARG();
+                                       if (get_time64(&slot.max_delay, *argv) ||
+                                           slot.max_delay < slot.min_delay) {
+                                               explain1("slot max_delay");
+                                               return -1;
+                                       }
+                               } else {
+                                       slot.max_delay = slot.min_delay;
+                               }
+                       } else {
+                               NEXT_ARG();
+                               if (strcmp(*argv, "distribution") == 0) {
+                                       present[TCA_NETEM_SLOT] = 1;
+                                       NEXT_ARG();
+                                       slot_dist_data = calloc(sizeof(slot_dist_data[0]), MAX_DIST);
+                                       if (!slot_dist_data)
+                                               return -1;
+                                       slot_dist_size = get_distribution(*argv, slot_dist_data, MAX_DIST);
+                                       if (slot_dist_size <= 0) {
+                                               free(slot_dist_data);
+                                               return -1;
+                                       }
+                                       NEXT_ARG();
+                                       if (get_time64(&slot.dist_delay, *argv)) {
+                                               explain1("slot delay");
+                                               return -1;
+                                       }
+                                       NEXT_ARG();
+                                       if (get_time64(&slot.dist_jitter, *argv)) {
+                                               explain1("slot jitter");
+                                               return -1;
+                                       }
+                                       if (slot.dist_jitter <= 0) {
+                                               fprintf(stderr, "Non-positive jitter\n");
+                                               return -1;
+                                       }
+                               } else {
+                                       fprintf(stderr, "Unknown slot parameter: %s\n",
+                                               *argv);
+                                       return -1;
+                               }
+                       }
+                       if (NEXT_ARG_OK() &&
+                           matches(*(argv+1), "packets") == 0) {
+                               NEXT_ARG();
+                               if (!NEXT_ARG_OK() ||
+                                   get_s32(&slot.max_packets, *(argv+1), 0)) {
+                                       explain1("slot packets");
+                                       return -1;
+                               }
+                               NEXT_ARG();
+                       }
+                       if (NEXT_ARG_OK() &&
+                           matches(*(argv+1), "bytes") == 0) {
+                               unsigned int max_bytes;
+                               NEXT_ARG();
+                               if (!NEXT_ARG_OK() ||
+                                   get_size(&max_bytes, *(argv+1))) {
+                                       explain1("slot bytes");
+                                       return -1;
+                               }
+                               slot.max_bytes = (int) max_bytes;
+                               NEXT_ARG();
+                       }
                } else if (strcmp(*argv, "help") == 0) {
                        explain();
                        return -1;
@@ -395,6 +508,8 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
        if (reorder.probability) {
                if (opt.latency == 0) {
                        fprintf(stderr, "reordering not possible without specifying some delay\n");
+                       explain();
+                       return -1;
                }
                if (opt.gap == 0)
                        opt.gap = 1;
@@ -404,6 +519,14 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                return -1;
        }
 
+       if (present[TCA_NETEM_ECN]) {
+               if (opt.loss <= 0 && loss_type == NETEM_LOSS_UNSPEC) {
+                       fprintf(stderr, "ecn requested without loss model\n");
+                       explain();
+                       return -1;
+               }
+       }
+
        if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
                fprintf(stderr, "distribution specified but no latency and jitter values\n");
                explain();
@@ -415,16 +538,25 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
 
        if (present[TCA_NETEM_CORR] &&
            addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
-                       return -1;
+               return -1;
 
        if (present[TCA_NETEM_REORDER] &&
            addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
                return -1;
 
+       if (present[TCA_NETEM_ECN] &&
+           addattr_l(n, 1024, TCA_NETEM_ECN, &present[TCA_NETEM_ECN],
+                     sizeof(present[TCA_NETEM_ECN])) < 0)
+               return -1;
+
        if (present[TCA_NETEM_CORRUPT] &&
            addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
                return -1;
 
+       if (present[TCA_NETEM_SLOT] &&
+           addattr_l(n, 1024, TCA_NETEM_SLOT, &slot, sizeof(slot)) < 0)
+               return -1;
+
        if (loss_type != NETEM_LOSS_UNSPEC) {
                struct rtattr *start;
 
@@ -432,19 +564,32 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                if (loss_type == NETEM_LOSS_GI) {
                        if (addattr_l(n, 1024, NETEM_LOSS_GI,
                                      &gimodel, sizeof(gimodel)) < 0)
-                           return -1;
+                               return -1;
                } else if (loss_type == NETEM_LOSS_GE) {
                        if (addattr_l(n, 1024, NETEM_LOSS_GE,
                                      &gemodel, sizeof(gemodel)) < 0)
-                           return -1;
+                               return -1;
                } else {
                        fprintf(stderr, "loss in the weeds!\n");
                        return -1;
                }
-               
+
                addattr_nest_end(n, start);
        }
 
+       if (present[TCA_NETEM_RATE]) {
+               if (rate64 >= (1ULL << 32)) {
+                       if (addattr_l(n, 1024,
+                                     TCA_NETEM_RATE64, &rate64, sizeof(rate64)) < 0)
+                               return -1;
+                       rate.rate = ~0U;
+               } else {
+                       rate.rate = rate64;
+               }
+               if (addattr_l(n, 1024, TCA_NETEM_RATE, &rate, sizeof(rate)) < 0)
+                       return -1;
+       }
+
        if (dist_data) {
                if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]),
                              TCA_NETEM_DELAY_DIST,
@@ -452,6 +597,14 @@ static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
                        return -1;
                free(dist_data);
        }
+
+       if (slot_dist_data) {
+               if (addattr_l(n, MAX_DIST * sizeof(slot_dist_data[0]),
+                             TCA_NETEM_SLOT_DIST,
+                             slot_dist_data, slot_dist_size * sizeof(slot_dist_data[0])) < 0)
+                       return -1;
+               free(slot_dist_data);
+       }
        tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
        return 0;
 }
@@ -463,13 +616,19 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
        const struct tc_netem_corrupt *corrupt = NULL;
        const struct tc_netem_gimodel *gimodel = NULL;
        const struct tc_netem_gemodel *gemodel = NULL;
+       int *ecn = NULL;
        struct tc_netem_qopt qopt;
-       int len = RTA_PAYLOAD(opt) - sizeof(qopt);
+       const struct tc_netem_rate *rate = NULL;
+       const struct tc_netem_slot *slot = NULL;
+       int len;
+       __u64 rate64 = 0;
+
        SPRINT_BUF(b1);
 
        if (opt == NULL)
                return 0;
 
+       len = RTA_PAYLOAD(opt) - sizeof(qopt);
        if (len < 0) {
                fprintf(stderr, "options size error\n");
                return -1;
@@ -478,6 +637,7 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 
        if (len > 0) {
                struct rtattr *tb[TCA_NETEM_MAX+1];
+
                parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
                             len);
 
@@ -501,10 +661,30 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
 
                        parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]);
                        if (lb[NETEM_LOSS_GI])
-                               gemodel = RTA_DATA(lb[NETEM_LOSS_GI]);
+                               gimodel = RTA_DATA(lb[NETEM_LOSS_GI]);
                        if (lb[NETEM_LOSS_GE])
                                gemodel = RTA_DATA(lb[NETEM_LOSS_GE]);
-               }                       
+               }
+               if (tb[TCA_NETEM_RATE]) {
+                       if (RTA_PAYLOAD(tb[TCA_NETEM_RATE]) < sizeof(*rate))
+                               return -1;
+                       rate = RTA_DATA(tb[TCA_NETEM_RATE]);
+               }
+               if (tb[TCA_NETEM_ECN]) {
+                       if (RTA_PAYLOAD(tb[TCA_NETEM_ECN]) < sizeof(*ecn))
+                               return -1;
+                       ecn = RTA_DATA(tb[TCA_NETEM_ECN]);
+               }
+               if (tb[TCA_NETEM_RATE64]) {
+                       if (RTA_PAYLOAD(tb[TCA_NETEM_RATE64]) < sizeof(rate64))
+                               return -1;
+                       rate64 = rta_getattr_u64(tb[TCA_NETEM_RATE64]);
+               }
+               if (tb[TCA_NETEM_SLOT]) {
+                       if (RTA_PAYLOAD(tb[TCA_NETEM_SLOT]) < sizeof(*slot))
+                               return -1;
+                       slot = RTA_DATA(tb[TCA_NETEM_SLOT]);
+               }
        }
 
        fprintf(f, "limit %d", qopt.limit);
@@ -534,10 +714,11 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
        }
 
        if (gemodel) {
-               fprintf(f, "loss gemodel p %s",
+               fprintf(f, " loss gemodel p %s",
                        sprint_percent(gemodel->p, b1));
                fprintf(f, " r %s", sprint_percent(gemodel->r, b1));
-               fprintf(f, " 1-h %s", sprint_percent(gemodel->h, b1));
+               fprintf(f, " 1-h %s", sprint_percent(UINT32_MAX -
+                                                    gemodel->h, b1));
                fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1));
        }
 
@@ -564,15 +745,45 @@ static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
                                sprint_percent(corrupt->correlation, b1));
        }
 
+       if (rate && rate->rate) {
+               if (rate64)
+                       fprintf(f, " rate %s", sprint_rate(rate64, b1));
+               else
+                       fprintf(f, " rate %s", sprint_rate(rate->rate, b1));
+               if (rate->packet_overhead)
+                       fprintf(f, " packetoverhead %d", rate->packet_overhead);
+               if (rate->cell_size)
+                       fprintf(f, " cellsize %u", rate->cell_size);
+               if (rate->cell_overhead)
+                       fprintf(f, " celloverhead %d", rate->cell_overhead);
+       }
+
+       if (slot) {
+               if (slot->dist_jitter > 0) {
+                   fprintf(f, " slot distribution %s", sprint_time64(slot->dist_delay, b1));
+                   fprintf(f, " %s", sprint_time64(slot->dist_jitter, b1));
+               } else {
+                   fprintf(f, " slot %s", sprint_time64(slot->min_delay, b1));
+                   fprintf(f, " %s", sprint_time64(slot->max_delay, b1));
+               }
+               if (slot->max_packets)
+                       fprintf(f, " packets %d", slot->max_packets);
+               if (slot->max_bytes)
+                       fprintf(f, " bytes %d", slot->max_bytes);
+       }
+
+       if (ecn)
+               fprintf(f, " ecn ");
+
        if (qopt.gap)
                fprintf(f, " gap %lu", (unsigned long)qopt.gap);
 
+
        return 0;
 }
 
 struct qdisc_util netem_qdisc_util = {
-       .id             = "netem",
+       .id             = "netem",
        .parse_qopt     = netem_parse_opt,
        .print_qopt     = netem_print_opt,
 };
-