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