xref: /xnu-10002.81.5/bsd/skywalk/packet/packet_queue.h (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions /*
2*5e3eaea3SApple OSS Distributions  * Copyright (c) 2016 Apple Inc. All rights reserved.
3*5e3eaea3SApple OSS Distributions  *
4*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5e3eaea3SApple OSS Distributions  *
6*5e3eaea3SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5e3eaea3SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5e3eaea3SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5e3eaea3SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5e3eaea3SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5e3eaea3SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5e3eaea3SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5e3eaea3SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5e3eaea3SApple OSS Distributions  *
15*5e3eaea3SApple OSS Distributions  * Please obtain a copy of the License at
16*5e3eaea3SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5e3eaea3SApple OSS Distributions  *
18*5e3eaea3SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5e3eaea3SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5e3eaea3SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5e3eaea3SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5e3eaea3SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5e3eaea3SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5e3eaea3SApple OSS Distributions  * limitations under the License.
25*5e3eaea3SApple OSS Distributions  *
26*5e3eaea3SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5e3eaea3SApple OSS Distributions  */
28*5e3eaea3SApple OSS Distributions 
29*5e3eaea3SApple OSS Distributions #ifndef _SKYWALK_PACKET_PACKETQUEUE_H_
30*5e3eaea3SApple OSS Distributions #define _SKYWALK_PACKET_PACKETQUEUE_H_
31*5e3eaea3SApple OSS Distributions 
32*5e3eaea3SApple OSS Distributions #ifdef BSD_KERNEL_PRIVATE
33*5e3eaea3SApple OSS Distributions 
34*5e3eaea3SApple OSS Distributions /*
35*5e3eaea3SApple OSS Distributions  * Simple __kern_packet queueing system
36*5e3eaea3SApple OSS Distributions  * This is basically a SIMPLEQ adapted to skywalk __kern_packet use.
37*5e3eaea3SApple OSS Distributions  */
38*5e3eaea3SApple OSS Distributions #define KPKTQ_HEAD(name)                                        \
39*5e3eaea3SApple OSS Distributions struct name {                                                   \
40*5e3eaea3SApple OSS Distributions 	struct __kern_packet *kq_first; /* first packet */              \
41*5e3eaea3SApple OSS Distributions 	struct __kern_packet **kq_last; /* addr of last next packet */  \
42*5e3eaea3SApple OSS Distributions 	uint32_t kq_len; /* number of packets in queue */       \
43*5e3eaea3SApple OSS Distributions }
44*5e3eaea3SApple OSS Distributions 
45*5e3eaea3SApple OSS Distributions #define KPKTQ_INIT(q)           do {                            \
46*5e3eaea3SApple OSS Distributions 	KPKTQ_FIRST(q) = NULL;                                  \
47*5e3eaea3SApple OSS Distributions 	(q)->kq_last = &KPKTQ_FIRST(q);                         \
48*5e3eaea3SApple OSS Distributions 	(q)->kq_len = 0;                                        \
49*5e3eaea3SApple OSS Distributions } while (0)
50*5e3eaea3SApple OSS Distributions 
51*5e3eaea3SApple OSS Distributions #define KPKTQ_FINI(q)           do {                            \
52*5e3eaea3SApple OSS Distributions 	ASSERT(KPKTQ_EMPTY(q));                                 \
53*5e3eaea3SApple OSS Distributions 	ASSERT(KPKTQ_LEN(q) == 0);                              \
54*5e3eaea3SApple OSS Distributions 	KPKTQ_INIT(q);                                          \
55*5e3eaea3SApple OSS Distributions } while (0)
56*5e3eaea3SApple OSS Distributions 
57*5e3eaea3SApple OSS Distributions #define KPKTQ_DISPOSE(q)        KPKTQ_INIT(q)
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions #define KPKTQ_CONCAT(q1, q2)    do {                            \
60*5e3eaea3SApple OSS Distributions 	if (!KPKTQ_EMPTY(q2)) {                                 \
61*5e3eaea3SApple OSS Distributions 	        *(q1)->kq_last = KPKTQ_FIRST(q2);               \
62*5e3eaea3SApple OSS Distributions 	        (q1)->kq_last = (q2)->kq_last;                  \
63*5e3eaea3SApple OSS Distributions 	        (q1)->kq_len += (q2)->kq_len;                   \
64*5e3eaea3SApple OSS Distributions 	        KPKTQ_DISPOSE((q2));                            \
65*5e3eaea3SApple OSS Distributions 	}                                                       \
66*5e3eaea3SApple OSS Distributions } while (0)
67*5e3eaea3SApple OSS Distributions 
68*5e3eaea3SApple OSS Distributions #define KPKTQ_PREPEND(q, p)     do {                            \
69*5e3eaea3SApple OSS Distributions 	if ((KPKTQ_NEXT(p) = KPKTQ_FIRST(q)) == NULL) {         \
70*5e3eaea3SApple OSS Distributions 	        ASSERT((q)->kq_len == 0);                       \
71*5e3eaea3SApple OSS Distributions 	        (q)->kq_last = &KPKTQ_NEXT(p);                 \
72*5e3eaea3SApple OSS Distributions 	}                                                       \
73*5e3eaea3SApple OSS Distributions 	KPKTQ_FIRST(q) = (p);                                   \
74*5e3eaea3SApple OSS Distributions 	(q)->kq_len++;                                          \
75*5e3eaea3SApple OSS Distributions } while (0)
76*5e3eaea3SApple OSS Distributions 
77*5e3eaea3SApple OSS Distributions #define KPKTQ_ENQUEUE(q, p)     do {                            \
78*5e3eaea3SApple OSS Distributions 	ASSERT(KPKTQ_NEXT(p) == NULL);                          \
79*5e3eaea3SApple OSS Distributions 	*(q)->kq_last = (p);                                    \
80*5e3eaea3SApple OSS Distributions 	(q)->kq_last = &KPKTQ_NEXT(p);                          \
81*5e3eaea3SApple OSS Distributions 	(q)->kq_len++;                                          \
82*5e3eaea3SApple OSS Distributions } while (0)
83*5e3eaea3SApple OSS Distributions 
84*5e3eaea3SApple OSS Distributions #define KPKTQ_ENQUEUE_MULTI(q, p, n, c)    do {                 \
85*5e3eaea3SApple OSS Distributions 	KPKTQ_NEXT(n) = NULL;                                   \
86*5e3eaea3SApple OSS Distributions 	*(q)->kq_last = (p);                                    \
87*5e3eaea3SApple OSS Distributions 	(q)->kq_last = &KPKTQ_NEXT(n);                          \
88*5e3eaea3SApple OSS Distributions 	(q)->kq_len += c;                                       \
89*5e3eaea3SApple OSS Distributions } while (0)
90*5e3eaea3SApple OSS Distributions 
91*5e3eaea3SApple OSS Distributions #define KPKTQ_ENQUEUE_LIST(q, p)           do {                 \
92*5e3eaea3SApple OSS Distributions 	uint32_t _c = 1;                                        \
93*5e3eaea3SApple OSS Distributions 	struct __kern_packet *_n = (p);                         \
94*5e3eaea3SApple OSS Distributions 	while (__improbable(KPKTQ_NEXT(_n) != NULL)) {          \
95*5e3eaea3SApple OSS Distributions 	        _c++;                                           \
96*5e3eaea3SApple OSS Distributions 	        _n = KPKTQ_NEXT(_n);                            \
97*5e3eaea3SApple OSS Distributions 	}                                                       \
98*5e3eaea3SApple OSS Distributions 	KPKTQ_ENQUEUE_MULTI(q, p, _n, _c);                      \
99*5e3eaea3SApple OSS Distributions } while (0)
100*5e3eaea3SApple OSS Distributions 
101*5e3eaea3SApple OSS Distributions #define KPKTQ_DEQUEUE(q, p)     do {                            \
102*5e3eaea3SApple OSS Distributions 	if (((p) = KPKTQ_FIRST(q)) != NULL) {                   \
103*5e3eaea3SApple OSS Distributions 	        (q)->kq_len--;                                  \
104*5e3eaea3SApple OSS Distributions 	        if ((KPKTQ_FIRST(q) = KPKTQ_NEXT(p)) == NULL) { \
105*5e3eaea3SApple OSS Distributions 	                ASSERT((q)->kq_len == 0);               \
106*5e3eaea3SApple OSS Distributions 	                (q)->kq_last = &KPKTQ_FIRST(q);         \
107*5e3eaea3SApple OSS Distributions 	        } else {                                        \
108*5e3eaea3SApple OSS Distributions 	                KPKTQ_NEXT(p) = NULL;                   \
109*5e3eaea3SApple OSS Distributions 	        }                                               \
110*5e3eaea3SApple OSS Distributions 	}                                                       \
111*5e3eaea3SApple OSS Distributions } while (0)
112*5e3eaea3SApple OSS Distributions 
113*5e3eaea3SApple OSS Distributions #define KPKTQ_REMOVE(q, p)      do {                            \
114*5e3eaea3SApple OSS Distributions 	if (KPKTQ_FIRST(q) == (p)) {                            \
115*5e3eaea3SApple OSS Distributions 	        KPKTQ_DEQUEUE(q, p);                            \
116*5e3eaea3SApple OSS Distributions 	} else {                                                \
117*5e3eaea3SApple OSS Distributions 	        struct __kern_packet *_p = KPKTQ_FIRST(q);      \
118*5e3eaea3SApple OSS Distributions 	        while (KPKTQ_NEXT(_p) != (p))                   \
119*5e3eaea3SApple OSS Distributions 	                _p = KPKTQ_NEXT(_p);                    \
120*5e3eaea3SApple OSS Distributions 	        if ((KPKTQ_NEXT(_p) =                           \
121*5e3eaea3SApple OSS Distributions 	            KPKTQ_NEXT(KPKTQ_NEXT(_p))) == NULL) {      \
122*5e3eaea3SApple OSS Distributions 	                (q)->kq_last = &KPKTQ_NEXT(_p);         \
123*5e3eaea3SApple OSS Distributions 	        }                                               \
124*5e3eaea3SApple OSS Distributions 	        (q)->kq_len--;                                  \
125*5e3eaea3SApple OSS Distributions 	        KPKTQ_NEXT(p) = NULL;                           \
126*5e3eaea3SApple OSS Distributions 	}                                                       \
127*5e3eaea3SApple OSS Distributions } while (0)
128*5e3eaea3SApple OSS Distributions 
129*5e3eaea3SApple OSS Distributions #define KPKTQ_FOREACH(p, q)                                     \
130*5e3eaea3SApple OSS Distributions 	for ((p) = KPKTQ_FIRST(q);                              \
131*5e3eaea3SApple OSS Distributions 	    (p);                                                \
132*5e3eaea3SApple OSS Distributions 	    (p) = KPKTQ_NEXT(p))
133*5e3eaea3SApple OSS Distributions 
134*5e3eaea3SApple OSS Distributions #define KPKTQ_FOREACH_SAFE(p, q, tvar)                          \
135*5e3eaea3SApple OSS Distributions 	for ((p) = KPKTQ_FIRST(q);                              \
136*5e3eaea3SApple OSS Distributions 	    (p) && ((tvar) = KPKTQ_NEXT(p), 1);                 \
137*5e3eaea3SApple OSS Distributions 	    (p) = (tvar))
138*5e3eaea3SApple OSS Distributions 
139*5e3eaea3SApple OSS Distributions #define KPKTQ_EMPTY(q)          ((q)->kq_first == NULL)
140*5e3eaea3SApple OSS Distributions #define KPKTQ_FIRST(q)          ((q)->kq_first)
141*5e3eaea3SApple OSS Distributions #define KPKTQ_NEXT(p)           ((p)->pkt_nextpkt)
142*5e3eaea3SApple OSS Distributions #define KPKTQ_LEN(q)            ((q)->kq_len)
143*5e3eaea3SApple OSS Distributions 
144*5e3eaea3SApple OSS Distributions /*
145*5e3eaea3SApple OSS Distributions  * kq_last is initialized to point to kq_first, so check if they're
146*5e3eaea3SApple OSS Distributions  * equal and return NULL when the list is empty.  Otherwise, we need
147*5e3eaea3SApple OSS Distributions  * to subtract the offset of KPKTQ_NEXT (i.e. pkt_nextpkt field) to get
148*5e3eaea3SApple OSS Distributions  * to the base packet address to return to caller.
149*5e3eaea3SApple OSS Distributions  */
150*5e3eaea3SApple OSS Distributions #define KPKTQ_LAST(head)                                        \
151*5e3eaea3SApple OSS Distributions 	(((head)->kq_last == &KPKTQ_FIRST(head)) ? NULL :       \
152*5e3eaea3SApple OSS Distributions 	((struct __kern_packet *)(void *)((char *)(head)->kq_last -     \
153*5e3eaea3SApple OSS Distributions 	    (size_t)(&KPKTQ_NEXT((struct __kern_packet *)0)))))
154*5e3eaea3SApple OSS Distributions 
155*5e3eaea3SApple OSS Distributions /*
156*5e3eaea3SApple OSS Distributions  * struct pktq serves as basic common batching data structure using KPKTQ.
157*5e3eaea3SApple OSS Distributions  * Elementary types of batch data structure, e.g. packet array, should be named
158*5e3eaea3SApple OSS Distributions  * as pkts.
159*5e3eaea3SApple OSS Distributions  * For instance:
160*5e3eaea3SApple OSS Distributions  * rx_dequeue_pktq(struct pktq *pktq);
161*5e3eaea3SApple OSS Distributions  * rx_dequeue_pkts(struct __kern_packet *pkts[], uint32_t n_pkts);
162*5e3eaea3SApple OSS Distributions  */
163*5e3eaea3SApple OSS Distributions KPKTQ_HEAD(pktq);
164*5e3eaea3SApple OSS Distributions 
165*5e3eaea3SApple OSS Distributions #endif /* BSD_KERNEL_PRIVATE */
166*5e3eaea3SApple OSS Distributions #endif /* !_SKYWALK_PACKET_PACKETQUEUE_H_ */
167