/dev/random was almost always returning 0 bytes. This was due to several
[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.6 2005/11/28 17:13:45 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 #include <sys/thread2.h>
45
46 #ifdef ALTQ
47 static __inline int
48 ifq_is_enabled(struct ifaltq *_ifq)
49 {
50         return(_ifq->altq_flags & ALTQF_ENABLED);
51 }
52
53 static __inline int
54 ifq_is_attached(struct ifaltq *_ifq)
55 {
56         return(_ifq->altq_disc != NULL);
57 }
58 #else
59 static __inline int
60 ifq_is_enabled(struct ifaltq *_ifq)
61 {
62         return(0);
63 }
64
65 static __inline int
66 ifq_is_attached(struct ifaltq *_ifq)
67 {
68         return(0);
69 }
70 #endif
71
72 /*
73  * WARNING: Should only be called in an MPSAFE manner.
74  */
75 static __inline int
76 ifq_is_ready(struct ifaltq *_ifq)
77 {
78         return(_ifq->altq_flags & ALTQF_READY);
79 }
80
81 /*
82  * WARNING: Should only be called in an MPSAFE manner.
83  */
84 static __inline void
85 ifq_set_ready(struct ifaltq *_ifq)
86 {
87         _ifq->altq_flags |= ALTQF_READY;
88 }
89
90 /*
91  * WARNING: Should only be called in an MPSAFE manner.
92  */
93 static __inline int
94 ifq_enqueue(struct ifaltq *_ifq, struct mbuf *_m, struct altq_pktattr *_pa)
95 {
96         return((*_ifq->altq_enqueue)(_ifq, _m, _pa));
97 }
98
99 /*
100  * WARNING: Should only be called in an MPSAFE manner.
101  */
102 static __inline struct mbuf *
103 ifq_dequeue(struct ifaltq *_ifq, struct mbuf *_mpolled)
104 {
105 #ifdef ALTQ
106         if (_ifq->altq_tbr != NULL)
107                 return(tbr_dequeue(_ifq, _mpolled, ALTDQ_REMOVE));
108 #endif
109         return((*_ifq->altq_dequeue)(_ifq, _mpolled, ALTDQ_REMOVE));
110 }
111
112 /*
113  * WARNING: Should only be called in an MPSAFE manner.
114  */
115 static __inline struct mbuf *
116 ifq_poll(struct ifaltq *_ifq)
117 {
118 #ifdef ALTQ
119         if (_ifq->altq_tbr != NULL)
120                 return(tbr_dequeue(_ifq, NULL, ALTDQ_POLL));
121 #endif
122         return((*_ifq->altq_dequeue)(_ifq, NULL, ALTDQ_POLL));
123 }
124
125 /*
126  * WARNING: Should only be called in an MPSAFE manner.
127  */
128 static __inline void
129 ifq_purge(struct ifaltq *_ifq)
130 {
131         (*_ifq->altq_request)(_ifq, ALTRQ_PURGE, NULL);
132 }
133
134 /*
135  * WARNING: Should only be called in an MPSAFE manner.
136  */
137 static __inline void
138 ifq_classify(struct ifaltq *_ifq, struct mbuf *_m, uint8_t _af,
139              struct altq_pktattr *_pa)
140 {
141         if (!ifq_is_enabled(_ifq))
142                 return;
143         _pa->pattr_af = _af;
144         _pa->pattr_hdr = mtod(_m, caddr_t);
145         if (_ifq->altq_flags & ALTQF_CLASSIFY)
146                 (*_ifq->altq_classify)(_ifq, _m, _pa);
147 }
148
149 /*
150  * Hand a packet to an interface. 
151  *
152  * For subsystems protected by the MP lock, access to the queue is protected
153  * by a critical section.
154  *
155  * For MPSAFE subsystems and drivers, access to the queue is protected by
156  * the ifnet serializer.
157  */
158 static __inline int
159 ifq_handoff(struct ifnet *_ifp, struct mbuf *_m, struct altq_pktattr *_pa)
160 {
161         int _error;
162
163         ASSERT_SERIALIZED(_ifp->if_serializer);
164         _error = ifq_enqueue(&_ifp->if_snd, _m, _pa);
165         if (_error == 0) {
166                 _ifp->if_obytes += _m->m_pkthdr.len;
167                 if (_m->m_flags & M_MCAST)
168                         _ifp->if_omcasts++;
169                 if ((_ifp->if_flags & IFF_OACTIVE) == 0)
170                         (*_ifp->if_start)(_ifp);
171         }
172         return(_error);
173 }
174
175 static __inline int
176 ifq_is_empty(struct ifaltq *_ifq)
177 {
178         return(_ifq->ifq_len == 0);
179 }
180
181 static __inline void
182 ifq_set_maxlen(struct ifaltq *_ifq, int _len)
183 {
184         _ifq->ifq_maxlen = _len;
185 }
186
187 void    ifq_set_classic(struct ifaltq *);
188
189 #endif