]>
git.proxmox.com Git - mirror_iproute2.git/blob - tc/tc.c
b72657ec2e60c8430d850c187b0ef6dcc6d7a268
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"
48 static char *conf_file
;
50 struct rtnl_handle rth
;
52 static void *BODY
; /* cached handle dlopen(NULL) */
53 static struct qdisc_util
*qdisc_list
;
54 static struct filter_util
*filter_list
;
56 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
59 if (opt
&& RTA_PAYLOAD(opt
))
60 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
61 (unsigned int) RTA_PAYLOAD(opt
));
65 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
,
66 struct nlmsghdr
*n
, const char *dev
)
70 "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n",
77 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
79 if (opt
&& RTA_PAYLOAD(opt
))
80 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
81 fhandle
, (unsigned int) RTA_PAYLOAD(opt
));
83 fprintf(f
, "fh %08x ", fhandle
);
87 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
,
88 int argc
, char **argv
, struct nlmsghdr
*n
)
94 "Unknown filter \"%s\", hence option \"%s\" is unparsable\n",
99 struct tcmsg
*t
= NLMSG_DATA(n
);
101 if (get_u32(&handle
, fhandle
, 16)) {
102 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
105 t
->tcm_handle
= handle
;
110 struct qdisc_util
*get_qdisc_kind(const char *str
)
114 struct qdisc_util
*q
;
116 for (q
= qdisc_list
; q
; q
= q
->next
)
117 if (strcmp(q
->id
, str
) == 0)
120 snprintf(buf
, sizeof(buf
), "%s/q_%s.so", get_tc_lib(), str
);
121 dlh
= dlopen(buf
, RTLD_LAZY
);
123 /* look in current binary, only open once */
126 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
132 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
138 q
->next
= qdisc_list
;
143 q
= calloc(1, sizeof(*q
));
146 q
->parse_qopt
= parse_noqopt
;
147 q
->print_qopt
= print_noqopt
;
154 struct filter_util
*get_filter_kind(const char *str
)
158 struct filter_util
*q
;
160 for (q
= filter_list
; q
; q
= q
->next
)
161 if (strcmp(q
->id
, str
) == 0)
164 snprintf(buf
, sizeof(buf
), "%s/f_%s.so", get_tc_lib(), str
);
165 dlh
= dlopen(buf
, RTLD_LAZY
);
169 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
175 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
181 q
->next
= filter_list
;
185 q
= calloc(1, sizeof(*q
));
187 strncpy(q
->id
, str
, 15);
188 q
->parse_fopt
= parse_nofopt
;
189 q
->print_fopt
= print_nofopt
;
195 static void usage(void)
198 "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
199 " tc [-force] -batch filename\n"
200 "where OBJECT := { qdisc | class | filter | chain |\n"
201 " action | monitor | exec }\n"
202 " OPTIONS := { -V[ersion] | -s[tatistics] | -d[etails] | -r[aw] |\n"
203 " -o[neline] | -j[son] | -p[retty] | -c[olor]\n"
204 " -b[atch] [filename] | -n[etns] name | -N[umeric] |\n"
205 " -nm | -nam[es] | { -cf | -conf } path }\n");
208 static int do_cmd(int argc
, char **argv
)
210 if (matches(*argv
, "qdisc") == 0)
211 return do_qdisc(argc
-1, argv
+1);
212 if (matches(*argv
, "class") == 0)
213 return do_class(argc
-1, argv
+1);
214 if (matches(*argv
, "filter") == 0)
215 return do_filter(argc
-1, argv
+1);
216 if (matches(*argv
, "chain") == 0)
217 return do_chain(argc
-1, argv
+1);
218 if (matches(*argv
, "actions") == 0)
219 return do_action(argc
-1, argv
+1);
220 if (matches(*argv
, "monitor") == 0)
221 return do_tcmonitor(argc
-1, argv
+1);
222 if (matches(*argv
, "exec") == 0)
223 return do_exec(argc
-1, argv
+1);
224 if (matches(*argv
, "help") == 0) {
229 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
234 static int batch(const char *name
)
241 if (name
&& strcmp(name
, "-") != 0) {
242 if (freopen(name
, "r", stdin
) == NULL
) {
244 "Cannot open file \"%s\" for reading: %s\n",
245 name
, strerror(errno
));
252 if (rtnl_open(&rth
, 0) < 0) {
253 fprintf(stderr
, "Cannot open rtnetlink\n");
258 while (getcmdline(&line
, &len
, stdin
) != -1) {
262 largc
= makeargs(line
, largv
, 100);
264 continue; /* blank line */
266 if (do_cmd(largc
, largv
)) {
267 fprintf(stderr
, "Command failed %s:%d\n",
282 int main(int argc
, char **argv
)
285 char *batch_file
= NULL
;
288 if (argv
[1][0] != '-')
290 if (matches(argv
[1], "-stats") == 0 ||
291 matches(argv
[1], "-statistics") == 0) {
293 } else if (matches(argv
[1], "-details") == 0) {
295 } else if (matches(argv
[1], "-raw") == 0) {
297 } else if (matches(argv
[1], "-pretty") == 0) {
299 } else if (matches(argv
[1], "-graph") == 0) {
301 } else if (matches(argv
[1], "-Version") == 0) {
302 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
304 } else if (matches(argv
[1], "-iec") == 0) {
306 } else if (matches(argv
[1], "-help") == 0) {
309 } else if (matches(argv
[1], "-force") == 0) {
311 } else if (matches(argv
[1], "-batch") == 0) {
315 batch_file
= argv
[1];
316 } else if (matches(argv
[1], "-netns") == 0) {
318 if (netns_switch(argv
[1]))
320 } else if (matches(argv
[1], "-Numeric") == 0) {
322 } else if (matches(argv
[1], "-names") == 0 ||
323 matches(argv
[1], "-nm") == 0) {
325 } else if (matches(argv
[1], "-cf") == 0 ||
326 matches(argv
[1], "-conf") == 0) {
329 } else if (matches_color(argv
[1], &color
)) {
330 } else if (matches(argv
[1], "-timestamp") == 0) {
332 } else if (matches(argv
[1], "-tshort") == 0) {
335 } else if (matches(argv
[1], "-json") == 0) {
337 } else if (matches(argv
[1], "-oneline") == 0) {
341 "Option \"%s\" is unknown, try \"tc -help\".\n",
348 _SL_
= oneline
? "\\" : "\n";
350 check_enable_color(color
, json
);
353 return batch(batch_file
);
361 if (rtnl_open(&rth
, 0) < 0) {
362 fprintf(stderr
, "Cannot open rtnetlink\n");
366 if (use_names
&& cls_names_init(conf_file
)) {
371 ret
= do_cmd(argc
-1, argv
+1);