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
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
27 #include "tc_common.h"
29 static int usage(void);
31 static int usage(void)
33 fprintf(stderr, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
34 fprintf(stderr, " [ handle QHANDLE ] [ root | ingress | parent CLASSID ]\n");
35 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
36 fprintf(stderr, " [ stab [ help | STAB_OPTIONS] ]\n");
37 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
38 fprintf(stderr, "\n");
39 fprintf(stderr, " tc qdisc show [ dev STRING ] [ingress]\n");
40 fprintf(stderr, "Where:\n");
41 fprintf(stderr, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
42 fprintf(stderr, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
43 fprintf(stderr, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
47 int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv)
49 struct qdisc_util *q = NULL;
50 struct tc_estimator est;
52 struct tc_sizespec szopts;
60 char buf[TCA_BUF_MAX];
63 memset(&req, 0, sizeof(req));
64 memset(&stab, 0, sizeof(stab));
65 memset(&est, 0, sizeof(est));
66 memset(&d, 0, sizeof(d));
67 memset(&k, 0, sizeof(k));
69 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
70 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
71 req.n.nlmsg_type = cmd;
72 req.t.tcm_family = AF_UNSPEC;
75 if (strcmp(*argv, "dev") == 0) {
79 strncpy(d, *argv, sizeof(d)-1);
80 } else if (strcmp(*argv, "handle") == 0) {
83 duparg("handle", *argv);
85 if (get_qdisc_handle(&handle, *argv))
86 invarg(*argv, "invalid qdisc ID");
87 req.t.tcm_handle = handle;
88 } else if (strcmp(*argv, "root") == 0) {
89 if (req.t.tcm_parent) {
90 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
93 req.t.tcm_parent = TC_H_ROOT;
95 } else if (strcmp(*argv, "ingress") == 0) {
96 if (req.t.tcm_parent) {
97 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
100 req.t.tcm_parent = TC_H_INGRESS;
101 strncpy(k, "ingress", sizeof(k)-1);
102 q = get_qdisc_kind(k);
103 req.t.tcm_handle = 0xffff0000;
108 } else if (strcmp(*argv, "parent") == 0) {
111 if (req.t.tcm_parent)
112 duparg("parent", *argv);
113 if (get_tc_classid(&handle, *argv))
114 invarg(*argv, "invalid parent ID");
115 req.t.tcm_parent = handle;
116 } else if (matches(*argv, "estimator") == 0) {
117 if (parse_estimator(&argc, &argv, &est))
119 } else if (matches(*argv, "stab") == 0) {
120 if (parse_size_table(&argc, &argv, &stab.szopts) < 0)
123 } else if (matches(*argv, "help") == 0) {
126 strncpy(k, *argv, sizeof(k)-1);
128 q = get_qdisc_kind(k);
136 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
138 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
141 if (!q->parse_qopt) {
142 fprintf(stderr, "qdisc '%s' does not support option parsing\n", k);
145 if (q->parse_qopt(q, argc, argv, &req.n))
149 if (matches(*argv, "help") == 0)
152 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
157 if (check_size_table_opts(&stab.szopts)) {
160 if (tc_calc_size_table(&stab.szopts, &stab.data) < 0) {
161 fprintf(stderr, "failed to calculate size table.\n");
165 tail = NLMSG_TAIL(&req.n);
166 addattr_l(&req.n, sizeof(req), TCA_STAB, NULL, 0);
167 addattr_l(&req.n, sizeof(req), TCA_STAB_BASE, &stab.szopts,
168 sizeof(stab.szopts));
170 addattr_l(&req.n, sizeof(req), TCA_STAB_DATA, stab.data,
171 stab.szopts.tsize * sizeof(__u16));
172 tail->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)tail;
182 if ((idx = ll_name_to_index(d)) == 0) {
183 fprintf(stderr, "Cannot find device \"%s\"\n", d);
186 req.t.tcm_ifindex = idx;
189 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
195 static int filter_ifindex;
197 int print_qdisc(const struct sockaddr_nl *who,
201 FILE *fp = (FILE*)arg;
202 struct tcmsg *t = NLMSG_DATA(n);
203 int len = n->nlmsg_len;
204 struct rtattr * tb[TCA_MAX+1];
205 struct qdisc_util *q;
208 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) {
209 fprintf(stderr, "Not a qdisc\n");
212 len -= NLMSG_LENGTH(sizeof(*t));
214 fprintf(stderr, "Wrong len %d\n", len);
218 if (filter_ifindex && filter_ifindex != t->tcm_ifindex)
221 memset(tb, 0, sizeof(tb));
222 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
224 if (tb[TCA_KIND] == NULL) {
225 fprintf(stderr, "print_qdisc: NULL kind\n");
229 if (n->nlmsg_type == RTM_DELQDISC)
230 fprintf(fp, "deleted ");
232 fprintf(fp, "qdisc %s %x: ", (char*)RTA_DATA(tb[TCA_KIND]), t->tcm_handle>>16);
233 if (filter_ifindex == 0)
234 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
235 if (t->tcm_parent == TC_H_ROOT)
236 fprintf(fp, "root ");
237 else if (t->tcm_parent) {
238 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
239 fprintf(fp, "parent %s ", abuf);
241 if (t->tcm_info != 1) {
242 fprintf(fp, "refcnt %d ", t->tcm_info);
244 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
246 if (0 == strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])))
247 q = get_qdisc_kind("prio");
249 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
251 if (tb[TCA_OPTIONS]) {
253 q->print_qopt(q, fp, tb[TCA_OPTIONS]);
255 fprintf(fp, "[cannot parse qdisc parameters]");
258 if (show_details && tb[TCA_STAB]) {
259 print_size_table(fp, " ", tb[TCA_STAB]);
263 struct rtattr *xstats = NULL;
265 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
266 print_tcstats_attr(fp, tb, " ", &xstats);
270 if (q && xstats && q->print_xstats) {
271 q->print_xstats(q, fp, xstats);
280 int tc_qdisc_list(int argc, char **argv)
285 memset(&t, 0, sizeof(t));
286 t.tcm_family = AF_UNSPEC;
287 memset(&d, 0, sizeof(d));
290 if (strcmp(*argv, "dev") == 0) {
292 strncpy(d, *argv, sizeof(d)-1);
294 } else if (strcmp(*argv, "ingress") == 0) {
296 fprintf(stderr, "Duplicate parent ID\n");
299 t.tcm_parent = TC_H_INGRESS;
301 } else if (matches(*argv, "help") == 0) {
304 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
314 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
315 fprintf(stderr, "Cannot find device \"%s\"\n", d);
318 filter_ifindex = t.tcm_ifindex;
321 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
322 perror("Cannot send dump request");
326 if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) {
327 fprintf(stderr, "Dump terminated\n");
334 int do_qdisc(int argc, char **argv)
337 return tc_qdisc_list(0, NULL);
338 if (matches(*argv, "add") == 0)
339 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
340 if (matches(*argv, "change") == 0)
341 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1);
342 if (matches(*argv, "replace") == 0)
343 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
344 if (matches(*argv, "link") == 0)
345 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1);
346 if (matches(*argv, "delete") == 0)
347 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1);
349 if (matches(*argv, "get") == 0)
350 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1);
352 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
353 || matches(*argv, "lst") == 0)
354 return tc_qdisc_list(argc-1, argv+1);
355 if (matches(*argv, "help") == 0) {
359 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);