xref: /freebsd/sys/net/altq/altq_classq.h (revision 3416500aef140042c64bc149cb1ec6620483bc44)
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  * $FreeBSD$
35  */
36 /*
37  * class queue definitions extracted from rm_class.h.
38  */
39 #ifndef _ALTQ_ALTQ_CLASSQ_H_
40 #define	_ALTQ_ALTQ_CLASSQ_H_
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /*
47  * Packet Queue types: RED or DROPHEAD.
48  */
49 #define	Q_DROPHEAD	0x00
50 #define	Q_RED		0x01
51 #define	Q_RIO		0x02
52 #define	Q_DROPTAIL	0x03
53 #define	Q_CODEL		0x04
54 
55 #ifdef _KERNEL
56 
57 /*
58  * Packet Queue structures and macros to manipulate them.
59  */
60 struct _class_queue_ {
61 	struct mbuf	*tail_;	/* Tail of packet queue */
62 	int	qlen_;		/* Queue length (in number of packets) */
63 	int	qlim_;		/* Queue limit (in number of packets*) */
64 	int	qsize_;		/* Queue size (in number of bytes*) */
65 	int	qtype_;		/* Queue type */
66 };
67 
68 typedef struct _class_queue_	class_queue_t;
69 
70 #define	qtype(q)	(q)->qtype_		/* Get queue type */
71 #define	qlimit(q)	(q)->qlim_		/* Max packets to be queued */
72 #define	qlen(q)		(q)->qlen_		/* Current queue length. */
73 #define	qsize(q)	(q)->qsize_		/* Current queue size. */
74 #define	qtail(q)	(q)->tail_		/* Tail of the queue */
75 #define	qhead(q)	((q)->tail_ ? (q)->tail_->m_nextpkt : NULL)
76 
77 #define	qempty(q)	((q)->qlen_ == 0)	/* Is the queue empty?? */
78 #define	q_is_codel(q)	((q)->qtype_ == Q_CODEL) /* Is the queue a codel queue */
79 #define	q_is_red(q)	((q)->qtype_ == Q_RED)	/* Is the queue a red queue */
80 #define	q_is_rio(q)	((q)->qtype_ == Q_RIO)	/* Is the queue a rio queue */
81 #define	q_is_red_or_rio(q)	((q)->qtype_ == Q_RED || (q)->qtype_ == Q_RIO)
82 
83 #if !defined(__GNUC__) || defined(ALTQ_DEBUG)
84 
85 extern void		_addq(class_queue_t *, struct mbuf *);
86 extern struct mbuf	*_getq(class_queue_t *);
87 extern struct mbuf	*_getq_tail(class_queue_t *);
88 extern struct mbuf	*_getq_random(class_queue_t *);
89 extern void		_removeq(class_queue_t *, struct mbuf *);
90 extern void		_flushq(class_queue_t *);
91 
92 #else /* __GNUC__ && !ALTQ_DEBUG */
93 /*
94  * inlined versions
95  */
96 static __inline void
97 _addq(class_queue_t *q, struct mbuf *m)
98 {
99         struct mbuf *m0;
100 
101 	if ((m0 = qtail(q)) != NULL)
102 		m->m_nextpkt = m0->m_nextpkt;
103 	else
104 		m0 = m;
105 	m0->m_nextpkt = m;
106 	qtail(q) = m;
107 	qlen(q)++;
108 	qsize(q) += m_pktlen(m);
109 }
110 
111 static __inline struct mbuf *
112 _getq(class_queue_t *q)
113 {
114 	struct mbuf  *m, *m0;
115 
116 	if ((m = qtail(q)) == NULL)
117 		return (NULL);
118 	if ((m0 = m->m_nextpkt) != m)
119 		m->m_nextpkt = m0->m_nextpkt;
120 	else
121 		qtail(q) = NULL;
122 	qlen(q)--;
123 	qsize(q) -= m_pktlen(m0);
124 	m0->m_nextpkt = NULL;
125 	return (m0);
126 }
127 
128 /* drop a packet at the tail of the queue */
129 static __inline struct mbuf *
130 _getq_tail(class_queue_t *q)
131 {
132 	struct mbuf *m, *m0, *prev;
133 
134 	if ((m = m0 = qtail(q)) == NULL)
135 		return NULL;
136 	do {
137 		prev = m0;
138 		m0 = m0->m_nextpkt;
139 	} while (m0 != m);
140 	prev->m_nextpkt = m->m_nextpkt;
141 	if (prev == m)
142 		qtail(q) = NULL;
143 	else
144 		qtail(q) = prev;
145 	qlen(q)--;
146 	m->m_nextpkt = NULL;
147 	return (m);
148 }
149 
150 /* randomly select a packet in the queue */
151 static __inline struct mbuf *
152 _getq_random(class_queue_t *q)
153 {
154 	struct mbuf *m;
155 	int i, n;
156 
157 	if ((m = qtail(q)) == NULL)
158 		return NULL;
159 	if (m->m_nextpkt == m)
160 		qtail(q) = NULL;
161 	else {
162 		struct mbuf *prev = NULL;
163 
164 		n = random() % qlen(q) + 1;
165 		for (i = 0; i < n; i++) {
166 			prev = m;
167 			m = m->m_nextpkt;
168 		}
169 		prev->m_nextpkt = m->m_nextpkt;
170 		if (m == qtail(q))
171 			qtail(q) = prev;
172 	}
173 	qlen(q)--;
174 	m->m_nextpkt = NULL;
175 	return (m);
176 }
177 
178 static __inline void
179 _removeq(class_queue_t *q, struct mbuf *m)
180 {
181 	struct mbuf *m0, *prev;
182 
183 	m0 = qtail(q);
184 	do {
185 		prev = m0;
186 		m0 = m0->m_nextpkt;
187 	} while (m0 != m);
188 	prev->m_nextpkt = m->m_nextpkt;
189 	if (prev == m)
190 		qtail(q) = NULL;
191 	else if (qtail(q) == m)
192 		qtail(q) = prev;
193 	qlen(q)--;
194 }
195 
196 static __inline void
197 _flushq(class_queue_t *q)
198 {
199 	struct mbuf *m;
200 
201 	while ((m = _getq(q)) != NULL)
202 		m_freem(m);
203 }
204 
205 #endif /* __GNUC__ && !ALTQ_DEBUG */
206 
207 #endif /* _KERNEL */
208 
209 #ifdef __cplusplus
210 }
211 #endif
212 
213 #endif /* _ALTQ_ALTQ_CLASSQ_H_ */
214