]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_qdisc.c
2 * tc_qdisc.c "tc qdisc".
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>
10 * J Hadi Salim: Extension to ingress
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
26 #include "tc_common.h"
28 static int usage(void)
30 fprintf(stderr
, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
31 fprintf(stderr
, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
32 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
33 fprintf(stderr
, " [ stab [ help | STAB_OPTIONS] ]\n");
34 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
35 fprintf(stderr
, "\n");
36 fprintf(stderr
, " tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\n");
37 fprintf(stderr
, "Where:\n");
38 fprintf(stderr
, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
39 fprintf(stderr
, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
40 fprintf(stderr
, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
44 static int tc_qdisc_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
46 struct qdisc_util
*q
= NULL
;
47 struct tc_estimator est
= {};
49 struct tc_sizespec szopts
;
52 char d
[IFNAMSIZ
] = {};
53 char k
[FILTER_NAMESZ
] = {};
57 char buf
[TCA_BUF_MAX
];
59 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
60 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
62 .t
.tcm_family
= AF_UNSPEC
,
66 if (strcmp(*argv
, "dev") == 0) {
70 strncpy(d
, *argv
, sizeof(d
)-1);
71 } else if (strcmp(*argv
, "handle") == 0) {
75 duparg("handle", *argv
);
77 if (get_qdisc_handle(&handle
, *argv
))
78 invarg("invalid qdisc ID", *argv
);
79 req
.t
.tcm_handle
= handle
;
80 } else if (strcmp(*argv
, "root") == 0) {
81 if (req
.t
.tcm_parent
) {
82 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
85 req
.t
.tcm_parent
= TC_H_ROOT
;
86 } else if (strcmp(*argv
, "clsact") == 0) {
87 if (req
.t
.tcm_parent
) {
88 fprintf(stderr
, "Error: \"clsact\" is a duplicate parent ID\n");
91 req
.t
.tcm_parent
= TC_H_CLSACT
;
92 strncpy(k
, "clsact", sizeof(k
) - 1);
93 q
= get_qdisc_kind(k
);
94 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_CLSACT
, 0);
97 } else if (strcmp(*argv
, "ingress") == 0) {
98 if (req
.t
.tcm_parent
) {
99 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
102 req
.t
.tcm_parent
= TC_H_INGRESS
;
103 strncpy(k
, "ingress", sizeof(k
) - 1);
104 q
= get_qdisc_kind(k
);
105 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
108 } else if (strcmp(*argv
, "parent") == 0) {
112 if (req
.t
.tcm_parent
)
113 duparg("parent", *argv
);
114 if (get_tc_classid(&handle
, *argv
))
115 invarg("invalid parent ID", *argv
);
116 req
.t
.tcm_parent
= handle
;
117 } else if (matches(*argv
, "estimator") == 0) {
118 if (parse_estimator(&argc
, &argv
, &est
))
120 } else if (matches(*argv
, "stab") == 0) {
121 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
124 } else if (matches(*argv
, "help") == 0) {
127 strncpy(k
, *argv
, sizeof(k
)-1);
129 q
= get_qdisc_kind(k
);
137 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
139 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
143 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
, d
))
146 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
151 if (matches(*argv
, "help") == 0)
154 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
159 if (check_size_table_opts(&stab
.szopts
)) {
162 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
163 fprintf(stderr
, "failed to calculate size table.\n");
167 tail
= NLMSG_TAIL(&req
.n
);
168 addattr_l(&req
.n
, sizeof(req
), TCA_STAB
, NULL
, 0);
169 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
170 sizeof(stab
.szopts
));
172 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
173 stab
.szopts
.tsize
* sizeof(__u16
));
174 tail
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)tail
;
184 idx
= ll_name_to_index(d
);
186 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
189 req
.t
.tcm_ifindex
= idx
;
192 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
198 static int filter_ifindex
;
200 int print_qdisc(const struct sockaddr_nl
*who
,
201 struct nlmsghdr
*n
, void *arg
)
203 FILE *fp
= (FILE *)arg
;
204 struct tcmsg
*t
= NLMSG_DATA(n
);
205 int len
= n
->nlmsg_len
;
206 struct rtattr
*tb
[TCA_MAX
+1];
207 struct qdisc_util
*q
;
210 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
211 fprintf(stderr
, "Not a qdisc\n");
214 len
-= NLMSG_LENGTH(sizeof(*t
));
216 fprintf(stderr
, "Wrong len %d\n", len
);
220 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
223 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
225 if (tb
[TCA_KIND
] == NULL
) {
226 fprintf(stderr
, "print_qdisc: NULL kind\n");
230 open_json_object(NULL
);
232 if (n
->nlmsg_type
== RTM_DELQDISC
)
233 print_bool(PRINT_ANY
, "deleted", "deleted ", true);
235 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
236 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
237 (n
->nlmsg_flags
& NLM_F_REPLACE
))
238 print_bool(PRINT_ANY
, "replaced", "replaced ", true);
240 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
241 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
242 (n
->nlmsg_flags
& NLM_F_EXCL
))
243 print_bool(PRINT_ANY
, "added", "added ", true);
245 print_string(PRINT_ANY
, "kind", "qdisc %s",
246 rta_getattr_str(tb
[TCA_KIND
]));
247 sprintf(abuf
, "%x:", t
->tcm_handle
>> 16);
248 print_string(PRINT_ANY
, "handle", " %s", abuf
);
250 sprintf(abuf
, "[%08x]", t
->tcm_handle
);
251 print_string(PRINT_FP
, NULL
, "%s", abuf
);
253 print_string(PRINT_FP
, NULL
, " ", NULL
);
255 if (filter_ifindex
== 0)
256 print_string(PRINT_ANY
, "dev", "dev %s ",
257 ll_index_to_name(t
->tcm_ifindex
));
259 if (t
->tcm_parent
== TC_H_ROOT
)
260 print_bool(PRINT_ANY
, "root", "root ", true);
261 else if (t
->tcm_parent
) {
262 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
263 print_string(PRINT_ANY
, "parent", "parent %s ", abuf
);
266 if (t
->tcm_info
!= 1)
267 print_uint(PRINT_ANY
, "refcnt", "refcnt %u ", t
->tcm_info
);
269 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
270 if (strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])) == 0)
271 q
= get_qdisc_kind("prio");
273 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
275 open_json_object("options");
276 if (tb
[TCA_OPTIONS
]) {
278 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
280 print_string(PRINT_FP
, NULL
,
281 "[cannot parse qdisc parameters]", NULL
);
285 print_string(PRINT_FP
, NULL
, "\n", NULL
);
287 if (show_details
&& tb
[TCA_STAB
]) {
288 print_size_table(fp
, " ", tb
[TCA_STAB
]);
289 print_string(PRINT_FP
, NULL
, "\n", NULL
);
293 struct rtattr
*xstats
= NULL
;
295 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
296 print_tcstats_attr(fp
, tb
, " ", &xstats
);
297 print_string(PRINT_FP
, NULL
, "\n", NULL
);
300 if (q
&& xstats
&& q
->print_xstats
) {
301 q
->print_xstats(q
, fp
, xstats
);
302 print_string(PRINT_FP
, NULL
, "\n", NULL
);
310 static int tc_qdisc_list(int argc
, char **argv
)
312 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
313 char d
[IFNAMSIZ
] = {};
314 bool dump_invisible
= false;
317 if (strcmp(*argv
, "dev") == 0) {
319 strncpy(d
, *argv
, sizeof(d
)-1);
320 } else if (strcmp(*argv
, "ingress") == 0 ||
321 strcmp(*argv
, "clsact") == 0) {
323 fprintf(stderr
, "Duplicate parent ID\n");
326 t
.tcm_parent
= TC_H_INGRESS
;
327 } else if (matches(*argv
, "help") == 0) {
329 } else if (strcmp(*argv
, "invisible") == 0) {
330 dump_invisible
= true;
332 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
342 t
.tcm_ifindex
= ll_name_to_index(d
);
343 if (t
.tcm_ifindex
== 0) {
344 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
347 filter_ifindex
= t
.tcm_ifindex
;
350 if (dump_invisible
) {
356 .n
.nlmsg_type
= RTM_GETQDISC
,
357 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
360 req
.t
.tcm_family
= AF_UNSPEC
;
362 addattr(&req
.n
, 256, TCA_DUMP_INVISIBLE
);
363 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
364 perror("Cannot send dump request");
368 } else if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
369 perror("Cannot send dump request");
374 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
375 fprintf(stderr
, "Dump terminated\n");
383 int do_qdisc(int argc
, char **argv
)
386 return tc_qdisc_list(0, NULL
);
387 if (matches(*argv
, "add") == 0)
388 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
389 if (matches(*argv
, "change") == 0)
390 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
391 if (matches(*argv
, "replace") == 0)
392 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
393 if (matches(*argv
, "link") == 0)
394 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
395 if (matches(*argv
, "delete") == 0)
396 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
398 if (matches(*argv
, "get") == 0)
399 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
401 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
402 || matches(*argv
, "lst") == 0)
403 return tc_qdisc_list(argc
-1, argv
+1);
404 if (matches(*argv
, "help") == 0) {
408 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);