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 <syslog.h> 17#include <fcntl.h> 18#include <sys/socket.h> 19#include <sys/param.h> 20#include <netinet/in.h> 21#include <arpa/inet.h> 22#include <string.h> 23#include <math.h> 24#include <errno.h> 25 26#include "utils.h" 27#include "names.h" 28#include "tc_util.h" 29#include "tc_common.h" 30 31#ifndef LIBDIR 32#define LIBDIR "/usr/lib" 33#endif 34 35static struct db_names *cls_names; 36 37#define NAMES_DB "/etc/iproute2/tc_cls" 38 39int cls_names_init(char *path) 40{ 41 int ret; 42 43 cls_names = db_names_alloc(); 44 if (!cls_names) 45 return -1; 46 47 ret = db_names_load(cls_names, path ?: NAMES_DB); 48 if (ret == -ENOENT && path) { 49 fprintf(stderr, "Can't open class names file: %s\n", path); 50 return -1; 51 } 52 if (ret) { 53 db_names_free(cls_names); 54 cls_names = NULL; 55 } 56 57 return 0; 58} 59 60void cls_names_uninit(void) 61{ 62 db_names_free(cls_names); 63} 64 65const char *get_tc_lib(void) 66{ 67 const char *lib_dir; 68 69 lib_dir = getenv("TC_LIB_DIR"); 70 if (!lib_dir) 71 lib_dir = LIBDIR "/tc/"; 72 73 return lib_dir; 74} 75 76int get_qdisc_handle(__u32 *h, const char *str) 77{ 78 __u32 maj; 79 char *p; 80 81 maj = TC_H_UNSPEC; 82 if (strcmp(str, "none") == 0) 83 goto ok; 84 maj = strtoul(str, &p, 16); 85 if (p == str || maj >= (1 << 16)) 86 return -1; 87 maj <<= 16; 88 if (*p != ':' && *p != 0) 89 return -1; 90ok: 91 *h = maj; 92 return 0; 93} 94 95int get_tc_classid(__u32 *h, const char *str) 96{ 97 __u32 maj, min; 98 char *p; 99 100 maj = TC_H_ROOT; 101 if (strcmp(str, "root") == 0) 102 goto ok; 103 maj = TC_H_UNSPEC; 104 if (strcmp(str, "none") == 0) 105 goto ok; 106 maj = strtoul(str, &p, 16); 107 if (p == str) { 108 maj = 0; 109 if (*p != ':') 110 return -1; 111 } 112 if (*p == ':') { 113 if (maj >= (1<<16)) 114 return -1; 115 maj <<= 16; 116 str = p+1; 117 min = strtoul(str, &p, 16); 118 if (*p != 0) 119 return -1; 120 if (min >= (1<<16)) 121 return -1; 122 maj |= min; 123 } else if (*p != 0) 124 return -1; 125 126ok: 127 *h = maj; 128 return 0; 129} 130 131int print_tc_classid(char *buf, int blen, __u32 h) 132{ 133 SPRINT_BUF(handle) = {}; 134 int hlen = SPRINT_BSIZE - 1; 135 136 if (h == TC_H_ROOT) 137 sprintf(handle, "root"); 138 else if (h == TC_H_UNSPEC) 139 snprintf(handle, hlen, "none"); 140 else if (TC_H_MAJ(h) == 0) 141 snprintf(handle, hlen, ":%x", TC_H_MIN(h)); 142 else if (TC_H_MIN(h) == 0) 143 snprintf(handle, hlen, "%x:", TC_H_MAJ(h) >> 16); 144 else 145 snprintf(handle, hlen, "%x:%x", TC_H_MAJ(h) >> 16, TC_H_MIN(h)); 146 147 if (use_names) { 148 char clname[IDNAME_MAX] = {}; 149 150 if (id_to_name(cls_names, h, clname)) 151 snprintf(buf, blen, "%s#%s", clname, handle); 152 else 153 snprintf(buf, blen, "%s", handle); 154 } else { 155 snprintf(buf, blen, "%s", handle); 156 } 157 158 return 0; 159} 160 161char *sprint_tc_classid(__u32 h, char *buf) 162{ 163 if (print_tc_classid(buf, SPRINT_BSIZE-1, h)) 164 strcpy(buf, "???"); 165 return buf; 166} 167 168/* See http://physics.nist.gov/cuu/Units/binary.html */ 169static const struct rate_suffix { 170 const char *name; 171 double scale; 172} suffixes[] = { 173 { "bit", 1. }, 174 { "Kibit", 1024. }, 175 { "kbit", 1000. }, 176 { "mibit", 1024.*1024. }, 177 { "mbit", 1000000. }, 178 { "gibit", 1024.*1024.*1024. }, 179 { "gbit", 1000000000. }, 180 { "tibit", 1024.*1024.*1024.*1024. }, 181 { "tbit", 1000000000000. }, 182 { "Bps", 8. }, 183 { "KiBps", 8.*1024. }, 184 { "KBps", 8000. }, 185 { "MiBps", 8.*1024*1024. }, 186 { "MBps", 8000000. }, 187 { "GiBps", 8.*1024.*1024.*1024. }, 188 { "GBps", 8000000000. }, 189 { "TiBps", 8.*1024.*1024.*1024.*1024. }, 190 { "TBps", 8000000000000. }, 191 { NULL } 192}; 193 194 195int get_rate(unsigned int *rate, const char *str) 196{ 197 char *p; 198 double bps = strtod(str, &p); 199 const struct rate_suffix *s; 200 201 if (p == str) 202 return -1; 203 204 for (s = suffixes; s->name; ++s) { 205 if (strcasecmp(s->name, p) == 0) { 206 bps *= s->scale; 207 p += strlen(p); 208 break; 209 } 210 } 211 212 if (*p) 213 return -1; /* unknown suffix */ 214 215 bps /= 8; /* -> bytes per second */ 216 *rate = bps; 217 /* detect if an overflow happened */ 218 if (*rate != floor(bps)) 219 return -1; 220 return 0; 221} 222 223int get_rate64(__u64 *rate, const char *str) 224{ 225 char *p; 226 double bps = strtod(str, &p); 227 const struct rate_suffix *s; 228 229 if (p == str) 230 return -1; 231 232 for (s = suffixes; s->name; ++s) { 233 if (strcasecmp(s->name, p) == 0) { 234 bps *= s->scale; 235 p += strlen(p); 236 break; 237 } 238 } 239 240 if (*p) 241 return -1; /* unknown suffix */ 242 243 bps /= 8; /* -> bytes per second */ 244 *rate = bps; 245 return 0; 246} 247 248void print_rate(char *buf, int len, __u64 rate) 249{ 250 extern int use_iec; 251 unsigned long kilo = use_iec ? 1024 : 1000; 252 const char *str = use_iec ? "i" : ""; 253 static char *units[5] = {"", "K", "M", "G", "T"}; 254 int i; 255 256 rate <<= 3; /* bytes/sec -> bits/sec */ 257 258 for (i = 0; i < ARRAY_SIZE(units) - 1; i++) { 259 if (rate < kilo) 260 break; 261 if (((rate % kilo) != 0) && rate < 1000*kilo) 262 break; 263 rate /= kilo; 264 } 265 266 snprintf(buf, len, "%.0f%s%sbit", (double)rate, units[i], str); 267} 268 269char *sprint_rate(__u64 rate, char *buf) 270{ 271 print_rate(buf, SPRINT_BSIZE-1, rate); 272 return buf; 273} 274 275int get_time(unsigned int *time, const char *str) 276{ 277 double t; 278 char *p; 279 280 t = strtod(str, &p); 281 if (p == str) 282 return -1; 283 284 if (*p) { 285 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 || 286 strcasecmp(p, "secs") == 0) 287 t *= TIME_UNITS_PER_SEC; 288 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec") == 0 || 289 strcasecmp(p, "msecs") == 0) 290 t *= TIME_UNITS_PER_SEC/1000; 291 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec") == 0 || 292 strcasecmp(p, "usecs") == 0) 293 t *= TIME_UNITS_PER_SEC/1000000; 294 else 295 return -1; 296 } 297 298 *time = t; 299 return 0; 300} 301 302 303void print_time(char *buf, int len, __u32 time) 304{ 305 double tmp = time; 306 307 if (tmp >= TIME_UNITS_PER_SEC) 308 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); 309 else if (tmp >= TIME_UNITS_PER_SEC/1000) 310 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); 311 else 312 snprintf(buf, len, "%uus", time); 313} 314 315char *sprint_time(__u32 time, char *buf) 316{ 317 print_time(buf, SPRINT_BSIZE-1, time); 318 return buf; 319} 320 321char *sprint_ticks(__u32 ticks, char *buf) 322{ 323 return sprint_time(tc_core_tick2time(ticks), buf); 324} 325 326int get_size(unsigned int *size, const char *str) 327{ 328 double sz; 329 char *p; 330 331 sz = strtod(str, &p); 332 if (p == str) 333 return -1; 334 335 if (*p) { 336 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k") == 0) 337 sz *= 1024; 338 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g") == 0) 339 sz *= 1024*1024*1024; 340 else if (strcasecmp(p, "gbit") == 0) 341 sz *= 1024*1024*1024/8; 342 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m") == 0) 343 sz *= 1024*1024; 344 else if (strcasecmp(p, "mbit") == 0) 345 sz *= 1024*1024/8; 346 else if (strcasecmp(p, "kbit") == 0) 347 sz *= 1024/8; 348 else if (strcasecmp(p, "b") != 0) 349 return -1; 350 } 351 352 *size = sz; 353 return 0; 354} 355 356int get_size_and_cell(unsigned int *size, int *cell_log, char *str) 357{ 358 char *slash = strchr(str, '/'); 359 360 if (slash) 361 *slash = 0; 362 363 if (get_size(size, str)) 364 return -1; 365 366 if (slash) { 367 int cell; 368 int i; 369 370 if (get_integer(&cell, slash+1, 0)) 371 return -1; 372 *slash = '/'; 373 374 for (i = 0; i < 32; i++) { 375 if ((1<<i) == cell) { 376 *cell_log = i; 377 return 0; 378 } 379 } 380 return -1; 381 } 382 return 0; 383} 384 385void print_size(char *buf, int len, __u32 sz) 386{ 387 double tmp = sz; 388 389 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) 390 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); 391 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) 392 snprintf(buf, len, "%gKb", rint(tmp/1024)); 393 else 394 snprintf(buf, len, "%ub", sz); 395} 396 397char *sprint_size(__u32 size, char *buf) 398{ 399 print_size(buf, SPRINT_BSIZE-1, size); 400 return buf; 401} 402 403void print_qdisc_handle(char *buf, int len, __u32 h) 404{ 405 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); 406} 407 408char *sprint_qdisc_handle(__u32 h, char *buf) 409{ 410 print_qdisc_handle(buf, SPRINT_BSIZE-1, h); 411 return buf; 412} 413 414static const char *action_n2a(int action) 415{ 416 static char buf[64]; 417 418 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) 419 return "goto"; 420 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP)) 421 return "jump"; 422 switch (action) { 423 case TC_ACT_UNSPEC: 424 return "continue"; 425 case TC_ACT_OK: 426 return "pass"; 427 case TC_ACT_SHOT: 428 return "drop"; 429 case TC_ACT_RECLASSIFY: 430 return "reclassify"; 431 case TC_ACT_PIPE: 432 return "pipe"; 433 case TC_ACT_STOLEN: 434 return "stolen"; 435 case TC_ACT_TRAP: 436 return "trap"; 437 default: 438 snprintf(buf, 64, "%d", action); 439 return buf; 440 } 441} 442 443/* Convert action branch name into numeric format. 444 * 445 * Parameters: 446 * @arg - string to parse 447 * @result - pointer to output variable 448 * @allow_num - whether @arg may be in numeric format already 449 * 450 * In error case, returns -1 and does not touch @result. Otherwise returns 0. 451 */ 452static int action_a2n(char *arg, int *result, bool allow_num) 453{ 454 int n; 455 char dummy; 456 struct { 457 const char *a; 458 int n; 459 } a2n[] = { 460 {"continue", TC_ACT_UNSPEC}, 461 {"drop", TC_ACT_SHOT}, 462 {"shot", TC_ACT_SHOT}, 463 {"pass", TC_ACT_OK}, 464 {"ok", TC_ACT_OK}, 465 {"reclassify", TC_ACT_RECLASSIFY}, 466 {"pipe", TC_ACT_PIPE}, 467 {"goto", TC_ACT_GOTO_CHAIN}, 468 {"jump", TC_ACT_JUMP}, 469 {"trap", TC_ACT_TRAP}, 470 { NULL }, 471 }, *iter; 472 473 for (iter = a2n; iter->a; iter++) { 474 if (matches(arg, iter->a) != 0) 475 continue; 476 *result = iter->n; 477 return 0; 478 } 479 if (!allow_num || sscanf(arg, "%d%c", &n, &dummy) != 1) 480 return -1; 481 482 *result = n; 483 return 0; 484} 485 486static int __parse_action_control(int *argc_p, char ***argv_p, int *result_p, 487 bool allow_num, bool ignore_a2n_miss) 488{ 489 int argc = *argc_p; 490 char **argv = *argv_p; 491 int result; 492 493 if (!argc) 494 return -1; 495 if (action_a2n(*argv, &result, allow_num) == -1) { 496 if (!ignore_a2n_miss) 497 fprintf(stderr, "Bad action type %s\n", *argv); 498 return -1; 499 } 500 if (result == TC_ACT_GOTO_CHAIN) { 501 __u32 chain_index; 502 503 NEXT_ARG(); 504 if (matches(*argv, "chain") != 0) { 505 fprintf(stderr, "\"chain index\" expected\n"); 506 return -1; 507 } 508 NEXT_ARG(); 509 if (get_u32(&chain_index, *argv, 10) || 510 chain_index > TC_ACT_EXT_VAL_MASK) { 511 fprintf(stderr, "Illegal \"chain index\"\n"); 512 return -1; 513 } 514 result |= chain_index; 515 } 516 if (result == TC_ACT_JUMP) { 517 __u32 jump_cnt = 0; 518 519 NEXT_ARG(); 520 if (get_u32(&jump_cnt, *argv, 10) || 521 jump_cnt > TC_ACT_EXT_VAL_MASK) { 522 fprintf(stderr, "Invalid \"jump count\" (%s)\n", *argv); 523 return -1; 524 } 525 result |= jump_cnt; 526 } 527 NEXT_ARG_FWD(); 528 *argc_p = argc; 529 *argv_p = argv; 530 *result_p = result; 531 return 0; 532} 533 534/* Parse action control including possible options. 535 * 536 * Parameters: 537 * @argc_p - pointer to argc to parse 538 * @argv_p - pointer to argv to parse 539 * @result_p - pointer to output variable 540 * @allow_num - whether action may be in numeric format already 541 * 542 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0. 543 */ 544int parse_action_control(int *argc_p, char ***argv_p, 545 int *result_p, bool allow_num) 546{ 547 return __parse_action_control(argc_p, argv_p, result_p, 548 allow_num, false); 549} 550 551/* Parse action control including possible options. 552 * 553 * Parameters: 554 * @argc_p - pointer to argc to parse 555 * @argv_p - pointer to argv to parse 556 * @result_p - pointer to output variable 557 * @allow_num - whether action may be in numeric format already 558 * @default_result - set as a result in case of parsing error 559 * 560 * In case there is an error during parsing, the default result is used. 561 */ 562void parse_action_control_dflt(int *argc_p, char ***argv_p, 563 int *result_p, bool allow_num, 564 int default_result) 565{ 566 if (__parse_action_control(argc_p, argv_p, result_p, allow_num, true)) 567 *result_p = default_result; 568} 569 570static int parse_action_control_slash_spaces(int *argc_p, char ***argv_p, 571 int *result1_p, int *result2_p, 572 bool allow_num) 573{ 574 int argc = *argc_p; 575 char **argv = *argv_p; 576 int result1, result2; 577 int *result_p = &result1; 578 int ok = 0; 579 int ret; 580 581 while (argc > 0) { 582 switch (ok) { 583 case 1: 584 if (strcmp(*argv, "/") != 0) 585 goto out; 586 result_p = &result2; 587 NEXT_ARG(); 588 /* fall-through */ 589 case 0: /* fall-through */ 590 case 2: 591 ret = parse_action_control(&argc, &argv, 592 result_p, allow_num); 593 if (ret) 594 return ret; 595 ok++; 596 break; 597 default: 598 goto out; 599 } 600 } 601out: 602 *result1_p = result1; 603 if (ok == 2) 604 *result2_p = result2; 605 *argc_p = argc; 606 *argv_p = argv; 607 return 0; 608} 609 610/* Parse action control with slash including possible options. 611 * 612 * Parameters: 613 * @argc_p - pointer to argc to parse 614 * @argv_p - pointer to argv to parse 615 * @result1_p - pointer to the first (before slash) output variable 616 * @result2_p - pointer to the second (after slash) output variable 617 * @allow_num - whether action may be in numeric format already 618 * 619 * In error case, returns -1 and does not touch @result*. Otherwise returns 0. 620 */ 621int parse_action_control_slash(int *argc_p, char ***argv_p, 622 int *result1_p, int *result2_p, bool allow_num) 623{ 624 char **argv = *argv_p; 625 int result1, result2; 626 char *p = strchr(*argv, '/'); 627 628 if (!p) 629 return parse_action_control_slash_spaces(argc_p, argv_p, 630 result1_p, result2_p, 631 allow_num); 632 *p = 0; 633 if (action_a2n(*argv, &result1, allow_num)) { 634 if (p) 635 *p = '/'; 636 return -1; 637 } 638 639 *p = '/'; 640 if (action_a2n(p + 1, &result2, allow_num)) 641 return -1; 642 643 *result1_p = result1; 644 *result2_p = result2; 645 return 0; 646} 647 648void print_action_control(FILE *f, const char *prefix, 649 int action, const char *suffix) 650{ 651 fprintf(f, "%s%s", prefix, action_n2a(action)); 652 if (TC_ACT_EXT_CMP(action, TC_ACT_GOTO_CHAIN)) 653 fprintf(f, " chain %u", action & TC_ACT_EXT_VAL_MASK); 654 if (TC_ACT_EXT_CMP(action, TC_ACT_JUMP)) 655 fprintf(f, " %u", action & TC_ACT_EXT_VAL_MASK); 656 fprintf(f, "%s", suffix); 657} 658 659int get_linklayer(unsigned int *val, const char *arg) 660{ 661 int res; 662 663 if (matches(arg, "ethernet") == 0) 664 res = LINKLAYER_ETHERNET; 665 else if (matches(arg, "atm") == 0) 666 res = LINKLAYER_ATM; 667 else if (matches(arg, "adsl") == 0) 668 res = LINKLAYER_ATM; 669 else 670 return -1; /* Indicate error */ 671 672 *val = res; 673 return 0; 674} 675 676void print_linklayer(char *buf, int len, unsigned int linklayer) 677{ 678 switch (linklayer) { 679 case LINKLAYER_UNSPEC: 680 snprintf(buf, len, "%s", "unspec"); 681 return; 682 case LINKLAYER_ETHERNET: 683 snprintf(buf, len, "%s", "ethernet"); 684 return; 685 case LINKLAYER_ATM: 686 snprintf(buf, len, "%s", "atm"); 687 return; 688 default: 689 snprintf(buf, len, "%s", "unknown"); 690 return; 691 } 692} 693 694char *sprint_linklayer(unsigned int linklayer, char *buf) 695{ 696 print_linklayer(buf, SPRINT_BSIZE-1, linklayer); 697 return buf; 698} 699 700void print_tm(FILE *f, const struct tcf_t *tm) 701{ 702 int hz = get_user_hz(); 703 704 if (tm->install != 0) 705 fprintf(f, " installed %u sec", (unsigned int)(tm->install/hz)); 706 if (tm->lastuse != 0) 707 fprintf(f, " used %u sec", (unsigned int)(tm->lastuse/hz)); 708 if (tm->expires != 0) 709 fprintf(f, " expires %u sec", (unsigned int)(tm->expires/hz)); 710} 711 712void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) 713{ 714 SPRINT_BUF(b1); 715 struct rtattr *tbs[TCA_STATS_MAX + 1]; 716 717 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); 718 719 if (tbs[TCA_STATS_BASIC]) { 720 struct gnet_stats_basic bs = {0}; 721 722 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs))); 723 fprintf(fp, "%sSent %llu bytes %u pkt", 724 prefix, (unsigned long long) bs.bytes, bs.packets); 725 } 726 727 if (tbs[TCA_STATS_QUEUE]) { 728 struct gnet_stats_queue q = {0}; 729 730 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); 731 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ", 732 q.drops, q.overlimits, q.requeues); 733 } 734 735 if (tbs[TCA_STATS_RATE_EST64]) { 736 struct gnet_stats_rate_est64 re = {0}; 737 738 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST64]), 739 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST64]), 740 sizeof(re))); 741 fprintf(fp, "\n%srate %s %llupps ", 742 prefix, sprint_rate(re.bps, b1), re.pps); 743 } else if (tbs[TCA_STATS_RATE_EST]) { 744 struct gnet_stats_rate_est re = {0}; 745 746 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), 747 MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re))); 748 fprintf(fp, "\n%srate %s %upps ", 749 prefix, sprint_rate(re.bps, b1), re.pps); 750 } 751 752 if (tbs[TCA_STATS_QUEUE]) { 753 struct gnet_stats_queue q = {0}; 754 755 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); 756 if (!tbs[TCA_STATS_RATE_EST]) 757 fprintf(fp, "\n%s", prefix); 758 fprintf(fp, "backlog %s %up requeues %u ", 759 sprint_size(q.backlog, b1), q.qlen, q.requeues); 760 } 761 762 if (xstats) 763 *xstats = tbs[TCA_STATS_APP] ? : NULL; 764} 765 766void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats) 767{ 768 SPRINT_BUF(b1); 769 770 if (tb[TCA_STATS2]) { 771 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); 772 if (xstats && NULL == *xstats) 773 goto compat_xstats; 774 return; 775 } 776 /* backward compatibility */ 777 if (tb[TCA_STATS]) { 778 struct tc_stats st = {}; 779 780 /* handle case where kernel returns more/less than we know about */ 781 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st))); 782 783 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ", 784 prefix, (unsigned long long)st.bytes, st.packets, st.drops, 785 st.overlimits); 786 787 if (st.bps || st.pps || st.qlen || st.backlog) { 788 fprintf(fp, "\n%s", prefix); 789 if (st.bps || st.pps) { 790 fprintf(fp, "rate "); 791 if (st.bps) 792 fprintf(fp, "%s ", sprint_rate(st.bps, b1)); 793 if (st.pps) 794 fprintf(fp, "%upps ", st.pps); 795 } 796 if (st.qlen || st.backlog) { 797 fprintf(fp, "backlog "); 798 if (st.backlog) 799 fprintf(fp, "%s ", sprint_size(st.backlog, b1)); 800 if (st.qlen) 801 fprintf(fp, "%up ", st.qlen); 802 } 803 } 804 } 805 806compat_xstats: 807 if (tb[TCA_XSTATS] && xstats) 808 *xstats = tb[TCA_XSTATS]; 809} 810