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/
31 * lnet/klnds/o2iblnd/o2iblnd_cb.c
33 * Author: Eric Barton <eric@bartonsoftware.com>
38 #define MAX_CONN_RACES_BEFORE_ABORT 20
40 static void kiblnd_peer_alive(struct kib_peer_ni *peer_ni);
41 static void kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
43 static void kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx,
44 int type, int body_nob);
45 static int kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
46 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie);
47 static void kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn);
48 static void kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn);
50 static void kiblnd_unmap_tx(struct kib_tx *tx);
51 static void kiblnd_check_sends_locked(struct kib_conn *conn);
54 kiblnd_tx_done(struct kib_tx *tx)
56 struct lnet_msg *lntmsg[2];
60 LASSERT (!in_interrupt());
61 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
62 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
63 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer_ni response */
64 LASSERT (tx->tx_pool != NULL);
68 /* tx may have up to 2 lnet msgs to finalise */
69 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
70 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
73 if (tx->tx_conn != NULL) {
74 kiblnd_conn_decref(tx->tx_conn);
78 tx->tx_nwrq = tx->tx_nsge = 0;
81 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
83 /* delay finalize until my descs have been freed */
84 for (i = 0; i < 2; i++) {
85 if (lntmsg[i] == NULL)
88 /* propagate health status to LNet for requests */
89 if (i == 0 && lntmsg[i])
90 lntmsg[i]->msg_health_status = tx->tx_hstatus;
92 lnet_finalize(lntmsg[i], rc);
97 kiblnd_txlist_done(struct list_head *txlist, int status,
98 enum lnet_msg_hstatus hstatus)
102 while ((tx = list_first_entry_or_null(txlist,
105 list_del(&tx->tx_list);
108 tx->tx_status = status;
109 if (hstatus != LNET_MSG_STATUS_OK)
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 #ifdef HAVE_IB_POST_SEND_RECV_CONST
206 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq,
207 (const struct ib_recv_wr **)&bad_wrq);
209 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
211 if (unlikely(rc != 0)) {
212 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
213 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
217 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
220 if (unlikely(rc != 0)) {
221 kiblnd_close_conn(conn, rc);
222 kiblnd_drop_rx(rx); /* No more posts for this rx */
226 if (credit == IBLND_POSTRX_NO_CREDIT)
229 spin_lock(&conn->ibc_lock);
230 if (credit == IBLND_POSTRX_PEER_CREDIT)
231 conn->ibc_outstanding_credits++;
233 conn->ibc_reserved_credits++;
234 kiblnd_check_sends_locked(conn);
235 spin_unlock(&conn->ibc_lock);
238 kiblnd_conn_decref(conn);
242 static struct kib_tx *
243 kiblnd_find_waiting_tx_locked(struct kib_conn *conn, int txtype, u64 cookie)
247 list_for_each_entry(tx, &conn->ibc_active_txs, tx_list) {
248 LASSERT(!tx->tx_queued);
249 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
251 if (tx->tx_cookie != cookie)
254 if (tx->tx_waiting &&
255 tx->tx_msg->ibm_type == txtype)
258 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
259 tx->tx_waiting ? "" : "NOT ",
260 tx->tx_msg->ibm_type, txtype);
266 kiblnd_handle_completion(struct kib_conn *conn, int txtype, int status, u64 cookie)
269 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
272 spin_lock(&conn->ibc_lock);
274 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
276 spin_unlock(&conn->ibc_lock);
278 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
279 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
280 kiblnd_close_conn(conn, -EPROTO);
284 if (tx->tx_status == 0) { /* success so far */
285 if (status < 0) { /* failed? */
286 tx->tx_status = status;
287 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
288 } else if (txtype == IBLND_MSG_GET_REQ) {
289 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
295 idle = !tx->tx_queued && (tx->tx_sending == 0);
297 list_del(&tx->tx_list);
299 spin_unlock(&conn->ibc_lock);
306 kiblnd_send_completion(struct kib_conn *conn, int type, int status, u64 cookie)
308 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
309 struct kib_tx *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
312 CERROR("Can't get tx for completion %x for %s\n",
313 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
317 tx->tx_msg->ibm_u.completion.ibcm_status = status;
318 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
319 kiblnd_init_tx_msg(ni, tx, type, sizeof(struct kib_completion_msg));
321 kiblnd_queue_tx(tx, conn);
325 kiblnd_handle_rx(struct kib_rx *rx)
327 struct kib_msg *msg = rx->rx_msg;
328 struct kib_conn *conn = rx->rx_conn;
329 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
330 int credits = msg->ibm_credits;
337 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
339 CDEBUG (D_NET, "Received %x[%d] from %s\n",
340 msg->ibm_type, credits,
341 libcfs_nid2str(conn->ibc_peer->ibp_nid));
344 /* Have I received credits that will let me send? */
345 spin_lock(&conn->ibc_lock);
347 if (conn->ibc_credits + credits >
348 conn->ibc_queue_depth) {
349 rc2 = conn->ibc_credits;
350 spin_unlock(&conn->ibc_lock);
352 CERROR("Bad credits from %s: %d + %d > %d\n",
353 libcfs_nid2str(conn->ibc_peer->ibp_nid),
355 conn->ibc_queue_depth);
357 kiblnd_close_conn(conn, -EPROTO);
358 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
362 conn->ibc_credits += credits;
364 /* This ensures the credit taken by NOOP can be returned */
365 if (msg->ibm_type == IBLND_MSG_NOOP &&
366 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
367 conn->ibc_outstanding_credits++;
369 kiblnd_check_sends_locked(conn);
370 spin_unlock(&conn->ibc_lock);
373 switch (msg->ibm_type) {
375 CERROR("Bad IBLND message type %x from %s\n",
376 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
377 post_credit = IBLND_POSTRX_NO_CREDIT;
382 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
383 post_credit = IBLND_POSTRX_NO_CREDIT;
387 if (credits != 0) /* credit already posted */
388 post_credit = IBLND_POSTRX_NO_CREDIT;
389 else /* a keepalive NOOP */
390 post_credit = IBLND_POSTRX_PEER_CREDIT;
393 case IBLND_MSG_IMMEDIATE:
394 post_credit = IBLND_POSTRX_DONT_POST;
395 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.immediate.ibim_hdr);
396 rc = lnet_parse(ni, &hdr, msg->ibm_srcnid, rx, 0);
397 if (rc < 0) /* repost on error */
398 post_credit = IBLND_POSTRX_PEER_CREDIT;
401 case IBLND_MSG_PUT_REQ:
402 post_credit = IBLND_POSTRX_DONT_POST;
403 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.putreq.ibprm_hdr);
404 rc = lnet_parse(ni, &hdr, msg->ibm_srcnid, rx, 1);
405 if (rc < 0) /* repost on error */
406 post_credit = IBLND_POSTRX_PEER_CREDIT;
409 case IBLND_MSG_PUT_NAK:
410 CWARN ("PUT_NACK from %s\n",
411 libcfs_nid2str(conn->ibc_peer->ibp_nid));
412 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
413 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
414 msg->ibm_u.completion.ibcm_status,
415 msg->ibm_u.completion.ibcm_cookie);
418 case IBLND_MSG_PUT_ACK:
419 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
421 spin_lock(&conn->ibc_lock);
422 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
423 msg->ibm_u.putack.ibpam_src_cookie);
425 list_del(&tx->tx_list);
426 spin_unlock(&conn->ibc_lock);
429 CERROR("Unmatched PUT_ACK from %s\n",
430 libcfs_nid2str(conn->ibc_peer->ibp_nid));
435 LASSERT (tx->tx_waiting);
436 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
437 * (a) I can overwrite tx_msg since my peer_ni has received it!
438 * (b) tx_waiting set tells tx_complete() it's not done. */
440 tx->tx_nwrq = tx->tx_nsge = 0; /* overwrite PUT_REQ */
442 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
443 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
444 &msg->ibm_u.putack.ibpam_rd,
445 msg->ibm_u.putack.ibpam_dst_cookie);
447 CERROR("Can't setup rdma for PUT to %s: %d\n",
448 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
450 spin_lock(&conn->ibc_lock);
451 tx->tx_waiting = 0; /* clear waiting and queue atomically */
452 kiblnd_queue_tx_locked(tx, conn);
453 spin_unlock(&conn->ibc_lock);
456 case IBLND_MSG_PUT_DONE:
457 post_credit = IBLND_POSTRX_PEER_CREDIT;
458 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
459 msg->ibm_u.completion.ibcm_status,
460 msg->ibm_u.completion.ibcm_cookie);
463 case IBLND_MSG_GET_REQ:
464 post_credit = IBLND_POSTRX_DONT_POST;
465 lnet_hdr_from_nid4(&hdr, &msg->ibm_u.get.ibgm_hdr);
466 rc = lnet_parse(ni, &hdr, msg->ibm_srcnid, rx, 1);
467 if (rc < 0) /* repost on error */
468 post_credit = IBLND_POSTRX_PEER_CREDIT;
471 case IBLND_MSG_GET_DONE:
472 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
473 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
474 msg->ibm_u.completion.ibcm_status,
475 msg->ibm_u.completion.ibcm_cookie);
479 if (rc < 0) /* protocol error */
480 kiblnd_close_conn(conn, rc);
482 if (post_credit != IBLND_POSTRX_DONT_POST)
483 kiblnd_post_rx(rx, post_credit);
487 kiblnd_rx_complete(struct kib_rx *rx, int status, int nob)
489 struct kib_msg *msg = rx->rx_msg;
490 struct kib_conn *conn = rx->rx_conn;
491 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
492 struct kib_net *net = ni->ni_data;
497 LASSERT(rx->rx_nob < 0); /* was posted */
498 rx->rx_nob = 0; /* isn't now */
500 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
503 if (status != IB_WC_SUCCESS) {
504 CNETERR("Rx from %s failed: %d\n",
505 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
512 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
514 CERROR("Error %d unpacking rx from %s\n",
515 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
519 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
520 msg->ibm_dstnid != lnet_nid_to_nid4(&ni->ni_nid) ||
521 msg->ibm_srcstamp != conn->ibc_incarnation ||
522 msg->ibm_dststamp != net->ibn_incarnation) {
523 CERROR("Stale rx from %s\n",
524 libcfs_nid2str(conn->ibc_peer->ibp_nid));
529 /* set time last known alive */
530 kiblnd_peer_alive(conn->ibc_peer);
532 /* racing with connection establishment/teardown! */
534 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
535 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
538 write_lock_irqsave(g_lock, flags);
539 /* must check holding global lock to eliminate race */
540 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
541 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
542 write_unlock_irqrestore(g_lock, flags);
545 write_unlock_irqrestore(g_lock, flags);
547 kiblnd_handle_rx(rx);
551 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
552 kiblnd_close_conn(conn, err);
554 kiblnd_drop_rx(rx); /* Don't re-post rx. */
558 kiblnd_fmr_map_tx(struct kib_net *net, struct kib_tx *tx,
559 struct kib_rdma_desc *rd, u32 nob)
561 struct kib_hca_dev *hdev;
563 struct kib_fmr_poolset *fps;
568 LASSERT(tx->tx_pool != NULL);
569 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
572 hdev = tx->tx_pool->tpo_hdev;
573 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
576 * If we're dealing with FastReg, but the device doesn't
577 * support GAPS and the tx has GAPS, then there is no real point
578 * in trying to map the memory, because it'll just fail. So
579 * preemptively fail with an appropriate message
581 if ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED) &&
582 !(dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_GAPS_SUPPORT) &&
584 CERROR("Using FastReg with no GAPS support, but tx has gaps. "
585 "Try setting use_fastreg_gaps to 1\n");
586 return -EPROTONOSUPPORT;
589 #ifdef HAVE_FMR_POOL_API
591 * FMR does not support gaps but the tx has gaps then
592 * we should make sure that the number of fragments we'll be sending
593 * over fits within the number of fragments negotiated on the
594 * connection, otherwise, we won't be able to RDMA the data.
595 * We need to maintain the number of fragments negotiation on the
596 * connection for backwards compatibility.
598 if (tx->tx_gaps && (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)) {
600 tx->tx_conn->ibc_max_frags <= rd->rd_nfrags) {
601 CERROR("TX number of frags (%d) is <= than connection"
602 " number of frags (%d). Consider setting peer's"
603 " map_on_demand to 256\n", tx->tx_nfrags,
604 tx->tx_conn->ibc_max_frags);
610 fps = net->ibn_fmr_ps[cpt];
611 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->tx_fmr);
613 CERROR("Can't map %u bytes (%u/%u)s: %d\n", nob,
614 tx->tx_nfrags, rd->rd_nfrags, rc);
619 * If rd is not tx_rd, it's going to get sent to a peer_ni, who will
622 rd->rd_key = tx->tx_fmr.fmr_key;
624 * for FastReg or FMR with no gaps we can accumulate all
625 * the fragments in one FastReg or FMR fragment.
628 #ifdef HAVE_FMR_POOL_API
629 ((dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
632 (dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED)) {
633 /* FMR requires zero based address */
634 #ifdef HAVE_FMR_POOL_API
635 if (dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED)
636 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
638 rd->rd_frags[0].rf_nob = nob;
642 * We're transmitting with gaps using FMR.
643 * We'll need to use multiple fragments and identify the
644 * zero based address of each fragment.
646 for (i = 0; i < rd->rd_nfrags; i++) {
647 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
648 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
656 kiblnd_unmap_tx(struct kib_tx *tx)
659 #ifdef HAVE_FMR_POOL_API
660 tx->tx_fmr.fmr_pfmr ||
663 kiblnd_fmr_pool_unmap(&tx->tx_fmr, tx->tx_status);
665 if (tx->tx_nfrags != 0) {
666 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev,
667 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
672 #ifdef HAVE_IB_GET_DMA_MR
673 static struct ib_mr *
674 kiblnd_find_rd_dma_mr(struct lnet_ni *ni, struct kib_rdma_desc *rd)
676 struct kib_net *net = ni->ni_data;
677 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
678 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
680 tunables = &ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
683 * if map-on-demand is turned on and the device supports
684 * either FMR or FastReg then use that. Otherwise use global
685 * memory regions. If that's not available either, then you're
686 * dead in the water and fail the operation.
688 if (tunables->lnd_map_on_demand &&
689 (net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FASTREG_ENABLED
690 #ifdef HAVE_FMR_POOL_API
691 || net->ibn_dev->ibd_dev_caps & IBLND_DEV_CAPS_FMR_ENABLED
697 * hdev->ibh_mrs can be NULL. This case is dealt with gracefully
698 * in the call chain. The mapping will fail with appropriate error
701 return hdev->ibh_mrs;
705 static int kiblnd_map_tx(struct lnet_ni *ni, struct kib_tx *tx,
706 struct kib_rdma_desc *rd, int nfrags)
708 struct kib_net *net = ni->ni_data;
709 struct kib_hca_dev *hdev = net->ibn_dev->ibd_hdev;
710 #ifdef HAVE_IB_GET_DMA_MR
711 struct ib_mr *mr = NULL;
716 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
718 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
719 tx->tx_nfrags = nfrags;
721 rd->rd_nfrags = kiblnd_dma_map_sg(hdev, tx->tx_frags,
722 tx->tx_nfrags, tx->tx_dmadir);
724 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
725 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
726 hdev->ibh_ibdev, &tx->tx_frags[i]);
727 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
728 hdev->ibh_ibdev, &tx->tx_frags[i]);
729 nob += rd->rd_frags[i].rf_nob;
732 #ifdef HAVE_IB_GET_DMA_MR
733 mr = kiblnd_find_rd_dma_mr(ni, rd);
735 /* found pre-mapping MR */
736 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
741 if (net->ibn_fmr_ps != NULL)
742 return kiblnd_fmr_map_tx(net, tx, rd, nob);
747 static int kiblnd_setup_rd_kiov(struct lnet_ni *ni, struct kib_tx *tx,
748 struct kib_rdma_desc *rd, int nkiov,
749 struct bio_vec *kiov, int offset, int nob)
751 struct kib_net *net = ni->ni_data;
752 struct scatterlist *sg;
757 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
761 LASSERT(net != NULL);
763 while (offset >= kiov->bv_len) {
764 offset -= kiov->bv_len;
777 CERROR("lacking enough sg entries to map tx\n");
782 fragnob = min((int)(kiov->bv_len - offset), nob);
785 * We're allowed to start at a non-aligned page offset in
786 * the first fragment and end at a non-aligned page offset
787 * in the last fragment.
789 if ((fragnob < (int)(kiov->bv_len - offset)) &&
790 nkiov < max_nkiov && nob > fragnob) {
791 CDEBUG(D_NET, "fragnob %d < available page %d: with"
792 " remaining %d kiovs with %d nob left\n",
793 fragnob, (int)(kiov->bv_len - offset),
798 sg_set_page(sg, kiov->bv_page, fragnob,
799 kiov->bv_offset + offset);
808 return kiblnd_map_tx(ni, tx, rd, sg_count);
812 kiblnd_post_tx_locked(struct kib_conn *conn, struct kib_tx *tx, int credit)
813 __must_hold(&conn->ibc_lock)
815 struct kib_msg *msg = tx->tx_msg;
816 struct kib_peer_ni *peer_ni = conn->ibc_peer;
817 struct lnet_ni *ni = peer_ni->ibp_ni;
818 struct kib_fast_reg_descriptor *frd = tx->tx_fmr.fmr_frd;
819 int ver = conn->ibc_version;
823 LASSERT(tx->tx_queued);
824 /* We rely on this for QP sizing */
825 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
826 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
828 LASSERT(credit == 0 || credit == 1);
829 LASSERT(conn->ibc_outstanding_credits >= 0);
830 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
831 LASSERT(conn->ibc_credits >= 0);
832 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
834 if (conn->ibc_nsends_posted ==
835 kiblnd_concurrent_sends(ver, ni)) {
836 /* tx completions outstanding... */
837 CDEBUG(D_NET, "%s: posted enough\n",
838 libcfs_nid2str(peer_ni->ibp_nid));
842 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
843 CDEBUG(D_NET, "%s: no credits\n",
844 libcfs_nid2str(peer_ni->ibp_nid));
848 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
849 conn->ibc_credits == 1 && /* last credit reserved */
850 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
851 CDEBUG(D_NET, "%s: not using last credit\n",
852 libcfs_nid2str(peer_ni->ibp_nid));
856 /* NB don't drop ibc_lock before bumping tx_sending */
857 list_del(&tx->tx_list);
860 if (msg->ibm_type == IBLND_MSG_NOOP &&
861 (!kiblnd_need_noop(conn) || /* redundant NOOP */
862 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
863 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
864 /* OK to drop when posted enough NOOPs, since
865 * kiblnd_check_sends_locked will queue NOOP again when
866 * posted NOOPs complete */
867 spin_unlock(&conn->ibc_lock);
868 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
870 spin_lock(&conn->ibc_lock);
871 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
872 libcfs_nid2str(peer_ni->ibp_nid),
873 conn->ibc_noops_posted);
877 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
878 peer_ni->ibp_nid, conn->ibc_incarnation);
880 conn->ibc_credits -= credit;
881 conn->ibc_outstanding_credits = 0;
882 conn->ibc_nsends_posted++;
883 if (msg->ibm_type == IBLND_MSG_NOOP)
884 conn->ibc_noops_posted++;
886 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
887 * PUT. If so, it was first queued here as a PUT_REQ, sent and
888 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
889 * and then re-queued here. It's (just) possible that
890 * tx_sending is non-zero if we've not done the tx_complete()
891 * from the first send; hence the ++ rather than = below. */
893 list_add(&tx->tx_list, &conn->ibc_active_txs);
895 /* I'm still holding ibc_lock! */
896 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
898 } else if (tx->tx_pool->tpo_pool.po_failed ||
899 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
900 /* close_conn will launch failover */
903 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
904 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
906 if (frd != NULL && !frd->frd_posted) {
907 if (!frd->frd_valid) {
908 wr = &frd->frd_inv_wr.wr;
909 wr->next = &frd->frd_fastreg_wr.wr;
911 wr = &frd->frd_fastreg_wr.wr;
913 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
916 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
917 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
918 bad->wr_id, bad->opcode, bad->send_flags,
919 libcfs_nid2str(conn->ibc_peer->ibp_nid));
922 if (lnet_send_error_simulation(tx->tx_lntmsg[0], &tx->tx_hstatus))
925 #ifdef HAVE_IB_POST_SEND_RECV_CONST
926 rc = ib_post_send(conn->ibc_cmid->qp, wr,
927 (const struct ib_send_wr **)&bad);
929 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
933 conn->ibc_last_send = ktime_get();
937 frd->frd_posted = true;
941 /* NB credits are transferred in the actual
942 * message, which can only be the last work item */
943 conn->ibc_credits += credit;
944 conn->ibc_outstanding_credits += msg->ibm_credits;
945 conn->ibc_nsends_posted--;
946 if (msg->ibm_type == IBLND_MSG_NOOP)
947 conn->ibc_noops_posted--;
953 done = (tx->tx_sending == 0);
955 list_del(&tx->tx_list);
957 spin_unlock(&conn->ibc_lock);
959 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
960 CERROR("Error %d posting transmit to %s\n",
961 rc, libcfs_nid2str(peer_ni->ibp_nid));
963 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
964 rc, libcfs_nid2str(peer_ni->ibp_nid));
966 kiblnd_close_conn(conn, rc);
971 spin_lock(&conn->ibc_lock);
977 kiblnd_check_sends_locked(struct kib_conn *conn)
979 int ver = conn->ibc_version;
980 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
983 /* Don't send anything until after the connection is established */
984 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
985 CDEBUG(D_NET, "%s too soon\n",
986 libcfs_nid2str(conn->ibc_peer->ibp_nid));
990 LASSERT(conn->ibc_nsends_posted <=
991 kiblnd_concurrent_sends(ver, ni));
992 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
993 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
994 LASSERT (conn->ibc_reserved_credits >= 0);
996 while (conn->ibc_reserved_credits > 0 &&
997 (tx = list_first_entry_or_null(&conn->ibc_tx_queue_rsrvd,
998 struct kib_tx, tx_list)) != NULL) {
999 list_move_tail(&tx->tx_list, &conn->ibc_tx_queue);
1000 conn->ibc_reserved_credits--;
1003 if (kiblnd_need_noop(conn)) {
1004 spin_unlock(&conn->ibc_lock);
1006 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1008 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
1010 spin_lock(&conn->ibc_lock);
1012 kiblnd_queue_tx_locked(tx, conn);
1018 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
1020 tx = list_first_entry(&conn->ibc_tx_queue_nocred,
1021 struct kib_tx, tx_list);
1022 } else if (!list_empty(&conn->ibc_tx_noops)) {
1023 LASSERT (!IBLND_OOB_CAPABLE(ver));
1025 tx = list_first_entry(&conn->ibc_tx_noops,
1026 struct kib_tx, tx_list);
1027 } else if (!list_empty(&conn->ibc_tx_queue)) {
1029 tx = list_first_entry(&conn->ibc_tx_queue,
1030 struct kib_tx, tx_list);
1034 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1040 kiblnd_tx_complete(struct kib_tx *tx, int status)
1042 int failed = (status != IB_WC_SUCCESS);
1043 struct kib_conn *conn = tx->tx_conn;
1046 if (tx->tx_sending <= 0) {
1047 CERROR("Received an event on a freed tx: %p status %d\n",
1053 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1054 CNETERR("Tx -> %s cookie %#llx"
1055 " sending %d waiting %d: failed %d\n",
1056 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1057 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1060 kiblnd_close_conn(conn, -EIO);
1062 kiblnd_peer_alive(conn->ibc_peer);
1065 spin_lock(&conn->ibc_lock);
1067 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1068 * gets to free it, which also drops its ref on 'conn'. */
1071 conn->ibc_nsends_posted--;
1072 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1073 conn->ibc_noops_posted--;
1076 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
1077 tx->tx_waiting = 0; /* don't wait for peer_ni */
1078 tx->tx_status = -EIO;
1081 idle = (tx->tx_sending == 0) && /* This is the final callback */
1082 !tx->tx_waiting && /* Not waiting for peer_ni */
1083 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1085 list_del(&tx->tx_list);
1087 kiblnd_check_sends_locked(conn);
1088 spin_unlock(&conn->ibc_lock);
1096 kiblnd_init_tx_sge(struct kib_tx *tx, u64 addr, unsigned int len)
1098 struct ib_sge *sge = &tx->tx_sge[tx->tx_nsge];
1099 struct kib_hca_dev *hdev = tx->tx_pool->tpo_hdev;
1100 #ifdef HAVE_IB_GET_DMA_MR
1101 struct ib_mr *mr = hdev->ibh_mrs;
1104 *sge = (struct ib_sge) {
1105 #ifdef HAVE_IB_GET_DMA_MR
1108 .lkey = hdev->ibh_pd->local_dma_lkey,
1118 kiblnd_init_tx_msg(struct lnet_ni *ni, struct kib_tx *tx, int type,
1121 struct ib_rdma_wr *wrq;
1122 int nob = offsetof(struct kib_msg, ibm_u) + body_nob;
1124 LASSERT(tx->tx_nwrq >= 0);
1125 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1126 LASSERT(nob <= IBLND_MSG_SIZE);
1128 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1130 wrq = &tx->tx_wrq[tx->tx_nwrq];
1132 *wrq = (struct ib_rdma_wr) {
1134 .wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
1136 .sg_list = &tx->tx_sge[tx->tx_nsge],
1137 .opcode = IB_WR_SEND,
1138 .send_flags = IB_SEND_SIGNALED,
1142 kiblnd_init_tx_sge(tx, tx->tx_msgaddr, nob);
1148 kiblnd_init_rdma(struct kib_conn *conn, struct kib_tx *tx, int type,
1149 int resid, struct kib_rdma_desc *dstrd, u64 dstcookie)
1151 struct kib_msg *ibmsg = tx->tx_msg;
1152 struct kib_rdma_desc *srcrd = tx->tx_rd;
1153 struct ib_rdma_wr *wrq = NULL;
1161 LASSERT(!in_interrupt());
1162 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1163 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1165 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1166 resid > 0; resid -= sge_nob) {
1169 if (srcidx >= srcrd->rd_nfrags) {
1170 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1175 if (dstidx >= dstrd->rd_nfrags) {
1176 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1181 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1182 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1183 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1184 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1185 conn->ibc_max_frags,
1186 srcidx, srcrd->rd_nfrags,
1187 dstidx, dstrd->rd_nfrags);
1192 sge_nob = min3(kiblnd_rd_frag_size(srcrd, srcidx),
1193 kiblnd_rd_frag_size(dstrd, dstidx),
1196 sge = &tx->tx_sge[tx->tx_nsge];
1197 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1198 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1199 sge->length = sge_nob;
1202 wrq = &tx->tx_wrq[tx->tx_nwrq];
1204 wrq->wr.next = &(wrq + 1)->wr;
1205 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1206 wrq->wr.sg_list = sge;
1207 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1208 wrq->wr.send_flags = 0;
1210 #ifdef HAVE_IB_RDMA_WR
1211 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1213 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1216 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1218 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1223 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1224 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1227 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1229 wrq->wr.num_sge = wrq_sge;
1235 if (rc < 0) /* no RDMA if completing with failure */
1236 tx->tx_nwrq = tx->tx_nsge = 0;
1238 ibmsg->ibm_u.completion.ibcm_status = rc;
1239 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1240 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1241 type, sizeof(struct kib_completion_msg));
1247 kiblnd_queue_tx_locked(struct kib_tx *tx, struct kib_conn *conn)
1249 struct list_head *q;
1252 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1253 LASSERT(!tx->tx_queued); /* not queued for sending already */
1254 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1256 if (conn->ibc_state >= IBLND_CONN_DISCONNECTED) {
1257 tx->tx_status = -ECONNABORTED;
1259 if (tx->tx_conn != NULL) {
1260 /* PUT_DONE first attached to conn as a PUT_REQ */
1261 LASSERT(tx->tx_conn == conn);
1262 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1264 kiblnd_conn_decref(conn);
1266 list_add(&tx->tx_list, &conn->ibc_zombie_txs);
1271 timeout_ns = kiblnd_timeout() * NSEC_PER_SEC;
1273 tx->tx_deadline = ktime_add_ns(ktime_get(), timeout_ns);
1275 if (tx->tx_conn == NULL) {
1276 kiblnd_conn_addref(conn);
1278 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1280 /* PUT_DONE first attached to conn as a PUT_REQ */
1281 LASSERT (tx->tx_conn == conn);
1282 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1285 switch (tx->tx_msg->ibm_type) {
1289 case IBLND_MSG_PUT_REQ:
1290 case IBLND_MSG_GET_REQ:
1291 q = &conn->ibc_tx_queue_rsrvd;
1294 case IBLND_MSG_PUT_NAK:
1295 case IBLND_MSG_PUT_ACK:
1296 case IBLND_MSG_PUT_DONE:
1297 case IBLND_MSG_GET_DONE:
1298 q = &conn->ibc_tx_queue_nocred;
1301 case IBLND_MSG_NOOP:
1302 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1303 q = &conn->ibc_tx_queue_nocred;
1305 q = &conn->ibc_tx_noops;
1308 case IBLND_MSG_IMMEDIATE:
1309 q = &conn->ibc_tx_queue;
1313 list_add_tail(&tx->tx_list, q);
1317 kiblnd_queue_tx(struct kib_tx *tx, struct kib_conn *conn)
1319 spin_lock(&conn->ibc_lock);
1320 kiblnd_queue_tx_locked(tx, conn);
1321 kiblnd_check_sends_locked(conn);
1322 spin_unlock(&conn->ibc_lock);
1326 kiblnd_resolve_addr_cap(struct rdma_cm_id *cmid,
1327 struct sockaddr_in *srcaddr,
1328 struct sockaddr_in *dstaddr,
1331 unsigned short port;
1334 /* allow the port to be reused */
1335 rc = rdma_set_reuseaddr(cmid, 1);
1337 CERROR("Unable to set reuse on cmid: %d\n", rc);
1341 /* look for a free privileged port */
1342 for (port = PROT_SOCK-1; port > 0; port--) {
1343 srcaddr->sin_port = htons(port);
1344 rc = rdma_resolve_addr(cmid,
1345 (struct sockaddr *)srcaddr,
1346 (struct sockaddr *)dstaddr,
1349 CDEBUG(D_NET, "bound to port %hu\n", port);
1351 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1352 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1359 CERROR("cannot bind to a free privileged port: rc = %d\n", rc);
1365 kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1366 struct sockaddr_in *srcaddr,
1367 struct sockaddr_in *dstaddr,
1370 const struct cred *old_creds = NULL;
1371 struct cred *new_creds;
1374 if (!capable(CAP_NET_BIND_SERVICE)) {
1375 new_creds = prepare_kernel_cred(NULL);
1379 cap_raise(new_creds->cap_effective, CAP_NET_BIND_SERVICE);
1380 old_creds = override_creds(new_creds);
1383 rc = kiblnd_resolve_addr_cap(cmid, srcaddr, dstaddr, timeout_ms);
1386 revert_creds(old_creds);
1392 kiblnd_connect_peer(struct kib_peer_ni *peer_ni)
1394 struct rdma_cm_id *cmid;
1395 struct kib_dev *dev;
1396 struct kib_net *net = peer_ni->ibp_ni->ni_data;
1397 struct sockaddr_in srcaddr;
1398 struct sockaddr_in dstaddr;
1401 LASSERT (net != NULL);
1402 LASSERT (peer_ni->ibp_connecting > 0);
1404 cmid = kiblnd_rdma_create_id(peer_ni->ibp_ni->ni_net_ns,
1405 kiblnd_cm_callback, peer_ni,
1406 RDMA_PS_TCP, IB_QPT_RC);
1409 CERROR("Can't create CMID for %s: %ld\n",
1410 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1416 memset(&srcaddr, 0, sizeof(srcaddr));
1417 srcaddr.sin_family = AF_INET;
1418 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1420 memset(&dstaddr, 0, sizeof(dstaddr));
1421 dstaddr.sin_family = AF_INET;
1422 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1423 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1425 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1427 if (*kiblnd_tunables.kib_use_priv_port) {
1428 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1429 kiblnd_timeout() * 1000);
1431 rc = rdma_resolve_addr(cmid,
1432 (struct sockaddr *)&srcaddr,
1433 (struct sockaddr *)&dstaddr,
1434 kiblnd_timeout() * 1000);
1437 /* Can't initiate address resolution: */
1438 CERROR("Can't resolve addr for %s: %d\n",
1439 libcfs_nid2str(peer_ni->ibp_nid), rc);
1446 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1447 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1448 rdma_destroy_id(cmid);
1451 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1455 kiblnd_reconnect_peer(struct kib_peer_ni *peer_ni)
1457 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1458 char *reason = NULL;
1460 unsigned long flags;
1462 write_lock_irqsave(glock, flags);
1463 if (peer_ni->ibp_reconnecting == 0) {
1464 if (peer_ni->ibp_accepting)
1465 reason = "accepting";
1466 else if (peer_ni->ibp_connecting)
1467 reason = "connecting";
1468 else if (!list_empty(&peer_ni->ibp_conns))
1469 reason = "connected";
1470 else /* connected then closed */
1476 if (peer_ni->ibp_accepting)
1477 CNETERR("Detecting race between accepting and reconnecting\n");
1478 peer_ni->ibp_reconnecting--;
1480 if (!kiblnd_peer_active(peer_ni)) {
1481 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1482 reason = "unlinked";
1486 peer_ni->ibp_connecting++;
1487 peer_ni->ibp_reconnected++;
1489 write_unlock_irqrestore(glock, flags);
1491 kiblnd_connect_peer(peer_ni);
1495 write_unlock_irqrestore(glock, flags);
1497 CWARN("Abort reconnection of %s: %s\n",
1498 libcfs_nid2str(peer_ni->ibp_nid), reason);
1499 kiblnd_txlist_done(&txs, -ECONNABORTED,
1500 LNET_MSG_STATUS_LOCAL_ABORTED);
1505 kiblnd_launch_tx(struct lnet_ni *ni, struct kib_tx *tx, lnet_nid_t nid)
1507 struct kib_peer_ni *peer_ni;
1508 struct kib_peer_ni *peer2;
1509 struct kib_conn *conn;
1510 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1511 unsigned long flags;
1514 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1516 /* If I get here, I've committed to send, so I complete the tx with
1517 * failure on any problems
1520 LASSERT(!tx || !tx->tx_conn); /* only set when assigned a conn */
1521 LASSERT(!tx || tx->tx_nwrq > 0); /* work items have been set up */
1523 /* First time, just use a read lock since I expect to find my peer_ni
1526 read_lock_irqsave(g_lock, flags);
1528 peer_ni = kiblnd_find_peer_locked(ni, nid);
1529 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1530 /* Found a peer_ni with an established connection */
1531 conn = kiblnd_get_conn_locked(peer_ni);
1532 kiblnd_conn_addref(conn); /* 1 ref for me... */
1534 read_unlock_irqrestore(g_lock, flags);
1537 kiblnd_queue_tx(tx, conn);
1538 kiblnd_conn_decref(conn); /* ...to here */
1542 read_unlock(g_lock);
1543 /* Re-try with a write lock */
1546 peer_ni = kiblnd_find_peer_locked(ni, nid);
1547 if (peer_ni != NULL) {
1548 if (list_empty(&peer_ni->ibp_conns)) {
1549 /* found a peer_ni, but it's still connecting... */
1550 LASSERT(kiblnd_peer_connecting(peer_ni));
1552 list_add_tail(&tx->tx_list,
1553 &peer_ni->ibp_tx_queue);
1554 write_unlock_irqrestore(g_lock, flags);
1556 conn = kiblnd_get_conn_locked(peer_ni);
1557 kiblnd_conn_addref(conn); /* 1 ref for me... */
1559 write_unlock_irqrestore(g_lock, flags);
1562 kiblnd_queue_tx(tx, conn);
1563 kiblnd_conn_decref(conn); /* ...to here */
1568 write_unlock_irqrestore(g_lock, flags);
1570 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1571 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1573 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1575 tx->tx_status = -EHOSTUNREACH;
1577 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1583 write_lock_irqsave(g_lock, flags);
1585 peer2 = kiblnd_find_peer_locked(ni, nid);
1586 if (peer2 != NULL) {
1587 if (list_empty(&peer2->ibp_conns)) {
1588 /* found a peer_ni, but it's still connecting... */
1589 LASSERT(kiblnd_peer_connecting(peer2));
1591 list_add_tail(&tx->tx_list,
1592 &peer2->ibp_tx_queue);
1593 write_unlock_irqrestore(g_lock, flags);
1595 conn = kiblnd_get_conn_locked(peer2);
1596 kiblnd_conn_addref(conn); /* 1 ref for me... */
1598 write_unlock_irqrestore(g_lock, flags);
1601 kiblnd_queue_tx(tx, conn);
1602 kiblnd_conn_decref(conn); /* ...to here */
1605 kiblnd_peer_decref(peer_ni);
1609 /* Brand new peer_ni */
1610 LASSERT(peer_ni->ibp_connecting == 0);
1611 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1612 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1614 /* always called with a ref on ni, which prevents ni being shutdown */
1615 LASSERT(((struct kib_net *)ni->ni_data)->ibn_shutdown == 0);
1618 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1620 kiblnd_peer_addref(peer_ni);
1621 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
1623 write_unlock_irqrestore(g_lock, flags);
1625 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1626 kiblnd_connect_peer(peer_ni);
1627 kiblnd_peer_decref(peer_ni);
1631 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1633 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1634 int type = lntmsg->msg_type;
1635 struct lnet_processid *target = &lntmsg->msg_target;
1636 int target_is_router = lntmsg->msg_target_is_router;
1637 int routing = lntmsg->msg_routing;
1638 unsigned int payload_niov = lntmsg->msg_niov;
1639 struct bio_vec *payload_kiov = lntmsg->msg_kiov;
1640 unsigned int payload_offset = lntmsg->msg_offset;
1641 unsigned int payload_nob = lntmsg->msg_len;
1642 struct kib_msg *ibmsg;
1643 struct kib_rdma_desc *rd;
1648 /* NB 'private' is different depending on what we're sending.... */
1650 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1651 payload_nob, payload_niov, libcfs_idstr(target));
1653 LASSERT (payload_nob == 0 || payload_niov > 0);
1654 LASSERT (payload_niov <= LNET_MAX_IOV);
1656 /* Thread context */
1657 LASSERT (!in_interrupt());
1659 tx = kiblnd_get_idle_tx(ni, lnet_nid_to_nid4(&target->nid));
1661 CERROR("Can't allocate %s txd for %s\n",
1662 lnet_msgtyp2str(type),
1663 libcfs_nidstr(&target->nid));
1674 LASSERT (payload_nob == 0);
1678 if (routing || target_is_router)
1679 break; /* send IMMEDIATE */
1681 /* is the REPLY message too small for RDMA? */
1682 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1683 if (nob <= IBLND_MSG_SIZE && !lntmsg->msg_rdma_force)
1684 break; /* send IMMEDIATE */
1686 rd = &ibmsg->ibm_u.get.ibgm_rd;
1687 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1688 lntmsg->msg_md->md_niov,
1689 lntmsg->msg_md->md_kiov,
1690 0, lntmsg->msg_md->md_length);
1692 CERROR("Can't setup GET sink for %s: %d\n",
1693 libcfs_nidstr(&target->nid), rc);
1694 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1699 nob = offsetof(struct kib_get_msg, ibgm_rd.rd_frags[rd->rd_nfrags]);
1700 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1701 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.get.ibgm_hdr);
1703 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1705 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1706 if (tx->tx_lntmsg[1] == NULL) {
1707 CERROR("Can't create reply for GET -> %s\n",
1708 libcfs_nidstr(&target->nid));
1713 /* finalise lntmsg[0,1] on completion */
1714 tx->tx_lntmsg[0] = lntmsg;
1715 tx->tx_waiting = 1; /* waiting for GET_DONE */
1716 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1719 case LNET_MSG_REPLY:
1721 /* Is the payload small enough not to need RDMA? */
1722 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob]);
1723 if (nob <= IBLND_MSG_SIZE && !lntmsg->msg_rdma_force)
1724 break; /* send IMMEDIATE */
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_nidstr(&target->nid), rc);
1736 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.putreq.ibprm_hdr);
1737 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1738 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ,
1739 sizeof(struct kib_putreq_msg));
1741 /* finalise lntmsg[0,1] on completion */
1742 tx->tx_lntmsg[0] = lntmsg;
1743 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1744 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1748 /* send IMMEDIATE */
1749 LASSERT(offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[payload_nob])
1753 lnet_hdr_to_nid4(hdr, &ibmsg->ibm_u.immediate.ibim_hdr);
1755 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1756 offsetof(struct kib_msg,
1757 ibm_u.immediate.ibim_payload),
1758 payload_niov, payload_kiov,
1759 payload_offset, payload_nob);
1761 nob = offsetof(struct kib_immediate_msg, ibim_payload[payload_nob]);
1763 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1765 /* finalise lntmsg on completion */
1766 tx->tx_lntmsg[0] = lntmsg;
1768 kiblnd_launch_tx(ni, tx, lnet_nid_to_nid4(&target->nid));
1773 kiblnd_reply(struct lnet_ni *ni, struct kib_rx *rx, struct lnet_msg *lntmsg)
1775 struct lnet_processid *target = &lntmsg->msg_target;
1776 unsigned int niov = lntmsg->msg_niov;
1777 struct bio_vec *kiov = lntmsg->msg_kiov;
1778 unsigned int offset = lntmsg->msg_offset;
1779 unsigned int nob = lntmsg->msg_len;
1783 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1785 CERROR("Can't get tx for REPLY to %s\n",
1786 libcfs_nidstr(&target->nid));
1793 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1794 niov, kiov, offset, nob);
1797 CERROR("Can't setup GET src for %s: %d\n",
1798 libcfs_nidstr(&target->nid), rc);
1802 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1803 IBLND_MSG_GET_DONE, nob,
1804 &rx->rx_msg->ibm_u.get.ibgm_rd,
1805 rx->rx_msg->ibm_u.get.ibgm_cookie);
1807 CERROR("Can't setup rdma for GET from %s: %d\n",
1808 libcfs_nidstr(&target->nid), rc);
1813 /* No RDMA: local completion may happen now! */
1814 lnet_finalize(lntmsg, 0);
1816 /* RDMA: lnet_finalize(lntmsg) when it
1818 tx->tx_lntmsg[0] = lntmsg;
1821 kiblnd_queue_tx(tx, rx->rx_conn);
1826 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1829 lnet_finalize(lntmsg, -EIO);
1833 kiblnd_get_dev_prio(struct lnet_ni *ni, unsigned int dev_idx)
1835 struct kib_net *net = ni->ni_data;
1836 struct device *dev = NULL;
1839 dev = net->ibn_dev->ibd_hdev->ibh_ibdev->dma_device;
1841 return lnet_get_dev_prio(dev, dev_idx);
1846 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1847 int delayed, unsigned int niov, struct bio_vec *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());
1862 switch (rxmsg->ibm_type) {
1866 case IBLND_MSG_IMMEDIATE:
1867 nob = offsetof(struct kib_msg, ibm_u.immediate.ibim_payload[rlen]);
1868 if (nob > rx->rx_nob) {
1869 CERROR("Immediate message from %s too big: %d(%d)\n",
1870 libcfs_nidstr(&lntmsg->msg_hdr.src_nid),
1876 lnet_copy_flat2kiov(niov, kiov, offset,
1877 IBLND_MSG_SIZE, rxmsg,
1878 offsetof(struct kib_msg,
1879 ibm_u.immediate.ibim_payload),
1881 lnet_finalize(lntmsg, 0);
1884 case IBLND_MSG_PUT_REQ: {
1885 struct kib_msg *txmsg;
1886 struct kib_rdma_desc *rd;
1887 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1890 lnet_finalize(lntmsg, 0);
1891 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1896 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1898 CERROR("Can't allocate tx for %s\n",
1899 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1900 /* Not replying will break the connection */
1906 rd = &txmsg->ibm_u.putack.ibpam_rd;
1907 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1908 niov, kiov, offset, mlen);
1910 CERROR("Can't setup PUT sink for %s: %d\n",
1911 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1912 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
1914 /* tell peer_ni it's over */
1915 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1920 nob = offsetof(struct kib_putack_msg, ibpam_rd.rd_frags[rd->rd_nfrags]);
1921 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1922 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1924 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1926 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1927 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1928 kiblnd_queue_tx(tx, conn);
1930 /* reposted buffer reserved for PUT_DONE */
1931 post_credit = IBLND_POSTRX_NO_CREDIT;
1935 case IBLND_MSG_GET_REQ:
1936 if (lntmsg != NULL) {
1937 /* Optimized GET; RDMA lntmsg's payload */
1938 kiblnd_reply(ni, rx, lntmsg);
1940 /* GET didn't match anything */
1941 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1943 rxmsg->ibm_u.get.ibgm_cookie);
1948 kiblnd_post_rx(rx, post_credit);
1953 kiblnd_thread_fini (void)
1955 atomic_dec (&kiblnd_data.kib_nthreads);
1959 kiblnd_peer_alive(struct kib_peer_ni *peer_ni)
1961 /* This is racy, but everyone's only writing ktime_get_seconds() */
1962 peer_ni->ibp_last_alive = ktime_get_seconds();
1967 kiblnd_peer_notify(struct kib_peer_ni *peer_ni)
1970 time64_t last_alive = 0;
1971 unsigned long flags;
1973 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1975 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
1976 error = peer_ni->ibp_error;
1977 peer_ni->ibp_error = 0;
1979 last_alive = peer_ni->ibp_last_alive;
1982 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1985 lnet_notify(peer_ni->ibp_ni,
1986 peer_ni->ibp_nid, false, false, last_alive);
1990 kiblnd_close_conn_locked(struct kib_conn *conn, int error)
1992 /* This just does the immediate housekeeping. 'error' is zero for a
1993 * normal shutdown which can happen only after the connection has been
1994 * established. If the connection is established, schedule the
1995 * connection to be finished off by the connd. Otherwise the connd is
1996 * already dealing with it (either to set it up or tear it down).
1997 * Caller holds kib_global_lock exclusively in irq context */
1998 struct kib_peer_ni *peer_ni = conn->ibc_peer;
1999 struct kib_dev *dev;
2000 unsigned long flags;
2002 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2004 if (error != 0 && conn->ibc_comms_error == 0)
2005 conn->ibc_comms_error = error;
2007 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
2008 return; /* already being handled */
2011 list_empty(&conn->ibc_tx_noops) &&
2012 list_empty(&conn->ibc_tx_queue) &&
2013 list_empty(&conn->ibc_tx_queue_rsrvd) &&
2014 list_empty(&conn->ibc_tx_queue_nocred) &&
2015 list_empty(&conn->ibc_active_txs)) {
2016 CDEBUG(D_NET, "closing conn to %s\n",
2017 libcfs_nid2str(peer_ni->ibp_nid));
2019 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
2020 libcfs_nid2str(peer_ni->ibp_nid), error,
2021 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
2022 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
2023 list_empty(&conn->ibc_tx_queue_rsrvd) ?
2024 "" : "(sending_rsrvd)",
2025 list_empty(&conn->ibc_tx_queue_nocred) ?
2026 "" : "(sending_nocred)",
2027 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
2030 dev = ((struct kib_net *)peer_ni->ibp_ni->ni_data)->ibn_dev;
2031 if (peer_ni->ibp_next_conn == conn)
2032 /* clear next_conn so it won't be used */
2033 peer_ni->ibp_next_conn = NULL;
2034 list_del(&conn->ibc_list);
2035 /* connd (see below) takes over ibc_list's ref */
2037 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
2038 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
2039 kiblnd_unlink_peer_locked(peer_ni);
2041 /* set/clear error on last conn */
2042 peer_ni->ibp_error = conn->ibc_comms_error;
2045 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
2048 kiblnd_dev_can_failover(dev)) {
2049 list_add_tail(&dev->ibd_fail_list,
2050 &kiblnd_data.kib_failed_devs);
2051 wake_up(&kiblnd_data.kib_failover_waitq);
2054 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2056 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
2057 wake_up(&kiblnd_data.kib_connd_waitq);
2059 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2063 kiblnd_close_conn(struct kib_conn *conn, int error)
2065 unsigned long flags;
2067 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2069 kiblnd_close_conn_locked(conn, error);
2071 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2075 kiblnd_handle_early_rxs(struct kib_conn *conn)
2077 unsigned long flags;
2080 LASSERT(!in_interrupt());
2081 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
2083 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2084 while ((rx = list_first_entry_or_null(&conn->ibc_early_rxs,
2086 rx_list)) != NULL) {
2087 list_del(&rx->rx_list);
2088 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2090 kiblnd_handle_rx(rx);
2092 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2094 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2098 kiblnd_abort_txs(struct kib_conn *conn, struct list_head *txs)
2104 spin_lock(&conn->ibc_lock);
2106 list_for_each_entry_safe(tx, nxt, txs, tx_list) {
2107 if (txs == &conn->ibc_active_txs) {
2108 LASSERT(!tx->tx_queued);
2109 LASSERT(tx->tx_waiting ||
2110 tx->tx_sending != 0);
2111 if (conn->ibc_comms_error == -ETIMEDOUT) {
2112 if (tx->tx_waiting && !tx->tx_sending)
2114 LNET_MSG_STATUS_REMOTE_TIMEOUT;
2115 else if (tx->tx_sending)
2117 LNET_MSG_STATUS_NETWORK_TIMEOUT;
2120 LASSERT(tx->tx_queued);
2121 if (conn->ibc_comms_error == -ETIMEDOUT)
2122 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2124 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
2127 tx->tx_status = -ECONNABORTED;
2131 * TODO: This makes an assumption that
2132 * kiblnd_tx_complete() will be called for each tx. If
2133 * that event is dropped we could end up with stale
2134 * connections floating around. We'd like to deal with
2135 * that in a better way.
2137 * Also that means we can exceed the timeout by many
2140 if (tx->tx_sending == 0) {
2142 list_move(&tx->tx_list, &zombies);
2144 /* keep tx until cq destroy */
2145 list_move(&tx->tx_list, &conn->ibc_zombie_txs);
2150 spin_unlock(&conn->ibc_lock);
2153 * aborting transmits occurs when finalizing the connection.
2154 * The connection is finalized on error.
2155 * Passing LNET_MSG_STATUS_OK to txlist_done() will not
2156 * override the value already set in tx->tx_hstatus above.
2158 kiblnd_txlist_done(&zombies, -ECONNABORTED, LNET_MSG_STATUS_OK);
2162 kiblnd_tx_may_discard(struct kib_conn *conn)
2168 spin_lock(&conn->ibc_lock);
2170 list_for_each_entry_safe(tx, nxt, &conn->ibc_zombie_txs, tx_list) {
2171 if (tx->tx_sending > 0 && tx->tx_lntmsg[0] &&
2172 lnet_md_discarded(tx->tx_lntmsg[0]->msg_md)) {
2174 if (tx->tx_sending == 0) {
2175 kiblnd_conn_decref(tx->tx_conn);
2182 spin_unlock(&conn->ibc_lock);
2187 kiblnd_finalise_conn(struct kib_conn *conn)
2189 LASSERT (!in_interrupt());
2190 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2192 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2193 * for connections that didn't get as far as being connected, because
2194 * rdma_disconnect() does this for free. */
2195 kiblnd_abort_receives(conn);
2197 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2199 /* Complete all tx descs not waiting for sends to complete.
2200 * NB we should be safe from RDMA now that the QP has changed state */
2202 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2203 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2204 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2205 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2206 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2208 kiblnd_handle_early_rxs(conn);
2212 kiblnd_peer_connect_failed(struct kib_peer_ni *peer_ni, int active,
2216 unsigned long flags;
2217 enum lnet_msg_hstatus hstatus;
2219 LASSERT(error != 0);
2220 LASSERT(!in_interrupt());
2222 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2225 LASSERT(peer_ni->ibp_connecting > 0);
2226 peer_ni->ibp_connecting--;
2228 LASSERT (peer_ni->ibp_accepting > 0);
2229 peer_ni->ibp_accepting--;
2232 if (kiblnd_peer_connecting(peer_ni)) {
2233 /* another connection attempt under way... */
2234 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2239 peer_ni->ibp_reconnected = 0;
2240 if (list_empty(&peer_ni->ibp_conns)) {
2241 /* Take peer_ni's blocked transmits to complete with error */
2242 list_splice_init(&peer_ni->ibp_tx_queue, &zombies);
2244 if (kiblnd_peer_active(peer_ni))
2245 kiblnd_unlink_peer_locked(peer_ni);
2247 peer_ni->ibp_error = error;
2249 /* Can't have blocked transmits if there are connections */
2250 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2253 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2255 kiblnd_peer_notify(peer_ni);
2257 if (list_empty(&zombies))
2260 CNETERR("Deleting messages for %s: connection failed\n",
2261 libcfs_nid2str(peer_ni->ibp_nid));
2266 hstatus = LNET_MSG_STATUS_NETWORK_TIMEOUT;
2269 hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
2272 hstatus = LNET_MSG_STATUS_LOCAL_DROPPED;
2276 kiblnd_txlist_done(&zombies, error, hstatus);
2280 kiblnd_connreq_done(struct kib_conn *conn, int status)
2282 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2285 unsigned long flags;
2288 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2290 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2291 libcfs_nid2str(peer_ni->ibp_nid), active,
2292 conn->ibc_version, status);
2294 LASSERT (!in_interrupt());
2295 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2296 peer_ni->ibp_connecting > 0) ||
2297 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2298 peer_ni->ibp_accepting > 0));
2300 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2301 conn->ibc_connvars = NULL;
2304 /* failed to establish connection */
2305 kiblnd_peer_connect_failed(peer_ni, active, status);
2306 kiblnd_finalise_conn(conn);
2310 /* connection established */
2311 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2313 conn->ibc_last_send = ktime_get();
2314 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2315 kiblnd_peer_alive(peer_ni);
2317 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2318 * peer_ni instance... */
2319 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2320 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2321 peer_ni->ibp_reconnected = 0;
2323 peer_ni->ibp_connecting--;
2325 peer_ni->ibp_accepting--;
2327 if (peer_ni->ibp_version == 0) {
2328 peer_ni->ibp_version = conn->ibc_version;
2329 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2332 if (peer_ni->ibp_version != conn->ibc_version ||
2333 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2334 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2335 conn->ibc_incarnation);
2336 peer_ni->ibp_version = conn->ibc_version;
2337 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2340 /* grab pending txs while I have the lock */
2341 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
2343 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2344 conn->ibc_comms_error != 0) { /* error has happened already */
2346 /* start to shut down connection */
2347 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2348 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2350 kiblnd_txlist_done(&txs, -ECONNABORTED,
2351 LNET_MSG_STATUS_LOCAL_ERROR);
2356 /* +1 ref for myself, this connection is visible to other threads
2357 * now, refcount of peer:ibp_conns can be released by connection
2358 * close from either a different thread, or the calling of
2359 * kiblnd_check_sends_locked() below. See bz21911 for details.
2361 kiblnd_conn_addref(conn);
2362 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2364 /* Schedule blocked txs
2365 * Note: if we are running with conns_per_peer > 1, these blocked
2366 * txs will all get scheduled to the first connection which gets
2367 * scheduled. We won't be using round robin on this first batch.
2369 spin_lock(&conn->ibc_lock);
2370 while ((tx = list_first_entry_or_null(&txs, struct kib_tx,
2371 tx_list)) != NULL) {
2372 list_del(&tx->tx_list);
2374 kiblnd_queue_tx_locked(tx, conn);
2376 kiblnd_check_sends_locked(conn);
2377 spin_unlock(&conn->ibc_lock);
2379 /* schedule blocked rxs */
2380 kiblnd_handle_early_rxs(conn);
2381 kiblnd_conn_decref(conn);
2385 kiblnd_reject(struct rdma_cm_id *cmid, struct kib_rej *rej)
2389 #ifdef HAVE_RDMA_REJECT_4ARGS
2390 rc = rdma_reject(cmid, rej, sizeof(*rej), IB_CM_REJ_CONSUMER_DEFINED);
2392 rc = rdma_reject(cmid, rej, sizeof(*rej));
2396 CWARN("Error %d sending reject\n", rc);
2400 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2402 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2403 struct kib_msg *reqmsg = priv;
2404 struct kib_msg *ackmsg;
2405 struct kib_dev *ibdev;
2406 struct kib_peer_ni *peer_ni;
2407 struct kib_peer_ni *peer2;
2408 struct kib_conn *conn;
2409 struct lnet_ni *ni = NULL;
2410 struct kib_net *net = NULL;
2412 struct rdma_conn_param cp;
2414 int version = IBLND_MSG_VERSION;
2415 unsigned long flags;
2417 struct sockaddr_in *peer_addr;
2419 LASSERT(!in_interrupt());
2420 /* cmid inherits 'context' from the corresponding listener id */
2421 ibdev = cmid->context;
2424 memset(&rej, 0, sizeof(rej));
2425 rej.ibr_magic = IBLND_MSG_MAGIC;
2426 rej.ibr_why = IBLND_REJECT_FATAL;
2427 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2429 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2430 if (*kiblnd_tunables.kib_require_priv_port &&
2431 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2432 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2433 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2434 &ip, ntohs(peer_addr->sin_port));
2438 if (priv_nob < offsetof(struct kib_msg, ibm_type)) {
2439 CERROR("Short connection request\n");
2443 /* Future protocol version compatibility support! If the
2444 * o2iblnd-specific protocol changes, or when LNET unifies
2445 * protocols over all LNDs, the initial connection will
2446 * negotiate a protocol version. I trap this here to avoid
2447 * console errors; the reject tells the peer_ni which protocol I
2449 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2450 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2452 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2453 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2454 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2456 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2457 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2458 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2461 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2463 CERROR("Can't parse connection request: %d\n", rc);
2467 nid = reqmsg->ibm_srcnid;
2468 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2471 net = (struct kib_net *)ni->ni_data;
2472 rej.ibr_incarnation = net->ibn_incarnation;
2475 if (ni == NULL || /* no matching net */
2476 lnet_nid_to_nid4(&ni->ni_nid) !=
2477 reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2478 net->ibn_dev != ibdev) { /* wrong device */
2479 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): bad dst nid %s\n", libcfs_nid2str(nid),
2480 ni ? libcfs_nidstr(&ni->ni_nid) : "NA",
2481 ibdev->ibd_ifname, ibdev->ibd_nnets,
2483 libcfs_nid2str(reqmsg->ibm_dstnid));
2488 /* check time stamp as soon as possible */
2489 if (reqmsg->ibm_dststamp != 0 &&
2490 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2491 CWARN("Stale connection request\n");
2492 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2496 /* I can accept peer_ni's version */
2497 version = reqmsg->ibm_version;
2499 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2500 CERROR("Unexpected connreq msg type: %x from %s\n",
2501 reqmsg->ibm_type, libcfs_nid2str(nid));
2505 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2506 kiblnd_msg_queue_size(version, ni)) {
2507 CERROR("Can't accept conn from %s, queue depth too large: %d (<=%d wanted)\n",
2508 libcfs_nid2str(nid),
2509 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2510 kiblnd_msg_queue_size(version, ni));
2512 if (version == IBLND_MSG_VERSION)
2513 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2518 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2519 IBLND_MAX_RDMA_FRAGS) {
2520 CWARN("Can't accept conn from %s (version %x): max_frags %d too large (%d wanted)\n",
2521 libcfs_nid2str(nid), version,
2522 reqmsg->ibm_u.connparams.ibcp_max_frags,
2523 IBLND_MAX_RDMA_FRAGS);
2525 if (version >= IBLND_MSG_VERSION)
2526 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2529 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2530 IBLND_MAX_RDMA_FRAGS &&
2531 net->ibn_fmr_ps == NULL) {
2532 CWARN("Can't accept conn from %s (version %x): max_frags %d incompatible without FMR pool (%d wanted)\n",
2533 libcfs_nid2str(nid), version,
2534 reqmsg->ibm_u.connparams.ibcp_max_frags,
2535 IBLND_MAX_RDMA_FRAGS);
2537 if (version == IBLND_MSG_VERSION)
2538 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2543 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2544 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2545 libcfs_nid2str(nid),
2546 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2551 /* assume 'nid' is a new peer_ni; create */
2552 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2554 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2555 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2559 /* We have validated the peer's parameters so use those */
2560 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2561 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2563 write_lock_irqsave(g_lock, flags);
2565 peer2 = kiblnd_find_peer_locked(ni, nid);
2566 if (peer2 != NULL) {
2567 if (peer2->ibp_version == 0) {
2568 peer2->ibp_version = version;
2569 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2572 /* not the guy I've talked with */
2573 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2574 peer2->ibp_version != version) {
2575 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2577 if (kiblnd_peer_active(peer2)) {
2578 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2579 peer2->ibp_version = version;
2581 write_unlock_irqrestore(g_lock, flags);
2583 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2584 libcfs_nid2str(nid), peer2->ibp_version, version,
2585 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2587 kiblnd_peer_decref(peer_ni);
2588 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2592 /* Tie-break connection race in favour of the higher NID.
2593 * If we keep running into a race condition multiple times,
2594 * we have to assume that the connection attempt with the
2595 * higher NID is stuck in a connecting state and will never
2596 * recover. As such, we pass through this if-block and let
2597 * the lower NID connection win so we can move forward.
2599 if (peer2->ibp_connecting != 0 &&
2600 nid < lnet_nid_to_nid4(&ni->ni_nid) &&
2601 peer2->ibp_races < MAX_CONN_RACES_BEFORE_ABORT) {
2603 write_unlock_irqrestore(g_lock, flags);
2605 CDEBUG(D_NET, "Conn race %s\n",
2606 libcfs_nid2str(peer2->ibp_nid));
2608 kiblnd_peer_decref(peer_ni);
2609 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2612 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2613 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2614 libcfs_nid2str(peer2->ibp_nid),
2615 MAX_CONN_RACES_BEFORE_ABORT);
2617 * passive connection is allowed even this peer_ni is waiting for
2620 peer2->ibp_reconnecting = 0;
2621 peer2->ibp_races = 0;
2622 peer2->ibp_accepting++;
2623 kiblnd_peer_addref(peer2);
2625 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2626 * so copy validated parameters since we now know what the
2627 * peer_ni's limits are */
2628 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2629 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2631 write_unlock_irqrestore(g_lock, flags);
2632 kiblnd_peer_decref(peer_ni);
2635 /* Brand new peer_ni */
2636 LASSERT(peer_ni->ibp_accepting == 0);
2637 LASSERT(peer_ni->ibp_version == 0 &&
2638 peer_ni->ibp_incarnation == 0);
2640 peer_ni->ibp_accepting = 1;
2641 peer_ni->ibp_version = version;
2642 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2644 /* I have a ref on ni that prevents it being shutdown */
2645 LASSERT(net->ibn_shutdown == 0);
2647 kiblnd_peer_addref(peer_ni);
2648 hash_add(kiblnd_data.kib_peers, &peer_ni->ibp_list, nid);
2650 write_unlock_irqrestore(g_lock, flags);
2653 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT,
2656 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2657 kiblnd_peer_decref(peer_ni);
2658 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2662 /* conn now "owns" cmid, so I return success from here on to ensure the
2663 * CM callback doesn't destroy cmid.
2665 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2666 conn->ibc_credits = conn->ibc_queue_depth;
2667 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2668 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2669 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2671 ackmsg = &conn->ibc_connvars->cv_msg;
2672 memset(ackmsg, 0, sizeof(*ackmsg));
2674 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2675 sizeof(ackmsg->ibm_u.connparams));
2676 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2677 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2678 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2680 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2682 memset(&cp, 0, sizeof(cp));
2683 cp.private_data = ackmsg;
2684 cp.private_data_len = ackmsg->ibm_nob;
2685 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2686 cp.initiator_depth = 0;
2687 cp.flow_control = 1;
2688 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2689 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2691 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2693 rc = rdma_accept(cmid, &cp);
2695 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2696 rej.ibr_version = version;
2697 rej.ibr_why = IBLND_REJECT_FATAL;
2699 kiblnd_reject(cmid, &rej);
2700 kiblnd_connreq_done(conn, rc);
2701 kiblnd_conn_decref(conn);
2709 rej.ibr_cp.ibcp_queue_depth =
2710 kiblnd_msg_queue_size(version, ni);
2711 rej.ibr_cp.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2715 rej.ibr_version = version;
2716 kiblnd_reject(cmid, &rej);
2718 return -ECONNREFUSED;
2722 kiblnd_check_reconnect(struct kib_conn *conn, int version,
2723 u64 incarnation, int why, struct kib_connparams *cp)
2725 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2726 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2728 int msg_size = IBLND_MSG_SIZE;
2732 unsigned long flags;
2734 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2735 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2738 msg_size = cp->ibcp_max_msg_size;
2739 frag_num = cp->ibcp_max_frags;
2740 queue_dep = cp->ibcp_queue_depth;
2743 write_lock_irqsave(glock, flags);
2744 /* retry connection if it's still needed and no other connection
2745 * attempts (active or passive) are in progress
2746 * NB: reconnect is still needed even when ibp_tx_queue is
2747 * empty if ibp_version != version because reconnect may be
2750 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2751 peer_ni->ibp_version != version) &&
2752 peer_ni->ibp_connecting &&
2753 peer_ni->ibp_accepting == 0;
2764 case IBLND_REJECT_RDMA_FRAGS: {
2766 reason = "can't negotiate max frags";
2770 if (conn->ibc_max_frags <= frag_num) {
2771 reason = "unsupported max frags";
2775 peer_ni->ibp_max_frags = frag_num;
2776 reason = "rdma fragments";
2779 case IBLND_REJECT_MSG_QUEUE_SIZE:
2781 reason = "can't negotiate queue depth";
2784 if (conn->ibc_queue_depth <= queue_dep) {
2785 reason = "unsupported queue depth";
2789 peer_ni->ibp_queue_depth = queue_dep;
2790 reason = "queue depth";
2793 case IBLND_REJECT_CONN_STALE:
2797 case IBLND_REJECT_CONN_RACE:
2798 reason = "conn race";
2801 case IBLND_REJECT_CONN_UNCOMPAT:
2802 reason = "version negotiation";
2806 conn->ibc_reconnect = 1;
2807 peer_ni->ibp_reconnecting++;
2808 peer_ni->ibp_version = version;
2809 if (incarnation != 0)
2810 peer_ni->ibp_incarnation = incarnation;
2812 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2814 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2815 libcfs_nid2str(peer_ni->ibp_nid),
2816 reconnect ? "reconnect" : "don't reconnect",
2817 reason, IBLND_MSG_VERSION, version, msg_size,
2818 conn->ibc_queue_depth, queue_dep,
2819 conn->ibc_max_frags, frag_num);
2821 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2822 * while destroying the zombie
2827 kiblnd_rejected(struct kib_conn *conn, int reason, void *priv, int priv_nob)
2829 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2830 int status = -ECONNREFUSED;
2832 LASSERT (!in_interrupt());
2833 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2836 case IB_CM_REJ_STALE_CONN:
2837 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2838 IBLND_REJECT_CONN_STALE, NULL);
2841 case IB_CM_REJ_INVALID_SERVICE_ID:
2842 status = -EHOSTUNREACH;
2843 CNETERR("%s rejected: no listener at %d\n",
2844 libcfs_nid2str(peer_ni->ibp_nid),
2845 *kiblnd_tunables.kib_service);
2848 case IB_CM_REJ_CONSUMER_DEFINED:
2849 if (priv_nob >= offsetof(struct kib_rej, ibr_padding)) {
2850 struct kib_rej *rej = priv;
2851 struct kib_connparams *cp = NULL;
2853 __u64 incarnation = -1;
2855 /* NB. default incarnation is -1 because:
2856 * a) V1 will ignore dst incarnation in connreq.
2857 * b) V2 will provide incarnation while rejecting me,
2858 * -1 will be overwrote.
2860 * if I try to connect to a V1 peer_ni with V2 protocol,
2861 * it rejected me then upgrade to V2, I have no idea
2862 * about the upgrading and try to reconnect with V1,
2863 * in this case upgraded V2 can find out I'm trying to
2864 * talk to the old guy and reject me(incarnation is -1).
2867 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2868 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2869 __swab32s(&rej->ibr_magic);
2870 __swab16s(&rej->ibr_version);
2874 if (priv_nob >= sizeof(struct kib_rej) &&
2875 rej->ibr_version > IBLND_MSG_VERSION_1) {
2876 /* priv_nob is always 148 in current version
2877 * of OFED, so we still need to check version.
2878 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE)
2883 __swab64s(&rej->ibr_incarnation);
2884 __swab16s(&cp->ibcp_queue_depth);
2885 __swab16s(&cp->ibcp_max_frags);
2886 __swab32s(&cp->ibcp_max_msg_size);
2889 incarnation = rej->ibr_incarnation;
2892 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2893 rej->ibr_magic != LNET_PROTO_MAGIC) {
2894 CERROR("%s rejected: consumer defined fatal error\n",
2895 libcfs_nid2str(peer_ni->ibp_nid));
2899 if (rej->ibr_version != IBLND_MSG_VERSION &&
2900 rej->ibr_version != IBLND_MSG_VERSION_1) {
2901 CERROR("%s rejected: o2iblnd version %x error\n",
2902 libcfs_nid2str(peer_ni->ibp_nid),
2907 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2908 rej->ibr_version == IBLND_MSG_VERSION_1) {
2909 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2910 libcfs_nid2str(peer_ni->ibp_nid),
2913 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2914 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2917 switch (rej->ibr_why) {
2918 case IBLND_REJECT_CONN_RACE:
2919 case IBLND_REJECT_CONN_STALE:
2920 case IBLND_REJECT_CONN_UNCOMPAT:
2921 case IBLND_REJECT_MSG_QUEUE_SIZE:
2922 case IBLND_REJECT_RDMA_FRAGS:
2923 kiblnd_check_reconnect(conn, rej->ibr_version,
2928 case IBLND_REJECT_NO_RESOURCES:
2929 CERROR("%s rejected: o2iblnd no resources\n",
2930 libcfs_nid2str(peer_ni->ibp_nid));
2933 case IBLND_REJECT_FATAL:
2934 CERROR("%s rejected: o2iblnd fatal error\n",
2935 libcfs_nid2str(peer_ni->ibp_nid));
2939 CERROR("%s rejected: o2iblnd reason %d\n",
2940 libcfs_nid2str(peer_ni->ibp_nid),
2948 CNETERR("%s rejected: reason %d, size %d\n",
2949 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
2953 kiblnd_connreq_done(conn, status);
2957 kiblnd_check_connreply(struct kib_conn *conn, void *priv, int priv_nob)
2959 struct kib_peer_ni *peer_ni = conn->ibc_peer;
2960 struct lnet_ni *ni = peer_ni->ibp_ni;
2961 struct kib_net *net = ni->ni_data;
2962 struct kib_msg *msg = priv;
2963 int ver = conn->ibc_version;
2964 int rc = kiblnd_unpack_msg(msg, priv_nob);
2965 unsigned long flags;
2967 LASSERT (net != NULL);
2970 CERROR("Can't unpack connack from %s: %d\n",
2971 libcfs_nid2str(peer_ni->ibp_nid), rc);
2975 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2976 CERROR("Unexpected message %d from %s\n",
2977 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
2982 if (ver != msg->ibm_version) {
2983 CERROR("%s replied version %x is different with "
2984 "requested version %x\n",
2985 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
2990 if (msg->ibm_u.connparams.ibcp_queue_depth >
2991 conn->ibc_queue_depth) {
2992 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
2993 libcfs_nid2str(peer_ni->ibp_nid),
2994 msg->ibm_u.connparams.ibcp_queue_depth,
2995 conn->ibc_queue_depth);
3000 if (msg->ibm_u.connparams.ibcp_max_frags >
3001 conn->ibc_max_frags) {
3002 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
3003 libcfs_nid2str(peer_ni->ibp_nid),
3004 msg->ibm_u.connparams.ibcp_max_frags,
3005 conn->ibc_max_frags);
3010 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
3011 CERROR("%s max message size %d too big (%d max)\n",
3012 libcfs_nid2str(peer_ni->ibp_nid),
3013 msg->ibm_u.connparams.ibcp_max_msg_size,
3019 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3020 if (msg->ibm_dstnid == lnet_nid_to_nid4(&ni->ni_nid) &&
3021 msg->ibm_dststamp == net->ibn_incarnation)
3025 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3028 CERROR("Bad connection reply from %s, rc = %d, "
3029 "version: %x max_frags: %d\n",
3030 libcfs_nid2str(peer_ni->ibp_nid), rc,
3031 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
3035 conn->ibc_incarnation = msg->ibm_srcstamp;
3036 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3037 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
3038 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
3039 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
3040 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
3041 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
3043 kiblnd_connreq_done(conn, 0);
3047 /* NB My QP has already established itself, so I handle anything going
3048 * wrong here by setting ibc_comms_error.
3049 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
3050 * immediately tears it down. */
3053 conn->ibc_comms_error = rc;
3054 kiblnd_connreq_done(conn, 0);
3058 kiblnd_active_connect(struct rdma_cm_id *cmid)
3060 struct kib_peer_ni *peer_ni = cmid->context;
3061 struct kib_conn *conn;
3062 struct kib_msg *msg;
3063 struct rdma_conn_param cp;
3066 unsigned long flags;
3069 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3071 incarnation = peer_ni->ibp_incarnation;
3072 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
3073 peer_ni->ibp_version;
3075 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3077 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
3080 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
3081 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
3085 /* conn "owns" cmid now, so I return success from here on to ensure the
3086 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
3089 msg = &conn->ibc_connvars->cv_msg;
3091 memset(msg, 0, sizeof(*msg));
3092 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
3093 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
3094 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
3095 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
3097 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
3098 0, peer_ni->ibp_nid, incarnation);
3100 memset(&cp, 0, sizeof(cp));
3101 cp.private_data = msg;
3102 cp.private_data_len = msg->ibm_nob;
3103 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
3104 cp.initiator_depth = 0;
3105 cp.flow_control = 1;
3106 cp.retry_count = *kiblnd_tunables.kib_retry_count;
3107 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
3109 LASSERT(cmid->context == (void *)conn);
3110 LASSERT(conn->ibc_cmid == cmid);
3111 rc = rdma_connect_locked(cmid, &cp);
3113 CERROR("Can't connect to %s: %d\n",
3114 libcfs_nid2str(peer_ni->ibp_nid), rc);
3115 kiblnd_connreq_done(conn, rc);
3116 kiblnd_conn_decref(conn);
3123 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
3125 struct kib_peer_ni *peer_ni;
3126 struct kib_conn *conn;
3129 switch (event->event) {
3131 CERROR("Unexpected event: %d, status: %d\n",
3132 event->event, event->status);
3135 case RDMA_CM_EVENT_CONNECT_REQUEST:
3136 /* destroy cmid on failure */
3137 rc = kiblnd_passive_connect(cmid,
3138 (void *)KIBLND_CONN_PARAM(event),
3139 KIBLND_CONN_PARAM_LEN(event));
3140 CDEBUG(D_NET, "connreq: %d\n", rc);
3143 case RDMA_CM_EVENT_ADDR_ERROR:
3144 peer_ni = cmid->context;
3145 CNETERR("%s: ADDR ERROR %d\n",
3146 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3147 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3148 kiblnd_peer_decref(peer_ni);
3149 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3151 case RDMA_CM_EVENT_ADDR_RESOLVED:
3152 peer_ni = cmid->context;
3154 CDEBUG(D_NET,"%s Addr resolved: %d\n",
3155 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3157 if (event->status != 0) {
3158 CNETERR("Can't resolve address for %s: %d\n",
3159 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3162 rc = rdma_resolve_route(
3163 cmid, kiblnd_timeout() * 1000);
3165 struct kib_net *net = peer_ni->ibp_ni->ni_data;
3166 struct kib_dev *dev = net->ibn_dev;
3168 CDEBUG(D_NET, "%s: connection bound to "\
3170 libcfs_nid2str(peer_ni->ibp_nid),
3172 &dev->ibd_ifip, cmid->device->name);
3177 /* Can't initiate route resolution */
3178 CERROR("Can't resolve route for %s: %d\n",
3179 libcfs_nid2str(peer_ni->ibp_nid), rc);
3181 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3182 kiblnd_peer_decref(peer_ni);
3183 return rc; /* rc != 0 destroys cmid */
3185 case RDMA_CM_EVENT_ROUTE_ERROR:
3186 peer_ni = cmid->context;
3187 CNETERR("%s: ROUTE ERROR %d\n",
3188 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3189 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3190 kiblnd_peer_decref(peer_ni);
3191 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3193 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3194 peer_ni = cmid->context;
3195 CDEBUG(D_NET,"%s Route resolved: %d\n",
3196 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3198 if (event->status == 0)
3199 return kiblnd_active_connect(cmid);
3201 CNETERR("Can't resolve route for %s: %d\n",
3202 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3203 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3204 kiblnd_peer_decref(peer_ni);
3205 return event->status; /* rc != 0 destroys cmid */
3207 case RDMA_CM_EVENT_UNREACHABLE:
3208 conn = cmid->context;
3209 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3210 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3211 CNETERR("%s: UNREACHABLE %d\n",
3212 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3213 kiblnd_connreq_done(conn, -ENETDOWN);
3214 kiblnd_conn_decref(conn);
3217 case RDMA_CM_EVENT_CONNECT_ERROR:
3218 conn = cmid->context;
3219 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3220 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3221 CNETERR("%s: CONNECT ERROR %d\n",
3222 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3223 kiblnd_connreq_done(conn, -ENOTCONN);
3224 kiblnd_conn_decref(conn);
3227 case RDMA_CM_EVENT_REJECTED:
3228 conn = cmid->context;
3229 switch (conn->ibc_state) {
3233 case IBLND_CONN_PASSIVE_WAIT:
3234 CERROR ("%s: REJECTED %d\n",
3235 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3237 kiblnd_connreq_done(conn, -ECONNRESET);
3240 case IBLND_CONN_ACTIVE_CONNECT:
3241 kiblnd_rejected(conn, event->status,
3242 (void *)KIBLND_CONN_PARAM(event),
3243 KIBLND_CONN_PARAM_LEN(event));
3246 kiblnd_conn_decref(conn);
3249 case RDMA_CM_EVENT_ESTABLISHED:
3250 conn = cmid->context;
3251 switch (conn->ibc_state) {
3255 case IBLND_CONN_PASSIVE_WAIT:
3256 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3257 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3258 kiblnd_connreq_done(conn, 0);
3261 case IBLND_CONN_ACTIVE_CONNECT:
3262 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3263 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3264 kiblnd_check_connreply(conn,
3265 (void *)KIBLND_CONN_PARAM(event),
3266 KIBLND_CONN_PARAM_LEN(event));
3269 /* net keeps its ref on conn! */
3272 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3273 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3276 case RDMA_CM_EVENT_DISCONNECTED:
3277 conn = cmid->context;
3278 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3279 CERROR("%s DISCONNECTED\n",
3280 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3281 kiblnd_connreq_done(conn, -ECONNRESET);
3283 kiblnd_close_conn(conn, 0);
3285 kiblnd_conn_decref(conn);
3286 cmid->context = NULL;
3289 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3290 LCONSOLE_ERROR_MSG(0x131,
3291 "Received notification of device removal\n"
3292 "Please shutdown LNET to allow this to proceed\n");
3293 /* Can't remove network from underneath LNET for now, so I have
3297 case RDMA_CM_EVENT_ADDR_CHANGE:
3298 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3304 kiblnd_check_txs_locked(struct kib_conn *conn, struct list_head *txs)
3308 list_for_each_entry(tx, txs, tx_list) {
3309 if (txs != &conn->ibc_active_txs) {
3310 LASSERT(tx->tx_queued);
3312 LASSERT(!tx->tx_queued);
3313 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3316 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3317 CERROR("Timed out tx: %s(WSQ:%d%d%d), %lld seconds\n",
3318 kiblnd_queue2str(conn, txs),
3319 tx->tx_waiting, tx->tx_sending, tx->tx_queued,
3321 ktime_ms_delta(ktime_get(),
3322 tx->tx_deadline) / MSEC_PER_SEC);
3331 kiblnd_conn_timed_out_locked(struct kib_conn *conn)
3333 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3334 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3335 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3336 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3337 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3341 kiblnd_check_conns (int idx)
3344 LIST_HEAD(checksends);
3345 LIST_HEAD(timedout_txs);
3346 struct hlist_head *peers = &kiblnd_data.kib_peers[idx];
3347 struct kib_peer_ni *peer_ni;
3348 struct kib_conn *conn;
3349 struct kib_tx *tx, *tx_tmp;
3350 unsigned long flags;
3352 /* NB. We expect to have a look at all the peers and not find any
3353 * RDMAs to time out, so we just use a shared lock while we
3356 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3358 hlist_for_each_entry(peer_ni, peers, ibp_list) {
3359 /* Check tx_deadline */
3360 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3361 if (ktime_compare(ktime_get(), tx->tx_deadline) >= 0) {
3362 CWARN("Timed out tx for %s: %lld seconds\n",
3363 libcfs_nid2str(peer_ni->ibp_nid),
3364 ktime_ms_delta(ktime_get(),
3365 tx->tx_deadline) / MSEC_PER_SEC);
3366 list_move(&tx->tx_list, &timedout_txs);
3370 list_for_each_entry(conn, &peer_ni->ibp_conns, ibc_list) {
3374 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3376 spin_lock(&conn->ibc_lock);
3378 sendnoop = kiblnd_need_noop(conn);
3379 timedout = kiblnd_conn_timed_out_locked(conn);
3380 if (!sendnoop && !timedout) {
3381 spin_unlock(&conn->ibc_lock);
3386 CERROR("Timed out RDMA with %s (%lld): c: %u, oc: %u, rc: %u\n",
3387 libcfs_nid2str(peer_ni->ibp_nid),
3389 - peer_ni->ibp_last_alive,
3391 conn->ibc_outstanding_credits,
3392 conn->ibc_reserved_credits);
3393 list_add(&conn->ibc_connd_list, &closes);
3395 list_add(&conn->ibc_connd_list, &checksends);
3397 /* +ref for 'closes' or 'checksends' */
3398 kiblnd_conn_addref(conn);
3400 spin_unlock(&conn->ibc_lock);
3404 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3406 if (!list_empty(&timedout_txs))
3407 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT,
3408 LNET_MSG_STATUS_NETWORK_TIMEOUT);
3410 /* Handle timeout by closing the whole
3411 * connection. We can only be sure RDMA activity
3412 * has ceased once the QP has been modified.
3414 while ((conn = list_first_entry_or_null(&closes,
3416 ibc_connd_list)) != NULL) {
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...
3426 while ((conn = list_first_entry_or_null(&checksends,
3428 ibc_connd_list)) != NULL) {
3429 list_del(&conn->ibc_connd_list);
3431 spin_lock(&conn->ibc_lock);
3432 kiblnd_check_sends_locked(conn);
3433 spin_unlock(&conn->ibc_lock);
3435 kiblnd_conn_decref(conn);
3440 kiblnd_disconnect_conn(struct kib_conn *conn)
3442 LASSERT (!in_interrupt());
3443 LASSERT (current == kiblnd_data.kib_connd);
3444 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3446 rdma_disconnect(conn->ibc_cmid);
3447 kiblnd_finalise_conn(conn);
3449 kiblnd_peer_notify(conn->ibc_peer);
3453 * High-water for reconnection to the same peer_ni, reconnection attempt should
3454 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3456 #define KIB_RECONN_HIGH_RACE 10
3458 * Allow connd to take a break and handle other things after consecutive
3459 * reconnection attemps.
3461 #define KIB_RECONN_BREAK 100
3464 kiblnd_connd (void *arg)
3466 spinlock_t *lock = &kiblnd_data.kib_connd_lock;
3467 wait_queue_entry_t wait;
3468 unsigned long flags;
3469 struct kib_conn *conn;
3474 unsigned long deadline = jiffies;
3477 kiblnd_data.kib_connd = current;
3479 spin_lock_irqsave(lock, flags);
3481 while (!kiblnd_data.kib_shutdown) {
3484 dropped_lock = false;
3486 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_zombies,
3487 struct kib_conn, ibc_list);
3489 struct kib_peer_ni *peer_ni = NULL;
3491 list_del(&conn->ibc_list);
3492 if (conn->ibc_reconnect) {
3493 peer_ni = conn->ibc_peer;
3494 kiblnd_peer_addref(peer_ni);
3497 spin_unlock_irqrestore(lock, flags);
3498 dropped_lock = true;
3500 kiblnd_destroy_conn(conn);
3502 spin_lock_irqsave(lock, flags);
3504 LIBCFS_FREE(conn, sizeof(*conn));
3508 conn->ibc_peer = peer_ni;
3509 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3510 list_add_tail(&conn->ibc_list,
3511 &kiblnd_data.kib_reconn_list);
3513 list_add_tail(&conn->ibc_list,
3514 &kiblnd_data.kib_reconn_wait);
3517 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_conns,
3518 struct kib_conn, ibc_list);
3522 list_del(&conn->ibc_list);
3524 spin_unlock_irqrestore(lock, flags);
3525 dropped_lock = true;
3527 kiblnd_disconnect_conn(conn);
3528 wait = conn->ibc_waits;
3529 if (wait == 0) /* keep ref for connd_wait, see below */
3530 kiblnd_conn_decref(conn);
3532 spin_lock_irqsave(lock, flags);
3535 list_add_tail(&conn->ibc_list,
3536 &kiblnd_data.kib_connd_waits);
3539 while (reconn < KIB_RECONN_BREAK) {
3540 if (kiblnd_data.kib_reconn_sec !=
3541 ktime_get_real_seconds()) {
3542 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3543 list_splice_init(&kiblnd_data.kib_reconn_wait,
3544 &kiblnd_data.kib_reconn_list);
3547 conn = list_first_entry_or_null(&kiblnd_data.kib_reconn_list,
3548 struct kib_conn, ibc_list);
3552 list_del(&conn->ibc_list);
3554 spin_unlock_irqrestore(lock, flags);
3555 dropped_lock = true;
3557 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3558 kiblnd_peer_decref(conn->ibc_peer);
3559 LIBCFS_FREE(conn, sizeof(*conn));
3561 spin_lock_irqsave(lock, flags);
3564 conn = list_first_entry_or_null(&kiblnd_data.kib_connd_waits,
3565 struct kib_conn, ibc_list);
3567 list_del(&conn->ibc_list);
3568 spin_unlock_irqrestore(lock, flags);
3570 dropped_lock = kiblnd_tx_may_discard(conn);
3572 kiblnd_conn_decref(conn);
3574 spin_lock_irqsave(lock, flags);
3576 list_add_tail(&conn->ibc_list,
3577 &kiblnd_data.kib_connd_waits);
3580 /* careful with the jiffy wrap... */
3581 timeout = (int)(deadline - jiffies);
3585 int chunk = HASH_SIZE(kiblnd_data.kib_peers);
3586 unsigned int lnd_timeout;
3588 spin_unlock_irqrestore(lock, flags);
3589 dropped_lock = true;
3591 /* Time to check for RDMA timeouts on a few more
3592 * peers: I do checks every 'p' seconds on a
3593 * proportion of the peer_ni table and I need to check
3594 * every connection 'n' times within a timeout
3595 * interval, to ensure I detect a timeout on any
3596 * connection within (n+1)/n times the timeout
3600 lnd_timeout = kiblnd_timeout();
3601 if (lnd_timeout > n * p)
3602 chunk = (chunk * n * p) / lnd_timeout;
3606 for (i = 0; i < chunk; i++) {
3607 kiblnd_check_conns(peer_index);
3608 peer_index = (peer_index + 1) %
3609 HASH_SIZE(kiblnd_data.kib_peers);
3612 deadline += cfs_time_seconds(p);
3613 spin_lock_irqsave(lock, flags);
3619 /* Nothing to do for 'timeout' */
3620 set_current_state(TASK_INTERRUPTIBLE);
3621 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3622 spin_unlock_irqrestore(lock, flags);
3624 schedule_timeout(timeout);
3626 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3627 spin_lock_irqsave(lock, flags);
3630 spin_unlock_irqrestore(lock, flags);
3632 kiblnd_thread_fini();
3637 kiblnd_qp_event(struct ib_event *event, void *arg)
3639 struct kib_conn *conn = arg;
3641 switch (event->event) {
3642 case IB_EVENT_COMM_EST:
3643 CDEBUG(D_NET, "%s established\n",
3644 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3645 /* We received a packet but connection isn't established
3646 * probably handshake packet was lost, so free to
3647 * force make connection established */
3648 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3651 case IB_EVENT_PORT_ERR:
3652 case IB_EVENT_DEVICE_FATAL:
3653 CERROR("Fatal device error for NI %s\n",
3654 libcfs_nidstr(&conn->ibc_peer->ibp_ni->ni_nid));
3655 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 1);
3658 case IB_EVENT_PORT_ACTIVE:
3659 CERROR("Port reactivated for NI %s\n",
3660 libcfs_nidstr(&conn->ibc_peer->ibp_ni->ni_nid));
3661 atomic_set(&conn->ibc_peer->ibp_ni->ni_fatal_error_on, 0);
3665 CERROR("%s: Async QP event type %d\n",
3666 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3672 kiblnd_complete (struct ib_wc *wc)
3674 switch (kiblnd_wreqid2type(wc->wr_id)) {
3679 if (wc->status != IB_WC_SUCCESS &&
3680 wc->status != IB_WC_WR_FLUSH_ERR)
3681 CNETERR("FastReg failed: %d\n", wc->status);
3684 case IBLND_WID_RDMA:
3685 /* We only get RDMA completion notification if it fails. All
3686 * subsequent work items, including the final SEND will fail
3687 * too. However we can't print out any more info about the
3688 * failing RDMA because 'tx' might be back on the idle list or
3689 * even reused already if we didn't manage to post all our work
3691 CNETERR("RDMA (tx: %p) failed: %d\n",
3692 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3696 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3700 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3707 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3709 /* NB I'm not allowed to schedule this conn once its refcount has
3710 * reached 0. Since fundamentally I'm racing with scheduler threads
3711 * consuming my CQ I could be called after all completions have
3712 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3713 * and this CQ is about to be destroyed so I NOOP. */
3714 struct kib_conn *conn = arg;
3715 struct kib_sched_info *sched = conn->ibc_sched;
3716 unsigned long flags;
3718 LASSERT(cq == conn->ibc_cq);
3720 spin_lock_irqsave(&sched->ibs_lock, flags);
3722 conn->ibc_ready = 1;
3724 if (!conn->ibc_scheduled &&
3725 (conn->ibc_nrx > 0 ||
3726 conn->ibc_nsends_posted > 0)) {
3727 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3728 conn->ibc_scheduled = 1;
3729 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3731 if (waitqueue_active(&sched->ibs_waitq))
3732 wake_up(&sched->ibs_waitq);
3735 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3739 kiblnd_cq_event(struct ib_event *event, void *arg)
3741 struct kib_conn *conn = arg;
3743 CERROR("%s: async CQ event type %d\n",
3744 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3748 kiblnd_scheduler(void *arg)
3750 long id = (long)arg;
3751 struct kib_sched_info *sched;
3752 struct kib_conn *conn;
3753 wait_queue_entry_t wait;
3754 unsigned long flags;
3761 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3763 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3765 CWARN("Unable to bind on CPU partition %d, please verify whether all CPUs are healthy and reload modules if necessary, otherwise your system might under risk of low performance\n", sched->ibs_cpt);
3768 spin_lock_irqsave(&sched->ibs_lock, flags);
3770 while (!kiblnd_data.kib_shutdown) {
3771 if (need_resched()) {
3772 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3776 spin_lock_irqsave(&sched->ibs_lock, flags);
3779 did_something = false;
3781 conn = list_first_entry_or_null(&sched->ibs_conns,
3785 /* take over kib_sched_conns' ref on conn... */
3786 LASSERT(conn->ibc_scheduled);
3787 list_del(&conn->ibc_sched_list);
3788 conn->ibc_ready = 0;
3790 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3792 wc.wr_id = IBLND_WID_INVAL;
3794 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3796 rc = ib_req_notify_cq(conn->ibc_cq,
3799 CWARN("%s: ib_req_notify_cq failed: %d, closing connection\n",
3800 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3801 kiblnd_close_conn(conn, -EIO);
3802 kiblnd_conn_decref(conn);
3803 spin_lock_irqsave(&sched->ibs_lock,
3808 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3811 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3813 "ib_poll_cq (rc: %d) returned invalid "
3814 "wr_id, opcode %d, status: %d, "
3815 "vendor_err: %d, conn: %s status: %d\n"
3816 "please upgrade firmware and OFED or "
3817 "contact vendor.\n", rc,
3818 wc.opcode, wc.status, wc.vendor_err,
3819 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3825 CWARN("%s: ib_poll_cq failed: %d, closing connection\n",
3826 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3828 kiblnd_close_conn(conn, -EIO);
3829 kiblnd_conn_decref(conn);
3830 spin_lock_irqsave(&sched->ibs_lock, flags);
3834 spin_lock_irqsave(&sched->ibs_lock, flags);
3836 if (rc != 0 || conn->ibc_ready) {
3837 /* There may be another completion waiting; get
3838 * another scheduler to check while I handle
3840 /* +1 ref for sched_conns */
3841 kiblnd_conn_addref(conn);
3842 list_add_tail(&conn->ibc_sched_list,
3844 if (waitqueue_active(&sched->ibs_waitq))
3845 wake_up(&sched->ibs_waitq);
3847 conn->ibc_scheduled = 0;
3851 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3852 kiblnd_complete(&wc);
3854 spin_lock_irqsave(&sched->ibs_lock, flags);
3857 kiblnd_conn_decref(conn); /* ..drop my ref from above */
3858 did_something = true;
3864 set_current_state(TASK_INTERRUPTIBLE);
3865 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3866 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3870 remove_wait_queue(&sched->ibs_waitq, &wait);
3871 set_current_state(TASK_RUNNING);
3872 spin_lock_irqsave(&sched->ibs_lock, flags);
3875 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3877 kiblnd_thread_fini();
3882 kiblnd_failover_thread(void *arg)
3884 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3885 struct kib_dev *dev;
3886 struct net *ns = arg;
3887 wait_queue_entry_t wait;
3888 unsigned long flags;
3891 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3894 write_lock_irqsave(glock, flags);
3896 while (!kiblnd_data.kib_shutdown) {
3897 bool do_failover = false;
3900 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3902 if (ktime_get_seconds() < dev->ibd_next_failover)
3909 list_del_init(&dev->ibd_fail_list);
3910 dev->ibd_failover = 1;
3911 write_unlock_irqrestore(glock, flags);
3913 rc = kiblnd_dev_failover(dev, ns);
3915 write_lock_irqsave(glock, flags);
3917 LASSERT(dev->ibd_failover);
3918 dev->ibd_failover = 0;
3919 if (rc >= 0) { /* Device is OK or failover succeed */
3920 dev->ibd_next_failover = ktime_get_seconds() + 3;
3924 /* failed to failover, retry later */
3925 dev->ibd_next_failover = ktime_get_seconds() +
3926 min(dev->ibd_failed_failover, 10);
3927 if (kiblnd_dev_can_failover(dev)) {
3928 list_add_tail(&dev->ibd_fail_list,
3929 &kiblnd_data.kib_failed_devs);
3935 /* long sleep if no more pending failover */
3936 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3938 set_current_state(TASK_INTERRUPTIBLE);
3939 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3940 write_unlock_irqrestore(glock, flags);
3942 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3943 cfs_time_seconds(1));
3944 set_current_state(TASK_RUNNING);
3945 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3946 write_lock_irqsave(glock, flags);
3948 if (!long_sleep || rc != 0)
3951 /* have a long sleep, routine check all active devices,
3952 * we need checking like this because if there is not active
3953 * connection on the dev and no SEND from local, we may listen
3954 * on wrong HCA for ever while there is a bonding failover
3956 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3957 if (kiblnd_dev_can_failover(dev)) {
3958 list_add_tail(&dev->ibd_fail_list,
3959 &kiblnd_data.kib_failed_devs);
3964 write_unlock_irqrestore(glock, flags);
3966 kiblnd_thread_fini();