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