osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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@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" |
Vadim Kochan | d954b34 | 2014-12-26 02:10:06 +0200 | [diff] [blame^] | 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 = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 42 | |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 43 | static void usage(void); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | |
| 45 | static void usage(void) |
| 46 | { |
Hasso Tepper | e5d179d | 2006-12-10 16:33:05 +0200 | [diff] [blame] | 47 | fprintf(stderr, "Usage: tc class [ add | del | change | replace | show ] dev STRING\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 55 | return; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 58 | static int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 59 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame] | 92 | invarg("invalid class ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 93 | req.t.tcm_handle = handle; |
shemminger | 7e6b809 | 2006-03-10 23:40:56 +0000 | [diff] [blame] | 94 | } else if (strcmp(*argv, "handle") == 0) { |
| 95 | fprintf(stderr, "Error: try \"classid\" instead of \"handle\"\n"); |
| 96 | return -1; |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 97 | } else if (strcmp(*argv, "root") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 98 | if (req.t.tcm_parent) { |
| 99 | fprintf(stderr, "Error: \"root\" is duplicate parent ID.\n"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 100 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame] | 109 | invarg("invalid parent ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 134 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 135 | } |
| 136 | if (q->parse_copt(q, argc, argv, &req.n)) |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 137 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 143 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 147 | if (d[0]) { |
osdl.net!shemminger | 7901660 | 2005-03-14 19:34:12 +0000 | [diff] [blame] | 148 | ll_init_map(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 149 | |
| 150 | if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) { |
| 151 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 152 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 156 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 157 | return 2; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 158 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 162 | int filter_ifindex; |
| 163 | __u32 filter_qdisc; |
Denys Fedoryshchenko | f4a8b23 | 2009-05-26 15:20:26 -0700 | [diff] [blame] | 164 | __u32 filter_classid; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 165 | |
Vadim Kochan | d954b34 | 2014-12-26 02:10:06 +0200 | [diff] [blame^] | 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 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 305 | int print_class(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 306 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 307 | { |
| 308 | FILE *fp = (FILE*)arg; |
| 309 | struct tcmsg *t = NLMSG_DATA(n); |
| 310 | int len = n->nlmsg_len; |
Vadim Kochan | d954b34 | 2014-12-26 02:10:06 +0200 | [diff] [blame^] | 311 | struct rtattr *tb[TCA_MAX + 1] = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | } |
Vadim Kochan | d954b34 | 2014-12-26 02:10:06 +0200 | [diff] [blame^] | 324 | |
| 325 | if (show_graph) { |
| 326 | graph_node_add(t->tcm_parent, t->tcm_handle, TCA_RTA(t), len); |
| 327 | return 0; |
| 328 | } |
| 329 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 330 | if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc)) |
| 331 | return 0; |
| 332 | |
Denys Fedoryshchenko | f4a8b23 | 2009-05-26 15:20:26 -0700 | [diff] [blame] | 333 | if (filter_classid && t->tcm_handle != filter_classid) |
| 334 | return 0; |
| 335 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 336 | parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len); |
| 337 | |
| 338 | if (tb[TCA_KIND] == NULL) { |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 339 | fprintf(stderr, "print_class: NULL kind\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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 | } |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 353 | fprintf(fp, "class %s %s ", rta_getattr_str(tb[TCA_KIND]), abuf); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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) { |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 378 | struct rtattr *xstats = NULL; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 379 | |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 380 | if (tb[TCA_STATS] || tb[TCA_STATS2]) { |
| 381 | print_tcstats_attr(fp, tb, " ", &xstats); |
osdl.net!shemminger | 2c5474a | 2004-08-31 17:54:25 +0000 | [diff] [blame] | 382 | fprintf(fp, "\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 383 | } |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 384 | if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) { |
| 385 | q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 386 | fprintf(fp, "\n"); |
| 387 | } |
| 388 | } |
| 389 | fflush(fp); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 394 | static int tc_class_list(int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 395 | { |
| 396 | struct tcmsg t; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 397 | char d[16]; |
Vadim Kochan | d954b34 | 2014-12-26 02:10:06 +0200 | [diff] [blame^] | 398 | char buf[1024] = {0}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 399 | |
| 400 | memset(&t, 0, sizeof(t)); |
| 401 | t.tcm_family = AF_UNSPEC; |
| 402 | memset(d, 0, sizeof(d)); |
| 403 | |
Nigel Kukard | 9bea14f | 2013-10-30 18:44:58 +0000 | [diff] [blame] | 404 | filter_qdisc = 0; |
| 405 | filter_classid = 0; |
| 406 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame] | 418 | invarg("invalid qdisc ID", *argv); |
Denys Fedoryshchenko | f4a8b23 | 2009-05-26 15:20:26 -0700 | [diff] [blame] | 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)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame] | 424 | invarg("invalid class ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 425 | } else if (strcmp(*argv, "root") == 0) { |
| 426 | if (t.tcm_parent) { |
| 427 | fprintf(stderr, "Error: \"root\" is duplicate parent ID\n"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 428 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame] | 437 | invarg("invalid parent ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 443 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | argc--; argv++; |
| 447 | } |
| 448 | |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 449 | ll_init_map(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 454 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 455 | } |
| 456 | filter_ifindex = t.tcm_ifindex; |
| 457 | } |
| 458 | |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 459 | if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 460 | perror("Cannot send dump request"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 461 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Stephen Hemminger | 3d0b743 | 2014-12-20 15:47:17 -0800 | [diff] [blame] | 464 | if (rtnl_dump_filter(&rth, print_class, stdout) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 465 | fprintf(stderr, "Dump terminated\n"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 466 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Vadim Kochan | d954b34 | 2014-12-26 02:10:06 +0200 | [diff] [blame^] | 469 | if (show_graph) |
| 470 | graph_cls_show(stdout, &buf[0], &root_cls_list, 0); |
| 471 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 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); |
Hasso Tepper | e5d179d | 2006-12-10 16:33:05 +0200 | [diff] [blame] | 494 | if (matches(*argv, "help") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 495 | usage(); |
Hasso Tepper | e5d179d | 2006-12-10 16:33:05 +0200 | [diff] [blame] | 496 | return 0; |
| 497 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 498 | fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv); |
| 499 | return -1; |
| 500 | } |