]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
2 * tc.c "tc" utility frontend.
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>
13 * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
30 #include "tc_common.h"
31 #include "namespace.h"
49 static char *conf_file
;
51 struct rtnl_handle rth
;
53 static void *BODY
; /* cached handle dlopen(NULL) */
54 static struct qdisc_util
*qdisc_list
;
55 static struct filter_util
*filter_list
;
57 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
60 if (opt
&& RTA_PAYLOAD(opt
))
61 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
62 (unsigned int) RTA_PAYLOAD(opt
));
66 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
,
67 struct nlmsghdr
*n
, const char *dev
)
71 "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n",
78 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
80 if (opt
&& RTA_PAYLOAD(opt
))
81 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
82 fhandle
, (unsigned int) RTA_PAYLOAD(opt
));
84 fprintf(f
, "fh %08x ", fhandle
);
88 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
,
89 int argc
, char **argv
, struct nlmsghdr
*n
)
95 "Unknown filter \"%s\", hence option \"%s\" is unparsable\n",
100 struct tcmsg
*t
= NLMSG_DATA(n
);
102 if (get_u32(&handle
, fhandle
, 16)) {
103 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
106 t
->tcm_handle
= handle
;
111 struct qdisc_util
*get_qdisc_kind(const char *str
)
115 struct qdisc_util
*q
;
117 for (q
= qdisc_list
; q
; q
= q
->next
)
118 if (strcmp(q
->id
, str
) == 0)
121 snprintf(buf
, sizeof(buf
), "%s/q_%s.so", get_tc_lib(), str
);
122 dlh
= dlopen(buf
, RTLD_LAZY
);
124 /* look in current binary, only open once */
127 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
133 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
139 q
->next
= qdisc_list
;
144 q
= calloc(1, sizeof(*q
));
147 q
->parse_qopt
= parse_noqopt
;
148 q
->print_qopt
= print_noqopt
;
155 struct filter_util
*get_filter_kind(const char *str
)
159 struct filter_util
*q
;
161 for (q
= filter_list
; q
; q
= q
->next
)
162 if (strcmp(q
->id
, str
) == 0)
165 snprintf(buf
, sizeof(buf
), "%s/f_%s.so", get_tc_lib(), str
);
166 dlh
= dlopen(buf
, RTLD_LAZY
);
170 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
176 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
182 q
->next
= filter_list
;
186 q
= calloc(1, sizeof(*q
));
188 strncpy(q
->id
, str
, 15);
189 q
->parse_fopt
= parse_nofopt
;
190 q
->print_fopt
= print_nofopt
;
196 static void usage(void)
199 "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
200 " tc [-force] -batch filename\n"
201 "where OBJECT := { qdisc | class | filter | chain |\n"
202 " action | monitor | exec }\n"
203 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
204 " -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
205 " -b[atch] [filename] | -n[etns] name | -N[umeric] |\n"
206 " -nm | -nam[es] | { -cf | -conf } path\n"
210 static int do_cmd(int argc
, char **argv
)
212 if (matches(*argv
, "qdisc") == 0)
213 return do_qdisc(argc
-1, argv
+1);
214 if (matches(*argv
, "class") == 0)
215 return do_class(argc
-1, argv
+1);
216 if (matches(*argv
, "filter") == 0)
217 return do_filter(argc
-1, argv
+1);
218 if (matches(*argv
, "chain") == 0)
219 return do_chain(argc
-1, argv
+1);
220 if (matches(*argv
, "actions") == 0)
221 return do_action(argc
-1, argv
+1);
222 if (matches(*argv
, "monitor") == 0)
223 return do_tcmonitor(argc
-1, argv
+1);
224 if (matches(*argv
, "exec") == 0)
225 return do_exec(argc
-1, argv
+1);
226 if (matches(*argv
, "help") == 0) {
231 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
236 static int tc_batch_cmd(int argc
, char *argv
[], void *data
)
238 return do_cmd(argc
, argv
);
241 static int batch(const char *name
)
248 if (rtnl_open(&rth
, 0) < 0) {
249 fprintf(stderr
, "Cannot open rtnetlink\n");
253 ret
= do_batch(name
, force
, tc_batch_cmd
, NULL
);
260 int main(int argc
, char **argv
)
263 char *batch_file
= NULL
;
266 if (argv
[1][0] != '-')
268 if (matches(argv
[1], "-stats") == 0 ||
269 matches(argv
[1], "-statistics") == 0) {
271 } else if (matches(argv
[1], "-details") == 0) {
273 } else if (matches(argv
[1], "-raw") == 0) {
275 } else if (matches(argv
[1], "-pretty") == 0) {
277 } else if (matches(argv
[1], "-graph") == 0) {
279 } else if (matches(argv
[1], "-Version") == 0) {
280 printf("tc utility, iproute2-%s\n", version
);
282 } else if (matches(argv
[1], "-iec") == 0) {
284 } else if (matches(argv
[1], "-help") == 0) {
287 } else if (matches(argv
[1], "-force") == 0) {
289 } else if (matches(argv
[1], "-batch") == 0) {
293 batch_file
= argv
[1];
294 } else if (matches(argv
[1], "-netns") == 0) {
296 if (netns_switch(argv
[1]))
298 } else if (matches(argv
[1], "-Numeric") == 0) {
300 } else if (matches(argv
[1], "-names") == 0 ||
301 matches(argv
[1], "-nm") == 0) {
303 } else if (matches(argv
[1], "-cf") == 0 ||
304 matches(argv
[1], "-conf") == 0) {
307 } else if (matches_color(argv
[1], &color
)) {
308 } else if (matches(argv
[1], "-timestamp") == 0) {
310 } else if (matches(argv
[1], "-tshort") == 0) {
313 } else if (matches(argv
[1], "-json") == 0) {
315 } else if (matches(argv
[1], "-oneline") == 0) {
317 }else if (matches(argv
[1], "-brief") == 0) {
321 "Option \"%s\" is unknown, try \"tc -help\".\n",
328 _SL_
= oneline
? "\\" : "\n";
330 check_enable_color(color
, json
);
333 return batch(batch_file
);
341 if (rtnl_open(&rth
, 0) < 0) {
342 fprintf(stderr
, "Cannot open rtnetlink\n");
346 if (use_names
&& cls_names_init(conf_file
)) {
351 ret
= do_cmd(argc
-1, argv
+1);