qop_rio.c revision 1.2 1 1.1 thorpej /* $KAME: qop_rio.c,v 1.3 2000/10/18 09:15:20 kjc Exp $ */
2 1.1 thorpej /*
3 1.1 thorpej * Copyright (C) 1999-2000
4 1.1 thorpej * Sony Computer Science Laboratories, Inc. All rights reserved.
5 1.1 thorpej *
6 1.1 thorpej * Redistribution and use in source and binary forms, with or without
7 1.1 thorpej * modification, are permitted provided that the following conditions
8 1.1 thorpej * are met:
9 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
10 1.1 thorpej * notice, this list of conditions and the following disclaimer.
11 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
13 1.1 thorpej * documentation and/or other materials provided with the distribution.
14 1.1 thorpej *
15 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
16 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
19 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 thorpej * SUCH DAMAGE.
26 1.1 thorpej */
27 1.1 thorpej
28 1.1 thorpej #include <sys/param.h>
29 1.1 thorpej #include <sys/socket.h>
30 1.1 thorpej #include <sys/sockio.h>
31 1.1 thorpej #include <sys/ioctl.h>
32 1.1 thorpej #include <sys/fcntl.h>
33 1.1 thorpej #include <net/if.h>
34 1.1 thorpej #include <netinet/in.h>
35 1.1 thorpej #include <arpa/inet.h>
36 1.1 thorpej
37 1.1 thorpej #include <stdio.h>
38 1.1 thorpej #include <stdlib.h>
39 1.1 thorpej #include <unistd.h>
40 1.1 thorpej #include <stddef.h>
41 1.1 thorpej #include <string.h>
42 1.1 thorpej #include <ctype.h>
43 1.1 thorpej #include <errno.h>
44 1.1 thorpej #include <syslog.h>
45 1.1 thorpej #include <netdb.h>
46 1.1 thorpej
47 1.1 thorpej #include <altq/altq.h>
48 1.1 thorpej #include <altq/altq_red.h>
49 1.1 thorpej #include <altq/altq_rio.h>
50 1.1 thorpej #include "altq_qop.h"
51 1.1 thorpej #include "qop_rio.h"
52 1.1 thorpej
53 1.2 itojun static int rio_attach(struct ifinfo *);
54 1.2 itojun static int rio_detach(struct ifinfo *);
55 1.2 itojun static int rio_enable(struct ifinfo *);
56 1.2 itojun static int rio_disable(struct ifinfo *);
57 1.1 thorpej
58 1.1 thorpej #define RIO_DEVICE "/dev/altq/rio"
59 1.1 thorpej
60 1.1 thorpej static int rio_fd = -1;
61 1.1 thorpej static int rio_refcount = 0;
62 1.1 thorpej
63 1.1 thorpej static struct qdisc_ops rio_qdisc = {
64 1.1 thorpej ALTQT_RIO,
65 1.1 thorpej "rio",
66 1.1 thorpej rio_attach,
67 1.1 thorpej rio_detach,
68 1.1 thorpej NULL, /* clear */
69 1.1 thorpej rio_enable,
70 1.1 thorpej rio_disable,
71 1.1 thorpej NULL, /* add class */
72 1.1 thorpej NULL, /* modify class */
73 1.1 thorpej NULL, /* delete class */
74 1.1 thorpej NULL, /* add filter */
75 1.1 thorpej NULL /* delete filter */
76 1.1 thorpej };
77 1.1 thorpej
78 1.1 thorpej /*
79 1.1 thorpej * parser interface
80 1.1 thorpej */
81 1.1 thorpej #define EQUAL(s1, s2) (strcmp((s1), (s2)) == 0)
82 1.1 thorpej
83 1.1 thorpej int
84 1.1 thorpej rio_interface_parser(const char *ifname, int argc, char **argv)
85 1.1 thorpej {
86 1.1 thorpej u_int bandwidth = 100000000; /* 100Mbps */
87 1.1 thorpej u_int tbrsize = 0;
88 1.1 thorpej int weight = 0; /* 0: use default */
89 1.1 thorpej int lo_inv_pmax = 0; /* 0: use default */
90 1.1 thorpej int lo_th_min = 0; /* 0: use default */
91 1.1 thorpej int lo_th_max = 0; /* 0: use default */
92 1.1 thorpej int med_inv_pmax = 0; /* 0: use default */
93 1.1 thorpej int med_th_min = 0; /* 0: use default */
94 1.1 thorpej int med_th_max = 0; /* 0: use default */
95 1.1 thorpej int hi_inv_pmax = 0; /* 0: use default */
96 1.1 thorpej int hi_th_min = 0; /* 0: use default */
97 1.1 thorpej int hi_th_max = 0; /* 0: use default */
98 1.1 thorpej int qlimit = 60;
99 1.1 thorpej int pkttime = 0;
100 1.1 thorpej int flags = 0;
101 1.1 thorpej int packet_size = 1000;
102 1.1 thorpej
103 1.1 thorpej /*
104 1.1 thorpej * process options
105 1.1 thorpej */
106 1.1 thorpej while (argc > 0) {
107 1.1 thorpej if (EQUAL(*argv, "bandwidth")) {
108 1.1 thorpej argc--; argv++;
109 1.1 thorpej if (argc > 0)
110 1.1 thorpej bandwidth = atobps(*argv);
111 1.1 thorpej } else if (EQUAL(*argv, "tbrsize")) {
112 1.1 thorpej argc--; argv++;
113 1.1 thorpej if (argc > 0)
114 1.1 thorpej tbrsize = atobytes(*argv);
115 1.1 thorpej } else if (EQUAL(*argv, "packetsize")) {
116 1.1 thorpej argc--; argv++;
117 1.1 thorpej if (argc > 0)
118 1.1 thorpej packet_size = atobytes(*argv);
119 1.1 thorpej } else if (EQUAL(*argv, "weight")) {
120 1.1 thorpej argc--; argv++;
121 1.1 thorpej if (argc > 0)
122 1.1 thorpej weight = (int)strtol(*argv, NULL, 0);
123 1.1 thorpej } else if (EQUAL(*argv, "qlimit")) {
124 1.1 thorpej argc--; argv++;
125 1.1 thorpej if (argc > 0)
126 1.1 thorpej qlimit = (int)strtol(*argv, NULL, 0);
127 1.1 thorpej } else if (EQUAL(*argv, "lo_thmin")) {
128 1.1 thorpej argc--; argv++;
129 1.1 thorpej if (argc > 0)
130 1.1 thorpej lo_th_min = (int)strtol(*argv, NULL, 0);
131 1.1 thorpej } else if (EQUAL(*argv, "lo_thmax")) {
132 1.1 thorpej argc--; argv++;
133 1.1 thorpej if (argc > 0)
134 1.1 thorpej lo_th_max = (int)strtol(*argv, NULL, 0);
135 1.1 thorpej } else if (EQUAL(*argv, "lo_invpmax")) {
136 1.1 thorpej argc--; argv++;
137 1.1 thorpej if (argc > 0)
138 1.1 thorpej lo_inv_pmax = (int)strtol(*argv, NULL, 0);
139 1.1 thorpej } else if (EQUAL(*argv, "med_thmin")) {
140 1.1 thorpej argc--; argv++;
141 1.1 thorpej if (argc > 0)
142 1.1 thorpej med_th_min = (int)strtol(*argv, NULL, 0);
143 1.1 thorpej } else if (EQUAL(*argv, "med_thmax")) {
144 1.1 thorpej argc--; argv++;
145 1.1 thorpej if (argc > 0)
146 1.1 thorpej med_th_max = (int)strtol(*argv, NULL, 0);
147 1.1 thorpej } else if (EQUAL(*argv, "med_invpmax")) {
148 1.1 thorpej argc--; argv++;
149 1.1 thorpej if (argc > 0)
150 1.1 thorpej med_inv_pmax = (int)strtol(*argv, NULL, 0);
151 1.1 thorpej } else if (EQUAL(*argv, "hi_thmin")) {
152 1.1 thorpej argc--; argv++;
153 1.1 thorpej if (argc > 0)
154 1.1 thorpej hi_th_min = (int)strtol(*argv, NULL, 0);
155 1.1 thorpej } else if (EQUAL(*argv, "hi_thmax")) {
156 1.1 thorpej argc--; argv++;
157 1.1 thorpej if (argc > 0)
158 1.1 thorpej hi_th_max = (int)strtol(*argv, NULL, 0);
159 1.1 thorpej } else if (EQUAL(*argv, "hi_invpmax")) {
160 1.1 thorpej argc--; argv++;
161 1.1 thorpej if (argc > 0)
162 1.1 thorpej hi_inv_pmax = (int)strtol(*argv, NULL, 0);
163 1.1 thorpej } else if (EQUAL(*argv, "rio")) {
164 1.1 thorpej /* just skip */
165 1.1 thorpej } else if (EQUAL(*argv, "ecn")) {
166 1.1 thorpej flags |= RIOF_ECN;
167 1.1 thorpej } else {
168 1.1 thorpej LOG(LOG_ERR, 0, "Unknown keyword '%s'\n", argv);
169 1.1 thorpej return (0);
170 1.1 thorpej }
171 1.1 thorpej argc--; argv++;
172 1.1 thorpej }
173 1.1 thorpej
174 1.1 thorpej if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
175 1.1 thorpej return (0);
176 1.1 thorpej
177 1.1 thorpej pkttime = packet_size * 8 * 1000 / (bandwidth / 1000);
178 1.1 thorpej if (weight != 0) {
179 1.1 thorpej /* check if weight is power of 2 */
180 1.1 thorpej int i, w;
181 1.1 thorpej
182 1.1 thorpej w = weight;
183 1.1 thorpej for (i = 0; w > 1; i++)
184 1.1 thorpej w = w >> 1;
185 1.1 thorpej w = 1 << i;
186 1.1 thorpej if (weight != w) {
187 1.1 thorpej LOG(LOG_ERR, 0, "weight %d: should be power of 2",
188 1.1 thorpej weight);
189 1.1 thorpej return (0);
190 1.1 thorpej }
191 1.1 thorpej }
192 1.1 thorpej
193 1.1 thorpej if (qcmd_rio_add_if(ifname, bandwidth, weight,
194 1.1 thorpej lo_inv_pmax, lo_th_min, lo_th_max,
195 1.1 thorpej med_inv_pmax, med_th_min, med_th_max,
196 1.1 thorpej hi_inv_pmax, hi_th_min, hi_th_max,
197 1.1 thorpej qlimit, pkttime, flags) != 0)
198 1.1 thorpej return (0);
199 1.1 thorpej return (1);
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej /*
203 1.1 thorpej * qcmd api
204 1.1 thorpej */
205 1.1 thorpej int
206 1.1 thorpej qcmd_rio_add_if(const char *ifname, u_int bandwidth, int weight,
207 1.1 thorpej int lo_inv_pmax, int lo_th_min, int lo_th_max,
208 1.1 thorpej int med_inv_pmax, int med_th_min, int med_th_max,
209 1.1 thorpej int hi_inv_pmax, int hi_th_min, int hi_th_max,
210 1.1 thorpej int qlimit, int pkttime, int flags)
211 1.1 thorpej {
212 1.1 thorpej struct redparams red_params[RIO_NDROPPREC];
213 1.1 thorpej int error;
214 1.1 thorpej
215 1.1 thorpej red_params[0].inv_pmax = lo_inv_pmax;
216 1.1 thorpej red_params[0].th_min = lo_th_min;
217 1.1 thorpej red_params[0].th_max = lo_th_max;
218 1.1 thorpej red_params[1].inv_pmax = med_inv_pmax;
219 1.1 thorpej red_params[1].th_min = med_th_min;
220 1.1 thorpej red_params[1].th_max = med_th_max;
221 1.1 thorpej red_params[2].inv_pmax = hi_inv_pmax;
222 1.1 thorpej red_params[2].th_min = hi_th_min;
223 1.1 thorpej red_params[2].th_max = hi_th_max;
224 1.1 thorpej
225 1.1 thorpej error = qop_rio_add_if(NULL, ifname, bandwidth, weight, red_params,
226 1.1 thorpej qlimit, pkttime, flags);
227 1.1 thorpej if (error != 0)
228 1.1 thorpej LOG(LOG_ERR, errno, "%s: can't add rio on interface '%s'\n",
229 1.1 thorpej qoperror(error), ifname);
230 1.1 thorpej return (error);
231 1.1 thorpej }
232 1.1 thorpej
233 1.1 thorpej /*
234 1.1 thorpej * qop api
235 1.1 thorpej */
236 1.1 thorpej int
237 1.1 thorpej qop_rio_add_if(struct ifinfo **rp, const char *ifname,
238 1.1 thorpej u_int bandwidth, int weight, struct redparams *red_params,
239 1.1 thorpej int qlimit, int pkttime, int flags)
240 1.1 thorpej {
241 1.1 thorpej struct ifinfo *ifinfo = NULL;
242 1.1 thorpej struct rio_ifinfo *rio_ifinfo;
243 1.1 thorpej int i, error;
244 1.1 thorpej
245 1.1 thorpej if ((rio_ifinfo = calloc(1, sizeof(*rio_ifinfo))) == NULL)
246 1.1 thorpej return (QOPERR_NOMEM);
247 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++)
248 1.1 thorpej rio_ifinfo->red_params[i] = red_params[i];
249 1.1 thorpej rio_ifinfo->weight = weight;
250 1.1 thorpej rio_ifinfo->qlimit = qlimit;
251 1.1 thorpej rio_ifinfo->pkttime = pkttime;
252 1.1 thorpej rio_ifinfo->flags = flags;
253 1.1 thorpej
254 1.1 thorpej error = qop_add_if(&ifinfo, ifname, bandwidth,
255 1.1 thorpej &rio_qdisc, rio_ifinfo);
256 1.1 thorpej if (error != 0) {
257 1.1 thorpej free(rio_ifinfo);
258 1.1 thorpej return (error);
259 1.1 thorpej }
260 1.1 thorpej
261 1.1 thorpej if (rp != NULL)
262 1.1 thorpej *rp = ifinfo;
263 1.1 thorpej return (0);
264 1.1 thorpej }
265 1.1 thorpej
266 1.1 thorpej /*
267 1.1 thorpej * system call interfaces for qdisc_ops
268 1.1 thorpej */
269 1.1 thorpej static int
270 1.1 thorpej rio_attach(struct ifinfo *ifinfo)
271 1.1 thorpej {
272 1.1 thorpej struct rio_interface iface;
273 1.1 thorpej struct rio_ifinfo *rio_ifinfo;
274 1.1 thorpej struct rio_conf conf;
275 1.1 thorpej int i;
276 1.1 thorpej
277 1.1 thorpej if (rio_fd < 0 &&
278 1.1 thorpej (rio_fd = open(RIO_DEVICE, O_RDWR)) < 0 &&
279 1.1 thorpej (rio_fd = open_module(RIO_DEVICE, O_RDWR)) < 0) {
280 1.1 thorpej LOG(LOG_ERR, errno, "RIO open\n");
281 1.1 thorpej return (QOPERR_SYSCALL);
282 1.1 thorpej }
283 1.1 thorpej
284 1.1 thorpej rio_refcount++;
285 1.1 thorpej memset(&iface, 0, sizeof(iface));
286 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
287 1.1 thorpej
288 1.1 thorpej if (ioctl(rio_fd, RIO_IF_ATTACH, &iface) < 0)
289 1.1 thorpej return (QOPERR_SYSCALL);
290 1.1 thorpej
291 1.1 thorpej /* set rio parameters */
292 1.1 thorpej rio_ifinfo = (struct rio_ifinfo *)ifinfo->private;
293 1.1 thorpej memset(&conf, 0, sizeof(conf));
294 1.1 thorpej strncpy(conf.iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
295 1.1 thorpej for (i = 0; i < RIO_NDROPPREC; i++)
296 1.1 thorpej conf.q_params[i] = rio_ifinfo->red_params[i];
297 1.1 thorpej conf.rio_weight = rio_ifinfo->weight;
298 1.1 thorpej conf.rio_limit = rio_ifinfo->qlimit;
299 1.1 thorpej conf.rio_flags = rio_ifinfo->flags;
300 1.1 thorpej if (ioctl(rio_fd, RIO_CONFIG, &conf) < 0)
301 1.1 thorpej return (QOPERR_SYSCALL);
302 1.1 thorpej
303 1.1 thorpej #if 1
304 1.1 thorpej LOG(LOG_INFO, 0, "rio attached to %s\n", iface.rio_ifname);
305 1.1 thorpej #endif
306 1.1 thorpej return (0);
307 1.1 thorpej }
308 1.1 thorpej
309 1.1 thorpej static int
310 1.1 thorpej rio_detach(struct ifinfo *ifinfo)
311 1.1 thorpej {
312 1.1 thorpej struct rio_interface iface;
313 1.1 thorpej
314 1.1 thorpej memset(&iface, 0, sizeof(iface));
315 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
316 1.1 thorpej
317 1.1 thorpej if (ioctl(rio_fd, RIO_IF_DETACH, &iface) < 0)
318 1.1 thorpej return (QOPERR_SYSCALL);
319 1.1 thorpej
320 1.1 thorpej if (--rio_refcount == 0) {
321 1.1 thorpej close(rio_fd);
322 1.1 thorpej rio_fd = -1;
323 1.1 thorpej }
324 1.1 thorpej return (0);
325 1.1 thorpej }
326 1.1 thorpej
327 1.1 thorpej static int
328 1.1 thorpej rio_enable(struct ifinfo *ifinfo)
329 1.1 thorpej {
330 1.1 thorpej struct rio_interface iface;
331 1.1 thorpej
332 1.1 thorpej memset(&iface, 0, sizeof(iface));
333 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
334 1.1 thorpej
335 1.1 thorpej if (ioctl(rio_fd, RIO_ENABLE, &iface) < 0)
336 1.1 thorpej return (QOPERR_SYSCALL);
337 1.1 thorpej return (0);
338 1.1 thorpej }
339 1.1 thorpej
340 1.1 thorpej static int
341 1.1 thorpej rio_disable(struct ifinfo *ifinfo)
342 1.1 thorpej {
343 1.1 thorpej struct rio_interface iface;
344 1.1 thorpej
345 1.1 thorpej memset(&iface, 0, sizeof(iface));
346 1.1 thorpej strncpy(iface.rio_ifname, ifinfo->ifname, IFNAMSIZ);
347 1.1 thorpej
348 1.1 thorpej if (ioctl(rio_fd, RIO_DISABLE, &iface) < 0)
349 1.1 thorpej return (QOPERR_SYSCALL);
350 1.1 thorpej return (0);
351 1.1 thorpej }
352