blob: 0352fd63db9328c494bc5805d0b1886108b57220 [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
28static void usage(void) __attribute__((noreturn));
29
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");
40 exit(-1);
41}
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");
83 exit(-1);
84 }
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);
117 exit(1);
118 }
119 if (q->parse_copt(q, argc, argv, &req.n))
120 exit(1);
121 } 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);
126 exit(-1);
127 }
128 }
129
130 if (rtnl_open(&rth, 0) < 0) {
131 fprintf(stderr, "Cannot open rtnetlink\n");
132 exit(1);
133 }
134
135 if (d[0]) {
136 ll_init_map(&rth);
137
138 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
139 fprintf(stderr, "Cannot find device \"%s\"\n", d);
140 exit(1);
141 }
142 }
143
144 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
145 exit(2);
146
147 rtnl_close(&rth);
148 return 0;
149}
150
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000151int filter_ifindex;
152__u32 filter_qdisc;
153
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000154static int print_class(const struct sockaddr_nl *who,
155 const struct nlmsghdr *n, void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000156{
157 FILE *fp = (FILE*)arg;
158 struct tcmsg *t = NLMSG_DATA(n);
159 int len = n->nlmsg_len;
160 struct rtattr * tb[TCA_MAX+1];
161 struct qdisc_util *q;
162 char abuf[256];
163
164 if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) {
165 fprintf(stderr, "Not a class\n");
166 return 0;
167 }
168 len -= NLMSG_LENGTH(sizeof(*t));
169 if (len < 0) {
170 fprintf(stderr, "Wrong len %d\n", len);
171 return -1;
172 }
173 if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc))
174 return 0;
175
176 memset(tb, 0, sizeof(tb));
177 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
178
179 if (tb[TCA_KIND] == NULL) {
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000180 fprintf(stderr, "print_class: NULL kind\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000181 return -1;
182 }
183
184 if (n->nlmsg_type == RTM_DELTCLASS)
185 fprintf(fp, "deleted ");
186
187 abuf[0] = 0;
188 if (t->tcm_handle) {
189 if (filter_qdisc)
190 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle));
191 else
192 print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
193 }
194 fprintf(fp, "class %s %s ", (char*)RTA_DATA(tb[TCA_KIND]), abuf);
195
196 if (filter_ifindex == 0)
197 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
198
199 if (t->tcm_parent == TC_H_ROOT)
200 fprintf(fp, "root ");
201 else {
202 if (filter_qdisc)
203 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
204 else
205 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
206 fprintf(fp, "parent %s ", abuf);
207 }
208 if (t->tcm_info)
209 fprintf(fp, "leaf %x: ", t->tcm_info>>16);
210 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
211 if (tb[TCA_OPTIONS]) {
212 if (q && q->print_copt)
213 q->print_copt(q, fp, tb[TCA_OPTIONS]);
214 else
215 fprintf(fp, "[cannot parse class parameters]");
216 }
217 fprintf(fp, "\n");
218 if (show_stats) {
219 if (tb[TCA_STATS]) {
osdl.net!shemminger2c5474a2004-08-31 17:54:25 +0000220 print_tcstats_attr(fp, tb[TCA_STATS]);
221 fprintf(fp, "\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000222 }
osdl.net!shemminger6dc9f012004-08-31 17:45:21 +0000223 if (q && tb[TCA_XSTATS] && q->print_xstats) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000224 q->print_xstats(q, fp, tb[TCA_XSTATS]);
225 fprintf(fp, "\n");
226 }
227 }
228 fflush(fp);
229 return 0;
230}
231
232
233int tc_class_list(int argc, char **argv)
234{
235 struct tcmsg t;
236 struct rtnl_handle rth;
237 char d[16];
238
239 memset(&t, 0, sizeof(t));
240 t.tcm_family = AF_UNSPEC;
241 memset(d, 0, sizeof(d));
242
243 while (argc > 0) {
244 if (strcmp(*argv, "dev") == 0) {
245 NEXT_ARG();
246 if (d[0])
247 duparg("dev", *argv);
248 strncpy(d, *argv, sizeof(d)-1);
249 } else if (strcmp(*argv, "qdisc") == 0) {
250 NEXT_ARG();
251 if (filter_qdisc)
252 duparg("qdisc", *argv);
253 if (get_qdisc_handle(&filter_qdisc, *argv))
254 invarg(*argv, "invalid qdisc ID");
255 } else if (strcmp(*argv, "root") == 0) {
256 if (t.tcm_parent) {
257 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
258 exit(-1);
259 }
260 t.tcm_parent = TC_H_ROOT;
261 } else if (strcmp(*argv, "parent") == 0) {
262 __u32 handle;
263 if (t.tcm_parent)
264 duparg("parent", *argv);
265 NEXT_ARG();
266 if (get_tc_classid(&handle, *argv))
267 invarg(*argv, "invalid parent ID");
268 t.tcm_parent = handle;
269 } else if (matches(*argv, "help") == 0) {
270 usage();
271 } else {
272 fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv);
273 exit(-1);
274 }
275
276 argc--; argv++;
277 }
278
279 if (rtnl_open(&rth, 0) < 0) {
280 fprintf(stderr, "Cannot open rtnetlink\n");
281 exit(1);
282 }
283
284 ll_init_map(&rth);
285
286 if (d[0]) {
287 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
288 fprintf(stderr, "Cannot find device \"%s\"\n", d);
289 exit(1);
290 }
291 filter_ifindex = t.tcm_ifindex;
292 }
293
294 if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
295 perror("Cannot send dump request");
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000296 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000297 exit(1);
298 }
299
300 if (rtnl_dump_filter(&rth, print_class, stdout, NULL, NULL) < 0) {
301 fprintf(stderr, "Dump terminated\n");
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000302 rtnl_close(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000303 exit(1);
304 }
305
306 rtnl_close(&rth);
307 return 0;
308}
309
310int do_class(int argc, char **argv)
311{
312 if (argc < 1)
313 return tc_class_list(0, NULL);
314 if (matches(*argv, "add") == 0)
315 return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
316 if (matches(*argv, "change") == 0)
317 return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1);
318 if (matches(*argv, "replace") == 0)
319 return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1);
320 if (matches(*argv, "delete") == 0)
321 return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1);
322#if 0
323 if (matches(*argv, "get") == 0)
324 return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1);
325#endif
326 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
327 || matches(*argv, "lst") == 0)
328 return tc_class_list(argc-1, argv+1);
329 if (matches(*argv, "help") == 0)
330 usage();
331 fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv);
332 return -1;
333}