Home | History | Annotate | Line # | Download | only in altq
altq_conf.c revision 1.12
      1 /*	$NetBSD: altq_conf.c,v 1.12 2005/12/11 12:16:03 christos 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.12 2005/12/11 12:16:03 christos 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/altqconf.h>
     59 #include <altq/altq_conf.h>
     60 
     61 #ifdef ALTQ_CBQ
     62 altqdev_decl(cbq);
     63 #endif
     64 #ifdef ALTQ_WFQ
     65 altqdev_decl(wfq);
     66 #endif
     67 #ifdef ALTQ_AFMAP
     68 altqdev_decl(afm);
     69 #endif
     70 #ifdef ALTQ_FIFOQ
     71 altqdev_decl(fifoq);
     72 #endif
     73 #ifdef ALTQ_RED
     74 altqdev_decl(red);
     75 #endif
     76 #ifdef ALTQ_RIO
     77 altqdev_decl(rio);
     78 #endif
     79 #ifdef ALTQ_LOCALQ
     80 altqdev_decl(localq);
     81 #endif
     82 #ifdef ALTQ_HFSC
     83 altqdev_decl(hfsc);
     84 #endif
     85 #ifdef ALTQ_CDNR
     86 altqdev_decl(cdnr);
     87 #endif
     88 #ifdef ALTQ_BLUE
     89 altqdev_decl(blue);
     90 #endif
     91 #ifdef ALTQ_PRIQ
     92 altqdev_decl(priq);
     93 #endif
     94 
     95 /*
     96  * altq minor device (discipline) table
     97  */
     98 static struct altqsw altqsw[] = {				/* minor */
     99 	{"noq",	noopen,		noclose,	noioctl},  /* 0 (reserved) */
    100 #ifdef ALTQ_CBQ
    101 	{"cbq",	cbqopen,	cbqclose,	cbqioctl},	/* 1 */
    102 #else
    103 	{"noq",	noopen,		noclose,	noioctl},	/* 1 */
    104 #endif
    105 #ifdef ALTQ_WFQ
    106 	{"wfq",	wfqopen,	wfqclose,	wfqioctl},	/* 2 */
    107 #else
    108 	{"noq",	noopen,		noclose,	noioctl},	/* 2 */
    109 #endif
    110 #ifdef ALTQ_AFMAP
    111 	{"afm",	afmopen,	afmclose,	afmioctl},	/* 3 */
    112 #else
    113 	{"noq",	noopen,		noclose,	noioctl},	/* 3 */
    114 #endif
    115 #ifdef ALTQ_FIFOQ
    116 	{"fifoq", fifoqopen,	fifoqclose,	fifoqioctl},	/* 4 */
    117 #else
    118 	{"noq",	noopen,		noclose,	noioctl},	/* 4 */
    119 #endif
    120 #ifdef ALTQ_RED
    121 	{"red", redopen,	redclose,	redioctl},	/* 5 */
    122 #else
    123 	{"noq",	noopen,		noclose,	noioctl},	/* 5 */
    124 #endif
    125 #ifdef ALTQ_RIO
    126 	{"rio", rioopen,	rioclose,	rioioctl},	/* 6 */
    127 #else
    128 	{"noq",	noopen,		noclose,	noioctl},	/* 6 */
    129 #endif
    130 #ifdef ALTQ_LOCALQ
    131 	{"localq",localqopen,	localqclose,	localqioctl}, /* 7 (local use) */
    132 #else
    133 	{"noq",	noopen,		noclose,	noioctl},  /* 7 (local use) */
    134 #endif
    135 #ifdef ALTQ_HFSC
    136 	{"hfsc",hfscopen,	hfscclose,	hfscioctl},	/* 8 */
    137 #else
    138 	{"noq",	noopen,		noclose,	noioctl},	/* 8 */
    139 #endif
    140 #ifdef ALTQ_CDNR
    141 	{"cdnr",cdnropen,	cdnrclose,	cdnrioctl},	/* 9 */
    142 #else
    143 	{"noq",	noopen,		noclose,	noioctl},	/* 9 */
    144 #endif
    145 #ifdef ALTQ_BLUE
    146 	{"blue",blueopen,	blueclose,	blueioctl},	/* 10 */
    147 #else
    148 	{"noq",	noopen,		noclose,	noioctl},	/* 10 */
    149 #endif
    150 #ifdef ALTQ_PRIQ
    151 	{"priq",priqopen,	priqclose,	priqioctl},	/* 11 */
    152 #else
    153 	{"noq",	noopen,		noclose,	noioctl},	/* 11 */
    154 #endif
    155 };
    156 
    157 /*
    158  * altq major device support
    159  */
    160 int	naltqsw = sizeof (altqsw) / sizeof (altqsw[0]);
    161 
    162 #if defined(__NetBSD__)
    163 dev_type_open(altqopen);
    164 dev_type_close(altqclose);
    165 dev_type_ioctl(altqioctl);
    166 #endif
    167 #if defined(__OpenBSD__)
    168 static	d_open_t	altqopen;
    169 static	d_close_t	altqclose;
    170 static	d_ioctl_t	altqioctl;
    171 #endif
    172 #ifdef __FreeBSD__
    173 static void altq_drvinit __P((void *));
    174 #else
    175 void	altqattach __P((int));
    176 #endif
    177 
    178 #if defined(__FreeBSD__)
    179 #define	CDEV_MAJOR 96		/* FreeBSD official number */
    180 #elif defined(__OpenBSD__)
    181 #if defined(__i386__)
    182 #define	CDEV_MAJOR 74		/* OpenBSD i386 (official) */
    183 #elif defined(__alpha__)
    184 #define	CDEV_MAJOR 53		/* OpenBSD alpha (official) */
    185 #else
    186 #error arch not supported
    187 #endif
    188 #endif
    189 
    190 #if defined(__FreeBSD__)
    191 #if (__FreeBSD_version < 400000)
    192 static struct cdevsw altq_cdevsw =
    193         { altqopen,	altqclose,	noread,	        nowrite,
    194 	  altqioctl,	nostop,		nullreset,	nodevtotty,
    195  	  seltrue,	nommap,		NULL,	"altq",	NULL,	  -1 };
    196 #else
    197 static struct cdevsw altq_cdevsw =
    198         { altqopen,	altqclose,	noread,	        nowrite,
    199 	  altqioctl,	seltrue,	nommap,		nostrategy,
    200 	  "altq",	CDEV_MAJOR,	nodump,		nopsize,  0,  -1 };
    201 #endif
    202 #endif
    203 
    204 #if defined(__NetBSD__)
    205 const struct cdevsw altq_cdevsw = {
    206 	altqopen, altqclose, noread, nowrite, altqioctl,
    207 	nostop, notty, nopoll, nommap, nokqfilter
    208 };
    209 #endif
    210 
    211 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
    212 static
    213 #endif
    214 int
    215 altqopen(dev, flag, fmt, l)
    216 	dev_t dev;
    217 	int flag, fmt;
    218 	struct lwp *l;
    219 {
    220 	int unit = minor(dev);
    221 
    222 	if (unit == 0)
    223 		return (0);
    224 	if (unit < naltqsw)
    225 		return (*altqsw[unit].d_open)(dev, flag, fmt, l);
    226 
    227 	return ENXIO;
    228 }
    229 
    230 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
    231 static
    232 #endif
    233 int
    234 altqclose(dev, flag, fmt, l)
    235 	dev_t dev;
    236 	int flag, fmt;
    237 	struct lwp *l;
    238 {
    239 	int unit = minor(dev);
    240 
    241 	if (unit == 0)
    242 		return (0);
    243 	if (unit < naltqsw)
    244 		return (*altqsw[unit].d_close)(dev, flag, fmt, l);
    245 
    246 	return ENXIO;
    247 }
    248 
    249 #if !defined(__NetBSD__) && !defined(__OpenBSD__)
    250 static
    251 #endif
    252 int
    253 altqioctl(dev, cmd, addr, flag, l)
    254 	dev_t dev;
    255 	ioctlcmd_t cmd;
    256 	caddr_t addr;
    257 	int flag;
    258 	struct lwp *l;
    259 {
    260 	struct proc *p = l->l_proc;
    261 	int unit = minor(dev);
    262 
    263 	if (unit == 0) {
    264 		struct ifnet *ifp;
    265 		struct altqreq *typereq;
    266 		struct tbrreq *tbrreq;
    267 		int error;
    268 
    269 		switch (cmd) {
    270 		case ALTQGTYPE:
    271 		case ALTQTBRGET:
    272 			break;
    273 		default:
    274 #if (__FreeBSD_version > 400000)
    275 			if ((error = suser(p)) != 0)
    276 				return (error);
    277 #else
    278 			if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
    279 				return (error);
    280 #endif
    281 			break;
    282 		}
    283 
    284 		switch (cmd) {
    285 		case ALTQGTYPE:
    286 			typereq = (struct altqreq *)addr;
    287 			if ((ifp = ifunit(typereq->ifname)) == NULL)
    288 				return (EINVAL);
    289 			typereq->arg = (u_long)ifp->if_snd.altq_type;
    290 			return (0);
    291 		case ALTQTBRSET:
    292 			tbrreq = (struct tbrreq *)addr;
    293 			if ((ifp = ifunit(tbrreq->ifname)) == NULL)
    294 				return (EINVAL);
    295 			return tbr_set(&ifp->if_snd, &tbrreq->tb_prof);
    296 		case ALTQTBRGET:
    297 			tbrreq = (struct tbrreq *)addr;
    298 			if ((ifp = ifunit(tbrreq->ifname)) == NULL)
    299 				return (EINVAL);
    300 			return tbr_get(&ifp->if_snd, &tbrreq->tb_prof);
    301 		default:
    302 			return (EINVAL);
    303 		}
    304 	}
    305 	if (unit < naltqsw)
    306 		return (*altqsw[unit].d_ioctl)(dev, cmd, addr, flag, l);
    307 
    308 	return ENXIO;
    309 }
    310 
    311 #if !defined(__NetBSD__)
    312 static int altq_devsw_installed = 0;
    313 #endif
    314 
    315 #ifdef __FreeBSD__
    316 #if (__FreeBSD_version < 400000)
    317 #ifdef DEVFS
    318 static	void *altq_devfs_token[sizeof (altqsw) / sizeof (altqsw[0])];
    319 #endif
    320 
    321 static void
    322 altq_drvinit(unused)
    323 	void *unused;
    324 {
    325 	dev_t dev;
    326 #ifdef DEVFS
    327 	int i;
    328 #endif
    329 
    330 	if (!altq_devsw_installed) {
    331 		dev = makedev(CDEV_MAJOR,0);
    332 		cdevsw_add(&dev,&altq_cdevsw,NULL);
    333 		altq_devsw_installed = 1;
    334 #ifdef DEVFS
    335 		for (i=0; i<naltqsw; i++)
    336 			altq_devfs_token[i] =
    337 				devfs_add_devswf(&altq_cdevsw, i, DV_CHR,
    338 						 0, 0, 0644, altqsw[i].d_name);
    339 #endif
    340 		printf("altq: major number is %d\n", CDEV_MAJOR);
    341 	}
    342 }
    343 
    344 #else /* FreeBSD 4.x */
    345 
    346 static void
    347 altq_drvinit(unused)
    348 	void *unused;
    349 {
    350 	int unit;
    351 
    352 	cdevsw_add(&altq_cdevsw);
    353 	altq_devsw_installed = 1;
    354 	printf("altq: major number is %d\n", CDEV_MAJOR);
    355 
    356 	/* create minor devices */
    357 	for (unit = 0; unit < naltqsw; unit++)
    358 		make_dev(&altq_cdevsw, unit, 0, 0, 0644,
    359 			 altqsw[unit].d_name);
    360 }
    361 
    362 #endif /* FreeBSD 4.x */
    363 
    364 SYSINIT(altqdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,altq_drvinit,NULL)
    365 
    366 #endif
    367 
    368 #ifdef ALTQ_KLD
    369 /*
    370  * KLD support
    371  */
    372 static int altq_module_register __P((struct altq_module_data *));
    373 static int altq_module_deregister __P((struct altq_module_data *));
    374 
    375 static struct altq_module_data *altq_modules[ALTQT_MAX];
    376 static struct altqsw noqdisc = {"noq", noopen, noclose, noioctl};
    377 
    378 void altq_module_incref(type)
    379 	int type;
    380 {
    381 	if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
    382 		return;
    383 
    384 	altq_modules[type]->ref++;
    385 }
    386 
    387 void altq_module_declref(type)
    388 	int type;
    389 {
    390 	if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
    391 		return;
    392 
    393 	altq_modules[type]->ref--;
    394 }
    395 
    396 static int
    397 altq_module_register(mdata)
    398 	struct altq_module_data *mdata;
    399 {
    400 	int type = mdata->type;
    401 
    402 	if (type < 0 || type >= ALTQT_MAX)
    403 		return (EINVAL);
    404 	if (altqsw[type].d_open != noopen)
    405 		return (EBUSY);
    406 	altqsw[type] = *mdata->altqsw;	/* set discipline functions */
    407 	altq_modules[type] = mdata;	/* save module data pointer */
    408 	return (0);
    409 }
    410 
    411 static int
    412 altq_module_deregister(mdata)
    413 	struct altq_module_data *mdata;
    414 {
    415 	int type = mdata->type;
    416 
    417 	if (type < 0 || type >= ALTQT_MAX)
    418 		return (EINVAL);
    419 	if (mdata != altq_modules[type])
    420 		return (EINVAL);
    421 	if (altq_modules[type]->ref > 0)
    422 		return (EBUSY);
    423 	altqsw[type] = noqdisc;
    424 	altq_modules[type] = NULL;
    425 	return (0);
    426 }
    427 
    428 int
    429 altq_module_handler(mod, cmd, arg)
    430     module_t	mod;
    431     int cmd;
    432     void * arg;
    433 {
    434 	struct altq_module_data *data = (struct altq_module_data *)arg;
    435 	int	error = 0;
    436 
    437 	switch (cmd) {
    438 	case MOD_LOAD:
    439 		error = altq_module_register(data);
    440 		break;
    441 
    442 	case MOD_UNLOAD:
    443 		error = altq_module_deregister(data);
    444 		break;
    445 
    446 	default:
    447 		error = EINVAL;
    448 		break;
    449 	}
    450 
    451 	return(error);
    452 }
    453 
    454 #endif  /* ALTQ_KLD */
    455