backchannel.c 10.0 KB

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