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