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