blob: 0a9894966c82d529ea14a45a10d1cc35e2f39ca0 [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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 *
Stephen Hemmingerae665a52006-12-05 10:10:22 -08009 * Authors: J Hadi Salim(hadi@nortelnetworks.com)
10 * code ruthlessly ripped from
11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000012 *
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>
Eric Dumazet1b6f0bb2012-01-20 16:27:50 +010024#include <math.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000025
26#include "utils.h"
27#include "tc_util.h"
28
29#include "tc_red.h"
30
31
32#if 0
Stephen Hemminger32a121c2016-03-21 11:48:36 -070033#define DPRINTF(format, args...) fprintf(stderr, format, ##args)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000034#else
Stephen Hemminger32a121c2016-03-21 11:48:36 -070035#define DPRINTF(format, args...)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000036#endif
37
38static void explain(void)
39{
David Ward357c45a2015-05-18 11:35:13 -040040 fprintf(stderr, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
David Wardaacee262015-05-18 11:35:14 -040041 fprintf(stderr, " default DEFAULT_VQ [ grio ] [ limit BYTES ]\n");
David Ward357c45a2015-05-18 11:35:13 -040042 fprintf(stderr, " tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n");
43 fprintf(stderr, " min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n");
44 fprintf(stderr, " [ probability PROBABILITY ] [ bandwidth KBPS ]\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000045}
46
Stephen Hemminger3d0b7432014-12-20 15:47:17 -080047static int init_gred(struct qdisc_util *qu, int argc, char **argv,
Stephen Hemmingerebde8782009-05-26 15:15:01 -070048 struct nlmsghdr *n)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000049{
50
51 struct rtattr *tail;
Stephen Hemmingercb4bd0e2010-08-25 09:04:55 -070052 struct tc_gred_sopt opt = { 0 };
David Wardaacee262015-05-18 11:35:14 -040053 __u32 limit = 0;
David Wardeb6d7d62015-05-18 11:35:12 -040054
55 opt.def_DP = MAX_DPs;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000056
57 while (argc > 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070058 DPRINTF(stderr, "init_gred: invoked with %s\n", *argv);
David Ward357c45a2015-05-18 11:35:13 -040059 if (strcmp(*argv, "vqs") == 0 ||
60 strcmp(*argv, "DPs") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000061 NEXT_ARG();
David Wardeb6d7d62015-05-18 11:35:12 -040062 if (get_unsigned(&opt.DPs, *argv, 10)) {
David Ward357c45a2015-05-18 11:35:13 -040063 fprintf(stderr, "Illegal \"vqs\"\n");
David Wardeb6d7d62015-05-18 11:35:12 -040064 return -1;
65 } else if (opt.DPs > MAX_DPs) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070066 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
67 MAX_DPs);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000068 return -1;
69 }
70 } else if (strcmp(*argv, "default") == 0) {
David Wardeb6d7d62015-05-18 11:35:12 -040071 if (opt.DPs == 0) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070072 fprintf(stderr, "\"default\" must be defined after \"vqs\"\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000073 return -1;
74 }
David Wardeb6d7d62015-05-18 11:35:12 -040075 NEXT_ARG();
76 if (get_unsigned(&opt.def_DP, *argv, 10)) {
77 fprintf(stderr, "Illegal \"default\"\n");
78 return -1;
79 } else if (opt.def_DP >= opt.DPs) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -070080 fprintf(stderr, "\"default\" must be less than \"vqs\"\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000081 return -1;
82 }
83 } else if (strcmp(*argv, "grio") == 0) {
Stephen Hemmingercb4bd0e2010-08-25 09:04:55 -070084 opt.grio = 1;
David Wardaacee262015-05-18 11:35:14 -040085 } else if (strcmp(*argv, "limit") == 0) {
86 NEXT_ARG();
87 if (get_size(&limit, *argv)) {
88 fprintf(stderr, "Illegal \"limit\"\n");
89 return -1;
90 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000091 } else if (strcmp(*argv, "help") == 0) {
92 explain();
93 return -1;
94 } else {
95 fprintf(stderr, "What is \"%s\"?\n", *argv);
96 explain();
97 return -1;
98 }
99 argc--; argv++;
Stephen Hemmingerebde8782009-05-26 15:15:01 -0700100 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000101
David Wardeb6d7d62015-05-18 11:35:12 -0400102 if (!opt.DPs || opt.def_DP == MAX_DPs) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700103 fprintf(stderr, "Illegal gred setup parameters\n");
Stephen Hemmingerebde8782009-05-26 15:15:01 -0700104 return -1;
105 }
106
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700107 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt.DPs, opt.def_DP);
108 n->nlmsg_flags |= NLM_F_CREATE;
4!tgraf228569c2005-01-18 01:24:18 +0000109 tail = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000110 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
111 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
David Wardaacee262015-05-18 11:35:14 -0400112 if (limit)
113 addattr32(n, 1024, TCA_GRED_LIMIT, limit);
4!tgraf228569c2005-01-18 01:24:18 +0000114 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
Stephen Hemmingerebde8782009-05-26 15:15:01 -0700115 return 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000116}
117/*
118^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119*/
120static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
121{
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700122 int ok = 0;
David Wardeb6d7d62015-05-18 11:35:12 -0400123 struct tc_gred_qopt opt = { 0 };
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700124 unsigned int burst = 0;
125 unsigned int avpkt = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000126 double probability = 0.02;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700127 unsigned int rate = 0;
David Ward9d9a67c2015-05-18 11:35:05 -0400128 int parm;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000129 __u8 sbuf[256];
130 struct rtattr *tail;
Eric Dumazet1b6f0bb2012-01-20 16:27:50 +0100131 __u32 max_P;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000132
David Wardeb6d7d62015-05-18 11:35:12 -0400133 opt.DP = MAX_DPs;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000134
135 while (argc > 0) {
136 if (strcmp(*argv, "limit") == 0) {
137 NEXT_ARG();
138 if (get_size(&opt.limit, *argv)) {
139 fprintf(stderr, "Illegal \"limit\"\n");
140 return -1;
141 }
142 ok++;
143 } else if (strcmp(*argv, "setup") == 0) {
144 if (ok) {
145 fprintf(stderr, "Illegal \"setup\"\n");
146 return -1;
147 }
David Wardd73e0402015-05-18 11:35:09 -0400148 return init_gred(qu, argc-1, argv+1, n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000149 } else if (strcmp(*argv, "min") == 0) {
150 NEXT_ARG();
151 if (get_size(&opt.qth_min, *argv)) {
152 fprintf(stderr, "Illegal \"min\"\n");
153 return -1;
154 }
155 ok++;
156 } else if (strcmp(*argv, "max") == 0) {
157 NEXT_ARG();
158 if (get_size(&opt.qth_max, *argv)) {
159 fprintf(stderr, "Illegal \"max\"\n");
160 return -1;
161 }
162 ok++;
David Ward357c45a2015-05-18 11:35:13 -0400163 } else if (strcmp(*argv, "vq") == 0 ||
164 strcmp(*argv, "DP") == 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000165 NEXT_ARG();
David Wardeb6d7d62015-05-18 11:35:12 -0400166 if (get_unsigned(&opt.DP, *argv, 10)) {
David Ward357c45a2015-05-18 11:35:13 -0400167 fprintf(stderr, "Illegal \"vq\"\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000168 return -1;
David Wardeb6d7d62015-05-18 11:35:12 -0400169 } else if (opt.DP >= MAX_DPs) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700170 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
171 MAX_DPs);
David Wardeb6d7d62015-05-18 11:35:12 -0400172 return -1;
173 } /* need a better error check */
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000174 ok++;
175 } else if (strcmp(*argv, "burst") == 0) {
176 NEXT_ARG();
David Wardd73e0402015-05-18 11:35:09 -0400177 if (get_unsigned(&burst, *argv, 0)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000178 fprintf(stderr, "Illegal \"burst\"\n");
179 return -1;
180 }
181 ok++;
182 } else if (strcmp(*argv, "avpkt") == 0) {
183 NEXT_ARG();
184 if (get_size(&avpkt, *argv)) {
185 fprintf(stderr, "Illegal \"avpkt\"\n");
186 return -1;
187 }
188 ok++;
189 } else if (strcmp(*argv, "probability") == 0) {
190 NEXT_ARG();
191 if (sscanf(*argv, "%lg", &probability) != 1) {
192 fprintf(stderr, "Illegal \"probability\"\n");
193 return -1;
194 }
195 ok++;
196 } else if (strcmp(*argv, "prio") == 0) {
197 NEXT_ARG();
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700198 opt.prio = strtol(*argv, (char **)NULL, 10);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000199 /* some error check here */
200 ok++;
201 } else if (strcmp(*argv, "bandwidth") == 0) {
202 NEXT_ARG();
203 if (get_rate(&rate, *argv)) {
204 fprintf(stderr, "Illegal \"bandwidth\"\n");
205 return -1;
206 }
207 ok++;
208 } else if (strcmp(*argv, "help") == 0) {
209 explain();
210 return -1;
211 } else {
212 fprintf(stderr, "What is \"%s\"?\n", *argv);
213 explain();
214 return -1;
215 }
216 argc--; argv++;
217 }
218
David Warda77905e2015-05-18 11:35:10 -0400219 if (!ok) {
220 explain();
221 return -1;
222 }
David Wardeb6d7d62015-05-18 11:35:12 -0400223 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
224 !avpkt) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700225 fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000226 return -1;
227 }
Eric Dumazetab15aea2011-12-01 15:25:59 +0100228 if (!burst) {
229 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
230 fprintf(stderr, "GRED: set burst to %u\n", burst);
231 }
David Wardd93c9092015-05-18 11:35:07 -0400232 if (!rate) {
233 get_rate(&rate, "10Mbit");
234 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
235 }
David Ward9d9a67c2015-05-18 11:35:05 -0400236 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000237 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
238 return -1;
239 }
David Ward9d9a67c2015-05-18 11:35:05 -0400240 if (parm >= 10)
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700241 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
242 burst);
David Ward9d9a67c2015-05-18 11:35:05 -0400243 opt.Wlog = parm;
244 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000245 fprintf(stderr, "GRED: failed to calculate probability.\n");
246 return -1;
247 }
David Ward9d9a67c2015-05-18 11:35:05 -0400248 opt.Plog = parm;
249 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000250 {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700251 fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000252 return -1;
253 }
David Ward9d9a67c2015-05-18 11:35:05 -0400254 opt.Scell_log = parm;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000255
4!tgraf228569c2005-01-18 01:24:18 +0000256 tail = NLMSG_TAIL(n);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000257 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
258 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
259 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
Eric Dumazet1b6f0bb2012-01-20 16:27:50 +0100260 max_P = probability * pow(2, 32);
261 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
4!tgraf228569c2005-01-18 01:24:18 +0000262 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000263 return 0;
264}
265
266static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
267{
Eric Dumazet1b6f0bb2012-01-20 16:27:50 +0100268 struct rtattr *tb[TCA_GRED_MAX + 1];
David Ward1693a4d2015-05-18 11:35:11 -0400269 struct tc_gred_sopt *sopt;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000270 struct tc_gred_qopt *qopt;
Eric Dumazet1b6f0bb2012-01-20 16:27:50 +0100271 __u32 *max_p = NULL;
David Wardaacee262015-05-18 11:35:14 -0400272 __u32 *limit = NULL;
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700273 unsigned int i;
274
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000275 SPRINT_BUF(b1);
276 SPRINT_BUF(b2);
277 SPRINT_BUF(b3);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000278
279 if (opt == NULL)
280 return 0;
281
Eric Dumazet1b6f0bb2012-01-20 16:27:50 +0100282 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000283
284 if (tb[TCA_GRED_PARMS] == NULL)
285 return -1;
osdl.net!shemmingera5a6f1e2005-03-10 20:26:08 +0000286
Eric Dumazet1b6f0bb2012-01-20 16:27:50 +0100287 if (tb[TCA_GRED_MAX_P] &&
288 RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
289 max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
290
David Wardaacee262015-05-18 11:35:14 -0400291 if (tb[TCA_GRED_LIMIT] &&
292 RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
293 limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
294
David Ward1693a4d2015-05-18 11:35:11 -0400295 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000296 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
David Ward1693a4d2015-05-18 11:35:11 -0400297 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
298 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700299 fprintf(f, "\n GRED received message smaller than expected\n");
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000300 return -1;
David Ward1693a4d2015-05-18 11:35:11 -0400301 }
Stephen Hemmingerae665a52006-12-05 10:10:22 -0800302
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000303/* Bad hack! should really return a proper message as shown above*/
304
David Ward357c45a2015-05-18 11:35:13 -0400305 fprintf(f, "vqs %u default %u %s",
David Ward1693a4d2015-05-18 11:35:11 -0400306 sopt->DPs,
307 sopt->def_DP,
308 sopt->grio ? "grio " : "");
309
David Wardaacee262015-05-18 11:35:14 -0400310 if (limit)
311 fprintf(f, "limit %s ",
312 sprint_size(*limit, b1));
313
Stephen Hemminger32a121c2016-03-21 11:48:36 -0700314 for (i = 0; i < MAX_DPs; i++, qopt++) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000315 if (qopt->DP >= MAX_DPs) continue;
David Ward357c45a2015-05-18 11:35:13 -0400316 fprintf(f, "\n vq %u prio %hhu limit %s min %s max %s ",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000317 qopt->DP,
318 qopt->prio,
David Ward1693a4d2015-05-18 11:35:11 -0400319 sprint_size(qopt->limit, b1),
320 sprint_size(qopt->qth_min, b2),
321 sprint_size(qopt->qth_max, b3));
322 if (show_details) {
323 fprintf(f, "ewma %u ", qopt->Wlog);
324 if (max_p)
325 fprintf(f, "probability %lg ", max_p[i] / pow(2, 32));
326 else
327 fprintf(f, "Plog %u ", qopt->Plog);
328 fprintf(f, "Scell_log %u ", qopt->Scell_log);
329 }
330 if (show_stats) {
331 fprintf(f, "\n Queue size: average %s current %s ",
332 sprint_size(qopt->qave, b1),
333 sprint_size(qopt->backlog, b2));
334 fprintf(f, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
335 qopt->forced,
336 qopt->early,
337 qopt->pdrop,
338 qopt->other);
339 fprintf(f, "\n Total packets: %u (%s) ",
340 qopt->packets,
341 sprint_size(qopt->bytesin, b1));
342 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000343 }
344 return 0;
345}
346
net[shemminger]!kaber95812b52004-09-28 18:35:49 +0000347struct qdisc_util gred_qdisc_util = {
osdl.net!shemmingerf2f99e22004-08-31 17:45:21 +0000348 .id = "gred",
349 .parse_qopt = gred_parse_opt,
350 .print_qopt = gred_print_opt,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000351};