xref: /xnu-8796.101.5/bsd/skywalk/packet/packet_queue.h (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1 /*
2  * Copyright (c) 2016 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 _SKYWALK_PACKET_PACKETQUEUE_H_
30 #define _SKYWALK_PACKET_PACKETQUEUE_H_
31 
32 #ifdef BSD_KERNEL_PRIVATE
33 
34 /*
35  * Simple __kern_packet queueing system
36  * This is basically a SIMPLEQ adapted to skywalk __kern_packet use.
37  */
38 #define KPKTQ_HEAD(name)                                        \
39 struct name {                                                   \
40 	struct __kern_packet *kq_first; /* first packet */              \
41 	struct __kern_packet **kq_last; /* addr of last next packet */  \
42 	uint32_t kq_len; /* number of packets in queue */       \
43 }
44 
45 #define KPKTQ_INIT(q)           do {                            \
46 	KPKTQ_FIRST(q) = NULL;                                  \
47 	(q)->kq_last = &KPKTQ_FIRST(q);                         \
48 	(q)->kq_len = 0;                                        \
49 } while (0)
50 
51 #define KPKTQ_FINI(q)           do {                            \
52 	ASSERT(KPKTQ_EMPTY(q));                                 \
53 	ASSERT(KPKTQ_LEN(q) == 0);                              \
54 	KPKTQ_INIT(q);                                          \
55 } while (0)
56 
57 #define KPKTQ_DISPOSE(q)        KPKTQ_INIT(q)
58 
59 #define KPKTQ_CONCAT(q1, q2)    do {                            \
60 	if (!KPKTQ_EMPTY(q2)) {                                 \
61 	        *(q1)->kq_last = KPKTQ_FIRST(q2);               \
62 	        (q1)->kq_last = (q2)->kq_last;                  \
63 	        (q1)->kq_len += (q2)->kq_len;                   \
64 	        KPKTQ_DISPOSE((q2));                            \
65 	}                                                       \
66 } while (0)
67 
68 #define KPKTQ_PREPEND(q, p)     do {                            \
69 	if ((KPKTQ_NEXT(p) = KPKTQ_FIRST(q)) == NULL) {         \
70 	        ASSERT((q)->kq_len == 0);                       \
71 	        (q)->kq_last = &KPKTQ_NEXT(p);                 \
72 	}                                                       \
73 	KPKTQ_FIRST(q) = (p);                                   \
74 	(q)->kq_len++;                                          \
75 } while (0)
76 
77 #define KPKTQ_ENQUEUE(q, p)     do {                            \
78 	ASSERT(KPKTQ_NEXT(p) == NULL);                          \
79 	*(q)->kq_last = (p);                                    \
80 	(q)->kq_last = &KPKTQ_NEXT(p);                          \
81 	(q)->kq_len++;                                          \
82 } while (0)
83 
84 #define KPKTQ_ENQUEUE_MULTI(q, p, n, c)    do {                 \
85 	KPKTQ_NEXT(n) = NULL;                                   \
86 	*(q)->kq_last = (p);                                    \
87 	(q)->kq_last = &KPKTQ_NEXT(n);                          \
88 	(q)->kq_len += c;                                       \
89 } while (0)
90 
91 #define KPKTQ_ENQUEUE_LIST(q, p)           do {                 \
92 	uint32_t _c = 1;                                        \
93 	struct __kern_packet *_n = (p);                         \
94 	while (__improbable(KPKTQ_NEXT(_n) != NULL)) {          \
95 	        _c++;                                           \
96 	        _n = KPKTQ_NEXT(_n);                            \
97 	}                                                       \
98 	KPKTQ_ENQUEUE_MULTI(q, p, _n, _c);                      \
99 } while (0)
100 
101 #define KPKTQ_DEQUEUE(q, p)     do {                            \
102 	if (((p) = KPKTQ_FIRST(q)) != NULL) {                   \
103 	        (q)->kq_len--;                                  \
104 	        if ((KPKTQ_FIRST(q) = KPKTQ_NEXT(p)) == NULL) { \
105 	                ASSERT((q)->kq_len == 0);               \
106 	                (q)->kq_last = &KPKTQ_FIRST(q);         \
107 	        } else {                                        \
108 	                KPKTQ_NEXT(p) = NULL;                   \
109 	        }                                               \
110 	}                                                       \
111 } while (0)
112 
113 #define KPKTQ_REMOVE(q, p)      do {                            \
114 	if (KPKTQ_FIRST(q) == (p)) {                            \
115 	        KPKTQ_DEQUEUE(q, p);                            \
116 	} else {                                                \
117 	        struct __kern_packet *_p = KPKTQ_FIRST(q);      \
118 	        while (KPKTQ_NEXT(_p) != (p))                   \
119 	                _p = KPKTQ_NEXT(_p);                    \
120 	        if ((KPKTQ_NEXT(_p) =                           \
121 	            KPKTQ_NEXT(KPKTQ_NEXT(_p))) == NULL) {      \
122 	                (q)->kq_last = &KPKTQ_NEXT(_p);         \
123 	        }                                               \
124 	        (q)->kq_len--;                                  \
125 	        KPKTQ_NEXT(p) = NULL;                           \
126 	}                                                       \
127 } while (0)
128 
129 #define KPKTQ_FOREACH(p, q)                                     \
130 	for ((p) = KPKTQ_FIRST(q);                              \
131 	    (p);                                                \
132 	    (p) = KPKTQ_NEXT(p))
133 
134 #define KPKTQ_FOREACH_SAFE(p, q, tvar)                          \
135 	for ((p) = KPKTQ_FIRST(q);                              \
136 	    (p) && ((tvar) = KPKTQ_NEXT(p), 1);                 \
137 	    (p) = (tvar))
138 
139 #define KPKTQ_EMPTY(q)          ((q)->kq_first == NULL)
140 #define KPKTQ_FIRST(q)          ((q)->kq_first)
141 #define KPKTQ_NEXT(p)           ((p)->pkt_nextpkt)
142 #define KPKTQ_LEN(q)            ((q)->kq_len)
143 
144 /*
145  * kq_last is initialized to point to kq_first, so check if they're
146  * equal and return NULL when the list is empty.  Otherwise, we need
147  * to subtract the offset of KPKTQ_NEXT (i.e. pkt_nextpkt field) to get
148  * to the base packet address to return to caller.
149  */
150 #define KPKTQ_LAST(head)                                        \
151 	(((head)->kq_last == &KPKTQ_FIRST(head)) ? NULL :       \
152 	((struct __kern_packet *)(void *)((char *)(head)->kq_last -     \
153 	    (size_t)(&KPKTQ_NEXT((struct __kern_packet *)0)))))
154 
155 /*
156  * struct pktq serves as basic common batching data structure using KPKTQ.
157  * Elementary types of batch data structure, e.g. packet array, should be named
158  * as pkts.
159  * For instance:
160  * rx_dequeue_pktq(struct pktq *pktq);
161  * rx_dequeue_pkts(struct __kern_packet *pkts[], uint32_t n_pkts);
162  */
163 KPKTQ_HEAD(pktq);
164 
165 #endif /* BSD_KERNEL_PRIVATE */
166 #endif /* !_SKYWALK_PACKET_PACKETQUEUE_H_ */
167