svc_rdma_recvfrom.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2016-2018 Oracle. All rights reserved.
  4. * Copyright (c) 2014 Open Grid Computing, Inc. All rights reserved.
  5. * Copyright (c) 2005-2006 Network Appliance, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the BSD-type
  11. * license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * Neither the name of the Network Appliance, Inc. nor the names of
  26. * its contributors may be used to endorse or promote products
  27. * derived from this software without specific prior written
  28. * permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  36. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  37. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  38. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  39. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  40. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * Author: Tom Tucker <tom@opengridcomputing.com>
  43. */
  44. /* Operation
  45. *
  46. * The main entry point is svc_rdma_recvfrom. This is called from
  47. * svc_recv when the transport indicates there is incoming data to
  48. * be read. "Data Ready" is signaled when an RDMA Receive completes,
  49. * or when a set of RDMA Reads complete.
  50. *
  51. * An svc_rqst is passed in. This structure contains an array of
  52. * free pages (rq_pages) that will contain the incoming RPC message.
  53. *
  54. * Short messages are moved directly into svc_rqst::rq_arg, and
  55. * the RPC Call is ready to be processed by the Upper Layer.
  56. * svc_rdma_recvfrom returns the length of the RPC Call message,
  57. * completing the reception of the RPC Call.
  58. *
  59. * However, when an incoming message has Read chunks,
  60. * svc_rdma_recvfrom must post RDMA Reads to pull the RPC Call's
  61. * data payload from the client. svc_rdma_recvfrom sets up the
  62. * RDMA Reads using pages in svc_rqst::rq_pages, which are
  63. * transferred to an svc_rdma_recv_ctxt for the duration of the
  64. * I/O. svc_rdma_recvfrom then returns zero, since the RPC message
  65. * is still not yet ready.
  66. *
  67. * When the Read chunk payloads have become available on the
  68. * server, "Data Ready" is raised again, and svc_recv calls
  69. * svc_rdma_recvfrom again. This second call may use a different
  70. * svc_rqst than the first one, thus any information that needs
  71. * to be preserved across these two calls is kept in an
  72. * svc_rdma_recv_ctxt.
  73. *
  74. * The second call to svc_rdma_recvfrom performs final assembly
  75. * of the RPC Call message, using the RDMA Read sink pages kept in
  76. * the svc_rdma_recv_ctxt. The xdr_buf is copied from the
  77. * svc_rdma_recv_ctxt to the second svc_rqst. The second call returns
  78. * the length of the completed RPC Call message.
  79. *
  80. * Page Management
  81. *
  82. * Pages under I/O must be transferred from the first svc_rqst to an
  83. * svc_rdma_recv_ctxt before the first svc_rdma_recvfrom call returns.
  84. *
  85. * The first svc_rqst supplies pages for RDMA Reads. These are moved
  86. * from rqstp::rq_pages into ctxt::pages. The consumed elements of
  87. * the rq_pages array are set to NULL and refilled with the first
  88. * svc_rdma_recvfrom call returns.
  89. *
  90. * During the second svc_rdma_recvfrom call, RDMA Read sink pages
  91. * are transferred from the svc_rdma_recv_ctxt to the second svc_rqst
  92. * (see rdma_read_complete() below).
  93. */
  94. #include <linux/spinlock.h>
  95. #include <asm/unaligned.h>
  96. #include <rdma/ib_verbs.h>
  97. #include <rdma/rdma_cm.h>
  98. #include <linux/sunrpc/xdr.h>
  99. #include <linux/sunrpc/debug.h>
  100. #include <linux/sunrpc/rpc_rdma.h>
  101. #include <linux/sunrpc/svc_rdma.h>
  102. #include "xprt_rdma.h"
  103. #include <trace/events/rpcrdma.h>
  104. #define RPCDBG_FACILITY RPCDBG_SVCXPRT
  105. static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc);
  106. static inline struct svc_rdma_recv_ctxt *
  107. svc_rdma_next_recv_ctxt(struct list_head *list)
  108. {
  109. return list_first_entry_or_null(list, struct svc_rdma_recv_ctxt,
  110. rc_list);
  111. }
  112. static struct svc_rdma_recv_ctxt *
  113. svc_rdma_recv_ctxt_alloc(struct svcxprt_rdma *rdma)
  114. {
  115. struct svc_rdma_recv_ctxt *ctxt;
  116. dma_addr_t addr;
  117. void *buffer;
  118. ctxt = kmalloc(sizeof(*ctxt), GFP_KERNEL);
  119. if (!ctxt)
  120. goto fail0;
  121. buffer = kmalloc(rdma->sc_max_req_size, GFP_KERNEL);
  122. if (!buffer)
  123. goto fail1;
  124. addr = ib_dma_map_single(rdma->sc_pd->device, buffer,
  125. rdma->sc_max_req_size, DMA_FROM_DEVICE);
  126. if (ib_dma_mapping_error(rdma->sc_pd->device, addr))
  127. goto fail2;
  128. ctxt->rc_recv_wr.next = NULL;
  129. ctxt->rc_recv_wr.wr_cqe = &ctxt->rc_cqe;
  130. ctxt->rc_recv_wr.sg_list = &ctxt->rc_recv_sge;
  131. ctxt->rc_recv_wr.num_sge = 1;
  132. ctxt->rc_cqe.done = svc_rdma_wc_receive;
  133. ctxt->rc_recv_sge.addr = addr;
  134. ctxt->rc_recv_sge.length = rdma->sc_max_req_size;
  135. ctxt->rc_recv_sge.lkey = rdma->sc_pd->local_dma_lkey;
  136. ctxt->rc_recv_buf = buffer;
  137. ctxt->rc_temp = false;
  138. return ctxt;
  139. fail2:
  140. kfree(buffer);
  141. fail1:
  142. kfree(ctxt);
  143. fail0:
  144. return NULL;
  145. }
  146. static void svc_rdma_recv_ctxt_destroy(struct svcxprt_rdma *rdma,
  147. struct svc_rdma_recv_ctxt *ctxt)
  148. {
  149. ib_dma_unmap_single(rdma->sc_pd->device, ctxt->rc_recv_sge.addr,
  150. ctxt->rc_recv_sge.length, DMA_FROM_DEVICE);
  151. kfree(ctxt->rc_recv_buf);
  152. kfree(ctxt);
  153. }
  154. /**
  155. * svc_rdma_recv_ctxts_destroy - Release all recv_ctxt's for an xprt
  156. * @rdma: svcxprt_rdma being torn down
  157. *
  158. */
  159. void svc_rdma_recv_ctxts_destroy(struct svcxprt_rdma *rdma)
  160. {
  161. struct svc_rdma_recv_ctxt *ctxt;
  162. while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_recv_ctxts))) {
  163. list_del(&ctxt->rc_list);
  164. svc_rdma_recv_ctxt_destroy(rdma, ctxt);
  165. }
  166. }
  167. static struct svc_rdma_recv_ctxt *
  168. svc_rdma_recv_ctxt_get(struct svcxprt_rdma *rdma)
  169. {
  170. struct svc_rdma_recv_ctxt *ctxt;
  171. spin_lock(&rdma->sc_recv_lock);
  172. ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_recv_ctxts);
  173. if (!ctxt)
  174. goto out_empty;
  175. list_del(&ctxt->rc_list);
  176. spin_unlock(&rdma->sc_recv_lock);
  177. out:
  178. ctxt->rc_page_count = 0;
  179. return ctxt;
  180. out_empty:
  181. spin_unlock(&rdma->sc_recv_lock);
  182. ctxt = svc_rdma_recv_ctxt_alloc(rdma);
  183. if (!ctxt)
  184. return NULL;
  185. goto out;
  186. }
  187. /**
  188. * svc_rdma_recv_ctxt_put - Return recv_ctxt to free list
  189. * @rdma: controlling svcxprt_rdma
  190. * @ctxt: object to return to the free list
  191. *
  192. */
  193. void svc_rdma_recv_ctxt_put(struct svcxprt_rdma *rdma,
  194. struct svc_rdma_recv_ctxt *ctxt)
  195. {
  196. unsigned int i;
  197. for (i = 0; i < ctxt->rc_page_count; i++)
  198. put_page(ctxt->rc_pages[i]);
  199. if (!ctxt->rc_temp) {
  200. spin_lock(&rdma->sc_recv_lock);
  201. list_add(&ctxt->rc_list, &rdma->sc_recv_ctxts);
  202. spin_unlock(&rdma->sc_recv_lock);
  203. } else
  204. svc_rdma_recv_ctxt_destroy(rdma, ctxt);
  205. }
  206. /**
  207. * svc_rdma_release_rqst - Release transport-specific per-rqst resources
  208. * @rqstp: svc_rqst being released
  209. *
  210. * Ensure that the recv_ctxt is released whether or not a Reply
  211. * was sent. For example, the client could close the connection,
  212. * or svc_process could drop an RPC, before the Reply is sent.
  213. */
  214. void svc_rdma_release_rqst(struct svc_rqst *rqstp)
  215. {
  216. struct svc_rdma_recv_ctxt *ctxt = rqstp->rq_xprt_ctxt;
  217. struct svc_xprt *xprt = rqstp->rq_xprt;
  218. struct svcxprt_rdma *rdma =
  219. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  220. rqstp->rq_xprt_ctxt = NULL;
  221. if (ctxt)
  222. svc_rdma_recv_ctxt_put(rdma, ctxt);
  223. }
  224. static int __svc_rdma_post_recv(struct svcxprt_rdma *rdma,
  225. struct svc_rdma_recv_ctxt *ctxt)
  226. {
  227. int ret;
  228. svc_xprt_get(&rdma->sc_xprt);
  229. ret = ib_post_recv(rdma->sc_qp, &ctxt->rc_recv_wr, NULL);
  230. trace_svcrdma_post_recv(&ctxt->rc_recv_wr, ret);
  231. if (ret)
  232. goto err_post;
  233. return 0;
  234. err_post:
  235. svc_rdma_recv_ctxt_put(rdma, ctxt);
  236. svc_xprt_put(&rdma->sc_xprt);
  237. return ret;
  238. }
  239. static int svc_rdma_post_recv(struct svcxprt_rdma *rdma)
  240. {
  241. struct svc_rdma_recv_ctxt *ctxt;
  242. if (test_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags))
  243. return 0;
  244. ctxt = svc_rdma_recv_ctxt_get(rdma);
  245. if (!ctxt)
  246. return -ENOMEM;
  247. return __svc_rdma_post_recv(rdma, ctxt);
  248. }
  249. /**
  250. * svc_rdma_post_recvs - Post initial set of Recv WRs
  251. * @rdma: fresh svcxprt_rdma
  252. *
  253. * Returns true if successful, otherwise false.
  254. */
  255. bool svc_rdma_post_recvs(struct svcxprt_rdma *rdma)
  256. {
  257. struct svc_rdma_recv_ctxt *ctxt;
  258. unsigned int i;
  259. int ret;
  260. for (i = 0; i < rdma->sc_max_requests; i++) {
  261. ctxt = svc_rdma_recv_ctxt_get(rdma);
  262. if (!ctxt)
  263. return false;
  264. ctxt->rc_temp = true;
  265. ret = __svc_rdma_post_recv(rdma, ctxt);
  266. if (ret) {
  267. pr_err("svcrdma: failure posting recv buffers: %d\n",
  268. ret);
  269. return false;
  270. }
  271. }
  272. return true;
  273. }
  274. /**
  275. * svc_rdma_wc_receive - Invoked by RDMA provider for each polled Receive WC
  276. * @cq: Completion Queue context
  277. * @wc: Work Completion object
  278. *
  279. * NB: The svc_xprt/svcxprt_rdma is pinned whenever it's possible that
  280. * the Receive completion handler could be running.
  281. */
  282. static void svc_rdma_wc_receive(struct ib_cq *cq, struct ib_wc *wc)
  283. {
  284. struct svcxprt_rdma *rdma = cq->cq_context;
  285. struct ib_cqe *cqe = wc->wr_cqe;
  286. struct svc_rdma_recv_ctxt *ctxt;
  287. trace_svcrdma_wc_receive(wc);
  288. /* WARNING: Only wc->wr_cqe and wc->status are reliable */
  289. ctxt = container_of(cqe, struct svc_rdma_recv_ctxt, rc_cqe);
  290. if (wc->status != IB_WC_SUCCESS)
  291. goto flushed;
  292. if (svc_rdma_post_recv(rdma))
  293. goto post_err;
  294. /* All wc fields are now known to be valid */
  295. ctxt->rc_byte_len = wc->byte_len;
  296. ib_dma_sync_single_for_cpu(rdma->sc_pd->device,
  297. ctxt->rc_recv_sge.addr,
  298. wc->byte_len, DMA_FROM_DEVICE);
  299. spin_lock(&rdma->sc_rq_dto_lock);
  300. list_add_tail(&ctxt->rc_list, &rdma->sc_rq_dto_q);
  301. spin_unlock(&rdma->sc_rq_dto_lock);
  302. set_bit(XPT_DATA, &rdma->sc_xprt.xpt_flags);
  303. if (!test_bit(RDMAXPRT_CONN_PENDING, &rdma->sc_flags))
  304. svc_xprt_enqueue(&rdma->sc_xprt);
  305. goto out;
  306. flushed:
  307. if (wc->status != IB_WC_WR_FLUSH_ERR)
  308. pr_err("svcrdma: Recv: %s (%u/0x%x)\n",
  309. ib_wc_status_msg(wc->status),
  310. wc->status, wc->vendor_err);
  311. post_err:
  312. svc_rdma_recv_ctxt_put(rdma, ctxt);
  313. set_bit(XPT_CLOSE, &rdma->sc_xprt.xpt_flags);
  314. svc_xprt_enqueue(&rdma->sc_xprt);
  315. out:
  316. svc_xprt_put(&rdma->sc_xprt);
  317. }
  318. /**
  319. * svc_rdma_flush_recv_queues - Drain pending Receive work
  320. * @rdma: svcxprt_rdma being shut down
  321. *
  322. */
  323. void svc_rdma_flush_recv_queues(struct svcxprt_rdma *rdma)
  324. {
  325. struct svc_rdma_recv_ctxt *ctxt;
  326. while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_read_complete_q))) {
  327. list_del(&ctxt->rc_list);
  328. svc_rdma_recv_ctxt_put(rdma, ctxt);
  329. }
  330. while ((ctxt = svc_rdma_next_recv_ctxt(&rdma->sc_rq_dto_q))) {
  331. list_del(&ctxt->rc_list);
  332. svc_rdma_recv_ctxt_put(rdma, ctxt);
  333. }
  334. }
  335. static void svc_rdma_build_arg_xdr(struct svc_rqst *rqstp,
  336. struct svc_rdma_recv_ctxt *ctxt)
  337. {
  338. struct xdr_buf *arg = &rqstp->rq_arg;
  339. arg->head[0].iov_base = ctxt->rc_recv_buf;
  340. arg->head[0].iov_len = ctxt->rc_byte_len;
  341. arg->tail[0].iov_base = NULL;
  342. arg->tail[0].iov_len = 0;
  343. arg->page_len = 0;
  344. arg->page_base = 0;
  345. arg->buflen = ctxt->rc_byte_len;
  346. arg->len = ctxt->rc_byte_len;
  347. }
  348. /* This accommodates the largest possible Write chunk,
  349. * in one segment.
  350. */
  351. #define MAX_BYTES_WRITE_SEG ((u32)(RPCSVC_MAXPAGES << PAGE_SHIFT))
  352. /* This accommodates the largest possible Position-Zero
  353. * Read chunk or Reply chunk, in one segment.
  354. */
  355. #define MAX_BYTES_SPECIAL_SEG ((u32)((RPCSVC_MAXPAGES + 2) << PAGE_SHIFT))
  356. /* Sanity check the Read list.
  357. *
  358. * Implementation limits:
  359. * - This implementation supports only one Read chunk.
  360. *
  361. * Sanity checks:
  362. * - Read list does not overflow buffer.
  363. * - Segment size limited by largest NFS data payload.
  364. *
  365. * The segment count is limited to how many segments can
  366. * fit in the transport header without overflowing the
  367. * buffer. That's about 40 Read segments for a 1KB inline
  368. * threshold.
  369. *
  370. * Returns pointer to the following Write list.
  371. */
  372. static __be32 *xdr_check_read_list(__be32 *p, const __be32 *end)
  373. {
  374. u32 position;
  375. bool first;
  376. first = true;
  377. while (*p++ != xdr_zero) {
  378. if (first) {
  379. position = be32_to_cpup(p++);
  380. first = false;
  381. } else if (be32_to_cpup(p++) != position) {
  382. return NULL;
  383. }
  384. p++; /* handle */
  385. if (be32_to_cpup(p++) > MAX_BYTES_SPECIAL_SEG)
  386. return NULL;
  387. p += 2; /* offset */
  388. if (p > end)
  389. return NULL;
  390. }
  391. return p;
  392. }
  393. /* The segment count is limited to how many segments can
  394. * fit in the transport header without overflowing the
  395. * buffer. That's about 60 Write segments for a 1KB inline
  396. * threshold.
  397. */
  398. static __be32 *xdr_check_write_chunk(__be32 *p, const __be32 *end,
  399. u32 maxlen)
  400. {
  401. u32 i, segcount;
  402. segcount = be32_to_cpup(p++);
  403. for (i = 0; i < segcount; i++) {
  404. p++; /* handle */
  405. if (be32_to_cpup(p++) > maxlen)
  406. return NULL;
  407. p += 2; /* offset */
  408. if (p > end)
  409. return NULL;
  410. }
  411. return p;
  412. }
  413. /* Sanity check the Write list.
  414. *
  415. * Implementation limits:
  416. * - This implementation supports only one Write chunk.
  417. *
  418. * Sanity checks:
  419. * - Write list does not overflow buffer.
  420. * - Segment size limited by largest NFS data payload.
  421. *
  422. * Returns pointer to the following Reply chunk.
  423. */
  424. static __be32 *xdr_check_write_list(__be32 *p, const __be32 *end)
  425. {
  426. u32 chcount;
  427. chcount = 0;
  428. while (*p++ != xdr_zero) {
  429. p = xdr_check_write_chunk(p, end, MAX_BYTES_WRITE_SEG);
  430. if (!p)
  431. return NULL;
  432. if (chcount++ > 1)
  433. return NULL;
  434. }
  435. return p;
  436. }
  437. /* Sanity check the Reply chunk.
  438. *
  439. * Sanity checks:
  440. * - Reply chunk does not overflow buffer.
  441. * - Segment size limited by largest NFS data payload.
  442. *
  443. * Returns pointer to the following RPC header.
  444. */
  445. static __be32 *xdr_check_reply_chunk(__be32 *p, const __be32 *end)
  446. {
  447. if (*p++ != xdr_zero) {
  448. p = xdr_check_write_chunk(p, end, MAX_BYTES_SPECIAL_SEG);
  449. if (!p)
  450. return NULL;
  451. }
  452. return p;
  453. }
  454. /* On entry, xdr->head[0].iov_base points to first byte in the
  455. * RPC-over-RDMA header.
  456. *
  457. * On successful exit, head[0] points to first byte past the
  458. * RPC-over-RDMA header. For RDMA_MSG, this is the RPC message.
  459. * The length of the RPC-over-RDMA header is returned.
  460. *
  461. * Assumptions:
  462. * - The transport header is entirely contained in the head iovec.
  463. */
  464. static int svc_rdma_xdr_decode_req(struct xdr_buf *rq_arg)
  465. {
  466. __be32 *p, *end, *rdma_argp;
  467. unsigned int hdr_len;
  468. /* Verify that there's enough bytes for header + something */
  469. if (rq_arg->len <= RPCRDMA_HDRLEN_ERR)
  470. goto out_short;
  471. rdma_argp = rq_arg->head[0].iov_base;
  472. if (*(rdma_argp + 1) != rpcrdma_version)
  473. goto out_version;
  474. switch (*(rdma_argp + 3)) {
  475. case rdma_msg:
  476. break;
  477. case rdma_nomsg:
  478. break;
  479. case rdma_done:
  480. goto out_drop;
  481. case rdma_error:
  482. goto out_drop;
  483. default:
  484. goto out_proc;
  485. }
  486. end = (__be32 *)((unsigned long)rdma_argp + rq_arg->len);
  487. p = xdr_check_read_list(rdma_argp + 4, end);
  488. if (!p)
  489. goto out_inval;
  490. p = xdr_check_write_list(p, end);
  491. if (!p)
  492. goto out_inval;
  493. p = xdr_check_reply_chunk(p, end);
  494. if (!p)
  495. goto out_inval;
  496. if (p > end)
  497. goto out_inval;
  498. rq_arg->head[0].iov_base = p;
  499. hdr_len = (unsigned long)p - (unsigned long)rdma_argp;
  500. rq_arg->head[0].iov_len -= hdr_len;
  501. rq_arg->len -= hdr_len;
  502. trace_svcrdma_decode_rqst(rdma_argp, hdr_len);
  503. return hdr_len;
  504. out_short:
  505. trace_svcrdma_decode_short(rq_arg->len);
  506. return -EINVAL;
  507. out_version:
  508. trace_svcrdma_decode_badvers(rdma_argp);
  509. return -EPROTONOSUPPORT;
  510. out_drop:
  511. trace_svcrdma_decode_drop(rdma_argp);
  512. return 0;
  513. out_proc:
  514. trace_svcrdma_decode_badproc(rdma_argp);
  515. return -EINVAL;
  516. out_inval:
  517. trace_svcrdma_decode_parse(rdma_argp);
  518. return -EINVAL;
  519. }
  520. static void rdma_read_complete(struct svc_rqst *rqstp,
  521. struct svc_rdma_recv_ctxt *head)
  522. {
  523. int page_no;
  524. /* Move Read chunk pages to rqstp so that they will be released
  525. * when svc_process is done with them.
  526. */
  527. for (page_no = 0; page_no < head->rc_page_count; page_no++) {
  528. put_page(rqstp->rq_pages[page_no]);
  529. rqstp->rq_pages[page_no] = head->rc_pages[page_no];
  530. }
  531. head->rc_page_count = 0;
  532. /* Point rq_arg.pages past header */
  533. rqstp->rq_arg.pages = &rqstp->rq_pages[head->rc_hdr_count];
  534. rqstp->rq_arg.page_len = head->rc_arg.page_len;
  535. /* rq_respages starts after the last arg page */
  536. rqstp->rq_respages = &rqstp->rq_pages[page_no];
  537. rqstp->rq_next_page = rqstp->rq_respages + 1;
  538. /* Rebuild rq_arg head and tail. */
  539. rqstp->rq_arg.head[0] = head->rc_arg.head[0];
  540. rqstp->rq_arg.tail[0] = head->rc_arg.tail[0];
  541. rqstp->rq_arg.len = head->rc_arg.len;
  542. rqstp->rq_arg.buflen = head->rc_arg.buflen;
  543. }
  544. static void svc_rdma_send_error(struct svcxprt_rdma *xprt,
  545. __be32 *rdma_argp, int status)
  546. {
  547. struct svc_rdma_send_ctxt *ctxt;
  548. unsigned int length;
  549. __be32 *p;
  550. int ret;
  551. ctxt = svc_rdma_send_ctxt_get(xprt);
  552. if (!ctxt)
  553. return;
  554. p = ctxt->sc_xprt_buf;
  555. *p++ = *rdma_argp;
  556. *p++ = *(rdma_argp + 1);
  557. *p++ = xprt->sc_fc_credits;
  558. *p++ = rdma_error;
  559. switch (status) {
  560. case -EPROTONOSUPPORT:
  561. *p++ = err_vers;
  562. *p++ = rpcrdma_version;
  563. *p++ = rpcrdma_version;
  564. trace_svcrdma_err_vers(*rdma_argp);
  565. break;
  566. default:
  567. *p++ = err_chunk;
  568. trace_svcrdma_err_chunk(*rdma_argp);
  569. }
  570. length = (unsigned long)p - (unsigned long)ctxt->sc_xprt_buf;
  571. svc_rdma_sync_reply_hdr(xprt, ctxt, length);
  572. ctxt->sc_send_wr.opcode = IB_WR_SEND;
  573. ret = svc_rdma_send(xprt, &ctxt->sc_send_wr);
  574. if (ret)
  575. svc_rdma_send_ctxt_put(xprt, ctxt);
  576. }
  577. /* By convention, backchannel calls arrive via rdma_msg type
  578. * messages, and never populate the chunk lists. This makes
  579. * the RPC/RDMA header small and fixed in size, so it is
  580. * straightforward to check the RPC header's direction field.
  581. */
  582. static bool svc_rdma_is_backchannel_reply(struct svc_xprt *xprt,
  583. __be32 *rdma_resp)
  584. {
  585. __be32 *p;
  586. if (!xprt->xpt_bc_xprt)
  587. return false;
  588. p = rdma_resp + 3;
  589. if (*p++ != rdma_msg)
  590. return false;
  591. if (*p++ != xdr_zero)
  592. return false;
  593. if (*p++ != xdr_zero)
  594. return false;
  595. if (*p++ != xdr_zero)
  596. return false;
  597. /* XID sanity */
  598. if (*p++ != *rdma_resp)
  599. return false;
  600. /* call direction */
  601. if (*p == cpu_to_be32(RPC_CALL))
  602. return false;
  603. return true;
  604. }
  605. /**
  606. * svc_rdma_recvfrom - Receive an RPC call
  607. * @rqstp: request structure into which to receive an RPC Call
  608. *
  609. * Returns:
  610. * The positive number of bytes in the RPC Call message,
  611. * %0 if there were no Calls ready to return,
  612. * %-EINVAL if the Read chunk data is too large,
  613. * %-ENOMEM if rdma_rw context pool was exhausted,
  614. * %-ENOTCONN if posting failed (connection is lost),
  615. * %-EIO if rdma_rw initialization failed (DMA mapping, etc).
  616. *
  617. * Called in a loop when XPT_DATA is set. XPT_DATA is cleared only
  618. * when there are no remaining ctxt's to process.
  619. *
  620. * The next ctxt is removed from the "receive" lists.
  621. *
  622. * - If the ctxt completes a Read, then finish assembling the Call
  623. * message and return the number of bytes in the message.
  624. *
  625. * - If the ctxt completes a Receive, then construct the Call
  626. * message from the contents of the Receive buffer.
  627. *
  628. * - If there are no Read chunks in this message, then finish
  629. * assembling the Call message and return the number of bytes
  630. * in the message.
  631. *
  632. * - If there are Read chunks in this message, post Read WRs to
  633. * pull that payload and return 0.
  634. */
  635. int svc_rdma_recvfrom(struct svc_rqst *rqstp)
  636. {
  637. struct svc_xprt *xprt = rqstp->rq_xprt;
  638. struct svcxprt_rdma *rdma_xprt =
  639. container_of(xprt, struct svcxprt_rdma, sc_xprt);
  640. struct svc_rdma_recv_ctxt *ctxt;
  641. __be32 *p;
  642. int ret;
  643. rqstp->rq_xprt_ctxt = NULL;
  644. spin_lock(&rdma_xprt->sc_rq_dto_lock);
  645. ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_read_complete_q);
  646. if (ctxt) {
  647. list_del(&ctxt->rc_list);
  648. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  649. rdma_read_complete(rqstp, ctxt);
  650. goto complete;
  651. }
  652. ctxt = svc_rdma_next_recv_ctxt(&rdma_xprt->sc_rq_dto_q);
  653. if (!ctxt) {
  654. /* No new incoming requests, terminate the loop */
  655. clear_bit(XPT_DATA, &xprt->xpt_flags);
  656. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  657. return 0;
  658. }
  659. list_del(&ctxt->rc_list);
  660. spin_unlock(&rdma_xprt->sc_rq_dto_lock);
  661. atomic_inc(&rdma_stat_recv);
  662. svc_rdma_build_arg_xdr(rqstp, ctxt);
  663. /* Prevent svc_xprt_release from releasing pages in rq_pages
  664. * if we return 0 or an error.
  665. */
  666. rqstp->rq_respages = rqstp->rq_pages;
  667. rqstp->rq_next_page = rqstp->rq_respages;
  668. p = (__be32 *)rqstp->rq_arg.head[0].iov_base;
  669. ret = svc_rdma_xdr_decode_req(&rqstp->rq_arg);
  670. if (ret < 0)
  671. goto out_err;
  672. if (ret == 0)
  673. goto out_drop;
  674. rqstp->rq_xprt_hlen = ret;
  675. if (svc_rdma_is_backchannel_reply(xprt, p)) {
  676. ret = svc_rdma_handle_bc_reply(xprt->xpt_bc_xprt, p,
  677. &rqstp->rq_arg);
  678. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  679. return ret;
  680. }
  681. p += rpcrdma_fixed_maxsz;
  682. if (*p != xdr_zero)
  683. goto out_readchunk;
  684. complete:
  685. rqstp->rq_xprt_ctxt = ctxt;
  686. rqstp->rq_prot = IPPROTO_MAX;
  687. svc_xprt_copy_addrs(rqstp, xprt);
  688. return rqstp->rq_arg.len;
  689. out_readchunk:
  690. ret = svc_rdma_recv_read_chunk(rdma_xprt, rqstp, ctxt, p);
  691. if (ret < 0)
  692. goto out_postfail;
  693. return 0;
  694. out_err:
  695. svc_rdma_send_error(rdma_xprt, p, ret);
  696. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  697. return 0;
  698. out_postfail:
  699. if (ret == -EINVAL)
  700. svc_rdma_send_error(rdma_xprt, p, ret);
  701. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  702. return ret;
  703. out_drop:
  704. svc_rdma_recv_ctxt_put(rdma_xprt, ctxt);
  705. return 0;
  706. }