]>
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
;
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 blen
, __u32 h
)
133 SPRINT_BUF(handle
) = {};
134 int hlen
= SPRINT_BSIZE
- 1;
137 sprintf(handle
, "root");
138 else if (h
== TC_H_UNSPEC
)
139 snprintf(handle
, hlen
, "none");
140 else if (TC_H_MAJ(h
) == 0)
141 snprintf(handle
, hlen
, ":%x", TC_H_MIN(h
));
142 else if (TC_H_MIN(h
) == 0)
143 snprintf(handle
, hlen
, "%x:", TC_H_MAJ(h
) >> 16);
145 snprintf(handle
, hlen
, "%x:%x", TC_H_MAJ(h
) >> 16, TC_H_MIN(h
));
148 char clname
[IDNAME_MAX
] = {};
150 if (id_to_name(cls_names
, h
, clname
))
151 snprintf(buf
, blen
, "%s#%s", clname
, handle
);
153 snprintf(buf
, blen
, "%s", handle
);
155 snprintf(buf
, blen
, "%s", handle
);
161 char *sprint_tc_classid(__u32 h
, char *buf
)
163 if (print_tc_classid(buf
, SPRINT_BSIZE
-1, h
))
168 /* See http://physics.nist.gov/cuu/Units/binary.html */
169 static const struct rate_suffix
{
176 { "mibit", 1024.*1024. },
177 { "mbit", 1000000. },
178 { "gibit", 1024.*1024.*1024. },
179 { "gbit", 1000000000. },
180 { "tibit", 1024.*1024.*1024.*1024. },
181 { "tbit", 1000000000000. },
183 { "KiBps", 8.*1024. },
185 { "MiBps", 8.*1024*1024. },
186 { "MBps", 8000000. },
187 { "GiBps", 8.*1024.*1024.*1024. },
188 { "GBps", 8000000000. },
189 { "TiBps", 8.*1024.*1024.*1024.*1024. },
190 { "TBps", 8000000000000. },
195 int get_rate(unsigned int *rate
, const char *str
)
198 double bps
= strtod(str
, &p
);
199 const struct rate_suffix
*s
;
204 for (s
= suffixes
; s
->name
; ++s
) {
205 if (strcasecmp(s
->name
, p
) == 0) {
213 return -1; /* unknown suffix */
215 bps
/= 8; /* -> bytes per second */
217 /* detect if an overflow happened */
218 if (*rate
!= floor(bps
))
223 int get_rate64(__u64
*rate
, const char *str
)
226 double bps
= strtod(str
, &p
);
227 const struct rate_suffix
*s
;
232 for (s
= suffixes
; s
->name
; ++s
) {
233 if (strcasecmp(s
->name
, p
) == 0) {
241 return -1; /* unknown suffix */
243 bps
/= 8; /* -> bytes per second */
248 void print_rate(char *buf
, int len
, __u64 rate
)
251 unsigned long kilo
= use_iec
? 1024 : 1000;
252 const char *str
= use_iec
? "i" : "";
253 static char *units
[5] = {"", "K", "M", "G", "T"};
256 rate
<<= 3; /* bytes/sec -> bits/sec */
258 for (i
= 0; i
< ARRAY_SIZE(units
) - 1; i
++) {
261 if (((rate
% kilo
) != 0) && rate
< 1000*kilo
)
266 snprintf(buf
, len
, "%.0f%s%sbit", (double)rate
, units
[i
], str
);
269 char *sprint_rate(__u64 rate
, char *buf
)
271 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
275 int get_time(unsigned int *time
, const char *str
)
285 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec") == 0 ||
286 strcasecmp(p
, "secs") == 0)
287 t
*= TIME_UNITS_PER_SEC
;
288 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec") == 0 ||
289 strcasecmp(p
, "msecs") == 0)
290 t
*= TIME_UNITS_PER_SEC
/1000;
291 else if (strcasecmp(p
, "us") == 0 || strcasecmp(p
, "usec") == 0 ||
292 strcasecmp(p
, "usecs") == 0)
293 t
*= TIME_UNITS_PER_SEC
/1000000;
303 void print_time(char *buf
, int len
, __u32 time
)
307 if (tmp
>= TIME_UNITS_PER_SEC
)
308 snprintf(buf
, len
, "%.1fs", tmp
/TIME_UNITS_PER_SEC
);
309 else if (tmp
>= TIME_UNITS_PER_SEC
/1000)
310 snprintf(buf
, len
, "%.1fms", tmp
/(TIME_UNITS_PER_SEC
/1000));
312 snprintf(buf
, len
, "%uus", time
);
315 char *sprint_time(__u32 time
, char *buf
)
317 print_time(buf
, SPRINT_BSIZE
-1, time
);
321 char *sprint_ticks(__u32 ticks
, char *buf
)
323 return sprint_time(tc_core_tick2time(ticks
), buf
);
326 int get_size(unsigned int *size
, const char *str
)
331 sz
= strtod(str
, &p
);
336 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k") == 0)
338 else if (strcasecmp(p
, "gb") == 0 || strcasecmp(p
, "g") == 0)
339 sz
*= 1024*1024*1024;
340 else if (strcasecmp(p
, "gbit") == 0)
341 sz
*= 1024*1024*1024/8;
342 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m") == 0)
344 else if (strcasecmp(p
, "mbit") == 0)
346 else if (strcasecmp(p
, "kbit") == 0)
348 else if (strcasecmp(p
, "b") != 0)
356 int get_size_and_cell(unsigned int *size
, int *cell_log
, char *str
)
358 char *slash
= strchr(str
, '/');
363 if (get_size(size
, str
))
370 if (get_integer(&cell
, slash
+1, 0))
374 for (i
= 0; i
< 32; i
++) {
375 if ((1<<i
) == cell
) {
385 void print_size(char *buf
, int len
, __u32 sz
)
389 if (sz
>= 1024*1024 && fabs(1024*1024*rint(tmp
/(1024*1024)) - sz
) < 1024)
390 snprintf(buf
, len
, "%gMb", rint(tmp
/(1024*1024)));
391 else if (sz
>= 1024 && fabs(1024*rint(tmp
/1024) - sz
) < 16)
392 snprintf(buf
, len
, "%gKb", rint(tmp
/1024));
394 snprintf(buf
, len
, "%ub", sz
);
397 char *sprint_size(__u32 size
, char *buf
)
399 print_size(buf
, SPRINT_BSIZE
-1, size
);
403 void print_qdisc_handle(char *buf
, int len
, __u32 h
)
405 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
408 char *sprint_qdisc_handle(__u32 h
, char *buf
)
410 print_qdisc_handle(buf
, SPRINT_BSIZE
-1, h
);
414 const char *action_n2a(int action
)
425 case TC_ACT_RECLASSIFY
:
432 snprintf(buf
, 64, "%d", action
);
438 /* Convert action branch name into numeric format.
441 * @arg - string to parse
442 * @result - pointer to output variable
443 * @allow_num - whether @arg may be in numeric format already
445 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
447 int action_a2n(char *arg
, int *result
, bool allow_num
)
455 {"continue", TC_ACT_UNSPEC
},
456 {"drop", TC_ACT_SHOT
},
457 {"shot", TC_ACT_SHOT
},
460 {"reclassify", TC_ACT_RECLASSIFY
},
461 {"pipe", TC_ACT_PIPE
},
465 for (iter
= a2n
; iter
->a
; iter
++) {
466 if (matches(arg
, iter
->a
) != 0)
471 if (!allow_num
|| sscanf(arg
, "%d%c", &n
, &dummy
) != 1)
478 int get_linklayer(unsigned int *val
, const char *arg
)
482 if (matches(arg
, "ethernet") == 0)
483 res
= LINKLAYER_ETHERNET
;
484 else if (matches(arg
, "atm") == 0)
486 else if (matches(arg
, "adsl") == 0)
489 return -1; /* Indicate error */
495 void print_linklayer(char *buf
, int len
, unsigned int linklayer
)
498 case LINKLAYER_UNSPEC
:
499 snprintf(buf
, len
, "%s", "unspec");
501 case LINKLAYER_ETHERNET
:
502 snprintf(buf
, len
, "%s", "ethernet");
505 snprintf(buf
, len
, "%s", "atm");
508 snprintf(buf
, len
, "%s", "unknown");
513 char *sprint_linklayer(unsigned int linklayer
, char *buf
)
515 print_linklayer(buf
, SPRINT_BSIZE
-1, linklayer
);
519 void print_tm(FILE *f
, const struct tcf_t
*tm
)
521 int hz
= get_user_hz();
523 if (tm
->install
!= 0)
524 fprintf(f
, " installed %u sec", (unsigned int)(tm
->install
/hz
));
525 if (tm
->lastuse
!= 0)
526 fprintf(f
, " used %u sec", (unsigned int)(tm
->lastuse
/hz
));
527 if (tm
->expires
!= 0)
528 fprintf(f
, " expires %u sec", (unsigned int)(tm
->expires
/hz
));
531 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
534 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
536 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
538 if (tbs
[TCA_STATS_BASIC
]) {
539 struct gnet_stats_basic bs
= {0};
541 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]), sizeof(bs
)));
542 fprintf(fp
, "%sSent %llu bytes %u pkt",
543 prefix
, (unsigned long long) bs
.bytes
, bs
.packets
);
546 if (tbs
[TCA_STATS_QUEUE
]) {
547 struct gnet_stats_queue q
= {0};
549 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
550 fprintf(fp
, " (dropped %u, overlimits %u requeues %u) ",
551 q
.drops
, q
.overlimits
, q
.requeues
);
554 if (tbs
[TCA_STATS_RATE_EST64
]) {
555 struct gnet_stats_rate_est64 re
= {0};
557 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST64
]),
558 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST64
]),
560 fprintf(fp
, "\n%srate %s %llupps ",
561 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
562 } else if (tbs
[TCA_STATS_RATE_EST
]) {
563 struct gnet_stats_rate_est re
= {0};
565 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]),
566 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
567 fprintf(fp
, "\n%srate %s %upps ",
568 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
571 if (tbs
[TCA_STATS_QUEUE
]) {
572 struct gnet_stats_queue q
= {0};
574 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
575 if (!tbs
[TCA_STATS_RATE_EST
])
576 fprintf(fp
, "\n%s", prefix
);
577 fprintf(fp
, "backlog %s %up requeues %u ",
578 sprint_size(q
.backlog
, b1
), q
.qlen
, q
.requeues
);
582 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
585 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
589 if (tb
[TCA_STATS2
]) {
590 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
591 if (xstats
&& NULL
== *xstats
)
595 /* backward compatibility */
597 struct tc_stats st
= {};
599 /* handle case where kernel returns more/less than we know about */
600 memcpy(&st
, RTA_DATA(tb
[TCA_STATS
]), MIN(RTA_PAYLOAD(tb
[TCA_STATS
]), sizeof(st
)));
602 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
603 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
606 if (st
.bps
|| st
.pps
|| st
.qlen
|| st
.backlog
) {
607 fprintf(fp
, "\n%s", prefix
);
608 if (st
.bps
|| st
.pps
) {
609 fprintf(fp
, "rate ");
611 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
613 fprintf(fp
, "%upps ", st
.pps
);
615 if (st
.qlen
|| st
.backlog
) {
616 fprintf(fp
, "backlog ");
618 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
620 fprintf(fp
, "%up ", st
.qlen
);
626 if (tb
[TCA_XSTATS
] && xstats
)
627 *xstats
= tb
[TCA_XSTATS
];