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