1/*
2 * q_red.c		RED.
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:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
16#include <syslog.h>
17#include <fcntl.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <string.h>
22
23#include "utils.h"
24#include "tc_util.h"
25
26#include "tc_red.h"
27
28static void explain(void)
29{
30	fprintf(stderr, "Usage: ... red limit BYTES min BYTES max BYTES avpkt BYTES burst PACKETS\n");
31	fprintf(stderr, "               probability PROBABILITY bandwidth KBPS [ ecn ]\n");
32}
33
34static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
35{
36	struct tc_red_qopt opt;
37	unsigned burst = 0;
38	unsigned avpkt = 0;
39	double probability = 0.02;
40	unsigned rate = 0;
41	int ecn_ok = 0;
42	int wlog;
43	__u8 sbuf[256];
44	struct rtattr *tail;
45
46	memset(&opt, 0, sizeof(opt));
47
48	while (argc > 0) {
49		if (strcmp(*argv, "limit") == 0) {
50			NEXT_ARG();
51			if (get_size(&opt.limit, *argv)) {
52				fprintf(stderr, "Illegal \"limit\"\n");
53				return -1;
54			}
55		} else if (strcmp(*argv, "min") == 0) {
56			NEXT_ARG();
57			if (get_size(&opt.qth_min, *argv)) {
58				fprintf(stderr, "Illegal \"min\"\n");
59				return -1;
60			}
61		} else if (strcmp(*argv, "max") == 0) {
62			NEXT_ARG();
63			if (get_size(&opt.qth_max, *argv)) {
64				fprintf(stderr, "Illegal \"max\"\n");
65				return -1;
66			}
67		} else if (strcmp(*argv, "burst") == 0) {
68			NEXT_ARG();
69			if (get_unsigned(&burst, *argv, 0)) {
70				fprintf(stderr, "Illegal \"burst\"\n");
71				return -1;
72			}
73		} else if (strcmp(*argv, "avpkt") == 0) {
74			NEXT_ARG();
75			if (get_size(&avpkt, *argv)) {
76				fprintf(stderr, "Illegal \"avpkt\"\n");
77				return -1;
78			}
79		} else if (strcmp(*argv, "probability") == 0) {
80			NEXT_ARG();
81			if (sscanf(*argv, "%lg", &probability) != 1) {
82				fprintf(stderr, "Illegal \"probability\"\n");
83				return -1;
84			}
85		} else if (strcmp(*argv, "bandwidth") == 0) {
86			NEXT_ARG();
87			if (get_rate(&rate, *argv)) {
88				fprintf(stderr, "Illegal \"bandwidth\"\n");
89				return -1;
90			}
91		} else if (strcmp(*argv, "ecn") == 0) {
92			ecn_ok = 1;
93		} else if (strcmp(*argv, "help") == 0) {
94			explain();
95			return -1;
96		} else {
97			fprintf(stderr, "What is \"%s\"?\n", *argv);
98			explain();
99			return -1;
100		}
101		argc--; argv++;
102	}
103
104	if (rate == 0)
105		get_rate(&rate, "10Mbit");
106
107	if (!opt.qth_min || !opt.qth_max || !burst || !opt.limit || !avpkt) {
108		fprintf(stderr, "Required parameter (min, max, burst, limit, avpkt) is missing\n");
109		return -1;
110	}
111
112	if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
113		fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
114		return -1;
115	}
116	if (wlog >= 10)
117		fprintf(stderr, "RED: WARNING. Burst %d seems to be to large.\n", burst);
118	opt.Wlog = wlog;
119	if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
120		fprintf(stderr, "RED: failed to calculate probability.\n");
121		return -1;
122	}
123	opt.Plog = wlog;
124	if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
125		fprintf(stderr, "RED: failed to calculate idle damping table.\n");
126		return -1;
127	}
128	opt.Scell_log = wlog;
129	if (ecn_ok) {
130#ifdef TC_RED_ECN
131		opt.flags |= TC_RED_ECN;
132#else
133		fprintf(stderr, "RED: ECN support is missing in this binary.\n");
134		return -1;
135#endif
136	}
137
138	tail = NLMSG_TAIL(n);
139	addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
140	addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
141	addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
142	tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
143	return 0;
144}
145
146static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
147{
148	struct rtattr *tb[TCA_RED_STAB+1];
149	struct tc_red_qopt *qopt;
150	SPRINT_BUF(b1);
151	SPRINT_BUF(b2);
152	SPRINT_BUF(b3);
153
154	if (opt == NULL)
155		return 0;
156
157	parse_rtattr_nested(tb, TCA_RED_STAB, opt);
158
159	if (tb[TCA_RED_PARMS] == NULL)
160		return -1;
161	qopt = RTA_DATA(tb[TCA_RED_PARMS]);
162	if (RTA_PAYLOAD(tb[TCA_RED_PARMS])  < sizeof(*qopt))
163		return -1;
164	fprintf(f, "limit %s min %s max %s ",
165		sprint_size(qopt->limit, b1),
166		sprint_size(qopt->qth_min, b2),
167		sprint_size(qopt->qth_max, b3));
168#ifdef TC_RED_ECN
169	if (qopt->flags & TC_RED_ECN)
170		fprintf(f, "ecn ");
171#endif
172	if (show_details) {
173		fprintf(f, "ewma %u Plog %u Scell_log %u",
174			qopt->Wlog, qopt->Plog, qopt->Scell_log);
175	}
176	return 0;
177}
178
179static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
180{
181#ifdef TC_RED_ECN
182	struct tc_red_xstats *st;
183
184	if (xstats == NULL)
185		return 0;
186
187	if (RTA_PAYLOAD(xstats) < sizeof(*st))
188		return -1;
189
190	st = RTA_DATA(xstats);
191	fprintf(f, "  marked %u early %u pdrop %u other %u",
192		st->marked, st->early, st->pdrop, st->other);
193	return 0;
194
195#endif
196	return 0;
197}
198
199
200struct qdisc_util red_qdisc_util = {
201	.id		= "red",
202	.parse_qopt	= red_parse_opt,
203	.print_qopt	= red_print_opt,
204	.print_xstats	= red_print_xstats,
205};
206