]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/q_red.c
0e5d2282980fa4fda40edbc08a59a1a564308416
[lisovros/iproute2_canprio.git] / tc / q_red.c
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>
16 #include <syslog.h>
17 #include <fcntl.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
21 #include <string.h>
22
23 #include "utils.h"
24 #include "tc_util.h"
25
26 #include "tc_red.h"
27
28 static void explain(void)
29 {
30         fprintf(stderr, "Usage: ... red limit BYTES [min BYTES] [max BYTES] avpkt BYTES [burst PACKETS]\n");
31         fprintf(stderr, "               [adaptative] [probability PROBABILITY] bandwidth KBPS\n");
32         fprintf(stderr, "               [ecn] [harddrop]\n");
33 }
34
35 static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
36 {
37         struct tc_red_qopt opt;
38         unsigned burst = 0;
39         unsigned avpkt = 0;
40         double probability = 0.02;
41         unsigned rate = 0;
42         int wlog;
43         __u8 sbuf[256];
44         __u32 max_P;
45         struct rtattr *tail;
46
47         memset(&opt, 0, sizeof(opt));
48
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                         }
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                         }
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                         }
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                         }
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                         }
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                         }
86                 } else if (strcmp(*argv, "bandwidth") == 0) {
87                         NEXT_ARG();
88                         if (get_rate(&rate, *argv)) {
89                                 fprintf(stderr, "Illegal \"bandwidth\"\n");
90                                 return -1;
91                         }
92                 } else if (strcmp(*argv, "ecn") == 0) {
93                         opt.flags |= TC_RED_ECN;
94                 } else if (strcmp(*argv, "harddrop") == 0) {
95                         opt.flags |= TC_RED_HARDDROP;
96                 } else if (strcmp(*argv, "adaptative") == 0) {
97                         opt.flags |= TC_RED_ADAPTATIVE;
98                 } else if (strcmp(*argv, "help") == 0) {
99                         explain();
100                         return -1;
101                 } else {
102                         fprintf(stderr, "What is \"%s\"?\n", *argv);
103                         explain();
104                         return -1;
105                 }
106                 argc--; argv++;
107         }
108
109         if (rate == 0)
110                 get_rate(&rate, "10Mbit");
111
112         if (!opt.limit || !avpkt) {
113                 fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
114                 return -1;
115         }
116         /* Compute default min/max thresholds based on
117          * Sally Floyd's recommendations:
118          * http://www.icir.org/floyd/REDparameters.txt
119          */
120         if (!opt.qth_max)
121                 opt.qth_max = opt.qth_min ? opt.qth_min * 3 : opt.limit / 4;
122         if (!opt.qth_min)
123                 opt.qth_min = opt.qth_max / 3;
124         if (!burst)
125                 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
126         if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
127                 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
128                 return -1;
129         }
130         if (wlog >= 10)
131                 fprintf(stderr, "RED: WARNING. Burst %d seems to be too large.\n", burst);
132         opt.Wlog = wlog;
133         if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
134                 fprintf(stderr, "RED: failed to calculate probability.\n");
135                 return -1;
136         }
137         opt.Plog = wlog;
138         if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
139                 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
140                 return -1;
141         }
142         opt.Scell_log = wlog;
143
144         tail = NLMSG_TAIL(n);
145         addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
146         addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
147         addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
148         max_P = probability * pow(2, 32);
149         addattr_l(n, 1024, TCA_RED_MAX_P, &max_P, sizeof(max_P));
150         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
151         return 0;
152 }
153
154 static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
155 {
156         struct rtattr *tb[TCA_RED_MAX + 1];
157         struct tc_red_qopt *qopt;
158         __u32 max_P = 0;
159         SPRINT_BUF(b1);
160         SPRINT_BUF(b2);
161         SPRINT_BUF(b3);
162
163         if (opt == NULL)
164                 return 0;
165
166         parse_rtattr_nested(tb, TCA_RED_MAX, opt);
167
168         if (tb[TCA_RED_PARMS] == NULL)
169                 return -1;
170         qopt = RTA_DATA(tb[TCA_RED_PARMS]);
171         if (RTA_PAYLOAD(tb[TCA_RED_PARMS])  < sizeof(*qopt))
172                 return -1;
173
174         if (tb[TCA_RED_MAX_P] &&
175             RTA_PAYLOAD(tb[TCA_RED_MAX_P]) >= sizeof(__u32))
176                 max_P = *(__u32 *)RTA_DATA(tb[TCA_RED_MAX_P]);
177
178         fprintf(f, "limit %s min %s max %s ",
179                 sprint_size(qopt->limit, b1),
180                 sprint_size(qopt->qth_min, b2),
181                 sprint_size(qopt->qth_max, b3));
182         if (qopt->flags & TC_RED_ECN)
183                 fprintf(f, "ecn ");
184         if (qopt->flags & TC_RED_HARDDROP)
185                 fprintf(f, "harddrop ");
186         if (qopt->flags & TC_RED_ADAPTATIVE)
187                 fprintf(f, "adaptative ");
188         if (show_details) {
189                 fprintf(f, "ewma %u ", qopt->Wlog);
190                 if (max_P)
191                         fprintf(f, "probability %lg ", max_P / pow(2, 32));
192                 else
193                         fprintf(f, "Plog %u ", qopt->Plog);
194                 fprintf(f, "Scell_log %u", qopt->Scell_log);
195         }
196         return 0;
197 }
198
199 static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
200 {
201 #ifdef TC_RED_ECN
202         struct tc_red_xstats *st;
203
204         if (xstats == NULL)
205                 return 0;
206
207         if (RTA_PAYLOAD(xstats) < sizeof(*st))
208                 return -1;
209
210         st = RTA_DATA(xstats);
211         fprintf(f, "  marked %u early %u pdrop %u other %u",
212                 st->marked, st->early, st->pdrop, st->other);
213         return 0;
214
215 #endif
216         return 0;
217 }
218
219
220 struct qdisc_util red_qdisc_util = {
221         .id             = "red",
222         .parse_qopt     = red_parse_opt,
223         .print_qopt     = red_print_opt,
224         .print_xstats   = red_print_xstats,
225 };