]>
Commit | Line | Data |
---|---|---|
f531a5db CL |
1 | /* |
2 | * Copyright (c) 2015 Oracle. All rights reserved. | |
3 | * | |
4 | * Support for backward direction RPCs on RPC/RDMA. | |
5 | */ | |
6 | ||
7 | #include <linux/module.h> | |
63cae470 CL |
8 | #include <linux/sunrpc/xprt.h> |
9 | #include <linux/sunrpc/svc.h> | |
76566773 | 10 | #include <linux/sunrpc/svc_xprt.h> |
f531a5db CL |
11 | |
12 | #include "xprt_rdma.h" | |
13 | ||
14 | #if IS_ENABLED(CONFIG_SUNRPC_DEBUG) | |
15 | # define RPCDBG_FACILITY RPCDBG_TRANS | |
16 | #endif | |
17 | ||
c8bbe0c7 | 18 | #undef RPCRDMA_BACKCHANNEL_DEBUG |
63cae470 | 19 | |
f531a5db CL |
20 | static void rpcrdma_bc_free_rqst(struct rpcrdma_xprt *r_xprt, |
21 | struct rpc_rqst *rqst) | |
22 | { | |
23 | struct rpcrdma_buffer *buf = &r_xprt->rx_buf; | |
24 | struct rpcrdma_req *req = rpcr_to_rdmar(rqst); | |
25 | ||
26 | spin_lock(&buf->rb_reqslock); | |
27 | list_del(&req->rl_all); | |
28 | spin_unlock(&buf->rb_reqslock); | |
29 | ||
13650c23 | 30 | rpcrdma_destroy_req(req); |
f531a5db CL |
31 | |
32 | kfree(rqst); | |
33 | } | |
34 | ||
35 | static int rpcrdma_bc_setup_rqst(struct rpcrdma_xprt *r_xprt, | |
36 | struct rpc_rqst *rqst) | |
37 | { | |
f531a5db CL |
38 | struct rpcrdma_regbuf *rb; |
39 | struct rpcrdma_req *req; | |
f531a5db CL |
40 | size_t size; |
41 | ||
42 | req = rpcrdma_create_req(r_xprt); | |
abfb6897 DC |
43 | if (IS_ERR(req)) |
44 | return PTR_ERR(req); | |
f531a5db CL |
45 | req->rl_backchannel = true; |
46 | ||
13650c23 | 47 | rb = rpcrdma_alloc_regbuf(RPCRDMA_HDRBUF_SIZE, |
99ef4db3 | 48 | DMA_TO_DEVICE, GFP_KERNEL); |
f531a5db CL |
49 | if (IS_ERR(rb)) |
50 | goto out_fail; | |
51 | req->rl_rdmabuf = rb; | |
52 | ||
08cf2efd | 53 | size = r_xprt->rx_data.inline_rsize; |
13650c23 | 54 | rb = rpcrdma_alloc_regbuf(size, DMA_TO_DEVICE, GFP_KERNEL); |
f531a5db CL |
55 | if (IS_ERR(rb)) |
56 | goto out_fail; | |
f531a5db | 57 | req->rl_sendbuf = rb; |
5a6d1db4 CL |
58 | xdr_buf_init(&rqst->rq_snd_buf, rb->rg_base, size); |
59 | rpcrdma_set_xprtdata(rqst, req); | |
f531a5db CL |
60 | return 0; |
61 | ||
62 | out_fail: | |
63 | rpcrdma_bc_free_rqst(r_xprt, rqst); | |
64 | return -ENOMEM; | |
65 | } | |
66 | ||
67 | /* Allocate and add receive buffers to the rpcrdma_buffer's | |
68 | * existing list of rep's. These are released when the | |
69 | * transport is destroyed. | |
70 | */ | |
71 | static int rpcrdma_bc_setup_reps(struct rpcrdma_xprt *r_xprt, | |
72 | unsigned int count) | |
73 | { | |
f531a5db | 74 | struct rpcrdma_rep *rep; |
f531a5db CL |
75 | int rc = 0; |
76 | ||
77 | while (count--) { | |
78 | rep = rpcrdma_create_rep(r_xprt); | |
79 | if (IS_ERR(rep)) { | |
80 | pr_err("RPC: %s: reply buffer alloc failed\n", | |
81 | __func__); | |
82 | rc = PTR_ERR(rep); | |
83 | break; | |
84 | } | |
85 | ||
9b06688b | 86 | rpcrdma_recv_buffer_put(rep); |
f531a5db CL |
87 | } |
88 | ||
89 | return rc; | |
90 | } | |
91 | ||
92 | /** | |
93 | * xprt_rdma_bc_setup - Pre-allocate resources for handling backchannel requests | |
94 | * @xprt: transport associated with these backchannel resources | |
95 | * @reqs: number of concurrent incoming requests to expect | |
96 | * | |
97 | * Returns 0 on success; otherwise a negative errno | |
98 | */ | |
99 | int xprt_rdma_bc_setup(struct rpc_xprt *xprt, unsigned int reqs) | |
100 | { | |
101 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
102 | struct rpcrdma_buffer *buffer = &r_xprt->rx_buf; | |
103 | struct rpc_rqst *rqst; | |
104 | unsigned int i; | |
105 | int rc; | |
106 | ||
107 | /* The backchannel reply path returns each rpc_rqst to the | |
108 | * bc_pa_list _after_ the reply is sent. If the server is | |
109 | * faster than the client, it can send another backward | |
110 | * direction request before the rpc_rqst is returned to the | |
111 | * list. The client rejects the request in this case. | |
112 | * | |
113 | * Twice as many rpc_rqsts are prepared to ensure there is | |
114 | * always an rpc_rqst available as soon as a reply is sent. | |
115 | */ | |
124fa17d CL |
116 | if (reqs > RPCRDMA_BACKWARD_WRS >> 1) |
117 | goto out_err; | |
118 | ||
f531a5db CL |
119 | for (i = 0; i < (reqs << 1); i++) { |
120 | rqst = kzalloc(sizeof(*rqst), GFP_KERNEL); | |
121 | if (!rqst) { | |
122 | pr_err("RPC: %s: Failed to create bc rpc_rqst\n", | |
123 | __func__); | |
124 | goto out_free; | |
125 | } | |
c8bbe0c7 | 126 | dprintk("RPC: %s: new rqst %p\n", __func__, rqst); |
f531a5db CL |
127 | |
128 | rqst->rq_xprt = &r_xprt->rx_xprt; | |
129 | INIT_LIST_HEAD(&rqst->rq_list); | |
130 | INIT_LIST_HEAD(&rqst->rq_bc_list); | |
131 | ||
132 | if (rpcrdma_bc_setup_rqst(r_xprt, rqst)) | |
133 | goto out_free; | |
134 | ||
135 | spin_lock_bh(&xprt->bc_pa_lock); | |
136 | list_add(&rqst->rq_bc_pa_list, &xprt->bc_pa_list); | |
137 | spin_unlock_bh(&xprt->bc_pa_lock); | |
138 | } | |
139 | ||
140 | rc = rpcrdma_bc_setup_reps(r_xprt, reqs); | |
141 | if (rc) | |
142 | goto out_free; | |
143 | ||
144 | rc = rpcrdma_ep_post_extra_recv(r_xprt, reqs); | |
145 | if (rc) | |
146 | goto out_free; | |
147 | ||
148 | buffer->rb_bc_srv_max_requests = reqs; | |
149 | request_module("svcrdma"); | |
150 | ||
151 | return 0; | |
152 | ||
153 | out_free: | |
154 | xprt_rdma_bc_destroy(xprt, reqs); | |
155 | ||
124fa17d | 156 | out_err: |
f531a5db CL |
157 | pr_err("RPC: %s: setup backchannel transport failed\n", __func__); |
158 | return -ENOMEM; | |
159 | } | |
160 | ||
76566773 CL |
161 | /** |
162 | * xprt_rdma_bc_up - Create transport endpoint for backchannel service | |
163 | * @serv: server endpoint | |
164 | * @net: network namespace | |
165 | * | |
166 | * The "xprt" is an implied argument: it supplies the name of the | |
167 | * backchannel transport class. | |
168 | * | |
169 | * Returns zero on success, negative errno on failure | |
170 | */ | |
171 | int xprt_rdma_bc_up(struct svc_serv *serv, struct net *net) | |
172 | { | |
173 | int ret; | |
174 | ||
175 | ret = svc_create_xprt(serv, "rdma-bc", net, PF_INET, 0, 0); | |
176 | if (ret < 0) | |
177 | return ret; | |
178 | return 0; | |
179 | } | |
180 | ||
6b26cc8c CL |
181 | /** |
182 | * xprt_rdma_bc_maxpayload - Return maximum backchannel message size | |
183 | * @xprt: transport | |
184 | * | |
185 | * Returns maximum size, in bytes, of a backchannel message | |
186 | */ | |
187 | size_t xprt_rdma_bc_maxpayload(struct rpc_xprt *xprt) | |
188 | { | |
189 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
190 | struct rpcrdma_create_data_internal *cdata = &r_xprt->rx_data; | |
191 | size_t maxmsg; | |
192 | ||
193 | maxmsg = min_t(unsigned int, cdata->inline_rsize, cdata->inline_wsize); | |
194 | return maxmsg - RPCRDMA_HDRLEN_MIN; | |
195 | } | |
196 | ||
83128a60 CL |
197 | /** |
198 | * rpcrdma_bc_marshal_reply - Send backwards direction reply | |
199 | * @rqst: buffer containing RPC reply data | |
200 | * | |
201 | * Returns zero on success. | |
202 | */ | |
203 | int rpcrdma_bc_marshal_reply(struct rpc_rqst *rqst) | |
204 | { | |
205 | struct rpc_xprt *xprt = rqst->rq_xprt; | |
206 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
207 | struct rpcrdma_req *req = rpcr_to_rdmar(rqst); | |
208 | struct rpcrdma_msg *headerp; | |
209 | size_t rpclen; | |
210 | ||
211 | headerp = rdmab_to_msg(req->rl_rdmabuf); | |
212 | headerp->rm_xid = rqst->rq_xid; | |
213 | headerp->rm_vers = rpcrdma_version; | |
214 | headerp->rm_credit = | |
215 | cpu_to_be32(r_xprt->rx_buf.rb_bc_srv_max_requests); | |
216 | headerp->rm_type = rdma_msg; | |
217 | headerp->rm_body.rm_chunks[0] = xdr_zero; | |
218 | headerp->rm_body.rm_chunks[1] = xdr_zero; | |
219 | headerp->rm_body.rm_chunks[2] = xdr_zero; | |
220 | ||
221 | rpclen = rqst->rq_svec[0].iov_len; | |
222 | ||
c8bbe0c7 | 223 | #ifdef RPCRDMA_BACKCHANNEL_DEBUG |
83128a60 CL |
224 | pr_info("RPC: %s: rpclen %zd headerp 0x%p lkey 0x%x\n", |
225 | __func__, rpclen, headerp, rdmab_lkey(req->rl_rdmabuf)); | |
226 | pr_info("RPC: %s: RPC/RDMA: %*ph\n", | |
227 | __func__, (int)RPCRDMA_HDRLEN_MIN, headerp); | |
228 | pr_info("RPC: %s: RPC: %*ph\n", | |
229 | __func__, (int)rpclen, rqst->rq_svec[0].iov_base); | |
c8bbe0c7 | 230 | #endif |
83128a60 | 231 | |
54cbd6b0 CL |
232 | if (!rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, req->rl_rdmabuf)) |
233 | goto out_map; | |
83128a60 CL |
234 | req->rl_send_iov[0].addr = rdmab_addr(req->rl_rdmabuf); |
235 | req->rl_send_iov[0].length = RPCRDMA_HDRLEN_MIN; | |
236 | req->rl_send_iov[0].lkey = rdmab_lkey(req->rl_rdmabuf); | |
237 | ||
54cbd6b0 CL |
238 | if (!rpcrdma_dma_map_regbuf(&r_xprt->rx_ia, req->rl_sendbuf)) |
239 | goto out_map; | |
83128a60 CL |
240 | req->rl_send_iov[1].addr = rdmab_addr(req->rl_sendbuf); |
241 | req->rl_send_iov[1].length = rpclen; | |
242 | req->rl_send_iov[1].lkey = rdmab_lkey(req->rl_sendbuf); | |
243 | ||
90aab602 CL |
244 | req->rl_send_wr.num_sge = 2; |
245 | ||
83128a60 | 246 | return 0; |
54cbd6b0 CL |
247 | |
248 | out_map: | |
249 | pr_err("rpcrdma: failed to DMA map a Send buffer\n"); | |
250 | return -EIO; | |
83128a60 CL |
251 | } |
252 | ||
f531a5db CL |
253 | /** |
254 | * xprt_rdma_bc_destroy - Release resources for handling backchannel requests | |
255 | * @xprt: transport associated with these backchannel resources | |
256 | * @reqs: number of incoming requests to destroy; ignored | |
257 | */ | |
258 | void xprt_rdma_bc_destroy(struct rpc_xprt *xprt, unsigned int reqs) | |
259 | { | |
260 | struct rpcrdma_xprt *r_xprt = rpcx_to_rdmax(xprt); | |
261 | struct rpc_rqst *rqst, *tmp; | |
262 | ||
263 | spin_lock_bh(&xprt->bc_pa_lock); | |
264 | list_for_each_entry_safe(rqst, tmp, &xprt->bc_pa_list, rq_bc_pa_list) { | |
265 | list_del(&rqst->rq_bc_pa_list); | |
266 | spin_unlock_bh(&xprt->bc_pa_lock); | |
267 | ||
268 | rpcrdma_bc_free_rqst(r_xprt, rqst); | |
269 | ||
270 | spin_lock_bh(&xprt->bc_pa_lock); | |
271 | } | |
272 | spin_unlock_bh(&xprt->bc_pa_lock); | |
273 | } | |
274 | ||
275 | /** | |
276 | * xprt_rdma_bc_free_rqst - Release a backchannel rqst | |
277 | * @rqst: request to release | |
278 | */ | |
279 | void xprt_rdma_bc_free_rqst(struct rpc_rqst *rqst) | |
280 | { | |
281 | struct rpc_xprt *xprt = rqst->rq_xprt; | |
282 | ||
c8bbe0c7 CL |
283 | dprintk("RPC: %s: freeing rqst %p (req %p)\n", |
284 | __func__, rqst, rpcr_to_rdmar(rqst)); | |
285 | ||
f531a5db CL |
286 | smp_mb__before_atomic(); |
287 | WARN_ON_ONCE(!test_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state)); | |
288 | clear_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state); | |
289 | smp_mb__after_atomic(); | |
290 | ||
291 | spin_lock_bh(&xprt->bc_pa_lock); | |
292 | list_add_tail(&rqst->rq_bc_pa_list, &xprt->bc_pa_list); | |
293 | spin_unlock_bh(&xprt->bc_pa_lock); | |
294 | } | |
63cae470 CL |
295 | |
296 | /** | |
297 | * rpcrdma_bc_receive_call - Handle a backward direction call | |
298 | * @xprt: transport receiving the call | |
299 | * @rep: receive buffer containing the call | |
300 | * | |
301 | * Called in the RPC reply handler, which runs in a tasklet. | |
302 | * Be quick about it. | |
303 | * | |
304 | * Operational assumptions: | |
305 | * o Backchannel credits are ignored, just as the NFS server | |
306 | * forechannel currently does | |
307 | * o The ULP manages a replay cache (eg, NFSv4.1 sessions). | |
308 | * No replay detection is done at the transport level | |
309 | */ | |
310 | void rpcrdma_bc_receive_call(struct rpcrdma_xprt *r_xprt, | |
311 | struct rpcrdma_rep *rep) | |
312 | { | |
313 | struct rpc_xprt *xprt = &r_xprt->rx_xprt; | |
314 | struct rpcrdma_msg *headerp; | |
315 | struct svc_serv *bc_serv; | |
316 | struct rpcrdma_req *req; | |
317 | struct rpc_rqst *rqst; | |
318 | struct xdr_buf *buf; | |
319 | size_t size; | |
320 | __be32 *p; | |
321 | ||
322 | headerp = rdmab_to_msg(rep->rr_rdmabuf); | |
323 | #ifdef RPCRDMA_BACKCHANNEL_DEBUG | |
324 | pr_info("RPC: %s: callback XID %08x, length=%u\n", | |
325 | __func__, be32_to_cpu(headerp->rm_xid), rep->rr_len); | |
326 | pr_info("RPC: %s: %*ph\n", __func__, rep->rr_len, headerp); | |
327 | #endif | |
328 | ||
329 | /* Sanity check: | |
330 | * Need at least enough bytes for RPC/RDMA header, as code | |
331 | * here references the header fields by array offset. Also, | |
332 | * backward calls are always inline, so ensure there | |
333 | * are some bytes beyond the RPC/RDMA header. | |
334 | */ | |
335 | if (rep->rr_len < RPCRDMA_HDRLEN_MIN + 24) | |
336 | goto out_short; | |
337 | p = (__be32 *)((unsigned char *)headerp + RPCRDMA_HDRLEN_MIN); | |
338 | size = rep->rr_len - RPCRDMA_HDRLEN_MIN; | |
339 | ||
340 | /* Grab a free bc rqst */ | |
341 | spin_lock(&xprt->bc_pa_lock); | |
342 | if (list_empty(&xprt->bc_pa_list)) { | |
343 | spin_unlock(&xprt->bc_pa_lock); | |
344 | goto out_overflow; | |
345 | } | |
346 | rqst = list_first_entry(&xprt->bc_pa_list, | |
347 | struct rpc_rqst, rq_bc_pa_list); | |
348 | list_del(&rqst->rq_bc_pa_list); | |
349 | spin_unlock(&xprt->bc_pa_lock); | |
c8bbe0c7 | 350 | dprintk("RPC: %s: using rqst %p\n", __func__, rqst); |
63cae470 CL |
351 | |
352 | /* Prepare rqst */ | |
353 | rqst->rq_reply_bytes_recvd = 0; | |
354 | rqst->rq_bytes_sent = 0; | |
355 | rqst->rq_xid = headerp->rm_xid; | |
9f74660b CL |
356 | |
357 | rqst->rq_private_buf.len = size; | |
63cae470 CL |
358 | set_bit(RPC_BC_PA_IN_USE, &rqst->rq_bc_pa_state); |
359 | ||
360 | buf = &rqst->rq_rcv_buf; | |
361 | memset(buf, 0, sizeof(*buf)); | |
362 | buf->head[0].iov_base = p; | |
363 | buf->head[0].iov_len = size; | |
364 | buf->len = size; | |
365 | ||
366 | /* The receive buffer has to be hooked to the rpcrdma_req | |
367 | * so that it can be reposted after the server is done | |
368 | * parsing it but just before sending the backward | |
369 | * direction reply. | |
370 | */ | |
371 | req = rpcr_to_rdmar(rqst); | |
c8bbe0c7 | 372 | dprintk("RPC: %s: attaching rep %p to req %p\n", |
63cae470 | 373 | __func__, rep, req); |
63cae470 CL |
374 | req->rl_reply = rep; |
375 | ||
376 | /* Defeat the retransmit detection logic in send_request */ | |
377 | req->rl_connect_cookie = 0; | |
378 | ||
379 | /* Queue rqst for ULP's callback service */ | |
380 | bc_serv = xprt->bc_serv; | |
381 | spin_lock(&bc_serv->sv_cb_lock); | |
382 | list_add(&rqst->rq_bc_list, &bc_serv->sv_cb_list); | |
383 | spin_unlock(&bc_serv->sv_cb_lock); | |
384 | ||
385 | wake_up(&bc_serv->sv_cb_waitq); | |
386 | ||
387 | r_xprt->rx_stats.bcall_count++; | |
388 | return; | |
389 | ||
390 | out_overflow: | |
391 | pr_warn("RPC/RDMA backchannel overflow\n"); | |
392 | xprt_disconnect_done(xprt); | |
393 | /* This receive buffer gets reposted automatically | |
394 | * when the connection is re-established. | |
395 | */ | |
396 | return; | |
397 | ||
398 | out_short: | |
399 | pr_warn("RPC/RDMA short backward direction call\n"); | |
400 | ||
b157380a | 401 | if (rpcrdma_ep_post_recv(&r_xprt->rx_ia, rep)) |
63cae470 CL |
402 | xprt_disconnect_done(xprt); |
403 | else | |
404 | pr_warn("RPC: %s: reposting rep %p\n", | |
405 | __func__, rep); | |
406 | } |