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