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