1*42e22086SApple OSS Distributions /* 2*42e22086SApple OSS Distributions * Copyright (c) 2016-2021 Apple Inc. All rights reserved. 3*42e22086SApple OSS Distributions * 4*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*42e22086SApple OSS Distributions * 6*42e22086SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*42e22086SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*42e22086SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*42e22086SApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*42e22086SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*42e22086SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*42e22086SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*42e22086SApple OSS Distributions * terms of an Apple operating system software license agreement. 14*42e22086SApple OSS Distributions * 15*42e22086SApple OSS Distributions * Please obtain a copy of the License at 16*42e22086SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*42e22086SApple OSS Distributions * 18*42e22086SApple OSS Distributions * The Original Code and all software distributed under the License are 19*42e22086SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*42e22086SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*42e22086SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*42e22086SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*42e22086SApple OSS Distributions * Please see the License for the specific language governing rights and 24*42e22086SApple OSS Distributions * limitations under the License. 25*42e22086SApple OSS Distributions * 26*42e22086SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*42e22086SApple OSS Distributions */ 28*42e22086SApple OSS Distributions 29*42e22086SApple OSS Distributions #ifndef _NET_CLASSQ_CLASSQ_FQ_CODEL_H 30*42e22086SApple OSS Distributions #define _NET_CLASSQ_CLASSQ_FQ_CODEL_H 31*42e22086SApple OSS Distributions #ifdef PRIVATE 32*42e22086SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE 33*42e22086SApple OSS Distributions #include <stdbool.h> 34*42e22086SApple OSS Distributions #include <sys/time.h> 35*42e22086SApple OSS Distributions #include <net/flowadv.h> 36*42e22086SApple OSS Distributions #include <net/classq/if_classq.h> 37*42e22086SApple OSS Distributions #if SKYWALK 38*42e22086SApple OSS Distributions #include <skywalk/os_skywalk_private.h> 39*42e22086SApple OSS Distributions #endif /* SKYWALK */ 40*42e22086SApple OSS Distributions 41*42e22086SApple OSS Distributions #ifdef __cplusplus 42*42e22086SApple OSS Distributions extern "C" { 43*42e22086SApple OSS Distributions #endif 44*42e22086SApple OSS Distributions 45*42e22086SApple OSS Distributions #define AQM_KTRACE_AON_FLOW_HIGH_DELAY AQMDBG_CODE(DBG_AQM_ALWAYSON, 0x001) 46*42e22086SApple OSS Distributions #define AQM_KTRACE_AON_THROTTLE AQMDBG_CODE(DBG_AQM_ALWAYSON, 0x002) 47*42e22086SApple OSS Distributions #define AQM_KTRACE_AON_FLOW_OVERWHELMING AQMDBG_CODE(DBG_AQM_ALWAYSON, 0x003) 48*42e22086SApple OSS Distributions #define AQM_KTRACE_AON_FLOW_DQ_STALL AQMDBG_CODE(DBG_AQM_ALWAYSON, 0x004) 49*42e22086SApple OSS Distributions 50*42e22086SApple OSS Distributions #define AQM_KTRACE_STATS_FLOW_ENQUEUE AQMDBG_CODE(DBG_AQM_STATS, 0x001) 51*42e22086SApple OSS Distributions #define AQM_KTRACE_STATS_FLOW_DEQUEUE AQMDBG_CODE(DBG_AQM_STATS, 0x002) 52*42e22086SApple OSS Distributions #define AQM_KTRACE_STATS_FLOW_CTL AQMDBG_CODE(DBG_AQM_STATS, 0x003) 53*42e22086SApple OSS Distributions #define AQM_KTRACE_STATS_FLOW_ALLOC AQMDBG_CODE(DBG_AQM_STATS, 0x004) 54*42e22086SApple OSS Distributions #define AQM_KTRACE_STATS_FLOW_DESTROY AQMDBG_CODE(DBG_AQM_STATS, 0x005) 55*42e22086SApple OSS Distributions 56*42e22086SApple OSS Distributions #define AQM_KTRACE_FQ_GRP_SC_IDX(_fq_) \ 57*42e22086SApple OSS Distributions ((_fq_)->fq_group->fqg_index << 4 | (_fq_)->fq_sc_index) 58*42e22086SApple OSS Distributions 59*42e22086SApple OSS Distributions #define FQ_MIN_FC_THRESHOLD_BYTES 7500 60*42e22086SApple OSS Distributions #define FQ_IS_DELAY_HIGH(_fq_) ((_fq_)->fq_flags & FQF_DELAY_HIGH) 61*42e22086SApple OSS Distributions #define FQ_SET_DELAY_HIGH(_fq_) do { \ 62*42e22086SApple OSS Distributions if (!FQ_IS_DELAY_HIGH(_fq_)) { \ 63*42e22086SApple OSS Distributions KDBG(AQM_KTRACE_AON_FLOW_HIGH_DELAY | DBG_FUNC_START, \ 64*42e22086SApple OSS Distributions (_fq_)->fq_flowhash, AQM_KTRACE_FQ_GRP_SC_IDX((_fq_)), \ 65*42e22086SApple OSS Distributions (_fq_)->fq_bytes, (_fq_)->fq_min_qdelay); \ 66*42e22086SApple OSS Distributions } \ 67*42e22086SApple OSS Distributions (_fq_)->fq_flags |= FQF_DELAY_HIGH; \ 68*42e22086SApple OSS Distributions } while (0) 69*42e22086SApple OSS Distributions #define FQ_CLEAR_DELAY_HIGH(_fq_) do { \ 70*42e22086SApple OSS Distributions if (FQ_IS_DELAY_HIGH(_fq_)) { \ 71*42e22086SApple OSS Distributions KDBG(AQM_KTRACE_AON_FLOW_HIGH_DELAY | DBG_FUNC_END, \ 72*42e22086SApple OSS Distributions (_fq_)->fq_flowhash, AQM_KTRACE_FQ_GRP_SC_IDX((_fq_)), \ 73*42e22086SApple OSS Distributions (_fq_)->fq_bytes, (_fq_)->fq_min_qdelay); \ 74*42e22086SApple OSS Distributions } \ 75*42e22086SApple OSS Distributions (_fq_)->fq_flags &= ~FQF_DELAY_HIGH; \ 76*42e22086SApple OSS Distributions } while (0) 77*42e22086SApple OSS Distributions 78*42e22086SApple OSS Distributions #define FQ_IS_OVERWHELMING(_fq_) ((_fq_)->fq_flags & FQF_OVERWHELMING) 79*42e22086SApple OSS Distributions #define FQ_SET_OVERWHELMING(_fq_) do { \ 80*42e22086SApple OSS Distributions if (!FQ_IS_OVERWHELMING(_fq_)) { \ 81*42e22086SApple OSS Distributions KDBG(AQM_KTRACE_AON_FLOW_OVERWHELMING | DBG_FUNC_START, \ 82*42e22086SApple OSS Distributions (_fq_)->fq_flowhash, AQM_KTRACE_FQ_GRP_SC_IDX((_fq_)), \ 83*42e22086SApple OSS Distributions (_fq_)->fq_bytes, (_fq_)->fq_min_qdelay); \ 84*42e22086SApple OSS Distributions } \ 85*42e22086SApple OSS Distributions (_fq_)->fq_flags |= FQF_OVERWHELMING; \ 86*42e22086SApple OSS Distributions } while (0) 87*42e22086SApple OSS Distributions #define FQ_CLEAR_OVERWHELMING(_fq_) do { \ 88*42e22086SApple OSS Distributions if (FQ_IS_OVERWHELMING(_fq_)) { \ 89*42e22086SApple OSS Distributions KDBG(AQM_KTRACE_AON_FLOW_OVERWHELMING | DBG_FUNC_END, \ 90*42e22086SApple OSS Distributions (_fq_)->fq_flowhash, AQM_KTRACE_FQ_GRP_SC_IDX((_fq_)), \ 91*42e22086SApple OSS Distributions (_fq_)->fq_bytes, (_fq_)->fq_min_qdelay); \ 92*42e22086SApple OSS Distributions } \ 93*42e22086SApple OSS Distributions (_fq_)->fq_flags &= ~FQF_OVERWHELMING; \ 94*42e22086SApple OSS Distributions } while (0) 95*42e22086SApple OSS Distributions 96*42e22086SApple OSS Distributions /* 97*42e22086SApple OSS Distributions * time (in ns) the flow queue can stay in empty state. 98*42e22086SApple OSS Distributions */ 99*42e22086SApple OSS Distributions #define FQ_EMPTY_PURGE_DELAY (3ULL * 1000 * 1000 * 1000) 100*42e22086SApple OSS Distributions 101*42e22086SApple OSS Distributions /* 102*42e22086SApple OSS Distributions * maximum number of flow queues which can be purged during a dequeue. 103*42e22086SApple OSS Distributions */ 104*42e22086SApple OSS Distributions #define FQ_EMPTY_PURGE_MAX 4 105*42e22086SApple OSS Distributions 106*42e22086SApple OSS Distributions struct flowq { 107*42e22086SApple OSS Distributions #pragma pack(push,1) 108*42e22086SApple OSS Distributions union { 109*42e22086SApple OSS Distributions MBUFQ_HEAD(mbufq_head) __mbufq; /* mbuf packet queue */ 110*42e22086SApple OSS Distributions #if SKYWALK 111*42e22086SApple OSS Distributions KPKTQ_HEAD(kpktq_head) __kpktq; /* skywalk packet queue */ 112*42e22086SApple OSS Distributions #endif /* SKYWALK */ 113*42e22086SApple OSS Distributions } __fq_pktq_u; 114*42e22086SApple OSS Distributions #pragma pack(pop) 115*42e22086SApple OSS Distributions uint32_t fq_flowhash; /* Flow hash */ 116*42e22086SApple OSS Distributions uint32_t fq_bytes; /* Number of bytes in the queue */ 117*42e22086SApple OSS Distributions int32_t fq_deficit; /* Deficit for scheduling */ 118*42e22086SApple OSS Distributions fq_if_group_t *fq_group; /* Back pointer to the group */ 119*42e22086SApple OSS Distributions #define FQF_FLOWCTL_CAPABLE 0x01 /* Use flow control instead of drop */ 120*42e22086SApple OSS Distributions #define FQF_DELAY_HIGH 0x02 /* Min delay is greater than target */ 121*42e22086SApple OSS Distributions #define FQF_NEW_FLOW 0x04 /* Currently on new flows queue */ 122*42e22086SApple OSS Distributions #define FQF_OLD_FLOW 0x08 /* Currently on old flows queue */ 123*42e22086SApple OSS Distributions #define FQF_FLOWCTL_ON 0x10 /* Currently flow controlled */ 124*42e22086SApple OSS Distributions #define FQF_EMPTY_FLOW 0x20 /* Currently on empty flows queue */ 125*42e22086SApple OSS Distributions #define FQF_OVERWHELMING 0x40 /* The largest flow when AQM hits queue limit */ 126*42e22086SApple OSS Distributions uint8_t fq_flags; /* flags */ 127*42e22086SApple OSS Distributions uint8_t fq_sc_index; /* service_class index */ 128*42e22086SApple OSS Distributions bool fq_in_dqlist; 129*42e22086SApple OSS Distributions fq_tfc_type_t fq_tfc_type; 130*42e22086SApple OSS Distributions uint8_t __fq_pad_uint8[4]; 131*42e22086SApple OSS Distributions uint64_t fq_min_qdelay; /* min queue delay for Codel */ 132*42e22086SApple OSS Distributions uint64_t fq_getqtime; /* last dequeue time */ 133*42e22086SApple OSS Distributions union { 134*42e22086SApple OSS Distributions uint64_t fq_updatetime; /* next update interval */ 135*42e22086SApple OSS Distributions /* empty list purge time (in nanoseconds) */ 136*42e22086SApple OSS Distributions uint64_t fq_empty_purge_time; 137*42e22086SApple OSS Distributions }; 138*42e22086SApple OSS Distributions SLIST_ENTRY(flowq) fq_hashlink; /* for flow queue hash table */ 139*42e22086SApple OSS Distributions /* 140*42e22086SApple OSS Distributions * flow queue will only be on either one of the lists. 141*42e22086SApple OSS Distributions */ 142*42e22086SApple OSS Distributions union { 143*42e22086SApple OSS Distributions STAILQ_ENTRY(flowq) fq_actlink; /* for new/old flow queues */ 144*42e22086SApple OSS Distributions /* entry on empty flow queue list */ 145*42e22086SApple OSS Distributions TAILQ_ENTRY(flowq) fq_empty_link; 146*42e22086SApple OSS Distributions }; 147*42e22086SApple OSS Distributions /* entry on dequeue flow list */ 148*42e22086SApple OSS Distributions STAILQ_ENTRY(flowq) fq_dqlink; 149*42e22086SApple OSS Distributions /* temporary packet queue for dequeued packets */ 150*42e22086SApple OSS Distributions classq_pkt_t fq_dq_head; 151*42e22086SApple OSS Distributions classq_pkt_t fq_dq_tail; 152*42e22086SApple OSS Distributions }; 153*42e22086SApple OSS Distributions 154*42e22086SApple OSS Distributions typedef struct flowq fq_t; 155*42e22086SApple OSS Distributions 156*42e22086SApple OSS Distributions #define FQF_FLOW_STATE_MASK (FQF_DELAY_HIGH | FQF_NEW_FLOW | \ 157*42e22086SApple OSS Distributions FQF_OLD_FLOW | FQF_FLOWCTL_ON | FQF_EMPTY_FLOW) 158*42e22086SApple OSS Distributions 159*42e22086SApple OSS Distributions #define fq_mbufq __fq_pktq_u.__mbufq 160*42e22086SApple OSS Distributions #if SKYWALK 161*42e22086SApple OSS Distributions #define fq_kpktq __fq_pktq_u.__kpktq 162*42e22086SApple OSS Distributions #endif /* SKYWALK */ 163*42e22086SApple OSS Distributions 164*42e22086SApple OSS Distributions #if SKYWALK 165*42e22086SApple OSS Distributions #define fq_empty(_q, _ptype) (((_ptype) == QP_MBUF) ? \ 166*42e22086SApple OSS Distributions MBUFQ_EMPTY(&(_q)->fq_mbufq) : KPKTQ_EMPTY(&(_q)->fq_kpktq)) 167*42e22086SApple OSS Distributions #else /* !SKYWALK */ 168*42e22086SApple OSS Distributions #define fq_empty(_q, _ptype) MBUFQ_EMPTY(&(_q)->fq_mbufq) 169*42e22086SApple OSS Distributions #endif /* !SKYWALK */ 170*42e22086SApple OSS Distributions 171*42e22086SApple OSS Distributions #if SKYWALK 172*42e22086SApple OSS Distributions #define fq_enqueue(_q, _h, _t, _c, _ptype) do { \ 173*42e22086SApple OSS Distributions switch (_ptype) { \ 174*42e22086SApple OSS Distributions case QP_MBUF: \ 175*42e22086SApple OSS Distributions ASSERT((_h).cp_ptype == QP_MBUF); \ 176*42e22086SApple OSS Distributions ASSERT((_t).cp_ptype == QP_MBUF); \ 177*42e22086SApple OSS Distributions MBUFQ_ENQUEUE_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf, \ 178*42e22086SApple OSS Distributions (_t).cp_mbuf); \ 179*42e22086SApple OSS Distributions MBUFQ_ADD_CRUMB_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf, \ 180*42e22086SApple OSS Distributions (_t).cp_mbuf, PKT_CRUMB_FQ_ENQUEUE); \ 181*42e22086SApple OSS Distributions break; \ 182*42e22086SApple OSS Distributions case QP_PACKET: \ 183*42e22086SApple OSS Distributions ASSERT((_h).cp_ptype == QP_PACKET); \ 184*42e22086SApple OSS Distributions ASSERT((_t).cp_ptype == QP_PACKET); \ 185*42e22086SApple OSS Distributions KPKTQ_ENQUEUE_MULTI(&(_q)->fq_kpktq, (_h).cp_kpkt, \ 186*42e22086SApple OSS Distributions (_t).cp_kpkt, (_c)); \ 187*42e22086SApple OSS Distributions break; \ 188*42e22086SApple OSS Distributions default: \ 189*42e22086SApple OSS Distributions VERIFY(0); \ 190*42e22086SApple OSS Distributions __builtin_unreachable(); \ 191*42e22086SApple OSS Distributions break; \ 192*42e22086SApple OSS Distributions } \ 193*42e22086SApple OSS Distributions } while (0) 194*42e22086SApple OSS Distributions #else /* !SKYWALK */ 195*42e22086SApple OSS Distributions #define fq_enqueue(_q, _h, _t, _c, _ptype) { \ 196*42e22086SApple OSS Distributions MBUFQ_ENQUEUE_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf, (_t).cp_mbuf); \ 197*42e22086SApple OSS Distributions MBUFQ_ADD_CRUMB_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf, \ 198*42e22086SApple OSS Distributions (_t).cp_mbuf, PKT_CRUMB_FQ_ENQUEUE); \ 199*42e22086SApple OSS Distributions } while (0) 200*42e22086SApple OSS Distributions #endif /* !SKYWALK */ 201*42e22086SApple OSS Distributions 202*42e22086SApple OSS Distributions #if SKYWALK 203*42e22086SApple OSS Distributions #define fq_dequeue(_q, _p, _ptype) do { \ 204*42e22086SApple OSS Distributions switch (_ptype) { \ 205*42e22086SApple OSS Distributions case QP_MBUF: { \ 206*42e22086SApple OSS Distributions MBUFQ_DEQUEUE(&(_q)->fq_mbufq, (_p)->cp_mbuf); \ 207*42e22086SApple OSS Distributions if (__probable((_p)->cp_mbuf != NULL)) { \ 208*42e22086SApple OSS Distributions CLASSQ_PKT_INIT_MBUF((_p), (_p)->cp_mbuf); \ 209*42e22086SApple OSS Distributions m_add_crumb((_p)->cp_mbuf, \ 210*42e22086SApple OSS Distributions PKT_CRUMB_FQ_DEQUEUE); \ 211*42e22086SApple OSS Distributions } \ 212*42e22086SApple OSS Distributions break; \ 213*42e22086SApple OSS Distributions } \ 214*42e22086SApple OSS Distributions case QP_PACKET: { \ 215*42e22086SApple OSS Distributions KPKTQ_DEQUEUE(&(_q)->fq_kpktq, (_p)->cp_kpkt); \ 216*42e22086SApple OSS Distributions if (__probable((_p)->cp_kpkt != NULL)) { \ 217*42e22086SApple OSS Distributions CLASSQ_PKT_INIT_PACKET((_p), (_p)->cp_kpkt); \ 218*42e22086SApple OSS Distributions } \ 219*42e22086SApple OSS Distributions break; \ 220*42e22086SApple OSS Distributions } \ 221*42e22086SApple OSS Distributions default: \ 222*42e22086SApple OSS Distributions VERIFY(0); \ 223*42e22086SApple OSS Distributions __builtin_unreachable(); \ 224*42e22086SApple OSS Distributions break; \ 225*42e22086SApple OSS Distributions } \ 226*42e22086SApple OSS Distributions } while (0) 227*42e22086SApple OSS Distributions #else /* !SKYWALK */ 228*42e22086SApple OSS Distributions #define fq_dequeue(_q, _p, _ptype) do { \ 229*42e22086SApple OSS Distributions MBUFQ_DEQUEUE(&(_q)->fq_mbufq, (_p)->cp_mbuf); \ 230*42e22086SApple OSS Distributions if (__probable((_p)->cp_mbuf != NULL)) { \ 231*42e22086SApple OSS Distributions CLASSQ_PKT_INIT_MBUF((_p), (_p)->cp_mbuf); \ 232*42e22086SApple OSS Distributions m_add_crumb((_p)->cp_mbuf, PKT_CRUMB_FQ_DEQUEUE); \ 233*42e22086SApple OSS Distributions } \ 234*42e22086SApple OSS Distributions } while (0) 235*42e22086SApple OSS Distributions #endif /* !SKYWALK */ 236*42e22086SApple OSS Distributions 237*42e22086SApple OSS Distributions struct fq_codel_sched_data; 238*42e22086SApple OSS Distributions struct fq_if_classq; 239*42e22086SApple OSS Distributions 240*42e22086SApple OSS Distributions /* Function definitions */ 241*42e22086SApple OSS Distributions extern void fq_codel_init(void); 242*42e22086SApple OSS Distributions extern void fq_codel_reap_caches(boolean_t); 243*42e22086SApple OSS Distributions extern fq_t *fq_alloc(classq_pkt_type_t); 244*42e22086SApple OSS Distributions extern void fq_destroy(fq_t *, classq_pkt_type_t); 245*42e22086SApple OSS Distributions extern int fq_addq(struct fq_codel_sched_data *, fq_if_group_t *, 246*42e22086SApple OSS Distributions pktsched_pkt_t *, struct fq_if_classq *); 247*42e22086SApple OSS Distributions extern void fq_getq_flow(struct fq_codel_sched_data *, fq_t *, 248*42e22086SApple OSS Distributions pktsched_pkt_t *, uint64_t now); 249*42e22086SApple OSS Distributions extern void fq_codel_dequeue(fq_if_t *fqs, fq_t *fq, 250*42e22086SApple OSS Distributions pktsched_pkt_t *pkt, uint64_t now); 251*42e22086SApple OSS Distributions extern void fq_getq_flow_internal(struct fq_codel_sched_data *, 252*42e22086SApple OSS Distributions fq_t *, pktsched_pkt_t *); 253*42e22086SApple OSS Distributions extern void fq_head_drop(struct fq_codel_sched_data *, fq_t *); 254*42e22086SApple OSS Distributions 255*42e22086SApple OSS Distributions #ifdef __cplusplus 256*42e22086SApple OSS Distributions } 257*42e22086SApple OSS Distributions #endif 258*42e22086SApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */ 259*42e22086SApple OSS Distributions #endif /* PRIVATE */ 260*42e22086SApple OSS Distributions #endif /* _NET_CLASSQ_CLASSQ_FQ_CODEL_H */ 261