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, "root") == 0) {
80 if (req.t.tcm_parent) {
81 fprintf(stderr, "Error: \"root\" is duplicate parent ID.\n");
84 req.t.tcm_parent = TC_H_ROOT;
85 } else if (strcmp(*argv, "parent") == 0) {
89 duparg("parent", *argv);
90 if (get_tc_classid(&handle, *argv))
91 invarg(*argv, "invalid parent ID");
92 req.t.tcm_parent = handle;
93 } else if (matches(*argv, "estimator") == 0) {
94 if (parse_estimator(&argc, &argv, &est))
96 } else if (matches(*argv, "help") == 0) {
99 strncpy(k, *argv, sizeof(k)-1);
101 q = get_qdisc_kind(k);
109 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
111 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
114 if (q->parse_copt == NULL) {
115 fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k);
118 if (q->parse_copt(q, argc, argv, &req.n))
122 if (matches(*argv, "help") == 0)
124 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv);
132 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
133 fprintf(stderr, "Cannot find device \"%s\"\n", d);
138 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
147 static int print_class(const struct sockaddr_nl *who,
148 struct nlmsghdr *n, void *arg)
150 FILE *fp = (FILE*)arg;
151 struct tcmsg *t = NLMSG_DATA(n);
152 int len = n->nlmsg_len;
153 struct rtattr * tb[TCA_MAX+1];
154 struct qdisc_util *q;
157 if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) {
158 fprintf(stderr, "Not a class\n");
161 len -= NLMSG_LENGTH(sizeof(*t));
163 fprintf(stderr, "Wrong len %d\n", len);
166 if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc))
169 memset(tb, 0, sizeof(tb));
170 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
172 if (tb[TCA_KIND] == NULL) {
173 fprintf(stderr, "print_class: NULL kind\n");
177 if (n->nlmsg_type == RTM_DELTCLASS)
178 fprintf(fp, "deleted ");
183 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle));
185 print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
187 fprintf(fp, "class %s %s ", (char*)RTA_DATA(tb[TCA_KIND]), abuf);
189 if (filter_ifindex == 0)
190 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
192 if (t->tcm_parent == TC_H_ROOT)
193 fprintf(fp, "root ");
196 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
198 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
199 fprintf(fp, "parent %s ", abuf);
202 fprintf(fp, "leaf %x: ", t->tcm_info>>16);
203 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
204 if (tb[TCA_OPTIONS]) {
205 if (q && q->print_copt)
206 q->print_copt(q, fp, tb[TCA_OPTIONS]);
208 fprintf(fp, "[cannot parse class parameters]");
212 struct rtattr *xstats = NULL;
214 if (tb[TCA_STATS] || tb[TCA_STATS2]) {
215 print_tcstats_attr(fp, tb, " ", &xstats);
218 if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) {
219 q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]);
228 int tc_class_list(int argc, char **argv)
233 memset(&t, 0, sizeof(t));
234 t.tcm_family = AF_UNSPEC;
235 memset(d, 0, sizeof(d));
238 if (strcmp(*argv, "dev") == 0) {
241 duparg("dev", *argv);
242 strncpy(d, *argv, sizeof(d)-1);
243 } else if (strcmp(*argv, "qdisc") == 0) {
246 duparg("qdisc", *argv);
247 if (get_qdisc_handle(&filter_qdisc, *argv))
248 invarg(*argv, "invalid qdisc ID");
249 } else if (strcmp(*argv, "root") == 0) {
251 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
254 t.tcm_parent = TC_H_ROOT;
255 } else if (strcmp(*argv, "parent") == 0) {
258 duparg("parent", *argv);
260 if (get_tc_classid(&handle, *argv))
261 invarg(*argv, "invalid parent ID");
262 t.tcm_parent = handle;
263 } else if (matches(*argv, "help") == 0) {
266 fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv);
276 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
277 fprintf(stderr, "Cannot find device \"%s\"\n", d);
280 filter_ifindex = t.tcm_ifindex;
283 if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
284 perror("Cannot send dump request");
288 if (rtnl_dump_filter(&rth, print_class, stdout, NULL, NULL) < 0) {
289 fprintf(stderr, "Dump terminated\n");
296 int do_class(int argc, char **argv)
299 return tc_class_list(0, NULL);
300 if (matches(*argv, "add") == 0)
301 return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
302 if (matches(*argv, "change") == 0)
303 return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1);
304 if (matches(*argv, "replace") == 0)
305 return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1);
306 if (matches(*argv, "delete") == 0)
307 return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1);
309 if (matches(*argv, "get") == 0)
310 return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1);
312 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
313 || matches(*argv, "lst") == 0)
314 return tc_class_list(argc-1, argv+1);
315 if (matches(*argv, "help") == 0)
317 fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv);