[BACK]Return to altq_fifoq.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / altq

File: [cvs.NetBSD.org] / src / sys / altq / altq_fifoq.c (download)

Revision 1.16, Sun Mar 4 05:59:01 2007 UTC (17 years, 1 month ago) by christos
Branch: MAIN
CVS Tags: yamt-x86pmap-base4, yamt-x86pmap-base3, yamt-x86pmap-base2, yamt-x86pmap-base, yamt-x86pmap, yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, yamt-nfs-mp-base9, yamt-nfs-mp-base8, yamt-nfs-mp-base7, yamt-nfs-mp-base6, yamt-nfs-mp-base5, yamt-nfs-mp-base4, yamt-nfs-mp-base3, yamt-nfs-mp-base2, yamt-nfs-mp-base11, yamt-nfs-mp-base10, yamt-nfs-mp-base, yamt-nfs-mp, yamt-lazymbuf-base15, yamt-lazymbuf-base14, yamt-kmem-base3, yamt-kmem-base2, yamt-kmem-base, yamt-kmem, yamt-idlelwp-base8, wrstuden-revivesa-base-4, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, vmlocking2-base3, vmlocking2-base2, vmlocking2-base1, vmlocking2, vmlocking-nbase, vmlocking-base, vmlocking, uebayasi-xip-base4, uebayasi-xip-base3, uebayasi-xip-base2, uebayasi-xip-base1, uebayasi-xip-base, uebayasi-xip, tls-maxphys-base, tls-earlyentropy-base, tls-earlyentropy, thorpej-atomic-base, thorpej-atomic, simonb-wapbl-nbase, simonb-wapbl-base, simonb-wapbl, rmind-uvmplock-nbase, rmind-uvmplock-base, rmind-uvmplock, rmind-smpnet-nbase, rmind-smpnet-base, rmind-smpnet, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, reinoud-bufcleanup-nbase, reinoud-bufcleanup-base, reinoud-bufcleanup, nick-nhusb-base-20160319, nick-nhusb-base-20151226, nick-nhusb-base-20150921, nick-nhusb-base-20150606, nick-nhusb-base-20150406, nick-nhusb-base, nick-net80211-sync-base, nick-net80211-sync, nick-hppapmap-base4, nick-hppapmap-base3, nick-hppapmap-base2, nick-hppapmap-base, nick-hppapmap, nick-csl-alignment-base5, nick-csl-alignment-base, nick-csl-alignment, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, mjf-ufs-trans-base, mjf-ufs-trans, mjf-devfs2-base, mjf-devfs2, mjf-devfs-base, mjf-devfs, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, khorben-n900, keiichi-mipv6-nbase, keiichi-mipv6-base, keiichi-mipv6, jymxensuspend-base, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, jruoho-x86intr-base, jruoho-x86intr, jmcneill-usbmp-pre-base2, jmcneill-usbmp-base9, jmcneill-usbmp-base8, jmcneill-usbmp-base7, jmcneill-usbmp-base6, jmcneill-usbmp-base5, jmcneill-usbmp-base4, jmcneill-usbmp-base3, jmcneill-usbmp-base2, jmcneill-usbmp-base10, jmcneill-usbmp-base, jmcneill-usbmp, jmcneill-pm-base, jmcneill-pm, jmcneill-base, jmcneill-audiomp3-base, jmcneill-audiomp3, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, haad-nbase2, haad-dm-base2, haad-dm-base1, haad-dm-base, haad-dm, cube-autoconf-base, cube-autoconf, cherry-xenmp-base, cherry-xenmp, bouyer-xeni386-nbase, bouyer-xeni386-merge1, bouyer-xeni386-base, bouyer-xeni386, bouyer-xenamd64-base2, bouyer-xenamd64-base, bouyer-xenamd64, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, ad-socklock-base1, ad-audiomp2-base, ad-audiomp2, ad-audiomp-base-1
Branch point for: tls-maxphys, nick-nhusb
Changes since 1.15: +3 -3 lines

Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.

/*	$NetBSD: altq_fifoq.c,v 1.16 2007/03/04 05:59:01 christos Exp $	*/
/*	$KAME: altq_fifoq.c,v 1.12 2003/07/10 12:07:48 kjc Exp $	*/

/*
 * Copyright (C) 1997-2002
 *	Sony Computer Science Laboratories Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY SONY CSL AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL SONY CSL OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: altq_fifoq.c,v 1.16 2007/03/04 05:59:01 christos Exp $");

#ifdef _KERNEL_OPT
#include "opt_altq.h"
#endif

#ifdef ALTQ_FIFOQ  /* fifoq is enabled by ALTQ_FIFOQ option in opt_altq.h */

/*
 * FIFOQ is an altq sample implementation.  There will be little
 * need to use FIFOQ as an alternative queueing scheme.
 * But this code is provided as a template for those who want to
 * write their own queueing schemes.
 */

#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/kauth.h>

