]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/classq/classq.c
92b76007fb939a52e695532de2256f2344ccf81e
2 * Copyright (c) 2007-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1991-1997 Regents of the University of California.
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the Network Research
44 * Group at Lawrence Berkeley Laboratory.
45 * 4. Neither the name of the University nor of the Laboratory may be used
46 * to endorse or promote products derived from this software without
47 * specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 #include <sys/cdefs.h>
63 #include <sys/param.h>
65 #include <sys/errno.h>
66 #include <sys/random.h>
67 #include <sys/kernel_types.h>
68 #include <sys/sysctl.h>
71 #include <net/net_osdep.h>
72 #include <net/classq/classq.h>
74 #include <libkern/libkern.h>
76 u_int32_t classq_verbose
; /* more noise if greater than 1 */
78 SYSCTL_NODE(_net
, OID_AUTO
, classq
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "classq");
80 SYSCTL_UINT(_net_classq
, OID_AUTO
, verbose
, CTLFLAG_RW
|CTLFLAG_LOCKED
,
81 &classq_verbose
, 0, "Class queue verbosity level");
84 _qinit(class_queue_t
*q
, int type
, int lim
)
86 MBUFQ_INIT(&q
->mbufq
);
91 qstate(q
) = QS_RUNNING
;
94 /* add a packet at the tail of the queue */
96 _addq(class_queue_t
*q
, struct mbuf
*m
)
98 MBUFQ_ENQUEUE(&q
->mbufq
, m
);
100 VERIFY(qlen(q
) != 0);
101 qsize(q
) += m_length(m
);
104 /* add one or more packets at the tail of the queue */
106 _addq_multi(class_queue_t
*q
, struct mbuf
*m_head
, struct mbuf
*m_tail
,
107 u_int32_t cnt
, u_int32_t size
)
109 MBUFQ_ENQUEUE_MULTI(&q
->mbufq
, m_head
, m_tail
);
114 /* get a packet at the head of the queue */
116 _getq(class_queue_t
*q
)
120 MBUFQ_DEQUEUE(&q
->mbufq
, m
);
122 VERIFY(qlen(q
) == 0);
130 /* qsize is an approximation, so adjust if necessary */
131 if (((int)qsize(q
) - m_length(m
)) > 0)
132 qsize(q
) -= m_length(m
);
133 else if (qsize(q
) != 0)
139 /* get a packet of a specific flow beginning from the head of the queue */
141 _getq_flow(class_queue_t
*q
, u_int32_t flow
)
143 struct mbuf
*m
, *m_tmp
;
145 MBUFQ_FOREACH_SAFE(m
, &q
->mbufq
, m_tmp
) {
146 if (flow
== 0 || ((m
->m_flags
& M_PKTHDR
) &&
147 m
->m_pkthdr
.m_flowhash
== flow
)) {
148 /* remove it from the class queue */
149 MBUFQ_REMOVE(&q
->mbufq
, m
);
150 MBUFQ_NEXT(m
) = NULL
;
156 u_int32_t l
= m_length(m
);
161 /* qsize is an approximation, so adjust if necessary */
162 if (((int)qsize(q
) - l
) > 0)
164 else if (qsize(q
) != 0)
171 /* get all packets starting from the head of the queue */
173 _getq_all(class_queue_t
*q
)
177 m
= MBUFQ_FIRST(&q
->mbufq
);
178 MBUFQ_INIT(&q
->mbufq
);
185 /* drop a packet at the tail of the queue */
187 _getq_tail(class_queue_t
*q
)
189 struct mq_head
*head
= &q
->mbufq
;
190 struct mbuf
*m
= MBUFQ_LAST(head
);
193 struct mbuf
*n
= MBUFQ_FIRST(head
);
196 struct mbuf
*next
= MBUFQ_NEXT(n
);
198 MBUFQ_NEXT(n
) = NULL
;
204 (qlen(q
) == 1 && m
== MBUFQ_FIRST(head
)));
208 /* qsize is an approximation, so adjust if necessary */
209 if (((int)qsize(q
) - m_length(m
)) > 0)
210 qsize(q
) -= m_length(m
);
211 else if (qsize(q
) != 0)
215 VERIFY(MBUFQ_EMPTY(head
));
219 head
->mq_last
= &MBUFQ_NEXT(n
);
225 /* randomly select a packet in the queue */
227 _getq_random(class_queue_t
*q
)
229 struct mq_head
*head
= &q
->mbufq
;
230 struct mbuf
*m
= NULL
;
236 VERIFY(MBUFQ_EMPTY(head
));
242 m
= MBUFQ_FIRST(head
);
243 read_random(&rnd
, sizeof (rnd
));
247 if ((MBUFQ_FIRST(head
) = MBUFQ_NEXT(m
)) == NULL
)
248 (head
)->mq_last
= &MBUFQ_FIRST(head
);
250 struct mbuf
*p
= NULL
;
254 if (MBUFQ_NEXT(m
) == NULL
)
259 VERIFY(p
!= NULL
&& MBUFQ_NEXT(p
) == m
);
261 if ((MBUFQ_NEXT(p
) = MBUFQ_NEXT(m
)) == NULL
)
262 (head
)->mq_last
= &MBUFQ_NEXT(p
);
268 /* qsize is an approximation, so adjust if necessary */
269 if (((int)qsize(q
) - m_length(m
)) > 0)
270 qsize(q
) -= m_length(m
);
271 else if (qsize(q
) != 0)
274 MBUFQ_NEXT(m
) = NULL
;
279 /* remove a packet from the queue */
281 _removeq(class_queue_t
*q
, struct mbuf
*m
)
283 struct mq_head
*head
= &q
->mbufq
;
284 struct mbuf
*m0
, **mtail
;
286 m0
= MBUFQ_FIRST(head
);
291 while (MBUFQ_NEXT(m0
) != m
) {
296 mtail
= &MBUFQ_NEXT(m0
);
298 mtail
= &MBUFQ_FIRST(head
);
301 *mtail
= MBUFQ_NEXT(m
);
303 head
->mq_last
= mtail
;
308 /* qsize is an approximation, so adjust if necessary */
309 if (((int)qsize(q
) - m_length(m
)) > 0)
310 qsize(q
) -= m_length(m
);
311 else if (qsize(q
) != 0)
314 MBUFQ_NEXT(m
) = NULL
;
318 _flushq(class_queue_t
*q
)
320 (void) _flushq_flow(q
, 0, NULL
, NULL
);
324 _flushq_flow(class_queue_t
*q
, u_int32_t flow
, u_int32_t
*cnt
, u_int32_t
*len
)
326 MBUFQ_HEAD(mq_freeq
) freeq
;
327 struct mbuf
*m
, *m_tmp
;
328 u_int32_t c
= 0, l
= 0;
332 MBUFQ_FOREACH_SAFE(m
, &q
->mbufq
, m_tmp
) {
333 if (flow
== 0 || ((m
->m_flags
& M_PKTHDR
) &&
334 m
->m_pkthdr
.m_flowhash
== flow
)) {
335 /* remove it from the class queue */
336 MBUFQ_REMOVE(&q
->mbufq
, m
);
337 MBUFQ_NEXT(m
) = NULL
;
339 /* and add it to the free queue */
340 MBUFQ_ENQUEUE(&freeq
, m
);
346 VERIFY(c
== 0 || !MBUFQ_EMPTY(&freeq
));
349 VERIFY(qlen(q
) >= c
);
352 /* qsize is an approximation, so adjust if necessary */
353 if (((int)qsize(q
) - l
) > 0)
355 else if (qsize(q
) != 0)
359 if (!MBUFQ_EMPTY(&freeq
))
360 m_freem_list(MBUFQ_FIRST(&freeq
));