Home | History | Annotate | Line # | Download | only in altq
altq_classq.h revision 1.1
      1  1.1  thorpej /*	$KAME: altq_classq.h,v 1.3 2000/07/25 10:12:29 kjc Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*
      4  1.1  thorpej  * Copyright (c) 1991-1997 Regents of the University of California.
      5  1.1  thorpej  * All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1  thorpej  * are met:
     10  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     16  1.1  thorpej  *    must display the following acknowledgement:
     17  1.1  thorpej  *	This product includes software developed by the Network Research
     18  1.1  thorpej  *	Group at Lawrence Berkeley Laboratory.
     19  1.1  thorpej  * 4. Neither the name of the University nor of the Laboratory may be used
     20  1.1  thorpej  *    to endorse or promote products derived from this software without
     21  1.1  thorpej  *    specific prior written permission.
     22  1.1  thorpej  *
     23  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1  thorpej  * SUCH DAMAGE.
     34  1.1  thorpej  */
     35  1.1  thorpej /*
     36  1.1  thorpej  * class queue definitions extracted from rm_class.h.
     37  1.1  thorpej  */
     38  1.1  thorpej #ifndef _ALTQ_ALTQ_CLASSQ_H_
     39  1.1  thorpej #define	_ALTQ_ALTQ_CLASSQ_H_
     40  1.1  thorpej 
     41  1.1  thorpej #ifdef __cplusplus
     42  1.1  thorpej extern "C" {
     43  1.1  thorpej #endif
     44  1.1  thorpej 
     45  1.1  thorpej /*
     46  1.1  thorpej  * Packet Queue types: RED or DROPHEAD.
     47  1.1  thorpej  */
     48  1.1  thorpej #define	Q_DROPHEAD	0x00
     49  1.1  thorpej #define	Q_RED		0x01
     50  1.1  thorpej #define	Q_RIO		0x02
     51  1.1  thorpej #define	Q_DROPTAIL	0x03
     52  1.1  thorpej 
     53  1.1  thorpej #ifdef _KERNEL
     54  1.1  thorpej 
     55  1.1  thorpej /*
     56  1.1  thorpej  * Packet Queue strcutures and macros to manipulate them.
     57  1.1  thorpej  */
     58  1.1  thorpej struct _class_queue_ {
     59  1.1  thorpej 	struct mbuf	*tail_;	/* Tail of packet queue */
     60  1.1  thorpej 	int	qlen_;		/* Queue length (in number of packets) */
     61  1.1  thorpej 	int	qlim_;		/* Queue limit (in number of packets*) */
     62  1.1  thorpej 	int	qtype_;		/* Queue type */
     63  1.1  thorpej };
     64  1.1  thorpej 
     65  1.1  thorpej typedef struct _class_queue_	class_queue_t;
     66  1.1  thorpej 
     67  1.1  thorpej #define	qtype(q)	(q)->qtype_		/* Get queue type */
     68  1.1  thorpej #define	qlimit(q)	(q)->qlim_		/* Max packets to be queued */
     69  1.1  thorpej #define	qlen(q)		(q)->qlen_		/* Current queue length. */
     70  1.1  thorpej #define	qtail(q)	(q)->tail_		/* Tail of the queue */
     71  1.1  thorpej #define	qhead(q)	((q)->tail_ ? (q)->tail_->m_nextpkt : NULL)
     72  1.1  thorpej 
     73  1.1  thorpej #define	qempty(q)	((q)->qlen_ == 0)	/* Is the queue empty?? */
     74  1.1  thorpej #define	q_is_red(q)	((q)->qtype_ == Q_RED)	/* Is the queue a red queue */
     75  1.1  thorpej #define	q_is_rio(q)	((q)->qtype_ == Q_RIO)	/* Is the queue a rio queue */
     76  1.1  thorpej #define	q_is_red_or_rio(q)	((q)->qtype_ == Q_RED || (q)->qtype_ == Q_RIO)
     77  1.1  thorpej 
     78  1.1  thorpej #if !defined(__GNUC__) || defined(ALTQ_DEBUG)
     79  1.1  thorpej 
     80  1.1  thorpej extern void		_addq(class_queue_t *, struct mbuf *);
     81  1.1  thorpej extern struct mbuf	*_getq(class_queue_t *);
     82  1.1  thorpej extern struct mbuf	*_getq_tail(class_queue_t *);
     83  1.1  thorpej extern struct mbuf	*_getq_random(class_queue_t *);
     84  1.1  thorpej extern void		_removeq(class_queue_t *, struct mbuf *);
     85  1.1  thorpej extern void		_flushq(class_queue_t *);
     86  1.1  thorpej 
     87  1.1  thorpej #else /* __GNUC__ && !ALTQ_DEBUG */
     88  1.1  thorpej /*
     89  1.1  thorpej  * inlined versions
     90  1.1  thorpej  */
     91  1.1  thorpej static __inline void
     92  1.1  thorpej _addq(class_queue_t *q, struct mbuf *m)
     93  1.1  thorpej {
     94  1.1  thorpej         struct mbuf *m0;
     95  1.1  thorpej 
     96  1.1  thorpej 	if ((m0 = qtail(q)) != NULL)
     97  1.1  thorpej 		m->m_nextpkt = m0->m_nextpkt;
     98  1.1  thorpej 	else
     99  1.1  thorpej 		m0 = m;
    100  1.1  thorpej 	m0->m_nextpkt = m;
    101  1.1  thorpej 	qtail(q) = m;
    102  1.1  thorpej 	qlen(q)++;
    103  1.1  thorpej }
    104  1.1  thorpej 
    105  1.1  thorpej static __inline struct mbuf *
    106  1.1  thorpej _getq(class_queue_t *q)
    107  1.1  thorpej {
    108  1.1  thorpej 	struct mbuf  *m, *m0;
    109  1.1  thorpej 
    110  1.1  thorpej 	if ((m = qtail(q)) == NULL)
    111  1.1  thorpej 		return (NULL);
    112  1.1  thorpej 	if ((m0 = m->m_nextpkt) != m)
    113  1.1  thorpej 		m->m_nextpkt = m0->m_nextpkt;
    114  1.1  thorpej 	else
    115  1.1  thorpej 		qtail(q) = NULL;
    116  1.1  thorpej 	qlen(q)--;
    117  1.1  thorpej 	return (m0);
    118  1.1  thorpej }
    119  1.1  thorpej 
    120  1.1  thorpej /* drop a packet at the tail of the queue */
    121  1.1  thorpej static __inline struct mbuf *
    122  1.1  thorpej _getq_tail(class_queue_t *q)
    123  1.1  thorpej {
    124  1.1  thorpej 	struct mbuf *m, *m0, *prev;
    125  1.1  thorpej 
    126  1.1  thorpej 	if ((m = m0 = qtail(q)) == NULL)
    127  1.1  thorpej 		return NULL;
    128  1.1  thorpej 	do {
    129  1.1  thorpej 		prev = m0;
    130  1.1  thorpej 		m0 = m0->m_nextpkt;
    131  1.1  thorpej 	} while (m0 != m);
    132  1.1  thorpej 	prev->m_nextpkt = m->m_nextpkt;
    133  1.1  thorpej 	if (prev == m)
    134  1.1  thorpej 		qtail(q) = NULL;
    135  1.1  thorpej 	else
    136  1.1  thorpej 		qtail(q) = prev;
    137  1.1  thorpej 	qlen(q)--;
    138  1.1  thorpej 	return (m);
    139  1.1  thorpej }
    140  1.1  thorpej 
    141  1.1  thorpej /* randomly select a packet in the queue */
    142  1.1  thorpej static __inline struct mbuf *
    143  1.1  thorpej _getq_random(class_queue_t *q)
    144  1.1  thorpej {
    145  1.1  thorpej 	struct mbuf *m;
    146  1.1  thorpej 	int i, n;
    147  1.1  thorpej 
    148  1.1  thorpej 	if ((m = qtail(q)) == NULL)
    149  1.1  thorpej 		return NULL;
    150  1.1  thorpej 	if (m->m_nextpkt == m)
    151  1.1  thorpej 		qtail(q) = NULL;
    152  1.1  thorpej 	else {
    153  1.1  thorpej 		struct mbuf *prev = NULL;
    154  1.1  thorpej 
    155  1.1  thorpej 		n = random() % qlen(q) + 1;
    156  1.1  thorpej 		for (i = 0; i < n; i++) {
    157  1.1  thorpej 			prev = m;
    158  1.1  thorpej 			m = m->m_nextpkt;
    159  1.1  thorpej 		}
    160  1.1  thorpej 		prev->m_nextpkt = m->m_nextpkt;
    161  1.1  thorpej 		if (m == qtail(q))
    162  1.1  thorpej 			qtail(q) = prev;
    163  1.1  thorpej 	}
    164  1.1  thorpej 	qlen(q)--;
    165  1.1  thorpej 	return (m);
    166  1.1  thorpej }
    167  1.1  thorpej 
    168  1.1  thorpej static __inline void
    169  1.1  thorpej _removeq(class_queue_t *q, struct mbuf *m)
    170  1.1  thorpej {
    171  1.1  thorpej 	struct mbuf *m0, *prev;
    172  1.1  thorpej 
    173  1.1  thorpej 	m0 = qtail(q);
    174  1.1  thorpej 	do {
    175  1.1  thorpej 		prev = m0;
    176  1.1  thorpej 		m0 = m0->m_nextpkt;
    177  1.1  thorpej 	} while (m0 != m);
    178  1.1  thorpej 	prev->m_nextpkt = m->m_nextpkt;
    179  1.1  thorpej 	if (prev == m)
    180  1.1  thorpej 		qtail(q) = NULL;
    181  1.1  thorpej 	else if (qtail(q) == m)
    182  1.1  thorpej 		qtail(q) = prev;
    183  1.1  thorpej 	qlen(q)--;
    184  1.1  thorpej }
    185  1.1  thorpej 
    186  1.1  thorpej static __inline void
    187  1.1  thorpej _flushq(class_queue_t *q)
    188  1.1  thorpej {
    189  1.1  thorpej 	struct mbuf *m;
    190  1.1  thorpej 
    191  1.1  thorpej 	while ((m = _getq(q)) != NULL)
    192  1.1  thorpej 		m_freem(m);
    193  1.1  thorpej }
    194  1.1  thorpej 
    195  1.1  thorpej #endif /* __GNUC__ && !ALTQ_DEBUG */
    196  1.1  thorpej 
    197  1.1  thorpej #endif /* _KERNEL */
    198  1.1  thorpej 
    199  1.1  thorpej #ifdef __cplusplus
    200  1.1  thorpej }
    201  1.1  thorpej #endif
    202  1.1  thorpej 
    203  1.1  thorpej #endif /* _ALTQ_ALTQ_CLASSQ_H_ */
    204