]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_util.c
53d15e08e9734e48ae0983b86a36a6096d1af700
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 /* Parse a percent e.g: '30%'
194 * return: 0 = ok, -1 = error, 1 = out of range
196 int parse_percent(double *val
, const char *str
)
200 *val
= strtod(str
, &p
) / 100.;
201 if (*val
== HUGE_VALF
|| *val
== HUGE_VALL
)
203 if (*p
&& strcmp(p
, "%"))
209 static int parse_percent_rate(char *rate
, size_t len
,
210 const char *str
, const char *dev
)
214 double perc
, rate_bit
;
215 char *str_perc
= NULL
;
218 fprintf(stderr
, "No device specified; specify device to rate limit by percentage\n");
222 if (read_prop(dev
, "speed", &dev_mbit
))
225 ret
= sscanf(str
, "%m[0-9.%]", &str_perc
);
229 if (parse_percent(&perc
, str_perc
))
234 if (perc
> 1.0 || perc
< 0.0) {
235 fprintf(stderr
, "Invalid rate specified; should be between [0,100]%% but is %s\n", str
);
239 rate_bit
= perc
* dev_mbit
* 1000 * 1000;
241 ret
= snprintf(rate
, len
, "%lf", rate_bit
);
242 if (ret
<= 0 || ret
>= len
) {
243 fprintf(stderr
, "Unable to parse calculated rate\n");
251 fprintf(stderr
, "Specified rate value could not be read or is malformed\n");
255 int get_percent_rate(unsigned int *rate
, const char *str
, const char *dev
)
259 if (parse_percent_rate(r_str
, sizeof(r_str
), str
, dev
))
262 return get_rate(rate
, r_str
);
265 int get_percent_rate64(__u64
*rate
, const char *str
, const char *dev
)
269 if (parse_percent_rate(r_str
, sizeof(r_str
), str
, dev
))
272 return get_rate64(rate
, r_str
);
275 int get_rate(unsigned int *rate
, const char *str
)
278 double bps
= strtod(str
, &p
);
279 const struct rate_suffix
*s
;
284 for (s
= suffixes
; s
->name
; ++s
) {
285 if (strcasecmp(s
->name
, p
) == 0) {
293 return -1; /* unknown suffix */
295 bps
/= 8; /* -> bytes per second */
297 /* detect if an overflow happened */
298 if (*rate
!= floor(bps
))
303 int get_rate64(__u64
*rate
, const char *str
)
306 double bps
= strtod(str
, &p
);
307 const struct rate_suffix
*s
;
312 for (s
= suffixes
; s
->name
; ++s
) {
313 if (strcasecmp(s
->name
, p
) == 0) {
321 return -1; /* unknown suffix */
323 bps
/= 8; /* -> bytes per second */
328 void print_rate(char *buf
, int len
, __u64 rate
)
331 unsigned long kilo
= use_iec
? 1024 : 1000;
332 const char *str
= use_iec
? "i" : "";
333 static char *units
[5] = {"", "K", "M", "G", "T"};
336 rate
<<= 3; /* bytes/sec -> bits/sec */
338 for (i
= 0; i
< ARRAY_SIZE(units
) - 1; i
++) {
341 if (((rate
% kilo
) != 0) && rate
< 1000*kilo
)
346 snprintf(buf
, len
, "%.0f%s%sbit", (double)rate
, units
[i
], str
);
349 char *sprint_rate(__u64 rate
, char *buf
)
351 print_rate(buf
, SPRINT_BSIZE
-1, rate
);
355 char *sprint_ticks(__u32 ticks
, char *buf
)
357 return sprint_time(tc_core_tick2time(ticks
), buf
);
360 int get_size(unsigned int *size
, const char *str
)
365 sz
= strtod(str
, &p
);
370 if (strcasecmp(p
, "kb") == 0 || strcasecmp(p
, "k") == 0)
372 else if (strcasecmp(p
, "gb") == 0 || strcasecmp(p
, "g") == 0)
373 sz
*= 1024*1024*1024;
374 else if (strcasecmp(p
, "gbit") == 0)
375 sz
*= 1024*1024*1024/8;
376 else if (strcasecmp(p
, "mb") == 0 || strcasecmp(p
, "m") == 0)
378 else if (strcasecmp(p
, "mbit") == 0)
380 else if (strcasecmp(p
, "kbit") == 0)
382 else if (strcasecmp(p
, "b") != 0)
390 int get_size_and_cell(unsigned int *size
, int *cell_log
, char *str
)
392 char *slash
= strchr(str
, '/');
397 if (get_size(size
, str
))
404 if (get_integer(&cell
, slash
+1, 0))
408 for (i
= 0; i
< 32; i
++) {
409 if ((1<<i
) == cell
) {
419 void print_devname(enum output_type type
, int ifindex
)
421 const char *ifname
= ll_index_to_name(ifindex
);
423 if (!is_json_context())
426 print_color_string(type
, COLOR_IFNAME
,
427 "dev", "%s ", ifname
);
430 static void print_size(char *buf
, int len
, __u32 sz
)
434 if (sz
>= 1024*1024 && fabs(1024*1024*rint(tmp
/(1024*1024)) - sz
) < 1024)
435 snprintf(buf
, len
, "%gMb", rint(tmp
/(1024*1024)));
436 else if (sz
>= 1024 && fabs(1024*rint(tmp
/1024) - sz
) < 16)
437 snprintf(buf
, len
, "%gKb", rint(tmp
/1024));
439 snprintf(buf
, len
, "%ub", sz
);
442 char *sprint_size(__u32 size
, char *buf
)
444 print_size(buf
, SPRINT_BSIZE
-1, size
);
448 static const char *action_n2a(int action
)
452 if (TC_ACT_EXT_CMP(action
, TC_ACT_GOTO_CHAIN
))
454 if (TC_ACT_EXT_CMP(action
, TC_ACT_JUMP
))
463 case TC_ACT_RECLASSIFY
:
472 snprintf(buf
, 64, "%d", action
);
477 /* Convert action branch name into numeric format.
480 * @arg - string to parse
481 * @result - pointer to output variable
482 * @allow_num - whether @arg may be in numeric format already
484 * In error case, returns -1 and does not touch @result. Otherwise returns 0.
486 int action_a2n(char *arg
, int *result
, bool allow_num
)
494 {"continue", TC_ACT_UNSPEC
},
495 {"drop", TC_ACT_SHOT
},
496 {"shot", TC_ACT_SHOT
},
499 {"reclassify", TC_ACT_RECLASSIFY
},
500 {"pipe", TC_ACT_PIPE
},
501 {"goto", TC_ACT_GOTO_CHAIN
},
502 {"jump", TC_ACT_JUMP
},
503 {"trap", TC_ACT_TRAP
},
507 for (iter
= a2n
; iter
->a
; iter
++) {
508 if (matches(arg
, iter
->a
) != 0)
513 if (!allow_num
|| sscanf(arg
, "%d%c", &n
, &dummy
) != 1)
522 static int __parse_action_control(int *argc_p
, char ***argv_p
, int *result_p
,
523 bool allow_num
, bool ignore_a2n_miss
)
526 char **argv
= *argv_p
;
531 if (action_a2n(*argv
, &result
, allow_num
) == -1) {
532 if (!ignore_a2n_miss
)
533 fprintf(stderr
, "Bad action type %s\n", *argv
);
536 if (result
== TC_ACT_GOTO_CHAIN
) {
540 if (matches(*argv
, "chain") != 0) {
541 fprintf(stderr
, "\"chain index\" expected\n");
545 if (get_u32(&chain_index
, *argv
, 10) ||
546 chain_index
> TC_ACT_EXT_VAL_MASK
) {
547 fprintf(stderr
, "Illegal \"chain index\"\n");
550 result
|= chain_index
;
552 if (result
== TC_ACT_JUMP
) {
556 if (get_u32(&jump_cnt
, *argv
, 10) ||
557 jump_cnt
> TC_ACT_EXT_VAL_MASK
) {
558 fprintf(stderr
, "Invalid \"jump count\" (%s)\n", *argv
);
570 /* Parse action control including possible options.
573 * @argc_p - pointer to argc to parse
574 * @argv_p - pointer to argv to parse
575 * @result_p - pointer to output variable
576 * @allow_num - whether action may be in numeric format already
578 * In error case, returns -1 and does not touch @result_1p. Otherwise returns 0.
580 int parse_action_control(int *argc_p
, char ***argv_p
,
581 int *result_p
, bool allow_num
)
583 return __parse_action_control(argc_p
, argv_p
, result_p
,
587 /* Parse action control including possible options.
590 * @argc_p - pointer to argc to parse
591 * @argv_p - pointer to argv to parse
592 * @result_p - pointer to output variable
593 * @allow_num - whether action may be in numeric format already
594 * @default_result - set as a result in case of parsing error
596 * In case there is an error during parsing, the default result is used.
598 void parse_action_control_dflt(int *argc_p
, char ***argv_p
,
599 int *result_p
, bool allow_num
,
602 if (__parse_action_control(argc_p
, argv_p
, result_p
, allow_num
, true))
603 *result_p
= default_result
;
606 static int parse_action_control_slash_spaces(int *argc_p
, char ***argv_p
,
607 int *result1_p
, int *result2_p
,
611 char **argv
= *argv_p
;
612 int result1
= -1, result2
;
613 int *result_p
= &result1
;
620 if (strcmp(*argv
, "/") != 0)
625 case 0: /* fall-through */
627 ret
= parse_action_control(&argc
, &argv
,
628 result_p
, allow_num
);
638 *result1_p
= result1
;
640 *result2_p
= result2
;
646 /* Parse action control with slash including possible options.
649 * @argc_p - pointer to argc to parse
650 * @argv_p - pointer to argv to parse
651 * @result1_p - pointer to the first (before slash) output variable
652 * @result2_p - pointer to the second (after slash) output variable
653 * @allow_num - whether action may be in numeric format already
655 * In error case, returns -1 and does not touch @result*. Otherwise returns 0.
657 int parse_action_control_slash(int *argc_p
, char ***argv_p
,
658 int *result1_p
, int *result2_p
, bool allow_num
)
660 int result1
, result2
, argc
= *argc_p
;
661 char **argv
= *argv_p
;
662 char *p
= strchr(*argv
, '/');
665 return parse_action_control_slash_spaces(argc_p
, argv_p
,
666 result1_p
, result2_p
,
669 if (action_a2n(*argv
, &result1
, allow_num
)) {
675 if (action_a2n(p
+ 1, &result2
, allow_num
))
678 *result1_p
= result1
;
679 *result2_p
= result2
;
686 void print_action_control(FILE *f
, const char *prefix
,
687 int action
, const char *suffix
)
689 print_string(PRINT_FP
, NULL
, "%s", prefix
);
690 open_json_object("control_action");
691 print_string(PRINT_ANY
, "type", "%s", action_n2a(action
));
692 if (TC_ACT_EXT_CMP(action
, TC_ACT_GOTO_CHAIN
))
693 print_uint(PRINT_ANY
, "chain", " chain %u",
694 action
& TC_ACT_EXT_VAL_MASK
);
695 if (TC_ACT_EXT_CMP(action
, TC_ACT_JUMP
))
696 print_uint(PRINT_ANY
, "jump", " %u",
697 action
& TC_ACT_EXT_VAL_MASK
);
699 print_string(PRINT_FP
, NULL
, "%s", suffix
);
702 int get_linklayer(unsigned int *val
, const char *arg
)
706 if (matches(arg
, "ethernet") == 0)
707 res
= LINKLAYER_ETHERNET
;
708 else if (matches(arg
, "atm") == 0)
710 else if (matches(arg
, "adsl") == 0)
713 return -1; /* Indicate error */
719 static void print_linklayer(char *buf
, int len
, unsigned int linklayer
)
722 case LINKLAYER_UNSPEC
:
723 snprintf(buf
, len
, "%s", "unspec");
725 case LINKLAYER_ETHERNET
:
726 snprintf(buf
, len
, "%s", "ethernet");
729 snprintf(buf
, len
, "%s", "atm");
732 snprintf(buf
, len
, "%s", "unknown");
737 char *sprint_linklayer(unsigned int linklayer
, char *buf
)
739 print_linklayer(buf
, SPRINT_BSIZE
-1, linklayer
);
743 void print_tm(FILE *f
, const struct tcf_t
*tm
)
745 int hz
= get_user_hz();
747 if (tm
->install
!= 0) {
748 print_uint(PRINT_JSON
, "installed", NULL
, tm
->install
);
749 print_uint(PRINT_FP
, NULL
, " installed %u sec",
750 (unsigned int)(tm
->install
/hz
));
752 if (tm
->lastuse
!= 0) {
753 print_uint(PRINT_JSON
, "last_used", NULL
, tm
->lastuse
);
754 print_uint(PRINT_FP
, NULL
, " used %u sec",
755 (unsigned int)(tm
->lastuse
/hz
));
757 if (tm
->expires
!= 0) {
758 print_uint(PRINT_JSON
, "expires", NULL
, tm
->expires
);
759 print_uint(PRINT_FP
, NULL
, " expires %u sec",
760 (unsigned int)(tm
->expires
/hz
));
764 static void print_tcstats_basic_hw(struct rtattr
**tbs
, char *prefix
)
766 struct gnet_stats_basic bs_hw
;
768 if (!tbs
[TCA_STATS_BASIC_HW
])
771 memcpy(&bs_hw
, RTA_DATA(tbs
[TCA_STATS_BASIC_HW
]),
772 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC_HW
]), sizeof(bs_hw
)));
774 if (bs_hw
.bytes
== 0 && bs_hw
.packets
== 0)
777 if (tbs
[TCA_STATS_BASIC
]) {
778 struct gnet_stats_basic bs
;
780 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]),
781 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]),
784 if (bs
.bytes
>= bs_hw
.bytes
&& bs
.packets
>= bs_hw
.packets
) {
785 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
786 print_string(PRINT_FP
, NULL
, "%s", prefix
);
787 print_lluint(PRINT_ANY
, "sw_bytes",
788 "Sent software %llu bytes",
789 bs
.bytes
- bs_hw
.bytes
);
790 print_uint(PRINT_ANY
, "sw_packets", " %u pkt",
791 bs
.packets
- bs_hw
.packets
);
795 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
796 print_string(PRINT_FP
, NULL
, "%s", prefix
);
797 print_lluint(PRINT_ANY
, "hw_bytes", "Sent hardware %llu bytes",
799 print_uint(PRINT_ANY
, "hw_packets", " %u pkt", bs_hw
.packets
);
802 void print_tcstats2_attr(FILE *fp
, struct rtattr
*rta
, char *prefix
, struct rtattr
**xstats
)
805 struct rtattr
*tbs
[TCA_STATS_MAX
+ 1];
807 parse_rtattr_nested(tbs
, TCA_STATS_MAX
, rta
);
809 if (tbs
[TCA_STATS_BASIC
]) {
810 struct gnet_stats_basic bs
= {0};
812 memcpy(&bs
, RTA_DATA(tbs
[TCA_STATS_BASIC
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_BASIC
]), sizeof(bs
)));
813 print_string(PRINT_FP
, NULL
, "%s", prefix
);
814 print_lluint(PRINT_ANY
, "bytes", "Sent %llu bytes", bs
.bytes
);
815 print_uint(PRINT_ANY
, "packets", " %u pkt", bs
.packets
);
818 if (tbs
[TCA_STATS_QUEUE
]) {
819 struct gnet_stats_queue q
= {0};
821 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
822 print_uint(PRINT_ANY
, "drops", " (dropped %u", q
.drops
);
823 print_uint(PRINT_ANY
, "overlimits", ", overlimits %u",
825 print_uint(PRINT_ANY
, "requeues", " requeues %u) ", q
.requeues
);
828 if (tbs
[TCA_STATS_BASIC_HW
])
829 print_tcstats_basic_hw(tbs
, prefix
);
831 if (tbs
[TCA_STATS_RATE_EST64
]) {
832 struct gnet_stats_rate_est64 re
= {0};
834 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST64
]),
835 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST64
]),
837 print_string(PRINT_FP
, NULL
, "\n%s", prefix
);
838 print_lluint(PRINT_JSON
, "rate", NULL
, re
.bps
);
839 print_string(PRINT_FP
, NULL
, "rate %s",
840 sprint_rate(re
.bps
, b1
));
841 print_lluint(PRINT_ANY
, "pps", " %llupps", re
.pps
);
842 } else if (tbs
[TCA_STATS_RATE_EST
]) {
843 struct gnet_stats_rate_est re
= {0};
845 memcpy(&re
, RTA_DATA(tbs
[TCA_STATS_RATE_EST
]),
846 MIN(RTA_PAYLOAD(tbs
[TCA_STATS_RATE_EST
]), sizeof(re
)));
847 print_string(PRINT_FP
, NULL
, "\n%s", prefix
);
848 print_uint(PRINT_JSON
, "rate", NULL
, re
.bps
);
849 print_string(PRINT_FP
, NULL
, "rate %s",
850 sprint_rate(re
.bps
, b1
));
851 print_uint(PRINT_ANY
, "pps", " %upps", re
.pps
);
854 if (tbs
[TCA_STATS_QUEUE
]) {
855 struct gnet_stats_queue q
= {0};
857 memcpy(&q
, RTA_DATA(tbs
[TCA_STATS_QUEUE
]), MIN(RTA_PAYLOAD(tbs
[TCA_STATS_QUEUE
]), sizeof(q
)));
858 if (!tbs
[TCA_STATS_RATE_EST
])
859 print_string(PRINT_FP
, NULL
, "\n", "");
860 print_uint(PRINT_JSON
, "backlog", NULL
, q
.backlog
);
861 print_string(PRINT_FP
, NULL
, "%s", prefix
);
862 print_string(PRINT_FP
, NULL
, "backlog %s",
863 sprint_size(q
.backlog
, b1
));
864 print_uint(PRINT_ANY
, "qlen", " %up", q
.qlen
);
865 print_uint(PRINT_FP
, NULL
, " requeues %u", q
.requeues
);
869 *xstats
= tbs
[TCA_STATS_APP
] ? : NULL
;
872 void print_tcstats_attr(FILE *fp
, struct rtattr
*tb
[], char *prefix
, struct rtattr
**xstats
)
876 if (tb
[TCA_STATS2
]) {
877 print_tcstats2_attr(fp
, tb
[TCA_STATS2
], prefix
, xstats
);
878 if (xstats
&& !*xstats
)
882 /* backward compatibility */
884 struct tc_stats st
= {};
886 /* handle case where kernel returns more/less than we know about */
887 memcpy(&st
, RTA_DATA(tb
[TCA_STATS
]), MIN(RTA_PAYLOAD(tb
[TCA_STATS
]), sizeof(st
)));
889 fprintf(fp
, "%sSent %llu bytes %u pkts (dropped %u, overlimits %u) ",
890 prefix
, (unsigned long long)st
.bytes
, st
.packets
, st
.drops
,
893 if (st
.bps
|| st
.pps
|| st
.qlen
|| st
.backlog
) {
894 fprintf(fp
, "\n%s", prefix
);
895 if (st
.bps
|| st
.pps
) {
896 fprintf(fp
, "rate ");
898 fprintf(fp
, "%s ", sprint_rate(st
.bps
, b1
));
900 fprintf(fp
, "%upps ", st
.pps
);
902 if (st
.qlen
|| st
.backlog
) {
903 fprintf(fp
, "backlog ");
905 fprintf(fp
, "%s ", sprint_size(st
.backlog
, b1
));
907 fprintf(fp
, "%up ", st
.qlen
);
913 if (tb
[TCA_XSTATS
] && xstats
)
914 *xstats
= tb
[TCA_XSTATS
];