1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/9p/trans_rdma.c
5 * RDMA transport layer based on the trans_fd.c implementation.
7 * Copyright (C) 2008 by Tom Tucker <tom@opengridcomputing.com>
8 * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
9 * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
10 * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
11 * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/module.h>
18 #include <linux/net.h>
19 #include <linux/ipv6.h>
20 #include <linux/kthread.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
24 #include <linux/uaccess.h>
25 #include <linux/inet.h>
26 #include <linux/idr.h>
27 #include <linux/file.h>
28 #include <linux/parser.h>
29 #include <linux/semaphore.h>
30 #include <linux/slab.h>
31 #include <linux/seq_file.h>
32 #include <net/9p/9p.h>
33 #include <net/9p/client.h>
34 #include <net/9p/transport.h>
35 #include <rdma/ib_verbs.h>
36 #include <rdma/rdma_cm.h>
39 #define P9_RDMA_SQ_DEPTH 32
40 #define P9_RDMA_RQ_DEPTH 32
41 #define P9_RDMA_SEND_SGE 4
42 #define P9_RDMA_RECV_SGE 4
45 #define P9_RDMA_TIMEOUT 30000 /* 30 seconds */
46 #define P9_RDMA_MAXSIZE (1024*1024) /* 1MB */
49 * struct p9_trans_rdma - RDMA transport instance
51 * @state: tracks the transport state machine for connection setup and tear down
52 * @cm_id: The RDMA CM ID
53 * @pd: Protection Domain pointer
54 * @qp: Queue Pair pointer
55 * @cq: Completion Queue pointer
56 * @timeout: Number of uSecs to wait for connection management events
57 * @privport: Whether a privileged port may be used
58 * @port: The port to use
59 * @sq_depth: The depth of the Send Queue
60 * @sq_sem: Semaphore for the SQ
61 * @rq_depth: The depth of the Receive Queue.
62 * @rq_sem: Semaphore for the RQ
63 * @excess_rc : Amount of posted Receive Contexts without a pending request.
65 * @addr: The remote peer's address
66 * @req_lock: Protects the active request list
67 * @cm_done: Completion event for connection management tracking
69 struct p9_trans_rdma {
72 P9_RDMA_ADDR_RESOLVED,
73 P9_RDMA_ROUTE_RESOLVED,
79 struct rdma_cm_id *cm_id;
87 struct semaphore sq_sem;
89 struct semaphore rq_sem;
91 struct sockaddr_in addr;
94 struct completion cm_done;
100 * struct p9_rdma_context - Keeps track of in-process WR
102 * @busa: Bus address to unmap when the WR completes
103 * @req: Keeps track of requests (send)
104 * @rc: Keepts track of replies (receive)
106 struct p9_rdma_context {
110 struct p9_req_t *req;
116 * struct p9_rdma_opts - Collection of mount options
117 * @port: port of connection
118 * @sq_depth: The requested depth of the SQ. This really doesn't need
119 * to be any deeper than the number of threads used in the client
120 * @rq_depth: The depth of the RQ. Should be greater than or equal to SQ depth
121 * @timeout: Time to wait in msecs for CM events
123 struct p9_rdma_opts {
132 * Option Parsing (code inspired by NFS code)
135 /* Options that take integer arguments */
136 Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout,
137 /* Options that take no argument */
142 static match_table_t tokens = {
143 {Opt_port, "port=%u"},
144 {Opt_sq_depth, "sq=%u"},
145 {Opt_rq_depth, "rq=%u"},
146 {Opt_timeout, "timeout=%u"},
147 {Opt_privport, "privport"},
151 static int p9_rdma_show_options(struct seq_file *m, struct p9_client *clnt)
153 struct p9_trans_rdma *rdma = clnt->trans;
155 if (rdma->port != P9_PORT)
156 seq_printf(m, ",port=%u", rdma->port);
157 if (rdma->sq_depth != P9_RDMA_SQ_DEPTH)
158 seq_printf(m, ",sq=%u", rdma->sq_depth);
159 if (rdma->rq_depth != P9_RDMA_RQ_DEPTH)
160 seq_printf(m, ",rq=%u", rdma->rq_depth);
161 if (rdma->timeout != P9_RDMA_TIMEOUT)
162 seq_printf(m, ",timeout=%lu", rdma->timeout);
164 seq_puts(m, ",privport");
169 * parse_opts - parse mount options into rdma options structure
170 * @params: options string passed from mount
171 * @opts: rdma transport-specific structure to parse options into
173 * Returns 0 upon success, -ERRNO upon failure
175 static int parse_opts(char *params, struct p9_rdma_opts *opts)
178 substring_t args[MAX_OPT_ARGS];
180 char *options, *tmp_options;
182 opts->port = P9_PORT;
183 opts->sq_depth = P9_RDMA_SQ_DEPTH;
184 opts->rq_depth = P9_RDMA_RQ_DEPTH;
185 opts->timeout = P9_RDMA_TIMEOUT;
186 opts->privport = false;
191 tmp_options = kstrdup(params, GFP_KERNEL);
193 p9_debug(P9_DEBUG_ERROR,
194 "failed to allocate copy of option string\n");
197 options = tmp_options;
199 while ((p = strsep(&options, ",")) != NULL) {
204 token = match_token(p, tokens, args);
205 if ((token != Opt_err) && (token != Opt_privport)) {
206 r = match_int(&args[0], &option);
208 p9_debug(P9_DEBUG_ERROR,
209 "integer field, but no integer?\n");
218 opts->sq_depth = option;
221 opts->rq_depth = option;
224 opts->timeout = option;
227 opts->privport = true;
233 /* RQ must be at least as large as the SQ */
234 opts->rq_depth = max(opts->rq_depth, opts->sq_depth);
240 p9_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
242 struct p9_client *c = id->context;
243 struct p9_trans_rdma *rdma = c->trans;
244 switch (event->event) {
245 case RDMA_CM_EVENT_ADDR_RESOLVED:
246 BUG_ON(rdma->state != P9_RDMA_INIT);
247 rdma->state = P9_RDMA_ADDR_RESOLVED;
250 case RDMA_CM_EVENT_ROUTE_RESOLVED:
251 BUG_ON(rdma->state != P9_RDMA_ADDR_RESOLVED);
252 rdma->state = P9_RDMA_ROUTE_RESOLVED;
255 case RDMA_CM_EVENT_ESTABLISHED:
256 BUG_ON(rdma->state != P9_RDMA_ROUTE_RESOLVED);
257 rdma->state = P9_RDMA_CONNECTED;
260 case RDMA_CM_EVENT_DISCONNECTED:
262 rdma->state = P9_RDMA_CLOSED;
263 c->status = Disconnected;
266 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
269 case RDMA_CM_EVENT_ADDR_CHANGE:
270 case RDMA_CM_EVENT_ROUTE_ERROR:
271 case RDMA_CM_EVENT_DEVICE_REMOVAL:
272 case RDMA_CM_EVENT_MULTICAST_JOIN:
273 case RDMA_CM_EVENT_MULTICAST_ERROR:
274 case RDMA_CM_EVENT_REJECTED:
275 case RDMA_CM_EVENT_CONNECT_REQUEST:
276 case RDMA_CM_EVENT_CONNECT_RESPONSE:
277 case RDMA_CM_EVENT_CONNECT_ERROR:
278 case RDMA_CM_EVENT_ADDR_ERROR:
279 case RDMA_CM_EVENT_UNREACHABLE:
280 c->status = Disconnected;
281 rdma_disconnect(rdma->cm_id);
286 complete(&rdma->cm_done);
291 recv_done(struct ib_cq *cq, struct ib_wc *wc)
293 struct p9_client *client = cq->cq_context;
294 struct p9_trans_rdma *rdma = client->trans;
295 struct p9_rdma_context *c =
296 container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
297 struct p9_req_t *req;
302 ib_dma_unmap_single(rdma->cm_id->device, c->busa, client->msize,
305 if (wc->status != IB_WC_SUCCESS)
308 c->rc.size = wc->byte_len;
309 err = p9_parse_header(&c->rc, NULL, NULL, &tag, 1);
313 req = p9_tag_lookup(client, tag);
317 /* Check that we have not yet received a reply for this request.
319 if (unlikely(req->rc.sdata)) {
320 pr_err("Duplicate reply for request %d", tag);
324 req->rc.size = c->rc.size;
325 req->rc.sdata = c->rc.sdata;
326 p9_client_cb(client, req, REQ_STATUS_RCVD);
334 p9_debug(P9_DEBUG_ERROR, "req %p err %d status %d\n",
335 req, err, wc->status);
336 rdma->state = P9_RDMA_FLUSHING;
337 client->status = Disconnected;
342 send_done(struct ib_cq *cq, struct ib_wc *wc)
344 struct p9_client *client = cq->cq_context;
345 struct p9_trans_rdma *rdma = client->trans;
346 struct p9_rdma_context *c =
347 container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
349 ib_dma_unmap_single(rdma->cm_id->device,
350 c->busa, c->req->tc.size,
357 static void qp_event_handler(struct ib_event *event, void *context)
359 p9_debug(P9_DEBUG_ERROR, "QP event %d context %p\n",
360 event->event, context);
363 static void rdma_destroy_trans(struct p9_trans_rdma *rdma)
368 if (rdma->qp && !IS_ERR(rdma->qp))
369 ib_destroy_qp(rdma->qp);
371 if (rdma->pd && !IS_ERR(rdma->pd))
372 ib_dealloc_pd(rdma->pd);
374 if (rdma->cq && !IS_ERR(rdma->cq))
375 ib_free_cq(rdma->cq);
377 if (rdma->cm_id && !IS_ERR(rdma->cm_id))
378 rdma_destroy_id(rdma->cm_id);
384 post_recv(struct p9_client *client, struct p9_rdma_context *c)
386 struct p9_trans_rdma *rdma = client->trans;
387 struct ib_recv_wr wr;
390 c->busa = ib_dma_map_single(rdma->cm_id->device,
391 c->rc.sdata, client->msize,
393 if (ib_dma_mapping_error(rdma->cm_id->device, c->busa))
396 c->cqe.done = recv_done;
399 sge.length = client->msize;
400 sge.lkey = rdma->pd->local_dma_lkey;
406 return ib_post_recv(rdma->qp, &wr, NULL);
409 p9_debug(P9_DEBUG_ERROR, "EIO\n");
413 static int rdma_request(struct p9_client *client, struct p9_req_t *req)
415 struct p9_trans_rdma *rdma = client->trans;
416 struct ib_send_wr wr;
420 struct p9_rdma_context *c = NULL;
421 struct p9_rdma_context *rpl_context = NULL;
423 /* When an error occurs between posting the recv and the send,
424 * there will be a receive context posted without a pending request.
425 * Since there is no way to "un-post" it, we remember it and skip
426 * post_recv() for the next request.
428 * see if we are this `next request' and need to absorb an excess rc.
429 * If yes, then drop and free our own, and do not recv_post().
431 if (unlikely(atomic_read(&rdma->excess_rc) > 0)) {
432 if ((atomic_sub_return(1, &rdma->excess_rc) >= 0)) {
434 p9_fcall_fini(&req->rc);
435 req->rc.sdata = NULL;
436 goto dont_need_post_recv;
438 /* We raced and lost. */
439 atomic_inc(&rdma->excess_rc);
443 /* Allocate an fcall for the reply */
444 rpl_context = kmalloc(sizeof *rpl_context, GFP_NOFS);
449 rpl_context->rc.sdata = req->rc.sdata;
452 * Post a receive buffer for this request. We need to ensure
453 * there is a reply buffer available for every outstanding
454 * request. A flushed request can result in no reply for an
455 * outstanding request, so we must keep a count to avoid
456 * overflowing the RQ.
458 if (down_interruptible(&rdma->rq_sem)) {
463 err = post_recv(client, rpl_context);
465 p9_debug(P9_DEBUG_ERROR, "POST RECV failed: %d\n", err);
468 /* remove posted receive buffer from request structure */
469 req->rc.sdata = NULL;
472 /* Post the request */
473 c = kmalloc(sizeof *c, GFP_NOFS);
480 c->busa = ib_dma_map_single(rdma->cm_id->device,
481 c->req->tc.sdata, c->req->tc.size,
483 if (ib_dma_mapping_error(rdma->cm_id->device, c->busa)) {
488 c->cqe.done = send_done;
491 sge.length = c->req->tc.size;
492 sge.lkey = rdma->pd->local_dma_lkey;
496 wr.opcode = IB_WR_SEND;
497 wr.send_flags = IB_SEND_SIGNALED;
501 if (down_interruptible(&rdma->sq_sem)) {
506 /* Mark request as `sent' *before* we actually send it,
507 * because doing if after could erase the REQ_STATUS_RCVD
508 * status in case of a very fast reply.
510 req->status = REQ_STATUS_SENT;
511 err = ib_post_send(rdma->qp, &wr, NULL);
518 /* Handle errors that happened during or while preparing the send: */
520 req->status = REQ_STATUS_ERROR;
522 p9_debug(P9_DEBUG_ERROR, "Error %d in rdma_request()\n", err);
525 * We did recv_post(), but not send. We have one recv_post in excess.
527 atomic_inc(&rdma->excess_rc);
530 /* Handle errors that happened during or while preparing post_recv(): */
533 spin_lock_irqsave(&rdma->req_lock, flags);
534 if (err != -EINTR && rdma->state < P9_RDMA_CLOSING) {
535 rdma->state = P9_RDMA_CLOSING;
536 spin_unlock_irqrestore(&rdma->req_lock, flags);
537 rdma_disconnect(rdma->cm_id);
539 spin_unlock_irqrestore(&rdma->req_lock, flags);
543 static void rdma_close(struct p9_client *client)
545 struct p9_trans_rdma *rdma;
550 rdma = client->trans;
554 client->status = Disconnected;
555 rdma_disconnect(rdma->cm_id);
556 rdma_destroy_trans(rdma);
560 * alloc_rdma - Allocate and initialize the rdma transport structure
561 * @opts: Mount options structure
563 static struct p9_trans_rdma *alloc_rdma(struct p9_rdma_opts *opts)
565 struct p9_trans_rdma *rdma;
567 rdma = kzalloc(sizeof(struct p9_trans_rdma), GFP_KERNEL);
571 rdma->port = opts->port;
572 rdma->privport = opts->privport;
573 rdma->sq_depth = opts->sq_depth;
574 rdma->rq_depth = opts->rq_depth;
575 rdma->timeout = opts->timeout;
576 spin_lock_init(&rdma->req_lock);
577 init_completion(&rdma->cm_done);
578 sema_init(&rdma->sq_sem, rdma->sq_depth);
579 sema_init(&rdma->rq_sem, rdma->rq_depth);
580 atomic_set(&rdma->excess_rc, 0);
585 static int rdma_cancel(struct p9_client *client, struct p9_req_t *req)
587 /* Nothing to do here.
588 * We will take care of it (if we have to) in rdma_cancelled()
593 /* A request has been fully flushed without a reply.
594 * That means we have posted one buffer in excess.
596 static int rdma_cancelled(struct p9_client *client, struct p9_req_t *req)
598 struct p9_trans_rdma *rdma = client->trans;
599 atomic_inc(&rdma->excess_rc);
603 static int p9_rdma_bind_privport(struct p9_trans_rdma *rdma)
605 struct sockaddr_in cl = {
606 .sin_family = AF_INET,
607 .sin_addr.s_addr = htonl(INADDR_ANY),
609 int port, err = -EINVAL;
611 for (port = P9_DEF_MAX_RESVPORT; port >= P9_DEF_MIN_RESVPORT; port--) {
612 cl.sin_port = htons((ushort)port);
613 err = rdma_bind_addr(rdma->cm_id, (struct sockaddr *)&cl);
614 if (err != -EADDRINUSE)
621 * rdma_create_trans - Transport method for creating a transport instance
622 * @client: client instance
623 * @addr: IP address string
624 * @args: Mount options string
627 rdma_create_trans(struct p9_client *client, const char *addr, char *args)
630 struct p9_rdma_opts opts;
631 struct p9_trans_rdma *rdma;
632 struct rdma_conn_param conn_param;
633 struct ib_qp_init_attr qp_attr;
638 /* Parse the transport specific mount options */
639 err = parse_opts(args, &opts);
643 /* Create and initialize the RDMA transport structure */
644 rdma = alloc_rdma(&opts);
648 /* Create the RDMA CM ID */
649 rdma->cm_id = rdma_create_id(&init_net, p9_cm_event_handler, client,
650 RDMA_PS_TCP, IB_QPT_RC);
651 if (IS_ERR(rdma->cm_id))
654 /* Associate the client with the transport */
655 client->trans = rdma;
657 /* Bind to a privileged port if we need to */
659 err = p9_rdma_bind_privport(rdma);
661 pr_err("%s (%d): problem binding to privport: %d\n",
662 __func__, task_pid_nr(current), -err);
667 /* Resolve the server's address */
668 rdma->addr.sin_family = AF_INET;
669 rdma->addr.sin_addr.s_addr = in_aton(addr);
670 rdma->addr.sin_port = htons(opts.port);
671 err = rdma_resolve_addr(rdma->cm_id, NULL,
672 (struct sockaddr *)&rdma->addr,
676 err = wait_for_completion_interruptible(&rdma->cm_done);
677 if (err || (rdma->state != P9_RDMA_ADDR_RESOLVED))
680 /* Resolve the route to the server */
681 err = rdma_resolve_route(rdma->cm_id, rdma->timeout);
684 err = wait_for_completion_interruptible(&rdma->cm_done);
685 if (err || (rdma->state != P9_RDMA_ROUTE_RESOLVED))
688 /* Create the Completion Queue */
689 rdma->cq = ib_alloc_cq_any(rdma->cm_id->device, client,
690 opts.sq_depth + opts.rq_depth + 1,
692 if (IS_ERR(rdma->cq))
695 /* Create the Protection Domain */
696 rdma->pd = ib_alloc_pd(rdma->cm_id->device, 0);
697 if (IS_ERR(rdma->pd))
700 /* Create the Queue Pair */
701 memset(&qp_attr, 0, sizeof qp_attr);
702 qp_attr.event_handler = qp_event_handler;
703 qp_attr.qp_context = client;
704 qp_attr.cap.max_send_wr = opts.sq_depth;
705 qp_attr.cap.max_recv_wr = opts.rq_depth;
706 qp_attr.cap.max_send_sge = P9_RDMA_SEND_SGE;
707 qp_attr.cap.max_recv_sge = P9_RDMA_RECV_SGE;
708 qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
709 qp_attr.qp_type = IB_QPT_RC;
710 qp_attr.send_cq = rdma->cq;
711 qp_attr.recv_cq = rdma->cq;
712 err = rdma_create_qp(rdma->cm_id, rdma->pd, &qp_attr);
715 rdma->qp = rdma->cm_id->qp;
717 /* Request a connection */
718 memset(&conn_param, 0, sizeof(conn_param));
719 conn_param.private_data = NULL;
720 conn_param.private_data_len = 0;
721 conn_param.responder_resources = P9_RDMA_IRD;
722 conn_param.initiator_depth = P9_RDMA_ORD;
723 err = rdma_connect(rdma->cm_id, &conn_param);
726 err = wait_for_completion_interruptible(&rdma->cm_done);
727 if (err || (rdma->state != P9_RDMA_CONNECTED))
730 client->status = Connected;
735 rdma_destroy_trans(rdma);
739 static struct p9_trans_module p9_rdma_trans = {
741 .maxsize = P9_RDMA_MAXSIZE,
743 .owner = THIS_MODULE,
744 .create = rdma_create_trans,
746 .request = rdma_request,
747 .cancel = rdma_cancel,
748 .cancelled = rdma_cancelled,
749 .show_options = p9_rdma_show_options,
753 * p9_trans_rdma_init - Register the 9P RDMA transport driver
755 static int __init p9_trans_rdma_init(void)
757 v9fs_register_trans(&p9_rdma_trans);
761 static void __exit p9_trans_rdma_exit(void)
763 v9fs_unregister_trans(&p9_rdma_trans);
766 module_init(p9_trans_rdma_init);
767 module_exit(p9_trans_rdma_exit);
769 MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>");
770 MODULE_DESCRIPTION("RDMA Transport for 9P");
771 MODULE_LICENSE("Dual BSD/GPL");