if: Add power of 2 mask based CPUID to subqueue mapping
[dragonfly.git] / sys / net / ifq_var.h
CommitLineData
4d723e5a
JS
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.
4d723e5a 30 */
78195a76 31
1bd40720
MD
32#ifndef _NET_IFQ_VAR_H_
33#define _NET_IFQ_VAR_H_
4d723e5a 34
03d6a592
MD
35#ifndef _KERNEL
36
37#error "This file should not be included by userland programs."
38
39#else
40
41#ifndef _SYS_SYSTM_H_
1bd40720
MD
42#include <sys/systm.h>
43#endif
44#ifndef _SYS_THREAD2_H_
4986965b 45#include <sys/thread2.h>
1bd40720
MD
46#endif
47#ifndef _SYS_SERIALIZE_H_
48#include <sys/serialize.h>
49#endif
50#ifndef _SYS_MBUF_H_
51#include <sys/mbuf.h>
52#endif
53#ifndef _NET_IF_VAR_H_
54#include <net/if_var.h>
55#endif
56#ifndef _NET_ALTQ_IF_ALTQ_H_
57#include <net/altq/if_altq.h>
58#endif
59
f0a26983
SZ
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"));
64
1bd40720 65struct ifaltq;
8a248085
SZ
66struct ifaltq_subque;
67
68typedef void (*ifsq_watchdog_t)(struct ifaltq_subque *);
69
70struct ifsubq_watchdog {
71 struct callout wd_callout;
72 int wd_timer;
73 struct ifaltq_subque *wd_subq;
74 ifsq_watchdog_t wd_watchdog;
75};
4986965b 76
9db4b353 77/*
058d1cd0 78 * Support for "classic" ALTQ interfaces.
9db4b353 79 */
f0a26983 80int ifsq_classic_enqueue(struct ifaltq_subque *, struct mbuf *,
058d1cd0 81 struct altq_pktattr *);
f0a26983
SZ
82struct mbuf *ifsq_classic_dequeue(struct ifaltq_subque *, struct mbuf *,
83 int);
84int ifsq_classic_request(struct ifaltq_subque *, int, void *);
9db4b353
SZ
85void ifq_set_classic(struct ifaltq *);
86
0faec0d1 87void ifq_set_maxlen(struct ifaltq *, int);
2cc2f639
SZ
88void ifq_set_methods(struct ifaltq *, altq_mapsubq_t,
89 ifsq_enqueue_t, ifsq_dequeue_t, ifsq_request_t);
90int ifq_mapsubq_default(struct ifaltq *, int);
c3fb75dd 91int ifq_mapsubq_mask(struct ifaltq *, int);
f0a26983
SZ
92
93void ifsq_devstart(struct ifaltq_subque *ifsq);
94void ifsq_devstart_sched(struct ifaltq_subque *ifsq);
058d1cd0 95
8a248085
SZ
96void ifsq_watchdog_init(struct ifsubq_watchdog *,
97 struct ifaltq_subque *, ifsq_watchdog_t);
98void ifsq_watchdog_start(struct ifsubq_watchdog *);
99void ifsq_watchdog_stop(struct ifsubq_watchdog *);
100
058d1cd0
SZ
101/*
102 * Dispatch a packet to an interface.
103 */
9db4b353 104int ifq_dispatch(struct ifnet *, struct mbuf *,
058d1cd0 105 struct altq_pktattr *);
9db4b353 106
4d723e5a 107#ifdef ALTQ
058d1cd0 108
4d723e5a
JS
109static __inline int
110ifq_is_enabled(struct ifaltq *_ifq)
111{
112 return(_ifq->altq_flags & ALTQF_ENABLED);
113}
114
115static __inline int
116ifq_is_attached(struct ifaltq *_ifq)
117{
118 return(_ifq->altq_disc != NULL);
119}
058d1cd0
SZ
120
121#else /* !ALTQ */
122
4d723e5a
JS
123static __inline int
124ifq_is_enabled(struct ifaltq *_ifq)
125{
126 return(0);
127}
128
129static __inline int
130ifq_is_attached(struct ifaltq *_ifq)
131{
132 return(0);
133}
4d723e5a 134
058d1cd0
SZ
135#endif /* ALTQ */
136
4d723e5a
JS
137static __inline int
138ifq_is_ready(struct ifaltq *_ifq)
139{
140 return(_ifq->altq_flags & ALTQF_READY);
141}
142
143static __inline void
144ifq_set_ready(struct ifaltq *_ifq)
145{
146 _ifq->altq_flags |= ALTQF_READY;
147}
148
78195a76 149/*
058d1cd0 150 * ALTQ lock must be held
78195a76 151 */
4d723e5a 152static __inline int
f0a26983
SZ
153ifsq_enqueue_locked(struct ifaltq_subque *_ifsq, struct mbuf *_m,
154 struct altq_pktattr *_pa)
9db4b353
SZ
155{
156#ifdef ALTQ
f0a26983
SZ
157 if (!ifq_is_enabled(_ifsq->ifsq_altq))
158 return ifsq_classic_enqueue(_ifsq, _m, _pa);
9db4b353
SZ
159 else
160#endif
f0a26983 161 return _ifsq->ifsq_enqueue(_ifsq, _m, _pa);
9db4b353
SZ
162}
163
164static __inline int
f0a26983
SZ
165ifsq_enqueue(struct ifaltq_subque *_ifsq, struct mbuf *_m,
166 struct altq_pktattr *_pa)
4d723e5a 167{
9db4b353
SZ
168 int _error;
169
f0a26983
SZ
170 ALTQ_SQ_LOCK(_ifsq);
171 _error = ifsq_enqueue_locked(_ifsq, _m, _pa);
172 ALTQ_SQ_UNLOCK(_ifsq);
9db4b353 173 return _error;
4d723e5a
JS
174}
175
176static __inline struct mbuf *
f0a26983 177ifsq_dequeue(struct ifaltq_subque *_ifsq, struct mbuf *_mpolled)
4d723e5a 178{
9db4b353
SZ
179 struct mbuf *_m;
180
f0a26983
SZ
181 ALTQ_SQ_LOCK(_ifsq);
182 if (_ifsq->ifsq_prepended != NULL) {
183 _m = _ifsq->ifsq_prepended;
184 _ifsq->ifsq_prepended = NULL;
185 KKASSERT(_ifsq->ifq_len > 0);
186 _ifsq->ifq_len--;
187 ALTQ_SQ_UNLOCK(_ifsq);
9db4b353
SZ
188 return _m;
189 }
190
4d723e5a 191#ifdef ALTQ
f0a26983
SZ
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);
9db4b353 196 else
4d723e5a 197#endif
f0a26983
SZ
198 _m = _ifsq->ifsq_dequeue(_ifsq, _mpolled, ALTDQ_REMOVE);
199 ALTQ_SQ_UNLOCK(_ifsq);
9db4b353 200 return _m;
4d723e5a
JS
201}
202
78195a76 203/*
058d1cd0 204 * ALTQ lock must be held
78195a76 205 */
4d723e5a 206static __inline struct mbuf *
f0a26983 207ifsq_poll_locked(struct ifaltq_subque *_ifsq)
4d723e5a 208{
f0a26983
SZ
209 if (_ifsq->ifsq_prepended != NULL)
210 return _ifsq->ifsq_prepended;
9db4b353 211
4d723e5a 212#ifdef ALTQ
f0a26983
SZ
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);
9db4b353 217 else
4d723e5a 218#endif
f0a26983 219 return _ifsq->ifsq_dequeue(_ifsq, NULL, ALTDQ_POLL);
9db4b353
SZ
220}
221
222static __inline struct mbuf *
f0a26983 223ifsq_poll(struct ifaltq_subque *_ifsq)
9db4b353
SZ
224{
225 struct mbuf *_m;
226
f0a26983
SZ
227 ALTQ_SQ_LOCK(_ifsq);
228 _m = ifsq_poll_locked(_ifsq);
229 ALTQ_SQ_UNLOCK(_ifsq);
9db4b353 230 return _m;
4d723e5a
JS
231}
232
78195a76 233/*
058d1cd0 234 * ALTQ lock must be held
78195a76 235 */
4d723e5a 236static __inline void
f0a26983 237ifsq_purge_locked(struct ifaltq_subque *_ifsq)
4d723e5a 238{
f0a26983
SZ
239 if (_ifsq->ifsq_prepended != NULL) {
240 m_freem(_ifsq->ifsq_prepended);
241 _ifsq->ifsq_prepended = NULL;
242 KKASSERT(_ifsq->ifq_len > 0);
243 _ifsq->ifq_len--;
9db4b353
SZ
244 }
245
246#ifdef ALTQ
f0a26983
SZ
247 if (!ifq_is_enabled(_ifsq->ifsq_altq))
248 ifsq_classic_request(_ifsq, ALTRQ_PURGE, NULL);
91148f10 249 else
9db4b353 250#endif
f0a26983 251 _ifsq->ifsq_request(_ifsq, ALTRQ_PURGE, NULL);
9db4b353
SZ
252}
253
254static __inline void
f0a26983
SZ
255ifsq_purge(struct ifaltq_subque *_ifsq)
256{
257 ALTQ_SQ_LOCK(_ifsq);
258 ifsq_purge_locked(_ifsq);
259 ALTQ_SQ_UNLOCK(_ifsq);
260}
261
262static __inline void
263ifq_lock_all(struct ifaltq *_ifq)
264{
265 int _q;
266
267 for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
268 ALTQ_SQ_LOCK(&_ifq->altq_subq[_q]);
269}
270
271static __inline void
272ifq_unlock_all(struct ifaltq *_ifq)
9db4b353 273{
f0a26983
SZ
274 int _q;
275
276 for (_q = _ifq->altq_subq_cnt - 1; _q >= 0; --_q)
277 ALTQ_SQ_UNLOCK(&_ifq->altq_subq[_q]);
4d723e5a
JS
278}
279
78195a76 280/*
058d1cd0 281 * ALTQ lock must be held
78195a76 282 */
4d723e5a 283static __inline void
9275f515
SZ
284ifq_purge_all_locked(struct ifaltq *_ifq)
285{
f0a26983
SZ
286 int _q;
287
288 for (_q = 0; _q < _ifq->altq_subq_cnt; ++_q)
289 ifsq_purge_locked(&_ifq->altq_subq[_q]);
9275f515
SZ
290}
291
292static __inline void
293ifq_purge_all(struct ifaltq *_ifq)
294{
f0a26983 295 ifq_lock_all(_ifq);
9275f515 296 ifq_purge_all_locked(_ifq);
f0a26983 297 ifq_unlock_all(_ifq);
9275f515
SZ
298}
299
9275f515 300static __inline void
4d723e5a 301ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
f0a26983 302 struct altq_pktattr *_pa)
4d723e5a 303{
9db4b353 304#ifdef ALTQ
9db4b353
SZ
305 if (ifq_is_enabled(_ifq)) {
306 _pa->pattr_af = _af;
307 _pa->pattr_hdr = mtod(_m, caddr_t);
f0a26983
SZ
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];
313
314 ALTQ_SQ_LOCK(_ifsq);
315 if (ifq_is_enabled(_ifq) &&
316 (_ifq->altq_flags & ALTQF_CLASSIFY))
317 _ifq->altq_classify(_ifq, _m, _pa);
318 ALTQ_SQ_UNLOCK(_ifsq);
319 }
9db4b353 320 }
9db4b353
SZ
321#endif
322}
323
324static __inline void
f0a26983 325ifsq_prepend(struct ifaltq_subque *_ifsq, struct mbuf *_m)
9db4b353 326{
f0a26983
SZ
327 ALTQ_SQ_LOCK(_ifsq);
328 KASSERT(_ifsq->ifsq_prepended == NULL, ("pending prepended mbuf"));
329 _ifsq->ifsq_prepended = _m;
330 _ifsq->ifq_len++;
331 ALTQ_SQ_UNLOCK(_ifsq);
4d723e5a
JS
332}
333
058d1cd0
SZ
334/*
335 * Interface TX serializer must be held
336 */
9ed293e0 337static __inline void
f0a26983 338ifsq_set_oactive(struct ifaltq_subque *_ifsq)
9ed293e0 339{
f0a26983 340 _ifsq->ifsq_hw_oactive = 1;
9ed293e0
SZ
341}
342
058d1cd0
SZ
343/*
344 * Interface TX serializer must be held
345 */
9ed293e0 346static __inline void
f0a26983 347ifsq_clr_oactive(struct ifaltq_subque *_ifsq)
9ed293e0 348{
f0a26983 349 _ifsq->ifsq_hw_oactive = 0;
9ed293e0
SZ
350}
351
058d1cd0
SZ
352/*
353 * Interface TX serializer must be held
354 */
9ed293e0 355static __inline int
f0a26983 356ifsq_is_oactive(const struct ifaltq_subque *_ifsq)
9ed293e0 357{
f0a26983 358 return _ifsq->ifsq_hw_oactive;
9ed293e0
SZ
359}
360
78195a76 361/*
058d1cd0 362 * Hand a packet to an interface.
78195a76 363 *
058d1cd0
SZ
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.
78195a76 367 */
4d723e5a
JS
368static __inline int
369ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
370{
f0a26983 371 struct ifaltq_subque *_ifsq;
4986965b 372 int _error;
f0a26983
SZ
373 int _qid = ALTQ_SUBQ_INDEX_DEFAULT; /* XXX default subqueue */
374
375 _ifsq = &_ifp->if_snd.altq_subq[_qid];
4d723e5a 376
3c4cd924 377 ASSERT_IFNET_SERIALIZED_TX(_ifp, _ifsq);
f0a26983 378 _error = ifsq_enqueue(_ifsq, _m, _pa);
4d723e5a
JS
379 if (_error == 0) {
380 _ifp->if_obytes += _m->m_pkthdr.len;
381 if (_m->m_flags & M_MCAST)
382 _ifp->if_omcasts++;
f0a26983
SZ
383 if (!ifsq_is_oactive(_ifsq))
384 (*_ifp->if_start)(_ifp, _ifsq);
4d723e5a 385 }
4d723e5a
JS
386 return(_error);
387}
388
389static __inline int
f0a26983 390ifsq_is_empty(const struct ifaltq_subque *_ifsq)
4d723e5a 391{
f0a26983 392 return(_ifsq->ifq_len == 0);
4d723e5a
JS
393}
394
058d1cd0
SZ
395/*
396 * ALTQ lock must be held
397 */
9db4b353 398static __inline int
f0a26983 399ifsq_data_ready(struct ifaltq_subque *_ifsq)
9db4b353
SZ
400{
401#ifdef ALTQ
f0a26983
SZ
402 if (_ifsq->ifsq_altq->altq_tbr != NULL)
403 return (ifsq_poll_locked(_ifsq) != NULL);
9db4b353
SZ
404 else
405#endif
f0a26983 406 return !ifsq_is_empty(_ifsq);
9db4b353 407}
b2f93efe 408
acf18b65
SZ
409/*
410 * ALTQ lock must be held
411 */
412static __inline int
f0a26983 413ifsq_is_started(const struct ifaltq_subque *_ifsq)
acf18b65 414{
f0a26983 415 return _ifsq->ifsq_started;
acf18b65
SZ
416}
417
418/*
419 * ALTQ lock must be held
420 */
421static __inline void
f0a26983 422ifsq_set_started(struct ifaltq_subque *_ifsq)
acf18b65 423{
f0a26983 424 _ifsq->ifsq_started = 1;
acf18b65
SZ
425}
426
427/*
428 * ALTQ lock must be held
429 */
430static __inline void
f0a26983 431ifsq_clr_started(struct ifaltq_subque *_ifsq)
acf18b65 432{
f0a26983 433 _ifsq->ifsq_started = 0;
acf18b65
SZ
434}
435
f0a26983
SZ
436static __inline struct ifsubq_stage *
437ifsq_get_stage(struct ifaltq_subque *_ifsq, int _cpuid)
928e2027 438{
f0a26983 439 return &_ifsq->ifsq_stage[_cpuid];
928e2027
SZ
440}
441
dfd3b18b 442static __inline int
f0a26983 443ifsq_get_cpuid(const struct ifaltq_subque *_ifsq)
dfd3b18b 444{
f0a26983 445 return _ifsq->ifsq_cpuid;
dfd3b18b
SZ
446}
447
448static __inline void
f0a26983 449ifsq_set_cpuid(struct ifaltq_subque *_ifsq, int _cpuid)
dfd3b18b 450{
f0a26983
SZ
451 KASSERT(_cpuid >= 0 && _cpuid < ncpus,
452 ("invalid ifsq_cpuid %d", _cpuid));
453 _ifsq->ifsq_cpuid = _cpuid;
dfd3b18b
SZ
454}
455
42fdf81e 456static __inline struct lwkt_msg *
f0a26983
SZ
457ifsq_get_ifstart_lmsg(struct ifaltq_subque *_ifsq, int _cpuid)
458{
459 return &_ifsq->ifsq_ifstart_nmsg[_cpuid].lmsg;
460}
461
462static __inline int
463ifsq_get_index(const struct ifaltq_subque *_ifsq)
464{
465 return _ifsq->ifsq_index;
466}
467
468static __inline void
469ifsq_set_priv(struct ifaltq_subque *_ifsq, void *_priv)
470{
471 _ifsq->ifsq_hw_priv = _priv;
472}
473
474static __inline void *
475ifsq_get_priv(const struct ifaltq_subque *_ifsq)
476{
477 return _ifsq->ifsq_hw_priv;
478}
479
480static __inline struct ifnet *
481ifsq_get_ifp(const struct ifaltq_subque *_ifsq)
482{
483 return _ifsq->ifsq_ifp;
484}
485
486static __inline struct ifaltq_subque *
487ifq_get_subq_default(const struct ifaltq *_ifq)
488{
489 return &_ifq->altq_subq[ALTQ_SUBQ_INDEX_DEFAULT];
490}
491
492static __inline struct ifaltq_subque *
493ifq_get_subq(const struct ifaltq *_ifq, int _idx)
494{
495 KASSERT(_idx >= 0 && _idx < _ifq->altq_subq_cnt,
496 ("invalid qid %d", _idx));
497 return &_ifq->altq_subq[_idx];
498}
499
2cc2f639
SZ
500static __inline struct ifaltq_subque *
501ifq_map_subq(struct ifaltq *_ifq, int _cpuid)
502{
503 int _idx = _ifq->altq_mapsubq(_ifq, _cpuid);
504 return ifq_get_subq(_ifq, _idx);
505}
506
9469a4a2
SZ
507static __inline void
508ifq_set_subq_cnt(struct ifaltq *_ifq, int _cnt)
509{
510 _ifq->altq_subq_cnt = _cnt;
511}
512
c3fb75dd
SZ
513static __inline void
514ifq_set_subq_mask(struct ifaltq *_ifq, uint32_t _mask)
515{
516 KASSERT(((_mask - 1) & _mask) == 0, ("invalid mask %08x", _mask));
517 _ifq->altq_subq_mask = _mask;
518}
519
f0a26983
SZ
520/* COMPAT */
521static __inline int
522ifq_is_oactive(const struct ifaltq *_ifq)
523{
524 return ifsq_is_oactive(ifq_get_subq_default(_ifq));
525}
526
527/* COMPAT */
528static __inline void
529ifq_set_oactive(struct ifaltq *_ifq)
530{
531 ifsq_set_oactive(ifq_get_subq_default(_ifq));
532}
533
534/* COMPAT */
535static __inline void
536ifq_clr_oactive(struct ifaltq *_ifq)
537{
538 ifsq_clr_oactive(ifq_get_subq_default(_ifq));
539}
540
541/* COMPAT */
542static __inline int
543ifq_is_empty(struct ifaltq *_ifq)
544{
545 return ifsq_is_empty(ifq_get_subq_default(_ifq));
546}
547
548/* COMPAT */
549static __inline void
550ifq_purge(struct ifaltq *_ifq)
551{
552 ifsq_purge(ifq_get_subq_default(_ifq));
553}
554
555/* COMPAT */
556static __inline struct mbuf *
557ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
558{
559 return ifsq_dequeue(ifq_get_subq_default(_ifq), _mpolled);
560}
561
562/* COMPAT */
563static __inline void
564ifq_prepend(struct ifaltq *_ifq, struct mbuf *_m)
565{
566 ifsq_prepend(ifq_get_subq_default(_ifq), _m);
567}
568
569/* COMPAT */
570static __inline void
571ifq_set_cpuid(struct ifaltq *_ifq, int _cpuid)
42fdf81e 572{
f0a26983
SZ
573 KASSERT(_ifq->altq_subq_cnt == 1,
574 ("invalid subqueue count %d", _ifq->altq_subq_cnt));
575 ifsq_set_cpuid(ifq_get_subq_default(_ifq), _cpuid);
42fdf81e
SZ
576}
577
03d6a592
MD
578#endif /* _KERNEL */
579#endif /* _NET_IFQ_VAR_H_ */