]>
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>
17 #include <sys/socket.h>
18 #include <sys/param.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
28 #include "tc_common.h"
31 #define LIBDIR "/usr/lib"
34 static struct db_names
*cls_names
;
36 #define NAMES_DB "/etc/iproute2/tc_cls"
38 int cls_names_init(char *path
)
42 cls_names
= db_names_alloc();
46 ret
= db_names_load(cls_names
, path
?: NAMES_DB
);
47 if (ret
== -ENOENT
&& path
) {
48 fprintf(stderr
, "Can't open class names file: %s\n", path
);
52 db_names_free(cls_names
);
59 void cls_names_uninit(void)
61 db_names_free(cls_names
);
64 const char *get_tc_lib(void)
68 lib_dir
= getenv("TC_LIB_DIR");
70 lib_dir
= LIBDIR
"/tc/";
75 int get_qdisc_handle(__u32
*h
, const char *str
)
81 if (strcmp(str
, "none") == 0)
83 maj
= strtoul(str
, &p
, 16);
84 if (p
== str
|| maj
>= (1 << 16))
87 if (*p
!= ':' && *p
!= 0)
94 int get_tc_classid(__u32
*h
, const char *str
)
100 if (strcmp(str
, "root") == 0)
103 if (strcmp(str
, "none") == 0)
105 maj
= strtoul(str
, &p
, 16);
116 min
= strtoul(str
, &p
, 16);
130 int print_tc_classid(char *buf
, int blen
, __u32 h
)
132 SPRINT_BUF(handle
) = {};
133 int hlen
= SPRINT_BSIZE
- 1;
136 sprintf(handle
, "root");
137 else if (h
== TC_H_UNSPEC
)
138 snprintf(handle
, hlen
, "none");
139 else if (TC_H_MAJ(h
) == 0)
140 snprintf(handle
, hlen
, ":%x", TC_H_MIN(h
));
141 else if (TC_H_MIN(h
) == 0)
142 snprintf(handle
, hlen
, "%x:", TC_H_MAJ(h
) >> 16);
144 snprintf(handle
, hlen
, "%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
, blen
, "%s#%s", clname
, handle
);
152 snprintf(buf
, blen
, "%s", handle
);
154 snprintf(buf
, blen
, "%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. },
193 int parse_percent_rate(char *rate
, const char *str
, const char *dev
)
197 double perc
, rate_mbit
;
201 fprintf(stderr
, "No device specified; specify device to rate limit by percentage\n");
205 if (read_prop(dev
, "speed", &dev_mbit
))
208 ret
= sscanf(str
, "%m[0-9.%]", &str_perc
);
212 if (parse_percent(&perc
, str_perc
))
217 if (perc
> 1.0 || perc
< 0.0) {
218 fprintf(stderr
, "Invalid rate specified; should be between [0,100]%% but is %s\n", str
);
222 rate_mbit
= perc
* dev_mbit
;
224 ret
= snprintf(rate
, 20, "%lf", rate_mbit
);
225 if (ret
<= 0 || ret
>= 20) {
226 fprintf(stderr
, "Unable to parse calculated rate\n");
233 fprintf(stderr
, "Specified rate value could not be read or is malformed\n");
237 int get_percent_rate(unsigned int *rate
, const char *str
, const char *dev
)
241 if (parse_percent_rate(r_str
, str
, dev
))
244 return get_rate(rate
, r_str
);
247 int get_percent_rate64(__u64
*rate
, const char *str
, const char *dev
)
251 if (parse_percent_rate(r_str
, str
, dev
))
254 return get_rate64(rate
, r_str
);
257 int get_rate(unsigned int *rate
, const char *str
)
260 double bps
= strtod(str
, &p
);
261 const struct rate_suffix
*s
;
266 for (s
= suffixes
; s
->name
; ++s
) {
267 if (strcasecmp(s
->name
, p
) == 0) {
275 return -1; /* unknown suffix */
277 bps
/= 8; /* -> bytes per second */
279 /* detect if an overflow happened */
280 if (*rate
!= floor(bps
))
285 int get_rate64(__u64
*rate
, const char *str
)
288 double bps
= strtod(str
, &p
);
289 const struct rate_suffix
*s
;
294 for (s
= suffixes
; s
->name
; ++s
) {
295 if (strcasecmp(s
->name
, p
) == 0) {
303 return -1; /* unknown suffix */
305 bps
/= 8; /* -> bytes per second */
310 void print_rate(char *buf
, int len
, __u64 rate
)
313 unsigned long kilo
= use_iec
? 1024 : 1000;
314 const char *str
= use_iec
? "i" : "";
315 static char *units
[5] = {"", "K", "M", "G", "T"};
318 rate
<<= 3; /* bytes/sec -> bits/sec */
320 for (i
= 0; i
< ARRAY_SIZE(units
) - 1; i
++) {
323 if (((rate
% kilo
) != 0) && rate
< 1000*kilo
)
328 snprintf(buf
, len
, "%.0f%s%sbit", (double)rate
, units
[i
], str
);
331 char *sprint_rate(__u64 rate
, char *buf
)
333 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
337 int get_time(unsigned int *time
, const char *str
)
347 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec") == 0 ||
348 strcasecmp(p
, "secs") == 0)
349 t
*= TIME_UNITS_PER_SEC
;
350 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec") == 0 ||
351 strcasecmp(p
, "msecs") == 0)
352 t
*= TIME_UNITS_PER_SEC
/1000;
353 else if (strcasecmp(p
, "us") == 0 || strcasecmp(p
, "usec") == 0 ||
354 strcasecmp(p
, "usecs") == 0)
355 t
*= TIME_UNITS_PER_SEC
/1000000;
365 void print_time(char *buf
, int len
, __u32 time
)
369 if (tmp
>= TIME_UNITS_PER_SEC
)
370 snprintf(buf
, len
, "%.1fs", tmp
/TIME_UNITS_PER_SEC
);
371 else if (tmp
>= TIME_UNITS_PER_SEC
/1000)
372 snprintf(buf
, len
, "%.1fms", tmp
/(TIME_UNITS_PER_SEC
/1000));
374 snprintf(buf
, len
, "%uus", time
);
377 char *sprint_time(__u32 time
, char *buf
)
379 print_time(buf
, SPRINT_BSIZE
-1, time
);
383 char *sprint_ticks(__u32 ticks
, char *buf
)
385 return sprint_time(tc_core_tick2time(ticks
), buf
);
388 int get_size(unsigned int *size
, const char *str
)
393 sz
= strtod(str
, &p
);
398 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k") == 0)
400 else if (strcasecmp(p
, "gb") == 0 || strcasecmp(p
, "g") == 0)
401 sz
*= 1024*1024*1024;
402 else if (strcasecmp(p
, "gbit") == 0)
403 sz
*= 1024*1024*1024/8;
404 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m") == 0)
406 else if (strcasecmp(p
, "mbit") == 0)
408 else if (strcasecmp(p
, "kbit") == 0)
410 else if (strcasecmp(p
, "b") != 0)
418 int get_size_and_cell(unsigned int *size
, int *cell_log
, char *str
)
420 char *slash
= strchr(str
, '/');
425 if (get_size(size
, str
))
432 if (get_integer(&cell
, slash
+1, 0))
436 for (i
= 0; i
< 32; i
++) {
437 if ((1<<i
) == cell
) {
447 void print_size(char *buf
, int len
, __u32 sz
)
451 if (sz
>= 1024*1024 && fabs(1024*1024*rint(tmp
/(1024*1024)) - sz
) < 1024)
452 snprintf(buf
, len
, "%gMb", rint(tmp
/(1024*1024)));
453 else if (sz
>= 1024 && fabs(1024*rint(tmp
/1024) - sz
) < 16)
454 snprintf(buf
, len
, "%gKb", rint(tmp
/1024));
456 snprintf(buf
, len
, "%ub", sz
);
459 char *sprint_size(__u32 size
, char *buf
)
461 print_size(buf
, SPRINT_BSIZE
-1, size
);
465 void print_qdisc_handle(char *buf
, int len
, __u32 h
)
467 snprintf(buf
, len
, "%x:", TC_H_MAJ(h
)>>16);
470 char *sprint_qdisc_handle(__u32 h
, char *buf
)
472 print_qdisc_handle(buf
, SPRINT_BSIZE
-1, h
);
476 static const char *action_n2a(int action
)
480 if (TC_ACT_EXT_CMP(action
, TC_ACT_GOTO_CHAIN
))
482 if (TC_ACT_EXT_CMP(action
, TC_ACT_JUMP
))
491 case TC_ACT_RECLASSIFY
:
500 snprintf(buf
, 64, "%d", action
);
505 /* Convert action branch name into numeric format.
508 * @arg - string to parse
509 * @result - pointer to output variable
510 * @allow_num - whether @arg may be in numeric format already
512 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
514 static int action_a2n(char *arg
, int *result
, bool allow_num
)
522 {"continue", TC_ACT_UNSPEC
},
523 {"drop", TC_ACT_SHOT
},
524 {"shot", TC_ACT_SHOT
},
527 {"reclassify", TC_ACT_RECLASSIFY
},
528 {"pipe", TC_ACT_PIPE
},
529 {"goto", TC_ACT_GOTO_CHAIN
},
530 {"jump", TC_ACT_JUMP
},
531 {"trap", TC_ACT_TRAP
},
535 for (iter
= a2n
; iter
->a
; iter
++) {
536 if (matches(arg
, iter
->a
) != 0)
541 if (!allow_num
|| sscanf(arg
, "%d%c", &n
, &dummy
) != 1)
548 static int __parse_action_control(int *argc_p
, char ***argv_p
, int *result_p
,
549 bool allow_num
, bool ignore_a2n_miss
)
552 char **argv
= *argv_p
;
557 if (action_a2n(*argv
, &result
, allow_num
) == -1) {
558 if (!ignore_a2n_miss
)
559 fprintf(stderr
, "Bad action type %s\n", *argv
);
562 if (result
== TC_ACT_GOTO_CHAIN
) {
566 if (matches(*argv
, "chain") != 0) {
567 fprintf(stderr
, "\"chain index\" expected\n");
571 if (get_u32(&chain_index
, *argv
, 10) ||
572 chain_index
> TC_ACT_EXT_VAL_MASK
) {
573 fprintf(stderr
, "Illegal \"chain index\"\n");
576 result
|= chain_index
;
578 if (result
== TC_ACT_JUMP
) {
582 if (get_u32(&jump_cnt
, *argv
, 10) ||
583 jump_cnt
> TC_ACT_EXT_VAL_MASK
) {
584 fprintf(stderr
, "Invalid \"jump count\" (%s)\n", *argv
);
596 /* Parse action control including possible options.
599 * @argc_p - pointer to argc to parse
600 * @argv_p - pointer to argv to parse
601 * @result_p - pointer to output variable
602 * @allow_num - whether action may be in numeric format already
604 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
606 int parse_action_control(int *argc_p
, char ***argv_p
,
607 int *result_p
, bool allow_num
)
609 return __parse_action_control(argc_p
, argv_p
, result_p
,
613 /* Parse action control including possible options.
616 * @argc_p - pointer to argc to parse
617 * @argv_p - pointer to argv to parse
618 * @result_p - pointer to output variable
619 * @allow_num - whether action may be in numeric format already
620 * @default_result - set as a result in case of parsing error
622 * In case there is an error during parsing, the default result is used.
624 void parse_action_control_dflt(int *argc_p
, char ***argv_p
,
625 int *result_p
, bool allow_num
,
628 if (__parse_action_control(argc_p
, argv_p
, result_p
, allow_num
, true))
629 *result_p
= default_result
;
632 static int parse_action_control_slash_spaces(int *argc_p
, char ***argv_p
,
633 int *result1_p
, int *result2_p
,
637 char **argv
= *argv_p
;
638 int result1
= -1, result2
;
639 int *result_p
= &result1
;
646 if (strcmp(*argv
, "/") != 0)
651 case 0: /* fall-through */
653 ret
= parse_action_control(&argc
, &argv
,
654 result_p
, allow_num
);
664 *result1_p
= result1
;
666 *result2_p
= result2
;
672 /* Parse action control with slash including possible options.
675 * @argc_p - pointer to argc to parse
676 * @argv_p - pointer to argv to parse
677 * @result1_p - pointer to the first (before slash) output variable
678 * @result2_p - pointer to the second (after slash) output variable
679 * @allow_num - whether action may be in numeric format already
681 * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
683 int parse_action_control_slash(int *argc_p
, char ***argv_p
,
684 int *result1_p
, int *result2_p
, bool allow_num
)
686 char **argv
= *argv_p
;
687 int result1
, result2
;
688 char *p
= strchr(*argv
, '/');
691 return parse_action_control_slash_spaces(argc_p
, argv_p
,
692 result1_p
, result2_p
,
695 if (action_a2n(*argv
, &result1
, allow_num
)) {
701 if (action_a2n(p
+ 1, &result2
, allow_num
))
704 *result1_p
= result1
;
705 *result2_p
= result2
;
709 void print_action_control(FILE *f
, const char *prefix
,
710 int action
, const char *suffix
)
712 print_string(PRINT_FP
, NULL
, "%s", prefix
);
713 open_json_object("control_action");
714 print_string(PRINT_ANY
, "type", "%s", action_n2a(action
));
715 if (TC_ACT_EXT_CMP(action
, TC_ACT_GOTO_CHAIN
))
716 print_uint(PRINT_ANY
, "chain", " chain %u",
717 action
& TC_ACT_EXT_VAL_MASK
);
718 if (TC_ACT_EXT_CMP(action
, TC_ACT_JUMP
))
719 print_uint(PRINT_ANY
, "jump", " %u",
720 action
& TC_ACT_EXT_VAL_MASK
);
722 print_string(PRINT_FP
, NULL
, "%s", suffix
);
725 int get_linklayer(unsigned int *val
, const char *arg
)
729 if (matches(arg
, "ethernet") == 0)
730 res
= LINKLAYER_ETHERNET
;
731 else if (matches(arg
, "atm") == 0)
733 else if (matches(arg
, "adsl") == 0)
736 return -1; /* Indicate error */
742 void print_linklayer(char *buf
, int len
, unsigned int linklayer
)
745 case LINKLAYER_UNSPEC
:
746 snprintf(buf
, len
, "%s", "unspec");
748 case LINKLAYER_ETHERNET
:
749 snprintf(buf
, len
, "%s", "ethernet");
752 snprintf(buf
, len
, "%s", "atm");
755 snprintf(buf
, len
, "%s", "unknown");
760 char *sprint_linklayer(unsigned int linklayer
, char *buf
)
762 print_linklayer(buf
, SPRINT_BSIZE
-1, linklayer
);
766 void print_tm(FILE *f
, const struct tcf_t
*tm
)
768 int hz
= get_user_hz();
770 if (tm
->install
!= 0) {
771 print_uint(PRINT_JSON
, "installed", NULL
, tm
->install
);
772 print_uint(PRINT_FP
, NULL
, " installed %u sec",
773 (unsigned int)(tm
->install
/hz
));
775 if (tm
->lastuse
!= 0) {
776 print_uint(PRINT_JSON
, "last_used", NULL
, tm
->lastuse
);
777 print_uint(PRINT_FP
, NULL
, " used %u sec",
778 (unsigned int)(tm
->lastuse
/hz
));
780 if (tm
->expires
!= 0) {
781 print_uint(PRINT_JSON
, "expires", NULL
, tm
->expires
);
782 print_uint(PRINT_FP
, NULL
, " expires %u sec",
783 (unsigned int)(tm
->expires
/hz
));
787 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
790 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
792 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
794 if (tbs
[TCA_STATS_BASIC
]) {
795 struct gnet_stats_basic bs
= {0};
797 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]), sizeof(bs
)));
798 print_string(PRINT_FP
, NULL
, "%s", prefix
);
799 print_lluint(PRINT_ANY
, "bytes", "Sent %llu bytes", bs
.bytes
);
800 print_uint(PRINT_ANY
, "packets", " %u pkt", bs
.packets
);
803 if (tbs
[TCA_STATS_QUEUE
]) {
804 struct gnet_stats_queue q
= {0};
806 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
807 print_uint(PRINT_ANY
, "drops", " (dropped %u", q
.drops
);
808 print_uint(PRINT_ANY
, "overlimits", ", overlimits %u",
810 print_uint(PRINT_ANY
, "requeues", " requeues %u) ", q
.requeues
);
813 if (tbs
[TCA_STATS_RATE_EST64
]) {
814 struct gnet_stats_rate_est64 re
= {0};
816 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST64
]),
817 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST64
]),
819 print_string(PRINT_FP
, NULL
, "\n%s", prefix
);
820 print_lluint(PRINT_JSON
, "rate", NULL
, re
.bps
);
821 print_string(PRINT_FP
, NULL
, "rate %s",
822 sprint_rate(re
.bps
, b1
));
823 print_lluint(PRINT_ANY
, "pps", " %llupps", re
.pps
);
824 } else if (tbs
[TCA_STATS_RATE_EST
]) {
825 struct gnet_stats_rate_est re
= {0};
827 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]),
828 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
829 fprintf(fp
, "\n%srate %s %upps ",
830 prefix
, sprint_rate(re
.bps
, b1
), re
.pps
);
831 print_string(PRINT_FP
, NULL
, "\n%s", prefix
);
832 print_uint(PRINT_JSON
, "rate", NULL
, re
.bps
);
833 print_string(PRINT_FP
, NULL
, "rate %s",
834 sprint_rate(re
.bps
, b1
));
835 print_uint(PRINT_ANY
, "pps", " %upps", re
.pps
);
838 if (tbs
[TCA_STATS_QUEUE
]) {
839 struct gnet_stats_queue q
= {0};
841 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
842 if (!tbs
[TCA_STATS_RATE_EST
])
843 print_string(PRINT_FP
, NULL
, "\n%s", prefix
);
844 print_uint(PRINT_JSON
, "backlog", NULL
, q
.backlog
);
845 print_string(PRINT_FP
, NULL
, "backlog %s",
846 sprint_size(q
.backlog
, b1
));
847 print_uint(PRINT_ANY
, "qlen", " %up", q
.qlen
);
848 print_uint(PRINT_ANY
, "requeues", " requeues %u", q
.qlen
);
852 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
855 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
859 if (tb
[TCA_STATS2
]) {
860 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
861 if (xstats
&& NULL
== *xstats
)
865 /* backward compatibility */
867 struct tc_stats st
= {};
869 /* handle case where kernel returns more/less than we know about */
870 memcpy(&st
, RTA_DATA(tb
[TCA_STATS
]), MIN(RTA_PAYLOAD(tb
[TCA_STATS
]), sizeof(st
)));
872 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
873 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
876 if (st
.bps
|| st
.pps
|| st
.qlen
|| st
.backlog
) {
877 fprintf(fp
, "\n%s", prefix
);
878 if (st
.bps
|| st
.pps
) {
879 fprintf(fp
, "rate ");
881 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
883 fprintf(fp
, "%upps ", st
.pps
);
885 if (st
.qlen
|| st
.backlog
) {
886 fprintf(fp
, "backlog ");
888 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
890 fprintf(fp
, "%up ", st
.qlen
);
896 if (tb
[TCA_XSTATS
] && xstats
)
897 *xstats
= tb
[TCA_XSTATS
];