I'm growing tired of having to add #include lines for header files that
[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  * $DragonFly: src/sys/net/ifq_var.h,v 1.7 2006/05/20 02:42:08 dillon Exp $
32  */
33 /*
34  * NOTE ON MPSAFE access.  Routines which manipulate the packet queue must
35  * be called within a critical section to interlock subsystems based on
36  * the MP lock, and must be holding the interface serializer to interlock
37  * MPSAFE subsystems.  Once all subsystems are made MPSAFE, the critical
38  * section will no longer be required.
39  */
40
41 #ifndef _NET_IFQ_VAR_H_
42 #define _NET_IFQ_VAR_H_
43
44 #if defined(_KERNEL) && !defined(_SYS_SYSTM_H_)
45 #include <sys/systm.h>
46 #endif
47 #ifndef _SYS_THREAD2_H_
48 #include <sys/thread2.h>
49 #endif
50 #ifndef _SYS_SERIALIZE_H_
51 #include <sys/serialize.h>
52 #endif
53 #ifndef _SYS_MBUF_H_
54 #include <sys/mbuf.h>
55 #endif
56 #ifndef _NET_IF_VAR_H_
57 #include <net/if_var.h>
58 #endif
59 #ifndef _NET_ALTQ_IF_ALTQ_H_
60 #include <net/altq/if_altq.h>
61 #endif
62
63 struct ifaltq;
64
65 #ifdef ALTQ
66 static __inline int
67 ifq_is_enabled(struct ifaltq *_ifq)
68 {
69         return(_ifq->altq_flags & ALTQF_ENABLED);
70 }
71
72 static __inline int
73 ifq_is_attached(struct ifaltq *_ifq)
74 {
75         return(_ifq->altq_disc != NULL);
76 }
77 #else
78 static __inline int
79 ifq_is_enabled(struct ifaltq *_ifq)
80 {
81         return(0);
82 }
83
84 static __inline int
85 ifq_is_attached(struct ifaltq *_ifq)
86 {
87         return(0);
88 }
89 #endif
90
91 /*
92  * WARNING: Should only be called in an MPSAFE manner.
93  */
94 static __inline int
95 ifq_is_ready(struct ifaltq *_ifq)
96 {
97         return(_ifq->altq_flags & ALTQF_READY);
98 }
99
100 /*
101  * WARNING: Should only be called in an MPSAFE manner.
102  */
103 static __inline void
104 ifq_set_ready(struct ifaltq *_ifq)
105 {
106         _ifq->altq_flags |= ALTQF_READY;
107 }
108
109 /*
110  * WARNING: Should only be called in an MPSAFE manner.
111  */
112 static __inline int
113 ifq_enqueue(struct ifaltq *_ifq, struct mbuf *_m, struct altq_pktattr *_pa)
114 {
115         return((*_ifq->altq_enqueue)(_ifq, _m, _pa));
116 }
117
118 /*
119  * WARNING: Should only be called in an MPSAFE manner.
120  */
121 static __inline struct mbuf *
122 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
123 {
124 #ifdef ALTQ
125         if (_ifq->altq_tbr != NULL)
126                 return(tbr_dequeue(_ifq, _mpolled, ALTDQ_REMOVE));
127 #endif
128         return((*_ifq->altq_dequeue)(_ifq, _mpolled, ALTDQ_REMOVE));
129 }
130
131 /*
132  * WARNING: Should only be called in an MPSAFE manner.
133  */
134 static __inline struct mbuf *
135 ifq_poll(struct ifaltq *_ifq)
136 {
137 #ifdef ALTQ
138         if (_ifq->altq_tbr != NULL)
139                 return(tbr_dequeue(_ifq, NULL, ALTDQ_POLL));
140 #endif
141         return((*_ifq->altq_dequeue)(_ifq, NULL, ALTDQ_POLL));
142 }
143
144 /*
145  * WARNING: Should only be called in an MPSAFE manner.
146  */
147 static __inline void
148 ifq_purge(struct ifaltq *_ifq)
149 {
150         (*_ifq->altq_request)(_ifq, ALTRQ_PURGE, NULL);
151 }
152
153 /*
154  * WARNING: Should only be called in an MPSAFE manner.
155  */
156 static __inline void
157 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
158              struct altq_pktattr *_pa)
159 {
160         if (!ifq_is_enabled(_ifq))
161                 return;
162         _pa->pattr_af = _af;
163         _pa->pattr_hdr = mtod(_m, caddr_t);
164         if (_ifq->altq_flags & ALTQF_CLASSIFY)
165                 (*_ifq->altq_classify)(_ifq, _m, _pa);
166 }
167
168 /*
169  * Hand a packet to an interface. 
170  *
171  * For subsystems protected by the MP lock, access to the queue is protected
172  * by a critical section.
173  *
174  * For MPSAFE subsystems and drivers, access to the queue is protected by
175  * the ifnet serializer.
176  */
177 static __inline int
178 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
179 {
180         int _error;
181
182         ASSERT_SERIALIZED(_ifp->if_serializer);
183         _error = ifq_enqueue(&_ifp->if_snd, _m, _pa);
184         if (_error == 0) {
185                 _ifp->if_obytes += _m->m_pkthdr.len;
186                 if (_m->m_flags & M_MCAST)
187                         _ifp->if_omcasts++;
188                 if ((_ifp->if_flags & IFF_OACTIVE) == 0)
189                         (*_ifp->if_start)(_ifp);
190         }
191         return(_error);
192 }
193
194 static __inline int
195 ifq_is_empty(struct ifaltq *_ifq)
196 {
197         return(_ifq->ifq_len == 0);
198 }
199
200 static __inline void
201 ifq_set_maxlen(struct ifaltq *_ifq, int _len)
202 {
203         _ifq->ifq_maxlen = _len;
204 }
205
206 void    ifq_set_classic(struct ifaltq *);
207
208 #endif