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