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/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/klnds/o2iblnd/o2iblnd_cb.c
34 * Author: Eric Barton <eric@bartonsoftware.com>
39 #define MAX_CONN_RACES_BEFORE_ABORT 20
41 static void kiblnd_peer_alive(struct kib_peer_ni *peer_ni);
42 static void kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
44 static void kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx,
45 int type, int body_nob);
46 static int kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
47 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie);
48 static void kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn);
49 static void kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn);
51 static void kiblnd_unmap_tx(struct kib_tx *tx);
52 static void kiblnd_check_sends_locked(struct kib_conn *conn);
55 kiblnd_tx_done(struct kib_tx *tx)
57 struct lnet_msg *lntmsg[2];
61 LASSERT (!in_interrupt());
62 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
63 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
64 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer_ni response */
65 LASSERT (tx->tx_pool != NULL);
69 /* tx may have up to 2 lnet msgs to finalise */
70 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
71 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
74 if (tx->tx_conn != NULL) {
75 kiblnd_conn_decref(tx->tx_conn);
79 tx->tx_nwrq = tx->tx_nsge = 0;
82 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
84 /* delay finalize until my descs have been freed */
85 for (i = 0; i < 2; i++) {
86 if (lntmsg[i] == NULL)
89 /* propagate health status to LNet for requests */
90 if (i == 0 && lntmsg[i])
91 lntmsg[i]->msg_health_status = tx->tx_hstatus;
93 lnet_finalize(lntmsg[i], rc);
98 kiblnd_txlist_done(struct list_head *txlist, int status,
99 enum lnet_msg_hstatus hstatus)
103 while (!list_empty(txlist)) {
104 tx = list_entry(txlist->next, struct kib_tx, tx_list);
106 list_del(&tx->tx_list);
109 tx->tx_status = status;
110 if (hstatus != LNET_MSG_STATUS_OK)
111 tx->tx_hstatus = hstatus;
116 static struct kib_tx *
117 kiblnd_get_idle_tx(struct lnet_ni *ni, lnet_nid_t target)
119 struct kib_net *net = ni->ni_data;
120 struct list_head *node;
122 struct kib_tx_poolset *tps;
124 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target, ni)];
125 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
128 tx = container_of(node, struct kib_tx, tx_list);
130 LASSERT (tx->tx_nwrq == 0);
131 LASSERT (!tx->tx_queued);
132 LASSERT (tx->tx_sending == 0);
133 LASSERT (!tx->tx_waiting);
134 LASSERT (tx->tx_status == 0);
135 LASSERT (tx->tx_conn == NULL);
136 LASSERT (tx->tx_lntmsg[0] == NULL);
137 LASSERT (tx->tx_lntmsg[1] == NULL);
138 LASSERT (tx->tx_nfrags == 0);
141 tx->tx_hstatus = LNET_MSG_STATUS_OK;
147 kiblnd_drop_rx(struct kib_rx *rx)
149 struct kib_conn *conn = rx->rx_conn;
150 struct kib_sched_info *sched = conn->ibc_sched;
153 spin_lock_irqsave(&sched->ibs_lock, flags);
154 LASSERT(conn->ibc_nrx > 0);
156 spin_unlock_irqrestore(&sched->ibs_lock, flags);
158 kiblnd_conn_decref(conn);
162 kiblnd_post_rx(struct kib_rx *rx, int credit)
164 struct kib_conn *conn = rx->rx_conn;
165 struct kib_net *net = conn->ibc_peer->ibp_ni->ni_data;
166 struct ib_recv_wr *bad_wrq = NULL;
167 #ifdef HAVE_IB_GET_DMA_MR
168 struct ib_mr *mr = conn->ibc_hdev->ibh_mrs;
172 LASSERT (net != NULL);
173 LASSERT (!in_interrupt());
174 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
175 credit == IBLND_POSTRX_PEER_CREDIT ||
176 credit == IBLND_POSTRX_RSRVD_CREDIT);
177 #ifdef HAVE_IB_GET_DMA_MR
180 rx->rx_sge.lkey = mr->lkey;
182 rx->rx_sge.lkey = conn->ibc_hdev->ibh_pd->local_dma_lkey;
184 rx->rx_sge.addr = rx->rx_msgaddr;
185 rx->rx_sge.length = IBLND_MSG_SIZE;
187 rx->rx_wrq.next = NULL;
188 rx->rx_wrq.sg_list = &rx->rx_sge;
189 rx->rx_wrq.num_sge = 1;
190 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
192 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
193 LASSERT (rx->rx_nob >= 0); /* not posted */
195 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
196 kiblnd_drop_rx(rx); /* No more posts for this rx */
200 rx->rx_nob = -1; /* flag posted */
202 /* NB: need an extra reference after ib_post_recv because we don't
203 * own this rx (and rx::rx_conn) anymore, LU-5678.
205 kiblnd_conn_addref(conn);
206 #ifdef HAVE_IB_POST_SEND_RECV_CONST
207 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq,
208 (const struct ib_recv_wr **)&bad_wrq);
210 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
212 if (unlikely(rc != 0)) {
213 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
214 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
218 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
221 if (unlikely(rc != 0)) {
222 kiblnd_close_conn(conn, rc);
223 kiblnd_drop_rx(rx); /* No more posts for this rx */
227 if (credit == IBLND_POSTRX_NO_CREDIT)
230 spin_lock(&conn->ibc_lock);
231 if (credit == IBLND_POSTRX_PEER_CREDIT)
232 conn->ibc_outstanding_credits++;
234 conn->ibc_reserved_credits++;
235 kiblnd_check_sends_locked(conn);
236 spin_unlock(&conn->ibc_lock);
239 kiblnd_conn_decref(conn);
243 static struct kib_tx *
244 kiblnd_find_waiting_tx_locked(struct kib_conn *conn, int txtype, u64 cookie)
246 struct list_head *tmp;
248 list_for_each(tmp, &conn->ibc_active_txs) {
249 struct kib_tx *tx = list_entry(tmp, struct kib_tx, tx_list);
251 LASSERT(!tx->tx_queued);
252 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
254 if (tx->tx_cookie != cookie)
257 if (tx->tx_waiting &&
258 tx->tx_msg->ibm_type == txtype)
261 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
262 tx->tx_waiting ? "" : "NOT ",
263 tx->tx_msg->ibm_type, txtype);
269 kiblnd_handle_completion(struct kib_conn *conn, int txtype, int status, u64 cookie)
272 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
275 spin_lock(&conn->ibc_lock);
277 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
279 spin_unlock(&conn->ibc_lock);
281 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
282 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
283 kiblnd_close_conn(conn, -EPROTO);
287 if (tx->tx_status == 0) { /* success so far */
288 if (status < 0) { /* failed? */
289 tx->tx_status = status;
290 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
291 } else if (txtype == IBLND_MSG_GET_REQ) {
292 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
298 idle = !tx->tx_queued && (tx->tx_sending == 0);
300 list_del(&tx->tx_list);
302 spin_unlock(&conn->ibc_lock);
309 kiblnd_send_completion(struct kib_conn *conn, int type, int status, u64 cookie)
311 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
312 struct kib_tx *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
315 CERROR("Can't get tx for completion %x for %s\n",
316 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
320 tx->tx_msg->ibm_u.completion.ibcm_status = status;
321 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
322 kiblnd_init_tx_msg(ni, tx, type, sizeof(struct kib_completion_msg));
324 kiblnd_queue_tx(tx, conn);
328 kiblnd_handle_rx(struct kib_rx *rx)
330 struct kib_msg *msg = rx->rx_msg;
331 struct kib_conn *conn = rx->rx_conn;
332 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
333 int credits = msg->ibm_credits;
339 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
341 CDEBUG (D_NET, "Received %x[%d] from %s\n",
342 msg->ibm_type, credits,
343 libcfs_nid2str(conn->ibc_peer->ibp_nid));
346 /* Have I received credits that will let me send? */
347 spin_lock(&conn->ibc_lock);
349 if (conn->ibc_credits + credits >
350 conn->ibc_queue_depth) {
351 rc2 = conn->ibc_credits;
352 spin_unlock(&conn->ibc_lock);
354 CERROR("Bad credits from %s: %d + %d > %d\n",
355 libcfs_nid2str(conn->ibc_peer->ibp_nid),
357 conn->ibc_queue_depth);
359 kiblnd_close_conn(conn, -EPROTO);
360 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
364 conn->ibc_credits += credits;
366 /* This ensures the credit taken by NOOP can be returned */
367 if (msg->ibm_type == IBLND_MSG_NOOP &&
368 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
369 conn->ibc_outstanding_credits++;
371 kiblnd_check_sends_locked(conn);
372 spin_unlock(&conn->ibc_lock);
375 switch (msg->ibm_type) {
377 CERROR("Bad IBLND message type %x from %s\n",
378 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
379 post_credit = IBLND_POSTRX_NO_CREDIT;
384 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
385 post_credit = IBLND_POSTRX_NO_CREDIT;
389 if (credits != 0) /* credit already posted */
390 post_credit = IBLND_POSTRX_NO_CREDIT;
391 else /* a keepalive NOOP */
392 post_credit = IBLND_POSTRX_PEER_CREDIT;
395 case IBLND_MSG_IMMEDIATE:
396 post_credit = IBLND_POSTRX_DONT_POST;
397 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
398 msg->ibm_srcnid, rx, 0);
399 if (rc < 0) /* repost on error */
400 post_credit = IBLND_POSTRX_PEER_CREDIT;
403 case IBLND_MSG_PUT_REQ:
404 post_credit = IBLND_POSTRX_DONT_POST;
405 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
406 msg->ibm_srcnid, rx, 1);
407 if (rc < 0) /* repost on error */
408 post_credit = IBLND_POSTRX_PEER_CREDIT;
411 case IBLND_MSG_PUT_NAK:
412 CWARN ("PUT_NACK from %s\n",
413 libcfs_nid2str(conn->ibc_peer->ibp_nid));
414 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
415 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
416 msg->ibm_u.completion.ibcm_status,
417 msg->ibm_u.completion.ibcm_cookie);
420 case IBLND_MSG_PUT_ACK:
421 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
423 spin_lock(&conn->ibc_lock);
424 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
425 msg->ibm_u.putack.ibpam_src_cookie);
427 list_del(&tx->tx_list);
428 spin_unlock(&conn->ibc_lock);
431 CERROR("Unmatched PUT_ACK from %s\n",
432 libcfs_nid2str(conn->ibc_peer->ibp_nid));
437 LASSERT (tx->tx_waiting);
438 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
439 * (a) I can overwrite tx_msg since my peer_ni has received it!
440 * (b) tx_waiting set tells tx_complete() it's not done. */
442 tx->tx_nwrq = tx->tx_nsge = 0; /* overwrite PUT_REQ */
444 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
445 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
446 &msg->ibm_u.putack.ibpam_rd,
447 msg->ibm_u.putack.ibpam_dst_cookie);
449 CERROR("Can't setup rdma for PUT to %s: %d\n",
450 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
452 spin_lock(&conn->ibc_lock);
453 tx->tx_waiting = 0; /* clear waiting and queue atomically */
454 kiblnd_queue_tx_locked(tx, conn);
455 spin_unlock(&conn->ibc_lock);
458 case IBLND_MSG_PUT_DONE:
459 post_credit = IBLND_POSTRX_PEER_CREDIT;
460 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
461 msg->ibm_u.completion.ibcm_status,
462 msg->ibm_u.completion.ibcm_cookie);
465 case IBLND_MSG_GET_REQ:
466 post_credit = IBLND_POSTRX_DONT_POST;
467 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
468 msg->ibm_srcnid, rx, 1);
469 if (rc < 0) /* repost on error */
470 post_credit = IBLND_POSTRX_PEER_CREDIT;
473 case IBLND_MSG_GET_DONE:
474 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
475 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
476 msg->ibm_u.completion.ibcm_status,
477 msg->ibm_u.completion.ibcm_cookie);
481 if (rc < 0) /* protocol error */
482 kiblnd_close_conn(conn, rc);
484 if (post_credit != IBLND_POSTRX_DONT_POST)
485 kiblnd_post_rx(rx, post_credit);
489 kiblnd_rx_complete(struct kib_rx *rx, int status, int nob)
491 struct kib_msg *msg = rx->rx_msg;
492 struct kib_conn *conn = rx->rx_conn;
493 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
494 struct kib_net *net = ni->ni_data;
498 LASSERT (net != NULL);
499 LASSERT (rx->rx_nob < 0); /* was posted */
500 rx->rx_nob = 0; /* isn't now */
502 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
505 if (status != IB_WC_SUCCESS) {
506 CNETERR("Rx from %s failed: %d\n",
507 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
514 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
516 CERROR ("Error %d unpacking rx from %s\n",
517 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
521 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
522 msg->ibm_dstnid != ni->ni_nid ||
523 msg->ibm_srcstamp != conn->ibc_incarnation ||
524 msg->ibm_dststamp != net->ibn_incarnation) {
525 CERROR ("Stale rx from %s\n",
526 libcfs_nid2str(conn->ibc_peer->ibp_nid));
531 /* set time last known alive */
532 kiblnd_peer_alive(conn->ibc_peer);
534 /* racing with connection establishment/teardown! */
536 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
537 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
540 write_lock_irqsave(g_lock, flags);
541 /* must check holding global lock to eliminate race */
542 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
543 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
544 write_unlock_irqrestore(g_lock, flags);
547 write_unlock_irqrestore(g_lock, flags);
549 kiblnd_handle_rx(rx);
553 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
554 kiblnd_close_conn(conn, err);
556 kiblnd_drop_rx(rx); /* Don't re-post rx. */
560 kiblnd_fmr_map_tx(struct kib_net *net, struct kib_tx *tx,
561 struct kib_rdma_desc *rd, u32 nob)
563 struct kib_hca_dev *hdev;
565 struct kib_fmr_poolset *fps;
570 LASSERT(tx->tx_pool != NULL);
571 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
574 hdev = tx->tx_pool->tpo_hdev;
575 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
578 * If we're dealing with FastReg, but the device doesn't
579 * support GAPS and the tx has GAPS, then there is no real point
580 * in trying to map the memory, because it'll just fail. So
581 * preemptively fail with an appropriate message
583 if ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED) &&
584 !(dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT) &&
586 CERROR("Using FastReg with no GAPS support, but tx has gaps. "
587 "Try setting use_fastreg_gaps to 1\n");
588 return -EPROTONOSUPPORT;
591 #ifdef HAVE_FMR_POOL_API
593 * FMR does not support gaps but the tx has gaps then
594 * we should make sure that the number of fragments we'll be sending
595 * over fits within the number of fragments negotiated on the
596 * connection, otherwise, we won't be able to RDMA the data.
597 * We need to maintain the number of fragments negotiation on the
598 * connection for backwards compatibility.
600 if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
602 tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
603 CERROR("TX number of frags (%d) is <= than connection"
604 " number of frags (%d). Consider setting peer's"
605 " map_on_demand to 256\n", tx->tx_nfrags,
606 tx->tx_conn->ibc_max_frags);
612 fps = net->ibn_fmr_ps[cpt];
613 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
615 CERROR("Can't map %u bytes (%u/%u)s: %d\n", nob,
616 tx->tx_nfrags, rd->rd_nfrags, rc);
621 * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
624 rd->rd_key = tx->tx_fmr.fmr_key;
626 * for FastReg or FMR with no gaps we can accumulate all
627 * the fragments in one FastReg or FMR fragment.
630 #ifdef HAVE_FMR_POOL_API
631 ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
634 (dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)) {
635 /* FMR requires zero based address */
636 #ifdef HAVE_FMR_POOL_API
637 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
638 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
640 rd->rd_frags[0].rf_nob = nob;
644 * We're transmitting with gaps using FMR.
645 * We'll need to use multiple fragments and identify the
646 * zero based address of each fragment.
648 for (i = 0; i < rd->rd_nfrags; i++) {
649 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
650 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
658 kiblnd_unmap_tx(struct kib_tx *tx)
661 #ifdef HAVE_FMR_POOL_API
662 tx->tx_fmr.fmr_pfmr ||
665 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
667 if (tx->tx_nfrags != 0) {
668 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
669 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
674 #ifdef HAVE_IB_GET_DMA_MR
675 static struct ib_mr *
676 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
678 struct kib_net *net = ni->ni_data;
679 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
680 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
682 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
685 * if map-on-demand is turned on and the device supports
686 * either FMR or FastReg then use that. Otherwise use global
687 * memory regions. If that's not available either, then you're
688 * dead in the water and fail the operation.
690 if (tunables->lnd_map_on_demand &&
691 (net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED
692 #ifdef HAVE_FMR_POOL_API
693 || net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED
699 * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
700 * in the call chain. The mapping will fail with appropriate error
703 return hdev->ibh_mrs;
707 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
708 struct kib_rdma_desc *rd, int nfrags)
710 struct kib_net *net = ni->ni_data;
711 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
712 #ifdef HAVE_IB_GET_DMA_MR
713 struct ib_mr *mr = NULL;
718 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
720 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
721 tx->tx_nfrags = nfrags;
723 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
724 tx->tx_nfrags, tx->tx_dmadir);
726 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
727 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
728 hdev->ibh_ibdev, &tx->tx_frags[i]);
729 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
730 hdev->ibh_ibdev, &tx->tx_frags[i]);
731 nob += rd->rd_frags[i].rf_nob;
734 #ifdef HAVE_IB_GET_DMA_MR
735 mr = kiblnd_find_rd_dma_mr(ni, rd);
737 /* found pre-mapping MR */
738 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
743 if (net->ibn_fmr_ps != NULL)
744 return kiblnd_fmr_map_tx(net, tx, rd, nob);
749 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
750 struct kib_rdma_desc *rd, int nkiov,
751 struct bio_vec *kiov, int offset, int nob)
753 struct kib_net *net = ni->ni_data;
754 struct scatterlist *sg;
758 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
762 LASSERT(net != NULL);
764 while (offset >= kiov->bv_len) {
765 offset -= kiov->bv_len;
777 fragnob = min((int)(kiov->bv_len - offset), nob);
780 * We're allowed to start at a non-aligned page offset in
781 * the first fragment and end at a non-aligned page offset
782 * in the last fragment.
784 if ((fragnob < (int)(kiov->bv_len - offset)) &&
785 nkiov < max_nkiov && nob > fragnob) {
786 CDEBUG(D_NET, "fragnob %d < available page %d: with"
787 " remaining %d kiovs with %d nob left\n",
788 fragnob, (int)(kiov->bv_len - offset),
793 sg_set_page(sg, kiov->bv_page, fragnob,
794 kiov->bv_offset + offset);
797 CERROR("lacking enough sg entries to map tx\n");
807 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
811 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
812 __must_hold(&conn->ibc_lock)
814 struct kib_msg *msg = tx->tx_msg;
815 struct kib_peer_ni *peer_ni = conn->ibc_peer;
816 struct lnet_ni *ni = peer_ni->ibp_ni;
817 int ver = conn->ibc_version;
821 LASSERT(tx->tx_queued);
822 /* We rely on this for QP sizing */
823 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
824 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
826 LASSERT(credit == 0 || credit == 1);
827 LASSERT(conn->ibc_outstanding_credits >= 0);
828 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
829 LASSERT(conn->ibc_credits >= 0);
830 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
832 if (conn->ibc_nsends_posted ==
833 kiblnd_concurrent_sends(ver, ni)) {
834 /* tx completions outstanding... */
835 CDEBUG(D_NET, "%s: posted enough\n",
836 libcfs_nid2str(peer_ni->ibp_nid));
840 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
841 CDEBUG(D_NET, "%s: no credits\n",
842 libcfs_nid2str(peer_ni->ibp_nid));
846 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
847 conn->ibc_credits == 1 && /* last credit reserved */
848 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
849 CDEBUG(D_NET, "%s: not using last credit\n",
850 libcfs_nid2str(peer_ni->ibp_nid));
854 /* NB don't drop ibc_lock before bumping tx_sending */
855 list_del(&tx->tx_list);
858 if (msg->ibm_type == IBLND_MSG_NOOP &&
859 (!kiblnd_need_noop(conn) || /* redundant NOOP */
860 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
861 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
862 /* OK to drop when posted enough NOOPs, since
863 * kiblnd_check_sends_locked will queue NOOP again when
864 * posted NOOPs complete */
865 spin_unlock(&conn->ibc_lock);
866 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
868 spin_lock(&conn->ibc_lock);
869 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
870 libcfs_nid2str(peer_ni->ibp_nid),
871 conn->ibc_noops_posted);
875 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
876 peer_ni->ibp_nid, conn->ibc_incarnation);
878 conn->ibc_credits -= credit;
879 conn->ibc_outstanding_credits = 0;
880 conn->ibc_nsends_posted++;
881 if (msg->ibm_type == IBLND_MSG_NOOP)
882 conn->ibc_noops_posted++;
884 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
885 * PUT. If so, it was first queued here as a PUT_REQ, sent and
886 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
887 * and then re-queued here. It's (just) possible that
888 * tx_sending is non-zero if we've not done the tx_complete()
889 * from the first send; hence the ++ rather than = below. */
891 list_add(&tx->tx_list, &conn->ibc_active_txs);
893 /* I'm still holding ibc_lock! */
894 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
896 } else if (tx->tx_pool->tpo_pool.po_failed ||
897 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
898 /* close_conn will launch failover */
901 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
902 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
903 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
906 if (!frd->frd_valid) {
907 wr = &frd->frd_inv_wr.wr;
908 wr->next = &frd->frd_fastreg_wr.wr;
910 wr = &frd->frd_fastreg_wr.wr;
912 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
915 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
916 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
917 bad->wr_id, bad->opcode, bad->send_flags,
918 libcfs_nid2str(conn->ibc_peer->ibp_nid));
921 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
924 #ifdef HAVE_IB_POST_SEND_RECV_CONST
925 rc = ib_post_send(conn->ibc_cmid->qp, wr,
926 (const struct ib_send_wr **)&bad);
928 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
932 conn->ibc_last_send = ktime_get();
937 /* NB credits are transferred in the actual
938 * message, which can only be the last work item */
939 conn->ibc_credits += credit;
940 conn->ibc_outstanding_credits += msg->ibm_credits;
941 conn->ibc_nsends_posted--;
942 if (msg->ibm_type == IBLND_MSG_NOOP)
943 conn->ibc_noops_posted--;
949 done = (tx->tx_sending == 0);
951 list_del(&tx->tx_list);
953 spin_unlock(&conn->ibc_lock);
955 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
956 CERROR("Error %d posting transmit to %s\n",
957 rc, libcfs_nid2str(peer_ni->ibp_nid));
959 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
960 rc, libcfs_nid2str(peer_ni->ibp_nid));
962 kiblnd_close_conn(conn, rc);
967 spin_lock(&conn->ibc_lock);
973 kiblnd_check_sends_locked(struct kib_conn *conn)
975 int ver = conn->ibc_version;
976 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
979 /* Don't send anything until after the connection is established */
980 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
981 CDEBUG(D_NET, "%s too soon\n",
982 libcfs_nid2str(conn->ibc_peer->ibp_nid));
986 LASSERT(conn->ibc_nsends_posted <=
987 kiblnd_concurrent_sends(ver, ni));
988 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
989 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
990 LASSERT (conn->ibc_reserved_credits >= 0);
992 while (conn->ibc_reserved_credits > 0 &&
993 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
994 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
995 struct kib_tx, tx_list);
996 list_move_tail(&tx->tx_list, &conn->ibc_tx_queue);
997 conn->ibc_reserved_credits--;
1000 if (kiblnd_need_noop(conn)) {
1001 spin_unlock(&conn->ibc_lock);
1003 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1005 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1007 spin_lock(&conn->ibc_lock);
1009 kiblnd_queue_tx_locked(tx, conn);
1015 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1017 tx = list_entry(conn->ibc_tx_queue_nocred.next,
1018 struct kib_tx, tx_list);
1019 } else if (!list_empty(&conn->ibc_tx_noops)) {
1020 LASSERT (!IBLND_OOB_CAPABLE(ver));
1022 tx = list_entry(conn->ibc_tx_noops.next,
1023 struct kib_tx, tx_list);
1024 } else if (!list_empty(&conn->ibc_tx_queue)) {
1026 tx = list_entry(conn->ibc_tx_queue.next,
1027 struct kib_tx, tx_list);
1031 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1037 kiblnd_tx_complete(struct kib_tx *tx, int status)
1039 int failed = (status != IB_WC_SUCCESS);
1040 struct kib_conn *conn = tx->tx_conn;
1043 if (tx->tx_sending <= 0) {
1044 CERROR("Received an event on a freed tx: %p status %d\n",
1050 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1051 CNETERR("Tx -> %s cookie %#llx"
1052 " sending %d waiting %d: failed %d\n",
1053 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1054 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1057 kiblnd_close_conn(conn, -EIO);
1059 kiblnd_peer_alive(conn->ibc_peer);
1062 spin_lock(&conn->ibc_lock);
1064 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1065 * gets to free it, which also drops its ref on 'conn'. */
1068 conn->ibc_nsends_posted--;
1069 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1070 conn->ibc_noops_posted--;
1073 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1074 tx->tx_waiting = 0; /* don't wait for peer_ni */
1075 tx->tx_status = -EIO;
1078 idle = (tx->tx_sending == 0) && /* This is the final callback */
1079 !tx->tx_waiting && /* Not waiting for peer_ni */
1080 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1082 list_del(&tx->tx_list);
1084 kiblnd_check_sends_locked(conn);
1085 spin_unlock(&conn->ibc_lock);
1092 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1095 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1096 struct ib_sge *sge = &tx->tx_msgsge;
1097 struct ib_rdma_wr *wrq;
1098 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1099 #ifdef HAVE_IB_GET_DMA_MR
1100 struct ib_mr *mr = hdev->ibh_mrs;
1103 LASSERT(tx->tx_nwrq >= 0);
1104 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1105 LASSERT(nob <= IBLND_MSG_SIZE);
1106 #ifdef HAVE_IB_GET_DMA_MR
1107 LASSERT(mr != NULL);
1110 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1112 #ifdef HAVE_IB_GET_DMA_MR
1113 sge->lkey = mr->lkey;
1115 sge->lkey = hdev->ibh_pd->local_dma_lkey;
1117 sge->addr = tx->tx_msgaddr;
1120 wrq = &tx->tx_wrq[tx->tx_nwrq];
1121 memset(wrq, 0, sizeof(*wrq));
1123 wrq->wr.next = NULL;
1124 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1125 wrq->wr.sg_list = sge;
1126 wrq->wr.num_sge = 1;
1127 wrq->wr.opcode = IB_WR_SEND;
1128 wrq->wr.send_flags = IB_SEND_SIGNALED;
1134 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1135 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1137 struct kib_msg *ibmsg = tx->tx_msg;
1138 struct kib_rdma_desc *srcrd = tx->tx_rd;
1139 struct ib_rdma_wr *wrq = NULL;
1147 LASSERT(!in_interrupt());
1148 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1149 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1151 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1152 resid > 0; resid -= sge_nob) {
1155 if (srcidx >= srcrd->rd_nfrags) {
1156 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1161 if (dstidx >= dstrd->rd_nfrags) {
1162 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1167 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1168 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1169 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1170 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1171 conn->ibc_max_frags,
1172 srcidx, srcrd->rd_nfrags,
1173 dstidx, dstrd->rd_nfrags);
1178 sge_nob = min3(kiblnd_rd_frag_size(srcrd, srcidx),
1179 kiblnd_rd_frag_size(dstrd, dstidx),
1182 sge = &tx->tx_sge[tx->tx_nsge];
1183 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1184 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1185 sge->length = sge_nob;
1188 wrq = &tx->tx_wrq[tx->tx_nwrq];
1190 wrq->wr.next = &(wrq + 1)->wr;
1191 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1192 wrq->wr.sg_list = sge;
1193 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1194 wrq->wr.send_flags = 0;
1196 #ifdef HAVE_IB_RDMA_WR
1197 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1199 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1202 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1204 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1209 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1210 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1213 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1215 wrq->wr.num_sge = wrq_sge;
1221 if (rc < 0) /* no RDMA if completing with failure */
1222 tx->tx_nwrq = tx->tx_nsge = 0;
1224 ibmsg->ibm_u.completion.ibcm_status = rc;
1225 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1226 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1227 type, sizeof(struct kib_completion_msg));
1233 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1235 struct list_head *q;
1238 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1239 LASSERT(!tx->tx_queued); /* not queued for sending already */
1240 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1242 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1243 tx->tx_status = -ECONNABORTED;
1245 if (tx->tx_conn != NULL) {
1246 /* PUT_DONE first attached to conn as a PUT_REQ */
1247 LASSERT(tx->tx_conn == conn);
1248 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1250 kiblnd_conn_decref(conn);
1252 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1257 timeout_ns = kiblnd_timeout() * NSEC_PER_SEC;
1259 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1261 if (tx->tx_conn == NULL) {
1262 kiblnd_conn_addref(conn);
1264 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1266 /* PUT_DONE first attached to conn as a PUT_REQ */
1267 LASSERT (tx->tx_conn == conn);
1268 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1271 switch (tx->tx_msg->ibm_type) {
1275 case IBLND_MSG_PUT_REQ:
1276 case IBLND_MSG_GET_REQ:
1277 q = &conn->ibc_tx_queue_rsrvd;
1280 case IBLND_MSG_PUT_NAK:
1281 case IBLND_MSG_PUT_ACK:
1282 case IBLND_MSG_PUT_DONE:
1283 case IBLND_MSG_GET_DONE:
1284 q = &conn->ibc_tx_queue_nocred;
1287 case IBLND_MSG_NOOP:
1288 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1289 q = &conn->ibc_tx_queue_nocred;
1291 q = &conn->ibc_tx_noops;
1294 case IBLND_MSG_IMMEDIATE:
1295 q = &conn->ibc_tx_queue;
1299 list_add_tail(&tx->tx_list, q);
1303 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1305 spin_lock(&conn->ibc_lock);
1306 kiblnd_queue_tx_locked(tx, conn);
1307 kiblnd_check_sends_locked(conn);
1308 spin_unlock(&conn->ibc_lock);
1312 kiblnd_resolve_addr_cap(struct rdma_cm_id *cmid,
1313 struct sockaddr_in *srcaddr,
1314 struct sockaddr_in *dstaddr,
1317 unsigned short port;
1320 /* allow the port to be reused */
1321 rc = rdma_set_reuseaddr(cmid, 1);
1323 CERROR("Unable to set reuse on cmid: %d\n", rc);
1327 /* look for a free privileged port */
1328 for (port = PROT_SOCK-1; port > 0; port--) {
1329 srcaddr->sin_port = htons(port);
1330 rc = rdma_resolve_addr(cmid,
1331 (struct sockaddr *)srcaddr,
1332 (struct sockaddr *)dstaddr,
1335 CDEBUG(D_NET, "bound to port %hu\n", port);
1337 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1338 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1345 CERROR("cannot bind to a free privileged port: rc = %d\n", rc);
1351 kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1352 struct sockaddr_in *srcaddr,
1353 struct sockaddr_in *dstaddr,
1356 const struct cred *old_creds = NULL;
1357 struct cred *new_creds;
1360 if (!capable(CAP_NET_BIND_SERVICE)) {
1361 new_creds = prepare_kernel_cred(NULL);
1365 cap_raise(new_creds->cap_effective, CAP_NET_BIND_SERVICE);
1366 old_creds = override_creds(new_creds);
1369 rc = kiblnd_resolve_addr_cap(cmid, srcaddr, dstaddr, timeout_ms);
1372 revert_creds(old_creds);
1378 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1380 struct rdma_cm_id *cmid;
1381 struct kib_dev *dev;
1382 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1383 struct sockaddr_in srcaddr;
1384 struct sockaddr_in dstaddr;
1387 LASSERT (net != NULL);
1388 LASSERT (peer_ni->ibp_connecting > 0);
1390 cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1391 kiblnd_cm_callback, peer_ni,
1392 RDMA_PS_TCP, IB_QPT_RC);
1395 CERROR("Can't create CMID for %s: %ld\n",
1396 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1402 memset(&srcaddr, 0, sizeof(srcaddr));
1403 srcaddr.sin_family = AF_INET;
1404 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1406 memset(&dstaddr, 0, sizeof(dstaddr));
1407 dstaddr.sin_family = AF_INET;
1408 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1409 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1411 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1413 if (*kiblnd_tunables.kib_use_priv_port) {
1414 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1415 kiblnd_timeout() * 1000);
1417 rc = rdma_resolve_addr(cmid,
1418 (struct sockaddr *)&srcaddr,
1419 (struct sockaddr *)&dstaddr,
1420 kiblnd_timeout() * 1000);
1423 /* Can't initiate address resolution: */
1424 CERROR("Can't resolve addr for %s: %d\n",
1425 libcfs_nid2str(peer_ni->ibp_nid), rc);
1432 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1433 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1434 rdma_destroy_id(cmid);
1437 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1441 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1443 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1444 char *reason = NULL;
1446 unsigned long flags;
1448 write_lock_irqsave(glock, flags);
1449 if (peer_ni->ibp_reconnecting == 0) {
1450 if (peer_ni->ibp_accepting)
1451 reason = "accepting";
1452 else if (peer_ni->ibp_connecting)
1453 reason = "connecting";
1454 else if (!list_empty(&peer_ni->ibp_conns))
1455 reason = "connected";
1456 else /* connected then closed */
1462 if (peer_ni->ibp_accepting)
1463 CNETERR("Detecting race between accepting and reconnecting\n");
1464 peer_ni->ibp_reconnecting--;
1466 if (!kiblnd_peer_active(peer_ni)) {
1467 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1468 reason = "unlinked";
1472 peer_ni->ibp_connecting++;
1473 peer_ni->ibp_reconnected++;
1475 write_unlock_irqrestore(glock, flags);
1477 kiblnd_connect_peer(peer_ni);
1481 write_unlock_irqrestore(glock, flags);
1483 CWARN("Abort reconnection of %s: %s\n",
1484 libcfs_nid2str(peer_ni->ibp_nid), reason);
1485 kiblnd_txlist_done(&txs, -ECONNABORTED,
1486 LNET_MSG_STATUS_LOCAL_ABORTED);
1491 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1493 struct kib_peer_ni *peer_ni;
1494 struct kib_peer_ni *peer2;
1495 struct kib_conn *conn;
1496 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1497 unsigned long flags;
1500 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1502 /* If I get here, I've committed to send, so I complete the tx with
1503 * failure on any problems */
1505 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1506 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1508 /* 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 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(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 (!list_empty(&conn->ibc_early_rxs)) {
2078 rx = list_entry(conn->ibc_early_rxs.next,
2079 struct kib_rx, rx_list);
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;
2211 LASSERT (error != 0);
2212 LASSERT (!in_interrupt());
2214 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2217 LASSERT(peer_ni->ibp_connecting > 0);
2218 peer_ni->ibp_connecting--;
2220 LASSERT (peer_ni->ibp_accepting > 0);
2221 peer_ni->ibp_accepting--;
2224 if (kiblnd_peer_connecting(peer_ni)) {
2225 /* another connection attempt under way... */
2226 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2231 peer_ni->ibp_reconnected = 0;
2232 if (list_empty(&peer_ni->ibp_conns)) {
2233 /* Take peer_ni's blocked transmits to complete with error */
2234 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2236 if (kiblnd_peer_active(peer_ni))
2237 kiblnd_unlink_peer_locked(peer_ni);
2239 peer_ni->ibp_error = error;
2241 /* Can't have blocked transmits if there are connections */
2242 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2245 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2247 kiblnd_peer_notify(peer_ni);
2249 if (list_empty(&zombies))
2252 CNETERR("Deleting messages for %s: connection failed\n",
2253 libcfs_nid2str(peer_ni->ibp_nid));
2255 if (error == -EHOSTUNREACH || error == -ETIMEDOUT)
2256 kiblnd_txlist_done(&zombies, error,
2257 LNET_MSG_STATUS_NETWORK_TIMEOUT);
2259 kiblnd_txlist_done(&zombies, error,
2260 LNET_MSG_STATUS_LOCAL_DROPPED);
2264 kiblnd_connreq_done(struct kib_conn *conn, int status)
2266 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2269 unsigned long flags;
2272 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2274 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2275 libcfs_nid2str(peer_ni->ibp_nid), active,
2276 conn->ibc_version, status);
2278 LASSERT (!in_interrupt());
2279 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2280 peer_ni->ibp_connecting > 0) ||
2281 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2282 peer_ni->ibp_accepting > 0));
2284 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2285 conn->ibc_connvars = NULL;
2288 /* failed to establish connection */
2289 kiblnd_peer_connect_failed(peer_ni, active, status);
2290 kiblnd_finalise_conn(conn);
2294 /* connection established */
2295 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2297 /* reset retry count */
2298 peer_ni->ibp_retries = 0;
2300 conn->ibc_last_send = ktime_get();
2301 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2302 kiblnd_peer_alive(peer_ni);
2304 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2305 * peer_ni instance... */
2306 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2307 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2308 peer_ni->ibp_reconnected = 0;
2310 peer_ni->ibp_connecting--;
2312 peer_ni->ibp_accepting--;
2314 if (peer_ni->ibp_version == 0) {
2315 peer_ni->ibp_version = conn->ibc_version;
2316 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2319 if (peer_ni->ibp_version != conn->ibc_version ||
2320 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2321 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2322 conn->ibc_incarnation);
2323 peer_ni->ibp_version = conn->ibc_version;
2324 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2327 /* grab pending txs while I have the lock */
2328 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2330 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2331 conn->ibc_comms_error != 0) { /* error has happened already */
2333 /* start to shut down connection */
2334 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2335 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2337 kiblnd_txlist_done(&txs, -ECONNABORTED,
2338 LNET_MSG_STATUS_LOCAL_ERROR);
2343 /* +1 ref for myself, this connection is visible to other threads
2344 * now, refcount of peer:ibp_conns can be released by connection
2345 * close from either a different thread, or the calling of
2346 * kiblnd_check_sends_locked() below. See bz21911 for details.
2348 kiblnd_conn_addref(conn);
2349 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2351 /* Schedule blocked txs
2352 * Note: if we are running with conns_per_peer > 1, these blocked
2353 * txs will all get scheduled to the first connection which gets
2354 * scheduled. We won't be using round robin on this first batch.
2356 spin_lock(&conn->ibc_lock);
2357 while (!list_empty(&txs)) {
2358 tx = list_entry(txs.next, struct kib_tx, tx_list);
2359 list_del(&tx->tx_list);
2361 kiblnd_queue_tx_locked(tx, conn);
2363 kiblnd_check_sends_locked(conn);
2364 spin_unlock(&conn->ibc_lock);
2366 /* schedule blocked rxs */
2367 kiblnd_handle_early_rxs(conn);
2368 kiblnd_conn_decref(conn);
2372 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2376 #ifdef HAVE_RDMA_REJECT_4ARGS
2377 rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2379 rc = rdma_reject(cmid, rej, sizeof(*rej));
2383 CWARN("Error %d sending reject\n", rc);
2387 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2389 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2390 struct kib_msg *reqmsg = priv;
2391 struct kib_msg *ackmsg;
2392 struct kib_dev *ibdev;
2393 struct kib_peer_ni *peer_ni;
2394 struct kib_peer_ni *peer2;
2395 struct kib_conn *conn;
2396 struct lnet_ni *ni = NULL;
2397 struct kib_net *net = NULL;
2399 struct rdma_conn_param cp;
2401 int version = IBLND_MSG_VERSION;
2402 unsigned long flags;
2404 struct sockaddr_in *peer_addr;
2405 LASSERT (!in_interrupt());
2407 /* cmid inherits 'context' from the corresponding listener id */
2408 ibdev = cmid->context;
2411 memset(&rej, 0, sizeof(rej));
2412 rej.ibr_magic = IBLND_MSG_MAGIC;
2413 rej.ibr_why = IBLND_REJECT_FATAL;
2414 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2416 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2417 if (*kiblnd_tunables.kib_require_priv_port &&
2418 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2419 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2420 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2421 &ip, ntohs(peer_addr->sin_port));
2425 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2426 CERROR("Short connection request\n");
2430 /* Future protocol version compatibility support! If the
2431 * o2iblnd-specific protocol changes, or when LNET unifies
2432 * protocols over all LNDs, the initial connection will
2433 * negotiate a protocol version. I trap this here to avoid
2434 * console errors; the reject tells the peer_ni which protocol I
2436 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2437 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2439 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2440 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2441 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2443 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2444 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2445 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2448 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2450 CERROR("Can't parse connection request: %d\n", rc);
2454 nid = reqmsg->ibm_srcnid;
2455 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2458 net = (struct kib_net *)ni->ni_data;
2459 rej.ibr_incarnation = net->ibn_incarnation;
2462 if (ni == NULL || /* no matching net */
2463 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2464 net->ibn_dev != ibdev) { /* wrong device */
2465 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2466 "bad dst nid %s\n", libcfs_nid2str(nid),
2467 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2468 ibdev->ibd_ifname, ibdev->ibd_nnets,
2470 libcfs_nid2str(reqmsg->ibm_dstnid));
2475 /* check time stamp as soon as possible */
2476 if (reqmsg->ibm_dststamp != 0 &&
2477 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2478 CWARN("Stale connection request\n");
2479 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2483 /* I can accept peer_ni's version */
2484 version = reqmsg->ibm_version;
2486 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2487 CERROR("Unexpected connreq msg type: %x from %s\n",
2488 reqmsg->ibm_type, libcfs_nid2str(nid));
2492 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2493 kiblnd_msg_queue_size(version, ni)) {
2494 CERROR("Can't accept conn from %s, queue depth too large: "
2495 " %d (<=%d wanted)\n",
2496 libcfs_nid2str(nid),
2497 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2498 kiblnd_msg_queue_size(version, ni));
2500 if (version == IBLND_MSG_VERSION)
2501 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2506 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2507 IBLND_MAX_RDMA_FRAGS) {
2508 CWARN("Can't accept conn from %s (version %x): "
2509 "max_frags %d too large (%d wanted)\n",
2510 libcfs_nid2str(nid), version,
2511 reqmsg->ibm_u.connparams.ibcp_max_frags,
2512 IBLND_MAX_RDMA_FRAGS);
2514 if (version >= IBLND_MSG_VERSION)
2515 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2518 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2519 IBLND_MAX_RDMA_FRAGS &&
2520 net->ibn_fmr_ps == NULL) {
2521 CWARN("Can't accept conn from %s (version %x): "
2522 "max_frags %d incompatible without FMR pool "
2524 libcfs_nid2str(nid), version,
2525 reqmsg->ibm_u.connparams.ibcp_max_frags,
2526 IBLND_MAX_RDMA_FRAGS);
2528 if (version == IBLND_MSG_VERSION)
2529 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2534 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2535 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2536 libcfs_nid2str(nid),
2537 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2542 /* assume 'nid' is a new peer_ni; create */
2543 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2545 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2546 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2550 /* We have validated the peer's parameters so use those */
2551 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2552 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2554 write_lock_irqsave(g_lock, flags);
2556 peer2 = kiblnd_find_peer_locked(ni, nid);
2557 if (peer2 != NULL) {
2558 if (peer2->ibp_version == 0) {
2559 peer2->ibp_version = version;
2560 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2563 /* not the guy I've talked with */
2564 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2565 peer2->ibp_version != version) {
2566 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2568 if (kiblnd_peer_active(peer2)) {
2569 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2570 peer2->ibp_version = version;
2572 write_unlock_irqrestore(g_lock, flags);
2574 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2575 libcfs_nid2str(nid), peer2->ibp_version, version,
2576 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2578 kiblnd_peer_decref(peer_ni);
2579 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2583 /* Tie-break connection race in favour of the higher NID.
2584 * If we keep running into a race condition multiple times,
2585 * we have to assume that the connection attempt with the
2586 * higher NID is stuck in a connecting state and will never
2587 * recover. As such, we pass through this if-block and let
2588 * the lower NID connection win so we can move forward.
2590 if (peer2->ibp_connecting != 0 &&
2591 nid < ni->ni_nid && peer2->ibp_races <
2592 MAX_CONN_RACES_BEFORE_ABORT) {
2594 write_unlock_irqrestore(g_lock, flags);
2596 CDEBUG(D_NET, "Conn race %s\n",
2597 libcfs_nid2str(peer2->ibp_nid));
2599 kiblnd_peer_decref(peer_ni);
2600 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2603 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2604 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2605 libcfs_nid2str(peer2->ibp_nid),
2606 MAX_CONN_RACES_BEFORE_ABORT);
2608 * passive connection is allowed even this peer_ni is waiting for
2611 peer2->ibp_reconnecting = 0;
2612 peer2->ibp_races = 0;
2613 peer2->ibp_accepting++;
2614 kiblnd_peer_addref(peer2);
2616 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2617 * so copy validated parameters since we now know what the
2618 * peer_ni's limits are */
2619 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2620 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2622 write_unlock_irqrestore(g_lock, flags);
2623 kiblnd_peer_decref(peer_ni);
2626 /* Brand new peer_ni */
2627 LASSERT (peer_ni->ibp_accepting == 0);
2628 LASSERT (peer_ni->ibp_version == 0 &&
2629 peer_ni->ibp_incarnation == 0);
2631 peer_ni->ibp_accepting = 1;
2632 peer_ni->ibp_version = version;
2633 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2635 /* I have a ref on ni that prevents it being shutdown */
2636 LASSERT (net->ibn_shutdown == 0);
2638 kiblnd_peer_addref(peer_ni);
2639 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
2641 write_unlock_irqrestore(g_lock, flags);
2644 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2646 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2647 kiblnd_peer_decref(peer_ni);
2648 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2652 /* conn now "owns" cmid, so I return success from here on to ensure the
2653 * CM callback doesn't destroy cmid. */
2654 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2655 conn->ibc_credits = conn->ibc_queue_depth;
2656 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2657 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2658 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2660 ackmsg = &conn->ibc_connvars->cv_msg;
2661 memset(ackmsg, 0, sizeof(*ackmsg));
2663 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2664 sizeof(ackmsg->ibm_u.connparams));
2665 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2666 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2667 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2669 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2671 memset(&cp, 0, sizeof(cp));
2672 cp.private_data = ackmsg;
2673 cp.private_data_len = ackmsg->ibm_nob;
2674 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2675 cp.initiator_depth = 0;
2676 cp.flow_control = 1;
2677 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2678 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2680 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2682 rc = rdma_accept(cmid, &cp);
2684 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2685 rej.ibr_version = version;
2686 rej.ibr_why = IBLND_REJECT_FATAL;
2688 kiblnd_reject(cmid, &rej);
2689 kiblnd_connreq_done(conn, rc);
2690 kiblnd_conn_decref(conn);
2698 rej.ibr_cp.ibcp_queue_depth =
2699 kiblnd_msg_queue_size(version, ni);
2700 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2704 rej.ibr_version = version;
2705 kiblnd_reject(cmid, &rej);
2707 return -ECONNREFUSED;
2711 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2712 u64 incarnation, int why, struct kib_connparams *cp)
2714 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2715 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2717 int msg_size = IBLND_MSG_SIZE;
2721 unsigned long flags;
2723 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2724 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2727 msg_size = cp->ibcp_max_msg_size;
2728 frag_num = cp->ibcp_max_frags;
2729 queue_dep = cp->ibcp_queue_depth;
2732 write_lock_irqsave(glock, flags);
2733 /* retry connection if it's still needed and no other connection
2734 * attempts (active or passive) are in progress
2735 * NB: reconnect is still needed even when ibp_tx_queue is
2736 * empty if ibp_version != version because reconnect may be
2739 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2740 peer_ni->ibp_version != version) &&
2741 peer_ni->ibp_connecting &&
2742 peer_ni->ibp_accepting == 0;
2748 if (peer_ni->ibp_retries > *kiblnd_tunables.kib_retry_count) {
2749 reason = "retry count exceeded due to no listener";
2758 case IBLND_REJECT_RDMA_FRAGS: {
2759 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2762 reason = "can't negotiate max frags";
2765 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2766 #ifdef HAVE_IB_GET_DMA_MR
2768 * This check only makes sense if the kernel supports global
2769 * memory registration. Otherwise, map_on_demand will never == 0
2771 if (!tunables->lnd_map_on_demand) {
2772 reason = "map_on_demand must be enabled";
2776 if (conn->ibc_max_frags <= frag_num) {
2777 reason = "unsupported max frags";
2781 peer_ni->ibp_max_frags = frag_num;
2782 reason = "rdma fragments";
2785 case IBLND_REJECT_MSG_QUEUE_SIZE:
2787 reason = "can't negotiate queue depth";
2790 if (conn->ibc_queue_depth <= queue_dep) {
2791 reason = "unsupported queue depth";
2795 peer_ni->ibp_queue_depth = queue_dep;
2796 reason = "queue depth";
2799 case IBLND_REJECT_CONN_STALE:
2803 case IBLND_REJECT_CONN_RACE:
2804 reason = "conn race";
2807 case IBLND_REJECT_CONN_UNCOMPAT:
2808 reason = "version negotiation";
2811 case IBLND_REJECT_INVALID_SRV_ID:
2812 reason = "invalid service id";
2816 conn->ibc_reconnect = 1;
2817 peer_ni->ibp_reconnecting++;
2818 peer_ni->ibp_version = version;
2819 if (incarnation != 0)
2820 peer_ni->ibp_incarnation = incarnation;
2822 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2824 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2825 libcfs_nid2str(peer_ni->ibp_nid),
2826 reconnect ? "reconnect" : "don't reconnect",
2827 reason, IBLND_MSG_VERSION, version, msg_size,
2828 conn->ibc_queue_depth, queue_dep,
2829 conn->ibc_max_frags, frag_num);
2831 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2832 * while destroying the zombie
2837 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2839 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2841 LASSERT (!in_interrupt());
2842 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2845 case IB_CM_REJ_STALE_CONN:
2846 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2847 IBLND_REJECT_CONN_STALE, NULL);
2850 case IB_CM_REJ_INVALID_SERVICE_ID:
2851 peer_ni->ibp_retries++;
2852 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2853 IBLND_REJECT_INVALID_SRV_ID, NULL);
2854 CNETERR("%s rejected: no listener at %d\n",
2855 libcfs_nid2str(peer_ni->ibp_nid),
2856 *kiblnd_tunables.kib_service);
2859 case IB_CM_REJ_CONSUMER_DEFINED:
2860 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2861 struct kib_rej *rej = priv;
2862 struct kib_connparams *cp = NULL;
2864 __u64 incarnation = -1;
2866 /* NB. default incarnation is -1 because:
2867 * a) V1 will ignore dst incarnation in connreq.
2868 * b) V2 will provide incarnation while rejecting me,
2869 * -1 will be overwrote.
2871 * if I try to connect to a V1 peer_ni with V2 protocol,
2872 * it rejected me then upgrade to V2, I have no idea
2873 * about the upgrading and try to reconnect with V1,
2874 * in this case upgraded V2 can find out I'm trying to
2875 * talk to the old guy and reject me(incarnation is -1).
2878 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2879 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2880 __swab32s(&rej->ibr_magic);
2881 __swab16s(&rej->ibr_version);
2885 if (priv_nob >= sizeof(struct kib_rej) &&
2886 rej->ibr_version > IBLND_MSG_VERSION_1) {
2887 /* priv_nob is always 148 in current version
2888 * of OFED, so we still need to check version.
2889 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2893 __swab64s(&rej->ibr_incarnation);
2894 __swab16s(&cp->ibcp_queue_depth);
2895 __swab16s(&cp->ibcp_max_frags);
2896 __swab32s(&cp->ibcp_max_msg_size);
2899 incarnation = rej->ibr_incarnation;
2902 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2903 rej->ibr_magic != LNET_PROTO_MAGIC) {
2904 CERROR("%s rejected: consumer defined fatal error\n",
2905 libcfs_nid2str(peer_ni->ibp_nid));
2909 if (rej->ibr_version != IBLND_MSG_VERSION &&
2910 rej->ibr_version != IBLND_MSG_VERSION_1) {
2911 CERROR("%s rejected: o2iblnd version %x error\n",
2912 libcfs_nid2str(peer_ni->ibp_nid),
2917 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2918 rej->ibr_version == IBLND_MSG_VERSION_1) {
2919 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2920 libcfs_nid2str(peer_ni->ibp_nid), rej->ibr_version);
2922 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2923 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2926 switch (rej->ibr_why) {
2927 case IBLND_REJECT_CONN_RACE:
2928 case IBLND_REJECT_CONN_STALE:
2929 case IBLND_REJECT_CONN_UNCOMPAT:
2930 case IBLND_REJECT_MSG_QUEUE_SIZE:
2931 case IBLND_REJECT_RDMA_FRAGS:
2932 kiblnd_check_reconnect(conn, rej->ibr_version,
2933 incarnation, rej->ibr_why, cp);
2936 case IBLND_REJECT_NO_RESOURCES:
2937 CERROR("%s rejected: o2iblnd no resources\n",
2938 libcfs_nid2str(peer_ni->ibp_nid));
2941 case IBLND_REJECT_FATAL:
2942 CERROR("%s rejected: o2iblnd fatal error\n",
2943 libcfs_nid2str(peer_ni->ibp_nid));
2947 CERROR("%s rejected: o2iblnd reason %d\n",
2948 libcfs_nid2str(peer_ni->ibp_nid),
2956 CNETERR("%s rejected: reason %d, size %d\n",
2957 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
2961 kiblnd_connreq_done(conn, -ECONNREFUSED);
2965 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
2967 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2968 struct lnet_ni *ni = peer_ni->ibp_ni;
2969 struct kib_net *net = ni->ni_data;
2970 struct kib_msg *msg = priv;
2971 int ver = conn->ibc_version;
2972 int rc = kiblnd_unpack_msg(msg, priv_nob);
2973 unsigned long flags;
2975 LASSERT (net != NULL);
2978 CERROR("Can't unpack connack from %s: %d\n",
2979 libcfs_nid2str(peer_ni->ibp_nid), rc);
2983 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2984 CERROR("Unexpected message %d from %s\n",
2985 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
2990 if (ver != msg->ibm_version) {
2991 CERROR("%s replied version %x is different with "
2992 "requested version %x\n",
2993 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
2998 if (msg->ibm_u.connparams.ibcp_queue_depth >
2999 conn->ibc_queue_depth) {
3000 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
3001 libcfs_nid2str(peer_ni->ibp_nid),
3002 msg->ibm_u.connparams.ibcp_queue_depth,
3003 conn->ibc_queue_depth);
3008 if (msg->ibm_u.connparams.ibcp_max_frags >
3009 conn->ibc_max_frags) {
3010 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3011 libcfs_nid2str(peer_ni->ibp_nid),
3012 msg->ibm_u.connparams.ibcp_max_frags,
3013 conn->ibc_max_frags);
3018 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3019 CERROR("%s max message size %d too big (%d max)\n",
3020 libcfs_nid2str(peer_ni->ibp_nid),
3021 msg->ibm_u.connparams.ibcp_max_msg_size,
3027 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3028 if (msg->ibm_dstnid == ni->ni_nid &&
3029 msg->ibm_dststamp == net->ibn_incarnation)
3033 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3036 CERROR("Bad connection reply from %s, rc = %d, "
3037 "version: %x max_frags: %d\n",
3038 libcfs_nid2str(peer_ni->ibp_nid), rc,
3039 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3043 conn->ibc_incarnation = msg->ibm_srcstamp;
3044 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3045 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3046 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3047 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3048 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3049 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3051 kiblnd_connreq_done(conn, 0);
3055 /* NB My QP has already established itself, so I handle anything going
3056 * wrong here by setting ibc_comms_error.
3057 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3058 * immediately tears it down. */
3061 conn->ibc_comms_error = rc;
3062 kiblnd_connreq_done(conn, 0);
3066 kiblnd_active_connect(struct rdma_cm_id *cmid)
3068 struct kib_peer_ni *peer_ni = cmid->context;
3069 struct kib_conn *conn;
3070 struct kib_msg *msg;
3071 struct rdma_conn_param cp;
3074 unsigned long flags;
3077 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3079 incarnation = peer_ni->ibp_incarnation;
3080 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3081 peer_ni->ibp_version;
3083 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3085 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3088 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3089 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3093 /* conn "owns" cmid now, so I return success from here on to ensure the
3094 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3097 msg = &conn->ibc_connvars->cv_msg;
3099 memset(msg, 0, sizeof(*msg));
3100 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3101 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3102 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3103 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3105 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3106 0, peer_ni->ibp_nid, incarnation);
3108 memset(&cp, 0, sizeof(cp));
3109 cp.private_data = msg;
3110 cp.private_data_len = msg->ibm_nob;
3111 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3112 cp.initiator_depth = 0;
3113 cp.flow_control = 1;
3114 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3115 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3117 LASSERT(cmid->context == (void *)conn);
3118 LASSERT(conn->ibc_cmid == cmid);
3120 rc = rdma_connect(cmid, &cp);
3122 CERROR("Can't connect to %s: %d\n",
3123 libcfs_nid2str(peer_ni->ibp_nid), rc);
3124 kiblnd_connreq_done(conn, rc);
3125 kiblnd_conn_decref(conn);
3132 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3134 struct kib_peer_ni *peer_ni;
3135 struct kib_conn *conn;
3138 switch (event->event) {
3140 CERROR("Unexpected event: %d, status: %d\n",
3141 event->event, event->status);
3144 case RDMA_CM_EVENT_CONNECT_REQUEST:
3145 /* destroy cmid on failure */
3146 rc = kiblnd_passive_connect(cmid,
3147 (void *)KIBLND_CONN_PARAM(event),
3148 KIBLND_CONN_PARAM_LEN(event));
3149 CDEBUG(D_NET, "connreq: %d\n", rc);
3152 case RDMA_CM_EVENT_ADDR_ERROR:
3153 peer_ni = cmid->context;
3154 CNETERR("%s: ADDR ERROR %d\n",
3155 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3156 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3157 kiblnd_peer_decref(peer_ni);
3158 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3160 case RDMA_CM_EVENT_ADDR_RESOLVED:
3161 peer_ni = cmid->context;
3163 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3164 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3166 if (event->status != 0) {
3167 CNETERR("Can't resolve address for %s: %d\n",
3168 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3171 rc = rdma_resolve_route(
3172 cmid, kiblnd_timeout() * 1000);
3174 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3175 struct kib_dev *dev = net->ibn_dev;
3177 CDEBUG(D_NET, "%s: connection bound to "\
3179 libcfs_nid2str(peer_ni->ibp_nid),
3181 &dev->ibd_ifip, cmid->device->name);
3186 /* Can't initiate route resolution */
3187 CERROR("Can't resolve route for %s: %d\n",
3188 libcfs_nid2str(peer_ni->ibp_nid), rc);
3190 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3191 kiblnd_peer_decref(peer_ni);
3192 return rc; /* rc != 0 destroys cmid */
3194 case RDMA_CM_EVENT_ROUTE_ERROR:
3195 peer_ni = cmid->context;
3196 CNETERR("%s: ROUTE ERROR %d\n",
3197 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3198 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3199 kiblnd_peer_decref(peer_ni);
3200 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3202 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3203 peer_ni = cmid->context;
3204 CDEBUG(D_NET,"%s Route resolved: %d\n",
3205 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3207 if (event->status == 0)
3208 return kiblnd_active_connect(cmid);
3210 CNETERR("Can't resolve route for %s: %d\n",
3211 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3212 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3213 kiblnd_peer_decref(peer_ni);
3214 return event->status; /* rc != 0 destroys cmid */
3216 case RDMA_CM_EVENT_UNREACHABLE:
3217 conn = cmid->context;
3218 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3219 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3220 CNETERR("%s: UNREACHABLE %d\n",
3221 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3222 kiblnd_connreq_done(conn, -ENETDOWN);
3223 kiblnd_conn_decref(conn);
3226 case RDMA_CM_EVENT_CONNECT_ERROR:
3227 conn = cmid->context;
3228 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3229 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3230 CNETERR("%s: CONNECT ERROR %d\n",
3231 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3232 kiblnd_connreq_done(conn, -ENOTCONN);
3233 kiblnd_conn_decref(conn);
3236 case RDMA_CM_EVENT_REJECTED:
3237 conn = cmid->context;
3238 switch (conn->ibc_state) {
3242 case IBLND_CONN_PASSIVE_WAIT:
3243 CERROR ("%s: REJECTED %d\n",
3244 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3246 kiblnd_connreq_done(conn, -ECONNRESET);
3249 case IBLND_CONN_ACTIVE_CONNECT:
3250 kiblnd_rejected(conn, event->status,
3251 (void *)KIBLND_CONN_PARAM(event),
3252 KIBLND_CONN_PARAM_LEN(event));
3255 kiblnd_conn_decref(conn);
3258 case RDMA_CM_EVENT_ESTABLISHED:
3259 conn = cmid->context;
3260 switch (conn->ibc_state) {
3264 case IBLND_CONN_PASSIVE_WAIT:
3265 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3266 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3267 kiblnd_connreq_done(conn, 0);
3270 case IBLND_CONN_ACTIVE_CONNECT:
3271 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3272 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3273 kiblnd_check_connreply(conn,
3274 (void *)KIBLND_CONN_PARAM(event),
3275 KIBLND_CONN_PARAM_LEN(event));
3278 /* net keeps its ref on conn! */
3281 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3282 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3285 case RDMA_CM_EVENT_DISCONNECTED:
3286 conn = cmid->context;
3287 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3288 CERROR("%s DISCONNECTED\n",
3289 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3290 kiblnd_connreq_done(conn, -ECONNRESET);
3292 kiblnd_close_conn(conn, 0);
3294 kiblnd_conn_decref(conn);
3295 cmid->context = NULL;
3298 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3299 LCONSOLE_ERROR_MSG(0x131,
3300 "Received notification of device removal\n"
3301 "Please shutdown LNET to allow this to proceed\n");
3302 /* Can't remove network from underneath LNET for now, so I have
3306 case RDMA_CM_EVENT_ADDR_CHANGE:
3307 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3313 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3316 struct list_head *ttmp;
3318 list_for_each(ttmp, txs) {
3319 tx = list_entry(ttmp, struct kib_tx, tx_list);
3321 if (txs != &conn->ibc_active_txs) {
3322 LASSERT(tx->tx_queued);
3324 LASSERT(!tx->tx_queued);
3325 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3328 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3329 CERROR("Timed out tx: %s(WSQ:%d%d%d), %lld seconds\n",
3330 kiblnd_queue2str(conn, txs),
3331 tx->tx_waiting, tx->tx_sending, tx->tx_queued,
3333 ktime_ms_delta(ktime_get(),
3334 tx->tx_deadline) / MSEC_PER_SEC);
3343 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3345 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3346 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3347 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3348 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3349 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3353 kiblnd_check_conns (int idx)
3356 LIST_HEAD(checksends);
3357 LIST_HEAD(timedout_txs);
3358 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3359 struct list_head *ptmp;
3360 struct kib_peer_ni *peer_ni;
3361 struct kib_conn *conn;
3362 struct kib_tx *tx, *tx_tmp;
3363 struct list_head *ctmp;
3364 unsigned long flags;
3366 /* NB. We expect to have a look at all the peers and not find any
3367 * RDMAs to time out, so we just use a shared lock while we
3369 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3371 list_for_each(ptmp, peers) {
3372 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
3374 /* Check tx_deadline */
3375 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3376 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3377 CWARN("Timed out tx for %s: %lld seconds\n",
3378 libcfs_nid2str(peer_ni->ibp_nid),
3379 ktime_ms_delta(ktime_get(),
3380 tx->tx_deadline) / MSEC_PER_SEC);
3381 list_move(&tx->tx_list, &timedout_txs);
3385 list_for_each(ctmp, &peer_ni->ibp_conns) {
3389 conn = list_entry(ctmp, struct kib_conn, ibc_list);
3391 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3393 spin_lock(&conn->ibc_lock);
3395 sendnoop = kiblnd_need_noop(conn);
3396 timedout = kiblnd_conn_timed_out_locked(conn);
3397 if (!sendnoop && !timedout) {
3398 spin_unlock(&conn->ibc_lock);
3403 CERROR("Timed out RDMA with %s (%lld): "
3404 "c: %u, oc: %u, rc: %u\n",
3405 libcfs_nid2str(peer_ni->ibp_nid),
3406 ktime_get_seconds() - peer_ni->ibp_last_alive,
3408 conn->ibc_outstanding_credits,
3409 conn->ibc_reserved_credits);
3410 list_add(&conn->ibc_connd_list, &closes);
3412 list_add(&conn->ibc_connd_list, &checksends);
3414 /* +ref for 'closes' or 'checksends' */
3415 kiblnd_conn_addref(conn);
3417 spin_unlock(&conn->ibc_lock);
3421 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3423 if (!list_empty(&timedout_txs))
3424 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3425 LNET_MSG_STATUS_NETWORK_TIMEOUT);
3427 /* Handle timeout by closing the whole
3428 * connection. We can only be sure RDMA activity
3429 * has ceased once the QP has been modified. */
3430 while (!list_empty(&closes)) {
3431 conn = list_entry(closes.next,
3432 struct kib_conn, ibc_connd_list);
3433 list_del(&conn->ibc_connd_list);
3434 kiblnd_close_conn(conn, -ETIMEDOUT);
3435 kiblnd_conn_decref(conn);
3438 /* In case we have enough credits to return via a
3439 * NOOP, but there were no non-blocking tx descs
3440 * free to do it last time... */
3441 while (!list_empty(&checksends)) {
3442 conn = list_entry(checksends.next,
3443 struct kib_conn, ibc_connd_list);
3444 list_del(&conn->ibc_connd_list);
3446 spin_lock(&conn->ibc_lock);
3447 kiblnd_check_sends_locked(conn);
3448 spin_unlock(&conn->ibc_lock);
3450 kiblnd_conn_decref(conn);
3455 kiblnd_disconnect_conn(struct kib_conn *conn)
3457 LASSERT (!in_interrupt());
3458 LASSERT (current == kiblnd_data.kib_connd);
3459 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3461 rdma_disconnect(conn->ibc_cmid);
3462 kiblnd_finalise_conn(conn);
3464 kiblnd_peer_notify(conn->ibc_peer);
3468 * High-water for reconnection to the same peer_ni, reconnection attempt should
3469 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3471 #define KIB_RECONN_HIGH_RACE 10
3473 * Allow connd to take a break and handle other things after consecutive
3474 * reconnection attemps.
3476 #define KIB_RECONN_BREAK 100
3479 kiblnd_connd (void *arg)
3481 spinlock_t *lock= &kiblnd_data.kib_connd_lock;
3482 wait_queue_entry_t wait;
3483 unsigned long flags;
3484 struct kib_conn *conn;
3489 unsigned long deadline = jiffies;
3492 kiblnd_data.kib_connd = current;
3494 spin_lock_irqsave(lock, flags);
3496 while (!kiblnd_data.kib_shutdown) {
3501 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3502 struct kib_peer_ni *peer_ni = NULL;
3504 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3505 struct kib_conn, ibc_list);
3506 list_del(&conn->ibc_list);
3507 if (conn->ibc_reconnect) {
3508 peer_ni = conn->ibc_peer;
3509 kiblnd_peer_addref(peer_ni);
3512 spin_unlock_irqrestore(lock, flags);
3515 kiblnd_destroy_conn(conn);
3517 spin_lock_irqsave(lock, flags);
3519 LIBCFS_FREE(conn, sizeof(*conn));
3523 conn->ibc_peer = peer_ni;
3524 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3525 list_add_tail(&conn->ibc_list,
3526 &kiblnd_data.kib_reconn_list);
3528 list_add_tail(&conn->ibc_list,
3529 &kiblnd_data.kib_reconn_wait);
3532 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3534 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3535 struct kib_conn, ibc_list);
3536 list_del(&conn->ibc_list);
3538 spin_unlock_irqrestore(lock, flags);
3541 kiblnd_disconnect_conn(conn);
3542 wait = conn->ibc_waits;
3543 if (wait == 0) /* keep ref for connd_wait, see below */
3544 kiblnd_conn_decref(conn);
3546 spin_lock_irqsave(lock, flags);
3549 list_add_tail(&conn->ibc_list,
3550 &kiblnd_data.kib_connd_waits);
3553 while (reconn < KIB_RECONN_BREAK) {
3554 if (kiblnd_data.kib_reconn_sec !=
3555 ktime_get_real_seconds()) {
3556 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3557 list_splice_init(&kiblnd_data.kib_reconn_wait,
3558 &kiblnd_data.kib_reconn_list);
3561 if (list_empty(&kiblnd_data.kib_reconn_list))
3564 conn = list_entry(kiblnd_data.kib_reconn_list.next,
3565 struct kib_conn, ibc_list);
3566 list_del(&conn->ibc_list);
3568 spin_unlock_irqrestore(lock, flags);
3571 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3572 kiblnd_peer_decref(conn->ibc_peer);
3573 LIBCFS_FREE(conn, sizeof(*conn));
3575 spin_lock_irqsave(lock, flags);
3578 if (!list_empty(&kiblnd_data.kib_connd_waits)) {
3579 conn = list_entry(kiblnd_data.kib_connd_waits.next,
3580 struct kib_conn, ibc_list);
3581 list_del(&conn->ibc_list);
3582 spin_unlock_irqrestore(lock, flags);
3584 dropped_lock = kiblnd_tx_may_discard(conn);
3586 kiblnd_conn_decref(conn);
3588 spin_lock_irqsave(lock, flags);
3589 if (dropped_lock == 0)
3590 list_add_tail(&conn->ibc_list,
3591 &kiblnd_data.kib_connd_waits);
3594 /* careful with the jiffy wrap... */
3595 timeout = (int)(deadline - jiffies);
3599 int chunk = kiblnd_data.kib_peer_hash_size;
3600 unsigned int lnd_timeout;
3602 spin_unlock_irqrestore(lock, flags);
3605 /* Time to check for RDMA timeouts on a few more
3606 * peers: I do checks every 'p' seconds on a
3607 * proportion of the peer_ni table and I need to check
3608 * every connection 'n' times within a timeout
3609 * interval, to ensure I detect a timeout on any
3610 * connection within (n+1)/n times the timeout
3613 lnd_timeout = kiblnd_timeout();
3614 if (lnd_timeout > n * p)
3615 chunk = (chunk * n * p) / lnd_timeout;
3619 for (i = 0; i < chunk; i++) {
3620 kiblnd_check_conns(peer_index);
3621 peer_index = (peer_index + 1) %
3622 kiblnd_data.kib_peer_hash_size;
3625 deadline += cfs_time_seconds(p);
3626 spin_lock_irqsave(lock, flags);
3632 /* Nothing to do for 'timeout' */
3633 set_current_state(TASK_INTERRUPTIBLE);
3634 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3635 spin_unlock_irqrestore(lock, flags);
3637 schedule_timeout(timeout);
3639 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3640 spin_lock_irqsave(lock, flags);
3643 spin_unlock_irqrestore(lock, flags);
3645 kiblnd_thread_fini();
3650 kiblnd_qp_event(struct ib_event *event, void *arg)
3652 struct kib_conn *conn = arg;
3654 switch (event->event) {
3655 case IB_EVENT_COMM_EST:
3656 CDEBUG(D_NET, "%s established\n",
3657 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3658 /* We received a packet but connection isn't established
3659 * probably handshake packet was lost, so free to
3660 * force make connection established */
3661 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3664 case IB_EVENT_PORT_ERR:
3665 case IB_EVENT_DEVICE_FATAL:
3666 CERROR("Fatal device error for NI %s\n",
3667 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3668 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3671 case IB_EVENT_PORT_ACTIVE:
3672 CERROR("Port reactivated for NI %s\n",
3673 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3674 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3678 CERROR("%s: Async QP event type %d\n",
3679 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3685 kiblnd_complete (struct ib_wc *wc)
3687 switch (kiblnd_wreqid2type(wc->wr_id)) {
3692 if (wc->status != IB_WC_SUCCESS &&
3693 wc->status != IB_WC_WR_FLUSH_ERR)
3694 CNETERR("FastReg failed: %d\n", wc->status);
3697 case IBLND_WID_RDMA:
3698 /* We only get RDMA completion notification if it fails. All
3699 * subsequent work items, including the final SEND will fail
3700 * too. However we can't print out any more info about the
3701 * failing RDMA because 'tx' might be back on the idle list or
3702 * even reused already if we didn't manage to post all our work
3704 CNETERR("RDMA (tx: %p) failed: %d\n",
3705 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3709 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3713 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3720 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3722 /* NB I'm not allowed to schedule this conn once its refcount has
3723 * reached 0. Since fundamentally I'm racing with scheduler threads
3724 * consuming my CQ I could be called after all completions have
3725 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3726 * and this CQ is about to be destroyed so I NOOP. */
3727 struct kib_conn *conn = arg;
3728 struct kib_sched_info *sched = conn->ibc_sched;
3729 unsigned long flags;
3731 LASSERT(cq == conn->ibc_cq);
3733 spin_lock_irqsave(&sched->ibs_lock, flags);
3735 conn->ibc_ready = 1;
3737 if (!conn->ibc_scheduled &&
3738 (conn->ibc_nrx > 0 ||
3739 conn->ibc_nsends_posted > 0)) {
3740 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3741 conn->ibc_scheduled = 1;
3742 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3744 if (waitqueue_active(&sched->ibs_waitq))
3745 wake_up(&sched->ibs_waitq);
3748 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3752 kiblnd_cq_event(struct ib_event *event, void *arg)
3754 struct kib_conn *conn = arg;
3756 CERROR("%s: async CQ event type %d\n",
3757 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3761 kiblnd_scheduler(void *arg)
3763 long id = (long)arg;
3764 struct kib_sched_info *sched;
3765 struct kib_conn *conn;
3766 wait_queue_entry_t wait;
3767 unsigned long flags;
3774 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3776 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3778 CWARN("Unable to bind on CPU partition %d, please verify "
3779 "whether all CPUs are healthy and reload modules if "
3780 "necessary, otherwise your system might under risk of "
3781 "low performance\n", sched->ibs_cpt);
3784 spin_lock_irqsave(&sched->ibs_lock, flags);
3786 while (!kiblnd_data.kib_shutdown) {
3787 if (need_resched()) {
3788 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3792 spin_lock_irqsave(&sched->ibs_lock, flags);
3797 if (!list_empty(&sched->ibs_conns)) {
3798 conn = list_entry(sched->ibs_conns.next,
3799 struct kib_conn, ibc_sched_list);
3800 /* take over kib_sched_conns' ref on conn... */
3801 LASSERT(conn->ibc_scheduled);
3802 list_del(&conn->ibc_sched_list);
3803 conn->ibc_ready = 0;
3805 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3807 wc.wr_id = IBLND_WID_INVAL;
3809 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3811 rc = ib_req_notify_cq(conn->ibc_cq,
3814 CWARN("%s: ib_req_notify_cq failed: %d, "
3815 "closing connection\n",
3816 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3817 kiblnd_close_conn(conn, -EIO);
3818 kiblnd_conn_decref(conn);
3819 spin_lock_irqsave(&sched->ibs_lock,
3824 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3827 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3829 "ib_poll_cq (rc: %d) returned invalid "
3830 "wr_id, opcode %d, status: %d, "
3831 "vendor_err: %d, conn: %s status: %d\n"
3832 "please upgrade firmware and OFED or "
3833 "contact vendor.\n", rc,
3834 wc.opcode, wc.status, wc.vendor_err,
3835 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3841 CWARN("%s: ib_poll_cq failed: %d, "
3842 "closing connection\n",
3843 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3845 kiblnd_close_conn(conn, -EIO);
3846 kiblnd_conn_decref(conn);
3847 spin_lock_irqsave(&sched->ibs_lock, flags);
3851 spin_lock_irqsave(&sched->ibs_lock, flags);
3853 if (rc != 0 || conn->ibc_ready) {
3854 /* There may be another completion waiting; get
3855 * another scheduler to check while I handle
3857 /* +1 ref for sched_conns */
3858 kiblnd_conn_addref(conn);
3859 list_add_tail(&conn->ibc_sched_list,
3861 if (waitqueue_active(&sched->ibs_waitq))
3862 wake_up(&sched->ibs_waitq);
3864 conn->ibc_scheduled = 0;
3868 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3869 kiblnd_complete(&wc);
3871 spin_lock_irqsave(&sched->ibs_lock, flags);
3874 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3881 set_current_state(TASK_INTERRUPTIBLE);
3882 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3883 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3887 remove_wait_queue(&sched->ibs_waitq, &wait);
3888 set_current_state(TASK_RUNNING);
3889 spin_lock_irqsave(&sched->ibs_lock, flags);
3892 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3894 kiblnd_thread_fini();
3899 kiblnd_failover_thread(void *arg)
3901 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3902 struct kib_dev *dev;
3903 struct net *ns = arg;
3904 wait_queue_entry_t wait;
3905 unsigned long flags;
3908 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3911 write_lock_irqsave(glock, flags);
3913 while (!kiblnd_data.kib_shutdown) {
3914 int do_failover = 0;
3917 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3919 if (ktime_get_seconds() < dev->ibd_next_failover)
3926 list_del_init(&dev->ibd_fail_list);
3927 dev->ibd_failover = 1;
3928 write_unlock_irqrestore(glock, flags);
3930 rc = kiblnd_dev_failover(dev, ns);
3932 write_lock_irqsave(glock, flags);
3934 LASSERT (dev->ibd_failover);
3935 dev->ibd_failover = 0;
3936 if (rc >= 0) { /* Device is OK or failover succeed */
3937 dev->ibd_next_failover = ktime_get_seconds() + 3;
3941 /* failed to failover, retry later */
3942 dev->ibd_next_failover = ktime_get_seconds() +
3943 min(dev->ibd_failed_failover, 10);
3944 if (kiblnd_dev_can_failover(dev)) {
3945 list_add_tail(&dev->ibd_fail_list,
3946 &kiblnd_data.kib_failed_devs);
3952 /* long sleep if no more pending failover */
3953 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3955 set_current_state(TASK_INTERRUPTIBLE);
3956 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3957 write_unlock_irqrestore(glock, flags);
3959 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3960 cfs_time_seconds(1));
3961 set_current_state(TASK_RUNNING);
3962 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3963 write_lock_irqsave(glock, flags);
3965 if (!long_sleep || rc != 0)
3968 /* have a long sleep, routine check all active devices,
3969 * we need checking like this because if there is not active
3970 * connection on the dev and no SEND from local, we may listen
3971 * on wrong HCA for ever while there is a bonding failover */
3972 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3973 if (kiblnd_dev_can_failover(dev)) {
3974 list_add_tail(&dev->ibd_fail_list,
3975 &kiblnd_data.kib_failed_devs);
3980 write_unlock_irqrestore(glock, flags);
3982 kiblnd_thread_fini();