rpc_rdma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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. /*
  400. * rpclen gets amount of data in first buffer, which is the
  401. * pre-registered buffer.
  402. */
  403. base = rqst->rq_svec[0].iov_base;
  404. rpclen = rqst->rq_svec[0].iov_len;
  405. headerp = rdmab_to_msg(req->rl_rdmabuf);
  406. /* don't byte-swap XID, it's already done in request */
  407. headerp->rm_xid = rqst->rq_xid;
  408. headerp->rm_vers = rpcrdma_version;
  409. headerp->rm_credit = cpu_to_be32(r_xprt->rx_buf.rb_max_requests);
  410. headerp->rm_type = rdma_msg;
  411. /*
  412. * Chunks needed for results?
  413. *
  414. * o Read ops return data as write chunk(s), header as inline.
  415. * o If the expected result is under the inline threshold, all ops
  416. * return as inline.
  417. * o Large non-read ops return as a single reply chunk.
  418. */
  419. if (rqst->rq_rcv_buf.flags & XDRBUF_READ)
  420. wtype = rpcrdma_writech;
  421. else if (rpcrdma_results_inline(rqst))
  422. wtype = rpcrdma_noch;
  423. else
  424. wtype = rpcrdma_replych;
  425. /*
  426. * Chunks needed for arguments?
  427. *
  428. * o If the total request is under the inline threshold, all ops
  429. * are sent as inline.
  430. * o Large write ops transmit data as read chunk(s), header as
  431. * inline.
  432. * o Large non-write ops are sent with the entire message as a
  433. * single read chunk (protocol 0-position special case).
  434. *
  435. * This assumes that the upper layer does not present a request
  436. * that both has a data payload, and whose non-data arguments
  437. * by themselves are larger than the inline threshold.
  438. */
  439. if (rpcrdma_args_inline(rqst)) {
  440. rtype = rpcrdma_noch;
  441. } else if (rqst->rq_snd_buf.flags & XDRBUF_WRITE) {
  442. rtype = rpcrdma_readch;
  443. } else {
  444. r_xprt->rx_stats.nomsg_call_count++;
  445. headerp->rm_type = htonl(RDMA_NOMSG);
  446. rtype = rpcrdma_areadch;
  447. rpclen = 0;
  448. }
  449. /* The following simplification is not true forever */
  450. if (rtype != rpcrdma_noch && wtype == rpcrdma_replych)
  451. wtype = rpcrdma_noch;
  452. if (rtype != rpcrdma_noch && wtype != rpcrdma_noch) {
  453. dprintk("RPC: %s: cannot marshal multiple chunk lists\n",
  454. __func__);
  455. return -EIO;
  456. }
  457. hdrlen = RPCRDMA_HDRLEN_MIN;
  458. /*
  459. * Pull up any extra send data into the preregistered buffer.
  460. * When padding is in use and applies to the transfer, insert
  461. * it and change the message type.
  462. */
  463. if (rtype == rpcrdma_noch) {
  464. rpcrdma_inline_pullup(rqst);
  465. headerp->rm_body.rm_nochunks.rm_empty[0] = xdr_zero;
  466. headerp->rm_body.rm_nochunks.rm_empty[1] = xdr_zero;
  467. headerp->rm_body.rm_nochunks.rm_empty[2] = xdr_zero;
  468. /* new length after pullup */
  469. rpclen = rqst->rq_svec[0].iov_len;
  470. } else if (rtype == rpcrdma_readch)
  471. rpclen += rpcrdma_tail_pullup(&rqst->rq_snd_buf);
  472. if (rtype != rpcrdma_noch) {
  473. hdrlen = rpcrdma_create_chunks(rqst, &rqst->rq_snd_buf,
  474. headerp, rtype);
  475. wtype = rtype; /* simplify dprintk */
  476. } else if (wtype != rpcrdma_noch) {
  477. hdrlen = rpcrdma_create_chunks(rqst, &rqst->rq_rcv_buf,
  478. headerp, wtype);
  479. }
  480. if (hdrlen < 0)
  481. return hdrlen;
  482. dprintk("RPC: %s: %s: hdrlen %zd rpclen %zd"
  483. " headerp 0x%p base 0x%p lkey 0x%x\n",
  484. __func__, transfertypes[wtype], hdrlen, rpclen,
  485. headerp, base, rdmab_lkey(req->rl_rdmabuf));
  486. /*
  487. * initialize send_iov's - normally only two: rdma chunk header and
  488. * single preregistered RPC header buffer, but if padding is present,
  489. * then use a preregistered (and zeroed) pad buffer between the RPC
  490. * header and any write data. In all non-rdma cases, any following
  491. * data has been copied into the RPC header buffer.
  492. */
  493. req->rl_send_iov[0].addr = rdmab_addr(req->rl_rdmabuf);
  494. req->rl_send_iov[0].length = hdrlen;
  495. req->rl_send_iov[0].lkey = rdmab_lkey(req->rl_rdmabuf);
  496. req->rl_niovs = 1;
  497. if (rtype == rpcrdma_areadch)
  498. return 0;
  499. req->rl_send_iov[1].addr = rdmab_addr(req->rl_sendbuf);
  500. req->rl_send_iov[1].length = rpclen;
  501. req->rl_send_iov[1].lkey = rdmab_lkey(req->rl_sendbuf);
  502. req->rl_niovs = 2;
  503. return 0;
  504. }
  505. /*
  506. * Chase down a received write or reply chunklist to get length
  507. * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
  508. */
  509. static int
  510. rpcrdma_count_chunks(struct rpcrdma_rep *rep, unsigned int max, int wrchunk, __be32 **iptrp)
  511. {
  512. unsigned int i, total_len;
  513. struct rpcrdma_write_chunk *cur_wchunk;
  514. char *base = (char *)rdmab_to_msg(rep->rr_rdmabuf);
  515. i = be32_to_cpu(**iptrp);
  516. if (i > max)
  517. return -1;
  518. cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
  519. total_len = 0;
  520. while (i--) {
  521. struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
  522. ifdebug(FACILITY) {
  523. u64 off;
  524. xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
  525. dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
  526. __func__,
  527. be32_to_cpu(seg->rs_length),
  528. (unsigned long long)off,
  529. be32_to_cpu(seg->rs_handle));
  530. }
  531. total_len += be32_to_cpu(seg->rs_length);
  532. ++cur_wchunk;
  533. }
  534. /* check and adjust for properly terminated write chunk */
  535. if (wrchunk) {
  536. __be32 *w = (__be32 *) cur_wchunk;
  537. if (*w++ != xdr_zero)
  538. return -1;
  539. cur_wchunk = (struct rpcrdma_write_chunk *) w;
  540. }
  541. if ((char *)cur_wchunk > base + rep->rr_len)
  542. return -1;
  543. *iptrp = (__be32 *) cur_wchunk;
  544. return total_len;
  545. }
  546. /*
  547. * Scatter inline received data back into provided iov's.
  548. */
  549. static void
  550. rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
  551. {
  552. int i, npages, curlen, olen;
  553. char *destp;
  554. struct page **ppages;
  555. int page_base;
  556. curlen = rqst->rq_rcv_buf.head[0].iov_len;
  557. if (curlen > copy_len) { /* write chunk header fixup */
  558. curlen = copy_len;
  559. rqst->rq_rcv_buf.head[0].iov_len = curlen;
  560. }
  561. dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
  562. __func__, srcp, copy_len, curlen);
  563. /* Shift pointer for first receive segment only */
  564. rqst->rq_rcv_buf.head[0].iov_base = srcp;
  565. srcp += curlen;
  566. copy_len -= curlen;
  567. olen = copy_len;
  568. i = 0;
  569. rpcx_to_rdmax(rqst->rq_xprt)->rx_stats.fixup_copy_count += olen;
  570. page_base = rqst->rq_rcv_buf.page_base;
  571. ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT);
  572. page_base &= ~PAGE_MASK;
  573. if (copy_len && rqst->rq_rcv_buf.page_len) {
  574. npages = PAGE_ALIGN(page_base +
  575. rqst->rq_rcv_buf.page_len) >> PAGE_SHIFT;
  576. for (; i < npages; i++) {
  577. curlen = PAGE_SIZE - page_base;
  578. if (curlen > copy_len)
  579. curlen = copy_len;
  580. dprintk("RPC: %s: page %d"
  581. " srcp 0x%p len %d curlen %d\n",
  582. __func__, i, srcp, copy_len, curlen);
  583. destp = kmap_atomic(ppages[i]);
  584. memcpy(destp + page_base, srcp, curlen);
  585. flush_dcache_page(ppages[i]);
  586. kunmap_atomic(destp);
  587. srcp += curlen;
  588. copy_len -= curlen;
  589. if (copy_len == 0)
  590. break;
  591. page_base = 0;
  592. }
  593. }
  594. if (copy_len && rqst->rq_rcv_buf.tail[0].iov_len) {
  595. curlen = copy_len;
  596. if (curlen > rqst->rq_rcv_buf.tail[0].iov_len)
  597. curlen = rqst->rq_rcv_buf.tail[0].iov_len;
  598. if (rqst->rq_rcv_buf.tail[0].iov_base != srcp)
  599. memmove(rqst->rq_rcv_buf.tail[0].iov_base, srcp, curlen);
  600. dprintk("RPC: %s: tail srcp 0x%p len %d curlen %d\n",
  601. __func__, srcp, copy_len, curlen);
  602. rqst->rq_rcv_buf.tail[0].iov_len = curlen;
  603. copy_len -= curlen; ++i;
  604. } else
  605. rqst->rq_rcv_buf.tail[0].iov_len = 0;
  606. if (pad) {
  607. /* implicit padding on terminal chunk */
  608. unsigned char *p = rqst->rq_rcv_buf.tail[0].iov_base;
  609. while (pad--)
  610. p[rqst->rq_rcv_buf.tail[0].iov_len++] = 0;
  611. }
  612. if (copy_len)
  613. dprintk("RPC: %s: %d bytes in"
  614. " %d extra segments (%d lost)\n",
  615. __func__, olen, i, copy_len);
  616. /* TBD avoid a warning from call_decode() */
  617. rqst->rq_private_buf = rqst->rq_rcv_buf;
  618. }
  619. void
  620. rpcrdma_connect_worker(struct work_struct *work)
  621. {
  622. struct rpcrdma_ep *ep =
  623. container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
  624. struct rpcrdma_xprt *r_xprt =
  625. container_of(ep, struct rpcrdma_xprt, rx_ep);
  626. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  627. spin_lock_bh(&xprt->transport_lock);
  628. if (++xprt->connect_cookie == 0) /* maintain a reserved value */
  629. ++xprt->connect_cookie;
  630. if (ep->rep_connected > 0) {
  631. if (!xprt_test_and_set_connected(xprt))
  632. xprt_wake_pending_tasks(xprt, 0);
  633. } else {
  634. if (xprt_test_and_clear_connected(xprt))
  635. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  636. }
  637. spin_unlock_bh(&xprt->transport_lock);
  638. }
  639. /*
  640. * This function is called when an async event is posted to
  641. * the connection which changes the connection state. All it
  642. * does at this point is mark the connection up/down, the rpc
  643. * timers do the rest.
  644. */
  645. void
  646. rpcrdma_conn_func(struct rpcrdma_ep *ep)
  647. {
  648. schedule_delayed_work(&ep->rep_connect_worker, 0);
  649. }
  650. /*
  651. * Called as a tasklet to do req/reply match and complete a request
  652. * Errors must result in the RPC task either being awakened, or
  653. * allowed to timeout, to discover the errors at that time.
  654. */
  655. void
  656. rpcrdma_reply_handler(struct rpcrdma_rep *rep)
  657. {
  658. struct rpcrdma_msg *headerp;
  659. struct rpcrdma_req *req;
  660. struct rpc_rqst *rqst;
  661. struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
  662. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  663. __be32 *iptr;
  664. int rdmalen, status;
  665. unsigned long cwnd;
  666. u32 credits;
  667. /* Check status. If bad, signal disconnect and return rep to pool */
  668. if (rep->rr_len == ~0U) {
  669. rpcrdma_recv_buffer_put(rep);
  670. if (r_xprt->rx_ep.rep_connected == 1) {
  671. r_xprt->rx_ep.rep_connected = -EIO;
  672. rpcrdma_conn_func(&r_xprt->rx_ep);
  673. }
  674. return;
  675. }
  676. if (rep->rr_len < RPCRDMA_HDRLEN_MIN) {
  677. dprintk("RPC: %s: short/invalid reply\n", __func__);
  678. goto repost;
  679. }
  680. headerp = rdmab_to_msg(rep->rr_rdmabuf);
  681. if (headerp->rm_vers != rpcrdma_version) {
  682. dprintk("RPC: %s: invalid version %d\n",
  683. __func__, be32_to_cpu(headerp->rm_vers));
  684. goto repost;
  685. }
  686. /* Get XID and try for a match. */
  687. spin_lock(&xprt->transport_lock);
  688. rqst = xprt_lookup_rqst(xprt, headerp->rm_xid);
  689. if (rqst == NULL) {
  690. spin_unlock(&xprt->transport_lock);
  691. dprintk("RPC: %s: reply 0x%p failed "
  692. "to match any request xid 0x%08x len %d\n",
  693. __func__, rep, be32_to_cpu(headerp->rm_xid),
  694. rep->rr_len);
  695. repost:
  696. r_xprt->rx_stats.bad_reply_count++;
  697. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
  698. rpcrdma_recv_buffer_put(rep);
  699. return;
  700. }
  701. /* get request object */
  702. req = rpcr_to_rdmar(rqst);
  703. if (req->rl_reply) {
  704. spin_unlock(&xprt->transport_lock);
  705. dprintk("RPC: %s: duplicate reply 0x%p to RPC "
  706. "request 0x%p: xid 0x%08x\n", __func__, rep, req,
  707. be32_to_cpu(headerp->rm_xid));
  708. goto repost;
  709. }
  710. dprintk("RPC: %s: reply 0x%p completes request 0x%p\n"
  711. " RPC request 0x%p xid 0x%08x\n",
  712. __func__, rep, req, rqst,
  713. be32_to_cpu(headerp->rm_xid));
  714. /* from here on, the reply is no longer an orphan */
  715. req->rl_reply = rep;
  716. xprt->reestablish_timeout = 0;
  717. /* check for expected message types */
  718. /* The order of some of these tests is important. */
  719. switch (headerp->rm_type) {
  720. case rdma_msg:
  721. /* never expect read chunks */
  722. /* never expect reply chunks (two ways to check) */
  723. /* never expect write chunks without having offered RDMA */
  724. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  725. (headerp->rm_body.rm_chunks[1] == xdr_zero &&
  726. headerp->rm_body.rm_chunks[2] != xdr_zero) ||
  727. (headerp->rm_body.rm_chunks[1] != xdr_zero &&
  728. req->rl_nchunks == 0))
  729. goto badheader;
  730. if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
  731. /* count any expected write chunks in read reply */
  732. /* start at write chunk array count */
  733. iptr = &headerp->rm_body.rm_chunks[2];
  734. rdmalen = rpcrdma_count_chunks(rep,
  735. req->rl_nchunks, 1, &iptr);
  736. /* check for validity, and no reply chunk after */
  737. if (rdmalen < 0 || *iptr++ != xdr_zero)
  738. goto badheader;
  739. rep->rr_len -=
  740. ((unsigned char *)iptr - (unsigned char *)headerp);
  741. status = rep->rr_len + rdmalen;
  742. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  743. /* special case - last chunk may omit padding */
  744. if (rdmalen &= 3) {
  745. rdmalen = 4 - rdmalen;
  746. status += rdmalen;
  747. }
  748. } else {
  749. /* else ordinary inline */
  750. rdmalen = 0;
  751. iptr = (__be32 *)((unsigned char *)headerp +
  752. RPCRDMA_HDRLEN_MIN);
  753. rep->rr_len -= RPCRDMA_HDRLEN_MIN;
  754. status = rep->rr_len;
  755. }
  756. /* Fix up the rpc results for upper layer */
  757. rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len, rdmalen);
  758. break;
  759. case rdma_nomsg:
  760. /* never expect read or write chunks, always reply chunks */
  761. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  762. headerp->rm_body.rm_chunks[1] != xdr_zero ||
  763. headerp->rm_body.rm_chunks[2] != xdr_one ||
  764. req->rl_nchunks == 0)
  765. goto badheader;
  766. iptr = (__be32 *)((unsigned char *)headerp +
  767. RPCRDMA_HDRLEN_MIN);
  768. rdmalen = rpcrdma_count_chunks(rep, req->rl_nchunks, 0, &iptr);
  769. if (rdmalen < 0)
  770. goto badheader;
  771. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  772. /* Reply chunk buffer already is the reply vector - no fixup. */
  773. status = rdmalen;
  774. break;
  775. badheader:
  776. default:
  777. dprintk("%s: invalid rpcrdma reply header (type %d):"
  778. " chunks[012] == %d %d %d"
  779. " expected chunks <= %d\n",
  780. __func__, be32_to_cpu(headerp->rm_type),
  781. headerp->rm_body.rm_chunks[0],
  782. headerp->rm_body.rm_chunks[1],
  783. headerp->rm_body.rm_chunks[2],
  784. req->rl_nchunks);
  785. status = -EIO;
  786. r_xprt->rx_stats.bad_reply_count++;
  787. break;
  788. }
  789. credits = be32_to_cpu(headerp->rm_credit);
  790. if (credits == 0)
  791. credits = 1; /* don't deadlock */
  792. else if (credits > r_xprt->rx_buf.rb_max_requests)
  793. credits = r_xprt->rx_buf.rb_max_requests;
  794. cwnd = xprt->cwnd;
  795. xprt->cwnd = credits << RPC_CWNDSHIFT;
  796. if (xprt->cwnd > cwnd)
  797. xprt_release_rqst_cong(rqst->rq_task);
  798. dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
  799. __func__, xprt, rqst, status);
  800. xprt_complete_rqst(rqst->rq_task, status);
  801. spin_unlock(&xprt->transport_lock);
  802. }