#include <net/if.h>
#include <net/if_types.h>
#include <netinet/in.h>

#include <altq/altq.h>
#include <altq/altq_conf.h>
#include <altq/altq_fifoq.h>

#ifdef ALTQ3_COMPAT

#define	FIFOQ_STATS	/* collect statistics */

/* fifoq_list keeps all fifoq_state_t's allocated. */
static fifoq_state_t *fifoq_list = NULL;

/* internal function prototypes */
static int		fifoq_enqueue(struct ifaltq *, struct mbuf *,
				      struct altq_pktattr *);
static struct mbuf 	*fifoq_dequeue(struct ifaltq *, int);
static int 		fifoq_detach(fifoq_state_t *);
static int		fifoq_request(struct ifaltq *, int, void *);
static void 		fifoq_purge(fifoq_state_t *);

/*
 * fifoq device interface
 */
altqdev_decl(fifoq);

int
fifoqopen(dev_t dev, int flag, int fmt,
    struct lwp *l)
{
	/* everything will be done when the queueing scheme is attached. */
	return 0;
}

/*
 * there are 2 ways to act on close.
 *   detach-all-on-close:
 *	use for the daemon style approach.  if the daemon dies, all the
 *	resource will be released.
 *   no-action-on-close:
 *	use for the command style approach.  (e.g.  fifoq on/off)
 *
 * note: close is called not on every close but when the last reference
 *       is removed (only once with multiple simultaneous references.)
 */
int
fifoqclose(dev_t dev, int flag, int fmt,
    struct lwp *l)
{
	fifoq_state_t *q;
	int err, error = 0;

	while ((q = fifoq_list) != NULL) {
		/* destroy all */
		err = fifoq_detach(q);
		if (err != 0 && error == 0)
			error = err;
	}

	return error;
}

int
fifoqioctl(dev_t dev, ioctlcmd_t cmd, void *addr, int flag,
    struct lwp *l)
{
	fifoq_state_t *q;
	struct fifoq_interface *ifacep;
	struct ifnet *ifp;
	int	error = 0;

	/* check super-user privilege */
	switch (cmd) {
	case FIFOQ_GETSTATS:
		break;
	default:
#if (__FreeBSD_version > 400000)
		if ((error = suser(p)) != 0)
			return (error);
#else
		if ((error = kauth_authorize_network(l->l_cred,
		    KAUTH_NETWORK_ALTQ, KAUTH_REQ_NETWORK_ALTQ_FIFOQ, NULL,
		    NULL, NULL)) != 0)
			return (error);
#endif
		break;
	}

	switch (cmd) {
	case FIFOQ_ENABLE:
		ifacep = (struct fifoq_interface *)addr;
		if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
		    == NULL) {
			error = EBADF;
			break;
		}
		error = altq_enable(q->q_ifq);
		break;

	case FIFOQ_DISABLE:
		ifacep = (struct fifoq_interface *)addr;
		if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
		    == NULL) {
			error = EBADF;
			break;
		}
		error = altq_disable(q->q_ifq);
		break;

	case FIFOQ_IF_ATTACH:
		ifp = ifunit(((struct fifoq_interface *)addr)->fifoq_ifname);
		if (ifp == NULL) {
			error = ENXIO;
			break;
		}

		/* allocate and initialize fifoq_state_t */
		q = malloc(sizeof(fifoq_state_t), M_DEVBUF, M_WAITOK|M_ZERO);
		if (q == NULL) {
			error = ENOMEM;
			break;
		}

		q->q_ifq = &ifp->if_snd;
		q->q_head = q->q_tail = NULL;
		q->q_len = 0;
		q->q_limit = FIFOQ_LIMIT;

		/*
		 * set FIFOQ to this ifnet structure.
		 */
		error = altq_attach(q->q_ifq, ALTQT_FIFOQ, q,
				    fifoq_enqueue, fifoq_dequeue, fifoq_request,
				    NULL, NULL);
		if (error) {
			free(q, M_DEVBUF);
			break;
		}

		/* add this state to the fifoq list */
		q->q_next = fifoq_list;
		fifoq_list = q;
		break;

	case FIFOQ_IF_DETACH:
		ifacep = (struct fifoq_interface *)addr;
		if ((q = altq_lookup(ifacep->fifoq_ifname, ALTQT_FIFOQ))
		    == NULL) {
			error = EBADF;
			break;
		}
		error = fifoq_detach(q);
		break;

	case FIFOQ_GETSTATS:
		do {
			struct fifoq_getstats *q_stats;

			q_stats = (struct fifoq_getstats *)addr;
			if ((q = altq_lookup(q_stats->iface.fifoq_ifname,
					     ALTQT_FIFOQ)) == NULL) {
				error = EBADF;
				break;
			}

			q_stats->q_len		= q->q_len;
			q_stats->q_limit 	= q->q_limit;
			q_stats->xmit_cnt	= q->q_stats.xmit_cnt;
			q_stats->drop_cnt 	= q->q_stats.drop_cnt;
			q_stats->period   	= q->q_stats.period;
		} while (/*CONSTCOND*/ 0);
		break;

	case FIFOQ_CONFIG:
		do {
			struct fifoq_conf *fc;
			int limit;

			fc = (struct fifoq_conf *)addr;
			if ((q = altq_lookup(fc->iface.fifoq_ifname,
					     ALTQT_FIFOQ)) == NULL) {
				error = EBADF;
				break;
			}
			limit = fc->fifoq_limit;
			if (limit < 0)
				limit = 0;
			q->q_limit = limit;
			fc->fifoq_limit = limit;
		} while (/*CONSTCOND*/ 0);
		break;

	default:
		error = EINVAL;
		break;
	}
	return error;
}

