]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_class.c
tc: m_action: Improve conversion to C99 style initializers
[mirror_iproute2.git] / tc / tc_class.c
CommitLineData
aba5acdf
SH
1/*
2 * tc_class.c "tc class".
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 *
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"
4952b459 27#include "list.h"
d954b34a
VK
28
29struct graph_node {
30 struct hlist_node hlist;
31 __u32 id;
32 __u32 parent_id;
33 struct graph_node *parent_node;
34 struct graph_node *right_node;
35 void *data;
36 int data_len;
37 int nodes_count;
38};
39
40static struct hlist_head cls_list = {};
41static struct hlist_head root_cls_list = {};
aba5acdf 42
b611d516 43static void usage(void);
aba5acdf
SH
44
45static void usage(void)
46{
e5d179d8 47 fprintf(stderr, "Usage: tc class [ add | del | change | replace | show ] dev STRING\n");
aba5acdf
SH
48 fprintf(stderr, " [ classid CLASSID ] [ root | parent CLASSID ]\n");
49 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
50 fprintf(stderr, "\n");
51 fprintf(stderr, " tc class show [ dev STRING ] [ root | parent CLASSID ]\n");
52 fprintf(stderr, "Where:\n");
53 fprintf(stderr, "QDISC_KIND := { prio | cbq | etc. }\n");
54 fprintf(stderr, "OPTIONS := ... try tc class add <desired QDISC_KIND> help\n");
b611d516 55 return;
aba5acdf
SH
56}
57
32a121cb 58static int tc_class_modify(int cmd, unsigned int flags, int argc, char **argv)
aba5acdf 59{
aba5acdf 60 struct {
32a121cb
SH
61 struct nlmsghdr n;
62 struct tcmsg t;
63 char buf[4096];
aba5acdf
SH
64 } req;
65 struct qdisc_util *q = NULL;
66 struct tc_estimator est;
67 char d[16];
68 char k[16];
69
70 memset(&req, 0, sizeof(req));
71 memset(&est, 0, sizeof(est));
72 memset(d, 0, sizeof(d));
73 memset(k, 0, sizeof(k));
74
75 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
76 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
77 req.n.nlmsg_type = cmd;
78 req.t.tcm_family = AF_UNSPEC;
79
80 while (argc > 0) {
81 if (strcmp(*argv, "dev") == 0) {
82 NEXT_ARG();
83 if (d[0])
84 duparg("dev", *argv);
85 strncpy(d, *argv, sizeof(d)-1);
86 } else if (strcmp(*argv, "classid") == 0) {
87 __u32 handle;
32a121cb 88
aba5acdf
SH
89 NEXT_ARG();
90 if (req.t.tcm_handle)
91 duparg("classid", *argv);
92 if (get_tc_classid(&handle, *argv))
f1675d61 93 invarg("invalid class ID", *argv);
aba5acdf 94 req.t.tcm_handle = handle;
7e6b809c
SH
95 } else if (strcmp(*argv, "handle") == 0) {
96 fprintf(stderr, "Error: try \"classid\" instead of \"handle\"\n");
97 return -1;
3d0b7439 98 } else if (strcmp(*argv, "root") == 0) {
aba5acdf
SH
99 if (req.t.tcm_parent) {
100 fprintf(stderr, "Error: \"root\" is duplicate parent ID.\n");
b611d516 101 return -1;
aba5acdf
SH
102 }
103 req.t.tcm_parent = TC_H_ROOT;
104 } else if (strcmp(*argv, "parent") == 0) {
105 __u32 handle;
32a121cb 106
aba5acdf
SH
107 NEXT_ARG();
108 if (req.t.tcm_parent)
109 duparg("parent", *argv);
110 if (get_tc_classid(&handle, *argv))
f1675d61 111 invarg("invalid parent ID", *argv);
aba5acdf
SH
112 req.t.tcm_parent = handle;
113 } else if (matches(*argv, "estimator") == 0) {
114 if (parse_estimator(&argc, &argv, &est))
115 return -1;
116 } else if (matches(*argv, "help") == 0) {
117 usage();
118 } else {
119 strncpy(k, *argv, sizeof(k)-1);
120
121 q = get_qdisc_kind(k);
122 argc--; argv++;
123 break;
124 }
125 argc--; argv++;
126 }
127
128 if (k[0])
129 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
130 if (est.ewma_log)
131 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
132
133 if (q) {
134 if (q->parse_copt == NULL) {
135 fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k);
b611d516 136 return 1;
aba5acdf
SH
137 }
138 if (q->parse_copt(q, argc, argv, &req.n))
b611d516 139 return 1;
aba5acdf
SH
140 } else {
141 if (argc) {
142 if (matches(*argv, "help") == 0)
143 usage();
144 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv);
b611d516 145 return -1;
aba5acdf
SH
146 }
147 }
148
aba5acdf 149 if (d[0]) {
7901660a 150 ll_init_map(&rth);
aba5acdf
SH
151
152 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
153 fprintf(stderr, "Cannot find device \"%s\"\n", d);
b611d516 154 return 1;
aba5acdf
SH
155 }
156 }
157
c079e121 158 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
b611d516 159 return 2;
aba5acdf 160
aba5acdf
SH
161 return 0;
162}
163
aba5acdf
SH
164int filter_ifindex;
165__u32 filter_qdisc;
f4a8b23d 166__u32 filter_classid;
aba5acdf 167
d954b34a
VK
168static void graph_node_add(__u32 parent_id, __u32 id, void *data,
169 int len)
170{
171 struct graph_node *node = malloc(sizeof(struct graph_node));
172
173 memset(node, 0, sizeof(*node));
174 node->id = id;
175 node->parent_id = parent_id;
176
177 if (data && len) {
178 node->data = malloc(len);
179 node->data_len = len;
180 memcpy(node->data, data, len);
181 }
182
183 if (parent_id == TC_H_ROOT)
184 hlist_add_head(&node->hlist, &root_cls_list);
185 else
186 hlist_add_head(&node->hlist, &cls_list);
187}
188
189static void graph_indent(char *buf, struct graph_node *node, int is_newline,
190 int add_spaces)
191{
192 char spaces[100] = {0};
193
194 while (node && node->parent_node) {
195 node->parent_node->right_node = node;
196 node = node->parent_node;
197 }
198 while (node && node->right_node) {
199 if (node->hlist.next)
200 strcat(buf, "| ");
201 else
202 strcat(buf, " ");
203
204 node = node->right_node;
205 }
206
207 if (is_newline) {
208 if (node->hlist.next && node->nodes_count)
209 strcat(buf, "| |");
210 else if (node->hlist.next)
211 strcat(buf, "| ");
212 else if (node->nodes_count)
213 strcat(buf, " |");
214 else if (!node->hlist.next)
215 strcat(buf, " ");
216 }
217 if (add_spaces > 0) {
218 sprintf(spaces, "%-*s", add_spaces, "");
219 strcat(buf, spaces);
220 }
221}
222
223static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list,
224 int level)
225{
226 struct hlist_node *n, *tmp_cls;
227 char cls_id_str[256] = {};
228 struct rtattr *tb[TCA_MAX + 1] = {};
229 struct qdisc_util *q;
230 char str[100] = {};
231
232 hlist_for_each_safe(n, tmp_cls, root_list) {
233 struct hlist_node *c, *tmp_chld;
234 struct hlist_head children = {};
235 struct graph_node *cls = container_of(n, struct graph_node,
236 hlist);
237
238 hlist_for_each_safe(c, tmp_chld, &cls_list) {
239 struct graph_node *child = container_of(c,
240 struct graph_node, hlist);
241
242 if (cls->id == child->parent_id) {
243 hlist_del(c);
244 hlist_add_head(c, &children);
245 cls->nodes_count++;
246 child->parent_node = cls;
247 }
248 }
249
250 graph_indent(buf, cls, 0, 0);
251
252 print_tc_classid(cls_id_str, sizeof(cls_id_str), cls->id);
253 sprintf(str, "+---(%s)", cls_id_str);
254 strcat(buf, str);
255
256 parse_rtattr(tb, TCA_MAX, (struct rtattr *)cls->data,
257 cls->data_len);
258
259 if (tb[TCA_KIND] == NULL) {
260 strcat(buf, " [unknown qdisc kind] ");
261 } else {
262 const char *kind = rta_getattr_str(tb[TCA_KIND]);
263
264 sprintf(str, " %s ", kind);
265 strcat(buf, str);
266 fprintf(fp, "%s", buf);
267 buf[0] = '\0';
268
269 q = get_qdisc_kind(kind);
270 if (q && q->print_copt) {
271 q->print_copt(q, fp, tb[TCA_OPTIONS]);
272 }
273 if (q && show_stats) {
274 int cls_indent = strlen(q->id) - 2 +
275 strlen(cls_id_str);
276 struct rtattr *stats = NULL;
277
278 graph_indent(buf, cls, 1, cls_indent);
279
280 if (tb[TCA_STATS] || tb[TCA_STATS2]) {
281 fprintf(fp, "\n");
282 print_tcstats_attr(fp, tb, buf, &stats);
283 buf[0] = '\0';
284 }
285 if (cls->hlist.next || cls->nodes_count) {
286 strcat(buf, "\n");
287 graph_indent(buf, cls, 1, 0);
288 }
289 }
290 }
291 free(cls->data);
292 fprintf(fp, "%s\n", buf);
293 buf[0] = '\0';
294
295 graph_cls_show(fp, buf, &children, level + 1);
296 if (!cls->hlist.next) {
297 graph_indent(buf, cls, 0, 0);
298 strcat(buf, "\n");
299 }
300
301 fprintf(fp, "%s", buf);
302 buf[0] = '\0';
303 free(cls);
304 }
305}
306
ae665a52 307int print_class(const struct sockaddr_nl *who,
50772dc5 308 struct nlmsghdr *n, void *arg)
aba5acdf 309{
32a121cb 310 FILE *fp = (FILE *)arg;
aba5acdf
SH
311 struct tcmsg *t = NLMSG_DATA(n);
312 int len = n->nlmsg_len;
d954b34a 313 struct rtattr *tb[TCA_MAX + 1] = {};
aba5acdf
SH
314 struct qdisc_util *q;
315 char abuf[256];
316
317 if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) {
318 fprintf(stderr, "Not a class\n");
319 return 0;
320 }
321 len -= NLMSG_LENGTH(sizeof(*t));
322 if (len < 0) {
323 fprintf(stderr, "Wrong len %d\n", len);
324 return -1;
325 }
d954b34a
VK
326
327 if (show_graph) {
328 graph_node_add(t->tcm_parent, t->tcm_handle, TCA_RTA(t), len);
329 return 0;
330 }
331
aba5acdf
SH
332 if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc))
333 return 0;
334
f4a8b23d
DF
335 if (filter_classid && t->tcm_handle != filter_classid)
336 return 0;
337
aba5acdf
SH
338 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
339
340 if (tb[TCA_KIND] == NULL) {
2373fde9 341 fprintf(stderr, "print_class: NULL kind\n");
aba5acdf
SH
342 return -1;
343 }
344
345 if (n->nlmsg_type == RTM_DELTCLASS)
346 fprintf(fp, "deleted ");
347
348 abuf[0] = 0;
349 if (t->tcm_handle) {
350 if (filter_qdisc)
351 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle));
352 else
353 print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
354 }
ff24746c 355 fprintf(fp, "class %s %s ", rta_getattr_str(tb[TCA_KIND]), abuf);
aba5acdf
SH
356
357 if (filter_ifindex == 0)
358 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
359
360 if (t->tcm_parent == TC_H_ROOT)
361 fprintf(fp, "root ");
362 else {
363 if (filter_qdisc)
364 print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
365 else
366 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
367 fprintf(fp, "parent %s ", abuf);
368 }
369 if (t->tcm_info)
370 fprintf(fp, "leaf %x: ", t->tcm_info>>16);
371 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
372 if (tb[TCA_OPTIONS]) {
373 if (q && q->print_copt)
374 q->print_copt(q, fp, tb[TCA_OPTIONS]);
375 else
376 fprintf(fp, "[cannot parse class parameters]");
377 }
378 fprintf(fp, "\n");
379 if (show_stats) {
e5879dc6 380 struct rtattr *xstats = NULL;
ae665a52 381
e5879dc6 382 if (tb[TCA_STATS] || tb[TCA_STATS2]) {
383 print_tcstats_attr(fp, tb, " ", &xstats);
2c5474ad 384 fprintf(fp, "\n");
aba5acdf 385 }
e5879dc6 386 if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) {
387 q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]);
aba5acdf
SH
388 fprintf(fp, "\n");
389 }
390 }
391 fflush(fp);
392 return 0;
393}
394
395
d1f28cf1 396static int tc_class_list(int argc, char **argv)
aba5acdf
SH
397{
398 struct tcmsg t;
aba5acdf 399 char d[16];
d954b34a 400 char buf[1024] = {0};
aba5acdf
SH
401
402 memset(&t, 0, sizeof(t));
403 t.tcm_family = AF_UNSPEC;
404 memset(d, 0, sizeof(d));
405
9bea14ff
NK
406 filter_qdisc = 0;
407 filter_classid = 0;
408
aba5acdf
SH
409 while (argc > 0) {
410 if (strcmp(*argv, "dev") == 0) {
411 NEXT_ARG();
412 if (d[0])
413 duparg("dev", *argv);
414 strncpy(d, *argv, sizeof(d)-1);
415 } else if (strcmp(*argv, "qdisc") == 0) {
416 NEXT_ARG();
417 if (filter_qdisc)
418 duparg("qdisc", *argv);
419 if (get_qdisc_handle(&filter_qdisc, *argv))
f1675d61 420 invarg("invalid qdisc ID", *argv);
f4a8b23d
DF
421 } else if (strcmp(*argv, "classid") == 0) {
422 NEXT_ARG();
423 if (filter_classid)
424 duparg("classid", *argv);
425 if (get_tc_classid(&filter_classid, *argv))
f1675d61 426 invarg("invalid class ID", *argv);
aba5acdf
SH
427 } else if (strcmp(*argv, "root") == 0) {
428 if (t.tcm_parent) {
429 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
b611d516 430 return -1;
aba5acdf
SH
431 }
432 t.tcm_parent = TC_H_ROOT;
433 } else if (strcmp(*argv, "parent") == 0) {
434 __u32 handle;
32a121cb 435
aba5acdf
SH
436 if (t.tcm_parent)
437 duparg("parent", *argv);
438 NEXT_ARG();
439 if (get_tc_classid(&handle, *argv))
f1675d61 440 invarg("invalid parent ID", *argv);
aba5acdf
SH
441 t.tcm_parent = handle;
442 } else if (matches(*argv, "help") == 0) {
443 usage();
444 } else {
445 fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv);
b611d516 446 return -1;
aba5acdf
SH
447 }
448
449 argc--; argv++;
450 }
451
3d0b7439 452 ll_init_map(&rth);
aba5acdf
SH
453
454 if (d[0]) {
455 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
456 fprintf(stderr, "Cannot find device \"%s\"\n", d);
b611d516 457 return 1;
aba5acdf
SH
458 }
459 filter_ifindex = t.tcm_ifindex;
460 }
461
3d0b7439 462 if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
aba5acdf 463 perror("Cannot send dump request");
b611d516 464 return 1;
aba5acdf
SH
465 }
466
3d0b7439 467 if (rtnl_dump_filter(&rth, print_class, stdout) < 0) {
aba5acdf 468 fprintf(stderr, "Dump terminated\n");
b611d516 469 return 1;
aba5acdf
SH
470 }
471
d954b34a
VK
472 if (show_graph)
473 graph_cls_show(stdout, &buf[0], &root_cls_list, 0);
474
aba5acdf
SH
475 return 0;
476}
477
478int do_class(int argc, char **argv)
479{
480 if (argc < 1)
481 return tc_class_list(0, NULL);
482 if (matches(*argv, "add") == 0)
483 return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
484 if (matches(*argv, "change") == 0)
485 return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1);
486 if (matches(*argv, "replace") == 0)
487 return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1);
488 if (matches(*argv, "delete") == 0)
489 return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1);
490#if 0
491 if (matches(*argv, "get") == 0)
492 return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1);
493#endif
494 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
495 || matches(*argv, "lst") == 0)
496 return tc_class_list(argc-1, argv+1);
e5d179d8 497 if (matches(*argv, "help") == 0) {
aba5acdf 498 usage();
e5d179d8
HT
499 return 0;
500 }
aba5acdf
SH
501 fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv);
502 return -1;
503}