]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_gred.c
xfrmmonitor: allows to monitor in several netns
[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
33#define DPRINTF(format,args...) fprintf(stderr,format,##args)
34#else
35#define DPRINTF(format,args...)
36#endif
37
38static void explain(void)
39{
357c45ad
DW
40 fprintf(stderr, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
41 fprintf(stderr, " default DEFAULT_VQ [ grio ]\n");
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 };
eb6d7d6a
DW
53
54 opt.def_DP = MAX_DPs;
aba5acdf
SH
55
56 while (argc > 0) {
57 DPRINTF(stderr,"init_gred: invoked with %s\n",*argv);
357c45ad
DW
58 if (strcmp(*argv, "vqs") == 0 ||
59 strcmp(*argv, "DPs") == 0) {
aba5acdf 60 NEXT_ARG();
eb6d7d6a 61 if (get_unsigned(&opt.DPs, *argv, 10)) {
357c45ad 62 fprintf(stderr, "Illegal \"vqs\"\n");
eb6d7d6a
DW
63 return -1;
64 } else if (opt.DPs > MAX_DPs) {
357c45ad 65 fprintf(stderr, "GRED: only %u VQs are "
eb6d7d6a 66 "currently supported\n", MAX_DPs);
aba5acdf
SH
67 return -1;
68 }
69 } else if (strcmp(*argv, "default") == 0) {
eb6d7d6a
DW
70 if (opt.DPs == 0) {
71 fprintf(stderr, "\"default\" must be defined "
357c45ad 72 "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) {
80 fprintf(stderr, "\"default\" must be less than "
357c45ad 81 "\"vqs\"\n");
aba5acdf
SH
82 return -1;
83 }
84 } else if (strcmp(*argv, "grio") == 0) {
cb4bd0ec 85 opt.grio = 1;
aba5acdf
SH
86 } else if (strcmp(*argv, "help") == 0) {
87 explain();
88 return -1;
89 } else {
90 fprintf(stderr, "What is \"%s\"?\n", *argv);
91 explain();
92 return -1;
93 }
94 argc--; argv++;
ebde8780 95 }
aba5acdf 96
eb6d7d6a 97 if (!opt.DPs || opt.def_DP == MAX_DPs) {
ebde8780
SH
98 fprintf(stderr, "Illegal gred setup parameters \n");
99 return -1;
100 }
101
eb6d7d6a 102 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n",opt.DPs,opt.def_DP);
aba5acdf 103 n->nlmsg_flags|=NLM_F_CREATE;
228569c3 104 tail = NLMSG_TAIL(n);
aba5acdf
SH
105 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
106 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
228569c3 107 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
ebde8780 108 return 0;
aba5acdf
SH
109}
110/*
111^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112*/
113static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
114{
115 int ok=0;
eb6d7d6a 116 struct tc_gred_qopt opt = { 0 };
aba5acdf
SH
117 unsigned burst = 0;
118 unsigned avpkt = 0;
119 double probability = 0.02;
120 unsigned rate = 0;
9d9a67c7 121 int parm;
aba5acdf
SH
122 __u8 sbuf[256];
123 struct rtattr *tail;
1b6f0bb5 124 __u32 max_P;
aba5acdf 125
eb6d7d6a 126 opt.DP = MAX_DPs;
aba5acdf
SH
127
128 while (argc > 0) {
129 if (strcmp(*argv, "limit") == 0) {
130 NEXT_ARG();
131 if (get_size(&opt.limit, *argv)) {
132 fprintf(stderr, "Illegal \"limit\"\n");
133 return -1;
134 }
135 ok++;
136 } else if (strcmp(*argv, "setup") == 0) {
137 if (ok) {
138 fprintf(stderr, "Illegal \"setup\"\n");
139 return -1;
140 }
d73e0408 141 return init_gred(qu, argc-1, argv+1, n);
aba5acdf
SH
142 } else if (strcmp(*argv, "min") == 0) {
143 NEXT_ARG();
144 if (get_size(&opt.qth_min, *argv)) {
145 fprintf(stderr, "Illegal \"min\"\n");
146 return -1;
147 }
148 ok++;
149 } else if (strcmp(*argv, "max") == 0) {
150 NEXT_ARG();
151 if (get_size(&opt.qth_max, *argv)) {
152 fprintf(stderr, "Illegal \"max\"\n");
153 return -1;
154 }
155 ok++;
357c45ad
DW
156 } else if (strcmp(*argv, "vq") == 0 ||
157 strcmp(*argv, "DP") == 0) {
aba5acdf 158 NEXT_ARG();
eb6d7d6a 159 if (get_unsigned(&opt.DP, *argv, 10)) {
357c45ad 160 fprintf(stderr, "Illegal \"vq\"\n");
aba5acdf 161 return -1;
eb6d7d6a 162 } else if (opt.DP >= MAX_DPs) {
357c45ad 163 fprintf(stderr, "GRED: only %u VQs are "
eb6d7d6a
DW
164 "currently supported\n", MAX_DPs);
165 return -1;
166 } /* need a better error check */
aba5acdf
SH
167 ok++;
168 } else if (strcmp(*argv, "burst") == 0) {
169 NEXT_ARG();
d73e0408 170 if (get_unsigned(&burst, *argv, 0)) {
aba5acdf
SH
171 fprintf(stderr, "Illegal \"burst\"\n");
172 return -1;
173 }
174 ok++;
175 } else if (strcmp(*argv, "avpkt") == 0) {
176 NEXT_ARG();
177 if (get_size(&avpkt, *argv)) {
178 fprintf(stderr, "Illegal \"avpkt\"\n");
179 return -1;
180 }
181 ok++;
182 } else if (strcmp(*argv, "probability") == 0) {
183 NEXT_ARG();
184 if (sscanf(*argv, "%lg", &probability) != 1) {
185 fprintf(stderr, "Illegal \"probability\"\n");
186 return -1;
187 }
188 ok++;
189 } else if (strcmp(*argv, "prio") == 0) {
190 NEXT_ARG();
191 opt.prio=strtol(*argv, (char **)NULL, 10);
192 /* some error check here */
193 ok++;
194 } else if (strcmp(*argv, "bandwidth") == 0) {
195 NEXT_ARG();
196 if (get_rate(&rate, *argv)) {
197 fprintf(stderr, "Illegal \"bandwidth\"\n");
198 return -1;
199 }
200 ok++;
201 } else if (strcmp(*argv, "help") == 0) {
202 explain();
203 return -1;
204 } else {
205 fprintf(stderr, "What is \"%s\"?\n", *argv);
206 explain();
207 return -1;
208 }
209 argc--; argv++;
210 }
211
a77905ef
DW
212 if (!ok) {
213 explain();
214 return -1;
215 }
eb6d7d6a
DW
216 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
217 !avpkt) {
357c45ad 218 fprintf(stderr, "Required parameter (vq, limit, min, max, "
eb6d7d6a 219 "avpkt) is missing\n");
aba5acdf
SH
220 return -1;
221 }
ab15aeac
ED
222 if (!burst) {
223 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
224 fprintf(stderr, "GRED: set burst to %u\n", burst);
225 }
d93c909a
DW
226 if (!rate) {
227 get_rate(&rate, "10Mbit");
228 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
229 }
9d9a67c7 230 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
aba5acdf
SH
231 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
232 return -1;
233 }
9d9a67c7 234 if (parm >= 10)
6c99695d 235 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too "
aba5acdf 236 "large.\n", burst);
9d9a67c7
DW
237 opt.Wlog = parm;
238 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
aba5acdf
SH
239 fprintf(stderr, "GRED: failed to calculate probability.\n");
240 return -1;
241 }
9d9a67c7
DW
242 opt.Plog = parm;
243 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
aba5acdf
SH
244 {
245 fprintf(stderr, "GRED: failed to calculate idle damping "
246 "table.\n");
247 return -1;
248 }
9d9a67c7 249 opt.Scell_log = parm;
aba5acdf 250
228569c3 251 tail = NLMSG_TAIL(n);
aba5acdf
SH
252 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
253 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
254 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
1b6f0bb5
ED
255 max_P = probability * pow(2, 32);
256 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
228569c3 257 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
aba5acdf
SH
258 return 0;
259}
260
261static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
262{
1b6f0bb5 263 struct rtattr *tb[TCA_GRED_MAX + 1];
1693a4d3 264 struct tc_gred_sopt *sopt;
aba5acdf 265 struct tc_gred_qopt *qopt;
1b6f0bb5 266 __u32 *max_p = NULL;
1693a4d3 267 unsigned i;
aba5acdf
SH
268 SPRINT_BUF(b1);
269 SPRINT_BUF(b2);
270 SPRINT_BUF(b3);
aba5acdf
SH
271
272 if (opt == NULL)
273 return 0;
274
1b6f0bb5 275 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
aba5acdf
SH
276
277 if (tb[TCA_GRED_PARMS] == NULL)
278 return -1;
a5a6f1e8 279
1b6f0bb5
ED
280 if (tb[TCA_GRED_MAX_P] &&
281 RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
282 max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
283
1693a4d3 284 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
aba5acdf 285 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
1693a4d3
DW
286 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
287 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
aba5acdf
SH
288 fprintf(f,"\n GRED received message smaller than expected\n");
289 return -1;
1693a4d3 290 }
ae665a52 291
aba5acdf
SH
292/* Bad hack! should really return a proper message as shown above*/
293
357c45ad 294 fprintf(f, "vqs %u default %u %s",
1693a4d3
DW
295 sopt->DPs,
296 sopt->def_DP,
297 sopt->grio ? "grio " : "");
298
aba5acdf
SH
299 for (i=0;i<MAX_DPs;i++, qopt++) {
300 if (qopt->DP >= MAX_DPs) continue;
357c45ad 301 fprintf(f, "\n vq %u prio %hhu limit %s min %s max %s ",
aba5acdf
SH
302 qopt->DP,
303 qopt->prio,
1693a4d3
DW
304 sprint_size(qopt->limit, b1),
305 sprint_size(qopt->qth_min, b2),
306 sprint_size(qopt->qth_max, b3));
307 if (show_details) {
308 fprintf(f, "ewma %u ", qopt->Wlog);
309 if (max_p)
310 fprintf(f, "probability %lg ", max_p[i] / pow(2, 32));
311 else
312 fprintf(f, "Plog %u ", qopt->Plog);
313 fprintf(f, "Scell_log %u ", qopt->Scell_log);
314 }
315 if (show_stats) {
316 fprintf(f, "\n Queue size: average %s current %s ",
317 sprint_size(qopt->qave, b1),
318 sprint_size(qopt->backlog, b2));
319 fprintf(f, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
320 qopt->forced,
321 qopt->early,
322 qopt->pdrop,
323 qopt->other);
324 fprintf(f, "\n Total packets: %u (%s) ",
325 qopt->packets,
326 sprint_size(qopt->bytesin, b1));
327 }
aba5acdf
SH
328 }
329 return 0;
330}
331
95812b56 332struct qdisc_util gred_qdisc_util = {
f2f99e2e
SH
333 .id = "gred",
334 .parse_qopt = gred_parse_opt,
335 .print_qopt = gred_print_opt,
aba5acdf 336};