]>
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 static int parse_percent_rate(char *rate
, size_t len
,
194 const char *str
, const char *dev
)
198 double perc
, rate_bit
;
199 char *str_perc
= NULL
;
202 fprintf(stderr
, "No device specified; specify device to rate limit by percentage\n");
206 if (read_prop(dev
, "speed", &dev_mbit
))
209 ret
= sscanf(str
, "%m[0-9.%]", &str_perc
);
213 if (parse_percent(&perc
, str_perc
))
218 if (perc
> 1.0 || perc
< 0.0) {
219 fprintf(stderr
, "Invalid rate specified; should be between [0,100]%% but is %s\n", str
);
223 rate_bit
= perc
* dev_mbit
* 1000 * 1000;
225 ret
= snprintf(rate
, len
, "%lf", rate_bit
);
226 if (ret
<= 0 || ret
>= len
) {
227 fprintf(stderr
, "Unable to parse calculated rate\n");
235 fprintf(stderr
, "Specified rate value could not be read or is malformed\n");
239 int get_percent_rate(unsigned int *rate
, const char *str
, const char *dev
)
243 if (parse_percent_rate(r_str
, sizeof(r_str
), str
, dev
))
246 return get_rate(rate
, r_str
);
249 int get_percent_rate64(__u64
*rate
, const char *str
, const char *dev
)
253 if (parse_percent_rate(r_str
, sizeof(r_str
), str
, dev
))
256 return get_rate64(rate
, r_str
);
259 int get_rate(unsigned int *rate
, const char *str
)
262 double bps
= strtod(str
, &p
);
263 const struct rate_suffix
*s
;
268 for (s
= suffixes
; s
->name
; ++s
) {
269 if (strcasecmp(s
->name
, p
) == 0) {
277 return -1; /* unknown suffix */
279 bps
/= 8; /* -> bytes per second */
281 /* detect if an overflow happened */
282 if (*rate
!= floor(bps
))
287 int get_rate64(__u64
*rate
, const char *str
)
290 double bps
= strtod(str
, &p
);
291 const struct rate_suffix
*s
;
296 for (s
= suffixes
; s
->name
; ++s
) {
297 if (strcasecmp(s
->name
, p
) == 0) {
305 return -1; /* unknown suffix */
307 bps
/= 8; /* -> bytes per second */
312 void print_rate(char *buf
, int len
, __u64 rate
)
315 unsigned long kilo
= use_iec
? 1024 : 1000;
316 const char *str
= use_iec
? "i" : "";
317 static char *units
[5] = {"", "K", "M", "G", "T"};
320 rate
<<= 3; /* bytes/sec -> bits/sec */
322 for (i
= 0; i
< ARRAY_SIZE(units
) - 1; i
++) {
325 if (((rate
% kilo
) != 0) && rate
< 1000*kilo
)
330 snprintf(buf
, len
, "%.0f%s%sbit", (double)rate
, units
[i
], str
);
333 char *sprint_rate(__u64 rate
, char *buf
)
335 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
339 char *sprint_ticks(__u32 ticks
, char *buf
)
341 return sprint_time(tc_core_tick2time(ticks
), buf
);
344 int get_size(unsigned int *size
, const char *str
)
349 sz
= strtod(str
, &p
);
354 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k") == 0)
356 else if (strcasecmp(p
, "gb") == 0 || strcasecmp(p
, "g") == 0)
357 sz
*= 1024*1024*1024;
358 else if (strcasecmp(p
, "gbit") == 0)
359 sz
*= 1024*1024*1024/8;
360 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m") == 0)
362 else if (strcasecmp(p
, "mbit") == 0)
364 else if (strcasecmp(p
, "kbit") == 0)
366 else if (strcasecmp(p
, "b") != 0)
374 int get_size_and_cell(unsigned int *size
, int *cell_log
, char *str
)
376 char *slash
= strchr(str
, '/');
381 if (get_size(size
, str
))
388 if (get_integer(&cell
, slash
+1, 0))
392 for (i
= 0; i
< 32; i
++) {
393 if ((1<<i
) == cell
) {
403 void print_devname(enum output_type type
, int ifindex
)
405 const char *ifname
= ll_index_to_name(ifindex
);
407 if (!is_json_context())
410 print_color_string(type
, COLOR_IFNAME
,
411 "dev", "%s ", ifname
);
414 static void print_size(char *buf
, int len
, __u32 sz
)
418 if (sz
>= 1024*1024 && fabs(1024*1024*rint(tmp
/(1024*1024)) - sz
) < 1024)
419 snprintf(buf
, len
, "%gMb", rint(tmp
/(1024*1024)));
420 else if (sz
>= 1024 && fabs(1024*rint(tmp
/1024) - sz
) < 16)
421 snprintf(buf
, len
, "%gKb", rint(tmp
/1024));
423 snprintf(buf
, len
, "%ub", sz
);
426 char *sprint_size(__u32 size
, char *buf
)
428 print_size(buf
, SPRINT_BSIZE
-1, size
);
432 static const char *action_n2a(int action
)
436 if (TC_ACT_EXT_CMP(action
, TC_ACT_GOTO_CHAIN
))
438 if (TC_ACT_EXT_CMP(action
, TC_ACT_JUMP
))
447 case TC_ACT_RECLASSIFY
:
456 snprintf(buf
, 64, "%d", action
);
461 /* Convert action branch name into numeric format.
464 * @arg - string to parse
465 * @result - pointer to output variable
466 * @allow_num - whether @arg may be in numeric format already
468 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
470 int action_a2n(char *arg
, int *result
, bool allow_num
)
478 {"continue", TC_ACT_UNSPEC
},
479 {"drop", TC_ACT_SHOT
},
480 {"shot", TC_ACT_SHOT
},
483 {"reclassify", TC_ACT_RECLASSIFY
},
484 {"pipe", TC_ACT_PIPE
},
485 {"goto", TC_ACT_GOTO_CHAIN
},
486 {"jump", TC_ACT_JUMP
},
487 {"trap", TC_ACT_TRAP
},
491 for (iter
= a2n
; iter
->a
; iter
++) {
492 if (matches(arg
, iter
->a
) != 0)
497 if (!allow_num
|| sscanf(arg
, "%d%c", &n
, &dummy
) != 1)
506 static int __parse_action_control(int *argc_p
, char ***argv_p
, int *result_p
,
507 bool allow_num
, bool ignore_a2n_miss
)
510 char **argv
= *argv_p
;
515 if (action_a2n(*argv
, &result
, allow_num
) == -1) {
516 if (!ignore_a2n_miss
)
517 fprintf(stderr
, "Bad action type %s\n", *argv
);
520 if (result
== TC_ACT_GOTO_CHAIN
) {
524 if (matches(*argv
, "chain") != 0) {
525 fprintf(stderr
, "\"chain index\" expected\n");
529 if (get_u32(&chain_index
, *argv
, 10) ||
530 chain_index
> TC_ACT_EXT_VAL_MASK
) {
531 fprintf(stderr
, "Illegal \"chain index\"\n");
534 result
|= chain_index
;
536 if (result
== TC_ACT_JUMP
) {
540 if (get_u32(&jump_cnt
, *argv
, 10) ||
541 jump_cnt
> TC_ACT_EXT_VAL_MASK
) {
542 fprintf(stderr
, "Invalid \"jump count\" (%s)\n", *argv
);
554 /* Parse action control including possible options.
557 * @argc_p - pointer to argc to parse
558 * @argv_p - pointer to argv to parse
559 * @result_p - pointer to output variable
560 * @allow_num - whether action may be in numeric format already
562 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
564 int parse_action_control(int *argc_p
, char ***argv_p
,
565 int *result_p
, bool allow_num
)
567 return __parse_action_control(argc_p
, argv_p
, result_p
,
571 /* Parse action control including possible options.
574 * @argc_p - pointer to argc to parse
575 * @argv_p - pointer to argv to parse
576 * @result_p - pointer to output variable
577 * @allow_num - whether action may be in numeric format already
578 * @default_result - set as a result in case of parsing error
580 * In case there is an error during parsing, the default result is used.
582 void parse_action_control_dflt(int *argc_p
, char ***argv_p
,
583 int *result_p
, bool allow_num
,
586 if (__parse_action_control(argc_p
, argv_p
, result_p
, allow_num
, true))
587 *result_p
= default_result
;
590 static int parse_action_control_slash_spaces(int *argc_p
, char ***argv_p
,
591 int *result1_p
, int *result2_p
,
595 char **argv
= *argv_p
;
596 int result1
= -1, result2
;
597 int *result_p
= &result1
;
604 if (strcmp(*argv
, "/") != 0)
609 case 0: /* fall-through */
611 ret
= parse_action_control(&argc
, &argv
,
612 result_p
, allow_num
);
622 *result1_p
= result1
;
624 *result2_p
= result2
;
630 /* Parse action control with slash including possible options.
633 * @argc_p - pointer to argc to parse
634 * @argv_p - pointer to argv to parse
635 * @result1_p - pointer to the first (before slash) output variable
636 * @result2_p - pointer to the second (after slash) output variable
637 * @allow_num - whether action may be in numeric format already
639 * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
641 int parse_action_control_slash(int *argc_p
, char ***argv_p
,
642 int *result1_p
, int *result2_p
, bool allow_num
)
644 int result1
, result2
, argc
= *argc_p
;
645 char **argv
= *argv_p
;
646 char *p
= strchr(*argv
, '/');
649 return parse_action_control_slash_spaces(argc_p
, argv_p
,
650 result1_p
, result2_p
,
653 if (action_a2n(*argv
, &result1
, allow_num
)) {
659 if (action_a2n(p
+ 1, &result2
, allow_num
))
662 *result1_p
= result1
;
663 *result2_p
= result2
;
670 void print_action_control(FILE *f
, const char *prefix
,
671 int action
, const char *suffix
)
673 print_string(PRINT_FP
, NULL
, "%s", prefix
);
674 open_json_object("control_action");
675 print_string(PRINT_ANY
, "type", "%s", action_n2a(action
));
676 if (TC_ACT_EXT_CMP(action
, TC_ACT_GOTO_CHAIN
))
677 print_uint(PRINT_ANY
, "chain", " chain %u",
678 action
& TC_ACT_EXT_VAL_MASK
);
679 if (TC_ACT_EXT_CMP(action
, TC_ACT_JUMP
))
680 print_uint(PRINT_ANY
, "jump", " %u",
681 action
& TC_ACT_EXT_VAL_MASK
);
683 print_string(PRINT_FP
, NULL
, "%s", suffix
);
686 int get_linklayer(unsigned int *val
, const char *arg
)
690 if (matches(arg
, "ethernet") == 0)
691 res
= LINKLAYER_ETHERNET
;
692 else if (matches(arg
, "atm") == 0)
694 else if (matches(arg
, "adsl") == 0)
697 return -1; /* Indicate error */
703 static void print_linklayer(char *buf
, int len
, unsigned int linklayer
)
706 case LINKLAYER_UNSPEC
:
707 snprintf(buf
, len
, "%s", "unspec");
709 case LINKLAYER_ETHERNET
:
710 snprintf(buf
, len
, "%s", "ethernet");
713 snprintf(buf
, len
, "%s", "atm");
716 snprintf(buf
, len
, "%s", "unknown");
721 char *sprint_linklayer(unsigned int linklayer
, char *buf
)
723 print_linklayer(buf
, SPRINT_BSIZE
-1, linklayer
);
727 void print_tm(FILE *f
, const struct tcf_t
*tm
)
729 int hz
= get_user_hz();
731 if (tm
->install
!= 0) {
732 print_uint(PRINT_JSON
, "installed", NULL
, tm
->install
);
733 print_uint(PRINT_FP
, NULL
, " installed %u sec",
734 (unsigned int)(tm
->install
/hz
));
736 if (tm
->lastuse
!= 0) {
737 print_uint(PRINT_JSON
, "last_used", NULL
, tm
->lastuse
);
738 print_uint(PRINT_FP
, NULL
, " used %u sec",
739 (unsigned int)(tm
->lastuse
/hz
));
741 if (tm
->expires
!= 0) {
742 print_uint(PRINT_JSON
, "expires", NULL
, tm
->expires
);
743 print_uint(PRINT_FP
, NULL
, " expires %u sec",
744 (unsigned int)(tm
->expires
/hz
));
748 static void print_tcstats_basic_hw(struct rtattr
**tbs
, char *prefix
)
750 struct gnet_stats_basic bs_hw
;
752 if (!tbs
[TCA_STATS_BASIC_HW
])
755 memcpy(&bs_hw
, RTA_DATA(tbs
[TCA_STATS_BASIC_HW
]),
756 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC_HW
]), sizeof(bs_hw
)));
758 if (bs_hw
.bytes
== 0 && bs_hw
.packets
== 0)
761 if (tbs
[TCA_STATS_BASIC
]) {
762 struct gnet_stats_basic bs
;
764 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]),
765 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]),
768 if (bs
.bytes
>= bs_hw
.bytes
&& bs
.packets
>= bs_hw
.packets
) {
769 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
770 print_string(PRINT_FP
, NULL
, "%s", prefix
);
771 print_lluint(PRINT_ANY
, "sw_bytes",
772 "Sent software %llu bytes",
773 bs
.bytes
- bs_hw
.bytes
);
774 print_uint(PRINT_ANY
, "sw_packets", " %u pkt",
775 bs
.packets
- bs_hw
.packets
);
779 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
780 print_string(PRINT_FP
, NULL
, "%s", prefix
);
781 print_lluint(PRINT_ANY
, "hw_bytes", "Sent hardware %llu bytes",
783 print_uint(PRINT_ANY
, "hw_packets", " %u pkt", bs_hw
.packets
);
786 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
789 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
791 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
793 if (tbs
[TCA_STATS_BASIC
]) {
794 struct gnet_stats_basic bs
= {0};
796 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]), sizeof(bs
)));
797 print_string(PRINT_FP
, NULL
, "%s", prefix
);
798 print_lluint(PRINT_ANY
, "bytes", "Sent %llu bytes", bs
.bytes
);
799 print_uint(PRINT_ANY
, "packets", " %u pkt", bs
.packets
);
802 if (tbs
[TCA_STATS_QUEUE
]) {
803 struct gnet_stats_queue q
= {0};
805 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
806 print_uint(PRINT_ANY
, "drops", " (dropped %u", q
.drops
);
807 print_uint(PRINT_ANY
, "overlimits", ", overlimits %u",
809 print_uint(PRINT_ANY
, "requeues", " requeues %u) ", q
.requeues
);
812 if (tbs
[TCA_STATS_BASIC_HW
])
813 print_tcstats_basic_hw(tbs
, prefix
);
815 if (tbs
[TCA_STATS_RATE_EST64
]) {
816 struct gnet_stats_rate_est64 re
= {0};
818 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST64
]),
819 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST64
]),
821 print_string(PRINT_FP
, NULL
, "\n%s", prefix
);
822 print_lluint(PRINT_JSON
, "rate", NULL
, re
.bps
);
823 print_string(PRINT_FP
, NULL
, "rate %s",
824 sprint_rate(re
.bps
, b1
));
825 print_lluint(PRINT_ANY
, "pps", " %llupps", re
.pps
);
826 } else if (tbs
[TCA_STATS_RATE_EST
]) {
827 struct gnet_stats_rate_est re
= {0};
829 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]),
830 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
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", "");
844 print_uint(PRINT_JSON
, "backlog", NULL
, q
.backlog
);
845 print_string(PRINT_FP
, NULL
, "%s", prefix
);
846 print_string(PRINT_FP
, NULL
, "backlog %s",
847 sprint_size(q
.backlog
, b1
));
848 print_uint(PRINT_ANY
, "qlen", " %up", q
.qlen
);
849 print_uint(PRINT_FP
, NULL
, " requeues %u", q
.requeues
);
853 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
856 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
860 if (tb
[TCA_STATS2
]) {
861 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
862 if (xstats
&& !*xstats
)
866 /* backward compatibility */
868 struct tc_stats st
= {};
870 /* handle case where kernel returns more/less than we know about */
871 memcpy(&st
, RTA_DATA(tb
[TCA_STATS
]), MIN(RTA_PAYLOAD(tb
[TCA_STATS
]), sizeof(st
)));
873 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
874 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
877 if (st
.bps
|| st
.pps
|| st
.qlen
|| st
.backlog
) {
878 fprintf(fp
, "\n%s", prefix
);
879 if (st
.bps
|| st
.pps
) {
880 fprintf(fp
, "rate ");
882 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
884 fprintf(fp
, "%upps ", st
.pps
);
886 if (st
.qlen
|| st
.backlog
) {
887 fprintf(fp
, "backlog ");
889 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
891 fprintf(fp
, "%up ", st
.qlen
);
897 if (tb
[TCA_XSTATS
] && xstats
)
898 *xstats
= tb
[TCA_XSTATS
];