xref: /xnu-12377.81.4/bsd/net/classq/classq.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2007-2021 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions /*	$NetBSD: altq_classq.h,v 1.7 2006/10/12 19:59:08 peter Exp $	*/
30*043036a2SApple OSS Distributions /*	$KAME: altq_classq.h,v 1.6 2003/01/07 07:33:38 kjc Exp $	*/
31*043036a2SApple OSS Distributions 
32*043036a2SApple OSS Distributions /*
33*043036a2SApple OSS Distributions  * Copyright (c) 1991-1997 Regents of the University of California.
34*043036a2SApple OSS Distributions  * All rights reserved.
35*043036a2SApple OSS Distributions  *
36*043036a2SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
37*043036a2SApple OSS Distributions  * modification, are permitted provided that the following conditions
38*043036a2SApple OSS Distributions  * are met:
39*043036a2SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
40*043036a2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
41*043036a2SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
42*043036a2SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
43*043036a2SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
44*043036a2SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
45*043036a2SApple OSS Distributions  *    must display the following acknowledgement:
46*043036a2SApple OSS Distributions  *	This product includes software developed by the Network Research
47*043036a2SApple OSS Distributions  *	Group at Lawrence Berkeley Laboratory.
48*043036a2SApple OSS Distributions  * 4. Neither the name of the University nor of the Laboratory may be used
49*043036a2SApple OSS Distributions  *    to endorse or promote products derived from this software without
50*043036a2SApple OSS Distributions  *    specific prior written permission.
51*043036a2SApple OSS Distributions  *
52*043036a2SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53*043036a2SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54*043036a2SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55*043036a2SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56*043036a2SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57*043036a2SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58*043036a2SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59*043036a2SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60*043036a2SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61*043036a2SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62*043036a2SApple OSS Distributions  * SUCH DAMAGE.
63*043036a2SApple OSS Distributions  */
64*043036a2SApple OSS Distributions /*
65*043036a2SApple OSS Distributions  * class queue definitions extracted from rm_class.h.
66*043036a2SApple OSS Distributions  */
67*043036a2SApple OSS Distributions #ifndef _NET_CLASSQ_CLASSQ_H_
68*043036a2SApple OSS Distributions #define _NET_CLASSQ_CLASSQ_H_
69*043036a2SApple OSS Distributions 
70*043036a2SApple OSS Distributions #ifdef PRIVATE
71*043036a2SApple OSS Distributions #ifdef __cplusplus
72*043036a2SApple OSS Distributions extern "C" {
73*043036a2SApple OSS Distributions #endif
74*043036a2SApple OSS Distributions 
75*043036a2SApple OSS Distributions /*
76*043036a2SApple OSS Distributions  * Packet types
77*043036a2SApple OSS Distributions  */
78*043036a2SApple OSS Distributions typedef enum classq_pkt_type {
79*043036a2SApple OSS Distributions 	QP_INVALID = 0,
80*043036a2SApple OSS Distributions 	QP_MBUF,        /* mbuf packet */
81*043036a2SApple OSS Distributions #if SKYWALK
82*043036a2SApple OSS Distributions 	QP_PACKET,      /* skywalk packet */
83*043036a2SApple OSS Distributions #endif /* SKYWALK */
84*043036a2SApple OSS Distributions } classq_pkt_type_t;
85*043036a2SApple OSS Distributions 
86*043036a2SApple OSS Distributions /*
87*043036a2SApple OSS Distributions  * Packet
88*043036a2SApple OSS Distributions  */
89*043036a2SApple OSS Distributions typedef struct classq_pkt {
90*043036a2SApple OSS Distributions 	union {
91*043036a2SApple OSS Distributions 		struct mbuf             *cp_mbuf;       /* mbuf packet */
92*043036a2SApple OSS Distributions #if SKYWALK
93*043036a2SApple OSS Distributions 		struct __kern_packet    *cp_kpkt;       /* skywalk packet */
94*043036a2SApple OSS Distributions #endif /* SKYWALK */
95*043036a2SApple OSS Distributions 	};
96*043036a2SApple OSS Distributions 	classq_pkt_type_t       cp_ptype;
97*043036a2SApple OSS Distributions } classq_pkt_t;
98*043036a2SApple OSS Distributions 
99*043036a2SApple OSS Distributions #define CLASSQ_PKT_INITIALIZER(_p)      \
100*043036a2SApple OSS Distributions 	(classq_pkt_t){ .cp_mbuf = NULL, .cp_ptype = QP_INVALID }
101*043036a2SApple OSS Distributions 
102*043036a2SApple OSS Distributions #define CLASSQ_PKT_INIT(_p)    do {    \
103*043036a2SApple OSS Distributions 	(_p)->cp_ptype = QP_INVALID;   \
104*043036a2SApple OSS Distributions 	(_p)->cp_mbuf = NULL;          \
105*043036a2SApple OSS Distributions } while (0)
106*043036a2SApple OSS Distributions 
107*043036a2SApple OSS Distributions #define CLASSQ_PKT_INIT_MBUF(_p, _m)    do {    \
108*043036a2SApple OSS Distributions 	(_p)->cp_ptype = QP_MBUF;               \
109*043036a2SApple OSS Distributions 	(_p)->cp_mbuf = (_m);                   \
110*043036a2SApple OSS Distributions } while (0)
111*043036a2SApple OSS Distributions 
112*043036a2SApple OSS Distributions #if SKYWALK
113*043036a2SApple OSS Distributions #define CLASSQ_PKT_INIT_PACKET(_p, _k)  do {    \
114*043036a2SApple OSS Distributions 	(_p)->cp_ptype = QP_PACKET;             \
115*043036a2SApple OSS Distributions 	(_p)->cp_kpkt = (_k);                   \
116*043036a2SApple OSS Distributions } while (0)
117*043036a2SApple OSS Distributions #endif /* SKYWALK */
118*043036a2SApple OSS Distributions 
119*043036a2SApple OSS Distributions /*
120*043036a2SApple OSS Distributions  * Packet Queue types
121*043036a2SApple OSS Distributions  */
122*043036a2SApple OSS Distributions typedef enum classq_type {
123*043036a2SApple OSS Distributions 	Q_DROPHEAD,
124*043036a2SApple OSS Distributions 	Q_DROPTAIL,
125*043036a2SApple OSS Distributions 	Q_SFB
126*043036a2SApple OSS Distributions } classq_type_t;
127*043036a2SApple OSS Distributions 
128*043036a2SApple OSS Distributions /*
129*043036a2SApple OSS Distributions  * Packet Queue states
130*043036a2SApple OSS Distributions  */
131*043036a2SApple OSS Distributions typedef enum classq_state {
132*043036a2SApple OSS Distributions 	QS_RUNNING,
133*043036a2SApple OSS Distributions 	QS_SUSPENDED
134*043036a2SApple OSS Distributions } classq_state_t;
135*043036a2SApple OSS Distributions 
136*043036a2SApple OSS Distributions #define DEFAULT_QLIMIT  128 /* default */
137*043036a2SApple OSS Distributions 
138*043036a2SApple OSS Distributions #define CLASSQ_DEQUEUE_MAX_PKT_LIMIT    2048
139*043036a2SApple OSS Distributions #define CLASSQ_DEQUEUE_MAX_BYTE_LIMIT   (1024 * 1024)
140*043036a2SApple OSS Distributions 
141*043036a2SApple OSS Distributions /*
142*043036a2SApple OSS Distributions  * generic packet counter
143*043036a2SApple OSS Distributions  */
144*043036a2SApple OSS Distributions struct pktcntr {
145*043036a2SApple OSS Distributions 	u_int64_t       packets;
146*043036a2SApple OSS Distributions 	u_int64_t       bytes;
147*043036a2SApple OSS Distributions };
148*043036a2SApple OSS Distributions 
149*043036a2SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE
150*043036a2SApple OSS Distributions #include <sys/mcache.h>
151*043036a2SApple OSS Distributions #include <sys/mbuf.h>
152*043036a2SApple OSS Distributions #include <sys/sysctl.h>
153*043036a2SApple OSS Distributions #if SKYWALK
154*043036a2SApple OSS Distributions #include <skywalk/packet/packet_queue.h>
155*043036a2SApple OSS Distributions #endif /* SKYWALK */
156*043036a2SApple OSS Distributions 
157*043036a2SApple OSS Distributions /*
158*043036a2SApple OSS Distributions  * Packet Queue structures and macros to manipulate them.
159*043036a2SApple OSS Distributions  */
160*043036a2SApple OSS Distributions typedef struct _class_queue_ {
161*043036a2SApple OSS Distributions 	union {
162*043036a2SApple OSS Distributions 		MBUFQ_HEAD(mq_head) __mbufq; /* mbuf packet queue */
163*043036a2SApple OSS Distributions #if SKYWALK
164*043036a2SApple OSS Distributions 		KPKTQ_HEAD(kq_head) __kpktq; /* skywalk packet queue */
165*043036a2SApple OSS Distributions #endif /* SKYWALK */
166*043036a2SApple OSS Distributions 	} __pktq_u;
167*043036a2SApple OSS Distributions 	u_int32_t       qlen;   /* Queue length (in number of packets) */
168*043036a2SApple OSS Distributions 	u_int32_t       qlim;   /* Queue limit (in number of packets*) */
169*043036a2SApple OSS Distributions 	u_int64_t       qsize;  /* Approx. queue size (in number of bytes) */
170*043036a2SApple OSS Distributions 	classq_type_t   qtype;  /* Queue type */
171*043036a2SApple OSS Distributions 	classq_state_t  qstate; /* Queue state */
172*043036a2SApple OSS Distributions 	classq_pkt_type_t       qptype; /* Packet type */
173*043036a2SApple OSS Distributions } class_queue_t;
174*043036a2SApple OSS Distributions 
175*043036a2SApple OSS Distributions #define qmbufq(q)       (q)->__pktq_u.__mbufq   /* Get mbuf packet queue */
176*043036a2SApple OSS Distributions #if SKYWALK
177*043036a2SApple OSS Distributions #define qkpktq(q)       (q)->__pktq_u.__kpktq   /* Get kernel packet queue */
178*043036a2SApple OSS Distributions #endif /* SKYWALK */
179*043036a2SApple OSS Distributions #define qptype(q)       (q)->qptype             /* Get queue packet type */
180*043036a2SApple OSS Distributions #define qtype(q)        (q)->qtype              /* Get queue type */
181*043036a2SApple OSS Distributions #define qstate(q)       (q)->qstate             /* Get queue state */
182*043036a2SApple OSS Distributions #define qlimit(q)       (q)->qlim               /* Max packets to be queued */
183*043036a2SApple OSS Distributions #define qlen(q)         (q)->qlen               /* Current queue length. */
184*043036a2SApple OSS Distributions #define qsize(q)        (q)->qsize              /* Approx. bytes in queue */
185*043036a2SApple OSS Distributions 
186*043036a2SApple OSS Distributions #if SKYWALK
187*043036a2SApple OSS Distributions #define qhead(q)        ((qptype(q) == QP_MBUF) ?               \
188*043036a2SApple OSS Distributions 	                    (void *)MBUFQ_FIRST(&qmbufq(q)) :   \
189*043036a2SApple OSS Distributions 	                    (void *)KPKTQ_FIRST(&qkpktq(q)))
190*043036a2SApple OSS Distributions #else /* !SKYWALK */
191*043036a2SApple OSS Distributions #define qhead(q)        MBUFQ_FIRST(&qmbufq(q))
192*043036a2SApple OSS Distributions #endif /* !SKYWALK */
193*043036a2SApple OSS Distributions 
194*043036a2SApple OSS Distributions #define qempty(q)       (qlen(q) == 0)  /* Is the queue empty?? */
195*043036a2SApple OSS Distributions #define q_is_red(q)     (qtype(q) == Q_RED)     /* Is the queue a RED queue */
196*043036a2SApple OSS Distributions #define q_is_rio(q)     (qtype(q) == Q_RIO)     /* Is the queue a RIO queue */
197*043036a2SApple OSS Distributions #define q_is_blue(q)    (qtype(q) == Q_BLUE)    /* Is the queue a BLUE queue */
198*043036a2SApple OSS Distributions #define q_is_sfb(q)     (qtype(q) == Q_SFB)     /* Is the queue a SFB queue */
199*043036a2SApple OSS Distributions #define q_is_red_or_rio(q) (qtype(q) == Q_RED || qtype(q) == Q_RIO)
200*043036a2SApple OSS Distributions #define q_is_suspended(q) (qstate(q) == QS_SUSPENDED)
201*043036a2SApple OSS Distributions 
202*043036a2SApple OSS Distributions #define PKTCNTR_ADD(_cntr, _pkt, _len) do {                             \
203*043036a2SApple OSS Distributions 	(_cntr)->packets += (_pkt);                                     \
204*043036a2SApple OSS Distributions 	(_cntr)->bytes += (_len);                                       \
205*043036a2SApple OSS Distributions } while (0)
206*043036a2SApple OSS Distributions 
207*043036a2SApple OSS Distributions #define PKTCNTR_CLEAR(_cntr) do {                                       \
208*043036a2SApple OSS Distributions 	(_cntr)->packets = 0;                                           \
209*043036a2SApple OSS Distributions 	(_cntr)->bytes = 0;                                             \
210*043036a2SApple OSS Distributions } while (0)
211*043036a2SApple OSS Distributions 
212*043036a2SApple OSS Distributions /* flags for mark_ecn() */
213*043036a2SApple OSS Distributions #define CLASSQF_ECN4    0x01    /* use packet marking for IPv4 packets */
214*043036a2SApple OSS Distributions #define CLASSQF_ECN6    0x02    /* use packet marking for IPv6 packets */
215*043036a2SApple OSS Distributions #define CLASSQF_ECN     (CLASSQF_ECN4 | CLASSQF_ECN6)
216*043036a2SApple OSS Distributions 
217*043036a2SApple OSS Distributions extern u_int32_t classq_verbose;
218*043036a2SApple OSS Distributions 
219*043036a2SApple OSS Distributions SYSCTL_DECL(_net_classq);
220*043036a2SApple OSS Distributions 
221*043036a2SApple OSS Distributions extern void _qinit(class_queue_t *, int, int, classq_pkt_type_t);
222*043036a2SApple OSS Distributions extern void _addq(class_queue_t *, classq_pkt_t *);
223*043036a2SApple OSS Distributions extern void _addq_multi(class_queue_t *, classq_pkt_t *, classq_pkt_t *,
224*043036a2SApple OSS Distributions     u_int32_t, u_int64_t);
225*043036a2SApple OSS Distributions extern void _getq(class_queue_t *, classq_pkt_t *);
226*043036a2SApple OSS Distributions extern void _getq_all(class_queue_t *, classq_pkt_t *, classq_pkt_t *,
227*043036a2SApple OSS Distributions     u_int32_t *, u_int64_t *);
228*043036a2SApple OSS Distributions extern void _getq_tail(class_queue_t *, classq_pkt_t *);
229*043036a2SApple OSS Distributions extern void _getq_random(class_queue_t *, classq_pkt_t *);
230*043036a2SApple OSS Distributions extern void _getq_flow(class_queue_t *, classq_pkt_t *, u_int32_t);
231*043036a2SApple OSS Distributions extern void _getq_scidx_lt(class_queue_t *, classq_pkt_t *, u_int32_t);
232*043036a2SApple OSS Distributions extern void _removeq(class_queue_t *, classq_pkt_t *);
233*043036a2SApple OSS Distributions extern void _flushq(class_queue_t *);
234*043036a2SApple OSS Distributions extern void _flushq_flow(class_queue_t *, u_int32_t, u_int32_t *, u_int32_t *);
235*043036a2SApple OSS Distributions 
236*043036a2SApple OSS Distributions extern void classq_init(void);
237*043036a2SApple OSS Distributions 
238*043036a2SApple OSS Distributions #if PF_ECN
239*043036a2SApple OSS Distributions extern u_int8_t read_dsfield(struct mbuf *, struct pf_mtag *);
240*043036a2SApple OSS Distributions extern void     write_dsfield(struct mbuf *, struct pf_mtag *, u_int8_t);
241*043036a2SApple OSS Distributions extern int      mark_ecn(struct mbuf *, struct pf_mtag *, int);
242*043036a2SApple OSS Distributions #endif /* PF_ECN */
243*043036a2SApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */
244*043036a2SApple OSS Distributions 
245*043036a2SApple OSS Distributions #ifdef __cplusplus
246*043036a2SApple OSS Distributions }
247*043036a2SApple OSS Distributions #endif
248*043036a2SApple OSS Distributions #endif /* PRIVATE */
249*043036a2SApple OSS Distributions #endif /* _NET_CLASSQ_CLASSQ_H_ */
250