]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/q_netem.c
tc-netem: Add support for ECN packet marking
[lisovros/iproute2_canprio.git] / tc / q_netem.c
1 /*
2  * q_netem.c            NETEM.
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:     Stephen Hemminger <shemminger@linux-foundation.org>
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <math.h>
16 #include <ctype.h>
17 #include <unistd.h>
18 #include <syslog.h>
19 #include <fcntl.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include "utils.h"
27 #include "tc_util.h"
28 #include "tc_common.h"
29
30 static void explain(void)
31 {
32         fprintf(stderr,
33 "Usage: ... netem [ limit PACKETS ] \n" \
34 "                 [ delay TIME [ JITTER [CORRELATION]]]\n" \
35 "                 [ distribution {uniform|normal|pareto|paretonormal} ]\n" \
36 "                 [ corrupt PERCENT [CORRELATION]] \n" \
37 "                 [ duplicate PERCENT [CORRELATION]]\n" \
38 "                 [ loss random PERCENT [CORRELATION]]\n" \
39 "                 [ loss state P13 [P31 [P32 [P23 P14]]]\n" \
40 "                 [ loss gemodel PERCENT [R [1-H [1-K]]]\n" \
41 "                 [ ecn ]\n" \
42 "                 [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n" \
43 "                 [ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]\n");
44 }
45
46 static void explain1(const char *arg)
47 {
48         fprintf(stderr, "Illegal \"%s\"\n", arg);
49 }
50
51 /* Upper bound on size of distribution
52  *  really (TCA_BUF_MAX - other headers) / sizeof (__s16)
53  */
54 #define MAX_DIST        (16*1024)
55
56 static const double max_percent_value = 0xffffffff;
57
58 /* scaled value used to percent of maximum. */
59 static void set_percent(__u32 *percent, double per)
60 {
61         *percent = (unsigned) rint(per * max_percent_value);
62 }
63
64
65 /* Parse either a fraction '.3' or percent '30%
66  * return: 0 = ok, -1 = error, 1 = out of range
67  */
68 static int parse_percent(double *val, const char *str)
69 {
70         char *p;
71
72         *val = strtod(str, &p) / 100.;
73         if (*p && strcmp(p, "%") )
74                 return -1;
75
76         return 0;
77 }
78
79 static int get_percent(__u32 *percent, const char *str)
80 {
81         double per;
82
83         if (parse_percent(&per, str))
84                 return -1;
85
86         set_percent(percent, per);
87         return 0;
88 }
89
90 void print_percent(char *buf, int len, __u32 per)
91 {
92         snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value);
93 }
94
95 char * sprint_percent(__u32 per, char *buf)
96 {
97         print_percent(buf, SPRINT_BSIZE-1, per);
98         return buf;
99 }
100
101 /*
102  * Simplistic file parser for distrbution data.
103  * Format is:
104  *      # comment line(s)
105  *      data0 data1 ...
106  */
107 static int get_distribution(const char *type, __s16 *data, int maxdata)
108 {
109         FILE *f;
110         int n;
111         long x;
112         size_t len;
113         char *line = NULL;
114         char name[128];
115
116         snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type);
117         if ((f = fopen(name, "r")) == NULL) {
118                 fprintf(stderr, "No distribution data for %s (%s: %s)\n",
119                         type, name, strerror(errno));
120                 return -1;
121         }
122
123         n = 0;
124         while (getline(&line, &len, f) != -1) {
125                 char *p, *endp;
126                 if (*line == '\n' || *line == '#')
127                         continue;
128
129                 for (p = line; ; p = endp) {
130                         x = strtol(p, &endp, 0);
131                         if (endp == p)
132                                 break;
133
134                         if (n >= maxdata) {
135                                 fprintf(stderr, "%s: too much data\n",
136                                         name);
137                                 n = -1;
138                                 goto error;
139                         }
140                         data[n++] = x;
141                 }
142         }
143  error:
144         free(line);
145         fclose(f);
146         return n;
147 }
148
149 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isdigit(argv[1][0]))
150
151 /* Adjust for the fact that psched_ticks aren't always usecs
152    (based on kernel PSCHED_CLOCK configuration */
153 static int get_ticks(__u32 *ticks, const char *str)
154 {
155         unsigned t;
156
157         if(get_time(&t, str))
158                 return -1;
159
160         if (tc_core_time2big(t)) {
161                 fprintf(stderr, "Illegal %u time (too large)\n", t);
162                 return -1;
163         }
164
165         *ticks = tc_core_time2tick(t);
166         return 0;
167 }
168
169 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
170                            struct nlmsghdr *n)
171 {
172         int dist_size = 0;
173         struct rtattr *tail;
174         struct tc_netem_qopt opt = { .limit = 1000 };
175         struct tc_netem_corr cor;
176         struct tc_netem_reorder reorder;
177         struct tc_netem_corrupt corrupt;
178         struct tc_netem_gimodel gimodel;
179         struct tc_netem_gemodel gemodel;
180         struct tc_netem_rate rate;
181         __s16 *dist_data = NULL;
182         __u16 loss_type = NETEM_LOSS_UNSPEC;
183         int present[__TCA_NETEM_MAX];
184
185         memset(&cor, 0, sizeof(cor));
186         memset(&reorder, 0, sizeof(reorder));
187         memset(&corrupt, 0, sizeof(corrupt));
188         memset(&rate, 0, sizeof(rate));
189         memset(present, 0, sizeof(present));
190
191         for( ; argc > 0; --argc, ++argv) {
192                 if (matches(*argv, "limit") == 0) {
193                         NEXT_ARG();
194                         if (get_size(&opt.limit, *argv)) {
195                                 explain1("limit");
196                                 return -1;
197                         }
198                 } else if (matches(*argv, "latency") == 0 ||
199                            matches(*argv, "delay") == 0) {
200                         NEXT_ARG();
201                         if (get_ticks(&opt.latency, *argv)) {
202                                 explain1("latency");
203                                 return -1;
204                         }
205
206                         if (NEXT_IS_NUMBER()) {
207                                 NEXT_ARG();
208                                 if (get_ticks(&opt.jitter, *argv)) {
209                                         explain1("latency");
210                                         return -1;
211                                 }
212
213                                 if (NEXT_IS_NUMBER()) {
214                                         NEXT_ARG();
215                                         ++present[TCA_NETEM_CORR];
216                                         if (get_percent(&cor.delay_corr, *argv)) {
217                                                 explain1("latency");
218                                                 return -1;
219                                         }
220                                 }
221                         }
222                 } else if (matches(*argv, "loss") == 0 ||
223                            matches(*argv, "drop") == 0) {
224                         if (opt.loss > 0 || loss_type != NETEM_LOSS_UNSPEC) {
225                                 explain1("duplicate loss argument\n");
226                                 return -1;
227                         }
228
229                         NEXT_ARG();
230                         /* Old (deprecated) random loss model syntax */
231                         if (isdigit(argv[0][0]))
232                                 goto random_loss_model;
233
234                         if (!strcmp(*argv, "random")) {
235                                 NEXT_ARG();
236         random_loss_model:
237                                 if (get_percent(&opt.loss, *argv)) {
238                                         explain1("loss percent");
239                                         return -1;
240                                 }
241                                 if (NEXT_IS_NUMBER()) {
242                                         NEXT_ARG();
243                                         ++present[TCA_NETEM_CORR];
244                                         if (get_percent(&cor.loss_corr, *argv)) {
245                                                 explain1("loss correllation");
246                                                 return -1;
247                                         }
248                                 }
249                         } else if (!strcmp(*argv, "state")) {
250                                 double p13;
251
252                                 NEXT_ARG();
253                                 if (parse_percent(&p13, *argv)) {
254                                         explain1("loss p13");
255                                         return -1;
256                                 }
257
258                                 /* set defaults */
259                                 set_percent(&gimodel.p13, p13);
260                                 set_percent(&gimodel.p31, 1. - p13);
261                                 set_percent(&gimodel.p32, 0);
262                                 set_percent(&gimodel.p23, 1.);
263                                 loss_type = NETEM_LOSS_GI;
264
265                                 if (!NEXT_IS_NUMBER())
266                                         continue;
267                                 NEXT_ARG();
268                                 if (get_percent(&gimodel.p31, *argv)) {
269                                         explain1("loss p31");
270                                         return -1;
271                                 }
272
273                                 if (!NEXT_IS_NUMBER())
274                                         continue;
275                                 NEXT_ARG();
276                                 if (get_percent(&gimodel.p32, *argv)) {
277                                         explain1("loss p32");
278                                         return -1;
279                                 }
280
281                                 if (!NEXT_IS_NUMBER())
282                                         continue;
283                                 NEXT_ARG();
284                                 if (get_percent(&gimodel.p23, *argv)) {
285                                         explain1("loss p23");
286                                         return -1;
287                                 }
288
289                         } else if (!strcmp(*argv, "gemodel")) {
290                                 NEXT_ARG();
291                                 if (get_percent(&gemodel.p, *argv)) {
292                                         explain1("loss gemodel p");
293                                         return -1;
294                                 }
295
296                                 /* set defaults */
297                                 set_percent(&gemodel.r, 1.);
298                                 set_percent(&gemodel.h, 0);
299                                 set_percent(&gemodel.k1, 1.);
300                                 loss_type = NETEM_LOSS_GE;
301
302                                 if (!NEXT_IS_NUMBER())
303                                         continue;
304                                 NEXT_ARG();
305                                 if (get_percent(&gemodel.r, *argv)) {
306                                         explain1("loss gemodel r");
307                                         return -1;
308                                 }
309
310                                 if (!NEXT_IS_NUMBER())
311                                         continue;
312                                 NEXT_ARG();
313                                 if (get_percent(&gemodel.h, *argv)) {
314                                         explain1("loss gemodel h");
315                                         return -1;
316                                 }
317
318                                 if (!NEXT_IS_NUMBER())
319                                         continue;
320                                 NEXT_ARG();
321                                 if (get_percent(&gemodel.k1, *argv)) {
322                                         explain1("loss gemodel k");
323                                         return -1;
324                                 }
325                         } else {
326                                 fprintf(stderr, "Unknown loss parameter: %s\n",
327                                         *argv);
328                                 return -1;
329                         }
330                 } else if (matches(*argv, "ecn") == 0) {
331                                 present[TCA_NETEM_ECN] = 1;
332                 } else if (matches(*argv, "reorder") == 0) {
333                         NEXT_ARG();
334                         present[TCA_NETEM_REORDER] = 1;
335                         if (get_percent(&reorder.probability, *argv)) {
336                                 explain1("reorder");
337                                 return -1;
338                         }
339                         if (NEXT_IS_NUMBER()) {
340                                 NEXT_ARG();
341                                 ++present[TCA_NETEM_CORR];
342                                 if (get_percent(&reorder.correlation, *argv)) {
343                                         explain1("reorder");
344                                         return -1;
345                                 }
346                         }
347                 } else if (matches(*argv, "corrupt") == 0) {
348                         NEXT_ARG();
349                         present[TCA_NETEM_CORRUPT] = 1;
350                         if (get_percent(&corrupt.probability, *argv)) {
351                                 explain1("corrupt");
352                                 return -1;
353                         }
354                         if (NEXT_IS_NUMBER()) {
355                                 NEXT_ARG();
356                                 ++present[TCA_NETEM_CORR];
357                                 if (get_percent(&corrupt.correlation, *argv)) {
358                                         explain1("corrupt");
359                                         return -1;
360                                 }
361                         }
362                 } else if (matches(*argv, "gap") == 0) {
363                         NEXT_ARG();
364                         if (get_u32(&opt.gap, *argv, 0)) {
365                                 explain1("gap");
366                                 return -1;
367                         }
368                 } else if (matches(*argv, "duplicate") == 0) {
369                         NEXT_ARG();
370                         if (get_percent(&opt.duplicate, *argv)) {
371                                 explain1("duplicate");
372                                 return -1;
373                         }
374                         if (NEXT_IS_NUMBER()) {
375                                 NEXT_ARG();
376                                 if (get_percent(&cor.dup_corr, *argv)) {
377                                         explain1("duplicate");
378                                         return -1;
379                                 }
380                         }
381                 } else if (matches(*argv, "distribution") == 0) {
382                         NEXT_ARG();
383                         dist_data = calloc(sizeof(dist_data[0]), MAX_DIST);
384                         dist_size = get_distribution(*argv, dist_data, MAX_DIST);
385                         if (dist_size <= 0) {
386                                 free(dist_data);
387                                 return -1;
388                         }
389                 } else if (matches(*argv, "rate") == 0) {
390                         ++present[TCA_NETEM_RATE];
391                         NEXT_ARG();
392                         if (get_rate(&rate.rate, *argv)) {
393                                 explain1("rate");
394                                 return -1;
395                         }
396                         if (NEXT_IS_NUMBER()) {
397                                 NEXT_ARG();
398                                 if (get_s32(&rate.packet_overhead, *argv, 0)) {
399                                         explain1("rate");
400                                         return -1;
401                                 }
402                         }
403                         if (NEXT_IS_NUMBER()) {
404                                 NEXT_ARG();
405                                 if (get_u32(&rate.cell_size, *argv, 0)) {
406                                         explain1("rate");
407                                         return -1;
408                                 }
409                         }
410                         if (NEXT_IS_NUMBER()) {
411                                 NEXT_ARG();
412                                 if (get_s32(&rate.cell_overhead, *argv, 0)) {
413                                         explain1("rate");
414                                         return -1;
415                                 }
416                         }
417                 } else if (strcmp(*argv, "help") == 0) {
418                         explain();
419                         return -1;
420                 } else {
421                         fprintf(stderr, "What is \"%s\"?\n", *argv);
422                         explain();
423                         return -1;
424                 }
425         }
426
427         tail = NLMSG_TAIL(n);
428
429         if (reorder.probability) {
430                 if (opt.latency == 0) {
431                         fprintf(stderr, "reordering not possible without specifying some delay\n");
432                         explain();
433                         return -1;
434                 }
435                 if (opt.gap == 0)
436                         opt.gap = 1;
437         } else if (opt.gap > 0) {
438                 fprintf(stderr, "gap specified without reorder probability\n");
439                 explain();
440                 return -1;
441         }
442
443         if (present[TCA_NETEM_ECN]) {
444                 if (opt.loss <= 0 && loss_type == NETEM_LOSS_UNSPEC) {
445                         fprintf(stderr, "ecn requested without loss model\n");
446                         explain();
447                         return -1;
448                 }
449         }
450
451         if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
452                 fprintf(stderr, "distribution specified but no latency and jitter values\n");
453                 explain();
454                 return -1;
455         }
456
457         if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
458                 return -1;
459
460         if (present[TCA_NETEM_CORR] &&
461             addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
462                         return -1;
463
464         if (present[TCA_NETEM_REORDER] &&
465             addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
466                 return -1;
467
468         if (present[TCA_NETEM_ECN] &&
469             addattr_l(n, 1024, TCA_NETEM_ECN, &present[TCA_NETEM_ECN],
470                       sizeof(present[TCA_NETEM_ECN])) < 0)
471                         return -1;
472
473         if (present[TCA_NETEM_CORRUPT] &&
474             addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
475                 return -1;
476
477         if (loss_type != NETEM_LOSS_UNSPEC) {
478                 struct rtattr *start;
479
480                 start = addattr_nest(n, 1024, TCA_NETEM_LOSS | NLA_F_NESTED);
481                 if (loss_type == NETEM_LOSS_GI) {
482                         if (addattr_l(n, 1024, NETEM_LOSS_GI,
483                                       &gimodel, sizeof(gimodel)) < 0)
484                             return -1;
485                 } else if (loss_type == NETEM_LOSS_GE) {
486                         if (addattr_l(n, 1024, NETEM_LOSS_GE,
487                                       &gemodel, sizeof(gemodel)) < 0)
488                             return -1;
489                 } else {
490                         fprintf(stderr, "loss in the weeds!\n");
491                         return -1;
492                 }
493                 
494                 addattr_nest_end(n, start);
495         }
496
497         if (present[TCA_NETEM_RATE] &&
498             addattr_l(n, 1024, TCA_NETEM_RATE, &rate, sizeof(rate)) < 0)
499                 return -1;
500
501         if (dist_data) {
502                 if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]),
503                               TCA_NETEM_DELAY_DIST,
504                               dist_data, dist_size * sizeof(dist_data[0])) < 0)
505                         return -1;
506                 free(dist_data);
507         }
508         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
509         return 0;
510 }
511
512 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
513 {
514         const struct tc_netem_corr *cor = NULL;
515         const struct tc_netem_reorder *reorder = NULL;
516         const struct tc_netem_corrupt *corrupt = NULL;
517         const struct tc_netem_gimodel *gimodel = NULL;
518         const struct tc_netem_gemodel *gemodel = NULL;
519         int *ecn = NULL;
520         struct tc_netem_qopt qopt;
521         const struct tc_netem_rate *rate = NULL;
522         int len = RTA_PAYLOAD(opt) - sizeof(qopt);
523         SPRINT_BUF(b1);
524
525         if (opt == NULL)
526                 return 0;
527
528         if (len < 0) {
529                 fprintf(stderr, "options size error\n");
530                 return -1;
531         }
532         memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
533
534         if (len > 0) {
535                 struct rtattr *tb[TCA_NETEM_MAX+1];
536                 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
537                              len);
538
539                 if (tb[TCA_NETEM_CORR]) {
540                         if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
541                                 return -1;
542                         cor = RTA_DATA(tb[TCA_NETEM_CORR]);
543                 }
544                 if (tb[TCA_NETEM_REORDER]) {
545                         if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
546                                 return -1;
547                         reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
548                 }
549                 if (tb[TCA_NETEM_CORRUPT]) {
550                         if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
551                                 return -1;
552                         corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
553                 }
554                 if (tb[TCA_NETEM_LOSS]) {
555                         struct rtattr *lb[NETEM_LOSS_MAX + 1];
556
557                         parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]);
558                         if (lb[NETEM_LOSS_GI])
559                                 gemodel = RTA_DATA(lb[NETEM_LOSS_GI]);
560                         if (lb[NETEM_LOSS_GE])
561                                 gemodel = RTA_DATA(lb[NETEM_LOSS_GE]);
562                 }                       
563                 if (tb[TCA_NETEM_RATE]) {
564                         if (RTA_PAYLOAD(tb[TCA_NETEM_RATE]) < sizeof(*rate))
565                                 return -1;
566                         rate = RTA_DATA(tb[TCA_NETEM_RATE]);
567                 }
568                 if (tb[TCA_NETEM_ECN]) {
569                         if (RTA_PAYLOAD(tb[TCA_NETEM_ECN]) < sizeof(*ecn))
570                                 return -1;
571                         ecn = RTA_DATA(tb[TCA_NETEM_ECN]);
572                 }
573         }
574
575         fprintf(f, "limit %d", qopt.limit);
576
577         if (qopt.latency) {
578                 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
579
580                 if (qopt.jitter) {
581                         fprintf(f, "  %s", sprint_ticks(qopt.jitter, b1));
582                         if (cor && cor->delay_corr)
583                                 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
584                 }
585         }
586
587         if (qopt.loss) {
588                 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
589                 if (cor && cor->loss_corr)
590                         fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
591         }
592
593         if (gimodel) {
594                 fprintf(f, " loss state p13 %s", sprint_percent(gimodel->p13, b1));
595                 fprintf(f, " p31 %s", sprint_percent(gimodel->p31, b1));
596                 fprintf(f, " p32 %s", sprint_percent(gimodel->p32, b1));
597                 fprintf(f, " p23 %s", sprint_percent(gimodel->p23, b1));
598                 fprintf(f, " p14 %s", sprint_percent(gimodel->p14, b1));
599         }
600
601         if (gemodel) {
602                 fprintf(f, "loss gemodel p %s",
603                         sprint_percent(gemodel->p, b1));
604                 fprintf(f, " r %s", sprint_percent(gemodel->r, b1));
605                 fprintf(f, " 1-h %s", sprint_percent(gemodel->h, b1));
606                 fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1));
607         }
608
609         if (qopt.duplicate) {
610                 fprintf(f, " duplicate %s",
611                         sprint_percent(qopt.duplicate, b1));
612                 if (cor && cor->dup_corr)
613                         fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
614         }
615
616         if (reorder && reorder->probability) {
617                 fprintf(f, " reorder %s",
618                         sprint_percent(reorder->probability, b1));
619                 if (reorder->correlation)
620                         fprintf(f, " %s",
621                                 sprint_percent(reorder->correlation, b1));
622         }
623
624         if (corrupt && corrupt->probability) {
625                 fprintf(f, " corrupt %s",
626                         sprint_percent(corrupt->probability, b1));
627                 if (corrupt->correlation)
628                         fprintf(f, " %s",
629                                 sprint_percent(corrupt->correlation, b1));
630         }
631
632         if (rate && rate->rate) {
633                 fprintf(f, " rate %s", sprint_rate(rate->rate, b1));
634                 if (rate->packet_overhead)
635                         fprintf(f, " packetoverhead %d", rate->packet_overhead);
636                 if (rate->cell_size)
637                         fprintf(f, " cellsize %u", rate->cell_size);
638                 if (rate->cell_overhead)
639                         fprintf(f, " celloverhead %d", rate->cell_overhead);
640         }
641
642         if (ecn)
643                 fprintf(f, " ecn ");
644
645         if (qopt.gap)
646                 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
647
648
649         return 0;
650 }
651
652 struct qdisc_util netem_qdisc_util = {
653         .id             = "netem",
654         .parse_qopt     = netem_parse_opt,
655         .print_qopt     = netem_print_opt,
656 };
657