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