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