]> git.proxmox.com Git - mirror_iproute2.git/blame_incremental - tc/tc_util.c
Move the use_iec declaration to the tools
[mirror_iproute2.git] / tc / tc_util.c
... / ...
CommitLineData
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>
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <sys/param.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <math.h>
23#include <errno.h>
24
25#include "utils.h"
26#include "names.h"
27#include "tc_util.h"
28#include "tc_common.h"
29
30#ifndef LIBDIR
31#define LIBDIR "/usr/lib"
32#endif
33
34static struct db_names *cls_names;
35
36#define NAMES_DB "/etc/iproute2/tc_cls"
37
38int cls_names_init(char *path)
39{
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);
49 return -1;
50 }
51 if (ret) {
52 db_names_free(cls_names);
53 cls_names = NULL;
54 }
55
56 return 0;
57}
58
59void cls_names_uninit(void)
60{
61 db_names_free(cls_names);
62}
63
64const char *get_tc_lib(void)
65{
66 const char *lib_dir;
67
68 lib_dir = getenv("TC_LIB_DIR");
69 if (!lib_dir)
70 lib_dir = LIBDIR "/tc/";
71
72 return lib_dir;
73}
74
75int get_qdisc_handle(__u32 *h, const char *str)
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);
84 if (p == str || maj >= (1 << 16))
85 return -1;
86 maj <<= 16;
87 if (*p != ':' && *p != 0)
88 return -1;
89ok:
90 *h = maj;
91 return 0;
92}
93
94int get_tc_classid(__u32 *h, const char *str)
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 == ':') {
112 if (maj >= (1<<16))
113 return -1;
114 maj <<= 16;
115 str = p+1;
116 min = strtoul(str, &p, 16);
117 if (*p != 0)
118 return -1;
119 if (min >= (1<<16))
120 return -1;
121 maj |= min;
122 } else if (*p != 0)
123 return -1;
124
125ok:
126 *h = maj;
127 return 0;
128}
129
130int print_tc_classid(char *buf, int blen, __u32 h)
131{
132 SPRINT_BUF(handle) = {};
133 int hlen = SPRINT_BSIZE - 1;
134
135 if (h == TC_H_ROOT)
136 sprintf(handle, "root");
137 else if (h == TC_H_UNSPEC)
138 snprintf(handle, hlen, "none");
139 else if (TC_H_MAJ(h) == 0)
140 snprintf(handle, hlen, ":%x", TC_H_MIN(h));
141 else if (TC_H_MIN(h) == 0)
142 snprintf(handle, hlen, "%x:", TC_H_MAJ(h) >> 16);
143 else
144 snprintf(handle, hlen, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h));
145
146 if (use_names) {
147 char clname[IDNAME_MAX] = {};
148
149 if (id_to_name(cls_names, h, clname))
150 snprintf(buf, blen, "%s#%s", clname, handle);
151 else
152 snprintf(buf, blen, "%s", handle);
153 } else {
154 snprintf(buf, blen, "%s", handle);
155 }
156
157 return 0;
158}
159
160char *sprint_tc_classid(__u32 h, char *buf)
161{
162 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
163 strcpy(buf, "???");
164 return buf;
165}
166
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
193/* Parse a percent e.g: '30%'
194 * return: 0 = ok, -1 = error, 1 = out of range
195 */
196int parse_percent(double *val, const char *str)
197{
198 char *p;
199
200 *val = strtod(str, &p) / 100.;
201 if (*val > 1.0 || *val < 0.0)
202 return 1;
203 if (*p && strcmp(p, "%"))
204 return -1;
205
206 return 0;
207}
208
209static int parse_percent_rate(char *rate, size_t len,
210 const char *str, const char *dev)
211{
212 long dev_mbit;
213 int ret;
214 double perc, rate_bit;
215 char *str_perc = NULL;
216
217 if (!dev[0]) {
218 fprintf(stderr, "No device specified; specify device to rate limit by percentage\n");
219 return -1;
220 }
221
222 if (read_prop(dev, "speed", &dev_mbit))
223 return -1;
224
225 ret = sscanf(str, "%m[0-9.%]", &str_perc);
226 if (ret != 1)
227 goto malf;
228
229 ret = parse_percent(&perc, str_perc);
230 if (ret == 1) {
231 fprintf(stderr, "Invalid rate specified; should be between [0,100]%% but is %s\n", str);
232 goto err;
233 } else if (ret == -1) {
234 goto malf;
235 }
236
237 free(str_perc);
238
239 rate_bit = perc * dev_mbit * 1000 * 1000;
240
241 ret = snprintf(rate, len, "%lf", rate_bit);
242 if (ret <= 0 || ret >= len) {
243 fprintf(stderr, "Unable to parse calculated rate\n");
244 return -1;
245 }
246
247 return 0;
248
249malf:
250 fprintf(stderr, "Specified rate value could not be read or is malformed\n");
251err:
252 free(str_perc);
253 return -1;
254}
255
256int get_percent_rate(unsigned int *rate, const char *str, const char *dev)
257{
258 char r_str[20];
259
260 if (parse_percent_rate(r_str, sizeof(r_str), str, dev))
261 return -1;
262
263 return get_rate(rate, r_str);
264}
265
266int get_percent_rate64(__u64 *rate, const char *str, const char *dev)
267{
268 char r_str[20];
269
270 if (parse_percent_rate(r_str, sizeof(r_str), str, dev))
271 return -1;
272
273 return get_rate64(rate, r_str);
274}
275
276int get_rate(unsigned int *rate, const char *str)
277{
278 char *p;
279 double bps = strtod(str, &p);
280 const struct rate_suffix *s;
281
282 if (p == str)
283 return -1;
284
285 for (s = suffixes; s->name; ++s) {
286 if (strcasecmp(s->name, p) == 0) {
287 bps *= s->scale;
288 p += strlen(p);
289 break;
290 }
291 }
292
293 if (*p)
294 return -1; /* unknown suffix */
295
296 bps /= 8; /* -> bytes per second */
297 *rate = bps;
298 /* detect if an overflow happened */
299 if (*rate != floor(bps))
300 return -1;
301 return 0;
302}
303
304int get_rate64(__u64 *rate, const char *str)
305{
306 char *p;
307 double bps = strtod(str, &p);
308 const struct rate_suffix *s;
309
310 if (p == str)
311 return -1;
312
313 for (s = suffixes; s->name; ++s) {
314 if (strcasecmp(s->name, p) == 0) {
315 bps *= s->scale;
316 p += strlen(p);
317 break;
318 }
319 }
320
321 if (*p)
322 return -1; /* unknown suffix */
323
324 bps /= 8; /* -> bytes per second */
325 *rate = bps;
326 return 0;
327}
328
329void print_rate(char *buf, int len, __u64 rate)
330{
331 extern int use_iec;
332 unsigned long kilo = use_iec ? 1024 : 1000;
333 const char *str = use_iec ? "i" : "";
334 static char *units[5] = {"", "K", "M", "G", "T"};
335 int i;
336
337 rate <<= 3; /* bytes/sec -> bits/sec */
338
339 for (i = 0; i < ARRAY_SIZE(units) - 1; i++) {
340 if (rate < kilo)
341 break;
342 if (((rate % kilo) != 0) && rate < 1000*kilo)
343 break;
344 rate /= kilo;
345 }
346
347 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
348}
349
350char *sprint_rate(__u64 rate, char *buf)
351{
352 print_rate(buf, SPRINT_BSIZE-1, rate);
353 return buf;
354}
355
356char *sprint_ticks(__u32 ticks, char *buf)
357{
358 return sprint_time(tc_core_tick2time(ticks), buf);
359}
360
361int get_size(unsigned int *size, const char *str)
362{
363 double sz;
364 char *p;
365
366 sz = strtod(str, &p);
367 if (p == str)
368 return -1;
369
370 if (*p) {
371 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0)
372 sz *= 1024;
373 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0)
374 sz *= 1024*1024*1024;
375 else if (strcasecmp(p, "gbit") == 0)
376 sz *= 1024*1024*1024/8;
377 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0)
378 sz *= 1024*1024;
379 else if (strcasecmp(p, "mbit") == 0)
380 sz *= 1024*1024/8;
381 else if (strcasecmp(p, "kbit") == 0)
382 sz *= 1024/8;
383 else if (strcasecmp(p, "b") != 0)
384 return -1;
385 }
386
387 *size = sz;
388
389 /* detect if an overflow happened */
390 if (*size != floor(sz))
391 return -1;
392
393 return 0;
394}
395
396int get_size_and_cell(unsigned int *size, int *cell_log, char *str)
397{
398 char *slash = strchr(str, '/');
399
400 if (slash)
401 *slash = 0;
402
403 if (get_size(size, str))
404 return -1;
405
406 if (slash) {
407 int cell;
408 int i;
409
410 if (get_integer(&cell, slash+1, 0))
411 return -1;
412 *slash = '/';
413
414 for (i = 0; i < 32; i++) {
415 if ((1<<i) == cell) {
416 *cell_log = i;
417 return 0;
418 }
419 }
420 return -1;
421 }
422 return 0;
423}
424
425void print_devname(enum output_type type, int ifindex)
426{
427 const char *ifname = ll_index_to_name(ifindex);
428
429 if (!is_json_context())
430 printf("dev ");
431
432 print_color_string(type, COLOR_IFNAME,
433 "dev", "%s ", ifname);
434}
435
436static void print_size(char *buf, int len, __u32 sz)
437{
438 double tmp = sz;
439
440 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
441 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
442 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
443 snprintf(buf, len, "%gKb", rint(tmp/1024));
444 else
445 snprintf(buf, len, "%ub", sz);
446}
447
448char *sprint_size(__u32 size, char *buf)
449{
450 print_size(buf, SPRINT_BSIZE-1, size);
451 return buf;
452}
453
454static const char *action_n2a(int action)
455{
456 static char buf[64];
457
458 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
459 return "goto";
460 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
461 return "jump";
462 switch (action) {
463 case TC_ACT_UNSPEC:
464 return "continue";
465 case TC_ACT_OK:
466 return "pass";
467 case TC_ACT_SHOT:
468 return "drop";
469 case TC_ACT_RECLASSIFY:
470 return "reclassify";
471 case TC_ACT_PIPE:
472 return "pipe";
473 case TC_ACT_STOLEN:
474 return "stolen";
475 case TC_ACT_TRAP:
476 return "trap";
477 default:
478 snprintf(buf, 64, "%d", action);
479 return buf;
480 }
481}
482
483/* Convert action branch name into numeric format.
484 *
485 * Parameters:
486 * @arg - string to parse
487 * @result - pointer to output variable
488 * @allow_num - whether @arg may be in numeric format already
489 *
490 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
491 */
492int action_a2n(char *arg, int *result, bool allow_num)
493{
494 int n;
495 char dummy;
496 struct {
497 const char *a;
498 int n;
499 } a2n[] = {
500 {"continue", TC_ACT_UNSPEC},
501 {"drop", TC_ACT_SHOT},
502 {"shot", TC_ACT_SHOT},
503 {"pass", TC_ACT_OK},
504 {"ok", TC_ACT_OK},
505 {"reclassify", TC_ACT_RECLASSIFY},
506 {"pipe", TC_ACT_PIPE},
507 {"goto", TC_ACT_GOTO_CHAIN},
508 {"jump", TC_ACT_JUMP},
509 {"trap", TC_ACT_TRAP},
510 { NULL },
511 }, *iter;
512
513 for (iter = a2n; iter->a; iter++) {
514 if (matches(arg, iter->a) != 0)
515 continue;
516 n = iter->n;
517 goto out_ok;
518 }
519 if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1)
520 return -1;
521
522out_ok:
523 if (result)
524 *result = n;
525 return 0;
526}
527
528static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p,
529 bool allow_num, bool ignore_a2n_miss)
530{
531 int argc = *argc_p;
532 char **argv = *argv_p;
533 int result;
534
535 if (!argc)
536 return -1;
537 if (action_a2n(*argv, &result, allow_num) == -1) {
538 if (!ignore_a2n_miss)
539 fprintf(stderr, "Bad action type %s\n", *argv);
540 return -1;
541 }
542 if (result == TC_ACT_GOTO_CHAIN) {
543 __u32 chain_index;
544
545 NEXT_ARG();
546 if (matches(*argv, "chain") != 0) {
547 fprintf(stderr, "\"chain index\" expected\n");
548 return -1;
549 }
550 NEXT_ARG();
551 if (get_u32(&chain_index, *argv, 10) ||
552 chain_index > TC_ACT_EXT_VAL_MASK) {
553 fprintf(stderr, "Illegal \"chain index\"\n");
554 return -1;
555 }
556 result |= chain_index;
557 }
558 if (result == TC_ACT_JUMP) {
559 __u32 jump_cnt = 0;
560
561 NEXT_ARG();
562 if (get_u32(&jump_cnt, *argv, 10) ||
563 jump_cnt > TC_ACT_EXT_VAL_MASK) {
564 fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv);
565 return -1;
566 }
567 result |= jump_cnt;
568 }
569 NEXT_ARG_FWD();
570 *argc_p = argc;
571 *argv_p = argv;
572 *result_p = result;
573 return 0;
574}
575
576/* Parse action control including possible options.
577 *
578 * Parameters:
579 * @argc_p - pointer to argc to parse
580 * @argv_p - pointer to argv to parse
581 * @result_p - pointer to output variable
582 * @allow_num - whether action may be in numeric format already
583 *
584 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
585 */
586int parse_action_control(int *argc_p, char ***argv_p,
587 int *result_p, bool allow_num)
588{
589 return __parse_action_control(argc_p, argv_p, result_p,
590 allow_num, false);
591}
592
593/* Parse action control including possible options.
594 *
595 * Parameters:
596 * @argc_p - pointer to argc to parse
597 * @argv_p - pointer to argv to parse
598 * @result_p - pointer to output variable
599 * @allow_num - whether action may be in numeric format already
600 * @default_result - set as a result in case of parsing error
601 *
602 * In case there is an error during parsing, the default result is used.
603 */
604void parse_action_control_dflt(int *argc_p, char ***argv_p,
605 int *result_p, bool allow_num,
606 int default_result)
607{
608 if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true))
609 *result_p = default_result;
610}
611
612static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p,
613 int *result1_p, int *result2_p,
614 bool allow_num)
615{
616 int argc = *argc_p;
617 char **argv = *argv_p;
618 int result1 = -1, result2;
619 int *result_p = &result1;
620 int ok = 0;
621 int ret;
622
623 while (argc > 0) {
624 switch (ok) {
625 case 1:
626 if (strcmp(*argv, "/") != 0)
627 goto out;
628 result_p = &result2;
629 NEXT_ARG();
630 /* fall-through */
631 case 0: /* fall-through */
632 case 2:
633 ret = parse_action_control(&argc, &argv,
634 result_p, allow_num);
635 if (ret)
636 return ret;
637 ok++;
638 break;
639 default:
640 goto out;
641 }
642 }
643out:
644 *result1_p = result1;
645 if (ok == 2)
646 *result2_p = result2;
647 *argc_p = argc;
648 *argv_p = argv;
649 return 0;
650}
651
652/* Parse action control with slash including possible options.
653 *
654 * Parameters:
655 * @argc_p - pointer to argc to parse
656 * @argv_p - pointer to argv to parse
657 * @result1_p - pointer to the first (before slash) output variable
658 * @result2_p - pointer to the second (after slash) output variable
659 * @allow_num - whether action may be in numeric format already
660 *
661 * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
662 */
663int parse_action_control_slash(int *argc_p, char ***argv_p,
664 int *result1_p, int *result2_p, bool allow_num)
665{
666 int result1, result2, argc = *argc_p;
667 char **argv = *argv_p;
668 char *p = strchr(*argv, '/');
669
670 if (!p)
671 return parse_action_control_slash_spaces(argc_p, argv_p,
672 result1_p, result2_p,
673 allow_num);
674 *p = 0;
675 if (action_a2n(*argv, &result1, allow_num)) {
676 *p = '/';
677 return -1;
678 }
679
680 *p = '/';
681 if (action_a2n(p + 1, &result2, allow_num))
682 return -1;
683
684 *result1_p = result1;
685 *result2_p = result2;
686 NEXT_ARG_FWD();
687 *argc_p = argc;
688 *argv_p = argv;
689 return 0;
690}
691
692void print_action_control(FILE *f, const char *prefix,
693 int action, const char *suffix)
694{
695 print_string(PRINT_FP, NULL, "%s", prefix);
696 open_json_object("control_action");
697 print_string(PRINT_ANY, "type", "%s", action_n2a(action));
698 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
699 print_uint(PRINT_ANY, "chain", " chain %u",
700 action & TC_ACT_EXT_VAL_MASK);
701 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
702 print_uint(PRINT_ANY, "jump", " %u",
703 action & TC_ACT_EXT_VAL_MASK);
704 close_json_object();
705 print_string(PRINT_FP, NULL, "%s", suffix);
706}
707
708int get_linklayer(unsigned int *val, const char *arg)
709{
710 int res;
711
712 if (matches(arg, "ethernet") == 0)
713 res = LINKLAYER_ETHERNET;
714 else if (matches(arg, "atm") == 0)
715 res = LINKLAYER_ATM;
716 else if (matches(arg, "adsl") == 0)
717 res = LINKLAYER_ATM;
718 else
719 return -1; /* Indicate error */
720
721 *val = res;
722 return 0;
723}
724
725static void print_linklayer(char *buf, int len, unsigned int linklayer)
726{
727 switch (linklayer) {
728 case LINKLAYER_UNSPEC:
729 snprintf(buf, len, "%s", "unspec");
730 return;
731 case LINKLAYER_ETHERNET:
732 snprintf(buf, len, "%s", "ethernet");
733 return;
734 case LINKLAYER_ATM:
735 snprintf(buf, len, "%s", "atm");
736 return;
737 default:
738 snprintf(buf, len, "%s", "unknown");
739 return;
740 }
741}
742
743char *sprint_linklayer(unsigned int linklayer, char *buf)
744{
745 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
746 return buf;
747}
748
749void print_tm(FILE *f, const struct tcf_t *tm)
750{
751 int hz = get_user_hz();
752
753 if (tm->install != 0)
754 print_uint(PRINT_ANY, "installed", " installed %u sec",
755 tm->install / hz);
756
757 if (tm->lastuse != 0)
758 print_uint(PRINT_ANY, "last_used", " used %u sec",
759 tm->lastuse / hz);
760
761 if (tm->firstuse != 0)
762 print_uint(PRINT_ANY, "first_used", " firstused %u sec",
763 tm->firstuse / hz);
764
765 if (tm->expires != 0)
766 print_uint(PRINT_ANY, "expires", " expires %u sec",
767 tm->expires / hz);
768}
769
770static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix)
771{
772 struct gnet_stats_basic bs_hw;
773
774 if (!tbs[TCA_STATS_BASIC_HW])
775 return;
776
777 memcpy(&bs_hw, RTA_DATA(tbs[TCA_STATS_BASIC_HW]),
778 MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC_HW]), sizeof(bs_hw)));
779
780 if (bs_hw.bytes == 0 && bs_hw.packets == 0)
781 return;
782
783 if (tbs[TCA_STATS_BASIC]) {
784 struct gnet_stats_basic bs;
785
786 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]),
787 MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]),
788 sizeof(bs)));
789
790 if (bs.bytes >= bs_hw.bytes && bs.packets >= bs_hw.packets) {
791 print_nl();
792 print_string(PRINT_FP, NULL, "%s", prefix);
793 print_lluint(PRINT_ANY, "sw_bytes",
794 "Sent software %llu bytes",
795 bs.bytes - bs_hw.bytes);
796 print_uint(PRINT_ANY, "sw_packets", " %u pkt",
797 bs.packets - bs_hw.packets);
798 }
799 }
800
801 print_nl();
802 print_string(PRINT_FP, NULL, "%s", prefix);
803 print_lluint(PRINT_ANY, "hw_bytes", "Sent hardware %llu bytes",
804 bs_hw.bytes);
805 print_uint(PRINT_ANY, "hw_packets", " %u pkt", bs_hw.packets);
806}
807
808void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
809{
810 SPRINT_BUF(b1);
811 struct rtattr *tbs[TCA_STATS_MAX + 1];
812
813 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
814
815 if (tbs[TCA_STATS_BASIC]) {
816 struct gnet_stats_basic bs = {0};
817 __u64 packets64 = 0;
818
819 if (tbs[TCA_STATS_PKT64])
820 packets64 = rta_getattr_u64(tbs[TCA_STATS_PKT64]);
821
822 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]),
823 MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
824 print_string(PRINT_FP, NULL, "%s", prefix);
825 print_lluint(PRINT_ANY, "bytes", "Sent %llu bytes", bs.bytes);
826 if (packets64)
827 print_lluint(PRINT_ANY, "packets",
828 " %llu pkt", packets64);
829 else
830 print_uint(PRINT_ANY, "packets",
831 " %u pkt", bs.packets);
832 }
833
834 if (tbs[TCA_STATS_QUEUE]) {
835 struct gnet_stats_queue q = {0};
836
837 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]),
838 MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
839 print_uint(PRINT_ANY, "drops", " (dropped %u", q.drops);
840 print_uint(PRINT_ANY, "overlimits", ", overlimits %u",
841 q.overlimits);
842 print_uint(PRINT_ANY, "requeues", " requeues %u) ", q.requeues);
843 }
844
845 if (tbs[TCA_STATS_BASIC_HW])
846 print_tcstats_basic_hw(tbs, prefix);
847
848 if (tbs[TCA_STATS_RATE_EST64]) {
849 struct gnet_stats_rate_est64 re = {0};
850
851 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
852 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
853 sizeof(re)));
854 print_string(PRINT_FP, NULL, "\n%s", prefix);
855 print_lluint(PRINT_JSON, "rate", NULL, re.bps);
856 print_string(PRINT_FP, NULL, "rate %s",
857 sprint_rate(re.bps, b1));
858 print_lluint(PRINT_ANY, "pps", " %llupps", re.pps);
859 } else if (tbs[TCA_STATS_RATE_EST]) {
860 struct gnet_stats_rate_est re = {0};
861
862 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
863 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
864 print_string(PRINT_FP, NULL, "\n%s", prefix);
865 print_uint(PRINT_JSON, "rate", NULL, re.bps);
866 print_string(PRINT_FP, NULL, "rate %s",
867 sprint_rate(re.bps, b1));
868 print_uint(PRINT_ANY, "pps", " %upps", re.pps);
869 }
870
871 if (tbs[TCA_STATS_QUEUE]) {
872 struct gnet_stats_queue q = {0};
873
874 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]),
875 MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
876 if (!tbs[TCA_STATS_RATE_EST])
877 print_nl();
878 print_uint(PRINT_JSON, "backlog", NULL, q.backlog);
879 print_string(PRINT_FP, NULL, "%s", prefix);
880 print_string(PRINT_FP, NULL, "backlog %s",
881 sprint_size(q.backlog, b1));
882 print_uint(PRINT_ANY, "qlen", " %up", q.qlen);
883 print_uint(PRINT_FP, NULL, " requeues %u", q.requeues);
884 }
885
886 if (xstats)
887 *xstats = tbs[TCA_STATS_APP] ? : NULL;
888}
889
890void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix,
891 struct rtattr **xstats)
892{
893 SPRINT_BUF(b1);
894
895 if (tb[TCA_STATS2]) {
896 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
897 if (xstats && !*xstats)
898 goto compat_xstats;
899 return;
900 }
901 /* backward compatibility */
902 if (tb[TCA_STATS]) {
903 struct tc_stats st = {};
904
905 /* handle case where kernel returns more/less than we know about */
906 memcpy(&st, RTA_DATA(tb[TCA_STATS]),
907 MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
908
909 fprintf(fp,
910 "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
911 prefix, (unsigned long long)st.bytes,
912 st.packets, st.drops, st.overlimits);
913
914 if (st.bps || st.pps || st.qlen || st.backlog) {
915 fprintf(fp, "\n%s", prefix);
916 if (st.bps || st.pps) {
917 fprintf(fp, "rate ");
918 if (st.bps)
919 fprintf(fp, "%s ",
920 sprint_rate(st.bps, b1));
921 if (st.pps)
922 fprintf(fp, "%upps ", st.pps);
923 }
924 if (st.qlen || st.backlog) {
925 fprintf(fp, "backlog ");
926 if (st.backlog)
927 fprintf(fp, "%s ",
928 sprint_size(st.backlog, b1));
929 if (st.qlen)
930 fprintf(fp, "%up ", st.qlen);
931 }
932 }
933 }
934
935compat_xstats:
936 if (tb[TCA_XSTATS] && xstats)
937 *xstats = tb[TCA_XSTATS];
938}
939
940static void print_masked_type(__u32 type_max,
941 __u32 (*rta_getattr_type)(const struct rtattr *),
942 const char *name, struct rtattr *attr,
943 struct rtattr *mask_attr, bool newline)
944{
945 SPRINT_BUF(namefrm);
946 __u32 value, mask;
947 SPRINT_BUF(out);
948 size_t done;
949
950 if (!attr)
951 return;
952
953 value = rta_getattr_type(attr);
954 mask = mask_attr ? rta_getattr_type(mask_attr) : type_max;
955
956 if (is_json_context()) {
957 sprintf(namefrm, "\n %s %%u", name);
958 print_hu(PRINT_ANY, name, namefrm,
959 rta_getattr_type(attr));
960 if (mask != type_max) {
961 char mask_name[SPRINT_BSIZE-6];
962
963 sprintf(mask_name, "%s_mask", name);
964 if (newline)
965 print_string(PRINT_FP, NULL, "%s ", _SL_);
966 sprintf(namefrm, " %s %%u", mask_name);
967 print_hu(PRINT_ANY, mask_name, namefrm, mask);
968 }
969 } else {
970 done = sprintf(out, "%u", value);
971 if (mask != type_max)
972 sprintf(out + done, "/0x%x", mask);
973 if (newline)
974 print_string(PRINT_FP, NULL, "%s ", _SL_);
975 sprintf(namefrm, " %s %%s", name);
976 print_string(PRINT_ANY, name, namefrm, out);
977 }
978}
979
980void print_masked_u32(const char *name, struct rtattr *attr,
981 struct rtattr *mask_attr, bool newline)
982{
983 print_masked_type(UINT32_MAX, rta_getattr_u32, name, attr, mask_attr,
984 newline);
985}
986
987static __u32 __rta_getattr_u16_u32(const struct rtattr *attr)
988{
989 return rta_getattr_u16(attr);
990}
991
992void print_masked_u16(const char *name, struct rtattr *attr,
993 struct rtattr *mask_attr, bool newline)
994{
995 print_masked_type(UINT16_MAX, __rta_getattr_u16_u32, name, attr,
996 mask_attr, newline);
997}
998
999static __u32 __rta_getattr_u8_u32(const struct rtattr *attr)
1000{
1001 return rta_getattr_u8(attr);
1002}
1003
1004void print_masked_u8(const char *name, struct rtattr *attr,
1005 struct rtattr *mask_attr, bool newline)
1006{
1007 print_masked_type(UINT8_MAX, __rta_getattr_u8_u32, name, attr,
1008 mask_attr, newline);
1009}
1010
1011static __u32 __rta_getattr_be16_u32(const struct rtattr *attr)
1012{
1013 return rta_getattr_be16(attr);
1014}
1015
1016void print_masked_be16(const char *name, struct rtattr *attr,
1017 struct rtattr *mask_attr, bool newline)
1018{
1019 print_masked_type(UINT16_MAX, __rta_getattr_be16_u32, name, attr,
1020 mask_attr, newline);
1021}