/*
 * fifoq support routines
 */

/*
 * enqueue routine:
 *
 *	returns: 0 when successfully queued.
 *		 ENOBUFS when drop occurs.
 */
static int
fifoq_enqueue(struct ifaltq *ifq, struct mbuf *m,
    struct altq_pktattr *pktattr)
{
	fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;

	/* if the queue is full, drop the incoming packet(drop-tail) */
	if (q->q_len >= q->q_limit) {
#ifdef FIFOQ_STATS
		PKTCNTR_ADD(&q->q_stats.drop_cnt, m_pktlen(m));
#endif
		m_freem(m);
		return (ENOBUFS);
	}

	/* enqueue the packet at the taile of the queue */
	m->m_nextpkt = NULL;
	if (q->q_tail == NULL)
		q->q_head = m;
	else
		q->q_tail->m_nextpkt = m;
	q->q_tail = m;
	q->q_len++;
	ifq->ifq_len++;
	return 0;
}

/*
 * dequeue routine:
 *	must be called in splnet.
 *
 *	returns: mbuf dequeued.
 *		 NULL when no packet is available in the queue.
 */
/*
 * ALTDQ_PEEK is provided for drivers which need to know the next packet
 * to send in advance.
 * when ALTDQ_PEEK is specified, the next packet to be dequeued is
 * returned without dequeueing the packet.
 * when ALTDQ_DEQUEUE is called *immediately after* an ALTDQ_PEEK
 * operation, the same packet should be returned.
 */
static struct mbuf *
fifoq_dequeue(struct ifaltq *ifq, int op)
{
	fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;
	struct mbuf *m = NULL;

	if (op == ALTDQ_POLL)
		return (q->q_head);

	if ((m = q->q_head) == NULL)
		return (NULL);

	if ((q->q_head = m->m_nextpkt) == NULL)
		q->q_tail = NULL;
	m->m_nextpkt = NULL;
	q->q_len--;
	ifq->ifq_len--;
#ifdef FIFOQ_STATS
	PKTCNTR_ADD(&q->q_stats.xmit_cnt, m_pktlen(m));
	if (q->q_len == 0)
		q->q_stats.period++;
#endif
	return (m);
}

static int
fifoq_request(struct ifaltq *ifq, int req, void *arg)
{
	fifoq_state_t *q = (fifoq_state_t *)ifq->altq_disc;

	switch (req) {
	case ALTRQ_PURGE:
		fifoq_purge(q);
		break;
	}
	return (0);
}


static int
fifoq_detach(fifoq_state_t *q)
{
	fifoq_state_t *tmp;
	int error = 0;

	if (ALTQ_IS_ENABLED(q->q_ifq))
		altq_disable(q->q_ifq);

	fifoq_purge(q);

	if ((error = altq_detach(q->q_ifq)))
		return (error);

	if (fifoq_list == q)
		fifoq_list = q->q_next;
	else {
		for (tmp = fifoq_list; tmp != NULL; tmp = tmp->q_next)
			if (tmp->q_next == q) {
				tmp->q_next = q->q_next;
				break;
			}
		if (tmp == NULL)
			printf("fifoq_detach: no state in fifoq_list!\n");
	}

	free(q, M_DEVBUF);
	return (error);
}

/*
 * fifoq_purge
 * should be called in splnet or after disabling the fifoq.
 */
static void
fifoq_purge(fifoq_state_t *q)
{
	struct mbuf *m;

	while ((m = q->q_head) != NULL) {
		q->q_head = m->m_nextpkt;
		m_freem(m);
	}
	q->q_tail = NULL;
	q->q_len = 0;
	if (ALTQ_IS_ENABLED(q->q_ifq))
		q->q_ifq->ifq_len = 0;
}

#ifdef KLD_MODULE

static struct altqsw fifoq_sw =
	{"fifoq", fifoqopen, fifoqclose, fifoqioctl};

ALTQ_MODULE(altq_fifoq, ALTQT_FIFOQ, &fifoq_sw);

#endif /* KLD_MODULE */

#endif /* ALTQ3_COMPAT */
#endif /* ALTQ_FIFOQ */