blob: aba06930cf8155a93c4684a6cbaf4f931f6d15a2 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
2 * tc_class.c "tc class".
3 *
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.
8 *
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22#include <math.h>
23
24#include "utils.h"
25#include "tc_util.h"
26#include "tc_common.h"
27
osdl.net!shemmingerb611d512005-03-14 19:02:41 +000028static void usage(void);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029
30static void usage(void)
31{
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");
osdl.net!shemmingerb611d512005-03-14 19:02:41 +000040 return;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000041}
42
43int tc_class_modify(int cmd, unsigned flags, int argc, char **argv)
44{
45 struct rtnl_handle rth;
46 struct {
47 struct nlmsghdr n;
48 struct tcmsg t;
49 char buf[4096];
50 } req;
51 struct qdisc_util *q = NULL;
52 struct tc_estimator est;
53 char d[16];
54 char k[16];
55
56 memset(&req, 0, sizeof(req));
57 memset(&est, 0, sizeof(est));
58 memset(d, 0, sizeof(d));
59 memset(k, 0, sizeof(k));
60
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;
65
66 while (argc > 0) {
67 if (strcmp(*argv, "dev") == 0) {
68 NEXT_ARG();
69 if (d[0])
70 duparg("dev", *argv);
71 strncpy(d, *argv, sizeof(d)-1);
72 } else if (strcmp(*argv, "classid") == 0) {
73 __u32 handle;
74 NEXT_ARG();
75 if (req.t.tcm_handle)
76 duparg("classid", *argv);
77 if (get_tc_classid(&handle, *argv))
78 invarg(*argv, "invalid class 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");
osdl.net!shemmingerb611d512005-03-14 19:02:41 +000083 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000084 }
85 req.t.tcm_parent = TC_H_ROOT;
86 } else if (strcmp(*argv, "parent") == 0) {
87 __u32 handle;
88 NEXT_ARG();
89 if (req.t.tcm_parent)
90 duparg("parent", *argv);
91 if (get_tc_classid(&handle, *argv))
92 invarg(*argv, "invalid parent ID");
93 req.t.tcm_parent = handle;
94 } else if (matches(*argv, "estimator") == 0) {
95 if (parse_estimator(&argc, &argv, &est))
96 return -1;
97 } else if (matches(*argv, "help") == 0) {
98 usage();
99 } else {
100 strncpy(k, *argv, sizeof(k)-1);
101
102 q = get_qdisc_kind(k);
103 argc--; argv++;
104 break;
105 }
106 argc--; argv++;
107 }
108
109 if (k[0])
110 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
111 if (est.ewma_log)
112 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
113
114 if (q) {
115 if (q->parse_copt == NULL) {
116 fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k);
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000117 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000118 }
119 if (q->parse_copt(q, argc, argv, &req.n))
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000120 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000121 } else {
122 if (argc) {
123 if (matches(*argv, "help") == 0)
124 usage();
125 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv);
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000126 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000127 }
128 }
129
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000130 if (!is_batch_mode)
131 if (rtnl_open(&rth, 0) < 0) {
132 fprintf(stderr, "Cannot open rtnetlink\n");
133 return 1;
134 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000135
136 if (d[0]) {
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000137 ll_init_map(&(is_batch_mode?g_rth:rth));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000138
139 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
140 fprintf(stderr, "Cannot find device \"%s\"\n", d);
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000141 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000142 }
143 }
144
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000145 if (rtnl_talk(&(is_batch_mode?g_rth:rth), &req.n, 0, 0, NULL, NULL, NULL) < 0)
146 return 2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000147
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000148 if (!is_batch_mode)
149 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000150 return 0;
151}
152
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000153int filter_ifindex;
154__u32 filter_qdisc;
155
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000156static int print_class(const struct sockaddr_nl *who,
osdl.net!shemminger50772dc2004-12-07 21:48:29 +0000157 struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000158{
159 FILE *fp = (FILE*)arg;
160 struct tcmsg *t = NLMSG_DATA(n);
161 int len = n->nlmsg_len;
162 struct rtattr * tb[TCA_MAX+1];
163 struct qdisc_util *q;
164 char abuf[256];
165
166 if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) {
167 fprintf(stderr, "Not a class\n");
168 return 0;
169 }
170 len -= NLMSG_LENGTH(sizeof(*t));
171 if (len < 0) {
172 fprintf(stderr, "Wrong len %d\n", len);
173 return -1;
174 }
175 if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc))
176 return 0;
177
178 memset(tb, 0, sizeof(tb));
179 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
180
181 if (tb[TCA_KIND] == NULL) {
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000182 fprintf(stderr, "print_class: NULL kind\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000183 return -1;
184 }
185
186 if (n->nlmsg_type == RTM_DELTCLASS)
187 fprintf(fp, "deleted ");
188
189 abuf[0] = 0;
190 if (t->tcm_handle) {
191 if (filter_qdisc)
192 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle));
193 else
194 print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
195 }
196 fprintf(fp, "class %s %s ", (char*)RTA_DATA(tb[TCA_KIND]), abuf);
197
198 if (filter_ifindex == 0)
199 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
200
201 if (t->tcm_parent == TC_H_ROOT)
202 fprintf(fp, "root ");
203 else {
204 if (filter_qdisc)
205 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
206 else
207 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
208 fprintf(fp, "parent %s ", abuf);
209 }
210 if (t->tcm_info)
211 fprintf(fp, "leaf %x: ", t->tcm_info>>16);
212 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
213 if (tb[TCA_OPTIONS]) {
214 if (q && q->print_copt)
215 q->print_copt(q, fp, tb[TCA_OPTIONS]);
216 else
217 fprintf(fp, "[cannot parse class parameters]");
218 }
219 fprintf(fp, "\n");
220 if (show_stats) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000221 struct rtattr *xstats = NULL;
222
223 if (tb[TCA_STATS] || tb[TCA_STATS2]) {
224 print_tcstats_attr(fp, tb, " ", &xstats);
osdl.net!shemminger2c5474a2004-08-31 17:54:25 +0000225 fprintf(fp, "\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000226 }
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000227 if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) {
228 q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000229 fprintf(fp, "\n");
230 }
231 }
232 fflush(fp);
233 return 0;
234}
235
236
237int tc_class_list(int argc, char **argv)
238{
239 struct tcmsg t;
240 struct rtnl_handle rth;
241 char d[16];
242
243 memset(&t, 0, sizeof(t));
244 t.tcm_family = AF_UNSPEC;
245 memset(d, 0, sizeof(d));
246
247 while (argc > 0) {
248 if (strcmp(*argv, "dev") == 0) {
249 NEXT_ARG();
250 if (d[0])
251 duparg("dev", *argv);
252 strncpy(d, *argv, sizeof(d)-1);
253 } else if (strcmp(*argv, "qdisc") == 0) {
254 NEXT_ARG();
255 if (filter_qdisc)
256 duparg("qdisc", *argv);
257 if (get_qdisc_handle(&filter_qdisc, *argv))
258 invarg(*argv, "invalid qdisc ID");
259 } else if (strcmp(*argv, "root") == 0) {
260 if (t.tcm_parent) {
261 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000262 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000263 }
264 t.tcm_parent = TC_H_ROOT;
265 } else if (strcmp(*argv, "parent") == 0) {
266 __u32 handle;
267 if (t.tcm_parent)
268 duparg("parent", *argv);
269 NEXT_ARG();
270 if (get_tc_classid(&handle, *argv))
271 invarg(*argv, "invalid parent ID");
272 t.tcm_parent = handle;
273 } else if (matches(*argv, "help") == 0) {
274 usage();
275 } else {
276 fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv);
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000277 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000278 }
279
280 argc--; argv++;
281 }
282
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000283 if (!is_batch_mode)
284 if (rtnl_open(&rth, 0) < 0) {
285 fprintf(stderr, "Cannot open rtnetlink\n");
286 return 1;
287 }
288
289 ll_init_map(&(is_batch_mode?g_rth:rth));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000290
291 if (d[0]) {
292 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
293 fprintf(stderr, "Cannot find device \"%s\"\n", d);
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000294 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000295 }
296 filter_ifindex = t.tcm_ifindex;
297 }
298
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000299 if (rtnl_dump_request(&(is_batch_mode?g_rth:rth), RTM_GETTCLASS, &t, sizeof(t)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000300 perror("Cannot send dump request");
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000301 rtnl_close(&rth);
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000302 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000303 }
304
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000305 if (rtnl_dump_filter(&(is_batch_mode?g_rth:rth), print_class, stdout, NULL, NULL) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000306 fprintf(stderr, "Dump terminated\n");
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000307 rtnl_close(&rth);
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000308 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000309 }
310
osdl.net!shemmingerb611d512005-03-14 19:02:41 +0000311 if (!is_batch_mode)
312 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000313 return 0;
314}
315
316int do_class(int argc, char **argv)
317{
318 if (argc < 1)
319 return tc_class_list(0, NULL);
320 if (matches(*argv, "add") == 0)
321 return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
322 if (matches(*argv, "change") == 0)
323 return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1);
324 if (matches(*argv, "replace") == 0)
325 return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1);
326 if (matches(*argv, "delete") == 0)
327 return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1);
328#if 0
329 if (matches(*argv, "get") == 0)
330 return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1);
331#endif
332 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
333 || matches(*argv, "lst") == 0)
334 return tc_class_list(argc-1, argv+1);
335 if (matches(*argv, "help") == 0)
336 usage();
337 fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv);
338 return -1;
339}