4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
31 * lnet/klnds/o2iblnd/o2iblnd_cb.c
33 * Author: Eric Barton <eric@bartonsoftware.com>
38 #define MAX_CONN_RACES_BEFORE_ABORT 20
40 static void kiblnd_peer_alive(struct kib_peer_ni *peer_ni);
41 static void kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
43 static struct ib_rdma_wr *
44 kiblnd_init_tx_msg_payload(struct lnet_ni *ni, struct kib_tx *tx,
45 int type, int body_nob, int payload_nob);
46 #define kiblnd_init_tx_msg(ni, tx, type, body) \
47 kiblnd_init_tx_msg_payload(ni, tx, type, body, 0)
48 static int kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
49 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie);
50 static void kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn);
51 static void kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn);
53 static void kiblnd_unmap_tx(struct kib_tx *tx);
54 static void kiblnd_check_sends_locked(struct kib_conn *conn);
57 kiblnd_tx_done(struct kib_tx *tx)
59 struct lnet_msg *lntmsg[2];
63 LASSERT (!in_interrupt());
64 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
65 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
66 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer_ni response */
67 LASSERT (tx->tx_pool != NULL);
71 /* tx may have up to 2 lnet msgs to finalise */
72 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
73 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
76 if (tx->tx_conn != NULL) {
77 kiblnd_conn_decref(tx->tx_conn);
81 tx->tx_nwrq = tx->tx_nsge = 0;
84 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
86 /* delay finalize until my descs have been freed */
87 for (i = 0; i < 2; i++) {
88 if (lntmsg[i] == NULL)
91 /* propagate health status to LNet for requests */
92 if (i == 0 && lntmsg[i])
93 lntmsg[i]->msg_health_status = tx->tx_hstatus;
95 lnet_finalize(lntmsg[i], rc);
100 kiblnd_txlist_done(struct list_head *txlist, int status,
101 enum lnet_msg_hstatus hstatus)
105 while ((tx = list_first_entry_or_null(txlist,
108 list_del(&tx->tx_list);
111 tx->tx_status = status;
112 if (hstatus != LNET_MSG_STATUS_OK)
113 tx->tx_hstatus = hstatus;
118 static struct kib_tx *
119 kiblnd_get_idle_tx(struct lnet_ni *ni, lnet_nid_t target)
121 struct kib_net *net = ni->ni_data;
122 struct list_head *node;
124 struct kib_tx_poolset *tps;
126 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target, ni)];
127 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
130 tx = container_of(node, struct kib_tx, tx_list);
132 LASSERT (tx->tx_nwrq == 0);
133 LASSERT (!tx->tx_queued);
134 LASSERT (tx->tx_sending == 0);
135 LASSERT (!tx->tx_waiting);
136 LASSERT (tx->tx_status == 0);
137 LASSERT (tx->tx_conn == NULL);
138 LASSERT (tx->tx_lntmsg[0] == NULL);
139 LASSERT (tx->tx_lntmsg[1] == NULL);
140 LASSERT (tx->tx_nfrags == 0);
143 tx->tx_hstatus = LNET_MSG_STATUS_OK;
149 kiblnd_drop_rx(struct kib_rx *rx)
151 struct kib_conn *conn = rx->rx_conn;
152 struct kib_sched_info *sched = conn->ibc_sched;
155 spin_lock_irqsave(&sched->ibs_lock, flags);
156 LASSERT(conn->ibc_nrx > 0);
158 spin_unlock_irqrestore(&sched->ibs_lock, flags);
160 kiblnd_conn_decref(conn);
164 kiblnd_post_rx(struct kib_rx *rx, int credit)
166 struct kib_conn *conn = rx->rx_conn;
167 struct kib_net *net = conn->ibc_peer->ibp_ni->ni_data;
168 struct ib_recv_wr *bad_wrq = NULL;
169 #ifdef HAVE_IB_GET_DMA_MR
170 struct ib_mr *mr = conn->ibc_hdev->ibh_mrs;
174 LASSERT (net != NULL);
175 LASSERT (!in_interrupt());
176 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
177 credit == IBLND_POSTRX_PEER_CREDIT ||
178 credit == IBLND_POSTRX_RSRVD_CREDIT);
179 #ifdef HAVE_IB_GET_DMA_MR
182 rx->rx_sge.lkey = mr->lkey;
184 rx->rx_sge.lkey = conn->ibc_hdev->ibh_pd->local_dma_lkey;
186 rx->rx_sge.addr = rx->rx_msgaddr;
187 rx->rx_sge.length = IBLND_MSG_SIZE;
189 rx->rx_wrq.next = NULL;
190 rx->rx_wrq.sg_list = &rx->rx_sge;
191 rx->rx_wrq.num_sge = 1;
192 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
194 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
195 LASSERT (rx->rx_nob >= 0); /* not posted */
197 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
198 kiblnd_drop_rx(rx); /* No more posts for this rx */
202 rx->rx_nob = -1; /* flag posted */
204 /* NB: need an extra reference after ib_post_recv because we don't
205 * own this rx (and rx::rx_conn) anymore, LU-5678.
207 kiblnd_conn_addref(conn);
208 #ifdef HAVE_IB_POST_SEND_RECV_CONST
209 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq,
210 (const struct ib_recv_wr **)&bad_wrq);
212 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
214 if (unlikely(rc != 0)) {
215 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
216 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
220 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
223 if (unlikely(rc != 0)) {
224 kiblnd_close_conn(conn, rc);
225 kiblnd_drop_rx(rx); /* No more posts for this rx */
229 if (credit == IBLND_POSTRX_NO_CREDIT)
232 spin_lock(&conn->ibc_lock);
233 if (credit == IBLND_POSTRX_PEER_CREDIT)
234 conn->ibc_outstanding_credits++;
236 conn->ibc_reserved_credits++;
237 kiblnd_check_sends_locked(conn);
238 spin_unlock(&conn->ibc_lock);
241 kiblnd_conn_decref(conn);
245 static struct kib_tx *
246 kiblnd_find_waiting_tx_locked(struct kib_conn *conn, int txtype, u64 cookie)
250 list_for_each_entry(tx, &conn->ibc_active_txs, tx_list) {
251 LASSERT(!tx->tx_queued);
252 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
254 if (tx->tx_cookie != cookie)
257 if (tx->tx_waiting &&
258 tx->tx_msg->ibm_type == txtype)
261 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
262 tx->tx_waiting ? "" : "NOT ",
263 tx->tx_msg->ibm_type, txtype);
269 kiblnd_handle_completion(struct kib_conn *conn, int txtype, int status, u64 cookie)
272 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
275 spin_lock(&conn->ibc_lock);
277 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
279 spin_unlock(&conn->ibc_lock);
281 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
282 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
283 kiblnd_close_conn(conn, -EPROTO);
287 if (tx->tx_status == 0) { /* success so far */
288 if (status < 0) { /* failed? */
289 if (status == -ECONNABORTED) {
290 CDEBUG(D_NET, "bad status for connection to %s "
291 "with completion type %x\n",
292 libcfs_nid2str(conn->ibc_peer->ibp_nid),
296 tx->tx_status = status;
297 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
298 } else if (txtype == IBLND_MSG_GET_REQ) {
299 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
305 idle = !tx->tx_queued && (tx->tx_sending == 0);
307 list_del(&tx->tx_list);
309 spin_unlock(&conn->ibc_lock);
316 kiblnd_send_completion(struct kib_conn *conn, int type, int status, u64 cookie)
318 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
319 struct kib_tx *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
322 CERROR("Can't get tx for completion %x for %s\n",
323 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
327 tx->tx_msg->ibm_u.completion.ibcm_status = status;
328 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
329 kiblnd_init_tx_msg(ni, tx, type, sizeof(struct kib_completion_msg));
331 kiblnd_queue_tx(tx, conn);
335 kiblnd_handle_rx(struct kib_rx *rx)
337 struct kib_msg *msg = rx->rx_msg;
338 struct kib_conn *conn = rx->rx_conn;
339 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
340 int credits = msg->ibm_credits;
346 struct lnet_nid srcnid;
348 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
350 CDEBUG(D_NET, "Received %x[%d] nob %u cm_id %p qp_num 0x%x\n",
351 msg->ibm_type, credits,
354 conn->ibc_cmid->qp ? conn->ibc_cmid->qp->qp_num : 0);
355 kiblnd_dump_conn_dbg(conn);
358 /* Have I received credits that will let me send? */
359 spin_lock(&conn->ibc_lock);
361 if (conn->ibc_credits + credits >
362 conn->ibc_queue_depth) {
363 rc2 = conn->ibc_credits;
364 spin_unlock(&conn->ibc_lock);
366 CERROR("Bad credits from %s: %d + %d > %d\n",
367 libcfs_nid2str(conn->ibc_peer->ibp_nid),
369 conn->ibc_queue_depth);
371 kiblnd_close_conn(conn, -EPROTO);
372 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
376 conn->ibc_credits += credits;
378 /* This ensures the credit taken by NOOP can be returned */
379 if (msg->ibm_type == IBLND_MSG_NOOP &&
380 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
381 conn->ibc_outstanding_credits++;
383 kiblnd_check_sends_locked(conn);
384 spin_unlock(&conn->ibc_lock);
387 switch (msg->ibm_type) {
389 CERROR("Bad IBLND message type %x from %s\n",
390 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
391 post_credit = IBLND_POSTRX_NO_CREDIT;
396 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
397 post_credit = IBLND_POSTRX_NO_CREDIT;
401 if (credits != 0) /* credit already posted */
402 post_credit = IBLND_POSTRX_NO_CREDIT;
403 else /* a keepalive NOOP */
404 post_credit = IBLND_POSTRX_PEER_CREDIT;
407 case IBLND_MSG_IMMEDIATE:
408 post_credit = IBLND_POSTRX_DONT_POST;
409 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.immediate.ibim_hdr);
410 lnet_nid4_to_nid(msg->ibm_srcnid, &srcnid);
411 rc = lnet_parse(ni, &hdr, &srcnid, rx, 0);
412 if (rc < 0) /* repost on error */
413 post_credit = IBLND_POSTRX_PEER_CREDIT;
416 case IBLND_MSG_PUT_REQ:
417 post_credit = IBLND_POSTRX_DONT_POST;
418 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.putreq.ibprm_hdr);
419 lnet_nid4_to_nid(msg->ibm_srcnid, &srcnid);
420 rc = lnet_parse(ni, &hdr, &srcnid, rx, 1);
421 if (rc < 0) /* repost on error */
422 post_credit = IBLND_POSTRX_PEER_CREDIT;
425 case IBLND_MSG_PUT_NAK:
426 CWARN ("PUT_NACK from %s\n",
427 libcfs_nid2str(conn->ibc_peer->ibp_nid));
428 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
429 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
430 msg->ibm_u.completion.ibcm_status,
431 msg->ibm_u.completion.ibcm_cookie);
434 case IBLND_MSG_PUT_ACK:
435 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
437 spin_lock(&conn->ibc_lock);
438 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
439 msg->ibm_u.putack.ibpam_src_cookie);
441 list_del(&tx->tx_list);
442 spin_unlock(&conn->ibc_lock);
445 CERROR("Unmatched PUT_ACK from %s\n",
446 libcfs_nid2str(conn->ibc_peer->ibp_nid));
451 LASSERT (tx->tx_waiting);
452 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
453 * (a) I can overwrite tx_msg since my peer_ni has received it!
454 * (b) tx_waiting set tells tx_complete() it's not done. */
456 tx->tx_nwrq = tx->tx_nsge = 0; /* overwrite PUT_REQ */
458 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
459 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
460 &msg->ibm_u.putack.ibpam_rd,
461 msg->ibm_u.putack.ibpam_dst_cookie);
463 CERROR("Can't setup rdma for PUT to %s: %d\n",
464 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
466 spin_lock(&conn->ibc_lock);
467 tx->tx_waiting = 0; /* clear waiting and queue atomically */
468 kiblnd_queue_tx_locked(tx, conn);
469 spin_unlock(&conn->ibc_lock);
472 case IBLND_MSG_PUT_DONE:
473 post_credit = IBLND_POSTRX_PEER_CREDIT;
474 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
475 msg->ibm_u.completion.ibcm_status,
476 msg->ibm_u.completion.ibcm_cookie);
479 case IBLND_MSG_GET_REQ:
480 post_credit = IBLND_POSTRX_DONT_POST;
481 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.get.ibgm_hdr);
482 lnet_nid4_to_nid(msg->ibm_srcnid, &srcnid);
483 rc = lnet_parse(ni, &hdr, &srcnid, rx, 1);
484 if (rc < 0) /* repost on error */
485 post_credit = IBLND_POSTRX_PEER_CREDIT;
488 case IBLND_MSG_GET_DONE:
489 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
490 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
491 msg->ibm_u.completion.ibcm_status,
492 msg->ibm_u.completion.ibcm_cookie);
496 if (rc < 0) /* protocol error */
497 kiblnd_close_conn(conn, rc);
499 if (post_credit != IBLND_POSTRX_DONT_POST)
500 kiblnd_post_rx(rx, post_credit);
504 kiblnd_rx_complete(struct kib_rx *rx, int status, int nob)
506 struct kib_msg *msg = rx->rx_msg;
507 struct kib_conn *conn = rx->rx_conn;
508 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
509 struct kib_net *net = ni->ni_data;
514 LASSERT(rx->rx_nob < 0); /* was posted */
515 rx->rx_nob = 0; /* isn't now */
517 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
520 if (status != IB_WC_SUCCESS) {
521 CNETERR("Rx from %s failed: %d\n",
522 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
529 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
531 CERROR("Error %d unpacking rx from %s\n",
532 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
536 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
537 msg->ibm_dstnid != lnet_nid_to_nid4(&ni->ni_nid) ||
538 msg->ibm_srcstamp != conn->ibc_incarnation ||
539 msg->ibm_dststamp != net->ibn_incarnation) {
540 CERROR("Stale rx from %s\n",
541 libcfs_nid2str(conn->ibc_peer->ibp_nid));
546 /* set time last known alive */
547 kiblnd_peer_alive(conn->ibc_peer);
549 /* racing with connection establishment/teardown! */
551 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
552 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
555 write_lock_irqsave(g_lock, flags);
556 /* must check holding global lock to eliminate race */
557 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
558 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
559 write_unlock_irqrestore(g_lock, flags);
562 write_unlock_irqrestore(g_lock, flags);
564 kiblnd_handle_rx(rx);
568 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
569 kiblnd_close_conn(conn, err);
571 kiblnd_drop_rx(rx); /* Don't re-post rx. */
575 kiblnd_fmr_map_tx(struct kib_net *net, struct kib_tx *tx,
576 struct kib_rdma_desc *rd, u32 nob)
578 struct kib_hca_dev *hdev;
580 struct kib_fmr_poolset *fps;
585 LASSERT(tx->tx_pool != NULL);
586 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
589 hdev = tx->tx_pool->tpo_hdev;
590 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
593 * If we're dealing with FastReg, but the device doesn't
594 * support GAPS and the tx has GAPS, then there is no real point
595 * in trying to map the memory, because it'll just fail. So
596 * preemptively fail with an appropriate message
598 if (IS_FAST_REG_DEV(dev) &&
599 !(dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT) &&
601 CERROR("Using FastReg with no GAPS support, but tx has gaps. "
602 "Try setting use_fastreg_gaps to 1\n");
603 return -EPROTONOSUPPORT;
606 #ifdef HAVE_FMR_POOL_API
608 * FMR does not support gaps but the tx has gaps then
609 * we should make sure that the number of fragments we'll be sending
610 * over fits within the number of fragments negotiated on the
611 * connection, otherwise, we won't be able to RDMA the data.
612 * We need to maintain the number of fragments negotiation on the
613 * connection for backwards compatibility.
615 if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
617 tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
618 CERROR("TX number of frags (%d) is <= than connection"
619 " number of frags (%d). Consider setting peer's"
620 " map_on_demand to 256\n", tx->tx_nfrags,
621 tx->tx_conn->ibc_max_frags);
627 fps = net->ibn_fmr_ps[cpt];
628 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
630 CERROR("Can't map %u bytes (%u/%u)s: %d\n", nob,
631 tx->tx_nfrags, rd->rd_nfrags, rc);
636 * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
639 rd->rd_key = tx->tx_fmr.fmr_key;
641 * for FastReg or FMR with no gaps we can accumulate all
642 * the fragments in one FastReg or FMR fragment.
645 #ifdef HAVE_FMR_POOL_API
646 ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
649 IS_FAST_REG_DEV(dev)) {
650 /* FMR requires zero based address */
651 #ifdef HAVE_FMR_POOL_API
652 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
653 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
655 rd->rd_frags[0].rf_nob = nob;
659 * We're transmitting with gaps using FMR.
660 * We'll need to use multiple fragments and identify the
661 * zero based address of each fragment.
663 for (i = 0; i < rd->rd_nfrags; i++) {
664 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
665 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
673 kiblnd_unmap_tx(struct kib_tx *tx)
676 #ifdef HAVE_FMR_POOL_API
677 tx->tx_fmr.fmr_pfmr ||
680 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
682 if (tx->tx_nfrags != 0) {
683 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev, tx);
688 #ifdef HAVE_IB_GET_DMA_MR
689 static struct ib_mr *
690 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
692 struct kib_net *net = ni->ni_data;
693 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
694 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
696 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
699 * if map-on-demand is turned on and the device supports
700 * either FMR or FastReg then use that. Otherwise use global
701 * memory regions. If that's not available either, then you're
702 * dead in the water and fail the operation.
704 if (tunables->lnd_map_on_demand && (IS_FAST_REG_DEV(net->ibn_dev)
705 #ifdef HAVE_FMR_POOL_API
706 || net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED
712 * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
713 * in the call chain. The mapping will fail with appropriate error
716 return hdev->ibh_mrs;
720 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
721 struct kib_rdma_desc *rd, int nfrags)
723 struct kib_net *net = ni->ni_data;
724 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
725 #ifdef HAVE_IB_GET_DMA_MR
726 struct ib_mr *mr = NULL;
731 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
733 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
734 tx->tx_nfrags = nfrags;
736 rd->rd_nfrags = kiblnd_dma_map_sg(hdev, tx);
737 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
738 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
739 hdev->ibh_ibdev, &tx->tx_frags[i]);
740 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
741 hdev->ibh_ibdev, &tx->tx_frags[i]);
742 nob += rd->rd_frags[i].rf_nob;
745 #ifdef HAVE_IB_GET_DMA_MR
746 mr = kiblnd_find_rd_dma_mr(ni, rd);
748 /* found pre-mapping MR */
749 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
754 if (net->ibn_fmr_ps != NULL)
755 return kiblnd_fmr_map_tx(net, tx, rd, nob);
760 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
761 struct kib_rdma_desc *rd, int nkiov,
762 struct bio_vec *kiov, int offset, int nob)
764 struct kib_net *net = ni->ni_data;
765 struct scatterlist *sg;
770 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
774 LASSERT(net != NULL);
776 while (offset >= kiov->bv_len) {
777 offset -= kiov->bv_len;
790 CERROR("lacking enough sg entries to map tx\n");
795 fragnob = min((int)(kiov->bv_len - offset), nob);
798 * We're allowed to start at a non-aligned page offset in
799 * the first fragment and end at a non-aligned page offset
800 * in the last fragment.
802 if ((fragnob < (int)(kiov->bv_len - offset)) &&
803 nkiov < max_nkiov && nob > fragnob) {
804 CDEBUG(D_NET, "fragnob %d < available page %d: with"
805 " remaining %d kiovs with %d nob left\n",
806 fragnob, (int)(kiov->bv_len - offset),
811 sg_set_page(sg, kiov->bv_page, fragnob,
812 kiov->bv_offset + offset);
821 return kiblnd_map_tx(ni, tx, rd, sg_count);
825 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
826 __must_hold(&conn->ibc_lock)
828 struct kib_msg *msg = tx->tx_msg;
829 struct kib_peer_ni *peer_ni = conn->ibc_peer;
830 struct lnet_ni *ni = peer_ni->ibp_ni;
831 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
832 int ver = conn->ibc_version;
836 LASSERT(tx->tx_queued);
837 /* We rely on this for QP sizing */
838 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
839 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
841 LASSERT(credit == 0 || credit == 1);
842 LASSERT(conn->ibc_outstanding_credits >= 0);
843 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
844 LASSERT(conn->ibc_credits >= 0);
845 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
847 if (conn->ibc_nsends_posted ==
848 kiblnd_concurrent_sends(ver, ni)) {
849 /* tx completions outstanding... */
850 CDEBUG(D_NET, "%s: posted enough\n",
851 libcfs_nid2str(peer_ni->ibp_nid));
855 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
856 CDEBUG(D_NET, "%s: no credits cm_id %p qp_num 0x%x\n",
857 libcfs_nid2str(peer_ni->ibp_nid),
859 conn->ibc_cmid->qp ? conn->ibc_cmid->qp->qp_num : 0);
860 kiblnd_dump_conn_dbg(conn);
864 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
865 conn->ibc_credits == 1 && /* last credit reserved */
866 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
867 CDEBUG(D_NET, "%s: not using last credit\n",
868 libcfs_nid2str(peer_ni->ibp_nid));
872 /* NB don't drop ibc_lock before bumping tx_sending */
873 list_del(&tx->tx_list);
876 if (msg->ibm_type == IBLND_MSG_NOOP &&
877 (!kiblnd_need_noop(conn) || /* redundant NOOP */
878 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
879 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
880 /* OK to drop when posted enough NOOPs, since
881 * kiblnd_check_sends_locked will queue NOOP again when
882 * posted NOOPs complete */
883 spin_unlock(&conn->ibc_lock);
884 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
886 spin_lock(&conn->ibc_lock);
887 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP cm_id %p qp_num 0x%x\n",
888 libcfs_nid2str(peer_ni->ibp_nid),
889 conn->ibc_noops_posted,
891 conn->ibc_cmid->qp ? conn->ibc_cmid->qp->qp_num : 0);
892 kiblnd_dump_conn_dbg(conn);
896 CDEBUG(D_NET, "Transmit %x[%d] nob %u cm_id %p qp_num 0x%x\n",
897 msg->ibm_type, credit,
900 conn->ibc_cmid->qp ? conn->ibc_cmid->qp->qp_num : 0);
901 kiblnd_dump_conn_dbg(conn);
903 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
904 peer_ni->ibp_nid, conn->ibc_incarnation);
906 conn->ibc_credits -= credit;
907 conn->ibc_outstanding_credits = 0;
908 conn->ibc_nsends_posted++;
909 if (msg->ibm_type == IBLND_MSG_NOOP)
910 conn->ibc_noops_posted++;
912 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
913 * PUT. If so, it was first queued here as a PUT_REQ, sent and
914 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
915 * and then re-queued here. It's (just) possible that
916 * tx_sending is non-zero if we've not done the tx_complete()
917 * from the first send; hence the ++ rather than = below. */
919 list_add(&tx->tx_list, &conn->ibc_active_txs);
921 /* I'm still holding ibc_lock! */
922 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
923 CDEBUG(D_NET, "connection to %s is not established\n",
924 conn->ibc_peer? libcfs_nid2str(conn->ibc_peer->ibp_nid): "NULL");
926 } else if (tx->tx_pool->tpo_pool.po_failed ||
927 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
928 /* close_conn will launch failover */
931 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
932 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
934 if (frd != NULL && !frd->frd_posted) {
935 wr = &frd->frd_inv_wr.wr;
936 wr->next = &frd->frd_fastreg_wr.wr;
937 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
940 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
941 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
942 bad->wr_id, bad->opcode, bad->send_flags,
943 libcfs_nid2str(conn->ibc_peer->ibp_nid));
946 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
949 #ifdef HAVE_IB_POST_SEND_RECV_CONST
950 rc = ib_post_send(conn->ibc_cmid->qp, wr,
951 (const struct ib_send_wr **)&bad);
953 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
955 if (frd && !frd->frd_posted) {
956 /* The local invalidate becomes invalid (has been
957 * successfully used) if the post succeeds or the
958 * failing wr was not the invalidate. */
960 !(rc == 0 || (bad != &frd->frd_inv_wr.wr));
964 conn->ibc_last_send = ktime_get();
968 frd->frd_posted = true;
972 /* NB credits are transferred in the actual
973 * message, which can only be the last work item */
974 conn->ibc_credits += credit;
975 conn->ibc_outstanding_credits += msg->ibm_credits;
976 conn->ibc_nsends_posted--;
977 if (msg->ibm_type == IBLND_MSG_NOOP)
978 conn->ibc_noops_posted--;
984 done = (tx->tx_sending == 0);
986 list_del(&tx->tx_list);
988 spin_unlock(&conn->ibc_lock);
990 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
991 CERROR("Error %d posting transmit to %s\n",
992 rc, libcfs_nid2str(peer_ni->ibp_nid));
994 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
995 rc, libcfs_nid2str(peer_ni->ibp_nid));
997 kiblnd_close_conn(conn, rc);
1002 spin_lock(&conn->ibc_lock);
1008 kiblnd_check_sends_locked(struct kib_conn *conn)
1010 int ver = conn->ibc_version;
1011 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
1014 /* Don't send anything until after the connection is established */
1015 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
1016 CDEBUG(D_NET, "%s too soon\n",
1017 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1021 LASSERT(conn->ibc_nsends_posted <=
1022 kiblnd_concurrent_sends(ver, ni));
1023 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
1024 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
1025 LASSERT (conn->ibc_reserved_credits >= 0);
1027 while (conn->ibc_reserved_credits > 0 &&
1028 (tx = list_first_entry_or_null(&conn->ibc_tx_queue_rsrvd,
1029 struct kib_tx, tx_list)) != NULL) {
1030 list_move_tail(&tx->tx_list, &conn->ibc_tx_queue);
1031 conn->ibc_reserved_credits--;
1034 if (kiblnd_need_noop(conn)) {
1035 spin_unlock(&conn->ibc_lock);
1037 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1039 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1041 spin_lock(&conn->ibc_lock);
1043 kiblnd_queue_tx_locked(tx, conn);
1049 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1051 tx = list_first_entry(&conn->ibc_tx_queue_nocred,
1052 struct kib_tx, tx_list);
1053 } else if (!list_empty(&conn->ibc_tx_noops)) {
1054 LASSERT (!IBLND_OOB_CAPABLE(ver));
1056 tx = list_first_entry(&conn->ibc_tx_noops,
1057 struct kib_tx, tx_list);
1058 } else if (!list_empty(&conn->ibc_tx_queue)) {
1060 tx = list_first_entry(&conn->ibc_tx_queue,
1061 struct kib_tx, tx_list);
1065 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1071 kiblnd_tx_complete(struct kib_tx *tx, int status)
1073 int failed = (status != IB_WC_SUCCESS);
1074 struct kib_conn *conn = tx->tx_conn;
1077 if (tx->tx_sending <= 0) {
1078 CERROR("Received an event on a freed tx: %p status %d\n",
1084 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1085 CNETERR("Tx -> %s cookie %#llx"
1086 " sending %d waiting %d: failed %d\n",
1087 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1088 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1091 kiblnd_close_conn(conn, -EIO);
1093 kiblnd_peer_alive(conn->ibc_peer);
1096 spin_lock(&conn->ibc_lock);
1098 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1099 * gets to free it, which also drops its ref on 'conn'. */
1102 conn->ibc_nsends_posted--;
1103 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1104 conn->ibc_noops_posted--;
1107 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1108 tx->tx_waiting = 0; /* don't wait for peer_ni */
1109 tx->tx_status = -EIO;
1110 #ifdef O2IBLND_CONN_STATE_DEBUG
1111 kiblnd_dump_conn_dbg(conn);
1115 idle = (tx->tx_sending == 0) && /* This is the final callback */
1116 !tx->tx_waiting && /* Not waiting for peer_ni */
1117 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1119 list_del(&tx->tx_list);
1121 kiblnd_check_sends_locked(conn);
1122 spin_unlock(&conn->ibc_lock);
1130 kiblnd_init_tx_sge(struct kib_tx *tx, u64 addr, unsigned int len)
1132 struct ib_sge *sge = &tx->tx_sge[tx->tx_nsge];
1133 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1134 #ifdef HAVE_IB_GET_DMA_MR
1135 struct ib_mr *mr = hdev->ibh_mrs;
1138 *sge = (struct ib_sge) {
1139 #ifdef HAVE_IB_GET_DMA_MR
1142 .lkey = hdev->ibh_pd->local_dma_lkey,
1151 static struct ib_rdma_wr *
1152 kiblnd_init_tx_msg_payload(struct lnet_ni *ni, struct kib_tx *tx, int type,
1153 int body_nob, int payload)
1155 struct ib_rdma_wr *wrq;
1156 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1158 LASSERT(tx->tx_nwrq >= 0);
1159 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1160 LASSERT(nob <= IBLND_MSG_SIZE);
1162 kiblnd_init_msg(tx->tx_msg, type, body_nob + payload);
1164 wrq = &tx->tx_wrq[tx->tx_nwrq];
1166 *wrq = (struct ib_rdma_wr) {
1168 .wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
1170 .sg_list = &tx->tx_sge[tx->tx_nsge],
1171 .opcode = IB_WR_SEND,
1172 .send_flags = IB_SEND_SIGNALED,
1176 kiblnd_init_tx_sge(tx, tx->tx_msgaddr, nob);
1183 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1184 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1186 struct kib_msg *ibmsg = tx->tx_msg;
1187 struct kib_rdma_desc *srcrd = tx->tx_rd;
1188 struct ib_rdma_wr *wrq = NULL;
1196 LASSERT(!in_interrupt());
1197 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1198 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1200 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1201 resid > 0; resid -= sge_nob) {
1204 if (srcidx >= srcrd->rd_nfrags) {
1205 CERROR("Src buffer exhausted: %d frags %px\n",
1211 if (dstidx >= dstrd->rd_nfrags) {
1212 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1217 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1218 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1219 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1220 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1221 conn->ibc_max_frags,
1222 srcidx, srcrd->rd_nfrags,
1223 dstidx, dstrd->rd_nfrags);
1228 sge_nob = min3(kiblnd_rd_frag_size(srcrd, srcidx),
1229 kiblnd_rd_frag_size(dstrd, dstidx),
1232 sge = &tx->tx_sge[tx->tx_nsge];
1233 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1234 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1235 sge->length = sge_nob;
1238 wrq = &tx->tx_wrq[tx->tx_nwrq];
1240 wrq->wr.next = &(wrq + 1)->wr;
1241 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1242 wrq->wr.sg_list = sge;
1243 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1244 wrq->wr.send_flags = 0;
1246 #ifdef HAVE_IB_RDMA_WR
1247 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1249 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1252 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1254 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1259 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1260 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1263 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1265 wrq->wr.num_sge = wrq_sge;
1271 if (rc < 0) /* no RDMA if completing with failure */
1272 tx->tx_nwrq = tx->tx_nsge = 0;
1274 ibmsg->ibm_u.completion.ibcm_status = rc;
1275 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1276 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1277 type, sizeof(struct kib_completion_msg));
1283 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1285 struct list_head *q;
1288 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1289 LASSERT(!tx->tx_queued); /* not queued for sending already */
1290 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1292 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1293 CDEBUG(D_NET, "connection with %s is disconnected\n",
1294 conn->ibc_peer? libcfs_nid2str(conn->ibc_peer->ibp_nid): "NULL");
1296 tx->tx_status = -ECONNABORTED;
1298 if (tx->tx_conn != NULL) {
1299 /* PUT_DONE first attached to conn as a PUT_REQ */
1300 LASSERT(tx->tx_conn == conn);
1301 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1303 kiblnd_conn_decref(conn);
1305 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1310 timeout_ns = kiblnd_timeout() * NSEC_PER_SEC;
1312 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1314 if (tx->tx_conn == NULL) {
1315 kiblnd_conn_addref(conn);
1317 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1319 /* PUT_DONE first attached to conn as a PUT_REQ */
1320 LASSERT (tx->tx_conn == conn);
1321 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1324 switch (tx->tx_msg->ibm_type) {
1328 case IBLND_MSG_PUT_REQ:
1329 case IBLND_MSG_GET_REQ:
1330 q = &conn->ibc_tx_queue_rsrvd;
1333 case IBLND_MSG_PUT_NAK:
1334 case IBLND_MSG_PUT_ACK:
1335 case IBLND_MSG_PUT_DONE:
1336 case IBLND_MSG_GET_DONE:
1337 q = &conn->ibc_tx_queue_nocred;
1340 case IBLND_MSG_NOOP:
1341 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1342 q = &conn->ibc_tx_queue_nocred;
1344 q = &conn->ibc_tx_noops;
1347 case IBLND_MSG_IMMEDIATE:
1348 q = &conn->ibc_tx_queue;
1352 list_add_tail(&tx->tx_list, q);
1356 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1358 spin_lock(&conn->ibc_lock);
1359 kiblnd_queue_tx_locked(tx, conn);
1360 kiblnd_check_sends_locked(conn);
1361 spin_unlock(&conn->ibc_lock);
1365 kiblnd_resolve_addr_cap(struct rdma_cm_id *cmid,
1366 struct sockaddr_in *srcaddr,
1367 struct sockaddr_in *dstaddr,
1370 unsigned short port;
1373 /* allow the port to be reused */
1374 rc = rdma_set_reuseaddr(cmid, 1);
1376 CERROR("Unable to set reuse on cmid: %d\n", rc);
1380 /* look for a free privileged port */
1381 for (port = PROT_SOCK-1; port > 0; port--) {
1382 srcaddr->sin_port = htons(port);
1383 rc = rdma_resolve_addr(cmid,
1384 (struct sockaddr *)srcaddr,
1385 (struct sockaddr *)dstaddr,
1388 CDEBUG(D_NET, "bound to port %hu\n", port);
1390 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1391 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1398 CERROR("cannot bind to a free privileged port: rc = %d\n", rc);
1404 kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1405 struct sockaddr_in *srcaddr,
1406 struct sockaddr_in *dstaddr,
1409 const struct cred *old_creds = NULL;
1410 struct cred *new_creds;
1413 if (!capable(CAP_NET_BIND_SERVICE)) {
1414 new_creds = prepare_kernel_cred(NULL);
1418 cap_raise(new_creds->cap_effective, CAP_NET_BIND_SERVICE);
1419 old_creds = override_creds(new_creds);
1422 rc = kiblnd_resolve_addr_cap(cmid, srcaddr, dstaddr, timeout_ms);
1425 revert_creds(old_creds);
1431 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1433 struct rdma_cm_id *cmid;
1434 struct kib_dev *dev;
1435 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1436 struct sockaddr_in srcaddr;
1437 struct sockaddr_in dstaddr;
1440 LASSERT (net != NULL);
1441 LASSERT (peer_ni->ibp_connecting > 0);
1443 cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1444 kiblnd_cm_callback, peer_ni,
1445 RDMA_PS_TCP, IB_QPT_RC);
1448 CERROR("Can't create CMID for %s: %ld\n",
1449 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1455 memset(&srcaddr, 0, sizeof(srcaddr));
1456 srcaddr.sin_family = AF_INET;
1457 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1459 memset(&dstaddr, 0, sizeof(dstaddr));
1460 dstaddr.sin_family = AF_INET;
1461 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1462 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1464 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1466 if (*kiblnd_tunables.kib_use_priv_port) {
1467 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1468 kiblnd_timeout() * 1000);
1470 rc = rdma_resolve_addr(cmid,
1471 (struct sockaddr *)&srcaddr,
1472 (struct sockaddr *)&dstaddr,
1473 kiblnd_timeout() * 1000);
1476 /* Can't initiate address resolution: */
1477 CERROR("Can't resolve addr for %s: %d\n",
1478 libcfs_nid2str(peer_ni->ibp_nid), rc);
1485 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1486 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1487 rdma_destroy_id(cmid);
1490 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1494 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1496 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1497 char *reason = NULL;
1499 unsigned long flags;
1501 write_lock_irqsave(glock, flags);
1502 if (peer_ni->ibp_reconnecting == 0) {
1503 if (peer_ni->ibp_accepting)
1504 reason = "accepting";
1505 else if (peer_ni->ibp_connecting)
1506 reason = "connecting";
1507 else if (!list_empty(&peer_ni->ibp_conns))
1508 reason = "connected";
1509 else /* connected then closed */
1515 if (peer_ni->ibp_accepting)
1516 CNETERR("Detecting race between accepting and reconnecting\n");
1517 peer_ni->ibp_reconnecting--;
1519 if (!kiblnd_peer_active(peer_ni)) {
1520 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1521 reason = "unlinked";
1525 peer_ni->ibp_connecting++;
1526 peer_ni->ibp_reconnected++;
1528 write_unlock_irqrestore(glock, flags);
1530 kiblnd_connect_peer(peer_ni);
1534 write_unlock_irqrestore(glock, flags);
1536 CWARN("Abort reconnection of %s: %s\n",
1537 libcfs_nid2str(peer_ni->ibp_nid), reason);
1538 kiblnd_txlist_done(&txs, -ECONNABORTED,
1539 LNET_MSG_STATUS_LOCAL_ABORTED);
1544 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1546 struct kib_peer_ni *peer_ni;
1547 struct kib_peer_ni *peer2;
1548 struct kib_conn *conn;
1549 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1550 unsigned long flags;
1553 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1556 /* If I get here, I've committed to send, so I complete the tx with
1557 * failure on any problems
1560 LASSERT(!tx || !tx->tx_conn); /* only set when assigned a conn */
1561 LASSERT(!tx || tx->tx_nwrq > 0); /* work items have been set up */
1563 /* First time, just use a read lock since I expect to find my peer_ni
1566 read_lock_irqsave(g_lock, flags);
1568 peer_ni = kiblnd_find_peer_locked(ni, nid);
1569 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1570 /* Found a peer_ni with an established connection */
1571 conn = kiblnd_get_conn_locked(peer_ni);
1572 kiblnd_conn_addref(conn); /* 1 ref for me... */
1574 read_unlock_irqrestore(g_lock, flags);
1577 kiblnd_queue_tx(tx, conn);
1578 kiblnd_conn_decref(conn); /* ...to here */
1582 timeout_ns = kiblnd_timeout() * NSEC_PER_SEC;
1583 read_unlock(g_lock);
1584 /* Re-try with a write lock */
1587 peer_ni = kiblnd_find_peer_locked(ni, nid);
1588 if (peer_ni != NULL) {
1589 if (list_empty(&peer_ni->ibp_conns)) {
1590 /* found a peer_ni, but it's still connecting... */
1591 LASSERT(kiblnd_peer_connecting(peer_ni));
1593 tx->tx_deadline = ktime_add_ns(ktime_get(),
1595 list_add_tail(&tx->tx_list,
1596 &peer_ni->ibp_tx_queue);
1598 write_unlock_irqrestore(g_lock, flags);
1600 conn = kiblnd_get_conn_locked(peer_ni);
1601 kiblnd_conn_addref(conn); /* 1 ref for me... */
1603 write_unlock_irqrestore(g_lock, flags);
1606 kiblnd_queue_tx(tx, conn);
1607 kiblnd_conn_decref(conn); /* ...to here */
1612 write_unlock_irqrestore(g_lock, flags);
1614 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1615 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1617 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1619 tx->tx_status = -EHOSTUNREACH;
1621 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1627 write_lock_irqsave(g_lock, flags);
1629 peer2 = kiblnd_find_peer_locked(ni, nid);
1630 if (peer2 != NULL) {
1631 if (list_empty(&peer2->ibp_conns)) {
1632 /* found a peer_ni, but it's still connecting... */
1633 LASSERT(kiblnd_peer_connecting(peer2));
1635 tx->tx_deadline = ktime_add_ns(ktime_get(),
1637 list_add_tail(&tx->tx_list,
1638 &peer2->ibp_tx_queue);
1640 write_unlock_irqrestore(g_lock, flags);
1642 conn = kiblnd_get_conn_locked(peer2);
1643 kiblnd_conn_addref(conn); /* 1 ref for me... */
1645 write_unlock_irqrestore(g_lock, flags);
1648 kiblnd_queue_tx(tx, conn);
1649 kiblnd_conn_decref(conn); /* ...to here */
1652 kiblnd_peer_decref(peer_ni);
1656 /* Brand new peer_ni */
1657 LASSERT(peer_ni->ibp_connecting == 0);
1658 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1659 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1661 /* always called with a ref on ni, which prevents ni being shutdown */
1662 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1665 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1666 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1669 kiblnd_peer_addref(peer_ni);
1670 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
1672 write_unlock_irqrestore(g_lock, flags);
1674 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1675 kiblnd_connect_peer(peer_ni);
1676 kiblnd_peer_decref(peer_ni);
1680 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1682 struct kib_dev *dev = ((struct kib_net *)ni->ni_data)->ibn_dev;
1683 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1684 int type = lntmsg->msg_type;
1685 struct lnet_processid *target = &lntmsg->msg_target;
1686 int target_is_router = lntmsg->msg_target_is_router;
1687 int routing = lntmsg->msg_routing;
1688 unsigned int payload_niov = lntmsg->msg_niov;
1689 struct bio_vec *payload_kiov = lntmsg->msg_kiov;
1690 unsigned int payload_offset = lntmsg->msg_offset;
1691 unsigned int payload_nob = lntmsg->msg_len;
1692 struct lnet_libmd *msg_md = lntmsg->msg_md;
1694 struct kib_msg *ibmsg;
1695 struct kib_rdma_desc *rd;
1700 /* NB 'private' is different depending on what we're sending.... */
1702 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1703 payload_nob, payload_niov, libcfs_idstr(target));
1705 LASSERT(payload_nob == 0 || payload_niov > 0);
1707 /* Thread context */
1708 LASSERT(!in_interrupt());
1710 tx = kiblnd_get_idle_tx(ni, lnet_nid_to_nid4(&target->nid));
1712 CERROR("Can't allocate %s txd for %s\n",
1713 lnet_msgtyp2str(type),
1714 libcfs_nidstr(&target->nid));
1718 gpu = msg_md ? (msg_md->md_flags & LNET_MD_FLAG_GPU) : false;
1726 LASSERT(payload_nob == 0);
1730 if (routing || target_is_router)
1731 break; /* send IMMEDIATE */
1733 /* is the REPLY message too small for RDMA? */
1734 nob = offsetof(struct kib_msg,
1735 ibm_u.immediate.ibim_payload[msg_md->md_length]);
1736 if (nob <= IBLND_MSG_SIZE && !gpu)
1737 break; /* send IMMEDIATE */
1739 rd = &ibmsg->ibm_u.get.ibgm_rd;
1741 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1744 0, msg_md->md_length);
1746 CERROR("Can't setup GET sink for %s: %d\n",
1747 libcfs_nidstr(&target->nid), rc);
1748 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1753 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1754 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1755 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.get.ibgm_hdr);
1757 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1759 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1760 if (tx->tx_lntmsg[1] == NULL) {
1761 CERROR("Can't create reply for GET -> %s\n",
1762 libcfs_nidstr(&target->nid));
1767 /* finalise lntmsg[0,1] on completion */
1768 tx->tx_lntmsg[0] = lntmsg;
1769 tx->tx_waiting = 1; /* waiting for GET_DONE */
1770 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1773 case LNET_MSG_REPLY:
1775 /* Is the payload small enough not to need RDMA? */
1776 nob = offsetof(struct kib_msg,
1777 ibm_u.immediate.ibim_payload[payload_nob]);
1778 if (nob <= IBLND_MSG_SIZE && !gpu)
1779 break; /* send IMMEDIATE */
1783 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1784 payload_niov, payload_kiov,
1785 payload_offset, payload_nob);
1787 CERROR("Can't setup PUT src for %s: %d\n",
1788 libcfs_nidstr(&target->nid), rc);
1793 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.putreq.ibprm_hdr);
1794 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1795 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1796 sizeof(struct kib_putreq_msg));
1798 /* finalise lntmsg[0,1] on completion */
1799 tx->tx_lntmsg[0] = lntmsg;
1800 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1801 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1805 /* send IMMEDIATE */
1806 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1810 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.immediate.ibim_hdr);
1812 if (IS_FAST_REG_DEV(dev) && payload_nob) {
1813 struct ib_rdma_wr *wrq;
1816 nob = offsetof(struct kib_immediate_msg, ibim_payload[0]);
1817 wrq = kiblnd_init_tx_msg_payload(ni, tx, IBLND_MSG_IMMEDIATE,
1821 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1822 payload_niov, payload_kiov,
1823 payload_offset, payload_nob);
1825 CERROR("Can't setup IMMEDIATE src for %s: %d\n",
1826 libcfs_nidstr(&target->nid), rc);
1831 /* lets generate a SGE chain */
1832 for (i = 0; i < rd->rd_nfrags; i++) {
1833 kiblnd_init_tx_sge(tx, rd->rd_frags[i].rf_addr,
1834 rd->rd_frags[i].rf_nob);
1838 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1839 offsetof(struct kib_msg,
1840 ibm_u.immediate.ibim_payload),
1841 payload_niov, payload_kiov,
1842 payload_offset, payload_nob);
1844 nob = offsetof(struct kib_immediate_msg,
1845 ibim_payload[payload_nob]);
1847 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1850 /* finalise lntmsg on completion */
1851 tx->tx_lntmsg[0] = lntmsg;
1853 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1858 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1860 struct lnet_processid *target = &lntmsg->msg_target;
1861 unsigned int niov = lntmsg->msg_niov;
1862 struct bio_vec *kiov = lntmsg->msg_kiov;
1863 unsigned int offset = lntmsg->msg_offset;
1864 unsigned int nob = lntmsg->msg_len;
1865 struct lnet_libmd *msg_md = lntmsg->msg_md;
1869 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1871 CERROR("Can't get tx for REPLY to %s\n",
1872 libcfs_nidstr(&target->nid));
1877 tx->tx_gpu = msg_md ? (msg_md->md_flags & LNET_MD_FLAG_GPU) : 0;
1882 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1883 niov, kiov, offset, nob);
1886 CERROR("Can't setup GET src for %s: %d\n",
1887 libcfs_nidstr(&target->nid), rc);
1891 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1892 IBLND_MSG_GET_DONE, nob,
1893 &rx->rx_msg->ibm_u.get.ibgm_rd,
1894 rx->rx_msg->ibm_u.get.ibgm_cookie);
1896 CERROR("Can't setup rdma for GET from %s: %d\n",
1897 libcfs_nidstr(&target->nid), rc);
1902 /* No RDMA: local completion may happen now! */
1903 lnet_finalize(lntmsg, 0);
1905 /* RDMA: lnet_finalize(lntmsg) when it
1907 tx->tx_lntmsg[0] = lntmsg;
1910 kiblnd_queue_tx(tx, rx->rx_conn);
1915 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1918 lnet_finalize(lntmsg, -EIO);
1922 kiblnd_get_dev_prio(struct lnet_ni *ni, unsigned int dev_idx)
1924 struct kib_net *net = ni->ni_data;
1925 struct device *dev = NULL;
1928 dev = net->ibn_dev->ibd_hdev->ibh_ibdev->dma_device;
1930 return lnet_get_dev_prio(dev, dev_idx);
1935 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1936 int delayed, unsigned int niov, struct bio_vec *kiov,
1937 unsigned int offset, unsigned int mlen, unsigned int rlen)
1939 struct kib_rx *rx = private;
1940 struct kib_msg *rxmsg = rx->rx_msg;
1941 struct kib_conn *conn = rx->rx_conn;
1945 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1948 LASSERT (mlen <= rlen);
1949 LASSERT (!in_interrupt());
1951 switch (rxmsg->ibm_type) {
1955 case IBLND_MSG_IMMEDIATE:
1956 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1957 if (nob > rx->rx_nob) {
1958 CERROR("Immediate message from %s too big: %d(%d)\n",
1959 libcfs_nidstr(&lntmsg->msg_hdr.src_nid),
1965 lnet_copy_flat2kiov(niov, kiov, offset,
1966 IBLND_MSG_SIZE, rxmsg,
1967 offsetof(struct kib_msg,
1968 ibm_u.immediate.ibim_payload),
1970 lnet_finalize(lntmsg, 0);
1973 case IBLND_MSG_PUT_REQ: {
1974 struct kib_msg *txmsg;
1975 struct kib_rdma_desc *rd;
1976 struct lnet_libmd *msg_md = lntmsg->msg_md;
1978 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1980 lnet_finalize(lntmsg, 0);
1981 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1986 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1988 CERROR("Can't allocate tx for %s\n",
1989 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1990 /* Not replying will break the connection */
1995 tx->tx_gpu = msg_md ? (msg_md->md_flags & LNET_MD_FLAG_GPU) : 0;
1998 rd = &txmsg->ibm_u.putack.ibpam_rd;
1999 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
2000 niov, kiov, offset, mlen);
2002 CERROR("Can't setup PUT sink for %s: %d\n",
2003 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
2004 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2006 /* tell peer_ni it's over */
2007 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
2012 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
2013 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
2014 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
2016 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
2018 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
2019 tx->tx_waiting = 1; /* waiting for PUT_DONE */
2020 kiblnd_queue_tx(tx, conn);
2022 /* reposted buffer reserved for PUT_DONE */
2023 post_credit = IBLND_POSTRX_NO_CREDIT;
2027 case IBLND_MSG_GET_REQ:
2028 if (lntmsg != NULL) {
2029 /* Optimized GET; RDMA lntmsg's payload */
2030 kiblnd_reply(ni, rx, lntmsg);
2032 /* GET didn't match anything */
2033 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
2035 rxmsg->ibm_u.get.ibgm_cookie);
2040 kiblnd_post_rx(rx, post_credit);
2045 kiblnd_thread_fini (void)
2047 atomic_dec (&kiblnd_data.kib_nthreads);
2051 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
2053 /* This is racy, but everyone's only writing ktime_get_seconds() */
2054 peer_ni->ibp_last_alive = ktime_get_seconds();
2059 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
2062 time64_t last_alive = 0;
2063 unsigned long flags;
2065 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2067 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
2068 error = peer_ni->ibp_error;
2069 peer_ni->ibp_error = 0;
2071 last_alive = peer_ni->ibp_last_alive;
2074 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2077 struct lnet_nid nid;
2079 lnet_nid4_to_nid(peer_ni->ibp_nid, &nid);
2080 lnet_notify(peer_ni->ibp_ni, &nid,
2081 false, false, last_alive);
2086 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
2088 /* This just does the immediate housekeeping. 'error' is zero for a
2089 * normal shutdown which can happen only after the connection has been
2090 * established. If the connection is established, schedule the
2091 * connection to be finished off by the connd. Otherwise the connd is
2092 * already dealing with it (either to set it up or tear it down).
2093 * Caller holds kib_global_lock exclusively in irq context */
2094 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2095 struct kib_dev *dev;
2096 unsigned long flags;
2098 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2100 if (error != 0 && conn->ibc_comms_error == 0)
2101 conn->ibc_comms_error = error;
2103 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2104 return; /* already being handled */
2107 list_empty(&conn->ibc_tx_noops) &&
2108 list_empty(&conn->ibc_tx_queue) &&
2109 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2110 list_empty(&conn->ibc_tx_queue_nocred) &&
2111 list_empty(&conn->ibc_active_txs))
2112 CDEBUG(D_NET, "closing conn %p to %s\n",
2114 libcfs_nid2str(peer_ni->ibp_nid));
2116 CNETERR("Closing conn %p to %s: error %d%s%s%s%s%s\n",
2118 libcfs_nid2str(peer_ni->ibp_nid), error,
2119 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2120 list_empty(&conn->ibc_tx_noops) ?
2121 "" : "(sending_noops)",
2122 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2123 "" : "(sending_rsrvd)",
2124 list_empty(&conn->ibc_tx_queue_nocred) ?
2125 "" : "(sending_nocred)",
2126 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2128 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2129 if (peer_ni->ibp_next_conn == conn)
2130 /* clear next_conn so it won't be used */
2131 peer_ni->ibp_next_conn = NULL;
2132 list_del(&conn->ibc_list);
2133 /* connd (see below) takes over ibc_list's ref */
2135 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2136 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2137 kiblnd_unlink_peer_locked(peer_ni);
2139 /* set/clear error on last conn */
2140 peer_ni->ibp_error = conn->ibc_comms_error;
2143 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2146 kiblnd_dev_can_failover(dev)) {
2147 list_add_tail(&dev->ibd_fail_list,
2148 &kiblnd_data.kib_failed_devs);
2149 wake_up(&kiblnd_data.kib_failover_waitq);
2152 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2154 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2155 wake_up(&kiblnd_data.kib_connd_waitq);
2157 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2161 kiblnd_close_conn(struct kib_conn *conn, int error)
2163 unsigned long flags;
2165 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2167 kiblnd_close_conn_locked(conn, error);
2169 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2173 kiblnd_handle_early_rxs(struct kib_conn *conn)
2175 unsigned long flags;
2178 LASSERT(!in_interrupt());
2179 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2181 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2182 while ((rx = list_first_entry_or_null(&conn->ibc_early_rxs,
2184 rx_list)) != NULL) {
2185 list_del(&rx->rx_list);
2186 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2188 kiblnd_handle_rx(rx);
2190 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2192 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2196 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2202 spin_lock(&conn->ibc_lock);
2204 list_for_each_entry_safe(tx, nxt, txs, tx_list) {
2205 if (txs == &conn->ibc_active_txs) {
2206 LASSERT(!tx->tx_queued);
2207 LASSERT(tx->tx_waiting ||
2208 tx->tx_sending != 0);
2209 if (conn->ibc_comms_error == -ETIMEDOUT) {
2210 if (tx->tx_waiting && !tx->tx_sending)
2212 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2213 else if (tx->tx_sending)
2215 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2218 LASSERT(tx->tx_queued);
2219 if (conn->ibc_comms_error == -ETIMEDOUT)
2220 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2222 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2225 tx->tx_status = -ECONNABORTED;
2229 * TODO: This makes an assumption that
2230 * kiblnd_tx_complete() will be called for each tx. If
2231 * that event is dropped we could end up with stale
2232 * connections floating around. We'd like to deal with
2233 * that in a better way.
2235 * Also that means we can exceed the timeout by many
2238 if (tx->tx_sending == 0) {
2240 list_move(&tx->tx_list, &zombies);
2242 /* keep tx until cq destroy */
2243 list_move(&tx->tx_list, &conn->ibc_zombie_txs);
2248 spin_unlock(&conn->ibc_lock);
2251 * aborting transmits occurs when finalizing the connection.
2252 * The connection is finalized on error.
2253 * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2254 * override the value already set in tx->tx_hstatus above.
2256 kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2260 kiblnd_tx_may_discard(struct kib_conn *conn)
2266 spin_lock(&conn->ibc_lock);
2268 list_for_each_entry_safe(tx, nxt, &conn->ibc_zombie_txs, tx_list) {
2269 if (tx->tx_sending > 0 && tx->tx_lntmsg[0] &&
2270 lnet_md_discarded(tx->tx_lntmsg[0]->msg_md)) {
2272 if (tx->tx_sending == 0) {
2273 kiblnd_conn_decref(tx->tx_conn);
2280 spin_unlock(&conn->ibc_lock);
2285 kiblnd_finalise_conn(struct kib_conn *conn)
2287 LASSERT (!in_interrupt());
2288 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2290 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2291 * for connections that didn't get as far as being connected, because
2292 * rdma_disconnect() does this for free. */
2293 kiblnd_abort_receives(conn);
2295 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2297 /* Complete all tx descs not waiting for sends to complete.
2298 * NB we should be safe from RDMA now that the QP has changed state */
2300 CDEBUG(D_NET, "abort connection with %s\n", libcfs_nid2str(conn->ibc_peer->ibp_nid));
2302 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2303 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2304 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2305 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2306 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2308 kiblnd_handle_early_rxs(conn);
2312 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2316 unsigned long flags;
2317 enum lnet_msg_hstatus hstatus;
2319 LASSERT(error != 0);
2320 LASSERT(!in_interrupt());
2322 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2325 LASSERT(peer_ni->ibp_connecting > 0);
2326 peer_ni->ibp_connecting--;
2328 LASSERT (peer_ni->ibp_accepting > 0);
2329 peer_ni->ibp_accepting--;
2332 if (kiblnd_peer_connecting(peer_ni)) {
2333 /* another connection attempt under way... */
2334 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2339 peer_ni->ibp_reconnected = 0;
2340 if (list_empty(&peer_ni->ibp_conns)) {
2341 /* Take peer_ni's blocked transmits to complete with error */
2342 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2344 if (kiblnd_peer_active(peer_ni))
2345 kiblnd_unlink_peer_locked(peer_ni);
2347 peer_ni->ibp_error = error;
2349 /* Can't have blocked transmits if there are connections */
2350 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2353 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2355 kiblnd_peer_notify(peer_ni);
2357 if (list_empty(&zombies))
2360 CNETERR("Deleting messages for %s: connection failed\n",
2361 libcfs_nid2str(peer_ni->ibp_nid));
2366 hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
2369 hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
2372 hstatus = LNET_MSG_STATUS_LOCAL_DROPPED;
2376 kiblnd_txlist_done(&zombies, error, hstatus);
2380 kiblnd_connreq_done(struct kib_conn *conn, int status)
2382 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2385 unsigned long flags;
2388 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2390 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2391 libcfs_nid2str(peer_ni->ibp_nid), active,
2392 conn->ibc_version, status);
2394 LASSERT (!in_interrupt());
2395 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2396 peer_ni->ibp_connecting > 0) ||
2397 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2398 peer_ni->ibp_accepting > 0));
2400 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2401 conn->ibc_connvars = NULL;
2404 /* failed to establish connection */
2405 kiblnd_peer_connect_failed(peer_ni, active, status);
2406 kiblnd_finalise_conn(conn);
2410 /* connection established */
2411 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2413 conn->ibc_last_send = ktime_get();
2414 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2415 kiblnd_peer_alive(peer_ni);
2417 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2418 * peer_ni instance... */
2419 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2420 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2421 peer_ni->ibp_reconnected = 0;
2423 peer_ni->ibp_connecting--;
2425 peer_ni->ibp_accepting--;
2427 if (peer_ni->ibp_version == 0) {
2428 peer_ni->ibp_version = conn->ibc_version;
2429 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2432 if (peer_ni->ibp_version != conn->ibc_version ||
2433 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2434 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2435 conn->ibc_incarnation);
2436 peer_ni->ibp_version = conn->ibc_version;
2437 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2440 /* grab pending txs while I have the lock */
2441 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2443 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2444 conn->ibc_comms_error != 0) { /* error has happened already */
2446 /* start to shut down connection */
2447 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2448 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2450 kiblnd_txlist_done(&txs, -ECONNABORTED,
2451 LNET_MSG_STATUS_LOCAL_ERROR);
2456 /* +1 ref for myself, this connection is visible to other threads
2457 * now, refcount of peer:ibp_conns can be released by connection
2458 * close from either a different thread, or the calling of
2459 * kiblnd_check_sends_locked() below. See bz21911 for details.
2461 kiblnd_conn_addref(conn);
2462 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2464 /* Schedule blocked txs
2465 * Note: if we are running with conns_per_peer > 1, these blocked
2466 * txs will all get scheduled to the first connection which gets
2467 * scheduled. We won't be using round robin on this first batch.
2469 spin_lock(&conn->ibc_lock);
2470 while ((tx = list_first_entry_or_null(&txs, struct kib_tx,
2471 tx_list)) != NULL) {
2472 list_del(&tx->tx_list);
2474 kiblnd_queue_tx_locked(tx, conn);
2476 kiblnd_check_sends_locked(conn);
2477 spin_unlock(&conn->ibc_lock);
2479 /* schedule blocked rxs */
2480 kiblnd_handle_early_rxs(conn);
2481 kiblnd_conn_decref(conn);
2485 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2489 #ifdef HAVE_RDMA_REJECT_4ARGS
2490 rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2492 rc = rdma_reject(cmid, rej, sizeof(*rej));
2496 CWARN("Error %d sending reject\n", rc);
2500 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2502 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2503 struct kib_msg *reqmsg = priv;
2504 struct kib_msg *ackmsg;
2505 struct kib_dev *ibdev;
2506 struct kib_peer_ni *peer_ni;
2507 struct kib_peer_ni *peer2;
2508 struct kib_conn *conn;
2509 struct lnet_ni *ni = NULL;
2510 struct kib_net *net = NULL;
2511 struct lnet_nid destnid;
2513 struct rdma_conn_param cp;
2515 int version = IBLND_MSG_VERSION;
2516 unsigned long flags;
2518 struct sockaddr_in *peer_addr;
2520 LASSERT(!in_interrupt());
2521 /* cmid inherits 'context' from the corresponding listener id */
2522 ibdev = cmid->context;
2525 memset(&rej, 0, sizeof(rej));
2526 rej.ibr_magic = IBLND_MSG_MAGIC;
2527 rej.ibr_why = IBLND_REJECT_FATAL;
2528 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2530 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2531 if (*kiblnd_tunables.kib_require_priv_port &&
2532 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2533 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2534 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2535 &ip, ntohs(peer_addr->sin_port));
2539 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2540 CERROR("Short connection request\n");
2544 /* Future protocol version compatibility support! If the
2545 * o2iblnd-specific protocol changes, or when LNET unifies
2546 * protocols over all LNDs, the initial connection will
2547 * negotiate a protocol version. I trap this here to avoid
2548 * console errors; the reject tells the peer_ni which protocol I
2550 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2551 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2553 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2554 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2555 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2557 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2558 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2559 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2562 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2564 CERROR("Can't parse connection request: %d\n", rc);
2568 nid = reqmsg->ibm_srcnid;
2569 lnet_nid4_to_nid(reqmsg->ibm_dstnid, &destnid);
2570 ni = lnet_nid_to_ni_addref(&destnid);
2573 net = (struct kib_net *)ni->ni_data;
2574 rej.ibr_incarnation = net->ibn_incarnation;
2576 if (ibdev->ibd_nnets == 0) {
2577 rej.ibr_why = IBLND_REJECT_EARLY;
2578 CNETERR("Can't accept conn from %s on %s (%s:%d:%pI4h): net for nid %s not added yet\n",
2579 libcfs_nid2str(nid),
2580 libcfs_nidstr(&net->ibn_ni->ni_nid),
2581 ibdev->ibd_ifname, ibdev->ibd_nnets,
2583 libcfs_nid2str(reqmsg->ibm_dstnid));
2586 list_for_each_entry(net, &ibdev->ibd_nets, ibn_list) {
2587 if ((net->ibn_dev == ibdev) &&
2588 (net->ibn_ni != NULL) &&
2589 (net->ibn_ni->ni_state != LNET_NI_STATE_ACTIVE)) {
2590 rej.ibr_why = IBLND_REJECT_EARLY;
2591 CNETERR("Can't accept conn from %s on %s (%s:%d:%pI4h): nid %s not ready\n",
2592 libcfs_nid2str(nid),
2593 libcfs_nidstr(&net->ibn_ni->ni_nid),
2594 ibdev->ibd_ifname, ibdev->ibd_nnets,
2596 libcfs_nid2str(reqmsg->ibm_dstnid));
2602 if (ni == NULL || /* no matching net */
2603 !nid_same(&ni->ni_nid, &destnid) || /* right NET, wrong NID! */
2604 net->ibn_dev != ibdev) { /* wrong device */
2605 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): bad dst nid %s\n",
2606 libcfs_nid2str(nid),
2607 ni ? libcfs_nidstr(&ni->ni_nid) : "NA",
2608 ibdev->ibd_ifname, ibdev->ibd_nnets,
2610 libcfs_nid2str(reqmsg->ibm_dstnid));
2615 /* check time stamp as soon as possible */
2616 if (reqmsg->ibm_dststamp != 0 &&
2617 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2618 CWARN("Stale connection request\n");
2619 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2623 /* I can accept peer_ni's version */
2624 version = reqmsg->ibm_version;
2626 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2627 CERROR("Unexpected connreq msg type: %x from %s\n",
2628 reqmsg->ibm_type, libcfs_nid2str(nid));
2632 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2633 kiblnd_msg_queue_size(version, ni)) {
2634 CERROR("Can't accept conn from %s, queue depth too large: %d (<=%d wanted)\n",
2635 libcfs_nid2str(nid),
2636 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2637 kiblnd_msg_queue_size(version, ni));
2639 if (version == IBLND_MSG_VERSION)
2640 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2645 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2646 IBLND_MAX_RDMA_FRAGS) {
2647 CWARN("Can't accept conn from %s (version %x): max_frags %d too large (%d wanted)\n",
2648 libcfs_nid2str(nid), version,
2649 reqmsg->ibm_u.connparams.ibcp_max_frags,
2650 IBLND_MAX_RDMA_FRAGS);
2652 if (version >= IBLND_MSG_VERSION)
2653 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2656 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2657 IBLND_MAX_RDMA_FRAGS &&
2658 net->ibn_fmr_ps == NULL) {
2659 CWARN("Can't accept conn from %s (version %x): max_frags %d incompatible without FMR pool (%d wanted)\n",
2660 libcfs_nid2str(nid), version,
2661 reqmsg->ibm_u.connparams.ibcp_max_frags,
2662 IBLND_MAX_RDMA_FRAGS);
2664 if (version == IBLND_MSG_VERSION)
2665 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2670 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2671 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2672 libcfs_nid2str(nid),
2673 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2678 /* assume 'nid' is a new peer_ni; create */
2679 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2681 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2682 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2686 /* We have validated the peer's parameters so use those */
2687 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2688 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2690 write_lock_irqsave(g_lock, flags);
2692 peer2 = kiblnd_find_peer_locked(ni, nid);
2693 if (peer2 != NULL) {
2694 if (peer2->ibp_version == 0) {
2695 peer2->ibp_version = version;
2696 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2699 /* not the guy I've talked with */
2700 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2701 peer2->ibp_version != version) {
2702 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2704 if (kiblnd_peer_active(peer2)) {
2705 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2706 peer2->ibp_version = version;
2708 write_unlock_irqrestore(g_lock, flags);
2710 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2711 libcfs_nid2str(nid), peer2->ibp_version, version,
2712 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2714 kiblnd_peer_decref(peer_ni);
2715 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2719 /* Tie-break connection race in favour of the higher NID.
2720 * If we keep running into a race condition multiple times,
2721 * we have to assume that the connection attempt with the
2722 * higher NID is stuck in a connecting state and will never
2723 * recover. As such, we pass through this if-block and let
2724 * the lower NID connection win so we can move forward.
2726 if (peer2->ibp_connecting != 0 &&
2727 nid < lnet_nid_to_nid4(&ni->ni_nid) &&
2728 peer2->ibp_races < MAX_CONN_RACES_BEFORE_ABORT) {
2730 write_unlock_irqrestore(g_lock, flags);
2732 CDEBUG(D_NET, "Conn race %s\n",
2733 libcfs_nid2str(peer2->ibp_nid));
2735 kiblnd_peer_decref(peer_ni);
2736 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2739 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2740 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2741 libcfs_nid2str(peer2->ibp_nid),
2742 MAX_CONN_RACES_BEFORE_ABORT);
2744 * passive connection is allowed even this peer_ni is waiting for
2747 peer2->ibp_reconnecting = 0;
2748 peer2->ibp_races = 0;
2749 peer2->ibp_accepting++;
2750 kiblnd_peer_addref(peer2);
2752 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2753 * so copy validated parameters since we now know what the
2754 * peer_ni's limits are */
2755 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2756 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2758 write_unlock_irqrestore(g_lock, flags);
2759 kiblnd_peer_decref(peer_ni);
2762 /* Brand new peer_ni */
2763 LASSERT(peer_ni->ibp_accepting == 0);
2764 LASSERT(peer_ni->ibp_version == 0 &&
2765 peer_ni->ibp_incarnation == 0);
2767 peer_ni->ibp_accepting = 1;
2768 peer_ni->ibp_version = version;
2769 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2771 /* I have a ref on ni that prevents it being shutdown */
2772 LASSERT(net->ibn_shutdown == 0);
2774 kiblnd_peer_addref(peer_ni);
2775 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
2777 write_unlock_irqrestore(g_lock, flags);
2780 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT,
2783 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2784 kiblnd_peer_decref(peer_ni);
2785 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2789 /* conn now "owns" cmid, so I return success from here on to ensure the
2790 * CM callback doesn't destroy cmid.
2792 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2793 conn->ibc_credits = conn->ibc_queue_depth;
2794 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2795 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2796 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2798 ackmsg = &conn->ibc_connvars->cv_msg;
2799 memset(ackmsg, 0, sizeof(*ackmsg));
2801 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2802 sizeof(ackmsg->ibm_u.connparams));
2803 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2804 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2805 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2807 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2809 memset(&cp, 0, sizeof(cp));
2810 cp.private_data = ackmsg;
2811 cp.private_data_len = ackmsg->ibm_nob;
2812 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2813 cp.initiator_depth = 0;
2814 cp.flow_control = 1;
2815 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2816 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2818 CDEBUG(D_NET, "Accept %s conn %p\n", libcfs_nid2str(nid), conn);
2820 rc = rdma_accept(cmid, &cp);
2822 CNETERR("Can't accept %s: %d cm_id %p\n", libcfs_nid2str(nid), rc, cmid);
2823 rej.ibr_version = version;
2824 rej.ibr_why = IBLND_REJECT_FATAL;
2826 kiblnd_reject(cmid, &rej);
2827 kiblnd_connreq_done(conn, rc);
2828 kiblnd_conn_decref(conn);
2836 rej.ibr_cp.ibcp_queue_depth =
2837 kiblnd_msg_queue_size(version, ni);
2838 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2842 rej.ibr_version = version;
2843 kiblnd_reject(cmid, &rej);
2845 return -ECONNREFUSED;
2849 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2850 u64 incarnation, int why, struct kib_connparams *cp)
2852 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2853 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2855 int msg_size = IBLND_MSG_SIZE;
2859 unsigned long flags;
2861 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2862 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2865 msg_size = cp->ibcp_max_msg_size;
2866 frag_num = cp->ibcp_max_frags;
2867 queue_dep = cp->ibcp_queue_depth;
2870 write_lock_irqsave(glock, flags);
2871 /* retry connection if it's still needed and no other connection
2872 * attempts (active or passive) are in progress
2873 * NB: reconnect is still needed even when ibp_tx_queue is
2874 * empty if ibp_version != version because reconnect may be
2877 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2878 peer_ni->ibp_version != version) &&
2879 peer_ni->ibp_connecting &&
2880 peer_ni->ibp_accepting == 0;
2891 case IBLND_REJECT_RDMA_FRAGS: {
2893 reason = "can't negotiate max frags";
2897 if (conn->ibc_max_frags <= frag_num) {
2898 reason = "unsupported max frags";
2902 peer_ni->ibp_max_frags = frag_num;
2903 reason = "rdma fragments";
2906 case IBLND_REJECT_MSG_QUEUE_SIZE:
2908 reason = "can't negotiate queue depth";
2911 if (conn->ibc_queue_depth <= queue_dep) {
2912 reason = "unsupported queue depth";
2916 peer_ni->ibp_queue_depth = queue_dep;
2917 reason = "queue depth";
2920 case IBLND_REJECT_CONN_STALE:
2924 case IBLND_REJECT_CONN_RACE:
2925 reason = "conn race";
2928 case IBLND_REJECT_CONN_UNCOMPAT:
2929 reason = "version negotiation";
2933 conn->ibc_reconnect = 1;
2934 peer_ni->ibp_reconnecting++;
2935 peer_ni->ibp_version = version;
2936 if (incarnation != 0)
2937 peer_ni->ibp_incarnation = incarnation;
2939 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2941 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2942 libcfs_nid2str(peer_ni->ibp_nid),
2943 reconnect ? "reconnect" : "don't reconnect",
2944 reason, IBLND_MSG_VERSION, version, msg_size,
2945 conn->ibc_queue_depth, queue_dep,
2946 conn->ibc_max_frags, frag_num);
2948 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2949 * while destroying the zombie
2954 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2956 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2957 int status = -ECONNREFUSED;
2959 LASSERT (!in_interrupt());
2960 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2963 case IB_CM_REJ_STALE_CONN:
2964 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2965 IBLND_REJECT_CONN_STALE, NULL);
2968 case IB_CM_REJ_INVALID_SERVICE_ID:
2969 status = -EHOSTUNREACH;
2970 CNETERR("%s rejected: no listener at %d\n",
2971 libcfs_nid2str(peer_ni->ibp_nid),
2972 *kiblnd_tunables.kib_service);
2975 case IB_CM_REJ_CONSUMER_DEFINED:
2976 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2977 struct kib_rej *rej = priv;
2978 struct kib_connparams *cp = NULL;
2980 __u64 incarnation = -1;
2982 /* NB. default incarnation is -1 because:
2983 * a) V1 will ignore dst incarnation in connreq.
2984 * b) V2 will provide incarnation while rejecting me,
2985 * -1 will be overwrote.
2987 * if I try to connect to a V1 peer_ni with V2 protocol,
2988 * it rejected me then upgrade to V2, I have no idea
2989 * about the upgrading and try to reconnect with V1,
2990 * in this case upgraded V2 can find out I'm trying to
2991 * talk to the old guy and reject me(incarnation is -1).
2994 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2995 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2996 __swab32s(&rej->ibr_magic);
2997 __swab16s(&rej->ibr_version);
3001 if (priv_nob >= sizeof(struct kib_rej) &&
3002 rej->ibr_version > IBLND_MSG_VERSION_1) {
3003 /* priv_nob is always 148 in current version
3004 * of OFED, so we still need to check version.
3005 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE)
3010 __swab64s(&rej->ibr_incarnation);
3011 __swab16s(&cp->ibcp_queue_depth);
3012 __swab16s(&cp->ibcp_max_frags);
3013 __swab32s(&cp->ibcp_max_msg_size);
3016 incarnation = rej->ibr_incarnation;
3019 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
3020 rej->ibr_magic != LNET_PROTO_MAGIC) {
3021 CERROR("%s rejected: consumer defined fatal error\n",
3022 libcfs_nid2str(peer_ni->ibp_nid));
3026 if (rej->ibr_version != IBLND_MSG_VERSION &&
3027 rej->ibr_version != IBLND_MSG_VERSION_1) {
3028 CERROR("%s rejected: o2iblnd version %x error\n",
3029 libcfs_nid2str(peer_ni->ibp_nid),
3034 if (rej->ibr_why == IBLND_REJECT_FATAL &&
3035 rej->ibr_version == IBLND_MSG_VERSION_1) {
3036 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
3037 libcfs_nid2str(peer_ni->ibp_nid),
3040 if (conn->ibc_version != IBLND_MSG_VERSION_1)
3041 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
3044 switch (rej->ibr_why) {
3045 case IBLND_REJECT_CONN_RACE:
3046 case IBLND_REJECT_CONN_STALE:
3047 case IBLND_REJECT_CONN_UNCOMPAT:
3048 case IBLND_REJECT_MSG_QUEUE_SIZE:
3049 case IBLND_REJECT_RDMA_FRAGS:
3050 kiblnd_check_reconnect(conn, rej->ibr_version,
3055 case IBLND_REJECT_NO_RESOURCES:
3056 CERROR("%s rejected: o2iblnd no resources\n",
3057 libcfs_nid2str(peer_ni->ibp_nid));
3060 case IBLND_REJECT_FATAL:
3061 CERROR("%s rejected: o2iblnd fatal error\n",
3062 libcfs_nid2str(peer_ni->ibp_nid));
3065 case IBLND_REJECT_EARLY:
3066 CNETERR("%s rejected: tried too early\n",
3067 libcfs_nid2str(peer_ni->ibp_nid));
3071 CERROR("%s rejected: o2iblnd reason %d\n",
3072 libcfs_nid2str(peer_ni->ibp_nid),
3080 CNETERR("%s rejected: reason %d, size %d\n",
3081 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
3085 kiblnd_connreq_done(conn, status);
3089 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
3091 struct kib_peer_ni *peer_ni = conn->ibc_peer;
3092 struct lnet_ni *ni = peer_ni->ibp_ni;
3093 struct kib_net *net = ni->ni_data;
3094 struct kib_msg *msg = priv;
3095 int ver = conn->ibc_version;
3096 int rc = kiblnd_unpack_msg(msg, priv_nob);
3097 unsigned long flags;
3099 LASSERT (net != NULL);
3102 CERROR("Can't unpack connack from %s: %d\n",
3103 libcfs_nid2str(peer_ni->ibp_nid), rc);
3107 if (msg->ibm_type != IBLND_MSG_CONNACK) {
3108 CERROR("Unexpected message %d from %s\n",
3109 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
3114 if (ver != msg->ibm_version) {
3115 CERROR("%s replied version %x is different with "
3116 "requested version %x\n",
3117 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
3122 if (msg->ibm_u.connparams.ibcp_queue_depth >
3123 conn->ibc_queue_depth) {
3124 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
3125 libcfs_nid2str(peer_ni->ibp_nid),
3126 msg->ibm_u.connparams.ibcp_queue_depth,
3127 conn->ibc_queue_depth);
3132 if (msg->ibm_u.connparams.ibcp_max_frags >
3133 conn->ibc_max_frags) {
3134 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3135 libcfs_nid2str(peer_ni->ibp_nid),
3136 msg->ibm_u.connparams.ibcp_max_frags,
3137 conn->ibc_max_frags);
3142 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3143 CERROR("%s max message size %d too big (%d max)\n",
3144 libcfs_nid2str(peer_ni->ibp_nid),
3145 msg->ibm_u.connparams.ibcp_max_msg_size,
3151 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3152 if (msg->ibm_dstnid == lnet_nid_to_nid4(&ni->ni_nid) &&
3153 msg->ibm_dststamp == net->ibn_incarnation)
3157 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3160 CERROR("Bad connection reply from %s, rc = %d, "
3161 "version: %x max_frags: %d\n",
3162 libcfs_nid2str(peer_ni->ibp_nid), rc,
3163 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3167 conn->ibc_incarnation = msg->ibm_srcstamp;
3168 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3169 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3170 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3171 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3172 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3173 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3175 kiblnd_connreq_done(conn, 0);
3179 /* NB My QP has already established itself, so I handle anything going
3180 * wrong here by setting ibc_comms_error.
3181 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3182 * immediately tears it down. */
3185 conn->ibc_comms_error = rc;
3186 kiblnd_connreq_done(conn, 0);
3190 kiblnd_active_connect(struct rdma_cm_id *cmid)
3192 struct kib_peer_ni *peer_ni = cmid->context;
3193 struct kib_conn *conn;
3194 struct kib_msg *msg;
3195 struct rdma_conn_param cp;
3198 unsigned long flags;
3201 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3203 incarnation = peer_ni->ibp_incarnation;
3204 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3205 peer_ni->ibp_version;
3207 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3209 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3212 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3213 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3217 /* conn "owns" cmid now, so I return success from here on to ensure the
3218 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3221 msg = &conn->ibc_connvars->cv_msg;
3223 memset(msg, 0, sizeof(*msg));
3224 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3225 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3226 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3227 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3229 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3230 0, peer_ni->ibp_nid, incarnation);
3232 memset(&cp, 0, sizeof(cp));
3233 cp.private_data = msg;
3234 cp.private_data_len = msg->ibm_nob;
3235 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3236 cp.initiator_depth = 0;
3237 cp.flow_control = 1;
3238 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3239 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3241 LASSERT(cmid->context == (void *)conn);
3242 LASSERT(conn->ibc_cmid == cmid);
3243 rc = rdma_connect_locked(cmid, &cp);
3245 CNETERR("Can't connect to %s: %d cm_id %p\n",
3246 libcfs_nid2str(peer_ni->ibp_nid), rc, cmid);
3247 kiblnd_connreq_done(conn, rc);
3248 kiblnd_conn_decref(conn);
3250 CDEBUG(D_NET, "Connected to %s: cm_id %p\n",
3251 libcfs_nid2str(peer_ni->ibp_nid), cmid);
3258 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3260 struct kib_peer_ni *peer_ni;
3261 struct kib_conn *conn;
3264 switch (event->event) {
3266 CERROR("Unexpected event: %d, status: %d\n",
3267 event->event, event->status);
3270 case RDMA_CM_EVENT_CONNECT_REQUEST:
3271 /* destroy cmid on failure */
3272 rc = kiblnd_passive_connect(cmid,
3273 (void *)KIBLND_CONN_PARAM(event),
3274 KIBLND_CONN_PARAM_LEN(event));
3275 CDEBUG(D_NET, "connreq: %d cm_id %p\n", rc, cmid);
3278 case RDMA_CM_EVENT_ADDR_ERROR:
3279 peer_ni = cmid->context;
3280 CNETERR("%s: ADDR ERROR %d cm_id %p\n",
3281 libcfs_nid2str(peer_ni->ibp_nid), event->status, cmid);
3282 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3283 kiblnd_peer_decref(peer_ni);
3284 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3286 case RDMA_CM_EVENT_ADDR_RESOLVED:
3287 peer_ni = cmid->context;
3289 CDEBUG(D_NET, "%s Addr resolved: %d cm_id %p\n",
3290 libcfs_nid2str(peer_ni->ibp_nid), event->status, cmid);
3292 if (event->status != 0) {
3293 CNETERR("Can't resolve address for %s: %d cm_id %p\n",
3294 libcfs_nid2str(peer_ni->ibp_nid),
3295 event->status, cmid);
3298 rc = rdma_resolve_route(
3299 cmid, kiblnd_timeout() * 1000);
3301 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3302 struct kib_dev *dev = net->ibn_dev;
3304 CDEBUG(D_NET, "%s: connection bound to "\
3306 libcfs_nid2str(peer_ni->ibp_nid),
3308 &dev->ibd_ifip, cmid->device->name);
3313 /* Can't initiate route resolution */
3314 CNETERR("Can't resolve route for %s: %d cm_id %p\n",
3315 libcfs_nid2str(peer_ni->ibp_nid), rc, cmid);
3317 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3318 kiblnd_peer_decref(peer_ni);
3319 return rc; /* rc != 0 destroys cmid */
3321 case RDMA_CM_EVENT_ROUTE_ERROR:
3322 peer_ni = cmid->context;
3323 CNETERR("%s: ROUTE ERROR %d cm_id %p\n",
3324 libcfs_nid2str(peer_ni->ibp_nid), event->status, cmid);
3325 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3326 kiblnd_peer_decref(peer_ni);
3327 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3329 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3330 peer_ni = cmid->context;
3331 CDEBUG(D_NET,"%s Route resolved: %d\n",
3332 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3334 if (event->status == 0)
3335 return kiblnd_active_connect(cmid);
3337 CNETERR("Can't resolve route for %s: %d cm_id %p\n",
3338 libcfs_nid2str(peer_ni->ibp_nid), event->status, cmid);
3339 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3340 kiblnd_peer_decref(peer_ni);
3341 return event->status; /* rc != 0 destroys cmid */
3343 case RDMA_CM_EVENT_UNREACHABLE:
3344 conn = cmid->context;
3345 CNETERR("%s: UNREACHABLE %d cm_id %p conn %p\n",
3346 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status, cmid, conn);
3347 LASSERT(conn->ibc_state != IBLND_CONN_ESTABLISHED &&
3348 conn->ibc_state != IBLND_CONN_INIT);
3349 if (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3350 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT) {
3351 kiblnd_connreq_done(conn, -ENETDOWN);
3352 kiblnd_conn_decref(conn);
3356 case RDMA_CM_EVENT_CONNECT_ERROR:
3357 conn = cmid->context;
3358 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3359 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3360 CNETERR("%s: CONNECT ERROR %d cm_id %p conn %p\n",
3361 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status, cmid, conn);
3362 kiblnd_connreq_done(conn, -ENOTCONN);
3363 kiblnd_conn_decref(conn);
3366 case RDMA_CM_EVENT_REJECTED:
3367 conn = cmid->context;
3368 switch (conn->ibc_state) {
3372 case IBLND_CONN_PASSIVE_WAIT:
3373 CERROR("%s: REJECTED %d cm_id %p\n",
3374 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3375 event->status, cmid);
3376 kiblnd_connreq_done(conn, -ECONNRESET);
3379 case IBLND_CONN_ACTIVE_CONNECT:
3380 kiblnd_rejected(conn, event->status,
3381 (void *)KIBLND_CONN_PARAM(event),
3382 KIBLND_CONN_PARAM_LEN(event));
3385 kiblnd_conn_decref(conn);
3388 case RDMA_CM_EVENT_ESTABLISHED:
3389 conn = cmid->context;
3390 switch (conn->ibc_state) {
3394 case IBLND_CONN_PASSIVE_WAIT:
3395 CDEBUG(D_NET, "ESTABLISHED (passive): %s cm_id %p conn %p\n",
3396 libcfs_nid2str(conn->ibc_peer->ibp_nid), cmid, conn);
3397 kiblnd_connreq_done(conn, 0);
3400 case IBLND_CONN_ACTIVE_CONNECT:
3401 CDEBUG(D_NET, "ESTABLISHED(active): %s cm_id %p conn %p\n",
3402 libcfs_nid2str(conn->ibc_peer->ibp_nid), cmid, conn);
3403 kiblnd_check_connreply(conn,
3404 (void *)KIBLND_CONN_PARAM(event),
3405 KIBLND_CONN_PARAM_LEN(event));
3408 /* net keeps its ref on conn! */
3411 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3412 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3415 case RDMA_CM_EVENT_DISCONNECTED:
3416 conn = cmid->context;
3417 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3418 CERROR("%s DISCONNECTED cm_id %p conn %p\n",
3419 libcfs_nid2str(conn->ibc_peer->ibp_nid), cmid, conn);
3420 kiblnd_connreq_done(conn, -ECONNRESET);
3422 kiblnd_close_conn(conn, 0);
3424 kiblnd_conn_decref(conn);
3425 cmid->context = NULL;
3428 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3429 LCONSOLE_ERROR_MSG(0x131,
3430 "Received notification of device removal\n"
3431 "Please shutdown LNET to allow this to proceed\n");
3432 /* Can't remove network from underneath LNET for now, so I have
3436 case RDMA_CM_EVENT_ADDR_CHANGE:
3437 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3443 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3447 list_for_each_entry(tx, txs, tx_list) {
3448 if (txs != &conn->ibc_active_txs) {
3449 LASSERT(tx->tx_queued);
3451 LASSERT(!tx->tx_queued);
3452 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3455 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3456 CERROR("Timed out tx: %s(WSQ:%d%d%d), %lld seconds\n",
3457 kiblnd_queue2str(conn, txs),
3458 tx->tx_waiting, tx->tx_sending, tx->tx_queued,
3460 ktime_ms_delta(ktime_get(),
3461 tx->tx_deadline) / MSEC_PER_SEC);
3470 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3472 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3473 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3474 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3475 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3476 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3480 kiblnd_check_conns (int idx)
3483 LIST_HEAD(checksends);
3484 LIST_HEAD(timedout_txs);
3485 struct hlist_head *peers = &kiblnd_data.kib_peers[idx];
3486 struct kib_peer_ni *peer_ni;
3487 struct kib_conn *conn;
3488 struct kib_tx *tx, *tx_tmp;
3489 unsigned long flags;
3491 /* NB. We expect to have a look at all the peers and not find any
3492 * RDMAs to time out, so we just use a shared lock while we
3495 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3497 hlist_for_each_entry(peer_ni, peers, ibp_list) {
3498 /* Check tx_deadline */
3499 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3500 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3501 CWARN("Timed out tx for %s: %lld seconds\n",
3502 libcfs_nid2str(peer_ni->ibp_nid),
3503 ktime_ms_delta(ktime_get(),
3504 tx->tx_deadline) / MSEC_PER_SEC);
3505 list_move(&tx->tx_list, &timedout_txs);
3509 list_for_each_entry(conn, &peer_ni->ibp_conns, ibc_list) {
3513 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3515 spin_lock(&conn->ibc_lock);
3517 sendnoop = kiblnd_need_noop(conn);
3518 timedout = kiblnd_conn_timed_out_locked(conn);
3519 if (!sendnoop && !timedout) {
3520 spin_unlock(&conn->ibc_lock);
3525 CERROR("Timed out RDMA with %s (%lld): c: %u, oc: %u, rc: %u\n",
3526 libcfs_nid2str(peer_ni->ibp_nid),
3528 - peer_ni->ibp_last_alive,
3530 conn->ibc_outstanding_credits,
3531 conn->ibc_reserved_credits);
3532 #ifdef O2IBLND_CONN_STATE_DEBUG
3533 kiblnd_dump_conn_dbg(conn);
3535 list_add(&conn->ibc_connd_list, &closes);
3537 list_add(&conn->ibc_connd_list, &checksends);
3539 /* +ref for 'closes' or 'checksends' */
3540 kiblnd_conn_addref(conn);
3542 spin_unlock(&conn->ibc_lock);
3546 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3548 if (!list_empty(&timedout_txs))
3549 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3550 LNET_MSG_STATUS_NETWORK_TIMEOUT);
3552 /* Handle timeout by closing the whole
3553 * connection. We can only be sure RDMA activity
3554 * has ceased once the QP has been modified.
3556 while ((conn = list_first_entry_or_null(&closes,
3558 ibc_connd_list)) != NULL) {
3559 list_del(&conn->ibc_connd_list);
3560 kiblnd_close_conn(conn, -ETIMEDOUT);
3561 kiblnd_conn_decref(conn);
3564 /* In case we have enough credits to return via a
3565 * NOOP, but there were no non-blocking tx descs
3566 * free to do it last time...
3568 while ((conn = list_first_entry_or_null(&checksends,
3570 ibc_connd_list)) != NULL) {
3571 list_del(&conn->ibc_connd_list);
3573 spin_lock(&conn->ibc_lock);
3574 kiblnd_check_sends_locked(conn);
3575 spin_unlock(&conn->ibc_lock);
3577 kiblnd_conn_decref(conn);
3582 kiblnd_disconnect_conn(struct kib_conn *conn)
3584 LASSERT (!in_interrupt());
3585 LASSERT (current == kiblnd_data.kib_connd);
3586 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3587 #ifdef O2IBLND_CONN_STATE_DEBUG
3588 kiblnd_dump_conn_dbg(conn);
3590 rdma_disconnect(conn->ibc_cmid);
3591 kiblnd_finalise_conn(conn);
3593 kiblnd_peer_notify(conn->ibc_peer);
3597 * High-water for reconnection to the same peer_ni, reconnection attempt should
3598 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3600 #define KIB_RECONN_HIGH_RACE 10
3602 * Allow connd to take a break and handle other things after consecutive
3603 * reconnection attemps.
3605 #define KIB_RECONN_BREAK 100
3608 kiblnd_connd (void *arg)
3610 spinlock_t *lock = &kiblnd_data.kib_connd_lock;
3611 wait_queue_entry_t wait;
3612 unsigned long flags;
3613 struct kib_conn *conn;
3618 unsigned long deadline = jiffies;
3621 kiblnd_data.kib_connd = current;
3623 spin_lock_irqsave(lock, flags);
3625 while (!kiblnd_data.kib_shutdown) {
3628 dropped_lock = false;
3630 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_zombies,
3631 struct kib_conn, ibc_list);
3633 struct kib_peer_ni *peer_ni = NULL;
3635 list_del(&conn->ibc_list);
3636 if (conn->ibc_reconnect) {
3637 peer_ni = conn->ibc_peer;
3638 kiblnd_peer_addref(peer_ni);
3641 spin_unlock_irqrestore(lock, flags);
3642 dropped_lock = true;
3644 kiblnd_destroy_conn(conn);
3646 spin_lock_irqsave(lock, flags);
3648 LIBCFS_FREE(conn, sizeof(*conn));
3652 conn->ibc_peer = peer_ni;
3653 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3654 list_add_tail(&conn->ibc_list,
3655 &kiblnd_data.kib_reconn_list);
3657 list_add_tail(&conn->ibc_list,
3658 &kiblnd_data.kib_reconn_wait);
3661 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_conns,
3662 struct kib_conn, ibc_list);
3666 list_del(&conn->ibc_list);
3668 spin_unlock_irqrestore(lock, flags);
3669 dropped_lock = true;
3671 kiblnd_disconnect_conn(conn);
3672 wait = conn->ibc_waits;
3673 if (wait == 0) /* keep ref for connd_wait, see below */
3674 kiblnd_conn_decref(conn);
3676 spin_lock_irqsave(lock, flags);
3679 list_add_tail(&conn->ibc_list,
3680 &kiblnd_data.kib_connd_waits);
3683 while (reconn < KIB_RECONN_BREAK) {
3684 if (kiblnd_data.kib_reconn_sec !=
3685 ktime_get_real_seconds()) {
3686 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3687 list_splice_init(&kiblnd_data.kib_reconn_wait,
3688 &kiblnd_data.kib_reconn_list);
3691 conn = list_first_entry_or_null(&kiblnd_data.kib_reconn_list,
3692 struct kib_conn, ibc_list);
3696 list_del(&conn->ibc_list);
3698 spin_unlock_irqrestore(lock, flags);
3699 dropped_lock = true;
3701 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3702 kiblnd_peer_decref(conn->ibc_peer);
3703 LIBCFS_FREE(conn, sizeof(*conn));
3705 spin_lock_irqsave(lock, flags);
3708 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_waits,
3709 struct kib_conn, ibc_list);
3711 list_del(&conn->ibc_list);
3712 spin_unlock_irqrestore(lock, flags);
3714 dropped_lock = kiblnd_tx_may_discard(conn);
3716 kiblnd_conn_decref(conn);
3718 spin_lock_irqsave(lock, flags);
3720 list_add_tail(&conn->ibc_list,
3721 &kiblnd_data.kib_connd_waits);
3724 /* careful with the jiffy wrap... */
3725 timeout = (int)(deadline - jiffies);
3729 int chunk = HASH_SIZE(kiblnd_data.kib_peers);
3730 unsigned int lnd_timeout;
3732 spin_unlock_irqrestore(lock, flags);
3733 dropped_lock = true;
3735 /* Time to check for RDMA timeouts on a few more
3736 * peers: I do checks every 'p' seconds on a
3737 * proportion of the peer_ni table and I need to check
3738 * every connection 'n' times within a timeout
3739 * interval, to ensure I detect a timeout on any
3740 * connection within (n+1)/n times the timeout
3744 lnd_timeout = kiblnd_timeout();
3745 if (lnd_timeout > n * p)
3746 chunk = (chunk * n * p) / lnd_timeout;
3750 for (i = 0; i < chunk; i++) {
3751 kiblnd_check_conns(peer_index);
3752 peer_index = (peer_index + 1) %
3753 HASH_SIZE(kiblnd_data.kib_peers);
3756 deadline += cfs_time_seconds(p);
3757 spin_lock_irqsave(lock, flags);
3763 /* Nothing to do for 'timeout' */
3764 set_current_state(TASK_INTERRUPTIBLE);
3765 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3766 spin_unlock_irqrestore(lock, flags);
3768 schedule_timeout(timeout);
3770 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3771 spin_lock_irqsave(lock, flags);
3774 spin_unlock_irqrestore(lock, flags);
3776 kiblnd_thread_fini();
3781 kiblnd_qp_event(struct ib_event *event, void *arg)
3783 struct kib_conn *conn = arg;
3785 switch (event->event) {
3786 case IB_EVENT_COMM_EST:
3787 CDEBUG(D_NET, "%s established\n",
3788 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3789 /* We received a packet but connection isn't established
3790 * probably handshake packet was lost, so free to
3791 * force make connection established */
3792 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3795 case IB_EVENT_PORT_ERR:
3796 case IB_EVENT_DEVICE_FATAL:
3797 CERROR("Fatal device error for NI %s\n",
3798 libcfs_nidstr(&conn->ibc_peer->ibp_ni->ni_nid));
3799 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3802 case IB_EVENT_PORT_ACTIVE:
3803 CERROR("Port reactivated for NI %s\n",
3804 libcfs_nidstr(&conn->ibc_peer->ibp_ni->ni_nid));
3805 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3809 CERROR("%s: Async QP event type %d\n",
3810 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3816 kiblnd_complete (struct ib_wc *wc)
3818 switch (kiblnd_wreqid2type(wc->wr_id)) {
3823 if (wc->status != IB_WC_SUCCESS &&
3824 wc->status != IB_WC_WR_FLUSH_ERR)
3825 CNETERR("FastReg failed: %d\n", wc->status);
3828 case IBLND_WID_RDMA:
3829 /* We only get RDMA completion notification if it fails. All
3830 * subsequent work items, including the final SEND will fail
3831 * too. However we can't print out any more info about the
3832 * failing RDMA because 'tx' might be back on the idle list or
3833 * even reused already if we didn't manage to post all our work
3835 CNETERR("RDMA (tx: %p) failed: %d\n",
3836 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3840 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3844 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3851 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3853 /* NB I'm not allowed to schedule this conn once its refcount has
3854 * reached 0. Since fundamentally I'm racing with scheduler threads
3855 * consuming my CQ I could be called after all completions have
3856 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3857 * and this CQ is about to be destroyed so I NOOP. */
3858 struct kib_conn *conn = arg;
3859 struct kib_sched_info *sched = conn->ibc_sched;
3860 unsigned long flags;
3862 LASSERT(cq == conn->ibc_cq);
3864 spin_lock_irqsave(&sched->ibs_lock, flags);
3866 conn->ibc_ready = 1;
3868 if (!conn->ibc_scheduled &&
3869 (conn->ibc_nrx > 0 ||
3870 conn->ibc_nsends_posted > 0)) {
3871 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3872 kiblnd_dump_conn_dbg(conn);
3873 conn->ibc_scheduled = 1;
3874 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3876 if (waitqueue_active(&sched->ibs_waitq))
3877 wake_up(&sched->ibs_waitq);
3880 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3884 kiblnd_cq_event(struct ib_event *event, void *arg)
3886 struct kib_conn *conn = arg;
3888 CERROR("%s: async CQ event type %d\n",
3889 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3893 kiblnd_scheduler(void *arg)
3895 long id = (long)arg;
3896 struct kib_sched_info *sched;
3897 struct kib_conn *conn;
3898 wait_queue_entry_t wait;
3899 unsigned long flags;
3906 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3908 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3910 CWARN("Unable to bind on CPU partition %d, please verify whether all CPUs are healthy and reload modules if necessary, otherwise your system might under risk of low performance\n", sched->ibs_cpt);
3913 spin_lock_irqsave(&sched->ibs_lock, flags);
3915 while (!kiblnd_data.kib_shutdown) {
3916 if (need_resched()) {
3917 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3921 spin_lock_irqsave(&sched->ibs_lock, flags);
3924 did_something = false;
3926 conn = list_first_entry_or_null(&sched->ibs_conns,
3930 /* take over kib_sched_conns' ref on conn... */
3931 LASSERT(conn->ibc_scheduled);
3932 list_del(&conn->ibc_sched_list);
3933 conn->ibc_ready = 0;
3935 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3937 wc.wr_id = IBLND_WID_INVAL;
3939 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3941 rc = ib_req_notify_cq(conn->ibc_cq,
3944 CWARN("%s: ib_req_notify_cq failed: %d, closing connection %p\n",
3945 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3947 kiblnd_close_conn(conn, -EIO);
3948 kiblnd_conn_decref(conn);
3949 spin_lock_irqsave(&sched->ibs_lock,
3954 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3957 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3959 "ib_poll_cq (rc: %d) returned invalid "
3960 "wr_id, opcode %d, status: %d, "
3961 "vendor_err: %d, conn: %s status: %d\n"
3962 "please upgrade firmware and OFED or "
3963 "contact vendor.\n", rc,
3964 wc.opcode, wc.status, wc.vendor_err,
3965 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3971 CWARN("%s: ib_poll_cq failed: %d, closing connection %p\n",
3972 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3974 kiblnd_close_conn(conn, -EIO);
3975 kiblnd_conn_decref(conn);
3976 spin_lock_irqsave(&sched->ibs_lock, flags);
3980 spin_lock_irqsave(&sched->ibs_lock, flags);
3982 if (rc != 0 || conn->ibc_ready) {
3983 /* There may be another completion waiting; get
3984 * another scheduler to check while I handle
3986 /* +1 ref for sched_conns */
3987 kiblnd_conn_addref(conn);
3988 list_add_tail(&conn->ibc_sched_list,
3990 if (waitqueue_active(&sched->ibs_waitq))
3991 wake_up(&sched->ibs_waitq);
3993 conn->ibc_scheduled = 0;
3997 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3998 kiblnd_complete(&wc);
4000 spin_lock_irqsave(&sched->ibs_lock, flags);
4003 kiblnd_conn_decref(conn); /* ..drop my ref from above */
4004 did_something = true;
4010 set_current_state(TASK_INTERRUPTIBLE);
4011 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
4012 spin_unlock_irqrestore(&sched->ibs_lock, flags);
4016 remove_wait_queue(&sched->ibs_waitq, &wait);
4017 set_current_state(TASK_RUNNING);
4018 spin_lock_irqsave(&sched->ibs_lock, flags);
4021 spin_unlock_irqrestore(&sched->ibs_lock, flags);
4023 kiblnd_thread_fini();
4028 kiblnd_failover_thread(void *arg)
4030 rwlock_t *glock = &kiblnd_data.kib_global_lock;
4031 struct kib_dev *dev;
4032 struct net *ns = arg;
4033 wait_queue_entry_t wait;
4034 unsigned long flags;
4037 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
4040 write_lock_irqsave(glock, flags);
4042 while (!kiblnd_data.kib_shutdown) {
4043 bool do_failover = false;
4046 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
4048 if (ktime_get_seconds() < dev->ibd_next_failover)
4055 list_del_init(&dev->ibd_fail_list);
4056 dev->ibd_failover = 1;
4057 write_unlock_irqrestore(glock, flags);
4059 rc = kiblnd_dev_failover(dev, ns);
4061 write_lock_irqsave(glock, flags);
4063 LASSERT(dev->ibd_failover);
4064 dev->ibd_failover = 0;
4065 if (rc >= 0) { /* Device is OK or failover succeed */
4066 dev->ibd_next_failover = ktime_get_seconds() + 3;
4070 /* failed to failover, retry later */
4071 dev->ibd_next_failover = ktime_get_seconds() +
4072 min(dev->ibd_failed_failover, 10);
4073 if (kiblnd_dev_can_failover(dev)) {
4074 list_add_tail(&dev->ibd_fail_list,
4075 &kiblnd_data.kib_failed_devs);
4081 /* long sleep if no more pending failover */
4082 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
4084 set_current_state(TASK_INTERRUPTIBLE);
4085 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
4086 write_unlock_irqrestore(glock, flags);
4088 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
4089 cfs_time_seconds(1));
4090 set_current_state(TASK_RUNNING);
4091 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
4092 write_lock_irqsave(glock, flags);
4094 if (!long_sleep || rc != 0)
4097 /* have a long sleep, routine check all active devices,
4098 * we need checking like this because if there is not active
4099 * connection on the dev and no SEND from local, we may listen
4100 * on wrong HCA for ever while there is a bonding failover
4102 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
4103 if (kiblnd_dev_can_failover(dev)) {
4104 list_add_tail(&dev->ibd_fail_list,
4105 &kiblnd_data.kib_failed_devs);
4110 write_unlock_irqrestore(glock, flags);
4112 kiblnd_thread_fini();