xref: /OK3568_Linux_fs/kernel/net/sunrpc/xprtrdma/svc_rdma_backchannel.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2015-2018 Oracle.  All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Support for backward direction RPCs on RPC/RDMA (server-side).
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/sunrpc/svc_rdma.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include "xprt_rdma.h"
11*4882a593Smuzhiyun #include <trace/events/rpcrdma.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun /**
14*4882a593Smuzhiyun  * svc_rdma_handle_bc_reply - Process incoming backchannel Reply
15*4882a593Smuzhiyun  * @rqstp: resources for handling the Reply
16*4882a593Smuzhiyun  * @rctxt: Received message
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  */
svc_rdma_handle_bc_reply(struct svc_rqst * rqstp,struct svc_rdma_recv_ctxt * rctxt)19*4882a593Smuzhiyun void svc_rdma_handle_bc_reply(struct svc_rqst *rqstp,
20*4882a593Smuzhiyun 			      struct svc_rdma_recv_ctxt *rctxt)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun 	struct svc_xprt *sxprt = rqstp->rq_xprt;
23*4882a593Smuzhiyun 	struct rpc_xprt *xprt = sxprt->xpt_bc_xprt;
24*4882a593Smuzhiyun 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
25*4882a593Smuzhiyun 	struct xdr_buf *rcvbuf = &rqstp->rq_arg;
26*4882a593Smuzhiyun 	struct kvec *dst, *src = &rcvbuf->head[0];
27*4882a593Smuzhiyun 	__be32 *rdma_resp = rctxt->rc_recv_buf;
28*4882a593Smuzhiyun 	struct rpc_rqst *req;
29*4882a593Smuzhiyun 	u32 credits;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	spin_lock(&xprt->queue_lock);
32*4882a593Smuzhiyun 	req = xprt_lookup_rqst(xprt, *rdma_resp);
33*4882a593Smuzhiyun 	if (!req)
34*4882a593Smuzhiyun 		goto out_unlock;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	dst = &req->rq_private_buf.head[0];
37*4882a593Smuzhiyun 	memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
38*4882a593Smuzhiyun 	if (dst->iov_len < src->iov_len)
39*4882a593Smuzhiyun 		goto out_unlock;
40*4882a593Smuzhiyun 	memcpy(dst->iov_base, src->iov_base, src->iov_len);
41*4882a593Smuzhiyun 	xprt_pin_rqst(req);
42*4882a593Smuzhiyun 	spin_unlock(&xprt->queue_lock);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	credits = be32_to_cpup(rdma_resp + 2);
45*4882a593Smuzhiyun 	if (credits == 0)
46*4882a593Smuzhiyun 		credits = 1;	/* don't deadlock */
47*4882a593Smuzhiyun 	else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
48*4882a593Smuzhiyun 		credits = r_xprt->rx_buf.rb_bc_max_requests;
49*4882a593Smuzhiyun 	spin_lock(&xprt->transport_lock);
50*4882a593Smuzhiyun 	xprt->cwnd = credits << RPC_CWNDSHIFT;
51*4882a593Smuzhiyun 	spin_unlock(&xprt->transport_lock);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	spin_lock(&xprt->queue_lock);
54*4882a593Smuzhiyun 	xprt_complete_rqst(req->rq_task, rcvbuf->len);
55*4882a593Smuzhiyun 	xprt_unpin_rqst(req);
56*4882a593Smuzhiyun 	rcvbuf->len = 0;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun out_unlock:
59*4882a593Smuzhiyun 	spin_unlock(&xprt->queue_lock);
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /* Send a backwards direction RPC call.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * Caller holds the connection's mutex and has already marshaled
65*4882a593Smuzhiyun  * the RPC/RDMA request.
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * This is similar to svc_rdma_send_reply_msg, but takes a struct
68*4882a593Smuzhiyun  * rpc_rqst instead, does not support chunks, and avoids blocking
69*4882a593Smuzhiyun  * memory allocation.
70*4882a593Smuzhiyun  *
71*4882a593Smuzhiyun  * XXX: There is still an opportunity to block in svc_rdma_send()
72*4882a593Smuzhiyun  * if there are no SQ entries to post the Send. This may occur if
73*4882a593Smuzhiyun  * the adapter has a small maximum SQ depth.
74*4882a593Smuzhiyun  */
svc_rdma_bc_sendto(struct svcxprt_rdma * rdma,struct rpc_rqst * rqst,struct svc_rdma_send_ctxt * ctxt)75*4882a593Smuzhiyun static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
76*4882a593Smuzhiyun 			      struct rpc_rqst *rqst,
77*4882a593Smuzhiyun 			      struct svc_rdma_send_ctxt *ctxt)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	int ret;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	ret = svc_rdma_map_reply_msg(rdma, ctxt, NULL, &rqst->rq_snd_buf);
82*4882a593Smuzhiyun 	if (ret < 0)
83*4882a593Smuzhiyun 		return -EIO;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	/* Bump page refcnt so Send completion doesn't release
86*4882a593Smuzhiyun 	 * the rq_buffer before all retransmits are complete.
87*4882a593Smuzhiyun 	 */
88*4882a593Smuzhiyun 	get_page(virt_to_page(rqst->rq_buffer));
89*4882a593Smuzhiyun 	ctxt->sc_send_wr.opcode = IB_WR_SEND;
90*4882a593Smuzhiyun 	return svc_rdma_send(rdma, ctxt);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /* Server-side transport endpoint wants a whole page for its send
94*4882a593Smuzhiyun  * buffer. The client RPC code constructs the RPC header in this
95*4882a593Smuzhiyun  * buffer before it invokes ->send_request.
96*4882a593Smuzhiyun  */
97*4882a593Smuzhiyun static int
xprt_rdma_bc_allocate(struct rpc_task * task)98*4882a593Smuzhiyun xprt_rdma_bc_allocate(struct rpc_task *task)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	struct rpc_rqst *rqst = task->tk_rqstp;
101*4882a593Smuzhiyun 	size_t size = rqst->rq_callsize;
102*4882a593Smuzhiyun 	struct page *page;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	if (size > PAGE_SIZE) {
105*4882a593Smuzhiyun 		WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
106*4882a593Smuzhiyun 			  size);
107*4882a593Smuzhiyun 		return -EINVAL;
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	page = alloc_page(RPCRDMA_DEF_GFP);
111*4882a593Smuzhiyun 	if (!page)
112*4882a593Smuzhiyun 		return -ENOMEM;
113*4882a593Smuzhiyun 	rqst->rq_buffer = page_address(page);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
116*4882a593Smuzhiyun 	if (!rqst->rq_rbuffer) {
117*4882a593Smuzhiyun 		put_page(page);
118*4882a593Smuzhiyun 		return -ENOMEM;
119*4882a593Smuzhiyun 	}
120*4882a593Smuzhiyun 	return 0;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun static void
xprt_rdma_bc_free(struct rpc_task * task)124*4882a593Smuzhiyun xprt_rdma_bc_free(struct rpc_task *task)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	struct rpc_rqst *rqst = task->tk_rqstp;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	put_page(virt_to_page(rqst->rq_buffer));
129*4882a593Smuzhiyun 	kfree(rqst->rq_rbuffer);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static int
rpcrdma_bc_send_request(struct svcxprt_rdma * rdma,struct rpc_rqst * rqst)133*4882a593Smuzhiyun rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	struct rpc_xprt *xprt = rqst->rq_xprt;
136*4882a593Smuzhiyun 	struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
137*4882a593Smuzhiyun 	struct svc_rdma_send_ctxt *ctxt;
138*4882a593Smuzhiyun 	__be32 *p;
139*4882a593Smuzhiyun 	int rc;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	ctxt = svc_rdma_send_ctxt_get(rdma);
142*4882a593Smuzhiyun 	if (!ctxt)
143*4882a593Smuzhiyun 		goto drop_connection;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	p = xdr_reserve_space(&ctxt->sc_stream, RPCRDMA_HDRLEN_MIN);
146*4882a593Smuzhiyun 	if (!p)
147*4882a593Smuzhiyun 		goto put_ctxt;
148*4882a593Smuzhiyun 	*p++ = rqst->rq_xid;
149*4882a593Smuzhiyun 	*p++ = rpcrdma_version;
150*4882a593Smuzhiyun 	*p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
151*4882a593Smuzhiyun 	*p++ = rdma_msg;
152*4882a593Smuzhiyun 	*p++ = xdr_zero;
153*4882a593Smuzhiyun 	*p++ = xdr_zero;
154*4882a593Smuzhiyun 	*p   = xdr_zero;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	rqst->rq_xtime = ktime_get();
157*4882a593Smuzhiyun 	rc = svc_rdma_bc_sendto(rdma, rqst, ctxt);
158*4882a593Smuzhiyun 	if (rc)
159*4882a593Smuzhiyun 		goto put_ctxt;
160*4882a593Smuzhiyun 	return 0;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun put_ctxt:
163*4882a593Smuzhiyun 	svc_rdma_send_ctxt_put(rdma, ctxt);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun drop_connection:
166*4882a593Smuzhiyun 	return -ENOTCONN;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun /**
170*4882a593Smuzhiyun  * xprt_rdma_bc_send_request - Send a reverse-direction Call
171*4882a593Smuzhiyun  * @rqst: rpc_rqst containing Call message to be sent
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * Return values:
174*4882a593Smuzhiyun  *   %0 if the message was sent successfully
175*4882a593Smuzhiyun  *   %ENOTCONN if the message was not sent
176*4882a593Smuzhiyun  */
xprt_rdma_bc_send_request(struct rpc_rqst * rqst)177*4882a593Smuzhiyun static int xprt_rdma_bc_send_request(struct rpc_rqst *rqst)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
180*4882a593Smuzhiyun 	struct svcxprt_rdma *rdma =
181*4882a593Smuzhiyun 		container_of(sxprt, struct svcxprt_rdma, sc_xprt);
182*4882a593Smuzhiyun 	int ret;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	if (test_bit(XPT_DEAD, &sxprt->xpt_flags))
185*4882a593Smuzhiyun 		return -ENOTCONN;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	ret = rpcrdma_bc_send_request(rdma, rqst);
188*4882a593Smuzhiyun 	if (ret == -ENOTCONN)
189*4882a593Smuzhiyun 		svc_close_xprt(sxprt);
190*4882a593Smuzhiyun 	return ret;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun static void
xprt_rdma_bc_close(struct rpc_xprt * xprt)194*4882a593Smuzhiyun xprt_rdma_bc_close(struct rpc_xprt *xprt)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun 	xprt_disconnect_done(xprt);
197*4882a593Smuzhiyun 	xprt->cwnd = RPC_CWNDSHIFT;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun static void
xprt_rdma_bc_put(struct rpc_xprt * xprt)201*4882a593Smuzhiyun xprt_rdma_bc_put(struct rpc_xprt *xprt)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	xprt_rdma_free_addresses(xprt);
204*4882a593Smuzhiyun 	xprt_free(xprt);
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
208*4882a593Smuzhiyun 	.reserve_xprt		= xprt_reserve_xprt_cong,
209*4882a593Smuzhiyun 	.release_xprt		= xprt_release_xprt_cong,
210*4882a593Smuzhiyun 	.alloc_slot		= xprt_alloc_slot,
211*4882a593Smuzhiyun 	.free_slot		= xprt_free_slot,
212*4882a593Smuzhiyun 	.release_request	= xprt_release_rqst_cong,
213*4882a593Smuzhiyun 	.buf_alloc		= xprt_rdma_bc_allocate,
214*4882a593Smuzhiyun 	.buf_free		= xprt_rdma_bc_free,
215*4882a593Smuzhiyun 	.send_request		= xprt_rdma_bc_send_request,
216*4882a593Smuzhiyun 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
217*4882a593Smuzhiyun 	.close			= xprt_rdma_bc_close,
218*4882a593Smuzhiyun 	.destroy		= xprt_rdma_bc_put,
219*4882a593Smuzhiyun 	.print_stats		= xprt_rdma_print_stats
220*4882a593Smuzhiyun };
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun static const struct rpc_timeout xprt_rdma_bc_timeout = {
223*4882a593Smuzhiyun 	.to_initval = 60 * HZ,
224*4882a593Smuzhiyun 	.to_maxval = 60 * HZ,
225*4882a593Smuzhiyun };
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /* It shouldn't matter if the number of backchannel session slots
228*4882a593Smuzhiyun  * doesn't match the number of RPC/RDMA credits. That just means
229*4882a593Smuzhiyun  * one or the other will have extra slots that aren't used.
230*4882a593Smuzhiyun  */
231*4882a593Smuzhiyun static struct rpc_xprt *
xprt_setup_rdma_bc(struct xprt_create * args)232*4882a593Smuzhiyun xprt_setup_rdma_bc(struct xprt_create *args)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	struct rpc_xprt *xprt;
235*4882a593Smuzhiyun 	struct rpcrdma_xprt *new_xprt;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	if (args->addrlen > sizeof(xprt->addr))
238*4882a593Smuzhiyun 		return ERR_PTR(-EBADF);
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 	xprt = xprt_alloc(args->net, sizeof(*new_xprt),
241*4882a593Smuzhiyun 			  RPCRDMA_MAX_BC_REQUESTS,
242*4882a593Smuzhiyun 			  RPCRDMA_MAX_BC_REQUESTS);
243*4882a593Smuzhiyun 	if (!xprt)
244*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	xprt->timeout = &xprt_rdma_bc_timeout;
247*4882a593Smuzhiyun 	xprt_set_bound(xprt);
248*4882a593Smuzhiyun 	xprt_set_connected(xprt);
249*4882a593Smuzhiyun 	xprt->bind_timeout = 0;
250*4882a593Smuzhiyun 	xprt->reestablish_timeout = 0;
251*4882a593Smuzhiyun 	xprt->idle_timeout = 0;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	xprt->prot = XPRT_TRANSPORT_BC_RDMA;
254*4882a593Smuzhiyun 	xprt->ops = &xprt_rdma_bc_procs;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
257*4882a593Smuzhiyun 	xprt->addrlen = args->addrlen;
258*4882a593Smuzhiyun 	xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
259*4882a593Smuzhiyun 	xprt->resvport = 0;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	xprt->max_payload = xprt_rdma_max_inline_read;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	new_xprt = rpcx_to_rdmax(xprt);
264*4882a593Smuzhiyun 	new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	xprt_get(xprt);
267*4882a593Smuzhiyun 	args->bc_xprt->xpt_bc_xprt = xprt;
268*4882a593Smuzhiyun 	xprt->bc_xprt = args->bc_xprt;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* Final put for backchannel xprt is in __svc_rdma_free */
271*4882a593Smuzhiyun 	xprt_get(xprt);
272*4882a593Smuzhiyun 	return xprt;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun struct xprt_class xprt_rdma_bc = {
276*4882a593Smuzhiyun 	.list			= LIST_HEAD_INIT(xprt_rdma_bc.list),
277*4882a593Smuzhiyun 	.name			= "rdma backchannel",
278*4882a593Smuzhiyun 	.owner			= THIS_MODULE,
279*4882a593Smuzhiyun 	.ident			= XPRT_TRANSPORT_BC_RDMA,
280*4882a593Smuzhiyun 	.setup			= xprt_setup_rdma_bc,
281*4882a593Smuzhiyun };
282