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