svc_rdma_backchannel.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015-2018 Oracle. All rights reserved.
  4. *
  5. * Support for backward direction RPCs on RPC/RDMA (server-side).
  6. */
  7. #include <linux/module.h>
  8. #include <linux/sunrpc/svc_rdma.h>
  9. #include "xprt_rdma.h"
  10. #include <trace/events/rpcrdma.h>
  11. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  12. #undef SVCRDMA_BACKCHANNEL_DEBUG
  13. /**
  14. * svc_rdma_handle_bc_reply - Process incoming backchannel reply
  15. * @xprt: controlling backchannel transport
  16. * @rdma_resp: pointer to incoming transport header
  17. * @rcvbuf: XDR buffer into which to decode the reply
  18. *
  19. * Returns:
  20. * %0 if @rcvbuf is filled in, xprt_complete_rqst called,
  21. * %-EAGAIN if server should call ->recvfrom again.
  22. */
  23. int svc_rdma_handle_bc_reply(struct rpc_xprt *xprt, __be32 *rdma_resp,
  24. struct xdr_buf *rcvbuf)
  25. {
  26. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  27. struct kvec *dst, *src = &rcvbuf->head[0];
  28. struct rpc_rqst *req;
  29. unsigned long cwnd;
  30. u32 credits;
  31. size_t len;
  32. __be32 xid;
  33. __be32 *p;
  34. int ret;
  35. p = (__be32 *)src->iov_base;
  36. len = src->iov_len;
  37. xid = *rdma_resp;
  38. #ifdef SVCRDMA_BACKCHANNEL_DEBUG
  39. pr_info("%s: xid=%08x, length=%zu\n",
  40. __func__, be32_to_cpu(xid), len);
  41. pr_info("%s: RPC/RDMA: %*ph\n",
  42. __func__, (int)RPCRDMA_HDRLEN_MIN, rdma_resp);
  43. pr_info("%s: RPC: %*ph\n",
  44. __func__, (int)len, p);
  45. #endif
  46. ret = -EAGAIN;
  47. if (src->iov_len < 24)
  48. goto out_shortreply;
  49. spin_lock(&xprt->recv_lock);
  50. req = xprt_lookup_rqst(xprt, xid);
  51. if (!req)
  52. goto out_notfound;
  53. dst = &req->rq_private_buf.head[0];
  54. memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
  55. if (dst->iov_len < len)
  56. goto out_unlock;
  57. memcpy(dst->iov_base, p, len);
  58. credits = be32_to_cpup(rdma_resp + 2);
  59. if (credits == 0)
  60. credits = 1; /* don't deadlock */
  61. else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
  62. credits = r_xprt->rx_buf.rb_bc_max_requests;
  63. spin_lock_bh(&xprt->transport_lock);
  64. cwnd = xprt->cwnd;
  65. xprt->cwnd = credits << RPC_CWNDSHIFT;
  66. if (xprt->cwnd > cwnd)
  67. xprt_release_rqst_cong(req->rq_task);
  68. spin_unlock_bh(&xprt->transport_lock);
  69. ret = 0;
  70. xprt_complete_rqst(req->rq_task, rcvbuf->len);
  71. rcvbuf->len = 0;
  72. out_unlock:
  73. spin_unlock(&xprt->recv_lock);
  74. out:
  75. return ret;
  76. out_shortreply:
  77. dprintk("svcrdma: short bc reply: xprt=%p, len=%zu\n",
  78. xprt, src->iov_len);
  79. goto out;
  80. out_notfound:
  81. dprintk("svcrdma: unrecognized bc reply: xprt=%p, xid=%08x\n",
  82. xprt, be32_to_cpu(xid));
  83. goto out_unlock;
  84. }
  85. /* Send a backwards direction RPC call.
  86. *
  87. * Caller holds the connection's mutex and has already marshaled
  88. * the RPC/RDMA request.
  89. *
  90. * This is similar to svc_rdma_send_reply_msg, but takes a struct
  91. * rpc_rqst instead, does not support chunks, and avoids blocking
  92. * memory allocation.
  93. *
  94. * XXX: There is still an opportunity to block in svc_rdma_send()
  95. * if there are no SQ entries to post the Send. This may occur if
  96. * the adapter has a small maximum SQ depth.
  97. */
  98. static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
  99. struct rpc_rqst *rqst,
  100. struct svc_rdma_send_ctxt *ctxt)
  101. {
  102. int ret;
  103. ret = svc_rdma_map_reply_msg(rdma, ctxt, &rqst->rq_snd_buf, NULL);
  104. if (ret < 0)
  105. return -EIO;
  106. /* Bump page refcnt so Send completion doesn't release
  107. * the rq_buffer before all retransmits are complete.
  108. */
  109. get_page(virt_to_page(rqst->rq_buffer));
  110. ctxt->sc_send_wr.opcode = IB_WR_SEND;
  111. return svc_rdma_send(rdma, &ctxt->sc_send_wr);
  112. }
  113. /* Server-side transport endpoint wants a whole page for its send
  114. * buffer. The client RPC code constructs the RPC header in this
  115. * buffer before it invokes ->send_request.
  116. */
  117. static int
  118. xprt_rdma_bc_allocate(struct rpc_task *task)
  119. {
  120. struct rpc_rqst *rqst = task->tk_rqstp;
  121. size_t size = rqst->rq_callsize;
  122. struct page *page;
  123. if (size > PAGE_SIZE) {
  124. WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
  125. size);
  126. return -EINVAL;
  127. }
  128. page = alloc_page(RPCRDMA_DEF_GFP);
  129. if (!page)
  130. return -ENOMEM;
  131. rqst->rq_buffer = page_address(page);
  132. rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
  133. if (!rqst->rq_rbuffer) {
  134. put_page(page);
  135. return -ENOMEM;
  136. }
  137. return 0;
  138. }
  139. static void
  140. xprt_rdma_bc_free(struct rpc_task *task)
  141. {
  142. struct rpc_rqst *rqst = task->tk_rqstp;
  143. put_page(virt_to_page(rqst->rq_buffer));
  144. kfree(rqst->rq_rbuffer);
  145. }
  146. static int
  147. rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
  148. {
  149. struct rpc_xprt *xprt = rqst->rq_xprt;
  150. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  151. struct svc_rdma_send_ctxt *ctxt;
  152. __be32 *p;
  153. int rc;
  154. ctxt = svc_rdma_send_ctxt_get(rdma);
  155. if (!ctxt)
  156. goto drop_connection;
  157. p = ctxt->sc_xprt_buf;
  158. *p++ = rqst->rq_xid;
  159. *p++ = rpcrdma_version;
  160. *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
  161. *p++ = rdma_msg;
  162. *p++ = xdr_zero;
  163. *p++ = xdr_zero;
  164. *p = xdr_zero;
  165. svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_MIN);
  166. #ifdef SVCRDMA_BACKCHANNEL_DEBUG
  167. pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer);
  168. #endif
  169. rc = svc_rdma_bc_sendto(rdma, rqst, ctxt);
  170. if (rc) {
  171. svc_rdma_send_ctxt_put(rdma, ctxt);
  172. goto drop_connection;
  173. }
  174. return rc;
  175. drop_connection:
  176. dprintk("svcrdma: failed to send bc call\n");
  177. xprt_disconnect_done(xprt);
  178. return -ENOTCONN;
  179. }
  180. /* Send an RPC call on the passive end of a transport
  181. * connection.
  182. */
  183. static int
  184. xprt_rdma_bc_send_request(struct rpc_task *task)
  185. {
  186. struct rpc_rqst *rqst = task->tk_rqstp;
  187. struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
  188. struct svcxprt_rdma *rdma;
  189. int ret;
  190. dprintk("svcrdma: sending bc call with xid: %08x\n",
  191. be32_to_cpu(rqst->rq_xid));
  192. if (!mutex_trylock(&sxprt->xpt_mutex)) {
  193. rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL);
  194. if (!mutex_trylock(&sxprt->xpt_mutex))
  195. return -EAGAIN;
  196. rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task);
  197. }
  198. ret = -ENOTCONN;
  199. rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt);
  200. if (!test_bit(XPT_DEAD, &sxprt->xpt_flags))
  201. ret = rpcrdma_bc_send_request(rdma, rqst);
  202. mutex_unlock(&sxprt->xpt_mutex);
  203. if (ret < 0)
  204. return ret;
  205. return 0;
  206. }
  207. static void
  208. xprt_rdma_bc_close(struct rpc_xprt *xprt)
  209. {
  210. dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
  211. xprt->cwnd = RPC_CWNDSHIFT;
  212. }
  213. static void
  214. xprt_rdma_bc_put(struct rpc_xprt *xprt)
  215. {
  216. dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
  217. xprt_free(xprt);
  218. module_put(THIS_MODULE);
  219. }
  220. static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
  221. .reserve_xprt = xprt_reserve_xprt_cong,
  222. .release_xprt = xprt_release_xprt_cong,
  223. .alloc_slot = xprt_alloc_slot,
  224. .free_slot = xprt_free_slot,
  225. .release_request = xprt_release_rqst_cong,
  226. .buf_alloc = xprt_rdma_bc_allocate,
  227. .buf_free = xprt_rdma_bc_free,
  228. .send_request = xprt_rdma_bc_send_request,
  229. .set_retrans_timeout = xprt_set_retrans_timeout_def,
  230. .close = xprt_rdma_bc_close,
  231. .destroy = xprt_rdma_bc_put,
  232. .print_stats = xprt_rdma_print_stats
  233. };
  234. static const struct rpc_timeout xprt_rdma_bc_timeout = {
  235. .to_initval = 60 * HZ,
  236. .to_maxval = 60 * HZ,
  237. };
  238. /* It shouldn't matter if the number of backchannel session slots
  239. * doesn't match the number of RPC/RDMA credits. That just means
  240. * one or the other will have extra slots that aren't used.
  241. */
  242. static struct rpc_xprt *
  243. xprt_setup_rdma_bc(struct xprt_create *args)
  244. {
  245. struct rpc_xprt *xprt;
  246. struct rpcrdma_xprt *new_xprt;
  247. if (args->addrlen > sizeof(xprt->addr)) {
  248. dprintk("RPC: %s: address too large\n", __func__);
  249. return ERR_PTR(-EBADF);
  250. }
  251. xprt = xprt_alloc(args->net, sizeof(*new_xprt),
  252. RPCRDMA_MAX_BC_REQUESTS,
  253. RPCRDMA_MAX_BC_REQUESTS);
  254. if (!xprt) {
  255. dprintk("RPC: %s: couldn't allocate rpc_xprt\n",
  256. __func__);
  257. return ERR_PTR(-ENOMEM);
  258. }
  259. xprt->timeout = &xprt_rdma_bc_timeout;
  260. xprt_set_bound(xprt);
  261. xprt_set_connected(xprt);
  262. xprt->bind_timeout = RPCRDMA_BIND_TO;
  263. xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
  264. xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
  265. xprt->prot = XPRT_TRANSPORT_BC_RDMA;
  266. xprt->tsh_size = 0;
  267. xprt->ops = &xprt_rdma_bc_procs;
  268. memcpy(&xprt->addr, args->dstaddr, args->addrlen);
  269. xprt->addrlen = args->addrlen;
  270. xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
  271. xprt->resvport = 0;
  272. xprt->max_payload = xprt_rdma_max_inline_read;
  273. new_xprt = rpcx_to_rdmax(xprt);
  274. new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
  275. xprt_get(xprt);
  276. args->bc_xprt->xpt_bc_xprt = xprt;
  277. xprt->bc_xprt = args->bc_xprt;
  278. if (!try_module_get(THIS_MODULE))
  279. goto out_fail;
  280. /* Final put for backchannel xprt is in __svc_rdma_free */
  281. xprt_get(xprt);
  282. return xprt;
  283. out_fail:
  284. xprt_rdma_free_addresses(xprt);
  285. args->bc_xprt->xpt_bc_xprt = NULL;
  286. args->bc_xprt->xpt_bc_xps = NULL;
  287. xprt_put(xprt);
  288. xprt_free(xprt);
  289. return ERR_PTR(-EINVAL);
  290. }
  291. struct xprt_class xprt_rdma_bc = {
  292. .list = LIST_HEAD_INIT(xprt_rdma_bc.list),
  293. .name = "rdma backchannel",
  294. .owner = THIS_MODULE,
  295. .ident = XPRT_TRANSPORT_BC_RDMA,
  296. .setup = xprt_setup_rdma_bc,
  297. };