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