backchannel.c 10 KB

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