Debian Codegraph @

tc/q_sfq.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * q_sfq.c SFQ.
4 *
5 * Authors: Alexey Kuznetsov, <[email protected]>
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <sys/socket.h>
13 #include <netinet/in.h>
14 #include <arpa/inet.h>
15 #include <string.h>
16 #include <math.h>
17
18 #include "utils.h"
19 #include "tc_util.h"
20 #include "tc_red.h"
21
22 static void explain(void)
23 {
24 fprintf(stderr,
25 "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n"
26 " [ divisor NUMBER ] [ flows NUMBER] [ depth NUMBER ]\n"
27 " [ headdrop ]\n"
28 " [ redflowlimit BYTES ] [ min BYTES ] [ max BYTES ]\n"
29 " [ avpkt BYTES ] [ burst PACKETS ] [ probability P ]\n"
30 " [ ecn ] [ harddrop ]\n");
31 }
32
33 static int sfq_parse_opt(const struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
34 {
35 int ok = 0, red = 0;
36 struct tc_sfq_qopt_v1 opt = {};
37 unsigned int burst = 0;
38 int wlog;
39 unsigned int avpkt = 1000;
40 double probability = 0.02;
41
42 while (argc > 0) {
43 if (strcmp(*argv, "quantum") == 0) {
44 NEXT_ARG();
45 if (get_size(&opt.v0.quantum, *argv)) {
46 fprintf(stderr, "Illegal \"limit\"\n");
47 return -1;
48 }
49 ok++;
50 } else if (strcmp(*argv, "perturb") == 0) {
51 NEXT_ARG();
52 if (get_integer(&opt.v0.perturb_period, *argv, 0)) {
53 fprintf(stderr, "Illegal \"perturb\"\n");
54 return -1;
55 }
56 ok++;
57 } else if (strcmp(*argv, "limit") == 0) {
58 NEXT_ARG();
59 if (get_u32(&opt.v0.limit, *argv, 0)) {
60 fprintf(stderr, "Illegal \"limit\"\n");
61 return -1;
62 }
63 if (opt.v0.limit < 2) {
64 fprintf(stderr, "Illegal \"limit\", must be > 1\n");
65 return -1;
66 }
67 ok++;
68 } else if (strcmp(*argv, "divisor") == 0) {
69 NEXT_ARG();
70 if (get_u32(&opt.v0.divisor, *argv, 0)) {
71 fprintf(stderr, "Illegal \"divisor\"\n");
72 return -1;
73 }
74 ok++;
75 } else if (strcmp(*argv, "flows") == 0) {
76 NEXT_ARG();
77 if (get_u32(&opt.v0.flows, *argv, 0)) {
78 fprintf(stderr, "Illegal \"flows\"\n");
79 return -1;
80 }
81 ok++;
82 } else if (strcmp(*argv, "depth") == 0) {
83 NEXT_ARG();
84 if (get_u32(&opt.depth, *argv, 0)) {
85 fprintf(stderr, "Illegal \"flows\"\n");
86 return -1;
87 }
88 ok++;
89 } else if (strcmp(*argv, "headdrop") == 0) {
90 opt.headdrop = 1;
91 ok++;
92 } else if (strcmp(*argv, "redflowlimit") == 0) {
93 NEXT_ARG();
94 if (get_u32(&opt.limit, *argv, 0)) {
95 fprintf(stderr, "Illegal \"redflowlimit\"\n");
96 return -1;
97 }
98 red++;
99 } else if (strcmp(*argv, "min") == 0) {
100 NEXT_ARG();
101 if (get_u32(&opt.qth_min, *argv, 0)) {
102 fprintf(stderr, "Illegal \"min\"\n");
103 return -1;
104 }
105 red++;
106 } else if (strcmp(*argv, "max") == 0) {
107 NEXT_ARG();
108 if (get_u32(&opt.qth_max, *argv, 0)) {
109 fprintf(stderr, "Illegal \"max\"\n");
110 return -1;
111 }
112 red++;
113 } else if (strcmp(*argv, "burst") == 0) {
114 NEXT_ARG();
115 if (get_unsigned(&burst, *argv, 0)) {
116 fprintf(stderr, "Illegal \"burst\"\n");
117 return -1;
118 }
119 red++;
120 } else if (strcmp(*argv, "avpkt") == 0) {
121 NEXT_ARG();
122 if (get_size(&avpkt, *argv)) {
123 fprintf(stderr, "Illegal \"avpkt\"\n");
124 return -1;
125 }
126 red++;
127 } else if (strcmp(*argv, "probability") == 0) {
128 NEXT_ARG();
129 if (sscanf(*argv, "%lg", &probability) != 1) {
130 fprintf(stderr, "Illegal \"probability\"\n");
131 return -1;
132 }
133 red++;
134 } else if (strcmp(*argv, "ecn") == 0) {
135 opt.flags |= TC_RED_ECN;
136 red++;
137 } else if (strcmp(*argv, "harddrop") == 0) {
138 opt.flags |= TC_RED_HARDDROP;
139 red++;
140 } else if (strcmp(*argv, "help") == 0) {
141 explain();
142 return -1;
143 } else {
144 fprintf(stderr, "What is \"%s\"?\n", *argv);
145 explain();
146 return -1;
147 }
148 argc--; argv++;
149 }
150 if (red) {
151 if (!opt.limit) {
152 fprintf(stderr, "Required parameter (redflowlimit) is missing\n");
153 return -1;
154 }
155 /* Compute default min/max thresholds based on
156 Sally Floyd's recommendations:
157 http://www.icir.org/floyd/REDparameters.txt
158 */
159 if (!opt.qth_max)
160 opt.qth_max = opt.limit / 4;
161 if (!opt.qth_min)
162 opt.qth_min = opt.qth_max / 3;
163 if (!burst)
164 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
165
166 if (opt.qth_max > opt.limit) {
167 fprintf(stderr, "\"max\" is larger than \"limit\"\n");
168 return -1;
169 }
170
171 if (opt.qth_min >= opt.qth_max) {
172 fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
173 return -1;
174 }
175
176 wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt);
177 if (wlog < 0) {
178 fprintf(stderr, "SFQ: failed to calculate EWMA constant.\n");
179 return -1;
180 }
181 if (wlog >= 10)
182 fprintf(stderr, "SFQ: WARNING. Burst %u seems to be too large.\n", burst);
183 opt.Wlog = wlog;
184
185 wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability);
186 if (wlog < 0) {
187 fprintf(stderr, "SFQ: failed to calculate probability.\n");
188 return -1;
189 }
190 opt.Plog = wlog;
191 opt.max_P = probability * pow(2, 32);
192 }
193
194 if (ok || red)
195 addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
196 return 0;
197 }
198
199 static int sfq_print_opt(const struct qdisc_util *qu, FILE *f, struct rtattr *opt)
200 {
201 struct tc_sfq_qopt *qopt;
202 struct tc_sfq_qopt_v1 *qopt_ext = NULL;
203
204 if (opt == NULL)
205 return 0;
206
207 if (RTA_PAYLOAD(opt) < sizeof(*qopt))
208 return -1;
209 if (RTA_PAYLOAD(opt) >= sizeof(*qopt_ext))
210 qopt_ext = RTA_DATA(opt);
211 qopt = RTA_DATA(opt);
212
213 print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit);
214 print_size(PRINT_ANY, "quantum", "quantum %s ", qopt->quantum);
215
216 if (qopt_ext && qopt_ext->depth)
217 print_uint(PRINT_ANY, "depth", "depth %u ", qopt_ext->depth);
218 if (qopt_ext && qopt_ext->headdrop)
219 print_bool(PRINT_ANY, "headdrop", "headdrop ", true);
220 if (show_details)
221 print_uint(PRINT_ANY, "flows", "flows %u ", qopt->flows);
222
223 print_uint(PRINT_ANY, "divisor", "divisor %u ", qopt->divisor);
224
225 if (qopt->perturb_period)
226 print_int(PRINT_ANY, "perturb", "perturb %dsec ",
227 qopt->perturb_period);
228 if (qopt_ext && qopt_ext->qth_min) {
229 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt_ext->Wlog);
230 print_size(PRINT_ANY, "min", "min %s ", qopt_ext->qth_min);
231 print_size(PRINT_ANY, "max", "max %s ", qopt_ext->qth_max);
232 print_float(PRINT_ANY, "probability", "probability %lg ",
233 qopt_ext->max_P / pow(2, 32));
234 tc_red_print_flags(qopt_ext->flags);
235 if (show_stats) {
236 print_nl();
237 print_uint(PRINT_ANY, "prob_mark", " prob_mark %u",
238 qopt_ext->stats.prob_mark);
239 print_uint(PRINT_ANY, "prob_mark_head",
240 " prob_mark_head %u",
241 qopt_ext->stats.prob_mark_head);
242 print_uint(PRINT_ANY, "prob_drop", " prob_drop %u",
243 qopt_ext->stats.prob_drop);
244 print_nl();
245 print_uint(PRINT_ANY, "forced_mark",
246 " forced_mark %u",
247 qopt_ext->stats.forced_mark);
248 print_uint(PRINT_ANY, "forced_mark_head",
249 " forced_mark_head %u",
250 qopt_ext->stats.forced_mark_head);
251 print_uint(PRINT_ANY, "forced_drop", " forced_drop %u",
252 qopt_ext->stats.forced_drop);
253 }
254 }
255 return 0;
256 }
257
258 static int sfq_print_xstats(const struct qdisc_util *qu, FILE *f,
259 struct rtattr *xstats)
260 {
261 struct tc_sfq_xstats *st;
262
263 if (xstats == NULL)
264 return 0;
265 if (RTA_PAYLOAD(xstats) < sizeof(*st))
266 return -1;
267 st = RTA_DATA(xstats);
268
269 print_int(PRINT_ANY, "allot", " allot %d", st->allot);
270
271 return 0;
272 }
273
274 struct qdisc_util sfq_qdisc_util = {
275 .id = "sfq",
276 .parse_qopt = sfq_parse_opt,
277 .print_qopt = sfq_print_opt,
278 .print_xstats = sfq_print_xstats,
279 };