trans_rdma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * RDMA transport layer based on the trans_fd.c implementation.
  4. *
  5. * Copyright (C) 2008 by Tom Tucker <tom@opengridcomputing.com>
  6. * Copyright (C) 2006 by Russ Cox <rsc@swtch.com>
  7. * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net>
  8. * Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
  9. * Copyright (C) 1997-2002 by Ron Minnich <rminnich@sarnoff.com>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/in.h>
  13. #include <linux/module.h>
  14. #include <linux/net.h>
  15. #include <linux/ipv6.h>
  16. #include <linux/kthread.h>
  17. #include <linux/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/un.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/inet.h>
  22. #include <linux/file.h>
  23. #include <linux/parser.h>
  24. #include <linux/semaphore.h>
  25. #include <linux/slab.h>
  26. #include <linux/seq_file.h>
  27. #include <net/9p/9p.h>
  28. #include <net/9p/client.h>
  29. #include <net/9p/transport.h>
  30. #include <rdma/ib_verbs.h>
  31. #include <rdma/rdma_cm.h>
  32. #define P9_PORT 5640
  33. #define P9_RDMA_SQ_DEPTH 32
  34. #define P9_RDMA_RQ_DEPTH 32
  35. #define P9_RDMA_SEND_SGE 4
  36. #define P9_RDMA_RECV_SGE 4
  37. #define P9_RDMA_IRD 0
  38. #define P9_RDMA_ORD 0
  39. #define P9_RDMA_TIMEOUT 30000 /* 30 seconds */
  40. #define P9_RDMA_MAXSIZE (1024*1024) /* 1MB */
  41. /**
  42. * struct p9_trans_rdma - RDMA transport instance
  43. *
  44. * @state: tracks the transport state machine for connection setup and tear down
  45. * @cm_id: The RDMA CM ID
  46. * @pd: Protection Domain pointer
  47. * @qp: Queue Pair pointer
  48. * @cq: Completion Queue pointer
  49. * @timeout: Number of uSecs to wait for connection management events
  50. * @privport: Whether a privileged port may be used
  51. * @port: The port to use
  52. * @sq_depth: The depth of the Send Queue
  53. * @sq_sem: Semaphore for the SQ
  54. * @rq_depth: The depth of the Receive Queue.
  55. * @rq_sem: Semaphore for the RQ
  56. * @excess_rc : Amount of posted Receive Contexts without a pending request.
  57. * See rdma_request()
  58. * @addr: The remote peer's address
  59. * @req_lock: Protects the active request list
  60. * @cm_done: Completion event for connection management tracking
  61. */
  62. struct p9_trans_rdma {
  63. enum {
  64. P9_RDMA_INIT,
  65. P9_RDMA_ADDR_RESOLVED,
  66. P9_RDMA_ROUTE_RESOLVED,
  67. P9_RDMA_CONNECTED,
  68. P9_RDMA_FLUSHING,
  69. P9_RDMA_CLOSING,
  70. P9_RDMA_CLOSED,
  71. } state;
  72. struct rdma_cm_id *cm_id;
  73. struct ib_pd *pd;
  74. struct ib_qp *qp;
  75. struct ib_cq *cq;
  76. long timeout;
  77. bool privport;
  78. u16 port;
  79. int sq_depth;
  80. struct semaphore sq_sem;
  81. int rq_depth;
  82. struct semaphore rq_sem;
  83. atomic_t excess_rc;
  84. struct sockaddr_in addr;
  85. spinlock_t req_lock;
  86. struct completion cm_done;
  87. };
  88. struct p9_rdma_req;
  89. /**
  90. * struct p9_rdma_context - Keeps track of in-process WR
  91. *
  92. * @cqe: completion queue entry
  93. * @busa: Bus address to unmap when the WR completes
  94. * @req: Keeps track of requests (send)
  95. * @rc: Keepts track of replies (receive)
  96. */
  97. struct p9_rdma_context {
  98. struct ib_cqe cqe;
  99. dma_addr_t busa;
  100. union {
  101. struct p9_req_t *req;
  102. struct p9_fcall rc;
  103. };
  104. };
  105. /**
  106. * struct p9_rdma_opts - Collection of mount options
  107. * @port: port of connection
  108. * @privport: Whether a privileged port may be used
  109. * @sq_depth: The requested depth of the SQ. This really doesn't need
  110. * to be any deeper than the number of threads used in the client
  111. * @rq_depth: The depth of the RQ. Should be greater than or equal to SQ depth
  112. * @timeout: Time to wait in msecs for CM events
  113. */
  114. struct p9_rdma_opts {
  115. short port;
  116. bool privport;
  117. int sq_depth;
  118. int rq_depth;
  119. long timeout;
  120. };
  121. /*
  122. * Option Parsing (code inspired by NFS code)
  123. */
  124. enum {
  125. /* Options that take integer arguments */
  126. Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout,
  127. /* Options that take no argument */
  128. Opt_privport,
  129. Opt_err,
  130. };
  131. static match_table_t tokens = {
  132. {Opt_port, "port=%u"},
  133. {Opt_sq_depth, "sq=%u"},
  134. {Opt_rq_depth, "rq=%u"},
  135. {Opt_timeout, "timeout=%u"},
  136. {Opt_privport, "privport"},
  137. {Opt_err, NULL},
  138. };
  139. static int p9_rdma_show_options(struct seq_file *m, struct p9_client *clnt)
  140. {
  141. struct p9_trans_rdma *rdma = clnt->trans;
  142. if (rdma->port != P9_PORT)
  143. seq_printf(m, ",port=%u", rdma->port);
  144. if (rdma->sq_depth != P9_RDMA_SQ_DEPTH)
  145. seq_printf(m, ",sq=%u", rdma->sq_depth);
  146. if (rdma->rq_depth != P9_RDMA_RQ_DEPTH)
  147. seq_printf(m, ",rq=%u", rdma->rq_depth);
  148. if (rdma->timeout != P9_RDMA_TIMEOUT)
  149. seq_printf(m, ",timeout=%lu", rdma->timeout);
  150. if (rdma->privport)
  151. seq_puts(m, ",privport");
  152. return 0;
  153. }
  154. /**
  155. * parse_opts - parse mount options into rdma options structure
  156. * @params: options string passed from mount
  157. * @opts: rdma transport-specific structure to parse options into
  158. *
  159. * Returns 0 upon success, -ERRNO upon failure
  160. */
  161. static int parse_opts(char *params, struct p9_rdma_opts *opts)
  162. {
  163. char *p;
  164. substring_t args[MAX_OPT_ARGS];
  165. int option;
  166. char *options, *tmp_options;
  167. opts->port = P9_PORT;
  168. opts->sq_depth = P9_RDMA_SQ_DEPTH;
  169. opts->rq_depth = P9_RDMA_RQ_DEPTH;
  170. opts->timeout = P9_RDMA_TIMEOUT;
  171. opts->privport = false;
  172. if (!params)
  173. return 0;
  174. tmp_options = kstrdup(params, GFP_KERNEL);
  175. if (!tmp_options) {
  176. p9_debug(P9_DEBUG_ERROR,
  177. "failed to allocate copy of option string\n");
  178. return -ENOMEM;
  179. }
  180. options = tmp_options;
  181. while ((p = strsep(&options, ",")) != NULL) {
  182. int token;
  183. int r;
  184. if (!*p)
  185. continue;
  186. token = match_token(p, tokens, args);
  187. if ((token != Opt_err) && (token != Opt_privport)) {
  188. r = match_int(&args[0], &option);
  189. if (r < 0) {
  190. p9_debug(P9_DEBUG_ERROR,
  191. "integer field, but no integer?\n");
  192. continue;
  193. }
  194. }
  195. switch (token) {
  196. case Opt_port:
  197. opts->port = option;
  198. break;
  199. case Opt_sq_depth:
  200. opts->sq_depth = option;
  201. break;
  202. case Opt_rq_depth:
  203. opts->rq_depth = option;
  204. break;
  205. case Opt_timeout:
  206. opts->timeout = option;
  207. break;
  208. case Opt_privport:
  209. opts->privport = true;
  210. break;
  211. default:
  212. continue;
  213. }
  214. }
  215. /* RQ must be at least as large as the SQ */
  216. opts->rq_depth = max(opts->rq_depth, opts->sq_depth);
  217. kfree(tmp_options);
  218. return 0;
  219. }
  220. static int
  221. p9_cm_event_handler(struct rdma_cm_id *id, struct rdma_cm_event *event)
  222. {
  223. struct p9_client *c = id->context;
  224. struct p9_trans_rdma *rdma = c->trans;
  225. switch (event->event) {
  226. case RDMA_CM_EVENT_ADDR_RESOLVED:
  227. BUG_ON(rdma->state != P9_RDMA_INIT);
  228. rdma->state = P9_RDMA_ADDR_RESOLVED;
  229. break;
  230. case RDMA_CM_EVENT_ROUTE_RESOLVED:
  231. BUG_ON(rdma->state != P9_RDMA_ADDR_RESOLVED);
  232. rdma->state = P9_RDMA_ROUTE_RESOLVED;
  233. break;
  234. case RDMA_CM_EVENT_ESTABLISHED:
  235. BUG_ON(rdma->state != P9_RDMA_ROUTE_RESOLVED);
  236. rdma->state = P9_RDMA_CONNECTED;
  237. break;
  238. case RDMA_CM_EVENT_DISCONNECTED:
  239. if (rdma)
  240. rdma->state = P9_RDMA_CLOSED;
  241. c->status = Disconnected;
  242. break;
  243. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  244. break;
  245. case RDMA_CM_EVENT_ADDR_CHANGE:
  246. case RDMA_CM_EVENT_ROUTE_ERROR:
  247. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  248. case RDMA_CM_EVENT_MULTICAST_JOIN:
  249. case RDMA_CM_EVENT_MULTICAST_ERROR:
  250. case RDMA_CM_EVENT_REJECTED:
  251. case RDMA_CM_EVENT_CONNECT_REQUEST:
  252. case RDMA_CM_EVENT_CONNECT_RESPONSE:
  253. case RDMA_CM_EVENT_CONNECT_ERROR:
  254. case RDMA_CM_EVENT_ADDR_ERROR:
  255. case RDMA_CM_EVENT_UNREACHABLE:
  256. c->status = Disconnected;
  257. rdma_disconnect(rdma->cm_id);
  258. break;
  259. default:
  260. BUG();
  261. }
  262. complete(&rdma->cm_done);
  263. return 0;
  264. }
  265. static void
  266. recv_done(struct ib_cq *cq, struct ib_wc *wc)
  267. {
  268. struct p9_client *client = cq->cq_context;
  269. struct p9_trans_rdma *rdma = client->trans;
  270. struct p9_rdma_context *c =
  271. container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
  272. struct p9_req_t *req;
  273. int err = 0;
  274. int16_t tag;
  275. req = NULL;
  276. ib_dma_unmap_single(rdma->cm_id->device, c->busa, client->msize,
  277. DMA_FROM_DEVICE);
  278. if (wc->status != IB_WC_SUCCESS)
  279. goto err_out;
  280. c->rc.size = wc->byte_len;
  281. err = p9_parse_header(&c->rc, NULL, NULL, &tag, 1);
  282. if (err)
  283. goto err_out;
  284. req = p9_tag_lookup(client, tag);
  285. if (!req)
  286. goto err_out;
  287. /* Check that we have not yet received a reply for this request.
  288. */
  289. if (unlikely(req->rc.sdata)) {
  290. pr_err("Duplicate reply for request %d", tag);
  291. goto err_out;
  292. }
  293. req->rc.size = c->rc.size;
  294. req->rc.sdata = c->rc.sdata;
  295. p9_client_cb(client, req, REQ_STATUS_RCVD);
  296. out:
  297. up(&rdma->rq_sem);
  298. kfree(c);
  299. return;
  300. err_out:
  301. p9_debug(P9_DEBUG_ERROR, "req %p err %d status %d\n",
  302. req, err, wc->status);
  303. rdma->state = P9_RDMA_FLUSHING;
  304. client->status = Disconnected;
  305. goto out;
  306. }
  307. static void
  308. send_done(struct ib_cq *cq, struct ib_wc *wc)
  309. {
  310. struct p9_client *client = cq->cq_context;
  311. struct p9_trans_rdma *rdma = client->trans;
  312. struct p9_rdma_context *c =
  313. container_of(wc->wr_cqe, struct p9_rdma_context, cqe);
  314. ib_dma_unmap_single(rdma->cm_id->device,
  315. c->busa, c->req->tc.size,
  316. DMA_TO_DEVICE);
  317. up(&rdma->sq_sem);
  318. p9_req_put(client, c->req);
  319. kfree(c);
  320. }
  321. static void qp_event_handler(struct ib_event *event, void *context)
  322. {
  323. p9_debug(P9_DEBUG_ERROR, "QP event %d context %p\n",
  324. event->event, context);
  325. }
  326. static void rdma_destroy_trans(struct p9_trans_rdma *rdma)
  327. {
  328. if (!rdma)
  329. return;
  330. if (rdma->qp && !IS_ERR(rdma->qp))
  331. ib_destroy_qp(rdma->qp);
  332. if (rdma->pd && !IS_ERR(rdma->pd))
  333. ib_dealloc_pd(rdma->pd);
  334. if (rdma->cq && !IS_ERR(rdma->cq))
  335. ib_free_cq(rdma->cq);
  336. if (rdma->cm_id && !IS_ERR(rdma->cm_id))
  337. rdma_destroy_id(rdma->cm_id);
  338. kfree(rdma);
  339. }
  340. static int
  341. post_recv(struct p9_client *client, struct p9_rdma_context *c)
  342. {
  343. struct p9_trans_rdma *rdma = client->trans;
  344. struct ib_recv_wr wr;
  345. struct ib_sge sge;
  346. int ret;
  347. c->busa = ib_dma_map_single(rdma->cm_id->device,
  348. c->rc.sdata, client->msize,
  349. DMA_FROM_DEVICE);
  350. if (ib_dma_mapping_error(rdma->cm_id->device, c->busa))
  351. goto error;
  352. c->cqe.done = recv_done;
  353. sge.addr = c->busa;
  354. sge.length = client->msize;
  355. sge.lkey = rdma->pd->local_dma_lkey;
  356. wr.next = NULL;
  357. wr.wr_cqe = &c->cqe;
  358. wr.sg_list = &sge;
  359. wr.num_sge = 1;
  360. ret = ib_post_recv(rdma->qp, &wr, NULL);
  361. if (ret)
  362. ib_dma_unmap_single(rdma->cm_id->device, c->busa,
  363. client->msize, DMA_FROM_DEVICE);
  364. return ret;
  365. error:
  366. p9_debug(P9_DEBUG_ERROR, "EIO\n");
  367. return -EIO;
  368. }
  369. static int rdma_request(struct p9_client *client, struct p9_req_t *req)
  370. {
  371. struct p9_trans_rdma *rdma = client->trans;
  372. struct ib_send_wr wr;
  373. struct ib_sge sge;
  374. int err = 0;
  375. unsigned long flags;
  376. struct p9_rdma_context *c = NULL;
  377. struct p9_rdma_context *rpl_context = NULL;
  378. /* When an error occurs between posting the recv and the send,
  379. * there will be a receive context posted without a pending request.
  380. * Since there is no way to "un-post" it, we remember it and skip
  381. * post_recv() for the next request.
  382. * So here,
  383. * see if we are this `next request' and need to absorb an excess rc.
  384. * If yes, then drop and free our own, and do not recv_post().
  385. **/
  386. if (unlikely(atomic_read(&rdma->excess_rc) > 0)) {
  387. if ((atomic_sub_return(1, &rdma->excess_rc) >= 0)) {
  388. /* Got one! */
  389. p9_fcall_fini(&req->rc);
  390. req->rc.sdata = NULL;
  391. goto dont_need_post_recv;
  392. } else {
  393. /* We raced and lost. */
  394. atomic_inc(&rdma->excess_rc);
  395. }
  396. }
  397. /* Allocate an fcall for the reply */
  398. rpl_context = kmalloc(sizeof *rpl_context, GFP_NOFS);
  399. if (!rpl_context) {
  400. err = -ENOMEM;
  401. goto recv_error;
  402. }
  403. rpl_context->rc.sdata = req->rc.sdata;
  404. /*
  405. * Post a receive buffer for this request. We need to ensure
  406. * there is a reply buffer available for every outstanding
  407. * request. A flushed request can result in no reply for an
  408. * outstanding request, so we must keep a count to avoid
  409. * overflowing the RQ.
  410. */
  411. if (down_interruptible(&rdma->rq_sem)) {
  412. err = -EINTR;
  413. goto recv_error;
  414. }
  415. err = post_recv(client, rpl_context);
  416. if (err) {
  417. p9_debug(P9_DEBUG_ERROR, "POST RECV failed: %d\n", err);
  418. goto recv_error;
  419. }
  420. /* remove posted receive buffer from request structure */
  421. req->rc.sdata = NULL;
  422. dont_need_post_recv:
  423. /* Post the request */
  424. c = kmalloc(sizeof *c, GFP_NOFS);
  425. if (!c) {
  426. err = -ENOMEM;
  427. goto send_error;
  428. }
  429. c->req = req;
  430. c->busa = ib_dma_map_single(rdma->cm_id->device,
  431. c->req->tc.sdata, c->req->tc.size,
  432. DMA_TO_DEVICE);
  433. if (ib_dma_mapping_error(rdma->cm_id->device, c->busa)) {
  434. err = -EIO;
  435. goto send_error;
  436. }
  437. c->cqe.done = send_done;
  438. sge.addr = c->busa;
  439. sge.length = c->req->tc.size;
  440. sge.lkey = rdma->pd->local_dma_lkey;
  441. wr.next = NULL;
  442. wr.wr_cqe = &c->cqe;
  443. wr.opcode = IB_WR_SEND;
  444. wr.send_flags = IB_SEND_SIGNALED;
  445. wr.sg_list = &sge;
  446. wr.num_sge = 1;
  447. if (down_interruptible(&rdma->sq_sem)) {
  448. err = -EINTR;
  449. goto dma_unmap;
  450. }
  451. /* Mark request as `sent' *before* we actually send it,
  452. * because doing if after could erase the REQ_STATUS_RCVD
  453. * status in case of a very fast reply.
  454. */
  455. WRITE_ONCE(req->status, REQ_STATUS_SENT);
  456. err = ib_post_send(rdma->qp, &wr, NULL);
  457. if (err)
  458. goto dma_unmap;
  459. /* Success */
  460. return 0;
  461. dma_unmap:
  462. ib_dma_unmap_single(rdma->cm_id->device, c->busa,
  463. c->req->tc.size, DMA_TO_DEVICE);
  464. /* Handle errors that happened during or while preparing the send: */
  465. send_error:
  466. WRITE_ONCE(req->status, REQ_STATUS_ERROR);
  467. kfree(c);
  468. p9_debug(P9_DEBUG_ERROR, "Error %d in rdma_request()\n", err);
  469. /* Ach.
  470. * We did recv_post(), but not send. We have one recv_post in excess.
  471. */
  472. atomic_inc(&rdma->excess_rc);
  473. return err;
  474. /* Handle errors that happened during or while preparing post_recv(): */
  475. recv_error:
  476. kfree(rpl_context);
  477. spin_lock_irqsave(&rdma->req_lock, flags);
  478. if (err != -EINTR && rdma->state < P9_RDMA_CLOSING) {
  479. rdma->state = P9_RDMA_CLOSING;
  480. spin_unlock_irqrestore(&rdma->req_lock, flags);
  481. rdma_disconnect(rdma->cm_id);
  482. } else
  483. spin_unlock_irqrestore(&rdma->req_lock, flags);
  484. return err;
  485. }
  486. static void rdma_close(struct p9_client *client)
  487. {
  488. struct p9_trans_rdma *rdma;
  489. if (!client)
  490. return;
  491. rdma = client->trans;
  492. if (!rdma)
  493. return;
  494. client->status = Disconnected;
  495. rdma_disconnect(rdma->cm_id);
  496. rdma_destroy_trans(rdma);
  497. }
  498. /**
  499. * alloc_rdma - Allocate and initialize the rdma transport structure
  500. * @opts: Mount options structure
  501. */
  502. static struct p9_trans_rdma *alloc_rdma(struct p9_rdma_opts *opts)
  503. {
  504. struct p9_trans_rdma *rdma;
  505. rdma = kzalloc(sizeof(struct p9_trans_rdma), GFP_KERNEL);
  506. if (!rdma)
  507. return NULL;
  508. rdma->port = opts->port;
  509. rdma->privport = opts->privport;
  510. rdma->sq_depth = opts->sq_depth;
  511. rdma->rq_depth = opts->rq_depth;
  512. rdma->timeout = opts->timeout;
  513. spin_lock_init(&rdma->req_lock);
  514. init_completion(&rdma->cm_done);
  515. sema_init(&rdma->sq_sem, rdma->sq_depth);
  516. sema_init(&rdma->rq_sem, rdma->rq_depth);
  517. atomic_set(&rdma->excess_rc, 0);
  518. return rdma;
  519. }
  520. static int rdma_cancel(struct p9_client *client, struct p9_req_t *req)
  521. {
  522. /* Nothing to do here.
  523. * We will take care of it (if we have to) in rdma_cancelled()
  524. */
  525. return 1;
  526. }
  527. /* A request has been fully flushed without a reply.
  528. * That means we have posted one buffer in excess.
  529. */
  530. static int rdma_cancelled(struct p9_client *client, struct p9_req_t *req)
  531. {
  532. struct p9_trans_rdma *rdma = client->trans;
  533. atomic_inc(&rdma->excess_rc);
  534. return 0;
  535. }
  536. static int p9_rdma_bind_privport(struct p9_trans_rdma *rdma)
  537. {
  538. struct sockaddr_in cl = {
  539. .sin_family = AF_INET,
  540. .sin_addr.s_addr = htonl(INADDR_ANY),
  541. };
  542. int port, err = -EINVAL;
  543. for (port = P9_DEF_MAX_RESVPORT; port >= P9_DEF_MIN_RESVPORT; port--) {
  544. cl.sin_port = htons((ushort)port);
  545. err = rdma_bind_addr(rdma->cm_id, (struct sockaddr *)&cl);
  546. if (err != -EADDRINUSE)
  547. break;
  548. }
  549. return err;
  550. }
  551. /**
  552. * rdma_create_trans - Transport method for creating a transport instance
  553. * @client: client instance
  554. * @addr: IP address string
  555. * @args: Mount options string
  556. */
  557. static int
  558. rdma_create_trans(struct p9_client *client, const char *addr, char *args)
  559. {
  560. int err;
  561. struct p9_rdma_opts opts;
  562. struct p9_trans_rdma *rdma;
  563. struct rdma_conn_param conn_param;
  564. struct ib_qp_init_attr qp_attr;
  565. if (addr == NULL)
  566. return -EINVAL;
  567. /* Parse the transport specific mount options */
  568. err = parse_opts(args, &opts);
  569. if (err < 0)
  570. return err;
  571. /* Create and initialize the RDMA transport structure */
  572. rdma = alloc_rdma(&opts);
  573. if (!rdma)
  574. return -ENOMEM;
  575. /* Create the RDMA CM ID */
  576. rdma->cm_id = rdma_create_id(&init_net, p9_cm_event_handler, client,
  577. RDMA_PS_TCP, IB_QPT_RC);
  578. if (IS_ERR(rdma->cm_id))
  579. goto error;
  580. /* Associate the client with the transport */
  581. client->trans = rdma;
  582. /* Bind to a privileged port if we need to */
  583. if (opts.privport) {
  584. err = p9_rdma_bind_privport(rdma);
  585. if (err < 0) {
  586. pr_err("%s (%d): problem binding to privport: %d\n",
  587. __func__, task_pid_nr(current), -err);
  588. goto error;
  589. }
  590. }
  591. /* Resolve the server's address */
  592. rdma->addr.sin_family = AF_INET;
  593. rdma->addr.sin_addr.s_addr = in_aton(addr);
  594. rdma->addr.sin_port = htons(opts.port);
  595. err = rdma_resolve_addr(rdma->cm_id, NULL,
  596. (struct sockaddr *)&rdma->addr,
  597. rdma->timeout);
  598. if (err)
  599. goto error;
  600. err = wait_for_completion_interruptible(&rdma->cm_done);
  601. if (err || (rdma->state != P9_RDMA_ADDR_RESOLVED))
  602. goto error;
  603. /* Resolve the route to the server */
  604. err = rdma_resolve_route(rdma->cm_id, rdma->timeout);
  605. if (err)
  606. goto error;
  607. err = wait_for_completion_interruptible(&rdma->cm_done);
  608. if (err || (rdma->state != P9_RDMA_ROUTE_RESOLVED))
  609. goto error;
  610. /* Create the Completion Queue */
  611. rdma->cq = ib_alloc_cq_any(rdma->cm_id->device, client,
  612. opts.sq_depth + opts.rq_depth + 1,
  613. IB_POLL_SOFTIRQ);
  614. if (IS_ERR(rdma->cq))
  615. goto error;
  616. /* Create the Protection Domain */
  617. rdma->pd = ib_alloc_pd(rdma->cm_id->device, 0);
  618. if (IS_ERR(rdma->pd))
  619. goto error;
  620. /* Create the Queue Pair */
  621. memset(&qp_attr, 0, sizeof qp_attr);
  622. qp_attr.event_handler = qp_event_handler;
  623. qp_attr.qp_context = client;
  624. qp_attr.cap.max_send_wr = opts.sq_depth;
  625. qp_attr.cap.max_recv_wr = opts.rq_depth;
  626. qp_attr.cap.max_send_sge = P9_RDMA_SEND_SGE;
  627. qp_attr.cap.max_recv_sge = P9_RDMA_RECV_SGE;
  628. qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  629. qp_attr.qp_type = IB_QPT_RC;
  630. qp_attr.send_cq = rdma->cq;
  631. qp_attr.recv_cq = rdma->cq;
  632. err = rdma_create_qp(rdma->cm_id, rdma->pd, &qp_attr);
  633. if (err)
  634. goto error;
  635. rdma->qp = rdma->cm_id->qp;
  636. /* Request a connection */
  637. memset(&conn_param, 0, sizeof(conn_param));
  638. conn_param.private_data = NULL;
  639. conn_param.private_data_len = 0;
  640. conn_param.responder_resources = P9_RDMA_IRD;
  641. conn_param.initiator_depth = P9_RDMA_ORD;
  642. err = rdma_connect(rdma->cm_id, &conn_param);
  643. if (err)
  644. goto error;
  645. err = wait_for_completion_interruptible(&rdma->cm_done);
  646. if (err || (rdma->state != P9_RDMA_CONNECTED))
  647. goto error;
  648. client->status = Connected;
  649. return 0;
  650. error:
  651. rdma_destroy_trans(rdma);
  652. return -ENOTCONN;
  653. }
  654. static struct p9_trans_module p9_rdma_trans = {
  655. .name = "rdma",
  656. .maxsize = P9_RDMA_MAXSIZE,
  657. .pooled_rbuffers = true,
  658. .def = 0,
  659. .owner = THIS_MODULE,
  660. .create = rdma_create_trans,
  661. .close = rdma_close,
  662. .request = rdma_request,
  663. .cancel = rdma_cancel,
  664. .cancelled = rdma_cancelled,
  665. .show_options = p9_rdma_show_options,
  666. };
  667. /**
  668. * p9_trans_rdma_init - Register the 9P RDMA transport driver
  669. */
  670. static int __init p9_trans_rdma_init(void)
  671. {
  672. v9fs_register_trans(&p9_rdma_trans);
  673. return 0;
  674. }
  675. static void __exit p9_trans_rdma_exit(void)
  676. {
  677. v9fs_unregister_trans(&p9_rdma_trans);
  678. }
  679. module_init(p9_trans_rdma_init);
  680. module_exit(p9_trans_rdma_exit);
  681. MODULE_ALIAS_9P("rdma");
  682. MODULE_AUTHOR("Tom Tucker <tom@opengridcomputing.com>");
  683. MODULE_DESCRIPTION("RDMA Transport for 9P");
  684. MODULE_LICENSE("Dual BSD/GPL");