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 int ifq_dispatch(struct ifnet *, struct mbuf *,
79 struct altq_pktattr *);
83 ifq_is_enabled(struct ifaltq *_ifq)
85 return(_ifq->altq_flags & ALTQF_ENABLED);
89 ifq_is_attached(struct ifaltq *_ifq)
91 return(_ifq->altq_disc != NULL);
95 ifq_is_enabled(struct ifaltq *_ifq)
101 ifq_is_attached(struct ifaltq *_ifq)
108 * WARNING: Should only be called in an MPSAFE manner.
111 ifq_is_ready(struct ifaltq *_ifq)
113 return(_ifq->altq_flags & ALTQF_READY);
117 * WARNING: Should only be called in an MPSAFE manner.
120 ifq_set_ready(struct ifaltq *_ifq)
122 _ifq->altq_flags |= ALTQF_READY;
126 * WARNING: Should only be called in an MPSAFE manner.
129 ifq_enqueue_locked(struct ifaltq *_ifq, struct mbuf *_m,
130 struct altq_pktattr *_pa)
133 if (!ifq_is_enabled(_ifq))
134 return ifq_classic_enqueue(_ifq, _m, _pa);
137 return _ifq->altq_enqueue(_ifq, _m, _pa);
141 ifq_enqueue(struct ifaltq *_ifq, struct mbuf *_m, struct altq_pktattr *_pa)
146 _error = ifq_enqueue_locked(_ifq, _m, _pa);
152 * WARNING: Should only be called in an MPSAFE manner.
154 static __inline struct mbuf *
155 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
160 if (_ifq->altq_prepended != NULL) {
161 _m = _ifq->altq_prepended;
162 _ifq->altq_prepended = NULL;
163 KKASSERT(_ifq->ifq_len > 0);
170 if (_ifq->altq_tbr != NULL)
171 _m = tbr_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
172 else if (!ifq_is_enabled(_ifq))
173 _m = ifq_classic_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
176 _m = _ifq->altq_dequeue(_ifq, _mpolled, ALTDQ_REMOVE);
182 * WARNING: Should only be called in an MPSAFE manner.
184 static __inline struct mbuf *
185 ifq_poll_locked(struct ifaltq *_ifq)
187 if (_ifq->altq_prepended != NULL)
188 return _ifq->altq_prepended;
191 if (_ifq->altq_tbr != NULL)
192 return tbr_dequeue(_ifq, NULL, ALTDQ_POLL);
193 else if (!ifq_is_enabled(_ifq))
194 return ifq_classic_dequeue(_ifq, NULL, ALTDQ_POLL);
197 return _ifq->altq_dequeue(_ifq, NULL, ALTDQ_POLL);
200 static __inline struct mbuf *
201 ifq_poll(struct ifaltq *_ifq)
206 _m = ifq_poll_locked(_ifq);
212 * WARNING: Should only be called in an MPSAFE manner.
215 ifq_purge_locked(struct ifaltq *_ifq)
217 if (_ifq->altq_prepended != NULL) {
218 m_freem(_ifq->altq_prepended);
219 _ifq->altq_prepended = NULL;
220 KKASSERT(_ifq->ifq_len > 0);
225 if (!ifq_is_enabled(_ifq))
226 ifq_classic_request(_ifq, ALTRQ_PURGE, NULL);
229 _ifq->altq_request(_ifq, ALTRQ_PURGE, NULL);
233 ifq_purge(struct ifaltq *_ifq)
236 ifq_purge_locked(_ifq);
241 * WARNING: Should only be called in an MPSAFE manner.
244 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
245 struct altq_pktattr *_pa)
249 if (ifq_is_enabled(_ifq)) {
251 _pa->pattr_hdr = mtod(_m, caddr_t);
252 if (_ifq->altq_flags & ALTQF_CLASSIFY)
253 _ifq->altq_classify(_ifq, _m, _pa);
260 ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
263 KASSERT(_ifq->altq_prepended == NULL, ("pending prepended mbuf"));
264 _ifq->altq_prepended = _m;
270 * Hand a packet to an interface.
272 * For subsystems protected by the MP lock, access to the queue is protected
273 * by a critical section.
275 * For MPSAFE subsystems and drivers, access to the queue is protected by
276 * the ifnet serializer.
279 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
283 ASSERT_IFNET_SERIALIZED_TX(_ifp);
284 _error = ifq_enqueue(&_ifp->if_snd, _m, _pa);
286 _ifp->if_obytes += _m->m_pkthdr.len;
287 if (_m->m_flags & M_MCAST)
289 if ((_ifp->if_flags & IFF_OACTIVE) == 0)
290 (*_ifp->if_start)(_ifp);
296 ifq_is_empty(struct ifaltq *_ifq)
298 return(_ifq->ifq_len == 0);
302 ifq_set_maxlen(struct ifaltq *_ifq, int _len)
304 _ifq->ifq_maxlen = _len;
308 ifq_data_ready(struct ifaltq *_ifq)
311 if (_ifq->altq_tbr != NULL)
312 return (ifq_poll_locked(_ifq) != NULL);
315 return !ifq_is_empty(_ifq);
319 #endif /* _NET_IFQ_VAR_H_ */