]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_gred.c
501437bc5abe88c9c23b5b0a1f61d2a9e0274c39
[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 = addattr_nest(n, 1024, TCA_OPTIONS);
109 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
110 if (limit)
111 addattr32(n, 1024, TCA_GRED_LIMIT, limit);
112 addattr_nest_end(n, tail);
113 return 0;
114 }
115 /*
116 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117 */
118 static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
119 {
120 int ok = 0;
121 struct tc_gred_qopt opt = { 0 };
122 unsigned int burst = 0;
123 unsigned int avpkt = 0;
124 double probability = 0.02;
125 unsigned int rate = 0;
126 int parm;
127 __u8 sbuf[256];
128 struct rtattr *tail;
129 __u32 max_P;
130
131 opt.DP = MAX_DPs;
132
133 while (argc > 0) {
134 if (strcmp(*argv, "limit") == 0) {
135 NEXT_ARG();
136 if (get_size(&opt.limit, *argv)) {
137 fprintf(stderr, "Illegal \"limit\"\n");
138 return -1;
139 }
140 ok++;
141 } else if (strcmp(*argv, "setup") == 0) {
142 if (ok) {
143 fprintf(stderr, "Illegal \"setup\"\n");
144 return -1;
145 }
146 return init_gred(qu, argc-1, argv+1, n);
147 } else if (strcmp(*argv, "min") == 0) {
148 NEXT_ARG();
149 if (get_size(&opt.qth_min, *argv)) {
150 fprintf(stderr, "Illegal \"min\"\n");
151 return -1;
152 }
153 ok++;
154 } else if (strcmp(*argv, "max") == 0) {
155 NEXT_ARG();
156 if (get_size(&opt.qth_max, *argv)) {
157 fprintf(stderr, "Illegal \"max\"\n");
158 return -1;
159 }
160 ok++;
161 } else if (strcmp(*argv, "vq") == 0 ||
162 strcmp(*argv, "DP") == 0) {
163 NEXT_ARG();
164 if (get_unsigned(&opt.DP, *argv, 10)) {
165 fprintf(stderr, "Illegal \"vq\"\n");
166 return -1;
167 } else if (opt.DP >= MAX_DPs) {
168 fprintf(stderr, "GRED: only %u VQs are currently supported\n",
169 MAX_DPs);
170 return -1;
171 } /* need a better error check */
172 ok++;
173 } else if (strcmp(*argv, "burst") == 0) {
174 NEXT_ARG();
175 if (get_unsigned(&burst, *argv, 0)) {
176 fprintf(stderr, "Illegal \"burst\"\n");
177 return -1;
178 }
179 ok++;
180 } else if (strcmp(*argv, "avpkt") == 0) {
181 NEXT_ARG();
182 if (get_size(&avpkt, *argv)) {
183 fprintf(stderr, "Illegal \"avpkt\"\n");
184 return -1;
185 }
186 ok++;
187 } else if (strcmp(*argv, "probability") == 0) {
188 NEXT_ARG();
189 if (sscanf(*argv, "%lg", &probability) != 1) {
190 fprintf(stderr, "Illegal \"probability\"\n");
191 return -1;
192 }
193 ok++;
194 } else if (strcmp(*argv, "prio") == 0) {
195 NEXT_ARG();
196 opt.prio = strtol(*argv, (char **)NULL, 10);
197 /* some error check here */
198 ok++;
199 } else if (strcmp(*argv, "bandwidth") == 0) {
200 NEXT_ARG();
201 if (strchr(*argv, '%')) {
202 if (get_percent_rate(&rate, *argv, dev)) {
203 fprintf(stderr, "Illegal \"bandwidth\"\n");
204 return -1;
205 }
206 } else if (get_rate(&rate, *argv)) {
207 fprintf(stderr, "Illegal \"bandwidth\"\n");
208 return -1;
209 }
210 ok++;
211 } else if (strcmp(*argv, "help") == 0) {
212 explain();
213 return -1;
214 } else {
215 fprintf(stderr, "What is \"%s\"?\n", *argv);
216 explain();
217 return -1;
218 }
219 argc--; argv++;
220 }
221
222 if (!ok) {
223 explain();
224 return -1;
225 }
226 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
227 !avpkt) {
228 fprintf(stderr, "Required parameter (vq, limit, min, max, avpkt) is missing\n");
229 return -1;
230 }
231 if (!burst) {
232 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
233 fprintf(stderr, "GRED: set burst to %u\n", burst);
234 }
235 if (!rate) {
236 get_rate(&rate, "10Mbit");
237 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
238 }
239 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
240 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
241 return -1;
242 }
243 if (parm >= 10)
244 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too large.\n",
245 burst);
246 opt.Wlog = parm;
247 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
248 fprintf(stderr, "GRED: failed to calculate probability.\n");
249 return -1;
250 }
251 opt.Plog = parm;
252 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
253 {
254 fprintf(stderr, "GRED: failed to calculate idle damping table.\n");
255 return -1;
256 }
257 opt.Scell_log = parm;
258
259 tail = addattr_nest(n, 1024, TCA_OPTIONS);
260 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
261 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
262 max_P = probability * pow(2, 32);
263 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
264 addattr_nest_end(n, tail);
265 return 0;
266 }
267
268 static void gred_print_stats(struct tc_gred_qopt *qopt)
269 {
270 SPRINT_BUF(b1);
271
272 if (!is_json_context())
273 printf("\n Queue size: ");
274
275 print_uint(PRINT_JSON, "qave", NULL, qopt->qave);
276 print_string(PRINT_FP, NULL, "average %s ",
277 sprint_size(qopt->qave, b1));
278
279 print_uint(PRINT_JSON, "backlog", NULL, qopt->backlog);
280 print_string(PRINT_FP, NULL, "current %s ",
281 sprint_size(qopt->backlog, b1));
282
283 if (!is_json_context())
284 printf("\n Dropped packets: ");
285
286 print_uint(PRINT_ANY, "forced_drop", "forced %u ", qopt->forced);
287 print_uint(PRINT_ANY, "prob_drop", "early %u ", qopt->early);
288 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", qopt->pdrop);
289 print_uint(PRINT_ANY, "other", "other %u ", qopt->other);
290
291 if (!is_json_context())
292 printf("\n Total packets: ");
293
294 print_uint(PRINT_ANY, "packets", "%u ", qopt->packets);
295
296 print_uint(PRINT_JSON, "bytes", NULL, qopt->bytesin);
297 print_string(PRINT_FP, NULL, "(%s) ", sprint_size(qopt->bytesin, b1));
298 }
299
300 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
301 {
302 struct rtattr *tb[TCA_GRED_MAX + 1];
303 struct tc_gred_sopt *sopt;
304 struct tc_gred_qopt *qopt;
305 __u32 *max_p = NULL;
306 __u32 *limit = NULL;
307 unsigned int i;
308
309 SPRINT_BUF(b1);
310
311 if (opt == NULL)
312 return 0;
313
314 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
315
316 if (tb[TCA_GRED_PARMS] == NULL)
317 return -1;
318
319 if (tb[TCA_GRED_MAX_P] &&
320 RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
321 max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
322
323 if (tb[TCA_GRED_LIMIT] &&
324 RTA_PAYLOAD(tb[TCA_GRED_LIMIT]) == sizeof(__u32))
325 limit = RTA_DATA(tb[TCA_GRED_LIMIT]);
326
327 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
328 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
329 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
330 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
331 fprintf(f, "\n GRED received message smaller than expected\n");
332 return -1;
333 }
334
335 print_uint(PRINT_ANY, "dp_cnt", "vqs %u ", sopt->DPs);
336 print_uint(PRINT_ANY, "dp_default", "default %u ", sopt->def_DP);
337
338 if (sopt->grio)
339 print_bool(PRINT_ANY, "grio", "grio ", true);
340 else
341 print_bool(PRINT_ANY, "grio", NULL, false);
342
343 if (limit) {
344 print_uint(PRINT_JSON, "limit", NULL, *limit);
345 print_string(PRINT_FP, NULL, "limit %s ",
346 sprint_size(*limit, b1));
347 }
348
349 open_json_array(PRINT_JSON, "vqs");
350 for (i = 0; i < MAX_DPs; i++, qopt++) {
351 if (qopt->DP >= MAX_DPs)
352 continue;
353
354 open_json_object(NULL);
355
356 print_uint(PRINT_ANY, "vq", "\n vq %u ", qopt->DP);
357 print_hhu(PRINT_ANY, "prio", "prio %hhu ", qopt->prio);
358
359 print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
360 print_string(PRINT_FP, NULL, "limit %s ",
361 sprint_size(qopt->limit, b1));
362
363 print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
364 print_string(PRINT_FP, NULL, "min %s ",
365 sprint_size(qopt->qth_min, b1));
366
367 print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
368 print_string(PRINT_FP, NULL, "max %s ",
369 sprint_size(qopt->qth_max, b1));
370
371 if (show_details) {
372 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
373 if (max_p)
374 print_float(PRINT_ANY, "probability",
375 "probability %lg ",
376 max_p[i] / pow(2, 32));
377 else
378 print_uint(PRINT_ANY, "Plog", "Plog %u ",
379 qopt->Plog);
380 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u ",
381 qopt->Scell_log);
382 }
383 if (show_stats)
384 gred_print_stats(qopt);
385 close_json_object();
386 }
387 close_json_array(PRINT_JSON, "vqs");
388 return 0;
389 }
390
391 struct qdisc_util gred_qdisc_util = {
392 .id = "gred",
393 .parse_qopt = gred_parse_opt,
394 .print_qopt = gred_print_opt,
395 };