Home | History | Annotate | Download | only in tc
      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 (at) ms2.inr.ac.ru>
     10  *
     11  * Fixes:
     12  *
     13  * Petri Mattila <petri (at) 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 #include "namespace.h"
     33 
     34 int show_stats;
     35 int show_details;
     36 int show_raw;
     37 int show_pretty;
     38 int show_graph;
     39 int timestamp;
     40 
     41 int batch_mode;
     42 int use_iec;
     43 int force;
     44 bool use_names;
     45 
     46 static char *conf_file;
     47 
     48 struct rtnl_handle rth;
     49 
     50 static void *BODY;	/* cached handle dlopen(NULL) */
     51 static struct qdisc_util *qdisc_list;
     52 static struct filter_util *filter_list;
     53 
     54 #ifdef ANDROID
     55 extern struct qdisc_util cbq_qdisc_util;
     56 extern struct qdisc_util htb_qdisc_util;
     57 extern struct qdisc_util ingress_qdisc_util;
     58 extern struct filter_util u32_filter_util;
     59 #endif
     60 
     61 static int print_noqopt(struct qdisc_util *qu, FILE *f,
     62 			struct rtattr *opt)
     63 {
     64 	if (opt && RTA_PAYLOAD(opt))
     65 		fprintf(f, "[Unknown qdisc, optlen=%u] ",
     66 			(unsigned int) RTA_PAYLOAD(opt));
     67 	return 0;
     68 }
     69 
     70 static int parse_noqopt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
     71 {
     72 	if (argc) {
     73 		fprintf(stderr, "Unknown qdisc \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
     74 		return -1;
     75 	}
     76 	return 0;
     77 }
     78 
     79 static int print_nofopt(struct filter_util *qu, FILE *f, struct rtattr *opt, __u32 fhandle)
     80 {
     81 	if (opt && RTA_PAYLOAD(opt))
     82 		fprintf(f, "fh %08x [Unknown filter, optlen=%u] ",
     83 			fhandle, (unsigned int) RTA_PAYLOAD(opt));
     84 	else if (fhandle)
     85 		fprintf(f, "fh %08x ", fhandle);
     86 	return 0;
     87 }
     88 
     89 static int parse_nofopt(struct filter_util *qu, char *fhandle, int argc, char **argv, struct nlmsghdr *n)
     90 {
     91 	__u32 handle;
     92 
     93 	if (argc) {
     94 		fprintf(stderr, "Unknown filter \"%s\", hence option \"%s\" is unparsable\n", qu->id, *argv);
     95 		return -1;
     96 	}
     97 	if (fhandle) {
     98 		struct tcmsg *t = NLMSG_DATA(n);
     99 
    100 		if (get_u32(&handle, fhandle, 16)) {
    101 			fprintf(stderr, "Unparsable filter ID \"%s\"\n", fhandle);
    102 			return -1;
    103 		}
    104 		t->tcm_handle = handle;
    105 	}
    106 	return 0;
    107 }
    108 
    109 struct qdisc_util *get_qdisc_kind(const char *str)
    110 {
    111 	void *dlh;
    112 	char buf[256];
    113 	struct qdisc_util *q;
    114 
    115 #ifdef ANDROID
    116 	if (!strcmp(str, "cbq"))
    117 		return &cbq_qdisc_util;
    118 	else if (!strcmp(str, "htb"))
    119 		return &htb_qdisc_util;
    120 	else if (!strcmp(str, "ingress"))
    121 		return &ingress_qdisc_util;
    122 	else {
    123 		fprintf(stderr, "Android does not support qdisc '%s'\n", str);
    124 		return NULL;
    125 	}
    126 #endif
    127 	for (q = qdisc_list; q; q = q->next)
    128 		if (strcmp(q->id, str) == 0)
    129 			return q;
    130 
    131 	snprintf(buf, sizeof(buf), "%s/q_%s.so", get_tc_lib(), str);
    132 	dlh = dlopen(buf, RTLD_LAZY);
    133 	if (!dlh) {
    134 		/* look in current binary, only open once */
    135 		dlh = BODY;
    136 		if (dlh == NULL) {
    137 			dlh = BODY = dlopen(NULL, RTLD_LAZY);
    138 			if (dlh == NULL)
    139 				goto noexist;
    140 		}
    141 	}
    142 
    143 	snprintf(buf, sizeof(buf), "%s_qdisc_util", str);
    144 	q = dlsym(dlh, buf);
    145 	if (q == NULL)
    146 		goto noexist;
    147 
    148 reg:
    149 	q->next = qdisc_list;
    150 	qdisc_list = q;
    151 	return q;
    152 
    153 noexist:
    154 	q = calloc(1, sizeof(*q));
    155 	if (q) {
    156 		q->id = strdup(str);
    157 		q->parse_qopt = parse_noqopt;
    158 		q->print_qopt = print_noqopt;
    159 		goto reg;
    160 	}
    161 	return q;
    162 }
    163 
    164 
    165 struct filter_util *get_filter_kind(const char *str)
    166 {
    167 	void *dlh;
    168 	char buf[256];
    169 	struct filter_util *q;
    170 #ifdef ANDROID
    171 	if (!strcmp(str, "u32"))
    172 		return &u32_filter_util;
    173 	else {
    174 		fprintf(stderr, "Android does not support filter '%s'\n", str);
    175 		return NULL;
    176 	}
    177 #endif
    178 
    179 	for (q = filter_list; q; q = q->next)
    180 		if (strcmp(q->id, str) == 0)
    181 			return q;
    182 
    183 	snprintf(buf, sizeof(buf), "%s/f_%s.so", get_tc_lib(), str);
    184 	dlh = dlopen(buf, RTLD_LAZY);
    185 	if (dlh == NULL) {
    186 		dlh = BODY;
    187 		if (dlh == NULL) {
    188 			dlh = BODY = dlopen(NULL, RTLD_LAZY);
    189 			if (dlh == NULL)
    190 				goto noexist;
    191 		}
    192 	}
    193 
    194 	snprintf(buf, sizeof(buf), "%s_filter_util", str);
    195 	q = dlsym(dlh, buf);
    196 	if (q == NULL)
    197 		goto noexist;
    198 
    199 reg:
    200 	q->next = filter_list;
    201 	filter_list = q;
    202 	return q;
    203 noexist:
    204 	q = calloc(1, sizeof(*q));
    205 	if (q) {
    206 		strncpy(q->id, str, 15);
    207 		q->parse_fopt = parse_nofopt;
    208 		q->print_fopt = print_nofopt;
    209 		goto reg;
    210 	}
    211 	return q;
    212 }
    213 
    214 static void usage(void)
    215 {
    216 	fprintf(stderr, "Usage: tc [ OPTIONS ] OBJECT { COMMAND | help }\n"
    217 #ifdef ANDROID
    218 			"       tc [-force]\n"
    219 #else
    220 			"       tc [-force] -batch filename\n"
    221 #endif
    222 			"where  OBJECT := { qdisc | class | filter | action | monitor | exec }\n"
    223 	                "       OPTIONS := { -s[tatistics] | -d[etails] | -r[aw] | -p[retty] | -b[atch] [filename] | -n[etns] name |\n"
    224 			"                    -nm | -nam[es] | { -cf | -conf } path }\n");
    225 }
    226 
    227 static int do_cmd(int argc, char **argv)
    228 {
    229 	if (matches(*argv, "qdisc") == 0)
    230 		return do_qdisc(argc-1, argv+1);
    231 	if (matches(*argv, "class") == 0)
    232 		return do_class(argc-1, argv+1);
    233 	if (matches(*argv, "filter") == 0)
    234 		return do_filter(argc-1, argv+1);
    235 	if (matches(*argv, "actions") == 0)
    236 		return do_action(argc-1, argv+1);
    237 	if (matches(*argv, "monitor") == 0)
    238 		return do_tcmonitor(argc-1, argv+1);
    239 	if (matches(*argv, "exec") == 0)
    240 		return do_exec(argc-1, argv+1);
    241 	if (matches(*argv, "help") == 0) {
    242 		usage();
    243 		return 0;
    244 	}
    245 
    246 	fprintf(stderr, "Object \"%s\" is unknown, try \"tc help\".\n",
    247 		*argv);
    248 	return -1;
    249 }
    250 
    251 #ifndef ANDROID
    252 static int batch(const char *name)
    253 {
    254 	char *line = NULL;
    255 	size_t len = 0;
    256 	int ret = 0;
    257 
    258 	batch_mode = 1;
    259 	if (name && strcmp(name, "-") != 0) {
    260 		if (freopen(name, "r", stdin) == NULL) {
    261 			fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n",
    262 				name, strerror(errno));
    263 			return -1;
    264 		}
    265 	}
    266 
    267 	tc_core_init();
    268 
    269 	if (rtnl_open(&rth, 0) < 0) {
    270 		fprintf(stderr, "Cannot open rtnetlink\n");
    271 		return -1;
    272 	}
    273 
    274 	cmdlineno = 0;
    275 	while (getcmdline(&line, &len, stdin) != -1) {
    276 		char *largv[100];
    277 		int largc;
    278 
    279 		largc = makeargs(line, largv, 100);
    280 		if (largc == 0)
    281 			continue;	/* blank line */
    282 
    283 		if (do_cmd(largc, largv)) {
    284 			fprintf(stderr, "Command failed %s:%d\n", name, cmdlineno);
    285 			ret = 1;
    286 			if (!force)
    287 				break;
    288 		}
    289 	}
    290 	if (line)
    291 		free(line);
    292 
    293 	rtnl_close(&rth);
    294 	return ret;
    295 }
    296 #endif
    297 
    298 
    299 int main(int argc, char **argv)
    300 {
    301 	int ret;
    302 #ifndef ANDROID
    303 	char *batch_file = NULL;
    304 #endif
    305 
    306 	while (argc > 1) {
    307 		if (argv[1][0] != '-')
    308 			break;
    309 		if (matches(argv[1], "-stats") == 0 ||
    310 			 matches(argv[1], "-statistics") == 0) {
    311 			++show_stats;
    312 		} else if (matches(argv[1], "-details") == 0) {
    313 			++show_details;
    314 		} else if (matches(argv[1], "-raw") == 0) {
    315 			++show_raw;
    316 		} else if (matches(argv[1], "-pretty") == 0) {
    317 			++show_pretty;
    318 		} else if (matches(argv[1], "-graph") == 0) {
    319 			show_graph = 1;
    320 		} else if (matches(argv[1], "-Version") == 0) {
    321 			printf("tc utility, iproute2-ss%s\n", SNAPSHOT);
    322 			return 0;
    323 		} else if (matches(argv[1], "-iec") == 0) {
    324 			++use_iec;
    325 		} else if (matches(argv[1], "-help") == 0) {
    326 			usage();
    327 			return 0;
    328 		} else if (matches(argv[1], "-force") == 0) {
    329 			++force;
    330 #ifndef ANDROID
    331 		} else if (matches(argv[1], "-batch") == 0) {
    332 			argc--;	argv++;
    333 			if (argc <= 1)
    334 				usage();
    335 			batch_file = argv[1];
    336 #endif
    337 		} else if (matches(argv[1], "-netns") == 0) {
    338 			NEXT_ARG();
    339 			if (netns_switch(argv[1]))
    340 				return -1;
    341 		} else if (matches(argv[1], "-names") == 0 ||
    342 				matches(argv[1], "-nm") == 0) {
    343 			use_names = true;
    344 		} else if (matches(argv[1], "-cf") == 0 ||
    345 				matches(argv[1], "-conf") == 0) {
    346 			NEXT_ARG();
    347 			conf_file = argv[1];
    348 		} else if (matches(argv[1], "-timestamp") == 0) {
    349 			timestamp++;
    350 		} else if (matches(argv[1], "-tshort") == 0) {
    351 			++timestamp;
    352 			++timestamp_short;
    353 		} else {
    354 			fprintf(stderr, "Option \"%s\" is unknown, try \"tc -help\".\n", argv[1]);
    355 			return -1;
    356 		}
    357 		argc--;	argv++;
    358 	}
    359 
    360 #ifndef ANDROID
    361 	if (batch_file)
    362 		return batch(batch_file);
    363 #endif
    364 
    365 	if (argc <= 1) {
    366 		usage();
    367 		return 0;
    368 	}
    369 
    370 	tc_core_init();
    371 	if (rtnl_open(&rth, 0) < 0) {
    372 		fprintf(stderr, "Cannot open rtnetlink\n");
    373 		exit(1);
    374 	}
    375 
    376 	if (use_names && cls_names_init(conf_file)) {
    377 		ret = -1;
    378 		goto Exit;
    379 	}
    380 
    381 	ret = do_cmd(argc-1, argv+1);
    382 Exit:
    383 	rtnl_close(&rth);
    384 
    385 	if (use_names)
    386 		cls_names_uninit();
    387 
    388 	return ret;
    389 }
    390