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 #ifndef _NET_IFQ_VAR_H_
33 #define _NET_IFQ_VAR_H_
36 #error "This file should not be included by userland programs."
40 #include <sys/systm.h>
42 #ifndef _SYS_THREAD2_H_
43 #include <sys/thread2.h>
45 #ifndef _SYS_SERIALIZE_H_
46 #include <sys/serialize.h>
51 #ifndef _NET_IF_VAR_H_
52 #include <net/if_var.h>
54 #ifndef _NET_ALTQ_IF_ALTQ_H_
55 #include <net/altq/if_altq.h>
58 #define ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq) \
59 KASSERT(ifsq_get_ifp((ifsq)) == (ifp) && \
60 ifsq_get_index((ifsq)) == ALTQ_SUBQ_INDEX_DEFAULT, \
61 ("not ifp's default subqueue"));
69 typedef void (*ifsq_watchdog_t)(struct ifaltq_subque *);
71 struct ifsubq_watchdog {
72 struct callout wd_callout;
74 struct ifaltq_subque *wd_subq;
75 ifsq_watchdog_t wd_watchdog;
79 * Support for "classic" ALTQ interfaces.
81 int ifsq_classic_enqueue(struct ifaltq_subque *, struct mbuf *,
82 struct altq_pktattr *);
83 struct mbuf *ifsq_classic_dequeue(struct ifaltq_subque *, struct mbuf *,
85 int ifsq_classic_request(struct ifaltq_subque *, int, void *);
86 void ifq_set_classic(struct ifaltq *);
88 void ifq_set_maxlen(struct ifaltq *, int);
89 void ifq_set_methods(struct ifaltq *, altq_mapsubq_t,
90 ifsq_enqueue_t, ifsq_dequeue_t, ifsq_request_t);
91 int ifq_mapsubq_default(struct ifaltq *, int);
92 int ifq_mapsubq_mask(struct ifaltq *, int);
94 void ifsq_devstart(struct ifaltq_subque *ifsq);
95 void ifsq_devstart_sched(struct ifaltq_subque *ifsq);
97 void ifsq_watchdog_init(struct ifsubq_watchdog *,
98 struct ifaltq_subque *, ifsq_watchdog_t);
99 void ifsq_watchdog_start(struct ifsubq_watchdog *);
100 void ifsq_watchdog_stop(struct ifsubq_watchdog *);
103 * Dispatch a packet to an interface.
105 int ifq_dispatch(struct ifnet *, struct mbuf *,
106 struct altq_pktattr *);
111 ifq_is_enabled(struct ifaltq *_ifq)
113 return(_ifq->altq_flags & ALTQF_ENABLED);
117 ifq_is_attached(struct ifaltq *_ifq)
119 return(_ifq->altq_disc != NULL);
125 ifq_is_enabled(struct ifaltq *_ifq)
131 ifq_is_attached(struct ifaltq *_ifq)
139 ifq_is_ready(struct ifaltq *_ifq)
141 return(_ifq->altq_flags & ALTQF_READY);
145 ifq_set_ready(struct ifaltq *_ifq)
147 _ifq->altq_flags |= ALTQF_READY;
151 * Subqueue lock must be held
154 ifsq_enqueue_locked(struct ifaltq_subque *_ifsq, struct mbuf *_m,
155 struct altq_pktattr *_pa)
158 if (!ifq_is_enabled(_ifsq->ifsq_altq))
159 return ifsq_classic_enqueue(_ifsq, _m, _pa);
162 return _ifsq->ifsq_enqueue(_ifsq, _m, _pa);
166 ifsq_enqueue(struct ifaltq_subque *_ifsq, struct mbuf *_m,
167 struct altq_pktattr *_pa)
172 _error = ifsq_enqueue_locked(_ifsq, _m, _pa);
173 ALTQ_SQ_UNLOCK(_ifsq);
177 static __inline struct mbuf *
178 ifsq_dequeue(struct ifaltq_subque *_ifsq, struct mbuf *_mpolled)
183 if (_ifsq->ifsq_prepended != NULL) {
184 _m = _ifsq->ifsq_prepended;
185 _ifsq->ifsq_prepended = NULL;
186 KKASSERT(_ifsq->ifq_len > 0);
188 ALTQ_SQ_UNLOCK(_ifsq);
193 if (_ifsq->ifsq_altq->altq_tbr != NULL)
194 _m = tbr_dequeue(_ifsq, _mpolled, ALTDQ_REMOVE);
195 else if (!ifq_is_enabled(_ifsq->ifsq_altq))
196 _m = ifsq_classic_dequeue(_ifsq, _mpolled, ALTDQ_REMOVE);
199 _m = _ifsq->ifsq_dequeue(_ifsq, _mpolled, ALTDQ_REMOVE);
200 ALTQ_SQ_UNLOCK(_ifsq);
205 * Subqueue lock must be held
207 static __inline struct mbuf *
208 ifsq_poll_locked(struct ifaltq_subque *_ifsq)
210 if (_ifsq->ifsq_prepended != NULL)
211 return _ifsq->ifsq_prepended;
214 if (_ifsq->ifsq_altq->altq_tbr != NULL)
215 return tbr_dequeue(_ifsq, NULL, ALTDQ_POLL);
216 else if (!ifq_is_enabled(_ifsq->ifsq_altq))
217 return ifsq_classic_dequeue(_ifsq, NULL, ALTDQ_POLL);
220 return _ifsq->ifsq_dequeue(_ifsq, NULL, ALTDQ_POLL);
223 static __inline struct mbuf *
224 ifsq_poll(struct ifaltq_subque *_ifsq)
229 _m = ifsq_poll_locked(_ifsq);
230 ALTQ_SQ_UNLOCK(_ifsq);
235 * Subqueue lock must be held
238 ifsq_purge_locked(struct ifaltq_subque *_ifsq)
240 if (_ifsq->ifsq_prepended != NULL) {
241 m_freem(_ifsq->ifsq_prepended);
242 _ifsq->ifsq_prepended = NULL;
243 KKASSERT(_ifsq->ifq_len > 0);
248 if (!ifq_is_enabled(_ifsq->ifsq_altq))
249 ifsq_classic_request(_ifsq, ALTRQ_PURGE, NULL);
252 _ifsq->ifsq_request(_ifsq, ALTRQ_PURGE, NULL);
256 ifsq_purge(struct ifaltq_subque *_ifsq)
259 ifsq_purge_locked(_ifsq);
260 ALTQ_SQ_UNLOCK(_ifsq);
264 ifq_lock_all(struct ifaltq *_ifq)
268 for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
269 ALTQ_SQ_LOCK(&_ifq->altq_subq[_q]);
273 ifq_unlock_all(struct ifaltq *_ifq)
277 for (_q = _ifq->altq_subq_cnt - 1; _q >= 0; --_q)
278 ALTQ_SQ_UNLOCK(&_ifq->altq_subq[_q]);
282 * All of the subqueue locks must be held
285 ifq_purge_all_locked(struct ifaltq *_ifq)
289 for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
290 ifsq_purge_locked(&_ifq->altq_subq[_q]);
294 ifq_purge_all(struct ifaltq *_ifq)
297 ifq_purge_all_locked(_ifq);
298 ifq_unlock_all(_ifq);
302 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
303 struct altq_pktattr *_pa)
306 if (ifq_is_enabled(_ifq)) {
308 _pa->pattr_hdr = mtod(_m, caddr_t);
309 if (ifq_is_enabled(_ifq) &&
310 (_ifq->altq_flags & ALTQF_CLASSIFY)) {
311 /* XXX default subqueue */
312 struct ifaltq_subque *_ifsq =
313 &_ifq->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
316 if (ifq_is_enabled(_ifq) &&
317 (_ifq->altq_flags & ALTQF_CLASSIFY))
318 _ifq->altq_classify(_ifq, _m, _pa);
319 ALTQ_SQ_UNLOCK(_ifsq);
326 ifsq_prepend(struct ifaltq_subque *_ifsq, struct mbuf *_m)
329 KASSERT(_ifsq->ifsq_prepended == NULL, ("pending prepended mbuf"));
330 _ifsq->ifsq_prepended = _m;
332 ALTQ_SQ_UNLOCK(_ifsq);
336 * Subqueue hardware serializer must be held
339 ifsq_set_oactive(struct ifaltq_subque *_ifsq)
341 _ifsq->ifsq_hw_oactive = 1;
345 * Subqueue hardware serializer must be held
348 ifsq_clr_oactive(struct ifaltq_subque *_ifsq)
350 _ifsq->ifsq_hw_oactive = 0;
354 * Subqueue hardware serializer must be held
357 ifsq_is_oactive(const struct ifaltq_subque *_ifsq)
359 return _ifsq->ifsq_hw_oactive;
363 * Hand a packet to the interface's default subqueue.
365 * The default subqueue hardware serializer must be held. If the
366 * subqueue hardware serializer is not held yet, ifq_dispatch()
367 * should be used to get better performance.
370 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
372 struct ifaltq_subque *_ifsq;
374 int _qid = ALTQ_SUBQ_INDEX_DEFAULT; /* XXX default subqueue */
376 _ifsq = &_ifp->if_snd.altq_subq[_qid];
378 ASSERT_ALTQ_SQ_SERIALIZED_HW(_ifsq);
379 _error = ifsq_enqueue(_ifsq, _m, _pa);
381 IFNET_STAT_INC(_ifp, obytes, _m->m_pkthdr.len);
382 if (_m->m_flags & M_MCAST)
383 IFNET_STAT_INC(_ifp, omcasts, 1);
384 if (!ifsq_is_oactive(_ifsq))
385 (*_ifp->if_start)(_ifp, _ifsq);
391 ifsq_is_empty(const struct ifaltq_subque *_ifsq)
393 return(_ifsq->ifq_len == 0);
397 * Subqueue lock must be held
400 ifsq_data_ready(struct ifaltq_subque *_ifsq)
403 if (_ifsq->ifsq_altq->altq_tbr != NULL)
404 return (ifsq_poll_locked(_ifsq) != NULL);
407 return !ifsq_is_empty(_ifsq);
411 * Subqueue lock must be held
414 ifsq_is_started(const struct ifaltq_subque *_ifsq)
416 return _ifsq->ifsq_started;
420 * Subqueue lock must be held
423 ifsq_set_started(struct ifaltq_subque *_ifsq)
425 _ifsq->ifsq_started = 1;
429 * Subqueue lock must be held
432 ifsq_clr_started(struct ifaltq_subque *_ifsq)
434 _ifsq->ifsq_started = 0;
437 static __inline struct ifsubq_stage *
438 ifsq_get_stage(struct ifaltq_subque *_ifsq, int _cpuid)
440 return &_ifsq->ifsq_stage[_cpuid];
444 ifsq_get_cpuid(const struct ifaltq_subque *_ifsq)
446 return _ifsq->ifsq_cpuid;
450 ifsq_set_cpuid(struct ifaltq_subque *_ifsq, int _cpuid)
452 KASSERT(_cpuid >= 0 && _cpuid < ncpus,
453 ("invalid ifsq_cpuid %d", _cpuid));
454 _ifsq->ifsq_cpuid = _cpuid;
457 static __inline struct lwkt_msg *
458 ifsq_get_ifstart_lmsg(struct ifaltq_subque *_ifsq, int _cpuid)
460 return &_ifsq->ifsq_ifstart_nmsg[_cpuid].lmsg;
464 ifsq_get_index(const struct ifaltq_subque *_ifsq)
466 return _ifsq->ifsq_index;
470 ifsq_set_priv(struct ifaltq_subque *_ifsq, void *_priv)
472 _ifsq->ifsq_hw_priv = _priv;
475 static __inline void *
476 ifsq_get_priv(const struct ifaltq_subque *_ifsq)
478 return _ifsq->ifsq_hw_priv;
481 static __inline struct ifnet *
482 ifsq_get_ifp(const struct ifaltq_subque *_ifsq)
484 return _ifsq->ifsq_ifp;
488 ifsq_set_hw_serialize(struct ifaltq_subque *_ifsq,
489 struct lwkt_serialize *_hwslz)
491 KASSERT(_hwslz != NULL, ("NULL hw serialize"));
492 KASSERT(_ifsq->ifsq_hw_serialize == NULL,
493 ("hw serialize has been setup"));
494 _ifsq->ifsq_hw_serialize = _hwslz;
498 ifsq_serialize_hw(struct ifaltq_subque *_ifsq)
500 lwkt_serialize_enter(_ifsq->ifsq_hw_serialize);
504 ifsq_deserialize_hw(struct ifaltq_subque *_ifsq)
506 lwkt_serialize_exit(_ifsq->ifsq_hw_serialize);
510 ifsq_tryserialize_hw(struct ifaltq_subque *_ifsq)
512 return lwkt_serialize_try(_ifsq->ifsq_hw_serialize);
515 static __inline struct ifaltq_subque *
516 ifq_get_subq_default(const struct ifaltq *_ifq)
518 return &_ifq->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
521 static __inline struct ifaltq_subque *
522 ifq_get_subq(const struct ifaltq *_ifq, int _idx)
524 KASSERT(_idx >= 0 && _idx < _ifq->altq_subq_cnt,
525 ("invalid qid %d", _idx));
526 return &_ifq->altq_subq[_idx];
529 static __inline struct ifaltq_subque *
530 ifq_map_subq(struct ifaltq *_ifq, int _cpuid)
532 int _idx = _ifq->altq_mapsubq(_ifq, _cpuid);
533 return ifq_get_subq(_ifq, _idx);
537 ifq_set_subq_cnt(struct ifaltq *_ifq, int _cnt)
539 _ifq->altq_subq_cnt = _cnt;
543 ifq_set_subq_mask(struct ifaltq *_ifq, uint32_t _mask)
545 KASSERT(((_mask + 1) & _mask) == 0, ("invalid mask %08x", _mask));
546 _ifq->altq_subq_mask = _mask;
551 ifq_is_oactive(const struct ifaltq *_ifq)
553 return ifsq_is_oactive(ifq_get_subq_default(_ifq));
558 ifq_set_oactive(struct ifaltq *_ifq)
560 ifsq_set_oactive(ifq_get_subq_default(_ifq));
565 ifq_clr_oactive(struct ifaltq *_ifq)
567 ifsq_clr_oactive(ifq_get_subq_default(_ifq));
572 ifq_is_empty(struct ifaltq *_ifq)
574 return ifsq_is_empty(ifq_get_subq_default(_ifq));
579 ifq_purge(struct ifaltq *_ifq)
581 ifsq_purge(ifq_get_subq_default(_ifq));
585 static __inline struct mbuf *
586 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
588 return ifsq_dequeue(ifq_get_subq_default(_ifq), _mpolled);
593 ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
595 ifsq_prepend(ifq_get_subq_default(_ifq), _m);
600 ifq_set_cpuid(struct ifaltq *_ifq, int _cpuid)
602 KASSERT(_ifq->altq_subq_cnt == 1,
603 ("invalid subqueue count %d", _ifq->altq_subq_cnt));
604 ifsq_set_cpuid(ifq_get_subq_default(_ifq), _cpuid);
607 #endif /* _NET_IFQ_VAR_H_ */