ifq: Move ifq_set_maxlen declaration to the declaration code block
[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  * NOTE ON MPSAFE access.  Routines which manipulate the packet queue must
33  * be called within a critical section to interlock subsystems based on
34  * the MP lock, and must be holding the interface serializer to interlock
35  * MPSAFE subsystems.  Once all subsystems are made MPSAFE, the critical
36  * section will no longer be required.
37  */
38
39 #ifndef _NET_IFQ_VAR_H_
40 #define _NET_IFQ_VAR_H_
41
42 #ifndef _KERNEL
43
44 #error "This file should not be included by userland programs."
45
46 #else
47
48 #ifndef _SYS_SYSTM_H_
49 #include <sys/systm.h>
50 #endif
51 #ifndef _SYS_THREAD2_H_
52 #include <sys/thread2.h>
53 #endif
54 #ifndef _SYS_SERIALIZE_H_
55 #include <sys/serialize.h>
56 #endif
57 #ifndef _SYS_MBUF_H_
58 #include <sys/mbuf.h>
59 #endif
60 #ifndef _NET_IF_VAR_H_
61 #include <net/if_var.h>
62 #endif
63 #ifndef _NET_ALTQ_IF_ALTQ_H_
64 #include <net/altq/if_altq.h>
65 #endif
66
67 struct ifaltq;
68
69 /*
70  * Support for non-ALTQ interfaces.
71  */
72 int             ifq_classic_enqueue(struct ifaltq *, struct mbuf *,
73                                     struct altq_pktattr *);
74 struct mbuf     *ifq_classic_dequeue(struct ifaltq *, struct mbuf *, int);
75 int             ifq_classic_request(struct ifaltq *, int, void *);
76 void            ifq_set_classic(struct ifaltq *);
77
78 void            ifq_set_maxlen(struct ifaltq *, int);
79 int             ifq_dispatch(struct ifnet *, struct mbuf *,
80                              struct altq_pktattr *);
81
82 #ifdef ALTQ
83 static __inline int
84 ifq_is_enabled(struct ifaltq *_ifq)
85 {
86         return(_ifq->altq_flags & ALTQF_ENABLED);
87 }
88
89 static __inline int
90 ifq_is_attached(struct ifaltq *_ifq)
91 {
92         return(_ifq->altq_disc != NULL);
93 }
94 #else
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 #endif
107
108 /*
109  * WARNING: Should only be called in an MPSAFE manner.
110  */
111 static __inline int
112 ifq_is_ready(struct ifaltq *_ifq)
113 {
114         return(_ifq->altq_flags & ALTQF_READY);
115 }
116
117 /*
118  * WARNING: Should only be called in an MPSAFE manner.
119  */
120 static __inline void
121 ifq_set_ready(struct ifaltq *_ifq)
122 {
123         _ifq->altq_flags |= ALTQF_READY;
124 }
125
126 /*
127  * WARNING: Should only be called in an MPSAFE manner.
128  */
129 static __inline int
130 ifq_enqueue_locked(struct ifaltq *_ifq, struct mbuf *_m,
131                    struct altq_pktattr *_pa)
132 {
133 #ifdef ALTQ
134         if (!ifq_is_enabled(_ifq))
135                 return ifq_classic_enqueue(_ifq, _m, _pa);
136         else
137 #endif
138         return _ifq->altq_enqueue(_ifq, _m, _pa);
139 }
140
141 static __inline int
142 ifq_enqueue(struct ifaltq *_ifq, struct mbuf *_m, struct altq_pktattr *_pa)
143 {
144         int _error;
145
146         ALTQ_LOCK(_ifq);
147         _error = ifq_enqueue_locked(_ifq, _m, _pa);
148         ALTQ_UNLOCK(_ifq);
149         return _error;
150 }
151
152 /*
153  * WARNING: Should only be called in an MPSAFE manner.
154  */
155 static __inline struct mbuf *
156 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
157 {
158         struct mbuf *_m;
159
160         ALTQ_LOCK(_ifq);
161         if (_ifq->altq_prepended != NULL) {
162                 _m = _ifq->altq_prepended;
163                 _ifq->altq_prepended = NULL;
164                 KKASSERT(_ifq->ifq_len > 0);
165                 _ifq->ifq_len--;
166                 ALTQ_UNLOCK(_ifq);
167                 return _m;
168         }
169
170 #ifdef ALTQ
171         if (_ifq->altq_tbr != NULL)
172                 _m = tbr_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
173         else if (!ifq_is_enabled(_ifq))
174                 _m = ifq_classic_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
175         else
176 #endif
177         _m = _ifq->altq_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
178         ALTQ_UNLOCK(_ifq);
179         return _m;
180 }
181
182 /*
183  * WARNING: Should only be called in an MPSAFE manner.
184  */
185 static __inline struct mbuf *
186 ifq_poll_locked(struct ifaltq *_ifq)
187 {
188         if (_ifq->altq_prepended != NULL)
189                 return _ifq->altq_prepended;
190
191 #ifdef ALTQ
192         if (_ifq->altq_tbr != NULL)
193                 return tbr_dequeue(_ifq, NULL, ALTDQ_POLL);
194         else if (!ifq_is_enabled(_ifq))
195                 return ifq_classic_dequeue(_ifq, NULL, ALTDQ_POLL);
196         else
197 #endif
198         return _ifq->altq_dequeue(_ifq, NULL, ALTDQ_POLL);
199 }
200
201 static __inline struct mbuf *
202 ifq_poll(struct ifaltq *_ifq)
203 {
204         struct mbuf *_m;
205
206         ALTQ_LOCK(_ifq);
207         _m = ifq_poll_locked(_ifq);
208         ALTQ_UNLOCK(_ifq);
209         return _m;
210 }
211
212 /*
213  * WARNING: Should only be called in an MPSAFE manner.
214  */
215 static __inline void
216 ifq_purge_locked(struct ifaltq *_ifq)
217 {
218         if (_ifq->altq_prepended != NULL) {
219                 m_freem(_ifq->altq_prepended);
220                 _ifq->altq_prepended = NULL;
221                 KKASSERT(_ifq->ifq_len > 0);
222                 _ifq->ifq_len--;
223         }
224
225 #ifdef ALTQ
226         if (!ifq_is_enabled(_ifq))
227                 ifq_classic_request(_ifq, ALTRQ_PURGE, NULL);
228         else
229 #endif
230         _ifq->altq_request(_ifq, ALTRQ_PURGE, NULL);
231 }
232
233 static __inline void
234 ifq_purge(struct ifaltq *_ifq)
235 {
236         ALTQ_LOCK(_ifq);
237         ifq_purge_locked(_ifq);
238         ALTQ_UNLOCK(_ifq);
239 }
240
241 /*
242  * WARNING: Should only be called in an MPSAFE manner.
243  */
244 static __inline void
245 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
246              struct altq_pktattr *_pa)
247 {
248 #ifdef ALTQ
249         ALTQ_LOCK(_ifq);
250         if (ifq_is_enabled(_ifq)) {
251                 _pa->pattr_af = _af;
252                 _pa->pattr_hdr = mtod(_m, caddr_t);
253                 if (_ifq->altq_flags & ALTQF_CLASSIFY)
254                         _ifq->altq_classify(_ifq, _m, _pa);
255         }
256         ALTQ_UNLOCK(_ifq);
257 #endif
258 }
259
260 static __inline void
261 ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
262 {
263         ALTQ_LOCK(_ifq);
264         KASSERT(_ifq->altq_prepended == NULL, ("pending prepended mbuf"));
265         _ifq->altq_prepended = _m;
266         _ifq->ifq_len++;
267         ALTQ_UNLOCK(_ifq);
268 }
269
270 /*
271  * Hand a packet to an interface. 
272  *
273  * For subsystems protected by the MP lock, access to the queue is protected
274  * by a critical section.
275  *
276  * For MPSAFE subsystems and drivers, access to the queue is protected by
277  * the ifnet serializer.
278  */
279 static __inline int
280 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
281 {
282         int _error;
283
284         ASSERT_IFNET_SERIALIZED_TX(_ifp);
285         _error = ifq_enqueue(&_ifp->if_snd, _m, _pa);
286         if (_error == 0) {
287                 _ifp->if_obytes += _m->m_pkthdr.len;
288                 if (_m->m_flags & M_MCAST)
289                         _ifp->if_omcasts++;
290                 if ((_ifp->if_flags & IFF_OACTIVE) == 0)
291                         (*_ifp->if_start)(_ifp);
292         }
293         return(_error);
294 }
295
296 static __inline int
297 ifq_is_empty(struct ifaltq *_ifq)
298 {
299         return(_ifq->ifq_len == 0);
300 }
301
302 static __inline int
303 ifq_data_ready(struct ifaltq *_ifq)
304 {
305 #ifdef ALTQ
306         if (_ifq->altq_tbr != NULL)
307                 return (ifq_poll_locked(_ifq) != NULL);
308         else
309 #endif
310         return !ifq_is_empty(_ifq);
311 }
312
313 #endif  /* _KERNEL */
314 #endif  /* _NET_IFQ_VAR_H_ */