]>
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"
40 int resolve_hosts
= 0;
43 struct rtnl_handle rth
;
45 static void *BODY
= NULL
; /* cached handle dlopen(NULL) */
46 static struct qdisc_util
* qdisc_list
;
47 static struct filter_util
* filter_list
;
49 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
52 if (opt
&& RTA_PAYLOAD(opt
))
53 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
54 (unsigned) RTA_PAYLOAD(opt
));
58 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
61 fprintf(stderr
, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
67 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
69 if (opt
&& RTA_PAYLOAD(opt
))
70 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
71 fhandle
, (unsigned) RTA_PAYLOAD(opt
));
73 fprintf(f
, "fh %08x ", fhandle
);
77 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
, int argc
, char **argv
, struct nlmsghdr
*n
)
82 fprintf(stderr
, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
86 struct tcmsg
*t
= NLMSG_DATA(n
);
87 if (get_u32(&handle
, fhandle
, 16)) {
88 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
91 t
->tcm_handle
= handle
;
96 struct qdisc_util
*get_qdisc_kind(const char *str
)
100 struct qdisc_util
*q
;
102 for (q
= qdisc_list
; q
; q
= q
->next
)
103 if (strcmp(q
->id
, str
) == 0)
106 snprintf(buf
, sizeof(buf
), "%s/q_%s.so", get_tc_lib(), str
);
107 dlh
= dlopen(buf
, RTLD_LAZY
);
109 /* look in current binary, only open once */
112 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
118 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
124 q
->next
= qdisc_list
;
129 q
= malloc(sizeof(*q
));
132 memset(q
, 0, sizeof(*q
));
133 q
->id
= strcpy(malloc(strlen(str
)+1), str
);
134 q
->parse_qopt
= parse_noqopt
;
135 q
->print_qopt
= print_noqopt
;
142 struct filter_util
*get_filter_kind(const char *str
)
146 struct filter_util
*q
;
148 for (q
= filter_list
; q
; q
= q
->next
)
149 if (strcmp(q
->id
, str
) == 0)
152 snprintf(buf
, sizeof(buf
), "%s/f_%s.so", get_tc_lib(), str
);
153 dlh
= dlopen(buf
, RTLD_LAZY
);
157 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
163 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
169 q
->next
= filter_list
;
173 q
= malloc(sizeof(*q
));
175 memset(q
, 0, sizeof(*q
));
176 strncpy(q
->id
, str
, 15);
177 q
->parse_fopt
= parse_nofopt
;
178 q
->print_fopt
= print_nofopt
;
184 static void usage(void)
186 fprintf(stderr
, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
187 " tc [-force] -batch filename\n"
188 "where OBJECT := { qdisc | class | filter | action | monitor }\n"
189 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] }\n");
192 static int do_cmd(int argc
, char **argv
)
194 if (matches(*argv
, "qdisc") == 0)
195 return do_qdisc(argc
-1, argv
+1);
197 if (matches(*argv
, "class") == 0)
198 return do_class(argc
-1, argv
+1);
200 if (matches(*argv
, "filter") == 0)
201 return do_filter(argc
-1, argv
+1);
203 if (matches(*argv
, "actions") == 0)
204 return do_action(argc
-1, argv
+1);
206 if (matches(*argv
, "monitor") == 0)
207 return do_tcmonitor(argc
-1, argv
+1);
209 if (matches(*argv
, "help") == 0) {
214 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
219 static int batch(const char *name
)
226 if (name
&& strcmp(name
, "-") != 0) {
227 if (freopen(name
, "r", stdin
) == NULL
) {
228 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s\n",
229 name
, strerror(errno
));
236 if (rtnl_open(&rth
, 0) < 0) {
237 fprintf(stderr
, "Cannot open rtnetlink\n");
242 while (getcmdline(&line
, &len
, stdin
) != -1) {
246 largc
= makeargs(line
, largv
, 100);
248 continue; /* blank line */
250 if (do_cmd(largc
, largv
)) {
251 fprintf(stderr
, "Command failed %s:%d\n", name
, cmdlineno
);
265 int main(int argc
, char **argv
)
268 char *batch_file
= NULL
;
271 if (argv
[1][0] != '-')
273 if (matches(argv
[1], "-stats") == 0 ||
274 matches(argv
[1], "-statistics") == 0) {
276 } else if (matches(argv
[1], "-details") == 0) {
278 } else if (matches(argv
[1], "-raw") == 0) {
280 } else if (matches(argv
[1], "-pretty") == 0) {
282 } else if (matches(argv
[1], "-graph") == 0) {
284 } else if (matches(argv
[1], "-Version") == 0) {
285 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
287 } else if (matches(argv
[1], "-iec") == 0) {
289 } else if (matches(argv
[1], "-help") == 0) {
292 } else if (matches(argv
[1], "-force") == 0) {
294 } else if (matches(argv
[1], "-batch") == 0) {
298 batch_file
= argv
[1];
300 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
307 return batch(batch_file
);
315 if (rtnl_open(&rth
, 0) < 0) {
316 fprintf(stderr
, "Cannot open rtnetlink\n");
320 ret
= do_cmd(argc
-1, argv
+1);