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