]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_choke.c
tc: util: constrain percentage in 0-100 interval
[mirror_iproute2.git] / tc / q_choke.c
CommitLineData
a4eca97c
SH
1/*
2 * q_choke.c CHOKE.
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: Stephen Hemminger <shemminger@vyatta.com>
10 */
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <unistd.h>
a4eca97c
SH
15#include <fcntl.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18#include <arpa/inet.h>
19#include <string.h>
650252d8 20#include <math.h>
a4eca97c
SH
21
22#include "utils.h"
23#include "tc_util.h"
24
25#include "tc_red.h"
26
27static void explain(void)
28{
8589eb4e
MC
29 fprintf(stderr,
30 "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n"
31 " [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n");
a4eca97c
SH
32}
33
34static int choke_parse_opt(struct qdisc_util *qu, int argc, char **argv,
927e3cfb 35 struct nlmsghdr *n, const char *dev)
a4eca97c 36{
d17b136f 37 struct tc_red_qopt opt = {};
32a121cb
SH
38 unsigned int burst = 0;
39 unsigned int avpkt = 1000;
a4eca97c 40 double probability = 0.02;
32a121cb 41 unsigned int rate = 0;
a4eca97c
SH
42 int ecn_ok = 0;
43 int wlog;
44 __u8 sbuf[256];
650252d8 45 __u32 max_P;
a4eca97c
SH
46 struct rtattr *tail;
47
a4eca97c
SH
48 while (argc > 0) {
49 if (strcmp(*argv, "limit") == 0) {
50 NEXT_ARG();
51 if (get_unsigned(&opt.limit, *argv, 0)) {
52 fprintf(stderr, "Illegal \"limit\"\n");
53 return -1;
54 }
55 } else if (strcmp(*argv, "bandwidth") == 0) {
56 NEXT_ARG();
927e3cfb
ND
57 if (strchr(*argv, '%')) {
58 if (get_percent_rate(&rate, *argv, dev)) {
59 fprintf(stderr, "Illegal \"bandwidth\"\n");
60 return -1;
61 }
62 } else if (get_rate(&rate, *argv)) {
a4eca97c
SH
63 fprintf(stderr, "Illegal \"bandwidth\"\n");
64 return -1;
65 }
66 } else if (strcmp(*argv, "ecn") == 0) {
67 ecn_ok = 1;
68 } else if (strcmp(*argv, "min") == 0) {
69 NEXT_ARG();
70 if (get_unsigned(&opt.qth_min, *argv, 0)) {
71 fprintf(stderr, "Illegal \"min\"\n");
72 return -1;
73 }
74 } else if (strcmp(*argv, "max") == 0) {
75 NEXT_ARG();
76 if (get_unsigned(&opt.qth_max, *argv, 0)) {
77 fprintf(stderr, "Illegal \"max\"\n");
78 return -1;
79 }
80 } else if (strcmp(*argv, "burst") == 0) {
81 NEXT_ARG();
82 if (get_unsigned(&burst, *argv, 0)) {
83 fprintf(stderr, "Illegal \"burst\"\n");
84 return -1;
85 }
86 } else if (strcmp(*argv, "avpkt") == 0) {
87 NEXT_ARG();
88 if (get_size(&avpkt, *argv)) {
89 fprintf(stderr, "Illegal \"avpkt\"\n");
90 return -1;
91 }
92 } else if (strcmp(*argv, "probability") == 0) {
93 NEXT_ARG();
94 if (sscanf(*argv, "%lg", &probability) != 1) {
95 fprintf(stderr, "Illegal \"probability\"\n");
96 return -1;
97 }
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 (!rate || !opt.limit) {
110 fprintf(stderr, "Required parameter (bandwidth, limit) is missing\n");
111 return -1;
112 }
113
3d0b7439 114 /* Compute default min/max thresholds based on
a4eca97c
SH
115 Sally Floyd's recommendations:
116 http://www.icir.org/floyd/REDparameters.txt
117 */
3d0b7439 118 if (!opt.qth_max)
a4eca97c
SH
119 opt.qth_max = opt.limit / 4;
120 if (!opt.qth_min)
121 opt.qth_min = opt.qth_max / 3;
122 if (!burst)
123 burst = (2 * opt.qth_min + opt.qth_max) / 3;
124
125 if (opt.qth_max > opt.limit) {
126 fprintf(stderr, "\"max\" is larger than \"limit\"\n");
127 return -1;
128 }
129
9f1ba570 130 if (opt.qth_min >= opt.qth_max) {
a4eca97c
SH
131 fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
132 return -1;
133 }
134
135 wlog = tc_red_eval_ewma(opt.qth_min*avpkt, burst, avpkt);
136 if (wlog < 0) {
137 fprintf(stderr, "CHOKE: failed to calculate EWMA constant.\n");
138 return -1;
139 }
140 if (wlog >= 10)
ab15aeac 141 fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst);
a4eca97c
SH
142 opt.Wlog = wlog;
143
144 wlog = tc_red_eval_P(opt.qth_min*avpkt, opt.qth_max*avpkt, probability);
145 if (wlog < 0) {
146 fprintf(stderr, "CHOKE: failed to calculate probability.\n");
147 return -1;
148 }
149 opt.Plog = wlog;
150
151 wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf);
152 if (wlog < 0) {
153 fprintf(stderr, "CHOKE: failed to calculate idle damping table.\n");
154 return -1;
155 }
156 opt.Scell_log = wlog;
157 if (ecn_ok)
158 opt.flags |= TC_RED_ECN;
159
c14f9d92 160 tail = addattr_nest(n, 1024, TCA_OPTIONS);
a4eca97c
SH
161 addattr_l(n, 1024, TCA_CHOKE_PARMS, &opt, sizeof(opt));
162 addattr_l(n, 1024, TCA_CHOKE_STAB, sbuf, 256);
650252d8
ED
163 max_P = probability * pow(2, 32);
164 addattr_l(n, 1024, TCA_CHOKE_MAX_P, &max_P, sizeof(max_P));
c14f9d92 165 addattr_nest_end(n, tail);
a4eca97c
SH
166 return 0;
167}
168
169static int choke_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
170{
650252d8 171 struct rtattr *tb[TCA_CHOKE_MAX+1];
a4eca97c 172 const struct tc_red_qopt *qopt;
650252d8 173 __u32 max_P = 0;
a4eca97c
SH
174
175 if (opt == NULL)
176 return 0;
177
650252d8 178 parse_rtattr_nested(tb, TCA_CHOKE_MAX, opt);
a4eca97c
SH
179
180 if (tb[TCA_CHOKE_PARMS] == NULL)
181 return -1;
182 qopt = RTA_DATA(tb[TCA_CHOKE_PARMS]);
183 if (RTA_PAYLOAD(tb[TCA_CHOKE_PARMS]) < sizeof(*qopt))
184 return -1;
650252d8
ED
185 if (tb[TCA_CHOKE_MAX_P] &&
186 RTA_PAYLOAD(tb[TCA_CHOKE_MAX_P]) >= sizeof(__u32))
ff24746c 187 max_P = rta_getattr_u32(tb[TCA_CHOKE_MAX_P]);
a4eca97c
SH
188
189 fprintf(f, "limit %up min %up max %up ",
190 qopt->limit, qopt->qth_min, qopt->qth_max);
191
33021752 192 tc_red_print_flags(qopt->flags);
a4eca97c
SH
193
194 if (show_details) {
650252d8
ED
195 fprintf(f, "ewma %u ", qopt->Wlog);
196 if (max_P)
197 fprintf(f, "probability %g ", max_P / pow(2, 32));
198 else
199 fprintf(f, "Plog %u ", qopt->Plog);
200 fprintf(f, "Scell_log %u", qopt->Scell_log);
a4eca97c
SH
201 }
202 return 0;
203}
204
205static int choke_print_xstats(struct qdisc_util *qu, FILE *f,
206 struct rtattr *xstats)
207{
208 struct tc_choke_xstats *st;
209
210 if (xstats == NULL)
211 return 0;
212
213 if (RTA_PAYLOAD(xstats) < sizeof(*st))
214 return -1;
215
216 st = RTA_DATA(xstats);
217 fprintf(f, " marked %u early %u pdrop %u other %u matched %u",
218 st->marked, st->early, st->pdrop, st->other, st->matched);
219 return 0;
220
221}
222
223struct qdisc_util choke_qdisc_util = {
224 .id = "choke",
225 .parse_qopt = choke_parse_opt,
226 .print_qopt = choke_print_opt,
227 .print_xstats = choke_print_xstats,
228};