osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * q_red.c RED. |
| 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> |
Stephen Hemminger | d798a04 | 2012-02-06 09:45:50 -0800 | [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" |
| 26 | |
| 27 | #include "tc_red.h" |
| 28 | |
| 29 | static void explain(void) |
| 30 | { |
Eric Dumazet | 841fc7b | 2011-12-09 00:11:40 +0100 | [diff] [blame] | 31 | fprintf(stderr, "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n"); |
David Ward | 7bf17a2 | 2015-05-18 11:35:08 -0400 | [diff] [blame] | 32 | fprintf(stderr, " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n"); |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 33 | fprintf(stderr, " [ecn] [harddrop]\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 34 | } |
| 35 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 36 | static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n) |
| 37 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 38 | struct tc_red_qopt opt = {}; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 39 | unsigned int burst = 0; |
| 40 | unsigned int avpkt = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 41 | double probability = 0.02; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 42 | unsigned int rate = 0; |
David Ward | 9d9a67c | 2015-05-18 11:35:05 -0400 | [diff] [blame] | 43 | int parm; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 44 | __u8 sbuf[256]; |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 45 | __u32 max_P; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 46 | struct rtattr *tail; |
| 47 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 48 | while (argc > 0) { |
| 49 | if (strcmp(*argv, "limit") == 0) { |
| 50 | NEXT_ARG(); |
| 51 | if (get_size(&opt.limit, *argv)) { |
| 52 | fprintf(stderr, "Illegal \"limit\"\n"); |
| 53 | return -1; |
| 54 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 55 | } else if (strcmp(*argv, "min") == 0) { |
| 56 | NEXT_ARG(); |
| 57 | if (get_size(&opt.qth_min, *argv)) { |
| 58 | fprintf(stderr, "Illegal \"min\"\n"); |
| 59 | return -1; |
| 60 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 61 | } else if (strcmp(*argv, "max") == 0) { |
| 62 | NEXT_ARG(); |
| 63 | if (get_size(&opt.qth_max, *argv)) { |
| 64 | fprintf(stderr, "Illegal \"max\"\n"); |
| 65 | return -1; |
| 66 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 67 | } else if (strcmp(*argv, "burst") == 0) { |
| 68 | NEXT_ARG(); |
| 69 | if (get_unsigned(&burst, *argv, 0)) { |
| 70 | fprintf(stderr, "Illegal \"burst\"\n"); |
| 71 | return -1; |
| 72 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 73 | } else if (strcmp(*argv, "avpkt") == 0) { |
| 74 | NEXT_ARG(); |
| 75 | if (get_size(&avpkt, *argv)) { |
| 76 | fprintf(stderr, "Illegal \"avpkt\"\n"); |
| 77 | return -1; |
| 78 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 79 | } else if (strcmp(*argv, "probability") == 0) { |
| 80 | NEXT_ARG(); |
| 81 | if (sscanf(*argv, "%lg", &probability) != 1) { |
| 82 | fprintf(stderr, "Illegal \"probability\"\n"); |
| 83 | return -1; |
| 84 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 85 | } else if (strcmp(*argv, "bandwidth") == 0) { |
| 86 | NEXT_ARG(); |
| 87 | if (get_rate(&rate, *argv)) { |
| 88 | fprintf(stderr, "Illegal \"bandwidth\"\n"); |
| 89 | return -1; |
| 90 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 91 | } else if (strcmp(*argv, "ecn") == 0) { |
Eric Dumazet | 841fc7b | 2011-12-09 00:11:40 +0100 | [diff] [blame] | 92 | opt.flags |= TC_RED_ECN; |
| 93 | } else if (strcmp(*argv, "harddrop") == 0) { |
| 94 | opt.flags |= TC_RED_HARDDROP; |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 95 | } else if (strcmp(*argv, "adaptative") == 0) { |
| 96 | opt.flags |= TC_RED_ADAPTATIVE; |
Eric Dumazet | 54a2fce | 2012-01-20 16:10:13 +0100 | [diff] [blame] | 97 | } else if (strcmp(*argv, "adaptive") == 0) { |
| 98 | opt.flags |= TC_RED_ADAPTATIVE; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 99 | } else if (strcmp(*argv, "help") == 0) { |
| 100 | explain(); |
| 101 | return -1; |
| 102 | } else { |
| 103 | fprintf(stderr, "What is \"%s\"?\n", *argv); |
| 104 | explain(); |
| 105 | return -1; |
| 106 | } |
| 107 | argc--; argv++; |
| 108 | } |
| 109 | |
Eric Dumazet | 841fc7b | 2011-12-09 00:11:40 +0100 | [diff] [blame] | 110 | if (!opt.limit || !avpkt) { |
| 111 | fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n"); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 112 | return -1; |
| 113 | } |
Eric Dumazet | 841fc7b | 2011-12-09 00:11:40 +0100 | [diff] [blame] | 114 | /* Compute default min/max thresholds based on |
| 115 | * Sally Floyd's recommendations: |
| 116 | * http://www.icir.org/floyd/REDparameters.txt |
| 117 | */ |
| 118 | if (!opt.qth_max) |
| 119 | opt.qth_max = opt.qth_min ? opt.qth_min * 3 : opt.limit / 4; |
| 120 | if (!opt.qth_min) |
| 121 | opt.qth_min = opt.qth_max / 3; |
| 122 | if (!burst) |
Eric Dumazet | ab15aea | 2011-12-01 15:25:59 +0100 | [diff] [blame] | 123 | burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt); |
David Ward | d93c909 | 2015-05-18 11:35:07 -0400 | [diff] [blame] | 124 | if (!rate) { |
| 125 | get_rate(&rate, "10Mbit"); |
| 126 | fprintf(stderr, "RED: set bandwidth to 10Mbit\n"); |
| 127 | } |
David Ward | 9d9a67c | 2015-05-18 11:35:05 -0400 | [diff] [blame] | 128 | if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 129 | fprintf(stderr, "RED: failed to calculate EWMA constant.\n"); |
| 130 | return -1; |
| 131 | } |
David Ward | 9d9a67c | 2015-05-18 11:35:05 -0400 | [diff] [blame] | 132 | if (parm >= 10) |
David Ward | 6c99695 | 2015-05-18 11:35:06 -0400 | [diff] [blame] | 133 | fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst); |
David Ward | 9d9a67c | 2015-05-18 11:35:05 -0400 | [diff] [blame] | 134 | opt.Wlog = parm; |
| 135 | if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 136 | fprintf(stderr, "RED: failed to calculate probability.\n"); |
| 137 | return -1; |
| 138 | } |
David Ward | 9d9a67c | 2015-05-18 11:35:05 -0400 | [diff] [blame] | 139 | opt.Plog = parm; |
| 140 | if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 141 | fprintf(stderr, "RED: failed to calculate idle damping table.\n"); |
| 142 | return -1; |
| 143 | } |
David Ward | 9d9a67c | 2015-05-18 11:35:05 -0400 | [diff] [blame] | 144 | opt.Scell_log = parm; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 145 | |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 146 | tail = NLMSG_TAIL(n); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 147 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); |
| 148 | addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt)); |
| 149 | addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256); |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 150 | max_P = probability * pow(2, 32); |
| 151 | addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P)); |
4!tgraf | 228569c | 2005-01-18 01:24:18 +0000 | [diff] [blame] | 152 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) |
| 157 | { |
Eric Dumazet | 841fc7b | 2011-12-09 00:11:40 +0100 | [diff] [blame] | 158 | struct rtattr *tb[TCA_RED_MAX + 1]; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 159 | struct tc_red_qopt *qopt; |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 160 | __u32 max_P = 0; |
Stephen Hemminger | 32a121c | 2016-03-21 11:48:36 -0700 | [diff] [blame] | 161 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 162 | SPRINT_BUF(b1); |
| 163 | SPRINT_BUF(b2); |
| 164 | SPRINT_BUF(b3); |
| 165 | |
| 166 | if (opt == NULL) |
| 167 | return 0; |
| 168 | |
Eric Dumazet | 841fc7b | 2011-12-09 00:11:40 +0100 | [diff] [blame] | 169 | parse_rtattr_nested(tb, TCA_RED_MAX, opt); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 170 | |
| 171 | if (tb[TCA_RED_PARMS] == NULL) |
| 172 | return -1; |
| 173 | qopt = RTA_DATA(tb[TCA_RED_PARMS]); |
| 174 | if (RTA_PAYLOAD(tb[TCA_RED_PARMS]) < sizeof(*qopt)) |
| 175 | return -1; |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 176 | |
| 177 | if (tb[TCA_RED_MAX_P] && |
| 178 | RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32)) |
Stephen Hemminger | ff24746 | 2012-04-10 08:47:55 -0700 | [diff] [blame] | 179 | max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]); |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 180 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 181 | fprintf(f, "limit %s min %s max %s ", |
| 182 | sprint_size(qopt->limit, b1), |
| 183 | sprint_size(qopt->qth_min, b2), |
| 184 | sprint_size(qopt->qth_max, b3)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 185 | if (qopt->flags & TC_RED_ECN) |
| 186 | fprintf(f, "ecn "); |
Eric Dumazet | 841fc7b | 2011-12-09 00:11:40 +0100 | [diff] [blame] | 187 | if (qopt->flags & TC_RED_HARDDROP) |
| 188 | fprintf(f, "harddrop "); |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 189 | if (qopt->flags & TC_RED_ADAPTATIVE) |
Eric Dumazet | 54a2fce | 2012-01-20 16:10:13 +0100 | [diff] [blame] | 190 | fprintf(f, "adaptive "); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 191 | if (show_details) { |
Eric Dumazet | e7e4abe | 2012-01-19 14:45:20 -0800 | [diff] [blame] | 192 | fprintf(f, "ewma %u ", qopt->Wlog); |
| 193 | if (max_P) |
| 194 | fprintf(f, "probability %lg ", max_P / pow(2, 32)); |
| 195 | else |
| 196 | fprintf(f, "Plog %u ", qopt->Plog); |
| 197 | fprintf(f, "Scell_log %u", qopt->Scell_log); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats) |
| 203 | { |
| 204 | #ifdef TC_RED_ECN |
| 205 | struct tc_red_xstats *st; |
| 206 | |
| 207 | if (xstats == NULL) |
| 208 | return 0; |
| 209 | |
| 210 | if (RTA_PAYLOAD(xstats) < sizeof(*st)) |
| 211 | return -1; |
| 212 | |
| 213 | st = RTA_DATA(xstats); |
| 214 | fprintf(f, " marked %u early %u pdrop %u other %u", |
| 215 | st->marked, st->early, st->pdrop, st->other); |
| 216 | return 0; |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 217 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | #endif |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | |
net[shemminger]!kaber | 95812b5 | 2004-09-28 18:35:49 +0000 | [diff] [blame] | 223 | struct qdisc_util red_qdisc_util = { |
osdl.net!shemminger | f2f99e2 | 2004-08-31 17:45:21 +0000 | [diff] [blame] | 224 | .id = "red", |
| 225 | .parse_qopt = red_parse_opt, |
| 226 | .print_qopt = red_print_opt, |
| 227 | .print_xstats = red_print_xstats, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 228 | }; |