osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tc.c "tc" utility frontend. |
| 3 | * |
| 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. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | * |
| 11 | * Fixes: |
| 12 | * |
| 13 | * Petri Mattila <petri@prihateam.fi> 990308: wrong memset's resulted in faults |
| 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <syslog.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <dlfcn.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <string.h> |
| 26 | #include <errno.h> |
| 27 | |
| 28 | #include "SNAPSHOT.h" |
| 29 | #include "utils.h" |
| 30 | #include "tc_util.h" |
| 31 | #include "tc_common.h" |
| 32 | |
| 33 | int show_stats = 0; |
| 34 | int show_details = 0; |
| 35 | int show_raw = 0; |
Stephen Hemminger | 44dcfe8 | 2008-05-09 15:42:34 -0700 | [diff] [blame] | 36 | int show_pretty = 0; |
Stephen Hemminger | a3aa47a | 2013-07-16 10:04:05 -0700 | [diff] [blame] | 37 | int batch_mode = 0; |
Stephen Hemminger | 44dcfe8 | 2008-05-09 15:42:34 -0700 | [diff] [blame] | 38 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 39 | int resolve_hosts = 0; |
osdl.net!shemminger | 3ea2bf4 | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 40 | int use_iec = 0; |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 41 | int force = 0; |
osdl.net!shemminger | 7901660 | 2005-03-14 19:34:12 +0000 | [diff] [blame] | 42 | struct rtnl_handle rth; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 43 | |
shemminger | f453a0d | 2005-10-07 16:33:21 +0000 | [diff] [blame] | 44 | static void *BODY = NULL; /* cached handle dlopen(NULL) */ |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | static struct qdisc_util * qdisc_list; |
| 46 | static struct filter_util * filter_list; |
| 47 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 48 | static int print_noqopt(struct qdisc_util *qu, FILE *f, |
osdl.net!shemminger | 1798c9d | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 49 | struct rtattr *opt) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 50 | { |
| 51 | if (opt && RTA_PAYLOAD(opt)) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 52 | fprintf(f, "[Unknown qdisc, optlen=%u] ", |
osdl.net!shemminger | bb6a21a | 2004-10-06 23:17:10 +0000 | [diff] [blame] | 53 | (unsigned) RTA_PAYLOAD(opt)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) |
| 58 | { |
| 59 | if (argc) { |
| 60 | fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); |
| 61 | return -1; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle) |
| 67 | { |
| 68 | if (opt && RTA_PAYLOAD(opt)) |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 69 | fprintf(f, "fh %08x [Unknown filter, optlen=%u] ", |
osdl.net!shemminger | bb6a21a | 2004-10-06 23:17:10 +0000 | [diff] [blame] | 70 | fhandle, (unsigned) RTA_PAYLOAD(opt)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 71 | else if (fhandle) |
| 72 | fprintf(f, "fh %08x ", fhandle); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n) |
| 77 | { |
| 78 | __u32 handle; |
| 79 | |
| 80 | if (argc) { |
| 81 | fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv); |
| 82 | return -1; |
| 83 | } |
| 84 | if (fhandle) { |
| 85 | struct tcmsg *t = NLMSG_DATA(n); |
| 86 | if (get_u32(&handle, fhandle, 16)) { |
| 87 | fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle); |
| 88 | return -1; |
| 89 | } |
| 90 | t->tcm_handle = handle; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
osdl.org!shemminger | 4094db7 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 95 | struct qdisc_util *get_qdisc_kind(const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 96 | { |
| 97 | void *dlh; |
| 98 | char buf[256]; |
| 99 | struct qdisc_util *q; |
| 100 | |
| 101 | for (q = qdisc_list; q; q = q->next) |
| 102 | if (strcmp(q->id, str) == 0) |
| 103 | return q; |
| 104 | |
Stephen Hemminger | aa27f88 | 2007-06-20 15:27:22 -0700 | [diff] [blame] | 105 | snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 106 | dlh = dlopen(buf, RTLD_LAZY); |
osdl.net!shemminger | b7a4515 | 2004-07-02 17:47:53 +0000 | [diff] [blame] | 107 | if (!dlh) { |
| 108 | /* look in current binary, only open once */ |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 109 | dlh = BODY; |
| 110 | if (dlh == NULL) { |
| 111 | dlh = BODY = dlopen(NULL, RTLD_LAZY); |
| 112 | if (dlh == NULL) |
| 113 | goto noexist; |
| 114 | } |
| 115 | } |
| 116 | |
net[shemminger]!kaber | 95812b5 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 117 | snprintf(buf, sizeof(buf), "%s_qdisc_util", str); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 118 | q = dlsym(dlh, buf); |
| 119 | if (q == NULL) |
| 120 | goto noexist; |
| 121 | |
| 122 | reg: |
| 123 | q->next = qdisc_list; |
| 124 | qdisc_list = q; |
| 125 | return q; |
| 126 | |
| 127 | noexist: |
| 128 | q = malloc(sizeof(*q)); |
| 129 | if (q) { |
osdl.net!shemminger | 1798c9d | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 130 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 131 | memset(q, 0, sizeof(*q)); |
osdl.net!shemminger | 1798c9d | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 132 | q->id = strcpy(malloc(strlen(str)+1), str); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 133 | q->parse_qopt = parse_noqopt; |
| 134 | q->print_qopt = print_noqopt; |
| 135 | goto reg; |
| 136 | } |
| 137 | return q; |
| 138 | } |
| 139 | |
| 140 | |
osdl.org!shemminger | 4094db7 | 2004-06-02 20:22:08 +0000 | [diff] [blame] | 141 | struct filter_util *get_filter_kind(const char *str) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 142 | { |
| 143 | void *dlh; |
| 144 | char buf[256]; |
| 145 | struct filter_util *q; |
| 146 | |
| 147 | for (q = filter_list; q; q = q->next) |
| 148 | if (strcmp(q->id, str) == 0) |
| 149 | return q; |
| 150 | |
Stephen Hemminger | aa27f88 | 2007-06-20 15:27:22 -0700 | [diff] [blame] | 151 | snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 152 | dlh = dlopen(buf, RTLD_LAZY); |
| 153 | if (dlh == NULL) { |
| 154 | dlh = BODY; |
| 155 | if (dlh == NULL) { |
| 156 | dlh = BODY = dlopen(NULL, RTLD_LAZY); |
| 157 | if (dlh == NULL) |
| 158 | goto noexist; |
| 159 | } |
| 160 | } |
| 161 | |
net[shemminger]!kaber | 95812b5 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 162 | snprintf(buf, sizeof(buf), "%s_filter_util", str); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 163 | q = dlsym(dlh, buf); |
| 164 | if (q == NULL) |
| 165 | goto noexist; |
| 166 | |
| 167 | reg: |
| 168 | q->next = filter_list; |
| 169 | filter_list = q; |
| 170 | return q; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 171 | noexist: |
| 172 | q = malloc(sizeof(*q)); |
| 173 | if (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; |
| 178 | goto reg; |
| 179 | } |
| 180 | return q; |
| 181 | } |
| 182 | |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 183 | static void usage(void) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 184 | { |
| 185 | fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n" |
Petr Jediný | 10494d2 | 2008-08-07 16:45:33 +0200 | [diff] [blame] | 186 | " tc [-force] -batch filename\n" |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 187 | "where OBJECT := { qdisc | class | filter | action | monitor }\n" |
Petr Jediný | 10494d2 | 2008-08-07 16:45:33 +0200 | [diff] [blame] | 188 | " OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] }\n"); |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int do_cmd(int argc, char **argv) |
| 192 | { |
| 193 | if (matches(*argv, "qdisc") == 0) |
| 194 | return do_qdisc(argc-1, argv+1); |
| 195 | |
| 196 | if (matches(*argv, "class") == 0) |
| 197 | return do_class(argc-1, argv+1); |
| 198 | |
| 199 | if (matches(*argv, "filter") == 0) |
| 200 | return do_filter(argc-1, argv+1); |
| 201 | |
| 202 | if (matches(*argv, "actions") == 0) |
| 203 | return do_action(argc-1, argv+1); |
| 204 | |
Jamal Hadi Salim | 5bec348 | 2006-08-08 11:55:15 -0700 | [diff] [blame] | 205 | if (matches(*argv, "monitor") == 0) |
| 206 | return do_tcmonitor(argc-1, argv+1); |
| 207 | |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 208 | if (matches(*argv, "help") == 0) { |
| 209 | usage(); |
| 210 | return 0; |
| 211 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 212 | |
| 213 | fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n", |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 214 | *argv); |
osdl.net!shemminger | 044ebf3 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 215 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 216 | } |
| 217 | |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 218 | static int batch(const char *name) |
| 219 | { |
| 220 | char *line = NULL; |
| 221 | size_t len = 0; |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 222 | int ret = 0; |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 223 | |
Stephen Hemminger | a3aa47a | 2013-07-16 10:04:05 -0700 | [diff] [blame] | 224 | batch_mode = 1; |
shemminger | dd3e908 | 2005-06-23 17:32:22 +0000 | [diff] [blame] | 225 | if (name && strcmp(name, "-") != 0) { |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 226 | if (freopen(name, "r", stdin) == NULL) { |
Stephen Hemminger | 84d6688 | 2008-02-07 22:10:14 -0800 | [diff] [blame] | 227 | fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n", |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 228 | name, strerror(errno)); |
| 229 | return -1; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | tc_core_init(); |
| 234 | |
| 235 | if (rtnl_open(&rth, 0) < 0) { |
| 236 | fprintf(stderr, "Cannot open rtnetlink\n"); |
| 237 | return -1; |
| 238 | } |
| 239 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 240 | cmdlineno = 0; |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 241 | while (getcmdline(&line, &len, stdin) != -1) { |
| 242 | char *largv[100]; |
| 243 | int largc; |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 244 | |
| 245 | largc = makeargs(line, largv, 100); |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 246 | if (largc == 0) |
| 247 | continue; /* blank line */ |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 248 | |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 249 | if (do_cmd(largc, largv)) { |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 250 | fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno); |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 251 | ret = 1; |
| 252 | if (!force) |
| 253 | break; |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
shemminger | 8ed63ab | 2005-09-21 19:33:17 +0000 | [diff] [blame] | 256 | if (line) |
| 257 | free(line); |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 258 | |
| 259 | rtnl_close(&rth); |
| 260 | return ret; |
| 261 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 262 | |
| 263 | |
| 264 | int main(int argc, char **argv) |
| 265 | { |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 266 | int ret; |
Stephen Hemminger | a3aa47a | 2013-07-16 10:04:05 -0700 | [diff] [blame] | 267 | char *batch_file = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 268 | |
| 269 | while (argc > 1) { |
| 270 | if (argv[1][0] != '-') |
| 271 | break; |
| 272 | if (matches(argv[1], "-stats") == 0 || |
osdl.net!shemminger | 3ea2bf4 | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 273 | matches(argv[1], "-statistics") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 274 | ++show_stats; |
| 275 | } else if (matches(argv[1], "-details") == 0) { |
| 276 | ++show_details; |
| 277 | } else if (matches(argv[1], "-raw") == 0) { |
| 278 | ++show_raw; |
Stephen Hemminger | 44dcfe8 | 2008-05-09 15:42:34 -0700 | [diff] [blame] | 279 | } else if (matches(argv[1], "-pretty") == 0) { |
| 280 | ++show_pretty; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 281 | } else if (matches(argv[1], "-Version") == 0) { |
| 282 | printf("tc utility, iproute2-ss%s\n", SNAPSHOT); |
osdl.net!shemminger | 044ebf3 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 283 | return 0; |
osdl.net!shemminger | 3ea2bf4 | 2004-06-28 20:42:59 +0000 | [diff] [blame] | 284 | } else if (matches(argv[1], "-iec") == 0) { |
| 285 | ++use_iec; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 286 | } else if (matches(argv[1], "-help") == 0) { |
| 287 | usage(); |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 288 | return 0; |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 289 | } else if (matches(argv[1], "-force") == 0) { |
| 290 | ++force; |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 291 | } else if (matches(argv[1], "-batch") == 0) { |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 292 | argc--; argv++; |
Stephen Hemminger | a3aa47a | 2013-07-16 10:04:05 -0700 | [diff] [blame] | 293 | if (argc <= 1) |
| 294 | usage(); |
| 295 | batch_file = argv[1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 296 | } else { |
| 297 | fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]); |
osdl.net!shemminger | 044ebf3 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 298 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 299 | } |
| 300 | argc--; argv++; |
| 301 | } |
| 302 | |
Stephen Hemminger | a3aa47a | 2013-07-16 10:04:05 -0700 | [diff] [blame] | 303 | if (batch_file) |
| 304 | return batch(batch_file); |
osdl.net!shemminger | 08856f0 | 2005-03-18 19:40:55 +0000 | [diff] [blame] | 305 | |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 306 | if (argc <= 1) { |
| 307 | usage(); |
| 308 | return 0; |
| 309 | } |
| 310 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 311 | tc_core_init(); |
osdl.net!shemminger | 7901660 | 2005-03-14 19:34:12 +0000 | [diff] [blame] | 312 | if (rtnl_open(&rth, 0) < 0) { |
| 313 | fprintf(stderr, "Cannot open rtnetlink\n"); |
| 314 | exit(1); |
| 315 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 316 | |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 317 | ret = do_cmd(argc-1, argv+1); |
osdl.net!shemminger | 7901660 | 2005-03-14 19:34:12 +0000 | [diff] [blame] | 318 | rtnl_close(&rth); |
site!shemminger | c2f3a0f | 2005-03-14 22:19:16 +0000 | [diff] [blame] | 319 | |
| 320 | return ret; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 321 | } |