]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/q_red.c
man: tc-taprio.8: fix syntax error
[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
859af0a5
SH
35static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv,
36 struct nlmsghdr *n, const char *dev)
aba5acdf 37{
d17b136f 38 struct tc_red_qopt opt = {};
32a121cb
SH
39 unsigned int burst = 0;
40 unsigned int avpkt = 0;
aba5acdf 41 double probability = 0.02;
32a121cb 42 unsigned int rate = 0;
9d9a67c7 43 int parm;
aba5acdf 44 __u8 sbuf[256];
e7e4abea 45 __u32 max_P;
aba5acdf
SH
46 struct rtattr *tail;
47
aba5acdf
SH
48 while (argc > 0) {
49 if (strcmp(*argv, "limit") == 0) {
50 NEXT_ARG();
51 if (get_size(&opt.limit, *argv)) {
52 fprintf(stderr, "Illegal \"limit\"\n");
53 return -1;
54 }
aba5acdf
SH
55 } else if (strcmp(*argv, "min") == 0) {
56 NEXT_ARG();
57 if (get_size(&opt.qth_min, *argv)) {
58 fprintf(stderr, "Illegal \"min\"\n");
59 return -1;
60 }
aba5acdf
SH
61 } else if (strcmp(*argv, "max") == 0) {
62 NEXT_ARG();
63 if (get_size(&opt.qth_max, *argv)) {
64 fprintf(stderr, "Illegal \"max\"\n");
65 return -1;
66 }
aba5acdf
SH
67 } else if (strcmp(*argv, "burst") == 0) {
68 NEXT_ARG();
69 if (get_unsigned(&burst, *argv, 0)) {
70 fprintf(stderr, "Illegal \"burst\"\n");
71 return -1;
72 }
aba5acdf
SH
73 } else if (strcmp(*argv, "avpkt") == 0) {
74 NEXT_ARG();
75 if (get_size(&avpkt, *argv)) {
76 fprintf(stderr, "Illegal \"avpkt\"\n");
77 return -1;
78 }
aba5acdf
SH
79 } else if (strcmp(*argv, "probability") == 0) {
80 NEXT_ARG();
81 if (sscanf(*argv, "%lg", &probability) != 1) {
82 fprintf(stderr, "Illegal \"probability\"\n");
83 return -1;
84 }
aba5acdf
SH
85 } else if (strcmp(*argv, "bandwidth") == 0) {
86 NEXT_ARG();
927e3cfb
ND
87 if (strchr(*argv, '%')) {
88 if (get_percent_rate(&rate, *argv, dev)) {
89 fprintf(stderr, "Illegal \"bandwidth\"\n");
90 return -1;
91 }
92 } else if (get_rate(&rate, *argv)) {
aba5acdf
SH
93 fprintf(stderr, "Illegal \"bandwidth\"\n");
94 return -1;
95 }
aba5acdf 96 } else if (strcmp(*argv, "ecn") == 0) {
841fc7bc
ED
97 opt.flags |= TC_RED_ECN;
98 } else if (strcmp(*argv, "harddrop") == 0) {
99 opt.flags |= TC_RED_HARDDROP;
e7e4abea
ED
100 } else if (strcmp(*argv, "adaptative") == 0) {
101 opt.flags |= TC_RED_ADAPTATIVE;
54a2fce8
ED
102 } else if (strcmp(*argv, "adaptive") == 0) {
103 opt.flags |= TC_RED_ADAPTATIVE;
aba5acdf
SH
104 } else if (strcmp(*argv, "help") == 0) {
105 explain();
106 return -1;
107 } else {
108 fprintf(stderr, "What is \"%s\"?\n", *argv);
109 explain();
110 return -1;
111 }
112 argc--; argv++;
113 }
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);
d93c909a
DW
129 if (!rate) {
130 get_rate(&rate, "10Mbit");
131 fprintf(stderr, "RED: set bandwidth to 10Mbit\n");
132 }
9d9a67c7 133 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
aba5acdf
SH
134 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
135 return -1;
136 }
9d9a67c7 137 if (parm >= 10)
6c99695d 138 fprintf(stderr, "RED: WARNING. Burst %u seems to be too large.\n", burst);
9d9a67c7
DW
139 opt.Wlog = parm;
140 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
aba5acdf
SH
141 fprintf(stderr, "RED: failed to calculate probability.\n");
142 return -1;
143 }
9d9a67c7
DW
144 opt.Plog = parm;
145 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
aba5acdf
SH
146 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
147 return -1;
148 }
9d9a67c7 149 opt.Scell_log = parm;
aba5acdf 150
c14f9d92 151 tail = addattr_nest(n, 1024, TCA_OPTIONS);
aba5acdf
SH
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));
c14f9d92 156 addattr_nest_end(n, 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
097415d5
JK
185 print_uint(PRINT_JSON, "limit", NULL, qopt->limit);
186 print_string(PRINT_FP, NULL, "limit %s ", sprint_size(qopt->limit, b1));
187 print_uint(PRINT_JSON, "min", NULL, qopt->qth_min);
188 print_string(PRINT_FP, NULL, "min %s ", sprint_size(qopt->qth_min, b2));
189 print_uint(PRINT_JSON, "max", NULL, qopt->qth_max);
190 print_string(PRINT_FP, NULL, "max %s ", sprint_size(qopt->qth_max, b3));
191
33021752
JK
192 tc_red_print_flags(qopt->flags);
193
aba5acdf 194 if (show_details) {
097415d5 195 print_uint(PRINT_ANY, "ewma", "ewma %u ", qopt->Wlog);
e7e4abea 196 if (max_P)
097415d5
JK
197 print_float(PRINT_ANY, "probability",
198 "probability %lg ", max_P / pow(2, 32));
e7e4abea 199 else
097415d5
JK
200 print_uint(PRINT_ANY, "Plog", "Plog %u ", qopt->Plog);
201 print_uint(PRINT_ANY, "Scell_log", "Scell_log %u",
202 qopt->Scell_log);
aba5acdf
SH
203 }
204 return 0;
205}
206
207static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
208{
209#ifdef TC_RED_ECN
210 struct tc_red_xstats *st;
211
212 if (xstats == NULL)
213 return 0;
214
215 if (RTA_PAYLOAD(xstats) < sizeof(*st))
216 return -1;
217
218 st = RTA_DATA(xstats);
097415d5
JK
219 print_uint(PRINT_ANY, "marked", " marked %u ", st->marked);
220 print_uint(PRINT_ANY, "early", "early %u ", st->early);
221 print_uint(PRINT_ANY, "pdrop", "pdrop %u ", st->pdrop);
222 print_uint(PRINT_ANY, "other", "other %u ", st->other);
aba5acdf
SH
223#endif
224 return 0;
225}
226
227
95812b56 228struct qdisc_util red_qdisc_util = {
f2f99e2e
SH
229 .id = "red",
230 .parse_qopt = red_parse_opt,
231 .print_qopt = red_print_opt,
232 .print_xstats = red_print_xstats,
aba5acdf 233};