svc_rdma_marshal.c 11 KB

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