iPXE
ring.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MIT */
2 /******************************************************************************
3  * ring.h
4  *
5  * Shared producer-consumer ring macros.
6  *
7  * Tim Deegan and Andrew Warfield November 2004.
8  */
9 
10 #ifndef __XEN_PUBLIC_IO_RING_H__
11 #define __XEN_PUBLIC_IO_RING_H__
12 
13 FILE_LICENCE ( MIT );
14 
15 /*
16  * When #include'ing this header, you need to provide the following
17  * declaration upfront:
18  * - standard integers types (uint8_t, uint16_t, etc)
19  * They are provided by stdint.h of the standard headers.
20  *
21  * In addition, if you intend to use the FLEX macros, you also need to
22  * provide the following, before invoking the FLEX macros:
23  * - size_t
24  * - memcpy
25  * - grant_ref_t
26  * These declarations are provided by string.h of the standard headers,
27  * and grant_table.h from the Xen public headers.
28  */
29 
30 #include "../xen-compat.h"
31 
32 #if __XEN_INTERFACE_VERSION__ < 0x00030208
33 #define xen_mb() mb()
34 #define xen_rmb() rmb()
35 #define xen_wmb() wmb()
36 #endif
37 
38 typedef unsigned int RING_IDX;
39 
40 /* Round a 32-bit unsigned constant down to the nearest power of two. */
41 #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
42 #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
43 #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
44 #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
45 #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
46 
47 /*
48  * Calculate size of a shared ring, given the total available space for the
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
51  * power of two (so we can mask with (size-1) to loop around).
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  */
59 #define __RING_SIZE(_s, _sz) \
60  (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
61 
62 /*
63  * Macros to make the correct C datatypes for a new kind of ring.
64  *
65  * To make a new ring datatype, you need to have two message structures,
66  * let's say request_t, and response_t already defined.
67  *
68  * In a header where you want the ring datatype declared, you then do:
69  *
70  * DEFINE_RING_TYPES(mytag, request_t, response_t);
71  *
72  * These expand out to give you a set of types, as you can see below.
73  * The most important of these are:
74  *
75  * mytag_sring_t - The shared ring.
76  * mytag_front_ring_t - The 'front' half of the ring.
77  * mytag_back_ring_t - The 'back' half of the ring.
78  *
79  * To initialize a ring in your code you need to know the location and size
80  * of the shared memory area (PAGE_SIZE, for instance). To initialise
81  * the front half:
82  *
83  * mytag_front_ring_t ring;
84  * XEN_FRONT_RING_INIT(&ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
85  *
86  * Initializing the back follows similarly (note that only the front
87  * initializes the shared ring):
88  *
89  * mytag_back_ring_t back_ring;
90  * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
91  */
92 
93 #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
94  \
95 /* Shared ring entry */ \
96 union __name##_sring_entry { \
97  __req_t req; \
98  __rsp_t rsp; \
99 }; \
100  \
101 /* Shared ring page */ \
102 struct __name##_sring { \
103  RING_IDX req_prod, req_event; \
104  RING_IDX rsp_prod, rsp_event; \
105  union { \
106  struct { \
107  uint8_t smartpoll_active; \
108  } netif; \
109  struct { \
110  uint8_t msg; \
111  } tapif_user; \
112  uint8_t pvt_pad[4]; \
113  } pvt; \
114  uint8_t __pad[44]; \
115  union __name##_sring_entry ring[1]; /* variable-length */ \
116 }; \
117  \
118 /* "Front" end's private variables */ \
119 struct __name##_front_ring { \
120  RING_IDX req_prod_pvt; \
121  RING_IDX rsp_cons; \
122  unsigned int nr_ents; \
123  struct __name##_sring *sring; \
124 }; \
125  \
126 /* "Back" end's private variables */ \
127 struct __name##_back_ring { \
128  RING_IDX rsp_prod_pvt; \
129  RING_IDX req_cons; \
130  unsigned int nr_ents; \
131  struct __name##_sring *sring; \
132 }; \
133  \
134 /* Syntactic sugar */ \
135 typedef struct __name##_sring __name##_sring_t; \
136 typedef struct __name##_front_ring __name##_front_ring_t; \
137 typedef struct __name##_back_ring __name##_back_ring_t
138 
139 /*
140  * Macros for manipulating rings.
141  *
142  * FRONT_RING_whatever works on the "front end" of a ring: here
143  * requests are pushed on to the ring and responses taken off it.
144  *
145  * BACK_RING_whatever works on the "back end" of a ring: here
146  * requests are taken off the ring and responses put on.
147  *
148  * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
149  * This is OK in 1-for-1 request-response situations where the
150  * requestor (front end) never has more than RING_SIZE()-1
151  * outstanding requests.
152  */
153 
154 /* Initialising empty rings */
155 #define SHARED_RING_INIT(_s) do { \
156  (_s)->req_prod = (_s)->rsp_prod = 0; \
157  (_s)->req_event = (_s)->rsp_event = 1; \
158  (void)memset((_s)->pvt.pvt_pad, 0, sizeof((_s)->pvt.pvt_pad)); \
159  (void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \
160 } while(0)
161 
162 #define FRONT_RING_ATTACH(_r, _s, _i, __size) do { \
163  (_r)->req_prod_pvt = (_i); \
164  (_r)->rsp_cons = (_i); \
165  (_r)->nr_ents = __RING_SIZE(_s, __size); \
166  (_r)->sring = (_s); \
167 } while (0)
168 
169 #define FRONT_RING_INIT(_r, _s, __size) FRONT_RING_ATTACH(_r, _s, 0, __size)
170 
171 #define XEN_FRONT_RING_INIT(r, s, size) do { \
172  SHARED_RING_INIT(s); \
173  FRONT_RING_INIT(r, s, size); \
174 } while (0)
175 
176 #define BACK_RING_ATTACH(_r, _s, _i, __size) do { \
177  (_r)->rsp_prod_pvt = (_i); \
178  (_r)->req_cons = (_i); \
179  (_r)->nr_ents = __RING_SIZE(_s, __size); \
180  (_r)->sring = (_s); \
181 } while (0)
182 
183 #define BACK_RING_INIT(_r, _s, __size) BACK_RING_ATTACH(_r, _s, 0, __size)
184 
185 /* How big is this ring? */
186 #define RING_SIZE(_r) \
187  ((_r)->nr_ents)
188 
189 /* Number of free requests (for use on front side only). */
190 #define RING_FREE_REQUESTS(_r) \
191  (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
192 
193 /* Test if there is an empty slot available on the front ring.
194  * (This is only meaningful from the front. )
195  */
196 #define RING_FULL(_r) \
197  (RING_FREE_REQUESTS(_r) == 0)
198 
199 /* Test if there are outstanding messages to be processed on a ring. */
200 #define XEN_RING_NR_UNCONSUMED_RESPONSES(_r) \
201  ((_r)->sring->rsp_prod - (_r)->rsp_cons)
202 
203 #ifdef __GNUC__
204 #define XEN_RING_NR_UNCONSUMED_REQUESTS(_r) ({ \
205  unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
206  unsigned int rsp = RING_SIZE(_r) - \
207  ((_r)->req_cons - (_r)->rsp_prod_pvt); \
208  req < rsp ? req : rsp; \
209 })
210 #else
211 /* Same as above, but without the nice GCC ({ ... }) syntax. */
212 #define XEN_RING_NR_UNCONSUMED_REQUESTS(_r) \
213  ((((_r)->sring->req_prod - (_r)->req_cons) < \
214  (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
215  ((_r)->sring->req_prod - (_r)->req_cons) : \
216  (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
217 #endif
218 
219 #ifdef XEN_RING_HAS_UNCONSUMED_IS_BOOL
220 /*
221  * These variants should only be used in case no caller is abusing them for
222  * obtaining the number of unconsumed responses/requests.
223  */
224 #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
225  (!!XEN_RING_NR_UNCONSUMED_RESPONSES(_r))
226 #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
227  (!!XEN_RING_NR_UNCONSUMED_REQUESTS(_r))
228 #else
229 #define RING_HAS_UNCONSUMED_RESPONSES(_r) XEN_RING_NR_UNCONSUMED_RESPONSES(_r)
230 #define RING_HAS_UNCONSUMED_REQUESTS(_r) XEN_RING_NR_UNCONSUMED_REQUESTS(_r)
231 #endif
232 
233 /* Direct access to individual ring elements, by index. */
234 #define RING_GET_REQUEST(_r, _idx) \
235  (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
236 
237 #define RING_GET_RESPONSE(_r, _idx) \
238  (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
239 
240 /*
241  * Get a local copy of a request/response.
242  *
243  * Use this in preference to RING_GET_{REQUEST,RESPONSE}() so all processing is
244  * done on a local copy that cannot be modified by the other end.
245  *
246  * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
247  * to be ineffective where dest is a struct which consists of only bitfields.
248  */
249 #define RING_COPY_(type, r, idx, dest) do { \
250  /* Use volatile to force the copy into dest. */ \
251  *(dest) = *(volatile __typeof__(dest))RING_GET_##type(r, idx); \
252 } while (0)
253 
254 #define RING_COPY_REQUEST(r, idx, req) RING_COPY_(REQUEST, r, idx, req)
255 #define RING_COPY_RESPONSE(r, idx, rsp) RING_COPY_(RESPONSE, r, idx, rsp)
256 
257 /* Loop termination condition: Would the specified index overflow the ring? */
258 #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
259  (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
260 
261 /* Ill-behaved frontend determination: Can there be this many requests? */
262 #define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \
263  (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
264 
265 /* Ill-behaved backend determination: Can there be this many responses? */
266 #define RING_RESPONSE_PROD_OVERFLOW(_r, _prod) \
267  (((_prod) - (_r)->rsp_cons) > RING_SIZE(_r))
268 
269 #define RING_PUSH_REQUESTS(_r) do { \
270  xen_wmb(); /* back sees requests /before/ updated producer index */ \
271  (_r)->sring->req_prod = (_r)->req_prod_pvt; \
272 } while (0)
273 
274 #define RING_PUSH_RESPONSES(_r) do { \
275  xen_wmb(); /* front sees resps /before/ updated producer index */ \
276  (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
277 } while (0)
278 
279 /*
280  * Notification hold-off (req_event and rsp_event):
281  *
282  * When queueing requests or responses on a shared ring, it may not always be
283  * necessary to notify the remote end. For example, if requests are in flight
284  * in a backend, the front may be able to queue further requests without
285  * notifying the back (if the back checks for new requests when it queues
286  * responses).
287  *
288  * When enqueuing requests or responses:
289  *
290  * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
291  * is a boolean return value. True indicates that the receiver requires an
292  * asynchronous notification.
293  *
294  * After dequeuing requests or responses (before sleeping the connection):
295  *
296  * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
297  * The second argument is a boolean return value. True indicates that there
298  * are pending messages on the ring (i.e., the connection should not be put
299  * to sleep).
300  *
301  * These macros will set the req_event/rsp_event field to trigger a
302  * notification on the very next message that is enqueued. If you want to
303  * create batches of work (i.e., only receive a notification after several
304  * messages have been enqueued) then you will need to create a customised
305  * version of the FINAL_CHECK macro in your own code, which sets the event
306  * field appropriately.
307  */
308 
309 #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
310  RING_IDX __old = (_r)->sring->req_prod; \
311  RING_IDX __new = (_r)->req_prod_pvt; \
312  xen_wmb(); /* back sees requests /before/ updated producer index */ \
313  (_r)->sring->req_prod = __new; \
314  xen_mb(); /* back sees new requests /before/ we check req_event */ \
315  (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
316  (RING_IDX)(__new - __old)); \
317 } while (0)
318 
319 #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
320  RING_IDX __old = (_r)->sring->rsp_prod; \
321  RING_IDX __new = (_r)->rsp_prod_pvt; \
322  xen_wmb(); /* front sees resps /before/ updated producer index */ \
323  (_r)->sring->rsp_prod = __new; \
324  xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
325  (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
326  (RING_IDX)(__new - __old)); \
327 } while (0)
328 
329 #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
330  (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
331  if (_work_to_do) break; \
332  (_r)->sring->req_event = (_r)->req_cons + 1; \
333  xen_mb(); \
334  (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
335 } while (0)
336 
337 #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
338  (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
339  if (_work_to_do) break; \
340  (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
341  xen_mb(); \
342  (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
343 } while (0)
344 
345 
346 /*
347  * DEFINE_XEN_FLEX_RING_AND_INTF defines two monodirectional rings and
348  * functions to check if there is data on the ring, and to read and
349  * write to them.
350  *
351  * DEFINE_XEN_FLEX_RING is similar to DEFINE_XEN_FLEX_RING_AND_INTF, but
352  * does not define the indexes page. As different protocols can have
353  * extensions to the basic format, this macro allow them to define their
354  * own struct.
355  *
356  * XEN_FLEX_RING_SIZE
357  * Convenience macro to calculate the size of one of the two rings
358  * from the overall order.
359  *
360  * $NAME_mask
361  * Function to apply the size mask to an index, to reduce the index
362  * within the range [0-size].
363  *
364  * $NAME_read_packet
365  * Function to read data from the ring. The amount of data to read is
366  * specified by the "size" argument.
367  *
368  * $NAME_write_packet
369  * Function to write data to the ring. The amount of data to write is
370  * specified by the "size" argument.
371  *
372  * $NAME_get_ring_ptr
373  * Convenience function that returns a pointer to read/write to the
374  * ring at the right location.
375  *
376  * $NAME_data_intf
377  * Indexes page, shared between frontend and backend. It also
378  * contains the array of grant refs.
379  *
380  * $NAME_queued
381  * Function to calculate how many bytes are currently on the ring,
382  * ready to be read. It can also be used to calculate how much free
383  * space is currently on the ring (XEN_FLEX_RING_SIZE() -
384  * $NAME_queued()).
385  */
386 
387 #ifndef XEN_PAGE_SHIFT
388 /* The PAGE_SIZE for ring protocols and hypercall interfaces is always
389  * 4K, regardless of the architecture, and page granularity chosen by
390  * operating systems.
391  */
392 #define XEN_PAGE_SHIFT 12
393 #endif
394 #define XEN_FLEX_RING_SIZE(order) \
395  (1UL << ((order) + XEN_PAGE_SHIFT - 1))
396 
397 #define DEFINE_XEN_FLEX_RING(name) \
398 static inline RING_IDX name##_mask(RING_IDX idx, RING_IDX ring_size) \
399 { \
400  return idx & (ring_size - 1); \
401 } \
402  \
403 static inline unsigned char *name##_get_ring_ptr(unsigned char *buf, \
404  RING_IDX idx, \
405  RING_IDX ring_size) \
406 { \
407  return buf + name##_mask(idx, ring_size); \
408 } \
409  \
410 static inline void name##_read_packet(void *opaque, \
411  const unsigned char *buf, \
412  size_t size, \
413  RING_IDX masked_prod, \
414  RING_IDX *masked_cons, \
415  RING_IDX ring_size) \
416 { \
417  if (*masked_cons < masked_prod || \
418  size <= ring_size - *masked_cons) { \
419  memcpy(opaque, buf + *masked_cons, size); \
420  } else { \
421  memcpy(opaque, buf + *masked_cons, ring_size - *masked_cons); \
422  memcpy((unsigned char *)opaque + ring_size - *masked_cons, buf, \
423  size - (ring_size - *masked_cons)); \
424  } \
425  *masked_cons = name##_mask(*masked_cons + size, ring_size); \
426 } \
427  \
428 static inline void name##_write_packet(unsigned char *buf, \
429  const void *opaque, \
430  size_t size, \
431  RING_IDX *masked_prod, \
432  RING_IDX masked_cons, \
433  RING_IDX ring_size) \
434 { \
435  if (*masked_prod < masked_cons || \
436  size <= ring_size - *masked_prod) { \
437  memcpy(buf + *masked_prod, opaque, size); \
438  } else { \
439  memcpy(buf + *masked_prod, opaque, ring_size - *masked_prod); \
440  memcpy(buf, (unsigned char *)opaque + (ring_size - *masked_prod), \
441  size - (ring_size - *masked_prod)); \
442  } \
443  *masked_prod = name##_mask(*masked_prod + size, ring_size); \
444 } \
445  \
446 static inline RING_IDX name##_queued(RING_IDX prod, \
447  RING_IDX cons, \
448  RING_IDX ring_size) \
449 { \
450  RING_IDX size; \
451  \
452  if (prod == cons) \
453  return 0; \
454  \
455  prod = name##_mask(prod, ring_size); \
456  cons = name##_mask(cons, ring_size); \
457  \
458  if (prod == cons) \
459  return ring_size; \
460  \
461  if (prod > cons) \
462  size = prod - cons; \
463  else \
464  size = ring_size - (cons - prod); \
465  return size; \
466 } \
467  \
468 struct name##_data { \
469  unsigned char *in; /* half of the allocation */ \
470  unsigned char *out; /* half of the allocation */ \
471 }
472 
473 #define DEFINE_XEN_FLEX_RING_AND_INTF(name) \
474 struct name##_data_intf { \
475  RING_IDX in_cons, in_prod; \
476  \
477  uint8_t pad1[56]; \
478  \
479  RING_IDX out_cons, out_prod; \
480  \
481  uint8_t pad2[56]; \
482  \
483  RING_IDX ring_order; \
484  grant_ref_t ref[]; \
485 }; \
486 DEFINE_XEN_FLEX_RING(name)
487 
488 #endif /* __XEN_PUBLIC_IO_RING_H__ */
489 
490 /*
491  * Local variables:
492  * mode: C
493  * c-file-style: "BSD"
494  * c-basic-offset: 4
495  * tab-width: 4
496  * indent-tabs-mode: nil
497  * End:
498  */
unsigned int RING_IDX
Definition: ring.h:38
FILE_LICENCE(MIT)