]>
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>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
26 #include "tc_common.h"
30 struct hlist_node hlist
;
33 struct graph_node
*parent_node
;
34 struct graph_node
*right_node
;
40 static struct hlist_head cls_list
= {};
41 static struct hlist_head root_cls_list
= {};
43 static void usage(void);
45 static void usage(void)
47 fprintf(stderr
, "Usage: tc class [ add | del | change | replace | show ] dev STRING\n");
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");
58 static int tc_class_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
65 struct qdisc_util
*q
= NULL
;
66 struct tc_estimator est
;
70 memset(&req
, 0, sizeof(req
));
71 memset(&est
, 0, sizeof(est
));
72 memset(d
, 0, sizeof(d
));
73 memset(k
, 0, sizeof(k
));
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
;
81 if (strcmp(*argv
, "dev") == 0) {
85 strncpy(d
, *argv
, sizeof(d
)-1);
86 } else if (strcmp(*argv
, "classid") == 0) {
90 duparg("classid", *argv
);
91 if (get_tc_classid(&handle
, *argv
))
92 invarg("invalid class ID", *argv
);
93 req
.t
.tcm_handle
= handle
;
94 } else if (strcmp(*argv
, "handle") == 0) {
95 fprintf(stderr
, "Error: try \"classid\" instead of \"handle\"\n");
97 } else if (strcmp(*argv
, "root") == 0) {
98 if (req
.t
.tcm_parent
) {
99 fprintf(stderr
, "Error: \"root\" is duplicate parent ID.\n");
102 req
.t
.tcm_parent
= TC_H_ROOT
;
103 } else if (strcmp(*argv
, "parent") == 0) {
106 if (req
.t
.tcm_parent
)
107 duparg("parent", *argv
);
108 if (get_tc_classid(&handle
, *argv
))
109 invarg("invalid parent ID", *argv
);
110 req
.t
.tcm_parent
= handle
;
111 } else if (matches(*argv
, "estimator") == 0) {
112 if (parse_estimator(&argc
, &argv
, &est
))
114 } else if (matches(*argv
, "help") == 0) {
117 strncpy(k
, *argv
, sizeof(k
)-1);
119 q
= get_qdisc_kind(k
);
127 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
129 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
132 if (q
->parse_copt
== NULL
) {
133 fprintf(stderr
, "Error: Qdisc \"%s\" is classless.\n", k
);
136 if (q
->parse_copt(q
, argc
, argv
, &req
.n
))
140 if (matches(*argv
, "help") == 0)
142 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv
);
150 if ((req
.t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
151 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
156 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
164 __u32 filter_classid
;
166 static void graph_node_add(__u32 parent_id
, __u32 id
, void *data
,
169 struct graph_node
*node
= malloc(sizeof(struct graph_node
));
171 memset(node
, 0, sizeof(*node
));
173 node
->parent_id
= parent_id
;
176 node
->data
= malloc(len
);
177 node
->data_len
= len
;
178 memcpy(node
->data
, data
, len
);
181 if (parent_id
== TC_H_ROOT
)
182 hlist_add_head(&node
->hlist
, &root_cls_list
);
184 hlist_add_head(&node
->hlist
, &cls_list
);
187 static void graph_indent(char *buf
, struct graph_node
*node
, int is_newline
,
190 char spaces
[100] = {0};
192 while (node
&& node
->parent_node
) {
193 node
->parent_node
->right_node
= node
;
194 node
= node
->parent_node
;
196 while (node
&& node
->right_node
) {
197 if (node
->hlist
.next
)
202 node
= node
->right_node
;
206 if (node
->hlist
.next
&& node
->nodes_count
)
208 else if (node
->hlist
.next
)
210 else if (node
->nodes_count
)
212 else if (!node
->hlist
.next
)
215 if (add_spaces
> 0) {
216 sprintf(spaces
, "%-*s", add_spaces
, "");
221 static void graph_cls_show(FILE *fp
, char *buf
, struct hlist_head
*root_list
,
224 struct hlist_node
*n
, *tmp_cls
;
225 char cls_id_str
[256] = {};
226 struct rtattr
*tb
[TCA_MAX
+ 1] = {};
227 struct qdisc_util
*q
;
230 hlist_for_each_safe(n
, tmp_cls
, root_list
) {
231 struct hlist_node
*c
, *tmp_chld
;
232 struct hlist_head children
= {};
233 struct graph_node
*cls
= container_of(n
, struct graph_node
,
236 hlist_for_each_safe(c
, tmp_chld
, &cls_list
) {
237 struct graph_node
*child
= container_of(c
,
238 struct graph_node
, hlist
);
240 if (cls
->id
== child
->parent_id
) {
242 hlist_add_head(c
, &children
);
244 child
->parent_node
= cls
;
248 graph_indent(buf
, cls
, 0, 0);
250 print_tc_classid(cls_id_str
, sizeof(cls_id_str
), cls
->id
);
251 sprintf(str
, "+---(%s)", cls_id_str
);
254 parse_rtattr(tb
, TCA_MAX
, (struct rtattr
*)cls
->data
,
257 if (tb
[TCA_KIND
] == NULL
) {
258 strcat(buf
, " [unknown qdisc kind] ");
260 const char *kind
= rta_getattr_str(tb
[TCA_KIND
]);
262 sprintf(str
, " %s ", kind
);
264 fprintf(fp
, "%s", buf
);
267 q
= get_qdisc_kind(kind
);
268 if (q
&& q
->print_copt
) {
269 q
->print_copt(q
, fp
, tb
[TCA_OPTIONS
]);
271 if (q
&& show_stats
) {
272 int cls_indent
= strlen(q
->id
) - 2 +
274 struct rtattr
*stats
= NULL
;
276 graph_indent(buf
, cls
, 1, cls_indent
);
278 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
]) {
280 print_tcstats_attr(fp
, tb
, buf
, &stats
);
283 if (cls
->hlist
.next
|| cls
->nodes_count
) {
285 graph_indent(buf
, cls
, 1, 0);
290 fprintf(fp
, "%s\n", buf
);
293 graph_cls_show(fp
, buf
, &children
, level
+ 1);
294 if (!cls
->hlist
.next
) {
295 graph_indent(buf
, cls
, 0, 0);
299 fprintf(fp
, "%s", buf
);
305 int print_class(const struct sockaddr_nl
*who
,
306 struct nlmsghdr
*n
, void *arg
)
308 FILE *fp
= (FILE*)arg
;
309 struct tcmsg
*t
= NLMSG_DATA(n
);
310 int len
= n
->nlmsg_len
;
311 struct rtattr
*tb
[TCA_MAX
+ 1] = {};
312 struct qdisc_util
*q
;
315 if (n
->nlmsg_type
!= RTM_NEWTCLASS
&& n
->nlmsg_type
!= RTM_DELTCLASS
) {
316 fprintf(stderr
, "Not a class\n");
319 len
-= NLMSG_LENGTH(sizeof(*t
));
321 fprintf(stderr
, "Wrong len %d\n", len
);
326 graph_node_add(t
->tcm_parent
, t
->tcm_handle
, TCA_RTA(t
), len
);
330 if (filter_qdisc
&& TC_H_MAJ(t
->tcm_handle
^filter_qdisc
))
333 if (filter_classid
&& t
->tcm_handle
!= filter_classid
)
336 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
338 if (tb
[TCA_KIND
] == NULL
) {
339 fprintf(stderr
, "print_class: NULL kind\n");
343 if (n
->nlmsg_type
== RTM_DELTCLASS
)
344 fprintf(fp
, "deleted ");
349 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_handle
));
351 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_handle
);
353 fprintf(fp
, "class %s %s ", rta_getattr_str(tb
[TCA_KIND
]), abuf
);
355 if (filter_ifindex
== 0)
356 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
358 if (t
->tcm_parent
== TC_H_ROOT
)
359 fprintf(fp
, "root ");
362 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_parent
));
364 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
365 fprintf(fp
, "parent %s ", abuf
);
368 fprintf(fp
, "leaf %x: ", t
->tcm_info
>>16);
369 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
370 if (tb
[TCA_OPTIONS
]) {
371 if (q
&& q
->print_copt
)
372 q
->print_copt(q
, fp
, tb
[TCA_OPTIONS
]);
374 fprintf(fp
, "[cannot parse class parameters]");
378 struct rtattr
*xstats
= NULL
;
380 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
]) {
381 print_tcstats_attr(fp
, tb
, " ", &xstats
);
384 if (q
&& (xstats
|| tb
[TCA_XSTATS
]) && q
->print_xstats
) {
385 q
->print_xstats(q
, fp
, xstats
? : tb
[TCA_XSTATS
]);
394 static int tc_class_list(int argc
, char **argv
)
398 char buf
[1024] = {0};
400 memset(&t
, 0, sizeof(t
));
401 t
.tcm_family
= AF_UNSPEC
;
402 memset(d
, 0, sizeof(d
));
408 if (strcmp(*argv
, "dev") == 0) {
411 duparg("dev", *argv
);
412 strncpy(d
, *argv
, sizeof(d
)-1);
413 } else if (strcmp(*argv
, "qdisc") == 0) {
416 duparg("qdisc", *argv
);
417 if (get_qdisc_handle(&filter_qdisc
, *argv
))
418 invarg("invalid qdisc ID", *argv
);
419 } else if (strcmp(*argv
, "classid") == 0) {
422 duparg("classid", *argv
);
423 if (get_tc_classid(&filter_classid
, *argv
))
424 invarg("invalid class ID", *argv
);
425 } else if (strcmp(*argv
, "root") == 0) {
427 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
430 t
.tcm_parent
= TC_H_ROOT
;
431 } else if (strcmp(*argv
, "parent") == 0) {
434 duparg("parent", *argv
);
436 if (get_tc_classid(&handle
, *argv
))
437 invarg("invalid parent ID", *argv
);
438 t
.tcm_parent
= handle
;
439 } else if (matches(*argv
, "help") == 0) {
442 fprintf(stderr
, "What is \"%s\"? Try \"tc class help\".\n", *argv
);
452 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
453 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
456 filter_ifindex
= t
.tcm_ifindex
;
459 if (rtnl_dump_request(&rth
, RTM_GETTCLASS
, &t
, sizeof(t
)) < 0) {
460 perror("Cannot send dump request");
464 if (rtnl_dump_filter(&rth
, print_class
, stdout
) < 0) {
465 fprintf(stderr
, "Dump terminated\n");
470 graph_cls_show(stdout
, &buf
[0], &root_cls_list
, 0);
475 int do_class(int argc
, char **argv
)
478 return tc_class_list(0, NULL
);
479 if (matches(*argv
, "add") == 0)
480 return tc_class_modify(RTM_NEWTCLASS
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
481 if (matches(*argv
, "change") == 0)
482 return tc_class_modify(RTM_NEWTCLASS
, 0, argc
-1, argv
+1);
483 if (matches(*argv
, "replace") == 0)
484 return tc_class_modify(RTM_NEWTCLASS
, NLM_F_CREATE
, argc
-1, argv
+1);
485 if (matches(*argv
, "delete") == 0)
486 return tc_class_modify(RTM_DELTCLASS
, 0, argc
-1, argv
+1);
488 if (matches(*argv
, "get") == 0)
489 return tc_class_get(RTM_GETTCLASS
, 0, argc
-1, argv
+1);
491 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
492 || matches(*argv
, "lst") == 0)
493 return tc_class_list(argc
-1, argv
+1);
494 if (matches(*argv
, "help") == 0) {
498 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv
);