]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc_qdisc.c
2 * tc_qdisc.c "tc qdisc".
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>
10 * J Hadi Salim: Extension to ingress
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
27 #include "tc_common.h"
29 static int usage(void)
31 fprintf(stderr
, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
32 fprintf(stderr
, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
33 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
34 fprintf(stderr
, " [ stab [ help | STAB_OPTIONS] ]\n");
35 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr
, "\n");
37 fprintf(stderr
, " tc qdisc show [ dev STRING ] [ ingress | clsact ]\n");
38 fprintf(stderr
, "Where:\n");
39 fprintf(stderr
, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
40 fprintf(stderr
, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
41 fprintf(stderr
, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
45 static int tc_qdisc_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
47 struct qdisc_util
*q
= NULL
;
48 struct tc_estimator est
;
50 struct tc_sizespec szopts
;
58 char buf
[TCA_BUF_MAX
];
61 memset(&req
, 0, sizeof(req
));
62 memset(&stab
, 0, sizeof(stab
));
63 memset(&est
, 0, sizeof(est
));
64 memset(&d
, 0, sizeof(d
));
65 memset(&k
, 0, sizeof(k
));
67 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
));
68 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
69 req
.n
.nlmsg_type
= cmd
;
70 req
.t
.tcm_family
= AF_UNSPEC
;
73 if (strcmp(*argv
, "dev") == 0) {
77 strncpy(d
, *argv
, sizeof(d
)-1);
78 } else if (strcmp(*argv
, "handle") == 0) {
81 duparg("handle", *argv
);
83 if (get_qdisc_handle(&handle
, *argv
))
84 invarg("invalid qdisc ID", *argv
);
85 req
.t
.tcm_handle
= handle
;
86 } else if (strcmp(*argv
, "root") == 0) {
87 if (req
.t
.tcm_parent
) {
88 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
91 req
.t
.tcm_parent
= TC_H_ROOT
;
92 } else if (strcmp(*argv
, "clsact") == 0) {
93 if (req
.t
.tcm_parent
) {
94 fprintf(stderr
, "Error: \"clsact\" is a duplicate parent ID\n");
97 req
.t
.tcm_parent
= TC_H_CLSACT
;
98 strncpy(k
, "clsact", sizeof(k
) - 1);
99 q
= get_qdisc_kind(k
);
100 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_CLSACT
, 0);
103 } else if (strcmp(*argv
, "ingress") == 0) {
104 if (req
.t
.tcm_parent
) {
105 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
108 req
.t
.tcm_parent
= TC_H_INGRESS
;
109 strncpy(k
, "ingress", sizeof(k
) - 1);
110 q
= get_qdisc_kind(k
);
111 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
114 } else if (strcmp(*argv
, "parent") == 0) {
117 if (req
.t
.tcm_parent
)
118 duparg("parent", *argv
);
119 if (get_tc_classid(&handle
, *argv
))
120 invarg("invalid parent ID", *argv
);
121 req
.t
.tcm_parent
= handle
;
122 } else if (matches(*argv
, "estimator") == 0) {
123 if (parse_estimator(&argc
, &argv
, &est
))
125 } else if (matches(*argv
, "stab") == 0) {
126 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
129 } else if (matches(*argv
, "help") == 0) {
132 strncpy(k
, *argv
, sizeof(k
)-1);
134 q
= get_qdisc_kind(k
);
142 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
144 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
148 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
))
151 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
156 if (matches(*argv
, "help") == 0)
159 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
164 if (check_size_table_opts(&stab
.szopts
)) {
167 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
168 fprintf(stderr
, "failed to calculate size table.\n");
172 tail
= NLMSG_TAIL(&req
.n
);
173 addattr_l(&req
.n
, sizeof(req
), TCA_STAB
, NULL
, 0);
174 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
175 sizeof(stab
.szopts
));
177 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
178 stab
.szopts
.tsize
* sizeof(__u16
));
179 tail
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)tail
;
189 if ((idx
= ll_name_to_index(d
)) == 0) {
190 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
193 req
.t
.tcm_ifindex
= idx
;
196 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
202 static int filter_ifindex
;
204 int print_qdisc(const struct sockaddr_nl
*who
,
208 FILE *fp
= (FILE*)arg
;
209 struct tcmsg
*t
= NLMSG_DATA(n
);
210 int len
= n
->nlmsg_len
;
211 struct rtattr
* tb
[TCA_MAX
+1];
212 struct qdisc_util
*q
;
215 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
216 fprintf(stderr
, "Not a qdisc\n");
219 len
-= NLMSG_LENGTH(sizeof(*t
));
221 fprintf(stderr
, "Wrong len %d\n", len
);
225 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
228 memset(tb
, 0, sizeof(tb
));
229 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
231 if (tb
[TCA_KIND
] == NULL
) {
232 fprintf(stderr
, "print_qdisc: NULL kind\n");
236 if (n
->nlmsg_type
== RTM_DELQDISC
)
237 fprintf(fp
, "deleted ");
239 fprintf(fp
, "qdisc %s %x: ", rta_getattr_str(tb
[TCA_KIND
]), t
->tcm_handle
>>16);
240 if (filter_ifindex
== 0)
241 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
242 if (t
->tcm_parent
== TC_H_ROOT
)
243 fprintf(fp
, "root ");
244 else if (t
->tcm_parent
) {
245 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
246 fprintf(fp
, "parent %s ", abuf
);
248 if (t
->tcm_info
!= 1) {
249 fprintf(fp
, "refcnt %d ", t
->tcm_info
);
251 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
253 if (0 == strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])))
254 q
= get_qdisc_kind("prio");
256 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
258 if (tb
[TCA_OPTIONS
]) {
260 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
262 fprintf(fp
, "[cannot parse qdisc parameters]");
265 if (show_details
&& tb
[TCA_STAB
]) {
266 print_size_table(fp
, " ", tb
[TCA_STAB
]);
270 struct rtattr
*xstats
= NULL
;
272 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
273 print_tcstats_attr(fp
, tb
, " ", &xstats
);
277 if (q
&& xstats
&& q
->print_xstats
) {
278 q
->print_xstats(q
, fp
, xstats
);
286 static int tc_qdisc_list(int argc
, char **argv
)
291 memset(&t
, 0, sizeof(t
));
292 t
.tcm_family
= AF_UNSPEC
;
293 memset(&d
, 0, sizeof(d
));
296 if (strcmp(*argv
, "dev") == 0) {
298 strncpy(d
, *argv
, sizeof(d
)-1);
299 } else if (strcmp(*argv
, "ingress") == 0 ||
300 strcmp(*argv
, "clsact") == 0) {
302 fprintf(stderr
, "Duplicate parent ID\n");
305 t
.tcm_parent
= TC_H_INGRESS
;
306 } else if (matches(*argv
, "help") == 0) {
309 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
319 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
320 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
323 filter_ifindex
= t
.tcm_ifindex
;
326 if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
327 perror("Cannot send dump request");
331 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
332 fprintf(stderr
, "Dump terminated\n");
339 int do_qdisc(int argc
, char **argv
)
342 return tc_qdisc_list(0, NULL
);
343 if (matches(*argv
, "add") == 0)
344 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
345 if (matches(*argv
, "change") == 0)
346 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
347 if (matches(*argv
, "replace") == 0)
348 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
349 if (matches(*argv
, "link") == 0)
350 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
351 if (matches(*argv
, "delete") == 0)
352 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
354 if (matches(*argv
, "get") == 0)
355 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
357 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
358 || matches(*argv
, "lst") == 0)
359 return tc_qdisc_list(argc
-1, argv
+1);
360 if (matches(*argv
, "help") == 0) {
364 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);