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 int ver = conn->ibc_version;
895 LASSERT(tx->tx_queued);
896 /* We rely on this for QP sizing */
897 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
898 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
900 LASSERT(credit == 0 || credit == 1);
901 LASSERT(conn->ibc_outstanding_credits >= 0);
902 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
903 LASSERT(conn->ibc_credits >= 0);
904 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
906 if (conn->ibc_nsends_posted ==
907 kiblnd_concurrent_sends(ver, ni)) {
908 /* tx completions outstanding... */
909 CDEBUG(D_NET, "%s: posted enough\n",
910 libcfs_nid2str(peer_ni->ibp_nid));
914 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
915 CDEBUG(D_NET, "%s: no credits\n",
916 libcfs_nid2str(peer_ni->ibp_nid));
920 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
921 conn->ibc_credits == 1 && /* last credit reserved */
922 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
923 CDEBUG(D_NET, "%s: not using last credit\n",
924 libcfs_nid2str(peer_ni->ibp_nid));
928 /* NB don't drop ibc_lock before bumping tx_sending */
929 list_del(&tx->tx_list);
932 if (msg->ibm_type == IBLND_MSG_NOOP &&
933 (!kiblnd_need_noop(conn) || /* redundant NOOP */
934 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
935 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
936 /* OK to drop when posted enough NOOPs, since
937 * kiblnd_check_sends_locked will queue NOOP again when
938 * posted NOOPs complete */
939 spin_unlock(&conn->ibc_lock);
940 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
942 spin_lock(&conn->ibc_lock);
943 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
944 libcfs_nid2str(peer_ni->ibp_nid),
945 conn->ibc_noops_posted);
949 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
950 peer_ni->ibp_nid, conn->ibc_incarnation);
952 conn->ibc_credits -= credit;
953 conn->ibc_outstanding_credits = 0;
954 conn->ibc_nsends_posted++;
955 if (msg->ibm_type == IBLND_MSG_NOOP)
956 conn->ibc_noops_posted++;
958 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
959 * PUT. If so, it was first queued here as a PUT_REQ, sent and
960 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
961 * and then re-queued here. It's (just) possible that
962 * tx_sending is non-zero if we've not done the tx_complete()
963 * from the first send; hence the ++ rather than = below. */
965 list_add(&tx->tx_list, &conn->ibc_active_txs);
967 /* I'm still holding ibc_lock! */
968 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
970 } else if (tx->tx_pool->tpo_pool.po_failed ||
971 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
972 /* close_conn will launch failover */
975 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
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;
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();
1011 /* NB credits are transferred in the actual
1012 * message, which can only be the last work item */
1013 conn->ibc_credits += credit;
1014 conn->ibc_outstanding_credits += msg->ibm_credits;
1015 conn->ibc_nsends_posted--;
1016 if (msg->ibm_type == IBLND_MSG_NOOP)
1017 conn->ibc_noops_posted--;
1023 done = (tx->tx_sending == 0);
1025 list_del(&tx->tx_list);
1027 spin_unlock(&conn->ibc_lock);
1029 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1030 CERROR("Error %d posting transmit to %s\n",
1031 rc, libcfs_nid2str(peer_ni->ibp_nid));
1033 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1034 rc, libcfs_nid2str(peer_ni->ibp_nid));
1036 kiblnd_close_conn(conn, rc);
1041 spin_lock(&conn->ibc_lock);
1047 kiblnd_check_sends_locked(struct kib_conn *conn)
1049 int ver = conn->ibc_version;
1050 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
1053 /* Don't send anything until after the connection is established */
1054 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
1055 CDEBUG(D_NET, "%s too soon\n",
1056 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1060 LASSERT(conn->ibc_nsends_posted <=
1061 kiblnd_concurrent_sends(ver, ni));
1062 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
1063 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
1064 LASSERT (conn->ibc_reserved_credits >= 0);
1066 while (conn->ibc_reserved_credits > 0 &&
1067 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
1068 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
1069 struct kib_tx, tx_list);
1070 list_del(&tx->tx_list);
1071 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
1072 conn->ibc_reserved_credits--;
1075 if (kiblnd_need_noop(conn)) {
1076 spin_unlock(&conn->ibc_lock);
1078 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1080 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1082 spin_lock(&conn->ibc_lock);
1084 kiblnd_queue_tx_locked(tx, conn);
1090 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1092 tx = list_entry(conn->ibc_tx_queue_nocred.next,
1093 struct kib_tx, tx_list);
1094 } else if (!list_empty(&conn->ibc_tx_noops)) {
1095 LASSERT (!IBLND_OOB_CAPABLE(ver));
1097 tx = list_entry(conn->ibc_tx_noops.next,
1098 struct kib_tx, tx_list);
1099 } else if (!list_empty(&conn->ibc_tx_queue)) {
1101 tx = list_entry(conn->ibc_tx_queue.next,
1102 struct kib_tx, tx_list);
1106 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1112 kiblnd_tx_complete(struct kib_tx *tx, int status)
1114 int failed = (status != IB_WC_SUCCESS);
1115 struct kib_conn *conn = tx->tx_conn;
1118 if (tx->tx_sending <= 0) {
1119 CERROR("Received an event on a freed tx: %p status %d\n",
1125 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1126 CNETERR("Tx -> %s cookie %#llx"
1127 " sending %d waiting %d: failed %d\n",
1128 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1129 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1132 kiblnd_close_conn(conn, -EIO);
1134 kiblnd_peer_alive(conn->ibc_peer);
1137 spin_lock(&conn->ibc_lock);
1139 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1140 * gets to free it, which also drops its ref on 'conn'. */
1143 conn->ibc_nsends_posted--;
1144 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1145 conn->ibc_noops_posted--;
1148 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1149 tx->tx_waiting = 0; /* don't wait for peer_ni */
1150 tx->tx_status = -EIO;
1153 idle = (tx->tx_sending == 0) && /* This is the final callback */
1154 !tx->tx_waiting && /* Not waiting for peer_ni */
1155 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1157 list_del(&tx->tx_list);
1159 kiblnd_check_sends_locked(conn);
1160 spin_unlock(&conn->ibc_lock);
1167 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1170 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1171 struct ib_sge *sge = &tx->tx_msgsge;
1172 struct ib_rdma_wr *wrq;
1173 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1174 #ifdef HAVE_IB_GET_DMA_MR
1175 struct ib_mr *mr = hdev->ibh_mrs;
1178 LASSERT(tx->tx_nwrq >= 0);
1179 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1180 LASSERT(nob <= IBLND_MSG_SIZE);
1181 #ifdef HAVE_IB_GET_DMA_MR
1182 LASSERT(mr != NULL);
1185 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1187 #ifdef HAVE_IB_GET_DMA_MR
1188 sge->lkey = mr->lkey;
1190 sge->lkey = hdev->ibh_pd->local_dma_lkey;
1192 sge->addr = tx->tx_msgaddr;
1195 wrq = &tx->tx_wrq[tx->tx_nwrq];
1196 memset(wrq, 0, sizeof(*wrq));
1198 wrq->wr.next = NULL;
1199 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1200 wrq->wr.sg_list = sge;
1201 wrq->wr.num_sge = 1;
1202 wrq->wr.opcode = IB_WR_SEND;
1203 wrq->wr.send_flags = IB_SEND_SIGNALED;
1209 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1210 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1212 struct kib_msg *ibmsg = tx->tx_msg;
1213 struct kib_rdma_desc *srcrd = tx->tx_rd;
1214 struct ib_rdma_wr *wrq = NULL;
1222 LASSERT(!in_interrupt());
1223 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1224 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1226 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1227 resid > 0; resid -= sge_nob) {
1230 if (srcidx >= srcrd->rd_nfrags) {
1231 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1236 if (dstidx >= dstrd->rd_nfrags) {
1237 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1242 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1243 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1244 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1245 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1246 conn->ibc_max_frags,
1247 srcidx, srcrd->rd_nfrags,
1248 dstidx, dstrd->rd_nfrags);
1253 sge_nob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1254 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1256 sge = &tx->tx_sge[tx->tx_nsge];
1257 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1258 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1259 sge->length = sge_nob;
1262 wrq = &tx->tx_wrq[tx->tx_nwrq];
1264 wrq->wr.next = &(wrq + 1)->wr;
1265 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1266 wrq->wr.sg_list = sge;
1267 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1268 wrq->wr.send_flags = 0;
1270 #ifdef HAVE_IB_RDMA_WR
1271 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1273 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1276 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1278 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1283 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1284 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1287 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1289 wrq->wr.num_sge = wrq_sge;
1295 if (rc < 0) /* no RDMA if completing with failure */
1296 tx->tx_nwrq = tx->tx_nsge = 0;
1298 ibmsg->ibm_u.completion.ibcm_status = rc;
1299 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1300 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1301 type, sizeof(struct kib_completion_msg));
1307 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1309 struct list_head *q;
1312 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1313 LASSERT(!tx->tx_queued); /* not queued for sending already */
1314 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1316 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1317 tx->tx_status = -ECONNABORTED;
1319 if (tx->tx_conn != NULL) {
1320 /* PUT_DONE first attached to conn as a PUT_REQ */
1321 LASSERT(tx->tx_conn == conn);
1322 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1324 kiblnd_conn_decref(conn);
1326 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1331 timeout_ns = lnet_get_lnd_timeout() * NSEC_PER_SEC;
1333 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1335 if (tx->tx_conn == NULL) {
1336 kiblnd_conn_addref(conn);
1338 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1340 /* PUT_DONE first attached to conn as a PUT_REQ */
1341 LASSERT (tx->tx_conn == conn);
1342 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1345 switch (tx->tx_msg->ibm_type) {
1349 case IBLND_MSG_PUT_REQ:
1350 case IBLND_MSG_GET_REQ:
1351 q = &conn->ibc_tx_queue_rsrvd;
1354 case IBLND_MSG_PUT_NAK:
1355 case IBLND_MSG_PUT_ACK:
1356 case IBLND_MSG_PUT_DONE:
1357 case IBLND_MSG_GET_DONE:
1358 q = &conn->ibc_tx_queue_nocred;
1361 case IBLND_MSG_NOOP:
1362 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1363 q = &conn->ibc_tx_queue_nocred;
1365 q = &conn->ibc_tx_noops;
1368 case IBLND_MSG_IMMEDIATE:
1369 q = &conn->ibc_tx_queue;
1373 list_add_tail(&tx->tx_list, q);
1377 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1379 spin_lock(&conn->ibc_lock);
1380 kiblnd_queue_tx_locked(tx, conn);
1381 kiblnd_check_sends_locked(conn);
1382 spin_unlock(&conn->ibc_lock);
1385 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1386 struct sockaddr_in *srcaddr,
1387 struct sockaddr_in *dstaddr,
1390 unsigned short port;
1393 /* allow the port to be reused */
1394 rc = rdma_set_reuseaddr(cmid, 1);
1396 CERROR("Unable to set reuse on cmid: %d\n", rc);
1400 /* look for a free privileged port */
1401 for (port = PROT_SOCK-1; port > 0; port--) {
1402 srcaddr->sin_port = htons(port);
1403 rc = rdma_resolve_addr(cmid,
1404 (struct sockaddr *)srcaddr,
1405 (struct sockaddr *)dstaddr,
1408 CDEBUG(D_NET, "bound to port %hu\n", port);
1410 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1411 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1418 CERROR("Failed to bind to a free privileged port\n");
1423 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1425 struct rdma_cm_id *cmid;
1426 struct kib_dev *dev;
1427 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1428 struct sockaddr_in srcaddr;
1429 struct sockaddr_in dstaddr;
1432 LASSERT (net != NULL);
1433 LASSERT (peer_ni->ibp_connecting > 0);
1435 cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1436 kiblnd_cm_callback, peer_ni,
1437 RDMA_PS_TCP, IB_QPT_RC);
1440 CERROR("Can't create CMID for %s: %ld\n",
1441 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1447 memset(&srcaddr, 0, sizeof(srcaddr));
1448 srcaddr.sin_family = AF_INET;
1449 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1451 memset(&dstaddr, 0, sizeof(dstaddr));
1452 dstaddr.sin_family = AF_INET;
1453 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1454 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1456 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1458 if (*kiblnd_tunables.kib_use_priv_port) {
1459 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1460 lnet_get_lnd_timeout() * 1000);
1462 rc = rdma_resolve_addr(cmid,
1463 (struct sockaddr *)&srcaddr,
1464 (struct sockaddr *)&dstaddr,
1465 lnet_get_lnd_timeout() * 1000);
1468 /* Can't initiate address resolution: */
1469 CERROR("Can't resolve addr for %s: %d\n",
1470 libcfs_nid2str(peer_ni->ibp_nid), rc);
1477 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1478 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1479 rdma_destroy_id(cmid);
1482 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1486 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1488 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1489 char *reason = NULL;
1490 struct list_head txs;
1491 unsigned long flags;
1493 INIT_LIST_HEAD(&txs);
1495 write_lock_irqsave(glock, flags);
1496 if (peer_ni->ibp_reconnecting == 0) {
1497 if (peer_ni->ibp_accepting)
1498 reason = "accepting";
1499 else if (peer_ni->ibp_connecting)
1500 reason = "connecting";
1501 else if (!list_empty(&peer_ni->ibp_conns))
1502 reason = "connected";
1503 else /* connected then closed */
1509 if (peer_ni->ibp_accepting)
1510 CNETERR("Detecting race between accepting and reconnecting\n");
1511 peer_ni->ibp_reconnecting--;
1513 if (!kiblnd_peer_active(peer_ni)) {
1514 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1515 reason = "unlinked";
1519 peer_ni->ibp_connecting++;
1520 peer_ni->ibp_reconnected++;
1522 write_unlock_irqrestore(glock, flags);
1524 kiblnd_connect_peer(peer_ni);
1528 write_unlock_irqrestore(glock, flags);
1530 CWARN("Abort reconnection of %s: %s\n",
1531 libcfs_nid2str(peer_ni->ibp_nid), reason);
1532 kiblnd_txlist_done(&txs, -ECONNABORTED,
1533 LNET_MSG_STATUS_LOCAL_ABORTED);
1538 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1540 struct kib_peer_ni *peer_ni;
1541 struct kib_peer_ni *peer2;
1542 struct kib_conn *conn;
1543 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1544 unsigned long flags;
1547 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1549 /* If I get here, I've committed to send, so I complete the tx with
1550 * failure on any problems */
1552 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1553 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1555 /* First time, just use a read lock since I expect to find my peer_ni
1557 read_lock_irqsave(g_lock, flags);
1559 peer_ni = kiblnd_find_peer_locked(ni, nid);
1560 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1561 /* Found a peer_ni with an established connection */
1562 conn = kiblnd_get_conn_locked(peer_ni);
1563 kiblnd_conn_addref(conn); /* 1 ref for me... */
1565 read_unlock_irqrestore(g_lock, flags);
1568 kiblnd_queue_tx(tx, conn);
1569 kiblnd_conn_decref(conn); /* ...to here */
1573 read_unlock(g_lock);
1574 /* Re-try with a write lock */
1577 peer_ni = kiblnd_find_peer_locked(ni, nid);
1578 if (peer_ni != NULL) {
1579 if (list_empty(&peer_ni->ibp_conns)) {
1580 /* found a peer_ni, but it's still connecting... */
1581 LASSERT(kiblnd_peer_connecting(peer_ni));
1583 list_add_tail(&tx->tx_list,
1584 &peer_ni->ibp_tx_queue);
1585 write_unlock_irqrestore(g_lock, flags);
1587 conn = kiblnd_get_conn_locked(peer_ni);
1588 kiblnd_conn_addref(conn); /* 1 ref for me... */
1590 write_unlock_irqrestore(g_lock, flags);
1593 kiblnd_queue_tx(tx, conn);
1594 kiblnd_conn_decref(conn); /* ...to here */
1599 write_unlock_irqrestore(g_lock, flags);
1601 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1602 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1604 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1606 tx->tx_status = -EHOSTUNREACH;
1608 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1614 write_lock_irqsave(g_lock, flags);
1616 peer2 = kiblnd_find_peer_locked(ni, nid);
1617 if (peer2 != NULL) {
1618 if (list_empty(&peer2->ibp_conns)) {
1619 /* found a peer_ni, but it's still connecting... */
1620 LASSERT(kiblnd_peer_connecting(peer2));
1622 list_add_tail(&tx->tx_list,
1623 &peer2->ibp_tx_queue);
1624 write_unlock_irqrestore(g_lock, flags);
1626 conn = kiblnd_get_conn_locked(peer2);
1627 kiblnd_conn_addref(conn); /* 1 ref for me... */
1629 write_unlock_irqrestore(g_lock, flags);
1632 kiblnd_queue_tx(tx, conn);
1633 kiblnd_conn_decref(conn); /* ...to here */
1636 kiblnd_peer_decref(peer_ni);
1640 /* Brand new peer_ni */
1641 LASSERT(peer_ni->ibp_connecting == 0);
1642 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1643 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1645 /* always called with a ref on ni, which prevents ni being shutdown */
1646 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1649 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1651 kiblnd_peer_addref(peer_ni);
1652 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
1654 write_unlock_irqrestore(g_lock, flags);
1656 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1657 kiblnd_connect_peer(peer_ni);
1658 kiblnd_peer_decref(peer_ni);
1662 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1664 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1665 int type = lntmsg->msg_type;
1666 struct lnet_process_id target = lntmsg->msg_target;
1667 int target_is_router = lntmsg->msg_target_is_router;
1668 int routing = lntmsg->msg_routing;
1669 unsigned int payload_niov = lntmsg->msg_niov;
1670 struct kvec *payload_iov = lntmsg->msg_iov;
1671 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1672 unsigned int payload_offset = lntmsg->msg_offset;
1673 unsigned int payload_nob = lntmsg->msg_len;
1674 struct kib_msg *ibmsg;
1675 struct kib_rdma_desc *rd;
1680 /* NB 'private' is different depending on what we're sending.... */
1682 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1683 payload_nob, payload_niov, libcfs_id2str(target));
1685 LASSERT (payload_nob == 0 || payload_niov > 0);
1686 LASSERT (payload_niov <= LNET_MAX_IOV);
1688 /* Thread context */
1689 LASSERT (!in_interrupt());
1690 /* payload is either all vaddrs or all pages */
1691 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1699 LASSERT (payload_nob == 0);
1703 if (routing || target_is_router)
1704 break; /* send IMMEDIATE */
1706 /* is the REPLY message too small for RDMA? */
1707 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1708 if (nob <= IBLND_MSG_SIZE)
1709 break; /* send IMMEDIATE */
1711 tx = kiblnd_get_idle_tx(ni, target.nid);
1713 CERROR("Can't allocate txd for GET to %s\n",
1714 libcfs_nid2str(target.nid));
1719 rd = &ibmsg->ibm_u.get.ibgm_rd;
1720 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1721 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1722 lntmsg->msg_md->md_niov,
1723 lntmsg->msg_md->md_iov.iov,
1724 0, lntmsg->msg_md->md_length);
1726 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1727 lntmsg->msg_md->md_niov,
1728 lntmsg->msg_md->md_iov.kiov,
1729 0, lntmsg->msg_md->md_length);
1731 CERROR("Can't setup GET sink for %s: %d\n",
1732 libcfs_nid2str(target.nid), rc);
1733 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1738 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1739 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1740 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1742 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1744 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1745 if (tx->tx_lntmsg[1] == NULL) {
1746 CERROR("Can't create reply for GET -> %s\n",
1747 libcfs_nid2str(target.nid));
1752 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1753 tx->tx_waiting = 1; /* waiting for GET_DONE */
1754 kiblnd_launch_tx(ni, tx, target.nid);
1757 case LNET_MSG_REPLY:
1759 /* Is the payload small enough not to need RDMA? */
1760 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob]);
1761 if (nob <= IBLND_MSG_SIZE)
1762 break; /* send IMMEDIATE */
1764 tx = kiblnd_get_idle_tx(ni, target.nid);
1766 CERROR("Can't allocate %s txd for %s\n",
1767 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1768 libcfs_nid2str(target.nid));
1772 if (payload_kiov == NULL)
1773 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1774 payload_niov, payload_iov,
1775 payload_offset, payload_nob);
1777 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1778 payload_niov, payload_kiov,
1779 payload_offset, payload_nob);
1781 CERROR("Can't setup PUT src for %s: %d\n",
1782 libcfs_nid2str(target.nid), rc);
1788 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1789 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1790 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1791 sizeof(struct kib_putreq_msg));
1793 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1794 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1795 kiblnd_launch_tx(ni, tx, target.nid);
1799 /* send IMMEDIATE */
1800 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1803 tx = kiblnd_get_idle_tx(ni, target.nid);
1805 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1806 type, libcfs_nid2str(target.nid));
1811 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1813 if (payload_kiov != NULL)
1814 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1815 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1816 payload_niov, payload_kiov,
1817 payload_offset, payload_nob);
1819 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1820 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1821 payload_niov, payload_iov,
1822 payload_offset, payload_nob);
1824 nob = offsetof(struct kib_immediate_msg, ibim_payload[payload_nob]);
1825 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1827 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1828 kiblnd_launch_tx(ni, tx, target.nid);
1833 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1835 struct lnet_process_id target = lntmsg->msg_target;
1836 unsigned int niov = lntmsg->msg_niov;
1837 struct kvec *iov = lntmsg->msg_iov;
1838 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1839 unsigned int offset = lntmsg->msg_offset;
1840 unsigned int nob = lntmsg->msg_len;
1844 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1846 CERROR("Can't get tx for REPLY to %s\n",
1847 libcfs_nid2str(target.nid));
1853 else if (kiov == NULL)
1854 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1855 niov, iov, offset, nob);
1857 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1858 niov, kiov, offset, nob);
1861 CERROR("Can't setup GET src for %s: %d\n",
1862 libcfs_nid2str(target.nid), rc);
1866 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1867 IBLND_MSG_GET_DONE, nob,
1868 &rx->rx_msg->ibm_u.get.ibgm_rd,
1869 rx->rx_msg->ibm_u.get.ibgm_cookie);
1871 CERROR("Can't setup rdma for GET from %s: %d\n",
1872 libcfs_nid2str(target.nid), rc);
1877 /* No RDMA: local completion may happen now! */
1878 lnet_finalize(lntmsg, 0);
1880 /* RDMA: lnet_finalize(lntmsg) when it
1882 tx->tx_lntmsg[0] = lntmsg;
1885 kiblnd_queue_tx(tx, rx->rx_conn);
1890 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1893 lnet_finalize(lntmsg, -EIO);
1897 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1898 int delayed, unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1899 unsigned int offset, unsigned int mlen, unsigned int rlen)
1901 struct kib_rx *rx = private;
1902 struct kib_msg *rxmsg = rx->rx_msg;
1903 struct kib_conn *conn = rx->rx_conn;
1907 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1910 LASSERT (mlen <= rlen);
1911 LASSERT (!in_interrupt());
1912 /* Either all pages or all vaddrs */
1913 LASSERT (!(kiov != NULL && iov != NULL));
1915 switch (rxmsg->ibm_type) {
1919 case IBLND_MSG_IMMEDIATE:
1920 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1921 if (nob > rx->rx_nob) {
1922 CERROR ("Immediate message from %s too big: %d(%d)\n",
1923 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1930 lnet_copy_flat2kiov(niov, kiov, offset,
1931 IBLND_MSG_SIZE, rxmsg,
1932 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1935 lnet_copy_flat2iov(niov, iov, offset,
1936 IBLND_MSG_SIZE, rxmsg,
1937 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1939 lnet_finalize(lntmsg, 0);
1942 case IBLND_MSG_PUT_REQ: {
1943 struct kib_msg *txmsg;
1944 struct kib_rdma_desc *rd;
1945 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1948 lnet_finalize(lntmsg, 0);
1949 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1954 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1956 CERROR("Can't allocate tx for %s\n",
1957 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1958 /* Not replying will break the connection */
1964 rd = &txmsg->ibm_u.putack.ibpam_rd;
1966 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1967 niov, iov, offset, mlen);
1969 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1970 niov, kiov, offset, mlen);
1972 CERROR("Can't setup PUT sink for %s: %d\n",
1973 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1974 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1976 /* tell peer_ni it's over */
1977 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1982 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1983 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1984 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1986 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1988 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1989 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1990 kiblnd_queue_tx(tx, conn);
1992 /* reposted buffer reserved for PUT_DONE */
1993 post_credit = IBLND_POSTRX_NO_CREDIT;
1997 case IBLND_MSG_GET_REQ:
1998 if (lntmsg != NULL) {
1999 /* Optimized GET; RDMA lntmsg's payload */
2000 kiblnd_reply(ni, rx, lntmsg);
2002 /* GET didn't match anything */
2003 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
2005 rxmsg->ibm_u.get.ibgm_cookie);
2010 kiblnd_post_rx(rx, post_credit);
2015 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
2017 struct task_struct *task = kthread_run(fn, arg, name);
2020 return PTR_ERR(task);
2022 atomic_inc(&kiblnd_data.kib_nthreads);
2027 kiblnd_thread_fini (void)
2029 atomic_dec (&kiblnd_data.kib_nthreads);
2033 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
2035 /* This is racy, but everyone's only writing ktime_get_seconds() */
2036 peer_ni->ibp_last_alive = ktime_get_seconds();
2041 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
2044 time64_t last_alive = 0;
2045 unsigned long flags;
2047 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2049 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
2050 error = peer_ni->ibp_error;
2051 peer_ni->ibp_error = 0;
2053 last_alive = peer_ni->ibp_last_alive;
2056 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2059 lnet_notify(peer_ni->ibp_ni,
2060 peer_ni->ibp_nid, 0, last_alive);
2064 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
2066 /* This just does the immediate housekeeping. 'error' is zero for a
2067 * normal shutdown which can happen only after the connection has been
2068 * established. If the connection is established, schedule the
2069 * connection to be finished off by the connd. Otherwise the connd is
2070 * already dealing with it (either to set it up or tear it down).
2071 * Caller holds kib_global_lock exclusively in irq context */
2072 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2073 struct kib_dev *dev;
2074 unsigned long flags;
2076 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2078 if (error != 0 && conn->ibc_comms_error == 0)
2079 conn->ibc_comms_error = error;
2081 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2082 return; /* already being handled */
2085 list_empty(&conn->ibc_tx_noops) &&
2086 list_empty(&conn->ibc_tx_queue) &&
2087 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2088 list_empty(&conn->ibc_tx_queue_nocred) &&
2089 list_empty(&conn->ibc_active_txs)) {
2090 CDEBUG(D_NET, "closing conn to %s\n",
2091 libcfs_nid2str(peer_ni->ibp_nid));
2093 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2094 libcfs_nid2str(peer_ni->ibp_nid), error,
2095 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2096 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2097 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2098 "" : "(sending_rsrvd)",
2099 list_empty(&conn->ibc_tx_queue_nocred) ?
2100 "" : "(sending_nocred)",
2101 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2104 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2105 if (peer_ni->ibp_next_conn == conn)
2106 /* clear next_conn so it won't be used */
2107 peer_ni->ibp_next_conn = NULL;
2108 list_del(&conn->ibc_list);
2109 /* connd (see below) takes over ibc_list's ref */
2111 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2112 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2113 kiblnd_unlink_peer_locked(peer_ni);
2115 /* set/clear error on last conn */
2116 peer_ni->ibp_error = conn->ibc_comms_error;
2119 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2122 kiblnd_dev_can_failover(dev)) {
2123 list_add_tail(&dev->ibd_fail_list,
2124 &kiblnd_data.kib_failed_devs);
2125 wake_up(&kiblnd_data.kib_failover_waitq);
2128 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2130 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2131 wake_up(&kiblnd_data.kib_connd_waitq);
2133 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2137 kiblnd_close_conn(struct kib_conn *conn, int error)
2139 unsigned long flags;
2141 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2143 kiblnd_close_conn_locked(conn, error);
2145 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2149 kiblnd_handle_early_rxs(struct kib_conn *conn)
2151 unsigned long flags;
2154 LASSERT(!in_interrupt());
2155 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2157 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2158 while (!list_empty(&conn->ibc_early_rxs)) {
2159 rx = list_entry(conn->ibc_early_rxs.next,
2160 struct kib_rx, rx_list);
2161 list_del(&rx->rx_list);
2162 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2164 kiblnd_handle_rx(rx);
2166 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2168 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2172 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2174 struct list_head zombies = LIST_HEAD_INIT(zombies);
2175 struct list_head *tmp;
2176 struct list_head *nxt;
2179 spin_lock(&conn->ibc_lock);
2181 list_for_each_safe(tmp, nxt, txs) {
2182 tx = list_entry(tmp, struct kib_tx, tx_list);
2184 if (txs == &conn->ibc_active_txs) {
2185 LASSERT(!tx->tx_queued);
2186 LASSERT(tx->tx_waiting ||
2187 tx->tx_sending != 0);
2188 if (conn->ibc_comms_error == -ETIMEDOUT) {
2189 if (tx->tx_waiting && !tx->tx_sending)
2191 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2192 else if (tx->tx_sending)
2194 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2197 LASSERT(tx->tx_queued);
2198 if (conn->ibc_comms_error == -ETIMEDOUT)
2199 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2201 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2204 tx->tx_status = -ECONNABORTED;
2208 * TODO: This makes an assumption that
2209 * kiblnd_tx_complete() will be called for each tx. If
2210 * that event is dropped we could end up with stale
2211 * connections floating around. We'd like to deal with
2212 * that in a better way.
2214 * Also that means we can exceed the timeout by many
2217 if (tx->tx_sending == 0) {
2219 list_del(&tx->tx_list);
2220 list_add(&tx->tx_list, &zombies);
2224 spin_unlock(&conn->ibc_lock);
2227 * aborting transmits occurs when finalizing the connection.
2228 * The connection is finalized on error.
2229 * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2230 * override the value already set in tx->tx_hstatus above.
2232 kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2236 kiblnd_finalise_conn(struct kib_conn *conn)
2238 LASSERT (!in_interrupt());
2239 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2241 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2242 * for connections that didn't get as far as being connected, because
2243 * rdma_disconnect() does this for free. */
2244 kiblnd_abort_receives(conn);
2246 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2248 /* Complete all tx descs not waiting for sends to complete.
2249 * NB we should be safe from RDMA now that the QP has changed state */
2251 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2252 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2253 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2254 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2255 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2257 kiblnd_handle_early_rxs(conn);
2261 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2264 struct list_head zombies = LIST_HEAD_INIT(zombies);
2265 unsigned long flags;
2267 LASSERT (error != 0);
2268 LASSERT (!in_interrupt());
2270 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2273 LASSERT(peer_ni->ibp_connecting > 0);
2274 peer_ni->ibp_connecting--;
2276 LASSERT (peer_ni->ibp_accepting > 0);
2277 peer_ni->ibp_accepting--;
2280 if (kiblnd_peer_connecting(peer_ni)) {
2281 /* another connection attempt under way... */
2282 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2287 peer_ni->ibp_reconnected = 0;
2288 if (list_empty(&peer_ni->ibp_conns)) {
2289 /* Take peer_ni's blocked transmits to complete with error */
2290 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2292 if (kiblnd_peer_active(peer_ni))
2293 kiblnd_unlink_peer_locked(peer_ni);
2295 peer_ni->ibp_error = error;
2297 /* Can't have blocked transmits if there are connections */
2298 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2301 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2303 kiblnd_peer_notify(peer_ni);
2305 if (list_empty(&zombies))
2308 CNETERR("Deleting messages for %s: connection failed\n",
2309 libcfs_nid2str(peer_ni->ibp_nid));
2311 kiblnd_txlist_done(&zombies, error,
2312 LNET_MSG_STATUS_LOCAL_DROPPED);
2316 kiblnd_connreq_done(struct kib_conn *conn, int status)
2318 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2320 struct list_head txs;
2321 unsigned long flags;
2324 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2326 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2327 libcfs_nid2str(peer_ni->ibp_nid), active,
2328 conn->ibc_version, status);
2330 LASSERT (!in_interrupt());
2331 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2332 peer_ni->ibp_connecting > 0) ||
2333 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2334 peer_ni->ibp_accepting > 0));
2336 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2337 conn->ibc_connvars = NULL;
2340 /* failed to establish connection */
2341 kiblnd_peer_connect_failed(peer_ni, active, status);
2342 kiblnd_finalise_conn(conn);
2346 /* connection established */
2347 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2349 conn->ibc_last_send = ktime_get();
2350 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2351 kiblnd_peer_alive(peer_ni);
2353 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2354 * peer_ni instance... */
2355 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2356 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2357 peer_ni->ibp_reconnected = 0;
2359 peer_ni->ibp_connecting--;
2361 peer_ni->ibp_accepting--;
2363 if (peer_ni->ibp_version == 0) {
2364 peer_ni->ibp_version = conn->ibc_version;
2365 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2368 if (peer_ni->ibp_version != conn->ibc_version ||
2369 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2370 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2371 conn->ibc_incarnation);
2372 peer_ni->ibp_version = conn->ibc_version;
2373 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2376 /* grab pending txs while I have the lock */
2377 INIT_LIST_HEAD(&txs);
2378 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2380 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2381 conn->ibc_comms_error != 0) { /* error has happened already */
2383 /* start to shut down connection */
2384 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2385 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2387 kiblnd_txlist_done(&txs, -ECONNABORTED,
2388 LNET_MSG_STATUS_LOCAL_ERROR);
2393 /* +1 ref for myself, this connection is visible to other threads
2394 * now, refcount of peer:ibp_conns can be released by connection
2395 * close from either a different thread, or the calling of
2396 * kiblnd_check_sends_locked() below. See bz21911 for details.
2398 kiblnd_conn_addref(conn);
2399 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2401 /* Schedule blocked txs
2402 * Note: if we are running with conns_per_peer > 1, these blocked
2403 * txs will all get scheduled to the first connection which gets
2404 * scheduled. We won't be using round robin on this first batch.
2406 spin_lock(&conn->ibc_lock);
2407 while (!list_empty(&txs)) {
2408 tx = list_entry(txs.next, struct kib_tx, tx_list);
2409 list_del(&tx->tx_list);
2411 kiblnd_queue_tx_locked(tx, conn);
2413 kiblnd_check_sends_locked(conn);
2414 spin_unlock(&conn->ibc_lock);
2416 /* schedule blocked rxs */
2417 kiblnd_handle_early_rxs(conn);
2418 kiblnd_conn_decref(conn);
2422 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2426 #ifdef HAVE_RDMA_REJECT_4ARGS
2427 rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2429 rc = rdma_reject(cmid, rej, sizeof(*rej));
2433 CWARN("Error %d sending reject\n", rc);
2437 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2439 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2440 struct kib_msg *reqmsg = priv;
2441 struct kib_msg *ackmsg;
2442 struct kib_dev *ibdev;
2443 struct kib_peer_ni *peer_ni;
2444 struct kib_peer_ni *peer2;
2445 struct kib_conn *conn;
2446 struct lnet_ni *ni = NULL;
2447 struct kib_net *net = NULL;
2449 struct rdma_conn_param cp;
2451 int version = IBLND_MSG_VERSION;
2452 unsigned long flags;
2454 struct sockaddr_in *peer_addr;
2455 LASSERT (!in_interrupt());
2457 /* cmid inherits 'context' from the corresponding listener id */
2458 ibdev = cmid->context;
2461 memset(&rej, 0, sizeof(rej));
2462 rej.ibr_magic = IBLND_MSG_MAGIC;
2463 rej.ibr_why = IBLND_REJECT_FATAL;
2464 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2466 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2467 if (*kiblnd_tunables.kib_require_priv_port &&
2468 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2469 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2470 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2471 &ip, ntohs(peer_addr->sin_port));
2475 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2476 CERROR("Short connection request\n");
2480 /* Future protocol version compatibility support! If the
2481 * o2iblnd-specific protocol changes, or when LNET unifies
2482 * protocols over all LNDs, the initial connection will
2483 * negotiate a protocol version. I trap this here to avoid
2484 * console errors; the reject tells the peer_ni which protocol I
2486 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2487 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2489 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2490 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2491 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2493 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2494 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2495 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2498 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2500 CERROR("Can't parse connection request: %d\n", rc);
2504 nid = reqmsg->ibm_srcnid;
2505 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2508 net = (struct kib_net *)ni->ni_data;
2509 rej.ibr_incarnation = net->ibn_incarnation;
2512 if (ni == NULL || /* no matching net */
2513 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2514 net->ibn_dev != ibdev) { /* wrong device */
2515 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2516 "bad dst nid %s\n", libcfs_nid2str(nid),
2517 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2518 ibdev->ibd_ifname, ibdev->ibd_nnets,
2520 libcfs_nid2str(reqmsg->ibm_dstnid));
2525 /* check time stamp as soon as possible */
2526 if (reqmsg->ibm_dststamp != 0 &&
2527 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2528 CWARN("Stale connection request\n");
2529 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2533 /* I can accept peer_ni's version */
2534 version = reqmsg->ibm_version;
2536 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2537 CERROR("Unexpected connreq msg type: %x from %s\n",
2538 reqmsg->ibm_type, libcfs_nid2str(nid));
2542 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2543 kiblnd_msg_queue_size(version, ni)) {
2544 CERROR("Can't accept conn from %s, queue depth too large: "
2545 " %d (<=%d wanted)\n",
2546 libcfs_nid2str(nid),
2547 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2548 kiblnd_msg_queue_size(version, ni));
2550 if (version == IBLND_MSG_VERSION)
2551 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2556 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2557 IBLND_MAX_RDMA_FRAGS) {
2558 CWARN("Can't accept conn from %s (version %x): "
2559 "max_frags %d too large (%d wanted)\n",
2560 libcfs_nid2str(nid), version,
2561 reqmsg->ibm_u.connparams.ibcp_max_frags,
2562 IBLND_MAX_RDMA_FRAGS);
2564 if (version >= IBLND_MSG_VERSION)
2565 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2568 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2569 IBLND_MAX_RDMA_FRAGS &&
2570 net->ibn_fmr_ps == NULL) {
2571 CWARN("Can't accept conn from %s (version %x): "
2572 "max_frags %d incompatible without FMR pool "
2574 libcfs_nid2str(nid), version,
2575 reqmsg->ibm_u.connparams.ibcp_max_frags,
2576 IBLND_MAX_RDMA_FRAGS);
2578 if (version == IBLND_MSG_VERSION)
2579 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2584 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2585 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2586 libcfs_nid2str(nid),
2587 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2592 /* assume 'nid' is a new peer_ni; create */
2593 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2595 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2596 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2600 /* We have validated the peer's parameters so use those */
2601 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2602 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2604 write_lock_irqsave(g_lock, flags);
2606 peer2 = kiblnd_find_peer_locked(ni, nid);
2607 if (peer2 != NULL) {
2608 if (peer2->ibp_version == 0) {
2609 peer2->ibp_version = version;
2610 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2613 /* not the guy I've talked with */
2614 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2615 peer2->ibp_version != version) {
2616 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2618 if (kiblnd_peer_active(peer2)) {
2619 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2620 peer2->ibp_version = version;
2622 write_unlock_irqrestore(g_lock, flags);
2624 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2625 libcfs_nid2str(nid), peer2->ibp_version, version,
2626 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2628 kiblnd_peer_decref(peer_ni);
2629 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2633 /* Tie-break connection race in favour of the higher NID.
2634 * If we keep running into a race condition multiple times,
2635 * we have to assume that the connection attempt with the
2636 * higher NID is stuck in a connecting state and will never
2637 * recover. As such, we pass through this if-block and let
2638 * the lower NID connection win so we can move forward.
2640 if (peer2->ibp_connecting != 0 &&
2641 nid < ni->ni_nid && peer2->ibp_races <
2642 MAX_CONN_RACES_BEFORE_ABORT) {
2644 write_unlock_irqrestore(g_lock, flags);
2646 CDEBUG(D_NET, "Conn race %s\n",
2647 libcfs_nid2str(peer2->ibp_nid));
2649 kiblnd_peer_decref(peer_ni);
2650 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2653 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2654 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2655 libcfs_nid2str(peer2->ibp_nid),
2656 MAX_CONN_RACES_BEFORE_ABORT);
2658 * passive connection is allowed even this peer_ni is waiting for
2661 peer2->ibp_reconnecting = 0;
2662 peer2->ibp_races = 0;
2663 peer2->ibp_accepting++;
2664 kiblnd_peer_addref(peer2);
2666 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2667 * so copy validated parameters since we now know what the
2668 * peer_ni's limits are */
2669 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2670 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2672 write_unlock_irqrestore(g_lock, flags);
2673 kiblnd_peer_decref(peer_ni);
2676 /* Brand new peer_ni */
2677 LASSERT (peer_ni->ibp_accepting == 0);
2678 LASSERT (peer_ni->ibp_version == 0 &&
2679 peer_ni->ibp_incarnation == 0);
2681 peer_ni->ibp_accepting = 1;
2682 peer_ni->ibp_version = version;
2683 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2685 /* I have a ref on ni that prevents it being shutdown */
2686 LASSERT (net->ibn_shutdown == 0);
2688 kiblnd_peer_addref(peer_ni);
2689 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
2691 write_unlock_irqrestore(g_lock, flags);
2694 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2696 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2697 kiblnd_peer_decref(peer_ni);
2698 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2702 /* conn now "owns" cmid, so I return success from here on to ensure the
2703 * CM callback doesn't destroy cmid. */
2704 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2705 conn->ibc_credits = conn->ibc_queue_depth;
2706 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2707 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2708 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2710 ackmsg = &conn->ibc_connvars->cv_msg;
2711 memset(ackmsg, 0, sizeof(*ackmsg));
2713 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2714 sizeof(ackmsg->ibm_u.connparams));
2715 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2716 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2717 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2719 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2721 memset(&cp, 0, sizeof(cp));
2722 cp.private_data = ackmsg;
2723 cp.private_data_len = ackmsg->ibm_nob;
2724 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2725 cp.initiator_depth = 0;
2726 cp.flow_control = 1;
2727 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2728 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2730 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2732 rc = rdma_accept(cmid, &cp);
2734 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2735 rej.ibr_version = version;
2736 rej.ibr_why = IBLND_REJECT_FATAL;
2738 kiblnd_reject(cmid, &rej);
2739 kiblnd_connreq_done(conn, rc);
2740 kiblnd_conn_decref(conn);
2748 rej.ibr_cp.ibcp_queue_depth =
2749 kiblnd_msg_queue_size(version, ni);
2750 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2754 rej.ibr_version = version;
2755 kiblnd_reject(cmid, &rej);
2757 return -ECONNREFUSED;
2761 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2762 u64 incarnation, int why, struct kib_connparams *cp)
2764 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2765 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2767 int msg_size = IBLND_MSG_SIZE;
2771 unsigned long flags;
2773 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2774 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2777 msg_size = cp->ibcp_max_msg_size;
2778 frag_num = cp->ibcp_max_frags;
2779 queue_dep = cp->ibcp_queue_depth;
2782 write_lock_irqsave(glock, flags);
2783 /* retry connection if it's still needed and no other connection
2784 * attempts (active or passive) are in progress
2785 * NB: reconnect is still needed even when ibp_tx_queue is
2786 * empty if ibp_version != version because reconnect may be
2787 * initiated by kiblnd_query() */
2788 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2789 peer_ni->ibp_version != version) &&
2790 peer_ni->ibp_connecting &&
2791 peer_ni->ibp_accepting == 0;
2802 case IBLND_REJECT_RDMA_FRAGS: {
2803 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2806 reason = "can't negotiate max frags";
2809 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2810 #ifdef HAVE_IB_GET_DMA_MR
2812 * This check only makes sense if the kernel supports global
2813 * memory registration. Otherwise, map_on_demand will never == 0
2815 if (!tunables->lnd_map_on_demand) {
2816 reason = "map_on_demand must be enabled";
2820 if (conn->ibc_max_frags <= frag_num) {
2821 reason = "unsupported max frags";
2825 peer_ni->ibp_max_frags = frag_num;
2826 reason = "rdma fragments";
2829 case IBLND_REJECT_MSG_QUEUE_SIZE:
2831 reason = "can't negotiate queue depth";
2834 if (conn->ibc_queue_depth <= queue_dep) {
2835 reason = "unsupported queue depth";
2839 peer_ni->ibp_queue_depth = queue_dep;
2840 reason = "queue depth";
2843 case IBLND_REJECT_CONN_STALE:
2847 case IBLND_REJECT_CONN_RACE:
2848 reason = "conn race";
2851 case IBLND_REJECT_CONN_UNCOMPAT:
2852 reason = "version negotiation";
2855 case IBLND_REJECT_INVALID_SRV_ID:
2856 reason = "invalid service id";
2860 conn->ibc_reconnect = 1;
2861 peer_ni->ibp_reconnecting++;
2862 peer_ni->ibp_version = version;
2863 if (incarnation != 0)
2864 peer_ni->ibp_incarnation = incarnation;
2866 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2868 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2869 libcfs_nid2str(peer_ni->ibp_nid),
2870 reconnect ? "reconnect" : "don't reconnect",
2871 reason, IBLND_MSG_VERSION, version, msg_size,
2872 conn->ibc_queue_depth, queue_dep,
2873 conn->ibc_max_frags, frag_num);
2875 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2876 * while destroying the zombie
2881 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2883 struct kib_peer_ni *peer_ni = conn->ibc_peer;
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 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2896 IBLND_REJECT_INVALID_SRV_ID, NULL);
2897 CNETERR("%s rejected: no listener at %d\n",
2898 libcfs_nid2str(peer_ni->ibp_nid),
2899 *kiblnd_tunables.kib_service);
2902 case IB_CM_REJ_CONSUMER_DEFINED:
2903 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2904 struct kib_rej *rej = priv;
2905 struct kib_connparams *cp = NULL;
2907 __u64 incarnation = -1;
2909 /* NB. default incarnation is -1 because:
2910 * a) V1 will ignore dst incarnation in connreq.
2911 * b) V2 will provide incarnation while rejecting me,
2912 * -1 will be overwrote.
2914 * if I try to connect to a V1 peer_ni with V2 protocol,
2915 * it rejected me then upgrade to V2, I have no idea
2916 * about the upgrading and try to reconnect with V1,
2917 * in this case upgraded V2 can find out I'm trying to
2918 * talk to the old guy and reject me(incarnation is -1).
2921 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2922 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2923 __swab32s(&rej->ibr_magic);
2924 __swab16s(&rej->ibr_version);
2928 if (priv_nob >= sizeof(struct kib_rej) &&
2929 rej->ibr_version > IBLND_MSG_VERSION_1) {
2930 /* priv_nob is always 148 in current version
2931 * of OFED, so we still need to check version.
2932 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2936 __swab64s(&rej->ibr_incarnation);
2937 __swab16s(&cp->ibcp_queue_depth);
2938 __swab16s(&cp->ibcp_max_frags);
2939 __swab32s(&cp->ibcp_max_msg_size);
2942 incarnation = rej->ibr_incarnation;
2945 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2946 rej->ibr_magic != LNET_PROTO_MAGIC) {
2947 CERROR("%s rejected: consumer defined fatal error\n",
2948 libcfs_nid2str(peer_ni->ibp_nid));
2952 if (rej->ibr_version != IBLND_MSG_VERSION &&
2953 rej->ibr_version != IBLND_MSG_VERSION_1) {
2954 CERROR("%s rejected: o2iblnd version %x error\n",
2955 libcfs_nid2str(peer_ni->ibp_nid),
2960 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2961 rej->ibr_version == IBLND_MSG_VERSION_1) {
2962 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2963 libcfs_nid2str(peer_ni->ibp_nid), rej->ibr_version);
2965 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2966 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2969 switch (rej->ibr_why) {
2970 case IBLND_REJECT_CONN_RACE:
2971 case IBLND_REJECT_CONN_STALE:
2972 case IBLND_REJECT_CONN_UNCOMPAT:
2973 case IBLND_REJECT_MSG_QUEUE_SIZE:
2974 case IBLND_REJECT_RDMA_FRAGS:
2975 kiblnd_check_reconnect(conn, rej->ibr_version,
2976 incarnation, rej->ibr_why, cp);
2979 case IBLND_REJECT_NO_RESOURCES:
2980 CERROR("%s rejected: o2iblnd no resources\n",
2981 libcfs_nid2str(peer_ni->ibp_nid));
2984 case IBLND_REJECT_FATAL:
2985 CERROR("%s rejected: o2iblnd fatal error\n",
2986 libcfs_nid2str(peer_ni->ibp_nid));
2990 CERROR("%s rejected: o2iblnd reason %d\n",
2991 libcfs_nid2str(peer_ni->ibp_nid),
2999 CNETERR("%s rejected: reason %d, size %d\n",
3000 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
3004 kiblnd_connreq_done(conn, -ECONNREFUSED);
3008 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
3010 struct kib_peer_ni *peer_ni = conn->ibc_peer;
3011 struct lnet_ni *ni = peer_ni->ibp_ni;
3012 struct kib_net *net = ni->ni_data;
3013 struct kib_msg *msg = priv;
3014 int ver = conn->ibc_version;
3015 int rc = kiblnd_unpack_msg(msg, priv_nob);
3016 unsigned long flags;
3018 LASSERT (net != NULL);
3021 CERROR("Can't unpack connack from %s: %d\n",
3022 libcfs_nid2str(peer_ni->ibp_nid), rc);
3026 if (msg->ibm_type != IBLND_MSG_CONNACK) {
3027 CERROR("Unexpected message %d from %s\n",
3028 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
3033 if (ver != msg->ibm_version) {
3034 CERROR("%s replied version %x is different with "
3035 "requested version %x\n",
3036 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
3041 if (msg->ibm_u.connparams.ibcp_queue_depth >
3042 conn->ibc_queue_depth) {
3043 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
3044 libcfs_nid2str(peer_ni->ibp_nid),
3045 msg->ibm_u.connparams.ibcp_queue_depth,
3046 conn->ibc_queue_depth);
3051 if (msg->ibm_u.connparams.ibcp_max_frags >
3052 conn->ibc_max_frags) {
3053 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3054 libcfs_nid2str(peer_ni->ibp_nid),
3055 msg->ibm_u.connparams.ibcp_max_frags,
3056 conn->ibc_max_frags);
3061 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3062 CERROR("%s max message size %d too big (%d max)\n",
3063 libcfs_nid2str(peer_ni->ibp_nid),
3064 msg->ibm_u.connparams.ibcp_max_msg_size,
3070 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3071 if (msg->ibm_dstnid == ni->ni_nid &&
3072 msg->ibm_dststamp == net->ibn_incarnation)
3076 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3079 CERROR("Bad connection reply from %s, rc = %d, "
3080 "version: %x max_frags: %d\n",
3081 libcfs_nid2str(peer_ni->ibp_nid), rc,
3082 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3086 conn->ibc_incarnation = msg->ibm_srcstamp;
3087 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3088 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3089 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3090 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3091 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3092 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3094 kiblnd_connreq_done(conn, 0);
3098 /* NB My QP has already established itself, so I handle anything going
3099 * wrong here by setting ibc_comms_error.
3100 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3101 * immediately tears it down. */
3104 conn->ibc_comms_error = rc;
3105 kiblnd_connreq_done(conn, 0);
3109 kiblnd_active_connect(struct rdma_cm_id *cmid)
3111 struct kib_peer_ni *peer_ni = cmid->context;
3112 struct kib_conn *conn;
3113 struct kib_msg *msg;
3114 struct rdma_conn_param cp;
3117 unsigned long flags;
3120 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3122 incarnation = peer_ni->ibp_incarnation;
3123 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3124 peer_ni->ibp_version;
3126 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3128 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3131 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3132 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3136 /* conn "owns" cmid now, so I return success from here on to ensure the
3137 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3140 msg = &conn->ibc_connvars->cv_msg;
3142 memset(msg, 0, sizeof(*msg));
3143 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3144 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3145 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3146 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3148 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3149 0, peer_ni->ibp_nid, incarnation);
3151 memset(&cp, 0, sizeof(cp));
3152 cp.private_data = msg;
3153 cp.private_data_len = msg->ibm_nob;
3154 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3155 cp.initiator_depth = 0;
3156 cp.flow_control = 1;
3157 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3158 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3160 LASSERT(cmid->context == (void *)conn);
3161 LASSERT(conn->ibc_cmid == cmid);
3163 rc = rdma_connect(cmid, &cp);
3165 CERROR("Can't connect to %s: %d\n",
3166 libcfs_nid2str(peer_ni->ibp_nid), rc);
3167 kiblnd_connreq_done(conn, rc);
3168 kiblnd_conn_decref(conn);
3175 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3177 struct kib_peer_ni *peer_ni;
3178 struct kib_conn *conn;
3181 switch (event->event) {
3183 CERROR("Unexpected event: %d, status: %d\n",
3184 event->event, event->status);
3187 case RDMA_CM_EVENT_CONNECT_REQUEST:
3188 /* destroy cmid on failure */
3189 rc = kiblnd_passive_connect(cmid,
3190 (void *)KIBLND_CONN_PARAM(event),
3191 KIBLND_CONN_PARAM_LEN(event));
3192 CDEBUG(D_NET, "connreq: %d\n", rc);
3195 case RDMA_CM_EVENT_ADDR_ERROR:
3196 peer_ni = cmid->context;
3197 CNETERR("%s: ADDR ERROR %d\n",
3198 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3199 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3200 kiblnd_peer_decref(peer_ni);
3201 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3203 case RDMA_CM_EVENT_ADDR_RESOLVED:
3204 peer_ni = cmid->context;
3206 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3207 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3209 if (event->status != 0) {
3210 CNETERR("Can't resolve address for %s: %d\n",
3211 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3214 rc = rdma_resolve_route(
3215 cmid, lnet_get_lnd_timeout() * 1000);
3217 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3218 struct kib_dev *dev = net->ibn_dev;
3220 CDEBUG(D_NET, "%s: connection bound to "\
3222 libcfs_nid2str(peer_ni->ibp_nid),
3224 &dev->ibd_ifip, cmid->device->name);
3229 /* Can't initiate route resolution */
3230 CERROR("Can't resolve route for %s: %d\n",
3231 libcfs_nid2str(peer_ni->ibp_nid), rc);
3233 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3234 kiblnd_peer_decref(peer_ni);
3235 return rc; /* rc != 0 destroys cmid */
3237 case RDMA_CM_EVENT_ROUTE_ERROR:
3238 peer_ni = cmid->context;
3239 CNETERR("%s: ROUTE ERROR %d\n",
3240 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3241 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3242 kiblnd_peer_decref(peer_ni);
3243 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3245 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3246 peer_ni = cmid->context;
3247 CDEBUG(D_NET,"%s Route resolved: %d\n",
3248 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3250 if (event->status == 0)
3251 return kiblnd_active_connect(cmid);
3253 CNETERR("Can't resolve route for %s: %d\n",
3254 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3255 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3256 kiblnd_peer_decref(peer_ni);
3257 return event->status; /* rc != 0 destroys cmid */
3259 case RDMA_CM_EVENT_UNREACHABLE:
3260 conn = cmid->context;
3261 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3262 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3263 CNETERR("%s: UNREACHABLE %d\n",
3264 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3265 kiblnd_connreq_done(conn, -ENETDOWN);
3266 kiblnd_conn_decref(conn);
3269 case RDMA_CM_EVENT_CONNECT_ERROR:
3270 conn = cmid->context;
3271 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3272 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3273 CNETERR("%s: CONNECT ERROR %d\n",
3274 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3275 kiblnd_connreq_done(conn, -ENOTCONN);
3276 kiblnd_conn_decref(conn);
3279 case RDMA_CM_EVENT_REJECTED:
3280 conn = cmid->context;
3281 switch (conn->ibc_state) {
3285 case IBLND_CONN_PASSIVE_WAIT:
3286 CERROR ("%s: REJECTED %d\n",
3287 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3289 kiblnd_connreq_done(conn, -ECONNRESET);
3292 case IBLND_CONN_ACTIVE_CONNECT:
3293 kiblnd_rejected(conn, event->status,
3294 (void *)KIBLND_CONN_PARAM(event),
3295 KIBLND_CONN_PARAM_LEN(event));
3298 kiblnd_conn_decref(conn);
3301 case RDMA_CM_EVENT_ESTABLISHED:
3302 conn = cmid->context;
3303 switch (conn->ibc_state) {
3307 case IBLND_CONN_PASSIVE_WAIT:
3308 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3309 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3310 kiblnd_connreq_done(conn, 0);
3313 case IBLND_CONN_ACTIVE_CONNECT:
3314 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3315 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3316 kiblnd_check_connreply(conn,
3317 (void *)KIBLND_CONN_PARAM(event),
3318 KIBLND_CONN_PARAM_LEN(event));
3321 /* net keeps its ref on conn! */
3324 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3325 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3328 case RDMA_CM_EVENT_DISCONNECTED:
3329 conn = cmid->context;
3330 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3331 CERROR("%s DISCONNECTED\n",
3332 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3333 kiblnd_connreq_done(conn, -ECONNRESET);
3335 kiblnd_close_conn(conn, 0);
3337 kiblnd_conn_decref(conn);
3338 cmid->context = NULL;
3341 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3342 LCONSOLE_ERROR_MSG(0x131,
3343 "Received notification of device removal\n"
3344 "Please shutdown LNET to allow this to proceed\n");
3345 /* Can't remove network from underneath LNET for now, so I have
3349 case RDMA_CM_EVENT_ADDR_CHANGE:
3350 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3356 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3359 struct list_head *ttmp;
3361 list_for_each(ttmp, txs) {
3362 tx = list_entry(ttmp, struct kib_tx, tx_list);
3364 if (txs != &conn->ibc_active_txs) {
3365 LASSERT(tx->tx_queued);
3367 LASSERT(!tx->tx_queued);
3368 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3371 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3372 CERROR("Timed out tx: %s, %lld seconds\n",
3373 kiblnd_queue2str(conn, txs),
3374 ktime_ms_delta(ktime_get(),
3375 tx->tx_deadline) / MSEC_PER_SEC);
3384 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3386 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3387 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3388 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3389 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3390 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3394 kiblnd_check_conns (int idx)
3396 struct list_head closes = LIST_HEAD_INIT(closes);
3397 struct list_head checksends = LIST_HEAD_INIT(checksends);
3398 struct list_head timedout_txs = LIST_HEAD_INIT(timedout_txs);
3399 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3400 struct list_head *ptmp;
3401 struct kib_peer_ni *peer_ni;
3402 struct kib_conn *conn;
3403 struct kib_tx *tx, *tx_tmp;
3404 struct list_head *ctmp;
3405 unsigned long flags;
3407 /* NB. We expect to have a look at all the peers and not find any
3408 * RDMAs to time out, so we just use a shared lock while we
3410 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3412 list_for_each(ptmp, peers) {
3413 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
3415 /* Check tx_deadline */
3416 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3417 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3418 CWARN("Timed out tx for %s: %lld seconds\n",
3419 libcfs_nid2str(peer_ni->ibp_nid),
3420 ktime_ms_delta(ktime_get(),
3421 tx->tx_deadline) / MSEC_PER_SEC);
3422 list_move(&tx->tx_list, &timedout_txs);
3426 list_for_each(ctmp, &peer_ni->ibp_conns) {
3430 conn = list_entry(ctmp, struct kib_conn, ibc_list);
3432 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3434 spin_lock(&conn->ibc_lock);
3436 sendnoop = kiblnd_need_noop(conn);
3437 timedout = kiblnd_conn_timed_out_locked(conn);
3438 if (!sendnoop && !timedout) {
3439 spin_unlock(&conn->ibc_lock);
3444 CERROR("Timed out RDMA with %s (%lld): "
3445 "c: %u, oc: %u, rc: %u\n",
3446 libcfs_nid2str(peer_ni->ibp_nid),
3447 ktime_get_seconds() - peer_ni->ibp_last_alive,
3449 conn->ibc_outstanding_credits,
3450 conn->ibc_reserved_credits);
3451 list_add(&conn->ibc_connd_list, &closes);
3453 list_add(&conn->ibc_connd_list, &checksends);
3455 /* +ref for 'closes' or 'checksends' */
3456 kiblnd_conn_addref(conn);
3458 spin_unlock(&conn->ibc_lock);
3462 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3464 if (!list_empty(&timedout_txs))
3465 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3466 LNET_MSG_STATUS_LOCAL_TIMEOUT);
3468 /* Handle timeout by closing the whole
3469 * connection. We can only be sure RDMA activity
3470 * has ceased once the QP has been modified. */
3471 while (!list_empty(&closes)) {
3472 conn = list_entry(closes.next,
3473 struct kib_conn, ibc_connd_list);
3474 list_del(&conn->ibc_connd_list);
3475 kiblnd_close_conn(conn, -ETIMEDOUT);
3476 kiblnd_conn_decref(conn);
3479 /* In case we have enough credits to return via a
3480 * NOOP, but there were no non-blocking tx descs
3481 * free to do it last time... */
3482 while (!list_empty(&checksends)) {
3483 conn = list_entry(checksends.next,
3484 struct kib_conn, ibc_connd_list);
3485 list_del(&conn->ibc_connd_list);
3487 spin_lock(&conn->ibc_lock);
3488 kiblnd_check_sends_locked(conn);
3489 spin_unlock(&conn->ibc_lock);
3491 kiblnd_conn_decref(conn);
3496 kiblnd_disconnect_conn(struct kib_conn *conn)
3498 LASSERT (!in_interrupt());
3499 LASSERT (current == kiblnd_data.kib_connd);
3500 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3502 rdma_disconnect(conn->ibc_cmid);
3503 kiblnd_finalise_conn(conn);
3505 kiblnd_peer_notify(conn->ibc_peer);
3509 * High-water for reconnection to the same peer_ni, reconnection attempt should
3510 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3512 #define KIB_RECONN_HIGH_RACE 10
3514 * Allow connd to take a break and handle other things after consecutive
3515 * reconnection attemps.
3517 #define KIB_RECONN_BREAK 100
3520 kiblnd_connd (void *arg)
3522 spinlock_t *lock= &kiblnd_data.kib_connd_lock;
3523 wait_queue_entry_t wait;
3524 unsigned long flags;
3525 struct kib_conn *conn;
3530 unsigned long deadline = jiffies;
3532 cfs_block_allsigs();
3534 init_waitqueue_entry(&wait, current);
3535 kiblnd_data.kib_connd = current;
3537 spin_lock_irqsave(lock, flags);
3539 while (!kiblnd_data.kib_shutdown) {
3544 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3545 struct kib_peer_ni *peer_ni = NULL;
3547 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3548 struct kib_conn, ibc_list);
3549 list_del(&conn->ibc_list);
3550 if (conn->ibc_reconnect) {
3551 peer_ni = conn->ibc_peer;
3552 kiblnd_peer_addref(peer_ni);
3555 spin_unlock_irqrestore(lock, flags);
3558 kiblnd_destroy_conn(conn);
3560 spin_lock_irqsave(lock, flags);
3562 LIBCFS_FREE(conn, sizeof(*conn));
3566 conn->ibc_peer = peer_ni;
3567 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3568 list_add_tail(&conn->ibc_list,
3569 &kiblnd_data.kib_reconn_list);
3571 list_add_tail(&conn->ibc_list,
3572 &kiblnd_data.kib_reconn_wait);
3575 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3576 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3577 struct kib_conn, ibc_list);
3578 list_del(&conn->ibc_list);
3580 spin_unlock_irqrestore(lock, flags);
3583 kiblnd_disconnect_conn(conn);
3584 kiblnd_conn_decref(conn);
3586 spin_lock_irqsave(lock, flags);
3589 while (reconn < KIB_RECONN_BREAK) {
3590 if (kiblnd_data.kib_reconn_sec !=
3591 ktime_get_real_seconds()) {
3592 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3593 list_splice_init(&kiblnd_data.kib_reconn_wait,
3594 &kiblnd_data.kib_reconn_list);
3597 if (list_empty(&kiblnd_data.kib_reconn_list))
3600 conn = list_entry(kiblnd_data.kib_reconn_list.next,
3601 struct kib_conn, ibc_list);
3602 list_del(&conn->ibc_list);
3604 spin_unlock_irqrestore(lock, flags);
3607 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3608 kiblnd_peer_decref(conn->ibc_peer);
3609 LIBCFS_FREE(conn, sizeof(*conn));
3611 spin_lock_irqsave(lock, flags);
3614 /* careful with the jiffy wrap... */
3615 timeout = (int)(deadline - jiffies);
3619 int chunk = kiblnd_data.kib_peer_hash_size;
3620 unsigned int lnd_timeout;
3622 spin_unlock_irqrestore(lock, flags);
3625 /* Time to check for RDMA timeouts on a few more
3626 * peers: I do checks every 'p' seconds on a
3627 * proportion of the peer_ni table and I need to check
3628 * every connection 'n' times within a timeout
3629 * interval, to ensure I detect a timeout on any
3630 * connection within (n+1)/n times the timeout
3633 lnd_timeout = lnet_get_lnd_timeout();
3634 if (lnd_timeout > n * p)
3635 chunk = (chunk * n * p) / lnd_timeout;
3639 for (i = 0; i < chunk; i++) {
3640 kiblnd_check_conns(peer_index);
3641 peer_index = (peer_index + 1) %
3642 kiblnd_data.kib_peer_hash_size;
3645 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3646 spin_lock_irqsave(lock, flags);
3652 /* Nothing to do for 'timeout' */
3653 set_current_state(TASK_INTERRUPTIBLE);
3654 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3655 spin_unlock_irqrestore(lock, flags);
3657 schedule_timeout(timeout);
3659 set_current_state(TASK_RUNNING);
3660 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3661 spin_lock_irqsave(lock, flags);
3664 spin_unlock_irqrestore(lock, flags);
3666 kiblnd_thread_fini();
3671 kiblnd_qp_event(struct ib_event *event, void *arg)
3673 struct kib_conn *conn = arg;
3675 switch (event->event) {
3676 case IB_EVENT_COMM_EST:
3677 CDEBUG(D_NET, "%s established\n",
3678 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3679 /* We received a packet but connection isn't established
3680 * probably handshake packet was lost, so free to
3681 * force make connection established */
3682 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3685 case IB_EVENT_PORT_ERR:
3686 case IB_EVENT_DEVICE_FATAL:
3687 CERROR("Fatal device error for NI %s\n",
3688 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3689 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3692 case IB_EVENT_PORT_ACTIVE:
3693 CERROR("Port reactivated for NI %s\n",
3694 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3695 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3699 CERROR("%s: Async QP event type %d\n",
3700 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3706 kiblnd_complete (struct ib_wc *wc)
3708 switch (kiblnd_wreqid2type(wc->wr_id)) {
3713 if (wc->status != IB_WC_SUCCESS &&
3714 wc->status != IB_WC_WR_FLUSH_ERR)
3715 CNETERR("FastReg failed: %d\n", wc->status);
3718 case IBLND_WID_RDMA:
3719 /* We only get RDMA completion notification if it fails. All
3720 * subsequent work items, including the final SEND will fail
3721 * too. However we can't print out any more info about the
3722 * failing RDMA because 'tx' might be back on the idle list or
3723 * even reused already if we didn't manage to post all our work
3725 CNETERR("RDMA (tx: %p) failed: %d\n",
3726 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3730 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3734 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3741 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3743 /* NB I'm not allowed to schedule this conn once its refcount has
3744 * reached 0. Since fundamentally I'm racing with scheduler threads
3745 * consuming my CQ I could be called after all completions have
3746 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3747 * and this CQ is about to be destroyed so I NOOP. */
3748 struct kib_conn *conn = arg;
3749 struct kib_sched_info *sched = conn->ibc_sched;
3750 unsigned long flags;
3752 LASSERT(cq == conn->ibc_cq);
3754 spin_lock_irqsave(&sched->ibs_lock, flags);
3756 conn->ibc_ready = 1;
3758 if (!conn->ibc_scheduled &&
3759 (conn->ibc_nrx > 0 ||
3760 conn->ibc_nsends_posted > 0)) {
3761 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3762 conn->ibc_scheduled = 1;
3763 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3765 if (waitqueue_active(&sched->ibs_waitq))
3766 wake_up(&sched->ibs_waitq);
3769 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3773 kiblnd_cq_event(struct ib_event *event, void *arg)
3775 struct kib_conn *conn = arg;
3777 CERROR("%s: async CQ event type %d\n",
3778 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3782 kiblnd_scheduler(void *arg)
3784 long id = (long)arg;
3785 struct kib_sched_info *sched;
3786 struct kib_conn *conn;
3787 wait_queue_entry_t wait;
3788 unsigned long flags;
3794 cfs_block_allsigs();
3796 init_waitqueue_entry(&wait, current);
3798 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3800 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3802 CWARN("Unable to bind on CPU partition %d, please verify "
3803 "whether all CPUs are healthy and reload modules if "
3804 "necessary, otherwise your system might under risk of "
3805 "low performance\n", sched->ibs_cpt);
3808 spin_lock_irqsave(&sched->ibs_lock, flags);
3810 while (!kiblnd_data.kib_shutdown) {
3811 if (busy_loops++ >= IBLND_RESCHED) {
3812 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3817 spin_lock_irqsave(&sched->ibs_lock, flags);
3822 if (!list_empty(&sched->ibs_conns)) {
3823 conn = list_entry(sched->ibs_conns.next,
3824 struct kib_conn, ibc_sched_list);
3825 /* take over kib_sched_conns' ref on conn... */
3826 LASSERT(conn->ibc_scheduled);
3827 list_del(&conn->ibc_sched_list);
3828 conn->ibc_ready = 0;
3830 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3832 wc.wr_id = IBLND_WID_INVAL;
3834 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3836 rc = ib_req_notify_cq(conn->ibc_cq,
3839 CWARN("%s: ib_req_notify_cq failed: %d, "
3840 "closing connection\n",
3841 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3842 kiblnd_close_conn(conn, -EIO);
3843 kiblnd_conn_decref(conn);
3844 spin_lock_irqsave(&sched->ibs_lock,
3849 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3852 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3854 "ib_poll_cq (rc: %d) returned invalid "
3855 "wr_id, opcode %d, status: %d, "
3856 "vendor_err: %d, conn: %s status: %d\n"
3857 "please upgrade firmware and OFED or "
3858 "contact vendor.\n", rc,
3859 wc.opcode, wc.status, wc.vendor_err,
3860 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3866 CWARN("%s: ib_poll_cq failed: %d, "
3867 "closing connection\n",
3868 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3870 kiblnd_close_conn(conn, -EIO);
3871 kiblnd_conn_decref(conn);
3872 spin_lock_irqsave(&sched->ibs_lock, flags);
3876 spin_lock_irqsave(&sched->ibs_lock, flags);
3878 if (rc != 0 || conn->ibc_ready) {
3879 /* There may be another completion waiting; get
3880 * another scheduler to check while I handle
3882 /* +1 ref for sched_conns */
3883 kiblnd_conn_addref(conn);
3884 list_add_tail(&conn->ibc_sched_list,
3886 if (waitqueue_active(&sched->ibs_waitq))
3887 wake_up(&sched->ibs_waitq);
3889 conn->ibc_scheduled = 0;
3893 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3894 kiblnd_complete(&wc);
3896 spin_lock_irqsave(&sched->ibs_lock, flags);
3899 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3906 set_current_state(TASK_INTERRUPTIBLE);
3907 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3908 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3913 remove_wait_queue(&sched->ibs_waitq, &wait);
3914 set_current_state(TASK_RUNNING);
3915 spin_lock_irqsave(&sched->ibs_lock, flags);
3918 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3920 kiblnd_thread_fini();
3925 kiblnd_failover_thread(void *arg)
3927 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3928 struct kib_dev *dev;
3929 struct net *ns = arg;
3930 wait_queue_entry_t wait;
3931 unsigned long flags;
3934 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3936 cfs_block_allsigs();
3938 init_waitqueue_entry(&wait, current);
3939 write_lock_irqsave(glock, flags);
3941 while (!kiblnd_data.kib_shutdown) {
3942 int do_failover = 0;
3945 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3947 if (ktime_get_seconds() < dev->ibd_next_failover)
3954 list_del_init(&dev->ibd_fail_list);
3955 dev->ibd_failover = 1;
3956 write_unlock_irqrestore(glock, flags);
3958 rc = kiblnd_dev_failover(dev, ns);
3960 write_lock_irqsave(glock, flags);
3962 LASSERT (dev->ibd_failover);
3963 dev->ibd_failover = 0;
3964 if (rc >= 0) { /* Device is OK or failover succeed */
3965 dev->ibd_next_failover = ktime_get_seconds() + 3;
3969 /* failed to failover, retry later */
3970 dev->ibd_next_failover = ktime_get_seconds() +
3971 min(dev->ibd_failed_failover, 10);
3972 if (kiblnd_dev_can_failover(dev)) {
3973 list_add_tail(&dev->ibd_fail_list,
3974 &kiblnd_data.kib_failed_devs);
3980 /* long sleep if no more pending failover */
3981 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3983 set_current_state(TASK_INTERRUPTIBLE);
3984 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3985 write_unlock_irqrestore(glock, flags);
3987 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3988 cfs_time_seconds(1));
3989 set_current_state(TASK_RUNNING);
3990 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3991 write_lock_irqsave(glock, flags);
3993 if (!long_sleep || rc != 0)
3996 /* have a long sleep, routine check all active devices,
3997 * we need checking like this because if there is not active
3998 * connection on the dev and no SEND from local, we may listen
3999 * on wrong HCA for ever while there is a bonding failover */
4000 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
4001 if (kiblnd_dev_can_failover(dev)) {
4002 list_add_tail(&dev->ibd_fail_list,
4003 &kiblnd_data.kib_failed_devs);
4008 write_unlock_irqrestore(glock, flags);
4010 kiblnd_thread_fini();