rpc_rdma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. #ifdef RPC_DEBUG
  49. # define RPCDBG_FACILITY RPCDBG_TRANS
  50. #endif
  51. #ifdef RPC_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 = htonl(pos);
  195. cur_rchunk->rc_target.rs_handle = htonl(seg->mr_rkey);
  196. cur_rchunk->rc_target.rs_length = htonl(seg->mr_len);
  197. xdr_encode_hyper(
  198. (__be32 *)&cur_rchunk->rc_target.rs_offset,
  199. seg->mr_base);
  200. dprintk("RPC: %s: read chunk "
  201. "elem %d@0x%llx:0x%x pos %u (%s)\n", __func__,
  202. seg->mr_len, (unsigned long long)seg->mr_base,
  203. seg->mr_rkey, pos, n < nsegs ? "more" : "last");
  204. cur_rchunk++;
  205. r_xprt->rx_stats.read_chunk_count++;
  206. } else { /* write/reply */
  207. cur_wchunk->wc_target.rs_handle = htonl(seg->mr_rkey);
  208. cur_wchunk->wc_target.rs_length = htonl(seg->mr_len);
  209. xdr_encode_hyper(
  210. (__be32 *)&cur_wchunk->wc_target.rs_offset,
  211. seg->mr_base);
  212. dprintk("RPC: %s: %s chunk "
  213. "elem %d@0x%llx:0x%x (%s)\n", __func__,
  214. (type == rpcrdma_replych) ? "reply" : "write",
  215. seg->mr_len, (unsigned long long)seg->mr_base,
  216. seg->mr_rkey, n < nsegs ? "more" : "last");
  217. cur_wchunk++;
  218. if (type == rpcrdma_replych)
  219. r_xprt->rx_stats.reply_chunk_count++;
  220. else
  221. r_xprt->rx_stats.write_chunk_count++;
  222. r_xprt->rx_stats.total_rdma_request += seg->mr_len;
  223. }
  224. nchunks++;
  225. seg += n;
  226. nsegs -= n;
  227. } while (nsegs);
  228. /* success. all failures return above */
  229. req->rl_nchunks = nchunks;
  230. /*
  231. * finish off header. If write, marshal discrim and nchunks.
  232. */
  233. if (cur_rchunk) {
  234. iptr = (__be32 *) cur_rchunk;
  235. *iptr++ = xdr_zero; /* finish the read chunk list */
  236. *iptr++ = xdr_zero; /* encode a NULL write chunk list */
  237. *iptr++ = xdr_zero; /* encode a NULL reply chunk */
  238. } else {
  239. warray->wc_discrim = xdr_one;
  240. warray->wc_nchunks = htonl(nchunks);
  241. iptr = (__be32 *) cur_wchunk;
  242. if (type == rpcrdma_writech) {
  243. *iptr++ = xdr_zero; /* finish the write chunk list */
  244. *iptr++ = xdr_zero; /* encode a NULL reply chunk */
  245. }
  246. }
  247. /*
  248. * Return header size.
  249. */
  250. return (unsigned char *)iptr - (unsigned char *)headerp;
  251. out:
  252. if (r_xprt->rx_ia.ri_memreg_strategy != RPCRDMA_FRMR) {
  253. for (pos = 0; nchunks--;)
  254. pos += rpcrdma_deregister_external(
  255. &req->rl_segments[pos], r_xprt);
  256. }
  257. return n;
  258. }
  259. /*
  260. * Marshal chunks. This routine returns the header length
  261. * consumed by marshaling.
  262. *
  263. * Returns positive RPC/RDMA header size, or negative errno.
  264. */
  265. ssize_t
  266. rpcrdma_marshal_chunks(struct rpc_rqst *rqst, ssize_t result)
  267. {
  268. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  269. struct rpcrdma_msg *headerp = (struct rpcrdma_msg *)req->rl_base;
  270. if (req->rl_rtype != rpcrdma_noch)
  271. result = rpcrdma_create_chunks(rqst, &rqst->rq_snd_buf,
  272. headerp, req->rl_rtype);
  273. else if (req->rl_wtype != rpcrdma_noch)
  274. result = rpcrdma_create_chunks(rqst, &rqst->rq_rcv_buf,
  275. headerp, req->rl_wtype);
  276. return result;
  277. }
  278. /*
  279. * Copy write data inline.
  280. * This function is used for "small" requests. Data which is passed
  281. * to RPC via iovecs (or page list) is copied directly into the
  282. * pre-registered memory buffer for this request. For small amounts
  283. * of data, this is efficient. The cutoff value is tunable.
  284. */
  285. static int
  286. rpcrdma_inline_pullup(struct rpc_rqst *rqst, int pad)
  287. {
  288. int i, npages, curlen;
  289. int copy_len;
  290. unsigned char *srcp, *destp;
  291. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  292. int page_base;
  293. struct page **ppages;
  294. destp = rqst->rq_svec[0].iov_base;
  295. curlen = rqst->rq_svec[0].iov_len;
  296. destp += curlen;
  297. /*
  298. * Do optional padding where it makes sense. Alignment of write
  299. * payload can help the server, if our setting is accurate.
  300. */
  301. pad -= (curlen + 36/*sizeof(struct rpcrdma_msg_padded)*/);
  302. if (pad < 0 || rqst->rq_slen - curlen < RPCRDMA_INLINE_PAD_THRESH)
  303. pad = 0; /* don't pad this request */
  304. dprintk("RPC: %s: pad %d destp 0x%p len %d hdrlen %d\n",
  305. __func__, pad, destp, rqst->rq_slen, curlen);
  306. copy_len = rqst->rq_snd_buf.page_len;
  307. if (rqst->rq_snd_buf.tail[0].iov_len) {
  308. curlen = rqst->rq_snd_buf.tail[0].iov_len;
  309. if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) {
  310. memmove(destp + copy_len,
  311. rqst->rq_snd_buf.tail[0].iov_base, curlen);
  312. r_xprt->rx_stats.pullup_copy_count += curlen;
  313. }
  314. dprintk("RPC: %s: tail destp 0x%p len %d\n",
  315. __func__, destp + copy_len, curlen);
  316. rqst->rq_svec[0].iov_len += curlen;
  317. }
  318. r_xprt->rx_stats.pullup_copy_count += copy_len;
  319. page_base = rqst->rq_snd_buf.page_base;
  320. ppages = rqst->rq_snd_buf.pages + (page_base >> PAGE_SHIFT);
  321. page_base &= ~PAGE_MASK;
  322. npages = PAGE_ALIGN(page_base+copy_len) >> PAGE_SHIFT;
  323. for (i = 0; copy_len && i < npages; i++) {
  324. curlen = PAGE_SIZE - page_base;
  325. if (curlen > copy_len)
  326. curlen = copy_len;
  327. dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n",
  328. __func__, i, destp, copy_len, curlen);
  329. srcp = kmap_atomic(ppages[i]);
  330. memcpy(destp, srcp+page_base, curlen);
  331. kunmap_atomic(srcp);
  332. rqst->rq_svec[0].iov_len += curlen;
  333. destp += curlen;
  334. copy_len -= curlen;
  335. page_base = 0;
  336. }
  337. /* header now contains entire send message */
  338. return pad;
  339. }
  340. /*
  341. * Marshal a request: the primary job of this routine is to choose
  342. * the transfer modes. See comments below.
  343. *
  344. * Uses multiple RDMA IOVs for a request:
  345. * [0] -- RPC RDMA header, which uses memory from the *start* of the
  346. * preregistered buffer that already holds the RPC data in
  347. * its middle.
  348. * [1] -- the RPC header/data, marshaled by RPC and the NFS protocol.
  349. * [2] -- optional padding.
  350. * [3] -- if padded, header only in [1] and data here.
  351. *
  352. * Returns zero on success, otherwise a negative errno.
  353. */
  354. int
  355. rpcrdma_marshal_req(struct rpc_rqst *rqst)
  356. {
  357. struct rpc_xprt *xprt = rqst->rq_xprt;
  358. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  359. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  360. char *base;
  361. size_t rpclen, padlen;
  362. ssize_t hdrlen;
  363. struct rpcrdma_msg *headerp;
  364. /*
  365. * rpclen gets amount of data in first buffer, which is the
  366. * pre-registered buffer.
  367. */
  368. base = rqst->rq_svec[0].iov_base;
  369. rpclen = rqst->rq_svec[0].iov_len;
  370. /* build RDMA header in private area at front */
  371. headerp = (struct rpcrdma_msg *) req->rl_base;
  372. /* don't htonl XID, it's already done in request */
  373. headerp->rm_xid = rqst->rq_xid;
  374. headerp->rm_vers = xdr_one;
  375. headerp->rm_credit = htonl(r_xprt->rx_buf.rb_max_requests);
  376. headerp->rm_type = htonl(RDMA_MSG);
  377. /*
  378. * Chunks needed for results?
  379. *
  380. * o If the expected result is under the inline threshold, all ops
  381. * return as inline (but see later).
  382. * o Large non-read ops return as a single reply chunk.
  383. * o Large read ops return data as write chunk(s), header as inline.
  384. *
  385. * Note: the NFS code sending down multiple result segments implies
  386. * the op is one of read, readdir[plus], readlink or NFSv4 getacl.
  387. */
  388. /*
  389. * This code can handle read chunks, write chunks OR reply
  390. * chunks -- only one type. If the request is too big to fit
  391. * inline, then we will choose read chunks. If the request is
  392. * a READ, then use write chunks to separate the file data
  393. * into pages; otherwise use reply chunks.
  394. */
  395. if (rqst->rq_rcv_buf.buflen <= RPCRDMA_INLINE_READ_THRESHOLD(rqst))
  396. req->rl_wtype = rpcrdma_noch;
  397. else if (rqst->rq_rcv_buf.page_len == 0)
  398. req->rl_wtype = rpcrdma_replych;
  399. else if (rqst->rq_rcv_buf.flags & XDRBUF_READ)
  400. req->rl_wtype = rpcrdma_writech;
  401. else
  402. req->rl_wtype = rpcrdma_replych;
  403. /*
  404. * Chunks needed for arguments?
  405. *
  406. * o If the total request is under the inline threshold, all ops
  407. * are sent as inline.
  408. * o Large non-write ops are sent with the entire message as a
  409. * single read chunk (protocol 0-position special case).
  410. * o Large write ops transmit data as read chunk(s), header as
  411. * inline.
  412. *
  413. * Note: the NFS code sending down multiple argument segments
  414. * implies the op is a write.
  415. * TBD check NFSv4 setacl
  416. */
  417. if (rqst->rq_snd_buf.len <= RPCRDMA_INLINE_WRITE_THRESHOLD(rqst))
  418. req->rl_rtype = rpcrdma_noch;
  419. else if (rqst->rq_snd_buf.page_len == 0)
  420. req->rl_rtype = rpcrdma_areadch;
  421. else
  422. req->rl_rtype = rpcrdma_readch;
  423. /* The following simplification is not true forever */
  424. if (req->rl_rtype != rpcrdma_noch && req->rl_wtype == rpcrdma_replych)
  425. req->rl_wtype = rpcrdma_noch;
  426. if (req->rl_rtype != rpcrdma_noch && req->rl_wtype != rpcrdma_noch) {
  427. dprintk("RPC: %s: cannot marshal multiple chunk lists\n",
  428. __func__);
  429. return -EIO;
  430. }
  431. hdrlen = 28; /*sizeof *headerp;*/
  432. padlen = 0;
  433. /*
  434. * Pull up any extra send data into the preregistered buffer.
  435. * When padding is in use and applies to the transfer, insert
  436. * it and change the message type.
  437. */
  438. if (req->rl_rtype == rpcrdma_noch) {
  439. padlen = rpcrdma_inline_pullup(rqst,
  440. RPCRDMA_INLINE_PAD_VALUE(rqst));
  441. if (padlen) {
  442. headerp->rm_type = htonl(RDMA_MSGP);
  443. headerp->rm_body.rm_padded.rm_align =
  444. htonl(RPCRDMA_INLINE_PAD_VALUE(rqst));
  445. headerp->rm_body.rm_padded.rm_thresh =
  446. htonl(RPCRDMA_INLINE_PAD_THRESH);
  447. headerp->rm_body.rm_padded.rm_pempty[0] = xdr_zero;
  448. headerp->rm_body.rm_padded.rm_pempty[1] = xdr_zero;
  449. headerp->rm_body.rm_padded.rm_pempty[2] = xdr_zero;
  450. hdrlen += 2 * sizeof(u32); /* extra words in padhdr */
  451. if (req->rl_wtype != rpcrdma_noch) {
  452. dprintk("RPC: %s: invalid chunk list\n",
  453. __func__);
  454. return -EIO;
  455. }
  456. } else {
  457. headerp->rm_body.rm_nochunks.rm_empty[0] = xdr_zero;
  458. headerp->rm_body.rm_nochunks.rm_empty[1] = xdr_zero;
  459. headerp->rm_body.rm_nochunks.rm_empty[2] = xdr_zero;
  460. /* new length after pullup */
  461. rpclen = rqst->rq_svec[0].iov_len;
  462. /*
  463. * Currently we try to not actually use read inline.
  464. * Reply chunks have the desirable property that
  465. * they land, packed, directly in the target buffers
  466. * without headers, so they require no fixup. The
  467. * additional RDMA Write op sends the same amount
  468. * of data, streams on-the-wire and adds no overhead
  469. * on receive. Therefore, we request a reply chunk
  470. * for non-writes wherever feasible and efficient.
  471. */
  472. if (req->rl_wtype == rpcrdma_noch)
  473. req->rl_wtype = rpcrdma_replych;
  474. }
  475. }
  476. hdrlen = rpcrdma_marshal_chunks(rqst, hdrlen);
  477. if (hdrlen < 0)
  478. return hdrlen;
  479. dprintk("RPC: %s: %s: hdrlen %zd rpclen %zd padlen %zd"
  480. " headerp 0x%p base 0x%p lkey 0x%x\n",
  481. __func__, transfertypes[req->rl_wtype], hdrlen, rpclen, padlen,
  482. headerp, base, req->rl_iov.lkey);
  483. /*
  484. * initialize send_iov's - normally only two: rdma chunk header and
  485. * single preregistered RPC header buffer, but if padding is present,
  486. * then use a preregistered (and zeroed) pad buffer between the RPC
  487. * header and any write data. In all non-rdma cases, any following
  488. * data has been copied into the RPC header buffer.
  489. */
  490. req->rl_send_iov[0].addr = req->rl_iov.addr;
  491. req->rl_send_iov[0].length = hdrlen;
  492. req->rl_send_iov[0].lkey = req->rl_iov.lkey;
  493. req->rl_send_iov[1].addr = req->rl_iov.addr + (base - req->rl_base);
  494. req->rl_send_iov[1].length = rpclen;
  495. req->rl_send_iov[1].lkey = req->rl_iov.lkey;
  496. req->rl_niovs = 2;
  497. if (padlen) {
  498. struct rpcrdma_ep *ep = &r_xprt->rx_ep;
  499. req->rl_send_iov[2].addr = ep->rep_pad.addr;
  500. req->rl_send_iov[2].length = padlen;
  501. req->rl_send_iov[2].lkey = ep->rep_pad.lkey;
  502. req->rl_send_iov[3].addr = req->rl_send_iov[1].addr + rpclen;
  503. req->rl_send_iov[3].length = rqst->rq_slen - rpclen;
  504. req->rl_send_iov[3].lkey = req->rl_iov.lkey;
  505. req->rl_niovs = 4;
  506. }
  507. return 0;
  508. }
  509. /*
  510. * Chase down a received write or reply chunklist to get length
  511. * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
  512. */
  513. static int
  514. rpcrdma_count_chunks(struct rpcrdma_rep *rep, unsigned int max, int wrchunk, __be32 **iptrp)
  515. {
  516. unsigned int i, total_len;
  517. struct rpcrdma_write_chunk *cur_wchunk;
  518. i = ntohl(**iptrp); /* get array count */
  519. if (i > max)
  520. return -1;
  521. cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
  522. total_len = 0;
  523. while (i--) {
  524. struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
  525. ifdebug(FACILITY) {
  526. u64 off;
  527. xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
  528. dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
  529. __func__,
  530. ntohl(seg->rs_length),
  531. (unsigned long long)off,
  532. ntohl(seg->rs_handle));
  533. }
  534. total_len += ntohl(seg->rs_length);
  535. ++cur_wchunk;
  536. }
  537. /* check and adjust for properly terminated write chunk */
  538. if (wrchunk) {
  539. __be32 *w = (__be32 *) cur_wchunk;
  540. if (*w++ != xdr_zero)
  541. return -1;
  542. cur_wchunk = (struct rpcrdma_write_chunk *) w;
  543. }
  544. if ((char *) cur_wchunk > rep->rr_base + rep->rr_len)
  545. return -1;
  546. *iptrp = (__be32 *) cur_wchunk;
  547. return total_len;
  548. }
  549. /*
  550. * Scatter inline received data back into provided iov's.
  551. */
  552. static void
  553. rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
  554. {
  555. int i, npages, curlen, olen;
  556. char *destp;
  557. struct page **ppages;
  558. int page_base;
  559. curlen = rqst->rq_rcv_buf.head[0].iov_len;
  560. if (curlen > copy_len) { /* write chunk header fixup */
  561. curlen = copy_len;
  562. rqst->rq_rcv_buf.head[0].iov_len = curlen;
  563. }
  564. dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
  565. __func__, srcp, copy_len, curlen);
  566. /* Shift pointer for first receive segment only */
  567. rqst->rq_rcv_buf.head[0].iov_base = srcp;
  568. srcp += curlen;
  569. copy_len -= curlen;
  570. olen = copy_len;
  571. i = 0;
  572. rpcx_to_rdmax(rqst->rq_xprt)->rx_stats.fixup_copy_count += olen;
  573. page_base = rqst->rq_rcv_buf.page_base;
  574. ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT);
  575. page_base &= ~PAGE_MASK;
  576. if (copy_len && rqst->rq_rcv_buf.page_len) {
  577. npages = PAGE_ALIGN(page_base +
  578. rqst->rq_rcv_buf.page_len) >> PAGE_SHIFT;
  579. for (; i < npages; i++) {
  580. curlen = PAGE_SIZE - page_base;
  581. if (curlen > copy_len)
  582. curlen = copy_len;
  583. dprintk("RPC: %s: page %d"
  584. " srcp 0x%p len %d curlen %d\n",
  585. __func__, i, srcp, copy_len, curlen);
  586. destp = kmap_atomic(ppages[i]);
  587. memcpy(destp + page_base, srcp, curlen);
  588. flush_dcache_page(ppages[i]);
  589. kunmap_atomic(destp);
  590. srcp += curlen;
  591. copy_len -= curlen;
  592. if (copy_len == 0)
  593. break;
  594. page_base = 0;
  595. }
  596. }
  597. if (copy_len && rqst->rq_rcv_buf.tail[0].iov_len) {
  598. curlen = copy_len;
  599. if (curlen > rqst->rq_rcv_buf.tail[0].iov_len)
  600. curlen = rqst->rq_rcv_buf.tail[0].iov_len;
  601. if (rqst->rq_rcv_buf.tail[0].iov_base != srcp)
  602. memmove(rqst->rq_rcv_buf.tail[0].iov_base, srcp, curlen);
  603. dprintk("RPC: %s: tail srcp 0x%p len %d curlen %d\n",
  604. __func__, srcp, copy_len, curlen);
  605. rqst->rq_rcv_buf.tail[0].iov_len = curlen;
  606. copy_len -= curlen; ++i;
  607. } else
  608. rqst->rq_rcv_buf.tail[0].iov_len = 0;
  609. if (pad) {
  610. /* implicit padding on terminal chunk */
  611. unsigned char *p = rqst->rq_rcv_buf.tail[0].iov_base;
  612. while (pad--)
  613. p[rqst->rq_rcv_buf.tail[0].iov_len++] = 0;
  614. }
  615. if (copy_len)
  616. dprintk("RPC: %s: %d bytes in"
  617. " %d extra segments (%d lost)\n",
  618. __func__, olen, i, copy_len);
  619. /* TBD avoid a warning from call_decode() */
  620. rqst->rq_private_buf = rqst->rq_rcv_buf;
  621. }
  622. void
  623. rpcrdma_connect_worker(struct work_struct *work)
  624. {
  625. struct rpcrdma_ep *ep =
  626. container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
  627. struct rpc_xprt *xprt = ep->rep_xprt;
  628. spin_lock_bh(&xprt->transport_lock);
  629. if (++xprt->connect_cookie == 0) /* maintain a reserved value */
  630. ++xprt->connect_cookie;
  631. if (ep->rep_connected > 0) {
  632. if (!xprt_test_and_set_connected(xprt))
  633. xprt_wake_pending_tasks(xprt, 0);
  634. } else {
  635. if (xprt_test_and_clear_connected(xprt))
  636. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  637. }
  638. spin_unlock_bh(&xprt->transport_lock);
  639. }
  640. /*
  641. * This function is called when an async event is posted to
  642. * the connection which changes the connection state. All it
  643. * does at this point is mark the connection up/down, the rpc
  644. * timers do the rest.
  645. */
  646. void
  647. rpcrdma_conn_func(struct rpcrdma_ep *ep)
  648. {
  649. schedule_delayed_work(&ep->rep_connect_worker, 0);
  650. }
  651. /*
  652. * Called as a tasklet to do req/reply match and complete a request
  653. * Errors must result in the RPC task either being awakened, or
  654. * allowed to timeout, to discover the errors at that time.
  655. */
  656. void
  657. rpcrdma_reply_handler(struct rpcrdma_rep *rep)
  658. {
  659. struct rpcrdma_msg *headerp;
  660. struct rpcrdma_req *req;
  661. struct rpc_rqst *rqst;
  662. struct rpc_xprt *xprt = rep->rr_xprt;
  663. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  664. __be32 *iptr;
  665. int rdmalen, status;
  666. unsigned long cwnd;
  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 < 28) {
  677. dprintk("RPC: %s: short/invalid reply\n", __func__);
  678. goto repost;
  679. }
  680. headerp = (struct rpcrdma_msg *) rep->rr_base;
  681. if (headerp->rm_vers != xdr_one) {
  682. dprintk("RPC: %s: invalid version %d\n",
  683. __func__, ntohl(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, headerp->rm_xid, rep->rr_len);
  694. repost:
  695. r_xprt->rx_stats.bad_reply_count++;
  696. rep->rr_func = rpcrdma_reply_handler;
  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. 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, headerp->rm_xid);
  713. /* from here on, the reply is no longer an orphan */
  714. req->rl_reply = rep;
  715. xprt->reestablish_timeout = 0;
  716. /* check for expected message types */
  717. /* The order of some of these tests is important. */
  718. switch (headerp->rm_type) {
  719. case htonl(RDMA_MSG):
  720. /* never expect read chunks */
  721. /* never expect reply chunks (two ways to check) */
  722. /* never expect write chunks without having offered RDMA */
  723. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  724. (headerp->rm_body.rm_chunks[1] == xdr_zero &&
  725. headerp->rm_body.rm_chunks[2] != xdr_zero) ||
  726. (headerp->rm_body.rm_chunks[1] != xdr_zero &&
  727. req->rl_nchunks == 0))
  728. goto badheader;
  729. if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
  730. /* count any expected write chunks in read reply */
  731. /* start at write chunk array count */
  732. iptr = &headerp->rm_body.rm_chunks[2];
  733. rdmalen = rpcrdma_count_chunks(rep,
  734. req->rl_nchunks, 1, &iptr);
  735. /* check for validity, and no reply chunk after */
  736. if (rdmalen < 0 || *iptr++ != xdr_zero)
  737. goto badheader;
  738. rep->rr_len -=
  739. ((unsigned char *)iptr - (unsigned char *)headerp);
  740. status = rep->rr_len + rdmalen;
  741. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  742. /* special case - last chunk may omit padding */
  743. if (rdmalen &= 3) {
  744. rdmalen = 4 - rdmalen;
  745. status += rdmalen;
  746. }
  747. } else {
  748. /* else ordinary inline */
  749. rdmalen = 0;
  750. iptr = (__be32 *)((unsigned char *)headerp + 28);
  751. rep->rr_len -= 28; /*sizeof *headerp;*/
  752. status = rep->rr_len;
  753. }
  754. /* Fix up the rpc results for upper layer */
  755. rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len, rdmalen);
  756. break;
  757. case htonl(RDMA_NOMSG):
  758. /* never expect read or write chunks, always reply chunks */
  759. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  760. headerp->rm_body.rm_chunks[1] != xdr_zero ||
  761. headerp->rm_body.rm_chunks[2] != xdr_one ||
  762. req->rl_nchunks == 0)
  763. goto badheader;
  764. iptr = (__be32 *)((unsigned char *)headerp + 28);
  765. rdmalen = rpcrdma_count_chunks(rep, req->rl_nchunks, 0, &iptr);
  766. if (rdmalen < 0)
  767. goto badheader;
  768. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  769. /* Reply chunk buffer already is the reply vector - no fixup. */
  770. status = rdmalen;
  771. break;
  772. badheader:
  773. default:
  774. dprintk("%s: invalid rpcrdma reply header (type %d):"
  775. " chunks[012] == %d %d %d"
  776. " expected chunks <= %d\n",
  777. __func__, ntohl(headerp->rm_type),
  778. headerp->rm_body.rm_chunks[0],
  779. headerp->rm_body.rm_chunks[1],
  780. headerp->rm_body.rm_chunks[2],
  781. req->rl_nchunks);
  782. status = -EIO;
  783. r_xprt->rx_stats.bad_reply_count++;
  784. break;
  785. }
  786. cwnd = xprt->cwnd;
  787. xprt->cwnd = atomic_read(&r_xprt->rx_buf.rb_credits) << RPC_CWNDSHIFT;
  788. if (xprt->cwnd > cwnd)
  789. xprt_release_rqst_cong(rqst->rq_task);
  790. dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
  791. __func__, xprt, rqst, status);
  792. xprt_complete_rqst(rqst->rq_task, status);
  793. spin_unlock(&xprt->transport_lock);
  794. }