Print this page
3373 gcc >= 4.5 concerns about offsetof()
Portions contributed by: Igor Pashev <pashev.igor@gmail.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/xen/public/io/ring.h
+++ new/usr/src/uts/common/xen/public/io/ring.h
1 1 /******************************************************************************
2 2 * ring.h
3 3 *
4 4 * Shared producer-consumer ring macros.
5 5 *
6 6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 7 * of this software and associated documentation files (the "Software"), to
8 8 * deal in the Software without restriction, including without limitation the
9 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 10 * sell copies of the Software, and to permit persons to whom the Software is
11 11 * furnished to do so, subject to the following conditions:
12 12 *
13 13 * The above copyright notice and this permission notice shall be included in
14 14 * all copies or substantial portions of the Software.
15 15 *
16 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 22 * DEALINGS IN THE SOFTWARE.
23 23 *
24 24 * Tim Deegan and Andrew Warfield November 2004.
25 25 */
26 26
27 27 #ifndef __XEN_PUBLIC_IO_RING_H__
28 28 #define __XEN_PUBLIC_IO_RING_H__
29 29
30 30 #include "../xen-compat.h"
31 31
32 32 #if __XEN_INTERFACE_VERSION__ < 0x00030208
33 33 #define xen_mb() mb()
34 34 #define xen_rmb() rmb()
35 35 #define xen_wmb() wmb()
36 36 #endif
37 37
38 38 typedef unsigned int RING_IDX;
39 39
↓ open down ↓ |
39 lines elided |
↑ open up ↑ |
40 40 /* Round a 32-bit unsigned constant down to the nearest power of two. */
41 41 #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
42 42 #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
43 43 #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
44 44 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
45 45 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
46 46
47 47 /*
48 48 * Calculate size of a shared ring, given the total available space for the
49 49 * ring and indexes (_sz), and the name tag of the request/response structure.
50 - * A ring contains as many entries as will fit, rounded down to the nearest
50 + * A ring contains as many entries as will fit, rounded down to the nearest
51 51 * power of two (so we can mask with (size-1) to loop around).
52 52 */
53 +#define __CONST_RING_SIZE(_s, _sz) \
54 + (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
55 + sizeof(((struct _s##_sring *)0)->ring[0])))
56 +/*
57 + * The same for passing in an actual pointer instead of a name tag.
58 + */
53 59 #define __RING_SIZE(_s, _sz) \
54 60 (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
55 61
56 62 /*
57 63 * Macros to make the correct C datatypes for a new kind of ring.
58 64 *
59 65 * To make a new ring datatype, you need to have two message structures,
60 66 * let's say request_t, and response_t already defined.
61 67 *
62 68 * In a header where you want the ring datatype declared, you then do:
63 69 *
64 70 * DEFINE_RING_TYPES(mytag, request_t, response_t);
65 71 *
66 72 * These expand out to give you a set of types, as you can see below.
67 73 * The most important of these are:
68 74 *
69 75 * mytag_sring_t - The shared ring.
70 76 * mytag_front_ring_t - The 'front' half of the ring.
71 77 * mytag_back_ring_t - The 'back' half of the ring.
72 78 *
73 79 * To initialize a ring in your code you need to know the location and size
74 80 * of the shared memory area (PAGE_SIZE, for instance). To initialise
75 81 * the front half:
76 82 *
77 83 * mytag_front_ring_t front_ring;
78 84 * SHARED_RING_INIT((mytag_sring_t *)shared_page);
79 85 * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
80 86 *
81 87 * Initializing the back follows similarly (note that only the front
82 88 * initializes the shared ring):
83 89 *
84 90 * mytag_back_ring_t back_ring;
85 91 * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
86 92 */
87 93
88 94 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
89 95 \
90 96 /* Shared ring entry */ \
91 97 union __name##_sring_entry { \
92 98 __req_t req; \
93 99 __rsp_t rsp; \
94 100 }; \
95 101 \
96 102 /* Shared ring page */ \
97 103 struct __name##_sring { \
98 104 RING_IDX req_prod, req_event; \
99 105 RING_IDX rsp_prod, rsp_event; \
100 106 uint8_t pad[48]; \
101 107 union __name##_sring_entry ring[1]; /* variable-length */ \
102 108 }; \
103 109 \
104 110 /* "Front" end's private variables */ \
105 111 struct __name##_front_ring { \
106 112 RING_IDX req_prod_pvt; \
107 113 RING_IDX rsp_cons; \
108 114 unsigned int nr_ents; \
109 115 struct __name##_sring *sring; \
110 116 }; \
111 117 \
112 118 /* "Back" end's private variables */ \
113 119 struct __name##_back_ring { \
114 120 RING_IDX rsp_prod_pvt; \
115 121 RING_IDX req_cons; \
116 122 unsigned int nr_ents; \
117 123 struct __name##_sring *sring; \
118 124 }; \
119 125 \
120 126 /* Syntactic sugar */ \
121 127 typedef struct __name##_sring __name##_sring_t; \
122 128 typedef struct __name##_front_ring __name##_front_ring_t; \
123 129 typedef struct __name##_back_ring __name##_back_ring_t
124 130
125 131 /*
126 132 * Macros for manipulating rings.
127 133 *
128 134 * FRONT_RING_whatever works on the "front end" of a ring: here
129 135 * requests are pushed on to the ring and responses taken off it.
130 136 *
131 137 * BACK_RING_whatever works on the "back end" of a ring: here
132 138 * requests are taken off the ring and responses put on.
133 139 *
134 140 * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
135 141 * This is OK in 1-for-1 request-response situations where the
136 142 * requestor (front end) never has more than RING_SIZE()-1
137 143 * outstanding requests.
138 144 */
139 145
140 146 /* Initialising empty rings */
141 147 #define SHARED_RING_INIT(_s) do { \
142 148 (_s)->req_prod = (_s)->rsp_prod = 0; \
143 149 (_s)->req_event = (_s)->rsp_event = 1; \
144 150 (void)memset((_s)->pad, 0, sizeof((_s)->pad)); \
145 151 } while(0)
146 152
147 153 #define FRONT_RING_INIT(_r, _s, __size) do { \
148 154 (_r)->req_prod_pvt = 0; \
149 155 (_r)->rsp_cons = 0; \
150 156 (_r)->nr_ents = __RING_SIZE(_s, __size); \
151 157 (_r)->sring = (_s); \
152 158 } while (0)
153 159
154 160 #define BACK_RING_INIT(_r, _s, __size) do { \
155 161 (_r)->rsp_prod_pvt = 0; \
156 162 (_r)->req_cons = 0; \
157 163 (_r)->nr_ents = __RING_SIZE(_s, __size); \
158 164 (_r)->sring = (_s); \
159 165 } while (0)
160 166
161 167 /* Initialize to existing shared indexes -- for recovery */
162 168 #define FRONT_RING_ATTACH(_r, _s, __size) do { \
163 169 (_r)->sring = (_s); \
164 170 (_r)->req_prod_pvt = (_s)->req_prod; \
165 171 (_r)->rsp_cons = (_s)->rsp_prod; \
166 172 (_r)->nr_ents = __RING_SIZE(_s, __size); \
167 173 } while (0)
168 174
169 175 #define BACK_RING_ATTACH(_r, _s, __size) do { \
170 176 (_r)->sring = (_s); \
171 177 (_r)->rsp_prod_pvt = (_s)->rsp_prod; \
172 178 (_r)->req_cons = (_s)->req_prod; \
173 179 (_r)->nr_ents = __RING_SIZE(_s, __size); \
174 180 } while (0)
175 181
176 182 /* How big is this ring? */
177 183 #define RING_SIZE(_r) \
178 184 ((_r)->nr_ents)
179 185
180 186 /* Number of free requests (for use on front side only). */
181 187 #define RING_FREE_REQUESTS(_r) \
182 188 (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
183 189
184 190 /* Test if there is an empty slot available on the front ring.
185 191 * (This is only meaningful from the front. )
186 192 */
187 193 #define RING_FULL(_r) \
188 194 (RING_FREE_REQUESTS(_r) == 0)
189 195
190 196 /* Test if there are outstanding messages to be processed on a ring. */
191 197 #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
192 198 ((_r)->sring->rsp_prod - (_r)->rsp_cons)
193 199
194 200 #ifdef __GNUC__
195 201 #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \
196 202 unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
197 203 unsigned int rsp = RING_SIZE(_r) - \
198 204 ((_r)->req_cons - (_r)->rsp_prod_pvt); \
199 205 req < rsp ? req : rsp; \
200 206 })
201 207 #else
202 208 /* Same as above, but without the nice GCC ({ ... }) syntax. */
203 209 #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
204 210 ((((_r)->sring->req_prod - (_r)->req_cons) < \
205 211 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
206 212 ((_r)->sring->req_prod - (_r)->req_cons) : \
207 213 (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
208 214 #endif
209 215
210 216 /* Direct access to individual ring elements, by index. */
211 217 #define RING_GET_REQUEST(_r, _idx) \
212 218 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
213 219
214 220 #define RING_GET_RESPONSE(_r, _idx) \
215 221 (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
216 222
217 223 /* Loop termination condition: Would the specified index overflow the ring? */
218 224 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
219 225 (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
220 226
221 227 #define RING_PUSH_REQUESTS(_r) do { \
222 228 xen_wmb(); /* back sees requests /before/ updated producer index */ \
223 229 (_r)->sring->req_prod = (_r)->req_prod_pvt; \
224 230 } while (0)
225 231
226 232 #define RING_PUSH_RESPONSES(_r) do { \
227 233 xen_wmb(); /* front sees resps /before/ updated producer index */ \
228 234 (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
229 235 } while (0)
230 236
231 237 /*
232 238 * Notification hold-off (req_event and rsp_event):
233 239 *
234 240 * When queueing requests or responses on a shared ring, it may not always be
235 241 * necessary to notify the remote end. For example, if requests are in flight
236 242 * in a backend, the front may be able to queue further requests without
237 243 * notifying the back (if the back checks for new requests when it queues
238 244 * responses).
239 245 *
240 246 * When enqueuing requests or responses:
241 247 *
242 248 * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
243 249 * is a boolean return value. True indicates that the receiver requires an
244 250 * asynchronous notification.
245 251 *
246 252 * After dequeuing requests or responses (before sleeping the connection):
247 253 *
248 254 * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
249 255 * The second argument is a boolean return value. True indicates that there
250 256 * are pending messages on the ring (i.e., the connection should not be put
251 257 * to sleep).
252 258 *
253 259 * These macros will set the req_event/rsp_event field to trigger a
254 260 * notification on the very next message that is enqueued. If you want to
255 261 * create batches of work (i.e., only receive a notification after several
256 262 * messages have been enqueued) then you will need to create a customised
257 263 * version of the FINAL_CHECK macro in your own code, which sets the event
258 264 * field appropriately.
259 265 */
260 266
261 267 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
262 268 RING_IDX __old = (_r)->sring->req_prod; \
263 269 RING_IDX __new = (_r)->req_prod_pvt; \
264 270 xen_wmb(); /* back sees requests /before/ updated producer index */ \
265 271 (_r)->sring->req_prod = __new; \
266 272 xen_mb(); /* back sees new requests /before/ we check req_event */ \
267 273 (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
268 274 (RING_IDX)(__new - __old)); \
269 275 } while (0)
270 276
271 277 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
272 278 RING_IDX __old = (_r)->sring->rsp_prod; \
273 279 RING_IDX __new = (_r)->rsp_prod_pvt; \
274 280 xen_wmb(); /* front sees resps /before/ updated producer index */ \
275 281 (_r)->sring->rsp_prod = __new; \
276 282 xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
277 283 (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
278 284 (RING_IDX)(__new - __old)); \
279 285 } while (0)
280 286
281 287 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
282 288 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
283 289 if (_work_to_do) break; \
284 290 (_r)->sring->req_event = (_r)->req_cons + 1; \
285 291 xen_mb(); \
286 292 (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
287 293 } while (0)
288 294
289 295 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
290 296 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
291 297 if (_work_to_do) break; \
292 298 (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
293 299 xen_mb(); \
294 300 (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
295 301 } while (0)
296 302
297 303 #endif /* __XEN_PUBLIC_IO_RING_H__ */
298 304
299 305 /*
300 306 * Local variables:
301 307 * mode: C
302 308 * c-set-style: "BSD"
303 309 * c-basic-offset: 4
304 310 * tab-width: 4
305 311 * indent-tabs-mode: nil
306 312 * End:
307 313 */
↓ open down ↓ |
245 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX