]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/sunrpc/xprtrdma/svc_rdma_recvfrom.c
svcrdma: Use generic RDMA R/W API in RPC Call path
[karo-tx-linux.git] / net / sunrpc / xprtrdma / svc_rdma_recvfrom.c
1 /*
2  * Copyright (c) 2016, 2017 Oracle. All rights reserved.
3  * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
4  * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the BSD-type
10  * license below:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  *
16  *      Redistributions of source code must retain the above copyright
17  *      notice, this list of conditions and the following disclaimer.
18  *
19  *      Redistributions in binary form must reproduce the above
20  *      copyright notice, this list of conditions and the following
21  *      disclaimer in the documentation and/or other materials provided
22  *      with the distribution.
23  *
24  *      Neither the name of the Network Appliance, Inc. nor the names of
25  *      its contributors may be used to endorse or promote products
26  *      derived from this software without specific prior written
27  *      permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Author: Tom Tucker <tom@opengridcomputing.com>
42  */
43
44 /* Operation
45  *
46  * The main entry point is svc_rdma_recvfrom. This is called from
47  * svc_recv when the transport indicates there is incoming data to
48  * be read. "Data Ready" is signaled when an RDMA Receive completes,
49  * or when a set of RDMA Reads complete.
50  *
51  * An svc_rqst is passed in. This structure contains an array of
52  * free pages (rq_pages) that will contain the incoming RPC message.
53  *
54  * Short messages are moved directly into svc_rqst::rq_arg, and
55  * the RPC Call is ready to be processed by the Upper Layer.
56  * svc_rdma_recvfrom returns the length of the RPC Call message,
57  * completing the reception of the RPC Call.
58  *
59  * However, when an incoming message has Read chunks,
60  * svc_rdma_recvfrom must post RDMA Reads to pull the RPC Call's
61  * data payload from the client. svc_rdma_recvfrom sets up the
62  * RDMA Reads using pages in svc_rqst::rq_pages, which are
63  * transferred to an svc_rdma_op_ctxt for the duration of the
64  * I/O. svc_rdma_recvfrom then returns zero, since the RPC message
65  * is still not yet ready.
66  *
67  * When the Read chunk payloads have become available on the
68  * server, "Data Ready" is raised again, and svc_recv calls
69  * svc_rdma_recvfrom again. This second call may use a different
70  * svc_rqst than the first one, thus any information that needs
71  * to be preserved across these two calls is kept in an
72  * svc_rdma_op_ctxt.
73  *
74  * The second call to svc_rdma_recvfrom performs final assembly
75  * of the RPC Call message, using the RDMA Read sink pages kept in
76  * the svc_rdma_op_ctxt. The xdr_buf is copied from the
77  * svc_rdma_op_ctxt to the second svc_rqst. The second call returns
78  * the length of the completed RPC Call message.
79  *
80  * Page Management
81  *
82  * Pages under I/O must be transferred from the first svc_rqst to an
83  * svc_rdma_op_ctxt before the first svc_rdma_recvfrom call returns.
84  *
85  * The first svc_rqst supplies pages for RDMA Reads. These are moved
86  * from rqstp::rq_pages into ctxt::pages. The consumed elements of
87  * the rq_pages array are set to NULL and refilled with the first
88  * svc_rdma_recvfrom call returns.
89  *
90  * During the second svc_rdma_recvfrom call, RDMA Read sink pages
91  * are transferred from the svc_rdma_op_ctxt to the second svc_rqst
92  * (see rdma_read_complete() below).
93  */
94
95 #include <asm/unaligned.h>
96 #include <rdma/ib_verbs.h>
97 #include <rdma/rdma_cm.h>
98
99 #include <linux/spinlock.h>
100
101 #include <linux/sunrpc/xdr.h>
102 #include <linux/sunrpc/debug.h>
103 #include <linux/sunrpc/rpc_rdma.h>
104 #include <linux/sunrpc/svc_rdma.h>
105
106 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
107
108 /*
109  * Replace the pages in the rq_argpages array with the pages from the SGE in
110  * the RDMA_RECV completion. The SGL should contain full pages up until the
111  * last one.
112  */
113 static void rdma_build_arg_xdr(struct svc_rqst *rqstp,
114                                struct svc_rdma_op_ctxt *ctxt,
115                                u32 byte_count)
116 {
117         struct page *page;
118         u32 bc;
119         int sge_no;
120
121         /* Swap the page in the SGE with the page in argpages */
122         page = ctxt->pages[0];
123         put_page(rqstp->rq_pages[0]);
124         rqstp->rq_pages[0] = page;
125
126         /* Set up the XDR head */
127         rqstp->rq_arg.head[0].iov_base = page_address(page);
128         rqstp->rq_arg.head[0].iov_len =
129                 min_t(size_t, byte_count, ctxt->sge[0].length);
130         rqstp->rq_arg.len = byte_count;
131         rqstp->rq_arg.buflen = byte_count;
132
133         /* Compute bytes past head in the SGL */
134         bc = byte_count - rqstp->rq_arg.head[0].iov_len;
135
136         /* If data remains, store it in the pagelist */
137         rqstp->rq_arg.page_len = bc;
138         rqstp->rq_arg.page_base = 0;
139
140         sge_no = 1;
141         while (bc && sge_no < ctxt->count) {
142                 page = ctxt->pages[sge_no];
143                 put_page(rqstp->rq_pages[sge_no]);
144                 rqstp->rq_pages[sge_no] = page;
145                 bc -= min_t(u32, bc, ctxt->sge[sge_no].length);
146                 rqstp->rq_arg.buflen += ctxt->sge[sge_no].length;
147                 sge_no++;
148         }
149         rqstp->rq_respages = &rqstp->rq_pages[sge_no];
150         rqstp->rq_next_page = rqstp->rq_respages + 1;
151
152         /* If not all pages were used from the SGL, free the remaining ones */
153         bc = sge_no;
154         while (sge_no < ctxt->count) {
155                 page = ctxt->pages[sge_no++];
156                 put_page(page);
157         }
158         ctxt->count = bc;
159
160         /* Set up tail */
161         rqstp->rq_arg.tail[0].iov_base = NULL;
162         rqstp->rq_arg.tail[0].iov_len = 0;
163 }
164
165 /* This accommodates the largest possible Write chunk,
166  * in one segment.
167  */
168 #define MAX_BYTES_WRITE_SEG     ((u32)(RPCSVC_MAXPAGES << PAGE_SHIFT))
169
170 /* This accommodates the largest possible Position-Zero
171  * Read chunk or Reply chunk, in one segment.
172  */
173 #define MAX_BYTES_SPECIAL_SEG   ((u32)((RPCSVC_MAXPAGES + 2) << PAGE_SHIFT))
174
175 /* Sanity check the Read list.
176  *
177  * Implementation limits:
178  * - This implementation supports only one Read chunk.
179  *
180  * Sanity checks:
181  * - Read list does not overflow buffer.
182  * - Segment size limited by largest NFS data payload.
183  *
184  * The segment count is limited to how many segments can
185  * fit in the transport header without overflowing the
186  * buffer. That's about 40 Read segments for a 1KB inline
187  * threshold.
188  *
189  * Returns pointer to the following Write list.
190  */
191 static __be32 *xdr_check_read_list(__be32 *p, const __be32 *end)
192 {
193         u32 position;
194         bool first;
195
196         first = true;
197         while (*p++ != xdr_zero) {
198                 if (first) {
199                         position = be32_to_cpup(p++);
200                         first = false;
201                 } else if (be32_to_cpup(p++) != position) {
202                         return NULL;
203                 }
204                 p++;    /* handle */
205                 if (be32_to_cpup(p++) > MAX_BYTES_SPECIAL_SEG)
206                         return NULL;
207                 p += 2; /* offset */
208
209                 if (p > end)
210                         return NULL;
211         }
212         return p;
213 }
214
215 /* The segment count is limited to how many segments can
216  * fit in the transport header without overflowing the
217  * buffer. That's about 60 Write segments for a 1KB inline
218  * threshold.
219  */
220 static __be32 *xdr_check_write_chunk(__be32 *p, const __be32 *end,
221                                      u32 maxlen)
222 {
223         u32 i, segcount;
224
225         segcount = be32_to_cpup(p++);
226         for (i = 0; i < segcount; i++) {
227                 p++;    /* handle */
228                 if (be32_to_cpup(p++) > maxlen)
229                         return NULL;
230                 p += 2; /* offset */
231
232                 if (p > end)
233                         return NULL;
234         }
235
236         return p;
237 }
238
239 /* Sanity check the Write list.
240  *
241  * Implementation limits:
242  * - This implementation supports only one Write chunk.
243  *
244  * Sanity checks:
245  * - Write list does not overflow buffer.
246  * - Segment size limited by largest NFS data payload.
247  *
248  * Returns pointer to the following Reply chunk.
249  */
250 static __be32 *xdr_check_write_list(__be32 *p, const __be32 *end)
251 {
252         u32 chcount;
253
254         chcount = 0;
255         while (*p++ != xdr_zero) {
256                 p = xdr_check_write_chunk(p, end, MAX_BYTES_WRITE_SEG);
257                 if (!p)
258                         return NULL;
259                 if (chcount++ > 1)
260                         return NULL;
261         }
262         return p;
263 }
264
265 /* Sanity check the Reply chunk.
266  *
267  * Sanity checks:
268  * - Reply chunk does not overflow buffer.
269  * - Segment size limited by largest NFS data payload.
270  *
271  * Returns pointer to the following RPC header.
272  */
273 static __be32 *xdr_check_reply_chunk(__be32 *p, const __be32 *end)
274 {
275         if (*p++ != xdr_zero) {
276                 p = xdr_check_write_chunk(p, end, MAX_BYTES_SPECIAL_SEG);
277                 if (!p)
278                         return NULL;
279         }
280         return p;
281 }
282
283 /* On entry, xdr->head[0].iov_base points to first byte in the
284  * RPC-over-RDMA header.
285  *
286  * On successful exit, head[0] points to first byte past the
287  * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
288  * The length of the RPC-over-RDMA header is returned.
289  *
290  * Assumptions:
291  * - The transport header is entirely contained in the head iovec.
292  */
293 static int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg)
294 {
295         __be32 *p, *end, *rdma_argp;
296         unsigned int hdr_len;
297         char *proc;
298
299         /* Verify that there's enough bytes for header + something */
300         if (rq_arg->len <= RPCRDMA_HDRLEN_ERR)
301                 goto out_short;
302
303         rdma_argp = rq_arg->head[0].iov_base;
304         if (*(rdma_argp + 1) != rpcrdma_version)
305                 goto out_version;
306
307         switch (*(rdma_argp + 3)) {
308         case rdma_msg:
309                 proc = "RDMA_MSG";
310                 break;
311         case rdma_nomsg:
312                 proc = "RDMA_NOMSG";
313                 break;
314
315         case rdma_done:
316                 goto out_drop;
317
318         case rdma_error:
319                 goto out_drop;
320
321         default:
322                 goto out_proc;
323         }
324
325         end = (__be32 *)((unsigned long)rdma_argp + rq_arg->len);
326         p = xdr_check_read_list(rdma_argp + 4, end);
327         if (!p)
328                 goto out_inval;
329         p = xdr_check_write_list(p, end);
330         if (!p)
331                 goto out_inval;
332         p = xdr_check_reply_chunk(p, end);
333         if (!p)
334                 goto out_inval;
335         if (p > end)
336                 goto out_inval;
337
338         rq_arg->head[0].iov_base = p;
339         hdr_len = (unsigned long)p - (unsigned long)rdma_argp;
340         rq_arg->head[0].iov_len -= hdr_len;
341         dprintk("svcrdma: received %s request for XID 0x%08x, hdr_len=%u\n",
342                 proc, be32_to_cpup(rdma_argp), hdr_len);
343         return hdr_len;
344
345 out_short:
346         dprintk("svcrdma: header too short = %d\n", rq_arg->len);
347         return -EINVAL;
348
349 out_version:
350         dprintk("svcrdma: bad xprt version: %u\n",
351                 be32_to_cpup(rdma_argp + 1));
352         return -EPROTONOSUPPORT;
353
354 out_drop:
355         dprintk("svcrdma: dropping RDMA_DONE/ERROR message\n");
356         return 0;
357
358 out_proc:
359         dprintk("svcrdma: bad rdma procedure (%u)\n",
360                 be32_to_cpup(rdma_argp + 3));
361         return -EINVAL;
362
363 out_inval:
364         dprintk("svcrdma: failed to parse transport header\n");
365         return -EINVAL;
366 }
367
368 static void rdma_read_complete(struct svc_rqst *rqstp,
369                                struct svc_rdma_op_ctxt *head)
370 {
371         int page_no;
372
373         /* Copy RPC pages */
374         for (page_no = 0; page_no < head->count; page_no++) {
375                 put_page(rqstp->rq_pages[page_no]);
376                 rqstp->rq_pages[page_no] = head->pages[page_no];
377         }
378
379         /* Point rq_arg.pages past header */
380         rqstp->rq_arg.pages = &rqstp->rq_pages[head->hdr_count];
381         rqstp->rq_arg.page_len = head->arg.page_len;
382
383         /* rq_respages starts after the last arg page */
384         rqstp->rq_respages = &rqstp->rq_pages[page_no];
385         rqstp->rq_next_page = rqstp->rq_respages + 1;
386
387         /* Rebuild rq_arg head and tail. */
388         rqstp->rq_arg.head[0] = head->arg.head[0];
389         rqstp->rq_arg.tail[0] = head->arg.tail[0];
390         rqstp->rq_arg.len = head->arg.len;
391         rqstp->rq_arg.buflen = head->arg.buflen;
392 }
393
394 static void svc_rdma_send_error(struct svcxprt_rdma *xprt,
395                                 __be32 *rdma_argp, int status)
396 {
397         struct svc_rdma_op_ctxt *ctxt;
398         __be32 *p, *err_msgp;
399         unsigned int length;
400         struct page *page;
401         int ret;
402
403         ret = svc_rdma_repost_recv(xprt, GFP_KERNEL);
404         if (ret)
405                 return;
406
407         page = alloc_page(GFP_KERNEL);
408         if (!page)
409                 return;
410         err_msgp = page_address(page);
411
412         p = err_msgp;
413         *p++ = *rdma_argp;
414         *p++ = *(rdma_argp + 1);
415         *p++ = xprt->sc_fc_credits;
416         *p++ = rdma_error;
417         if (status == -EPROTONOSUPPORT) {
418                 *p++ = err_vers;
419                 *p++ = rpcrdma_version;
420                 *p++ = rpcrdma_version;
421         } else {
422                 *p++ = err_chunk;
423         }
424         length = (unsigned long)p - (unsigned long)err_msgp;
425
426         /* Map transport header; no RPC message payload */
427         ctxt = svc_rdma_get_context(xprt);
428         ret = svc_rdma_map_reply_hdr(xprt, ctxt, err_msgp, length);
429         if (ret) {
430                 dprintk("svcrdma: Error %d mapping send for protocol error\n",
431                         ret);
432                 return;
433         }
434
435         ret = svc_rdma_post_send_wr(xprt, ctxt, 1, 0);
436         if (ret) {
437                 dprintk("svcrdma: Error %d posting send for protocol error\n",
438                         ret);
439                 svc_rdma_unmap_dma(ctxt);
440                 svc_rdma_put_context(ctxt, 1);
441         }
442 }
443
444 /* By convention, backchannel calls arrive via rdma_msg type
445  * messages, and never populate the chunk lists. This makes
446  * the RPC/RDMA header small and fixed in size, so it is
447  * straightforward to check the RPC header's direction field.
448  */
449 static bool svc_rdma_is_backchannel_reply(struct svc_xprt *xprt,
450                                           __be32 *rdma_resp)
451 {
452         __be32 *p;
453
454         if (!xprt->xpt_bc_xprt)
455                 return false;
456
457         p = rdma_resp + 3;
458         if (*p++ != rdma_msg)
459                 return false;
460
461         if (*p++ != xdr_zero)
462                 return false;
463         if (*p++ != xdr_zero)
464                 return false;
465         if (*p++ != xdr_zero)
466                 return false;
467
468         /* XID sanity */
469         if (*p++ != *rdma_resp)
470                 return false;
471         /* call direction */
472         if (*p == cpu_to_be32(RPC_CALL))
473                 return false;
474
475         return true;
476 }
477
478 /**
479  * svc_rdma_recvfrom - Receive an RPC call
480  * @rqstp: request structure into which to receive an RPC Call
481  *
482  * Returns:
483  *      The positive number of bytes in the RPC Call message,
484  *      %0 if there were no Calls ready to return,
485  *      %-EINVAL if the Read chunk data is too large,
486  *      %-ENOMEM if rdma_rw context pool was exhausted,
487  *      %-ENOTCONN if posting failed (connection is lost),
488  *      %-EIO if rdma_rw initialization failed (DMA mapping, etc).
489  *
490  * Called in a loop when XPT_DATA is set. XPT_DATA is cleared only
491  * when there are no remaining ctxt's to process.
492  *
493  * The next ctxt is removed from the "receive" lists.
494  *
495  * - If the ctxt completes a Read, then finish assembling the Call
496  *   message and return the number of bytes in the message.
497  *
498  * - If the ctxt completes a Receive, then construct the Call
499  *   message from the contents of the Receive buffer.
500  *
501  *   - If there are no Read chunks in this message, then finish
502  *     assembling the Call message and return the number of bytes
503  *     in the message.
504  *
505  *   - If there are Read chunks in this message, post Read WRs to
506  *     pull that payload and return 0.
507  */
508 int svc_rdma_recvfrom(struct svc_rqst *rqstp)
509 {
510         struct svc_xprt *xprt = rqstp->rq_xprt;
511         struct svcxprt_rdma *rdma_xprt =
512                 container_of(xprt, struct svcxprt_rdma, sc_xprt);
513         struct svc_rdma_op_ctxt *ctxt;
514         __be32 *p;
515         int ret;
516
517         spin_lock(&rdma_xprt->sc_rq_dto_lock);
518         if (!list_empty(&rdma_xprt->sc_read_complete_q)) {
519                 ctxt = list_first_entry(&rdma_xprt->sc_read_complete_q,
520                                         struct svc_rdma_op_ctxt, list);
521                 list_del(&ctxt->list);
522                 spin_unlock(&rdma_xprt->sc_rq_dto_lock);
523                 rdma_read_complete(rqstp, ctxt);
524                 goto complete;
525         } else if (!list_empty(&rdma_xprt->sc_rq_dto_q)) {
526                 ctxt = list_first_entry(&rdma_xprt->sc_rq_dto_q,
527                                         struct svc_rdma_op_ctxt, list);
528                 list_del(&ctxt->list);
529         } else {
530                 /* No new incoming requests, terminate the loop */
531                 clear_bit(XPT_DATA, &xprt->xpt_flags);
532                 spin_unlock(&rdma_xprt->sc_rq_dto_lock);
533                 return 0;
534         }
535         spin_unlock(&rdma_xprt->sc_rq_dto_lock);
536
537         dprintk("svcrdma: recvfrom: ctxt=%p on xprt=%p, rqstp=%p\n",
538                 ctxt, rdma_xprt, rqstp);
539         atomic_inc(&rdma_stat_recv);
540
541         /* Build up the XDR from the receive buffers. */
542         rdma_build_arg_xdr(rqstp, ctxt, ctxt->byte_len);
543
544         /* Decode the RDMA header. */
545         p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
546         ret = svc_rdma_xdr_decode_req(&rqstp->rq_arg);
547         if (ret < 0)
548                 goto out_err;
549         if (ret == 0)
550                 goto out_drop;
551         rqstp->rq_xprt_hlen = ret;
552
553         if (svc_rdma_is_backchannel_reply(xprt, p)) {
554                 ret = svc_rdma_handle_bc_reply(xprt->xpt_bc_xprt, p,
555                                                &rqstp->rq_arg);
556                 svc_rdma_put_context(ctxt, 0);
557                 if (ret)
558                         goto repost;
559                 return ret;
560         }
561
562         p += rpcrdma_fixed_maxsz;
563         if (*p != xdr_zero)
564                 goto out_readchunk;
565
566 complete:
567         ret = rqstp->rq_arg.head[0].iov_len
568                 + rqstp->rq_arg.page_len
569                 + rqstp->rq_arg.tail[0].iov_len;
570         svc_rdma_put_context(ctxt, 0);
571         dprintk("svcrdma: ret=%d, rq_arg.len=%u, "
572                 "rq_arg.head[0].iov_base=%p, rq_arg.head[0].iov_len=%zd\n",
573                 ret, rqstp->rq_arg.len,
574                 rqstp->rq_arg.head[0].iov_base,
575                 rqstp->rq_arg.head[0].iov_len);
576         rqstp->rq_prot = IPPROTO_MAX;
577         svc_xprt_copy_addrs(rqstp, xprt);
578         return ret;
579
580 out_readchunk:
581         ret = svc_rdma_recv_read_chunk(rdma_xprt, rqstp, ctxt, p);
582         if (ret < 0)
583                 goto out_postfail;
584         return 0;
585
586 out_err:
587         svc_rdma_send_error(rdma_xprt, p, ret);
588         svc_rdma_put_context(ctxt, 0);
589         return 0;
590
591 out_postfail:
592         if (ret == -EINVAL)
593                 svc_rdma_send_error(rdma_xprt, p, ret);
594         svc_rdma_put_context(ctxt, 1);
595         return ret;
596
597 out_drop:
598         svc_rdma_put_context(ctxt, 1);
599 repost:
600         return svc_rdma_repost_recv(rdma_xprt, GFP_KERNEL);
601 }