Linux 4.11-rc6
[linux/fpc-iii.git] / net / sunrpc / xprtrdma / svc_rdma_marshal.c
blob1c4aabf0f65772c13265421262feb030ab4a58ca
1 /*
2 * Copyright (c) 2016 Oracle. All rights reserved.
3 * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the BSD-type
9 * license below:
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
18 * Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials provided
21 * with the distribution.
23 * Neither the name of the Network Appliance, Inc. nor the names of
24 * its contributors may be used to endorse or promote products
25 * derived from this software without specific prior written
26 * permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 * Author: Tom Tucker <tom@opengridcomputing.com>
43 #include <linux/sunrpc/xdr.h>
44 #include <linux/sunrpc/debug.h>
45 #include <asm/unaligned.h>
46 #include <linux/sunrpc/rpc_rdma.h>
47 #include <linux/sunrpc/svc_rdma.h>
49 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
51 static __be32 *xdr_check_read_list(__be32 *p, __be32 *end)
53 __be32 *next;
55 while (*p++ != xdr_zero) {
56 next = p + rpcrdma_readchunk_maxsz - 1;
57 if (next > end)
58 return NULL;
59 p = next;
61 return p;
64 static __be32 *xdr_check_write_list(__be32 *p, __be32 *end)
66 __be32 *next;
68 while (*p++ != xdr_zero) {
69 next = p + 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
70 if (next > end)
71 return NULL;
72 p = next;
74 return p;
77 static __be32 *xdr_check_reply_chunk(__be32 *p, __be32 *end)
79 __be32 *next;
81 if (*p++ != xdr_zero) {
82 next = p + 1 + be32_to_cpup(p) * rpcrdma_segment_maxsz;
83 if (next > end)
84 return NULL;
85 p = next;
87 return p;
90 /**
91 * svc_rdma_xdr_decode_req - Parse incoming RPC-over-RDMA header
92 * @rq_arg: Receive buffer
94 * On entry, xdr->head[0].iov_base points to first byte in the
95 * RPC-over-RDMA header.
97 * On successful exit, head[0] points to first byte past the
98 * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
99 * The length of the RPC-over-RDMA header is returned.
101 int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg)
103 __be32 *p, *end, *rdma_argp;
104 unsigned int hdr_len;
106 /* Verify that there's enough bytes for header + something */
107 if (rq_arg->len <= RPCRDMA_HDRLEN_ERR)
108 goto out_short;
110 rdma_argp = rq_arg->head[0].iov_base;
111 if (*(rdma_argp + 1) != rpcrdma_version)
112 goto out_version;
114 switch (*(rdma_argp + 3)) {
115 case rdma_msg:
116 case rdma_nomsg:
117 break;
119 case rdma_done:
120 goto out_drop;
122 case rdma_error:
123 goto out_drop;
125 default:
126 goto out_proc;
129 end = (__be32 *)((unsigned long)rdma_argp + rq_arg->len);
130 p = xdr_check_read_list(rdma_argp + 4, end);
131 if (!p)
132 goto out_inval;
133 p = xdr_check_write_list(p, end);
134 if (!p)
135 goto out_inval;
136 p = xdr_check_reply_chunk(p, end);
137 if (!p)
138 goto out_inval;
139 if (p > end)
140 goto out_inval;
142 rq_arg->head[0].iov_base = p;
143 hdr_len = (unsigned long)p - (unsigned long)rdma_argp;
144 rq_arg->head[0].iov_len -= hdr_len;
145 return hdr_len;
147 out_short:
148 dprintk("svcrdma: header too short = %d\n", rq_arg->len);
149 return -EINVAL;
151 out_version:
152 dprintk("svcrdma: bad xprt version: %u\n",
153 be32_to_cpup(rdma_argp + 1));
154 return -EPROTONOSUPPORT;
156 out_drop:
157 dprintk("svcrdma: dropping RDMA_DONE/ERROR message\n");
158 return 0;
160 out_proc:
161 dprintk("svcrdma: bad rdma procedure (%u)\n",
162 be32_to_cpup(rdma_argp + 3));
163 return -EINVAL;
165 out_inval:
166 dprintk("svcrdma: failed to parse transport header\n");
167 return -EINVAL;
170 int svc_rdma_xdr_encode_error(struct svcxprt_rdma *xprt,
171 struct rpcrdma_msg *rmsgp,
172 enum rpcrdma_errcode err, __be32 *va)
174 __be32 *startp = va;
176 *va++ = rmsgp->rm_xid;
177 *va++ = rmsgp->rm_vers;
178 *va++ = xprt->sc_fc_credits;
179 *va++ = rdma_error;
180 *va++ = cpu_to_be32(err);
181 if (err == ERR_VERS) {
182 *va++ = rpcrdma_version;
183 *va++ = rpcrdma_version;
186 return (int)((unsigned long)va - (unsigned long)startp);
190 * svc_rdma_xdr_get_reply_hdr_length - Get length of Reply transport header
191 * @rdma_resp: buffer containing Reply transport header
193 * Returns length of transport header, in bytes.
195 unsigned int svc_rdma_xdr_get_reply_hdr_len(__be32 *rdma_resp)
197 unsigned int nsegs;
198 __be32 *p;
200 p = rdma_resp;
202 /* RPC-over-RDMA V1 replies never have a Read list. */
203 p += rpcrdma_fixed_maxsz + 1;
205 /* Skip Write list. */
206 while (*p++ != xdr_zero) {
207 nsegs = be32_to_cpup(p++);
208 p += nsegs * rpcrdma_segment_maxsz;
211 /* Skip Reply chunk. */
212 if (*p++ != xdr_zero) {
213 nsegs = be32_to_cpup(p++);
214 p += nsegs * rpcrdma_segment_maxsz;
217 return (unsigned long)p - (unsigned long)rdma_resp;
220 void svc_rdma_xdr_encode_write_list(struct rpcrdma_msg *rmsgp, int chunks)
222 struct rpcrdma_write_array *ary;
224 /* no read-list */
225 rmsgp->rm_body.rm_chunks[0] = xdr_zero;
227 /* write-array discrim */
228 ary = (struct rpcrdma_write_array *)
229 &rmsgp->rm_body.rm_chunks[1];
230 ary->wc_discrim = xdr_one;
231 ary->wc_nchunks = cpu_to_be32(chunks);
233 /* write-list terminator */
234 ary->wc_array[chunks].wc_target.rs_handle = xdr_zero;
236 /* reply-array discriminator */
237 ary->wc_array[chunks].wc_target.rs_length = xdr_zero;
240 void svc_rdma_xdr_encode_reply_array(struct rpcrdma_write_array *ary,
241 int chunks)
243 ary->wc_discrim = xdr_one;
244 ary->wc_nchunks = cpu_to_be32(chunks);
247 void svc_rdma_xdr_encode_array_chunk(struct rpcrdma_write_array *ary,
248 int chunk_no,
249 __be32 rs_handle,
250 __be64 rs_offset,
251 u32 write_len)
253 struct rpcrdma_segment *seg = &ary->wc_array[chunk_no].wc_target;
254 seg->rs_handle = rs_handle;
255 seg->rs_offset = rs_offset;
256 seg->rs_length = cpu_to_be32(write_len);