backchannel.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (c) 2015 Oracle. All rights reserved.
  3. *
  4. * Support for backward direction RPCs on RPC/RDMA.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/sunrpc/xprt.h>
  8. #include <linux/sunrpc/svc.h>
  9. #include <linux/sunrpc/svc_xprt.h>
  10. #include "xprt_rdma.h"
  11. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  12. # define RPCDBG_FACILITY RPCDBG_TRANS
  13. #endif
  14. #undef RPCRDMA_BACKCHANNEL_DEBUG
  15. static void rpcrdma_bc_free_rqst(struct rpcrdma_xprt *r_xprt,
  16. struct rpc_rqst *rqst)
  17. {
  18. struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
  19. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  20. spin_lock(&buf->rb_reqslock);
  21. list_del(&req->rl_all);
  22. spin_unlock(&buf->rb_reqslock);
  23. rpcrdma_destroy_req(&r_xprt->rx_ia, req);
  24. kfree(rqst);
  25. }
  26. static int rpcrdma_bc_setup_rqst(struct rpcrdma_xprt *r_xprt,
  27. struct rpc_rqst *rqst)
  28. {
  29. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  30. struct rpcrdma_regbuf *rb;
  31. struct rpcrdma_req *req;
  32. struct xdr_buf *buf;
  33. size_t size;
  34. req = rpcrdma_create_req(r_xprt);
  35. if (IS_ERR(req))
  36. return PTR_ERR(req);
  37. req->rl_backchannel = true;
  38. size = RPCRDMA_INLINE_WRITE_THRESHOLD(rqst);
  39. rb = rpcrdma_alloc_regbuf(ia, size, GFP_KERNEL);
  40. if (IS_ERR(rb))
  41. goto out_fail;
  42. req->rl_rdmabuf = rb;
  43. size += RPCRDMA_INLINE_READ_THRESHOLD(rqst);
  44. rb = rpcrdma_alloc_regbuf(ia, size, GFP_KERNEL);
  45. if (IS_ERR(rb))
  46. goto out_fail;
  47. rb->rg_owner = req;
  48. req->rl_sendbuf = rb;
  49. /* so that rpcr_to_rdmar works when receiving a request */
  50. rqst->rq_buffer = (void *)req->rl_sendbuf->rg_base;
  51. buf = &rqst->rq_snd_buf;
  52. buf->head[0].iov_base = rqst->rq_buffer;
  53. buf->head[0].iov_len = 0;
  54. buf->tail[0].iov_base = NULL;
  55. buf->tail[0].iov_len = 0;
  56. buf->page_len = 0;
  57. buf->len = 0;
  58. buf->buflen = size;
  59. return 0;
  60. out_fail:
  61. rpcrdma_bc_free_rqst(r_xprt, rqst);
  62. return -ENOMEM;
  63. }
  64. /* Allocate and add receive buffers to the rpcrdma_buffer's
  65. * existing list of rep's. These are released when the
  66. * transport is destroyed.
  67. */
  68. static int rpcrdma_bc_setup_reps(struct rpcrdma_xprt *r_xprt,
  69. unsigned int count)
  70. {
  71. struct rpcrdma_rep *rep;
  72. int rc = 0;
  73. while (count--) {
  74. rep = rpcrdma_create_rep(r_xprt);
  75. if (IS_ERR(rep)) {
  76. pr_err("RPC: %s: reply buffer alloc failed\n",
  77. __func__);
  78. rc = PTR_ERR(rep);
  79. break;
  80. }
  81. rpcrdma_recv_buffer_put(rep);
  82. }
  83. return rc;
  84. }
  85. /**
  86. * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests
  87. * @xprt: transport associated with these backchannel resources
  88. * @reqs: number of concurrent incoming requests to expect
  89. *
  90. * Returns 0 on success; otherwise a negative errno
  91. */
  92. int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs)
  93. {
  94. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  95. struct rpcrdma_buffer *buffer = &r_xprt->rx_buf;
  96. struct rpc_rqst *rqst;
  97. unsigned int i;
  98. int rc;
  99. /* The backchannel reply path returns each rpc_rqst to the
  100. * bc_pa_list _after_ the reply is sent. If the server is
  101. * faster than the client, it can send another backward
  102. * direction request before the rpc_rqst is returned to the
  103. * list. The client rejects the request in this case.
  104. *
  105. * Twice as many rpc_rqsts are prepared to ensure there is
  106. * always an rpc_rqst available as soon as a reply is sent.
  107. */
  108. if (reqs > RPCRDMA_BACKWARD_WRS >> 1)
  109. goto out_err;
  110. for (i = 0; i < (reqs << 1); i++) {
  111. rqst = kzalloc(sizeof(*rqst), GFP_KERNEL);
  112. if (!rqst) {
  113. pr_err("RPC: %s: Failed to create bc rpc_rqst\n",
  114. __func__);
  115. goto out_free;
  116. }
  117. dprintk("RPC: %s: new rqst %p\n", __func__, rqst);
  118. rqst->rq_xprt = &r_xprt->rx_xprt;
  119. INIT_LIST_HEAD(&rqst->rq_list);
  120. INIT_LIST_HEAD(&rqst->rq_bc_list);
  121. if (rpcrdma_bc_setup_rqst(r_xprt, rqst))
  122. goto out_free;
  123. spin_lock_bh(&xprt->bc_pa_lock);
  124. list_add(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  125. spin_unlock_bh(&xprt->bc_pa_lock);
  126. }
  127. rc = rpcrdma_bc_setup_reps(r_xprt, reqs);
  128. if (rc)
  129. goto out_free;
  130. rc = rpcrdma_ep_post_extra_recv(r_xprt, reqs);
  131. if (rc)
  132. goto out_free;
  133. buffer->rb_bc_srv_max_requests = reqs;
  134. request_module("svcrdma");
  135. return 0;
  136. out_free:
  137. xprt_rdma_bc_destroy(xprt, reqs);
  138. out_err:
  139. pr_err("RPC: %s: setup backchannel transport failed\n", __func__);
  140. return -ENOMEM;
  141. }
  142. /**
  143. * xprt_rdma_bc_up - Create transport endpoint for backchannel service
  144. * @serv: server endpoint
  145. * @net: network namespace
  146. *
  147. * The "xprt" is an implied argument: it supplies the name of the
  148. * backchannel transport class.
  149. *
  150. * Returns zero on success, negative errno on failure
  151. */
  152. int xprt_rdma_bc_up(struct svc_serv *serv, struct net *net)
  153. {
  154. int ret;
  155. ret = svc_create_xprt(serv, "rdma-bc", net, PF_INET, 0, 0);
  156. if (ret < 0)
  157. return ret;
  158. return 0;
  159. }
  160. /**
  161. * rpcrdma_bc_marshal_reply - Send backwards direction reply
  162. * @rqst: buffer containing RPC reply data
  163. *
  164. * Returns zero on success.
  165. */
  166. int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
  167. {
  168. struct rpc_xprt *xprt = rqst->rq_xprt;
  169. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  170. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  171. struct rpcrdma_msg *headerp;
  172. size_t rpclen;
  173. headerp = rdmab_to_msg(req->rl_rdmabuf);
  174. headerp->rm_xid = rqst->rq_xid;
  175. headerp->rm_vers = rpcrdma_version;
  176. headerp->rm_credit =
  177. cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
  178. headerp->rm_type = rdma_msg;
  179. headerp->rm_body.rm_chunks[0] = xdr_zero;
  180. headerp->rm_body.rm_chunks[1] = xdr_zero;
  181. headerp->rm_body.rm_chunks[2] = xdr_zero;
  182. rpclen = rqst->rq_svec[0].iov_len;
  183. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  184. pr_info("RPC: %s: rpclen %zd headerp 0x%p lkey 0x%x\n",
  185. __func__, rpclen, headerp, rdmab_lkey(req->rl_rdmabuf));
  186. pr_info("RPC: %s: RPC/RDMA: %*ph\n",
  187. __func__, (int)RPCRDMA_HDRLEN_MIN, headerp);
  188. pr_info("RPC: %s: RPC: %*ph\n",
  189. __func__, (int)rpclen, rqst->rq_svec[0].iov_base);
  190. #endif
  191. req->rl_send_iov[0].addr = rdmab_addr(req->rl_rdmabuf);
  192. req->rl_send_iov[0].length = RPCRDMA_HDRLEN_MIN;
  193. req->rl_send_iov[0].lkey = rdmab_lkey(req->rl_rdmabuf);
  194. req->rl_send_iov[1].addr = rdmab_addr(req->rl_sendbuf);
  195. req->rl_send_iov[1].length = rpclen;
  196. req->rl_send_iov[1].lkey = rdmab_lkey(req->rl_sendbuf);
  197. req->rl_niovs = 2;
  198. return 0;
  199. }
  200. /**
  201. * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
  202. * @xprt: transport associated with these backchannel resources
  203. * @reqs: number of incoming requests to destroy; ignored
  204. */
  205. void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
  206. {
  207. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  208. struct rpc_rqst *rqst, *tmp;
  209. spin_lock_bh(&xprt->bc_pa_lock);
  210. list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  211. list_del(&rqst->rq_bc_pa_list);
  212. spin_unlock_bh(&xprt->bc_pa_lock);
  213. rpcrdma_bc_free_rqst(r_xprt, rqst);
  214. spin_lock_bh(&xprt->bc_pa_lock);
  215. }
  216. spin_unlock_bh(&xprt->bc_pa_lock);
  217. }
  218. /**
  219. * xprt_rdma_bc_free_rqst - Release a backchannel rqst
  220. * @rqst: request to release
  221. */
  222. void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
  223. {
  224. struct rpc_xprt *xprt = rqst->rq_xprt;
  225. dprintk("RPC: %s: freeing rqst %p (req %p)\n",
  226. __func__, rqst, rpcr_to_rdmar(rqst));
  227. smp_mb__before_atomic();
  228. WARN_ON_ONCE(!test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state));
  229. clear_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  230. smp_mb__after_atomic();
  231. spin_lock_bh(&xprt->bc_pa_lock);
  232. list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  233. spin_unlock_bh(&xprt->bc_pa_lock);
  234. }
  235. /**
  236. * rpcrdma_bc_receive_call - Handle a backward direction call
  237. * @xprt: transport receiving the call
  238. * @rep: receive buffer containing the call
  239. *
  240. * Called in the RPC reply handler, which runs in a tasklet.
  241. * Be quick about it.
  242. *
  243. * Operational assumptions:
  244. * o Backchannel credits are ignored, just as the NFS server
  245. * forechannel currently does
  246. * o The ULP manages a replay cache (eg, NFSv4.1 sessions).
  247. * No replay detection is done at the transport level
  248. */
  249. void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
  250. struct rpcrdma_rep *rep)
  251. {
  252. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  253. struct rpcrdma_msg *headerp;
  254. struct svc_serv *bc_serv;
  255. struct rpcrdma_req *req;
  256. struct rpc_rqst *rqst;
  257. struct xdr_buf *buf;
  258. size_t size;
  259. __be32 *p;
  260. headerp = rdmab_to_msg(rep->rr_rdmabuf);
  261. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  262. pr_info("RPC: %s: callback XID %08x, length=%u\n",
  263. __func__, be32_to_cpu(headerp->rm_xid), rep->rr_len);
  264. pr_info("RPC: %s: %*ph\n", __func__, rep->rr_len, headerp);
  265. #endif
  266. /* Sanity check:
  267. * Need at least enough bytes for RPC/RDMA header, as code
  268. * here references the header fields by array offset. Also,
  269. * backward calls are always inline, so ensure there
  270. * are some bytes beyond the RPC/RDMA header.
  271. */
  272. if (rep->rr_len < RPCRDMA_HDRLEN_MIN + 24)
  273. goto out_short;
  274. p = (__be32 *)((unsigned char *)headerp + RPCRDMA_HDRLEN_MIN);
  275. size = rep->rr_len - RPCRDMA_HDRLEN_MIN;
  276. /* Grab a free bc rqst */
  277. spin_lock(&xprt->bc_pa_lock);
  278. if (list_empty(&xprt->bc_pa_list)) {
  279. spin_unlock(&xprt->bc_pa_lock);
  280. goto out_overflow;
  281. }
  282. rqst = list_first_entry(&xprt->bc_pa_list,
  283. struct rpc_rqst, rq_bc_pa_list);
  284. list_del(&rqst->rq_bc_pa_list);
  285. spin_unlock(&xprt->bc_pa_lock);
  286. dprintk("RPC: %s: using rqst %p\n", __func__, rqst);
  287. /* Prepare rqst */
  288. rqst->rq_reply_bytes_recvd = 0;
  289. rqst->rq_bytes_sent = 0;
  290. rqst->rq_xid = headerp->rm_xid;
  291. set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  292. buf = &rqst->rq_rcv_buf;
  293. memset(buf, 0, sizeof(*buf));
  294. buf->head[0].iov_base = p;
  295. buf->head[0].iov_len = size;
  296. buf->len = size;
  297. /* The receive buffer has to be hooked to the rpcrdma_req
  298. * so that it can be reposted after the server is done
  299. * parsing it but just before sending the backward
  300. * direction reply.
  301. */
  302. req = rpcr_to_rdmar(rqst);
  303. dprintk("RPC: %s: attaching rep %p to req %p\n",
  304. __func__, rep, req);
  305. req->rl_reply = rep;
  306. /* Defeat the retransmit detection logic in send_request */
  307. req->rl_connect_cookie = 0;
  308. /* Queue rqst for ULP's callback service */
  309. bc_serv = xprt->bc_serv;
  310. spin_lock(&bc_serv->sv_cb_lock);
  311. list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
  312. spin_unlock(&bc_serv->sv_cb_lock);
  313. wake_up(&bc_serv->sv_cb_waitq);
  314. r_xprt->rx_stats.bcall_count++;
  315. return;
  316. out_overflow:
  317. pr_warn("RPC/RDMA backchannel overflow\n");
  318. xprt_disconnect_done(xprt);
  319. /* This receive buffer gets reposted automatically
  320. * when the connection is re-established.
  321. */
  322. return;
  323. out_short:
  324. pr_warn("RPC/RDMA short backward direction call\n");
  325. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
  326. xprt_disconnect_done(xprt);
  327. else
  328. pr_warn("RPC: %s: reposting rep %p\n",
  329. __func__, rep);
  330. }