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 void kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx,
44 int type, int body_nob);
45 static int kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
46 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie);
47 static void kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn);
48 static void kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn);
50 static void kiblnd_unmap_tx(struct kib_tx *tx);
51 static void kiblnd_check_sends_locked(struct kib_conn *conn);
54 kiblnd_tx_done(struct kib_tx *tx)
56 struct lnet_msg *lntmsg[2];
60 LASSERT (!in_interrupt());
61 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
62 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
63 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer_ni response */
64 LASSERT (tx->tx_pool != NULL);
68 /* tx may have up to 2 lnet msgs to finalise */
69 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
70 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
73 if (tx->tx_conn != NULL) {
74 kiblnd_conn_decref(tx->tx_conn);
78 tx->tx_nwrq = tx->tx_nsge = 0;
81 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
83 /* delay finalize until my descs have been freed */
84 for (i = 0; i < 2; i++) {
85 if (lntmsg[i] == NULL)
88 /* propagate health status to LNet for requests */
89 if (i == 0 && lntmsg[i])
90 lntmsg[i]->msg_health_status = tx->tx_hstatus;
92 lnet_finalize(lntmsg[i], rc);
97 kiblnd_txlist_done(struct list_head *txlist, int status,
98 enum lnet_msg_hstatus hstatus)
102 while ((tx = list_first_entry_or_null(txlist,
105 list_del(&tx->tx_list);
108 tx->tx_status = status;
109 if (hstatus != LNET_MSG_STATUS_OK)
110 tx->tx_hstatus = hstatus;
115 static struct kib_tx *
116 kiblnd_get_idle_tx(struct lnet_ni *ni, lnet_nid_t target)
118 struct kib_net *net = ni->ni_data;
119 struct list_head *node;
121 struct kib_tx_poolset *tps;
123 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target, ni)];
124 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
127 tx = container_of(node, struct kib_tx, tx_list);
129 LASSERT (tx->tx_nwrq == 0);
130 LASSERT (!tx->tx_queued);
131 LASSERT (tx->tx_sending == 0);
132 LASSERT (!tx->tx_waiting);
133 LASSERT (tx->tx_status == 0);
134 LASSERT (tx->tx_conn == NULL);
135 LASSERT (tx->tx_lntmsg[0] == NULL);
136 LASSERT (tx->tx_lntmsg[1] == NULL);
137 LASSERT (tx->tx_nfrags == 0);
140 tx->tx_hstatus = LNET_MSG_STATUS_OK;
146 kiblnd_drop_rx(struct kib_rx *rx)
148 struct kib_conn *conn = rx->rx_conn;
149 struct kib_sched_info *sched = conn->ibc_sched;
152 spin_lock_irqsave(&sched->ibs_lock, flags);
153 LASSERT(conn->ibc_nrx > 0);
155 spin_unlock_irqrestore(&sched->ibs_lock, flags);
157 kiblnd_conn_decref(conn);
161 kiblnd_post_rx(struct kib_rx *rx, int credit)
163 struct kib_conn *conn = rx->rx_conn;
164 struct kib_net *net = conn->ibc_peer->ibp_ni->ni_data;
165 struct ib_recv_wr *bad_wrq = NULL;
166 #ifdef HAVE_IB_GET_DMA_MR
167 struct ib_mr *mr = conn->ibc_hdev->ibh_mrs;
171 LASSERT (net != NULL);
172 LASSERT (!in_interrupt());
173 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
174 credit == IBLND_POSTRX_PEER_CREDIT ||
175 credit == IBLND_POSTRX_RSRVD_CREDIT);
176 #ifdef HAVE_IB_GET_DMA_MR
179 rx->rx_sge.lkey = mr->lkey;
181 rx->rx_sge.lkey = conn->ibc_hdev->ibh_pd->local_dma_lkey;
183 rx->rx_sge.addr = rx->rx_msgaddr;
184 rx->rx_sge.length = IBLND_MSG_SIZE;
186 rx->rx_wrq.next = NULL;
187 rx->rx_wrq.sg_list = &rx->rx_sge;
188 rx->rx_wrq.num_sge = 1;
189 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
191 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
192 LASSERT (rx->rx_nob >= 0); /* not posted */
194 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
195 kiblnd_drop_rx(rx); /* No more posts for this rx */
199 rx->rx_nob = -1; /* flag posted */
201 /* NB: need an extra reference after ib_post_recv because we don't
202 * own this rx (and rx::rx_conn) anymore, LU-5678.
204 kiblnd_conn_addref(conn);
205 #ifdef HAVE_IB_POST_SEND_RECV_CONST
206 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq,
207 (const struct ib_recv_wr **)&bad_wrq);
209 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
211 if (unlikely(rc != 0)) {
212 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
213 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
217 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
220 if (unlikely(rc != 0)) {
221 kiblnd_close_conn(conn, rc);
222 kiblnd_drop_rx(rx); /* No more posts for this rx */
226 if (credit == IBLND_POSTRX_NO_CREDIT)
229 spin_lock(&conn->ibc_lock);
230 if (credit == IBLND_POSTRX_PEER_CREDIT)
231 conn->ibc_outstanding_credits++;
233 conn->ibc_reserved_credits++;
234 kiblnd_check_sends_locked(conn);
235 spin_unlock(&conn->ibc_lock);
238 kiblnd_conn_decref(conn);
242 static struct kib_tx *
243 kiblnd_find_waiting_tx_locked(struct kib_conn *conn, int txtype, u64 cookie)
245 struct list_head *tmp;
247 list_for_each(tmp, &conn->ibc_active_txs) {
248 struct kib_tx *tx = list_entry(tmp, struct kib_tx, tx_list);
250 LASSERT(!tx->tx_queued);
251 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
253 if (tx->tx_cookie != cookie)
256 if (tx->tx_waiting &&
257 tx->tx_msg->ibm_type == txtype)
260 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
261 tx->tx_waiting ? "" : "NOT ",
262 tx->tx_msg->ibm_type, txtype);
268 kiblnd_handle_completion(struct kib_conn *conn, int txtype, int status, u64 cookie)
271 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
274 spin_lock(&conn->ibc_lock);
276 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
278 spin_unlock(&conn->ibc_lock);
280 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
281 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
282 kiblnd_close_conn(conn, -EPROTO);
286 if (tx->tx_status == 0) { /* success so far */
287 if (status < 0) { /* failed? */
288 tx->tx_status = status;
289 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
290 } else if (txtype == IBLND_MSG_GET_REQ) {
291 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
297 idle = !tx->tx_queued && (tx->tx_sending == 0);
299 list_del(&tx->tx_list);
301 spin_unlock(&conn->ibc_lock);
308 kiblnd_send_completion(struct kib_conn *conn, int type, int status, u64 cookie)
310 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
311 struct kib_tx *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
314 CERROR("Can't get tx for completion %x for %s\n",
315 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
319 tx->tx_msg->ibm_u.completion.ibcm_status = status;
320 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
321 kiblnd_init_tx_msg(ni, tx, type, sizeof(struct kib_completion_msg));
323 kiblnd_queue_tx(tx, conn);
327 kiblnd_handle_rx(struct kib_rx *rx)
329 struct kib_msg *msg = rx->rx_msg;
330 struct kib_conn *conn = rx->rx_conn;
331 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
332 int credits = msg->ibm_credits;
338 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
340 CDEBUG (D_NET, "Received %x[%d] from %s\n",
341 msg->ibm_type, credits,
342 libcfs_nid2str(conn->ibc_peer->ibp_nid));
345 /* Have I received credits that will let me send? */
346 spin_lock(&conn->ibc_lock);
348 if (conn->ibc_credits + credits >
349 conn->ibc_queue_depth) {
350 rc2 = conn->ibc_credits;
351 spin_unlock(&conn->ibc_lock);
353 CERROR("Bad credits from %s: %d + %d > %d\n",
354 libcfs_nid2str(conn->ibc_peer->ibp_nid),
356 conn->ibc_queue_depth);
358 kiblnd_close_conn(conn, -EPROTO);
359 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
363 conn->ibc_credits += credits;
365 /* This ensures the credit taken by NOOP can be returned */
366 if (msg->ibm_type == IBLND_MSG_NOOP &&
367 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
368 conn->ibc_outstanding_credits++;
370 kiblnd_check_sends_locked(conn);
371 spin_unlock(&conn->ibc_lock);
374 switch (msg->ibm_type) {
376 CERROR("Bad IBLND message type %x from %s\n",
377 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
378 post_credit = IBLND_POSTRX_NO_CREDIT;
383 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
384 post_credit = IBLND_POSTRX_NO_CREDIT;
388 if (credits != 0) /* credit already posted */
389 post_credit = IBLND_POSTRX_NO_CREDIT;
390 else /* a keepalive NOOP */
391 post_credit = IBLND_POSTRX_PEER_CREDIT;
394 case IBLND_MSG_IMMEDIATE:
395 post_credit = IBLND_POSTRX_DONT_POST;
396 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
397 msg->ibm_srcnid, rx, 0);
398 if (rc < 0) /* repost on error */
399 post_credit = IBLND_POSTRX_PEER_CREDIT;
402 case IBLND_MSG_PUT_REQ:
403 post_credit = IBLND_POSTRX_DONT_POST;
404 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
405 msg->ibm_srcnid, rx, 1);
406 if (rc < 0) /* repost on error */
407 post_credit = IBLND_POSTRX_PEER_CREDIT;
410 case IBLND_MSG_PUT_NAK:
411 CWARN ("PUT_NACK from %s\n",
412 libcfs_nid2str(conn->ibc_peer->ibp_nid));
413 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
414 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
415 msg->ibm_u.completion.ibcm_status,
416 msg->ibm_u.completion.ibcm_cookie);
419 case IBLND_MSG_PUT_ACK:
420 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
422 spin_lock(&conn->ibc_lock);
423 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
424 msg->ibm_u.putack.ibpam_src_cookie);
426 list_del(&tx->tx_list);
427 spin_unlock(&conn->ibc_lock);
430 CERROR("Unmatched PUT_ACK from %s\n",
431 libcfs_nid2str(conn->ibc_peer->ibp_nid));
436 LASSERT (tx->tx_waiting);
437 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
438 * (a) I can overwrite tx_msg since my peer_ni has received it!
439 * (b) tx_waiting set tells tx_complete() it's not done. */
441 tx->tx_nwrq = tx->tx_nsge = 0; /* overwrite PUT_REQ */
443 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
444 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
445 &msg->ibm_u.putack.ibpam_rd,
446 msg->ibm_u.putack.ibpam_dst_cookie);
448 CERROR("Can't setup rdma for PUT to %s: %d\n",
449 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
451 spin_lock(&conn->ibc_lock);
452 tx->tx_waiting = 0; /* clear waiting and queue atomically */
453 kiblnd_queue_tx_locked(tx, conn);
454 spin_unlock(&conn->ibc_lock);
457 case IBLND_MSG_PUT_DONE:
458 post_credit = IBLND_POSTRX_PEER_CREDIT;
459 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
460 msg->ibm_u.completion.ibcm_status,
461 msg->ibm_u.completion.ibcm_cookie);
464 case IBLND_MSG_GET_REQ:
465 post_credit = IBLND_POSTRX_DONT_POST;
466 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
467 msg->ibm_srcnid, rx, 1);
468 if (rc < 0) /* repost on error */
469 post_credit = IBLND_POSTRX_PEER_CREDIT;
472 case IBLND_MSG_GET_DONE:
473 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
474 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
475 msg->ibm_u.completion.ibcm_status,
476 msg->ibm_u.completion.ibcm_cookie);
480 if (rc < 0) /* protocol error */
481 kiblnd_close_conn(conn, rc);
483 if (post_credit != IBLND_POSTRX_DONT_POST)
484 kiblnd_post_rx(rx, post_credit);
488 kiblnd_rx_complete(struct kib_rx *rx, int status, int nob)
490 struct kib_msg *msg = rx->rx_msg;
491 struct kib_conn *conn = rx->rx_conn;
492 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
493 struct kib_net *net = ni->ni_data;
497 LASSERT (net != NULL);
498 LASSERT (rx->rx_nob < 0); /* was posted */
499 rx->rx_nob = 0; /* isn't now */
501 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
504 if (status != IB_WC_SUCCESS) {
505 CNETERR("Rx from %s failed: %d\n",
506 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
513 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
515 CERROR ("Error %d unpacking rx from %s\n",
516 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
520 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
521 msg->ibm_dstnid != ni->ni_nid ||
522 msg->ibm_srcstamp != conn->ibc_incarnation ||
523 msg->ibm_dststamp != net->ibn_incarnation) {
524 CERROR ("Stale rx from %s\n",
525 libcfs_nid2str(conn->ibc_peer->ibp_nid));
530 /* set time last known alive */
531 kiblnd_peer_alive(conn->ibc_peer);
533 /* racing with connection establishment/teardown! */
535 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
536 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
539 write_lock_irqsave(g_lock, flags);
540 /* must check holding global lock to eliminate race */
541 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
542 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
543 write_unlock_irqrestore(g_lock, flags);
546 write_unlock_irqrestore(g_lock, flags);
548 kiblnd_handle_rx(rx);
552 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
553 kiblnd_close_conn(conn, err);
555 kiblnd_drop_rx(rx); /* Don't re-post rx. */
559 kiblnd_fmr_map_tx(struct kib_net *net, struct kib_tx *tx,
560 struct kib_rdma_desc *rd, u32 nob)
562 struct kib_hca_dev *hdev;
564 struct kib_fmr_poolset *fps;
569 LASSERT(tx->tx_pool != NULL);
570 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
573 hdev = tx->tx_pool->tpo_hdev;
574 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
577 * If we're dealing with FastReg, but the device doesn't
578 * support GAPS and the tx has GAPS, then there is no real point
579 * in trying to map the memory, because it'll just fail. So
580 * preemptively fail with an appropriate message
582 if ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED) &&
583 !(dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT) &&
585 CERROR("Using FastReg with no GAPS support, but tx has gaps. "
586 "Try setting use_fastreg_gaps to 1\n");
587 return -EPROTONOSUPPORT;
590 #ifdef HAVE_FMR_POOL_API
592 * FMR does not support gaps but the tx has gaps then
593 * we should make sure that the number of fragments we'll be sending
594 * over fits within the number of fragments negotiated on the
595 * connection, otherwise, we won't be able to RDMA the data.
596 * We need to maintain the number of fragments negotiation on the
597 * connection for backwards compatibility.
599 if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
601 tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
602 CERROR("TX number of frags (%d) is <= than connection"
603 " number of frags (%d). Consider setting peer's"
604 " map_on_demand to 256\n", tx->tx_nfrags,
605 tx->tx_conn->ibc_max_frags);
611 fps = net->ibn_fmr_ps[cpt];
612 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
614 CERROR("Can't map %u bytes (%u/%u)s: %d\n", nob,
615 tx->tx_nfrags, rd->rd_nfrags, rc);
620 * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
623 rd->rd_key = tx->tx_fmr.fmr_key;
625 * for FastReg or FMR with no gaps we can accumulate all
626 * the fragments in one FastReg or FMR fragment.
629 #ifdef HAVE_FMR_POOL_API
630 ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
633 (dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)) {
634 /* FMR requires zero based address */
635 #ifdef HAVE_FMR_POOL_API
636 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
637 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
639 rd->rd_frags[0].rf_nob = nob;
643 * We're transmitting with gaps using FMR.
644 * We'll need to use multiple fragments and identify the
645 * zero based address of each fragment.
647 for (i = 0; i < rd->rd_nfrags; i++) {
648 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
649 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
657 kiblnd_unmap_tx(struct kib_tx *tx)
660 #ifdef HAVE_FMR_POOL_API
661 tx->tx_fmr.fmr_pfmr ||
664 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
666 if (tx->tx_nfrags != 0) {
667 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
668 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
673 #ifdef HAVE_IB_GET_DMA_MR
674 static struct ib_mr *
675 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
677 struct kib_net *net = ni->ni_data;
678 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
679 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
681 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
684 * if map-on-demand is turned on and the device supports
685 * either FMR or FastReg then use that. Otherwise use global
686 * memory regions. If that's not available either, then you're
687 * dead in the water and fail the operation.
689 if (tunables->lnd_map_on_demand &&
690 (net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED
691 #ifdef HAVE_FMR_POOL_API
692 || net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED
698 * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
699 * in the call chain. The mapping will fail with appropriate error
702 return hdev->ibh_mrs;
706 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
707 struct kib_rdma_desc *rd, int nfrags)
709 struct kib_net *net = ni->ni_data;
710 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
711 #ifdef HAVE_IB_GET_DMA_MR
712 struct ib_mr *mr = NULL;
717 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
719 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
720 tx->tx_nfrags = nfrags;
722 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
723 tx->tx_nfrags, tx->tx_dmadir);
725 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
726 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
727 hdev->ibh_ibdev, &tx->tx_frags[i]);
728 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
729 hdev->ibh_ibdev, &tx->tx_frags[i]);
730 nob += rd->rd_frags[i].rf_nob;
733 #ifdef HAVE_IB_GET_DMA_MR
734 mr = kiblnd_find_rd_dma_mr(ni, rd);
736 /* found pre-mapping MR */
737 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
742 if (net->ibn_fmr_ps != NULL)
743 return kiblnd_fmr_map_tx(net, tx, rd, nob);
748 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
749 struct kib_rdma_desc *rd, int nkiov,
750 struct bio_vec *kiov, int offset, int nob)
752 struct kib_net *net = ni->ni_data;
753 struct scatterlist *sg;
757 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
761 LASSERT(net != NULL);
763 while (offset >= kiov->bv_len) {
764 offset -= kiov->bv_len;
776 fragnob = min((int)(kiov->bv_len - offset), nob);
779 * We're allowed to start at a non-aligned page offset in
780 * the first fragment and end at a non-aligned page offset
781 * in the last fragment.
783 if ((fragnob < (int)(kiov->bv_len - offset)) &&
784 nkiov < max_nkiov && nob > fragnob) {
785 CDEBUG(D_NET, "fragnob %d < available page %d: with"
786 " remaining %d kiovs with %d nob left\n",
787 fragnob, (int)(kiov->bv_len - offset),
792 sg_set_page(sg, kiov->bv_page, fragnob,
793 kiov->bv_offset + offset);
796 CERROR("lacking enough sg entries to map tx\n");
806 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
810 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
811 __must_hold(&conn->ibc_lock)
813 struct kib_msg *msg = tx->tx_msg;
814 struct kib_peer_ni *peer_ni = conn->ibc_peer;
815 struct lnet_ni *ni = peer_ni->ibp_ni;
816 int ver = conn->ibc_version;
820 LASSERT(tx->tx_queued);
821 /* We rely on this for QP sizing */
822 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
823 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
825 LASSERT(credit == 0 || credit == 1);
826 LASSERT(conn->ibc_outstanding_credits >= 0);
827 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
828 LASSERT(conn->ibc_credits >= 0);
829 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
831 if (conn->ibc_nsends_posted ==
832 kiblnd_concurrent_sends(ver, ni)) {
833 /* tx completions outstanding... */
834 CDEBUG(D_NET, "%s: posted enough\n",
835 libcfs_nid2str(peer_ni->ibp_nid));
839 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
840 CDEBUG(D_NET, "%s: no credits\n",
841 libcfs_nid2str(peer_ni->ibp_nid));
845 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
846 conn->ibc_credits == 1 && /* last credit reserved */
847 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
848 CDEBUG(D_NET, "%s: not using last credit\n",
849 libcfs_nid2str(peer_ni->ibp_nid));
853 /* NB don't drop ibc_lock before bumping tx_sending */
854 list_del(&tx->tx_list);
857 if (msg->ibm_type == IBLND_MSG_NOOP &&
858 (!kiblnd_need_noop(conn) || /* redundant NOOP */
859 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
860 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
861 /* OK to drop when posted enough NOOPs, since
862 * kiblnd_check_sends_locked will queue NOOP again when
863 * posted NOOPs complete */
864 spin_unlock(&conn->ibc_lock);
865 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
867 spin_lock(&conn->ibc_lock);
868 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
869 libcfs_nid2str(peer_ni->ibp_nid),
870 conn->ibc_noops_posted);
874 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
875 peer_ni->ibp_nid, conn->ibc_incarnation);
877 conn->ibc_credits -= credit;
878 conn->ibc_outstanding_credits = 0;
879 conn->ibc_nsends_posted++;
880 if (msg->ibm_type == IBLND_MSG_NOOP)
881 conn->ibc_noops_posted++;
883 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
884 * PUT. If so, it was first queued here as a PUT_REQ, sent and
885 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
886 * and then re-queued here. It's (just) possible that
887 * tx_sending is non-zero if we've not done the tx_complete()
888 * from the first send; hence the ++ rather than = below. */
890 list_add(&tx->tx_list, &conn->ibc_active_txs);
892 /* I'm still holding ibc_lock! */
893 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
895 } else if (tx->tx_pool->tpo_pool.po_failed ||
896 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
897 /* close_conn will launch failover */
900 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
901 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
902 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
905 if (!frd->frd_valid) {
906 wr = &frd->frd_inv_wr.wr;
907 wr->next = &frd->frd_fastreg_wr.wr;
909 wr = &frd->frd_fastreg_wr.wr;
911 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
914 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
915 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
916 bad->wr_id, bad->opcode, bad->send_flags,
917 libcfs_nid2str(conn->ibc_peer->ibp_nid));
920 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
923 #ifdef HAVE_IB_POST_SEND_RECV_CONST
924 rc = ib_post_send(conn->ibc_cmid->qp, wr,
925 (const struct ib_send_wr **)&bad);
927 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
931 conn->ibc_last_send = ktime_get();
936 /* NB credits are transferred in the actual
937 * message, which can only be the last work item */
938 conn->ibc_credits += credit;
939 conn->ibc_outstanding_credits += msg->ibm_credits;
940 conn->ibc_nsends_posted--;
941 if (msg->ibm_type == IBLND_MSG_NOOP)
942 conn->ibc_noops_posted--;
948 done = (tx->tx_sending == 0);
950 list_del(&tx->tx_list);
952 spin_unlock(&conn->ibc_lock);
954 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
955 CERROR("Error %d posting transmit to %s\n",
956 rc, libcfs_nid2str(peer_ni->ibp_nid));
958 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
959 rc, libcfs_nid2str(peer_ni->ibp_nid));
961 kiblnd_close_conn(conn, rc);
966 spin_lock(&conn->ibc_lock);
972 kiblnd_check_sends_locked(struct kib_conn *conn)
974 int ver = conn->ibc_version;
975 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
978 /* Don't send anything until after the connection is established */
979 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
980 CDEBUG(D_NET, "%s too soon\n",
981 libcfs_nid2str(conn->ibc_peer->ibp_nid));
985 LASSERT(conn->ibc_nsends_posted <=
986 kiblnd_concurrent_sends(ver, ni));
987 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
988 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
989 LASSERT (conn->ibc_reserved_credits >= 0);
991 while (conn->ibc_reserved_credits > 0 &&
992 (tx = list_first_entry_or_null(&conn->ibc_tx_queue_rsrvd,
993 struct kib_tx, tx_list)) != NULL) {
994 list_move_tail(&tx->tx_list, &conn->ibc_tx_queue);
995 conn->ibc_reserved_credits--;
998 if (kiblnd_need_noop(conn)) {
999 spin_unlock(&conn->ibc_lock);
1001 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1003 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1005 spin_lock(&conn->ibc_lock);
1007 kiblnd_queue_tx_locked(tx, conn);
1013 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1015 tx = list_first_entry(&conn->ibc_tx_queue_nocred,
1016 struct kib_tx, tx_list);
1017 } else if (!list_empty(&conn->ibc_tx_noops)) {
1018 LASSERT (!IBLND_OOB_CAPABLE(ver));
1020 tx = list_first_entry(&conn->ibc_tx_noops,
1021 struct kib_tx, tx_list);
1022 } else if (!list_empty(&conn->ibc_tx_queue)) {
1024 tx = list_first_entry(&conn->ibc_tx_queue,
1025 struct kib_tx, tx_list);
1029 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1035 kiblnd_tx_complete(struct kib_tx *tx, int status)
1037 int failed = (status != IB_WC_SUCCESS);
1038 struct kib_conn *conn = tx->tx_conn;
1041 if (tx->tx_sending <= 0) {
1042 CERROR("Received an event on a freed tx: %p status %d\n",
1048 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1049 CNETERR("Tx -> %s cookie %#llx"
1050 " sending %d waiting %d: failed %d\n",
1051 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1052 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1055 kiblnd_close_conn(conn, -EIO);
1057 kiblnd_peer_alive(conn->ibc_peer);
1060 spin_lock(&conn->ibc_lock);
1062 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1063 * gets to free it, which also drops its ref on 'conn'. */
1066 conn->ibc_nsends_posted--;
1067 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1068 conn->ibc_noops_posted--;
1071 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1072 tx->tx_waiting = 0; /* don't wait for peer_ni */
1073 tx->tx_status = -EIO;
1076 idle = (tx->tx_sending == 0) && /* This is the final callback */
1077 !tx->tx_waiting && /* Not waiting for peer_ni */
1078 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1080 list_del(&tx->tx_list);
1082 kiblnd_check_sends_locked(conn);
1083 spin_unlock(&conn->ibc_lock);
1090 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1093 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1094 struct ib_sge *sge = &tx->tx_msgsge;
1095 struct ib_rdma_wr *wrq;
1096 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1097 #ifdef HAVE_IB_GET_DMA_MR
1098 struct ib_mr *mr = hdev->ibh_mrs;
1101 LASSERT(tx->tx_nwrq >= 0);
1102 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1103 LASSERT(nob <= IBLND_MSG_SIZE);
1104 #ifdef HAVE_IB_GET_DMA_MR
1105 LASSERT(mr != NULL);
1108 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1110 #ifdef HAVE_IB_GET_DMA_MR
1111 sge->lkey = mr->lkey;
1113 sge->lkey = hdev->ibh_pd->local_dma_lkey;
1115 sge->addr = tx->tx_msgaddr;
1118 wrq = &tx->tx_wrq[tx->tx_nwrq];
1119 memset(wrq, 0, sizeof(*wrq));
1121 wrq->wr.next = NULL;
1122 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1123 wrq->wr.sg_list = sge;
1124 wrq->wr.num_sge = 1;
1125 wrq->wr.opcode = IB_WR_SEND;
1126 wrq->wr.send_flags = IB_SEND_SIGNALED;
1132 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1133 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1135 struct kib_msg *ibmsg = tx->tx_msg;
1136 struct kib_rdma_desc *srcrd = tx->tx_rd;
1137 struct ib_rdma_wr *wrq = NULL;
1145 LASSERT(!in_interrupt());
1146 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1147 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1149 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1150 resid > 0; resid -= sge_nob) {
1153 if (srcidx >= srcrd->rd_nfrags) {
1154 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1159 if (dstidx >= dstrd->rd_nfrags) {
1160 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1165 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1166 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1167 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1168 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1169 conn->ibc_max_frags,
1170 srcidx, srcrd->rd_nfrags,
1171 dstidx, dstrd->rd_nfrags);
1176 sge_nob = min3(kiblnd_rd_frag_size(srcrd, srcidx),
1177 kiblnd_rd_frag_size(dstrd, dstidx),
1180 sge = &tx->tx_sge[tx->tx_nsge];
1181 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1182 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1183 sge->length = sge_nob;
1186 wrq = &tx->tx_wrq[tx->tx_nwrq];
1188 wrq->wr.next = &(wrq + 1)->wr;
1189 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1190 wrq->wr.sg_list = sge;
1191 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1192 wrq->wr.send_flags = 0;
1194 #ifdef HAVE_IB_RDMA_WR
1195 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1197 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1200 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1202 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1207 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1208 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1211 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1213 wrq->wr.num_sge = wrq_sge;
1219 if (rc < 0) /* no RDMA if completing with failure */
1220 tx->tx_nwrq = tx->tx_nsge = 0;
1222 ibmsg->ibm_u.completion.ibcm_status = rc;
1223 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1224 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1225 type, sizeof(struct kib_completion_msg));
1231 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1233 struct list_head *q;
1236 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1237 LASSERT(!tx->tx_queued); /* not queued for sending already */
1238 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1240 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1241 tx->tx_status = -ECONNABORTED;
1243 if (tx->tx_conn != NULL) {
1244 /* PUT_DONE first attached to conn as a PUT_REQ */
1245 LASSERT(tx->tx_conn == conn);
1246 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1248 kiblnd_conn_decref(conn);
1250 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1255 timeout_ns = kiblnd_timeout() * NSEC_PER_SEC;
1257 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1259 if (tx->tx_conn == NULL) {
1260 kiblnd_conn_addref(conn);
1262 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1264 /* PUT_DONE first attached to conn as a PUT_REQ */
1265 LASSERT (tx->tx_conn == conn);
1266 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1269 switch (tx->tx_msg->ibm_type) {
1273 case IBLND_MSG_PUT_REQ:
1274 case IBLND_MSG_GET_REQ:
1275 q = &conn->ibc_tx_queue_rsrvd;
1278 case IBLND_MSG_PUT_NAK:
1279 case IBLND_MSG_PUT_ACK:
1280 case IBLND_MSG_PUT_DONE:
1281 case IBLND_MSG_GET_DONE:
1282 q = &conn->ibc_tx_queue_nocred;
1285 case IBLND_MSG_NOOP:
1286 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1287 q = &conn->ibc_tx_queue_nocred;
1289 q = &conn->ibc_tx_noops;
1292 case IBLND_MSG_IMMEDIATE:
1293 q = &conn->ibc_tx_queue;
1297 list_add_tail(&tx->tx_list, q);
1301 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1303 spin_lock(&conn->ibc_lock);
1304 kiblnd_queue_tx_locked(tx, conn);
1305 kiblnd_check_sends_locked(conn);
1306 spin_unlock(&conn->ibc_lock);
1310 kiblnd_resolve_addr_cap(struct rdma_cm_id *cmid,
1311 struct sockaddr_in *srcaddr,
1312 struct sockaddr_in *dstaddr,
1315 unsigned short port;
1318 /* allow the port to be reused */
1319 rc = rdma_set_reuseaddr(cmid, 1);
1321 CERROR("Unable to set reuse on cmid: %d\n", rc);
1325 /* look for a free privileged port */
1326 for (port = PROT_SOCK-1; port > 0; port--) {
1327 srcaddr->sin_port = htons(port);
1328 rc = rdma_resolve_addr(cmid,
1329 (struct sockaddr *)srcaddr,
1330 (struct sockaddr *)dstaddr,
1333 CDEBUG(D_NET, "bound to port %hu\n", port);
1335 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1336 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1343 CERROR("cannot bind to a free privileged port: rc = %d\n", rc);
1349 kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1350 struct sockaddr_in *srcaddr,
1351 struct sockaddr_in *dstaddr,
1354 const struct cred *old_creds = NULL;
1355 struct cred *new_creds;
1358 if (!capable(CAP_NET_BIND_SERVICE)) {
1359 new_creds = prepare_kernel_cred(NULL);
1363 cap_raise(new_creds->cap_effective, CAP_NET_BIND_SERVICE);
1364 old_creds = override_creds(new_creds);
1367 rc = kiblnd_resolve_addr_cap(cmid, srcaddr, dstaddr, timeout_ms);
1370 revert_creds(old_creds);
1376 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1378 struct rdma_cm_id *cmid;
1379 struct kib_dev *dev;
1380 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1381 struct sockaddr_in srcaddr;
1382 struct sockaddr_in dstaddr;
1385 LASSERT (net != NULL);
1386 LASSERT (peer_ni->ibp_connecting > 0);
1388 cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1389 kiblnd_cm_callback, peer_ni,
1390 RDMA_PS_TCP, IB_QPT_RC);
1393 CERROR("Can't create CMID for %s: %ld\n",
1394 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1400 memset(&srcaddr, 0, sizeof(srcaddr));
1401 srcaddr.sin_family = AF_INET;
1402 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1404 memset(&dstaddr, 0, sizeof(dstaddr));
1405 dstaddr.sin_family = AF_INET;
1406 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1407 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1409 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1411 if (*kiblnd_tunables.kib_use_priv_port) {
1412 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1413 kiblnd_timeout() * 1000);
1415 rc = rdma_resolve_addr(cmid,
1416 (struct sockaddr *)&srcaddr,
1417 (struct sockaddr *)&dstaddr,
1418 kiblnd_timeout() * 1000);
1421 /* Can't initiate address resolution: */
1422 CERROR("Can't resolve addr for %s: %d\n",
1423 libcfs_nid2str(peer_ni->ibp_nid), rc);
1430 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1431 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1432 rdma_destroy_id(cmid);
1435 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1439 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1441 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1442 char *reason = NULL;
1444 unsigned long flags;
1446 write_lock_irqsave(glock, flags);
1447 if (peer_ni->ibp_reconnecting == 0) {
1448 if (peer_ni->ibp_accepting)
1449 reason = "accepting";
1450 else if (peer_ni->ibp_connecting)
1451 reason = "connecting";
1452 else if (!list_empty(&peer_ni->ibp_conns))
1453 reason = "connected";
1454 else /* connected then closed */
1460 if (peer_ni->ibp_accepting)
1461 CNETERR("Detecting race between accepting and reconnecting\n");
1462 peer_ni->ibp_reconnecting--;
1464 if (!kiblnd_peer_active(peer_ni)) {
1465 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1466 reason = "unlinked";
1470 peer_ni->ibp_connecting++;
1471 peer_ni->ibp_reconnected++;
1473 write_unlock_irqrestore(glock, flags);
1475 kiblnd_connect_peer(peer_ni);
1479 write_unlock_irqrestore(glock, flags);
1481 CWARN("Abort reconnection of %s: %s\n",
1482 libcfs_nid2str(peer_ni->ibp_nid), reason);
1483 kiblnd_txlist_done(&txs, -ECONNABORTED,
1484 LNET_MSG_STATUS_LOCAL_ABORTED);
1489 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1491 struct kib_peer_ni *peer_ni;
1492 struct kib_peer_ni *peer2;
1493 struct kib_conn *conn;
1494 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1495 unsigned long flags;
1498 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1500 /* If I get here, I've committed to send, so I complete the tx with
1501 * failure on any problems
1504 LASSERT(!tx || !tx->tx_conn); /* only set when assigned a conn */
1505 LASSERT(!tx || tx->tx_nwrq > 0); /* work items have been set up */
1507 /* First time, just use a read lock since I expect to find my peer_ni
1510 read_lock_irqsave(g_lock, flags);
1512 peer_ni = kiblnd_find_peer_locked(ni, nid);
1513 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1514 /* Found a peer_ni with an established connection */
1515 conn = kiblnd_get_conn_locked(peer_ni);
1516 kiblnd_conn_addref(conn); /* 1 ref for me... */
1518 read_unlock_irqrestore(g_lock, flags);
1521 kiblnd_queue_tx(tx, conn);
1522 kiblnd_conn_decref(conn); /* ...to here */
1526 read_unlock(g_lock);
1527 /* Re-try with a write lock */
1530 peer_ni = kiblnd_find_peer_locked(ni, nid);
1531 if (peer_ni != NULL) {
1532 if (list_empty(&peer_ni->ibp_conns)) {
1533 /* found a peer_ni, but it's still connecting... */
1534 LASSERT(kiblnd_peer_connecting(peer_ni));
1536 list_add_tail(&tx->tx_list,
1537 &peer_ni->ibp_tx_queue);
1538 write_unlock_irqrestore(g_lock, flags);
1540 conn = kiblnd_get_conn_locked(peer_ni);
1541 kiblnd_conn_addref(conn); /* 1 ref for me... */
1543 write_unlock_irqrestore(g_lock, flags);
1546 kiblnd_queue_tx(tx, conn);
1547 kiblnd_conn_decref(conn); /* ...to here */
1552 write_unlock_irqrestore(g_lock, flags);
1554 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1555 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1557 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1559 tx->tx_status = -EHOSTUNREACH;
1561 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1567 write_lock_irqsave(g_lock, flags);
1569 peer2 = kiblnd_find_peer_locked(ni, nid);
1570 if (peer2 != NULL) {
1571 if (list_empty(&peer2->ibp_conns)) {
1572 /* found a peer_ni, but it's still connecting... */
1573 LASSERT(kiblnd_peer_connecting(peer2));
1575 list_add_tail(&tx->tx_list,
1576 &peer2->ibp_tx_queue);
1577 write_unlock_irqrestore(g_lock, flags);
1579 conn = kiblnd_get_conn_locked(peer2);
1580 kiblnd_conn_addref(conn); /* 1 ref for me... */
1582 write_unlock_irqrestore(g_lock, flags);
1585 kiblnd_queue_tx(tx, conn);
1586 kiblnd_conn_decref(conn); /* ...to here */
1589 kiblnd_peer_decref(peer_ni);
1593 /* Brand new peer_ni */
1594 LASSERT(peer_ni->ibp_connecting == 0);
1595 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1596 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1598 /* always called with a ref on ni, which prevents ni being shutdown */
1599 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1602 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1604 kiblnd_peer_addref(peer_ni);
1605 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
1607 write_unlock_irqrestore(g_lock, flags);
1609 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1610 kiblnd_connect_peer(peer_ni);
1611 kiblnd_peer_decref(peer_ni);
1615 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1617 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1618 int type = lntmsg->msg_type;
1619 struct lnet_process_id target = lntmsg->msg_target;
1620 int target_is_router = lntmsg->msg_target_is_router;
1621 int routing = lntmsg->msg_routing;
1622 unsigned int payload_niov = lntmsg->msg_niov;
1623 struct bio_vec *payload_kiov = lntmsg->msg_kiov;
1624 unsigned int payload_offset = lntmsg->msg_offset;
1625 unsigned int payload_nob = lntmsg->msg_len;
1626 struct kib_msg *ibmsg;
1627 struct kib_rdma_desc *rd;
1632 /* NB 'private' is different depending on what we're sending.... */
1634 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1635 payload_nob, payload_niov, libcfs_id2str(target));
1637 LASSERT (payload_nob == 0 || payload_niov > 0);
1638 LASSERT (payload_niov <= LNET_MAX_IOV);
1640 /* Thread context */
1641 LASSERT (!in_interrupt());
1649 LASSERT (payload_nob == 0);
1653 if (routing || target_is_router)
1654 break; /* send IMMEDIATE */
1656 /* is the REPLY message too small for RDMA? */
1657 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1658 if (nob <= IBLND_MSG_SIZE)
1659 break; /* send IMMEDIATE */
1661 tx = kiblnd_get_idle_tx(ni, target.nid);
1663 CERROR("Can't allocate txd for GET to %s\n",
1664 libcfs_nid2str(target.nid));
1669 rd = &ibmsg->ibm_u.get.ibgm_rd;
1670 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1671 lntmsg->msg_md->md_niov,
1672 lntmsg->msg_md->md_kiov,
1673 0, lntmsg->msg_md->md_length);
1675 CERROR("Can't setup GET sink for %s: %d\n",
1676 libcfs_nid2str(target.nid), rc);
1677 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1682 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1683 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1684 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1686 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1688 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1689 if (tx->tx_lntmsg[1] == NULL) {
1690 CERROR("Can't create reply for GET -> %s\n",
1691 libcfs_nid2str(target.nid));
1696 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1697 tx->tx_waiting = 1; /* waiting for GET_DONE */
1698 kiblnd_launch_tx(ni, tx, target.nid);
1701 case LNET_MSG_REPLY:
1703 /* Is the payload small enough not to need RDMA? */
1704 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob]);
1705 if (nob <= IBLND_MSG_SIZE)
1706 break; /* send IMMEDIATE */
1708 tx = kiblnd_get_idle_tx(ni, target.nid);
1710 CERROR("Can't allocate %s txd for %s\n",
1711 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1712 libcfs_nid2str(target.nid));
1716 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1717 payload_niov, payload_kiov,
1718 payload_offset, payload_nob);
1720 CERROR("Can't setup PUT src for %s: %d\n",
1721 libcfs_nid2str(target.nid), rc);
1727 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1728 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1729 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1730 sizeof(struct kib_putreq_msg));
1732 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1733 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1734 kiblnd_launch_tx(ni, tx, target.nid);
1738 /* send IMMEDIATE */
1739 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1742 tx = kiblnd_get_idle_tx(ni, target.nid);
1744 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1745 type, libcfs_nid2str(target.nid));
1750 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1752 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1753 offsetof(struct kib_msg,
1754 ibm_u.immediate.ibim_payload),
1755 payload_niov, payload_kiov,
1756 payload_offset, payload_nob);
1758 nob = offsetof(struct kib_immediate_msg, ibim_payload[payload_nob]);
1759 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1761 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1762 kiblnd_launch_tx(ni, tx, target.nid);
1767 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1769 struct lnet_process_id target = lntmsg->msg_target;
1770 unsigned int niov = lntmsg->msg_niov;
1771 struct bio_vec *kiov = lntmsg->msg_kiov;
1772 unsigned int offset = lntmsg->msg_offset;
1773 unsigned int nob = lntmsg->msg_len;
1777 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1779 CERROR("Can't get tx for REPLY to %s\n",
1780 libcfs_nid2str(target.nid));
1787 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1788 niov, kiov, offset, nob);
1791 CERROR("Can't setup GET src for %s: %d\n",
1792 libcfs_nid2str(target.nid), rc);
1796 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1797 IBLND_MSG_GET_DONE, nob,
1798 &rx->rx_msg->ibm_u.get.ibgm_rd,
1799 rx->rx_msg->ibm_u.get.ibgm_cookie);
1801 CERROR("Can't setup rdma for GET from %s: %d\n",
1802 libcfs_nid2str(target.nid), rc);
1807 /* No RDMA: local completion may happen now! */
1808 lnet_finalize(lntmsg, 0);
1810 /* RDMA: lnet_finalize(lntmsg) when it
1812 tx->tx_lntmsg[0] = lntmsg;
1815 kiblnd_queue_tx(tx, rx->rx_conn);
1820 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1823 lnet_finalize(lntmsg, -EIO);
1827 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1828 int delayed, unsigned int niov, struct bio_vec *kiov,
1829 unsigned int offset, unsigned int mlen, unsigned int rlen)
1831 struct kib_rx *rx = private;
1832 struct kib_msg *rxmsg = rx->rx_msg;
1833 struct kib_conn *conn = rx->rx_conn;
1837 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1840 LASSERT (mlen <= rlen);
1841 LASSERT (!in_interrupt());
1843 switch (rxmsg->ibm_type) {
1847 case IBLND_MSG_IMMEDIATE:
1848 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1849 if (nob > rx->rx_nob) {
1850 CERROR ("Immediate message from %s too big: %d(%d)\n",
1851 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1857 lnet_copy_flat2kiov(niov, kiov, offset,
1858 IBLND_MSG_SIZE, rxmsg,
1859 offsetof(struct kib_msg,
1860 ibm_u.immediate.ibim_payload),
1862 lnet_finalize(lntmsg, 0);
1865 case IBLND_MSG_PUT_REQ: {
1866 struct kib_msg *txmsg;
1867 struct kib_rdma_desc *rd;
1868 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1871 lnet_finalize(lntmsg, 0);
1872 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1877 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1879 CERROR("Can't allocate tx for %s\n",
1880 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1881 /* Not replying will break the connection */
1887 rd = &txmsg->ibm_u.putack.ibpam_rd;
1888 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1889 niov, kiov, offset, mlen);
1891 CERROR("Can't setup PUT sink for %s: %d\n",
1892 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1893 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1895 /* tell peer_ni it's over */
1896 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1901 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1902 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1903 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1905 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1907 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1908 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1909 kiblnd_queue_tx(tx, conn);
1911 /* reposted buffer reserved for PUT_DONE */
1912 post_credit = IBLND_POSTRX_NO_CREDIT;
1916 case IBLND_MSG_GET_REQ:
1917 if (lntmsg != NULL) {
1918 /* Optimized GET; RDMA lntmsg's payload */
1919 kiblnd_reply(ni, rx, lntmsg);
1921 /* GET didn't match anything */
1922 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1924 rxmsg->ibm_u.get.ibgm_cookie);
1929 kiblnd_post_rx(rx, post_credit);
1934 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1936 struct task_struct *task = kthread_run(fn, arg, "%s", name);
1939 return PTR_ERR(task);
1941 atomic_inc(&kiblnd_data.kib_nthreads);
1946 kiblnd_thread_fini (void)
1948 atomic_dec (&kiblnd_data.kib_nthreads);
1952 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
1954 /* This is racy, but everyone's only writing ktime_get_seconds() */
1955 peer_ni->ibp_last_alive = ktime_get_seconds();
1960 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
1963 time64_t last_alive = 0;
1964 unsigned long flags;
1966 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1968 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
1969 error = peer_ni->ibp_error;
1970 peer_ni->ibp_error = 0;
1972 last_alive = peer_ni->ibp_last_alive;
1975 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1978 lnet_notify(peer_ni->ibp_ni,
1979 peer_ni->ibp_nid, false, false, last_alive);
1983 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
1985 /* This just does the immediate housekeeping. 'error' is zero for a
1986 * normal shutdown which can happen only after the connection has been
1987 * established. If the connection is established, schedule the
1988 * connection to be finished off by the connd. Otherwise the connd is
1989 * already dealing with it (either to set it up or tear it down).
1990 * Caller holds kib_global_lock exclusively in irq context */
1991 struct kib_peer_ni *peer_ni = conn->ibc_peer;
1992 struct kib_dev *dev;
1993 unsigned long flags;
1995 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1997 if (error != 0 && conn->ibc_comms_error == 0)
1998 conn->ibc_comms_error = error;
2000 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2001 return; /* already being handled */
2004 list_empty(&conn->ibc_tx_noops) &&
2005 list_empty(&conn->ibc_tx_queue) &&
2006 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2007 list_empty(&conn->ibc_tx_queue_nocred) &&
2008 list_empty(&conn->ibc_active_txs)) {
2009 CDEBUG(D_NET, "closing conn to %s\n",
2010 libcfs_nid2str(peer_ni->ibp_nid));
2012 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2013 libcfs_nid2str(peer_ni->ibp_nid), error,
2014 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2015 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2016 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2017 "" : "(sending_rsrvd)",
2018 list_empty(&conn->ibc_tx_queue_nocred) ?
2019 "" : "(sending_nocred)",
2020 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2023 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2024 if (peer_ni->ibp_next_conn == conn)
2025 /* clear next_conn so it won't be used */
2026 peer_ni->ibp_next_conn = NULL;
2027 list_del(&conn->ibc_list);
2028 /* connd (see below) takes over ibc_list's ref */
2030 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2031 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2032 kiblnd_unlink_peer_locked(peer_ni);
2034 /* set/clear error on last conn */
2035 peer_ni->ibp_error = conn->ibc_comms_error;
2038 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2041 kiblnd_dev_can_failover(dev)) {
2042 list_add_tail(&dev->ibd_fail_list,
2043 &kiblnd_data.kib_failed_devs);
2044 wake_up(&kiblnd_data.kib_failover_waitq);
2047 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2049 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2050 wake_up(&kiblnd_data.kib_connd_waitq);
2052 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2056 kiblnd_close_conn(struct kib_conn *conn, int error)
2058 unsigned long flags;
2060 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2062 kiblnd_close_conn_locked(conn, error);
2064 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2068 kiblnd_handle_early_rxs(struct kib_conn *conn)
2070 unsigned long flags;
2073 LASSERT(!in_interrupt());
2074 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2076 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2077 while ((rx = list_first_entry_or_null(&conn->ibc_early_rxs,
2079 rx_list)) != NULL) {
2080 list_del(&rx->rx_list);
2081 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2083 kiblnd_handle_rx(rx);
2085 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2087 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2091 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2097 spin_lock(&conn->ibc_lock);
2099 list_for_each_entry_safe(tx, nxt, txs, tx_list) {
2100 if (txs == &conn->ibc_active_txs) {
2101 LASSERT(!tx->tx_queued);
2102 LASSERT(tx->tx_waiting ||
2103 tx->tx_sending != 0);
2104 if (conn->ibc_comms_error == -ETIMEDOUT) {
2105 if (tx->tx_waiting && !tx->tx_sending)
2107 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2108 else if (tx->tx_sending)
2110 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2113 LASSERT(tx->tx_queued);
2114 if (conn->ibc_comms_error == -ETIMEDOUT)
2115 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2117 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2120 tx->tx_status = -ECONNABORTED;
2124 * TODO: This makes an assumption that
2125 * kiblnd_tx_complete() will be called for each tx. If
2126 * that event is dropped we could end up with stale
2127 * connections floating around. We'd like to deal with
2128 * that in a better way.
2130 * Also that means we can exceed the timeout by many
2133 if (tx->tx_sending == 0) {
2135 list_move(&tx->tx_list, &zombies);
2137 /* keep tx until cq destroy */
2138 list_move(&tx->tx_list, &conn->ibc_zombie_txs);
2143 spin_unlock(&conn->ibc_lock);
2146 * aborting transmits occurs when finalizing the connection.
2147 * The connection is finalized on error.
2148 * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2149 * override the value already set in tx->tx_hstatus above.
2151 kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2155 kiblnd_tx_may_discard(struct kib_conn *conn)
2161 spin_lock(&conn->ibc_lock);
2163 list_for_each_entry_safe(tx, nxt, &conn->ibc_zombie_txs, tx_list) {
2164 if (tx->tx_sending > 0 && tx->tx_lntmsg[0] &&
2165 lnet_md_discarded(tx->tx_lntmsg[0]->msg_md)) {
2167 if (tx->tx_sending == 0) {
2168 kiblnd_conn_decref(tx->tx_conn);
2175 spin_unlock(&conn->ibc_lock);
2180 kiblnd_finalise_conn(struct kib_conn *conn)
2182 LASSERT (!in_interrupt());
2183 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2185 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2186 * for connections that didn't get as far as being connected, because
2187 * rdma_disconnect() does this for free. */
2188 kiblnd_abort_receives(conn);
2190 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2192 /* Complete all tx descs not waiting for sends to complete.
2193 * NB we should be safe from RDMA now that the QP has changed state */
2195 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2196 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2197 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2198 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2199 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2201 kiblnd_handle_early_rxs(conn);
2205 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2209 unsigned long flags;
2210 enum lnet_msg_hstatus hstatus;
2212 LASSERT(error != 0);
2213 LASSERT(!in_interrupt());
2215 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2218 LASSERT(peer_ni->ibp_connecting > 0);
2219 peer_ni->ibp_connecting--;
2221 LASSERT (peer_ni->ibp_accepting > 0);
2222 peer_ni->ibp_accepting--;
2225 if (kiblnd_peer_connecting(peer_ni)) {
2226 /* another connection attempt under way... */
2227 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2232 peer_ni->ibp_reconnected = 0;
2233 if (list_empty(&peer_ni->ibp_conns)) {
2234 /* Take peer_ni's blocked transmits to complete with error */
2235 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2237 if (kiblnd_peer_active(peer_ni))
2238 kiblnd_unlink_peer_locked(peer_ni);
2240 peer_ni->ibp_error = error;
2242 /* Can't have blocked transmits if there are connections */
2243 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2246 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2248 kiblnd_peer_notify(peer_ni);
2250 if (list_empty(&zombies))
2253 CNETERR("Deleting messages for %s: connection failed\n",
2254 libcfs_nid2str(peer_ni->ibp_nid));
2259 hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
2262 hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
2265 hstatus = LNET_MSG_STATUS_LOCAL_DROPPED;
2269 kiblnd_txlist_done(&zombies, error, hstatus);
2273 kiblnd_connreq_done(struct kib_conn *conn, int status)
2275 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2278 unsigned long flags;
2281 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2283 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2284 libcfs_nid2str(peer_ni->ibp_nid), active,
2285 conn->ibc_version, status);
2287 LASSERT (!in_interrupt());
2288 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2289 peer_ni->ibp_connecting > 0) ||
2290 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2291 peer_ni->ibp_accepting > 0));
2293 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2294 conn->ibc_connvars = NULL;
2297 /* failed to establish connection */
2298 kiblnd_peer_connect_failed(peer_ni, active, status);
2299 kiblnd_finalise_conn(conn);
2303 /* connection established */
2304 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2306 conn->ibc_last_send = ktime_get();
2307 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2308 kiblnd_peer_alive(peer_ni);
2310 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2311 * peer_ni instance... */
2312 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2313 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2314 peer_ni->ibp_reconnected = 0;
2316 peer_ni->ibp_connecting--;
2318 peer_ni->ibp_accepting--;
2320 if (peer_ni->ibp_version == 0) {
2321 peer_ni->ibp_version = conn->ibc_version;
2322 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2325 if (peer_ni->ibp_version != conn->ibc_version ||
2326 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2327 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2328 conn->ibc_incarnation);
2329 peer_ni->ibp_version = conn->ibc_version;
2330 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2333 /* grab pending txs while I have the lock */
2334 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2336 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2337 conn->ibc_comms_error != 0) { /* error has happened already */
2339 /* start to shut down connection */
2340 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2341 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2343 kiblnd_txlist_done(&txs, -ECONNABORTED,
2344 LNET_MSG_STATUS_LOCAL_ERROR);
2349 /* +1 ref for myself, this connection is visible to other threads
2350 * now, refcount of peer:ibp_conns can be released by connection
2351 * close from either a different thread, or the calling of
2352 * kiblnd_check_sends_locked() below. See bz21911 for details.
2354 kiblnd_conn_addref(conn);
2355 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2357 /* Schedule blocked txs
2358 * Note: if we are running with conns_per_peer > 1, these blocked
2359 * txs will all get scheduled to the first connection which gets
2360 * scheduled. We won't be using round robin on this first batch.
2362 spin_lock(&conn->ibc_lock);
2363 while ((tx = list_first_entry_or_null(&txs, struct kib_tx,
2364 tx_list)) != NULL) {
2365 list_del(&tx->tx_list);
2367 kiblnd_queue_tx_locked(tx, conn);
2369 kiblnd_check_sends_locked(conn);
2370 spin_unlock(&conn->ibc_lock);
2372 /* schedule blocked rxs */
2373 kiblnd_handle_early_rxs(conn);
2374 kiblnd_conn_decref(conn);
2378 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2382 #ifdef HAVE_RDMA_REJECT_4ARGS
2383 rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2385 rc = rdma_reject(cmid, rej, sizeof(*rej));
2389 CWARN("Error %d sending reject\n", rc);
2393 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2395 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2396 struct kib_msg *reqmsg = priv;
2397 struct kib_msg *ackmsg;
2398 struct kib_dev *ibdev;
2399 struct kib_peer_ni *peer_ni;
2400 struct kib_peer_ni *peer2;
2401 struct kib_conn *conn;
2402 struct lnet_ni *ni = NULL;
2403 struct kib_net *net = NULL;
2405 struct rdma_conn_param cp;
2407 int version = IBLND_MSG_VERSION;
2408 unsigned long flags;
2410 struct sockaddr_in *peer_addr;
2412 LASSERT(!in_interrupt());
2413 /* cmid inherits 'context' from the corresponding listener id */
2414 ibdev = cmid->context;
2417 memset(&rej, 0, sizeof(rej));
2418 rej.ibr_magic = IBLND_MSG_MAGIC;
2419 rej.ibr_why = IBLND_REJECT_FATAL;
2420 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2422 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2423 if (*kiblnd_tunables.kib_require_priv_port &&
2424 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2425 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2426 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2427 &ip, ntohs(peer_addr->sin_port));
2431 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2432 CERROR("Short connection request\n");
2436 /* Future protocol version compatibility support! If the
2437 * o2iblnd-specific protocol changes, or when LNET unifies
2438 * protocols over all LNDs, the initial connection will
2439 * negotiate a protocol version. I trap this here to avoid
2440 * console errors; the reject tells the peer_ni which protocol I
2442 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2443 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2445 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2446 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2447 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2449 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2450 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2451 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2454 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2456 CERROR("Can't parse connection request: %d\n", rc);
2460 nid = reqmsg->ibm_srcnid;
2461 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2464 net = (struct kib_net *)ni->ni_data;
2465 rej.ibr_incarnation = net->ibn_incarnation;
2468 if (ni == NULL || /* no matching net */
2469 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2470 net->ibn_dev != ibdev) { /* wrong device */
2471 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): bad dst nid %s\n", libcfs_nid2str(nid),
2472 ni ? libcfs_nid2str(ni->ni_nid) : "NA",
2473 ibdev->ibd_ifname, ibdev->ibd_nnets,
2475 libcfs_nid2str(reqmsg->ibm_dstnid));
2480 /* check time stamp as soon as possible */
2481 if (reqmsg->ibm_dststamp != 0 &&
2482 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2483 CWARN("Stale connection request\n");
2484 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2488 /* I can accept peer_ni's version */
2489 version = reqmsg->ibm_version;
2491 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2492 CERROR("Unexpected connreq msg type: %x from %s\n",
2493 reqmsg->ibm_type, libcfs_nid2str(nid));
2497 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2498 kiblnd_msg_queue_size(version, ni)) {
2499 CERROR("Can't accept conn from %s, queue depth too large: %d (<=%d wanted)\n",
2500 libcfs_nid2str(nid),
2501 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2502 kiblnd_msg_queue_size(version, ni));
2504 if (version == IBLND_MSG_VERSION)
2505 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2510 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2511 IBLND_MAX_RDMA_FRAGS) {
2512 CWARN("Can't accept conn from %s (version %x): max_frags %d too large (%d wanted)\n",
2513 libcfs_nid2str(nid), version,
2514 reqmsg->ibm_u.connparams.ibcp_max_frags,
2515 IBLND_MAX_RDMA_FRAGS);
2517 if (version >= IBLND_MSG_VERSION)
2518 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2521 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2522 IBLND_MAX_RDMA_FRAGS &&
2523 net->ibn_fmr_ps == NULL) {
2524 CWARN("Can't accept conn from %s (version %x): max_frags %d incompatible without FMR pool (%d wanted)\n",
2525 libcfs_nid2str(nid), version,
2526 reqmsg->ibm_u.connparams.ibcp_max_frags,
2527 IBLND_MAX_RDMA_FRAGS);
2529 if (version == IBLND_MSG_VERSION)
2530 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2535 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2536 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2537 libcfs_nid2str(nid),
2538 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2543 /* assume 'nid' is a new peer_ni; create */
2544 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2546 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2547 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2551 /* We have validated the peer's parameters so use those */
2552 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2553 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2555 write_lock_irqsave(g_lock, flags);
2557 peer2 = kiblnd_find_peer_locked(ni, nid);
2558 if (peer2 != NULL) {
2559 if (peer2->ibp_version == 0) {
2560 peer2->ibp_version = version;
2561 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2564 /* not the guy I've talked with */
2565 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2566 peer2->ibp_version != version) {
2567 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2569 if (kiblnd_peer_active(peer2)) {
2570 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2571 peer2->ibp_version = version;
2573 write_unlock_irqrestore(g_lock, flags);
2575 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2576 libcfs_nid2str(nid), peer2->ibp_version, version,
2577 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2579 kiblnd_peer_decref(peer_ni);
2580 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2584 /* Tie-break connection race in favour of the higher NID.
2585 * If we keep running into a race condition multiple times,
2586 * we have to assume that the connection attempt with the
2587 * higher NID is stuck in a connecting state and will never
2588 * recover. As such, we pass through this if-block and let
2589 * the lower NID connection win so we can move forward.
2591 if (peer2->ibp_connecting != 0 &&
2592 nid < ni->ni_nid && peer2->ibp_races <
2593 MAX_CONN_RACES_BEFORE_ABORT) {
2595 write_unlock_irqrestore(g_lock, flags);
2597 CDEBUG(D_NET, "Conn race %s\n",
2598 libcfs_nid2str(peer2->ibp_nid));
2600 kiblnd_peer_decref(peer_ni);
2601 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2604 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2605 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2606 libcfs_nid2str(peer2->ibp_nid),
2607 MAX_CONN_RACES_BEFORE_ABORT);
2609 * passive connection is allowed even this peer_ni is waiting for
2612 peer2->ibp_reconnecting = 0;
2613 peer2->ibp_races = 0;
2614 peer2->ibp_accepting++;
2615 kiblnd_peer_addref(peer2);
2617 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2618 * so copy validated parameters since we now know what the
2619 * peer_ni's limits are */
2620 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2621 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2623 write_unlock_irqrestore(g_lock, flags);
2624 kiblnd_peer_decref(peer_ni);
2627 /* Brand new peer_ni */
2628 LASSERT(peer_ni->ibp_accepting == 0);
2629 LASSERT(peer_ni->ibp_version == 0 &&
2630 peer_ni->ibp_incarnation == 0);
2632 peer_ni->ibp_accepting = 1;
2633 peer_ni->ibp_version = version;
2634 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2636 /* I have a ref on ni that prevents it being shutdown */
2637 LASSERT(net->ibn_shutdown == 0);
2639 kiblnd_peer_addref(peer_ni);
2640 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
2642 write_unlock_irqrestore(g_lock, flags);
2645 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT,
2648 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2649 kiblnd_peer_decref(peer_ni);
2650 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2654 /* conn now "owns" cmid, so I return success from here on to ensure the
2655 * CM callback doesn't destroy cmid.
2657 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2658 conn->ibc_credits = conn->ibc_queue_depth;
2659 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2660 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2661 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2663 ackmsg = &conn->ibc_connvars->cv_msg;
2664 memset(ackmsg, 0, sizeof(*ackmsg));
2666 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2667 sizeof(ackmsg->ibm_u.connparams));
2668 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2669 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2670 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2672 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2674 memset(&cp, 0, sizeof(cp));
2675 cp.private_data = ackmsg;
2676 cp.private_data_len = ackmsg->ibm_nob;
2677 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2678 cp.initiator_depth = 0;
2679 cp.flow_control = 1;
2680 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2681 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2683 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2685 rc = rdma_accept(cmid, &cp);
2687 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2688 rej.ibr_version = version;
2689 rej.ibr_why = IBLND_REJECT_FATAL;
2691 kiblnd_reject(cmid, &rej);
2692 kiblnd_connreq_done(conn, rc);
2693 kiblnd_conn_decref(conn);
2701 rej.ibr_cp.ibcp_queue_depth =
2702 kiblnd_msg_queue_size(version, ni);
2703 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2707 rej.ibr_version = version;
2708 kiblnd_reject(cmid, &rej);
2710 return -ECONNREFUSED;
2714 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2715 u64 incarnation, int why, struct kib_connparams *cp)
2717 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2718 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2720 int msg_size = IBLND_MSG_SIZE;
2724 unsigned long flags;
2726 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2727 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2730 msg_size = cp->ibcp_max_msg_size;
2731 frag_num = cp->ibcp_max_frags;
2732 queue_dep = cp->ibcp_queue_depth;
2735 write_lock_irqsave(glock, flags);
2736 /* retry connection if it's still needed and no other connection
2737 * attempts (active or passive) are in progress
2738 * NB: reconnect is still needed even when ibp_tx_queue is
2739 * empty if ibp_version != version because reconnect may be
2742 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2743 peer_ni->ibp_version != version) &&
2744 peer_ni->ibp_connecting &&
2745 peer_ni->ibp_accepting == 0;
2756 case IBLND_REJECT_RDMA_FRAGS: {
2757 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2760 reason = "can't negotiate max frags";
2763 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2764 #ifdef HAVE_IB_GET_DMA_MR
2766 * This check only makes sense if the kernel supports global
2767 * memory registration. Otherwise, map_on_demand will never == 0
2769 if (!tunables->lnd_map_on_demand) {
2770 reason = "map_on_demand must be enabled";
2774 if (conn->ibc_max_frags <= frag_num) {
2775 reason = "unsupported max frags";
2779 peer_ni->ibp_max_frags = frag_num;
2780 reason = "rdma fragments";
2783 case IBLND_REJECT_MSG_QUEUE_SIZE:
2785 reason = "can't negotiate queue depth";
2788 if (conn->ibc_queue_depth <= queue_dep) {
2789 reason = "unsupported queue depth";
2793 peer_ni->ibp_queue_depth = queue_dep;
2794 reason = "queue depth";
2797 case IBLND_REJECT_CONN_STALE:
2801 case IBLND_REJECT_CONN_RACE:
2802 reason = "conn race";
2805 case IBLND_REJECT_CONN_UNCOMPAT:
2806 reason = "version negotiation";
2810 conn->ibc_reconnect = 1;
2811 peer_ni->ibp_reconnecting++;
2812 peer_ni->ibp_version = version;
2813 if (incarnation != 0)
2814 peer_ni->ibp_incarnation = incarnation;
2816 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2818 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2819 libcfs_nid2str(peer_ni->ibp_nid),
2820 reconnect ? "reconnect" : "don't reconnect",
2821 reason, IBLND_MSG_VERSION, version, msg_size,
2822 conn->ibc_queue_depth, queue_dep,
2823 conn->ibc_max_frags, frag_num);
2825 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2826 * while destroying the zombie
2831 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2833 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2834 int status = -ECONNREFUSED;
2836 LASSERT (!in_interrupt());
2837 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2840 case IB_CM_REJ_STALE_CONN:
2841 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2842 IBLND_REJECT_CONN_STALE, NULL);
2845 case IB_CM_REJ_INVALID_SERVICE_ID:
2846 status = -EHOSTUNREACH;
2847 CNETERR("%s rejected: no listener at %d\n",
2848 libcfs_nid2str(peer_ni->ibp_nid),
2849 *kiblnd_tunables.kib_service);
2852 case IB_CM_REJ_CONSUMER_DEFINED:
2853 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2854 struct kib_rej *rej = priv;
2855 struct kib_connparams *cp = NULL;
2857 __u64 incarnation = -1;
2859 /* NB. default incarnation is -1 because:
2860 * a) V1 will ignore dst incarnation in connreq.
2861 * b) V2 will provide incarnation while rejecting me,
2862 * -1 will be overwrote.
2864 * if I try to connect to a V1 peer_ni with V2 protocol,
2865 * it rejected me then upgrade to V2, I have no idea
2866 * about the upgrading and try to reconnect with V1,
2867 * in this case upgraded V2 can find out I'm trying to
2868 * talk to the old guy and reject me(incarnation is -1).
2871 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2872 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2873 __swab32s(&rej->ibr_magic);
2874 __swab16s(&rej->ibr_version);
2878 if (priv_nob >= sizeof(struct kib_rej) &&
2879 rej->ibr_version > IBLND_MSG_VERSION_1) {
2880 /* priv_nob is always 148 in current version
2881 * of OFED, so we still need to check version.
2882 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE)
2887 __swab64s(&rej->ibr_incarnation);
2888 __swab16s(&cp->ibcp_queue_depth);
2889 __swab16s(&cp->ibcp_max_frags);
2890 __swab32s(&cp->ibcp_max_msg_size);
2893 incarnation = rej->ibr_incarnation;
2896 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2897 rej->ibr_magic != LNET_PROTO_MAGIC) {
2898 CERROR("%s rejected: consumer defined fatal error\n",
2899 libcfs_nid2str(peer_ni->ibp_nid));
2903 if (rej->ibr_version != IBLND_MSG_VERSION &&
2904 rej->ibr_version != IBLND_MSG_VERSION_1) {
2905 CERROR("%s rejected: o2iblnd version %x error\n",
2906 libcfs_nid2str(peer_ni->ibp_nid),
2911 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2912 rej->ibr_version == IBLND_MSG_VERSION_1) {
2913 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2914 libcfs_nid2str(peer_ni->ibp_nid),
2917 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2918 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2921 switch (rej->ibr_why) {
2922 case IBLND_REJECT_CONN_RACE:
2923 case IBLND_REJECT_CONN_STALE:
2924 case IBLND_REJECT_CONN_UNCOMPAT:
2925 case IBLND_REJECT_MSG_QUEUE_SIZE:
2926 case IBLND_REJECT_RDMA_FRAGS:
2927 kiblnd_check_reconnect(conn, rej->ibr_version,
2932 case IBLND_REJECT_NO_RESOURCES:
2933 CERROR("%s rejected: o2iblnd no resources\n",
2934 libcfs_nid2str(peer_ni->ibp_nid));
2937 case IBLND_REJECT_FATAL:
2938 CERROR("%s rejected: o2iblnd fatal error\n",
2939 libcfs_nid2str(peer_ni->ibp_nid));
2943 CERROR("%s rejected: o2iblnd reason %d\n",
2944 libcfs_nid2str(peer_ni->ibp_nid),
2952 CNETERR("%s rejected: reason %d, size %d\n",
2953 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
2957 kiblnd_connreq_done(conn, status);
2961 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
2963 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2964 struct lnet_ni *ni = peer_ni->ibp_ni;
2965 struct kib_net *net = ni->ni_data;
2966 struct kib_msg *msg = priv;
2967 int ver = conn->ibc_version;
2968 int rc = kiblnd_unpack_msg(msg, priv_nob);
2969 unsigned long flags;
2971 LASSERT (net != NULL);
2974 CERROR("Can't unpack connack from %s: %d\n",
2975 libcfs_nid2str(peer_ni->ibp_nid), rc);
2979 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2980 CERROR("Unexpected message %d from %s\n",
2981 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
2986 if (ver != msg->ibm_version) {
2987 CERROR("%s replied version %x is different with "
2988 "requested version %x\n",
2989 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
2994 if (msg->ibm_u.connparams.ibcp_queue_depth >
2995 conn->ibc_queue_depth) {
2996 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
2997 libcfs_nid2str(peer_ni->ibp_nid),
2998 msg->ibm_u.connparams.ibcp_queue_depth,
2999 conn->ibc_queue_depth);
3004 if (msg->ibm_u.connparams.ibcp_max_frags >
3005 conn->ibc_max_frags) {
3006 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3007 libcfs_nid2str(peer_ni->ibp_nid),
3008 msg->ibm_u.connparams.ibcp_max_frags,
3009 conn->ibc_max_frags);
3014 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3015 CERROR("%s max message size %d too big (%d max)\n",
3016 libcfs_nid2str(peer_ni->ibp_nid),
3017 msg->ibm_u.connparams.ibcp_max_msg_size,
3023 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3024 if (msg->ibm_dstnid == ni->ni_nid &&
3025 msg->ibm_dststamp == net->ibn_incarnation)
3029 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3032 CERROR("Bad connection reply from %s, rc = %d, "
3033 "version: %x max_frags: %d\n",
3034 libcfs_nid2str(peer_ni->ibp_nid), rc,
3035 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3039 conn->ibc_incarnation = msg->ibm_srcstamp;
3040 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3041 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3042 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3043 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3044 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3045 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3047 kiblnd_connreq_done(conn, 0);
3051 /* NB My QP has already established itself, so I handle anything going
3052 * wrong here by setting ibc_comms_error.
3053 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3054 * immediately tears it down. */
3057 conn->ibc_comms_error = rc;
3058 kiblnd_connreq_done(conn, 0);
3062 kiblnd_active_connect(struct rdma_cm_id *cmid)
3064 struct kib_peer_ni *peer_ni = cmid->context;
3065 struct kib_conn *conn;
3066 struct kib_msg *msg;
3067 struct rdma_conn_param cp;
3070 unsigned long flags;
3073 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3075 incarnation = peer_ni->ibp_incarnation;
3076 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3077 peer_ni->ibp_version;
3079 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3081 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3084 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3085 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3089 /* conn "owns" cmid now, so I return success from here on to ensure the
3090 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3093 msg = &conn->ibc_connvars->cv_msg;
3095 memset(msg, 0, sizeof(*msg));
3096 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3097 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3098 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3099 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3101 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3102 0, peer_ni->ibp_nid, incarnation);
3104 memset(&cp, 0, sizeof(cp));
3105 cp.private_data = msg;
3106 cp.private_data_len = msg->ibm_nob;
3107 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3108 cp.initiator_depth = 0;
3109 cp.flow_control = 1;
3110 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3111 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3113 LASSERT(cmid->context == (void *)conn);
3114 LASSERT(conn->ibc_cmid == cmid);
3115 rc = rdma_connect_locked(cmid, &cp);
3117 CERROR("Can't connect to %s: %d\n",
3118 libcfs_nid2str(peer_ni->ibp_nid), rc);
3119 kiblnd_connreq_done(conn, rc);
3120 kiblnd_conn_decref(conn);
3127 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3129 struct kib_peer_ni *peer_ni;
3130 struct kib_conn *conn;
3133 switch (event->event) {
3135 CERROR("Unexpected event: %d, status: %d\n",
3136 event->event, event->status);
3139 case RDMA_CM_EVENT_CONNECT_REQUEST:
3140 /* destroy cmid on failure */
3141 rc = kiblnd_passive_connect(cmid,
3142 (void *)KIBLND_CONN_PARAM(event),
3143 KIBLND_CONN_PARAM_LEN(event));
3144 CDEBUG(D_NET, "connreq: %d\n", rc);
3147 case RDMA_CM_EVENT_ADDR_ERROR:
3148 peer_ni = cmid->context;
3149 CNETERR("%s: ADDR ERROR %d\n",
3150 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3151 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3152 kiblnd_peer_decref(peer_ni);
3153 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3155 case RDMA_CM_EVENT_ADDR_RESOLVED:
3156 peer_ni = cmid->context;
3158 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3159 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3161 if (event->status != 0) {
3162 CNETERR("Can't resolve address for %s: %d\n",
3163 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3166 rc = rdma_resolve_route(
3167 cmid, kiblnd_timeout() * 1000);
3169 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3170 struct kib_dev *dev = net->ibn_dev;
3172 CDEBUG(D_NET, "%s: connection bound to "\
3174 libcfs_nid2str(peer_ni->ibp_nid),
3176 &dev->ibd_ifip, cmid->device->name);
3181 /* Can't initiate route resolution */
3182 CERROR("Can't resolve route for %s: %d\n",
3183 libcfs_nid2str(peer_ni->ibp_nid), rc);
3185 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3186 kiblnd_peer_decref(peer_ni);
3187 return rc; /* rc != 0 destroys cmid */
3189 case RDMA_CM_EVENT_ROUTE_ERROR:
3190 peer_ni = cmid->context;
3191 CNETERR("%s: ROUTE ERROR %d\n",
3192 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3193 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3194 kiblnd_peer_decref(peer_ni);
3195 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3197 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3198 peer_ni = cmid->context;
3199 CDEBUG(D_NET,"%s Route resolved: %d\n",
3200 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3202 if (event->status == 0)
3203 return kiblnd_active_connect(cmid);
3205 CNETERR("Can't resolve route for %s: %d\n",
3206 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3207 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3208 kiblnd_peer_decref(peer_ni);
3209 return event->status; /* rc != 0 destroys cmid */
3211 case RDMA_CM_EVENT_UNREACHABLE:
3212 conn = cmid->context;
3213 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3214 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3215 CNETERR("%s: UNREACHABLE %d\n",
3216 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3217 kiblnd_connreq_done(conn, -ENETDOWN);
3218 kiblnd_conn_decref(conn);
3221 case RDMA_CM_EVENT_CONNECT_ERROR:
3222 conn = cmid->context;
3223 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3224 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3225 CNETERR("%s: CONNECT ERROR %d\n",
3226 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3227 kiblnd_connreq_done(conn, -ENOTCONN);
3228 kiblnd_conn_decref(conn);
3231 case RDMA_CM_EVENT_REJECTED:
3232 conn = cmid->context;
3233 switch (conn->ibc_state) {
3237 case IBLND_CONN_PASSIVE_WAIT:
3238 CERROR ("%s: REJECTED %d\n",
3239 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3241 kiblnd_connreq_done(conn, -ECONNRESET);
3244 case IBLND_CONN_ACTIVE_CONNECT:
3245 kiblnd_rejected(conn, event->status,
3246 (void *)KIBLND_CONN_PARAM(event),
3247 KIBLND_CONN_PARAM_LEN(event));
3250 kiblnd_conn_decref(conn);
3253 case RDMA_CM_EVENT_ESTABLISHED:
3254 conn = cmid->context;
3255 switch (conn->ibc_state) {
3259 case IBLND_CONN_PASSIVE_WAIT:
3260 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3261 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3262 kiblnd_connreq_done(conn, 0);
3265 case IBLND_CONN_ACTIVE_CONNECT:
3266 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3267 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3268 kiblnd_check_connreply(conn,
3269 (void *)KIBLND_CONN_PARAM(event),
3270 KIBLND_CONN_PARAM_LEN(event));
3273 /* net keeps its ref on conn! */
3276 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3277 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3280 case RDMA_CM_EVENT_DISCONNECTED:
3281 conn = cmid->context;
3282 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3283 CERROR("%s DISCONNECTED\n",
3284 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3285 kiblnd_connreq_done(conn, -ECONNRESET);
3287 kiblnd_close_conn(conn, 0);
3289 kiblnd_conn_decref(conn);
3290 cmid->context = NULL;
3293 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3294 LCONSOLE_ERROR_MSG(0x131,
3295 "Received notification of device removal\n"
3296 "Please shutdown LNET to allow this to proceed\n");
3297 /* Can't remove network from underneath LNET for now, so I have
3301 case RDMA_CM_EVENT_ADDR_CHANGE:
3302 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3308 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3311 struct list_head *ttmp;
3313 list_for_each(ttmp, txs) {
3314 tx = list_entry(ttmp, struct kib_tx, tx_list);
3316 if (txs != &conn->ibc_active_txs) {
3317 LASSERT(tx->tx_queued);
3319 LASSERT(!tx->tx_queued);
3320 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3323 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3324 CERROR("Timed out tx: %s(WSQ:%d%d%d), %lld seconds\n",
3325 kiblnd_queue2str(conn, txs),
3326 tx->tx_waiting, tx->tx_sending, tx->tx_queued,
3328 ktime_ms_delta(ktime_get(),
3329 tx->tx_deadline) / MSEC_PER_SEC);
3338 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3340 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3341 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3342 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3343 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3344 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3348 kiblnd_check_conns (int idx)
3351 LIST_HEAD(checksends);
3352 LIST_HEAD(timedout_txs);
3353 struct hlist_head *peers = &kiblnd_data.kib_peers[idx];
3354 struct kib_peer_ni *peer_ni;
3355 struct kib_conn *conn;
3356 struct kib_tx *tx, *tx_tmp;
3357 struct list_head *ctmp;
3358 unsigned long flags;
3360 /* NB. We expect to have a look at all the peers and not find any
3361 * RDMAs to time out, so we just use a shared lock while we
3364 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3366 hlist_for_each_entry(peer_ni, peers, ibp_list) {
3367 /* Check tx_deadline */
3368 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3369 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3370 CWARN("Timed out tx for %s: %lld seconds\n",
3371 libcfs_nid2str(peer_ni->ibp_nid),
3372 ktime_ms_delta(ktime_get(),
3373 tx->tx_deadline) / MSEC_PER_SEC);
3374 list_move(&tx->tx_list, &timedout_txs);
3378 list_for_each(ctmp, &peer_ni->ibp_conns) {
3382 conn = list_entry(ctmp, struct kib_conn, ibc_list);
3384 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3386 spin_lock(&conn->ibc_lock);
3388 sendnoop = kiblnd_need_noop(conn);
3389 timedout = kiblnd_conn_timed_out_locked(conn);
3390 if (!sendnoop && !timedout) {
3391 spin_unlock(&conn->ibc_lock);
3396 CERROR("Timed out RDMA with %s (%lld): c: %u, oc: %u, rc: %u\n",
3397 libcfs_nid2str(peer_ni->ibp_nid),
3399 - peer_ni->ibp_last_alive,
3401 conn->ibc_outstanding_credits,
3402 conn->ibc_reserved_credits);
3403 list_add(&conn->ibc_connd_list, &closes);
3405 list_add(&conn->ibc_connd_list, &checksends);
3407 /* +ref for 'closes' or 'checksends' */
3408 kiblnd_conn_addref(conn);
3410 spin_unlock(&conn->ibc_lock);
3414 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3416 if (!list_empty(&timedout_txs))
3417 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3418 LNET_MSG_STATUS_NETWORK_TIMEOUT);
3420 /* Handle timeout by closing the whole
3421 * connection. We can only be sure RDMA activity
3422 * has ceased once the QP has been modified.
3424 while ((conn = list_first_entry_or_null(&closes,
3426 ibc_connd_list)) != NULL) {
3427 list_del(&conn->ibc_connd_list);
3428 kiblnd_close_conn(conn, -ETIMEDOUT);
3429 kiblnd_conn_decref(conn);
3432 /* In case we have enough credits to return via a
3433 * NOOP, but there were no non-blocking tx descs
3434 * free to do it last time...
3436 while ((conn = list_first_entry_or_null(&checksends,
3438 ibc_connd_list)) != NULL) {
3439 list_del(&conn->ibc_connd_list);
3441 spin_lock(&conn->ibc_lock);
3442 kiblnd_check_sends_locked(conn);
3443 spin_unlock(&conn->ibc_lock);
3445 kiblnd_conn_decref(conn);
3450 kiblnd_disconnect_conn(struct kib_conn *conn)
3452 LASSERT (!in_interrupt());
3453 LASSERT (current == kiblnd_data.kib_connd);
3454 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3456 rdma_disconnect(conn->ibc_cmid);
3457 kiblnd_finalise_conn(conn);
3459 kiblnd_peer_notify(conn->ibc_peer);
3463 * High-water for reconnection to the same peer_ni, reconnection attempt should
3464 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3466 #define KIB_RECONN_HIGH_RACE 10
3468 * Allow connd to take a break and handle other things after consecutive
3469 * reconnection attemps.
3471 #define KIB_RECONN_BREAK 100
3474 kiblnd_connd (void *arg)
3476 spinlock_t *lock = &kiblnd_data.kib_connd_lock;
3477 wait_queue_entry_t wait;
3478 unsigned long flags;
3479 struct kib_conn *conn;
3484 unsigned long deadline = jiffies;
3487 kiblnd_data.kib_connd = current;
3489 spin_lock_irqsave(lock, flags);
3491 while (!kiblnd_data.kib_shutdown) {
3494 dropped_lock = false;
3496 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_zombies,
3497 struct kib_conn, ibc_list);
3499 struct kib_peer_ni *peer_ni = NULL;
3501 list_del(&conn->ibc_list);
3502 if (conn->ibc_reconnect) {
3503 peer_ni = conn->ibc_peer;
3504 kiblnd_peer_addref(peer_ni);
3507 spin_unlock_irqrestore(lock, flags);
3508 dropped_lock = true;
3510 kiblnd_destroy_conn(conn);
3512 spin_lock_irqsave(lock, flags);
3514 LIBCFS_FREE(conn, sizeof(*conn));
3518 conn->ibc_peer = peer_ni;
3519 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3520 list_add_tail(&conn->ibc_list,
3521 &kiblnd_data.kib_reconn_list);
3523 list_add_tail(&conn->ibc_list,
3524 &kiblnd_data.kib_reconn_wait);
3527 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_conns,
3528 struct kib_conn, ibc_list);
3532 list_del(&conn->ibc_list);
3534 spin_unlock_irqrestore(lock, flags);
3535 dropped_lock = true;
3537 kiblnd_disconnect_conn(conn);
3538 wait = conn->ibc_waits;
3539 if (wait == 0) /* keep ref for connd_wait, see below */
3540 kiblnd_conn_decref(conn);
3542 spin_lock_irqsave(lock, flags);
3545 list_add_tail(&conn->ibc_list,
3546 &kiblnd_data.kib_connd_waits);
3549 while (reconn < KIB_RECONN_BREAK) {
3550 if (kiblnd_data.kib_reconn_sec !=
3551 ktime_get_real_seconds()) {
3552 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3553 list_splice_init(&kiblnd_data.kib_reconn_wait,
3554 &kiblnd_data.kib_reconn_list);
3557 conn = list_first_entry_or_null(&kiblnd_data.kib_reconn_list,
3558 struct kib_conn, ibc_list);
3562 list_del(&conn->ibc_list);
3564 spin_unlock_irqrestore(lock, flags);
3565 dropped_lock = true;
3567 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3568 kiblnd_peer_decref(conn->ibc_peer);
3569 LIBCFS_FREE(conn, sizeof(*conn));
3571 spin_lock_irqsave(lock, flags);
3574 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_waits,
3578 list_del(&conn->ibc_list);
3579 spin_unlock_irqrestore(lock, flags);
3581 dropped_lock = kiblnd_tx_may_discard(conn);
3583 kiblnd_conn_decref(conn);
3585 spin_lock_irqsave(lock, flags);
3587 list_add_tail(&conn->ibc_list,
3588 &kiblnd_data.kib_connd_waits);
3591 /* careful with the jiffy wrap... */
3592 timeout = (int)(deadline - jiffies);
3596 int chunk = HASH_SIZE(kiblnd_data.kib_peers);
3597 unsigned int lnd_timeout;
3599 spin_unlock_irqrestore(lock, flags);
3600 dropped_lock = true;
3602 /* Time to check for RDMA timeouts on a few more
3603 * peers: I do checks every 'p' seconds on a
3604 * proportion of the peer_ni table and I need to check
3605 * every connection 'n' times within a timeout
3606 * interval, to ensure I detect a timeout on any
3607 * connection within (n+1)/n times the timeout
3611 lnd_timeout = kiblnd_timeout();
3612 if (lnd_timeout > n * p)
3613 chunk = (chunk * n * p) / lnd_timeout;
3617 for (i = 0; i < chunk; i++) {
3618 kiblnd_check_conns(peer_index);
3619 peer_index = (peer_index + 1) %
3620 HASH_SIZE(kiblnd_data.kib_peers);
3623 deadline += cfs_time_seconds(p);
3624 spin_lock_irqsave(lock, flags);
3630 /* Nothing to do for 'timeout' */
3631 set_current_state(TASK_INTERRUPTIBLE);
3632 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3633 spin_unlock_irqrestore(lock, flags);
3635 schedule_timeout(timeout);
3637 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3638 spin_lock_irqsave(lock, flags);
3641 spin_unlock_irqrestore(lock, flags);
3643 kiblnd_thread_fini();
3648 kiblnd_qp_event(struct ib_event *event, void *arg)
3650 struct kib_conn *conn = arg;
3652 switch (event->event) {
3653 case IB_EVENT_COMM_EST:
3654 CDEBUG(D_NET, "%s established\n",
3655 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3656 /* We received a packet but connection isn't established
3657 * probably handshake packet was lost, so free to
3658 * force make connection established */
3659 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3662 case IB_EVENT_PORT_ERR:
3663 case IB_EVENT_DEVICE_FATAL:
3664 CERROR("Fatal device error for NI %s\n",
3665 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3666 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3669 case IB_EVENT_PORT_ACTIVE:
3670 CERROR("Port reactivated for NI %s\n",
3671 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3672 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3676 CERROR("%s: Async QP event type %d\n",
3677 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3683 kiblnd_complete (struct ib_wc *wc)
3685 switch (kiblnd_wreqid2type(wc->wr_id)) {
3690 if (wc->status != IB_WC_SUCCESS &&
3691 wc->status != IB_WC_WR_FLUSH_ERR)
3692 CNETERR("FastReg failed: %d\n", wc->status);
3695 case IBLND_WID_RDMA:
3696 /* We only get RDMA completion notification if it fails. All
3697 * subsequent work items, including the final SEND will fail
3698 * too. However we can't print out any more info about the
3699 * failing RDMA because 'tx' might be back on the idle list or
3700 * even reused already if we didn't manage to post all our work
3702 CNETERR("RDMA (tx: %p) failed: %d\n",
3703 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3707 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3711 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3718 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3720 /* NB I'm not allowed to schedule this conn once its refcount has
3721 * reached 0. Since fundamentally I'm racing with scheduler threads
3722 * consuming my CQ I could be called after all completions have
3723 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3724 * and this CQ is about to be destroyed so I NOOP. */
3725 struct kib_conn *conn = arg;
3726 struct kib_sched_info *sched = conn->ibc_sched;
3727 unsigned long flags;
3729 LASSERT(cq == conn->ibc_cq);
3731 spin_lock_irqsave(&sched->ibs_lock, flags);
3733 conn->ibc_ready = 1;
3735 if (!conn->ibc_scheduled &&
3736 (conn->ibc_nrx > 0 ||
3737 conn->ibc_nsends_posted > 0)) {
3738 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3739 conn->ibc_scheduled = 1;
3740 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3742 if (waitqueue_active(&sched->ibs_waitq))
3743 wake_up(&sched->ibs_waitq);
3746 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3750 kiblnd_cq_event(struct ib_event *event, void *arg)
3752 struct kib_conn *conn = arg;
3754 CERROR("%s: async CQ event type %d\n",
3755 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3759 kiblnd_scheduler(void *arg)
3761 long id = (long)arg;
3762 struct kib_sched_info *sched;
3763 struct kib_conn *conn;
3764 wait_queue_entry_t wait;
3765 unsigned long flags;
3772 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3774 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3776 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);
3779 spin_lock_irqsave(&sched->ibs_lock, flags);
3781 while (!kiblnd_data.kib_shutdown) {
3782 if (need_resched()) {
3783 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3787 spin_lock_irqsave(&sched->ibs_lock, flags);
3790 did_something = false;
3792 conn = list_first_entry_or_null(&sched->ibs_conns,
3796 /* take over kib_sched_conns' ref on conn... */
3797 LASSERT(conn->ibc_scheduled);
3798 list_del(&conn->ibc_sched_list);
3799 conn->ibc_ready = 0;
3801 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3803 wc.wr_id = IBLND_WID_INVAL;
3805 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3807 rc = ib_req_notify_cq(conn->ibc_cq,
3810 CWARN("%s: ib_req_notify_cq failed: %d, closing connection\n",
3811 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3812 kiblnd_close_conn(conn, -EIO);
3813 kiblnd_conn_decref(conn);
3814 spin_lock_irqsave(&sched->ibs_lock,
3819 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3822 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3824 "ib_poll_cq (rc: %d) returned invalid "
3825 "wr_id, opcode %d, status: %d, "
3826 "vendor_err: %d, conn: %s status: %d\n"
3827 "please upgrade firmware and OFED or "
3828 "contact vendor.\n", rc,
3829 wc.opcode, wc.status, wc.vendor_err,
3830 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3836 CWARN("%s: ib_poll_cq failed: %d, closing connection\n",
3837 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3839 kiblnd_close_conn(conn, -EIO);
3840 kiblnd_conn_decref(conn);
3841 spin_lock_irqsave(&sched->ibs_lock, flags);
3845 spin_lock_irqsave(&sched->ibs_lock, flags);
3847 if (rc != 0 || conn->ibc_ready) {
3848 /* There may be another completion waiting; get
3849 * another scheduler to check while I handle
3851 /* +1 ref for sched_conns */
3852 kiblnd_conn_addref(conn);
3853 list_add_tail(&conn->ibc_sched_list,
3855 if (waitqueue_active(&sched->ibs_waitq))
3856 wake_up(&sched->ibs_waitq);
3858 conn->ibc_scheduled = 0;
3862 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3863 kiblnd_complete(&wc);
3865 spin_lock_irqsave(&sched->ibs_lock, flags);
3868 kiblnd_conn_decref(conn); /* ..drop my ref from above */
3869 did_something = true;
3875 set_current_state(TASK_INTERRUPTIBLE);
3876 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3877 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3881 remove_wait_queue(&sched->ibs_waitq, &wait);
3882 set_current_state(TASK_RUNNING);
3883 spin_lock_irqsave(&sched->ibs_lock, flags);
3886 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3888 kiblnd_thread_fini();
3893 kiblnd_failover_thread(void *arg)
3895 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3896 struct kib_dev *dev;
3897 struct net *ns = arg;
3898 wait_queue_entry_t wait;
3899 unsigned long flags;
3902 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3905 write_lock_irqsave(glock, flags);
3907 while (!kiblnd_data.kib_shutdown) {
3908 bool do_failover = false;
3911 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3913 if (ktime_get_seconds() < dev->ibd_next_failover)
3920 list_del_init(&dev->ibd_fail_list);
3921 dev->ibd_failover = 1;
3922 write_unlock_irqrestore(glock, flags);
3924 rc = kiblnd_dev_failover(dev, ns);
3926 write_lock_irqsave(glock, flags);
3928 LASSERT(dev->ibd_failover);
3929 dev->ibd_failover = 0;
3930 if (rc >= 0) { /* Device is OK or failover succeed */
3931 dev->ibd_next_failover = ktime_get_seconds() + 3;
3935 /* failed to failover, retry later */
3936 dev->ibd_next_failover = ktime_get_seconds() +
3937 min(dev->ibd_failed_failover, 10);
3938 if (kiblnd_dev_can_failover(dev)) {
3939 list_add_tail(&dev->ibd_fail_list,
3940 &kiblnd_data.kib_failed_devs);
3946 /* long sleep if no more pending failover */
3947 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3949 set_current_state(TASK_INTERRUPTIBLE);
3950 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3951 write_unlock_irqrestore(glock, flags);
3953 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3954 cfs_time_seconds(1));
3955 set_current_state(TASK_RUNNING);
3956 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3957 write_lock_irqsave(glock, flags);
3959 if (!long_sleep || rc != 0)
3962 /* have a long sleep, routine check all active devices,
3963 * we need checking like this because if there is not active
3964 * connection on the dev and no SEND from local, we may listen
3965 * on wrong HCA for ever while there is a bonding failover
3967 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3968 if (kiblnd_dev_can_failover(dev)) {
3969 list_add_tail(&dev->ibd_fail_list,
3970 &kiblnd_data.kib_failed_devs);
3975 write_unlock_irqrestore(glock, flags);
3977 kiblnd_thread_fini();