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 (opt.def_DP>opt.DPs) { 80/* 81 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); 82*/ 83 fprintf(stderr, "\"default DP\" must be less than %d\n",opt.DPs); 84 return -1; 85 } 86 } else if (strcmp(*argv, "grio") == 0) { 87 opt.grio=1; 88 } else if (strcmp(*argv, "help") == 0) { 89 explain(); 90 return -1; 91 } else { 92 fprintf(stderr, "What is \"%s\"?\n", *argv); 93 explain(); 94 return -1; 95 } 96 argc--; argv++; 97} 98 99if ((!opt.DPs) || (!opt.def_DP)) 100{ 101 fprintf(stderr, "Illegal gred setup parameters \n"); 102 return -1; 103} 104DPRINTF("TC_GRED: sending DPs=%d default=%d\n",opt.DPs,opt.def_DP); 105 n->nlmsg_flags|=NLM_F_CREATE; 106 tail = NLMSG_TAIL(n); 107 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); 108 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt)); 109 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; 110return 0; 111} 112/* 113^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 114*/ 115static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) 116{ 117 int ok=0; 118 struct tc_gred_qopt opt; 119 unsigned burst = 0; 120 unsigned avpkt = 0; 121 double probability = 0.02; 122 unsigned rate = 0; 123 int wlog; 124 __u8 sbuf[256]; 125 struct rtattr *tail; 126 127 memset(&opt, 0, sizeof(opt)); 128 129 while (argc > 0) { 130 if (strcmp(*argv, "limit") == 0) { 131 NEXT_ARG(); 132 if (get_size(&opt.limit, *argv)) { 133 fprintf(stderr, "Illegal \"limit\"\n"); 134 return -1; 135 } 136 ok++; 137 } else if (strcmp(*argv, "setup") == 0) { 138 if (ok) { 139 fprintf(stderr, "Illegal \"setup\"\n"); 140 return -1; 141 } 142 return init_gred(qu,argc-1, argv+1,n); 143 144 } else if (strcmp(*argv, "min") == 0) { 145 NEXT_ARG(); 146 if (get_size(&opt.qth_min, *argv)) { 147 fprintf(stderr, "Illegal \"min\"\n"); 148 return -1; 149 } 150 ok++; 151 } else if (strcmp(*argv, "max") == 0) { 152 NEXT_ARG(); 153 if (get_size(&opt.qth_max, *argv)) { 154 fprintf(stderr, "Illegal \"max\"\n"); 155 return -1; 156 } 157 ok++; 158 } else if (strcmp(*argv, "DP") == 0) { 159 NEXT_ARG(); 160 opt.DP=strtol(*argv, (char **)NULL, 10); 161 DPRINTF ("\n ******* DP =%u\n",opt.DP); 162 if (opt.DP >MAX_DPs) { /* need a better error check */ 163 fprintf(stderr, "DP =%u \n",opt.DP); 164 fprintf(stderr, "Illegal \"DP\"\n"); 165 fprintf(stderr, "GRED: only %d DPs are currently supported\n",MAX_DPs); 166 return -1; 167 } 168 ok++; 169 } else if (strcmp(*argv, "burst") == 0) { 170 NEXT_ARG(); 171 if (get_unsigned(&burst, *argv, 0)) { 172 fprintf(stderr, "Illegal \"burst\"\n"); 173 return -1; 174 } 175 ok++; 176 } else if (strcmp(*argv, "avpkt") == 0) { 177 NEXT_ARG(); 178 if (get_size(&avpkt, *argv)) { 179 fprintf(stderr, "Illegal \"avpkt\"\n"); 180 return -1; 181 } 182 ok++; 183 } else if (strcmp(*argv, "probability") == 0) { 184 NEXT_ARG(); 185 if (sscanf(*argv, "%lg", &probability) != 1) { 186 fprintf(stderr, "Illegal \"probability\"\n"); 187 return -1; 188 } 189 ok++; 190 } else if (strcmp(*argv, "prio") == 0) { 191 NEXT_ARG(); 192 opt.prio=strtol(*argv, (char **)NULL, 10); 193 /* some error check here */ 194 ok++; 195 } else if (strcmp(*argv, "bandwidth") == 0) { 196 NEXT_ARG(); 197 if (get_rate(&rate, *argv)) { 198 fprintf(stderr, "Illegal \"bandwidth\"\n"); 199 return -1; 200 } 201 ok++; 202 } else if (strcmp(*argv, "help") == 0) { 203 explain(); 204 return -1; 205 } else { 206 fprintf(stderr, "What is \"%s\"?\n", *argv); 207 explain(); 208 return -1; 209 } 210 argc--; argv++; 211 } 212 213 if (!ok) 214 return 0; 215 216 if (rate == 0) 217 get_rate(&rate, "10Mbit"); 218 219 if (!opt.qth_min || !opt.qth_max || !burst || !opt.limit || !avpkt || 220 (opt.DP<0)) { 221 fprintf(stderr, "Required parameter (min, max, burst, limit, " 222 "avpket, DP) is missing\n"); 223 return -1; 224 } 225 226 if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) { 227 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n"); 228 return -1; 229 } 230 if (wlog >= 10) 231 fprintf(stderr, "GRED: WARNING. Burst %d seems to be to " 232 "large.\n", burst); 233 opt.Wlog = wlog; 234 if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) { 235 fprintf(stderr, "GRED: failed to calculate probability.\n"); 236 return -1; 237 } 238 opt.Plog = wlog; 239 if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) 240 { 241 fprintf(stderr, "GRED: failed to calculate idle damping " 242 "table.\n"); 243 return -1; 244 } 245 opt.Scell_log = wlog; 246 247 tail = NLMSG_TAIL(n); 248 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); 249 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt)); 250 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256); 251 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; 252 return 0; 253} 254 255static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) 256{ 257 struct rtattr *tb[TCA_GRED_STAB+1]; 258 struct tc_gred_qopt *qopt; 259 int i; 260 SPRINT_BUF(b1); 261 SPRINT_BUF(b2); 262 SPRINT_BUF(b3); 263 SPRINT_BUF(b4); 264 SPRINT_BUF(b5); 265 266 if (opt == NULL) 267 return 0; 268 269 parse_rtattr_nested(tb, TCA_GRED_STAB, opt); 270 271 if (tb[TCA_GRED_PARMS] == NULL) 272 return -1; 273 274 qopt = RTA_DATA(tb[TCA_GRED_PARMS]); 275 if (RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) { 276 fprintf(f,"\n GRED received message smaller than expected\n"); 277 return -1; 278 } 279 280/* Bad hack! should really return a proper message as shown above*/ 281 282 for (i=0;i<MAX_DPs;i++, qopt++) { 283 if (qopt->DP >= MAX_DPs) continue; 284 fprintf(f, "\n DP:%d (prio %d) Average Queue %s Measured " 285 "Queue %s ", 286 qopt->DP, 287 qopt->prio, 288 sprint_size(qopt->qave, b4), 289 sprint_size(qopt->backlog, b5)); 290 fprintf(f, "\n\t Packet drops: %d (forced %d early %d) ", 291 qopt->forced+qopt->early, 292 qopt->forced, 293 qopt->early); 294 fprintf(f, "\n\t Packet totals: %u (bytes %u) ", 295 qopt->packets, 296 qopt->bytesin); 297 if (show_details) 298 fprintf(f, "\n limit %s min %s max %s ", 299 sprint_size(qopt->limit, b1), 300 sprint_size(qopt->qth_min, b2), 301 sprint_size(qopt->qth_max, b3)); 302 fprintf(f, "ewma %u Plog %u Scell_log %u", 303 qopt->Wlog, qopt->Plog, qopt->Scell_log); 304 } 305 return 0; 306} 307 308struct qdisc_util gred_qdisc_util = { 309 .id = "gred", 310 .parse_qopt = gred_parse_opt, 311 .print_qopt = gred_print_opt, 312}; 313