xref: /xnu-8792.41.9/bsd/net/pktsched/pktsched_fq_codel.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
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_PKTSCHED_FQ_CODEL_H_
30 #define _NET_PKTSCHED_FQ_CODEL_H_
31 
32 #ifdef PRIVATE
33 #include <sys/types.h>
34 #include <sys/param.h>
35 
36 #ifdef BSD_KERNEL_PRIVATE
37 #include <net/flowadv.h>
38 #include <net/pktsched/pktsched.h>
39 #endif /* BSD_KERNEL_PRIVATE */
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #ifdef BSD_KERNEL_PRIVATE
46 struct fcl_stat {
47 	u_int32_t fcl_flow_control;
48 	u_int32_t fcl_flow_feedback;
49 	u_int32_t fcl_dequeue_stall;
50 	u_int32_t fcl_flow_control_fail;
51 	u_int64_t fcl_drop_overflow;
52 	u_int64_t fcl_drop_early;
53 	u_int32_t fcl_drop_memfailure;
54 	u_int32_t fcl_flows_cnt;
55 	u_int32_t fcl_newflows_cnt;
56 	u_int32_t fcl_oldflows_cnt;
57 	u_int64_t fcl_pkt_cnt;
58 	u_int64_t fcl_dequeue;
59 	u_int64_t fcl_dequeue_bytes;
60 	u_int64_t fcl_byte_cnt;
61 	u_int32_t fcl_throttle_on;
62 	u_int32_t fcl_throttle_off;
63 	u_int32_t fcl_throttle_drops;
64 	u_int32_t fcl_dup_rexmts;
65 	u_int32_t fcl_pkts_compressible;
66 	u_int32_t fcl_pkts_compressed;
67 	uint64_t fcl_min_qdelay;
68 	uint64_t fcl_max_qdelay;
69 	uint64_t fcl_avg_qdelay;
70 	uint32_t fcl_overwhelming;
71 };
72 
73 /*
74  * Use 8 bits from the flow id as the tag for set associative
75  * hashing
76  * NOTE: The first 2 bits of the flow id is being used to encode the flow
77  * domain information, so don't use the top 8 bits as it won't have a uniform
78  * distribution.
79  */
80 
81 #define FQ_IF_HASH_TAG_SIZE     8
82 #define FQ_IF_HASH_TAG_SHIFT    16
83 #define FQ_IF_HASH_TAG_MASK     0xFF
84 #define FQ_IF_HASH_TABLE_SIZE   (1 << FQ_IF_HASH_TAG_SIZE)
85 
86 /* Set the quantum to be one MTU */
87 #define FQ_IF_DEFAULT_QUANTUM   1500
88 
89 /* Max number of service classes currently supported */
90 #define FQ_IF_MAX_CLASSES       10
91 _Static_assert(FQ_IF_MAX_CLASSES < 127,
92     "maximum number of classes needs to fit in a single byte");
93 
94 #define FQ_IF_LARGE_FLOW_BYTE_LIMIT     15000
95 
96 /* Max number of classq groups currently supported */
97 #define FQ_IF_MAX_GROUPS                16
98 
99 typedef enum : uint8_t {
100 	FQ_TFC_C            = 0, /* classic traffic */
101 	FQ_TFC_L4S          = 1, /* L4S traffic */
102 	FQ_TFC_CNT          = 2,
103 } fq_tfc_type_t;
104 
105 struct flowq;
106 typedef u_int32_t pktsched_bitmap_t;
107 struct if_ifclassq_stats;
108 
109 typedef enum : uint8_t {
110 	FQ_IF_ER = 0,           /* eligible, ready */
111 	FQ_IF_IR = 1,           /* ineligible, ready */
112 	FQ_IF_EB = 2,           /* eligible blocked */
113 	FQ_IF_IB = 3,           /* ineligible, blocked */
114 	FQ_IF_MAX_STATE
115 } fq_if_state;
116 
117 /*
118  * This priority index is used for QFQ state bitmaps, lower index gets
119  * higher priority
120  */
121 #define FQ_IF_BK_SYS_INDEX      9
122 #define FQ_IF_BK_INDEX  8
123 #define FQ_IF_BE_INDEX  7
124 #define FQ_IF_RD_INDEX  6
125 #define FQ_IF_OAM_INDEX 5
126 #define FQ_IF_AV_INDEX  4
127 #define FQ_IF_RV_INDEX  3
128 #define FQ_IF_VI_INDEX  2
129 #define FQ_IF_SIG_INDEX 2
130 #define FQ_IF_VO_INDEX  1
131 #define FQ_IF_CTL_INDEX 0
132 
133 typedef SLIST_HEAD(, flowq) flowq_list_t;
134 typedef STAILQ_HEAD(, flowq) flowq_stailq_t;
135 typedef struct fq_if_classq {
136 	uint32_t fcl_pri;      /* class priority, lower the better */
137 	uint32_t fcl_service_class;    /* service class */
138 	uint32_t fcl_quantum;          /* quantum in bytes */
139 	uint32_t fcl_drr_max;          /* max flows per class for DRR */
140 	int64_t fcl_budget;             /* budget for this classq */
141 	flowq_stailq_t fcl_new_flows;   /* List of new flows */
142 	flowq_stailq_t fcl_old_flows;   /* List of old flows */
143 	struct fcl_stat fcl_stat;
144 } fq_if_classq_t;
145 typedef struct fq_codel_classq_group {
146 	/* Target queue delays (ns) */
147 	uint64_t                fqg_target_qdelays[FQ_TFC_CNT];
148 	/* update intervals (ns) */
149 	uint64_t                fqg_update_intervals[FQ_TFC_CNT];
150 	/* classq bitmaps */
151 	pktsched_bitmap_t       fqg_bitmaps[FQ_IF_MAX_STATE];
152 	TAILQ_ENTRY(fq_codel_classq_group) fqg_grp_link;
153 	uint32_t                fqg_bytes;     /* bytes count */
154 	uint32_t                fqg_len;       /* pkts count */
155 	uint8_t                 fqg_flags;     /* flags */
156 #define FQ_IF_DEFAULT_GRP                   0x1
157 	uint8_t                 fqg_index;     /* group index */
158 	fq_if_classq_t          fqg_classq[FQ_IF_MAX_CLASSES]; /* class queues */
159 	struct flowq            *fqg_large_flow; /* flow has highest number of bytes */
160 } fq_if_group_t;
161 
162 #define FQG_LEN(_fqg)           ((_fqg)->fqg_len)
163 #define FQG_IS_EMPTY(_fqg)      (FQG_LEN(_fqg) == 0)
164 #define FQG_INC_LEN(_fqg)       (FQG_LEN(_fqg)++)
165 #define FQG_DEC_LEN(_fqg)       (FQG_LEN(_fqg)--)
166 #define FQG_ADD_LEN(_fqg, _len) (FQG_LEN(_fqg) += (_len))
167 #define FQG_SUB_LEN(_fqg, _len) (FQG_LEN(_fqg) -= (_len))
168 #define FQG_BYTES(_fqg)         ((_fqg)->fqg_bytes)
169 
170 #define FQG_INC_BYTES(_fqg, _len)     \
171     ((_fqg)->fqg_bytes = (_fqg)->fqg_bytes + (_len))
172 #define FQG_DEC_BYTES(_fqg, _len)     \
173     ((_fqg)->fqg_bytes = (_fqg)->fqg_bytes - (_len))
174 
175 typedef TAILQ_HEAD(, fq_codel_classq_group) fq_grp_tailq_t;
176 
177 typedef int (* fq_if_bitmaps_ffs)(fq_grp_tailq_t *, int, fq_if_state, fq_if_group_t **);
178 typedef boolean_t (* fq_if_bitmaps_zeros)(fq_grp_tailq_t *, int, fq_if_state);
179 typedef void (* fq_if_bitmaps_cpy)(fq_grp_tailq_t *, int, fq_if_state, fq_if_state);
180 typedef void (* fq_if_bitmaps_clr)(fq_grp_tailq_t *, int, fq_if_state);
181 
182 /*
183  * Functions that are used to look at groups'
184  * bitmaps and decide which pri and group are the
185  * next one to dequeue from.
186  */
187 typedef struct fq_if_bitmap_ops {
188 	fq_if_bitmaps_ffs       ffs;
189 	fq_if_bitmaps_zeros     zeros;
190 	fq_if_bitmaps_cpy       cpy;
191 	fq_if_bitmaps_clr       clr;
192 } bitmap_ops_t;
193 
194 typedef struct fq_codel_sched_data {
195 	struct ifclassq         *fqs_ifq;       /* back pointer to ifclassq */
196 	flowq_list_t            fqs_flows[FQ_IF_HASH_TABLE_SIZE]; /* flows table */
197 	uint32_t                fqs_pkt_droplimit;  /* drop limit */
198 	uint8_t                 fqs_throttle;   /* throttle on or off */
199 	uint8_t                 fqs_flags;      /* flags */
200 #define FQS_DRIVER_MANAGED      0x1
201 	struct flowadv_fclist   fqs_fclist; /* flow control state */
202 	struct flowq            *fqs_large_flow; /* flow has highest number of bytes */
203 	TAILQ_HEAD(, flowq)     fqs_empty_list; /* list of empty flows */
204 	/* list of groups in combined mode */
205 	fq_grp_tailq_t          fqs_combined_grp_list;
206 	uint32_t                fqs_empty_list_cnt;
207 	/* bitmap indicating which grp is in combined mode */
208 	pktsched_bitmap_t       fqs_combined_grp_bitmap;
209 	classq_pkt_type_t       fqs_ptype;
210 	bitmap_ops_t            *fqs_bm_ops;
211 #define grp_bitmaps_ffs     fqs_bm_ops->ffs
212 #define grp_bitmaps_zeros   fqs_bm_ops->zeros
213 #define grp_bitmaps_cpy     fqs_bm_ops->cpy
214 #define grp_bitmaps_clr     fqs_bm_ops->clr
215 	fq_if_group_t           *fqs_classq_groups[FQ_IF_MAX_GROUPS];
216 } fq_if_t;
217 
218 #define FQS_GROUP(_fqs, _group_idx)                                      \
219 	(fq_if_find_grp((_fqs), (_group_idx)))
220 
221 #define FQS_CLASSQ(_fqs, _group_idx, _sc_idx)                            \
222     (FQS_GROUP((_fqs), (_group_idx))->fqg_classq[_sc_idx])
223 
224 #define FQ_GROUP(_fq)                                      \
225 	((_fq)->fq_group)
226 
227 #define FQ_GRP_LEN(_fq)                                    \
228 	(FQ_GROUP((_fq))->fqg_len)
229 #define FQ_GRP_IS_EMPTY(_fq)                               \
230 	(FQ_GRP_LEN((_fq)) == 0)
231 #define FQ_GRP_INC_LEN(_fq)                                \
232     (FQ_GRP_LEN((_fq))++)
233 #define FQ_GRP_DEC_LEN(_fq)                                \
234     (FQ_GRP_LEN((_fq))--)
235 #define FQ_GRP_ADD_LEN(_fq, _len)                          \
236 	(FQ_GRP_LEN((_fq)) += (_len))
237 #define FQ_GRP_SUB_LEN(_fq, _len)                          \
238 	(FQ_GRP_LEN((_fq)) -= (_len))
239 
240 #define FQS_GRP_ADD_LEN(_fqs, _grp_idx, _len)              \
241 	(FQS_GROUP(_fqs, grp_idx)->fqg_len += (_len))
242 
243 
244 #define FQ_GRP_BYTES(_fq)                     \
245 	(FQ_GROUP((_fq))->fqg_bytes)
246 #define FQ_GRP_INC_BYTES(_fq, _len)           \
247     (FQ_GRP_BYTES((_fq)) += (_len))
248 #define FQ_GRP_DEC_BYTES(_fq, _len)           \
249     (FQ_GRP_BYTES((_fq)) -= (_len))
250 
251 #define FQS_GRP_INC_BYTES(_fqs, grp_idx, _len)           \
252 	(FQS_GROUP(_fqs, grp_idx)->fqg_bytes += (_len))
253 
254 #define FQ_CLASSQ(_fq)                                   \
255 	(FQ_GROUP((_fq))->fqg_classq[(_fq)->fq_sc_index])
256 
257 #define FQ_TARGET_DELAY(_fq)              \
258 	(FQ_GROUP((_fq))->fqg_target_qdelays[(_fq)->fq_tfc_type])
259 #define FQ_UPDATE_INTERVAL(_fq)           \
260 	(FQ_GROUP((_fq))->fqg_update_intervals[(_fq)->fq_tfc_type])
261 
262 #endif /* BSD_KERNEL_PRIVATE */
263 
264 struct fq_codel_flowstats {
265 	u_int32_t       fqst_min_qdelay;
266 #define FQ_FLOWSTATS_OLD_FLOW   0x1
267 #define FQ_FLOWSTATS_NEW_FLOW   0x2
268 #define FQ_FLOWSTATS_LARGE_FLOW 0x4
269 #define FQ_FLOWSTATS_DELAY_HIGH 0x8
270 #define FQ_FLOWSTATS_FLOWCTL_ON 0x10
271 	u_int32_t       fqst_flags;
272 	u_int32_t       fqst_bytes;
273 	u_int32_t       fqst_flowhash;
274 };
275 
276 #define FQ_IF_MAX_FLOWSTATS     20
277 
278 struct fq_codel_classstats {
279 	u_int32_t       fcls_pri;
280 	u_int32_t       fcls_service_class;
281 	u_int32_t       fcls_quantum;
282 	u_int32_t       fcls_drr_max;
283 	int64_t         fcls_budget;
284 	u_int64_t       fcls_target_qdelay;
285 	u_int64_t       fcls_update_interval;
286 	u_int32_t       fcls_flow_control;
287 	u_int32_t       fcls_flow_feedback;
288 	u_int32_t       fcls_dequeue_stall;
289 	u_int32_t       fcls_flow_control_fail;
290 	u_int64_t       fcls_drop_overflow;
291 	u_int64_t       fcls_drop_early;
292 	u_int32_t       fcls_drop_memfailure;
293 	u_int32_t       fcls_flows_cnt;
294 	u_int32_t       fcls_newflows_cnt;
295 	u_int32_t       fcls_oldflows_cnt;
296 	u_int64_t       fcls_pkt_cnt;
297 	u_int64_t       fcls_dequeue;
298 	u_int64_t       fcls_dequeue_bytes;
299 	u_int64_t       fcls_byte_cnt;
300 	u_int32_t       fcls_throttle_on;
301 	u_int32_t       fcls_throttle_off;
302 	u_int32_t       fcls_throttle_drops;
303 	u_int32_t       fcls_dup_rexmts;
304 	u_int32_t       fcls_flowstats_cnt;
305 	struct fq_codel_flowstats fcls_flowstats[FQ_IF_MAX_FLOWSTATS];
306 	u_int32_t       fcls_pkts_compressible;
307 	u_int32_t       fcls_pkts_compressed;
308 	uint64_t        fcls_min_qdelay;
309 	uint64_t        fcls_max_qdelay;
310 	uint64_t        fcls_avg_qdelay;
311 	uint32_t        fcls_overwhelming;
312 };
313 
314 #ifdef BSD_KERNEL_PRIVATE
315 extern void pktsched_fq_init(void);
316 extern void fq_codel_scheduler_init(void);
317 extern int fq_if_enqueue_classq(struct ifclassq *ifq, classq_pkt_t *h,
318     classq_pkt_t *t, uint32_t cnt, uint32_t bytes, boolean_t *pdrop);
319 extern void fq_if_dequeue_classq(struct ifclassq *ifq, classq_pkt_t *pkt,
320     uint8_t grp_idx);
321 extern void fq_if_dequeue_sc_classq(struct ifclassq *ifq, mbuf_svc_class_t svc,
322     classq_pkt_t *pkt, uint8_t grp_idx);
323 extern int fq_if_dequeue_classq_multi(struct ifclassq *ifq, u_int32_t maxpktcnt,
324     u_int32_t maxbytecnt, classq_pkt_t *first_packet, classq_pkt_t *last_packet,
325     u_int32_t *retpktcnt, u_int32_t *retbytecnt, uint8_t grp_idx);
326 extern int fq_if_dequeue_sc_classq_multi(struct ifclassq *ifq,
327     mbuf_svc_class_t svc, u_int32_t maxpktcnt, u_int32_t maxbytecnt,
328     classq_pkt_t *first_packet, classq_pkt_t *last_packet, u_int32_t *retpktcnt,
329     u_int32_t *retbytecnt, uint8_t grp_idx);
330 extern int fq_if_request_classq(struct ifclassq *ifq, cqrq_t rq, void *arg);
331 extern struct flowq *fq_if_hash_pkt(fq_if_t *, fq_if_group_t *,
332     u_int32_t, mbuf_svc_class_t, u_int64_t, bool, fq_tfc_type_t);
333 extern boolean_t fq_if_at_drop_limit(fq_if_t *);
334 extern boolean_t fq_if_almost_at_drop_limit(fq_if_t *fqs);
335 extern void fq_if_drop_packet(fq_if_t *, uint64_t);
336 extern void fq_if_is_flow_heavy(fq_if_t *, struct flowq *);
337 extern boolean_t fq_if_add_fcentry(fq_if_t *, pktsched_pkt_t *, uint8_t,
338     struct flowq *, fq_if_classq_t *);
339 extern void fq_if_flow_feedback(fq_if_t *, struct flowq *, fq_if_classq_t *);
340 extern int fq_if_setup_ifclassq(struct ifclassq *ifq, u_int32_t flags,
341     classq_pkt_type_t ptype);
342 extern void fq_if_teardown_ifclassq(struct ifclassq *ifq);
343 extern int fq_if_getqstats_ifclassq(struct ifclassq *ifq, uint8_t gid,
344     u_int32_t qid, struct if_ifclassq_stats *ifqs);
345 extern void fq_if_destroy_flow(fq_if_t *, fq_if_classq_t *, struct flowq *);
346 extern void fq_if_move_to_empty_flow(fq_if_t *, fq_if_classq_t *,
347     struct flowq *, uint64_t);
348 extern int fq_if_create_grp(struct ifclassq *ifcq, uint8_t qset_idx, uint8_t flags);
349 extern void fq_if_set_grp_combined(struct ifclassq *ifcq, uint8_t qset_idx);
350 extern void fq_if_set_grp_separated(struct ifclassq *ifcq, uint8_t qset_idx);
351 extern fq_if_group_t *fq_if_find_grp(fq_if_t *fqs, uint8_t grp_idx);
352 #endif /* BSD_KERNEL_PRIVATE */
353 
354 #ifdef __cplusplus
355 }
356 #endif
357 
358 #endif /* PRIVATE */
359 #endif /* _NET_PKTSCHED_PKTSCHED_FQ_CODEL_H_ */
360