]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_qdisc.c
Auto merged
[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 void usage(void) __attribute__((noreturn));
29
30 static void 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 exit(-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 exit(-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 exit(-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 exit(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 exit(-1);
139 }
140 }
141
142 if (rtnl_open(&rth, 0) < 0) {
143 fprintf(stderr, "Cannot open rtnetlink\n");
144 rtnl_close(&rth);
145 exit(1);
146 }
147
148 if (d[0]) {
149 int idx;
150
151 ll_init_map(&rth);
152
153 if ((idx = ll_name_to_index(d)) == 0) {
154 fprintf(stderr, "Cannot find device \"%s\"\n", d);
155 rtnl_close(&rth);
156 exit(1);
157 }
158 req.t.tcm_ifindex = idx;
159 }
160
161 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
162 rtnl_close(&rth);
163 exit(2);
164 }
165
166 rtnl_close(&rth);
167 return 0;
168 }
169
170 void print_tcstats(FILE *fp, struct tc_stats *st)
171 {
172 SPRINT_BUF(b1);
173
174 fprintf(fp, " Sent %llu bytes %u pkts (dropped %u, overlimits %u ) ",
175 (unsigned long long)st->bytes, st->packets, st->drops, st->overlimits);
176 if (st->bps || st->pps || st->qlen || st->backlog) {
177 fprintf(fp, "\n ");
178 if (st->bps || st->pps) {
179 fprintf(fp, "rate ");
180 if (st->bps)
181 fprintf(fp, "%s ", sprint_rate(st->bps, b1));
182 if (st->pps)
183 fprintf(fp, "%upps ", st->pps);
184 }
185 if (st->qlen || st->backlog) {
186 fprintf(fp, "backlog ");
187 if (st->backlog)
188 fprintf(fp, "%s ", sprint_size(st->backlog, b1));
189 if (st->qlen)
190 fprintf(fp, "%up ", st->qlen);
191 }
192 }
193 }
194
195 static int filter_ifindex;
196
197 int print_qdisc(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
198 {
199 FILE *fp = (FILE*)arg;
200 struct tcmsg *t = NLMSG_DATA(n);
201 int len = n->nlmsg_len;
202 struct rtattr * tb[TCA_MAX+1];
203 struct qdisc_util *q;
204 char abuf[256];
205
206 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) {
207 fprintf(stderr, "Not a qdisc\n");
208 return 0;
209 }
210 len -= NLMSG_LENGTH(sizeof(*t));
211 if (len < 0) {
212 fprintf(stderr, "Wrong len %d\n", len);
213 return -1;
214 }
215
216 if (filter_ifindex && filter_ifindex != t->tcm_ifindex)
217 return 0;
218
219 memset(tb, 0, sizeof(tb));
220 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
221
222 if (tb[TCA_KIND] == NULL) {
223 fprintf(stderr, "print_qdisc: NULL kind\n");
224 return -1;
225 }
226
227 if (n->nlmsg_type == RTM_DELQDISC)
228 fprintf(fp, "deleted ");
229
230 fprintf(fp, "qdisc %s %x: ", (char*)RTA_DATA(tb[TCA_KIND]), t->tcm_handle>>16);
231 if (filter_ifindex == 0)
232 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
233 if (t->tcm_parent == TC_H_ROOT)
234 fprintf(fp, "root ");
235 else if (t->tcm_parent) {
236 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
237 fprintf(fp, "parent %s ", abuf);
238 }
239 if (t->tcm_info != 1) {
240 fprintf(fp, "refcnt %d ", t->tcm_info);
241 }
242 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
243
244 if (0 == strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])))
245 q = get_qdisc_kind("prio");
246 else
247 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
248
249 if (tb[TCA_OPTIONS]) {
250 if (q)
251 q->print_qopt(q, fp, tb[TCA_OPTIONS]);
252 else
253 fprintf(fp, "[cannot parse qdisc parameters]");
254 }
255 fprintf(fp, "\n");
256 if (show_stats) {
257 if (tb[TCA_STATS]) {
258 if (RTA_PAYLOAD(tb[TCA_STATS]) < sizeof(struct tc_stats))
259 fprintf(fp, "statistics truncated");
260 else {
261 struct tc_stats st;
262 memcpy(&st, RTA_DATA(tb[TCA_STATS]), sizeof(st));
263 print_tcstats(fp, &st);
264 fprintf(fp, "\n");
265 }
266 }
267 if (q && tb[TCA_XSTATS]) {
268 q->print_xstats(q, fp, tb[TCA_XSTATS]);
269 fprintf(fp, "\n");
270 }
271 }
272 fflush(fp);
273 return 0;
274 }
275
276
277 int tc_qdisc_list(int argc, char **argv)
278 {
279 struct tcmsg t;
280 struct rtnl_handle rth;
281 char d[16];
282
283 memset(&t, 0, sizeof(t));
284 t.tcm_family = AF_UNSPEC;
285 memset(&d, 0, sizeof(d));
286
287 while (argc > 0) {
288 if (strcmp(*argv, "dev") == 0) {
289 NEXT_ARG();
290 strncpy(d, *argv, sizeof(d)-1);
291 #ifdef TC_H_INGRESS
292 } else if (strcmp(*argv, "ingress") == 0) {
293 if (t.tcm_parent) {
294 fprintf(stderr, "Duplicate parent ID\n");
295 usage();
296 }
297 t.tcm_parent = TC_H_INGRESS;
298 #endif
299 } else if (matches(*argv, "help") == 0) {
300 usage();
301 } else {
302 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv);
303 return -1;
304 }
305
306 argc--; argv++;
307 }
308
309 if (rtnl_open(&rth, 0) < 0) {
310 fprintf(stderr, "Cannot open rtnetlink\n");
311 exit(1);
312 }
313
314 ll_init_map(&rth);
315
316 if (d[0]) {
317 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
318 fprintf(stderr, "Cannot find device \"%s\"\n", d);
319 rtnl_close(&rth);
320 exit(1);
321 }
322 filter_ifindex = t.tcm_ifindex;
323 }
324
325 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
326 perror("Cannot send dump request");
327 rtnl_close(&rth);
328 exit(1);
329 }
330
331 if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) {
332 fprintf(stderr, "Dump terminated\n");
333 rtnl_close(&rth);
334 exit(1);
335 }
336
337 rtnl_close(&rth);
338 return 0;
339 }
340
341 int 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);
362 if (matches(*argv, "help") == 0)
363 usage();
364 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv);
365 return -1;
366 }