2 * tc_util.c Misc TC utility functions.
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
28 #define LIBDIR "/usr/lib/"
31 const char *get_tc_lib(void)
35 lib_dir = getenv("TC_LIB_DIR");
37 lib_dir = LIBDIR "/tc/";
42 int get_qdisc_handle(__u32 *h, const char *str)
48 if (strcmp(str, "none") == 0)
50 maj = strtoul(str, &p, 16);
54 if (*p != ':' && *p!=0)
61 int get_tc_classid(__u32 *h, const char *str)
67 if (strcmp(str, "root") == 0)
70 if (strcmp(str, "none") == 0)
72 maj = strtoul(str, &p, 16);
83 min = strtoul(str, &p, 16);
97 int print_tc_classid(char *buf, int len, __u32 h)
100 sprintf(buf, "root");
101 else if (h == TC_H_UNSPEC)
102 snprintf(buf, len, "none");
103 else if (TC_H_MAJ(h) == 0)
104 snprintf(buf, len, ":%x", TC_H_MIN(h));
105 else if (TC_H_MIN(h) == 0)
106 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
108 snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h));
112 char * sprint_tc_classid(__u32 h, char *buf)
114 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
119 /* See http://physics.nist.gov/cuu/Units/binary.html */
120 static const struct rate_suffix {
127 { "mibit", 1024.*1024. },
128 { "mbit", 1000000. },
129 { "gibit", 1024.*1024.*1024. },
130 { "gbit", 1000000000. },
131 { "tibit", 1024.*1024.*1024.*1024. },
132 { "tbit", 1000000000000. },
134 { "KiBps", 8.*1024. },
136 { "MiBps", 8.*1024*1024. },
137 { "MBps", 8000000. },
138 { "GiBps", 8.*1024.*1024.*1024. },
139 { "GBps", 8000000000. },
140 { "TiBps", 8.*1024.*1024.*1024.*1024. },
141 { "TBps", 8000000000000. },
146 int get_rate(unsigned *rate, const char *str)
149 double bps = strtod(str, &p);
150 const struct rate_suffix *s;
156 *rate = bps / 8.; /* assume bytes/sec */
160 for (s = suffixes; s->name; ++s) {
161 if (strcasecmp(s->name, p) == 0) {
162 *rate = (bps * s->scale) / 8.;
170 int get_rate_and_cell(unsigned *rate, int *cell_log, char *str)
172 char * slash = strchr(str, '/');
177 if (get_rate(rate, str))
184 if (get_integer(&cell, slash+1, 0))
188 for (i=0; i<32; i++) {
189 if ((1<<i) == cell) {
199 void print_rate(char *buf, int len, __u32 rate)
201 double tmp = (double)rate*8;
205 if (tmp >= 1000.0*1024.0*1024.0)
206 snprintf(buf, len, "%.0fMibit", tmp/1024.0*1024.0);
207 else if (tmp >= 1000.0*1024)
208 snprintf(buf, len, "%.0fKibit", tmp/1024);
210 snprintf(buf, len, "%.0fbit", tmp);
212 if (tmp >= 1000.0*1000000.0)
213 snprintf(buf, len, "%.0fMbit", tmp/1000000.0);
214 else if (tmp >= 1000.0 * 1000.0)
215 snprintf(buf, len, "%.0fKbit", tmp/1000.0);
217 snprintf(buf, len, "%.0fbit", tmp);
221 char * sprint_rate(__u32 rate, char *buf)
223 print_rate(buf, SPRINT_BSIZE-1, rate);
227 int get_time(unsigned *time, const char *str)
237 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
238 strcasecmp(p, "secs")==0)
239 t *= TIME_UNITS_PER_SEC;
240 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
241 strcasecmp(p, "msecs") == 0)
242 t *= TIME_UNITS_PER_SEC/1000;
243 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
244 strcasecmp(p, "usecs") == 0)
245 t *= TIME_UNITS_PER_SEC/1000000;
255 void print_time(char *buf, int len, __u32 time)
259 if (tmp >= TIME_UNITS_PER_SEC)
260 snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC);
261 else if (tmp >= TIME_UNITS_PER_SEC/1000)
262 snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000));
264 snprintf(buf, len, "%uus", time);
267 char * sprint_time(__u32 time, char *buf)
269 print_time(buf, SPRINT_BSIZE-1, time);
273 char * sprint_ticks(__u32 ticks, char *buf)
275 return sprint_time(tc_core_tick2time(ticks), buf);
278 int get_size(unsigned *size, const char *str)
283 sz = strtod(str, &p);
288 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
290 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
291 sz *= 1024*1024*1024;
292 else if (strcasecmp(p, "gbit") == 0)
293 sz *= 1024*1024*1024/8;
294 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
296 else if (strcasecmp(p, "mbit") == 0)
298 else if (strcasecmp(p, "kbit") == 0)
300 else if (strcasecmp(p, "b") != 0)
308 int get_size_and_cell(unsigned *size, int *cell_log, char *str)
310 char * slash = strchr(str, '/');
315 if (get_size(size, str))
322 if (get_integer(&cell, slash+1, 0))
326 for (i=0; i<32; i++) {
327 if ((1<<i) == cell) {
337 void print_size(char *buf, int len, __u32 sz)
341 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
342 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
343 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
344 snprintf(buf, len, "%gKb", rint(tmp/1024));
346 snprintf(buf, len, "%ub", sz);
349 char * sprint_size(__u32 size, char *buf)
351 print_size(buf, SPRINT_BSIZE-1, size);
355 static const double max_percent_value = 0xffffffff;
357 int get_percent(__u32 *percent, const char *str)
360 double per = strtod(str, &p) / 100.;
362 if (per > 1. || per < 0)
364 if (*p && strcmp(p, "%"))
367 *percent = (unsigned) rint(per * max_percent_value);
371 void print_percent(char *buf, int len, __u32 per)
373 snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value);
376 char * sprint_percent(__u32 per, char *buf)
378 print_percent(buf, SPRINT_BSIZE-1, per);
382 void print_qdisc_handle(char *buf, int len, __u32 h)
384 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
387 char * sprint_qdisc_handle(__u32 h, char *buf)
389 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
393 char * action_n2a(int action, char *buf, int len)
405 case TC_ACT_RECLASSIFY:
412 snprintf(buf, len, "%d", action);
417 int action_a2n(char *arg, int *result)
421 if (matches(arg, "continue") == 0)
423 else if (matches(arg, "drop") == 0)
425 else if (matches(arg, "shot") == 0)
427 else if (matches(arg, "pass") == 0)
429 else if (strcmp(arg, "ok") == 0)
431 else if (matches(arg, "reclassify") == 0)
432 res = TC_ACT_RECLASSIFY;
435 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
442 int get_linklayer(unsigned *val, const char *arg)
446 if (matches(arg, "ethernet") == 0)
447 res = LINKLAYER_ETHERNET;
448 else if (matches(arg, "atm") == 0)
450 else if (matches(arg, "adsl") == 0)
453 return -1; /* Indicate error */
459 void print_linklayer(char *buf, int len, unsigned linklayer)
462 case LINKLAYER_UNSPEC:
463 snprintf(buf, len, "%s", "unspec");
465 case LINKLAYER_ETHERNET:
466 snprintf(buf, len, "%s", "ethernet");
469 snprintf(buf, len, "%s", "atm");
472 snprintf(buf, len, "%s", "unknown");
477 char *sprint_linklayer(unsigned linklayer, char *buf)
479 print_linklayer(buf, SPRINT_BSIZE-1, linklayer);
483 void print_tm(FILE * f, const struct tcf_t *tm)
485 int hz = get_user_hz();
486 if (tm->install != 0)
487 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
488 if (tm->lastuse != 0)
489 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
490 if (tm->expires != 0)
491 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
494 void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
497 struct rtattr *tbs[TCA_STATS_MAX + 1];
499 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
501 if (tbs[TCA_STATS_BASIC]) {
502 struct gnet_stats_basic bs = {0};
503 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
504 fprintf(fp, "%sSent %llu bytes %u pkt",
505 prefix, (unsigned long long) bs.bytes, bs.packets);
508 if (tbs[TCA_STATS_QUEUE]) {
509 struct gnet_stats_queue q = {0};
510 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
511 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
512 q.drops, q.overlimits, q.requeues);
515 if (tbs[TCA_STATS_RATE_EST]) {
516 struct gnet_stats_rate_est re = {0};
517 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
518 fprintf(fp, "\n%srate %s %upps ",
519 prefix, sprint_rate(re.bps, b1), re.pps);
522 if (tbs[TCA_STATS_QUEUE]) {
523 struct gnet_stats_queue q = {0};
524 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
525 if (!tbs[TCA_STATS_RATE_EST])
526 fprintf(fp, "\n%s", prefix);
527 fprintf(fp, "backlog %s %up requeues %u ",
528 sprint_size(q.backlog, b1), q.qlen, q.requeues);
532 *xstats = tbs[TCA_STATS_APP] ? : NULL;
535 void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
539 if (tb[TCA_STATS2]) {
540 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
541 if (xstats && NULL == *xstats)
545 /* backward compatibility */
549 /* handle case where kernel returns more/less than we know about */
550 memset(&st, 0, sizeof(st));
551 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
553 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
554 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
557 if (st.bps || st.pps || st.qlen || st.backlog) {
558 fprintf(fp, "\n%s", prefix);
559 if (st.bps || st.pps) {
560 fprintf(fp, "rate ");
562 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
564 fprintf(fp, "%upps ", st.pps);
566 if (st.qlen || st.backlog) {
567 fprintf(fp, "backlog ");
569 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
571 fprintf(fp, "%up ", st.qlen);
577 if (tb[TCA_XSTATS] && xstats)
578 *xstats = tb[TCA_XSTATS];