1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 2007-2021 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions *
4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions *
6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions *
15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions *
18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions * limitations under the License.
25*1b191cb5SApple OSS Distributions *
26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions */
28*1b191cb5SApple OSS Distributions
29*1b191cb5SApple OSS Distributions /*
30*1b191cb5SApple OSS Distributions * Copyright (c) 1991-1997 Regents of the University of California.
31*1b191cb5SApple OSS Distributions * All rights reserved.
32*1b191cb5SApple OSS Distributions *
33*1b191cb5SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
34*1b191cb5SApple OSS Distributions * modification, are permitted provided that the following conditions
35*1b191cb5SApple OSS Distributions * are met:
36*1b191cb5SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
37*1b191cb5SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
38*1b191cb5SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
39*1b191cb5SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
40*1b191cb5SApple OSS Distributions * documentation and/or other materials provided with the distribution.
41*1b191cb5SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
42*1b191cb5SApple OSS Distributions * must display the following acknowledgement:
43*1b191cb5SApple OSS Distributions * This product includes software developed by the Network Research
44*1b191cb5SApple OSS Distributions * Group at Lawrence Berkeley Laboratory.
45*1b191cb5SApple OSS Distributions * 4. Neither the name of the University nor of the Laboratory may be used
46*1b191cb5SApple OSS Distributions * to endorse or promote products derived from this software without
47*1b191cb5SApple OSS Distributions * specific prior written permission.
48*1b191cb5SApple OSS Distributions *
49*1b191cb5SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50*1b191cb5SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*1b191cb5SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52*1b191cb5SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53*1b191cb5SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54*1b191cb5SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55*1b191cb5SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56*1b191cb5SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57*1b191cb5SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58*1b191cb5SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59*1b191cb5SApple OSS Distributions * SUCH DAMAGE.
60*1b191cb5SApple OSS Distributions */
61*1b191cb5SApple OSS Distributions
62*1b191cb5SApple OSS Distributions #include <sys/cdefs.h>
63*1b191cb5SApple OSS Distributions #include <sys/param.h>
64*1b191cb5SApple OSS Distributions #include <sys/mbuf.h>
65*1b191cb5SApple OSS Distributions #include <sys/errno.h>
66*1b191cb5SApple OSS Distributions #include <sys/random.h>
67*1b191cb5SApple OSS Distributions #include <sys/kernel_types.h>
68*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
69*1b191cb5SApple OSS Distributions
70*1b191cb5SApple OSS Distributions #include <net/if.h>
71*1b191cb5SApple OSS Distributions #include <net/net_osdep.h>
72*1b191cb5SApple OSS Distributions #include <net/classq/classq.h>
73*1b191cb5SApple OSS Distributions
74*1b191cb5SApple OSS Distributions #include <libkern/libkern.h>
75*1b191cb5SApple OSS Distributions
76*1b191cb5SApple OSS Distributions #if SKYWALK
77*1b191cb5SApple OSS Distributions #include <skywalk/os_skywalk_private.h>
78*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
79*1b191cb5SApple OSS Distributions
80*1b191cb5SApple OSS Distributions u_int32_t classq_verbose = 0; /* more noise if greater than 1 */
81*1b191cb5SApple OSS Distributions
82*1b191cb5SApple OSS Distributions SYSCTL_NODE(_net, OID_AUTO, classq, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "classq");
83*1b191cb5SApple OSS Distributions
84*1b191cb5SApple OSS Distributions SYSCTL_UINT(_net_classq, OID_AUTO, verbose, CTLFLAG_RW | CTLFLAG_LOCKED,
85*1b191cb5SApple OSS Distributions &classq_verbose, 0, "Class queue verbosity level");
86*1b191cb5SApple OSS Distributions
87*1b191cb5SApple OSS Distributions void
_qinit(class_queue_t * q,int type,int lim,classq_pkt_type_t ptype)88*1b191cb5SApple OSS Distributions _qinit(class_queue_t *q, int type, int lim, classq_pkt_type_t ptype)
89*1b191cb5SApple OSS Distributions {
90*1b191cb5SApple OSS Distributions switch (ptype) {
91*1b191cb5SApple OSS Distributions case QP_MBUF:
92*1b191cb5SApple OSS Distributions MBUFQ_INIT(&qmbufq(q));
93*1b191cb5SApple OSS Distributions break;
94*1b191cb5SApple OSS Distributions
95*1b191cb5SApple OSS Distributions #if SKYWALK
96*1b191cb5SApple OSS Distributions case QP_PACKET:
97*1b191cb5SApple OSS Distributions KPKTQ_INIT(&qkpktq(q));
98*1b191cb5SApple OSS Distributions break;
99*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
100*1b191cb5SApple OSS Distributions
101*1b191cb5SApple OSS Distributions default:
102*1b191cb5SApple OSS Distributions VERIFY(0);
103*1b191cb5SApple OSS Distributions /* NOTREACHED */
104*1b191cb5SApple OSS Distributions }
105*1b191cb5SApple OSS Distributions
106*1b191cb5SApple OSS Distributions qlimit(q) = lim;
107*1b191cb5SApple OSS Distributions qlen(q) = 0;
108*1b191cb5SApple OSS Distributions qsize(q) = 0;
109*1b191cb5SApple OSS Distributions qtype(q) = type;
110*1b191cb5SApple OSS Distributions qptype(q) = ptype;
111*1b191cb5SApple OSS Distributions qstate(q) = QS_RUNNING;
112*1b191cb5SApple OSS Distributions }
113*1b191cb5SApple OSS Distributions
114*1b191cb5SApple OSS Distributions /* add a packet at the tail of the queue */
115*1b191cb5SApple OSS Distributions void
_addq(class_queue_t * q,classq_pkt_t * pkt)116*1b191cb5SApple OSS Distributions _addq(class_queue_t *q, classq_pkt_t *pkt)
117*1b191cb5SApple OSS Distributions {
118*1b191cb5SApple OSS Distributions uint32_t size = 0;
119*1b191cb5SApple OSS Distributions
120*1b191cb5SApple OSS Distributions ASSERT(pkt->cp_ptype == qptype(q));
121*1b191cb5SApple OSS Distributions
122*1b191cb5SApple OSS Distributions switch (qptype(q)) {
123*1b191cb5SApple OSS Distributions case QP_MBUF: {
124*1b191cb5SApple OSS Distributions struct mbuf *m = pkt->cp_mbuf;
125*1b191cb5SApple OSS Distributions MBUFQ_ENQUEUE(&qmbufq(q), m);
126*1b191cb5SApple OSS Distributions size = m_length(m);
127*1b191cb5SApple OSS Distributions break;
128*1b191cb5SApple OSS Distributions }
129*1b191cb5SApple OSS Distributions
130*1b191cb5SApple OSS Distributions #if SKYWALK
131*1b191cb5SApple OSS Distributions case QP_PACKET: {
132*1b191cb5SApple OSS Distributions struct __kern_packet *kp = pkt->cp_kpkt;
133*1b191cb5SApple OSS Distributions KPKTQ_ENQUEUE(&qkpktq(q), kp);
134*1b191cb5SApple OSS Distributions size = kp->pkt_length;
135*1b191cb5SApple OSS Distributions break;
136*1b191cb5SApple OSS Distributions }
137*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
138*1b191cb5SApple OSS Distributions
139*1b191cb5SApple OSS Distributions default:
140*1b191cb5SApple OSS Distributions VERIFY(0);
141*1b191cb5SApple OSS Distributions /* NOTREACHED */
142*1b191cb5SApple OSS Distributions __builtin_unreachable();
143*1b191cb5SApple OSS Distributions }
144*1b191cb5SApple OSS Distributions
145*1b191cb5SApple OSS Distributions qlen(q)++;
146*1b191cb5SApple OSS Distributions VERIFY(qlen(q) != 0);
147*1b191cb5SApple OSS Distributions qsize(q) += size;
148*1b191cb5SApple OSS Distributions }
149*1b191cb5SApple OSS Distributions
150*1b191cb5SApple OSS Distributions /* add one or more packets at the tail of the queue */
151*1b191cb5SApple OSS Distributions void
_addq_multi(class_queue_t * q,classq_pkt_t * pkt_head,classq_pkt_t * pkt_tail,u_int32_t cnt,u_int64_t size)152*1b191cb5SApple OSS Distributions _addq_multi(class_queue_t *q, classq_pkt_t *pkt_head, classq_pkt_t *pkt_tail,
153*1b191cb5SApple OSS Distributions u_int32_t cnt, u_int64_t size)
154*1b191cb5SApple OSS Distributions {
155*1b191cb5SApple OSS Distributions ASSERT(pkt_head->cp_ptype == qptype(q));
156*1b191cb5SApple OSS Distributions ASSERT(pkt_tail->cp_ptype == qptype(q));
157*1b191cb5SApple OSS Distributions switch (qptype(q)) {
158*1b191cb5SApple OSS Distributions case QP_MBUF: {
159*1b191cb5SApple OSS Distributions struct mbuf *m_head = pkt_head->cp_mbuf;
160*1b191cb5SApple OSS Distributions struct mbuf *m_tail = pkt_tail->cp_mbuf;
161*1b191cb5SApple OSS Distributions MBUFQ_ENQUEUE_MULTI(&qmbufq(q), m_head, m_tail);
162*1b191cb5SApple OSS Distributions break;
163*1b191cb5SApple OSS Distributions }
164*1b191cb5SApple OSS Distributions
165*1b191cb5SApple OSS Distributions #if SKYWALK
166*1b191cb5SApple OSS Distributions case QP_PACKET: {
167*1b191cb5SApple OSS Distributions struct __kern_packet *kp_head = pkt_head->cp_kpkt;
168*1b191cb5SApple OSS Distributions struct __kern_packet *kp_tail = pkt_tail->cp_kpkt;
169*1b191cb5SApple OSS Distributions KPKTQ_ENQUEUE_MULTI(&qkpktq(q), kp_head, kp_tail, cnt);
170*1b191cb5SApple OSS Distributions break;
171*1b191cb5SApple OSS Distributions }
172*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
173*1b191cb5SApple OSS Distributions
174*1b191cb5SApple OSS Distributions default:
175*1b191cb5SApple OSS Distributions VERIFY(0);
176*1b191cb5SApple OSS Distributions /* NOTREACHED */
177*1b191cb5SApple OSS Distributions __builtin_unreachable();
178*1b191cb5SApple OSS Distributions }
179*1b191cb5SApple OSS Distributions
180*1b191cb5SApple OSS Distributions qlen(q) += cnt;
181*1b191cb5SApple OSS Distributions qsize(q) += size;
182*1b191cb5SApple OSS Distributions }
183*1b191cb5SApple OSS Distributions
184*1b191cb5SApple OSS Distributions /* get a packet at the head of the queue */
185*1b191cb5SApple OSS Distributions void
_getq(class_queue_t * q,classq_pkt_t * pkt)186*1b191cb5SApple OSS Distributions _getq(class_queue_t *q, classq_pkt_t *pkt)
187*1b191cb5SApple OSS Distributions {
188*1b191cb5SApple OSS Distributions uint32_t pkt_len = 0;
189*1b191cb5SApple OSS Distributions
190*1b191cb5SApple OSS Distributions switch (qptype(q)) {
191*1b191cb5SApple OSS Distributions case QP_MBUF: {
192*1b191cb5SApple OSS Distributions MBUFQ_DEQUEUE(&qmbufq(q), pkt->cp_mbuf);
193*1b191cb5SApple OSS Distributions if (__probable(pkt->cp_mbuf != NULL)) {
194*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_MBUF(pkt, pkt->cp_mbuf);
195*1b191cb5SApple OSS Distributions pkt_len = m_length(pkt->cp_mbuf);
196*1b191cb5SApple OSS Distributions }
197*1b191cb5SApple OSS Distributions break;
198*1b191cb5SApple OSS Distributions }
199*1b191cb5SApple OSS Distributions
200*1b191cb5SApple OSS Distributions #if SKYWALK
201*1b191cb5SApple OSS Distributions case QP_PACKET: {
202*1b191cb5SApple OSS Distributions KPKTQ_DEQUEUE(&qkpktq(q), pkt->cp_kpkt);
203*1b191cb5SApple OSS Distributions if (__probable(pkt->cp_kpkt != NULL)) {
204*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_PACKET(pkt, pkt->cp_kpkt);
205*1b191cb5SApple OSS Distributions pkt_len = pkt->cp_kpkt->pkt_length;
206*1b191cb5SApple OSS Distributions }
207*1b191cb5SApple OSS Distributions break;
208*1b191cb5SApple OSS Distributions }
209*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
210*1b191cb5SApple OSS Distributions
211*1b191cb5SApple OSS Distributions default:
212*1b191cb5SApple OSS Distributions VERIFY(0);
213*1b191cb5SApple OSS Distributions /* NOTREACHED */
214*1b191cb5SApple OSS Distributions __builtin_unreachable();
215*1b191cb5SApple OSS Distributions }
216*1b191cb5SApple OSS Distributions
217*1b191cb5SApple OSS Distributions if (pkt->cp_mbuf == NULL) {
218*1b191cb5SApple OSS Distributions VERIFY(qlen(q) == 0);
219*1b191cb5SApple OSS Distributions if (qsize(q) > 0) {
220*1b191cb5SApple OSS Distributions qsize(q) = 0;
221*1b191cb5SApple OSS Distributions }
222*1b191cb5SApple OSS Distributions return;
223*1b191cb5SApple OSS Distributions }
224*1b191cb5SApple OSS Distributions VERIFY(qlen(q) > 0);
225*1b191cb5SApple OSS Distributions qlen(q)--;
226*1b191cb5SApple OSS Distributions
227*1b191cb5SApple OSS Distributions /* qsize is an approximation, so adjust if necessary */
228*1b191cb5SApple OSS Distributions if (((int)qsize(q) - pkt_len) > 0) {
229*1b191cb5SApple OSS Distributions qsize(q) -= pkt_len;
230*1b191cb5SApple OSS Distributions } else if (qsize(q) != 0) {
231*1b191cb5SApple OSS Distributions qsize(q) = 0;
232*1b191cb5SApple OSS Distributions }
233*1b191cb5SApple OSS Distributions }
234*1b191cb5SApple OSS Distributions
235*1b191cb5SApple OSS Distributions static void
_getq_flow_or_scidx(class_queue_t * q,classq_pkt_t * pkt,u_int32_t val,boolean_t isflowid)236*1b191cb5SApple OSS Distributions _getq_flow_or_scidx(class_queue_t *q, classq_pkt_t *pkt, u_int32_t val,
237*1b191cb5SApple OSS Distributions boolean_t isflowid)
238*1b191cb5SApple OSS Distributions {
239*1b191cb5SApple OSS Distributions uint32_t pkt_len = 0;
240*1b191cb5SApple OSS Distributions
241*1b191cb5SApple OSS Distributions switch (qptype(q)) {
242*1b191cb5SApple OSS Distributions case QP_MBUF: {
243*1b191cb5SApple OSS Distributions struct mbuf *m, *m_tmp;
244*1b191cb5SApple OSS Distributions
245*1b191cb5SApple OSS Distributions MBUFQ_FOREACH_SAFE(m, &qmbufq(q), m_tmp) {
246*1b191cb5SApple OSS Distributions if ((isflowid && (val == 0 ||
247*1b191cb5SApple OSS Distributions ((m->m_flags & M_PKTHDR) &&
248*1b191cb5SApple OSS Distributions m->m_pkthdr.pkt_flowid == val))) ||
249*1b191cb5SApple OSS Distributions (!isflowid &&
250*1b191cb5SApple OSS Distributions MBUF_SCIDX(mbuf_get_service_class(m)) < val)) {
251*1b191cb5SApple OSS Distributions /* remove it from the class queue */
252*1b191cb5SApple OSS Distributions MBUFQ_REMOVE(&qmbufq(q), m);
253*1b191cb5SApple OSS Distributions MBUFQ_NEXT(m) = NULL;
254*1b191cb5SApple OSS Distributions break;
255*1b191cb5SApple OSS Distributions }
256*1b191cb5SApple OSS Distributions }
257*1b191cb5SApple OSS Distributions if (__probable(m != NULL)) {
258*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_MBUF(pkt, m);
259*1b191cb5SApple OSS Distributions pkt_len = m_length(m);
260*1b191cb5SApple OSS Distributions }
261*1b191cb5SApple OSS Distributions break;
262*1b191cb5SApple OSS Distributions }
263*1b191cb5SApple OSS Distributions
264*1b191cb5SApple OSS Distributions #if SKYWALK
265*1b191cb5SApple OSS Distributions case QP_PACKET: {
266*1b191cb5SApple OSS Distributions struct __kern_packet *kp, *kp_tmp;
267*1b191cb5SApple OSS Distributions
268*1b191cb5SApple OSS Distributions KPKTQ_FOREACH_SAFE(kp, &qkpktq(q), kp_tmp) {
269*1b191cb5SApple OSS Distributions if ((isflowid && (val == 0 ||
270*1b191cb5SApple OSS Distributions (kp->pkt_flow_token == val))) || (!isflowid &&
271*1b191cb5SApple OSS Distributions MBUF_SCIDX(kp->pkt_svc_class) < val)) {
272*1b191cb5SApple OSS Distributions /* remove it from the class queue */
273*1b191cb5SApple OSS Distributions KPKTQ_REMOVE(&qkpktq(q), kp);
274*1b191cb5SApple OSS Distributions KPKTQ_NEXT(kp) = NULL;
275*1b191cb5SApple OSS Distributions break;
276*1b191cb5SApple OSS Distributions }
277*1b191cb5SApple OSS Distributions }
278*1b191cb5SApple OSS Distributions if (__probable(kp != NULL)) {
279*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_PACKET(pkt, kp);
280*1b191cb5SApple OSS Distributions pkt_len = kp->pkt_length;
281*1b191cb5SApple OSS Distributions }
282*1b191cb5SApple OSS Distributions break;
283*1b191cb5SApple OSS Distributions }
284*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
285*1b191cb5SApple OSS Distributions
286*1b191cb5SApple OSS Distributions default:
287*1b191cb5SApple OSS Distributions VERIFY(0);
288*1b191cb5SApple OSS Distributions /* NOTREACHED */
289*1b191cb5SApple OSS Distributions __builtin_unreachable();
290*1b191cb5SApple OSS Distributions }
291*1b191cb5SApple OSS Distributions
292*1b191cb5SApple OSS Distributions if (pkt->cp_mbuf != NULL) {
293*1b191cb5SApple OSS Distributions VERIFY(qlen(q) > 0);
294*1b191cb5SApple OSS Distributions qlen(q)--;
295*1b191cb5SApple OSS Distributions
296*1b191cb5SApple OSS Distributions /* qsize is an approximation, so adjust if necessary */
297*1b191cb5SApple OSS Distributions if (((int)qsize(q) - pkt_len) > 0) {
298*1b191cb5SApple OSS Distributions qsize(q) -= pkt_len;
299*1b191cb5SApple OSS Distributions } else if (qsize(q) != 0) {
300*1b191cb5SApple OSS Distributions qsize(q) = 0;
301*1b191cb5SApple OSS Distributions }
302*1b191cb5SApple OSS Distributions }
303*1b191cb5SApple OSS Distributions }
304*1b191cb5SApple OSS Distributions
305*1b191cb5SApple OSS Distributions /* get a packet of a specific flow beginning from the head of the queue */
306*1b191cb5SApple OSS Distributions void
_getq_flow(class_queue_t * q,classq_pkt_t * pkt,u_int32_t flow)307*1b191cb5SApple OSS Distributions _getq_flow(class_queue_t *q, classq_pkt_t *pkt, u_int32_t flow)
308*1b191cb5SApple OSS Distributions {
309*1b191cb5SApple OSS Distributions return _getq_flow_or_scidx(q, pkt, flow, TRUE);
310*1b191cb5SApple OSS Distributions }
311*1b191cb5SApple OSS Distributions
312*1b191cb5SApple OSS Distributions /* Get a packet whose MBUF_SCIDX() < scidx from head of queue */
313*1b191cb5SApple OSS Distributions void
_getq_scidx_lt(class_queue_t * q,classq_pkt_t * pkt,u_int32_t scidx)314*1b191cb5SApple OSS Distributions _getq_scidx_lt(class_queue_t *q, classq_pkt_t *pkt, u_int32_t scidx)
315*1b191cb5SApple OSS Distributions {
316*1b191cb5SApple OSS Distributions return _getq_flow_or_scidx(q, pkt, scidx, FALSE);
317*1b191cb5SApple OSS Distributions }
318*1b191cb5SApple OSS Distributions
319*1b191cb5SApple OSS Distributions /* get all packets (chained) starting from the head of the queue */
320*1b191cb5SApple OSS Distributions void
_getq_all(class_queue_t * q,classq_pkt_t * first,classq_pkt_t * last,u_int32_t * qlenp,u_int64_t * qsizep)321*1b191cb5SApple OSS Distributions _getq_all(class_queue_t *q, classq_pkt_t *first, classq_pkt_t *last,
322*1b191cb5SApple OSS Distributions u_int32_t *qlenp, u_int64_t *qsizep)
323*1b191cb5SApple OSS Distributions {
324*1b191cb5SApple OSS Distributions switch (qptype(q)) {
325*1b191cb5SApple OSS Distributions case QP_MBUF:
326*1b191cb5SApple OSS Distributions first->cp_mbuf = MBUFQ_FIRST(&qmbufq(q));
327*1b191cb5SApple OSS Distributions if (__probable(first->cp_mbuf != NULL)) {
328*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_MBUF(first, first->cp_mbuf);
329*1b191cb5SApple OSS Distributions }
330*1b191cb5SApple OSS Distributions if (last != NULL) {
331*1b191cb5SApple OSS Distributions last->cp_mbuf = MBUFQ_LAST(&qmbufq(q));
332*1b191cb5SApple OSS Distributions if (__probable(last->cp_mbuf != NULL)) {
333*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_MBUF(last, last->cp_mbuf);
334*1b191cb5SApple OSS Distributions }
335*1b191cb5SApple OSS Distributions }
336*1b191cb5SApple OSS Distributions MBUFQ_INIT(&qmbufq(q));
337*1b191cb5SApple OSS Distributions break;
338*1b191cb5SApple OSS Distributions
339*1b191cb5SApple OSS Distributions #if SKYWALK
340*1b191cb5SApple OSS Distributions case QP_PACKET:
341*1b191cb5SApple OSS Distributions first->cp_kpkt = KPKTQ_FIRST(&qkpktq(q));
342*1b191cb5SApple OSS Distributions if (__probable(first->cp_kpkt != NULL)) {
343*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_PACKET(first, first->cp_kpkt);
344*1b191cb5SApple OSS Distributions }
345*1b191cb5SApple OSS Distributions if (last != NULL) {
346*1b191cb5SApple OSS Distributions last->cp_kpkt = KPKTQ_LAST(&qkpktq(q));
347*1b191cb5SApple OSS Distributions if (__probable(last->cp_kpkt != NULL)) {
348*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_PACKET(last, last->cp_kpkt);
349*1b191cb5SApple OSS Distributions }
350*1b191cb5SApple OSS Distributions }
351*1b191cb5SApple OSS Distributions KPKTQ_INIT(&qkpktq(q));
352*1b191cb5SApple OSS Distributions break;
353*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
354*1b191cb5SApple OSS Distributions
355*1b191cb5SApple OSS Distributions default:
356*1b191cb5SApple OSS Distributions VERIFY(0);
357*1b191cb5SApple OSS Distributions /* NOTREACHED */
358*1b191cb5SApple OSS Distributions __builtin_unreachable();
359*1b191cb5SApple OSS Distributions }
360*1b191cb5SApple OSS Distributions
361*1b191cb5SApple OSS Distributions if (qlenp != NULL) {
362*1b191cb5SApple OSS Distributions *qlenp = qlen(q);
363*1b191cb5SApple OSS Distributions }
364*1b191cb5SApple OSS Distributions if (qsizep != NULL) {
365*1b191cb5SApple OSS Distributions *qsizep = qsize(q);
366*1b191cb5SApple OSS Distributions }
367*1b191cb5SApple OSS Distributions
368*1b191cb5SApple OSS Distributions qlen(q) = 0;
369*1b191cb5SApple OSS Distributions qsize(q) = 0;
370*1b191cb5SApple OSS Distributions }
371*1b191cb5SApple OSS Distributions
372*1b191cb5SApple OSS Distributions static inline struct mbuf *
_getq_tail_mbuf(class_queue_t * q)373*1b191cb5SApple OSS Distributions _getq_tail_mbuf(class_queue_t *q)
374*1b191cb5SApple OSS Distributions {
375*1b191cb5SApple OSS Distributions struct mq_head *head = &qmbufq(q);
376*1b191cb5SApple OSS Distributions struct mbuf *m = MBUFQ_LAST(head);
377*1b191cb5SApple OSS Distributions
378*1b191cb5SApple OSS Distributions if (m != NULL) {
379*1b191cb5SApple OSS Distributions struct mbuf *n = MBUFQ_FIRST(head);
380*1b191cb5SApple OSS Distributions
381*1b191cb5SApple OSS Distributions while (n != NULL) {
382*1b191cb5SApple OSS Distributions struct mbuf *next = MBUFQ_NEXT(n);
383*1b191cb5SApple OSS Distributions if (next == m) {
384*1b191cb5SApple OSS Distributions MBUFQ_NEXT(n) = NULL;
385*1b191cb5SApple OSS Distributions break;
386*1b191cb5SApple OSS Distributions }
387*1b191cb5SApple OSS Distributions n = next;
388*1b191cb5SApple OSS Distributions }
389*1b191cb5SApple OSS Distributions VERIFY(n != NULL ||
390*1b191cb5SApple OSS Distributions (qlen(q) == 1 && m == MBUFQ_FIRST(head)));
391*1b191cb5SApple OSS Distributions VERIFY(qlen(q) > 0);
392*1b191cb5SApple OSS Distributions --qlen(q);
393*1b191cb5SApple OSS Distributions
394*1b191cb5SApple OSS Distributions /* qsize is an approximation, so adjust if necessary */
395*1b191cb5SApple OSS Distributions if (((int)qsize(q) - m_length(m)) > 0) {
396*1b191cb5SApple OSS Distributions qsize(q) -= m_length(m);
397*1b191cb5SApple OSS Distributions } else if (qsize(q) != 0) {
398*1b191cb5SApple OSS Distributions qsize(q) = 0;
399*1b191cb5SApple OSS Distributions }
400*1b191cb5SApple OSS Distributions
401*1b191cb5SApple OSS Distributions if (qempty(q)) {
402*1b191cb5SApple OSS Distributions VERIFY(m == MBUFQ_FIRST(head));
403*1b191cb5SApple OSS Distributions MBUFQ_INIT(head);
404*1b191cb5SApple OSS Distributions } else {
405*1b191cb5SApple OSS Distributions VERIFY(n != NULL);
406*1b191cb5SApple OSS Distributions head->mq_last = &MBUFQ_NEXT(n);
407*1b191cb5SApple OSS Distributions }
408*1b191cb5SApple OSS Distributions }
409*1b191cb5SApple OSS Distributions return m;
410*1b191cb5SApple OSS Distributions }
411*1b191cb5SApple OSS Distributions
412*1b191cb5SApple OSS Distributions /* drop a packet at the tail of the queue */
413*1b191cb5SApple OSS Distributions void
_getq_tail(class_queue_t * q,classq_pkt_t * pkt)414*1b191cb5SApple OSS Distributions _getq_tail(class_queue_t *q, classq_pkt_t *pkt)
415*1b191cb5SApple OSS Distributions {
416*1b191cb5SApple OSS Distributions switch (qptype(q)) {
417*1b191cb5SApple OSS Distributions case QP_MBUF:
418*1b191cb5SApple OSS Distributions pkt->cp_mbuf = _getq_tail_mbuf(q);
419*1b191cb5SApple OSS Distributions if (__probable(pkt->cp_mbuf != NULL)) {
420*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_MBUF(pkt, pkt->cp_mbuf);
421*1b191cb5SApple OSS Distributions }
422*1b191cb5SApple OSS Distributions break;
423*1b191cb5SApple OSS Distributions
424*1b191cb5SApple OSS Distributions #if SKYWALK
425*1b191cb5SApple OSS Distributions case QP_PACKET: /* XXX: Add support for Kernel packet when needed */
426*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
427*1b191cb5SApple OSS Distributions default:
428*1b191cb5SApple OSS Distributions VERIFY(0);
429*1b191cb5SApple OSS Distributions /* NOTREACHED */
430*1b191cb5SApple OSS Distributions __builtin_unreachable();
431*1b191cb5SApple OSS Distributions }
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions
434*1b191cb5SApple OSS Distributions static inline struct mbuf *
_getq_random_mbuf(class_queue_t * q)435*1b191cb5SApple OSS Distributions _getq_random_mbuf(class_queue_t *q)
436*1b191cb5SApple OSS Distributions {
437*1b191cb5SApple OSS Distributions struct mq_head *head = &qmbufq(q);
438*1b191cb5SApple OSS Distributions struct mbuf *m = NULL;
439*1b191cb5SApple OSS Distributions unsigned int n;
440*1b191cb5SApple OSS Distributions u_int32_t rnd;
441*1b191cb5SApple OSS Distributions
442*1b191cb5SApple OSS Distributions /* XXX: Add support for Kernel packet when needed */
443*1b191cb5SApple OSS Distributions VERIFY((qptype(q) == QP_MBUF));
444*1b191cb5SApple OSS Distributions
445*1b191cb5SApple OSS Distributions n = qlen(q);
446*1b191cb5SApple OSS Distributions if (n == 0) {
447*1b191cb5SApple OSS Distributions VERIFY(MBUFQ_EMPTY(head));
448*1b191cb5SApple OSS Distributions if (qsize(q) > 0) {
449*1b191cb5SApple OSS Distributions qsize(q) = 0;
450*1b191cb5SApple OSS Distributions }
451*1b191cb5SApple OSS Distributions return NULL;
452*1b191cb5SApple OSS Distributions }
453*1b191cb5SApple OSS Distributions
454*1b191cb5SApple OSS Distributions m = MBUFQ_FIRST(head);
455*1b191cb5SApple OSS Distributions read_frandom(&rnd, sizeof(rnd));
456*1b191cb5SApple OSS Distributions n = (rnd % n) + 1;
457*1b191cb5SApple OSS Distributions
458*1b191cb5SApple OSS Distributions if (n == 1) {
459*1b191cb5SApple OSS Distributions if ((MBUFQ_FIRST(head) = MBUFQ_NEXT(m)) == NULL) {
460*1b191cb5SApple OSS Distributions (head)->mq_last = &MBUFQ_FIRST(head);
461*1b191cb5SApple OSS Distributions }
462*1b191cb5SApple OSS Distributions } else {
463*1b191cb5SApple OSS Distributions struct mbuf *p = NULL;
464*1b191cb5SApple OSS Distributions
465*1b191cb5SApple OSS Distributions VERIFY(n > 1);
466*1b191cb5SApple OSS Distributions while (n--) {
467*1b191cb5SApple OSS Distributions if (MBUFQ_NEXT(m) == NULL) {
468*1b191cb5SApple OSS Distributions break;
469*1b191cb5SApple OSS Distributions }
470*1b191cb5SApple OSS Distributions p = m;
471*1b191cb5SApple OSS Distributions m = MBUFQ_NEXT(m);
472*1b191cb5SApple OSS Distributions }
473*1b191cb5SApple OSS Distributions VERIFY(p != NULL && MBUFQ_NEXT(p) == m);
474*1b191cb5SApple OSS Distributions
475*1b191cb5SApple OSS Distributions if ((MBUFQ_NEXT(p) = MBUFQ_NEXT(m)) == NULL) {
476*1b191cb5SApple OSS Distributions (head)->mq_last = &MBUFQ_NEXT(p);
477*1b191cb5SApple OSS Distributions }
478*1b191cb5SApple OSS Distributions }
479*1b191cb5SApple OSS Distributions
480*1b191cb5SApple OSS Distributions VERIFY(qlen(q) > 0);
481*1b191cb5SApple OSS Distributions --qlen(q);
482*1b191cb5SApple OSS Distributions
483*1b191cb5SApple OSS Distributions /* qsize is an approximation, so adjust if necessary */
484*1b191cb5SApple OSS Distributions if (((int)qsize(q) - m_length(m)) > 0) {
485*1b191cb5SApple OSS Distributions qsize(q) -= m_length(m);
486*1b191cb5SApple OSS Distributions } else if (qsize(q) != 0) {
487*1b191cb5SApple OSS Distributions qsize(q) = 0;
488*1b191cb5SApple OSS Distributions }
489*1b191cb5SApple OSS Distributions
490*1b191cb5SApple OSS Distributions MBUFQ_NEXT(m) = NULL;
491*1b191cb5SApple OSS Distributions
492*1b191cb5SApple OSS Distributions return m;
493*1b191cb5SApple OSS Distributions }
494*1b191cb5SApple OSS Distributions
495*1b191cb5SApple OSS Distributions /* randomly select a packet in the queue */
496*1b191cb5SApple OSS Distributions void
_getq_random(class_queue_t * q,classq_pkt_t * pkt)497*1b191cb5SApple OSS Distributions _getq_random(class_queue_t *q, classq_pkt_t *pkt)
498*1b191cb5SApple OSS Distributions {
499*1b191cb5SApple OSS Distributions switch (qptype(q)) {
500*1b191cb5SApple OSS Distributions case QP_MBUF:
501*1b191cb5SApple OSS Distributions pkt->cp_mbuf = _getq_random_mbuf(q);
502*1b191cb5SApple OSS Distributions if (__probable(pkt->cp_mbuf != NULL)) {
503*1b191cb5SApple OSS Distributions CLASSQ_PKT_INIT_MBUF(pkt, pkt->cp_mbuf);
504*1b191cb5SApple OSS Distributions }
505*1b191cb5SApple OSS Distributions break;
506*1b191cb5SApple OSS Distributions
507*1b191cb5SApple OSS Distributions #if SKYWALK
508*1b191cb5SApple OSS Distributions case QP_PACKET: /* XXX: Add support for Kernel packet when needed */
509*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
510*1b191cb5SApple OSS Distributions default:
511*1b191cb5SApple OSS Distributions VERIFY(0);
512*1b191cb5SApple OSS Distributions /* NOTREACHED */
513*1b191cb5SApple OSS Distributions __builtin_unreachable();
514*1b191cb5SApple OSS Distributions }
515*1b191cb5SApple OSS Distributions }
516*1b191cb5SApple OSS Distributions
517*1b191cb5SApple OSS Distributions static inline void
_removeq_mbuf(class_queue_t * q,struct mbuf * m)518*1b191cb5SApple OSS Distributions _removeq_mbuf(class_queue_t *q, struct mbuf *m)
519*1b191cb5SApple OSS Distributions {
520*1b191cb5SApple OSS Distributions struct mq_head *head = &qmbufq(q);
521*1b191cb5SApple OSS Distributions struct mbuf *m0, **mtail;
522*1b191cb5SApple OSS Distributions
523*1b191cb5SApple OSS Distributions m0 = MBUFQ_FIRST(head);
524*1b191cb5SApple OSS Distributions if (m0 == NULL) {
525*1b191cb5SApple OSS Distributions return;
526*1b191cb5SApple OSS Distributions }
527*1b191cb5SApple OSS Distributions
528*1b191cb5SApple OSS Distributions if (m0 != m) {
529*1b191cb5SApple OSS Distributions while (m0 != NULL && MBUFQ_NEXT(m0) != m) {
530*1b191cb5SApple OSS Distributions m0 = MBUFQ_NEXT(m0);
531*1b191cb5SApple OSS Distributions }
532*1b191cb5SApple OSS Distributions if (m0 == NULL) {
533*1b191cb5SApple OSS Distributions return;
534*1b191cb5SApple OSS Distributions }
535*1b191cb5SApple OSS Distributions
536*1b191cb5SApple OSS Distributions mtail = &MBUFQ_NEXT(m0);
537*1b191cb5SApple OSS Distributions } else {
538*1b191cb5SApple OSS Distributions mtail = &MBUFQ_FIRST(head);
539*1b191cb5SApple OSS Distributions }
540*1b191cb5SApple OSS Distributions
541*1b191cb5SApple OSS Distributions *mtail = MBUFQ_NEXT(m);
542*1b191cb5SApple OSS Distributions if (*mtail == NULL) {
543*1b191cb5SApple OSS Distributions head->mq_last = mtail;
544*1b191cb5SApple OSS Distributions }
545*1b191cb5SApple OSS Distributions
546*1b191cb5SApple OSS Distributions VERIFY(qlen(q) > 0);
547*1b191cb5SApple OSS Distributions --qlen(q);
548*1b191cb5SApple OSS Distributions
549*1b191cb5SApple OSS Distributions /* qsize is an approximation, so adjust if necessary */
550*1b191cb5SApple OSS Distributions if (((int)qsize(q) - m_length(m)) > 0) {
551*1b191cb5SApple OSS Distributions qsize(q) -= m_length(m);
552*1b191cb5SApple OSS Distributions } else if (qsize(q) != 0) {
553*1b191cb5SApple OSS Distributions qsize(q) = 0;
554*1b191cb5SApple OSS Distributions }
555*1b191cb5SApple OSS Distributions
556*1b191cb5SApple OSS Distributions MBUFQ_NEXT(m) = NULL;
557*1b191cb5SApple OSS Distributions }
558*1b191cb5SApple OSS Distributions
559*1b191cb5SApple OSS Distributions /* remove a packet from the queue */
560*1b191cb5SApple OSS Distributions void
_removeq(class_queue_t * q,classq_pkt_t * pkt)561*1b191cb5SApple OSS Distributions _removeq(class_queue_t *q, classq_pkt_t *pkt)
562*1b191cb5SApple OSS Distributions {
563*1b191cb5SApple OSS Distributions switch (qptype(q)) {
564*1b191cb5SApple OSS Distributions case QP_MBUF:
565*1b191cb5SApple OSS Distributions ASSERT(pkt->cp_ptype == QP_MBUF);
566*1b191cb5SApple OSS Distributions _removeq_mbuf(q, pkt->cp_mbuf);
567*1b191cb5SApple OSS Distributions break;
568*1b191cb5SApple OSS Distributions
569*1b191cb5SApple OSS Distributions #if SKYWALK
570*1b191cb5SApple OSS Distributions case QP_PACKET: /* XXX: Add support for Kernel packet when needed */
571*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
572*1b191cb5SApple OSS Distributions default:
573*1b191cb5SApple OSS Distributions VERIFY(0);
574*1b191cb5SApple OSS Distributions /* NOTREACHED */
575*1b191cb5SApple OSS Distributions __builtin_unreachable();
576*1b191cb5SApple OSS Distributions }
577*1b191cb5SApple OSS Distributions }
578*1b191cb5SApple OSS Distributions
579*1b191cb5SApple OSS Distributions void
_flushq(class_queue_t * q)580*1b191cb5SApple OSS Distributions _flushq(class_queue_t *q)
581*1b191cb5SApple OSS Distributions {
582*1b191cb5SApple OSS Distributions (void) _flushq_flow(q, 0, NULL, NULL);
583*1b191cb5SApple OSS Distributions }
584*1b191cb5SApple OSS Distributions
585*1b191cb5SApple OSS Distributions static inline void
_flushq_flow_mbuf(class_queue_t * q,u_int32_t flow,u_int32_t * cnt,u_int32_t * len)586*1b191cb5SApple OSS Distributions _flushq_flow_mbuf(class_queue_t *q, u_int32_t flow, u_int32_t *cnt,
587*1b191cb5SApple OSS Distributions u_int32_t *len)
588*1b191cb5SApple OSS Distributions {
589*1b191cb5SApple OSS Distributions MBUFQ_HEAD(mq_freeq) freeq;
590*1b191cb5SApple OSS Distributions struct mbuf *m, *m_tmp;
591*1b191cb5SApple OSS Distributions u_int32_t c = 0, l = 0;
592*1b191cb5SApple OSS Distributions
593*1b191cb5SApple OSS Distributions MBUFQ_INIT(&freeq);
594*1b191cb5SApple OSS Distributions
595*1b191cb5SApple OSS Distributions MBUFQ_FOREACH_SAFE(m, &qmbufq(q), m_tmp) {
596*1b191cb5SApple OSS Distributions if (flow == 0 || ((m->m_flags & M_PKTHDR) &&
597*1b191cb5SApple OSS Distributions m->m_pkthdr.pkt_flowid == flow)) {
598*1b191cb5SApple OSS Distributions /* remove it from the class queue */
599*1b191cb5SApple OSS Distributions MBUFQ_REMOVE(&qmbufq(q), m);
600*1b191cb5SApple OSS Distributions MBUFQ_NEXT(m) = NULL;
601*1b191cb5SApple OSS Distributions
602*1b191cb5SApple OSS Distributions /* and add it to the free queue */
603*1b191cb5SApple OSS Distributions MBUFQ_ENQUEUE(&freeq, m);
604*1b191cb5SApple OSS Distributions
605*1b191cb5SApple OSS Distributions l += m_length(m);
606*1b191cb5SApple OSS Distributions c++;
607*1b191cb5SApple OSS Distributions }
608*1b191cb5SApple OSS Distributions }
609*1b191cb5SApple OSS Distributions VERIFY(c == 0 || !MBUFQ_EMPTY(&freeq));
610*1b191cb5SApple OSS Distributions
611*1b191cb5SApple OSS Distributions if (c > 0) {
612*1b191cb5SApple OSS Distributions VERIFY(qlen(q) >= c);
613*1b191cb5SApple OSS Distributions qlen(q) -= c;
614*1b191cb5SApple OSS Distributions
615*1b191cb5SApple OSS Distributions /* qsize is an approximation, so adjust if necessary */
616*1b191cb5SApple OSS Distributions if (((int)qsize(q) - l) > 0) {
617*1b191cb5SApple OSS Distributions qsize(q) -= l;
618*1b191cb5SApple OSS Distributions } else if (qsize(q) != 0) {
619*1b191cb5SApple OSS Distributions qsize(q) = 0;
620*1b191cb5SApple OSS Distributions }
621*1b191cb5SApple OSS Distributions }
622*1b191cb5SApple OSS Distributions
623*1b191cb5SApple OSS Distributions if (!MBUFQ_EMPTY(&freeq)) {
624*1b191cb5SApple OSS Distributions m_freem_list(MBUFQ_FIRST(&freeq));
625*1b191cb5SApple OSS Distributions }
626*1b191cb5SApple OSS Distributions
627*1b191cb5SApple OSS Distributions if (cnt != NULL) {
628*1b191cb5SApple OSS Distributions *cnt = c;
629*1b191cb5SApple OSS Distributions }
630*1b191cb5SApple OSS Distributions if (len != NULL) {
631*1b191cb5SApple OSS Distributions *len = l;
632*1b191cb5SApple OSS Distributions }
633*1b191cb5SApple OSS Distributions }
634*1b191cb5SApple OSS Distributions
635*1b191cb5SApple OSS Distributions #if SKYWALK
636*1b191cb5SApple OSS Distributions static inline void
_flushq_flow_kpkt(class_queue_t * q,u_int32_t flow,u_int32_t * cnt,u_int32_t * len)637*1b191cb5SApple OSS Distributions _flushq_flow_kpkt(class_queue_t *q, u_int32_t flow, u_int32_t *cnt,
638*1b191cb5SApple OSS Distributions u_int32_t *len)
639*1b191cb5SApple OSS Distributions {
640*1b191cb5SApple OSS Distributions KPKTQ_HEAD(pq_freeq) freeq;
641*1b191cb5SApple OSS Distributions struct __kern_packet *p, *p_tmp;
642*1b191cb5SApple OSS Distributions u_int32_t c = 0, l = 0;
643*1b191cb5SApple OSS Distributions
644*1b191cb5SApple OSS Distributions KPKTQ_INIT(&freeq);
645*1b191cb5SApple OSS Distributions
646*1b191cb5SApple OSS Distributions KPKTQ_FOREACH_SAFE(p, &qkpktq(q), p_tmp) {
647*1b191cb5SApple OSS Distributions if (flow == 0 || p->pkt_flow_token == flow) {
648*1b191cb5SApple OSS Distributions /* remove it from the class queue */
649*1b191cb5SApple OSS Distributions KPKTQ_REMOVE(&qkpktq(q), p);
650*1b191cb5SApple OSS Distributions KPKTQ_NEXT(p) = NULL;
651*1b191cb5SApple OSS Distributions
652*1b191cb5SApple OSS Distributions /* and add it to the free queue */
653*1b191cb5SApple OSS Distributions KPKTQ_ENQUEUE(&freeq, p);
654*1b191cb5SApple OSS Distributions
655*1b191cb5SApple OSS Distributions l += p->pkt_length;
656*1b191cb5SApple OSS Distributions c++;
657*1b191cb5SApple OSS Distributions }
658*1b191cb5SApple OSS Distributions }
659*1b191cb5SApple OSS Distributions VERIFY(c == 0 || !KPKTQ_EMPTY(&freeq));
660*1b191cb5SApple OSS Distributions
661*1b191cb5SApple OSS Distributions if (c > 0) {
662*1b191cb5SApple OSS Distributions VERIFY(qlen(q) >= c);
663*1b191cb5SApple OSS Distributions qlen(q) -= c;
664*1b191cb5SApple OSS Distributions
665*1b191cb5SApple OSS Distributions /* qsize is an approximation, so adjust if necessary */
666*1b191cb5SApple OSS Distributions if (((int)qsize(q) - l) > 0) {
667*1b191cb5SApple OSS Distributions qsize(q) -= l;
668*1b191cb5SApple OSS Distributions } else if (qsize(q) != 0) {
669*1b191cb5SApple OSS Distributions qsize(q) = 0;
670*1b191cb5SApple OSS Distributions }
671*1b191cb5SApple OSS Distributions }
672*1b191cb5SApple OSS Distributions
673*1b191cb5SApple OSS Distributions if (!KPKTQ_EMPTY(&freeq)) {
674*1b191cb5SApple OSS Distributions pp_free_packet_chain(KPKTQ_FIRST(&freeq), NULL);
675*1b191cb5SApple OSS Distributions }
676*1b191cb5SApple OSS Distributions
677*1b191cb5SApple OSS Distributions if (cnt != NULL) {
678*1b191cb5SApple OSS Distributions *cnt = c;
679*1b191cb5SApple OSS Distributions }
680*1b191cb5SApple OSS Distributions if (len != NULL) {
681*1b191cb5SApple OSS Distributions *len = l;
682*1b191cb5SApple OSS Distributions }
683*1b191cb5SApple OSS Distributions }
684*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
685*1b191cb5SApple OSS Distributions
686*1b191cb5SApple OSS Distributions void
_flushq_flow(class_queue_t * q,u_int32_t flow,u_int32_t * cnt,u_int32_t * len)687*1b191cb5SApple OSS Distributions _flushq_flow(class_queue_t *q, u_int32_t flow, u_int32_t *cnt, u_int32_t *len)
688*1b191cb5SApple OSS Distributions {
689*1b191cb5SApple OSS Distributions switch (qptype(q)) {
690*1b191cb5SApple OSS Distributions case QP_MBUF:
691*1b191cb5SApple OSS Distributions _flushq_flow_mbuf(q, flow, cnt, len);
692*1b191cb5SApple OSS Distributions break;
693*1b191cb5SApple OSS Distributions
694*1b191cb5SApple OSS Distributions #if SKYWALK
695*1b191cb5SApple OSS Distributions case QP_PACKET:
696*1b191cb5SApple OSS Distributions _flushq_flow_kpkt(q, flow, cnt, len);
697*1b191cb5SApple OSS Distributions break;
698*1b191cb5SApple OSS Distributions
699*1b191cb5SApple OSS Distributions #endif /* SKYWALK */
700*1b191cb5SApple OSS Distributions default:
701*1b191cb5SApple OSS Distributions VERIFY(0);
702*1b191cb5SApple OSS Distributions /* NOTREACHED */
703*1b191cb5SApple OSS Distributions __builtin_unreachable();
704*1b191cb5SApple OSS Distributions }
705*1b191cb5SApple OSS Distributions }
706