]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_util.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / tc_util.c
CommitLineData
aba5acdf
SH
1/*
2 * tc_util.c Misc TC utility functions.
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: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
aba5acdf
SH
16#include <fcntl.h>
17#include <sys/socket.h>
dd9cc0ee 18#include <sys/param.h>
aba5acdf
SH
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <math.h>
8b90a990 23#include <errno.h>
aba5acdf
SH
24
25#include "utils.h"
4612d04d 26#include "names.h"
aba5acdf 27#include "tc_util.h"
4612d04d 28#include "tc_common.h"
aba5acdf 29
5e3bb534 30#ifndef LIBDIR
5c434a9e 31#define LIBDIR "/usr/lib"
b514b358
RA
32#endif
33
32a121cb 34static struct db_names *cls_names;
4612d04d 35
8b90a990 36#define NAMES_DB "/etc/iproute2/tc_cls"
4612d04d
VK
37
38int cls_names_init(char *path)
39{
8b90a990
VK
40 int ret;
41
42 cls_names = db_names_alloc();
43 if (!cls_names)
44 return -1;
45
46 ret = db_names_load(cls_names, path ?: NAMES_DB);
47 if (ret == -ENOENT && path) {
48 fprintf(stderr, "Can't open class names file: %s\n", path);
4612d04d
VK
49 return -1;
50 }
8b90a990
VK
51 if (ret) {
52 db_names_free(cls_names);
53 cls_names = NULL;
54 }
4612d04d
VK
55
56 return 0;
57}
58
59void cls_names_uninit(void)
60{
61 db_names_free(cls_names);
62}
63
aa27f88c
SH
64const char *get_tc_lib(void)
65{
66 const char *lib_dir;
67
68 lib_dir = getenv("TC_LIB_DIR");
69 if (!lib_dir)
5e3bb534 70 lib_dir = LIBDIR "/tc/";
aa27f88c
SH
71
72 return lib_dir;
73}
74
dbd90dc2 75int get_qdisc_handle(__u32 *h, const char *str)
aba5acdf
SH
76{
77 __u32 maj;
78 char *p;
79
80 maj = TC_H_UNSPEC;
81 if (strcmp(str, "none") == 0)
82 goto ok;
83 maj = strtoul(str, &p, 16);
087dec7f 84 if (p == str || maj >= (1 << 16))
aba5acdf
SH
85 return -1;
86 maj <<= 16;
32a121cb 87 if (*p != ':' && *p != 0)
aba5acdf
SH
88 return -1;
89ok:
90 *h = maj;
91 return 0;
92}
93
dbd90dc2 94int get_tc_classid(__u32 *h, const char *str)
aba5acdf
SH
95{
96 __u32 maj, min;
97 char *p;
98
99 maj = TC_H_ROOT;
100 if (strcmp(str, "root") == 0)
101 goto ok;
102 maj = TC_H_UNSPEC;
103 if (strcmp(str, "none") == 0)
104 goto ok;
105 maj = strtoul(str, &p, 16);
106 if (p == str) {
107 maj = 0;
108 if (*p != ':')
109 return -1;
110 }
111 if (*p == ':') {
a8b303cc
SH
112 if (maj >= (1<<16))
113 return -1;
aba5acdf
SH
114 maj <<= 16;
115 str = p+1;
116 min = strtoul(str, &p, 16);
117 if (*p != 0)
118 return -1;
a8b303cc
SH
119 if (min >= (1<<16))
120 return -1;
aba5acdf
SH
121 maj |= min;
122 } else if (*p != 0)
123 return -1;
124
125ok:
126 *h = maj;
127 return 0;
128}
129
46679bbb 130int print_tc_classid(char *buf, int blen, __u32 h)
aba5acdf 131{
46679bbb
VK
132 SPRINT_BUF(handle) = {};
133 int hlen = SPRINT_BSIZE - 1;
4612d04d 134
aba5acdf 135 if (h == TC_H_ROOT)
4612d04d 136 sprintf(handle, "root");
aba5acdf 137 else if (h == TC_H_UNSPEC)
46679bbb 138 snprintf(handle, hlen, "none");
aba5acdf 139 else if (TC_H_MAJ(h) == 0)
46679bbb 140 snprintf(handle, hlen, ":%x", TC_H_MIN(h));
aba5acdf 141 else if (TC_H_MIN(h) == 0)
46679bbb 142 snprintf(handle, hlen, "%x:", TC_H_MAJ(h) >> 16);
aba5acdf 143 else
46679bbb 144 snprintf(handle, hlen, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h));
4612d04d
VK
145
146 if (use_names) {
147 char clname[IDNAME_MAX] = {};
148
149 if (id_to_name(cls_names, h, clname))
46679bbb 150 snprintf(buf, blen, "%s#%s", clname, handle);
4612d04d 151 else
46679bbb 152 snprintf(buf, blen, "%s", handle);
4612d04d 153 } else {
46679bbb 154 snprintf(buf, blen, "%s", handle);
4612d04d
VK
155 }
156
aba5acdf
SH
157 return 0;
158}
159
4612d04d 160char *sprint_tc_classid(__u32 h, char *buf)
aba5acdf
SH
161{
162 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
163 strcpy(buf, "???");
164 return buf;
165}
166
26ab0b10
SH
167/* See http://physics.nist.gov/cuu/Units/binary.html */
168static const struct rate_suffix {
169 const char *name;
170 double scale;
171} suffixes[] = {
172 { "bit", 1. },
173 { "Kibit", 1024. },
174 { "kbit", 1000. },
175 { "mibit", 1024.*1024. },
176 { "mbit", 1000000. },
177 { "gibit", 1024.*1024.*1024. },
178 { "gbit", 1000000000. },
179 { "tibit", 1024.*1024.*1024.*1024. },
180 { "tbit", 1000000000000. },
181 { "Bps", 8. },
182 { "KiBps", 8.*1024. },
183 { "KBps", 8000. },
184 { "MiBps", 8.*1024*1024. },
185 { "MBps", 8000000. },
186 { "GiBps", 8.*1024.*1024.*1024. },
187 { "GBps", 8000000000. },
188 { "TiBps", 8.*1024.*1024.*1024.*1024. },
189 { "TBps", 8000000000000. },
190 { NULL }
191};
192
927e3cfb
ND
193int parse_percent_rate(char *rate, const char *str, const char *dev)
194{
195 long dev_mbit;
196 int ret;
197 double perc, rate_mbit;
198 char *str_perc;
199
200 if (!dev[0]) {
201 fprintf(stderr, "No device specified; specify device to rate limit by percentage\n");
202 return -1;
203 }
204
205 if (read_prop(dev, "speed", &dev_mbit))
206 return -1;
207
208 ret = sscanf(str, "%m[0-9.%]", &str_perc);
209 if (ret != 1)
210 goto malf;
211
212 if (parse_percent(&perc, str_perc))
213 goto malf;
214
215 free(str_perc);
216
217 if (perc > 1.0 || perc < 0.0) {
218 fprintf(stderr, "Invalid rate specified; should be between [0,100]%% but is %s\n", str);
219 return -1;
220 }
221
222 rate_mbit = perc * dev_mbit;
223
224 ret = snprintf(rate, 20, "%lf", rate_mbit);
225 if (ret <= 0 || ret >= 20) {
226 fprintf(stderr, "Unable to parse calculated rate\n");
227 return -1;
228 }
229
230 return 0;
231
232malf:
233 fprintf(stderr, "Specified rate value could not be read or is malformed\n");
234 return -1;
235}
236
237int get_percent_rate(unsigned int *rate, const char *str, const char *dev)
238{
239 char r_str[20];
240
241 if (parse_percent_rate(r_str, str, dev))
242 return -1;
243
244 return get_rate(rate, r_str);
245}
246
247int get_percent_rate64(__u64 *rate, const char *str, const char *dev)
248{
249 char r_str[20];
250
251 if (parse_percent_rate(r_str, str, dev))
252 return -1;
253
254 return get_rate64(rate, r_str);
255}
26ab0b10 256
32a121cb 257int get_rate(unsigned int *rate, const char *str)
aba5acdf
SH
258{
259 char *p;
260 double bps = strtod(str, &p);
26ab0b10 261 const struct rate_suffix *s;
aba5acdf
SH
262
263 if (p == str)
264 return -1;
265
26ab0b10
SH
266 for (s = suffixes; s->name; ++s) {
267 if (strcasecmp(s->name, p) == 0) {
a303853e
ED
268 bps *= s->scale;
269 p += strlen(p);
270 break;
26ab0b10
SH
271 }
272 }
273
a303853e
ED
274 if (*p)
275 return -1; /* unknown suffix */
276
277 bps /= 8; /* -> bytes per second */
278 *rate = bps;
279 /* detect if an overflow happened */
280 if (*rate != floor(bps))
281 return -1;
282 return 0;
aba5acdf
SH
283}
284
8334bb32
ED
285int get_rate64(__u64 *rate, const char *str)
286{
287 char *p;
288 double bps = strtod(str, &p);
289 const struct rate_suffix *s;
290
291 if (p == str)
292 return -1;
293
294 for (s = suffixes; s->name; ++s) {
295 if (strcasecmp(s->name, p) == 0) {
296 bps *= s->scale;
297 p += strlen(p);
298 break;
299 }
300 }
301
302 if (*p)
303 return -1; /* unknown suffix */
304
305 bps /= 8; /* -> bytes per second */
306 *rate = bps;
307 return 0;
308}
309
8f7574ed 310void print_rate(char *buf, int len, __u64 rate)
aba5acdf 311{
d40b38b4 312 extern int use_iec;
8cecdc28
ED
313 unsigned long kilo = use_iec ? 1024 : 1000;
314 const char *str = use_iec ? "i" : "";
8cecdc28 315 static char *units[5] = {"", "K", "M", "G", "T"};
ad1fe0d8 316 int i;
d40b38b4 317
8cecdc28
ED
318 rate <<= 3; /* bytes/sec -> bits/sec */
319
ad1fe0d8 320 for (i = 0; i < ARRAY_SIZE(units) - 1; i++) {
8cecdc28
ED
321 if (rate < kilo)
322 break;
323 if (((rate % kilo) != 0) && rate < 1000*kilo)
324 break;
325 rate /= kilo;
d40b38b4 326 }
ad1fe0d8 327
8cecdc28 328 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
aba5acdf
SH
329}
330
32a121cb 331char *sprint_rate(__u64 rate, char *buf)
aba5acdf 332{
d40b38b4 333 print_rate(buf, SPRINT_BSIZE-1, rate);
aba5acdf
SH
334 return buf;
335}
336
32a121cb 337int get_time(unsigned int *time, const char *str)
aba5acdf
SH
338{
339 double t;
340 char *p;
341
342 t = strtod(str, &p);
343 if (p == str)
344 return -1;
345
346 if (*p) {
32a121cb
SH
347 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 ||
348 strcasecmp(p, "secs") == 0)
f0bda7e5 349 t *= TIME_UNITS_PER_SEC;
32a121cb 350 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec") == 0 ||
aba5acdf 351 strcasecmp(p, "msecs") == 0)
f0bda7e5 352 t *= TIME_UNITS_PER_SEC/1000;
32a121cb 353 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec") == 0 ||
aba5acdf 354 strcasecmp(p, "usecs") == 0)
f0bda7e5 355 t *= TIME_UNITS_PER_SEC/1000000;
aba5acdf
SH
356 else
357 return -1;
358 }
359
8f34caaf 360 *time = t;
aba5acdf
SH
361 return 0;
362}
363
364
8f34caaf 365void print_time(char *buf, int len, __u32 time)
aba5acdf 366{
8f34caaf 367 double tmp = time;
aba5acdf 368
f0bda7e5
PM
369 if (tmp >= TIME_UNITS_PER_SEC)
370 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
371 else if (tmp >= TIME_UNITS_PER_SEC/1000)
372 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
aba5acdf 373 else
89151447 374 snprintf(buf, len, "%uus", time);
aba5acdf
SH
375}
376
32a121cb 377char *sprint_time(__u32 time, char *buf)
aba5acdf 378{
8f34caaf 379 print_time(buf, SPRINT_BSIZE-1, time);
aba5acdf
SH
380 return buf;
381}
382
32a121cb 383char *sprint_ticks(__u32 ticks, char *buf)
bd29e35d
PM
384{
385 return sprint_time(tc_core_tick2time(ticks), buf);
386}
387
32a121cb 388int get_size(unsigned int *size, const char *str)
aba5acdf
SH
389{
390 double sz;
391 char *p;
392
393 sz = strtod(str, &p);
394 if (p == str)
395 return -1;
396
397 if (*p) {
32a121cb 398 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0)
aba5acdf 399 sz *= 1024;
32a121cb 400 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0)
dbd90dc2
SH
401 sz *= 1024*1024*1024;
402 else if (strcasecmp(p, "gbit") == 0)
403 sz *= 1024*1024*1024/8;
32a121cb 404 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0)
aba5acdf
SH
405 sz *= 1024*1024;
406 else if (strcasecmp(p, "mbit") == 0)
407 sz *= 1024*1024/8;
408 else if (strcasecmp(p, "kbit") == 0)
409 sz *= 1024/8;
410 else if (strcasecmp(p, "b") != 0)
411 return -1;
412 }
413
414 *size = sz;
415 return 0;
416}
417
32a121cb 418int get_size_and_cell(unsigned int *size, int *cell_log, char *str)
aba5acdf 419{
32a121cb 420 char *slash = strchr(str, '/');
aba5acdf
SH
421
422 if (slash)
423 *slash = 0;
424
425 if (get_size(size, str))
426 return -1;
427
428 if (slash) {
429 int cell;
430 int i;
431
432 if (get_integer(&cell, slash+1, 0))
433 return -1;
434 *slash = '/';
435
32a121cb 436 for (i = 0; i < 32; i++) {
aba5acdf
SH
437 if ((1<<i) == cell) {
438 *cell_log = i;
439 return 0;
440 }
441 }
442 return -1;
443 }
444 return 0;
445}
446
d40b38b4 447void print_size(char *buf, int len, __u32 sz)
aba5acdf
SH
448{
449 double tmp = sz;
450
451 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
452 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
453 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
454 snprintf(buf, len, "%gKb", rint(tmp/1024));
455 else
456 snprintf(buf, len, "%ub", sz);
aba5acdf
SH
457}
458
32a121cb 459char *sprint_size(__u32 size, char *buf)
aba5acdf 460{
d40b38b4 461 print_size(buf, SPRINT_BSIZE-1, size);
aba5acdf
SH
462 return buf;
463}
464
d40b38b4 465void print_qdisc_handle(char *buf, int len, __u32 h)
aba5acdf
SH
466{
467 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
aba5acdf
SH
468}
469
32a121cb 470char *sprint_qdisc_handle(__u32 h, char *buf)
aba5acdf 471{
d40b38b4 472 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
aba5acdf
SH
473 return buf;
474}
475
e67aba55 476static const char *action_n2a(int action)
2373fde9 477{
70932006
PS
478 static char buf[64];
479
d19f72f7
JP
480 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
481 return "goto";
35f2a763
JHS
482 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
483 return "jump";
2373fde9 484 switch (action) {
70932006 485 case TC_ACT_UNSPEC:
2373fde9 486 return "continue";
2373fde9
SH
487 case TC_ACT_OK:
488 return "pass";
2373fde9
SH
489 case TC_ACT_SHOT:
490 return "drop";
2373fde9
SH
491 case TC_ACT_RECLASSIFY:
492 return "reclassify";
493 case TC_ACT_PIPE:
494 return "pipe";
495 case TC_ACT_STOLEN:
496 return "stolen";
d5ebd6fd
JP
497 case TC_ACT_TRAP:
498 return "trap";
2373fde9 499 default:
70932006 500 snprintf(buf, 64, "%d", action);
2373fde9
SH
501 return buf;
502 }
503}
aba5acdf 504
53aadc52
PS
505/* Convert action branch name into numeric format.
506 *
507 * Parameters:
508 * @arg - string to parse
509 * @result - pointer to output variable
510 * @allow_num - whether @arg may be in numeric format already
511 *
512 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
513 */
e67aba55 514static int action_a2n(char *arg, int *result, bool allow_num)
2373fde9 515{
53aadc52
PS
516 int n;
517 char dummy;
518 struct {
519 const char *a;
520 int n;
521 } a2n[] = {
522 {"continue", TC_ACT_UNSPEC},
523 {"drop", TC_ACT_SHOT},
524 {"shot", TC_ACT_SHOT},
525 {"pass", TC_ACT_OK},
526 {"ok", TC_ACT_OK},
527 {"reclassify", TC_ACT_RECLASSIFY},
528 {"pipe", TC_ACT_PIPE},
d19f72f7 529 {"goto", TC_ACT_GOTO_CHAIN},
35f2a763 530 {"jump", TC_ACT_JUMP},
d5ebd6fd 531 {"trap", TC_ACT_TRAP},
53aadc52
PS
532 { NULL },
533 }, *iter;
534
535 for (iter = a2n; iter->a; iter++) {
536 if (matches(arg, iter->a) != 0)
537 continue;
538 *result = iter->n;
539 return 0;
2373fde9 540 }
53aadc52
PS
541 if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1)
542 return -1;
543
544 *result = n;
2373fde9
SH
545 return 0;
546}
547
c794b7b1
JP
548static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p,
549 bool allow_num, bool ignore_a2n_miss)
e67aba55
JP
550{
551 int argc = *argc_p;
552 char **argv = *argv_p;
553 int result;
554
555 if (!argc)
556 return -1;
557 if (action_a2n(*argv, &result, allow_num) == -1) {
c794b7b1
JP
558 if (!ignore_a2n_miss)
559 fprintf(stderr, "Bad action type %s\n", *argv);
e67aba55
JP
560 return -1;
561 }
d19f72f7
JP
562 if (result == TC_ACT_GOTO_CHAIN) {
563 __u32 chain_index;
564
565 NEXT_ARG();
566 if (matches(*argv, "chain") != 0) {
567 fprintf(stderr, "\"chain index\" expected\n");
568 return -1;
569 }
570 NEXT_ARG();
571 if (get_u32(&chain_index, *argv, 10) ||
572 chain_index > TC_ACT_EXT_VAL_MASK) {
573 fprintf(stderr, "Illegal \"chain index\"\n");
574 return -1;
575 }
576 result |= chain_index;
577 }
35f2a763
JHS
578 if (result == TC_ACT_JUMP) {
579 __u32 jump_cnt = 0;
580
581 NEXT_ARG();
582 if (get_u32(&jump_cnt, *argv, 10) ||
583 jump_cnt > TC_ACT_EXT_VAL_MASK) {
584 fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv);
585 return -1;
586 }
587 result |= jump_cnt;
588 }
e67aba55
JP
589 NEXT_ARG_FWD();
590 *argc_p = argc;
591 *argv_p = argv;
592 *result_p = result;
593 return 0;
594}
595
c794b7b1
JP
596/* Parse action control including possible options.
597 *
598 * Parameters:
599 * @argc_p - pointer to argc to parse
600 * @argv_p - pointer to argv to parse
601 * @result_p - pointer to output variable
602 * @allow_num - whether action may be in numeric format already
603 *
604 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
605 */
606int parse_action_control(int *argc_p, char ***argv_p,
607 int *result_p, bool allow_num)
608{
609 return __parse_action_control(argc_p, argv_p, result_p,
610 allow_num, false);
611}
612
e67aba55
JP
613/* Parse action control including possible options.
614 *
615 * Parameters:
616 * @argc_p - pointer to argc to parse
617 * @argv_p - pointer to argv to parse
618 * @result_p - pointer to output variable
619 * @allow_num - whether action may be in numeric format already
620 * @default_result - set as a result in case of parsing error
621 *
622 * In case there is an error during parsing, the default result is used.
623 */
624void parse_action_control_dflt(int *argc_p, char ***argv_p,
625 int *result_p, bool allow_num,
626 int default_result)
627{
c794b7b1 628 if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true))
e67aba55
JP
629 *result_p = default_result;
630}
631
632static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p,
633 int *result1_p, int *result2_p,
634 bool allow_num)
635{
636 int argc = *argc_p;
637 char **argv = *argv_p;
66942e52 638 int result1 = -1, result2;
e67aba55
JP
639 int *result_p = &result1;
640 int ok = 0;
641 int ret;
642
643 while (argc > 0) {
644 switch (ok) {
645 case 1:
646 if (strcmp(*argv, "/") != 0)
647 goto out;
648 result_p = &result2;
649 NEXT_ARG();
650 /* fall-through */
651 case 0: /* fall-through */
652 case 2:
653 ret = parse_action_control(&argc, &argv,
654 result_p, allow_num);
655 if (ret)
656 return ret;
657 ok++;
658 break;
659 default:
660 goto out;
661 }
662 }
663out:
664 *result1_p = result1;
665 if (ok == 2)
666 *result2_p = result2;
667 *argc_p = argc;
668 *argv_p = argv;
669 return 0;
670}
671
672/* Parse action control with slash including possible options.
673 *
674 * Parameters:
675 * @argc_p - pointer to argc to parse
676 * @argv_p - pointer to argv to parse
677 * @result1_p - pointer to the first (before slash) output variable
678 * @result2_p - pointer to the second (after slash) output variable
679 * @allow_num - whether action may be in numeric format already
680 *
681 * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
682 */
683int parse_action_control_slash(int *argc_p, char ***argv_p,
684 int *result1_p, int *result2_p, bool allow_num)
685{
686 char **argv = *argv_p;
687 int result1, result2;
688 char *p = strchr(*argv, '/');
689
690 if (!p)
691 return parse_action_control_slash_spaces(argc_p, argv_p,
692 result1_p, result2_p,
693 allow_num);
694 *p = 0;
695 if (action_a2n(*argv, &result1, allow_num)) {
b7c61286 696 *p = '/';
e67aba55
JP
697 return -1;
698 }
699
700 *p = '/';
701 if (action_a2n(p + 1, &result2, allow_num))
702 return -1;
703
704 *result1_p = result1;
705 *result2_p = result2;
706 return 0;
707}
708
709void print_action_control(FILE *f, const char *prefix,
710 int action, const char *suffix)
711{
d19f72f7
JP
712 fprintf(f, "%s%s", prefix, action_n2a(action));
713 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
714 fprintf(f, " chain %u", action & TC_ACT_EXT_VAL_MASK);
35f2a763
JHS
715 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
716 fprintf(f, " %u", action & TC_ACT_EXT_VAL_MASK);
d19f72f7 717 fprintf(f, "%s", suffix);
e67aba55
JP
718}
719
32a121cb 720int get_linklayer(unsigned int *val, const char *arg)
292f29b4
JDB
721{
722 int res;
723
724 if (matches(arg, "ethernet") == 0)
725 res = LINKLAYER_ETHERNET;
726 else if (matches(arg, "atm") == 0)
727 res = LINKLAYER_ATM;
728 else if (matches(arg, "adsl") == 0)
729 res = LINKLAYER_ATM;
730 else
731 return -1; /* Indicate error */
732
733 *val = res;
734 return 0;
735}
736
32a121cb 737void print_linklayer(char *buf, int len, unsigned int linklayer)
839c8456
JK
738{
739 switch (linklayer) {
740 case LINKLAYER_UNSPEC:
741 snprintf(buf, len, "%s", "unspec");
742 return;
743 case LINKLAYER_ETHERNET:
744 snprintf(buf, len, "%s", "ethernet");
745 return;
746 case LINKLAYER_ATM:
747 snprintf(buf, len, "%s", "atm");
748 return;
749 default:
750 snprintf(buf, len, "%s", "unknown");
751 return;
752 }
753}
754
32a121cb 755char *sprint_linklayer(unsigned int linklayer, char *buf)
839c8456
JK
756{
757 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
758 return buf;
759}
760
32a121cb 761void print_tm(FILE *f, const struct tcf_t *tm)
2373fde9 762{
5e8bc631 763 int hz = get_user_hz();
32a121cb 764
2373fde9 765 if (tm->install != 0)
32a121cb 766 fprintf(f, " installed %u sec", (unsigned int)(tm->install/hz));
2373fde9 767 if (tm->lastuse != 0)
32a121cb 768 fprintf(f, " used %u sec", (unsigned int)(tm->lastuse/hz));
2373fde9 769 if (tm->expires != 0)
32a121cb 770 fprintf(f, " expires %u sec", (unsigned int)(tm->expires/hz));
2373fde9 771}
e5879dc6 772
773void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
774{
775 SPRINT_BUF(b1);
7d69fd97 776 struct rtattr *tbs[TCA_STATS_MAX + 1];
e5879dc6 777
7d69fd97 778 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
e5879dc6 779
780 if (tbs[TCA_STATS_BASIC]) {
781 struct gnet_stats_basic bs = {0};
32a121cb 782
e5879dc6 783 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
784 fprintf(fp, "%sSent %llu bytes %u pkt",
b906243b 785 prefix, (unsigned long long) bs.bytes, bs.packets);
e5879dc6 786 }
787
788 if (tbs[TCA_STATS_QUEUE]) {
789 struct gnet_stats_queue q = {0};
32a121cb 790
e5879dc6 791 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
792 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
793 q.drops, q.overlimits, q.requeues);
794 }
ae665a52 795
8f7574ed
ED
796 if (tbs[TCA_STATS_RATE_EST64]) {
797 struct gnet_stats_rate_est64 re = {0};
798
799 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
800 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
801 sizeof(re)));
802 fprintf(fp, "\n%srate %s %llupps ",
803 prefix, sprint_rate(re.bps, b1), re.pps);
804 } else if (tbs[TCA_STATS_RATE_EST]) {
e5879dc6 805 struct gnet_stats_rate_est re = {0};
8f7574ed
ED
806
807 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
808 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
e5879dc6 809 fprintf(fp, "\n%srate %s %upps ",
810 prefix, sprint_rate(re.bps, b1), re.pps);
811 }
812
813 if (tbs[TCA_STATS_QUEUE]) {
814 struct gnet_stats_queue q = {0};
32a121cb 815
e5879dc6 816 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
817 if (!tbs[TCA_STATS_RATE_EST])
818 fprintf(fp, "\n%s", prefix);
819 fprintf(fp, "backlog %s %up requeues %u ",
820 sprint_size(q.backlog, b1), q.qlen, q.requeues);
821 }
822
823 if (xstats)
824 *xstats = tbs[TCA_STATS_APP] ? : NULL;
825}
826
827void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
828{
829 SPRINT_BUF(b1);
830
831 if (tb[TCA_STATS2]) {
832 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
833 if (xstats && NULL == *xstats)
834 goto compat_xstats;
835 return;
836 }
837 /* backward compatibility */
838 if (tb[TCA_STATS]) {
d17b136f 839 struct tc_stats st = {};
e5879dc6 840
841 /* handle case where kernel returns more/less than we know about */
e5879dc6 842 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
843
844 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
ae665a52 845 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
e5879dc6 846 st.overlimits);
847
848 if (st.bps || st.pps || st.qlen || st.backlog) {
849 fprintf(fp, "\n%s", prefix);
850 if (st.bps || st.pps) {
851 fprintf(fp, "rate ");
852 if (st.bps)
853 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
854 if (st.pps)
855 fprintf(fp, "%upps ", st.pps);
856 }
857 if (st.qlen || st.backlog) {
858 fprintf(fp, "backlog ");
859 if (st.backlog)
860 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
861 if (st.qlen)
862 fprintf(fp, "%up ", st.qlen);
863 }
864 }
865 }
866
867compat_xstats:
868 if (tb[TCA_XSTATS] && xstats)
869 *xstats = tb[TCA_XSTATS];
870}