]>
Commit | Line | Data |
---|---|---|
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 | 28 | static 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 | 44 | static 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 |
198 | static int filter_ifindex; |
199 | ||
ae665a52 | 200 | int 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 | ||
c91d262f JP |
230 | open_json_object(NULL); |
231 | ||
aba5acdf | 232 | if (n->nlmsg_type == RTM_DELQDISC) |
c91d262f | 233 | print_bool(PRINT_ANY, "deleted", "deleted ", true); |
aba5acdf | 234 | |
274b63ae RM |
235 | if (n->nlmsg_type == RTM_NEWQDISC && |
236 | (n->nlmsg_flags & NLM_F_CREATE) && | |
237 | (n->nlmsg_flags & NLM_F_REPLACE)) | |
c91d262f | 238 | print_bool(PRINT_ANY, "replaced", "replaced ", true); |
274b63ae RM |
239 | |
240 | if (n->nlmsg_type == RTM_NEWQDISC && | |
241 | (n->nlmsg_flags & NLM_F_CREATE) && | |
242 | (n->nlmsg_flags & NLM_F_EXCL)) | |
c91d262f JP |
243 | print_bool(PRINT_ANY, "added", "added ", true); |
244 | ||
245 | print_string(PRINT_ANY, "kind", "qdisc %s", | |
246 | rta_getattr_str(tb[TCA_KIND])); | |
247 | sprintf(abuf, "%x:", t->tcm_handle >> 16); | |
248 | print_string(PRINT_ANY, "handle", " %s", abuf); | |
249 | if (show_raw) { | |
250 | sprintf(abuf, "[%08x]", t->tcm_handle); | |
251 | print_string(PRINT_FP, NULL, "%s", abuf); | |
252 | } | |
253 | print_string(PRINT_FP, NULL, " ", NULL); | |
d42e1444 | 254 | |
aba5acdf | 255 | if (filter_ifindex == 0) |
c91d262f JP |
256 | print_string(PRINT_ANY, "dev", "dev %s ", |
257 | ll_index_to_name(t->tcm_ifindex)); | |
b932e6f3 | 258 | |
aba5acdf | 259 | if (t->tcm_parent == TC_H_ROOT) |
c91d262f | 260 | print_bool(PRINT_ANY, "root", "root ", true); |
aba5acdf SH |
261 | else if (t->tcm_parent) { |
262 | print_tc_classid(abuf, sizeof(abuf), t->tcm_parent); | |
c91d262f | 263 | print_string(PRINT_ANY, "parent", "parent %s ", abuf); |
aba5acdf | 264 | } |
b932e6f3 SH |
265 | |
266 | if (t->tcm_info != 1) | |
c91d262f | 267 | print_uint(PRINT_ANY, "refcnt", "refcnt %u ", t->tcm_info); |
ae665a52 | 268 | |
b932e6f3 SH |
269 | /* pfifo_fast is generic enough to warrant the hardcoding --JHS */ |
270 | if (strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND])) == 0) | |
2373fde9 SH |
271 | q = get_qdisc_kind("prio"); |
272 | else | |
273 | q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND])); | |
ae665a52 | 274 | |
c91d262f | 275 | open_json_object("options"); |
aba5acdf SH |
276 | if (tb[TCA_OPTIONS]) { |
277 | if (q) | |
278 | q->print_qopt(q, fp, tb[TCA_OPTIONS]); | |
279 | else | |
c91d262f JP |
280 | print_string(PRINT_FP, NULL, |
281 | "[cannot parse qdisc parameters]", NULL); | |
aba5acdf | 282 | } |
c91d262f JP |
283 | close_json_object(); |
284 | ||
285 | print_string(PRINT_FP, NULL, "\n", NULL); | |
b932e6f3 | 286 | |
839c8456 JK |
287 | if (show_details && tb[TCA_STAB]) { |
288 | print_size_table(fp, " ", tb[TCA_STAB]); | |
c91d262f | 289 | print_string(PRINT_FP, NULL, "\n", NULL); |
839c8456 | 290 | } |
b932e6f3 | 291 | |
aba5acdf | 292 | if (show_stats) { |
e5879dc6 | 293 | struct rtattr *xstats = NULL; |
294 | ||
295 | if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) { | |
296 | print_tcstats_attr(fp, tb, " ", &xstats); | |
c91d262f | 297 | print_string(PRINT_FP, NULL, "\n", NULL); |
aba5acdf | 298 | } |
649b0755 | 299 | |
e5879dc6 | 300 | if (q && xstats && q->print_xstats) { |
301 | q->print_xstats(q, fp, xstats); | |
c91d262f | 302 | print_string(PRINT_FP, NULL, "\n", NULL); |
aba5acdf SH |
303 | } |
304 | } | |
c91d262f | 305 | close_json_object(); |
aba5acdf SH |
306 | fflush(fp); |
307 | return 0; | |
308 | } | |
309 | ||
d1f28cf1 | 310 | static int tc_qdisc_list(int argc, char **argv) |
aba5acdf | 311 | { |
d17b136f | 312 | struct tcmsg t = { .tcm_family = AF_UNSPEC }; |
b317557f | 313 | char d[IFNAMSIZ] = {}; |
7c581a12 | 314 | bool dump_invisible = false; |
ae665a52 | 315 | |
aba5acdf SH |
316 | while (argc > 0) { |
317 | if (strcmp(*argv, "dev") == 0) { | |
318 | NEXT_ARG(); | |
319 | strncpy(d, *argv, sizeof(d)-1); | |
32a121cb | 320 | } else if (strcmp(*argv, "ingress") == 0 || |
8f9afdd5 | 321 | strcmp(*argv, "clsact") == 0) { |
b932e6f3 SH |
322 | if (t.tcm_parent) { |
323 | fprintf(stderr, "Duplicate parent ID\n"); | |
324 | usage(); | |
325 | } | |
326 | t.tcm_parent = TC_H_INGRESS; | |
aba5acdf SH |
327 | } else if (matches(*argv, "help") == 0) { |
328 | usage(); | |
7c581a12 JK |
329 | } else if (strcmp(*argv, "invisible") == 0) { |
330 | dump_invisible = true; | |
aba5acdf SH |
331 | } else { |
332 | fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv); | |
333 | return -1; | |
334 | } | |
335 | ||
336 | argc--; argv++; | |
337 | } | |
338 | ||
3d0b7439 | 339 | ll_init_map(&rth); |
aba5acdf SH |
340 | |
341 | if (d[0]) { | |
b932e6f3 SH |
342 | t.tcm_ifindex = ll_name_to_index(d); |
343 | if (t.tcm_ifindex == 0) { | |
aba5acdf | 344 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
11656f1b | 345 | return 1; |
aba5acdf SH |
346 | } |
347 | filter_ifindex = t.tcm_ifindex; | |
348 | } | |
349 | ||
7c581a12 JK |
350 | if (dump_invisible) { |
351 | struct { | |
352 | struct nlmsghdr n; | |
353 | struct tcmsg t; | |
354 | char buf[256]; | |
355 | } req = { | |
356 | .n.nlmsg_type = RTM_GETQDISC, | |
357 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)), | |
358 | }; | |
359 | ||
360 | req.t.tcm_family = AF_UNSPEC; | |
361 | ||
362 | addattr(&req.n, 256, TCA_DUMP_INVISIBLE); | |
363 | if (rtnl_dump_request_n(&rth, &req.n) < 0) { | |
364 | perror("Cannot send dump request"); | |
365 | return 1; | |
366 | } | |
367 | ||
368 | } else if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) { | |
aba5acdf | 369 | perror("Cannot send dump request"); |
11656f1b | 370 | return 1; |
aba5acdf SH |
371 | } |
372 | ||
c91d262f | 373 | new_json_obj(json); |
3d0b7439 | 374 | if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) { |
aba5acdf | 375 | fprintf(stderr, "Dump terminated\n"); |
11656f1b | 376 | return 1; |
aba5acdf | 377 | } |
c91d262f | 378 | delete_json_obj(); |
aba5acdf | 379 | |
aba5acdf SH |
380 | return 0; |
381 | } | |
382 | ||
383 | int do_qdisc(int argc, char **argv) | |
384 | { | |
385 | if (argc < 1) | |
386 | return tc_qdisc_list(0, NULL); | |
387 | if (matches(*argv, "add") == 0) | |
388 | return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1); | |
389 | if (matches(*argv, "change") == 0) | |
390 | return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1); | |
391 | if (matches(*argv, "replace") == 0) | |
392 | return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); | |
393 | if (matches(*argv, "link") == 0) | |
394 | return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1); | |
395 | if (matches(*argv, "delete") == 0) | |
396 | return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1); | |
397 | #if 0 | |
398 | if (matches(*argv, "get") == 0) | |
399 | return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1); | |
400 | #endif | |
401 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 | |
402 | || matches(*argv, "lst") == 0) | |
403 | return tc_qdisc_list(argc-1, argv+1); | |
e5d179d8 | 404 | if (matches(*argv, "help") == 0) { |
aba5acdf | 405 | usage(); |
e5d179d8 | 406 | return 0; |
32a121cb | 407 | } |
aba5acdf SH |
408 | fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv); |
409 | return -1; | |
410 | } |