]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_class.c
make all filtering handles take const args.
[mirror_iproute2.git] / tc / tc_class.c
CommitLineData
aba5acdf
SH
1/*
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
151void print_class_tcstats(FILE *fp, struct tc_stats *st)
152{
153 SPRINT_BUF(b1);
154
155 fprintf(fp, " Sent %llu bytes %u pkts (dropped %u, overlimits %u) ",
156 (unsigned long long)st->bytes, st->packets, st->drops, st->overlimits);
157 if (st->bps || st->pps || st->qlen || st->backlog) {
158 fprintf(fp, "\n ");
159 if (st->bps || st->pps) {
160 fprintf(fp, "rate ");
161 if (st->bps)
162 fprintf(fp, "%s ", sprint_rate(st->bps, b1));
163 if (st->pps)
164 fprintf(fp, "%upps ", st->pps);
165 }
166 if (st->qlen || st->backlog) {
167 fprintf(fp, "backlog ");
168 if (st->backlog)
169 fprintf(fp, "%s ", sprint_size(st->backlog, b1));
170 if (st->qlen)
171 fprintf(fp, "%up ", st->qlen);
172 }
173 }
174}
175
176int filter_ifindex;
177__u32 filter_qdisc;
178
6dc9f016
SH
179static int print_class(const struct sockaddr_nl *who,
180 const struct nlmsghdr *n, void *arg)
aba5acdf
SH
181{
182 FILE *fp = (FILE*)arg;
183 struct tcmsg *t = NLMSG_DATA(n);
184 int len = n->nlmsg_len;
185 struct rtattr * tb[TCA_MAX+1];
186 struct qdisc_util *q;
187 char abuf[256];
188
189 if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) {
190 fprintf(stderr, "Not a class\n");
191 return 0;
192 }
193 len -= NLMSG_LENGTH(sizeof(*t));
194 if (len < 0) {
195 fprintf(stderr, "Wrong len %d\n", len);
196 return -1;
197 }
198 if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc))
199 return 0;
200
201 memset(tb, 0, sizeof(tb));
202 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
203
204 if (tb[TCA_KIND] == NULL) {
2373fde9 205 fprintf(stderr, "print_class: NULL kind\n");
aba5acdf
SH
206 return -1;
207 }
208
209 if (n->nlmsg_type == RTM_DELTCLASS)
210 fprintf(fp, "deleted ");
211
212 abuf[0] = 0;
213 if (t->tcm_handle) {
214 if (filter_qdisc)
215 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle));
216 else
217 print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
218 }
219 fprintf(fp, "class %s %s ", (char*)RTA_DATA(tb[TCA_KIND]), abuf);
220
221 if (filter_ifindex == 0)
222 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
223
224 if (t->tcm_parent == TC_H_ROOT)
225 fprintf(fp, "root ");
226 else {
227 if (filter_qdisc)
228 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
229 else
230 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
231 fprintf(fp, "parent %s ", abuf);
232 }
233 if (t->tcm_info)
234 fprintf(fp, "leaf %x: ", t->tcm_info>>16);
235 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
236 if (tb[TCA_OPTIONS]) {
237 if (q && q->print_copt)
238 q->print_copt(q, fp, tb[TCA_OPTIONS]);
239 else
240 fprintf(fp, "[cannot parse class parameters]");
241 }
242 fprintf(fp, "\n");
243 if (show_stats) {
244 if (tb[TCA_STATS]) {
245 if (RTA_PAYLOAD(tb[TCA_STATS]) < sizeof(struct tc_stats))
246 fprintf(fp, "statistics truncated");
247 else {
248 struct tc_stats st;
249 memcpy(&st, RTA_DATA(tb[TCA_STATS]), sizeof(st));
250 print_class_tcstats(fp, &st);
251 fprintf(fp, "\n");
252 }
253 }
6dc9f016 254 if (q && tb[TCA_XSTATS] && q->print_xstats) {
aba5acdf
SH
255 q->print_xstats(q, fp, tb[TCA_XSTATS]);
256 fprintf(fp, "\n");
257 }
258 }
259 fflush(fp);
260 return 0;
261}
262
263
264int tc_class_list(int argc, char **argv)
265{
266 struct tcmsg t;
267 struct rtnl_handle rth;
268 char d[16];
269
270 memset(&t, 0, sizeof(t));
271 t.tcm_family = AF_UNSPEC;
272 memset(d, 0, sizeof(d));
273
274 while (argc > 0) {
275 if (strcmp(*argv, "dev") == 0) {
276 NEXT_ARG();
277 if (d[0])
278 duparg("dev", *argv);
279 strncpy(d, *argv, sizeof(d)-1);
280 } else if (strcmp(*argv, "qdisc") == 0) {
281 NEXT_ARG();
282 if (filter_qdisc)
283 duparg("qdisc", *argv);
284 if (get_qdisc_handle(&filter_qdisc, *argv))
285 invarg(*argv, "invalid qdisc ID");
286 } else if (strcmp(*argv, "root") == 0) {
287 if (t.tcm_parent) {
288 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
289 exit(-1);
290 }
291 t.tcm_parent = TC_H_ROOT;
292 } else if (strcmp(*argv, "parent") == 0) {
293 __u32 handle;
294 if (t.tcm_parent)
295 duparg("parent", *argv);
296 NEXT_ARG();
297 if (get_tc_classid(&handle, *argv))
298 invarg(*argv, "invalid parent ID");
299 t.tcm_parent = handle;
300 } else if (matches(*argv, "help") == 0) {
301 usage();
302 } else {
303 fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv);
304 exit(-1);
305 }
306
307 argc--; argv++;
308 }
309
310 if (rtnl_open(&rth, 0) < 0) {
311 fprintf(stderr, "Cannot open rtnetlink\n");
312 exit(1);
313 }
314
315 ll_init_map(&rth);
316
317 if (d[0]) {
318 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
319 fprintf(stderr, "Cannot find device \"%s\"\n", d);
320 exit(1);
321 }
322 filter_ifindex = t.tcm_ifindex;
323 }
324
325 if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
326 perror("Cannot send dump request");
2373fde9 327 rtnl_close(&rth);
aba5acdf
SH
328 exit(1);
329 }
330
331 if (rtnl_dump_filter(&rth, print_class, stdout, NULL, NULL) < 0) {
332 fprintf(stderr, "Dump terminated\n");
2373fde9 333 rtnl_close(&rth);
aba5acdf
SH
334 exit(1);
335 }
336
337 rtnl_close(&rth);
338 return 0;
339}
340
341int do_class(int argc, char **argv)
342{
343 if (argc < 1)
344 return tc_class_list(0, NULL);
345 if (matches(*argv, "add") == 0)
346 return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
347 if (matches(*argv, "change") == 0)
348 return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1);
349 if (matches(*argv, "replace") == 0)
350 return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1);
351 if (matches(*argv, "delete") == 0)
352 return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1);
353#if 0
354 if (matches(*argv, "get") == 0)
355 return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1);
356#endif
357 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
358 || matches(*argv, "lst") == 0)
359 return tc_class_list(argc-1, argv+1);
360 if (matches(*argv, "help") == 0)
361 usage();
362 fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv);
363 return -1;
364}