Home | History | Annotate | Line # | Download | only in altq
altq_fifoq.c revision 1.7.12.2
      1 /*	$NetBSD: altq_fifoq.c,v 1.7.12.2 2006/06/09 19:52:35 peter Exp $	*/
      2 /*	$KAME: altq_fifoq.c,v 1.12 2003/07/10 12:07:48 kjc Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1997-2002
      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_fifoq.c,v 1.7.12.2 2006/06/09 19:52:35 peter Exp $");
     32 
     33 #ifdef _KERNEL_OPT
     34 #include "opt_altq.h"
     35 #endif
     36 
     37 #ifdef ALTQ_FIFOQ  /* fifoq is enabled by ALTQ_FIFOQ option in opt_altq.h */
     38 
     39 /*
     40  * FIFOQ is an altq sample implementation.  There will be little
     41  * need to use FIFOQ as an alternative queueing scheme.
     42  * But this code is provided as a template for those who want to
     43  * write their own queueing schemes.
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/malloc.h>
     48 #include <sys/mbuf.h>
     49 #include <sys/socket.h>
     50 #include <sys/sockio.h>
     51 #include <sys/systm.h>
     52 #include <sys/proc.h>
     53 #include <sys/errno.h>
     54 #include <sys/kernel.h>
     55 #include <sys/kauth.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_types.h>
     59 #include <netinet/in.h>
     60 
     61 #include <altq/altq.h>
     62 #include <altq/altq_conf.h>
     63 #include <altq/altq_fifoq.h>
     64 
     65 #ifdef ALTQ3_COMPAT
     66 
     67 #define	FIFOQ_STATS	/* collect statistics */
     68 
     69 /* fifoq_list keeps all fifoq_state_t's allocated. */
     70 static fifoq_state_t *fifoq_list = NULL;
     71 
     72 /* internal function prototypes */
     73 static int		fifoq_enqueue(struct ifaltq *, struct mbuf *,
     74 				      struct altq_pktattr *);
     75 static struct mbuf 	*fifoq_dequeue(struct ifaltq *, int);
     76 static int 		fifoq_detach(fifoq_state_t *);
     77 static int		fifoq_request(struct ifaltq *, int, void *);
     78 static void 		fifoq_purge(fifoq_state_t *);
     79 
     80 /*
     81  * fifoq device interface
     82  */
     83 altqdev_decl(fifoq);
     84 
     85 int
     86 fifoqopen(dev, flag, fmt, l)
     87 	dev_t dev;
     88 	int flag, fmt;
     89 	struct lwp *l;
     90 {
     91 	/* everything will be done when the queueing scheme is attached. */
     92 	return 0;
     93 }
     94 
     95 /*
     96  * there are 2 ways to act on close.
     97  *   detach-all-on-close:
     98  *	use for the daemon style approach.  if the daemon dies, all the
     99  *	resource will be released.
    100  *   no-action-on-close:
    101  *	use for the command style approach.  (e.g.  fifoq on/off)
    102  *
    103  * note: close is called not on every close but when the last reference
    104  *       is removed (only once with multiple simultaneous references.)
    105  */
    106 int
    107 fifoqclose(dev, flag, fmt, l)
    108 	dev_t dev;
    109 	int flag, fmt;
    110 	struct lwp *l;
    111 {
    112 	fifoq_state_t *q;
    113 	int err, error = 0;
    114 
    115 	while ((q = fifoq_list) != NULL) {
    116 		/* destroy all */
    117 		err = fifoq_detach(q);
    118 		if (err != 0 && error == 0)
    119 			error = err;
    120 	}
    121 
    122 	return error;
    123 }
    124 
    125 int
    126 fifoqioctl(dev, cmd, addr, flag, l)
    127 	dev_t dev;
    128 	ioctlcmd_t cmd;
    129 	caddr_t addr;
    130 	int flag;
    131 	struct lwp *l;
    132 {
    133 	fifoq_state_t *q;
    134 	struct fifoq_interface *ifacep;
    135 	struct ifnet *ifp;
    136 	struct proc *p = l->l_proc;
    137 	int	error = 0;
    138 
    139 	/* check super-user privilege */
    140 	switch (cmd) {
    141 	case FIFOQ_GETSTATS:
    142 		break;
    143 	default:
    144 #if (__FreeBSD_version > 400000)
    145 		if ((error = suser(p)) != 0)
    146 			return (error);
    147 #else
    148 		if ((error = kauth_authorize_generic(p->p_cred,
    149 		    KAUTH_GENERIC_ISSUSER, &p->p_acflag)) != 0)
    150 			return (error);
    151 #endif
    152 		break;
    153 	}
    154 
    155 	switch (cmd) {
    156 	case FIFOQ_ENABLE:
    157 		ifacep = (struct fifoq_interface *)addr;
    158 		if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
    159 		    == NULL) {
    160 			error = EBADF;
    161 			break;
    162 		}
    163 		error = altq_enable(q->q_ifq);
    164 		break;
    165 
    166 	case FIFOQ_DISABLE:
    167 		ifacep = (struct fifoq_interface *)addr;
    168 		if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
    169 		    == NULL) {
    170 			error = EBADF;
    171 			break;
    172 		}
    173 		error = altq_disable(q->q_ifq);
    174 		break;
    175 
    176 	case FIFOQ_IF_ATTACH:
    177 		ifp = ifunit(((struct fifoq_interface *)addr)->fifoq_ifname);
    178 		if (ifp == NULL) {
    179 			error = ENXIO;
    180 			break;
    181 		}
    182 
    183 		/* allocate and initialize fifoq_state_t */
    184 		q = malloc(sizeof(fifoq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
    185 		if (q == NULL) {
    186 			error = ENOMEM;
    187 			break;
    188 		}
    189 
    190 		q->q_ifq = &ifp->if_snd;
    191 		q->q_head = q->q_tail = NULL;
    192 		q->q_len = 0;
    193 		q->q_limit = FIFOQ_LIMIT;
    194 
    195 		/*
    196 		 * set FIFOQ to this ifnet structure.
    197 		 */
    198 		error = altq_attach(q->q_ifq, ALTQT_FIFOQ, q,
    199 				    fifoq_enqueue, fifoq_dequeue, fifoq_request,
    200 				    NULL, NULL);
    201 		if (error) {
    202 			free(q, M_DEVBUF);
    203 			break;
    204 		}
    205 
    206 		/* add this state to the fifoq list */
    207 		q->q_next = fifoq_list;
    208 		fifoq_list = q;
    209 		break;
    210 
    211 	case FIFOQ_IF_DETACH:
    212 		ifacep = (struct fifoq_interface *)addr;
    213 		if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
    214 		    == NULL) {
    215 			error = EBADF;
    216 			break;
    217 		}
    218 		error = fifoq_detach(q);
    219 		break;
    220 
    221 	case FIFOQ_GETSTATS:
    222 		do {
    223 			struct fifoq_getstats *q_stats;
    224 
    225 			q_stats = (struct fifoq_getstats *)addr;
    226 			if ((q = altq_lookup(q_stats->iface.fifoq_ifname,
    227 					     ALTQT_FIFOQ)) == NULL) {
    228 				error = EBADF;
    229 				break;
    230 			}
    231 
    232 			q_stats->q_len		= q->q_len;
    233 			q_stats->q_limit 	= q->q_limit;
    234 			q_stats->xmit_cnt	= q->q_stats.xmit_cnt;
    235 			q_stats->drop_cnt 	= q->q_stats.drop_cnt;
    236 			q_stats->period   	= q->q_stats.period;
    237 		} while (/*CONSTCOND*/ 0);
    238 		break;
    239 
    240 	case FIFOQ_CONFIG:
    241 		do {
    242 			struct fifoq_conf *fc;
    243 			int limit;
    244 
    245 			fc = (struct fifoq_conf *)addr;
    246 			if ((q = altq_lookup(fc->iface.fifoq_ifname,
    247 					     ALTQT_FIFOQ)) == NULL) {
    248 				error = EBADF;
    249 				break;
    250 			}
    251 			limit = fc->fifoq_limit;
    252 			if (limit < 0)
    253 				limit = 0;
    254 			q->q_limit = limit;
    255 			fc->fifoq_limit = limit;
    256 		} while (/*CONSTCOND*/ 0);
    257 		break;
    258 
    259 	default:
    260 		error = EINVAL;
    261 		break;
    262 	}
    263 	return error;
    264 }
    265 
    266 /*
    267  * fifoq support routines
    268  */
    269 
    270 /*
    271  * enqueue routine:
    272  *
    273  *	returns: 0 when successfully queued.
    274  *		 ENOBUFS when drop occurs.
    275  */
    276 static int
    277 fifoq_enqueue(ifq, m, pktattr)
    278 	struct ifaltq *ifq;
    279 	struct mbuf *m;
    280 	struct altq_pktattr *pktattr;
    281 {
    282 	fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
    283 
    284 	/* if the queue is full, drop the incoming packet(drop-tail) */
    285 	if (q->q_len >= q->q_limit) {
    286 #ifdef FIFOQ_STATS
    287 		PKTCNTR_ADD(&q->q_stats.drop_cnt, m_pktlen(m));
    288 #endif
    289 		m_freem(m);
    290 		return (ENOBUFS);
    291 	}
    292 
    293 	/* enqueue the packet at the taile of the queue */
    294 	m->m_nextpkt = NULL;
    295 	if (q->q_tail == NULL)
    296 		q->q_head = m;
    297 	else
    298 		q->q_tail->m_nextpkt = m;
    299 	q->q_tail = m;
    300 	q->q_len++;
    301 	ifq->ifq_len++;
    302 	return 0;
    303 }
    304 
    305 /*
    306  * dequeue routine:
    307  *	must be called in splnet.
    308  *
    309  *	returns: mbuf dequeued.
    310  *		 NULL when no packet is available in the queue.
    311  */
    312 /*
    313  * ALTDQ_PEEK is provided for drivers which need to know the next packet
    314  * to send in advance.
    315  * when ALTDQ_PEEK is specified, the next packet to be dequeued is
    316  * returned without dequeueing the packet.
    317  * when ALTDQ_DEQUEUE is called *immediately after* an ALTDQ_PEEK
    318  * operation, the same packet should be returned.
    319  */
    320 static struct mbuf *
    321 fifoq_dequeue(ifq, op)
    322 	struct ifaltq *ifq;
    323 	int op;
    324 {
    325 	fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
    326 	struct mbuf *m = NULL;
    327 
    328 	if (op == ALTDQ_POLL)
    329 		return (q->q_head);
    330 
    331 	if ((m = q->q_head) == NULL)
    332 		return (NULL);
    333 
    334 	if ((q->q_head = m->m_nextpkt) == NULL)
    335 		q->q_tail = NULL;
    336 	m->m_nextpkt = NULL;
    337 	q->q_len--;
    338 	ifq->ifq_len--;
    339 #ifdef FIFOQ_STATS
    340 	PKTCNTR_ADD(&q->q_stats.xmit_cnt, m_pktlen(m));
    341 	if (q->q_len == 0)
    342 		q->q_stats.period++;
    343 #endif
    344 	return (m);
    345 }
    346 
    347 static int
    348 fifoq_request(ifq, req, arg)
    349 	struct ifaltq *ifq;
    350 	int req;
    351 	void *arg;
    352 {
    353 	fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
    354 
    355 	switch (req) {
    356 	case ALTRQ_PURGE:
    357 		fifoq_purge(q);
    358 		break;
    359 	}
    360 	return (0);
    361 }
    362 
    363 
    364 static int fifoq_detach(q)
    365 	fifoq_state_t *q;
    366 {
    367 	fifoq_state_t *tmp;
    368 	int error = 0;
    369 
    370 	if (ALTQ_IS_ENABLED(q->q_ifq))
    371 		altq_disable(q->q_ifq);
    372 
    373 	fifoq_purge(q);
    374 
    375 	if ((error = altq_detach(q->q_ifq)))
    376 		return (error);
    377 
    378 	if (fifoq_list == q)
    379 		fifoq_list = q->q_next;
    380 	else {
    381 		for (tmp = fifoq_list; tmp != NULL; tmp = tmp->q_next)
    382 			if (tmp->q_next == q) {
    383 				tmp->q_next = q->q_next;
    384 				break;
    385 			}
    386 		if (tmp == NULL)
    387 			printf("fifoq_detach: no state in fifoq_list!\n");
    388 	}
    389 
    390 	free(q, M_DEVBUF);
    391 	return (error);
    392 }
    393 
    394 /*
    395  * fifoq_purge
    396  * should be called in splnet or after disabling the fifoq.
    397  */
    398 static void fifoq_purge(q)
    399 	fifoq_state_t *q;
    400 {
    401 	struct mbuf *m;
    402 
    403 	while ((m = q->q_head) != NULL) {
    404 		q->q_head = m->m_nextpkt;
    405 		m_freem(m);
    406 	}
    407 	q->q_tail = NULL;
    408 	q->q_len = 0;
    409 	if (ALTQ_IS_ENABLED(q->q_ifq))
    410 		q->q_ifq->ifq_len = 0;
    411 }
    412 
    413 #ifdef KLD_MODULE
    414 
    415 static struct altqsw fifoq_sw =
    416 	{"fifoq", fifoqopen, fifoqclose, fifoqioctl};
    417 
    418 ALTQ_MODULE(altq_fifoq, ALTQT_FIFOQ, &fifoq_sw);
    419 
    420 #endif /* KLD_MODULE */
    421 
    422 #endif /* ALTQ3_COMPAT */
    423 #endif /* ALTQ_FIFOQ */
    424