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