blob: 1035516d54e28079e233a4fb5f3ae5bdd4ffe76f [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/*
3 * Copyright (c) 2015 Oracle. All rights reserved.
4 *
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 Lever5d252f92016-01-07 14:50:10 -05009#include <linux/sunrpc/svc_rdma.h>
10#include "xprt_rdma.h"
11
12#define RPCDBG_FACILITY RPCDBG_SVCXPRT
13
14#undef SVCRDMA_BACKCHANNEL_DEBUG
15
Chuck Leverf5821c72017-04-09 13:06:49 -040016/**
17 * svc_rdma_handle_bc_reply - Process incoming backchannel reply
18 * @xprt: controlling backchannel transport
19 * @rdma_resp: pointer to incoming transport header
20 * @rcvbuf: XDR buffer into which to decode the reply
21 *
22 * Returns:
23 * %0 if @rcvbuf is filled in, xprt_complete_rqst called,
24 * %-EAGAIN if server should call ->recvfrom again.
25 */
26int svc_rdma_handle_bc_reply(struct rpc_xprt *xprt, __be32 *rdma_resp,
Chuck Lever5d252f92016-01-07 14:50:10 -050027 struct xdr_buf *rcvbuf)
28{
29 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
30 struct kvec *dst, *src = &rcvbuf->head[0];
31 struct rpc_rqst *req;
32 unsigned long cwnd;
33 u32 credits;
34 size_t len;
35 __be32 xid;
36 __be32 *p;
37 int ret;
38
39 p = (__be32 *)src->iov_base;
40 len = src->iov_len;
Chuck Leverf5821c72017-04-09 13:06:49 -040041 xid = *rdma_resp;
Chuck Lever5d252f92016-01-07 14:50:10 -050042
43#ifdef SVCRDMA_BACKCHANNEL_DEBUG
44 pr_info("%s: xid=%08x, length=%zu\n",
45 __func__, be32_to_cpu(xid), len);
46 pr_info("%s: RPC/RDMA: %*ph\n",
Chuck Leverf5821c72017-04-09 13:06:49 -040047 __func__, (int)RPCRDMA_HDRLEN_MIN, rdma_resp);
Chuck Lever5d252f92016-01-07 14:50:10 -050048 pr_info("%s: RPC: %*ph\n",
49 __func__, (int)len, p);
50#endif
51
52 ret = -EAGAIN;
53 if (src->iov_len < 24)
54 goto out_shortreply;
55
Trond Myklebustce7c2522017-08-16 15:30:35 -040056 spin_lock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050057 req = xprt_lookup_rqst(xprt, xid);
58 if (!req)
59 goto out_notfound;
60
61 dst = &req->rq_private_buf.head[0];
62 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
63 if (dst->iov_len < len)
64 goto out_unlock;
65 memcpy(dst->iov_base, p, len);
66
Chuck Leverf5821c72017-04-09 13:06:49 -040067 credits = be32_to_cpup(rdma_resp + 2);
Chuck Lever5d252f92016-01-07 14:50:10 -050068 if (credits == 0)
69 credits = 1; /* don't deadlock */
70 else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
71 credits = r_xprt->rx_buf.rb_bc_max_requests;
72
Trond Myklebustce7c2522017-08-16 15:30:35 -040073 spin_lock_bh(&xprt->transport_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050074 cwnd = xprt->cwnd;
75 xprt->cwnd = credits << RPC_CWNDSHIFT;
76 if (xprt->cwnd > cwnd)
77 xprt_release_rqst_cong(req->rq_task);
Trond Myklebustce7c2522017-08-16 15:30:35 -040078 spin_unlock_bh(&xprt->transport_lock);
79
Chuck Lever5d252f92016-01-07 14:50:10 -050080
81 ret = 0;
82 xprt_complete_rqst(req->rq_task, rcvbuf->len);
83 rcvbuf->len = 0;
84
85out_unlock:
Trond Myklebustce7c2522017-08-16 15:30:35 -040086 spin_unlock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050087out:
88 return ret;
89
90out_shortreply:
91 dprintk("svcrdma: short bc reply: xprt=%p, len=%zu\n",
92 xprt, src->iov_len);
93 goto out;
94
95out_notfound:
96 dprintk("svcrdma: unrecognized bc reply: xprt=%p, xid=%08x\n",
97 xprt, be32_to_cpu(xid));
Chuck Lever5d252f92016-01-07 14:50:10 -050098 goto out_unlock;
99}
100
101/* Send a backwards direction RPC call.
102 *
103 * Caller holds the connection's mutex and has already marshaled
104 * the RPC/RDMA request.
105 *
Chuck Lever9a6a1802017-04-09 13:06:25 -0400106 * This is similar to svc_rdma_send_reply_msg, but takes a struct
107 * rpc_rqst instead, does not support chunks, and avoids blocking
108 * memory allocation.
Chuck Lever5d252f92016-01-07 14:50:10 -0500109 *
110 * XXX: There is still an opportunity to block in svc_rdma_send()
111 * if there are no SQ entries to post the Send. This may occur if
112 * the adapter has a small maximum SQ depth.
113 */
114static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
115 struct rpc_rqst *rqst)
116{
Chuck Lever5d252f92016-01-07 14:50:10 -0500117 struct svc_rdma_op_ctxt *ctxt;
Chuck Lever5d252f92016-01-07 14:50:10 -0500118 int ret;
119
Chuck Lever6e6092c2017-04-09 13:05:44 -0400120 ctxt = svc_rdma_get_context(rdma);
121
122 /* rpcrdma_bc_send_request builds the transport header and
123 * the backchannel RPC message in the same buffer. Thus only
124 * one SGE is needed to send both.
125 */
126 ret = svc_rdma_map_reply_hdr(rdma, ctxt, rqst->rq_buffer,
127 rqst->rq_snd_buf.len);
128 if (ret < 0)
Chuck Lever5d252f92016-01-07 14:50:10 -0500129 goto out_err;
130
Chuck Lever0bad47c2017-10-16 12:14:33 -0400131 /* Bump page refcnt so Send completion doesn't release
132 * the rq_buffer before all retransmits are complete.
133 */
134 get_page(virt_to_page(rqst->rq_buffer));
Chuck Lever17f5f7f2017-04-09 13:05:36 -0400135 ret = svc_rdma_post_send_wr(rdma, ctxt, 1, 0);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400136 if (ret)
Chuck Lever5d252f92016-01-07 14:50:10 -0500137 goto out_unmap;
Chuck Lever5d252f92016-01-07 14:50:10 -0500138
139out_err:
Chuck Lever5d252f92016-01-07 14:50:10 -0500140 dprintk("svcrdma: %s returns %d\n", __func__, ret);
141 return ret;
142
143out_unmap:
144 svc_rdma_unmap_dma(ctxt);
145 svc_rdma_put_context(ctxt, 1);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400146 ret = -EIO;
Chuck Lever5d252f92016-01-07 14:50:10 -0500147 goto out_err;
148}
149
150/* Server-side transport endpoint wants a whole page for its send
151 * buffer. The client RPC code constructs the RPC header in this
152 * buffer before it invokes ->send_request.
Chuck Lever5d252f92016-01-07 14:50:10 -0500153 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400154static int
155xprt_rdma_bc_allocate(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500156{
157 struct rpc_rqst *rqst = task->tk_rqstp;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400158 size_t size = rqst->rq_callsize;
Chuck Lever5d252f92016-01-07 14:50:10 -0500159 struct page *page;
160
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400161 if (size > PAGE_SIZE) {
Chuck Lever5d252f92016-01-07 14:50:10 -0500162 WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
163 size);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400164 return -EINVAL;
165 }
Chuck Lever5d252f92016-01-07 14:50:10 -0500166
167 page = alloc_page(RPCRDMA_DEF_GFP);
168 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400169 return -ENOMEM;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400170 rqst->rq_buffer = page_address(page);
Chuck Lever8d426292016-10-28 22:22:33 -0400171
172 rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
173 if (!rqst->rq_rbuffer) {
174 put_page(page);
175 return -ENOMEM;
176 }
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400177 return 0;
Chuck Lever5d252f92016-01-07 14:50:10 -0500178}
179
180static void
Chuck Lever3435c742016-09-15 10:55:29 -0400181xprt_rdma_bc_free(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500182{
Chuck Lever8d426292016-10-28 22:22:33 -0400183 struct rpc_rqst *rqst = task->tk_rqstp;
184
Chuck Lever0bad47c2017-10-16 12:14:33 -0400185 put_page(virt_to_page(rqst->rq_buffer));
Chuck Lever8d426292016-10-28 22:22:33 -0400186 kfree(rqst->rq_rbuffer);
Chuck Lever5d252f92016-01-07 14:50:10 -0500187}
188
189static int
190rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
191{
192 struct rpc_xprt *xprt = rqst->rq_xprt;
193 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
Chuck Leverc2ccf642017-02-07 11:58:40 -0500194 __be32 *p;
Chuck Lever5d252f92016-01-07 14:50:10 -0500195 int rc;
196
197 /* Space in the send buffer for an RPC/RDMA header is reserved
198 * via xprt->tsh_size.
199 */
Chuck Leverc2ccf642017-02-07 11:58:40 -0500200 p = rqst->rq_buffer;
201 *p++ = rqst->rq_xid;
202 *p++ = rpcrdma_version;
203 *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
204 *p++ = rdma_msg;
205 *p++ = xdr_zero;
206 *p++ = xdr_zero;
207 *p = xdr_zero;
Chuck Lever5d252f92016-01-07 14:50:10 -0500208
209#ifdef SVCRDMA_BACKCHANNEL_DEBUG
210 pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer);
211#endif
212
213 rc = svc_rdma_bc_sendto(rdma, rqst);
214 if (rc)
215 goto drop_connection;
216 return rc;
217
218drop_connection:
219 dprintk("svcrdma: failed to send bc call\n");
220 xprt_disconnect_done(xprt);
221 return -ENOTCONN;
222}
223
224/* Send an RPC call on the passive end of a transport
225 * connection.
226 */
227static int
228xprt_rdma_bc_send_request(struct rpc_task *task)
229{
230 struct rpc_rqst *rqst = task->tk_rqstp;
231 struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
232 struct svcxprt_rdma *rdma;
233 int ret;
234
235 dprintk("svcrdma: sending bc call with xid: %08x\n",
236 be32_to_cpu(rqst->rq_xid));
237
238 if (!mutex_trylock(&sxprt->xpt_mutex)) {
239 rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL);
240 if (!mutex_trylock(&sxprt->xpt_mutex))
241 return -EAGAIN;
242 rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task);
243 }
244
245 ret = -ENOTCONN;
246 rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt);
247 if (!test_bit(XPT_DEAD, &sxprt->xpt_flags))
248 ret = rpcrdma_bc_send_request(rdma, rqst);
249
250 mutex_unlock(&sxprt->xpt_mutex);
251
252 if (ret < 0)
253 return ret;
254 return 0;
255}
256
257static void
258xprt_rdma_bc_close(struct rpc_xprt *xprt)
259{
260 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
261}
262
263static void
264xprt_rdma_bc_put(struct rpc_xprt *xprt)
265{
266 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
267
268 xprt_free(xprt);
269 module_put(THIS_MODULE);
270}
271
Chuck Leverd31ae252017-08-01 12:00:39 -0400272static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
Chuck Lever5d252f92016-01-07 14:50:10 -0500273 .reserve_xprt = xprt_reserve_xprt_cong,
274 .release_xprt = xprt_release_xprt_cong,
275 .alloc_slot = xprt_alloc_slot,
Chuck Levera9cde232018-05-04 15:34:59 -0400276 .free_slot = xprt_free_slot,
Chuck Lever5d252f92016-01-07 14:50:10 -0500277 .release_request = xprt_release_rqst_cong,
278 .buf_alloc = xprt_rdma_bc_allocate,
279 .buf_free = xprt_rdma_bc_free,
280 .send_request = xprt_rdma_bc_send_request,
281 .set_retrans_timeout = xprt_set_retrans_timeout_def,
282 .close = xprt_rdma_bc_close,
283 .destroy = xprt_rdma_bc_put,
284 .print_stats = xprt_rdma_print_stats
285};
286
287static const struct rpc_timeout xprt_rdma_bc_timeout = {
288 .to_initval = 60 * HZ,
289 .to_maxval = 60 * HZ,
290};
291
292/* It shouldn't matter if the number of backchannel session slots
293 * doesn't match the number of RPC/RDMA credits. That just means
294 * one or the other will have extra slots that aren't used.
295 */
296static struct rpc_xprt *
297xprt_setup_rdma_bc(struct xprt_create *args)
298{
299 struct rpc_xprt *xprt;
300 struct rpcrdma_xprt *new_xprt;
301
302 if (args->addrlen > sizeof(xprt->addr)) {
303 dprintk("RPC: %s: address too large\n", __func__);
304 return ERR_PTR(-EBADF);
305 }
306
307 xprt = xprt_alloc(args->net, sizeof(*new_xprt),
308 RPCRDMA_MAX_BC_REQUESTS,
309 RPCRDMA_MAX_BC_REQUESTS);
310 if (!xprt) {
311 dprintk("RPC: %s: couldn't allocate rpc_xprt\n",
312 __func__);
313 return ERR_PTR(-ENOMEM);
314 }
315
316 xprt->timeout = &xprt_rdma_bc_timeout;
317 xprt_set_bound(xprt);
318 xprt_set_connected(xprt);
319 xprt->bind_timeout = RPCRDMA_BIND_TO;
320 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
321 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
322
323 xprt->prot = XPRT_TRANSPORT_BC_RDMA;
324 xprt->tsh_size = RPCRDMA_HDRLEN_MIN / sizeof(__be32);
325 xprt->ops = &xprt_rdma_bc_procs;
326
327 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
328 xprt->addrlen = args->addrlen;
329 xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
330 xprt->resvport = 0;
331
332 xprt->max_payload = xprt_rdma_max_inline_read;
333
334 new_xprt = rpcx_to_rdmax(xprt);
335 new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
336
337 xprt_get(xprt);
338 args->bc_xprt->xpt_bc_xprt = xprt;
339 xprt->bc_xprt = args->bc_xprt;
340
341 if (!try_module_get(THIS_MODULE))
342 goto out_fail;
343
344 /* Final put for backchannel xprt is in __svc_rdma_free */
345 xprt_get(xprt);
346 return xprt;
347
348out_fail:
349 xprt_rdma_free_addresses(xprt);
350 args->bc_xprt->xpt_bc_xprt = NULL;
Chuck Lever1b9f7002016-11-29 11:04:26 -0500351 args->bc_xprt->xpt_bc_xps = NULL;
Chuck Lever5d252f92016-01-07 14:50:10 -0500352 xprt_put(xprt);
353 xprt_free(xprt);
354 return ERR_PTR(-EINVAL);
355}
356
357struct xprt_class xprt_rdma_bc = {
358 .list = LIST_HEAD_INIT(xprt_rdma_bc.list),
359 .name = "rdma backchannel",
360 .owner = THIS_MODULE,
361 .ident = XPRT_TRANSPORT_BC_RDMA,
362 .setup = xprt_setup_rdma_bc,
363};