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