This commit was generated by cvs2svn to compensate for changes in r2587,
[iproute2.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), "/usr/lib/tc/%s.dist", 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_usecs(&t, str))
112                 return -1;
113         
114         *ticks = tc_core_usec2tick(t);
115         return 0;
116 }
117
118 static char *sprint_ticks(__u32 ticks, char *buf)
119 {
120         return sprint_usecs(tc_core_tick2usec(ticks), buf);
121 }
122
123
124 static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv, 
125                            struct nlmsghdr *n)
126 {
127         size_t dist_size = 0;
128         struct rtattr *tail;
129         struct tc_netem_qopt opt;
130         struct tc_netem_corr cor;
131         struct tc_netem_reorder reorder;
132         struct tc_netem_corrupt corrupt;
133         __s16 *dist_data = NULL;
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
141         while (argc > 0) {
142                 if (matches(*argv, "limit") == 0) {
143                         NEXT_ARG();
144                         if (get_size(&opt.limit, *argv)) {
145                                 explain1("limit");
146                                 return -1;
147                         }
148                 } else if (matches(*argv, "latency") == 0 ||
149                            matches(*argv, "delay") == 0) {
150                         NEXT_ARG();
151                         if (get_ticks(&opt.latency, *argv)) {
152                                 explain1("latency");
153                                 return -1;
154                         }
155
156                         if (NEXT_IS_NUMBER()) {
157                                 NEXT_ARG();
158                                 if (get_ticks(&opt.jitter, *argv)) {
159                                         explain1("latency");
160                                         return -1;
161                                 }
162
163                                 if (NEXT_IS_NUMBER()) {
164                                         NEXT_ARG();
165                                         if (get_percent(&cor.delay_corr, 
166                                                         *argv)) {
167                                                 explain1("latency");
168                                                 return -1;
169                                         }
170                                 }
171                         }
172                 } else if (matches(*argv, "loss") == 0 ||
173                            matches(*argv, "drop") == 0) {
174                         NEXT_ARG();
175                         if (get_percent(&opt.loss, *argv)) {
176                                 explain1("loss");
177                                 return -1;
178                         }
179                         if (NEXT_IS_NUMBER()) {
180                                 NEXT_ARG();
181                                 if (get_percent(&cor.loss_corr, *argv)) {
182                                         explain1("loss");
183                                         return -1;
184                                 }
185                         }
186                 } else if (matches(*argv, "reorder") == 0) {
187                         NEXT_ARG();
188                         if (get_percent(&reorder.probability, *argv)) {
189                                 explain1("reorder");
190                                 return -1;
191                         }
192                         if (NEXT_IS_NUMBER()) {
193                                 NEXT_ARG();
194                                 if (get_percent(&reorder.correlation, *argv)) {
195                                         explain1("reorder");
196                                         return -1;
197                                 }
198                         }
199                 } else if (matches(*argv, "corrupt") == 0) {
200                         NEXT_ARG();
201                         if (get_percent(&corrupt.probability, *argv)) {
202                                 explain1("corrupt");
203                                 return -1;
204                         }
205                         if (NEXT_IS_NUMBER()) {
206                                 NEXT_ARG();
207                                 if (get_percent(&corrupt.correlation, *argv)) {
208                                         explain1("corrupt");
209                                         return -1;
210                                 }
211                         }
212                 } else if (matches(*argv, "gap") == 0) {
213                         NEXT_ARG();
214                         if (get_u32(&opt.gap, *argv, 0)) {
215                                 explain1("gap");
216                                 return -1;
217                         }
218                 } else if (matches(*argv, "duplicate") == 0) {
219                         NEXT_ARG();
220                         if (get_percent(&opt.duplicate, *argv)) {
221                                 explain1("duplicate");
222                                 return -1;
223                         }
224                         if (NEXT_IS_NUMBER()) {
225                                 NEXT_ARG();
226                                 if (get_percent(&cor.dup_corr, *argv)) {
227                                         explain1("duplicate");
228                                         return -1;
229                                 }
230                         }
231                 } else if (matches(*argv, "distribution") == 0) {
232                         NEXT_ARG();
233                         dist_data = alloca(MAXDIST);
234                         dist_size = get_distribution(*argv, dist_data);
235                         if (dist_size < 0)
236                                 return -1;
237                 } else if (strcmp(*argv, "help") == 0) {
238                         explain();
239                         return -1;
240                 } else {
241                         fprintf(stderr, "What is \"%s\"?\n", *argv);
242                         explain();
243                         return -1;
244                 }
245                 argc--; argv++;
246         }
247
248         tail = NLMSG_TAIL(n);
249
250         if (reorder.probability) {
251                 if (opt.latency == 0) {
252                         fprintf(stderr, "reordering not possible without specifying some delay\n");
253                 }
254                 if (opt.gap == 0)
255                         opt.gap = 1;
256         } else if (opt.gap > 0) {
257                 fprintf(stderr, "gap specified without reorder probability\n");
258                 explain();
259                 return -1;
260         }
261
262         if (dist_data && (opt.latency == 0 || opt.jitter == 0)) {
263                 fprintf(stderr, "distribution specified but no latency and jitter values\n");
264                 explain();
265                 return -1;
266         }
267
268         if (addattr_l(n, TCA_BUF_MAX, TCA_OPTIONS, &opt, sizeof(opt)) < 0)
269                 return -1;
270
271         if (cor.delay_corr || cor.loss_corr || cor.dup_corr) {
272                 if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0)
273                         return -1;
274         }
275
276         if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0)
277                 return -1;
278
279         if (corrupt.probability) {
280                 if (addattr_l(n, TCA_BUF_MAX, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0)
281                         return -1;
282         }
283
284         if (dist_data) {
285                 if (addattr_l(n, 32768, TCA_NETEM_DELAY_DIST,
286                               dist_data, dist_size*sizeof(dist_data[0])) < 0)
287                         return -1;
288         }
289         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
290         return 0;
291 }
292
293 static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
294 {
295         const struct tc_netem_corr *cor = NULL;
296         const struct tc_netem_reorder *reorder = NULL;
297         const struct tc_netem_corrupt *corrupt = NULL;
298         struct tc_netem_qopt qopt;
299         int len = RTA_PAYLOAD(opt) - sizeof(qopt);
300         SPRINT_BUF(b1);
301
302         if (opt == NULL)
303                 return 0;
304
305         if (len < 0) {
306                 fprintf(stderr, "options size error\n");
307                 return -1;
308         }
309         memcpy(&qopt, RTA_DATA(opt), sizeof(qopt));
310
311         if (len > 0) {
312                 struct rtattr *tb[TCA_NETEM_MAX+1];
313                 parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt),
314                              len);
315                 
316                 if (tb[TCA_NETEM_CORR]) {
317                         if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor))
318                                 return -1;
319                         cor = RTA_DATA(tb[TCA_NETEM_CORR]);
320                 }
321                 if (tb[TCA_NETEM_REORDER]) {
322                         if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder))
323                                 return -1;
324                         reorder = RTA_DATA(tb[TCA_NETEM_REORDER]);
325                 }
326                 if (tb[TCA_NETEM_CORRUPT]) {
327                         if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt))
328                                 return -1;
329                         corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]);
330                 }
331         }
332
333         fprintf(f, "limit %d", qopt.limit);
334
335         if (qopt.latency) {
336                 fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1));
337
338                 if (qopt.jitter) {
339                         fprintf(f, "  %s", sprint_ticks(qopt.jitter, b1));
340                         if (cor && cor->delay_corr)
341                                 fprintf(f, " %s", sprint_percent(cor->delay_corr, b1));
342                 }
343         }
344
345         if (qopt.loss) {
346                 fprintf(f, " loss %s", sprint_percent(qopt.loss, b1));
347                 if (cor && cor->loss_corr)
348                         fprintf(f, " %s", sprint_percent(cor->loss_corr, b1));
349         }
350
351         if (qopt.duplicate) {
352                 fprintf(f, " duplicate %s",
353                         sprint_percent(qopt.duplicate, b1));
354                 if (cor && cor->dup_corr)
355                         fprintf(f, " %s", sprint_percent(cor->dup_corr, b1));
356         }
357                         
358         if (reorder && reorder->probability) {
359                 fprintf(f, " reorder %s", 
360                         sprint_percent(reorder->probability, b1));
361                 if (reorder->correlation)
362                         fprintf(f, " %s", 
363                                 sprint_percent(reorder->correlation, b1));
364         }
365
366         if (corrupt && corrupt->probability) {
367                 fprintf(f, " corrupt %s", 
368                         sprint_percent(corrupt->probability, b1));
369                 if (corrupt->correlation)
370                         fprintf(f, " %s", 
371                                 sprint_percent(corrupt->correlation, b1));
372         }
373
374         if (qopt.gap)
375                 fprintf(f, " gap %lu", (unsigned long)qopt.gap);
376
377         return 0;
378 }
379
380 struct qdisc_util netem_qdisc_util = {
381         .id             = "netem",
382         .parse_qopt     = netem_parse_opt,
383         .print_qopt     = netem_print_opt,
384 };
385