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