1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42#include <linux/sunrpc/xdr.h>
43#include <linux/sunrpc/debug.h>
44#include <asm/unaligned.h>
45#include <linux/sunrpc/rpc_rdma.h>
46#include <linux/sunrpc/svc_rdma.h>
47
48#define RPCDBG_FACILITY RPCDBG_SVCXPRT
49
50
51
52
53
54
55
56
57
58static __be32 *decode_read_list(__be32 *va, __be32 *vaend)
59{
60 struct rpcrdma_read_chunk *ch = (struct rpcrdma_read_chunk *)va;
61
62 while (ch->rc_discrim != xdr_zero) {
63 if (((unsigned long)ch + sizeof(struct rpcrdma_read_chunk)) >
64 (unsigned long)vaend) {
65 dprintk("svcrdma: vaend=%p, ch=%p\n", vaend, ch);
66 return NULL;
67 }
68 ch++;
69 }
70 return &ch->rc_position;
71}
72
73
74
75
76
77
78
79
80
81
82
83static __be32 *decode_write_list(__be32 *va, __be32 *vaend)
84{
85 unsigned long start, end;
86 int nchunks;
87
88 struct rpcrdma_write_array *ary =
89 (struct rpcrdma_write_array *)va;
90
91
92 if (ary->wc_discrim == xdr_zero)
93 return &ary->wc_nchunks;
94
95 if ((unsigned long)ary + sizeof(struct rpcrdma_write_array) >
96 (unsigned long)vaend) {
97 dprintk("svcrdma: ary=%p, vaend=%p\n", ary, vaend);
98 return NULL;
99 }
100 nchunks = be32_to_cpu(ary->wc_nchunks);
101
102 start = (unsigned long)&ary->wc_array[0];
103 end = (unsigned long)vaend;
104 if (nchunks < 0 ||
105 nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
106 (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
107 dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
108 ary, nchunks, vaend);
109 return NULL;
110 }
111
112
113
114
115 return &ary->wc_array[nchunks].wc_target.rs_length;
116}
117
118static __be32 *decode_reply_array(__be32 *va, __be32 *vaend)
119{
120 unsigned long start, end;
121 int nchunks;
122 struct rpcrdma_write_array *ary =
123 (struct rpcrdma_write_array *)va;
124
125
126 if (ary->wc_discrim == xdr_zero)
127 return &ary->wc_nchunks;
128
129 if ((unsigned long)ary + sizeof(struct rpcrdma_write_array) >
130 (unsigned long)vaend) {
131 dprintk("svcrdma: ary=%p, vaend=%p\n", ary, vaend);
132 return NULL;
133 }
134 nchunks = be32_to_cpu(ary->wc_nchunks);
135
136 start = (unsigned long)&ary->wc_array[0];
137 end = (unsigned long)vaend;
138 if (nchunks < 0 ||
139 nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
140 (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
141 dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
142 ary, nchunks, vaend);
143 return NULL;
144 }
145 return (__be32 *)&ary->wc_array[nchunks];
146}
147
148
149
150
151
152
153
154
155
156
157
158
159int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg)
160{
161 struct rpcrdma_msg *rmsgp;
162 __be32 *va, *vaend;
163 unsigned int len;
164 u32 hdr_len;
165
166
167 if (rq_arg->len <= RPCRDMA_HDRLEN_ERR) {
168 dprintk("svcrdma: header too short = %d\n",
169 rq_arg->len);
170 return -EINVAL;
171 }
172
173 rmsgp = (struct rpcrdma_msg *)rq_arg->head[0].iov_base;
174 if (rmsgp->rm_vers != rpcrdma_version) {
175 dprintk("%s: bad version %u\n", __func__,
176 be32_to_cpu(rmsgp->rm_vers));
177 return -EPROTONOSUPPORT;
178 }
179
180 switch (be32_to_cpu(rmsgp->rm_type)) {
181 case RDMA_MSG:
182 case RDMA_NOMSG:
183 break;
184
185 case RDMA_DONE:
186
187 dprintk("svcrdma: dropping RDMA_DONE message\n");
188 return 0;
189
190 case RDMA_ERROR:
191
192
193
194 dprintk("svcrdma: dropping RDMA_ERROR message\n");
195 return 0;
196
197 case RDMA_MSGP:
198
199 rmsgp->rm_body.rm_padded.rm_align =
200 be32_to_cpu(rmsgp->rm_body.rm_padded.rm_align);
201 rmsgp->rm_body.rm_padded.rm_thresh =
202 be32_to_cpu(rmsgp->rm_body.rm_padded.rm_thresh);
203
204 va = &rmsgp->rm_body.rm_padded.rm_pempty[4];
205 rq_arg->head[0].iov_base = va;
206 len = (u32)((unsigned long)va - (unsigned long)rmsgp);
207 rq_arg->head[0].iov_len -= len;
208 if (len > rq_arg->len)
209 return -EINVAL;
210 return len;
211 default:
212 dprintk("svcrdma: bad rdma procedure (%u)\n",
213 be32_to_cpu(rmsgp->rm_type));
214 return -EINVAL;
215 }
216
217
218
219
220 va = &rmsgp->rm_body.rm_chunks[0];
221 vaend = (__be32 *)((unsigned long)rmsgp + rq_arg->len);
222 va = decode_read_list(va, vaend);
223 if (!va) {
224 dprintk("svcrdma: failed to decode read list\n");
225 return -EINVAL;
226 }
227 va = decode_write_list(va, vaend);
228 if (!va) {
229 dprintk("svcrdma: failed to decode write list\n");
230 return -EINVAL;
231 }
232 va = decode_reply_array(va, vaend);
233 if (!va) {
234 dprintk("svcrdma: failed to decode reply chunk\n");
235 return -EINVAL;
236 }
237
238 rq_arg->head[0].iov_base = va;
239 hdr_len = (unsigned long)va - (unsigned long)rmsgp;
240 rq_arg->head[0].iov_len -= hdr_len;
241 return hdr_len;
242}
243
244int svc_rdma_xdr_encode_error(struct svcxprt_rdma *xprt,
245 struct rpcrdma_msg *rmsgp,
246 enum rpcrdma_errcode err, __be32 *va)
247{
248 __be32 *startp = va;
249
250 *va++ = rmsgp->rm_xid;
251 *va++ = rmsgp->rm_vers;
252 *va++ = cpu_to_be32(xprt->sc_max_requests);
253 *va++ = rdma_error;
254 *va++ = cpu_to_be32(err);
255 if (err == ERR_VERS) {
256 *va++ = rpcrdma_version;
257 *va++ = rpcrdma_version;
258 }
259
260 return (int)((unsigned long)va - (unsigned long)startp);
261}
262
263int svc_rdma_xdr_get_reply_hdr_len(struct rpcrdma_msg *rmsgp)
264{
265 struct rpcrdma_write_array *wr_ary;
266
267
268
269
270 wr_ary = (struct rpcrdma_write_array *)
271 &rmsgp->rm_body.rm_chunks[1];
272 if (wr_ary->wc_discrim)
273 wr_ary = (struct rpcrdma_write_array *)
274 &wr_ary->wc_array[be32_to_cpu(wr_ary->wc_nchunks)].
275 wc_target.rs_length;
276 else
277 wr_ary = (struct rpcrdma_write_array *)
278 &wr_ary->wc_nchunks;
279
280
281 if (wr_ary->wc_discrim)
282 wr_ary = (struct rpcrdma_write_array *)
283 &wr_ary->wc_array[be32_to_cpu(wr_ary->wc_nchunks)];
284 else
285 wr_ary = (struct rpcrdma_write_array *)
286 &wr_ary->wc_nchunks;
287
288 return (unsigned long) wr_ary - (unsigned long) rmsgp;
289}
290
291void svc_rdma_xdr_encode_write_list(struct rpcrdma_msg *rmsgp, int chunks)
292{
293 struct rpcrdma_write_array *ary;
294
295
296 rmsgp->rm_body.rm_chunks[0] = xdr_zero;
297
298
299 ary = (struct rpcrdma_write_array *)
300 &rmsgp->rm_body.rm_chunks[1];
301 ary->wc_discrim = xdr_one;
302 ary->wc_nchunks = cpu_to_be32(chunks);
303
304
305 ary->wc_array[chunks].wc_target.rs_handle = xdr_zero;
306
307
308 ary->wc_array[chunks].wc_target.rs_length = xdr_zero;
309}
310
311void svc_rdma_xdr_encode_reply_array(struct rpcrdma_write_array *ary,
312 int chunks)
313{
314 ary->wc_discrim = xdr_one;
315 ary->wc_nchunks = cpu_to_be32(chunks);
316}
317
318void svc_rdma_xdr_encode_array_chunk(struct rpcrdma_write_array *ary,
319 int chunk_no,
320 __be32 rs_handle,
321 __be64 rs_offset,
322 u32 write_len)
323{
324 struct rpcrdma_segment *seg = &ary->wc_array[chunk_no].wc_target;
325 seg->rs_handle = rs_handle;
326 seg->rs_offset = rs_offset;
327 seg->rs_length = cpu_to_be32(write_len);
328}
329
330void svc_rdma_xdr_encode_reply_header(struct svcxprt_rdma *xprt,
331 struct rpcrdma_msg *rdma_argp,
332 struct rpcrdma_msg *rdma_resp,
333 enum rpcrdma_proc rdma_type)
334{
335 rdma_resp->rm_xid = rdma_argp->rm_xid;
336 rdma_resp->rm_vers = rdma_argp->rm_vers;
337 rdma_resp->rm_credit = cpu_to_be32(xprt->sc_max_requests);
338 rdma_resp->rm_type = cpu_to_be32(rdma_type);
339
340
341 rdma_resp->rm_body.rm_chunks[0] = xdr_zero;
342 rdma_resp->rm_body.rm_chunks[1] = xdr_zero;
343 rdma_resp->rm_body.rm_chunks[2] = xdr_zero;
344}
345