rpc_rdma.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. static const char transfertypes[][12] = {
  59. "inline", /* no chunks */
  60. "read list", /* some argument via rdma read */
  61. "*read list", /* entire request via rdma read */
  62. "write list", /* some result via rdma write */
  63. "reply chunk" /* entire reply via rdma write */
  64. };
  65. /* Returns size of largest RPC-over-RDMA header in a Call message
  66. *
  67. * The largest Call header contains a full-size Read list and a
  68. * minimal Reply chunk.
  69. */
  70. static unsigned int rpcrdma_max_call_header_size(unsigned int maxsegs)
  71. {
  72. unsigned int size;
  73. /* Fixed header fields and list discriminators */
  74. size = RPCRDMA_HDRLEN_MIN;
  75. /* Maximum Read list size */
  76. maxsegs += 2; /* segment for head and tail buffers */
  77. size = maxsegs * sizeof(struct rpcrdma_read_chunk);
  78. /* Minimal Read chunk size */
  79. size += sizeof(__be32); /* segment count */
  80. size += sizeof(struct rpcrdma_segment);
  81. size += sizeof(__be32); /* list discriminator */
  82. dprintk("RPC: %s: max call header size = %u\n",
  83. __func__, size);
  84. return size;
  85. }
  86. /* Returns size of largest RPC-over-RDMA header in a Reply message
  87. *
  88. * There is only one Write list or one Reply chunk per Reply
  89. * message. The larger list is the Write list.
  90. */
  91. static unsigned int rpcrdma_max_reply_header_size(unsigned int maxsegs)
  92. {
  93. unsigned int size;
  94. /* Fixed header fields and list discriminators */
  95. size = RPCRDMA_HDRLEN_MIN;
  96. /* Maximum Write list size */
  97. maxsegs += 2; /* segment for head and tail buffers */
  98. size = sizeof(__be32); /* segment count */
  99. size += maxsegs * sizeof(struct rpcrdma_segment);
  100. size += sizeof(__be32); /* list discriminator */
  101. dprintk("RPC: %s: max reply header size = %u\n",
  102. __func__, size);
  103. return size;
  104. }
  105. void rpcrdma_set_max_header_sizes(struct rpcrdma_ia *ia,
  106. struct rpcrdma_create_data_internal *cdata,
  107. unsigned int maxsegs)
  108. {
  109. ia->ri_max_inline_write = cdata->inline_wsize -
  110. rpcrdma_max_call_header_size(maxsegs);
  111. ia->ri_max_inline_read = cdata->inline_rsize -
  112. rpcrdma_max_reply_header_size(maxsegs);
  113. }
  114. /* The client can send a request inline as long as the RPCRDMA header
  115. * plus the RPC call fit under the transport's inline limit. If the
  116. * combined call message size exceeds that limit, the client must use
  117. * the read chunk list for this operation.
  118. */
  119. static bool rpcrdma_args_inline(struct rpcrdma_xprt *r_xprt,
  120. struct rpc_rqst *rqst)
  121. {
  122. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  123. return rqst->rq_snd_buf.len <= ia->ri_max_inline_write;
  124. }
  125. /* The client can't know how large the actual reply will be. Thus it
  126. * plans for the largest possible reply for that particular ULP
  127. * operation. If the maximum combined reply message size exceeds that
  128. * limit, the client must provide a write list or a reply chunk for
  129. * this request.
  130. */
  131. static bool rpcrdma_results_inline(struct rpcrdma_xprt *r_xprt,
  132. struct rpc_rqst *rqst)
  133. {
  134. struct rpcrdma_ia *ia = &r_xprt->rx_ia;
  135. return rqst->rq_rcv_buf.buflen <= ia->ri_max_inline_read;
  136. }
  137. static int
  138. rpcrdma_tail_pullup(struct xdr_buf *buf)
  139. {
  140. size_t tlen = buf->tail[0].iov_len;
  141. size_t skip = tlen & 3;
  142. /* Do not include the tail if it is only an XDR pad */
  143. if (tlen < 4)
  144. return 0;
  145. /* xdr_write_pages() adds a pad at the beginning of the tail
  146. * if the content in "buf->pages" is unaligned. Force the
  147. * tail's actual content to land at the next XDR position
  148. * after the head instead.
  149. */
  150. if (skip) {
  151. unsigned char *src, *dst;
  152. unsigned int count;
  153. src = buf->tail[0].iov_base;
  154. dst = buf->head[0].iov_base;
  155. dst += buf->head[0].iov_len;
  156. src += skip;
  157. tlen -= skip;
  158. dprintk("RPC: %s: skip=%zu, memmove(%p, %p, %zu)\n",
  159. __func__, skip, dst, src, tlen);
  160. for (count = tlen; count; count--)
  161. *dst++ = *src++;
  162. }
  163. return tlen;
  164. }
  165. /* Split "vec" on page boundaries into segments. FMR registers pages,
  166. * not a byte range. Other modes coalesce these segments into a single
  167. * MR when they can.
  168. */
  169. static int
  170. rpcrdma_convert_kvec(struct kvec *vec, struct rpcrdma_mr_seg *seg, int n)
  171. {
  172. size_t page_offset;
  173. u32 remaining;
  174. char *base;
  175. base = vec->iov_base;
  176. page_offset = offset_in_page(base);
  177. remaining = vec->iov_len;
  178. while (remaining && n < RPCRDMA_MAX_SEGS) {
  179. seg[n].mr_page = NULL;
  180. seg[n].mr_offset = base;
  181. seg[n].mr_len = min_t(u32, PAGE_SIZE - page_offset, remaining);
  182. remaining -= seg[n].mr_len;
  183. base += seg[n].mr_len;
  184. ++n;
  185. page_offset = 0;
  186. }
  187. return n;
  188. }
  189. /*
  190. * Chunk assembly from upper layer xdr_buf.
  191. *
  192. * Prepare the passed-in xdr_buf into representation as RPC/RDMA chunk
  193. * elements. Segments are then coalesced when registered, if possible
  194. * within the selected memreg mode.
  195. *
  196. * Returns positive number of segments converted, or a negative errno.
  197. */
  198. static int
  199. rpcrdma_convert_iovs(struct xdr_buf *xdrbuf, unsigned int pos,
  200. enum rpcrdma_chunktype type, struct rpcrdma_mr_seg *seg)
  201. {
  202. int len, n, p, page_base;
  203. struct page **ppages;
  204. n = 0;
  205. if (pos == 0) {
  206. n = rpcrdma_convert_kvec(&xdrbuf->head[0], seg, n);
  207. if (n == RPCRDMA_MAX_SEGS)
  208. goto out_overflow;
  209. }
  210. len = xdrbuf->page_len;
  211. ppages = xdrbuf->pages + (xdrbuf->page_base >> PAGE_SHIFT);
  212. page_base = xdrbuf->page_base & ~PAGE_MASK;
  213. p = 0;
  214. while (len && n < RPCRDMA_MAX_SEGS) {
  215. if (!ppages[p]) {
  216. /* alloc the pagelist for receiving buffer */
  217. ppages[p] = alloc_page(GFP_ATOMIC);
  218. if (!ppages[p])
  219. return -EAGAIN;
  220. }
  221. seg[n].mr_page = ppages[p];
  222. seg[n].mr_offset = (void *)(unsigned long) page_base;
  223. seg[n].mr_len = min_t(u32, PAGE_SIZE - page_base, len);
  224. if (seg[n].mr_len > PAGE_SIZE)
  225. goto out_overflow;
  226. len -= seg[n].mr_len;
  227. ++n;
  228. ++p;
  229. page_base = 0; /* page offset only applies to first page */
  230. }
  231. /* Message overflows the seg array */
  232. if (len && n == RPCRDMA_MAX_SEGS)
  233. goto out_overflow;
  234. /* When encoding the read list, the tail is always sent inline */
  235. if (type == rpcrdma_readch)
  236. return n;
  237. if (xdrbuf->tail[0].iov_len) {
  238. /* the rpcrdma protocol allows us to omit any trailing
  239. * xdr pad bytes, saving the server an RDMA operation. */
  240. if (xdrbuf->tail[0].iov_len < 4 && xprt_rdma_pad_optimize)
  241. return n;
  242. n = rpcrdma_convert_kvec(&xdrbuf->tail[0], seg, n);
  243. if (n == RPCRDMA_MAX_SEGS)
  244. goto out_overflow;
  245. }
  246. return n;
  247. out_overflow:
  248. pr_err("rpcrdma: segment array overflow\n");
  249. return -EIO;
  250. }
  251. static inline __be32 *
  252. xdr_encode_rdma_segment(__be32 *iptr, struct rpcrdma_mw *mw)
  253. {
  254. *iptr++ = cpu_to_be32(mw->mw_handle);
  255. *iptr++ = cpu_to_be32(mw->mw_length);
  256. return xdr_encode_hyper(iptr, mw->mw_offset);
  257. }
  258. /* XDR-encode the Read list. Supports encoding a list of read
  259. * segments that belong to a single read chunk.
  260. *
  261. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  262. *
  263. * Read chunklist (a linked list):
  264. * N elements, position P (same P for all chunks of same arg!):
  265. * 1 - PHLOO - 1 - PHLOO - ... - 1 - PHLOO - 0
  266. *
  267. * Returns a pointer to the XDR word in the RDMA header following
  268. * the end of the Read list, or an error pointer.
  269. */
  270. static __be32 *
  271. rpcrdma_encode_read_list(struct rpcrdma_xprt *r_xprt,
  272. struct rpcrdma_req *req, struct rpc_rqst *rqst,
  273. __be32 *iptr, enum rpcrdma_chunktype rtype)
  274. {
  275. struct rpcrdma_mr_seg *seg;
  276. struct rpcrdma_mw *mw;
  277. unsigned int pos;
  278. int n, nsegs;
  279. if (rtype == rpcrdma_noch) {
  280. *iptr++ = xdr_zero; /* item not present */
  281. return iptr;
  282. }
  283. pos = rqst->rq_snd_buf.head[0].iov_len;
  284. if (rtype == rpcrdma_areadch)
  285. pos = 0;
  286. seg = req->rl_segments;
  287. nsegs = rpcrdma_convert_iovs(&rqst->rq_snd_buf, pos, rtype, seg);
  288. if (nsegs < 0)
  289. return ERR_PTR(nsegs);
  290. do {
  291. n = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
  292. false, &mw);
  293. if (n < 0)
  294. return ERR_PTR(n);
  295. list_add(&mw->mw_list, &req->rl_registered);
  296. *iptr++ = xdr_one; /* item present */
  297. /* All read segments in this chunk
  298. * have the same "position".
  299. */
  300. *iptr++ = cpu_to_be32(pos);
  301. iptr = xdr_encode_rdma_segment(iptr, mw);
  302. dprintk("RPC: %5u %s: pos %u %u@0x%016llx:0x%08x (%s)\n",
  303. rqst->rq_task->tk_pid, __func__, pos,
  304. mw->mw_length, (unsigned long long)mw->mw_offset,
  305. mw->mw_handle, n < nsegs ? "more" : "last");
  306. r_xprt->rx_stats.read_chunk_count++;
  307. seg += n;
  308. nsegs -= n;
  309. } while (nsegs);
  310. /* Finish Read list */
  311. *iptr++ = xdr_zero; /* Next item not present */
  312. return iptr;
  313. }
  314. /* XDR-encode the Write list. Supports encoding a list containing
  315. * one array of plain segments that belong to a single write chunk.
  316. *
  317. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  318. *
  319. * Write chunklist (a list of (one) counted array):
  320. * N elements:
  321. * 1 - N - HLOO - HLOO - ... - HLOO - 0
  322. *
  323. * Returns a pointer to the XDR word in the RDMA header following
  324. * the end of the Write list, or an error pointer.
  325. */
  326. static __be32 *
  327. rpcrdma_encode_write_list(struct rpcrdma_xprt *r_xprt, struct rpcrdma_req *req,
  328. struct rpc_rqst *rqst, __be32 *iptr,
  329. enum rpcrdma_chunktype wtype)
  330. {
  331. struct rpcrdma_mr_seg *seg;
  332. struct rpcrdma_mw *mw;
  333. int n, nsegs, nchunks;
  334. __be32 *segcount;
  335. if (wtype != rpcrdma_writech) {
  336. *iptr++ = xdr_zero; /* no Write list present */
  337. return iptr;
  338. }
  339. seg = req->rl_segments;
  340. nsegs = rpcrdma_convert_iovs(&rqst->rq_rcv_buf,
  341. rqst->rq_rcv_buf.head[0].iov_len,
  342. wtype, seg);
  343. if (nsegs < 0)
  344. return ERR_PTR(nsegs);
  345. *iptr++ = xdr_one; /* Write list present */
  346. segcount = iptr++; /* save location of segment count */
  347. nchunks = 0;
  348. do {
  349. n = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
  350. true, &mw);
  351. if (n < 0)
  352. return ERR_PTR(n);
  353. list_add(&mw->mw_list, &req->rl_registered);
  354. iptr = xdr_encode_rdma_segment(iptr, mw);
  355. dprintk("RPC: %5u %s: %u@0x016%llx:0x%08x (%s)\n",
  356. rqst->rq_task->tk_pid, __func__,
  357. mw->mw_length, (unsigned long long)mw->mw_offset,
  358. mw->mw_handle, n < nsegs ? "more" : "last");
  359. r_xprt->rx_stats.write_chunk_count++;
  360. r_xprt->rx_stats.total_rdma_request += seg->mr_len;
  361. nchunks++;
  362. seg += n;
  363. nsegs -= n;
  364. } while (nsegs);
  365. /* Update count of segments in this Write chunk */
  366. *segcount = cpu_to_be32(nchunks);
  367. /* Finish Write list */
  368. *iptr++ = xdr_zero; /* Next item not present */
  369. return iptr;
  370. }
  371. /* XDR-encode the Reply chunk. Supports encoding an array of plain
  372. * segments that belong to a single write (reply) chunk.
  373. *
  374. * Encoding key for single-list chunks (HLOO = Handle32 Length32 Offset64):
  375. *
  376. * Reply chunk (a counted array):
  377. * N elements:
  378. * 1 - N - HLOO - HLOO - ... - HLOO
  379. *
  380. * Returns a pointer to the XDR word in the RDMA header following
  381. * the end of the Reply chunk, or an error pointer.
  382. */
  383. static __be32 *
  384. rpcrdma_encode_reply_chunk(struct rpcrdma_xprt *r_xprt,
  385. struct rpcrdma_req *req, struct rpc_rqst *rqst,
  386. __be32 *iptr, enum rpcrdma_chunktype wtype)
  387. {
  388. struct rpcrdma_mr_seg *seg;
  389. struct rpcrdma_mw *mw;
  390. int n, nsegs, nchunks;
  391. __be32 *segcount;
  392. if (wtype != rpcrdma_replych) {
  393. *iptr++ = xdr_zero; /* no Reply chunk present */
  394. return iptr;
  395. }
  396. seg = req->rl_segments;
  397. nsegs = rpcrdma_convert_iovs(&rqst->rq_rcv_buf, 0, wtype, seg);
  398. if (nsegs < 0)
  399. return ERR_PTR(nsegs);
  400. *iptr++ = xdr_one; /* Reply chunk present */
  401. segcount = iptr++; /* save location of segment count */
  402. nchunks = 0;
  403. do {
  404. n = r_xprt->rx_ia.ri_ops->ro_map(r_xprt, seg, nsegs,
  405. true, &mw);
  406. if (n < 0)
  407. return ERR_PTR(n);
  408. list_add(&mw->mw_list, &req->rl_registered);
  409. iptr = xdr_encode_rdma_segment(iptr, mw);
  410. dprintk("RPC: %5u %s: %u@0x%016llx:0x%08x (%s)\n",
  411. rqst->rq_task->tk_pid, __func__,
  412. mw->mw_length, (unsigned long long)mw->mw_offset,
  413. mw->mw_handle, n < nsegs ? "more" : "last");
  414. r_xprt->rx_stats.reply_chunk_count++;
  415. r_xprt->rx_stats.total_rdma_request += seg->mr_len;
  416. nchunks++;
  417. seg += n;
  418. nsegs -= n;
  419. } while (nsegs);
  420. /* Update count of segments in the Reply chunk */
  421. *segcount = cpu_to_be32(nchunks);
  422. return iptr;
  423. }
  424. /*
  425. * Copy write data inline.
  426. * This function is used for "small" requests. Data which is passed
  427. * to RPC via iovecs (or page list) is copied directly into the
  428. * pre-registered memory buffer for this request. For small amounts
  429. * of data, this is efficient. The cutoff value is tunable.
  430. */
  431. static void rpcrdma_inline_pullup(struct rpc_rqst *rqst)
  432. {
  433. int i, npages, curlen;
  434. int copy_len;
  435. unsigned char *srcp, *destp;
  436. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(rqst->rq_xprt);
  437. int page_base;
  438. struct page **ppages;
  439. destp = rqst->rq_svec[0].iov_base;
  440. curlen = rqst->rq_svec[0].iov_len;
  441. destp += curlen;
  442. dprintk("RPC: %s: destp 0x%p len %d hdrlen %d\n",
  443. __func__, destp, rqst->rq_slen, curlen);
  444. copy_len = rqst->rq_snd_buf.page_len;
  445. if (rqst->rq_snd_buf.tail[0].iov_len) {
  446. curlen = rqst->rq_snd_buf.tail[0].iov_len;
  447. if (destp + copy_len != rqst->rq_snd_buf.tail[0].iov_base) {
  448. memmove(destp + copy_len,
  449. rqst->rq_snd_buf.tail[0].iov_base, curlen);
  450. r_xprt->rx_stats.pullup_copy_count += curlen;
  451. }
  452. dprintk("RPC: %s: tail destp 0x%p len %d\n",
  453. __func__, destp + copy_len, curlen);
  454. rqst->rq_svec[0].iov_len += curlen;
  455. }
  456. r_xprt->rx_stats.pullup_copy_count += copy_len;
  457. page_base = rqst->rq_snd_buf.page_base;
  458. ppages = rqst->rq_snd_buf.pages + (page_base >> PAGE_SHIFT);
  459. page_base &= ~PAGE_MASK;
  460. npages = PAGE_ALIGN(page_base+copy_len) >> PAGE_SHIFT;
  461. for (i = 0; copy_len && i < npages; i++) {
  462. curlen = PAGE_SIZE - page_base;
  463. if (curlen > copy_len)
  464. curlen = copy_len;
  465. dprintk("RPC: %s: page %d destp 0x%p len %d curlen %d\n",
  466. __func__, i, destp, copy_len, curlen);
  467. srcp = kmap_atomic(ppages[i]);
  468. memcpy(destp, srcp+page_base, curlen);
  469. kunmap_atomic(srcp);
  470. rqst->rq_svec[0].iov_len += curlen;
  471. destp += curlen;
  472. copy_len -= curlen;
  473. page_base = 0;
  474. }
  475. /* header now contains entire send message */
  476. }
  477. /*
  478. * Marshal a request: the primary job of this routine is to choose
  479. * the transfer modes. See comments below.
  480. *
  481. * Prepares up to two IOVs per Call message:
  482. *
  483. * [0] -- RPC RDMA header
  484. * [1] -- the RPC header/data
  485. *
  486. * Returns zero on success, otherwise a negative errno.
  487. */
  488. int
  489. rpcrdma_marshal_req(struct rpc_rqst *rqst)
  490. {
  491. struct rpc_xprt *xprt = rqst->rq_xprt;
  492. struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt);
  493. struct rpcrdma_req *req = rpcr_to_rdmar(rqst);
  494. enum rpcrdma_chunktype rtype, wtype;
  495. struct rpcrdma_msg *headerp;
  496. bool ddp_allowed;
  497. ssize_t hdrlen;
  498. size_t rpclen;
  499. __be32 *iptr;
  500. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  501. if (test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state))
  502. return rpcrdma_bc_marshal_reply(rqst);
  503. #endif
  504. headerp = rdmab_to_msg(req->rl_rdmabuf);
  505. /* don't byte-swap XID, it's already done in request */
  506. headerp->rm_xid = rqst->rq_xid;
  507. headerp->rm_vers = rpcrdma_version;
  508. headerp->rm_credit = cpu_to_be32(r_xprt->rx_buf.rb_max_requests);
  509. headerp->rm_type = rdma_msg;
  510. /* When the ULP employs a GSS flavor that guarantees integrity
  511. * or privacy, direct data placement of individual data items
  512. * is not allowed.
  513. */
  514. ddp_allowed = !(rqst->rq_cred->cr_auth->au_flags &
  515. RPCAUTH_AUTH_DATATOUCH);
  516. /*
  517. * Chunks needed for results?
  518. *
  519. * o If the expected result is under the inline threshold, all ops
  520. * return as inline.
  521. * o Large read ops return data as write chunk(s), header as
  522. * inline.
  523. * o Large non-read ops return as a single reply chunk.
  524. */
  525. if (rpcrdma_results_inline(r_xprt, rqst))
  526. wtype = rpcrdma_noch;
  527. else if (ddp_allowed && rqst->rq_rcv_buf.flags & XDRBUF_READ)
  528. wtype = rpcrdma_writech;
  529. else
  530. wtype = rpcrdma_replych;
  531. /*
  532. * Chunks needed for arguments?
  533. *
  534. * o If the total request is under the inline threshold, all ops
  535. * are sent as inline.
  536. * o Large write ops transmit data as read chunk(s), header as
  537. * inline.
  538. * o Large non-write ops are sent with the entire message as a
  539. * single read chunk (protocol 0-position special case).
  540. *
  541. * This assumes that the upper layer does not present a request
  542. * that both has a data payload, and whose non-data arguments
  543. * by themselves are larger than the inline threshold.
  544. */
  545. if (rpcrdma_args_inline(r_xprt, rqst)) {
  546. rtype = rpcrdma_noch;
  547. rpcrdma_inline_pullup(rqst);
  548. rpclen = rqst->rq_svec[0].iov_len;
  549. } else if (ddp_allowed && rqst->rq_snd_buf.flags & XDRBUF_WRITE) {
  550. rtype = rpcrdma_readch;
  551. rpclen = rqst->rq_svec[0].iov_len;
  552. rpclen += rpcrdma_tail_pullup(&rqst->rq_snd_buf);
  553. } else {
  554. r_xprt->rx_stats.nomsg_call_count++;
  555. headerp->rm_type = htonl(RDMA_NOMSG);
  556. rtype = rpcrdma_areadch;
  557. rpclen = 0;
  558. }
  559. /* This implementation supports the following combinations
  560. * of chunk lists in one RPC-over-RDMA Call message:
  561. *
  562. * - Read list
  563. * - Write list
  564. * - Reply chunk
  565. * - Read list + Reply chunk
  566. *
  567. * It might not yet support the following combinations:
  568. *
  569. * - Read list + Write list
  570. *
  571. * It does not support the following combinations:
  572. *
  573. * - Write list + Reply chunk
  574. * - Read list + Write list + Reply chunk
  575. *
  576. * This implementation supports only a single chunk in each
  577. * Read or Write list. Thus for example the client cannot
  578. * send a Call message with a Position Zero Read chunk and a
  579. * regular Read chunk at the same time.
  580. */
  581. iptr = headerp->rm_body.rm_chunks;
  582. iptr = rpcrdma_encode_read_list(r_xprt, req, rqst, iptr, rtype);
  583. if (IS_ERR(iptr))
  584. goto out_unmap;
  585. iptr = rpcrdma_encode_write_list(r_xprt, req, rqst, iptr, wtype);
  586. if (IS_ERR(iptr))
  587. goto out_unmap;
  588. iptr = rpcrdma_encode_reply_chunk(r_xprt, req, rqst, iptr, wtype);
  589. if (IS_ERR(iptr))
  590. goto out_unmap;
  591. hdrlen = (unsigned char *)iptr - (unsigned char *)headerp;
  592. if (hdrlen + rpclen > RPCRDMA_INLINE_WRITE_THRESHOLD(rqst))
  593. goto out_overflow;
  594. dprintk("RPC: %5u %s: %s/%s: hdrlen %zd rpclen %zd\n",
  595. rqst->rq_task->tk_pid, __func__,
  596. transfertypes[rtype], transfertypes[wtype],
  597. hdrlen, rpclen);
  598. req->rl_send_iov[0].addr = rdmab_addr(req->rl_rdmabuf);
  599. req->rl_send_iov[0].length = hdrlen;
  600. req->rl_send_iov[0].lkey = rdmab_lkey(req->rl_rdmabuf);
  601. req->rl_niovs = 1;
  602. if (rtype == rpcrdma_areadch)
  603. return 0;
  604. req->rl_send_iov[1].addr = rdmab_addr(req->rl_sendbuf);
  605. req->rl_send_iov[1].length = rpclen;
  606. req->rl_send_iov[1].lkey = rdmab_lkey(req->rl_sendbuf);
  607. req->rl_niovs = 2;
  608. return 0;
  609. out_overflow:
  610. pr_err("rpcrdma: send overflow: hdrlen %zd rpclen %zu %s/%s\n",
  611. hdrlen, rpclen, transfertypes[rtype], transfertypes[wtype]);
  612. iptr = ERR_PTR(-EIO);
  613. out_unmap:
  614. r_xprt->rx_ia.ri_ops->ro_unmap_safe(r_xprt, req, false);
  615. return PTR_ERR(iptr);
  616. }
  617. /*
  618. * Chase down a received write or reply chunklist to get length
  619. * RDMA'd by server. See map at rpcrdma_create_chunks()! :-)
  620. */
  621. static int
  622. rpcrdma_count_chunks(struct rpcrdma_rep *rep, int wrchunk, __be32 **iptrp)
  623. {
  624. unsigned int i, total_len;
  625. struct rpcrdma_write_chunk *cur_wchunk;
  626. char *base = (char *)rdmab_to_msg(rep->rr_rdmabuf);
  627. i = be32_to_cpu(**iptrp);
  628. cur_wchunk = (struct rpcrdma_write_chunk *) (*iptrp + 1);
  629. total_len = 0;
  630. while (i--) {
  631. struct rpcrdma_segment *seg = &cur_wchunk->wc_target;
  632. ifdebug(FACILITY) {
  633. u64 off;
  634. xdr_decode_hyper((__be32 *)&seg->rs_offset, &off);
  635. dprintk("RPC: %s: chunk %d@0x%llx:0x%x\n",
  636. __func__,
  637. be32_to_cpu(seg->rs_length),
  638. (unsigned long long)off,
  639. be32_to_cpu(seg->rs_handle));
  640. }
  641. total_len += be32_to_cpu(seg->rs_length);
  642. ++cur_wchunk;
  643. }
  644. /* check and adjust for properly terminated write chunk */
  645. if (wrchunk) {
  646. __be32 *w = (__be32 *) cur_wchunk;
  647. if (*w++ != xdr_zero)
  648. return -1;
  649. cur_wchunk = (struct rpcrdma_write_chunk *) w;
  650. }
  651. if ((char *)cur_wchunk > base + rep->rr_len)
  652. return -1;
  653. *iptrp = (__be32 *) cur_wchunk;
  654. return total_len;
  655. }
  656. /**
  657. * rpcrdma_inline_fixup - Scatter inline received data into rqst's iovecs
  658. * @rqst: controlling RPC request
  659. * @srcp: points to RPC message payload in receive buffer
  660. * @copy_len: remaining length of receive buffer content
  661. * @pad: Write chunk pad bytes needed (zero for pure inline)
  662. *
  663. * The upper layer has set the maximum number of bytes it can
  664. * receive in each component of rq_rcv_buf. These values are set in
  665. * the head.iov_len, page_len, tail.iov_len, and buflen fields.
  666. *
  667. * Unlike the TCP equivalent (xdr_partial_copy_from_skb), in
  668. * many cases this function simply updates iov_base pointers in
  669. * rq_rcv_buf to point directly to the received reply data, to
  670. * avoid copying reply data.
  671. *
  672. * Returns the count of bytes which had to be memcopied.
  673. */
  674. static unsigned long
  675. rpcrdma_inline_fixup(struct rpc_rqst *rqst, char *srcp, int copy_len, int pad)
  676. {
  677. unsigned long fixup_copy_count;
  678. int i, npages, curlen;
  679. char *destp;
  680. struct page **ppages;
  681. int page_base;
  682. /* The head iovec is redirected to the RPC reply message
  683. * in the receive buffer, to avoid a memcopy.
  684. */
  685. rqst->rq_rcv_buf.head[0].iov_base = srcp;
  686. rqst->rq_private_buf.head[0].iov_base = srcp;
  687. /* The contents of the receive buffer that follow
  688. * head.iov_len bytes are copied into the page list.
  689. */
  690. curlen = rqst->rq_rcv_buf.head[0].iov_len;
  691. if (curlen > copy_len)
  692. curlen = copy_len;
  693. dprintk("RPC: %s: srcp 0x%p len %d hdrlen %d\n",
  694. __func__, srcp, copy_len, curlen);
  695. srcp += curlen;
  696. copy_len -= curlen;
  697. page_base = rqst->rq_rcv_buf.page_base;
  698. ppages = rqst->rq_rcv_buf.pages + (page_base >> PAGE_SHIFT);
  699. page_base &= ~PAGE_MASK;
  700. fixup_copy_count = 0;
  701. if (copy_len && rqst->rq_rcv_buf.page_len) {
  702. int pagelist_len;
  703. pagelist_len = rqst->rq_rcv_buf.page_len;
  704. if (pagelist_len > copy_len)
  705. pagelist_len = copy_len;
  706. npages = PAGE_ALIGN(page_base + pagelist_len) >> PAGE_SHIFT;
  707. for (i = 0; i < npages; i++) {
  708. curlen = PAGE_SIZE - page_base;
  709. if (curlen > pagelist_len)
  710. curlen = pagelist_len;
  711. dprintk("RPC: %s: page %d"
  712. " srcp 0x%p len %d curlen %d\n",
  713. __func__, i, srcp, copy_len, curlen);
  714. destp = kmap_atomic(ppages[i]);
  715. memcpy(destp + page_base, srcp, curlen);
  716. flush_dcache_page(ppages[i]);
  717. kunmap_atomic(destp);
  718. srcp += curlen;
  719. copy_len -= curlen;
  720. fixup_copy_count += curlen;
  721. pagelist_len -= curlen;
  722. if (!pagelist_len)
  723. break;
  724. page_base = 0;
  725. }
  726. /* Implicit padding for the last segment in a Write
  727. * chunk is inserted inline at the front of the tail
  728. * iovec. The upper layer ignores the content of
  729. * the pad. Simply ensure inline content in the tail
  730. * that follows the Write chunk is properly aligned.
  731. */
  732. if (pad)
  733. srcp -= pad;
  734. }
  735. /* The tail iovec is redirected to the remaining data
  736. * in the receive buffer, to avoid a memcopy.
  737. */
  738. if (copy_len || pad) {
  739. rqst->rq_rcv_buf.tail[0].iov_base = srcp;
  740. rqst->rq_private_buf.tail[0].iov_base = srcp;
  741. }
  742. return fixup_copy_count;
  743. }
  744. void
  745. rpcrdma_connect_worker(struct work_struct *work)
  746. {
  747. struct rpcrdma_ep *ep =
  748. container_of(work, struct rpcrdma_ep, rep_connect_worker.work);
  749. struct rpcrdma_xprt *r_xprt =
  750. container_of(ep, struct rpcrdma_xprt, rx_ep);
  751. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  752. spin_lock_bh(&xprt->transport_lock);
  753. if (++xprt->connect_cookie == 0) /* maintain a reserved value */
  754. ++xprt->connect_cookie;
  755. if (ep->rep_connected > 0) {
  756. if (!xprt_test_and_set_connected(xprt))
  757. xprt_wake_pending_tasks(xprt, 0);
  758. } else {
  759. if (xprt_test_and_clear_connected(xprt))
  760. xprt_wake_pending_tasks(xprt, -ENOTCONN);
  761. }
  762. spin_unlock_bh(&xprt->transport_lock);
  763. }
  764. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  765. /* By convention, backchannel calls arrive via rdma_msg type
  766. * messages, and never populate the chunk lists. This makes
  767. * the RPC/RDMA header small and fixed in size, so it is
  768. * straightforward to check the RPC header's direction field.
  769. */
  770. static bool
  771. rpcrdma_is_bcall(struct rpcrdma_msg *headerp)
  772. {
  773. __be32 *p = (__be32 *)headerp;
  774. if (headerp->rm_type != rdma_msg)
  775. return false;
  776. if (headerp->rm_body.rm_chunks[0] != xdr_zero)
  777. return false;
  778. if (headerp->rm_body.rm_chunks[1] != xdr_zero)
  779. return false;
  780. if (headerp->rm_body.rm_chunks[2] != xdr_zero)
  781. return false;
  782. /* sanity */
  783. if (p[7] != headerp->rm_xid)
  784. return false;
  785. /* call direction */
  786. if (p[8] != cpu_to_be32(RPC_CALL))
  787. return false;
  788. return true;
  789. }
  790. #endif /* CONFIG_SUNRPC_BACKCHANNEL */
  791. /*
  792. * This function is called when an async event is posted to
  793. * the connection which changes the connection state. All it
  794. * does at this point is mark the connection up/down, the rpc
  795. * timers do the rest.
  796. */
  797. void
  798. rpcrdma_conn_func(struct rpcrdma_ep *ep)
  799. {
  800. schedule_delayed_work(&ep->rep_connect_worker, 0);
  801. }
  802. /* Process received RPC/RDMA messages.
  803. *
  804. * Errors must result in the RPC task either being awakened, or
  805. * allowed to timeout, to discover the errors at that time.
  806. */
  807. void
  808. rpcrdma_reply_handler(struct rpcrdma_rep *rep)
  809. {
  810. struct rpcrdma_msg *headerp;
  811. struct rpcrdma_req *req;
  812. struct rpc_rqst *rqst;
  813. struct rpcrdma_xprt *r_xprt = rep->rr_rxprt;
  814. struct rpc_xprt *xprt = &r_xprt->rx_xprt;
  815. __be32 *iptr;
  816. int rdmalen, status, rmerr;
  817. unsigned long cwnd;
  818. dprintk("RPC: %s: incoming rep %p\n", __func__, rep);
  819. if (rep->rr_len == RPCRDMA_BAD_LEN)
  820. goto out_badstatus;
  821. if (rep->rr_len < RPCRDMA_HDRLEN_ERR)
  822. goto out_shortreply;
  823. headerp = rdmab_to_msg(rep->rr_rdmabuf);
  824. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  825. if (rpcrdma_is_bcall(headerp))
  826. goto out_bcall;
  827. #endif
  828. /* Match incoming rpcrdma_rep to an rpcrdma_req to
  829. * get context for handling any incoming chunks.
  830. */
  831. spin_lock_bh(&xprt->transport_lock);
  832. rqst = xprt_lookup_rqst(xprt, headerp->rm_xid);
  833. if (!rqst)
  834. goto out_nomatch;
  835. req = rpcr_to_rdmar(rqst);
  836. if (req->rl_reply)
  837. goto out_duplicate;
  838. /* Sanity checking has passed. We are now committed
  839. * to complete this transaction.
  840. */
  841. list_del_init(&rqst->rq_list);
  842. spin_unlock_bh(&xprt->transport_lock);
  843. dprintk("RPC: %s: reply %p completes request %p (xid 0x%08x)\n",
  844. __func__, rep, req, be32_to_cpu(headerp->rm_xid));
  845. /* from here on, the reply is no longer an orphan */
  846. req->rl_reply = rep;
  847. xprt->reestablish_timeout = 0;
  848. if (headerp->rm_vers != rpcrdma_version)
  849. goto out_badversion;
  850. /* check for expected message types */
  851. /* The order of some of these tests is important. */
  852. switch (headerp->rm_type) {
  853. case rdma_msg:
  854. /* never expect read chunks */
  855. /* never expect reply chunks (two ways to check) */
  856. /* never expect write chunks without having offered RDMA */
  857. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  858. (headerp->rm_body.rm_chunks[1] == xdr_zero &&
  859. headerp->rm_body.rm_chunks[2] != xdr_zero) ||
  860. (headerp->rm_body.rm_chunks[1] != xdr_zero &&
  861. list_empty(&req->rl_registered)))
  862. goto badheader;
  863. if (headerp->rm_body.rm_chunks[1] != xdr_zero) {
  864. /* count any expected write chunks in read reply */
  865. /* start at write chunk array count */
  866. iptr = &headerp->rm_body.rm_chunks[2];
  867. rdmalen = rpcrdma_count_chunks(rep, 1, &iptr);
  868. /* check for validity, and no reply chunk after */
  869. if (rdmalen < 0 || *iptr++ != xdr_zero)
  870. goto badheader;
  871. rep->rr_len -=
  872. ((unsigned char *)iptr - (unsigned char *)headerp);
  873. status = rep->rr_len + rdmalen;
  874. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  875. /* special case - last chunk may omit padding */
  876. if (rdmalen &= 3) {
  877. rdmalen = 4 - rdmalen;
  878. status += rdmalen;
  879. }
  880. } else {
  881. /* else ordinary inline */
  882. rdmalen = 0;
  883. iptr = (__be32 *)((unsigned char *)headerp +
  884. RPCRDMA_HDRLEN_MIN);
  885. rep->rr_len -= RPCRDMA_HDRLEN_MIN;
  886. status = rep->rr_len;
  887. }
  888. r_xprt->rx_stats.fixup_copy_count +=
  889. rpcrdma_inline_fixup(rqst, (char *)iptr, rep->rr_len,
  890. rdmalen);
  891. break;
  892. case rdma_nomsg:
  893. /* never expect read or write chunks, always reply chunks */
  894. if (headerp->rm_body.rm_chunks[0] != xdr_zero ||
  895. headerp->rm_body.rm_chunks[1] != xdr_zero ||
  896. headerp->rm_body.rm_chunks[2] != xdr_one ||
  897. list_empty(&req->rl_registered))
  898. goto badheader;
  899. iptr = (__be32 *)((unsigned char *)headerp +
  900. RPCRDMA_HDRLEN_MIN);
  901. rdmalen = rpcrdma_count_chunks(rep, 0, &iptr);
  902. if (rdmalen < 0)
  903. goto badheader;
  904. r_xprt->rx_stats.total_rdma_reply += rdmalen;
  905. /* Reply chunk buffer already is the reply vector - no fixup. */
  906. status = rdmalen;
  907. break;
  908. case rdma_error:
  909. goto out_rdmaerr;
  910. badheader:
  911. default:
  912. dprintk("RPC: %5u %s: invalid rpcrdma reply (type %u)\n",
  913. rqst->rq_task->tk_pid, __func__,
  914. be32_to_cpu(headerp->rm_type));
  915. status = -EIO;
  916. r_xprt->rx_stats.bad_reply_count++;
  917. break;
  918. }
  919. out:
  920. /* Invalidate and flush the data payloads before waking the
  921. * waiting application. This guarantees the memory region is
  922. * properly fenced from the server before the application
  923. * accesses the data. It also ensures proper send flow
  924. * control: waking the next RPC waits until this RPC has
  925. * relinquished all its Send Queue entries.
  926. */
  927. if (!list_empty(&req->rl_registered))
  928. r_xprt->rx_ia.ri_ops->ro_unmap_sync(r_xprt, req);
  929. spin_lock_bh(&xprt->transport_lock);
  930. cwnd = xprt->cwnd;
  931. xprt->cwnd = atomic_read(&r_xprt->rx_buf.rb_credits) << RPC_CWNDSHIFT;
  932. if (xprt->cwnd > cwnd)
  933. xprt_release_rqst_cong(rqst->rq_task);
  934. xprt_complete_rqst(rqst->rq_task, status);
  935. spin_unlock_bh(&xprt->transport_lock);
  936. dprintk("RPC: %s: xprt_complete_rqst(0x%p, 0x%p, %d)\n",
  937. __func__, xprt, rqst, status);
  938. return;
  939. out_badstatus:
  940. rpcrdma_recv_buffer_put(rep);
  941. if (r_xprt->rx_ep.rep_connected == 1) {
  942. r_xprt->rx_ep.rep_connected = -EIO;
  943. rpcrdma_conn_func(&r_xprt->rx_ep);
  944. }
  945. return;
  946. #if defined(CONFIG_SUNRPC_BACKCHANNEL)
  947. out_bcall:
  948. rpcrdma_bc_receive_call(r_xprt, rep);
  949. return;
  950. #endif
  951. /* If the incoming reply terminated a pending RPC, the next
  952. * RPC call will post a replacement receive buffer as it is
  953. * being marshaled.
  954. */
  955. out_badversion:
  956. dprintk("RPC: %s: invalid version %d\n",
  957. __func__, be32_to_cpu(headerp->rm_vers));
  958. status = -EIO;
  959. r_xprt->rx_stats.bad_reply_count++;
  960. goto out;
  961. out_rdmaerr:
  962. rmerr = be32_to_cpu(headerp->rm_body.rm_error.rm_err);
  963. switch (rmerr) {
  964. case ERR_VERS:
  965. pr_err("%s: server reports header version error (%u-%u)\n",
  966. __func__,
  967. be32_to_cpu(headerp->rm_body.rm_error.rm_vers_low),
  968. be32_to_cpu(headerp->rm_body.rm_error.rm_vers_high));
  969. break;
  970. case ERR_CHUNK:
  971. pr_err("%s: server reports header decoding error\n",
  972. __func__);
  973. break;
  974. default:
  975. pr_err("%s: server reports unknown error %d\n",
  976. __func__, rmerr);
  977. }
  978. status = -EREMOTEIO;
  979. r_xprt->rx_stats.bad_reply_count++;
  980. goto out;
  981. /* If no pending RPC transaction was matched, post a replacement
  982. * receive buffer before returning.
  983. */
  984. out_shortreply:
  985. dprintk("RPC: %s: short/invalid reply\n", __func__);
  986. goto repost;
  987. out_nomatch:
  988. spin_unlock_bh(&xprt->transport_lock);
  989. dprintk("RPC: %s: no match for incoming xid 0x%08x len %d\n",
  990. __func__, be32_to_cpu(headerp->rm_xid),
  991. rep->rr_len);
  992. goto repost;
  993. out_duplicate:
  994. spin_unlock_bh(&xprt->transport_lock);
  995. dprintk("RPC: %s: "
  996. "duplicate reply %p to RPC request %p: xid 0x%08x\n",
  997. __func__, rep, req, be32_to_cpu(headerp->rm_xid));
  998. repost:
  999. r_xprt->rx_stats.bad_reply_count++;
  1000. if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, &r_xprt->rx_ep, rep))
  1001. rpcrdma_recv_buffer_put(rep);
  1002. }