]>
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>
26 #include "tc_common.h"
28 static int usage(void);
30 static int usage(void)
32 fprintf(stderr
, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
33 fprintf(stderr
, " [ handle QHANDLE ] [ root | ingress | parent CLASSID ]\n");
34 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
35 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
36 fprintf(stderr
, "\n");
37 fprintf(stderr
, " tc qdisc show [ dev STRING ] [ingress]\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");
44 int tc_qdisc_modify(int cmd
, unsigned flags
, int argc
, char **argv
)
46 struct qdisc_util
*q
= NULL
;
47 struct tc_estimator est
;
53 char buf
[TCA_BUF_MAX
];
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
, "handle") == 0) {
75 duparg("handle", *argv
);
77 if (get_qdisc_handle(&handle
, *argv
))
78 invarg(*argv
, "invalid qdisc 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
;
87 } else if (strcmp(*argv
, "ingress") == 0) {
88 if (req
.t
.tcm_parent
) {
89 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
92 req
.t
.tcm_parent
= TC_H_INGRESS
;
93 strncpy(k
, "ingress", sizeof(k
)-1);
94 q
= get_qdisc_kind(k
);
95 req
.t
.tcm_handle
= 0xffff0000;
100 } else if (strcmp(*argv
, "parent") == 0) {
103 if (req
.t
.tcm_parent
)
104 duparg("parent", *argv
);
105 if (get_tc_classid(&handle
, *argv
))
106 invarg(*argv
, "invalid parent ID");
107 req
.t
.tcm_parent
= handle
;
108 } else if (matches(*argv
, "estimator") == 0) {
109 if (parse_estimator(&argc
, &argv
, &est
))
111 } else if (matches(*argv
, "help") == 0) {
114 strncpy(k
, *argv
, sizeof(k
)-1);
116 q
= get_qdisc_kind(k
);
124 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
126 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
129 if (!q
->parse_qopt
) {
130 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
133 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
))
137 if (matches(*argv
, "help") == 0)
140 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
150 if ((idx
= ll_name_to_index(d
)) == 0) {
151 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
154 req
.t
.tcm_ifindex
= idx
;
157 if (rtnl_talk(&rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
) < 0)
163 static int filter_ifindex
;
165 int print_qdisc(const struct sockaddr_nl
*who
,
169 FILE *fp
= (FILE*)arg
;
170 struct tcmsg
*t
= NLMSG_DATA(n
);
171 int len
= n
->nlmsg_len
;
172 struct rtattr
* tb
[TCA_MAX
+1];
173 struct qdisc_util
*q
;
176 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
177 fprintf(stderr
, "Not a qdisc\n");
180 len
-= NLMSG_LENGTH(sizeof(*t
));
182 fprintf(stderr
, "Wrong len %d\n", len
);
186 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
189 memset(tb
, 0, sizeof(tb
));
190 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
192 if (tb
[TCA_KIND
] == NULL
) {
193 fprintf(stderr
, "print_qdisc: NULL kind\n");
197 if (n
->nlmsg_type
== RTM_DELQDISC
)
198 fprintf(fp
, "deleted ");
200 fprintf(fp
, "qdisc %s %x: ", (char*)RTA_DATA(tb
[TCA_KIND
]), t
->tcm_handle
>>16);
201 if (filter_ifindex
== 0)
202 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
203 if (t
->tcm_parent
== TC_H_ROOT
)
204 fprintf(fp
, "root ");
205 else if (t
->tcm_parent
) {
206 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
207 fprintf(fp
, "parent %s ", abuf
);
209 if (t
->tcm_info
!= 1) {
210 fprintf(fp
, "refcnt %d ", t
->tcm_info
);
212 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
214 if (0 == strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])))
215 q
= get_qdisc_kind("prio");
217 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
219 if (tb
[TCA_OPTIONS
]) {
221 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
223 fprintf(fp
, "[cannot parse qdisc parameters]");
227 struct rtattr
*xstats
= NULL
;
229 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
230 print_tcstats_attr(fp
, tb
, " ", &xstats
);
234 if (q
&& xstats
&& q
->print_xstats
) {
235 q
->print_xstats(q
, fp
, xstats
);
244 int tc_qdisc_list(int argc
, char **argv
)
249 memset(&t
, 0, sizeof(t
));
250 t
.tcm_family
= AF_UNSPEC
;
251 memset(&d
, 0, sizeof(d
));
254 if (strcmp(*argv
, "dev") == 0) {
256 strncpy(d
, *argv
, sizeof(d
)-1);
258 } else if (strcmp(*argv
, "ingress") == 0) {
260 fprintf(stderr
, "Duplicate parent ID\n");
263 t
.tcm_parent
= TC_H_INGRESS
;
265 } else if (matches(*argv
, "help") == 0) {
268 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
278 if ((t
.tcm_ifindex
= ll_name_to_index(d
)) == 0) {
279 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
282 filter_ifindex
= t
.tcm_ifindex
;
285 if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
286 perror("Cannot send dump request");
290 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
, NULL
, NULL
) < 0) {
291 fprintf(stderr
, "Dump terminated\n");
298 int do_qdisc(int argc
, char **argv
)
301 return tc_qdisc_list(0, NULL
);
302 if (matches(*argv
, "add") == 0)
303 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
304 if (matches(*argv
, "change") == 0)
305 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
306 if (matches(*argv
, "replace") == 0)
307 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
308 if (matches(*argv
, "link") == 0)
309 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
310 if (matches(*argv
, "delete") == 0)
311 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
313 if (matches(*argv
, "get") == 0)
314 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
316 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
317 || matches(*argv
, "lst") == 0)
318 return tc_qdisc_list(argc
-1, argv
+1);
319 if (matches(*argv
, "help") == 0) {
323 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);