blob: 95e33511cc6f3f2758c79bc08f8ef8ad3a93411a [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;
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 Lever4201c7462018-05-07 15:28:04 -0400120 struct svc_rdma_send_ctxt *ctxt;
Chuck Lever5d252f92016-01-07 14:50:10 -0500121 int ret;
122
Chuck Lever4201c7462018-05-07 15:28:04 -0400123 ctxt = svc_rdma_send_ctxt_get(rdma);
124 if (!ctxt) {
125 ret = -ENOMEM;
126 goto out_err;
127 }
Chuck Lever6e6092c2017-04-09 13:05:44 -0400128
129 /* rpcrdma_bc_send_request builds the transport header and
130 * the backchannel RPC message in the same buffer. Thus only
131 * one SGE is needed to send both.
132 */
133 ret = svc_rdma_map_reply_hdr(rdma, ctxt, rqst->rq_buffer,
134 rqst->rq_snd_buf.len);
135 if (ret < 0)
Chuck Lever5d252f92016-01-07 14:50:10 -0500136 goto out_err;
137
Chuck Lever0bad47c2017-10-16 12:14:33 -0400138 /* Bump page refcnt so Send completion doesn't release
139 * the rq_buffer before all retransmits are complete.
140 */
141 get_page(virt_to_page(rqst->rq_buffer));
Chuck Lever23262792018-05-07 15:27:59 -0400142 ret = svc_rdma_post_send_wr(rdma, ctxt, 0);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400143 if (ret)
Chuck Lever5d252f92016-01-07 14:50:10 -0500144 goto out_unmap;
Chuck Lever5d252f92016-01-07 14:50:10 -0500145
146out_err:
Chuck Lever5d252f92016-01-07 14:50:10 -0500147 dprintk("svcrdma: %s returns %d\n", __func__, ret);
148 return ret;
149
150out_unmap:
Chuck Lever4201c7462018-05-07 15:28:04 -0400151 svc_rdma_send_ctxt_put(rdma, ctxt);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400152 ret = -EIO;
Chuck Lever5d252f92016-01-07 14:50:10 -0500153 goto out_err;
154}
155
156/* Server-side transport endpoint wants a whole page for its send
157 * buffer. The client RPC code constructs the RPC header in this
158 * buffer before it invokes ->send_request.
Chuck Lever5d252f92016-01-07 14:50:10 -0500159 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400160static int
161xprt_rdma_bc_allocate(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500162{
163 struct rpc_rqst *rqst = task->tk_rqstp;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400164 size_t size = rqst->rq_callsize;
Chuck Lever5d252f92016-01-07 14:50:10 -0500165 struct page *page;
166
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400167 if (size > PAGE_SIZE) {
Chuck Lever5d252f92016-01-07 14:50:10 -0500168 WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
169 size);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400170 return -EINVAL;
171 }
Chuck Lever5d252f92016-01-07 14:50:10 -0500172
173 page = alloc_page(RPCRDMA_DEF_GFP);
174 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400175 return -ENOMEM;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400176 rqst->rq_buffer = page_address(page);
Chuck Lever8d426292016-10-28 22:22:33 -0400177
178 rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
179 if (!rqst->rq_rbuffer) {
180 put_page(page);
181 return -ENOMEM;
182 }
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400183 return 0;
Chuck Lever5d252f92016-01-07 14:50:10 -0500184}
185
186static void
Chuck Lever3435c742016-09-15 10:55:29 -0400187xprt_rdma_bc_free(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500188{
Chuck Lever8d426292016-10-28 22:22:33 -0400189 struct rpc_rqst *rqst = task->tk_rqstp;
190
Chuck Lever0bad47c2017-10-16 12:14:33 -0400191 put_page(virt_to_page(rqst->rq_buffer));
Chuck Lever8d426292016-10-28 22:22:33 -0400192 kfree(rqst->rq_rbuffer);
Chuck Lever5d252f92016-01-07 14:50:10 -0500193}
194
195static int
196rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
197{
198 struct rpc_xprt *xprt = rqst->rq_xprt;
199 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
Chuck Leverc2ccf642017-02-07 11:58:40 -0500200 __be32 *p;
Chuck Lever5d252f92016-01-07 14:50:10 -0500201 int rc;
202
203 /* Space in the send buffer for an RPC/RDMA header is reserved
204 * via xprt->tsh_size.
205 */
Chuck Leverc2ccf642017-02-07 11:58:40 -0500206 p = rqst->rq_buffer;
207 *p++ = rqst->rq_xid;
208 *p++ = rpcrdma_version;
209 *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
210 *p++ = rdma_msg;
211 *p++ = xdr_zero;
212 *p++ = xdr_zero;
213 *p = xdr_zero;
Chuck Lever5d252f92016-01-07 14:50:10 -0500214
215#ifdef SVCRDMA_BACKCHANNEL_DEBUG
216 pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer);
217#endif
218
219 rc = svc_rdma_bc_sendto(rdma, rqst);
220 if (rc)
221 goto drop_connection;
222 return rc;
223
224drop_connection:
225 dprintk("svcrdma: failed to send bc call\n");
226 xprt_disconnect_done(xprt);
227 return -ENOTCONN;
228}
229
230/* Send an RPC call on the passive end of a transport
231 * connection.
232 */
233static int
234xprt_rdma_bc_send_request(struct rpc_task *task)
235{
236 struct rpc_rqst *rqst = task->tk_rqstp;
237 struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
238 struct svcxprt_rdma *rdma;
239 int ret;
240
241 dprintk("svcrdma: sending bc call with xid: %08x\n",
242 be32_to_cpu(rqst->rq_xid));
243
244 if (!mutex_trylock(&sxprt->xpt_mutex)) {
245 rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL);
246 if (!mutex_trylock(&sxprt->xpt_mutex))
247 return -EAGAIN;
248 rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task);
249 }
250
251 ret = -ENOTCONN;
252 rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt);
253 if (!test_bit(XPT_DEAD, &sxprt->xpt_flags))
254 ret = rpcrdma_bc_send_request(rdma, rqst);
255
256 mutex_unlock(&sxprt->xpt_mutex);
257
258 if (ret < 0)
259 return ret;
260 return 0;
261}
262
263static void
264xprt_rdma_bc_close(struct rpc_xprt *xprt)
265{
266 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
267}
268
269static void
270xprt_rdma_bc_put(struct rpc_xprt *xprt)
271{
272 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
273
274 xprt_free(xprt);
275 module_put(THIS_MODULE);
276}
277
Chuck Leverd31ae252017-08-01 12:00:39 -0400278static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
Chuck Lever5d252f92016-01-07 14:50:10 -0500279 .reserve_xprt = xprt_reserve_xprt_cong,
280 .release_xprt = xprt_release_xprt_cong,
281 .alloc_slot = xprt_alloc_slot,
282 .release_request = xprt_release_rqst_cong,
283 .buf_alloc = xprt_rdma_bc_allocate,
284 .buf_free = xprt_rdma_bc_free,
285 .send_request = xprt_rdma_bc_send_request,
286 .set_retrans_timeout = xprt_set_retrans_timeout_def,
287 .close = xprt_rdma_bc_close,
288 .destroy = xprt_rdma_bc_put,
289 .print_stats = xprt_rdma_print_stats
290};
291
292static const struct rpc_timeout xprt_rdma_bc_timeout = {
293 .to_initval = 60 * HZ,
294 .to_maxval = 60 * HZ,
295};
296
297/* It shouldn't matter if the number of backchannel session slots
298 * doesn't match the number of RPC/RDMA credits. That just means
299 * one or the other will have extra slots that aren't used.
300 */
301static struct rpc_xprt *
302xprt_setup_rdma_bc(struct xprt_create *args)
303{
304 struct rpc_xprt *xprt;
305 struct rpcrdma_xprt *new_xprt;
306
307 if (args->addrlen > sizeof(xprt->addr)) {
308 dprintk("RPC: %s: address too large\n", __func__);
309 return ERR_PTR(-EBADF);
310 }
311
312 xprt = xprt_alloc(args->net, sizeof(*new_xprt),
313 RPCRDMA_MAX_BC_REQUESTS,
314 RPCRDMA_MAX_BC_REQUESTS);
315 if (!xprt) {
316 dprintk("RPC: %s: couldn't allocate rpc_xprt\n",
317 __func__);
318 return ERR_PTR(-ENOMEM);
319 }
320
321 xprt->timeout = &xprt_rdma_bc_timeout;
322 xprt_set_bound(xprt);
323 xprt_set_connected(xprt);
324 xprt->bind_timeout = RPCRDMA_BIND_TO;
325 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
326 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
327
328 xprt->prot = XPRT_TRANSPORT_BC_RDMA;
329 xprt->tsh_size = RPCRDMA_HDRLEN_MIN / sizeof(__be32);
330 xprt->ops = &xprt_rdma_bc_procs;
331
332 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
333 xprt->addrlen = args->addrlen;
334 xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
335 xprt->resvport = 0;
336
337 xprt->max_payload = xprt_rdma_max_inline_read;
338
339 new_xprt = rpcx_to_rdmax(xprt);
340 new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
341
342 xprt_get(xprt);
343 args->bc_xprt->xpt_bc_xprt = xprt;
344 xprt->bc_xprt = args->bc_xprt;
345
346 if (!try_module_get(THIS_MODULE))
347 goto out_fail;
348
349 /* Final put for backchannel xprt is in __svc_rdma_free */
350 xprt_get(xprt);
351 return xprt;
352
353out_fail:
354 xprt_rdma_free_addresses(xprt);
355 args->bc_xprt->xpt_bc_xprt = NULL;
Chuck Lever1b9f7002016-11-29 11:04:26 -0500356 args->bc_xprt->xpt_bc_xps = NULL;
Chuck Lever5d252f92016-01-07 14:50:10 -0500357 xprt_put(xprt);
358 xprt_free(xprt);
359 return ERR_PTR(-EINVAL);
360}
361
362struct xprt_class xprt_rdma_bc = {
363 .list = LIST_HEAD_INIT(xprt_rdma_bc.list),
364 .name = "rdma backchannel",
365 .owner = THIS_MODULE,
366 .ident = XPRT_TRANSPORT_BC_RDMA,
367 .setup = xprt_setup_rdma_bc,
368};