q_gred.c revision 3b3ecd31c1e513f029ebbb83f2e808553de0a741
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;
109	tail = NLMSG_TAIL(n);
110	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
111	addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
112	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
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
254	tail = NLMSG_TAIL(n);
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);
258	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
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	parse_rtattr_nested(tb, TCA_GRED_STAB, opt);
277
278	if (tb[TCA_GRED_PARMS] == NULL)
279		return -1;
280#if 0
281	sopt = RTA_DATA(tb[TCA_GRED_DPS]);
282	if (RTA_PAYLOAD(tb[TCA_GRED_DPS])  < sizeof(*sopt)) {
283		printf("\n GRED DPs message smaller than expected\n");
284		return -1;
285		}
286
287	DPRINTF(f, "\n\tDPs:%d Default DP %d\n ",
288		sopt->DPs, sopt->def_DP);
289#endif
290	qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
291	if (RTA_PAYLOAD(tb[TCA_GRED_PARMS])  < sizeof(*qopt)*MAX_DPs) {
292		fprintf(f,"\n GRED received message smaller than expected\n");
293		return -1;
294		}
295
296
297#if 0
298
299	for (i=0;i<sopt->DPs;i++)
300#endif
301/* Bad hack! should really return a proper message as shown above*/
302
303	for (i=0;i<MAX_DPs;i++, qopt++) {
304		if (qopt->DP >= MAX_DPs) continue;
305		fprintf(f, "\n DP:%d (prio %d) Average Queue %s Measured "
306		    "Queue %s  ",
307			qopt->DP,
308			qopt->prio,
309			sprint_size(qopt->qave, b4),
310			sprint_size(qopt->backlog, b5));
311		fprintf(f, "\n\t Packet drops: %d (forced %d early %d)  ",
312			qopt->forced+qopt->early,
313			qopt->forced,
314			qopt->early);
315		fprintf(f, "\n\t Packet totals: %u (bytes %u)  ",
316			qopt->packets,
317			qopt->bytesin);
318		if (show_details)
319			fprintf(f, "\n limit %s min %s max %s ",
320				sprint_size(qopt->limit, b1),
321				sprint_size(qopt->qth_min, b2),
322				sprint_size(qopt->qth_max, b3));
323				fprintf(f, "ewma %u Plog %u Scell_log %u",
324				    qopt->Wlog, qopt->Plog, qopt->Scell_log);
325	}
326	return 0;
327}
328
329struct qdisc_util gred_qdisc_util = {
330	.id		= "gred",
331	.parse_qopt	= gred_parse_opt,
332	.print_qopt	= gred_print_opt,
333};
334