Home | History | Annotate | Download | only in tc
      1 /*
      2  * tc_class.c		"tc class".
      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  */
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 #include <unistd.h>
     16 #include <syslog.h>
     17 #include <fcntl.h>
     18 #include <sys/socket.h>
     19 #include <netinet/in.h>
     20 #include <arpa/inet.h>
     21 #include <string.h>
     22 #include <math.h>
     23 
     24 #include "utils.h"
     25 #include "tc_util.h"
     26 #include "tc_common.h"
     27 #include "hlist.h"
     28 
     29 struct graph_node {
     30 	struct hlist_node hlist;
     31 	__u32 id;
     32 	__u32 parent_id;
     33 	struct graph_node *parent_node;
     34 	struct graph_node *right_node;
     35 	void *data;
     36 	int data_len;
     37 	int nodes_count;
     38 };
     39 
     40 static struct hlist_head cls_list = {};
     41 static struct hlist_head root_cls_list = {};
     42 
     43 static void usage(void);
     44 
     45 static void usage(void)
     46 {
     47 	fprintf(stderr, "Usage: tc class [ add | del | change | replace | show ] dev STRING\n");
     48 	fprintf(stderr, "       [ classid CLASSID ] [ root | parent CLASSID ]\n");
     49 	fprintf(stderr, "       [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n");
     50 	fprintf(stderr, "\n");
     51 	fprintf(stderr, "       tc class show [ dev STRING ] [ root | parent CLASSID ]\n");
     52 	fprintf(stderr, "Where:\n");
     53 	fprintf(stderr, "QDISC_KIND := { prio | cbq | etc. }\n");
     54 	fprintf(stderr, "OPTIONS := ... try tc class add <desired QDISC_KIND> help\n");
     55 	return;
     56 }
     57 
     58 static int tc_class_modify(int cmd, unsigned flags, int argc, char **argv)
     59 {
     60 	struct {
     61 		struct nlmsghdr 	n;
     62 		struct tcmsg 		t;
     63 		char   			buf[4096];
     64 	} req;
     65 	struct qdisc_util *q = NULL;
     66 	struct tc_estimator est;
     67 	char  d[16];
     68 	char  k[16];
     69 
     70 	memset(&req, 0, sizeof(req));
     71 	memset(&est, 0, sizeof(est));
     72 	memset(d, 0, sizeof(d));
     73 	memset(k, 0, sizeof(k));
     74 
     75 	req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg));
     76 	req.n.nlmsg_flags = NLM_F_REQUEST|flags;
     77 	req.n.nlmsg_type = cmd;
     78 	req.t.tcm_family = AF_UNSPEC;
     79 
     80 	while (argc > 0) {
     81 		if (strcmp(*argv, "dev") == 0) {
     82 			NEXT_ARG();
     83 			if (d[0])
     84 				duparg("dev", *argv);
     85 			strncpy(d, *argv, sizeof(d)-1);
     86 		} else if (strcmp(*argv, "classid") == 0) {
     87 			__u32 handle;
     88 			NEXT_ARG();
     89 			if (req.t.tcm_handle)
     90 				duparg("classid", *argv);
     91 			if (get_tc_classid(&handle, *argv))
     92 				invarg("invalid class ID", *argv);
     93 			req.t.tcm_handle = handle;
     94 		} else if (strcmp(*argv, "handle") == 0) {
     95 			fprintf(stderr, "Error: try \"classid\" instead of \"handle\"\n");
     96 			return -1;
     97 		} else if (strcmp(*argv, "root") == 0) {
     98 			if (req.t.tcm_parent) {
     99 				fprintf(stderr, "Error: \"root\" is duplicate parent ID.\n");
    100 				return -1;
    101 			}
    102 			req.t.tcm_parent = TC_H_ROOT;
    103 		} else if (strcmp(*argv, "parent") == 0) {
    104 			__u32 handle;
    105 			NEXT_ARG();
    106 			if (req.t.tcm_parent)
    107 				duparg("parent", *argv);
    108 			if (get_tc_classid(&handle, *argv))
    109 				invarg("invalid parent ID", *argv);
    110 			req.t.tcm_parent = handle;
    111 		} else if (matches(*argv, "estimator") == 0) {
    112 			if (parse_estimator(&argc, &argv, &est))
    113 				return -1;
    114 		} else if (matches(*argv, "help") == 0) {
    115 			usage();
    116 		} else {
    117 			strncpy(k, *argv, sizeof(k)-1);
    118 
    119 			q = get_qdisc_kind(k);
    120 			argc--; argv++;
    121 			break;
    122 		}
    123 		argc--; argv++;
    124 	}
    125 
    126 	if (k[0])
    127 		addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1);
    128 	if (est.ewma_log)
    129 		addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
    130 
    131 	if (q) {
    132 		if (q->parse_copt == NULL) {
    133 			fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k);
    134 			return 1;
    135 		}
    136 		if (q->parse_copt(q, argc, argv, &req.n))
    137 			return 1;
    138 	} else {
    139 		if (argc) {
    140 			if (matches(*argv, "help") == 0)
    141 				usage();
    142 			fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc class help\".", *argv);
    143 			return -1;
    144 		}
    145 	}
    146 
    147 	if (d[0])  {
    148 		ll_init_map(&rth);
    149 
    150 		if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
    151 			fprintf(stderr, "Cannot find device \"%s\"\n", d);
    152 			return 1;
    153 		}
    154 	}
    155 
    156 	if (rtnl_talk(&rth, &req.n, NULL, 0) < 0)
    157 		return 2;
    158 
    159 	return 0;
    160 }
    161 
    162 int filter_ifindex;
    163 __u32 filter_qdisc;
    164 __u32 filter_classid;
    165 
    166 static void graph_node_add(__u32 parent_id, __u32 id, void *data,
    167 		int len)
    168 {
    169 	struct graph_node *node = malloc(sizeof(struct graph_node));
    170 
    171 	memset(node, 0, sizeof(*node));
    172 	node->id         = id;
    173 	node->parent_id  = parent_id;
    174 
    175 	if (data && len) {
    176 		node->data       = malloc(len);
    177 		node->data_len   = len;
    178 		memcpy(node->data, data, len);
    179 	}
    180 
    181 	if (parent_id == TC_H_ROOT)
    182 		hlist_add_head(&node->hlist, &root_cls_list);
    183 	else
    184 		hlist_add_head(&node->hlist, &cls_list);
    185 }
    186 
    187 static void graph_indent(char *buf, struct graph_node *node, int is_newline,
    188 		int add_spaces)
    189 {
    190 	char spaces[100] = {0};
    191 
    192 	while (node && node->parent_node) {
    193 		node->parent_node->right_node = node;
    194 		node = node->parent_node;
    195 	}
    196 	while (node && node->right_node) {
    197 		if (node->hlist.next)
    198 			strcat(buf, "|    ");
    199 		else
    200 			strcat(buf, "     ");
    201 
    202 		node = node->right_node;
    203 	}
    204 
    205 	if (is_newline) {
    206 		if (node->hlist.next && node->nodes_count)
    207 			strcat(buf, "|    |");
    208 		else if (node->hlist.next)
    209 			strcat(buf, "|     ");
    210 		else if (node->nodes_count)
    211 			strcat(buf, "     |");
    212 		else if (!node->hlist.next)
    213 			strcat(buf, "      ");
    214 	}
    215 	if (add_spaces > 0) {
    216 		sprintf(spaces, "%-*s", add_spaces, "");
    217 		strcat(buf, spaces);
    218 	}
    219 }
    220 
    221 static void graph_cls_show(FILE *fp, char *buf, struct hlist_head *root_list,
    222 		int level)
    223 {
    224 	struct hlist_node *n, *tmp_cls;
    225 	char cls_id_str[256] = {};
    226 	struct rtattr *tb[TCA_MAX + 1] = {};
    227 	struct qdisc_util *q;
    228 	char str[100] = {};
    229 
    230 	hlist_for_each_safe(n, tmp_cls, root_list) {
    231 		struct hlist_node *c, *tmp_chld;
    232 		struct hlist_head children = {};
    233 		struct graph_node *cls = container_of(n, struct graph_node,
    234 				hlist);
    235 
    236 		hlist_for_each_safe(c, tmp_chld, &cls_list) {
    237 			struct graph_node *child = container_of(c,
    238 					struct graph_node, hlist);
    239 
    240 			if (cls->id == child->parent_id) {
    241 				hlist_del(c);
    242 				hlist_add_head(c, &children);
    243 				cls->nodes_count++;
    244 				child->parent_node = cls;
    245 			}
    246 		}
    247 
    248 		graph_indent(buf, cls, 0, 0);
    249 
    250 		print_tc_classid(cls_id_str, sizeof(cls_id_str), cls->id);
    251 		sprintf(str, "+---(%s)", cls_id_str);
    252 		strcat(buf, str);
    253 
    254 		parse_rtattr(tb, TCA_MAX, (struct rtattr *)cls->data,
    255 				cls->data_len);
    256 
    257 		if (tb[TCA_KIND] == NULL) {
    258 			strcat(buf, " [unknown qdisc kind] ");
    259 		} else {
    260 			const char *kind = rta_getattr_str(tb[TCA_KIND]);
    261 
    262 			sprintf(str, " %s ", kind);
    263 			strcat(buf, str);
    264 			fprintf(fp, "%s", buf);
    265 			buf[0] = '\0';
    266 
    267 			q = get_qdisc_kind(kind);
    268 			if (q && q->print_copt) {
    269 				q->print_copt(q, fp, tb[TCA_OPTIONS]);
    270 			}
    271 			if (q && show_stats) {
    272 				int cls_indent = strlen(q->id) - 2 +
    273 					strlen(cls_id_str);
    274 				struct rtattr *stats = NULL;
    275 
    276 				graph_indent(buf, cls, 1, cls_indent);
    277 
    278 				if (tb[TCA_STATS] || tb[TCA_STATS2]) {
    279 					fprintf(fp, "\n");
    280 					print_tcstats_attr(fp, tb, buf, &stats);
    281 					buf[0] = '\0';
    282 				}
    283 				if (cls->hlist.next || cls->nodes_count) {
    284 					strcat(buf, "\n");
    285 					graph_indent(buf, cls, 1, 0);
    286 				}
    287 			}
    288 		}
    289 		free(cls->data);
    290 		fprintf(fp, "%s\n", buf);
    291 		buf[0] = '\0';
    292 
    293 		graph_cls_show(fp, buf, &children, level + 1);
    294 		if (!cls->hlist.next) {
    295 			graph_indent(buf, cls, 0, 0);
    296 			strcat(buf, "\n");
    297 		}
    298 
    299 		fprintf(fp, "%s", buf);
    300 		buf[0] = '\0';
    301 		free(cls);
    302 	}
    303 }
    304 
    305 int print_class(const struct sockaddr_nl *who,
    306 		       struct nlmsghdr *n, void *arg)
    307 {
    308 	FILE *fp = (FILE*)arg;
    309 	struct tcmsg *t = NLMSG_DATA(n);
    310 	int len = n->nlmsg_len;
    311 	struct rtattr *tb[TCA_MAX + 1] = {};
    312 	struct qdisc_util *q;
    313 	char abuf[256];
    314 
    315 	if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) {
    316 		fprintf(stderr, "Not a class\n");
    317 		return 0;
    318 	}
    319 	len -= NLMSG_LENGTH(sizeof(*t));
    320 	if (len < 0) {
    321 		fprintf(stderr, "Wrong len %d\n", len);
    322 		return -1;
    323 	}
    324 
    325 	if (show_graph) {
    326 		graph_node_add(t->tcm_parent, t->tcm_handle, TCA_RTA(t), len);
    327 		return 0;
    328 	}
    329 
    330 	if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc))
    331 		return 0;
    332 
    333 	if (filter_classid && t->tcm_handle != filter_classid)
    334 		return 0;
    335 
    336 	parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len);
    337 
    338 	if (tb[TCA_KIND] == NULL) {
    339 		fprintf(stderr, "print_class: NULL kind\n");
    340 		return -1;
    341 	}
    342 
    343 	if (n->nlmsg_type == RTM_DELTCLASS)
    344 		fprintf(fp, "deleted ");
    345 
    346 	abuf[0] = 0;
    347 	if (t->tcm_handle) {
    348 		if (filter_qdisc)
    349 			print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle));
    350 		else
    351 			print_tc_classid(abuf, sizeof(abuf), t->tcm_handle);
    352 	}
    353 	fprintf(fp, "class %s %s ", rta_getattr_str(tb[TCA_KIND]), abuf);
    354 
    355 	if (filter_ifindex == 0)
    356 		fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex));
    357 
    358 	if (t->tcm_parent == TC_H_ROOT)
    359 		fprintf(fp, "root ");
    360 	else {
    361 		if (filter_qdisc)
    362 			print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent));
    363 		else
    364 			print_tc_classid(abuf, sizeof(abuf), t->tcm_parent);
    365 		fprintf(fp, "parent %s ", abuf);
    366 	}
    367 	if (t->tcm_info)
    368 		fprintf(fp, "leaf %x: ", t->tcm_info>>16);
    369 	q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND]));
    370 	if (tb[TCA_OPTIONS]) {
    371 		if (q && q->print_copt)
    372 			q->print_copt(q, fp, tb[TCA_OPTIONS]);
    373 		else
    374 			fprintf(fp, "[cannot parse class parameters]");
    375 	}
    376 	fprintf(fp, "\n");
    377 	if (show_stats) {
    378 		struct rtattr *xstats = NULL;
    379 
    380 		if (tb[TCA_STATS] || tb[TCA_STATS2]) {
    381 			print_tcstats_attr(fp, tb, " ", &xstats);
    382 			fprintf(fp, "\n");
    383 		}
    384 		if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) {
    385 			q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]);
    386 			fprintf(fp, "\n");
    387 		}
    388 	}
    389 	fflush(fp);
    390 	return 0;
    391 }
    392 
    393 
    394 static int tc_class_list(int argc, char **argv)
    395 {
    396 	struct tcmsg t;
    397 	char d[16];
    398 	char buf[1024] = {0};
    399 
    400 	memset(&t, 0, sizeof(t));
    401 	t.tcm_family = AF_UNSPEC;
    402 	memset(d, 0, sizeof(d));
    403 
    404 	filter_qdisc = 0;
    405 	filter_classid = 0;
    406 
    407 	while (argc > 0) {
    408 		if (strcmp(*argv, "dev") == 0) {
    409 			NEXT_ARG();
    410 			if (d[0])
    411 				duparg("dev", *argv);
    412 			strncpy(d, *argv, sizeof(d)-1);
    413 		} else if (strcmp(*argv, "qdisc") == 0) {
    414 			NEXT_ARG();
    415 			if (filter_qdisc)
    416 				duparg("qdisc", *argv);
    417 			if (get_qdisc_handle(&filter_qdisc, *argv))
    418 				invarg("invalid qdisc ID", *argv);
    419 		} else if (strcmp(*argv, "classid") == 0) {
    420 			NEXT_ARG();
    421 			if (filter_classid)
    422 				duparg("classid", *argv);
    423 			if (get_tc_classid(&filter_classid, *argv))
    424 				invarg("invalid class ID", *argv);
    425 		} else if (strcmp(*argv, "root") == 0) {
    426 			if (t.tcm_parent) {
    427 				fprintf(stderr, "Error: \"root\" is duplicate parent ID\n");
    428 				return -1;
    429 			}
    430 			t.tcm_parent = TC_H_ROOT;
    431 		} else if (strcmp(*argv, "parent") == 0) {
    432 			__u32 handle;
    433 			if (t.tcm_parent)
    434 				duparg("parent", *argv);
    435 			NEXT_ARG();
    436 			if (get_tc_classid(&handle, *argv))
    437 				invarg("invalid parent ID", *argv);
    438 			t.tcm_parent = handle;
    439 		} else if (matches(*argv, "help") == 0) {
    440 			usage();
    441 		} else {
    442 			fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv);
    443 			return -1;
    444 		}
    445 
    446 		argc--; argv++;
    447 	}
    448 
    449 	ll_init_map(&rth);
    450 
    451 	if (d[0]) {
    452 		if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
    453 			fprintf(stderr, "Cannot find device \"%s\"\n", d);
    454 			return 1;
    455 		}
    456 		filter_ifindex = t.tcm_ifindex;
    457 	}
    458 
    459 	if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
    460 		perror("Cannot send dump request");
    461 		return 1;
    462 	}
    463 
    464 	if (rtnl_dump_filter(&rth, print_class, stdout) < 0) {
    465 		fprintf(stderr, "Dump terminated\n");
    466 		return 1;
    467 	}
    468 
    469 	if (show_graph)
    470 		graph_cls_show(stdout, &buf[0], &root_cls_list, 0);
    471 
    472 	return 0;
    473 }
    474 
    475 int do_class(int argc, char **argv)
    476 {
    477 	if (argc < 1)
    478 		return tc_class_list(0, NULL);
    479 	if (matches(*argv, "add") == 0)
    480 		return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1);
    481 	if (matches(*argv, "change") == 0)
    482 		return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1);
    483 	if (matches(*argv, "replace") == 0)
    484 		return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1);
    485 	if (matches(*argv, "delete") == 0)
    486 		return tc_class_modify(RTM_DELTCLASS, 0,  argc-1, argv+1);
    487 #if 0
    488 	if (matches(*argv, "get") == 0)
    489 		return tc_class_get(RTM_GETTCLASS, 0,  argc-1, argv+1);
    490 #endif
    491 	if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
    492 	    || matches(*argv, "lst") == 0)
    493 		return tc_class_list(argc-1, argv+1);
    494 	if (matches(*argv, "help") == 0) {
    495 		usage();
    496 		return 0;
    497 	}
    498 	fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv);
    499 	return -1;
    500 }
    501