]>
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 <sys/param.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
29 #include "tc_common.h"
32 #define LIBDIR "/usr/lib"
35 static struct db_names
*cls_names
= NULL
;
37 #define NAMES_DB "/etc/iproute2/tc_cls"
39 int cls_names_init(char *path
)
43 cls_names
= db_names_alloc();
47 ret
= db_names_load(cls_names
, path
?: NAMES_DB
);
48 if (ret
== -ENOENT
&& path
) {
49 fprintf(stderr
, "Can't open class names file: %s\n", path
);
53 db_names_free(cls_names
);
60 void cls_names_uninit(void)
62 db_names_free(cls_names
);
65 const char *get_tc_lib(void)
69 lib_dir
= getenv("TC_LIB_DIR");
71 lib_dir
= LIBDIR
"/tc/";
76 int get_qdisc_handle(__u32
*h
, const char *str
)
82 if (strcmp(str
, "none") == 0)
84 maj
= strtoul(str
, &p
, 16);
88 if (*p
!= ':' && *p
!=0)
95 int get_tc_classid(__u32
*h
, const char *str
)
101 if (strcmp(str
, "root") == 0)
104 if (strcmp(str
, "none") == 0)
106 maj
= strtoul(str
, &p
, 16);
117 min
= strtoul(str
, &p
, 16);
131 int print_tc_classid(char *buf
, int len
, __u32 h
)
133 char handle
[40] = {};
136 sprintf(handle
, "root");
137 else if (h
== TC_H_UNSPEC
)
138 snprintf(handle
, len
, "none");
139 else if (TC_H_MAJ(h
) == 0)
140 snprintf(handle
, len
, ":%x", TC_H_MIN(h
));
141 else if (TC_H_MIN(h
) == 0)
142 snprintf(handle
, len
, "%x:", TC_H_MAJ(h
) >> 16);
144 snprintf(handle
, len
, "%x:%x", TC_H_MAJ(h
) >> 16, TC_H_MIN(h
));
147 char clname
[IDNAME_MAX
] = {};
149 if (id_to_name(cls_names
, h
, clname
))
150 snprintf(buf
, len
, "%s#%s", clname
, handle
);
152 snprintf(buf
, len
, "%s", handle
);
154 snprintf(buf
, len
, "%s", handle
);
160 char *sprint_tc_classid(__u32 h
, char *buf
)
162 if (print_tc_classid(buf
, SPRINT_BSIZE
-1, h
))
167 /* See http://physics.nist.gov/cuu/Units/binary.html */
168 static const struct rate_suffix
{
175 { "mibit", 1024.*1024. },
176 { "mbit", 1000000. },
177 { "gibit", 1024.*1024.*1024. },
178 { "gbit", 1000000000. },
179 { "tibit", 1024.*1024.*1024.*1024. },
180 { "tbit", 1000000000000. },
182 { "KiBps", 8.*1024. },
184 { "MiBps", 8.*1024*1024. },
185 { "MBps", 8000000. },
186 { "GiBps", 8.*1024.*1024.*1024. },
187 { "GBps", 8000000000. },
188 { "TiBps", 8.*1024.*1024.*1024.*1024. },
189 { "TBps", 8000000000000. },
194 int get_rate(unsigned *rate
, const char *str
)
197 double bps
= strtod(str
, &p
);
198 const struct rate_suffix
*s
;
203 for (s
= suffixes
; s
->name
; ++s
) {
204 if (strcasecmp(s
->name
, p
) == 0) {
212 return -1; /* unknown suffix */
214 bps
/= 8; /* -> bytes per second */
216 /* detect if an overflow happened */
217 if (*rate
!= floor(bps
))
222 int get_rate64(__u64
*rate
, const char *str
)
225 double bps
= strtod(str
, &p
);
226 const struct rate_suffix
*s
;
231 for (s
= suffixes
; s
->name
; ++s
) {
232 if (strcasecmp(s
->name
, p
) == 0) {
240 return -1; /* unknown suffix */
242 bps
/= 8; /* -> bytes per second */
247 void print_rate(char *buf
, int len
, __u64 rate
)
250 unsigned long kilo
= use_iec
? 1024 : 1000;
251 const char *str
= use_iec
? "i" : "";
253 static char *units
[5] = {"", "K", "M", "G", "T"};
255 rate
<<= 3; /* bytes/sec -> bits/sec */
257 for (i
= 0; i
< ARRAY_SIZE(units
); i
++) {
260 if (((rate
% kilo
) != 0) && rate
< 1000*kilo
)
264 snprintf(buf
, len
, "%.0f%s%sbit", (double)rate
, units
[i
], str
);
267 char * sprint_rate(__u64 rate
, char *buf
)
269 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
273 int get_time(unsigned *time
, const char *str
)
283 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec")==0 ||
284 strcasecmp(p
, "secs")==0)
285 t
*= TIME_UNITS_PER_SEC
;
286 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec")==0 ||
287 strcasecmp(p
, "msecs") == 0)
288 t
*= TIME_UNITS_PER_SEC
/1000;
289 else if (strcasecmp(p
, "us") == 0 || strcasecmp(p
, "usec")==0 ||
290 strcasecmp(p
, "usecs") == 0)
291 t
*= TIME_UNITS_PER_SEC
/1000000;
301 void print_time(char *buf
, int len
, __u32 time
)
305 if (tmp
>= TIME_UNITS_PER_SEC
)
306 snprintf(buf
, len
, "%.1fs", tmp
/TIME_UNITS_PER_SEC
);
307 else if (tmp
>= TIME_UNITS_PER_SEC
/1000)
308 snprintf(buf
, len
, "%.1fms", tmp
/(TIME_UNITS_PER_SEC
/1000));
310 snprintf(buf
, len
, "%uus", time
);
313 char * sprint_time(__u32 time
, char *buf
)
315 print_time(buf
, SPRINT_BSIZE
-1, time
);
319 char * sprint_ticks(__u32 ticks
, char *buf
)
321 return sprint_time(tc_core_tick2time(ticks
), buf
);
324 int get_size(unsigned *size
, const char *str
)
329 sz
= strtod(str
, &p
);
334 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k")==0)
336 else if (strcasecmp(p
, "gb") == 0 || strcasecmp(p
, "g")==0)
337 sz
*= 1024*1024*1024;
338 else if (strcasecmp(p
, "gbit") == 0)
339 sz
*= 1024*1024*1024/8;
340 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m")==0)
342 else if (strcasecmp(p
, "mbit") == 0)
344 else if (strcasecmp(p
, "kbit") == 0)
346 else if (strcasecmp(p
, "b") != 0)
354 int get_size_and_cell(unsigned *size
, int *cell_log
, char *str
)
356 char * slash
= strchr(str
, '/');
361 if (get_size(size
, str
))
368 if (get_integer(&cell
, slash
+1, 0))
372 for (i
=0; i
<32; i
++) {
373 if ((1<<i
) == cell
) {
383 void print_size(char *buf
, int len
, __u32 sz
)
387 if (sz
>= 1024*1024 && fabs(1024*1024*rint(tmp
/(1024*1024)) - sz
) < 1024)
388 snprintf(buf
, len
, "%gMb", rint(tmp
/(1024*1024)));
389 else if (sz
>= 1024 && fabs(1024*rint(tmp
/1024) - sz
) < 16)
390 snprintf(buf
, len
, "%gKb", rint(tmp
/1024));
392 snprintf(buf
, len
, "%ub", sz
);
395 char * sprint_size(__u32 size
, char *buf
)
397 print_size(buf
, SPRINT_BSIZE
-1, size
);
401 void print_qdisc_handle(char *buf
, int len
, __u32 h
)
403 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
406 char * sprint_qdisc_handle(__u32 h
, char *buf
)
408 print_qdisc_handle(buf
, SPRINT_BSIZE
-1, h
);
412 char * action_n2a(int action
, char *buf
, int len
)
424 case TC_ACT_RECLASSIFY
:
431 snprintf(buf
, len
, "%d", action
);
436 int action_a2n(char *arg
, int *result
)
440 if (matches(arg
, "continue") == 0)
442 else if (matches(arg
, "drop") == 0)
444 else if (matches(arg
, "shot") == 0)
446 else if (matches(arg
, "pass") == 0)
448 else if (strcmp(arg
, "ok") == 0)
450 else if (matches(arg
, "reclassify") == 0)
451 res
= TC_ACT_RECLASSIFY
;
454 if (sscanf(arg
, "%d%c", &res
, &dummy
) != 1)
461 int get_linklayer(unsigned *val
, const char *arg
)
465 if (matches(arg
, "ethernet") == 0)
466 res
= LINKLAYER_ETHERNET
;
467 else if (matches(arg
, "atm") == 0)
469 else if (matches(arg
, "adsl") == 0)
472 return -1; /* Indicate error */
478 void print_linklayer(char *buf
, int len
, unsigned linklayer
)
481 case LINKLAYER_UNSPEC
:
482 snprintf(buf
, len
, "%s", "unspec");
484 case LINKLAYER_ETHERNET
:
485 snprintf(buf
, len
, "%s", "ethernet");
488 snprintf(buf
, len
, "%s", "atm");
491 snprintf(buf
, len
, "%s", "unknown");
496 char *sprint_linklayer(unsigned linklayer
, char *buf
)
498 print_linklayer(buf
, SPRINT_BSIZE
-1, linklayer
);
502 void print_tm(FILE * f
, const struct tcf_t
*tm
)
504 int hz
= get_user_hz();
505 if (tm
->install
!= 0)
506 fprintf(f
, " installed %u sec", (unsigned)(tm
->install
/hz
));
507 if (tm
->lastuse
!= 0)
508 fprintf(f
, " used %u sec", (unsigned)(tm
->lastuse
/hz
));
509 if (tm
->expires
!= 0)
510 fprintf(f
, " expires %u sec", (unsigned)(tm
->expires
/hz
));
513 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
516 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
518 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
520 if (tbs
[TCA_STATS_BASIC
]) {
521 struct gnet_stats_basic bs
= {0};
522 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]), sizeof(bs
)));
523 fprintf(fp
, "%sSent %llu bytes %u pkt",
524 prefix
, (unsigned long long) bs
.bytes
, bs
.packets
);
527 if (tbs
[TCA_STATS_QUEUE
]) {
528 struct gnet_stats_queue q
= {0};
529 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
530 fprintf(fp
, " (dropped %u, overlimits %u requeues %u) ",
531 q
.drops
, q
.overlimits
, q
.requeues
);
534 if (tbs
[TCA_STATS_RATE_EST64
]) {
535 struct gnet_stats_rate_est64 re
= {0};
537 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST64
]),
538 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST64
]),
540 fprintf(fp
, "\n%srate %s %llupps ",
541 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
542 } else if (tbs
[TCA_STATS_RATE_EST
]) {
543 struct gnet_stats_rate_est re
= {0};
545 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]),
546 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
547 fprintf(fp
, "\n%srate %s %upps ",
548 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
551 if (tbs
[TCA_STATS_QUEUE
]) {
552 struct gnet_stats_queue q
= {0};
553 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
554 if (!tbs
[TCA_STATS_RATE_EST
])
555 fprintf(fp
, "\n%s", prefix
);
556 fprintf(fp
, "backlog %s %up requeues %u ",
557 sprint_size(q
.backlog
, b1
), q
.qlen
, q
.requeues
);
561 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
564 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
568 if (tb
[TCA_STATS2
]) {
569 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
570 if (xstats
&& NULL
== *xstats
)
574 /* backward compatibility */
578 /* handle case where kernel returns more/less than we know about */
579 memset(&st
, 0, sizeof(st
));
580 memcpy(&st
, RTA_DATA(tb
[TCA_STATS
]), MIN(RTA_PAYLOAD(tb
[TCA_STATS
]), sizeof(st
)));
582 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
583 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
586 if (st
.bps
|| st
.pps
|| st
.qlen
|| st
.backlog
) {
587 fprintf(fp
, "\n%s", prefix
);
588 if (st
.bps
|| st
.pps
) {
589 fprintf(fp
, "rate ");
591 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
593 fprintf(fp
, "%upps ", st
.pps
);
595 if (st
.qlen
|| st
.backlog
) {
596 fprintf(fp
, "backlog ");
598 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
600 fprintf(fp
, "%up ", st
.qlen
);
606 if (tb
[TCA_XSTATS
] && xstats
)
607 *xstats
= tb
[TCA_XSTATS
];