]>
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 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);
66 min
= strtoul(str
, &p
, 16);
78 int print_tc_classid(char *buf
, int len
, __u32 h
)
82 else if (h
== TC_H_UNSPEC
)
83 snprintf(buf
, len
, "none");
84 else if (TC_H_MAJ(h
) == 0)
85 snprintf(buf
, len
, ":%x", TC_H_MIN(h
));
86 else if (TC_H_MIN(h
) == 0)
87 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
89 snprintf(buf
, len
, "%x:%x", TC_H_MAJ(h
)>>16, TC_H_MIN(h
));
93 char * sprint_tc_classid(__u32 h
, char *buf
)
95 if (print_tc_classid(buf
, SPRINT_BSIZE
-1, h
))
100 /* See http://physics.nist.gov/cuu/Units/binary.html */
101 static const struct rate_suffix
{
108 { "mibit", 1024.*1024. },
109 { "mbit", 1000000. },
110 { "gibit", 1024.*1024.*1024. },
111 { "gbit", 1000000000. },
112 { "tibit", 1024.*1024.*1024.*1024. },
113 { "tbit", 1000000000000. },
115 { "KiBps", 8.*1024. },
117 { "MiBps", 8.*1024*1024. },
118 { "MBps", 8000000. },
119 { "GiBps", 8.*1024.*1024.*1024. },
120 { "GBps", 8000000000. },
121 { "TiBps", 8.*1024.*1024.*1024.*1024. },
122 { "TBps", 8000000000000. },
127 int get_rate(unsigned *rate
, const char *str
)
130 double bps
= strtod(str
, &p
);
131 const struct rate_suffix
*s
;
137 *rate
= bps
/ 8.; /* assume bytes/sec */
141 for (s
= suffixes
; s
->name
; ++s
) {
142 if (strcasecmp(s
->name
, p
) == 0) {
143 *rate
= (bps
* s
->scale
) / 8.;
151 int get_rate_and_cell(unsigned *rate
, int *cell_log
, char *str
)
153 char * slash
= strchr(str
, '/');
158 if (get_rate(rate
, str
))
165 if (get_integer(&cell
, slash
+1, 0))
169 for (i
=0; i
<32; i
++) {
170 if ((1<<i
) == cell
) {
180 void print_rate(char *buf
, int len
, __u32 rate
)
182 double tmp
= (double)rate
*8;
186 if (tmp
>= 1000.0*1024.0*1024.0)
187 snprintf(buf
, len
, "%.0fMibit", tmp
/1024.0*1024.0);
188 else if (tmp
>= 1000.0*1024)
189 snprintf(buf
, len
, "%.0fKibit", tmp
/1024);
191 snprintf(buf
, len
, "%.0fbit", tmp
);
193 if (tmp
>= 1000.0*1000000.0)
194 snprintf(buf
, len
, "%.0fMbit", tmp
/1000000.0);
195 else if (tmp
>= 1000.0 * 1000.0)
196 snprintf(buf
, len
, "%.0fKbit", tmp
/1000.0);
198 snprintf(buf
, len
, "%.0fbit", tmp
);
202 char * sprint_rate(__u32 rate
, char *buf
)
204 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
208 int get_usecs(unsigned *usecs
, const char *str
)
218 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec")==0 ||
219 strcasecmp(p
, "secs")==0)
221 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec")==0 ||
222 strcasecmp(p
, "msecs") == 0)
224 else if (strcasecmp(p
, "us") == 0 || strcasecmp(p
, "usec")==0 ||
225 strcasecmp(p
, "usecs") == 0)
236 void print_usecs(char *buf
, int len
, __u32 usec
)
241 snprintf(buf
, len
, "%.1fs", tmp
/1000000);
242 else if (tmp
>= 1000)
243 snprintf(buf
, len
, "%.1fms", tmp
/1000);
245 snprintf(buf
, len
, "%uus", usec
);
248 char * sprint_usecs(__u32 usecs
, char *buf
)
250 print_usecs(buf
, SPRINT_BSIZE
-1, usecs
);
254 int get_size(unsigned *size
, const char *str
)
259 sz
= strtod(str
, &p
);
264 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k")==0)
266 else if (strcasecmp(p
, "gb") == 0 || strcasecmp(p
, "g")==0)
267 sz
*= 1024*1024*1024;
268 else if (strcasecmp(p
, "gbit") == 0)
269 sz
*= 1024*1024*1024/8;
270 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m")==0)
272 else if (strcasecmp(p
, "mbit") == 0)
274 else if (strcasecmp(p
, "kbit") == 0)
276 else if (strcasecmp(p
, "b") != 0)
284 int get_size_and_cell(unsigned *size
, int *cell_log
, char *str
)
286 char * slash
= strchr(str
, '/');
291 if (get_size(size
, str
))
298 if (get_integer(&cell
, slash
+1, 0))
302 for (i
=0; i
<32; i
++) {
303 if ((1<<i
) == cell
) {
313 void print_size(char *buf
, int len
, __u32 sz
)
317 if (sz
>= 1024*1024 && fabs(1024*1024*rint(tmp
/(1024*1024)) - sz
) < 1024)
318 snprintf(buf
, len
, "%gMb", rint(tmp
/(1024*1024)));
319 else if (sz
>= 1024 && fabs(1024*rint(tmp
/1024) - sz
) < 16)
320 snprintf(buf
, len
, "%gKb", rint(tmp
/1024));
322 snprintf(buf
, len
, "%ub", sz
);
325 char * sprint_size(__u32 size
, char *buf
)
327 print_size(buf
, SPRINT_BSIZE
-1, size
);
331 static const double max_percent_value
= 0xffffffff;
333 int get_percent(__u32
*percent
, const char *str
)
336 double per
= strtod(str
, &p
) / 100.;
338 if (per
> 1. || per
< 0)
340 if (*p
&& strcmp(p
, "%"))
343 *percent
= (unsigned) rint(per
* max_percent_value
);
347 void print_percent(char *buf
, int len
, __u32 per
)
349 snprintf(buf
, len
, "%g%%", 100. * (double) per
/ max_percent_value
);
352 char * sprint_percent(__u32 per
, char *buf
)
354 print_percent(buf
, SPRINT_BSIZE
-1, per
);
358 void print_qdisc_handle(char *buf
, int len
, __u32 h
)
360 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
363 char * sprint_qdisc_handle(__u32 h
, char *buf
)
365 print_qdisc_handle(buf
, SPRINT_BSIZE
-1, h
);
369 char * action_n2a(int action
, char *buf
, int len
)
381 case TC_ACT_RECLASSIFY
:
388 snprintf(buf
, len
, "%d", action
);
393 int action_a2n(char *arg
, int *result
)
397 if (matches(arg
, "continue") == 0)
399 else if (matches(arg
, "drop") == 0)
401 else if (matches(arg
, "shot") == 0)
403 else if (matches(arg
, "pass") == 0)
405 else if (strcmp(arg
, "ok") == 0)
407 else if (matches(arg
, "reclassify") == 0)
408 res
= TC_ACT_RECLASSIFY
;
411 if (sscanf(arg
, "%d%c", &res
, &dummy
) != 1)
418 void print_tm(FILE * f
, const struct tcf_t
*tm
)
421 if (tm
->install
!= 0)
422 fprintf(f
, " installed %d sec", tm
->install
/hz
);
423 if (tm
->lastuse
!= 0)
424 fprintf(f
, " used %d sec", tm
->lastuse
/hz
);
425 if (tm
->expires
!= 0)
426 fprintf(f
, " expires %d sec", tm
->expires
/hz
);
429 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
432 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1] = {0};
434 parse_rtattr(tbs
, TCA_STATS_MAX
, RTA_DATA(rta
), RTA_PAYLOAD(rta
));
436 if (tbs
[TCA_STATS_BASIC
]) {
437 struct gnet_stats_basic bs
= {0};
438 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]), sizeof(bs
)));
439 fprintf(fp
, "%sSent %llu bytes %u pkt",
440 prefix
, bs
.bytes
, bs
.packets
);
443 if (tbs
[TCA_STATS_QUEUE
]) {
444 struct gnet_stats_queue q
= {0};
445 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
446 fprintf(fp
, " (dropped %u, overlimits %u requeues %u) ",
447 q
.drops
, q
.overlimits
, q
.requeues
);
450 if (tbs
[TCA_STATS_RATE_EST
]) {
451 struct gnet_stats_rate_est re
= {0};
452 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
453 fprintf(fp
, "\n%srate %s %upps ",
454 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
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 if (!tbs
[TCA_STATS_RATE_EST
])
461 fprintf(fp
, "\n%s", prefix
);
462 fprintf(fp
, "backlog %s %up requeues %u ",
463 sprint_size(q
.backlog
, b1
), q
.qlen
, q
.requeues
);
467 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
470 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
474 if (tb
[TCA_STATS2
]) {
475 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
476 if (xstats
&& NULL
== *xstats
)
480 /* backward compatibility */
484 /* handle case where kernel returns more/less than we know about */
485 memset(&st
, 0, sizeof(st
));
486 memcpy(&st
, RTA_DATA(tb
[TCA_STATS
]), MIN(RTA_PAYLOAD(tb
[TCA_STATS
]), sizeof(st
)));
488 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
489 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
492 if (st
.bps
|| st
.pps
|| st
.qlen
|| st
.backlog
) {
493 fprintf(fp
, "\n%s", prefix
);
494 if (st
.bps
|| st
.pps
) {
495 fprintf(fp
, "rate ");
497 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
499 fprintf(fp
, "%upps ", st
.pps
);
501 if (st
.qlen
|| st
.backlog
) {
502 fprintf(fp
, "backlog ");
504 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
506 fprintf(fp
, "%up ", st
.qlen
);
512 if (tb
[TCA_XSTATS
] && xstats
)
513 *xstats
= tb
[TCA_XSTATS
];