xref: /dragonfly/sys/net/ifq_var.h (revision 7bc7e232)
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.9 2007/08/21 19:21:54 corecode 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 #ifndef _KERNEL
45 
46 #error "This file should not be included by userland programs."
47 
48 #else
49 
50 #ifndef _SYS_SYSTM_H_
51 #include <sys/systm.h>
52 #endif
53 #ifndef _SYS_THREAD2_H_
54 #include <sys/thread2.h>
55 #endif
56 #ifndef _SYS_SERIALIZE_H_
57 #include <sys/serialize.h>
58 #endif
59 #ifndef _SYS_MBUF_H_
60 #include <sys/mbuf.h>
61 #endif
62 #ifndef _NET_IF_VAR_H_
63 #include <net/if_var.h>
64 #endif
65 #ifndef _NET_ALTQ_IF_ALTQ_H_
66 #include <net/altq/if_altq.h>
67 #endif
68 
69 struct ifaltq;
70 
71 #ifdef ALTQ
72 static __inline int
73 ifq_is_enabled(struct ifaltq *_ifq)
74 {
75 	return(_ifq->altq_flags & ALTQF_ENABLED);
76 }
77 
78 static __inline int
79 ifq_is_attached(struct ifaltq *_ifq)
80 {
81 	return(_ifq->altq_disc != NULL);
82 }
83 #else
84 static __inline int
85 ifq_is_enabled(struct ifaltq *_ifq)
86 {
87 	return(0);
88 }
89 
90 static __inline int
91 ifq_is_attached(struct ifaltq *_ifq)
92 {
93 	return(0);
94 }
95 #endif
96 
97 /*
98  * WARNING: Should only be called in an MPSAFE manner.
99  */
100 static __inline int
101 ifq_is_ready(struct ifaltq *_ifq)
102 {
103 	return(_ifq->altq_flags & ALTQF_READY);
104 }
105 
106 /*
107  * WARNING: Should only be called in an MPSAFE manner.
108  */
109 static __inline void
110 ifq_set_ready(struct ifaltq *_ifq)
111 {
112 	_ifq->altq_flags |= ALTQF_READY;
113 }
114 
115 /*
116  * WARNING: Should only be called in an MPSAFE manner.
117  */
118 static __inline int
119 ifq_enqueue(struct ifaltq *_ifq, struct mbuf *_m, struct altq_pktattr *_pa)
120 {
121 	return((*_ifq->altq_enqueue)(_ifq, _m, _pa));
122 }
123 
124 /*
125  * WARNING: Should only be called in an MPSAFE manner.
126  */
127 static __inline struct mbuf *
128 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
129 {
130 #ifdef ALTQ
131 	if (_ifq->altq_tbr != NULL)
132 		return(tbr_dequeue(_ifq, _mpolled, ALTDQ_REMOVE));
133 #endif
134 	return((*_ifq->altq_dequeue)(_ifq, _mpolled, ALTDQ_REMOVE));
135 }
136 
137 /*
138  * WARNING: Should only be called in an MPSAFE manner.
139  */
140 static __inline struct mbuf *
141 ifq_poll(struct ifaltq *_ifq)
142 {
143 #ifdef ALTQ
144 	if (_ifq->altq_tbr != NULL)
145 		return(tbr_dequeue(_ifq, NULL, ALTDQ_POLL));
146 #endif
147 	return((*_ifq->altq_dequeue)(_ifq, NULL, ALTDQ_POLL));
148 }
149 
150 /*
151  * WARNING: Should only be called in an MPSAFE manner.
152  */
153 static __inline void
154 ifq_purge(struct ifaltq *_ifq)
155 {
156 	if (ifq_is_enabled(_ifq))
157 		(*_ifq->altq_request)(_ifq, ALTRQ_PURGE, NULL);
158 	else
159 		IF_DRAIN(_ifq);
160 }
161 
162 /*
163  * WARNING: Should only be called in an MPSAFE manner.
164  */
165 static __inline void
166 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
167 	     struct altq_pktattr *_pa)
168 {
169 	if (!ifq_is_enabled(_ifq))
170 		return;
171 	_pa->pattr_af = _af;
172 	_pa->pattr_hdr = mtod(_m, caddr_t);
173 	if (_ifq->altq_flags & ALTQF_CLASSIFY)
174 		(*_ifq->altq_classify)(_ifq, _m, _pa);
175 }
176 
177 /*
178  * Hand a packet to an interface.
179  *
180  * For subsystems protected by the MP lock, access to the queue is protected
181  * by a critical section.
182  *
183  * For MPSAFE subsystems and drivers, access to the queue is protected by
184  * the ifnet serializer.
185  */
186 static __inline int
187 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
188 {
189 	int _error;
190 
191 	ASSERT_SERIALIZED(_ifp->if_serializer);
192 	_error = ifq_enqueue(&_ifp->if_snd, _m, _pa);
193 	if (_error == 0) {
194 		_ifp->if_obytes += _m->m_pkthdr.len;
195 		if (_m->m_flags & M_MCAST)
196 			_ifp->if_omcasts++;
197 		if ((_ifp->if_flags & IFF_OACTIVE) == 0)
198 			(*_ifp->if_start)(_ifp);
199 	}
200 	return(_error);
201 }
202 
203 static __inline int
204 ifq_is_empty(struct ifaltq *_ifq)
205 {
206 	return(_ifq->ifq_len == 0);
207 }
208 
209 static __inline void
210 ifq_set_maxlen(struct ifaltq *_ifq, int _len)
211 {
212 	_ifq->ifq_maxlen = _len;
213 }
214 
215 void	ifq_set_classic(struct ifaltq *);
216 
217 #endif	/* _KERNEL */
218 #endif	/* _NET_IFQ_VAR_H_ */
219