xref: /xnu-11417.121.6/bsd/net/classq/classq_fq_codel.c (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributions /*
2*a1e26a70SApple OSS Distributions  * Copyright (c) 2016-2021 Apple Inc. All rights reserved.
3*a1e26a70SApple OSS Distributions  *
4*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*a1e26a70SApple OSS Distributions  *
6*a1e26a70SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*a1e26a70SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*a1e26a70SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*a1e26a70SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*a1e26a70SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*a1e26a70SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*a1e26a70SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*a1e26a70SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*a1e26a70SApple OSS Distributions  *
15*a1e26a70SApple OSS Distributions  * Please obtain a copy of the License at
16*a1e26a70SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*a1e26a70SApple OSS Distributions  *
18*a1e26a70SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*a1e26a70SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*a1e26a70SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*a1e26a70SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*a1e26a70SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*a1e26a70SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*a1e26a70SApple OSS Distributions  * limitations under the License.
25*a1e26a70SApple OSS Distributions  *
26*a1e26a70SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*a1e26a70SApple OSS Distributions  */
28*a1e26a70SApple OSS Distributions 
29*a1e26a70SApple OSS Distributions /*
30*a1e26a70SApple OSS Distributions  * The migration of flow queue between the different states is summarised in
31*a1e26a70SApple OSS Distributions  * the below state diagram. (RFC 8290)
32*a1e26a70SApple OSS Distributions  *
33*a1e26a70SApple OSS Distributions  * +-----------------+                +------------------+
34*a1e26a70SApple OSS Distributions  * |                 |     Empty      |                  |
35*a1e26a70SApple OSS Distributions  * |     Empty       |<---------------+       Old        +----+
36*a1e26a70SApple OSS Distributions  * |                 |                |                  |    |
37*a1e26a70SApple OSS Distributions  * +-------+---------+                +------------------+    |
38*a1e26a70SApple OSS Distributions  *         |                             ^            ^       |Credits
39*a1e26a70SApple OSS Distributions  *         |Arrival                      |            |       |Exhausted
40*a1e26a70SApple OSS Distributions  *         v                             |            |       |
41*a1e26a70SApple OSS Distributions  * +-----------------+                   |            |       |
42*a1e26a70SApple OSS Distributions  * |                 |      Empty or     |            |       |
43*a1e26a70SApple OSS Distributions  * |      New        +-------------------+            +-------+
44*a1e26a70SApple OSS Distributions  * |                 | Credits Exhausted
45*a1e26a70SApple OSS Distributions  * +-----------------+
46*a1e26a70SApple OSS Distributions  *
47*a1e26a70SApple OSS Distributions  * In this implementation of FQ-CODEL, flow queue is a dynamically allocated
48*a1e26a70SApple OSS Distributions  * object. An active flow queue goes through the above cycle of state
49*a1e26a70SApple OSS Distributions  * transitions very often. To avoid the cost of frequent flow queue object
50*a1e26a70SApple OSS Distributions  * allocation/free, this implementation retains the flow queue object in
51*a1e26a70SApple OSS Distributions  * [Empty] state on an Empty flow queue list with an active reference in flow
52*a1e26a70SApple OSS Distributions  * queue hash table. The flow queue objects on the Empty flow queue list have
53*a1e26a70SApple OSS Distributions  * an associated age and are purged accordingly.
54*a1e26a70SApple OSS Distributions  */
55*a1e26a70SApple OSS Distributions 
56*a1e26a70SApple OSS Distributions #include <sys/cdefs.h>
57*a1e26a70SApple OSS Distributions #include <sys/param.h>
58*a1e26a70SApple OSS Distributions #include <sys/mbuf.h>
59*a1e26a70SApple OSS Distributions #include <sys/socket.h>
60*a1e26a70SApple OSS Distributions #include <sys/sockio.h>
61*a1e26a70SApple OSS Distributions #include <sys/systm.h>
62*a1e26a70SApple OSS Distributions #include <sys/syslog.h>
63*a1e26a70SApple OSS Distributions #include <sys/proc.h>
64*a1e26a70SApple OSS Distributions #include <sys/errno.h>
65*a1e26a70SApple OSS Distributions #include <sys/kernel.h>
66*a1e26a70SApple OSS Distributions #include <sys/kauth.h>
67*a1e26a70SApple OSS Distributions #include <sys/sdt.h>
68*a1e26a70SApple OSS Distributions #include <kern/zalloc.h>
69*a1e26a70SApple OSS Distributions #include <netinet/in.h>
70*a1e26a70SApple OSS Distributions 
71*a1e26a70SApple OSS Distributions #include <net/classq/classq.h>
72*a1e26a70SApple OSS Distributions #include <net/classq/if_classq.h>
73*a1e26a70SApple OSS Distributions #include <net/pktsched/pktsched.h>
74*a1e26a70SApple OSS Distributions #include <net/pktsched/pktsched_fq_codel.h>
75*a1e26a70SApple OSS Distributions #include <net/classq/classq_fq_codel.h>
76*a1e26a70SApple OSS Distributions #include <net/droptap.h>
77*a1e26a70SApple OSS Distributions 
78*a1e26a70SApple OSS Distributions #include <netinet/tcp_var.h>
79*a1e26a70SApple OSS Distributions 
80*a1e26a70SApple OSS Distributions #define FQ_ZONE_MAX     (32 * 1024)     /* across all interfaces */
81*a1e26a70SApple OSS Distributions 
82*a1e26a70SApple OSS Distributions #define DTYPE_NODROP    0       /* no drop */
83*a1e26a70SApple OSS Distributions #define DTYPE_FORCED    1       /* a "forced" drop */
84*a1e26a70SApple OSS Distributions #define DTYPE_EARLY     2       /* an "unforced" (early) drop */
85*a1e26a70SApple OSS Distributions 
86*a1e26a70SApple OSS Distributions static uint32_t pkt_compressor = 1;
87*a1e26a70SApple OSS Distributions static uint64_t l4s_ce_threshold = 0; /* in usec */
88*a1e26a70SApple OSS Distributions static uint32_t l4s_local_ce_report = 0;
89*a1e26a70SApple OSS Distributions static uint64_t pkt_pacing_leeway = 0; /* in usec */
90*a1e26a70SApple OSS Distributions static uint64_t max_pkt_pacing_interval = 3 * NSEC_PER_SEC;
91*a1e26a70SApple OSS Distributions static uint64_t l4s_min_delay_threshold = 20 * NSEC_PER_MSEC; /* 20 ms */
92*a1e26a70SApple OSS Distributions #if (DEBUG || DEVELOPMENT)
93*a1e26a70SApple OSS Distributions SYSCTL_NODE(_net_classq, OID_AUTO, flow_q, CTLFLAG_RW | CTLFLAG_LOCKED,
94*a1e26a70SApple OSS Distributions     0, "FQ-CODEL parameters");
95*a1e26a70SApple OSS Distributions 
96*a1e26a70SApple OSS Distributions SYSCTL_UINT(_net_classq_flow_q, OID_AUTO, pkt_compressor,
97*a1e26a70SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &pkt_compressor, 0, "enable pkt compression");
98*a1e26a70SApple OSS Distributions 
99*a1e26a70SApple OSS Distributions SYSCTL_QUAD(_net_classq, OID_AUTO, l4s_ce_threshold,
100*a1e26a70SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &l4s_ce_threshold,
101*a1e26a70SApple OSS Distributions     "L4S CE threshold");
102*a1e26a70SApple OSS Distributions 
103*a1e26a70SApple OSS Distributions SYSCTL_UINT(_net_classq_flow_q, OID_AUTO, l4s_local_ce_report,
104*a1e26a70SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &l4s_local_ce_report, 0,
105*a1e26a70SApple OSS Distributions     "enable L4S local CE report");
106*a1e26a70SApple OSS Distributions 
107*a1e26a70SApple OSS Distributions SYSCTL_QUAD(_net_classq_flow_q, OID_AUTO, pkt_pacing_leeway,
108*a1e26a70SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &pkt_pacing_leeway, "packet pacing leeway");
109*a1e26a70SApple OSS Distributions 
110*a1e26a70SApple OSS Distributions SYSCTL_QUAD(_net_classq_flow_q, OID_AUTO, max_pkt_pacing_interval,
111*a1e26a70SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &max_pkt_pacing_interval, "max packet pacing interval");
112*a1e26a70SApple OSS Distributions 
113*a1e26a70SApple OSS Distributions SYSCTL_QUAD(_net_classq_flow_q, OID_AUTO, l4s_min_delay_threshold,
114*a1e26a70SApple OSS Distributions     CTLFLAG_RW | CTLFLAG_LOCKED, &l4s_min_delay_threshold, "l4s min delay threshold");
115*a1e26a70SApple OSS Distributions #endif /* (DEBUG || DEVELOPMENT) */
116*a1e26a70SApple OSS Distributions 
117*a1e26a70SApple OSS Distributions void
fq_codel_init(void)118*a1e26a70SApple OSS Distributions fq_codel_init(void)
119*a1e26a70SApple OSS Distributions {
120*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_AON_FLOW_HIGH_DELAY == 0x8300004);
121*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_AON_THROTTLE == 0x8300008);
122*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_AON_FLOW_OVERWHELMING == 0x830000c);
123*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_AON_FLOW_DQ_STALL == 0x8300010);
124*a1e26a70SApple OSS Distributions 
125*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_STATS_FLOW_ENQUEUE == 0x8310004);
126*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_STATS_FLOW_DEQUEUE == 0x8310008);
127*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_STATS_FLOW_CTL == 0x831000c);
128*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_STATS_FLOW_ALLOC == 0x8310010);
129*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_STATS_FLOW_DESTROY == 0x8310014);
130*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_STATS_FLOW_REPORT_CE == 0x8310018);
131*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_STATS_GET_QLEN == 0x831001c);
132*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_TX_NOT_READY == 0x8310020);
133*a1e26a70SApple OSS Distributions 	_CASSERT(AQM_KTRACE_TX_PACEMAKER == 0x8310024);
134*a1e26a70SApple OSS Distributions }
135*a1e26a70SApple OSS Distributions 
136*a1e26a70SApple OSS Distributions fq_t *
fq_alloc(classq_pkt_type_t ptype)137*a1e26a70SApple OSS Distributions fq_alloc(classq_pkt_type_t ptype)
138*a1e26a70SApple OSS Distributions {
139*a1e26a70SApple OSS Distributions 	fq_t *fq = NULL;
140*a1e26a70SApple OSS Distributions 
141*a1e26a70SApple OSS Distributions 	fq = kalloc_type(fq_t, Z_WAITOK_ZERO);
142*a1e26a70SApple OSS Distributions 	if (ptype == QP_MBUF) {
143*a1e26a70SApple OSS Distributions 		MBUFQ_INIT(&fq->fq_mbufq);
144*a1e26a70SApple OSS Distributions 	}
145*a1e26a70SApple OSS Distributions #if SKYWALK
146*a1e26a70SApple OSS Distributions 	else {
147*a1e26a70SApple OSS Distributions 		VERIFY(ptype == QP_PACKET);
148*a1e26a70SApple OSS Distributions 		KPKTQ_INIT(&fq->fq_kpktq);
149*a1e26a70SApple OSS Distributions 	}
150*a1e26a70SApple OSS Distributions #endif /* SKYWALK */
151*a1e26a70SApple OSS Distributions 	CLASSQ_PKT_INIT(&fq->fq_dq_head);
152*a1e26a70SApple OSS Distributions 	CLASSQ_PKT_INIT(&fq->fq_dq_tail);
153*a1e26a70SApple OSS Distributions 	fq->fq_in_dqlist = false;
154*a1e26a70SApple OSS Distributions 
155*a1e26a70SApple OSS Distributions 	return fq;
156*a1e26a70SApple OSS Distributions }
157*a1e26a70SApple OSS Distributions 
158*a1e26a70SApple OSS Distributions void
fq_destroy(fq_t * fq,classq_pkt_type_t ptype)159*a1e26a70SApple OSS Distributions fq_destroy(fq_t *fq, classq_pkt_type_t ptype)
160*a1e26a70SApple OSS Distributions {
161*a1e26a70SApple OSS Distributions 	VERIFY(!fq->fq_in_dqlist);
162*a1e26a70SApple OSS Distributions 	VERIFY(fq_empty(fq, ptype));
163*a1e26a70SApple OSS Distributions 	VERIFY(!(fq->fq_flags & (FQF_NEW_FLOW | FQF_OLD_FLOW |
164*a1e26a70SApple OSS Distributions 	    FQF_EMPTY_FLOW)));
165*a1e26a70SApple OSS Distributions 	VERIFY(fq->fq_bytes == 0);
166*a1e26a70SApple OSS Distributions 	kfree_type(fq_t, fq);
167*a1e26a70SApple OSS Distributions }
168*a1e26a70SApple OSS Distributions 
169*a1e26a70SApple 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)170*a1e26a70SApple OSS Distributions fq_detect_dequeue_stall(fq_if_t *fqs, fq_t *flowq, fq_if_classq_t *fq_cl,
171*a1e26a70SApple OSS Distributions     u_int64_t *now)
172*a1e26a70SApple OSS Distributions {
173*a1e26a70SApple OSS Distributions 	u_int64_t maxgetqtime, update_interval;
174*a1e26a70SApple OSS Distributions 	if (FQ_IS_DELAY_HIGH(flowq) || flowq->fq_getqtime == 0 ||
175*a1e26a70SApple OSS Distributions 	    fq_empty(flowq, fqs->fqs_ptype) ||
176*a1e26a70SApple OSS Distributions 	    flowq->fq_bytes < FQ_MIN_FC_THRESHOLD_BYTES) {
177*a1e26a70SApple OSS Distributions 		return;
178*a1e26a70SApple OSS Distributions 	}
179*a1e26a70SApple OSS Distributions 
180*a1e26a70SApple OSS Distributions 	update_interval = FQ_UPDATE_INTERVAL(flowq);
181*a1e26a70SApple OSS Distributions 	maxgetqtime = flowq->fq_getqtime + update_interval;
182*a1e26a70SApple OSS Distributions 	if ((*now) > maxgetqtime) {
183*a1e26a70SApple OSS Distributions 		/*
184*a1e26a70SApple OSS Distributions 		 * there was no dequeue in an update interval worth of
185*a1e26a70SApple OSS Distributions 		 * time. It means that the queue is stalled.
186*a1e26a70SApple OSS Distributions 		 */
187*a1e26a70SApple OSS Distributions 		FQ_SET_DELAY_HIGH(flowq);
188*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_dequeue_stall++;
189*a1e26a70SApple OSS Distributions 		os_log_error(OS_LOG_DEFAULT, "%s:num: %d, "
190*a1e26a70SApple OSS Distributions 		    "scidx: %d, flow: 0x%x, iface: %s grp: %hhu", __func__,
191*a1e26a70SApple OSS Distributions 		    fq_cl->fcl_stat.fcl_dequeue_stall, flowq->fq_sc_index,
192*a1e26a70SApple OSS Distributions 		    flowq->fq_flowhash, if_name(fqs->fqs_ifq->ifcq_ifp),
193*a1e26a70SApple OSS Distributions 		    FQ_GROUP(flowq)->fqg_index);
194*a1e26a70SApple OSS Distributions 		KDBG(AQM_KTRACE_AON_FLOW_DQ_STALL, flowq->fq_flowhash,
195*a1e26a70SApple OSS Distributions 		    AQM_KTRACE_FQ_GRP_SC_IDX(flowq), flowq->fq_bytes,
196*a1e26a70SApple OSS Distributions 		    (*now) - flowq->fq_getqtime);
197*a1e26a70SApple OSS Distributions 	}
198*a1e26a70SApple OSS Distributions }
199*a1e26a70SApple OSS Distributions 
200*a1e26a70SApple OSS Distributions void
fq_head_drop(fq_if_t * fqs,fq_t * fq)201*a1e26a70SApple OSS Distributions fq_head_drop(fq_if_t *fqs, fq_t *fq)
202*a1e26a70SApple OSS Distributions {
203*a1e26a70SApple OSS Distributions 	pktsched_pkt_t pkt;
204*a1e26a70SApple OSS Distributions 	volatile uint32_t *__single pkt_flags;
205*a1e26a70SApple OSS Distributions 	uint64_t *__single pkt_timestamp;
206*a1e26a70SApple OSS Distributions 	struct ifclassq *ifq = fqs->fqs_ifq;
207*a1e26a70SApple OSS Distributions 
208*a1e26a70SApple OSS Distributions 	_PKTSCHED_PKT_INIT(&pkt);
209*a1e26a70SApple OSS Distributions 	fq_getq_flow_internal(fqs, fq, &pkt);
210*a1e26a70SApple OSS Distributions 	if (pkt.pktsched_pkt_mbuf == NULL) {
211*a1e26a70SApple OSS Distributions 		return;
212*a1e26a70SApple OSS Distributions 	}
213*a1e26a70SApple OSS Distributions 
214*a1e26a70SApple OSS Distributions 	pktsched_get_pkt_vars(&pkt, &pkt_flags, &pkt_timestamp, NULL, NULL,
215*a1e26a70SApple OSS Distributions 	    NULL, NULL, NULL);
216*a1e26a70SApple OSS Distributions 
217*a1e26a70SApple OSS Distributions 	*pkt_timestamp = 0;
218*a1e26a70SApple OSS Distributions 	switch (pkt.pktsched_ptype) {
219*a1e26a70SApple OSS Distributions 	case QP_MBUF:
220*a1e26a70SApple OSS Distributions 		*pkt_flags &= ~PKTF_PRIV_GUARDED;
221*a1e26a70SApple OSS Distributions 		break;
222*a1e26a70SApple OSS Distributions #if SKYWALK
223*a1e26a70SApple OSS Distributions 	case QP_PACKET:
224*a1e26a70SApple OSS Distributions 		/* sanity check */
225*a1e26a70SApple OSS Distributions 		ASSERT((*pkt_flags & ~PKT_F_COMMON_MASK) == 0);
226*a1e26a70SApple OSS Distributions 		break;
227*a1e26a70SApple OSS Distributions #endif /* SKYWALK */
228*a1e26a70SApple OSS Distributions 	default:
229*a1e26a70SApple OSS Distributions 		VERIFY(0);
230*a1e26a70SApple OSS Distributions 		/* NOTREACHED */
231*a1e26a70SApple OSS Distributions 		__builtin_unreachable();
232*a1e26a70SApple OSS Distributions 	}
233*a1e26a70SApple OSS Distributions 
234*a1e26a70SApple OSS Distributions 	IFCQ_DROP_ADD(ifq, 1, pktsched_get_pkt_len(&pkt));
235*a1e26a70SApple OSS Distributions 	IFCQ_CONVERT_LOCK(ifq);
236*a1e26a70SApple OSS Distributions 	pktsched_free_pkt(&pkt);
237*a1e26a70SApple OSS Distributions }
238*a1e26a70SApple OSS Distributions 
239*a1e26a70SApple OSS Distributions 
240*a1e26a70SApple OSS Distributions static int
fq_compressor(fq_if_t * fqs,fq_t * fq,fq_if_classq_t * fq_cl,pktsched_pkt_t * pkt)241*a1e26a70SApple OSS Distributions fq_compressor(fq_if_t *fqs, fq_t *fq, fq_if_classq_t *fq_cl,
242*a1e26a70SApple OSS Distributions     pktsched_pkt_t *pkt)
243*a1e26a70SApple OSS Distributions {
244*a1e26a70SApple OSS Distributions 	classq_pkt_type_t ptype = fqs->fqs_ptype;
245*a1e26a70SApple OSS Distributions 	uint32_t comp_gencnt = 0;
246*a1e26a70SApple OSS Distributions 	uint64_t *__single pkt_timestamp;
247*a1e26a70SApple OSS Distributions 	uint64_t old_timestamp = 0;
248*a1e26a70SApple OSS Distributions 	uint32_t old_pktlen = 0;
249*a1e26a70SApple OSS Distributions 	struct ifclassq *ifq = fqs->fqs_ifq;
250*a1e26a70SApple OSS Distributions 
251*a1e26a70SApple OSS Distributions 	if (__improbable(pkt_compressor == 0)) {
252*a1e26a70SApple OSS Distributions 		return 0;
253*a1e26a70SApple OSS Distributions 	}
254*a1e26a70SApple OSS Distributions 
255*a1e26a70SApple OSS Distributions 	pktsched_get_pkt_vars(pkt, NULL, &pkt_timestamp, NULL, NULL, NULL,
256*a1e26a70SApple OSS Distributions 	    &comp_gencnt, NULL);
257*a1e26a70SApple OSS Distributions 
258*a1e26a70SApple OSS Distributions 	if (comp_gencnt == 0) {
259*a1e26a70SApple OSS Distributions 		return 0;
260*a1e26a70SApple OSS Distributions 	}
261*a1e26a70SApple OSS Distributions 
262*a1e26a70SApple OSS Distributions 	fq_cl->fcl_stat.fcl_pkts_compressible++;
263*a1e26a70SApple OSS Distributions 
264*a1e26a70SApple OSS Distributions 	if (fq_empty(fq, fqs->fqs_ptype)) {
265*a1e26a70SApple OSS Distributions 		return 0;
266*a1e26a70SApple OSS Distributions 	}
267*a1e26a70SApple OSS Distributions 
268*a1e26a70SApple OSS Distributions 	if (ptype == QP_MBUF) {
269*a1e26a70SApple OSS Distributions 		struct mbuf *m = MBUFQ_LAST(&fq->fq_mbufq);
270*a1e26a70SApple OSS Distributions 
271*a1e26a70SApple OSS Distributions 		if (comp_gencnt != m->m_pkthdr.comp_gencnt) {
272*a1e26a70SApple OSS Distributions 			return 0;
273*a1e26a70SApple OSS Distributions 		}
274*a1e26a70SApple OSS Distributions 
275*a1e26a70SApple OSS Distributions 		/* If we got until here, we should merge/replace the segment */
276*a1e26a70SApple OSS Distributions 		MBUFQ_REMOVE(&fq->fq_mbufq, m);
277*a1e26a70SApple OSS Distributions 		old_pktlen = m_pktlen(m);
278*a1e26a70SApple OSS Distributions 		old_timestamp = m->m_pkthdr.pkt_timestamp;
279*a1e26a70SApple OSS Distributions 
280*a1e26a70SApple OSS Distributions 		IFCQ_CONVERT_LOCK(fqs->fqs_ifq);
281*a1e26a70SApple OSS Distributions 
282*a1e26a70SApple OSS Distributions 		if (__improbable(droptap_verbose > 0)) {
283*a1e26a70SApple OSS Distributions 			droptap_output_mbuf(m, DROP_REASON_AQM_COMPRESSED, NULL, 0, 0,
284*a1e26a70SApple OSS Distributions 			    fqs->fqs_ifq->ifcq_ifp);
285*a1e26a70SApple OSS Distributions 		}
286*a1e26a70SApple OSS Distributions 
287*a1e26a70SApple OSS Distributions 		m_freem(m);
288*a1e26a70SApple OSS Distributions 	}
289*a1e26a70SApple OSS Distributions #if SKYWALK
290*a1e26a70SApple OSS Distributions 	else {
291*a1e26a70SApple OSS Distributions 		struct __kern_packet *kpkt = KPKTQ_LAST(&fq->fq_kpktq);
292*a1e26a70SApple OSS Distributions 
293*a1e26a70SApple OSS Distributions 		if (comp_gencnt != kpkt->pkt_comp_gencnt) {
294*a1e26a70SApple OSS Distributions 			return 0;
295*a1e26a70SApple OSS Distributions 		}
296*a1e26a70SApple OSS Distributions 
297*a1e26a70SApple OSS Distributions 		/* If we got until here, we should merge/replace the segment */
298*a1e26a70SApple OSS Distributions 		KPKTQ_REMOVE(&fq->fq_kpktq, kpkt);
299*a1e26a70SApple OSS Distributions 		old_pktlen = kpkt->pkt_length;
300*a1e26a70SApple OSS Distributions 		old_timestamp = kpkt->pkt_timestamp;
301*a1e26a70SApple OSS Distributions 
302*a1e26a70SApple OSS Distributions 		IFCQ_CONVERT_LOCK(fqs->fqs_ifq);
303*a1e26a70SApple OSS Distributions 
304*a1e26a70SApple OSS Distributions 		if (__improbable(droptap_verbose > 0)) {
305*a1e26a70SApple OSS Distributions 			droptap_output_packet(SK_PKT2PH(kpkt), DROP_REASON_AQM_COMPRESSED, NULL, 0, 0,
306*a1e26a70SApple OSS Distributions 			    fqs->fqs_ifq->ifcq_ifp, 0, NULL, -1, NULL, 0, 0);
307*a1e26a70SApple OSS Distributions 		}
308*a1e26a70SApple OSS Distributions 
309*a1e26a70SApple OSS Distributions 		struct kern_pbufpool *pp =
310*a1e26a70SApple OSS Distributions 		    __DECONST(struct kern_pbufpool *, ((struct __kern_quantum *)kpkt)->qum_pp);
311*a1e26a70SApple OSS Distributions 		pp_free_packet(pp, (uint64_t)kpkt);
312*a1e26a70SApple OSS Distributions 	}
313*a1e26a70SApple OSS Distributions #endif /* SKYWALK */
314*a1e26a70SApple OSS Distributions 
315*a1e26a70SApple OSS Distributions 	fq->fq_bytes -= old_pktlen;
316*a1e26a70SApple OSS Distributions 	fq_cl->fcl_stat.fcl_byte_cnt -= old_pktlen;
317*a1e26a70SApple OSS Distributions 	fq_cl->fcl_stat.fcl_pkt_cnt--;
318*a1e26a70SApple OSS Distributions 	IFCQ_DEC_LEN(ifq);
319*a1e26a70SApple OSS Distributions 	IFCQ_DEC_BYTES(ifq, old_pktlen);
320*a1e26a70SApple OSS Distributions 
321*a1e26a70SApple OSS Distributions 	FQ_GRP_DEC_LEN(fq);
322*a1e26a70SApple OSS Distributions 	FQ_GRP_DEC_BYTES(fq, old_pktlen);
323*a1e26a70SApple OSS Distributions 
324*a1e26a70SApple OSS Distributions 	*pkt_timestamp = old_timestamp;
325*a1e26a70SApple OSS Distributions 
326*a1e26a70SApple OSS Distributions 	return CLASSQEQ_COMPRESSED;
327*a1e26a70SApple OSS Distributions }
328*a1e26a70SApple OSS Distributions 
329*a1e26a70SApple OSS Distributions int
fq_addq(fq_if_t * fqs,fq_if_group_t * fq_grp,pktsched_pkt_t * pkt,fq_if_classq_t * fq_cl)330*a1e26a70SApple OSS Distributions fq_addq(fq_if_t *fqs, fq_if_group_t *fq_grp, pktsched_pkt_t *pkt,
331*a1e26a70SApple OSS Distributions     fq_if_classq_t *fq_cl)
332*a1e26a70SApple OSS Distributions {
333*a1e26a70SApple OSS Distributions 	int droptype = DTYPE_NODROP, fc_adv = 0, ret = CLASSQEQ_SUCCESS;
334*a1e26a70SApple OSS Distributions 	u_int64_t now;
335*a1e26a70SApple OSS Distributions 	fq_t *fq = NULL;
336*a1e26a70SApple OSS Distributions 	uint64_t *__single pkt_timestamp;
337*a1e26a70SApple OSS Distributions 	volatile uint32_t *__single pkt_flags;
338*a1e26a70SApple OSS Distributions 	uint32_t pkt_flowid, cnt;
339*a1e26a70SApple OSS Distributions 	uint8_t pkt_proto, pkt_flowsrc;
340*a1e26a70SApple OSS Distributions 	fq_tfc_type_t tfc_type = FQ_TFC_C;
341*a1e26a70SApple OSS Distributions 
342*a1e26a70SApple OSS Distributions 	cnt = pkt->pktsched_pcnt;
343*a1e26a70SApple OSS Distributions 	pktsched_get_pkt_vars(pkt, &pkt_flags, &pkt_timestamp, &pkt_flowid,
344*a1e26a70SApple OSS Distributions 	    &pkt_flowsrc, &pkt_proto, NULL, NULL);
345*a1e26a70SApple OSS Distributions 
346*a1e26a70SApple OSS Distributions 	/*
347*a1e26a70SApple OSS Distributions 	 * XXX Not walking the chain to set this flag on every packet.
348*a1e26a70SApple OSS Distributions 	 * This flag is only used for debugging. Nothing is affected if it's
349*a1e26a70SApple OSS Distributions 	 * not set.
350*a1e26a70SApple OSS Distributions 	 */
351*a1e26a70SApple OSS Distributions 	switch (pkt->pktsched_ptype) {
352*a1e26a70SApple OSS Distributions 	case QP_MBUF:
353*a1e26a70SApple OSS Distributions 		/* See comments in <rdar://problem/14040693> */
354*a1e26a70SApple OSS Distributions 		VERIFY(!(*pkt_flags & PKTF_PRIV_GUARDED));
355*a1e26a70SApple OSS Distributions 		*pkt_flags |= PKTF_PRIV_GUARDED;
356*a1e26a70SApple OSS Distributions 		break;
357*a1e26a70SApple OSS Distributions #if SKYWALK
358*a1e26a70SApple OSS Distributions 	case QP_PACKET:
359*a1e26a70SApple OSS Distributions 		/* sanity check */
360*a1e26a70SApple OSS Distributions 		ASSERT((*pkt_flags & ~PKT_F_COMMON_MASK) == 0);
361*a1e26a70SApple OSS Distributions 		break;
362*a1e26a70SApple OSS Distributions #endif /* SKYWALK */
363*a1e26a70SApple OSS Distributions 	default:
364*a1e26a70SApple OSS Distributions 		VERIFY(0);
365*a1e26a70SApple OSS Distributions 		/* NOTREACHED */
366*a1e26a70SApple OSS Distributions 		__builtin_unreachable();
367*a1e26a70SApple OSS Distributions 	}
368*a1e26a70SApple OSS Distributions 
369*a1e26a70SApple OSS Distributions 	if (ifclassq_enable_l4s) {
370*a1e26a70SApple OSS Distributions 		tfc_type = pktsched_is_pkt_l4s(pkt) ? FQ_TFC_L4S : FQ_TFC_C;
371*a1e26a70SApple OSS Distributions 	}
372*a1e26a70SApple OSS Distributions 
373*a1e26a70SApple OSS Distributions 	/*
374*a1e26a70SApple OSS Distributions 	 * Timestamps for every packet must be set prior to entering this path.
375*a1e26a70SApple OSS Distributions 	 */
376*a1e26a70SApple OSS Distributions 	now = *pkt_timestamp;
377*a1e26a70SApple OSS Distributions 	ASSERT(now > 0);
378*a1e26a70SApple OSS Distributions 
379*a1e26a70SApple OSS Distributions 	/* find the flowq for this packet */
380*a1e26a70SApple OSS Distributions 	fq = fq_if_hash_pkt(fqs, fq_grp, pkt_flowid, pktsched_get_pkt_svc(pkt),
381*a1e26a70SApple OSS Distributions 	    now, true, tfc_type);
382*a1e26a70SApple OSS Distributions 	if (__improbable(fq == NULL)) {
383*a1e26a70SApple OSS Distributions 		DTRACE_IP1(memfail__drop, fq_if_t *, fqs);
384*a1e26a70SApple OSS Distributions 		/* drop the packet if we could not allocate a flow queue */
385*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_drop_memfailure += cnt;
386*a1e26a70SApple OSS Distributions 		return CLASSQEQ_DROP;
387*a1e26a70SApple OSS Distributions 	}
388*a1e26a70SApple OSS Distributions 	VERIFY(fq->fq_group == fq_grp);
389*a1e26a70SApple OSS Distributions 	VERIFY(fqs->fqs_ptype == pkt->pktsched_ptype);
390*a1e26a70SApple OSS Distributions 
391*a1e26a70SApple OSS Distributions 	KDBG(AQM_KTRACE_STATS_FLOW_ENQUEUE, fq->fq_flowhash,
392*a1e26a70SApple OSS Distributions 	    AQM_KTRACE_FQ_GRP_SC_IDX(fq),
393*a1e26a70SApple OSS Distributions 	    fq->fq_bytes, pktsched_get_pkt_len(pkt));
394*a1e26a70SApple OSS Distributions 
395*a1e26a70SApple OSS Distributions 	fq_detect_dequeue_stall(fqs, fq, fq_cl, &now);
396*a1e26a70SApple OSS Distributions 
397*a1e26a70SApple OSS Distributions 	/*
398*a1e26a70SApple OSS Distributions 	 * Skip the dropping part if it's L4S. Flow control or ECN marking decision
399*a1e26a70SApple OSS Distributions 	 * will be made at dequeue time.
400*a1e26a70SApple OSS Distributions 	 */
401*a1e26a70SApple OSS Distributions 	if (ifclassq_enable_l4s && tfc_type == FQ_TFC_L4S) {
402*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_l4s_pkts += cnt;
403*a1e26a70SApple OSS Distributions 		droptype = DTYPE_NODROP;
404*a1e26a70SApple OSS Distributions 	}
405*a1e26a70SApple OSS Distributions 
406*a1e26a70SApple OSS Distributions 	if (__improbable(FQ_IS_DELAY_HIGH(fq) || FQ_IS_OVERWHELMING(fq))) {
407*a1e26a70SApple OSS Distributions 		if ((fq->fq_flags & FQF_FLOWCTL_CAPABLE) &&
408*a1e26a70SApple OSS Distributions 		    (*pkt_flags & PKTF_FLOW_ADV)) {
409*a1e26a70SApple OSS Distributions 			fc_adv = 1;
410*a1e26a70SApple OSS Distributions 			/*
411*a1e26a70SApple OSS Distributions 			 * If the flow is suspended or it is not
412*a1e26a70SApple OSS Distributions 			 * TCP/QUIC, drop the chain.
413*a1e26a70SApple OSS Distributions 			 */
414*a1e26a70SApple OSS Distributions 			if ((pkt_proto != IPPROTO_TCP) &&
415*a1e26a70SApple OSS Distributions 			    (pkt_proto != IPPROTO_QUIC)) {
416*a1e26a70SApple OSS Distributions 				droptype = DTYPE_EARLY;
417*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_drop_early += cnt;
418*a1e26a70SApple OSS Distributions 				IFCQ_DROP_ADD(fqs->fqs_ifq, cnt, pktsched_get_pkt_len(pkt));
419*a1e26a70SApple OSS Distributions 			}
420*a1e26a70SApple OSS Distributions 			DTRACE_IP6(flow__adv, fq_if_t *, fqs,
421*a1e26a70SApple OSS Distributions 			    fq_if_classq_t *, fq_cl, fq_t *, fq,
422*a1e26a70SApple OSS Distributions 			    int, droptype, pktsched_pkt_t *, pkt,
423*a1e26a70SApple OSS Distributions 			    uint32_t, cnt);
424*a1e26a70SApple OSS Distributions 		} else {
425*a1e26a70SApple OSS Distributions 			/*
426*a1e26a70SApple OSS Distributions 			 * Need to drop packets to make room for the new
427*a1e26a70SApple OSS Distributions 			 * ones. Try to drop from the head of the queue
428*a1e26a70SApple OSS Distributions 			 * instead of the latest packets.
429*a1e26a70SApple OSS Distributions 			 */
430*a1e26a70SApple OSS Distributions 			if (!fq_empty(fq, fqs->fqs_ptype)) {
431*a1e26a70SApple OSS Distributions 				uint32_t i;
432*a1e26a70SApple OSS Distributions 
433*a1e26a70SApple OSS Distributions 				for (i = 0; i < cnt; i++) {
434*a1e26a70SApple OSS Distributions 					fq_head_drop(fqs, fq);
435*a1e26a70SApple OSS Distributions 				}
436*a1e26a70SApple OSS Distributions 				droptype = DTYPE_NODROP;
437*a1e26a70SApple OSS Distributions 			} else {
438*a1e26a70SApple OSS Distributions 				droptype = DTYPE_EARLY;
439*a1e26a70SApple OSS Distributions 			}
440*a1e26a70SApple OSS Distributions 			fq_cl->fcl_stat.fcl_drop_early += cnt;
441*a1e26a70SApple OSS Distributions 
442*a1e26a70SApple OSS Distributions 			DTRACE_IP6(no__flow__adv, fq_if_t *, fqs,
443*a1e26a70SApple OSS Distributions 			    fq_if_classq_t *, fq_cl, fq_t *, fq,
444*a1e26a70SApple OSS Distributions 			    int, droptype, pktsched_pkt_t *, pkt,
445*a1e26a70SApple OSS Distributions 			    uint32_t, cnt);
446*a1e26a70SApple OSS Distributions 		}
447*a1e26a70SApple OSS Distributions 	}
448*a1e26a70SApple OSS Distributions 
449*a1e26a70SApple OSS Distributions 	/* Set the return code correctly */
450*a1e26a70SApple OSS Distributions 	if (__improbable(fc_adv == 1 && droptype != DTYPE_FORCED)) {
451*a1e26a70SApple OSS Distributions 		if (fq_if_add_fcentry(fqs, pkt, pkt_flowsrc, fq, fq_cl)) {
452*a1e26a70SApple OSS Distributions 			fq->fq_flags |= FQF_FLOWCTL_ON;
453*a1e26a70SApple OSS Distributions 			/* deliver flow control advisory error */
454*a1e26a70SApple OSS Distributions 			if (droptype == DTYPE_NODROP) {
455*a1e26a70SApple OSS Distributions 				ret = CLASSQEQ_SUCCESS_FC;
456*a1e26a70SApple OSS Distributions 			} else {
457*a1e26a70SApple OSS Distributions 				/* dropped due to flow control */
458*a1e26a70SApple OSS Distributions 				ret = CLASSQEQ_DROP_FC;
459*a1e26a70SApple OSS Distributions 			}
460*a1e26a70SApple OSS Distributions 		} else {
461*a1e26a70SApple OSS Distributions 			/*
462*a1e26a70SApple OSS Distributions 			 * if we could not flow control the flow, it is
463*a1e26a70SApple OSS Distributions 			 * better to drop
464*a1e26a70SApple OSS Distributions 			 */
465*a1e26a70SApple OSS Distributions 			droptype = DTYPE_FORCED;
466*a1e26a70SApple OSS Distributions 			ret = CLASSQEQ_DROP_FC;
467*a1e26a70SApple OSS Distributions 			fq_cl->fcl_stat.fcl_flow_control_fail++;
468*a1e26a70SApple OSS Distributions 		}
469*a1e26a70SApple OSS Distributions 		DTRACE_IP3(fc__ret, fq_if_t *, fqs, int, droptype, int, ret);
470*a1e26a70SApple OSS Distributions 	}
471*a1e26a70SApple OSS Distributions 
472*a1e26a70SApple OSS Distributions 	/*
473*a1e26a70SApple OSS Distributions 	 * If the queue length hits the queue limit, drop a chain with the
474*a1e26a70SApple OSS Distributions 	 * same number of packets from the front of the queue for a flow with
475*a1e26a70SApple OSS Distributions 	 * maximum number of bytes. This will penalize heavy and unresponsive
476*a1e26a70SApple OSS Distributions 	 * flows. It will also avoid a tail drop.
477*a1e26a70SApple OSS Distributions 	 */
478*a1e26a70SApple OSS Distributions 	if (__improbable(droptype == DTYPE_NODROP &&
479*a1e26a70SApple OSS Distributions 	    fq_if_at_drop_limit(fqs))) {
480*a1e26a70SApple OSS Distributions 		uint32_t i;
481*a1e26a70SApple OSS Distributions 
482*a1e26a70SApple OSS Distributions 		if (fqs->fqs_large_flow == fq) {
483*a1e26a70SApple OSS Distributions 			/*
484*a1e26a70SApple OSS Distributions 			 * Drop from the head of the current fq. Since a
485*a1e26a70SApple OSS Distributions 			 * new packet will be added to the tail, it is ok
486*a1e26a70SApple OSS Distributions 			 * to leave fq in place.
487*a1e26a70SApple OSS Distributions 			 */
488*a1e26a70SApple OSS Distributions 			DTRACE_IP5(large__flow, fq_if_t *, fqs,
489*a1e26a70SApple OSS Distributions 			    fq_if_classq_t *, fq_cl, fq_t *, fq,
490*a1e26a70SApple OSS Distributions 			    pktsched_pkt_t *, pkt, uint32_t, cnt);
491*a1e26a70SApple OSS Distributions 
492*a1e26a70SApple OSS Distributions 			for (i = 0; i < cnt; i++) {
493*a1e26a70SApple OSS Distributions 				fq_head_drop(fqs, fq);
494*a1e26a70SApple OSS Distributions 			}
495*a1e26a70SApple OSS Distributions 			fq_cl->fcl_stat.fcl_drop_overflow += cnt;
496*a1e26a70SApple OSS Distributions 
497*a1e26a70SApple OSS Distributions 			/*
498*a1e26a70SApple OSS Distributions 			 * TCP and QUIC will react to the loss of those head dropped pkts
499*a1e26a70SApple OSS Distributions 			 * and adjust send rate.
500*a1e26a70SApple OSS Distributions 			 */
501*a1e26a70SApple OSS Distributions 			if ((fq->fq_flags & FQF_FLOWCTL_CAPABLE) &&
502*a1e26a70SApple OSS Distributions 			    (*pkt_flags & PKTF_FLOW_ADV) &&
503*a1e26a70SApple OSS Distributions 			    (pkt_proto != IPPROTO_TCP) &&
504*a1e26a70SApple OSS Distributions 			    (pkt_proto != IPPROTO_QUIC)) {
505*a1e26a70SApple OSS Distributions 				if (fq_if_add_fcentry(fqs, pkt, pkt_flowsrc, fq, fq_cl)) {
506*a1e26a70SApple OSS Distributions 					fq->fq_flags |= FQF_FLOWCTL_ON;
507*a1e26a70SApple OSS Distributions 					FQ_SET_OVERWHELMING(fq);
508*a1e26a70SApple OSS Distributions 					fq_cl->fcl_stat.fcl_overwhelming++;
509*a1e26a70SApple OSS Distributions 					/* deliver flow control advisory error */
510*a1e26a70SApple OSS Distributions 					ret = CLASSQEQ_SUCCESS_FC;
511*a1e26a70SApple OSS Distributions 				}
512*a1e26a70SApple OSS Distributions 			}
513*a1e26a70SApple OSS Distributions 		} else {
514*a1e26a70SApple OSS Distributions 			if (fqs->fqs_large_flow == NULL) {
515*a1e26a70SApple OSS Distributions 				droptype = DTYPE_FORCED;
516*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_drop_overflow += cnt;
517*a1e26a70SApple OSS Distributions 				ret = CLASSQEQ_DROP;
518*a1e26a70SApple OSS Distributions 
519*a1e26a70SApple OSS Distributions 				DTRACE_IP5(no__large__flow, fq_if_t *, fqs,
520*a1e26a70SApple OSS Distributions 				    fq_if_classq_t *, fq_cl, fq_t *, fq,
521*a1e26a70SApple OSS Distributions 				    pktsched_pkt_t *, pkt, uint32_t, cnt);
522*a1e26a70SApple OSS Distributions 
523*a1e26a70SApple OSS Distributions 				/*
524*a1e26a70SApple OSS Distributions 				 * if this fq was freshly created and there
525*a1e26a70SApple OSS Distributions 				 * is nothing to enqueue, move it to empty list
526*a1e26a70SApple OSS Distributions 				 */
527*a1e26a70SApple OSS Distributions 				if (fq_empty(fq, fqs->fqs_ptype) &&
528*a1e26a70SApple OSS Distributions 				    !(fq->fq_flags & (FQF_NEW_FLOW |
529*a1e26a70SApple OSS Distributions 				    FQF_OLD_FLOW))) {
530*a1e26a70SApple OSS Distributions 					fq_if_move_to_empty_flow(fqs, fq_cl,
531*a1e26a70SApple OSS Distributions 					    fq, now);
532*a1e26a70SApple OSS Distributions 					fq = NULL;
533*a1e26a70SApple OSS Distributions 				}
534*a1e26a70SApple OSS Distributions 			} else {
535*a1e26a70SApple OSS Distributions 				DTRACE_IP5(different__large__flow,
536*a1e26a70SApple OSS Distributions 				    fq_if_t *, fqs, fq_if_classq_t *, fq_cl,
537*a1e26a70SApple OSS Distributions 				    fq_t *, fq, pktsched_pkt_t *, pkt,
538*a1e26a70SApple OSS Distributions 				    uint32_t, cnt);
539*a1e26a70SApple OSS Distributions 
540*a1e26a70SApple OSS Distributions 				for (i = 0; i < cnt; i++) {
541*a1e26a70SApple OSS Distributions 					fq_if_drop_packet(fqs, now);
542*a1e26a70SApple OSS Distributions 				}
543*a1e26a70SApple OSS Distributions 			}
544*a1e26a70SApple OSS Distributions 		}
545*a1e26a70SApple OSS Distributions 	}
546*a1e26a70SApple OSS Distributions 
547*a1e26a70SApple OSS Distributions 	fq_cl->fcl_flags &= ~FCL_PACED;
548*a1e26a70SApple OSS Distributions 
549*a1e26a70SApple OSS Distributions 	if (__probable(droptype == DTYPE_NODROP)) {
550*a1e26a70SApple OSS Distributions 		uint32_t chain_len = pktsched_get_pkt_len(pkt);
551*a1e26a70SApple OSS Distributions 		int ret_compress = 0;
552*a1e26a70SApple OSS Distributions 
553*a1e26a70SApple OSS Distributions 		/*
554*a1e26a70SApple OSS Distributions 		 * We do not compress if we are enqueuing a chain.
555*a1e26a70SApple OSS Distributions 		 * Traversing the chain to look for acks would defeat the
556*a1e26a70SApple OSS Distributions 		 * purpose of batch enqueueing.
557*a1e26a70SApple OSS Distributions 		 */
558*a1e26a70SApple OSS Distributions 		if (cnt == 1) {
559*a1e26a70SApple OSS Distributions 			ret_compress = fq_compressor(fqs, fq, fq_cl, pkt);
560*a1e26a70SApple OSS Distributions 			if (ret_compress == CLASSQEQ_COMPRESSED) {
561*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_pkts_compressed++;
562*a1e26a70SApple OSS Distributions 			}
563*a1e26a70SApple OSS Distributions 		}
564*a1e26a70SApple OSS Distributions 		DTRACE_IP5(fq_enqueue, fq_if_t *, fqs, fq_if_classq_t *, fq_cl,
565*a1e26a70SApple OSS Distributions 		    fq_t *, fq, pktsched_pkt_t *, pkt, uint32_t, cnt);
566*a1e26a70SApple OSS Distributions 		fq_enqueue(fq, pkt->pktsched_pkt, pkt->pktsched_tail, cnt,
567*a1e26a70SApple OSS Distributions 		    pkt->pktsched_ptype);
568*a1e26a70SApple OSS Distributions 
569*a1e26a70SApple OSS Distributions 		fq->fq_bytes += chain_len;
570*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_byte_cnt += chain_len;
571*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_pkt_cnt += cnt;
572*a1e26a70SApple OSS Distributions 
573*a1e26a70SApple OSS Distributions 		/*
574*a1e26a70SApple OSS Distributions 		 * check if this queue will qualify to be the next
575*a1e26a70SApple OSS Distributions 		 * victim queue
576*a1e26a70SApple OSS Distributions 		 */
577*a1e26a70SApple OSS Distributions 		fq_if_is_flow_heavy(fqs, fq);
578*a1e26a70SApple OSS Distributions 	} else {
579*a1e26a70SApple OSS Distributions 		DTRACE_IP3(fq_drop, fq_if_t *, fqs, int, droptype, int, ret);
580*a1e26a70SApple OSS Distributions 		return (ret != CLASSQEQ_SUCCESS) ? ret : CLASSQEQ_DROP;
581*a1e26a70SApple OSS Distributions 	}
582*a1e26a70SApple OSS Distributions 
583*a1e26a70SApple OSS Distributions 	/*
584*a1e26a70SApple OSS Distributions 	 * If the queue is not currently active, add it to the end of new
585*a1e26a70SApple OSS Distributions 	 * flows list for that service class.
586*a1e26a70SApple OSS Distributions 	 */
587*a1e26a70SApple OSS Distributions 	if ((fq->fq_flags & (FQF_NEW_FLOW | FQF_OLD_FLOW)) == 0) {
588*a1e26a70SApple OSS Distributions 		VERIFY(STAILQ_NEXT(fq, fq_actlink) == NULL);
589*a1e26a70SApple OSS Distributions 		STAILQ_INSERT_TAIL(&fq_cl->fcl_new_flows, fq, fq_actlink);
590*a1e26a70SApple OSS Distributions 		fq->fq_flags |= FQF_NEW_FLOW;
591*a1e26a70SApple OSS Distributions 
592*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_newflows_cnt++;
593*a1e26a70SApple OSS Distributions 
594*a1e26a70SApple OSS Distributions 		fq->fq_deficit = fq_cl->fcl_quantum;
595*a1e26a70SApple OSS Distributions 	}
596*a1e26a70SApple OSS Distributions 	return ret;
597*a1e26a70SApple OSS Distributions }
598*a1e26a70SApple OSS Distributions 
599*a1e26a70SApple OSS Distributions void
fq_getq_flow_internal(fq_if_t * fqs,fq_t * fq,pktsched_pkt_t * pkt)600*a1e26a70SApple OSS Distributions fq_getq_flow_internal(fq_if_t *fqs, fq_t *fq, pktsched_pkt_t *pkt)
601*a1e26a70SApple OSS Distributions {
602*a1e26a70SApple OSS Distributions 	classq_pkt_t p = CLASSQ_PKT_INITIALIZER(p);
603*a1e26a70SApple OSS Distributions 	uint32_t plen;
604*a1e26a70SApple OSS Distributions 	fq_if_classq_t *fq_cl;
605*a1e26a70SApple OSS Distributions 	struct ifclassq *ifq = fqs->fqs_ifq;
606*a1e26a70SApple OSS Distributions 
607*a1e26a70SApple OSS Distributions 	fq_dequeue(fq, &p, fqs->fqs_ptype);
608*a1e26a70SApple OSS Distributions 	if (p.cp_ptype == QP_INVALID) {
609*a1e26a70SApple OSS Distributions 		VERIFY(p.cp_mbuf == NULL);
610*a1e26a70SApple OSS Distributions 		return;
611*a1e26a70SApple OSS Distributions 	}
612*a1e26a70SApple OSS Distributions 
613*a1e26a70SApple OSS Distributions 	fq->fq_next_tx_time = FQ_INVALID_TX_TS;
614*a1e26a70SApple OSS Distributions 
615*a1e26a70SApple OSS Distributions 	pktsched_pkt_encap(pkt, &p);
616*a1e26a70SApple OSS Distributions 	plen = pktsched_get_pkt_len(pkt);
617*a1e26a70SApple OSS Distributions 
618*a1e26a70SApple OSS Distributions 	VERIFY(fq->fq_bytes >= plen);
619*a1e26a70SApple OSS Distributions 	fq->fq_bytes -= plen;
620*a1e26a70SApple OSS Distributions 
621*a1e26a70SApple OSS Distributions 	fq_cl = &FQ_CLASSQ(fq);
622*a1e26a70SApple OSS Distributions 	fq_cl->fcl_stat.fcl_byte_cnt -= plen;
623*a1e26a70SApple OSS Distributions 	fq_cl->fcl_stat.fcl_pkt_cnt--;
624*a1e26a70SApple OSS Distributions 	fq_cl->fcl_flags &= ~FCL_PACED;
625*a1e26a70SApple OSS Distributions 
626*a1e26a70SApple OSS Distributions 	IFCQ_DEC_LEN(ifq);
627*a1e26a70SApple OSS Distributions 	IFCQ_DEC_BYTES(ifq, plen);
628*a1e26a70SApple OSS Distributions 
629*a1e26a70SApple OSS Distributions 	FQ_GRP_DEC_LEN(fq);
630*a1e26a70SApple OSS Distributions 	FQ_GRP_DEC_BYTES(fq, plen);
631*a1e26a70SApple OSS Distributions 
632*a1e26a70SApple OSS Distributions 	/* Reset getqtime so that we don't count idle times */
633*a1e26a70SApple OSS Distributions 	if (fq_empty(fq, fqs->fqs_ptype)) {
634*a1e26a70SApple OSS Distributions 		fq->fq_getqtime = 0;
635*a1e26a70SApple OSS Distributions 	}
636*a1e26a70SApple OSS Distributions }
637*a1e26a70SApple OSS Distributions 
638*a1e26a70SApple OSS Distributions /*
639*a1e26a70SApple OSS Distributions  * fq_get_next_tx_time returns FQ_INVALID_TX_TS when there is no tx time in fq
640*a1e26a70SApple OSS Distributions  */
641*a1e26a70SApple OSS Distributions static uint64_t
fq_get_next_tx_time(fq_if_t * fqs,fq_t * fq)642*a1e26a70SApple OSS Distributions fq_get_next_tx_time(fq_if_t *fqs, fq_t *fq)
643*a1e26a70SApple OSS Distributions {
644*a1e26a70SApple OSS Distributions 	uint64_t tx_time = FQ_INVALID_TX_TS;
645*a1e26a70SApple OSS Distributions 
646*a1e26a70SApple OSS Distributions 	/*
647*a1e26a70SApple OSS Distributions 	 * Check the cached value in fq
648*a1e26a70SApple OSS Distributions 	 */
649*a1e26a70SApple OSS Distributions 	if (fq->fq_next_tx_time != FQ_INVALID_TX_TS) {
650*a1e26a70SApple OSS Distributions 		return fq->fq_next_tx_time;
651*a1e26a70SApple OSS Distributions 	}
652*a1e26a70SApple OSS Distributions 
653*a1e26a70SApple OSS Distributions 	switch (fqs->fqs_ptype) {
654*a1e26a70SApple OSS Distributions 	case QP_MBUF: {
655*a1e26a70SApple OSS Distributions 		struct mbuf *m;
656*a1e26a70SApple OSS Distributions 		if ((m = MBUFQ_FIRST(&fq->fq_mbufq)) != NULL) {
657*a1e26a70SApple OSS Distributions 			struct m_tag *tag;
658*a1e26a70SApple OSS Distributions 			tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID,
659*a1e26a70SApple OSS Distributions 			    KERNEL_TAG_TYPE_AQM);
660*a1e26a70SApple OSS Distributions 			if (tag != NULL) {
661*a1e26a70SApple OSS Distributions 				tx_time = *(uint64_t *)tag->m_tag_data;
662*a1e26a70SApple OSS Distributions 			}
663*a1e26a70SApple OSS Distributions 		}
664*a1e26a70SApple OSS Distributions 		break;
665*a1e26a70SApple OSS Distributions 	}
666*a1e26a70SApple OSS Distributions 	case QP_PACKET: {
667*a1e26a70SApple OSS Distributions 		struct __kern_packet *p = KPKTQ_FIRST(&fq->fq_kpktq);
668*a1e26a70SApple OSS Distributions 		if (__probable(p != NULL)) {
669*a1e26a70SApple OSS Distributions 			tx_time = __packet_get_tx_timestamp(SK_PKT2PH(p));
670*a1e26a70SApple OSS Distributions 		}
671*a1e26a70SApple OSS Distributions 		break;
672*a1e26a70SApple OSS Distributions 	}
673*a1e26a70SApple OSS Distributions 	default:
674*a1e26a70SApple OSS Distributions 		VERIFY(0);
675*a1e26a70SApple OSS Distributions 		/* NOTREACHED */
676*a1e26a70SApple OSS Distributions 		__builtin_unreachable();
677*a1e26a70SApple OSS Distributions 	}
678*a1e26a70SApple OSS Distributions 
679*a1e26a70SApple OSS Distributions 	/*
680*a1e26a70SApple OSS Distributions 	 * Cache the tx time in fq. The cache will be clear after dequeue or drop
681*a1e26a70SApple OSS Distributions 	 * from the fq.
682*a1e26a70SApple OSS Distributions 	 */
683*a1e26a70SApple OSS Distributions 	fq->fq_next_tx_time = tx_time;
684*a1e26a70SApple OSS Distributions 
685*a1e26a70SApple OSS Distributions 	return tx_time;
686*a1e26a70SApple OSS Distributions }
687*a1e26a70SApple OSS Distributions 
688*a1e26a70SApple OSS Distributions /*
689*a1e26a70SApple OSS Distributions  * fq_tx_time_ready returns true if the fq is empty so that it doesn't
690*a1e26a70SApple OSS Distributions  * affect caller logics that handles empty flow.
691*a1e26a70SApple OSS Distributions  */
692*a1e26a70SApple OSS Distributions boolean_t
fq_tx_time_ready(fq_if_t * fqs,fq_t * fq,uint64_t now,uint64_t * ready_time)693*a1e26a70SApple OSS Distributions fq_tx_time_ready(fq_if_t *fqs, fq_t *fq, uint64_t now, uint64_t *ready_time)
694*a1e26a70SApple OSS Distributions {
695*a1e26a70SApple OSS Distributions 	uint64_t pkt_tx_time;
696*a1e26a70SApple OSS Distributions 	fq_if_classq_t *fq_cl = &FQ_CLASSQ(fq);
697*a1e26a70SApple OSS Distributions 
698*a1e26a70SApple OSS Distributions 	if (!ifclassq_enable_pacing || !ifclassq_enable_l4s || fq->fq_tfc_type != FQ_TFC_L4S) {
699*a1e26a70SApple OSS Distributions 		return TRUE;
700*a1e26a70SApple OSS Distributions 	}
701*a1e26a70SApple OSS Distributions 
702*a1e26a70SApple OSS Distributions 	pkt_tx_time = fq_get_next_tx_time(fqs, fq);
703*a1e26a70SApple OSS Distributions 	if (ready_time != NULL) {
704*a1e26a70SApple OSS Distributions 		*ready_time = pkt_tx_time;
705*a1e26a70SApple OSS Distributions 	}
706*a1e26a70SApple OSS Distributions 
707*a1e26a70SApple OSS Distributions 	if (pkt_tx_time <= now + pkt_pacing_leeway ||
708*a1e26a70SApple OSS Distributions 	    pkt_tx_time == FQ_INVALID_TX_TS) {
709*a1e26a70SApple OSS Distributions 		return TRUE;
710*a1e26a70SApple OSS Distributions 	}
711*a1e26a70SApple OSS Distributions 
712*a1e26a70SApple OSS Distributions 	/*
713*a1e26a70SApple OSS Distributions 	 * Ignore the tx time if it's scheduled too far in the future
714*a1e26a70SApple OSS Distributions 	 */
715*a1e26a70SApple OSS Distributions 	if (pkt_tx_time > max_pkt_pacing_interval + now) {
716*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_ignore_tx_time++;
717*a1e26a70SApple OSS Distributions 		return TRUE;
718*a1e26a70SApple OSS Distributions 	}
719*a1e26a70SApple OSS Distributions 
720*a1e26a70SApple OSS Distributions 	ASSERT(pkt_tx_time != FQ_INVALID_TX_TS);
721*a1e26a70SApple OSS Distributions 	KDBG(AQM_KTRACE_TX_NOT_READY, fq->fq_flowhash,
722*a1e26a70SApple OSS Distributions 	    AQM_KTRACE_FQ_GRP_SC_IDX(fq), now, pkt_tx_time);
723*a1e26a70SApple OSS Distributions 	return FALSE;
724*a1e26a70SApple OSS Distributions }
725*a1e26a70SApple OSS Distributions 
726*a1e26a70SApple OSS Distributions void
fq_getq_flow(fq_if_t * fqs,fq_t * fq,pktsched_pkt_t * pkt,uint64_t now)727*a1e26a70SApple OSS Distributions fq_getq_flow(fq_if_t *fqs, fq_t *fq, pktsched_pkt_t *pkt, uint64_t now)
728*a1e26a70SApple OSS Distributions {
729*a1e26a70SApple OSS Distributions 	fq_if_classq_t *fq_cl = &FQ_CLASSQ(fq);
730*a1e26a70SApple OSS Distributions 	int64_t qdelay = 0;
731*a1e26a70SApple OSS Distributions 	volatile uint32_t *__single pkt_flags;
732*a1e26a70SApple OSS Distributions 	uint64_t *__single pkt_timestamp, pkt_tx_time = 0, pacing_delay = 0;
733*a1e26a70SApple OSS Distributions 	uint64_t fq_min_delay_threshold = FQ_TARGET_DELAY(fq);
734*a1e26a70SApple OSS Distributions 	uint8_t pkt_flowsrc;
735*a1e26a70SApple OSS Distributions 	boolean_t l4s_pkt;
736*a1e26a70SApple OSS Distributions 
737*a1e26a70SApple OSS Distributions 	fq_getq_flow_internal(fqs, fq, pkt);
738*a1e26a70SApple OSS Distributions 	if (pkt->pktsched_ptype == QP_INVALID) {
739*a1e26a70SApple OSS Distributions 		VERIFY(pkt->pktsched_pkt_mbuf == NULL);
740*a1e26a70SApple OSS Distributions 		return;
741*a1e26a70SApple OSS Distributions 	}
742*a1e26a70SApple OSS Distributions 
743*a1e26a70SApple OSS Distributions 	pktsched_get_pkt_vars(pkt, &pkt_flags, &pkt_timestamp, NULL, &pkt_flowsrc,
744*a1e26a70SApple OSS Distributions 	    NULL, NULL, &pkt_tx_time);
745*a1e26a70SApple OSS Distributions 	l4s_pkt = pktsched_is_pkt_l4s(pkt);
746*a1e26a70SApple OSS Distributions 	if (ifclassq_enable_pacing && ifclassq_enable_l4s) {
747*a1e26a70SApple OSS Distributions 		if (pkt_tx_time > *pkt_timestamp) {
748*a1e26a70SApple OSS Distributions 			pacing_delay = pkt_tx_time - *pkt_timestamp;
749*a1e26a70SApple OSS Distributions 			fq_cl->fcl_stat.fcl_paced_pkts++;
750*a1e26a70SApple OSS Distributions 			DTRACE_SKYWALK3(aqm__pacing__delta, uint64_t, now - pkt_tx_time,
751*a1e26a70SApple OSS Distributions 			    fq_if_t *, fqs, fq_t *, fq);
752*a1e26a70SApple OSS Distributions 		}
753*a1e26a70SApple OSS Distributions #if (DEVELOPMENT || DEBUG)
754*a1e26a70SApple OSS Distributions 		else if (pkt_tx_time != 0) {
755*a1e26a70SApple OSS Distributions 			DTRACE_SKYWALK5(aqm__miss__pacing__delay, uint64_t, *pkt_timestamp,
756*a1e26a70SApple OSS Distributions 			    uint64_t, pkt_tx_time, uint64_t, now, fq_if_t *,
757*a1e26a70SApple OSS Distributions 			    fqs, fq_t *, fq);
758*a1e26a70SApple OSS Distributions 		}
759*a1e26a70SApple OSS Distributions #endif // (DEVELOPMENT || DEBUG)
760*a1e26a70SApple OSS Distributions 	}
761*a1e26a70SApple OSS Distributions 
762*a1e26a70SApple OSS Distributions 	/* this will compute qdelay in nanoseconds */
763*a1e26a70SApple OSS Distributions 	if (now > *pkt_timestamp) {
764*a1e26a70SApple OSS Distributions 		qdelay = now - *pkt_timestamp;
765*a1e26a70SApple OSS Distributions 	}
766*a1e26a70SApple OSS Distributions 
767*a1e26a70SApple OSS Distributions 	/* Update min/max/avg qdelay for the respective class */
768*a1e26a70SApple OSS Distributions 	if (fq_cl->fcl_stat.fcl_min_qdelay == 0 ||
769*a1e26a70SApple OSS Distributions 	    (qdelay > 0 && (u_int64_t)qdelay < fq_cl->fcl_stat.fcl_min_qdelay)) {
770*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_min_qdelay = qdelay;
771*a1e26a70SApple OSS Distributions 	}
772*a1e26a70SApple OSS Distributions 
773*a1e26a70SApple OSS Distributions 	if (fq_cl->fcl_stat.fcl_max_qdelay == 0 ||
774*a1e26a70SApple OSS Distributions 	    (qdelay > 0 && (u_int64_t)qdelay > fq_cl->fcl_stat.fcl_max_qdelay)) {
775*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_max_qdelay = qdelay;
776*a1e26a70SApple OSS Distributions 	}
777*a1e26a70SApple OSS Distributions 
778*a1e26a70SApple OSS Distributions 	uint64_t num_dequeues = fq_cl->fcl_stat.fcl_dequeue;
779*a1e26a70SApple OSS Distributions 
780*a1e26a70SApple OSS Distributions 	if (num_dequeues == 0) {
781*a1e26a70SApple OSS Distributions 		fq_cl->fcl_stat.fcl_avg_qdelay = qdelay;
782*a1e26a70SApple OSS Distributions 	} else if (qdelay > 0) {
783*a1e26a70SApple OSS Distributions 		uint64_t res = 0;
784*a1e26a70SApple OSS Distributions 		if (os_add_overflow(num_dequeues, 1, &res)) {
785*a1e26a70SApple OSS Distributions 			/* Reset the dequeue num and dequeue bytes */
786*a1e26a70SApple OSS Distributions 			fq_cl->fcl_stat.fcl_dequeue = num_dequeues = 0;
787*a1e26a70SApple OSS Distributions 			fq_cl->fcl_stat.fcl_dequeue_bytes = 0;
788*a1e26a70SApple OSS Distributions 			fq_cl->fcl_stat.fcl_avg_qdelay = qdelay;
789*a1e26a70SApple OSS Distributions 			os_log_info(OS_LOG_DEFAULT, "%s: dequeue num overflow, "
790*a1e26a70SApple OSS Distributions 			    "flow: 0x%x, iface: %s", __func__, fq->fq_flowhash,
791*a1e26a70SApple OSS Distributions 			    if_name(fqs->fqs_ifq->ifcq_ifp));
792*a1e26a70SApple OSS Distributions 		} else {
793*a1e26a70SApple OSS Distributions 			uint64_t product = 0;
794*a1e26a70SApple OSS Distributions 			if (os_mul_overflow(fq_cl->fcl_stat.fcl_avg_qdelay,
795*a1e26a70SApple OSS Distributions 			    num_dequeues, &product) || os_add_overflow(product, qdelay, &res)) {
796*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_avg_qdelay = qdelay;
797*a1e26a70SApple OSS Distributions 			} else {
798*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_avg_qdelay = res /
799*a1e26a70SApple OSS Distributions 				    (num_dequeues + 1);
800*a1e26a70SApple OSS Distributions 			}
801*a1e26a70SApple OSS Distributions 		}
802*a1e26a70SApple OSS Distributions 	}
803*a1e26a70SApple OSS Distributions 
804*a1e26a70SApple OSS Distributions 	fq->fq_pkts_since_last_report++;
805*a1e26a70SApple OSS Distributions 	if (ifclassq_enable_l4s && l4s_pkt) {
806*a1e26a70SApple OSS Distributions 		/*
807*a1e26a70SApple OSS Distributions 		 * A safe guard to make sure that L4S is not going to build a huge
808*a1e26a70SApple OSS Distributions 		 * queue if we encounter unexpected problems (for eg., if ACKs don't
809*a1e26a70SApple OSS Distributions 		 * arrive in timely manner due to congestion in reverse path).
810*a1e26a70SApple OSS Distributions 		 */
811*a1e26a70SApple OSS Distributions 		fq_min_delay_threshold = l4s_min_delay_threshold;
812*a1e26a70SApple OSS Distributions 
813*a1e26a70SApple OSS Distributions 		if ((l4s_ce_threshold != 0 && qdelay > l4s_ce_threshold + pacing_delay) ||
814*a1e26a70SApple OSS Distributions 		    (l4s_ce_threshold == 0 && qdelay > FQ_TARGET_DELAY(fq) + pacing_delay)) {
815*a1e26a70SApple OSS Distributions 			DTRACE_SKYWALK4(aqm__mark__ce, uint64_t, qdelay, uint64_t, pacing_delay,
816*a1e26a70SApple OSS Distributions 			    fq_if_t *, fqs, fq_t *, fq);
817*a1e26a70SApple OSS Distributions 			KDBG(AQM_KTRACE_STATS_FLOW_REPORT_CE, fq->fq_flowhash,
818*a1e26a70SApple OSS Distributions 			    AQM_KTRACE_FQ_GRP_SC_IDX(fq), qdelay, pacing_delay);
819*a1e26a70SApple OSS Distributions 			/*
820*a1e26a70SApple OSS Distributions 			 * The packet buffer that pktsched_mark_ecn writes to can be pageable.
821*a1e26a70SApple OSS Distributions 			 * Since it is not safe to write to pageable memory while preemption
822*a1e26a70SApple OSS Distributions 			 * is disabled, convert the spin lock into mutex.
823*a1e26a70SApple OSS Distributions 			 */
824*a1e26a70SApple OSS Distributions 			IFCQ_CONVERT_LOCK(fqs->fqs_ifq);
825*a1e26a70SApple OSS Distributions 			if (__improbable(l4s_local_ce_report != 0) &&
826*a1e26a70SApple OSS Distributions 			    (*pkt_flags & PKTF_FLOW_ADV) != 0 &&
827*a1e26a70SApple OSS Distributions 			    fq_if_report_ce(fqs, pkt, 1, fq->fq_pkts_since_last_report)) {
828*a1e26a70SApple OSS Distributions 				fq->fq_pkts_since_last_report = 0;
829*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_ce_reported++;
830*a1e26a70SApple OSS Distributions 			} else if (pktsched_mark_ecn(pkt) == 0) {
831*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_ce_marked++;
832*a1e26a70SApple OSS Distributions 			} else {
833*a1e26a70SApple OSS Distributions 				fq_cl->fcl_stat.fcl_ce_mark_failures++;
834*a1e26a70SApple OSS Distributions 			}
835*a1e26a70SApple OSS Distributions 		}
836*a1e26a70SApple OSS Distributions 	}
837*a1e26a70SApple OSS Distributions 
838*a1e26a70SApple OSS Distributions 	ASSERT(pacing_delay <= INT64_MAX);
839*a1e26a70SApple OSS Distributions 	qdelay = MAX(0, qdelay - (int64_t)pacing_delay);
840*a1e26a70SApple OSS Distributions 	if (fq->fq_min_qdelay == 0 ||
841*a1e26a70SApple OSS Distributions 	    (u_int64_t)qdelay < fq->fq_min_qdelay) {
842*a1e26a70SApple OSS Distributions 		fq->fq_min_qdelay = qdelay;
843*a1e26a70SApple OSS Distributions 	}
844*a1e26a70SApple OSS Distributions 
845*a1e26a70SApple OSS Distributions 	if (now >= fq->fq_updatetime) {
846*a1e26a70SApple OSS Distributions 		if (fq->fq_min_qdelay > fq_min_delay_threshold) {
847*a1e26a70SApple OSS Distributions 			if (!FQ_IS_DELAY_HIGH(fq)) {
848*a1e26a70SApple OSS Distributions 				FQ_SET_DELAY_HIGH(fq);
849*a1e26a70SApple OSS Distributions 			}
850*a1e26a70SApple OSS Distributions 		} else {
851*a1e26a70SApple OSS Distributions 			FQ_CLEAR_DELAY_HIGH(fq);
852*a1e26a70SApple OSS Distributions 		}
853*a1e26a70SApple OSS Distributions 		/* Reset measured queue delay and update time */
854*a1e26a70SApple OSS Distributions 		fq->fq_updatetime = now + FQ_UPDATE_INTERVAL(fq);
855*a1e26a70SApple OSS Distributions 		fq->fq_min_qdelay = 0;
856*a1e26a70SApple OSS Distributions 	}
857*a1e26a70SApple OSS Distributions 
858*a1e26a70SApple OSS Distributions 	if (fqs->fqs_large_flow != fq || !fq_if_almost_at_drop_limit(fqs)) {
859*a1e26a70SApple OSS Distributions 		FQ_CLEAR_OVERWHELMING(fq);
860*a1e26a70SApple OSS Distributions 	}
861*a1e26a70SApple OSS Distributions 	if (!FQ_IS_DELAY_HIGH(fq) || fq_empty(fq, fqs->fqs_ptype)) {
862*a1e26a70SApple OSS Distributions 		FQ_CLEAR_DELAY_HIGH(fq);
863*a1e26a70SApple OSS Distributions 	}
864*a1e26a70SApple OSS Distributions 
865*a1e26a70SApple OSS Distributions 	if ((fq->fq_flags & FQF_FLOWCTL_ON) &&
866*a1e26a70SApple OSS Distributions 	    !FQ_IS_DELAY_HIGH(fq) && !FQ_IS_OVERWHELMING(fq)) {
867*a1e26a70SApple OSS Distributions 		fq_if_flow_feedback(fqs, fq, fq_cl);
868*a1e26a70SApple OSS Distributions 	}
869*a1e26a70SApple OSS Distributions 
870*a1e26a70SApple OSS Distributions 	if (fq_empty(fq, fqs->fqs_ptype)) {
871*a1e26a70SApple OSS Distributions 		/* Reset getqtime so that we don't count idle times */
872*a1e26a70SApple OSS Distributions 		fq->fq_getqtime = 0;
873*a1e26a70SApple OSS Distributions 	} else {
874*a1e26a70SApple OSS Distributions 		fq->fq_getqtime = now;
875*a1e26a70SApple OSS Distributions 	}
876*a1e26a70SApple OSS Distributions 	fq_if_is_flow_heavy(fqs, fq);
877*a1e26a70SApple OSS Distributions 
878*a1e26a70SApple OSS Distributions 	*pkt_timestamp = 0;
879*a1e26a70SApple OSS Distributions 	switch (pkt->pktsched_ptype) {
880*a1e26a70SApple OSS Distributions 	case QP_MBUF:
881*a1e26a70SApple OSS Distributions 		*pkt_flags &= ~PKTF_PRIV_GUARDED;
882*a1e26a70SApple OSS Distributions 		break;
883*a1e26a70SApple OSS Distributions #if SKYWALK
884*a1e26a70SApple OSS Distributions 	case QP_PACKET:
885*a1e26a70SApple OSS Distributions 		/* sanity check */
886*a1e26a70SApple OSS Distributions 		ASSERT((*pkt_flags & ~PKT_F_COMMON_MASK) == 0);
887*a1e26a70SApple OSS Distributions 		break;
888*a1e26a70SApple OSS Distributions #endif /* SKYWALK */
889*a1e26a70SApple OSS Distributions 	default:
890*a1e26a70SApple OSS Distributions 		VERIFY(0);
891*a1e26a70SApple OSS Distributions 		/* NOTREACHED */
892*a1e26a70SApple OSS Distributions 		__builtin_unreachable();
893*a1e26a70SApple OSS Distributions 	}
894*a1e26a70SApple OSS Distributions }
895