svc_rdma_marshal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /**
  135. * svc_rdma_xdr_decode_req - Parse incoming RPC-over-RDMA header
  136. * @rq_arg: Receive buffer
  137. *
  138. * On entry, xdr->head[0].iov_base points to first byte in the
  139. * RPC-over-RDMA header.
  140. *
  141. * On successful exit, head[0] points to first byte past the
  142. * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
  143. * The length of the RPC-over-RDMA header is returned.
  144. */
  145. int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg)
  146. {
  147. struct rpcrdma_msg *rmsgp;
  148. __be32 *va, *vaend;
  149. unsigned int len;
  150. u32 hdr_len;
  151. /* Verify that there's enough bytes for header + something */
  152. if (rq_arg->len <= RPCRDMA_HDRLEN_ERR) {
  153. dprintk("svcrdma: header too short = %d\n",
  154. rq_arg->len);
  155. return -EINVAL;
  156. }
  157. rmsgp = (struct rpcrdma_msg *)rq_arg->head[0].iov_base;
  158. if (rmsgp->rm_vers != rpcrdma_version) {
  159. dprintk("%s: bad version %u\n", __func__,
  160. be32_to_cpu(rmsgp->rm_vers));
  161. return -EPROTONOSUPPORT;
  162. }
  163. switch (be32_to_cpu(rmsgp->rm_type)) {
  164. case RDMA_MSG:
  165. case RDMA_NOMSG:
  166. break;
  167. case RDMA_DONE:
  168. /* Just drop it */
  169. dprintk("svcrdma: dropping RDMA_DONE message\n");
  170. return 0;
  171. case RDMA_ERROR:
  172. /* Possible if this is a backchannel reply.
  173. * XXX: We should cancel this XID, though.
  174. */
  175. dprintk("svcrdma: dropping RDMA_ERROR message\n");
  176. return 0;
  177. case RDMA_MSGP:
  178. /* Pull in the extra for the padded case, bump our pointer */
  179. rmsgp->rm_body.rm_padded.rm_align =
  180. be32_to_cpu(rmsgp->rm_body.rm_padded.rm_align);
  181. rmsgp->rm_body.rm_padded.rm_thresh =
  182. be32_to_cpu(rmsgp->rm_body.rm_padded.rm_thresh);
  183. va = &rmsgp->rm_body.rm_padded.rm_pempty[4];
  184. rq_arg->head[0].iov_base = va;
  185. len = (u32)((unsigned long)va - (unsigned long)rmsgp);
  186. rq_arg->head[0].iov_len -= len;
  187. if (len > rq_arg->len)
  188. return -EINVAL;
  189. return len;
  190. default:
  191. dprintk("svcrdma: bad rdma procedure (%u)\n",
  192. be32_to_cpu(rmsgp->rm_type));
  193. return -EINVAL;
  194. }
  195. /* The chunk list may contain either a read chunk list or a write
  196. * chunk list and a reply chunk list.
  197. */
  198. va = &rmsgp->rm_body.rm_chunks[0];
  199. vaend = (__be32 *)((unsigned long)rmsgp + rq_arg->len);
  200. va = decode_read_list(va, vaend);
  201. if (!va) {
  202. dprintk("svcrdma: failed to decode read list\n");
  203. return -EINVAL;
  204. }
  205. va = decode_write_list(va, vaend);
  206. if (!va) {
  207. dprintk("svcrdma: failed to decode write list\n");
  208. return -EINVAL;
  209. }
  210. va = decode_reply_array(va, vaend);
  211. if (!va) {
  212. dprintk("svcrdma: failed to decode reply chunk\n");
  213. return -EINVAL;
  214. }
  215. rq_arg->head[0].iov_base = va;
  216. hdr_len = (unsigned long)va - (unsigned long)rmsgp;
  217. rq_arg->head[0].iov_len -= hdr_len;
  218. return hdr_len;
  219. }
  220. int svc_rdma_xdr_encode_error(struct svcxprt_rdma *xprt,
  221. struct rpcrdma_msg *rmsgp,
  222. enum rpcrdma_errcode err, __be32 *va)
  223. {
  224. __be32 *startp = va;
  225. *va++ = rmsgp->rm_xid;
  226. *va++ = rmsgp->rm_vers;
  227. *va++ = cpu_to_be32(xprt->sc_max_requests);
  228. *va++ = rdma_error;
  229. *va++ = cpu_to_be32(err);
  230. if (err == ERR_VERS) {
  231. *va++ = rpcrdma_version;
  232. *va++ = rpcrdma_version;
  233. }
  234. return (int)((unsigned long)va - (unsigned long)startp);
  235. }
  236. int svc_rdma_xdr_get_reply_hdr_len(struct rpcrdma_msg *rmsgp)
  237. {
  238. struct rpcrdma_write_array *wr_ary;
  239. /* There is no read-list in a reply */
  240. /* skip write list */
  241. wr_ary = (struct rpcrdma_write_array *)
  242. &rmsgp->rm_body.rm_chunks[1];
  243. if (wr_ary->wc_discrim)
  244. wr_ary = (struct rpcrdma_write_array *)
  245. &wr_ary->wc_array[be32_to_cpu(wr_ary->wc_nchunks)].
  246. wc_target.rs_length;
  247. else
  248. wr_ary = (struct rpcrdma_write_array *)
  249. &wr_ary->wc_nchunks;
  250. /* skip reply array */
  251. if (wr_ary->wc_discrim)
  252. wr_ary = (struct rpcrdma_write_array *)
  253. &wr_ary->wc_array[be32_to_cpu(wr_ary->wc_nchunks)];
  254. else
  255. wr_ary = (struct rpcrdma_write_array *)
  256. &wr_ary->wc_nchunks;
  257. return (unsigned long) wr_ary - (unsigned long) rmsgp;
  258. }
  259. void svc_rdma_xdr_encode_write_list(struct rpcrdma_msg *rmsgp, int chunks)
  260. {
  261. struct rpcrdma_write_array *ary;
  262. /* no read-list */
  263. rmsgp->rm_body.rm_chunks[0] = xdr_zero;
  264. /* write-array discrim */
  265. ary = (struct rpcrdma_write_array *)
  266. &rmsgp->rm_body.rm_chunks[1];
  267. ary->wc_discrim = xdr_one;
  268. ary->wc_nchunks = cpu_to_be32(chunks);
  269. /* write-list terminator */
  270. ary->wc_array[chunks].wc_target.rs_handle = xdr_zero;
  271. /* reply-array discriminator */
  272. ary->wc_array[chunks].wc_target.rs_length = xdr_zero;
  273. }
  274. void svc_rdma_xdr_encode_reply_array(struct rpcrdma_write_array *ary,
  275. int chunks)
  276. {
  277. ary->wc_discrim = xdr_one;
  278. ary->wc_nchunks = cpu_to_be32(chunks);
  279. }
  280. void svc_rdma_xdr_encode_array_chunk(struct rpcrdma_write_array *ary,
  281. int chunk_no,
  282. __be32 rs_handle,
  283. __be64 rs_offset,
  284. u32 write_len)
  285. {
  286. struct rpcrdma_segment *seg = &ary->wc_array[chunk_no].wc_target;
  287. seg->rs_handle = rs_handle;
  288. seg->rs_offset = rs_offset;
  289. seg->rs_length = cpu_to_be32(write_len);
  290. }
  291. void svc_rdma_xdr_encode_reply_header(struct svcxprt_rdma *xprt,
  292. struct rpcrdma_msg *rdma_argp,
  293. struct rpcrdma_msg *rdma_resp,
  294. enum rpcrdma_proc rdma_type)
  295. {
  296. rdma_resp->rm_xid = rdma_argp->rm_xid;
  297. rdma_resp->rm_vers = rdma_argp->rm_vers;
  298. rdma_resp->rm_credit = cpu_to_be32(xprt->sc_max_requests);
  299. rdma_resp->rm_type = cpu_to_be32(rdma_type);
  300. /* Encode <nul> chunks lists */
  301. rdma_resp->rm_body.rm_chunks[0] = xdr_zero;
  302. rdma_resp->rm_body.rm_chunks[1] = xdr_zero;
  303. rdma_resp->rm_body.rm_chunks[2] = xdr_zero;
  304. }