svc_rdma_recvfrom.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright (c) 2016, 2017 Oracle. All rights reserved.
  3. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  4. * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the BSD-type
  10. * license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * Neither the name of the Network Appliance, Inc. nor the names of
  25. * its contributors may be used to endorse or promote products
  26. * derived from this software without specific prior written
  27. * permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * Author: Tom Tucker <tom@opengridcomputing.com>
  42. */
  43. /* Operation
  44. *
  45. * The main entry point is svc_rdma_recvfrom. This is called from
  46. * svc_recv when the transport indicates there is incoming data to
  47. * be read. "Data Ready" is signaled when an RDMA Receive completes,
  48. * or when a set of RDMA Reads complete.
  49. *
  50. * An svc_rqst is passed in. This structure contains an array of
  51. * free pages (rq_pages) that will contain the incoming RPC message.
  52. *
  53. * Short messages are moved directly into svc_rqst::rq_arg, and
  54. * the RPC Call is ready to be processed by the Upper Layer.
  55. * svc_rdma_recvfrom returns the length of the RPC Call message,
  56. * completing the reception of the RPC Call.
  57. *
  58. * However, when an incoming message has Read chunks,
  59. * svc_rdma_recvfrom must post RDMA Reads to pull the RPC Call's
  60. * data payload from the client. svc_rdma_recvfrom sets up the
  61. * RDMA Reads using pages in svc_rqst::rq_pages, which are
  62. * transferred to an svc_rdma_op_ctxt for the duration of the
  63. * I/O. svc_rdma_recvfrom then returns zero, since the RPC message
  64. * is still not yet ready.
  65. *
  66. * When the Read chunk payloads have become available on the
  67. * server, "Data Ready" is raised again, and svc_recv calls
  68. * svc_rdma_recvfrom again. This second call may use a different
  69. * svc_rqst than the first one, thus any information that needs
  70. * to be preserved across these two calls is kept in an
  71. * svc_rdma_op_ctxt.
  72. *
  73. * The second call to svc_rdma_recvfrom performs final assembly
  74. * of the RPC Call message, using the RDMA Read sink pages kept in
  75. * the svc_rdma_op_ctxt. The xdr_buf is copied from the
  76. * svc_rdma_op_ctxt to the second svc_rqst. The second call returns
  77. * the length of the completed RPC Call message.
  78. *
  79. * Page Management
  80. *
  81. * Pages under I/O must be transferred from the first svc_rqst to an
  82. * svc_rdma_op_ctxt before the first svc_rdma_recvfrom call returns.
  83. *
  84. * The first svc_rqst supplies pages for RDMA Reads. These are moved
  85. * from rqstp::rq_pages into ctxt::pages. The consumed elements of
  86. * the rq_pages array are set to NULL and refilled with the first
  87. * svc_rdma_recvfrom call returns.
  88. *
  89. * During the second svc_rdma_recvfrom call, RDMA Read sink pages
  90. * are transferred from the svc_rdma_op_ctxt to the second svc_rqst
  91. * (see rdma_read_complete() below).
  92. */
  93. #include <asm/unaligned.h>
  94. #include <rdma/ib_verbs.h>
  95. #include <rdma/rdma_cm.h>
  96. #include <linux/spinlock.h>
  97. #include <linux/sunrpc/xdr.h>
  98. #include <linux/sunrpc/debug.h>
  99. #include <linux/sunrpc/rpc_rdma.h>
  100. #include <linux/sunrpc/svc_rdma.h>
  101. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  102. /*
  103. * Replace the pages in the rq_argpages array with the pages from the SGE in
  104. * the RDMA_RECV completion. The SGL should contain full pages up until the
  105. * last one.
  106. */
  107. static void rdma_build_arg_xdr(struct svc_rqst *rqstp,
  108. struct svc_rdma_op_ctxt *ctxt,
  109. u32 byte_count)
  110. {
  111. struct page *page;
  112. u32 bc;
  113. int sge_no;
  114. /* Swap the page in the SGE with the page in argpages */
  115. page = ctxt->pages[0];
  116. put_page(rqstp->rq_pages[0]);
  117. rqstp->rq_pages[0] = page;
  118. /* Set up the XDR head */
  119. rqstp->rq_arg.head[0].iov_base = page_address(page);
  120. rqstp->rq_arg.head[0].iov_len =
  121. min_t(size_t, byte_count, ctxt->sge[0].length);
  122. rqstp->rq_arg.len = byte_count;
  123. rqstp->rq_arg.buflen = byte_count;
  124. /* Compute bytes past head in the SGL */
  125. bc = byte_count - rqstp->rq_arg.head[0].iov_len;
  126. /* If data remains, store it in the pagelist */
  127. rqstp->rq_arg.page_len = bc;
  128. rqstp->rq_arg.page_base = 0;
  129. sge_no = 1;
  130. while (bc && sge_no < ctxt->count) {
  131. page = ctxt->pages[sge_no];
  132. put_page(rqstp->rq_pages[sge_no]);
  133. rqstp->rq_pages[sge_no] = page;
  134. bc -= min_t(u32, bc, ctxt->sge[sge_no].length);
  135. sge_no++;
  136. }
  137. rqstp->rq_respages = &rqstp->rq_pages[sge_no];
  138. rqstp->rq_next_page = rqstp->rq_respages + 1;
  139. /* If not all pages were used from the SGL, free the remaining ones */
  140. bc = sge_no;
  141. while (sge_no < ctxt->count) {
  142. page = ctxt->pages[sge_no++];
  143. put_page(page);
  144. }
  145. ctxt->count = bc;
  146. /* Set up tail */
  147. rqstp->rq_arg.tail[0].iov_base = NULL;
  148. rqstp->rq_arg.tail[0].iov_len = 0;
  149. }
  150. /* This accommodates the largest possible Write chunk,
  151. * in one segment.
  152. */
  153. #define MAX_BYTES_WRITE_SEG ((u32)(RPCSVC_MAXPAGES << PAGE_SHIFT))
  154. /* This accommodates the largest possible Position-Zero
  155. * Read chunk or Reply chunk, in one segment.
  156. */
  157. #define MAX_BYTES_SPECIAL_SEG ((u32)((RPCSVC_MAXPAGES + 2) << PAGE_SHIFT))
  158. /* Sanity check the Read list.
  159. *
  160. * Implementation limits:
  161. * - This implementation supports only one Read chunk.
  162. *
  163. * Sanity checks:
  164. * - Read list does not overflow buffer.
  165. * - Segment size limited by largest NFS data payload.
  166. *
  167. * The segment count is limited to how many segments can
  168. * fit in the transport header without overflowing the
  169. * buffer. That's about 40 Read segments for a 1KB inline
  170. * threshold.
  171. *
  172. * Returns pointer to the following Write list.
  173. */
  174. static __be32 *xdr_check_read_list(__be32 *p, const __be32 *end)
  175. {
  176. u32 position;
  177. bool first;
  178. first = true;
  179. while (*p++ != xdr_zero) {
  180. if (first) {
  181. position = be32_to_cpup(p++);
  182. first = false;
  183. } else if (be32_to_cpup(p++) != position) {
  184. return NULL;
  185. }
  186. p++; /* handle */
  187. if (be32_to_cpup(p++) > MAX_BYTES_SPECIAL_SEG)
  188. return NULL;
  189. p += 2; /* offset */
  190. if (p > end)
  191. return NULL;
  192. }
  193. return p;
  194. }
  195. /* The segment count is limited to how many segments can
  196. * fit in the transport header without overflowing the
  197. * buffer. That's about 60 Write segments for a 1KB inline
  198. * threshold.
  199. */
  200. static __be32 *xdr_check_write_chunk(__be32 *p, const __be32 *end,
  201. u32 maxlen)
  202. {
  203. u32 i, segcount;
  204. segcount = be32_to_cpup(p++);
  205. for (i = 0; i < segcount; i++) {
  206. p++; /* handle */
  207. if (be32_to_cpup(p++) > maxlen)
  208. return NULL;
  209. p += 2; /* offset */
  210. if (p > end)
  211. return NULL;
  212. }
  213. return p;
  214. }
  215. /* Sanity check the Write list.
  216. *
  217. * Implementation limits:
  218. * - This implementation supports only one Write chunk.
  219. *
  220. * Sanity checks:
  221. * - Write list does not overflow buffer.
  222. * - Segment size limited by largest NFS data payload.
  223. *
  224. * Returns pointer to the following Reply chunk.
  225. */
  226. static __be32 *xdr_check_write_list(__be32 *p, const __be32 *end)
  227. {
  228. u32 chcount;
  229. chcount = 0;
  230. while (*p++ != xdr_zero) {
  231. p = xdr_check_write_chunk(p, end, MAX_BYTES_WRITE_SEG);
  232. if (!p)
  233. return NULL;
  234. if (chcount++ > 1)
  235. return NULL;
  236. }
  237. return p;
  238. }
  239. /* Sanity check the Reply chunk.
  240. *
  241. * Sanity checks:
  242. * - Reply chunk does not overflow buffer.
  243. * - Segment size limited by largest NFS data payload.
  244. *
  245. * Returns pointer to the following RPC header.
  246. */
  247. static __be32 *xdr_check_reply_chunk(__be32 *p, const __be32 *end)
  248. {
  249. if (*p++ != xdr_zero) {
  250. p = xdr_check_write_chunk(p, end, MAX_BYTES_SPECIAL_SEG);
  251. if (!p)
  252. return NULL;
  253. }
  254. return p;
  255. }
  256. /* On entry, xdr->head[0].iov_base points to first byte in the
  257. * RPC-over-RDMA header.
  258. *
  259. * On successful exit, head[0] points to first byte past the
  260. * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
  261. * The length of the RPC-over-RDMA header is returned.
  262. *
  263. * Assumptions:
  264. * - The transport header is entirely contained in the head iovec.
  265. */
  266. static int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg)
  267. {
  268. __be32 *p, *end, *rdma_argp;
  269. unsigned int hdr_len;
  270. char *proc;
  271. /* Verify that there's enough bytes for header + something */
  272. if (rq_arg->len <= RPCRDMA_HDRLEN_ERR)
  273. goto out_short;
  274. rdma_argp = rq_arg->head[0].iov_base;
  275. if (*(rdma_argp + 1) != rpcrdma_version)
  276. goto out_version;
  277. switch (*(rdma_argp + 3)) {
  278. case rdma_msg:
  279. proc = "RDMA_MSG";
  280. break;
  281. case rdma_nomsg:
  282. proc = "RDMA_NOMSG";
  283. break;
  284. case rdma_done:
  285. goto out_drop;
  286. case rdma_error:
  287. goto out_drop;
  288. default:
  289. goto out_proc;
  290. }
  291. end = (__be32 *)((unsigned long)rdma_argp + rq_arg->len);
  292. p = xdr_check_read_list(rdma_argp + 4, end);
  293. if (!p)
  294. goto out_inval;
  295. p = xdr_check_write_list(p, end);
  296. if (!p)
  297. goto out_inval;
  298. p = xdr_check_reply_chunk(p, end);
  299. if (!p)
  300. goto out_inval;
  301. if (p > end)
  302. goto out_inval;
  303. rq_arg->head[0].iov_base = p;
  304. hdr_len = (unsigned long)p - (unsigned long)rdma_argp;
  305. rq_arg->head[0].iov_len -= hdr_len;
  306. rq_arg->len -= hdr_len;
  307. dprintk("svcrdma: received %s request for XID 0x%08x, hdr_len=%u\n",
  308. proc, be32_to_cpup(rdma_argp), hdr_len);
  309. return hdr_len;
  310. out_short:
  311. dprintk("svcrdma: header too short = %d\n", rq_arg->len);
  312. return -EINVAL;
  313. out_version:
  314. dprintk("svcrdma: bad xprt version: %u\n",
  315. be32_to_cpup(rdma_argp + 1));
  316. return -EPROTONOSUPPORT;
  317. out_drop:
  318. dprintk("svcrdma: dropping RDMA_DONE/ERROR message\n");
  319. return 0;
  320. out_proc:
  321. dprintk("svcrdma: bad rdma procedure (%u)\n",
  322. be32_to_cpup(rdma_argp + 3));
  323. return -EINVAL;
  324. out_inval:
  325. dprintk("svcrdma: failed to parse transport header\n");
  326. return -EINVAL;
  327. }
  328. static void rdma_read_complete(struct svc_rqst *rqstp,
  329. struct svc_rdma_op_ctxt *head)
  330. {
  331. int page_no;
  332. /* Copy RPC pages */
  333. for (page_no = 0; page_no < head->count; page_no++) {
  334. put_page(rqstp->rq_pages[page_no]);
  335. rqstp->rq_pages[page_no] = head->pages[page_no];
  336. }
  337. /* Point rq_arg.pages past header */
  338. rqstp->rq_arg.pages = &rqstp->rq_pages[head->hdr_count];
  339. rqstp->rq_arg.page_len = head->arg.page_len;
  340. /* rq_respages starts after the last arg page */
  341. rqstp->rq_respages = &rqstp->rq_pages[page_no];
  342. rqstp->rq_next_page = rqstp->rq_respages + 1;
  343. /* Rebuild rq_arg head and tail. */
  344. rqstp->rq_arg.head[0] = head->arg.head[0];
  345. rqstp->rq_arg.tail[0] = head->arg.tail[0];
  346. rqstp->rq_arg.len = head->arg.len;
  347. rqstp->rq_arg.buflen = head->arg.buflen;
  348. }
  349. static void svc_rdma_send_error(struct svcxprt_rdma *xprt,
  350. __be32 *rdma_argp, int status)
  351. {
  352. struct svc_rdma_op_ctxt *ctxt;
  353. __be32 *p, *err_msgp;
  354. unsigned int length;
  355. struct page *page;
  356. int ret;
  357. page = alloc_page(GFP_KERNEL);
  358. if (!page)
  359. return;
  360. err_msgp = page_address(page);
  361. p = err_msgp;
  362. *p++ = *rdma_argp;
  363. *p++ = *(rdma_argp + 1);
  364. *p++ = xprt->sc_fc_credits;
  365. *p++ = rdma_error;
  366. if (status == -EPROTONOSUPPORT) {
  367. *p++ = err_vers;
  368. *p++ = rpcrdma_version;
  369. *p++ = rpcrdma_version;
  370. } else {
  371. *p++ = err_chunk;
  372. }
  373. length = (unsigned long)p - (unsigned long)err_msgp;
  374. /* Map transport header; no RPC message payload */
  375. ctxt = svc_rdma_get_context(xprt);
  376. ret = svc_rdma_map_reply_hdr(xprt, ctxt, err_msgp, length);
  377. if (ret) {
  378. dprintk("svcrdma: Error %d mapping send for protocol error\n",
  379. ret);
  380. return;
  381. }
  382. ret = svc_rdma_post_send_wr(xprt, ctxt, 1, 0);
  383. if (ret) {
  384. dprintk("svcrdma: Error %d posting send for protocol error\n",
  385. ret);
  386. svc_rdma_unmap_dma(ctxt);
  387. svc_rdma_put_context(ctxt, 1);
  388. }
  389. }
  390. /* By convention, backchannel calls arrive via rdma_msg type
  391. * messages, and never populate the chunk lists. This makes
  392. * the RPC/RDMA header small and fixed in size, so it is
  393. * straightforward to check the RPC header's direction field.
  394. */
  395. static bool svc_rdma_is_backchannel_reply(struct svc_xprt *xprt,
  396. __be32 *rdma_resp)
  397. {
  398. __be32 *p;
  399. if (!xprt->xpt_bc_xprt)
  400. return false;
  401. p = rdma_resp + 3;
  402. if (*p++ != rdma_msg)
  403. return false;
  404. if (*p++ != xdr_zero)
  405. return false;
  406. if (*p++ != xdr_zero)
  407. return false;
  408. if (*p++ != xdr_zero)
  409. return false;
  410. /* XID sanity */
  411. if (*p++ != *rdma_resp)
  412. return false;
  413. /* call direction */
  414. if (*p == cpu_to_be32(RPC_CALL))
  415. return false;
  416. return true;
  417. }
  418. /**
  419. * svc_rdma_recvfrom - Receive an RPC call
  420. * @rqstp: request structure into which to receive an RPC Call
  421. *
  422. * Returns:
  423. * The positive number of bytes in the RPC Call message,
  424. * %0 if there were no Calls ready to return,
  425. * %-EINVAL if the Read chunk data is too large,
  426. * %-ENOMEM if rdma_rw context pool was exhausted,
  427. * %-ENOTCONN if posting failed (connection is lost),
  428. * %-EIO if rdma_rw initialization failed (DMA mapping, etc).
  429. *
  430. * Called in a loop when XPT_DATA is set. XPT_DATA is cleared only
  431. * when there are no remaining ctxt's to process.
  432. *
  433. * The next ctxt is removed from the "receive" lists.
  434. *
  435. * - If the ctxt completes a Read, then finish assembling the Call
  436. * message and return the number of bytes in the message.
  437. *
  438. * - If the ctxt completes a Receive, then construct the Call
  439. * message from the contents of the Receive buffer.
  440. *
  441. * - If there are no Read chunks in this message, then finish
  442. * assembling the Call message and return the number of bytes
  443. * in the message.
  444. *
  445. * - If there are Read chunks in this message, post Read WRs to
  446. * pull that payload and return 0.
  447. */
  448. int svc_rdma_recvfrom(struct svc_rqst *rqstp)
  449. {
  450. struct svc_xprt *xprt = rqstp->rq_xprt;
  451. struct svcxprt_rdma *rdma_xprt =
  452. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  453. struct svc_rdma_op_ctxt *ctxt;
  454. __be32 *p;
  455. int ret;
  456. spin_lock(&rdma_xprt->sc_rq_dto_lock);
  457. if (!list_empty(&rdma_xprt->sc_read_complete_q)) {
  458. ctxt = list_first_entry(&rdma_xprt->sc_read_complete_q,
  459. struct svc_rdma_op_ctxt, list);
  460. list_del(&ctxt->list);
  461. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  462. rdma_read_complete(rqstp, ctxt);
  463. goto complete;
  464. } else if (!list_empty(&rdma_xprt->sc_rq_dto_q)) {
  465. ctxt = list_first_entry(&rdma_xprt->sc_rq_dto_q,
  466. struct svc_rdma_op_ctxt, list);
  467. list_del(&ctxt->list);
  468. } else {
  469. /* No new incoming requests, terminate the loop */
  470. clear_bit(XPT_DATA, &xprt->xpt_flags);
  471. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  472. return 0;
  473. }
  474. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  475. dprintk("svcrdma: recvfrom: ctxt=%p on xprt=%p, rqstp=%p\n",
  476. ctxt, rdma_xprt, rqstp);
  477. atomic_inc(&rdma_stat_recv);
  478. /* Build up the XDR from the receive buffers. */
  479. rdma_build_arg_xdr(rqstp, ctxt, ctxt->byte_len);
  480. /* Decode the RDMA header. */
  481. p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
  482. ret = svc_rdma_xdr_decode_req(&rqstp->rq_arg);
  483. if (ret < 0)
  484. goto out_err;
  485. if (ret == 0)
  486. goto out_drop;
  487. rqstp->rq_xprt_hlen = ret;
  488. if (svc_rdma_is_backchannel_reply(xprt, p)) {
  489. ret = svc_rdma_handle_bc_reply(xprt->xpt_bc_xprt, p,
  490. &rqstp->rq_arg);
  491. svc_rdma_put_context(ctxt, 0);
  492. return ret;
  493. }
  494. p += rpcrdma_fixed_maxsz;
  495. if (*p != xdr_zero)
  496. goto out_readchunk;
  497. complete:
  498. svc_rdma_put_context(ctxt, 0);
  499. dprintk("svcrdma: recvfrom: xprt=%p, rqstp=%p, rq_arg.len=%u\n",
  500. rdma_xprt, rqstp, rqstp->rq_arg.len);
  501. rqstp->rq_prot = IPPROTO_MAX;
  502. svc_xprt_copy_addrs(rqstp, xprt);
  503. return rqstp->rq_arg.len;
  504. out_readchunk:
  505. ret = svc_rdma_recv_read_chunk(rdma_xprt, rqstp, ctxt, p);
  506. if (ret < 0)
  507. goto out_postfail;
  508. return 0;
  509. out_err:
  510. svc_rdma_send_error(rdma_xprt, p, ret);
  511. svc_rdma_put_context(ctxt, 0);
  512. return 0;
  513. out_postfail:
  514. if (ret == -EINVAL)
  515. svc_rdma_send_error(rdma_xprt, p, ret);
  516. svc_rdma_put_context(ctxt, 1);
  517. return ret;
  518. out_drop:
  519. svc_rdma_put_context(ctxt, 1);
  520. return 0;
  521. }