blob: d501521631904c90f28c915f309efad14c36c41f [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 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;
35 unsigned long cwnd;
36 u32 credits;
37 size_t len;
38 __be32 xid;
39 __be32 *p;
40 int ret;
41
42 p = (__be32 *)src->iov_base;
43 len = src->iov_len;
Chuck Leverf5821c72017-04-09 13:06:49 -040044 xid = *rdma_resp;
Chuck Lever5d252f92016-01-07 14:50:10 -050045
46#ifdef SVCRDMA_BACKCHANNEL_DEBUG
47 pr_info("%s: xid=%08x, length=%zu\n",
48 __func__, be32_to_cpu(xid), len);
49 pr_info("%s: RPC/RDMA: %*ph\n",
Chuck Leverf5821c72017-04-09 13:06:49 -040050 __func__, (int)RPCRDMA_HDRLEN_MIN, rdma_resp);
Chuck Lever5d252f92016-01-07 14:50:10 -050051 pr_info("%s: RPC: %*ph\n",
52 __func__, (int)len, p);
53#endif
54
55 ret = -EAGAIN;
56 if (src->iov_len < 24)
57 goto out_shortreply;
58
Trond Myklebustce7c2522017-08-16 15:30:35 -040059 spin_lock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050060 req = xprt_lookup_rqst(xprt, xid);
61 if (!req)
62 goto out_notfound;
63
64 dst = &req->rq_private_buf.head[0];
65 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
66 if (dst->iov_len < len)
67 goto out_unlock;
68 memcpy(dst->iov_base, p, len);
69
Chuck Leverf5821c72017-04-09 13:06:49 -040070 credits = be32_to_cpup(rdma_resp + 2);
Chuck Lever5d252f92016-01-07 14:50:10 -050071 if (credits == 0)
72 credits = 1; /* don't deadlock */
73 else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
74 credits = r_xprt->rx_buf.rb_bc_max_requests;
75
Trond Myklebustce7c2522017-08-16 15:30:35 -040076 spin_lock_bh(&xprt->transport_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050077 cwnd = xprt->cwnd;
78 xprt->cwnd = credits << RPC_CWNDSHIFT;
79 if (xprt->cwnd > cwnd)
80 xprt_release_rqst_cong(req->rq_task);
Trond Myklebustce7c2522017-08-16 15:30:35 -040081 spin_unlock_bh(&xprt->transport_lock);
82
Chuck Lever5d252f92016-01-07 14:50:10 -050083
84 ret = 0;
85 xprt_complete_rqst(req->rq_task, rcvbuf->len);
86 rcvbuf->len = 0;
87
88out_unlock:
Trond Myklebustce7c2522017-08-16 15:30:35 -040089 spin_unlock(&xprt->recv_lock);
Chuck Lever5d252f92016-01-07 14:50:10 -050090out:
91 return ret;
92
93out_shortreply:
94 dprintk("svcrdma: short bc reply: xprt=%p, len=%zu\n",
95 xprt, src->iov_len);
96 goto out;
97
98out_notfound:
99 dprintk("svcrdma: unrecognized bc reply: xprt=%p, xid=%08x\n",
100 xprt, be32_to_cpu(xid));
Chuck Lever5d252f92016-01-07 14:50:10 -0500101 goto out_unlock;
102}
103
104/* Send a backwards direction RPC call.
105 *
106 * Caller holds the connection's mutex and has already marshaled
107 * the RPC/RDMA request.
108 *
Chuck Lever9a6a1802017-04-09 13:06:25 -0400109 * This is similar to svc_rdma_send_reply_msg, but takes a struct
110 * rpc_rqst instead, does not support chunks, and avoids blocking
111 * memory allocation.
Chuck Lever5d252f92016-01-07 14:50:10 -0500112 *
113 * XXX: There is still an opportunity to block in svc_rdma_send()
114 * if there are no SQ entries to post the Send. This may occur if
115 * the adapter has a small maximum SQ depth.
116 */
117static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
118 struct rpc_rqst *rqst)
119{
Chuck Lever5d252f92016-01-07 14:50:10 -0500120 struct svc_rdma_op_ctxt *ctxt;
Chuck Lever5d252f92016-01-07 14:50:10 -0500121 int ret;
122
Chuck Lever6e6092c2017-04-09 13:05:44 -0400123 ctxt = svc_rdma_get_context(rdma);
124
125 /* rpcrdma_bc_send_request builds the transport header and
126 * the backchannel RPC message in the same buffer. Thus only
127 * one SGE is needed to send both.
128 */
129 ret = svc_rdma_map_reply_hdr(rdma, ctxt, rqst->rq_buffer,
130 rqst->rq_snd_buf.len);
131 if (ret < 0)
Chuck Lever5d252f92016-01-07 14:50:10 -0500132 goto out_err;
133
Chuck Lever0bad47c2017-10-16 12:14:33 -0400134 /* Bump page refcnt so Send completion doesn't release
135 * the rq_buffer before all retransmits are complete.
136 */
137 get_page(virt_to_page(rqst->rq_buffer));
Chuck Lever17f5f7f2017-04-09 13:05:36 -0400138 ret = svc_rdma_post_send_wr(rdma, ctxt, 1, 0);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400139 if (ret)
Chuck Lever5d252f92016-01-07 14:50:10 -0500140 goto out_unmap;
Chuck Lever5d252f92016-01-07 14:50:10 -0500141
142out_err:
Chuck Lever5d252f92016-01-07 14:50:10 -0500143 dprintk("svcrdma: %s returns %d\n", __func__, ret);
144 return ret;
145
146out_unmap:
147 svc_rdma_unmap_dma(ctxt);
148 svc_rdma_put_context(ctxt, 1);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400149 ret = -EIO;
Chuck Lever5d252f92016-01-07 14:50:10 -0500150 goto out_err;
151}
152
153/* Server-side transport endpoint wants a whole page for its send
154 * buffer. The client RPC code constructs the RPC header in this
155 * buffer before it invokes ->send_request.
Chuck Lever5d252f92016-01-07 14:50:10 -0500156 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400157static int
158xprt_rdma_bc_allocate(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500159{
160 struct rpc_rqst *rqst = task->tk_rqstp;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400161 size_t size = rqst->rq_callsize;
Chuck Lever5d252f92016-01-07 14:50:10 -0500162 struct page *page;
163
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400164 if (size > PAGE_SIZE) {
Chuck Lever5d252f92016-01-07 14:50:10 -0500165 WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
166 size);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400167 return -EINVAL;
168 }
Chuck Lever5d252f92016-01-07 14:50:10 -0500169
170 page = alloc_page(RPCRDMA_DEF_GFP);
171 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400172 return -ENOMEM;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400173 rqst->rq_buffer = page_address(page);
Chuck Lever8d426292016-10-28 22:22:33 -0400174
175 rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
176 if (!rqst->rq_rbuffer) {
177 put_page(page);
178 return -ENOMEM;
179 }
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400180 return 0;
Chuck Lever5d252f92016-01-07 14:50:10 -0500181}
182
183static void
Chuck Lever3435c742016-09-15 10:55:29 -0400184xprt_rdma_bc_free(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500185{
Chuck Lever8d426292016-10-28 22:22:33 -0400186 struct rpc_rqst *rqst = task->tk_rqstp;
187
Chuck Lever0bad47c2017-10-16 12:14:33 -0400188 put_page(virt_to_page(rqst->rq_buffer));
Chuck Lever8d426292016-10-28 22:22:33 -0400189 kfree(rqst->rq_rbuffer);
Chuck Lever5d252f92016-01-07 14:50:10 -0500190}
191
192static int
193rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
194{
195 struct rpc_xprt *xprt = rqst->rq_xprt;
196 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
Chuck Leverc2ccf642017-02-07 11:58:40 -0500197 __be32 *p;
Chuck Lever5d252f92016-01-07 14:50:10 -0500198 int rc;
199
200 /* Space in the send buffer for an RPC/RDMA header is reserved
201 * via xprt->tsh_size.
202 */
Chuck Leverc2ccf642017-02-07 11:58:40 -0500203 p = rqst->rq_buffer;
204 *p++ = rqst->rq_xid;
205 *p++ = rpcrdma_version;
206 *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
207 *p++ = rdma_msg;
208 *p++ = xdr_zero;
209 *p++ = xdr_zero;
210 *p = xdr_zero;
Chuck Lever5d252f92016-01-07 14:50:10 -0500211
212#ifdef SVCRDMA_BACKCHANNEL_DEBUG
213 pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer);
214#endif
215
216 rc = svc_rdma_bc_sendto(rdma, rqst);
217 if (rc)
218 goto drop_connection;
219 return rc;
220
221drop_connection:
222 dprintk("svcrdma: failed to send bc call\n");
223 xprt_disconnect_done(xprt);
224 return -ENOTCONN;
225}
226
227/* Send an RPC call on the passive end of a transport
228 * connection.
229 */
230static int
231xprt_rdma_bc_send_request(struct rpc_task *task)
232{
233 struct rpc_rqst *rqst = task->tk_rqstp;
234 struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
235 struct svcxprt_rdma *rdma;
236 int ret;
237
238 dprintk("svcrdma: sending bc call with xid: %08x\n",
239 be32_to_cpu(rqst->rq_xid));
240
241 if (!mutex_trylock(&sxprt->xpt_mutex)) {
242 rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL);
243 if (!mutex_trylock(&sxprt->xpt_mutex))
244 return -EAGAIN;
245 rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task);
246 }
247
248 ret = -ENOTCONN;
249 rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt);
250 if (!test_bit(XPT_DEAD, &sxprt->xpt_flags))
251 ret = rpcrdma_bc_send_request(rdma, rqst);
252
253 mutex_unlock(&sxprt->xpt_mutex);
254
255 if (ret < 0)
256 return ret;
257 return 0;
258}
259
260static void
261xprt_rdma_bc_close(struct rpc_xprt *xprt)
262{
263 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
264}
265
266static void
267xprt_rdma_bc_put(struct rpc_xprt *xprt)
268{
269 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
270
271 xprt_free(xprt);
272 module_put(THIS_MODULE);
273}
274
Chuck Leverd31ae252017-08-01 12:00:39 -0400275static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
Chuck Lever5d252f92016-01-07 14:50:10 -0500276 .reserve_xprt = xprt_reserve_xprt_cong,
277 .release_xprt = xprt_release_xprt_cong,
278 .alloc_slot = xprt_alloc_slot,
279 .release_request = xprt_release_rqst_cong,
280 .buf_alloc = xprt_rdma_bc_allocate,
281 .buf_free = xprt_rdma_bc_free,
282 .send_request = xprt_rdma_bc_send_request,
283 .set_retrans_timeout = xprt_set_retrans_timeout_def,
284 .close = xprt_rdma_bc_close,
285 .destroy = xprt_rdma_bc_put,
286 .print_stats = xprt_rdma_print_stats
287};
288
289static const struct rpc_timeout xprt_rdma_bc_timeout = {
290 .to_initval = 60 * HZ,
291 .to_maxval = 60 * HZ,
292};
293
294/* It shouldn't matter if the number of backchannel session slots
295 * doesn't match the number of RPC/RDMA credits. That just means
296 * one or the other will have extra slots that aren't used.
297 */
298static struct rpc_xprt *
299xprt_setup_rdma_bc(struct xprt_create *args)
300{
301 struct rpc_xprt *xprt;
302 struct rpcrdma_xprt *new_xprt;
303
304 if (args->addrlen > sizeof(xprt->addr)) {
305 dprintk("RPC: %s: address too large\n", __func__);
306 return ERR_PTR(-EBADF);
307 }
308
309 xprt = xprt_alloc(args->net, sizeof(*new_xprt),
310 RPCRDMA_MAX_BC_REQUESTS,
311 RPCRDMA_MAX_BC_REQUESTS);
312 if (!xprt) {
313 dprintk("RPC: %s: couldn't allocate rpc_xprt\n",
314 __func__);
315 return ERR_PTR(-ENOMEM);
316 }
317
318 xprt->timeout = &xprt_rdma_bc_timeout;
319 xprt_set_bound(xprt);
320 xprt_set_connected(xprt);
321 xprt->bind_timeout = RPCRDMA_BIND_TO;
322 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
323 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
324
325 xprt->prot = XPRT_TRANSPORT_BC_RDMA;
326 xprt->tsh_size = RPCRDMA_HDRLEN_MIN / sizeof(__be32);
327 xprt->ops = &xprt_rdma_bc_procs;
328
329 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
330 xprt->addrlen = args->addrlen;
331 xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
332 xprt->resvport = 0;
333
334 xprt->max_payload = xprt_rdma_max_inline_read;
335
336 new_xprt = rpcx_to_rdmax(xprt);
337 new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
338
339 xprt_get(xprt);
340 args->bc_xprt->xpt_bc_xprt = xprt;
341 xprt->bc_xprt = args->bc_xprt;
342
343 if (!try_module_get(THIS_MODULE))
344 goto out_fail;
345
346 /* Final put for backchannel xprt is in __svc_rdma_free */
347 xprt_get(xprt);
348 return xprt;
349
350out_fail:
351 xprt_rdma_free_addresses(xprt);
352 args->bc_xprt->xpt_bc_xprt = NULL;
Chuck Lever1b9f7002016-11-29 11:04:26 -0500353 args->bc_xprt->xpt_bc_xps = NULL;
Chuck Lever5d252f92016-01-07 14:50:10 -0500354 xprt_put(xprt);
355 xprt_free(xprt);
356 return ERR_PTR(-EINVAL);
357}
358
359struct xprt_class xprt_rdma_bc = {
360 .list = LIST_HEAD_INIT(xprt_rdma_bc.list),
361 .name = "rdma backchannel",
362 .owner = THIS_MODULE,
363 .ident = XPRT_TRANSPORT_BC_RDMA,
364 .setup = xprt_setup_rdma_bc,
365};