ring.h 25 KB

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