]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_util.c
Merge 'iproute2-master' into iproute2-next
[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
9455bec5 193static int parse_percent_rate(char *rate, const char *str, const char *dev)
927e3cfb
ND
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 337char *sprint_ticks(__u32 ticks, char *buf)
bd29e35d
PM
338{
339 return sprint_time(tc_core_tick2time(ticks), buf);
340}
341
32a121cb 342int get_size(unsigned int *size, const char *str)
aba5acdf
SH
343{
344 double sz;
345 char *p;
346
347 sz = strtod(str, &p);
348 if (p == str)
349 return -1;
350
351 if (*p) {
32a121cb 352 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0)
aba5acdf 353 sz *= 1024;
32a121cb 354 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0)
dbd90dc2
SH
355 sz *= 1024*1024*1024;
356 else if (strcasecmp(p, "gbit") == 0)
357 sz *= 1024*1024*1024/8;
32a121cb 358 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0)
aba5acdf
SH
359 sz *= 1024*1024;
360 else if (strcasecmp(p, "mbit") == 0)
361 sz *= 1024*1024/8;
362 else if (strcasecmp(p, "kbit") == 0)
363 sz *= 1024/8;
364 else if (strcasecmp(p, "b") != 0)
365 return -1;
366 }
367
368 *size = sz;
369 return 0;
370}
371
32a121cb 372int get_size_and_cell(unsigned int *size, int *cell_log, char *str)
aba5acdf 373{
32a121cb 374 char *slash = strchr(str, '/');
aba5acdf
SH
375
376 if (slash)
377 *slash = 0;
378
379 if (get_size(size, str))
380 return -1;
381
382 if (slash) {
383 int cell;
384 int i;
385
386 if (get_integer(&cell, slash+1, 0))
387 return -1;
388 *slash = '/';
389
32a121cb 390 for (i = 0; i < 32; i++) {
aba5acdf
SH
391 if ((1<<i) == cell) {
392 *cell_log = i;
393 return 0;
394 }
395 }
396 return -1;
397 }
398 return 0;
399}
400
2d165c08
SH
401void print_devname(enum output_type type, int ifindex)
402{
403 const char *ifname = ll_index_to_name(ifindex);
404
405 if (!is_json_context())
406 printf("dev ");
407
408 print_color_string(type, COLOR_IFNAME,
409 "dev", "%s ", ifname);
410}
411
9455bec5 412static void print_size(char *buf, int len, __u32 sz)
aba5acdf
SH
413{
414 double tmp = sz;
415
416 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
417 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
418 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
419 snprintf(buf, len, "%gKb", rint(tmp/1024));
420 else
421 snprintf(buf, len, "%ub", sz);
aba5acdf
SH
422}
423
32a121cb 424char *sprint_size(__u32 size, char *buf)
aba5acdf 425{
d40b38b4 426 print_size(buf, SPRINT_BSIZE-1, size);
aba5acdf
SH
427 return buf;
428}
429
e67aba55 430static const char *action_n2a(int action)
2373fde9 431{
70932006
PS
432 static char buf[64];
433
d19f72f7
JP
434 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
435 return "goto";
35f2a763
JHS
436 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
437 return "jump";
2373fde9 438 switch (action) {
70932006 439 case TC_ACT_UNSPEC:
2373fde9 440 return "continue";
2373fde9
SH
441 case TC_ACT_OK:
442 return "pass";
2373fde9
SH
443 case TC_ACT_SHOT:
444 return "drop";
2373fde9
SH
445 case TC_ACT_RECLASSIFY:
446 return "reclassify";
447 case TC_ACT_PIPE:
448 return "pipe";
449 case TC_ACT_STOLEN:
450 return "stolen";
d5ebd6fd
JP
451 case TC_ACT_TRAP:
452 return "trap";
2373fde9 453 default:
70932006 454 snprintf(buf, 64, "%d", action);
2373fde9
SH
455 return buf;
456 }
457}
aba5acdf 458
53aadc52
PS
459/* Convert action branch name into numeric format.
460 *
461 * Parameters:
462 * @arg - string to parse
463 * @result - pointer to output variable
464 * @allow_num - whether @arg may be in numeric format already
465 *
466 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
467 */
6f7df6b2 468int action_a2n(char *arg, int *result, bool allow_num)
2373fde9 469{
53aadc52
PS
470 int n;
471 char dummy;
472 struct {
473 const char *a;
474 int n;
475 } a2n[] = {
476 {"continue", TC_ACT_UNSPEC},
477 {"drop", TC_ACT_SHOT},
478 {"shot", TC_ACT_SHOT},
479 {"pass", TC_ACT_OK},
480 {"ok", TC_ACT_OK},
481 {"reclassify", TC_ACT_RECLASSIFY},
482 {"pipe", TC_ACT_PIPE},
d19f72f7 483 {"goto", TC_ACT_GOTO_CHAIN},
35f2a763 484 {"jump", TC_ACT_JUMP},
d5ebd6fd 485 {"trap", TC_ACT_TRAP},
53aadc52
PS
486 { NULL },
487 }, *iter;
488
489 for (iter = a2n; iter->a; iter++) {
490 if (matches(arg, iter->a) != 0)
491 continue;
6f7df6b2
PS
492 n = iter->n;
493 goto out_ok;
2373fde9 494 }
53aadc52
PS
495 if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1)
496 return -1;
497
6f7df6b2
PS
498out_ok:
499 if (result)
500 *result = n;
2373fde9
SH
501 return 0;
502}
503
c794b7b1
JP
504static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p,
505 bool allow_num, bool ignore_a2n_miss)
e67aba55
JP
506{
507 int argc = *argc_p;
508 char **argv = *argv_p;
509 int result;
510
511 if (!argc)
512 return -1;
513 if (action_a2n(*argv, &result, allow_num) == -1) {
c794b7b1
JP
514 if (!ignore_a2n_miss)
515 fprintf(stderr, "Bad action type %s\n", *argv);
e67aba55
JP
516 return -1;
517 }
d19f72f7
JP
518 if (result == TC_ACT_GOTO_CHAIN) {
519 __u32 chain_index;
520
521 NEXT_ARG();
522 if (matches(*argv, "chain") != 0) {
523 fprintf(stderr, "\"chain index\" expected\n");
524 return -1;
525 }
526 NEXT_ARG();
527 if (get_u32(&chain_index, *argv, 10) ||
528 chain_index > TC_ACT_EXT_VAL_MASK) {
529 fprintf(stderr, "Illegal \"chain index\"\n");
530 return -1;
531 }
532 result |= chain_index;
533 }
35f2a763
JHS
534 if (result == TC_ACT_JUMP) {
535 __u32 jump_cnt = 0;
536
537 NEXT_ARG();
538 if (get_u32(&jump_cnt, *argv, 10) ||
539 jump_cnt > TC_ACT_EXT_VAL_MASK) {
540 fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv);
541 return -1;
542 }
543 result |= jump_cnt;
544 }
75ef7b18 545 NEXT_ARG_FWD();
e67aba55
JP
546 *argc_p = argc;
547 *argv_p = argv;
548 *result_p = result;
549 return 0;
550}
551
c794b7b1
JP
552/* Parse action control including possible options.
553 *
554 * Parameters:
555 * @argc_p - pointer to argc to parse
556 * @argv_p - pointer to argv to parse
557 * @result_p - pointer to output variable
558 * @allow_num - whether action may be in numeric format already
559 *
560 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
561 */
562int parse_action_control(int *argc_p, char ***argv_p,
563 int *result_p, bool allow_num)
564{
565 return __parse_action_control(argc_p, argv_p, result_p,
566 allow_num, false);
567}
568
e67aba55
JP
569/* Parse action control including possible options.
570 *
571 * Parameters:
572 * @argc_p - pointer to argc to parse
573 * @argv_p - pointer to argv to parse
574 * @result_p - pointer to output variable
575 * @allow_num - whether action may be in numeric format already
576 * @default_result - set as a result in case of parsing error
577 *
578 * In case there is an error during parsing, the default result is used.
579 */
580void parse_action_control_dflt(int *argc_p, char ***argv_p,
581 int *result_p, bool allow_num,
582 int default_result)
583{
c794b7b1 584 if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true))
e67aba55
JP
585 *result_p = default_result;
586}
587
588static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p,
589 int *result1_p, int *result2_p,
590 bool allow_num)
591{
592 int argc = *argc_p;
593 char **argv = *argv_p;
66942e52 594 int result1 = -1, result2;
e67aba55
JP
595 int *result_p = &result1;
596 int ok = 0;
597 int ret;
598
599 while (argc > 0) {
600 switch (ok) {
601 case 1:
602 if (strcmp(*argv, "/") != 0)
603 goto out;
604 result_p = &result2;
605 NEXT_ARG();
606 /* fall-through */
607 case 0: /* fall-through */
608 case 2:
609 ret = parse_action_control(&argc, &argv,
610 result_p, allow_num);
611 if (ret)
612 return ret;
613 ok++;
614 break;
615 default:
616 goto out;
617 }
618 }
619out:
620 *result1_p = result1;
621 if (ok == 2)
622 *result2_p = result2;
623 *argc_p = argc;
624 *argv_p = argv;
625 return 0;
626}
627
628/* Parse action control with slash including possible options.
629 *
630 * Parameters:
631 * @argc_p - pointer to argc to parse
632 * @argv_p - pointer to argv to parse
633 * @result1_p - pointer to the first (before slash) output variable
634 * @result2_p - pointer to the second (after slash) output variable
635 * @allow_num - whether action may be in numeric format already
636 *
637 * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
638 */
639int parse_action_control_slash(int *argc_p, char ***argv_p,
640 int *result1_p, int *result2_p, bool allow_num)
641{
75ef7b18 642 int result1, result2, argc = *argc_p;
e67aba55 643 char **argv = *argv_p;
e67aba55
JP
644 char *p = strchr(*argv, '/');
645
646 if (!p)
647 return parse_action_control_slash_spaces(argc_p, argv_p,
648 result1_p, result2_p,
649 allow_num);
650 *p = 0;
651 if (action_a2n(*argv, &result1, allow_num)) {
b7c61286 652 *p = '/';
e67aba55
JP
653 return -1;
654 }
655
656 *p = '/';
657 if (action_a2n(p + 1, &result2, allow_num))
658 return -1;
659
660 *result1_p = result1;
661 *result2_p = result2;
75ef7b18
DC
662 NEXT_ARG_FWD();
663 *argc_p = argc;
664 *argv_p = argv;
e67aba55
JP
665 return 0;
666}
667
668void print_action_control(FILE *f, const char *prefix,
669 int action, const char *suffix)
670{
2704bd62
JP
671 print_string(PRINT_FP, NULL, "%s", prefix);
672 open_json_object("control_action");
673 print_string(PRINT_ANY, "type", "%s", action_n2a(action));
d19f72f7 674 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
2704bd62
JP
675 print_uint(PRINT_ANY, "chain", " chain %u",
676 action & TC_ACT_EXT_VAL_MASK);
35f2a763 677 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
2704bd62
JP
678 print_uint(PRINT_ANY, "jump", " %u",
679 action & TC_ACT_EXT_VAL_MASK);
680 close_json_object();
681 print_string(PRINT_FP, NULL, "%s", suffix);
e67aba55
JP
682}
683
32a121cb 684int get_linklayer(unsigned int *val, const char *arg)
292f29b4
JDB
685{
686 int res;
687
688 if (matches(arg, "ethernet") == 0)
689 res = LINKLAYER_ETHERNET;
690 else if (matches(arg, "atm") == 0)
691 res = LINKLAYER_ATM;
692 else if (matches(arg, "adsl") == 0)
693 res = LINKLAYER_ATM;
694 else
695 return -1; /* Indicate error */
696
697 *val = res;
698 return 0;
699}
700
9455bec5 701static void print_linklayer(char *buf, int len, unsigned int linklayer)
839c8456
JK
702{
703 switch (linklayer) {
704 case LINKLAYER_UNSPEC:
705 snprintf(buf, len, "%s", "unspec");
706 return;
707 case LINKLAYER_ETHERNET:
708 snprintf(buf, len, "%s", "ethernet");
709 return;
710 case LINKLAYER_ATM:
711 snprintf(buf, len, "%s", "atm");
712 return;
713 default:
714 snprintf(buf, len, "%s", "unknown");
715 return;
716 }
717}
718
32a121cb 719char *sprint_linklayer(unsigned int linklayer, char *buf)
839c8456
JK
720{
721 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
722 return buf;
723}
724
32a121cb 725void print_tm(FILE *f, const struct tcf_t *tm)
2373fde9 726{
5e8bc631 727 int hz = get_user_hz();
32a121cb 728
2704bd62
JP
729 if (tm->install != 0) {
730 print_uint(PRINT_JSON, "installed", NULL, tm->install);
731 print_uint(PRINT_FP, NULL, " installed %u sec",
732 (unsigned int)(tm->install/hz));
733 }
734 if (tm->lastuse != 0) {
735 print_uint(PRINT_JSON, "last_used", NULL, tm->lastuse);
736 print_uint(PRINT_FP, NULL, " used %u sec",
737 (unsigned int)(tm->lastuse/hz));
738 }
739 if (tm->expires != 0) {
740 print_uint(PRINT_JSON, "expires", NULL, tm->expires);
741 print_uint(PRINT_FP, NULL, " expires %u sec",
742 (unsigned int)(tm->expires/hz));
743 }
2373fde9 744}
e5879dc6 745
5ac13832
EC
746static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix)
747{
748 struct gnet_stats_basic bs_hw;
749
750 if (!tbs[TCA_STATS_BASIC_HW])
751 return;
752
753 memcpy(&bs_hw, RTA_DATA(tbs[TCA_STATS_BASIC_HW]),
754 MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC_HW]), sizeof(bs_hw)));
755
756 if (bs_hw.bytes == 0 && bs_hw.packets == 0)
757 return;
758
759 if (tbs[TCA_STATS_BASIC]) {
760 struct gnet_stats_basic bs;
761
762 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]),
763 MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]),
764 sizeof(bs)));
765
766 if (bs.bytes >= bs_hw.bytes && bs.packets >= bs_hw.packets) {
767 print_string(PRINT_FP, NULL, "%s", _SL_);
768 print_string(PRINT_FP, NULL, "%s", prefix);
769 print_lluint(PRINT_ANY, "sw_bytes",
770 "Sent software %llu bytes",
771 bs.bytes - bs_hw.bytes);
772 print_uint(PRINT_ANY, "sw_packets", " %u pkt",
773 bs.packets - bs_hw.packets);
774 }
775 }
776
777 print_string(PRINT_FP, NULL, "%s", _SL_);
778 print_string(PRINT_FP, NULL, "%s", prefix);
779 print_lluint(PRINT_ANY, "hw_bytes", "Sent hardware %llu bytes",
780 bs_hw.bytes);
781 print_uint(PRINT_ANY, "hw_packets", " %u pkt", bs_hw.packets);
782}
783
e5879dc6 784void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
785{
786 SPRINT_BUF(b1);
7d69fd97 787 struct rtattr *tbs[TCA_STATS_MAX + 1];
e5879dc6 788
7d69fd97 789 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
e5879dc6 790
791 if (tbs[TCA_STATS_BASIC]) {
792 struct gnet_stats_basic bs = {0};
32a121cb 793
e5879dc6 794 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
4fcec7f3
JP
795 print_string(PRINT_FP, NULL, "%s", prefix);
796 print_lluint(PRINT_ANY, "bytes", "Sent %llu bytes", bs.bytes);
797 print_uint(PRINT_ANY, "packets", " %u pkt", bs.packets);
e5879dc6 798 }
799
800 if (tbs[TCA_STATS_QUEUE]) {
801 struct gnet_stats_queue q = {0};
32a121cb 802
e5879dc6 803 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
4fcec7f3
JP
804 print_uint(PRINT_ANY, "drops", " (dropped %u", q.drops);
805 print_uint(PRINT_ANY, "overlimits", ", overlimits %u",
806 q.overlimits);
807 print_uint(PRINT_ANY, "requeues", " requeues %u) ", q.requeues);
e5879dc6 808 }
ae665a52 809
5ac13832
EC
810 if (tbs[TCA_STATS_BASIC_HW])
811 print_tcstats_basic_hw(tbs, prefix);
812
8f7574ed
ED
813 if (tbs[TCA_STATS_RATE_EST64]) {
814 struct gnet_stats_rate_est64 re = {0};
815
816 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
817 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
818 sizeof(re)));
4fcec7f3
JP
819 print_string(PRINT_FP, NULL, "\n%s", prefix);
820 print_lluint(PRINT_JSON, "rate", NULL, re.bps);
821 print_string(PRINT_FP, NULL, "rate %s",
822 sprint_rate(re.bps, b1));
823 print_lluint(PRINT_ANY, "pps", " %llupps", re.pps);
8f7574ed 824 } else if (tbs[TCA_STATS_RATE_EST]) {
e5879dc6 825 struct gnet_stats_rate_est re = {0};
8f7574ed
ED
826
827 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
828 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
4fcec7f3
JP
829 print_string(PRINT_FP, NULL, "\n%s", prefix);
830 print_uint(PRINT_JSON, "rate", NULL, re.bps);
831 print_string(PRINT_FP, NULL, "rate %s",
832 sprint_rate(re.bps, b1));
833 print_uint(PRINT_ANY, "pps", " %upps", re.pps);
e5879dc6 834 }
835
836 if (tbs[TCA_STATS_QUEUE]) {
837 struct gnet_stats_queue q = {0};
32a121cb 838
e5879dc6 839 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
840 if (!tbs[TCA_STATS_RATE_EST])
3adcbf37 841 print_string(PRINT_FP, NULL, "\n", "");
4fcec7f3 842 print_uint(PRINT_JSON, "backlog", NULL, q.backlog);
3adcbf37 843 print_string(PRINT_FP, NULL, "%s", prefix);
4fcec7f3
JP
844 print_string(PRINT_FP, NULL, "backlog %s",
845 sprint_size(q.backlog, b1));
846 print_uint(PRINT_ANY, "qlen", " %up", q.qlen);
44c76551 847 print_uint(PRINT_FP, NULL, " requeues %u", q.requeues);
e5879dc6 848 }
849
850 if (xstats)
851 *xstats = tbs[TCA_STATS_APP] ? : NULL;
852}
853
854void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
855{
856 SPRINT_BUF(b1);
857
858 if (tb[TCA_STATS2]) {
859 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
860 if (xstats && NULL == *xstats)
861 goto compat_xstats;
862 return;
863 }
864 /* backward compatibility */
865 if (tb[TCA_STATS]) {
d17b136f 866 struct tc_stats st = {};
e5879dc6 867
868 /* handle case where kernel returns more/less than we know about */
e5879dc6 869 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
870
871 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
ae665a52 872 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
e5879dc6 873 st.overlimits);
874
875 if (st.bps || st.pps || st.qlen || st.backlog) {
876 fprintf(fp, "\n%s", prefix);
877 if (st.bps || st.pps) {
878 fprintf(fp, "rate ");
879 if (st.bps)
880 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
881 if (st.pps)
882 fprintf(fp, "%upps ", st.pps);
883 }
884 if (st.qlen || st.backlog) {
885 fprintf(fp, "backlog ");
886 if (st.backlog)
887 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
888 if (st.qlen)
889 fprintf(fp, "%up ", st.qlen);
890 }
891 }
892 }
893
894compat_xstats:
895 if (tb[TCA_XSTATS] && xstats)
896 *xstats = tb[TCA_XSTATS];
897}