]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_util.c
tc_util: Add support for showing TCA_STATS_BASIC_HW statistics
[mirror_iproute2.git] / tc / tc_util.c
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
34 static struct db_names *cls_names;
35
36 #define NAMES_DB "/etc/iproute2/tc_cls"
37
38 int 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
59 void cls_names_uninit(void)
60 {
61 db_names_free(cls_names);
62 }
63
64 const 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
75 int 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;
89 ok:
90 *h = maj;
91 return 0;
92 }
93
94 int 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
125 ok:
126 *h = maj;
127 return 0;
128 }
129
130 int 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
160 char *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 */
168 static 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 int 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
232 malf:
233 fprintf(stderr, "Specified rate value could not be read or is malformed\n");
234 return -1;
235 }
236
237 int 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
247 int 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 }
256
257 int get_rate(unsigned int *rate, const char *str)
258 {
259 char *p;
260 double bps = strtod(str, &p);
261 const struct rate_suffix *s;
262
263 if (p == str)
264 return -1;
265
266 for (s = suffixes; s->name; ++s) {
267 if (strcasecmp(s->name, p) == 0) {
268 bps *= s->scale;
269 p += strlen(p);
270 break;
271 }
272 }
273
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;
283 }
284
285 int 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
310 void print_rate(char *buf, int len, __u64 rate)
311 {
312 extern int use_iec;
313 unsigned long kilo = use_iec ? 1024 : 1000;
314 const char *str = use_iec ? "i" : "";
315 static char *units[5] = {"", "K", "M", "G", "T"};
316 int i;
317
318 rate <<= 3; /* bytes/sec -> bits/sec */
319
320 for (i = 0; i < ARRAY_SIZE(units) - 1; i++) {
321 if (rate < kilo)
322 break;
323 if (((rate % kilo) != 0) && rate < 1000*kilo)
324 break;
325 rate /= kilo;
326 }
327
328 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str);
329 }
330
331 char *sprint_rate(__u64 rate, char *buf)
332 {
333 print_rate(buf, SPRINT_BSIZE-1, rate);
334 return buf;
335 }
336
337 char *sprint_ticks(__u32 ticks, char *buf)
338 {
339 return sprint_time(tc_core_tick2time(ticks), buf);
340 }
341
342 int get_size(unsigned int *size, const char *str)
343 {
344 double sz;
345 char *p;
346
347 sz = strtod(str, &p);
348 if (p == str)
349 return -1;
350
351 if (*p) {
352 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0)
353 sz *= 1024;
354 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0)
355 sz *= 1024*1024*1024;
356 else if (strcasecmp(p, "gbit") == 0)
357 sz *= 1024*1024*1024/8;
358 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0)
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
372 int get_size_and_cell(unsigned int *size, int *cell_log, char *str)
373 {
374 char *slash = strchr(str, '/');
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
390 for (i = 0; i < 32; i++) {
391 if ((1<<i) == cell) {
392 *cell_log = i;
393 return 0;
394 }
395 }
396 return -1;
397 }
398 return 0;
399 }
400
401 void 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
412 void print_size(char *buf, int len, __u32 sz)
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);
422 }
423
424 char *sprint_size(__u32 size, char *buf)
425 {
426 print_size(buf, SPRINT_BSIZE-1, size);
427 return buf;
428 }
429
430 void print_qdisc_handle(char *buf, int len, __u32 h)
431 {
432 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
433 }
434
435 char *sprint_qdisc_handle(__u32 h, char *buf)
436 {
437 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
438 return buf;
439 }
440
441 static const char *action_n2a(int action)
442 {
443 static char buf[64];
444
445 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
446 return "goto";
447 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
448 return "jump";
449 switch (action) {
450 case TC_ACT_UNSPEC:
451 return "continue";
452 case TC_ACT_OK:
453 return "pass";
454 case TC_ACT_SHOT:
455 return "drop";
456 case TC_ACT_RECLASSIFY:
457 return "reclassify";
458 case TC_ACT_PIPE:
459 return "pipe";
460 case TC_ACT_STOLEN:
461 return "stolen";
462 case TC_ACT_TRAP:
463 return "trap";
464 default:
465 snprintf(buf, 64, "%d", action);
466 return buf;
467 }
468 }
469
470 /* Convert action branch name into numeric format.
471 *
472 * Parameters:
473 * @arg - string to parse
474 * @result - pointer to output variable
475 * @allow_num - whether @arg may be in numeric format already
476 *
477 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
478 */
479 int action_a2n(char *arg, int *result, bool allow_num)
480 {
481 int n;
482 char dummy;
483 struct {
484 const char *a;
485 int n;
486 } a2n[] = {
487 {"continue", TC_ACT_UNSPEC},
488 {"drop", TC_ACT_SHOT},
489 {"shot", TC_ACT_SHOT},
490 {"pass", TC_ACT_OK},
491 {"ok", TC_ACT_OK},
492 {"reclassify", TC_ACT_RECLASSIFY},
493 {"pipe", TC_ACT_PIPE},
494 {"goto", TC_ACT_GOTO_CHAIN},
495 {"jump", TC_ACT_JUMP},
496 {"trap", TC_ACT_TRAP},
497 { NULL },
498 }, *iter;
499
500 for (iter = a2n; iter->a; iter++) {
501 if (matches(arg, iter->a) != 0)
502 continue;
503 n = iter->n;
504 goto out_ok;
505 }
506 if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1)
507 return -1;
508
509 out_ok:
510 if (result)
511 *result = n;
512 return 0;
513 }
514
515 static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p,
516 bool allow_num, bool ignore_a2n_miss)
517 {
518 int argc = *argc_p;
519 char **argv = *argv_p;
520 int result;
521
522 if (!argc)
523 return -1;
524 if (action_a2n(*argv, &result, allow_num) == -1) {
525 if (!ignore_a2n_miss)
526 fprintf(stderr, "Bad action type %s\n", *argv);
527 return -1;
528 }
529 if (result == TC_ACT_GOTO_CHAIN) {
530 __u32 chain_index;
531
532 NEXT_ARG();
533 if (matches(*argv, "chain") != 0) {
534 fprintf(stderr, "\"chain index\" expected\n");
535 return -1;
536 }
537 NEXT_ARG();
538 if (get_u32(&chain_index, *argv, 10) ||
539 chain_index > TC_ACT_EXT_VAL_MASK) {
540 fprintf(stderr, "Illegal \"chain index\"\n");
541 return -1;
542 }
543 result |= chain_index;
544 }
545 if (result == TC_ACT_JUMP) {
546 __u32 jump_cnt = 0;
547
548 NEXT_ARG();
549 if (get_u32(&jump_cnt, *argv, 10) ||
550 jump_cnt > TC_ACT_EXT_VAL_MASK) {
551 fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv);
552 return -1;
553 }
554 result |= jump_cnt;
555 }
556 NEXT_ARG_FWD();
557 *argc_p = argc;
558 *argv_p = argv;
559 *result_p = result;
560 return 0;
561 }
562
563 /* Parse action control including possible options.
564 *
565 * Parameters:
566 * @argc_p - pointer to argc to parse
567 * @argv_p - pointer to argv to parse
568 * @result_p - pointer to output variable
569 * @allow_num - whether action may be in numeric format already
570 *
571 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
572 */
573 int parse_action_control(int *argc_p, char ***argv_p,
574 int *result_p, bool allow_num)
575 {
576 return __parse_action_control(argc_p, argv_p, result_p,
577 allow_num, false);
578 }
579
580 /* Parse action control including possible options.
581 *
582 * Parameters:
583 * @argc_p - pointer to argc to parse
584 * @argv_p - pointer to argv to parse
585 * @result_p - pointer to output variable
586 * @allow_num - whether action may be in numeric format already
587 * @default_result - set as a result in case of parsing error
588 *
589 * In case there is an error during parsing, the default result is used.
590 */
591 void parse_action_control_dflt(int *argc_p, char ***argv_p,
592 int *result_p, bool allow_num,
593 int default_result)
594 {
595 if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true))
596 *result_p = default_result;
597 }
598
599 static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p,
600 int *result1_p, int *result2_p,
601 bool allow_num)
602 {
603 int argc = *argc_p;
604 char **argv = *argv_p;
605 int result1 = -1, result2;
606 int *result_p = &result1;
607 int ok = 0;
608 int ret;
609
610 while (argc > 0) {
611 switch (ok) {
612 case 1:
613 if (strcmp(*argv, "/") != 0)
614 goto out;
615 result_p = &result2;
616 NEXT_ARG();
617 /* fall-through */
618 case 0: /* fall-through */
619 case 2:
620 ret = parse_action_control(&argc, &argv,
621 result_p, allow_num);
622 if (ret)
623 return ret;
624 ok++;
625 break;
626 default:
627 goto out;
628 }
629 }
630 out:
631 *result1_p = result1;
632 if (ok == 2)
633 *result2_p = result2;
634 *argc_p = argc;
635 *argv_p = argv;
636 return 0;
637 }
638
639 /* Parse action control with slash including possible options.
640 *
641 * Parameters:
642 * @argc_p - pointer to argc to parse
643 * @argv_p - pointer to argv to parse
644 * @result1_p - pointer to the first (before slash) output variable
645 * @result2_p - pointer to the second (after slash) output variable
646 * @allow_num - whether action may be in numeric format already
647 *
648 * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
649 */
650 int parse_action_control_slash(int *argc_p, char ***argv_p,
651 int *result1_p, int *result2_p, bool allow_num)
652 {
653 int result1, result2, argc = *argc_p;
654 char **argv = *argv_p;
655 char *p = strchr(*argv, '/');
656
657 if (!p)
658 return parse_action_control_slash_spaces(argc_p, argv_p,
659 result1_p, result2_p,
660 allow_num);
661 *p = 0;
662 if (action_a2n(*argv, &result1, allow_num)) {
663 *p = '/';
664 return -1;
665 }
666
667 *p = '/';
668 if (action_a2n(p + 1, &result2, allow_num))
669 return -1;
670
671 *result1_p = result1;
672 *result2_p = result2;
673 NEXT_ARG_FWD();
674 *argc_p = argc;
675 *argv_p = argv;
676 return 0;
677 }
678
679 void print_action_control(FILE *f, const char *prefix,
680 int action, const char *suffix)
681 {
682 print_string(PRINT_FP, NULL, "%s", prefix);
683 open_json_object("control_action");
684 print_string(PRINT_ANY, "type", "%s", action_n2a(action));
685 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN))
686 print_uint(PRINT_ANY, "chain", " chain %u",
687 action & TC_ACT_EXT_VAL_MASK);
688 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP))
689 print_uint(PRINT_ANY, "jump", " %u",
690 action & TC_ACT_EXT_VAL_MASK);
691 close_json_object();
692 print_string(PRINT_FP, NULL, "%s", suffix);
693 }
694
695 int get_linklayer(unsigned int *val, const char *arg)
696 {
697 int res;
698
699 if (matches(arg, "ethernet") == 0)
700 res = LINKLAYER_ETHERNET;
701 else if (matches(arg, "atm") == 0)
702 res = LINKLAYER_ATM;
703 else if (matches(arg, "adsl") == 0)
704 res = LINKLAYER_ATM;
705 else
706 return -1; /* Indicate error */
707
708 *val = res;
709 return 0;
710 }
711
712 void print_linklayer(char *buf, int len, unsigned int linklayer)
713 {
714 switch (linklayer) {
715 case LINKLAYER_UNSPEC:
716 snprintf(buf, len, "%s", "unspec");
717 return;
718 case LINKLAYER_ETHERNET:
719 snprintf(buf, len, "%s", "ethernet");
720 return;
721 case LINKLAYER_ATM:
722 snprintf(buf, len, "%s", "atm");
723 return;
724 default:
725 snprintf(buf, len, "%s", "unknown");
726 return;
727 }
728 }
729
730 char *sprint_linklayer(unsigned int linklayer, char *buf)
731 {
732 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
733 return buf;
734 }
735
736 void print_tm(FILE *f, const struct tcf_t *tm)
737 {
738 int hz = get_user_hz();
739
740 if (tm->install != 0) {
741 print_uint(PRINT_JSON, "installed", NULL, tm->install);
742 print_uint(PRINT_FP, NULL, " installed %u sec",
743 (unsigned int)(tm->install/hz));
744 }
745 if (tm->lastuse != 0) {
746 print_uint(PRINT_JSON, "last_used", NULL, tm->lastuse);
747 print_uint(PRINT_FP, NULL, " used %u sec",
748 (unsigned int)(tm->lastuse/hz));
749 }
750 if (tm->expires != 0) {
751 print_uint(PRINT_JSON, "expires", NULL, tm->expires);
752 print_uint(PRINT_FP, NULL, " expires %u sec",
753 (unsigned int)(tm->expires/hz));
754 }
755 }
756
757 static void print_tcstats_basic_hw(struct rtattr **tbs, char *prefix)
758 {
759 struct gnet_stats_basic bs_hw;
760
761 if (!tbs[TCA_STATS_BASIC_HW])
762 return;
763
764 memcpy(&bs_hw, RTA_DATA(tbs[TCA_STATS_BASIC_HW]),
765 MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC_HW]), sizeof(bs_hw)));
766
767 if (bs_hw.bytes == 0 && bs_hw.packets == 0)
768 return;
769
770 if (tbs[TCA_STATS_BASIC]) {
771 struct gnet_stats_basic bs;
772
773 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]),
774 MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]),
775 sizeof(bs)));
776
777 if (bs.bytes >= bs_hw.bytes && bs.packets >= bs_hw.packets) {
778 print_string(PRINT_FP, NULL, "%s", _SL_);
779 print_string(PRINT_FP, NULL, "%s", prefix);
780 print_lluint(PRINT_ANY, "sw_bytes",
781 "Sent software %llu bytes",
782 bs.bytes - bs_hw.bytes);
783 print_uint(PRINT_ANY, "sw_packets", " %u pkt",
784 bs.packets - bs_hw.packets);
785 }
786 }
787
788 print_string(PRINT_FP, NULL, "%s", _SL_);
789 print_string(PRINT_FP, NULL, "%s", prefix);
790 print_lluint(PRINT_ANY, "hw_bytes", "Sent hardware %llu bytes",
791 bs_hw.bytes);
792 print_uint(PRINT_ANY, "hw_packets", " %u pkt", bs_hw.packets);
793 }
794
795 void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
796 {
797 SPRINT_BUF(b1);
798 struct rtattr *tbs[TCA_STATS_MAX + 1];
799
800 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
801
802 if (tbs[TCA_STATS_BASIC]) {
803 struct gnet_stats_basic bs = {0};
804
805 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
806 print_string(PRINT_FP, NULL, "%s", prefix);
807 print_lluint(PRINT_ANY, "bytes", "Sent %llu bytes", bs.bytes);
808 print_uint(PRINT_ANY, "packets", " %u pkt", bs.packets);
809 }
810
811 if (tbs[TCA_STATS_QUEUE]) {
812 struct gnet_stats_queue q = {0};
813
814 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
815 print_uint(PRINT_ANY, "drops", " (dropped %u", q.drops);
816 print_uint(PRINT_ANY, "overlimits", ", overlimits %u",
817 q.overlimits);
818 print_uint(PRINT_ANY, "requeues", " requeues %u) ", q.requeues);
819 }
820
821 if (tbs[TCA_STATS_BASIC_HW])
822 print_tcstats_basic_hw(tbs, prefix);
823
824 if (tbs[TCA_STATS_RATE_EST64]) {
825 struct gnet_stats_rate_est64 re = {0};
826
827 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]),
828 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]),
829 sizeof(re)));
830 print_string(PRINT_FP, NULL, "\n%s", prefix);
831 print_lluint(PRINT_JSON, "rate", NULL, re.bps);
832 print_string(PRINT_FP, NULL, "rate %s",
833 sprint_rate(re.bps, b1));
834 print_lluint(PRINT_ANY, "pps", " %llupps", re.pps);
835 } else if (tbs[TCA_STATS_RATE_EST]) {
836 struct gnet_stats_rate_est re = {0};
837
838 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]),
839 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
840 print_string(PRINT_FP, NULL, "\n%s", prefix);
841 print_uint(PRINT_JSON, "rate", NULL, re.bps);
842 print_string(PRINT_FP, NULL, "rate %s",
843 sprint_rate(re.bps, b1));
844 print_uint(PRINT_ANY, "pps", " %upps", re.pps);
845 }
846
847 if (tbs[TCA_STATS_QUEUE]) {
848 struct gnet_stats_queue q = {0};
849
850 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
851 if (!tbs[TCA_STATS_RATE_EST])
852 print_string(PRINT_FP, NULL, "\n%s", prefix);
853 print_uint(PRINT_JSON, "backlog", NULL, q.backlog);
854 print_string(PRINT_FP, NULL, "backlog %s",
855 sprint_size(q.backlog, b1));
856 print_uint(PRINT_ANY, "qlen", " %up", q.qlen);
857 print_uint(PRINT_FP, NULL, " requeues %u", q.requeues);
858 }
859
860 if (xstats)
861 *xstats = tbs[TCA_STATS_APP] ? : NULL;
862 }
863
864 void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
865 {
866 SPRINT_BUF(b1);
867
868 if (tb[TCA_STATS2]) {
869 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
870 if (xstats && NULL == *xstats)
871 goto compat_xstats;
872 return;
873 }
874 /* backward compatibility */
875 if (tb[TCA_STATS]) {
876 struct tc_stats st = {};
877
878 /* handle case where kernel returns more/less than we know about */
879 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
880
881 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
882 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
883 st.overlimits);
884
885 if (st.bps || st.pps || st.qlen || st.backlog) {
886 fprintf(fp, "\n%s", prefix);
887 if (st.bps || st.pps) {
888 fprintf(fp, "rate ");
889 if (st.bps)
890 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
891 if (st.pps)
892 fprintf(fp, "%upps ", st.pps);
893 }
894 if (st.qlen || st.backlog) {
895 fprintf(fp, "backlog ");
896 if (st.backlog)
897 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
898 if (st.qlen)
899 fprintf(fp, "%up ", st.qlen);
900 }
901 }
902 }
903
904 compat_xstats:
905 if (tb[TCA_XSTATS] && xstats)
906 *xstats = tb[TCA_XSTATS];
907 }