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