]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_class.c
2 * tc_class.c "tc class".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
25 #include "tc_common.h"
29 struct hlist_node hlist
;
32 struct graph_node
*parent_node
;
33 struct graph_node
*right_node
;
39 static struct hlist_head cls_list
= {};
40 static struct hlist_head root_cls_list
= {};
42 static void usage(void);
44 static void usage(void)
46 fprintf(stderr
, "Usage: tc class [ add | del | change | replace | show ] dev STRING\n");
47 fprintf(stderr
, " [ classid CLASSID ] [ root | parent CLASSID ]\n");
48 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
49 fprintf(stderr
, "\n");
50 fprintf(stderr
, " tc class show [ dev STRING ] [ root | parent CLASSID ]\n");
51 fprintf(stderr
, "Where:\n");
52 fprintf(stderr
, "QDISC_KIND := { prio | cbq | etc. }\n");
53 fprintf(stderr
, "OPTIONS := ... try tc class add <desired QDISC_KIND> help\n");
56 static int tc_class_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
63 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
64 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
66 .t
.tcm_family
= AF_UNSPEC
,
68 struct qdisc_util
*q
= NULL
;
69 struct tc_estimator est
= {};
70 char d
[IFNAMSIZ
] = {};
71 char k
[FILTER_NAMESZ
] = {};
74 if (strcmp(*argv
, "dev") == 0) {
78 strncpy(d
, *argv
, sizeof(d
)-1);
79 } else if (strcmp(*argv
, "classid") == 0) {
84 duparg("classid", *argv
);
85 if (get_tc_classid(&handle
, *argv
))
86 invarg("invalid class ID", *argv
);
87 req
.t
.tcm_handle
= handle
;
88 } else if (strcmp(*argv
, "handle") == 0) {
89 fprintf(stderr
, "Error: try \"classid\" instead of \"handle\"\n");
91 } else if (strcmp(*argv
, "root") == 0) {
92 if (req
.t
.tcm_parent
) {
93 fprintf(stderr
, "Error: \"root\" is duplicate parent ID.\n");
96 req
.t
.tcm_parent
= TC_H_ROOT
;
97 } else if (strcmp(*argv
, "parent") == 0) {
101 if (req
.t
.tcm_parent
)
102 duparg("parent", *argv
);
103 if (get_tc_classid(&handle
, *argv
))
104 invarg("invalid parent ID", *argv
);
105 req
.t
.tcm_parent
= handle
;
106 } else if (matches(*argv
, "estimator") == 0) {
107 if (parse_estimator(&argc
, &argv
, &est
))
109 } else if (matches(*argv
, "help") == 0) {
112 strncpy(k
, *argv
, sizeof(k
)-1);
114 q
= get_qdisc_kind(k
);
122 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
124 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
127 if (q
->parse_copt
== NULL
) {
128 fprintf(stderr
, "Error: Qdisc \"%s\" is classless.\n", k
);
131 if (q
->parse_copt(q
, argc
, argv
, &req
.n
, d
))
135 if (matches(*argv
, "help") == 0)
137 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv
);
145 req
.t
.tcm_ifindex
= ll_name_to_index(d
);
146 if (!req
.t
.tcm_ifindex
)
150 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
158 __u32 filter_classid
;
160 static void graph_node_add(__u32 parent_id
, __u32 id
, void *data
,
163 struct graph_node
*node
= calloc(1, sizeof(struct graph_node
));
166 node
->parent_id
= parent_id
;
169 node
->data
= malloc(len
);
170 node
->data_len
= len
;
171 memcpy(node
->data
, data
, len
);
174 if (parent_id
== TC_H_ROOT
)
175 hlist_add_head(&node
->hlist
, &root_cls_list
);
177 hlist_add_head(&node
->hlist
, &cls_list
);
180 static void graph_indent(char *buf
, struct graph_node
*node
, int is_newline
,
183 char spaces
[100] = {0};
185 while (node
&& node
->parent_node
) {
186 node
->parent_node
->right_node
= node
;
187 node
= node
->parent_node
;
189 while (node
&& node
->right_node
) {
190 if (node
->hlist
.next
)
195 node
= node
->right_node
;
199 if (node
->hlist
.next
&& node
->nodes_count
)
201 else if (node
->hlist
.next
)
203 else if (node
->nodes_count
)
205 else if (!node
->hlist
.next
)
208 if (add_spaces
> 0) {
209 sprintf(spaces
, "%-*s", add_spaces
, "");
214 static void graph_cls_show(FILE *fp
, char *buf
, struct hlist_head
*root_list
,
217 struct hlist_node
*n
, *tmp_cls
;
218 char cls_id_str
[256] = {};
219 struct rtattr
*tb
[TCA_MAX
+ 1];
220 struct qdisc_util
*q
;
223 hlist_for_each_safe(n
, tmp_cls
, root_list
) {
224 struct hlist_node
*c
, *tmp_chld
;
225 struct hlist_head children
= {};
226 struct graph_node
*cls
= container_of(n
, struct graph_node
,
229 hlist_for_each_safe(c
, tmp_chld
, &cls_list
) {
230 struct graph_node
*child
= container_of(c
,
231 struct graph_node
, hlist
);
233 if (cls
->id
== child
->parent_id
) {
235 hlist_add_head(c
, &children
);
237 child
->parent_node
= cls
;
241 graph_indent(buf
, cls
, 0, 0);
243 print_tc_classid(cls_id_str
, sizeof(cls_id_str
), cls
->id
);
244 snprintf(str
, sizeof(str
),
245 "+---(%s)", cls_id_str
);
248 parse_rtattr(tb
, TCA_MAX
, (struct rtattr
*)cls
->data
,
251 if (tb
[TCA_KIND
] == NULL
) {
252 strcat(buf
, " [unknown qdisc kind] ");
254 const char *kind
= rta_getattr_str(tb
[TCA_KIND
]);
256 sprintf(str
, " %s ", kind
);
258 fprintf(fp
, "%s", buf
);
261 q
= get_qdisc_kind(kind
);
262 if (q
&& q
->print_copt
) {
263 q
->print_copt(q
, fp
, tb
[TCA_OPTIONS
]);
265 if (q
&& show_stats
) {
266 int cls_indent
= strlen(q
->id
) - 2 +
268 struct rtattr
*stats
= NULL
;
270 graph_indent(buf
, cls
, 1, cls_indent
);
272 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
]) {
274 print_tcstats_attr(fp
, tb
, buf
, &stats
);
277 if (cls
->hlist
.next
|| cls
->nodes_count
) {
279 graph_indent(buf
, cls
, 1, 0);
284 fprintf(fp
, "%s\n", buf
);
287 graph_cls_show(fp
, buf
, &children
, level
+ 1);
288 if (!cls
->hlist
.next
) {
289 graph_indent(buf
, cls
, 0, 0);
293 fprintf(fp
, "%s", buf
);
299 int print_class(const struct sockaddr_nl
*who
,
300 struct nlmsghdr
*n
, void *arg
)
302 FILE *fp
= (FILE *)arg
;
303 struct tcmsg
*t
= NLMSG_DATA(n
);
304 int len
= n
->nlmsg_len
;
305 struct rtattr
*tb
[TCA_MAX
+ 1];
306 struct qdisc_util
*q
;
309 if (n
->nlmsg_type
!= RTM_NEWTCLASS
&& n
->nlmsg_type
!= RTM_DELTCLASS
) {
310 fprintf(stderr
, "Not a class\n");
313 len
-= NLMSG_LENGTH(sizeof(*t
));
315 fprintf(stderr
, "Wrong len %d\n", len
);
320 graph_node_add(t
->tcm_parent
, t
->tcm_handle
, TCA_RTA(t
), len
);
324 if (filter_qdisc
&& TC_H_MAJ(t
->tcm_handle
^filter_qdisc
))
327 if (filter_classid
&& t
->tcm_handle
!= filter_classid
)
330 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
332 if (tb
[TCA_KIND
] == NULL
) {
333 fprintf(stderr
, "print_class: NULL kind\n");
337 if (n
->nlmsg_type
== RTM_DELTCLASS
)
338 fprintf(fp
, "deleted ");
343 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_handle
));
345 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_handle
);
347 fprintf(fp
, "class %s %s ", rta_getattr_str(tb
[TCA_KIND
]), abuf
);
349 if (filter_ifindex
== 0)
350 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
352 if (t
->tcm_parent
== TC_H_ROOT
)
353 fprintf(fp
, "root ");
356 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_parent
));
358 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
359 fprintf(fp
, "parent %s ", abuf
);
362 fprintf(fp
, "leaf %x: ", t
->tcm_info
>>16);
363 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
364 if (tb
[TCA_OPTIONS
]) {
365 if (q
&& q
->print_copt
)
366 q
->print_copt(q
, fp
, tb
[TCA_OPTIONS
]);
368 fprintf(fp
, "[cannot parse class parameters]");
372 struct rtattr
*xstats
= NULL
;
374 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
]) {
375 print_tcstats_attr(fp
, tb
, " ", &xstats
);
378 if (q
&& (xstats
|| tb
[TCA_XSTATS
]) && q
->print_xstats
) {
379 q
->print_xstats(q
, fp
, xstats
? : tb
[TCA_XSTATS
]);
388 static int tc_class_list(int argc
, char **argv
)
390 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
391 char d
[IFNAMSIZ
] = {};
392 char buf
[1024] = {0};
398 if (strcmp(*argv
, "dev") == 0) {
401 duparg("dev", *argv
);
402 strncpy(d
, *argv
, sizeof(d
)-1);
403 } else if (strcmp(*argv
, "qdisc") == 0) {
406 duparg("qdisc", *argv
);
407 if (get_qdisc_handle(&filter_qdisc
, *argv
))
408 invarg("invalid qdisc ID", *argv
);
409 } else if (strcmp(*argv
, "classid") == 0) {
412 duparg("classid", *argv
);
413 if (get_tc_classid(&filter_classid
, *argv
))
414 invarg("invalid class ID", *argv
);
415 } else if (strcmp(*argv
, "root") == 0) {
417 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
420 t
.tcm_parent
= TC_H_ROOT
;
421 } else if (strcmp(*argv
, "parent") == 0) {
425 duparg("parent", *argv
);
427 if (get_tc_classid(&handle
, *argv
))
428 invarg("invalid parent ID", *argv
);
429 t
.tcm_parent
= handle
;
430 } else if (matches(*argv
, "help") == 0) {
433 fprintf(stderr
, "What is \"%s\"? Try \"tc class help\".\n", *argv
);
443 t
.tcm_ifindex
= ll_name_to_index(d
);
446 filter_ifindex
= t
.tcm_ifindex
;
449 if (rtnl_dump_request(&rth
, RTM_GETTCLASS
, &t
, sizeof(t
)) < 0) {
450 perror("Cannot send dump request");
454 if (rtnl_dump_filter(&rth
, print_class
, stdout
) < 0) {
455 fprintf(stderr
, "Dump terminated\n");
460 graph_cls_show(stdout
, &buf
[0], &root_cls_list
, 0);
465 int do_class(int argc
, char **argv
)
468 return tc_class_list(0, NULL
);
469 if (matches(*argv
, "add") == 0)
470 return tc_class_modify(RTM_NEWTCLASS
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
471 if (matches(*argv
, "change") == 0)
472 return tc_class_modify(RTM_NEWTCLASS
, 0, argc
-1, argv
+1);
473 if (matches(*argv
, "replace") == 0)
474 return tc_class_modify(RTM_NEWTCLASS
, NLM_F_CREATE
, argc
-1, argv
+1);
475 if (matches(*argv
, "delete") == 0)
476 return tc_class_modify(RTM_DELTCLASS
, 0, argc
-1, argv
+1);
478 if (matches(*argv
, "get") == 0)
479 return tc_class_get(RTM_GETTCLASS
, 0, argc
-1, argv
+1);
481 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
482 || matches(*argv
, "lst") == 0)
483 return tc_class_list(argc
-1, argv
+1);
484 if (matches(*argv
, "help") == 0) {
488 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv
);