blob: e015cbdaf2e80062f2934f3e4e9eac4d60a7d6a6 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 Hemmingerd798a042012-02-06 09:45:50 -080022#include <math.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000023
24#include "utils.h"
25#include "tc_util.h"
26
27#include "tc_red.h"
28
29static void explain(void)
30{
Eric Dumazet841fc7b2011-12-09 00:11:40 +010031 fprintf(stderr, "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n");
David Ward7bf17a22015-05-18 11:35:08 -040032 fprintf(stderr, " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n");
Eric Dumazete7e4abe2012-01-19 14:45:20 -080033 fprintf(stderr, " [ecn] [harddrop]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000034}
35
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
37{
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000038 struct tc_red_qopt opt;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070039 unsigned int burst = 0;
40 unsigned int avpkt = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000041 double probability = 0.02;
Stephen Hemminger32a121c2016-03-21 11:48:36 -070042 unsigned int rate = 0;
David Ward9d9a67c2015-05-18 11:35:05 -040043 int parm;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000044 __u8 sbuf[256];
Eric Dumazete7e4abe2012-01-19 14:45:20 -080045 __u32 max_P;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000046 struct rtattr *tail;
47
48 memset(&opt, 0, sizeof(opt));
49
50 while (argc > 0) {
51 if (strcmp(*argv, "limit") == 0) {
52 NEXT_ARG();
53 if (get_size(&opt.limit, *argv)) {
54 fprintf(stderr, "Illegal \"limit\"\n");
55 return -1;
56 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000057 } else if (strcmp(*argv, "min") == 0) {
58 NEXT_ARG();
59 if (get_size(&opt.qth_min, *argv)) {
60 fprintf(stderr, "Illegal \"min\"\n");
61 return -1;
62 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000063 } else if (strcmp(*argv, "max") == 0) {
64 NEXT_ARG();
65 if (get_size(&opt.qth_max, *argv)) {
66 fprintf(stderr, "Illegal \"max\"\n");
67 return -1;
68 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000069 } else if (strcmp(*argv, "burst") == 0) {
70 NEXT_ARG();
71 if (get_unsigned(&burst, *argv, 0)) {
72 fprintf(stderr, "Illegal \"burst\"\n");
73 return -1;
74 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000075 } else if (strcmp(*argv, "avpkt") == 0) {
76 NEXT_ARG();
77 if (get_size(&avpkt, *argv)) {
78 fprintf(stderr, "Illegal \"avpkt\"\n");
79 return -1;
80 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000081 } else if (strcmp(*argv, "probability") == 0) {
82 NEXT_ARG();
83 if (sscanf(*argv, "%lg", &probability) != 1) {
84 fprintf(stderr, "Illegal \"probability\"\n");
85 return -1;
86 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000087 } else if (strcmp(*argv, "bandwidth") == 0) {
88 NEXT_ARG();
89 if (get_rate(&rate, *argv)) {
90 fprintf(stderr, "Illegal \"bandwidth\"\n");
91 return -1;
92 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000093 } else if (strcmp(*argv, "ecn") == 0) {
Eric Dumazet841fc7b2011-12-09 00:11:40 +010094 opt.flags |= TC_RED_ECN;
95 } else if (strcmp(*argv, "harddrop") == 0) {
96 opt.flags |= TC_RED_HARDDROP;
Eric Dumazete7e4abe2012-01-19 14:45:20 -080097 } else if (strcmp(*argv, "adaptative") == 0) {
98 opt.flags |= TC_RED_ADAPTATIVE;
Eric Dumazet54a2fce2012-01-20 16:10:13 +010099 } else if (strcmp(*argv, "adaptive") == 0) {
100 opt.flags |= TC_RED_ADAPTATIVE;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101 } else if (strcmp(*argv, "help") == 0) {
102 explain();
103 return -1;
104 } else {
105 fprintf(stderr, "What is \"%s\"?\n", *argv);
106 explain();
107 return -1;
108 }
109 argc--; argv++;
110 }
111
Eric Dumazet841fc7b2011-12-09 00:11:40 +0100112 if (!opt.limit || !avpkt) {
113 fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000114 return -1;
115 }
Eric Dumazet841fc7b2011-12-09 00:11:40 +0100116 /* Compute default min/max thresholds based on
117 * Sally Floyd's recommendations:
118 * http://www.icir.org/floyd/REDparameters.txt
119 */
120 if (!opt.qth_max)
121 opt.qth_max = opt.qth_min ? opt.qth_min * 3 : opt.limit / 4;
122 if (!opt.qth_min)
123 opt.qth_min = opt.qth_max / 3;
124 if (!burst)
Eric Dumazetab15aea2011-12-01 15:25:59 +0100125 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
David Wardd93c9092015-05-18 11:35:07 -0400126 if (!rate) {
127 get_rate(&rate, "10Mbit");
128 fprintf(stderr, "RED: set bandwidth to 10Mbit\n");
129 }
David Ward9d9a67c2015-05-18 11:35:05 -0400130 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
132 return -1;
133 }
David Ward9d9a67c2015-05-18 11:35:05 -0400134 if (parm >= 10)
David Ward6c996952015-05-18 11:35:06 -0400135 fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst);
David Ward9d9a67c2015-05-18 11:35:05 -0400136 opt.Wlog = parm;
137 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000138 fprintf(stderr, "RED: failed to calculate probability.\n");
139 return -1;
140 }
David Ward9d9a67c2015-05-18 11:35:05 -0400141 opt.Plog = parm;
142 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000143 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
144 return -1;
145 }
David Ward9d9a67c2015-05-18 11:35:05 -0400146 opt.Scell_log = parm;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000147
4!tgraf228569c2005-01-18 01:24:18 +0000148 tail = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000149 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
150 addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
151 addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
Eric Dumazete7e4abe2012-01-19 14:45:20 -0800152 max_P = probability * pow(2, 32);
153 addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P));
4!tgraf228569c2005-01-18 01:24:18 +0000154 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000155 return 0;
156}
157
158static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
159{
Eric Dumazet841fc7b2011-12-09 00:11:40 +0100160 struct rtattr *tb[TCA_RED_MAX + 1];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000161 struct tc_red_qopt *qopt;
Eric Dumazete7e4abe2012-01-19 14:45:20 -0800162 __u32 max_P = 0;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700163
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000164 SPRINT_BUF(b1);
165 SPRINT_BUF(b2);
166 SPRINT_BUF(b3);
167
168 if (opt == NULL)
169 return 0;
170
Eric Dumazet841fc7b2011-12-09 00:11:40 +0100171 parse_rtattr_nested(tb, TCA_RED_MAX, opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000172
173 if (tb[TCA_RED_PARMS] == NULL)
174 return -1;
175 qopt = RTA_DATA(tb[TCA_RED_PARMS]);
176 if (RTA_PAYLOAD(tb[TCA_RED_PARMS]) < sizeof(*qopt))
177 return -1;
Eric Dumazete7e4abe2012-01-19 14:45:20 -0800178
179 if (tb[TCA_RED_MAX_P] &&
180 RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
Stephen Hemmingerff247462012-04-10 08:47:55 -0700181 max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]);
Eric Dumazete7e4abe2012-01-19 14:45:20 -0800182
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000183 fprintf(f, "limit %s min %s max %s ",
184 sprint_size(qopt->limit, b1),
185 sprint_size(qopt->qth_min, b2),
186 sprint_size(qopt->qth_max, b3));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000187 if (qopt->flags & TC_RED_ECN)
188 fprintf(f, "ecn ");
Eric Dumazet841fc7b2011-12-09 00:11:40 +0100189 if (qopt->flags & TC_RED_HARDDROP)
190 fprintf(f, "harddrop ");
Eric Dumazete7e4abe2012-01-19 14:45:20 -0800191 if (qopt->flags & TC_RED_ADAPTATIVE)
Eric Dumazet54a2fce2012-01-20 16:10:13 +0100192 fprintf(f, "adaptive ");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000193 if (show_details) {
Eric Dumazete7e4abe2012-01-19 14:45:20 -0800194 fprintf(f, "ewma %u ", qopt->Wlog);
195 if (max_P)
196 fprintf(f, "probability %lg ", max_P / pow(2, 32));
197 else
198 fprintf(f, "Plog %u ", qopt->Plog);
199 fprintf(f, "Scell_log %u", qopt->Scell_log);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000200 }
201 return 0;
202}
203
204static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
205{
206#ifdef TC_RED_ECN
207 struct tc_red_xstats *st;
208
209 if (xstats == NULL)
210 return 0;
211
212 if (RTA_PAYLOAD(xstats) < sizeof(*st))
213 return -1;
214
215 st = RTA_DATA(xstats);
216 fprintf(f, " marked %u early %u pdrop %u other %u",
217 st->marked, st->early, st->pdrop, st->other);
218 return 0;
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800219
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000220#endif
221 return 0;
222}
223
224
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000225struct qdisc_util red_qdisc_util = {
osdl.net!shemmingerf2f99e22004-08-31 17:45:21 +0000226 .id = "red",
227 .parse_qopt = red_parse_opt,
228 .print_qopt = red_print_opt,
229 .print_xstats = red_print_xstats,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000230};