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>
27 int get_qdisc_handle(__u32 *h, const char *str)
33 if (strcmp(str, "none") == 0)
35 maj = strtoul(str, &p, 16);
39 if (*p != ':' && *p!=0)
46 int get_tc_classid(__u32 *h, const char *str)
52 if (strcmp(str, "root") == 0)
55 if (strcmp(str, "none") == 0)
57 maj = strtoul(str, &p, 16);
68 min = strtoul(str, &p, 16);
82 int print_tc_classid(char *buf, int len, __u32 h)
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);
93 snprintf(buf, len, "%x:%x", TC_H_MAJ(h)>>16, TC_H_MIN(h));
97 char * sprint_tc_classid(__u32 h, char *buf)
99 if (print_tc_classid(buf, SPRINT_BSIZE-1, h))
104 /* See http://physics.nist.gov/cuu/Units/binary.html */
105 static const struct rate_suffix {
112 { "mibit", 1024.*1024. },
113 { "mbit", 1000000. },
114 { "gibit", 1024.*1024.*1024. },
115 { "gbit", 1000000000. },
116 { "tibit", 1024.*1024.*1024.*1024. },
117 { "tbit", 1000000000000. },
119 { "KiBps", 8.*1024. },
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. },
131 int get_rate(unsigned *rate, const char *str)
134 double bps = strtod(str, &p);
135 const struct rate_suffix *s;
141 *rate = bps / 8.; /* assume bytes/sec */
145 for (s = suffixes; s->name; ++s) {
146 if (strcasecmp(s->name, p) == 0) {
147 *rate = (bps * s->scale) / 8.;
155 int get_rate_and_cell(unsigned *rate, int *cell_log, char *str)
157 char * slash = strchr(str, '/');
162 if (get_rate(rate, str))
169 if (get_integer(&cell, slash+1, 0))
173 for (i=0; i<32; i++) {
174 if ((1<<i) == cell) {
184 void print_rate(char *buf, int len, __u32 rate)
186 double tmp = (double)rate*8;
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);
195 snprintf(buf, len, "%.0fbit", tmp);
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);
202 snprintf(buf, len, "%.0fbit", tmp);
206 char * sprint_rate(__u32 rate, char *buf)
208 print_rate(buf, SPRINT_BSIZE-1, rate);
212 int get_usecs(unsigned *usecs, const char *str)
222 if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec")==0 ||
223 strcasecmp(p, "secs")==0)
225 else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec")==0 ||
226 strcasecmp(p, "msecs") == 0)
228 else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec")==0 ||
229 strcasecmp(p, "usecs") == 0)
240 void print_usecs(char *buf, int len, __u32 usec)
245 snprintf(buf, len, "%.1fs", tmp/1000000);
246 else if (tmp >= 1000)
247 snprintf(buf, len, "%.1fms", tmp/1000);
249 snprintf(buf, len, "%uus", usec);
252 char * sprint_usecs(__u32 usecs, char *buf)
254 print_usecs(buf, SPRINT_BSIZE-1, usecs);
258 int get_size(unsigned *size, const char *str)
263 sz = strtod(str, &p);
268 if (strcasecmp(p, "kb") == 0 || strcasecmp(p, "k")==0)
270 else if (strcasecmp(p, "gb") == 0 || strcasecmp(p, "g")==0)
271 sz *= 1024*1024*1024;
272 else if (strcasecmp(p, "gbit") == 0)
273 sz *= 1024*1024*1024/8;
274 else if (strcasecmp(p, "mb") == 0 || strcasecmp(p, "m")==0)
276 else if (strcasecmp(p, "mbit") == 0)
278 else if (strcasecmp(p, "kbit") == 0)
280 else if (strcasecmp(p, "b") != 0)
288 int get_size_and_cell(unsigned *size, int *cell_log, char *str)
290 char * slash = strchr(str, '/');
295 if (get_size(size, str))
302 if (get_integer(&cell, slash+1, 0))
306 for (i=0; i<32; i++) {
307 if ((1<<i) == cell) {
317 void print_size(char *buf, int len, __u32 sz)
321 if (sz >= 1024*1024 && fabs(1024*1024*rint(tmp/(1024*1024)) - sz) < 1024)
322 snprintf(buf, len, "%gMb", rint(tmp/(1024*1024)));
323 else if (sz >= 1024 && fabs(1024*rint(tmp/1024) - sz) < 16)
324 snprintf(buf, len, "%gKb", rint(tmp/1024));
326 snprintf(buf, len, "%ub", sz);
329 char * sprint_size(__u32 size, char *buf)
331 print_size(buf, SPRINT_BSIZE-1, size);
335 static const double max_percent_value = 0xffffffff;
337 int get_percent(__u32 *percent, const char *str)
340 double per = strtod(str, &p) / 100.;
342 if (per > 1. || per < 0)
344 if (*p && strcmp(p, "%"))
347 *percent = (unsigned) rint(per * max_percent_value);
351 void print_percent(char *buf, int len, __u32 per)
353 snprintf(buf, len, "%g%%", 100. * (double) per / max_percent_value);
356 char * sprint_percent(__u32 per, char *buf)
358 print_percent(buf, SPRINT_BSIZE-1, per);
362 void print_qdisc_handle(char *buf, int len, __u32 h)
364 snprintf(buf, len, "%x:", TC_H_MAJ(h)>>16);
367 char * sprint_qdisc_handle(__u32 h, char *buf)
369 print_qdisc_handle(buf, SPRINT_BSIZE-1, h);
373 char * action_n2a(int action, char *buf, int len)
385 case TC_ACT_RECLASSIFY:
392 snprintf(buf, len, "%d", action);
397 int action_a2n(char *arg, int *result)
401 if (matches(arg, "continue") == 0)
403 else if (matches(arg, "drop") == 0)
405 else if (matches(arg, "shot") == 0)
407 else if (matches(arg, "pass") == 0)
409 else if (strcmp(arg, "ok") == 0)
411 else if (matches(arg, "reclassify") == 0)
412 res = TC_ACT_RECLASSIFY;
415 if (sscanf(arg, "%d%c", &res, &dummy) != 1)
422 void print_tm(FILE * f, const struct tcf_t *tm)
424 int hz = get_user_hz();
425 if (tm->install != 0)
426 fprintf(f, " installed %u sec", (unsigned)(tm->install/hz));
427 if (tm->lastuse != 0)
428 fprintf(f, " used %u sec", (unsigned)(tm->lastuse/hz));
429 if (tm->expires != 0)
430 fprintf(f, " expires %u sec", (unsigned)(tm->expires/hz));
433 void print_tcstats2_attr(FILE *fp, struct rtattr *rta, char *prefix, struct rtattr **xstats)
436 struct rtattr *tbs[TCA_STATS_MAX + 1];
438 parse_rtattr_nested(tbs, TCA_STATS_MAX, rta);
440 if (tbs[TCA_STATS_BASIC]) {
441 struct gnet_stats_basic bs = {0};
442 memcpy(&bs, RTA_DATA(tbs[TCA_STATS_BASIC]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_BASIC]), sizeof(bs)));
443 fprintf(fp, "%sSent %llu bytes %u pkt",
444 prefix, (unsigned long long) bs.bytes, bs.packets);
447 if (tbs[TCA_STATS_QUEUE]) {
448 struct gnet_stats_queue q = {0};
449 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
450 fprintf(fp, " (dropped %u, overlimits %u requeues %u) ",
451 q.drops, q.overlimits, q.requeues);
454 if (tbs[TCA_STATS_RATE_EST]) {
455 struct gnet_stats_rate_est re = {0};
456 memcpy(&re, RTA_DATA(tbs[TCA_STATS_RATE_EST]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_RATE_EST]), sizeof(re)));
457 fprintf(fp, "\n%srate %s %upps ",
458 prefix, sprint_rate(re.bps, b1), re.pps);
461 if (tbs[TCA_STATS_QUEUE]) {
462 struct gnet_stats_queue q = {0};
463 memcpy(&q, RTA_DATA(tbs[TCA_STATS_QUEUE]), MIN(RTA_PAYLOAD(tbs[TCA_STATS_QUEUE]), sizeof(q)));
464 if (!tbs[TCA_STATS_RATE_EST])
465 fprintf(fp, "\n%s", prefix);
466 fprintf(fp, "backlog %s %up requeues %u ",
467 sprint_size(q.backlog, b1), q.qlen, q.requeues);
471 *xstats = tbs[TCA_STATS_APP] ? : NULL;
474 void print_tcstats_attr(FILE *fp, struct rtattr *tb[], char *prefix, struct rtattr **xstats)
478 if (tb[TCA_STATS2]) {
479 print_tcstats2_attr(fp, tb[TCA_STATS2], prefix, xstats);
480 if (xstats && NULL == *xstats)
484 /* backward compatibility */
488 /* handle case where kernel returns more/less than we know about */
489 memset(&st, 0, sizeof(st));
490 memcpy(&st, RTA_DATA(tb[TCA_STATS]), MIN(RTA_PAYLOAD(tb[TCA_STATS]), sizeof(st)));
492 fprintf(fp, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
493 prefix, (unsigned long long)st.bytes, st.packets, st.drops,
496 if (st.bps || st.pps || st.qlen || st.backlog) {
497 fprintf(fp, "\n%s", prefix);
498 if (st.bps || st.pps) {
499 fprintf(fp, "rate ");
501 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
503 fprintf(fp, "%upps ", st.pps);
505 if (st.qlen || st.backlog) {
506 fprintf(fp, "backlog ");
508 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
510 fprintf(fp, "%up ", st.qlen);
516 if (tb[TCA_XSTATS] && xstats)
517 *xstats = tb[TCA_XSTATS];