]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_red.c
tc: B.W limits can now be specified in %.
[mirror_iproute2.git] / tc / q_red.c
CommitLineData
aba5acdf
SH
1/*
2 * q_red.c RED.
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>
d798a048 21#include <math.h>
aba5acdf
SH
22
23#include "utils.h"
24#include "tc_util.h"
25
26#include "tc_red.h"
27
28static void explain(void)
29{
841fc7bc 30 fprintf(stderr, "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n");
7bf17a22 31 fprintf(stderr, " [adaptive] [probability PROBABILITY] [bandwidth KBPS]\n");
e7e4abea 32 fprintf(stderr, " [ecn] [harddrop]\n");
aba5acdf
SH
33}
34
927e3cfb 35static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n, const char *dev)
aba5acdf 36{
d17b136f 37 struct tc_red_qopt opt = {};
32a121cb
SH
38 unsigned int burst = 0;
39 unsigned int avpkt = 0;
aba5acdf 40 double probability = 0.02;
32a121cb 41 unsigned int rate = 0;
9d9a67c7 42 int parm;
aba5acdf 43 __u8 sbuf[256];
e7e4abea 44 __u32 max_P;
aba5acdf
SH
45 struct rtattr *tail;
46
aba5acdf
SH
47 while (argc > 0) {
48 if (strcmp(*argv, "limit") == 0) {
49 NEXT_ARG();
50 if (get_size(&opt.limit, *argv)) {
51 fprintf(stderr, "Illegal \"limit\"\n");
52 return -1;
53 }
aba5acdf
SH
54 } else if (strcmp(*argv, "min") == 0) {
55 NEXT_ARG();
56 if (get_size(&opt.qth_min, *argv)) {
57 fprintf(stderr, "Illegal \"min\"\n");
58 return -1;
59 }
aba5acdf
SH
60 } else if (strcmp(*argv, "max") == 0) {
61 NEXT_ARG();
62 if (get_size(&opt.qth_max, *argv)) {
63 fprintf(stderr, "Illegal \"max\"\n");
64 return -1;
65 }
aba5acdf
SH
66 } else if (strcmp(*argv, "burst") == 0) {
67 NEXT_ARG();
68 if (get_unsigned(&burst, *argv, 0)) {
69 fprintf(stderr, "Illegal \"burst\"\n");
70 return -1;
71 }
aba5acdf
SH
72 } else if (strcmp(*argv, "avpkt") == 0) {
73 NEXT_ARG();
74 if (get_size(&avpkt, *argv)) {
75 fprintf(stderr, "Illegal \"avpkt\"\n");
76 return -1;
77 }
aba5acdf
SH
78 } else if (strcmp(*argv, "probability") == 0) {
79 NEXT_ARG();
80 if (sscanf(*argv, "%lg", &probability) != 1) {
81 fprintf(stderr, "Illegal \"probability\"\n");
82 return -1;
83 }
aba5acdf
SH
84 } else if (strcmp(*argv, "bandwidth") == 0) {
85 NEXT_ARG();
927e3cfb
ND
86 if (strchr(*argv, '%')) {
87 if (get_percent_rate(&rate, *argv, dev)) {
88 fprintf(stderr, "Illegal \"bandwidth\"\n");
89 return -1;
90 }
91 } else if (get_rate(&rate, *argv)) {
aba5acdf
SH
92 fprintf(stderr, "Illegal \"bandwidth\"\n");
93 return -1;
94 }
aba5acdf 95 } else if (strcmp(*argv, "ecn") == 0) {
841fc7bc
ED
96 opt.flags |= TC_RED_ECN;
97 } else if (strcmp(*argv, "harddrop") == 0) {
98 opt.flags |= TC_RED_HARDDROP;
e7e4abea
ED
99 } else if (strcmp(*argv, "adaptative") == 0) {
100 opt.flags |= TC_RED_ADAPTATIVE;
54a2fce8
ED
101 } else if (strcmp(*argv, "adaptive") == 0) {
102 opt.flags |= TC_RED_ADAPTATIVE;
aba5acdf
SH
103 } else if (strcmp(*argv, "help") == 0) {
104 explain();
105 return -1;
106 } else {
107 fprintf(stderr, "What is \"%s\"?\n", *argv);
108 explain();
109 return -1;
110 }
111 argc--; argv++;
112 }
113
841fc7bc
ED
114 if (!opt.limit || !avpkt) {
115 fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
aba5acdf
SH
116 return -1;
117 }
841fc7bc
ED
118 /* Compute default min/max thresholds based on
119 * Sally Floyd's recommendations:
120 * http://www.icir.org/floyd/REDparameters.txt
121 */
122 if (!opt.qth_max)
123 opt.qth_max = opt.qth_min ? opt.qth_min * 3 : opt.limit / 4;
124 if (!opt.qth_min)
125 opt.qth_min = opt.qth_max / 3;
126 if (!burst)
ab15aeac 127 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
d93c909a
DW
128 if (!rate) {
129 get_rate(&rate, "10Mbit");
130 fprintf(stderr, "RED: set bandwidth to 10Mbit\n");
131 }
9d9a67c7 132 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
aba5acdf
SH
133 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
134 return -1;
135 }
9d9a67c7 136 if (parm >= 10)
6c99695d 137 fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst);
9d9a67c7
DW
138 opt.Wlog = parm;
139 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
aba5acdf
SH
140 fprintf(stderr, "RED: failed to calculate probability.\n");
141 return -1;
142 }
9d9a67c7
DW
143 opt.Plog = parm;
144 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
aba5acdf
SH
145 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
146 return -1;
147 }
9d9a67c7 148 opt.Scell_log = parm;
aba5acdf 149
228569c3 150 tail = NLMSG_TAIL(n);
aba5acdf
SH
151 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
152 addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
153 addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
e7e4abea
ED
154 max_P = probability * pow(2, 32);
155 addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P));
228569c3 156 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
aba5acdf
SH
157 return 0;
158}
159
160static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
161{
841fc7bc 162 struct rtattr *tb[TCA_RED_MAX + 1];
aba5acdf 163 struct tc_red_qopt *qopt;
e7e4abea 164 __u32 max_P = 0;
32a121cb 165
aba5acdf
SH
166 SPRINT_BUF(b1);
167 SPRINT_BUF(b2);
168 SPRINT_BUF(b3);
169
170 if (opt == NULL)
171 return 0;
172
841fc7bc 173 parse_rtattr_nested(tb, TCA_RED_MAX, opt);
aba5acdf
SH
174
175 if (tb[TCA_RED_PARMS] == NULL)
176 return -1;
177 qopt = RTA_DATA(tb[TCA_RED_PARMS]);
178 if (RTA_PAYLOAD(tb[TCA_RED_PARMS]) < sizeof(*qopt))
179 return -1;
e7e4abea
ED
180
181 if (tb[TCA_RED_MAX_P] &&
182 RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
ff24746c 183 max_P = rta_getattr_u32(tb[TCA_RED_MAX_P]);
e7e4abea 184
aba5acdf
SH
185 fprintf(f, "limit %s min %s max %s ",
186 sprint_size(qopt->limit, b1),
187 sprint_size(qopt->qth_min, b2),
188 sprint_size(qopt->qth_max, b3));
aba5acdf
SH
189 if (qopt->flags & TC_RED_ECN)
190 fprintf(f, "ecn ");
841fc7bc
ED
191 if (qopt->flags & TC_RED_HARDDROP)
192 fprintf(f, "harddrop ");
e7e4abea 193 if (qopt->flags & TC_RED_ADAPTATIVE)
54a2fce8 194 fprintf(f, "adaptive ");
aba5acdf 195 if (show_details) {
e7e4abea
ED
196 fprintf(f, "ewma %u ", qopt->Wlog);
197 if (max_P)
198 fprintf(f, "probability %lg ", max_P / pow(2, 32));
199 else
200 fprintf(f, "Plog %u ", qopt->Plog);
201 fprintf(f, "Scell_log %u", qopt->Scell_log);
aba5acdf
SH
202 }
203 return 0;
204}
205
206static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
207{
208#ifdef TC_RED_ECN
209 struct tc_red_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",
219 st->marked, st->early, st->pdrop, st->other);
220 return 0;
ae665a52 221
aba5acdf
SH
222#endif
223 return 0;
224}
225
226
95812b56 227struct qdisc_util red_qdisc_util = {
f2f99e2e
SH
228 .id = "red",
229 .parse_qopt = red_parse_opt,
230 .print_qopt = red_print_opt,
231 .print_xstats = red_print_xstats,
aba5acdf 232};