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