rpc_rdma.c 28 KB

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