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