rdma_verbs.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2010-2014 Intel Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #if !defined(RDMA_VERBS_H)
  33. #define RDMA_VERBS_H
  34. #include <assert.h>
  35. #include <infiniband/verbs.h>
  36. #include <rdma/rdma_cma.h>
  37. #include <errno.h>
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. static inline int rdma_seterrno(int ret)
  42. {
  43. if (ret) {
  44. errno = ret;
  45. ret = -1;
  46. }
  47. return ret;
  48. }
  49. /*
  50. * Shared receive queues.
  51. */
  52. int rdma_create_srq(struct rdma_cm_id *id, struct ibv_pd *pd,
  53. struct ibv_srq_init_attr *attr);
  54. int rdma_create_srq_ex(struct rdma_cm_id *id, struct ibv_srq_init_attr_ex *attr);
  55. void rdma_destroy_srq(struct rdma_cm_id *id);
  56. /*
  57. * Memory registration helpers.
  58. */
  59. static inline struct ibv_mr *
  60. rdma_reg_msgs(struct rdma_cm_id *id, void *addr, size_t length)
  61. {
  62. return ibv_reg_mr(id->pd, addr, length, IBV_ACCESS_LOCAL_WRITE);
  63. }
  64. static inline struct ibv_mr *
  65. rdma_reg_read(struct rdma_cm_id *id, void *addr, size_t length)
  66. {
  67. return ibv_reg_mr(id->pd, addr, length, IBV_ACCESS_LOCAL_WRITE |
  68. IBV_ACCESS_REMOTE_READ);
  69. }
  70. static inline struct ibv_mr *
  71. rdma_reg_write(struct rdma_cm_id *id, void *addr, size_t length)
  72. {
  73. return ibv_reg_mr(id->pd, addr, length, IBV_ACCESS_LOCAL_WRITE |
  74. IBV_ACCESS_REMOTE_WRITE);
  75. }
  76. static inline int
  77. rdma_dereg_mr(struct ibv_mr *mr)
  78. {
  79. return rdma_seterrno(ibv_dereg_mr(mr));
  80. }
  81. /*
  82. * Vectored send, receive, and RDMA operations.
  83. * Support multiple scatter-gather entries.
  84. */
  85. static inline int
  86. rdma_post_recvv(struct rdma_cm_id *id, void *context, struct ibv_sge *sgl,
  87. int nsge)
  88. {
  89. struct ibv_recv_wr wr, *bad;
  90. wr.wr_id = (uintptr_t) context;
  91. wr.next = NULL;
  92. wr.sg_list = sgl;
  93. wr.num_sge = nsge;
  94. if (id->srq)
  95. return rdma_seterrno(ibv_post_srq_recv(id->srq, &wr, &bad));
  96. else
  97. return rdma_seterrno(ibv_post_recv(id->qp, &wr, &bad));
  98. }
  99. static inline int
  100. rdma_post_sendv(struct rdma_cm_id *id, void *context, struct ibv_sge *sgl,
  101. int nsge, int flags)
  102. {
  103. struct ibv_send_wr wr, *bad;
  104. wr.wr_id = (uintptr_t) context;
  105. wr.next = NULL;
  106. wr.sg_list = sgl;
  107. wr.num_sge = nsge;
  108. wr.opcode = IBV_WR_SEND;
  109. wr.send_flags = flags;
  110. return rdma_seterrno(ibv_post_send(id->qp, &wr, &bad));
  111. }
  112. static inline int
  113. rdma_post_readv(struct rdma_cm_id *id, void *context, struct ibv_sge *sgl,
  114. int nsge, int flags, uint64_t remote_addr, uint32_t rkey)
  115. {
  116. struct ibv_send_wr wr, *bad;
  117. wr.wr_id = (uintptr_t) context;
  118. wr.next = NULL;
  119. wr.sg_list = sgl;
  120. wr.num_sge = nsge;
  121. wr.opcode = IBV_WR_RDMA_READ;
  122. wr.send_flags = flags;
  123. wr.wr.rdma.remote_addr = remote_addr;
  124. wr.wr.rdma.rkey = rkey;
  125. return rdma_seterrno(ibv_post_send(id->qp, &wr, &bad));
  126. }
  127. static inline int
  128. rdma_post_writev(struct rdma_cm_id *id, void *context, struct ibv_sge *sgl,
  129. int nsge, int flags, uint64_t remote_addr, uint32_t rkey)
  130. {
  131. struct ibv_send_wr wr, *bad;
  132. wr.wr_id = (uintptr_t) context;
  133. wr.next = NULL;
  134. wr.sg_list = sgl;
  135. wr.num_sge = nsge;
  136. wr.opcode = IBV_WR_RDMA_WRITE;
  137. wr.send_flags = flags;
  138. wr.wr.rdma.remote_addr = remote_addr;
  139. wr.wr.rdma.rkey = rkey;
  140. return rdma_seterrno(ibv_post_send(id->qp, &wr, &bad));
  141. }
  142. /*
  143. * Simple send, receive, and RDMA calls.
  144. */
  145. static inline int
  146. rdma_post_recv(struct rdma_cm_id *id, void *context, void *addr,
  147. size_t length, struct ibv_mr *mr)
  148. {
  149. struct ibv_sge sge;
  150. assert((addr >= mr->addr) &&
  151. (((uint8_t *) addr + length) <= ((uint8_t *) mr->addr + mr->length)));
  152. sge.addr = (uint64_t) (uintptr_t) addr;
  153. sge.length = (uint32_t) length;
  154. sge.lkey = mr->lkey;
  155. return rdma_post_recvv(id, context, &sge, 1);
  156. }
  157. static inline int
  158. rdma_post_send(struct rdma_cm_id *id, void *context, void *addr,
  159. size_t length, struct ibv_mr *mr, int flags)
  160. {
  161. struct ibv_sge sge;
  162. sge.addr = (uint64_t) (uintptr_t) addr;
  163. sge.length = (uint32_t) length;
  164. sge.lkey = mr ? mr->lkey : 0;
  165. return rdma_post_sendv(id, context, &sge, 1, flags);
  166. }
  167. static inline int
  168. rdma_post_read(struct rdma_cm_id *id, void *context, void *addr,
  169. size_t length, struct ibv_mr *mr, int flags,
  170. uint64_t remote_addr, uint32_t rkey)
  171. {
  172. struct ibv_sge sge;
  173. sge.addr = (uint64_t) (uintptr_t) addr;
  174. sge.length = (uint32_t) length;
  175. sge.lkey = mr->lkey;
  176. return rdma_post_readv(id, context, &sge, 1, flags, remote_addr, rkey);
  177. }
  178. static inline int
  179. rdma_post_write(struct rdma_cm_id *id, void *context, void *addr,
  180. size_t length, struct ibv_mr *mr, int flags,
  181. uint64_t remote_addr, uint32_t rkey)
  182. {
  183. struct ibv_sge sge;
  184. sge.addr = (uint64_t) (uintptr_t) addr;
  185. sge.length = (uint32_t) length;
  186. sge.lkey = mr ? mr->lkey : 0;
  187. return rdma_post_writev(id, context, &sge, 1, flags, remote_addr, rkey);
  188. }
  189. static inline int
  190. rdma_post_ud_send(struct rdma_cm_id *id, void *context, void *addr,
  191. size_t length, struct ibv_mr *mr, int flags,
  192. struct ibv_ah *ah, uint32_t remote_qpn)
  193. {
  194. struct ibv_send_wr wr, *bad;
  195. struct ibv_sge sge;
  196. sge.addr = (uint64_t) (uintptr_t) addr;
  197. sge.length = (uint32_t) length;
  198. sge.lkey = mr ? mr->lkey : 0;
  199. wr.wr_id = (uintptr_t) context;
  200. wr.next = NULL;
  201. wr.sg_list = &sge;
  202. wr.num_sge = 1;
  203. wr.opcode = IBV_WR_SEND;
  204. wr.send_flags = flags;
  205. wr.wr.ud.ah = ah;
  206. wr.wr.ud.remote_qpn = remote_qpn;
  207. wr.wr.ud.remote_qkey = RDMA_UDP_QKEY;
  208. return rdma_seterrno(ibv_post_send(id->qp, &wr, &bad));
  209. }
  210. static inline int
  211. rdma_get_send_comp(struct rdma_cm_id *id, struct ibv_wc *wc)
  212. {
  213. struct ibv_cq *cq;
  214. void *context;
  215. int ret;
  216. do {
  217. ret = ibv_poll_cq(id->send_cq, 1, wc);
  218. if (ret)
  219. break;
  220. ret = ibv_req_notify_cq(id->send_cq, 0);
  221. if (ret)
  222. return rdma_seterrno(ret);
  223. ret = ibv_poll_cq(id->send_cq, 1, wc);
  224. if (ret)
  225. break;
  226. ret = ibv_get_cq_event(id->send_cq_channel, &cq, &context);
  227. if (ret)
  228. return ret;
  229. assert(cq == id->send_cq && context == id);
  230. ibv_ack_cq_events(id->send_cq, 1);
  231. } while (1);
  232. return (ret < 0) ? rdma_seterrno(ret) : ret;
  233. }
  234. static inline int
  235. rdma_get_recv_comp(struct rdma_cm_id *id, struct ibv_wc *wc)
  236. {
  237. struct ibv_cq *cq;
  238. void *context;
  239. int ret;
  240. do {
  241. ret = ibv_poll_cq(id->recv_cq, 1, wc);
  242. if (ret)
  243. break;
  244. ret = ibv_req_notify_cq(id->recv_cq, 0);
  245. if (ret)
  246. return rdma_seterrno(ret);
  247. ret = ibv_poll_cq(id->recv_cq, 1, wc);
  248. if (ret)
  249. break;
  250. ret = ibv_get_cq_event(id->recv_cq_channel, &cq, &context);
  251. if (ret)
  252. return ret;
  253. assert(cq == id->recv_cq && context == id);
  254. ibv_ack_cq_events(id->recv_cq, 1);
  255. } while (1);
  256. return (ret < 0) ? rdma_seterrno(ret) : ret;
  257. }
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. #endif /* RDMA_CMA_H */