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" |
| 27 | |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 28 | static void usage(void); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 29 | |
| 30 | static void usage(void) |
| 31 | { |
Hasso Tepper | e5d179d | 2006-12-10 16:33:05 +0200 | [diff] [blame] | 32 | 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] | 33 | fprintf(stderr, " [ classid CLASSID ] [ root | parent CLASSID ]\n"); |
| 34 | fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n"); |
| 35 | fprintf(stderr, "\n"); |
| 36 | fprintf(stderr, " tc class show [ dev STRING ] [ root | parent CLASSID ]\n"); |
| 37 | fprintf(stderr, "Where:\n"); |
| 38 | fprintf(stderr, "QDISC_KIND := { prio | cbq | etc. }\n"); |
| 39 | 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] | 40 | return; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) |
| 44 | { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 45 | struct { |
| 46 | struct nlmsghdr n; |
| 47 | struct tcmsg t; |
| 48 | char buf[4096]; |
| 49 | } req; |
| 50 | struct qdisc_util *q = NULL; |
| 51 | struct tc_estimator est; |
| 52 | char d[16]; |
| 53 | char k[16]; |
| 54 | |
| 55 | memset(&req, 0, sizeof(req)); |
| 56 | memset(&est, 0, sizeof(est)); |
| 57 | memset(d, 0, sizeof(d)); |
| 58 | memset(k, 0, sizeof(k)); |
| 59 | |
| 60 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)); |
| 61 | req.n.nlmsg_flags = NLM_F_REQUEST|flags; |
| 62 | req.n.nlmsg_type = cmd; |
| 63 | req.t.tcm_family = AF_UNSPEC; |
| 64 | |
| 65 | while (argc > 0) { |
| 66 | if (strcmp(*argv, "dev") == 0) { |
| 67 | NEXT_ARG(); |
| 68 | if (d[0]) |
| 69 | duparg("dev", *argv); |
| 70 | strncpy(d, *argv, sizeof(d)-1); |
| 71 | } else if (strcmp(*argv, "classid") == 0) { |
| 72 | __u32 handle; |
| 73 | NEXT_ARG(); |
| 74 | if (req.t.tcm_handle) |
| 75 | duparg("classid", *argv); |
| 76 | if (get_tc_classid(&handle, *argv)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame^] | 77 | invarg("invalid class ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 78 | req.t.tcm_handle = handle; |
shemminger | 7e6b809 | 2006-03-10 23:40:56 +0000 | [diff] [blame] | 79 | } else if (strcmp(*argv, "handle") == 0) { |
| 80 | fprintf(stderr, "Error: try \"classid\" instead of \"handle\"\n"); |
| 81 | return -1; |
| 82 | } else if (strcmp(*argv, "root") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 83 | if (req.t.tcm_parent) { |
| 84 | fprintf(stderr, "Error: \"root\" is duplicate parent ID.\n"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 85 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 86 | } |
| 87 | req.t.tcm_parent = TC_H_ROOT; |
| 88 | } else if (strcmp(*argv, "parent") == 0) { |
| 89 | __u32 handle; |
| 90 | NEXT_ARG(); |
| 91 | if (req.t.tcm_parent) |
| 92 | duparg("parent", *argv); |
| 93 | if (get_tc_classid(&handle, *argv)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame^] | 94 | invarg("invalid parent ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 95 | req.t.tcm_parent = handle; |
| 96 | } else if (matches(*argv, "estimator") == 0) { |
| 97 | if (parse_estimator(&argc, &argv, &est)) |
| 98 | return -1; |
| 99 | } else if (matches(*argv, "help") == 0) { |
| 100 | usage(); |
| 101 | } else { |
| 102 | strncpy(k, *argv, sizeof(k)-1); |
| 103 | |
| 104 | q = get_qdisc_kind(k); |
| 105 | argc--; argv++; |
| 106 | break; |
| 107 | } |
| 108 | argc--; argv++; |
| 109 | } |
| 110 | |
| 111 | if (k[0]) |
| 112 | addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1); |
| 113 | if (est.ewma_log) |
| 114 | addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est)); |
| 115 | |
| 116 | if (q) { |
| 117 | if (q->parse_copt == NULL) { |
| 118 | fprintf(stderr, "Error: Qdisc \"%s\" is classless.\n", k); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 119 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 120 | } |
| 121 | if (q->parse_copt(q, argc, argv, &req.n)) |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 122 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 123 | } else { |
| 124 | if (argc) { |
| 125 | if (matches(*argv, "help") == 0) |
| 126 | usage(); |
| 127 | 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] | 128 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 132 | if (d[0]) { |
osdl.net!shemminger | 7901660 | 2005-03-14 19:34:12 +0000 | [diff] [blame] | 133 | ll_init_map(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 134 | |
| 135 | if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) { |
| 136 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
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 | } |
| 139 | } |
| 140 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 141 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 142 | return 2; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 143 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 147 | int filter_ifindex; |
| 148 | __u32 filter_qdisc; |
Denys Fedoryshchenko | f4a8b23 | 2009-05-26 15:20:26 -0700 | [diff] [blame] | 149 | __u32 filter_classid; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 150 | |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 151 | int print_class(const struct sockaddr_nl *who, |
osdl.net!shemminger | 50772dc | 2004-12-07 21:48:29 +0000 | [diff] [blame] | 152 | struct nlmsghdr *n, void *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | { |
| 154 | FILE *fp = (FILE*)arg; |
| 155 | struct tcmsg *t = NLMSG_DATA(n); |
| 156 | int len = n->nlmsg_len; |
| 157 | struct rtattr * tb[TCA_MAX+1]; |
| 158 | struct qdisc_util *q; |
| 159 | char abuf[256]; |
| 160 | |
| 161 | if (n->nlmsg_type != RTM_NEWTCLASS && n->nlmsg_type != RTM_DELTCLASS) { |
| 162 | fprintf(stderr, "Not a class\n"); |
| 163 | return 0; |
| 164 | } |
| 165 | len -= NLMSG_LENGTH(sizeof(*t)); |
| 166 | if (len < 0) { |
| 167 | fprintf(stderr, "Wrong len %d\n", len); |
| 168 | return -1; |
| 169 | } |
| 170 | if (filter_qdisc && TC_H_MAJ(t->tcm_handle^filter_qdisc)) |
| 171 | return 0; |
| 172 | |
Denys Fedoryshchenko | f4a8b23 | 2009-05-26 15:20:26 -0700 | [diff] [blame] | 173 | if (filter_classid && t->tcm_handle != filter_classid) |
| 174 | return 0; |
| 175 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 176 | memset(tb, 0, sizeof(tb)); |
| 177 | parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len); |
| 178 | |
| 179 | if (tb[TCA_KIND] == NULL) { |
osdl.net!shemminger | 2373fde | 2004-08-13 23:54:55 +0000 | [diff] [blame] | 180 | fprintf(stderr, "print_class: NULL kind\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | if (n->nlmsg_type == RTM_DELTCLASS) |
| 185 | fprintf(fp, "deleted "); |
| 186 | |
| 187 | abuf[0] = 0; |
| 188 | if (t->tcm_handle) { |
| 189 | if (filter_qdisc) |
| 190 | print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_handle)); |
| 191 | else |
| 192 | print_tc_classid(abuf, sizeof(abuf), t->tcm_handle); |
| 193 | } |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 194 | 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] | 195 | |
| 196 | if (filter_ifindex == 0) |
| 197 | fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex)); |
| 198 | |
| 199 | if (t->tcm_parent == TC_H_ROOT) |
| 200 | fprintf(fp, "root "); |
| 201 | else { |
| 202 | if (filter_qdisc) |
| 203 | print_tc_classid(abuf, sizeof(abuf), TC_H_MIN(t->tcm_parent)); |
| 204 | else |
| 205 | print_tc_classid(abuf, sizeof(abuf), t->tcm_parent); |
| 206 | fprintf(fp, "parent %s ", abuf); |
| 207 | } |
| 208 | if (t->tcm_info) |
| 209 | fprintf(fp, "leaf %x: ", t->tcm_info>>16); |
| 210 | q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND])); |
| 211 | if (tb[TCA_OPTIONS]) { |
| 212 | if (q && q->print_copt) |
| 213 | q->print_copt(q, fp, tb[TCA_OPTIONS]); |
| 214 | else |
| 215 | fprintf(fp, "[cannot parse class parameters]"); |
| 216 | } |
| 217 | fprintf(fp, "\n"); |
| 218 | if (show_stats) { |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 219 | struct rtattr *xstats = NULL; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 220 | |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 221 | if (tb[TCA_STATS] || tb[TCA_STATS2]) { |
| 222 | print_tcstats_attr(fp, tb, " ", &xstats); |
osdl.net!shemminger | 2c5474a | 2004-08-31 17:54:25 +0000 | [diff] [blame] | 223 | fprintf(fp, "\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 224 | } |
ch[shemminger]!tgraf | e5879dc | 2004-12-07 23:52:52 +0000 | [diff] [blame] | 225 | if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) { |
| 226 | q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 227 | fprintf(fp, "\n"); |
| 228 | } |
| 229 | } |
| 230 | fflush(fp); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | int tc_class_list(int argc, char **argv) |
| 236 | { |
| 237 | struct tcmsg t; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 238 | char d[16]; |
| 239 | |
| 240 | memset(&t, 0, sizeof(t)); |
| 241 | t.tcm_family = AF_UNSPEC; |
| 242 | memset(d, 0, sizeof(d)); |
| 243 | |
| 244 | while (argc > 0) { |
| 245 | if (strcmp(*argv, "dev") == 0) { |
| 246 | NEXT_ARG(); |
| 247 | if (d[0]) |
| 248 | duparg("dev", *argv); |
| 249 | strncpy(d, *argv, sizeof(d)-1); |
| 250 | } else if (strcmp(*argv, "qdisc") == 0) { |
| 251 | NEXT_ARG(); |
| 252 | if (filter_qdisc) |
| 253 | duparg("qdisc", *argv); |
| 254 | if (get_qdisc_handle(&filter_qdisc, *argv)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame^] | 255 | invarg("invalid qdisc ID", *argv); |
Denys Fedoryshchenko | f4a8b23 | 2009-05-26 15:20:26 -0700 | [diff] [blame] | 256 | } else if (strcmp(*argv, "classid") == 0) { |
| 257 | NEXT_ARG(); |
| 258 | if (filter_classid) |
| 259 | duparg("classid", *argv); |
| 260 | if (get_tc_classid(&filter_classid, *argv)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame^] | 261 | invarg("invalid class ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 262 | } else if (strcmp(*argv, "root") == 0) { |
| 263 | if (t.tcm_parent) { |
| 264 | fprintf(stderr, "Error: \"root\" is duplicate parent ID\n"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 265 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 266 | } |
| 267 | t.tcm_parent = TC_H_ROOT; |
| 268 | } else if (strcmp(*argv, "parent") == 0) { |
| 269 | __u32 handle; |
| 270 | if (t.tcm_parent) |
| 271 | duparg("parent", *argv); |
| 272 | NEXT_ARG(); |
| 273 | if (get_tc_classid(&handle, *argv)) |
Dan Kenigsberg | f1675d6 | 2012-08-16 02:25:56 +0000 | [diff] [blame^] | 274 | invarg("invalid parent ID", *argv); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 275 | t.tcm_parent = handle; |
| 276 | } else if (matches(*argv, "help") == 0) { |
| 277 | usage(); |
| 278 | } else { |
| 279 | fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 280 | return -1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | argc--; argv++; |
| 284 | } |
| 285 | |
osdl.net!shemminger | 7901660 | 2005-03-14 19:34:12 +0000 | [diff] [blame] | 286 | ll_init_map(&rth); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 287 | |
| 288 | if (d[0]) { |
| 289 | if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { |
| 290 | fprintf(stderr, "Cannot find device \"%s\"\n", d); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 291 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 292 | } |
| 293 | filter_ifindex = t.tcm_ifindex; |
| 294 | } |
| 295 | |
osdl.net!shemminger | 7901660 | 2005-03-14 19:34:12 +0000 | [diff] [blame] | 296 | if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 297 | perror("Cannot send dump request"); |
osdl.net!shemminger | b611d51 | 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 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 301 | if (rtnl_dump_filter(&rth, print_class, stdout) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 302 | fprintf(stderr, "Dump terminated\n"); |
osdl.net!shemminger | b611d51 | 2005-03-14 19:02:41 +0000 | [diff] [blame] | 303 | return 1; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 304 | } |
| 305 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | int do_class(int argc, char **argv) |
| 310 | { |
| 311 | if (argc < 1) |
| 312 | return tc_class_list(0, NULL); |
| 313 | if (matches(*argv, "add") == 0) |
| 314 | return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1); |
| 315 | if (matches(*argv, "change") == 0) |
| 316 | return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1); |
| 317 | if (matches(*argv, "replace") == 0) |
| 318 | return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1); |
| 319 | if (matches(*argv, "delete") == 0) |
| 320 | return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1); |
| 321 | #if 0 |
| 322 | if (matches(*argv, "get") == 0) |
| 323 | return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1); |
| 324 | #endif |
| 325 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 326 | || matches(*argv, "lst") == 0) |
| 327 | return tc_class_list(argc-1, argv+1); |
Hasso Tepper | e5d179d | 2006-12-10 16:33:05 +0200 | [diff] [blame] | 328 | if (matches(*argv, "help") == 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 329 | usage(); |
Hasso Tepper | e5d179d | 2006-12-10 16:33:05 +0200 | [diff] [blame] | 330 | return 0; |
| 331 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 332 | fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv); |
| 333 | return -1; |
| 334 | } |