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