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