]> rtime.felk.cvut.cz Git - lisovros/iproute2_canprio.git/blob - tc/q_red.c
tc: red, gred, tbf: more helpful error messages
[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 ]\n");
32 }
33
34 #define usage() return(-1)
35
36 static int red_parse_opt(struct qdisc_util *qu, int argc, char **argv, struct nlmsghdr *n)
37 {
38         struct tc_red_qopt opt;
39         unsigned burst = 0;
40         unsigned avpkt = 0;
41         double probability = 0.02;
42         unsigned rate = 0;
43         int ecn_ok = 0;
44         int wlog;
45         __u8 sbuf[256];
46         struct rtattr *tail;
47
48         memset(&opt, 0, sizeof(opt));
49
50         while (argc > 0) {
51                 if (strcmp(*argv, "limit") == 0) {
52                         NEXT_ARG();
53                         if (get_size(&opt.limit, *argv)) {
54                                 fprintf(stderr, "Illegal \"limit\"\n");
55                                 return -1;
56                         }
57                 } else if (strcmp(*argv, "min") == 0) {
58                         NEXT_ARG();
59                         if (get_size(&opt.qth_min, *argv)) {
60                                 fprintf(stderr, "Illegal \"min\"\n");
61                                 return -1;
62                         }
63                 } else if (strcmp(*argv, "max") == 0) {
64                         NEXT_ARG();
65                         if (get_size(&opt.qth_max, *argv)) {
66                                 fprintf(stderr, "Illegal \"max\"\n");
67                                 return -1;
68                         }
69                 } else if (strcmp(*argv, "burst") == 0) {
70                         NEXT_ARG();
71                         if (get_unsigned(&burst, *argv, 0)) {
72                                 fprintf(stderr, "Illegal \"burst\"\n");
73                                 return -1;
74                         }
75                 } else if (strcmp(*argv, "avpkt") == 0) {
76                         NEXT_ARG();
77                         if (get_size(&avpkt, *argv)) {
78                                 fprintf(stderr, "Illegal \"avpkt\"\n");
79                                 return -1;
80                         }
81                 } else if (strcmp(*argv, "probability") == 0) {
82                         NEXT_ARG();
83                         if (sscanf(*argv, "%lg", &probability) != 1) {
84                                 fprintf(stderr, "Illegal \"probability\"\n");
85                                 return -1;
86                         }
87                 } else if (strcmp(*argv, "bandwidth") == 0) {
88                         NEXT_ARG();
89                         if (get_rate(&rate, *argv)) {
90                                 fprintf(stderr, "Illegal \"bandwidth\"\n");
91                                 return -1;
92                         }
93                 } else if (strcmp(*argv, "ecn") == 0) {
94                         ecn_ok = 1;
95                 } else if (strcmp(*argv, "help") == 0) {
96                         explain();
97                         return -1;
98                 } else {
99                         fprintf(stderr, "What is \"%s\"?\n", *argv);
100                         explain();
101                         return -1;
102                 }
103                 argc--; argv++;
104         }
105
106         if (rate == 0)
107                 get_rate(&rate, "10Mbit");
108
109         if (!opt.qth_min || !opt.qth_max || !burst || !opt.limit || !avpkt) {
110                 fprintf(stderr, "Required parameter (min, max, burst, limit, avpkt) is missing\n");
111                 return -1;
112         }
113
114         if ((wlog = tc_red_eval_ewma(opt.qth_min, burst, avpkt)) < 0) {
115                 fprintf(stderr, "RED: failed to calculate EWMA constant.\n");
116                 return -1;
117         }
118         if (wlog >= 10)
119                 fprintf(stderr, "RED: WARNING. Burst %d seems to be to large.\n", burst);
120         opt.Wlog = wlog;
121         if ((wlog = tc_red_eval_P(opt.qth_min, opt.qth_max, probability)) < 0) {
122                 fprintf(stderr, "RED: failed to calculate probability.\n");
123                 return -1;
124         }
125         opt.Plog = wlog;
126         if ((wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf)) < 0) {
127                 fprintf(stderr, "RED: failed to calculate idle damping table.\n");
128                 return -1;
129         }
130         opt.Scell_log = wlog;
131         if (ecn_ok) {
132 #ifdef TC_RED_ECN
133                 opt.flags |= TC_RED_ECN;
134 #else
135                 fprintf(stderr, "RED: ECN support is missing in this binary.\n");
136                 return -1;
137 #endif
138         }
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_STAB+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_STAB, 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 #ifdef TC_RED_ECN
171         if (qopt->flags & TC_RED_ECN)
172                 fprintf(f, "ecn ");
173 #endif
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 };