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