2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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.
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
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.
39 #ifndef _NET_IFQ_VAR_H_
40 #define _NET_IFQ_VAR_H_
44 #error "This file should not be included by userland programs."
49 #include <sys/systm.h>
51 #ifndef _SYS_THREAD2_H_
52 #include <sys/thread2.h>
54 #ifndef _SYS_SERIALIZE_H_
55 #include <sys/serialize.h>
60 #ifndef _NET_IF_VAR_H_
61 #include <net/if_var.h>
63 #ifndef _NET_ALTQ_IF_ALTQ_H_
64 #include <net/altq/if_altq.h>
70 * Support for non-ALTQ interfaces.
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 *);
78 void ifq_set_maxlen(struct ifaltq *, int);
79 int ifq_dispatch(struct ifnet *, struct mbuf *,
80 struct altq_pktattr *);
84 ifq_is_enabled(struct ifaltq *_ifq)
86 return(_ifq->altq_flags & ALTQF_ENABLED);
90 ifq_is_attached(struct ifaltq *_ifq)
92 return(_ifq->altq_disc != NULL);
96 ifq_is_enabled(struct ifaltq *_ifq)
102 ifq_is_attached(struct ifaltq *_ifq)
109 * WARNING: Should only be called in an MPSAFE manner.
112 ifq_is_ready(struct ifaltq *_ifq)
114 return(_ifq->altq_flags & ALTQF_READY);
118 * WARNING: Should only be called in an MPSAFE manner.
121 ifq_set_ready(struct ifaltq *_ifq)
123 _ifq->altq_flags |= ALTQF_READY;
127 * WARNING: Should only be called in an MPSAFE manner.
130 ifq_enqueue_locked(struct ifaltq *_ifq, struct mbuf *_m,
131 struct altq_pktattr *_pa)
134 if (!ifq_is_enabled(_ifq))
135 return ifq_classic_enqueue(_ifq, _m, _pa);
138 return _ifq->altq_enqueue(_ifq, _m, _pa);
142 ifq_enqueue(struct ifaltq *_ifq, struct mbuf *_m, struct altq_pktattr *_pa)
147 _error = ifq_enqueue_locked(_ifq, _m, _pa);
153 * WARNING: Should only be called in an MPSAFE manner.
155 static __inline struct mbuf *
156 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
161 if (_ifq->altq_prepended != NULL) {
162 _m = _ifq->altq_prepended;
163 _ifq->altq_prepended = NULL;
164 KKASSERT(_ifq->ifq_len > 0);
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);
177 _m = _ifq->altq_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
183 * WARNING: Should only be called in an MPSAFE manner.
185 static __inline struct mbuf *
186 ifq_poll_locked(struct ifaltq *_ifq)
188 if (_ifq->altq_prepended != NULL)
189 return _ifq->altq_prepended;
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);
198 return _ifq->altq_dequeue(_ifq, NULL, ALTDQ_POLL);
201 static __inline struct mbuf *
202 ifq_poll(struct ifaltq *_ifq)
207 _m = ifq_poll_locked(_ifq);
213 * WARNING: Should only be called in an MPSAFE manner.
216 ifq_purge_locked(struct ifaltq *_ifq)
218 if (_ifq->altq_prepended != NULL) {
219 m_freem(_ifq->altq_prepended);
220 _ifq->altq_prepended = NULL;
221 KKASSERT(_ifq->ifq_len > 0);
226 if (!ifq_is_enabled(_ifq))
227 ifq_classic_request(_ifq, ALTRQ_PURGE, NULL);
230 _ifq->altq_request(_ifq, ALTRQ_PURGE, NULL);
234 ifq_purge(struct ifaltq *_ifq)
237 ifq_purge_locked(_ifq);
242 * WARNING: Should only be called in an MPSAFE manner.
245 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
246 struct altq_pktattr *_pa)
250 if (ifq_is_enabled(_ifq)) {
252 _pa->pattr_hdr = mtod(_m, caddr_t);
253 if (_ifq->altq_flags & ALTQF_CLASSIFY)
254 _ifq->altq_classify(_ifq, _m, _pa);
261 ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
264 KASSERT(_ifq->altq_prepended == NULL, ("pending prepended mbuf"));
265 _ifq->altq_prepended = _m;
271 * Hand a packet to an interface.
273 * For subsystems protected by the MP lock, access to the queue is protected
274 * by a critical section.
276 * For MPSAFE subsystems and drivers, access to the queue is protected by
277 * the ifnet serializer.
280 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
284 ASSERT_IFNET_SERIALIZED_TX(_ifp);
285 _error = ifq_enqueue(&_ifp->if_snd, _m, _pa);
287 _ifp->if_obytes += _m->m_pkthdr.len;
288 if (_m->m_flags & M_MCAST)
290 if ((_ifp->if_flags & IFF_OACTIVE) == 0)
291 (*_ifp->if_start)(_ifp);
297 ifq_is_empty(struct ifaltq *_ifq)
299 return(_ifq->ifq_len == 0);
303 ifq_data_ready(struct ifaltq *_ifq)
306 if (_ifq->altq_tbr != NULL)
307 return (ifq_poll_locked(_ifq) != NULL);
310 return !ifq_is_empty(_ifq);
314 #endif /* _NET_IFQ_VAR_H_ */