You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ring.h 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #ifndef __XEN_PUBLIC_IO_RING_H__
  27. #define __XEN_PUBLIC_IO_RING_H__
  28. FILE_LICENCE ( MIT );
  29. #include "../xen-compat.h"
  30. #if __XEN_INTERFACE_VERSION__ < 0x00030208
  31. #define xen_mb() mb()
  32. #define xen_rmb() rmb()
  33. #define xen_wmb() wmb()
  34. #endif
  35. typedef unsigned int RING_IDX;
  36. /* Round a 32-bit unsigned constant down to the nearest power of two. */
  37. #define __RD2(_x) (((_x) & 0x00000002) ? 0x2 : ((_x) & 0x1))
  38. #define __RD4(_x) (((_x) & 0x0000000c) ? __RD2((_x)>>2)<<2 : __RD2(_x))
  39. #define __RD8(_x) (((_x) & 0x000000f0) ? __RD4((_x)>>4)<<4 : __RD4(_x))
  40. #define __RD16(_x) (((_x) & 0x0000ff00) ? __RD8((_x)>>8)<<8 : __RD8(_x))
  41. #define __RD32(_x) (((_x) & 0xffff0000) ? __RD16((_x)>>16)<<16 : __RD16(_x))
  42. /*
  43. * Calculate size of a shared ring, given the total available space for the
  44. * ring and indexes (_sz), and the name tag of the request/response structure.
  45. * A ring contains as many entries as will fit, rounded down to the nearest
  46. * power of two (so we can mask with (size-1) to loop around).
  47. */
  48. #define __CONST_RING_SIZE(_s, _sz) \
  49. (__RD32(((_sz) - offsetof(struct _s##_sring, ring)) / \
  50. sizeof(((struct _s##_sring *)0)->ring[0])))
  51. /*
  52. * The same for passing in an actual pointer instead of a name tag.
  53. */
  54. #define __RING_SIZE(_s, _sz) \
  55. (__RD32(((_sz) - (long)(_s)->ring + (long)(_s)) / sizeof((_s)->ring[0])))
  56. /*
  57. * Macros to make the correct C datatypes for a new kind of ring.
  58. *
  59. * To make a new ring datatype, you need to have two message structures,
  60. * let's say request_t, and response_t already defined.
  61. *
  62. * In a header where you want the ring datatype declared, you then do:
  63. *
  64. * DEFINE_RING_TYPES(mytag, request_t, response_t);
  65. *
  66. * These expand out to give you a set of types, as you can see below.
  67. * The most important of these are:
  68. *
  69. * mytag_sring_t - The shared ring.
  70. * mytag_front_ring_t - The 'front' half of the ring.
  71. * mytag_back_ring_t - The 'back' half of the ring.
  72. *
  73. * To initialize a ring in your code you need to know the location and size
  74. * of the shared memory area (PAGE_SIZE, for instance). To initialise
  75. * the front half:
  76. *
  77. * mytag_front_ring_t front_ring;
  78. * SHARED_RING_INIT((mytag_sring_t *)shared_page);
  79. * FRONT_RING_INIT(&front_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
  80. *
  81. * Initializing the back follows similarly (note that only the front
  82. * initializes the shared ring):
  83. *
  84. * mytag_back_ring_t back_ring;
  85. * BACK_RING_INIT(&back_ring, (mytag_sring_t *)shared_page, PAGE_SIZE);
  86. */
  87. #define DEFINE_RING_TYPES(__name, __req_t, __rsp_t) \
  88. \
  89. /* Shared ring entry */ \
  90. union __name##_sring_entry { \
  91. __req_t req; \
  92. __rsp_t rsp; \
  93. }; \
  94. \
  95. /* Shared ring page */ \
  96. struct __name##_sring { \
  97. RING_IDX req_prod, req_event; \
  98. RING_IDX rsp_prod, rsp_event; \
  99. union { \
  100. struct { \
  101. uint8_t smartpoll_active; \
  102. } netif; \
  103. struct { \
  104. uint8_t msg; \
  105. } tapif_user; \
  106. uint8_t pvt_pad[4]; \
  107. } private; \
  108. uint8_t __pad[44]; \
  109. union __name##_sring_entry ring[1]; /* variable-length */ \
  110. }; \
  111. \
  112. /* "Front" end's private variables */ \
  113. struct __name##_front_ring { \
  114. RING_IDX req_prod_pvt; \
  115. RING_IDX rsp_cons; \
  116. unsigned int nr_ents; \
  117. struct __name##_sring *sring; \
  118. }; \
  119. \
  120. /* "Back" end's private variables */ \
  121. struct __name##_back_ring { \
  122. RING_IDX rsp_prod_pvt; \
  123. RING_IDX req_cons; \
  124. unsigned int nr_ents; \
  125. struct __name##_sring *sring; \
  126. }; \
  127. \
  128. /* Syntactic sugar */ \
  129. typedef struct __name##_sring __name##_sring_t; \
  130. typedef struct __name##_front_ring __name##_front_ring_t; \
  131. typedef struct __name##_back_ring __name##_back_ring_t
  132. /*
  133. * Macros for manipulating rings.
  134. *
  135. * FRONT_RING_whatever works on the "front end" of a ring: here
  136. * requests are pushed on to the ring and responses taken off it.
  137. *
  138. * BACK_RING_whatever works on the "back end" of a ring: here
  139. * requests are taken off the ring and responses put on.
  140. *
  141. * N.B. these macros do NO INTERLOCKS OR FLOW CONTROL.
  142. * This is OK in 1-for-1 request-response situations where the
  143. * requestor (front end) never has more than RING_SIZE()-1
  144. * outstanding requests.
  145. */
  146. /* Initialising empty rings */
  147. #define SHARED_RING_INIT(_s) do { \
  148. (_s)->req_prod = (_s)->rsp_prod = 0; \
  149. (_s)->req_event = (_s)->rsp_event = 1; \
  150. (void)memset((_s)->private.pvt_pad, 0, sizeof((_s)->private.pvt_pad)); \
  151. (void)memset((_s)->__pad, 0, sizeof((_s)->__pad)); \
  152. } while(0)
  153. #define FRONT_RING_INIT(_r, _s, __size) do { \
  154. (_r)->req_prod_pvt = 0; \
  155. (_r)->rsp_cons = 0; \
  156. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  157. (_r)->sring = (_s); \
  158. } while (0)
  159. #define BACK_RING_INIT(_r, _s, __size) do { \
  160. (_r)->rsp_prod_pvt = 0; \
  161. (_r)->req_cons = 0; \
  162. (_r)->nr_ents = __RING_SIZE(_s, __size); \
  163. (_r)->sring = (_s); \
  164. } while (0)
  165. /* How big is this ring? */
  166. #define RING_SIZE(_r) \
  167. ((_r)->nr_ents)
  168. /* Number of free requests (for use on front side only). */
  169. #define RING_FREE_REQUESTS(_r) \
  170. (RING_SIZE(_r) - ((_r)->req_prod_pvt - (_r)->rsp_cons))
  171. /* Test if there is an empty slot available on the front ring.
  172. * (This is only meaningful from the front. )
  173. */
  174. #define RING_FULL(_r) \
  175. (RING_FREE_REQUESTS(_r) == 0)
  176. /* Test if there are outstanding messages to be processed on a ring. */
  177. #define RING_HAS_UNCONSUMED_RESPONSES(_r) \
  178. ((_r)->sring->rsp_prod - (_r)->rsp_cons)
  179. #ifdef __GNUC__
  180. #define RING_HAS_UNCONSUMED_REQUESTS(_r) ({ \
  181. unsigned int req = (_r)->sring->req_prod - (_r)->req_cons; \
  182. unsigned int rsp = RING_SIZE(_r) - \
  183. ((_r)->req_cons - (_r)->rsp_prod_pvt); \
  184. req < rsp ? req : rsp; \
  185. })
  186. #else
  187. /* Same as above, but without the nice GCC ({ ... }) syntax. */
  188. #define RING_HAS_UNCONSUMED_REQUESTS(_r) \
  189. ((((_r)->sring->req_prod - (_r)->req_cons) < \
  190. (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt))) ? \
  191. ((_r)->sring->req_prod - (_r)->req_cons) : \
  192. (RING_SIZE(_r) - ((_r)->req_cons - (_r)->rsp_prod_pvt)))
  193. #endif
  194. /* Direct access to individual ring elements, by index. */
  195. #define RING_GET_REQUEST(_r, _idx) \
  196. (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].req))
  197. #define RING_GET_RESPONSE(_r, _idx) \
  198. (&((_r)->sring->ring[((_idx) & (RING_SIZE(_r) - 1))].rsp))
  199. /* Loop termination condition: Would the specified index overflow the ring? */
  200. #define RING_REQUEST_CONS_OVERFLOW(_r, _cons) \
  201. (((_cons) - (_r)->rsp_prod_pvt) >= RING_SIZE(_r))
  202. /* Ill-behaved frontend determination: Can there be this many requests? */
  203. #define RING_REQUEST_PROD_OVERFLOW(_r, _prod) \
  204. (((_prod) - (_r)->rsp_prod_pvt) > RING_SIZE(_r))
  205. #define RING_PUSH_REQUESTS(_r) do { \
  206. xen_wmb(); /* back sees requests /before/ updated producer index */ \
  207. (_r)->sring->req_prod = (_r)->req_prod_pvt; \
  208. } while (0)
  209. #define RING_PUSH_RESPONSES(_r) do { \
  210. xen_wmb(); /* front sees resps /before/ updated producer index */ \
  211. (_r)->sring->rsp_prod = (_r)->rsp_prod_pvt; \
  212. } while (0)
  213. /*
  214. * Notification hold-off (req_event and rsp_event):
  215. *
  216. * When queueing requests or responses on a shared ring, it may not always be
  217. * necessary to notify the remote end. For example, if requests are in flight
  218. * in a backend, the front may be able to queue further requests without
  219. * notifying the back (if the back checks for new requests when it queues
  220. * responses).
  221. *
  222. * When enqueuing requests or responses:
  223. *
  224. * Use RING_PUSH_{REQUESTS,RESPONSES}_AND_CHECK_NOTIFY(). The second argument
  225. * is a boolean return value. True indicates that the receiver requires an
  226. * asynchronous notification.
  227. *
  228. * After dequeuing requests or responses (before sleeping the connection):
  229. *
  230. * Use RING_FINAL_CHECK_FOR_REQUESTS() or RING_FINAL_CHECK_FOR_RESPONSES().
  231. * The second argument is a boolean return value. True indicates that there
  232. * are pending messages on the ring (i.e., the connection should not be put
  233. * to sleep).
  234. *
  235. * These macros will set the req_event/rsp_event field to trigger a
  236. * notification on the very next message that is enqueued. If you want to
  237. * create batches of work (i.e., only receive a notification after several
  238. * messages have been enqueued) then you will need to create a customised
  239. * version of the FINAL_CHECK macro in your own code, which sets the event
  240. * field appropriately.
  241. */
  242. #define RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(_r, _notify) do { \
  243. RING_IDX __old = (_r)->sring->req_prod; \
  244. RING_IDX __new = (_r)->req_prod_pvt; \
  245. xen_wmb(); /* back sees requests /before/ updated producer index */ \
  246. (_r)->sring->req_prod = __new; \
  247. xen_mb(); /* back sees new requests /before/ we check req_event */ \
  248. (_notify) = ((RING_IDX)(__new - (_r)->sring->req_event) < \
  249. (RING_IDX)(__new - __old)); \
  250. } while (0)
  251. #define RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(_r, _notify) do { \
  252. RING_IDX __old = (_r)->sring->rsp_prod; \
  253. RING_IDX __new = (_r)->rsp_prod_pvt; \
  254. xen_wmb(); /* front sees resps /before/ updated producer index */ \
  255. (_r)->sring->rsp_prod = __new; \
  256. xen_mb(); /* front sees new resps /before/ we check rsp_event */ \
  257. (_notify) = ((RING_IDX)(__new - (_r)->sring->rsp_event) < \
  258. (RING_IDX)(__new - __old)); \
  259. } while (0)
  260. #define RING_FINAL_CHECK_FOR_REQUESTS(_r, _work_to_do) do { \
  261. (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
  262. if (_work_to_do) break; \
  263. (_r)->sring->req_event = (_r)->req_cons + 1; \
  264. xen_mb(); \
  265. (_work_to_do) = RING_HAS_UNCONSUMED_REQUESTS(_r); \
  266. } while (0)
  267. #define RING_FINAL_CHECK_FOR_RESPONSES(_r, _work_to_do) do { \
  268. (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
  269. if (_work_to_do) break; \
  270. (_r)->sring->rsp_event = (_r)->rsp_cons + 1; \
  271. xen_mb(); \
  272. (_work_to_do) = RING_HAS_UNCONSUMED_RESPONSES(_r); \
  273. } while (0)
  274. #endif /* __XEN_PUBLIC_IO_RING_H__ */
  275. /*
  276. * Local variables:
  277. * mode: C
  278. * c-file-style: "BSD"
  279. * c-basic-offset: 4
  280. * tab-width: 4
  281. * indent-tabs-mode: nil
  282. * End:
  283. */