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, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lnet/klnds/o2iblnd/o2iblnd_cb.c
34 * Author: Eric Barton <eric@bartonsoftware.com>
39 #define MAX_CONN_RACES_BEFORE_ABORT 20
41 static void kiblnd_peer_alive(kib_peer_ni_t *peer_ni);
42 static void kiblnd_peer_connect_failed(kib_peer_ni_t *peer_ni, int active, int error);
43 static void kiblnd_init_tx_msg(struct lnet_ni *ni, kib_tx_t *tx,
44 int type, int body_nob);
45 static int kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
46 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie);
47 static void kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn);
48 static void kiblnd_queue_tx(kib_tx_t *tx, kib_conn_t *conn);
50 static void kiblnd_unmap_tx(kib_tx_t *tx);
51 static void kiblnd_check_sends_locked(kib_conn_t *conn);
54 kiblnd_tx_done(kib_tx_t *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 lnet_finalize(lntmsg[i], rc);
93 kiblnd_txlist_done(struct list_head *txlist, int status)
97 while (!list_empty(txlist)) {
98 tx = list_entry(txlist->next, kib_tx_t, tx_list);
100 list_del(&tx->tx_list);
103 tx->tx_status = status;
109 kiblnd_get_idle_tx(struct lnet_ni *ni, lnet_nid_t target)
111 kib_net_t *net = (kib_net_t *)ni->ni_data;
112 struct list_head *node;
114 kib_tx_poolset_t *tps;
116 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target, ni)];
117 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
120 tx = container_of(node, kib_tx_t, tx_list);
122 LASSERT (tx->tx_nwrq == 0);
123 LASSERT (!tx->tx_queued);
124 LASSERT (tx->tx_sending == 0);
125 LASSERT (!tx->tx_waiting);
126 LASSERT (tx->tx_status == 0);
127 LASSERT (tx->tx_conn == NULL);
128 LASSERT (tx->tx_lntmsg[0] == NULL);
129 LASSERT (tx->tx_lntmsg[1] == NULL);
130 LASSERT (tx->tx_nfrags == 0);
136 kiblnd_drop_rx(kib_rx_t *rx)
138 kib_conn_t *conn = rx->rx_conn;
139 struct kib_sched_info *sched = conn->ibc_sched;
142 spin_lock_irqsave(&sched->ibs_lock, flags);
143 LASSERT(conn->ibc_nrx > 0);
145 spin_unlock_irqrestore(&sched->ibs_lock, flags);
147 kiblnd_conn_decref(conn);
151 kiblnd_post_rx (kib_rx_t *rx, int credit)
153 kib_conn_t *conn = rx->rx_conn;
154 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
155 struct ib_recv_wr *bad_wrq = NULL;
156 #ifdef HAVE_IB_GET_DMA_MR
157 struct ib_mr *mr = conn->ibc_hdev->ibh_mrs;
161 LASSERT (net != NULL);
162 LASSERT (!in_interrupt());
163 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
164 credit == IBLND_POSTRX_PEER_CREDIT ||
165 credit == IBLND_POSTRX_RSRVD_CREDIT);
166 #ifdef HAVE_IB_GET_DMA_MR
169 rx->rx_sge.lkey = mr->lkey;
171 rx->rx_sge.lkey = conn->ibc_hdev->ibh_pd->local_dma_lkey;
173 rx->rx_sge.addr = rx->rx_msgaddr;
174 rx->rx_sge.length = IBLND_MSG_SIZE;
176 rx->rx_wrq.next = NULL;
177 rx->rx_wrq.sg_list = &rx->rx_sge;
178 rx->rx_wrq.num_sge = 1;
179 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
181 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
182 LASSERT (rx->rx_nob >= 0); /* not posted */
184 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
185 kiblnd_drop_rx(rx); /* No more posts for this rx */
189 rx->rx_nob = -1; /* flag posted */
191 /* NB: need an extra reference after ib_post_recv because we don't
192 * own this rx (and rx::rx_conn) anymore, LU-5678.
194 kiblnd_conn_addref(conn);
195 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
196 if (unlikely(rc != 0)) {
197 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
198 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
202 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
205 if (unlikely(rc != 0)) {
206 kiblnd_close_conn(conn, rc);
207 kiblnd_drop_rx(rx); /* No more posts for this rx */
211 if (credit == IBLND_POSTRX_NO_CREDIT)
214 spin_lock(&conn->ibc_lock);
215 if (credit == IBLND_POSTRX_PEER_CREDIT)
216 conn->ibc_outstanding_credits++;
218 conn->ibc_reserved_credits++;
219 kiblnd_check_sends_locked(conn);
220 spin_unlock(&conn->ibc_lock);
223 kiblnd_conn_decref(conn);
228 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
230 struct list_head *tmp;
232 list_for_each(tmp, &conn->ibc_active_txs) {
233 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
235 LASSERT(!tx->tx_queued);
236 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
238 if (tx->tx_cookie != cookie)
241 if (tx->tx_waiting &&
242 tx->tx_msg->ibm_type == txtype)
245 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
246 tx->tx_waiting ? "" : "NOT ",
247 tx->tx_msg->ibm_type, txtype);
253 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
256 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
259 spin_lock(&conn->ibc_lock);
261 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
263 spin_unlock(&conn->ibc_lock);
265 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
266 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
267 kiblnd_close_conn(conn, -EPROTO);
271 if (tx->tx_status == 0) { /* success so far */
272 if (status < 0) { /* failed? */
273 tx->tx_status = status;
274 } else if (txtype == IBLND_MSG_GET_REQ) {
275 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
281 idle = !tx->tx_queued && (tx->tx_sending == 0);
283 list_del(&tx->tx_list);
285 spin_unlock(&conn->ibc_lock);
292 kiblnd_send_completion(kib_conn_t *conn, int type, int status, __u64 cookie)
294 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
295 kib_tx_t *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
298 CERROR("Can't get tx for completion %x for %s\n",
299 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
303 tx->tx_msg->ibm_u.completion.ibcm_status = status;
304 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
305 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
307 kiblnd_queue_tx(tx, conn);
311 kiblnd_handle_rx (kib_rx_t *rx)
313 kib_msg_t *msg = rx->rx_msg;
314 kib_conn_t *conn = rx->rx_conn;
315 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
316 int credits = msg->ibm_credits;
322 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
324 CDEBUG (D_NET, "Received %x[%d] from %s\n",
325 msg->ibm_type, credits,
326 libcfs_nid2str(conn->ibc_peer->ibp_nid));
329 /* Have I received credits that will let me send? */
330 spin_lock(&conn->ibc_lock);
332 if (conn->ibc_credits + credits >
333 conn->ibc_queue_depth) {
334 rc2 = conn->ibc_credits;
335 spin_unlock(&conn->ibc_lock);
337 CERROR("Bad credits from %s: %d + %d > %d\n",
338 libcfs_nid2str(conn->ibc_peer->ibp_nid),
340 conn->ibc_queue_depth);
342 kiblnd_close_conn(conn, -EPROTO);
343 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
347 conn->ibc_credits += credits;
349 /* This ensures the credit taken by NOOP can be returned */
350 if (msg->ibm_type == IBLND_MSG_NOOP &&
351 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
352 conn->ibc_outstanding_credits++;
354 kiblnd_check_sends_locked(conn);
355 spin_unlock(&conn->ibc_lock);
358 switch (msg->ibm_type) {
360 CERROR("Bad IBLND message type %x from %s\n",
361 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
362 post_credit = IBLND_POSTRX_NO_CREDIT;
367 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
368 post_credit = IBLND_POSTRX_NO_CREDIT;
372 if (credits != 0) /* credit already posted */
373 post_credit = IBLND_POSTRX_NO_CREDIT;
374 else /* a keepalive NOOP */
375 post_credit = IBLND_POSTRX_PEER_CREDIT;
378 case IBLND_MSG_IMMEDIATE:
379 post_credit = IBLND_POSTRX_DONT_POST;
380 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
381 msg->ibm_srcnid, rx, 0);
382 if (rc < 0) /* repost on error */
383 post_credit = IBLND_POSTRX_PEER_CREDIT;
386 case IBLND_MSG_PUT_REQ:
387 post_credit = IBLND_POSTRX_DONT_POST;
388 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
389 msg->ibm_srcnid, rx, 1);
390 if (rc < 0) /* repost on error */
391 post_credit = IBLND_POSTRX_PEER_CREDIT;
394 case IBLND_MSG_PUT_NAK:
395 CWARN ("PUT_NACK from %s\n",
396 libcfs_nid2str(conn->ibc_peer->ibp_nid));
397 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
398 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
399 msg->ibm_u.completion.ibcm_status,
400 msg->ibm_u.completion.ibcm_cookie);
403 case IBLND_MSG_PUT_ACK:
404 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
406 spin_lock(&conn->ibc_lock);
407 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
408 msg->ibm_u.putack.ibpam_src_cookie);
410 list_del(&tx->tx_list);
411 spin_unlock(&conn->ibc_lock);
414 CERROR("Unmatched PUT_ACK from %s\n",
415 libcfs_nid2str(conn->ibc_peer->ibp_nid));
420 LASSERT (tx->tx_waiting);
421 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
422 * (a) I can overwrite tx_msg since my peer_ni has received it!
423 * (b) tx_waiting set tells tx_complete() it's not done. */
425 tx->tx_nwrq = tx->tx_nsge = 0; /* overwrite PUT_REQ */
427 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
428 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
429 &msg->ibm_u.putack.ibpam_rd,
430 msg->ibm_u.putack.ibpam_dst_cookie);
432 CERROR("Can't setup rdma for PUT to %s: %d\n",
433 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
435 spin_lock(&conn->ibc_lock);
436 tx->tx_waiting = 0; /* clear waiting and queue atomically */
437 kiblnd_queue_tx_locked(tx, conn);
438 spin_unlock(&conn->ibc_lock);
441 case IBLND_MSG_PUT_DONE:
442 post_credit = IBLND_POSTRX_PEER_CREDIT;
443 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
444 msg->ibm_u.completion.ibcm_status,
445 msg->ibm_u.completion.ibcm_cookie);
448 case IBLND_MSG_GET_REQ:
449 post_credit = IBLND_POSTRX_DONT_POST;
450 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
451 msg->ibm_srcnid, rx, 1);
452 if (rc < 0) /* repost on error */
453 post_credit = IBLND_POSTRX_PEER_CREDIT;
456 case IBLND_MSG_GET_DONE:
457 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
458 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
459 msg->ibm_u.completion.ibcm_status,
460 msg->ibm_u.completion.ibcm_cookie);
464 if (rc < 0) /* protocol error */
465 kiblnd_close_conn(conn, rc);
467 if (post_credit != IBLND_POSTRX_DONT_POST)
468 kiblnd_post_rx(rx, post_credit);
472 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
474 kib_msg_t *msg = rx->rx_msg;
475 kib_conn_t *conn = rx->rx_conn;
476 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
477 kib_net_t *net = ni->ni_data;
481 LASSERT (net != NULL);
482 LASSERT (rx->rx_nob < 0); /* was posted */
483 rx->rx_nob = 0; /* isn't now */
485 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
488 if (status != IB_WC_SUCCESS) {
489 CNETERR("Rx from %s failed: %d\n",
490 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
497 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
499 CERROR ("Error %d unpacking rx from %s\n",
500 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
504 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
505 msg->ibm_dstnid != ni->ni_nid ||
506 msg->ibm_srcstamp != conn->ibc_incarnation ||
507 msg->ibm_dststamp != net->ibn_incarnation) {
508 CERROR ("Stale rx from %s\n",
509 libcfs_nid2str(conn->ibc_peer->ibp_nid));
514 /* set time last known alive */
515 kiblnd_peer_alive(conn->ibc_peer);
517 /* racing with connection establishment/teardown! */
519 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
520 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
523 write_lock_irqsave(g_lock, flags);
524 /* must check holding global lock to eliminate race */
525 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
526 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
527 write_unlock_irqrestore(g_lock, flags);
530 write_unlock_irqrestore(g_lock, flags);
532 kiblnd_handle_rx(rx);
536 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
537 kiblnd_close_conn(conn, err);
539 kiblnd_drop_rx(rx); /* Don't re-post rx. */
543 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, __u32 nob)
546 kib_fmr_poolset_t *fps;
552 LASSERT(tx->tx_pool != NULL);
553 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
555 hdev = tx->tx_pool->tpo_hdev;
556 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
558 fps = net->ibn_fmr_ps[cpt];
559 rc = kiblnd_fmr_pool_map(fps, tx, rd, nob, 0, &tx->fmr, &is_fastreg);
561 CERROR("Can't map %u pages: %d\n", nob, rc);
565 /* If rd is not tx_rd, it's going to get sent to a peer_ni, who will need
567 rd->rd_key = tx->fmr.fmr_key;
569 for (i = 0; i < rd->rd_nfrags; i++) {
570 rd->rd_frags[i].rf_addr &= ~hdev->ibh_page_mask;
571 rd->rd_frags[i].rf_addr += i << hdev->ibh_page_shift;
574 rd->rd_frags[0].rf_nob = nob;
582 kiblnd_unmap_tx(kib_tx_t *tx)
584 if (tx->fmr.fmr_pfmr || tx->fmr.fmr_frd)
585 kiblnd_fmr_pool_unmap(&tx->fmr, tx->tx_status);
587 if (tx->tx_nfrags != 0) {
588 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
589 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
595 kiblnd_map_tx(struct lnet_ni *ni, kib_tx_t *tx, kib_rdma_desc_t *rd, int nfrags)
597 kib_net_t *net = ni->ni_data;
598 kib_hca_dev_t *hdev = net->ibn_dev->ibd_hdev;
599 #ifdef HAVE_IB_GET_DMA_MR
600 struct ib_mr *mr = NULL;
605 /* If rd is not tx_rd, it's going to get sent to a peer_ni and I'm the
607 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
608 tx->tx_nfrags = nfrags;
610 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
611 tx->tx_nfrags, tx->tx_dmadir);
613 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
614 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
615 hdev->ibh_ibdev, &tx->tx_frags[i]);
616 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
617 hdev->ibh_ibdev, &tx->tx_frags[i]);
618 nob += rd->rd_frags[i].rf_nob;
621 #ifdef HAVE_IB_GET_DMA_MR
622 mr = kiblnd_find_rd_dma_mr(ni, rd,
623 (tx->tx_conn != NULL) ?
624 tx->tx_conn->ibc_max_frags : -1);
626 /* found pre-mapping MR */
627 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
632 if (net->ibn_fmr_ps != NULL)
633 return kiblnd_fmr_map_tx(net, tx, rd, nob);
640 kiblnd_setup_rd_iov(struct lnet_ni *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
641 unsigned int niov, struct kvec *iov, int offset, int nob)
643 kib_net_t *net = ni->ni_data;
645 struct scatterlist *sg;
652 LASSERT (net != NULL);
654 while (offset >= iov->iov_len) {
655 offset -= iov->iov_len;
665 vaddr = ((unsigned long)iov->iov_base) + offset;
666 page_offset = vaddr & (PAGE_SIZE - 1);
667 page = lnet_kvaddr_to_page(vaddr);
669 CERROR("Can't find page\n");
673 fragnob = min((int)(iov->iov_len - offset), nob);
674 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
676 sg_set_page(sg, page, fragnob, page_offset);
679 CERROR("lacking enough sg entries to map tx\n");
683 if (offset + fragnob < iov->iov_len) {
693 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
697 kiblnd_setup_rd_kiov(struct lnet_ni *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
698 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
700 kib_net_t *net = ni->ni_data;
701 struct scatterlist *sg;
704 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
708 LASSERT (net != NULL);
710 while (offset >= kiov->kiov_len) {
711 offset -= kiov->kiov_len;
721 fragnob = min((int)(kiov->kiov_len - offset), nob);
723 sg_set_page(sg, kiov->kiov_page, fragnob,
724 kiov->kiov_offset + offset);
727 CERROR("lacking enough sg entries to map tx\n");
737 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
741 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
742 __must_hold(&conn->ibc_lock)
744 kib_msg_t *msg = tx->tx_msg;
745 kib_peer_ni_t *peer_ni = conn->ibc_peer;
746 struct lnet_ni *ni = peer_ni->ibp_ni;
747 int ver = conn->ibc_version;
751 LASSERT(tx->tx_queued);
752 /* We rely on this for QP sizing */
753 LASSERT(tx->tx_nwrq > 0 && tx->tx_nsge >= 0);
754 LASSERT(tx->tx_nwrq <= 1 + conn->ibc_max_frags);
756 LASSERT(credit == 0 || credit == 1);
757 LASSERT(conn->ibc_outstanding_credits >= 0);
758 LASSERT(conn->ibc_outstanding_credits <= conn->ibc_queue_depth);
759 LASSERT(conn->ibc_credits >= 0);
760 LASSERT(conn->ibc_credits <= conn->ibc_queue_depth);
762 if (conn->ibc_nsends_posted ==
763 kiblnd_concurrent_sends(ver, ni)) {
764 /* tx completions outstanding... */
765 CDEBUG(D_NET, "%s: posted enough\n",
766 libcfs_nid2str(peer_ni->ibp_nid));
770 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
771 CDEBUG(D_NET, "%s: no credits\n",
772 libcfs_nid2str(peer_ni->ibp_nid));
776 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
777 conn->ibc_credits == 1 && /* last credit reserved */
778 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
779 CDEBUG(D_NET, "%s: not using last credit\n",
780 libcfs_nid2str(peer_ni->ibp_nid));
784 /* NB don't drop ibc_lock before bumping tx_sending */
785 list_del(&tx->tx_list);
788 if (msg->ibm_type == IBLND_MSG_NOOP &&
789 (!kiblnd_need_noop(conn) || /* redundant NOOP */
790 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
791 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
792 /* OK to drop when posted enough NOOPs, since
793 * kiblnd_check_sends_locked will queue NOOP again when
794 * posted NOOPs complete */
795 spin_unlock(&conn->ibc_lock);
797 spin_lock(&conn->ibc_lock);
798 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
799 libcfs_nid2str(peer_ni->ibp_nid),
800 conn->ibc_noops_posted);
804 kiblnd_pack_msg(peer_ni->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
805 peer_ni->ibp_nid, conn->ibc_incarnation);
807 conn->ibc_credits -= credit;
808 conn->ibc_outstanding_credits = 0;
809 conn->ibc_nsends_posted++;
810 if (msg->ibm_type == IBLND_MSG_NOOP)
811 conn->ibc_noops_posted++;
813 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
814 * PUT. If so, it was first queued here as a PUT_REQ, sent and
815 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
816 * and then re-queued here. It's (just) possible that
817 * tx_sending is non-zero if we've not done the tx_complete()
818 * from the first send; hence the ++ rather than = below. */
820 list_add(&tx->tx_list, &conn->ibc_active_txs);
822 /* I'm still holding ibc_lock! */
823 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
825 } else if (tx->tx_pool->tpo_pool.po_failed ||
826 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
827 /* close_conn will launch failover */
830 struct kib_fast_reg_descriptor *frd = tx->fmr.fmr_frd;
831 struct ib_send_wr *bad = &tx->tx_wrq[tx->tx_nwrq - 1].wr;
832 struct ib_send_wr *wr = &tx->tx_wrq[0].wr;
835 if (!frd->frd_valid) {
836 wr = &frd->frd_inv_wr.wr;
837 wr->next = &frd->frd_fastreg_wr.wr;
839 wr = &frd->frd_fastreg_wr.wr;
841 frd->frd_fastreg_wr.wr.next = &tx->tx_wrq[0].wr;
844 LASSERTF(bad->wr_id == kiblnd_ptr2wreqid(tx, IBLND_WID_TX),
845 "bad wr_id %#llx, opc %d, flags %d, peer_ni: %s\n",
846 bad->wr_id, bad->opcode, bad->send_flags,
847 libcfs_nid2str(conn->ibc_peer->ibp_nid));
850 rc = ib_post_send(conn->ibc_cmid->qp, wr, &bad);
853 conn->ibc_last_send = jiffies;
858 /* NB credits are transferred in the actual
859 * message, which can only be the last work item */
860 conn->ibc_credits += credit;
861 conn->ibc_outstanding_credits += msg->ibm_credits;
862 conn->ibc_nsends_posted--;
863 if (msg->ibm_type == IBLND_MSG_NOOP)
864 conn->ibc_noops_posted--;
870 done = (tx->tx_sending == 0);
872 list_del(&tx->tx_list);
874 spin_unlock(&conn->ibc_lock);
876 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
877 CERROR("Error %d posting transmit to %s\n",
878 rc, libcfs_nid2str(peer_ni->ibp_nid));
880 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
881 rc, libcfs_nid2str(peer_ni->ibp_nid));
883 kiblnd_close_conn(conn, rc);
888 spin_lock(&conn->ibc_lock);
894 kiblnd_check_sends_locked(kib_conn_t *conn)
896 int ver = conn->ibc_version;
897 struct lnet_ni *ni = conn->ibc_peer->ibp_ni;
900 /* Don't send anything until after the connection is established */
901 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
902 CDEBUG(D_NET, "%s too soon\n",
903 libcfs_nid2str(conn->ibc_peer->ibp_nid));
907 LASSERT(conn->ibc_nsends_posted <=
908 kiblnd_concurrent_sends(ver, ni));
909 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
910 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
911 LASSERT (conn->ibc_reserved_credits >= 0);
913 while (conn->ibc_reserved_credits > 0 &&
914 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
915 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
917 list_del(&tx->tx_list);
918 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
919 conn->ibc_reserved_credits--;
922 if (kiblnd_need_noop(conn)) {
923 spin_unlock(&conn->ibc_lock);
925 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
927 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
929 spin_lock(&conn->ibc_lock);
931 kiblnd_queue_tx_locked(tx, conn);
937 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
939 tx = list_entry(conn->ibc_tx_queue_nocred.next,
941 } else if (!list_empty(&conn->ibc_tx_noops)) {
942 LASSERT (!IBLND_OOB_CAPABLE(ver));
944 tx = list_entry(conn->ibc_tx_noops.next,
946 } else if (!list_empty(&conn->ibc_tx_queue)) {
948 tx = list_entry(conn->ibc_tx_queue.next,
953 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
959 kiblnd_tx_complete (kib_tx_t *tx, int status)
961 int failed = (status != IB_WC_SUCCESS);
962 kib_conn_t *conn = tx->tx_conn;
965 LASSERT (tx->tx_sending > 0);
968 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
969 CNETERR("Tx -> %s cookie %#llx"
970 " sending %d waiting %d: failed %d\n",
971 libcfs_nid2str(conn->ibc_peer->ibp_nid),
972 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
975 kiblnd_close_conn(conn, -EIO);
977 kiblnd_peer_alive(conn->ibc_peer);
980 spin_lock(&conn->ibc_lock);
982 /* I could be racing with rdma completion. Whoever makes 'tx' idle
983 * gets to free it, which also drops its ref on 'conn'. */
986 conn->ibc_nsends_posted--;
987 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
988 conn->ibc_noops_posted--;
991 tx->tx_waiting = 0; /* don't wait for peer_ni */
992 tx->tx_status = -EIO;
995 idle = (tx->tx_sending == 0) && /* This is the final callback */
996 !tx->tx_waiting && /* Not waiting for peer_ni */
997 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
999 list_del(&tx->tx_list);
1001 kiblnd_check_sends_locked(conn);
1002 spin_unlock(&conn->ibc_lock);
1009 kiblnd_init_tx_msg(struct lnet_ni *ni, kib_tx_t *tx, int type, int body_nob)
1011 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
1012 struct ib_sge *sge = &tx->tx_msgsge;
1013 struct ib_rdma_wr *wrq;
1014 int nob = offsetof(kib_msg_t, ibm_u) + body_nob;
1015 #ifdef HAVE_IB_GET_DMA_MR
1016 struct ib_mr *mr = hdev->ibh_mrs;
1019 LASSERT(tx->tx_nwrq >= 0);
1020 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1021 LASSERT(nob <= IBLND_MSG_SIZE);
1022 #ifdef HAVE_IB_GET_DMA_MR
1023 LASSERT(mr != NULL);
1026 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1028 #ifdef HAVE_IB_GET_DMA_MR
1029 sge->lkey = mr->lkey;
1031 sge->lkey = hdev->ibh_pd->local_dma_lkey;
1033 sge->addr = tx->tx_msgaddr;
1036 wrq = &tx->tx_wrq[tx->tx_nwrq];
1037 memset(wrq, 0, sizeof(*wrq));
1039 wrq->wr.next = NULL;
1040 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1041 wrq->wr.sg_list = sge;
1042 wrq->wr.num_sge = 1;
1043 wrq->wr.opcode = IB_WR_SEND;
1044 wrq->wr.send_flags = IB_SEND_SIGNALED;
1050 kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
1051 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1053 kib_msg_t *ibmsg = tx->tx_msg;
1054 kib_rdma_desc_t *srcrd = tx->tx_rd;
1055 struct ib_rdma_wr *wrq = NULL;
1063 LASSERT(!in_interrupt());
1064 LASSERT(tx->tx_nwrq == 0 && tx->tx_nsge == 0);
1065 LASSERT(type == IBLND_MSG_GET_DONE || type == IBLND_MSG_PUT_DONE);
1067 for (srcidx = dstidx = wrq_sge = sge_nob = 0;
1068 resid > 0; resid -= sge_nob) {
1071 if (srcidx >= srcrd->rd_nfrags) {
1072 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1077 if (dstidx >= dstrd->rd_nfrags) {
1078 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1083 if (tx->tx_nwrq >= conn->ibc_max_frags) {
1084 CERROR("RDMA has too many fragments for peer_ni %s (%d), "
1085 "src idx/frags: %d/%d dst idx/frags: %d/%d\n",
1086 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1087 conn->ibc_max_frags,
1088 srcidx, srcrd->rd_nfrags,
1089 dstidx, dstrd->rd_nfrags);
1094 sge_nob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1095 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1097 sge = &tx->tx_sge[tx->tx_nsge];
1098 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1099 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1100 sge->length = sge_nob;
1103 wrq = &tx->tx_wrq[tx->tx_nwrq];
1105 wrq->wr.next = &(wrq + 1)->wr;
1106 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1107 wrq->wr.sg_list = sge;
1108 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1109 wrq->wr.send_flags = 0;
1111 #ifdef HAVE_IB_RDMA_WR
1112 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd,
1114 wrq->rkey = kiblnd_rd_frag_key(dstrd,
1117 wrq->wr.wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd,
1119 wrq->wr.wr.rdma.rkey = kiblnd_rd_frag_key(dstrd,
1124 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, sge_nob);
1125 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, sge_nob);
1128 if (wrq_sge == *kiblnd_tunables.kib_wrq_sge || dstidx != prev) {
1130 wrq->wr.num_sge = wrq_sge;
1136 if (rc < 0) /* no RDMA if completing with failure */
1137 tx->tx_nwrq = tx->tx_nsge = 0;
1139 ibmsg->ibm_u.completion.ibcm_status = rc;
1140 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1141 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1142 type, sizeof (kib_completion_msg_t));
1148 kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn)
1150 struct list_head *q;
1152 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1153 LASSERT(!tx->tx_queued); /* not queued for sending already */
1154 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1157 tx->tx_deadline = jiffies +
1158 msecs_to_jiffies(*kiblnd_tunables.kib_timeout *
1161 if (tx->tx_conn == NULL) {
1162 kiblnd_conn_addref(conn);
1164 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1166 /* PUT_DONE first attached to conn as a PUT_REQ */
1167 LASSERT (tx->tx_conn == conn);
1168 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1171 switch (tx->tx_msg->ibm_type) {
1175 case IBLND_MSG_PUT_REQ:
1176 case IBLND_MSG_GET_REQ:
1177 q = &conn->ibc_tx_queue_rsrvd;
1180 case IBLND_MSG_PUT_NAK:
1181 case IBLND_MSG_PUT_ACK:
1182 case IBLND_MSG_PUT_DONE:
1183 case IBLND_MSG_GET_DONE:
1184 q = &conn->ibc_tx_queue_nocred;
1187 case IBLND_MSG_NOOP:
1188 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1189 q = &conn->ibc_tx_queue_nocred;
1191 q = &conn->ibc_tx_noops;
1194 case IBLND_MSG_IMMEDIATE:
1195 q = &conn->ibc_tx_queue;
1199 list_add_tail(&tx->tx_list, q);
1203 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1205 spin_lock(&conn->ibc_lock);
1206 kiblnd_queue_tx_locked(tx, conn);
1207 kiblnd_check_sends_locked(conn);
1208 spin_unlock(&conn->ibc_lock);
1211 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1212 struct sockaddr_in *srcaddr,
1213 struct sockaddr_in *dstaddr,
1216 unsigned short port;
1219 /* allow the port to be reused */
1220 rc = rdma_set_reuseaddr(cmid, 1);
1222 CERROR("Unable to set reuse on cmid: %d\n", rc);
1226 /* look for a free privileged port */
1227 for (port = PROT_SOCK-1; port > 0; port--) {
1228 srcaddr->sin_port = htons(port);
1229 rc = rdma_resolve_addr(cmid,
1230 (struct sockaddr *)srcaddr,
1231 (struct sockaddr *)dstaddr,
1234 CDEBUG(D_NET, "bound to port %hu\n", port);
1236 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1237 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1244 CERROR("Failed to bind to a free privileged port\n");
1249 kiblnd_connect_peer (kib_peer_ni_t *peer_ni)
1251 struct rdma_cm_id *cmid;
1253 kib_net_t *net = peer_ni->ibp_ni->ni_data;
1254 struct sockaddr_in srcaddr;
1255 struct sockaddr_in dstaddr;
1258 LASSERT (net != NULL);
1259 LASSERT (peer_ni->ibp_connecting > 0);
1261 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer_ni, RDMA_PS_TCP,
1265 CERROR("Can't create CMID for %s: %ld\n",
1266 libcfs_nid2str(peer_ni->ibp_nid), PTR_ERR(cmid));
1272 memset(&srcaddr, 0, sizeof(srcaddr));
1273 srcaddr.sin_family = AF_INET;
1274 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1276 memset(&dstaddr, 0, sizeof(dstaddr));
1277 dstaddr.sin_family = AF_INET;
1278 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1279 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer_ni->ibp_nid));
1281 kiblnd_peer_addref(peer_ni); /* cmid's ref */
1283 if (*kiblnd_tunables.kib_use_priv_port) {
1284 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1285 *kiblnd_tunables.kib_timeout * 1000);
1287 rc = rdma_resolve_addr(cmid,
1288 (struct sockaddr *)&srcaddr,
1289 (struct sockaddr *)&dstaddr,
1290 *kiblnd_tunables.kib_timeout * 1000);
1293 /* Can't initiate address resolution: */
1294 CERROR("Can't resolve addr for %s: %d\n",
1295 libcfs_nid2str(peer_ni->ibp_nid), rc);
1302 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1303 kiblnd_peer_decref(peer_ni); /* cmid's ref */
1304 rdma_destroy_id(cmid);
1307 kiblnd_peer_connect_failed(peer_ni, 1, rc);
1311 kiblnd_reconnect_peer(kib_peer_ni_t *peer_ni)
1313 rwlock_t *glock = &kiblnd_data.kib_global_lock;
1314 char *reason = NULL;
1315 struct list_head txs;
1316 unsigned long flags;
1318 INIT_LIST_HEAD(&txs);
1320 write_lock_irqsave(glock, flags);
1321 if (peer_ni->ibp_reconnecting == 0) {
1322 if (peer_ni->ibp_accepting)
1323 reason = "accepting";
1324 else if (peer_ni->ibp_connecting)
1325 reason = "connecting";
1326 else if (!list_empty(&peer_ni->ibp_conns))
1327 reason = "connected";
1328 else /* connected then closed */
1334 if (peer_ni->ibp_accepting)
1335 CNETERR("Detecting race between accepting and reconnecting\n");
1336 peer_ni->ibp_reconnecting--;
1338 if (!kiblnd_peer_active(peer_ni)) {
1339 list_splice_init(&peer_ni->ibp_tx_queue, &txs);
1340 reason = "unlinked";
1344 peer_ni->ibp_connecting++;
1345 peer_ni->ibp_reconnected++;
1347 write_unlock_irqrestore(glock, flags);
1349 kiblnd_connect_peer(peer_ni);
1353 write_unlock_irqrestore(glock, flags);
1355 CWARN("Abort reconnection of %s: %s\n",
1356 libcfs_nid2str(peer_ni->ibp_nid), reason);
1357 kiblnd_txlist_done(&txs, -ECONNABORTED);
1362 kiblnd_launch_tx(struct lnet_ni *ni, kib_tx_t *tx, lnet_nid_t nid)
1364 kib_peer_ni_t *peer_ni;
1365 kib_peer_ni_t *peer2;
1367 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1368 unsigned long flags;
1371 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
1373 /* If I get here, I've committed to send, so I complete the tx with
1374 * failure on any problems */
1376 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1377 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1379 /* First time, just use a read lock since I expect to find my peer_ni
1381 read_lock_irqsave(g_lock, flags);
1383 peer_ni = kiblnd_find_peer_locked(ni, nid);
1384 if (peer_ni != NULL && !list_empty(&peer_ni->ibp_conns)) {
1385 /* Found a peer_ni with an established connection */
1386 conn = kiblnd_get_conn_locked(peer_ni);
1387 kiblnd_conn_addref(conn); /* 1 ref for me... */
1389 read_unlock_irqrestore(g_lock, flags);
1392 kiblnd_queue_tx(tx, conn);
1393 kiblnd_conn_decref(conn); /* ...to here */
1397 read_unlock(g_lock);
1398 /* Re-try with a write lock */
1401 peer_ni = kiblnd_find_peer_locked(ni, nid);
1402 if (peer_ni != NULL) {
1403 if (list_empty(&peer_ni->ibp_conns)) {
1404 /* found a peer_ni, but it's still connecting... */
1405 LASSERT(kiblnd_peer_connecting(peer_ni));
1407 list_add_tail(&tx->tx_list,
1408 &peer_ni->ibp_tx_queue);
1409 write_unlock_irqrestore(g_lock, flags);
1411 conn = kiblnd_get_conn_locked(peer_ni);
1412 kiblnd_conn_addref(conn); /* 1 ref for me... */
1414 write_unlock_irqrestore(g_lock, flags);
1417 kiblnd_queue_tx(tx, conn);
1418 kiblnd_conn_decref(conn); /* ...to here */
1423 write_unlock_irqrestore(g_lock, flags);
1425 /* Allocate a peer_ni ready to add to the peer_ni table and retry */
1426 rc = kiblnd_create_peer(ni, &peer_ni, nid);
1428 CERROR("Can't create peer_ni %s\n", libcfs_nid2str(nid));
1430 tx->tx_status = -EHOSTUNREACH;
1437 write_lock_irqsave(g_lock, flags);
1439 peer2 = kiblnd_find_peer_locked(ni, nid);
1440 if (peer2 != NULL) {
1441 if (list_empty(&peer2->ibp_conns)) {
1442 /* found a peer_ni, but it's still connecting... */
1443 LASSERT(kiblnd_peer_connecting(peer2));
1445 list_add_tail(&tx->tx_list,
1446 &peer2->ibp_tx_queue);
1447 write_unlock_irqrestore(g_lock, flags);
1449 conn = kiblnd_get_conn_locked(peer2);
1450 kiblnd_conn_addref(conn); /* 1 ref for me... */
1452 write_unlock_irqrestore(g_lock, flags);
1455 kiblnd_queue_tx(tx, conn);
1456 kiblnd_conn_decref(conn); /* ...to here */
1459 kiblnd_peer_decref(peer_ni);
1463 /* Brand new peer_ni */
1464 LASSERT(peer_ni->ibp_connecting == 0);
1465 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
1466 peer_ni->ibp_connecting = tunables->lnd_conns_per_peer;
1468 /* always called with a ref on ni, which prevents ni being shutdown */
1469 LASSERT(((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1472 list_add_tail(&tx->tx_list, &peer_ni->ibp_tx_queue);
1474 kiblnd_peer_addref(peer_ni);
1475 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
1477 write_unlock_irqrestore(g_lock, flags);
1479 for (i = 0; i < tunables->lnd_conns_per_peer; i++)
1480 kiblnd_connect_peer(peer_ni);
1481 kiblnd_peer_decref(peer_ni);
1485 kiblnd_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
1487 struct lnet_hdr *hdr = &lntmsg->msg_hdr;
1488 int type = lntmsg->msg_type;
1489 struct lnet_process_id target = lntmsg->msg_target;
1490 int target_is_router = lntmsg->msg_target_is_router;
1491 int routing = lntmsg->msg_routing;
1492 unsigned int payload_niov = lntmsg->msg_niov;
1493 struct kvec *payload_iov = lntmsg->msg_iov;
1494 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1495 unsigned int payload_offset = lntmsg->msg_offset;
1496 unsigned int payload_nob = lntmsg->msg_len;
1498 kib_rdma_desc_t *rd;
1503 /* NB 'private' is different depending on what we're sending.... */
1505 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1506 payload_nob, payload_niov, libcfs_id2str(target));
1508 LASSERT (payload_nob == 0 || payload_niov > 0);
1509 LASSERT (payload_niov <= LNET_MAX_IOV);
1511 /* Thread context */
1512 LASSERT (!in_interrupt());
1513 /* payload is either all vaddrs or all pages */
1514 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1522 LASSERT (payload_nob == 0);
1526 if (routing || target_is_router)
1527 break; /* send IMMEDIATE */
1529 /* is the REPLY message too small for RDMA? */
1530 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1531 if (nob <= IBLND_MSG_SIZE)
1532 break; /* send IMMEDIATE */
1534 tx = kiblnd_get_idle_tx(ni, target.nid);
1536 CERROR("Can't allocate txd for GET to %s\n",
1537 libcfs_nid2str(target.nid));
1542 rd = &ibmsg->ibm_u.get.ibgm_rd;
1543 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1544 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1545 lntmsg->msg_md->md_niov,
1546 lntmsg->msg_md->md_iov.iov,
1547 0, lntmsg->msg_md->md_length);
1549 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1550 lntmsg->msg_md->md_niov,
1551 lntmsg->msg_md->md_iov.kiov,
1552 0, lntmsg->msg_md->md_length);
1554 CERROR("Can't setup GET sink for %s: %d\n",
1555 libcfs_nid2str(target.nid), rc);
1560 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[rd->rd_nfrags]);
1561 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1562 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1564 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1566 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1567 if (tx->tx_lntmsg[1] == NULL) {
1568 CERROR("Can't create reply for GET -> %s\n",
1569 libcfs_nid2str(target.nid));
1574 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1575 tx->tx_waiting = 1; /* waiting for GET_DONE */
1576 kiblnd_launch_tx(ni, tx, target.nid);
1579 case LNET_MSG_REPLY:
1581 /* Is the payload small enough not to need RDMA? */
1582 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1583 if (nob <= IBLND_MSG_SIZE)
1584 break; /* send IMMEDIATE */
1586 tx = kiblnd_get_idle_tx(ni, target.nid);
1588 CERROR("Can't allocate %s txd for %s\n",
1589 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1590 libcfs_nid2str(target.nid));
1594 if (payload_kiov == NULL)
1595 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1596 payload_niov, payload_iov,
1597 payload_offset, payload_nob);
1599 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1600 payload_niov, payload_kiov,
1601 payload_offset, payload_nob);
1603 CERROR("Can't setup PUT src for %s: %d\n",
1604 libcfs_nid2str(target.nid), rc);
1610 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1611 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1612 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1614 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1615 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1616 kiblnd_launch_tx(ni, tx, target.nid);
1620 /* send IMMEDIATE */
1622 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1625 tx = kiblnd_get_idle_tx(ni, target.nid);
1627 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1628 type, libcfs_nid2str(target.nid));
1633 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1635 if (payload_kiov != NULL)
1636 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1637 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1638 payload_niov, payload_kiov,
1639 payload_offset, payload_nob);
1641 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1642 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1643 payload_niov, payload_iov,
1644 payload_offset, payload_nob);
1646 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1647 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1649 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1650 kiblnd_launch_tx(ni, tx, target.nid);
1655 kiblnd_reply(struct lnet_ni *ni, kib_rx_t *rx, struct lnet_msg *lntmsg)
1657 struct lnet_process_id target = lntmsg->msg_target;
1658 unsigned int niov = lntmsg->msg_niov;
1659 struct kvec *iov = lntmsg->msg_iov;
1660 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1661 unsigned int offset = lntmsg->msg_offset;
1662 unsigned int nob = lntmsg->msg_len;
1666 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1668 CERROR("Can't get tx for REPLY to %s\n",
1669 libcfs_nid2str(target.nid));
1675 else if (kiov == NULL)
1676 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1677 niov, iov, offset, nob);
1679 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1680 niov, kiov, offset, nob);
1683 CERROR("Can't setup GET src for %s: %d\n",
1684 libcfs_nid2str(target.nid), rc);
1688 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1689 IBLND_MSG_GET_DONE, nob,
1690 &rx->rx_msg->ibm_u.get.ibgm_rd,
1691 rx->rx_msg->ibm_u.get.ibgm_cookie);
1693 CERROR("Can't setup rdma for GET from %s: %d\n",
1694 libcfs_nid2str(target.nid), rc);
1699 /* No RDMA: local completion may happen now! */
1700 lnet_finalize(lntmsg, 0);
1702 /* RDMA: lnet_finalize(lntmsg) when it
1704 tx->tx_lntmsg[0] = lntmsg;
1707 kiblnd_queue_tx(tx, rx->rx_conn);
1713 lnet_finalize(lntmsg, -EIO);
1717 kiblnd_recv(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg,
1718 int delayed, unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1719 unsigned int offset, unsigned int mlen, unsigned int rlen)
1721 kib_rx_t *rx = private;
1722 kib_msg_t *rxmsg = rx->rx_msg;
1723 kib_conn_t *conn = rx->rx_conn;
1727 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1730 LASSERT (mlen <= rlen);
1731 LASSERT (!in_interrupt());
1732 /* Either all pages or all vaddrs */
1733 LASSERT (!(kiov != NULL && iov != NULL));
1735 switch (rxmsg->ibm_type) {
1739 case IBLND_MSG_IMMEDIATE:
1740 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1741 if (nob > rx->rx_nob) {
1742 CERROR ("Immediate message from %s too big: %d(%d)\n",
1743 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1750 lnet_copy_flat2kiov(niov, kiov, offset,
1751 IBLND_MSG_SIZE, rxmsg,
1752 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1755 lnet_copy_flat2iov(niov, iov, offset,
1756 IBLND_MSG_SIZE, rxmsg,
1757 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1759 lnet_finalize(lntmsg, 0);
1762 case IBLND_MSG_PUT_REQ: {
1764 kib_rdma_desc_t *rd;
1765 ibprm_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1768 lnet_finalize(lntmsg, 0);
1769 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1774 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1776 CERROR("Can't allocate tx for %s\n",
1777 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1778 /* Not replying will break the connection */
1784 rd = &txmsg->ibm_u.putack.ibpam_rd;
1786 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1787 niov, iov, offset, mlen);
1789 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1790 niov, kiov, offset, mlen);
1792 CERROR("Can't setup PUT sink for %s: %d\n",
1793 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1795 /* tell peer_ni it's over */
1796 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK,
1801 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[rd->rd_nfrags]);
1802 txmsg->ibm_u.putack.ibpam_src_cookie = ibprm_cookie;
1803 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1805 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1807 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1808 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1809 kiblnd_queue_tx(tx, conn);
1811 /* reposted buffer reserved for PUT_DONE */
1812 post_credit = IBLND_POSTRX_NO_CREDIT;
1816 case IBLND_MSG_GET_REQ:
1817 if (lntmsg != NULL) {
1818 /* Optimized GET; RDMA lntmsg's payload */
1819 kiblnd_reply(ni, rx, lntmsg);
1821 /* GET didn't match anything */
1822 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1824 rxmsg->ibm_u.get.ibgm_cookie);
1829 kiblnd_post_rx(rx, post_credit);
1834 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1836 struct task_struct *task = kthread_run(fn, arg, name);
1839 return PTR_ERR(task);
1841 atomic_inc(&kiblnd_data.kib_nthreads);
1846 kiblnd_thread_fini (void)
1848 atomic_dec (&kiblnd_data.kib_nthreads);
1852 kiblnd_peer_alive (kib_peer_ni_t *peer_ni)
1854 /* This is racy, but everyone's only writing cfs_time_current() */
1855 peer_ni->ibp_last_alive = cfs_time_current();
1860 kiblnd_peer_notify (kib_peer_ni_t *peer_ni)
1863 cfs_time_t last_alive = 0;
1864 unsigned long flags;
1866 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1868 if (kiblnd_peer_idle(peer_ni) && peer_ni->ibp_error != 0) {
1869 error = peer_ni->ibp_error;
1870 peer_ni->ibp_error = 0;
1872 last_alive = peer_ni->ibp_last_alive;
1875 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1878 lnet_notify(peer_ni->ibp_ni,
1879 peer_ni->ibp_nid, 0, last_alive);
1883 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1885 /* This just does the immediate housekeeping. 'error' is zero for a
1886 * normal shutdown which can happen only after the connection has been
1887 * established. If the connection is established, schedule the
1888 * connection to be finished off by the connd. Otherwise the connd is
1889 * already dealing with it (either to set it up or tear it down).
1890 * Caller holds kib_global_lock exclusively in irq context */
1891 kib_peer_ni_t *peer_ni = conn->ibc_peer;
1893 unsigned long flags;
1895 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1897 if (error != 0 && conn->ibc_comms_error == 0)
1898 conn->ibc_comms_error = error;
1900 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1901 return; /* already being handled */
1904 list_empty(&conn->ibc_tx_noops) &&
1905 list_empty(&conn->ibc_tx_queue) &&
1906 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1907 list_empty(&conn->ibc_tx_queue_nocred) &&
1908 list_empty(&conn->ibc_active_txs)) {
1909 CDEBUG(D_NET, "closing conn to %s\n",
1910 libcfs_nid2str(peer_ni->ibp_nid));
1912 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1913 libcfs_nid2str(peer_ni->ibp_nid), error,
1914 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1915 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1916 list_empty(&conn->ibc_tx_queue_rsrvd) ?
1917 "" : "(sending_rsrvd)",
1918 list_empty(&conn->ibc_tx_queue_nocred) ?
1919 "" : "(sending_nocred)",
1920 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1923 dev = ((kib_net_t *)peer_ni->ibp_ni->ni_data)->ibn_dev;
1924 if (peer_ni->ibp_next_conn == conn)
1925 /* clear next_conn so it won't be used */
1926 peer_ni->ibp_next_conn = NULL;
1927 list_del(&conn->ibc_list);
1928 /* connd (see below) takes over ibc_list's ref */
1930 if (list_empty(&peer_ni->ibp_conns) && /* no more conns */
1931 kiblnd_peer_active(peer_ni)) { /* still in peer_ni table */
1932 kiblnd_unlink_peer_locked(peer_ni);
1934 /* set/clear error on last conn */
1935 peer_ni->ibp_error = conn->ibc_comms_error;
1938 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1941 kiblnd_dev_can_failover(dev)) {
1942 list_add_tail(&dev->ibd_fail_list,
1943 &kiblnd_data.kib_failed_devs);
1944 wake_up(&kiblnd_data.kib_failover_waitq);
1947 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1949 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1950 wake_up(&kiblnd_data.kib_connd_waitq);
1952 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1956 kiblnd_close_conn(kib_conn_t *conn, int error)
1958 unsigned long flags;
1960 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1962 kiblnd_close_conn_locked(conn, error);
1964 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1968 kiblnd_handle_early_rxs(kib_conn_t *conn)
1970 unsigned long flags;
1973 LASSERT(!in_interrupt());
1974 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1976 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1977 while (!list_empty(&conn->ibc_early_rxs)) {
1978 rx = list_entry(conn->ibc_early_rxs.next,
1980 list_del(&rx->rx_list);
1981 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1983 kiblnd_handle_rx(rx);
1985 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1987 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1991 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1993 struct list_head zombies = LIST_HEAD_INIT(zombies);
1994 struct list_head *tmp;
1995 struct list_head *nxt;
1998 spin_lock(&conn->ibc_lock);
2000 list_for_each_safe(tmp, nxt, txs) {
2001 tx = list_entry(tmp, kib_tx_t, tx_list);
2003 if (txs == &conn->ibc_active_txs) {
2004 LASSERT(!tx->tx_queued);
2005 LASSERT(tx->tx_waiting ||
2006 tx->tx_sending != 0);
2008 LASSERT(tx->tx_queued);
2011 tx->tx_status = -ECONNABORTED;
2014 if (tx->tx_sending == 0) {
2016 list_del(&tx->tx_list);
2017 list_add(&tx->tx_list, &zombies);
2021 spin_unlock(&conn->ibc_lock);
2023 kiblnd_txlist_done(&zombies, -ECONNABORTED);
2027 kiblnd_finalise_conn (kib_conn_t *conn)
2029 LASSERT (!in_interrupt());
2030 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2032 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2034 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2035 * for connections that didn't get as far as being connected, because
2036 * rdma_disconnect() does this for free. */
2037 kiblnd_abort_receives(conn);
2039 /* Complete all tx descs not waiting for sends to complete.
2040 * NB we should be safe from RDMA now that the QP has changed state */
2042 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2043 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2044 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2045 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2046 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2048 kiblnd_handle_early_rxs(conn);
2052 kiblnd_peer_connect_failed(kib_peer_ni_t *peer_ni, int active, int error)
2054 struct list_head zombies = LIST_HEAD_INIT(zombies);
2055 unsigned long flags;
2057 LASSERT (error != 0);
2058 LASSERT (!in_interrupt());
2060 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2063 LASSERT(peer_ni->ibp_connecting > 0);
2064 peer_ni->ibp_connecting--;
2066 LASSERT (peer_ni->ibp_accepting > 0);
2067 peer_ni->ibp_accepting--;
2070 if (kiblnd_peer_connecting(peer_ni)) {
2071 /* another connection attempt under way... */
2072 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2077 peer_ni->ibp_reconnected = 0;
2078 if (list_empty(&peer_ni->ibp_conns)) {
2079 /* Take peer_ni's blocked transmits to complete with error */
2080 list_add(&zombies, &peer_ni->ibp_tx_queue);
2081 list_del_init(&peer_ni->ibp_tx_queue);
2083 if (kiblnd_peer_active(peer_ni))
2084 kiblnd_unlink_peer_locked(peer_ni);
2086 peer_ni->ibp_error = error;
2088 /* Can't have blocked transmits if there are connections */
2089 LASSERT(list_empty(&peer_ni->ibp_tx_queue));
2092 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2094 kiblnd_peer_notify(peer_ni);
2096 if (list_empty(&zombies))
2099 CNETERR("Deleting messages for %s: connection failed\n",
2100 libcfs_nid2str(peer_ni->ibp_nid));
2102 kiblnd_txlist_done(&zombies, -EHOSTUNREACH);
2106 kiblnd_connreq_done(kib_conn_t *conn, int status)
2108 kib_peer_ni_t *peer_ni = conn->ibc_peer;
2110 struct list_head txs;
2111 unsigned long flags;
2114 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2116 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2117 libcfs_nid2str(peer_ni->ibp_nid), active,
2118 conn->ibc_version, status);
2120 LASSERT (!in_interrupt());
2121 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2122 peer_ni->ibp_connecting > 0) ||
2123 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2124 peer_ni->ibp_accepting > 0));
2126 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2127 conn->ibc_connvars = NULL;
2130 /* failed to establish connection */
2131 kiblnd_peer_connect_failed(peer_ni, active, status);
2132 kiblnd_finalise_conn(conn);
2136 /* connection established */
2137 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2139 conn->ibc_last_send = jiffies;
2140 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2141 kiblnd_peer_alive(peer_ni);
2143 /* Add conn to peer_ni's list and nuke any dangling conns from a different
2144 * peer_ni instance... */
2145 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2146 list_add(&conn->ibc_list, &peer_ni->ibp_conns);
2147 peer_ni->ibp_reconnected = 0;
2149 peer_ni->ibp_connecting--;
2151 peer_ni->ibp_accepting--;
2153 if (peer_ni->ibp_version == 0) {
2154 peer_ni->ibp_version = conn->ibc_version;
2155 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2158 if (peer_ni->ibp_version != conn->ibc_version ||
2159 peer_ni->ibp_incarnation != conn->ibc_incarnation) {
2160 kiblnd_close_stale_conns_locked(peer_ni, conn->ibc_version,
2161 conn->ibc_incarnation);
2162 peer_ni->ibp_version = conn->ibc_version;
2163 peer_ni->ibp_incarnation = conn->ibc_incarnation;
2166 /* grab pending txs while I have the lock */
2167 list_add(&txs, &peer_ni->ibp_tx_queue);
2168 list_del_init(&peer_ni->ibp_tx_queue);
2170 if (!kiblnd_peer_active(peer_ni) || /* peer_ni has been deleted */
2171 conn->ibc_comms_error != 0) { /* error has happened already */
2173 /* start to shut down connection */
2174 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2175 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2177 kiblnd_txlist_done(&txs, -ECONNABORTED);
2182 /* +1 ref for myself, this connection is visible to other threads
2183 * now, refcount of peer:ibp_conns can be released by connection
2184 * close from either a different thread, or the calling of
2185 * kiblnd_check_sends_locked() below. See bz21911 for details.
2187 kiblnd_conn_addref(conn);
2188 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2190 /* Schedule blocked txs
2191 * Note: if we are running with conns_per_peer > 1, these blocked
2192 * txs will all get scheduled to the first connection which gets
2193 * scheduled. We won't be using round robin on this first batch.
2195 spin_lock(&conn->ibc_lock);
2196 while (!list_empty(&txs)) {
2197 tx = list_entry(txs.next, kib_tx_t, tx_list);
2198 list_del(&tx->tx_list);
2200 kiblnd_queue_tx_locked(tx, conn);
2202 kiblnd_check_sends_locked(conn);
2203 spin_unlock(&conn->ibc_lock);
2205 /* schedule blocked rxs */
2206 kiblnd_handle_early_rxs(conn);
2207 kiblnd_conn_decref(conn);
2211 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2215 rc = rdma_reject(cmid, rej, sizeof(*rej));
2218 CWARN("Error %d sending reject\n", rc);
2222 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2224 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2225 kib_msg_t *reqmsg = priv;
2228 kib_peer_ni_t *peer_ni;
2229 kib_peer_ni_t *peer2;
2231 struct lnet_ni *ni = NULL;
2232 kib_net_t *net = NULL;
2234 struct rdma_conn_param cp;
2236 int version = IBLND_MSG_VERSION;
2237 unsigned long flags;
2239 struct sockaddr_in *peer_addr;
2240 LASSERT (!in_interrupt());
2242 /* cmid inherits 'context' from the corresponding listener id */
2243 ibdev = (kib_dev_t *)cmid->context;
2244 LASSERT (ibdev != NULL);
2246 memset(&rej, 0, sizeof(rej));
2247 rej.ibr_magic = IBLND_MSG_MAGIC;
2248 rej.ibr_why = IBLND_REJECT_FATAL;
2249 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2251 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2252 if (*kiblnd_tunables.kib_require_priv_port &&
2253 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2254 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2255 CERROR("peer_ni's port (%pI4h:%hu) is not privileged\n",
2256 &ip, ntohs(peer_addr->sin_port));
2260 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2261 CERROR("Short connection request\n");
2265 /* Future protocol version compatibility support! If the
2266 * o2iblnd-specific protocol changes, or when LNET unifies
2267 * protocols over all LNDs, the initial connection will
2268 * negotiate a protocol version. I trap this here to avoid
2269 * console errors; the reject tells the peer_ni which protocol I
2271 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2272 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2274 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2275 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2276 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2278 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2279 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2280 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2283 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2285 CERROR("Can't parse connection request: %d\n", rc);
2289 nid = reqmsg->ibm_srcnid;
2290 ni = lnet_nid2ni_addref(reqmsg->ibm_dstnid);
2293 net = (kib_net_t *)ni->ni_data;
2294 rej.ibr_incarnation = net->ibn_incarnation;
2297 if (ni == NULL || /* no matching net */
2298 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2299 net->ibn_dev != ibdev) { /* wrong device */
2300 CERROR("Can't accept conn from %s on %s (%s:%d:%pI4h): "
2301 "bad dst nid %s\n", libcfs_nid2str(nid),
2302 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2303 ibdev->ibd_ifname, ibdev->ibd_nnets,
2305 libcfs_nid2str(reqmsg->ibm_dstnid));
2310 /* check time stamp as soon as possible */
2311 if (reqmsg->ibm_dststamp != 0 &&
2312 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2313 CWARN("Stale connection request\n");
2314 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2318 /* I can accept peer_ni's version */
2319 version = reqmsg->ibm_version;
2321 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2322 CERROR("Unexpected connreq msg type: %x from %s\n",
2323 reqmsg->ibm_type, libcfs_nid2str(nid));
2327 if (reqmsg->ibm_u.connparams.ibcp_queue_depth >
2328 kiblnd_msg_queue_size(version, ni)) {
2329 CERROR("Can't accept conn from %s, queue depth too large: "
2330 " %d (<=%d wanted)\n",
2331 libcfs_nid2str(nid),
2332 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2333 kiblnd_msg_queue_size(version, ni));
2335 if (version == IBLND_MSG_VERSION)
2336 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2341 if (reqmsg->ibm_u.connparams.ibcp_max_frags >
2342 kiblnd_rdma_frags(version, ni)) {
2343 CWARN("Can't accept conn from %s (version %x): "
2344 "max_frags %d too large (%d wanted)\n",
2345 libcfs_nid2str(nid), version,
2346 reqmsg->ibm_u.connparams.ibcp_max_frags,
2347 kiblnd_rdma_frags(version, ni));
2349 if (version >= IBLND_MSG_VERSION)
2350 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2353 } else if (reqmsg->ibm_u.connparams.ibcp_max_frags <
2354 kiblnd_rdma_frags(version, ni) &&
2355 net->ibn_fmr_ps == NULL) {
2356 CWARN("Can't accept conn from %s (version %x): "
2357 "max_frags %d incompatible without FMR pool "
2359 libcfs_nid2str(nid), version,
2360 reqmsg->ibm_u.connparams.ibcp_max_frags,
2361 kiblnd_rdma_frags(version, ni));
2363 if (version == IBLND_MSG_VERSION)
2364 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2369 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2370 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2371 libcfs_nid2str(nid),
2372 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2377 /* assume 'nid' is a new peer_ni; create */
2378 rc = kiblnd_create_peer(ni, &peer_ni, nid);
2380 CERROR("Can't create peer_ni for %s\n", libcfs_nid2str(nid));
2381 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2385 /* We have validated the peer's parameters so use those */
2386 peer_ni->ibp_max_frags = reqmsg->ibm_u.connparams.ibcp_max_frags;
2387 peer_ni->ibp_queue_depth = reqmsg->ibm_u.connparams.ibcp_queue_depth;
2389 write_lock_irqsave(g_lock, flags);
2391 peer2 = kiblnd_find_peer_locked(ni, nid);
2392 if (peer2 != NULL) {
2393 if (peer2->ibp_version == 0) {
2394 peer2->ibp_version = version;
2395 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2398 /* not the guy I've talked with */
2399 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2400 peer2->ibp_version != version) {
2401 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2403 if (kiblnd_peer_active(peer2)) {
2404 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2405 peer2->ibp_version = version;
2407 write_unlock_irqrestore(g_lock, flags);
2409 CWARN("Conn stale %s version %x/%x incarnation %llu/%llu\n",
2410 libcfs_nid2str(nid), peer2->ibp_version, version,
2411 peer2->ibp_incarnation, reqmsg->ibm_srcstamp);
2413 kiblnd_peer_decref(peer_ni);
2414 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2418 /* Tie-break connection race in favour of the higher NID.
2419 * If we keep running into a race condition multiple times,
2420 * we have to assume that the connection attempt with the
2421 * higher NID is stuck in a connecting state and will never
2422 * recover. As such, we pass through this if-block and let
2423 * the lower NID connection win so we can move forward.
2425 if (peer2->ibp_connecting != 0 &&
2426 nid < ni->ni_nid && peer2->ibp_races <
2427 MAX_CONN_RACES_BEFORE_ABORT) {
2429 write_unlock_irqrestore(g_lock, flags);
2431 CDEBUG(D_NET, "Conn race %s\n",
2432 libcfs_nid2str(peer2->ibp_nid));
2434 kiblnd_peer_decref(peer_ni);
2435 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2438 if (peer2->ibp_races >= MAX_CONN_RACES_BEFORE_ABORT)
2439 CNETERR("Conn race %s: unresolved after %d attempts, letting lower NID win\n",
2440 libcfs_nid2str(peer2->ibp_nid),
2441 MAX_CONN_RACES_BEFORE_ABORT);
2443 * passive connection is allowed even this peer_ni is waiting for
2446 peer2->ibp_reconnecting = 0;
2447 peer2->ibp_races = 0;
2448 peer2->ibp_accepting++;
2449 kiblnd_peer_addref(peer2);
2451 /* Race with kiblnd_launch_tx (active connect) to create peer_ni
2452 * so copy validated parameters since we now know what the
2453 * peer_ni's limits are */
2454 peer2->ibp_max_frags = peer_ni->ibp_max_frags;
2455 peer2->ibp_queue_depth = peer_ni->ibp_queue_depth;
2457 write_unlock_irqrestore(g_lock, flags);
2458 kiblnd_peer_decref(peer_ni);
2461 /* Brand new peer_ni */
2462 LASSERT (peer_ni->ibp_accepting == 0);
2463 LASSERT (peer_ni->ibp_version == 0 &&
2464 peer_ni->ibp_incarnation == 0);
2466 peer_ni->ibp_accepting = 1;
2467 peer_ni->ibp_version = version;
2468 peer_ni->ibp_incarnation = reqmsg->ibm_srcstamp;
2470 /* I have a ref on ni that prevents it being shutdown */
2471 LASSERT (net->ibn_shutdown == 0);
2473 kiblnd_peer_addref(peer_ni);
2474 list_add_tail(&peer_ni->ibp_list, kiblnd_nid2peerlist(nid));
2476 write_unlock_irqrestore(g_lock, flags);
2479 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2481 kiblnd_peer_connect_failed(peer_ni, 0, -ENOMEM);
2482 kiblnd_peer_decref(peer_ni);
2483 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2487 /* conn now "owns" cmid, so I return success from here on to ensure the
2488 * CM callback doesn't destroy cmid. */
2489 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2490 conn->ibc_credits = conn->ibc_queue_depth;
2491 conn->ibc_reserved_credits = conn->ibc_queue_depth;
2492 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2493 IBLND_OOB_MSGS(version) <= IBLND_RX_MSGS(conn));
2495 ackmsg = &conn->ibc_connvars->cv_msg;
2496 memset(ackmsg, 0, sizeof(*ackmsg));
2498 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2499 sizeof(ackmsg->ibm_u.connparams));
2500 ackmsg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2501 ackmsg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2502 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2504 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2506 memset(&cp, 0, sizeof(cp));
2507 cp.private_data = ackmsg;
2508 cp.private_data_len = ackmsg->ibm_nob;
2509 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2510 cp.initiator_depth = 0;
2511 cp.flow_control = 1;
2512 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2513 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2515 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2517 rc = rdma_accept(cmid, &cp);
2519 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2520 rej.ibr_version = version;
2521 rej.ibr_why = IBLND_REJECT_FATAL;
2523 kiblnd_reject(cmid, &rej);
2524 kiblnd_connreq_done(conn, rc);
2525 kiblnd_conn_decref(conn);
2533 rej.ibr_cp.ibcp_queue_depth =
2534 kiblnd_msg_queue_size(version, ni);
2535 rej.ibr_cp.ibcp_max_frags = kiblnd_rdma_frags(version, ni);
2539 rej.ibr_version = version;
2540 kiblnd_reject(cmid, &rej);
2542 return -ECONNREFUSED;
2546 kiblnd_check_reconnect(kib_conn_t *conn, int version,
2547 __u64 incarnation, int why, kib_connparams_t *cp)
2549 rwlock_t *glock = &kiblnd_data.kib_global_lock;
2550 kib_peer_ni_t *peer_ni = conn->ibc_peer;
2552 int msg_size = IBLND_MSG_SIZE;
2556 unsigned long flags;
2558 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2559 LASSERT(peer_ni->ibp_connecting > 0); /* 'conn' at least */
2562 msg_size = cp->ibcp_max_msg_size;
2563 frag_num = cp->ibcp_max_frags;
2564 queue_dep = cp->ibcp_queue_depth;
2567 write_lock_irqsave(glock, flags);
2568 /* retry connection if it's still needed and no other connection
2569 * attempts (active or passive) are in progress
2570 * NB: reconnect is still needed even when ibp_tx_queue is
2571 * empty if ibp_version != version because reconnect may be
2572 * initiated by kiblnd_query() */
2573 reconnect = (!list_empty(&peer_ni->ibp_tx_queue) ||
2574 peer_ni->ibp_version != version) &&
2575 peer_ni->ibp_connecting &&
2576 peer_ni->ibp_accepting == 0;
2587 case IBLND_REJECT_RDMA_FRAGS: {
2588 struct lnet_ioctl_config_o2iblnd_tunables *tunables;
2591 reason = "can't negotiate max frags";
2594 tunables = &peer_ni->ibp_ni->ni_lnd_tunables.lnd_tun_u.lnd_o2ib;
2595 if (!tunables->lnd_map_on_demand) {
2596 reason = "map_on_demand must be enabled";
2599 if (conn->ibc_max_frags <= frag_num) {
2600 reason = "unsupported max frags";
2604 peer_ni->ibp_max_frags = frag_num;
2605 reason = "rdma fragments";
2608 case IBLND_REJECT_MSG_QUEUE_SIZE:
2610 reason = "can't negotiate queue depth";
2613 if (conn->ibc_queue_depth <= queue_dep) {
2614 reason = "unsupported queue depth";
2618 peer_ni->ibp_queue_depth = queue_dep;
2619 reason = "queue depth";
2622 case IBLND_REJECT_CONN_STALE:
2626 case IBLND_REJECT_CONN_RACE:
2627 reason = "conn race";
2630 case IBLND_REJECT_CONN_UNCOMPAT:
2631 reason = "version negotiation";
2634 case IBLND_REJECT_INVALID_SRV_ID:
2635 reason = "invalid service id";
2639 conn->ibc_reconnect = 1;
2640 peer_ni->ibp_reconnecting++;
2641 peer_ni->ibp_version = version;
2642 if (incarnation != 0)
2643 peer_ni->ibp_incarnation = incarnation;
2645 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2647 CNETERR("%s: %s (%s), %x, %x, msg_size: %d, queue_depth: %d/%d, max_frags: %d/%d\n",
2648 libcfs_nid2str(peer_ni->ibp_nid),
2649 reconnect ? "reconnect" : "don't reconnect",
2650 reason, IBLND_MSG_VERSION, version, msg_size,
2651 conn->ibc_queue_depth, queue_dep,
2652 conn->ibc_max_frags, frag_num);
2654 * if conn::ibc_reconnect is TRUE, connd will reconnect to the peer_ni
2655 * while destroying the zombie
2660 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2662 kib_peer_ni_t *peer_ni = conn->ibc_peer;
2664 LASSERT (!in_interrupt());
2665 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2668 case IB_CM_REJ_STALE_CONN:
2669 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2670 IBLND_REJECT_CONN_STALE, NULL);
2673 case IB_CM_REJ_INVALID_SERVICE_ID:
2674 kiblnd_check_reconnect(conn, IBLND_MSG_VERSION, 0,
2675 IBLND_REJECT_INVALID_SRV_ID, NULL);
2676 CNETERR("%s rejected: no listener at %d\n",
2677 libcfs_nid2str(peer_ni->ibp_nid),
2678 *kiblnd_tunables.kib_service);
2681 case IB_CM_REJ_CONSUMER_DEFINED:
2682 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2683 kib_rej_t *rej = priv;
2684 kib_connparams_t *cp = NULL;
2686 __u64 incarnation = -1;
2688 /* NB. default incarnation is -1 because:
2689 * a) V1 will ignore dst incarnation in connreq.
2690 * b) V2 will provide incarnation while rejecting me,
2691 * -1 will be overwrote.
2693 * if I try to connect to a V1 peer_ni with V2 protocol,
2694 * it rejected me then upgrade to V2, I have no idea
2695 * about the upgrading and try to reconnect with V1,
2696 * in this case upgraded V2 can find out I'm trying to
2697 * talk to the old guy and reject me(incarnation is -1).
2700 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2701 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2702 __swab32s(&rej->ibr_magic);
2703 __swab16s(&rej->ibr_version);
2707 if (priv_nob >= sizeof(kib_rej_t) &&
2708 rej->ibr_version > IBLND_MSG_VERSION_1) {
2709 /* priv_nob is always 148 in current version
2710 * of OFED, so we still need to check version.
2711 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2715 __swab64s(&rej->ibr_incarnation);
2716 __swab16s(&cp->ibcp_queue_depth);
2717 __swab16s(&cp->ibcp_max_frags);
2718 __swab32s(&cp->ibcp_max_msg_size);
2721 incarnation = rej->ibr_incarnation;
2724 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2725 rej->ibr_magic != LNET_PROTO_MAGIC) {
2726 CERROR("%s rejected: consumer defined fatal error\n",
2727 libcfs_nid2str(peer_ni->ibp_nid));
2731 if (rej->ibr_version != IBLND_MSG_VERSION &&
2732 rej->ibr_version != IBLND_MSG_VERSION_1) {
2733 CERROR("%s rejected: o2iblnd version %x error\n",
2734 libcfs_nid2str(peer_ni->ibp_nid),
2739 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2740 rej->ibr_version == IBLND_MSG_VERSION_1) {
2741 CDEBUG(D_NET, "rejected by old version peer_ni %s: %x\n",
2742 libcfs_nid2str(peer_ni->ibp_nid), rej->ibr_version);
2744 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2745 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2748 switch (rej->ibr_why) {
2749 case IBLND_REJECT_CONN_RACE:
2750 case IBLND_REJECT_CONN_STALE:
2751 case IBLND_REJECT_CONN_UNCOMPAT:
2752 case IBLND_REJECT_MSG_QUEUE_SIZE:
2753 case IBLND_REJECT_RDMA_FRAGS:
2754 kiblnd_check_reconnect(conn, rej->ibr_version,
2755 incarnation, rej->ibr_why, cp);
2758 case IBLND_REJECT_NO_RESOURCES:
2759 CERROR("%s rejected: o2iblnd no resources\n",
2760 libcfs_nid2str(peer_ni->ibp_nid));
2763 case IBLND_REJECT_FATAL:
2764 CERROR("%s rejected: o2iblnd fatal error\n",
2765 libcfs_nid2str(peer_ni->ibp_nid));
2769 CERROR("%s rejected: o2iblnd reason %d\n",
2770 libcfs_nid2str(peer_ni->ibp_nid),
2778 CNETERR("%s rejected: reason %d, size %d\n",
2779 libcfs_nid2str(peer_ni->ibp_nid), reason, priv_nob);
2783 kiblnd_connreq_done(conn, -ECONNREFUSED);
2787 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2789 kib_peer_ni_t *peer_ni = conn->ibc_peer;
2790 struct lnet_ni *ni = peer_ni->ibp_ni;
2791 kib_net_t *net = ni->ni_data;
2792 kib_msg_t *msg = priv;
2793 int ver = conn->ibc_version;
2794 int rc = kiblnd_unpack_msg(msg, priv_nob);
2795 unsigned long flags;
2797 LASSERT (net != NULL);
2800 CERROR("Can't unpack connack from %s: %d\n",
2801 libcfs_nid2str(peer_ni->ibp_nid), rc);
2805 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2806 CERROR("Unexpected message %d from %s\n",
2807 msg->ibm_type, libcfs_nid2str(peer_ni->ibp_nid));
2812 if (ver != msg->ibm_version) {
2813 CERROR("%s replied version %x is different with "
2814 "requested version %x\n",
2815 libcfs_nid2str(peer_ni->ibp_nid), msg->ibm_version, ver);
2820 if (msg->ibm_u.connparams.ibcp_queue_depth >
2821 conn->ibc_queue_depth) {
2822 CERROR("%s has incompatible queue depth %d (<=%d wanted)\n",
2823 libcfs_nid2str(peer_ni->ibp_nid),
2824 msg->ibm_u.connparams.ibcp_queue_depth,
2825 conn->ibc_queue_depth);
2830 if (msg->ibm_u.connparams.ibcp_max_frags >
2831 conn->ibc_max_frags) {
2832 CERROR("%s has incompatible max_frags %d (<=%d wanted)\n",
2833 libcfs_nid2str(peer_ni->ibp_nid),
2834 msg->ibm_u.connparams.ibcp_max_frags,
2835 conn->ibc_max_frags);
2840 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2841 CERROR("%s max message size %d too big (%d max)\n",
2842 libcfs_nid2str(peer_ni->ibp_nid),
2843 msg->ibm_u.connparams.ibcp_max_msg_size,
2849 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2850 if (msg->ibm_dstnid == ni->ni_nid &&
2851 msg->ibm_dststamp == net->ibn_incarnation)
2855 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2858 CERROR("Bad connection reply from %s, rc = %d, "
2859 "version: %x max_frags: %d\n",
2860 libcfs_nid2str(peer_ni->ibp_nid), rc,
2861 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2865 conn->ibc_incarnation = msg->ibm_srcstamp;
2866 conn->ibc_credits = msg->ibm_u.connparams.ibcp_queue_depth;
2867 conn->ibc_reserved_credits = msg->ibm_u.connparams.ibcp_queue_depth;
2868 conn->ibc_queue_depth = msg->ibm_u.connparams.ibcp_queue_depth;
2869 conn->ibc_max_frags = msg->ibm_u.connparams.ibcp_max_frags;
2870 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits +
2871 IBLND_OOB_MSGS(ver) <= IBLND_RX_MSGS(conn));
2873 kiblnd_connreq_done(conn, 0);
2877 /* NB My QP has already established itself, so I handle anything going
2878 * wrong here by setting ibc_comms_error.
2879 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2880 * immediately tears it down. */
2883 conn->ibc_comms_error = rc;
2884 kiblnd_connreq_done(conn, 0);
2888 kiblnd_active_connect (struct rdma_cm_id *cmid)
2890 kib_peer_ni_t *peer_ni = (kib_peer_ni_t *)cmid->context;
2893 struct rdma_conn_param cp;
2896 unsigned long flags;
2899 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2901 incarnation = peer_ni->ibp_incarnation;
2902 version = (peer_ni->ibp_version == 0) ? IBLND_MSG_VERSION :
2903 peer_ni->ibp_version;
2905 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2907 conn = kiblnd_create_conn(peer_ni, cmid, IBLND_CONN_ACTIVE_CONNECT,
2910 kiblnd_peer_connect_failed(peer_ni, 1, -ENOMEM);
2911 kiblnd_peer_decref(peer_ni); /* lose cmid's ref */
2915 /* conn "owns" cmid now, so I return success from here on to ensure the
2916 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2919 msg = &conn->ibc_connvars->cv_msg;
2921 memset(msg, 0, sizeof(*msg));
2922 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2923 msg->ibm_u.connparams.ibcp_queue_depth = conn->ibc_queue_depth;
2924 msg->ibm_u.connparams.ibcp_max_frags = conn->ibc_max_frags;
2925 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2927 kiblnd_pack_msg(peer_ni->ibp_ni, msg, version,
2928 0, peer_ni->ibp_nid, incarnation);
2930 memset(&cp, 0, sizeof(cp));
2931 cp.private_data = msg;
2932 cp.private_data_len = msg->ibm_nob;
2933 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2934 cp.initiator_depth = 0;
2935 cp.flow_control = 1;
2936 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2937 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2939 LASSERT(cmid->context == (void *)conn);
2940 LASSERT(conn->ibc_cmid == cmid);
2942 rc = rdma_connect(cmid, &cp);
2944 CERROR("Can't connect to %s: %d\n",
2945 libcfs_nid2str(peer_ni->ibp_nid), rc);
2946 kiblnd_connreq_done(conn, rc);
2947 kiblnd_conn_decref(conn);
2954 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2956 kib_peer_ni_t *peer_ni;
2960 switch (event->event) {
2962 CERROR("Unexpected event: %d, status: %d\n",
2963 event->event, event->status);
2966 case RDMA_CM_EVENT_CONNECT_REQUEST:
2967 /* destroy cmid on failure */
2968 rc = kiblnd_passive_connect(cmid,
2969 (void *)KIBLND_CONN_PARAM(event),
2970 KIBLND_CONN_PARAM_LEN(event));
2971 CDEBUG(D_NET, "connreq: %d\n", rc);
2974 case RDMA_CM_EVENT_ADDR_ERROR:
2975 peer_ni = (kib_peer_ni_t *)cmid->context;
2976 CNETERR("%s: ADDR ERROR %d\n",
2977 libcfs_nid2str(peer_ni->ibp_nid), event->status);
2978 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
2979 kiblnd_peer_decref(peer_ni);
2980 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2982 case RDMA_CM_EVENT_ADDR_RESOLVED:
2983 peer_ni = (kib_peer_ni_t *)cmid->context;
2985 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2986 libcfs_nid2str(peer_ni->ibp_nid), event->status);
2988 if (event->status != 0) {
2989 CNETERR("Can't resolve address for %s: %d\n",
2990 libcfs_nid2str(peer_ni->ibp_nid), event->status);
2993 rc = rdma_resolve_route(
2994 cmid, *kiblnd_tunables.kib_timeout * 1000);
2996 kib_net_t *net = peer_ni->ibp_ni->ni_data;
2997 kib_dev_t *dev = net->ibn_dev;
2999 CDEBUG(D_NET, "%s: connection bound to "\
3001 libcfs_nid2str(peer_ni->ibp_nid),
3003 &dev->ibd_ifip, cmid->device->name);
3008 /* Can't initiate route resolution */
3009 CERROR("Can't resolve route for %s: %d\n",
3010 libcfs_nid2str(peer_ni->ibp_nid), rc);
3012 kiblnd_peer_connect_failed(peer_ni, 1, rc);
3013 kiblnd_peer_decref(peer_ni);
3014 return rc; /* rc != 0 destroys cmid */
3016 case RDMA_CM_EVENT_ROUTE_ERROR:
3017 peer_ni = (kib_peer_ni_t *)cmid->context;
3018 CNETERR("%s: ROUTE ERROR %d\n",
3019 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3020 kiblnd_peer_connect_failed(peer_ni, 1, -EHOSTUNREACH);
3021 kiblnd_peer_decref(peer_ni);
3022 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
3024 case RDMA_CM_EVENT_ROUTE_RESOLVED:
3025 peer_ni = (kib_peer_ni_t *)cmid->context;
3026 CDEBUG(D_NET,"%s Route resolved: %d\n",
3027 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3029 if (event->status == 0)
3030 return kiblnd_active_connect(cmid);
3032 CNETERR("Can't resolve route for %s: %d\n",
3033 libcfs_nid2str(peer_ni->ibp_nid), event->status);
3034 kiblnd_peer_connect_failed(peer_ni, 1, event->status);
3035 kiblnd_peer_decref(peer_ni);
3036 return event->status; /* rc != 0 destroys cmid */
3038 case RDMA_CM_EVENT_UNREACHABLE:
3039 conn = (kib_conn_t *)cmid->context;
3040 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3041 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3042 CNETERR("%s: UNREACHABLE %d\n",
3043 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3044 kiblnd_connreq_done(conn, -ENETDOWN);
3045 kiblnd_conn_decref(conn);
3048 case RDMA_CM_EVENT_CONNECT_ERROR:
3049 conn = (kib_conn_t *)cmid->context;
3050 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
3051 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
3052 CNETERR("%s: CONNECT ERROR %d\n",
3053 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
3054 kiblnd_connreq_done(conn, -ENOTCONN);
3055 kiblnd_conn_decref(conn);
3058 case RDMA_CM_EVENT_REJECTED:
3059 conn = (kib_conn_t *)cmid->context;
3060 switch (conn->ibc_state) {
3064 case IBLND_CONN_PASSIVE_WAIT:
3065 CERROR ("%s: REJECTED %d\n",
3066 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3068 kiblnd_connreq_done(conn, -ECONNRESET);
3071 case IBLND_CONN_ACTIVE_CONNECT:
3072 kiblnd_rejected(conn, event->status,
3073 (void *)KIBLND_CONN_PARAM(event),
3074 KIBLND_CONN_PARAM_LEN(event));
3077 kiblnd_conn_decref(conn);
3080 case RDMA_CM_EVENT_ESTABLISHED:
3081 conn = (kib_conn_t *)cmid->context;
3082 switch (conn->ibc_state) {
3086 case IBLND_CONN_PASSIVE_WAIT:
3087 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
3088 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3089 kiblnd_connreq_done(conn, 0);
3092 case IBLND_CONN_ACTIVE_CONNECT:
3093 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
3094 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3095 kiblnd_check_connreply(conn,
3096 (void *)KIBLND_CONN_PARAM(event),
3097 KIBLND_CONN_PARAM_LEN(event));
3100 /* net keeps its ref on conn! */
3103 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
3104 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
3107 case RDMA_CM_EVENT_DISCONNECTED:
3108 conn = (kib_conn_t *)cmid->context;
3109 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
3110 CERROR("%s DISCONNECTED\n",
3111 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3112 kiblnd_connreq_done(conn, -ECONNRESET);
3114 kiblnd_close_conn(conn, 0);
3116 kiblnd_conn_decref(conn);
3117 cmid->context = NULL;
3120 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3121 LCONSOLE_ERROR_MSG(0x131,
3122 "Received notification of device removal\n"
3123 "Please shutdown LNET to allow this to proceed\n");
3124 /* Can't remove network from underneath LNET for now, so I have
3128 case RDMA_CM_EVENT_ADDR_CHANGE:
3129 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3135 kiblnd_check_txs_locked(kib_conn_t *conn, struct list_head *txs)
3138 struct list_head *ttmp;
3140 list_for_each(ttmp, txs) {
3141 tx = list_entry(ttmp, kib_tx_t, tx_list);
3143 if (txs != &conn->ibc_active_txs) {
3144 LASSERT(tx->tx_queued);
3146 LASSERT(!tx->tx_queued);
3147 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3150 if (cfs_time_aftereq(jiffies, tx->tx_deadline)) {
3151 CERROR("Timed out tx: %s, %lu seconds\n",
3152 kiblnd_queue2str(conn, txs),
3153 cfs_duration_sec(jiffies - tx->tx_deadline));
3162 kiblnd_conn_timed_out_locked(kib_conn_t *conn)
3164 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3165 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3166 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3167 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3168 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3172 kiblnd_check_conns (int idx)
3174 struct list_head closes = LIST_HEAD_INIT(closes);
3175 struct list_head checksends = LIST_HEAD_INIT(checksends);
3176 struct list_head timedout_txs = LIST_HEAD_INIT(timedout_txs);
3177 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3178 struct list_head *ptmp;
3179 kib_peer_ni_t *peer_ni;
3181 kib_tx_t *tx, *tx_tmp;
3182 struct list_head *ctmp;
3183 unsigned long flags;
3185 /* NB. We expect to have a look at all the peers and not find any
3186 * RDMAs to time out, so we just use a shared lock while we
3188 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3190 list_for_each(ptmp, peers) {
3191 peer_ni = list_entry(ptmp, kib_peer_ni_t, ibp_list);
3193 /* Check tx_deadline */
3194 list_for_each_entry_safe(tx, tx_tmp, &peer_ni->ibp_tx_queue, tx_list) {
3195 if (cfs_time_aftereq(jiffies, tx->tx_deadline)) {
3196 CWARN("Timed out tx for %s: %lu seconds\n",
3197 libcfs_nid2str(peer_ni->ibp_nid),
3198 cfs_duration_sec(jiffies - tx->tx_deadline));
3199 list_move(&tx->tx_list, &timedout_txs);
3203 list_for_each(ctmp, &peer_ni->ibp_conns) {
3207 conn = list_entry(ctmp, kib_conn_t, ibc_list);
3209 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3211 spin_lock(&conn->ibc_lock);
3213 sendnoop = kiblnd_need_noop(conn);
3214 timedout = kiblnd_conn_timed_out_locked(conn);
3215 if (!sendnoop && !timedout) {
3216 spin_unlock(&conn->ibc_lock);
3221 CERROR("Timed out RDMA with %s (%lu): "
3222 "c: %u, oc: %u, rc: %u\n",
3223 libcfs_nid2str(peer_ni->ibp_nid),
3224 cfs_duration_sec(cfs_time_current() -
3225 peer_ni->ibp_last_alive),
3227 conn->ibc_outstanding_credits,
3228 conn->ibc_reserved_credits);
3229 list_add(&conn->ibc_connd_list, &closes);
3231 list_add(&conn->ibc_connd_list, &checksends);
3233 /* +ref for 'closes' or 'checksends' */
3234 kiblnd_conn_addref(conn);
3236 spin_unlock(&conn->ibc_lock);
3240 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3242 if (!list_empty(&timedout_txs))
3243 kiblnd_txlist_done(&timedout_txs, -ETIMEDOUT);
3245 /* Handle timeout by closing the whole
3246 * connection. We can only be sure RDMA activity
3247 * has ceased once the QP has been modified. */
3248 while (!list_empty(&closes)) {
3249 conn = list_entry(closes.next,
3250 kib_conn_t, ibc_connd_list);
3251 list_del(&conn->ibc_connd_list);
3252 kiblnd_close_conn(conn, -ETIMEDOUT);
3253 kiblnd_conn_decref(conn);
3256 /* In case we have enough credits to return via a
3257 * NOOP, but there were no non-blocking tx descs
3258 * free to do it last time... */
3259 while (!list_empty(&checksends)) {
3260 conn = list_entry(checksends.next,
3261 kib_conn_t, ibc_connd_list);
3262 list_del(&conn->ibc_connd_list);
3264 spin_lock(&conn->ibc_lock);
3265 kiblnd_check_sends_locked(conn);
3266 spin_unlock(&conn->ibc_lock);
3268 kiblnd_conn_decref(conn);
3273 kiblnd_disconnect_conn (kib_conn_t *conn)
3275 LASSERT (!in_interrupt());
3276 LASSERT (current == kiblnd_data.kib_connd);
3277 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3279 rdma_disconnect(conn->ibc_cmid);
3280 kiblnd_finalise_conn(conn);
3282 kiblnd_peer_notify(conn->ibc_peer);
3286 * High-water for reconnection to the same peer_ni, reconnection attempt should
3287 * be delayed after trying more than KIB_RECONN_HIGH_RACE.
3289 #define KIB_RECONN_HIGH_RACE 10
3291 * Allow connd to take a break and handle other things after consecutive
3292 * reconnection attemps.
3294 #define KIB_RECONN_BREAK 100
3297 kiblnd_connd (void *arg)
3299 spinlock_t *lock= &kiblnd_data.kib_connd_lock;
3301 unsigned long flags;
3307 unsigned long deadline = jiffies;
3309 cfs_block_allsigs();
3311 init_waitqueue_entry(&wait, current);
3312 kiblnd_data.kib_connd = current;
3314 spin_lock_irqsave(lock, flags);
3316 while (!kiblnd_data.kib_shutdown) {
3321 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3322 kib_peer_ni_t *peer_ni = NULL;
3324 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3325 kib_conn_t, ibc_list);
3326 list_del(&conn->ibc_list);
3327 if (conn->ibc_reconnect) {
3328 peer_ni = conn->ibc_peer;
3329 kiblnd_peer_addref(peer_ni);
3332 spin_unlock_irqrestore(lock, flags);
3335 kiblnd_destroy_conn(conn, !peer_ni);
3337 spin_lock_irqsave(lock, flags);
3341 conn->ibc_peer = peer_ni;
3342 if (peer_ni->ibp_reconnected < KIB_RECONN_HIGH_RACE)
3343 list_add_tail(&conn->ibc_list,
3344 &kiblnd_data.kib_reconn_list);
3346 list_add_tail(&conn->ibc_list,
3347 &kiblnd_data.kib_reconn_wait);
3350 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3351 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3352 kib_conn_t, ibc_list);
3353 list_del(&conn->ibc_list);
3355 spin_unlock_irqrestore(lock, flags);
3358 kiblnd_disconnect_conn(conn);
3359 kiblnd_conn_decref(conn);
3361 spin_lock_irqsave(lock, flags);
3364 while (reconn < KIB_RECONN_BREAK) {
3365 if (kiblnd_data.kib_reconn_sec !=
3366 ktime_get_real_seconds()) {
3367 kiblnd_data.kib_reconn_sec = ktime_get_real_seconds();
3368 list_splice_init(&kiblnd_data.kib_reconn_wait,
3369 &kiblnd_data.kib_reconn_list);
3372 if (list_empty(&kiblnd_data.kib_reconn_list))
3375 conn = list_entry(kiblnd_data.kib_reconn_list.next,
3376 kib_conn_t, ibc_list);
3377 list_del(&conn->ibc_list);
3379 spin_unlock_irqrestore(lock, flags);
3382 reconn += kiblnd_reconnect_peer(conn->ibc_peer);
3383 kiblnd_peer_decref(conn->ibc_peer);
3384 LIBCFS_FREE(conn, sizeof(*conn));
3386 spin_lock_irqsave(lock, flags);
3389 /* careful with the jiffy wrap... */
3390 timeout = (int)(deadline - jiffies);
3394 int chunk = kiblnd_data.kib_peer_hash_size;
3396 spin_unlock_irqrestore(lock, flags);
3399 /* Time to check for RDMA timeouts on a few more
3400 * peers: I do checks every 'p' seconds on a
3401 * proportion of the peer_ni table and I need to check
3402 * every connection 'n' times within a timeout
3403 * interval, to ensure I detect a timeout on any
3404 * connection within (n+1)/n times the timeout
3407 if (*kiblnd_tunables.kib_timeout > n * p)
3408 chunk = (chunk * n * p) /
3409 *kiblnd_tunables.kib_timeout;
3413 for (i = 0; i < chunk; i++) {
3414 kiblnd_check_conns(peer_index);
3415 peer_index = (peer_index + 1) %
3416 kiblnd_data.kib_peer_hash_size;
3419 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3420 spin_lock_irqsave(lock, flags);
3426 /* Nothing to do for 'timeout' */
3427 set_current_state(TASK_INTERRUPTIBLE);
3428 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3429 spin_unlock_irqrestore(lock, flags);
3431 schedule_timeout(timeout);
3433 set_current_state(TASK_RUNNING);
3434 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3435 spin_lock_irqsave(lock, flags);
3438 spin_unlock_irqrestore(lock, flags);
3440 kiblnd_thread_fini();
3445 kiblnd_qp_event(struct ib_event *event, void *arg)
3447 kib_conn_t *conn = arg;
3449 switch (event->event) {
3450 case IB_EVENT_COMM_EST:
3451 CDEBUG(D_NET, "%s established\n",
3452 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3453 /* We received a packet but connection isn't established
3454 * probably handshake packet was lost, so free to
3455 * force make connection established */
3456 rdma_notify(conn->ibc_cmid, IB_EVENT_COMM_EST);
3460 CERROR("%s: Async QP event type %d\n",
3461 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3467 kiblnd_complete (struct ib_wc *wc)
3469 switch (kiblnd_wreqid2type(wc->wr_id)) {
3474 if (wc->status != IB_WC_SUCCESS &&
3475 wc->status != IB_WC_WR_FLUSH_ERR)
3476 CNETERR("FastReg failed: %d\n", wc->status);
3479 case IBLND_WID_RDMA:
3480 /* We only get RDMA completion notification if it fails. All
3481 * subsequent work items, including the final SEND will fail
3482 * too. However we can't print out any more info about the
3483 * failing RDMA because 'tx' might be back on the idle list or
3484 * even reused already if we didn't manage to post all our work
3486 CNETERR("RDMA (tx: %p) failed: %d\n",
3487 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3491 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3495 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3502 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3504 /* NB I'm not allowed to schedule this conn once its refcount has
3505 * reached 0. Since fundamentally I'm racing with scheduler threads
3506 * consuming my CQ I could be called after all completions have
3507 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3508 * and this CQ is about to be destroyed so I NOOP. */
3509 kib_conn_t *conn = (kib_conn_t *)arg;
3510 struct kib_sched_info *sched = conn->ibc_sched;
3511 unsigned long flags;
3513 LASSERT(cq == conn->ibc_cq);
3515 spin_lock_irqsave(&sched->ibs_lock, flags);
3517 conn->ibc_ready = 1;
3519 if (!conn->ibc_scheduled &&
3520 (conn->ibc_nrx > 0 ||
3521 conn->ibc_nsends_posted > 0)) {
3522 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3523 conn->ibc_scheduled = 1;
3524 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3526 if (waitqueue_active(&sched->ibs_waitq))
3527 wake_up(&sched->ibs_waitq);
3530 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3534 kiblnd_cq_event(struct ib_event *event, void *arg)
3536 kib_conn_t *conn = arg;
3538 CERROR("%s: async CQ event type %d\n",
3539 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3543 kiblnd_scheduler(void *arg)
3545 long id = (long)arg;
3546 struct kib_sched_info *sched;
3549 unsigned long flags;
3555 cfs_block_allsigs();
3557 init_waitqueue_entry(&wait, current);
3559 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3561 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3563 CWARN("Unable to bind on CPU partition %d, please verify "
3564 "whether all CPUs are healthy and reload modules if "
3565 "necessary, otherwise your system might under risk of "
3566 "low performance\n", sched->ibs_cpt);
3569 spin_lock_irqsave(&sched->ibs_lock, flags);
3571 while (!kiblnd_data.kib_shutdown) {
3572 if (busy_loops++ >= IBLND_RESCHED) {
3573 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3578 spin_lock_irqsave(&sched->ibs_lock, flags);
3583 if (!list_empty(&sched->ibs_conns)) {
3584 conn = list_entry(sched->ibs_conns.next,
3585 kib_conn_t, ibc_sched_list);
3586 /* take over kib_sched_conns' ref on conn... */
3587 LASSERT(conn->ibc_scheduled);
3588 list_del(&conn->ibc_sched_list);
3589 conn->ibc_ready = 0;
3591 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3593 wc.wr_id = IBLND_WID_INVAL;
3595 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3597 rc = ib_req_notify_cq(conn->ibc_cq,
3600 CWARN("%s: ib_req_notify_cq failed: %d, "
3601 "closing connection\n",
3602 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3603 kiblnd_close_conn(conn, -EIO);
3604 kiblnd_conn_decref(conn);
3605 spin_lock_irqsave(&sched->ibs_lock,
3610 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3613 if (unlikely(rc > 0 && wc.wr_id == IBLND_WID_INVAL)) {
3615 "ib_poll_cq (rc: %d) returned invalid "
3616 "wr_id, opcode %d, status: %d, "
3617 "vendor_err: %d, conn: %s status: %d\n"
3618 "please upgrade firmware and OFED or "
3619 "contact vendor.\n", rc,
3620 wc.opcode, wc.status, wc.vendor_err,
3621 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3627 CWARN("%s: ib_poll_cq failed: %d, "
3628 "closing connection\n",
3629 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3631 kiblnd_close_conn(conn, -EIO);
3632 kiblnd_conn_decref(conn);
3633 spin_lock_irqsave(&sched->ibs_lock, flags);
3637 spin_lock_irqsave(&sched->ibs_lock, flags);
3639 if (rc != 0 || conn->ibc_ready) {
3640 /* There may be another completion waiting; get
3641 * another scheduler to check while I handle
3643 /* +1 ref for sched_conns */
3644 kiblnd_conn_addref(conn);
3645 list_add_tail(&conn->ibc_sched_list,
3647 if (waitqueue_active(&sched->ibs_waitq))
3648 wake_up(&sched->ibs_waitq);
3650 conn->ibc_scheduled = 0;
3654 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3655 kiblnd_complete(&wc);
3657 spin_lock_irqsave(&sched->ibs_lock, flags);
3660 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3667 set_current_state(TASK_INTERRUPTIBLE);
3668 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3669 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3674 remove_wait_queue(&sched->ibs_waitq, &wait);
3675 set_current_state(TASK_RUNNING);
3676 spin_lock_irqsave(&sched->ibs_lock, flags);
3679 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3681 kiblnd_thread_fini();
3686 kiblnd_failover_thread(void *arg)
3688 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3691 unsigned long flags;
3694 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3696 cfs_block_allsigs();
3698 init_waitqueue_entry(&wait, current);
3699 write_lock_irqsave(glock, flags);
3701 while (!kiblnd_data.kib_shutdown) {
3702 int do_failover = 0;
3705 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3707 if (cfs_time_before(cfs_time_current(),
3708 dev->ibd_next_failover))
3715 list_del_init(&dev->ibd_fail_list);
3716 dev->ibd_failover = 1;
3717 write_unlock_irqrestore(glock, flags);
3719 rc = kiblnd_dev_failover(dev);
3721 write_lock_irqsave(glock, flags);
3723 LASSERT (dev->ibd_failover);
3724 dev->ibd_failover = 0;
3725 if (rc >= 0) { /* Device is OK or failover succeed */
3726 dev->ibd_next_failover = cfs_time_shift(3);
3730 /* failed to failover, retry later */
3731 dev->ibd_next_failover =
3732 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3733 if (kiblnd_dev_can_failover(dev)) {
3734 list_add_tail(&dev->ibd_fail_list,
3735 &kiblnd_data.kib_failed_devs);
3741 /* long sleep if no more pending failover */
3742 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3744 set_current_state(TASK_INTERRUPTIBLE);
3745 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3746 write_unlock_irqrestore(glock, flags);
3748 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3749 cfs_time_seconds(1));
3750 set_current_state(TASK_RUNNING);
3751 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3752 write_lock_irqsave(glock, flags);
3754 if (!long_sleep || rc != 0)
3757 /* have a long sleep, routine check all active devices,
3758 * we need checking like this because if there is not active
3759 * connection on the dev and no SEND from local, we may listen
3760 * on wrong HCA for ever while there is a bonding failover */
3761 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3762 if (kiblnd_dev_can_failover(dev)) {
3763 list_add_tail(&dev->ibd_fail_list,
3764 &kiblnd_data.kib_failed_devs);
3769 write_unlock_irqrestore(glock, flags);
3771 kiblnd_thread_fini();