2 * tc_class.c "tc class".
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>
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);
30 static void usage(void)
32 fprintf(stderr, "Usage: tc class [ add | del | change | get ] dev STRING\n");
33 fprintf(stderr, " [ classid CLASSID ] [ root | parent CLASSID ]\n");
34 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
35 fprintf(stderr, "\n");
36 fprintf(stderr, " tc class show [ dev STRING ] [ root | parent CLASSID ]\n");
37 fprintf(stderr, "Where:\n");
38 fprintf(stderr, "QDISC_KIND := { prio | cbq | etc. }\n");
39 fprintf(stderr, "OPTIONS := ... try tc class add <desired QDISC_KIND> help\n");
43 int tc_class_modify(int cmd, unsigned flags, int argc, char **argv)
50 struct qdisc_util *q = NULL;
51 struct tc_estimator est;
55 memset(&req, 0, sizeof(req));
56 memset(&est, 0, sizeof(est));
57 memset(d, 0, sizeof(d));
58 memset(k, 0, sizeof(k));
60 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
61 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
62 req.n.nlmsg_type = cmd;
63 req.t.tcm_family = AF_UNSPEC;
66 if (strcmp(*argv, "dev") == 0) {
70 strncpy(d, *argv, sizeof(d)-1);
71 } else if (strcmp(*argv, "classid") == 0) {
75 duparg("classid", *argv);
76 if (get_tc_classid(&handle, *argv))
77 invarg(*argv, "invalid class ID");
78 req.t.tcm_handle = handle;
79 } else if (strcmp(*argv, "handle") == 0) {
80 fprintf(stderr, "Error: try \"classid\" instead of \"handle\"\n");
82 } else if (strcmp(*argv, "root") == 0) {
83 if (req.t.tcm_parent) {
84 fprintf(stderr, "Error: \"root\" is duplicate parent ID.\n");
87 req.t.tcm_parent = TC_H_ROOT;
88 } else if (strcmp(*argv, "parent") == 0) {
92 duparg("parent", *argv);
93 if (get_tc_classid(&handle, *argv))
94 invarg(*argv, "invalid parent ID");
95 req.t.tcm_parent = handle;
96 } else if (matches(*argv, "estimator") == 0) {
97 if (parse_estimator(&argc, &argv, &est))
99 } else if (matches(*argv, "help") == 0) {
102 strncpy(k, *argv, sizeof(k)-1);
104 q = get_qdisc_kind(k);
112 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
114 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
117 if (q->parse_copt == NULL) {
118 fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k);
121 if (q->parse_copt(q, argc, argv, &req.n))
125 if (matches(*argv, "help") == 0)
127 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv);
135 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
136 fprintf(stderr, "Cannot find device \"%s\"\n", d);
141 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
150 static int print_class(const struct sockaddr_nl *who,
151 struct nlmsghdr *n, void *arg)
153 FILE *fp = (FILE*)arg;
154 struct tcmsg *t = NLMSG_DATA(n);
155 int len = n->nlmsg_len;
156 struct rtattr * tb[TCA_MAX+1];
157 struct qdisc_util *q;
160 if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) {
161 fprintf(stderr, "Not a class\n");
164 len -= NLMSG_LENGTH(sizeof(*t));
166 fprintf(stderr, "Wrong len %d\n", len);
169 if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc))
172 memset(tb, 0, sizeof(tb));
173 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
175 if (tb[TCA_KIND] == NULL) {
176 fprintf(stderr, "print_class: NULL kind\n");
180 if (n->nlmsg_type == RTM_DELTCLASS)
181 fprintf(fp, "deleted ");
186 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle));
188 print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
190 fprintf(fp, "class %s %s ", (char*)RTA_DATA(tb[TCA_KIND]), abuf);
192 if (filter_ifindex == 0)
193 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
195 if (t->tcm_parent == TC_H_ROOT)
196 fprintf(fp, "root ");
199 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
201 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
202 fprintf(fp, "parent %s ", abuf);
205 fprintf(fp, "leaf %x: ", t->tcm_info>>16);
206 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
207 if (tb[TCA_OPTIONS]) {
208 if (q && q->print_copt)
209 q->print_copt(q, fp, tb[TCA_OPTIONS]);
211 fprintf(fp, "[cannot parse class parameters]");
215 struct rtattr *xstats = NULL;
217 if (tb[TCA_STATS] || tb[TCA_STATS2]) {
218 print_tcstats_attr(fp, tb, " ", &xstats);
221 if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) {
222 q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]);
231 int tc_class_list(int argc, char **argv)
236 memset(&t, 0, sizeof(t));
237 t.tcm_family = AF_UNSPEC;
238 memset(d, 0, sizeof(d));
241 if (strcmp(*argv, "dev") == 0) {
244 duparg("dev", *argv);
245 strncpy(d, *argv, sizeof(d)-1);
246 } else if (strcmp(*argv, "qdisc") == 0) {
249 duparg("qdisc", *argv);
250 if (get_qdisc_handle(&filter_qdisc, *argv))
251 invarg(*argv, "invalid qdisc ID");
252 } else if (strcmp(*argv, "root") == 0) {
254 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
257 t.tcm_parent = TC_H_ROOT;
258 } else if (strcmp(*argv, "parent") == 0) {
261 duparg("parent", *argv);
263 if (get_tc_classid(&handle, *argv))
264 invarg(*argv, "invalid parent ID");
265 t.tcm_parent = handle;
266 } else if (matches(*argv, "help") == 0) {
269 fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv);
279 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
280 fprintf(stderr, "Cannot find device \"%s\"\n", d);
283 filter_ifindex = t.tcm_ifindex;
286 if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
287 perror("Cannot send dump request");
291 if (rtnl_dump_filter(&rth, print_class, stdout, NULL, NULL) < 0) {
292 fprintf(stderr, "Dump terminated\n");
299 int do_class(int argc, char **argv)
302 return tc_class_list(0, NULL);
303 if (matches(*argv, "add") == 0)
304 return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
305 if (matches(*argv, "change") == 0)
306 return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1);
307 if (matches(*argv, "replace") == 0)
308 return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1);
309 if (matches(*argv, "delete") == 0)
310 return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1);
312 if (matches(*argv, "get") == 0)
313 return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1);
315 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
316 || matches(*argv, "lst") == 0)
317 return tc_class_list(argc-1, argv+1);
318 if (matches(*argv, "help") == 0)
320 fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv);