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