blob: ca41e28d2b36aa2e40b5418986922bf368834f6a [file] [log] [blame]
Chuck Lever5d252f92016-01-07 14:50:10 -05001/*
2 * Copyright (c) 2015 Oracle. All rights reserved.
3 *
4 * Support for backward direction RPCs on RPC/RDMA (server-side).
5 */
6
Russell Kingf39b2dde2017-02-07 15:02:54 -08007#include <linux/module.h>
Chuck Lever5d252f92016-01-07 14:50:10 -05008#include <linux/sunrpc/svc_rdma.h>
9#include "xprt_rdma.h"
10
11#define RPCDBG_FACILITY RPCDBG_SVCXPRT
12
13#undef SVCRDMA_BACKCHANNEL_DEBUG
14
Chuck Leverf5821c72017-04-09 13:06:49 -040015/**
16 * svc_rdma_handle_bc_reply - Process incoming backchannel reply
17 * @xprt: controlling backchannel transport
18 * @rdma_resp: pointer to incoming transport header
19 * @rcvbuf: XDR buffer into which to decode the reply
20 *
21 * Returns:
22 * %0 if @rcvbuf is filled in, xprt_complete_rqst called,
23 * %-EAGAIN if server should call ->recvfrom again.
24 */
25int svc_rdma_handle_bc_reply(struct rpc_xprt *xprt, __be32 *rdma_resp,
Chuck Lever5d252f92016-01-07 14:50:10 -050026 struct xdr_buf *rcvbuf)
27{
28 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
29 struct kvec *dst, *src = &rcvbuf->head[0];
30 struct rpc_rqst *req;
31 unsigned long cwnd;
32 u32 credits;
33 size_t len;
34 __be32 xid;
35 __be32 *p;
36 int ret;
37
38 p = (__be32 *)src->iov_base;
39 len = src->iov_len;
Chuck Leverf5821c72017-04-09 13:06:49 -040040 xid = *rdma_resp;
Chuck Lever5d252f92016-01-07 14:50:10 -050041
42#ifdef SVCRDMA_BACKCHANNEL_DEBUG
43 pr_info("%s: xid=%08x, length=%zu\n",
44 __func__, be32_to_cpu(xid), len);
45 pr_info("%s: RPC/RDMA: %*ph\n",
Chuck Leverf5821c72017-04-09 13:06:49 -040046 __func__, (int)RPCRDMA_HDRLEN_MIN, rdma_resp);
Chuck Lever5d252f92016-01-07 14:50:10 -050047 pr_info("%s: RPC: %*ph\n",
48 __func__, (int)len, p);
49#endif
50
51 ret = -EAGAIN;
52 if (src->iov_len < 24)
53 goto out_shortreply;
54
55 spin_lock_bh(&xprt->transport_lock);
56 req = xprt_lookup_rqst(xprt, xid);
57 if (!req)
58 goto out_notfound;
59
60 dst = &req->rq_private_buf.head[0];
61 memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf));
62 if (dst->iov_len < len)
63 goto out_unlock;
64 memcpy(dst->iov_base, p, len);
65
Chuck Leverf5821c72017-04-09 13:06:49 -040066 credits = be32_to_cpup(rdma_resp + 2);
Chuck Lever5d252f92016-01-07 14:50:10 -050067 if (credits == 0)
68 credits = 1; /* don't deadlock */
69 else if (credits > r_xprt->rx_buf.rb_bc_max_requests)
70 credits = r_xprt->rx_buf.rb_bc_max_requests;
71
72 cwnd = xprt->cwnd;
73 xprt->cwnd = credits << RPC_CWNDSHIFT;
74 if (xprt->cwnd > cwnd)
75 xprt_release_rqst_cong(req->rq_task);
76
77 ret = 0;
78 xprt_complete_rqst(req->rq_task, rcvbuf->len);
79 rcvbuf->len = 0;
80
81out_unlock:
82 spin_unlock_bh(&xprt->transport_lock);
83out:
84 return ret;
85
86out_shortreply:
87 dprintk("svcrdma: short bc reply: xprt=%p, len=%zu\n",
88 xprt, src->iov_len);
89 goto out;
90
91out_notfound:
92 dprintk("svcrdma: unrecognized bc reply: xprt=%p, xid=%08x\n",
93 xprt, be32_to_cpu(xid));
94
95 goto out_unlock;
96}
97
98/* Send a backwards direction RPC call.
99 *
100 * Caller holds the connection's mutex and has already marshaled
101 * the RPC/RDMA request.
102 *
Chuck Lever9a6a1802017-04-09 13:06:25 -0400103 * This is similar to svc_rdma_send_reply_msg, but takes a struct
104 * rpc_rqst instead, does not support chunks, and avoids blocking
105 * memory allocation.
Chuck Lever5d252f92016-01-07 14:50:10 -0500106 *
107 * XXX: There is still an opportunity to block in svc_rdma_send()
108 * if there are no SQ entries to post the Send. This may occur if
109 * the adapter has a small maximum SQ depth.
110 */
111static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma,
112 struct rpc_rqst *rqst)
113{
Chuck Lever5d252f92016-01-07 14:50:10 -0500114 struct svc_rdma_op_ctxt *ctxt;
Chuck Lever5d252f92016-01-07 14:50:10 -0500115 int ret;
116
Chuck Lever6e6092c2017-04-09 13:05:44 -0400117 ctxt = svc_rdma_get_context(rdma);
118
119 /* rpcrdma_bc_send_request builds the transport header and
120 * the backchannel RPC message in the same buffer. Thus only
121 * one SGE is needed to send both.
122 */
123 ret = svc_rdma_map_reply_hdr(rdma, ctxt, rqst->rq_buffer,
124 rqst->rq_snd_buf.len);
125 if (ret < 0)
Chuck Lever5d252f92016-01-07 14:50:10 -0500126 goto out_err;
127
Chuck Leverbf363872016-03-01 13:06:20 -0500128 ret = svc_rdma_repost_recv(rdma, GFP_NOIO);
129 if (ret)
Chuck Lever5d252f92016-01-07 14:50:10 -0500130 goto out_err;
Chuck Lever5d252f92016-01-07 14:50:10 -0500131
Chuck Lever17f5f7f2017-04-09 13:05:36 -0400132 ret = svc_rdma_post_send_wr(rdma, ctxt, 1, 0);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400133 if (ret)
Chuck Lever5d252f92016-01-07 14:50:10 -0500134 goto out_unmap;
Chuck Lever5d252f92016-01-07 14:50:10 -0500135
136out_err:
Chuck Lever5d252f92016-01-07 14:50:10 -0500137 dprintk("svcrdma: %s returns %d\n", __func__, ret);
138 return ret;
139
140out_unmap:
141 svc_rdma_unmap_dma(ctxt);
142 svc_rdma_put_context(ctxt, 1);
Chuck Lever6e6092c2017-04-09 13:05:44 -0400143 ret = -EIO;
Chuck Lever5d252f92016-01-07 14:50:10 -0500144 goto out_err;
145}
146
147/* Server-side transport endpoint wants a whole page for its send
148 * buffer. The client RPC code constructs the RPC header in this
149 * buffer before it invokes ->send_request.
Chuck Lever5d252f92016-01-07 14:50:10 -0500150 */
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400151static int
152xprt_rdma_bc_allocate(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500153{
154 struct rpc_rqst *rqst = task->tk_rqstp;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400155 size_t size = rqst->rq_callsize;
Chuck Lever5d252f92016-01-07 14:50:10 -0500156 struct page *page;
157
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400158 if (size > PAGE_SIZE) {
Chuck Lever5d252f92016-01-07 14:50:10 -0500159 WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n",
160 size);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400161 return -EINVAL;
162 }
Chuck Lever5d252f92016-01-07 14:50:10 -0500163
Chuck Lever8d426292016-10-28 22:22:33 -0400164 /* svc_rdma_sendto releases this page */
Chuck Lever5d252f92016-01-07 14:50:10 -0500165 page = alloc_page(RPCRDMA_DEF_GFP);
166 if (!page)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400167 return -ENOMEM;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400168 rqst->rq_buffer = page_address(page);
Chuck Lever8d426292016-10-28 22:22:33 -0400169
170 rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP);
171 if (!rqst->rq_rbuffer) {
172 put_page(page);
173 return -ENOMEM;
174 }
Chuck Lever5fe6eaa2016-09-15 10:55:20 -0400175 return 0;
Chuck Lever5d252f92016-01-07 14:50:10 -0500176}
177
178static void
Chuck Lever3435c742016-09-15 10:55:29 -0400179xprt_rdma_bc_free(struct rpc_task *task)
Chuck Lever5d252f92016-01-07 14:50:10 -0500180{
Chuck Lever8d426292016-10-28 22:22:33 -0400181 struct rpc_rqst *rqst = task->tk_rqstp;
182
183 kfree(rqst->rq_rbuffer);
Chuck Lever5d252f92016-01-07 14:50:10 -0500184}
185
186static int
187rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst)
188{
189 struct rpc_xprt *xprt = rqst->rq_xprt;
190 struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
Chuck Leverc2ccf642017-02-07 11:58:40 -0500191 __be32 *p;
Chuck Lever5d252f92016-01-07 14:50:10 -0500192 int rc;
193
194 /* Space in the send buffer for an RPC/RDMA header is reserved
195 * via xprt->tsh_size.
196 */
Chuck Leverc2ccf642017-02-07 11:58:40 -0500197 p = rqst->rq_buffer;
198 *p++ = rqst->rq_xid;
199 *p++ = rpcrdma_version;
200 *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests);
201 *p++ = rdma_msg;
202 *p++ = xdr_zero;
203 *p++ = xdr_zero;
204 *p = xdr_zero;
Chuck Lever5d252f92016-01-07 14:50:10 -0500205
206#ifdef SVCRDMA_BACKCHANNEL_DEBUG
207 pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer);
208#endif
209
210 rc = svc_rdma_bc_sendto(rdma, rqst);
211 if (rc)
212 goto drop_connection;
213 return rc;
214
215drop_connection:
216 dprintk("svcrdma: failed to send bc call\n");
217 xprt_disconnect_done(xprt);
218 return -ENOTCONN;
219}
220
221/* Send an RPC call on the passive end of a transport
222 * connection.
223 */
224static int
225xprt_rdma_bc_send_request(struct rpc_task *task)
226{
227 struct rpc_rqst *rqst = task->tk_rqstp;
228 struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt;
229 struct svcxprt_rdma *rdma;
230 int ret;
231
232 dprintk("svcrdma: sending bc call with xid: %08x\n",
233 be32_to_cpu(rqst->rq_xid));
234
235 if (!mutex_trylock(&sxprt->xpt_mutex)) {
236 rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL);
237 if (!mutex_trylock(&sxprt->xpt_mutex))
238 return -EAGAIN;
239 rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task);
240 }
241
242 ret = -ENOTCONN;
243 rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt);
244 if (!test_bit(XPT_DEAD, &sxprt->xpt_flags))
245 ret = rpcrdma_bc_send_request(rdma, rqst);
246
247 mutex_unlock(&sxprt->xpt_mutex);
248
249 if (ret < 0)
250 return ret;
251 return 0;
252}
253
254static void
255xprt_rdma_bc_close(struct rpc_xprt *xprt)
256{
257 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
258}
259
260static void
261xprt_rdma_bc_put(struct rpc_xprt *xprt)
262{
263 dprintk("svcrdma: %s: xprt %p\n", __func__, xprt);
264
265 xprt_free(xprt);
266 module_put(THIS_MODULE);
267}
268
Chuck Leverd31ae252017-08-01 12:00:39 -0400269static const struct rpc_xprt_ops xprt_rdma_bc_procs = {
Chuck Lever5d252f92016-01-07 14:50:10 -0500270 .reserve_xprt = xprt_reserve_xprt_cong,
271 .release_xprt = xprt_release_xprt_cong,
272 .alloc_slot = xprt_alloc_slot,
273 .release_request = xprt_release_rqst_cong,
274 .buf_alloc = xprt_rdma_bc_allocate,
275 .buf_free = xprt_rdma_bc_free,
276 .send_request = xprt_rdma_bc_send_request,
277 .set_retrans_timeout = xprt_set_retrans_timeout_def,
278 .close = xprt_rdma_bc_close,
279 .destroy = xprt_rdma_bc_put,
280 .print_stats = xprt_rdma_print_stats
281};
282
283static const struct rpc_timeout xprt_rdma_bc_timeout = {
284 .to_initval = 60 * HZ,
285 .to_maxval = 60 * HZ,
286};
287
288/* It shouldn't matter if the number of backchannel session slots
289 * doesn't match the number of RPC/RDMA credits. That just means
290 * one or the other will have extra slots that aren't used.
291 */
292static struct rpc_xprt *
293xprt_setup_rdma_bc(struct xprt_create *args)
294{
295 struct rpc_xprt *xprt;
296 struct rpcrdma_xprt *new_xprt;
297
298 if (args->addrlen > sizeof(xprt->addr)) {
299 dprintk("RPC: %s: address too large\n", __func__);
300 return ERR_PTR(-EBADF);
301 }
302
303 xprt = xprt_alloc(args->net, sizeof(*new_xprt),
304 RPCRDMA_MAX_BC_REQUESTS,
305 RPCRDMA_MAX_BC_REQUESTS);
306 if (!xprt) {
307 dprintk("RPC: %s: couldn't allocate rpc_xprt\n",
308 __func__);
309 return ERR_PTR(-ENOMEM);
310 }
311
312 xprt->timeout = &xprt_rdma_bc_timeout;
313 xprt_set_bound(xprt);
314 xprt_set_connected(xprt);
315 xprt->bind_timeout = RPCRDMA_BIND_TO;
316 xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO;
317 xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO;
318
319 xprt->prot = XPRT_TRANSPORT_BC_RDMA;
320 xprt->tsh_size = RPCRDMA_HDRLEN_MIN / sizeof(__be32);
321 xprt->ops = &xprt_rdma_bc_procs;
322
323 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
324 xprt->addrlen = args->addrlen;
325 xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr);
326 xprt->resvport = 0;
327
328 xprt->max_payload = xprt_rdma_max_inline_read;
329
330 new_xprt = rpcx_to_rdmax(xprt);
331 new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs;
332
333 xprt_get(xprt);
334 args->bc_xprt->xpt_bc_xprt = xprt;
335 xprt->bc_xprt = args->bc_xprt;
336
337 if (!try_module_get(THIS_MODULE))
338 goto out_fail;
339
340 /* Final put for backchannel xprt is in __svc_rdma_free */
341 xprt_get(xprt);
342 return xprt;
343
344out_fail:
345 xprt_rdma_free_addresses(xprt);
346 args->bc_xprt->xpt_bc_xprt = NULL;
Chuck Lever1b9f7002016-11-29 11:04:26 -0500347 args->bc_xprt->xpt_bc_xps = NULL;
Chuck Lever5d252f92016-01-07 14:50:10 -0500348 xprt_put(xprt);
349 xprt_free(xprt);
350 return ERR_PTR(-EINVAL);
351}
352
353struct xprt_class xprt_rdma_bc = {
354 .list = LIST_HEAD_INIT(xprt_rdma_bc.list),
355 .name = "rdma backchannel",
356 .owner = THIS_MODULE,
357 .ident = XPRT_TRANSPORT_BC_RDMA,
358 .setup = xprt_setup_rdma_bc,
359};