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