qp.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Linux 2.6.32 and later Kernel module for VMware MVP Hypervisor Support
  3. *
  4. * Copyright (C) 2010-2013 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; see the file COPYING. If not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #line 5
  20. /**
  21. * @file
  22. *
  23. * @brief MVP Queue Pairs function and structure declarations
  24. *
  25. * MVP Queue Pairs:
  26. *
  27. * Queue pairs are intended to be a generic bulk data transport mechanism
  28. * between the guest and host kernels. The queue pair abstraction is based
  29. * on two ring buffers (queues) placed on a shared memory region mapped
  30. * into both guest and host kernel address spaces.
  31. *
  32. * NOTE: Queue pairs are SINGLE-READER, SINGLE-WRITER. Any caller is
  33. * responsible for multi-reader/writer serialization!!!
  34. *
  35. * There are a maximum of QP_MAX_QUEUE_PAIRS in the system, with a maximum
  36. * size of QP_MAX_CAPACITY per pair. Each queue pair is identified by
  37. * an ID.
  38. *
  39. * Each peer follows a producer-consumer model in which one side is the
  40. * producer on one queue, and the other side is the consumer on that queue
  41. * (and vice-versa for its pair).
  42. *
  43. * Data is enqueued and dequeued into the pair in transactional stages,
  44. * meaning each enqueue/dequeue can be followed by zero or more
  45. * enqueue/dequeues, but the enqueue/dequeue is not visible to the peer
  46. * until it has been committed with the *Commit() function.
  47. * In PVTCP, for example, this is used to enqueue a short header, then
  48. * followed by 'segments' of iovecs, then followed by a commit. This
  49. * model prevents a peer from reading the header, expecting a payload,
  50. * but not being able to read the payload because it hasn't been
  51. * enqueued yet.
  52. *
  53. * Queue Pair setup:
  54. *
  55. * Before data can be passed, the guest and host kernel must perform
  56. * the following connection handshake:
  57. *
  58. * 1). A host kernel service registers a listener with the queue pair
  59. * subsystem with a callback to be called when guests create
  60. * and attach to a shared memory region.
  61. *
  62. * 2). Guest initiates an QP_Attach() operation to a shared memory region
  63. * keyed by ID. This step allocates memory, maps it into the host
  64. * address space, and optionally notifies any host services who are
  65. * listening for attach requests from the guest (see previous step).
  66. * Host listeners are provided with a copy of the initialization
  67. * arguments used by the guest (id, size, service type). All registered
  68. * listeners are iterated over until one of them handles the attach
  69. * request and acknowledges with QP_SUCCESS.
  70. *
  71. * 3). The registered host callback is called, notifying the host that
  72. * the guest has attached.
  73. *
  74. * 4). The host can now QP_Attach() to the shared memory region with the same
  75. * arguments as the guest. The queue pair is now well formed and enqueues
  76. * and dequeues can proceed on either side.
  77. *
  78. * Queue Pair teardown:
  79. *
  80. * 1). As before, teardowns are initiated by the guest. Hosts can register
  81. * a callback to be called upon detach. Guests initiate a teardown
  82. * through a call to QP_Detach().
  83. *
  84. * 2). Registered hosts are notified through the aforementioned callback.
  85. * 3). The host service can call QP_Detach() at its own leisure. Memory
  86. * is freed, the queue pair is destroyed.
  87. *
  88. * If at any point the guest unexpectedly shuts down, the host will be
  89. * notified at monitor shutdown time. Memory is freed, and the queue
  90. * pair is destroyed.
  91. *
  92. */
  93. #ifndef _QP_H
  94. #define _QP_H
  95. #define INCLUDE_ALLOW_MODULE
  96. #define INCLUDE_ALLOW_MONITOR
  97. #define INCLUDE_ALLOW_PV
  98. #define INCLUDE_ALLOW_GPL
  99. #include "include_check.h"
  100. /*#define QP_DEBUG 1 */
  101. typedef enum QPState {
  102. QP_STATE_FREE = 0x1, /**< No peers, not memory-backed */
  103. QP_STATE_CONNECTED, /**< Both peers attached , memory backed */
  104. /**< Guest allocated memory, host not yet attached */
  105. QP_STATE_GUEST_ATTACHED,
  106. QP_STATE_MAX /* leave this at the end! */
  107. } QPState;
  108. typedef struct QPId {
  109. uint32 context;
  110. uint32 resource;
  111. } QPId;
  112. /*
  113. * Initialization arguments for each queue pair
  114. */
  115. typedef struct QPInitArgs {
  116. QPId id; /**< Shared memory region ID */
  117. uint32 capacity; /**< Total size of shared region in bytes */
  118. uint32 type; /**< Type of queue pair (PVTCP, other)... */
  119. } QPInitArgs;
  120. /*
  121. * Placed on the shared region, two per region
  122. */
  123. typedef struct QHandle {
  124. volatile uint32 head; /**< queue head offset */
  125. volatile uint32 tail; /**< queue tail offset */
  126. volatile uint32 phantom_head; /**< queue shadow head offset */
  127. volatile uint32 phantom_tail; /**< queue shadow tail offset */
  128. uint8 data[0]; /**< start of data, runs off */
  129. /* the struct */
  130. } QHandle;
  131. /*
  132. * Local to each peer
  133. */
  134. typedef struct QPHandle {
  135. QPId id; /**< shared memory region ID */
  136. uint32 capacity; /**< size of region in bytes */
  137. QHandle *produceQ; /**< producer queue */
  138. QHandle *consumeQ; /**< consumer queue */
  139. uint32 queueSize; /**< size of each queue in bytes */
  140. uint32 type; /**< type of queue pair */
  141. /*
  142. * Following fields unused by guest
  143. */
  144. QPState state;
  145. void (*peerDetachCB)(void *data);/**< detach notification callback */
  146. void *detachData; /**< data for the detach cb */
  147. struct page **pages; /**< page pointers for shared region */
  148. } QPHandle;
  149. /*
  150. * QP Error codes
  151. */
  152. #define QP_SUCCESS 0
  153. #define QP_ERROR_NO_MEM (-1)
  154. #define QP_ERROR_INVALID_HANDLE (-2)
  155. #define QP_ERROR_INVALID_ARGS (-3)
  156. #define QP_ERROR_ALREADY_ATTACHED (-4)
  157. /*
  158. * Hard-coded limits
  159. */
  160. #define QP_MIN_CAPACITY (PAGE_SIZE * 2)
  161. #define QP_MAX_CAPACITY (1024*1024) /* 1M */
  162. #define QP_MAX_QUEUE_PAIRS 32
  163. #define QP_MAX_ID QP_MAX_QUEUE_PAIRS
  164. #define QP_MAX_LISTENERS QP_MAX_QUEUE_PAIRS
  165. #define QP_MAX_PAGES (QP_MAX_CAPACITY/PAGE_SIZE) /* 256 pages */
  166. #define QP_INVALID_ID 0xFFFFFFFF
  167. #define QP_INVALID_SIZE 0xFFFFFFFF
  168. #define QP_INVALID_REGION 0xFFFFFFFF
  169. #define QP_INVALID_TYPE 0xFFFFFFFF
  170. #ifdef __KERNEL__
  171. /**
  172. * @brief Utility function to sanity check arguments
  173. * @param args argument structure to check
  174. * @return true if arguments are sane, false otherwise
  175. */
  176. static inline
  177. _Bool QP_CheckArgs(QPInitArgs *args)
  178. {
  179. if (!args ||
  180. !is_power_of_2(args->capacity) ||
  181. (args->capacity < QP_MIN_CAPACITY) ||
  182. (args->capacity > QP_MAX_CAPACITY) ||
  183. !(args->id.resource < QP_MAX_ID ||
  184. args->id.resource == QP_INVALID_ID) ||
  185. (args->type == QP_INVALID_TYPE))
  186. return false;
  187. else
  188. return true;
  189. }
  190. #endif
  191. /**
  192. * @brief Utility function to sanity check a queue pair handle
  193. * @param qp handle to the queue pair
  194. * @return true if the handle is sane, false otherwise
  195. */
  196. static inline
  197. _Bool QP_CheckHandle(QPHandle *qp)
  198. {
  199. #ifdef MVP_DEBUG
  200. if (!(qp) ||
  201. !(qp->produceQ) ||
  202. !(qp->consumeQ) ||
  203. (qp->state >= (uint32)QP_STATE_MAX) ||
  204. !(qp->queueSize < (QP_MAX_CAPACITY/2)))
  205. return false;
  206. else
  207. return true;
  208. #else
  209. return true;
  210. #endif
  211. }
  212. /**
  213. * @brief Initializes an invalid handle
  214. * @param[in, out] qp handle to the queue pair
  215. */
  216. static inline void
  217. QP_MakeInvalidQPHandle(QPHandle *qp)
  218. {
  219. if (!qp)
  220. return;
  221. qp->id.context = QP_INVALID_ID;
  222. qp->id.resource = QP_INVALID_ID;
  223. qp->capacity = QP_INVALID_SIZE;
  224. qp->produceQ = NULL;
  225. qp->consumeQ = NULL;
  226. qp->queueSize = QP_INVALID_SIZE;
  227. qp->type = QP_INVALID_TYPE;
  228. qp->state = QP_STATE_FREE;
  229. qp->peerDetachCB = NULL;
  230. qp->detachData = NULL;
  231. }
  232. /*
  233. * Host only
  234. */
  235. typedef int32 (*QPListener)(const QPInitArgs *);
  236. int32 QP_RegisterListener(const QPListener);
  237. int32 QP_UnregisterListener(const QPListener);
  238. int32 QP_RegisterDetachCB(QPHandle *qp, void (*callback)(void *), void *data);
  239. /*
  240. * Host and guest specific implementations, see qp_host.c and qp_guest.c
  241. */
  242. int32 QP_Attach(QPInitArgs *args, QPHandle **qp);
  243. int32 QP_Detach(QPHandle *qp);
  244. int32 QP_Notify(QPInitArgs *args);
  245. /*
  246. * Common implementation, see qp_common.c
  247. */
  248. int32 QP_EnqueueSpace(QPHandle *qp);
  249. int32 QP_EnqueueSegment(QPHandle *qp, const void *buf, size_t length, int kern);
  250. int32 QP_EnqueueCommit(QPHandle *qp);
  251. int32 QP_EnqueueReset(QPHandle *qp);
  252. int32 QP_DequeueSpace(QPHandle *qp);
  253. int32 QP_DequeueSegment(QPHandle *qp, void *buf, size_t length, int kern);
  254. int32 QP_DequeueReset(QPHandle *qp);
  255. int32 QP_DequeueCommit(QPHandle *qp);
  256. /*
  257. * HVC methods and signatures
  258. */
  259. #define MVP_QP_SIGNATURE 0x53525051 /**< 'QPRS' */
  260. #define MVP_QP_ATTACH \
  261. (MVP_OBJECT_CUSTOM_BASE + 0) /**< attach to a queue pair */
  262. #define MVP_QP_DETACH \
  263. (MVP_OBJECT_CUSTOM_BASE + 1) /**< detach from a queue pair */
  264. #define MVP_QP_NOTIFY \
  265. (MVP_OBJECT_CUSTOM_BASE + 2) /**< notify host of attach */
  266. #define MVP_QP_LAST \
  267. (MVP_OBJECT_CUSTOM_BASE + 3) /**< Number of methods */
  268. /*
  269. * Debug macros
  270. */
  271. #ifdef QP_DEBUG
  272. #ifdef IN_MONITOR
  273. #define QP_DBG(...) Log(__VA_ARGS__)
  274. #else
  275. #define QP_DBG(...) pr_info(__VA_ARGS__)
  276. #endif
  277. #else
  278. #define QP_DBG(...)
  279. #endif
  280. #endif