svc_rdma_marshal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 : u32 offset into XDR stream
  51. * handle : u32 RKEY
  52. * . . .
  53. * end-of-list: xdr_zero
  54. */
  55. static u32 *decode_read_list(u32 *va, u32 *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 (u32 *)&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 : u32 RKEY ---+
  73. * length : u32 <len of segment> |
  74. * offset : remove va + <count>
  75. * . . . |
  76. * ---+
  77. */
  78. static u32 *decode_write_list(u32 *va, u32 *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 (u32 *)&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 = ntohl(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 (u32 *)&ary->wc_array[nchunks].wc_target.rs_length;
  107. }
  108. static u32 *decode_reply_array(u32 *va, u32 *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 (u32 *)&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 = ntohl(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 (u32 *)&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. u32 *va;
  139. u32 *vaend;
  140. u32 hdr_len;
  141. rmsgp = (struct rpcrdma_msg *)rqstp->rq_arg.head[0].iov_base;
  142. /* Verify that there's enough bytes for header + something */
  143. if (rqstp->rq_arg.len <= RPCRDMA_HDRLEN_MIN) {
  144. dprintk("svcrdma: header too short = %d\n",
  145. rqstp->rq_arg.len);
  146. return -EINVAL;
  147. }
  148. /* Decode the header */
  149. rmsgp->rm_xid = ntohl(rmsgp->rm_xid);
  150. rmsgp->rm_vers = ntohl(rmsgp->rm_vers);
  151. rmsgp->rm_credit = ntohl(rmsgp->rm_credit);
  152. rmsgp->rm_type = ntohl(rmsgp->rm_type);
  153. if (rmsgp->rm_vers != RPCRDMA_VERSION)
  154. return -ENOSYS;
  155. /* Pull in the extra for the padded case and bump our pointer */
  156. if (rmsgp->rm_type == RDMA_MSGP) {
  157. int hdrlen;
  158. rmsgp->rm_body.rm_padded.rm_align =
  159. ntohl(rmsgp->rm_body.rm_padded.rm_align);
  160. rmsgp->rm_body.rm_padded.rm_thresh =
  161. ntohl(rmsgp->rm_body.rm_padded.rm_thresh);
  162. va = &rmsgp->rm_body.rm_padded.rm_pempty[4];
  163. rqstp->rq_arg.head[0].iov_base = va;
  164. hdrlen = (u32)((unsigned long)va - (unsigned long)rmsgp);
  165. rqstp->rq_arg.head[0].iov_len -= hdrlen;
  166. if (hdrlen > rqstp->rq_arg.len)
  167. return -EINVAL;
  168. return hdrlen;
  169. }
  170. /* The chunk list may contain either a read chunk list or a write
  171. * chunk list and a reply chunk list.
  172. */
  173. va = &rmsgp->rm_body.rm_chunks[0];
  174. vaend = (u32 *)((unsigned long)rmsgp + rqstp->rq_arg.len);
  175. va = decode_read_list(va, vaend);
  176. if (!va)
  177. return -EINVAL;
  178. va = decode_write_list(va, vaend);
  179. if (!va)
  180. return -EINVAL;
  181. va = decode_reply_array(va, vaend);
  182. if (!va)
  183. return -EINVAL;
  184. rqstp->rq_arg.head[0].iov_base = va;
  185. hdr_len = (unsigned long)va - (unsigned long)rmsgp;
  186. rqstp->rq_arg.head[0].iov_len -= hdr_len;
  187. *rdma_req = rmsgp;
  188. return hdr_len;
  189. }
  190. int svc_rdma_xdr_decode_deferred_req(struct svc_rqst *rqstp)
  191. {
  192. struct rpcrdma_msg *rmsgp = NULL;
  193. struct rpcrdma_read_chunk *ch;
  194. struct rpcrdma_write_array *ary;
  195. u32 *va;
  196. u32 hdrlen;
  197. dprintk("svcrdma: processing deferred RDMA header on rqstp=%p\n",
  198. rqstp);
  199. rmsgp = (struct rpcrdma_msg *)rqstp->rq_arg.head[0].iov_base;
  200. /* Pull in the extra for the padded case and bump our pointer */
  201. if (rmsgp->rm_type == RDMA_MSGP) {
  202. va = &rmsgp->rm_body.rm_padded.rm_pempty[4];
  203. rqstp->rq_arg.head[0].iov_base = va;
  204. hdrlen = (u32)((unsigned long)va - (unsigned long)rmsgp);
  205. rqstp->rq_arg.head[0].iov_len -= hdrlen;
  206. return hdrlen;
  207. }
  208. /*
  209. * Skip all chunks to find RPC msg. These were previously processed
  210. */
  211. va = &rmsgp->rm_body.rm_chunks[0];
  212. /* Skip read-list */
  213. for (ch = (struct rpcrdma_read_chunk *)va;
  214. ch->rc_discrim != xdr_zero; ch++);
  215. va = (u32 *)&ch->rc_position;
  216. /* Skip write-list */
  217. ary = (struct rpcrdma_write_array *)va;
  218. if (ary->wc_discrim == xdr_zero)
  219. va = (u32 *)&ary->wc_nchunks;
  220. else
  221. /*
  222. * rs_length is the 2nd 4B field in wc_target and taking its
  223. * address skips the list terminator
  224. */
  225. va = (u32 *)&ary->wc_array[ary->wc_nchunks].wc_target.rs_length;
  226. /* Skip reply-array */
  227. ary = (struct rpcrdma_write_array *)va;
  228. if (ary->wc_discrim == xdr_zero)
  229. va = (u32 *)&ary->wc_nchunks;
  230. else
  231. va = (u32 *)&ary->wc_array[ary->wc_nchunks];
  232. rqstp->rq_arg.head[0].iov_base = va;
  233. hdrlen = (unsigned long)va - (unsigned long)rmsgp;
  234. rqstp->rq_arg.head[0].iov_len -= hdrlen;
  235. return hdrlen;
  236. }
  237. int svc_rdma_xdr_encode_error(struct svcxprt_rdma *xprt,
  238. struct rpcrdma_msg *rmsgp,
  239. enum rpcrdma_errcode err, u32 *va)
  240. {
  241. u32 *startp = va;
  242. *va++ = htonl(rmsgp->rm_xid);
  243. *va++ = htonl(rmsgp->rm_vers);
  244. *va++ = htonl(xprt->sc_max_requests);
  245. *va++ = htonl(RDMA_ERROR);
  246. *va++ = htonl(err);
  247. if (err == ERR_VERS) {
  248. *va++ = htonl(RPCRDMA_VERSION);
  249. *va++ = htonl(RPCRDMA_VERSION);
  250. }
  251. return (int)((unsigned long)va - (unsigned long)startp);
  252. }
  253. int svc_rdma_xdr_get_reply_hdr_len(struct rpcrdma_msg *rmsgp)
  254. {
  255. struct rpcrdma_write_array *wr_ary;
  256. /* There is no read-list in a reply */
  257. /* skip write list */
  258. wr_ary = (struct rpcrdma_write_array *)
  259. &rmsgp->rm_body.rm_chunks[1];
  260. if (wr_ary->wc_discrim)
  261. wr_ary = (struct rpcrdma_write_array *)
  262. &wr_ary->wc_array[ntohl(wr_ary->wc_nchunks)].
  263. wc_target.rs_length;
  264. else
  265. wr_ary = (struct rpcrdma_write_array *)
  266. &wr_ary->wc_nchunks;
  267. /* skip reply array */
  268. if (wr_ary->wc_discrim)
  269. wr_ary = (struct rpcrdma_write_array *)
  270. &wr_ary->wc_array[ntohl(wr_ary->wc_nchunks)];
  271. else
  272. wr_ary = (struct rpcrdma_write_array *)
  273. &wr_ary->wc_nchunks;
  274. return (unsigned long) wr_ary - (unsigned long) rmsgp;
  275. }
  276. void svc_rdma_xdr_encode_write_list(struct rpcrdma_msg *rmsgp, int chunks)
  277. {
  278. struct rpcrdma_write_array *ary;
  279. /* no read-list */
  280. rmsgp->rm_body.rm_chunks[0] = xdr_zero;
  281. /* write-array discrim */
  282. ary = (struct rpcrdma_write_array *)
  283. &rmsgp->rm_body.rm_chunks[1];
  284. ary->wc_discrim = xdr_one;
  285. ary->wc_nchunks = htonl(chunks);
  286. /* write-list terminator */
  287. ary->wc_array[chunks].wc_target.rs_handle = xdr_zero;
  288. /* reply-array discriminator */
  289. ary->wc_array[chunks].wc_target.rs_length = xdr_zero;
  290. }
  291. void svc_rdma_xdr_encode_reply_array(struct rpcrdma_write_array *ary,
  292. int chunks)
  293. {
  294. ary->wc_discrim = xdr_one;
  295. ary->wc_nchunks = htonl(chunks);
  296. }
  297. void svc_rdma_xdr_encode_array_chunk(struct rpcrdma_write_array *ary,
  298. int chunk_no,
  299. __be32 rs_handle,
  300. __be64 rs_offset,
  301. u32 write_len)
  302. {
  303. struct rpcrdma_segment *seg = &ary->wc_array[chunk_no].wc_target;
  304. seg->rs_handle = rs_handle;
  305. seg->rs_offset = rs_offset;
  306. seg->rs_length = htonl(write_len);
  307. }
  308. void svc_rdma_xdr_encode_reply_header(struct svcxprt_rdma *xprt,
  309. struct rpcrdma_msg *rdma_argp,
  310. struct rpcrdma_msg *rdma_resp,
  311. enum rpcrdma_proc rdma_type)
  312. {
  313. rdma_resp->rm_xid = htonl(rdma_argp->rm_xid);
  314. rdma_resp->rm_vers = htonl(rdma_argp->rm_vers);
  315. rdma_resp->rm_credit = htonl(xprt->sc_max_requests);
  316. rdma_resp->rm_type = htonl(rdma_type);
  317. /* Encode <nul> chunks lists */
  318. rdma_resp->rm_body.rm_chunks[0] = xdr_zero;
  319. rdma_resp->rm_body.rm_chunks[1] = xdr_zero;
  320. rdma_resp->rm_body.rm_chunks[2] = xdr_zero;
  321. }