]>
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 ] [ invisible ]\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 int 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
];
60 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
61 .n
.nlmsg_flags
= NLM_F_REQUEST
| flags
,
63 .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) {
76 duparg("handle", *argv
);
78 if (get_qdisc_handle(&handle
, *argv
))
79 invarg("invalid qdisc ID", *argv
);
80 req
.t
.tcm_handle
= handle
;
81 } else if (strcmp(*argv
, "root") == 0) {
82 if (req
.t
.tcm_parent
) {
83 fprintf(stderr
, "Error: \"root\" is duplicate parent ID\n");
86 req
.t
.tcm_parent
= TC_H_ROOT
;
87 } else if (strcmp(*argv
, "clsact") == 0) {
88 if (req
.t
.tcm_parent
) {
89 fprintf(stderr
, "Error: \"clsact\" is a duplicate parent ID\n");
92 req
.t
.tcm_parent
= TC_H_CLSACT
;
93 strncpy(k
, "clsact", sizeof(k
) - 1);
94 q
= get_qdisc_kind(k
);
95 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_CLSACT
, 0);
98 } else if (strcmp(*argv
, "ingress") == 0) {
99 if (req
.t
.tcm_parent
) {
100 fprintf(stderr
, "Error: \"ingress\" is a duplicate parent ID\n");
103 req
.t
.tcm_parent
= TC_H_INGRESS
;
104 strncpy(k
, "ingress", sizeof(k
) - 1);
105 q
= get_qdisc_kind(k
);
106 req
.t
.tcm_handle
= TC_H_MAKE(TC_H_INGRESS
, 0);
109 } else if (strcmp(*argv
, "parent") == 0) {
113 if (req
.t
.tcm_parent
)
114 duparg("parent", *argv
);
115 if (get_tc_classid(&handle
, *argv
))
116 invarg("invalid parent ID", *argv
);
117 req
.t
.tcm_parent
= handle
;
118 } else if (matches(*argv
, "estimator") == 0) {
119 if (parse_estimator(&argc
, &argv
, &est
))
121 } else if (matches(*argv
, "stab") == 0) {
122 if (parse_size_table(&argc
, &argv
, &stab
.szopts
) < 0)
125 } else if (matches(*argv
, "help") == 0) {
128 strncpy(k
, *argv
, sizeof(k
)-1);
130 q
= get_qdisc_kind(k
);
138 addattr_l(&req
.n
, sizeof(req
), TCA_KIND
, k
, strlen(k
)+1);
140 addattr_l(&req
.n
, sizeof(req
), TCA_RATE
, &est
, sizeof(est
));
144 if (q
->parse_qopt(q
, argc
, argv
, &req
.n
))
147 fprintf(stderr
, "qdisc '%s' does not support option parsing\n", k
);
152 if (matches(*argv
, "help") == 0)
155 fprintf(stderr
, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv
);
160 if (check_size_table_opts(&stab
.szopts
)) {
163 if (tc_calc_size_table(&stab
.szopts
, &stab
.data
) < 0) {
164 fprintf(stderr
, "failed to calculate size table.\n");
168 tail
= NLMSG_TAIL(&req
.n
);
169 addattr_l(&req
.n
, sizeof(req
), TCA_STAB
, NULL
, 0);
170 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_BASE
, &stab
.szopts
,
171 sizeof(stab
.szopts
));
173 addattr_l(&req
.n
, sizeof(req
), TCA_STAB_DATA
, stab
.data
,
174 stab
.szopts
.tsize
* sizeof(__u16
));
175 tail
->rta_len
= (void *)NLMSG_TAIL(&req
.n
) - (void *)tail
;
185 idx
= ll_name_to_index(d
);
187 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
190 req
.t
.tcm_ifindex
= idx
;
193 if (rtnl_talk(&rth
, &req
.n
, NULL
, 0) < 0)
199 static int filter_ifindex
;
201 int print_qdisc(const struct sockaddr_nl
*who
,
202 struct nlmsghdr
*n
, void *arg
)
204 FILE *fp
= (FILE *)arg
;
205 struct tcmsg
*t
= NLMSG_DATA(n
);
206 int len
= n
->nlmsg_len
;
207 struct rtattr
*tb
[TCA_MAX
+1];
208 struct qdisc_util
*q
;
211 if (n
->nlmsg_type
!= RTM_NEWQDISC
&& n
->nlmsg_type
!= RTM_DELQDISC
) {
212 fprintf(stderr
, "Not a qdisc\n");
215 len
-= NLMSG_LENGTH(sizeof(*t
));
217 fprintf(stderr
, "Wrong len %d\n", len
);
221 if (filter_ifindex
&& filter_ifindex
!= t
->tcm_ifindex
)
224 parse_rtattr(tb
, TCA_MAX
, TCA_RTA(t
), len
);
226 if (tb
[TCA_KIND
] == NULL
) {
227 fprintf(stderr
, "print_qdisc: NULL kind\n");
231 if (n
->nlmsg_type
== RTM_DELQDISC
)
232 fprintf(fp
, "deleted ");
235 fprintf(fp
, "qdisc %s %x:[%08x] ",
236 rta_getattr_str(tb
[TCA_KIND
]),
237 t
->tcm_handle
>> 16, t
->tcm_handle
);
239 fprintf(fp
, "qdisc %s %x: ", rta_getattr_str(tb
[TCA_KIND
]),
240 t
->tcm_handle
>> 16);
242 if (filter_ifindex
== 0)
243 fprintf(fp
, "dev %s ", ll_index_to_name(t
->tcm_ifindex
));
245 if (t
->tcm_parent
== TC_H_ROOT
)
246 fprintf(fp
, "root ");
247 else if (t
->tcm_parent
) {
248 print_tc_classid(abuf
, sizeof(abuf
), t
->tcm_parent
);
249 fprintf(fp
, "parent %s ", abuf
);
252 if (t
->tcm_info
!= 1)
253 fprintf(fp
, "refcnt %d ", t
->tcm_info
);
255 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */
256 if (strcmp("pfifo_fast", RTA_DATA(tb
[TCA_KIND
])) == 0)
257 q
= get_qdisc_kind("prio");
259 q
= get_qdisc_kind(RTA_DATA(tb
[TCA_KIND
]));
261 if (tb
[TCA_OPTIONS
]) {
263 q
->print_qopt(q
, fp
, tb
[TCA_OPTIONS
]);
265 fprintf(fp
, "[cannot parse qdisc parameters]");
269 if (show_details
&& tb
[TCA_STAB
]) {
270 print_size_table(fp
, " ", tb
[TCA_STAB
]);
275 struct rtattr
*xstats
= NULL
;
277 if (tb
[TCA_STATS
] || tb
[TCA_STATS2
] || tb
[TCA_XSTATS
]) {
278 print_tcstats_attr(fp
, tb
, " ", &xstats
);
282 if (q
&& xstats
&& q
->print_xstats
) {
283 q
->print_xstats(q
, fp
, xstats
);
291 static int tc_qdisc_list(int argc
, char **argv
)
293 struct tcmsg t
= { .tcm_family
= AF_UNSPEC
};
295 bool dump_invisible
= false;
298 if (strcmp(*argv
, "dev") == 0) {
300 strncpy(d
, *argv
, sizeof(d
)-1);
301 } else if (strcmp(*argv
, "ingress") == 0 ||
302 strcmp(*argv
, "clsact") == 0) {
304 fprintf(stderr
, "Duplicate parent ID\n");
307 t
.tcm_parent
= TC_H_INGRESS
;
308 } else if (matches(*argv
, "help") == 0) {
310 } else if (strcmp(*argv
, "invisible") == 0) {
311 dump_invisible
= true;
313 fprintf(stderr
, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv
);
323 t
.tcm_ifindex
= ll_name_to_index(d
);
324 if (t
.tcm_ifindex
== 0) {
325 fprintf(stderr
, "Cannot find device \"%s\"\n", d
);
328 filter_ifindex
= t
.tcm_ifindex
;
331 if (dump_invisible
) {
337 .n
.nlmsg_type
= RTM_GETQDISC
,
338 .n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct tcmsg
)),
341 req
.t
.tcm_family
= AF_UNSPEC
;
343 addattr(&req
.n
, 256, TCA_DUMP_INVISIBLE
);
344 if (rtnl_dump_request_n(&rth
, &req
.n
) < 0) {
345 perror("Cannot send dump request");
349 } else if (rtnl_dump_request(&rth
, RTM_GETQDISC
, &t
, sizeof(t
)) < 0) {
350 perror("Cannot send dump request");
354 if (rtnl_dump_filter(&rth
, print_qdisc
, stdout
) < 0) {
355 fprintf(stderr
, "Dump terminated\n");
362 int do_qdisc(int argc
, char **argv
)
365 return tc_qdisc_list(0, NULL
);
366 if (matches(*argv
, "add") == 0)
367 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_EXCL
|NLM_F_CREATE
, argc
-1, argv
+1);
368 if (matches(*argv
, "change") == 0)
369 return tc_qdisc_modify(RTM_NEWQDISC
, 0, argc
-1, argv
+1);
370 if (matches(*argv
, "replace") == 0)
371 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_CREATE
|NLM_F_REPLACE
, argc
-1, argv
+1);
372 if (matches(*argv
, "link") == 0)
373 return tc_qdisc_modify(RTM_NEWQDISC
, NLM_F_REPLACE
, argc
-1, argv
+1);
374 if (matches(*argv
, "delete") == 0)
375 return tc_qdisc_modify(RTM_DELQDISC
, 0, argc
-1, argv
+1);
377 if (matches(*argv
, "get") == 0)
378 return tc_qdisc_get(RTM_GETQDISC
, 0, argc
-1, argv
+1);
380 if (matches(*argv
, "list") == 0 || matches(*argv
, "show") == 0
381 || matches(*argv
, "lst") == 0)
382 return tc_qdisc_list(argc
-1, argv
+1);
383 if (matches(*argv
, "help") == 0) {
387 fprintf(stderr
, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv
);