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