]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/classq/if_classq.h
xnu-7195.81.3.tar.gz
[apple/xnu.git] / bsd / net / classq / if_classq.h
CommitLineData
316670eb 1/*
cb323159 2 * Copyright (c) 2011-2018 Apple Inc. All rights reserved.
316670eb
A
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_IF_CLASSQ_H_
0a7de745 30#define _NET_CLASSQ_IF_CLASSQ_H_
316670eb
A
31
32#ifdef PRIVATE
0a7de745 33#define IFCQ_SC_MAX 10 /* max number of queues */
316670eb
A
34
35#ifdef BSD_KERNEL_PRIVATE
36#include <net/classq/classq.h>
5ba3f43e
A
37
38/* maximum number of packets stored across all queues */
0a7de745 39#define IFCQ_DEFAULT_PKT_DROP_LIMIT 2048
316670eb
A
40
41/* classq request types */
42typedef enum cqrq {
0a7de745
A
43 CLASSQRQ_PURGE = 1, /* purge all packets */
44 CLASSQRQ_PURGE_SC = 2, /* purge service class (and flow) */
45 CLASSQRQ_EVENT = 3, /* interface events */
46 CLASSQRQ_THROTTLE = 4, /* throttle packets */
47 CLASSQRQ_STAT_SC = 5, /* get service class queue stats */
316670eb
A
48} cqrq_t;
49
50/* classq purge_sc request argument */
51typedef struct cqrq_purge_sc {
0a7de745
A
52 mbuf_svc_class_t sc; /* (in) service class */
53 u_int32_t flow; /* (in) 0 means all flows */
54 u_int32_t packets; /* (out) purged packets */
55 u_int32_t bytes; /* (out) purged bytes */
316670eb
A
56} cqrq_purge_sc_t;
57
58/* classq throttle request argument */
59typedef struct cqrq_throttle {
0a7de745
A
60 u_int32_t set; /* set or get */
61 u_int32_t level; /* (in/out) throttling level */
316670eb
A
62} cqrq_throttle_t;
63
39236c6e
A
64/* classq service class stats request argument */
65typedef struct cqrq_stat_sc {
0a7de745
A
66 mbuf_svc_class_t sc; /* (in) service class */
67 u_int32_t packets; /* (out) packets enqueued */
68 u_int32_t bytes; /* (out) bytes enqueued */
39236c6e
A
69} cqrq_stat_sc_t;
70
316670eb
A
71/*
72 * A token-bucket regulator limits the rate that a network driver can
73 * dequeue packets from the output queue. Modern cards are able to buffer
74 * a large amount of packets and dequeue too many packets at a time. This
75 * bursty dequeue behavior makes it impossible to schedule packets by
76 * queueing disciplines. A token-bucket is used to control the burst size
77 * in a device independent manner.
78 */
79struct tb_regulator {
0a7de745
A
80 u_int64_t tbr_rate_raw; /* (unscaled) token bucket rate */
81 u_int32_t tbr_percent; /* token bucket rate in percentage */
82 int64_t tbr_rate; /* (scaled) token bucket rate */
83 int64_t tbr_depth; /* (scaled) token bucket depth */
316670eb 84
0a7de745
A
85 int64_t tbr_token; /* (scaled) current token */
86 int64_t tbr_filluptime; /* (scaled) time to fill up bucket */
87 u_int64_t tbr_last; /* last time token was updated */
316670eb 88
0a7de745 89 /* needed for poll-and-dequeue */
316670eb
A
90};
91
92/* simple token bucket meter profile */
93struct tb_profile {
0a7de745
A
94 u_int64_t rate; /* rate in bit-per-sec */
95 u_int32_t percent; /* rate in percentage */
96 u_int32_t depth; /* depth in bytes */
316670eb
A
97};
98
99struct ifclassq;
100enum cqdq_op;
101enum cqrq;
102
cb323159 103typedef int (*ifclassq_enq_func)(struct ifclassq *, classq_pkt_t *,
5ba3f43e 104 boolean_t *);
cb323159
A
105typedef void (*ifclassq_deq_func)(struct ifclassq *, classq_pkt_t *);
106typedef void (*ifclassq_deq_sc_func)(struct ifclassq *, mbuf_svc_class_t,
107 classq_pkt_t *);
5ba3f43e 108typedef int (*ifclassq_deq_multi_func)(struct ifclassq *, u_int32_t,
cb323159 109 u_int32_t, classq_pkt_t *, classq_pkt_t *, u_int32_t *, u_int32_t *);
5ba3f43e 110typedef int (*ifclassq_deq_sc_multi_func)(struct ifclassq *,
cb323159
A
111 mbuf_svc_class_t, u_int32_t, u_int32_t, classq_pkt_t *, classq_pkt_t *,
112 u_int32_t *, u_int32_t *);
316670eb
A
113typedef int (*ifclassq_req_func)(struct ifclassq *, enum cqrq, void *);
114
115/*
116 * Structure defining a queue for a network interface.
117 */
118struct ifclassq {
119 decl_lck_mtx_data(, ifcq_lock);
120
0a7de745
A
121 struct ifnet *ifcq_ifp; /* back pointer to interface */
122 u_int32_t ifcq_len; /* packet count */
123 u_int32_t ifcq_maxlen;
124 struct pktcntr ifcq_xmitcnt;
125 struct pktcntr ifcq_dropcnt;
126
127 u_int32_t ifcq_type; /* scheduler type */
128 u_int32_t ifcq_flags; /* flags */
129 u_int32_t ifcq_sflags; /* scheduler flags */
130 u_int32_t ifcq_target_qdelay; /* target queue delay */
131 u_int32_t ifcq_bytes; /* bytes count */
132 u_int32_t ifcq_pkt_drop_limit;
133 void *ifcq_disc; /* for scheduler-specific use */
316670eb
A
134 /*
135 * ifcq_disc_slots[] represents the leaf classes configured for the
136 * corresponding discpline/scheduler, ordered by their corresponding
137 * service class index. Each slot holds the queue ID used to identify
138 * the class instance, as well as the class instance pointer itself.
139 * The latter is used during enqueue and dequeue in order to avoid the
140 * costs associated with looking up the class pointer based on the
141 * queue ID. The queue ID is used when querying the statistics from
142 * user space.
143 *
144 * Avoiding the use of queue ID during enqueue and dequeue is made
145 * possible by virtue of knowing the particular mbuf service class
146 * associated with the packets. The service class index of the
147 * packet is used as the index to ifcq_disc_slots[].
148 *
149 * ifcq_disc_slots[] therefore also acts as a lookup table which
150 * provides for the mapping between MBUF_SC values and the actual
151 * scheduler classes.
152 */
153 struct ifclassq_disc_slot {
0a7de745
A
154 u_int32_t qid;
155 void *cl;
316670eb
A
156 } ifcq_disc_slots[IFCQ_SC_MAX]; /* for discipline use */
157
316670eb 158 /* token bucket regulator */
0a7de745 159 struct tb_regulator ifcq_tbr; /* TBR */
316670eb
A
160};
161
316670eb 162/* ifcq_flags */
0a7de745
A
163#define IFCQF_READY 0x01 /* ifclassq supports discipline */
164#define IFCQF_ENABLED 0x02 /* ifclassq is in use */
165#define IFCQF_TBR 0x04 /* Token Bucket Regulator is in use */
316670eb 166
0a7de745
A
167#define IFCQ_IS_READY(_ifcq) ((_ifcq)->ifcq_flags & IFCQF_READY)
168#define IFCQ_IS_ENABLED(_ifcq) ((_ifcq)->ifcq_flags & IFCQF_ENABLED)
169#define IFCQ_TBR_IS_ENABLED(_ifcq) ((_ifcq)->ifcq_flags & IFCQF_TBR)
316670eb
A
170
171/* classq enqueue return value */
5ba3f43e 172/* packet has to be dropped */
0a7de745 173#define CLASSQEQ_DROP (-1)
5ba3f43e 174/* packet successfully enqueued */
0a7de745 175#define CLASSQEQ_SUCCESS 0
5ba3f43e 176/* packet enqueued; give flow control feedback */
0a7de745 177#define CLASSQEQ_SUCCESS_FC 1
5ba3f43e 178/* packet needs to be dropped due to flowcontrol; give flow control feedback */
0a7de745 179#define CLASSQEQ_DROP_FC 2
5ba3f43e 180/* packet needs to be dropped due to suspension; give flow control feedback */
0a7de745 181#define CLASSQEQ_DROP_SP 3
f427ee49
A
182/* packet has been compressed with another one */
183#define CLASSQEQ_COMPRESSED 4
316670eb
A
184
185/* interface event argument for CLASSQRQ_EVENT */
186typedef enum cqev {
5ba3f43e 187 CLASSQ_EV_INIT = 0,
0a7de745
A
188 CLASSQ_EV_LINK_BANDWIDTH = 1, /* link bandwidth has changed */
189 CLASSQ_EV_LINK_LATENCY = 2, /* link latency has changed */
190 CLASSQ_EV_LINK_MTU = 3, /* link MTU has changed */
191 CLASSQ_EV_LINK_UP = 4, /* link is now up */
192 CLASSQ_EV_LINK_DOWN = 5, /* link is now down */
316670eb
A
193} cqev_t;
194#endif /* BSD_KERNEL_PRIVATE */
195
f427ee49 196#include <net/classq/classq.h>
39037602 197#include <net/pktsched/pktsched_fq_codel.h>
316670eb
A
198
199#ifdef __cplusplus
200extern "C" {
201#endif
316670eb 202struct if_ifclassq_stats {
0a7de745
A
203 u_int32_t ifqs_len;
204 u_int32_t ifqs_maxlen;
205 struct pktcntr ifqs_xmitcnt;
206 struct pktcntr ifqs_dropcnt;
207 u_int32_t ifqs_scheduler;
f427ee49 208 struct fq_codel_classstats ifqs_fq_codel_stats;
316670eb
A
209} __attribute__((aligned(8)));
210
211#ifdef __cplusplus
212}
213#endif
214
215#ifdef BSD_KERNEL_PRIVATE
216/*
217 * For ifclassq lock
218 */
0a7de745 219#define IFCQ_LOCK_ASSERT_HELD(_ifcq) \
5ba3f43e 220 LCK_MTX_ASSERT(&(_ifcq)->ifcq_lock, LCK_MTX_ASSERT_OWNED)
316670eb 221
0a7de745 222#define IFCQ_LOCK_ASSERT_NOTHELD(_ifcq) \
5ba3f43e 223 LCK_MTX_ASSERT(&(_ifcq)->ifcq_lock, LCK_MTX_ASSERT_NOTOWNED)
316670eb 224
0a7de745 225#define IFCQ_LOCK(_ifcq) \
316670eb
A
226 lck_mtx_lock(&(_ifcq)->ifcq_lock)
227
0a7de745 228#define IFCQ_LOCK_SPIN(_ifcq) \
316670eb
A
229 lck_mtx_lock_spin(&(_ifcq)->ifcq_lock)
230
0a7de745
A
231#define IFCQ_CONVERT_LOCK(_ifcq) do { \
232 IFCQ_LOCK_ASSERT_HELD(_ifcq); \
233 lck_mtx_convert_spin(&(_ifcq)->ifcq_lock); \
316670eb
A
234} while (0)
235
0a7de745 236#define IFCQ_UNLOCK(_ifcq) \
316670eb
A
237 lck_mtx_unlock(&(_ifcq)->ifcq_lock)
238
239/*
240 * For ifclassq operations
241 */
cb323159
A
242#define IFCQ_TBR_DEQUEUE(_ifcq, _p) do { \
243 ifclassq_tbr_dequeue(_ifcq, _p); \
316670eb
A
244} while (0)
245
cb323159
A
246#define IFCQ_TBR_DEQUEUE_SC(_ifcq, _sc, _p) do { \
247 ifclassq_tbr_dequeue_sc(_ifcq, _sc, _p); \
316670eb
A
248} while (0)
249
0a7de745
A
250#define IFCQ_LEN(_ifcq) ((_ifcq)->ifcq_len)
251#define IFCQ_QFULL(_ifcq) (IFCQ_LEN(_ifcq) >= (_ifcq)->ifcq_maxlen)
252#define IFCQ_IS_EMPTY(_ifcq) (IFCQ_LEN(_ifcq) == 0)
253#define IFCQ_INC_LEN(_ifcq) (IFCQ_LEN(_ifcq)++)
254#define IFCQ_DEC_LEN(_ifcq) (IFCQ_LEN(_ifcq)--)
f427ee49
A
255#define IFCQ_ADD_LEN(_ifcq, _len) (IFCQ_LEN(_ifcq) += (_len))
256#define IFCQ_SUB_LEN(_ifcq, _len) (IFCQ_LEN(_ifcq) -= (_len))
0a7de745
A
257#define IFCQ_MAXLEN(_ifcq) ((_ifcq)->ifcq_maxlen)
258#define IFCQ_SET_MAXLEN(_ifcq, _len) ((_ifcq)->ifcq_maxlen = (_len))
259#define IFCQ_TARGET_QDELAY(_ifcq) ((_ifcq)->ifcq_target_qdelay)
260#define IFCQ_BYTES(_ifcq) ((_ifcq)->ifcq_bytes)
261#define IFCQ_INC_BYTES(_ifcq, _len) \
39037602 262 ((_ifcq)->ifcq_bytes = (_ifcq)->ifcq_bytes + (_len))
0a7de745 263#define IFCQ_DEC_BYTES(_ifcq, _len) \
39037602 264 ((_ifcq)->ifcq_bytes = (_ifcq)->ifcq_bytes - (_len))
316670eb 265
0a7de745
A
266#define IFCQ_XMIT_ADD(_ifcq, _pkt, _len) do { \
267 PKTCNTR_ADD(&(_ifcq)->ifcq_xmitcnt, _pkt, _len); \
316670eb
A
268} while (0)
269
0a7de745
A
270#define IFCQ_DROP_ADD(_ifcq, _pkt, _len) do { \
271 PKTCNTR_ADD(&(_ifcq)->ifcq_dropcnt, _pkt, _len); \
316670eb
A
272} while (0)
273
0a7de745 274#define IFCQ_PKT_DROP_LIMIT(_ifcq) ((_ifcq)->ifcq_pkt_drop_limit)
5ba3f43e 275
316670eb
A
276extern int ifclassq_setup(struct ifnet *, u_int32_t, boolean_t);
277extern void ifclassq_teardown(struct ifnet *);
278extern int ifclassq_pktsched_setup(struct ifclassq *);
279extern void ifclassq_set_maxlen(struct ifclassq *, u_int32_t);
280extern u_int32_t ifclassq_get_maxlen(struct ifclassq *);
39236c6e
A
281extern int ifclassq_get_len(struct ifclassq *, mbuf_svc_class_t,
282 u_int32_t *, u_int32_t *);
f427ee49
A
283extern errno_t ifclassq_enqueue(struct ifclassq *, classq_pkt_t *,
284 classq_pkt_t *, u_int32_t, u_int32_t, boolean_t *);
39037602 285extern errno_t ifclassq_dequeue(struct ifclassq *, u_int32_t, u_int32_t,
cb323159 286 classq_pkt_t *, classq_pkt_t *, u_int32_t *, u_int32_t *);
316670eb 287extern errno_t ifclassq_dequeue_sc(struct ifclassq *, mbuf_svc_class_t,
cb323159
A
288 u_int32_t, u_int32_t, classq_pkt_t *, classq_pkt_t *, u_int32_t *,
289 u_int32_t *);
5ba3f43e
A
290extern void *ifclassq_poll(struct ifclassq *, classq_pkt_type_t *);
291extern void *ifclassq_poll_sc(struct ifclassq *, mbuf_svc_class_t,
292 classq_pkt_type_t *);
316670eb 293extern void ifclassq_update(struct ifclassq *, cqev_t);
f427ee49
A
294extern int ifclassq_attach(struct ifclassq *, u_int32_t, void *);
295extern void ifclassq_detach(struct ifclassq *);
316670eb
A
296extern int ifclassq_getqstats(struct ifclassq *, u_int32_t,
297 void *, u_int32_t *);
298extern const char *ifclassq_ev2str(cqev_t);
299extern int ifclassq_tbr_set(struct ifclassq *, struct tb_profile *, boolean_t);
cb323159
A
300extern void ifclassq_tbr_dequeue(struct ifclassq *, classq_pkt_t *);
301extern void ifclassq_tbr_dequeue_sc(struct ifclassq *, mbuf_svc_class_t,
302 classq_pkt_t *);
39037602
A
303extern void ifclassq_calc_target_qdelay(struct ifnet *ifp,
304 u_int64_t *if_target_qdelay);
305extern void ifclassq_calc_update_interval(u_int64_t *update_interval);
5ba3f43e 306extern void ifclassq_set_packet_metadata(struct ifclassq *ifq,
cb323159 307 struct ifnet *ifp, classq_pkt_t *p);
a39ff7e2 308extern void ifclassq_reap_caches(boolean_t);
39037602 309
316670eb
A
310#endif /* BSD_KERNEL_PRIVATE */
311#endif /* PRIVATE */
312#endif /* _NET_CLASSQ_IF_CLASSQ_H_ */