Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 4 | * |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 7 | * Bernhard Reutner-Fischer adjusted for busybox |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 9 | //config:config TC |
| 10 | //config: bool "tc (3.1 kb)" |
| 11 | //config: default y |
| 12 | //config: help |
| 13 | //config: Show / manipulate traffic control settings |
| 14 | //config: |
| 15 | //config:config FEATURE_TC_INGRESS |
| 16 | //config: bool "Enable ingress" |
| 17 | //config: default y |
| 18 | //config: depends on TC |
Denys Vlasenko | 29e2c45 | 2016-11-23 09:51:33 +0100 | [diff] [blame] | 19 | |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 20 | //applet:IF_TC(APPLET(tc, BB_DIR_SBIN, BB_SUID_DROP)) |
| 21 | |
| 22 | //kbuild:lib-$(CONFIG_TC) += tc.o |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 23 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 24 | //usage:#define tc_trivial_usage |
| 25 | /* //usage: "[OPTIONS] OBJECT CMD [dev STRING]" */ |
| 26 | //usage: "OBJECT CMD [dev STRING]" |
| 27 | //usage:#define tc_full_usage "\n\n" |
Denys Vlasenko | 1238988 | 2017-01-21 14:27:07 +0100 | [diff] [blame] | 28 | //usage: "OBJECT: qdisc|class|filter\n" |
| 29 | //usage: "CMD: add|del|change|replace|show\n" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 30 | //usage: "\n" |
Denys Vlasenko | 1238988 | 2017-01-21 14:27:07 +0100 | [diff] [blame] | 31 | //usage: "qdisc [handle QHANDLE] [root|"IF_FEATURE_TC_INGRESS("ingress|")"parent CLASSID]\n" |
| 32 | /* //usage: "[estimator INTERVAL TIME_CONSTANT]\n" */ |
| 33 | //usage: " [[QDISC_KIND] [help|OPTIONS]]\n" |
| 34 | //usage: " QDISC_KIND := [p|b]fifo|tbf|prio|cbq|red|etc.\n" |
| 35 | //usage: "qdisc show [dev STRING]"IF_FEATURE_TC_INGRESS(" [ingress]")"\n" |
| 36 | //usage: "class [classid CLASSID] [root|parent CLASSID]\n" |
| 37 | //usage: " [[QDISC_KIND] [help|OPTIONS] ]\n" |
| 38 | //usage: "class show [ dev STRING ] [root|parent CLASSID]\n" |
| 39 | //usage: "filter [pref PRIO] [protocol PROTO]\n" |
| 40 | /* //usage: "\t[estimator INTERVAL TIME_CONSTANT]\n" */ |
| 41 | //usage: " [root|classid CLASSID] [handle FILTERID]\n" |
| 42 | //usage: " [[FILTER_TYPE] [help|OPTIONS]]\n" |
| 43 | //usage: "filter show [dev STRING] [root|parent CLASSID]" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 44 | |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 45 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 46 | #include "common_bufsiz.h" |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 47 | |
| 48 | #include "libiproute/utils.h" |
| 49 | #include "libiproute/ip_common.h" |
| 50 | #include "libiproute/rt_names.h" |
| 51 | #include <linux/pkt_sched.h> /* for the TC_H_* macros */ |
| 52 | |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 53 | /* This is the deprecated multiqueue interface */ |
| 54 | #ifndef TCA_PRIO_MAX |
| 55 | enum |
| 56 | { |
| 57 | TCA_PRIO_UNSPEC, |
| 58 | TCA_PRIO_MQ, |
| 59 | __TCA_PRIO_MAX |
| 60 | }; |
| 61 | #define TCA_PRIO_MAX (__TCA_PRIO_MAX - 1) |
| 62 | #endif |
| 63 | |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 64 | #define parse_rtattr_nested(tb, max, rta) \ |
| 65 | (parse_rtattr((tb), (max), RTA_DATA(rta), RTA_PAYLOAD(rta))) |
| 66 | |
| 67 | /* nullifies tb on error */ |
| 68 | #define __parse_rtattr_nested_compat(tb, max, rta, len) \ |
| 69 | ({if ((RTA_PAYLOAD(rta) >= len) && \ |
| 70 | (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr))) { \ |
| 71 | rta = RTA_DATA(rta) + RTA_ALIGN(len); \ |
| 72 | parse_rtattr_nested(tb, max, rta); \ |
| 73 | } else \ |
| 74 | memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \ |
| 75 | }) |
| 76 | |
| 77 | #define parse_rtattr_nested_compat(tb, max, rta, data, len) \ |
| 78 | ({data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \ |
| 79 | __parse_rtattr_nested_compat(tb, max, rta, len); }) |
| 80 | |
| 81 | #define show_details (0) /* not implemented. Does anyone need it? */ |
| 82 | #define use_iec (0) /* not currently documented in the upstream manpage */ |
| 83 | |
| 84 | |
| 85 | struct globals { |
| 86 | int filter_ifindex; |
Daniel Fandrich | 6295d27 | 2011-06-09 15:44:44 -0700 | [diff] [blame] | 87 | uint32_t filter_qdisc; |
| 88 | uint32_t filter_parent; |
| 89 | uint32_t filter_prio; |
| 90 | uint32_t filter_proto; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 91 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 92 | #define G (*(struct globals*)bb_common_bufsiz1) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 93 | #define filter_ifindex (G.filter_ifindex) |
| 94 | #define filter_qdisc (G.filter_qdisc) |
| 95 | #define filter_parent (G.filter_parent) |
| 96 | #define filter_prio (G.filter_prio) |
| 97 | #define filter_proto (G.filter_proto) |
Denys Vlasenko | ab3964d | 2015-10-13 14:50:20 +0200 | [diff] [blame] | 98 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 99 | setup_common_bufsiz(); \ |
Denys Vlasenko | ab3964d | 2015-10-13 14:50:20 +0200 | [diff] [blame] | 100 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ |
| 101 | } while (0) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 102 | |
| 103 | /* Allocates a buffer containing the name of a class id. |
| 104 | * The caller must free the returned memory. */ |
| 105 | static char* print_tc_classid(uint32_t cid) |
| 106 | { |
| 107 | #if 0 /* IMPOSSIBLE */ |
| 108 | if (cid == TC_H_ROOT) |
| 109 | return xasprintf("root"); |
| 110 | else |
| 111 | #endif |
| 112 | if (cid == TC_H_UNSPEC) |
| 113 | return xasprintf("none"); |
| 114 | else if (TC_H_MAJ(cid) == 0) |
| 115 | return xasprintf(":%x", TC_H_MIN(cid)); |
| 116 | else if (TC_H_MIN(cid) == 0) |
| 117 | return xasprintf("%x:", TC_H_MAJ(cid)>>16); |
| 118 | else |
| 119 | return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid)); |
| 120 | } |
| 121 | |
| 122 | /* Get a qdisc handle. Return 0 on success, !0 otherwise. */ |
Daniel Fandrich | 6295d27 | 2011-06-09 15:44:44 -0700 | [diff] [blame] | 123 | static int get_qdisc_handle(uint32_t *h, const char *str) { |
| 124 | uint32_t maj; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 125 | char *p; |
| 126 | |
| 127 | maj = TC_H_UNSPEC; |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 128 | if (strcmp(str, "none") == 0) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 129 | goto ok; |
| 130 | maj = strtoul(str, &p, 16); |
| 131 | if (p == str) |
| 132 | return 1; |
| 133 | maj <<= 16; |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 134 | if (*p != ':' && *p != '\0') |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 135 | return 1; |
| 136 | ok: |
| 137 | *h = maj; |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* Get class ID. Return 0 on success, !0 otherwise. */ |
Daniel Fandrich | 6295d27 | 2011-06-09 15:44:44 -0700 | [diff] [blame] | 142 | static int get_tc_classid(uint32_t *h, const char *str) { |
| 143 | uint32_t maj, min; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 144 | char *p; |
| 145 | |
| 146 | maj = TC_H_ROOT; |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 147 | if (strcmp(str, "root") == 0) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 148 | goto ok; |
| 149 | maj = TC_H_UNSPEC; |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 150 | if (strcmp(str, "none") == 0) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 151 | goto ok; |
| 152 | maj = strtoul(str, &p, 16); |
| 153 | if (p == str) { |
| 154 | if (*p != ':') |
| 155 | return 1; |
| 156 | maj = 0; |
| 157 | } |
| 158 | if (*p == ':') { |
| 159 | if (maj >= (1<<16)) |
| 160 | return 1; |
| 161 | maj <<= 16; |
| 162 | str = p + 1; |
| 163 | min = strtoul(str, &p, 16); |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 164 | //FIXME: check for "" too? |
| 165 | if (*p != '\0' || min >= (1<<16)) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 166 | return 1; |
| 167 | maj |= min; |
| 168 | } else if (*p != 0) |
| 169 | return 1; |
| 170 | ok: |
| 171 | *h = maj; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static void print_rate(char *buf, int len, uint32_t rate) |
| 176 | { |
| 177 | double tmp = (double)rate*8; |
| 178 | |
| 179 | if (use_iec) { |
Denys Vlasenko | b878121 | 2015-05-24 18:01:53 +0200 | [diff] [blame] | 180 | if (tmp >= 1000*1024*1024) |
| 181 | snprintf(buf, len, "%.0fMibit", tmp/(1024*1024)); |
| 182 | else if (tmp >= 1000*1024) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 183 | snprintf(buf, len, "%.0fKibit", tmp/1024); |
| 184 | else |
| 185 | snprintf(buf, len, "%.0fbit", tmp); |
| 186 | } else { |
Denys Vlasenko | b878121 | 2015-05-24 18:01:53 +0200 | [diff] [blame] | 187 | if (tmp >= 1000*1000000) |
| 188 | snprintf(buf, len, "%.0fMbit", tmp/1000000); |
| 189 | else if (tmp >= 1000*1000) |
| 190 | snprintf(buf, len, "%.0fKbit", tmp/1000); |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 191 | else |
| 192 | snprintf(buf, len, "%.0fbit", tmp); |
| 193 | } |
| 194 | } |
| 195 | |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 196 | #if 0 |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 197 | /* This is "pfifo_fast". */ |
| 198 | static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n) |
| 199 | { |
| 200 | return 0; |
| 201 | } |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 202 | #endif |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 203 | static int prio_print_opt(struct rtattr *opt) |
| 204 | { |
| 205 | int i; |
| 206 | struct tc_prio_qopt *qopt; |
| 207 | struct rtattr *tb[TCA_PRIO_MAX+1]; |
| 208 | |
| 209 | if (opt == NULL) |
| 210 | return 0; |
| 211 | parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt)); |
| 212 | if (tb == NULL) |
| 213 | return 0; |
| 214 | printf("bands %u priomap ", qopt->bands); |
| 215 | for (i=0; i<=TC_PRIO_MAX; i++) |
| 216 | printf(" %d", qopt->priomap[i]); |
| 217 | |
| 218 | if (tb[TCA_PRIO_MQ]) |
| 219 | printf(" multiqueue: o%s ", |
| 220 | *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff"); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 225 | #if 0 |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 226 | /* Class Based Queue */ |
| 227 | static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n) |
| 228 | { |
| 229 | return 0; |
| 230 | } |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 231 | #endif |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 232 | static int cbq_print_opt(struct rtattr *opt) |
| 233 | { |
| 234 | struct rtattr *tb[TCA_CBQ_MAX+1]; |
| 235 | struct tc_ratespec *r = NULL; |
| 236 | struct tc_cbq_lssopt *lss = NULL; |
| 237 | struct tc_cbq_wrropt *wrr = NULL; |
| 238 | struct tc_cbq_fopt *fopt = NULL; |
| 239 | struct tc_cbq_ovl *ovl = NULL; |
Denys Vlasenko | 695fa51 | 2010-01-06 18:16:39 +0100 | [diff] [blame] | 240 | const char *const error = "CBQ: too short %s opt"; |
| 241 | char buf[64]; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 242 | |
| 243 | if (opt == NULL) |
| 244 | goto done; |
| 245 | parse_rtattr_nested(tb, TCA_CBQ_MAX, opt); |
| 246 | |
| 247 | if (tb[TCA_CBQ_RATE]) { |
| 248 | if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r)) |
| 249 | bb_error_msg(error, "rate"); |
| 250 | else |
| 251 | r = RTA_DATA(tb[TCA_CBQ_RATE]); |
| 252 | } |
| 253 | if (tb[TCA_CBQ_LSSOPT]) { |
| 254 | if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss)) |
| 255 | bb_error_msg(error, "lss"); |
| 256 | else |
| 257 | lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]); |
| 258 | } |
| 259 | if (tb[TCA_CBQ_WRROPT]) { |
| 260 | if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr)) |
| 261 | bb_error_msg(error, "wrr"); |
| 262 | else |
| 263 | wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]); |
| 264 | } |
| 265 | if (tb[TCA_CBQ_FOPT]) { |
| 266 | if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt)) |
| 267 | bb_error_msg(error, "fopt"); |
| 268 | else |
| 269 | fopt = RTA_DATA(tb[TCA_CBQ_FOPT]); |
| 270 | } |
| 271 | if (tb[TCA_CBQ_OVL_STRATEGY]) { |
| 272 | if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl)) |
| 273 | bb_error_msg("CBQ: too short overlimit strategy %u/%u", |
| 274 | (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]), |
| 275 | (unsigned) sizeof(*ovl)); |
| 276 | else |
| 277 | ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]); |
| 278 | } |
| 279 | |
| 280 | if (r) { |
| 281 | print_rate(buf, sizeof(buf), r->rate); |
| 282 | printf("rate %s ", buf); |
| 283 | if (show_details) { |
| 284 | printf("cell %ub ", 1<<r->cell_log); |
| 285 | if (r->mpu) |
| 286 | printf("mpu %ub ", r->mpu); |
| 287 | if (r->overhead) |
| 288 | printf("overhead %ub ", r->overhead); |
| 289 | } |
| 290 | } |
| 291 | if (lss && lss->flags) { |
| 292 | bool comma = false; |
| 293 | bb_putchar('('); |
| 294 | if (lss->flags&TCF_CBQ_LSS_BOUNDED) { |
| 295 | printf("bounded"); |
| 296 | comma = true; |
| 297 | } |
| 298 | if (lss->flags&TCF_CBQ_LSS_ISOLATED) { |
| 299 | if (comma) |
| 300 | bb_putchar(','); |
| 301 | printf("isolated"); |
| 302 | } |
| 303 | printf(") "); |
| 304 | } |
| 305 | if (wrr) { |
| 306 | if (wrr->priority != TC_CBQ_MAXPRIO) |
| 307 | printf("prio %u", wrr->priority); |
| 308 | else |
| 309 | printf("prio no-transmit"); |
| 310 | if (show_details) { |
| 311 | printf("/%u ", wrr->cpriority); |
| 312 | if (wrr->weight != 1) { |
| 313 | print_rate(buf, sizeof(buf), wrr->weight); |
| 314 | printf("weight %s ", buf); |
| 315 | } |
| 316 | if (wrr->allot) |
| 317 | printf("allot %ub ", wrr->allot); |
| 318 | } |
| 319 | } |
| 320 | done: |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 324 | static FAST_FUNC int print_qdisc( |
| 325 | const struct sockaddr_nl *who UNUSED_PARAM, |
| 326 | struct nlmsghdr *hdr, |
| 327 | void *arg UNUSED_PARAM) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 328 | { |
| 329 | struct tcmsg *msg = NLMSG_DATA(hdr); |
| 330 | int len = hdr->nlmsg_len; |
| 331 | struct rtattr * tb[TCA_MAX+1]; |
| 332 | char *name; |
| 333 | |
| 334 | if (hdr->nlmsg_type != RTM_NEWQDISC && hdr->nlmsg_type != RTM_DELQDISC) { |
Denis Vlasenko | 1fd3b38 | 2009-04-29 12:02:57 +0000 | [diff] [blame] | 335 | /* bb_error_msg("not a qdisc"); */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 336 | return 0; /* ??? mimic upstream; should perhaps return -1 */ |
| 337 | } |
| 338 | len -= NLMSG_LENGTH(sizeof(*msg)); |
| 339 | if (len < 0) { |
Denis Vlasenko | 1fd3b38 | 2009-04-29 12:02:57 +0000 | [diff] [blame] | 340 | /* bb_error_msg("wrong len %d", len); */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 341 | return -1; |
| 342 | } |
| 343 | /* not the desired interface? */ |
| 344 | if (filter_ifindex && filter_ifindex != msg->tcm_ifindex) |
| 345 | return 0; |
| 346 | memset (tb, 0, sizeof(tb)); |
| 347 | parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len); |
| 348 | if (tb[TCA_KIND] == NULL) { |
| 349 | /* bb_error_msg("%s: NULL kind", "qdisc"); */ |
| 350 | return -1; |
| 351 | } |
| 352 | if (hdr->nlmsg_type == RTM_DELQDISC) |
| 353 | printf("deleted "); |
| 354 | name = (char*)RTA_DATA(tb[TCA_KIND]); |
| 355 | printf("qdisc %s %x: ", name, msg->tcm_handle>>16); |
| 356 | if (filter_ifindex == 0) |
| 357 | printf("dev %s ", ll_index_to_name(msg->tcm_ifindex)); |
| 358 | if (msg->tcm_parent == TC_H_ROOT) |
| 359 | printf("root "); |
| 360 | else if (msg->tcm_parent) { |
| 361 | char *classid = print_tc_classid(msg->tcm_parent); |
| 362 | printf("parent %s ", classid); |
| 363 | if (ENABLE_FEATURE_CLEAN_UP) |
| 364 | free(classid); |
| 365 | } |
| 366 | if (msg->tcm_info != 1) |
| 367 | printf("refcnt %d ", msg->tcm_info); |
| 368 | if (tb[TCA_OPTIONS]) { |
| 369 | static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0"; |
| 370 | int qqq = index_in_strings(_q_, name); |
| 371 | if (qqq == 0) { /* pfifo_fast aka prio */ |
| 372 | prio_print_opt(tb[TCA_OPTIONS]); |
Denys Vlasenko | bf2af9a | 2009-05-25 04:15:37 +0200 | [diff] [blame] | 373 | } else if (qqq == 1) { /* class based queuing */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 374 | cbq_print_opt(tb[TCA_OPTIONS]); |
| 375 | } else |
| 376 | bb_error_msg("unknown %s", name); |
| 377 | } |
| 378 | bb_putchar('\n'); |
| 379 | return 0; |
| 380 | } |
| 381 | |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 382 | static FAST_FUNC int print_class( |
| 383 | const struct sockaddr_nl *who UNUSED_PARAM, |
| 384 | struct nlmsghdr *hdr, |
| 385 | void *arg UNUSED_PARAM) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 386 | { |
| 387 | struct tcmsg *msg = NLMSG_DATA(hdr); |
| 388 | int len = hdr->nlmsg_len; |
| 389 | struct rtattr * tb[TCA_MAX+1]; |
| 390 | char *name, *classid; |
| 391 | |
| 392 | /*XXX Eventually factor out common code */ |
| 393 | |
| 394 | if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) { |
Denis Vlasenko | 1fd3b38 | 2009-04-29 12:02:57 +0000 | [diff] [blame] | 395 | /* bb_error_msg("not a class"); */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 396 | return 0; /* ??? mimic upstream; should perhaps return -1 */ |
| 397 | } |
| 398 | len -= NLMSG_LENGTH(sizeof(*msg)); |
| 399 | if (len < 0) { |
Denis Vlasenko | 1fd3b38 | 2009-04-29 12:02:57 +0000 | [diff] [blame] | 400 | /* bb_error_msg("wrong len %d", len); */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 401 | return -1; |
| 402 | } |
| 403 | /* not the desired interface? */ |
| 404 | if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc)) |
| 405 | return 0; |
| 406 | memset (tb, 0, sizeof(tb)); |
| 407 | parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len); |
| 408 | if (tb[TCA_KIND] == NULL) { |
| 409 | /* bb_error_msg("%s: NULL kind", "class"); */ |
| 410 | return -1; |
| 411 | } |
| 412 | if (hdr->nlmsg_type == RTM_DELTCLASS) |
| 413 | printf("deleted "); |
| 414 | |
| 415 | name = (char*)RTA_DATA(tb[TCA_KIND]); |
| 416 | classid = !msg->tcm_handle ? NULL : print_tc_classid( |
| 417 | filter_qdisc ? TC_H_MIN(msg->tcm_parent) : msg->tcm_parent); |
| 418 | printf ("class %s %s", name, classid); |
| 419 | if (ENABLE_FEATURE_CLEAN_UP) |
| 420 | free(classid); |
| 421 | |
| 422 | if (filter_ifindex == 0) |
| 423 | printf("dev %s ", ll_index_to_name(msg->tcm_ifindex)); |
| 424 | if (msg->tcm_parent == TC_H_ROOT) |
| 425 | printf("root "); |
| 426 | else if (msg->tcm_parent) { |
| 427 | classid = print_tc_classid(filter_qdisc ? |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 428 | TC_H_MIN(msg->tcm_parent) : msg->tcm_parent); |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 429 | printf("parent %s ", classid); |
| 430 | if (ENABLE_FEATURE_CLEAN_UP) |
| 431 | free(classid); |
| 432 | } |
| 433 | if (msg->tcm_info) |
| 434 | printf("leaf %x ", msg->tcm_info >> 16); |
| 435 | /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])). */ |
| 436 | if (tb[TCA_OPTIONS]) { |
| 437 | static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0"; |
| 438 | int qqq = index_in_strings(_q_, name); |
| 439 | if (qqq == 0) { /* pfifo_fast aka prio */ |
| 440 | /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/ |
Denys Vlasenko | bf2af9a | 2009-05-25 04:15:37 +0200 | [diff] [blame] | 441 | } else if (qqq == 1) { /* class based queuing */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 442 | /* cbq_print_copt() is identical to cbq_print_opt(). */ |
| 443 | cbq_print_opt(tb[TCA_OPTIONS]); |
| 444 | } else |
| 445 | bb_error_msg("unknown %s", name); |
| 446 | } |
| 447 | bb_putchar('\n'); |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 452 | static FAST_FUNC int print_filter( |
| 453 | const struct sockaddr_nl *who UNUSED_PARAM, |
| 454 | struct nlmsghdr *hdr UNUSED_PARAM, |
| 455 | void *arg UNUSED_PARAM) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 456 | { |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 461 | int tc_main(int argc UNUSED_PARAM, char **argv) |
| 462 | { |
| 463 | static const char objects[] ALIGN1 = |
| 464 | "qdisc\0""class\0""filter\0" |
| 465 | ; |
| 466 | enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter }; |
| 467 | static const char commands[] ALIGN1 = |
| 468 | "add\0""delete\0""change\0" |
| 469 | "link\0" /* only qdisc */ |
| 470 | "replace\0" |
| 471 | "show\0""list\0" |
| 472 | ; |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 473 | enum { |
| 474 | CMD_add = 0, CMD_del, CMD_change, |
| 475 | CMD_link, |
| 476 | CMD_replace, |
| 477 | CMD_show |
| 478 | }; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 479 | static const char args[] ALIGN1 = |
| 480 | "dev\0" /* qdisc, class, filter */ |
| 481 | "root\0" /* class, filter */ |
| 482 | "parent\0" /* class, filter */ |
| 483 | "qdisc\0" /* class */ |
| 484 | "handle\0" /* change: qdisc, class(classid) list: filter */ |
| 485 | "classid\0" /* change: for class use "handle" */ |
| 486 | "preference\0""priority\0""protocol\0" /* filter */ |
| 487 | ; |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 488 | enum { |
| 489 | ARG_dev = 0, |
| 490 | ARG_root, |
| 491 | ARG_parent, |
| 492 | ARG_qdisc, |
| 493 | ARG_handle, |
| 494 | ARG_classid, |
| 495 | ARG_pref, ARG_prio, ARG_proto |
| 496 | }; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 497 | struct rtnl_handle rth; |
| 498 | struct tcmsg msg; |
| 499 | int ret, obj, cmd, arg; |
| 500 | char *dev = NULL; |
| 501 | |
| 502 | INIT_G(); |
| 503 | |
| 504 | if (!*++argv) |
| 505 | bb_show_usage(); |
| 506 | xrtnl_open(&rth); |
| 507 | ret = EXIT_SUCCESS; |
| 508 | |
| 509 | obj = index_in_substrings(objects, *argv++); |
| 510 | |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 511 | if (obj < 0) |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 512 | bb_show_usage(); |
| 513 | if (!*argv) |
| 514 | cmd = CMD_show; /* list is the default */ |
| 515 | else { |
| 516 | cmd = index_in_substrings(commands, *argv); |
| 517 | if (cmd < 0) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 518 | invarg_1_to_2(*argv, argv[-1]); |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 519 | argv++; |
| 520 | } |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 521 | |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 522 | memset(&msg, 0, sizeof(msg)); |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 523 | if (AF_UNSPEC != 0) |
| 524 | msg.tcm_family = AF_UNSPEC; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 525 | ll_init_map(&rth); |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 526 | |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 527 | while (*argv) { |
| 528 | arg = index_in_substrings(args, *argv); |
| 529 | if (arg == ARG_dev) { |
| 530 | NEXT_ARG(); |
| 531 | if (dev) |
| 532 | duparg2("dev", *argv); |
| 533 | dev = *argv++; |
| 534 | msg.tcm_ifindex = xll_name_to_index(dev); |
| 535 | if (cmd >= CMD_show) |
| 536 | filter_ifindex = msg.tcm_ifindex; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 537 | } else |
| 538 | if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show) |
| 539 | || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change) |
| 540 | ) { |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 541 | NEXT_ARG(); |
| 542 | /* We don't care about duparg2("qdisc handle",*argv) for now */ |
| 543 | if (get_qdisc_handle(&filter_qdisc, *argv)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 544 | invarg_1_to_2(*argv, "qdisc"); |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 545 | } else |
| 546 | if (obj != OBJ_qdisc |
| 547 | && (arg == ARG_root |
| 548 | || arg == ARG_parent |
| 549 | || (obj == OBJ_filter && arg >= ARG_pref) |
| 550 | ) |
| 551 | ) { |
| 552 | /* nothing */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 553 | } else { |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 554 | invarg_1_to_2(*argv, "command"); |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 555 | } |
| 556 | NEXT_ARG(); |
| 557 | if (arg == ARG_root) { |
| 558 | if (msg.tcm_parent) |
| 559 | duparg("parent", *argv); |
| 560 | msg.tcm_parent = TC_H_ROOT; |
| 561 | if (obj == OBJ_filter) |
| 562 | filter_parent = TC_H_ROOT; |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 563 | } else |
| 564 | if (arg == ARG_parent) { |
Daniel Fandrich | 6295d27 | 2011-06-09 15:44:44 -0700 | [diff] [blame] | 565 | uint32_t handle; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 566 | if (msg.tcm_parent) |
| 567 | duparg(*argv, "parent"); |
| 568 | if (get_tc_classid(&handle, *argv)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 569 | invarg_1_to_2(*argv, "parent"); |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 570 | msg.tcm_parent = handle; |
| 571 | if (obj == OBJ_filter) |
| 572 | filter_parent = handle; |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 573 | } else |
| 574 | if (arg == ARG_handle) { /* filter::list */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 575 | if (msg.tcm_handle) |
| 576 | duparg(*argv, "handle"); |
| 577 | /* reject LONG_MIN || LONG_MAX */ |
| 578 | /* TODO: for fw |
Denys Vlasenko | 60cb48c | 2013-01-14 15:57:44 +0100 | [diff] [blame] | 579 | slash = strchr(handle, '/'); |
| 580 | if (slash != NULL) |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 581 | *slash = '\0'; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 582 | */ |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 583 | msg.tcm_handle = get_u32(*argv, "handle"); |
Daniel Fandrich | 6295d27 | 2011-06-09 15:44:44 -0700 | [diff] [blame] | 584 | /* if (slash) {if (get_u32(uint32_t &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */ |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 585 | } else |
| 586 | if (arg == ARG_classid |
| 587 | && obj == OBJ_class |
| 588 | && cmd == CMD_change |
| 589 | ) { |
| 590 | /* TODO */ |
| 591 | } else |
| 592 | if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */ |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 593 | if (filter_prio) |
| 594 | duparg(*argv, "priority"); |
Denis Vlasenko | 76140a7 | 2009-03-05 09:21:57 +0000 | [diff] [blame] | 595 | filter_prio = get_u32(*argv, "priority"); |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 596 | } else |
| 597 | if (arg == ARG_proto) { /* filter::list */ |
Denys Vlasenko | 0568b6e | 2009-08-08 03:20:12 +0200 | [diff] [blame] | 598 | uint16_t tmp; |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 599 | if (filter_proto) |
| 600 | duparg(*argv, "protocol"); |
| 601 | if (ll_proto_a2n(&tmp, *argv)) |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 602 | invarg_1_to_2(*argv, "protocol"); |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 603 | filter_proto = tmp; |
| 604 | } |
| 605 | } |
Denys Vlasenko | a034cab | 2018-01-04 10:27:51 +0100 | [diff] [blame] | 606 | |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 607 | if (cmd >= CMD_show) { /* show or list */ |
| 608 | if (obj == OBJ_filter) |
| 609 | msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto); |
| 610 | if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC : |
| 611 | obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER, |
| 612 | &msg, sizeof(msg)) < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 613 | bb_simple_perror_msg_and_die("can't send dump request"); |
Bernhard Reutner-Fischer | 0901c51 | 2008-09-04 13:22:58 +0000 | [diff] [blame] | 614 | |
| 615 | xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc : |
| 616 | obj == OBJ_class ? print_class : print_filter, |
| 617 | NULL); |
| 618 | } |
| 619 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 620 | rtnl_close(&rth); |
| 621 | } |
| 622 | return ret; |
| 623 | } |