rpc_rdma.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /*
  2. * Copyright (c) 2003-2007 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. /*
  40. * rpc_rdma.c
  41. *
  42. * This file contains the guts of the RPC RDMA protocol, and
  43. * does marshaling/unmarshaling, etc. It is also where interfacing
  44. * to the Linux RPC framework lives.
  45. */
  46. #include "xprt_rdma.h"
  47. #include <linux/highmem.h>
  48. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  49. # define RPCDBG_FACILITY RPCDBG_TRANS
  50. #endif
  51. enum rpcrdma_chunktype {
  52. rpcrdma_noch = 0,
  53. rpcrdma_readch,
  54. rpcrdma_areadch,
  55. rpcrdma_writech,
  56. rpcrdma_replych
  57. };
  58. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  59. static const char transfertypes[][12] = {
  60. "pure inline", /* no chunks */
  61. " read chunk", /* some argument via rdma read */
  62. "*read chunk", /* entire request via rdma read */
  63. "write chunk", /* some result via rdma write */
  64. "reply chunk" /* entire reply via rdma write */
  65. };
  66. #endif
  67. /* The client can send a request inline as long as the RPCRDMA header
  68. * plus the RPC call fit under the transport's inline limit. If the
  69. * combined call message size exceeds that limit, the client must use
  70. * the read chunk list for this operation.
  71. */
  72. static bool rpcrdma_args_inline(struct rpc_rqst *rqst)
  73. {
  74. unsigned int callsize = RPCRDMA_HDRLEN_MIN + rqst->rq_snd_buf.len;
  75. return callsize <= RPCRDMA_INLINE_WRITE_THRESHOLD(rqst);
  76. }
  77. /* The client can't know how large the actual reply will be. Thus it
  78. * plans for the largest possible reply for that particular ULP
  79. * operation. If the maximum combined reply message size exceeds that
  80. * limit, the client must provide a write list or a reply chunk for
  81. * this request.
  82. */
  83. static bool rpcrdma_results_inline(struct rpc_rqst *rqst)
  84. {
  85. unsigned int repsize = RPCRDMA_HDRLEN_MIN + rqst->rq_rcv_buf.buflen;
  86. return repsize <= RPCRDMA_INLINE_READ_THRESHOLD(rqst);
  87. }
  88. static int
  89. rpcrdma_tail_pullup(struct xdr_buf *buf)
  90. {
  91. size_t tlen = buf->tail[0].iov_len;
  92. size_t skip = tlen & 3;
  93. /* Do not include the tail if it is only an XDR pad */
  94. if (tlen < 4)
  95. return 0;
  96. /* xdr_write_pages() adds a pad at the beginning of the tail
  97. * if the content in "buf->pages" is unaligned. Force the
  98. * tail's actual content to land at the next XDR position
  99. * after the head instead.
  100. */
  101. if (skip) {
  102. unsigned char *src, *dst;
  103. unsigned int count;
  104. src = buf->tail[0].iov_base;
  105. dst = buf->head[0].iov_base;
  106. dst += buf->head[0].iov_len;
  107. src += skip;
  108. tlen -= skip;
  109. dprintk("RPC: %s: skip=%zu, memmove(%p, %p, %zu)\n",
  110. __func__, skip, dst, src, tlen);
  111. for (count = tlen; count; count--)
  112. *dst++ = *src++;
  113. }
  114. return tlen;
  115. }
  116. /*
  117. * Chunk assembly from upper layer xdr_buf.
  118. *
  119. * Prepare the passed-in xdr_buf into representation as RPC/RDMA chunk
  120. * elements. Segments are then coalesced when registered, if possible
  121. * within the selected memreg mode.
  122. *
  123. * Returns positive number of segments converted, or a negative errno.
  124. */
  125. static int
  126. rpcrdma_convert_iovs(struct xdr_buf *xdrbuf, unsigned int pos,
  127. enum rpcrdma_chunktype type, struct rpcrdma_mr_seg *seg, int nsegs)
  128. {
  129. int len, n = 0, p;
  130. int page_base;
  131. struct page **ppages;
  132. if (pos == 0 && xdrbuf->head[0].iov_len) {
  133. seg[n].mr_page = NULL;
  134. seg[n].mr_offset = xdrbuf->head[0].iov_base;
  135. seg[n].mr_len = xdrbuf->head[0].iov_len;
  136. ++n;
  137. }
  138. len = xdrbuf->page_len;
  139. ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT);
  140. page_base = xdrbuf->page_base & ~PAGE_MASK;
  141. p = 0;
  142. while (len && n < nsegs) {
  143. if (!ppages[p]) {
  144. /* alloc the pagelist for receiving buffer */
  145. ppages[p] = alloc_page(GFP_ATOMIC);
  146. if (!ppages[p])
  147. return -ENOMEM;
  148. }
  149. seg[n].mr_page = ppages[p];
  150. seg[n].mr_offset = (void *)(unsigned long) page_base;
  151. seg[n].mr_len = min_t(u32, PAGE_SIZE - page_base, len);
  152. if (seg[n].mr_len > PAGE_SIZE)
  153. return -EIO;
  154. len -= seg[n].mr_len;
  155. ++n;
  156. ++p;
  157. page_base = 0; /* page offset only applies to first page */
  158. }
  159. /* Message overflows the seg array */
  160. if (len && n == nsegs)
  161. return -EIO;
  162. /* When encoding the read list, the tail is always sent inline */
  163. if (type == rpcrdma_readch)
  164. return n;
  165. if (xdrbuf->tail[0].iov_len) {
  166. /* the rpcrdma protocol allows us to omit any trailing
  167. * xdr pad bytes, saving the server an RDMA operation. */
  168. if (xdrbuf->tail[0].iov_len < 4 && xprt_rdma_pad_optimize)
  169. return n;
  170. if (n == nsegs)
  171. /* Tail remains, but we're out of segments */
  172. return -EIO;
  173. seg[n].mr_page = NULL;
  174. seg[n].mr_offset = xdrbuf->tail[0].iov_base;
  175. seg[n].mr_len = xdrbuf->tail[0].iov_len;
  176. ++n;
  177. }
  178. return n;
  179. }
  180. /*
  181. * Create read/write chunk lists, and reply chunks, for RDMA
  182. *
  183. * Assume check against THRESHOLD has been done, and chunks are required.
  184. * Assume only encoding one list entry for read|write chunks. The NFSv3
  185. * protocol is simple enough to allow this as it only has a single "bulk
  186. * result" in each procedure - complicated NFSv4 COMPOUNDs are not. (The
  187. * RDMA/Sessions NFSv4 proposal addresses this for future v4 revs.)
  188. *
  189. * When used for a single reply chunk (which is a special write
  190. * chunk used for the entire reply, rather than just the data), it
  191. * is used primarily for READDIR and READLINK which would otherwise
  192. * be severely size-limited by a small rdma inline read max. The server
  193. * response will come back as an RDMA Write, followed by a message
  194. * of type RDMA_NOMSG carrying the xid and length. As a result, reply
  195. * chunks do not provide data alignment, however they do not require
  196. * "fixup" (moving the response to the upper layer buffer) either.
  197. *
  198. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  199. *
  200. * Read chunklist (a linked list):
  201. * N elements, position P (same P for all chunks of same arg!):
  202. * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
  203. *
  204. * Write chunklist (a list of (one) counted array):
  205. * N elements:
  206. * 1 - N - HLOO - HLOO - ... - HLOO - 0
  207. *
  208. * Reply chunk (a counted array):
  209. * N elements:
  210. * 1 - N - HLOO - HLOO - ... - HLOO
  211. *
  212. * Returns positive RPC/RDMA header size, or negative errno.
  213. */
  214. static ssize_t
  215. rpcrdma_create_chunks(struct rpc_rqst *rqst, struct xdr_buf *target,
  216. struct rpcrdma_msg *headerp, enum rpcrdma_chunktype type)
  217. {
  218. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  219. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  220. int n, nsegs, nchunks = 0;
  221. unsigned int pos;
  222. struct rpcrdma_mr_seg *seg = req->rl_segments;
  223. struct rpcrdma_read_chunk *cur_rchunk = NULL;
  224. struct rpcrdma_write_array *warray = NULL;
  225. struct rpcrdma_write_chunk *cur_wchunk = NULL;
  226. __be32 *iptr = headerp->rm_body.rm_chunks;
  227. int (*map)(struct rpcrdma_xprt *, struct rpcrdma_mr_seg *, int, bool);
  228. if (type == rpcrdma_readch || type == rpcrdma_areadch) {
  229. /* a read chunk - server will RDMA Read our memory */
  230. cur_rchunk = (struct rpcrdma_read_chunk *) iptr;
  231. } else {
  232. /* a write or reply chunk - server will RDMA Write our memory */
  233. *iptr++ = xdr_zero; /* encode a NULL read chunk list */
  234. if (type == rpcrdma_replych)
  235. *iptr++ = xdr_zero; /* a NULL write chunk list */
  236. warray = (struct rpcrdma_write_array *) iptr;
  237. cur_wchunk = (struct rpcrdma_write_chunk *) (warray + 1);
  238. }
  239. if (type == rpcrdma_replych || type == rpcrdma_areadch)
  240. pos = 0;
  241. else
  242. pos = target->head[0].iov_len;
  243. nsegs = rpcrdma_convert_iovs(target, pos, type, seg, RPCRDMA_MAX_SEGS);
  244. if (nsegs < 0)
  245. return nsegs;
  246. map = r_xprt->rx_ia.ri_ops->ro_map;
  247. do {
  248. n = map(r_xprt, seg, nsegs, cur_wchunk != NULL);
  249. if (n <= 0)
  250. goto out;
  251. if (cur_rchunk) { /* read */
  252. cur_rchunk->rc_discrim = xdr_one;
  253. /* all read chunks have the same "position" */
  254. cur_rchunk->rc_position = cpu_to_be32(pos);
  255. cur_rchunk->rc_target.rs_handle =
  256. cpu_to_be32(seg->mr_rkey);
  257. cur_rchunk->rc_target.rs_length =
  258. cpu_to_be32(seg->mr_len);
  259. xdr_encode_hyper(
  260. (__be32 *)&cur_rchunk->rc_target.rs_offset,
  261. seg->mr_base);
  262. dprintk("RPC: %s: read chunk "
  263. "elem %d@0x%llx:0x%x pos %u (%s)\n", __func__,
  264. seg->mr_len, (unsigned long long)seg->mr_base,
  265. seg->mr_rkey, pos, n < nsegs ? "more" : "last");
  266. cur_rchunk++;
  267. r_xprt->rx_stats.read_chunk_count++;
  268. } else { /* write/reply */
  269. cur_wchunk->wc_target.rs_handle =
  270. cpu_to_be32(seg->mr_rkey);
  271. cur_wchunk->wc_target.rs_length =
  272. cpu_to_be32(seg->mr_len);
  273. xdr_encode_hyper(
  274. (__be32 *)&cur_wchunk->wc_target.rs_offset,
  275. seg->mr_base);
  276. dprintk("RPC: %s: %s chunk "
  277. "elem %d@0x%llx:0x%x (%s)\n", __func__,
  278. (type == rpcrdma_replych) ? "reply" : "write",
  279. seg->mr_len, (unsigned long long)seg->mr_base,
  280. seg->mr_rkey, n < nsegs ? "more" : "last");
  281. cur_wchunk++;
  282. if (type == rpcrdma_replych)
  283. r_xprt->rx_stats.reply_chunk_count++;
  284. else
  285. r_xprt->rx_stats.write_chunk_count++;
  286. r_xprt->rx_stats.total_rdma_request += seg->mr_len;
  287. }
  288. nchunks++;
  289. seg += n;
  290. nsegs -= n;
  291. } while (nsegs);
  292. /* success. all failures return above */
  293. req->rl_nchunks = nchunks;
  294. /*
  295. * finish off header. If write, marshal discrim and nchunks.
  296. */
  297. if (cur_rchunk) {
  298. iptr = (__be32 *) cur_rchunk;
  299. *iptr++ = xdr_zero; /* finish the read chunk list */
  300. *iptr++ = xdr_zero; /* encode a NULL write chunk list */
  301. *iptr++ = xdr_zero; /* encode a NULL reply chunk */
  302. } else {
  303. warray->wc_discrim = xdr_one;
  304. warray->wc_nchunks = cpu_to_be32(nchunks);
  305. iptr = (__be32 *) cur_wchunk;
  306. if (type == rpcrdma_writech) {
  307. *iptr++ = xdr_zero; /* finish the write chunk list */
  308. *iptr++ = xdr_zero; /* encode a NULL reply chunk */
  309. }
  310. }
  311. /*
  312. * Return header size.
  313. */
  314. return (unsigned char *)iptr - (unsigned char *)headerp;
  315. out:
  316. for (pos = 0; nchunks--;)
  317. pos += r_xprt->rx_ia.ri_ops->ro_unmap(r_xprt,
  318. &req->rl_segments[pos]);
  319. return n;
  320. }
  321. /*
  322. * Copy write data inline.
  323. * This function is used for "small" requests. Data which is passed
  324. * to RPC via iovecs (or page list) is copied directly into the
  325. * pre-registered memory buffer for this request. For small amounts
  326. * of data, this is efficient. The cutoff value is tunable.
  327. */
  328. static void rpcrdma_inline_pullup(struct rpc_rqst *rqst)
  329. {
  330. int i, npages, curlen;
  331. int copy_len;
  332. unsigned char *srcp, *destp;
  333. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  334. int page_base;
  335. struct page **ppages;
  336. destp = rqst->rq_svec[0].iov_base;
  337. curlen = rqst->rq_svec[0].iov_len;
  338. destp += curlen;
  339. dprintk("RPC: %s: destp 0x%p len %d hdrlen %d\n",
  340. __func__, destp, rqst->rq_slen, curlen);
  341. copy_len = rqst->rq_snd_buf.page_len;
  342. if (rqst->rq_snd_buf.tail[0].iov_len) {
  343. curlen = rqst->rq_snd_buf.tail[0].iov_len;
  344. if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) {
  345. memmove(destp + copy_len,
  346. rqst->rq_snd_buf.tail[0].iov_base, curlen);
  347. r_xprt->rx_stats.pullup_copy_count += curlen;
  348. }
  349. dprintk("RPC: %s: tail destp 0x%p len %d\n",
  350. __func__, destp + copy_len, curlen);
  351. rqst->rq_svec[0].iov_len += curlen;
  352. }
  353. r_xprt->rx_stats.pullup_copy_count += copy_len;
  354. page_base = rqst->rq_snd_buf.page_base;
  355. ppages = rqst->rq_snd_buf.pages + (page_base >> PAGE_SHIFT);
  356. page_base &= ~PAGE_MASK;
  357. npages = PAGE_ALIGN(page_base+copy_len) >> PAGE_SHIFT;
  358. for (i = 0; copy_len && i < npages; i++) {
  359. curlen = PAGE_SIZE - page_base;
  360. if (curlen > copy_len)
  361. curlen = copy_len;
  362. dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n",
  363. __func__, i, destp, copy_len, curlen);
  364. srcp = kmap_atomic(ppages[i]);
  365. memcpy(destp, srcp+page_base, curlen);
  366. kunmap_atomic(srcp);
  367. rqst->rq_svec[0].iov_len += curlen;
  368. destp += curlen;
  369. copy_len -= curlen;
  370. page_base = 0;
  371. }
  372. /* header now contains entire send message */
  373. }
  374. /*
  375. * Marshal a request: the primary job of this routine is to choose
  376. * the transfer modes. See comments below.
  377. *
  378. * Uses multiple RDMA IOVs for a request:
  379. * [0] -- RPC RDMA header, which uses memory from the *start* of the
  380. * preregistered buffer that already holds the RPC data in
  381. * its middle.
  382. * [1] -- the RPC header/data, marshaled by RPC and the NFS protocol.
  383. * [2] -- optional padding.
  384. * [3] -- if padded, header only in [1] and data here.
  385. *
  386. * Returns zero on success, otherwise a negative errno.
  387. */
  388. int
  389. rpcrdma_marshal_req(struct rpc_rqst *rqst)
  390. {
  391. struct rpc_xprt *xprt = rqst->rq_xprt;
  392. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  393. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  394. char *base;
  395. size_t rpclen;
  396. ssize_t hdrlen;
  397. enum rpcrdma_chunktype rtype, wtype;
  398. struct rpcrdma_msg *headerp;
  399. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  400. if (test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state))
  401. return rpcrdma_bc_marshal_reply(rqst);
  402. #endif
  403. /*
  404. * rpclen gets amount of data in first buffer, which is the
  405. * pre-registered buffer.
  406. */
  407. base = rqst->rq_svec[0].iov_base;
  408. rpclen = rqst->rq_svec[0].iov_len;
  409. headerp = rdmab_to_msg(req->rl_rdmabuf);
  410. /* don't byte-swap XID, it's already done in request */
  411. headerp->rm_xid = rqst->rq_xid;
  412. headerp->rm_vers = rpcrdma_version;
  413. headerp->rm_credit = cpu_to_be32(r_xprt->rx_buf.rb_max_requests);
  414. headerp->rm_type = rdma_msg;
  415. /*
  416. * Chunks needed for results?
  417. *
  418. * o Read ops return data as write chunk(s), header as inline.
  419. * o If the expected result is under the inline threshold, all ops
  420. * return as inline.
  421. * o Large non-read ops return as a single reply chunk.
  422. */
  423. if (rqst->rq_rcv_buf.flags & XDRBUF_READ)
  424. wtype = rpcrdma_writech;
  425. else if (rpcrdma_results_inline(rqst))
  426. wtype = rpcrdma_noch;
  427. else
  428. wtype = rpcrdma_replych;
  429. /*
  430. * Chunks needed for arguments?
  431. *
  432. * o If the total request is under the inline threshold, all ops
  433. * are sent as inline.
  434. * o Large write ops transmit data as read chunk(s), header as
  435. * inline.
  436. * o Large non-write ops are sent with the entire message as a
  437. * single read chunk (protocol 0-position special case).
  438. *
  439. * This assumes that the upper layer does not present a request
  440. * that both has a data payload, and whose non-data arguments
  441. * by themselves are larger than the inline threshold.
  442. */
  443. if (rpcrdma_args_inline(rqst)) {
  444. rtype = rpcrdma_noch;
  445. } else if (rqst->rq_snd_buf.flags & XDRBUF_WRITE) {
  446. rtype = rpcrdma_readch;
  447. } else {
  448. r_xprt->rx_stats.nomsg_call_count++;
  449. headerp->rm_type = htonl(RDMA_NOMSG);
  450. rtype = rpcrdma_areadch;
  451. rpclen = 0;
  452. }
  453. /* The following simplification is not true forever */
  454. if (rtype != rpcrdma_noch && wtype == rpcrdma_replych)
  455. wtype = rpcrdma_noch;
  456. if (rtype != rpcrdma_noch && wtype != rpcrdma_noch) {
  457. dprintk("RPC: %s: cannot marshal multiple chunk lists\n",
  458. __func__);
  459. return -EIO;
  460. }
  461. hdrlen = RPCRDMA_HDRLEN_MIN;
  462. /*
  463. * Pull up any extra send data into the preregistered buffer.
  464. * When padding is in use and applies to the transfer, insert
  465. * it and change the message type.
  466. */
  467. if (rtype == rpcrdma_noch) {
  468. rpcrdma_inline_pullup(rqst);
  469. headerp->rm_body.rm_nochunks.rm_empty[0] = xdr_zero;
  470. headerp->rm_body.rm_nochunks.rm_empty[1] = xdr_zero;
  471. headerp->rm_body.rm_nochunks.rm_empty[2] = xdr_zero;
  472. /* new length after pullup */
  473. rpclen = rqst->rq_svec[0].iov_len;
  474. } else if (rtype == rpcrdma_readch)
  475. rpclen += rpcrdma_tail_pullup(&rqst->rq_snd_buf);
  476. if (rtype != rpcrdma_noch) {
  477. hdrlen = rpcrdma_create_chunks(rqst, &rqst->rq_snd_buf,
  478. headerp, rtype);
  479. wtype = rtype; /* simplify dprintk */
  480. } else if (wtype != rpcrdma_noch) {
  481. hdrlen = rpcrdma_create_chunks(rqst, &rqst->rq_rcv_buf,
  482. headerp, wtype);
  483. }
  484. if (hdrlen < 0)
  485. return hdrlen;
  486. dprintk("RPC: %s: %s: hdrlen %zd rpclen %zd"
  487. " headerp 0x%p base 0x%p lkey 0x%x\n",
  488. __func__, transfertypes[wtype], hdrlen, rpclen,
  489. headerp, base, rdmab_lkey(req->rl_rdmabuf));
  490. /*
  491. * initialize send_iov's - normally only two: rdma chunk header and
  492. * single preregistered RPC header buffer, but if padding is present,
  493. * then use a preregistered (and zeroed) pad buffer between the RPC
  494. * header and any write data. In all non-rdma cases, any following
  495. * data has been copied into the RPC header buffer.
  496. */
  497. req->rl_send_iov[0].addr = rdmab_addr(req->rl_rdmabuf);
  498. req->rl_send_iov[0].length = hdrlen;
  499. req->rl_send_iov[0].lkey = rdmab_lkey(req->rl_rdmabuf);
  500. req->rl_niovs = 1;
  501. if (rtype == rpcrdma_areadch)
  502. return 0;
  503. req->rl_send_iov[1].addr = rdmab_addr(req->rl_sendbuf);
  504. req->rl_send_iov[1].length = rpclen;
  505. req->rl_send_iov[1].lkey = rdmab_lkey(req->rl_sendbuf);
  506. req->rl_niovs = 2;
  507. return 0;
  508. }
  509. /*
  510. * Chase down a received write or reply chunklist to get length
  511. * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
  512. */
  513. static int
  514. rpcrdma_count_chunks(struct rpcrdma_rep *rep, unsigned int max, int wrchunk, __be32 **iptrp)
  515. {
  516. unsigned int i, total_len;
  517. struct rpcrdma_write_chunk *cur_wchunk;
  518. char *base = (char *)rdmab_to_msg(rep->rr_rdmabuf);
  519. i = be32_to_cpu(**iptrp);
  520. if (i > max)
  521. return -1;
  522. cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
  523. total_len = 0;
  524. while (i--) {
  525. struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
  526. ifdebug(FACILITY) {
  527. u64 off;
  528. xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
  529. dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
  530. __func__,
  531. be32_to_cpu(seg->rs_length),
  532. (unsigned long long)off,
  533. be32_to_cpu(seg->rs_handle));
  534. }
  535. total_len += be32_to_cpu(seg->rs_length);
  536. ++cur_wchunk;
  537. }
  538. /* check and adjust for properly terminated write chunk */
  539. if (wrchunk) {
  540. __be32 *w = (__be32 *) cur_wchunk;
  541. if (*w++ != xdr_zero)
  542. return -1;
  543. cur_wchunk = (struct rpcrdma_write_chunk *) w;
  544. }
  545. if ((char *)cur_wchunk > base + rep->rr_len)
  546. return -1;
  547. *iptrp = (__be32 *) cur_wchunk;
  548. return total_len;
  549. }
  550. /*
  551. * Scatter inline received data back into provided iov's.
  552. */
  553. static void
  554. rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
  555. {
  556. int i, npages, curlen, olen;
  557. char *destp;
  558. struct page **ppages;
  559. int page_base;
  560. curlen = rqst->rq_rcv_buf.head[0].iov_len;
  561. if (curlen > copy_len) { /* write chunk header fixup */
  562. curlen = copy_len;
  563. rqst->rq_rcv_buf.head[0].iov_len = curlen;
  564. }
  565. dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
  566. __func__, srcp, copy_len, curlen);
  567. /* Shift pointer for first receive segment only */
  568. rqst->rq_rcv_buf.head[0].iov_base = srcp;
  569. srcp += curlen;
  570. copy_len -= curlen;
  571. olen = copy_len;
  572. i = 0;
  573. rpcx_to_rdmax(rqst->rq_xprt)->rx_stats.fixup_copy_count += olen;
  574. page_base = rqst->rq_rcv_buf.page_base;
  575. ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT);
  576. page_base &= ~PAGE_MASK;
  577. if (copy_len && rqst->rq_rcv_buf.page_len) {
  578. npages = PAGE_ALIGN(page_base +
  579. rqst->rq_rcv_buf.page_len) >> PAGE_SHIFT;
  580. for (; i < npages; i++) {
  581. curlen = PAGE_SIZE - page_base;
  582. if (curlen > copy_len)
  583. curlen = copy_len;
  584. dprintk("RPC: %s: page %d"
  585. " srcp 0x%p len %d curlen %d\n",
  586. __func__, i, srcp, copy_len, curlen);
  587. destp = kmap_atomic(ppages[i]);
  588. memcpy(destp + page_base, srcp, curlen);
  589. flush_dcache_page(ppages[i]);
  590. kunmap_atomic(destp);
  591. srcp += curlen;
  592. copy_len -= curlen;
  593. if (copy_len == 0)
  594. break;
  595. page_base = 0;
  596. }
  597. }
  598. if (copy_len && rqst->rq_rcv_buf.tail[0].iov_len) {
  599. curlen = copy_len;
  600. if (curlen > rqst->rq_rcv_buf.tail[0].iov_len)
  601. curlen = rqst->rq_rcv_buf.tail[0].iov_len;
  602. if (rqst->rq_rcv_buf.tail[0].iov_base != srcp)
  603. memmove(rqst->rq_rcv_buf.tail[0].iov_base, srcp, curlen);
  604. dprintk("RPC: %s: tail srcp 0x%p len %d curlen %d\n",
  605. __func__, srcp, copy_len, curlen);
  606. rqst->rq_rcv_buf.tail[0].iov_len = curlen;
  607. copy_len -= curlen; ++i;
  608. } else
  609. rqst->rq_rcv_buf.tail[0].iov_len = 0;
  610. if (pad) {
  611. /* implicit padding on terminal chunk */
  612. unsigned char *p = rqst->rq_rcv_buf.tail[0].iov_base;
  613. while (pad--)
  614. p[rqst->rq_rcv_buf.tail[0].iov_len++] = 0;
  615. }
  616. if (copy_len)
  617. dprintk("RPC: %s: %d bytes in"
  618. " %d extra segments (%d lost)\n",
  619. __func__, olen, i, copy_len);
  620. /* TBD avoid a warning from call_decode() */
  621. rqst->rq_private_buf = rqst->rq_rcv_buf;
  622. }
  623. void
  624. rpcrdma_connect_worker(struct work_struct *work)
  625. {
  626. struct rpcrdma_ep *ep =
  627. container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
  628. struct rpcrdma_xprt *r_xprt =
  629. container_of(ep, struct rpcrdma_xprt, rx_ep);
  630. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  631. spin_lock_bh(&xprt->transport_lock);
  632. if (++xprt->connect_cookie == 0) /* maintain a reserved value */
  633. ++xprt->connect_cookie;
  634. if (ep->rep_connected > 0) {
  635. if (!xprt_test_and_set_connected(xprt))
  636. xprt_wake_pending_tasks(xprt, 0);
  637. } else {
  638. if (xprt_test_and_clear_connected(xprt))
  639. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  640. }
  641. spin_unlock_bh(&xprt->transport_lock);
  642. }
  643. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  644. /* By convention, backchannel calls arrive via rdma_msg type
  645. * messages, and never populate the chunk lists. This makes
  646. * the RPC/RDMA header small and fixed in size, so it is
  647. * straightforward to check the RPC header's direction field.
  648. */
  649. static bool
  650. rpcrdma_is_bcall(struct rpcrdma_msg *headerp)
  651. {
  652. __be32 *p = (__be32 *)headerp;
  653. if (headerp->rm_type != rdma_msg)
  654. return false;
  655. if (headerp->rm_body.rm_chunks[0] != xdr_zero)
  656. return false;
  657. if (headerp->rm_body.rm_chunks[1] != xdr_zero)
  658. return false;
  659. if (headerp->rm_body.rm_chunks[2] != xdr_zero)
  660. return false;
  661. /* sanity */
  662. if (p[7] != headerp->rm_xid)
  663. return false;
  664. /* call direction */
  665. if (p[8] != cpu_to_be32(RPC_CALL))
  666. return false;
  667. return true;
  668. }
  669. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  670. /*
  671. * This function is called when an async event is posted to
  672. * the connection which changes the connection state. All it
  673. * does at this point is mark the connection up/down, the rpc
  674. * timers do the rest.
  675. */
  676. void
  677. rpcrdma_conn_func(struct rpcrdma_ep *ep)
  678. {
  679. schedule_delayed_work(&ep->rep_connect_worker, 0);
  680. }
  681. /* Process received RPC/RDMA messages.
  682. *
  683. * Errors must result in the RPC task either being awakened, or
  684. * allowed to timeout, to discover the errors at that time.
  685. */
  686. void
  687. rpcrdma_reply_handler(struct rpcrdma_rep *rep)
  688. {
  689. struct rpcrdma_msg *headerp;
  690. struct rpcrdma_req *req;
  691. struct rpc_rqst *rqst;
  692. struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
  693. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  694. __be32 *iptr;
  695. int rdmalen, status;
  696. unsigned long cwnd;
  697. u32 credits;
  698. dprintk("RPC: %s: incoming rep %p\n", __func__, rep);
  699. if (rep->rr_len == RPCRDMA_BAD_LEN)
  700. goto out_badstatus;
  701. if (rep->rr_len < RPCRDMA_HDRLEN_MIN)
  702. goto out_shortreply;
  703. headerp = rdmab_to_msg(rep->rr_rdmabuf);
  704. if (headerp->rm_vers != rpcrdma_version)
  705. goto out_badversion;
  706. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  707. if (rpcrdma_is_bcall(headerp))
  708. goto out_bcall;
  709. #endif
  710. /* Match incoming rpcrdma_rep to an rpcrdma_req to
  711. * get context for handling any incoming chunks.
  712. */
  713. spin_lock_bh(&xprt->transport_lock);
  714. rqst = xprt_lookup_rqst(xprt, headerp->rm_xid);
  715. if (!rqst)
  716. goto out_nomatch;
  717. req = rpcr_to_rdmar(rqst);
  718. if (req->rl_reply)
  719. goto out_duplicate;
  720. /* Sanity checking has passed. We are now committed
  721. * to complete this transaction.
  722. */
  723. list_del_init(&rqst->rq_list);
  724. spin_unlock_bh(&xprt->transport_lock);
  725. dprintk("RPC: %s: reply 0x%p completes request 0x%p\n"
  726. " RPC request 0x%p xid 0x%08x\n",
  727. __func__, rep, req, rqst,
  728. be32_to_cpu(headerp->rm_xid));
  729. /* from here on, the reply is no longer an orphan */
  730. req->rl_reply = rep;
  731. xprt->reestablish_timeout = 0;
  732. /* check for expected message types */
  733. /* The order of some of these tests is important. */
  734. switch (headerp->rm_type) {
  735. case rdma_msg:
  736. /* never expect read chunks */
  737. /* never expect reply chunks (two ways to check) */
  738. /* never expect write chunks without having offered RDMA */
  739. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  740. (headerp->rm_body.rm_chunks[1] == xdr_zero &&
  741. headerp->rm_body.rm_chunks[2] != xdr_zero) ||
  742. (headerp->rm_body.rm_chunks[1] != xdr_zero &&
  743. req->rl_nchunks == 0))
  744. goto badheader;
  745. if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
  746. /* count any expected write chunks in read reply */
  747. /* start at write chunk array count */
  748. iptr = &headerp->rm_body.rm_chunks[2];
  749. rdmalen = rpcrdma_count_chunks(rep,
  750. req->rl_nchunks, 1, &iptr);
  751. /* check for validity, and no reply chunk after */
  752. if (rdmalen < 0 || *iptr++ != xdr_zero)
  753. goto badheader;
  754. rep->rr_len -=
  755. ((unsigned char *)iptr - (unsigned char *)headerp);
  756. status = rep->rr_len + rdmalen;
  757. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  758. /* special case - last chunk may omit padding */
  759. if (rdmalen &= 3) {
  760. rdmalen = 4 - rdmalen;
  761. status += rdmalen;
  762. }
  763. } else {
  764. /* else ordinary inline */
  765. rdmalen = 0;
  766. iptr = (__be32 *)((unsigned char *)headerp +
  767. RPCRDMA_HDRLEN_MIN);
  768. rep->rr_len -= RPCRDMA_HDRLEN_MIN;
  769. status = rep->rr_len;
  770. }
  771. /* Fix up the rpc results for upper layer */
  772. rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len, rdmalen);
  773. break;
  774. case rdma_nomsg:
  775. /* never expect read or write chunks, always reply chunks */
  776. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  777. headerp->rm_body.rm_chunks[1] != xdr_zero ||
  778. headerp->rm_body.rm_chunks[2] != xdr_one ||
  779. req->rl_nchunks == 0)
  780. goto badheader;
  781. iptr = (__be32 *)((unsigned char *)headerp +
  782. RPCRDMA_HDRLEN_MIN);
  783. rdmalen = rpcrdma_count_chunks(rep, req->rl_nchunks, 0, &iptr);
  784. if (rdmalen < 0)
  785. goto badheader;
  786. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  787. /* Reply chunk buffer already is the reply vector - no fixup. */
  788. status = rdmalen;
  789. break;
  790. badheader:
  791. default:
  792. dprintk("%s: invalid rpcrdma reply header (type %d):"
  793. " chunks[012] == %d %d %d"
  794. " expected chunks <= %d\n",
  795. __func__, be32_to_cpu(headerp->rm_type),
  796. headerp->rm_body.rm_chunks[0],
  797. headerp->rm_body.rm_chunks[1],
  798. headerp->rm_body.rm_chunks[2],
  799. req->rl_nchunks);
  800. status = -EIO;
  801. r_xprt->rx_stats.bad_reply_count++;
  802. break;
  803. }
  804. /* Invalidate and flush the data payloads before waking the
  805. * waiting application. This guarantees the memory region is
  806. * properly fenced from the server before the application
  807. * accesses the data. It also ensures proper send flow
  808. * control: waking the next RPC waits until this RPC has
  809. * relinquished all its Send Queue entries.
  810. */
  811. if (req->rl_nchunks)
  812. r_xprt->rx_ia.ri_ops->ro_unmap_sync(r_xprt, req);
  813. credits = be32_to_cpu(headerp->rm_credit);
  814. if (credits == 0)
  815. credits = 1; /* don't deadlock */
  816. else if (credits > r_xprt->rx_buf.rb_max_requests)
  817. credits = r_xprt->rx_buf.rb_max_requests;
  818. spin_lock_bh(&xprt->transport_lock);
  819. cwnd = xprt->cwnd;
  820. xprt->cwnd = credits << RPC_CWNDSHIFT;
  821. if (xprt->cwnd > cwnd)
  822. xprt_release_rqst_cong(rqst->rq_task);
  823. xprt_complete_rqst(rqst->rq_task, status);
  824. spin_unlock_bh(&xprt->transport_lock);
  825. dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
  826. __func__, xprt, rqst, status);
  827. return;
  828. out_badstatus:
  829. rpcrdma_recv_buffer_put(rep);
  830. if (r_xprt->rx_ep.rep_connected == 1) {
  831. r_xprt->rx_ep.rep_connected = -EIO;
  832. rpcrdma_conn_func(&r_xprt->rx_ep);
  833. }
  834. return;
  835. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  836. out_bcall:
  837. rpcrdma_bc_receive_call(r_xprt, rep);
  838. return;
  839. #endif
  840. out_shortreply:
  841. dprintk("RPC: %s: short/invalid reply\n", __func__);
  842. goto repost;
  843. out_badversion:
  844. dprintk("RPC: %s: invalid version %d\n",
  845. __func__, be32_to_cpu(headerp->rm_vers));
  846. goto repost;
  847. out_nomatch:
  848. spin_unlock_bh(&xprt->transport_lock);
  849. dprintk("RPC: %s: no match for incoming xid 0x%08x len %d\n",
  850. __func__, be32_to_cpu(headerp->rm_xid),
  851. rep->rr_len);
  852. goto repost;
  853. out_duplicate:
  854. spin_unlock_bh(&xprt->transport_lock);
  855. dprintk("RPC: %s: "
  856. "duplicate reply %p to RPC request %p: xid 0x%08x\n",
  857. __func__, rep, req, be32_to_cpu(headerp->rm_xid));
  858. repost:
  859. r_xprt->rx_stats.bad_reply_count++;
  860. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
  861. rpcrdma_recv_buffer_put(rep);
  862. }