]>
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 int 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) {
91 duparg("classid", *argv
);
92 if (get_tc_classid(&handle
, *argv
))
93 invarg("invalid class ID", *argv
);
94 req
.t
.tcm_handle
= handle
;
95 } else if (strcmp(*argv
, "handle") == 0) {
96 fprintf(stderr
, "Error: try \"classid\" instead of \"handle\"\n");
98 } else if (strcmp(*argv
, "root") == 0) {
99 if (req
.t
.tcm_parent
) {
100 fprintf(stderr
, "Error: \"root\" is duplicate parent ID.\n");
103 req
.t
.tcm_parent
= TC_H_ROOT
;
104 } else if (strcmp(*argv
, "parent") == 0) {
108 if (req
.t
.tcm_parent
)
109 duparg("parent", *argv
);
110 if (get_tc_classid(&handle
, *argv
))
111 invarg("invalid parent ID", *argv
);
112 req
.t
.tcm_parent
= handle
;
113 } else if (matches(*argv
, "estimator") == 0) {
114 if (parse_estimator(&argc
, &argv
, &est
))
116 } else if (matches(*argv
, "help") == 0) {
119 strncpy(k
, *argv
, sizeof(k
)-1);
121 q
= get_qdisc_kind(k
);
129 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
131 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
134 if (q
->parse_copt
== NULL
) {
135 fprintf(stderr
, "Error: Qdisc \"%s\" is classless.\n", k
);
138 if (q
->parse_copt(q
, argc
, argv
, &req
.n
))
142 if (matches(*argv
, "help") == 0)
144 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv
);
152 if ((req
.t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
153 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
158 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
166 __u32 filter_classid
;
168 static void graph_node_add(__u32 parent_id
, __u32 id
, void *data
,
171 struct graph_node
*node
= malloc(sizeof(struct graph_node
));
173 memset(node
, 0, sizeof(*node
));
175 node
->parent_id
= parent_id
;
178 node
->data
= malloc(len
);
179 node
->data_len
= len
;
180 memcpy(node
->data
, data
, len
);
183 if (parent_id
== TC_H_ROOT
)
184 hlist_add_head(&node
->hlist
, &root_cls_list
);
186 hlist_add_head(&node
->hlist
, &cls_list
);
189 static void graph_indent(char *buf
, struct graph_node
*node
, int is_newline
,
192 char spaces
[100] = {0};
194 while (node
&& node
->parent_node
) {
195 node
->parent_node
->right_node
= node
;
196 node
= node
->parent_node
;
198 while (node
&& node
->right_node
) {
199 if (node
->hlist
.next
)
204 node
= node
->right_node
;
208 if (node
->hlist
.next
&& node
->nodes_count
)
210 else if (node
->hlist
.next
)
212 else if (node
->nodes_count
)
214 else if (!node
->hlist
.next
)
217 if (add_spaces
> 0) {
218 sprintf(spaces
, "%-*s", add_spaces
, "");
223 static void graph_cls_show(FILE *fp
, char *buf
, struct hlist_head
*root_list
,
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
;
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
,
238 hlist_for_each_safe(c
, tmp_chld
, &cls_list
) {
239 struct graph_node
*child
= container_of(c
,
240 struct graph_node
, hlist
);
242 if (cls
->id
== child
->parent_id
) {
244 hlist_add_head(c
, &children
);
246 child
->parent_node
= cls
;
250 graph_indent(buf
, cls
, 0, 0);
252 print_tc_classid(cls_id_str
, sizeof(cls_id_str
), cls
->id
);
253 sprintf(str
, "+---(%s)", cls_id_str
);
256 parse_rtattr(tb
, TCA_MAX
, (struct rtattr
*)cls
->data
,
259 if (tb
[TCA_KIND
] == NULL
) {
260 strcat(buf
, " [unknown qdisc kind] ");
262 const char *kind
= rta_getattr_str(tb
[TCA_KIND
]);
264 sprintf(str
, " %s ", kind
);
266 fprintf(fp
, "%s", buf
);
269 q
= get_qdisc_kind(kind
);
270 if (q
&& q
->print_copt
) {
271 q
->print_copt(q
, fp
, tb
[TCA_OPTIONS
]);
273 if (q
&& show_stats
) {
274 int cls_indent
= strlen(q
->id
) - 2 +
276 struct rtattr
*stats
= NULL
;
278 graph_indent(buf
, cls
, 1, cls_indent
);
280 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
]) {
282 print_tcstats_attr(fp
, tb
, buf
, &stats
);
285 if (cls
->hlist
.next
|| cls
->nodes_count
) {
287 graph_indent(buf
, cls
, 1, 0);
292 fprintf(fp
, "%s\n", buf
);
295 graph_cls_show(fp
, buf
, &children
, level
+ 1);
296 if (!cls
->hlist
.next
) {
297 graph_indent(buf
, cls
, 0, 0);
301 fprintf(fp
, "%s", buf
);
307 int print_class(const struct sockaddr_nl
*who
,
308 struct nlmsghdr
*n
, void *arg
)
310 FILE *fp
= (FILE *)arg
;
311 struct tcmsg
*t
= NLMSG_DATA(n
);
312 int len
= n
->nlmsg_len
;
313 struct rtattr
*tb
[TCA_MAX
+ 1] = {};
314 struct qdisc_util
*q
;
317 if (n
->nlmsg_type
!= RTM_NEWTCLASS
&& n
->nlmsg_type
!= RTM_DELTCLASS
) {
318 fprintf(stderr
, "Not a class\n");
321 len
-= NLMSG_LENGTH(sizeof(*t
));
323 fprintf(stderr
, "Wrong len %d\n", len
);
328 graph_node_add(t
->tcm_parent
, t
->tcm_handle
, TCA_RTA(t
), len
);
332 if (filter_qdisc
&& TC_H_MAJ(t
->tcm_handle
^filter_qdisc
))
335 if (filter_classid
&& t
->tcm_handle
!= filter_classid
)
338 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
340 if (tb
[TCA_KIND
] == NULL
) {
341 fprintf(stderr
, "print_class: NULL kind\n");
345 if (n
->nlmsg_type
== RTM_DELTCLASS
)
346 fprintf(fp
, "deleted ");
351 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_handle
));
353 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_handle
);
355 fprintf(fp
, "class %s %s ", rta_getattr_str(tb
[TCA_KIND
]), abuf
);
357 if (filter_ifindex
== 0)
358 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
360 if (t
->tcm_parent
== TC_H_ROOT
)
361 fprintf(fp
, "root ");
364 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_parent
));
366 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
367 fprintf(fp
, "parent %s ", abuf
);
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
]);
376 fprintf(fp
, "[cannot parse class parameters]");
380 struct rtattr
*xstats
= NULL
;
382 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
]) {
383 print_tcstats_attr(fp
, tb
, " ", &xstats
);
386 if (q
&& (xstats
|| tb
[TCA_XSTATS
]) && q
->print_xstats
) {
387 q
->print_xstats(q
, fp
, xstats
? : tb
[TCA_XSTATS
]);
396 static int tc_class_list(int argc
, char **argv
)
400 char buf
[1024] = {0};
402 memset(&t
, 0, sizeof(t
));
403 t
.tcm_family
= AF_UNSPEC
;
404 memset(d
, 0, sizeof(d
));
410 if (strcmp(*argv
, "dev") == 0) {
413 duparg("dev", *argv
);
414 strncpy(d
, *argv
, sizeof(d
)-1);
415 } else if (strcmp(*argv
, "qdisc") == 0) {
418 duparg("qdisc", *argv
);
419 if (get_qdisc_handle(&filter_qdisc
, *argv
))
420 invarg("invalid qdisc ID", *argv
);
421 } else if (strcmp(*argv
, "classid") == 0) {
424 duparg("classid", *argv
);
425 if (get_tc_classid(&filter_classid
, *argv
))
426 invarg("invalid class ID", *argv
);
427 } else if (strcmp(*argv
, "root") == 0) {
429 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
432 t
.tcm_parent
= TC_H_ROOT
;
433 } else if (strcmp(*argv
, "parent") == 0) {
437 duparg("parent", *argv
);
439 if (get_tc_classid(&handle
, *argv
))
440 invarg("invalid parent ID", *argv
);
441 t
.tcm_parent
= handle
;
442 } else if (matches(*argv
, "help") == 0) {
445 fprintf(stderr
, "What is \"%s\"? Try \"tc class help\".\n", *argv
);
455 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
456 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
459 filter_ifindex
= t
.tcm_ifindex
;
462 if (rtnl_dump_request(&rth
, RTM_GETTCLASS
, &t
, sizeof(t
)) < 0) {
463 perror("Cannot send dump request");
467 if (rtnl_dump_filter(&rth
, print_class
, stdout
) < 0) {
468 fprintf(stderr
, "Dump terminated\n");
473 graph_cls_show(stdout
, &buf
[0], &root_cls_list
, 0);
478 int do_class(int argc
, char **argv
)
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);
491 if (matches(*argv
, "get") == 0)
492 return tc_class_get(RTM_GETTCLASS
, 0, argc
-1, argv
+1);
494 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
495 || matches(*argv
, "lst") == 0)
496 return tc_class_list(argc
-1, argv
+1);
497 if (matches(*argv
, "help") == 0) {
501 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv
);