altq_conf.c revision 1.7 1 /* $NetBSD: altq_conf.c,v 1.7 2002/09/06 13:18:43 gehenna Exp $ */
2 /* $KAME: altq_conf.c,v 1.13 2002/01/29 10:16:01 kjc Exp $ */
3
4 /*
5 * Copyright (C) 1997-2000
6 * Sony Computer Science Laboratories Inc. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: altq_conf.c,v 1.7 2002/09/06 13:18:43 gehenna Exp $");
32
33 #if defined(__FreeBSD__) || defined(__NetBSD__)
34 #include "opt_altq.h"
35 #if (__FreeBSD__ != 2)
36 #include "opt_inet.h"
37 #ifdef __FreeBSD__
38 #include "opt_inet6.h"
39 #endif
40 #endif
41 #endif /* __FreeBSD__ || __NetBSD__ */
42
43 /*
44 * altq device interface.
45 */
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/kernel.h>
50 #include <sys/proc.h>
51 #include <sys/errno.h>
52 #if defined(__FreeBSD__) && (__FreeBSD_version < 400000) && defined(DEVFS)
53 #include <sys/devfsext.h>
54 #endif /*DEVFS*/
55 #include <net/if.h>
56
57 #include <altq/altq.h>
58 #include <altq/altq_conf.h>
59
60 #ifdef ALTQ_CBQ
61 altqdev_decl(cbq);
62 #endif
63 #ifdef ALTQ_WFQ
64 altqdev_decl(wfq);
65 #endif
66 #ifdef ALTQ_AFMAP
67 altqdev_decl(afm);
68 #endif
69 #ifdef ALTQ_FIFOQ
70 altqdev_decl(fifoq);
71 #endif
72 #ifdef ALTQ_RED
73 altqdev_decl(red);
74 #endif
75 #ifdef ALTQ_RIO
76 altqdev_decl(rio);
77 #endif
78 #ifdef ALTQ_LOCALQ
79 altqdev_decl(localq);
80 #endif
81 #ifdef ALTQ_HFSC
82 altqdev_decl(hfsc);
83 #endif
84 #ifdef ALTQ_CDNR
85 altqdev_decl(cdnr);
86 #endif
87 #ifdef ALTQ_BLUE
88 altqdev_decl(blue);
89 #endif
90 #ifdef ALTQ_PRIQ
91 altqdev_decl(priq);
92 #endif
93
94 /*
95 * altq minor device (discipline) table
96 */
97 static struct altqsw altqsw[] = { /* minor */
98 {"noq", noopen, noclose, noioctl}, /* 0 (reserved) */
99 #ifdef ALTQ_CBQ
100 {"cbq", cbqopen, cbqclose, cbqioctl}, /* 1 */
101 #else
102 {"noq", noopen, noclose, noioctl}, /* 1 */
103 #endif
104 #ifdef ALTQ_WFQ
105 {"wfq", wfqopen, wfqclose, wfqioctl}, /* 2 */
106 #else
107 {"noq", noopen, noclose, noioctl}, /* 2 */
108 #endif
109 #ifdef ALTQ_AFMAP
110 {"afm", afmopen, afmclose, afmioctl}, /* 3 */
111 #else
112 {"noq", noopen, noclose, noioctl}, /* 3 */
113 #endif
114 #ifdef ALTQ_FIFOQ
115 {"fifoq", fifoqopen, fifoqclose, fifoqioctl}, /* 4 */
116 #else
117 {"noq", noopen, noclose, noioctl}, /* 4 */
118 #endif
119 #ifdef ALTQ_RED
120 {"red", redopen, redclose, redioctl}, /* 5 */
121 #else
122 {"noq", noopen, noclose, noioctl}, /* 5 */
123 #endif
124 #ifdef ALTQ_RIO
125 {"rio", rioopen, rioclose, rioioctl}, /* 6 */
126 #else
127 {"noq", noopen, noclose, noioctl}, /* 6 */
128 #endif
129 #ifdef ALTQ_LOCALQ
130 {"localq",localqopen, localqclose, localqioctl}, /* 7 (local use) */
131 #else
132 {"noq", noopen, noclose, noioctl}, /* 7 (local use) */
133 #endif
134 #ifdef ALTQ_HFSC
135 {"hfsc",hfscopen, hfscclose, hfscioctl}, /* 8 */
136 #else
137 {"noq", noopen, noclose, noioctl}, /* 8 */
138 #endif
139 #ifdef ALTQ_CDNR
140 {"cdnr",cdnropen, cdnrclose, cdnrioctl}, /* 9 */
141 #else
142 {"noq", noopen, noclose, noioctl}, /* 9 */
143 #endif
144 #ifdef ALTQ_BLUE
145 {"blue",blueopen, blueclose, blueioctl}, /* 10 */
146 #else
147 {"noq", noopen, noclose, noioctl}, /* 10 */
148 #endif
149 #ifdef ALTQ_PRIQ
150 {"priq",priqopen, priqclose, priqioctl}, /* 11 */
151 #else
152 {"noq", noopen, noclose, noioctl}, /* 11 */
153 #endif
154 };
155
156 /*
157 * altq major device support
158 */
159 int naltqsw = sizeof (altqsw) / sizeof (altqsw[0]);
160
161 #if !defined(__NetBSD__) && defined(__OpenBSD__)
162 static d_open_t altqopen;
163 static d_close_t altqclose;
164 static d_ioctl_t altqioctl;
165 #endif
166 #ifdef __FreeBSD__
167 static void altq_drvinit __P((void *));
168 #else
169 void altqattach __P((int));
170 #endif
171
172 #if defined(__FreeBSD__)
173 #define CDEV_MAJOR 96 /* FreeBSD official number */
174 #elif defined(__OpenBSD__)
175 #if defined(__i386__)
176 #define CDEV_MAJOR 74 /* OpenBSD i386 (official) */
177 #elif defined(__alpha__)
178 #define CDEV_MAJOR 53 /* OpenBSD alpha (official) */
179 #else
180 #error arch not supported
181 #endif
182 #endif
183
184 #if defined(__FreeBSD__)
185 #if (__FreeBSD_version < 400000)
186 static struct cdevsw altq_cdevsw =
187 { altqopen, altqclose, noread, nowrite,
188 altqioctl, nostop, nullreset, nodevtotty,
189 seltrue, nommap, NULL, "altq", NULL, -1 };
190 #else
191 static struct cdevsw altq_cdevsw =
192 { altqopen, altqclose, noread, nowrite,
193 altqioctl, seltrue, nommap, nostrategy,
194 "altq", CDEV_MAJOR, nodump, nopsize, 0, -1 };
195 #endif
196 #endif
197
198 #if defined(__NetBSD__)
199 const struct cdevsw altq_cdevsw = {
200 altqopen, altqclose, noread, nowrite, altqioctl,
201 nostop, notty, nopoll, nommap, D_DULL
202 };
203 #endif
204
205 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
206 static
207 #endif
208 int
209 altqopen(dev, flag, fmt, p)
210 dev_t dev;
211 int flag, fmt;
212 struct proc *p;
213 {
214 int unit = minor(dev);
215
216 if (unit == 0)
217 return (0);
218 if (unit < naltqsw)
219 return (*altqsw[unit].d_open)(dev, flag, fmt, p);
220
221 return ENXIO;
222 }
223
224 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
225 static
226 #endif
227 int
228 altqclose(dev, flag, fmt, p)
229 dev_t dev;
230 int flag, fmt;
231 struct proc *p;
232 {
233 int unit = minor(dev);
234
235 if (unit == 0)
236 return (0);
237 if (unit < naltqsw)
238 return (*altqsw[unit].d_close)(dev, flag, fmt, p);
239
240 return ENXIO;
241 }
242
243 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
244 static
245 #endif
246 int
247 altqioctl(dev, cmd, addr, flag, p)
248 dev_t dev;
249 ioctlcmd_t cmd;
250 caddr_t addr;
251 int flag;
252 struct proc *p;
253 {
254 int unit = minor(dev);
255
256 if (unit == 0) {
257 struct ifnet *ifp;
258 struct altqreq *typereq;
259 struct tbrreq *tbrreq;
260 int error;
261
262 switch (cmd) {
263 case ALTQGTYPE:
264 case ALTQTBRGET:
265 break;
266 default:
267 #if (__FreeBSD_version > 400000)
268 if ((error = suser(p)) != 0)
269 return (error);
270 #else
271 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
272 return (error);
273 #endif
274 break;
275 }
276
277 switch (cmd) {
278 case ALTQGTYPE:
279 typereq = (struct altqreq *)addr;
280 if ((ifp = ifunit(typereq->ifname)) == NULL)
281 return (EINVAL);
282 typereq->arg = (u_long)ifp->if_snd.altq_type;
283 return (0);
284 case ALTQTBRSET:
285 tbrreq = (struct tbrreq *)addr;
286 if ((ifp = ifunit(tbrreq->ifname)) == NULL)
287 return (EINVAL);
288 return tbr_set(&ifp->if_snd, &tbrreq->tb_prof);
289 case ALTQTBRGET:
290 tbrreq = (struct tbrreq *)addr;
291 if ((ifp = ifunit(tbrreq->ifname)) == NULL)
292 return (EINVAL);
293 return tbr_get(&ifp->if_snd, &tbrreq->tb_prof);
294 default:
295 return (EINVAL);
296 }
297 }
298 if (unit < naltqsw)
299 return (*altqsw[unit].d_ioctl)(dev, cmd, addr, flag, p);
300
301 return ENXIO;
302 }
303
304 #if !defined(__NetBSD__)
305 static int altq_devsw_installed = 0;
306 #endif
307
308 #ifdef __FreeBSD__
309 #if (__FreeBSD_version < 400000)
310 #ifdef DEVFS
311 static void *altq_devfs_token[sizeof (altqsw) / sizeof (altqsw[0])];
312 #endif
313
314 static void
315 altq_drvinit(unused)
316 void *unused;
317 {
318 dev_t dev;
319 #ifdef DEVFS
320 int i;
321 #endif
322
323 if (!altq_devsw_installed) {
324 dev = makedev(CDEV_MAJOR,0);
325 cdevsw_add(&dev,&altq_cdevsw,NULL);
326 altq_devsw_installed = 1;
327 #ifdef DEVFS
328 for (i=0; i<naltqsw; i++)
329 altq_devfs_token[i] =
330 devfs_add_devswf(&altq_cdevsw, i, DV_CHR,
331 0, 0, 0644, altqsw[i].d_name);
332 #endif
333 printf("altq: major number is %d\n", CDEV_MAJOR);
334 }
335 }
336
337 #else /* FreeBSD 4.x */
338
339 static void
340 altq_drvinit(unused)
341 void *unused;
342 {
343 int unit;
344
345 cdevsw_add(&altq_cdevsw);
346 altq_devsw_installed = 1;
347 printf("altq: major number is %d\n", CDEV_MAJOR);
348
349 /* create minor devices */
350 for (unit = 0; unit < naltqsw; unit++)
351 make_dev(&altq_cdevsw, unit, 0, 0, 0644,
352 altqsw[unit].d_name);
353 }
354
355 #endif /* FreeBSD 4.x */
356
357 SYSINIT(altqdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,altq_drvinit,NULL)
358
359 #endif
360
361 #ifdef ALTQ_KLD
362 /*
363 * KLD support
364 */
365 static int altq_module_register __P((struct altq_module_data *));
366 static int altq_module_deregister __P((struct altq_module_data *));
367
368 static struct altq_module_data *altq_modules[ALTQT_MAX];
369 static struct altqsw noqdisc = {"noq", noopen, noclose, noioctl};
370
371 void altq_module_incref(type)
372 int type;
373 {
374 if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
375 return;
376
377 altq_modules[type]->ref++;
378 }
379
380 void altq_module_declref(type)
381 int type;
382 {
383 if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
384 return;
385
386 altq_modules[type]->ref--;
387 }
388
389 static int
390 altq_module_register(mdata)
391 struct altq_module_data *mdata;
392 {
393 int type = mdata->type;
394
395 if (type < 0 || type >= ALTQT_MAX)
396 return (EINVAL);
397 if (altqsw[type].d_open != noopen)
398 return (EBUSY);
399 altqsw[type] = *mdata->altqsw; /* set discipline functions */
400 altq_modules[type] = mdata; /* save module data pointer */
401 return (0);
402 }
403
404 static int
405 altq_module_deregister(mdata)
406 struct altq_module_data *mdata;
407 {
408 int type = mdata->type;
409
410 if (type < 0 || type >= ALTQT_MAX)
411 return (EINVAL);
412 if (mdata != altq_modules[type])
413 return (EINVAL);
414 if (altq_modules[type]->ref > 0)
415 return (EBUSY);
416 altqsw[type] = noqdisc;
417 altq_modules[type] = NULL;
418 return (0);
419 }
420
421 int
422 altq_module_handler(mod, cmd, arg)
423 module_t mod;
424 int cmd;
425 void * arg;
426 {
427 struct altq_module_data *data = (struct altq_module_data *)arg;
428 int error = 0;
429
430 switch (cmd) {
431 case MOD_LOAD:
432 error = altq_module_register(data);
433 break;
434
435 case MOD_UNLOAD:
436 error = altq_module_deregister(data);
437 break;
438
439 default:
440 error = EINVAL;
441 break;
442 }
443
444 return(error);
445 }
446
447 #endif /* ALTQ_KLD */
448