backchannel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. * xprt_rdma_bc_maxpayload - Return maximum backchannel message size
  162. * @xprt: transport
  163. *
  164. * Returns maximum size, in bytes, of a backchannel message
  165. */
  166. size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt)
  167. {
  168. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  169. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  170. size_t maxmsg;
  171. maxmsg = min_t(unsigned int, cdata->inline_rsize, cdata->inline_wsize);
  172. return maxmsg - RPCRDMA_HDRLEN_MIN;
  173. }
  174. /**
  175. * rpcrdma_bc_marshal_reply - Send backwards direction reply
  176. * @rqst: buffer containing RPC reply data
  177. *
  178. * Returns zero on success.
  179. */
  180. int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
  181. {
  182. struct rpc_xprt *xprt = rqst->rq_xprt;
  183. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  184. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  185. struct rpcrdma_msg *headerp;
  186. size_t rpclen;
  187. headerp = rdmab_to_msg(req->rl_rdmabuf);
  188. headerp->rm_xid = rqst->rq_xid;
  189. headerp->rm_vers = rpcrdma_version;
  190. headerp->rm_credit =
  191. cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
  192. headerp->rm_type = rdma_msg;
  193. headerp->rm_body.rm_chunks[0] = xdr_zero;
  194. headerp->rm_body.rm_chunks[1] = xdr_zero;
  195. headerp->rm_body.rm_chunks[2] = xdr_zero;
  196. rpclen = rqst->rq_svec[0].iov_len;
  197. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  198. pr_info("RPC: %s: rpclen %zd headerp 0x%p lkey 0x%x\n",
  199. __func__, rpclen, headerp, rdmab_lkey(req->rl_rdmabuf));
  200. pr_info("RPC: %s: RPC/RDMA: %*ph\n",
  201. __func__, (int)RPCRDMA_HDRLEN_MIN, headerp);
  202. pr_info("RPC: %s: RPC: %*ph\n",
  203. __func__, (int)rpclen, rqst->rq_svec[0].iov_base);
  204. #endif
  205. req->rl_send_iov[0].addr = rdmab_addr(req->rl_rdmabuf);
  206. req->rl_send_iov[0].length = RPCRDMA_HDRLEN_MIN;
  207. req->rl_send_iov[0].lkey = rdmab_lkey(req->rl_rdmabuf);
  208. req->rl_send_iov[1].addr = rdmab_addr(req->rl_sendbuf);
  209. req->rl_send_iov[1].length = rpclen;
  210. req->rl_send_iov[1].lkey = rdmab_lkey(req->rl_sendbuf);
  211. req->rl_niovs = 2;
  212. return 0;
  213. }
  214. /**
  215. * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
  216. * @xprt: transport associated with these backchannel resources
  217. * @reqs: number of incoming requests to destroy; ignored
  218. */
  219. void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
  220. {
  221. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  222. struct rpc_rqst *rqst, *tmp;
  223. spin_lock_bh(&xprt->bc_pa_lock);
  224. list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  225. list_del(&rqst->rq_bc_pa_list);
  226. spin_unlock_bh(&xprt->bc_pa_lock);
  227. rpcrdma_bc_free_rqst(r_xprt, rqst);
  228. spin_lock_bh(&xprt->bc_pa_lock);
  229. }
  230. spin_unlock_bh(&xprt->bc_pa_lock);
  231. }
  232. /**
  233. * xprt_rdma_bc_free_rqst - Release a backchannel rqst
  234. * @rqst: request to release
  235. */
  236. void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
  237. {
  238. struct rpc_xprt *xprt = rqst->rq_xprt;
  239. dprintk("RPC: %s: freeing rqst %p (req %p)\n",
  240. __func__, rqst, rpcr_to_rdmar(rqst));
  241. smp_mb__before_atomic();
  242. WARN_ON_ONCE(!test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state));
  243. clear_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  244. smp_mb__after_atomic();
  245. spin_lock_bh(&xprt->bc_pa_lock);
  246. list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  247. spin_unlock_bh(&xprt->bc_pa_lock);
  248. }
  249. /**
  250. * rpcrdma_bc_receive_call - Handle a backward direction call
  251. * @xprt: transport receiving the call
  252. * @rep: receive buffer containing the call
  253. *
  254. * Called in the RPC reply handler, which runs in a tasklet.
  255. * Be quick about it.
  256. *
  257. * Operational assumptions:
  258. * o Backchannel credits are ignored, just as the NFS server
  259. * forechannel currently does
  260. * o The ULP manages a replay cache (eg, NFSv4.1 sessions).
  261. * No replay detection is done at the transport level
  262. */
  263. void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
  264. struct rpcrdma_rep *rep)
  265. {
  266. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  267. struct rpcrdma_msg *headerp;
  268. struct svc_serv *bc_serv;
  269. struct rpcrdma_req *req;
  270. struct rpc_rqst *rqst;
  271. struct xdr_buf *buf;
  272. size_t size;
  273. __be32 *p;
  274. headerp = rdmab_to_msg(rep->rr_rdmabuf);
  275. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  276. pr_info("RPC: %s: callback XID %08x, length=%u\n",
  277. __func__, be32_to_cpu(headerp->rm_xid), rep->rr_len);
  278. pr_info("RPC: %s: %*ph\n", __func__, rep->rr_len, headerp);
  279. #endif
  280. /* Sanity check:
  281. * Need at least enough bytes for RPC/RDMA header, as code
  282. * here references the header fields by array offset. Also,
  283. * backward calls are always inline, so ensure there
  284. * are some bytes beyond the RPC/RDMA header.
  285. */
  286. if (rep->rr_len < RPCRDMA_HDRLEN_MIN + 24)
  287. goto out_short;
  288. p = (__be32 *)((unsigned char *)headerp + RPCRDMA_HDRLEN_MIN);
  289. size = rep->rr_len - RPCRDMA_HDRLEN_MIN;
  290. /* Grab a free bc rqst */
  291. spin_lock(&xprt->bc_pa_lock);
  292. if (list_empty(&xprt->bc_pa_list)) {
  293. spin_unlock(&xprt->bc_pa_lock);
  294. goto out_overflow;
  295. }
  296. rqst = list_first_entry(&xprt->bc_pa_list,
  297. struct rpc_rqst, rq_bc_pa_list);
  298. list_del(&rqst->rq_bc_pa_list);
  299. spin_unlock(&xprt->bc_pa_lock);
  300. dprintk("RPC: %s: using rqst %p\n", __func__, rqst);
  301. /* Prepare rqst */
  302. rqst->rq_reply_bytes_recvd = 0;
  303. rqst->rq_bytes_sent = 0;
  304. rqst->rq_xid = headerp->rm_xid;
  305. rqst->rq_private_buf.len = size;
  306. set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  307. buf = &rqst->rq_rcv_buf;
  308. memset(buf, 0, sizeof(*buf));
  309. buf->head[0].iov_base = p;
  310. buf->head[0].iov_len = size;
  311. buf->len = size;
  312. /* The receive buffer has to be hooked to the rpcrdma_req
  313. * so that it can be reposted after the server is done
  314. * parsing it but just before sending the backward
  315. * direction reply.
  316. */
  317. req = rpcr_to_rdmar(rqst);
  318. dprintk("RPC: %s: attaching rep %p to req %p\n",
  319. __func__, rep, req);
  320. req->rl_reply = rep;
  321. /* Defeat the retransmit detection logic in send_request */
  322. req->rl_connect_cookie = 0;
  323. /* Queue rqst for ULP's callback service */
  324. bc_serv = xprt->bc_serv;
  325. spin_lock(&bc_serv->sv_cb_lock);
  326. list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
  327. spin_unlock(&bc_serv->sv_cb_lock);
  328. wake_up(&bc_serv->sv_cb_waitq);
  329. r_xprt->rx_stats.bcall_count++;
  330. return;
  331. out_overflow:
  332. pr_warn("RPC/RDMA backchannel overflow\n");
  333. xprt_disconnect_done(xprt);
  334. /* This receive buffer gets reposted automatically
  335. * when the connection is re-established.
  336. */
  337. return;
  338. out_short:
  339. pr_warn("RPC/RDMA short backward direction call\n");
  340. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
  341. xprt_disconnect_done(xprt);
  342. else
  343. pr_warn("RPC: %s: reposting rep %p\n",
  344. __func__, rep);
  345. }