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 28static void usage(void); 29 30static 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"); 40 return; 41} 42 43int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) 44{ 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"); 82 return -1; 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); 116 return 1; 117 } 118 if (q->parse_copt(q, argc, argv, &req.n)) 119 return 1; 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); 125 return -1; 126 } 127 } 128 129 if (d[0]) { 130 ll_init_map(&rth); 131 132 if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) { 133 fprintf(stderr, "Cannot find device \"%s\"\n", d); 134 return 1; 135 } 136 } 137 138 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) 139 return 2; 140 141 return 0; 142} 143 144int filter_ifindex; 145__u32 filter_qdisc; 146 147static int print_class(const struct sockaddr_nl *who, 148 struct nlmsghdr *n, void *arg) 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) { 173 fprintf(stderr, "print_class: NULL kind\n"); 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) { 212 struct rtattr *xstats = NULL; 213 214 if (tb[TCA_STATS] || tb[TCA_STATS2]) { 215 print_tcstats_attr(fp, tb, " ", &xstats); 216 fprintf(fp, "\n"); 217 } 218 if (q && (xstats || tb[TCA_XSTATS]) && q->print_xstats) { 219 q->print_xstats(q, fp, xstats ? : tb[TCA_XSTATS]); 220 fprintf(fp, "\n"); 221 } 222 } 223 fflush(fp); 224 return 0; 225} 226 227 228int tc_class_list(int argc, char **argv) 229{ 230 struct tcmsg t; 231 char d[16]; 232 233 memset(&t, 0, sizeof(t)); 234 t.tcm_family = AF_UNSPEC; 235 memset(d, 0, sizeof(d)); 236 237 while (argc > 0) { 238 if (strcmp(*argv, "dev") == 0) { 239 NEXT_ARG(); 240 if (d[0]) 241 duparg("dev", *argv); 242 strncpy(d, *argv, sizeof(d)-1); 243 } else if (strcmp(*argv, "qdisc") == 0) { 244 NEXT_ARG(); 245 if (filter_qdisc) 246 duparg("qdisc", *argv); 247 if (get_qdisc_handle(&filter_qdisc, *argv)) 248 invarg(*argv, "invalid qdisc ID"); 249 } else if (strcmp(*argv, "root") == 0) { 250 if (t.tcm_parent) { 251 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n"); 252 return -1; 253 } 254 t.tcm_parent = TC_H_ROOT; 255 } else if (strcmp(*argv, "parent") == 0) { 256 __u32 handle; 257 if (t.tcm_parent) 258 duparg("parent", *argv); 259 NEXT_ARG(); 260 if (get_tc_classid(&handle, *argv)) 261 invarg(*argv, "invalid parent ID"); 262 t.tcm_parent = handle; 263 } else if (matches(*argv, "help") == 0) { 264 usage(); 265 } else { 266 fprintf(stderr, "What is \"%s\"? Try \"tc class help\".\n", *argv); 267 return -1; 268 } 269 270 argc--; argv++; 271 } 272 273 ll_init_map(&rth); 274 275 if (d[0]) { 276 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { 277 fprintf(stderr, "Cannot find device \"%s\"\n", d); 278 return 1; 279 } 280 filter_ifindex = t.tcm_ifindex; 281 } 282 283 if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) { 284 perror("Cannot send dump request"); 285 return 1; 286 } 287 288 if (rtnl_dump_filter(&rth, print_class, stdout, NULL, NULL) < 0) { 289 fprintf(stderr, "Dump terminated\n"); 290 return 1; 291 } 292 293 return 0; 294} 295 296int do_class(int argc, char **argv) 297{ 298 if (argc < 1) 299 return tc_class_list(0, NULL); 300 if (matches(*argv, "add") == 0) 301 return tc_class_modify(RTM_NEWTCLASS, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1); 302 if (matches(*argv, "change") == 0) 303 return tc_class_modify(RTM_NEWTCLASS, 0, argc-1, argv+1); 304 if (matches(*argv, "replace") == 0) 305 return tc_class_modify(RTM_NEWTCLASS, NLM_F_CREATE, argc-1, argv+1); 306 if (matches(*argv, "delete") == 0) 307 return tc_class_modify(RTM_DELTCLASS, 0, argc-1, argv+1); 308#if 0 309 if (matches(*argv, "get") == 0) 310 return tc_class_get(RTM_GETTCLASS, 0, argc-1, argv+1); 311#endif 312 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 313 || matches(*argv, "lst") == 0) 314 return tc_class_list(argc-1, argv+1); 315 if (matches(*argv, "help") == 0) 316 usage(); 317 fprintf(stderr, "Command \"%s\" is unknown, try \"tc class help\".\n", *argv); 318 return -1; 319} 320