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