Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Oracle. All rights reserved. |
| 3 | * |
| 4 | * Support for backward direction RPCs on RPC/RDMA (server-side). |
| 5 | */ |
| 6 | |
Russell King | f39b2dde | 2017-02-07 15:02:54 -0800 | [diff] [blame] | 7 | #include <linux/module.h> |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 8 | #include <linux/sunrpc/svc_rdma.h> |
| 9 | #include "xprt_rdma.h" |
| 10 | |
| 11 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT |
| 12 | |
| 13 | #undef SVCRDMA_BACKCHANNEL_DEBUG |
| 14 | |
| 15 | int svc_rdma_handle_bc_reply(struct rpc_xprt *xprt, struct rpcrdma_msg *rmsgp, |
| 16 | struct xdr_buf *rcvbuf) |
| 17 | { |
| 18 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); |
| 19 | struct kvec *dst, *src = &rcvbuf->head[0]; |
| 20 | struct rpc_rqst *req; |
| 21 | unsigned long cwnd; |
| 22 | u32 credits; |
| 23 | size_t len; |
| 24 | __be32 xid; |
| 25 | __be32 *p; |
| 26 | int ret; |
| 27 | |
| 28 | p = (__be32 *)src->iov_base; |
| 29 | len = src->iov_len; |
| 30 | xid = rmsgp->rm_xid; |
| 31 | |
| 32 | #ifdef SVCRDMA_BACKCHANNEL_DEBUG |
| 33 | pr_info("%s: xid=%08x, length=%zu\n", |
| 34 | __func__, be32_to_cpu(xid), len); |
| 35 | pr_info("%s: RPC/RDMA: %*ph\n", |
| 36 | __func__, (int)RPCRDMA_HDRLEN_MIN, rmsgp); |
| 37 | pr_info("%s: RPC: %*ph\n", |
| 38 | __func__, (int)len, p); |
| 39 | #endif |
| 40 | |
| 41 | ret = -EAGAIN; |
| 42 | if (src->iov_len < 24) |
| 43 | goto out_shortreply; |
| 44 | |
| 45 | spin_lock_bh(&xprt->transport_lock); |
| 46 | req = xprt_lookup_rqst(xprt, xid); |
| 47 | if (!req) |
| 48 | goto out_notfound; |
| 49 | |
| 50 | dst = &req->rq_private_buf.head[0]; |
| 51 | memcpy(&req->rq_private_buf, &req->rq_rcv_buf, sizeof(struct xdr_buf)); |
| 52 | if (dst->iov_len < len) |
| 53 | goto out_unlock; |
| 54 | memcpy(dst->iov_base, p, len); |
| 55 | |
| 56 | credits = be32_to_cpu(rmsgp->rm_credit); |
| 57 | if (credits == 0) |
| 58 | credits = 1; /* don't deadlock */ |
| 59 | else if (credits > r_xprt->rx_buf.rb_bc_max_requests) |
| 60 | credits = r_xprt->rx_buf.rb_bc_max_requests; |
| 61 | |
| 62 | cwnd = xprt->cwnd; |
| 63 | xprt->cwnd = credits << RPC_CWNDSHIFT; |
| 64 | if (xprt->cwnd > cwnd) |
| 65 | xprt_release_rqst_cong(req->rq_task); |
| 66 | |
| 67 | ret = 0; |
| 68 | xprt_complete_rqst(req->rq_task, rcvbuf->len); |
| 69 | rcvbuf->len = 0; |
| 70 | |
| 71 | out_unlock: |
| 72 | spin_unlock_bh(&xprt->transport_lock); |
| 73 | out: |
| 74 | return ret; |
| 75 | |
| 76 | out_shortreply: |
| 77 | dprintk("svcrdma: short bc reply: xprt=%p, len=%zu\n", |
| 78 | xprt, src->iov_len); |
| 79 | goto out; |
| 80 | |
| 81 | out_notfound: |
| 82 | dprintk("svcrdma: unrecognized bc reply: xprt=%p, xid=%08x\n", |
| 83 | xprt, be32_to_cpu(xid)); |
| 84 | |
| 85 | goto out_unlock; |
| 86 | } |
| 87 | |
| 88 | /* Send a backwards direction RPC call. |
| 89 | * |
| 90 | * Caller holds the connection's mutex and has already marshaled |
| 91 | * the RPC/RDMA request. |
| 92 | * |
| 93 | * This is similar to svc_rdma_reply, but takes an rpc_rqst |
| 94 | * instead, does not support chunks, and avoids blocking memory |
| 95 | * allocation. |
| 96 | * |
| 97 | * XXX: There is still an opportunity to block in svc_rdma_send() |
| 98 | * if there are no SQ entries to post the Send. This may occur if |
| 99 | * the adapter has a small maximum SQ depth. |
| 100 | */ |
| 101 | static int svc_rdma_bc_sendto(struct svcxprt_rdma *rdma, |
| 102 | struct rpc_rqst *rqst) |
| 103 | { |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 104 | struct svc_rdma_op_ctxt *ctxt; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 105 | int ret; |
| 106 | |
Chuck Lever | 6e6092c | 2017-04-09 13:05:44 -0400 | [diff] [blame^] | 107 | ctxt = svc_rdma_get_context(rdma); |
| 108 | |
| 109 | /* rpcrdma_bc_send_request builds the transport header and |
| 110 | * the backchannel RPC message in the same buffer. Thus only |
| 111 | * one SGE is needed to send both. |
| 112 | */ |
| 113 | ret = svc_rdma_map_reply_hdr(rdma, ctxt, rqst->rq_buffer, |
| 114 | rqst->rq_snd_buf.len); |
| 115 | if (ret < 0) |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 116 | goto out_err; |
| 117 | |
Chuck Lever | bf36387 | 2016-03-01 13:06:20 -0500 | [diff] [blame] | 118 | ret = svc_rdma_repost_recv(rdma, GFP_NOIO); |
| 119 | if (ret) |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 120 | goto out_err; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 121 | |
Chuck Lever | 17f5f7f | 2017-04-09 13:05:36 -0400 | [diff] [blame] | 122 | ret = svc_rdma_post_send_wr(rdma, ctxt, 1, 0); |
Chuck Lever | 6e6092c | 2017-04-09 13:05:44 -0400 | [diff] [blame^] | 123 | if (ret) |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 124 | goto out_unmap; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 125 | |
| 126 | out_err: |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 127 | dprintk("svcrdma: %s returns %d\n", __func__, ret); |
| 128 | return ret; |
| 129 | |
| 130 | out_unmap: |
| 131 | svc_rdma_unmap_dma(ctxt); |
| 132 | svc_rdma_put_context(ctxt, 1); |
Chuck Lever | 6e6092c | 2017-04-09 13:05:44 -0400 | [diff] [blame^] | 133 | ret = -EIO; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 134 | goto out_err; |
| 135 | } |
| 136 | |
| 137 | /* Server-side transport endpoint wants a whole page for its send |
| 138 | * buffer. The client RPC code constructs the RPC header in this |
| 139 | * buffer before it invokes ->send_request. |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 140 | */ |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 141 | static int |
| 142 | xprt_rdma_bc_allocate(struct rpc_task *task) |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 143 | { |
| 144 | struct rpc_rqst *rqst = task->tk_rqstp; |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 145 | size_t size = rqst->rq_callsize; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 146 | struct page *page; |
| 147 | |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 148 | if (size > PAGE_SIZE) { |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 149 | WARN_ONCE(1, "svcrdma: large bc buffer request (size %zu)\n", |
| 150 | size); |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | } |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 153 | |
Chuck Lever | 8d42629 | 2016-10-28 22:22:33 -0400 | [diff] [blame] | 154 | /* svc_rdma_sendto releases this page */ |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 155 | page = alloc_page(RPCRDMA_DEF_GFP); |
| 156 | if (!page) |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 157 | return -ENOMEM; |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 158 | rqst->rq_buffer = page_address(page); |
Chuck Lever | 8d42629 | 2016-10-28 22:22:33 -0400 | [diff] [blame] | 159 | |
| 160 | rqst->rq_rbuffer = kmalloc(rqst->rq_rcvsize, RPCRDMA_DEF_GFP); |
| 161 | if (!rqst->rq_rbuffer) { |
| 162 | put_page(page); |
| 163 | return -ENOMEM; |
| 164 | } |
Chuck Lever | 5fe6eaa | 2016-09-15 10:55:20 -0400 | [diff] [blame] | 165 | return 0; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static void |
Chuck Lever | 3435c74 | 2016-09-15 10:55:29 -0400 | [diff] [blame] | 169 | xprt_rdma_bc_free(struct rpc_task *task) |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 170 | { |
Chuck Lever | 8d42629 | 2016-10-28 22:22:33 -0400 | [diff] [blame] | 171 | struct rpc_rqst *rqst = task->tk_rqstp; |
| 172 | |
| 173 | kfree(rqst->rq_rbuffer); |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static int |
| 177 | rpcrdma_bc_send_request(struct svcxprt_rdma *rdma, struct rpc_rqst *rqst) |
| 178 | { |
| 179 | struct rpc_xprt *xprt = rqst->rq_xprt; |
| 180 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); |
Chuck Lever | c2ccf64 | 2017-02-07 11:58:40 -0500 | [diff] [blame] | 181 | __be32 *p; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 182 | int rc; |
| 183 | |
| 184 | /* Space in the send buffer for an RPC/RDMA header is reserved |
| 185 | * via xprt->tsh_size. |
| 186 | */ |
Chuck Lever | c2ccf64 | 2017-02-07 11:58:40 -0500 | [diff] [blame] | 187 | p = rqst->rq_buffer; |
| 188 | *p++ = rqst->rq_xid; |
| 189 | *p++ = rpcrdma_version; |
| 190 | *p++ = cpu_to_be32(r_xprt->rx_buf.rb_bc_max_requests); |
| 191 | *p++ = rdma_msg; |
| 192 | *p++ = xdr_zero; |
| 193 | *p++ = xdr_zero; |
| 194 | *p = xdr_zero; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 195 | |
| 196 | #ifdef SVCRDMA_BACKCHANNEL_DEBUG |
| 197 | pr_info("%s: %*ph\n", __func__, 64, rqst->rq_buffer); |
| 198 | #endif |
| 199 | |
| 200 | rc = svc_rdma_bc_sendto(rdma, rqst); |
| 201 | if (rc) |
| 202 | goto drop_connection; |
| 203 | return rc; |
| 204 | |
| 205 | drop_connection: |
| 206 | dprintk("svcrdma: failed to send bc call\n"); |
| 207 | xprt_disconnect_done(xprt); |
| 208 | return -ENOTCONN; |
| 209 | } |
| 210 | |
| 211 | /* Send an RPC call on the passive end of a transport |
| 212 | * connection. |
| 213 | */ |
| 214 | static int |
| 215 | xprt_rdma_bc_send_request(struct rpc_task *task) |
| 216 | { |
| 217 | struct rpc_rqst *rqst = task->tk_rqstp; |
| 218 | struct svc_xprt *sxprt = rqst->rq_xprt->bc_xprt; |
| 219 | struct svcxprt_rdma *rdma; |
| 220 | int ret; |
| 221 | |
| 222 | dprintk("svcrdma: sending bc call with xid: %08x\n", |
| 223 | be32_to_cpu(rqst->rq_xid)); |
| 224 | |
| 225 | if (!mutex_trylock(&sxprt->xpt_mutex)) { |
| 226 | rpc_sleep_on(&sxprt->xpt_bc_pending, task, NULL); |
| 227 | if (!mutex_trylock(&sxprt->xpt_mutex)) |
| 228 | return -EAGAIN; |
| 229 | rpc_wake_up_queued_task(&sxprt->xpt_bc_pending, task); |
| 230 | } |
| 231 | |
| 232 | ret = -ENOTCONN; |
| 233 | rdma = container_of(sxprt, struct svcxprt_rdma, sc_xprt); |
| 234 | if (!test_bit(XPT_DEAD, &sxprt->xpt_flags)) |
| 235 | ret = rpcrdma_bc_send_request(rdma, rqst); |
| 236 | |
| 237 | mutex_unlock(&sxprt->xpt_mutex); |
| 238 | |
| 239 | if (ret < 0) |
| 240 | return ret; |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static void |
| 245 | xprt_rdma_bc_close(struct rpc_xprt *xprt) |
| 246 | { |
| 247 | dprintk("svcrdma: %s: xprt %p\n", __func__, xprt); |
| 248 | } |
| 249 | |
| 250 | static void |
| 251 | xprt_rdma_bc_put(struct rpc_xprt *xprt) |
| 252 | { |
| 253 | dprintk("svcrdma: %s: xprt %p\n", __func__, xprt); |
| 254 | |
| 255 | xprt_free(xprt); |
| 256 | module_put(THIS_MODULE); |
| 257 | } |
| 258 | |
| 259 | static struct rpc_xprt_ops xprt_rdma_bc_procs = { |
| 260 | .reserve_xprt = xprt_reserve_xprt_cong, |
| 261 | .release_xprt = xprt_release_xprt_cong, |
| 262 | .alloc_slot = xprt_alloc_slot, |
| 263 | .release_request = xprt_release_rqst_cong, |
| 264 | .buf_alloc = xprt_rdma_bc_allocate, |
| 265 | .buf_free = xprt_rdma_bc_free, |
| 266 | .send_request = xprt_rdma_bc_send_request, |
| 267 | .set_retrans_timeout = xprt_set_retrans_timeout_def, |
| 268 | .close = xprt_rdma_bc_close, |
| 269 | .destroy = xprt_rdma_bc_put, |
| 270 | .print_stats = xprt_rdma_print_stats |
| 271 | }; |
| 272 | |
| 273 | static const struct rpc_timeout xprt_rdma_bc_timeout = { |
| 274 | .to_initval = 60 * HZ, |
| 275 | .to_maxval = 60 * HZ, |
| 276 | }; |
| 277 | |
| 278 | /* It shouldn't matter if the number of backchannel session slots |
| 279 | * doesn't match the number of RPC/RDMA credits. That just means |
| 280 | * one or the other will have extra slots that aren't used. |
| 281 | */ |
| 282 | static struct rpc_xprt * |
| 283 | xprt_setup_rdma_bc(struct xprt_create *args) |
| 284 | { |
| 285 | struct rpc_xprt *xprt; |
| 286 | struct rpcrdma_xprt *new_xprt; |
| 287 | |
| 288 | if (args->addrlen > sizeof(xprt->addr)) { |
| 289 | dprintk("RPC: %s: address too large\n", __func__); |
| 290 | return ERR_PTR(-EBADF); |
| 291 | } |
| 292 | |
| 293 | xprt = xprt_alloc(args->net, sizeof(*new_xprt), |
| 294 | RPCRDMA_MAX_BC_REQUESTS, |
| 295 | RPCRDMA_MAX_BC_REQUESTS); |
| 296 | if (!xprt) { |
| 297 | dprintk("RPC: %s: couldn't allocate rpc_xprt\n", |
| 298 | __func__); |
| 299 | return ERR_PTR(-ENOMEM); |
| 300 | } |
| 301 | |
| 302 | xprt->timeout = &xprt_rdma_bc_timeout; |
| 303 | xprt_set_bound(xprt); |
| 304 | xprt_set_connected(xprt); |
| 305 | xprt->bind_timeout = RPCRDMA_BIND_TO; |
| 306 | xprt->reestablish_timeout = RPCRDMA_INIT_REEST_TO; |
| 307 | xprt->idle_timeout = RPCRDMA_IDLE_DISC_TO; |
| 308 | |
| 309 | xprt->prot = XPRT_TRANSPORT_BC_RDMA; |
| 310 | xprt->tsh_size = RPCRDMA_HDRLEN_MIN / sizeof(__be32); |
| 311 | xprt->ops = &xprt_rdma_bc_procs; |
| 312 | |
| 313 | memcpy(&xprt->addr, args->dstaddr, args->addrlen); |
| 314 | xprt->addrlen = args->addrlen; |
| 315 | xprt_rdma_format_addresses(xprt, (struct sockaddr *)&xprt->addr); |
| 316 | xprt->resvport = 0; |
| 317 | |
| 318 | xprt->max_payload = xprt_rdma_max_inline_read; |
| 319 | |
| 320 | new_xprt = rpcx_to_rdmax(xprt); |
| 321 | new_xprt->rx_buf.rb_bc_max_requests = xprt->max_reqs; |
| 322 | |
| 323 | xprt_get(xprt); |
| 324 | args->bc_xprt->xpt_bc_xprt = xprt; |
| 325 | xprt->bc_xprt = args->bc_xprt; |
| 326 | |
| 327 | if (!try_module_get(THIS_MODULE)) |
| 328 | goto out_fail; |
| 329 | |
| 330 | /* Final put for backchannel xprt is in __svc_rdma_free */ |
| 331 | xprt_get(xprt); |
| 332 | return xprt; |
| 333 | |
| 334 | out_fail: |
| 335 | xprt_rdma_free_addresses(xprt); |
| 336 | args->bc_xprt->xpt_bc_xprt = NULL; |
Chuck Lever | 1b9f700 | 2016-11-29 11:04:26 -0500 | [diff] [blame] | 337 | args->bc_xprt->xpt_bc_xps = NULL; |
Chuck Lever | 5d252f9 | 2016-01-07 14:50:10 -0500 | [diff] [blame] | 338 | xprt_put(xprt); |
| 339 | xprt_free(xprt); |
| 340 | return ERR_PTR(-EINVAL); |
| 341 | } |
| 342 | |
| 343 | struct xprt_class xprt_rdma_bc = { |
| 344 | .list = LIST_HEAD_INIT(xprt_rdma_bc.list), |
| 345 | .name = "rdma backchannel", |
| 346 | .owner = THIS_MODULE, |
| 347 | .ident = XPRT_TRANSPORT_BC_RDMA, |
| 348 | .setup = xprt_setup_rdma_bc, |
| 349 | }; |