]>
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> | |
3c7950af SH |
15 | #include <math.h> |
16 | #include <ctype.h> | |
309a4c90 SH |
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> | |
b7be3d0c | 24 | #include <errno.h> |
309a4c90 SH |
25 | |
26 | #include "utils.h" | |
27 | #include "tc_util.h" | |
b7be3d0c | 28 | #include "tc_common.h" |
309a4c90 SH |
29 | |
30 | static void explain(void) | |
31 | { | |
ae665a52 | 32 | fprintf(stderr, |
32a121cb | 33 | "Usage: ... netem [ limit PACKETS ]\n" \ |
ea8fc104 SH |
34 | " [ delay TIME [ JITTER [CORRELATION]]]\n" \ |
35 | " [ distribution {uniform|normal|pareto|paretonormal} ]\n" \ | |
32a121cb | 36 | " [ corrupt PERCENT [CORRELATION]]\n" \ |
b7be3d0c | 37 | " [ duplicate PERCENT [CORRELATION]]\n" \ |
3c7950af SH |
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" \ | |
1070205d | 41 | " [ ecn ]\n" \ |
6b8dc4de HPP |
42 | " [ reorder PRECENT [CORRELATION] [ gap DISTANCE ]]\n" \ |
43 | " [ rate RATE [PACKETOVERHEAD] [CELLSIZE] [CELLOVERHEAD]]\n"); | |
309a4c90 SH |
44 | } |
45 | ||
46 | static void explain1(const char *arg) | |
47 | { | |
48 | fprintf(stderr, "Illegal \"%s\"\n", arg); | |
49 | } | |
50 | ||
3c7950af | 51 | /* Upper bound on size of distribution |
c1b81cb5 SH |
52 | * really (TCA_BUF_MAX - other headers) / sizeof (__s16) |
53 | */ | |
54 | #define MAX_DIST (16*1024) | |
55 | ||
3c7950af SH |
56 | static const double max_percent_value = 0xffffffff; |
57 | ||
58 | /* scaled value used to percent of maximum. */ | |
59 | static void set_percent(__u32 *percent, double per) | |
60 | { | |
32a121cb | 61 | *percent = (unsigned int) rint(per * max_percent_value); |
3c7950af SH |
62 | } |
63 | ||
64 | ||
65 | /* Parse either a fraction '.3' or percent '30% | |
66 | * return: 0 = ok, -1 = error, 1 = out of range | |
67 | */ | |
68 | static int parse_percent(double *val, const char *str) | |
69 | { | |
70 | char *p; | |
71 | ||
72 | *val = strtod(str, &p) / 100.; | |
32a121cb | 73 | if (*p && strcmp(p, "%")) |
3c7950af SH |
74 | return -1; |
75 | ||
76 | return 0; | |
77 | } | |
78 | ||
79 | static int get_percent(__u32 *percent, const char *str) | |
80 | { | |
81 | double per; | |
82 | ||
83 | if (parse_percent(&per, str)) | |
84 | return -1; | |
85 | ||
86 | set_percent(percent, per); | |
87 | return 0; | |
88 | } | |
89 | ||
d1f28cf1 | 90 | static void print_percent(char *buf, int len, __u32 per) |
3c7950af SH |
91 | { |
92 | snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value); | |
93 | } | |
94 | ||
32a121cb | 95 | static char *sprint_percent(__u32 per, char *buf) |
3c7950af SH |
96 | { |
97 | print_percent(buf, SPRINT_BSIZE-1, per); | |
98 | return buf; | |
99 | } | |
100 | ||
2e21655e SH |
101 | /* |
102 | * Simplistic file parser for distrbution data. | |
103 | * Format is: | |
104 | * # comment line(s) | |
c1b81cb5 | 105 | * data0 data1 ... |
2e21655e | 106 | */ |
c1b81cb5 | 107 | static int get_distribution(const char *type, __s16 *data, int maxdata) |
b7be3d0c SH |
108 | { |
109 | FILE *f; | |
110 | int n; | |
2e21655e SH |
111 | long x; |
112 | size_t len; | |
fb9b1d0f | 113 | char *line = NULL; |
2e21655e | 114 | char name[128]; |
b7be3d0c | 115 | |
aa27f88c | 116 | snprintf(name, sizeof(name), "%s/%s.dist", get_tc_lib(), type); |
2e21655e | 117 | if ((f = fopen(name, "r")) == NULL) { |
ae665a52 | 118 | fprintf(stderr, "No distribution data for %s (%s: %s)\n", |
2e21655e | 119 | type, name, strerror(errno)); |
b7be3d0c SH |
120 | return -1; |
121 | } | |
ae665a52 | 122 | |
b7be3d0c | 123 | n = 0; |
2e21655e SH |
124 | while (getline(&line, &len, f) != -1) { |
125 | char *p, *endp; | |
32a121cb | 126 | |
2e21655e | 127 | if (*line == '\n' || *line == '#') |
b7be3d0c SH |
128 | continue; |
129 | ||
2e21655e SH |
130 | for (p = line; ; p = endp) { |
131 | x = strtol(p, &endp, 0); | |
ae665a52 | 132 | if (endp == p) |
2e21655e SH |
133 | break; |
134 | ||
c1b81cb5 | 135 | if (n >= maxdata) { |
2e21655e SH |
136 | fprintf(stderr, "%s: too much data\n", |
137 | name); | |
138 | n = -1; | |
139 | goto error; | |
140 | } | |
b7be3d0c SH |
141 | data[n++] = x; |
142 | } | |
143 | } | |
2e21655e SH |
144 | error: |
145 | free(line); | |
b7be3d0c | 146 | fclose(f); |
b7be3d0c SH |
147 | return n; |
148 | } | |
149 | ||
3c7950af | 150 | #define NEXT_IS_NUMBER() (NEXT_ARG_OK() && isdigit(argv[1][0])) |
e72ca3fb JN |
151 | #define NEXT_IS_SIGNED_NUMBER() \ |
152 | (NEXT_ARG_OK() && (isdigit(argv[1][0]) || argv[1][0] == '-')) | |
b7be3d0c | 153 | |
ae665a52 | 154 | /* Adjust for the fact that psched_ticks aren't always usecs |
b7be3d0c SH |
155 | (based on kernel PSCHED_CLOCK configuration */ |
156 | static int get_ticks(__u32 *ticks, const char *str) | |
157 | { | |
32a121cb | 158 | unsigned int t; |
b7be3d0c | 159 | |
32a121cb | 160 | if (get_time(&t, str)) |
b7be3d0c | 161 | return -1; |
ae665a52 | 162 | |
8f34caaf PM |
163 | if (tc_core_time2big(t)) { |
164 | fprintf(stderr, "Illegal %u time (too large)\n", t); | |
fa565130 SH |
165 | return -1; |
166 | } | |
167 | ||
8f34caaf | 168 | *ticks = tc_core_time2tick(t); |
b7be3d0c SH |
169 | return 0; |
170 | } | |
171 | ||
ae665a52 | 172 | static int netem_parse_opt(struct qdisc_util *qu, int argc, char **argv, |
309a4c90 SH |
173 | struct nlmsghdr *n) |
174 | { | |
fcbd0165 | 175 | int dist_size = 0; |
2e21655e | 176 | struct rtattr *tail; |
3c7950af | 177 | struct tc_netem_qopt opt = { .limit = 1000 }; |
2e21655e | 178 | struct tc_netem_corr cor; |
ea8fc104 | 179 | struct tc_netem_reorder reorder; |
a31a5d59 | 180 | struct tc_netem_corrupt corrupt; |
3c7950af SH |
181 | struct tc_netem_gimodel gimodel; |
182 | struct tc_netem_gemodel gemodel; | |
6b8dc4de | 183 | struct tc_netem_rate rate; |
a31a5d59 | 184 | __s16 *dist_data = NULL; |
3c7950af | 185 | __u16 loss_type = NETEM_LOSS_UNSPEC; |
40076f62 | 186 | int present[__TCA_NETEM_MAX]; |
dad2f72b | 187 | __u64 rate64 = 0; |
309a4c90 | 188 | |
2e21655e | 189 | memset(&cor, 0, sizeof(cor)); |
ea8fc104 | 190 | memset(&reorder, 0, sizeof(reorder)); |
a31a5d59 | 191 | memset(&corrupt, 0, sizeof(corrupt)); |
6b8dc4de | 192 | memset(&rate, 0, sizeof(rate)); |
40076f62 | 193 | memset(present, 0, sizeof(present)); |
309a4c90 | 194 | |
32a121cb | 195 | for ( ; argc > 0; --argc, ++argv) { |
309a4c90 SH |
196 | if (matches(*argv, "limit") == 0) { |
197 | NEXT_ARG(); | |
2e21655e | 198 | if (get_size(&opt.limit, *argv)) { |
309a4c90 SH |
199 | explain1("limit"); |
200 | return -1; | |
201 | } | |
b7be3d0c SH |
202 | } else if (matches(*argv, "latency") == 0 || |
203 | matches(*argv, "delay") == 0) { | |
309a4c90 | 204 | NEXT_ARG(); |
2e21655e | 205 | if (get_ticks(&opt.latency, *argv)) { |
309a4c90 SH |
206 | explain1("latency"); |
207 | return -1; | |
208 | } | |
b7be3d0c SH |
209 | |
210 | if (NEXT_IS_NUMBER()) { | |
211 | NEXT_ARG(); | |
2e21655e | 212 | if (get_ticks(&opt.jitter, *argv)) { |
b7be3d0c SH |
213 | explain1("latency"); |
214 | return -1; | |
215 | } | |
216 | ||
217 | if (NEXT_IS_NUMBER()) { | |
218 | NEXT_ARG(); | |
40076f62 | 219 | ++present[TCA_NETEM_CORR]; |
3c7950af | 220 | if (get_percent(&cor.delay_corr, *argv)) { |
b7be3d0c SH |
221 | explain1("latency"); |
222 | return -1; | |
223 | } | |
224 | } | |
225 | } | |
226 | } else if (matches(*argv, "loss") == 0 || | |
227 | matches(*argv, "drop") == 0) { | |
3c7950af SH |
228 | if (opt.loss > 0 || loss_type != NETEM_LOSS_UNSPEC) { |
229 | explain1("duplicate loss argument\n"); | |
309a4c90 SH |
230 | return -1; |
231 | } | |
3c7950af SH |
232 | |
233 | NEXT_ARG(); | |
234 | /* Old (deprecated) random loss model syntax */ | |
235 | if (isdigit(argv[0][0])) | |
236 | goto random_loss_model; | |
237 | ||
238 | if (!strcmp(*argv, "random")) { | |
b7be3d0c | 239 | NEXT_ARG(); |
32a121cb | 240 | random_loss_model: |
3c7950af SH |
241 | if (get_percent(&opt.loss, *argv)) { |
242 | explain1("loss percent"); | |
243 | return -1; | |
244 | } | |
245 | if (NEXT_IS_NUMBER()) { | |
246 | NEXT_ARG(); | |
247 | ++present[TCA_NETEM_CORR]; | |
248 | if (get_percent(&cor.loss_corr, *argv)) { | |
249 | explain1("loss correllation"); | |
250 | return -1; | |
251 | } | |
252 | } | |
253 | } else if (!strcmp(*argv, "state")) { | |
254 | double p13; | |
255 | ||
256 | NEXT_ARG(); | |
257 | if (parse_percent(&p13, *argv)) { | |
258 | explain1("loss p13"); | |
259 | return -1; | |
260 | } | |
261 | ||
262 | /* set defaults */ | |
263 | set_percent(&gimodel.p13, p13); | |
264 | set_percent(&gimodel.p31, 1. - p13); | |
265 | set_percent(&gimodel.p32, 0); | |
266 | set_percent(&gimodel.p23, 1.); | |
8f9672af | 267 | set_percent(&gimodel.p14, 0); |
3c7950af SH |
268 | loss_type = NETEM_LOSS_GI; |
269 | ||
270 | if (!NEXT_IS_NUMBER()) | |
271 | continue; | |
272 | NEXT_ARG(); | |
273 | if (get_percent(&gimodel.p31, *argv)) { | |
274 | explain1("loss p31"); | |
275 | return -1; | |
276 | } | |
277 | ||
278 | if (!NEXT_IS_NUMBER()) | |
279 | continue; | |
280 | NEXT_ARG(); | |
281 | if (get_percent(&gimodel.p32, *argv)) { | |
282 | explain1("loss p32"); | |
283 | return -1; | |
284 | } | |
285 | ||
286 | if (!NEXT_IS_NUMBER()) | |
287 | continue; | |
288 | NEXT_ARG(); | |
289 | if (get_percent(&gimodel.p23, *argv)) { | |
290 | explain1("loss p23"); | |
291 | return -1; | |
292 | } | |
8f9672af JV |
293 | if (!NEXT_IS_NUMBER()) |
294 | continue; | |
295 | NEXT_ARG(); | |
296 | if (get_percent(&gimodel.p14, *argv)) { | |
297 | explain1("loss p14"); | |
298 | return -1; | |
299 | } | |
3c7950af SH |
300 | |
301 | } else if (!strcmp(*argv, "gemodel")) { | |
302 | NEXT_ARG(); | |
303 | if (get_percent(&gemodel.p, *argv)) { | |
304 | explain1("loss gemodel p"); | |
305 | return -1; | |
306 | } | |
307 | ||
308 | /* set defaults */ | |
309 | set_percent(&gemodel.r, 1.); | |
310 | set_percent(&gemodel.h, 0); | |
3757185b | 311 | set_percent(&gemodel.k1, 0); |
3c7950af SH |
312 | loss_type = NETEM_LOSS_GE; |
313 | ||
314 | if (!NEXT_IS_NUMBER()) | |
315 | continue; | |
316 | NEXT_ARG(); | |
317 | if (get_percent(&gemodel.r, *argv)) { | |
318 | explain1("loss gemodel r"); | |
319 | return -1; | |
320 | } | |
321 | ||
322 | if (!NEXT_IS_NUMBER()) | |
323 | continue; | |
324 | NEXT_ARG(); | |
325 | if (get_percent(&gemodel.h, *argv)) { | |
326 | explain1("loss gemodel h"); | |
327 | return -1; | |
328 | } | |
3757185b JV |
329 | /* netem option is "1-h" but kernel |
330 | * expects "h". | |
331 | */ | |
332 | gemodel.h = max_percent_value - gemodel.h; | |
3c7950af SH |
333 | |
334 | if (!NEXT_IS_NUMBER()) | |
335 | continue; | |
336 | NEXT_ARG(); | |
337 | if (get_percent(&gemodel.k1, *argv)) { | |
338 | explain1("loss gemodel k"); | |
b7be3d0c SH |
339 | return -1; |
340 | } | |
3c7950af SH |
341 | } else { |
342 | fprintf(stderr, "Unknown loss parameter: %s\n", | |
343 | *argv); | |
344 | return -1; | |
b7be3d0c | 345 | } |
1070205d VS |
346 | } else if (matches(*argv, "ecn") == 0) { |
347 | present[TCA_NETEM_ECN] = 1; | |
ea8fc104 SH |
348 | } else if (matches(*argv, "reorder") == 0) { |
349 | NEXT_ARG(); | |
40076f62 | 350 | present[TCA_NETEM_REORDER] = 1; |
ea8fc104 SH |
351 | if (get_percent(&reorder.probability, *argv)) { |
352 | explain1("reorder"); | |
353 | return -1; | |
354 | } | |
355 | if (NEXT_IS_NUMBER()) { | |
356 | NEXT_ARG(); | |
40076f62 | 357 | ++present[TCA_NETEM_CORR]; |
ea8fc104 SH |
358 | if (get_percent(&reorder.correlation, *argv)) { |
359 | explain1("reorder"); | |
360 | return -1; | |
361 | } | |
362 | } | |
a31a5d59 SH |
363 | } else if (matches(*argv, "corrupt") == 0) { |
364 | NEXT_ARG(); | |
40076f62 | 365 | present[TCA_NETEM_CORRUPT] = 1; |
a31a5d59 SH |
366 | if (get_percent(&corrupt.probability, *argv)) { |
367 | explain1("corrupt"); | |
368 | return -1; | |
369 | } | |
370 | if (NEXT_IS_NUMBER()) { | |
371 | NEXT_ARG(); | |
40076f62 | 372 | ++present[TCA_NETEM_CORR]; |
a31a5d59 SH |
373 | if (get_percent(&corrupt.correlation, *argv)) { |
374 | explain1("corrupt"); | |
375 | return -1; | |
376 | } | |
377 | } | |
309a4c90 | 378 | } else if (matches(*argv, "gap") == 0) { |
309a4c90 | 379 | NEXT_ARG(); |
2e21655e | 380 | if (get_u32(&opt.gap, *argv, 0)) { |
309a4c90 SH |
381 | explain1("gap"); |
382 | return -1; | |
383 | } | |
ffb79d06 | 384 | } else if (matches(*argv, "duplicate") == 0) { |
309a4c90 | 385 | NEXT_ARG(); |
2e21655e | 386 | if (get_percent(&opt.duplicate, *argv)) { |
ffb79d06 | 387 | explain1("duplicate"); |
309a4c90 SH |
388 | return -1; |
389 | } | |
b7be3d0c SH |
390 | if (NEXT_IS_NUMBER()) { |
391 | NEXT_ARG(); | |
2e21655e | 392 | if (get_percent(&cor.dup_corr, *argv)) { |
b7be3d0c SH |
393 | explain1("duplicate"); |
394 | return -1; | |
395 | } | |
396 | } | |
397 | } else if (matches(*argv, "distribution") == 0) { | |
309a4c90 | 398 | NEXT_ARG(); |
c1b81cb5 SH |
399 | dist_data = calloc(sizeof(dist_data[0]), MAX_DIST); |
400 | dist_size = get_distribution(*argv, dist_data, MAX_DIST); | |
401 | if (dist_size <= 0) { | |
402 | free(dist_data); | |
309a4c90 | 403 | return -1; |
c1b81cb5 | 404 | } |
6b8dc4de HPP |
405 | } else if (matches(*argv, "rate") == 0) { |
406 | ++present[TCA_NETEM_RATE]; | |
407 | NEXT_ARG(); | |
dad2f72b | 408 | if (get_rate64(&rate64, *argv)) { |
6b8dc4de HPP |
409 | explain1("rate"); |
410 | return -1; | |
411 | } | |
e72ca3fb | 412 | if (NEXT_IS_SIGNED_NUMBER()) { |
6b8dc4de HPP |
413 | NEXT_ARG(); |
414 | if (get_s32(&rate.packet_overhead, *argv, 0)) { | |
415 | explain1("rate"); | |
416 | return -1; | |
417 | } | |
418 | } | |
419 | if (NEXT_IS_NUMBER()) { | |
420 | NEXT_ARG(); | |
421 | if (get_u32(&rate.cell_size, *argv, 0)) { | |
422 | explain1("rate"); | |
423 | return -1; | |
424 | } | |
425 | } | |
e72ca3fb | 426 | if (NEXT_IS_SIGNED_NUMBER()) { |
6b8dc4de HPP |
427 | NEXT_ARG(); |
428 | if (get_s32(&rate.cell_overhead, *argv, 0)) { | |
429 | explain1("rate"); | |
430 | return -1; | |
431 | } | |
432 | } | |
309a4c90 SH |
433 | } else if (strcmp(*argv, "help") == 0) { |
434 | explain(); | |
435 | return -1; | |
436 | } else { | |
437 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
438 | explain(); | |
439 | return -1; | |
440 | } | |
309a4c90 SH |
441 | } |
442 | ||
1a1d22a7 | 443 | tail = NLMSG_TAIL(n); |
025dc69a | 444 | |
ea8fc104 SH |
445 | if (reorder.probability) { |
446 | if (opt.latency == 0) { | |
447 | fprintf(stderr, "reordering not possible without specifying some delay\n"); | |
14a1c164 VS |
448 | explain(); |
449 | return -1; | |
ea8fc104 SH |
450 | } |
451 | if (opt.gap == 0) | |
452 | opt.gap = 1; | |
453 | } else if (opt.gap > 0) { | |
454 | fprintf(stderr, "gap specified without reorder probability\n"); | |
455 | explain(); | |
456 | return -1; | |
457 | } | |
458 | ||
1070205d VS |
459 | if (present[TCA_NETEM_ECN]) { |
460 | if (opt.loss <= 0 && loss_type == NETEM_LOSS_UNSPEC) { | |
461 | fprintf(stderr, "ecn requested without loss model\n"); | |
462 | explain(); | |
463 | return -1; | |
464 | } | |
465 | } | |
466 | ||
a31a5d59 | 467 | if (dist_data && (opt.latency == 0 || opt.jitter == 0)) { |
ea8fc104 SH |
468 | fprintf(stderr, "distribution specified but no latency and jitter values\n"); |
469 | explain(); | |
470 | return -1; | |
471 | } | |
472 | ||
c1b81cb5 | 473 | if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0) |
a31a5d59 | 474 | return -1; |
2e21655e | 475 | |
40076f62 | 476 | if (present[TCA_NETEM_CORR] && |
c1b81cb5 | 477 | addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0) |
a31a5d59 | 478 | return -1; |
a31a5d59 | 479 | |
3c7950af | 480 | if (present[TCA_NETEM_REORDER] && |
c1b81cb5 | 481 | addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0) |
e9bc3c40 | 482 | return -1; |
a31a5d59 | 483 | |
1070205d VS |
484 | if (present[TCA_NETEM_ECN] && |
485 | addattr_l(n, 1024, TCA_NETEM_ECN, &present[TCA_NETEM_ECN], | |
486 | sizeof(present[TCA_NETEM_ECN])) < 0) | |
487 | return -1; | |
488 | ||
40076f62 | 489 | if (present[TCA_NETEM_CORRUPT] && |
c1b81cb5 | 490 | addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0) |
40076f62 | 491 | return -1; |
a31a5d59 | 492 | |
3c7950af SH |
493 | if (loss_type != NETEM_LOSS_UNSPEC) { |
494 | struct rtattr *start; | |
495 | ||
496 | start = addattr_nest(n, 1024, TCA_NETEM_LOSS | NLA_F_NESTED); | |
497 | if (loss_type == NETEM_LOSS_GI) { | |
498 | if (addattr_l(n, 1024, NETEM_LOSS_GI, | |
499 | &gimodel, sizeof(gimodel)) < 0) | |
500 | return -1; | |
501 | } else if (loss_type == NETEM_LOSS_GE) { | |
502 | if (addattr_l(n, 1024, NETEM_LOSS_GE, | |
503 | &gemodel, sizeof(gemodel)) < 0) | |
504 | return -1; | |
505 | } else { | |
506 | fprintf(stderr, "loss in the weeds!\n"); | |
507 | return -1; | |
508 | } | |
3d0b7439 | 509 | |
3c7950af SH |
510 | addattr_nest_end(n, start); |
511 | } | |
512 | ||
dad2f72b YY |
513 | if (present[TCA_NETEM_RATE]) { |
514 | if (rate64 >= (1ULL << 32)) { | |
515 | if (addattr_l(n, 1024, | |
516 | TCA_NETEM_RATE64, &rate64, sizeof(rate64)) < 0) | |
517 | return -1; | |
518 | rate.rate = ~0U; | |
519 | } else { | |
520 | rate.rate = rate64; | |
521 | } | |
522 | if (addattr_l(n, 1024, TCA_NETEM_RATE, &rate, sizeof(rate)) < 0) | |
523 | return -1; | |
524 | } | |
6b8dc4de | 525 | |
a31a5d59 | 526 | if (dist_data) { |
c1b81cb5 SH |
527 | if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]), |
528 | TCA_NETEM_DELAY_DIST, | |
529 | dist_data, dist_size * sizeof(dist_data[0])) < 0) | |
a31a5d59 | 530 | return -1; |
c1b81cb5 | 531 | free(dist_data); |
2e21655e | 532 | } |
1a1d22a7 | 533 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
b7be3d0c | 534 | return 0; |
309a4c90 SH |
535 | } |
536 | ||
537 | static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
538 | { | |
2e21655e | 539 | const struct tc_netem_corr *cor = NULL; |
ea8fc104 | 540 | const struct tc_netem_reorder *reorder = NULL; |
a31a5d59 | 541 | const struct tc_netem_corrupt *corrupt = NULL; |
3c7950af SH |
542 | const struct tc_netem_gimodel *gimodel = NULL; |
543 | const struct tc_netem_gemodel *gemodel = NULL; | |
1070205d | 544 | int *ecn = NULL; |
2e21655e | 545 | struct tc_netem_qopt qopt; |
6b8dc4de | 546 | const struct tc_netem_rate *rate = NULL; |
2e21655e | 547 | int len = RTA_PAYLOAD(opt) - sizeof(qopt); |
dad2f72b | 548 | __u64 rate64 = 0; |
32a121cb | 549 | |
309a4c90 | 550 | SPRINT_BUF(b1); |
309a4c90 SH |
551 | |
552 | if (opt == NULL) | |
553 | return 0; | |
554 | ||
2e21655e SH |
555 | if (len < 0) { |
556 | fprintf(stderr, "options size error\n"); | |
309a4c90 | 557 | return -1; |
b7be3d0c | 558 | } |
2e21655e SH |
559 | memcpy(&qopt, RTA_DATA(opt), sizeof(qopt)); |
560 | ||
561 | if (len > 0) { | |
1d2d1cb5 | 562 | struct rtattr *tb[TCA_NETEM_MAX+1]; |
32a121cb | 563 | |
2e21655e SH |
564 | parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt), |
565 | len); | |
ae665a52 | 566 | |
2e21655e SH |
567 | if (tb[TCA_NETEM_CORR]) { |
568 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor)) | |
569 | return -1; | |
570 | cor = RTA_DATA(tb[TCA_NETEM_CORR]); | |
571 | } | |
ea8fc104 SH |
572 | if (tb[TCA_NETEM_REORDER]) { |
573 | if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder)) | |
574 | return -1; | |
575 | reorder = RTA_DATA(tb[TCA_NETEM_REORDER]); | |
576 | } | |
a31a5d59 SH |
577 | if (tb[TCA_NETEM_CORRUPT]) { |
578 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt)) | |
579 | return -1; | |
e9bc3c40 | 580 | corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]); |
a31a5d59 | 581 | } |
3c7950af SH |
582 | if (tb[TCA_NETEM_LOSS]) { |
583 | struct rtattr *lb[NETEM_LOSS_MAX + 1]; | |
584 | ||
585 | parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]); | |
586 | if (lb[NETEM_LOSS_GI]) | |
8f9672af | 587 | gimodel = RTA_DATA(lb[NETEM_LOSS_GI]); |
3c7950af SH |
588 | if (lb[NETEM_LOSS_GE]) |
589 | gemodel = RTA_DATA(lb[NETEM_LOSS_GE]); | |
3d0b7439 | 590 | } |
6b8dc4de HPP |
591 | if (tb[TCA_NETEM_RATE]) { |
592 | if (RTA_PAYLOAD(tb[TCA_NETEM_RATE]) < sizeof(*rate)) | |
593 | return -1; | |
594 | rate = RTA_DATA(tb[TCA_NETEM_RATE]); | |
595 | } | |
1070205d VS |
596 | if (tb[TCA_NETEM_ECN]) { |
597 | if (RTA_PAYLOAD(tb[TCA_NETEM_ECN]) < sizeof(*ecn)) | |
598 | return -1; | |
599 | ecn = RTA_DATA(tb[TCA_NETEM_ECN]); | |
600 | } | |
dad2f72b YY |
601 | if (tb[TCA_NETEM_RATE64]) { |
602 | if (RTA_PAYLOAD(tb[TCA_NETEM_RATE64]) < sizeof(rate64)) | |
603 | return -1; | |
604 | rate64 = rta_getattr_u64(tb[TCA_NETEM_RATE64]); | |
605 | } | |
2e21655e | 606 | } |
309a4c90 | 607 | |
2e21655e | 608 | fprintf(f, "limit %d", qopt.limit); |
31fa60e0 | 609 | |
2e21655e SH |
610 | if (qopt.latency) { |
611 | fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1)); | |
b7be3d0c | 612 | |
2e21655e SH |
613 | if (qopt.jitter) { |
614 | fprintf(f, " %s", sprint_ticks(qopt.jitter, b1)); | |
615 | if (cor && cor->delay_corr) | |
616 | fprintf(f, " %s", sprint_percent(cor->delay_corr, b1)); | |
b7be3d0c SH |
617 | } |
618 | } | |
619 | ||
2e21655e SH |
620 | if (qopt.loss) { |
621 | fprintf(f, " loss %s", sprint_percent(qopt.loss, b1)); | |
622 | if (cor && cor->loss_corr) | |
623 | fprintf(f, " %s", sprint_percent(cor->loss_corr, b1)); | |
b7be3d0c SH |
624 | } |
625 | ||
3c7950af SH |
626 | if (gimodel) { |
627 | fprintf(f, " loss state p13 %s", sprint_percent(gimodel->p13, b1)); | |
628 | fprintf(f, " p31 %s", sprint_percent(gimodel->p31, b1)); | |
629 | fprintf(f, " p32 %s", sprint_percent(gimodel->p32, b1)); | |
630 | fprintf(f, " p23 %s", sprint_percent(gimodel->p23, b1)); | |
631 | fprintf(f, " p14 %s", sprint_percent(gimodel->p14, b1)); | |
632 | } | |
633 | ||
634 | if (gemodel) { | |
3757185b | 635 | fprintf(f, " loss gemodel p %s", |
3c7950af SH |
636 | sprint_percent(gemodel->p, b1)); |
637 | fprintf(f, " r %s", sprint_percent(gemodel->r, b1)); | |
3757185b JV |
638 | fprintf(f, " 1-h %s", sprint_percent(max_percent_value - |
639 | gemodel->h, b1)); | |
3c7950af SH |
640 | fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1)); |
641 | } | |
642 | ||
2e21655e | 643 | if (qopt.duplicate) { |
b7be3d0c | 644 | fprintf(f, " duplicate %s", |
2e21655e SH |
645 | sprint_percent(qopt.duplicate, b1)); |
646 | if (cor && cor->dup_corr) | |
647 | fprintf(f, " %s", sprint_percent(cor->dup_corr, b1)); | |
b7be3d0c | 648 | } |
ae665a52 | 649 | |
ea8fc104 | 650 | if (reorder && reorder->probability) { |
ae665a52 | 651 | fprintf(f, " reorder %s", |
ea8fc104 SH |
652 | sprint_percent(reorder->probability, b1)); |
653 | if (reorder->correlation) | |
ae665a52 | 654 | fprintf(f, " %s", |
ea8fc104 SH |
655 | sprint_percent(reorder->correlation, b1)); |
656 | } | |
b7be3d0c | 657 | |
a31a5d59 | 658 | if (corrupt && corrupt->probability) { |
ae665a52 | 659 | fprintf(f, " corrupt %s", |
a31a5d59 SH |
660 | sprint_percent(corrupt->probability, b1)); |
661 | if (corrupt->correlation) | |
ae665a52 | 662 | fprintf(f, " %s", |
a31a5d59 SH |
663 | sprint_percent(corrupt->correlation, b1)); |
664 | } | |
665 | ||
6b8dc4de | 666 | if (rate && rate->rate) { |
dad2f72b YY |
667 | if (rate64) |
668 | fprintf(f, " rate %s", sprint_rate(rate64, b1)); | |
669 | else | |
670 | fprintf(f, " rate %s", sprint_rate(rate->rate, b1)); | |
6b8dc4de HPP |
671 | if (rate->packet_overhead) |
672 | fprintf(f, " packetoverhead %d", rate->packet_overhead); | |
673 | if (rate->cell_size) | |
674 | fprintf(f, " cellsize %u", rate->cell_size); | |
675 | if (rate->cell_overhead) | |
676 | fprintf(f, " celloverhead %d", rate->cell_overhead); | |
677 | } | |
678 | ||
1070205d VS |
679 | if (ecn) |
680 | fprintf(f, " ecn "); | |
681 | ||
2e21655e SH |
682 | if (qopt.gap) |
683 | fprintf(f, " gap %lu", (unsigned long)qopt.gap); | |
309a4c90 | 684 | |
1070205d | 685 | |
309a4c90 SH |
686 | return 0; |
687 | } | |
688 | ||
95812b56 | 689 | struct qdisc_util netem_qdisc_util = { |
32a121cb | 690 | .id = "netem", |
31fa60e0 SH |
691 | .parse_qopt = netem_parse_opt, |
692 | .print_qopt = netem_print_opt, | |
309a4c90 | 693 | }; |