]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_util.c
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 const char *get_tc_lib(void)
31 lib_dir
= getenv("TC_LIB_DIR");
33 lib_dir
= "/usr/lib/tc";
38 int get_qdisc_handle(__u32
*h
, const char *str
)
44 if (strcmp(str
, "none") == 0)
46 maj
= strtoul(str
, &p
, 16);
50 if (*p
!= ':' && *p
!=0)
57 int get_tc_classid(__u32
*h
, const char *str
)
63 if (strcmp(str
, "root") == 0)
66 if (strcmp(str
, "none") == 0)
68 maj
= strtoul(str
, &p
, 16);
79 min
= strtoul(str
, &p
, 16);
93 int print_tc_classid(char *buf
, int len
, __u32 h
)
97 else if (h
== TC_H_UNSPEC
)
98 snprintf(buf
, len
, "none");
99 else if (TC_H_MAJ(h
) == 0)
100 snprintf(buf
, len
, ":%x", TC_H_MIN(h
));
101 else if (TC_H_MIN(h
) == 0)
102 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
104 snprintf(buf
, len
, "%x:%x", TC_H_MAJ(h
)>>16, TC_H_MIN(h
));
108 char * sprint_tc_classid(__u32 h
, char *buf
)
110 if (print_tc_classid(buf
, SPRINT_BSIZE
-1, h
))
115 /* See http://physics.nist.gov/cuu/Units/binary.html */
116 static const struct rate_suffix
{
123 { "mibit", 1024.*1024. },
124 { "mbit", 1000000. },
125 { "gibit", 1024.*1024.*1024. },
126 { "gbit", 1000000000. },
127 { "tibit", 1024.*1024.*1024.*1024. },
128 { "tbit", 1000000000000. },
130 { "KiBps", 8.*1024. },
132 { "MiBps", 8.*1024*1024. },
133 { "MBps", 8000000. },
134 { "GiBps", 8.*1024.*1024.*1024. },
135 { "GBps", 8000000000. },
136 { "TiBps", 8.*1024.*1024.*1024.*1024. },
137 { "TBps", 8000000000000. },
142 int get_rate(unsigned *rate
, const char *str
)
145 double bps
= strtod(str
, &p
);
146 const struct rate_suffix
*s
;
152 *rate
= bps
/ 8.; /* assume bytes/sec */
156 for (s
= suffixes
; s
->name
; ++s
) {
157 if (strcasecmp(s
->name
, p
) == 0) {
158 *rate
= (bps
* s
->scale
) / 8.;
166 int get_rate_and_cell(unsigned *rate
, int *cell_log
, char *str
)
168 char * slash
= strchr(str
, '/');
173 if (get_rate(rate
, str
))
180 if (get_integer(&cell
, slash
+1, 0))
184 for (i
=0; i
<32; i
++) {
185 if ((1<<i
) == cell
) {
195 void print_rate(char *buf
, int len
, __u32 rate
)
197 double tmp
= (double)rate
*8;
201 if (tmp
>= 1000.0*1024.0*1024.0)
202 snprintf(buf
, len
, "%.0fMibit", tmp
/1024.0*1024.0);
203 else if (tmp
>= 1000.0*1024)
204 snprintf(buf
, len
, "%.0fKibit", tmp
/1024);
206 snprintf(buf
, len
, "%.0fbit", tmp
);
208 if (tmp
>= 1000.0*1000000.0)
209 snprintf(buf
, len
, "%.0fMbit", tmp
/1000000.0);
210 else if (tmp
>= 1000.0 * 1000.0)
211 snprintf(buf
, len
, "%.0fKbit", tmp
/1000.0);
213 snprintf(buf
, len
, "%.0fbit", tmp
);
217 char * sprint_rate(__u32 rate
, char *buf
)
219 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
223 int get_time(unsigned *time
, const char *str
)
233 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec")==0 ||
234 strcasecmp(p
, "secs")==0)
235 t
*= TIME_UNITS_PER_SEC
;
236 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec")==0 ||
237 strcasecmp(p
, "msecs") == 0)
238 t
*= TIME_UNITS_PER_SEC
/1000;
239 else if (strcasecmp(p
, "us") == 0 || strcasecmp(p
, "usec")==0 ||
240 strcasecmp(p
, "usecs") == 0)
241 t
*= TIME_UNITS_PER_SEC
/1000000;
251 void print_time(char *buf
, int len
, __u32 time
)
255 if (tmp
>= TIME_UNITS_PER_SEC
)
256 snprintf(buf
, len
, "%.1fs", tmp
/TIME_UNITS_PER_SEC
);
257 else if (tmp
>= TIME_UNITS_PER_SEC
/1000)
258 snprintf(buf
, len
, "%.1fms", tmp
/(TIME_UNITS_PER_SEC
/1000));
260 snprintf(buf
, len
, "%uus", time
);
263 char * sprint_time(__u32 time
, char *buf
)
265 print_time(buf
, SPRINT_BSIZE
-1, time
);
269 char * sprint_ticks(__u32 ticks
, char *buf
)
271 return sprint_time(tc_core_tick2time(ticks
), buf
);
274 int get_size(unsigned *size
, const char *str
)
279 sz
= strtod(str
, &p
);
284 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k")==0)
286 else if (strcasecmp(p
, "gb") == 0 || strcasecmp(p
, "g")==0)
287 sz
*= 1024*1024*1024;
288 else if (strcasecmp(p
, "gbit") == 0)
289 sz
*= 1024*1024*1024/8;
290 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m")==0)
292 else if (strcasecmp(p
, "mbit") == 0)
294 else if (strcasecmp(p
, "kbit") == 0)
296 else if (strcasecmp(p
, "b") != 0)
304 int get_size_and_cell(unsigned *size
, int *cell_log
, char *str
)
306 char * slash
= strchr(str
, '/');
311 if (get_size(size
, str
))
318 if (get_integer(&cell
, slash
+1, 0))
322 for (i
=0; i
<32; i
++) {
323 if ((1<<i
) == cell
) {
333 void print_size(char *buf
, int len
, __u32 sz
)
337 if (sz
>= 1024*1024 && fabs(1024*1024*rint(tmp
/(1024*1024)) - sz
) < 1024)
338 snprintf(buf
, len
, "%gMb", rint(tmp
/(1024*1024)));
339 else if (sz
>= 1024 && fabs(1024*rint(tmp
/1024) - sz
) < 16)
340 snprintf(buf
, len
, "%gKb", rint(tmp
/1024));
342 snprintf(buf
, len
, "%ub", sz
);
345 char * sprint_size(__u32 size
, char *buf
)
347 print_size(buf
, SPRINT_BSIZE
-1, size
);
351 static const double max_percent_value
= 0xffffffff;
353 int get_percent(__u32
*percent
, const char *str
)
356 double per
= strtod(str
, &p
) / 100.;
358 if (per
> 1. || per
< 0)
360 if (*p
&& strcmp(p
, "%"))
363 *percent
= (unsigned) rint(per
* max_percent_value
);
367 void print_percent(char *buf
, int len
, __u32 per
)
369 snprintf(buf
, len
, "%g%%", 100. * (double) per
/ max_percent_value
);
372 char * sprint_percent(__u32 per
, char *buf
)
374 print_percent(buf
, SPRINT_BSIZE
-1, per
);
378 void print_qdisc_handle(char *buf
, int len
, __u32 h
)
380 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
383 char * sprint_qdisc_handle(__u32 h
, char *buf
)
385 print_qdisc_handle(buf
, SPRINT_BSIZE
-1, h
);
389 char * action_n2a(int action
, char *buf
, int len
)
401 case TC_ACT_RECLASSIFY
:
408 snprintf(buf
, len
, "%d", action
);
413 int action_a2n(char *arg
, int *result
)
417 if (matches(arg
, "continue") == 0)
419 else if (matches(arg
, "drop") == 0)
421 else if (matches(arg
, "shot") == 0)
423 else if (matches(arg
, "pass") == 0)
425 else if (strcmp(arg
, "ok") == 0)
427 else if (matches(arg
, "reclassify") == 0)
428 res
= TC_ACT_RECLASSIFY
;
431 if (sscanf(arg
, "%d%c", &res
, &dummy
) != 1)
438 void print_tm(FILE * f
, const struct tcf_t
*tm
)
440 int hz
= get_user_hz();
441 if (tm
->install
!= 0)
442 fprintf(f
, " installed %u sec", (unsigned)(tm
->install
/hz
));
443 if (tm
->lastuse
!= 0)
444 fprintf(f
, " used %u sec", (unsigned)(tm
->lastuse
/hz
));
445 if (tm
->expires
!= 0)
446 fprintf(f
, " expires %u sec", (unsigned)(tm
->expires
/hz
));
449 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
452 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
454 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
456 if (tbs
[TCA_STATS_BASIC
]) {
457 struct gnet_stats_basic bs
= {0};
458 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]), sizeof(bs
)));
459 fprintf(fp
, "%sSent %llu bytes %u pkt",
460 prefix
, (unsigned long long) bs
.bytes
, bs
.packets
);
463 if (tbs
[TCA_STATS_QUEUE
]) {
464 struct gnet_stats_queue q
= {0};
465 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
466 fprintf(fp
, " (dropped %u, overlimits %u requeues %u) ",
467 q
.drops
, q
.overlimits
, q
.requeues
);
470 if (tbs
[TCA_STATS_RATE_EST
]) {
471 struct gnet_stats_rate_est re
= {0};
472 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
473 fprintf(fp
, "\n%srate %s %upps ",
474 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
477 if (tbs
[TCA_STATS_QUEUE
]) {
478 struct gnet_stats_queue q
= {0};
479 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
480 if (!tbs
[TCA_STATS_RATE_EST
])
481 fprintf(fp
, "\n%s", prefix
);
482 fprintf(fp
, "backlog %s %up requeues %u ",
483 sprint_size(q
.backlog
, b1
), q
.qlen
, q
.requeues
);
487 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
490 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
494 if (tb
[TCA_STATS2
]) {
495 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
496 if (xstats
&& NULL
== *xstats
)
500 /* backward compatibility */
504 /* handle case where kernel returns more/less than we know about */
505 memset(&st
, 0, sizeof(st
));
506 memcpy(&st
, RTA_DATA(tb
[TCA_STATS
]), MIN(RTA_PAYLOAD(tb
[TCA_STATS
]), sizeof(st
)));
508 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
509 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
512 if (st
.bps
|| st
.pps
|| st
.qlen
|| st
.backlog
) {
513 fprintf(fp
, "\n%s", prefix
);
514 if (st
.bps
|| st
.pps
) {
515 fprintf(fp
, "rate ");
517 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
519 fprintf(fp
, "%upps ", st
.pps
);
521 if (st
.qlen
|| st
.backlog
) {
522 fprintf(fp
, "backlog ");
524 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
526 fprintf(fp
, "%up ", st
.qlen
);
532 if (tb
[TCA_XSTATS
] && xstats
)
533 *xstats
= tb
[TCA_XSTATS
];