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