xref: /linux/net/sunrpc/xprtrdma/backchannel.c (revision 75891f502f5fc70f52a01af5b924384ed4866907)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2f531a5dbSChuck Lever /*
3f531a5dbSChuck Lever  * Copyright (c) 2015 Oracle.  All rights reserved.
4f531a5dbSChuck Lever  *
5f531a5dbSChuck Lever  * Support for backward direction RPCs on RPC/RDMA.
6f531a5dbSChuck Lever  */
7f531a5dbSChuck Lever 
8f531a5dbSChuck Lever #include <linux/module.h>
963cae470SChuck Lever #include <linux/sunrpc/xprt.h>
1063cae470SChuck Lever #include <linux/sunrpc/svc.h>
1176566773SChuck Lever #include <linux/sunrpc/svc_xprt.h>
12bd2abef3SChuck Lever #include <linux/sunrpc/svc_rdma.h>
13f531a5dbSChuck Lever 
14f531a5dbSChuck Lever #include "xprt_rdma.h"
15b6e717cbSChuck Lever #include <trace/events/rpcrdma.h>
16f531a5dbSChuck Lever 
17f531a5dbSChuck Lever #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
18f531a5dbSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
19f531a5dbSChuck Lever #endif
20f531a5dbSChuck Lever 
21c8bbe0c7SChuck Lever #undef RPCRDMA_BACKCHANNEL_DEBUG
2263cae470SChuck Lever 
23f531a5dbSChuck Lever static void rpcrdma_bc_free_rqst(struct rpcrdma_xprt *r_xprt,
24f531a5dbSChuck Lever 				 struct rpc_rqst *rqst)
25f531a5dbSChuck Lever {
26f531a5dbSChuck Lever 	struct rpcrdma_buffer *buf = &r_xprt->rx_buf;
27f531a5dbSChuck Lever 	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
28f531a5dbSChuck Lever 
29f531a5dbSChuck Lever 	spin_lock(&buf->rb_reqslock);
30f531a5dbSChuck Lever 	list_del(&req->rl_all);
31f531a5dbSChuck Lever 	spin_unlock(&buf->rb_reqslock);
32f531a5dbSChuck Lever 
3313650c23SChuck Lever 	rpcrdma_destroy_req(req);
34f531a5dbSChuck Lever }
35f531a5dbSChuck Lever 
36edb41e61SChuck Lever static int rpcrdma_bc_setup_reqs(struct rpcrdma_xprt *r_xprt,
37edb41e61SChuck Lever 				 unsigned int count)
38f531a5dbSChuck Lever {
39edb41e61SChuck Lever 	struct rpc_xprt *xprt = &r_xprt->rx_xprt;
40edb41e61SChuck Lever 	struct rpc_rqst *rqst;
41edb41e61SChuck Lever 	unsigned int i;
42edb41e61SChuck Lever 
43edb41e61SChuck Lever 	for (i = 0; i < (count << 1); i++) {
44f531a5dbSChuck Lever 		struct rpcrdma_regbuf *rb;
45f531a5dbSChuck Lever 		struct rpcrdma_req *req;
46f531a5dbSChuck Lever 		size_t size;
47f531a5dbSChuck Lever 
48f531a5dbSChuck Lever 		req = rpcrdma_create_req(r_xprt);
49abfb6897SDan Carpenter 		if (IS_ERR(req))
50abfb6897SDan Carpenter 			return PTR_ERR(req);
51edb41e61SChuck Lever 		rqst = &req->rl_slot;
52edb41e61SChuck Lever 
53edb41e61SChuck Lever 		rqst->rq_xprt = xprt;
54edb41e61SChuck Lever 		INIT_LIST_HEAD(&rqst->rq_bc_list);
55edb41e61SChuck Lever 		__set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
56edb41e61SChuck Lever 		spin_lock_bh(&xprt->bc_pa_lock);
57edb41e61SChuck Lever 		list_add(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
58edb41e61SChuck Lever 		spin_unlock_bh(&xprt->bc_pa_lock);
59f531a5dbSChuck Lever 
6008cf2efdSChuck Lever 		size = r_xprt->rx_data.inline_rsize;
6113650c23SChuck Lever 		rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, GFP_KERNEL);
62f531a5dbSChuck Lever 		if (IS_ERR(rb))
63f531a5dbSChuck Lever 			goto out_fail;
64f531a5dbSChuck Lever 		req->rl_sendbuf = rb;
6562aee0e3SChuck Lever 		xdr_buf_init(&rqst->rq_snd_buf, rb->rg_base,
6662aee0e3SChuck Lever 			     min_t(size_t, size, PAGE_SIZE));
67edb41e61SChuck Lever 	}
68f531a5dbSChuck Lever 	return 0;
69f531a5dbSChuck Lever 
70f531a5dbSChuck Lever out_fail:
71f531a5dbSChuck Lever 	rpcrdma_bc_free_rqst(r_xprt, rqst);
72f531a5dbSChuck Lever 	return -ENOMEM;
73f531a5dbSChuck Lever }
74f531a5dbSChuck Lever 
75f531a5dbSChuck Lever /**
76f531a5dbSChuck Lever  * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests
77f531a5dbSChuck Lever  * @xprt: transport associated with these backchannel resources
78f531a5dbSChuck Lever  * @reqs: number of concurrent incoming requests to expect
79f531a5dbSChuck Lever  *
80f531a5dbSChuck Lever  * Returns 0 on success; otherwise a negative errno
81f531a5dbSChuck Lever  */
82f531a5dbSChuck Lever int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs)
83f531a5dbSChuck Lever {
84f531a5dbSChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
85f531a5dbSChuck Lever 	int rc;
86f531a5dbSChuck Lever 
87f531a5dbSChuck Lever 	/* The backchannel reply path returns each rpc_rqst to the
88f531a5dbSChuck Lever 	 * bc_pa_list _after_ the reply is sent. If the server is
89f531a5dbSChuck Lever 	 * faster than the client, it can send another backward
90f531a5dbSChuck Lever 	 * direction request before the rpc_rqst is returned to the
91f531a5dbSChuck Lever 	 * list. The client rejects the request in this case.
92f531a5dbSChuck Lever 	 *
93f531a5dbSChuck Lever 	 * Twice as many rpc_rqsts are prepared to ensure there is
94f531a5dbSChuck Lever 	 * always an rpc_rqst available as soon as a reply is sent.
95f531a5dbSChuck Lever 	 */
96124fa17dSChuck Lever 	if (reqs > RPCRDMA_BACKWARD_WRS >> 1)
97124fa17dSChuck Lever 		goto out_err;
98124fa17dSChuck Lever 
99edb41e61SChuck Lever 	rc = rpcrdma_bc_setup_reqs(r_xprt, reqs);
100f531a5dbSChuck Lever 	if (rc)
101f531a5dbSChuck Lever 		goto out_free;
102f531a5dbSChuck Lever 
103edb41e61SChuck Lever 	r_xprt->rx_buf.rb_bc_srv_max_requests = reqs;
104f531a5dbSChuck Lever 	request_module("svcrdma");
105fc1eb807SChuck Lever 	trace_xprtrdma_cb_setup(r_xprt, reqs);
106f531a5dbSChuck Lever 	return 0;
107f531a5dbSChuck Lever 
108f531a5dbSChuck Lever out_free:
109f531a5dbSChuck Lever 	xprt_rdma_bc_destroy(xprt, reqs);
110f531a5dbSChuck Lever 
111124fa17dSChuck Lever out_err:
112f531a5dbSChuck Lever 	pr_err("RPC:       %s: setup backchannel transport failed\n", __func__);
113f531a5dbSChuck Lever 	return -ENOMEM;
114f531a5dbSChuck Lever }
115f531a5dbSChuck Lever 
116f531a5dbSChuck Lever /**
11776566773SChuck Lever  * xprt_rdma_bc_up - Create transport endpoint for backchannel service
11876566773SChuck Lever  * @serv: server endpoint
11976566773SChuck Lever  * @net: network namespace
12076566773SChuck Lever  *
12176566773SChuck Lever  * The "xprt" is an implied argument: it supplies the name of the
12276566773SChuck Lever  * backchannel transport class.
12376566773SChuck Lever  *
12476566773SChuck Lever  * Returns zero on success, negative errno on failure
12576566773SChuck Lever  */
12676566773SChuck Lever int xprt_rdma_bc_up(struct svc_serv *serv, struct net *net)
12776566773SChuck Lever {
12876566773SChuck Lever 	int ret;
12976566773SChuck Lever 
13076566773SChuck Lever 	ret = svc_create_xprt(serv, "rdma-bc", net, PF_INET, 0, 0);
13176566773SChuck Lever 	if (ret < 0)
13276566773SChuck Lever 		return ret;
13376566773SChuck Lever 	return 0;
13476566773SChuck Lever }
13576566773SChuck Lever 
13676566773SChuck Lever /**
1376b26cc8cSChuck Lever  * xprt_rdma_bc_maxpayload - Return maximum backchannel message size
1386b26cc8cSChuck Lever  * @xprt: transport
1396b26cc8cSChuck Lever  *
1406b26cc8cSChuck Lever  * Returns maximum size, in bytes, of a backchannel message
1416b26cc8cSChuck Lever  */
1426b26cc8cSChuck Lever size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt)
1436b26cc8cSChuck Lever {
1446b26cc8cSChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
1456b26cc8cSChuck Lever 	struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data;
1466b26cc8cSChuck Lever 	size_t maxmsg;
1476b26cc8cSChuck Lever 
1486b26cc8cSChuck Lever 	maxmsg = min_t(unsigned int, cdata->inline_rsize, cdata->inline_wsize);
14962aee0e3SChuck Lever 	maxmsg = min_t(unsigned int, maxmsg, PAGE_SIZE);
1506b26cc8cSChuck Lever 	return maxmsg - RPCRDMA_HDRLEN_MIN;
1516b26cc8cSChuck Lever }
1526b26cc8cSChuck Lever 
153cf73daf5SChuck Lever static int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
15483128a60SChuck Lever {
1557ec910e7SChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
15683128a60SChuck Lever 	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
1577ec910e7SChuck Lever 	__be32 *p;
15883128a60SChuck Lever 
1597ec910e7SChuck Lever 	rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0);
1607ec910e7SChuck Lever 	xdr_init_encode(&req->rl_stream, &req->rl_hdrbuf,
1617ec910e7SChuck Lever 			req->rl_rdmabuf->rg_base);
1627ec910e7SChuck Lever 
1637ec910e7SChuck Lever 	p = xdr_reserve_space(&req->rl_stream, 28);
1647ec910e7SChuck Lever 	if (unlikely(!p))
1657ec910e7SChuck Lever 		return -EIO;
1667ec910e7SChuck Lever 	*p++ = rqst->rq_xid;
1677ec910e7SChuck Lever 	*p++ = rpcrdma_version;
1687ec910e7SChuck Lever 	*p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
1697ec910e7SChuck Lever 	*p++ = rdma_msg;
1707ec910e7SChuck Lever 	*p++ = xdr_zero;
1717ec910e7SChuck Lever 	*p++ = xdr_zero;
1727ec910e7SChuck Lever 	*p = xdr_zero;
17383128a60SChuck Lever 
174857f9acaSChuck Lever 	if (rpcrdma_prepare_send_sges(r_xprt, req, RPCRDMA_HDRLEN_MIN,
175655fec69SChuck Lever 				      &rqst->rq_snd_buf, rpcrdma_noch))
17654cbd6b0SChuck Lever 		return -EIO;
177fc1eb807SChuck Lever 
178fc1eb807SChuck Lever 	trace_xprtrdma_cb_reply(rqst);
179655fec69SChuck Lever 	return 0;
18083128a60SChuck Lever }
18183128a60SChuck Lever 
18283128a60SChuck Lever /**
183cf73daf5SChuck Lever  * xprt_rdma_bc_send_reply - marshal and send a backchannel reply
184cf73daf5SChuck Lever  * @rqst: RPC rqst with a backchannel RPC reply in rq_snd_buf
185cf73daf5SChuck Lever  *
186cf73daf5SChuck Lever  * Caller holds the transport's write lock.
187cf73daf5SChuck Lever  *
188cf73daf5SChuck Lever  * Returns:
189cf73daf5SChuck Lever  *	%0 if the RPC message has been sent
190cf73daf5SChuck Lever  *	%-ENOTCONN if the caller should reconnect and call again
191cf73daf5SChuck Lever  *	%-EIO if a permanent error occurred and the request was not
192cf73daf5SChuck Lever  *		sent. Do not try to send this message again.
193cf73daf5SChuck Lever  */
194cf73daf5SChuck Lever int xprt_rdma_bc_send_reply(struct rpc_rqst *rqst)
195cf73daf5SChuck Lever {
196cf73daf5SChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
197cf73daf5SChuck Lever 	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
198cf73daf5SChuck Lever 	int rc;
199cf73daf5SChuck Lever 
200cf73daf5SChuck Lever 	if (!xprt_connected(rqst->rq_xprt))
201cf73daf5SChuck Lever 		goto drop_connection;
202cf73daf5SChuck Lever 
203*75891f50STrond Myklebust 	if (!xprt_request_get_cong(rqst->rq_xprt, rqst))
204*75891f50STrond Myklebust 		return -EBADSLT;
205*75891f50STrond Myklebust 
206cf73daf5SChuck Lever 	rc = rpcrdma_bc_marshal_reply(rqst);
207cf73daf5SChuck Lever 	if (rc < 0)
208cf73daf5SChuck Lever 		goto failed_marshal;
209cf73daf5SChuck Lever 
2107c8d9e7cSChuck Lever 	rpcrdma_post_recvs(r_xprt, true);
211cf73daf5SChuck Lever 	if (rpcrdma_ep_post(&r_xprt->rx_ia, &r_xprt->rx_ep, req))
212cf73daf5SChuck Lever 		goto drop_connection;
213cf73daf5SChuck Lever 	return 0;
214cf73daf5SChuck Lever 
215cf73daf5SChuck Lever failed_marshal:
216cf73daf5SChuck Lever 	if (rc != -ENOTCONN)
217cf73daf5SChuck Lever 		return rc;
218cf73daf5SChuck Lever drop_connection:
219cf73daf5SChuck Lever 	xprt_disconnect_done(rqst->rq_xprt);
220cf73daf5SChuck Lever 	return -ENOTCONN;
221cf73daf5SChuck Lever }
222cf73daf5SChuck Lever 
223cf73daf5SChuck Lever /**
224f531a5dbSChuck Lever  * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
225f531a5dbSChuck Lever  * @xprt: transport associated with these backchannel resources
226f531a5dbSChuck Lever  * @reqs: number of incoming requests to destroy; ignored
227f531a5dbSChuck Lever  */
228f531a5dbSChuck Lever void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
229f531a5dbSChuck Lever {
230f531a5dbSChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
231f531a5dbSChuck Lever 	struct rpc_rqst *rqst, *tmp;
232f531a5dbSChuck Lever 
233f531a5dbSChuck Lever 	spin_lock_bh(&xprt->bc_pa_lock);
234f531a5dbSChuck Lever 	list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
235f531a5dbSChuck Lever 		list_del(&rqst->rq_bc_pa_list);
236f531a5dbSChuck Lever 		spin_unlock_bh(&xprt->bc_pa_lock);
237f531a5dbSChuck Lever 
238f531a5dbSChuck Lever 		rpcrdma_bc_free_rqst(r_xprt, rqst);
239f531a5dbSChuck Lever 
240f531a5dbSChuck Lever 		spin_lock_bh(&xprt->bc_pa_lock);
241f531a5dbSChuck Lever 	}
242f531a5dbSChuck Lever 	spin_unlock_bh(&xprt->bc_pa_lock);
243f531a5dbSChuck Lever }
244f531a5dbSChuck Lever 
245f531a5dbSChuck Lever /**
246f531a5dbSChuck Lever  * xprt_rdma_bc_free_rqst - Release a backchannel rqst
247f531a5dbSChuck Lever  * @rqst: request to release
248f531a5dbSChuck Lever  */
249f531a5dbSChuck Lever void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
250f531a5dbSChuck Lever {
2517c8d9e7cSChuck Lever 	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
252f531a5dbSChuck Lever 	struct rpc_xprt *xprt = rqst->rq_xprt;
253f531a5dbSChuck Lever 
254c8bbe0c7SChuck Lever 	dprintk("RPC:       %s: freeing rqst %p (req %p)\n",
2557c8d9e7cSChuck Lever 		__func__, rqst, req);
2567c8d9e7cSChuck Lever 
2577c8d9e7cSChuck Lever 	rpcrdma_recv_buffer_put(req->rl_reply);
2587c8d9e7cSChuck Lever 	req->rl_reply = NULL;
259c8bbe0c7SChuck Lever 
260f531a5dbSChuck Lever 	spin_lock_bh(&xprt->bc_pa_lock);
261f531a5dbSChuck Lever 	list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
262f531a5dbSChuck Lever 	spin_unlock_bh(&xprt->bc_pa_lock);
263f531a5dbSChuck Lever }
26463cae470SChuck Lever 
26563cae470SChuck Lever /**
26663cae470SChuck Lever  * rpcrdma_bc_receive_call - Handle a backward direction call
2679ab6d89eSChuck Lever  * @r_xprt: transport receiving the call
26863cae470SChuck Lever  * @rep: receive buffer containing the call
26963cae470SChuck Lever  *
27063cae470SChuck Lever  * Operational assumptions:
27163cae470SChuck Lever  *    o Backchannel credits are ignored, just as the NFS server
27263cae470SChuck Lever  *      forechannel currently does
27363cae470SChuck Lever  *    o The ULP manages a replay cache (eg, NFSv4.1 sessions).
27463cae470SChuck Lever  *      No replay detection is done at the transport level
27563cae470SChuck Lever  */
27663cae470SChuck Lever void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
27763cae470SChuck Lever 			     struct rpcrdma_rep *rep)
27863cae470SChuck Lever {
27963cae470SChuck Lever 	struct rpc_xprt *xprt = &r_xprt->rx_xprt;
28063cae470SChuck Lever 	struct svc_serv *bc_serv;
28163cae470SChuck Lever 	struct rpcrdma_req *req;
28263cae470SChuck Lever 	struct rpc_rqst *rqst;
28363cae470SChuck Lever 	struct xdr_buf *buf;
28463cae470SChuck Lever 	size_t size;
28563cae470SChuck Lever 	__be32 *p;
28663cae470SChuck Lever 
28741c8f70fSChuck Lever 	p = xdr_inline_decode(&rep->rr_stream, 0);
28841c8f70fSChuck Lever 	size = xdr_stream_remaining(&rep->rr_stream);
28941c8f70fSChuck Lever 
29063cae470SChuck Lever #ifdef RPCRDMA_BACKCHANNEL_DEBUG
29163cae470SChuck Lever 	pr_info("RPC:       %s: callback XID %08x, length=%u\n",
29241c8f70fSChuck Lever 		__func__, be32_to_cpup(p), size);
29341c8f70fSChuck Lever 	pr_info("RPC:       %s: %*ph\n", __func__, size, p);
29463cae470SChuck Lever #endif
29563cae470SChuck Lever 
29663cae470SChuck Lever 	/* Grab a free bc rqst */
29763cae470SChuck Lever 	spin_lock(&xprt->bc_pa_lock);
29863cae470SChuck Lever 	if (list_empty(&xprt->bc_pa_list)) {
29963cae470SChuck Lever 		spin_unlock(&xprt->bc_pa_lock);
30063cae470SChuck Lever 		goto out_overflow;
30163cae470SChuck Lever 	}
30263cae470SChuck Lever 	rqst = list_first_entry(&xprt->bc_pa_list,
30363cae470SChuck Lever 				struct rpc_rqst, rq_bc_pa_list);
30463cae470SChuck Lever 	list_del(&rqst->rq_bc_pa_list);
30563cae470SChuck Lever 	spin_unlock(&xprt->bc_pa_lock);
30663cae470SChuck Lever 
30763cae470SChuck Lever 	/* Prepare rqst */
30863cae470SChuck Lever 	rqst->rq_reply_bytes_recvd = 0;
30963cae470SChuck Lever 	rqst->rq_bytes_sent = 0;
31041c8f70fSChuck Lever 	rqst->rq_xid = *p;
3119f74660bSChuck Lever 
3129f74660bSChuck Lever 	rqst->rq_private_buf.len = size;
31363cae470SChuck Lever 
31463cae470SChuck Lever 	buf = &rqst->rq_rcv_buf;
31563cae470SChuck Lever 	memset(buf, 0, sizeof(*buf));
31663cae470SChuck Lever 	buf->head[0].iov_base = p;
31763cae470SChuck Lever 	buf->head[0].iov_len = size;
31863cae470SChuck Lever 	buf->len = size;
31963cae470SChuck Lever 
32063cae470SChuck Lever 	/* The receive buffer has to be hooked to the rpcrdma_req
32141c8f70fSChuck Lever 	 * so that it is not released while the req is pointing
32241c8f70fSChuck Lever 	 * to its buffer, and so that it can be reposted after
32341c8f70fSChuck Lever 	 * the Upper Layer is done decoding it.
32463cae470SChuck Lever 	 */
32563cae470SChuck Lever 	req = rpcr_to_rdmar(rqst);
32663cae470SChuck Lever 	req->rl_reply = rep;
327fc1eb807SChuck Lever 	trace_xprtrdma_cb_call(rqst);
32863cae470SChuck Lever 
32963cae470SChuck Lever 	/* Queue rqst for ULP's callback service */
33063cae470SChuck Lever 	bc_serv = xprt->bc_serv;
33163cae470SChuck Lever 	spin_lock(&bc_serv->sv_cb_lock);
33263cae470SChuck Lever 	list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
33363cae470SChuck Lever 	spin_unlock(&bc_serv->sv_cb_lock);
33463cae470SChuck Lever 
33563cae470SChuck Lever 	wake_up(&bc_serv->sv_cb_waitq);
33663cae470SChuck Lever 
33763cae470SChuck Lever 	r_xprt->rx_stats.bcall_count++;
33863cae470SChuck Lever 	return;
33963cae470SChuck Lever 
34063cae470SChuck Lever out_overflow:
34163cae470SChuck Lever 	pr_warn("RPC/RDMA backchannel overflow\n");
34263cae470SChuck Lever 	xprt_disconnect_done(xprt);
34363cae470SChuck Lever 	/* This receive buffer gets reposted automatically
34463cae470SChuck Lever 	 * when the connection is re-established.
34563cae470SChuck Lever 	 */
34663cae470SChuck Lever 	return;
34763cae470SChuck Lever }
348