]>
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"
38 int resolve_hosts
= 0;
41 struct rtnl_handle rth
;
43 static void *BODY
= NULL
; /* cached handle dlopen(NULL) */
44 static struct qdisc_util
* qdisc_list
;
45 static struct filter_util
* filter_list
;
47 static int print_noqopt(struct qdisc_util
*qu
, FILE *f
,
50 if (opt
&& RTA_PAYLOAD(opt
))
51 fprintf(f
, "[Unknown qdisc, optlen=%u] ",
52 (unsigned) RTA_PAYLOAD(opt
));
56 static int parse_noqopt(struct qdisc_util
*qu
, int argc
, char **argv
, struct nlmsghdr
*n
)
59 fprintf(stderr
, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
65 static int print_nofopt(struct filter_util
*qu
, FILE *f
, struct rtattr
*opt
, __u32 fhandle
)
67 if (opt
&& RTA_PAYLOAD(opt
))
68 fprintf(f
, "fh %08x [Unknown filter, optlen=%u] ",
69 fhandle
, (unsigned) RTA_PAYLOAD(opt
));
71 fprintf(f
, "fh %08x ", fhandle
);
75 static int parse_nofopt(struct filter_util
*qu
, char *fhandle
, int argc
, char **argv
, struct nlmsghdr
*n
)
80 fprintf(stderr
, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu
->id
, *argv
);
84 struct tcmsg
*t
= NLMSG_DATA(n
);
85 if (get_u32(&handle
, fhandle
, 16)) {
86 fprintf(stderr
, "Unparsable filter ID \"%s\"\n", fhandle
);
89 t
->tcm_handle
= handle
;
94 struct qdisc_util
*get_qdisc_kind(const char *str
)
100 for (q
= qdisc_list
; q
; q
= q
->next
)
101 if (strcmp(q
->id
, str
) == 0)
104 snprintf(buf
, sizeof(buf
), "%s/q_%s.so", get_tc_lib(), str
);
105 dlh
= dlopen(buf
, RTLD_LAZY
);
107 /* look in current binary, only open once */
110 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
116 snprintf(buf
, sizeof(buf
), "%s_qdisc_util", str
);
122 q
->next
= qdisc_list
;
127 q
= malloc(sizeof(*q
));
130 memset(q
, 0, sizeof(*q
));
131 q
->id
= strcpy(malloc(strlen(str
)+1), str
);
132 q
->parse_qopt
= parse_noqopt
;
133 q
->print_qopt
= print_noqopt
;
140 struct filter_util
*get_filter_kind(const char *str
)
144 struct filter_util
*q
;
146 for (q
= filter_list
; q
; q
= q
->next
)
147 if (strcmp(q
->id
, str
) == 0)
150 snprintf(buf
, sizeof(buf
), "%s/f_%s.so", get_tc_lib(), str
);
151 dlh
= dlopen(buf
, RTLD_LAZY
);
155 dlh
= BODY
= dlopen(NULL
, RTLD_LAZY
);
161 snprintf(buf
, sizeof(buf
), "%s_filter_util", str
);
167 q
->next
= filter_list
;
171 q
= malloc(sizeof(*q
));
173 memset(q
, 0, sizeof(*q
));
174 strncpy(q
->id
, str
, 15);
175 q
->parse_fopt
= parse_nofopt
;
176 q
->print_fopt
= print_nofopt
;
182 static void usage(void)
184 fprintf(stderr
, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
185 " tc [-force] -batch file\n"
186 "where OBJECT := { qdisc | class | filter | action | monitor }\n"
187 " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [file] }\n");
190 static int do_cmd(int argc
, char **argv
)
192 if (matches(*argv
, "qdisc") == 0)
193 return do_qdisc(argc
-1, argv
+1);
195 if (matches(*argv
, "class") == 0)
196 return do_class(argc
-1, argv
+1);
198 if (matches(*argv
, "filter") == 0)
199 return do_filter(argc
-1, argv
+1);
201 if (matches(*argv
, "actions") == 0)
202 return do_action(argc
-1, argv
+1);
204 if (matches(*argv
, "monitor") == 0)
205 return do_tcmonitor(argc
-1, argv
+1);
207 if (matches(*argv
, "help") == 0) {
212 fprintf(stderr
, "Object \"%s\" is unknown, try \"tc help\".\n",
217 static int batch(const char *name
)
223 if (name
&& strcmp(name
, "-") != 0) {
224 if (freopen(name
, "r", stdin
) == NULL
) {
225 fprintf(stderr
, "Cannot open file \"%s\" for reading: %s\n",
226 name
, strerror(errno
));
233 if (rtnl_open(&rth
, 0) < 0) {
234 fprintf(stderr
, "Cannot open rtnetlink\n");
239 while (getcmdline(&line
, &len
, stdin
) != -1) {
243 largc
= makeargs(line
, largv
, 100);
245 continue; /* blank line */
247 if (do_cmd(largc
, largv
)) {
248 fprintf(stderr
, "Command failed %s:%d\n", name
, cmdlineno
);
262 int main(int argc
, char **argv
)
266 char *batchfile
= NULL
;
269 if (argv
[1][0] != '-')
271 if (matches(argv
[1], "-stats") == 0 ||
272 matches(argv
[1], "-statistics") == 0) {
274 } else if (matches(argv
[1], "-details") == 0) {
276 } else if (matches(argv
[1], "-raw") == 0) {
278 } else if (matches(argv
[1], "-pretty") == 0) {
280 } else if (matches(argv
[1], "-Version") == 0) {
281 printf("tc utility, iproute2-ss%s\n", SNAPSHOT
);
283 } else if (matches(argv
[1], "-iec") == 0) {
285 } else if (matches(argv
[1], "-help") == 0) {
288 } else if (matches(argv
[1], "-force") == 0) {
290 } else if (matches(argv
[1], "-batch") == 0) {
296 fprintf(stderr
, "Option \"%s\" is unknown, try \"tc -help\".\n", argv
[1]);
303 return batch(batchfile
);
311 if (rtnl_open(&rth
, 0) < 0) {
312 fprintf(stderr
, "Cannot open rtnetlink\n");
316 ret
= do_cmd(argc
-1, argv
+1);