]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_qdisc.c
tc: code cleanup
[mirror_iproute2.git] / tc / tc_qdisc.c
1 /*
2 * tc_qdisc.c "tc qdisc".
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 * J Hadi Salim: Extension to ingress
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 #include <malloc.h>
24
25 #include "utils.h"
26 #include "tc_util.h"
27 #include "tc_common.h"
28
29 static int usage(void)
30 {
31 fprintf(stderr, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
32 fprintf(stderr, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
33 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
34 fprintf(stderr, " [ stab [ help | STAB_OPTIONS] ]\n");
35 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr, "\n");
37 fprintf(stderr, " tc qdisc show [ dev STRING ] [ ingress | clsact ]\n");
38 fprintf(stderr, "Where:\n");
39 fprintf(stderr, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
40 fprintf(stderr, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
41 fprintf(stderr, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
42 return -1;
43 }
44
45 static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
46 {
47 struct qdisc_util *q = NULL;
48 struct tc_estimator est;
49 struct {
50 struct tc_sizespec szopts;
51 __u16 *data;
52 } stab;
53 char d[16];
54 char k[16];
55 struct {
56 struct nlmsghdr n;
57 struct tcmsg t;
58 char buf[TCA_BUF_MAX];
59 } req;
60
61 memset(&req, 0, sizeof(req));
62 memset(&stab, 0, sizeof(stab));
63 memset(&est, 0, sizeof(est));
64 memset(&d, 0, sizeof(d));
65 memset(&k, 0, sizeof(k));
66
67 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
68 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
69 req.n.nlmsg_type = cmd;
70 req.t.tcm_family = AF_UNSPEC;
71
72 while (argc > 0) {
73 if (strcmp(*argv, "dev") == 0) {
74 NEXT_ARG();
75 if (d[0])
76 duparg("dev", *argv);
77 strncpy(d, *argv, sizeof(d)-1);
78 } else if (strcmp(*argv, "handle") == 0) {
79 __u32 handle;
80
81 if (req.t.tcm_handle)
82 duparg("handle", *argv);
83 NEXT_ARG();
84 if (get_qdisc_handle(&handle, *argv))
85 invarg("invalid qdisc ID", *argv);
86 req.t.tcm_handle = handle;
87 } else if (strcmp(*argv, "root") == 0) {
88 if (req.t.tcm_parent) {
89 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
90 return -1;
91 }
92 req.t.tcm_parent = TC_H_ROOT;
93 } else if (strcmp(*argv, "clsact") == 0) {
94 if (req.t.tcm_parent) {
95 fprintf(stderr, "Error: \"clsact\" is a duplicate parent ID\n");
96 return -1;
97 }
98 req.t.tcm_parent = TC_H_CLSACT;
99 strncpy(k, "clsact", sizeof(k) - 1);
100 q = get_qdisc_kind(k);
101 req.t.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0);
102 NEXT_ARG_FWD();
103 break;
104 } else if (strcmp(*argv, "ingress") == 0) {
105 if (req.t.tcm_parent) {
106 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
107 return -1;
108 }
109 req.t.tcm_parent = TC_H_INGRESS;
110 strncpy(k, "ingress", sizeof(k) - 1);
111 q = get_qdisc_kind(k);
112 req.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
113 NEXT_ARG_FWD();
114 break;
115 } else if (strcmp(*argv, "parent") == 0) {
116 __u32 handle;
117
118 NEXT_ARG();
119 if (req.t.tcm_parent)
120 duparg("parent", *argv);
121 if (get_tc_classid(&handle, *argv))
122 invarg("invalid parent ID", *argv);
123 req.t.tcm_parent = handle;
124 } else if (matches(*argv, "estimator") == 0) {
125 if (parse_estimator(&argc, &argv, &est))
126 return -1;
127 } else if (matches(*argv, "stab") == 0) {
128 if (parse_size_table(&argc, &argv, &stab.szopts) < 0)
129 return -1;
130 continue;
131 } else if (matches(*argv, "help") == 0) {
132 usage();
133 } else {
134 strncpy(k, *argv, sizeof(k)-1);
135
136 q = get_qdisc_kind(k);
137 argc--; argv++;
138 break;
139 }
140 argc--; argv++;
141 }
142
143 if (k[0])
144 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
145 if (est.ewma_log)
146 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
147
148 if (q) {
149 if (q->parse_qopt) {
150 if (q->parse_qopt(q, argc, argv, &req.n))
151 return 1;
152 } else if (argc) {
153 fprintf(stderr, "qdisc '%s' does not support option parsing\n", k);
154 return -1;
155 }
156 } else {
157 if (argc) {
158 if (matches(*argv, "help") == 0)
159 usage();
160
161 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
162 return -1;
163 }
164 }
165
166 if (check_size_table_opts(&stab.szopts)) {
167 struct rtattr *tail;
168
169 if (tc_calc_size_table(&stab.szopts, &stab.data) < 0) {
170 fprintf(stderr, "failed to calculate size table.\n");
171 return -1;
172 }
173
174 tail = NLMSG_TAIL(&req.n);
175 addattr_l(&req.n, sizeof(req), TCA_STAB, NULL, 0);
176 addattr_l(&req.n, sizeof(req), TCA_STAB_BASE, &stab.szopts,
177 sizeof(stab.szopts));
178 if (stab.data)
179 addattr_l(&req.n, sizeof(req), TCA_STAB_DATA, stab.data,
180 stab.szopts.tsize * sizeof(__u16));
181 tail->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)tail;
182 if (stab.data)
183 free(stab.data);
184 }
185
186 if (d[0]) {
187 int idx;
188
189 ll_init_map(&rth);
190
191 if ((idx = ll_name_to_index(d)) == 0) {
192 fprintf(stderr, "Cannot find device \"%s\"\n", d);
193 return 1;
194 }
195 req.t.tcm_ifindex = idx;
196 }
197
198 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
199 return 2;
200
201 return 0;
202 }
203
204 static int filter_ifindex;
205
206 int print_qdisc(const struct sockaddr_nl *who,
207 struct nlmsghdr *n,
208 void *arg)
209 {
210 FILE *fp = (FILE *)arg;
211 struct tcmsg *t = NLMSG_DATA(n);
212 int len = n->nlmsg_len;
213 struct rtattr *tb[TCA_MAX+1];
214 struct qdisc_util *q;
215 char abuf[256];
216
217 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) {
218 fprintf(stderr, "Not a qdisc\n");
219 return 0;
220 }
221 len -= NLMSG_LENGTH(sizeof(*t));
222 if (len < 0) {
223 fprintf(stderr, "Wrong len %d\n", len);
224 return -1;
225 }
226
227 if (filter_ifindex && filter_ifindex != t->tcm_ifindex)
228 return 0;
229
230 memset(tb, 0, sizeof(tb));
231 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
232
233 if (tb[TCA_KIND] == NULL) {
234 fprintf(stderr, "print_qdisc: NULL kind\n");
235 return -1;
236 }
237
238 if (n->nlmsg_type == RTM_DELQDISC)
239 fprintf(fp, "deleted ");
240
241 fprintf(fp, "qdisc %s %x: ", rta_getattr_str(tb[TCA_KIND]), t->tcm_handle>>16);
242 if (filter_ifindex == 0)
243 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
244 if (t->tcm_parent == TC_H_ROOT)
245 fprintf(fp, "root ");
246 else if (t->tcm_parent) {
247 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
248 fprintf(fp, "parent %s ", abuf);
249 }
250 if (t->tcm_info != 1) {
251 fprintf(fp, "refcnt %d ", t->tcm_info);
252 }
253 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
254
255 if (0 == strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])))
256 q = get_qdisc_kind("prio");
257 else
258 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
259
260 if (tb[TCA_OPTIONS]) {
261 if (q)
262 q->print_qopt(q, fp, tb[TCA_OPTIONS]);
263 else
264 fprintf(fp, "[cannot parse qdisc parameters]");
265 }
266 fprintf(fp, "\n");
267 if (show_details && tb[TCA_STAB]) {
268 print_size_table(fp, " ", tb[TCA_STAB]);
269 fprintf(fp, "\n");
270 }
271 if (show_stats) {
272 struct rtattr *xstats = NULL;
273
274 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
275 print_tcstats_attr(fp, tb, " ", &xstats);
276 fprintf(fp, "\n");
277 }
278
279 if (q && xstats && q->print_xstats) {
280 q->print_xstats(q, fp, xstats);
281 fprintf(fp, "\n");
282 }
283 }
284 fflush(fp);
285 return 0;
286 }
287
288 static int tc_qdisc_list(int argc, char **argv)
289 {
290 struct tcmsg t;
291 char d[16];
292
293 memset(&t, 0, sizeof(t));
294 t.tcm_family = AF_UNSPEC;
295 memset(&d, 0, sizeof(d));
296
297 while (argc > 0) {
298 if (strcmp(*argv, "dev") == 0) {
299 NEXT_ARG();
300 strncpy(d, *argv, sizeof(d)-1);
301 } else if (strcmp(*argv, "ingress") == 0 ||
302 strcmp(*argv, "clsact") == 0) {
303 if (t.tcm_parent) {
304 fprintf(stderr, "Duplicate parent ID\n");
305 usage();
306 }
307 t.tcm_parent = TC_H_INGRESS;
308 } else if (matches(*argv, "help") == 0) {
309 usage();
310 } else {
311 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
312 return -1;
313 }
314
315 argc--; argv++;
316 }
317
318 ll_init_map(&rth);
319
320 if (d[0]) {
321 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
322 fprintf(stderr, "Cannot find device \"%s\"\n", d);
323 return 1;
324 }
325 filter_ifindex = t.tcm_ifindex;
326 }
327
328 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
329 perror("Cannot send dump request");
330 return 1;
331 }
332
333 if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
334 fprintf(stderr, "Dump terminated\n");
335 return 1;
336 }
337
338 return 0;
339 }
340
341 int do_qdisc(int argc, char **argv)
342 {
343 if (argc < 1)
344 return tc_qdisc_list(0, NULL);
345 if (matches(*argv, "add") == 0)
346 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
347 if (matches(*argv, "change") == 0)
348 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1);
349 if (matches(*argv, "replace") == 0)
350 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1);
351 if (matches(*argv, "link") == 0)
352 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1);
353 if (matches(*argv, "delete") == 0)
354 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1);
355 #if 0
356 if (matches(*argv, "get") == 0)
357 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1);
358 #endif
359 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
360 || matches(*argv, "lst") == 0)
361 return tc_qdisc_list(argc-1, argv+1);
362 if (matches(*argv, "help") == 0) {
363 usage();
364 return 0;
365 }
366 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);
367 return -1;
368 }