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;
592 * FMR does not support gaps but the tx has gaps then
593 * we should make sure that the number of fragments we'll be sending
594 * over fits within the number of fragments negotiated on the
595 * connection, otherwise, we won't be able to RDMA the data.
596 * We need to maintain the number of fragments negotiation on the
597 * connection for backwards compatibility.
599 if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
601 tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
602 CERROR("TX number of frags (%d) is <= than connection"
603 " number of frags (%d). Consider setting peer's"
604 " map_on_demand to 256\n", tx->tx_nfrags,
605 tx->tx_conn->ibc_max_frags);
610 fps = net->ibn_fmr_ps[cpt];
611 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
613 CERROR("Can't map %u pages: %d\n", nob, rc);
618 * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
621 rd->rd_key = tx->tx_fmr.fmr_key;
623 * for FastReg or FMR with no gaps we can accumulate all
624 * the fragments in one FastReg or FMR fragment.
626 if (((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED) && !tx->tx_gaps) ||
627 (dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)) {
628 /* FMR requires zero based address */
629 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
630 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
631 rd->rd_frags[0].rf_nob = nob;
635 * We're transmitting with gaps using FMR.
636 * We'll need to use multiple fragments and identify the
637 * zero based address of each fragment.
639 for (i = 0; i < rd->rd_nfrags; i++) {
640 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
641 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
649 kiblnd_unmap_tx(struct kib_tx *tx)
651 if (tx->tx_fmr.fmr_pfmr || tx->tx_fmr.fmr_frd)
652 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
654 if (tx->tx_nfrags != 0) {
655 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
656 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
661 #ifdef HAVE_IB_GET_DMA_MR
662 static struct ib_mr *
663 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
665 struct kib_net *net = ni->ni_data;
666 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
667 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
669 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
672 * if map-on-demand is turned on and the device supports
673 * either FMR or FastReg then use that. Otherwise use global
674 * memory regions. If that's not available either, then you're
675 * dead in the water and fail the operation.
677 if (tunables->lnd_map_on_demand &&
678 (net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED ||
679 net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED))
683 * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
684 * in the call chain. The mapping will fail with appropriate error
687 return hdev->ibh_mrs;
691 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
692 struct kib_rdma_desc *rd, int nfrags)
694 struct kib_net *net = ni->ni_data;
695 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
696 #ifdef HAVE_IB_GET_DMA_MR
697 struct ib_mr *mr = NULL;
702 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
704 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
705 tx->tx_nfrags = nfrags;
707 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
708 tx->tx_nfrags, tx->tx_dmadir);
710 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
711 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
712 hdev->ibh_ibdev, &tx->tx_frags[i]);
713 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
714 hdev->ibh_ibdev, &tx->tx_frags[i]);
715 nob += rd->rd_frags[i].rf_nob;
718 #ifdef HAVE_IB_GET_DMA_MR
719 mr = kiblnd_find_rd_dma_mr(ni, rd);
721 /* found pre-mapping MR */
722 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
727 if (net->ibn_fmr_ps != NULL)
728 return kiblnd_fmr_map_tx(net, tx, rd, nob);
733 static int kiblnd_setup_rd_iov(struct lnet_ni *ni, struct kib_tx *tx,
734 struct kib_rdma_desc *rd, unsigned int niov,
735 struct kvec *iov, int offset, int nob)
737 struct kib_net *net = ni->ni_data;
739 struct scatterlist *sg;
743 unsigned int max_niov;
747 LASSERT (net != NULL);
749 while (offset >= iov->iov_len) {
750 offset -= iov->iov_len;
762 vaddr = ((unsigned long)iov->iov_base) + offset;
763 page_offset = vaddr & (PAGE_SIZE - 1);
764 page = lnet_kvaddr_to_page(vaddr);
766 CERROR("Can't find page\n");
770 fragnob = min((int)(iov->iov_len - offset), nob);
771 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
774 * We're allowed to start at a non-aligned page offset in
775 * the first fragment and end at a non-aligned page offset
776 * in the last fragment.
778 if ((fragnob < (int)PAGE_SIZE - page_offset) &&
779 (niov < max_niov) && nob > fragnob) {
780 CDEBUG(D_NET, "fragnob %d < available page %d: with"
781 " remaining %d iovs with %d nob left\n",
782 fragnob, (int)PAGE_SIZE - page_offset, niov,
787 sg_set_page(sg, page, fragnob, page_offset);
790 CERROR("lacking enough sg entries to map tx\n");
794 if (offset + fragnob < iov->iov_len) {
804 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
807 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
808 struct kib_rdma_desc *rd, int nkiov,
809 lnet_kiov_t *kiov, int offset, int nob)
811 struct kib_net *net = ni->ni_data;
812 struct scatterlist *sg;
816 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
820 LASSERT(net != NULL);
822 while (offset >= kiov->kiov_len) {
823 offset -= kiov->kiov_len;
835 fragnob = min((int)(kiov->kiov_len - offset), nob);
838 * We're allowed to start at a non-aligned page offset in
839 * the first fragment and end at a non-aligned page offset
840 * in the last fragment.
842 if ((fragnob < (int)(kiov->kiov_len - offset)) &&
843 nkiov < max_nkiov && nob > fragnob) {
844 CDEBUG(D_NET, "fragnob %d < available page %d: with"
845 " remaining %d kiovs with %d nob left\n",
846 fragnob, (int)(kiov->kiov_len - offset),
851 sg_set_page(sg, kiov->kiov_page, fragnob,
852 kiov->kiov_offset + offset);
855 CERROR("lacking enough sg entries to map tx\n");
865 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
869 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
870 __must_hold(&conn->ibc_lock)
872 struct kib_msg *msg = tx->tx_msg;
873 struct kib_peer_ni *peer_ni = conn->ibc_peer;
874 struct lnet_ni *ni = peer_ni->ibp_ni;
875 int ver = conn->ibc_version;
879 LASSERT(tx->tx_queued);
880 /* We rely on this for QP sizing */
881 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
882 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
884 LASSERT(credit == 0 || credit == 1);
885 LASSERT(conn->ibc_outstanding_credits >= 0);
886 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
887 LASSERT(conn->ibc_credits >= 0);
888 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
890 if (conn->ibc_nsends_posted ==
891 kiblnd_concurrent_sends(ver, ni)) {
892 /* tx completions outstanding... */
893 CDEBUG(D_NET, "%s: posted enough\n",
894 libcfs_nid2str(peer_ni->ibp_nid));
898 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
899 CDEBUG(D_NET, "%s: no credits\n",
900 libcfs_nid2str(peer_ni->ibp_nid));
904 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
905 conn->ibc_credits == 1 && /* last credit reserved */
906 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
907 CDEBUG(D_NET, "%s: not using last credit\n",
908 libcfs_nid2str(peer_ni->ibp_nid));
912 /* NB don't drop ibc_lock before bumping tx_sending */
913 list_del(&tx->tx_list);
916 if (msg->ibm_type == IBLND_MSG_NOOP &&
917 (!kiblnd_need_noop(conn) || /* redundant NOOP */
918 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
919 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
920 /* OK to drop when posted enough NOOPs, since
921 * kiblnd_check_sends_locked will queue NOOP again when
922 * posted NOOPs complete */
923 spin_unlock(&conn->ibc_lock);
924 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
926 spin_lock(&conn->ibc_lock);
927 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
928 libcfs_nid2str(peer_ni->ibp_nid),
929 conn->ibc_noops_posted);
933 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
934 peer_ni->ibp_nid, conn->ibc_incarnation);
936 conn->ibc_credits -= credit;
937 conn->ibc_outstanding_credits = 0;
938 conn->ibc_nsends_posted++;
939 if (msg->ibm_type == IBLND_MSG_NOOP)
940 conn->ibc_noops_posted++;
942 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
943 * PUT. If so, it was first queued here as a PUT_REQ, sent and
944 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
945 * and then re-queued here. It's (just) possible that
946 * tx_sending is non-zero if we've not done the tx_complete()
947 * from the first send; hence the ++ rather than = below. */
949 list_add(&tx->tx_list, &conn->ibc_active_txs);
951 /* I'm still holding ibc_lock! */
952 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
954 } else if (tx->tx_pool->tpo_pool.po_failed ||
955 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
956 /* close_conn will launch failover */
959 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
960 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
961 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
964 if (!frd->frd_valid) {
965 wr = &frd->frd_inv_wr.wr;
966 wr->next = &frd->frd_fastreg_wr.wr;
968 wr = &frd->frd_fastreg_wr.wr;
970 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
973 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
974 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
975 bad->wr_id, bad->opcode, bad->send_flags,
976 libcfs_nid2str(conn->ibc_peer->ibp_nid));
979 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
982 #ifdef HAVE_IB_POST_SEND_RECV_CONST
983 rc = ib_post_send(conn->ibc_cmid->qp, wr,
984 (const struct ib_send_wr **)&bad);
986 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
990 conn->ibc_last_send = ktime_get();
995 /* NB credits are transferred in the actual
996 * message, which can only be the last work item */
997 conn->ibc_credits += credit;
998 conn->ibc_outstanding_credits += msg->ibm_credits;
999 conn->ibc_nsends_posted--;
1000 if (msg->ibm_type == IBLND_MSG_NOOP)
1001 conn->ibc_noops_posted--;
1007 done = (tx->tx_sending == 0);
1009 list_del(&tx->tx_list);
1011 spin_unlock(&conn->ibc_lock);
1013 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1014 CERROR("Error %d posting transmit to %s\n",
1015 rc, libcfs_nid2str(peer_ni->ibp_nid));
1017 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1018 rc, libcfs_nid2str(peer_ni->ibp_nid));
1020 kiblnd_close_conn(conn, rc);
1025 spin_lock(&conn->ibc_lock);
1031 kiblnd_check_sends_locked(struct kib_conn *conn)
1033 int ver = conn->ibc_version;
1034 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
1037 /* Don't send anything until after the connection is established */
1038 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
1039 CDEBUG(D_NET, "%s too soon\n",
1040 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1044 LASSERT(conn->ibc_nsends_posted <=
1045 kiblnd_concurrent_sends(ver, ni));
1046 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
1047 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
1048 LASSERT (conn->ibc_reserved_credits >= 0);
1050 while (conn->ibc_reserved_credits > 0 &&
1051 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
1052 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
1053 struct kib_tx, tx_list);
1054 list_del(&tx->tx_list);
1055 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
1056 conn->ibc_reserved_credits--;
1059 if (kiblnd_need_noop(conn)) {
1060 spin_unlock(&conn->ibc_lock);
1062 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1064 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1066 spin_lock(&conn->ibc_lock);
1068 kiblnd_queue_tx_locked(tx, conn);
1074 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1076 tx = list_entry(conn->ibc_tx_queue_nocred.next,
1077 struct kib_tx, tx_list);
1078 } else if (!list_empty(&conn->ibc_tx_noops)) {
1079 LASSERT (!IBLND_OOB_CAPABLE(ver));
1081 tx = list_entry(conn->ibc_tx_noops.next,
1082 struct kib_tx, tx_list);
1083 } else if (!list_empty(&conn->ibc_tx_queue)) {
1085 tx = list_entry(conn->ibc_tx_queue.next,
1086 struct kib_tx, tx_list);
1090 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1096 kiblnd_tx_complete(struct kib_tx *tx, int status)
1098 int failed = (status != IB_WC_SUCCESS);
1099 struct kib_conn *conn = tx->tx_conn;
1102 LASSERT (tx->tx_sending > 0);
1105 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1106 CNETERR("Tx -> %s cookie %#llx"
1107 " sending %d waiting %d: failed %d\n",
1108 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1109 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1112 kiblnd_close_conn(conn, -EIO);
1114 kiblnd_peer_alive(conn->ibc_peer);
1117 spin_lock(&conn->ibc_lock);
1119 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1120 * gets to free it, which also drops its ref on 'conn'. */
1123 conn->ibc_nsends_posted--;
1124 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1125 conn->ibc_noops_posted--;
1128 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1129 tx->tx_waiting = 0; /* don't wait for peer_ni */
1130 tx->tx_status = -EIO;
1133 idle = (tx->tx_sending == 0) && /* This is the final callback */
1134 !tx->tx_waiting && /* Not waiting for peer_ni */
1135 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1137 list_del(&tx->tx_list);
1139 kiblnd_check_sends_locked(conn);
1140 spin_unlock(&conn->ibc_lock);
1147 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1150 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1151 struct ib_sge *sge = &tx->tx_msgsge;
1152 struct ib_rdma_wr *wrq;
1153 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1154 #ifdef HAVE_IB_GET_DMA_MR
1155 struct ib_mr *mr = hdev->ibh_mrs;
1158 LASSERT(tx->tx_nwrq >= 0);
1159 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1160 LASSERT(nob <= IBLND_MSG_SIZE);
1161 #ifdef HAVE_IB_GET_DMA_MR
1162 LASSERT(mr != NULL);
1165 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1167 #ifdef HAVE_IB_GET_DMA_MR
1168 sge->lkey = mr->lkey;
1170 sge->lkey = hdev->ibh_pd->local_dma_lkey;
1172 sge->addr = tx->tx_msgaddr;
1175 wrq = &tx->tx_wrq[tx->tx_nwrq];
1176 memset(wrq, 0, sizeof(*wrq));
1178 wrq->wr.next = NULL;
1179 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1180 wrq->wr.sg_list = sge;
1181 wrq->wr.num_sge = 1;
1182 wrq->wr.opcode = IB_WR_SEND;
1183 wrq->wr.send_flags = IB_SEND_SIGNALED;
1189 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1190 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1192 struct kib_msg *ibmsg = tx->tx_msg;
1193 struct kib_rdma_desc *srcrd = tx->tx_rd;
1194 struct ib_rdma_wr *wrq = NULL;
1202 LASSERT(!in_interrupt());
1203 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1204 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1206 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1207 resid > 0; resid -= sge_nob) {
1210 if (srcidx >= srcrd->rd_nfrags) {
1211 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1216 if (dstidx >= dstrd->rd_nfrags) {
1217 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1222 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1223 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1224 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1225 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1226 conn->ibc_max_frags,
1227 srcidx, srcrd->rd_nfrags,
1228 dstidx, dstrd->rd_nfrags);
1233 sge_nob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1234 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1236 sge = &tx->tx_sge[tx->tx_nsge];
1237 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1238 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1239 sge->length = sge_nob;
1242 wrq = &tx->tx_wrq[tx->tx_nwrq];
1244 wrq->wr.next = &(wrq + 1)->wr;
1245 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1246 wrq->wr.sg_list = sge;
1247 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1248 wrq->wr.send_flags = 0;
1250 #ifdef HAVE_IB_RDMA_WR
1251 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1253 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1256 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1258 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1263 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1264 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1267 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1269 wrq->wr.num_sge = wrq_sge;
1275 if (rc < 0) /* no RDMA if completing with failure */
1276 tx->tx_nwrq = tx->tx_nsge = 0;
1278 ibmsg->ibm_u.completion.ibcm_status = rc;
1279 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1280 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1281 type, sizeof(struct kib_completion_msg));
1287 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1289 struct list_head *q;
1292 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1293 LASSERT(!tx->tx_queued); /* not queued for sending already */
1294 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1296 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1297 tx->tx_status = -ECONNABORTED;
1299 if (tx->tx_conn != NULL) {
1300 /* PUT_DONE first attached to conn as a PUT_REQ */
1301 LASSERT(tx->tx_conn == conn);
1302 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1304 kiblnd_conn_decref(conn);
1306 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1311 timeout_ns = lnet_get_lnd_timeout() * NSEC_PER_SEC;
1313 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1315 if (tx->tx_conn == NULL) {
1316 kiblnd_conn_addref(conn);
1318 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
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);
1325 switch (tx->tx_msg->ibm_type) {
1329 case IBLND_MSG_PUT_REQ:
1330 case IBLND_MSG_GET_REQ:
1331 q = &conn->ibc_tx_queue_rsrvd;
1334 case IBLND_MSG_PUT_NAK:
1335 case IBLND_MSG_PUT_ACK:
1336 case IBLND_MSG_PUT_DONE:
1337 case IBLND_MSG_GET_DONE:
1338 q = &conn->ibc_tx_queue_nocred;
1341 case IBLND_MSG_NOOP:
1342 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1343 q = &conn->ibc_tx_queue_nocred;
1345 q = &conn->ibc_tx_noops;
1348 case IBLND_MSG_IMMEDIATE:
1349 q = &conn->ibc_tx_queue;
1353 list_add_tail(&tx->tx_list, q);
1357 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1359 spin_lock(&conn->ibc_lock);
1360 kiblnd_queue_tx_locked(tx, conn);
1361 kiblnd_check_sends_locked(conn);
1362 spin_unlock(&conn->ibc_lock);
1365 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1366 struct sockaddr_in *srcaddr,
1367 struct sockaddr_in *dstaddr,
1370 unsigned short port;
1373 /* allow the port to be reused */
1374 rc = rdma_set_reuseaddr(cmid, 1);
1376 CERROR("Unable to set reuse on cmid: %d\n", rc);
1380 /* look for a free privileged port */
1381 for (port = PROT_SOCK-1; port > 0; port--) {
1382 srcaddr->sin_port = htons(port);
1383 rc = rdma_resolve_addr(cmid,
1384 (struct sockaddr *)srcaddr,
1385 (struct sockaddr *)dstaddr,
1388 CDEBUG(D_NET, "bound to port %hu\n", port);
1390 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1391 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1398 CERROR("Failed to bind to a free privileged port\n");
1403 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1405 struct rdma_cm_id *cmid;
1406 struct kib_dev *dev;
1407 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1408 struct sockaddr_in srcaddr;
1409 struct sockaddr_in dstaddr;
1412 LASSERT (net != NULL);
1413 LASSERT (peer_ni->ibp_connecting > 0);
1415 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer_ni, RDMA_PS_TCP,
1419 CERROR("Can't create CMID for %s: %ld\n",
1420 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1426 memset(&srcaddr, 0, sizeof(srcaddr));
1427 srcaddr.sin_family = AF_INET;
1428 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1430 memset(&dstaddr, 0, sizeof(dstaddr));
1431 dstaddr.sin_family = AF_INET;
1432 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1433 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1435 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1437 if (*kiblnd_tunables.kib_use_priv_port) {
1438 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1439 lnet_get_lnd_timeout() * 1000);
1441 rc = rdma_resolve_addr(cmid,
1442 (struct sockaddr *)&srcaddr,
1443 (struct sockaddr *)&dstaddr,
1444 lnet_get_lnd_timeout() * 1000);
1447 /* Can't initiate address resolution: */
1448 CERROR("Can't resolve addr for %s: %d\n",
1449 libcfs_nid2str(peer_ni->ibp_nid), rc);
1456 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1457 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1458 rdma_destroy_id(cmid);
1461 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1465 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1467 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1468 char *reason = NULL;
1469 struct list_head txs;
1470 unsigned long flags;
1472 INIT_LIST_HEAD(&txs);
1474 write_lock_irqsave(glock, flags);
1475 if (peer_ni->ibp_reconnecting == 0) {
1476 if (peer_ni->ibp_accepting)
1477 reason = "accepting";
1478 else if (peer_ni->ibp_connecting)
1479 reason = "connecting";
1480 else if (!list_empty(&peer_ni->ibp_conns))
1481 reason = "connected";
1482 else /* connected then closed */
1488 if (peer_ni->ibp_accepting)
1489 CNETERR("Detecting race between accepting and reconnecting\n");
1490 peer_ni->ibp_reconnecting--;
1492 if (!kiblnd_peer_active(peer_ni)) {
1493 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1494 reason = "unlinked";
1498 peer_ni->ibp_connecting++;
1499 peer_ni->ibp_reconnected++;
1501 write_unlock_irqrestore(glock, flags);
1503 kiblnd_connect_peer(peer_ni);
1507 write_unlock_irqrestore(glock, flags);
1509 CWARN("Abort reconnection of %s: %s\n",
1510 libcfs_nid2str(peer_ni->ibp_nid), reason);
1511 kiblnd_txlist_done(&txs, -ECONNABORTED,
1512 LNET_MSG_STATUS_LOCAL_ABORTED);
1517 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1519 struct kib_peer_ni *peer_ni;
1520 struct kib_peer_ni *peer2;
1521 struct kib_conn *conn;
1522 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1523 unsigned long flags;
1526 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1528 /* If I get here, I've committed to send, so I complete the tx with
1529 * failure on any problems */
1531 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1532 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1534 /* First time, just use a read lock since I expect to find my peer_ni
1536 read_lock_irqsave(g_lock, flags);
1538 peer_ni = kiblnd_find_peer_locked(ni, nid);
1539 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1540 /* Found a peer_ni with an established connection */
1541 conn = kiblnd_get_conn_locked(peer_ni);
1542 kiblnd_conn_addref(conn); /* 1 ref for me... */
1544 read_unlock_irqrestore(g_lock, flags);
1547 kiblnd_queue_tx(tx, conn);
1548 kiblnd_conn_decref(conn); /* ...to here */
1552 read_unlock(g_lock);
1553 /* Re-try with a write lock */
1556 peer_ni = kiblnd_find_peer_locked(ni, nid);
1557 if (peer_ni != NULL) {
1558 if (list_empty(&peer_ni->ibp_conns)) {
1559 /* found a peer_ni, but it's still connecting... */
1560 LASSERT(kiblnd_peer_connecting(peer_ni));
1562 list_add_tail(&tx->tx_list,
1563 &peer_ni->ibp_tx_queue);
1564 write_unlock_irqrestore(g_lock, flags);
1566 conn = kiblnd_get_conn_locked(peer_ni);
1567 kiblnd_conn_addref(conn); /* 1 ref for me... */
1569 write_unlock_irqrestore(g_lock, flags);
1572 kiblnd_queue_tx(tx, conn);
1573 kiblnd_conn_decref(conn); /* ...to here */
1578 write_unlock_irqrestore(g_lock, flags);
1580 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1581 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1583 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1585 tx->tx_status = -EHOSTUNREACH;
1587 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1593 write_lock_irqsave(g_lock, flags);
1595 peer2 = kiblnd_find_peer_locked(ni, nid);
1596 if (peer2 != NULL) {
1597 if (list_empty(&peer2->ibp_conns)) {
1598 /* found a peer_ni, but it's still connecting... */
1599 LASSERT(kiblnd_peer_connecting(peer2));
1601 list_add_tail(&tx->tx_list,
1602 &peer2->ibp_tx_queue);
1603 write_unlock_irqrestore(g_lock, flags);
1605 conn = kiblnd_get_conn_locked(peer2);
1606 kiblnd_conn_addref(conn); /* 1 ref for me... */
1608 write_unlock_irqrestore(g_lock, flags);
1611 kiblnd_queue_tx(tx, conn);
1612 kiblnd_conn_decref(conn); /* ...to here */
1615 kiblnd_peer_decref(peer_ni);
1619 /* Brand new peer_ni */
1620 LASSERT(peer_ni->ibp_connecting == 0);
1621 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1622 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1624 /* always called with a ref on ni, which prevents ni being shutdown */
1625 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1628 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1630 kiblnd_peer_addref(peer_ni);
1631 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
1633 write_unlock_irqrestore(g_lock, flags);
1635 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1636 kiblnd_connect_peer(peer_ni);
1637 kiblnd_peer_decref(peer_ni);
1641 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1643 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1644 int type = lntmsg->msg_type;
1645 struct lnet_process_id target = lntmsg->msg_target;
1646 int target_is_router = lntmsg->msg_target_is_router;
1647 int routing = lntmsg->msg_routing;
1648 unsigned int payload_niov = lntmsg->msg_niov;
1649 struct kvec *payload_iov = lntmsg->msg_iov;
1650 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1651 unsigned int payload_offset = lntmsg->msg_offset;
1652 unsigned int payload_nob = lntmsg->msg_len;
1653 struct kib_msg *ibmsg;
1654 struct kib_rdma_desc *rd;
1659 /* NB 'private' is different depending on what we're sending.... */
1661 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1662 payload_nob, payload_niov, libcfs_id2str(target));
1664 LASSERT (payload_nob == 0 || payload_niov > 0);
1665 LASSERT (payload_niov <= LNET_MAX_IOV);
1667 /* Thread context */
1668 LASSERT (!in_interrupt());
1669 /* payload is either all vaddrs or all pages */
1670 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1678 LASSERT (payload_nob == 0);
1682 if (routing || target_is_router)
1683 break; /* send IMMEDIATE */
1685 /* is the REPLY message too small for RDMA? */
1686 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1687 if (nob <= IBLND_MSG_SIZE)
1688 break; /* send IMMEDIATE */
1690 tx = kiblnd_get_idle_tx(ni, target.nid);
1692 CERROR("Can't allocate txd for GET to %s\n",
1693 libcfs_nid2str(target.nid));
1698 rd = &ibmsg->ibm_u.get.ibgm_rd;
1699 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1700 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1701 lntmsg->msg_md->md_niov,
1702 lntmsg->msg_md->md_iov.iov,
1703 0, lntmsg->msg_md->md_length);
1705 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1706 lntmsg->msg_md->md_niov,
1707 lntmsg->msg_md->md_iov.kiov,
1708 0, lntmsg->msg_md->md_length);
1710 CERROR("Can't setup GET sink for %s: %d\n",
1711 libcfs_nid2str(target.nid), rc);
1712 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1717 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1718 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1719 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1721 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1723 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1724 if (tx->tx_lntmsg[1] == NULL) {
1725 CERROR("Can't create reply for GET -> %s\n",
1726 libcfs_nid2str(target.nid));
1731 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1732 tx->tx_waiting = 1; /* waiting for GET_DONE */
1733 kiblnd_launch_tx(ni, tx, target.nid);
1736 case LNET_MSG_REPLY:
1738 /* Is the payload small enough not to need RDMA? */
1739 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob]);
1740 if (nob <= IBLND_MSG_SIZE)
1741 break; /* send IMMEDIATE */
1743 tx = kiblnd_get_idle_tx(ni, target.nid);
1745 CERROR("Can't allocate %s txd for %s\n",
1746 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1747 libcfs_nid2str(target.nid));
1751 if (payload_kiov == NULL)
1752 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1753 payload_niov, payload_iov,
1754 payload_offset, payload_nob);
1756 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1757 payload_niov, payload_kiov,
1758 payload_offset, payload_nob);
1760 CERROR("Can't setup PUT src for %s: %d\n",
1761 libcfs_nid2str(target.nid), rc);
1767 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1768 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1769 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1770 sizeof(struct kib_putreq_msg));
1772 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1773 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1774 kiblnd_launch_tx(ni, tx, target.nid);
1778 /* send IMMEDIATE */
1779 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1782 tx = kiblnd_get_idle_tx(ni, target.nid);
1784 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1785 type, libcfs_nid2str(target.nid));
1790 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1792 if (payload_kiov != NULL)
1793 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1794 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1795 payload_niov, payload_kiov,
1796 payload_offset, payload_nob);
1798 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1799 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1800 payload_niov, payload_iov,
1801 payload_offset, payload_nob);
1803 nob = offsetof(struct kib_immediate_msg, ibim_payload[payload_nob]);
1804 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1806 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1807 kiblnd_launch_tx(ni, tx, target.nid);
1812 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1814 struct lnet_process_id target = lntmsg->msg_target;
1815 unsigned int niov = lntmsg->msg_niov;
1816 struct kvec *iov = lntmsg->msg_iov;
1817 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1818 unsigned int offset = lntmsg->msg_offset;
1819 unsigned int nob = lntmsg->msg_len;
1823 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1825 CERROR("Can't get tx for REPLY to %s\n",
1826 libcfs_nid2str(target.nid));
1832 else if (kiov == NULL)
1833 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1834 niov, iov, offset, nob);
1836 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1837 niov, kiov, offset, nob);
1840 CERROR("Can't setup GET src for %s: %d\n",
1841 libcfs_nid2str(target.nid), rc);
1845 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1846 IBLND_MSG_GET_DONE, nob,
1847 &rx->rx_msg->ibm_u.get.ibgm_rd,
1848 rx->rx_msg->ibm_u.get.ibgm_cookie);
1850 CERROR("Can't setup rdma for GET from %s: %d\n",
1851 libcfs_nid2str(target.nid), rc);
1856 /* No RDMA: local completion may happen now! */
1857 lnet_finalize(lntmsg, 0);
1859 /* RDMA: lnet_finalize(lntmsg) when it
1861 tx->tx_lntmsg[0] = lntmsg;
1864 kiblnd_queue_tx(tx, rx->rx_conn);
1869 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1872 lnet_finalize(lntmsg, -EIO);
1876 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1877 int delayed, unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1878 unsigned int offset, unsigned int mlen, unsigned int rlen)
1880 struct kib_rx *rx = private;
1881 struct kib_msg *rxmsg = rx->rx_msg;
1882 struct kib_conn *conn = rx->rx_conn;
1886 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1889 LASSERT (mlen <= rlen);
1890 LASSERT (!in_interrupt());
1891 /* Either all pages or all vaddrs */
1892 LASSERT (!(kiov != NULL && iov != NULL));
1894 switch (rxmsg->ibm_type) {
1898 case IBLND_MSG_IMMEDIATE:
1899 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1900 if (nob > rx->rx_nob) {
1901 CERROR ("Immediate message from %s too big: %d(%d)\n",
1902 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1909 lnet_copy_flat2kiov(niov, kiov, offset,
1910 IBLND_MSG_SIZE, rxmsg,
1911 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1914 lnet_copy_flat2iov(niov, iov, offset,
1915 IBLND_MSG_SIZE, rxmsg,
1916 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1918 lnet_finalize(lntmsg, 0);
1921 case IBLND_MSG_PUT_REQ: {
1922 struct kib_msg *txmsg;
1923 struct kib_rdma_desc *rd;
1924 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1927 lnet_finalize(lntmsg, 0);
1928 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1933 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1935 CERROR("Can't allocate tx for %s\n",
1936 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1937 /* Not replying will break the connection */
1943 rd = &txmsg->ibm_u.putack.ibpam_rd;
1945 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1946 niov, iov, offset, mlen);
1948 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1949 niov, kiov, offset, mlen);
1951 CERROR("Can't setup PUT sink for %s: %d\n",
1952 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1953 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1955 /* tell peer_ni it's over */
1956 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1961 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1962 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1963 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1965 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1967 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1968 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1969 kiblnd_queue_tx(tx, conn);
1971 /* reposted buffer reserved for PUT_DONE */
1972 post_credit = IBLND_POSTRX_NO_CREDIT;
1976 case IBLND_MSG_GET_REQ:
1977 if (lntmsg != NULL) {
1978 /* Optimized GET; RDMA lntmsg's payload */
1979 kiblnd_reply(ni, rx, lntmsg);
1981 /* GET didn't match anything */
1982 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1984 rxmsg->ibm_u.get.ibgm_cookie);
1989 kiblnd_post_rx(rx, post_credit);
1994 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1996 struct task_struct *task = kthread_run(fn, arg, name);
1999 return PTR_ERR(task);
2001 atomic_inc(&kiblnd_data.kib_nthreads);
2006 kiblnd_thread_fini (void)
2008 atomic_dec (&kiblnd_data.kib_nthreads);
2012 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
2014 /* This is racy, but everyone's only writing ktime_get_seconds() */
2015 peer_ni->ibp_last_alive = ktime_get_seconds();
2020 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
2023 time64_t last_alive = 0;
2024 unsigned long flags;
2026 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2028 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
2029 error = peer_ni->ibp_error;
2030 peer_ni->ibp_error = 0;
2032 last_alive = peer_ni->ibp_last_alive;
2035 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2038 lnet_notify(peer_ni->ibp_ni,
2039 peer_ni->ibp_nid, false, false, last_alive);
2043 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
2045 /* This just does the immediate housekeeping. 'error' is zero for a
2046 * normal shutdown which can happen only after the connection has been
2047 * established. If the connection is established, schedule the
2048 * connection to be finished off by the connd. Otherwise the connd is
2049 * already dealing with it (either to set it up or tear it down).
2050 * Caller holds kib_global_lock exclusively in irq context */
2051 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2052 struct kib_dev *dev;
2053 unsigned long flags;
2055 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2057 if (error != 0 && conn->ibc_comms_error == 0)
2058 conn->ibc_comms_error = error;
2060 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2061 return; /* already being handled */
2064 list_empty(&conn->ibc_tx_noops) &&
2065 list_empty(&conn->ibc_tx_queue) &&
2066 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2067 list_empty(&conn->ibc_tx_queue_nocred) &&
2068 list_empty(&conn->ibc_active_txs)) {
2069 CDEBUG(D_NET, "closing conn to %s\n",
2070 libcfs_nid2str(peer_ni->ibp_nid));
2072 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2073 libcfs_nid2str(peer_ni->ibp_nid), error,
2074 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2075 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2076 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2077 "" : "(sending_rsrvd)",
2078 list_empty(&conn->ibc_tx_queue_nocred) ?
2079 "" : "(sending_nocred)",
2080 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2083 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2084 if (peer_ni->ibp_next_conn == conn)
2085 /* clear next_conn so it won't be used */
2086 peer_ni->ibp_next_conn = NULL;
2087 list_del(&conn->ibc_list);
2088 /* connd (see below) takes over ibc_list's ref */
2090 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2091 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2092 kiblnd_unlink_peer_locked(peer_ni);
2094 /* set/clear error on last conn */
2095 peer_ni->ibp_error = conn->ibc_comms_error;
2098 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2101 kiblnd_dev_can_failover(dev)) {
2102 list_add_tail(&dev->ibd_fail_list,
2103 &kiblnd_data.kib_failed_devs);
2104 wake_up(&kiblnd_data.kib_failover_waitq);
2107 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2109 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2110 wake_up(&kiblnd_data.kib_connd_waitq);
2112 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2116 kiblnd_close_conn(struct kib_conn *conn, int error)
2118 unsigned long flags;
2120 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2122 kiblnd_close_conn_locked(conn, error);
2124 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2128 kiblnd_handle_early_rxs(struct kib_conn *conn)
2130 unsigned long flags;
2133 LASSERT(!in_interrupt());
2134 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2136 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2137 while (!list_empty(&conn->ibc_early_rxs)) {
2138 rx = list_entry(conn->ibc_early_rxs.next,
2139 struct kib_rx, rx_list);
2140 list_del(&rx->rx_list);
2141 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2143 kiblnd_handle_rx(rx);
2145 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2147 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2151 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2153 struct list_head zombies = LIST_HEAD_INIT(zombies);
2154 struct list_head *tmp;
2155 struct list_head *nxt;
2158 spin_lock(&conn->ibc_lock);
2160 list_for_each_safe(tmp, nxt, txs) {
2161 tx = list_entry(tmp, struct kib_tx, tx_list);
2163 if (txs == &conn->ibc_active_txs) {
2164 LASSERT(!tx->tx_queued);
2165 LASSERT(tx->tx_waiting ||
2166 tx->tx_sending != 0);
2167 if (conn->ibc_comms_error == -ETIMEDOUT) {
2168 if (tx->tx_waiting && !tx->tx_sending)
2170 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2171 else if (tx->tx_sending)
2173 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2176 LASSERT(tx->tx_queued);
2177 if (conn->ibc_comms_error == -ETIMEDOUT)
2178 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2180 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2183 tx->tx_status = -ECONNABORTED;
2187 * TODO: This makes an assumption that
2188 * kiblnd_tx_complete() will be called for each tx. If
2189 * that event is dropped we could end up with stale
2190 * connections floating around. We'd like to deal with
2191 * that in a better way.
2193 * Also that means we can exceed the timeout by many
2196 if (tx->tx_sending == 0) {
2198 list_del(&tx->tx_list);
2199 list_add(&tx->tx_list, &zombies);
2203 spin_unlock(&conn->ibc_lock);
2206 * aborting transmits occurs when finalizing the connection.
2207 * The connection is finalized on error.
2208 * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2209 * override the value already set in tx->tx_hstatus above.
2211 kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2215 kiblnd_finalise_conn(struct kib_conn *conn)
2217 LASSERT (!in_interrupt());
2218 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2220 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2221 * for connections that didn't get as far as being connected, because
2222 * rdma_disconnect() does this for free. */
2223 kiblnd_abort_receives(conn);
2225 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2227 /* Complete all tx descs not waiting for sends to complete.
2228 * NB we should be safe from RDMA now that the QP has changed state */
2230 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2231 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2232 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2233 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2234 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2236 kiblnd_handle_early_rxs(conn);
2240 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2243 struct list_head zombies = LIST_HEAD_INIT(zombies);
2244 unsigned long flags;
2246 LASSERT (error != 0);
2247 LASSERT (!in_interrupt());
2249 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2252 LASSERT(peer_ni->ibp_connecting > 0);
2253 peer_ni->ibp_connecting--;
2255 LASSERT (peer_ni->ibp_accepting > 0);
2256 peer_ni->ibp_accepting--;
2259 if (kiblnd_peer_connecting(peer_ni)) {
2260 /* another connection attempt under way... */
2261 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2266 peer_ni->ibp_reconnected = 0;
2267 if (list_empty(&peer_ni->ibp_conns)) {
2268 /* Take peer_ni's blocked transmits to complete with error */
2269 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2271 if (kiblnd_peer_active(peer_ni))
2272 kiblnd_unlink_peer_locked(peer_ni);
2274 peer_ni->ibp_error = error;
2276 /* Can't have blocked transmits if there are connections */
2277 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2280 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2282 kiblnd_peer_notify(peer_ni);
2284 if (list_empty(&zombies))
2287 CNETERR("Deleting messages for %s: connection failed\n",
2288 libcfs_nid2str(peer_ni->ibp_nid));
2290 kiblnd_txlist_done(&zombies, error,
2291 LNET_MSG_STATUS_LOCAL_DROPPED);
2295 kiblnd_connreq_done(struct kib_conn *conn, int status)
2297 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2299 struct list_head txs;
2300 unsigned long flags;
2303 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2305 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2306 libcfs_nid2str(peer_ni->ibp_nid), active,
2307 conn->ibc_version, status);
2309 LASSERT (!in_interrupt());
2310 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2311 peer_ni->ibp_connecting > 0) ||
2312 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2313 peer_ni->ibp_accepting > 0));
2315 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2316 conn->ibc_connvars = NULL;
2319 /* failed to establish connection */
2320 kiblnd_peer_connect_failed(peer_ni, active, status);
2321 kiblnd_finalise_conn(conn);
2325 /* connection established */
2326 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2328 conn->ibc_last_send = ktime_get();
2329 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2330 kiblnd_peer_alive(peer_ni);
2332 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2333 * peer_ni instance... */
2334 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2335 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2336 peer_ni->ibp_reconnected = 0;
2338 peer_ni->ibp_connecting--;
2340 peer_ni->ibp_accepting--;
2342 if (peer_ni->ibp_version == 0) {
2343 peer_ni->ibp_version = conn->ibc_version;
2344 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2347 if (peer_ni->ibp_version != conn->ibc_version ||
2348 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2349 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2350 conn->ibc_incarnation);
2351 peer_ni->ibp_version = conn->ibc_version;
2352 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2355 /* grab pending txs while I have the lock */
2356 INIT_LIST_HEAD(&txs);
2357 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2359 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2360 conn->ibc_comms_error != 0) { /* error has happened already */
2362 /* start to shut down connection */
2363 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2364 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2366 kiblnd_txlist_done(&txs, -ECONNABORTED,
2367 LNET_MSG_STATUS_LOCAL_ERROR);
2372 /* +1 ref for myself, this connection is visible to other threads
2373 * now, refcount of peer:ibp_conns can be released by connection
2374 * close from either a different thread, or the calling of
2375 * kiblnd_check_sends_locked() below. See bz21911 for details.
2377 kiblnd_conn_addref(conn);
2378 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2380 /* Schedule blocked txs
2381 * Note: if we are running with conns_per_peer > 1, these blocked
2382 * txs will all get scheduled to the first connection which gets
2383 * scheduled. We won't be using round robin on this first batch.
2385 spin_lock(&conn->ibc_lock);
2386 while (!list_empty(&txs)) {
2387 tx = list_entry(txs.next, struct kib_tx, tx_list);
2388 list_del(&tx->tx_list);
2390 kiblnd_queue_tx_locked(tx, conn);
2392 kiblnd_check_sends_locked(conn);
2393 spin_unlock(&conn->ibc_lock);
2395 /* schedule blocked rxs */
2396 kiblnd_handle_early_rxs(conn);
2397 kiblnd_conn_decref(conn);
2401 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2405 rc = rdma_reject(cmid, rej, sizeof(*rej));
2408 CWARN("Error %d sending reject\n", rc);
2412 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2414 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2415 struct kib_msg *reqmsg = priv;
2416 struct kib_msg *ackmsg;
2417 struct kib_dev *ibdev;
2418 struct kib_peer_ni *peer_ni;
2419 struct kib_peer_ni *peer2;
2420 struct kib_conn *conn;
2421 struct lnet_ni *ni = NULL;
2422 struct kib_net *net = NULL;
2424 struct rdma_conn_param cp;
2426 int version = IBLND_MSG_VERSION;
2427 unsigned long flags;
2429 struct sockaddr_in *peer_addr;
2430 LASSERT (!in_interrupt());
2432 /* cmid inherits 'context' from the corresponding listener id */
2433 ibdev = cmid->context;
2436 memset(&rej, 0, sizeof(rej));
2437 rej.ibr_magic = IBLND_MSG_MAGIC;
2438 rej.ibr_why = IBLND_REJECT_FATAL;
2439 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2441 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2442 if (*kiblnd_tunables.kib_require_priv_port &&
2443 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2444 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2445 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2446 &ip, ntohs(peer_addr->sin_port));
2450 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2451 CERROR("Short connection request\n");
2455 /* Future protocol version compatibility support! If the
2456 * o2iblnd-specific protocol changes, or when LNET unifies
2457 * protocols over all LNDs, the initial connection will
2458 * negotiate a protocol version. I trap this here to avoid
2459 * console errors; the reject tells the peer_ni which protocol I
2461 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2462 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2464 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2465 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2466 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2468 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2469 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2470 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2473 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2475 CERROR("Can't parse connection request: %d\n", rc);
2479 nid = reqmsg->ibm_srcnid;
2480 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2483 net = (struct kib_net *)ni->ni_data;
2484 rej.ibr_incarnation = net->ibn_incarnation;
2487 if (ni == NULL || /* no matching net */
2488 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2489 net->ibn_dev != ibdev) { /* wrong device */
2490 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2491 "bad dst nid %s\n", libcfs_nid2str(nid),
2492 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2493 ibdev->ibd_ifname, ibdev->ibd_nnets,
2495 libcfs_nid2str(reqmsg->ibm_dstnid));
2500 /* check time stamp as soon as possible */
2501 if (reqmsg->ibm_dststamp != 0 &&
2502 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2503 CWARN("Stale connection request\n");
2504 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2508 /* I can accept peer_ni's version */
2509 version = reqmsg->ibm_version;
2511 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2512 CERROR("Unexpected connreq msg type: %x from %s\n",
2513 reqmsg->ibm_type, libcfs_nid2str(nid));
2517 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2518 kiblnd_msg_queue_size(version, ni)) {
2519 CERROR("Can't accept conn from %s, queue depth too large: "
2520 " %d (<=%d wanted)\n",
2521 libcfs_nid2str(nid),
2522 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2523 kiblnd_msg_queue_size(version, ni));
2525 if (version == IBLND_MSG_VERSION)
2526 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2531 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2532 IBLND_MAX_RDMA_FRAGS) {
2533 CWARN("Can't accept conn from %s (version %x): "
2534 "max_frags %d too large (%d wanted)\n",
2535 libcfs_nid2str(nid), version,
2536 reqmsg->ibm_u.connparams.ibcp_max_frags,
2537 IBLND_MAX_RDMA_FRAGS);
2539 if (version >= IBLND_MSG_VERSION)
2540 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2543 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2544 IBLND_MAX_RDMA_FRAGS &&
2545 net->ibn_fmr_ps == NULL) {
2546 CWARN("Can't accept conn from %s (version %x): "
2547 "max_frags %d incompatible without FMR pool "
2549 libcfs_nid2str(nid), version,
2550 reqmsg->ibm_u.connparams.ibcp_max_frags,
2551 IBLND_MAX_RDMA_FRAGS);
2553 if (version == IBLND_MSG_VERSION)
2554 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2559 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2560 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2561 libcfs_nid2str(nid),
2562 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2567 /* assume 'nid' is a new peer_ni; create */
2568 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2570 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2571 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2575 /* We have validated the peer's parameters so use those */
2576 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2577 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2579 write_lock_irqsave(g_lock, flags);
2581 peer2 = kiblnd_find_peer_locked(ni, nid);
2582 if (peer2 != NULL) {
2583 if (peer2->ibp_version == 0) {
2584 peer2->ibp_version = version;
2585 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2588 /* not the guy I've talked with */
2589 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2590 peer2->ibp_version != version) {
2591 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2593 if (kiblnd_peer_active(peer2)) {
2594 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2595 peer2->ibp_version = version;
2597 write_unlock_irqrestore(g_lock, flags);
2599 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2600 libcfs_nid2str(nid), peer2->ibp_version, version,
2601 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2603 kiblnd_peer_decref(peer_ni);
2604 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2608 /* Tie-break connection race in favour of the higher NID.
2609 * If we keep running into a race condition multiple times,
2610 * we have to assume that the connection attempt with the
2611 * higher NID is stuck in a connecting state and will never
2612 * recover. As such, we pass through this if-block and let
2613 * the lower NID connection win so we can move forward.
2615 if (peer2->ibp_connecting != 0 &&
2616 nid < ni->ni_nid && peer2->ibp_races <
2617 MAX_CONN_RACES_BEFORE_ABORT) {
2619 write_unlock_irqrestore(g_lock, flags);
2621 CDEBUG(D_NET, "Conn race %s\n",
2622 libcfs_nid2str(peer2->ibp_nid));
2624 kiblnd_peer_decref(peer_ni);
2625 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2628 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2629 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2630 libcfs_nid2str(peer2->ibp_nid),
2631 MAX_CONN_RACES_BEFORE_ABORT);
2633 * passive connection is allowed even this peer_ni is waiting for
2636 peer2->ibp_reconnecting = 0;
2637 peer2->ibp_races = 0;
2638 peer2->ibp_accepting++;
2639 kiblnd_peer_addref(peer2);
2641 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2642 * so copy validated parameters since we now know what the
2643 * peer_ni's limits are */
2644 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2645 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2647 write_unlock_irqrestore(g_lock, flags);
2648 kiblnd_peer_decref(peer_ni);
2651 /* Brand new peer_ni */
2652 LASSERT (peer_ni->ibp_accepting == 0);
2653 LASSERT (peer_ni->ibp_version == 0 &&
2654 peer_ni->ibp_incarnation == 0);
2656 peer_ni->ibp_accepting = 1;
2657 peer_ni->ibp_version = version;
2658 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2660 /* I have a ref on ni that prevents it being shutdown */
2661 LASSERT (net->ibn_shutdown == 0);
2663 kiblnd_peer_addref(peer_ni);
2664 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
2666 write_unlock_irqrestore(g_lock, flags);
2669 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2671 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2672 kiblnd_peer_decref(peer_ni);
2673 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2677 /* conn now "owns" cmid, so I return success from here on to ensure the
2678 * CM callback doesn't destroy cmid. */
2679 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2680 conn->ibc_credits = conn->ibc_queue_depth;
2681 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2682 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2683 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2685 ackmsg = &conn->ibc_connvars->cv_msg;
2686 memset(ackmsg, 0, sizeof(*ackmsg));
2688 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2689 sizeof(ackmsg->ibm_u.connparams));
2690 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2691 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2692 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2694 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2696 memset(&cp, 0, sizeof(cp));
2697 cp.private_data = ackmsg;
2698 cp.private_data_len = ackmsg->ibm_nob;
2699 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2700 cp.initiator_depth = 0;
2701 cp.flow_control = 1;
2702 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2703 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2705 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2707 rc = rdma_accept(cmid, &cp);
2709 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2710 rej.ibr_version = version;
2711 rej.ibr_why = IBLND_REJECT_FATAL;
2713 kiblnd_reject(cmid, &rej);
2714 kiblnd_connreq_done(conn, rc);
2715 kiblnd_conn_decref(conn);
2723 rej.ibr_cp.ibcp_queue_depth =
2724 kiblnd_msg_queue_size(version, ni);
2725 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2729 rej.ibr_version = version;
2730 kiblnd_reject(cmid, &rej);
2732 return -ECONNREFUSED;
2736 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2737 u64 incarnation, int why, struct kib_connparams *cp)
2739 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2740 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2742 int msg_size = IBLND_MSG_SIZE;
2746 unsigned long flags;
2748 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2749 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2752 msg_size = cp->ibcp_max_msg_size;
2753 frag_num = cp->ibcp_max_frags;
2754 queue_dep = cp->ibcp_queue_depth;
2757 write_lock_irqsave(glock, flags);
2758 /* retry connection if it's still needed and no other connection
2759 * attempts (active or passive) are in progress
2760 * NB: reconnect is still needed even when ibp_tx_queue is
2761 * empty if ibp_version != version because reconnect may be
2762 * initiated by kiblnd_query() */
2763 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2764 peer_ni->ibp_version != version) &&
2765 peer_ni->ibp_connecting &&
2766 peer_ni->ibp_accepting == 0;
2777 case IBLND_REJECT_RDMA_FRAGS: {
2778 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2781 reason = "can't negotiate max frags";
2784 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2785 #ifdef HAVE_IB_GET_DMA_MR
2787 * This check only makes sense if the kernel supports global
2788 * memory registration. Otherwise, map_on_demand will never == 0
2790 if (!tunables->lnd_map_on_demand) {
2791 reason = "map_on_demand must be enabled";
2795 if (conn->ibc_max_frags <= frag_num) {
2796 reason = "unsupported max frags";
2800 peer_ni->ibp_max_frags = frag_num;
2801 reason = "rdma fragments";
2804 case IBLND_REJECT_MSG_QUEUE_SIZE:
2806 reason = "can't negotiate queue depth";
2809 if (conn->ibc_queue_depth <= queue_dep) {
2810 reason = "unsupported queue depth";
2814 peer_ni->ibp_queue_depth = queue_dep;
2815 reason = "queue depth";
2818 case IBLND_REJECT_CONN_STALE:
2822 case IBLND_REJECT_CONN_RACE:
2823 reason = "conn race";
2826 case IBLND_REJECT_CONN_UNCOMPAT:
2827 reason = "version negotiation";
2830 case IBLND_REJECT_INVALID_SRV_ID:
2831 reason = "invalid service id";
2835 conn->ibc_reconnect = 1;
2836 peer_ni->ibp_reconnecting++;
2837 peer_ni->ibp_version = version;
2838 if (incarnation != 0)
2839 peer_ni->ibp_incarnation = incarnation;
2841 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2843 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2844 libcfs_nid2str(peer_ni->ibp_nid),
2845 reconnect ? "reconnect" : "don't reconnect",
2846 reason, IBLND_MSG_VERSION, version, msg_size,
2847 conn->ibc_queue_depth, queue_dep,
2848 conn->ibc_max_frags, frag_num);
2850 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2851 * while destroying the zombie
2856 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2858 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2860 LASSERT (!in_interrupt());
2861 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2864 case IB_CM_REJ_STALE_CONN:
2865 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2866 IBLND_REJECT_CONN_STALE, NULL);
2869 case IB_CM_REJ_INVALID_SERVICE_ID:
2870 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2871 IBLND_REJECT_INVALID_SRV_ID, NULL);
2872 CNETERR("%s rejected: no listener at %d\n",
2873 libcfs_nid2str(peer_ni->ibp_nid),
2874 *kiblnd_tunables.kib_service);
2877 case IB_CM_REJ_CONSUMER_DEFINED:
2878 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2879 struct kib_rej *rej = priv;
2880 struct kib_connparams *cp = NULL;
2882 __u64 incarnation = -1;
2884 /* NB. default incarnation is -1 because:
2885 * a) V1 will ignore dst incarnation in connreq.
2886 * b) V2 will provide incarnation while rejecting me,
2887 * -1 will be overwrote.
2889 * if I try to connect to a V1 peer_ni with V2 protocol,
2890 * it rejected me then upgrade to V2, I have no idea
2891 * about the upgrading and try to reconnect with V1,
2892 * in this case upgraded V2 can find out I'm trying to
2893 * talk to the old guy and reject me(incarnation is -1).
2896 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2897 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2898 __swab32s(&rej->ibr_magic);
2899 __swab16s(&rej->ibr_version);
2903 if (priv_nob >= sizeof(struct kib_rej) &&
2904 rej->ibr_version > IBLND_MSG_VERSION_1) {
2905 /* priv_nob is always 148 in current version
2906 * of OFED, so we still need to check version.
2907 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2911 __swab64s(&rej->ibr_incarnation);
2912 __swab16s(&cp->ibcp_queue_depth);
2913 __swab16s(&cp->ibcp_max_frags);
2914 __swab32s(&cp->ibcp_max_msg_size);
2917 incarnation = rej->ibr_incarnation;
2920 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2921 rej->ibr_magic != LNET_PROTO_MAGIC) {
2922 CERROR("%s rejected: consumer defined fatal error\n",
2923 libcfs_nid2str(peer_ni->ibp_nid));
2927 if (rej->ibr_version != IBLND_MSG_VERSION &&
2928 rej->ibr_version != IBLND_MSG_VERSION_1) {
2929 CERROR("%s rejected: o2iblnd version %x error\n",
2930 libcfs_nid2str(peer_ni->ibp_nid),
2935 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2936 rej->ibr_version == IBLND_MSG_VERSION_1) {
2937 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2938 libcfs_nid2str(peer_ni->ibp_nid), rej->ibr_version);
2940 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2941 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2944 switch (rej->ibr_why) {
2945 case IBLND_REJECT_CONN_RACE:
2946 case IBLND_REJECT_CONN_STALE:
2947 case IBLND_REJECT_CONN_UNCOMPAT:
2948 case IBLND_REJECT_MSG_QUEUE_SIZE:
2949 case IBLND_REJECT_RDMA_FRAGS:
2950 kiblnd_check_reconnect(conn, rej->ibr_version,
2951 incarnation, rej->ibr_why, cp);
2954 case IBLND_REJECT_NO_RESOURCES:
2955 CERROR("%s rejected: o2iblnd no resources\n",
2956 libcfs_nid2str(peer_ni->ibp_nid));
2959 case IBLND_REJECT_FATAL:
2960 CERROR("%s rejected: o2iblnd fatal error\n",
2961 libcfs_nid2str(peer_ni->ibp_nid));
2965 CERROR("%s rejected: o2iblnd reason %d\n",
2966 libcfs_nid2str(peer_ni->ibp_nid),
2974 CNETERR("%s rejected: reason %d, size %d\n",
2975 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
2979 kiblnd_connreq_done(conn, -ECONNREFUSED);
2983 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
2985 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2986 struct lnet_ni *ni = peer_ni->ibp_ni;
2987 struct kib_net *net = ni->ni_data;
2988 struct kib_msg *msg = priv;
2989 int ver = conn->ibc_version;
2990 int rc = kiblnd_unpack_msg(msg, priv_nob);
2991 unsigned long flags;
2993 LASSERT (net != NULL);
2996 CERROR("Can't unpack connack from %s: %d\n",
2997 libcfs_nid2str(peer_ni->ibp_nid), rc);
3001 if (msg->ibm_type != IBLND_MSG_CONNACK) {
3002 CERROR("Unexpected message %d from %s\n",
3003 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
3008 if (ver != msg->ibm_version) {
3009 CERROR("%s replied version %x is different with "
3010 "requested version %x\n",
3011 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
3016 if (msg->ibm_u.connparams.ibcp_queue_depth >
3017 conn->ibc_queue_depth) {
3018 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
3019 libcfs_nid2str(peer_ni->ibp_nid),
3020 msg->ibm_u.connparams.ibcp_queue_depth,
3021 conn->ibc_queue_depth);
3026 if (msg->ibm_u.connparams.ibcp_max_frags >
3027 conn->ibc_max_frags) {
3028 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3029 libcfs_nid2str(peer_ni->ibp_nid),
3030 msg->ibm_u.connparams.ibcp_max_frags,
3031 conn->ibc_max_frags);
3036 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3037 CERROR("%s max message size %d too big (%d max)\n",
3038 libcfs_nid2str(peer_ni->ibp_nid),
3039 msg->ibm_u.connparams.ibcp_max_msg_size,
3045 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3046 if (msg->ibm_dstnid == ni->ni_nid &&
3047 msg->ibm_dststamp == net->ibn_incarnation)
3051 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3054 CERROR("Bad connection reply from %s, rc = %d, "
3055 "version: %x max_frags: %d\n",
3056 libcfs_nid2str(peer_ni->ibp_nid), rc,
3057 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3061 conn->ibc_incarnation = msg->ibm_srcstamp;
3062 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3063 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3064 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3065 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3066 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3067 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3069 kiblnd_connreq_done(conn, 0);
3073 /* NB My QP has already established itself, so I handle anything going
3074 * wrong here by setting ibc_comms_error.
3075 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3076 * immediately tears it down. */
3079 conn->ibc_comms_error = rc;
3080 kiblnd_connreq_done(conn, 0);
3084 kiblnd_active_connect(struct rdma_cm_id *cmid)
3086 struct kib_peer_ni *peer_ni = cmid->context;
3087 struct kib_conn *conn;
3088 struct kib_msg *msg;
3089 struct rdma_conn_param cp;
3092 unsigned long flags;
3095 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3097 incarnation = peer_ni->ibp_incarnation;
3098 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3099 peer_ni->ibp_version;
3101 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3103 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3106 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3107 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3111 /* conn "owns" cmid now, so I return success from here on to ensure the
3112 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3115 msg = &conn->ibc_connvars->cv_msg;
3117 memset(msg, 0, sizeof(*msg));
3118 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3119 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3120 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3121 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3123 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3124 0, peer_ni->ibp_nid, incarnation);
3126 memset(&cp, 0, sizeof(cp));
3127 cp.private_data = msg;
3128 cp.private_data_len = msg->ibm_nob;
3129 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3130 cp.initiator_depth = 0;
3131 cp.flow_control = 1;
3132 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3133 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3135 LASSERT(cmid->context == (void *)conn);
3136 LASSERT(conn->ibc_cmid == cmid);
3138 rc = rdma_connect(cmid, &cp);
3140 CERROR("Can't connect to %s: %d\n",
3141 libcfs_nid2str(peer_ni->ibp_nid), rc);
3142 kiblnd_connreq_done(conn, rc);
3143 kiblnd_conn_decref(conn);
3150 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3152 struct kib_peer_ni *peer_ni;
3153 struct kib_conn *conn;
3156 switch (event->event) {
3158 CERROR("Unexpected event: %d, status: %d\n",
3159 event->event, event->status);
3162 case RDMA_CM_EVENT_CONNECT_REQUEST:
3163 /* destroy cmid on failure */
3164 rc = kiblnd_passive_connect(cmid,
3165 (void *)KIBLND_CONN_PARAM(event),
3166 KIBLND_CONN_PARAM_LEN(event));
3167 CDEBUG(D_NET, "connreq: %d\n", rc);
3170 case RDMA_CM_EVENT_ADDR_ERROR:
3171 peer_ni = cmid->context;
3172 CNETERR("%s: ADDR ERROR %d\n",
3173 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3174 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3175 kiblnd_peer_decref(peer_ni);
3176 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3178 case RDMA_CM_EVENT_ADDR_RESOLVED:
3179 peer_ni = cmid->context;
3181 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3182 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3184 if (event->status != 0) {
3185 CNETERR("Can't resolve address for %s: %d\n",
3186 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3189 rc = rdma_resolve_route(
3190 cmid, lnet_get_lnd_timeout() * 1000);
3192 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3193 struct kib_dev *dev = net->ibn_dev;
3195 CDEBUG(D_NET, "%s: connection bound to "\
3197 libcfs_nid2str(peer_ni->ibp_nid),
3199 &dev->ibd_ifip, cmid->device->name);
3204 /* Can't initiate route resolution */
3205 CERROR("Can't resolve route for %s: %d\n",
3206 libcfs_nid2str(peer_ni->ibp_nid), rc);
3208 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3209 kiblnd_peer_decref(peer_ni);
3210 return rc; /* rc != 0 destroys cmid */
3212 case RDMA_CM_EVENT_ROUTE_ERROR:
3213 peer_ni = cmid->context;
3214 CNETERR("%s: ROUTE ERROR %d\n",
3215 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3216 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3217 kiblnd_peer_decref(peer_ni);
3218 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3220 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3221 peer_ni = cmid->context;
3222 CDEBUG(D_NET,"%s Route resolved: %d\n",
3223 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3225 if (event->status == 0)
3226 return kiblnd_active_connect(cmid);
3228 CNETERR("Can't resolve route for %s: %d\n",
3229 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3230 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3231 kiblnd_peer_decref(peer_ni);
3232 return event->status; /* rc != 0 destroys cmid */
3234 case RDMA_CM_EVENT_UNREACHABLE:
3235 conn = cmid->context;
3236 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3237 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3238 CNETERR("%s: UNREACHABLE %d\n",
3239 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3240 kiblnd_connreq_done(conn, -ENETDOWN);
3241 kiblnd_conn_decref(conn);
3244 case RDMA_CM_EVENT_CONNECT_ERROR:
3245 conn = cmid->context;
3246 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3247 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3248 CNETERR("%s: CONNECT ERROR %d\n",
3249 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3250 kiblnd_connreq_done(conn, -ENOTCONN);
3251 kiblnd_conn_decref(conn);
3254 case RDMA_CM_EVENT_REJECTED:
3255 conn = cmid->context;
3256 switch (conn->ibc_state) {
3260 case IBLND_CONN_PASSIVE_WAIT:
3261 CERROR ("%s: REJECTED %d\n",
3262 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3264 kiblnd_connreq_done(conn, -ECONNRESET);
3267 case IBLND_CONN_ACTIVE_CONNECT:
3268 kiblnd_rejected(conn, event->status,
3269 (void *)KIBLND_CONN_PARAM(event),
3270 KIBLND_CONN_PARAM_LEN(event));
3273 kiblnd_conn_decref(conn);
3276 case RDMA_CM_EVENT_ESTABLISHED:
3277 conn = cmid->context;
3278 switch (conn->ibc_state) {
3282 case IBLND_CONN_PASSIVE_WAIT:
3283 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3284 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3285 kiblnd_connreq_done(conn, 0);
3288 case IBLND_CONN_ACTIVE_CONNECT:
3289 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3290 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3291 kiblnd_check_connreply(conn,
3292 (void *)KIBLND_CONN_PARAM(event),
3293 KIBLND_CONN_PARAM_LEN(event));
3296 /* net keeps its ref on conn! */
3299 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3300 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3303 case RDMA_CM_EVENT_DISCONNECTED:
3304 conn = cmid->context;
3305 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3306 CERROR("%s DISCONNECTED\n",
3307 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3308 kiblnd_connreq_done(conn, -ECONNRESET);
3310 kiblnd_close_conn(conn, 0);
3312 kiblnd_conn_decref(conn);
3313 cmid->context = NULL;
3316 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3317 LCONSOLE_ERROR_MSG(0x131,
3318 "Received notification of device removal\n"
3319 "Please shutdown LNET to allow this to proceed\n");
3320 /* Can't remove network from underneath LNET for now, so I have
3324 case RDMA_CM_EVENT_ADDR_CHANGE:
3325 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3331 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3334 struct list_head *ttmp;
3336 list_for_each(ttmp, txs) {
3337 tx = list_entry(ttmp, struct kib_tx, tx_list);
3339 if (txs != &conn->ibc_active_txs) {
3340 LASSERT(tx->tx_queued);
3342 LASSERT(!tx->tx_queued);
3343 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3346 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3347 CERROR("Timed out tx: %s, %lld seconds\n",
3348 kiblnd_queue2str(conn, txs),
3349 ktime_ms_delta(ktime_get(),
3350 tx->tx_deadline) / MSEC_PER_SEC);
3359 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3361 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3362 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3363 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3364 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3365 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3369 kiblnd_check_conns (int idx)
3371 struct list_head closes = LIST_HEAD_INIT(closes);
3372 struct list_head checksends = LIST_HEAD_INIT(checksends);
3373 struct list_head timedout_txs = LIST_HEAD_INIT(timedout_txs);
3374 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3375 struct list_head *ptmp;
3376 struct kib_peer_ni *peer_ni;
3377 struct kib_conn *conn;
3378 struct kib_tx *tx, *tx_tmp;
3379 struct list_head *ctmp;
3380 unsigned long flags;
3382 /* NB. We expect to have a look at all the peers and not find any
3383 * RDMAs to time out, so we just use a shared lock while we
3385 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3387 list_for_each(ptmp, peers) {
3388 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
3390 /* Check tx_deadline */
3391 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3392 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3393 CWARN("Timed out tx for %s: %lld seconds\n",
3394 libcfs_nid2str(peer_ni->ibp_nid),
3395 ktime_ms_delta(ktime_get(),
3396 tx->tx_deadline) / MSEC_PER_SEC);
3397 list_move(&tx->tx_list, &timedout_txs);
3401 list_for_each(ctmp, &peer_ni->ibp_conns) {
3405 conn = list_entry(ctmp, struct kib_conn, ibc_list);
3407 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3409 spin_lock(&conn->ibc_lock);
3411 sendnoop = kiblnd_need_noop(conn);
3412 timedout = kiblnd_conn_timed_out_locked(conn);
3413 if (!sendnoop && !timedout) {
3414 spin_unlock(&conn->ibc_lock);
3419 CERROR("Timed out RDMA with %s (%lld): "
3420 "c: %u, oc: %u, rc: %u\n",
3421 libcfs_nid2str(peer_ni->ibp_nid),
3422 ktime_get_seconds() - peer_ni->ibp_last_alive,
3424 conn->ibc_outstanding_credits,
3425 conn->ibc_reserved_credits);
3426 list_add(&conn->ibc_connd_list, &closes);
3428 list_add(&conn->ibc_connd_list, &checksends);
3430 /* +ref for 'closes' or 'checksends' */
3431 kiblnd_conn_addref(conn);
3433 spin_unlock(&conn->ibc_lock);
3437 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3439 if (!list_empty(&timedout_txs))
3440 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3441 LNET_MSG_STATUS_LOCAL_TIMEOUT);
3443 /* Handle timeout by closing the whole
3444 * connection. We can only be sure RDMA activity
3445 * has ceased once the QP has been modified. */
3446 while (!list_empty(&closes)) {
3447 conn = list_entry(closes.next,
3448 struct kib_conn, ibc_connd_list);
3449 list_del(&conn->ibc_connd_list);
3450 kiblnd_close_conn(conn, -ETIMEDOUT);
3451 kiblnd_conn_decref(conn);
3454 /* In case we have enough credits to return via a
3455 * NOOP, but there were no non-blocking tx descs
3456 * free to do it last time... */
3457 while (!list_empty(&checksends)) {
3458 conn = list_entry(checksends.next,
3459 struct kib_conn, ibc_connd_list);
3460 list_del(&conn->ibc_connd_list);
3462 spin_lock(&conn->ibc_lock);
3463 kiblnd_check_sends_locked(conn);
3464 spin_unlock(&conn->ibc_lock);
3466 kiblnd_conn_decref(conn);
3471 kiblnd_disconnect_conn(struct kib_conn *conn)
3473 LASSERT (!in_interrupt());
3474 LASSERT (current == kiblnd_data.kib_connd);
3475 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3477 rdma_disconnect(conn->ibc_cmid);
3478 kiblnd_finalise_conn(conn);
3480 kiblnd_peer_notify(conn->ibc_peer);
3484 * High-water for reconnection to the same peer_ni, reconnection attempt should
3485 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3487 #define KIB_RECONN_HIGH_RACE 10
3489 * Allow connd to take a break and handle other things after consecutive
3490 * reconnection attemps.
3492 #define KIB_RECONN_BREAK 100
3495 kiblnd_connd (void *arg)
3497 spinlock_t *lock= &kiblnd_data.kib_connd_lock;
3498 wait_queue_entry_t wait;
3499 unsigned long flags;
3500 struct kib_conn *conn;
3505 unsigned long deadline = jiffies;
3507 cfs_block_allsigs();
3509 init_waitqueue_entry(&wait, current);
3510 kiblnd_data.kib_connd = current;
3512 spin_lock_irqsave(lock, flags);
3514 while (!kiblnd_data.kib_shutdown) {
3519 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3520 struct kib_peer_ni *peer_ni = NULL;
3522 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3523 struct kib_conn, ibc_list);
3524 list_del(&conn->ibc_list);
3525 if (conn->ibc_reconnect) {
3526 peer_ni = conn->ibc_peer;
3527 kiblnd_peer_addref(peer_ni);
3530 spin_unlock_irqrestore(lock, flags);
3533 kiblnd_destroy_conn(conn);
3535 spin_lock_irqsave(lock, flags);
3537 LIBCFS_FREE(conn, sizeof(*conn));
3541 conn->ibc_peer = peer_ni;
3542 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3543 list_add_tail(&conn->ibc_list,
3544 &kiblnd_data.kib_reconn_list);
3546 list_add_tail(&conn->ibc_list,
3547 &kiblnd_data.kib_reconn_wait);
3550 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3551 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3552 struct kib_conn, ibc_list);
3553 list_del(&conn->ibc_list);
3555 spin_unlock_irqrestore(lock, flags);
3558 kiblnd_disconnect_conn(conn);
3559 kiblnd_conn_decref(conn);
3561 spin_lock_irqsave(lock, flags);
3564 while (reconn < KIB_RECONN_BREAK) {
3565 if (kiblnd_data.kib_reconn_sec !=
3566 ktime_get_real_seconds()) {
3567 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3568 list_splice_init(&kiblnd_data.kib_reconn_wait,
3569 &kiblnd_data.kib_reconn_list);
3572 if (list_empty(&kiblnd_data.kib_reconn_list))
3575 conn = list_entry(kiblnd_data.kib_reconn_list.next,
3576 struct kib_conn, ibc_list);
3577 list_del(&conn->ibc_list);
3579 spin_unlock_irqrestore(lock, flags);
3582 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3583 kiblnd_peer_decref(conn->ibc_peer);
3584 LIBCFS_FREE(conn, sizeof(*conn));
3586 spin_lock_irqsave(lock, flags);
3589 /* careful with the jiffy wrap... */
3590 timeout = (int)(deadline - jiffies);
3594 int chunk = kiblnd_data.kib_peer_hash_size;
3595 unsigned int lnd_timeout;
3597 spin_unlock_irqrestore(lock, flags);
3600 /* Time to check for RDMA timeouts on a few more
3601 * peers: I do checks every 'p' seconds on a
3602 * proportion of the peer_ni table and I need to check
3603 * every connection 'n' times within a timeout
3604 * interval, to ensure I detect a timeout on any
3605 * connection within (n+1)/n times the timeout
3608 lnd_timeout = lnet_get_lnd_timeout();
3609 if (lnd_timeout > n * p)
3610 chunk = (chunk * n * p) / lnd_timeout;
3614 for (i = 0; i < chunk; i++) {
3615 kiblnd_check_conns(peer_index);
3616 peer_index = (peer_index + 1) %
3617 kiblnd_data.kib_peer_hash_size;
3620 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3621 spin_lock_irqsave(lock, flags);
3627 /* Nothing to do for 'timeout' */
3628 set_current_state(TASK_INTERRUPTIBLE);
3629 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3630 spin_unlock_irqrestore(lock, flags);
3632 schedule_timeout(timeout);
3634 set_current_state(TASK_RUNNING);
3635 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3636 spin_lock_irqsave(lock, flags);
3639 spin_unlock_irqrestore(lock, flags);
3641 kiblnd_thread_fini();
3646 kiblnd_qp_event(struct ib_event *event, void *arg)
3648 struct kib_conn *conn = arg;
3650 switch (event->event) {
3651 case IB_EVENT_COMM_EST:
3652 CDEBUG(D_NET, "%s established\n",
3653 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3654 /* We received a packet but connection isn't established
3655 * probably handshake packet was lost, so free to
3656 * force make connection established */
3657 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3660 case IB_EVENT_PORT_ERR:
3661 case IB_EVENT_DEVICE_FATAL:
3662 CERROR("Fatal device error for NI %s\n",
3663 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3664 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3667 case IB_EVENT_PORT_ACTIVE:
3668 CERROR("Port reactivated for NI %s\n",
3669 libcfs_nid2str(conn->ibc_peer->ibp_ni->ni_nid));
3670 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3674 CERROR("%s: Async QP event type %d\n",
3675 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3681 kiblnd_complete (struct ib_wc *wc)
3683 switch (kiblnd_wreqid2type(wc->wr_id)) {
3688 if (wc->status != IB_WC_SUCCESS &&
3689 wc->status != IB_WC_WR_FLUSH_ERR)
3690 CNETERR("FastReg failed: %d\n", wc->status);
3693 case IBLND_WID_RDMA:
3694 /* We only get RDMA completion notification if it fails. All
3695 * subsequent work items, including the final SEND will fail
3696 * too. However we can't print out any more info about the
3697 * failing RDMA because 'tx' might be back on the idle list or
3698 * even reused already if we didn't manage to post all our work
3700 CNETERR("RDMA (tx: %p) failed: %d\n",
3701 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3705 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3709 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3716 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3718 /* NB I'm not allowed to schedule this conn once its refcount has
3719 * reached 0. Since fundamentally I'm racing with scheduler threads
3720 * consuming my CQ I could be called after all completions have
3721 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3722 * and this CQ is about to be destroyed so I NOOP. */
3723 struct kib_conn *conn = arg;
3724 struct kib_sched_info *sched = conn->ibc_sched;
3725 unsigned long flags;
3727 LASSERT(cq == conn->ibc_cq);
3729 spin_lock_irqsave(&sched->ibs_lock, flags);
3731 conn->ibc_ready = 1;
3733 if (!conn->ibc_scheduled &&
3734 (conn->ibc_nrx > 0 ||
3735 conn->ibc_nsends_posted > 0)) {
3736 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3737 conn->ibc_scheduled = 1;
3738 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3740 if (waitqueue_active(&sched->ibs_waitq))
3741 wake_up(&sched->ibs_waitq);
3744 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3748 kiblnd_cq_event(struct ib_event *event, void *arg)
3750 struct kib_conn *conn = arg;
3752 CERROR("%s: async CQ event type %d\n",
3753 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3757 kiblnd_scheduler(void *arg)
3759 long id = (long)arg;
3760 struct kib_sched_info *sched;
3761 struct kib_conn *conn;
3762 wait_queue_entry_t wait;
3763 unsigned long flags;
3769 cfs_block_allsigs();
3771 init_waitqueue_entry(&wait, current);
3773 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3775 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3777 CWARN("Unable to bind on CPU partition %d, please verify "
3778 "whether all CPUs are healthy and reload modules if "
3779 "necessary, otherwise your system might under risk of "
3780 "low performance\n", sched->ibs_cpt);
3783 spin_lock_irqsave(&sched->ibs_lock, flags);
3785 while (!kiblnd_data.kib_shutdown) {
3786 if (busy_loops++ >= IBLND_RESCHED) {
3787 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3792 spin_lock_irqsave(&sched->ibs_lock, flags);
3797 if (!list_empty(&sched->ibs_conns)) {
3798 conn = list_entry(sched->ibs_conns.next,
3799 struct kib_conn, ibc_sched_list);
3800 /* take over kib_sched_conns' ref on conn... */
3801 LASSERT(conn->ibc_scheduled);
3802 list_del(&conn->ibc_sched_list);
3803 conn->ibc_ready = 0;
3805 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3807 wc.wr_id = IBLND_WID_INVAL;
3809 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3811 rc = ib_req_notify_cq(conn->ibc_cq,
3814 CWARN("%s: ib_req_notify_cq failed: %d, "
3815 "closing connection\n",
3816 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3817 kiblnd_close_conn(conn, -EIO);
3818 kiblnd_conn_decref(conn);
3819 spin_lock_irqsave(&sched->ibs_lock,
3824 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3827 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3829 "ib_poll_cq (rc: %d) returned invalid "
3830 "wr_id, opcode %d, status: %d, "
3831 "vendor_err: %d, conn: %s status: %d\n"
3832 "please upgrade firmware and OFED or "
3833 "contact vendor.\n", rc,
3834 wc.opcode, wc.status, wc.vendor_err,
3835 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3841 CWARN("%s: ib_poll_cq failed: %d, "
3842 "closing connection\n",
3843 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3845 kiblnd_close_conn(conn, -EIO);
3846 kiblnd_conn_decref(conn);
3847 spin_lock_irqsave(&sched->ibs_lock, flags);
3851 spin_lock_irqsave(&sched->ibs_lock, flags);
3853 if (rc != 0 || conn->ibc_ready) {
3854 /* There may be another completion waiting; get
3855 * another scheduler to check while I handle
3857 /* +1 ref for sched_conns */
3858 kiblnd_conn_addref(conn);
3859 list_add_tail(&conn->ibc_sched_list,
3861 if (waitqueue_active(&sched->ibs_waitq))
3862 wake_up(&sched->ibs_waitq);
3864 conn->ibc_scheduled = 0;
3868 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3869 kiblnd_complete(&wc);
3871 spin_lock_irqsave(&sched->ibs_lock, flags);
3874 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3881 set_current_state(TASK_INTERRUPTIBLE);
3882 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3883 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3888 remove_wait_queue(&sched->ibs_waitq, &wait);
3889 set_current_state(TASK_RUNNING);
3890 spin_lock_irqsave(&sched->ibs_lock, flags);
3893 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3895 kiblnd_thread_fini();
3900 kiblnd_failover_thread(void *arg)
3902 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3903 struct kib_dev *dev;
3904 wait_queue_entry_t wait;
3905 unsigned long flags;
3908 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3910 cfs_block_allsigs();
3912 init_waitqueue_entry(&wait, current);
3913 write_lock_irqsave(glock, flags);
3915 while (!kiblnd_data.kib_shutdown) {
3916 int do_failover = 0;
3919 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3921 if (ktime_get_seconds() < dev->ibd_next_failover)
3928 list_del_init(&dev->ibd_fail_list);
3929 dev->ibd_failover = 1;
3930 write_unlock_irqrestore(glock, flags);
3932 rc = kiblnd_dev_failover(dev);
3934 write_lock_irqsave(glock, flags);
3936 LASSERT (dev->ibd_failover);
3937 dev->ibd_failover = 0;
3938 if (rc >= 0) { /* Device is OK or failover succeed */
3939 dev->ibd_next_failover = ktime_get_seconds() + 3;
3943 /* failed to failover, retry later */
3944 dev->ibd_next_failover = ktime_get_seconds() +
3945 min(dev->ibd_failed_failover, 10);
3946 if (kiblnd_dev_can_failover(dev)) {
3947 list_add_tail(&dev->ibd_fail_list,
3948 &kiblnd_data.kib_failed_devs);
3954 /* long sleep if no more pending failover */
3955 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3957 set_current_state(TASK_INTERRUPTIBLE);
3958 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3959 write_unlock_irqrestore(glock, flags);
3961 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3962 cfs_time_seconds(1));
3963 set_current_state(TASK_RUNNING);
3964 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3965 write_lock_irqsave(glock, flags);
3967 if (!long_sleep || rc != 0)
3970 /* have a long sleep, routine check all active devices,
3971 * we need checking like this because if there is not active
3972 * connection on the dev and no SEND from local, we may listen
3973 * on wrong HCA for ever while there is a bonding failover */
3974 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3975 if (kiblnd_dev_can_failover(dev)) {
3976 list_add_tail(&dev->ibd_fail_list,
3977 &kiblnd_data.kib_failed_devs);
3982 write_unlock_irqrestore(glock, flags);
3984 kiblnd_thread_fini();