]>
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"
28 static void usage(void);
30 static void usage(void)
32 fprintf(stderr
, "Usage: tc class [ add | del | change | get ] dev STRING\n");
33 fprintf(stderr
, " [ classid CLASSID ] [ root | parent CLASSID ]\n");
34 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
35 fprintf(stderr
, "\n");
36 fprintf(stderr
, " tc class show [ dev STRING ] [ root | parent CLASSID ]\n");
37 fprintf(stderr
, "Where:\n");
38 fprintf(stderr
, "QDISC_KIND := { prio | cbq | etc. }\n");
39 fprintf(stderr
, "OPTIONS := ... try tc class add <desired QDISC_KIND> help\n");
43 int tc_class_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
45 struct rtnl_handle rth
;
51 struct qdisc_util
*q
= NULL
;
52 struct tc_estimator est
;
56 memset(&req
, 0, sizeof(req
));
57 memset(&est
, 0, sizeof(est
));
58 memset(d
, 0, sizeof(d
));
59 memset(k
, 0, sizeof(k
));
61 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
));
62 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
63 req
.n
.nlmsg_type
= cmd
;
64 req
.t
.tcm_family
= AF_UNSPEC
;
67 if (strcmp(*argv
, "dev") == 0) {
71 strncpy(d
, *argv
, sizeof(d
)-1);
72 } else if (strcmp(*argv
, "classid") == 0) {
76 duparg("classid", *argv
);
77 if (get_tc_classid(&handle
, *argv
))
78 invarg(*argv
, "invalid class ID");
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");
85 req
.t
.tcm_parent
= TC_H_ROOT
;
86 } else if (strcmp(*argv
, "parent") == 0) {
90 duparg("parent", *argv
);
91 if (get_tc_classid(&handle
, *argv
))
92 invarg(*argv
, "invalid parent ID");
93 req
.t
.tcm_parent
= handle
;
94 } else if (matches(*argv
, "estimator") == 0) {
95 if (parse_estimator(&argc
, &argv
, &est
))
97 } else if (matches(*argv
, "help") == 0) {
100 strncpy(k
, *argv
, sizeof(k
)-1);
102 q
= get_qdisc_kind(k
);
110 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
112 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
115 if (q
->parse_copt
== NULL
) {
116 fprintf(stderr
, "Error: Qdisc \"%s\" is classless.\n", k
);
119 if (q
->parse_copt(q
, argc
, argv
, &req
.n
))
123 if (matches(*argv
, "help") == 0)
125 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv
);
131 if (rtnl_open(&rth
, 0) < 0) {
132 fprintf(stderr
, "Cannot open rtnetlink\n");
137 ll_init_map(&(is_batch_mode
?g_rth
:rth
));
139 if ((req
.t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
140 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
145 if (rtnl_talk(&(is_batch_mode
?g_rth
:rth
), &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
156 static int print_class(const struct sockaddr_nl
*who
,
157 struct nlmsghdr
*n
, void *arg
)
159 FILE *fp
= (FILE*)arg
;
160 struct tcmsg
*t
= NLMSG_DATA(n
);
161 int len
= n
->nlmsg_len
;
162 struct rtattr
* tb
[TCA_MAX
+1];
163 struct qdisc_util
*q
;
166 if (n
->nlmsg_type
!= RTM_NEWTCLASS
&& n
->nlmsg_type
!= RTM_DELTCLASS
) {
167 fprintf(stderr
, "Not a class\n");
170 len
-= NLMSG_LENGTH(sizeof(*t
));
172 fprintf(stderr
, "Wrong len %d\n", len
);
175 if (filter_qdisc
&& TC_H_MAJ(t
->tcm_handle
^filter_qdisc
))
178 memset(tb
, 0, sizeof(tb
));
179 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
181 if (tb
[TCA_KIND
] == NULL
) {
182 fprintf(stderr
, "print_class: NULL kind\n");
186 if (n
->nlmsg_type
== RTM_DELTCLASS
)
187 fprintf(fp
, "deleted ");
192 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_handle
));
194 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_handle
);
196 fprintf(fp
, "class %s %s ", (char*)RTA_DATA(tb
[TCA_KIND
]), abuf
);
198 if (filter_ifindex
== 0)
199 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
201 if (t
->tcm_parent
== TC_H_ROOT
)
202 fprintf(fp
, "root ");
205 print_tc_classid(abuf
, sizeof(abuf
), TC_H_MIN(t
->tcm_parent
));
207 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
208 fprintf(fp
, "parent %s ", abuf
);
211 fprintf(fp
, "leaf %x: ", t
->tcm_info
>>16);
212 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
213 if (tb
[TCA_OPTIONS
]) {
214 if (q
&& q
->print_copt
)
215 q
->print_copt(q
, fp
, tb
[TCA_OPTIONS
]);
217 fprintf(fp
, "[cannot parse class parameters]");
221 struct rtattr
*xstats
= NULL
;
223 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
]) {
224 print_tcstats_attr(fp
, tb
, " ", &xstats
);
227 if (q
&& (xstats
|| tb
[TCA_XSTATS
]) && q
->print_xstats
) {
228 q
->print_xstats(q
, fp
, xstats
? : tb
[TCA_XSTATS
]);
237 int tc_class_list(int argc
, char **argv
)
240 struct rtnl_handle rth
;
243 memset(&t
, 0, sizeof(t
));
244 t
.tcm_family
= AF_UNSPEC
;
245 memset(d
, 0, sizeof(d
));
248 if (strcmp(*argv
, "dev") == 0) {
251 duparg("dev", *argv
);
252 strncpy(d
, *argv
, sizeof(d
)-1);
253 } else if (strcmp(*argv
, "qdisc") == 0) {
256 duparg("qdisc", *argv
);
257 if (get_qdisc_handle(&filter_qdisc
, *argv
))
258 invarg(*argv
, "invalid qdisc ID");
259 } else if (strcmp(*argv
, "root") == 0) {
261 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
264 t
.tcm_parent
= TC_H_ROOT
;
265 } else if (strcmp(*argv
, "parent") == 0) {
268 duparg("parent", *argv
);
270 if (get_tc_classid(&handle
, *argv
))
271 invarg(*argv
, "invalid parent ID");
272 t
.tcm_parent
= handle
;
273 } else if (matches(*argv
, "help") == 0) {
276 fprintf(stderr
, "What is \"%s\"? Try \"tc class help\".\n", *argv
);
284 if (rtnl_open(&rth
, 0) < 0) {
285 fprintf(stderr
, "Cannot open rtnetlink\n");
289 ll_init_map(&(is_batch_mode
?g_rth
:rth
));
292 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
293 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
296 filter_ifindex
= t
.tcm_ifindex
;
299 if (rtnl_dump_request(&(is_batch_mode
?g_rth
:rth
), RTM_GETTCLASS
, &t
, sizeof(t
)) < 0) {
300 perror("Cannot send dump request");
305 if (rtnl_dump_filter(&(is_batch_mode
?g_rth
:rth
), print_class
, stdout
, NULL
, NULL
) < 0) {
306 fprintf(stderr
, "Dump terminated\n");
316 int do_class(int argc
, char **argv
)
319 return tc_class_list(0, NULL
);
320 if (matches(*argv
, "add") == 0)
321 return tc_class_modify(RTM_NEWTCLASS
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
322 if (matches(*argv
, "change") == 0)
323 return tc_class_modify(RTM_NEWTCLASS
, 0, argc
-1, argv
+1);
324 if (matches(*argv
, "replace") == 0)
325 return tc_class_modify(RTM_NEWTCLASS
, NLM_F_CREATE
, argc
-1, argv
+1);
326 if (matches(*argv
, "delete") == 0)
327 return tc_class_modify(RTM_DELTCLASS
, 0, argc
-1, argv
+1);
329 if (matches(*argv
, "get") == 0)
330 return tc_class_get(RTM_GETTCLASS
, 0, argc
-1, argv
+1);
332 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
333 || matches(*argv
, "lst") == 0)
334 return tc_class_list(argc
-1, argv
+1);
335 if (matches(*argv
, "help") == 0)
337 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv
);