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