blob: 4fa3e47bff2c8e7bb3febfed2189814b75515539 [file] [log] [blame]
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00004 *
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00006 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00007 * Bernhard Reutner-Fischer adjusted for busybox
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +00008 */
Denys Vlasenkoa034cab2018-01-04 10:27:51 +01009//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 Vlasenko29e2c452016-11-23 09:51:33 +010019
Denys Vlasenkoa034cab2018-01-04 10:27:51 +010020//applet:IF_TC(APPLET(tc, BB_DIR_SBIN, BB_SUID_DROP))
21
22//kbuild:lib-$(CONFIG_TC) += tc.o
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000023
Pere Orga5bc8c002011-04-11 03:29:49 +020024//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 Vlasenko12389882017-01-21 14:27:07 +010028//usage: "OBJECT: qdisc|class|filter\n"
29//usage: "CMD: add|del|change|replace|show\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage: "\n"
Denys Vlasenko12389882017-01-21 14:27:07 +010031//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 Orga5bc8c002011-04-11 03:29:49 +020044
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000045#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020046#include "common_bufsiz.h"
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000047
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 Vlasenkoa034cab2018-01-04 10:27:51 +010053/* This is the deprecated multiqueue interface */
54#ifndef TCA_PRIO_MAX
55enum
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-Fischer0901c512008-09-04 13:22:58 +000064#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) \
Denys Vlasenko68ae5422018-02-08 08:42:37 +010069({ \
70 if ((RTA_PAYLOAD(rta) >= len) \
71 && (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) \
72 ) { \
73 rta = RTA_DATA(rta) + RTA_ALIGN(len); \
74 parse_rtattr_nested(tb, max, rta); \
75 } else \
76 memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); \
77})
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000078
79#define parse_rtattr_nested_compat(tb, max, rta, data, len) \
Denys Vlasenko68ae5422018-02-08 08:42:37 +010080({ \
81 data = RTA_PAYLOAD(rta) >= len ? RTA_DATA(rta) : NULL; \
82 __parse_rtattr_nested_compat(tb, max, rta, len); \
83})
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000084
85#define show_details (0) /* not implemented. Does anyone need it? */
86#define use_iec (0) /* not currently documented in the upstream manpage */
87
88
89struct globals {
90 int filter_ifindex;
Daniel Fandrich6295d272011-06-09 15:44:44 -070091 uint32_t filter_qdisc;
92 uint32_t filter_parent;
93 uint32_t filter_prio;
94 uint32_t filter_proto;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010095} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020096#define G (*(struct globals*)bb_common_bufsiz1)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +000097#define filter_ifindex (G.filter_ifindex)
98#define filter_qdisc (G.filter_qdisc)
99#define filter_parent (G.filter_parent)
100#define filter_prio (G.filter_prio)
101#define filter_proto (G.filter_proto)
Denys Vlasenkoab3964d2015-10-13 14:50:20 +0200102#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200103 setup_common_bufsiz(); \
Denys Vlasenkoab3964d2015-10-13 14:50:20 +0200104 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
105} while (0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000106
107/* Allocates a buffer containing the name of a class id.
108 * The caller must free the returned memory. */
109static char* print_tc_classid(uint32_t cid)
110{
111#if 0 /* IMPOSSIBLE */
112 if (cid == TC_H_ROOT)
113 return xasprintf("root");
114 else
115#endif
116 if (cid == TC_H_UNSPEC)
117 return xasprintf("none");
118 else if (TC_H_MAJ(cid) == 0)
119 return xasprintf(":%x", TC_H_MIN(cid));
120 else if (TC_H_MIN(cid) == 0)
121 return xasprintf("%x:", TC_H_MAJ(cid)>>16);
122 else
123 return xasprintf("%x:%x", TC_H_MAJ(cid)>>16, TC_H_MIN(cid));
124}
125
126/* Get a qdisc handle. Return 0 on success, !0 otherwise. */
Daniel Fandrich6295d272011-06-09 15:44:44 -0700127static int get_qdisc_handle(uint32_t *h, const char *str) {
128 uint32_t maj;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000129 char *p;
130
131 maj = TC_H_UNSPEC;
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100132 if (strcmp(str, "none") == 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000133 goto ok;
134 maj = strtoul(str, &p, 16);
135 if (p == str)
136 return 1;
137 maj <<= 16;
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200138 if (*p != ':' && *p != '\0')
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000139 return 1;
140 ok:
141 *h = maj;
142 return 0;
143}
144
145/* Get class ID. Return 0 on success, !0 otherwise. */
Daniel Fandrich6295d272011-06-09 15:44:44 -0700146static int get_tc_classid(uint32_t *h, const char *str) {
147 uint32_t maj, min;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000148 char *p;
149
150 maj = TC_H_ROOT;
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100151 if (strcmp(str, "root") == 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000152 goto ok;
153 maj = TC_H_UNSPEC;
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100154 if (strcmp(str, "none") == 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000155 goto ok;
156 maj = strtoul(str, &p, 16);
157 if (p == str) {
158 if (*p != ':')
159 return 1;
160 maj = 0;
161 }
162 if (*p == ':') {
163 if (maj >= (1<<16))
164 return 1;
165 maj <<= 16;
166 str = p + 1;
167 min = strtoul(str, &p, 16);
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200168//FIXME: check for "" too?
169 if (*p != '\0' || min >= (1<<16))
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000170 return 1;
171 maj |= min;
172 } else if (*p != 0)
173 return 1;
174 ok:
175 *h = maj;
176 return 0;
177}
178
179static void print_rate(char *buf, int len, uint32_t rate)
180{
181 double tmp = (double)rate*8;
182
183 if (use_iec) {
Denys Vlasenkob8781212015-05-24 18:01:53 +0200184 if (tmp >= 1000*1024*1024)
185 snprintf(buf, len, "%.0fMibit", tmp/(1024*1024));
186 else if (tmp >= 1000*1024)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000187 snprintf(buf, len, "%.0fKibit", tmp/1024);
188 else
189 snprintf(buf, len, "%.0fbit", tmp);
190 } else {
Denys Vlasenkob8781212015-05-24 18:01:53 +0200191 if (tmp >= 1000*1000000)
192 snprintf(buf, len, "%.0fMbit", tmp/1000000);
193 else if (tmp >= 1000*1000)
194 snprintf(buf, len, "%.0fKbit", tmp/1000);
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000195 else
196 snprintf(buf, len, "%.0fbit", tmp);
197 }
198}
199
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100200#if 0
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000201/* This is "pfifo_fast". */
202static int prio_parse_opt(int argc, char **argv, struct nlmsghdr *n)
203{
204 return 0;
205}
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100206#endif
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000207static int prio_print_opt(struct rtattr *opt)
208{
209 int i;
210 struct tc_prio_qopt *qopt;
211 struct rtattr *tb[TCA_PRIO_MAX+1];
212
213 if (opt == NULL)
214 return 0;
215 parse_rtattr_nested_compat(tb, TCA_PRIO_MAX, opt, qopt, sizeof(*qopt));
216 if (tb == NULL)
217 return 0;
218 printf("bands %u priomap ", qopt->bands);
219 for (i=0; i<=TC_PRIO_MAX; i++)
220 printf(" %d", qopt->priomap[i]);
221
222 if (tb[TCA_PRIO_MQ])
223 printf(" multiqueue: o%s ",
224 *(unsigned char *)RTA_DATA(tb[TCA_PRIO_MQ]) ? "n" : "ff");
225
226 return 0;
227}
228
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100229#if 0
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000230/* Class Based Queue */
231static int cbq_parse_opt(int argc, char **argv, struct nlmsghdr *n)
232{
233 return 0;
234}
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100235#endif
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000236static int cbq_print_opt(struct rtattr *opt)
237{
238 struct rtattr *tb[TCA_CBQ_MAX+1];
239 struct tc_ratespec *r = NULL;
240 struct tc_cbq_lssopt *lss = NULL;
241 struct tc_cbq_wrropt *wrr = NULL;
242 struct tc_cbq_fopt *fopt = NULL;
243 struct tc_cbq_ovl *ovl = NULL;
Denys Vlasenko695fa512010-01-06 18:16:39 +0100244 const char *const error = "CBQ: too short %s opt";
245 char buf[64];
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000246
247 if (opt == NULL)
248 goto done;
249 parse_rtattr_nested(tb, TCA_CBQ_MAX, opt);
250
251 if (tb[TCA_CBQ_RATE]) {
252 if (RTA_PAYLOAD(tb[TCA_CBQ_RATE]) < sizeof(*r))
253 bb_error_msg(error, "rate");
254 else
255 r = RTA_DATA(tb[TCA_CBQ_RATE]);
256 }
257 if (tb[TCA_CBQ_LSSOPT]) {
258 if (RTA_PAYLOAD(tb[TCA_CBQ_LSSOPT]) < sizeof(*lss))
259 bb_error_msg(error, "lss");
260 else
261 lss = RTA_DATA(tb[TCA_CBQ_LSSOPT]);
262 }
263 if (tb[TCA_CBQ_WRROPT]) {
264 if (RTA_PAYLOAD(tb[TCA_CBQ_WRROPT]) < sizeof(*wrr))
265 bb_error_msg(error, "wrr");
266 else
267 wrr = RTA_DATA(tb[TCA_CBQ_WRROPT]);
268 }
269 if (tb[TCA_CBQ_FOPT]) {
270 if (RTA_PAYLOAD(tb[TCA_CBQ_FOPT]) < sizeof(*fopt))
271 bb_error_msg(error, "fopt");
272 else
273 fopt = RTA_DATA(tb[TCA_CBQ_FOPT]);
274 }
275 if (tb[TCA_CBQ_OVL_STRATEGY]) {
276 if (RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]) < sizeof(*ovl))
277 bb_error_msg("CBQ: too short overlimit strategy %u/%u",
278 (unsigned) RTA_PAYLOAD(tb[TCA_CBQ_OVL_STRATEGY]),
279 (unsigned) sizeof(*ovl));
280 else
281 ovl = RTA_DATA(tb[TCA_CBQ_OVL_STRATEGY]);
282 }
283
284 if (r) {
285 print_rate(buf, sizeof(buf), r->rate);
286 printf("rate %s ", buf);
287 if (show_details) {
288 printf("cell %ub ", 1<<r->cell_log);
289 if (r->mpu)
290 printf("mpu %ub ", r->mpu);
291 if (r->overhead)
292 printf("overhead %ub ", r->overhead);
293 }
294 }
295 if (lss && lss->flags) {
296 bool comma = false;
297 bb_putchar('(');
298 if (lss->flags&TCF_CBQ_LSS_BOUNDED) {
299 printf("bounded");
300 comma = true;
301 }
302 if (lss->flags&TCF_CBQ_LSS_ISOLATED) {
303 if (comma)
304 bb_putchar(',');
305 printf("isolated");
306 }
307 printf(") ");
308 }
309 if (wrr) {
310 if (wrr->priority != TC_CBQ_MAXPRIO)
311 printf("prio %u", wrr->priority);
312 else
313 printf("prio no-transmit");
314 if (show_details) {
315 printf("/%u ", wrr->cpriority);
316 if (wrr->weight != 1) {
317 print_rate(buf, sizeof(buf), wrr->weight);
318 printf("weight %s ", buf);
319 }
320 if (wrr->allot)
321 printf("allot %ub ", wrr->allot);
322 }
323 }
324 done:
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000325 return 0;
326}
327
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100328static FAST_FUNC int print_qdisc(
329 const struct sockaddr_nl *who UNUSED_PARAM,
330 struct nlmsghdr *hdr,
331 void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000332{
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) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000339 /* bb_error_msg("not a qdisc"); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000340 return 0; /* ??? mimic upstream; should perhaps return -1 */
341 }
342 len -= NLMSG_LENGTH(sizeof(*msg));
343 if (len < 0) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000344 /* bb_error_msg("wrong len %d", len); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000345 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]);
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200377 } else if (qqq == 1) { /* class based queuing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000378 cbq_print_opt(tb[TCA_OPTIONS]);
379 } else
380 bb_error_msg("unknown %s", name);
381 }
382 bb_putchar('\n');
383 return 0;
384}
385
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100386static FAST_FUNC int print_class(
387 const struct sockaddr_nl *who UNUSED_PARAM,
388 struct nlmsghdr *hdr,
389 void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000390{
391 struct tcmsg *msg = NLMSG_DATA(hdr);
392 int len = hdr->nlmsg_len;
393 struct rtattr * tb[TCA_MAX+1];
394 char *name, *classid;
395
396 /*XXX Eventually factor out common code */
397
398 if (hdr->nlmsg_type != RTM_NEWTCLASS && hdr->nlmsg_type != RTM_DELTCLASS) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000399 /* bb_error_msg("not a class"); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000400 return 0; /* ??? mimic upstream; should perhaps return -1 */
401 }
402 len -= NLMSG_LENGTH(sizeof(*msg));
403 if (len < 0) {
Denis Vlasenko1fd3b382009-04-29 12:02:57 +0000404 /* bb_error_msg("wrong len %d", len); */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000405 return -1;
406 }
407 /* not the desired interface? */
408 if (filter_qdisc && TC_H_MAJ(msg->tcm_handle^filter_qdisc))
409 return 0;
410 memset (tb, 0, sizeof(tb));
411 parse_rtattr(tb, TCA_MAX, TCA_RTA(msg), len);
412 if (tb[TCA_KIND] == NULL) {
413 /* bb_error_msg("%s: NULL kind", "class"); */
414 return -1;
415 }
416 if (hdr->nlmsg_type == RTM_DELTCLASS)
417 printf("deleted ");
418
419 name = (char*)RTA_DATA(tb[TCA_KIND]);
420 classid = !msg->tcm_handle ? NULL : print_tc_classid(
421 filter_qdisc ? TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
422 printf ("class %s %s", name, classid);
423 if (ENABLE_FEATURE_CLEAN_UP)
424 free(classid);
425
426 if (filter_ifindex == 0)
427 printf("dev %s ", ll_index_to_name(msg->tcm_ifindex));
428 if (msg->tcm_parent == TC_H_ROOT)
429 printf("root ");
430 else if (msg->tcm_parent) {
431 classid = print_tc_classid(filter_qdisc ?
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100432 TC_H_MIN(msg->tcm_parent) : msg->tcm_parent);
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000433 printf("parent %s ", classid);
434 if (ENABLE_FEATURE_CLEAN_UP)
435 free(classid);
436 }
437 if (msg->tcm_info)
438 printf("leaf %x ", msg->tcm_info >> 16);
439 /* Do that get_qdisc_kind(RTA_DATA(tb[TCA_KIND])). */
440 if (tb[TCA_OPTIONS]) {
441 static const char _q_[] ALIGN1 = "pfifo_fast\0""cbq\0";
442 int qqq = index_in_strings(_q_, name);
443 if (qqq == 0) { /* pfifo_fast aka prio */
444 /* nothing. */ /*prio_print_opt(tb[TCA_OPTIONS]);*/
Denys Vlasenkobf2af9a2009-05-25 04:15:37 +0200445 } else if (qqq == 1) { /* class based queuing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000446 /* cbq_print_copt() is identical to cbq_print_opt(). */
447 cbq_print_opt(tb[TCA_OPTIONS]);
448 } else
449 bb_error_msg("unknown %s", name);
450 }
451 bb_putchar('\n');
452
453 return 0;
454}
455
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100456static FAST_FUNC int print_filter(
457 const struct sockaddr_nl *who UNUSED_PARAM,
458 struct nlmsghdr *hdr UNUSED_PARAM,
459 void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000460{
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000461 return 0;
462}
463
464int tc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
465int tc_main(int argc UNUSED_PARAM, char **argv)
466{
467 static const char objects[] ALIGN1 =
468 "qdisc\0""class\0""filter\0"
469 ;
470 enum { OBJ_qdisc = 0, OBJ_class, OBJ_filter };
471 static const char commands[] ALIGN1 =
472 "add\0""delete\0""change\0"
473 "link\0" /* only qdisc */
474 "replace\0"
475 "show\0""list\0"
476 ;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100477 enum {
478 CMD_add = 0, CMD_del, CMD_change,
479 CMD_link,
480 CMD_replace,
481 CMD_show
482 };
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000483 static const char args[] ALIGN1 =
484 "dev\0" /* qdisc, class, filter */
485 "root\0" /* class, filter */
486 "parent\0" /* class, filter */
487 "qdisc\0" /* class */
488 "handle\0" /* change: qdisc, class(classid) list: filter */
489 "classid\0" /* change: for class use "handle" */
490 "preference\0""priority\0""protocol\0" /* filter */
491 ;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100492 enum {
493 ARG_dev = 0,
494 ARG_root,
495 ARG_parent,
496 ARG_qdisc,
497 ARG_handle,
498 ARG_classid,
499 ARG_pref, ARG_prio, ARG_proto
500 };
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000501 struct rtnl_handle rth;
502 struct tcmsg msg;
503 int ret, obj, cmd, arg;
504 char *dev = NULL;
505
506 INIT_G();
507
508 if (!*++argv)
509 bb_show_usage();
510 xrtnl_open(&rth);
511 ret = EXIT_SUCCESS;
512
513 obj = index_in_substrings(objects, *argv++);
514
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200515 if (obj < 0)
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000516 bb_show_usage();
517 if (!*argv)
518 cmd = CMD_show; /* list is the default */
519 else {
520 cmd = index_in_substrings(commands, *argv);
521 if (cmd < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200522 invarg_1_to_2(*argv, argv[-1]);
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000523 argv++;
524 }
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100525
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000526 memset(&msg, 0, sizeof(msg));
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100527 if (AF_UNSPEC != 0)
528 msg.tcm_family = AF_UNSPEC;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000529 ll_init_map(&rth);
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100530
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000531 while (*argv) {
532 arg = index_in_substrings(args, *argv);
533 if (arg == ARG_dev) {
534 NEXT_ARG();
535 if (dev)
536 duparg2("dev", *argv);
537 dev = *argv++;
538 msg.tcm_ifindex = xll_name_to_index(dev);
539 if (cmd >= CMD_show)
540 filter_ifindex = msg.tcm_ifindex;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100541 } else
542 if ((arg == ARG_qdisc && obj == OBJ_class && cmd >= CMD_show)
543 || (arg == ARG_handle && obj == OBJ_qdisc && cmd == CMD_change)
544 ) {
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000545 NEXT_ARG();
546 /* We don't care about duparg2("qdisc handle",*argv) for now */
547 if (get_qdisc_handle(&filter_qdisc, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200548 invarg_1_to_2(*argv, "qdisc");
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100549 } else
550 if (obj != OBJ_qdisc
551 && (arg == ARG_root
552 || arg == ARG_parent
553 || (obj == OBJ_filter && arg >= ARG_pref)
554 )
555 ) {
556 /* nothing */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000557 } else {
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200558 invarg_1_to_2(*argv, "command");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000559 }
560 NEXT_ARG();
561 if (arg == ARG_root) {
562 if (msg.tcm_parent)
563 duparg("parent", *argv);
564 msg.tcm_parent = TC_H_ROOT;
565 if (obj == OBJ_filter)
566 filter_parent = TC_H_ROOT;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100567 } else
568 if (arg == ARG_parent) {
Daniel Fandrich6295d272011-06-09 15:44:44 -0700569 uint32_t handle;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000570 if (msg.tcm_parent)
571 duparg(*argv, "parent");
572 if (get_tc_classid(&handle, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200573 invarg_1_to_2(*argv, "parent");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000574 msg.tcm_parent = handle;
575 if (obj == OBJ_filter)
576 filter_parent = handle;
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100577 } else
578 if (arg == ARG_handle) { /* filter::list */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000579 if (msg.tcm_handle)
580 duparg(*argv, "handle");
581 /* reject LONG_MIN || LONG_MAX */
582 /* TODO: for fw
Denys Vlasenko60cb48c2013-01-14 15:57:44 +0100583 slash = strchr(handle, '/');
584 if (slash != NULL)
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100585 *slash = '\0';
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000586 */
Denis Vlasenko76140a72009-03-05 09:21:57 +0000587 msg.tcm_handle = get_u32(*argv, "handle");
Daniel Fandrich6295d272011-06-09 15:44:44 -0700588 /* if (slash) {if (get_u32(uint32_t &mask, slash+1, NULL)) inv mask; addattr32(n, MAX_MSG, TCA_FW_MASK, mask); */
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100589 } else
590 if (arg == ARG_classid
591 && obj == OBJ_class
592 && cmd == CMD_change
593 ) {
594 /* TODO */
595 } else
596 if (arg == ARG_pref || arg == ARG_prio) { /* filter::list */
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000597 if (filter_prio)
598 duparg(*argv, "priority");
Denis Vlasenko76140a72009-03-05 09:21:57 +0000599 filter_prio = get_u32(*argv, "priority");
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100600 } else
601 if (arg == ARG_proto) { /* filter::list */
Denys Vlasenko0568b6e2009-08-08 03:20:12 +0200602 uint16_t tmp;
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000603 if (filter_proto)
604 duparg(*argv, "protocol");
605 if (ll_proto_a2n(&tmp, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200606 invarg_1_to_2(*argv, "protocol");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000607 filter_proto = tmp;
608 }
609 }
Denys Vlasenkoa034cab2018-01-04 10:27:51 +0100610
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000611 if (cmd >= CMD_show) { /* show or list */
612 if (obj == OBJ_filter)
613 msg.tcm_info = TC_H_MAKE(filter_prio<<16, filter_proto);
614 if (rtnl_dump_request(&rth, obj == OBJ_qdisc ? RTM_GETQDISC :
615 obj == OBJ_class ? RTM_GETTCLASS : RTM_GETTFILTER,
616 &msg, sizeof(msg)) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100617 bb_simple_perror_msg_and_die("can't send dump request");
Bernhard Reutner-Fischer0901c512008-09-04 13:22:58 +0000618
619 xrtnl_dump_filter(&rth, obj == OBJ_qdisc ? print_qdisc :
620 obj == OBJ_class ? print_class : print_filter,
621 NULL);
622 }
623 if (ENABLE_FEATURE_CLEAN_UP) {
624 rtnl_close(&rth);
625 }
626 return ret;
627}