xref: /xnu-11215.81.4/bsd/net/pktsched/pktsched_fq_codel.h (revision d4514f0bc1d3f944c22d92e68b646ac3fb40d452)
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_reported;
73 	uint64_t fcl_ce_mark_failures;
74 	uint64_t fcl_l4s_pkts;
75 	uint64_t fcl_ignore_tx_time;
76 	uint64_t fcl_paced_pkts;
77 	uint64_t fcl_fcl_pacemaker_needed;
78 };
79 
80 /* Set the quantum to be one MTU */
81 #define FQ_IF_DEFAULT_QUANTUM   1500
82 
83 /* Max number of service classes currently supported */
84 #define FQ_IF_MAX_CLASSES       10
85 _Static_assert(FQ_IF_MAX_CLASSES < 127,
86     "maximum number of classes needs to fit in a single byte");
87 
88 #define FQ_IF_LARGE_FLOW_BYTE_LIMIT     15000
89 
90 /* Max number of classq groups currently supported */
91 #define FQ_IF_MAX_GROUPS                16
92 
93 typedef enum : uint8_t {
94 	FQ_TFC_C            = 0, /* classic traffic */
95 	FQ_TFC_L4S          = 1, /* L4S traffic */
96 	FQ_TFC_CNT          = 2,
97 } fq_tfc_type_t;
98 
99 struct flowq;
100 typedef u_int32_t pktsched_bitmap_t;
101 struct if_ifclassq_stats;
102 
103 typedef enum : uint8_t {
104 	FQ_IF_ER = 0,           /* eligible, ready */
105 	FQ_IF_IR = 1,           /* ineligible, ready */
106 	FQ_IF_EB = 2,           /* eligible blocked */
107 	FQ_IF_IB = 3,           /* ineligible, blocked */
108 	FQ_IF_MAX_STATE
109 } fq_if_state;
110 
111 /*
112  * This priority index is used for QFQ state bitmaps, lower index gets
113  * higher priority
114  */
115 #define FQ_IF_BK_SYS_INDEX      9
116 #define FQ_IF_BK_INDEX  8
117 #define FQ_IF_BE_INDEX  7
118 #define FQ_IF_RD_INDEX  6
119 #define FQ_IF_OAM_INDEX 5
120 #define FQ_IF_AV_INDEX  4
121 #define FQ_IF_RV_INDEX  3
122 #define FQ_IF_VI_INDEX  2
123 #define FQ_IF_SIG_INDEX 2
124 #define FQ_IF_VO_INDEX  1
125 #define FQ_IF_CTL_INDEX 0
126 
127 typedef LIST_HEAD(, flowq) flowq_list_t;
128 typedef STAILQ_HEAD(, flowq) flowq_stailq_t;
129 typedef struct fq_if_classq {
130 	uint32_t fcl_pri;      /* class priority, lower the better */
131 	uint32_t fcl_service_class;    /* service class */
132 	uint32_t fcl_quantum;          /* quantum in bytes */
133 	uint32_t fcl_drr_max;          /* max flows per class for DRR */
134 	int64_t  fcl_budget;             /* budget for this classq */
135 	uint64_t fcl_next_tx_time;      /* next time a packet is ready */
136 	flowq_stailq_t fcl_new_flows;   /* List of new flows */
137 	flowq_stailq_t fcl_old_flows;   /* List of old flows */
138 	struct fcl_stat fcl_stat;
139 #define FCL_PACED               0x1
140 	uint8_t fcl_flags;
141 } fq_if_classq_t;
142 typedef struct fq_codel_classq_group {
143 	/* Target queue delays (ns) */
144 	uint64_t                fqg_target_qdelays[FQ_TFC_CNT];
145 	/* update intervals (ns) */
146 	uint64_t                fqg_update_intervals[FQ_TFC_CNT];
147 	/* classq bitmaps */
148 	pktsched_bitmap_t       fqg_bitmaps[FQ_IF_MAX_STATE];
149 	TAILQ_ENTRY(fq_codel_classq_group) fqg_grp_link;
150 	uint32_t                fqg_bytes;     /* bytes count */
151 	uint32_t                fqg_len;       /* pkts count */
152 	uint8_t                 fqg_flags;     /* flags */
153 #define FQ_IF_DEFAULT_GRP                   0x1
154 	uint8_t                 fqg_index;     /* group index */
155 	fq_if_classq_t          fqg_classq[FQ_IF_MAX_CLASSES]; /* class queues */
156 	struct flowq            *fqg_large_flow; /* flow has highest number of bytes */
157 } fq_if_group_t;
158 
159 #define FQG_LEN(_fqg)           ((_fqg)->fqg_len)
160 #define FQG_IS_EMPTY(_fqg)      (FQG_LEN(_fqg) == 0)
161 #define FQG_INC_LEN(_fqg)       (FQG_LEN(_fqg)++)
162 #define FQG_DEC_LEN(_fqg)       (FQG_LEN(_fqg)--)
163 #define FQG_ADD_LEN(_fqg, _len) (FQG_LEN(_fqg) += (_len))
164 #define FQG_SUB_LEN(_fqg, _len) (FQG_LEN(_fqg) -= (_len))
165 #define FQG_BYTES(_fqg)         ((_fqg)->fqg_bytes)
166 
167 #define FQG_INC_BYTES(_fqg, _len)     \
168     ((_fqg)->fqg_bytes = (_fqg)->fqg_bytes + (_len))
169 #define FQG_DEC_BYTES(_fqg, _len)     \
170     ((_fqg)->fqg_bytes = (_fqg)->fqg_bytes - (_len))
171 
172 typedef TAILQ_HEAD(, fq_codel_classq_group) fq_grp_tailq_t;
173 
174 typedef int (* fq_if_bitmaps_ffs)(fq_grp_tailq_t *, int, fq_if_state, fq_if_group_t **);
175 typedef boolean_t (* fq_if_bitmaps_zeros)(fq_grp_tailq_t *, int, fq_if_state);
176 typedef void (* fq_if_bitmaps_cpy)(fq_grp_tailq_t *, int, fq_if_state, fq_if_state);
177 typedef void (* fq_if_bitmaps_clr)(fq_grp_tailq_t *, int, fq_if_state);
178 typedef void (* fq_if_bitmaps_move)(fq_grp_tailq_t *, int, fq_if_state, fq_if_state);
179 
180 /*
181  * Functions that are used to look at groups'
182  * bitmaps and decide which pri and group are the
183  * next one to dequeue from.
184  */
185 typedef struct fq_if_bitmap_ops {
186 	fq_if_bitmaps_ffs       ffs;
187 	fq_if_bitmaps_zeros     zeros;
188 	fq_if_bitmaps_cpy       cpy;
189 	fq_if_bitmaps_clr       clr;
190 	fq_if_bitmaps_move      move;
191 } bitmap_ops_t;
192 
193 typedef struct fq_codel_sched_data {
194 	struct ifclassq         *fqs_ifq;       /* back pointer to ifclassq */
195 	flowq_list_t            *fqs_flows __counted_by(fqs_flows_count); /* flows table */
196 	uint32_t                fqs_flows_count;
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 	thread_call_t           fqs_pacemaker_tcall;
211 	bitmap_ops_t            *fqs_bm_ops;
212 #define grp_bitmaps_ffs     fqs_bm_ops->ffs
213 #define grp_bitmaps_zeros   fqs_bm_ops->zeros
214 #define grp_bitmaps_cpy     fqs_bm_ops->cpy
215 #define grp_bitmaps_clr     fqs_bm_ops->clr
216 #define grp_bitmaps_move    fqs_bm_ops->move
217 	fq_if_group_t           *fqs_classq_groups[FQ_IF_MAX_GROUPS];
218 } fq_if_t;
219 
220 #define FQS_GROUP(_fqs, _group_idx)                                      \
221 	(fq_if_find_grp((_fqs), (_group_idx)))
222 
223 #define FQS_CLASSQ(_fqs, _group_idx, _sc_idx)                            \
224     (FQS_GROUP((_fqs), (_group_idx))->fqg_classq[_sc_idx])
225 
226 #define FQ_GROUP(_fq)                                      \
227 	((_fq)->fq_group)
228 
229 #define FQ_GRP_LEN(_fq)                                    \
230 	(FQ_GROUP((_fq))->fqg_len)
231 #define FQ_GRP_IS_EMPTY(_fq)                               \
232 	(FQ_GRP_LEN((_fq)) == 0)
233 #define FQ_GRP_INC_LEN(_fq)                                \
234     (FQ_GRP_LEN((_fq))++)
235 #define FQ_GRP_DEC_LEN(_fq)                                \
236     (FQ_GRP_LEN((_fq))--)
237 #define FQ_GRP_ADD_LEN(_fq, _len)                          \
238 	(FQ_GRP_LEN((_fq)) += (_len))
239 #define FQ_GRP_SUB_LEN(_fq, _len)                          \
240 	(FQ_GRP_LEN((_fq)) -= (_len))
241 
242 #define FQS_GRP_ADD_LEN(_fqs, _grp_idx, _len)              \
243 	(FQS_GROUP(_fqs, grp_idx)->fqg_len += (_len))
244 
245 
246 #define FQ_GRP_BYTES(_fq)                     \
247 	(FQ_GROUP((_fq))->fqg_bytes)
248 #define FQ_GRP_INC_BYTES(_fq, _len)           \
249     (FQ_GRP_BYTES((_fq)) += (_len))
250 #define FQ_GRP_DEC_BYTES(_fq, _len)           \
251     (FQ_GRP_BYTES((_fq)) -= (_len))
252 
253 #define FQS_GRP_INC_BYTES(_fqs, grp_idx, _len)           \
254 	(FQS_GROUP(_fqs, grp_idx)->fqg_bytes += (_len))
255 
256 #define FQ_CLASSQ(_fq)                                   \
257 	(FQ_GROUP((_fq))->fqg_classq[(_fq)->fq_sc_index])
258 
259 #define FQ_TARGET_DELAY(_fq)              \
260 	(FQ_GROUP((_fq))->fqg_target_qdelays[(_fq)->fq_tfc_type])
261 #define FQ_UPDATE_INTERVAL(_fq)           \
262 	(FQ_GROUP((_fq))->fqg_update_intervals[(_fq)->fq_tfc_type])
263 
264 #endif /* BSD_KERNEL_PRIVATE */
265 
266 struct fq_codel_flowstats {
267 	u_int32_t       fqst_min_qdelay;
268 #define FQ_FLOWSTATS_OLD_FLOW   0x1
269 #define FQ_FLOWSTATS_NEW_FLOW   0x2
270 #define FQ_FLOWSTATS_LARGE_FLOW 0x4
271 #define FQ_FLOWSTATS_DELAY_HIGH 0x8
272 #define FQ_FLOWSTATS_FLOWCTL_ON 0x10
273 	u_int32_t       fqst_flags;
274 	u_int32_t       fqst_bytes;
275 	u_int32_t       fqst_flowhash;
276 };
277 
278 #define FQ_IF_MAX_FLOWSTATS     20
279 #define FQ_IF_STATS_MAX_GROUPS  16
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_reported;
318 	uint64_t        fcls_ce_mark_failures;
319 	uint64_t        fcls_l4s_pkts;
320 	uint64_t        fcls_ignore_tx_time;
321 	uint64_t        fcls_paced_pkts;
322 	uint64_t        fcls_fcl_pacing_needed;
323 };
324 
325 #ifdef BSD_KERNEL_PRIVATE
326 
327 _Static_assert(FQ_IF_STATS_MAX_GROUPS == FQ_IF_MAX_GROUPS,
328     "max group counts do not match");
329 
330 extern void pktsched_fq_init(void);
331 extern void fq_codel_scheduler_init(void);
332 extern int fq_if_enqueue_classq(struct ifclassq *ifq, classq_pkt_t *h,
333     classq_pkt_t *t, uint32_t cnt, uint32_t bytes, boolean_t *pdrop);
334 extern void fq_if_dequeue_classq(struct ifclassq *ifq, classq_pkt_t *pkt,
335     uint8_t grp_idx);
336 extern void fq_if_dequeue_sc_classq(struct ifclassq *ifq, mbuf_svc_class_t svc,
337     classq_pkt_t *pkt, uint8_t grp_idx);
338 extern int fq_if_dequeue_classq_multi(struct ifclassq *ifq, u_int32_t maxpktcnt,
339     u_int32_t maxbytecnt, classq_pkt_t *first_packet, classq_pkt_t *last_packet,
340     u_int32_t *retpktcnt, u_int32_t *retbytecnt, uint8_t grp_idx);
341 extern int fq_if_dequeue_sc_classq_multi(struct ifclassq *ifq,
342     mbuf_svc_class_t svc, u_int32_t maxpktcnt, u_int32_t maxbytecnt,
343     classq_pkt_t *first_packet, classq_pkt_t *last_packet, u_int32_t *retpktcnt,
344     u_int32_t *retbytecnt, uint8_t grp_idx);
345 extern int fq_if_request_classq(struct ifclassq *ifq, cqrq_t rq, void *arg);
346 extern struct flowq *fq_if_hash_pkt(fq_if_t *, fq_if_group_t *,
347     u_int32_t, mbuf_svc_class_t, u_int64_t, bool, fq_tfc_type_t);
348 extern boolean_t fq_if_at_drop_limit(fq_if_t *);
349 extern boolean_t fq_if_almost_at_drop_limit(fq_if_t *fqs);
350 extern void fq_if_drop_packet(fq_if_t *, uint64_t);
351 extern void fq_if_is_flow_heavy(fq_if_t *, struct flowq *);
352 extern boolean_t fq_if_add_fcentry(fq_if_t *, pktsched_pkt_t *, uint8_t,
353     struct flowq *, fq_if_classq_t *);
354 extern void fq_if_flow_feedback(fq_if_t *, struct flowq *, fq_if_classq_t *);
355 extern boolean_t fq_if_report_ce(fq_if_t *, pktsched_pkt_t *, uint32_t, uint32_t);
356 extern int fq_if_setup_ifclassq(struct ifclassq *ifq, u_int32_t flags,
357     classq_pkt_type_t ptype);
358 extern void fq_if_teardown_ifclassq(struct ifclassq *ifq);
359 extern int fq_if_getqstats_ifclassq(struct ifclassq *ifq, uint8_t gid,
360     u_int32_t qid, struct if_ifclassq_stats *ifqs);
361 extern void fq_if_destroy_flow(fq_if_t *, fq_if_classq_t *, struct flowq *);
362 extern void fq_if_move_to_empty_flow(fq_if_t *, fq_if_classq_t *,
363     struct flowq *, uint64_t);
364 extern int fq_if_create_grp(struct ifclassq *ifcq, uint8_t qset_idx, uint8_t flags);
365 extern void fq_if_set_grp_combined(struct ifclassq *ifcq, uint8_t qset_idx);
366 extern void fq_if_set_grp_separated(struct ifclassq *ifcq, uint8_t qset_idx);
367 extern fq_if_group_t *fq_if_find_grp(fq_if_t *fqs, uint8_t grp_idx);
368 extern boolean_t fq_if_is_all_paced(struct ifclassq *ifq);
369 #endif /* BSD_KERNEL_PRIVATE */
370 
371 #ifdef __cplusplus
372 }
373 #endif
374 
375 #endif /* PRIVATE */
376 #endif /* _NET_PKTSCHED_PKTSCHED_FQ_CODEL_H_ */
377