]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_choke.c
tc: B.W limits can now be specified in %.
[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{
29 fprintf(stderr, "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n");
30 fprintf(stderr, " [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n");
31}
32
33static int choke_parse_opt(struct qdisc_util *qu, int argc, char **argv,
927e3cfb 34 struct nlmsghdr *n, const char *dev)
a4eca97c 35{
d17b136f 36 struct tc_red_qopt opt = {};
32a121cb
SH
37 unsigned int burst = 0;
38 unsigned int avpkt = 1000;
a4eca97c 39 double probability = 0.02;
32a121cb 40 unsigned int rate = 0;
a4eca97c
SH
41 int ecn_ok = 0;
42 int wlog;
43 __u8 sbuf[256];
650252d8 44 __u32 max_P;
a4eca97c
SH
45 struct rtattr *tail;
46
a4eca97c
SH
47 while (argc > 0) {
48 if (strcmp(*argv, "limit") == 0) {
49 NEXT_ARG();
50 if (get_unsigned(&opt.limit, *argv, 0)) {
51 fprintf(stderr, "Illegal \"limit\"\n");
52 return -1;
53 }
54 } else if (strcmp(*argv, "bandwidth") == 0) {
55 NEXT_ARG();
927e3cfb
ND
56 if (strchr(*argv, '%')) {
57 if (get_percent_rate(&rate, *argv, dev)) {
58 fprintf(stderr, "Illegal \"bandwidth\"\n");
59 return -1;
60 }
61 } else if (get_rate(&rate, *argv)) {
a4eca97c
SH
62 fprintf(stderr, "Illegal \"bandwidth\"\n");
63 return -1;
64 }
65 } else if (strcmp(*argv, "ecn") == 0) {
66 ecn_ok = 1;
67 } else if (strcmp(*argv, "min") == 0) {
68 NEXT_ARG();
69 if (get_unsigned(&opt.qth_min, *argv, 0)) {
70 fprintf(stderr, "Illegal \"min\"\n");
71 return -1;
72 }
73 } else if (strcmp(*argv, "max") == 0) {
74 NEXT_ARG();
75 if (get_unsigned(&opt.qth_max, *argv, 0)) {
76 fprintf(stderr, "Illegal \"max\"\n");
77 return -1;
78 }
79 } else if (strcmp(*argv, "burst") == 0) {
80 NEXT_ARG();
81 if (get_unsigned(&burst, *argv, 0)) {
82 fprintf(stderr, "Illegal \"burst\"\n");
83 return -1;
84 }
85 } else if (strcmp(*argv, "avpkt") == 0) {
86 NEXT_ARG();
87 if (get_size(&avpkt, *argv)) {
88 fprintf(stderr, "Illegal \"avpkt\"\n");
89 return -1;
90 }
91 } else if (strcmp(*argv, "probability") == 0) {
92 NEXT_ARG();
93 if (sscanf(*argv, "%lg", &probability) != 1) {
94 fprintf(stderr, "Illegal \"probability\"\n");
95 return -1;
96 }
97 } else if (strcmp(*argv, "help") == 0) {
98 explain();
99 return -1;
100 } else {
101 fprintf(stderr, "What is \"%s\"?\n", *argv);
102 explain();
103 return -1;
104 }
105 argc--; argv++;
106 }
107
108 if (!rate || !opt.limit) {
109 fprintf(stderr, "Required parameter (bandwidth, limit) is missing\n");
110 return -1;
111 }
112
3d0b7439 113 /* Compute default min/max thresholds based on
a4eca97c
SH
114 Sally Floyd's recommendations:
115 http://www.icir.org/floyd/REDparameters.txt
116 */
3d0b7439 117 if (!opt.qth_max)
a4eca97c
SH
118 opt.qth_max = opt.limit / 4;
119 if (!opt.qth_min)
120 opt.qth_min = opt.qth_max / 3;
121 if (!burst)
122 burst = (2 * opt.qth_min + opt.qth_max) / 3;
123
124 if (opt.qth_max > opt.limit) {
125 fprintf(stderr, "\"max\" is larger than \"limit\"\n");
126 return -1;
127 }
128
9f1ba570 129 if (opt.qth_min >= opt.qth_max) {
a4eca97c
SH
130 fprintf(stderr, "\"min\" is not smaller than \"max\"\n");
131 return -1;
132 }
133
134 wlog = tc_red_eval_ewma(opt.qth_min*avpkt, burst, avpkt);
135 if (wlog < 0) {
136 fprintf(stderr, "CHOKE: failed to calculate EWMA constant.\n");
137 return -1;
138 }
139 if (wlog >= 10)
ab15aeac 140 fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be too large.\n", burst);
a4eca97c
SH
141 opt.Wlog = wlog;
142
143 wlog = tc_red_eval_P(opt.qth_min*avpkt, opt.qth_max*avpkt, probability);
144 if (wlog < 0) {
145 fprintf(stderr, "CHOKE: failed to calculate probability.\n");
146 return -1;
147 }
148 opt.Plog = wlog;
149
150 wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf);
151 if (wlog < 0) {
152 fprintf(stderr, "CHOKE: failed to calculate idle damping table.\n");
153 return -1;
154 }
155 opt.Scell_log = wlog;
156 if (ecn_ok)
157 opt.flags |= TC_RED_ECN;
158
159 tail = NLMSG_TAIL(n);
160 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
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));
a4eca97c
SH
165 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
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
192 if (qopt->flags & TC_RED_ECN)
193 fprintf(f, "ecn ");
194
195 if (show_details) {
650252d8
ED
196 fprintf(f, "ewma %u ", qopt->Wlog);
197 if (max_P)
198 fprintf(f, "probability %g ", max_P / pow(2, 32));
199 else
200 fprintf(f, "Plog %u ", qopt->Plog);
201 fprintf(f, "Scell_log %u", qopt->Scell_log);
a4eca97c
SH
202 }
203 return 0;
204}
205
206static int choke_print_xstats(struct qdisc_util *qu, FILE *f,
207 struct rtattr *xstats)
208{
209 struct tc_choke_xstats *st;
210
211 if (xstats == NULL)
212 return 0;
213
214 if (RTA_PAYLOAD(xstats) < sizeof(*st))
215 return -1;
216
217 st = RTA_DATA(xstats);
218 fprintf(f, " marked %u early %u pdrop %u other %u matched %u",
219 st->marked, st->early, st->pdrop, st->other, st->matched);
220 return 0;
221
222}
223
224struct qdisc_util choke_qdisc_util = {
225 .id = "choke",
226 .parse_qopt = choke_parse_opt,
227 .print_qopt = choke_print_opt,
228 .print_xstats = choke_print_xstats,
229};