backchannel.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. goto out_free;
  104. dprintk("RPC: %s: new rqst %p\n", __func__, rqst);
  105. rqst->rq_xprt = &r_xprt->rx_xprt;
  106. INIT_LIST_HEAD(&rqst->rq_list);
  107. INIT_LIST_HEAD(&rqst->rq_bc_list);
  108. if (rpcrdma_bc_setup_rqst(r_xprt, rqst))
  109. goto out_free;
  110. spin_lock_bh(&xprt->bc_pa_lock);
  111. list_add(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  112. spin_unlock_bh(&xprt->bc_pa_lock);
  113. }
  114. rc = rpcrdma_bc_setup_reps(r_xprt, reqs);
  115. if (rc)
  116. goto out_free;
  117. rc = rpcrdma_ep_post_extra_recv(r_xprt, reqs);
  118. if (rc)
  119. goto out_free;
  120. buffer->rb_bc_srv_max_requests = reqs;
  121. request_module("svcrdma");
  122. return 0;
  123. out_free:
  124. xprt_rdma_bc_destroy(xprt, reqs);
  125. out_err:
  126. pr_err("RPC: %s: setup backchannel transport failed\n", __func__);
  127. return -ENOMEM;
  128. }
  129. /**
  130. * xprt_rdma_bc_up - Create transport endpoint for backchannel service
  131. * @serv: server endpoint
  132. * @net: network namespace
  133. *
  134. * The "xprt" is an implied argument: it supplies the name of the
  135. * backchannel transport class.
  136. *
  137. * Returns zero on success, negative errno on failure
  138. */
  139. int xprt_rdma_bc_up(struct svc_serv *serv, struct net *net)
  140. {
  141. int ret;
  142. ret = svc_create_xprt(serv, "rdma-bc", net, PF_INET, 0, 0);
  143. if (ret < 0)
  144. return ret;
  145. return 0;
  146. }
  147. /**
  148. * xprt_rdma_bc_maxpayload - Return maximum backchannel message size
  149. * @xprt: transport
  150. *
  151. * Returns maximum size, in bytes, of a backchannel message
  152. */
  153. size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt)
  154. {
  155. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  156. struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
  157. size_t maxmsg;
  158. maxmsg = min_t(unsigned int, cdata->inline_rsize, cdata->inline_wsize);
  159. maxmsg = min_t(unsigned int, maxmsg, PAGE_SIZE);
  160. return maxmsg - RPCRDMA_HDRLEN_MIN;
  161. }
  162. /**
  163. * rpcrdma_bc_marshal_reply - Send backwards direction reply
  164. * @rqst: buffer containing RPC reply data
  165. *
  166. * Returns zero on success.
  167. */
  168. int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
  169. {
  170. struct rpc_xprt *xprt = rqst->rq_xprt;
  171. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  172. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  173. struct rpcrdma_msg *headerp;
  174. headerp = rdmab_to_msg(req->rl_rdmabuf);
  175. headerp->rm_xid = rqst->rq_xid;
  176. headerp->rm_vers = rpcrdma_version;
  177. headerp->rm_credit =
  178. cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
  179. headerp->rm_type = rdma_msg;
  180. headerp->rm_body.rm_chunks[0] = xdr_zero;
  181. headerp->rm_body.rm_chunks[1] = xdr_zero;
  182. headerp->rm_body.rm_chunks[2] = xdr_zero;
  183. if (!rpcrdma_prepare_send_sges(&r_xprt->rx_ia, req, RPCRDMA_HDRLEN_MIN,
  184. &rqst->rq_snd_buf, rpcrdma_noch))
  185. return -EIO;
  186. return 0;
  187. }
  188. /**
  189. * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
  190. * @xprt: transport associated with these backchannel resources
  191. * @reqs: number of incoming requests to destroy; ignored
  192. */
  193. void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
  194. {
  195. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  196. struct rpc_rqst *rqst, *tmp;
  197. spin_lock_bh(&xprt->bc_pa_lock);
  198. list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
  199. list_del(&rqst->rq_bc_pa_list);
  200. spin_unlock_bh(&xprt->bc_pa_lock);
  201. rpcrdma_bc_free_rqst(r_xprt, rqst);
  202. spin_lock_bh(&xprt->bc_pa_lock);
  203. }
  204. spin_unlock_bh(&xprt->bc_pa_lock);
  205. }
  206. /**
  207. * xprt_rdma_bc_free_rqst - Release a backchannel rqst
  208. * @rqst: request to release
  209. */
  210. void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
  211. {
  212. struct rpc_xprt *xprt = rqst->rq_xprt;
  213. dprintk("RPC: %s: freeing rqst %p (req %p)\n",
  214. __func__, rqst, rpcr_to_rdmar(rqst));
  215. smp_mb__before_atomic();
  216. WARN_ON_ONCE(!test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state));
  217. clear_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  218. smp_mb__after_atomic();
  219. spin_lock_bh(&xprt->bc_pa_lock);
  220. list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
  221. spin_unlock_bh(&xprt->bc_pa_lock);
  222. }
  223. /**
  224. * rpcrdma_bc_receive_call - Handle a backward direction call
  225. * @xprt: transport receiving the call
  226. * @rep: receive buffer containing the call
  227. *
  228. * Called in the RPC reply handler, which runs in a tasklet.
  229. * Be quick about it.
  230. *
  231. * Operational assumptions:
  232. * o Backchannel credits are ignored, just as the NFS server
  233. * forechannel currently does
  234. * o The ULP manages a replay cache (eg, NFSv4.1 sessions).
  235. * No replay detection is done at the transport level
  236. */
  237. void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
  238. struct rpcrdma_rep *rep)
  239. {
  240. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  241. struct rpcrdma_msg *headerp;
  242. struct svc_serv *bc_serv;
  243. struct rpcrdma_req *req;
  244. struct rpc_rqst *rqst;
  245. struct xdr_buf *buf;
  246. size_t size;
  247. __be32 *p;
  248. headerp = rdmab_to_msg(rep->rr_rdmabuf);
  249. #ifdef RPCRDMA_BACKCHANNEL_DEBUG
  250. pr_info("RPC: %s: callback XID %08x, length=%u\n",
  251. __func__, be32_to_cpu(headerp->rm_xid), rep->rr_len);
  252. pr_info("RPC: %s: %*ph\n", __func__, rep->rr_len, headerp);
  253. #endif
  254. /* Sanity check:
  255. * Need at least enough bytes for RPC/RDMA header, as code
  256. * here references the header fields by array offset. Also,
  257. * backward calls are always inline, so ensure there
  258. * are some bytes beyond the RPC/RDMA header.
  259. */
  260. if (rep->rr_len < RPCRDMA_HDRLEN_MIN + 24)
  261. goto out_short;
  262. p = (__be32 *)((unsigned char *)headerp + RPCRDMA_HDRLEN_MIN);
  263. size = rep->rr_len - RPCRDMA_HDRLEN_MIN;
  264. /* Grab a free bc rqst */
  265. spin_lock(&xprt->bc_pa_lock);
  266. if (list_empty(&xprt->bc_pa_list)) {
  267. spin_unlock(&xprt->bc_pa_lock);
  268. goto out_overflow;
  269. }
  270. rqst = list_first_entry(&xprt->bc_pa_list,
  271. struct rpc_rqst, rq_bc_pa_list);
  272. list_del(&rqst->rq_bc_pa_list);
  273. spin_unlock(&xprt->bc_pa_lock);
  274. dprintk("RPC: %s: using rqst %p\n", __func__, rqst);
  275. /* Prepare rqst */
  276. rqst->rq_reply_bytes_recvd = 0;
  277. rqst->rq_bytes_sent = 0;
  278. rqst->rq_xid = headerp->rm_xid;
  279. rqst->rq_private_buf.len = size;
  280. set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
  281. buf = &rqst->rq_rcv_buf;
  282. memset(buf, 0, sizeof(*buf));
  283. buf->head[0].iov_base = p;
  284. buf->head[0].iov_len = size;
  285. buf->len = size;
  286. /* The receive buffer has to be hooked to the rpcrdma_req
  287. * so that it can be reposted after the server is done
  288. * parsing it but just before sending the backward
  289. * direction reply.
  290. */
  291. req = rpcr_to_rdmar(rqst);
  292. dprintk("RPC: %s: attaching rep %p to req %p\n",
  293. __func__, rep, req);
  294. req->rl_reply = rep;
  295. /* Defeat the retransmit detection logic in send_request */
  296. req->rl_connect_cookie = 0;
  297. /* Queue rqst for ULP's callback service */
  298. bc_serv = xprt->bc_serv;
  299. spin_lock(&bc_serv->sv_cb_lock);
  300. list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
  301. spin_unlock(&bc_serv->sv_cb_lock);
  302. wake_up(&bc_serv->sv_cb_waitq);
  303. r_xprt->rx_stats.bcall_count++;
  304. return;
  305. out_overflow:
  306. pr_warn("RPC/RDMA backchannel overflow\n");
  307. xprt_disconnect_done(xprt);
  308. /* This receive buffer gets reposted automatically
  309. * when the connection is re-established.
  310. */
  311. return;
  312. out_short:
  313. pr_warn("RPC/RDMA short backward direction call\n");
  314. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, rep))
  315. xprt_disconnect_done(xprt);
  316. else
  317. pr_warn("RPC: %s: reposting rep %p\n",
  318. __func__, rep);
  319. }