1 /* 2 * tc_qdisc.c "tc qdisc". 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 * J Hadi Salim: Extension to ingress 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 #include <malloc.h> 24 25 #include "utils.h" 26 #include "tc_util.h" 27 #include "tc_common.h" 28 29 static int usage(void) 30 { 31 fprintf(stderr, "Usage: tc qdisc [ add | del | replace | change | show ] dev STRING\n"); 32 fprintf(stderr, " [ handle QHANDLE ] [ root | ingress | clsact | parent CLASSID ]\n"); 33 fprintf(stderr, " [ estimator INTERVAL TIME_CONSTANT ]\n"); 34 fprintf(stderr, " [ stab [ help | STAB_OPTIONS] ]\n"); 35 fprintf(stderr, " [ [ QDISC_KIND ] [ help | OPTIONS ] ]\n"); 36 fprintf(stderr, "\n"); 37 fprintf(stderr, " tc qdisc show [ dev STRING ] [ ingress | clsact ]\n"); 38 fprintf(stderr, "Where:\n"); 39 fprintf(stderr, "QDISC_KIND := { [p|b]fifo | tbf | prio | cbq | red | etc. }\n"); 40 fprintf(stderr, "OPTIONS := ... try tc qdisc add <desired QDISC_KIND> help\n"); 41 fprintf(stderr, "STAB_OPTIONS := ... try tc qdisc add stab help\n"); 42 return -1; 43 } 44 45 static int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv) 46 { 47 struct qdisc_util *q = NULL; 48 struct tc_estimator est; 49 struct { 50 struct tc_sizespec szopts; 51 __u16 *data; 52 } stab; 53 char d[16]; 54 char k[16]; 55 struct { 56 struct nlmsghdr n; 57 struct tcmsg t; 58 char buf[TCA_BUF_MAX]; 59 } req; 60 61 memset(&req, 0, sizeof(req)); 62 memset(&stab, 0, sizeof(stab)); 63 memset(&est, 0, sizeof(est)); 64 memset(&d, 0, sizeof(d)); 65 memset(&k, 0, sizeof(k)); 66 67 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct tcmsg)); 68 req.n.nlmsg_flags = NLM_F_REQUEST|flags; 69 req.n.nlmsg_type = cmd; 70 req.t.tcm_family = AF_UNSPEC; 71 72 while (argc > 0) { 73 if (strcmp(*argv, "dev") == 0) { 74 NEXT_ARG(); 75 if (d[0]) 76 duparg("dev", *argv); 77 strncpy(d, *argv, sizeof(d)-1); 78 } else if (strcmp(*argv, "handle") == 0) { 79 __u32 handle; 80 if (req.t.tcm_handle) 81 duparg("handle", *argv); 82 NEXT_ARG(); 83 if (get_qdisc_handle(&handle, *argv)) 84 invarg("invalid qdisc ID", *argv); 85 req.t.tcm_handle = handle; 86 } else if (strcmp(*argv, "root") == 0) { 87 if (req.t.tcm_parent) { 88 fprintf(stderr, "Error: \"root\" is duplicate parent ID\n"); 89 return -1; 90 } 91 req.t.tcm_parent = TC_H_ROOT; 92 } else if (strcmp(*argv, "clsact") == 0) { 93 if (req.t.tcm_parent) { 94 fprintf(stderr, "Error: \"clsact\" is a duplicate parent ID\n"); 95 return -1; 96 } 97 req.t.tcm_parent = TC_H_CLSACT; 98 strncpy(k, "clsact", sizeof(k) - 1); 99 q = get_qdisc_kind(k); 100 req.t.tcm_handle = TC_H_MAKE(TC_H_CLSACT, 0); 101 NEXT_ARG_FWD(); 102 break; 103 } else if (strcmp(*argv, "ingress") == 0) { 104 if (req.t.tcm_parent) { 105 fprintf(stderr, "Error: \"ingress\" is a duplicate parent ID\n"); 106 return -1; 107 } 108 req.t.tcm_parent = TC_H_INGRESS; 109 strncpy(k, "ingress", sizeof(k) - 1); 110 q = get_qdisc_kind(k); 111 req.t.tcm_handle = TC_H_MAKE(TC_H_INGRESS, 0); 112 NEXT_ARG_FWD(); 113 break; 114 } else if (strcmp(*argv, "parent") == 0) { 115 __u32 handle; 116 NEXT_ARG(); 117 if (req.t.tcm_parent) 118 duparg("parent", *argv); 119 if (get_tc_classid(&handle, *argv)) 120 invarg("invalid parent ID", *argv); 121 req.t.tcm_parent = handle; 122 } else if (matches(*argv, "estimator") == 0) { 123 if (parse_estimator(&argc, &argv, &est)) 124 return -1; 125 } else if (matches(*argv, "stab") == 0) { 126 if (parse_size_table(&argc, &argv, &stab.szopts) < 0) 127 return -1; 128 continue; 129 } else if (matches(*argv, "help") == 0) { 130 usage(); 131 } else { 132 strncpy(k, *argv, sizeof(k)-1); 133 134 q = get_qdisc_kind(k); 135 argc--; argv++; 136 break; 137 } 138 argc--; argv++; 139 } 140 141 if (k[0]) 142 addattr_l(&req.n, sizeof(req), TCA_KIND, k, strlen(k)+1); 143 if (est.ewma_log) 144 addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est)); 145 146 if (q) { 147 if (q->parse_qopt) { 148 if (q->parse_qopt(q, argc, argv, &req.n)) 149 return 1; 150 } else if (argc) { 151 fprintf(stderr, "qdisc '%s' does not support option parsing\n", k); 152 return -1; 153 } 154 } else { 155 if (argc) { 156 if (matches(*argv, "help") == 0) 157 usage(); 158 159 fprintf(stderr, "Garbage instead of arguments \"%s ...\". Try \"tc qdisc help\".\n", *argv); 160 return -1; 161 } 162 } 163 164 if (check_size_table_opts(&stab.szopts)) { 165 struct rtattr *tail; 166 167 if (tc_calc_size_table(&stab.szopts, &stab.data) < 0) { 168 fprintf(stderr, "failed to calculate size table.\n"); 169 return -1; 170 } 171 172 tail = NLMSG_TAIL(&req.n); 173 addattr_l(&req.n, sizeof(req), TCA_STAB, NULL, 0); 174 addattr_l(&req.n, sizeof(req), TCA_STAB_BASE, &stab.szopts, 175 sizeof(stab.szopts)); 176 if (stab.data) 177 addattr_l(&req.n, sizeof(req), TCA_STAB_DATA, stab.data, 178 stab.szopts.tsize * sizeof(__u16)); 179 tail->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)tail; 180 if (stab.data) 181 free(stab.data); 182 } 183 184 if (d[0]) { 185 int idx; 186 187 ll_init_map(&rth); 188 189 if ((idx = ll_name_to_index(d)) == 0) { 190 fprintf(stderr, "Cannot find device \"%s\"\n", d); 191 return 1; 192 } 193 req.t.tcm_ifindex = idx; 194 } 195 196 if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) 197 return 2; 198 199 return 0; 200 } 201 202 static int filter_ifindex; 203 204 int print_qdisc(const struct sockaddr_nl *who, 205 struct nlmsghdr *n, 206 void *arg) 207 { 208 FILE *fp = (FILE*)arg; 209 struct tcmsg *t = NLMSG_DATA(n); 210 int len = n->nlmsg_len; 211 struct rtattr * tb[TCA_MAX+1]; 212 struct qdisc_util *q; 213 char abuf[256]; 214 215 if (n->nlmsg_type != RTM_NEWQDISC && n->nlmsg_type != RTM_DELQDISC) { 216 fprintf(stderr, "Not a qdisc\n"); 217 return 0; 218 } 219 len -= NLMSG_LENGTH(sizeof(*t)); 220 if (len < 0) { 221 fprintf(stderr, "Wrong len %d\n", len); 222 return -1; 223 } 224 225 if (filter_ifindex && filter_ifindex != t->tcm_ifindex) 226 return 0; 227 228 memset(tb, 0, sizeof(tb)); 229 parse_rtattr(tb, TCA_MAX, TCA_RTA(t), len); 230 231 if (tb[TCA_KIND] == NULL) { 232 fprintf(stderr, "print_qdisc: NULL kind\n"); 233 return -1; 234 } 235 236 if (n->nlmsg_type == RTM_DELQDISC) 237 fprintf(fp, "deleted "); 238 239 fprintf(fp, "qdisc %s %x: ", rta_getattr_str(tb[TCA_KIND]), t->tcm_handle>>16); 240 if (filter_ifindex == 0) 241 fprintf(fp, "dev %s ", ll_index_to_name(t->tcm_ifindex)); 242 if (t->tcm_parent == TC_H_ROOT) 243 fprintf(fp, "root "); 244 else if (t->tcm_parent) { 245 print_tc_classid(abuf, sizeof(abuf), t->tcm_parent); 246 fprintf(fp, "parent %s ", abuf); 247 } 248 if (t->tcm_info != 1) { 249 fprintf(fp, "refcnt %d ", t->tcm_info); 250 } 251 /* pfifo_fast is generic enough to warrant the hardcoding --JHS */ 252 253 if (0 == strcmp("pfifo_fast", RTA_DATA(tb[TCA_KIND]))) 254 q = get_qdisc_kind("prio"); 255 else 256 q = get_qdisc_kind(RTA_DATA(tb[TCA_KIND])); 257 258 if (tb[TCA_OPTIONS]) { 259 if (q) 260 q->print_qopt(q, fp, tb[TCA_OPTIONS]); 261 else 262 fprintf(fp, "[cannot parse qdisc parameters]"); 263 } 264 fprintf(fp, "\n"); 265 if (show_details && tb[TCA_STAB]) { 266 print_size_table(fp, " ", tb[TCA_STAB]); 267 fprintf(fp, "\n"); 268 } 269 if (show_stats) { 270 struct rtattr *xstats = NULL; 271 272 if (tb[TCA_STATS] || tb[TCA_STATS2] || tb[TCA_XSTATS]) { 273 print_tcstats_attr(fp, tb, " ", &xstats); 274 fprintf(fp, "\n"); 275 } 276 277 if (q && xstats && q->print_xstats) { 278 q->print_xstats(q, fp, xstats); 279 fprintf(fp, "\n"); 280 } 281 } 282 fflush(fp); 283 return 0; 284 } 285 286 static int tc_qdisc_list(int argc, char **argv) 287 { 288 struct tcmsg t; 289 char d[16]; 290 291 memset(&t, 0, sizeof(t)); 292 t.tcm_family = AF_UNSPEC; 293 memset(&d, 0, sizeof(d)); 294 295 while (argc > 0) { 296 if (strcmp(*argv, "dev") == 0) { 297 NEXT_ARG(); 298 strncpy(d, *argv, sizeof(d)-1); 299 } else if (strcmp(*argv, "ingress") == 0 || 300 strcmp(*argv, "clsact") == 0) { 301 if (t.tcm_parent) { 302 fprintf(stderr, "Duplicate parent ID\n"); 303 usage(); 304 } 305 t.tcm_parent = TC_H_INGRESS; 306 } else if (matches(*argv, "help") == 0) { 307 usage(); 308 } else { 309 fprintf(stderr, "What is \"%s\"? Try \"tc qdisc help\".\n", *argv); 310 return -1; 311 } 312 313 argc--; argv++; 314 } 315 316 ll_init_map(&rth); 317 318 if (d[0]) { 319 if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { 320 fprintf(stderr, "Cannot find device \"%s\"\n", d); 321 return 1; 322 } 323 filter_ifindex = t.tcm_ifindex; 324 } 325 326 if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) { 327 perror("Cannot send dump request"); 328 return 1; 329 } 330 331 if (rtnl_dump_filter(&rth, print_qdisc, stdout) < 0) { 332 fprintf(stderr, "Dump terminated\n"); 333 return 1; 334 } 335 336 return 0; 337 } 338 339 int do_qdisc(int argc, char **argv) 340 { 341 if (argc < 1) 342 return tc_qdisc_list(0, NULL); 343 if (matches(*argv, "add") == 0) 344 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_EXCL|NLM_F_CREATE, argc-1, argv+1); 345 if (matches(*argv, "change") == 0) 346 return tc_qdisc_modify(RTM_NEWQDISC, 0, argc-1, argv+1); 347 if (matches(*argv, "replace") == 0) 348 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); 349 if (matches(*argv, "link") == 0) 350 return tc_qdisc_modify(RTM_NEWQDISC, NLM_F_REPLACE, argc-1, argv+1); 351 if (matches(*argv, "delete") == 0) 352 return tc_qdisc_modify(RTM_DELQDISC, 0, argc-1, argv+1); 353 #if 0 354 if (matches(*argv, "get") == 0) 355 return tc_qdisc_get(RTM_GETQDISC, 0, argc-1, argv+1); 356 #endif 357 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 358 || matches(*argv, "lst") == 0) 359 return tc_qdisc_list(argc-1, argv+1); 360 if (matches(*argv, "help") == 0) { 361 usage(); 362 return 0; 363 } 364 fprintf(stderr, "Command \"%s\" is unknown, try \"tc qdisc help\".\n", *argv); 365 return -1; 366 } 367