q_gred.c revision 95812b56a5a66e7e9a21744cfe8bc0bb9791ea98
1/* 2 * q_gred.c GRED. 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: J Hadi Salim(hadi@nortelnetworks.com) 10 * code ruthlessly ripped from 11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 12 * 13 */ 14 15#include <stdio.h> 16#include <stdlib.h> 17#include <unistd.h> 18#include <syslog.h> 19#include <fcntl.h> 20#include <sys/socket.h> 21#include <netinet/in.h> 22#include <arpa/inet.h> 23#include <string.h> 24 25#include "utils.h" 26#include "tc_util.h" 27 28#include "tc_red.h" 29 30 31#if 0 32#define DPRINTF(format,args...) fprintf(stderr,format,##args) 33#else 34#define DPRINTF(format,args...) 35#endif 36 37static void explain(void) 38{ 39 fprintf(stderr, "Usage: ... gred DP drop-probability limit BYTES " 40 "min BYTES max BYTES\n"); 41 fprintf(stderr, " avpkt BYTES burst PACKETS probability PROBABILITY " 42 "bandwidth KBPS\n"); 43 fprintf(stderr, " [prio value]\n"); 44 fprintf(stderr," OR ...\n"); 45 fprintf(stderr," gred setup DPs <num of DPs> default <default DP> " 46 "[grio]\n"); 47} 48 49#define usage() return(-1) 50 51static int init_gred(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) 52{ 53 54 struct rtattr *tail; 55 struct tc_gred_sopt opt; 56 memset(&opt, 0, sizeof(struct tc_gred_sopt)); 57 58 while (argc > 0) { 59 DPRINTF(stderr,"init_gred: invoked with %s\n",*argv); 60 if (strcmp(*argv, "DPs") == 0) { 61 NEXT_ARG(); 62 DPRINTF(stderr,"init_gred: next_arg with %s\n",*argv); 63 opt.DPs=strtol(*argv, (char **)NULL, 10); 64 if (opt.DPs >MAX_DPs) { /* need a better error check */ 65 fprintf(stderr, "DPs =%u \n",opt.DPs); 66 fprintf(stderr, "Illegal \"DPs\"\n"); 67 fprintf(stderr, "GRED: only %d DPs are " 68 "currently supported\n",MAX_DPs); 69 return -1; 70 } 71 } else if (strcmp(*argv, "default") == 0) { 72 NEXT_ARG(); 73 opt.def_DP=strtol(*argv, (char **)NULL, 10); 74 if (!opt.DPs) { 75 fprintf(stderr, "\"default DP\" must be " 76 "defined after DPs\n"); 77 return -1; 78 } 79#if 0 80 if (opt.def_DP>opt.DPs-1) { 81#endif 82 if (opt.def_DP>opt.DPs) { 83/* 84 fprintf(stderr, "\"default DP\" must be less than %d\nNote: DP runs from 0 to %d for %d DPs\n",opt.DPs,opt.DPs-1,opt.DPs); 85*/ 86 fprintf(stderr, "\"default DP\" must be less than %d\n",opt.DPs); 87 return -1; 88 } 89 } else if (strcmp(*argv, "grio") == 0) { 90 opt.grio=1; 91 } else if (strcmp(*argv, "help") == 0) { 92 explain(); 93 return -1; 94 } else { 95 fprintf(stderr, "What is \"%s\"?\n", *argv); 96 explain(); 97 return -1; 98 } 99 argc--; argv++; 100} 101 102if ((!opt.DPs) || (!opt.def_DP)) 103{ 104 fprintf(stderr, "Illegal gred setup parameters \n"); 105 return -1; 106} 107DPRINTF("TC_GRED: sending DPs=%d default=%d\n",opt.DPs,opt.def_DP); 108 n->nlmsg_flags|=NLM_F_CREATE; 109 tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len)); 110 111 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); 112 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt)); 113 tail->rta_len = (((void*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail; 114return 0; 115} 116/* 117^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 118*/ 119static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) 120{ 121 int ok=0; 122 struct tc_gred_qopt opt; 123 unsigned burst = 0; 124 unsigned avpkt = 0; 125 double probability = 0.02; 126 unsigned rate = 0; 127 int wlog; 128 __u8 sbuf[256]; 129 struct rtattr *tail; 130 131 memset(&opt, 0, sizeof(opt)); 132 133 while (argc > 0) { 134 if (strcmp(*argv, "limit") == 0) { 135 NEXT_ARG(); 136 if (get_size(&opt.limit, *argv)) { 137 fprintf(stderr, "Illegal \"limit\"\n"); 138 return -1; 139 } 140 ok++; 141 } else if (strcmp(*argv, "setup") == 0) { 142 if (ok) { 143 fprintf(stderr, "Illegal \"setup\"\n"); 144 return -1; 145 } 146 return init_gred(qu,argc-1, argv+1,n); 147 148 } else if (strcmp(*argv, "min") == 0) { 149 NEXT_ARG(); 150 if (get_size(&opt.qth_min, *argv)) { 151 fprintf(stderr, "Illegal \"min\"\n"); 152 return -1; 153 } 154 ok++; 155 } else if (strcmp(*argv, "max") == 0) { 156 NEXT_ARG(); 157 if (get_size(&opt.qth_max, *argv)) { 158 fprintf(stderr, "Illegal \"max\"\n"); 159 return -1; 160 } 161 ok++; 162 } else if (strcmp(*argv, "DP") == 0) { 163 NEXT_ARG(); 164 opt.DP=strtol(*argv, (char **)NULL, 10); 165 DPRINTF ("\n ******* DP =%u\n",opt.DP); 166 if (opt.DP >MAX_DPs) { /* need a better error check */ 167 fprintf(stderr, "DP =%u \n",opt.DP); 168 fprintf(stderr, "Illegal \"DP\"\n"); 169 fprintf(stderr, "GRED: only %d DPs are currently supported\n",MAX_DPs); 170 return -1; 171 } 172#if 0 173 return -1; 174 } 175#endif 176 ok++; 177 } else if (strcmp(*argv, "burst") == 0) { 178 NEXT_ARG(); 179 if (get_unsigned(&burst, *argv, 0)) { 180 fprintf(stderr, "Illegal \"burst\"\n"); 181 return -1; 182 } 183 ok++; 184 } else if (strcmp(*argv, "avpkt") == 0) { 185 NEXT_ARG(); 186 if (get_size(&avpkt, *argv)) { 187 fprintf(stderr, "Illegal \"avpkt\"\n"); 188 return -1; 189 } 190 ok++; 191 } else if (strcmp(*argv, "probability") == 0) { 192 NEXT_ARG(); 193 if (sscanf(*argv, "%lg", &probability) != 1) { 194 fprintf(stderr, "Illegal \"probability\"\n"); 195 return -1; 196 } 197 ok++; 198 } else if (strcmp(*argv, "prio") == 0) { 199 NEXT_ARG(); 200 opt.prio=strtol(*argv, (char **)NULL, 10); 201 /* some error check here */ 202 ok++; 203 } else if (strcmp(*argv, "bandwidth") == 0) { 204 NEXT_ARG(); 205 if (get_rate(&rate, *argv)) { 206 fprintf(stderr, "Illegal \"bandwidth\"\n"); 207 return -1; 208 } 209 ok++; 210 } else if (strcmp(*argv, "help") == 0) { 211 explain(); 212 return -1; 213 } else { 214 fprintf(stderr, "What is \"%s\"?\n", *argv); 215 explain(); 216 return -1; 217 } 218 argc--; argv++; 219 } 220 221 if (!ok) 222 return 0; 223 224 if (rate == 0) 225 get_rate(&rate, "10Mbit"); 226 227 if (!opt.qth_min || !opt.qth_max || !burst || !opt.limit || !avpkt || 228 (opt.DP<0)) { 229 fprintf(stderr, "Required parameter (min, max, burst, limit, " 230 "avpket, DP) is missing\n"); 231 return -1; 232 } 233 234 if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) { 235 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n"); 236 return -1; 237 } 238 if (wlog >= 10) 239 fprintf(stderr, "GRED: WARNING. Burst %d seems to be to " 240 "large.\n", burst); 241 opt.Wlog = wlog; 242 if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) { 243 fprintf(stderr, "GRED: failed to calculate probability.\n"); 244 return -1; 245 } 246 opt.Plog = wlog; 247 if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) 248 { 249 fprintf(stderr, "GRED: failed to calculate idle damping " 250 "table.\n"); 251 return -1; 252 } 253 opt.Scell_log = wlog; 254 255 tail = (struct rtattr*)(((void*)n)+NLMSG_ALIGN(n->nlmsg_len)); 256 257 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); 258 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt)); 259 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256); 260 tail->rta_len = (((void*)n)+NLMSG_ALIGN(n->nlmsg_len)) - (void*)tail; 261 return 0; 262} 263 264static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) 265{ 266 struct rtattr *tb[TCA_GRED_STAB+1]; 267 struct tc_gred_qopt *qopt; 268 int i; 269 SPRINT_BUF(b1); 270 SPRINT_BUF(b2); 271 SPRINT_BUF(b3); 272 SPRINT_BUF(b4); 273 SPRINT_BUF(b5); 274 275 if (opt == NULL) 276 return 0; 277 278 memset(tb, 0, sizeof(tb)); 279 parse_rtattr(tb, TCA_GRED_STAB, RTA_DATA(opt), RTA_PAYLOAD(opt)); 280 281 if (tb[TCA_GRED_PARMS] == NULL) 282 return -1; 283#if 0 284 sopt = RTA_DATA(tb[TCA_GRED_DPS]); 285 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt)) { 286 printf("\n GRED DPs message smaller than expected\n"); 287 return -1; 288 } 289 290 DPRINTF(f, "\n\tDPs:%d Default DP %d\n ", 291 sopt->DPs, sopt->def_DP); 292#endif 293 qopt = RTA_DATA(tb[TCA_GRED_PARMS]); 294 if (RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) { 295 fprintf(f,"\n GRED received message smaller than expected\n"); 296 return -1; 297 } 298 299 300#if 0 301 302 for (i=0;i<sopt->DPs;i++) 303#endif 304/* Bad hack! should really return a proper message as shown above*/ 305 306 for (i=0;i<MAX_DPs;i++, qopt++) { 307 if (qopt->DP >= MAX_DPs) continue; 308 fprintf(f, "\n DP:%d (prio %d) Average Queue %s Measured " 309 "Queue %s ", 310 qopt->DP, 311 qopt->prio, 312 sprint_size(qopt->qave, b4), 313 sprint_size(qopt->backlog, b5)); 314 fprintf(f, "\n\t Packet drops: %d (forced %d early %d) ", 315 qopt->forced+qopt->early, 316 qopt->forced, 317 qopt->early); 318 fprintf(f, "\n\t Packet totals: %u (bytes %u) ", 319 qopt->packets, 320 qopt->bytesin); 321 if (show_details) 322 fprintf(f, "\n limit %s min %s max %s ", 323 sprint_size(qopt->limit, b1), 324 sprint_size(qopt->qth_min, b2), 325 sprint_size(qopt->qth_max, b3)); 326 fprintf(f, "ewma %u Plog %u Scell_log %u", 327 qopt->Wlog, qopt->Plog, qopt->Scell_log); 328 } 329 return 0; 330} 331 332struct qdisc_util gred_qdisc_util = { 333 .id = "gred", 334 .parse_qopt = gred_parse_opt, 335 .print_qopt = gred_print_opt, 336}; 337