]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/q_netem.c
Add TC_LIB_DIR environment variable.
[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@osdl.org>
10  *
11  */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22 #include <errno.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26 #include "tc_common.h"
27
28 static void explain(void)
29 {
30         fprintf(stderr,
31 "Usage: ... netem [ limit PACKETS ] \n" \
32 "                 [ delay TIME [ JITTER [CORRELATION]]]\n" \
33 "                 [ distribution {uniform|normal|pareto|paretonormal} ]\n" \
34 "                 [ drop PERCENT [CORRELATION]] \n" \
35 "                 [ corrupt PERCENT [CORRELATION]] \n" \
36 "                 [ duplicate PERCENT [CORRELATION]]\n" \
37 "                 [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n");
38 }
39
40 static void explain1(const char *arg)
41 {
42         fprintf(stderr, "Illegal \"%s\"\n", arg);
43 }
44
45 #define usage() return(-1)
46
47 /*
48  * Simplistic file parser for distrbution data.
49  * Format is:
50  *      # comment line(s)
51  *      data0 data1
52  */
53 #define MAXDIST 65536
54 static int get_distribution(const char *type, __s16 *data)
55 {
56         FILE *f;
57         int n;
58         long x;
59         size_t len;
60         char *line = NULL;
61         char name[128];
62
63         snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type);
64         if ((f = fopen(name, "r")) == NULL) {
65                 fprintf(stderr, "No distribution data for %s (%s: %s)\n",
66                         type, name, strerror(errno));
67                 return -1;
68         }
69
70         n = 0;
71         while (getline(&line, &len, f) != -1) {
72                 char *p, *endp;
73                 if (*line == '\n' || *line == '#')
74                         continue;
75
76                 for (p = line; ; p = endp) {
77                         x = strtol(p, &endp, 0);
78                         if (endp == p)
79                                 break;
80
81                         if (n >= MAXDIST) {
82                                 fprintf(stderr, "%s: too much data\n",
83                                         name);
84                                 n = -1;
85                                 goto error;
86                         }
87                         data[n++] = x;
88                 }
89         }
90  error:
91         free(line);
92         fclose(f);
93         return n;
94 }
95
96 static int isnumber(const char *arg)
97 {
98         char *p;
99         (void) strtod(arg, &p);
100         return (p != arg);
101 }
102
103 #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isnumber(argv[1]))
104
105 /* Adjust for the fact that psched_ticks aren't always usecs
106    (based on kernel PSCHED_CLOCK configuration */
107 static int get_ticks(__u32 *ticks, const char *str)
108 {
109         unsigned t;
110
111         if(get_time(&t, str))
112                 return -1;
113
114         if (tc_core_time2big(t)) {
115                 fprintf(stderr, "Illegal %u time (too large)\n", t);
116                 return -1;
117         }
118
119         *ticks = tc_core_time2tick(t);
120         return 0;
121 }
122
123 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv,
124                            struct nlmsghdr *n)
125 {
126         size_t dist_size = 0;
127         struct rtattr *tail;
128         struct tc_netem_qopt opt;
129         struct tc_netem_corr cor;
130         struct tc_netem_reorder reorder;
131         struct tc_netem_corrupt corrupt;
132         __s16 *dist_data = NULL;
133         int present[__TCA_NETEM_MAX];
134
135         memset(&opt, 0, sizeof(opt));
136         opt.limit = 1000;
137         memset(&cor, 0, sizeof(cor));
138         memset(&reorder, 0, sizeof(reorder));
139         memset(&corrupt, 0, sizeof(corrupt));
140         memset(present, 0, sizeof(present));
141
142         while (argc > 0) {
143                 if (matches(*argv, "limit") == 0) {
144                         NEXT_ARG();
145                         if (get_size(&opt.limit, *argv)) {
146                                 explain1("limit");
147                                 return -1;
148                         }
149                 } else if (matches(*argv, "latency") == 0 ||
150                            matches(*argv, "delay") == 0) {
151                         NEXT_ARG();
152                         if (get_ticks(&opt.latency, *argv)) {
153                                 explain1("latency");
154                                 return -1;
155                         }
156
157                         if (NEXT_IS_NUMBER()) {
158                                 NEXT_ARG();
159                                 if (get_ticks(&opt.jitter, *argv)) {
160                                         explain1("latency");
161                                         return -1;
162                                 }
163
164                                 if (NEXT_IS_NUMBER()) {
165                                         NEXT_ARG();
166                                         ++present[TCA_NETEM_CORR];
167                                         if (get_percent(&cor.delay_corr,                                                        *argv)) {
168                                                 explain1("latency");
169                                                 return -1;
170                                         }
171                                 }
172                         }
173                 } else if (matches(*argv, "loss") == 0 ||
174                            matches(*argv, "drop") == 0) {
175                         NEXT_ARG();
176                         if (get_percent(&opt.loss, *argv)) {
177                                 explain1("loss");
178                                 return -1;
179                         }
180                         if (NEXT_IS_NUMBER()) {
181                                 NEXT_ARG();
182                                 ++present[TCA_NETEM_CORR];
183                                 if (get_percent(&cor.loss_corr, *argv)) {
184                                         explain1("loss");
185                                         return -1;
186                                 }
187                         }
188                 } else if (matches(*argv, "reorder") == 0) {
189                         NEXT_ARG();
190                         present[TCA_NETEM_REORDER] = 1;
191                         if (get_percent(&reorder.probability, *argv)) {
192                                 explain1("reorder");
193                                 return -1;
194                         }
195                         if (NEXT_IS_NUMBER()) {
196                                 NEXT_ARG();
197                                 ++present[TCA_NETEM_CORR];
198                                 if (get_percent(&reorder.correlation, *argv)) {
199                                         explain1("reorder");
200                                         return -1;
201                                 }
202                         }
203                 } else if (matches(*argv, "corrupt") == 0) {
204                         NEXT_ARG();
205                         present[TCA_NETEM_CORRUPT] = 1;
206                         if (get_percent(&corrupt.probability, *argv)) {
207                                 explain1("corrupt");
208                                 return -1;
209                         }
210                         if (NEXT_IS_NUMBER()) {
211                                 NEXT_ARG();
212                                 ++present[TCA_NETEM_CORR];
213                                 if (get_percent(&corrupt.correlation, *argv)) {
214                                         explain1("corrupt");
215                                         return -1;
216                                 }
217                         }
218                 } else if (matches(*argv, "gap") == 0) {
219                         NEXT_ARG();
220                         if (get_u32(&opt.gap, *argv, 0)) {
221                                 explain1("gap");
222                                 return -1;
223                         }
224                 } else if (matches(*argv, "duplicate") == 0) {
225                         NEXT_ARG();
226                         if (get_percent(&opt.duplicate, *argv)) {
227                                 explain1("duplicate");
228                                 return -1;
229                         }
230                         if (NEXT_IS_NUMBER()) {
231                                 NEXT_ARG();
232                                 if (get_percent(&cor.dup_corr, *argv)) {
233                                         explain1("duplicate");
234                                         return -1;
235                                 }
236                         }
237                 } else if (matches(*argv, "distribution") == 0) {
238                         NEXT_ARG();
239                         dist_data = alloca(MAXDIST);
240                         dist_size = get_distribution(*argv, dist_data);
241                         if (dist_size < 0)
242                                 return -1;
243                 } else if (strcmp(*argv, "help") == 0) {
244                         explain();
245                         return -1;
246                 } else {
247                         fprintf(stderr, "What is \"%s\"?\n", *argv);
248                         explain();
249                         return -1;
250                 }
251                 argc--; argv++;
252         }
253
254         tail = NLMSG_TAIL(n);
255
256         if (reorder.probability) {
257                 if (opt.latency == 0) {
258                         fprintf(stderr, "reordering not possible without specifying some delay\n");
259                 }
260                 if (opt.gap == 0)
261                         opt.gap = 1;
262         } else if (opt.gap > 0) {
263                 fprintf(stderr, "gap specified without reorder probability\n");
264                 explain();
265                 return -1;
266         }
267
268         if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
269                 fprintf(stderr, "distribution specified but no latency and jitter values\n");
270                 explain();
271                 return -1;
272         }
273
274         if (addattr_l(n, TCA_BUF_MAX, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
275                 return -1;
276
277         if (present[TCA_NETEM_CORR] &&
278             addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
279                         return -1;
280
281         if (present[TCA_NETEM_REORDER] && 
282             addattr_l(n, TCA_BUF_MAX, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
283                 return -1;
284
285         if (present[TCA_NETEM_CORRUPT] &&
286             addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
287                 return -1;
288
289         if (dist_data) {
290                 if (addattr_l(n, 32768, TCA_NETEM_DELAY_DIST,
291                               dist_data, dist_size*sizeof(dist_data[0])) < 0)
292                         return -1;
293         }
294         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
295         return 0;
296 }
297
298 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
299 {
300         const struct tc_netem_corr *cor = NULL;
301         const struct tc_netem_reorder *reorder = NULL;
302         const struct tc_netem_corrupt *corrupt = NULL;
303         struct tc_netem_qopt qopt;
304         int len = RTA_PAYLOAD(opt) - sizeof(qopt);
305         SPRINT_BUF(b1);
306
307         if (opt == NULL)
308                 return 0;
309
310         if (len < 0) {
311                 fprintf(stderr, "options size error\n");
312                 return -1;
313         }
314         memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
315
316         if (len > 0) {
317                 struct rtattr *tb[TCA_NETEM_MAX+1];
318                 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
319                              len);
320
321                 if (tb[TCA_NETEM_CORR]) {
322                         if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
323                                 return -1;
324                         cor = RTA_DATA(tb[TCA_NETEM_CORR]);
325                 }
326                 if (tb[TCA_NETEM_REORDER]) {
327                         if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
328                                 return -1;
329                         reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
330                 }
331                 if (tb[TCA_NETEM_CORRUPT]) {
332                         if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
333                                 return -1;
334                         corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
335                 }
336         }
337
338         fprintf(f, "limit %d", qopt.limit);
339
340         if (qopt.latency) {
341                 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
342
343                 if (qopt.jitter) {
344                         fprintf(f, "  %s", sprint_ticks(qopt.jitter, b1));
345                         if (cor && cor->delay_corr)
346                                 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
347                 }
348         }
349
350         if (qopt.loss) {
351                 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
352                 if (cor && cor->loss_corr)
353                         fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
354         }
355
356         if (qopt.duplicate) {
357                 fprintf(f, " duplicate %s",
358                         sprint_percent(qopt.duplicate, b1));
359                 if (cor && cor->dup_corr)
360                         fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
361         }
362
363         if (reorder && reorder->probability) {
364                 fprintf(f, " reorder %s",
365                         sprint_percent(reorder->probability, b1));
366                 if (reorder->correlation)
367                         fprintf(f, " %s",
368                                 sprint_percent(reorder->correlation, b1));
369         }
370
371         if (corrupt && corrupt->probability) {
372                 fprintf(f, " corrupt %s",
373                         sprint_percent(corrupt->probability, b1));
374                 if (corrupt->correlation)
375                         fprintf(f, " %s",
376                                 sprint_percent(corrupt->correlation, b1));
377         }
378
379         if (qopt.gap)
380                 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
381
382         return 0;
383 }
384
385 struct qdisc_util netem_qdisc_util = {
386         .id             = "netem",
387         .parse_qopt     = netem_parse_opt,
388         .print_qopt     = netem_print_opt,
389 };
390