]>
Commit | Line | Data |
---|---|---|
a4eca97c SH |
1 | /* |
2 | * q_choke.c CHOKE. | |
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: Stephen Hemminger <shemminger@vyatta.com> | |
10 | */ | |
11 | ||
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <unistd.h> | |
15 | #include <syslog.h> | |
16 | #include <fcntl.h> | |
17 | #include <sys/socket.h> | |
18 | #include <netinet/in.h> | |
19 | #include <arpa/inet.h> | |
20 | #include <string.h> | |
21 | ||
22 | #include "utils.h" | |
23 | #include "tc_util.h" | |
24 | ||
25 | #include "tc_red.h" | |
26 | ||
27 | static void explain(void) | |
28 | { | |
29 | fprintf(stderr, "Usage: ... choke limit PACKETS bandwidth KBPS [ecn]\n"); | |
30 | fprintf(stderr, " [ min PACKETS ] [ max PACKETS ] [ burst PACKETS ]\n"); | |
31 | } | |
32 | ||
33 | static int choke_parse_opt(struct qdisc_util *qu, int argc, char **argv, | |
34 | struct nlmsghdr *n) | |
35 | { | |
36 | struct tc_red_qopt opt; | |
37 | unsigned burst = 0; | |
38 | unsigned avpkt = 1000; | |
39 | double probability = 0.02; | |
40 | unsigned rate = 0; | |
41 | int ecn_ok = 0; | |
42 | int wlog; | |
43 | __u8 sbuf[256]; | |
44 | struct rtattr *tail; | |
45 | ||
46 | memset(&opt, 0, sizeof(opt)); | |
47 | ||
48 | while (argc > 0) { | |
49 | if (strcmp(*argv, "limit") == 0) { | |
50 | NEXT_ARG(); | |
51 | if (get_unsigned(&opt.limit, *argv, 0)) { | |
52 | fprintf(stderr, "Illegal \"limit\"\n"); | |
53 | return -1; | |
54 | } | |
55 | } else if (strcmp(*argv, "bandwidth") == 0) { | |
56 | NEXT_ARG(); | |
57 | if (get_rate(&rate, *argv)) { | |
58 | fprintf(stderr, "Illegal \"bandwidth\"\n"); | |
59 | return -1; | |
60 | } | |
61 | } else if (strcmp(*argv, "ecn") == 0) { | |
62 | ecn_ok = 1; | |
63 | } else if (strcmp(*argv, "min") == 0) { | |
64 | NEXT_ARG(); | |
65 | if (get_unsigned(&opt.qth_min, *argv, 0)) { | |
66 | fprintf(stderr, "Illegal \"min\"\n"); | |
67 | return -1; | |
68 | } | |
69 | } else if (strcmp(*argv, "max") == 0) { | |
70 | NEXT_ARG(); | |
71 | if (get_unsigned(&opt.qth_max, *argv, 0)) { | |
72 | fprintf(stderr, "Illegal \"max\"\n"); | |
73 | return -1; | |
74 | } | |
75 | } else if (strcmp(*argv, "burst") == 0) { | |
76 | NEXT_ARG(); | |
77 | if (get_unsigned(&burst, *argv, 0)) { | |
78 | fprintf(stderr, "Illegal \"burst\"\n"); | |
79 | return -1; | |
80 | } | |
81 | } else if (strcmp(*argv, "avpkt") == 0) { | |
82 | NEXT_ARG(); | |
83 | if (get_size(&avpkt, *argv)) { | |
84 | fprintf(stderr, "Illegal \"avpkt\"\n"); | |
85 | return -1; | |
86 | } | |
87 | } else if (strcmp(*argv, "probability") == 0) { | |
88 | NEXT_ARG(); | |
89 | if (sscanf(*argv, "%lg", &probability) != 1) { | |
90 | fprintf(stderr, "Illegal \"probability\"\n"); | |
91 | return -1; | |
92 | } | |
93 | } else if (strcmp(*argv, "help") == 0) { | |
94 | explain(); | |
95 | return -1; | |
96 | } else { | |
97 | fprintf(stderr, "What is \"%s\"?\n", *argv); | |
98 | explain(); | |
99 | return -1; | |
100 | } | |
101 | argc--; argv++; | |
102 | } | |
103 | ||
104 | if (!rate || !opt.limit) { | |
105 | fprintf(stderr, "Required parameter (bandwidth, limit) is missing\n"); | |
106 | return -1; | |
107 | } | |
108 | ||
109 | /* Compute default min/max thresholds based on | |
110 | Sally Floyd's recommendations: | |
111 | http://www.icir.org/floyd/REDparameters.txt | |
112 | */ | |
113 | if (!opt.qth_max) | |
114 | opt.qth_max = opt.limit / 4; | |
115 | if (!opt.qth_min) | |
116 | opt.qth_min = opt.qth_max / 3; | |
117 | if (!burst) | |
118 | burst = (2 * opt.qth_min + opt.qth_max) / 3; | |
119 | ||
120 | if (opt.qth_max > opt.limit) { | |
121 | fprintf(stderr, "\"max\" is larger than \"limit\"\n"); | |
122 | return -1; | |
123 | } | |
124 | ||
9f1ba570 | 125 | if (opt.qth_min >= opt.qth_max) { |
a4eca97c SH |
126 | fprintf(stderr, "\"min\" is not smaller than \"max\"\n"); |
127 | return -1; | |
128 | } | |
129 | ||
130 | wlog = tc_red_eval_ewma(opt.qth_min*avpkt, burst, avpkt); | |
131 | if (wlog < 0) { | |
132 | fprintf(stderr, "CHOKE: failed to calculate EWMA constant.\n"); | |
133 | return -1; | |
134 | } | |
135 | if (wlog >= 10) | |
136 | fprintf(stderr, "CHOKE: WARNING. Burst %d seems to be to large.\n", burst); | |
137 | opt.Wlog = wlog; | |
138 | ||
139 | wlog = tc_red_eval_P(opt.qth_min*avpkt, opt.qth_max*avpkt, probability); | |
140 | if (wlog < 0) { | |
141 | fprintf(stderr, "CHOKE: failed to calculate probability.\n"); | |
142 | return -1; | |
143 | } | |
144 | opt.Plog = wlog; | |
145 | ||
146 | wlog = tc_red_eval_idle_damping(opt.Wlog, avpkt, rate, sbuf); | |
147 | if (wlog < 0) { | |
148 | fprintf(stderr, "CHOKE: failed to calculate idle damping table.\n"); | |
149 | return -1; | |
150 | } | |
151 | opt.Scell_log = wlog; | |
152 | if (ecn_ok) | |
153 | opt.flags |= TC_RED_ECN; | |
154 | ||
155 | tail = NLMSG_TAIL(n); | |
156 | addattr_l(n, 1024, TCA_OPTIONS, NULL, 0); | |
157 | addattr_l(n, 1024, TCA_CHOKE_PARMS, &opt, sizeof(opt)); | |
158 | addattr_l(n, 1024, TCA_CHOKE_STAB, sbuf, 256); | |
159 | tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; | |
160 | return 0; | |
161 | } | |
162 | ||
163 | static int choke_print_opt(struct qdisc_util *qu, FILE *f, struct rtattr *opt) | |
164 | { | |
165 | struct rtattr *tb[TCA_CHOKE_STAB+1]; | |
166 | const struct tc_red_qopt *qopt; | |
167 | ||
168 | if (opt == NULL) | |
169 | return 0; | |
170 | ||
171 | parse_rtattr_nested(tb, TCA_CHOKE_STAB, opt); | |
172 | ||
173 | if (tb[TCA_CHOKE_PARMS] == NULL) | |
174 | return -1; | |
175 | qopt = RTA_DATA(tb[TCA_CHOKE_PARMS]); | |
176 | if (RTA_PAYLOAD(tb[TCA_CHOKE_PARMS]) < sizeof(*qopt)) | |
177 | return -1; | |
178 | ||
179 | fprintf(f, "limit %up min %up max %up ", | |
180 | qopt->limit, qopt->qth_min, qopt->qth_max); | |
181 | ||
182 | if (qopt->flags & TC_RED_ECN) | |
183 | fprintf(f, "ecn "); | |
184 | ||
185 | if (show_details) { | |
186 | fprintf(f, "ewma %u Plog %u Scell_log %u", | |
187 | qopt->Wlog, qopt->Plog, qopt->Scell_log); | |
188 | } | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static int choke_print_xstats(struct qdisc_util *qu, FILE *f, | |
193 | struct rtattr *xstats) | |
194 | { | |
195 | struct tc_choke_xstats *st; | |
196 | ||
197 | if (xstats == NULL) | |
198 | return 0; | |
199 | ||
200 | if (RTA_PAYLOAD(xstats) < sizeof(*st)) | |
201 | return -1; | |
202 | ||
203 | st = RTA_DATA(xstats); | |
204 | fprintf(f, " marked %u early %u pdrop %u other %u matched %u", | |
205 | st->marked, st->early, st->pdrop, st->other, st->matched); | |
206 | return 0; | |
207 | ||
208 | } | |
209 | ||
210 | struct qdisc_util choke_qdisc_util = { | |
211 | .id = "choke", | |
212 | .parse_qopt = choke_parse_opt, | |
213 | .print_qopt = choke_print_opt, | |
214 | .print_xstats = choke_print_xstats, | |
215 | }; |