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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
27 static void explain(void)
30 "Usage: ... sfq [ limit NUMBER ] [ perturb SECS ] [ quantum BYTES ]\n"
31 " [ divisor NUMBER ] [ flows NUMBER] [ depth NUMBER ]\n"
33 " [ redflowlimit BYTES ] [ min BYTES ] [ max BYTES ]\n"
34 " [ avpkt BYTES ] [ burst PACKETS ] [ probability P ]\n"
35 " [ ecn ] [ harddrop ]\n");
38 static int sfq_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
41 struct tc_sfq_qopt_v1 opt = {};
42 unsigned int burst = 0;
44 unsigned int avpkt = 1000;
45 double probability = 0.02;
48 if (strcmp(*argv, "quantum") == 0) {
50 if (get_size(&opt.v0.quantum, *argv)) {
51 fprintf(stderr, "Illegal \"limit\"\n");
55 } else if (strcmp(*argv, "perturb") == 0) {
57 if (get_integer(&opt.v0.perturb_period, *argv, 0)) {
58 fprintf(stderr, "Illegal \"perturb\"\n");
62 } else if (strcmp(*argv, "limit") == 0) {
64 if (get_u32(&opt.v0.limit, *argv, 0)) {
65 fprintf(stderr, "Illegal \"limit\"\n");
68 if (opt.v0.limit < 2) {
69 fprintf(stderr, "Illegal \"limit\", must be > 1\n");
73 } else if (strcmp(*argv, "divisor") == 0) {
75 if (get_u32(&opt.v0.divisor, *argv, 0)) {
76 fprintf(stderr, "Illegal \"divisor\"\n");
80 } else if (strcmp(*argv, "flows") == 0) {
82 if (get_u32(&opt.v0.flows, *argv, 0)) {
83 fprintf(stderr, "Illegal \"flows\"\n");
87 } else if (strcmp(*argv, "depth") == 0) {
89 if (get_u32(&opt.depth, *argv, 0)) {
90 fprintf(stderr, "Illegal \"flows\"\n");
94 } else if (strcmp(*argv, "headdrop") == 0) {
97 } else if (strcmp(*argv, "redflowlimit") == 0) {
99 if (get_u32(&opt.limit, *argv, 0)) {
100 fprintf(stderr, "Illegal \"redflowlimit\"\n");
104 } else if (strcmp(*argv, "min") == 0) {
106 if (get_u32(&opt.qth_min, *argv, 0)) {
107 fprintf(stderr, "Illegal \"min\"\n");
111 } else if (strcmp(*argv, "max") == 0) {
113 if (get_u32(&opt.qth_max, *argv, 0)) {
114 fprintf(stderr, "Illegal \"max\"\n");
118 } else if (strcmp(*argv, "burst") == 0) {
120 if (get_unsigned(&burst, *argv, 0)) {
121 fprintf(stderr, "Illegal \"burst\"\n");
125 } else if (strcmp(*argv, "avpkt") == 0) {
127 if (get_size(&avpkt, *argv)) {
128 fprintf(stderr, "Illegal \"avpkt\"\n");
132 } else if (strcmp(*argv, "probability") == 0) {
134 if (sscanf(*argv, "%lg", &probability) != 1) {
135 fprintf(stderr, "Illegal \"probability\"\n");
139 } else if (strcmp(*argv, "ecn") == 0) {
140 opt.flags |= TC_RED_ECN;
142 } else if (strcmp(*argv, "harddrop") == 0) {
143 opt.flags |= TC_RED_HARDDROP;
145 } else if (strcmp(*argv, "help") == 0) {
149 fprintf(stderr, "What is \"%s\"?\n", *argv);
157 fprintf(stderr, "Required parameter (redflowlimit) is missing\n");
160 /* Compute default min/max thresholds based on
161 Sally Floyd's recommendations:
162 http://www.icir.org/floyd/REDparameters.txt
165 opt.qth_max = opt.limit / 4;
167 opt.qth_min = opt.qth_max / 3;
169 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
171 if (opt.qth_max > opt.limit) {
172 fprintf(stderr, "\"max\" is larger than \"limit\"\n");
176 if (opt.qth_min >= opt.qth_max) {
177 fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
181 wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt);
183 fprintf(stderr, "SFQ: failed to calculate EWMA constant.\n");
187 fprintf(stderr, "SFQ: WARNING. Burst %u seems to be too large.\n", burst);
190 wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability);
192 fprintf(stderr, "SFQ: failed to calculate probability.\n");
196 opt.max_P = probability * pow(2, 32);
200 addattr_l(n, 1024, TCA_OPTIONS, &opt, sizeof(opt));
204 static int sfq_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
206 struct tc_sfq_qopt *qopt;
207 struct tc_sfq_qopt_v1 *qopt_ext = NULL;
215 if (RTA_PAYLOAD(opt) < sizeof(*qopt))
217 if (RTA_PAYLOAD(opt) >= sizeof(*qopt_ext))
218 qopt_ext = RTA_DATA(opt);
219 qopt = RTA_DATA(opt);
221 print_uint(PRINT_ANY, "limit", "limit %up ", qopt->limit);
222 print_uint(PRINT_JSON, "quantum", NULL, qopt->quantum);
223 print_string(PRINT_FP, NULL, "quantum %s ",
224 sprint_size(qopt->quantum, b1));
226 if (qopt_ext && qopt_ext->depth)
227 print_uint(PRINT_ANY, "depth", "depth %u ", qopt_ext->depth);
228 if (qopt_ext && qopt_ext->headdrop)
229 print_bool(PRINT_ANY, "headdrop", "headdrop ", true);
231 print_uint(PRINT_ANY, "flows", "flows %u ", qopt->flows);
233 print_uint(PRINT_ANY, "divisor", "divisor %u ", qopt->divisor);
235 if (qopt->perturb_period)
236 print_int(PRINT_ANY, "perturb", "perturb %dsec ",
237 qopt->perturb_period);
238 if (qopt_ext && qopt_ext->qth_min) {
239 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt_ext->Wlog);
240 print_uint(PRINT_JSON, "min", NULL, qopt_ext->qth_min);
241 print_string(PRINT_FP, NULL, "min %s ",
242 sprint_size(qopt_ext->qth_min, b2));
243 print_uint(PRINT_JSON, "max", NULL, qopt_ext->qth_max);
244 print_string(PRINT_FP, NULL, "max %s ",
245 sprint_size(qopt_ext->qth_max, b3));
246 print_float(PRINT_ANY, "probability", "probability %lg ",
247 qopt_ext->max_P / pow(2, 32));
248 tc_red_print_flags(qopt_ext->flags);
251 print_uint(PRINT_ANY, "prob_mark", " prob_mark %u",
252 qopt_ext->stats.prob_mark);
253 print_uint(PRINT_ANY, "prob_mark_head",
254 " prob_mark_head %u",
255 qopt_ext->stats.prob_mark_head);
256 print_uint(PRINT_ANY, "prob_drop", " prob_drop %u",
257 qopt_ext->stats.prob_drop);
259 print_uint(PRINT_ANY, "forced_mark",
261 qopt_ext->stats.forced_mark);
262 print_uint(PRINT_ANY, "forced_mark_head",
263 " forced_mark_head %u",
264 qopt_ext->stats.forced_mark_head);
265 print_uint(PRINT_ANY, "forced_drop", " forced_drop %u",
266 qopt_ext->stats.forced_drop);
272 static int sfq_print_xstats(struct qdisc_util *qu, FILE *f,
273 struct rtattr *xstats)
275 struct tc_sfq_xstats *st;
279 if (RTA_PAYLOAD(xstats) < sizeof(*st))
281 st = RTA_DATA(xstats);
283 print_int(PRINT_ANY, "allot", " allot %d", st->allot);
288 struct qdisc_util sfq_qdisc_util = {
290 .parse_qopt = sfq_parse_opt,
291 .print_qopt = sfq_print_opt,
292 .print_xstats = sfq_print_xstats,