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 void usage(void) __attribute__((noreturn));
30 static void 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 rtnl_handle rth;
47 struct qdisc_util *q = NULL;
48 struct tc_estimator est;
54 char buf[TCA_BUF_MAX];
57 memset(&req, 0, sizeof(req));
58 memset(&est, 0, sizeof(est));
59 memset(&d, 0, sizeof(d));
60 memset(&k, 0, sizeof(k));
62 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
63 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
64 req.n.nlmsg_type = cmd;
65 req.t.tcm_family = AF_UNSPEC;
68 if (strcmp(*argv, "dev") == 0) {
72 strncpy(d, *argv, sizeof(d)-1);
73 } else if (strcmp(*argv, "handle") == 0) {
76 duparg("handle", *argv);
78 if (get_qdisc_handle(&handle, *argv))
79 invarg(*argv, "invalid qdisc ID");
80 req.t.tcm_handle = handle;
81 } else if (strcmp(*argv, "root") == 0) {
82 if (req.t.tcm_parent) {
83 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
86 req.t.tcm_parent = TC_H_ROOT;
88 } else if (strcmp(*argv, "ingress") == 0) {
89 if (req.t.tcm_parent) {
90 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
93 req.t.tcm_parent = TC_H_INGRESS;
94 strncpy(k, "ingress", sizeof(k)-1);
95 q = get_qdisc_kind(k);
96 req.t.tcm_handle = 0xffff0000;
101 } else if (strcmp(*argv, "parent") == 0) {
104 if (req.t.tcm_parent)
105 duparg("parent", *argv);
106 if (get_tc_classid(&handle, *argv))
107 invarg(*argv, "invalid parent ID");
108 req.t.tcm_parent = handle;
109 } else if (matches(*argv, "estimator") == 0) {
110 if (parse_estimator(&argc, &argv, &est))
112 } else if (matches(*argv, "help") == 0) {
115 strncpy(k, *argv, sizeof(k)-1);
117 q = get_qdisc_kind(k);
125 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
127 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
130 if (q->parse_qopt(q, argc, argv, &req.n))
134 if (matches(*argv, "help") == 0)
137 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
142 if (rtnl_open(&rth, 0) < 0) {
143 fprintf(stderr, "Cannot open rtnetlink\n");
153 if ((idx = ll_name_to_index(d)) == 0) {
154 fprintf(stderr, "Cannot find device \"%s\"\n", d);
158 req.t.tcm_ifindex = idx;
161 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
171 void print_tcstats_attr(FILE *fp, const struct rtattr *rta)
176 /* handle case where kernel returns more/less than we know about */
177 memset(&st, 0, sizeof(st));
178 memcpy(&st, RTA_DATA(rta), MIN(RTA_PAYLOAD(rta), sizeof(st)));
180 fprintf(fp, " Sent %llu bytes %u pkts (dropped %u, overlimits %u) ",
181 (unsigned long long)st.bytes, st.packets, st.drops,
184 if (st.bps || st.pps || st.qlen || st.backlog) {
186 if (st.bps || st.pps) {
187 fprintf(fp, "rate ");
189 fprintf(fp, "%s ", sprint_rate(st.bps, b1));
191 fprintf(fp, "%upps ", st.pps);
193 if (st.qlen || st.backlog) {
194 fprintf(fp, "backlog ");
196 fprintf(fp, "%s ", sprint_size(st.backlog, b1));
198 fprintf(fp, "%up ", st.qlen);
203 static int filter_ifindex;
205 static int print_qdisc(const struct sockaddr_nl *who,
209 FILE *fp = (FILE*)arg;
210 struct tcmsg *t = NLMSG_DATA(n);
211 int len = n->nlmsg_len;
212 struct rtattr * tb[TCA_MAX+1];
213 struct qdisc_util *q;
216 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) {
217 fprintf(stderr, "Not a qdisc\n");
220 len -= NLMSG_LENGTH(sizeof(*t));
222 fprintf(stderr, "Wrong len %d\n", len);
226 if (filter_ifindex && filter_ifindex != t->tcm_ifindex)
229 memset(tb, 0, sizeof(tb));
230 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
232 if (tb[TCA_KIND] == NULL) {
233 fprintf(stderr, "print_qdisc: NULL kind\n");
237 if (n->nlmsg_type == RTM_DELQDISC)
238 fprintf(fp, "deleted ");
240 fprintf(fp, "qdisc %s %x: ", (char*)RTA_DATA(tb[TCA_KIND]), t->tcm_handle>>16);
241 if (filter_ifindex == 0)
242 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
243 if (t->tcm_parent == TC_H_ROOT)
244 fprintf(fp, "root ");
245 else if (t->tcm_parent) {
246 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
247 fprintf(fp, "parent %s ", abuf);
249 if (t->tcm_info != 1) {
250 fprintf(fp, "refcnt %d ", t->tcm_info);
252 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
254 if (0 == strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])))
255 q = get_qdisc_kind("prio");
257 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
259 if (tb[TCA_OPTIONS]) {
261 q->print_qopt(q, fp, tb[TCA_OPTIONS]);
263 fprintf(fp, "[cannot parse qdisc parameters]");
268 print_tcstats_attr(fp, tb[TCA_STATS]);
272 if (q && tb[TCA_XSTATS] && q->print_xstats) {
273 q->print_xstats(q, fp, tb[TCA_XSTATS]);
282 int tc_qdisc_list(int argc, char **argv)
285 struct rtnl_handle rth;
288 memset(&t, 0, sizeof(t));
289 t.tcm_family = AF_UNSPEC;
290 memset(&d, 0, sizeof(d));
293 if (strcmp(*argv, "dev") == 0) {
295 strncpy(d, *argv, sizeof(d)-1);
297 } else if (strcmp(*argv, "ingress") == 0) {
299 fprintf(stderr, "Duplicate parent ID\n");
302 t.tcm_parent = TC_H_INGRESS;
304 } else if (matches(*argv, "help") == 0) {
307 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
314 if (rtnl_open(&rth, 0) < 0) {
315 fprintf(stderr, "Cannot open rtnetlink\n");
322 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
323 fprintf(stderr, "Cannot find device \"%s\"\n", d);
327 filter_ifindex = t.tcm_ifindex;
330 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
331 perror("Cannot send dump request");
336 if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) {
337 fprintf(stderr, "Dump terminated\n");
346 int do_qdisc(int argc, char **argv)
349 return tc_qdisc_list(0, NULL);
350 if (matches(*argv, "add") == 0)
351 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
352 if (matches(*argv, "change") == 0)
353 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1);
354 if (matches(*argv, "replace") == 0)
355 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
356 if (matches(*argv, "link") == 0)
357 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1);
358 if (matches(*argv, "delete") == 0)
359 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1);
361 if (matches(*argv, "get") == 0)
362 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1);
364 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
365 || matches(*argv, "lst") == 0)
366 return tc_qdisc_list(argc-1, argv+1);
367 if (matches(*argv, "help") == 0)
369 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);