]>
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 }; |
d17b136f PS |
178 | struct tc_netem_corr cor = {}; |
179 | struct tc_netem_reorder reorder = {}; | |
180 | struct tc_netem_corrupt corrupt = {}; | |
3c7950af SH |
181 | struct tc_netem_gimodel gimodel; |
182 | struct tc_netem_gemodel gemodel; | |
d17b136f | 183 | struct tc_netem_rate rate = {}; |
a31a5d59 | 184 | __s16 *dist_data = NULL; |
3c7950af | 185 | __u16 loss_type = NETEM_LOSS_UNSPEC; |
d17b136f | 186 | int present[__TCA_NETEM_MAX] = {}; |
dad2f72b | 187 | __u64 rate64 = 0; |
309a4c90 | 188 | |
32a121cb | 189 | for ( ; argc > 0; --argc, ++argv) { |
309a4c90 SH |
190 | if (matches(*argv, "limit") == 0) { |
191 | NEXT_ARG(); | |
2e21655e | 192 | if (get_size(&opt.limit, *argv)) { |
309a4c90 SH |
193 | explain1("limit"); |
194 | return -1; | |
195 | } | |
b7be3d0c SH |
196 | } else if (matches(*argv, "latency") == 0 || |
197 | matches(*argv, "delay") == 0) { | |
309a4c90 | 198 | NEXT_ARG(); |
2e21655e | 199 | if (get_ticks(&opt.latency, *argv)) { |
309a4c90 SH |
200 | explain1("latency"); |
201 | return -1; | |
202 | } | |
b7be3d0c SH |
203 | |
204 | if (NEXT_IS_NUMBER()) { | |
205 | NEXT_ARG(); | |
2e21655e | 206 | if (get_ticks(&opt.jitter, *argv)) { |
b7be3d0c SH |
207 | explain1("latency"); |
208 | return -1; | |
209 | } | |
210 | ||
211 | if (NEXT_IS_NUMBER()) { | |
212 | NEXT_ARG(); | |
40076f62 | 213 | ++present[TCA_NETEM_CORR]; |
3c7950af | 214 | if (get_percent(&cor.delay_corr, *argv)) { |
b7be3d0c SH |
215 | explain1("latency"); |
216 | return -1; | |
217 | } | |
218 | } | |
219 | } | |
220 | } else if (matches(*argv, "loss") == 0 || | |
221 | matches(*argv, "drop") == 0) { | |
3c7950af SH |
222 | if (opt.loss > 0 || loss_type != NETEM_LOSS_UNSPEC) { |
223 | explain1("duplicate loss argument\n"); | |
309a4c90 SH |
224 | return -1; |
225 | } | |
3c7950af SH |
226 | |
227 | NEXT_ARG(); | |
228 | /* Old (deprecated) random loss model syntax */ | |
229 | if (isdigit(argv[0][0])) | |
230 | goto random_loss_model; | |
231 | ||
232 | if (!strcmp(*argv, "random")) { | |
b7be3d0c | 233 | NEXT_ARG(); |
32a121cb | 234 | random_loss_model: |
3c7950af SH |
235 | if (get_percent(&opt.loss, *argv)) { |
236 | explain1("loss percent"); | |
237 | return -1; | |
238 | } | |
239 | if (NEXT_IS_NUMBER()) { | |
240 | NEXT_ARG(); | |
241 | ++present[TCA_NETEM_CORR]; | |
242 | if (get_percent(&cor.loss_corr, *argv)) { | |
243 | explain1("loss correllation"); | |
244 | return -1; | |
245 | } | |
246 | } | |
247 | } else if (!strcmp(*argv, "state")) { | |
248 | double p13; | |
249 | ||
250 | NEXT_ARG(); | |
251 | if (parse_percent(&p13, *argv)) { | |
252 | explain1("loss p13"); | |
253 | return -1; | |
254 | } | |
255 | ||
256 | /* set defaults */ | |
257 | set_percent(&gimodel.p13, p13); | |
258 | set_percent(&gimodel.p31, 1. - p13); | |
259 | set_percent(&gimodel.p32, 0); | |
260 | set_percent(&gimodel.p23, 1.); | |
8f9672af | 261 | set_percent(&gimodel.p14, 0); |
3c7950af SH |
262 | loss_type = NETEM_LOSS_GI; |
263 | ||
264 | if (!NEXT_IS_NUMBER()) | |
265 | continue; | |
266 | NEXT_ARG(); | |
267 | if (get_percent(&gimodel.p31, *argv)) { | |
268 | explain1("loss p31"); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | if (!NEXT_IS_NUMBER()) | |
273 | continue; | |
274 | NEXT_ARG(); | |
275 | if (get_percent(&gimodel.p32, *argv)) { | |
276 | explain1("loss p32"); | |
277 | return -1; | |
278 | } | |
279 | ||
280 | if (!NEXT_IS_NUMBER()) | |
281 | continue; | |
282 | NEXT_ARG(); | |
283 | if (get_percent(&gimodel.p23, *argv)) { | |
284 | explain1("loss p23"); | |
285 | return -1; | |
286 | } | |
8f9672af JV |
287 | if (!NEXT_IS_NUMBER()) |
288 | continue; | |
289 | NEXT_ARG(); | |
290 | if (get_percent(&gimodel.p14, *argv)) { | |
291 | explain1("loss p14"); | |
292 | return -1; | |
293 | } | |
3c7950af SH |
294 | |
295 | } else if (!strcmp(*argv, "gemodel")) { | |
296 | NEXT_ARG(); | |
297 | if (get_percent(&gemodel.p, *argv)) { | |
298 | explain1("loss gemodel p"); | |
299 | return -1; | |
300 | } | |
301 | ||
302 | /* set defaults */ | |
303 | set_percent(&gemodel.r, 1.); | |
304 | set_percent(&gemodel.h, 0); | |
3757185b | 305 | set_percent(&gemodel.k1, 0); |
3c7950af SH |
306 | loss_type = NETEM_LOSS_GE; |
307 | ||
308 | if (!NEXT_IS_NUMBER()) | |
309 | continue; | |
310 | NEXT_ARG(); | |
311 | if (get_percent(&gemodel.r, *argv)) { | |
312 | explain1("loss gemodel r"); | |
313 | return -1; | |
314 | } | |
315 | ||
316 | if (!NEXT_IS_NUMBER()) | |
317 | continue; | |
318 | NEXT_ARG(); | |
319 | if (get_percent(&gemodel.h, *argv)) { | |
320 | explain1("loss gemodel h"); | |
321 | return -1; | |
322 | } | |
3757185b JV |
323 | /* netem option is "1-h" but kernel |
324 | * expects "h". | |
325 | */ | |
326 | gemodel.h = max_percent_value - gemodel.h; | |
3c7950af SH |
327 | |
328 | if (!NEXT_IS_NUMBER()) | |
329 | continue; | |
330 | NEXT_ARG(); | |
331 | if (get_percent(&gemodel.k1, *argv)) { | |
332 | explain1("loss gemodel k"); | |
b7be3d0c SH |
333 | return -1; |
334 | } | |
3c7950af SH |
335 | } else { |
336 | fprintf(stderr, "Unknown loss parameter: %s\n", | |
337 | *argv); | |
338 | return -1; | |
b7be3d0c | 339 | } |
1070205d VS |
340 | } else if (matches(*argv, "ecn") == 0) { |
341 | present[TCA_NETEM_ECN] = 1; | |
ea8fc104 SH |
342 | } else if (matches(*argv, "reorder") == 0) { |
343 | NEXT_ARG(); | |
40076f62 | 344 | present[TCA_NETEM_REORDER] = 1; |
ea8fc104 SH |
345 | if (get_percent(&reorder.probability, *argv)) { |
346 | explain1("reorder"); | |
347 | return -1; | |
348 | } | |
349 | if (NEXT_IS_NUMBER()) { | |
350 | NEXT_ARG(); | |
40076f62 | 351 | ++present[TCA_NETEM_CORR]; |
ea8fc104 SH |
352 | if (get_percent(&reorder.correlation, *argv)) { |
353 | explain1("reorder"); | |
354 | return -1; | |
355 | } | |
356 | } | |
a31a5d59 SH |
357 | } else if (matches(*argv, "corrupt") == 0) { |
358 | NEXT_ARG(); | |
40076f62 | 359 | present[TCA_NETEM_CORRUPT] = 1; |
a31a5d59 SH |
360 | if (get_percent(&corrupt.probability, *argv)) { |
361 | explain1("corrupt"); | |
362 | return -1; | |
363 | } | |
364 | if (NEXT_IS_NUMBER()) { | |
365 | NEXT_ARG(); | |
40076f62 | 366 | ++present[TCA_NETEM_CORR]; |
a31a5d59 SH |
367 | if (get_percent(&corrupt.correlation, *argv)) { |
368 | explain1("corrupt"); | |
369 | return -1; | |
370 | } | |
371 | } | |
309a4c90 | 372 | } else if (matches(*argv, "gap") == 0) { |
309a4c90 | 373 | NEXT_ARG(); |
2e21655e | 374 | if (get_u32(&opt.gap, *argv, 0)) { |
309a4c90 SH |
375 | explain1("gap"); |
376 | return -1; | |
377 | } | |
ffb79d06 | 378 | } else if (matches(*argv, "duplicate") == 0) { |
309a4c90 | 379 | NEXT_ARG(); |
2e21655e | 380 | if (get_percent(&opt.duplicate, *argv)) { |
ffb79d06 | 381 | explain1("duplicate"); |
309a4c90 SH |
382 | return -1; |
383 | } | |
b7be3d0c SH |
384 | if (NEXT_IS_NUMBER()) { |
385 | NEXT_ARG(); | |
2e21655e | 386 | if (get_percent(&cor.dup_corr, *argv)) { |
b7be3d0c SH |
387 | explain1("duplicate"); |
388 | return -1; | |
389 | } | |
390 | } | |
391 | } else if (matches(*argv, "distribution") == 0) { | |
309a4c90 | 392 | NEXT_ARG(); |
c1b81cb5 SH |
393 | dist_data = calloc(sizeof(dist_data[0]), MAX_DIST); |
394 | dist_size = get_distribution(*argv, dist_data, MAX_DIST); | |
395 | if (dist_size <= 0) { | |
396 | free(dist_data); | |
309a4c90 | 397 | return -1; |
c1b81cb5 | 398 | } |
6b8dc4de HPP |
399 | } else if (matches(*argv, "rate") == 0) { |
400 | ++present[TCA_NETEM_RATE]; | |
401 | NEXT_ARG(); | |
dad2f72b | 402 | if (get_rate64(&rate64, *argv)) { |
6b8dc4de HPP |
403 | explain1("rate"); |
404 | return -1; | |
405 | } | |
e72ca3fb | 406 | if (NEXT_IS_SIGNED_NUMBER()) { |
6b8dc4de HPP |
407 | NEXT_ARG(); |
408 | if (get_s32(&rate.packet_overhead, *argv, 0)) { | |
409 | explain1("rate"); | |
410 | return -1; | |
411 | } | |
412 | } | |
413 | if (NEXT_IS_NUMBER()) { | |
414 | NEXT_ARG(); | |
415 | if (get_u32(&rate.cell_size, *argv, 0)) { | |
416 | explain1("rate"); | |
417 | return -1; | |
418 | } | |
419 | } | |
e72ca3fb | 420 | if (NEXT_IS_SIGNED_NUMBER()) { |
6b8dc4de HPP |
421 | NEXT_ARG(); |
422 | if (get_s32(&rate.cell_overhead, *argv, 0)) { | |
423 | explain1("rate"); | |
424 | return -1; | |
425 | } | |
426 | } | |
309a4c90 SH |
427 | } else if (strcmp(*argv, "help") == 0) { |
428 | explain(); | |
429 | return -1; | |
430 | } else { | |
431 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
432 | explain(); | |
433 | return -1; | |
434 | } | |
309a4c90 SH |
435 | } |
436 | ||
1a1d22a7 | 437 | tail = NLMSG_TAIL(n); |
025dc69a | 438 | |
ea8fc104 SH |
439 | if (reorder.probability) { |
440 | if (opt.latency == 0) { | |
441 | fprintf(stderr, "reordering not possible without specifying some delay\n"); | |
14a1c164 VS |
442 | explain(); |
443 | return -1; | |
ea8fc104 SH |
444 | } |
445 | if (opt.gap == 0) | |
446 | opt.gap = 1; | |
447 | } else if (opt.gap > 0) { | |
448 | fprintf(stderr, "gap specified without reorder probability\n"); | |
449 | explain(); | |
450 | return -1; | |
451 | } | |
452 | ||
1070205d VS |
453 | if (present[TCA_NETEM_ECN]) { |
454 | if (opt.loss <= 0 && loss_type == NETEM_LOSS_UNSPEC) { | |
455 | fprintf(stderr, "ecn requested without loss model\n"); | |
456 | explain(); | |
457 | return -1; | |
458 | } | |
459 | } | |
460 | ||
a31a5d59 | 461 | if (dist_data && (opt.latency == 0 || opt.jitter == 0)) { |
ea8fc104 SH |
462 | fprintf(stderr, "distribution specified but no latency and jitter values\n"); |
463 | explain(); | |
464 | return -1; | |
465 | } | |
466 | ||
c1b81cb5 | 467 | if (addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)) < 0) |
a31a5d59 | 468 | return -1; |
2e21655e | 469 | |
40076f62 | 470 | if (present[TCA_NETEM_CORR] && |
c1b81cb5 | 471 | addattr_l(n, 1024, TCA_NETEM_CORR, &cor, sizeof(cor)) < 0) |
a31a5d59 | 472 | return -1; |
a31a5d59 | 473 | |
3c7950af | 474 | if (present[TCA_NETEM_REORDER] && |
c1b81cb5 | 475 | addattr_l(n, 1024, TCA_NETEM_REORDER, &reorder, sizeof(reorder)) < 0) |
e9bc3c40 | 476 | return -1; |
a31a5d59 | 477 | |
1070205d VS |
478 | if (present[TCA_NETEM_ECN] && |
479 | addattr_l(n, 1024, TCA_NETEM_ECN, &present[TCA_NETEM_ECN], | |
480 | sizeof(present[TCA_NETEM_ECN])) < 0) | |
481 | return -1; | |
482 | ||
40076f62 | 483 | if (present[TCA_NETEM_CORRUPT] && |
c1b81cb5 | 484 | addattr_l(n, 1024, TCA_NETEM_CORRUPT, &corrupt, sizeof(corrupt)) < 0) |
40076f62 | 485 | return -1; |
a31a5d59 | 486 | |
3c7950af SH |
487 | if (loss_type != NETEM_LOSS_UNSPEC) { |
488 | struct rtattr *start; | |
489 | ||
490 | start = addattr_nest(n, 1024, TCA_NETEM_LOSS | NLA_F_NESTED); | |
491 | if (loss_type == NETEM_LOSS_GI) { | |
492 | if (addattr_l(n, 1024, NETEM_LOSS_GI, | |
493 | &gimodel, sizeof(gimodel)) < 0) | |
494 | return -1; | |
495 | } else if (loss_type == NETEM_LOSS_GE) { | |
496 | if (addattr_l(n, 1024, NETEM_LOSS_GE, | |
497 | &gemodel, sizeof(gemodel)) < 0) | |
498 | return -1; | |
499 | } else { | |
500 | fprintf(stderr, "loss in the weeds!\n"); | |
501 | return -1; | |
502 | } | |
3d0b7439 | 503 | |
3c7950af SH |
504 | addattr_nest_end(n, start); |
505 | } | |
506 | ||
dad2f72b YY |
507 | if (present[TCA_NETEM_RATE]) { |
508 | if (rate64 >= (1ULL << 32)) { | |
509 | if (addattr_l(n, 1024, | |
510 | TCA_NETEM_RATE64, &rate64, sizeof(rate64)) < 0) | |
511 | return -1; | |
512 | rate.rate = ~0U; | |
513 | } else { | |
514 | rate.rate = rate64; | |
515 | } | |
516 | if (addattr_l(n, 1024, TCA_NETEM_RATE, &rate, sizeof(rate)) < 0) | |
517 | return -1; | |
518 | } | |
6b8dc4de | 519 | |
a31a5d59 | 520 | if (dist_data) { |
c1b81cb5 SH |
521 | if (addattr_l(n, MAX_DIST * sizeof(dist_data[0]), |
522 | TCA_NETEM_DELAY_DIST, | |
523 | dist_data, dist_size * sizeof(dist_data[0])) < 0) | |
a31a5d59 | 524 | return -1; |
c1b81cb5 | 525 | free(dist_data); |
2e21655e | 526 | } |
1a1d22a7 | 527 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
b7be3d0c | 528 | return 0; |
309a4c90 SH |
529 | } |
530 | ||
531 | static int netem_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
532 | { | |
2e21655e | 533 | const struct tc_netem_corr *cor = NULL; |
ea8fc104 | 534 | const struct tc_netem_reorder *reorder = NULL; |
a31a5d59 | 535 | const struct tc_netem_corrupt *corrupt = NULL; |
3c7950af SH |
536 | const struct tc_netem_gimodel *gimodel = NULL; |
537 | const struct tc_netem_gemodel *gemodel = NULL; | |
1070205d | 538 | int *ecn = NULL; |
2e21655e | 539 | struct tc_netem_qopt qopt; |
6b8dc4de | 540 | const struct tc_netem_rate *rate = NULL; |
2e21655e | 541 | int len = RTA_PAYLOAD(opt) - sizeof(qopt); |
dad2f72b | 542 | __u64 rate64 = 0; |
32a121cb | 543 | |
309a4c90 | 544 | SPRINT_BUF(b1); |
309a4c90 SH |
545 | |
546 | if (opt == NULL) | |
547 | return 0; | |
548 | ||
2e21655e SH |
549 | if (len < 0) { |
550 | fprintf(stderr, "options size error\n"); | |
309a4c90 | 551 | return -1; |
b7be3d0c | 552 | } |
2e21655e SH |
553 | memcpy(&qopt, RTA_DATA(opt), sizeof(qopt)); |
554 | ||
555 | if (len > 0) { | |
1d2d1cb5 | 556 | struct rtattr *tb[TCA_NETEM_MAX+1]; |
32a121cb | 557 | |
2e21655e SH |
558 | parse_rtattr(tb, TCA_NETEM_MAX, RTA_DATA(opt) + sizeof(qopt), |
559 | len); | |
ae665a52 | 560 | |
2e21655e SH |
561 | if (tb[TCA_NETEM_CORR]) { |
562 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORR]) < sizeof(*cor)) | |
563 | return -1; | |
564 | cor = RTA_DATA(tb[TCA_NETEM_CORR]); | |
565 | } | |
ea8fc104 SH |
566 | if (tb[TCA_NETEM_REORDER]) { |
567 | if (RTA_PAYLOAD(tb[TCA_NETEM_REORDER]) < sizeof(*reorder)) | |
568 | return -1; | |
569 | reorder = RTA_DATA(tb[TCA_NETEM_REORDER]); | |
570 | } | |
a31a5d59 SH |
571 | if (tb[TCA_NETEM_CORRUPT]) { |
572 | if (RTA_PAYLOAD(tb[TCA_NETEM_CORRUPT]) < sizeof(*corrupt)) | |
573 | return -1; | |
e9bc3c40 | 574 | corrupt = RTA_DATA(tb[TCA_NETEM_CORRUPT]); |
a31a5d59 | 575 | } |
3c7950af SH |
576 | if (tb[TCA_NETEM_LOSS]) { |
577 | struct rtattr *lb[NETEM_LOSS_MAX + 1]; | |
578 | ||
579 | parse_rtattr_nested(lb, NETEM_LOSS_MAX, tb[TCA_NETEM_LOSS]); | |
580 | if (lb[NETEM_LOSS_GI]) | |
8f9672af | 581 | gimodel = RTA_DATA(lb[NETEM_LOSS_GI]); |
3c7950af SH |
582 | if (lb[NETEM_LOSS_GE]) |
583 | gemodel = RTA_DATA(lb[NETEM_LOSS_GE]); | |
3d0b7439 | 584 | } |
6b8dc4de HPP |
585 | if (tb[TCA_NETEM_RATE]) { |
586 | if (RTA_PAYLOAD(tb[TCA_NETEM_RATE]) < sizeof(*rate)) | |
587 | return -1; | |
588 | rate = RTA_DATA(tb[TCA_NETEM_RATE]); | |
589 | } | |
1070205d VS |
590 | if (tb[TCA_NETEM_ECN]) { |
591 | if (RTA_PAYLOAD(tb[TCA_NETEM_ECN]) < sizeof(*ecn)) | |
592 | return -1; | |
593 | ecn = RTA_DATA(tb[TCA_NETEM_ECN]); | |
594 | } | |
dad2f72b YY |
595 | if (tb[TCA_NETEM_RATE64]) { |
596 | if (RTA_PAYLOAD(tb[TCA_NETEM_RATE64]) < sizeof(rate64)) | |
597 | return -1; | |
598 | rate64 = rta_getattr_u64(tb[TCA_NETEM_RATE64]); | |
599 | } | |
2e21655e | 600 | } |
309a4c90 | 601 | |
2e21655e | 602 | fprintf(f, "limit %d", qopt.limit); |
31fa60e0 | 603 | |
2e21655e SH |
604 | if (qopt.latency) { |
605 | fprintf(f, " delay %s", sprint_ticks(qopt.latency, b1)); | |
b7be3d0c | 606 | |
2e21655e SH |
607 | if (qopt.jitter) { |
608 | fprintf(f, " %s", sprint_ticks(qopt.jitter, b1)); | |
609 | if (cor && cor->delay_corr) | |
610 | fprintf(f, " %s", sprint_percent(cor->delay_corr, b1)); | |
b7be3d0c SH |
611 | } |
612 | } | |
613 | ||
2e21655e SH |
614 | if (qopt.loss) { |
615 | fprintf(f, " loss %s", sprint_percent(qopt.loss, b1)); | |
616 | if (cor && cor->loss_corr) | |
617 | fprintf(f, " %s", sprint_percent(cor->loss_corr, b1)); | |
b7be3d0c SH |
618 | } |
619 | ||
3c7950af SH |
620 | if (gimodel) { |
621 | fprintf(f, " loss state p13 %s", sprint_percent(gimodel->p13, b1)); | |
622 | fprintf(f, " p31 %s", sprint_percent(gimodel->p31, b1)); | |
623 | fprintf(f, " p32 %s", sprint_percent(gimodel->p32, b1)); | |
624 | fprintf(f, " p23 %s", sprint_percent(gimodel->p23, b1)); | |
625 | fprintf(f, " p14 %s", sprint_percent(gimodel->p14, b1)); | |
626 | } | |
627 | ||
628 | if (gemodel) { | |
3757185b | 629 | fprintf(f, " loss gemodel p %s", |
3c7950af SH |
630 | sprint_percent(gemodel->p, b1)); |
631 | fprintf(f, " r %s", sprint_percent(gemodel->r, b1)); | |
3757185b JV |
632 | fprintf(f, " 1-h %s", sprint_percent(max_percent_value - |
633 | gemodel->h, b1)); | |
3c7950af SH |
634 | fprintf(f, " 1-k %s", sprint_percent(gemodel->k1, b1)); |
635 | } | |
636 | ||
2e21655e | 637 | if (qopt.duplicate) { |
b7be3d0c | 638 | fprintf(f, " duplicate %s", |
2e21655e SH |
639 | sprint_percent(qopt.duplicate, b1)); |
640 | if (cor && cor->dup_corr) | |
641 | fprintf(f, " %s", sprint_percent(cor->dup_corr, b1)); | |
b7be3d0c | 642 | } |
ae665a52 | 643 | |
ea8fc104 | 644 | if (reorder && reorder->probability) { |
ae665a52 | 645 | fprintf(f, " reorder %s", |
ea8fc104 SH |
646 | sprint_percent(reorder->probability, b1)); |
647 | if (reorder->correlation) | |
ae665a52 | 648 | fprintf(f, " %s", |
ea8fc104 SH |
649 | sprint_percent(reorder->correlation, b1)); |
650 | } | |
b7be3d0c | 651 | |
a31a5d59 | 652 | if (corrupt && corrupt->probability) { |
ae665a52 | 653 | fprintf(f, " corrupt %s", |
a31a5d59 SH |
654 | sprint_percent(corrupt->probability, b1)); |
655 | if (corrupt->correlation) | |
ae665a52 | 656 | fprintf(f, " %s", |
a31a5d59 SH |
657 | sprint_percent(corrupt->correlation, b1)); |
658 | } | |
659 | ||
6b8dc4de | 660 | if (rate && rate->rate) { |
dad2f72b YY |
661 | if (rate64) |
662 | fprintf(f, " rate %s", sprint_rate(rate64, b1)); | |
663 | else | |
664 | fprintf(f, " rate %s", sprint_rate(rate->rate, b1)); | |
6b8dc4de HPP |
665 | if (rate->packet_overhead) |
666 | fprintf(f, " packetoverhead %d", rate->packet_overhead); | |
667 | if (rate->cell_size) | |
668 | fprintf(f, " cellsize %u", rate->cell_size); | |
669 | if (rate->cell_overhead) | |
670 | fprintf(f, " celloverhead %d", rate->cell_overhead); | |
671 | } | |
672 | ||
1070205d VS |
673 | if (ecn) |
674 | fprintf(f, " ecn "); | |
675 | ||
2e21655e SH |
676 | if (qopt.gap) |
677 | fprintf(f, " gap %lu", (unsigned long)qopt.gap); | |
309a4c90 | 678 | |
1070205d | 679 | |
309a4c90 SH |
680 | return 0; |
681 | } | |
682 | ||
95812b56 | 683 | struct qdisc_util netem_qdisc_util = { |
32a121cb | 684 | .id = "netem", |
31fa60e0 SH |
685 | .parse_qopt = netem_parse_opt, |
686 | .print_qopt = netem_print_opt, | |
309a4c90 | 687 | }; |