]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_gred.c
tc: red, gred, tbf: more helpful error messages
[mirror_iproute2.git] / tc / q_gred.c
1 /*
2 * q_gred.c GRED.
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: J Hadi Salim(hadi@nortelnetworks.com)
10 * code ruthlessly ripped from
11 * Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 *
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <syslog.h>
19 #include <fcntl.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <string.h>
24
25 #include "utils.h"
26 #include "tc_util.h"
27
28 #include "tc_red.h"
29
30
31 #if 0
32 #define DPRINTF(format,args...) fprintf(stderr,format,##args)
33 #else
34 #define DPRINTF(format,args...)
35 #endif
36
37 static void explain(void)
38 {
39 fprintf(stderr, "Usage: ... gred DP drop-probability limit BYTES "
40 "min BYTES max BYTES\n");
41 fprintf(stderr, " avpkt BYTES burst PACKETS probability PROBABILITY "
42 "bandwidth KBPS\n");
43 fprintf(stderr, " [prio value]\n");
44 fprintf(stderr," OR ...\n");
45 fprintf(stderr," gred setup DPs <num of DPs> default <default DP> "
46 "[grio]\n");
47 }
48
49 #define usage() return(-1)
50
51 static int init_gred(struct qdisc_util *qu, int argc, char **argv,
52 struct nlmsghdr *n)
53 {
54
55 struct rtattr *tail;
56 struct tc_gred_sopt opt;
57 int dps = 0;
58 int def_dp = -1;
59
60 while (argc > 0) {
61 DPRINTF(stderr,"init_gred: invoked with %s\n",*argv);
62 if (strcmp(*argv, "DPs") == 0) {
63 NEXT_ARG();
64 DPRINTF(stderr,"init_gred: next_arg with %s\n",*argv);
65 dps = strtol(*argv, (char **)NULL, 10);
66 if (dps < 0 || dps >MAX_DPs) {
67 fprintf(stderr, "DPs =%d\n", dps);
68 fprintf(stderr, "Illegal \"DPs\"\n");
69 fprintf(stderr, "GRED: only %d DPs are "
70 "currently supported\n",MAX_DPs);
71 return -1;
72 }
73 } else if (strcmp(*argv, "default") == 0) {
74 NEXT_ARG();
75 def_dp = strtol(*argv, (char **)NULL, 10);
76 if (dps == 0) {
77 fprintf(stderr, "\"default DP\" must be "
78 "defined after DPs\n");
79 return -1;
80 }
81 if (def_dp < 0 || def_dp > dps) {
82 fprintf(stderr,
83 "\"default DP\" must be less than %d\n",
84 opt.DPs);
85 return -1;
86 }
87 } else if (strcmp(*argv, "grio") == 0) {
88 opt.grio=1;
89 } else if (strcmp(*argv, "help") == 0) {
90 explain();
91 return -1;
92 } else {
93 fprintf(stderr, "What is \"%s\"?\n", *argv);
94 explain();
95 return -1;
96 }
97 argc--; argv++;
98 }
99
100 if (!dps || def_dp == -1) {
101 fprintf(stderr, "Illegal gred setup parameters \n");
102 return -1;
103 }
104
105 memset(&opt, 0, sizeof(struct tc_gred_sopt));
106 opt.DPs = dps;
107 opt.def_DP = def_dp;
108
109 DPRINTF("TC_GRED: sending DPs=%d default=%d\n",opt.DPs,opt.def_DP);
110 n->nlmsg_flags|=NLM_F_CREATE;
111 tail = NLMSG_TAIL(n);
112 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
113 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
114 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
115 return 0;
116 }
117 /*
118 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119 */
120 static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
121 {
122 int ok=0;
123 struct tc_gred_qopt opt;
124 unsigned burst = 0;
125 unsigned avpkt = 0;
126 double probability = 0.02;
127 unsigned rate = 0;
128 int wlog;
129 __u8 sbuf[256];
130 struct rtattr *tail;
131
132 memset(&opt, 0, sizeof(opt));
133
134 while (argc > 0) {
135 if (strcmp(*argv, "limit") == 0) {
136 NEXT_ARG();
137 if (get_size(&opt.limit, *argv)) {
138 fprintf(stderr, "Illegal \"limit\"\n");
139 return -1;
140 }
141 ok++;
142 } else if (strcmp(*argv, "setup") == 0) {
143 if (ok) {
144 fprintf(stderr, "Illegal \"setup\"\n");
145 return -1;
146 }
147 return init_gred(qu,argc-1, argv+1,n);
148
149 } else if (strcmp(*argv, "min") == 0) {
150 NEXT_ARG();
151 if (get_size(&opt.qth_min, *argv)) {
152 fprintf(stderr, "Illegal \"min\"\n");
153 return -1;
154 }
155 ok++;
156 } else if (strcmp(*argv, "max") == 0) {
157 NEXT_ARG();
158 if (get_size(&opt.qth_max, *argv)) {
159 fprintf(stderr, "Illegal \"max\"\n");
160 return -1;
161 }
162 ok++;
163 } else if (strcmp(*argv, "DP") == 0) {
164 NEXT_ARG();
165 opt.DP=strtol(*argv, (char **)NULL, 10);
166 DPRINTF ("\n ******* DP =%u\n",opt.DP);
167 if (opt.DP >MAX_DPs) { /* need a better error check */
168 fprintf(stderr, "DP =%u \n",opt.DP);
169 fprintf(stderr, "Illegal \"DP\"\n");
170 fprintf(stderr, "GRED: only %d DPs are currently supported\n",MAX_DPs);
171 return -1;
172 }
173 ok++;
174 } else if (strcmp(*argv, "burst") == 0) {
175 NEXT_ARG();
176 if (get_unsigned(&burst, *argv, 0)) {
177 fprintf(stderr, "Illegal \"burst\"\n");
178 return -1;
179 }
180 ok++;
181 } else if (strcmp(*argv, "avpkt") == 0) {
182 NEXT_ARG();
183 if (get_size(&avpkt, *argv)) {
184 fprintf(stderr, "Illegal \"avpkt\"\n");
185 return -1;
186 }
187 ok++;
188 } else if (strcmp(*argv, "probability") == 0) {
189 NEXT_ARG();
190 if (sscanf(*argv, "%lg", &probability) != 1) {
191 fprintf(stderr, "Illegal \"probability\"\n");
192 return -1;
193 }
194 ok++;
195 } else if (strcmp(*argv, "prio") == 0) {
196 NEXT_ARG();
197 opt.prio=strtol(*argv, (char **)NULL, 10);
198 /* some error check here */
199 ok++;
200 } else if (strcmp(*argv, "bandwidth") == 0) {
201 NEXT_ARG();
202 if (get_rate(&rate, *argv)) {
203 fprintf(stderr, "Illegal \"bandwidth\"\n");
204 return -1;
205 }
206 ok++;
207 } else if (strcmp(*argv, "help") == 0) {
208 explain();
209 return -1;
210 } else {
211 fprintf(stderr, "What is \"%s\"?\n", *argv);
212 explain();
213 return -1;
214 }
215 argc--; argv++;
216 }
217
218 if (rate == 0)
219 get_rate(&rate, "10Mbit");
220
221 if (!opt.qth_min || !opt.qth_max || !burst || !opt.limit || !avpkt ||
222 (opt.DP<0)) {
223 fprintf(stderr, "Required parameter (min, max, burst, limit, "
224 "avpkt, DP) is missing\n");
225 return -1;
226 }
227
228 if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
229 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
230 return -1;
231 }
232 if (wlog >= 10)
233 fprintf(stderr, "GRED: WARNING. Burst %d seems to be to "
234 "large.\n", burst);
235 opt.Wlog = wlog;
236 if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
237 fprintf(stderr, "GRED: failed to calculate probability.\n");
238 return -1;
239 }
240 opt.Plog = wlog;
241 if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
242 {
243 fprintf(stderr, "GRED: failed to calculate idle damping "
244 "table.\n");
245 return -1;
246 }
247 opt.Scell_log = wlog;
248
249 tail = NLMSG_TAIL(n);
250 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
251 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
252 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
253 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
254 return 0;
255 }
256
257 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
258 {
259 struct rtattr *tb[TCA_GRED_STAB+1];
260 struct tc_gred_qopt *qopt;
261 int i;
262 SPRINT_BUF(b1);
263 SPRINT_BUF(b2);
264 SPRINT_BUF(b3);
265 SPRINT_BUF(b4);
266 SPRINT_BUF(b5);
267
268 if (opt == NULL)
269 return 0;
270
271 parse_rtattr_nested(tb, TCA_GRED_STAB, opt);
272
273 if (tb[TCA_GRED_PARMS] == NULL)
274 return -1;
275
276 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
277 if (RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
278 fprintf(f,"\n GRED received message smaller than expected\n");
279 return -1;
280 }
281
282 /* Bad hack! should really return a proper message as shown above*/
283
284 for (i=0;i<MAX_DPs;i++, qopt++) {
285 if (qopt->DP >= MAX_DPs) continue;
286 fprintf(f, "\n DP:%d (prio %d) Average Queue %s Measured "
287 "Queue %s ",
288 qopt->DP,
289 qopt->prio,
290 sprint_size(qopt->qave, b4),
291 sprint_size(qopt->backlog, b5));
292 fprintf(f, "\n\t Packet drops: %d (forced %d early %d) ",
293 qopt->forced+qopt->early,
294 qopt->forced,
295 qopt->early);
296 fprintf(f, "\n\t Packet totals: %u (bytes %u) ",
297 qopt->packets,
298 qopt->bytesin);
299 if (show_details)
300 fprintf(f, "\n limit %s min %s max %s ",
301 sprint_size(qopt->limit, b1),
302 sprint_size(qopt->qth_min, b2),
303 sprint_size(qopt->qth_max, b3));
304 fprintf(f, "ewma %u Plog %u Scell_log %u",
305 qopt->Wlog, qopt->Plog, qopt->Scell_log);
306 }
307 return 0;
308 }
309
310 struct qdisc_util gred_qdisc_util = {
311 .id = "gred",
312 .parse_qopt = gred_parse_opt,
313 .print_qopt = gred_print_opt,
314 };