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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/klnds/o2iblnd/o2iblnd_cb.c
38 * Author: Eric Barton <eric@bartonsoftware.com>
43 static void kiblnd_peer_alive(kib_peer_t *peer);
44 static void kiblnd_peer_connect_failed(kib_peer_t *peer, int active, int error);
45 static void kiblnd_check_sends(kib_conn_t *conn);
46 static void kiblnd_init_tx_msg(lnet_ni_t *ni, kib_tx_t *tx,
47 int type, int body_nob);
48 static int kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
49 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie);
50 static void kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn);
51 static void kiblnd_queue_tx(kib_tx_t *tx, kib_conn_t *conn);
52 static void kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx);
55 kiblnd_tx_done(lnet_ni_t *ni, kib_tx_t *tx)
57 lnet_msg_t *lntmsg[2];
58 kib_net_t *net = ni->ni_data;
63 LASSERT(!in_interrupt());
64 LASSERT(!tx->tx_queued); /* mustn't be queued for sending */
65 LASSERT(!tx->tx_sending); /* mustn't be awaiting sent callback */
66 LASSERT(!tx->tx_waiting); /* mustn't be awaiting peer response */
69 kiblnd_unmap_tx(ni, tx);
71 /* tx may have up to 2 lnet msgs to finalise */
72 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
73 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
77 LASSERT(ni == tx->tx_conn->ibc_peer->ibp_ni);
79 kiblnd_conn_decref(tx->tx_conn);
86 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
88 /* delay finalize until my descs have been freed */
89 for (i = 0; i < 2; i++) {
93 lnet_finalize(ni, lntmsg[i], rc);
98 kiblnd_txlist_done(lnet_ni_t *ni, struct list_head *txlist, int status)
102 while (!list_empty(txlist)) {
103 tx = list_entry(txlist->next, kib_tx_t, tx_list);
105 list_del(&tx->tx_list);
108 tx->tx_status = status;
109 kiblnd_tx_done(ni, tx);
114 kiblnd_get_idle_tx(lnet_ni_t *ni, lnet_nid_t target)
116 kib_net_t *net = (kib_net_t *)ni->ni_data;
117 struct list_head *node;
119 kib_tx_poolset_t *tps;
121 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target)];
122 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
125 tx = list_entry(node, kib_tx_t, tx_list);
127 LASSERT(!tx->tx_nwrq);
128 LASSERT(!tx->tx_queued);
129 LASSERT(!tx->tx_sending);
130 LASSERT(!tx->tx_waiting);
131 LASSERT(!tx->tx_status);
132 LASSERT(!tx->tx_conn);
133 LASSERT(!tx->tx_lntmsg[0]);
134 LASSERT(!tx->tx_lntmsg[1]);
135 LASSERT(!tx->tx_nfrags);
141 kiblnd_drop_rx(kib_rx_t *rx)
143 kib_conn_t *conn = rx->rx_conn;
144 struct kib_sched_info *sched = conn->ibc_sched;
147 spin_lock_irqsave(&sched->ibs_lock, flags);
148 LASSERT(conn->ibc_nrx > 0);
150 spin_unlock_irqrestore(&sched->ibs_lock, flags);
152 kiblnd_conn_decref(conn);
156 kiblnd_post_rx(kib_rx_t *rx, int credit)
158 kib_conn_t *conn = rx->rx_conn;
159 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
160 struct ib_recv_wr *bad_wrq = NULL;
165 LASSERT(!in_interrupt());
166 LASSERT(credit == IBLND_POSTRX_NO_CREDIT ||
167 credit == IBLND_POSTRX_PEER_CREDIT ||
168 credit == IBLND_POSTRX_RSRVD_CREDIT);
170 mr = kiblnd_find_dma_mr(conn->ibc_hdev, rx->rx_msgaddr, IBLND_MSG_SIZE);
173 rx->rx_sge.lkey = mr->lkey;
174 rx->rx_sge.addr = rx->rx_msgaddr;
175 rx->rx_sge.length = IBLND_MSG_SIZE;
177 rx->rx_wrq.next = NULL;
178 rx->rx_wrq.sg_list = &rx->rx_sge;
179 rx->rx_wrq.num_sge = 1;
180 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
182 LASSERT(conn->ibc_state >= IBLND_CONN_INIT);
183 LASSERT(rx->rx_nob >= 0); /* not posted */
185 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
186 kiblnd_drop_rx(rx); /* No more posts for this rx */
190 rx->rx_nob = -1; /* flag posted */
192 /* NB: need an extra reference after ib_post_recv because we don't
193 * own this rx (and rx::rx_conn) anymore, LU-5678.
195 kiblnd_conn_addref(conn);
196 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
198 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
199 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
203 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
207 kiblnd_close_conn(conn, rc);
208 kiblnd_drop_rx(rx); /* No more posts for this rx */
212 if (credit == IBLND_POSTRX_NO_CREDIT)
215 spin_lock(&conn->ibc_lock);
216 if (credit == IBLND_POSTRX_PEER_CREDIT)
217 conn->ibc_outstanding_credits++;
219 conn->ibc_reserved_credits++;
220 spin_unlock(&conn->ibc_lock);
222 kiblnd_check_sends(conn);
224 kiblnd_conn_decref(conn);
229 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
231 struct list_head *tmp;
233 list_for_each(tmp, &conn->ibc_active_txs) {
234 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
236 LASSERT(!tx->tx_queued);
237 LASSERT(tx->tx_sending || tx->tx_waiting);
239 if (tx->tx_cookie != cookie)
242 if (tx->tx_waiting &&
243 tx->tx_msg->ibm_type == txtype)
246 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
247 tx->tx_waiting ? "" : "NOT ",
248 tx->tx_msg->ibm_type, txtype);
254 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
257 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
260 spin_lock(&conn->ibc_lock);
262 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
264 spin_unlock(&conn->ibc_lock);
266 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
267 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
268 kiblnd_close_conn(conn, -EPROTO);
272 if (!tx->tx_status) { /* success so far */
273 if (status < 0) /* failed? */
274 tx->tx_status = status;
275 else if (txtype == IBLND_MSG_GET_REQ)
276 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
281 idle = !tx->tx_queued && !tx->tx_sending;
283 list_del(&tx->tx_list);
285 spin_unlock(&conn->ibc_lock);
288 kiblnd_tx_done(ni, tx);
292 kiblnd_send_completion(kib_conn_t *conn, int type, int status, __u64 cookie)
294 lnet_ni_t *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 lnet_ni_t *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 IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
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 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
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 spin_unlock(&conn->ibc_lock);
355 kiblnd_check_sends(conn);
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) /* 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);
422 * CAVEAT EMPTOR: I could be racing with tx_complete, but...
423 * (a) I can overwrite tx_msg since my peer has received it!
424 * (b) tx_waiting set tells tx_complete() it's not done.
426 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
428 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
429 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
430 &msg->ibm_u.putack.ibpam_rd,
431 msg->ibm_u.putack.ibpam_dst_cookie);
433 CERROR("Can't setup rdma for PUT to %s: %d\n",
434 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
436 spin_lock(&conn->ibc_lock);
437 tx->tx_waiting = 0; /* clear waiting and queue atomically */
438 kiblnd_queue_tx_locked(tx, conn);
439 spin_unlock(&conn->ibc_lock);
442 case IBLND_MSG_PUT_DONE:
443 post_credit = IBLND_POSTRX_PEER_CREDIT;
444 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
445 msg->ibm_u.completion.ibcm_status,
446 msg->ibm_u.completion.ibcm_cookie);
449 case IBLND_MSG_GET_REQ:
450 post_credit = IBLND_POSTRX_DONT_POST;
451 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
452 msg->ibm_srcnid, rx, 1);
453 if (rc < 0) /* repost on error */
454 post_credit = IBLND_POSTRX_PEER_CREDIT;
457 case IBLND_MSG_GET_DONE:
458 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
459 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
460 msg->ibm_u.completion.ibcm_status,
461 msg->ibm_u.completion.ibcm_cookie);
465 if (rc < 0) /* protocol error */
466 kiblnd_close_conn(conn, rc);
468 if (post_credit != IBLND_POSTRX_DONT_POST)
469 kiblnd_post_rx(rx, post_credit);
473 kiblnd_rx_complete(kib_rx_t *rx, int status, int nob)
475 kib_msg_t *msg = rx->rx_msg;
476 kib_conn_t *conn = rx->rx_conn;
477 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
478 kib_net_t *net = ni->ni_data;
483 LASSERT(rx->rx_nob < 0); /* was posted */
484 rx->rx_nob = 0; /* isn't now */
486 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
489 if (status != IB_WC_SUCCESS) {
490 CNETERR("Rx from %s failed: %d\n",
491 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
498 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
500 CERROR("Error %d unpacking rx from %s\n",
501 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
505 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
506 msg->ibm_dstnid != ni->ni_nid ||
507 msg->ibm_srcstamp != conn->ibc_incarnation ||
508 msg->ibm_dststamp != net->ibn_incarnation) {
509 CERROR("Stale rx from %s\n",
510 libcfs_nid2str(conn->ibc_peer->ibp_nid));
515 /* set time last known alive */
516 kiblnd_peer_alive(conn->ibc_peer);
518 /* racing with connection establishment/teardown! */
520 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
521 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
524 write_lock_irqsave(g_lock, flags);
525 /* must check holding global lock to eliminate race */
526 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
527 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
528 write_unlock_irqrestore(g_lock, flags);
531 write_unlock_irqrestore(g_lock, flags);
533 kiblnd_handle_rx(rx);
537 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
538 kiblnd_close_conn(conn, err);
540 kiblnd_drop_rx(rx); /* Don't re-post rx. */
544 kiblnd_kvaddr_to_page(unsigned long vaddr)
548 if (is_vmalloc_addr((void *)vaddr)) {
549 page = vmalloc_to_page((void *)vaddr);
553 #ifdef CONFIG_HIGHMEM
554 if (vaddr >= PKMAP_BASE &&
555 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
556 /* No highmem pages only used for bulk (kiov) I/O */
557 CERROR("find page for address in highmem\n");
561 page = virt_to_page(vaddr);
567 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
570 __u64 *pages = tx->tx_pages;
571 kib_fmr_poolset_t *fps;
578 LASSERT(tx->tx_pool);
579 LASSERT(tx->tx_pool->tpo_pool.po_owner);
581 hdev = tx->tx_pool->tpo_hdev;
583 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
584 for (size = 0; size < rd->rd_frags[i].rf_nob;
585 size += hdev->ibh_page_size) {
586 pages[npages++] = (rd->rd_frags[i].rf_addr &
587 hdev->ibh_page_mask) + size;
591 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
593 fps = net->ibn_fmr_ps[cpt];
594 rc = kiblnd_fmr_pool_map(fps, pages, npages, 0, &tx->fmr);
596 CERROR("Can't map %d pages: %d\n", npages, rc);
601 * If rd is not tx_rd, it's going to get sent to a peer, who will need
604 rd->rd_key = (rd != tx->tx_rd) ? tx->fmr.fmr_pfmr->fmr->rkey :
605 tx->fmr.fmr_pfmr->fmr->lkey;
606 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
607 rd->rd_frags[0].rf_nob = nob;
613 static void kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
615 kib_net_t *net = ni->ni_data;
619 if (net->ibn_fmr_ps && tx->fmr.fmr_pfmr) {
620 kiblnd_fmr_pool_unmap(&tx->fmr, tx->tx_status);
621 tx->fmr.fmr_pfmr = NULL;
625 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
626 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
631 static int kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
634 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
635 kib_net_t *net = ni->ni_data;
636 struct ib_mr *mr = NULL;
641 * If rd is not tx_rd, it's going to get sent to a peer and I'm the
644 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
645 tx->tx_nfrags = nfrags;
647 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
648 tx->tx_nfrags, tx->tx_dmadir);
650 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
651 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
652 hdev->ibh_ibdev, &tx->tx_frags[i]);
653 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
654 hdev->ibh_ibdev, &tx->tx_frags[i]);
655 nob += rd->rd_frags[i].rf_nob;
658 /* looking for pre-mapping MR */
659 mr = kiblnd_find_rd_dma_mr(hdev, rd);
661 /* found pre-mapping MR */
662 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
667 return kiblnd_fmr_map_tx(net, tx, rd, nob);
673 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
674 unsigned int niov, struct kvec *iov, int offset, int nob)
676 kib_net_t *net = ni->ni_data;
678 struct scatterlist *sg;
687 while (offset >= iov->iov_len) {
688 offset -= iov->iov_len;
698 vaddr = ((unsigned long)iov->iov_base) + offset;
699 page_offset = vaddr & (PAGE_SIZE - 1);
700 page = kiblnd_kvaddr_to_page(vaddr);
702 CERROR("Can't find page\n");
706 fragnob = min((int)(iov->iov_len - offset), nob);
707 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
709 sg_set_page(sg, page, fragnob, page_offset);
712 if (offset + fragnob < iov->iov_len) {
722 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
726 kiblnd_setup_rd_kiov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
727 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
729 kib_net_t *net = ni->ni_data;
730 struct scatterlist *sg;
733 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
739 while (offset >= kiov->kiov_len) {
740 offset -= kiov->kiov_len;
750 fragnob = min((int)(kiov->kiov_len - offset), nob);
752 sg_set_page(sg, kiov->kiov_page, fragnob,
753 kiov->kiov_offset + offset);
762 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
766 kiblnd_post_tx_locked(kib_conn_t *conn, kib_tx_t *tx, int credit)
767 __must_hold(&conn->ibc_lock)
769 kib_msg_t *msg = tx->tx_msg;
770 kib_peer_t *peer = conn->ibc_peer;
771 int ver = conn->ibc_version;
774 struct ib_send_wr *bad_wrq;
776 LASSERT(tx->tx_queued);
777 /* We rely on this for QP sizing */
778 LASSERT(tx->tx_nwrq > 0);
779 LASSERT(tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
781 LASSERT(!credit || credit == 1);
782 LASSERT(conn->ibc_outstanding_credits >= 0);
783 LASSERT(conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
784 LASSERT(conn->ibc_credits >= 0);
785 LASSERT(conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
787 if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
788 /* tx completions outstanding... */
789 CDEBUG(D_NET, "%s: posted enough\n",
790 libcfs_nid2str(peer->ibp_nid));
794 if (credit && !conn->ibc_credits) { /* no credits */
795 CDEBUG(D_NET, "%s: no credits\n",
796 libcfs_nid2str(peer->ibp_nid));
800 if (credit && !IBLND_OOB_CAPABLE(ver) &&
801 conn->ibc_credits == 1 && /* last credit reserved */
802 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
803 CDEBUG(D_NET, "%s: not using last credit\n",
804 libcfs_nid2str(peer->ibp_nid));
808 /* NB don't drop ibc_lock before bumping tx_sending */
809 list_del(&tx->tx_list);
812 if (msg->ibm_type == IBLND_MSG_NOOP &&
813 (!kiblnd_need_noop(conn) || /* redundant NOOP */
814 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
815 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
817 * OK to drop when posted enough NOOPs, since
818 * kiblnd_check_sends will queue NOOP again when
819 * posted NOOPs complete
821 spin_unlock(&conn->ibc_lock);
822 kiblnd_tx_done(peer->ibp_ni, tx);
823 spin_lock(&conn->ibc_lock);
824 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
825 libcfs_nid2str(peer->ibp_nid),
826 conn->ibc_noops_posted);
830 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
831 peer->ibp_nid, conn->ibc_incarnation);
833 conn->ibc_credits -= credit;
834 conn->ibc_outstanding_credits = 0;
835 conn->ibc_nsends_posted++;
836 if (msg->ibm_type == IBLND_MSG_NOOP)
837 conn->ibc_noops_posted++;
840 * CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
841 * PUT. If so, it was first queued here as a PUT_REQ, sent and
842 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
843 * and then re-queued here. It's (just) possible that
844 * tx_sending is non-zero if we've not done the tx_complete()
845 * from the first send; hence the ++ rather than = below.
848 list_add(&tx->tx_list, &conn->ibc_active_txs);
850 /* I'm still holding ibc_lock! */
851 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
853 } else if (tx->tx_pool->tpo_pool.po_failed ||
854 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
855 /* close_conn will launch failover */
858 rc = ib_post_send(conn->ibc_cmid->qp, &tx->tx_wrq->wr, &bad_wrq);
861 conn->ibc_last_send = jiffies;
867 * NB credits are transferred in the actual
868 * message, which can only be the last work item
870 conn->ibc_credits += credit;
871 conn->ibc_outstanding_credits += msg->ibm_credits;
872 conn->ibc_nsends_posted--;
873 if (msg->ibm_type == IBLND_MSG_NOOP)
874 conn->ibc_noops_posted--;
880 done = !tx->tx_sending;
882 list_del(&tx->tx_list);
884 spin_unlock(&conn->ibc_lock);
886 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
887 CERROR("Error %d posting transmit to %s\n",
888 rc, libcfs_nid2str(peer->ibp_nid));
890 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
891 rc, libcfs_nid2str(peer->ibp_nid));
893 kiblnd_close_conn(conn, rc);
896 kiblnd_tx_done(peer->ibp_ni, tx);
898 spin_lock(&conn->ibc_lock);
904 kiblnd_check_sends(kib_conn_t *conn)
906 int ver = conn->ibc_version;
907 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
910 /* Don't send anything until after the connection is established */
911 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
912 CDEBUG(D_NET, "%s too soon\n",
913 libcfs_nid2str(conn->ibc_peer->ibp_nid));
917 spin_lock(&conn->ibc_lock);
919 LASSERT(conn->ibc_nsends_posted <= IBLND_CONCURRENT_SENDS(ver));
920 LASSERT(!IBLND_OOB_CAPABLE(ver) ||
921 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
922 LASSERT(conn->ibc_reserved_credits >= 0);
924 while (conn->ibc_reserved_credits > 0 &&
925 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
926 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
928 list_del(&tx->tx_list);
929 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
930 conn->ibc_reserved_credits--;
933 if (kiblnd_need_noop(conn)) {
934 spin_unlock(&conn->ibc_lock);
936 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
938 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
940 spin_lock(&conn->ibc_lock);
942 kiblnd_queue_tx_locked(tx, conn);
945 kiblnd_conn_addref(conn); /* 1 ref for me.... (see b21911) */
950 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
952 tx = list_entry(conn->ibc_tx_queue_nocred.next,
954 } else if (!list_empty(&conn->ibc_tx_noops)) {
955 LASSERT(!IBLND_OOB_CAPABLE(ver));
957 tx = list_entry(conn->ibc_tx_noops.next,
959 } else if (!list_empty(&conn->ibc_tx_queue)) {
961 tx = list_entry(conn->ibc_tx_queue.next,
967 if (kiblnd_post_tx_locked(conn, tx, credit))
971 spin_unlock(&conn->ibc_lock);
973 kiblnd_conn_decref(conn); /* ...until here */
977 kiblnd_tx_complete(kib_tx_t *tx, int status)
979 int failed = (status != IB_WC_SUCCESS);
980 kib_conn_t *conn = tx->tx_conn;
983 LASSERT(tx->tx_sending > 0);
986 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
987 CNETERR("Tx -> %s cookie %#llx sending %d waiting %d: failed %d\n",
988 libcfs_nid2str(conn->ibc_peer->ibp_nid),
989 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
992 kiblnd_close_conn(conn, -EIO);
994 kiblnd_peer_alive(conn->ibc_peer);
997 spin_lock(&conn->ibc_lock);
1000 * I could be racing with rdma completion. Whoever makes 'tx' idle
1001 * gets to free it, which also drops its ref on 'conn'.
1004 conn->ibc_nsends_posted--;
1005 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1006 conn->ibc_noops_posted--;
1009 tx->tx_waiting = 0; /* don't wait for peer */
1010 tx->tx_status = -EIO;
1013 idle = !tx->tx_sending && /* This is the final callback */
1014 !tx->tx_waiting && /* Not waiting for peer */
1015 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1017 list_del(&tx->tx_list);
1019 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1021 spin_unlock(&conn->ibc_lock);
1024 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1026 kiblnd_check_sends(conn);
1028 kiblnd_conn_decref(conn); /* ...until here */
1032 kiblnd_init_tx_msg(lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1034 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
1035 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1036 struct ib_rdma_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1037 int nob = offsetof(kib_msg_t, ibm_u) + body_nob;
1040 LASSERT(tx->tx_nwrq >= 0);
1041 LASSERT(tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1042 LASSERT(nob <= IBLND_MSG_SIZE);
1044 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1046 mr = kiblnd_find_dma_mr(hdev, tx->tx_msgaddr, nob);
1049 sge->lkey = mr->lkey;
1050 sge->addr = tx->tx_msgaddr;
1053 memset(wrq, 0, sizeof(*wrq));
1055 wrq->wr.next = NULL;
1056 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1057 wrq->wr.sg_list = sge;
1058 wrq->wr.num_sge = 1;
1059 wrq->wr.opcode = IB_WR_SEND;
1060 wrq->wr.send_flags = IB_SEND_SIGNALED;
1066 kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
1067 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1069 kib_msg_t *ibmsg = tx->tx_msg;
1070 kib_rdma_desc_t *srcrd = tx->tx_rd;
1071 struct ib_sge *sge = &tx->tx_sge[0];
1072 struct ib_rdma_wr *wrq = &tx->tx_wrq[0], *next;
1078 LASSERT(!in_interrupt());
1079 LASSERT(!tx->tx_nwrq);
1080 LASSERT(type == IBLND_MSG_GET_DONE ||
1081 type == IBLND_MSG_PUT_DONE);
1084 if (srcidx >= srcrd->rd_nfrags) {
1085 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1090 if (dstidx == dstrd->rd_nfrags) {
1091 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1096 if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
1097 CERROR("RDMA too fragmented for %s (%d): %d/%d src %d/%d dst frags\n",
1098 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1099 IBLND_RDMA_FRAGS(conn->ibc_version),
1100 srcidx, srcrd->rd_nfrags,
1101 dstidx, dstrd->rd_nfrags);
1106 wrknob = min(min(kiblnd_rd_frag_size(srcrd, srcidx),
1107 kiblnd_rd_frag_size(dstrd, dstidx)),
1110 sge = &tx->tx_sge[tx->tx_nwrq];
1111 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1112 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1113 sge->length = wrknob;
1115 wrq = &tx->tx_wrq[tx->tx_nwrq];
1118 wrq->wr.next = &next->wr;
1119 wrq->wr.wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1120 wrq->wr.sg_list = sge;
1121 wrq->wr.num_sge = 1;
1122 wrq->wr.opcode = IB_WR_RDMA_WRITE;
1123 wrq->wr.send_flags = 0;
1125 wrq->remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1126 wrq->rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1128 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1129 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1138 if (rc < 0) /* no RDMA if completing with failure */
1141 ibmsg->ibm_u.completion.ibcm_status = rc;
1142 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1143 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1144 type, sizeof(kib_completion_msg_t));
1150 kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn)
1152 struct list_head *q;
1154 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1155 LASSERT(!tx->tx_queued); /* not queued for sending already */
1156 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1159 tx->tx_deadline = jiffies +
1160 msecs_to_jiffies(*kiblnd_tunables.kib_timeout *
1164 kiblnd_conn_addref(conn);
1166 LASSERT(tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1168 /* PUT_DONE first attached to conn as a PUT_REQ */
1169 LASSERT(tx->tx_conn == conn);
1170 LASSERT(tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1173 switch (tx->tx_msg->ibm_type) {
1177 case IBLND_MSG_PUT_REQ:
1178 case IBLND_MSG_GET_REQ:
1179 q = &conn->ibc_tx_queue_rsrvd;
1182 case IBLND_MSG_PUT_NAK:
1183 case IBLND_MSG_PUT_ACK:
1184 case IBLND_MSG_PUT_DONE:
1185 case IBLND_MSG_GET_DONE:
1186 q = &conn->ibc_tx_queue_nocred;
1189 case IBLND_MSG_NOOP:
1190 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1191 q = &conn->ibc_tx_queue_nocred;
1193 q = &conn->ibc_tx_noops;
1196 case IBLND_MSG_IMMEDIATE:
1197 q = &conn->ibc_tx_queue;
1201 list_add_tail(&tx->tx_list, q);
1205 kiblnd_queue_tx(kib_tx_t *tx, kib_conn_t *conn)
1207 spin_lock(&conn->ibc_lock);
1208 kiblnd_queue_tx_locked(tx, conn);
1209 spin_unlock(&conn->ibc_lock);
1211 kiblnd_check_sends(conn);
1214 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1215 struct sockaddr_in *srcaddr,
1216 struct sockaddr_in *dstaddr,
1219 unsigned short port;
1222 /* allow the port to be reused */
1223 rc = rdma_set_reuseaddr(cmid, 1);
1225 CERROR("Unable to set reuse on cmid: %d\n", rc);
1229 /* look for a free privileged port */
1230 for (port = PROT_SOCK - 1; port > 0; port--) {
1231 srcaddr->sin_port = htons(port);
1232 rc = rdma_resolve_addr(cmid,
1233 (struct sockaddr *)srcaddr,
1234 (struct sockaddr *)dstaddr,
1237 CDEBUG(D_NET, "bound to port %hu\n", port);
1239 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1240 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1247 CERROR("Failed to bind to a free privileged port\n");
1252 kiblnd_connect_peer(kib_peer_t *peer)
1254 struct rdma_cm_id *cmid;
1256 kib_net_t *net = peer->ibp_ni->ni_data;
1257 struct sockaddr_in srcaddr;
1258 struct sockaddr_in dstaddr;
1262 LASSERT(peer->ibp_connecting > 0);
1264 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP,
1268 CERROR("Can't create CMID for %s: %ld\n",
1269 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1275 memset(&srcaddr, 0, sizeof(srcaddr));
1276 srcaddr.sin_family = AF_INET;
1277 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1279 memset(&dstaddr, 0, sizeof(dstaddr));
1280 dstaddr.sin_family = AF_INET;
1281 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1282 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1284 kiblnd_peer_addref(peer); /* cmid's ref */
1286 if (*kiblnd_tunables.kib_use_priv_port) {
1287 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1288 *kiblnd_tunables.kib_timeout * 1000);
1290 rc = rdma_resolve_addr(cmid,
1291 (struct sockaddr *)&srcaddr,
1292 (struct sockaddr *)&dstaddr,
1293 *kiblnd_tunables.kib_timeout * 1000);
1296 /* Can't initiate address resolution: */
1297 CERROR("Can't resolve addr for %s: %d\n",
1298 libcfs_nid2str(peer->ibp_nid), rc);
1302 LASSERT(cmid->device);
1303 CDEBUG(D_NET, "%s: connection bound to %s:%pI4h:%s\n",
1304 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1305 &dev->ibd_ifip, cmid->device->name);
1310 kiblnd_peer_decref(peer); /* cmid's ref */
1311 rdma_destroy_id(cmid);
1313 kiblnd_peer_connect_failed(peer, 1, rc);
1317 kiblnd_launch_tx(lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1322 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1323 unsigned long flags;
1327 * If I get here, I've committed to send, so I complete the tx with
1328 * failure on any problems
1330 LASSERT(!tx || !tx->tx_conn); /* only set when assigned a conn */
1331 LASSERT(!tx || tx->tx_nwrq > 0); /* work items have been set up */
1334 * First time, just use a read lock since I expect to find my peer
1337 read_lock_irqsave(g_lock, flags);
1339 peer = kiblnd_find_peer_locked(nid);
1340 if (peer && !list_empty(&peer->ibp_conns)) {
1341 /* Found a peer with an established connection */
1342 conn = kiblnd_get_conn_locked(peer);
1343 kiblnd_conn_addref(conn); /* 1 ref for me... */
1345 read_unlock_irqrestore(g_lock, flags);
1348 kiblnd_queue_tx(tx, conn);
1349 kiblnd_conn_decref(conn); /* ...to here */
1353 read_unlock(g_lock);
1354 /* Re-try with a write lock */
1357 peer = kiblnd_find_peer_locked(nid);
1359 if (list_empty(&peer->ibp_conns)) {
1360 /* found a peer, but it's still connecting... */
1361 LASSERT(peer->ibp_connecting ||
1362 peer->ibp_accepting);
1364 list_add_tail(&tx->tx_list,
1365 &peer->ibp_tx_queue);
1366 write_unlock_irqrestore(g_lock, flags);
1368 conn = kiblnd_get_conn_locked(peer);
1369 kiblnd_conn_addref(conn); /* 1 ref for me... */
1371 write_unlock_irqrestore(g_lock, flags);
1374 kiblnd_queue_tx(tx, conn);
1375 kiblnd_conn_decref(conn); /* ...to here */
1380 write_unlock_irqrestore(g_lock, flags);
1382 /* Allocate a peer ready to add to the peer table and retry */
1383 rc = kiblnd_create_peer(ni, &peer, nid);
1385 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1387 tx->tx_status = -EHOSTUNREACH;
1389 kiblnd_tx_done(ni, tx);
1394 write_lock_irqsave(g_lock, flags);
1396 peer2 = kiblnd_find_peer_locked(nid);
1398 if (list_empty(&peer2->ibp_conns)) {
1399 /* found a peer, but it's still connecting... */
1400 LASSERT(peer2->ibp_connecting ||
1401 peer2->ibp_accepting);
1403 list_add_tail(&tx->tx_list,
1404 &peer2->ibp_tx_queue);
1405 write_unlock_irqrestore(g_lock, flags);
1407 conn = kiblnd_get_conn_locked(peer2);
1408 kiblnd_conn_addref(conn); /* 1 ref for me... */
1410 write_unlock_irqrestore(g_lock, flags);
1413 kiblnd_queue_tx(tx, conn);
1414 kiblnd_conn_decref(conn); /* ...to here */
1417 kiblnd_peer_decref(peer);
1421 /* Brand new peer */
1422 LASSERT(!peer->ibp_connecting);
1423 peer->ibp_connecting = 1;
1425 /* always called with a ref on ni, which prevents ni being shutdown */
1426 LASSERT(!((kib_net_t *)ni->ni_data)->ibn_shutdown);
1429 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1431 kiblnd_peer_addref(peer);
1432 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1434 write_unlock_irqrestore(g_lock, flags);
1436 kiblnd_connect_peer(peer);
1437 kiblnd_peer_decref(peer);
1441 kiblnd_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1443 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1444 int type = lntmsg->msg_type;
1445 lnet_process_id_t target = lntmsg->msg_target;
1446 int target_is_router = lntmsg->msg_target_is_router;
1447 int routing = lntmsg->msg_routing;
1448 unsigned int payload_niov = lntmsg->msg_niov;
1449 struct kvec *payload_iov = lntmsg->msg_iov;
1450 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1451 unsigned int payload_offset = lntmsg->msg_offset;
1452 unsigned int payload_nob = lntmsg->msg_len;
1454 kib_rdma_desc_t *rd;
1459 /* NB 'private' is different depending on what we're sending.... */
1461 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1462 payload_nob, payload_niov, libcfs_id2str(target));
1464 LASSERT(!payload_nob || payload_niov > 0);
1465 LASSERT(payload_niov <= LNET_MAX_IOV);
1467 /* Thread context */
1468 LASSERT(!in_interrupt());
1469 /* payload is either all vaddrs or all pages */
1470 LASSERT(!(payload_kiov && payload_iov));
1478 LASSERT(!payload_nob);
1482 if (routing || target_is_router)
1483 break; /* send IMMEDIATE */
1485 /* is the REPLY message too small for RDMA? */
1486 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1487 if (nob <= IBLND_MSG_SIZE)
1488 break; /* send IMMEDIATE */
1490 tx = kiblnd_get_idle_tx(ni, target.nid);
1492 CERROR("Can't allocate txd for GET to %s\n",
1493 libcfs_nid2str(target.nid));
1498 rd = &ibmsg->ibm_u.get.ibgm_rd;
1499 if (!(lntmsg->msg_md->md_options & LNET_MD_KIOV))
1500 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1501 lntmsg->msg_md->md_niov,
1502 lntmsg->msg_md->md_iov.iov,
1503 0, lntmsg->msg_md->md_length);
1505 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1506 lntmsg->msg_md->md_niov,
1507 lntmsg->msg_md->md_iov.kiov,
1508 0, lntmsg->msg_md->md_length);
1510 CERROR("Can't setup GET sink for %s: %d\n",
1511 libcfs_nid2str(target.nid), rc);
1512 kiblnd_tx_done(ni, tx);
1516 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[rd->rd_nfrags]);
1517 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1518 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1520 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1522 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1523 if (!tx->tx_lntmsg[1]) {
1524 CERROR("Can't create reply for GET -> %s\n",
1525 libcfs_nid2str(target.nid));
1526 kiblnd_tx_done(ni, tx);
1530 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1531 tx->tx_waiting = 1; /* waiting for GET_DONE */
1532 kiblnd_launch_tx(ni, tx, target.nid);
1535 case LNET_MSG_REPLY:
1537 /* Is the payload small enough not to need RDMA? */
1538 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1539 if (nob <= IBLND_MSG_SIZE)
1540 break; /* send IMMEDIATE */
1542 tx = kiblnd_get_idle_tx(ni, target.nid);
1544 CERROR("Can't allocate %s txd for %s\n",
1545 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1546 libcfs_nid2str(target.nid));
1551 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1552 payload_niov, payload_iov,
1553 payload_offset, payload_nob);
1555 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1556 payload_niov, payload_kiov,
1557 payload_offset, payload_nob);
1559 CERROR("Can't setup PUT src for %s: %d\n",
1560 libcfs_nid2str(target.nid), rc);
1561 kiblnd_tx_done(ni, tx);
1566 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1567 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1568 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1570 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1571 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1572 kiblnd_launch_tx(ni, tx, target.nid);
1576 /* send IMMEDIATE */
1578 LASSERT(offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1581 tx = kiblnd_get_idle_tx(ni, target.nid);
1583 CERROR("Can't send %d to %s: tx descs exhausted\n",
1584 type, libcfs_nid2str(target.nid));
1589 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1592 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1593 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1594 payload_niov, payload_kiov,
1595 payload_offset, payload_nob);
1597 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1598 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1599 payload_niov, payload_iov,
1600 payload_offset, payload_nob);
1602 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1603 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1605 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1606 kiblnd_launch_tx(ni, tx, target.nid);
1611 kiblnd_reply(lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1613 lnet_process_id_t target = lntmsg->msg_target;
1614 unsigned int niov = lntmsg->msg_niov;
1615 struct kvec *iov = lntmsg->msg_iov;
1616 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1617 unsigned int offset = lntmsg->msg_offset;
1618 unsigned int nob = lntmsg->msg_len;
1622 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1624 CERROR("Can't get tx for REPLY to %s\n",
1625 libcfs_nid2str(target.nid));
1632 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1633 niov, iov, offset, nob);
1635 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1636 niov, kiov, offset, nob);
1639 CERROR("Can't setup GET src for %s: %d\n",
1640 libcfs_nid2str(target.nid), rc);
1644 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1645 IBLND_MSG_GET_DONE, nob,
1646 &rx->rx_msg->ibm_u.get.ibgm_rd,
1647 rx->rx_msg->ibm_u.get.ibgm_cookie);
1649 CERROR("Can't setup rdma for GET from %s: %d\n",
1650 libcfs_nid2str(target.nid), rc);
1655 /* No RDMA: local completion may happen now! */
1656 lnet_finalize(ni, lntmsg, 0);
1658 /* RDMA: lnet_finalize(lntmsg) when it completes */
1659 tx->tx_lntmsg[0] = lntmsg;
1662 kiblnd_queue_tx(tx, rx->rx_conn);
1666 kiblnd_tx_done(ni, tx);
1668 lnet_finalize(ni, lntmsg, -EIO);
1672 kiblnd_recv(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1673 unsigned int niov, struct kvec *iov, lnet_kiov_t *kiov,
1674 unsigned int offset, unsigned int mlen, unsigned int rlen)
1676 kib_rx_t *rx = private;
1677 kib_msg_t *rxmsg = rx->rx_msg;
1678 kib_conn_t *conn = rx->rx_conn;
1681 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1684 LASSERT(mlen <= rlen);
1685 LASSERT(!in_interrupt());
1686 /* Either all pages or all vaddrs */
1687 LASSERT(!(kiov && iov));
1689 switch (rxmsg->ibm_type) {
1693 case IBLND_MSG_IMMEDIATE:
1694 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1695 if (nob > rx->rx_nob) {
1696 CERROR("Immediate message from %s too big: %d(%d)\n",
1697 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1704 lnet_copy_flat2kiov(niov, kiov, offset,
1705 IBLND_MSG_SIZE, rxmsg,
1706 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1709 lnet_copy_flat2iov(niov, iov, offset,
1710 IBLND_MSG_SIZE, rxmsg,
1711 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1713 lnet_finalize(ni, lntmsg, 0);
1716 case IBLND_MSG_PUT_REQ: {
1718 kib_rdma_desc_t *rd;
1721 lnet_finalize(ni, lntmsg, 0);
1722 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1723 rxmsg->ibm_u.putreq.ibprm_cookie);
1727 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1729 CERROR("Can't allocate tx for %s\n",
1730 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1731 /* Not replying will break the connection */
1737 rd = &txmsg->ibm_u.putack.ibpam_rd;
1739 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1740 niov, iov, offset, mlen);
1742 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1743 niov, kiov, offset, mlen);
1745 CERROR("Can't setup PUT sink for %s: %d\n",
1746 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1747 kiblnd_tx_done(ni, tx);
1748 /* tell peer it's over */
1749 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1750 rxmsg->ibm_u.putreq.ibprm_cookie);
1754 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[rd->rd_nfrags]);
1755 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1756 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1758 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1760 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1761 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1762 kiblnd_queue_tx(tx, conn);
1764 /* reposted buffer reserved for PUT_DONE */
1765 post_credit = IBLND_POSTRX_NO_CREDIT;
1769 case IBLND_MSG_GET_REQ:
1771 /* Optimized GET; RDMA lntmsg's payload */
1772 kiblnd_reply(ni, rx, lntmsg);
1774 /* GET didn't match anything */
1775 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1777 rxmsg->ibm_u.get.ibgm_cookie);
1782 kiblnd_post_rx(rx, post_credit);
1787 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1789 struct task_struct *task = kthread_run(fn, arg, "%s", name);
1792 return PTR_ERR(task);
1794 atomic_inc(&kiblnd_data.kib_nthreads);
1799 kiblnd_thread_fini(void)
1801 atomic_dec(&kiblnd_data.kib_nthreads);
1805 kiblnd_peer_alive(kib_peer_t *peer)
1807 /* This is racy, but everyone's only writing cfs_time_current() */
1808 peer->ibp_last_alive = cfs_time_current();
1813 kiblnd_peer_notify(kib_peer_t *peer)
1816 unsigned long last_alive = 0;
1817 unsigned long flags;
1819 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1821 if (list_empty(&peer->ibp_conns) &&
1822 !peer->ibp_accepting &&
1823 !peer->ibp_connecting &&
1825 error = peer->ibp_error;
1826 peer->ibp_error = 0;
1828 last_alive = peer->ibp_last_alive;
1831 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1834 lnet_notify(peer->ibp_ni,
1835 peer->ibp_nid, 0, last_alive);
1839 kiblnd_close_conn_locked(kib_conn_t *conn, int error)
1842 * This just does the immediate housekeeping. 'error' is zero for a
1843 * normal shutdown which can happen only after the connection has been
1844 * established. If the connection is established, schedule the
1845 * connection to be finished off by the connd. Otherwise the connd is
1846 * already dealing with it (either to set it up or tear it down).
1847 * Caller holds kib_global_lock exclusively in irq context
1849 kib_peer_t *peer = conn->ibc_peer;
1851 unsigned long flags;
1853 LASSERT(error || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1855 if (error && !conn->ibc_comms_error)
1856 conn->ibc_comms_error = error;
1858 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1859 return; /* already being handled */
1862 list_empty(&conn->ibc_tx_noops) &&
1863 list_empty(&conn->ibc_tx_queue) &&
1864 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1865 list_empty(&conn->ibc_tx_queue_nocred) &&
1866 list_empty(&conn->ibc_active_txs)) {
1867 CDEBUG(D_NET, "closing conn to %s\n",
1868 libcfs_nid2str(peer->ibp_nid));
1870 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1871 libcfs_nid2str(peer->ibp_nid), error,
1872 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1873 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1874 list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1875 list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1876 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1879 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1880 list_del(&conn->ibc_list);
1881 /* connd (see below) takes over ibc_list's ref */
1883 if (list_empty(&peer->ibp_conns) && /* no more conns */
1884 kiblnd_peer_active(peer)) { /* still in peer table */
1885 kiblnd_unlink_peer_locked(peer);
1887 /* set/clear error on last conn */
1888 peer->ibp_error = conn->ibc_comms_error;
1891 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1894 kiblnd_dev_can_failover(dev)) {
1895 list_add_tail(&dev->ibd_fail_list,
1896 &kiblnd_data.kib_failed_devs);
1897 wake_up(&kiblnd_data.kib_failover_waitq);
1900 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1902 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1903 wake_up(&kiblnd_data.kib_connd_waitq);
1905 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1909 kiblnd_close_conn(kib_conn_t *conn, int error)
1911 unsigned long flags;
1913 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1915 kiblnd_close_conn_locked(conn, error);
1917 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1921 kiblnd_handle_early_rxs(kib_conn_t *conn)
1923 unsigned long flags;
1927 LASSERT(!in_interrupt());
1928 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1930 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1931 list_for_each_entry_safe(rx, tmp, &conn->ibc_early_rxs, rx_list) {
1932 list_del(&rx->rx_list);
1933 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1935 kiblnd_handle_rx(rx);
1937 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1939 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1943 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1946 struct list_head *tmp;
1947 struct list_head *nxt;
1950 spin_lock(&conn->ibc_lock);
1952 list_for_each_safe(tmp, nxt, txs) {
1953 tx = list_entry(tmp, kib_tx_t, tx_list);
1955 if (txs == &conn->ibc_active_txs) {
1956 LASSERT(!tx->tx_queued);
1957 LASSERT(tx->tx_waiting || tx->tx_sending);
1959 LASSERT(tx->tx_queued);
1962 tx->tx_status = -ECONNABORTED;
1965 if (!tx->tx_sending) {
1967 list_del(&tx->tx_list);
1968 list_add(&tx->tx_list, &zombies);
1972 spin_unlock(&conn->ibc_lock);
1974 kiblnd_txlist_done(conn->ibc_peer->ibp_ni, &zombies, -ECONNABORTED);
1978 kiblnd_finalise_conn(kib_conn_t *conn)
1980 LASSERT(!in_interrupt());
1981 LASSERT(conn->ibc_state > IBLND_CONN_INIT);
1983 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
1986 * abort_receives moves QP state to IB_QPS_ERR. This is only required
1987 * for connections that didn't get as far as being connected, because
1988 * rdma_disconnect() does this for free.
1990 kiblnd_abort_receives(conn);
1993 * Complete all tx descs not waiting for sends to complete.
1994 * NB we should be safe from RDMA now that the QP has changed state
1996 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
1997 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
1998 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
1999 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2000 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2002 kiblnd_handle_early_rxs(conn);
2006 kiblnd_peer_connect_failed(kib_peer_t *peer, int active, int error)
2009 unsigned long flags;
2012 LASSERT(!in_interrupt());
2014 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2017 LASSERT(peer->ibp_connecting > 0);
2018 peer->ibp_connecting--;
2020 LASSERT(peer->ibp_accepting > 0);
2021 peer->ibp_accepting--;
2024 if (peer->ibp_connecting ||
2025 peer->ibp_accepting) {
2026 /* another connection attempt under way... */
2027 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2032 if (list_empty(&peer->ibp_conns)) {
2033 /* Take peer's blocked transmits to complete with error */
2034 list_add(&zombies, &peer->ibp_tx_queue);
2035 list_del_init(&peer->ibp_tx_queue);
2037 if (kiblnd_peer_active(peer))
2038 kiblnd_unlink_peer_locked(peer);
2040 peer->ibp_error = error;
2042 /* Can't have blocked transmits if there are connections */
2043 LASSERT(list_empty(&peer->ibp_tx_queue));
2046 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2048 kiblnd_peer_notify(peer);
2050 if (list_empty(&zombies))
2053 CNETERR("Deleting messages for %s: connection failed\n",
2054 libcfs_nid2str(peer->ibp_nid));
2056 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2060 kiblnd_connreq_done(kib_conn_t *conn, int status)
2062 kib_peer_t *peer = conn->ibc_peer;
2065 struct list_head txs;
2066 unsigned long flags;
2069 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2071 CDEBUG(D_NET, "%s: active(%d), version(%x), status(%d)\n",
2072 libcfs_nid2str(peer->ibp_nid), active,
2073 conn->ibc_version, status);
2075 LASSERT(!in_interrupt());
2076 LASSERT((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2077 peer->ibp_connecting > 0) ||
2078 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2079 peer->ibp_accepting > 0));
2081 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2082 conn->ibc_connvars = NULL;
2085 /* failed to establish connection */
2086 kiblnd_peer_connect_failed(peer, active, status);
2087 kiblnd_finalise_conn(conn);
2091 /* connection established */
2092 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2094 conn->ibc_last_send = jiffies;
2095 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2096 kiblnd_peer_alive(peer);
2099 * Add conn to peer's list and nuke any dangling conns from a different
2102 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2103 list_add(&conn->ibc_list, &peer->ibp_conns);
2105 peer->ibp_connecting--;
2107 peer->ibp_accepting--;
2109 if (!peer->ibp_version) {
2110 peer->ibp_version = conn->ibc_version;
2111 peer->ibp_incarnation = conn->ibc_incarnation;
2114 if (peer->ibp_version != conn->ibc_version ||
2115 peer->ibp_incarnation != conn->ibc_incarnation) {
2116 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2117 conn->ibc_incarnation);
2118 peer->ibp_version = conn->ibc_version;
2119 peer->ibp_incarnation = conn->ibc_incarnation;
2122 /* grab pending txs while I have the lock */
2123 list_add(&txs, &peer->ibp_tx_queue);
2124 list_del_init(&peer->ibp_tx_queue);
2126 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2127 conn->ibc_comms_error) { /* error has happened already */
2128 lnet_ni_t *ni = peer->ibp_ni;
2130 /* start to shut down connection */
2131 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2132 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2134 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2139 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2141 /* Schedule blocked txs */
2142 spin_lock(&conn->ibc_lock);
2143 list_for_each_entry_safe(tx, tmp, &txs, tx_list) {
2144 list_del(&tx->tx_list);
2146 kiblnd_queue_tx_locked(tx, conn);
2148 spin_unlock(&conn->ibc_lock);
2150 kiblnd_check_sends(conn);
2152 /* schedule blocked rxs */
2153 kiblnd_handle_early_rxs(conn);
2157 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2161 rc = rdma_reject(cmid, rej, sizeof(*rej));
2164 CWARN("Error %d sending reject\n", rc);
2168 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2170 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2171 kib_msg_t *reqmsg = priv;
2177 lnet_ni_t *ni = NULL;
2178 kib_net_t *net = NULL;
2180 struct rdma_conn_param cp;
2182 int version = IBLND_MSG_VERSION;
2183 unsigned long flags;
2185 struct sockaddr_in *peer_addr;
2187 LASSERT(!in_interrupt());
2189 /* cmid inherits 'context' from the corresponding listener id */
2190 ibdev = (kib_dev_t *)cmid->context;
2193 memset(&rej, 0, sizeof(rej));
2194 rej.ibr_magic = IBLND_MSG_MAGIC;
2195 rej.ibr_why = IBLND_REJECT_FATAL;
2196 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2198 peer_addr = (struct sockaddr_in *)&cmid->route.addr.dst_addr;
2199 if (*kiblnd_tunables.kib_require_priv_port &&
2200 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2201 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2203 CERROR("Peer's port (%pI4h:%hu) is not privileged\n",
2204 &ip, ntohs(peer_addr->sin_port));
2208 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2209 CERROR("Short connection request\n");
2214 * Future protocol version compatibility support! If the
2215 * o2iblnd-specific protocol changes, or when LNET unifies
2216 * protocols over all LNDs, the initial connection will
2217 * negotiate a protocol version. I trap this here to avoid
2218 * console errors; the reject tells the peer which protocol I
2221 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2222 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2224 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2225 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2226 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2228 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2229 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2230 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2233 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2235 CERROR("Can't parse connection request: %d\n", rc);
2239 nid = reqmsg->ibm_srcnid;
2240 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2243 net = (kib_net_t *)ni->ni_data;
2244 rej.ibr_incarnation = net->ibn_incarnation;
2247 if (!ni || /* no matching net */
2248 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2249 net->ibn_dev != ibdev) { /* wrong device */
2250 CERROR("Can't accept %s on %s (%s:%d:%pI4h): bad dst nid %s\n",
2251 libcfs_nid2str(nid),
2252 !ni ? "NA" : libcfs_nid2str(ni->ni_nid),
2253 ibdev->ibd_ifname, ibdev->ibd_nnets,
2255 libcfs_nid2str(reqmsg->ibm_dstnid));
2260 /* check time stamp as soon as possible */
2261 if (reqmsg->ibm_dststamp &&
2262 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2263 CWARN("Stale connection request\n");
2264 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2268 /* I can accept peer's version */
2269 version = reqmsg->ibm_version;
2271 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2272 CERROR("Unexpected connreq msg type: %x from %s\n",
2273 reqmsg->ibm_type, libcfs_nid2str(nid));
2277 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2278 IBLND_MSG_QUEUE_SIZE(version)) {
2279 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2280 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2281 IBLND_MSG_QUEUE_SIZE(version));
2283 if (version == IBLND_MSG_VERSION)
2284 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2289 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2290 IBLND_RDMA_FRAGS(version)) {
2291 CERROR("Can't accept %s(version %x): incompatible max_frags %d (%d wanted)\n",
2292 libcfs_nid2str(nid), version,
2293 reqmsg->ibm_u.connparams.ibcp_max_frags,
2294 IBLND_RDMA_FRAGS(version));
2296 if (version == IBLND_MSG_VERSION)
2297 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2302 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2303 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2304 libcfs_nid2str(nid),
2305 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2310 /* assume 'nid' is a new peer; create */
2311 rc = kiblnd_create_peer(ni, &peer, nid);
2313 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2314 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2318 write_lock_irqsave(g_lock, flags);
2320 peer2 = kiblnd_find_peer_locked(nid);
2322 if (!peer2->ibp_version) {
2323 peer2->ibp_version = version;
2324 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2327 /* not the guy I've talked with */
2328 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2329 peer2->ibp_version != version) {
2330 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2331 write_unlock_irqrestore(g_lock, flags);
2333 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2334 libcfs_nid2str(nid), peer2->ibp_version, version);
2336 kiblnd_peer_decref(peer);
2337 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2341 /* tie-break connection race in favour of the higher NID */
2342 if (peer2->ibp_connecting &&
2344 write_unlock_irqrestore(g_lock, flags);
2346 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2348 kiblnd_peer_decref(peer);
2349 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2353 peer2->ibp_accepting++;
2354 kiblnd_peer_addref(peer2);
2356 write_unlock_irqrestore(g_lock, flags);
2357 kiblnd_peer_decref(peer);
2360 /* Brand new peer */
2361 LASSERT(!peer->ibp_accepting);
2362 LASSERT(!peer->ibp_version &&
2363 !peer->ibp_incarnation);
2365 peer->ibp_accepting = 1;
2366 peer->ibp_version = version;
2367 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2369 /* I have a ref on ni that prevents it being shutdown */
2370 LASSERT(!net->ibn_shutdown);
2372 kiblnd_peer_addref(peer);
2373 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2375 write_unlock_irqrestore(g_lock, flags);
2378 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2380 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2381 kiblnd_peer_decref(peer);
2382 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2387 * conn now "owns" cmid, so I return success from here on to ensure the
2388 * CM callback doesn't destroy cmid.
2390 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2391 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2392 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2393 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2394 <= IBLND_RX_MSGS(version));
2396 ackmsg = &conn->ibc_connvars->cv_msg;
2397 memset(ackmsg, 0, sizeof(*ackmsg));
2399 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2400 sizeof(ackmsg->ibm_u.connparams));
2401 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2402 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2403 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2405 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2407 memset(&cp, 0, sizeof(cp));
2408 cp.private_data = ackmsg;
2409 cp.private_data_len = ackmsg->ibm_nob;
2410 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2411 cp.initiator_depth = 0;
2412 cp.flow_control = 1;
2413 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2414 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2416 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2418 rc = rdma_accept(cmid, &cp);
2420 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2421 rej.ibr_version = version;
2422 rej.ibr_why = IBLND_REJECT_FATAL;
2424 kiblnd_reject(cmid, &rej);
2425 kiblnd_connreq_done(conn, rc);
2426 kiblnd_conn_decref(conn);
2436 rej.ibr_version = version;
2437 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2438 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2439 kiblnd_reject(cmid, &rej);
2441 return -ECONNREFUSED;
2445 kiblnd_reconnect(kib_conn_t *conn, int version,
2446 __u64 incarnation, int why, kib_connparams_t *cp)
2448 kib_peer_t *peer = conn->ibc_peer;
2451 unsigned long flags;
2453 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2454 LASSERT(peer->ibp_connecting > 0); /* 'conn' at least */
2456 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2459 * retry connection if it's still needed and no other connection
2460 * attempts (active or passive) are in progress
2461 * NB: reconnect is still needed even when ibp_tx_queue is
2462 * empty if ibp_version != version because reconnect may be
2463 * initiated by kiblnd_query()
2465 if ((!list_empty(&peer->ibp_tx_queue) ||
2466 peer->ibp_version != version) &&
2467 peer->ibp_connecting == 1 &&
2468 !peer->ibp_accepting) {
2470 peer->ibp_connecting++;
2472 peer->ibp_version = version;
2473 peer->ibp_incarnation = incarnation;
2476 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2486 case IBLND_REJECT_CONN_STALE:
2490 case IBLND_REJECT_CONN_RACE:
2491 reason = "conn race";
2494 case IBLND_REJECT_CONN_UNCOMPAT:
2495 reason = "version negotiation";
2499 CNETERR("%s: retrying (%s), %x, %x, queue_dep: %d, max_frag: %d, msg_size: %d\n",
2500 libcfs_nid2str(peer->ibp_nid),
2501 reason, IBLND_MSG_VERSION, version,
2502 cp ? cp->ibcp_queue_depth : IBLND_MSG_QUEUE_SIZE(version),
2503 cp ? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2504 cp ? cp->ibcp_max_msg_size : IBLND_MSG_SIZE);
2506 kiblnd_connect_peer(peer);
2510 kiblnd_rejected(kib_conn_t *conn, int reason, void *priv, int priv_nob)
2512 kib_peer_t *peer = conn->ibc_peer;
2514 LASSERT(!in_interrupt());
2515 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2518 case IB_CM_REJ_STALE_CONN:
2519 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2520 IBLND_REJECT_CONN_STALE, NULL);
2523 case IB_CM_REJ_INVALID_SERVICE_ID:
2524 CNETERR("%s rejected: no listener at %d\n",
2525 libcfs_nid2str(peer->ibp_nid),
2526 *kiblnd_tunables.kib_service);
2529 case IB_CM_REJ_CONSUMER_DEFINED:
2530 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2531 kib_rej_t *rej = priv;
2532 kib_connparams_t *cp = NULL;
2534 __u64 incarnation = -1;
2536 /* NB. default incarnation is -1 because:
2537 * a) V1 will ignore dst incarnation in connreq.
2538 * b) V2 will provide incarnation while rejecting me,
2539 * -1 will be overwrote.
2541 * if I try to connect to a V1 peer with V2 protocol,
2542 * it rejected me then upgrade to V2, I have no idea
2543 * about the upgrading and try to reconnect with V1,
2544 * in this case upgraded V2 can find out I'm trying to
2545 * talk to the old guy and reject me(incarnation is -1).
2548 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2549 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2550 __swab32s(&rej->ibr_magic);
2551 __swab16s(&rej->ibr_version);
2555 if (priv_nob >= sizeof(kib_rej_t) &&
2556 rej->ibr_version > IBLND_MSG_VERSION_1) {
2558 * priv_nob is always 148 in current version
2559 * of OFED, so we still need to check version.
2560 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE)
2565 __swab64s(&rej->ibr_incarnation);
2566 __swab16s(&cp->ibcp_queue_depth);
2567 __swab16s(&cp->ibcp_max_frags);
2568 __swab32s(&cp->ibcp_max_msg_size);
2571 incarnation = rej->ibr_incarnation;
2574 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2575 rej->ibr_magic != LNET_PROTO_MAGIC) {
2576 CERROR("%s rejected: consumer defined fatal error\n",
2577 libcfs_nid2str(peer->ibp_nid));
2581 if (rej->ibr_version != IBLND_MSG_VERSION &&
2582 rej->ibr_version != IBLND_MSG_VERSION_1) {
2583 CERROR("%s rejected: o2iblnd version %x error\n",
2584 libcfs_nid2str(peer->ibp_nid),
2589 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2590 rej->ibr_version == IBLND_MSG_VERSION_1) {
2591 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2592 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2594 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2595 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2598 switch (rej->ibr_why) {
2599 case IBLND_REJECT_CONN_RACE:
2600 case IBLND_REJECT_CONN_STALE:
2601 case IBLND_REJECT_CONN_UNCOMPAT:
2602 kiblnd_reconnect(conn, rej->ibr_version,
2603 incarnation, rej->ibr_why, cp);
2606 case IBLND_REJECT_MSG_QUEUE_SIZE:
2607 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2608 libcfs_nid2str(peer->ibp_nid),
2609 cp ? cp->ibcp_queue_depth :
2610 IBLND_MSG_QUEUE_SIZE(rej->ibr_version),
2611 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2614 case IBLND_REJECT_RDMA_FRAGS:
2615 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2616 libcfs_nid2str(peer->ibp_nid),
2617 cp ? cp->ibcp_max_frags :
2618 IBLND_RDMA_FRAGS(rej->ibr_version),
2619 IBLND_RDMA_FRAGS(conn->ibc_version));
2622 case IBLND_REJECT_NO_RESOURCES:
2623 CERROR("%s rejected: o2iblnd no resources\n",
2624 libcfs_nid2str(peer->ibp_nid));
2627 case IBLND_REJECT_FATAL:
2628 CERROR("%s rejected: o2iblnd fatal error\n",
2629 libcfs_nid2str(peer->ibp_nid));
2633 CERROR("%s rejected: o2iblnd reason %d\n",
2634 libcfs_nid2str(peer->ibp_nid),
2642 CNETERR("%s rejected: reason %d, size %d\n",
2643 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2647 kiblnd_connreq_done(conn, -ECONNREFUSED);
2651 kiblnd_check_connreply(kib_conn_t *conn, void *priv, int priv_nob)
2653 kib_peer_t *peer = conn->ibc_peer;
2654 lnet_ni_t *ni = peer->ibp_ni;
2655 kib_net_t *net = ni->ni_data;
2656 kib_msg_t *msg = priv;
2657 int ver = conn->ibc_version;
2658 int rc = kiblnd_unpack_msg(msg, priv_nob);
2659 unsigned long flags;
2664 CERROR("Can't unpack connack from %s: %d\n",
2665 libcfs_nid2str(peer->ibp_nid), rc);
2669 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2670 CERROR("Unexpected message %d from %s\n",
2671 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2676 if (ver != msg->ibm_version) {
2677 CERROR("%s replied version %x is different with requested version %x\n",
2678 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2683 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2684 IBLND_MSG_QUEUE_SIZE(ver)) {
2685 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2686 libcfs_nid2str(peer->ibp_nid),
2687 msg->ibm_u.connparams.ibcp_queue_depth,
2688 IBLND_MSG_QUEUE_SIZE(ver));
2693 if (msg->ibm_u.connparams.ibcp_max_frags !=
2694 IBLND_RDMA_FRAGS(ver)) {
2695 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2696 libcfs_nid2str(peer->ibp_nid),
2697 msg->ibm_u.connparams.ibcp_max_frags,
2698 IBLND_RDMA_FRAGS(ver));
2703 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2704 CERROR("%s max message size %d too big (%d max)\n",
2705 libcfs_nid2str(peer->ibp_nid),
2706 msg->ibm_u.connparams.ibcp_max_msg_size,
2712 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2713 if (msg->ibm_dstnid == ni->ni_nid &&
2714 msg->ibm_dststamp == net->ibn_incarnation)
2718 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2721 CERROR("Bad connection reply from %s, rc = %d, version: %x max_frags: %d\n",
2722 libcfs_nid2str(peer->ibp_nid), rc,
2723 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2727 conn->ibc_incarnation = msg->ibm_srcstamp;
2729 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2730 LASSERT(conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2731 <= IBLND_RX_MSGS(ver));
2733 kiblnd_connreq_done(conn, 0);
2738 * NB My QP has already established itself, so I handle anything going
2739 * wrong here by setting ibc_comms_error.
2740 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2741 * immediately tears it down.
2744 conn->ibc_comms_error = rc;
2745 kiblnd_connreq_done(conn, 0);
2749 kiblnd_active_connect(struct rdma_cm_id *cmid)
2751 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2754 struct rdma_conn_param cp;
2757 unsigned long flags;
2760 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2762 incarnation = peer->ibp_incarnation;
2763 version = !peer->ibp_version ? IBLND_MSG_VERSION :
2766 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2768 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2770 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2771 kiblnd_peer_decref(peer); /* lose cmid's ref */
2776 * conn "owns" cmid now, so I return success from here on to ensure the
2777 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2780 msg = &conn->ibc_connvars->cv_msg;
2782 memset(msg, 0, sizeof(*msg));
2783 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2784 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2785 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2786 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2788 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2789 0, peer->ibp_nid, incarnation);
2791 memset(&cp, 0, sizeof(cp));
2792 cp.private_data = msg;
2793 cp.private_data_len = msg->ibm_nob;
2794 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2795 cp.initiator_depth = 0;
2796 cp.flow_control = 1;
2797 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2798 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2800 LASSERT(cmid->context == (void *)conn);
2801 LASSERT(conn->ibc_cmid == cmid);
2803 rc = rdma_connect(cmid, &cp);
2805 CERROR("Can't connect to %s: %d\n",
2806 libcfs_nid2str(peer->ibp_nid), rc);
2807 kiblnd_connreq_done(conn, rc);
2808 kiblnd_conn_decref(conn);
2815 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2821 switch (event->event) {
2823 CERROR("Unexpected event: %d, status: %d\n",
2824 event->event, event->status);
2827 case RDMA_CM_EVENT_CONNECT_REQUEST:
2828 /* destroy cmid on failure */
2829 rc = kiblnd_passive_connect(cmid,
2830 (void *)KIBLND_CONN_PARAM(event),
2831 KIBLND_CONN_PARAM_LEN(event));
2832 CDEBUG(D_NET, "connreq: %d\n", rc);
2835 case RDMA_CM_EVENT_ADDR_ERROR:
2836 peer = (kib_peer_t *)cmid->context;
2837 CNETERR("%s: ADDR ERROR %d\n",
2838 libcfs_nid2str(peer->ibp_nid), event->status);
2839 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2840 kiblnd_peer_decref(peer);
2841 return -EHOSTUNREACH; /* rc destroys cmid */
2843 case RDMA_CM_EVENT_ADDR_RESOLVED:
2844 peer = (kib_peer_t *)cmid->context;
2846 CDEBUG(D_NET, "%s Addr resolved: %d\n",
2847 libcfs_nid2str(peer->ibp_nid), event->status);
2849 if (event->status) {
2850 CNETERR("Can't resolve address for %s: %d\n",
2851 libcfs_nid2str(peer->ibp_nid), event->status);
2854 rc = rdma_resolve_route(
2855 cmid, *kiblnd_tunables.kib_timeout * 1000);
2858 /* Can't initiate route resolution */
2859 CERROR("Can't resolve route for %s: %d\n",
2860 libcfs_nid2str(peer->ibp_nid), rc);
2862 kiblnd_peer_connect_failed(peer, 1, rc);
2863 kiblnd_peer_decref(peer);
2864 return rc; /* rc destroys cmid */
2866 case RDMA_CM_EVENT_ROUTE_ERROR:
2867 peer = (kib_peer_t *)cmid->context;
2868 CNETERR("%s: ROUTE ERROR %d\n",
2869 libcfs_nid2str(peer->ibp_nid), event->status);
2870 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2871 kiblnd_peer_decref(peer);
2872 return -EHOSTUNREACH; /* rc destroys cmid */
2874 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2875 peer = (kib_peer_t *)cmid->context;
2876 CDEBUG(D_NET, "%s Route resolved: %d\n",
2877 libcfs_nid2str(peer->ibp_nid), event->status);
2880 return kiblnd_active_connect(cmid);
2882 CNETERR("Can't resolve route for %s: %d\n",
2883 libcfs_nid2str(peer->ibp_nid), event->status);
2884 kiblnd_peer_connect_failed(peer, 1, event->status);
2885 kiblnd_peer_decref(peer);
2886 return event->status; /* rc destroys cmid */
2888 case RDMA_CM_EVENT_UNREACHABLE:
2889 conn = (kib_conn_t *)cmid->context;
2890 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2891 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2892 CNETERR("%s: UNREACHABLE %d\n",
2893 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2894 kiblnd_connreq_done(conn, -ENETDOWN);
2895 kiblnd_conn_decref(conn);
2898 case RDMA_CM_EVENT_CONNECT_ERROR:
2899 conn = (kib_conn_t *)cmid->context;
2900 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2901 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2902 CNETERR("%s: CONNECT ERROR %d\n",
2903 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2904 kiblnd_connreq_done(conn, -ENOTCONN);
2905 kiblnd_conn_decref(conn);
2908 case RDMA_CM_EVENT_REJECTED:
2909 conn = (kib_conn_t *)cmid->context;
2910 switch (conn->ibc_state) {
2914 case IBLND_CONN_PASSIVE_WAIT:
2915 CERROR("%s: REJECTED %d\n",
2916 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2918 kiblnd_connreq_done(conn, -ECONNRESET);
2921 case IBLND_CONN_ACTIVE_CONNECT:
2922 kiblnd_rejected(conn, event->status,
2923 (void *)KIBLND_CONN_PARAM(event),
2924 KIBLND_CONN_PARAM_LEN(event));
2927 kiblnd_conn_decref(conn);
2930 case RDMA_CM_EVENT_ESTABLISHED:
2931 conn = (kib_conn_t *)cmid->context;
2932 switch (conn->ibc_state) {
2936 case IBLND_CONN_PASSIVE_WAIT:
2937 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2938 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2939 kiblnd_connreq_done(conn, 0);
2942 case IBLND_CONN_ACTIVE_CONNECT:
2943 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2944 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2945 kiblnd_check_connreply(conn,
2946 (void *)KIBLND_CONN_PARAM(event),
2947 KIBLND_CONN_PARAM_LEN(event));
2950 /* net keeps its ref on conn! */
2953 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2954 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2956 case RDMA_CM_EVENT_DISCONNECTED:
2957 conn = (kib_conn_t *)cmid->context;
2958 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2959 CERROR("%s DISCONNECTED\n",
2960 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2961 kiblnd_connreq_done(conn, -ECONNRESET);
2963 kiblnd_close_conn(conn, 0);
2965 kiblnd_conn_decref(conn);
2966 cmid->context = NULL;
2969 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2970 LCONSOLE_ERROR_MSG(0x131,
2971 "Received notification of device removal\n"
2972 "Please shutdown LNET to allow this to proceed\n");
2974 * Can't remove network from underneath LNET for now, so I have
2979 case RDMA_CM_EVENT_ADDR_CHANGE:
2980 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2986 kiblnd_check_txs_locked(kib_conn_t *conn, struct list_head *txs)
2989 struct list_head *ttmp;
2991 list_for_each(ttmp, txs) {
2992 tx = list_entry(ttmp, kib_tx_t, tx_list);
2994 if (txs != &conn->ibc_active_txs) {
2995 LASSERT(tx->tx_queued);
2997 LASSERT(!tx->tx_queued);
2998 LASSERT(tx->tx_waiting || tx->tx_sending);
3001 if (cfs_time_aftereq(jiffies, tx->tx_deadline)) {
3002 CERROR("Timed out tx: %s, %lu seconds\n",
3003 kiblnd_queue2str(conn, txs),
3004 cfs_duration_sec(jiffies - tx->tx_deadline));
3013 kiblnd_conn_timed_out_locked(kib_conn_t *conn)
3015 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3016 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3017 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3018 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3019 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3023 kiblnd_check_conns(int idx)
3026 LIST_HEAD(checksends);
3027 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3028 struct list_head *ptmp;
3032 struct list_head *ctmp;
3033 unsigned long flags;
3036 * NB. We expect to have a look at all the peers and not find any
3037 * RDMAs to time out, so we just use a shared lock while we
3040 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3042 list_for_each(ptmp, peers) {
3043 peer = list_entry(ptmp, kib_peer_t, ibp_list);
3045 list_for_each(ctmp, &peer->ibp_conns) {
3049 conn = list_entry(ctmp, kib_conn_t, ibc_list);
3051 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3053 spin_lock(&conn->ibc_lock);
3055 sendnoop = kiblnd_need_noop(conn);
3056 timedout = kiblnd_conn_timed_out_locked(conn);
3057 if (!sendnoop && !timedout) {
3058 spin_unlock(&conn->ibc_lock);
3063 CERROR("Timed out RDMA with %s (%lu): c: %u, oc: %u, rc: %u\n",
3064 libcfs_nid2str(peer->ibp_nid),
3065 cfs_duration_sec(cfs_time_current() -
3066 peer->ibp_last_alive),
3068 conn->ibc_outstanding_credits,
3069 conn->ibc_reserved_credits);
3070 list_add(&conn->ibc_connd_list, &closes);
3072 list_add(&conn->ibc_connd_list, &checksends);
3074 /* +ref for 'closes' or 'checksends' */
3075 kiblnd_conn_addref(conn);
3077 spin_unlock(&conn->ibc_lock);
3081 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3084 * Handle timeout by closing the whole
3085 * connection. We can only be sure RDMA activity
3086 * has ceased once the QP has been modified.
3088 list_for_each_entry_safe(conn, tmp, &closes, ibc_connd_list) {
3089 list_del(&conn->ibc_connd_list);
3090 kiblnd_close_conn(conn, -ETIMEDOUT);
3091 kiblnd_conn_decref(conn);
3095 * In case we have enough credits to return via a
3096 * NOOP, but there were no non-blocking tx descs
3097 * free to do it last time...
3099 while (!list_empty(&checksends)) {
3100 conn = list_entry(checksends.next, kib_conn_t, ibc_connd_list);
3101 list_del(&conn->ibc_connd_list);
3102 kiblnd_check_sends(conn);
3103 kiblnd_conn_decref(conn);
3108 kiblnd_disconnect_conn(kib_conn_t *conn)
3110 LASSERT(!in_interrupt());
3111 LASSERT(current == kiblnd_data.kib_connd);
3112 LASSERT(conn->ibc_state == IBLND_CONN_CLOSING);
3114 rdma_disconnect(conn->ibc_cmid);
3115 kiblnd_finalise_conn(conn);
3117 kiblnd_peer_notify(conn->ibc_peer);
3121 kiblnd_connd(void *arg)
3124 unsigned long flags;
3130 unsigned long deadline = jiffies;
3132 cfs_block_allsigs();
3134 init_waitqueue_entry(&wait, current);
3135 kiblnd_data.kib_connd = current;
3137 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3139 while (!kiblnd_data.kib_shutdown) {
3142 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3143 conn = list_entry(kiblnd_data.kib_connd_zombies.next,
3144 kib_conn_t, ibc_list);
3145 list_del(&conn->ibc_list);
3147 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3151 kiblnd_destroy_conn(conn);
3153 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3156 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3157 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3158 kib_conn_t, ibc_list);
3159 list_del(&conn->ibc_list);
3161 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3165 kiblnd_disconnect_conn(conn);
3166 kiblnd_conn_decref(conn);
3168 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3171 /* careful with the jiffy wrap... */
3172 timeout = (int)(deadline - jiffies);
3176 int chunk = kiblnd_data.kib_peer_hash_size;
3178 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3182 * Time to check for RDMA timeouts on a few more
3183 * peers: I do checks every 'p' seconds on a
3184 * proportion of the peer table and I need to check
3185 * every connection 'n' times within a timeout
3186 * interval, to ensure I detect a timeout on any
3187 * connection within (n+1)/n times the timeout
3190 if (*kiblnd_tunables.kib_timeout > n * p)
3191 chunk = (chunk * n * p) /
3192 *kiblnd_tunables.kib_timeout;
3196 for (i = 0; i < chunk; i++) {
3197 kiblnd_check_conns(peer_index);
3198 peer_index = (peer_index + 1) %
3199 kiblnd_data.kib_peer_hash_size;
3202 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3203 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3209 /* Nothing to do for 'timeout' */
3210 set_current_state(TASK_INTERRUPTIBLE);
3211 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3212 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3214 schedule_timeout(timeout);
3216 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3217 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3220 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3222 kiblnd_thread_fini();
3227 kiblnd_qp_event(struct ib_event *event, void *arg)
3229 kib_conn_t *conn = arg;
3231 switch (event->event) {
3232 case IB_EVENT_COMM_EST:
3233 CDEBUG(D_NET, "%s established\n",
3234 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3238 CERROR("%s: Async QP event type %d\n",
3239 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3245 kiblnd_complete(struct ib_wc *wc)
3247 switch (kiblnd_wreqid2type(wc->wr_id)) {
3251 case IBLND_WID_RDMA:
3253 * We only get RDMA completion notification if it fails. All
3254 * subsequent work items, including the final SEND will fail
3255 * too. However we can't print out any more info about the
3256 * failing RDMA because 'tx' might be back on the idle list or
3257 * even reused already if we didn't manage to post all our work
3260 CNETERR("RDMA (tx: %p) failed: %d\n",
3261 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3265 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3269 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3276 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3279 * NB I'm not allowed to schedule this conn once its refcount has
3280 * reached 0. Since fundamentally I'm racing with scheduler threads
3281 * consuming my CQ I could be called after all completions have
3282 * occurred. But in this case, !ibc_nrx && !ibc_nsends_posted
3283 * and this CQ is about to be destroyed so I NOOP.
3285 kib_conn_t *conn = arg;
3286 struct kib_sched_info *sched = conn->ibc_sched;
3287 unsigned long flags;
3289 LASSERT(cq == conn->ibc_cq);
3291 spin_lock_irqsave(&sched->ibs_lock, flags);
3293 conn->ibc_ready = 1;
3295 if (!conn->ibc_scheduled &&
3296 (conn->ibc_nrx > 0 ||
3297 conn->ibc_nsends_posted > 0)) {
3298 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3299 conn->ibc_scheduled = 1;
3300 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3302 if (waitqueue_active(&sched->ibs_waitq))
3303 wake_up(&sched->ibs_waitq);
3306 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3310 kiblnd_cq_event(struct ib_event *event, void *arg)
3312 kib_conn_t *conn = arg;
3314 CERROR("%s: async CQ event type %d\n",
3315 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3319 kiblnd_scheduler(void *arg)
3321 long id = (long)arg;
3322 struct kib_sched_info *sched;
3325 unsigned long flags;
3331 cfs_block_allsigs();
3333 init_waitqueue_entry(&wait, current);
3335 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3337 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3339 CWARN("Failed to bind on CPT %d, please verify whether all CPUs are healthy and reload modules if necessary, otherwise your system might under risk of low performance\n",
3343 spin_lock_irqsave(&sched->ibs_lock, flags);
3345 while (!kiblnd_data.kib_shutdown) {
3346 if (busy_loops++ >= IBLND_RESCHED) {
3347 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3352 spin_lock_irqsave(&sched->ibs_lock, flags);
3357 if (!list_empty(&sched->ibs_conns)) {
3358 conn = list_entry(sched->ibs_conns.next, kib_conn_t,
3360 /* take over kib_sched_conns' ref on conn... */
3361 LASSERT(conn->ibc_scheduled);
3362 list_del(&conn->ibc_sched_list);
3363 conn->ibc_ready = 0;
3365 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3367 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3369 rc = ib_req_notify_cq(conn->ibc_cq,
3372 CWARN("%s: ib_req_notify_cq failed: %d, closing connection\n",
3373 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3374 kiblnd_close_conn(conn, -EIO);
3375 kiblnd_conn_decref(conn);
3376 spin_lock_irqsave(&sched->ibs_lock,
3381 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3385 CWARN("%s: ib_poll_cq failed: %d, closing connection\n",
3386 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3388 kiblnd_close_conn(conn, -EIO);
3389 kiblnd_conn_decref(conn);
3390 spin_lock_irqsave(&sched->ibs_lock, flags);
3394 spin_lock_irqsave(&sched->ibs_lock, flags);
3396 if (rc || conn->ibc_ready) {
3398 * There may be another completion waiting; get
3399 * another scheduler to check while I handle
3402 /* +1 ref for sched_conns */
3403 kiblnd_conn_addref(conn);
3404 list_add_tail(&conn->ibc_sched_list,
3406 if (waitqueue_active(&sched->ibs_waitq))
3407 wake_up(&sched->ibs_waitq);
3409 conn->ibc_scheduled = 0;
3413 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3414 kiblnd_complete(&wc);
3416 spin_lock_irqsave(&sched->ibs_lock, flags);
3419 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3426 set_current_state(TASK_INTERRUPTIBLE);
3427 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3428 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3433 remove_wait_queue(&sched->ibs_waitq, &wait);
3434 spin_lock_irqsave(&sched->ibs_lock, flags);
3437 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3439 kiblnd_thread_fini();
3444 kiblnd_failover_thread(void *arg)
3446 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3449 unsigned long flags;
3452 LASSERT(*kiblnd_tunables.kib_dev_failover);
3454 cfs_block_allsigs();
3456 init_waitqueue_entry(&wait, current);
3457 write_lock_irqsave(glock, flags);
3459 while (!kiblnd_data.kib_shutdown) {
3460 int do_failover = 0;
3463 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3465 if (time_before(cfs_time_current(),
3466 dev->ibd_next_failover))
3473 list_del_init(&dev->ibd_fail_list);
3474 dev->ibd_failover = 1;
3475 write_unlock_irqrestore(glock, flags);
3477 rc = kiblnd_dev_failover(dev);
3479 write_lock_irqsave(glock, flags);
3481 LASSERT(dev->ibd_failover);
3482 dev->ibd_failover = 0;
3483 if (rc >= 0) { /* Device is OK or failover succeed */
3484 dev->ibd_next_failover = cfs_time_shift(3);
3488 /* failed to failover, retry later */
3489 dev->ibd_next_failover =
3490 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3491 if (kiblnd_dev_can_failover(dev)) {
3492 list_add_tail(&dev->ibd_fail_list,
3493 &kiblnd_data.kib_failed_devs);
3499 /* long sleep if no more pending failover */
3500 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3502 set_current_state(TASK_INTERRUPTIBLE);
3503 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3504 write_unlock_irqrestore(glock, flags);
3506 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3507 cfs_time_seconds(1));
3508 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3509 write_lock_irqsave(glock, flags);
3511 if (!long_sleep || rc)
3515 * have a long sleep, routine check all active devices,
3516 * we need checking like this because if there is not active
3517 * connection on the dev and no SEND from local, we may listen
3518 * on wrong HCA for ever while there is a bonding failover
3520 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3521 if (kiblnd_dev_can_failover(dev)) {
3522 list_add_tail(&dev->ibd_fail_list,
3523 &kiblnd_data.kib_failed_devs);
3528 write_unlock_irqrestore(glock, flags);
3530 kiblnd_thread_fini();