]>
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 static int usage(void)
33 fprintf(stderr
, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
34 fprintf(stderr
, " [ handle QHANDLE ] [ root | ingress | parent CLASSID ]\n");
35 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
36 fprintf(stderr
, " [ stab [ help | STAB_OPTIONS] ]\n");
37 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
38 fprintf(stderr
, "\n");
39 fprintf(stderr
, " tc qdisc show [ dev STRING ] [ingress]\n");
40 fprintf(stderr
, "Where:\n");
41 fprintf(stderr
, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
42 fprintf(stderr
, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
43 fprintf(stderr
, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
47 static int tc_qdisc_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
49 struct qdisc_util
*q
= NULL
;
50 struct tc_estimator est
;
52 struct tc_sizespec szopts
;
60 char buf
[TCA_BUF_MAX
];
63 memset(&req
, 0, sizeof(req
));
64 memset(&stab
, 0, sizeof(stab
));
65 memset(&est
, 0, sizeof(est
));
66 memset(&d
, 0, sizeof(d
));
67 memset(&k
, 0, sizeof(k
));
69 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
));
70 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|flags
;
71 req
.n
.nlmsg_type
= cmd
;
72 req
.t
.tcm_family
= AF_UNSPEC
;
75 if (strcmp(*argv
, "dev") == 0) {
79 strncpy(d
, *argv
, sizeof(d
)-1);
80 } else if (strcmp(*argv
, "handle") == 0) {
83 duparg("handle", *argv
);
85 if (get_qdisc_handle(&handle
, *argv
))
86 invarg("invalid qdisc ID", *argv
);
87 req
.t
.tcm_handle
= handle
;
88 } else if (strcmp(*argv
, "root") == 0) {
89 if (req
.t
.tcm_parent
) {
90 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
93 req
.t
.tcm_parent
= TC_H_ROOT
;
95 } else if (strcmp(*argv
, "ingress") == 0) {
96 if (req
.t
.tcm_parent
) {
97 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
100 req
.t
.tcm_parent
= TC_H_INGRESS
;
101 strncpy(k
, "ingress", sizeof(k
)-1);
102 q
= get_qdisc_kind(k
);
103 req
.t
.tcm_handle
= 0xffff0000;
108 } else if (strcmp(*argv
, "parent") == 0) {
111 if (req
.t
.tcm_parent
)
112 duparg("parent", *argv
);
113 if (get_tc_classid(&handle
, *argv
))
114 invarg("invalid parent ID", *argv
);
115 req
.t
.tcm_parent
= handle
;
116 } else if (matches(*argv
, "estimator") == 0) {
117 if (parse_estimator(&argc
, &argv
, &est
))
119 } else if (matches(*argv
, "stab") == 0) {
120 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
123 } else if (matches(*argv
, "help") == 0) {
126 strncpy(k
, *argv
, sizeof(k
)-1);
128 q
= get_qdisc_kind(k
);
136 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
138 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
142 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
))
145 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
150 if (matches(*argv
, "help") == 0)
153 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
158 if (check_size_table_opts(&stab
.szopts
)) {
161 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
162 fprintf(stderr
, "failed to calculate size table.\n");
166 tail
= NLMSG_TAIL(&req
.n
);
167 addattr_l(&req
.n
, sizeof(req
), TCA_STAB
, NULL
, 0);
168 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
169 sizeof(stab
.szopts
));
171 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
172 stab
.szopts
.tsize
* sizeof(__u16
));
173 tail
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)tail
;
183 if ((idx
= ll_name_to_index(d
)) == 0) {
184 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
187 req
.t
.tcm_ifindex
= idx
;
190 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
196 static int filter_ifindex
;
198 int print_qdisc(const struct sockaddr_nl
*who
,
202 FILE *fp
= (FILE*)arg
;
203 struct tcmsg
*t
= NLMSG_DATA(n
);
204 int len
= n
->nlmsg_len
;
205 struct rtattr
* tb
[TCA_MAX
+1];
206 struct qdisc_util
*q
;
209 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
210 fprintf(stderr
, "Not a qdisc\n");
213 len
-= NLMSG_LENGTH(sizeof(*t
));
215 fprintf(stderr
, "Wrong len %d\n", len
);
219 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
222 memset(tb
, 0, sizeof(tb
));
223 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
225 if (tb
[TCA_KIND
] == NULL
) {
226 fprintf(stderr
, "print_qdisc: NULL kind\n");
230 if (n
->nlmsg_type
== RTM_DELQDISC
)
231 fprintf(fp
, "deleted ");
233 fprintf(fp
, "qdisc %s %x: ", rta_getattr_str(tb
[TCA_KIND
]), t
->tcm_handle
>>16);
234 if (filter_ifindex
== 0)
235 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
236 if (t
->tcm_parent
== TC_H_ROOT
)
237 fprintf(fp
, "root ");
238 else if (t
->tcm_parent
) {
239 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
240 fprintf(fp
, "parent %s ", abuf
);
242 if (t
->tcm_info
!= 1) {
243 fprintf(fp
, "refcnt %d ", t
->tcm_info
);
245 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
247 if (0 == strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])))
248 q
= get_qdisc_kind("prio");
250 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
252 if (tb
[TCA_OPTIONS
]) {
254 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
256 fprintf(fp
, "[cannot parse qdisc parameters]");
259 if (show_details
&& tb
[TCA_STAB
]) {
260 print_size_table(fp
, " ", tb
[TCA_STAB
]);
264 struct rtattr
*xstats
= NULL
;
266 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
267 print_tcstats_attr(fp
, tb
, " ", &xstats
);
271 if (q
&& xstats
&& q
->print_xstats
) {
272 q
->print_xstats(q
, fp
, xstats
);
281 static int tc_qdisc_list(int argc
, char **argv
)
286 memset(&t
, 0, sizeof(t
));
287 t
.tcm_family
= AF_UNSPEC
;
288 memset(&d
, 0, sizeof(d
));
291 if (strcmp(*argv
, "dev") == 0) {
293 strncpy(d
, *argv
, sizeof(d
)-1);
295 } else if (strcmp(*argv
, "ingress") == 0) {
297 fprintf(stderr
, "Duplicate parent ID\n");
300 t
.tcm_parent
= TC_H_INGRESS
;
302 } else if (matches(*argv
, "help") == 0) {
305 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
315 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
316 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
319 filter_ifindex
= t
.tcm_ifindex
;
322 if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
323 perror("Cannot send dump request");
327 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
328 fprintf(stderr
, "Dump terminated\n");
335 int do_qdisc(int argc
, char **argv
)
338 return tc_qdisc_list(0, NULL
);
339 if (matches(*argv
, "add") == 0)
340 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
341 if (matches(*argv
, "change") == 0)
342 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
343 if (matches(*argv
, "replace") == 0)
344 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
345 if (matches(*argv
, "link") == 0)
346 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
347 if (matches(*argv
, "delete") == 0)
348 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
350 if (matches(*argv
, "get") == 0)
351 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
353 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
354 || matches(*argv
, "lst") == 0)
355 return tc_qdisc_list(argc
-1, argv
+1);
356 if (matches(*argv
, "help") == 0) {
360 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);