]> git.saurik.com Git - apple/xnu.git/blob - iokit/Families/IONetworking/IOPacketQueue.cpp
dd5a223bde8a44269ed065ca7a1226d9d2edd965
[apple/xnu.git] / iokit / Families / IONetworking / IOPacketQueue.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
24 *
25 * IOPacketQueue.cpp - Implements a FIFO mbuf packet queue.
26 *
27 * HISTORY
28 * 9-Dec-1998 Joe Liu (jliu) created.
29 *
30 */
31
32 #include <IOKit/assert.h>
33 #include <IOKit/IOLib.h> // IOLog
34 #include <IOKit/network/IOPacketQueue.h>
35 #include "IOMbufQueue.h"
36
37 #define super OSObject
38 OSDefineMetaClassAndStructors( IOPacketQueue, OSObject )
39 OSMetaClassDefineReservedUnused( IOPacketQueue, 0);
40 OSMetaClassDefineReservedUnused( IOPacketQueue, 1);
41 OSMetaClassDefineReservedUnused( IOPacketQueue, 2);
42 OSMetaClassDefineReservedUnused( IOPacketQueue, 3);
43 OSMetaClassDefineReservedUnused( IOPacketQueue, 4);
44 OSMetaClassDefineReservedUnused( IOPacketQueue, 5);
45 OSMetaClassDefineReservedUnused( IOPacketQueue, 6);
46 OSMetaClassDefineReservedUnused( IOPacketQueue, 7);
47 OSMetaClassDefineReservedUnused( IOPacketQueue, 8);
48 OSMetaClassDefineReservedUnused( IOPacketQueue, 9);
49 OSMetaClassDefineReservedUnused( IOPacketQueue, 10);
50 OSMetaClassDefineReservedUnused( IOPacketQueue, 11);
51 OSMetaClassDefineReservedUnused( IOPacketQueue, 12);
52 OSMetaClassDefineReservedUnused( IOPacketQueue, 13);
53 OSMetaClassDefineReservedUnused( IOPacketQueue, 14);
54 OSMetaClassDefineReservedUnused( IOPacketQueue, 15);
55
56
57 //---------------------------------------------------------------------------
58 // Lock macros
59
60 #define LOCK IOSimpleLockLock(_lock)
61 #define UNLOCK IOSimpleLockUnlock(_lock)
62
63 //---------------------------------------------------------------------------
64 // Initialize an IOPacketQueue object.
65
66 bool IOPacketQueue::initWithCapacity(UInt32 capacity)
67 {
68 if (super::init() == false)
69 {
70 IOLog("IOPacketQueue: super::init() failed\n");
71 return false;
72 }
73
74 _queue = IONew(IOMbufQueue, 1);
75 if (_queue == 0)
76 return false;
77
78 IOMbufQueueInit(_queue, capacity);
79
80 if ((_lock = IOSimpleLockAlloc()) == 0)
81 return false;
82
83 IOSimpleLockInit(_lock);
84
85 return true;
86 }
87
88 //---------------------------------------------------------------------------
89 // Factory method that will construct and initialize an IOPacketQueue object.
90
91 IOPacketQueue * IOPacketQueue::withCapacity(UInt32 capacity)
92 {
93 IOPacketQueue * queue = new IOPacketQueue;
94
95 if (queue && !queue->initWithCapacity(capacity))
96 {
97 queue->release();
98 queue = 0;
99 }
100 return queue;
101 }
102
103 //---------------------------------------------------------------------------
104 // Frees the IOPacketQueue instance.
105
106 void IOPacketQueue::free()
107 {
108 if (_lock)
109 {
110 IOSimpleLockFree(_lock);
111 _lock = 0;
112 }
113
114 if (_queue)
115 {
116 flush();
117 IODelete(_queue, IOMbufQueue, 1);
118 _queue = 0;
119 }
120
121 super::free();
122 }
123
124 //---------------------------------------------------------------------------
125 // Get the current size of the queue.
126
127 UInt32 IOPacketQueue::getSize() const
128 {
129 return IOMbufQueueGetSize(_queue);
130 }
131
132 //---------------------------------------------------------------------------
133 // Change the capacity of the queue.
134
135 bool IOPacketQueue::setCapacity(UInt32 capacity)
136 {
137 IOMbufQueueSetCapacity(_queue, capacity);
138 return true;
139 }
140
141 //---------------------------------------------------------------------------
142 // Get the capacity of the queue.
143
144 UInt32 IOPacketQueue::getCapacity() const
145 {
146 return IOMbufQueueGetCapacity(_queue);
147 }
148
149 //---------------------------------------------------------------------------
150 // Peek at the head of the queue without dequeueing the packet.
151
152 const struct mbuf * IOPacketQueue::peek() const
153 {
154 return IOMbufQueuePeek(_queue);
155 }
156
157 //---------------------------------------------------------------------------
158 // Add a packet chain to the head of the queue.
159
160 void IOPacketQueue::prepend(struct mbuf * m)
161 {
162 IOMbufQueuePrepend(_queue, m);
163 }
164
165 void IOPacketQueue::prepend(IOPacketQueue * queue)
166 {
167 IOMbufQueuePrepend(_queue, queue->_queue);
168 }
169
170 //---------------------------------------------------------------------------
171 // Add a packet chain to the tail of the queue.
172
173 bool IOPacketQueue::enqueue(struct mbuf * m)
174 {
175 return IOMbufQueueEnqueue(_queue, m);
176 }
177
178 bool IOPacketQueue::enqueue(IOPacketQueue * queue)
179 {
180 return IOMbufQueueEnqueue(_queue, queue->_queue);
181 }
182
183 UInt32 IOPacketQueue::enqueueWithDrop(struct mbuf * m)
184 {
185 return IOMbufQueueEnqueue(_queue, m) ? 0 : IOMbufFree(m);
186 }
187
188 //---------------------------------------------------------------------------
189 // Remove a single packet from the head of the queue.
190
191 struct mbuf * IOPacketQueue::dequeue()
192 {
193 return IOMbufQueueDequeue(_queue);
194 }
195
196 //---------------------------------------------------------------------------
197 // Remove all packets from the queue and return the chain of packet(s).
198
199 struct mbuf * IOPacketQueue::dequeueAll()
200 {
201 return IOMbufQueueDequeueAll(_queue);
202 }
203
204 //---------------------------------------------------------------------------
205 // Remove all packets from the queue and put them back to the free mbuf
206 // pool. The size of the queue will be cleared to zero.
207
208 UInt32 IOPacketQueue::flush()
209 {
210 return IOMbufFree(IOMbufQueueDequeueAll(_queue));
211 }
212
213 //---------------------------------------------------------------------------
214 // Locked forms of prepend/enqueue/dequeue/dequeueAll methods.
215 // A spinlock will enforce mutually exclusive queue access.
216
217 void IOPacketQueue::lockPrepend(struct mbuf * m)
218 {
219 LOCK;
220 IOMbufQueuePrepend(_queue, m);
221 UNLOCK;
222 }
223
224 bool IOPacketQueue::lockEnqueue(struct mbuf * m)
225 {
226 bool ok;
227 LOCK;
228 ok = IOMbufQueueEnqueue(_queue, m);
229 UNLOCK;
230 return ok;
231 }
232
233 UInt32 IOPacketQueue::lockEnqueueWithDrop(struct mbuf * m)
234 {
235 bool ok;
236 LOCK;
237 ok = IOMbufQueueEnqueue(_queue, m);
238 UNLOCK;
239 return ok ? 0 : IOMbufFree(m);
240 }
241
242 struct mbuf * IOPacketQueue::lockDequeue()
243 {
244 struct mbuf * m;
245 LOCK;
246 m = IOMbufQueueDequeue(_queue);
247 UNLOCK;
248 return m;
249 }
250
251 struct mbuf * IOPacketQueue::lockDequeueAll()
252 {
253 struct mbuf * m;
254 LOCK;
255 m = IOMbufQueueDequeueAll(_queue);
256 UNLOCK;
257 return m;
258 }
259
260 UInt32 IOPacketQueue::lockFlush()
261 {
262 struct mbuf * m;
263 LOCK;
264 m = IOMbufQueueDequeueAll(_queue);
265 UNLOCK;
266 return IOMbufFree(m);
267 }