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>
26 #include "tc_common.h"
28 static int usage(void);
30 static int usage(void)
32 fprintf(stderr, "Usage: tc qdisc [ add | del | replace | change | get ] dev STRING\n");
33 fprintf(stderr, " [ handle QHANDLE ] [ root | ingress | parent CLASSID ]\n");
34 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
35 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr, "\n");
37 fprintf(stderr, " tc qdisc show [ dev STRING ] [ingress]\n");
38 fprintf(stderr, "Where:\n");
39 fprintf(stderr, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
40 fprintf(stderr, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
44 int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv)
46 struct qdisc_util *q = NULL;
47 struct tc_estimator est;
53 char buf[TCA_BUF_MAX];
56 memset(&req, 0, sizeof(req));
57 memset(&est, 0, sizeof(est));
58 memset(&d, 0, sizeof(d));
59 memset(&k, 0, sizeof(k));
61 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
62 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
63 req.n.nlmsg_type = cmd;
64 req.t.tcm_family = AF_UNSPEC;
67 if (strcmp(*argv, "dev") == 0) {
71 strncpy(d, *argv, sizeof(d)-1);
72 } else if (strcmp(*argv, "handle") == 0) {
75 duparg("handle", *argv);
77 if (get_qdisc_handle(&handle, *argv))
78 invarg(*argv, "invalid qdisc ID");
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;
87 } else if (strcmp(*argv, "ingress") == 0) {
88 if (req.t.tcm_parent) {
89 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
92 req.t.tcm_parent = TC_H_INGRESS;
93 strncpy(k, "ingress", sizeof(k)-1);
94 q = get_qdisc_kind(k);
95 req.t.tcm_handle = 0xffff0000;
100 } else if (strcmp(*argv, "parent") == 0) {
103 if (req.t.tcm_parent)
104 duparg("parent", *argv);
105 if (get_tc_classid(&handle, *argv))
106 invarg(*argv, "invalid parent ID");
107 req.t.tcm_parent = handle;
108 } else if (matches(*argv, "estimator") == 0) {
109 if (parse_estimator(&argc, &argv, &est))
111 } else if (matches(*argv, "help") == 0) {
114 strncpy(k, *argv, sizeof(k)-1);
116 q = get_qdisc_kind(k);
124 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
126 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
129 if (q->parse_qopt(q, argc, argv, &req.n))
133 if (matches(*argv, "help") == 0)
136 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
146 if ((idx = ll_name_to_index(d)) == 0) {
147 fprintf(stderr, "Cannot find device \"%s\"\n", d);
150 req.t.tcm_ifindex = idx;
153 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
159 static int filter_ifindex;
161 static int print_qdisc(const struct sockaddr_nl *who,
165 FILE *fp = (FILE*)arg;
166 struct tcmsg *t = NLMSG_DATA(n);
167 int len = n->nlmsg_len;
168 struct rtattr * tb[TCA_MAX+1];
169 struct qdisc_util *q;
172 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) {
173 fprintf(stderr, "Not a qdisc\n");
176 len -= NLMSG_LENGTH(sizeof(*t));
178 fprintf(stderr, "Wrong len %d\n", len);
182 if (filter_ifindex && filter_ifindex != t->tcm_ifindex)
185 memset(tb, 0, sizeof(tb));
186 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
188 if (tb[TCA_KIND] == NULL) {
189 fprintf(stderr, "print_qdisc: NULL kind\n");
193 if (n->nlmsg_type == RTM_DELQDISC)
194 fprintf(fp, "deleted ");
196 fprintf(fp, "qdisc %s %x: ", (char*)RTA_DATA(tb[TCA_KIND]), t->tcm_handle>>16);
197 if (filter_ifindex == 0)
198 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
199 if (t->tcm_parent == TC_H_ROOT)
200 fprintf(fp, "root ");
201 else if (t->tcm_parent) {
202 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
203 fprintf(fp, "parent %s ", abuf);
205 if (t->tcm_info != 1) {
206 fprintf(fp, "refcnt %d ", t->tcm_info);
208 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
210 if (0 == strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])))
211 q = get_qdisc_kind("prio");
213 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
215 if (tb[TCA_OPTIONS]) {
217 q->print_qopt(q, fp, tb[TCA_OPTIONS]);
219 fprintf(fp, "[cannot parse qdisc parameters]");
223 struct rtattr *xstats = NULL;
225 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
226 print_tcstats_attr(fp, tb, " ", &xstats);
230 if (q && xstats && q->print_xstats) {
231 q->print_xstats(q, fp, xstats);
240 int tc_qdisc_list(int argc, char **argv)
245 memset(&t, 0, sizeof(t));
246 t.tcm_family = AF_UNSPEC;
247 memset(&d, 0, sizeof(d));
250 if (strcmp(*argv, "dev") == 0) {
252 strncpy(d, *argv, sizeof(d)-1);
254 } else if (strcmp(*argv, "ingress") == 0) {
256 fprintf(stderr, "Duplicate parent ID\n");
259 t.tcm_parent = TC_H_INGRESS;
261 } else if (matches(*argv, "help") == 0) {
264 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
274 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
275 fprintf(stderr, "Cannot find device \"%s\"\n", d);
278 filter_ifindex = t.tcm_ifindex;
281 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
282 perror("Cannot send dump request");
286 if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) {
287 fprintf(stderr, "Dump terminated\n");
294 int do_qdisc(int argc, char **argv)
297 return tc_qdisc_list(0, NULL);
298 if (matches(*argv, "add") == 0)
299 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
300 if (matches(*argv, "change") == 0)
301 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1);
302 if (matches(*argv, "replace") == 0)
303 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
304 if (matches(*argv, "link") == 0)
305 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1);
306 if (matches(*argv, "delete") == 0)
307 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1);
309 if (matches(*argv, "get") == 0)
310 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1);
312 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
313 || matches(*argv, "lst") == 0)
314 return tc_qdisc_list(argc-1, argv+1);
315 if (matches(*argv, "help") == 0)
317 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);