]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_gred.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_gred.c
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 *
9 * Authors: J Hadi Salim(hadi@nortelnetworks.com)
10 * code ruthlessly ripped from
11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 *
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <string.h>
23 #include <math.h>
24
25 #include "utils.h"
26 #include "tc_util.h"
27
28 #include "tc_red.h"
29
30
31 #if 0
32 #define DPRINTF(format, args...) fprintf(stderr, format, ##args)
33 #else
34 #define DPRINTF(format, args...)
35 #endif
36
37 static void explain(void)
38 {
39 fprintf(stderr, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
40 fprintf(stderr, " default DEFAULT_VQ [ grio ] [ limit BYTES ]\n");
41 fprintf(stderr, " tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n");
42 fprintf(stderr, " min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n");
43 fprintf(stderr, " [ probability PROBABILITY ] [ bandwidth KBPS ]\n");
44 }
45
46 static int init_gred(struct qdisc_util *qu, int argc, char **argv,
47 struct nlmsghdr *n)
48 {
49
50 struct rtattr *tail;
51 struct tc_gred_sopt opt = { 0 };
52 __u32 limit = 0;
53
54 opt.def_DP = MAX_DPs;
55
56 while (argc > 0) {
57 DPRINTF(stderr, "init_gred: invoked with %s\n", *argv);
58 if (strcmp(*argv, "vqs") == 0 ||
59 strcmp(*argv, "DPs") == 0) {
60 NEXT_ARG();
61 if (get_unsigned(&opt.DPs, *argv, 10)) {
62 fprintf(stderr, "Illegal \"vqs\"\n");
63 return -1;
64 } else if (opt.DPs > MAX_DPs) {
65 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
66 MAX_DPs);
67 return -1;
68 }
69 } else if (strcmp(*argv, "default") == 0) {
70 if (opt.DPs == 0) {
71 fprintf(stderr, "\"default\" must be defined after \"vqs\"\n");
72 return -1;
73 }
74 NEXT_ARG();
75 if (get_unsigned(&opt.def_DP, *argv, 10)) {
76 fprintf(stderr, "Illegal \"default\"\n");
77 return -1;
78 } else if (opt.def_DP >= opt.DPs) {
79 fprintf(stderr, "\"default\" must be less than \"vqs\"\n");
80 return -1;
81 }
82 } else if (strcmp(*argv, "grio") == 0) {
83 opt.grio = 1;
84 } else if (strcmp(*argv, "limit") == 0) {
85 NEXT_ARG();
86 if (get_size(&limit, *argv)) {
87 fprintf(stderr, "Illegal \"limit\"\n");
88 return -1;
89 }
90 } else if (strcmp(*argv, "help") == 0) {
91 explain();
92 return -1;
93 } else {
94 fprintf(stderr, "What is \"%s\"?\n", *argv);
95 explain();
96 return -1;
97 }
98 argc--; argv++;
99 }
100
101 if (!opt.DPs || opt.def_DP == MAX_DPs) {
102 fprintf(stderr, "Illegal gred setup parameters\n");
103 return -1;
104 }
105
106 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n", opt.DPs, opt.def_DP);
107 n->nlmsg_flags |= NLM_F_CREATE;
108 tail = NLMSG_TAIL(n);
109 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
110 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
111 if (limit)
112 addattr32(n, 1024, TCA_GRED_LIMIT, limit);
113 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
114 return 0;
115 }
116 /*
117 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
118 */
119 static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
120 {
121 int ok = 0;
122 struct tc_gred_qopt opt = { 0 };
123 unsigned int burst = 0;
124 unsigned int avpkt = 0;
125 double probability = 0.02;
126 unsigned int rate = 0;
127 int parm;
128 __u8 sbuf[256];
129 struct rtattr *tail;
130 __u32 max_P;
131
132 opt.DP = MAX_DPs;
133
134 while (argc > 0) {
135 if (strcmp(*argv, "limit") == 0) {
136 NEXT_ARG();
137 if (get_size(&opt.limit, *argv)) {
138 fprintf(stderr, "Illegal \"limit\"\n");
139 return -1;
140 }
141 ok++;
142 } else if (strcmp(*argv, "setup") == 0) {
143 if (ok) {
144 fprintf(stderr, "Illegal \"setup\"\n");
145 return -1;
146 }
147 return init_gred(qu, argc-1, argv+1, n);
148 } else if (strcmp(*argv, "min") == 0) {
149 NEXT_ARG();
150 if (get_size(&opt.qth_min, *argv)) {
151 fprintf(stderr, "Illegal \"min\"\n");
152 return -1;
153 }
154 ok++;
155 } else if (strcmp(*argv, "max") == 0) {
156 NEXT_ARG();
157 if (get_size(&opt.qth_max, *argv)) {
158 fprintf(stderr, "Illegal \"max\"\n");
159 return -1;
160 }
161 ok++;
162 } else if (strcmp(*argv, "vq") == 0 ||
163 strcmp(*argv, "DP") == 0) {
164 NEXT_ARG();
165 if (get_unsigned(&opt.DP, *argv, 10)) {
166 fprintf(stderr, "Illegal \"vq\"\n");
167 return -1;
168 } else if (opt.DP >= MAX_DPs) {
169 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
170 MAX_DPs);
171 return -1;
172 } /* need a better error check */
173 ok++;
174 } else if (strcmp(*argv, "burst") == 0) {
175 NEXT_ARG();
176 if (get_unsigned(&burst, *argv, 0)) {
177 fprintf(stderr, "Illegal \"burst\"\n");
178 return -1;
179 }
180 ok++;
181 } else if (strcmp(*argv, "avpkt") == 0) {
182 NEXT_ARG();
183 if (get_size(&avpkt, *argv)) {
184 fprintf(stderr, "Illegal \"avpkt\"\n");
185 return -1;
186 }
187 ok++;
188 } else if (strcmp(*argv, "probability") == 0) {
189 NEXT_ARG();
190 if (sscanf(*argv, "%lg", &probability) != 1) {
191 fprintf(stderr, "Illegal \"probability\"\n");
192 return -1;
193 }
194 ok++;
195 } else if (strcmp(*argv, "prio") == 0) {
196 NEXT_ARG();
197 opt.prio = strtol(*argv, (char **)NULL, 10);
198 /* some error check here */
199 ok++;
200 } else if (strcmp(*argv, "bandwidth") == 0) {
201 NEXT_ARG();
202 if (strchr(*argv, '%')) {
203 if (get_percent_rate(&rate, *argv, dev)) {
204 fprintf(stderr, "Illegal \"bandwidth\"\n");
205 return -1;
206 }
207 } else if (get_rate(&rate, *argv)) {
208 fprintf(stderr, "Illegal \"bandwidth\"\n");
209 return -1;
210 }
211 ok++;
212 } else if (strcmp(*argv, "help") == 0) {
213 explain();
214 return -1;
215 } else {
216 fprintf(stderr, "What is \"%s\"?\n", *argv);
217 explain();
218 return -1;
219 }
220 argc--; argv++;
221 }
222
223 if (!ok) {
224 explain();
225 return -1;
226 }
227 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
228 !avpkt) {
229 fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
230 return -1;
231 }
232 if (!burst) {
233 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
234 fprintf(stderr, "GRED: set burst to %u\n", burst);
235 }
236 if (!rate) {
237 get_rate(&rate, "10Mbit");
238 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
239 }
240 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
241 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
242 return -1;
243 }
244 if (parm >= 10)
245 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
246 burst);
247 opt.Wlog = parm;
248 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
249 fprintf(stderr, "GRED: failed to calculate probability.\n");
250 return -1;
251 }
252 opt.Plog = parm;
253 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
254 {
255 fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
256 return -1;
257 }
258 opt.Scell_log = parm;
259
260 tail = NLMSG_TAIL(n);
261 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
262 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
263 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
264 max_P = probability * pow(2, 32);
265 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
266 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
267 return 0;
268 }
269
270 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
271 {
272 struct rtattr *tb[TCA_GRED_MAX + 1];
273 struct tc_gred_sopt *sopt;
274 struct tc_gred_qopt *qopt;
275 __u32 *max_p = NULL;
276 __u32 *limit = NULL;
277 unsigned int i;
278
279 SPRINT_BUF(b1);
280 SPRINT_BUF(b2);
281 SPRINT_BUF(b3);
282
283 if (opt == NULL)
284 return 0;
285
286 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
287
288 if (tb[TCA_GRED_PARMS] == NULL)
289 return -1;
290
291 if (tb[TCA_GRED_MAX_P] &&
292 RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
293 max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
294
295 if (tb[TCA_GRED_LIMIT] &&
296 RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
297 limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
298
299 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
300 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
301 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
302 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
303 fprintf(f, "\n GRED received message smaller than expected\n");
304 return -1;
305 }
306
307 /* Bad hack! should really return a proper message as shown above*/
308
309 fprintf(f, "vqs %u default %u %s",
310 sopt->DPs,
311 sopt->def_DP,
312 sopt->grio ? "grio " : "");
313
314 if (limit)
315 fprintf(f, "limit %s ",
316 sprint_size(*limit, b1));
317
318 for (i = 0; i < MAX_DPs; i++, qopt++) {
319 if (qopt->DP >= MAX_DPs) continue;
320 fprintf(f, "\n vq %u prio %hhu limit %s min %s max %s ",
321 qopt->DP,
322 qopt->prio,
323 sprint_size(qopt->limit, b1),
324 sprint_size(qopt->qth_min, b2),
325 sprint_size(qopt->qth_max, b3));
326 if (show_details) {
327 fprintf(f, "ewma %u ", qopt->Wlog);
328 if (max_p)
329 fprintf(f, "probability %lg ", max_p[i] / pow(2, 32));
330 else
331 fprintf(f, "Plog %u ", qopt->Plog);
332 fprintf(f, "Scell_log %u ", qopt->Scell_log);
333 }
334 if (show_stats) {
335 fprintf(f, "\n Queue size: average %s current %s ",
336 sprint_size(qopt->qave, b1),
337 sprint_size(qopt->backlog, b2));
338 fprintf(f, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
339 qopt->forced,
340 qopt->early,
341 qopt->pdrop,
342 qopt->other);
343 fprintf(f, "\n Total packets: %u (%s) ",
344 qopt->packets,
345 sprint_size(qopt->bytesin, b1));
346 }
347 }
348 return 0;
349 }
350
351 struct qdisc_util gred_qdisc_util = {
352 .id = "gred",
353 .parse_qopt = gred_parse_opt,
354 .print_qopt = gred_print_opt,
355 };