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] from %s\n",
351 msg->ibm_type, credits,
352 libcfs_nid2str(conn->ibc_peer->ibp_nid));
355 /* Have I received credits that will let me send? */
356 spin_lock(&conn->ibc_lock);
358 if (conn->ibc_credits + credits >
359 conn->ibc_queue_depth) {
360 rc2 = conn->ibc_credits;
361 spin_unlock(&conn->ibc_lock);
363 CERROR("Bad credits from %s: %d + %d > %d\n",
364 libcfs_nid2str(conn->ibc_peer->ibp_nid),
366 conn->ibc_queue_depth);
368 kiblnd_close_conn(conn, -EPROTO);
369 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
373 conn->ibc_credits += credits;
375 /* This ensures the credit taken by NOOP can be returned */
376 if (msg->ibm_type == IBLND_MSG_NOOP &&
377 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
378 conn->ibc_outstanding_credits++;
380 kiblnd_check_sends_locked(conn);
381 spin_unlock(&conn->ibc_lock);
384 switch (msg->ibm_type) {
386 CERROR("Bad IBLND message type %x from %s\n",
387 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
388 post_credit = IBLND_POSTRX_NO_CREDIT;
393 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
394 post_credit = IBLND_POSTRX_NO_CREDIT;
398 if (credits != 0) /* credit already posted */
399 post_credit = IBLND_POSTRX_NO_CREDIT;
400 else /* a keepalive NOOP */
401 post_credit = IBLND_POSTRX_PEER_CREDIT;
404 case IBLND_MSG_IMMEDIATE:
405 post_credit = IBLND_POSTRX_DONT_POST;
406 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.immediate.ibim_hdr);
407 lnet_nid4_to_nid(msg->ibm_srcnid, &srcnid);
408 rc = lnet_parse(ni, &hdr, &srcnid, rx, 0);
409 if (rc < 0) /* repost on error */
410 post_credit = IBLND_POSTRX_PEER_CREDIT;
413 case IBLND_MSG_PUT_REQ:
414 post_credit = IBLND_POSTRX_DONT_POST;
415 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.putreq.ibprm_hdr);
416 lnet_nid4_to_nid(msg->ibm_srcnid, &srcnid);
417 rc = lnet_parse(ni, &hdr, &srcnid, rx, 1);
418 if (rc < 0) /* repost on error */
419 post_credit = IBLND_POSTRX_PEER_CREDIT;
422 case IBLND_MSG_PUT_NAK:
423 CWARN ("PUT_NACK from %s\n",
424 libcfs_nid2str(conn->ibc_peer->ibp_nid));
425 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
426 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
427 msg->ibm_u.completion.ibcm_status,
428 msg->ibm_u.completion.ibcm_cookie);
431 case IBLND_MSG_PUT_ACK:
432 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
434 spin_lock(&conn->ibc_lock);
435 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
436 msg->ibm_u.putack.ibpam_src_cookie);
438 list_del(&tx->tx_list);
439 spin_unlock(&conn->ibc_lock);
442 CERROR("Unmatched PUT_ACK from %s\n",
443 libcfs_nid2str(conn->ibc_peer->ibp_nid));
448 LASSERT (tx->tx_waiting);
449 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
450 * (a) I can overwrite tx_msg since my peer_ni has received it!
451 * (b) tx_waiting set tells tx_complete() it's not done. */
453 tx->tx_nwrq = tx->tx_nsge = 0; /* overwrite PUT_REQ */
455 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
456 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
457 &msg->ibm_u.putack.ibpam_rd,
458 msg->ibm_u.putack.ibpam_dst_cookie);
460 CERROR("Can't setup rdma for PUT to %s: %d\n",
461 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
463 spin_lock(&conn->ibc_lock);
464 tx->tx_waiting = 0; /* clear waiting and queue atomically */
465 kiblnd_queue_tx_locked(tx, conn);
466 spin_unlock(&conn->ibc_lock);
469 case IBLND_MSG_PUT_DONE:
470 post_credit = IBLND_POSTRX_PEER_CREDIT;
471 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
472 msg->ibm_u.completion.ibcm_status,
473 msg->ibm_u.completion.ibcm_cookie);
476 case IBLND_MSG_GET_REQ:
477 post_credit = IBLND_POSTRX_DONT_POST;
478 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.get.ibgm_hdr);
479 lnet_nid4_to_nid(msg->ibm_srcnid, &srcnid);
480 rc = lnet_parse(ni, &hdr, &srcnid, rx, 1);
481 if (rc < 0) /* repost on error */
482 post_credit = IBLND_POSTRX_PEER_CREDIT;
485 case IBLND_MSG_GET_DONE:
486 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
487 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
488 msg->ibm_u.completion.ibcm_status,
489 msg->ibm_u.completion.ibcm_cookie);
493 if (rc < 0) /* protocol error */
494 kiblnd_close_conn(conn, rc);
496 if (post_credit != IBLND_POSTRX_DONT_POST)
497 kiblnd_post_rx(rx, post_credit);
501 kiblnd_rx_complete(struct kib_rx *rx, int status, int nob)
503 struct kib_msg *msg = rx->rx_msg;
504 struct kib_conn *conn = rx->rx_conn;
505 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
506 struct kib_net *net = ni->ni_data;
511 LASSERT(rx->rx_nob < 0); /* was posted */
512 rx->rx_nob = 0; /* isn't now */
514 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
517 if (status != IB_WC_SUCCESS) {
518 CNETERR("Rx from %s failed: %d\n",
519 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
526 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
528 CERROR("Error %d unpacking rx from %s\n",
529 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
533 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
534 msg->ibm_dstnid != lnet_nid_to_nid4(&ni->ni_nid) ||
535 msg->ibm_srcstamp != conn->ibc_incarnation ||
536 msg->ibm_dststamp != net->ibn_incarnation) {
537 CERROR("Stale rx from %s\n",
538 libcfs_nid2str(conn->ibc_peer->ibp_nid));
543 /* set time last known alive */
544 kiblnd_peer_alive(conn->ibc_peer);
546 /* racing with connection establishment/teardown! */
548 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
549 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
552 write_lock_irqsave(g_lock, flags);
553 /* must check holding global lock to eliminate race */
554 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
555 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
556 write_unlock_irqrestore(g_lock, flags);
559 write_unlock_irqrestore(g_lock, flags);
561 kiblnd_handle_rx(rx);
565 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
566 kiblnd_close_conn(conn, err);
568 kiblnd_drop_rx(rx); /* Don't re-post rx. */
572 kiblnd_fmr_map_tx(struct kib_net *net, struct kib_tx *tx,
573 struct kib_rdma_desc *rd, u32 nob)
575 struct kib_hca_dev *hdev;
577 struct kib_fmr_poolset *fps;
582 LASSERT(tx->tx_pool != NULL);
583 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
586 hdev = tx->tx_pool->tpo_hdev;
587 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
590 * If we're dealing with FastReg, but the device doesn't
591 * support GAPS and the tx has GAPS, then there is no real point
592 * in trying to map the memory, because it'll just fail. So
593 * preemptively fail with an appropriate message
595 if (IS_FAST_REG_DEV(dev) &&
596 !(dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT) &&
598 CERROR("Using FastReg with no GAPS support, but tx has gaps. "
599 "Try setting use_fastreg_gaps to 1\n");
600 return -EPROTONOSUPPORT;
603 #ifdef HAVE_FMR_POOL_API
605 * FMR does not support gaps but the tx has gaps then
606 * we should make sure that the number of fragments we'll be sending
607 * over fits within the number of fragments negotiated on the
608 * connection, otherwise, we won't be able to RDMA the data.
609 * We need to maintain the number of fragments negotiation on the
610 * connection for backwards compatibility.
612 if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
614 tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
615 CERROR("TX number of frags (%d) is <= than connection"
616 " number of frags (%d). Consider setting peer's"
617 " map_on_demand to 256\n", tx->tx_nfrags,
618 tx->tx_conn->ibc_max_frags);
624 fps = net->ibn_fmr_ps[cpt];
625 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
627 CERROR("Can't map %u bytes (%u/%u)s: %d\n", nob,
628 tx->tx_nfrags, rd->rd_nfrags, rc);
633 * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
636 rd->rd_key = tx->tx_fmr.fmr_key;
638 * for FastReg or FMR with no gaps we can accumulate all
639 * the fragments in one FastReg or FMR fragment.
642 #ifdef HAVE_FMR_POOL_API
643 ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
646 IS_FAST_REG_DEV(dev)) {
647 /* FMR requires zero based address */
648 #ifdef HAVE_FMR_POOL_API
649 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
650 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
652 rd->rd_frags[0].rf_nob = nob;
656 * We're transmitting with gaps using FMR.
657 * We'll need to use multiple fragments and identify the
658 * zero based address of each fragment.
660 for (i = 0; i < rd->rd_nfrags; i++) {
661 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
662 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
670 kiblnd_unmap_tx(struct kib_tx *tx)
673 #ifdef HAVE_FMR_POOL_API
674 tx->tx_fmr.fmr_pfmr ||
677 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
679 if (tx->tx_nfrags != 0) {
680 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev, tx);
685 #ifdef HAVE_IB_GET_DMA_MR
686 static struct ib_mr *
687 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
689 struct kib_net *net = ni->ni_data;
690 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
691 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
693 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
696 * if map-on-demand is turned on and the device supports
697 * either FMR or FastReg then use that. Otherwise use global
698 * memory regions. If that's not available either, then you're
699 * dead in the water and fail the operation.
701 if (tunables->lnd_map_on_demand && (IS_FAST_REG_DEV(net->ibn_dev)
702 #ifdef HAVE_FMR_POOL_API
703 || net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED
709 * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
710 * in the call chain. The mapping will fail with appropriate error
713 return hdev->ibh_mrs;
717 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
718 struct kib_rdma_desc *rd, int nfrags)
720 struct kib_net *net = ni->ni_data;
721 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
722 #ifdef HAVE_IB_GET_DMA_MR
723 struct ib_mr *mr = NULL;
728 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
730 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
731 tx->tx_nfrags = nfrags;
733 rd->rd_nfrags = kiblnd_dma_map_sg(hdev, tx);
734 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
735 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
736 hdev->ibh_ibdev, &tx->tx_frags[i]);
737 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
738 hdev->ibh_ibdev, &tx->tx_frags[i]);
739 nob += rd->rd_frags[i].rf_nob;
742 #ifdef HAVE_IB_GET_DMA_MR
743 mr = kiblnd_find_rd_dma_mr(ni, rd);
745 /* found pre-mapping MR */
746 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
751 if (net->ibn_fmr_ps != NULL)
752 return kiblnd_fmr_map_tx(net, tx, rd, nob);
757 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
758 struct kib_rdma_desc *rd, int nkiov,
759 struct bio_vec *kiov, int offset, int nob)
761 struct kib_net *net = ni->ni_data;
762 struct scatterlist *sg;
767 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
771 LASSERT(net != NULL);
773 while (offset >= kiov->bv_len) {
774 offset -= kiov->bv_len;
787 CERROR("lacking enough sg entries to map tx\n");
792 fragnob = min((int)(kiov->bv_len - offset), nob);
795 * We're allowed to start at a non-aligned page offset in
796 * the first fragment and end at a non-aligned page offset
797 * in the last fragment.
799 if ((fragnob < (int)(kiov->bv_len - offset)) &&
800 nkiov < max_nkiov && nob > fragnob) {
801 CDEBUG(D_NET, "fragnob %d < available page %d: with"
802 " remaining %d kiovs with %d nob left\n",
803 fragnob, (int)(kiov->bv_len - offset),
808 sg_set_page(sg, kiov->bv_page, fragnob,
809 kiov->bv_offset + offset);
818 return kiblnd_map_tx(ni, tx, rd, sg_count);
822 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
823 __must_hold(&conn->ibc_lock)
825 struct kib_msg *msg = tx->tx_msg;
826 struct kib_peer_ni *peer_ni = conn->ibc_peer;
827 struct lnet_ni *ni = peer_ni->ibp_ni;
828 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
829 int ver = conn->ibc_version;
833 LASSERT(tx->tx_queued);
834 /* We rely on this for QP sizing */
835 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
836 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
838 LASSERT(credit == 0 || credit == 1);
839 LASSERT(conn->ibc_outstanding_credits >= 0);
840 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
841 LASSERT(conn->ibc_credits >= 0);
842 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
844 if (conn->ibc_nsends_posted ==
845 kiblnd_concurrent_sends(ver, ni)) {
846 /* tx completions outstanding... */
847 CDEBUG(D_NET, "%s: posted enough\n",
848 libcfs_nid2str(peer_ni->ibp_nid));
852 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
853 CDEBUG(D_NET, "%s: no credits\n",
854 libcfs_nid2str(peer_ni->ibp_nid));
858 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
859 conn->ibc_credits == 1 && /* last credit reserved */
860 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
861 CDEBUG(D_NET, "%s: not using last credit\n",
862 libcfs_nid2str(peer_ni->ibp_nid));
866 /* NB don't drop ibc_lock before bumping tx_sending */
867 list_del(&tx->tx_list);
870 if (msg->ibm_type == IBLND_MSG_NOOP &&
871 (!kiblnd_need_noop(conn) || /* redundant NOOP */
872 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
873 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
874 /* OK to drop when posted enough NOOPs, since
875 * kiblnd_check_sends_locked will queue NOOP again when
876 * posted NOOPs complete */
877 spin_unlock(&conn->ibc_lock);
878 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
880 spin_lock(&conn->ibc_lock);
881 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
882 libcfs_nid2str(peer_ni->ibp_nid),
883 conn->ibc_noops_posted);
887 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
888 peer_ni->ibp_nid, conn->ibc_incarnation);
890 conn->ibc_credits -= credit;
891 conn->ibc_outstanding_credits = 0;
892 conn->ibc_nsends_posted++;
893 if (msg->ibm_type == IBLND_MSG_NOOP)
894 conn->ibc_noops_posted++;
896 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
897 * PUT. If so, it was first queued here as a PUT_REQ, sent and
898 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
899 * and then re-queued here. It's (just) possible that
900 * tx_sending is non-zero if we've not done the tx_complete()
901 * from the first send; hence the ++ rather than = below. */
903 list_add(&tx->tx_list, &conn->ibc_active_txs);
905 /* I'm still holding ibc_lock! */
906 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
907 CDEBUG(D_NET, "connection to %s is not established\n",
908 conn->ibc_peer? libcfs_nid2str(conn->ibc_peer->ibp_nid): "NULL");
910 } else if (tx->tx_pool->tpo_pool.po_failed ||
911 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
912 /* close_conn will launch failover */
915 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
916 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
918 if (frd != NULL && !frd->frd_posted) {
919 if (!frd->frd_valid) {
920 wr = &frd->frd_inv_wr.wr;
921 wr->next = &frd->frd_fastreg_wr.wr;
923 wr = &frd->frd_fastreg_wr.wr;
925 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
928 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
929 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
930 bad->wr_id, bad->opcode, bad->send_flags,
931 libcfs_nid2str(conn->ibc_peer->ibp_nid));
934 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
937 #ifdef HAVE_IB_POST_SEND_RECV_CONST
938 rc = ib_post_send(conn->ibc_cmid->qp, wr,
939 (const struct ib_send_wr **)&bad);
941 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
945 conn->ibc_last_send = ktime_get();
949 frd->frd_posted = true;
953 /* NB credits are transferred in the actual
954 * message, which can only be the last work item */
955 conn->ibc_credits += credit;
956 conn->ibc_outstanding_credits += msg->ibm_credits;
957 conn->ibc_nsends_posted--;
958 if (msg->ibm_type == IBLND_MSG_NOOP)
959 conn->ibc_noops_posted--;
965 done = (tx->tx_sending == 0);
967 list_del(&tx->tx_list);
969 spin_unlock(&conn->ibc_lock);
971 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
972 CERROR("Error %d posting transmit to %s\n",
973 rc, libcfs_nid2str(peer_ni->ibp_nid));
975 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
976 rc, libcfs_nid2str(peer_ni->ibp_nid));
978 kiblnd_close_conn(conn, rc);
983 spin_lock(&conn->ibc_lock);
989 kiblnd_check_sends_locked(struct kib_conn *conn)
991 int ver = conn->ibc_version;
992 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
995 /* Don't send anything until after the connection is established */
996 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
997 CDEBUG(D_NET, "%s too soon\n",
998 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1002 LASSERT(conn->ibc_nsends_posted <=
1003 kiblnd_concurrent_sends(ver, ni));
1004 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
1005 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
1006 LASSERT (conn->ibc_reserved_credits >= 0);
1008 while (conn->ibc_reserved_credits > 0 &&
1009 (tx = list_first_entry_or_null(&conn->ibc_tx_queue_rsrvd,
1010 struct kib_tx, tx_list)) != NULL) {
1011 list_move_tail(&tx->tx_list, &conn->ibc_tx_queue);
1012 conn->ibc_reserved_credits--;
1015 if (kiblnd_need_noop(conn)) {
1016 spin_unlock(&conn->ibc_lock);
1018 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1020 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1022 spin_lock(&conn->ibc_lock);
1024 kiblnd_queue_tx_locked(tx, conn);
1030 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1032 tx = list_first_entry(&conn->ibc_tx_queue_nocred,
1033 struct kib_tx, tx_list);
1034 } else if (!list_empty(&conn->ibc_tx_noops)) {
1035 LASSERT (!IBLND_OOB_CAPABLE(ver));
1037 tx = list_first_entry(&conn->ibc_tx_noops,
1038 struct kib_tx, tx_list);
1039 } else if (!list_empty(&conn->ibc_tx_queue)) {
1041 tx = list_first_entry(&conn->ibc_tx_queue,
1042 struct kib_tx, tx_list);
1046 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1052 kiblnd_tx_complete(struct kib_tx *tx, int status)
1054 int failed = (status != IB_WC_SUCCESS);
1055 struct kib_conn *conn = tx->tx_conn;
1058 if (tx->tx_sending <= 0) {
1059 CERROR("Received an event on a freed tx: %p status %d\n",
1065 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1066 CNETERR("Tx -> %s cookie %#llx"
1067 " sending %d waiting %d: failed %d\n",
1068 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1069 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1072 kiblnd_close_conn(conn, -EIO);
1074 kiblnd_peer_alive(conn->ibc_peer);
1077 spin_lock(&conn->ibc_lock);
1079 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1080 * gets to free it, which also drops its ref on 'conn'. */
1083 conn->ibc_nsends_posted--;
1084 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1085 conn->ibc_noops_posted--;
1088 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1089 tx->tx_waiting = 0; /* don't wait for peer_ni */
1090 tx->tx_status = -EIO;
1093 idle = (tx->tx_sending == 0) && /* This is the final callback */
1094 !tx->tx_waiting && /* Not waiting for peer_ni */
1095 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1097 list_del(&tx->tx_list);
1099 kiblnd_check_sends_locked(conn);
1100 spin_unlock(&conn->ibc_lock);
1108 kiblnd_init_tx_sge(struct kib_tx *tx, u64 addr, unsigned int len)
1110 struct ib_sge *sge = &tx->tx_sge[tx->tx_nsge];
1111 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1112 #ifdef HAVE_IB_GET_DMA_MR
1113 struct ib_mr *mr = hdev->ibh_mrs;
1116 *sge = (struct ib_sge) {
1117 #ifdef HAVE_IB_GET_DMA_MR
1120 .lkey = hdev->ibh_pd->local_dma_lkey,
1129 static struct ib_rdma_wr *
1130 kiblnd_init_tx_msg_payload(struct lnet_ni *ni, struct kib_tx *tx, int type,
1131 int body_nob, int payload)
1133 struct ib_rdma_wr *wrq;
1134 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1136 LASSERT(tx->tx_nwrq >= 0);
1137 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1138 LASSERT(nob <= IBLND_MSG_SIZE);
1140 kiblnd_init_msg(tx->tx_msg, type, body_nob + payload);
1142 wrq = &tx->tx_wrq[tx->tx_nwrq];
1144 *wrq = (struct ib_rdma_wr) {
1146 .wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
1148 .sg_list = &tx->tx_sge[tx->tx_nsge],
1149 .opcode = IB_WR_SEND,
1150 .send_flags = IB_SEND_SIGNALED,
1154 kiblnd_init_tx_sge(tx, tx->tx_msgaddr, nob);
1161 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1162 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1164 struct kib_msg *ibmsg = tx->tx_msg;
1165 struct kib_rdma_desc *srcrd = tx->tx_rd;
1166 struct ib_rdma_wr *wrq = NULL;
1174 LASSERT(!in_interrupt());
1175 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1176 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1178 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1179 resid > 0; resid -= sge_nob) {
1182 if (srcidx >= srcrd->rd_nfrags) {
1183 CERROR("Src buffer exhausted: %d frags %px\n",
1189 if (dstidx >= dstrd->rd_nfrags) {
1190 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1195 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1196 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1197 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1198 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1199 conn->ibc_max_frags,
1200 srcidx, srcrd->rd_nfrags,
1201 dstidx, dstrd->rd_nfrags);
1206 sge_nob = min3(kiblnd_rd_frag_size(srcrd, srcidx),
1207 kiblnd_rd_frag_size(dstrd, dstidx),
1210 sge = &tx->tx_sge[tx->tx_nsge];
1211 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1212 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1213 sge->length = sge_nob;
1216 wrq = &tx->tx_wrq[tx->tx_nwrq];
1218 wrq->wr.next = &(wrq + 1)->wr;
1219 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1220 wrq->wr.sg_list = sge;
1221 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1222 wrq->wr.send_flags = 0;
1224 #ifdef HAVE_IB_RDMA_WR
1225 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1227 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1230 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1232 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1237 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1238 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1241 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1243 wrq->wr.num_sge = wrq_sge;
1249 if (rc < 0) /* no RDMA if completing with failure */
1250 tx->tx_nwrq = tx->tx_nsge = 0;
1252 ibmsg->ibm_u.completion.ibcm_status = rc;
1253 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1254 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1255 type, sizeof(struct kib_completion_msg));
1261 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1263 struct list_head *q;
1266 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1267 LASSERT(!tx->tx_queued); /* not queued for sending already */
1268 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1270 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1271 CDEBUG(D_NET, "connection with %s is disconnected\n",
1272 conn->ibc_peer? libcfs_nid2str(conn->ibc_peer->ibp_nid): "NULL");
1274 tx->tx_status = -ECONNABORTED;
1276 if (tx->tx_conn != NULL) {
1277 /* PUT_DONE first attached to conn as a PUT_REQ */
1278 LASSERT(tx->tx_conn == conn);
1279 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1281 kiblnd_conn_decref(conn);
1283 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1288 timeout_ns = kiblnd_timeout() * NSEC_PER_SEC;
1290 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1292 if (tx->tx_conn == NULL) {
1293 kiblnd_conn_addref(conn);
1295 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1297 /* PUT_DONE first attached to conn as a PUT_REQ */
1298 LASSERT (tx->tx_conn == conn);
1299 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1302 switch (tx->tx_msg->ibm_type) {
1306 case IBLND_MSG_PUT_REQ:
1307 case IBLND_MSG_GET_REQ:
1308 q = &conn->ibc_tx_queue_rsrvd;
1311 case IBLND_MSG_PUT_NAK:
1312 case IBLND_MSG_PUT_ACK:
1313 case IBLND_MSG_PUT_DONE:
1314 case IBLND_MSG_GET_DONE:
1315 q = &conn->ibc_tx_queue_nocred;
1318 case IBLND_MSG_NOOP:
1319 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1320 q = &conn->ibc_tx_queue_nocred;
1322 q = &conn->ibc_tx_noops;
1325 case IBLND_MSG_IMMEDIATE:
1326 q = &conn->ibc_tx_queue;
1330 list_add_tail(&tx->tx_list, q);
1334 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1336 spin_lock(&conn->ibc_lock);
1337 kiblnd_queue_tx_locked(tx, conn);
1338 kiblnd_check_sends_locked(conn);
1339 spin_unlock(&conn->ibc_lock);
1343 kiblnd_resolve_addr_cap(struct rdma_cm_id *cmid,
1344 struct sockaddr_in *srcaddr,
1345 struct sockaddr_in *dstaddr,
1348 unsigned short port;
1351 /* allow the port to be reused */
1352 rc = rdma_set_reuseaddr(cmid, 1);
1354 CERROR("Unable to set reuse on cmid: %d\n", rc);
1358 /* look for a free privileged port */
1359 for (port = PROT_SOCK-1; port > 0; port--) {
1360 srcaddr->sin_port = htons(port);
1361 rc = rdma_resolve_addr(cmid,
1362 (struct sockaddr *)srcaddr,
1363 (struct sockaddr *)dstaddr,
1366 CDEBUG(D_NET, "bound to port %hu\n", port);
1368 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1369 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1376 CERROR("cannot bind to a free privileged port: rc = %d\n", rc);
1382 kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1383 struct sockaddr_in *srcaddr,
1384 struct sockaddr_in *dstaddr,
1387 const struct cred *old_creds = NULL;
1388 struct cred *new_creds;
1391 if (!capable(CAP_NET_BIND_SERVICE)) {
1392 new_creds = prepare_kernel_cred(NULL);
1396 cap_raise(new_creds->cap_effective, CAP_NET_BIND_SERVICE);
1397 old_creds = override_creds(new_creds);
1400 rc = kiblnd_resolve_addr_cap(cmid, srcaddr, dstaddr, timeout_ms);
1403 revert_creds(old_creds);
1409 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1411 struct rdma_cm_id *cmid;
1412 struct kib_dev *dev;
1413 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1414 struct sockaddr_in srcaddr;
1415 struct sockaddr_in dstaddr;
1418 LASSERT (net != NULL);
1419 LASSERT (peer_ni->ibp_connecting > 0);
1421 cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1422 kiblnd_cm_callback, peer_ni,
1423 RDMA_PS_TCP, IB_QPT_RC);
1426 CERROR("Can't create CMID for %s: %ld\n",
1427 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1433 memset(&srcaddr, 0, sizeof(srcaddr));
1434 srcaddr.sin_family = AF_INET;
1435 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1437 memset(&dstaddr, 0, sizeof(dstaddr));
1438 dstaddr.sin_family = AF_INET;
1439 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1440 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1442 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1444 if (*kiblnd_tunables.kib_use_priv_port) {
1445 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1446 kiblnd_timeout() * 1000);
1448 rc = rdma_resolve_addr(cmid,
1449 (struct sockaddr *)&srcaddr,
1450 (struct sockaddr *)&dstaddr,
1451 kiblnd_timeout() * 1000);
1454 /* Can't initiate address resolution: */
1455 CERROR("Can't resolve addr for %s: %d\n",
1456 libcfs_nid2str(peer_ni->ibp_nid), rc);
1463 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1464 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1465 rdma_destroy_id(cmid);
1468 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1472 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1474 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1475 char *reason = NULL;
1477 unsigned long flags;
1479 write_lock_irqsave(glock, flags);
1480 if (peer_ni->ibp_reconnecting == 0) {
1481 if (peer_ni->ibp_accepting)
1482 reason = "accepting";
1483 else if (peer_ni->ibp_connecting)
1484 reason = "connecting";
1485 else if (!list_empty(&peer_ni->ibp_conns))
1486 reason = "connected";
1487 else /* connected then closed */
1493 if (peer_ni->ibp_accepting)
1494 CNETERR("Detecting race between accepting and reconnecting\n");
1495 peer_ni->ibp_reconnecting--;
1497 if (!kiblnd_peer_active(peer_ni)) {
1498 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1499 reason = "unlinked";
1503 peer_ni->ibp_connecting++;
1504 peer_ni->ibp_reconnected++;
1506 write_unlock_irqrestore(glock, flags);
1508 kiblnd_connect_peer(peer_ni);
1512 write_unlock_irqrestore(glock, flags);
1514 CWARN("Abort reconnection of %s: %s\n",
1515 libcfs_nid2str(peer_ni->ibp_nid), reason);
1516 kiblnd_txlist_done(&txs, -ECONNABORTED,
1517 LNET_MSG_STATUS_LOCAL_ABORTED);
1522 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1524 struct kib_peer_ni *peer_ni;
1525 struct kib_peer_ni *peer2;
1526 struct kib_conn *conn;
1527 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1528 unsigned long flags;
1531 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1533 /* If I get here, I've committed to send, so I complete the tx with
1534 * failure on any problems
1537 LASSERT(!tx || !tx->tx_conn); /* only set when assigned a conn */
1538 LASSERT(!tx || tx->tx_nwrq > 0); /* work items have been set up */
1540 /* First time, just use a read lock since I expect to find my peer_ni
1543 read_lock_irqsave(g_lock, flags);
1545 peer_ni = kiblnd_find_peer_locked(ni, nid);
1546 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1547 /* Found a peer_ni with an established connection */
1548 conn = kiblnd_get_conn_locked(peer_ni);
1549 kiblnd_conn_addref(conn); /* 1 ref for me... */
1551 read_unlock_irqrestore(g_lock, flags);
1554 kiblnd_queue_tx(tx, conn);
1555 kiblnd_conn_decref(conn); /* ...to here */
1559 read_unlock(g_lock);
1560 /* Re-try with a write lock */
1563 peer_ni = kiblnd_find_peer_locked(ni, nid);
1564 if (peer_ni != NULL) {
1565 if (list_empty(&peer_ni->ibp_conns)) {
1566 /* found a peer_ni, but it's still connecting... */
1567 LASSERT(kiblnd_peer_connecting(peer_ni));
1569 list_add_tail(&tx->tx_list,
1570 &peer_ni->ibp_tx_queue);
1571 write_unlock_irqrestore(g_lock, flags);
1573 conn = kiblnd_get_conn_locked(peer_ni);
1574 kiblnd_conn_addref(conn); /* 1 ref for me... */
1576 write_unlock_irqrestore(g_lock, flags);
1579 kiblnd_queue_tx(tx, conn);
1580 kiblnd_conn_decref(conn); /* ...to here */
1585 write_unlock_irqrestore(g_lock, flags);
1587 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1588 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1590 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1592 tx->tx_status = -EHOSTUNREACH;
1594 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1600 write_lock_irqsave(g_lock, flags);
1602 peer2 = kiblnd_find_peer_locked(ni, nid);
1603 if (peer2 != NULL) {
1604 if (list_empty(&peer2->ibp_conns)) {
1605 /* found a peer_ni, but it's still connecting... */
1606 LASSERT(kiblnd_peer_connecting(peer2));
1608 list_add_tail(&tx->tx_list,
1609 &peer2->ibp_tx_queue);
1610 write_unlock_irqrestore(g_lock, flags);
1612 conn = kiblnd_get_conn_locked(peer2);
1613 kiblnd_conn_addref(conn); /* 1 ref for me... */
1615 write_unlock_irqrestore(g_lock, flags);
1618 kiblnd_queue_tx(tx, conn);
1619 kiblnd_conn_decref(conn); /* ...to here */
1622 kiblnd_peer_decref(peer_ni);
1626 /* Brand new peer_ni */
1627 LASSERT(peer_ni->ibp_connecting == 0);
1628 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1629 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1631 /* always called with a ref on ni, which prevents ni being shutdown */
1632 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1635 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1637 kiblnd_peer_addref(peer_ni);
1638 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
1640 write_unlock_irqrestore(g_lock, flags);
1642 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1643 kiblnd_connect_peer(peer_ni);
1644 kiblnd_peer_decref(peer_ni);
1648 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1650 struct kib_dev *dev = ((struct kib_net *)ni->ni_data)->ibn_dev;
1651 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1652 int type = lntmsg->msg_type;
1653 struct lnet_processid *target = &lntmsg->msg_target;
1654 int target_is_router = lntmsg->msg_target_is_router;
1655 int routing = lntmsg->msg_routing;
1656 unsigned int payload_niov = lntmsg->msg_niov;
1657 struct bio_vec *payload_kiov = lntmsg->msg_kiov;
1658 unsigned int payload_offset = lntmsg->msg_offset;
1659 unsigned int payload_nob = lntmsg->msg_len;
1660 struct lnet_libmd *msg_md = lntmsg->msg_md;
1662 struct kib_msg *ibmsg;
1663 struct kib_rdma_desc *rd;
1668 /* NB 'private' is different depending on what we're sending.... */
1670 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1671 payload_nob, payload_niov, libcfs_idstr(target));
1673 LASSERT(payload_nob == 0 || payload_niov > 0);
1675 /* Thread context */
1676 LASSERT(!in_interrupt());
1678 tx = kiblnd_get_idle_tx(ni, lnet_nid_to_nid4(&target->nid));
1680 CERROR("Can't allocate %s txd for %s\n",
1681 lnet_msgtyp2str(type),
1682 libcfs_nidstr(&target->nid));
1686 gpu = msg_md ? (msg_md->md_flags & LNET_MD_FLAG_GPU) : false;
1694 LASSERT(payload_nob == 0);
1698 if (routing || target_is_router)
1699 break; /* send IMMEDIATE */
1701 /* is the REPLY message too small for RDMA? */
1702 nob = offsetof(struct kib_msg,
1703 ibm_u.immediate.ibim_payload[msg_md->md_length]);
1704 if (nob <= IBLND_MSG_SIZE && !gpu)
1705 break; /* send IMMEDIATE */
1707 rd = &ibmsg->ibm_u.get.ibgm_rd;
1709 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1712 0, msg_md->md_length);
1714 CERROR("Can't setup GET sink for %s: %d\n",
1715 libcfs_nidstr(&target->nid), rc);
1716 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1721 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1722 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1723 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.get.ibgm_hdr);
1725 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1727 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1728 if (tx->tx_lntmsg[1] == NULL) {
1729 CERROR("Can't create reply for GET -> %s\n",
1730 libcfs_nidstr(&target->nid));
1735 /* finalise lntmsg[0,1] on completion */
1736 tx->tx_lntmsg[0] = lntmsg;
1737 tx->tx_waiting = 1; /* waiting for GET_DONE */
1738 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1741 case LNET_MSG_REPLY:
1743 /* Is the payload small enough not to need RDMA? */
1744 nob = offsetof(struct kib_msg,
1745 ibm_u.immediate.ibim_payload[payload_nob]);
1746 if (nob <= IBLND_MSG_SIZE && !gpu)
1747 break; /* send IMMEDIATE */
1751 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1752 payload_niov, payload_kiov,
1753 payload_offset, payload_nob);
1755 CERROR("Can't setup PUT src for %s: %d\n",
1756 libcfs_nidstr(&target->nid), rc);
1761 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.putreq.ibprm_hdr);
1762 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1763 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1764 sizeof(struct kib_putreq_msg));
1766 /* finalise lntmsg[0,1] on completion */
1767 tx->tx_lntmsg[0] = lntmsg;
1768 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1769 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1773 /* send IMMEDIATE */
1774 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1778 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.immediate.ibim_hdr);
1780 if (IS_FAST_REG_DEV(dev) && payload_nob) {
1781 struct ib_rdma_wr *wrq;
1784 nob = offsetof(struct kib_immediate_msg, ibim_payload[0]);
1785 wrq = kiblnd_init_tx_msg_payload(ni, tx, IBLND_MSG_IMMEDIATE,
1789 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1790 payload_niov, payload_kiov,
1791 payload_offset, payload_nob);
1793 CERROR("Can't setup IMMEDIATE src for %s: %d\n",
1794 libcfs_nidstr(&target->nid), rc);
1799 /* lets generate a SGE chain */
1800 for (i = 0; i < rd->rd_nfrags; i++) {
1801 kiblnd_init_tx_sge(tx, rd->rd_frags[i].rf_addr,
1802 rd->rd_frags[i].rf_nob);
1806 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1807 offsetof(struct kib_msg,
1808 ibm_u.immediate.ibim_payload),
1809 payload_niov, payload_kiov,
1810 payload_offset, payload_nob);
1812 nob = offsetof(struct kib_immediate_msg,
1813 ibim_payload[payload_nob]);
1815 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1818 /* finalise lntmsg on completion */
1819 tx->tx_lntmsg[0] = lntmsg;
1821 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1826 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1828 struct lnet_processid *target = &lntmsg->msg_target;
1829 unsigned int niov = lntmsg->msg_niov;
1830 struct bio_vec *kiov = lntmsg->msg_kiov;
1831 unsigned int offset = lntmsg->msg_offset;
1832 unsigned int nob = lntmsg->msg_len;
1833 struct lnet_libmd *payload_md = lntmsg->msg_md;
1837 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1839 CERROR("Can't get tx for REPLY to %s\n",
1840 libcfs_nidstr(&target->nid));
1844 tx->tx_gpu = !!(payload_md->md_flags & LNET_MD_FLAG_GPU);
1848 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1849 niov, kiov, offset, nob);
1852 CERROR("Can't setup GET src for %s: %d\n",
1853 libcfs_nidstr(&target->nid), rc);
1857 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1858 IBLND_MSG_GET_DONE, nob,
1859 &rx->rx_msg->ibm_u.get.ibgm_rd,
1860 rx->rx_msg->ibm_u.get.ibgm_cookie);
1862 CERROR("Can't setup rdma for GET from %s: %d\n",
1863 libcfs_nidstr(&target->nid), rc);
1868 /* No RDMA: local completion may happen now! */
1869 lnet_finalize(lntmsg, 0);
1871 /* RDMA: lnet_finalize(lntmsg) when it
1873 tx->tx_lntmsg[0] = lntmsg;
1876 kiblnd_queue_tx(tx, rx->rx_conn);
1881 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1884 lnet_finalize(lntmsg, -EIO);
1888 kiblnd_get_dev_prio(struct lnet_ni *ni, unsigned int dev_idx)
1890 struct kib_net *net = ni->ni_data;
1891 struct device *dev = NULL;
1894 dev = net->ibn_dev->ibd_hdev->ibh_ibdev->dma_device;
1896 return lnet_get_dev_prio(dev, dev_idx);
1901 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1902 int delayed, unsigned int niov, struct bio_vec *kiov,
1903 unsigned int offset, unsigned int mlen, unsigned int rlen)
1905 struct kib_rx *rx = private;
1906 struct kib_msg *rxmsg = rx->rx_msg;
1907 struct kib_conn *conn = rx->rx_conn;
1911 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1914 LASSERT (mlen <= rlen);
1915 LASSERT (!in_interrupt());
1917 switch (rxmsg->ibm_type) {
1921 case IBLND_MSG_IMMEDIATE:
1922 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1923 if (nob > rx->rx_nob) {
1924 CERROR("Immediate message from %s too big: %d(%d)\n",
1925 libcfs_nidstr(&lntmsg->msg_hdr.src_nid),
1931 lnet_copy_flat2kiov(niov, kiov, offset,
1932 IBLND_MSG_SIZE, rxmsg,
1933 offsetof(struct kib_msg,
1934 ibm_u.immediate.ibim_payload),
1936 lnet_finalize(lntmsg, 0);
1939 case IBLND_MSG_PUT_REQ: {
1940 struct kib_msg *txmsg;
1941 struct kib_rdma_desc *rd;
1942 struct lnet_libmd *payload_md = lntmsg->msg_md;
1944 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1946 lnet_finalize(lntmsg, 0);
1947 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1952 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1954 CERROR("Can't allocate tx for %s\n",
1955 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1956 /* Not replying will break the connection */
1961 tx->tx_gpu = !!(payload_md->md_flags & LNET_MD_FLAG_GPU);
1963 rd = &txmsg->ibm_u.putack.ibpam_rd;
1964 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1965 niov, kiov, offset, mlen);
1967 CERROR("Can't setup PUT sink for %s: %d\n",
1968 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1969 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1971 /* tell peer_ni it's over */
1972 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1977 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1978 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1979 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1981 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1983 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1984 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1985 kiblnd_queue_tx(tx, conn);
1987 /* reposted buffer reserved for PUT_DONE */
1988 post_credit = IBLND_POSTRX_NO_CREDIT;
1992 case IBLND_MSG_GET_REQ:
1993 if (lntmsg != NULL) {
1994 /* Optimized GET; RDMA lntmsg's payload */
1995 kiblnd_reply(ni, rx, lntmsg);
1997 /* GET didn't match anything */
1998 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
2000 rxmsg->ibm_u.get.ibgm_cookie);
2005 kiblnd_post_rx(rx, post_credit);
2010 kiblnd_thread_fini (void)
2012 atomic_dec (&kiblnd_data.kib_nthreads);
2016 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
2018 /* This is racy, but everyone's only writing ktime_get_seconds() */
2019 peer_ni->ibp_last_alive = ktime_get_seconds();
2024 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
2027 time64_t last_alive = 0;
2028 unsigned long flags;
2030 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2032 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
2033 error = peer_ni->ibp_error;
2034 peer_ni->ibp_error = 0;
2036 last_alive = peer_ni->ibp_last_alive;
2039 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2042 lnet_notify(peer_ni->ibp_ni,
2043 peer_ni->ibp_nid, false, false, last_alive);
2047 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
2049 /* This just does the immediate housekeeping. 'error' is zero for a
2050 * normal shutdown which can happen only after the connection has been
2051 * established. If the connection is established, schedule the
2052 * connection to be finished off by the connd. Otherwise the connd is
2053 * already dealing with it (either to set it up or tear it down).
2054 * Caller holds kib_global_lock exclusively in irq context */
2055 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2056 struct kib_dev *dev;
2057 unsigned long flags;
2059 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2061 if (error != 0 && conn->ibc_comms_error == 0)
2062 conn->ibc_comms_error = error;
2064 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2065 return; /* already being handled */
2068 list_empty(&conn->ibc_tx_noops) &&
2069 list_empty(&conn->ibc_tx_queue) &&
2070 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2071 list_empty(&conn->ibc_tx_queue_nocred) &&
2072 list_empty(&conn->ibc_active_txs)) {
2073 CDEBUG(D_NET, "closing conn to %s\n",
2074 libcfs_nid2str(peer_ni->ibp_nid));
2076 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2077 libcfs_nid2str(peer_ni->ibp_nid), error,
2078 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2079 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2080 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2081 "" : "(sending_rsrvd)",
2082 list_empty(&conn->ibc_tx_queue_nocred) ?
2083 "" : "(sending_nocred)",
2084 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2087 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2088 if (peer_ni->ibp_next_conn == conn)
2089 /* clear next_conn so it won't be used */
2090 peer_ni->ibp_next_conn = NULL;
2091 list_del(&conn->ibc_list);
2092 /* connd (see below) takes over ibc_list's ref */
2094 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2095 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2096 kiblnd_unlink_peer_locked(peer_ni);
2098 /* set/clear error on last conn */
2099 peer_ni->ibp_error = conn->ibc_comms_error;
2102 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2105 kiblnd_dev_can_failover(dev)) {
2106 list_add_tail(&dev->ibd_fail_list,
2107 &kiblnd_data.kib_failed_devs);
2108 wake_up(&kiblnd_data.kib_failover_waitq);
2111 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2113 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2114 wake_up(&kiblnd_data.kib_connd_waitq);
2116 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2120 kiblnd_close_conn(struct kib_conn *conn, int error)
2122 unsigned long flags;
2124 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2126 kiblnd_close_conn_locked(conn, error);
2128 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2132 kiblnd_handle_early_rxs(struct kib_conn *conn)
2134 unsigned long flags;
2137 LASSERT(!in_interrupt());
2138 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2140 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2141 while ((rx = list_first_entry_or_null(&conn->ibc_early_rxs,
2143 rx_list)) != NULL) {
2144 list_del(&rx->rx_list);
2145 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2147 kiblnd_handle_rx(rx);
2149 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2151 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2155 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2161 spin_lock(&conn->ibc_lock);
2163 list_for_each_entry_safe(tx, nxt, txs, tx_list) {
2164 if (txs == &conn->ibc_active_txs) {
2165 LASSERT(!tx->tx_queued);
2166 LASSERT(tx->tx_waiting ||
2167 tx->tx_sending != 0);
2168 if (conn->ibc_comms_error == -ETIMEDOUT) {
2169 if (tx->tx_waiting && !tx->tx_sending)
2171 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2172 else if (tx->tx_sending)
2174 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2177 LASSERT(tx->tx_queued);
2178 if (conn->ibc_comms_error == -ETIMEDOUT)
2179 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2181 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2184 tx->tx_status = -ECONNABORTED;
2188 * TODO: This makes an assumption that
2189 * kiblnd_tx_complete() will be called for each tx. If
2190 * that event is dropped we could end up with stale
2191 * connections floating around. We'd like to deal with
2192 * that in a better way.
2194 * Also that means we can exceed the timeout by many
2197 if (tx->tx_sending == 0) {
2199 list_move(&tx->tx_list, &zombies);
2201 /* keep tx until cq destroy */
2202 list_move(&tx->tx_list, &conn->ibc_zombie_txs);
2207 spin_unlock(&conn->ibc_lock);
2210 * aborting transmits occurs when finalizing the connection.
2211 * The connection is finalized on error.
2212 * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2213 * override the value already set in tx->tx_hstatus above.
2215 kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2219 kiblnd_tx_may_discard(struct kib_conn *conn)
2225 spin_lock(&conn->ibc_lock);
2227 list_for_each_entry_safe(tx, nxt, &conn->ibc_zombie_txs, tx_list) {
2228 if (tx->tx_sending > 0 && tx->tx_lntmsg[0] &&
2229 lnet_md_discarded(tx->tx_lntmsg[0]->msg_md)) {
2231 if (tx->tx_sending == 0) {
2232 kiblnd_conn_decref(tx->tx_conn);
2239 spin_unlock(&conn->ibc_lock);
2244 kiblnd_finalise_conn(struct kib_conn *conn)
2246 LASSERT (!in_interrupt());
2247 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2249 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2250 * for connections that didn't get as far as being connected, because
2251 * rdma_disconnect() does this for free. */
2252 kiblnd_abort_receives(conn);
2254 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2256 /* Complete all tx descs not waiting for sends to complete.
2257 * NB we should be safe from RDMA now that the QP has changed state */
2259 CDEBUG(D_NET, "abort connection with %s\n", libcfs_nid2str(conn->ibc_peer->ibp_nid));
2261 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2262 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2263 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2264 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2265 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2267 kiblnd_handle_early_rxs(conn);
2271 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2275 unsigned long flags;
2276 enum lnet_msg_hstatus hstatus;
2278 LASSERT(error != 0);
2279 LASSERT(!in_interrupt());
2281 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2284 LASSERT(peer_ni->ibp_connecting > 0);
2285 peer_ni->ibp_connecting--;
2287 LASSERT (peer_ni->ibp_accepting > 0);
2288 peer_ni->ibp_accepting--;
2291 if (kiblnd_peer_connecting(peer_ni)) {
2292 /* another connection attempt under way... */
2293 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2298 peer_ni->ibp_reconnected = 0;
2299 if (list_empty(&peer_ni->ibp_conns)) {
2300 /* Take peer_ni's blocked transmits to complete with error */
2301 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2303 if (kiblnd_peer_active(peer_ni))
2304 kiblnd_unlink_peer_locked(peer_ni);
2306 peer_ni->ibp_error = error;
2308 /* Can't have blocked transmits if there are connections */
2309 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2312 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2314 kiblnd_peer_notify(peer_ni);
2316 if (list_empty(&zombies))
2319 CNETERR("Deleting messages for %s: connection failed\n",
2320 libcfs_nid2str(peer_ni->ibp_nid));
2325 hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
2328 hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
2331 hstatus = LNET_MSG_STATUS_LOCAL_DROPPED;
2335 kiblnd_txlist_done(&zombies, error, hstatus);
2339 kiblnd_connreq_done(struct kib_conn *conn, int status)
2341 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2344 unsigned long flags;
2347 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2349 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2350 libcfs_nid2str(peer_ni->ibp_nid), active,
2351 conn->ibc_version, status);
2353 LASSERT (!in_interrupt());
2354 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2355 peer_ni->ibp_connecting > 0) ||
2356 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2357 peer_ni->ibp_accepting > 0));
2359 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2360 conn->ibc_connvars = NULL;
2363 /* failed to establish connection */
2364 kiblnd_peer_connect_failed(peer_ni, active, status);
2365 kiblnd_finalise_conn(conn);
2369 /* connection established */
2370 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2372 conn->ibc_last_send = ktime_get();
2373 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2374 kiblnd_peer_alive(peer_ni);
2376 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2377 * peer_ni instance... */
2378 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2379 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2380 peer_ni->ibp_reconnected = 0;
2382 peer_ni->ibp_connecting--;
2384 peer_ni->ibp_accepting--;
2386 if (peer_ni->ibp_version == 0) {
2387 peer_ni->ibp_version = conn->ibc_version;
2388 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2391 if (peer_ni->ibp_version != conn->ibc_version ||
2392 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2393 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2394 conn->ibc_incarnation);
2395 peer_ni->ibp_version = conn->ibc_version;
2396 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2399 /* grab pending txs while I have the lock */
2400 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2402 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2403 conn->ibc_comms_error != 0) { /* error has happened already */
2405 /* start to shut down connection */
2406 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2407 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2409 kiblnd_txlist_done(&txs, -ECONNABORTED,
2410 LNET_MSG_STATUS_LOCAL_ERROR);
2415 /* +1 ref for myself, this connection is visible to other threads
2416 * now, refcount of peer:ibp_conns can be released by connection
2417 * close from either a different thread, or the calling of
2418 * kiblnd_check_sends_locked() below. See bz21911 for details.
2420 kiblnd_conn_addref(conn);
2421 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2423 /* Schedule blocked txs
2424 * Note: if we are running with conns_per_peer > 1, these blocked
2425 * txs will all get scheduled to the first connection which gets
2426 * scheduled. We won't be using round robin on this first batch.
2428 spin_lock(&conn->ibc_lock);
2429 while ((tx = list_first_entry_or_null(&txs, struct kib_tx,
2430 tx_list)) != NULL) {
2431 list_del(&tx->tx_list);
2433 kiblnd_queue_tx_locked(tx, conn);
2435 kiblnd_check_sends_locked(conn);
2436 spin_unlock(&conn->ibc_lock);
2438 /* schedule blocked rxs */
2439 kiblnd_handle_early_rxs(conn);
2440 kiblnd_conn_decref(conn);
2444 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2448 #ifdef HAVE_RDMA_REJECT_4ARGS
2449 rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2451 rc = rdma_reject(cmid, rej, sizeof(*rej));
2455 CWARN("Error %d sending reject\n", rc);
2459 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2461 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2462 struct kib_msg *reqmsg = priv;
2463 struct kib_msg *ackmsg;
2464 struct kib_dev *ibdev;
2465 struct kib_peer_ni *peer_ni;
2466 struct kib_peer_ni *peer2;
2467 struct kib_conn *conn;
2468 struct lnet_ni *ni = NULL;
2469 struct kib_net *net = NULL;
2471 struct rdma_conn_param cp;
2473 int version = IBLND_MSG_VERSION;
2474 unsigned long flags;
2476 struct sockaddr_in *peer_addr;
2478 LASSERT(!in_interrupt());
2479 /* cmid inherits 'context' from the corresponding listener id */
2480 ibdev = cmid->context;
2483 memset(&rej, 0, sizeof(rej));
2484 rej.ibr_magic = IBLND_MSG_MAGIC;
2485 rej.ibr_why = IBLND_REJECT_FATAL;
2486 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2488 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2489 if (*kiblnd_tunables.kib_require_priv_port &&
2490 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2491 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2492 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2493 &ip, ntohs(peer_addr->sin_port));
2497 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2498 CERROR("Short connection request\n");
2502 /* Future protocol version compatibility support! If the
2503 * o2iblnd-specific protocol changes, or when LNET unifies
2504 * protocols over all LNDs, the initial connection will
2505 * negotiate a protocol version. I trap this here to avoid
2506 * console errors; the reject tells the peer_ni which protocol I
2508 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2509 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2511 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2512 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2513 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2515 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2516 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2517 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2520 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2522 CERROR("Can't parse connection request: %d\n", rc);
2526 nid = reqmsg->ibm_srcnid;
2527 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2530 net = (struct kib_net *)ni->ni_data;
2531 rej.ibr_incarnation = net->ibn_incarnation;
2534 if (ni == NULL || /* no matching net */
2535 lnet_nid_to_nid4(&ni->ni_nid) !=
2536 reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2537 net->ibn_dev != ibdev) { /* wrong device */
2538 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): bad dst nid %s\n", libcfs_nid2str(nid),
2539 ni ? libcfs_nidstr(&ni->ni_nid) : "NA",
2540 ibdev->ibd_ifname, ibdev->ibd_nnets,
2542 libcfs_nid2str(reqmsg->ibm_dstnid));
2547 /* check time stamp as soon as possible */
2548 if (reqmsg->ibm_dststamp != 0 &&
2549 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2550 CWARN("Stale connection request\n");
2551 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2555 /* I can accept peer_ni's version */
2556 version = reqmsg->ibm_version;
2558 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2559 CERROR("Unexpected connreq msg type: %x from %s\n",
2560 reqmsg->ibm_type, libcfs_nid2str(nid));
2564 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2565 kiblnd_msg_queue_size(version, ni)) {
2566 CERROR("Can't accept conn from %s, queue depth too large: %d (<=%d wanted)\n",
2567 libcfs_nid2str(nid),
2568 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2569 kiblnd_msg_queue_size(version, ni));
2571 if (version == IBLND_MSG_VERSION)
2572 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2577 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2578 IBLND_MAX_RDMA_FRAGS) {
2579 CWARN("Can't accept conn from %s (version %x): max_frags %d too large (%d wanted)\n",
2580 libcfs_nid2str(nid), version,
2581 reqmsg->ibm_u.connparams.ibcp_max_frags,
2582 IBLND_MAX_RDMA_FRAGS);
2584 if (version >= IBLND_MSG_VERSION)
2585 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2588 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2589 IBLND_MAX_RDMA_FRAGS &&
2590 net->ibn_fmr_ps == NULL) {
2591 CWARN("Can't accept conn from %s (version %x): max_frags %d incompatible without FMR pool (%d wanted)\n",
2592 libcfs_nid2str(nid), version,
2593 reqmsg->ibm_u.connparams.ibcp_max_frags,
2594 IBLND_MAX_RDMA_FRAGS);
2596 if (version == IBLND_MSG_VERSION)
2597 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2602 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2603 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2604 libcfs_nid2str(nid),
2605 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2610 /* assume 'nid' is a new peer_ni; create */
2611 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2613 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2614 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2618 /* We have validated the peer's parameters so use those */
2619 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2620 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2622 write_lock_irqsave(g_lock, flags);
2624 peer2 = kiblnd_find_peer_locked(ni, nid);
2625 if (peer2 != NULL) {
2626 if (peer2->ibp_version == 0) {
2627 peer2->ibp_version = version;
2628 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2631 /* not the guy I've talked with */
2632 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2633 peer2->ibp_version != version) {
2634 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2636 if (kiblnd_peer_active(peer2)) {
2637 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2638 peer2->ibp_version = version;
2640 write_unlock_irqrestore(g_lock, flags);
2642 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2643 libcfs_nid2str(nid), peer2->ibp_version, version,
2644 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2646 kiblnd_peer_decref(peer_ni);
2647 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2651 /* Tie-break connection race in favour of the higher NID.
2652 * If we keep running into a race condition multiple times,
2653 * we have to assume that the connection attempt with the
2654 * higher NID is stuck in a connecting state and will never
2655 * recover. As such, we pass through this if-block and let
2656 * the lower NID connection win so we can move forward.
2658 if (peer2->ibp_connecting != 0 &&
2659 nid < lnet_nid_to_nid4(&ni->ni_nid) &&
2660 peer2->ibp_races < MAX_CONN_RACES_BEFORE_ABORT) {
2662 write_unlock_irqrestore(g_lock, flags);
2664 CDEBUG(D_NET, "Conn race %s\n",
2665 libcfs_nid2str(peer2->ibp_nid));
2667 kiblnd_peer_decref(peer_ni);
2668 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2671 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2672 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2673 libcfs_nid2str(peer2->ibp_nid),
2674 MAX_CONN_RACES_BEFORE_ABORT);
2676 * passive connection is allowed even this peer_ni is waiting for
2679 peer2->ibp_reconnecting = 0;
2680 peer2->ibp_races = 0;
2681 peer2->ibp_accepting++;
2682 kiblnd_peer_addref(peer2);
2684 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2685 * so copy validated parameters since we now know what the
2686 * peer_ni's limits are */
2687 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2688 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2690 write_unlock_irqrestore(g_lock, flags);
2691 kiblnd_peer_decref(peer_ni);
2694 /* Brand new peer_ni */
2695 LASSERT(peer_ni->ibp_accepting == 0);
2696 LASSERT(peer_ni->ibp_version == 0 &&
2697 peer_ni->ibp_incarnation == 0);
2699 peer_ni->ibp_accepting = 1;
2700 peer_ni->ibp_version = version;
2701 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2703 /* I have a ref on ni that prevents it being shutdown */
2704 LASSERT(net->ibn_shutdown == 0);
2706 kiblnd_peer_addref(peer_ni);
2707 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
2709 write_unlock_irqrestore(g_lock, flags);
2712 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT,
2715 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2716 kiblnd_peer_decref(peer_ni);
2717 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2721 /* conn now "owns" cmid, so I return success from here on to ensure the
2722 * CM callback doesn't destroy cmid.
2724 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2725 conn->ibc_credits = conn->ibc_queue_depth;
2726 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2727 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2728 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2730 ackmsg = &conn->ibc_connvars->cv_msg;
2731 memset(ackmsg, 0, sizeof(*ackmsg));
2733 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2734 sizeof(ackmsg->ibm_u.connparams));
2735 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2736 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2737 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2739 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2741 memset(&cp, 0, sizeof(cp));
2742 cp.private_data = ackmsg;
2743 cp.private_data_len = ackmsg->ibm_nob;
2744 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2745 cp.initiator_depth = 0;
2746 cp.flow_control = 1;
2747 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2748 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2750 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2752 rc = rdma_accept(cmid, &cp);
2754 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2755 rej.ibr_version = version;
2756 rej.ibr_why = IBLND_REJECT_FATAL;
2758 kiblnd_reject(cmid, &rej);
2759 kiblnd_connreq_done(conn, rc);
2760 kiblnd_conn_decref(conn);
2768 rej.ibr_cp.ibcp_queue_depth =
2769 kiblnd_msg_queue_size(version, ni);
2770 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2774 rej.ibr_version = version;
2775 kiblnd_reject(cmid, &rej);
2777 return -ECONNREFUSED;
2781 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2782 u64 incarnation, int why, struct kib_connparams *cp)
2784 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2785 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2787 int msg_size = IBLND_MSG_SIZE;
2791 unsigned long flags;
2793 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2794 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2797 msg_size = cp->ibcp_max_msg_size;
2798 frag_num = cp->ibcp_max_frags;
2799 queue_dep = cp->ibcp_queue_depth;
2802 write_lock_irqsave(glock, flags);
2803 /* retry connection if it's still needed and no other connection
2804 * attempts (active or passive) are in progress
2805 * NB: reconnect is still needed even when ibp_tx_queue is
2806 * empty if ibp_version != version because reconnect may be
2809 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2810 peer_ni->ibp_version != version) &&
2811 peer_ni->ibp_connecting &&
2812 peer_ni->ibp_accepting == 0;
2823 case IBLND_REJECT_RDMA_FRAGS: {
2825 reason = "can't negotiate max frags";
2829 if (conn->ibc_max_frags <= frag_num) {
2830 reason = "unsupported max frags";
2834 peer_ni->ibp_max_frags = frag_num;
2835 reason = "rdma fragments";
2838 case IBLND_REJECT_MSG_QUEUE_SIZE:
2840 reason = "can't negotiate queue depth";
2843 if (conn->ibc_queue_depth <= queue_dep) {
2844 reason = "unsupported queue depth";
2848 peer_ni->ibp_queue_depth = queue_dep;
2849 reason = "queue depth";
2852 case IBLND_REJECT_CONN_STALE:
2856 case IBLND_REJECT_CONN_RACE:
2857 reason = "conn race";
2860 case IBLND_REJECT_CONN_UNCOMPAT:
2861 reason = "version negotiation";
2865 conn->ibc_reconnect = 1;
2866 peer_ni->ibp_reconnecting++;
2867 peer_ni->ibp_version = version;
2868 if (incarnation != 0)
2869 peer_ni->ibp_incarnation = incarnation;
2871 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2873 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2874 libcfs_nid2str(peer_ni->ibp_nid),
2875 reconnect ? "reconnect" : "don't reconnect",
2876 reason, IBLND_MSG_VERSION, version, msg_size,
2877 conn->ibc_queue_depth, queue_dep,
2878 conn->ibc_max_frags, frag_num);
2880 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2881 * while destroying the zombie
2886 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2888 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2889 int status = -ECONNREFUSED;
2891 LASSERT (!in_interrupt());
2892 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2895 case IB_CM_REJ_STALE_CONN:
2896 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2897 IBLND_REJECT_CONN_STALE, NULL);
2900 case IB_CM_REJ_INVALID_SERVICE_ID:
2901 status = -EHOSTUNREACH;
2902 CNETERR("%s rejected: no listener at %d\n",
2903 libcfs_nid2str(peer_ni->ibp_nid),
2904 *kiblnd_tunables.kib_service);
2907 case IB_CM_REJ_CONSUMER_DEFINED:
2908 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2909 struct kib_rej *rej = priv;
2910 struct kib_connparams *cp = NULL;
2912 __u64 incarnation = -1;
2914 /* NB. default incarnation is -1 because:
2915 * a) V1 will ignore dst incarnation in connreq.
2916 * b) V2 will provide incarnation while rejecting me,
2917 * -1 will be overwrote.
2919 * if I try to connect to a V1 peer_ni with V2 protocol,
2920 * it rejected me then upgrade to V2, I have no idea
2921 * about the upgrading and try to reconnect with V1,
2922 * in this case upgraded V2 can find out I'm trying to
2923 * talk to the old guy and reject me(incarnation is -1).
2926 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2927 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2928 __swab32s(&rej->ibr_magic);
2929 __swab16s(&rej->ibr_version);
2933 if (priv_nob >= sizeof(struct kib_rej) &&
2934 rej->ibr_version > IBLND_MSG_VERSION_1) {
2935 /* priv_nob is always 148 in current version
2936 * of OFED, so we still need to check version.
2937 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE)
2942 __swab64s(&rej->ibr_incarnation);
2943 __swab16s(&cp->ibcp_queue_depth);
2944 __swab16s(&cp->ibcp_max_frags);
2945 __swab32s(&cp->ibcp_max_msg_size);
2948 incarnation = rej->ibr_incarnation;
2951 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2952 rej->ibr_magic != LNET_PROTO_MAGIC) {
2953 CERROR("%s rejected: consumer defined fatal error\n",
2954 libcfs_nid2str(peer_ni->ibp_nid));
2958 if (rej->ibr_version != IBLND_MSG_VERSION &&
2959 rej->ibr_version != IBLND_MSG_VERSION_1) {
2960 CERROR("%s rejected: o2iblnd version %x error\n",
2961 libcfs_nid2str(peer_ni->ibp_nid),
2966 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2967 rej->ibr_version == IBLND_MSG_VERSION_1) {
2968 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2969 libcfs_nid2str(peer_ni->ibp_nid),
2972 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2973 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2976 switch (rej->ibr_why) {
2977 case IBLND_REJECT_CONN_RACE:
2978 case IBLND_REJECT_CONN_STALE:
2979 case IBLND_REJECT_CONN_UNCOMPAT:
2980 case IBLND_REJECT_MSG_QUEUE_SIZE:
2981 case IBLND_REJECT_RDMA_FRAGS:
2982 kiblnd_check_reconnect(conn, rej->ibr_version,
2987 case IBLND_REJECT_NO_RESOURCES:
2988 CERROR("%s rejected: o2iblnd no resources\n",
2989 libcfs_nid2str(peer_ni->ibp_nid));
2992 case IBLND_REJECT_FATAL:
2993 CERROR("%s rejected: o2iblnd fatal error\n",
2994 libcfs_nid2str(peer_ni->ibp_nid));
2998 CERROR("%s rejected: o2iblnd reason %d\n",
2999 libcfs_nid2str(peer_ni->ibp_nid),
3007 CNETERR("%s rejected: reason %d, size %d\n",
3008 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
3012 kiblnd_connreq_done(conn, status);
3016 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
3018 struct kib_peer_ni *peer_ni = conn->ibc_peer;
3019 struct lnet_ni *ni = peer_ni->ibp_ni;
3020 struct kib_net *net = ni->ni_data;
3021 struct kib_msg *msg = priv;
3022 int ver = conn->ibc_version;
3023 int rc = kiblnd_unpack_msg(msg, priv_nob);
3024 unsigned long flags;
3026 LASSERT (net != NULL);
3029 CERROR("Can't unpack connack from %s: %d\n",
3030 libcfs_nid2str(peer_ni->ibp_nid), rc);
3034 if (msg->ibm_type != IBLND_MSG_CONNACK) {
3035 CERROR("Unexpected message %d from %s\n",
3036 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
3041 if (ver != msg->ibm_version) {
3042 CERROR("%s replied version %x is different with "
3043 "requested version %x\n",
3044 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
3049 if (msg->ibm_u.connparams.ibcp_queue_depth >
3050 conn->ibc_queue_depth) {
3051 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
3052 libcfs_nid2str(peer_ni->ibp_nid),
3053 msg->ibm_u.connparams.ibcp_queue_depth,
3054 conn->ibc_queue_depth);
3059 if (msg->ibm_u.connparams.ibcp_max_frags >
3060 conn->ibc_max_frags) {
3061 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3062 libcfs_nid2str(peer_ni->ibp_nid),
3063 msg->ibm_u.connparams.ibcp_max_frags,
3064 conn->ibc_max_frags);
3069 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3070 CERROR("%s max message size %d too big (%d max)\n",
3071 libcfs_nid2str(peer_ni->ibp_nid),
3072 msg->ibm_u.connparams.ibcp_max_msg_size,
3078 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3079 if (msg->ibm_dstnid == lnet_nid_to_nid4(&ni->ni_nid) &&
3080 msg->ibm_dststamp == net->ibn_incarnation)
3084 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3087 CERROR("Bad connection reply from %s, rc = %d, "
3088 "version: %x max_frags: %d\n",
3089 libcfs_nid2str(peer_ni->ibp_nid), rc,
3090 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3094 conn->ibc_incarnation = msg->ibm_srcstamp;
3095 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3096 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3097 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3098 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3099 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3100 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3102 kiblnd_connreq_done(conn, 0);
3106 /* NB My QP has already established itself, so I handle anything going
3107 * wrong here by setting ibc_comms_error.
3108 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3109 * immediately tears it down. */
3112 conn->ibc_comms_error = rc;
3113 kiblnd_connreq_done(conn, 0);
3117 kiblnd_active_connect(struct rdma_cm_id *cmid)
3119 struct kib_peer_ni *peer_ni = cmid->context;
3120 struct kib_conn *conn;
3121 struct kib_msg *msg;
3122 struct rdma_conn_param cp;
3125 unsigned long flags;
3128 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3130 incarnation = peer_ni->ibp_incarnation;
3131 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3132 peer_ni->ibp_version;
3134 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3136 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3139 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3140 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3144 /* conn "owns" cmid now, so I return success from here on to ensure the
3145 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3148 msg = &conn->ibc_connvars->cv_msg;
3150 memset(msg, 0, sizeof(*msg));
3151 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3152 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3153 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3154 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3156 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3157 0, peer_ni->ibp_nid, incarnation);
3159 memset(&cp, 0, sizeof(cp));
3160 cp.private_data = msg;
3161 cp.private_data_len = msg->ibm_nob;
3162 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3163 cp.initiator_depth = 0;
3164 cp.flow_control = 1;
3165 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3166 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3168 LASSERT(cmid->context == (void *)conn);
3169 LASSERT(conn->ibc_cmid == cmid);
3170 rc = rdma_connect_locked(cmid, &cp);
3172 CERROR("Can't connect to %s: %d\n",
3173 libcfs_nid2str(peer_ni->ibp_nid), rc);
3174 kiblnd_connreq_done(conn, rc);
3175 kiblnd_conn_decref(conn);
3182 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3184 struct kib_peer_ni *peer_ni;
3185 struct kib_conn *conn;
3188 switch (event->event) {
3190 CERROR("Unexpected event: %d, status: %d\n",
3191 event->event, event->status);
3194 case RDMA_CM_EVENT_CONNECT_REQUEST:
3195 /* destroy cmid on failure */
3196 rc = kiblnd_passive_connect(cmid,
3197 (void *)KIBLND_CONN_PARAM(event),
3198 KIBLND_CONN_PARAM_LEN(event));
3199 CDEBUG(D_NET, "connreq: %d\n", rc);
3202 case RDMA_CM_EVENT_ADDR_ERROR:
3203 peer_ni = cmid->context;
3204 CNETERR("%s: ADDR ERROR %d\n",
3205 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3206 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3207 kiblnd_peer_decref(peer_ni);
3208 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3210 case RDMA_CM_EVENT_ADDR_RESOLVED:
3211 peer_ni = cmid->context;
3213 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3214 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3216 if (event->status != 0) {
3217 CNETERR("Can't resolve address for %s: %d\n",
3218 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3221 rc = rdma_resolve_route(
3222 cmid, kiblnd_timeout() * 1000);
3224 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3225 struct kib_dev *dev = net->ibn_dev;
3227 CDEBUG(D_NET, "%s: connection bound to "\
3229 libcfs_nid2str(peer_ni->ibp_nid),
3231 &dev->ibd_ifip, cmid->device->name);
3236 /* Can't initiate route resolution */
3237 CERROR("Can't resolve route for %s: %d\n",
3238 libcfs_nid2str(peer_ni->ibp_nid), rc);
3240 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3241 kiblnd_peer_decref(peer_ni);
3242 return rc; /* rc != 0 destroys cmid */
3244 case RDMA_CM_EVENT_ROUTE_ERROR:
3245 peer_ni = cmid->context;
3246 CNETERR("%s: ROUTE ERROR %d\n",
3247 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3248 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3249 kiblnd_peer_decref(peer_ni);
3250 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3252 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3253 peer_ni = cmid->context;
3254 CDEBUG(D_NET,"%s Route resolved: %d\n",
3255 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3257 if (event->status == 0)
3258 return kiblnd_active_connect(cmid);
3260 CNETERR("Can't resolve route for %s: %d\n",
3261 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3262 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3263 kiblnd_peer_decref(peer_ni);
3264 return event->status; /* rc != 0 destroys cmid */
3266 case RDMA_CM_EVENT_UNREACHABLE:
3267 conn = cmid->context;
3268 CNETERR("%s: UNREACHABLE %d, ibc_state: %d\n",
3269 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3272 LASSERT(conn->ibc_state != IBLND_CONN_ESTABLISHED &&
3273 conn->ibc_state != IBLND_CONN_INIT);
3274 if (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3275 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT) {
3276 kiblnd_connreq_done(conn, -ENETDOWN);
3277 kiblnd_conn_decref(conn);
3281 case RDMA_CM_EVENT_CONNECT_ERROR:
3282 conn = cmid->context;
3283 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3284 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3285 CNETERR("%s: CONNECT ERROR %d\n",
3286 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3287 kiblnd_connreq_done(conn, -ENOTCONN);
3288 kiblnd_conn_decref(conn);
3291 case RDMA_CM_EVENT_REJECTED:
3292 conn = cmid->context;
3293 switch (conn->ibc_state) {
3297 case IBLND_CONN_PASSIVE_WAIT:
3298 CERROR ("%s: REJECTED %d\n",
3299 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3301 kiblnd_connreq_done(conn, -ECONNRESET);
3304 case IBLND_CONN_ACTIVE_CONNECT:
3305 kiblnd_rejected(conn, event->status,
3306 (void *)KIBLND_CONN_PARAM(event),
3307 KIBLND_CONN_PARAM_LEN(event));
3310 kiblnd_conn_decref(conn);
3313 case RDMA_CM_EVENT_ESTABLISHED:
3314 conn = cmid->context;
3315 switch (conn->ibc_state) {
3319 case IBLND_CONN_PASSIVE_WAIT:
3320 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3321 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3322 kiblnd_connreq_done(conn, 0);
3325 case IBLND_CONN_ACTIVE_CONNECT:
3326 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3327 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3328 kiblnd_check_connreply(conn,
3329 (void *)KIBLND_CONN_PARAM(event),
3330 KIBLND_CONN_PARAM_LEN(event));
3333 /* net keeps its ref on conn! */
3336 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3337 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3340 case RDMA_CM_EVENT_DISCONNECTED:
3341 conn = cmid->context;
3342 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3343 CERROR("%s DISCONNECTED\n",
3344 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3345 kiblnd_connreq_done(conn, -ECONNRESET);
3347 kiblnd_close_conn(conn, 0);
3349 kiblnd_conn_decref(conn);
3350 cmid->context = NULL;
3353 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3354 LCONSOLE_ERROR_MSG(0x131,
3355 "Received notification of device removal\n"
3356 "Please shutdown LNET to allow this to proceed\n");
3357 /* Can't remove network from underneath LNET for now, so I have
3361 case RDMA_CM_EVENT_ADDR_CHANGE:
3362 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3368 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3372 list_for_each_entry(tx, txs, tx_list) {
3373 if (txs != &conn->ibc_active_txs) {
3374 LASSERT(tx->tx_queued);
3376 LASSERT(!tx->tx_queued);
3377 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3380 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3381 CERROR("Timed out tx: %s(WSQ:%d%d%d), %lld seconds\n",
3382 kiblnd_queue2str(conn, txs),
3383 tx->tx_waiting, tx->tx_sending, tx->tx_queued,
3385 ktime_ms_delta(ktime_get(),
3386 tx->tx_deadline) / MSEC_PER_SEC);
3395 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3397 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3398 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3399 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3400 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3401 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3405 kiblnd_check_conns (int idx)
3408 LIST_HEAD(checksends);
3409 LIST_HEAD(timedout_txs);
3410 struct hlist_head *peers = &kiblnd_data.kib_peers[idx];
3411 struct kib_peer_ni *peer_ni;
3412 struct kib_conn *conn;
3413 struct kib_tx *tx, *tx_tmp;
3414 unsigned long flags;
3416 /* NB. We expect to have a look at all the peers and not find any
3417 * RDMAs to time out, so we just use a shared lock while we
3420 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3422 hlist_for_each_entry(peer_ni, peers, ibp_list) {
3423 /* Check tx_deadline */
3424 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3425 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3426 CWARN("Timed out tx for %s: %lld seconds\n",
3427 libcfs_nid2str(peer_ni->ibp_nid),
3428 ktime_ms_delta(ktime_get(),
3429 tx->tx_deadline) / MSEC_PER_SEC);
3430 list_move(&tx->tx_list, &timedout_txs);
3434 list_for_each_entry(conn, &peer_ni->ibp_conns, ibc_list) {
3438 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3440 spin_lock(&conn->ibc_lock);
3442 sendnoop = kiblnd_need_noop(conn);
3443 timedout = kiblnd_conn_timed_out_locked(conn);
3444 if (!sendnoop && !timedout) {
3445 spin_unlock(&conn->ibc_lock);
3450 CERROR("Timed out RDMA with %s (%lld): c: %u, oc: %u, rc: %u\n",
3451 libcfs_nid2str(peer_ni->ibp_nid),
3453 - peer_ni->ibp_last_alive,
3455 conn->ibc_outstanding_credits,
3456 conn->ibc_reserved_credits);
3457 list_add(&conn->ibc_connd_list, &closes);
3459 list_add(&conn->ibc_connd_list, &checksends);
3461 /* +ref for 'closes' or 'checksends' */
3462 kiblnd_conn_addref(conn);
3464 spin_unlock(&conn->ibc_lock);
3468 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3470 if (!list_empty(&timedout_txs))
3471 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3472 LNET_MSG_STATUS_NETWORK_TIMEOUT);
3474 /* Handle timeout by closing the whole
3475 * connection. We can only be sure RDMA activity
3476 * has ceased once the QP has been modified.
3478 while ((conn = list_first_entry_or_null(&closes,
3480 ibc_connd_list)) != NULL) {
3481 list_del(&conn->ibc_connd_list);
3482 kiblnd_close_conn(conn, -ETIMEDOUT);
3483 kiblnd_conn_decref(conn);
3486 /* In case we have enough credits to return via a
3487 * NOOP, but there were no non-blocking tx descs
3488 * free to do it last time...
3490 while ((conn = list_first_entry_or_null(&checksends,
3492 ibc_connd_list)) != NULL) {
3493 list_del(&conn->ibc_connd_list);
3495 spin_lock(&conn->ibc_lock);
3496 kiblnd_check_sends_locked(conn);
3497 spin_unlock(&conn->ibc_lock);
3499 kiblnd_conn_decref(conn);
3504 kiblnd_disconnect_conn(struct kib_conn *conn)
3506 LASSERT (!in_interrupt());
3507 LASSERT (current == kiblnd_data.kib_connd);
3508 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3510 rdma_disconnect(conn->ibc_cmid);
3511 kiblnd_finalise_conn(conn);
3513 kiblnd_peer_notify(conn->ibc_peer);
3517 * High-water for reconnection to the same peer_ni, reconnection attempt should
3518 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3520 #define KIB_RECONN_HIGH_RACE 10
3522 * Allow connd to take a break and handle other things after consecutive
3523 * reconnection attemps.
3525 #define KIB_RECONN_BREAK 100
3528 kiblnd_connd (void *arg)
3530 spinlock_t *lock = &kiblnd_data.kib_connd_lock;
3531 wait_queue_entry_t wait;
3532 unsigned long flags;
3533 struct kib_conn *conn;
3538 unsigned long deadline = jiffies;
3541 kiblnd_data.kib_connd = current;
3543 spin_lock_irqsave(lock, flags);
3545 while (!kiblnd_data.kib_shutdown) {
3548 dropped_lock = false;
3550 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_zombies,
3551 struct kib_conn, ibc_list);
3553 struct kib_peer_ni *peer_ni = NULL;
3555 list_del(&conn->ibc_list);
3556 if (conn->ibc_reconnect) {
3557 peer_ni = conn->ibc_peer;
3558 kiblnd_peer_addref(peer_ni);
3561 spin_unlock_irqrestore(lock, flags);
3562 dropped_lock = true;
3564 kiblnd_destroy_conn(conn);
3566 spin_lock_irqsave(lock, flags);
3568 LIBCFS_FREE(conn, sizeof(*conn));
3572 conn->ibc_peer = peer_ni;
3573 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3574 list_add_tail(&conn->ibc_list,
3575 &kiblnd_data.kib_reconn_list);
3577 list_add_tail(&conn->ibc_list,
3578 &kiblnd_data.kib_reconn_wait);
3581 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_conns,
3582 struct kib_conn, ibc_list);
3586 list_del(&conn->ibc_list);
3588 spin_unlock_irqrestore(lock, flags);
3589 dropped_lock = true;
3591 kiblnd_disconnect_conn(conn);
3592 wait = conn->ibc_waits;
3593 if (wait == 0) /* keep ref for connd_wait, see below */
3594 kiblnd_conn_decref(conn);
3596 spin_lock_irqsave(lock, flags);
3599 list_add_tail(&conn->ibc_list,
3600 &kiblnd_data.kib_connd_waits);
3603 while (reconn < KIB_RECONN_BREAK) {
3604 if (kiblnd_data.kib_reconn_sec !=
3605 ktime_get_real_seconds()) {
3606 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3607 list_splice_init(&kiblnd_data.kib_reconn_wait,
3608 &kiblnd_data.kib_reconn_list);
3611 conn = list_first_entry_or_null(&kiblnd_data.kib_reconn_list,
3612 struct kib_conn, ibc_list);
3616 list_del(&conn->ibc_list);
3618 spin_unlock_irqrestore(lock, flags);
3619 dropped_lock = true;
3621 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3622 kiblnd_peer_decref(conn->ibc_peer);
3623 LIBCFS_FREE(conn, sizeof(*conn));
3625 spin_lock_irqsave(lock, flags);
3628 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_waits,
3629 struct kib_conn, ibc_list);
3631 list_del(&conn->ibc_list);
3632 spin_unlock_irqrestore(lock, flags);
3634 dropped_lock = kiblnd_tx_may_discard(conn);
3636 kiblnd_conn_decref(conn);
3638 spin_lock_irqsave(lock, flags);
3640 list_add_tail(&conn->ibc_list,
3641 &kiblnd_data.kib_connd_waits);
3644 /* careful with the jiffy wrap... */
3645 timeout = (int)(deadline - jiffies);
3649 int chunk = HASH_SIZE(kiblnd_data.kib_peers);
3650 unsigned int lnd_timeout;
3652 spin_unlock_irqrestore(lock, flags);
3653 dropped_lock = true;
3655 /* Time to check for RDMA timeouts on a few more
3656 * peers: I do checks every 'p' seconds on a
3657 * proportion of the peer_ni table and I need to check
3658 * every connection 'n' times within a timeout
3659 * interval, to ensure I detect a timeout on any
3660 * connection within (n+1)/n times the timeout
3664 lnd_timeout = kiblnd_timeout();
3665 if (lnd_timeout > n * p)
3666 chunk = (chunk * n * p) / lnd_timeout;
3670 for (i = 0; i < chunk; i++) {
3671 kiblnd_check_conns(peer_index);
3672 peer_index = (peer_index + 1) %
3673 HASH_SIZE(kiblnd_data.kib_peers);
3676 deadline += cfs_time_seconds(p);
3677 spin_lock_irqsave(lock, flags);
3683 /* Nothing to do for 'timeout' */
3684 set_current_state(TASK_INTERRUPTIBLE);
3685 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3686 spin_unlock_irqrestore(lock, flags);
3688 schedule_timeout(timeout);
3690 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3691 spin_lock_irqsave(lock, flags);
3694 spin_unlock_irqrestore(lock, flags);
3696 kiblnd_thread_fini();
3701 kiblnd_qp_event(struct ib_event *event, void *arg)
3703 struct kib_conn *conn = arg;
3705 switch (event->event) {
3706 case IB_EVENT_COMM_EST:
3707 CDEBUG(D_NET, "%s established\n",
3708 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3709 /* We received a packet but connection isn't established
3710 * probably handshake packet was lost, so free to
3711 * force make connection established */
3712 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3715 case IB_EVENT_PORT_ERR:
3716 case IB_EVENT_DEVICE_FATAL:
3717 CERROR("Fatal device error for NI %s\n",
3718 libcfs_nidstr(&conn->ibc_peer->ibp_ni->ni_nid));
3719 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3722 case IB_EVENT_PORT_ACTIVE:
3723 CERROR("Port reactivated for NI %s\n",
3724 libcfs_nidstr(&conn->ibc_peer->ibp_ni->ni_nid));
3725 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3729 CERROR("%s: Async QP event type %d\n",
3730 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3736 kiblnd_complete (struct ib_wc *wc)
3738 switch (kiblnd_wreqid2type(wc->wr_id)) {
3743 if (wc->status != IB_WC_SUCCESS &&
3744 wc->status != IB_WC_WR_FLUSH_ERR)
3745 CNETERR("FastReg failed: %d\n", wc->status);
3748 case IBLND_WID_RDMA:
3749 /* We only get RDMA completion notification if it fails. All
3750 * subsequent work items, including the final SEND will fail
3751 * too. However we can't print out any more info about the
3752 * failing RDMA because 'tx' might be back on the idle list or
3753 * even reused already if we didn't manage to post all our work
3755 CNETERR("RDMA (tx: %p) failed: %d\n",
3756 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3760 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3764 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3771 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3773 /* NB I'm not allowed to schedule this conn once its refcount has
3774 * reached 0. Since fundamentally I'm racing with scheduler threads
3775 * consuming my CQ I could be called after all completions have
3776 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3777 * and this CQ is about to be destroyed so I NOOP. */
3778 struct kib_conn *conn = arg;
3779 struct kib_sched_info *sched = conn->ibc_sched;
3780 unsigned long flags;
3782 LASSERT(cq == conn->ibc_cq);
3784 spin_lock_irqsave(&sched->ibs_lock, flags);
3786 conn->ibc_ready = 1;
3788 if (!conn->ibc_scheduled &&
3789 (conn->ibc_nrx > 0 ||
3790 conn->ibc_nsends_posted > 0)) {
3791 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3792 conn->ibc_scheduled = 1;
3793 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3795 if (waitqueue_active(&sched->ibs_waitq))
3796 wake_up(&sched->ibs_waitq);
3799 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3803 kiblnd_cq_event(struct ib_event *event, void *arg)
3805 struct kib_conn *conn = arg;
3807 CERROR("%s: async CQ event type %d\n",
3808 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3812 kiblnd_scheduler(void *arg)
3814 long id = (long)arg;
3815 struct kib_sched_info *sched;
3816 struct kib_conn *conn;
3817 wait_queue_entry_t wait;
3818 unsigned long flags;
3825 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3827 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3829 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);
3832 spin_lock_irqsave(&sched->ibs_lock, flags);
3834 while (!kiblnd_data.kib_shutdown) {
3835 if (need_resched()) {
3836 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3840 spin_lock_irqsave(&sched->ibs_lock, flags);
3843 did_something = false;
3845 conn = list_first_entry_or_null(&sched->ibs_conns,
3849 /* take over kib_sched_conns' ref on conn... */
3850 LASSERT(conn->ibc_scheduled);
3851 list_del(&conn->ibc_sched_list);
3852 conn->ibc_ready = 0;
3854 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3856 wc.wr_id = IBLND_WID_INVAL;
3858 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3860 rc = ib_req_notify_cq(conn->ibc_cq,
3863 CWARN("%s: ib_req_notify_cq failed: %d, closing connection\n",
3864 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3865 kiblnd_close_conn(conn, -EIO);
3866 kiblnd_conn_decref(conn);
3867 spin_lock_irqsave(&sched->ibs_lock,
3872 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3875 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3877 "ib_poll_cq (rc: %d) returned invalid "
3878 "wr_id, opcode %d, status: %d, "
3879 "vendor_err: %d, conn: %s status: %d\n"
3880 "please upgrade firmware and OFED or "
3881 "contact vendor.\n", rc,
3882 wc.opcode, wc.status, wc.vendor_err,
3883 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3889 CWARN("%s: ib_poll_cq failed: %d, closing connection\n",
3890 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3892 kiblnd_close_conn(conn, -EIO);
3893 kiblnd_conn_decref(conn);
3894 spin_lock_irqsave(&sched->ibs_lock, flags);
3898 spin_lock_irqsave(&sched->ibs_lock, flags);
3900 if (rc != 0 || conn->ibc_ready) {
3901 /* There may be another completion waiting; get
3902 * another scheduler to check while I handle
3904 /* +1 ref for sched_conns */
3905 kiblnd_conn_addref(conn);
3906 list_add_tail(&conn->ibc_sched_list,
3908 if (waitqueue_active(&sched->ibs_waitq))
3909 wake_up(&sched->ibs_waitq);
3911 conn->ibc_scheduled = 0;
3915 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3916 kiblnd_complete(&wc);
3918 spin_lock_irqsave(&sched->ibs_lock, flags);
3921 kiblnd_conn_decref(conn); /* ..drop my ref from above */
3922 did_something = true;
3928 set_current_state(TASK_INTERRUPTIBLE);
3929 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3930 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3934 remove_wait_queue(&sched->ibs_waitq, &wait);
3935 set_current_state(TASK_RUNNING);
3936 spin_lock_irqsave(&sched->ibs_lock, flags);
3939 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3941 kiblnd_thread_fini();
3946 kiblnd_failover_thread(void *arg)
3948 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3949 struct kib_dev *dev;
3950 struct net *ns = arg;
3951 wait_queue_entry_t wait;
3952 unsigned long flags;
3955 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3958 write_lock_irqsave(glock, flags);
3960 while (!kiblnd_data.kib_shutdown) {
3961 bool do_failover = false;
3964 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3966 if (ktime_get_seconds() < dev->ibd_next_failover)
3973 list_del_init(&dev->ibd_fail_list);
3974 dev->ibd_failover = 1;
3975 write_unlock_irqrestore(glock, flags);
3977 rc = kiblnd_dev_failover(dev, ns);
3979 write_lock_irqsave(glock, flags);
3981 LASSERT(dev->ibd_failover);
3982 dev->ibd_failover = 0;
3983 if (rc >= 0) { /* Device is OK or failover succeed */
3984 dev->ibd_next_failover = ktime_get_seconds() + 3;
3988 /* failed to failover, retry later */
3989 dev->ibd_next_failover = ktime_get_seconds() +
3990 min(dev->ibd_failed_failover, 10);
3991 if (kiblnd_dev_can_failover(dev)) {
3992 list_add_tail(&dev->ibd_fail_list,
3993 &kiblnd_data.kib_failed_devs);
3999 /* long sleep if no more pending failover */
4000 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
4002 set_current_state(TASK_INTERRUPTIBLE);
4003 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
4004 write_unlock_irqrestore(glock, flags);
4006 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
4007 cfs_time_seconds(1));
4008 set_current_state(TASK_RUNNING);
4009 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
4010 write_lock_irqsave(glock, flags);
4012 if (!long_sleep || rc != 0)
4015 /* have a long sleep, routine check all active devices,
4016 * we need checking like this because if there is not active
4017 * connection on the dev and no SEND from local, we may listen
4018 * on wrong HCA for ever while there is a bonding failover
4020 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
4021 if (kiblnd_dev_can_failover(dev)) {
4022 list_add_tail(&dev->ibd_fail_list,
4023 &kiblnd_data.kib_failed_devs);
4028 write_unlock_irqrestore(glock, flags);
4030 kiblnd_thread_fini();