ring.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /******************************************************************************
  2. * ring.h
  3. *
  4. * Shared producer-consumer ring macros.
  5. *
  6. * Tim Deegan and Andrew Warfield November 2004.
  7. */
  8. #ifndef __XEN_PUBLIC_IO_RING_H__
  9. #define __XEN_PUBLIC_IO_RING_H__
  10. typedef unsigned int RING_IDX;
  11. /* Round a 32-bit unsigned constant down to the nearest power of two. */
  12. #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
  13. #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
  14. #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
  15. #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
  16. #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
  17. /*
  18. * Calculate size of a shared ring, given the total available space for the
  19. * ring and indexes (_sz), and the name tag of the request/response structure.
  20. * A ring contains as many entries as will fit, rounded down to the nearest
  21. * power of two (so we can mask with (size-1) to loop around).
  22. */
  23. #define __CONST_RING_SIZE(_s, _sz) \
  24. (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
  25. sizeof(((struct _s##_sring *)0)->ring[0])))
  26. /*
  27. * The same for passing in an actual pointer instead of a name tag.
  28. */
  29. #define __RING_SIZE(_s, _sz) \
  30. (__RD32(((_sz) - (long)&(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
  31. /*
  32. * Macros to make the correct C datatypes for a new kind of ring.
  33. *
  34. * To make a new ring datatype, you need to have two message structures,
  35. * let's say struct request, and struct response already defined.
  36. *
  37. * In a header where you want the ring datatype declared, you then do:
  38. *
  39. * DEFINE_RING_TYPES(mytag, struct request, struct response);
  40. *
  41. * These expand out to give you a set of types, as you can see below.
  42. * The most important of these are:
  43. *
  44. * struct mytag_sring - The shared ring.
  45. * struct mytag_front_ring - The 'front' half of the ring.
  46. * struct mytag_back_ring - The 'back' half of the ring.
  47. *
  48. * To initialize a ring in your code you need to know the location and size
  49. * of the shared memory area (PAGE_SIZE, for instance). To initialise
  50. * the front half:
  51. *
  52. * struct mytag_front_ring front_ring;
  53. * SHARED_RING_INIT((struct mytag_sring *)shared_page);
  54. * FRONT_RING_INIT(&front_ring, (struct mytag_sring *)shared_page,
  55. * PAGE_SIZE);
  56. *
  57. * Initializing the back follows similarly (note that only the front
  58. * initializes the shared ring):
  59. *
  60. * struct mytag_back_ring back_ring;
  61. * BACK_RING_INIT(&back_ring, (struct mytag_sring *)shared_page,
  62. * PAGE_SIZE);
  63. */
  64. #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
  65. \
  66. /* Shared ring entry */ \
  67. union __name##_sring_entry { \
  68. __req_t req; \
  69. __rsp_t rsp; \
  70. }; \
  71. \
  72. /* Shared ring page */ \
  73. struct __name##_sring { \
  74. RING_IDX req_prod, req_event; \
  75. RING_IDX rsp_prod, rsp_event; \
  76. uint8_t pad[48]; \
  77. union __name##_sring_entry ring[1]; /* variable-length */ \
  78. }; \
  79. \
  80. /* "Front" end's private variables */ \
  81. struct __name##_front_ring { \
  82. RING_IDX req_prod_pvt; \
  83. RING_IDX rsp_cons; \
  84. unsigned int nr_ents; \
  85. struct __name##_sring *sring; \
  86. }; \
  87. \
  88. /* "Back" end's private variables */ \
  89. struct __name##_back_ring { \
  90. RING_IDX rsp_prod_pvt; \
  91. RING_IDX req_cons; \
  92. unsigned int nr_ents; \
  93. struct __name##_sring *sring; \
  94. };
  95. /*
  96. * Macros for manipulating rings.
  97. *
  98. * FRONT_RING_whatever works on the "front end" of a ring: here
  99. * requests are pushed on to the ring and responses taken off it.
  100. *
  101. * BACK_RING_whatever works on the "back end" of a ring: here
  102. * requests are taken off the ring and responses put on.
  103. *
  104. * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
  105. * This is OK in 1-for-1 request-response situations where the
  106. * requestor (front end) never has more than RING_SIZE()-1
  107. * outstanding requests.
  108. */
  109. /* Initialising empty rings */
  110. #define SHARED_RING_INIT(_s) do { \
  111. (_s)->req_prod = (_s)->rsp_prod = 0; \
  112. (_s)->req_event = (_s)->rsp_event = 1; \
  113. memset((_s)->pad, 0, sizeof((_s)->pad)); \
  114. } while(0)
  115. #define FRONT_RING_INIT(_r, _s, __size) do { \
  116. (_r)->req_prod_pvt = 0; \
  117. (_r)->rsp_cons = 0; \
  118. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  119. (_r)->sring = (_s); \
  120. } while (0)
  121. #define BACK_RING_INIT(_r, _s, __size) do { \
  122. (_r)->rsp_prod_pvt = 0; \
  123. (_r)->req_cons = 0; \
  124. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  125. (_r)->sring = (_s); \
  126. } while (0)
  127. /* Initialize to existing shared indexes -- for recovery */
  128. #define FRONT_RING_ATTACH(_r, _s, __size) do { \
  129. (_r)->sring = (_s); \
  130. (_r)->req_prod_pvt = (_s)->req_prod; \
  131. (_r)->rsp_cons = (_s)->rsp_prod; \
  132. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  133. } while (0)
  134. #define BACK_RING_ATTACH(_r, _s, __size) do { \
  135. (_r)->sring = (_s); \
  136. (_r)->rsp_prod_pvt = (_s)->rsp_prod; \
  137. (_r)->req_cons = (_s)->req_prod; \
  138. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  139. } while (0)
  140. /* How big is this ring? */
  141. #define RING_SIZE(_r) \
  142. ((_r)->nr_ents)
  143. /* Number of free requests (for use on front side only). */
  144. #define RING_FREE_REQUESTS(_r) \
  145. (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
  146. /* Test if there is an empty slot available on the front ring.
  147. * (This is only meaningful from the front. )
  148. */
  149. #define RING_FULL(_r) \
  150. (RING_FREE_REQUESTS(_r) == 0)
  151. /* Test if there are outstanding messages to be processed on a ring. */
  152. #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
  153. ((_r)->sring->rsp_prod - (_r)->rsp_cons)
  154. #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
  155. ({ \
  156. unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
  157. unsigned int rsp = RING_SIZE(_r) - \
  158. ((_r)->req_cons - (_r)->rsp_prod_pvt); \
  159. req < rsp ? req : rsp; \
  160. })
  161. /* Direct access to individual ring elements, by index. */
  162. #define RING_GET_REQUEST(_r, _idx) \
  163. (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
  164. /*
  165. * Get a local copy of a request.
  166. *
  167. * Use this in preference to RING_GET_REQUEST() so all processing is
  168. * done on a local copy that cannot be modified by the other end.
  169. *
  170. * Note that https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58145 may cause this
  171. * to be ineffective where _req is a struct which consists of only bitfields.
  172. */
  173. #define RING_COPY_REQUEST(_r, _idx, _req) do { \
  174. /* Use volatile to force the copy into _req. */ \
  175. *(_req) = *(volatile typeof(_req))RING_GET_REQUEST(_r, _idx); \
  176. } while (0)
  177. #define RING_GET_RESPONSE(_r, _idx) \
  178. (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
  179. /* Loop termination condition: Would the specified index overflow the ring? */
  180. #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
  181. (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
  182. /* Ill-behaved frontend determination: Can there be this many requests? */
  183. #define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \
  184. (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
  185. #define RING_PUSH_REQUESTS(_r) do { \
  186. wmb(); /* back sees requests /before/ updated producer index */ \
  187. (_r)->sring->req_prod = (_r)->req_prod_pvt; \
  188. } while (0)
  189. #define RING_PUSH_RESPONSES(_r) do { \
  190. wmb(); /* front sees responses /before/ updated producer index */ \
  191. (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
  192. } while (0)
  193. /*
  194. * Notification hold-off (req_event and rsp_event):
  195. *
  196. * When queueing requests or responses on a shared ring, it may not always be
  197. * necessary to notify the remote end. For example, if requests are in flight
  198. * in a backend, the front may be able to queue further requests without
  199. * notifying the back (if the back checks for new requests when it queues
  200. * responses).
  201. *
  202. * When enqueuing requests or responses:
  203. *
  204. * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
  205. * is a boolean return value. True indicates that the receiver requires an
  206. * asynchronous notification.
  207. *
  208. * After dequeuing requests or responses (before sleeping the connection):
  209. *
  210. * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
  211. * The second argument is a boolean return value. True indicates that there
  212. * are pending messages on the ring (i.e., the connection should not be put
  213. * to sleep).
  214. *
  215. * These macros will set the req_event/rsp_event field to trigger a
  216. * notification on the very next message that is enqueued. If you want to
  217. * create batches of work (i.e., only receive a notification after several
  218. * messages have been enqueued) then you will need to create a customised
  219. * version of the FINAL_CHECK macro in your own code, which sets the event
  220. * field appropriately.
  221. */
  222. #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
  223. RING_IDX __old = (_r)->sring->req_prod; \
  224. RING_IDX __new = (_r)->req_prod_pvt; \
  225. wmb(); /* back sees requests /before/ updated producer index */ \
  226. (_r)->sring->req_prod = __new; \
  227. mb(); /* back sees new requests /before/ we check req_event */ \
  228. (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
  229. (RING_IDX)(__new - __old)); \
  230. } while (0)
  231. #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
  232. RING_IDX __old = (_r)->sring->rsp_prod; \
  233. RING_IDX __new = (_r)->rsp_prod_pvt; \
  234. wmb(); /* front sees responses /before/ updated producer index */ \
  235. (_r)->sring->rsp_prod = __new; \
  236. mb(); /* front sees new responses /before/ we check rsp_event */ \
  237. (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
  238. (RING_IDX)(__new - __old)); \
  239. } while (0)
  240. #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
  241. (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
  242. if (_work_to_do) break; \
  243. (_r)->sring->req_event = (_r)->req_cons + 1; \
  244. mb(); \
  245. (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
  246. } while (0)
  247. #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
  248. (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
  249. if (_work_to_do) break; \
  250. (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
  251. mb(); \
  252. (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
  253. } while (0)
  254. #endif /* __XEN_PUBLIC_IO_RING_H__ */