svc_rdma_marshal.c 12 KB

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