]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/q_gred.c
tc: gred: Adopt the term VQ in the command syntax and output
[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 #include <math.h>
25
26 #include "utils.h"
27 #include "tc_util.h"
28
29 #include "tc_red.h"
30
31
32 #if 0
33 #define DPRINTF(format,args...) fprintf(stderr,format,##args)
34 #else
35 #define DPRINTF(format,args...)
36 #endif
37
38 static void explain(void)
39 {
40 fprintf(stderr, "Usage: tc qdisc { add | replace | change } ... gred setup vqs NUMBER\n");
41 fprintf(stderr, " default DEFAULT_VQ [ grio ]\n");
42 fprintf(stderr, " tc qdisc change ... gred vq VQ [ prio VALUE ] limit BYTES\n");
43 fprintf(stderr, " min BYTES max BYTES avpkt BYTES [ burst PACKETS ]\n");
44 fprintf(stderr, " [ probability PROBABILITY ] [ bandwidth KBPS ]\n");
45 }
46
47 static int init_gred(struct qdisc_util *qu, int argc, char **argv,
48 struct nlmsghdr *n)
49 {
50
51 struct rtattr *tail;
52 struct tc_gred_sopt opt = { 0 };
53
54 opt.def_DP = MAX_DPs;
55
56 while (argc > 0) {
57 DPRINTF(stderr,"init_gred: invoked with %s\n",*argv);
58 if (strcmp(*argv, "vqs") == 0 ||
59 strcmp(*argv, "DPs") == 0) {
60 NEXT_ARG();
61 if (get_unsigned(&opt.DPs, *argv, 10)) {
62 fprintf(stderr, "Illegal \"vqs\"\n");
63 return -1;
64 } else if (opt.DPs > MAX_DPs) {
65 fprintf(stderr, "GRED: only %u VQs are "
66 "currently supported\n", MAX_DPs);
67 return -1;
68 }
69 } else if (strcmp(*argv, "default") == 0) {
70 if (opt.DPs == 0) {
71 fprintf(stderr, "\"default\" must be defined "
72 "after \"vqs\"\n");
73 return -1;
74 }
75 NEXT_ARG();
76 if (get_unsigned(&opt.def_DP, *argv, 10)) {
77 fprintf(stderr, "Illegal \"default\"\n");
78 return -1;
79 } else if (opt.def_DP >= opt.DPs) {
80 fprintf(stderr, "\"default\" must be less than "
81 "\"vqs\"\n");
82 return -1;
83 }
84 } else if (strcmp(*argv, "grio") == 0) {
85 opt.grio = 1;
86 } else if (strcmp(*argv, "help") == 0) {
87 explain();
88 return -1;
89 } else {
90 fprintf(stderr, "What is \"%s\"?\n", *argv);
91 explain();
92 return -1;
93 }
94 argc--; argv++;
95 }
96
97 if (!opt.DPs || opt.def_DP == MAX_DPs) {
98 fprintf(stderr, "Illegal gred setup parameters \n");
99 return -1;
100 }
101
102 DPRINTF("TC_GRED: sending DPs=%u def_DP=%u\n",opt.DPs,opt.def_DP);
103 n->nlmsg_flags|=NLM_F_CREATE;
104 tail = NLMSG_TAIL(n);
105 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
106 addattr_l(n, 1024, TCA_GRED_DPS, &opt, sizeof(struct tc_gred_sopt));
107 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
108 return 0;
109 }
110 /*
111 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
112 */
113 static int gred_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
114 {
115 int ok=0;
116 struct tc_gred_qopt opt = { 0 };
117 unsigned burst = 0;
118 unsigned avpkt = 0;
119 double probability = 0.02;
120 unsigned rate = 0;
121 int parm;
122 __u8 sbuf[256];
123 struct rtattr *tail;
124 __u32 max_P;
125
126 opt.DP = MAX_DPs;
127
128 while (argc > 0) {
129 if (strcmp(*argv, "limit") == 0) {
130 NEXT_ARG();
131 if (get_size(&opt.limit, *argv)) {
132 fprintf(stderr, "Illegal \"limit\"\n");
133 return -1;
134 }
135 ok++;
136 } else if (strcmp(*argv, "setup") == 0) {
137 if (ok) {
138 fprintf(stderr, "Illegal \"setup\"\n");
139 return -1;
140 }
141 return init_gred(qu, argc-1, argv+1, n);
142 } else if (strcmp(*argv, "min") == 0) {
143 NEXT_ARG();
144 if (get_size(&opt.qth_min, *argv)) {
145 fprintf(stderr, "Illegal \"min\"\n");
146 return -1;
147 }
148 ok++;
149 } else if (strcmp(*argv, "max") == 0) {
150 NEXT_ARG();
151 if (get_size(&opt.qth_max, *argv)) {
152 fprintf(stderr, "Illegal \"max\"\n");
153 return -1;
154 }
155 ok++;
156 } else if (strcmp(*argv, "vq") == 0 ||
157 strcmp(*argv, "DP") == 0) {
158 NEXT_ARG();
159 if (get_unsigned(&opt.DP, *argv, 10)) {
160 fprintf(stderr, "Illegal \"vq\"\n");
161 return -1;
162 } else if (opt.DP >= MAX_DPs) {
163 fprintf(stderr, "GRED: only %u VQs are "
164 "currently supported\n", MAX_DPs);
165 return -1;
166 } /* need a better error check */
167 ok++;
168 } else if (strcmp(*argv, "burst") == 0) {
169 NEXT_ARG();
170 if (get_unsigned(&burst, *argv, 0)) {
171 fprintf(stderr, "Illegal \"burst\"\n");
172 return -1;
173 }
174 ok++;
175 } else if (strcmp(*argv, "avpkt") == 0) {
176 NEXT_ARG();
177 if (get_size(&avpkt, *argv)) {
178 fprintf(stderr, "Illegal \"avpkt\"\n");
179 return -1;
180 }
181 ok++;
182 } else if (strcmp(*argv, "probability") == 0) {
183 NEXT_ARG();
184 if (sscanf(*argv, "%lg", &probability) != 1) {
185 fprintf(stderr, "Illegal \"probability\"\n");
186 return -1;
187 }
188 ok++;
189 } else if (strcmp(*argv, "prio") == 0) {
190 NEXT_ARG();
191 opt.prio=strtol(*argv, (char **)NULL, 10);
192 /* some error check here */
193 ok++;
194 } else if (strcmp(*argv, "bandwidth") == 0) {
195 NEXT_ARG();
196 if (get_rate(&rate, *argv)) {
197 fprintf(stderr, "Illegal \"bandwidth\"\n");
198 return -1;
199 }
200 ok++;
201 } else if (strcmp(*argv, "help") == 0) {
202 explain();
203 return -1;
204 } else {
205 fprintf(stderr, "What is \"%s\"?\n", *argv);
206 explain();
207 return -1;
208 }
209 argc--; argv++;
210 }
211
212 if (!ok) {
213 explain();
214 return -1;
215 }
216 if (opt.DP == MAX_DPs || !opt.limit || !opt.qth_min || !opt.qth_max ||
217 !avpkt) {
218 fprintf(stderr, "Required parameter (vq, limit, min, max, "
219 "avpkt) is missing\n");
220 return -1;
221 }
222 if (!burst) {
223 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
224 fprintf(stderr, "GRED: set burst to %u\n", burst);
225 }
226 if (!rate) {
227 get_rate(&rate, "10Mbit");
228 fprintf(stderr, "GRED: set bandwidth to 10Mbit\n");
229 }
230 if ((parm = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
231 fprintf(stderr, "GRED: failed to calculate EWMA constant.\n");
232 return -1;
233 }
234 if (parm >= 10)
235 fprintf(stderr, "GRED: WARNING. Burst %u seems to be too "
236 "large.\n", burst);
237 opt.Wlog = parm;
238 if ((parm = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
239 fprintf(stderr, "GRED: failed to calculate probability.\n");
240 return -1;
241 }
242 opt.Plog = parm;
243 if ((parm = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0)
244 {
245 fprintf(stderr, "GRED: failed to calculate idle damping "
246 "table.\n");
247 return -1;
248 }
249 opt.Scell_log = parm;
250
251 tail = NLMSG_TAIL(n);
252 addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
253 addattr_l(n, 1024, TCA_GRED_PARMS, &opt, sizeof(opt));
254 addattr_l(n, 1024, TCA_GRED_STAB, sbuf, 256);
255 max_P = probability * pow(2, 32);
256 addattr32(n, 1024, TCA_GRED_MAX_P, max_P);
257 tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
258 return 0;
259 }
260
261 static int gred_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
262 {
263 struct rtattr *tb[TCA_GRED_MAX + 1];
264 struct tc_gred_sopt *sopt;
265 struct tc_gred_qopt *qopt;
266 __u32 *max_p = NULL;
267 unsigned i;
268 SPRINT_BUF(b1);
269 SPRINT_BUF(b2);
270 SPRINT_BUF(b3);
271
272 if (opt == NULL)
273 return 0;
274
275 parse_rtattr_nested(tb, TCA_GRED_MAX, opt);
276
277 if (tb[TCA_GRED_PARMS] == NULL)
278 return -1;
279
280 if (tb[TCA_GRED_MAX_P] &&
281 RTA_PAYLOAD(tb[TCA_GRED_MAX_P]) >= sizeof(__u32) * MAX_DPs)
282 max_p = RTA_DATA(tb[TCA_GRED_MAX_P]);
283
284 sopt = RTA_DATA(tb[TCA_GRED_DPS]);
285 qopt = RTA_DATA(tb[TCA_GRED_PARMS]);
286 if (RTA_PAYLOAD(tb[TCA_GRED_DPS]) < sizeof(*sopt) ||
287 RTA_PAYLOAD(tb[TCA_GRED_PARMS]) < sizeof(*qopt)*MAX_DPs) {
288 fprintf(f,"\n GRED received message smaller than expected\n");
289 return -1;
290 }
291
292 /* Bad hack! should really return a proper message as shown above*/
293
294 fprintf(f, "vqs %u default %u %s",
295 sopt->DPs,
296 sopt->def_DP,
297 sopt->grio ? "grio " : "");
298
299 for (i=0;i<MAX_DPs;i++, qopt++) {
300 if (qopt->DP >= MAX_DPs) continue;
301 fprintf(f, "\n vq %u prio %hhu limit %s min %s max %s ",
302 qopt->DP,
303 qopt->prio,
304 sprint_size(qopt->limit, b1),
305 sprint_size(qopt->qth_min, b2),
306 sprint_size(qopt->qth_max, b3));
307 if (show_details) {
308 fprintf(f, "ewma %u ", qopt->Wlog);
309 if (max_p)
310 fprintf(f, "probability %lg ", max_p[i] / pow(2, 32));
311 else
312 fprintf(f, "Plog %u ", qopt->Plog);
313 fprintf(f, "Scell_log %u ", qopt->Scell_log);
314 }
315 if (show_stats) {
316 fprintf(f, "\n Queue size: average %s current %s ",
317 sprint_size(qopt->qave, b1),
318 sprint_size(qopt->backlog, b2));
319 fprintf(f, "\n Dropped packets: forced %u early %u pdrop %u other %u ",
320 qopt->forced,
321 qopt->early,
322 qopt->pdrop,
323 qopt->other);
324 fprintf(f, "\n Total packets: %u (%s) ",
325 qopt->packets,
326 sprint_size(qopt->bytesin, b1));
327 }
328 }
329 return 0;
330 }
331
332 struct qdisc_util gred_qdisc_util = {
333 .id = "gred",
334 .parse_qopt = gred_parse_opt,
335 .print_qopt = gred_print_opt,
336 };