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