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