1/* 2 * q_sfq.c SFQ. 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_red.h" 27 28static void explain(void) 29{ 30 fprintf(stderr, "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n"); 31 fprintf(stderr, " [ divisor NUMBER ] [ flows NUMBER] [ depth NUMBER ]\n"); 32 fprintf(stderr, " [ headdrop ]\n"); 33 fprintf(stderr, " [ redflowlimit BYTES ] [ min BYTES ] [ max BYTES ]\n"); 34 fprintf(stderr, " [ avpkt BYTES ] [ burst PACKETS ] [ probability P ]\n"); 35 fprintf(stderr, " [ ecn ] [ harddrop ]\n"); 36} 37 38static int sfq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) 39{ 40 int ok = 0, red = 0; 41 struct tc_sfq_qopt_v1 opt; 42 unsigned int burst = 0; 43 int wlog; 44 unsigned int avpkt = 1000; 45 double probability = 0.02; 46 47 memset(&opt, 0, sizeof(opt)); 48 49 while (argc > 0) { 50 if (strcmp(*argv, "quantum") == 0) { 51 NEXT_ARG(); 52 if (get_size(&opt.v0.quantum, *argv)) { 53 fprintf(stderr, "Illegal \"limit\"\n"); 54 return -1; 55 } 56 ok++; 57 } else if (strcmp(*argv, "perturb") == 0) { 58 NEXT_ARG(); 59 if (get_integer(&opt.v0.perturb_period, *argv, 0)) { 60 fprintf(stderr, "Illegal \"perturb\"\n"); 61 return -1; 62 } 63 ok++; 64 } else if (strcmp(*argv, "limit") == 0) { 65 NEXT_ARG(); 66 if (get_u32(&opt.v0.limit, *argv, 0)) { 67 fprintf(stderr, "Illegal \"limit\"\n"); 68 return -1; 69 } 70 if (opt.v0.limit < 2) { 71 fprintf(stderr, "Illegal \"limit\", must be > 1\n"); 72 return -1; 73 } 74 ok++; 75 } else if (strcmp(*argv, "divisor") == 0) { 76 NEXT_ARG(); 77 if (get_u32(&opt.v0.divisor, *argv, 0)) { 78 fprintf(stderr, "Illegal \"divisor\"\n"); 79 return -1; 80 } 81 ok++; 82 } else if (strcmp(*argv, "flows") == 0) { 83 NEXT_ARG(); 84 if (get_u32(&opt.v0.flows, *argv, 0)) { 85 fprintf(stderr, "Illegal \"flows\"\n"); 86 return -1; 87 } 88 ok++; 89 } else if (strcmp(*argv, "depth") == 0) { 90 NEXT_ARG(); 91 if (get_u32(&opt.depth, *argv, 0)) { 92 fprintf(stderr, "Illegal \"flows\"\n"); 93 return -1; 94 } 95 ok++; 96 } else if (strcmp(*argv, "headdrop") == 0) { 97 opt.headdrop = 1; 98 ok++; 99 } else if (strcmp(*argv, "redflowlimit") == 0) { 100 NEXT_ARG(); 101 if (get_u32(&opt.limit, *argv, 0)) { 102 fprintf(stderr, "Illegal \"redflowlimit\"\n"); 103 return -1; 104 } 105 red++; 106 } else if (strcmp(*argv, "min") == 0) { 107 NEXT_ARG(); 108 if (get_u32(&opt.qth_min, *argv, 0)) { 109 fprintf(stderr, "Illegal \"min\"\n"); 110 return -1; 111 } 112 red++; 113 } else if (strcmp(*argv, "max") == 0) { 114 NEXT_ARG(); 115 if (get_u32(&opt.qth_max, *argv, 0)) { 116 fprintf(stderr, "Illegal \"max\"\n"); 117 return -1; 118 } 119 red++; 120 } else if (strcmp(*argv, "burst") == 0) { 121 NEXT_ARG(); 122 if (get_unsigned(&burst, *argv, 0)) { 123 fprintf(stderr, "Illegal \"burst\"\n"); 124 return -1; 125 } 126 red++; 127 } else if (strcmp(*argv, "avpkt") == 0) { 128 NEXT_ARG(); 129 if (get_size(&avpkt, *argv)) { 130 fprintf(stderr, "Illegal \"avpkt\"\n"); 131 return -1; 132 } 133 red++; 134 } else if (strcmp(*argv, "probability") == 0) { 135 NEXT_ARG(); 136 if (sscanf(*argv, "%lg", &probability) != 1) { 137 fprintf(stderr, "Illegal \"probability\"\n"); 138 return -1; 139 } 140 red++; 141 } else if (strcmp(*argv, "ecn") == 0) { 142 opt.flags |= TC_RED_ECN; 143 red++; 144 } else if (strcmp(*argv, "harddrop") == 0) { 145 opt.flags |= TC_RED_HARDDROP; 146 red++; 147 } else if (strcmp(*argv, "help") == 0) { 148 explain(); 149 return -1; 150 } else { 151 fprintf(stderr, "What is \"%s\"?\n", *argv); 152 explain(); 153 return -1; 154 } 155 argc--; argv++; 156 } 157 if (red) { 158 if (!opt.limit) { 159 fprintf(stderr, "Required parameter (redflowlimit) is missing\n"); 160 return -1; 161 } 162 /* Compute default min/max thresholds based on 163 Sally Floyd's recommendations: 164 http://www.icir.org/floyd/REDparameters.txt 165 */ 166 if (!opt.qth_max) 167 opt.qth_max = opt.limit / 4; 168 if (!opt.qth_min) 169 opt.qth_min = opt.qth_max / 3; 170 if (!burst) 171 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt); 172 173 if (opt.qth_max > opt.limit) { 174 fprintf(stderr, "\"max\" is larger than \"limit\"\n"); 175 return -1; 176 } 177 178 if (opt.qth_min >= opt.qth_max) { 179 fprintf(stderr, "\"min\" is not smaller than \"max\"\n"); 180 return -1; 181 } 182 183 wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt); 184 if (wlog < 0) { 185 fprintf(stderr, "SFQ: failed to calculate EWMA constant.\n"); 186 return -1; 187 } 188 if (wlog >= 10) 189 fprintf(stderr, "SFQ: WARNING. Burst %u seems to be too large.\n", burst); 190 opt.Wlog = wlog; 191 192 wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability); 193 if (wlog < 0) { 194 fprintf(stderr, "SFQ: failed to calculate probability.\n"); 195 return -1; 196 } 197 opt.Plog = wlog; 198 opt.max_P = probability * pow(2, 32); 199 } 200 201 if (ok || red) 202 addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt)); 203 return 0; 204} 205 206static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) 207{ 208 struct tc_sfq_qopt *qopt; 209 struct tc_sfq_qopt_v1 *qopt_ext = NULL; 210 SPRINT_BUF(b1); 211 SPRINT_BUF(b2); 212 SPRINT_BUF(b3); 213 if (opt == NULL) 214 return 0; 215 216 if (RTA_PAYLOAD(opt) < sizeof(*qopt)) 217 return -1; 218 if (RTA_PAYLOAD(opt) >= sizeof(*qopt_ext)) 219 qopt_ext = RTA_DATA(opt); 220 qopt = RTA_DATA(opt); 221 fprintf(f, "limit %up ", qopt->limit); 222 fprintf(f, "quantum %s ", sprint_size(qopt->quantum, b1)); 223 if (qopt_ext && qopt_ext->depth) 224 fprintf(f, "depth %u ", qopt_ext->depth); 225 if (qopt_ext && qopt_ext->headdrop) 226 fprintf(f, "headdrop "); 227 228 if (show_details) { 229 fprintf(f, "flows %u/%u ", qopt->flows, qopt->divisor); 230 } 231 fprintf(f, "divisor %u ", qopt->divisor); 232 if (qopt->perturb_period) 233 fprintf(f, "perturb %dsec ", qopt->perturb_period); 234 if (qopt_ext && qopt_ext->qth_min) { 235 fprintf(f, "\n ewma %u ", qopt_ext->Wlog); 236 fprintf(f, "min %s max %s probability %g ", 237 sprint_size(qopt_ext->qth_min, b2), 238 sprint_size(qopt_ext->qth_max, b3), 239 qopt_ext->max_P / pow(2, 32)); 240 if (qopt_ext->flags & TC_RED_ECN) 241 fprintf(f, "ecn "); 242 if (show_stats) { 243 fprintf(f, "\n prob_mark %u prob_mark_head %u prob_drop %u", 244 qopt_ext->stats.prob_mark, 245 qopt_ext->stats.prob_mark_head, 246 qopt_ext->stats.prob_drop); 247 fprintf(f, "\n forced_mark %u forced_mark_head %u forced_drop %u", 248 qopt_ext->stats.forced_mark, 249 qopt_ext->stats.forced_mark_head, 250 qopt_ext->stats.forced_drop); 251 } 252 } 253 return 0; 254} 255 256static int sfq_print_xstats(struct qdisc_util *qu, FILE *f, 257 struct rtattr *xstats) 258{ 259 struct tc_sfq_xstats *st; 260 261 if (xstats == NULL) 262 return 0; 263 if (RTA_PAYLOAD(xstats) < sizeof(*st)) 264 return -1; 265 st = RTA_DATA(xstats); 266 267 fprintf(f, " allot %d ", st->allot); 268 fprintf(f, "\n"); 269 return 0; 270} 271 272struct qdisc_util sfq_qdisc_util = { 273 .id = "sfq", 274 .parse_qopt = sfq_parse_opt, 275 .print_qopt = sfq_print_opt, 276 .print_xstats = sfq_print_xstats, 277}; 278