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