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