tc_util.c revision fd784ccaf649128965be016f857708669e798479
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 <netinet/in.h> 20#include <arpa/inet.h> 21#include <string.h> 22#include <math.h> 23 24#include "utils.h" 25#include "tc_util.h" 26 27int get_qdisc_handle(__u32 *h, const char *str) 28{ 29 __u32 maj; 30 char *p; 31 32 maj = TC_H_UNSPEC; 33 if (strcmp(str, "none") == 0) 34 goto ok; 35 maj = strtoul(str, &p, 16); 36 if (p == str) 37 return -1; 38 maj <<= 16; 39 if (*p != ':' && *p!=0) 40 return -1; 41ok: 42 *h = maj; 43 return 0; 44} 45 46int get_tc_classid(__u32 *h, const char *str) 47{ 48 __u32 maj, min; 49 char *p; 50 51 maj = TC_H_ROOT; 52 if (strcmp(str, "root") == 0) 53 goto ok; 54 maj = TC_H_UNSPEC; 55 if (strcmp(str, "none") == 0) 56 goto ok; 57 maj = strtoul(str, &p, 16); 58 if (p == str) { 59 maj = 0; 60 if (*p != ':') 61 return -1; 62 } 63 if (*p == ':') { 64 if (maj >= (1<<16)) 65 return -1; 66 maj <<= 16; 67 str = p+1; 68 min = strtoul(str, &p, 16); 69 if (*p != 0) 70 return -1; 71 if (min >= (1<<16)) 72 return -1; 73 maj |= min; 74 } else if (*p != 0) 75 return -1; 76 77ok: 78 *h = maj; 79 return 0; 80} 81 82int print_tc_classid(char *buf, int len, __u32 h) 83{ 84 if (h == TC_H_ROOT) 85 sprintf(buf, "root"); 86 else if (h == TC_H_UNSPEC) 87 snprintf(buf, len, "none"); 88 else if (TC_H_MAJ(h) == 0) 89 snprintf(buf, len, ":%x", TC_H_MIN(h)); 90 else if (TC_H_MIN(h) == 0) 91 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); 92 else 93 snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h)); 94 return 0; 95} 96 97char * sprint_tc_classid(__u32 h, char *buf) 98{ 99 if (print_tc_classid(buf, SPRINT_BSIZE-1, h)) 100 strcpy(buf, "???"); 101 return buf; 102} 103 104/* See http://physics.nist.gov/cuu/Units/binary.html */ 105static const struct rate_suffix { 106 const char *name; 107 double scale; 108} suffixes[] = { 109 { "bit", 1. }, 110 { "Kibit", 1024. }, 111 { "kbit", 1000. }, 112 { "mibit", 1024.*1024. }, 113 { "mbit", 1000000. }, 114 { "gibit", 1024.*1024.*1024. }, 115 { "gbit", 1000000000. }, 116 { "tibit", 1024.*1024.*1024.*1024. }, 117 { "tbit", 1000000000000. }, 118 { "Bps", 8. }, 119 { "KiBps", 8.*1024. }, 120 { "KBps", 8000. }, 121 { "MiBps", 8.*1024*1024. }, 122 { "MBps", 8000000. }, 123 { "GiBps", 8.*1024.*1024.*1024. }, 124 { "GBps", 8000000000. }, 125 { "TiBps", 8.*1024.*1024.*1024.*1024. }, 126 { "TBps", 8000000000000. }, 127 { NULL } 128}; 129 130 131int get_rate(unsigned *rate, const char *str) 132{ 133 char *p; 134 double bps = strtod(str, &p); 135 const struct rate_suffix *s; 136 137 if (p == str) 138 return -1; 139 140 if (*p == '\0') { 141 *rate = bps / 8.; /* assume bytes/sec */ 142 return 0; 143 } 144 145 for (s = suffixes; s->name; ++s) { 146 if (strcasecmp(s->name, p) == 0) { 147 *rate = (bps * s->scale) / 8.; 148 return 0; 149 } 150 } 151 152 return -1; 153} 154 155int get_rate_and_cell(unsigned *rate, int *cell_log, char *str) 156{ 157 char * slash = strchr(str, '/'); 158 159 if (slash) 160 *slash = 0; 161 162 if (get_rate(rate, str)) 163 return -1; 164 165 if (slash) { 166 int cell; 167 int i; 168 169 if (get_integer(&cell, slash+1, 0)) 170 return -1; 171 *slash = '/'; 172 173 for (i=0; i<32; i++) { 174 if ((1<<i) == cell) { 175 *cell_log = i; 176 return 0; 177 } 178 } 179 return -1; 180 } 181 return 0; 182} 183 184void print_rate(char *buf, int len, __u32 rate) 185{ 186 double tmp = (double)rate*8; 187 extern int use_iec; 188 189 if (use_iec) { 190 if (tmp >= 1000.0*1024.0*1024.0) 191 snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0); 192 else if (tmp >= 1000.0*1024) 193 snprintf(buf, len, "%.0fKibit", tmp/1024); 194 else 195 snprintf(buf, len, "%.0fbit", tmp); 196 } else { 197 if (tmp >= 1000.0*1000000.0) 198 snprintf(buf, len, "%.0fMbit", tmp/1000000.0); 199 else if (tmp >= 1000.0 * 1000.0) 200 snprintf(buf, len, "%.0fKbit", tmp/1000.0); 201 else 202 snprintf(buf, len, "%.0fbit", tmp); 203 } 204} 205 206char * sprint_rate(__u32 rate, char *buf) 207{ 208 print_rate(buf, SPRINT_BSIZE-1, rate); 209 return buf; 210} 211 212int get_time(unsigned *time, const char *str) 213{ 214 double t; 215 char *p; 216 217 t = strtod(str, &p); 218 if (p == str) 219 return -1; 220 221 if (*p) { 222 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 || 223 strcasecmp(p, "secs")==0) 224 t *= TIME_UNITS_PER_SEC; 225 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 || 226 strcasecmp(p, "msecs") == 0) 227 t *= TIME_UNITS_PER_SEC/1000; 228 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 || 229 strcasecmp(p, "usecs") == 0) 230 t *= TIME_UNITS_PER_SEC/1000000; 231 else if (strcasecmp(p, "ns") == 0 || strcasecmp(p, "nsec")==0 || 232 strcasecmp(p, "nsecs") == 0) 233 t *= TIME_UNITS_PER_SEC/1000000000; 234 else 235 return -1; 236 } 237 238 *time = t; 239 return 0; 240} 241 242 243void print_time(char *buf, int len, __u32 time) 244{ 245 double tmp = time; 246 247 if (tmp >= TIME_UNITS_PER_SEC) 248 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); 249 else if (tmp >= TIME_UNITS_PER_SEC/1000) 250 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); 251 else if (tmp >= TIME_UNITS_PER_SEC/1000000) 252 snprintf(buf, len, "%.1fus", tmp/(TIME_UNITS_PER_SEC/1000000)); 253 else 254 snprintf(buf, len, "%uns", time); 255} 256 257char * sprint_time(__u32 time, char *buf) 258{ 259 print_time(buf, SPRINT_BSIZE-1, time); 260 return buf; 261} 262 263char * sprint_ticks(__u32 ticks, char *buf) 264{ 265 return sprint_time(tc_core_tick2time(ticks), buf); 266} 267 268int get_size(unsigned *size, const char *str) 269{ 270 double sz; 271 char *p; 272 273 sz = strtod(str, &p); 274 if (p == str) 275 return -1; 276 277 if (*p) { 278 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0) 279 sz *= 1024; 280 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0) 281 sz *= 1024*1024*1024; 282 else if (strcasecmp(p, "gbit") == 0) 283 sz *= 1024*1024*1024/8; 284 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0) 285 sz *= 1024*1024; 286 else if (strcasecmp(p, "mbit") == 0) 287 sz *= 1024*1024/8; 288 else if (strcasecmp(p, "kbit") == 0) 289 sz *= 1024/8; 290 else if (strcasecmp(p, "b") != 0) 291 return -1; 292 } 293 294 *size = sz; 295 return 0; 296} 297 298int get_size_and_cell(unsigned *size, int *cell_log, char *str) 299{ 300 char * slash = strchr(str, '/'); 301 302 if (slash) 303 *slash = 0; 304 305 if (get_size(size, str)) 306 return -1; 307 308 if (slash) { 309 int cell; 310 int i; 311 312 if (get_integer(&cell, slash+1, 0)) 313 return -1; 314 *slash = '/'; 315 316 for (i=0; i<32; i++) { 317 if ((1<<i) == cell) { 318 *cell_log = i; 319 return 0; 320 } 321 } 322 return -1; 323 } 324 return 0; 325} 326 327void print_size(char *buf, int len, __u32 sz) 328{ 329 double tmp = sz; 330 331 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024) 332 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024))); 333 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16) 334 snprintf(buf, len, "%gKb", rint(tmp/1024)); 335 else 336 snprintf(buf, len, "%ub", sz); 337} 338 339char * sprint_size(__u32 size, char *buf) 340{ 341 print_size(buf, SPRINT_BSIZE-1, size); 342 return buf; 343} 344 345static const double max_percent_value = 0xffffffff; 346 347int get_percent(__u32 *percent, const char *str) 348{ 349 char *p; 350 double per = strtod(str, &p) / 100.; 351 352 if (per > 1. || per < 0) 353 return -1; 354 if (*p && strcmp(p, "%")) 355 return -1; 356 357 *percent = (unsigned) rint(per * max_percent_value); 358 return 0; 359} 360 361void print_percent(char *buf, int len, __u32 per) 362{ 363 snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value); 364} 365 366char * sprint_percent(__u32 per, char *buf) 367{ 368 print_percent(buf, SPRINT_BSIZE-1, per); 369 return buf; 370} 371 372void print_qdisc_handle(char *buf, int len, __u32 h) 373{ 374 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16); 375} 376 377char * sprint_qdisc_handle(__u32 h, char *buf) 378{ 379 print_qdisc_handle(buf, SPRINT_BSIZE-1, h); 380 return buf; 381} 382 383char * action_n2a(int action, char *buf, int len) 384{ 385 switch (action) { 386 case -1: 387 return "continue"; 388 break; 389 case TC_ACT_OK: 390 return "pass"; 391 break; 392 case TC_ACT_SHOT: 393 return "drop"; 394 break; 395 case TC_ACT_RECLASSIFY: 396 return "reclassify"; 397 case TC_ACT_PIPE: 398 return "pipe"; 399 case TC_ACT_STOLEN: 400 return "stolen"; 401 default: 402 snprintf(buf, len, "%d", action); 403 return buf; 404 } 405} 406 407int action_a2n(char *arg, int *result) 408{ 409 int res; 410 411 if (matches(arg, "continue") == 0) 412 res = -1; 413 else if (matches(arg, "drop") == 0) 414 res = TC_ACT_SHOT; 415 else if (matches(arg, "shot") == 0) 416 res = TC_ACT_SHOT; 417 else if (matches(arg, "pass") == 0) 418 res = TC_ACT_OK; 419 else if (strcmp(arg, "ok") == 0) 420 res = TC_ACT_OK; 421 else if (matches(arg, "reclassify") == 0) 422 res = TC_ACT_RECLASSIFY; 423 else { 424 char dummy; 425 if (sscanf(arg, "%d%c", &res, &dummy) != 1) 426 return -1; 427 } 428 *result = res; 429 return 0; 430} 431 432void print_tm(FILE * f, const struct tcf_t *tm) 433{ 434 int hz = get_user_hz(); 435 if (tm->install != 0) 436 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz)); 437 if (tm->lastuse != 0) 438 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz)); 439 if (tm->expires != 0) 440 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz)); 441} 442 443void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats) 444{ 445 SPRINT_BUF(b1); 446 struct rtattr *tbs[TCA_STATS_MAX + 1]; 447 448 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta); 449 450 if (tbs[TCA_STATS_BASIC]) { 451 struct gnet_stats_basic bs = {0}; 452 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs))); 453 fprintf(fp, "%sSent %llu bytes %u pkt", 454 prefix, (unsigned long long) bs.bytes, bs.packets); 455 } 456 457 if (tbs[TCA_STATS_QUEUE]) { 458 struct gnet_stats_queue q = {0}; 459 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); 460 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ", 461 q.drops, q.overlimits, q.requeues); 462 } 463 464 if (tbs[TCA_STATS_RATE_EST]) { 465 struct gnet_stats_rate_est re = {0}; 466 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re))); 467 fprintf(fp, "\n%srate %s %upps ", 468 prefix, sprint_rate(re.bps, b1), re.pps); 469 } 470 471 if (tbs[TCA_STATS_QUEUE]) { 472 struct gnet_stats_queue q = {0}; 473 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q))); 474 if (!tbs[TCA_STATS_RATE_EST]) 475 fprintf(fp, "\n%s", prefix); 476 fprintf(fp, "backlog %s %up requeues %u ", 477 sprint_size(q.backlog, b1), q.qlen, q.requeues); 478 } 479 480 if (xstats) 481 *xstats = tbs[TCA_STATS_APP] ? : NULL; 482} 483 484void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats) 485{ 486 SPRINT_BUF(b1); 487 488 if (tb[TCA_STATS2]) { 489 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats); 490 if (xstats && NULL == *xstats) 491 goto compat_xstats; 492 return; 493 } 494 /* backward compatibility */ 495 if (tb[TCA_STATS]) { 496 struct tc_stats st; 497 498 /* handle case where kernel returns more/less than we know about */ 499 memset(&st, 0, sizeof(st)); 500 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st))); 501 502 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ", 503 prefix, (unsigned long long)st.bytes, st.packets, st.drops, 504 st.overlimits); 505 506 if (st.bps || st.pps || st.qlen || st.backlog) { 507 fprintf(fp, "\n%s", prefix); 508 if (st.bps || st.pps) { 509 fprintf(fp, "rate "); 510 if (st.bps) 511 fprintf(fp, "%s ", sprint_rate(st.bps, b1)); 512 if (st.pps) 513 fprintf(fp, "%upps ", st.pps); 514 } 515 if (st.qlen || st.backlog) { 516 fprintf(fp, "backlog "); 517 if (st.backlog) 518 fprintf(fp, "%s ", sprint_size(st.backlog, b1)); 519 if (st.qlen) 520 fprintf(fp, "%up ", st.qlen); 521 } 522 } 523 } 524 525compat_xstats: 526 if (tb[TCA_XSTATS] && xstats) 527 *xstats = tb[TCA_XSTATS]; 528} 529 530