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