]>
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 batch(const char *name
)
243 if (name
&& strcmp(name
, "-") != 0) {
244 if (freopen(name
, "r", stdin
) == NULL
) {
246 "Cannot open file \"%s\" for reading: %s\n",
247 name
, strerror(errno
));
254 if (rtnl_open(&rth
, 0) < 0) {
255 fprintf(stderr
, "Cannot open rtnetlink\n");
260 while (getcmdline(&line
, &len
, stdin
) != -1) {
264 largc
= makeargs(line
, largv
, 100);
266 continue; /* blank line */
268 if (do_cmd(largc
, largv
)) {
269 fprintf(stderr
, "Command failed %s:%d\n",
284 int main(int argc
, char **argv
)
287 char *batch_file
= NULL
;
290 if (argv
[1][0] != '-')
292 if (matches(argv
[1], "-stats") == 0 ||
293 matches(argv
[1], "-statistics") == 0) {
295 } else if (matches(argv
[1], "-details") == 0) {
297 } else if (matches(argv
[1], "-raw") == 0) {
299 } else if (matches(argv
[1], "-pretty") == 0) {
301 } else if (matches(argv
[1], "-graph") == 0) {
303 } else if (matches(argv
[1], "-Version") == 0) {
304 printf("tc utility, iproute2-%s\n", version
);
306 } else if (matches(argv
[1], "-iec") == 0) {
308 } else if (matches(argv
[1], "-help") == 0) {
311 } else if (matches(argv
[1], "-force") == 0) {
313 } else if (matches(argv
[1], "-batch") == 0) {
317 batch_file
= argv
[1];
318 } else if (matches(argv
[1], "-netns") == 0) {
320 if (netns_switch(argv
[1]))
322 } else if (matches(argv
[1], "-Numeric") == 0) {
324 } else if (matches(argv
[1], "-names") == 0 ||
325 matches(argv
[1], "-nm") == 0) {
327 } else if (matches(argv
[1], "-cf") == 0 ||
328 matches(argv
[1], "-conf") == 0) {
331 } else if (matches_color(argv
[1], &color
)) {
332 } else if (matches(argv
[1], "-timestamp") == 0) {
334 } else if (matches(argv
[1], "-tshort") == 0) {
337 } else if (matches(argv
[1], "-json") == 0) {
339 } else if (matches(argv
[1], "-oneline") == 0) {
341 }else if (matches(argv
[1], "-brief") == 0) {
345 "Option \"%s\" is unknown, try \"tc -help\".\n",
352 _SL_
= oneline
? "\\" : "\n";
354 check_enable_color(color
, json
);
357 return batch(batch_file
);
365 if (rtnl_open(&rth
, 0) < 0) {
366 fprintf(stderr
, "Cannot open rtnetlink\n");
370 if (use_names
&& cls_names_init(conf_file
)) {
375 ret
= do_cmd(argc
-1, argv
+1);