altq_conf.c revision 1.8 1 /* $NetBSD: altq_conf.c,v 1.8 2002/09/07 11:56:33 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.8 2002/09/07 11:56:33 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__)
162 dev_type_open(altqopen);
163 dev_type_close(altqclose);
164 dev_type_ioctl(altqioctl);
165 #endif
166 #if defined(__OpenBSD__)
167 static d_open_t altqopen;
168 static d_close_t altqclose;
169 static d_ioctl_t altqioctl;
170 #endif
171 #ifdef __FreeBSD__
172 static void altq_drvinit __P((void *));
173 #else
174 void altqattach __P((int));
175 #endif
176
177 #if defined(__FreeBSD__)
178 #define CDEV_MAJOR 96 /* FreeBSD official number */
179 #elif defined(__OpenBSD__)
180 #if defined(__i386__)
181 #define CDEV_MAJOR 74 /* OpenBSD i386 (official) */
182 #elif defined(__alpha__)
183 #define CDEV_MAJOR 53 /* OpenBSD alpha (official) */
184 #else
185 #error arch not supported
186 #endif
187 #endif
188
189 #if defined(__FreeBSD__)
190 #if (__FreeBSD_version < 400000)
191 static struct cdevsw altq_cdevsw =
192 { altqopen, altqclose, noread, nowrite,
193 altqioctl, nostop, nullreset, nodevtotty,
194 seltrue, nommap, NULL, "altq", NULL, -1 };
195 #else
196 static struct cdevsw altq_cdevsw =
197 { altqopen, altqclose, noread, nowrite,
198 altqioctl, seltrue, nommap, nostrategy,
199 "altq", CDEV_MAJOR, nodump, nopsize, 0, -1 };
200 #endif
201 #endif
202
203 #if defined(__NetBSD__)
204 const struct cdevsw altq_cdevsw = {
205 altqopen, altqclose, noread, nowrite, altqioctl,
206 nostop, notty, nopoll, nommap,
207 };
208 #endif
209
210 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
211 static
212 #endif
213 int
214 altqopen(dev, flag, fmt, p)
215 dev_t dev;
216 int flag, fmt;
217 struct proc *p;
218 {
219 int unit = minor(dev);
220
221 if (unit == 0)
222 return (0);
223 if (unit < naltqsw)
224 return (*altqsw[unit].d_open)(dev, flag, fmt, p);
225
226 return ENXIO;
227 }
228
229 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
230 static
231 #endif
232 int
233 altqclose(dev, flag, fmt, p)
234 dev_t dev;
235 int flag, fmt;
236 struct proc *p;
237 {
238 int unit = minor(dev);
239
240 if (unit == 0)
241 return (0);
242 if (unit < naltqsw)
243 return (*altqsw[unit].d_close)(dev, flag, fmt, p);
244
245 return ENXIO;
246 }
247
248 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
249 static
250 #endif
251 int
252 altqioctl(dev, cmd, addr, flag, p)
253 dev_t dev;
254 ioctlcmd_t cmd;
255 caddr_t addr;
256 int flag;
257 struct proc *p;
258 {
259 int unit = minor(dev);
260
261 if (unit == 0) {
262 struct ifnet *ifp;
263 struct altqreq *typereq;
264 struct tbrreq *tbrreq;
265 int error;
266
267 switch (cmd) {
268 case ALTQGTYPE:
269 case ALTQTBRGET:
270 break;
271 default:
272 #if (__FreeBSD_version > 400000)
273 if ((error = suser(p)) != 0)
274 return (error);
275 #else
276 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
277 return (error);
278 #endif
279 break;
280 }
281
282 switch (cmd) {
283 case ALTQGTYPE:
284 typereq = (struct altqreq *)addr;
285 if ((ifp = ifunit(typereq->ifname)) == NULL)
286 return (EINVAL);
287 typereq->arg = (u_long)ifp->if_snd.altq_type;
288 return (0);
289 case ALTQTBRSET:
290 tbrreq = (struct tbrreq *)addr;
291 if ((ifp = ifunit(tbrreq->ifname)) == NULL)
292 return (EINVAL);
293 return tbr_set(&ifp->if_snd, &tbrreq->tb_prof);
294 case ALTQTBRGET:
295 tbrreq = (struct tbrreq *)addr;
296 if ((ifp = ifunit(tbrreq->ifname)) == NULL)
297 return (EINVAL);
298 return tbr_get(&ifp->if_snd, &tbrreq->tb_prof);
299 default:
300 return (EINVAL);
301 }
302 }
303 if (unit < naltqsw)
304 return (*altqsw[unit].d_ioctl)(dev, cmd, addr, flag, p);
305
306 return ENXIO;
307 }
308
309 #if !defined(__NetBSD__)
310 static int altq_devsw_installed = 0;
311 #endif
312
313 #ifdef __FreeBSD__
314 #if (__FreeBSD_version < 400000)
315 #ifdef DEVFS
316 static void *altq_devfs_token[sizeof (altqsw) / sizeof (altqsw[0])];
317 #endif
318
319 static void
320 altq_drvinit(unused)
321 void *unused;
322 {
323 dev_t dev;
324 #ifdef DEVFS
325 int i;
326 #endif
327
328 if (!altq_devsw_installed) {
329 dev = makedev(CDEV_MAJOR,0);
330 cdevsw_add(&dev,&altq_cdevsw,NULL);
331 altq_devsw_installed = 1;
332 #ifdef DEVFS
333 for (i=0; i<naltqsw; i++)
334 altq_devfs_token[i] =
335 devfs_add_devswf(&altq_cdevsw, i, DV_CHR,
336 0, 0, 0644, altqsw[i].d_name);
337 #endif
338 printf("altq: major number is %d\n", CDEV_MAJOR);
339 }
340 }
341
342 #else /* FreeBSD 4.x */
343
344 static void
345 altq_drvinit(unused)
346 void *unused;
347 {
348 int unit;
349
350 cdevsw_add(&altq_cdevsw);
351 altq_devsw_installed = 1;
352 printf("altq: major number is %d\n", CDEV_MAJOR);
353
354 /* create minor devices */
355 for (unit = 0; unit < naltqsw; unit++)
356 make_dev(&altq_cdevsw, unit, 0, 0, 0644,
357 altqsw[unit].d_name);
358 }
359
360 #endif /* FreeBSD 4.x */
361
362 SYSINIT(altqdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,altq_drvinit,NULL)
363
364 #endif
365
366 #ifdef ALTQ_KLD
367 /*
368 * KLD support
369 */
370 static int altq_module_register __P((struct altq_module_data *));
371 static int altq_module_deregister __P((struct altq_module_data *));
372
373 static struct altq_module_data *altq_modules[ALTQT_MAX];
374 static struct altqsw noqdisc = {"noq", noopen, noclose, noioctl};
375
376 void altq_module_incref(type)
377 int type;
378 {
379 if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
380 return;
381
382 altq_modules[type]->ref++;
383 }
384
385 void altq_module_declref(type)
386 int type;
387 {
388 if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
389 return;
390
391 altq_modules[type]->ref--;
392 }
393
394 static int
395 altq_module_register(mdata)
396 struct altq_module_data *mdata;
397 {
398 int type = mdata->type;
399
400 if (type < 0 || type >= ALTQT_MAX)
401 return (EINVAL);
402 if (altqsw[type].d_open != noopen)
403 return (EBUSY);
404 altqsw[type] = *mdata->altqsw; /* set discipline functions */
405 altq_modules[type] = mdata; /* save module data pointer */
406 return (0);
407 }
408
409 static int
410 altq_module_deregister(mdata)
411 struct altq_module_data *mdata;
412 {
413 int type = mdata->type;
414
415 if (type < 0 || type >= ALTQT_MAX)
416 return (EINVAL);
417 if (mdata != altq_modules[type])
418 return (EINVAL);
419 if (altq_modules[type]->ref > 0)
420 return (EBUSY);
421 altqsw[type] = noqdisc;
422 altq_modules[type] = NULL;
423 return (0);
424 }
425
426 int
427 altq_module_handler(mod, cmd, arg)
428 module_t mod;
429 int cmd;
430 void * arg;
431 {
432 struct altq_module_data *data = (struct altq_module_data *)arg;
433 int error = 0;
434
435 switch (cmd) {
436 case MOD_LOAD:
437 error = altq_module_register(data);
438 break;
439
440 case MOD_UNLOAD:
441 error = altq_module_deregister(data);
442 break;
443
444 default:
445 error = EINVAL;
446 break;
447 }
448
449 return(error);
450 }
451
452 #endif /* ALTQ_KLD */
453