xref: /xnu-8019.80.24/bsd/net/classq/classq_fq_codel.h (revision a325d9c4a84054e40bbe985afedcb50ab80993ea)
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 FQ_MIN_FC_THRESHOLD_BYTES       7500
46 #define FQ_IS_DELAYHIGH(_fq_)   ((_fq_)->fq_flags & FQF_DELAY_HIGH)
47 #define FQ_SET_DELAY_HIGH(_fq_) do { \
48 	(_fq_)->fq_flags |= FQF_DELAY_HIGH; \
49 } while (0)
50 #define FQ_CLEAR_DELAY_HIGH(_fq_) do { \
51 	(_fq_)->fq_flags &= ~FQF_DELAY_HIGH; \
52 } while (0)
53 
54 typedef struct flowq {
55 	union {
56 		MBUFQ_HEAD(mbufq_head) __mbufq; /* mbuf packet queue */
57 #if SKYWALK
58 		KPKTQ_HEAD(kpktq_head) __kpktq; /* skywalk packet queue */
59 #endif /* SKYWALK */
60 	} __fq_pktq_u;
61 #define FQF_FLOWCTL_CAPABLE 0x01 /* Use flow control instead of drop */
62 #define FQF_DELAY_HIGH  0x02    /* Min delay is greater than target */
63 #define FQF_NEW_FLOW    0x04    /* Currently on new flows queue */
64 #define FQF_OLD_FLOW    0x08    /* Currently on old flows queue */
65 #define FQF_FLOWCTL_ON  0x10    /* Currently flow controlled */
66 #define FQF_DESTROYED   0x80    /* flowq destroyed */
67 	uint8_t        fq_flags;       /* flags */
68 	uint8_t        fq_sc_index; /* service_class index */
69 	int32_t        fq_deficit;     /* Deficit for scheduling */
70 	uint32_t       fq_bytes;       /* Number of bytes in the queue */
71 	uint64_t       fq_min_qdelay; /* min queue delay for Codel */
72 	uint64_t       fq_updatetime; /* next update interval */
73 	uint64_t       fq_getqtime;    /* last dequeue time */
74 	SLIST_ENTRY(flowq) fq_hashlink; /* for flow queue hash table */
75 	STAILQ_ENTRY(flowq) fq_actlink; /* for new/old flow queues */
76 	uint32_t       fq_flowhash;    /* Flow hash */
77 	classq_pkt_type_t       fq_ptype; /* Packet type */
78 	/* temporary packet queue for dequeued packets */
79 	classq_pkt_t   fq_dq_head;
80 	classq_pkt_t   fq_dq_tail;
81 	STAILQ_ENTRY(flowq) fq_dqlink; /* entry on dequeue flow list */
82 	bool           fq_in_dqlist;
83 } fq_t;
84 
85 #define fq_mbufq        __fq_pktq_u.__mbufq
86 #if SKYWALK
87 #define fq_kpktq        __fq_pktq_u.__kpktq
88 #endif /* SKYWALK */
89 
90 #if SKYWALK
91 #define fq_empty(_q)    (((_q)->fq_ptype == QP_MBUF) ?  \
92     MBUFQ_EMPTY(&(_q)->fq_mbufq) :                      \
93     KPKTQ_EMPTY(&(_q)->fq_kpktq))
94 #else /* !SKYWALK */
95 #define fq_empty(_q)    MBUFQ_EMPTY(&(_q)->fq_mbufq)
96 #endif /* !SKYWALK */
97 
98 #if SKYWALK
99 #define fq_enqueue(_q, _h, _t, _c) do {                                 \
100 	switch ((_q)->fq_ptype) {                                       \
101 	case QP_MBUF:                                                   \
102 	        ASSERT((_h).cp_ptype == QP_MBUF);                       \
103 	        ASSERT((_t).cp_ptype == QP_MBUF);                       \
104 	        MBUFQ_ENQUEUE_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf,      \
105 	            (_t).cp_mbuf);                                      \
106 	        MBUFQ_ADD_CRUMB_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf,    \
107 	            (_t).cp_mbuf, PKT_CRUMB_FQ_ENQUEUE);                \
108 	        break;                                                  \
109 	case QP_PACKET:                                                 \
110 	        ASSERT((_h).cp_ptype == QP_PACKET);                     \
111 	        ASSERT((_t).cp_ptype == QP_PACKET);                     \
112 	        KPKTQ_ENQUEUE_MULTI(&(_q)->fq_kpktq, (_h).cp_kpkt,      \
113 	            (_t).cp_kpkt, (_c));                                \
114 	        break;                                                  \
115 	default:                                                        \
116 	        VERIFY(0);                                              \
117 	        __builtin_unreachable();                                \
118 	        break;                                                  \
119 	}                                                               \
120 } while (0)
121 #else /* !SKYWALK */
122 #define fq_enqueue(_q, _h, _t, _c) do {                                 \
123 	MBUFQ_ENQUEUE_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf,              \
124 	    (_t).cp_mbuf);                                              \
125 	MBUFQ_ADD_CRUMB_MULTI(&(_q)->fq_mbufq, (_h).cp_mbuf,            \
126 	    (_t).cp_mbuf, PKT_CRUMB_FQ_ENQUEUE);                        \
127 } while (0)
128 #endif /* !SKYWALK */
129 
130 #if SKYWALK
131 #define fq_dequeue(_q, _p) do {                                         \
132 	switch ((_q)->fq_ptype) {                                       \
133 	case QP_MBUF: {                                                 \
134 	        MBUFQ_DEQUEUE(&(_q)->fq_mbufq, (_p)->cp_mbuf);          \
135 	        if (__probable((_p)->cp_mbuf != NULL)) {                \
136 	                CLASSQ_PKT_INIT_MBUF((_p), (_p)->cp_mbuf);      \
137 	                m_add_crumb((_p)->cp_mbuf,                      \
138 	                    PKT_CRUMB_FQ_DEQUEUE);                      \
139 	        }                                                       \
140 	        break;                                                  \
141 	}                                                               \
142 	case QP_PACKET: {                                               \
143 	        KPKTQ_DEQUEUE(&(_q)->fq_kpktq, (_p)->cp_kpkt);          \
144 	        if (__probable((_p)->cp_kpkt != NULL)) {                \
145 	                CLASSQ_PKT_INIT_PACKET((_p), (_p)->cp_kpkt);    \
146 	        }                                                       \
147 	        break;                                                  \
148 	}                                                               \
149 	default:                                                        \
150 	        VERIFY(0);                                              \
151 	        __builtin_unreachable();                                \
152 	        break;                                                  \
153 	}                                                               \
154 } while (0)
155 #else /* !SKYWALK */
156 #define fq_dequeue(_q, _p) do {                                         \
157 	MBUFQ_DEQUEUE(&(_q)->fq_mbufq, (_p)->cp_mbuf);                  \
158 	if (__probable((_p)->cp_mbuf != NULL)) {                        \
159 	        CLASSQ_PKT_INIT_MBUF((_p), (_p)->cp_mbuf);              \
160 	        m_add_crumb((_p)->cp_mbuf, PKT_CRUMB_FQ_DEQUEUE);       \
161 	}                                                               \
162 } while (0)
163 #endif /* !SKYWALK */
164 
165 struct fq_codel_sched_data;
166 struct fq_if_classq;
167 
168 /* Function definitions */
169 extern void fq_codel_init(void);
170 extern void fq_codel_reap_caches(boolean_t);
171 extern fq_t *fq_alloc(classq_pkt_type_t);
172 extern void fq_destroy(fq_t *);
173 extern int fq_addq(struct fq_codel_sched_data *, pktsched_pkt_t *,
174     struct fq_if_classq *);
175 extern void fq_getq_flow(struct fq_codel_sched_data *, fq_t *,
176     pktsched_pkt_t *);
177 extern void fq_getq_flow_internal(struct fq_codel_sched_data *,
178     fq_t *, pktsched_pkt_t *);
179 extern void fq_head_drop(struct fq_codel_sched_data *, fq_t *);
180 
181 #ifdef __cplusplus
182 }
183 #endif
184 #endif /* BSD_KERNEL_PRIVATE */
185 #endif /* PRIVATE */
186 #endif /* _NET_CLASSQ_CLASSQ_FQ_CODEL_H */
187