]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/q_red.c
18020d7d4b8c3acbb33d9cbed9f0f62476b9a573
[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, "               [probability PROBABILITY] bandwidth KBPS [ecn] [harddrop]\n");
32 }
33
34 static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
35 {
36         struct tc_red_qopt opt;
37         unsigned burst = 0;
38         unsigned avpkt = 0;
39         double probability = 0.02;
40         unsigned rate = 0;
41         int wlog;
42         __u8 sbuf[256];
43         struct rtattr *tail;
44
45         memset(&opt, 0, sizeof(opt));
46
47         while (argc > 0) {
48                 if (strcmp(*argv, "limit") == 0) {
49                         NEXT_ARG();
50                         if (get_size(&opt.limit, *argv)) {
51                                 fprintf(stderr, "Illegal \"limit\"\n");
52                                 return -1;
53                         }
54                 } else if (strcmp(*argv, "min") == 0) {
55                         NEXT_ARG();
56                         if (get_size(&opt.qth_min, *argv)) {
57                                 fprintf(stderr, "Illegal \"min\"\n");
58                                 return -1;
59                         }
60                 } else if (strcmp(*argv, "max") == 0) {
61                         NEXT_ARG();
62                         if (get_size(&opt.qth_max, *argv)) {
63                                 fprintf(stderr, "Illegal \"max\"\n");
64                                 return -1;
65                         }
66                 } else if (strcmp(*argv, "burst") == 0) {
67                         NEXT_ARG();
68                         if (get_unsigned(&burst, *argv, 0)) {
69                                 fprintf(stderr, "Illegal \"burst\"\n");
70                                 return -1;
71                         }
72                 } else if (strcmp(*argv, "avpkt") == 0) {
73                         NEXT_ARG();
74                         if (get_size(&avpkt, *argv)) {
75                                 fprintf(stderr, "Illegal \"avpkt\"\n");
76                                 return -1;
77                         }
78                 } else if (strcmp(*argv, "probability") == 0) {
79                         NEXT_ARG();
80                         if (sscanf(*argv, "%lg", &probability) != 1) {
81                                 fprintf(stderr, "Illegal \"probability\"\n");
82                                 return -1;
83                         }
84                 } else if (strcmp(*argv, "bandwidth") == 0) {
85                         NEXT_ARG();
86                         if (get_rate(&rate, *argv)) {
87                                 fprintf(stderr, "Illegal \"bandwidth\"\n");
88                                 return -1;
89                         }
90                 } else if (strcmp(*argv, "ecn") == 0) {
91                         opt.flags |= TC_RED_ECN;
92                 } else if (strcmp(*argv, "harddrop") == 0) {
93                         opt.flags |= TC_RED_HARDDROP;
94                 } else if (strcmp(*argv, "help") == 0) {
95                         explain();
96                         return -1;
97                 } else {
98                         fprintf(stderr, "What is \"%s\"?\n", *argv);
99                         explain();
100                         return -1;
101                 }
102                 argc--; argv++;
103         }
104
105         if (rate == 0)
106                 get_rate(&rate, "10Mbit");
107
108         if (!opt.limit || !avpkt) {
109                 fprintf(stderr, "RED: Required parameter (limit, avpkt) is missing\n");
110                 return -1;
111         }
112         /* Compute default min/max thresholds based on
113          * Sally Floyd's recommendations:
114          * http://www.icir.org/floyd/REDparameters.txt
115          */
116         if (!opt.qth_max)
117                 opt.qth_max = opt.qth_min ? opt.qth_min * 3 : opt.limit / 4;
118         if (!opt.qth_min)
119                 opt.qth_min = opt.qth_max / 3;
120         if (!burst)
121                 burst = (2 * opt.qth_min + opt.qth_max) / (3 * avpkt);
122         if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
123                 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
124                 return -1;
125         }
126         if (wlog >= 10)
127                 fprintf(stderr, "RED: WARNING. Burst %d seems to be too large.\n", burst);
128         opt.Wlog = wlog;
129         if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
130                 fprintf(stderr, "RED: failed to calculate probability.\n");
131                 return -1;
132         }
133         opt.Plog = wlog;
134         if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
135                 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
136                 return -1;
137         }
138         opt.Scell_log = wlog;
139
140         tail = NLMSG_TAIL(n);
141         addattr_l(n, 1024, TCA_OPTIONS, NULL, 0);
142         addattr_l(n, 1024, TCA_RED_PARMS, &opt, sizeof(opt));
143         addattr_l(n, 1024, TCA_RED_STAB, sbuf, 256);
144         tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail;
145         return 0;
146 }
147
148 static int red_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt)
149 {
150         struct rtattr *tb[TCA_RED_MAX + 1];
151         struct tc_red_qopt *qopt;
152         SPRINT_BUF(b1);
153         SPRINT_BUF(b2);
154         SPRINT_BUF(b3);
155
156         if (opt == NULL)
157                 return 0;
158
159         parse_rtattr_nested(tb, TCA_RED_MAX, opt);
160
161         if (tb[TCA_RED_PARMS] == NULL)
162                 return -1;
163         qopt = RTA_DATA(tb[TCA_RED_PARMS]);
164         if (RTA_PAYLOAD(tb[TCA_RED_PARMS])  < sizeof(*qopt))
165                 return -1;
166         fprintf(f, "limit %s min %s max %s ",
167                 sprint_size(qopt->limit, b1),
168                 sprint_size(qopt->qth_min, b2),
169                 sprint_size(qopt->qth_max, b3));
170         if (qopt->flags & TC_RED_ECN)
171                 fprintf(f, "ecn ");
172         if (qopt->flags & TC_RED_HARDDROP)
173                 fprintf(f, "harddrop ");
174         if (show_details) {
175                 fprintf(f, "ewma %u Plog %u Scell_log %u",
176                         qopt->Wlog, qopt->Plog, qopt->Scell_log);
177         }
178         return 0;
179 }
180
181 static int red_print_xstats(struct qdisc_util *qu, FILE *f, struct rtattr *xstats)
182 {
183 #ifdef TC_RED_ECN
184         struct tc_red_xstats *st;
185
186         if (xstats == NULL)
187                 return 0;
188
189         if (RTA_PAYLOAD(xstats) < sizeof(*st))
190                 return -1;
191
192         st = RTA_DATA(xstats);
193         fprintf(f, "  marked %u early %u pdrop %u other %u",
194                 st->marked, st->early, st->pdrop, st->other);
195         return 0;
196
197 #endif
198         return 0;
199 }
200
201
202 struct qdisc_util red_qdisc_util = {
203         .id             = "red",
204         .parse_qopt     = red_parse_opt,
205         .print_qopt     = red_print_opt,
206         .print_xstats   = red_print_xstats,
207 };