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_iov(struct lnet_ni *ni, struct kib_tx *tx,
750 struct kib_rdma_desc *rd, unsigned int niov,
751 struct kvec *iov, int offset, int nob)
753 struct kib_net *net = ni->ni_data;
755 struct scatterlist *sg;
759 unsigned int max_niov;
763 LASSERT (net != NULL);
765 while (offset >= iov->iov_len) {
766 offset -= iov->iov_len;
778 vaddr = ((unsigned long)iov->iov_base) + offset;
779 page_offset = vaddr & (PAGE_SIZE - 1);
780 page = lnet_kvaddr_to_page(vaddr);
782 CERROR("Can't find page\n");
786 fragnob = min((int)(iov->iov_len - offset), nob);
787 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
790 * We're allowed to start at a non-aligned page offset in
791 * the first fragment and end at a non-aligned page offset
792 * in the last fragment.
794 if ((fragnob < (int)PAGE_SIZE - page_offset) &&
795 (niov < max_niov) && nob > fragnob) {
796 CDEBUG(D_NET, "fragnob %d < available page %d: with"
797 " remaining %d iovs with %d nob left\n",
798 fragnob, (int)PAGE_SIZE - page_offset, niov,
803 sg_set_page(sg, page, fragnob, page_offset);
806 CERROR("lacking enough sg entries to map tx\n");
810 if (offset + fragnob < iov->iov_len) {
820 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
823 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
824 struct kib_rdma_desc *rd, int nkiov,
825 lnet_kiov_t *kiov, int offset, int nob)
827 struct kib_net *net = ni->ni_data;
828 struct scatterlist *sg;
832 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
836 LASSERT(net != NULL);
838 while (offset >= kiov->kiov_len) {
839 offset -= kiov->kiov_len;
851 fragnob = min((int)(kiov->kiov_len - offset), nob);
854 * We're allowed to start at a non-aligned page offset in
855 * the first fragment and end at a non-aligned page offset
856 * in the last fragment.
858 if ((fragnob < (int)(kiov->kiov_len - offset)) &&
859 nkiov < max_nkiov && nob > fragnob) {
860 CDEBUG(D_NET, "fragnob %d < available page %d: with"
861 " remaining %d kiovs with %d nob left\n",
862 fragnob, (int)(kiov->kiov_len - offset),
867 sg_set_page(sg, kiov->kiov_page, fragnob,
868 kiov->kiov_offset + offset);
871 CERROR("lacking enough sg entries to map tx\n");
881 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
885 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
886 __must_hold(&conn->ibc_lock)
888 struct kib_msg *msg = tx->tx_msg;
889 struct kib_peer_ni *peer_ni = conn->ibc_peer;
890 struct lnet_ni *ni = peer_ni->ibp_ni;
891 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
892 int ver = conn->ibc_version;
896 LASSERT(tx->tx_queued);
897 /* We rely on this for QP sizing */
898 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
899 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
901 LASSERT(credit == 0 || credit == 1);
902 LASSERT(conn->ibc_outstanding_credits >= 0);
903 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
904 LASSERT(conn->ibc_credits >= 0);
905 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
907 if (conn->ibc_nsends_posted ==
908 kiblnd_concurrent_sends(ver, ni)) {
909 /* tx completions outstanding... */
910 CDEBUG(D_NET, "%s: posted enough\n",
911 libcfs_nid2str(peer_ni->ibp_nid));
915 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
916 CDEBUG(D_NET, "%s: no credits\n",
917 libcfs_nid2str(peer_ni->ibp_nid));
921 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
922 conn->ibc_credits == 1 && /* last credit reserved */
923 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
924 CDEBUG(D_NET, "%s: not using last credit\n",
925 libcfs_nid2str(peer_ni->ibp_nid));
929 /* NB don't drop ibc_lock before bumping tx_sending */
930 list_del(&tx->tx_list);
933 if (msg->ibm_type == IBLND_MSG_NOOP &&
934 (!kiblnd_need_noop(conn) || /* redundant NOOP */
935 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
936 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
937 /* OK to drop when posted enough NOOPs, since
938 * kiblnd_check_sends_locked will queue NOOP again when
939 * posted NOOPs complete */
940 spin_unlock(&conn->ibc_lock);
941 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
943 spin_lock(&conn->ibc_lock);
944 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
945 libcfs_nid2str(peer_ni->ibp_nid),
946 conn->ibc_noops_posted);
950 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
951 peer_ni->ibp_nid, conn->ibc_incarnation);
953 conn->ibc_credits -= credit;
954 conn->ibc_outstanding_credits = 0;
955 conn->ibc_nsends_posted++;
956 if (msg->ibm_type == IBLND_MSG_NOOP)
957 conn->ibc_noops_posted++;
959 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
960 * PUT. If so, it was first queued here as a PUT_REQ, sent and
961 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
962 * and then re-queued here. It's (just) possible that
963 * tx_sending is non-zero if we've not done the tx_complete()
964 * from the first send; hence the ++ rather than = below. */
966 list_add(&tx->tx_list, &conn->ibc_active_txs);
968 /* I'm still holding ibc_lock! */
969 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
971 } else if (tx->tx_pool->tpo_pool.po_failed ||
972 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
973 /* close_conn will launch failover */
976 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
977 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
979 if (frd != NULL && !frd->frd_posted) {
980 if (!frd->frd_valid) {
981 wr = &frd->frd_inv_wr.wr;
982 wr->next = &frd->frd_fastreg_wr.wr;
984 wr = &frd->frd_fastreg_wr.wr;
986 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
989 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
990 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
991 bad->wr_id, bad->opcode, bad->send_flags,
992 libcfs_nid2str(conn->ibc_peer->ibp_nid));
995 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
998 #ifdef HAVE_IB_POST_SEND_RECV_CONST
999 rc = ib_post_send(conn->ibc_cmid->qp, wr,
1000 (const struct ib_send_wr **)&bad);
1002 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
1006 conn->ibc_last_send = ktime_get();
1010 frd->frd_posted = true;
1014 /* NB credits are transferred in the actual
1015 * message, which can only be the last work item */
1016 conn->ibc_credits += credit;
1017 conn->ibc_outstanding_credits += msg->ibm_credits;
1018 conn->ibc_nsends_posted--;
1019 if (msg->ibm_type == IBLND_MSG_NOOP)
1020 conn->ibc_noops_posted--;
1026 done = (tx->tx_sending == 0);
1028 list_del(&tx->tx_list);
1030 spin_unlock(&conn->ibc_lock);
1032 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1033 CERROR("Error %d posting transmit to %s\n",
1034 rc, libcfs_nid2str(peer_ni->ibp_nid));
1036 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1037 rc, libcfs_nid2str(peer_ni->ibp_nid));
1039 kiblnd_close_conn(conn, rc);
1044 spin_lock(&conn->ibc_lock);
1050 kiblnd_check_sends_locked(struct kib_conn *conn)
1052 int ver = conn->ibc_version;
1053 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
1056 /* Don't send anything until after the connection is established */
1057 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
1058 CDEBUG(D_NET, "%s too soon\n",
1059 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1063 LASSERT(conn->ibc_nsends_posted <=
1064 kiblnd_concurrent_sends(ver, ni));
1065 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
1066 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
1067 LASSERT (conn->ibc_reserved_credits >= 0);
1069 while (conn->ibc_reserved_credits > 0 &&
1070 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
1071 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
1072 struct kib_tx, tx_list);
1073 list_del(&tx->tx_list);
1074 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
1075 conn->ibc_reserved_credits--;
1078 if (kiblnd_need_noop(conn)) {
1079 spin_unlock(&conn->ibc_lock);
1081 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1083 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1085 spin_lock(&conn->ibc_lock);
1087 kiblnd_queue_tx_locked(tx, conn);
1093 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1095 tx = list_entry(conn->ibc_tx_queue_nocred.next,
1096 struct kib_tx, tx_list);
1097 } else if (!list_empty(&conn->ibc_tx_noops)) {
1098 LASSERT (!IBLND_OOB_CAPABLE(ver));
1100 tx = list_entry(conn->ibc_tx_noops.next,
1101 struct kib_tx, tx_list);
1102 } else if (!list_empty(&conn->ibc_tx_queue)) {
1104 tx = list_entry(conn->ibc_tx_queue.next,
1105 struct kib_tx, tx_list);
1109 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1115 kiblnd_tx_complete(struct kib_tx *tx, int status)
1117 int failed = (status != IB_WC_SUCCESS);
1118 struct kib_conn *conn = tx->tx_conn;
1121 if (tx->tx_sending <= 0) {
1122 CERROR("Received an event on a freed tx: %p status %d\n",
1128 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1129 CNETERR("Tx -> %s cookie %#llx"
1130 " sending %d waiting %d: failed %d\n",
1131 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1132 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1135 kiblnd_close_conn(conn, -EIO);
1137 kiblnd_peer_alive(conn->ibc_peer);
1140 spin_lock(&conn->ibc_lock);
1142 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1143 * gets to free it, which also drops its ref on 'conn'. */
1146 conn->ibc_nsends_posted--;
1147 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1148 conn->ibc_noops_posted--;
1151 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1152 tx->tx_waiting = 0; /* don't wait for peer_ni */
1153 tx->tx_status = -EIO;
1156 idle = (tx->tx_sending == 0) && /* This is the final callback */
1157 !tx->tx_waiting && /* Not waiting for peer_ni */
1158 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1160 list_del(&tx->tx_list);
1162 kiblnd_check_sends_locked(conn);
1163 spin_unlock(&conn->ibc_lock);
1170 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1173 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1174 struct ib_sge *sge = &tx->tx_msgsge;
1175 struct ib_rdma_wr *wrq;
1176 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1177 #ifdef HAVE_IB_GET_DMA_MR
1178 struct ib_mr *mr = hdev->ibh_mrs;
1181 LASSERT(tx->tx_nwrq >= 0);
1182 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1183 LASSERT(nob <= IBLND_MSG_SIZE);
1184 #ifdef HAVE_IB_GET_DMA_MR
1185 LASSERT(mr != NULL);
1188 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1190 #ifdef HAVE_IB_GET_DMA_MR
1191 sge->lkey = mr->lkey;
1193 sge->lkey = hdev->ibh_pd->local_dma_lkey;
1195 sge->addr = tx->tx_msgaddr;
1198 wrq = &tx->tx_wrq[tx->tx_nwrq];
1199 memset(wrq, 0, sizeof(*wrq));
1201 wrq->wr.next = NULL;
1202 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1203 wrq->wr.sg_list = sge;
1204 wrq->wr.num_sge = 1;
1205 wrq->wr.opcode = IB_WR_SEND;
1206 wrq->wr.send_flags = IB_SEND_SIGNALED;
1212 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1213 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1215 struct kib_msg *ibmsg = tx->tx_msg;
1216 struct kib_rdma_desc *srcrd = tx->tx_rd;
1217 struct ib_rdma_wr *wrq = NULL;
1225 LASSERT(!in_interrupt());
1226 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1227 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1229 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1230 resid > 0; resid -= sge_nob) {
1233 if (srcidx >= srcrd->rd_nfrags) {
1234 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1239 if (dstidx >= dstrd->rd_nfrags) {
1240 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1245 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1246 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1247 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1248 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1249 conn->ibc_max_frags,
1250 srcidx, srcrd->rd_nfrags,
1251 dstidx, dstrd->rd_nfrags);
1256 sge_nob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1257 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1259 sge = &tx->tx_sge[tx->tx_nsge];
1260 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1261 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1262 sge->length = sge_nob;
1265 wrq = &tx->tx_wrq[tx->tx_nwrq];
1267 wrq->wr.next = &(wrq + 1)->wr;
1268 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1269 wrq->wr.sg_list = sge;
1270 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1271 wrq->wr.send_flags = 0;
1273 #ifdef HAVE_IB_RDMA_WR
1274 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1276 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1279 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1281 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1286 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1287 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1290 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1292 wrq->wr.num_sge = wrq_sge;
1298 if (rc < 0) /* no RDMA if completing with failure */
1299 tx->tx_nwrq = tx->tx_nsge = 0;
1301 ibmsg->ibm_u.completion.ibcm_status = rc;
1302 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1303 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1304 type, sizeof(struct kib_completion_msg));
1310 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1312 struct list_head *q;
1315 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1316 LASSERT(!tx->tx_queued); /* not queued for sending already */
1317 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1319 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1320 tx->tx_status = -ECONNABORTED;
1322 if (tx->tx_conn != NULL) {
1323 /* PUT_DONE first attached to conn as a PUT_REQ */
1324 LASSERT(tx->tx_conn == conn);
1325 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1327 kiblnd_conn_decref(conn);
1329 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1334 timeout_ns = lnet_get_lnd_timeout() * NSEC_PER_SEC;
1336 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1338 if (tx->tx_conn == NULL) {
1339 kiblnd_conn_addref(conn);
1341 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1343 /* PUT_DONE first attached to conn as a PUT_REQ */
1344 LASSERT (tx->tx_conn == conn);
1345 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1348 switch (tx->tx_msg->ibm_type) {
1352 case IBLND_MSG_PUT_REQ:
1353 case IBLND_MSG_GET_REQ:
1354 q = &conn->ibc_tx_queue_rsrvd;
1357 case IBLND_MSG_PUT_NAK:
1358 case IBLND_MSG_PUT_ACK:
1359 case IBLND_MSG_PUT_DONE:
1360 case IBLND_MSG_GET_DONE:
1361 q = &conn->ibc_tx_queue_nocred;
1364 case IBLND_MSG_NOOP:
1365 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1366 q = &conn->ibc_tx_queue_nocred;
1368 q = &conn->ibc_tx_noops;
1371 case IBLND_MSG_IMMEDIATE:
1372 q = &conn->ibc_tx_queue;
1376 list_add_tail(&tx->tx_list, q);
1380 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1382 spin_lock(&conn->ibc_lock);
1383 kiblnd_queue_tx_locked(tx, conn);
1384 kiblnd_check_sends_locked(conn);
1385 spin_unlock(&conn->ibc_lock);
1388 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1389 struct sockaddr_in *srcaddr,
1390 struct sockaddr_in *dstaddr,
1393 unsigned short port;
1396 /* allow the port to be reused */
1397 rc = rdma_set_reuseaddr(cmid, 1);
1399 CERROR("Unable to set reuse on cmid: %d\n", rc);
1403 /* look for a free privileged port */
1404 for (port = PROT_SOCK-1; port > 0; port--) {
1405 srcaddr->sin_port = htons(port);
1406 rc = rdma_resolve_addr(cmid,
1407 (struct sockaddr *)srcaddr,
1408 (struct sockaddr *)dstaddr,
1411 CDEBUG(D_NET, "bound to port %hu\n", port);
1413 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1414 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1421 CERROR("Failed to bind to a free privileged port\n");
1426 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1428 struct rdma_cm_id *cmid;
1429 struct kib_dev *dev;
1430 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1431 struct sockaddr_in srcaddr;
1432 struct sockaddr_in dstaddr;
1435 LASSERT (net != NULL);
1436 LASSERT (peer_ni->ibp_connecting > 0);
1438 cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1439 kiblnd_cm_callback, peer_ni,
1440 RDMA_PS_TCP, IB_QPT_RC);
1443 CERROR("Can't create CMID for %s: %ld\n",
1444 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1450 memset(&srcaddr, 0, sizeof(srcaddr));
1451 srcaddr.sin_family = AF_INET;
1452 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1454 memset(&dstaddr, 0, sizeof(dstaddr));
1455 dstaddr.sin_family = AF_INET;
1456 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1457 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1459 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1461 if (*kiblnd_tunables.kib_use_priv_port) {
1462 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1463 lnet_get_lnd_timeout() * 1000);
1465 rc = rdma_resolve_addr(cmid,
1466 (struct sockaddr *)&srcaddr,
1467 (struct sockaddr *)&dstaddr,
1468 lnet_get_lnd_timeout() * 1000);
1471 /* Can't initiate address resolution: */
1472 CERROR("Can't resolve addr for %s: %d\n",
1473 libcfs_nid2str(peer_ni->ibp_nid), rc);
1480 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1481 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1482 rdma_destroy_id(cmid);
1485 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1489 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1491 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1492 char *reason = NULL;
1493 struct list_head txs;
1494 unsigned long flags;
1496 INIT_LIST_HEAD(&txs);
1498 write_lock_irqsave(glock, flags);
1499 if (peer_ni->ibp_reconnecting == 0) {
1500 if (peer_ni->ibp_accepting)
1501 reason = "accepting";
1502 else if (peer_ni->ibp_connecting)
1503 reason = "connecting";
1504 else if (!list_empty(&peer_ni->ibp_conns))
1505 reason = "connected";
1506 else /* connected then closed */
1512 if (peer_ni->ibp_accepting)
1513 CNETERR("Detecting race between accepting and reconnecting\n");
1514 peer_ni->ibp_reconnecting--;
1516 if (!kiblnd_peer_active(peer_ni)) {
1517 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1518 reason = "unlinked";
1522 peer_ni->ibp_connecting++;
1523 peer_ni->ibp_reconnected++;
1525 write_unlock_irqrestore(glock, flags);
1527 kiblnd_connect_peer(peer_ni);
1531 write_unlock_irqrestore(glock, flags);
1533 CWARN("Abort reconnection of %s: %s\n",
1534 libcfs_nid2str(peer_ni->ibp_nid), reason);
1535 kiblnd_txlist_done(&txs, -ECONNABORTED,
1536 LNET_MSG_STATUS_LOCAL_ABORTED);
1541 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1543 struct kib_peer_ni *peer_ni;
1544 struct kib_peer_ni *peer2;
1545 struct kib_conn *conn;
1546 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1547 unsigned long flags;
1550 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1552 /* If I get here, I've committed to send, so I complete the tx with
1553 * failure on any problems */
1555 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1556 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1558 /* First time, just use a read lock since I expect to find my peer_ni
1560 read_lock_irqsave(g_lock, flags);
1562 peer_ni = kiblnd_find_peer_locked(ni, nid);
1563 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1564 /* Found a peer_ni with an established connection */
1565 conn = kiblnd_get_conn_locked(peer_ni);
1566 kiblnd_conn_addref(conn); /* 1 ref for me... */
1568 read_unlock_irqrestore(g_lock, flags);
1571 kiblnd_queue_tx(tx, conn);
1572 kiblnd_conn_decref(conn); /* ...to here */
1576 read_unlock(g_lock);
1577 /* Re-try with a write lock */
1580 peer_ni = kiblnd_find_peer_locked(ni, nid);
1581 if (peer_ni != NULL) {
1582 if (list_empty(&peer_ni->ibp_conns)) {
1583 /* found a peer_ni, but it's still connecting... */
1584 LASSERT(kiblnd_peer_connecting(peer_ni));
1586 list_add_tail(&tx->tx_list,
1587 &peer_ni->ibp_tx_queue);
1588 write_unlock_irqrestore(g_lock, flags);
1590 conn = kiblnd_get_conn_locked(peer_ni);
1591 kiblnd_conn_addref(conn); /* 1 ref for me... */
1593 write_unlock_irqrestore(g_lock, flags);
1596 kiblnd_queue_tx(tx, conn);
1597 kiblnd_conn_decref(conn); /* ...to here */
1602 write_unlock_irqrestore(g_lock, flags);
1604 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1605 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1607 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1609 tx->tx_status = -EHOSTUNREACH;
1611 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1617 write_lock_irqsave(g_lock, flags);
1619 peer2 = kiblnd_find_peer_locked(ni, nid);
1620 if (peer2 != NULL) {
1621 if (list_empty(&peer2->ibp_conns)) {
1622 /* found a peer_ni, but it's still connecting... */
1623 LASSERT(kiblnd_peer_connecting(peer2));
1625 list_add_tail(&tx->tx_list,
1626 &peer2->ibp_tx_queue);
1627 write_unlock_irqrestore(g_lock, flags);
1629 conn = kiblnd_get_conn_locked(peer2);
1630 kiblnd_conn_addref(conn); /* 1 ref for me... */
1632 write_unlock_irqrestore(g_lock, flags);
1635 kiblnd_queue_tx(tx, conn);
1636 kiblnd_conn_decref(conn); /* ...to here */
1639 kiblnd_peer_decref(peer_ni);
1643 /* Brand new peer_ni */
1644 LASSERT(peer_ni->ibp_connecting == 0);
1645 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1646 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1648 /* always called with a ref on ni, which prevents ni being shutdown */
1649 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1652 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1654 kiblnd_peer_addref(peer_ni);
1655 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
1657 write_unlock_irqrestore(g_lock, flags);
1659 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1660 kiblnd_connect_peer(peer_ni);
1661 kiblnd_peer_decref(peer_ni);
1665 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1667 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1668 int type = lntmsg->msg_type;
1669 struct lnet_process_id target = lntmsg->msg_target;
1670 int target_is_router = lntmsg->msg_target_is_router;
1671 int routing = lntmsg->msg_routing;
1672 unsigned int payload_niov = lntmsg->msg_niov;
1673 struct kvec *payload_iov = lntmsg->msg_iov;
1674 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1675 unsigned int payload_offset = lntmsg->msg_offset;
1676 unsigned int payload_nob = lntmsg->msg_len;
1677 struct kib_msg *ibmsg;
1678 struct kib_rdma_desc *rd;
1683 /* NB 'private' is different depending on what we're sending.... */
1685 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1686 payload_nob, payload_niov, libcfs_id2str(target));
1688 LASSERT (payload_nob == 0 || payload_niov > 0);
1689 LASSERT (payload_niov <= LNET_MAX_IOV);
1691 /* Thread context */
1692 LASSERT (!in_interrupt());
1693 /* payload is either all vaddrs or all pages */
1694 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1702 LASSERT (payload_nob == 0);
1706 if (routing || target_is_router)
1707 break; /* send IMMEDIATE */
1709 /* is the REPLY message too small for RDMA? */
1710 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1711 if (nob <= IBLND_MSG_SIZE)
1712 break; /* send IMMEDIATE */
1714 tx = kiblnd_get_idle_tx(ni, target.nid);
1716 CERROR("Can't allocate txd for GET to %s\n",
1717 libcfs_nid2str(target.nid));
1722 rd = &ibmsg->ibm_u.get.ibgm_rd;
1723 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1724 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1725 lntmsg->msg_md->md_niov,
1726 lntmsg->msg_md->md_iov.iov,
1727 0, lntmsg->msg_md->md_length);
1729 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1730 lntmsg->msg_md->md_niov,
1731 lntmsg->msg_md->md_iov.kiov,
1732 0, lntmsg->msg_md->md_length);
1734 CERROR("Can't setup GET sink for %s: %d\n",
1735 libcfs_nid2str(target.nid), rc);
1736 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1741 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1742 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1743 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1745 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1747 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1748 if (tx->tx_lntmsg[1] == NULL) {
1749 CERROR("Can't create reply for GET -> %s\n",
1750 libcfs_nid2str(target.nid));
1755 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1756 tx->tx_waiting = 1; /* waiting for GET_DONE */
1757 kiblnd_launch_tx(ni, tx, target.nid);
1760 case LNET_MSG_REPLY:
1762 /* Is the payload small enough not to need RDMA? */
1763 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob]);
1764 if (nob <= IBLND_MSG_SIZE)
1765 break; /* send IMMEDIATE */
1767 tx = kiblnd_get_idle_tx(ni, target.nid);
1769 CERROR("Can't allocate %s txd for %s\n",
1770 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1771 libcfs_nid2str(target.nid));
1775 if (payload_kiov == NULL)
1776 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1777 payload_niov, payload_iov,
1778 payload_offset, payload_nob);
1780 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1781 payload_niov, payload_kiov,
1782 payload_offset, payload_nob);
1784 CERROR("Can't setup PUT src for %s: %d\n",
1785 libcfs_nid2str(target.nid), rc);
1791 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1792 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1793 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1794 sizeof(struct kib_putreq_msg));
1796 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1797 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1798 kiblnd_launch_tx(ni, tx, target.nid);
1802 /* send IMMEDIATE */
1803 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1806 tx = kiblnd_get_idle_tx(ni, target.nid);
1808 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1809 type, libcfs_nid2str(target.nid));
1814 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1816 if (payload_kiov != NULL)
1817 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1818 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1819 payload_niov, payload_kiov,
1820 payload_offset, payload_nob);
1822 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1823 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1824 payload_niov, payload_iov,
1825 payload_offset, payload_nob);
1827 nob = offsetof(struct kib_immediate_msg, ibim_payload[payload_nob]);
1828 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1830 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1831 kiblnd_launch_tx(ni, tx, target.nid);
1836 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1838 struct lnet_process_id target = lntmsg->msg_target;
1839 unsigned int niov = lntmsg->msg_niov;
1840 struct kvec *iov = lntmsg->msg_iov;
1841 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1842 unsigned int offset = lntmsg->msg_offset;
1843 unsigned int nob = lntmsg->msg_len;
1847 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1849 CERROR("Can't get tx for REPLY to %s\n",
1850 libcfs_nid2str(target.nid));
1856 else if (kiov == NULL)
1857 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1858 niov, iov, offset, nob);
1860 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1861 niov, kiov, offset, nob);
1864 CERROR("Can't setup GET src for %s: %d\n",
1865 libcfs_nid2str(target.nid), rc);
1869 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1870 IBLND_MSG_GET_DONE, nob,
1871 &rx->rx_msg->ibm_u.get.ibgm_rd,
1872 rx->rx_msg->ibm_u.get.ibgm_cookie);
1874 CERROR("Can't setup rdma for GET from %s: %d\n",
1875 libcfs_nid2str(target.nid), rc);
1880 /* No RDMA: local completion may happen now! */
1881 lnet_finalize(lntmsg, 0);
1883 /* RDMA: lnet_finalize(lntmsg) when it
1885 tx->tx_lntmsg[0] = lntmsg;
1888 kiblnd_queue_tx(tx, rx->rx_conn);
1893 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1896 lnet_finalize(lntmsg, -EIO);
1900 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1901 int delayed, unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1902 unsigned int offset, unsigned int mlen, unsigned int rlen)
1904 struct kib_rx *rx = private;
1905 struct kib_msg *rxmsg = rx->rx_msg;
1906 struct kib_conn *conn = rx->rx_conn;
1910 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1913 LASSERT (mlen <= rlen);
1914 LASSERT (!in_interrupt());
1915 /* Either all pages or all vaddrs */
1916 LASSERT (!(kiov != NULL && iov != NULL));
1918 switch (rxmsg->ibm_type) {
1922 case IBLND_MSG_IMMEDIATE:
1923 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1924 if (nob > rx->rx_nob) {
1925 CERROR ("Immediate message from %s too big: %d(%d)\n",
1926 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1933 lnet_copy_flat2kiov(niov, kiov, offset,
1934 IBLND_MSG_SIZE, rxmsg,
1935 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1938 lnet_copy_flat2iov(niov, iov, offset,
1939 IBLND_MSG_SIZE, rxmsg,
1940 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1942 lnet_finalize(lntmsg, 0);
1945 case IBLND_MSG_PUT_REQ: {
1946 struct kib_msg *txmsg;
1947 struct kib_rdma_desc *rd;
1948 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1951 lnet_finalize(lntmsg, 0);
1952 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1957 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1959 CERROR("Can't allocate tx for %s\n",
1960 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1961 /* Not replying will break the connection */
1967 rd = &txmsg->ibm_u.putack.ibpam_rd;
1969 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1970 niov, iov, offset, mlen);
1972 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1973 niov, kiov, offset, mlen);
1975 CERROR("Can't setup PUT sink for %s: %d\n",
1976 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1977 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1979 /* tell peer_ni it's over */
1980 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1985 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1986 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1987 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1989 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1991 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1992 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1993 kiblnd_queue_tx(tx, conn);
1995 /* reposted buffer reserved for PUT_DONE */
1996 post_credit = IBLND_POSTRX_NO_CREDIT;
2000 case IBLND_MSG_GET_REQ:
2001 if (lntmsg != NULL) {
2002 /* Optimized GET; RDMA lntmsg's payload */
2003 kiblnd_reply(ni, rx, lntmsg);
2005 /* GET didn't match anything */
2006 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
2008 rxmsg->ibm_u.get.ibgm_cookie);
2013 kiblnd_post_rx(rx, post_credit);
2018 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
2020 struct task_struct *task = kthread_run(fn, arg, name);
2023 return PTR_ERR(task);
2025 atomic_inc(&kiblnd_data.kib_nthreads);
2030 kiblnd_thread_fini (void)
2032 atomic_dec (&kiblnd_data.kib_nthreads);
2036 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
2038 /* This is racy, but everyone's only writing ktime_get_seconds() */
2039 peer_ni->ibp_last_alive = ktime_get_seconds();
2044 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
2047 time64_t last_alive = 0;
2048 unsigned long flags;
2050 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2052 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
2053 error = peer_ni->ibp_error;
2054 peer_ni->ibp_error = 0;
2056 last_alive = peer_ni->ibp_last_alive;
2059 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2062 lnet_notify(peer_ni->ibp_ni,
2063 peer_ni->ibp_nid, 0, last_alive);
2067 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
2069 /* This just does the immediate housekeeping. 'error' is zero for a
2070 * normal shutdown which can happen only after the connection has been
2071 * established. If the connection is established, schedule the
2072 * connection to be finished off by the connd. Otherwise the connd is
2073 * already dealing with it (either to set it up or tear it down).
2074 * Caller holds kib_global_lock exclusively in irq context */
2075 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2076 struct kib_dev *dev;
2077 unsigned long flags;
2079 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2081 if (error != 0 && conn->ibc_comms_error == 0)
2082 conn->ibc_comms_error = error;
2084 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2085 return; /* already being handled */
2088 list_empty(&conn->ibc_tx_noops) &&
2089 list_empty(&conn->ibc_tx_queue) &&
2090 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2091 list_empty(&conn->ibc_tx_queue_nocred) &&
2092 list_empty(&conn->ibc_active_txs)) {
2093 CDEBUG(D_NET, "closing conn to %s\n",
2094 libcfs_nid2str(peer_ni->ibp_nid));
2096 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2097 libcfs_nid2str(peer_ni->ibp_nid), error,
2098 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2099 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2100 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2101 "" : "(sending_rsrvd)",
2102 list_empty(&conn->ibc_tx_queue_nocred) ?
2103 "" : "(sending_nocred)",
2104 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2107 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2108 if (peer_ni->ibp_next_conn == conn)
2109 /* clear next_conn so it won't be used */
2110 peer_ni->ibp_next_conn = NULL;
2111 list_del(&conn->ibc_list);
2112 /* connd (see below) takes over ibc_list's ref */
2114 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2115 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2116 kiblnd_unlink_peer_locked(peer_ni);
2118 /* set/clear error on last conn */
2119 peer_ni->ibp_error = conn->ibc_comms_error;
2122 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2125 kiblnd_dev_can_failover(dev)) {
2126 list_add_tail(&dev->ibd_fail_list,
2127 &kiblnd_data.kib_failed_devs);
2128 wake_up(&kiblnd_data.kib_failover_waitq);
2131 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2133 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2134 wake_up(&kiblnd_data.kib_connd_waitq);
2136 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2140 kiblnd_close_conn(struct kib_conn *conn, int error)
2142 unsigned long flags;
2144 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2146 kiblnd_close_conn_locked(conn, error);
2148 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2152 kiblnd_handle_early_rxs(struct kib_conn *conn)
2154 unsigned long flags;
2157 LASSERT(!in_interrupt());
2158 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2160 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2161 while (!list_empty(&conn->ibc_early_rxs)) {
2162 rx = list_entry(conn->ibc_early_rxs.next,
2163 struct kib_rx, rx_list);
2164 list_del(&rx->rx_list);
2165 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2167 kiblnd_handle_rx(rx);
2169 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2171 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2175 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2177 struct list_head zombies = LIST_HEAD_INIT(zombies);
2178 struct list_head *tmp;
2179 struct list_head *nxt;
2182 spin_lock(&conn->ibc_lock);
2184 list_for_each_safe(tmp, nxt, txs) {
2185 tx = list_entry(tmp, struct kib_tx, tx_list);
2187 if (txs == &conn->ibc_active_txs) {
2188 LASSERT(!tx->tx_queued);
2189 LASSERT(tx->tx_waiting ||
2190 tx->tx_sending != 0);
2191 if (conn->ibc_comms_error == -ETIMEDOUT) {
2192 if (tx->tx_waiting && !tx->tx_sending)
2194 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2195 else if (tx->tx_sending)
2197 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2200 LASSERT(tx->tx_queued);
2201 if (conn->ibc_comms_error == -ETIMEDOUT)
2202 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2204 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2207 tx->tx_status = -ECONNABORTED;
2211 * TODO: This makes an assumption that
2212 * kiblnd_tx_complete() will be called for each tx. If
2213 * that event is dropped we could end up with stale
2214 * connections floating around. We'd like to deal with
2215 * that in a better way.
2217 * Also that means we can exceed the timeout by many
2220 if (tx->tx_sending == 0) {
2222 list_del(&tx->tx_list);
2223 list_add(&tx->tx_list, &zombies);
2227 spin_unlock(&conn->ibc_lock);
2230 * aborting transmits occurs when finalizing the connection.
2231 * The connection is finalized on error.
2232 * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2233 * override the value already set in tx->tx_hstatus above.
2235 kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2239 kiblnd_finalise_conn(struct kib_conn *conn)
2241 LASSERT (!in_interrupt());
2242 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2244 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2245 * for connections that didn't get as far as being connected, because
2246 * rdma_disconnect() does this for free. */
2247 kiblnd_abort_receives(conn);
2249 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2251 /* Complete all tx descs not waiting for sends to complete.
2252 * NB we should be safe from RDMA now that the QP has changed state */
2254 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2255 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2256 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2257 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2258 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2260 kiblnd_handle_early_rxs(conn);
2264 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2267 struct list_head zombies = LIST_HEAD_INIT(zombies);
2268 unsigned long flags;
2270 LASSERT (error != 0);
2271 LASSERT (!in_interrupt());
2273 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2276 LASSERT(peer_ni->ibp_connecting > 0);
2277 peer_ni->ibp_connecting--;
2279 LASSERT (peer_ni->ibp_accepting > 0);
2280 peer_ni->ibp_accepting--;
2283 if (kiblnd_peer_connecting(peer_ni)) {
2284 /* another connection attempt under way... */
2285 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2290 peer_ni->ibp_reconnected = 0;
2291 if (list_empty(&peer_ni->ibp_conns)) {
2292 /* Take peer_ni's blocked transmits to complete with error */
2293 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2295 if (kiblnd_peer_active(peer_ni))
2296 kiblnd_unlink_peer_locked(peer_ni);
2298 peer_ni->ibp_error = error;
2300 /* Can't have blocked transmits if there are connections */
2301 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2304 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2306 kiblnd_peer_notify(peer_ni);
2308 if (list_empty(&zombies))
2311 CNETERR("Deleting messages for %s: connection failed\n",
2312 libcfs_nid2str(peer_ni->ibp_nid));
2314 kiblnd_txlist_done(&zombies, error,
2315 LNET_MSG_STATUS_LOCAL_DROPPED);
2319 kiblnd_connreq_done(struct kib_conn *conn, int status)
2321 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2323 struct list_head txs;
2324 unsigned long flags;
2327 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2329 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2330 libcfs_nid2str(peer_ni->ibp_nid), active,
2331 conn->ibc_version, status);
2333 LASSERT (!in_interrupt());
2334 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2335 peer_ni->ibp_connecting > 0) ||
2336 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2337 peer_ni->ibp_accepting > 0));
2339 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2340 conn->ibc_connvars = NULL;
2343 /* failed to establish connection */
2344 kiblnd_peer_connect_failed(peer_ni, active, status);
2345 kiblnd_finalise_conn(conn);
2349 /* connection established */
2350 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2352 conn->ibc_last_send = ktime_get();
2353 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2354 kiblnd_peer_alive(peer_ni);
2356 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2357 * peer_ni instance... */
2358 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2359 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2360 peer_ni->ibp_reconnected = 0;
2362 peer_ni->ibp_connecting--;
2364 peer_ni->ibp_accepting--;
2366 if (peer_ni->ibp_version == 0) {
2367 peer_ni->ibp_version = conn->ibc_version;
2368 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2371 if (peer_ni->ibp_version != conn->ibc_version ||
2372 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2373 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2374 conn->ibc_incarnation);
2375 peer_ni->ibp_version = conn->ibc_version;
2376 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2379 /* grab pending txs while I have the lock */
2380 INIT_LIST_HEAD(&txs);
2381 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2383 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2384 conn->ibc_comms_error != 0) { /* error has happened already */
2386 /* start to shut down connection */
2387 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2388 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2390 kiblnd_txlist_done(&txs, -ECONNABORTED,
2391 LNET_MSG_STATUS_LOCAL_ERROR);
2396 /* +1 ref for myself, this connection is visible to other threads
2397 * now, refcount of peer:ibp_conns can be released by connection
2398 * close from either a different thread, or the calling of
2399 * kiblnd_check_sends_locked() below. See bz21911 for details.
2401 kiblnd_conn_addref(conn);
2402 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2404 /* Schedule blocked txs
2405 * Note: if we are running with conns_per_peer > 1, these blocked
2406 * txs will all get scheduled to the first connection which gets
2407 * scheduled. We won't be using round robin on this first batch.
2409 spin_lock(&conn->ibc_lock);
2410 while (!list_empty(&txs)) {
2411 tx = list_entry(txs.next, struct kib_tx, tx_list);
2412 list_del(&tx->tx_list);
2414 kiblnd_queue_tx_locked(tx, conn);
2416 kiblnd_check_sends_locked(conn);
2417 spin_unlock(&conn->ibc_lock);
2419 /* schedule blocked rxs */
2420 kiblnd_handle_early_rxs(conn);
2421 kiblnd_conn_decref(conn);
2425 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2429 #ifdef HAVE_RDMA_REJECT_4ARGS
2430 rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2432 rc = rdma_reject(cmid, rej, sizeof(*rej));
2436 CWARN("Error %d sending reject\n", rc);
2440 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2442 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2443 struct kib_msg *reqmsg = priv;
2444 struct kib_msg *ackmsg;
2445 struct kib_dev *ibdev;
2446 struct kib_peer_ni *peer_ni;
2447 struct kib_peer_ni *peer2;
2448 struct kib_conn *conn;
2449 struct lnet_ni *ni = NULL;
2450 struct kib_net *net = NULL;
2452 struct rdma_conn_param cp;
2454 int version = IBLND_MSG_VERSION;
2455 unsigned long flags;
2457 struct sockaddr_in *peer_addr;
2458 LASSERT (!in_interrupt());
2460 /* cmid inherits 'context' from the corresponding listener id */
2461 ibdev = cmid->context;
2464 memset(&rej, 0, sizeof(rej));
2465 rej.ibr_magic = IBLND_MSG_MAGIC;
2466 rej.ibr_why = IBLND_REJECT_FATAL;
2467 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2469 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2470 if (*kiblnd_tunables.kib_require_priv_port &&
2471 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2472 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2473 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2474 &ip, ntohs(peer_addr->sin_port));
2478 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2479 CERROR("Short connection request\n");
2483 /* Future protocol version compatibility support! If the
2484 * o2iblnd-specific protocol changes, or when LNET unifies
2485 * protocols over all LNDs, the initial connection will
2486 * negotiate a protocol version. I trap this here to avoid
2487 * console errors; the reject tells the peer_ni which protocol I
2489 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2490 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2492 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2493 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2494 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2496 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2497 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2498 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2501 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2503 CERROR("Can't parse connection request: %d\n", rc);
2507 nid = reqmsg->ibm_srcnid;
2508 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2511 net = (struct kib_net *)ni->ni_data;
2512 rej.ibr_incarnation = net->ibn_incarnation;
2515 if (ni == NULL || /* no matching net */
2516 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2517 net->ibn_dev != ibdev) { /* wrong device */
2518 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2519 "bad dst nid %s\n", libcfs_nid2str(nid),
2520 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2521 ibdev->ibd_ifname, ibdev->ibd_nnets,
2523 libcfs_nid2str(reqmsg->ibm_dstnid));
2528 /* check time stamp as soon as possible */
2529 if (reqmsg->ibm_dststamp != 0 &&
2530 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2531 CWARN("Stale connection request\n");
2532 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2536 /* I can accept peer_ni's version */
2537 version = reqmsg->ibm_version;
2539 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2540 CERROR("Unexpected connreq msg type: %x from %s\n",
2541 reqmsg->ibm_type, libcfs_nid2str(nid));
2545 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2546 kiblnd_msg_queue_size(version, ni)) {
2547 CERROR("Can't accept conn from %s, queue depth too large: "
2548 " %d (<=%d wanted)\n",
2549 libcfs_nid2str(nid),
2550 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2551 kiblnd_msg_queue_size(version, ni));
2553 if (version == IBLND_MSG_VERSION)
2554 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2559 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2560 IBLND_MAX_RDMA_FRAGS) {
2561 CWARN("Can't accept conn from %s (version %x): "
2562 "max_frags %d too large (%d wanted)\n",
2563 libcfs_nid2str(nid), version,
2564 reqmsg->ibm_u.connparams.ibcp_max_frags,
2565 IBLND_MAX_RDMA_FRAGS);
2567 if (version >= IBLND_MSG_VERSION)
2568 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2571 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2572 IBLND_MAX_RDMA_FRAGS &&
2573 net->ibn_fmr_ps == NULL) {
2574 CWARN("Can't accept conn from %s (version %x): "
2575 "max_frags %d incompatible without FMR pool "
2577 libcfs_nid2str(nid), version,
2578 reqmsg->ibm_u.connparams.ibcp_max_frags,
2579 IBLND_MAX_RDMA_FRAGS);
2581 if (version == IBLND_MSG_VERSION)
2582 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2587 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2588 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2589 libcfs_nid2str(nid),
2590 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2595 /* assume 'nid' is a new peer_ni; create */
2596 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2598 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2599 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2603 /* We have validated the peer's parameters so use those */
2604 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2605 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2607 write_lock_irqsave(g_lock, flags);
2609 peer2 = kiblnd_find_peer_locked(ni, nid);
2610 if (peer2 != NULL) {
2611 if (peer2->ibp_version == 0) {
2612 peer2->ibp_version = version;
2613 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2616 /* not the guy I've talked with */
2617 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2618 peer2->ibp_version != version) {
2619 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2621 if (kiblnd_peer_active(peer2)) {
2622 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2623 peer2->ibp_version = version;
2625 write_unlock_irqrestore(g_lock, flags);
2627 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2628 libcfs_nid2str(nid), peer2->ibp_version, version,
2629 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2631 kiblnd_peer_decref(peer_ni);
2632 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2636 /* Tie-break connection race in favour of the higher NID.
2637 * If we keep running into a race condition multiple times,
2638 * we have to assume that the connection attempt with the
2639 * higher NID is stuck in a connecting state and will never
2640 * recover. As such, we pass through this if-block and let
2641 * the lower NID connection win so we can move forward.
2643 if (peer2->ibp_connecting != 0 &&
2644 nid < ni->ni_nid && peer2->ibp_races <
2645 MAX_CONN_RACES_BEFORE_ABORT) {
2647 write_unlock_irqrestore(g_lock, flags);
2649 CDEBUG(D_NET, "Conn race %s\n",
2650 libcfs_nid2str(peer2->ibp_nid));
2652 kiblnd_peer_decref(peer_ni);
2653 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2656 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2657 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2658 libcfs_nid2str(peer2->ibp_nid),
2659 MAX_CONN_RACES_BEFORE_ABORT);
2661 * passive connection is allowed even this peer_ni is waiting for
2664 peer2->ibp_reconnecting = 0;
2665 peer2->ibp_races = 0;
2666 peer2->ibp_accepting++;
2667 kiblnd_peer_addref(peer2);
2669 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2670 * so copy validated parameters since we now know what the
2671 * peer_ni's limits are */
2672 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2673 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2675 write_unlock_irqrestore(g_lock, flags);
2676 kiblnd_peer_decref(peer_ni);
2679 /* Brand new peer_ni */
2680 LASSERT (peer_ni->ibp_accepting == 0);
2681 LASSERT (peer_ni->ibp_version == 0 &&
2682 peer_ni->ibp_incarnation == 0);
2684 peer_ni->ibp_accepting = 1;
2685 peer_ni->ibp_version = version;
2686 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2688 /* I have a ref on ni that prevents it being shutdown */
2689 LASSERT (net->ibn_shutdown == 0);
2691 kiblnd_peer_addref(peer_ni);
2692 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
2694 write_unlock_irqrestore(g_lock, flags);
2697 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2699 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2700 kiblnd_peer_decref(peer_ni);
2701 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2705 /* conn now "owns" cmid, so I return success from here on to ensure the
2706 * CM callback doesn't destroy cmid. */
2707 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2708 conn->ibc_credits = conn->ibc_queue_depth;
2709 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2710 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2711 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2713 ackmsg = &conn->ibc_connvars->cv_msg;
2714 memset(ackmsg, 0, sizeof(*ackmsg));
2716 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2717 sizeof(ackmsg->ibm_u.connparams));
2718 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2719 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2720 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2722 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2724 memset(&cp, 0, sizeof(cp));
2725 cp.private_data = ackmsg;
2726 cp.private_data_len = ackmsg->ibm_nob;
2727 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2728 cp.initiator_depth = 0;
2729 cp.flow_control = 1;
2730 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2731 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2733 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2735 rc = rdma_accept(cmid, &cp);
2737 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2738 rej.ibr_version = version;
2739 rej.ibr_why = IBLND_REJECT_FATAL;
2741 kiblnd_reject(cmid, &rej);
2742 kiblnd_connreq_done(conn, rc);
2743 kiblnd_conn_decref(conn);
2751 rej.ibr_cp.ibcp_queue_depth =
2752 kiblnd_msg_queue_size(version, ni);
2753 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2757 rej.ibr_version = version;
2758 kiblnd_reject(cmid, &rej);
2760 return -ECONNREFUSED;
2764 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2765 u64 incarnation, int why, struct kib_connparams *cp)
2767 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2768 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2770 int msg_size = IBLND_MSG_SIZE;
2774 unsigned long flags;
2776 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2777 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2780 msg_size = cp->ibcp_max_msg_size;
2781 frag_num = cp->ibcp_max_frags;
2782 queue_dep = cp->ibcp_queue_depth;
2785 write_lock_irqsave(glock, flags);
2786 /* retry connection if it's still needed and no other connection
2787 * attempts (active or passive) are in progress
2788 * NB: reconnect is still needed even when ibp_tx_queue is
2789 * empty if ibp_version != version because reconnect may be
2790 * initiated by kiblnd_query() */
2791 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2792 peer_ni->ibp_version != version) &&
2793 peer_ni->ibp_connecting &&
2794 peer_ni->ibp_accepting == 0;
2805 case IBLND_REJECT_RDMA_FRAGS: {
2806 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2809 reason = "can't negotiate max frags";
2812 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2813 #ifdef HAVE_IB_GET_DMA_MR
2815 * This check only makes sense if the kernel supports global
2816 * memory registration. Otherwise, map_on_demand will never == 0
2818 if (!tunables->lnd_map_on_demand) {
2819 reason = "map_on_demand must be enabled";
2823 if (conn->ibc_max_frags <= frag_num) {
2824 reason = "unsupported max frags";
2828 peer_ni->ibp_max_frags = frag_num;
2829 reason = "rdma fragments";
2832 case IBLND_REJECT_MSG_QUEUE_SIZE:
2834 reason = "can't negotiate queue depth";
2837 if (conn->ibc_queue_depth <= queue_dep) {
2838 reason = "unsupported queue depth";
2842 peer_ni->ibp_queue_depth = queue_dep;
2843 reason = "queue depth";
2846 case IBLND_REJECT_CONN_STALE:
2850 case IBLND_REJECT_CONN_RACE:
2851 reason = "conn race";
2854 case IBLND_REJECT_CONN_UNCOMPAT:
2855 reason = "version negotiation";
2859 conn->ibc_reconnect = 1;
2860 peer_ni->ibp_reconnecting++;
2861 peer_ni->ibp_version = version;
2862 if (incarnation != 0)
2863 peer_ni->ibp_incarnation = incarnation;
2865 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2867 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2868 libcfs_nid2str(peer_ni->ibp_nid),
2869 reconnect ? "reconnect" : "don't reconnect",
2870 reason, IBLND_MSG_VERSION, version, msg_size,
2871 conn->ibc_queue_depth, queue_dep,
2872 conn->ibc_max_frags, frag_num);
2874 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2875 * while destroying the zombie
2880 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2882 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2883 int status = -ECONNREFUSED;
2885 LASSERT (!in_interrupt());
2886 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2889 case IB_CM_REJ_STALE_CONN:
2890 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2891 IBLND_REJECT_CONN_STALE, NULL);
2894 case IB_CM_REJ_INVALID_SERVICE_ID:
2895 status = -EHOSTUNREACH;
2896 CNETERR("%s rejected: no listener at %d\n",
2897 libcfs_nid2str(peer_ni->ibp_nid),
2898 *kiblnd_tunables.kib_service);
2901 case IB_CM_REJ_CONSUMER_DEFINED:
2902 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2903 struct kib_rej *rej = priv;
2904 struct kib_connparams *cp = NULL;
2906 __u64 incarnation = -1;
2908 /* NB. default incarnation is -1 because:
2909 * a) V1 will ignore dst incarnation in connreq.
2910 * b) V2 will provide incarnation while rejecting me,
2911 * -1 will be overwrote.
2913 * if I try to connect to a V1 peer_ni with V2 protocol,
2914 * it rejected me then upgrade to V2, I have no idea
2915 * about the upgrading and try to reconnect with V1,
2916 * in this case upgraded V2 can find out I'm trying to
2917 * talk to the old guy and reject me(incarnation is -1).
2920 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2921 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2922 __swab32s(&rej->ibr_magic);
2923 __swab16s(&rej->ibr_version);
2927 if (priv_nob >= sizeof(struct kib_rej) &&
2928 rej->ibr_version > IBLND_MSG_VERSION_1) {
2929 /* priv_nob is always 148 in current version
2930 * of OFED, so we still need to check version.
2931 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2935 __swab64s(&rej->ibr_incarnation);
2936 __swab16s(&cp->ibcp_queue_depth);
2937 __swab16s(&cp->ibcp_max_frags);
2938 __swab32s(&cp->ibcp_max_msg_size);
2941 incarnation = rej->ibr_incarnation;
2944 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2945 rej->ibr_magic != LNET_PROTO_MAGIC) {
2946 CERROR("%s rejected: consumer defined fatal error\n",
2947 libcfs_nid2str(peer_ni->ibp_nid));
2951 if (rej->ibr_version != IBLND_MSG_VERSION &&
2952 rej->ibr_version != IBLND_MSG_VERSION_1) {
2953 CERROR("%s rejected: o2iblnd version %x error\n",
2954 libcfs_nid2str(peer_ni->ibp_nid),
2959 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2960 rej->ibr_version == IBLND_MSG_VERSION_1) {
2961 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2962 libcfs_nid2str(peer_ni->ibp_nid), rej->ibr_version);
2964 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2965 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2968 switch (rej->ibr_why) {
2969 case IBLND_REJECT_CONN_RACE:
2970 case IBLND_REJECT_CONN_STALE:
2971 case IBLND_REJECT_CONN_UNCOMPAT:
2972 case IBLND_REJECT_MSG_QUEUE_SIZE:
2973 case IBLND_REJECT_RDMA_FRAGS:
2974 kiblnd_check_reconnect(conn, rej->ibr_version,
2975 incarnation, rej->ibr_why, cp);
2978 case IBLND_REJECT_NO_RESOURCES:
2979 CERROR("%s rejected: o2iblnd no resources\n",
2980 libcfs_nid2str(peer_ni->ibp_nid));
2983 case IBLND_REJECT_FATAL:
2984 CERROR("%s rejected: o2iblnd fatal error\n",
2985 libcfs_nid2str(peer_ni->ibp_nid));
2989 CERROR("%s rejected: o2iblnd reason %d\n",
2990 libcfs_nid2str(peer_ni->ibp_nid),
2998 CNETERR("%s rejected: reason %d, size %d\n",
2999 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
3003 kiblnd_connreq_done(conn, status);
3007 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
3009 struct kib_peer_ni *peer_ni = conn->ibc_peer;
3010 struct lnet_ni *ni = peer_ni->ibp_ni;
3011 struct kib_net *net = ni->ni_data;
3012 struct kib_msg *msg = priv;
3013 int ver = conn->ibc_version;
3014 int rc = kiblnd_unpack_msg(msg, priv_nob);
3015 unsigned long flags;
3017 LASSERT (net != NULL);
3020 CERROR("Can't unpack connack from %s: %d\n",
3021 libcfs_nid2str(peer_ni->ibp_nid), rc);
3025 if (msg->ibm_type != IBLND_MSG_CONNACK) {
3026 CERROR("Unexpected message %d from %s\n",
3027 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
3032 if (ver != msg->ibm_version) {
3033 CERROR("%s replied version %x is different with "
3034 "requested version %x\n",
3035 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
3040 if (msg->ibm_u.connparams.ibcp_queue_depth >
3041 conn->ibc_queue_depth) {
3042 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
3043 libcfs_nid2str(peer_ni->ibp_nid),
3044 msg->ibm_u.connparams.ibcp_queue_depth,
3045 conn->ibc_queue_depth);
3050 if (msg->ibm_u.connparams.ibcp_max_frags >
3051 conn->ibc_max_frags) {
3052 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3053 libcfs_nid2str(peer_ni->ibp_nid),
3054 msg->ibm_u.connparams.ibcp_max_frags,
3055 conn->ibc_max_frags);
3060 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3061 CERROR("%s max message size %d too big (%d max)\n",
3062 libcfs_nid2str(peer_ni->ibp_nid),
3063 msg->ibm_u.connparams.ibcp_max_msg_size,
3069 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3070 if (msg->ibm_dstnid == ni->ni_nid &&
3071 msg->ibm_dststamp == net->ibn_incarnation)
3075 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3078 CERROR("Bad connection reply from %s, rc = %d, "
3079 "version: %x max_frags: %d\n",
3080 libcfs_nid2str(peer_ni->ibp_nid), rc,
3081 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3085 conn->ibc_incarnation = msg->ibm_srcstamp;
3086 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3087 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3088 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3089 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3090 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3091 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3093 kiblnd_connreq_done(conn, 0);
3097 /* NB My QP has already established itself, so I handle anything going
3098 * wrong here by setting ibc_comms_error.
3099 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3100 * immediately tears it down. */
3103 conn->ibc_comms_error = rc;
3104 kiblnd_connreq_done(conn, 0);
3108 kiblnd_active_connect(struct rdma_cm_id *cmid)
3110 struct kib_peer_ni *peer_ni = cmid->context;
3111 struct kib_conn *conn;
3112 struct kib_msg *msg;
3113 struct rdma_conn_param cp;
3116 unsigned long flags;
3119 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3121 incarnation = peer_ni->ibp_incarnation;
3122 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3123 peer_ni->ibp_version;
3125 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3127 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3130 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3131 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3135 /* conn "owns" cmid now, so I return success from here on to ensure the
3136 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3139 msg = &conn->ibc_connvars->cv_msg;
3141 memset(msg, 0, sizeof(*msg));
3142 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3143 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3144 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3145 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3147 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3148 0, peer_ni->ibp_nid, incarnation);
3150 memset(&cp, 0, sizeof(cp));
3151 cp.private_data = msg;
3152 cp.private_data_len = msg->ibm_nob;
3153 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3154 cp.initiator_depth = 0;
3155 cp.flow_control = 1;
3156 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3157 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3159 LASSERT(cmid->context == (void *)conn);
3160 LASSERT(conn->ibc_cmid == cmid);
3161 rc = rdma_connect_locked(cmid, &cp);
3163 CERROR("Can't connect to %s: %d\n",
3164 libcfs_nid2str(peer_ni->ibp_nid), rc);
3165 kiblnd_connreq_done(conn, rc);
3166 kiblnd_conn_decref(conn);
3173 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3175 struct kib_peer_ni *peer_ni;
3176 struct kib_conn *conn;
3179 switch (event->event) {
3181 CERROR("Unexpected event: %d, status: %d\n",
3182 event->event, event->status);
3185 case RDMA_CM_EVENT_CONNECT_REQUEST:
3186 /* destroy cmid on failure */
3187 rc = kiblnd_passive_connect(cmid,
3188 (void *)KIBLND_CONN_PARAM(event),
3189 KIBLND_CONN_PARAM_LEN(event));
3190 CDEBUG(D_NET, "connreq: %d\n", rc);
3193 case RDMA_CM_EVENT_ADDR_ERROR:
3194 peer_ni = cmid->context;
3195 CNETERR("%s: ADDR ERROR %d\n",
3196 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3197 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3198 kiblnd_peer_decref(peer_ni);
3199 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3201 case RDMA_CM_EVENT_ADDR_RESOLVED:
3202 peer_ni = cmid->context;
3204 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3205 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3207 if (event->status != 0) {
3208 CNETERR("Can't resolve address for %s: %d\n",
3209 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3212 rc = rdma_resolve_route(
3213 cmid, lnet_get_lnd_timeout() * 1000);
3215 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3216 struct kib_dev *dev = net->ibn_dev;
3218 CDEBUG(D_NET, "%s: connection bound to "\
3220 libcfs_nid2str(peer_ni->ibp_nid),
3222 &dev->ibd_ifip, cmid->device->name);
3227 /* Can't initiate route resolution */
3228 CERROR("Can't resolve route for %s: %d\n",
3229 libcfs_nid2str(peer_ni->ibp_nid), rc);
3231 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3232 kiblnd_peer_decref(peer_ni);
3233 return rc; /* rc != 0 destroys cmid */
3235 case RDMA_CM_EVENT_ROUTE_ERROR:
3236 peer_ni = cmid->context;
3237 CNETERR("%s: ROUTE ERROR %d\n",
3238 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3239 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3240 kiblnd_peer_decref(peer_ni);
3241 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3243 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3244 peer_ni = cmid->context;
3245 CDEBUG(D_NET,"%s Route resolved: %d\n",
3246 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3248 if (event->status == 0)
3249 return kiblnd_active_connect(cmid);
3251 CNETERR("Can't resolve route for %s: %d\n",
3252 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3253 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3254 kiblnd_peer_decref(peer_ni);
3255 return event->status; /* rc != 0 destroys cmid */
3257 case RDMA_CM_EVENT_UNREACHABLE:
3258 conn = cmid->context;
3259 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3260 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3261 CNETERR("%s: UNREACHABLE %d\n",
3262 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3263 kiblnd_connreq_done(conn, -ENETDOWN);
3264 kiblnd_conn_decref(conn);
3267 case RDMA_CM_EVENT_CONNECT_ERROR:
3268 conn = cmid->context;
3269 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3270 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3271 CNETERR("%s: CONNECT ERROR %d\n",
3272 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3273 kiblnd_connreq_done(conn, -ENOTCONN);
3274 kiblnd_conn_decref(conn);
3277 case RDMA_CM_EVENT_REJECTED:
3278 conn = cmid->context;
3279 switch (conn->ibc_state) {
3283 case IBLND_CONN_PASSIVE_WAIT:
3284 CERROR ("%s: REJECTED %d\n",
3285 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3287 kiblnd_connreq_done(conn, -ECONNRESET);
3290 case IBLND_CONN_ACTIVE_CONNECT:
3291 kiblnd_rejected(conn, event->status,
3292 (void *)KIBLND_CONN_PARAM(event),
3293 KIBLND_CONN_PARAM_LEN(event));
3296 kiblnd_conn_decref(conn);
3299 case RDMA_CM_EVENT_ESTABLISHED:
3300 conn = cmid->context;
3301 switch (conn->ibc_state) {
3305 case IBLND_CONN_PASSIVE_WAIT:
3306 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3307 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3308 kiblnd_connreq_done(conn, 0);
3311 case IBLND_CONN_ACTIVE_CONNECT:
3312 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3313 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3314 kiblnd_check_connreply(conn,
3315 (void *)KIBLND_CONN_PARAM(event),
3316 KIBLND_CONN_PARAM_LEN(event));
3319 /* net keeps its ref on conn! */
3322 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3323 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3326 case RDMA_CM_EVENT_DISCONNECTED:
3327 conn = cmid->context;
3328 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3329 CERROR("%s DISCONNECTED\n",
3330 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3331 kiblnd_connreq_done(conn, -ECONNRESET);
3333 kiblnd_close_conn(conn, 0);
3335 kiblnd_conn_decref(conn);
3336 cmid->context = NULL;
3339 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3340 LCONSOLE_ERROR_MSG(0x131,
3341 "Received notification of device removal\n"
3342 "Please shutdown LNET to allow this to proceed\n");
3343 /* Can't remove network from underneath LNET for now, so I have
3347 case RDMA_CM_EVENT_ADDR_CHANGE:
3348 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3354 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3357 struct list_head *ttmp;
3359 list_for_each(ttmp, txs) {
3360 tx = list_entry(ttmp, struct kib_tx, tx_list);
3362 if (txs != &conn->ibc_active_txs) {
3363 LASSERT(tx->tx_queued);
3365 LASSERT(!tx->tx_queued);
3366 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3369 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3370 CERROR("Timed out tx: %s, %lld seconds\n",
3371 kiblnd_queue2str(conn, txs),
3372 ktime_ms_delta(ktime_get(),
3373 tx->tx_deadline) / MSEC_PER_SEC);
3382 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3384 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3385 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3386 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3387 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3388 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3392 kiblnd_check_conns (int idx)
3394 struct list_head closes = LIST_HEAD_INIT(closes);
3395 struct list_head checksends = LIST_HEAD_INIT(checksends);
3396 struct list_head timedout_txs = LIST_HEAD_INIT(timedout_txs);
3397 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3398 struct list_head *ptmp;
3399 struct kib_peer_ni *peer_ni;
3400 struct kib_conn *conn;
3401 struct kib_tx *tx, *tx_tmp;
3402 struct list_head *ctmp;
3403 unsigned long flags;
3405 /* NB. We expect to have a look at all the peers and not find any
3406 * RDMAs to time out, so we just use a shared lock while we
3408 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3410 list_for_each(ptmp, peers) {
3411 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
3413 /* Check tx_deadline */
3414 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3415 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3416 CWARN("Timed out tx for %s: %lld seconds\n",
3417 libcfs_nid2str(peer_ni->ibp_nid),
3418 ktime_ms_delta(ktime_get(),
3419 tx->tx_deadline) / MSEC_PER_SEC);
3420 list_move(&tx->tx_list, &timedout_txs);
3424 list_for_each(ctmp, &peer_ni->ibp_conns) {
3428 conn = list_entry(ctmp, struct kib_conn, ibc_list);
3430 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3432 spin_lock(&conn->ibc_lock);
3434 sendnoop = kiblnd_need_noop(conn);
3435 timedout = kiblnd_conn_timed_out_locked(conn);
3436 if (!sendnoop && !timedout) {
3437 spin_unlock(&conn->ibc_lock);
3442 CERROR("Timed out RDMA with %s (%lld): "
3443 "c: %u, oc: %u, rc: %u\n",
3444 libcfs_nid2str(peer_ni->ibp_nid),
3445 ktime_get_seconds() - peer_ni->ibp_last_alive,
3447 conn->ibc_outstanding_credits,
3448 conn->ibc_reserved_credits);
3449 list_add(&conn->ibc_connd_list, &closes);
3451 list_add(&conn->ibc_connd_list, &checksends);
3453 /* +ref for 'closes' or 'checksends' */
3454 kiblnd_conn_addref(conn);
3456 spin_unlock(&conn->ibc_lock);
3460 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3462 if (!list_empty(&timedout_txs))
3463 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3464 LNET_MSG_STATUS_LOCAL_TIMEOUT);
3466 /* Handle timeout by closing the whole
3467 * connection. We can only be sure RDMA activity
3468 * has ceased once the QP has been modified. */
3469 while (!list_empty(&closes)) {
3470 conn = list_entry(closes.next,
3471 struct kib_conn, ibc_connd_list);
3472 list_del(&conn->ibc_connd_list);
3473 kiblnd_close_conn(conn, -ETIMEDOUT);
3474 kiblnd_conn_decref(conn);
3477 /* In case we have enough credits to return via a
3478 * NOOP, but there were no non-blocking tx descs
3479 * free to do it last time... */
3480 while (!list_empty(&checksends)) {
3481 conn = list_entry(checksends.next,
3482 struct kib_conn, ibc_connd_list);
3483 list_del(&conn->ibc_connd_list);
3485 spin_lock(&conn->ibc_lock);
3486 kiblnd_check_sends_locked(conn);
3487 spin_unlock(&conn->ibc_lock);
3489 kiblnd_conn_decref(conn);
3494 kiblnd_disconnect_conn(struct kib_conn *conn)
3496 LASSERT (!in_interrupt());
3497 LASSERT (current == kiblnd_data.kib_connd);
3498 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3500 rdma_disconnect(conn->ibc_cmid);
3501 kiblnd_finalise_conn(conn);
3503 kiblnd_peer_notify(conn->ibc_peer);
3507 * High-water for reconnection to the same peer_ni, reconnection attempt should
3508 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3510 #define KIB_RECONN_HIGH_RACE 10
3512 * Allow connd to take a break and handle other things after consecutive
3513 * reconnection attemps.
3515 #define KIB_RECONN_BREAK 100
3518 kiblnd_connd (void *arg)
3520 spinlock_t *lock= &kiblnd_data.kib_connd_lock;
3521 wait_queue_entry_t wait;
3522 unsigned long flags;
3523 struct kib_conn *conn;
3528 unsigned long deadline = jiffies;
3530 cfs_block_allsigs();
3532 init_waitqueue_entry(&wait, current);
3533 kiblnd_data.kib_connd = current;
3535 spin_lock_irqsave(lock, flags);
3537 while (!kiblnd_data.kib_shutdown) {
3542 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3543 struct kib_peer_ni *peer_ni = NULL;
3545 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3546 struct kib_conn, ibc_list);
3547 list_del(&conn->ibc_list);
3548 if (conn->ibc_reconnect) {
3549 peer_ni = conn->ibc_peer;
3550 kiblnd_peer_addref(peer_ni);
3553 spin_unlock_irqrestore(lock, flags);
3556 kiblnd_destroy_conn(conn);
3558 spin_lock_irqsave(lock, flags);
3560 LIBCFS_FREE(conn, sizeof(*conn));
3564 conn->ibc_peer = peer_ni;
3565 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3566 list_add_tail(&conn->ibc_list,
3567 &kiblnd_data.kib_reconn_list);
3569 list_add_tail(&conn->ibc_list,
3570 &kiblnd_data.kib_reconn_wait);
3573 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3574 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3575 struct kib_conn, ibc_list);
3576 list_del(&conn->ibc_list);
3578 spin_unlock_irqrestore(lock, flags);
3581 kiblnd_disconnect_conn(conn);
3582 kiblnd_conn_decref(conn);
3584 spin_lock_irqsave(lock, flags);
3587 while (reconn < KIB_RECONN_BREAK) {
3588 if (kiblnd_data.kib_reconn_sec !=
3589 ktime_get_real_seconds()) {
3590 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3591 list_splice_init(&kiblnd_data.kib_reconn_wait,
3592 &kiblnd_data.kib_reconn_list);
3595 if (list_empty(&kiblnd_data.kib_reconn_list))
3598 conn = list_entry(kiblnd_data.kib_reconn_list.next,
3599 struct kib_conn, ibc_list);
3600 list_del(&conn->ibc_list);
3602 spin_unlock_irqrestore(lock, flags);
3605 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3606 kiblnd_peer_decref(conn->ibc_peer);
3607 LIBCFS_FREE(conn, sizeof(*conn));
3609 spin_lock_irqsave(lock, flags);
3612 /* careful with the jiffy wrap... */
3613 timeout = (int)(deadline - jiffies);
3617 int chunk = kiblnd_data.kib_peer_hash_size;
3618 unsigned int lnd_timeout;
3620 spin_unlock_irqrestore(lock, flags);
3623 /* Time to check for RDMA timeouts on a few more
3624 * peers: I do checks every 'p' seconds on a
3625 * proportion of the peer_ni table and I need to check
3626 * every connection 'n' times within a timeout
3627 * interval, to ensure I detect a timeout on any
3628 * connection within (n+1)/n times the timeout
3631 lnd_timeout = lnet_get_lnd_timeout();
3632 if (lnd_timeout > n * p)
3633 chunk = (chunk * n * p) / lnd_timeout;
3637 for (i = 0; i < chunk; i++) {
3638 kiblnd_check_conns(peer_index);
3639 peer_index = (peer_index + 1) %
3640 kiblnd_data.kib_peer_hash_size;
3643 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3644 spin_lock_irqsave(lock, flags);
3650 /* Nothing to do for 'timeout' */
3651 set_current_state(TASK_INTERRUPTIBLE);
3652 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3653 spin_unlock_irqrestore(lock, flags);
3655 schedule_timeout(timeout);
3657 set_current_state(TASK_RUNNING);
3658 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3659 spin_lock_irqsave(lock, flags);
3662 spin_unlock_irqrestore(lock, flags);
3664 kiblnd_thread_fini();
3669 kiblnd_qp_event(struct ib_event *event, void *arg)
3671 struct kib_conn *conn = arg;
3673 switch (event->event) {
3674 case IB_EVENT_COMM_EST:
3675 CDEBUG(D_NET, "%s established\n",
3676 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3677 /* We received a packet but connection isn't established
3678 * probably handshake packet was lost, so free to
3679 * force make connection established */
3680 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3683 case IB_EVENT_PORT_ERR:
3684 case IB_EVENT_DEVICE_FATAL:
3685 CERROR("Fatal device error for NI %s\n",
3686 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3687 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3690 case IB_EVENT_PORT_ACTIVE:
3691 CERROR("Port reactivated for NI %s\n",
3692 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3693 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3697 CERROR("%s: Async QP event type %d\n",
3698 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3704 kiblnd_complete (struct ib_wc *wc)
3706 switch (kiblnd_wreqid2type(wc->wr_id)) {
3711 if (wc->status != IB_WC_SUCCESS &&
3712 wc->status != IB_WC_WR_FLUSH_ERR)
3713 CNETERR("FastReg failed: %d\n", wc->status);
3716 case IBLND_WID_RDMA:
3717 /* We only get RDMA completion notification if it fails. All
3718 * subsequent work items, including the final SEND will fail
3719 * too. However we can't print out any more info about the
3720 * failing RDMA because 'tx' might be back on the idle list or
3721 * even reused already if we didn't manage to post all our work
3723 CNETERR("RDMA (tx: %p) failed: %d\n",
3724 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3728 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3732 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3739 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3741 /* NB I'm not allowed to schedule this conn once its refcount has
3742 * reached 0. Since fundamentally I'm racing with scheduler threads
3743 * consuming my CQ I could be called after all completions have
3744 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3745 * and this CQ is about to be destroyed so I NOOP. */
3746 struct kib_conn *conn = arg;
3747 struct kib_sched_info *sched = conn->ibc_sched;
3748 unsigned long flags;
3750 LASSERT(cq == conn->ibc_cq);
3752 spin_lock_irqsave(&sched->ibs_lock, flags);
3754 conn->ibc_ready = 1;
3756 if (!conn->ibc_scheduled &&
3757 (conn->ibc_nrx > 0 ||
3758 conn->ibc_nsends_posted > 0)) {
3759 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3760 conn->ibc_scheduled = 1;
3761 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3763 if (waitqueue_active(&sched->ibs_waitq))
3764 wake_up(&sched->ibs_waitq);
3767 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3771 kiblnd_cq_event(struct ib_event *event, void *arg)
3773 struct kib_conn *conn = arg;
3775 CERROR("%s: async CQ event type %d\n",
3776 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3780 kiblnd_scheduler(void *arg)
3782 long id = (long)arg;
3783 struct kib_sched_info *sched;
3784 struct kib_conn *conn;
3785 wait_queue_entry_t wait;
3786 unsigned long flags;
3792 cfs_block_allsigs();
3794 init_waitqueue_entry(&wait, current);
3796 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3798 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3800 CWARN("Unable to bind on CPU partition %d, please verify "
3801 "whether all CPUs are healthy and reload modules if "
3802 "necessary, otherwise your system might under risk of "
3803 "low performance\n", sched->ibs_cpt);
3806 spin_lock_irqsave(&sched->ibs_lock, flags);
3808 while (!kiblnd_data.kib_shutdown) {
3809 if (busy_loops++ >= IBLND_RESCHED) {
3810 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3815 spin_lock_irqsave(&sched->ibs_lock, flags);
3820 if (!list_empty(&sched->ibs_conns)) {
3821 conn = list_entry(sched->ibs_conns.next,
3822 struct kib_conn, ibc_sched_list);
3823 /* take over kib_sched_conns' ref on conn... */
3824 LASSERT(conn->ibc_scheduled);
3825 list_del(&conn->ibc_sched_list);
3826 conn->ibc_ready = 0;
3828 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3830 wc.wr_id = IBLND_WID_INVAL;
3832 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3834 rc = ib_req_notify_cq(conn->ibc_cq,
3837 CWARN("%s: ib_req_notify_cq failed: %d, "
3838 "closing connection\n",
3839 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3840 kiblnd_close_conn(conn, -EIO);
3841 kiblnd_conn_decref(conn);
3842 spin_lock_irqsave(&sched->ibs_lock,
3847 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3850 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3852 "ib_poll_cq (rc: %d) returned invalid "
3853 "wr_id, opcode %d, status: %d, "
3854 "vendor_err: %d, conn: %s status: %d\n"
3855 "please upgrade firmware and OFED or "
3856 "contact vendor.\n", rc,
3857 wc.opcode, wc.status, wc.vendor_err,
3858 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3864 CWARN("%s: ib_poll_cq failed: %d, "
3865 "closing connection\n",
3866 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3868 kiblnd_close_conn(conn, -EIO);
3869 kiblnd_conn_decref(conn);
3870 spin_lock_irqsave(&sched->ibs_lock, flags);
3874 spin_lock_irqsave(&sched->ibs_lock, flags);
3876 if (rc != 0 || conn->ibc_ready) {
3877 /* There may be another completion waiting; get
3878 * another scheduler to check while I handle
3880 /* +1 ref for sched_conns */
3881 kiblnd_conn_addref(conn);
3882 list_add_tail(&conn->ibc_sched_list,
3884 if (waitqueue_active(&sched->ibs_waitq))
3885 wake_up(&sched->ibs_waitq);
3887 conn->ibc_scheduled = 0;
3891 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3892 kiblnd_complete(&wc);
3894 spin_lock_irqsave(&sched->ibs_lock, flags);
3897 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3904 set_current_state(TASK_INTERRUPTIBLE);
3905 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3906 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3911 remove_wait_queue(&sched->ibs_waitq, &wait);
3912 set_current_state(TASK_RUNNING);
3913 spin_lock_irqsave(&sched->ibs_lock, flags);
3916 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3918 kiblnd_thread_fini();
3923 kiblnd_failover_thread(void *arg)
3925 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3926 struct kib_dev *dev;
3927 struct net *ns = arg;
3928 wait_queue_entry_t wait;
3929 unsigned long flags;
3932 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3934 cfs_block_allsigs();
3936 init_waitqueue_entry(&wait, current);
3937 write_lock_irqsave(glock, flags);
3939 while (!kiblnd_data.kib_shutdown) {
3940 int do_failover = 0;
3943 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3945 if (ktime_get_seconds() < dev->ibd_next_failover)
3952 list_del_init(&dev->ibd_fail_list);
3953 dev->ibd_failover = 1;
3954 write_unlock_irqrestore(glock, flags);
3956 rc = kiblnd_dev_failover(dev, ns);
3958 write_lock_irqsave(glock, flags);
3960 LASSERT (dev->ibd_failover);
3961 dev->ibd_failover = 0;
3962 if (rc >= 0) { /* Device is OK or failover succeed */
3963 dev->ibd_next_failover = ktime_get_seconds() + 3;
3967 /* failed to failover, retry later */
3968 dev->ibd_next_failover = ktime_get_seconds() +
3969 min(dev->ibd_failed_failover, 10);
3970 if (kiblnd_dev_can_failover(dev)) {
3971 list_add_tail(&dev->ibd_fail_list,
3972 &kiblnd_data.kib_failed_devs);
3978 /* long sleep if no more pending failover */
3979 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3981 set_current_state(TASK_INTERRUPTIBLE);
3982 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3983 write_unlock_irqrestore(glock, flags);
3985 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3986 cfs_time_seconds(1));
3987 set_current_state(TASK_RUNNING);
3988 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3989 write_lock_irqsave(glock, flags);
3991 if (!long_sleep || rc != 0)
3994 /* have a long sleep, routine check all active devices,
3995 * we need checking like this because if there is not active
3996 * connection on the dev and no SEND from local, we may listen
3997 * on wrong HCA for ever while there is a bonding failover */
3998 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3999 if (kiblnd_dev_can_failover(dev)) {
4000 list_add_tail(&dev->ibd_fail_list,
4001 &kiblnd_data.kib_failed_devs);
4006 write_unlock_irqrestore(glock, flags);
4008 kiblnd_thread_fini();