]>
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
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
26 #include "tc_common.h"
28 static int usage(void)
30 fprintf(stderr
, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n");
31 fprintf(stderr
, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n");
32 fprintf(stderr
, " [ estimator INTERVAL TIME_CONSTANT ]\n");
33 fprintf(stderr
, " [ stab [ help | STAB_OPTIONS] ]\n");
34 fprintf(stderr
, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
35 fprintf(stderr
, "\n");
36 fprintf(stderr
, " tc qdisc show [ dev STRING ] [ ingress | clsact ] [ invisible ]\n");
37 fprintf(stderr
, "Where:\n");
38 fprintf(stderr
, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n");
39 fprintf(stderr
, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n");
40 fprintf(stderr
, "STAB_OPTIONS := ... try tc qdisc add stab help\n");
44 static int tc_qdisc_modify(int cmd
, unsigned int flags
, int argc
, char **argv
)
46 struct qdisc_util
*q
= NULL
;
47 struct tc_estimator est
= {};
49 struct tc_sizespec szopts
;
52 char d
[IFNAMSIZ
] = {};
53 char k
[FILTER_NAMESZ
] = {};
57 char buf
[TCA_BUF_MAX
];
59 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
60 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
62 .t
.tcm_family
= AF_UNSPEC
,
66 if (strcmp(*argv
, "dev") == 0) {
70 strncpy(d
, *argv
, sizeof(d
)-1);
71 } else if (strcmp(*argv
, "handle") == 0) {
75 duparg("handle", *argv
);
77 if (get_qdisc_handle(&handle
, *argv
))
78 invarg("invalid qdisc ID", *argv
);
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
, "clsact") == 0) {
87 if (req
.t
.tcm_parent
) {
88 fprintf(stderr
, "Error: \"clsact\" is a duplicate parent ID\n");
91 req
.t
.tcm_parent
= TC_H_CLSACT
;
92 strncpy(k
, "clsact", sizeof(k
) - 1);
93 q
= get_qdisc_kind(k
);
94 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_CLSACT
, 0);
97 } else if (strcmp(*argv
, "ingress") == 0) {
98 if (req
.t
.tcm_parent
) {
99 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
102 req
.t
.tcm_parent
= TC_H_INGRESS
;
103 strncpy(k
, "ingress", sizeof(k
) - 1);
104 q
= get_qdisc_kind(k
);
105 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
108 } else if (strcmp(*argv
, "parent") == 0) {
112 if (req
.t
.tcm_parent
)
113 duparg("parent", *argv
);
114 if (get_tc_classid(&handle
, *argv
))
115 invarg("invalid parent ID", *argv
);
116 req
.t
.tcm_parent
= handle
;
117 } else if (matches(*argv
, "estimator") == 0) {
118 if (parse_estimator(&argc
, &argv
, &est
))
120 } else if (matches(*argv
, "stab") == 0) {
121 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
124 } else if (matches(*argv
, "help") == 0) {
127 strncpy(k
, *argv
, sizeof(k
)-1);
129 q
= get_qdisc_kind(k
);
137 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
139 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
143 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
))
146 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
151 if (matches(*argv
, "help") == 0)
154 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
159 if (check_size_table_opts(&stab
.szopts
)) {
162 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
163 fprintf(stderr
, "failed to calculate size table.\n");
167 tail
= NLMSG_TAIL(&req
.n
);
168 addattr_l(&req
.n
, sizeof(req
), TCA_STAB
, NULL
, 0);
169 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
170 sizeof(stab
.szopts
));
172 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
173 stab
.szopts
.tsize
* sizeof(__u16
));
174 tail
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)tail
;
184 idx
= ll_name_to_index(d
);
186 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
189 req
.t
.tcm_ifindex
= idx
;
192 if (rtnl_talk(&rth
, &req
.n
, NULL
) < 0)
198 static int filter_ifindex
;
200 int print_qdisc(const struct sockaddr_nl
*who
,
201 struct nlmsghdr
*n
, void *arg
)
203 FILE *fp
= (FILE *)arg
;
204 struct tcmsg
*t
= NLMSG_DATA(n
);
205 int len
= n
->nlmsg_len
;
206 struct rtattr
*tb
[TCA_MAX
+1];
207 struct qdisc_util
*q
;
210 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
211 fprintf(stderr
, "Not a qdisc\n");
214 len
-= NLMSG_LENGTH(sizeof(*t
));
216 fprintf(stderr
, "Wrong len %d\n", len
);
220 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
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 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
234 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
235 (n
->nlmsg_flags
& NLM_F_REPLACE
))
236 fprintf(fp
, "replaced ");
238 if (n
->nlmsg_type
== RTM_NEWQDISC
&&
239 (n
->nlmsg_flags
& NLM_F_CREATE
) &&
240 (n
->nlmsg_flags
& NLM_F_EXCL
))
241 fprintf(fp
, "added ");
244 fprintf(fp
, "qdisc %s %x:[%08x] ",
245 rta_getattr_str(tb
[TCA_KIND
]),
246 t
->tcm_handle
>> 16, t
->tcm_handle
);
248 fprintf(fp
, "qdisc %s %x: ", rta_getattr_str(tb
[TCA_KIND
]),
249 t
->tcm_handle
>> 16);
251 if (filter_ifindex
== 0)
252 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
254 if (t
->tcm_parent
== TC_H_ROOT
)
255 fprintf(fp
, "root ");
256 else if (t
->tcm_parent
) {
257 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
258 fprintf(fp
, "parent %s ", abuf
);
261 if (t
->tcm_info
!= 1)
262 fprintf(fp
, "refcnt %d ", t
->tcm_info
);
264 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
265 if (strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])) == 0)
266 q
= get_qdisc_kind("prio");
268 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
270 if (tb
[TCA_OPTIONS
]) {
272 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
274 fprintf(fp
, "[cannot parse qdisc parameters]");
278 if (show_details
&& tb
[TCA_STAB
]) {
279 print_size_table(fp
, " ", tb
[TCA_STAB
]);
284 struct rtattr
*xstats
= NULL
;
286 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
287 print_tcstats_attr(fp
, tb
, " ", &xstats
);
291 if (q
&& xstats
&& q
->print_xstats
) {
292 q
->print_xstats(q
, fp
, xstats
);
300 static int tc_qdisc_list(int argc
, char **argv
)
302 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
303 char d
[IFNAMSIZ
] = {};
304 bool dump_invisible
= false;
307 if (strcmp(*argv
, "dev") == 0) {
309 strncpy(d
, *argv
, sizeof(d
)-1);
310 } else if (strcmp(*argv
, "ingress") == 0 ||
311 strcmp(*argv
, "clsact") == 0) {
313 fprintf(stderr
, "Duplicate parent ID\n");
316 t
.tcm_parent
= TC_H_INGRESS
;
317 } else if (matches(*argv
, "help") == 0) {
319 } else if (strcmp(*argv
, "invisible") == 0) {
320 dump_invisible
= true;
322 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
332 t
.tcm_ifindex
= ll_name_to_index(d
);
333 if (t
.tcm_ifindex
== 0) {
334 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
337 filter_ifindex
= t
.tcm_ifindex
;
340 if (dump_invisible
) {
346 .n
.nlmsg_type
= RTM_GETQDISC
,
347 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
350 req
.t
.tcm_family
= AF_UNSPEC
;
352 addattr(&req
.n
, 256, TCA_DUMP_INVISIBLE
);
353 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
354 perror("Cannot send dump request");
358 } else if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
359 perror("Cannot send dump request");
363 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
364 fprintf(stderr
, "Dump terminated\n");
371 int do_qdisc(int argc
, char **argv
)
374 return tc_qdisc_list(0, NULL
);
375 if (matches(*argv
, "add") == 0)
376 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
377 if (matches(*argv
, "change") == 0)
378 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
379 if (matches(*argv
, "replace") == 0)
380 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
381 if (matches(*argv
, "link") == 0)
382 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
383 if (matches(*argv
, "delete") == 0)
384 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
386 if (matches(*argv
, "get") == 0)
387 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
389 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
390 || matches(*argv
, "lst") == 0)
391 return tc_qdisc_list(argc
-1, argv
+1);
392 if (matches(*argv
, "help") == 0) {
396 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);