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