Home | History | Annotate | Line # | Download | only in altq
altq_conf.c revision 1.2
      1 /*	$NetBSD: altq_conf.c,v 1.2 2000/12/14 08:49:50 thorpej Exp $	*/
      2 /*	$KAME: altq_conf.c,v 1.10 2000/12/14 08:12:45 thorpej 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 #ifdef ALTQ
     31 #if defined(__FreeBSD__) || defined(__NetBSD__)
     32 #include "opt_altq.h"
     33 #if (__FreeBSD__ != 2)
     34 #include "opt_inet.h"
     35 #ifdef __FreeBSD__
     36 #include "opt_inet6.h"
     37 #endif
     38 #endif
     39 #endif /* __FreeBSD__ || __NetBSD__ */
     40 
     41 /*
     42  * altq device interface.
     43  */
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/socket.h>
     47 #include <sys/kernel.h>
     48 #include <sys/proc.h>
     49 #include <sys/errno.h>
     50 #if defined(__FreeBSD__) && (__FreeBSD_version < 400000) && defined(DEVFS)
     51 #include <sys/devfsext.h>
     52 #endif /*DEVFS*/
     53 #include <net/if.h>
     54 
     55 #include <altq/altq.h>
     56 #include <altq/altq_conf.h>
     57 
     58 #ifdef ALTQ_CBQ
     59 altqdev_decl(cbq);
     60 #endif
     61 #ifdef ALTQ_WFQ
     62 altqdev_decl(wfq);
     63 #endif
     64 #ifdef ALTQ_AFMAP
     65 altqdev_decl(afm);
     66 #endif
     67 #ifdef ALTQ_FIFOQ
     68 altqdev_decl(fifoq);
     69 #endif
     70 #ifdef ALTQ_RED
     71 altqdev_decl(red);
     72 #endif
     73 #ifdef ALTQ_RIO
     74 altqdev_decl(rio);
     75 #endif
     76 #ifdef ALTQ_LOCALQ
     77 altqdev_decl(localq);
     78 #endif
     79 #ifdef ALTQ_HFSC
     80 altqdev_decl(hfsc);
     81 #endif
     82 #ifdef ALTQ_CDNR
     83 altqdev_decl(cdnr);
     84 #endif
     85 #ifdef ALTQ_BLUE
     86 altqdev_decl(blue);
     87 #endif
     88 #ifdef ALTQ_PRIQ
     89 altqdev_decl(priq);
     90 #endif
     91 
     92 /*
     93  * altq minor device (discipline) table
     94  */
     95 static struct altqsw altqsw[] = {				/* minor */
     96 	{"noq",	noopen,		noclose,	noioctl},  /* 0 (reserved) */
     97 #ifdef ALTQ_CBQ
     98 	{"cbq",	cbqopen,	cbqclose,	cbqioctl},	/* 1 */
     99 #else
    100 	{"noq",	noopen,		noclose,	noioctl},	/* 1 */
    101 #endif
    102 #ifdef ALTQ_WFQ
    103 	{"wfq",	wfqopen,	wfqclose,	wfqioctl},	/* 2 */
    104 #else
    105 	{"noq",	noopen,		noclose,	noioctl},	/* 2 */
    106 #endif
    107 #ifdef ALTQ_AFMAP
    108 	{"afm",	afmopen,	afmclose,	afmioctl},	/* 3 */
    109 #else
    110 	{"noq",	noopen,		noclose,	noioctl},	/* 3 */
    111 #endif
    112 #ifdef ALTQ_FIFOQ
    113 	{"fifoq", fifoqopen,	fifoqclose,	fifoqioctl},	/* 4 */
    114 #else
    115 	{"noq",	noopen,		noclose,	noioctl},	/* 4 */
    116 #endif
    117 #ifdef ALTQ_RED
    118 	{"red", redopen,	redclose,	redioctl},	/* 5 */
    119 #else
    120 	{"noq",	noopen,		noclose,	noioctl},	/* 5 */
    121 #endif
    122 #ifdef ALTQ_RIO
    123 	{"rio", rioopen,	rioclose,	rioioctl},	/* 6 */
    124 #else
    125 	{"noq",	noopen,		noclose,	noioctl},	/* 6 */
    126 #endif
    127 #ifdef ALTQ_LOCALQ
    128 	{"localq",localqopen,	localqclose,	localqioctl}, /* 7 (local use) */
    129 #else
    130 	{"noq",	noopen,		noclose,	noioctl},  /* 7 (local use) */
    131 #endif
    132 #ifdef ALTQ_HFSC
    133 	{"hfsc",hfscopen,	hfscclose,	hfscioctl},	/* 8 */
    134 #else
    135 	{"noq",	noopen,		noclose,	noioctl},	/* 8 */
    136 #endif
    137 #ifdef ALTQ_CDNR
    138 	{"cdnr",cdnropen,	cdnrclose,	cdnrioctl},	/* 9 */
    139 #else
    140 	{"noq",	noopen,		noclose,	noioctl},	/* 9 */
    141 #endif
    142 #ifdef ALTQ_BLUE
    143 	{"blue",blueopen,	blueclose,	blueioctl},	/* 10 */
    144 #else
    145 	{"noq",	noopen,		noclose,	noioctl},	/* 10 */
    146 #endif
    147 #ifdef ALTQ_PRIQ
    148 	{"priq",priqopen,	priqclose,	priqioctl},	/* 11 */
    149 #else
    150 	{"noq",	noopen,		noclose,	noioctl},	/* 11 */
    151 #endif
    152 };
    153 
    154 /*
    155  * altq major device support
    156  */
    157 int	naltqsw = sizeof (altqsw) / sizeof (altqsw[0]);
    158 
    159 #ifndef __OpenBSD__
    160 static	d_open_t	altqopen;
    161 static	d_close_t	altqclose;
    162 static	d_ioctl_t	altqioctl;
    163 #endif
    164 #ifdef __FreeBSD__
    165 static void altq_drvinit __P((void *));
    166 #else
    167 void	altqattach __P((int));
    168 #endif
    169 
    170 #if defined(__FreeBSD__)
    171 #define	CDEV_MAJOR 96		/* FreeBSD official number */
    172 #elif defined(__NetBSD__)
    173 #if defined(__i386__)
    174 #define	CDEV_MAJOR 75		/* NetBSD i386 (not official) */
    175 #elif defined(__alpha__)
    176 #define	CDEV_MAJOR 62		/* NetBSD alpha (not official) */
    177 #else
    178 #error arch not supported
    179 #endif
    180 #elif defined(__OpenBSD__)
    181 #if defined(__i386__)
    182 #define	CDEV_MAJOR 67		/* OpenBSD i386 (not official) */
    183 #elif defined(__alpha__)
    184 #define	CDEV_MAJOR 52		/* OpenBSD alpha (not 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 #elif defined(__NetBSD__)
    203 static struct cdevsw altq_cdevsw = cdev__oci_init(1,altq);
    204 #elif defined(__OpenBSD__)
    205 static struct cdevsw altq_cdevsw = {
    206 	altqopen, altqclose, 0, 0, altqioctl, 0,
    207 	0, 0, 0, 0 };
    208 #endif
    209 
    210 #if !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(__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(__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 
    310 static int altq_devsw_installed = 0;
    311 
    312 #ifdef __FreeBSD__
    313 #if (__FreeBSD_version < 400000)
    314 #ifdef DEVFS
    315 static	void *altq_devfs_token[sizeof (altqsw) / sizeof (altqsw[0])];
    316 #endif
    317 
    318 static void
    319 altq_drvinit(unused)
    320 	void *unused;
    321 {
    322 	dev_t dev;
    323 #ifdef DEVFS
    324 	int i;
    325 #endif
    326 
    327 	if (!altq_devsw_installed) {
    328 		dev = makedev(CDEV_MAJOR,0);
    329 		cdevsw_add(&dev,&altq_cdevsw,NULL);
    330 		altq_devsw_installed = 1;
    331 #ifdef DEVFS
    332 		for (i=0; i<naltqsw; i++)
    333 			altq_devfs_token[i] =
    334 				devfs_add_devswf(&altq_cdevsw, i, DV_CHR,
    335 						 0, 0, 0644, altqsw[i].d_name);
    336 #endif
    337 		printf("altq: major number is %d\n", CDEV_MAJOR);
    338 	}
    339 }
    340 
    341 #else /* FreeBSD 4.x */
    342 
    343 static void
    344 altq_drvinit(unused)
    345 	void *unused;
    346 {
    347 	int unit;
    348 
    349 	cdevsw_add(&altq_cdevsw);
    350 	altq_devsw_installed = 1;
    351 	printf("altq: major number is %d\n", CDEV_MAJOR);
    352 
    353 	/* create minor devices */
    354 	for (unit = 0; unit < naltqsw; unit++)
    355 		make_dev(&altq_cdevsw, unit, 0, 0, 0644,
    356 			 altqsw[unit].d_name);
    357 }
    358 
    359 #endif /* FreeBSD 4.x */
    360 
    361 SYSINIT(altqdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,altq_drvinit,NULL)
    362 
    363 #elif defined(__NetBSD__)||defined(__OpenBSD__)
    364 
    365 void
    366 altqattach(int unused)
    367 {
    368 	if (!altq_devsw_installed) {
    369 		bcopy(&altq_cdevsw,
    370 		      &cdevsw[CDEV_MAJOR],
    371 		      sizeof(struct cdevsw));
    372 		altq_devsw_installed = 1;
    373 		printf("altq: major number is %d\n", CDEV_MAJOR);
    374 	}
    375 }
    376 #else
    377 #error altqattach()??
    378 #endif
    379 
    380 #ifdef ALTQ_KLD
    381 /*
    382  * KLD support
    383  */
    384 static int altq_module_register __P((struct altq_module_data *));
    385 static int altq_module_deregister __P((struct altq_module_data *));
    386 
    387 static struct altq_module_data *altq_modules[ALTQT_MAX];
    388 static struct altqsw noqdisc = {"noq", noopen, noclose, noioctl};
    389 
    390 void altq_module_incref(type)
    391 	int type;
    392 {
    393 	if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
    394 		return;
    395 
    396 	altq_modules[type]->ref++;
    397 }
    398 
    399 void altq_module_declref(type)
    400 	int type;
    401 {
    402 	if (type < 0 || type >= ALTQT_MAX || altq_modules[type] == NULL)
    403 		return;
    404 
    405 	altq_modules[type]->ref--;
    406 }
    407 
    408 static int
    409 altq_module_register(mdata)
    410 	struct altq_module_data *mdata;
    411 {
    412 	int type = mdata->type;
    413 
    414 	if (type < 0 || type >= ALTQT_MAX)
    415 		return (EINVAL);
    416 	if (altqsw[type].d_open != noopen)
    417 		return (EBUSY);
    418 	altqsw[type] = *mdata->altqsw;	/* set discipline functions */
    419 	altq_modules[type] = mdata;	/* save module data pointer */
    420 	return (0);
    421 }
    422 
    423 static int
    424 altq_module_deregister(mdata)
    425 	struct altq_module_data *mdata;
    426 {
    427 	int type = mdata->type;
    428 
    429 	if (type < 0 || type >= ALTQT_MAX)
    430 		return (EINVAL);
    431 	if (mdata != altq_modules[type])
    432 		return (EINVAL);
    433 	if (altq_modules[type]->ref > 0)
    434 		return (EBUSY);
    435 	altqsw[type] = noqdisc;
    436 	altq_modules[type] = NULL;
    437 	return (0);
    438 }
    439 
    440 int
    441 altq_module_handler(mod, cmd, arg)
    442     module_t	mod;
    443     int cmd;
    444     void * arg;
    445 {
    446 	struct altq_module_data *data = (struct altq_module_data *)arg;
    447 	int	error = 0;
    448 
    449 	switch (cmd) {
    450 	case MOD_LOAD:
    451 		error = altq_module_register(data);
    452 		break;
    453 
    454 	case MOD_UNLOAD:
    455 		error = altq_module_deregister(data);
    456 		break;
    457 
    458 	default:
    459 		error = EINVAL;
    460 		break;
    461 	}
    462 
    463 	return(error);
    464 }
    465 
    466 #endif  /* ALTQ_KLD */
    467 
    468 #endif /* ALTQ */
    469