xref: /xnu-8020.140.41/bsd/net/classq/classq_fq_codel.c (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * Copyright (c) 2016-2021 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions  *
4*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions  *
6*27b03b36SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions  *
15*27b03b36SApple OSS Distributions  * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions  *
18*27b03b36SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions  * limitations under the License.
25*27b03b36SApple OSS Distributions  *
26*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions  */
28*27b03b36SApple OSS Distributions 
29*27b03b36SApple OSS Distributions #include <sys/cdefs.h>
30*27b03b36SApple OSS Distributions #include <sys/param.h>
31*27b03b36SApple OSS Distributions #include <sys/mbuf.h>
32*27b03b36SApple OSS Distributions #include <sys/socket.h>
33*27b03b36SApple OSS Distributions #include <sys/sockio.h>
34*27b03b36SApple OSS Distributions #include <sys/systm.h>
35*27b03b36SApple OSS Distributions #include <sys/sysctl.h>
36*27b03b36SApple OSS Distributions #include <sys/syslog.h>
37*27b03b36SApple OSS Distributions #include <sys/proc.h>
38*27b03b36SApple OSS Distributions #include <sys/errno.h>
39*27b03b36SApple OSS Distributions #include <sys/kernel.h>
40*27b03b36SApple OSS Distributions #include <sys/kauth.h>
41*27b03b36SApple OSS Distributions #include <sys/sdt.h>
42*27b03b36SApple OSS Distributions #include <kern/zalloc.h>
43*27b03b36SApple OSS Distributions #include <netinet/in.h>
44*27b03b36SApple OSS Distributions 
45*27b03b36SApple OSS Distributions #include <net/classq/classq.h>
46*27b03b36SApple OSS Distributions #include <net/classq/if_classq.h>
47*27b03b36SApple OSS Distributions #include <net/pktsched/pktsched.h>
48*27b03b36SApple OSS Distributions #include <net/pktsched/pktsched_fq_codel.h>
49*27b03b36SApple OSS Distributions #include <net/classq/classq_fq_codel.h>
50*27b03b36SApple OSS Distributions 
51*27b03b36SApple OSS Distributions #include <netinet/tcp_var.h>
52*27b03b36SApple OSS Distributions 
53*27b03b36SApple OSS Distributions static uint32_t flowq_size;                     /* size of flowq */
54*27b03b36SApple OSS Distributions static struct mcache *flowq_cache = NULL;       /* mcache for flowq */
55*27b03b36SApple OSS Distributions 
56*27b03b36SApple OSS Distributions #define FQ_ZONE_MAX     (32 * 1024)     /* across all interfaces */
57*27b03b36SApple OSS Distributions 
58*27b03b36SApple OSS Distributions #define DTYPE_NODROP    0       /* no drop */
59*27b03b36SApple OSS Distributions #define DTYPE_FORCED    1       /* a "forced" drop */
60*27b03b36SApple OSS Distributions #define DTYPE_EARLY     2       /* an "unforced" (early) drop */
61*27b03b36SApple OSS Distributions 
62*27b03b36SApple OSS Distributions void
fq_codel_init(void)63*27b03b36SApple OSS Distributions fq_codel_init(void)
64*27b03b36SApple OSS Distributions {
65*27b03b36SApple OSS Distributions 	if (flowq_cache != NULL) {
66*27b03b36SApple OSS Distributions 		return;
67*27b03b36SApple OSS Distributions 	}
68*27b03b36SApple OSS Distributions 
69*27b03b36SApple OSS Distributions 	flowq_size = sizeof(fq_t);
70*27b03b36SApple OSS Distributions 	flowq_cache = mcache_create("fq.flowq", flowq_size, sizeof(uint64_t),
71*27b03b36SApple OSS Distributions 	    0, MCR_SLEEP);
72*27b03b36SApple OSS Distributions 	if (flowq_cache == NULL) {
73*27b03b36SApple OSS Distributions 		panic("%s: failed to allocate flowq_cache", __func__);
74*27b03b36SApple OSS Distributions 		/* NOTREACHED */
75*27b03b36SApple OSS Distributions 		__builtin_unreachable();
76*27b03b36SApple OSS Distributions 	}
77*27b03b36SApple OSS Distributions }
78*27b03b36SApple OSS Distributions 
79*27b03b36SApple OSS Distributions void
fq_codel_reap_caches(boolean_t purge)80*27b03b36SApple OSS Distributions fq_codel_reap_caches(boolean_t purge)
81*27b03b36SApple OSS Distributions {
82*27b03b36SApple OSS Distributions 	mcache_reap_now(flowq_cache, purge);
83*27b03b36SApple OSS Distributions }
84*27b03b36SApple OSS Distributions 
85*27b03b36SApple OSS Distributions fq_t *
fq_alloc(classq_pkt_type_t ptype)86*27b03b36SApple OSS Distributions fq_alloc(classq_pkt_type_t ptype)
87*27b03b36SApple OSS Distributions {
88*27b03b36SApple OSS Distributions 	fq_t *fq = NULL;
89*27b03b36SApple OSS Distributions 	fq = mcache_alloc(flowq_cache, MCR_SLEEP);
90*27b03b36SApple OSS Distributions 	if (fq == NULL) {
91*27b03b36SApple OSS Distributions 		log(LOG_ERR, "%s: unable to allocate from flowq_cache\n", __func__);
92*27b03b36SApple OSS Distributions 		return NULL;
93*27b03b36SApple OSS Distributions 	}
94*27b03b36SApple OSS Distributions 
95*27b03b36SApple OSS Distributions 	bzero(fq, flowq_size);
96*27b03b36SApple OSS Distributions 	fq->fq_ptype = ptype;
97*27b03b36SApple OSS Distributions 	if (ptype == QP_MBUF) {
98*27b03b36SApple OSS Distributions 		MBUFQ_INIT(&fq->fq_mbufq);
99*27b03b36SApple OSS Distributions 	}
100*27b03b36SApple OSS Distributions #if SKYWALK
101*27b03b36SApple OSS Distributions 	else {
102*27b03b36SApple OSS Distributions 		VERIFY(ptype == QP_PACKET);
103*27b03b36SApple OSS Distributions 		KPKTQ_INIT(&fq->fq_kpktq);
104*27b03b36SApple OSS Distributions 	}
105*27b03b36SApple OSS Distributions #endif /* SKYWALK */
106*27b03b36SApple OSS Distributions 	CLASSQ_PKT_INIT(&fq->fq_dq_head);
107*27b03b36SApple OSS Distributions 	CLASSQ_PKT_INIT(&fq->fq_dq_tail);
108*27b03b36SApple OSS Distributions 	fq->fq_in_dqlist = false;
109*27b03b36SApple OSS Distributions 	return fq;
110*27b03b36SApple OSS Distributions }
111*27b03b36SApple OSS Distributions 
112*27b03b36SApple OSS Distributions void
fq_destroy(fq_t * fq)113*27b03b36SApple OSS Distributions fq_destroy(fq_t *fq)
114*27b03b36SApple OSS Distributions {
115*27b03b36SApple OSS Distributions 	VERIFY(fq->fq_flags & FQF_DESTROYED);
116*27b03b36SApple OSS Distributions 	VERIFY(fq_empty(fq));
117*27b03b36SApple OSS Distributions 	VERIFY(!(fq->fq_flags & (FQF_NEW_FLOW | FQF_OLD_FLOW)));
118*27b03b36SApple OSS Distributions 	VERIFY(fq->fq_bytes == 0);
119*27b03b36SApple OSS Distributions 	mcache_free(flowq_cache, fq);
120*27b03b36SApple OSS Distributions }
121*27b03b36SApple OSS Distributions 
122*27b03b36SApple OSS Distributions static inline void
fq_detect_dequeue_stall(fq_if_t * fqs,fq_t * flowq,fq_if_classq_t * fq_cl,u_int64_t * now)123*27b03b36SApple OSS Distributions fq_detect_dequeue_stall(fq_if_t *fqs, fq_t *flowq, fq_if_classq_t *fq_cl,
124*27b03b36SApple OSS Distributions     u_int64_t *now)
125*27b03b36SApple OSS Distributions {
126*27b03b36SApple OSS Distributions 	u_int64_t maxgetqtime;
127*27b03b36SApple OSS Distributions 	if (FQ_IS_DELAYHIGH(flowq) || flowq->fq_getqtime == 0 ||
128*27b03b36SApple OSS Distributions 	    fq_empty(flowq) ||
129*27b03b36SApple OSS Distributions 	    flowq->fq_bytes < FQ_MIN_FC_THRESHOLD_BYTES) {
130*27b03b36SApple OSS Distributions 		return;
131*27b03b36SApple OSS Distributions 	}
132*27b03b36SApple OSS Distributions 	maxgetqtime = flowq->fq_getqtime + fqs->fqs_update_interval;
133*27b03b36SApple OSS Distributions 	if ((*now) > maxgetqtime) {
134*27b03b36SApple OSS Distributions 		/*
135*27b03b36SApple OSS Distributions 		 * there was no dequeue in an update interval worth of
136*27b03b36SApple OSS Distributions 		 * time. It means that the queue is stalled.
137*27b03b36SApple OSS Distributions 		 */
138*27b03b36SApple OSS Distributions 		FQ_SET_DELAY_HIGH(flowq);
139*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_dequeue_stall++;
140*27b03b36SApple OSS Distributions 		os_log_error(OS_LOG_DEFAULT, "%s: dequeue stall num: %d, "
141*27b03b36SApple OSS Distributions 		    "scidx: %d, flow: 0x%x, iface: %s", __func__,
142*27b03b36SApple OSS Distributions 		    fq_cl->fcl_stat.fcl_dequeue_stall, flowq->fq_sc_index,
143*27b03b36SApple OSS Distributions 		    flowq->fq_flowhash, if_name(fqs->fqs_ifq->ifcq_ifp));
144*27b03b36SApple OSS Distributions 	}
145*27b03b36SApple OSS Distributions }
146*27b03b36SApple OSS Distributions 
147*27b03b36SApple OSS Distributions void
fq_head_drop(fq_if_t * fqs,fq_t * fq)148*27b03b36SApple OSS Distributions fq_head_drop(fq_if_t *fqs, fq_t *fq)
149*27b03b36SApple OSS Distributions {
150*27b03b36SApple OSS Distributions 	pktsched_pkt_t pkt;
151*27b03b36SApple OSS Distributions 	volatile uint32_t *pkt_flags;
152*27b03b36SApple OSS Distributions 	uint64_t *pkt_timestamp;
153*27b03b36SApple OSS Distributions 	struct ifclassq *ifq = fqs->fqs_ifq;
154*27b03b36SApple OSS Distributions 
155*27b03b36SApple OSS Distributions 	_PKTSCHED_PKT_INIT(&pkt);
156*27b03b36SApple OSS Distributions 	fq_getq_flow_internal(fqs, fq, &pkt);
157*27b03b36SApple OSS Distributions 	if (pkt.pktsched_pkt_mbuf == NULL) {
158*27b03b36SApple OSS Distributions 		return;
159*27b03b36SApple OSS Distributions 	}
160*27b03b36SApple OSS Distributions 
161*27b03b36SApple OSS Distributions 	pktsched_get_pkt_vars(&pkt, &pkt_flags, &pkt_timestamp, NULL, NULL,
162*27b03b36SApple OSS Distributions 	    NULL, NULL);
163*27b03b36SApple OSS Distributions 
164*27b03b36SApple OSS Distributions 	*pkt_timestamp = 0;
165*27b03b36SApple OSS Distributions 	switch (pkt.pktsched_ptype) {
166*27b03b36SApple OSS Distributions 	case QP_MBUF:
167*27b03b36SApple OSS Distributions 		*pkt_flags &= ~PKTF_PRIV_GUARDED;
168*27b03b36SApple OSS Distributions 		break;
169*27b03b36SApple OSS Distributions #if SKYWALK
170*27b03b36SApple OSS Distributions 	case QP_PACKET:
171*27b03b36SApple OSS Distributions 		/* sanity check */
172*27b03b36SApple OSS Distributions 		ASSERT((*pkt_flags & ~PKT_F_COMMON_MASK) == 0);
173*27b03b36SApple OSS Distributions 		break;
174*27b03b36SApple OSS Distributions #endif /* SKYWALK */
175*27b03b36SApple OSS Distributions 	default:
176*27b03b36SApple OSS Distributions 		VERIFY(0);
177*27b03b36SApple OSS Distributions 		/* NOTREACHED */
178*27b03b36SApple OSS Distributions 		__builtin_unreachable();
179*27b03b36SApple OSS Distributions 	}
180*27b03b36SApple OSS Distributions 
181*27b03b36SApple OSS Distributions 	IFCQ_DROP_ADD(ifq, 1, pktsched_get_pkt_len(&pkt));
182*27b03b36SApple OSS Distributions 	IFCQ_CONVERT_LOCK(ifq);
183*27b03b36SApple OSS Distributions 	pktsched_free_pkt(&pkt);
184*27b03b36SApple OSS Distributions }
185*27b03b36SApple OSS Distributions 
186*27b03b36SApple OSS Distributions 
187*27b03b36SApple OSS Distributions static int
fq_compressor(fq_if_t * fqs,fq_t * fq,fq_if_classq_t * fq_cl,pktsched_pkt_t * pkt)188*27b03b36SApple OSS Distributions fq_compressor(fq_if_t *fqs, fq_t *fq, fq_if_classq_t *fq_cl,
189*27b03b36SApple OSS Distributions     pktsched_pkt_t *pkt)
190*27b03b36SApple OSS Distributions {
191*27b03b36SApple OSS Distributions 	classq_pkt_type_t ptype = fq->fq_ptype;
192*27b03b36SApple OSS Distributions 	uint32_t comp_gencnt = 0;
193*27b03b36SApple OSS Distributions 	uint64_t *pkt_timestamp;
194*27b03b36SApple OSS Distributions 	uint64_t old_timestamp = 0;
195*27b03b36SApple OSS Distributions 	uint32_t old_pktlen = 0;
196*27b03b36SApple OSS Distributions 	struct ifclassq *ifq = fqs->fqs_ifq;
197*27b03b36SApple OSS Distributions 
198*27b03b36SApple OSS Distributions 	if (__improbable(!tcp_do_ack_compression)) {
199*27b03b36SApple OSS Distributions 		return 0;
200*27b03b36SApple OSS Distributions 	}
201*27b03b36SApple OSS Distributions 
202*27b03b36SApple OSS Distributions 	pktsched_get_pkt_vars(pkt, NULL, &pkt_timestamp, NULL, NULL, NULL,
203*27b03b36SApple OSS Distributions 	    &comp_gencnt);
204*27b03b36SApple OSS Distributions 
205*27b03b36SApple OSS Distributions 	if (comp_gencnt == 0) {
206*27b03b36SApple OSS Distributions 		return 0;
207*27b03b36SApple OSS Distributions 	}
208*27b03b36SApple OSS Distributions 
209*27b03b36SApple OSS Distributions 	fq_cl->fcl_stat.fcl_pkts_compressible++;
210*27b03b36SApple OSS Distributions 
211*27b03b36SApple OSS Distributions 	if (fq_empty(fq)) {
212*27b03b36SApple OSS Distributions 		return 0;
213*27b03b36SApple OSS Distributions 	}
214*27b03b36SApple OSS Distributions 
215*27b03b36SApple OSS Distributions 	if (ptype == QP_MBUF) {
216*27b03b36SApple OSS Distributions 		struct mbuf *m = MBUFQ_LAST(&fq->fq_mbufq);
217*27b03b36SApple OSS Distributions 
218*27b03b36SApple OSS Distributions 		if (comp_gencnt != m->m_pkthdr.comp_gencnt) {
219*27b03b36SApple OSS Distributions 			return 0;
220*27b03b36SApple OSS Distributions 		}
221*27b03b36SApple OSS Distributions 
222*27b03b36SApple OSS Distributions 		/* If we got until here, we should merge/replace the segment */
223*27b03b36SApple OSS Distributions 		MBUFQ_REMOVE(&fq->fq_mbufq, m);
224*27b03b36SApple OSS Distributions 		old_pktlen = m_pktlen(m);
225*27b03b36SApple OSS Distributions 		old_timestamp = m->m_pkthdr.pkt_timestamp;
226*27b03b36SApple OSS Distributions 
227*27b03b36SApple OSS Distributions 		IFCQ_CONVERT_LOCK(fqs->fqs_ifq);
228*27b03b36SApple OSS Distributions 		m_freem(m);
229*27b03b36SApple OSS Distributions 	}
230*27b03b36SApple OSS Distributions #if SKYWALK
231*27b03b36SApple OSS Distributions 	else {
232*27b03b36SApple OSS Distributions 		struct __kern_packet *kpkt = KPKTQ_LAST(&fq->fq_kpktq);
233*27b03b36SApple OSS Distributions 
234*27b03b36SApple OSS Distributions 		if (comp_gencnt != kpkt->pkt_comp_gencnt) {
235*27b03b36SApple OSS Distributions 			return 0;
236*27b03b36SApple OSS Distributions 		}
237*27b03b36SApple OSS Distributions 
238*27b03b36SApple OSS Distributions 		/* If we got until here, we should merge/replace the segment */
239*27b03b36SApple OSS Distributions 		KPKTQ_REMOVE(&fq->fq_kpktq, kpkt);
240*27b03b36SApple OSS Distributions 		old_pktlen = kpkt->pkt_length;
241*27b03b36SApple OSS Distributions 		old_timestamp = kpkt->pkt_timestamp;
242*27b03b36SApple OSS Distributions 
243*27b03b36SApple OSS Distributions 		IFCQ_CONVERT_LOCK(fqs->fqs_ifq);
244*27b03b36SApple OSS Distributions 		pp_free_packet(*(struct kern_pbufpool **)(uintptr_t)&
245*27b03b36SApple OSS Distributions 		    (((struct __kern_quantum *)kpkt)->qum_pp),
246*27b03b36SApple OSS Distributions 		    (uint64_t)kpkt);
247*27b03b36SApple OSS Distributions 	}
248*27b03b36SApple OSS Distributions #endif /* SKYWALK */
249*27b03b36SApple OSS Distributions 
250*27b03b36SApple OSS Distributions 	fq->fq_bytes -= old_pktlen;
251*27b03b36SApple OSS Distributions 	fq_cl->fcl_stat.fcl_byte_cnt -= old_pktlen;
252*27b03b36SApple OSS Distributions 	fq_cl->fcl_stat.fcl_pkt_cnt--;
253*27b03b36SApple OSS Distributions 	IFCQ_DEC_LEN(ifq);
254*27b03b36SApple OSS Distributions 	IFCQ_DEC_BYTES(ifq, old_pktlen);
255*27b03b36SApple OSS Distributions 
256*27b03b36SApple OSS Distributions 	*pkt_timestamp = old_timestamp;
257*27b03b36SApple OSS Distributions 
258*27b03b36SApple OSS Distributions 	return CLASSQEQ_COMPRESSED;
259*27b03b36SApple OSS Distributions }
260*27b03b36SApple OSS Distributions 
261*27b03b36SApple OSS Distributions int
fq_addq(fq_if_t * fqs,pktsched_pkt_t * pkt,fq_if_classq_t * fq_cl)262*27b03b36SApple OSS Distributions fq_addq(fq_if_t *fqs, pktsched_pkt_t *pkt, fq_if_classq_t *fq_cl)
263*27b03b36SApple OSS Distributions {
264*27b03b36SApple OSS Distributions 	int droptype = DTYPE_NODROP, fc_adv = 0, ret = CLASSQEQ_SUCCESS;
265*27b03b36SApple OSS Distributions 	u_int64_t now;
266*27b03b36SApple OSS Distributions 	fq_t *fq = NULL;
267*27b03b36SApple OSS Distributions 	uint64_t *pkt_timestamp;
268*27b03b36SApple OSS Distributions 	volatile uint32_t *pkt_flags;
269*27b03b36SApple OSS Distributions 	uint32_t pkt_flowid, cnt;
270*27b03b36SApple OSS Distributions 	uint8_t pkt_proto, pkt_flowsrc;
271*27b03b36SApple OSS Distributions 
272*27b03b36SApple OSS Distributions 	cnt = pkt->pktsched_pcnt;
273*27b03b36SApple OSS Distributions 	pktsched_get_pkt_vars(pkt, &pkt_flags, &pkt_timestamp, &pkt_flowid,
274*27b03b36SApple OSS Distributions 	    &pkt_flowsrc, &pkt_proto, NULL);
275*27b03b36SApple OSS Distributions 
276*27b03b36SApple OSS Distributions 	/*
277*27b03b36SApple OSS Distributions 	 * XXX Not walking the chain to set this flag on every packet.
278*27b03b36SApple OSS Distributions 	 * This flag is only used for debugging. Nothing is affected if it's
279*27b03b36SApple OSS Distributions 	 * not set.
280*27b03b36SApple OSS Distributions 	 */
281*27b03b36SApple OSS Distributions 	switch (pkt->pktsched_ptype) {
282*27b03b36SApple OSS Distributions 	case QP_MBUF:
283*27b03b36SApple OSS Distributions 		/* See comments in <rdar://problem/14040693> */
284*27b03b36SApple OSS Distributions 		VERIFY(!(*pkt_flags & PKTF_PRIV_GUARDED));
285*27b03b36SApple OSS Distributions 		*pkt_flags |= PKTF_PRIV_GUARDED;
286*27b03b36SApple OSS Distributions 		break;
287*27b03b36SApple OSS Distributions #if SKYWALK
288*27b03b36SApple OSS Distributions 	case QP_PACKET:
289*27b03b36SApple OSS Distributions 		/* sanity check */
290*27b03b36SApple OSS Distributions 		ASSERT((*pkt_flags & ~PKT_F_COMMON_MASK) == 0);
291*27b03b36SApple OSS Distributions 		break;
292*27b03b36SApple OSS Distributions #endif /* SKYWALK */
293*27b03b36SApple OSS Distributions 	default:
294*27b03b36SApple OSS Distributions 		VERIFY(0);
295*27b03b36SApple OSS Distributions 		/* NOTREACHED */
296*27b03b36SApple OSS Distributions 		__builtin_unreachable();
297*27b03b36SApple OSS Distributions 	}
298*27b03b36SApple OSS Distributions 
299*27b03b36SApple OSS Distributions 	/*
300*27b03b36SApple OSS Distributions 	 * Timestamps for every packet must be set prior to entering this path.
301*27b03b36SApple OSS Distributions 	 */
302*27b03b36SApple OSS Distributions 	now = *pkt_timestamp;
303*27b03b36SApple OSS Distributions 	ASSERT(now > 0);
304*27b03b36SApple OSS Distributions 
305*27b03b36SApple OSS Distributions 	/* find the flowq for this packet */
306*27b03b36SApple OSS Distributions 	fq = fq_if_hash_pkt(fqs, pkt_flowid, pktsched_get_pkt_svc(pkt),
307*27b03b36SApple OSS Distributions 	    now, TRUE, pkt->pktsched_ptype);
308*27b03b36SApple OSS Distributions 	if (__improbable(fq == NULL)) {
309*27b03b36SApple OSS Distributions 		DTRACE_IP1(memfail__drop, fq_if_t *, fqs);
310*27b03b36SApple OSS Distributions 		/* drop the packet if we could not allocate a flow queue */
311*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_drop_memfailure += cnt;
312*27b03b36SApple OSS Distributions 		return CLASSQEQ_DROP;
313*27b03b36SApple OSS Distributions 	}
314*27b03b36SApple OSS Distributions 	VERIFY(fq->fq_ptype == pkt->pktsched_ptype);
315*27b03b36SApple OSS Distributions 
316*27b03b36SApple OSS Distributions 	fq_detect_dequeue_stall(fqs, fq, fq_cl, &now);
317*27b03b36SApple OSS Distributions 
318*27b03b36SApple OSS Distributions 	if (__improbable(FQ_IS_DELAYHIGH(fq) || FQ_IS_OVERWHELMING(fq))) {
319*27b03b36SApple OSS Distributions 		if ((fq->fq_flags & FQF_FLOWCTL_CAPABLE) &&
320*27b03b36SApple OSS Distributions 		    (*pkt_flags & PKTF_FLOW_ADV)) {
321*27b03b36SApple OSS Distributions 			fc_adv = 1;
322*27b03b36SApple OSS Distributions 			/*
323*27b03b36SApple OSS Distributions 			 * If the flow is suspended or it is not
324*27b03b36SApple OSS Distributions 			 * TCP/QUIC, drop the chain.
325*27b03b36SApple OSS Distributions 			 */
326*27b03b36SApple OSS Distributions 			if ((pkt_proto != IPPROTO_TCP) &&
327*27b03b36SApple OSS Distributions 			    (pkt_proto != IPPROTO_QUIC)) {
328*27b03b36SApple OSS Distributions 				droptype = DTYPE_EARLY;
329*27b03b36SApple OSS Distributions 				fq_cl->fcl_stat.fcl_drop_early += cnt;
330*27b03b36SApple OSS Distributions 				IFCQ_DROP_ADD(fqs->fqs_ifq, cnt, pktsched_get_pkt_len(pkt));
331*27b03b36SApple OSS Distributions 			}
332*27b03b36SApple OSS Distributions 			DTRACE_IP6(flow__adv, fq_if_t *, fqs,
333*27b03b36SApple OSS Distributions 			    fq_if_classq_t *, fq_cl, fq_t *, fq,
334*27b03b36SApple OSS Distributions 			    int, droptype, pktsched_pkt_t *, pkt,
335*27b03b36SApple OSS Distributions 			    uint32_t, cnt);
336*27b03b36SApple OSS Distributions 		} else {
337*27b03b36SApple OSS Distributions 			/*
338*27b03b36SApple OSS Distributions 			 * Need to drop packets to make room for the new
339*27b03b36SApple OSS Distributions 			 * ones. Try to drop from the head of the queue
340*27b03b36SApple OSS Distributions 			 * instead of the latest packets.
341*27b03b36SApple OSS Distributions 			 */
342*27b03b36SApple OSS Distributions 			if (!fq_empty(fq)) {
343*27b03b36SApple OSS Distributions 				uint32_t i;
344*27b03b36SApple OSS Distributions 
345*27b03b36SApple OSS Distributions 				for (i = 0; i < cnt; i++) {
346*27b03b36SApple OSS Distributions 					fq_head_drop(fqs, fq);
347*27b03b36SApple OSS Distributions 				}
348*27b03b36SApple OSS Distributions 				droptype = DTYPE_NODROP;
349*27b03b36SApple OSS Distributions 			} else {
350*27b03b36SApple OSS Distributions 				droptype = DTYPE_EARLY;
351*27b03b36SApple OSS Distributions 			}
352*27b03b36SApple OSS Distributions 			fq_cl->fcl_stat.fcl_drop_early += cnt;
353*27b03b36SApple OSS Distributions 
354*27b03b36SApple OSS Distributions 			DTRACE_IP6(no__flow__adv, fq_if_t *, fqs,
355*27b03b36SApple OSS Distributions 			    fq_if_classq_t *, fq_cl, fq_t *, fq,
356*27b03b36SApple OSS Distributions 			    int, droptype, pktsched_pkt_t *, pkt,
357*27b03b36SApple OSS Distributions 			    uint32_t, cnt);
358*27b03b36SApple OSS Distributions 		}
359*27b03b36SApple OSS Distributions 	}
360*27b03b36SApple OSS Distributions 
361*27b03b36SApple OSS Distributions 	/* Set the return code correctly */
362*27b03b36SApple OSS Distributions 	if (__improbable(fc_adv == 1 && droptype != DTYPE_FORCED)) {
363*27b03b36SApple OSS Distributions 		if (fq_if_add_fcentry(fqs, pkt, pkt_flowsrc, fq, fq_cl)) {
364*27b03b36SApple OSS Distributions 			fq->fq_flags |= FQF_FLOWCTL_ON;
365*27b03b36SApple OSS Distributions 			/* deliver flow control advisory error */
366*27b03b36SApple OSS Distributions 			if (droptype == DTYPE_NODROP) {
367*27b03b36SApple OSS Distributions 				ret = CLASSQEQ_SUCCESS_FC;
368*27b03b36SApple OSS Distributions 			} else {
369*27b03b36SApple OSS Distributions 				/* dropped due to flow control */
370*27b03b36SApple OSS Distributions 				ret = CLASSQEQ_DROP_FC;
371*27b03b36SApple OSS Distributions 			}
372*27b03b36SApple OSS Distributions 		} else {
373*27b03b36SApple OSS Distributions 			/*
374*27b03b36SApple OSS Distributions 			 * if we could not flow control the flow, it is
375*27b03b36SApple OSS Distributions 			 * better to drop
376*27b03b36SApple OSS Distributions 			 */
377*27b03b36SApple OSS Distributions 			droptype = DTYPE_FORCED;
378*27b03b36SApple OSS Distributions 			ret = CLASSQEQ_DROP_FC;
379*27b03b36SApple OSS Distributions 			fq_cl->fcl_stat.fcl_flow_control_fail++;
380*27b03b36SApple OSS Distributions 		}
381*27b03b36SApple OSS Distributions 		DTRACE_IP3(fc__ret, fq_if_t *, fqs, int, droptype, int, ret);
382*27b03b36SApple OSS Distributions 	}
383*27b03b36SApple OSS Distributions 
384*27b03b36SApple OSS Distributions 	/*
385*27b03b36SApple OSS Distributions 	 * If the queue length hits the queue limit, drop a chain with the
386*27b03b36SApple OSS Distributions 	 * same number of packets from the front of the queue for a flow with
387*27b03b36SApple OSS Distributions 	 * maximum number of bytes. This will penalize heavy and unresponsive
388*27b03b36SApple OSS Distributions 	 * flows. It will also avoid a tail drop.
389*27b03b36SApple OSS Distributions 	 */
390*27b03b36SApple OSS Distributions 	if (__improbable(droptype == DTYPE_NODROP &&
391*27b03b36SApple OSS Distributions 	    fq_if_at_drop_limit(fqs))) {
392*27b03b36SApple OSS Distributions 		uint32_t i;
393*27b03b36SApple OSS Distributions 
394*27b03b36SApple OSS Distributions 		if (fqs->fqs_large_flow == fq) {
395*27b03b36SApple OSS Distributions 			/*
396*27b03b36SApple OSS Distributions 			 * Drop from the head of the current fq. Since a
397*27b03b36SApple OSS Distributions 			 * new packet will be added to the tail, it is ok
398*27b03b36SApple OSS Distributions 			 * to leave fq in place.
399*27b03b36SApple OSS Distributions 			 */
400*27b03b36SApple OSS Distributions 			DTRACE_IP5(large__flow, fq_if_t *, fqs,
401*27b03b36SApple OSS Distributions 			    fq_if_classq_t *, fq_cl, fq_t *, fq,
402*27b03b36SApple OSS Distributions 			    pktsched_pkt_t *, pkt, uint32_t, cnt);
403*27b03b36SApple OSS Distributions 
404*27b03b36SApple OSS Distributions 			for (i = 0; i < cnt; i++) {
405*27b03b36SApple OSS Distributions 				fq_head_drop(fqs, fq);
406*27b03b36SApple OSS Distributions 			}
407*27b03b36SApple OSS Distributions 			fq_cl->fcl_stat.fcl_drop_overflow += cnt;
408*27b03b36SApple OSS Distributions 
409*27b03b36SApple OSS Distributions 			/*
410*27b03b36SApple OSS Distributions 			 * TCP and QUIC will react to the loss of those head dropped pkts
411*27b03b36SApple OSS Distributions 			 * and adjust send rate.
412*27b03b36SApple OSS Distributions 			 */
413*27b03b36SApple OSS Distributions 			if ((fq->fq_flags & FQF_FLOWCTL_CAPABLE) &&
414*27b03b36SApple OSS Distributions 			    (*pkt_flags & PKTF_FLOW_ADV) &&
415*27b03b36SApple OSS Distributions 			    (pkt_proto != IPPROTO_TCP) &&
416*27b03b36SApple OSS Distributions 			    (pkt_proto != IPPROTO_QUIC)) {
417*27b03b36SApple OSS Distributions 				if (fq_if_add_fcentry(fqs, pkt, pkt_flowsrc, fq, fq_cl)) {
418*27b03b36SApple OSS Distributions 					fq->fq_flags |= FQF_FLOWCTL_ON;
419*27b03b36SApple OSS Distributions 					FQ_SET_OVERWHELMING(fq);
420*27b03b36SApple OSS Distributions 					fq_cl->fcl_stat.fcl_overwhelming++;
421*27b03b36SApple OSS Distributions 					/* deliver flow control advisory error */
422*27b03b36SApple OSS Distributions 					ret = CLASSQEQ_SUCCESS_FC;
423*27b03b36SApple OSS Distributions 				}
424*27b03b36SApple OSS Distributions 			}
425*27b03b36SApple OSS Distributions 		} else {
426*27b03b36SApple OSS Distributions 			if (fqs->fqs_large_flow == NULL) {
427*27b03b36SApple OSS Distributions 				droptype = DTYPE_FORCED;
428*27b03b36SApple OSS Distributions 				fq_cl->fcl_stat.fcl_drop_overflow += cnt;
429*27b03b36SApple OSS Distributions 				ret = CLASSQEQ_DROP;
430*27b03b36SApple OSS Distributions 
431*27b03b36SApple OSS Distributions 				DTRACE_IP5(no__large__flow, fq_if_t *, fqs,
432*27b03b36SApple OSS Distributions 				    fq_if_classq_t *, fq_cl, fq_t *, fq,
433*27b03b36SApple OSS Distributions 				    pktsched_pkt_t *, pkt, uint32_t, cnt);
434*27b03b36SApple OSS Distributions 
435*27b03b36SApple OSS Distributions 				/*
436*27b03b36SApple OSS Distributions 				 * if this fq was freshly created and there
437*27b03b36SApple OSS Distributions 				 * is nothing to enqueue, free it
438*27b03b36SApple OSS Distributions 				 */
439*27b03b36SApple OSS Distributions 				if (fq_empty(fq) && !(fq->fq_flags &
440*27b03b36SApple OSS Distributions 				    (FQF_NEW_FLOW | FQF_OLD_FLOW))) {
441*27b03b36SApple OSS Distributions 					fq_if_destroy_flow(fqs, fq_cl, fq, true);
442*27b03b36SApple OSS Distributions 					fq = NULL;
443*27b03b36SApple OSS Distributions 				}
444*27b03b36SApple OSS Distributions 			} else {
445*27b03b36SApple OSS Distributions 				DTRACE_IP5(different__large__flow,
446*27b03b36SApple OSS Distributions 				    fq_if_t *, fqs, fq_if_classq_t *, fq_cl,
447*27b03b36SApple OSS Distributions 				    fq_t *, fq, pktsched_pkt_t *, pkt,
448*27b03b36SApple OSS Distributions 				    uint32_t, cnt);
449*27b03b36SApple OSS Distributions 
450*27b03b36SApple OSS Distributions 				for (i = 0; i < cnt; i++) {
451*27b03b36SApple OSS Distributions 					fq_if_drop_packet(fqs);
452*27b03b36SApple OSS Distributions 				}
453*27b03b36SApple OSS Distributions 			}
454*27b03b36SApple OSS Distributions 		}
455*27b03b36SApple OSS Distributions 	}
456*27b03b36SApple OSS Distributions 
457*27b03b36SApple OSS Distributions 	if (__probable(droptype == DTYPE_NODROP)) {
458*27b03b36SApple OSS Distributions 		uint32_t chain_len = pktsched_get_pkt_len(pkt);
459*27b03b36SApple OSS Distributions 
460*27b03b36SApple OSS Distributions 		/*
461*27b03b36SApple OSS Distributions 		 * We do not compress if we are enqueuing a chain.
462*27b03b36SApple OSS Distributions 		 * Traversing the chain to look for acks would defeat the
463*27b03b36SApple OSS Distributions 		 * purpose of batch enqueueing.
464*27b03b36SApple OSS Distributions 		 */
465*27b03b36SApple OSS Distributions 		if (cnt == 1) {
466*27b03b36SApple OSS Distributions 			ret = fq_compressor(fqs, fq, fq_cl, pkt);
467*27b03b36SApple OSS Distributions 			if (ret != CLASSQEQ_COMPRESSED) {
468*27b03b36SApple OSS Distributions 				ret = CLASSQEQ_SUCCESS;
469*27b03b36SApple OSS Distributions 			} else {
470*27b03b36SApple OSS Distributions 				fq_cl->fcl_stat.fcl_pkts_compressed++;
471*27b03b36SApple OSS Distributions 			}
472*27b03b36SApple OSS Distributions 		}
473*27b03b36SApple OSS Distributions 		DTRACE_IP5(fq_enqueue, fq_if_t *, fqs, fq_if_classq_t *, fq_cl,
474*27b03b36SApple OSS Distributions 		    fq_t *, fq, pktsched_pkt_t *, pkt, uint32_t, cnt);
475*27b03b36SApple OSS Distributions 		fq_enqueue(fq, pkt->pktsched_pkt, pkt->pktsched_tail, cnt);
476*27b03b36SApple OSS Distributions 
477*27b03b36SApple OSS Distributions 		fq->fq_bytes += chain_len;
478*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_byte_cnt += chain_len;
479*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_pkt_cnt += cnt;
480*27b03b36SApple OSS Distributions 
481*27b03b36SApple OSS Distributions 		/*
482*27b03b36SApple OSS Distributions 		 * check if this queue will qualify to be the next
483*27b03b36SApple OSS Distributions 		 * victim queue
484*27b03b36SApple OSS Distributions 		 */
485*27b03b36SApple OSS Distributions 		fq_if_is_flow_heavy(fqs, fq);
486*27b03b36SApple OSS Distributions 	} else {
487*27b03b36SApple OSS Distributions 		DTRACE_IP3(fq_drop, fq_if_t *, fqs, int, droptype, int, ret);
488*27b03b36SApple OSS Distributions 		return (ret != CLASSQEQ_SUCCESS) ? ret : CLASSQEQ_DROP;
489*27b03b36SApple OSS Distributions 	}
490*27b03b36SApple OSS Distributions 
491*27b03b36SApple OSS Distributions 	/*
492*27b03b36SApple OSS Distributions 	 * If the queue is not currently active, add it to the end of new
493*27b03b36SApple OSS Distributions 	 * flows list for that service class.
494*27b03b36SApple OSS Distributions 	 */
495*27b03b36SApple OSS Distributions 	if ((fq->fq_flags & (FQF_NEW_FLOW | FQF_OLD_FLOW)) == 0) {
496*27b03b36SApple OSS Distributions 		VERIFY(STAILQ_NEXT(fq, fq_actlink) == NULL);
497*27b03b36SApple OSS Distributions 		STAILQ_INSERT_TAIL(&fq_cl->fcl_new_flows, fq, fq_actlink);
498*27b03b36SApple OSS Distributions 		fq->fq_flags |= FQF_NEW_FLOW;
499*27b03b36SApple OSS Distributions 
500*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_newflows_cnt++;
501*27b03b36SApple OSS Distributions 
502*27b03b36SApple OSS Distributions 		fq->fq_deficit = fq_cl->fcl_quantum;
503*27b03b36SApple OSS Distributions 	}
504*27b03b36SApple OSS Distributions 	return ret;
505*27b03b36SApple OSS Distributions }
506*27b03b36SApple OSS Distributions 
507*27b03b36SApple OSS Distributions void
fq_getq_flow_internal(fq_if_t * fqs,fq_t * fq,pktsched_pkt_t * pkt)508*27b03b36SApple OSS Distributions fq_getq_flow_internal(fq_if_t *fqs, fq_t *fq, pktsched_pkt_t *pkt)
509*27b03b36SApple OSS Distributions {
510*27b03b36SApple OSS Distributions 	classq_pkt_t p = CLASSQ_PKT_INITIALIZER(p);
511*27b03b36SApple OSS Distributions 	uint32_t plen;
512*27b03b36SApple OSS Distributions 	fq_if_classq_t *fq_cl;
513*27b03b36SApple OSS Distributions 	struct ifclassq *ifq = fqs->fqs_ifq;
514*27b03b36SApple OSS Distributions 
515*27b03b36SApple OSS Distributions 	fq_dequeue(fq, &p);
516*27b03b36SApple OSS Distributions 	if (p.cp_ptype == QP_INVALID) {
517*27b03b36SApple OSS Distributions 		VERIFY(p.cp_mbuf == NULL);
518*27b03b36SApple OSS Distributions 		return;
519*27b03b36SApple OSS Distributions 	}
520*27b03b36SApple OSS Distributions 
521*27b03b36SApple OSS Distributions 	pktsched_pkt_encap(pkt, &p);
522*27b03b36SApple OSS Distributions 	plen = pktsched_get_pkt_len(pkt);
523*27b03b36SApple OSS Distributions 
524*27b03b36SApple OSS Distributions 	VERIFY(fq->fq_bytes >= plen);
525*27b03b36SApple OSS Distributions 	fq->fq_bytes -= plen;
526*27b03b36SApple OSS Distributions 
527*27b03b36SApple OSS Distributions 	fq_cl = &fqs->fqs_classq[fq->fq_sc_index];
528*27b03b36SApple OSS Distributions 	fq_cl->fcl_stat.fcl_byte_cnt -= plen;
529*27b03b36SApple OSS Distributions 	fq_cl->fcl_stat.fcl_pkt_cnt--;
530*27b03b36SApple OSS Distributions 	IFCQ_DEC_LEN(ifq);
531*27b03b36SApple OSS Distributions 	IFCQ_DEC_BYTES(ifq, plen);
532*27b03b36SApple OSS Distributions 
533*27b03b36SApple OSS Distributions 	/* Reset getqtime so that we don't count idle times */
534*27b03b36SApple OSS Distributions 	if (fq_empty(fq)) {
535*27b03b36SApple OSS Distributions 		fq->fq_getqtime = 0;
536*27b03b36SApple OSS Distributions 	}
537*27b03b36SApple OSS Distributions }
538*27b03b36SApple OSS Distributions 
539*27b03b36SApple OSS Distributions void
fq_getq_flow(fq_if_t * fqs,fq_t * fq,pktsched_pkt_t * pkt)540*27b03b36SApple OSS Distributions fq_getq_flow(fq_if_t *fqs, fq_t *fq, pktsched_pkt_t *pkt)
541*27b03b36SApple OSS Distributions {
542*27b03b36SApple OSS Distributions 	fq_if_classq_t *fq_cl;
543*27b03b36SApple OSS Distributions 	u_int64_t now;
544*27b03b36SApple OSS Distributions 	int64_t qdelay = 0;
545*27b03b36SApple OSS Distributions 	struct timespec now_ts;
546*27b03b36SApple OSS Distributions 	volatile uint32_t *pkt_flags;
547*27b03b36SApple OSS Distributions 	uint64_t *pkt_timestamp;
548*27b03b36SApple OSS Distributions 
549*27b03b36SApple OSS Distributions 	fq_getq_flow_internal(fqs, fq, pkt);
550*27b03b36SApple OSS Distributions 	if (pkt->pktsched_ptype == QP_INVALID) {
551*27b03b36SApple OSS Distributions 		VERIFY(pkt->pktsched_pkt_mbuf == NULL);
552*27b03b36SApple OSS Distributions 		return;
553*27b03b36SApple OSS Distributions 	}
554*27b03b36SApple OSS Distributions 
555*27b03b36SApple OSS Distributions 	pktsched_get_pkt_vars(pkt, &pkt_flags, &pkt_timestamp, NULL, NULL,
556*27b03b36SApple OSS Distributions 	    NULL, NULL);
557*27b03b36SApple OSS Distributions 
558*27b03b36SApple OSS Distributions 	nanouptime(&now_ts);
559*27b03b36SApple OSS Distributions 	now = (now_ts.tv_sec * NSEC_PER_SEC) + now_ts.tv_nsec;
560*27b03b36SApple OSS Distributions 
561*27b03b36SApple OSS Distributions 	/* this will compute qdelay in nanoseconds */
562*27b03b36SApple OSS Distributions 	if (now > *pkt_timestamp) {
563*27b03b36SApple OSS Distributions 		qdelay = now - *pkt_timestamp;
564*27b03b36SApple OSS Distributions 	}
565*27b03b36SApple OSS Distributions 	fq_cl = &fqs->fqs_classq[fq->fq_sc_index];
566*27b03b36SApple OSS Distributions 
567*27b03b36SApple OSS Distributions 	if (fq->fq_min_qdelay == 0 ||
568*27b03b36SApple OSS Distributions 	    (qdelay > 0 && (u_int64_t)qdelay < fq->fq_min_qdelay)) {
569*27b03b36SApple OSS Distributions 		fq->fq_min_qdelay = qdelay;
570*27b03b36SApple OSS Distributions 	}
571*27b03b36SApple OSS Distributions 
572*27b03b36SApple OSS Distributions 	/* Update min/max/avg qdelay for the respective class */
573*27b03b36SApple OSS Distributions 	if (fq_cl->fcl_stat.fcl_min_qdelay == 0 ||
574*27b03b36SApple OSS Distributions 	    (qdelay > 0 && (u_int64_t)qdelay < fq_cl->fcl_stat.fcl_min_qdelay)) {
575*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_min_qdelay = qdelay;
576*27b03b36SApple OSS Distributions 	}
577*27b03b36SApple OSS Distributions 
578*27b03b36SApple OSS Distributions 	if (fq_cl->fcl_stat.fcl_max_qdelay == 0 ||
579*27b03b36SApple OSS Distributions 	    (qdelay > 0 && (u_int64_t)qdelay > fq_cl->fcl_stat.fcl_max_qdelay)) {
580*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_max_qdelay = qdelay;
581*27b03b36SApple OSS Distributions 	}
582*27b03b36SApple OSS Distributions 
583*27b03b36SApple OSS Distributions 	uint64_t num_dequeues = fq_cl->fcl_stat.fcl_dequeue;
584*27b03b36SApple OSS Distributions 
585*27b03b36SApple OSS Distributions 	if (num_dequeues == 0) {
586*27b03b36SApple OSS Distributions 		fq_cl->fcl_stat.fcl_avg_qdelay = qdelay;
587*27b03b36SApple OSS Distributions 	} else if (qdelay > 0) {
588*27b03b36SApple OSS Distributions 		uint64_t res = 0;
589*27b03b36SApple OSS Distributions 		if (os_add_overflow(num_dequeues, 1, &res)) {
590*27b03b36SApple OSS Distributions 			/* Reset the dequeue num and dequeue bytes */
591*27b03b36SApple OSS Distributions 			fq_cl->fcl_stat.fcl_dequeue = num_dequeues = 0;
592*27b03b36SApple OSS Distributions 			fq_cl->fcl_stat.fcl_dequeue_bytes = 0;
593*27b03b36SApple OSS Distributions 			fq_cl->fcl_stat.fcl_avg_qdelay = qdelay;
594*27b03b36SApple OSS Distributions 			os_log_info(OS_LOG_DEFAULT, "%s: dequeue num overflow, "
595*27b03b36SApple OSS Distributions 			    "flow: 0x%x, iface: %s", __func__, fq->fq_flowhash,
596*27b03b36SApple OSS Distributions 			    if_name(fqs->fqs_ifq->ifcq_ifp));
597*27b03b36SApple OSS Distributions 		} else {
598*27b03b36SApple OSS Distributions 			uint64_t product = 0;
599*27b03b36SApple OSS Distributions 			if (os_mul_overflow(fq_cl->fcl_stat.fcl_avg_qdelay,
600*27b03b36SApple OSS Distributions 			    num_dequeues, &product) || os_add_overflow(product, qdelay, &res)) {
601*27b03b36SApple OSS Distributions 				fq_cl->fcl_stat.fcl_avg_qdelay = qdelay;
602*27b03b36SApple OSS Distributions 			} else {
603*27b03b36SApple OSS Distributions 				fq_cl->fcl_stat.fcl_avg_qdelay = res /
604*27b03b36SApple OSS Distributions 				    (num_dequeues + 1);
605*27b03b36SApple OSS Distributions 			}
606*27b03b36SApple OSS Distributions 		}
607*27b03b36SApple OSS Distributions 	}
608*27b03b36SApple OSS Distributions 
609*27b03b36SApple OSS Distributions 	if (now >= fq->fq_updatetime) {
610*27b03b36SApple OSS Distributions 		if (fq->fq_min_qdelay > fqs->fqs_target_qdelay) {
611*27b03b36SApple OSS Distributions 			if (!FQ_IS_DELAYHIGH(fq)) {
612*27b03b36SApple OSS Distributions 				FQ_SET_DELAY_HIGH(fq);
613*27b03b36SApple OSS Distributions 				os_log_error(OS_LOG_DEFAULT,
614*27b03b36SApple OSS Distributions 				    "%s: high delay idx: %d, %llu, flow: 0x%x, "
615*27b03b36SApple OSS Distributions 				    "iface: %s", __func__, fq->fq_sc_index,
616*27b03b36SApple OSS Distributions 				    fq->fq_min_qdelay, fq->fq_flowhash,
617*27b03b36SApple OSS Distributions 				    if_name(fqs->fqs_ifq->ifcq_ifp));
618*27b03b36SApple OSS Distributions 			}
619*27b03b36SApple OSS Distributions 		} else {
620*27b03b36SApple OSS Distributions 			FQ_CLEAR_DELAY_HIGH(fq);
621*27b03b36SApple OSS Distributions 		}
622*27b03b36SApple OSS Distributions 		/* Reset measured queue delay and update time */
623*27b03b36SApple OSS Distributions 		fq->fq_updatetime = now + fqs->fqs_update_interval;
624*27b03b36SApple OSS Distributions 		fq->fq_min_qdelay = 0;
625*27b03b36SApple OSS Distributions 	}
626*27b03b36SApple OSS Distributions 
627*27b03b36SApple OSS Distributions 	if (fqs->fqs_large_flow != fq || !fq_if_almost_at_drop_limit(fqs)) {
628*27b03b36SApple OSS Distributions 		FQ_CLEAR_OVERWHELMING(fq);
629*27b03b36SApple OSS Distributions 	}
630*27b03b36SApple OSS Distributions 	if (!FQ_IS_DELAYHIGH(fq) || fq_empty(fq)) {
631*27b03b36SApple OSS Distributions 		FQ_CLEAR_DELAY_HIGH(fq);
632*27b03b36SApple OSS Distributions 	}
633*27b03b36SApple OSS Distributions 
634*27b03b36SApple OSS Distributions 	if ((fq->fq_flags & FQF_FLOWCTL_ON) &&
635*27b03b36SApple OSS Distributions 	    !FQ_IS_DELAYHIGH(fq) && !FQ_IS_OVERWHELMING(fq)) {
636*27b03b36SApple OSS Distributions 		fq_if_flow_feedback(fqs, fq, fq_cl);
637*27b03b36SApple OSS Distributions 	}
638*27b03b36SApple OSS Distributions 
639*27b03b36SApple OSS Distributions 	if (fq_empty(fq)) {
640*27b03b36SApple OSS Distributions 		/* Reset getqtime so that we don't count idle times */
641*27b03b36SApple OSS Distributions 		fq->fq_getqtime = 0;
642*27b03b36SApple OSS Distributions 	} else {
643*27b03b36SApple OSS Distributions 		fq->fq_getqtime = now;
644*27b03b36SApple OSS Distributions 	}
645*27b03b36SApple OSS Distributions 	fq_if_is_flow_heavy(fqs, fq);
646*27b03b36SApple OSS Distributions 
647*27b03b36SApple OSS Distributions 	*pkt_timestamp = 0;
648*27b03b36SApple OSS Distributions 	switch (pkt->pktsched_ptype) {
649*27b03b36SApple OSS Distributions 	case QP_MBUF:
650*27b03b36SApple OSS Distributions 		*pkt_flags &= ~PKTF_PRIV_GUARDED;
651*27b03b36SApple OSS Distributions 		break;
652*27b03b36SApple OSS Distributions #if SKYWALK
653*27b03b36SApple OSS Distributions 	case QP_PACKET:
654*27b03b36SApple OSS Distributions 		/* sanity check */
655*27b03b36SApple OSS Distributions 		ASSERT((*pkt_flags & ~PKT_F_COMMON_MASK) == 0);
656*27b03b36SApple OSS Distributions 		break;
657*27b03b36SApple OSS Distributions #endif /* SKYWALK */
658*27b03b36SApple OSS Distributions 	default:
659*27b03b36SApple OSS Distributions 		VERIFY(0);
660*27b03b36SApple OSS Distributions 		/* NOTREACHED */
661*27b03b36SApple OSS Distributions 		__builtin_unreachable();
662*27b03b36SApple OSS Distributions 	}
663*27b03b36SApple OSS Distributions }
664