blob: 642f0310cc1659cb3677b0045a3604d825b83f58 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Chuck Lever5d252f92016-01-07 14:50:10 -05002/*
Chuck Lever4201c7462018-05-07 15:28:04 -04003 * Copyright (c) 2015-2018 Oracle. All rights reserved.
Chuck Lever5d252f92016-01-07 14:50:10 -05004 *
5 * Support for backward direction RPCs on RPC/RDMA (server-side).
6 */
7
Russell Kingf39b2dde2017-02-07 15:02:54 -08008#include <linux/module.h>
Chuck Leverbd2abef2018-05-07 15:27:16 -04009
Chuck Lever5d252f92016-01-07 14:50:10 -050010#include <linux/sunrpc/svc_rdma.h>
Chuck Leverbd2abef2018-05-07 15:27:16 -040011
Chuck Lever5d252f92016-01-07 14:50:10 -050012#include "xprt_rdma.h"
Chuck Leverbd2abef2018-05-07 15:27:16 -040013#include <trace/events/rpcrdma.h>
Chuck Lever5d252f92016-01-07 14:50:10 -050014
15#define RPCDBG_FACILITY RPCDBG_SVCXPRT
16
17#undef SVCRDMA_BACKCHANNEL_DEBUG
18
Chuck Leverf5821c72017-04-09 13:06:49 -040019/**
20 * svc_rdma_handle_bc_reply - Process incoming backchannel reply
21 * @xprt: controlling backchannel transport
22 * @rdma_resp: pointer to incoming transport header
23 * @rcvbuf: XDR buffer into which to decode the reply
24 *
25 * Returns:
26 * %0 if @rcvbuf is filled in, xprt_complete_rqst called,
27 * %-EAGAIN if server should call ->recvfrom again.
28 */
29int svc_rdma_handle_bc_reply(struct rpc_xprt *xprt, __be32 *rdma_resp,
Chuck Lever5d252f92016-01-07 14:50:10 -050030 struct xdr_buf *rcvbuf)
31{
32 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
33 struct kvec *dst, *src = &rcvbuf->head[0];
34 struct rpc_rqst *req;
Chuck Lever5d252f92016-01-07 14:50:10 -050035 u32 credits;
36 size_t len;
37 __be32 xid;
38 __be32 *p;
39 int ret;
40
41 p = (__be32 *)src->iov_base;
42 len = src->iov_len;
Chuck Leverf5821c72017-04-09 13:06:49 -040043 xid = *rdma_resp;
Chuck Lever5d252f92016-01-07 14:50:10 -050044
45#ifdef SVCRDMA_BACKCHANNEL_DEBUG
46 pr_info("%s: xid=%08x, length=%zu\n",
47 __func__, be32_to_cpu(xid), len);
48 pr_info("%s: RPC/RDMA: %*ph\n",
Chuck Leverf5821c72017-04-09 13:06:49 -040049 __func__, (int)RPCRDMA_HDRLEN_MIN, rdma_resp);
Chuck Lever5d252f92016-01-07 14:50:10 -050050 pr_info("%s: RPC: %*ph\n",
51 __func__, (int)len, p);
52#endif
53
54 ret = -EAGAIN;
55 if (src->iov_len < 24)
56 goto out_shortreply;
57
Trond Myklebustce7c2522017-08-16 15:30:35 -040058 spin_lock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050059 req = xprt_lookup_rqst(xprt, xid);
60 if (!req)
61 goto out_notfound;
62
63 dst = &req->rq_private_buf.head[0];
64 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
65 if (dst->iov_len < len)
66 goto out_unlock;
67 memcpy(dst->iov_base, p, len);
Chuck Lever596f2a12018-10-01 14:16:01 -040068 xprt_pin_rqst(req);
69 spin_unlock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050070
Chuck Leverf5821c72017-04-09 13:06:49 -040071 credits = be32_to_cpup(rdma_resp + 2);
Chuck Lever5d252f92016-01-07 14:50:10 -050072 if (credits == 0)
73 credits = 1; /* don't deadlock */
74 else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
75 credits = r_xprt->rx_buf.rb_bc_max_requests;
76
Trond Myklebustce7c2522017-08-16 15:30:35 -040077 spin_lock_bh(&xprt->transport_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050078 xprt->cwnd = credits << RPC_CWNDSHIFT;
Trond Myklebustce7c2522017-08-16 15:30:35 -040079 spin_unlock_bh(&xprt->transport_lock);
80
Chuck Lever596f2a12018-10-01 14:16:01 -040081 spin_lock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050082 ret = 0;
83 xprt_complete_rqst(req->rq_task, rcvbuf->len);
Chuck Lever596f2a12018-10-01 14:16:01 -040084 xprt_unpin_rqst(req);
Chuck Lever5d252f92016-01-07 14:50:10 -050085 rcvbuf->len = 0;
86
87out_unlock:
Trond Myklebustce7c2522017-08-16 15:30:35 -040088 spin_unlock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050089out:
90 return ret;
91
92out_shortreply:
93 dprintk("svcrdma: short bc reply: xprt=%p, len=%zu\n",
94 xprt, src->iov_len);
95 goto out;
96
97out_notfound:
98 dprintk("svcrdma: unrecognized bc reply: xprt=%p, xid=%08x\n",
99 xprt, be32_to_cpu(xid));
Chuck Lever5d252f92016-01-07 14:50:10 -0500100 goto out_unlock;
101}
102
103/* Send a backwards direction RPC call.
104 *
105 * Caller holds the connection's mutex and has already marshaled
106 * the RPC/RDMA request.
107 *
Chuck Lever9a6a1802017-04-09 13:06:25 -0400108 * This is similar to svc_rdma_send_reply_msg, but takes a struct
109 * rpc_rqst instead, does not support chunks, and avoids blocking
110 * memory allocation.
Chuck Lever5d252f92016-01-07 14:50:10 -0500111 *
112 * XXX: There is still an opportunity to block in svc_rdma_send()
113 * if there are no SQ entries to post the Send. This may occur if
114 * the adapter has a small maximum SQ depth.
115 */
116static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
Chuck Lever99722fe2018-05-07 15:28:25 -0400117 struct rpc_rqst *rqst,
118 struct svc_rdma_send_ctxt *ctxt)
Chuck Lever5d252f92016-01-07 14:50:10 -0500119{
Chuck Lever5d252f92016-01-07 14:50:10 -0500120 int ret;
121
Chuck Lever99722fe2018-05-07 15:28:25 -0400122 ret = svc_rdma_map_reply_msg(rdma, ctxt, &rqst->rq_snd_buf, NULL);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400123 if (ret < 0)
Chuck Lever99722fe2018-05-07 15:28:25 -0400124 return -EIO;
Chuck Lever5d252f92016-01-07 14:50:10 -0500125
Chuck Lever0bad47c2017-10-16 12:14:33 -0400126 /* Bump page refcnt so Send completion doesn't release
127 * the rq_buffer before all retransmits are complete.
128 */
129 get_page(virt_to_page(rqst->rq_buffer));
Chuck Lever986b7882018-05-07 15:28:15 -0400130 ctxt->sc_send_wr.opcode = IB_WR_SEND;
Chuck Lever99722fe2018-05-07 15:28:25 -0400131 return svc_rdma_send(rdma, &ctxt->sc_send_wr);
Chuck Lever5d252f92016-01-07 14:50:10 -0500132}
133
134/* Server-side transport endpoint wants a whole page for its send
135 * buffer. The client RPC code constructs the RPC header in this
136 * buffer before it invokes ->send_request.
Chuck Lever5d252f92016-01-07 14:50:10 -0500137 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400138static int
139xprt_rdma_bc_allocate(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500140{
141 struct rpc_rqst *rqst = task->tk_rqstp;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400142 size_t size = rqst->rq_callsize;
Chuck Lever5d252f92016-01-07 14:50:10 -0500143 struct page *page;
144
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400145 if (size > PAGE_SIZE) {
Chuck Lever5d252f92016-01-07 14:50:10 -0500146 WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
147 size);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400148 return -EINVAL;
149 }
Chuck Lever5d252f92016-01-07 14:50:10 -0500150
151 page = alloc_page(RPCRDMA_DEF_GFP);
152 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400153 return -ENOMEM;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400154 rqst->rq_buffer = page_address(page);
Chuck Lever8d426292016-10-28 22:22:33 -0400155
156 rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
157 if (!rqst->rq_rbuffer) {
158 put_page(page);
159 return -ENOMEM;
160 }
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400161 return 0;
Chuck Lever5d252f92016-01-07 14:50:10 -0500162}
163
164static void
Chuck Lever3435c742016-09-15 10:55:29 -0400165xprt_rdma_bc_free(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500166{
Chuck Lever8d426292016-10-28 22:22:33 -0400167 struct rpc_rqst *rqst = task->tk_rqstp;
168
Chuck Lever0bad47c2017-10-16 12:14:33 -0400169 put_page(virt_to_page(rqst->rq_buffer));
Chuck Lever8d426292016-10-28 22:22:33 -0400170 kfree(rqst->rq_rbuffer);
Chuck Lever5d252f92016-01-07 14:50:10 -0500171}
172
173static int
174rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
175{
176 struct rpc_xprt *xprt = rqst->rq_xprt;
177 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
Chuck Lever99722fe2018-05-07 15:28:25 -0400178 struct svc_rdma_send_ctxt *ctxt;
Chuck Leverc2ccf642017-02-07 11:58:40 -0500179 __be32 *p;
Chuck Lever5d252f92016-01-07 14:50:10 -0500180 int rc;
181
Chuck Lever99722fe2018-05-07 15:28:25 -0400182 ctxt = svc_rdma_send_ctxt_get(rdma);
183 if (!ctxt)
184 goto drop_connection;
185
186 p = ctxt->sc_xprt_buf;
Chuck Leverc2ccf642017-02-07 11:58:40 -0500187 *p++ = rqst->rq_xid;
188 *p++ = rpcrdma_version;
189 *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
190 *p++ = rdma_msg;
191 *p++ = xdr_zero;
192 *p++ = xdr_zero;
193 *p = xdr_zero;
Chuck Lever99722fe2018-05-07 15:28:25 -0400194 svc_rdma_sync_reply_hdr(rdma, ctxt, RPCRDMA_HDRLEN_MIN);
Chuck Lever5d252f92016-01-07 14:50:10 -0500195
196#ifdef SVCRDMA_BACKCHANNEL_DEBUG
197 pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer);
198#endif
199
Chuck Lever99722fe2018-05-07 15:28:25 -0400200 rc = svc_rdma_bc_sendto(rdma, rqst, ctxt);
201 if (rc) {
202 svc_rdma_send_ctxt_put(rdma, ctxt);
Chuck Lever5d252f92016-01-07 14:50:10 -0500203 goto drop_connection;
Chuck Lever99722fe2018-05-07 15:28:25 -0400204 }
Chuck Lever5d252f92016-01-07 14:50:10 -0500205 return rc;
206
207drop_connection:
208 dprintk("svcrdma: failed to send bc call\n");
209 xprt_disconnect_done(xprt);
210 return -ENOTCONN;
211}
212
213/* Send an RPC call on the passive end of a transport
214 * connection.
215 */
216static int
217xprt_rdma_bc_send_request(struct rpc_task *task)
218{
219 struct rpc_rqst *rqst = task->tk_rqstp;
220 struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
221 struct svcxprt_rdma *rdma;
222 int ret;
223
224 dprintk("svcrdma: sending bc call with xid: %08x\n",
225 be32_to_cpu(rqst->rq_xid));
226
227 if (!mutex_trylock(&sxprt->xpt_mutex)) {
228 rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL);
229 if (!mutex_trylock(&sxprt->xpt_mutex))
230 return -EAGAIN;
231 rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task);
232 }
233
234 ret = -ENOTCONN;
235 rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt);
236 if (!test_bit(XPT_DEAD, &sxprt->xpt_flags))
237 ret = rpcrdma_bc_send_request(rdma, rqst);
238
239 mutex_unlock(&sxprt->xpt_mutex);
240
241 if (ret < 0)
242 return ret;
243 return 0;
244}
245
246static void
247xprt_rdma_bc_close(struct rpc_xprt *xprt)
248{
249 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
250}
251
252static void
253xprt_rdma_bc_put(struct rpc_xprt *xprt)
254{
255 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
256
257 xprt_free(xprt);
258 module_put(THIS_MODULE);
259}
260
Chuck Leverd31ae252017-08-01 12:00:39 -0400261static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
Chuck Lever5d252f92016-01-07 14:50:10 -0500262 .reserve_xprt = xprt_reserve_xprt_cong,
263 .release_xprt = xprt_release_xprt_cong,
264 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -0400265 .free_slot = xprt_free_slot,
Chuck Lever5d252f92016-01-07 14:50:10 -0500266 .release_request = xprt_release_rqst_cong,
267 .buf_alloc = xprt_rdma_bc_allocate,
268 .buf_free = xprt_rdma_bc_free,
269 .send_request = xprt_rdma_bc_send_request,
270 .set_retrans_timeout = xprt_set_retrans_timeout_def,
271 .close = xprt_rdma_bc_close,
272 .destroy = xprt_rdma_bc_put,
273 .print_stats = xprt_rdma_print_stats
274};
275
276static const struct rpc_timeout xprt_rdma_bc_timeout = {
277 .to_initval = 60 * HZ,
278 .to_maxval = 60 * HZ,
279};
280
281/* It shouldn't matter if the number of backchannel session slots
282 * doesn't match the number of RPC/RDMA credits. That just means
283 * one or the other will have extra slots that aren't used.
284 */
285static struct rpc_xprt *
286xprt_setup_rdma_bc(struct xprt_create *args)
287{
288 struct rpc_xprt *xprt;
289 struct rpcrdma_xprt *new_xprt;
290
291 if (args->addrlen > sizeof(xprt->addr)) {
292 dprintk("RPC: %s: address too large\n", __func__);
293 return ERR_PTR(-EBADF);
294 }
295
296 xprt = xprt_alloc(args->net, sizeof(*new_xprt),
297 RPCRDMA_MAX_BC_REQUESTS,
298 RPCRDMA_MAX_BC_REQUESTS);
299 if (!xprt) {
300 dprintk("RPC: %s: couldn't allocate rpc_xprt\n",
301 __func__);
302 return ERR_PTR(-ENOMEM);
303 }
304
305 xprt->timeout = &xprt_rdma_bc_timeout;
306 xprt_set_bound(xprt);
307 xprt_set_connected(xprt);
308 xprt->bind_timeout = RPCRDMA_BIND_TO;
309 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
310 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
311
312 xprt->prot = XPRT_TRANSPORT_BC_RDMA;
Chuck Lever99722fe2018-05-07 15:28:25 -0400313 xprt->tsh_size = 0;
Chuck Lever5d252f92016-01-07 14:50:10 -0500314 xprt->ops = &xprt_rdma_bc_procs;
315
316 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
317 xprt->addrlen = args->addrlen;
318 xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
319 xprt->resvport = 0;
320
321 xprt->max_payload = xprt_rdma_max_inline_read;
322
323 new_xprt = rpcx_to_rdmax(xprt);
324 new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
325
326 xprt_get(xprt);
327 args->bc_xprt->xpt_bc_xprt = xprt;
328 xprt->bc_xprt = args->bc_xprt;
329
330 if (!try_module_get(THIS_MODULE))
331 goto out_fail;
332
333 /* Final put for backchannel xprt is in __svc_rdma_free */
334 xprt_get(xprt);
335 return xprt;
336
337out_fail:
338 xprt_rdma_free_addresses(xprt);
339 args->bc_xprt->xpt_bc_xprt = NULL;
Chuck Lever1b9f7002016-11-29 11:04:26 -0500340 args->bc_xprt->xpt_bc_xps = NULL;
Chuck Lever5d252f92016-01-07 14:50:10 -0500341 xprt_put(xprt);
342 xprt_free(xprt);
343 return ERR_PTR(-EINVAL);
344}
345
346struct xprt_class xprt_rdma_bc = {
347 .list = LIST_HEAD_INIT(xprt_rdma_bc.list),
348 .name = "rdma backchannel",
349 .owner = THIS_MODULE,
350 .ident = XPRT_TRANSPORT_BC_RDMA,
351 .setup = xprt_setup_rdma_bc,
352};