qop_blue.c revision 1.2 1 1.1 thorpej /* $KAME: qop_blue.c,v 1.3 2000/10/18 09:15:18 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_blue.h>
49 1.1 thorpej #include "altq_qop.h"
50 1.1 thorpej #include "qop_blue.h"
51 1.1 thorpej
52 1.2 itojun static int blue_attach(struct ifinfo *);
53 1.2 itojun static int blue_detach(struct ifinfo *);
54 1.2 itojun static int blue_enable(struct ifinfo *);
55 1.2 itojun static int blue_disable(struct ifinfo *);
56 1.1 thorpej
57 1.1 thorpej #define BLUE_DEVICE "/dev/altq/blue"
58 1.1 thorpej
59 1.1 thorpej static int blue_fd = -1;
60 1.1 thorpej static int blue_refcount = 0;
61 1.1 thorpej
62 1.1 thorpej static struct qdisc_ops blue_qdisc = {
63 1.1 thorpej ALTQT_BLUE,
64 1.1 thorpej "blue",
65 1.1 thorpej blue_attach,
66 1.1 thorpej blue_detach,
67 1.1 thorpej NULL, /* clear */
68 1.1 thorpej blue_enable,
69 1.1 thorpej blue_disable,
70 1.1 thorpej NULL, /* add class */
71 1.1 thorpej NULL, /* modify class */
72 1.1 thorpej NULL, /* delete class */
73 1.1 thorpej NULL, /* add filter */
74 1.1 thorpej NULL /* delete filter */
75 1.1 thorpej };
76 1.1 thorpej
77 1.1 thorpej /*
78 1.1 thorpej * parser interface
79 1.1 thorpej */
80 1.1 thorpej #define EQUAL(s1, s2) (strcmp((s1), (s2)) == 0)
81 1.1 thorpej
82 1.1 thorpej int
83 1.1 thorpej blue_interface_parser(const char *ifname, int argc, char **argv)
84 1.1 thorpej {
85 1.1 thorpej u_int bandwidth = 100000000; /* 100Mbps */
86 1.1 thorpej u_int tbrsize = 0;
87 1.1 thorpej int max_pmark = 4000;
88 1.1 thorpej int hold_time = 1000;
89 1.1 thorpej int qlimit = 60;
90 1.1 thorpej int pkttime = 0;
91 1.1 thorpej int flags = 0;
92 1.1 thorpej int packet_size = 1000;
93 1.1 thorpej
94 1.1 thorpej /*
95 1.1 thorpej * process options
96 1.1 thorpej */
97 1.1 thorpej while (argc > 0) {
98 1.1 thorpej if (EQUAL(*argv, "bandwidth")) {
99 1.1 thorpej argc--; argv++;
100 1.1 thorpej if (argc > 0)
101 1.1 thorpej bandwidth = atobps(*argv);
102 1.1 thorpej } else if (EQUAL(*argv, "tbrsize")) {
103 1.1 thorpej argc--; argv++;
104 1.1 thorpej if (argc > 0)
105 1.1 thorpej tbrsize = atobytes(*argv);
106 1.1 thorpej } else if (EQUAL(*argv, "packetsize")) {
107 1.1 thorpej argc--; argv++;
108 1.1 thorpej if (argc > 0)
109 1.1 thorpej packet_size = atobytes(*argv);
110 1.1 thorpej } else if (EQUAL(*argv, "qlimit")) {
111 1.1 thorpej argc--; argv++;
112 1.1 thorpej if (argc > 0)
113 1.1 thorpej qlimit = (int)strtol(*argv, NULL, 0);
114 1.1 thorpej } else if (EQUAL(*argv, "maxpmark")) {
115 1.1 thorpej argc--; argv++;
116 1.1 thorpej if (argc > 0)
117 1.1 thorpej max_pmark = (int)strtol(*argv, NULL, 0);
118 1.1 thorpej } else if (EQUAL(*argv, "holdtime")) {
119 1.1 thorpej argc--; argv++;
120 1.1 thorpej if (argc > 0)
121 1.1 thorpej hold_time = (int)strtol(*argv, NULL, 0);
122 1.1 thorpej } else if (EQUAL(*argv, "blue")) {
123 1.1 thorpej /* just skip */
124 1.1 thorpej } else if (EQUAL(*argv, "ecn")) {
125 1.1 thorpej flags |= BLUEF_ECN;
126 1.1 thorpej } else {
127 1.1 thorpej LOG(LOG_ERR, 0, "Unknown keyword '%s'\n", argv);
128 1.1 thorpej return (0);
129 1.1 thorpej }
130 1.1 thorpej argc--; argv++;
131 1.1 thorpej }
132 1.1 thorpej
133 1.1 thorpej if (qcmd_tbr_register(ifname, bandwidth, tbrsize) != 0)
134 1.1 thorpej return (0);
135 1.1 thorpej
136 1.1 thorpej pkttime = packet_size * 8 * 1000 / (bandwidth / 1000);
137 1.1 thorpej
138 1.1 thorpej if (qcmd_blue_add_if(ifname, bandwidth, max_pmark, hold_time,
139 1.1 thorpej qlimit, pkttime, flags) != 0)
140 1.1 thorpej return (0);
141 1.1 thorpej return (1);
142 1.1 thorpej }
143 1.1 thorpej
144 1.1 thorpej /*
145 1.1 thorpej * qcmd api
146 1.1 thorpej */
147 1.1 thorpej int
148 1.1 thorpej qcmd_blue_add_if(const char *ifname, u_int bandwidth, int max_pmark,
149 1.1 thorpej int hold_time, int qlimit, int pkttime, int flags)
150 1.1 thorpej {
151 1.1 thorpej int error;
152 1.1 thorpej
153 1.1 thorpej error = qop_blue_add_if(NULL, ifname, bandwidth, max_pmark, hold_time,
154 1.1 thorpej qlimit, pkttime, flags);
155 1.1 thorpej if (error != 0)
156 1.1 thorpej LOG(LOG_ERR, errno, "%s: can't add blue on interface '%s'\n",
157 1.1 thorpej qoperror(error), ifname);
158 1.1 thorpej return (error);
159 1.1 thorpej }
160 1.1 thorpej
161 1.1 thorpej /*
162 1.1 thorpej * qop api
163 1.1 thorpej */
164 1.1 thorpej int
165 1.1 thorpej qop_blue_add_if(struct ifinfo **rp, const char *ifname, u_int bandwidth,
166 1.1 thorpej int max_pmark, int hold_time, int qlimit,
167 1.1 thorpej int pkttime, int flags)
168 1.1 thorpej {
169 1.1 thorpej struct ifinfo *ifinfo = NULL;
170 1.1 thorpej struct blue_ifinfo *blue_ifinfo;
171 1.1 thorpej int error;
172 1.1 thorpej
173 1.1 thorpej if ((blue_ifinfo = calloc(1, sizeof(*blue_ifinfo))) == NULL)
174 1.1 thorpej return (QOPERR_NOMEM);
175 1.1 thorpej blue_ifinfo->max_pmark = max_pmark;
176 1.1 thorpej blue_ifinfo->hold_time = hold_time;
177 1.1 thorpej blue_ifinfo->qlimit = qlimit;
178 1.1 thorpej blue_ifinfo->pkttime = pkttime;
179 1.1 thorpej blue_ifinfo->flags = flags;
180 1.1 thorpej
181 1.1 thorpej error = qop_add_if(&ifinfo, ifname, bandwidth,
182 1.1 thorpej &blue_qdisc, blue_ifinfo);
183 1.1 thorpej if (error != 0) {
184 1.1 thorpej free(blue_ifinfo);
185 1.1 thorpej return (error);
186 1.1 thorpej }
187 1.1 thorpej
188 1.1 thorpej if (rp != NULL)
189 1.1 thorpej *rp = ifinfo;
190 1.1 thorpej return (0);
191 1.1 thorpej }
192 1.1 thorpej
193 1.1 thorpej /*
194 1.1 thorpej * system call interfaces for qdisc_ops
195 1.1 thorpej */
196 1.1 thorpej static int
197 1.1 thorpej blue_attach(struct ifinfo *ifinfo)
198 1.1 thorpej {
199 1.1 thorpej struct blue_interface iface;
200 1.1 thorpej struct blue_ifinfo *blue_ifinfo;
201 1.1 thorpej struct blue_conf conf;
202 1.1 thorpej
203 1.1 thorpej if (blue_fd < 0 &&
204 1.1 thorpej (blue_fd = open(BLUE_DEVICE, O_RDWR)) < 0 &&
205 1.1 thorpej (blue_fd = open_module(BLUE_DEVICE, O_RDWR)) < 0) {
206 1.1 thorpej LOG(LOG_ERR, errno, "BLUE open\n");
207 1.1 thorpej return (QOPERR_SYSCALL);
208 1.1 thorpej }
209 1.1 thorpej
210 1.1 thorpej blue_refcount++;
211 1.1 thorpej memset(&iface, 0, sizeof(iface));
212 1.1 thorpej strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
213 1.1 thorpej
214 1.1 thorpej if (ioctl(blue_fd, BLUE_IF_ATTACH, &iface) < 0)
215 1.1 thorpej return (QOPERR_SYSCALL);
216 1.1 thorpej
217 1.1 thorpej /* set blue parameters */
218 1.1 thorpej blue_ifinfo = (struct blue_ifinfo *)ifinfo->private;
219 1.1 thorpej memset(&conf, 0, sizeof(conf));
220 1.1 thorpej strncpy(conf.iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
221 1.1 thorpej conf.blue_max_pmark = blue_ifinfo->max_pmark;
222 1.1 thorpej conf.blue_hold_time = blue_ifinfo->hold_time;
223 1.1 thorpej conf.blue_limit = blue_ifinfo->qlimit;
224 1.1 thorpej conf.blue_pkttime = blue_ifinfo->pkttime;
225 1.1 thorpej conf.blue_flags = blue_ifinfo->flags;
226 1.1 thorpej if (ioctl(blue_fd, BLUE_CONFIG, &conf) < 0)
227 1.1 thorpej return (QOPERR_SYSCALL);
228 1.1 thorpej
229 1.1 thorpej #if 1
230 1.1 thorpej LOG(LOG_INFO, 0, "blue attached to %s\n", iface.blue_ifname);
231 1.1 thorpej #endif
232 1.1 thorpej return (0);
233 1.1 thorpej }
234 1.1 thorpej
235 1.1 thorpej static int
236 1.1 thorpej blue_detach(struct ifinfo *ifinfo)
237 1.1 thorpej {
238 1.1 thorpej struct blue_interface iface;
239 1.1 thorpej
240 1.1 thorpej memset(&iface, 0, sizeof(iface));
241 1.1 thorpej strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
242 1.1 thorpej
243 1.1 thorpej if (ioctl(blue_fd, BLUE_IF_DETACH, &iface) < 0)
244 1.1 thorpej return (QOPERR_SYSCALL);
245 1.1 thorpej
246 1.1 thorpej if (--blue_refcount == 0) {
247 1.1 thorpej close(blue_fd);
248 1.1 thorpej blue_fd = -1;
249 1.1 thorpej }
250 1.1 thorpej return (0);
251 1.1 thorpej }
252 1.1 thorpej
253 1.1 thorpej static int
254 1.1 thorpej blue_enable(struct ifinfo *ifinfo)
255 1.1 thorpej {
256 1.1 thorpej struct blue_interface iface;
257 1.1 thorpej
258 1.1 thorpej memset(&iface, 0, sizeof(iface));
259 1.1 thorpej strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
260 1.1 thorpej
261 1.1 thorpej if (ioctl(blue_fd, BLUE_ENABLE, &iface) < 0)
262 1.1 thorpej return (QOPERR_SYSCALL);
263 1.1 thorpej return (0);
264 1.1 thorpej }
265 1.1 thorpej
266 1.1 thorpej static int
267 1.1 thorpej blue_disable(struct ifinfo *ifinfo)
268 1.1 thorpej {
269 1.1 thorpej struct blue_interface iface;
270 1.1 thorpej
271 1.1 thorpej memset(&iface, 0, sizeof(iface));
272 1.1 thorpej strncpy(iface.blue_ifname, ifinfo->ifname, IFNAMSIZ);
273 1.1 thorpej
274 1.1 thorpej if (ioctl(blue_fd, BLUE_DISABLE, &iface) < 0)
275 1.1 thorpej return (QOPERR_SYSCALL);
276 1.1 thorpej return (0);
277 1.1 thorpej }
278