xref: /linux/net/sunrpc/xprtrdma/backchannel.c (revision e28ce90083f032ca0e8ea03478f5b6a38f5930f7)
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 
863cae470SChuck Lever #include <linux/sunrpc/xprt.h>
963cae470SChuck Lever #include <linux/sunrpc/svc.h>
1076566773SChuck Lever #include <linux/sunrpc/svc_xprt.h>
11bd2abef3SChuck Lever #include <linux/sunrpc/svc_rdma.h>
12f531a5dbSChuck Lever 
13f531a5dbSChuck Lever #include "xprt_rdma.h"
14b6e717cbSChuck Lever #include <trace/events/rpcrdma.h>
15f531a5dbSChuck Lever 
16f531a5dbSChuck Lever #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
17f531a5dbSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
18f531a5dbSChuck Lever #endif
19f531a5dbSChuck Lever 
20c8bbe0c7SChuck Lever #undef RPCRDMA_BACKCHANNEL_DEBUG
2163cae470SChuck Lever 
22f531a5dbSChuck Lever /**
23f531a5dbSChuck Lever  * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests
24f531a5dbSChuck Lever  * @xprt: transport associated with these backchannel resources
25f531a5dbSChuck Lever  * @reqs: number of concurrent incoming requests to expect
26f531a5dbSChuck Lever  *
27f531a5dbSChuck Lever  * Returns 0 on success; otherwise a negative errno
28f531a5dbSChuck Lever  */
29f531a5dbSChuck Lever int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs)
30f531a5dbSChuck Lever {
31f531a5dbSChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
32f531a5dbSChuck Lever 
333f9c7e76SChuck Lever 	r_xprt->rx_buf.rb_bc_srv_max_requests = RPCRDMA_BACKWARD_WRS >> 1;
34fc1eb807SChuck Lever 	trace_xprtrdma_cb_setup(r_xprt, reqs);
35f531a5dbSChuck Lever 	return 0;
36f531a5dbSChuck Lever }
37f531a5dbSChuck Lever 
38f531a5dbSChuck Lever /**
396b26cc8cSChuck Lever  * xprt_rdma_bc_maxpayload - Return maximum backchannel message size
406b26cc8cSChuck Lever  * @xprt: transport
416b26cc8cSChuck Lever  *
426b26cc8cSChuck Lever  * Returns maximum size, in bytes, of a backchannel message
436b26cc8cSChuck Lever  */
446b26cc8cSChuck Lever size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt)
456b26cc8cSChuck Lever {
466b26cc8cSChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
47*e28ce900SChuck Lever 	struct rpcrdma_ep *ep = r_xprt->rx_ep;
486b26cc8cSChuck Lever 	size_t maxmsg;
496b26cc8cSChuck Lever 
5093aa8e0aSChuck Lever 	maxmsg = min_t(unsigned int, ep->re_inline_send, ep->re_inline_recv);
5162aee0e3SChuck Lever 	maxmsg = min_t(unsigned int, maxmsg, PAGE_SIZE);
526b26cc8cSChuck Lever 	return maxmsg - RPCRDMA_HDRLEN_MIN;
536b26cc8cSChuck Lever }
546b26cc8cSChuck Lever 
557402a4feSTrond Myklebust unsigned int xprt_rdma_bc_max_slots(struct rpc_xprt *xprt)
567402a4feSTrond Myklebust {
5717d47f93SChuck Lever 	return RPCRDMA_BACKWARD_WRS >> 1;
587402a4feSTrond Myklebust }
597402a4feSTrond Myklebust 
60cf73daf5SChuck Lever static int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst)
6183128a60SChuck Lever {
627ec910e7SChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
6383128a60SChuck Lever 	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
647ec910e7SChuck Lever 	__be32 *p;
6583128a60SChuck Lever 
667ec910e7SChuck Lever 	rpcrdma_set_xdrlen(&req->rl_hdrbuf, 0);
677ec910e7SChuck Lever 	xdr_init_encode(&req->rl_stream, &req->rl_hdrbuf,
688cec3dbaSChuck Lever 			rdmab_data(req->rl_rdmabuf), rqst);
697ec910e7SChuck Lever 
707ec910e7SChuck Lever 	p = xdr_reserve_space(&req->rl_stream, 28);
717ec910e7SChuck Lever 	if (unlikely(!p))
727ec910e7SChuck Lever 		return -EIO;
737ec910e7SChuck Lever 	*p++ = rqst->rq_xid;
747ec910e7SChuck Lever 	*p++ = rpcrdma_version;
757ec910e7SChuck Lever 	*p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests);
767ec910e7SChuck Lever 	*p++ = rdma_msg;
777ec910e7SChuck Lever 	*p++ = xdr_zero;
787ec910e7SChuck Lever 	*p++ = xdr_zero;
797ec910e7SChuck Lever 	*p = xdr_zero;
8083128a60SChuck Lever 
81857f9acaSChuck Lever 	if (rpcrdma_prepare_send_sges(r_xprt, req, RPCRDMA_HDRLEN_MIN,
82614f3c96SChuck Lever 				      &rqst->rq_snd_buf, rpcrdma_noch_pullup))
8354cbd6b0SChuck Lever 		return -EIO;
84fc1eb807SChuck Lever 
85fc1eb807SChuck Lever 	trace_xprtrdma_cb_reply(rqst);
86655fec69SChuck Lever 	return 0;
8783128a60SChuck Lever }
8883128a60SChuck Lever 
8983128a60SChuck Lever /**
90cf73daf5SChuck Lever  * xprt_rdma_bc_send_reply - marshal and send a backchannel reply
91cf73daf5SChuck Lever  * @rqst: RPC rqst with a backchannel RPC reply in rq_snd_buf
92cf73daf5SChuck Lever  *
93cf73daf5SChuck Lever  * Caller holds the transport's write lock.
94cf73daf5SChuck Lever  *
95cf73daf5SChuck Lever  * Returns:
96cf73daf5SChuck Lever  *	%0 if the RPC message has been sent
97cf73daf5SChuck Lever  *	%-ENOTCONN if the caller should reconnect and call again
98cf73daf5SChuck Lever  *	%-EIO if a permanent error occurred and the request was not
99cf73daf5SChuck Lever  *		sent. Do not try to send this message again.
100cf73daf5SChuck Lever  */
101cf73daf5SChuck Lever int xprt_rdma_bc_send_reply(struct rpc_rqst *rqst)
102cf73daf5SChuck Lever {
1030c0829bcSChuck Lever 	struct rpc_xprt *xprt = rqst->rq_xprt;
1040c0829bcSChuck Lever 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
105cf73daf5SChuck Lever 	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
106cf73daf5SChuck Lever 	int rc;
107cf73daf5SChuck Lever 
1080c0829bcSChuck Lever 	if (!xprt_connected(xprt))
1090c0829bcSChuck Lever 		return -ENOTCONN;
110cf73daf5SChuck Lever 
1110c0829bcSChuck Lever 	if (!xprt_request_get_cong(xprt, rqst))
11275891f50STrond Myklebust 		return -EBADSLT;
11375891f50STrond Myklebust 
114cf73daf5SChuck Lever 	rc = rpcrdma_bc_marshal_reply(rqst);
115cf73daf5SChuck Lever 	if (rc < 0)
116cf73daf5SChuck Lever 		goto failed_marshal;
117cf73daf5SChuck Lever 
11897d0de88SChuck Lever 	if (rpcrdma_post_sends(r_xprt, req))
119cf73daf5SChuck Lever 		goto drop_connection;
120cf73daf5SChuck Lever 	return 0;
121cf73daf5SChuck Lever 
122cf73daf5SChuck Lever failed_marshal:
123cf73daf5SChuck Lever 	if (rc != -ENOTCONN)
124cf73daf5SChuck Lever 		return rc;
125cf73daf5SChuck Lever drop_connection:
1260c0829bcSChuck Lever 	xprt_rdma_close(xprt);
127cf73daf5SChuck Lever 	return -ENOTCONN;
128cf73daf5SChuck Lever }
129cf73daf5SChuck Lever 
130cf73daf5SChuck Lever /**
131f531a5dbSChuck Lever  * xprt_rdma_bc_destroy - Release resources for handling backchannel requests
132f531a5dbSChuck Lever  * @xprt: transport associated with these backchannel resources
133f531a5dbSChuck Lever  * @reqs: number of incoming requests to destroy; ignored
134f531a5dbSChuck Lever  */
135f531a5dbSChuck Lever void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs)
136f531a5dbSChuck Lever {
137f531a5dbSChuck Lever 	struct rpc_rqst *rqst, *tmp;
138f531a5dbSChuck Lever 
139f7d46681SChuck Lever 	spin_lock(&xprt->bc_pa_lock);
140f531a5dbSChuck Lever 	list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) {
141f531a5dbSChuck Lever 		list_del(&rqst->rq_bc_pa_list);
142f7d46681SChuck Lever 		spin_unlock(&xprt->bc_pa_lock);
143f531a5dbSChuck Lever 
14492f4433eSChuck Lever 		rpcrdma_req_destroy(rpcr_to_rdmar(rqst));
145f531a5dbSChuck Lever 
146f7d46681SChuck Lever 		spin_lock(&xprt->bc_pa_lock);
147f531a5dbSChuck Lever 	}
148f7d46681SChuck Lever 	spin_unlock(&xprt->bc_pa_lock);
149f531a5dbSChuck Lever }
150f531a5dbSChuck Lever 
151f531a5dbSChuck Lever /**
152f531a5dbSChuck Lever  * xprt_rdma_bc_free_rqst - Release a backchannel rqst
153f531a5dbSChuck Lever  * @rqst: request to release
154f531a5dbSChuck Lever  */
155f531a5dbSChuck Lever void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst)
156f531a5dbSChuck Lever {
1577c8d9e7cSChuck Lever 	struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
158f531a5dbSChuck Lever 	struct rpc_xprt *xprt = rqst->rq_xprt;
159f531a5dbSChuck Lever 
1607c8d9e7cSChuck Lever 	rpcrdma_recv_buffer_put(req->rl_reply);
1617c8d9e7cSChuck Lever 	req->rl_reply = NULL;
162c8bbe0c7SChuck Lever 
163f7d46681SChuck Lever 	spin_lock(&xprt->bc_pa_lock);
164f531a5dbSChuck Lever 	list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list);
165f7d46681SChuck Lever 	spin_unlock(&xprt->bc_pa_lock);
1669edb455eSTrond Myklebust 	xprt_put(xprt);
167f531a5dbSChuck Lever }
16863cae470SChuck Lever 
1693f9c7e76SChuck Lever static struct rpc_rqst *rpcrdma_bc_rqst_get(struct rpcrdma_xprt *r_xprt)
1703f9c7e76SChuck Lever {
1713f9c7e76SChuck Lever 	struct rpc_xprt *xprt = &r_xprt->rx_xprt;
1723f9c7e76SChuck Lever 	struct rpcrdma_req *req;
1733f9c7e76SChuck Lever 	struct rpc_rqst *rqst;
1743f9c7e76SChuck Lever 	size_t size;
1753f9c7e76SChuck Lever 
1763f9c7e76SChuck Lever 	spin_lock(&xprt->bc_pa_lock);
1773f9c7e76SChuck Lever 	rqst = list_first_entry_or_null(&xprt->bc_pa_list, struct rpc_rqst,
1783f9c7e76SChuck Lever 					rq_bc_pa_list);
1793f9c7e76SChuck Lever 	if (!rqst)
1803f9c7e76SChuck Lever 		goto create_req;
1813f9c7e76SChuck Lever 	list_del(&rqst->rq_bc_pa_list);
1823f9c7e76SChuck Lever 	spin_unlock(&xprt->bc_pa_lock);
1833f9c7e76SChuck Lever 	return rqst;
1843f9c7e76SChuck Lever 
1853f9c7e76SChuck Lever create_req:
1863f9c7e76SChuck Lever 	spin_unlock(&xprt->bc_pa_lock);
1873f9c7e76SChuck Lever 
1883f9c7e76SChuck Lever 	/* Set a limit to prevent a remote from overrunning our resources.
1893f9c7e76SChuck Lever 	 */
1903f9c7e76SChuck Lever 	if (xprt->bc_alloc_count >= RPCRDMA_BACKWARD_WRS)
1913f9c7e76SChuck Lever 		return NULL;
1923f9c7e76SChuck Lever 
193*e28ce900SChuck Lever 	size = min_t(size_t, r_xprt->rx_ep->re_inline_recv, PAGE_SIZE);
1943f9c7e76SChuck Lever 	req = rpcrdma_req_create(r_xprt, size, GFP_KERNEL);
1953f9c7e76SChuck Lever 	if (!req)
1963f9c7e76SChuck Lever 		return NULL;
197b78de1dcSChuck Lever 	if (rpcrdma_req_setup(r_xprt, req)) {
198b78de1dcSChuck Lever 		rpcrdma_req_destroy(req);
199b78de1dcSChuck Lever 		return NULL;
200b78de1dcSChuck Lever 	}
2013f9c7e76SChuck Lever 
2023f9c7e76SChuck Lever 	xprt->bc_alloc_count++;
2033f9c7e76SChuck Lever 	rqst = &req->rl_slot;
2043f9c7e76SChuck Lever 	rqst->rq_xprt = xprt;
2053f9c7e76SChuck Lever 	__set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state);
2063f9c7e76SChuck Lever 	xdr_buf_init(&rqst->rq_snd_buf, rdmab_data(req->rl_sendbuf), size);
2073f9c7e76SChuck Lever 	return rqst;
2083f9c7e76SChuck Lever }
2093f9c7e76SChuck Lever 
21063cae470SChuck Lever /**
21163cae470SChuck Lever  * rpcrdma_bc_receive_call - Handle a backward direction call
2129ab6d89eSChuck Lever  * @r_xprt: transport receiving the call
21363cae470SChuck Lever  * @rep: receive buffer containing the call
21463cae470SChuck Lever  *
21563cae470SChuck Lever  * Operational assumptions:
21663cae470SChuck Lever  *    o Backchannel credits are ignored, just as the NFS server
21763cae470SChuck Lever  *      forechannel currently does
21863cae470SChuck Lever  *    o The ULP manages a replay cache (eg, NFSv4.1 sessions).
21963cae470SChuck Lever  *      No replay detection is done at the transport level
22063cae470SChuck Lever  */
22163cae470SChuck Lever void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt,
22263cae470SChuck Lever 			     struct rpcrdma_rep *rep)
22363cae470SChuck Lever {
22463cae470SChuck Lever 	struct rpc_xprt *xprt = &r_xprt->rx_xprt;
22563cae470SChuck Lever 	struct svc_serv *bc_serv;
22663cae470SChuck Lever 	struct rpcrdma_req *req;
22763cae470SChuck Lever 	struct rpc_rqst *rqst;
22863cae470SChuck Lever 	struct xdr_buf *buf;
22963cae470SChuck Lever 	size_t size;
23063cae470SChuck Lever 	__be32 *p;
23163cae470SChuck Lever 
23241c8f70fSChuck Lever 	p = xdr_inline_decode(&rep->rr_stream, 0);
23341c8f70fSChuck Lever 	size = xdr_stream_remaining(&rep->rr_stream);
23441c8f70fSChuck Lever 
23563cae470SChuck Lever #ifdef RPCRDMA_BACKCHANNEL_DEBUG
23663cae470SChuck Lever 	pr_info("RPC:       %s: callback XID %08x, length=%u\n",
23741c8f70fSChuck Lever 		__func__, be32_to_cpup(p), size);
23841c8f70fSChuck Lever 	pr_info("RPC:       %s: %*ph\n", __func__, size, p);
23963cae470SChuck Lever #endif
24063cae470SChuck Lever 
2413f9c7e76SChuck Lever 	rqst = rpcrdma_bc_rqst_get(r_xprt);
2423f9c7e76SChuck Lever 	if (!rqst)
24363cae470SChuck Lever 		goto out_overflow;
24463cae470SChuck Lever 
24563cae470SChuck Lever 	rqst->rq_reply_bytes_recvd = 0;
24641c8f70fSChuck Lever 	rqst->rq_xid = *p;
2479f74660bSChuck Lever 
2489f74660bSChuck Lever 	rqst->rq_private_buf.len = size;
24963cae470SChuck Lever 
25063cae470SChuck Lever 	buf = &rqst->rq_rcv_buf;
25163cae470SChuck Lever 	memset(buf, 0, sizeof(*buf));
25263cae470SChuck Lever 	buf->head[0].iov_base = p;
25363cae470SChuck Lever 	buf->head[0].iov_len = size;
25463cae470SChuck Lever 	buf->len = size;
25563cae470SChuck Lever 
25663cae470SChuck Lever 	/* The receive buffer has to be hooked to the rpcrdma_req
25741c8f70fSChuck Lever 	 * so that it is not released while the req is pointing
25841c8f70fSChuck Lever 	 * to its buffer, and so that it can be reposted after
25941c8f70fSChuck Lever 	 * the Upper Layer is done decoding it.
26063cae470SChuck Lever 	 */
26163cae470SChuck Lever 	req = rpcr_to_rdmar(rqst);
26263cae470SChuck Lever 	req->rl_reply = rep;
263fc1eb807SChuck Lever 	trace_xprtrdma_cb_call(rqst);
26463cae470SChuck Lever 
26563cae470SChuck Lever 	/* Queue rqst for ULP's callback service */
26663cae470SChuck Lever 	bc_serv = xprt->bc_serv;
2679edb455eSTrond Myklebust 	xprt_get(xprt);
26863cae470SChuck Lever 	spin_lock(&bc_serv->sv_cb_lock);
26963cae470SChuck Lever 	list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list);
27063cae470SChuck Lever 	spin_unlock(&bc_serv->sv_cb_lock);
27163cae470SChuck Lever 
27263cae470SChuck Lever 	wake_up(&bc_serv->sv_cb_waitq);
27363cae470SChuck Lever 
27463cae470SChuck Lever 	r_xprt->rx_stats.bcall_count++;
27563cae470SChuck Lever 	return;
27663cae470SChuck Lever 
27763cae470SChuck Lever out_overflow:
27863cae470SChuck Lever 	pr_warn("RPC/RDMA backchannel overflow\n");
2790c0829bcSChuck Lever 	xprt_force_disconnect(xprt);
28063cae470SChuck Lever 	/* This receive buffer gets reposted automatically
28163cae470SChuck Lever 	 * when the connection is re-established.
28263cae470SChuck Lever 	 */
28363cae470SChuck Lever 	return;
28463cae470SChuck Lever }
285