svc_rdma_marshal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. * Determine number of chunks and total bytes in chunk list. The chunk
  70. * list has already been verified to fit within the RPCRDMA header.
  71. */
  72. void svc_rdma_rcl_chunk_counts(struct rpcrdma_read_chunk *ch,
  73. int *ch_count, int *byte_count)
  74. {
  75. /* compute the number of bytes represented by read chunks */
  76. *byte_count = 0;
  77. *ch_count = 0;
  78. for (; ch->rc_discrim != 0; ch++) {
  79. *byte_count = *byte_count + ntohl(ch->rc_target.rs_length);
  80. *ch_count = *ch_count + 1;
  81. }
  82. }
  83. /*
  84. * Decodes a write chunk list. The expected format is as follows:
  85. * descrim : xdr_one
  86. * nchunks : <count>
  87. * handle : u32 RKEY ---+
  88. * length : u32 <len of segment> |
  89. * offset : remove va + <count>
  90. * . . . |
  91. * ---+
  92. */
  93. static u32 *decode_write_list(u32 *va, u32 *vaend)
  94. {
  95. unsigned long start, end;
  96. int nchunks;
  97. struct rpcrdma_write_array *ary =
  98. (struct rpcrdma_write_array *)va;
  99. /* Check for not write-array */
  100. if (ary->wc_discrim == xdr_zero)
  101. return (u32 *)&ary->wc_nchunks;
  102. if ((unsigned long)ary + sizeof(struct rpcrdma_write_array) >
  103. (unsigned long)vaend) {
  104. dprintk("svcrdma: ary=%p, vaend=%p\n", ary, vaend);
  105. return NULL;
  106. }
  107. nchunks = ntohl(ary->wc_nchunks);
  108. start = (unsigned long)&ary->wc_array[0];
  109. end = (unsigned long)vaend;
  110. if (nchunks < 0 ||
  111. nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
  112. (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
  113. dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
  114. ary, nchunks, vaend);
  115. return NULL;
  116. }
  117. /*
  118. * rs_length is the 2nd 4B field in wc_target and taking its
  119. * address skips the list terminator
  120. */
  121. return (u32 *)&ary->wc_array[nchunks].wc_target.rs_length;
  122. }
  123. static u32 *decode_reply_array(u32 *va, u32 *vaend)
  124. {
  125. unsigned long start, end;
  126. int nchunks;
  127. struct rpcrdma_write_array *ary =
  128. (struct rpcrdma_write_array *)va;
  129. /* Check for no reply-array */
  130. if (ary->wc_discrim == xdr_zero)
  131. return (u32 *)&ary->wc_nchunks;
  132. if ((unsigned long)ary + sizeof(struct rpcrdma_write_array) >
  133. (unsigned long)vaend) {
  134. dprintk("svcrdma: ary=%p, vaend=%p\n", ary, vaend);
  135. return NULL;
  136. }
  137. nchunks = ntohl(ary->wc_nchunks);
  138. start = (unsigned long)&ary->wc_array[0];
  139. end = (unsigned long)vaend;
  140. if (nchunks < 0 ||
  141. nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
  142. (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
  143. dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
  144. ary, nchunks, vaend);
  145. return NULL;
  146. }
  147. return (u32 *)&ary->wc_array[nchunks];
  148. }
  149. int svc_rdma_xdr_decode_req(struct rpcrdma_msg **rdma_req,
  150. struct svc_rqst *rqstp)
  151. {
  152. struct rpcrdma_msg *rmsgp = NULL;
  153. u32 *va;
  154. u32 *vaend;
  155. u32 hdr_len;
  156. rmsgp = (struct rpcrdma_msg *)rqstp->rq_arg.head[0].iov_base;
  157. /* Verify that there's enough bytes for header + something */
  158. if (rqstp->rq_arg.len <= RPCRDMA_HDRLEN_MIN) {
  159. dprintk("svcrdma: header too short = %d\n",
  160. rqstp->rq_arg.len);
  161. return -EINVAL;
  162. }
  163. /* Decode the header */
  164. rmsgp->rm_xid = ntohl(rmsgp->rm_xid);
  165. rmsgp->rm_vers = ntohl(rmsgp->rm_vers);
  166. rmsgp->rm_credit = ntohl(rmsgp->rm_credit);
  167. rmsgp->rm_type = ntohl(rmsgp->rm_type);
  168. if (rmsgp->rm_vers != RPCRDMA_VERSION)
  169. return -ENOSYS;
  170. /* Pull in the extra for the padded case and bump our pointer */
  171. if (rmsgp->rm_type == RDMA_MSGP) {
  172. int hdrlen;
  173. rmsgp->rm_body.rm_padded.rm_align =
  174. ntohl(rmsgp->rm_body.rm_padded.rm_align);
  175. rmsgp->rm_body.rm_padded.rm_thresh =
  176. ntohl(rmsgp->rm_body.rm_padded.rm_thresh);
  177. va = &rmsgp->rm_body.rm_padded.rm_pempty[4];
  178. rqstp->rq_arg.head[0].iov_base = va;
  179. hdrlen = (u32)((unsigned long)va - (unsigned long)rmsgp);
  180. rqstp->rq_arg.head[0].iov_len -= hdrlen;
  181. if (hdrlen > rqstp->rq_arg.len)
  182. return -EINVAL;
  183. return hdrlen;
  184. }
  185. /* The chunk list may contain either a read chunk list or a write
  186. * chunk list and a reply chunk list.
  187. */
  188. va = &rmsgp->rm_body.rm_chunks[0];
  189. vaend = (u32 *)((unsigned long)rmsgp + rqstp->rq_arg.len);
  190. va = decode_read_list(va, vaend);
  191. if (!va)
  192. return -EINVAL;
  193. va = decode_write_list(va, vaend);
  194. if (!va)
  195. return -EINVAL;
  196. va = decode_reply_array(va, vaend);
  197. if (!va)
  198. return -EINVAL;
  199. rqstp->rq_arg.head[0].iov_base = va;
  200. hdr_len = (unsigned long)va - (unsigned long)rmsgp;
  201. rqstp->rq_arg.head[0].iov_len -= hdr_len;
  202. *rdma_req = rmsgp;
  203. return hdr_len;
  204. }
  205. int svc_rdma_xdr_decode_deferred_req(struct svc_rqst *rqstp)
  206. {
  207. struct rpcrdma_msg *rmsgp = NULL;
  208. struct rpcrdma_read_chunk *ch;
  209. struct rpcrdma_write_array *ary;
  210. u32 *va;
  211. u32 hdrlen;
  212. dprintk("svcrdma: processing deferred RDMA header on rqstp=%p\n",
  213. rqstp);
  214. rmsgp = (struct rpcrdma_msg *)rqstp->rq_arg.head[0].iov_base;
  215. /* Pull in the extra for the padded case and bump our pointer */
  216. if (rmsgp->rm_type == RDMA_MSGP) {
  217. va = &rmsgp->rm_body.rm_padded.rm_pempty[4];
  218. rqstp->rq_arg.head[0].iov_base = va;
  219. hdrlen = (u32)((unsigned long)va - (unsigned long)rmsgp);
  220. rqstp->rq_arg.head[0].iov_len -= hdrlen;
  221. return hdrlen;
  222. }
  223. /*
  224. * Skip all chunks to find RPC msg. These were previously processed
  225. */
  226. va = &rmsgp->rm_body.rm_chunks[0];
  227. /* Skip read-list */
  228. for (ch = (struct rpcrdma_read_chunk *)va;
  229. ch->rc_discrim != xdr_zero; ch++);
  230. va = (u32 *)&ch->rc_position;
  231. /* Skip write-list */
  232. ary = (struct rpcrdma_write_array *)va;
  233. if (ary->wc_discrim == xdr_zero)
  234. va = (u32 *)&ary->wc_nchunks;
  235. else
  236. /*
  237. * rs_length is the 2nd 4B field in wc_target and taking its
  238. * address skips the list terminator
  239. */
  240. va = (u32 *)&ary->wc_array[ary->wc_nchunks].wc_target.rs_length;
  241. /* Skip reply-array */
  242. ary = (struct rpcrdma_write_array *)va;
  243. if (ary->wc_discrim == xdr_zero)
  244. va = (u32 *)&ary->wc_nchunks;
  245. else
  246. va = (u32 *)&ary->wc_array[ary->wc_nchunks];
  247. rqstp->rq_arg.head[0].iov_base = va;
  248. hdrlen = (unsigned long)va - (unsigned long)rmsgp;
  249. rqstp->rq_arg.head[0].iov_len -= hdrlen;
  250. return hdrlen;
  251. }
  252. int svc_rdma_xdr_encode_error(struct svcxprt_rdma *xprt,
  253. struct rpcrdma_msg *rmsgp,
  254. enum rpcrdma_errcode err, u32 *va)
  255. {
  256. u32 *startp = va;
  257. *va++ = htonl(rmsgp->rm_xid);
  258. *va++ = htonl(rmsgp->rm_vers);
  259. *va++ = htonl(xprt->sc_max_requests);
  260. *va++ = htonl(RDMA_ERROR);
  261. *va++ = htonl(err);
  262. if (err == ERR_VERS) {
  263. *va++ = htonl(RPCRDMA_VERSION);
  264. *va++ = htonl(RPCRDMA_VERSION);
  265. }
  266. return (int)((unsigned long)va - (unsigned long)startp);
  267. }
  268. int svc_rdma_xdr_get_reply_hdr_len(struct rpcrdma_msg *rmsgp)
  269. {
  270. struct rpcrdma_write_array *wr_ary;
  271. /* There is no read-list in a reply */
  272. /* skip write list */
  273. wr_ary = (struct rpcrdma_write_array *)
  274. &rmsgp->rm_body.rm_chunks[1];
  275. if (wr_ary->wc_discrim)
  276. wr_ary = (struct rpcrdma_write_array *)
  277. &wr_ary->wc_array[ntohl(wr_ary->wc_nchunks)].
  278. wc_target.rs_length;
  279. else
  280. wr_ary = (struct rpcrdma_write_array *)
  281. &wr_ary->wc_nchunks;
  282. /* skip reply array */
  283. if (wr_ary->wc_discrim)
  284. wr_ary = (struct rpcrdma_write_array *)
  285. &wr_ary->wc_array[ntohl(wr_ary->wc_nchunks)];
  286. else
  287. wr_ary = (struct rpcrdma_write_array *)
  288. &wr_ary->wc_nchunks;
  289. return (unsigned long) wr_ary - (unsigned long) rmsgp;
  290. }
  291. void svc_rdma_xdr_encode_write_list(struct rpcrdma_msg *rmsgp, int chunks)
  292. {
  293. struct rpcrdma_write_array *ary;
  294. /* no read-list */
  295. rmsgp->rm_body.rm_chunks[0] = xdr_zero;
  296. /* write-array discrim */
  297. ary = (struct rpcrdma_write_array *)
  298. &rmsgp->rm_body.rm_chunks[1];
  299. ary->wc_discrim = xdr_one;
  300. ary->wc_nchunks = htonl(chunks);
  301. /* write-list terminator */
  302. ary->wc_array[chunks].wc_target.rs_handle = xdr_zero;
  303. /* reply-array discriminator */
  304. ary->wc_array[chunks].wc_target.rs_length = xdr_zero;
  305. }
  306. void svc_rdma_xdr_encode_reply_array(struct rpcrdma_write_array *ary,
  307. int chunks)
  308. {
  309. ary->wc_discrim = xdr_one;
  310. ary->wc_nchunks = htonl(chunks);
  311. }
  312. void svc_rdma_xdr_encode_array_chunk(struct rpcrdma_write_array *ary,
  313. int chunk_no,
  314. __be32 rs_handle,
  315. __be64 rs_offset,
  316. u32 write_len)
  317. {
  318. struct rpcrdma_segment *seg = &ary->wc_array[chunk_no].wc_target;
  319. seg->rs_handle = rs_handle;
  320. seg->rs_offset = rs_offset;
  321. seg->rs_length = htonl(write_len);
  322. }
  323. void svc_rdma_xdr_encode_reply_header(struct svcxprt_rdma *xprt,
  324. struct rpcrdma_msg *rdma_argp,
  325. struct rpcrdma_msg *rdma_resp,
  326. enum rpcrdma_proc rdma_type)
  327. {
  328. rdma_resp->rm_xid = htonl(rdma_argp->rm_xid);
  329. rdma_resp->rm_vers = htonl(rdma_argp->rm_vers);
  330. rdma_resp->rm_credit = htonl(xprt->sc_max_requests);
  331. rdma_resp->rm_type = htonl(rdma_type);
  332. /* Encode <nul> chunks lists */
  333. rdma_resp->rm_body.rm_chunks[0] = xdr_zero;
  334. rdma_resp->rm_body.rm_chunks[1] = xdr_zero;
  335. rdma_resp->rm_body.rm_chunks[2] = xdr_zero;
  336. }