rpc_rdma.c 27 KB

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