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 tx->tx_hstatus = hstatus;
115 static struct kib_tx *
116 kiblnd_get_idle_tx(struct lnet_ni *ni, lnet_nid_t target)
118 struct kib_net *net = ni->ni_data;
119 struct list_head *node;
121 struct kib_tx_poolset *tps;
123 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target, ni)];
124 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
127 tx = container_of(node, struct kib_tx, tx_list);
129 LASSERT (tx->tx_nwrq == 0);
130 LASSERT (!tx->tx_queued);
131 LASSERT (tx->tx_sending == 0);
132 LASSERT (!tx->tx_waiting);
133 LASSERT (tx->tx_status == 0);
134 LASSERT (tx->tx_conn == NULL);
135 LASSERT (tx->tx_lntmsg[0] == NULL);
136 LASSERT (tx->tx_lntmsg[1] == NULL);
137 LASSERT (tx->tx_nfrags == 0);
140 tx->tx_hstatus = LNET_MSG_STATUS_OK;
146 kiblnd_drop_rx(struct kib_rx *rx)
148 struct kib_conn *conn = rx->rx_conn;
149 struct kib_sched_info *sched = conn->ibc_sched;
152 spin_lock_irqsave(&sched->ibs_lock, flags);
153 LASSERT(conn->ibc_nrx > 0);
155 spin_unlock_irqrestore(&sched->ibs_lock, flags);
157 kiblnd_conn_decref(conn);
161 kiblnd_post_rx(struct kib_rx *rx, int credit)
163 struct kib_conn *conn = rx->rx_conn;
164 struct kib_net *net = conn->ibc_peer->ibp_ni->ni_data;
165 struct ib_recv_wr *bad_wrq = NULL;
166 #ifdef HAVE_IB_GET_DMA_MR
167 struct ib_mr *mr = conn->ibc_hdev->ibh_mrs;
171 LASSERT (net != NULL);
172 LASSERT (!in_interrupt());
173 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
174 credit == IBLND_POSTRX_PEER_CREDIT ||
175 credit == IBLND_POSTRX_RSRVD_CREDIT);
176 #ifdef HAVE_IB_GET_DMA_MR
179 rx->rx_sge.lkey = mr->lkey;
181 rx->rx_sge.lkey = conn->ibc_hdev->ibh_pd->local_dma_lkey;
183 rx->rx_sge.addr = rx->rx_msgaddr;
184 rx->rx_sge.length = IBLND_MSG_SIZE;
186 rx->rx_wrq.next = NULL;
187 rx->rx_wrq.sg_list = &rx->rx_sge;
188 rx->rx_wrq.num_sge = 1;
189 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
191 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
192 LASSERT (rx->rx_nob >= 0); /* not posted */
194 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
195 kiblnd_drop_rx(rx); /* No more posts for this rx */
199 rx->rx_nob = -1; /* flag posted */
201 /* NB: need an extra reference after ib_post_recv because we don't
202 * own this rx (and rx::rx_conn) anymore, LU-5678.
204 kiblnd_conn_addref(conn);
205 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
206 if (unlikely(rc != 0)) {
207 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
208 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
212 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
215 if (unlikely(rc != 0)) {
216 kiblnd_close_conn(conn, rc);
217 kiblnd_drop_rx(rx); /* No more posts for this rx */
221 if (credit == IBLND_POSTRX_NO_CREDIT)
224 spin_lock(&conn->ibc_lock);
225 if (credit == IBLND_POSTRX_PEER_CREDIT)
226 conn->ibc_outstanding_credits++;
228 conn->ibc_reserved_credits++;
229 kiblnd_check_sends_locked(conn);
230 spin_unlock(&conn->ibc_lock);
233 kiblnd_conn_decref(conn);
237 static struct kib_tx *
238 kiblnd_find_waiting_tx_locked(struct kib_conn *conn, int txtype, u64 cookie)
240 struct list_head *tmp;
242 list_for_each(tmp, &conn->ibc_active_txs) {
243 struct kib_tx *tx = list_entry(tmp, struct kib_tx, tx_list);
245 LASSERT(!tx->tx_queued);
246 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
248 if (tx->tx_cookie != cookie)
251 if (tx->tx_waiting &&
252 tx->tx_msg->ibm_type == txtype)
255 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
256 tx->tx_waiting ? "" : "NOT ",
257 tx->tx_msg->ibm_type, txtype);
263 kiblnd_handle_completion(struct kib_conn *conn, int txtype, int status, u64 cookie)
266 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
269 spin_lock(&conn->ibc_lock);
271 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
273 spin_unlock(&conn->ibc_lock);
275 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
276 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
277 kiblnd_close_conn(conn, -EPROTO);
281 if (tx->tx_status == 0) { /* success so far */
282 if (status < 0) { /* failed? */
283 tx->tx_status = status;
284 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
285 } else if (txtype == IBLND_MSG_GET_REQ) {
286 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
292 idle = !tx->tx_queued && (tx->tx_sending == 0);
294 list_del(&tx->tx_list);
296 spin_unlock(&conn->ibc_lock);
303 kiblnd_send_completion(struct kib_conn *conn, int type, int status, u64 cookie)
305 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
306 struct kib_tx *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
309 CERROR("Can't get tx for completion %x for %s\n",
310 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
314 tx->tx_msg->ibm_u.completion.ibcm_status = status;
315 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
316 kiblnd_init_tx_msg(ni, tx, type, sizeof(struct kib_completion_msg));
318 kiblnd_queue_tx(tx, conn);
322 kiblnd_handle_rx(struct kib_rx *rx)
324 struct kib_msg *msg = rx->rx_msg;
325 struct kib_conn *conn = rx->rx_conn;
326 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
327 int credits = msg->ibm_credits;
333 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
335 CDEBUG (D_NET, "Received %x[%d] from %s\n",
336 msg->ibm_type, credits,
337 libcfs_nid2str(conn->ibc_peer->ibp_nid));
340 /* Have I received credits that will let me send? */
341 spin_lock(&conn->ibc_lock);
343 if (conn->ibc_credits + credits >
344 conn->ibc_queue_depth) {
345 rc2 = conn->ibc_credits;
346 spin_unlock(&conn->ibc_lock);
348 CERROR("Bad credits from %s: %d + %d > %d\n",
349 libcfs_nid2str(conn->ibc_peer->ibp_nid),
351 conn->ibc_queue_depth);
353 kiblnd_close_conn(conn, -EPROTO);
354 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
358 conn->ibc_credits += credits;
360 /* This ensures the credit taken by NOOP can be returned */
361 if (msg->ibm_type == IBLND_MSG_NOOP &&
362 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
363 conn->ibc_outstanding_credits++;
365 kiblnd_check_sends_locked(conn);
366 spin_unlock(&conn->ibc_lock);
369 switch (msg->ibm_type) {
371 CERROR("Bad IBLND message type %x from %s\n",
372 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
373 post_credit = IBLND_POSTRX_NO_CREDIT;
378 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
379 post_credit = IBLND_POSTRX_NO_CREDIT;
383 if (credits != 0) /* credit already posted */
384 post_credit = IBLND_POSTRX_NO_CREDIT;
385 else /* a keepalive NOOP */
386 post_credit = IBLND_POSTRX_PEER_CREDIT;
389 case IBLND_MSG_IMMEDIATE:
390 post_credit = IBLND_POSTRX_DONT_POST;
391 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
392 msg->ibm_srcnid, rx, 0);
393 if (rc < 0) /* repost on error */
394 post_credit = IBLND_POSTRX_PEER_CREDIT;
397 case IBLND_MSG_PUT_REQ:
398 post_credit = IBLND_POSTRX_DONT_POST;
399 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
400 msg->ibm_srcnid, rx, 1);
401 if (rc < 0) /* repost on error */
402 post_credit = IBLND_POSTRX_PEER_CREDIT;
405 case IBLND_MSG_PUT_NAK:
406 CWARN ("PUT_NACK from %s\n",
407 libcfs_nid2str(conn->ibc_peer->ibp_nid));
408 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
409 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
410 msg->ibm_u.completion.ibcm_status,
411 msg->ibm_u.completion.ibcm_cookie);
414 case IBLND_MSG_PUT_ACK:
415 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
417 spin_lock(&conn->ibc_lock);
418 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
419 msg->ibm_u.putack.ibpam_src_cookie);
421 list_del(&tx->tx_list);
422 spin_unlock(&conn->ibc_lock);
425 CERROR("Unmatched PUT_ACK from %s\n",
426 libcfs_nid2str(conn->ibc_peer->ibp_nid));
431 LASSERT (tx->tx_waiting);
432 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
433 * (a) I can overwrite tx_msg since my peer_ni has received it!
434 * (b) tx_waiting set tells tx_complete() it's not done. */
436 tx->tx_nwrq = tx->tx_nsge = 0; /* overwrite PUT_REQ */
438 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
439 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
440 &msg->ibm_u.putack.ibpam_rd,
441 msg->ibm_u.putack.ibpam_dst_cookie);
443 CERROR("Can't setup rdma for PUT to %s: %d\n",
444 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
446 spin_lock(&conn->ibc_lock);
447 tx->tx_waiting = 0; /* clear waiting and queue atomically */
448 kiblnd_queue_tx_locked(tx, conn);
449 spin_unlock(&conn->ibc_lock);
452 case IBLND_MSG_PUT_DONE:
453 post_credit = IBLND_POSTRX_PEER_CREDIT;
454 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
455 msg->ibm_u.completion.ibcm_status,
456 msg->ibm_u.completion.ibcm_cookie);
459 case IBLND_MSG_GET_REQ:
460 post_credit = IBLND_POSTRX_DONT_POST;
461 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
462 msg->ibm_srcnid, rx, 1);
463 if (rc < 0) /* repost on error */
464 post_credit = IBLND_POSTRX_PEER_CREDIT;
467 case IBLND_MSG_GET_DONE:
468 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
469 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
470 msg->ibm_u.completion.ibcm_status,
471 msg->ibm_u.completion.ibcm_cookie);
475 if (rc < 0) /* protocol error */
476 kiblnd_close_conn(conn, rc);
478 if (post_credit != IBLND_POSTRX_DONT_POST)
479 kiblnd_post_rx(rx, post_credit);
483 kiblnd_rx_complete(struct kib_rx *rx, int status, int nob)
485 struct kib_msg *msg = rx->rx_msg;
486 struct kib_conn *conn = rx->rx_conn;
487 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
488 struct kib_net *net = ni->ni_data;
492 LASSERT (net != NULL);
493 LASSERT (rx->rx_nob < 0); /* was posted */
494 rx->rx_nob = 0; /* isn't now */
496 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
499 if (status != IB_WC_SUCCESS) {
500 CNETERR("Rx from %s failed: %d\n",
501 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
508 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
510 CERROR ("Error %d unpacking rx from %s\n",
511 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
515 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
516 msg->ibm_dstnid != ni->ni_nid ||
517 msg->ibm_srcstamp != conn->ibc_incarnation ||
518 msg->ibm_dststamp != net->ibn_incarnation) {
519 CERROR ("Stale rx from %s\n",
520 libcfs_nid2str(conn->ibc_peer->ibp_nid));
525 /* set time last known alive */
526 kiblnd_peer_alive(conn->ibc_peer);
528 /* racing with connection establishment/teardown! */
530 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
531 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
534 write_lock_irqsave(g_lock, flags);
535 /* must check holding global lock to eliminate race */
536 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
537 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
538 write_unlock_irqrestore(g_lock, flags);
541 write_unlock_irqrestore(g_lock, flags);
543 kiblnd_handle_rx(rx);
547 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
548 kiblnd_close_conn(conn, err);
550 kiblnd_drop_rx(rx); /* Don't re-post rx. */
554 kiblnd_fmr_map_tx(struct kib_net *net, struct kib_tx *tx,
555 struct kib_rdma_desc *rd, u32 nob)
557 struct kib_hca_dev *hdev;
559 struct kib_fmr_poolset *fps;
564 LASSERT(tx->tx_pool != NULL);
565 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
568 hdev = tx->tx_pool->tpo_hdev;
569 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
572 * If we're dealing with FastReg, but the device doesn't
573 * support GAPS and the tx has GAPS, then there is no real point
574 * in trying to map the memory, because it'll just fail. So
575 * preemptively fail with an appropriate message
577 if ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED) &&
578 !(dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT) &&
580 CERROR("Using FastReg with no GAPS support, but tx has gaps. "
581 "Try setting use_fastreg_gaps to 1\n");
582 return -EPROTONOSUPPORT;
586 * FMR does not support gaps but the tx has gaps then
587 * we should make sure that the number of fragments we'll be sending
588 * over fits within the number of fragments negotiated on the
589 * connection, otherwise, we won't be able to RDMA the data.
590 * We need to maintain the number of fragments negotiation on the
591 * connection for backwards compatibility.
593 if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
595 tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
596 CERROR("TX number of frags (%d) is <= than connection"
597 " number of frags (%d). Consider setting peer's"
598 " map_on_demand to 256\n", tx->tx_nfrags,
599 tx->tx_conn->ibc_max_frags);
604 fps = net->ibn_fmr_ps[cpt];
605 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
607 CERROR("Can't map %u pages: %d\n", nob, rc);
612 * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
615 rd->rd_key = tx->tx_fmr.fmr_key;
617 * for FastReg or FMR with no gaps we can accumulate all
618 * the fragments in one FastReg or FMR fragment.
620 if (((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED) && !tx->tx_gaps) ||
621 (dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)) {
622 /* FMR requires zero based address */
623 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
624 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
625 rd->rd_frags[0].rf_nob = nob;
629 * We're transmitting with gaps using FMR.
630 * We'll need to use multiple fragments and identify the
631 * zero based address of each fragment.
633 for (i = 0; i < rd->rd_nfrags; i++) {
634 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
635 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
643 kiblnd_unmap_tx(struct kib_tx *tx)
645 if (tx->tx_fmr.fmr_pfmr || tx->tx_fmr.fmr_frd)
646 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
648 if (tx->tx_nfrags != 0) {
649 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
650 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
655 #ifdef HAVE_IB_GET_DMA_MR
656 static struct ib_mr *
657 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
659 struct kib_net *net = ni->ni_data;
660 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
661 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
663 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
666 * if map-on-demand is turned on and the device supports
667 * either FMR or FastReg then use that. Otherwise use global
668 * memory regions. If that's not available either, then you're
669 * dead in the water and fail the operation.
671 if (tunables->lnd_map_on_demand &&
672 (net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED ||
673 net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED))
677 * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
678 * in the call chain. The mapping will fail with appropriate error
681 return hdev->ibh_mrs;
685 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
686 struct kib_rdma_desc *rd, int nfrags)
688 struct kib_net *net = ni->ni_data;
689 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
690 #ifdef HAVE_IB_GET_DMA_MR
691 struct ib_mr *mr = NULL;
696 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
698 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
699 tx->tx_nfrags = nfrags;
701 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
702 tx->tx_nfrags, tx->tx_dmadir);
704 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
705 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
706 hdev->ibh_ibdev, &tx->tx_frags[i]);
707 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
708 hdev->ibh_ibdev, &tx->tx_frags[i]);
709 nob += rd->rd_frags[i].rf_nob;
712 #ifdef HAVE_IB_GET_DMA_MR
713 mr = kiblnd_find_rd_dma_mr(ni, rd);
715 /* found pre-mapping MR */
716 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
721 if (net->ibn_fmr_ps != NULL)
722 return kiblnd_fmr_map_tx(net, tx, rd, nob);
727 static int kiblnd_setup_rd_iov(struct lnet_ni *ni, struct kib_tx *tx,
728 struct kib_rdma_desc *rd, unsigned int niov,
729 struct kvec *iov, int offset, int nob)
731 struct kib_net *net = ni->ni_data;
733 struct scatterlist *sg;
737 unsigned int max_niov;
741 LASSERT (net != NULL);
743 while (offset >= iov->iov_len) {
744 offset -= iov->iov_len;
756 vaddr = ((unsigned long)iov->iov_base) + offset;
757 page_offset = vaddr & (PAGE_SIZE - 1);
758 page = lnet_kvaddr_to_page(vaddr);
760 CERROR("Can't find page\n");
764 fragnob = min((int)(iov->iov_len - offset), nob);
765 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
768 * We're allowed to start at a non-aligned page offset in
769 * the first fragment and end at a non-aligned page offset
770 * in the last fragment.
772 if ((fragnob < (int)PAGE_SIZE - page_offset) &&
773 (niov < max_niov) && nob > fragnob) {
774 CDEBUG(D_NET, "fragnob %d < available page %d: with"
775 " remaining %d iovs with %d nob left\n",
776 fragnob, (int)PAGE_SIZE - page_offset, niov,
781 sg_set_page(sg, page, fragnob, page_offset);
784 CERROR("lacking enough sg entries to map tx\n");
788 if (offset + fragnob < iov->iov_len) {
798 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
801 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
802 struct kib_rdma_desc *rd, int nkiov,
803 lnet_kiov_t *kiov, int offset, int nob)
805 struct kib_net *net = ni->ni_data;
806 struct scatterlist *sg;
810 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
814 LASSERT(net != NULL);
816 while (offset >= kiov->kiov_len) {
817 offset -= kiov->kiov_len;
829 fragnob = min((int)(kiov->kiov_len - offset), nob);
832 * We're allowed to start at a non-aligned page offset in
833 * the first fragment and end at a non-aligned page offset
834 * in the last fragment.
836 if ((fragnob < (int)(kiov->kiov_len - offset)) &&
837 nkiov < max_nkiov && nob > fragnob) {
838 CDEBUG(D_NET, "fragnob %d < available page %d: with"
839 " remaining %d kiovs with %d nob left\n",
840 fragnob, (int)(kiov->kiov_len - offset),
845 sg_set_page(sg, kiov->kiov_page, fragnob,
846 kiov->kiov_offset + offset);
849 CERROR("lacking enough sg entries to map tx\n");
859 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
863 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
864 __must_hold(&conn->ibc_lock)
866 struct kib_msg *msg = tx->tx_msg;
867 struct kib_peer_ni *peer_ni = conn->ibc_peer;
868 int ver = conn->ibc_version;
872 LASSERT(tx->tx_queued);
873 /* We rely on this for QP sizing */
874 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
875 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
877 LASSERT(credit == 0 || credit == 1);
878 LASSERT(conn->ibc_outstanding_credits >= 0);
879 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
880 LASSERT(conn->ibc_credits >= 0);
881 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
883 if (conn->ibc_nsends_posted ==
884 conn->ibc_queue_depth) {
885 /* tx completions outstanding... */
886 CDEBUG(D_NET, "%s: posted enough\n",
887 libcfs_nid2str(peer_ni->ibp_nid));
891 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
892 CDEBUG(D_NET, "%s: no credits\n",
893 libcfs_nid2str(peer_ni->ibp_nid));
897 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
898 conn->ibc_credits == 1 && /* last credit reserved */
899 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
900 CDEBUG(D_NET, "%s: not using last credit\n",
901 libcfs_nid2str(peer_ni->ibp_nid));
905 /* NB don't drop ibc_lock before bumping tx_sending */
906 list_del(&tx->tx_list);
909 if (msg->ibm_type == IBLND_MSG_NOOP &&
910 (!kiblnd_need_noop(conn) || /* redundant NOOP */
911 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
912 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
913 /* OK to drop when posted enough NOOPs, since
914 * kiblnd_check_sends_locked will queue NOOP again when
915 * posted NOOPs complete */
916 spin_unlock(&conn->ibc_lock);
917 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
919 spin_lock(&conn->ibc_lock);
920 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
921 libcfs_nid2str(peer_ni->ibp_nid),
922 conn->ibc_noops_posted);
926 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
927 peer_ni->ibp_nid, conn->ibc_incarnation);
929 conn->ibc_credits -= credit;
930 conn->ibc_outstanding_credits = 0;
931 conn->ibc_nsends_posted++;
932 if (msg->ibm_type == IBLND_MSG_NOOP)
933 conn->ibc_noops_posted++;
935 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
936 * PUT. If so, it was first queued here as a PUT_REQ, sent and
937 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
938 * and then re-queued here. It's (just) possible that
939 * tx_sending is non-zero if we've not done the tx_complete()
940 * from the first send; hence the ++ rather than = below. */
942 list_add(&tx->tx_list, &conn->ibc_active_txs);
944 /* I'm still holding ibc_lock! */
945 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
947 } else if (tx->tx_pool->tpo_pool.po_failed ||
948 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
949 /* close_conn will launch failover */
952 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
953 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
954 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
957 if (!frd->frd_valid) {
958 wr = &frd->frd_inv_wr.wr;
959 wr->next = &frd->frd_fastreg_wr.wr;
961 wr = &frd->frd_fastreg_wr.wr;
963 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
966 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
967 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
968 bad->wr_id, bad->opcode, bad->send_flags,
969 libcfs_nid2str(conn->ibc_peer->ibp_nid));
972 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
975 conn->ibc_last_send = ktime_get();
980 /* NB credits are transferred in the actual
981 * message, which can only be the last work item */
982 conn->ibc_credits += credit;
983 conn->ibc_outstanding_credits += msg->ibm_credits;
984 conn->ibc_nsends_posted--;
985 if (msg->ibm_type == IBLND_MSG_NOOP)
986 conn->ibc_noops_posted--;
992 done = (tx->tx_sending == 0);
994 list_del(&tx->tx_list);
996 spin_unlock(&conn->ibc_lock);
998 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
999 CERROR("Error %d posting transmit to %s\n",
1000 rc, libcfs_nid2str(peer_ni->ibp_nid));
1002 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1003 rc, libcfs_nid2str(peer_ni->ibp_nid));
1005 kiblnd_close_conn(conn, rc);
1010 spin_lock(&conn->ibc_lock);
1016 kiblnd_check_sends_locked(struct kib_conn *conn)
1018 int ver = conn->ibc_version;
1019 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
1022 /* Don't send anything until after the connection is established */
1023 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
1024 CDEBUG(D_NET, "%s too soon\n",
1025 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1029 LASSERT(conn->ibc_nsends_posted <=
1030 conn->ibc_queue_depth);
1031 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
1032 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
1033 LASSERT (conn->ibc_reserved_credits >= 0);
1035 while (conn->ibc_reserved_credits > 0 &&
1036 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
1037 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
1038 struct kib_tx, tx_list);
1039 list_del(&tx->tx_list);
1040 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
1041 conn->ibc_reserved_credits--;
1044 if (kiblnd_need_noop(conn)) {
1045 spin_unlock(&conn->ibc_lock);
1047 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1049 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1051 spin_lock(&conn->ibc_lock);
1053 kiblnd_queue_tx_locked(tx, conn);
1059 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1061 tx = list_entry(conn->ibc_tx_queue_nocred.next,
1062 struct kib_tx, tx_list);
1063 } else if (!list_empty(&conn->ibc_tx_noops)) {
1064 LASSERT (!IBLND_OOB_CAPABLE(ver));
1066 tx = list_entry(conn->ibc_tx_noops.next,
1067 struct kib_tx, tx_list);
1068 } else if (!list_empty(&conn->ibc_tx_queue)) {
1070 tx = list_entry(conn->ibc_tx_queue.next,
1071 struct kib_tx, tx_list);
1075 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1081 kiblnd_tx_complete(struct kib_tx *tx, int status)
1083 int failed = (status != IB_WC_SUCCESS);
1084 struct kib_conn *conn = tx->tx_conn;
1087 LASSERT (tx->tx_sending > 0);
1090 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1091 CNETERR("Tx -> %s cookie %#llx"
1092 " sending %d waiting %d: failed %d\n",
1093 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1094 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1097 kiblnd_close_conn(conn, -EIO);
1099 kiblnd_peer_alive(conn->ibc_peer);
1102 spin_lock(&conn->ibc_lock);
1104 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1105 * gets to free it, which also drops its ref on 'conn'. */
1108 conn->ibc_nsends_posted--;
1109 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1110 conn->ibc_noops_posted--;
1113 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1114 tx->tx_waiting = 0; /* don't wait for peer_ni */
1115 tx->tx_status = -EIO;
1118 idle = (tx->tx_sending == 0) && /* This is the final callback */
1119 !tx->tx_waiting && /* Not waiting for peer_ni */
1120 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1122 list_del(&tx->tx_list);
1124 kiblnd_check_sends_locked(conn);
1125 spin_unlock(&conn->ibc_lock);
1132 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1135 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1136 struct ib_sge *sge = &tx->tx_msgsge;
1137 struct ib_rdma_wr *wrq;
1138 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1139 #ifdef HAVE_IB_GET_DMA_MR
1140 struct ib_mr *mr = hdev->ibh_mrs;
1143 LASSERT(tx->tx_nwrq >= 0);
1144 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1145 LASSERT(nob <= IBLND_MSG_SIZE);
1146 #ifdef HAVE_IB_GET_DMA_MR
1147 LASSERT(mr != NULL);
1150 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1152 #ifdef HAVE_IB_GET_DMA_MR
1153 sge->lkey = mr->lkey;
1155 sge->lkey = hdev->ibh_pd->local_dma_lkey;
1157 sge->addr = tx->tx_msgaddr;
1160 wrq = &tx->tx_wrq[tx->tx_nwrq];
1161 memset(wrq, 0, sizeof(*wrq));
1163 wrq->wr.next = NULL;
1164 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1165 wrq->wr.sg_list = sge;
1166 wrq->wr.num_sge = 1;
1167 wrq->wr.opcode = IB_WR_SEND;
1168 wrq->wr.send_flags = IB_SEND_SIGNALED;
1174 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1175 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1177 struct kib_msg *ibmsg = tx->tx_msg;
1178 struct kib_rdma_desc *srcrd = tx->tx_rd;
1179 struct ib_rdma_wr *wrq = NULL;
1187 LASSERT(!in_interrupt());
1188 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1189 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1191 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1192 resid > 0; resid -= sge_nob) {
1195 if (srcidx >= srcrd->rd_nfrags) {
1196 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1201 if (dstidx >= dstrd->rd_nfrags) {
1202 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1207 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1208 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1209 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1210 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1211 conn->ibc_max_frags,
1212 srcidx, srcrd->rd_nfrags,
1213 dstidx, dstrd->rd_nfrags);
1218 sge_nob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1219 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1221 sge = &tx->tx_sge[tx->tx_nsge];
1222 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1223 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1224 sge->length = sge_nob;
1227 wrq = &tx->tx_wrq[tx->tx_nwrq];
1229 wrq->wr.next = &(wrq + 1)->wr;
1230 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1231 wrq->wr.sg_list = sge;
1232 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1233 wrq->wr.send_flags = 0;
1235 #ifdef HAVE_IB_RDMA_WR
1236 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1238 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1241 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1243 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1248 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1249 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1252 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1254 wrq->wr.num_sge = wrq_sge;
1260 if (rc < 0) /* no RDMA if completing with failure */
1261 tx->tx_nwrq = tx->tx_nsge = 0;
1263 ibmsg->ibm_u.completion.ibcm_status = rc;
1264 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1265 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1266 type, sizeof(struct kib_completion_msg));
1272 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1274 struct list_head *q;
1277 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1278 LASSERT(!tx->tx_queued); /* not queued for sending already */
1279 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1281 timeout_ns = *kiblnd_tunables.kib_timeout * NSEC_PER_SEC;
1283 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1285 if (tx->tx_conn == NULL) {
1286 kiblnd_conn_addref(conn);
1288 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1290 /* PUT_DONE first attached to conn as a PUT_REQ */
1291 LASSERT (tx->tx_conn == conn);
1292 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1295 switch (tx->tx_msg->ibm_type) {
1299 case IBLND_MSG_PUT_REQ:
1300 case IBLND_MSG_GET_REQ:
1301 q = &conn->ibc_tx_queue_rsrvd;
1304 case IBLND_MSG_PUT_NAK:
1305 case IBLND_MSG_PUT_ACK:
1306 case IBLND_MSG_PUT_DONE:
1307 case IBLND_MSG_GET_DONE:
1308 q = &conn->ibc_tx_queue_nocred;
1311 case IBLND_MSG_NOOP:
1312 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1313 q = &conn->ibc_tx_queue_nocred;
1315 q = &conn->ibc_tx_noops;
1318 case IBLND_MSG_IMMEDIATE:
1319 q = &conn->ibc_tx_queue;
1323 list_add_tail(&tx->tx_list, q);
1327 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1329 spin_lock(&conn->ibc_lock);
1330 kiblnd_queue_tx_locked(tx, conn);
1331 kiblnd_check_sends_locked(conn);
1332 spin_unlock(&conn->ibc_lock);
1335 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1336 struct sockaddr_in *srcaddr,
1337 struct sockaddr_in *dstaddr,
1340 unsigned short port;
1343 /* allow the port to be reused */
1344 rc = rdma_set_reuseaddr(cmid, 1);
1346 CERROR("Unable to set reuse on cmid: %d\n", rc);
1350 /* look for a free privileged port */
1351 for (port = PROT_SOCK-1; port > 0; port--) {
1352 srcaddr->sin_port = htons(port);
1353 rc = rdma_resolve_addr(cmid,
1354 (struct sockaddr *)srcaddr,
1355 (struct sockaddr *)dstaddr,
1358 CDEBUG(D_NET, "bound to port %hu\n", port);
1360 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1361 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1368 CERROR("Failed to bind to a free privileged port\n");
1373 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1375 struct rdma_cm_id *cmid;
1376 struct kib_dev *dev;
1377 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1378 struct sockaddr_in srcaddr;
1379 struct sockaddr_in dstaddr;
1382 LASSERT (net != NULL);
1383 LASSERT (peer_ni->ibp_connecting > 0);
1385 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer_ni, RDMA_PS_TCP,
1389 CERROR("Can't create CMID for %s: %ld\n",
1390 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1396 memset(&srcaddr, 0, sizeof(srcaddr));
1397 srcaddr.sin_family = AF_INET;
1398 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1400 memset(&dstaddr, 0, sizeof(dstaddr));
1401 dstaddr.sin_family = AF_INET;
1402 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1403 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1405 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1407 if (*kiblnd_tunables.kib_use_priv_port) {
1408 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1409 *kiblnd_tunables.kib_timeout * 1000);
1411 rc = rdma_resolve_addr(cmid,
1412 (struct sockaddr *)&srcaddr,
1413 (struct sockaddr *)&dstaddr,
1414 *kiblnd_tunables.kib_timeout * 1000);
1417 /* Can't initiate address resolution: */
1418 CERROR("Can't resolve addr for %s: %d\n",
1419 libcfs_nid2str(peer_ni->ibp_nid), rc);
1426 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1427 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1428 rdma_destroy_id(cmid);
1431 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1435 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1437 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1438 char *reason = NULL;
1439 struct list_head txs;
1440 unsigned long flags;
1442 INIT_LIST_HEAD(&txs);
1444 write_lock_irqsave(glock, flags);
1445 if (peer_ni->ibp_reconnecting == 0) {
1446 if (peer_ni->ibp_accepting)
1447 reason = "accepting";
1448 else if (peer_ni->ibp_connecting)
1449 reason = "connecting";
1450 else if (!list_empty(&peer_ni->ibp_conns))
1451 reason = "connected";
1452 else /* connected then closed */
1458 if (peer_ni->ibp_accepting)
1459 CNETERR("Detecting race between accepting and reconnecting\n");
1460 peer_ni->ibp_reconnecting--;
1462 if (!kiblnd_peer_active(peer_ni)) {
1463 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1464 reason = "unlinked";
1468 peer_ni->ibp_connecting++;
1469 peer_ni->ibp_reconnected++;
1471 write_unlock_irqrestore(glock, flags);
1473 kiblnd_connect_peer(peer_ni);
1477 write_unlock_irqrestore(glock, flags);
1479 CWARN("Abort reconnection of %s: %s\n",
1480 libcfs_nid2str(peer_ni->ibp_nid), reason);
1481 kiblnd_txlist_done(&txs, -ECONNABORTED,
1482 LNET_MSG_STATUS_LOCAL_ABORTED);
1487 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1489 struct kib_peer_ni *peer_ni;
1490 struct kib_peer_ni *peer2;
1491 struct kib_conn *conn;
1492 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1493 unsigned long flags;
1496 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1498 /* If I get here, I've committed to send, so I complete the tx with
1499 * failure on any problems */
1501 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1502 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1504 /* First time, just use a read lock since I expect to find my peer_ni
1506 read_lock_irqsave(g_lock, flags);
1508 peer_ni = kiblnd_find_peer_locked(ni, nid);
1509 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1510 /* Found a peer_ni with an established connection */
1511 conn = kiblnd_get_conn_locked(peer_ni);
1512 kiblnd_conn_addref(conn); /* 1 ref for me... */
1514 read_unlock_irqrestore(g_lock, flags);
1517 kiblnd_queue_tx(tx, conn);
1518 kiblnd_conn_decref(conn); /* ...to here */
1522 read_unlock(g_lock);
1523 /* Re-try with a write lock */
1526 peer_ni = kiblnd_find_peer_locked(ni, nid);
1527 if (peer_ni != NULL) {
1528 if (list_empty(&peer_ni->ibp_conns)) {
1529 /* found a peer_ni, but it's still connecting... */
1530 LASSERT(kiblnd_peer_connecting(peer_ni));
1532 list_add_tail(&tx->tx_list,
1533 &peer_ni->ibp_tx_queue);
1534 write_unlock_irqrestore(g_lock, flags);
1536 conn = kiblnd_get_conn_locked(peer_ni);
1537 kiblnd_conn_addref(conn); /* 1 ref for me... */
1539 write_unlock_irqrestore(g_lock, flags);
1542 kiblnd_queue_tx(tx, conn);
1543 kiblnd_conn_decref(conn); /* ...to here */
1548 write_unlock_irqrestore(g_lock, flags);
1550 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1551 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1553 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1555 tx->tx_status = -EHOSTUNREACH;
1557 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1563 write_lock_irqsave(g_lock, flags);
1565 peer2 = kiblnd_find_peer_locked(ni, nid);
1566 if (peer2 != NULL) {
1567 if (list_empty(&peer2->ibp_conns)) {
1568 /* found a peer_ni, but it's still connecting... */
1569 LASSERT(kiblnd_peer_connecting(peer2));
1571 list_add_tail(&tx->tx_list,
1572 &peer2->ibp_tx_queue);
1573 write_unlock_irqrestore(g_lock, flags);
1575 conn = kiblnd_get_conn_locked(peer2);
1576 kiblnd_conn_addref(conn); /* 1 ref for me... */
1578 write_unlock_irqrestore(g_lock, flags);
1581 kiblnd_queue_tx(tx, conn);
1582 kiblnd_conn_decref(conn); /* ...to here */
1585 kiblnd_peer_decref(peer_ni);
1589 /* Brand new peer_ni */
1590 LASSERT(peer_ni->ibp_connecting == 0);
1591 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1592 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1594 /* always called with a ref on ni, which prevents ni being shutdown */
1595 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1598 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1600 kiblnd_peer_addref(peer_ni);
1601 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
1603 write_unlock_irqrestore(g_lock, flags);
1605 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1606 kiblnd_connect_peer(peer_ni);
1607 kiblnd_peer_decref(peer_ni);
1611 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1613 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1614 int type = lntmsg->msg_type;
1615 struct lnet_process_id target = lntmsg->msg_target;
1616 int target_is_router = lntmsg->msg_target_is_router;
1617 int routing = lntmsg->msg_routing;
1618 unsigned int payload_niov = lntmsg->msg_niov;
1619 struct kvec *payload_iov = lntmsg->msg_iov;
1620 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1621 unsigned int payload_offset = lntmsg->msg_offset;
1622 unsigned int payload_nob = lntmsg->msg_len;
1623 struct kib_msg *ibmsg;
1624 struct kib_rdma_desc *rd;
1629 /* NB 'private' is different depending on what we're sending.... */
1631 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1632 payload_nob, payload_niov, libcfs_id2str(target));
1634 LASSERT (payload_nob == 0 || payload_niov > 0);
1635 LASSERT (payload_niov <= LNET_MAX_IOV);
1637 /* Thread context */
1638 LASSERT (!in_interrupt());
1639 /* payload is either all vaddrs or all pages */
1640 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1648 LASSERT (payload_nob == 0);
1652 if (routing || target_is_router)
1653 break; /* send IMMEDIATE */
1655 /* is the REPLY message too small for RDMA? */
1656 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1657 if (nob <= IBLND_MSG_SIZE)
1658 break; /* send IMMEDIATE */
1660 tx = kiblnd_get_idle_tx(ni, target.nid);
1662 CERROR("Can't allocate txd for GET to %s\n",
1663 libcfs_nid2str(target.nid));
1668 rd = &ibmsg->ibm_u.get.ibgm_rd;
1669 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1670 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1671 lntmsg->msg_md->md_niov,
1672 lntmsg->msg_md->md_iov.iov,
1673 0, lntmsg->msg_md->md_length);
1675 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1676 lntmsg->msg_md->md_niov,
1677 lntmsg->msg_md->md_iov.kiov,
1678 0, lntmsg->msg_md->md_length);
1680 CERROR("Can't setup GET sink for %s: %d\n",
1681 libcfs_nid2str(target.nid), rc);
1682 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1687 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1688 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1689 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1691 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1693 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1694 if (tx->tx_lntmsg[1] == NULL) {
1695 CERROR("Can't create reply for GET -> %s\n",
1696 libcfs_nid2str(target.nid));
1701 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1702 tx->tx_waiting = 1; /* waiting for GET_DONE */
1703 kiblnd_launch_tx(ni, tx, target.nid);
1706 case LNET_MSG_REPLY:
1708 /* Is the payload small enough not to need RDMA? */
1709 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob]);
1710 if (nob <= IBLND_MSG_SIZE)
1711 break; /* send IMMEDIATE */
1713 tx = kiblnd_get_idle_tx(ni, target.nid);
1715 CERROR("Can't allocate %s txd for %s\n",
1716 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1717 libcfs_nid2str(target.nid));
1721 if (payload_kiov == NULL)
1722 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1723 payload_niov, payload_iov,
1724 payload_offset, payload_nob);
1726 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1727 payload_niov, payload_kiov,
1728 payload_offset, payload_nob);
1730 CERROR("Can't setup PUT src for %s: %d\n",
1731 libcfs_nid2str(target.nid), rc);
1737 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1738 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1739 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1740 sizeof(struct kib_putreq_msg));
1742 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1743 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1744 kiblnd_launch_tx(ni, tx, target.nid);
1748 /* send IMMEDIATE */
1749 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1752 tx = kiblnd_get_idle_tx(ni, target.nid);
1754 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1755 type, libcfs_nid2str(target.nid));
1760 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1762 if (payload_kiov != NULL)
1763 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1764 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1765 payload_niov, payload_kiov,
1766 payload_offset, payload_nob);
1768 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1769 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1770 payload_niov, payload_iov,
1771 payload_offset, payload_nob);
1773 nob = offsetof(struct kib_immediate_msg, ibim_payload[payload_nob]);
1774 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1776 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1777 kiblnd_launch_tx(ni, tx, target.nid);
1782 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1784 struct lnet_process_id target = lntmsg->msg_target;
1785 unsigned int niov = lntmsg->msg_niov;
1786 struct kvec *iov = lntmsg->msg_iov;
1787 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1788 unsigned int offset = lntmsg->msg_offset;
1789 unsigned int nob = lntmsg->msg_len;
1793 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1795 CERROR("Can't get tx for REPLY to %s\n",
1796 libcfs_nid2str(target.nid));
1802 else if (kiov == NULL)
1803 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1804 niov, iov, offset, nob);
1806 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1807 niov, kiov, offset, nob);
1810 CERROR("Can't setup GET src for %s: %d\n",
1811 libcfs_nid2str(target.nid), rc);
1815 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1816 IBLND_MSG_GET_DONE, nob,
1817 &rx->rx_msg->ibm_u.get.ibgm_rd,
1818 rx->rx_msg->ibm_u.get.ibgm_cookie);
1820 CERROR("Can't setup rdma for GET from %s: %d\n",
1821 libcfs_nid2str(target.nid), rc);
1826 /* No RDMA: local completion may happen now! */
1827 lnet_finalize(lntmsg, 0);
1829 /* RDMA: lnet_finalize(lntmsg) when it
1831 tx->tx_lntmsg[0] = lntmsg;
1834 kiblnd_queue_tx(tx, rx->rx_conn);
1839 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1842 lnet_finalize(lntmsg, -EIO);
1846 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1847 int delayed, unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1848 unsigned int offset, unsigned int mlen, unsigned int rlen)
1850 struct kib_rx *rx = private;
1851 struct kib_msg *rxmsg = rx->rx_msg;
1852 struct kib_conn *conn = rx->rx_conn;
1856 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1859 LASSERT (mlen <= rlen);
1860 LASSERT (!in_interrupt());
1861 /* Either all pages or all vaddrs */
1862 LASSERT (!(kiov != NULL && iov != NULL));
1864 switch (rxmsg->ibm_type) {
1868 case IBLND_MSG_IMMEDIATE:
1869 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1870 if (nob > rx->rx_nob) {
1871 CERROR ("Immediate message from %s too big: %d(%d)\n",
1872 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1879 lnet_copy_flat2kiov(niov, kiov, offset,
1880 IBLND_MSG_SIZE, rxmsg,
1881 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1884 lnet_copy_flat2iov(niov, iov, offset,
1885 IBLND_MSG_SIZE, rxmsg,
1886 offsetof(struct kib_msg, ibm_u.immediate.ibim_payload),
1888 lnet_finalize(lntmsg, 0);
1891 case IBLND_MSG_PUT_REQ: {
1892 struct kib_msg *txmsg;
1893 struct kib_rdma_desc *rd;
1894 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1897 lnet_finalize(lntmsg, 0);
1898 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1903 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1905 CERROR("Can't allocate tx for %s\n",
1906 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1907 /* Not replying will break the connection */
1913 rd = &txmsg->ibm_u.putack.ibpam_rd;
1915 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1916 niov, iov, offset, mlen);
1918 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1919 niov, kiov, offset, mlen);
1921 CERROR("Can't setup PUT sink for %s: %d\n",
1922 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1923 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1925 /* tell peer_ni it's over */
1926 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1931 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1932 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1933 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1935 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1937 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1938 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1939 kiblnd_queue_tx(tx, conn);
1941 /* reposted buffer reserved for PUT_DONE */
1942 post_credit = IBLND_POSTRX_NO_CREDIT;
1946 case IBLND_MSG_GET_REQ:
1947 if (lntmsg != NULL) {
1948 /* Optimized GET; RDMA lntmsg's payload */
1949 kiblnd_reply(ni, rx, lntmsg);
1951 /* GET didn't match anything */
1952 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1954 rxmsg->ibm_u.get.ibgm_cookie);
1959 kiblnd_post_rx(rx, post_credit);
1964 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1966 struct task_struct *task = kthread_run(fn, arg, name);
1969 return PTR_ERR(task);
1971 atomic_inc(&kiblnd_data.kib_nthreads);
1976 kiblnd_thread_fini (void)
1978 atomic_dec (&kiblnd_data.kib_nthreads);
1982 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
1984 /* This is racy, but everyone's only writing ktime_get_seconds() */
1985 peer_ni->ibp_last_alive = ktime_get_seconds();
1990 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
1993 time64_t last_alive = 0;
1994 unsigned long flags;
1996 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1998 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
1999 error = peer_ni->ibp_error;
2000 peer_ni->ibp_error = 0;
2002 last_alive = peer_ni->ibp_last_alive;
2005 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2008 lnet_notify(peer_ni->ibp_ni,
2009 peer_ni->ibp_nid, 0, last_alive);
2013 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
2015 /* This just does the immediate housekeeping. 'error' is zero for a
2016 * normal shutdown which can happen only after the connection has been
2017 * established. If the connection is established, schedule the
2018 * connection to be finished off by the connd. Otherwise the connd is
2019 * already dealing with it (either to set it up or tear it down).
2020 * Caller holds kib_global_lock exclusively in irq context */
2021 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2022 struct kib_dev *dev;
2023 unsigned long flags;
2025 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2027 if (error != 0 && conn->ibc_comms_error == 0)
2028 conn->ibc_comms_error = error;
2030 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2031 return; /* already being handled */
2034 list_empty(&conn->ibc_tx_noops) &&
2035 list_empty(&conn->ibc_tx_queue) &&
2036 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2037 list_empty(&conn->ibc_tx_queue_nocred) &&
2038 list_empty(&conn->ibc_active_txs)) {
2039 CDEBUG(D_NET, "closing conn to %s\n",
2040 libcfs_nid2str(peer_ni->ibp_nid));
2042 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2043 libcfs_nid2str(peer_ni->ibp_nid), error,
2044 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2045 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2046 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2047 "" : "(sending_rsrvd)",
2048 list_empty(&conn->ibc_tx_queue_nocred) ?
2049 "" : "(sending_nocred)",
2050 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2053 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2054 if (peer_ni->ibp_next_conn == conn)
2055 /* clear next_conn so it won't be used */
2056 peer_ni->ibp_next_conn = NULL;
2057 list_del(&conn->ibc_list);
2058 /* connd (see below) takes over ibc_list's ref */
2060 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2061 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2062 kiblnd_unlink_peer_locked(peer_ni);
2064 /* set/clear error on last conn */
2065 peer_ni->ibp_error = conn->ibc_comms_error;
2068 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2071 kiblnd_dev_can_failover(dev)) {
2072 list_add_tail(&dev->ibd_fail_list,
2073 &kiblnd_data.kib_failed_devs);
2074 wake_up(&kiblnd_data.kib_failover_waitq);
2077 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2079 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2080 wake_up(&kiblnd_data.kib_connd_waitq);
2082 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2086 kiblnd_close_conn(struct kib_conn *conn, int error)
2088 unsigned long flags;
2090 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2092 kiblnd_close_conn_locked(conn, error);
2094 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2098 kiblnd_handle_early_rxs(struct kib_conn *conn)
2100 unsigned long flags;
2103 LASSERT(!in_interrupt());
2104 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2106 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2107 while (!list_empty(&conn->ibc_early_rxs)) {
2108 rx = list_entry(conn->ibc_early_rxs.next,
2109 struct kib_rx, rx_list);
2110 list_del(&rx->rx_list);
2111 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2113 kiblnd_handle_rx(rx);
2115 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2117 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2121 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2123 struct list_head zombies = LIST_HEAD_INIT(zombies);
2124 struct list_head *tmp;
2125 struct list_head *nxt;
2128 spin_lock(&conn->ibc_lock);
2130 list_for_each_safe(tmp, nxt, txs) {
2131 tx = list_entry(tmp, struct kib_tx, tx_list);
2133 if (txs == &conn->ibc_active_txs) {
2134 LASSERT(!tx->tx_queued);
2135 LASSERT(tx->tx_waiting ||
2136 tx->tx_sending != 0);
2137 if (conn->ibc_comms_error == -ETIMEDOUT) {
2138 if (tx->tx_waiting && !tx->tx_sending)
2140 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2141 else if (tx->tx_sending)
2143 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2146 LASSERT(tx->tx_queued);
2147 if (conn->ibc_comms_error == -ETIMEDOUT)
2148 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2150 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2153 tx->tx_status = -ECONNABORTED;
2157 * TODO: This makes an assumption that
2158 * kiblnd_tx_complete() will be called for each tx. If
2159 * that event is dropped we could end up with stale
2160 * connections floating around. We'd like to deal with
2161 * that in a better way.
2163 * Also that means we can exceed the timeout by many
2166 if (tx->tx_sending == 0) {
2168 list_del(&tx->tx_list);
2169 list_add(&tx->tx_list, &zombies);
2173 spin_unlock(&conn->ibc_lock);
2176 * aborting transmits occurs when finalizing the connection.
2177 * The connection is finalized on error
2179 kiblnd_txlist_done(&zombies, -ECONNABORTED, -1);
2183 kiblnd_finalise_conn(struct kib_conn *conn)
2185 LASSERT (!in_interrupt());
2186 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2188 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2190 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2191 * for connections that didn't get as far as being connected, because
2192 * rdma_disconnect() does this for free. */
2193 kiblnd_abort_receives(conn);
2195 /* Complete all tx descs not waiting for sends to complete.
2196 * NB we should be safe from RDMA now that the QP has changed state */
2198 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2199 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2200 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2201 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2202 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2204 kiblnd_handle_early_rxs(conn);
2208 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2211 struct list_head zombies = LIST_HEAD_INIT(zombies);
2212 unsigned long flags;
2214 LASSERT (error != 0);
2215 LASSERT (!in_interrupt());
2217 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2220 LASSERT(peer_ni->ibp_connecting > 0);
2221 peer_ni->ibp_connecting--;
2223 LASSERT (peer_ni->ibp_accepting > 0);
2224 peer_ni->ibp_accepting--;
2227 if (kiblnd_peer_connecting(peer_ni)) {
2228 /* another connection attempt under way... */
2229 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2234 peer_ni->ibp_reconnected = 0;
2235 if (list_empty(&peer_ni->ibp_conns)) {
2236 /* Take peer_ni's blocked transmits to complete with error */
2237 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2239 if (kiblnd_peer_active(peer_ni))
2240 kiblnd_unlink_peer_locked(peer_ni);
2242 peer_ni->ibp_error = error;
2244 /* Can't have blocked transmits if there are connections */
2245 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2248 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2250 kiblnd_peer_notify(peer_ni);
2252 if (list_empty(&zombies))
2255 CNETERR("Deleting messages for %s: connection failed\n",
2256 libcfs_nid2str(peer_ni->ibp_nid));
2258 kiblnd_txlist_done(&zombies, error,
2259 LNET_MSG_STATUS_LOCAL_DROPPED);
2263 kiblnd_connreq_done(struct kib_conn *conn, int status)
2265 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2267 struct list_head txs;
2268 unsigned long flags;
2271 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2273 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2274 libcfs_nid2str(peer_ni->ibp_nid), active,
2275 conn->ibc_version, status);
2277 LASSERT (!in_interrupt());
2278 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2279 peer_ni->ibp_connecting > 0) ||
2280 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2281 peer_ni->ibp_accepting > 0));
2283 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2284 conn->ibc_connvars = NULL;
2287 /* failed to establish connection */
2288 kiblnd_peer_connect_failed(peer_ni, active, status);
2289 kiblnd_finalise_conn(conn);
2293 /* connection established */
2294 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2296 conn->ibc_last_send = ktime_get();
2297 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2298 kiblnd_peer_alive(peer_ni);
2300 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2301 * peer_ni instance... */
2302 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2303 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2304 peer_ni->ibp_reconnected = 0;
2306 peer_ni->ibp_connecting--;
2308 peer_ni->ibp_accepting--;
2310 if (peer_ni->ibp_version == 0) {
2311 peer_ni->ibp_version = conn->ibc_version;
2312 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2315 if (peer_ni->ibp_version != conn->ibc_version ||
2316 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2317 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2318 conn->ibc_incarnation);
2319 peer_ni->ibp_version = conn->ibc_version;
2320 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2323 /* grab pending txs while I have the lock */
2324 INIT_LIST_HEAD(&txs);
2325 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2327 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2328 conn->ibc_comms_error != 0) { /* error has happened already */
2330 /* start to shut down connection */
2331 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2332 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2334 kiblnd_txlist_done(&txs, -ECONNABORTED,
2335 LNET_MSG_STATUS_LOCAL_ERROR);
2340 /* +1 ref for myself, this connection is visible to other threads
2341 * now, refcount of peer:ibp_conns can be released by connection
2342 * close from either a different thread, or the calling of
2343 * kiblnd_check_sends_locked() below. See bz21911 for details.
2345 kiblnd_conn_addref(conn);
2346 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2348 /* Schedule blocked txs
2349 * Note: if we are running with conns_per_peer > 1, these blocked
2350 * txs will all get scheduled to the first connection which gets
2351 * scheduled. We won't be using round robin on this first batch.
2353 spin_lock(&conn->ibc_lock);
2354 while (!list_empty(&txs)) {
2355 tx = list_entry(txs.next, struct kib_tx, tx_list);
2356 list_del(&tx->tx_list);
2358 kiblnd_queue_tx_locked(tx, conn);
2360 kiblnd_check_sends_locked(conn);
2361 spin_unlock(&conn->ibc_lock);
2363 /* schedule blocked rxs */
2364 kiblnd_handle_early_rxs(conn);
2365 kiblnd_conn_decref(conn);
2369 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2373 rc = rdma_reject(cmid, rej, sizeof(*rej));
2376 CWARN("Error %d sending reject\n", rc);
2380 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2382 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2383 struct kib_msg *reqmsg = priv;
2384 struct kib_msg *ackmsg;
2385 struct kib_dev *ibdev;
2386 struct kib_peer_ni *peer_ni;
2387 struct kib_peer_ni *peer2;
2388 struct kib_conn *conn;
2389 struct lnet_ni *ni = NULL;
2390 struct kib_net *net = NULL;
2392 struct rdma_conn_param cp;
2394 int version = IBLND_MSG_VERSION;
2395 unsigned long flags;
2397 struct sockaddr_in *peer_addr;
2398 LASSERT (!in_interrupt());
2400 /* cmid inherits 'context' from the corresponding listener id */
2401 ibdev = cmid->context;
2404 memset(&rej, 0, sizeof(rej));
2405 rej.ibr_magic = IBLND_MSG_MAGIC;
2406 rej.ibr_why = IBLND_REJECT_FATAL;
2407 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2409 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2410 if (*kiblnd_tunables.kib_require_priv_port &&
2411 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2412 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2413 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2414 &ip, ntohs(peer_addr->sin_port));
2418 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2419 CERROR("Short connection request\n");
2423 /* Future protocol version compatibility support! If the
2424 * o2iblnd-specific protocol changes, or when LNET unifies
2425 * protocols over all LNDs, the initial connection will
2426 * negotiate a protocol version. I trap this here to avoid
2427 * console errors; the reject tells the peer_ni which protocol I
2429 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2430 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2432 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2433 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2434 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2436 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2437 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2438 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2441 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2443 CERROR("Can't parse connection request: %d\n", rc);
2447 nid = reqmsg->ibm_srcnid;
2448 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2451 net = (struct kib_net *)ni->ni_data;
2452 rej.ibr_incarnation = net->ibn_incarnation;
2455 if (ni == NULL || /* no matching net */
2456 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2457 net->ibn_dev != ibdev) { /* wrong device */
2458 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2459 "bad dst nid %s\n", libcfs_nid2str(nid),
2460 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2461 ibdev->ibd_ifname, ibdev->ibd_nnets,
2463 libcfs_nid2str(reqmsg->ibm_dstnid));
2468 /* check time stamp as soon as possible */
2469 if (reqmsg->ibm_dststamp != 0 &&
2470 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2471 CWARN("Stale connection request\n");
2472 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2476 /* I can accept peer_ni's version */
2477 version = reqmsg->ibm_version;
2479 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2480 CERROR("Unexpected connreq msg type: %x from %s\n",
2481 reqmsg->ibm_type, libcfs_nid2str(nid));
2485 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2486 kiblnd_msg_queue_size(version, ni)) {
2487 CERROR("Can't accept conn from %s, queue depth too large: "
2488 " %d (<=%d wanted)\n",
2489 libcfs_nid2str(nid),
2490 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2491 kiblnd_msg_queue_size(version, ni));
2493 if (version == IBLND_MSG_VERSION)
2494 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2499 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2500 IBLND_MAX_RDMA_FRAGS) {
2501 CWARN("Can't accept conn from %s (version %x): "
2502 "max_frags %d too large (%d wanted)\n",
2503 libcfs_nid2str(nid), version,
2504 reqmsg->ibm_u.connparams.ibcp_max_frags,
2505 IBLND_MAX_RDMA_FRAGS);
2507 if (version >= IBLND_MSG_VERSION)
2508 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2511 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2512 IBLND_MAX_RDMA_FRAGS &&
2513 net->ibn_fmr_ps == NULL) {
2514 CWARN("Can't accept conn from %s (version %x): "
2515 "max_frags %d incompatible without FMR pool "
2517 libcfs_nid2str(nid), version,
2518 reqmsg->ibm_u.connparams.ibcp_max_frags,
2519 IBLND_MAX_RDMA_FRAGS);
2521 if (version == IBLND_MSG_VERSION)
2522 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2527 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2528 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2529 libcfs_nid2str(nid),
2530 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2535 /* assume 'nid' is a new peer_ni; create */
2536 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2538 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2539 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2543 /* We have validated the peer's parameters so use those */
2544 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2545 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2547 write_lock_irqsave(g_lock, flags);
2549 peer2 = kiblnd_find_peer_locked(ni, nid);
2550 if (peer2 != NULL) {
2551 if (peer2->ibp_version == 0) {
2552 peer2->ibp_version = version;
2553 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2556 /* not the guy I've talked with */
2557 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2558 peer2->ibp_version != version) {
2559 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2561 if (kiblnd_peer_active(peer2)) {
2562 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2563 peer2->ibp_version = version;
2565 write_unlock_irqrestore(g_lock, flags);
2567 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2568 libcfs_nid2str(nid), peer2->ibp_version, version,
2569 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2571 kiblnd_peer_decref(peer_ni);
2572 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2576 /* Tie-break connection race in favour of the higher NID.
2577 * If we keep running into a race condition multiple times,
2578 * we have to assume that the connection attempt with the
2579 * higher NID is stuck in a connecting state and will never
2580 * recover. As such, we pass through this if-block and let
2581 * the lower NID connection win so we can move forward.
2583 if (peer2->ibp_connecting != 0 &&
2584 nid < ni->ni_nid && peer2->ibp_races <
2585 MAX_CONN_RACES_BEFORE_ABORT) {
2587 write_unlock_irqrestore(g_lock, flags);
2589 CDEBUG(D_NET, "Conn race %s\n",
2590 libcfs_nid2str(peer2->ibp_nid));
2592 kiblnd_peer_decref(peer_ni);
2593 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2596 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2597 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2598 libcfs_nid2str(peer2->ibp_nid),
2599 MAX_CONN_RACES_BEFORE_ABORT);
2601 * passive connection is allowed even this peer_ni is waiting for
2604 peer2->ibp_reconnecting = 0;
2605 peer2->ibp_races = 0;
2606 peer2->ibp_accepting++;
2607 kiblnd_peer_addref(peer2);
2609 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2610 * so copy validated parameters since we now know what the
2611 * peer_ni's limits are */
2612 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2613 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2615 write_unlock_irqrestore(g_lock, flags);
2616 kiblnd_peer_decref(peer_ni);
2619 /* Brand new peer_ni */
2620 LASSERT (peer_ni->ibp_accepting == 0);
2621 LASSERT (peer_ni->ibp_version == 0 &&
2622 peer_ni->ibp_incarnation == 0);
2624 peer_ni->ibp_accepting = 1;
2625 peer_ni->ibp_version = version;
2626 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2628 /* I have a ref on ni that prevents it being shutdown */
2629 LASSERT (net->ibn_shutdown == 0);
2631 kiblnd_peer_addref(peer_ni);
2632 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
2634 write_unlock_irqrestore(g_lock, flags);
2637 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2639 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2640 kiblnd_peer_decref(peer_ni);
2641 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2645 /* conn now "owns" cmid, so I return success from here on to ensure the
2646 * CM callback doesn't destroy cmid. */
2647 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2648 conn->ibc_credits = conn->ibc_queue_depth;
2649 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2650 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2651 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2653 ackmsg = &conn->ibc_connvars->cv_msg;
2654 memset(ackmsg, 0, sizeof(*ackmsg));
2656 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2657 sizeof(ackmsg->ibm_u.connparams));
2658 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2659 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2660 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2662 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2664 memset(&cp, 0, sizeof(cp));
2665 cp.private_data = ackmsg;
2666 cp.private_data_len = ackmsg->ibm_nob;
2667 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2668 cp.initiator_depth = 0;
2669 cp.flow_control = 1;
2670 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2671 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2673 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2675 rc = rdma_accept(cmid, &cp);
2677 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2678 rej.ibr_version = version;
2679 rej.ibr_why = IBLND_REJECT_FATAL;
2681 kiblnd_reject(cmid, &rej);
2682 kiblnd_connreq_done(conn, rc);
2683 kiblnd_conn_decref(conn);
2691 rej.ibr_cp.ibcp_queue_depth =
2692 kiblnd_msg_queue_size(version, ni);
2693 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2697 rej.ibr_version = version;
2698 kiblnd_reject(cmid, &rej);
2700 return -ECONNREFUSED;
2704 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2705 u64 incarnation, int why, struct kib_connparams *cp)
2707 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2708 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2710 int msg_size = IBLND_MSG_SIZE;
2714 unsigned long flags;
2716 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2717 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2720 msg_size = cp->ibcp_max_msg_size;
2721 frag_num = cp->ibcp_max_frags;
2722 queue_dep = cp->ibcp_queue_depth;
2725 write_lock_irqsave(glock, flags);
2726 /* retry connection if it's still needed and no other connection
2727 * attempts (active or passive) are in progress
2728 * NB: reconnect is still needed even when ibp_tx_queue is
2729 * empty if ibp_version != version because reconnect may be
2730 * initiated by kiblnd_query() */
2731 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2732 peer_ni->ibp_version != version) &&
2733 peer_ni->ibp_connecting &&
2734 peer_ni->ibp_accepting == 0;
2745 case IBLND_REJECT_RDMA_FRAGS: {
2746 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2749 reason = "can't negotiate max frags";
2752 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2753 #ifdef HAVE_IB_GET_DMA_MR
2755 * This check only makes sense if the kernel supports global
2756 * memory registration. Otherwise, map_on_demand will never == 0
2758 if (!tunables->lnd_map_on_demand) {
2759 reason = "map_on_demand must be enabled";
2763 if (conn->ibc_max_frags <= frag_num) {
2764 reason = "unsupported max frags";
2768 peer_ni->ibp_max_frags = frag_num;
2769 reason = "rdma fragments";
2772 case IBLND_REJECT_MSG_QUEUE_SIZE:
2774 reason = "can't negotiate queue depth";
2777 if (conn->ibc_queue_depth <= queue_dep) {
2778 reason = "unsupported queue depth";
2782 peer_ni->ibp_queue_depth = queue_dep;
2783 reason = "queue depth";
2786 case IBLND_REJECT_CONN_STALE:
2790 case IBLND_REJECT_CONN_RACE:
2791 reason = "conn race";
2794 case IBLND_REJECT_CONN_UNCOMPAT:
2795 reason = "version negotiation";
2798 case IBLND_REJECT_INVALID_SRV_ID:
2799 reason = "invalid service id";
2803 conn->ibc_reconnect = 1;
2804 peer_ni->ibp_reconnecting++;
2805 peer_ni->ibp_version = version;
2806 if (incarnation != 0)
2807 peer_ni->ibp_incarnation = incarnation;
2809 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2811 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2812 libcfs_nid2str(peer_ni->ibp_nid),
2813 reconnect ? "reconnect" : "don't reconnect",
2814 reason, IBLND_MSG_VERSION, version, msg_size,
2815 conn->ibc_queue_depth, queue_dep,
2816 conn->ibc_max_frags, frag_num);
2818 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2819 * while destroying the zombie
2824 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2826 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2828 LASSERT (!in_interrupt());
2829 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2832 case IB_CM_REJ_STALE_CONN:
2833 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2834 IBLND_REJECT_CONN_STALE, NULL);
2837 case IB_CM_REJ_INVALID_SERVICE_ID:
2838 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2839 IBLND_REJECT_INVALID_SRV_ID, NULL);
2840 CNETERR("%s rejected: no listener at %d\n",
2841 libcfs_nid2str(peer_ni->ibp_nid),
2842 *kiblnd_tunables.kib_service);
2845 case IB_CM_REJ_CONSUMER_DEFINED:
2846 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2847 struct kib_rej *rej = priv;
2848 struct kib_connparams *cp = NULL;
2850 __u64 incarnation = -1;
2852 /* NB. default incarnation is -1 because:
2853 * a) V1 will ignore dst incarnation in connreq.
2854 * b) V2 will provide incarnation while rejecting me,
2855 * -1 will be overwrote.
2857 * if I try to connect to a V1 peer_ni with V2 protocol,
2858 * it rejected me then upgrade to V2, I have no idea
2859 * about the upgrading and try to reconnect with V1,
2860 * in this case upgraded V2 can find out I'm trying to
2861 * talk to the old guy and reject me(incarnation is -1).
2864 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2865 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2866 __swab32s(&rej->ibr_magic);
2867 __swab16s(&rej->ibr_version);
2871 if (priv_nob >= sizeof(struct kib_rej) &&
2872 rej->ibr_version > IBLND_MSG_VERSION_1) {
2873 /* priv_nob is always 148 in current version
2874 * of OFED, so we still need to check version.
2875 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2879 __swab64s(&rej->ibr_incarnation);
2880 __swab16s(&cp->ibcp_queue_depth);
2881 __swab16s(&cp->ibcp_max_frags);
2882 __swab32s(&cp->ibcp_max_msg_size);
2885 incarnation = rej->ibr_incarnation;
2888 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2889 rej->ibr_magic != LNET_PROTO_MAGIC) {
2890 CERROR("%s rejected: consumer defined fatal error\n",
2891 libcfs_nid2str(peer_ni->ibp_nid));
2895 if (rej->ibr_version != IBLND_MSG_VERSION &&
2896 rej->ibr_version != IBLND_MSG_VERSION_1) {
2897 CERROR("%s rejected: o2iblnd version %x error\n",
2898 libcfs_nid2str(peer_ni->ibp_nid),
2903 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2904 rej->ibr_version == IBLND_MSG_VERSION_1) {
2905 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2906 libcfs_nid2str(peer_ni->ibp_nid), rej->ibr_version);
2908 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2909 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2912 switch (rej->ibr_why) {
2913 case IBLND_REJECT_CONN_RACE:
2914 case IBLND_REJECT_CONN_STALE:
2915 case IBLND_REJECT_CONN_UNCOMPAT:
2916 case IBLND_REJECT_MSG_QUEUE_SIZE:
2917 case IBLND_REJECT_RDMA_FRAGS:
2918 kiblnd_check_reconnect(conn, rej->ibr_version,
2919 incarnation, rej->ibr_why, cp);
2922 case IBLND_REJECT_NO_RESOURCES:
2923 CERROR("%s rejected: o2iblnd no resources\n",
2924 libcfs_nid2str(peer_ni->ibp_nid));
2927 case IBLND_REJECT_FATAL:
2928 CERROR("%s rejected: o2iblnd fatal error\n",
2929 libcfs_nid2str(peer_ni->ibp_nid));
2933 CERROR("%s rejected: o2iblnd reason %d\n",
2934 libcfs_nid2str(peer_ni->ibp_nid),
2942 CNETERR("%s rejected: reason %d, size %d\n",
2943 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
2947 kiblnd_connreq_done(conn, -ECONNREFUSED);
2951 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
2953 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2954 struct lnet_ni *ni = peer_ni->ibp_ni;
2955 struct kib_net *net = ni->ni_data;
2956 struct kib_msg *msg = priv;
2957 int ver = conn->ibc_version;
2958 int rc = kiblnd_unpack_msg(msg, priv_nob);
2959 unsigned long flags;
2961 LASSERT (net != NULL);
2964 CERROR("Can't unpack connack from %s: %d\n",
2965 libcfs_nid2str(peer_ni->ibp_nid), rc);
2969 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2970 CERROR("Unexpected message %d from %s\n",
2971 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
2976 if (ver != msg->ibm_version) {
2977 CERROR("%s replied version %x is different with "
2978 "requested version %x\n",
2979 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
2984 if (msg->ibm_u.connparams.ibcp_queue_depth >
2985 conn->ibc_queue_depth) {
2986 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
2987 libcfs_nid2str(peer_ni->ibp_nid),
2988 msg->ibm_u.connparams.ibcp_queue_depth,
2989 conn->ibc_queue_depth);
2994 if (msg->ibm_u.connparams.ibcp_max_frags >
2995 conn->ibc_max_frags) {
2996 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
2997 libcfs_nid2str(peer_ni->ibp_nid),
2998 msg->ibm_u.connparams.ibcp_max_frags,
2999 conn->ibc_max_frags);
3004 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3005 CERROR("%s max message size %d too big (%d max)\n",
3006 libcfs_nid2str(peer_ni->ibp_nid),
3007 msg->ibm_u.connparams.ibcp_max_msg_size,
3013 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3014 if (msg->ibm_dstnid == ni->ni_nid &&
3015 msg->ibm_dststamp == net->ibn_incarnation)
3019 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3022 CERROR("Bad connection reply from %s, rc = %d, "
3023 "version: %x max_frags: %d\n",
3024 libcfs_nid2str(peer_ni->ibp_nid), rc,
3025 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3029 conn->ibc_incarnation = msg->ibm_srcstamp;
3030 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3031 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3032 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3033 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3034 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3035 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3037 kiblnd_connreq_done(conn, 0);
3041 /* NB My QP has already established itself, so I handle anything going
3042 * wrong here by setting ibc_comms_error.
3043 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3044 * immediately tears it down. */
3047 conn->ibc_comms_error = rc;
3048 kiblnd_connreq_done(conn, 0);
3052 kiblnd_active_connect(struct rdma_cm_id *cmid)
3054 struct kib_peer_ni *peer_ni = cmid->context;
3055 struct kib_conn *conn;
3056 struct kib_msg *msg;
3057 struct rdma_conn_param cp;
3060 unsigned long flags;
3063 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3065 incarnation = peer_ni->ibp_incarnation;
3066 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3067 peer_ni->ibp_version;
3069 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3071 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3074 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3075 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3079 /* conn "owns" cmid now, so I return success from here on to ensure the
3080 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3083 msg = &conn->ibc_connvars->cv_msg;
3085 memset(msg, 0, sizeof(*msg));
3086 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3087 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3088 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3089 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3091 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3092 0, peer_ni->ibp_nid, incarnation);
3094 memset(&cp, 0, sizeof(cp));
3095 cp.private_data = msg;
3096 cp.private_data_len = msg->ibm_nob;
3097 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3098 cp.initiator_depth = 0;
3099 cp.flow_control = 1;
3100 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3101 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3103 LASSERT(cmid->context == (void *)conn);
3104 LASSERT(conn->ibc_cmid == cmid);
3106 rc = rdma_connect(cmid, &cp);
3108 CERROR("Can't connect to %s: %d\n",
3109 libcfs_nid2str(peer_ni->ibp_nid), rc);
3110 kiblnd_connreq_done(conn, rc);
3111 kiblnd_conn_decref(conn);
3118 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3120 struct kib_peer_ni *peer_ni;
3121 struct kib_conn *conn;
3124 switch (event->event) {
3126 CERROR("Unexpected event: %d, status: %d\n",
3127 event->event, event->status);
3130 case RDMA_CM_EVENT_CONNECT_REQUEST:
3131 /* destroy cmid on failure */
3132 rc = kiblnd_passive_connect(cmid,
3133 (void *)KIBLND_CONN_PARAM(event),
3134 KIBLND_CONN_PARAM_LEN(event));
3135 CDEBUG(D_NET, "connreq: %d\n", rc);
3138 case RDMA_CM_EVENT_ADDR_ERROR:
3139 peer_ni = cmid->context;
3140 CNETERR("%s: ADDR ERROR %d\n",
3141 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3142 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3143 kiblnd_peer_decref(peer_ni);
3144 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3146 case RDMA_CM_EVENT_ADDR_RESOLVED:
3147 peer_ni = cmid->context;
3149 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3150 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3152 if (event->status != 0) {
3153 CNETERR("Can't resolve address for %s: %d\n",
3154 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3157 rc = rdma_resolve_route(
3158 cmid, *kiblnd_tunables.kib_timeout * 1000);
3160 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3161 struct kib_dev *dev = net->ibn_dev;
3163 CDEBUG(D_NET, "%s: connection bound to "\
3165 libcfs_nid2str(peer_ni->ibp_nid),
3167 &dev->ibd_ifip, cmid->device->name);
3172 /* Can't initiate route resolution */
3173 CERROR("Can't resolve route for %s: %d\n",
3174 libcfs_nid2str(peer_ni->ibp_nid), rc);
3176 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3177 kiblnd_peer_decref(peer_ni);
3178 return rc; /* rc != 0 destroys cmid */
3180 case RDMA_CM_EVENT_ROUTE_ERROR:
3181 peer_ni = cmid->context;
3182 CNETERR("%s: ROUTE ERROR %d\n",
3183 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3184 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3185 kiblnd_peer_decref(peer_ni);
3186 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3188 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3189 peer_ni = cmid->context;
3190 CDEBUG(D_NET,"%s Route resolved: %d\n",
3191 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3193 if (event->status == 0)
3194 return kiblnd_active_connect(cmid);
3196 CNETERR("Can't resolve route for %s: %d\n",
3197 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3198 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3199 kiblnd_peer_decref(peer_ni);
3200 return event->status; /* rc != 0 destroys cmid */
3202 case RDMA_CM_EVENT_UNREACHABLE:
3203 conn = cmid->context;
3204 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3205 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3206 CNETERR("%s: UNREACHABLE %d\n",
3207 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3208 kiblnd_connreq_done(conn, -ENETDOWN);
3209 kiblnd_conn_decref(conn);
3212 case RDMA_CM_EVENT_CONNECT_ERROR:
3213 conn = cmid->context;
3214 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3215 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3216 CNETERR("%s: CONNECT ERROR %d\n",
3217 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3218 kiblnd_connreq_done(conn, -ENOTCONN);
3219 kiblnd_conn_decref(conn);
3222 case RDMA_CM_EVENT_REJECTED:
3223 conn = cmid->context;
3224 switch (conn->ibc_state) {
3228 case IBLND_CONN_PASSIVE_WAIT:
3229 CERROR ("%s: REJECTED %d\n",
3230 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3232 kiblnd_connreq_done(conn, -ECONNRESET);
3235 case IBLND_CONN_ACTIVE_CONNECT:
3236 kiblnd_rejected(conn, event->status,
3237 (void *)KIBLND_CONN_PARAM(event),
3238 KIBLND_CONN_PARAM_LEN(event));
3241 kiblnd_conn_decref(conn);
3244 case RDMA_CM_EVENT_ESTABLISHED:
3245 conn = cmid->context;
3246 switch (conn->ibc_state) {
3250 case IBLND_CONN_PASSIVE_WAIT:
3251 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3252 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3253 kiblnd_connreq_done(conn, 0);
3256 case IBLND_CONN_ACTIVE_CONNECT:
3257 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3258 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3259 kiblnd_check_connreply(conn,
3260 (void *)KIBLND_CONN_PARAM(event),
3261 KIBLND_CONN_PARAM_LEN(event));
3264 /* net keeps its ref on conn! */
3267 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3268 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3271 case RDMA_CM_EVENT_DISCONNECTED:
3272 conn = cmid->context;
3273 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3274 CERROR("%s DISCONNECTED\n",
3275 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3276 kiblnd_connreq_done(conn, -ECONNRESET);
3278 kiblnd_close_conn(conn, 0);
3280 kiblnd_conn_decref(conn);
3281 cmid->context = NULL;
3284 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3285 LCONSOLE_ERROR_MSG(0x131,
3286 "Received notification of device removal\n"
3287 "Please shutdown LNET to allow this to proceed\n");
3288 /* Can't remove network from underneath LNET for now, so I have
3292 case RDMA_CM_EVENT_ADDR_CHANGE:
3293 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3299 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3302 struct list_head *ttmp;
3304 list_for_each(ttmp, txs) {
3305 tx = list_entry(ttmp, struct kib_tx, tx_list);
3307 if (txs != &conn->ibc_active_txs) {
3308 LASSERT(tx->tx_queued);
3310 LASSERT(!tx->tx_queued);
3311 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3314 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3315 CERROR("Timed out tx: %s, %lld seconds\n",
3316 kiblnd_queue2str(conn, txs),
3317 ktime_ms_delta(ktime_get(),
3318 tx->tx_deadline) / MSEC_PER_SEC);
3327 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3329 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3330 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3331 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3332 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3333 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3337 kiblnd_check_conns (int idx)
3339 struct list_head closes = LIST_HEAD_INIT(closes);
3340 struct list_head checksends = LIST_HEAD_INIT(checksends);
3341 struct list_head timedout_txs = LIST_HEAD_INIT(timedout_txs);
3342 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3343 struct list_head *ptmp;
3344 struct kib_peer_ni *peer_ni;
3345 struct kib_conn *conn;
3346 struct kib_tx *tx, *tx_tmp;
3347 struct list_head *ctmp;
3348 unsigned long flags;
3350 /* NB. We expect to have a look at all the peers and not find any
3351 * RDMAs to time out, so we just use a shared lock while we
3353 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3355 list_for_each(ptmp, peers) {
3356 peer_ni = list_entry(ptmp, struct kib_peer_ni, ibp_list);
3358 /* Check tx_deadline */
3359 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3360 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3361 CWARN("Timed out tx for %s: %lld seconds\n",
3362 libcfs_nid2str(peer_ni->ibp_nid),
3363 ktime_ms_delta(ktime_get(),
3364 tx->tx_deadline) / MSEC_PER_SEC);
3365 list_move(&tx->tx_list, &timedout_txs);
3369 list_for_each(ctmp, &peer_ni->ibp_conns) {
3373 conn = list_entry(ctmp, struct kib_conn, ibc_list);
3375 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3377 spin_lock(&conn->ibc_lock);
3379 sendnoop = kiblnd_need_noop(conn);
3380 timedout = kiblnd_conn_timed_out_locked(conn);
3381 if (!sendnoop && !timedout) {
3382 spin_unlock(&conn->ibc_lock);
3387 CERROR("Timed out RDMA with %s (%lld): "
3388 "c: %u, oc: %u, rc: %u\n",
3389 libcfs_nid2str(peer_ni->ibp_nid),
3390 ktime_get_seconds() - peer_ni->ibp_last_alive,
3392 conn->ibc_outstanding_credits,
3393 conn->ibc_reserved_credits);
3394 list_add(&conn->ibc_connd_list, &closes);
3396 list_add(&conn->ibc_connd_list, &checksends);
3398 /* +ref for 'closes' or 'checksends' */
3399 kiblnd_conn_addref(conn);
3401 spin_unlock(&conn->ibc_lock);
3405 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3407 if (!list_empty(&timedout_txs))
3408 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3409 LNET_MSG_STATUS_LOCAL_TIMEOUT);
3411 /* Handle timeout by closing the whole
3412 * connection. We can only be sure RDMA activity
3413 * has ceased once the QP has been modified. */
3414 while (!list_empty(&closes)) {
3415 conn = list_entry(closes.next,
3416 struct kib_conn, ibc_connd_list);
3417 list_del(&conn->ibc_connd_list);
3418 kiblnd_close_conn(conn, -ETIMEDOUT);
3419 kiblnd_conn_decref(conn);
3422 /* In case we have enough credits to return via a
3423 * NOOP, but there were no non-blocking tx descs
3424 * free to do it last time... */
3425 while (!list_empty(&checksends)) {
3426 conn = list_entry(checksends.next,
3427 struct kib_conn, ibc_connd_list);
3428 list_del(&conn->ibc_connd_list);
3430 spin_lock(&conn->ibc_lock);
3431 kiblnd_check_sends_locked(conn);
3432 spin_unlock(&conn->ibc_lock);
3434 kiblnd_conn_decref(conn);
3439 kiblnd_disconnect_conn(struct kib_conn *conn)
3441 LASSERT (!in_interrupt());
3442 LASSERT (current == kiblnd_data.kib_connd);
3443 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3445 rdma_disconnect(conn->ibc_cmid);
3446 kiblnd_finalise_conn(conn);
3448 kiblnd_peer_notify(conn->ibc_peer);
3452 * High-water for reconnection to the same peer_ni, reconnection attempt should
3453 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3455 #define KIB_RECONN_HIGH_RACE 10
3457 * Allow connd to take a break and handle other things after consecutive
3458 * reconnection attemps.
3460 #define KIB_RECONN_BREAK 100
3463 kiblnd_connd (void *arg)
3465 spinlock_t *lock= &kiblnd_data.kib_connd_lock;
3466 wait_queue_entry_t wait;
3467 unsigned long flags;
3468 struct kib_conn *conn;
3473 unsigned long deadline = jiffies;
3475 cfs_block_allsigs();
3477 init_waitqueue_entry(&wait, current);
3478 kiblnd_data.kib_connd = current;
3480 spin_lock_irqsave(lock, flags);
3482 while (!kiblnd_data.kib_shutdown) {
3487 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3488 struct kib_peer_ni *peer_ni = NULL;
3490 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3491 struct kib_conn, ibc_list);
3492 list_del(&conn->ibc_list);
3493 if (conn->ibc_reconnect) {
3494 peer_ni = conn->ibc_peer;
3495 kiblnd_peer_addref(peer_ni);
3498 spin_unlock_irqrestore(lock, flags);
3501 kiblnd_destroy_conn(conn);
3503 spin_lock_irqsave(lock, flags);
3505 LIBCFS_FREE(conn, sizeof(*conn));
3509 conn->ibc_peer = peer_ni;
3510 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3511 list_add_tail(&conn->ibc_list,
3512 &kiblnd_data.kib_reconn_list);
3514 list_add_tail(&conn->ibc_list,
3515 &kiblnd_data.kib_reconn_wait);
3518 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3519 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3520 struct kib_conn, ibc_list);
3521 list_del(&conn->ibc_list);
3523 spin_unlock_irqrestore(lock, flags);
3526 kiblnd_disconnect_conn(conn);
3527 kiblnd_conn_decref(conn);
3529 spin_lock_irqsave(lock, flags);
3532 while (reconn < KIB_RECONN_BREAK) {
3533 if (kiblnd_data.kib_reconn_sec !=
3534 ktime_get_real_seconds()) {
3535 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3536 list_splice_init(&kiblnd_data.kib_reconn_wait,
3537 &kiblnd_data.kib_reconn_list);
3540 if (list_empty(&kiblnd_data.kib_reconn_list))
3543 conn = list_entry(kiblnd_data.kib_reconn_list.next,
3544 struct kib_conn, ibc_list);
3545 list_del(&conn->ibc_list);
3547 spin_unlock_irqrestore(lock, flags);
3550 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3551 kiblnd_peer_decref(conn->ibc_peer);
3552 LIBCFS_FREE(conn, sizeof(*conn));
3554 spin_lock_irqsave(lock, flags);
3557 /* careful with the jiffy wrap... */
3558 timeout = (int)(deadline - jiffies);
3562 int chunk = kiblnd_data.kib_peer_hash_size;
3564 spin_unlock_irqrestore(lock, flags);
3567 /* Time to check for RDMA timeouts on a few more
3568 * peers: I do checks every 'p' seconds on a
3569 * proportion of the peer_ni table and I need to check
3570 * every connection 'n' times within a timeout
3571 * interval, to ensure I detect a timeout on any
3572 * connection within (n+1)/n times the timeout
3575 if (*kiblnd_tunables.kib_timeout > n * p)
3576 chunk = (chunk * n * p) /
3577 *kiblnd_tunables.kib_timeout;
3581 for (i = 0; i < chunk; i++) {
3582 kiblnd_check_conns(peer_index);
3583 peer_index = (peer_index + 1) %
3584 kiblnd_data.kib_peer_hash_size;
3587 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3588 spin_lock_irqsave(lock, flags);
3594 /* Nothing to do for 'timeout' */
3595 set_current_state(TASK_INTERRUPTIBLE);
3596 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3597 spin_unlock_irqrestore(lock, flags);
3599 schedule_timeout(timeout);
3601 set_current_state(TASK_RUNNING);
3602 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3603 spin_lock_irqsave(lock, flags);
3606 spin_unlock_irqrestore(lock, flags);
3608 kiblnd_thread_fini();
3613 kiblnd_qp_event(struct ib_event *event, void *arg)
3615 struct kib_conn *conn = arg;
3617 switch (event->event) {
3618 case IB_EVENT_COMM_EST:
3619 CDEBUG(D_NET, "%s established\n",
3620 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3621 /* We received a packet but connection isn't established
3622 * probably handshake packet was lost, so free to
3623 * force make connection established */
3624 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3628 CERROR("%s: Async QP event type %d\n",
3629 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3635 kiblnd_complete (struct ib_wc *wc)
3637 switch (kiblnd_wreqid2type(wc->wr_id)) {
3642 if (wc->status != IB_WC_SUCCESS &&
3643 wc->status != IB_WC_WR_FLUSH_ERR)
3644 CNETERR("FastReg failed: %d\n", wc->status);
3647 case IBLND_WID_RDMA:
3648 /* We only get RDMA completion notification if it fails. All
3649 * subsequent work items, including the final SEND will fail
3650 * too. However we can't print out any more info about the
3651 * failing RDMA because 'tx' might be back on the idle list or
3652 * even reused already if we didn't manage to post all our work
3654 CNETERR("RDMA (tx: %p) failed: %d\n",
3655 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3659 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3663 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3670 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3672 /* NB I'm not allowed to schedule this conn once its refcount has
3673 * reached 0. Since fundamentally I'm racing with scheduler threads
3674 * consuming my CQ I could be called after all completions have
3675 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3676 * and this CQ is about to be destroyed so I NOOP. */
3677 struct kib_conn *conn = arg;
3678 struct kib_sched_info *sched = conn->ibc_sched;
3679 unsigned long flags;
3681 LASSERT(cq == conn->ibc_cq);
3683 spin_lock_irqsave(&sched->ibs_lock, flags);
3685 conn->ibc_ready = 1;
3687 if (!conn->ibc_scheduled &&
3688 (conn->ibc_nrx > 0 ||
3689 conn->ibc_nsends_posted > 0)) {
3690 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3691 conn->ibc_scheduled = 1;
3692 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3694 if (waitqueue_active(&sched->ibs_waitq))
3695 wake_up(&sched->ibs_waitq);
3698 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3702 kiblnd_cq_event(struct ib_event *event, void *arg)
3704 struct kib_conn *conn = arg;
3706 CERROR("%s: async CQ event type %d\n",
3707 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3711 kiblnd_scheduler(void *arg)
3713 long id = (long)arg;
3714 struct kib_sched_info *sched;
3715 struct kib_conn *conn;
3716 wait_queue_entry_t wait;
3717 unsigned long flags;
3723 cfs_block_allsigs();
3725 init_waitqueue_entry(&wait, current);
3727 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3729 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3731 CWARN("Unable to bind on CPU partition %d, please verify "
3732 "whether all CPUs are healthy and reload modules if "
3733 "necessary, otherwise your system might under risk of "
3734 "low performance\n", sched->ibs_cpt);
3737 spin_lock_irqsave(&sched->ibs_lock, flags);
3739 while (!kiblnd_data.kib_shutdown) {
3740 if (busy_loops++ >= IBLND_RESCHED) {
3741 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3746 spin_lock_irqsave(&sched->ibs_lock, flags);
3751 if (!list_empty(&sched->ibs_conns)) {
3752 conn = list_entry(sched->ibs_conns.next,
3753 struct kib_conn, ibc_sched_list);
3754 /* take over kib_sched_conns' ref on conn... */
3755 LASSERT(conn->ibc_scheduled);
3756 list_del(&conn->ibc_sched_list);
3757 conn->ibc_ready = 0;
3759 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3761 wc.wr_id = IBLND_WID_INVAL;
3763 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3765 rc = ib_req_notify_cq(conn->ibc_cq,
3768 CWARN("%s: ib_req_notify_cq failed: %d, "
3769 "closing connection\n",
3770 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3771 kiblnd_close_conn(conn, -EIO);
3772 kiblnd_conn_decref(conn);
3773 spin_lock_irqsave(&sched->ibs_lock,
3778 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3781 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3783 "ib_poll_cq (rc: %d) returned invalid "
3784 "wr_id, opcode %d, status: %d, "
3785 "vendor_err: %d, conn: %s status: %d\n"
3786 "please upgrade firmware and OFED or "
3787 "contact vendor.\n", rc,
3788 wc.opcode, wc.status, wc.vendor_err,
3789 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3795 CWARN("%s: ib_poll_cq failed: %d, "
3796 "closing connection\n",
3797 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3799 kiblnd_close_conn(conn, -EIO);
3800 kiblnd_conn_decref(conn);
3801 spin_lock_irqsave(&sched->ibs_lock, flags);
3805 spin_lock_irqsave(&sched->ibs_lock, flags);
3807 if (rc != 0 || conn->ibc_ready) {
3808 /* There may be another completion waiting; get
3809 * another scheduler to check while I handle
3811 /* +1 ref for sched_conns */
3812 kiblnd_conn_addref(conn);
3813 list_add_tail(&conn->ibc_sched_list,
3815 if (waitqueue_active(&sched->ibs_waitq))
3816 wake_up(&sched->ibs_waitq);
3818 conn->ibc_scheduled = 0;
3822 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3823 kiblnd_complete(&wc);
3825 spin_lock_irqsave(&sched->ibs_lock, flags);
3828 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3835 set_current_state(TASK_INTERRUPTIBLE);
3836 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3837 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3842 remove_wait_queue(&sched->ibs_waitq, &wait);
3843 set_current_state(TASK_RUNNING);
3844 spin_lock_irqsave(&sched->ibs_lock, flags);
3847 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3849 kiblnd_thread_fini();
3854 kiblnd_failover_thread(void *arg)
3856 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3857 struct kib_dev *dev;
3858 wait_queue_entry_t wait;
3859 unsigned long flags;
3862 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3864 cfs_block_allsigs();
3866 init_waitqueue_entry(&wait, current);
3867 write_lock_irqsave(glock, flags);
3869 while (!kiblnd_data.kib_shutdown) {
3870 int do_failover = 0;
3873 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3875 if (ktime_get_seconds() < dev->ibd_next_failover)
3882 list_del_init(&dev->ibd_fail_list);
3883 dev->ibd_failover = 1;
3884 write_unlock_irqrestore(glock, flags);
3886 rc = kiblnd_dev_failover(dev);
3888 write_lock_irqsave(glock, flags);
3890 LASSERT (dev->ibd_failover);
3891 dev->ibd_failover = 0;
3892 if (rc >= 0) { /* Device is OK or failover succeed */
3893 dev->ibd_next_failover = ktime_get_seconds() + 3;
3897 /* failed to failover, retry later */
3898 dev->ibd_next_failover = ktime_get_seconds() +
3899 min(dev->ibd_failed_failover, 10);
3900 if (kiblnd_dev_can_failover(dev)) {
3901 list_add_tail(&dev->ibd_fail_list,
3902 &kiblnd_data.kib_failed_devs);
3908 /* long sleep if no more pending failover */
3909 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3911 set_current_state(TASK_INTERRUPTIBLE);
3912 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3913 write_unlock_irqrestore(glock, flags);
3915 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3916 cfs_time_seconds(1));
3917 set_current_state(TASK_RUNNING);
3918 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3919 write_lock_irqsave(glock, flags);
3921 if (!long_sleep || rc != 0)
3924 /* have a long sleep, routine check all active devices,
3925 * we need checking like this because if there is not active
3926 * connection on the dev and no SEND from local, we may listen
3927 * on wrong HCA for ever while there is a bonding failover */
3928 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3929 if (kiblnd_dev_can_failover(dev)) {
3930 list_add_tail(&dev->ibd_fail_list,
3931 &kiblnd_data.kib_failed_devs);
3936 write_unlock_irqrestore(glock, flags);
3938 kiblnd_thread_fini();