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_
37 #error "This file should not be included by userland programs."
42 #include <sys/systm.h>
44 #ifndef _SYS_THREAD2_H_
45 #include <sys/thread2.h>
47 #ifndef _SYS_SERIALIZE_H_
48 #include <sys/serialize.h>
53 #ifndef _NET_IF_VAR_H_
54 #include <net/if_var.h>
56 #ifndef _NET_ALTQ_IF_ALTQ_H_
57 #include <net/altq/if_altq.h>
60 #define ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq) \
61 KASSERT(ifsq_get_ifp((ifsq)) == (ifp) && \
62 ifsq_get_index((ifsq)) == ALTQ_SUBQ_INDEX_DEFAULT, \
63 ("not ifp's default subqueue"));
68 typedef void (*ifsq_watchdog_t)(struct ifaltq_subque *);
70 struct ifsubq_watchdog {
71 struct callout wd_callout;
73 struct ifaltq_subque *wd_subq;
74 ifsq_watchdog_t wd_watchdog;
78 * Support for "classic" ALTQ interfaces.
80 int ifsq_classic_enqueue(struct ifaltq_subque *, struct mbuf *,
81 struct altq_pktattr *);
82 struct mbuf *ifsq_classic_dequeue(struct ifaltq_subque *, struct mbuf *,
84 int ifsq_classic_request(struct ifaltq_subque *, int, void *);
85 void ifq_set_classic(struct ifaltq *);
87 void ifq_set_maxlen(struct ifaltq *, int);
88 void ifq_set_methods(struct ifaltq *, altq_mapsubq_t,
89 ifsq_enqueue_t, ifsq_dequeue_t, ifsq_request_t);
90 int ifq_mapsubq_default(struct ifaltq *, int);
91 int ifq_mapsubq_mask(struct ifaltq *, int);
93 void ifsq_devstart(struct ifaltq_subque *ifsq);
94 void ifsq_devstart_sched(struct ifaltq_subque *ifsq);
96 void ifsq_watchdog_init(struct ifsubq_watchdog *,
97 struct ifaltq_subque *, ifsq_watchdog_t);
98 void ifsq_watchdog_start(struct ifsubq_watchdog *);
99 void ifsq_watchdog_stop(struct ifsubq_watchdog *);
102 * Dispatch a packet to an interface.
104 int ifq_dispatch(struct ifnet *, struct mbuf *,
105 struct altq_pktattr *);
110 ifq_is_enabled(struct ifaltq *_ifq)
112 return(_ifq->altq_flags & ALTQF_ENABLED);
116 ifq_is_attached(struct ifaltq *_ifq)
118 return(_ifq->altq_disc != NULL);
124 ifq_is_enabled(struct ifaltq *_ifq)
130 ifq_is_attached(struct ifaltq *_ifq)
138 ifq_is_ready(struct ifaltq *_ifq)
140 return(_ifq->altq_flags & ALTQF_READY);
144 ifq_set_ready(struct ifaltq *_ifq)
146 _ifq->altq_flags |= ALTQF_READY;
150 * ALTQ lock must be held
153 ifsq_enqueue_locked(struct ifaltq_subque *_ifsq, struct mbuf *_m,
154 struct altq_pktattr *_pa)
157 if (!ifq_is_enabled(_ifsq->ifsq_altq))
158 return ifsq_classic_enqueue(_ifsq, _m, _pa);
161 return _ifsq->ifsq_enqueue(_ifsq, _m, _pa);
165 ifsq_enqueue(struct ifaltq_subque *_ifsq, struct mbuf *_m,
166 struct altq_pktattr *_pa)
171 _error = ifsq_enqueue_locked(_ifsq, _m, _pa);
172 ALTQ_SQ_UNLOCK(_ifsq);
176 static __inline struct mbuf *
177 ifsq_dequeue(struct ifaltq_subque *_ifsq, struct mbuf *_mpolled)
182 if (_ifsq->ifsq_prepended != NULL) {
183 _m = _ifsq->ifsq_prepended;
184 _ifsq->ifsq_prepended = NULL;
185 KKASSERT(_ifsq->ifq_len > 0);
187 ALTQ_SQ_UNLOCK(_ifsq);
192 if (_ifsq->ifsq_altq->altq_tbr != NULL)
193 _m = tbr_dequeue(_ifsq, _mpolled, ALTDQ_REMOVE);
194 else if (!ifq_is_enabled(_ifsq->ifsq_altq))
195 _m = ifsq_classic_dequeue(_ifsq, _mpolled, ALTDQ_REMOVE);
198 _m = _ifsq->ifsq_dequeue(_ifsq, _mpolled, ALTDQ_REMOVE);
199 ALTQ_SQ_UNLOCK(_ifsq);
204 * ALTQ lock must be held
206 static __inline struct mbuf *
207 ifsq_poll_locked(struct ifaltq_subque *_ifsq)
209 if (_ifsq->ifsq_prepended != NULL)
210 return _ifsq->ifsq_prepended;
213 if (_ifsq->ifsq_altq->altq_tbr != NULL)
214 return tbr_dequeue(_ifsq, NULL, ALTDQ_POLL);
215 else if (!ifq_is_enabled(_ifsq->ifsq_altq))
216 return ifsq_classic_dequeue(_ifsq, NULL, ALTDQ_POLL);
219 return _ifsq->ifsq_dequeue(_ifsq, NULL, ALTDQ_POLL);
222 static __inline struct mbuf *
223 ifsq_poll(struct ifaltq_subque *_ifsq)
228 _m = ifsq_poll_locked(_ifsq);
229 ALTQ_SQ_UNLOCK(_ifsq);
234 * ALTQ lock must be held
237 ifsq_purge_locked(struct ifaltq_subque *_ifsq)
239 if (_ifsq->ifsq_prepended != NULL) {
240 m_freem(_ifsq->ifsq_prepended);
241 _ifsq->ifsq_prepended = NULL;
242 KKASSERT(_ifsq->ifq_len > 0);
247 if (!ifq_is_enabled(_ifsq->ifsq_altq))
248 ifsq_classic_request(_ifsq, ALTRQ_PURGE, NULL);
251 _ifsq->ifsq_request(_ifsq, ALTRQ_PURGE, NULL);
255 ifsq_purge(struct ifaltq_subque *_ifsq)
258 ifsq_purge_locked(_ifsq);
259 ALTQ_SQ_UNLOCK(_ifsq);
263 ifq_lock_all(struct ifaltq *_ifq)
267 for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
268 ALTQ_SQ_LOCK(&_ifq->altq_subq[_q]);
272 ifq_unlock_all(struct ifaltq *_ifq)
276 for (_q = _ifq->altq_subq_cnt - 1; _q >= 0; --_q)
277 ALTQ_SQ_UNLOCK(&_ifq->altq_subq[_q]);
281 * ALTQ lock must be held
284 ifq_purge_all_locked(struct ifaltq *_ifq)
288 for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
289 ifsq_purge_locked(&_ifq->altq_subq[_q]);
293 ifq_purge_all(struct ifaltq *_ifq)
296 ifq_purge_all_locked(_ifq);
297 ifq_unlock_all(_ifq);
301 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
302 struct altq_pktattr *_pa)
305 if (ifq_is_enabled(_ifq)) {
307 _pa->pattr_hdr = mtod(_m, caddr_t);
308 if (ifq_is_enabled(_ifq) &&
309 (_ifq->altq_flags & ALTQF_CLASSIFY)) {
310 /* XXX default subqueue */
311 struct ifaltq_subque *_ifsq =
312 &_ifq->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
315 if (ifq_is_enabled(_ifq) &&
316 (_ifq->altq_flags & ALTQF_CLASSIFY))
317 _ifq->altq_classify(_ifq, _m, _pa);
318 ALTQ_SQ_UNLOCK(_ifsq);
325 ifsq_prepend(struct ifaltq_subque *_ifsq, struct mbuf *_m)
328 KASSERT(_ifsq->ifsq_prepended == NULL, ("pending prepended mbuf"));
329 _ifsq->ifsq_prepended = _m;
331 ALTQ_SQ_UNLOCK(_ifsq);
335 * Interface TX serializer must be held
338 ifsq_set_oactive(struct ifaltq_subque *_ifsq)
340 _ifsq->ifsq_hw_oactive = 1;
344 * Interface TX serializer must be held
347 ifsq_clr_oactive(struct ifaltq_subque *_ifsq)
349 _ifsq->ifsq_hw_oactive = 0;
353 * Interface TX serializer must be held
356 ifsq_is_oactive(const struct ifaltq_subque *_ifsq)
358 return _ifsq->ifsq_hw_oactive;
362 * Hand a packet to an interface.
364 * Interface TX serializer must be held. If the interface TX
365 * serializer is not held yet, ifq_dispatch() should be used
366 * to get better performance.
369 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
371 struct ifaltq_subque *_ifsq;
373 int _qid = ALTQ_SUBQ_INDEX_DEFAULT; /* XXX default subqueue */
375 _ifsq = &_ifp->if_snd.altq_subq[_qid];
377 ASSERT_ALTQ_SQ_SERIALIZED_HW(_ifsq);
378 _error = ifsq_enqueue(_ifsq, _m, _pa);
380 IFNET_STAT_INC(_ifp, obytes, _m->m_pkthdr.len);
381 if (_m->m_flags & M_MCAST)
382 IFNET_STAT_INC(_ifp, omcasts, 1);
383 if (!ifsq_is_oactive(_ifsq))
384 (*_ifp->if_start)(_ifp, _ifsq);
390 ifsq_is_empty(const struct ifaltq_subque *_ifsq)
392 return(_ifsq->ifq_len == 0);
396 * ALTQ lock must be held
399 ifsq_data_ready(struct ifaltq_subque *_ifsq)
402 if (_ifsq->ifsq_altq->altq_tbr != NULL)
403 return (ifsq_poll_locked(_ifsq) != NULL);
406 return !ifsq_is_empty(_ifsq);
410 * ALTQ lock must be held
413 ifsq_is_started(const struct ifaltq_subque *_ifsq)
415 return _ifsq->ifsq_started;
419 * ALTQ lock must be held
422 ifsq_set_started(struct ifaltq_subque *_ifsq)
424 _ifsq->ifsq_started = 1;
428 * ALTQ lock must be held
431 ifsq_clr_started(struct ifaltq_subque *_ifsq)
433 _ifsq->ifsq_started = 0;
436 static __inline struct ifsubq_stage *
437 ifsq_get_stage(struct ifaltq_subque *_ifsq, int _cpuid)
439 return &_ifsq->ifsq_stage[_cpuid];
443 ifsq_get_cpuid(const struct ifaltq_subque *_ifsq)
445 return _ifsq->ifsq_cpuid;
449 ifsq_set_cpuid(struct ifaltq_subque *_ifsq, int _cpuid)
451 KASSERT(_cpuid >= 0 && _cpuid < ncpus,
452 ("invalid ifsq_cpuid %d", _cpuid));
453 _ifsq->ifsq_cpuid = _cpuid;
456 static __inline struct lwkt_msg *
457 ifsq_get_ifstart_lmsg(struct ifaltq_subque *_ifsq, int _cpuid)
459 return &_ifsq->ifsq_ifstart_nmsg[_cpuid].lmsg;
463 ifsq_get_index(const struct ifaltq_subque *_ifsq)
465 return _ifsq->ifsq_index;
469 ifsq_set_priv(struct ifaltq_subque *_ifsq, void *_priv)
471 _ifsq->ifsq_hw_priv = _priv;
474 static __inline void *
475 ifsq_get_priv(const struct ifaltq_subque *_ifsq)
477 return _ifsq->ifsq_hw_priv;
480 static __inline struct ifnet *
481 ifsq_get_ifp(const struct ifaltq_subque *_ifsq)
483 return _ifsq->ifsq_ifp;
487 ifsq_set_hw_serialize(struct ifaltq_subque *_ifsq,
488 struct lwkt_serialize *_hwslz)
490 KASSERT(_hwslz != NULL, ("NULL hw serialize"));
491 KASSERT(_ifsq->ifsq_hw_serialize == NULL,
492 ("hw serialize has been setup"));
493 _ifsq->ifsq_hw_serialize = _hwslz;
497 ifsq_serialize_hw(struct ifaltq_subque *_ifsq)
499 lwkt_serialize_enter(_ifsq->ifsq_hw_serialize);
503 ifsq_deserialize_hw(struct ifaltq_subque *_ifsq)
505 lwkt_serialize_exit(_ifsq->ifsq_hw_serialize);
509 ifsq_tryserialize_hw(struct ifaltq_subque *_ifsq)
511 return lwkt_serialize_try(_ifsq->ifsq_hw_serialize);
514 static __inline struct ifaltq_subque *
515 ifq_get_subq_default(const struct ifaltq *_ifq)
517 return &_ifq->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
520 static __inline struct ifaltq_subque *
521 ifq_get_subq(const struct ifaltq *_ifq, int _idx)
523 KASSERT(_idx >= 0 && _idx < _ifq->altq_subq_cnt,
524 ("invalid qid %d", _idx));
525 return &_ifq->altq_subq[_idx];
528 static __inline struct ifaltq_subque *
529 ifq_map_subq(struct ifaltq *_ifq, int _cpuid)
531 int _idx = _ifq->altq_mapsubq(_ifq, _cpuid);
532 return ifq_get_subq(_ifq, _idx);
536 ifq_set_subq_cnt(struct ifaltq *_ifq, int _cnt)
538 _ifq->altq_subq_cnt = _cnt;
542 ifq_set_subq_mask(struct ifaltq *_ifq, uint32_t _mask)
544 KASSERT(((_mask + 1) & _mask) == 0, ("invalid mask %08x", _mask));
545 _ifq->altq_subq_mask = _mask;
550 ifq_is_oactive(const struct ifaltq *_ifq)
552 return ifsq_is_oactive(ifq_get_subq_default(_ifq));
557 ifq_set_oactive(struct ifaltq *_ifq)
559 ifsq_set_oactive(ifq_get_subq_default(_ifq));
564 ifq_clr_oactive(struct ifaltq *_ifq)
566 ifsq_clr_oactive(ifq_get_subq_default(_ifq));
571 ifq_is_empty(struct ifaltq *_ifq)
573 return ifsq_is_empty(ifq_get_subq_default(_ifq));
578 ifq_purge(struct ifaltq *_ifq)
580 ifsq_purge(ifq_get_subq_default(_ifq));
584 static __inline struct mbuf *
585 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
587 return ifsq_dequeue(ifq_get_subq_default(_ifq), _mpolled);
592 ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
594 ifsq_prepend(ifq_get_subq_default(_ifq), _m);
599 ifq_set_cpuid(struct ifaltq *_ifq, int _cpuid)
601 KASSERT(_ifq->altq_subq_cnt == 1,
602 ("invalid subqueue count %d", _ifq->altq_subq_cnt));
603 ifsq_set_cpuid(ifq_get_subq_default(_ifq), _cpuid);
607 #endif /* _NET_IFQ_VAR_H_ */