]>
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
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
31 #include "tc_common.h"
39 int resolve_hosts
= 0;
42 struct rtnl_handle rth
;
44 static void *BODY
= NULL
; /* cached handle dlopen(NULL) */
45 static struct qdisc_util
* qdisc_list
;
46 static struct filter_util
* filter_list
;
48 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
51 if (opt
&& RTA_PAYLOAD(opt
))
52 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
53 (unsigned) RTA_PAYLOAD(opt
));
57 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
60 fprintf(stderr
, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
66 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
68 if (opt
&& RTA_PAYLOAD(opt
))
69 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
70 fhandle
, (unsigned) RTA_PAYLOAD(opt
));
72 fprintf(f
, "fh %08x ", fhandle
);
76 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
, int argc
, char **argv
, struct nlmsghdr
*n
)
81 fprintf(stderr
, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
85 struct tcmsg
*t
= NLMSG_DATA(n
);
86 if (get_u32(&handle
, fhandle
, 16)) {
87 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
90 t
->tcm_handle
= handle
;
95 struct qdisc_util
*get_qdisc_kind(const char *str
)
101 for (q
= qdisc_list
; q
; q
= q
->next
)
102 if (strcmp(q
->id
, str
) == 0)
105 snprintf(buf
, sizeof(buf
), "%s/q_%s.so", get_tc_lib(), str
);
106 dlh
= dlopen(buf
, RTLD_LAZY
);
108 /* look in current binary, only open once */
111 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
117 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
123 q
->next
= qdisc_list
;
128 q
= malloc(sizeof(*q
));
131 memset(q
, 0, sizeof(*q
));
132 q
->id
= strcpy(malloc(strlen(str
)+1), str
);
133 q
->parse_qopt
= parse_noqopt
;
134 q
->print_qopt
= print_noqopt
;
141 struct filter_util
*get_filter_kind(const char *str
)
145 struct filter_util
*q
;
147 for (q
= filter_list
; q
; q
= q
->next
)
148 if (strcmp(q
->id
, str
) == 0)
151 snprintf(buf
, sizeof(buf
), "%s/f_%s.so", get_tc_lib(), str
);
152 dlh
= dlopen(buf
, RTLD_LAZY
);
156 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
162 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
168 q
->next
= filter_list
;
172 q
= malloc(sizeof(*q
));
174 memset(q
, 0, sizeof(*q
));
175 strncpy(q
->id
, str
, 15);
176 q
->parse_fopt
= parse_nofopt
;
177 q
->print_fopt
= print_nofopt
;
183 static void usage(void)
185 fprintf(stderr
, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
186 " tc [-force] -batch filename\n"
187 "where OBJECT := { qdisc | class | filter | action | monitor }\n"
188 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] }\n");
191 static int do_cmd(int argc
, char **argv
)
193 if (matches(*argv
, "qdisc") == 0)
194 return do_qdisc(argc
-1, argv
+1);
196 if (matches(*argv
, "class") == 0)
197 return do_class(argc
-1, argv
+1);
199 if (matches(*argv
, "filter") == 0)
200 return do_filter(argc
-1, argv
+1);
202 if (matches(*argv
, "actions") == 0)
203 return do_action(argc
-1, argv
+1);
205 if (matches(*argv
, "monitor") == 0)
206 return do_tcmonitor(argc
-1, argv
+1);
208 if (matches(*argv
, "help") == 0) {
213 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
218 static int batch(const char *name
)
225 if (name
&& strcmp(name
, "-") != 0) {
226 if (freopen(name
, "r", stdin
) == NULL
) {
227 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s\n",
228 name
, strerror(errno
));
235 if (rtnl_open(&rth
, 0) < 0) {
236 fprintf(stderr
, "Cannot open rtnetlink\n");
241 while (getcmdline(&line
, &len
, stdin
) != -1) {
245 largc
= makeargs(line
, largv
, 100);
247 continue; /* blank line */
249 if (do_cmd(largc
, largv
)) {
250 fprintf(stderr
, "Command failed %s:%d\n", name
, cmdlineno
);
264 int main(int argc
, char **argv
)
267 char *batch_file
= NULL
;
270 if (argv
[1][0] != '-')
272 if (matches(argv
[1], "-stats") == 0 ||
273 matches(argv
[1], "-statistics") == 0) {
275 } else if (matches(argv
[1], "-details") == 0) {
277 } else if (matches(argv
[1], "-raw") == 0) {
279 } else if (matches(argv
[1], "-pretty") == 0) {
281 } else if (matches(argv
[1], "-Version") == 0) {
282 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
284 } else if (matches(argv
[1], "-iec") == 0) {
286 } else if (matches(argv
[1], "-help") == 0) {
289 } else if (matches(argv
[1], "-force") == 0) {
291 } else if (matches(argv
[1], "-batch") == 0) {
295 batch_file
= argv
[1];
297 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
304 return batch(batch_file
);
312 if (rtnl_open(&rth
, 0) < 0) {
313 fprintf(stderr
, "Cannot open rtnetlink\n");
317 ret
= do_cmd(argc
-1, argv
+1);