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