blob: a63c47625a7b27872e48bf340aa14d452cecc07a [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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>
Jussi Kivilinna839c8452008-07-25 16:19:09 +030023#include <malloc.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000024
25#include "utils.h"
26#include "tc_util.h"
27#include "tc_common.h"
28
osdl.net!shemminger11656f12005-03-14 19:02:41 +000029static int usage(void)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000030{
Hasso Teppere5d179d2006-12-10 16:33:05 +020031 fprintf(stderr, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +010032 fprintf(stderr, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000033 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n");
Jussi Kivilinna839c8452008-07-25 16:19:09 +030034 fprintf(stderr, " [ stab [ help | STAB_OPTIONS] ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000035 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr, "\n");
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +010037 fprintf(stderr, " tc qdisc show [ dev STRING ] [ ingress | clsact ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000038 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");
Jussi Kivilinna839c8452008-07-25 16:19:09 +030041 fprintf(stderr, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
osdl.net!shemminger11656f12005-03-14 19:02:41 +000042 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000043}
44
Stephen Hemminger32a121c2016-03-21 11:48:36 -070045static int tc_qdisc_modify(int cmd, unsigned int flags, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000046{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000047 struct qdisc_util *q = NULL;
48 struct tc_estimator est;
Jussi Kivilinna839c8452008-07-25 16:19:09 +030049 struct {
50 struct tc_sizespec szopts;
51 __u16 *data;
52 } stab;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000053 char d[16];
54 char k[16];
osdl.net!shemmingerf307c242004-08-23 20:21:21 +000055 struct {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070056 struct nlmsghdr n;
57 struct tcmsg t;
58 char buf[TCA_BUF_MAX];
osdl.net!shemmingerf307c242004-08-23 20:21:21 +000059 } req;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000060
61 memset(&req, 0, sizeof(req));
Jussi Kivilinna839c8452008-07-25 16:19:09 +030062 memset(&stab, 0, sizeof(stab));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000063 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;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070080
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000081 if (req.t.tcm_handle)
82 duparg("handle", *argv);
83 NEXT_ARG();
84 if (get_qdisc_handle(&handle, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +000085 invarg("invalid qdisc ID", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000086 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");
osdl.net!shemminger11656f12005-03-14 19:02:41 +000090 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000091 }
92 req.t.tcm_parent = TC_H_ROOT;
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +010093 } 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;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000104 } else if (strcmp(*argv, "ingress") == 0) {
105 if (req.t.tcm_parent) {
106 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n");
osdl.net!shemminger11656f12005-03-14 19:02:41 +0000107 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000108 }
109 req.t.tcm_parent = TC_H_INGRESS;
Daniel Borkmann0d45c4b2016-01-12 01:42:19 +0100110 strncpy(k, "ingress", sizeof(k) - 1);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000111 q = get_qdisc_kind(k);
Daniel Borkmann0d45c4b2016-01-12 01:42:19 +0100112 req.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0);
113 NEXT_ARG_FWD();
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000114 break;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000115 } else if (strcmp(*argv, "parent") == 0) {
116 __u32 handle;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700117
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000118 NEXT_ARG();
119 if (req.t.tcm_parent)
120 duparg("parent", *argv);
121 if (get_tc_classid(&handle, *argv))
Dan Kenigsbergf1675d62012-08-16 02:25:56 +0000122 invarg("invalid parent ID", *argv);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000123 req.t.tcm_parent = handle;
124 } else if (matches(*argv, "estimator") == 0) {
125 if (parse_estimator(&argc, &argv, &est))
126 return -1;
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300127 } else if (matches(*argv, "stab") == 0) {
128 if (parse_size_table(&argc, &argv, &stab.szopts) < 0)
129 return -1;
130 continue;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131 } 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
Stephen Hemminger0a502b22013-10-27 12:26:47 -0700148 if (q) {
149 if (q->parse_qopt) {
Stephen Hemmingere9e78b02013-08-26 08:41:19 -0700150 if (q->parse_qopt(q, argc, argv, &req.n))
151 return 1;
Stephen Hemminger0a502b22013-10-27 12:26:47 -0700152 } else if (argc) {
153 fprintf(stderr, "qdisc '%s' does not support option parsing\n", k);
154 return -1;
155 }
156 } else {
157 if (argc) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000158 if (matches(*argv, "help") == 0)
159 usage();
160
161 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv);
osdl.net!shemminger11656f12005-03-14 19:02:41 +0000162 return -1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000163 }
164 }
165
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300166 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
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000186 if (d[0]) {
187 int idx;
188
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800189 ll_init_map(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000190
191 if ((idx = ll_name_to_index(d)) == 0) {
192 fprintf(stderr, "Cannot find device \"%s\"\n", d);
osdl.net!shemminger11656f12005-03-14 19:02:41 +0000193 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000194 }
195 req.t.tcm_ifindex = idx;
196 }
197
Stephen Hemmingerc079e122015-05-27 12:26:14 -0700198 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
osdl.net!shemminger11656f12005-03-14 19:02:41 +0000199 return 2;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201 return 0;
202}
203
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000204static int filter_ifindex;
205
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800206int print_qdisc(const struct sockaddr_nl *who,
207 struct nlmsghdr *n,
osdl.net!shemminger649b0752004-08-31 17:45:21 +0000208 void *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000209{
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700210 FILE *fp = (FILE *)arg;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000211 struct tcmsg *t = NLMSG_DATA(n);
212 int len = n->nlmsg_len;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700213 struct rtattr *tb[TCA_MAX+1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000214 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) {
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000234 fprintf(stderr, "print_qdisc: NULL kind\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000235 return -1;
236 }
237
238 if (n->nlmsg_type == RTM_DELQDISC)
239 fprintf(fp, "deleted ");
240
Stephen Hemmingerff247462012-04-10 08:47:55 -0700241 fprintf(fp, "qdisc %s %x: ", rta_getattr_str(tb[TCA_KIND]), t->tcm_handle>>16);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000242 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 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800253 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
254
osdl.net!shemminger2373fde2004-08-13 23:54:55 +0000255 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]));
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800259
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000260 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");
Jussi Kivilinna839c8452008-07-25 16:19:09 +0300267 if (show_details && tb[TCA_STAB]) {
268 print_size_table(fp, " ", tb[TCA_STAB]);
269 fprintf(fp, "\n");
270 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000271 if (show_stats) {
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000272 struct rtattr *xstats = NULL;
273
274 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) {
275 print_tcstats_attr(fp, tb, " ", &xstats);
osdl.net!shemminger649b0752004-08-31 17:45:21 +0000276 fprintf(fp, "\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000277 }
osdl.net!shemminger649b0752004-08-31 17:45:21 +0000278
ch[shemminger]!tgrafe5879dc2004-12-07 23:52:52 +0000279 if (q && xstats && q->print_xstats) {
280 q->print_xstats(q, fp, xstats);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000281 fprintf(fp, "\n");
282 }
283 }
284 fflush(fp);
285 return 0;
286}
287
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -0800288static int tc_qdisc_list(int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000289{
290 struct tcmsg t;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000291 char d[16];
292
293 memset(&t, 0, sizeof(t));
294 t.tcm_family = AF_UNSPEC;
295 memset(&d, 0, sizeof(d));
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800296
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000297 while (argc > 0) {
298 if (strcmp(*argv, "dev") == 0) {
299 NEXT_ARG();
300 strncpy(d, *argv, sizeof(d)-1);
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700301 } else if (strcmp(*argv, "ingress") == 0 ||
Daniel Borkmann8f9afdd2016-01-12 01:42:20 +0100302 strcmp(*argv, "clsact") == 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700303 if (t.tcm_parent) {
304 fprintf(stderr, "Duplicate parent ID\n");
305 usage();
306 }
307 t.tcm_parent = TC_H_INGRESS;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000308 } 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
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800318 ll_init_map(&rth);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000319
320 if (d[0]) {
321 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
322 fprintf(stderr, "Cannot find device \"%s\"\n", d);
osdl.net!shemminger11656f12005-03-14 19:02:41 +0000323 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000324 }
325 filter_ifindex = t.tcm_ifindex;
326 }
327
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800328 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000329 perror("Cannot send dump request");
osdl.net!shemminger11656f12005-03-14 19:02:41 +0000330 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000331 }
332
Stephen Hemminger3d0b7432014-12-20 15:47:17 -0800333 if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000334 fprintf(stderr, "Dump terminated\n");
osdl.net!shemminger11656f12005-03-14 19:02:41 +0000335 return 1;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000336 }
337
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000338 return 0;
339}
340
341int 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);
Hasso Teppere5d179d2006-12-10 16:33:05 +0200362 if (matches(*argv, "help") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000363 usage();
Hasso Teppere5d179d2006-12-10 16:33:05 +0200364 return 0;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700365 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000366 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);
367 return -1;
368}