\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 1 | /* |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 2 | * Copyright (c) 2003-2007 Network Appliance, Inc. All rights reserved. |
| 3 | * |
| 4 | * This software is available to you under a choice of one of two |
| 5 | * licenses. You may choose to be licensed under the terms of the GNU |
| 6 | * General Public License (GPL) Version 2, available from the file |
| 7 | * COPYING in the main directory of this source tree, or the BSD-type |
| 8 | * license below: |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * |
| 14 | * Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * |
| 17 | * Redistributions in binary form must reproduce the above |
| 18 | * copyright notice, this list of conditions and the following |
| 19 | * disclaimer in the documentation and/or other materials provided |
| 20 | * with the distribution. |
| 21 | * |
| 22 | * Neither the name of the Network Appliance, Inc. nor the names of |
| 23 | * its contributors may be used to endorse or promote products |
| 24 | * derived from this software without specific prior written |
| 25 | * permission. |
| 26 | * |
| 27 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 28 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 29 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 30 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 31 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 32 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 33 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 34 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 35 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 36 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 37 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | * rpc_rdma.c |
| 42 | * |
| 43 | * This file contains the guts of the RPC RDMA protocol, and |
| 44 | * does marshaling/unmarshaling, etc. It is also where interfacing |
| 45 | * to the Linux RPC framework lives. |
\"Talpey, Thomas\ | f58851e | 2007-09-10 13:50:12 -0400 | [diff] [blame] | 46 | */ |
| 47 | |
| 48 | #include "xprt_rdma.h" |
| 49 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 50 | #include <linux/highmem.h> |
| 51 | |
| 52 | #ifdef RPC_DEBUG |
| 53 | # define RPCDBG_FACILITY RPCDBG_TRANS |
| 54 | #endif |
| 55 | |
| 56 | enum rpcrdma_chunktype { |
| 57 | rpcrdma_noch = 0, |
| 58 | rpcrdma_readch, |
| 59 | rpcrdma_areadch, |
| 60 | rpcrdma_writech, |
| 61 | rpcrdma_replych |
| 62 | }; |
| 63 | |
| 64 | #ifdef RPC_DEBUG |
| 65 | static const char transfertypes[][12] = { |
| 66 | "pure inline", /* no chunks */ |
| 67 | " read chunk", /* some argument via rdma read */ |
| 68 | "*read chunk", /* entire request via rdma read */ |
| 69 | "write chunk", /* some result via rdma write */ |
| 70 | "reply chunk" /* entire reply via rdma write */ |
| 71 | }; |
| 72 | #endif |
| 73 | |
| 74 | /* |
| 75 | * Chunk assembly from upper layer xdr_buf. |
| 76 | * |
| 77 | * Prepare the passed-in xdr_buf into representation as RPC/RDMA chunk |
| 78 | * elements. Segments are then coalesced when registered, if possible |
| 79 | * within the selected memreg mode. |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 80 | */ |
| 81 | |
| 82 | static int |
Chuck Lever | 2a428b2 | 2007-10-26 13:30:43 -0400 | [diff] [blame] | 83 | rpcrdma_convert_iovs(struct xdr_buf *xdrbuf, unsigned int pos, |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 84 | enum rpcrdma_chunktype type, struct rpcrdma_mr_seg *seg, int nsegs) |
| 85 | { |
| 86 | int len, n = 0, p; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 87 | int page_base; |
| 88 | struct page **ppages; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 89 | |
| 90 | if (pos == 0 && xdrbuf->head[0].iov_len) { |
| 91 | seg[n].mr_page = NULL; |
| 92 | seg[n].mr_offset = xdrbuf->head[0].iov_base; |
| 93 | seg[n].mr_len = xdrbuf->head[0].iov_len; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 94 | ++n; |
| 95 | } |
| 96 | |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 97 | len = xdrbuf->page_len; |
| 98 | ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT); |
| 99 | page_base = xdrbuf->page_base & ~PAGE_MASK; |
| 100 | p = 0; |
| 101 | while (len && n < nsegs) { |
Shirley Ma | 196c699 | 2014-05-28 10:34:24 -0400 | [diff] [blame] | 102 | if (!ppages[p]) { |
| 103 | /* alloc the pagelist for receiving buffer */ |
| 104 | ppages[p] = alloc_page(GFP_ATOMIC); |
| 105 | if (!ppages[p]) |
| 106 | return 0; |
| 107 | } |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 108 | seg[n].mr_page = ppages[p]; |
| 109 | seg[n].mr_offset = (void *)(unsigned long) page_base; |
| 110 | seg[n].mr_len = min_t(u32, PAGE_SIZE - page_base, len); |
| 111 | BUG_ON(seg[n].mr_len > PAGE_SIZE); |
| 112 | len -= seg[n].mr_len; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 113 | ++n; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 114 | ++p; |
| 115 | page_base = 0; /* page offset only applies to first page */ |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 118 | /* Message overflows the seg array */ |
| 119 | if (len && n == nsegs) |
| 120 | return 0; |
| 121 | |
James Lentini | 50e1092 | 2007-12-10 11:24:48 -0500 | [diff] [blame] | 122 | if (xdrbuf->tail[0].iov_len) { |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 123 | /* the rpcrdma protocol allows us to omit any trailing |
| 124 | * xdr pad bytes, saving the server an RDMA operation. */ |
| 125 | if (xdrbuf->tail[0].iov_len < 4 && xprt_rdma_pad_optimize) |
| 126 | return n; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 127 | if (n == nsegs) |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 128 | /* Tail remains, but we're out of segments */ |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 129 | return 0; |
| 130 | seg[n].mr_page = NULL; |
| 131 | seg[n].mr_offset = xdrbuf->tail[0].iov_base; |
| 132 | seg[n].mr_len = xdrbuf->tail[0].iov_len; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 133 | ++n; |
| 134 | } |
| 135 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 136 | return n; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Create read/write chunk lists, and reply chunks, for RDMA |
| 141 | * |
| 142 | * Assume check against THRESHOLD has been done, and chunks are required. |
| 143 | * Assume only encoding one list entry for read|write chunks. The NFSv3 |
| 144 | * protocol is simple enough to allow this as it only has a single "bulk |
| 145 | * result" in each procedure - complicated NFSv4 COMPOUNDs are not. (The |
| 146 | * RDMA/Sessions NFSv4 proposal addresses this for future v4 revs.) |
| 147 | * |
| 148 | * When used for a single reply chunk (which is a special write |
| 149 | * chunk used for the entire reply, rather than just the data), it |
| 150 | * is used primarily for READDIR and READLINK which would otherwise |
| 151 | * be severely size-limited by a small rdma inline read max. The server |
| 152 | * response will come back as an RDMA Write, followed by a message |
| 153 | * of type RDMA_NOMSG carrying the xid and length. As a result, reply |
| 154 | * chunks do not provide data alignment, however they do not require |
| 155 | * "fixup" (moving the response to the upper layer buffer) either. |
| 156 | * |
| 157 | * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64): |
| 158 | * |
| 159 | * Read chunklist (a linked list): |
| 160 | * N elements, position P (same P for all chunks of same arg!): |
| 161 | * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0 |
| 162 | * |
| 163 | * Write chunklist (a list of (one) counted array): |
| 164 | * N elements: |
| 165 | * 1 - N - HLOO - HLOO - ... - HLOO - 0 |
| 166 | * |
| 167 | * Reply chunk (a counted array): |
| 168 | * N elements: |
| 169 | * 1 - N - HLOO - HLOO - ... - HLOO |
| 170 | */ |
| 171 | |
| 172 | static unsigned int |
| 173 | rpcrdma_create_chunks(struct rpc_rqst *rqst, struct xdr_buf *target, |
| 174 | struct rpcrdma_msg *headerp, enum rpcrdma_chunktype type) |
| 175 | { |
| 176 | struct rpcrdma_req *req = rpcr_to_rdmar(rqst); |
Trond Myklebust | a4f0835 | 2013-01-08 09:10:21 -0500 | [diff] [blame] | 177 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 178 | int nsegs, nchunks = 0; |
Chuck Lever | 2a428b2 | 2007-10-26 13:30:43 -0400 | [diff] [blame] | 179 | unsigned int pos; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 180 | struct rpcrdma_mr_seg *seg = req->rl_segments; |
| 181 | struct rpcrdma_read_chunk *cur_rchunk = NULL; |
| 182 | struct rpcrdma_write_array *warray = NULL; |
| 183 | struct rpcrdma_write_chunk *cur_wchunk = NULL; |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 184 | __be32 *iptr = headerp->rm_body.rm_chunks; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 185 | |
| 186 | if (type == rpcrdma_readch || type == rpcrdma_areadch) { |
| 187 | /* a read chunk - server will RDMA Read our memory */ |
| 188 | cur_rchunk = (struct rpcrdma_read_chunk *) iptr; |
| 189 | } else { |
| 190 | /* a write or reply chunk - server will RDMA Write our memory */ |
| 191 | *iptr++ = xdr_zero; /* encode a NULL read chunk list */ |
| 192 | if (type == rpcrdma_replych) |
| 193 | *iptr++ = xdr_zero; /* a NULL write chunk list */ |
| 194 | warray = (struct rpcrdma_write_array *) iptr; |
| 195 | cur_wchunk = (struct rpcrdma_write_chunk *) (warray + 1); |
| 196 | } |
| 197 | |
| 198 | if (type == rpcrdma_replych || type == rpcrdma_areadch) |
| 199 | pos = 0; |
| 200 | else |
| 201 | pos = target->head[0].iov_len; |
| 202 | |
| 203 | nsegs = rpcrdma_convert_iovs(target, pos, type, seg, RPCRDMA_MAX_SEGS); |
| 204 | if (nsegs == 0) |
| 205 | return 0; |
| 206 | |
| 207 | do { |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 208 | int n = rpcrdma_register_external(seg, nsegs, |
| 209 | cur_wchunk != NULL, r_xprt); |
| 210 | if (n <= 0) |
| 211 | goto out; |
| 212 | if (cur_rchunk) { /* read */ |
| 213 | cur_rchunk->rc_discrim = xdr_one; |
| 214 | /* all read chunks have the same "position" */ |
| 215 | cur_rchunk->rc_position = htonl(pos); |
| 216 | cur_rchunk->rc_target.rs_handle = htonl(seg->mr_rkey); |
| 217 | cur_rchunk->rc_target.rs_length = htonl(seg->mr_len); |
| 218 | xdr_encode_hyper( |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 219 | (__be32 *)&cur_rchunk->rc_target.rs_offset, |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 220 | seg->mr_base); |
| 221 | dprintk("RPC: %s: read chunk " |
Chuck Lever | 2a428b2 | 2007-10-26 13:30:43 -0400 | [diff] [blame] | 222 | "elem %d@0x%llx:0x%x pos %u (%s)\n", __func__, |
Stephen Rothwell | e08a132 | 2007-10-30 00:44:32 -0700 | [diff] [blame] | 223 | seg->mr_len, (unsigned long long)seg->mr_base, |
| 224 | seg->mr_rkey, pos, n < nsegs ? "more" : "last"); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 225 | cur_rchunk++; |
| 226 | r_xprt->rx_stats.read_chunk_count++; |
| 227 | } else { /* write/reply */ |
| 228 | cur_wchunk->wc_target.rs_handle = htonl(seg->mr_rkey); |
| 229 | cur_wchunk->wc_target.rs_length = htonl(seg->mr_len); |
| 230 | xdr_encode_hyper( |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 231 | (__be32 *)&cur_wchunk->wc_target.rs_offset, |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 232 | seg->mr_base); |
| 233 | dprintk("RPC: %s: %s chunk " |
| 234 | "elem %d@0x%llx:0x%x (%s)\n", __func__, |
| 235 | (type == rpcrdma_replych) ? "reply" : "write", |
Stephen Rothwell | e08a132 | 2007-10-30 00:44:32 -0700 | [diff] [blame] | 236 | seg->mr_len, (unsigned long long)seg->mr_base, |
| 237 | seg->mr_rkey, n < nsegs ? "more" : "last"); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 238 | cur_wchunk++; |
| 239 | if (type == rpcrdma_replych) |
| 240 | r_xprt->rx_stats.reply_chunk_count++; |
| 241 | else |
| 242 | r_xprt->rx_stats.write_chunk_count++; |
| 243 | r_xprt->rx_stats.total_rdma_request += seg->mr_len; |
| 244 | } |
| 245 | nchunks++; |
| 246 | seg += n; |
| 247 | nsegs -= n; |
| 248 | } while (nsegs); |
| 249 | |
| 250 | /* success. all failures return above */ |
| 251 | req->rl_nchunks = nchunks; |
| 252 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 253 | /* |
| 254 | * finish off header. If write, marshal discrim and nchunks. |
| 255 | */ |
| 256 | if (cur_rchunk) { |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 257 | iptr = (__be32 *) cur_rchunk; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 258 | *iptr++ = xdr_zero; /* finish the read chunk list */ |
| 259 | *iptr++ = xdr_zero; /* encode a NULL write chunk list */ |
| 260 | *iptr++ = xdr_zero; /* encode a NULL reply chunk */ |
| 261 | } else { |
| 262 | warray->wc_discrim = xdr_one; |
| 263 | warray->wc_nchunks = htonl(nchunks); |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 264 | iptr = (__be32 *) cur_wchunk; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 265 | if (type == rpcrdma_writech) { |
| 266 | *iptr++ = xdr_zero; /* finish the write chunk list */ |
| 267 | *iptr++ = xdr_zero; /* encode a NULL reply chunk */ |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Return header size. |
| 273 | */ |
| 274 | return (unsigned char *)iptr - (unsigned char *)headerp; |
| 275 | |
| 276 | out: |
| 277 | for (pos = 0; nchunks--;) |
| 278 | pos += rpcrdma_deregister_external( |
Chuck Lever | 13c9ff8 | 2014-05-28 10:33:08 -0400 | [diff] [blame] | 279 | &req->rl_segments[pos], r_xprt); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Copy write data inline. |
| 285 | * This function is used for "small" requests. Data which is passed |
| 286 | * to RPC via iovecs (or page list) is copied directly into the |
| 287 | * pre-registered memory buffer for this request. For small amounts |
| 288 | * of data, this is efficient. The cutoff value is tunable. |
| 289 | */ |
| 290 | static int |
| 291 | rpcrdma_inline_pullup(struct rpc_rqst *rqst, int pad) |
| 292 | { |
| 293 | int i, npages, curlen; |
| 294 | int copy_len; |
| 295 | unsigned char *srcp, *destp; |
| 296 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt); |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 297 | int page_base; |
| 298 | struct page **ppages; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 299 | |
| 300 | destp = rqst->rq_svec[0].iov_base; |
| 301 | curlen = rqst->rq_svec[0].iov_len; |
| 302 | destp += curlen; |
| 303 | /* |
| 304 | * Do optional padding where it makes sense. Alignment of write |
| 305 | * payload can help the server, if our setting is accurate. |
| 306 | */ |
| 307 | pad -= (curlen + 36/*sizeof(struct rpcrdma_msg_padded)*/); |
| 308 | if (pad < 0 || rqst->rq_slen - curlen < RPCRDMA_INLINE_PAD_THRESH) |
| 309 | pad = 0; /* don't pad this request */ |
| 310 | |
| 311 | dprintk("RPC: %s: pad %d destp 0x%p len %d hdrlen %d\n", |
| 312 | __func__, pad, destp, rqst->rq_slen, curlen); |
| 313 | |
| 314 | copy_len = rqst->rq_snd_buf.page_len; |
Tom Talpey | b38ab40 | 2009-03-11 14:37:55 -0400 | [diff] [blame] | 315 | |
| 316 | if (rqst->rq_snd_buf.tail[0].iov_len) { |
| 317 | curlen = rqst->rq_snd_buf.tail[0].iov_len; |
| 318 | if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) { |
| 319 | memmove(destp + copy_len, |
| 320 | rqst->rq_snd_buf.tail[0].iov_base, curlen); |
| 321 | r_xprt->rx_stats.pullup_copy_count += curlen; |
| 322 | } |
| 323 | dprintk("RPC: %s: tail destp 0x%p len %d\n", |
| 324 | __func__, destp + copy_len, curlen); |
| 325 | rqst->rq_svec[0].iov_len += curlen; |
| 326 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 327 | r_xprt->rx_stats.pullup_copy_count += copy_len; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 328 | |
| 329 | page_base = rqst->rq_snd_buf.page_base; |
| 330 | ppages = rqst->rq_snd_buf.pages + (page_base >> PAGE_SHIFT); |
| 331 | page_base &= ~PAGE_MASK; |
| 332 | npages = PAGE_ALIGN(page_base+copy_len) >> PAGE_SHIFT; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 333 | for (i = 0; copy_len && i < npages; i++) { |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 334 | curlen = PAGE_SIZE - page_base; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 335 | if (curlen > copy_len) |
| 336 | curlen = copy_len; |
| 337 | dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n", |
| 338 | __func__, i, destp, copy_len, curlen); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 339 | srcp = kmap_atomic(ppages[i]); |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 340 | memcpy(destp, srcp+page_base, curlen); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 341 | kunmap_atomic(srcp); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 342 | rqst->rq_svec[0].iov_len += curlen; |
| 343 | destp += curlen; |
| 344 | copy_len -= curlen; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 345 | page_base = 0; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 346 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 347 | /* header now contains entire send message */ |
| 348 | return pad; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Marshal a request: the primary job of this routine is to choose |
| 353 | * the transfer modes. See comments below. |
| 354 | * |
| 355 | * Uses multiple RDMA IOVs for a request: |
| 356 | * [0] -- RPC RDMA header, which uses memory from the *start* of the |
| 357 | * preregistered buffer that already holds the RPC data in |
| 358 | * its middle. |
| 359 | * [1] -- the RPC header/data, marshaled by RPC and the NFS protocol. |
| 360 | * [2] -- optional padding. |
| 361 | * [3] -- if padded, header only in [1] and data here. |
| 362 | */ |
| 363 | |
| 364 | int |
| 365 | rpcrdma_marshal_req(struct rpc_rqst *rqst) |
| 366 | { |
Trond Myklebust | a4f0835 | 2013-01-08 09:10:21 -0500 | [diff] [blame] | 367 | struct rpc_xprt *xprt = rqst->rq_xprt; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 368 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); |
| 369 | struct rpcrdma_req *req = rpcr_to_rdmar(rqst); |
| 370 | char *base; |
| 371 | size_t hdrlen, rpclen, padlen; |
| 372 | enum rpcrdma_chunktype rtype, wtype; |
| 373 | struct rpcrdma_msg *headerp; |
| 374 | |
| 375 | /* |
| 376 | * rpclen gets amount of data in first buffer, which is the |
| 377 | * pre-registered buffer. |
| 378 | */ |
| 379 | base = rqst->rq_svec[0].iov_base; |
| 380 | rpclen = rqst->rq_svec[0].iov_len; |
| 381 | |
| 382 | /* build RDMA header in private area at front */ |
| 383 | headerp = (struct rpcrdma_msg *) req->rl_base; |
| 384 | /* don't htonl XID, it's already done in request */ |
| 385 | headerp->rm_xid = rqst->rq_xid; |
| 386 | headerp->rm_vers = xdr_one; |
| 387 | headerp->rm_credit = htonl(r_xprt->rx_buf.rb_max_requests); |
YOSHIFUJI Hideaki | 8d61443 | 2007-12-12 03:55:42 +0900 | [diff] [blame] | 388 | headerp->rm_type = htonl(RDMA_MSG); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 389 | |
| 390 | /* |
| 391 | * Chunks needed for results? |
| 392 | * |
| 393 | * o If the expected result is under the inline threshold, all ops |
| 394 | * return as inline (but see later). |
| 395 | * o Large non-read ops return as a single reply chunk. |
| 396 | * o Large read ops return data as write chunk(s), header as inline. |
| 397 | * |
| 398 | * Note: the NFS code sending down multiple result segments implies |
| 399 | * the op is one of read, readdir[plus], readlink or NFSv4 getacl. |
| 400 | */ |
| 401 | |
| 402 | /* |
| 403 | * This code can handle read chunks, write chunks OR reply |
| 404 | * chunks -- only one type. If the request is too big to fit |
| 405 | * inline, then we will choose read chunks. If the request is |
| 406 | * a READ, then use write chunks to separate the file data |
| 407 | * into pages; otherwise use reply chunks. |
| 408 | */ |
| 409 | if (rqst->rq_rcv_buf.buflen <= RPCRDMA_INLINE_READ_THRESHOLD(rqst)) |
| 410 | wtype = rpcrdma_noch; |
| 411 | else if (rqst->rq_rcv_buf.page_len == 0) |
| 412 | wtype = rpcrdma_replych; |
| 413 | else if (rqst->rq_rcv_buf.flags & XDRBUF_READ) |
| 414 | wtype = rpcrdma_writech; |
| 415 | else |
| 416 | wtype = rpcrdma_replych; |
| 417 | |
| 418 | /* |
| 419 | * Chunks needed for arguments? |
| 420 | * |
| 421 | * o If the total request is under the inline threshold, all ops |
| 422 | * are sent as inline. |
| 423 | * o Large non-write ops are sent with the entire message as a |
| 424 | * single read chunk (protocol 0-position special case). |
| 425 | * o Large write ops transmit data as read chunk(s), header as |
| 426 | * inline. |
| 427 | * |
| 428 | * Note: the NFS code sending down multiple argument segments |
| 429 | * implies the op is a write. |
| 430 | * TBD check NFSv4 setacl |
| 431 | */ |
| 432 | if (rqst->rq_snd_buf.len <= RPCRDMA_INLINE_WRITE_THRESHOLD(rqst)) |
| 433 | rtype = rpcrdma_noch; |
| 434 | else if (rqst->rq_snd_buf.page_len == 0) |
| 435 | rtype = rpcrdma_areadch; |
| 436 | else |
| 437 | rtype = rpcrdma_readch; |
| 438 | |
| 439 | /* The following simplification is not true forever */ |
| 440 | if (rtype != rpcrdma_noch && wtype == rpcrdma_replych) |
| 441 | wtype = rpcrdma_noch; |
| 442 | BUG_ON(rtype != rpcrdma_noch && wtype != rpcrdma_noch); |
| 443 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 444 | hdrlen = 28; /*sizeof *headerp;*/ |
| 445 | padlen = 0; |
| 446 | |
| 447 | /* |
| 448 | * Pull up any extra send data into the preregistered buffer. |
| 449 | * When padding is in use and applies to the transfer, insert |
| 450 | * it and change the message type. |
| 451 | */ |
| 452 | if (rtype == rpcrdma_noch) { |
| 453 | |
| 454 | padlen = rpcrdma_inline_pullup(rqst, |
| 455 | RPCRDMA_INLINE_PAD_VALUE(rqst)); |
| 456 | |
| 457 | if (padlen) { |
YOSHIFUJI Hideaki | 8d61443 | 2007-12-12 03:55:42 +0900 | [diff] [blame] | 458 | headerp->rm_type = htonl(RDMA_MSGP); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 459 | headerp->rm_body.rm_padded.rm_align = |
| 460 | htonl(RPCRDMA_INLINE_PAD_VALUE(rqst)); |
| 461 | headerp->rm_body.rm_padded.rm_thresh = |
YOSHIFUJI Hideaki | 8d61443 | 2007-12-12 03:55:42 +0900 | [diff] [blame] | 462 | htonl(RPCRDMA_INLINE_PAD_THRESH); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 463 | headerp->rm_body.rm_padded.rm_pempty[0] = xdr_zero; |
| 464 | headerp->rm_body.rm_padded.rm_pempty[1] = xdr_zero; |
| 465 | headerp->rm_body.rm_padded.rm_pempty[2] = xdr_zero; |
| 466 | hdrlen += 2 * sizeof(u32); /* extra words in padhdr */ |
| 467 | BUG_ON(wtype != rpcrdma_noch); |
| 468 | |
| 469 | } else { |
| 470 | headerp->rm_body.rm_nochunks.rm_empty[0] = xdr_zero; |
| 471 | headerp->rm_body.rm_nochunks.rm_empty[1] = xdr_zero; |
| 472 | headerp->rm_body.rm_nochunks.rm_empty[2] = xdr_zero; |
| 473 | /* new length after pullup */ |
| 474 | rpclen = rqst->rq_svec[0].iov_len; |
| 475 | /* |
| 476 | * Currently we try to not actually use read inline. |
| 477 | * Reply chunks have the desirable property that |
| 478 | * they land, packed, directly in the target buffers |
| 479 | * without headers, so they require no fixup. The |
| 480 | * additional RDMA Write op sends the same amount |
| 481 | * of data, streams on-the-wire and adds no overhead |
| 482 | * on receive. Therefore, we request a reply chunk |
| 483 | * for non-writes wherever feasible and efficient. |
| 484 | */ |
Chuck Lever | 0ac531c | 2014-05-28 10:32:43 -0400 | [diff] [blame] | 485 | if (wtype == rpcrdma_noch) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 486 | wtype = rpcrdma_replych; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Marshal chunks. This routine will return the header length |
| 492 | * consumed by marshaling. |
| 493 | */ |
| 494 | if (rtype != rpcrdma_noch) { |
| 495 | hdrlen = rpcrdma_create_chunks(rqst, |
| 496 | &rqst->rq_snd_buf, headerp, rtype); |
| 497 | wtype = rtype; /* simplify dprintk */ |
| 498 | |
| 499 | } else if (wtype != rpcrdma_noch) { |
| 500 | hdrlen = rpcrdma_create_chunks(rqst, |
| 501 | &rqst->rq_rcv_buf, headerp, wtype); |
| 502 | } |
| 503 | |
| 504 | if (hdrlen == 0) |
| 505 | return -1; |
| 506 | |
Tom Talpey | 5f37d56 | 2008-10-09 15:01:52 -0400 | [diff] [blame] | 507 | dprintk("RPC: %s: %s: hdrlen %zd rpclen %zd padlen %zd" |
| 508 | " headerp 0x%p base 0x%p lkey 0x%x\n", |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 509 | __func__, transfertypes[wtype], hdrlen, rpclen, padlen, |
| 510 | headerp, base, req->rl_iov.lkey); |
| 511 | |
| 512 | /* |
| 513 | * initialize send_iov's - normally only two: rdma chunk header and |
| 514 | * single preregistered RPC header buffer, but if padding is present, |
| 515 | * then use a preregistered (and zeroed) pad buffer between the RPC |
| 516 | * header and any write data. In all non-rdma cases, any following |
| 517 | * data has been copied into the RPC header buffer. |
| 518 | */ |
| 519 | req->rl_send_iov[0].addr = req->rl_iov.addr; |
| 520 | req->rl_send_iov[0].length = hdrlen; |
| 521 | req->rl_send_iov[0].lkey = req->rl_iov.lkey; |
| 522 | |
| 523 | req->rl_send_iov[1].addr = req->rl_iov.addr + (base - req->rl_base); |
| 524 | req->rl_send_iov[1].length = rpclen; |
| 525 | req->rl_send_iov[1].lkey = req->rl_iov.lkey; |
| 526 | |
| 527 | req->rl_niovs = 2; |
| 528 | |
| 529 | if (padlen) { |
| 530 | struct rpcrdma_ep *ep = &r_xprt->rx_ep; |
| 531 | |
| 532 | req->rl_send_iov[2].addr = ep->rep_pad.addr; |
| 533 | req->rl_send_iov[2].length = padlen; |
| 534 | req->rl_send_iov[2].lkey = ep->rep_pad.lkey; |
| 535 | |
| 536 | req->rl_send_iov[3].addr = req->rl_send_iov[1].addr + rpclen; |
| 537 | req->rl_send_iov[3].length = rqst->rq_slen - rpclen; |
| 538 | req->rl_send_iov[3].lkey = req->rl_iov.lkey; |
| 539 | |
| 540 | req->rl_niovs = 4; |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | /* |
| 547 | * Chase down a received write or reply chunklist to get length |
| 548 | * RDMA'd by server. See map at rpcrdma_create_chunks()! :-) |
| 549 | */ |
| 550 | static int |
Chuck Lever | d4b37ff | 2007-10-26 13:30:49 -0400 | [diff] [blame] | 551 | rpcrdma_count_chunks(struct rpcrdma_rep *rep, unsigned int max, int wrchunk, __be32 **iptrp) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 552 | { |
| 553 | unsigned int i, total_len; |
| 554 | struct rpcrdma_write_chunk *cur_wchunk; |
| 555 | |
| 556 | i = ntohl(**iptrp); /* get array count */ |
| 557 | if (i > max) |
| 558 | return -1; |
| 559 | cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1); |
| 560 | total_len = 0; |
| 561 | while (i--) { |
| 562 | struct rpcrdma_segment *seg = &cur_wchunk->wc_target; |
| 563 | ifdebug(FACILITY) { |
| 564 | u64 off; |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 565 | xdr_decode_hyper((__be32 *)&seg->rs_offset, &off); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 566 | dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n", |
| 567 | __func__, |
| 568 | ntohl(seg->rs_length), |
Stephen Rothwell | e08a132 | 2007-10-30 00:44:32 -0700 | [diff] [blame] | 569 | (unsigned long long)off, |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 570 | ntohl(seg->rs_handle)); |
| 571 | } |
| 572 | total_len += ntohl(seg->rs_length); |
| 573 | ++cur_wchunk; |
| 574 | } |
| 575 | /* check and adjust for properly terminated write chunk */ |
| 576 | if (wrchunk) { |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 577 | __be32 *w = (__be32 *) cur_wchunk; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 578 | if (*w++ != xdr_zero) |
| 579 | return -1; |
| 580 | cur_wchunk = (struct rpcrdma_write_chunk *) w; |
| 581 | } |
| 582 | if ((char *) cur_wchunk > rep->rr_base + rep->rr_len) |
| 583 | return -1; |
| 584 | |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 585 | *iptrp = (__be32 *) cur_wchunk; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 586 | return total_len; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Scatter inline received data back into provided iov's. |
| 591 | */ |
| 592 | static void |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 593 | rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 594 | { |
| 595 | int i, npages, curlen, olen; |
| 596 | char *destp; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 597 | struct page **ppages; |
| 598 | int page_base; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 599 | |
| 600 | curlen = rqst->rq_rcv_buf.head[0].iov_len; |
| 601 | if (curlen > copy_len) { /* write chunk header fixup */ |
| 602 | curlen = copy_len; |
| 603 | rqst->rq_rcv_buf.head[0].iov_len = curlen; |
| 604 | } |
| 605 | |
| 606 | dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n", |
| 607 | __func__, srcp, copy_len, curlen); |
| 608 | |
| 609 | /* Shift pointer for first receive segment only */ |
| 610 | rqst->rq_rcv_buf.head[0].iov_base = srcp; |
| 611 | srcp += curlen; |
| 612 | copy_len -= curlen; |
| 613 | |
| 614 | olen = copy_len; |
| 615 | i = 0; |
| 616 | rpcx_to_rdmax(rqst->rq_xprt)->rx_stats.fixup_copy_count += olen; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 617 | page_base = rqst->rq_rcv_buf.page_base; |
| 618 | ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT); |
| 619 | page_base &= ~PAGE_MASK; |
| 620 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 621 | if (copy_len && rqst->rq_rcv_buf.page_len) { |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 622 | npages = PAGE_ALIGN(page_base + |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 623 | rqst->rq_rcv_buf.page_len) >> PAGE_SHIFT; |
| 624 | for (; i < npages; i++) { |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 625 | curlen = PAGE_SIZE - page_base; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 626 | if (curlen > copy_len) |
| 627 | curlen = copy_len; |
| 628 | dprintk("RPC: %s: page %d" |
| 629 | " srcp 0x%p len %d curlen %d\n", |
| 630 | __func__, i, srcp, copy_len, curlen); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 631 | destp = kmap_atomic(ppages[i]); |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 632 | memcpy(destp + page_base, srcp, curlen); |
| 633 | flush_dcache_page(ppages[i]); |
Cong Wang | b854178 | 2011-11-25 23:14:40 +0800 | [diff] [blame] | 634 | kunmap_atomic(destp); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 635 | srcp += curlen; |
| 636 | copy_len -= curlen; |
| 637 | if (copy_len == 0) |
| 638 | break; |
Tom Tucker | bd7ea31 | 2011-02-09 19:45:28 +0000 | [diff] [blame] | 639 | page_base = 0; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 640 | } |
Chuck Lever | 2b7bbc9 | 2014-03-12 12:51:30 -0400 | [diff] [blame] | 641 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 642 | |
| 643 | if (copy_len && rqst->rq_rcv_buf.tail[0].iov_len) { |
| 644 | curlen = copy_len; |
| 645 | if (curlen > rqst->rq_rcv_buf.tail[0].iov_len) |
| 646 | curlen = rqst->rq_rcv_buf.tail[0].iov_len; |
| 647 | if (rqst->rq_rcv_buf.tail[0].iov_base != srcp) |
Tom Talpey | b38ab40 | 2009-03-11 14:37:55 -0400 | [diff] [blame] | 648 | memmove(rqst->rq_rcv_buf.tail[0].iov_base, srcp, curlen); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 649 | dprintk("RPC: %s: tail srcp 0x%p len %d curlen %d\n", |
| 650 | __func__, srcp, copy_len, curlen); |
| 651 | rqst->rq_rcv_buf.tail[0].iov_len = curlen; |
| 652 | copy_len -= curlen; ++i; |
| 653 | } else |
| 654 | rqst->rq_rcv_buf.tail[0].iov_len = 0; |
| 655 | |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 656 | if (pad) { |
| 657 | /* implicit padding on terminal chunk */ |
| 658 | unsigned char *p = rqst->rq_rcv_buf.tail[0].iov_base; |
| 659 | while (pad--) |
| 660 | p[rqst->rq_rcv_buf.tail[0].iov_len++] = 0; |
| 661 | } |
| 662 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 663 | if (copy_len) |
| 664 | dprintk("RPC: %s: %d bytes in" |
| 665 | " %d extra segments (%d lost)\n", |
| 666 | __func__, olen, i, copy_len); |
| 667 | |
| 668 | /* TBD avoid a warning from call_decode() */ |
| 669 | rqst->rq_private_buf = rqst->rq_rcv_buf; |
| 670 | } |
| 671 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 672 | void |
Chuck Lever | 254f91e | 2014-05-28 10:32:17 -0400 | [diff] [blame] | 673 | rpcrdma_connect_worker(struct work_struct *work) |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 674 | { |
Chuck Lever | 254f91e | 2014-05-28 10:32:17 -0400 | [diff] [blame] | 675 | struct rpcrdma_ep *ep = |
| 676 | container_of(work, struct rpcrdma_ep, rep_connect_worker.work); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 677 | struct rpc_xprt *xprt = ep->rep_xprt; |
| 678 | |
| 679 | spin_lock_bh(&xprt->transport_lock); |
Tom Talpey | 575448b | 2008-10-09 15:00:40 -0400 | [diff] [blame] | 680 | if (++xprt->connect_cookie == 0) /* maintain a reserved value */ |
| 681 | ++xprt->connect_cookie; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 682 | if (ep->rep_connected > 0) { |
| 683 | if (!xprt_test_and_set_connected(xprt)) |
| 684 | xprt_wake_pending_tasks(xprt, 0); |
| 685 | } else { |
| 686 | if (xprt_test_and_clear_connected(xprt)) |
Tom Talpey | 926449b | 2008-10-09 15:01:21 -0400 | [diff] [blame] | 687 | xprt_wake_pending_tasks(xprt, -ENOTCONN); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 688 | } |
| 689 | spin_unlock_bh(&xprt->transport_lock); |
| 690 | } |
| 691 | |
| 692 | /* |
Chuck Lever | 254f91e | 2014-05-28 10:32:17 -0400 | [diff] [blame] | 693 | * This function is called when an async event is posted to |
| 694 | * the connection which changes the connection state. All it |
| 695 | * does at this point is mark the connection up/down, the rpc |
| 696 | * timers do the rest. |
| 697 | */ |
| 698 | void |
| 699 | rpcrdma_conn_func(struct rpcrdma_ep *ep) |
| 700 | { |
| 701 | schedule_delayed_work(&ep->rep_connect_worker, 0); |
| 702 | } |
| 703 | |
| 704 | /* |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 705 | * Called as a tasklet to do req/reply match and complete a request |
| 706 | * Errors must result in the RPC task either being awakened, or |
| 707 | * allowed to timeout, to discover the errors at that time. |
| 708 | */ |
| 709 | void |
| 710 | rpcrdma_reply_handler(struct rpcrdma_rep *rep) |
| 711 | { |
| 712 | struct rpcrdma_msg *headerp; |
| 713 | struct rpcrdma_req *req; |
| 714 | struct rpc_rqst *rqst; |
| 715 | struct rpc_xprt *xprt = rep->rr_xprt; |
| 716 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 717 | __be32 *iptr; |
Chuck Lever | b45ccfd | 2014-05-28 10:32:34 -0400 | [diff] [blame] | 718 | int rdmalen, status; |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 719 | |
| 720 | /* Check status. If bad, signal disconnect and return rep to pool */ |
| 721 | if (rep->rr_len == ~0U) { |
| 722 | rpcrdma_recv_buffer_put(rep); |
| 723 | if (r_xprt->rx_ep.rep_connected == 1) { |
| 724 | r_xprt->rx_ep.rep_connected = -EIO; |
| 725 | rpcrdma_conn_func(&r_xprt->rx_ep); |
| 726 | } |
| 727 | return; |
| 728 | } |
| 729 | if (rep->rr_len < 28) { |
| 730 | dprintk("RPC: %s: short/invalid reply\n", __func__); |
| 731 | goto repost; |
| 732 | } |
| 733 | headerp = (struct rpcrdma_msg *) rep->rr_base; |
| 734 | if (headerp->rm_vers != xdr_one) { |
| 735 | dprintk("RPC: %s: invalid version %d\n", |
| 736 | __func__, ntohl(headerp->rm_vers)); |
| 737 | goto repost; |
| 738 | } |
| 739 | |
| 740 | /* Get XID and try for a match. */ |
| 741 | spin_lock(&xprt->transport_lock); |
| 742 | rqst = xprt_lookup_rqst(xprt, headerp->rm_xid); |
| 743 | if (rqst == NULL) { |
| 744 | spin_unlock(&xprt->transport_lock); |
| 745 | dprintk("RPC: %s: reply 0x%p failed " |
| 746 | "to match any request xid 0x%08x len %d\n", |
| 747 | __func__, rep, headerp->rm_xid, rep->rr_len); |
| 748 | repost: |
| 749 | r_xprt->rx_stats.bad_reply_count++; |
| 750 | rep->rr_func = rpcrdma_reply_handler; |
| 751 | if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep)) |
| 752 | rpcrdma_recv_buffer_put(rep); |
| 753 | |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | /* get request object */ |
| 758 | req = rpcr_to_rdmar(rqst); |
Tom Tucker | 4a6862b | 2012-02-20 13:07:42 -0600 | [diff] [blame] | 759 | if (req->rl_reply) { |
| 760 | spin_unlock(&xprt->transport_lock); |
| 761 | dprintk("RPC: %s: duplicate reply 0x%p to RPC " |
| 762 | "request 0x%p: xid 0x%08x\n", __func__, rep, req, |
| 763 | headerp->rm_xid); |
| 764 | goto repost; |
| 765 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 766 | |
| 767 | dprintk("RPC: %s: reply 0x%p completes request 0x%p\n" |
| 768 | " RPC request 0x%p xid 0x%08x\n", |
| 769 | __func__, rep, req, rqst, headerp->rm_xid); |
| 770 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 771 | /* from here on, the reply is no longer an orphan */ |
| 772 | req->rl_reply = rep; |
| 773 | |
| 774 | /* check for expected message types */ |
| 775 | /* The order of some of these tests is important. */ |
| 776 | switch (headerp->rm_type) { |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 777 | case htonl(RDMA_MSG): |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 778 | /* never expect read chunks */ |
| 779 | /* never expect reply chunks (two ways to check) */ |
| 780 | /* never expect write chunks without having offered RDMA */ |
| 781 | if (headerp->rm_body.rm_chunks[0] != xdr_zero || |
| 782 | (headerp->rm_body.rm_chunks[1] == xdr_zero && |
| 783 | headerp->rm_body.rm_chunks[2] != xdr_zero) || |
| 784 | (headerp->rm_body.rm_chunks[1] != xdr_zero && |
| 785 | req->rl_nchunks == 0)) |
| 786 | goto badheader; |
| 787 | if (headerp->rm_body.rm_chunks[1] != xdr_zero) { |
| 788 | /* count any expected write chunks in read reply */ |
| 789 | /* start at write chunk array count */ |
| 790 | iptr = &headerp->rm_body.rm_chunks[2]; |
| 791 | rdmalen = rpcrdma_count_chunks(rep, |
| 792 | req->rl_nchunks, 1, &iptr); |
| 793 | /* check for validity, and no reply chunk after */ |
| 794 | if (rdmalen < 0 || *iptr++ != xdr_zero) |
| 795 | goto badheader; |
| 796 | rep->rr_len -= |
| 797 | ((unsigned char *)iptr - (unsigned char *)headerp); |
| 798 | status = rep->rr_len + rdmalen; |
| 799 | r_xprt->rx_stats.total_rdma_reply += rdmalen; |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 800 | /* special case - last chunk may omit padding */ |
| 801 | if (rdmalen &= 3) { |
| 802 | rdmalen = 4 - rdmalen; |
| 803 | status += rdmalen; |
| 804 | } |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 805 | } else { |
| 806 | /* else ordinary inline */ |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 807 | rdmalen = 0; |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 808 | iptr = (__be32 *)((unsigned char *)headerp + 28); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 809 | rep->rr_len -= 28; /*sizeof *headerp;*/ |
| 810 | status = rep->rr_len; |
| 811 | } |
| 812 | /* Fix up the rpc results for upper layer */ |
Tom Talpey | 9191ca3 | 2008-10-09 15:01:11 -0400 | [diff] [blame] | 813 | rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len, rdmalen); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 814 | break; |
| 815 | |
Arnaldo Carvalho de Melo | 6067804 | 2008-09-20 22:20:49 -0700 | [diff] [blame] | 816 | case htonl(RDMA_NOMSG): |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 817 | /* never expect read or write chunks, always reply chunks */ |
| 818 | if (headerp->rm_body.rm_chunks[0] != xdr_zero || |
| 819 | headerp->rm_body.rm_chunks[1] != xdr_zero || |
| 820 | headerp->rm_body.rm_chunks[2] != xdr_one || |
| 821 | req->rl_nchunks == 0) |
| 822 | goto badheader; |
Al Viro | 2d8a972 | 2007-10-29 04:37:58 +0000 | [diff] [blame] | 823 | iptr = (__be32 *)((unsigned char *)headerp + 28); |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 824 | rdmalen = rpcrdma_count_chunks(rep, req->rl_nchunks, 0, &iptr); |
| 825 | if (rdmalen < 0) |
| 826 | goto badheader; |
| 827 | r_xprt->rx_stats.total_rdma_reply += rdmalen; |
| 828 | /* Reply chunk buffer already is the reply vector - no fixup. */ |
| 829 | status = rdmalen; |
| 830 | break; |
| 831 | |
| 832 | badheader: |
| 833 | default: |
| 834 | dprintk("%s: invalid rpcrdma reply header (type %d):" |
| 835 | " chunks[012] == %d %d %d" |
| 836 | " expected chunks <= %d\n", |
| 837 | __func__, ntohl(headerp->rm_type), |
| 838 | headerp->rm_body.rm_chunks[0], |
| 839 | headerp->rm_body.rm_chunks[1], |
| 840 | headerp->rm_body.rm_chunks[2], |
| 841 | req->rl_nchunks); |
| 842 | status = -EIO; |
| 843 | r_xprt->rx_stats.bad_reply_count++; |
| 844 | break; |
| 845 | } |
| 846 | |
\"Talpey, Thomas\ | e960182 | 2007-09-10 13:50:42 -0400 | [diff] [blame] | 847 | dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n", |
| 848 | __func__, xprt, rqst, status); |
| 849 | xprt_complete_rqst(rqst->rq_task, status); |
| 850 | spin_unlock(&xprt->transport_lock); |
| 851 | } |