ifq: Update comment in net/ifq_var.h
[dragonfly.git] / sys / net / ifq_var.h
1 /*-
2  * Copyright (c) 2005 The DragonFly Project.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  * 3. Neither the name of The DragonFly Project nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific, prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef _NET_IFQ_VAR_H_
33 #define _NET_IFQ_VAR_H_
34
35 #ifndef _KERNEL
36
37 #error "This file should not be included by userland programs."
38
39 #else
40
41 #ifndef _SYS_SYSTM_H_
42 #include <sys/systm.h>
43 #endif
44 #ifndef _SYS_THREAD2_H_
45 #include <sys/thread2.h>
46 #endif
47 #ifndef _SYS_SERIALIZE_H_
48 #include <sys/serialize.h>
49 #endif
50 #ifndef _SYS_MBUF_H_
51 #include <sys/mbuf.h>
52 #endif
53 #ifndef _NET_IF_VAR_H_
54 #include <net/if_var.h>
55 #endif
56 #ifndef _NET_ALTQ_IF_ALTQ_H_
57 #include <net/altq/if_altq.h>
58 #endif
59
60 struct ifaltq;
61
62 /*
63  * Support for "classic" ALTQ interfaces.
64  */
65 int             ifq_classic_enqueue(struct ifaltq *, struct mbuf *,
66                     struct altq_pktattr *);
67 struct mbuf     *ifq_classic_dequeue(struct ifaltq *, struct mbuf *, int);
68 int             ifq_classic_request(struct ifaltq *, int, void *);
69 void            ifq_set_classic(struct ifaltq *);
70
71 void            ifq_set_maxlen(struct ifaltq *, int);
72
73 /*
74  * Dispatch a packet to an interface.
75  */
76 int             ifq_dispatch(struct ifnet *, struct mbuf *,
77                     struct altq_pktattr *);
78
79 #ifdef ALTQ
80
81 static __inline int
82 ifq_is_enabled(struct ifaltq *_ifq)
83 {
84         return(_ifq->altq_flags & ALTQF_ENABLED);
85 }
86
87 static __inline int
88 ifq_is_attached(struct ifaltq *_ifq)
89 {
90         return(_ifq->altq_disc != NULL);
91 }
92
93 #else   /* !ALTQ */
94
95 static __inline int
96 ifq_is_enabled(struct ifaltq *_ifq)
97 {
98         return(0);
99 }
100
101 static __inline int
102 ifq_is_attached(struct ifaltq *_ifq)
103 {
104         return(0);
105 }
106
107 #endif  /* ALTQ */
108
109 static __inline int
110 ifq_is_ready(struct ifaltq *_ifq)
111 {
112         return(_ifq->altq_flags & ALTQF_READY);
113 }
114
115 static __inline void
116 ifq_set_ready(struct ifaltq *_ifq)
117 {
118         _ifq->altq_flags |= ALTQF_READY;
119 }
120
121 /*
122  * ALTQ lock must be held
123  */
124 static __inline int
125 ifq_enqueue_locked(struct ifaltq *_ifq, struct mbuf *_m,
126                    struct altq_pktattr *_pa)
127 {
128 #ifdef ALTQ
129         if (!ifq_is_enabled(_ifq))
130                 return ifq_classic_enqueue(_ifq, _m, _pa);
131         else
132 #endif
133         return _ifq->altq_enqueue(_ifq, _m, _pa);
134 }
135
136 static __inline int
137 ifq_enqueue(struct ifaltq *_ifq, struct mbuf *_m, struct altq_pktattr *_pa)
138 {
139         int _error;
140
141         ALTQ_LOCK(_ifq);
142         _error = ifq_enqueue_locked(_ifq, _m, _pa);
143         ALTQ_UNLOCK(_ifq);
144         return _error;
145 }
146
147 static __inline struct mbuf *
148 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
149 {
150         struct mbuf *_m;
151
152         ALTQ_LOCK(_ifq);
153         if (_ifq->altq_prepended != NULL) {
154                 _m = _ifq->altq_prepended;
155                 _ifq->altq_prepended = NULL;
156                 KKASSERT(_ifq->ifq_len > 0);
157                 _ifq->ifq_len--;
158                 ALTQ_UNLOCK(_ifq);
159                 return _m;
160         }
161
162 #ifdef ALTQ
163         if (_ifq->altq_tbr != NULL)
164                 _m = tbr_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
165         else if (!ifq_is_enabled(_ifq))
166                 _m = ifq_classic_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
167         else
168 #endif
169         _m = _ifq->altq_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
170         ALTQ_UNLOCK(_ifq);
171         return _m;
172 }
173
174 /*
175  * ALTQ lock must be held
176  */
177 static __inline struct mbuf *
178 ifq_poll_locked(struct ifaltq *_ifq)
179 {
180         if (_ifq->altq_prepended != NULL)
181                 return _ifq->altq_prepended;
182
183 #ifdef ALTQ
184         if (_ifq->altq_tbr != NULL)
185                 return tbr_dequeue(_ifq, NULL, ALTDQ_POLL);
186         else if (!ifq_is_enabled(_ifq))
187                 return ifq_classic_dequeue(_ifq, NULL, ALTDQ_POLL);
188         else
189 #endif
190         return _ifq->altq_dequeue(_ifq, NULL, ALTDQ_POLL);
191 }
192
193 static __inline struct mbuf *
194 ifq_poll(struct ifaltq *_ifq)
195 {
196         struct mbuf *_m;
197
198         ALTQ_LOCK(_ifq);
199         _m = ifq_poll_locked(_ifq);
200         ALTQ_UNLOCK(_ifq);
201         return _m;
202 }
203
204 /*
205  * ALTQ lock must be held
206  */
207 static __inline void
208 ifq_purge_locked(struct ifaltq *_ifq)
209 {
210         if (_ifq->altq_prepended != NULL) {
211                 m_freem(_ifq->altq_prepended);
212                 _ifq->altq_prepended = NULL;
213                 KKASSERT(_ifq->ifq_len > 0);
214                 _ifq->ifq_len--;
215         }
216
217 #ifdef ALTQ
218         if (!ifq_is_enabled(_ifq))
219                 ifq_classic_request(_ifq, ALTRQ_PURGE, NULL);
220         else
221 #endif
222         _ifq->altq_request(_ifq, ALTRQ_PURGE, NULL);
223 }
224
225 static __inline void
226 ifq_purge(struct ifaltq *_ifq)
227 {
228         ALTQ_LOCK(_ifq);
229         ifq_purge_locked(_ifq);
230         ALTQ_UNLOCK(_ifq);
231 }
232
233 /*
234  * ALTQ lock must be held
235  */
236 static __inline void
237 ifq_purge_all_locked(struct ifaltq *_ifq)
238 {
239         /* XXX temporary */
240         ifq_purge_locked(_ifq);
241 }
242
243 static __inline void
244 ifq_purge_all(struct ifaltq *_ifq)
245 {
246         ALTQ_LOCK(_ifq);
247         ifq_purge_all_locked(_ifq);
248         ALTQ_UNLOCK(_ifq);
249 }
250
251 static __inline void
252 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
253              struct altq_pktattr *_pa)
254 {
255 #ifdef ALTQ
256         ALTQ_LOCK(_ifq);
257         if (ifq_is_enabled(_ifq)) {
258                 _pa->pattr_af = _af;
259                 _pa->pattr_hdr = mtod(_m, caddr_t);
260                 if (_ifq->altq_flags & ALTQF_CLASSIFY)
261                         _ifq->altq_classify(_ifq, _m, _pa);
262         }
263         ALTQ_UNLOCK(_ifq);
264 #endif
265 }
266
267 static __inline void
268 ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
269 {
270         ALTQ_LOCK(_ifq);
271         KASSERT(_ifq->altq_prepended == NULL, ("pending prepended mbuf"));
272         _ifq->altq_prepended = _m;
273         _ifq->ifq_len++;
274         ALTQ_UNLOCK(_ifq);
275 }
276
277 /*
278  * Interface TX serializer must be held
279  */
280 static __inline void
281 ifq_set_oactive(struct ifaltq *_ifq)
282 {
283         _ifq->altq_hw_oactive = 1;
284 }
285
286 /*
287  * Interface TX serializer must be held
288  */
289 static __inline void
290 ifq_clr_oactive(struct ifaltq *_ifq)
291 {
292         _ifq->altq_hw_oactive = 0;
293 }
294
295 /*
296  * Interface TX serializer must be held
297  */
298 static __inline int
299 ifq_is_oactive(const struct ifaltq *_ifq)
300 {
301         return _ifq->altq_hw_oactive;
302 }
303
304 /*
305  * Hand a packet to an interface.
306  *
307  * Interface TX serializer must be held.  If the interface TX
308  * serializer is not held yet, ifq_dispatch() should be used
309  * to get better performance.
310  */
311 static __inline int
312 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
313 {
314         int _error;
315
316         ASSERT_IFNET_SERIALIZED_TX(_ifp);
317         _error = ifq_enqueue(&_ifp->if_snd, _m, _pa);
318         if (_error == 0) {
319                 _ifp->if_obytes += _m->m_pkthdr.len;
320                 if (_m->m_flags & M_MCAST)
321                         _ifp->if_omcasts++;
322                 if (!ifq_is_oactive(&_ifp->if_snd))
323                         (*_ifp->if_start)(_ifp);
324         }
325         return(_error);
326 }
327
328 static __inline int
329 ifq_is_empty(struct ifaltq *_ifq)
330 {
331         return(_ifq->ifq_len == 0);
332 }
333
334 /*
335  * ALTQ lock must be held
336  */
337 static __inline int
338 ifq_data_ready(struct ifaltq *_ifq)
339 {
340 #ifdef ALTQ
341         if (_ifq->altq_tbr != NULL)
342                 return (ifq_poll_locked(_ifq) != NULL);
343         else
344 #endif
345         return !ifq_is_empty(_ifq);
346 }
347
348 #endif  /* _KERNEL */
349 #endif  /* _NET_IFQ_VAR_H_ */