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