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