svc_rdma_marshal.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) 2005-2006 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. * Author: Tom Tucker <tom@opengridcomputing.com>
  40. */
  41. #include <linux/sunrpc/xdr.h>
  42. #include <linux/sunrpc/debug.h>
  43. #include <asm/unaligned.h>
  44. #include <linux/sunrpc/rpc_rdma.h>
  45. #include <linux/sunrpc/svc_rdma.h>
  46. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  47. /*
  48. * Decodes a read chunk list. The expected format is as follows:
  49. * descrim : xdr_one
  50. * position : __be32 offset into XDR stream
  51. * handle : __be32 RKEY
  52. * . . .
  53. * end-of-list: xdr_zero
  54. */
  55. static __be32 *decode_read_list(__be32 *va, __be32 *vaend)
  56. {
  57. struct rpcrdma_read_chunk *ch = (struct rpcrdma_read_chunk *)va;
  58. while (ch->rc_discrim != xdr_zero) {
  59. if (((unsigned long)ch + sizeof(struct rpcrdma_read_chunk)) >
  60. (unsigned long)vaend) {
  61. dprintk("svcrdma: vaend=%p, ch=%p\n", vaend, ch);
  62. return NULL;
  63. }
  64. ch++;
  65. }
  66. return &ch->rc_position;
  67. }
  68. /*
  69. * Decodes a write chunk list. The expected format is as follows:
  70. * descrim : xdr_one
  71. * nchunks : <count>
  72. * handle : __be32 RKEY ---+
  73. * length : __be32 <len of segment> |
  74. * offset : remove va + <count>
  75. * . . . |
  76. * ---+
  77. */
  78. static __be32 *decode_write_list(__be32 *va, __be32 *vaend)
  79. {
  80. unsigned long start, end;
  81. int nchunks;
  82. struct rpcrdma_write_array *ary =
  83. (struct rpcrdma_write_array *)va;
  84. /* Check for not write-array */
  85. if (ary->wc_discrim == xdr_zero)
  86. return &ary->wc_nchunks;
  87. if ((unsigned long)ary + sizeof(struct rpcrdma_write_array) >
  88. (unsigned long)vaend) {
  89. dprintk("svcrdma: ary=%p, vaend=%p\n", ary, vaend);
  90. return NULL;
  91. }
  92. nchunks = be32_to_cpu(ary->wc_nchunks);
  93. start = (unsigned long)&ary->wc_array[0];
  94. end = (unsigned long)vaend;
  95. if (nchunks < 0 ||
  96. nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
  97. (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
  98. dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
  99. ary, nchunks, vaend);
  100. return NULL;
  101. }
  102. /*
  103. * rs_length is the 2nd 4B field in wc_target and taking its
  104. * address skips the list terminator
  105. */
  106. return &ary->wc_array[nchunks].wc_target.rs_length;
  107. }
  108. static __be32 *decode_reply_array(__be32 *va, __be32 *vaend)
  109. {
  110. unsigned long start, end;
  111. int nchunks;
  112. struct rpcrdma_write_array *ary =
  113. (struct rpcrdma_write_array *)va;
  114. /* Check for no reply-array */
  115. if (ary->wc_discrim == xdr_zero)
  116. return &ary->wc_nchunks;
  117. if ((unsigned long)ary + sizeof(struct rpcrdma_write_array) >
  118. (unsigned long)vaend) {
  119. dprintk("svcrdma: ary=%p, vaend=%p\n", ary, vaend);
  120. return NULL;
  121. }
  122. nchunks = be32_to_cpu(ary->wc_nchunks);
  123. start = (unsigned long)&ary->wc_array[0];
  124. end = (unsigned long)vaend;
  125. if (nchunks < 0 ||
  126. nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
  127. (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
  128. dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
  129. ary, nchunks, vaend);
  130. return NULL;
  131. }
  132. return (__be32 *)&ary->wc_array[nchunks];
  133. }
  134. int svc_rdma_xdr_decode_req(struct rpcrdma_msg **rdma_req,
  135. struct svc_rqst *rqstp)
  136. {
  137. struct rpcrdma_msg *rmsgp = NULL;
  138. __be32 *va, *vaend;
  139. u32 hdr_len;
  140. rmsgp = (struct rpcrdma_msg *)rqstp->rq_arg.head[0].iov_base;
  141. /* Verify that there's enough bytes for header + something */
  142. if (rqstp->rq_arg.len <= RPCRDMA_HDRLEN_MIN) {
  143. dprintk("svcrdma: header too short = %d\n",
  144. rqstp->rq_arg.len);
  145. return -EINVAL;
  146. }
  147. if (rmsgp->rm_vers != rpcrdma_version)
  148. return -ENOSYS;
  149. /* Pull in the extra for the padded case and bump our pointer */
  150. if (rmsgp->rm_type == rdma_msgp) {
  151. int hdrlen;
  152. rmsgp->rm_body.rm_padded.rm_align =
  153. be32_to_cpu(rmsgp->rm_body.rm_padded.rm_align);
  154. rmsgp->rm_body.rm_padded.rm_thresh =
  155. be32_to_cpu(rmsgp->rm_body.rm_padded.rm_thresh);
  156. va = &rmsgp->rm_body.rm_padded.rm_pempty[4];
  157. rqstp->rq_arg.head[0].iov_base = va;
  158. hdrlen = (u32)((unsigned long)va - (unsigned long)rmsgp);
  159. rqstp->rq_arg.head[0].iov_len -= hdrlen;
  160. if (hdrlen > rqstp->rq_arg.len)
  161. return -EINVAL;
  162. return hdrlen;
  163. }
  164. /* The chunk list may contain either a read chunk list or a write
  165. * chunk list and a reply chunk list.
  166. */
  167. va = &rmsgp->rm_body.rm_chunks[0];
  168. vaend = (__be32 *)((unsigned long)rmsgp + rqstp->rq_arg.len);
  169. va = decode_read_list(va, vaend);
  170. if (!va)
  171. return -EINVAL;
  172. va = decode_write_list(va, vaend);
  173. if (!va)
  174. return -EINVAL;
  175. va = decode_reply_array(va, vaend);
  176. if (!va)
  177. return -EINVAL;
  178. rqstp->rq_arg.head[0].iov_base = va;
  179. hdr_len = (unsigned long)va - (unsigned long)rmsgp;
  180. rqstp->rq_arg.head[0].iov_len -= hdr_len;
  181. *rdma_req = rmsgp;
  182. return hdr_len;
  183. }
  184. int svc_rdma_xdr_encode_error(struct svcxprt_rdma *xprt,
  185. struct rpcrdma_msg *rmsgp,
  186. enum rpcrdma_errcode err, __be32 *va)
  187. {
  188. __be32 *startp = va;
  189. *va++ = rmsgp->rm_xid;
  190. *va++ = rmsgp->rm_vers;
  191. *va++ = cpu_to_be32(xprt->sc_max_requests);
  192. *va++ = rdma_error;
  193. *va++ = cpu_to_be32(err);
  194. if (err == ERR_VERS) {
  195. *va++ = rpcrdma_version;
  196. *va++ = rpcrdma_version;
  197. }
  198. return (int)((unsigned long)va - (unsigned long)startp);
  199. }
  200. int svc_rdma_xdr_get_reply_hdr_len(struct rpcrdma_msg *rmsgp)
  201. {
  202. struct rpcrdma_write_array *wr_ary;
  203. /* There is no read-list in a reply */
  204. /* skip write list */
  205. wr_ary = (struct rpcrdma_write_array *)
  206. &rmsgp->rm_body.rm_chunks[1];
  207. if (wr_ary->wc_discrim)
  208. wr_ary = (struct rpcrdma_write_array *)
  209. &wr_ary->wc_array[be32_to_cpu(wr_ary->wc_nchunks)].
  210. wc_target.rs_length;
  211. else
  212. wr_ary = (struct rpcrdma_write_array *)
  213. &wr_ary->wc_nchunks;
  214. /* skip reply array */
  215. if (wr_ary->wc_discrim)
  216. wr_ary = (struct rpcrdma_write_array *)
  217. &wr_ary->wc_array[be32_to_cpu(wr_ary->wc_nchunks)];
  218. else
  219. wr_ary = (struct rpcrdma_write_array *)
  220. &wr_ary->wc_nchunks;
  221. return (unsigned long) wr_ary - (unsigned long) rmsgp;
  222. }
  223. void svc_rdma_xdr_encode_write_list(struct rpcrdma_msg *rmsgp, int chunks)
  224. {
  225. struct rpcrdma_write_array *ary;
  226. /* no read-list */
  227. rmsgp->rm_body.rm_chunks[0] = xdr_zero;
  228. /* write-array discrim */
  229. ary = (struct rpcrdma_write_array *)
  230. &rmsgp->rm_body.rm_chunks[1];
  231. ary->wc_discrim = xdr_one;
  232. ary->wc_nchunks = cpu_to_be32(chunks);
  233. /* write-list terminator */
  234. ary->wc_array[chunks].wc_target.rs_handle = xdr_zero;
  235. /* reply-array discriminator */
  236. ary->wc_array[chunks].wc_target.rs_length = xdr_zero;
  237. }
  238. void svc_rdma_xdr_encode_reply_array(struct rpcrdma_write_array *ary,
  239. int chunks)
  240. {
  241. ary->wc_discrim = xdr_one;
  242. ary->wc_nchunks = cpu_to_be32(chunks);
  243. }
  244. void svc_rdma_xdr_encode_array_chunk(struct rpcrdma_write_array *ary,
  245. int chunk_no,
  246. __be32 rs_handle,
  247. __be64 rs_offset,
  248. u32 write_len)
  249. {
  250. struct rpcrdma_segment *seg = &ary->wc_array[chunk_no].wc_target;
  251. seg->rs_handle = rs_handle;
  252. seg->rs_offset = rs_offset;
  253. seg->rs_length = cpu_to_be32(write_len);
  254. }
  255. void svc_rdma_xdr_encode_reply_header(struct svcxprt_rdma *xprt,
  256. struct rpcrdma_msg *rdma_argp,
  257. struct rpcrdma_msg *rdma_resp,
  258. enum rpcrdma_proc rdma_type)
  259. {
  260. rdma_resp->rm_xid = rdma_argp->rm_xid;
  261. rdma_resp->rm_vers = rdma_argp->rm_vers;
  262. rdma_resp->rm_credit = cpu_to_be32(xprt->sc_max_requests);
  263. rdma_resp->rm_type = cpu_to_be32(rdma_type);
  264. /* Encode <nul> chunks lists */
  265. rdma_resp->rm_body.rm_chunks[0] = xdr_zero;
  266. rdma_resp->rm_body.rm_chunks[1] = xdr_zero;
  267. rdma_resp->rm_body.rm_chunks[2] = xdr_zero;
  268. }