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