1/*
2 * Copyright (c) 2011-2020 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 _PKTSCHED_PKTSCHED_H_
30#define _PKTSCHED_PKTSCHED_H_
31
32#ifdef PRIVATE
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37/* packet scheduler type */
38#define PKTSCHEDT_NONE 0 /* reserved */
39#define PKTSCHEDT_CBQ 1 /* cbq */
40#define PKTSCHEDT_HFSC 2 /* hfsc */
41#define PKTSCHEDT_PRIQ 3 /* priority queue */
42#define PKTSCHEDT_FAIRQ 4 /* fairq */
43#define PKTSCHEDT_TCQ 5 /* traffic class queue */
44#define PKTSCHEDT_QFQ 6 /* quick fair queueing */
45#define PKTSCHEDT_FQ_CODEL 7 /* Flow queues with CoDel */
46#define PKTSCHEDT_MAX 8 /* should be max sched type + 1 */
47
48#ifdef BSD_KERNEL_PRIVATE
49#include <mach/mach_time.h>
50#include <sys/sysctl.h>
51#include <libkern/libkern.h>
52
53/* flags for pktsched_setup */
54#define PKTSCHEDF_QALG_ECN 0x01 /* enable ECN */
55#define PKTSCHEDF_QALG_FLOWCTL 0x02 /* enable flow control advisories */
56#define PKTSCHEDF_QALG_DELAYBASED 0x04 /* Delay based queueing */
57#define PKTSCHEDF_QALG_DRIVER_MANAGED 0x08 /* driver managed */
58
59typedef struct _pktsched_pkt_ {
60 classq_pkt_t __pkt;
61 classq_pkt_t __tail;
62 uint32_t __plen;
63 uint32_t __pcnt;
64#define pktsched_ptype __pkt.cp_ptype
65#define pktsched_plen __plen
66#define pktsched_pcnt __pcnt
67#define pktsched_pkt __pkt
68#define pktsched_pkt_mbuf __pkt.cp_mbuf
69#define pktsched_pkt_kpkt __pkt.cp_kpkt
70#define pktsched_tail __tail
71#define pktsched_tail_mbuf __tail.cp_mbuf
72#define pktsched_tail_kpkt __tail.cp_kpkt
73} pktsched_pkt_t;
74
75#define _PKTSCHED_PKT_INIT(_p) do { \
76 (_p)->pktsched_pkt = CLASSQ_PKT_INITIALIZER((_p)->pktsched_pkt);\
77 (_p)->pktsched_tail = CLASSQ_PKT_INITIALIZER((_p)->pktsched_tail);\
78 (_p)->pktsched_plen = 0; \
79 (_p)->pktsched_pcnt = 0; \
80} while (0)
81
82/* macro for timeout/untimeout */
83/* use old-style timeout/untimeout */
84/* dummy callout structure */
85struct callout {
86 void *c_arg; /* function argument */
87 void (*c_func)(void *); /* function to call */
88};
89
90#define CALLOUT_INIT(c) do { \
91 (void) memset((c), 0, sizeof (*(c))); \
92} while ( /*CONSTCOND*/ 0)
93
94#define CALLOUT_RESET(c, t, f, a) do { \
95 (c)->c_arg = (a); \
96 (c)->c_func = (f); \
97 timeout((f), (a), (t)); \
98} while ( /*CONSTCOND*/ 0)
99
100#define CALLOUT_STOP(c) untimeout((c)->c_func, (c)->c_arg)
101#define CALLOUT_INITIALIZER { NULL, NULL }
102
103typedef void (timeout_t)(void *);
104
105/*
106 * Bitmap operations
107 */
108typedef u_int32_t pktsched_bitmap_t;
109
110static inline boolean_t
111pktsched_bit_tst(u_int32_t ix, pktsched_bitmap_t *pData)
112{
113 return (boolean_t)(*pData & (1 << ix));
114}
115
116static inline void
117pktsched_bit_set(u_int32_t ix, pktsched_bitmap_t *pData)
118{
119 *pData |= (1 << ix);
120}
121
122static inline void
123pktsched_bit_clr(u_int32_t ix, pktsched_bitmap_t *pData)
124{
125 *pData &= ~(1 << ix);
126}
127
128static inline void
129pktsched_bit_cpy(u_int32_t ix, pktsched_bitmap_t *pData_dst,
130 pktsched_bitmap_t *pData_src)
131{
132 *pData_dst ^= (-(*pData_src & (1 << ix)) ^ *pData_dst) & (1 << ix);
133}
134
135static inline pktsched_bitmap_t
136pktsched_ffs(pktsched_bitmap_t pData)
137{
138 return (pktsched_bitmap_t)ffs(pData);
139}
140
141static inline pktsched_bitmap_t
142pktsched_fls(pktsched_bitmap_t pData)
143{
144 return (pktsched_bitmap_t)((sizeof(pktsched_bitmap_t) << 3) - (unsigned long)clz(num: pData));
145}
146
147static inline pktsched_bitmap_t
148__fls(pktsched_bitmap_t word)
149{
150 VERIFY(word != 0);
151 return pktsched_fls(pData: word) - 1;
152}
153
154static inline uint32_t
155pktsched_get_pkt_len(pktsched_pkt_t *pkt)
156{
157 return pkt->pktsched_plen;
158}
159
160static inline void
161pktsched_bit_move(u_int32_t ix, pktsched_bitmap_t *pData_dst,
162 pktsched_bitmap_t *pData_src)
163{
164 *pData_dst |= (*pData_src & (1 << ix));
165}
166
167/*
168 * We can use mach_absolute_time which returns a 64-bit value with
169 * granularity less than a microsecond even on the slowest processor.
170 */
171#define read_machclk() mach_absolute_time()
172
173/*
174 * machine dependent clock
175 * a 64bit high resolution time counter.
176 */
177extern uint32_t machclk_freq;
178extern uint64_t machclk_per_sec;
179extern uint32_t pktsched_verbose;
180
181SYSCTL_DECL(_net_pktsched);
182
183struct if_ifclassq_stats;
184
185extern void pktsched_register_m_tag(void);
186
187extern void pktsched_init(void);
188extern int pktsched_setup(struct ifclassq *, u_int32_t, u_int32_t,
189 classq_pkt_type_t);
190extern void pktsched_teardown(struct ifclassq *);
191extern int pktsched_getqstats(struct ifclassq *, u_int32_t, u_int32_t,
192 struct if_ifclassq_stats *);
193extern u_int64_t pktsched_abs_to_nsecs(u_int64_t);
194extern u_int64_t pktsched_nsecs_to_abstime(u_int64_t);
195extern void pktsched_free_pkt(pktsched_pkt_t *);
196extern int pktsched_clone_pkt(pktsched_pkt_t *, pktsched_pkt_t *);
197extern void pktsched_corrupt_packet(pktsched_pkt_t *pkt);
198extern void pktsched_get_pkt_vars(pktsched_pkt_t *, volatile uint32_t **,
199 uint64_t **, uint32_t *, uint8_t *, uint8_t *, uint32_t *, uint64_t *);
200extern uint32_t *pktsched_get_pkt_sfb_vars(pktsched_pkt_t *, uint32_t **);
201extern void pktsched_pkt_encap(pktsched_pkt_t *, classq_pkt_t *);
202extern void pktsched_pkt_encap_chain(pktsched_pkt_t *, classq_pkt_t *,
203 classq_pkt_t *, uint32_t, uint32_t);
204extern mbuf_svc_class_t pktsched_get_pkt_svc(pktsched_pkt_t *);
205extern struct flowadv_fcentry *pktsched_alloc_fcentry(pktsched_pkt_t *,
206 struct ifnet *, int);
207extern int pktsched_mark_ecn(pktsched_pkt_t *pkt);
208extern boolean_t pktsched_is_pkt_l4s(pktsched_pkt_t *pkt);
209#endif /* BSD_KERNEL_PRIVATE */
210
211#ifdef __cplusplus
212}
213#endif
214#endif /* PRIVATE */
215#endif /* _PKTSCHED_PKTSCHED_H_ */
216