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