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