]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_gred.c
tc: updated ife man page.
[mirror_iproute2.git] / tc / q_gred.c
CommitLineData
aba5acdf
SH
1/*
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 *
ae665a52
SH
9 * Authors: J Hadi Salim(hadi@nortelnetworks.com)
10 * code ruthlessly ripped from
11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
aba5acdf
SH
12 *
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>
1b6f0bb5 24#include <math.h>
aba5acdf
SH
25
26#include "utils.h"
27#include "tc_util.h"
28
29#include "tc_red.h"
30
31
32#if 0
32a121cb 33#define DPRINTF(format, args...) fprintf(stderr, format, ##args)
aba5acdf 34#else
32a121cb 35#define DPRINTF(format, args...)
aba5acdf
SH
36#endif
37
38static void explain(void)
39{
357c45ad 40 fprintf(stderr, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
aacee269 41 fprintf(stderr, " default DEFAULT_VQ [ grio ] [ limit BYTES ]\n");
357c45ad
DW
42 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");
aba5acdf
SH
45}
46
3d0b7439 47static int init_gred(struct qdisc_util *qu, int argc, char **argv,
ebde8780 48 struct nlmsghdr *n)
aba5acdf
SH
49{
50
51 struct rtattr *tail;
cb4bd0ec 52 struct tc_gred_sopt opt = { 0 };
aacee269 53 __u32 limit = 0;
eb6d7d6a
DW
54
55 opt.def_DP = MAX_DPs;
aba5acdf
SH
56
57 while (argc > 0) {
32a121cb 58 DPRINTF(stderr, "init_gred: invoked with %s\n", *argv);
357c45ad
DW
59 if (strcmp(*argv, "vqs") == 0 ||
60 strcmp(*argv, "DPs") == 0) {
aba5acdf 61 NEXT_ARG();
eb6d7d6a 62 if (get_unsigned(&opt.DPs, *argv, 10)) {
357c45ad 63 fprintf(stderr, "Illegal \"vqs\"\n");
eb6d7d6a
DW
64 return -1;
65 } else if (opt.DPs > MAX_DPs) {
32a121cb
SH
66 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
67 MAX_DPs);
aba5acdf
SH
68 return -1;
69 }
70 } else if (strcmp(*argv, "default") == 0) {
eb6d7d6a 71 if (opt.DPs == 0) {
32a121cb 72 fprintf(stderr, "\"default\" must be defined after \"vqs\"\n");
aba5acdf
SH
73 return -1;
74 }
eb6d7d6a
DW
75 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) {
32a121cb 80 fprintf(stderr, "\"default\" must be less than \"vqs\"\n");
aba5acdf
SH
81 return -1;
82 }
83 } else if (strcmp(*argv, "grio") == 0) {
cb4bd0ec 84 opt.grio = 1;
aacee269
DW
85 } 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 }
aba5acdf
SH
91 } 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++;
ebde8780 100 }
aba5acdf 101
eb6d7d6a 102 if (!opt.DPs || opt.def_DP == MAX_DPs) {
32a121cb 103 fprintf(stderr, "Illegal gred setup parameters\n");
ebde8780
SH
104 return -1;
105 }
106
32a121cb
SH
107 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt.DPs, opt.def_DP);
108 n->nlmsg_flags |= NLM_F_CREATE;
228569c3 109 tail = NLMSG_TAIL(n);
aba5acdf
SH
110 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
111 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
aacee269
DW
112 if (limit)
113 addattr32(n, 1024, TCA_GRED_LIMIT, limit);
228569c3 114 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
ebde8780 115 return 0;
aba5acdf
SH
116}
117/*
118^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119*/
120static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
121{
32a121cb 122 int ok = 0;
eb6d7d6a 123 struct tc_gred_qopt opt = { 0 };
32a121cb
SH
124 unsigned int burst = 0;
125 unsigned int avpkt = 0;
aba5acdf 126 double probability = 0.02;
32a121cb 127 unsigned int rate = 0;
9d9a67c7 128 int parm;
aba5acdf
SH
129 __u8 sbuf[256];
130 struct rtattr *tail;
1b6f0bb5 131 __u32 max_P;
aba5acdf 132
eb6d7d6a 133 opt.DP = MAX_DPs;
aba5acdf
SH
134
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 }
d73e0408 148 return init_gred(qu, argc-1, argv+1, n);
aba5acdf
SH
149 } 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++;
357c45ad
DW
163 } else if (strcmp(*argv, "vq") == 0 ||
164 strcmp(*argv, "DP") == 0) {
aba5acdf 165 NEXT_ARG();
eb6d7d6a 166 if (get_unsigned(&opt.DP, *argv, 10)) {
357c45ad 167 fprintf(stderr, "Illegal \"vq\"\n");
aba5acdf 168 return -1;
eb6d7d6a 169 } else if (opt.DP >= MAX_DPs) {
32a121cb
SH
170 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
171 MAX_DPs);
eb6d7d6a
DW
172 return -1;
173 } /* need a better error check */
aba5acdf
SH
174 ok++;
175 } else if (strcmp(*argv, "burst") == 0) {
176 NEXT_ARG();
d73e0408 177 if (get_unsigned(&burst, *argv, 0)) {
aba5acdf
SH
178 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();
32a121cb 198 opt.prio = strtol(*argv, (char **)NULL, 10);
aba5acdf
SH
199 /* 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
a77905ef
DW
219 if (!ok) {
220 explain();
221 return -1;
222 }
eb6d7d6a
DW
223 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
224 !avpkt) {
32a121cb 225 fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
aba5acdf
SH
226 return -1;
227 }
ab15aeac
ED
228 if (!burst) {
229 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
230 fprintf(stderr, "GRED: set burst to %u\n", burst);
231 }
d93c909a
DW
232 if (!rate) {
233 get_rate(&rate, "10Mbit");
234 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
235 }
9d9a67c7 236 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
aba5acdf
SH
237 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
238 return -1;
239 }
9d9a67c7 240 if (parm >= 10)
32a121cb
SH
241 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
242 burst);
9d9a67c7
DW
243 opt.Wlog = parm;
244 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
aba5acdf
SH
245 fprintf(stderr, "GRED: failed to calculate probability.\n");
246 return -1;
247 }
9d9a67c7
DW
248 opt.Plog = parm;
249 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
aba5acdf 250 {
32a121cb 251 fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
aba5acdf
SH
252 return -1;
253 }
9d9a67c7 254 opt.Scell_log = parm;
aba5acdf 255
228569c3 256 tail = NLMSG_TAIL(n);
aba5acdf
SH
257 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);
1b6f0bb5
ED
260 max_P = probability * pow(2, 32);
261 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
228569c3 262 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
aba5acdf
SH
263 return 0;
264}
265
266static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
267{
1b6f0bb5 268 struct rtattr *tb[TCA_GRED_MAX + 1];
1693a4d3 269 struct tc_gred_sopt *sopt;
aba5acdf 270 struct tc_gred_qopt *qopt;
1b6f0bb5 271 __u32 *max_p = NULL;
aacee269 272 __u32 *limit = NULL;
32a121cb
SH
273 unsigned int i;
274
aba5acdf
SH
275 SPRINT_BUF(b1);
276 SPRINT_BUF(b2);
277 SPRINT_BUF(b3);
aba5acdf
SH
278
279 if (opt == NULL)
280 return 0;
281
1b6f0bb5 282 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
aba5acdf
SH
283
284 if (tb[TCA_GRED_PARMS] == NULL)
285 return -1;
a5a6f1e8 286
1b6f0bb5
ED
287 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
aacee269
DW
291 if (tb[TCA_GRED_LIMIT] &&
292 RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
293 limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
294
1693a4d3 295 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
aba5acdf 296 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
1693a4d3
DW
297 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
298 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
32a121cb 299 fprintf(f, "\n GRED received message smaller than expected\n");
aba5acdf 300 return -1;
1693a4d3 301 }
ae665a52 302
aba5acdf
SH
303/* Bad hack! should really return a proper message as shown above*/
304
357c45ad 305 fprintf(f, "vqs %u default %u %s",
1693a4d3
DW
306 sopt->DPs,
307 sopt->def_DP,
308 sopt->grio ? "grio " : "");
309
aacee269
DW
310 if (limit)
311 fprintf(f, "limit %s ",
312 sprint_size(*limit, b1));
313
32a121cb 314 for (i = 0; i < MAX_DPs; i++, qopt++) {
aba5acdf 315 if (qopt->DP >= MAX_DPs) continue;
357c45ad 316 fprintf(f, "\n vq %u prio %hhu limit %s min %s max %s ",
aba5acdf
SH
317 qopt->DP,
318 qopt->prio,
1693a4d3
DW
319 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 }
aba5acdf
SH
343 }
344 return 0;
345}
346
95812b56 347struct qdisc_util gred_qdisc_util = {
f2f99e2e
SH
348 .id = "gred",
349 .parse_qopt = gred_parse_opt,
350 .print_qopt = gred_print_opt,
aba5acdf 351};