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