1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
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>
44 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
46 lnet_msg_t *lntmsg[2];
47 kib_net_t *net = ni->ni_data;
51 LASSERT (net != NULL);
52 LASSERT (!cfs_in_interrupt());
53 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
54 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
55 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
56 LASSERT (tx->tx_pool != NULL);
58 kiblnd_unmap_tx(ni, tx);
60 /* tx may have up to 2 lnet msgs to finalise */
61 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
62 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
65 if (tx->tx_conn != NULL) {
66 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
68 kiblnd_conn_decref(tx->tx_conn);
75 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
77 /* delay finalize until my descs have been freed */
78 for (i = 0; i < 2; i++) {
79 if (lntmsg[i] == NULL)
82 lnet_finalize(ni, lntmsg[i], rc);
87 kiblnd_txlist_done (lnet_ni_t *ni, cfs_list_t *txlist, int status)
91 while (!cfs_list_empty (txlist)) {
92 tx = cfs_list_entry (txlist->next, kib_tx_t, tx_list);
94 cfs_list_del(&tx->tx_list);
97 tx->tx_status = status;
98 kiblnd_tx_done(ni, tx);
103 kiblnd_get_idle_tx (lnet_ni_t *ni)
105 kib_net_t *net = (kib_net_t *)ni->ni_data;
109 node = kiblnd_pool_alloc_node(&net->ibn_tx_ps.tps_poolset);
112 tx = container_of(node, kib_tx_t, tx_list);
114 LASSERT (tx->tx_nwrq == 0);
115 LASSERT (!tx->tx_queued);
116 LASSERT (tx->tx_sending == 0);
117 LASSERT (!tx->tx_waiting);
118 LASSERT (tx->tx_status == 0);
119 LASSERT (tx->tx_conn == NULL);
120 LASSERT (tx->tx_lntmsg[0] == NULL);
121 LASSERT (tx->tx_lntmsg[1] == NULL);
122 LASSERT (tx->tx_u.pmr == NULL);
123 LASSERT (tx->tx_nfrags == 0);
129 kiblnd_drop_rx (kib_rx_t *rx)
131 kib_conn_t *conn = rx->rx_conn;
134 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
135 LASSERT (conn->ibc_nrx > 0);
137 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
139 kiblnd_conn_decref(conn);
143 kiblnd_post_rx (kib_rx_t *rx, int credit)
145 kib_conn_t *conn = rx->rx_conn;
146 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
147 struct ib_recv_wr *bad_wrq = NULL;
151 LASSERT (net != NULL);
152 LASSERT (!cfs_in_interrupt());
153 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
154 credit == IBLND_POSTRX_PEER_CREDIT ||
155 credit == IBLND_POSTRX_RSRVD_CREDIT);
157 mr = kiblnd_find_dma_mr(conn->ibc_hdev, rx->rx_msgaddr, IBLND_MSG_SIZE);
158 LASSERT (mr != NULL);
160 rx->rx_sge.lkey = mr->lkey;
161 rx->rx_sge.addr = rx->rx_msgaddr;
162 rx->rx_sge.length = IBLND_MSG_SIZE;
164 rx->rx_wrq.next = NULL;
165 rx->rx_wrq.sg_list = &rx->rx_sge;
166 rx->rx_wrq.num_sge = 1;
167 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
169 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
170 LASSERT (rx->rx_nob >= 0); /* not posted */
172 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
173 kiblnd_drop_rx(rx); /* No more posts for this rx */
177 rx->rx_nob = -1; /* flag posted */
179 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
181 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
182 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
186 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
190 kiblnd_close_conn(conn, rc);
191 kiblnd_drop_rx(rx); /* No more posts for this rx */
195 if (credit == IBLND_POSTRX_NO_CREDIT)
198 cfs_spin_lock(&conn->ibc_lock);
199 if (credit == IBLND_POSTRX_PEER_CREDIT)
200 conn->ibc_outstanding_credits++;
202 conn->ibc_reserved_credits++;
203 cfs_spin_unlock(&conn->ibc_lock);
205 kiblnd_check_sends(conn);
210 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
214 cfs_list_for_each(tmp, &conn->ibc_active_txs) {
215 kib_tx_t *tx = cfs_list_entry(tmp, kib_tx_t, tx_list);
217 LASSERT (!tx->tx_queued);
218 LASSERT (tx->tx_sending != 0 || tx->tx_waiting);
220 if (tx->tx_cookie != cookie)
223 if (tx->tx_waiting &&
224 tx->tx_msg->ibm_type == txtype)
227 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
228 tx->tx_waiting ? "" : "NOT ",
229 tx->tx_msg->ibm_type, txtype);
235 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
238 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
241 cfs_spin_lock(&conn->ibc_lock);
243 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
245 cfs_spin_unlock(&conn->ibc_lock);
247 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
248 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
249 kiblnd_close_conn(conn, -EPROTO);
253 if (tx->tx_status == 0) { /* success so far */
254 if (status < 0) { /* failed? */
255 tx->tx_status = status;
256 } else if (txtype == IBLND_MSG_GET_REQ) {
257 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
263 idle = !tx->tx_queued && (tx->tx_sending == 0);
265 cfs_list_del(&tx->tx_list);
267 cfs_spin_unlock(&conn->ibc_lock);
270 kiblnd_tx_done(ni, tx);
274 kiblnd_send_completion (kib_conn_t *conn, int type, int status, __u64 cookie)
276 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
277 kib_tx_t *tx = kiblnd_get_idle_tx(ni);
280 CERROR("Can't get tx for completion %x for %s\n",
281 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
285 tx->tx_msg->ibm_u.completion.ibcm_status = status;
286 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
287 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
289 kiblnd_queue_tx(tx, conn);
293 kiblnd_handle_rx (kib_rx_t *rx)
295 kib_msg_t *msg = rx->rx_msg;
296 kib_conn_t *conn = rx->rx_conn;
297 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
298 int credits = msg->ibm_credits;
304 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
306 CDEBUG (D_NET, "Received %x[%d] from %s\n",
307 msg->ibm_type, credits,
308 libcfs_nid2str(conn->ibc_peer->ibp_nid));
311 /* Have I received credits that will let me send? */
312 cfs_spin_lock(&conn->ibc_lock);
314 if (conn->ibc_credits + credits >
315 IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
316 rc2 = conn->ibc_credits;
317 cfs_spin_unlock(&conn->ibc_lock);
319 CERROR("Bad credits from %s: %d + %d > %d\n",
320 libcfs_nid2str(conn->ibc_peer->ibp_nid),
322 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
324 kiblnd_close_conn(conn, -EPROTO);
325 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
329 conn->ibc_credits += credits;
331 /* This ensures the credit taken by NOOP can be returned */
332 if (msg->ibm_type == IBLND_MSG_NOOP &&
333 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
334 conn->ibc_outstanding_credits++;
336 cfs_spin_unlock(&conn->ibc_lock);
337 kiblnd_check_sends(conn);
340 switch (msg->ibm_type) {
342 CERROR("Bad IBLND message type %x from %s\n",
343 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
344 post_credit = IBLND_POSTRX_NO_CREDIT;
349 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
350 post_credit = IBLND_POSTRX_NO_CREDIT;
354 if (credits != 0) /* credit already posted */
355 post_credit = IBLND_POSTRX_NO_CREDIT;
356 else /* a keepalive NOOP */
357 post_credit = IBLND_POSTRX_PEER_CREDIT;
360 case IBLND_MSG_IMMEDIATE:
361 post_credit = IBLND_POSTRX_DONT_POST;
362 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
363 msg->ibm_srcnid, rx, 0);
364 if (rc < 0) /* repost on error */
365 post_credit = IBLND_POSTRX_PEER_CREDIT;
368 case IBLND_MSG_PUT_REQ:
369 post_credit = IBLND_POSTRX_DONT_POST;
370 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
371 msg->ibm_srcnid, rx, 1);
372 if (rc < 0) /* repost on error */
373 post_credit = IBLND_POSTRX_PEER_CREDIT;
376 case IBLND_MSG_PUT_NAK:
377 CWARN ("PUT_NACK from %s\n",
378 libcfs_nid2str(conn->ibc_peer->ibp_nid));
379 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
380 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
381 msg->ibm_u.completion.ibcm_status,
382 msg->ibm_u.completion.ibcm_cookie);
385 case IBLND_MSG_PUT_ACK:
386 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
388 cfs_spin_lock(&conn->ibc_lock);
389 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
390 msg->ibm_u.putack.ibpam_src_cookie);
392 cfs_list_del(&tx->tx_list);
393 cfs_spin_unlock(&conn->ibc_lock);
396 CERROR("Unmatched PUT_ACK from %s\n",
397 libcfs_nid2str(conn->ibc_peer->ibp_nid));
402 LASSERT (tx->tx_waiting);
403 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
404 * (a) I can overwrite tx_msg since my peer has received it!
405 * (b) tx_waiting set tells tx_complete() it's not done. */
407 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
409 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
410 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
411 &msg->ibm_u.putack.ibpam_rd,
412 msg->ibm_u.putack.ibpam_dst_cookie);
414 CERROR("Can't setup rdma for PUT to %s: %d\n",
415 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
417 cfs_spin_lock(&conn->ibc_lock);
418 tx->tx_waiting = 0; /* clear waiting and queue atomically */
419 kiblnd_queue_tx_locked(tx, conn);
420 cfs_spin_unlock(&conn->ibc_lock);
423 case IBLND_MSG_PUT_DONE:
424 post_credit = IBLND_POSTRX_PEER_CREDIT;
425 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
426 msg->ibm_u.completion.ibcm_status,
427 msg->ibm_u.completion.ibcm_cookie);
430 case IBLND_MSG_GET_REQ:
431 post_credit = IBLND_POSTRX_DONT_POST;
432 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
433 msg->ibm_srcnid, rx, 1);
434 if (rc < 0) /* repost on error */
435 post_credit = IBLND_POSTRX_PEER_CREDIT;
438 case IBLND_MSG_GET_DONE:
439 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
440 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
441 msg->ibm_u.completion.ibcm_status,
442 msg->ibm_u.completion.ibcm_cookie);
446 if (rc < 0) /* protocol error */
447 kiblnd_close_conn(conn, rc);
449 if (post_credit != IBLND_POSTRX_DONT_POST)
450 kiblnd_post_rx(rx, post_credit);
454 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
456 kib_msg_t *msg = rx->rx_msg;
457 kib_conn_t *conn = rx->rx_conn;
458 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
459 kib_net_t *net = ni->ni_data;
463 LASSERT (net != NULL);
464 LASSERT (rx->rx_nob < 0); /* was posted */
465 rx->rx_nob = 0; /* isn't now */
467 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
470 if (status != IB_WC_SUCCESS) {
471 CNETERR("Rx from %s failed: %d\n",
472 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
479 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
481 CERROR ("Error %d unpacking rx from %s\n",
482 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
486 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
487 msg->ibm_dstnid != ni->ni_nid ||
488 msg->ibm_srcstamp != conn->ibc_incarnation ||
489 msg->ibm_dststamp != net->ibn_incarnation) {
490 CERROR ("Stale rx from %s\n",
491 libcfs_nid2str(conn->ibc_peer->ibp_nid));
496 /* set time last known alive */
497 kiblnd_peer_alive(conn->ibc_peer);
499 /* racing with connection establishment/teardown! */
501 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
502 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
505 cfs_write_lock_irqsave(g_lock, flags);
506 /* must check holding global lock to eliminate race */
507 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
508 cfs_list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
509 cfs_write_unlock_irqrestore(g_lock, flags);
512 cfs_write_unlock_irqrestore(g_lock, flags);
514 kiblnd_handle_rx(rx);
518 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
519 kiblnd_close_conn(conn, err);
521 kiblnd_drop_rx(rx); /* Don't re-post rx. */
525 kiblnd_kvaddr_to_page (unsigned long vaddr)
529 if (vaddr >= VMALLOC_START &&
530 vaddr < VMALLOC_END) {
531 page = vmalloc_to_page ((void *)vaddr);
532 LASSERT (page != NULL);
535 #ifdef CONFIG_HIGHMEM
536 if (vaddr >= PKMAP_BASE &&
537 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
538 /* No highmem pages only used for bulk (kiov) I/O */
539 CERROR("find page for address in highmem\n");
543 page = virt_to_page (vaddr);
544 LASSERT (page != NULL);
549 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
551 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
552 __u64 *pages = tx->tx_pages;
558 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
559 for (size = 0; size < rd->rd_frags[i].rf_nob;
560 size += hdev->ibh_page_size) {
561 pages[npages ++] = (rd->rd_frags[i].rf_addr &
562 hdev->ibh_page_mask) + size;
566 rc = kiblnd_fmr_pool_map(&net->ibn_fmr_ps, pages, npages, 0, &tx->tx_u.fmr);
568 CERROR ("Can't map %d pages: %d\n", npages, rc);
572 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
574 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.fmr.fmr_pfmr->fmr->rkey :
575 tx->tx_u.fmr.fmr_pfmr->fmr->lkey;
576 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
577 rd->rd_frags[0].rf_nob = nob;
584 kiblnd_pmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
586 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
590 iova = rd->rd_frags[0].rf_addr & ~hdev->ibh_page_mask;
592 rc = kiblnd_pmr_pool_map(&net->ibn_pmr_ps, hdev, rd, &iova, &tx->tx_u.pmr);
594 CERROR("Failed to create MR by phybuf: %d\n", rc);
598 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
600 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.pmr->pmr_mr->rkey :
601 tx->tx_u.pmr->pmr_mr->lkey;
603 rd->rd_frags[0].rf_addr = iova;
604 rd->rd_frags[0].rf_nob = nob;
610 kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
612 kib_net_t *net = ni->ni_data;
614 LASSERT (net != NULL);
616 if (net->ibn_with_fmr && tx->tx_u.fmr.fmr_pfmr != NULL) {
617 kiblnd_fmr_pool_unmap(&tx->tx_u.fmr, tx->tx_status);
618 tx->tx_u.fmr.fmr_pfmr = NULL;
619 } else if (net->ibn_with_pmr && tx->tx_u.pmr != NULL) {
620 kiblnd_pmr_pool_unmap(tx->tx_u.pmr);
624 if (tx->tx_nfrags != 0) {
625 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
626 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
632 kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx,
633 kib_rdma_desc_t *rd, int nfrags)
635 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
636 kib_net_t *net = ni->ni_data;
637 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
643 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
644 tx->tx_nfrags = nfrags;
647 kiblnd_dma_map_sg(hdev->ibh_ibdev,
648 tx->tx_frags, 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;
666 if (net->ibn_with_fmr)
667 return kiblnd_fmr_map_tx(net, tx, rd, nob);
668 else if (net->ibn_with_pmr)
669 return kiblnd_pmr_map_tx(net, tx, rd, nob);
676 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
677 unsigned int niov, struct iovec *iov, int offset, int nob)
679 kib_net_t *net = ni->ni_data;
681 struct scatterlist *sg;
688 LASSERT (net != NULL);
690 while (offset >= iov->iov_len) {
691 offset -= iov->iov_len;
701 vaddr = ((unsigned long)iov->iov_base) + offset;
702 page_offset = vaddr & (PAGE_SIZE - 1);
703 page = kiblnd_kvaddr_to_page(vaddr);
705 CERROR ("Can't find page\n");
709 fragnob = min((int)(iov->iov_len - offset), nob);
710 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
712 sg_set_page(sg, page, fragnob, page_offset);
715 if (offset + fragnob < iov->iov_len) {
725 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
729 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
730 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
732 kib_net_t *net = ni->ni_data;
733 struct scatterlist *sg;
736 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
740 LASSERT (net != NULL);
742 while (offset >= kiov->kiov_len) {
743 offset -= kiov->kiov_len;
753 fragnob = min((int)(kiov->kiov_len - offset), nob);
755 memset(sg, 0, sizeof(*sg));
756 sg_set_page(sg, kiov->kiov_page, fragnob,
757 kiov->kiov_offset + offset);
766 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
770 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
772 kib_msg_t *msg = tx->tx_msg;
773 kib_peer_t *peer = conn->ibc_peer;
774 int ver = conn->ibc_version;
777 struct ib_send_wr *bad_wrq;
779 LASSERT (tx->tx_queued);
780 /* We rely on this for QP sizing */
781 LASSERT (tx->tx_nwrq > 0);
782 LASSERT (tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
784 LASSERT (credit == 0 || credit == 1);
785 LASSERT (conn->ibc_outstanding_credits >= 0);
786 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
787 LASSERT (conn->ibc_credits >= 0);
788 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
790 if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
791 /* tx completions outstanding... */
792 CDEBUG(D_NET, "%s: posted enough\n",
793 libcfs_nid2str(peer->ibp_nid));
797 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
798 CDEBUG(D_NET, "%s: no credits\n",
799 libcfs_nid2str(peer->ibp_nid));
803 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
804 conn->ibc_credits == 1 && /* last credit reserved */
805 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
806 CDEBUG(D_NET, "%s: not using last credit\n",
807 libcfs_nid2str(peer->ibp_nid));
811 /* NB don't drop ibc_lock before bumping tx_sending */
812 cfs_list_del(&tx->tx_list);
815 if (msg->ibm_type == IBLND_MSG_NOOP &&
816 (!kiblnd_send_noop(conn) || /* redundant NOOP */
817 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
818 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
819 /* OK to drop when posted enough NOOPs, since
820 * kiblnd_check_sends will queue NOOP again when
821 * posted NOOPs complete */
822 cfs_spin_unlock(&conn->ibc_lock);
823 kiblnd_tx_done(peer->ibp_ni, tx);
824 cfs_spin_lock(&conn->ibc_lock);
825 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
826 libcfs_nid2str(peer->ibp_nid),
827 conn->ibc_noops_posted);
831 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
832 peer->ibp_nid, conn->ibc_incarnation);
834 conn->ibc_credits -= credit;
835 conn->ibc_outstanding_credits = 0;
836 conn->ibc_nsends_posted++;
837 if (msg->ibm_type == IBLND_MSG_NOOP)
838 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. */
847 cfs_list_add(&tx->tx_list, &conn->ibc_active_txs);
849 /* I'm still holding ibc_lock! */
850 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
852 } else if (tx->tx_pool->tpo_pool.po_failed ||
853 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
854 /* close_conn will launch failover */
857 rc = ib_post_send(conn->ibc_cmid->qp,
858 tx->tx_wrq, &bad_wrq);
861 conn->ibc_last_send = jiffies;
866 /* NB credits are transferred in the actual
867 * message, which can only be the last work item */
868 conn->ibc_credits += credit;
869 conn->ibc_outstanding_credits += msg->ibm_credits;
870 conn->ibc_nsends_posted--;
871 if (msg->ibm_type == IBLND_MSG_NOOP)
872 conn->ibc_noops_posted--;
878 done = (tx->tx_sending == 0);
880 cfs_list_del(&tx->tx_list);
882 cfs_spin_unlock(&conn->ibc_lock);
884 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
885 CERROR("Error %d posting transmit to %s\n",
886 rc, libcfs_nid2str(peer->ibp_nid));
888 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
889 rc, libcfs_nid2str(peer->ibp_nid));
891 kiblnd_close_conn(conn, rc);
894 kiblnd_tx_done(peer->ibp_ni, tx);
896 cfs_spin_lock(&conn->ibc_lock);
902 kiblnd_check_sends (kib_conn_t *conn)
904 int ver = conn->ibc_version;
905 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
908 /* Don't send anything until after the connection is established */
909 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
910 CDEBUG(D_NET, "%s too soon\n",
911 libcfs_nid2str(conn->ibc_peer->ibp_nid));
915 cfs_spin_lock(&conn->ibc_lock);
917 LASSERT (conn->ibc_nsends_posted <= IBLND_CONCURRENT_SENDS(ver));
918 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
919 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
920 LASSERT (conn->ibc_reserved_credits >= 0);
922 while (conn->ibc_reserved_credits > 0 &&
923 !cfs_list_empty(&conn->ibc_tx_queue_rsrvd)) {
924 tx = cfs_list_entry(conn->ibc_tx_queue_rsrvd.next,
926 cfs_list_del(&tx->tx_list);
927 cfs_list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
928 conn->ibc_reserved_credits--;
931 if (kiblnd_send_noop(conn)) {
932 cfs_spin_unlock(&conn->ibc_lock);
934 tx = kiblnd_get_idle_tx(ni);
936 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
938 cfs_spin_lock(&conn->ibc_lock);
940 kiblnd_queue_tx_locked(tx, conn);
943 kiblnd_conn_addref(conn); /* 1 ref for me.... (see b21911) */
948 if (!cfs_list_empty(&conn->ibc_tx_queue_nocred)) {
950 tx = cfs_list_entry(conn->ibc_tx_queue_nocred.next,
952 } else if (!cfs_list_empty(&conn->ibc_tx_noops)) {
953 LASSERT (!IBLND_OOB_CAPABLE(ver));
955 tx = cfs_list_entry(conn->ibc_tx_noops.next,
957 } else if (!cfs_list_empty(&conn->ibc_tx_queue)) {
959 tx = cfs_list_entry(conn->ibc_tx_queue.next,
964 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
968 cfs_spin_unlock(&conn->ibc_lock);
970 kiblnd_conn_decref(conn); /* ...until here */
974 kiblnd_tx_complete (kib_tx_t *tx, int status)
976 int failed = (status != IB_WC_SUCCESS);
977 kib_conn_t *conn = tx->tx_conn;
980 LASSERT (tx->tx_sending > 0);
983 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
984 CNETERR("Tx -> %s cookie "LPX64
985 " sending %d waiting %d: failed %d\n",
986 libcfs_nid2str(conn->ibc_peer->ibp_nid),
987 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
990 kiblnd_close_conn(conn, -EIO);
992 kiblnd_peer_alive(conn->ibc_peer);
995 cfs_spin_lock(&conn->ibc_lock);
997 /* I could be racing with rdma completion. Whoever makes 'tx' idle
998 * gets to free it, which also drops its ref on 'conn'. */
1001 conn->ibc_nsends_posted--;
1002 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1003 conn->ibc_noops_posted--;
1006 tx->tx_waiting = 0; /* don't wait for peer */
1007 tx->tx_status = -EIO;
1010 idle = (tx->tx_sending == 0) && /* This is the final callback */
1011 !tx->tx_waiting && /* Not waiting for peer */
1012 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1014 cfs_list_del(&tx->tx_list);
1016 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1018 cfs_spin_unlock(&conn->ibc_lock);
1021 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1023 kiblnd_check_sends(conn);
1025 kiblnd_conn_decref(conn); /* ...until here */
1029 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1031 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
1032 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1033 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1034 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1037 LASSERT (tx->tx_nwrq >= 0);
1038 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1039 LASSERT (nob <= IBLND_MSG_SIZE);
1041 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1043 mr = kiblnd_find_dma_mr(hdev, tx->tx_msgaddr, nob);
1044 LASSERT (mr != NULL);
1046 sge->lkey = mr->lkey;
1047 sge->addr = tx->tx_msgaddr;
1050 memset(wrq, 0, sizeof(*wrq));
1053 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1056 wrq->opcode = IB_WR_SEND;
1057 wrq->send_flags = IB_SEND_SIGNALED;
1063 kiblnd_init_rdma (kib_conn_t *conn, kib_tx_t *tx, int type,
1064 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1066 kib_msg_t *ibmsg = tx->tx_msg;
1067 kib_rdma_desc_t *srcrd = tx->tx_rd;
1068 struct ib_sge *sge = &tx->tx_sge[0];
1069 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1075 LASSERT (!cfs_in_interrupt());
1076 LASSERT (tx->tx_nwrq == 0);
1077 LASSERT (type == IBLND_MSG_GET_DONE ||
1078 type == IBLND_MSG_PUT_DONE);
1080 srcidx = dstidx = 0;
1083 if (srcidx >= srcrd->rd_nfrags) {
1084 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1089 if (dstidx == dstrd->rd_nfrags) {
1090 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1095 if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
1096 CERROR("RDMA too fragmented for %s (%d): "
1097 "%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)), resid);
1109 sge = &tx->tx_sge[tx->tx_nwrq];
1110 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1111 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1112 sge->length = wrknob;
1114 wrq = &tx->tx_wrq[tx->tx_nwrq];
1116 wrq->next = wrq + 1;
1117 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1120 wrq->opcode = IB_WR_RDMA_WRITE;
1121 wrq->send_flags = 0;
1123 wrq->wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1124 wrq->wr.rdma.rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1126 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1127 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1136 if (rc < 0) /* no RDMA if completing with failure */
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)
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 + (*kiblnd_tunables.kib_timeout * CFS_HZ);
1159 if (tx->tx_conn == NULL) {
1160 kiblnd_conn_addref(conn);
1162 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1164 /* PUT_DONE first attached to conn as a PUT_REQ */
1165 LASSERT (tx->tx_conn == conn);
1166 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1169 switch (tx->tx_msg->ibm_type) {
1173 case IBLND_MSG_PUT_REQ:
1174 case IBLND_MSG_GET_REQ:
1175 q = &conn->ibc_tx_queue_rsrvd;
1178 case IBLND_MSG_PUT_NAK:
1179 case IBLND_MSG_PUT_ACK:
1180 case IBLND_MSG_PUT_DONE:
1181 case IBLND_MSG_GET_DONE:
1182 q = &conn->ibc_tx_queue_nocred;
1185 case IBLND_MSG_NOOP:
1186 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1187 q = &conn->ibc_tx_queue_nocred;
1189 q = &conn->ibc_tx_noops;
1192 case IBLND_MSG_IMMEDIATE:
1193 q = &conn->ibc_tx_queue;
1197 cfs_list_add_tail(&tx->tx_list, q);
1201 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1203 cfs_spin_lock(&conn->ibc_lock);
1204 kiblnd_queue_tx_locked(tx, conn);
1205 cfs_spin_unlock(&conn->ibc_lock);
1207 kiblnd_check_sends(conn);
1211 kiblnd_connect_peer (kib_peer_t *peer)
1213 struct rdma_cm_id *cmid;
1215 kib_net_t *net = peer->ibp_ni->ni_data;
1216 struct sockaddr_in srcaddr;
1217 struct sockaddr_in dstaddr;
1220 LASSERT (net != NULL);
1221 LASSERT (peer->ibp_connecting > 0);
1223 cmid = rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP);
1225 CERROR("Can't create CMID for %s: %ld\n",
1226 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1232 memset(&srcaddr, 0, sizeof(srcaddr));
1233 srcaddr.sin_family = AF_INET;
1234 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1236 memset(&dstaddr, 0, sizeof(dstaddr));
1237 dstaddr.sin_family = AF_INET;
1238 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1239 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1241 kiblnd_peer_addref(peer); /* cmid's ref */
1243 rc = rdma_resolve_addr(cmid,
1244 (struct sockaddr *)&srcaddr,
1245 (struct sockaddr *)&dstaddr,
1246 *kiblnd_tunables.kib_timeout * 1000);
1248 LASSERT (cmid->device != NULL);
1249 CDEBUG(D_NET, "%s: connection bound to %s:%u.%u.%u.%u:%s\n",
1250 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1251 HIPQUAD(dev->ibd_ifip), cmid->device->name);
1255 /* Can't initiate address resolution: */
1256 CERROR("Can't resolve addr for %s: %d\n",
1257 libcfs_nid2str(peer->ibp_nid), rc);
1259 kiblnd_peer_decref(peer); /* cmid's ref */
1260 rdma_destroy_id(cmid);
1262 kiblnd_peer_connect_failed(peer, 1, rc);
1266 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1271 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1272 unsigned long flags;
1275 /* If I get here, I've committed to send, so I complete the tx with
1276 * failure on any problems */
1278 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1279 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1281 /* First time, just use a read lock since I expect to find my peer
1283 cfs_read_lock_irqsave(g_lock, flags);
1285 peer = kiblnd_find_peer_locked(nid);
1286 if (peer != NULL && !cfs_list_empty(&peer->ibp_conns)) {
1287 /* Found a peer with an established connection */
1288 conn = kiblnd_get_conn_locked(peer);
1289 kiblnd_conn_addref(conn); /* 1 ref for me... */
1291 cfs_read_unlock_irqrestore(g_lock, flags);
1294 kiblnd_queue_tx(tx, conn);
1295 kiblnd_conn_decref(conn); /* ...to here */
1299 cfs_read_unlock(g_lock);
1300 /* Re-try with a write lock */
1301 cfs_write_lock(g_lock);
1303 peer = kiblnd_find_peer_locked(nid);
1305 if (cfs_list_empty(&peer->ibp_conns)) {
1306 /* found a peer, but it's still connecting... */
1307 LASSERT (peer->ibp_connecting != 0 ||
1308 peer->ibp_accepting != 0);
1310 cfs_list_add_tail(&tx->tx_list,
1311 &peer->ibp_tx_queue);
1312 cfs_write_unlock_irqrestore(g_lock, flags);
1314 conn = kiblnd_get_conn_locked(peer);
1315 kiblnd_conn_addref(conn); /* 1 ref for me... */
1317 cfs_write_unlock_irqrestore(g_lock, flags);
1320 kiblnd_queue_tx(tx, conn);
1321 kiblnd_conn_decref(conn); /* ...to here */
1326 cfs_write_unlock_irqrestore(g_lock, flags);
1328 /* Allocate a peer ready to add to the peer table and retry */
1329 rc = kiblnd_create_peer(ni, &peer, nid);
1331 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1333 tx->tx_status = -EHOSTUNREACH;
1335 kiblnd_tx_done(ni, tx);
1340 cfs_write_lock_irqsave(g_lock, flags);
1342 peer2 = kiblnd_find_peer_locked(nid);
1343 if (peer2 != NULL) {
1344 if (cfs_list_empty(&peer2->ibp_conns)) {
1345 /* found a peer, but it's still connecting... */
1346 LASSERT (peer2->ibp_connecting != 0 ||
1347 peer2->ibp_accepting != 0);
1349 cfs_list_add_tail(&tx->tx_list,
1350 &peer2->ibp_tx_queue);
1351 cfs_write_unlock_irqrestore(g_lock, flags);
1353 conn = kiblnd_get_conn_locked(peer2);
1354 kiblnd_conn_addref(conn); /* 1 ref for me... */
1356 cfs_write_unlock_irqrestore(g_lock, flags);
1359 kiblnd_queue_tx(tx, conn);
1360 kiblnd_conn_decref(conn); /* ...to here */
1363 kiblnd_peer_decref(peer);
1367 /* Brand new peer */
1368 LASSERT (peer->ibp_connecting == 0);
1369 peer->ibp_connecting = 1;
1371 /* always called with a ref on ni, which prevents ni being shutdown */
1372 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1375 cfs_list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1377 kiblnd_peer_addref(peer);
1378 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1380 cfs_write_unlock_irqrestore(g_lock, flags);
1382 kiblnd_connect_peer(peer);
1383 kiblnd_peer_decref(peer);
1387 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1389 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1390 int type = lntmsg->msg_type;
1391 lnet_process_id_t target = lntmsg->msg_target;
1392 int target_is_router = lntmsg->msg_target_is_router;
1393 int routing = lntmsg->msg_routing;
1394 unsigned int payload_niov = lntmsg->msg_niov;
1395 struct iovec *payload_iov = lntmsg->msg_iov;
1396 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1397 unsigned int payload_offset = lntmsg->msg_offset;
1398 unsigned int payload_nob = lntmsg->msg_len;
1404 /* NB 'private' is different depending on what we're sending.... */
1406 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1407 payload_nob, payload_niov, libcfs_id2str(target));
1409 LASSERT (payload_nob == 0 || payload_niov > 0);
1410 LASSERT (payload_niov <= LNET_MAX_IOV);
1412 /* Thread context */
1413 LASSERT (!cfs_in_interrupt());
1414 /* payload is either all vaddrs or all pages */
1415 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1423 LASSERT (payload_nob == 0);
1427 if (routing || target_is_router)
1428 break; /* send IMMEDIATE */
1430 /* is the REPLY message too small for RDMA? */
1431 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1432 if (nob <= IBLND_MSG_SIZE)
1433 break; /* send IMMEDIATE */
1435 tx = kiblnd_get_idle_tx(ni);
1437 CERROR("Can't allocate txd for GET to %s: \n",
1438 libcfs_nid2str(target.nid));
1444 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1445 rc = kiblnd_setup_rd_iov(ni, tx,
1446 &ibmsg->ibm_u.get.ibgm_rd,
1447 lntmsg->msg_md->md_niov,
1448 lntmsg->msg_md->md_iov.iov,
1449 0, lntmsg->msg_md->md_length);
1451 rc = kiblnd_setup_rd_kiov(ni, tx,
1452 &ibmsg->ibm_u.get.ibgm_rd,
1453 lntmsg->msg_md->md_niov,
1454 lntmsg->msg_md->md_iov.kiov,
1455 0, lntmsg->msg_md->md_length);
1457 CERROR("Can't setup GET sink for %s: %d\n",
1458 libcfs_nid2str(target.nid), rc);
1459 kiblnd_tx_done(ni, tx);
1463 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1464 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1465 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1467 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1469 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1470 if (tx->tx_lntmsg[1] == NULL) {
1471 CERROR("Can't create reply for GET -> %s\n",
1472 libcfs_nid2str(target.nid));
1473 kiblnd_tx_done(ni, tx);
1477 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1478 tx->tx_waiting = 1; /* waiting for GET_DONE */
1479 kiblnd_launch_tx(ni, tx, target.nid);
1482 case LNET_MSG_REPLY:
1484 /* Is the payload small enough not to need RDMA? */
1485 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1486 if (nob <= IBLND_MSG_SIZE)
1487 break; /* send IMMEDIATE */
1489 tx = kiblnd_get_idle_tx(ni);
1491 CERROR("Can't allocate %s txd for %s\n",
1492 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1493 libcfs_nid2str(target.nid));
1497 if (payload_kiov == NULL)
1498 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1499 payload_niov, payload_iov,
1500 payload_offset, payload_nob);
1502 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1503 payload_niov, payload_kiov,
1504 payload_offset, payload_nob);
1506 CERROR("Can't setup PUT src for %s: %d\n",
1507 libcfs_nid2str(target.nid), rc);
1508 kiblnd_tx_done(ni, tx);
1513 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1514 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1515 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1517 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1518 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1519 kiblnd_launch_tx(ni, tx, target.nid);
1523 /* send IMMEDIATE */
1525 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1528 tx = kiblnd_get_idle_tx(ni);
1530 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1531 type, libcfs_nid2str(target.nid));
1536 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1538 if (payload_kiov != NULL)
1539 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1540 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1541 payload_niov, payload_kiov,
1542 payload_offset, payload_nob);
1544 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1545 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1546 payload_niov, payload_iov,
1547 payload_offset, payload_nob);
1549 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1550 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1552 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1553 kiblnd_launch_tx(ni, tx, target.nid);
1558 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1560 lnet_process_id_t target = lntmsg->msg_target;
1561 unsigned int niov = lntmsg->msg_niov;
1562 struct iovec *iov = lntmsg->msg_iov;
1563 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1564 unsigned int offset = lntmsg->msg_offset;
1565 unsigned int nob = lntmsg->msg_len;
1569 tx = kiblnd_get_idle_tx(ni);
1571 CERROR("Can't get tx for REPLY to %s\n",
1572 libcfs_nid2str(target.nid));
1578 else if (kiov == NULL)
1579 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1580 niov, iov, offset, nob);
1582 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1583 niov, kiov, offset, nob);
1586 CERROR("Can't setup GET src for %s: %d\n",
1587 libcfs_nid2str(target.nid), rc);
1591 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1592 IBLND_MSG_GET_DONE, nob,
1593 &rx->rx_msg->ibm_u.get.ibgm_rd,
1594 rx->rx_msg->ibm_u.get.ibgm_cookie);
1596 CERROR("Can't setup rdma for GET from %s: %d\n",
1597 libcfs_nid2str(target.nid), rc);
1602 /* No RDMA: local completion may happen now! */
1603 lnet_finalize(ni, lntmsg, 0);
1605 /* RDMA: lnet_finalize(lntmsg) when it
1607 tx->tx_lntmsg[0] = lntmsg;
1610 kiblnd_queue_tx(tx, rx->rx_conn);
1614 kiblnd_tx_done(ni, tx);
1616 lnet_finalize(ni, lntmsg, -EIO);
1620 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1621 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1622 unsigned int offset, unsigned int mlen, unsigned int rlen)
1624 kib_rx_t *rx = private;
1625 kib_msg_t *rxmsg = rx->rx_msg;
1626 kib_conn_t *conn = rx->rx_conn;
1630 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1633 LASSERT (mlen <= rlen);
1634 LASSERT (!cfs_in_interrupt());
1635 /* Either all pages or all vaddrs */
1636 LASSERT (!(kiov != NULL && iov != NULL));
1638 switch (rxmsg->ibm_type) {
1642 case IBLND_MSG_IMMEDIATE:
1643 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1644 if (nob > rx->rx_nob) {
1645 CERROR ("Immediate message from %s too big: %d(%d)\n",
1646 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1653 lnet_copy_flat2kiov(niov, kiov, offset,
1654 IBLND_MSG_SIZE, rxmsg,
1655 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1658 lnet_copy_flat2iov(niov, iov, offset,
1659 IBLND_MSG_SIZE, rxmsg,
1660 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1662 lnet_finalize (ni, lntmsg, 0);
1665 case IBLND_MSG_PUT_REQ:
1667 lnet_finalize(ni, lntmsg, 0);
1668 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1669 rxmsg->ibm_u.putreq.ibprm_cookie);
1673 tx = kiblnd_get_idle_tx(ni);
1675 CERROR("Can't allocate tx for %s\n",
1676 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1677 /* Not replying will break the connection */
1684 rc = kiblnd_setup_rd_iov(ni, tx,
1685 &txmsg->ibm_u.putack.ibpam_rd,
1686 niov, iov, offset, mlen);
1688 rc = kiblnd_setup_rd_kiov(ni, tx,
1689 &txmsg->ibm_u.putack.ibpam_rd,
1690 niov, kiov, offset, mlen);
1692 CERROR("Can't setup PUT sink for %s: %d\n",
1693 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1694 kiblnd_tx_done(ni, tx);
1695 /* tell peer it's over */
1696 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1697 rxmsg->ibm_u.putreq.ibprm_cookie);
1701 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1702 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1703 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1705 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1707 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1708 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1709 kiblnd_queue_tx(tx, conn);
1711 /* reposted buffer reserved for PUT_DONE */
1712 post_credit = IBLND_POSTRX_NO_CREDIT;
1715 case IBLND_MSG_GET_REQ:
1716 if (lntmsg != NULL) {
1717 /* Optimized GET; RDMA lntmsg's payload */
1718 kiblnd_reply(ni, rx, lntmsg);
1720 /* GET didn't match anything */
1721 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1723 rxmsg->ibm_u.get.ibgm_cookie);
1728 kiblnd_post_rx(rx, post_credit);
1733 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1735 long pid = cfs_kernel_thread (fn, arg, 0);
1740 cfs_atomic_inc (&kiblnd_data.kib_nthreads);
1745 kiblnd_thread_fini (void)
1747 cfs_atomic_dec (&kiblnd_data.kib_nthreads);
1751 kiblnd_peer_alive (kib_peer_t *peer)
1753 /* This is racy, but everyone's only writing cfs_time_current() */
1754 peer->ibp_last_alive = cfs_time_current();
1759 kiblnd_peer_notify (kib_peer_t *peer)
1762 cfs_time_t last_alive = 0;
1763 unsigned long flags;
1765 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1767 if (cfs_list_empty(&peer->ibp_conns) &&
1768 peer->ibp_accepting == 0 &&
1769 peer->ibp_connecting == 0 &&
1770 peer->ibp_error != 0) {
1771 error = peer->ibp_error;
1772 peer->ibp_error = 0;
1774 last_alive = peer->ibp_last_alive;
1777 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1780 lnet_notify(peer->ibp_ni,
1781 peer->ibp_nid, 0, last_alive);
1785 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1787 /* This just does the immediate housekeeping. 'error' is zero for a
1788 * normal shutdown which can happen only after the connection has been
1789 * established. If the connection is established, schedule the
1790 * connection to be finished off by the connd. Otherwise the connd is
1791 * already dealing with it (either to set it up or tear it down).
1792 * Caller holds kib_global_lock exclusively in irq context */
1793 kib_peer_t *peer = conn->ibc_peer;
1795 unsigned long flags;
1797 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1799 if (error != 0 && conn->ibc_comms_error == 0)
1800 conn->ibc_comms_error = error;
1802 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1803 return; /* already being handled */
1806 cfs_list_empty(&conn->ibc_tx_noops) &&
1807 cfs_list_empty(&conn->ibc_tx_queue) &&
1808 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) &&
1809 cfs_list_empty(&conn->ibc_tx_queue_nocred) &&
1810 cfs_list_empty(&conn->ibc_active_txs)) {
1811 CDEBUG(D_NET, "closing conn to %s\n",
1812 libcfs_nid2str(peer->ibp_nid));
1814 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1815 libcfs_nid2str(peer->ibp_nid), error,
1816 cfs_list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1817 cfs_list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1818 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1819 cfs_list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1820 cfs_list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1823 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1824 cfs_list_del(&conn->ibc_list);
1825 /* connd (see below) takes over ibc_list's ref */
1827 if (cfs_list_empty (&peer->ibp_conns) && /* no more conns */
1828 kiblnd_peer_active(peer)) { /* still in peer table */
1829 kiblnd_unlink_peer_locked(peer);
1831 /* set/clear error on last conn */
1832 peer->ibp_error = conn->ibc_comms_error;
1835 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1838 kiblnd_dev_can_failover(dev)) {
1839 cfs_list_add_tail(&dev->ibd_fail_list,
1840 &kiblnd_data.kib_failed_devs);
1841 cfs_waitq_signal(&kiblnd_data.kib_failover_waitq);
1844 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1846 cfs_list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1847 cfs_waitq_signal (&kiblnd_data.kib_connd_waitq);
1849 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1853 kiblnd_close_conn (kib_conn_t *conn, int error)
1855 unsigned long flags;
1857 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1859 kiblnd_close_conn_locked(conn, error);
1861 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1865 kiblnd_handle_early_rxs(kib_conn_t *conn)
1867 unsigned long flags;
1870 LASSERT (!cfs_in_interrupt());
1871 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1873 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1874 while (!cfs_list_empty(&conn->ibc_early_rxs)) {
1875 rx = cfs_list_entry(conn->ibc_early_rxs.next,
1877 cfs_list_del(&rx->rx_list);
1878 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
1881 kiblnd_handle_rx(rx);
1883 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1885 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1889 kiblnd_abort_txs(kib_conn_t *conn, cfs_list_t *txs)
1891 CFS_LIST_HEAD (zombies);
1896 cfs_spin_lock(&conn->ibc_lock);
1898 cfs_list_for_each_safe (tmp, nxt, txs) {
1899 tx = cfs_list_entry (tmp, kib_tx_t, tx_list);
1901 if (txs == &conn->ibc_active_txs) {
1902 LASSERT (!tx->tx_queued);
1903 LASSERT (tx->tx_waiting ||
1904 tx->tx_sending != 0);
1906 LASSERT (tx->tx_queued);
1909 tx->tx_status = -ECONNABORTED;
1912 if (tx->tx_sending == 0) {
1914 cfs_list_del (&tx->tx_list);
1915 cfs_list_add (&tx->tx_list, &zombies);
1919 cfs_spin_unlock(&conn->ibc_lock);
1921 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
1922 &zombies, -ECONNABORTED);
1926 kiblnd_finalise_conn (kib_conn_t *conn)
1928 LASSERT (!cfs_in_interrupt());
1929 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
1931 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
1933 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
1934 * for connections that didn't get as far as being connected, because
1935 * rdma_disconnect() does this for free. */
1936 kiblnd_abort_receives(conn);
1938 /* Complete all tx descs not waiting for sends to complete.
1939 * NB we should be safe from RDMA now that the QP has changed state */
1941 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
1942 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
1943 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
1944 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
1945 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
1947 kiblnd_handle_early_rxs(conn);
1951 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
1953 CFS_LIST_HEAD (zombies);
1954 unsigned long flags;
1956 LASSERT (error != 0);
1957 LASSERT (!cfs_in_interrupt());
1959 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1962 LASSERT (peer->ibp_connecting > 0);
1963 peer->ibp_connecting--;
1965 LASSERT (peer->ibp_accepting > 0);
1966 peer->ibp_accepting--;
1969 if (peer->ibp_connecting != 0 ||
1970 peer->ibp_accepting != 0) {
1971 /* another connection attempt under way... */
1972 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
1977 if (cfs_list_empty(&peer->ibp_conns)) {
1978 /* Take peer's blocked transmits to complete with error */
1979 cfs_list_add(&zombies, &peer->ibp_tx_queue);
1980 cfs_list_del_init(&peer->ibp_tx_queue);
1982 if (kiblnd_peer_active(peer))
1983 kiblnd_unlink_peer_locked(peer);
1985 peer->ibp_error = error;
1987 /* Can't have blocked transmits if there are connections */
1988 LASSERT (cfs_list_empty(&peer->ibp_tx_queue));
1991 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1993 kiblnd_peer_notify(peer);
1995 if (cfs_list_empty (&zombies))
1998 CNETERR("Deleting messages for %s: connection failed\n",
1999 libcfs_nid2str(peer->ibp_nid));
2001 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2005 kiblnd_connreq_done(kib_conn_t *conn, int status)
2007 kib_peer_t *peer = conn->ibc_peer;
2010 unsigned long flags;
2013 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2015 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2016 libcfs_nid2str(peer->ibp_nid), active,
2017 conn->ibc_version, status);
2019 LASSERT (!cfs_in_interrupt());
2020 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2021 peer->ibp_connecting > 0) ||
2022 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2023 peer->ibp_accepting > 0));
2025 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2026 conn->ibc_connvars = NULL;
2029 /* failed to establish connection */
2030 kiblnd_peer_connect_failed(peer, active, status);
2031 kiblnd_finalise_conn(conn);
2035 /* connection established */
2036 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2038 conn->ibc_last_send = jiffies;
2039 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2040 kiblnd_peer_alive(peer);
2042 /* Add conn to peer's list and nuke any dangling conns from a different
2043 * peer instance... */
2044 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2045 cfs_list_add(&conn->ibc_list, &peer->ibp_conns);
2047 peer->ibp_connecting--;
2049 peer->ibp_accepting--;
2051 if (peer->ibp_version == 0) {
2052 peer->ibp_version = conn->ibc_version;
2053 peer->ibp_incarnation = conn->ibc_incarnation;
2056 if (peer->ibp_version != conn->ibc_version ||
2057 peer->ibp_incarnation != conn->ibc_incarnation) {
2058 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2059 conn->ibc_incarnation);
2060 peer->ibp_version = conn->ibc_version;
2061 peer->ibp_incarnation = conn->ibc_incarnation;
2064 /* grab pending txs while I have the lock */
2065 cfs_list_add(&txs, &peer->ibp_tx_queue);
2066 cfs_list_del_init(&peer->ibp_tx_queue);
2068 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2069 conn->ibc_comms_error != 0) { /* error has happened already */
2070 lnet_ni_t *ni = peer->ibp_ni;
2072 /* start to shut down connection */
2073 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2074 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2077 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2082 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2084 /* Schedule blocked txs */
2085 cfs_spin_lock (&conn->ibc_lock);
2086 while (!cfs_list_empty (&txs)) {
2087 tx = cfs_list_entry (txs.next, kib_tx_t, tx_list);
2088 cfs_list_del(&tx->tx_list);
2090 kiblnd_queue_tx_locked(tx, conn);
2092 cfs_spin_unlock (&conn->ibc_lock);
2094 kiblnd_check_sends(conn);
2096 /* schedule blocked rxs */
2097 kiblnd_handle_early_rxs(conn);
2101 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2105 rc = rdma_reject(cmid, rej, sizeof(*rej));
2108 CWARN("Error %d sending reject\n", rc);
2112 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2114 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2115 kib_msg_t *reqmsg = priv;
2121 lnet_ni_t *ni = NULL;
2122 kib_net_t *net = NULL;
2124 struct rdma_conn_param cp;
2126 int version = IBLND_MSG_VERSION;
2127 unsigned long flags;
2130 LASSERT (!cfs_in_interrupt());
2132 /* cmid inherits 'context' from the corresponding listener id */
2133 ibdev = (kib_dev_t *)cmid->context;
2134 LASSERT (ibdev != NULL);
2136 memset(&rej, 0, sizeof(rej));
2137 rej.ibr_magic = IBLND_MSG_MAGIC;
2138 rej.ibr_why = IBLND_REJECT_FATAL;
2139 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2141 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2142 CERROR("Short connection request\n");
2146 /* Future protocol version compatibility support! If the
2147 * o2iblnd-specific protocol changes, or when LNET unifies
2148 * protocols over all LNDs, the initial connection will
2149 * negotiate a protocol version. I trap this here to avoid
2150 * console errors; the reject tells the peer which protocol I
2152 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2153 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2155 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2156 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2157 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2159 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2160 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2161 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2164 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2166 CERROR("Can't parse connection request: %d\n", rc);
2170 nid = reqmsg->ibm_srcnid;
2171 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2174 net = (kib_net_t *)ni->ni_data;
2175 rej.ibr_incarnation = net->ibn_incarnation;
2178 if (ni == NULL || /* no matching net */
2179 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2180 net->ibn_dev != ibdev) { /* wrong device */
2181 CERROR("Can't accept %s on %s (%s:%d:%u.%u.%u.%u): "
2182 "bad dst nid %s\n", libcfs_nid2str(nid),
2183 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2184 ibdev->ibd_ifname, ibdev->ibd_nnets,
2185 HIPQUAD(ibdev->ibd_ifip),
2186 libcfs_nid2str(reqmsg->ibm_dstnid));
2191 /* check time stamp as soon as possible */
2192 if (reqmsg->ibm_dststamp != 0 &&
2193 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2194 CWARN("Stale connection request\n");
2195 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2199 /* I can accept peer's version */
2200 version = reqmsg->ibm_version;
2202 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2203 CERROR("Unexpected connreq msg type: %x from %s\n",
2204 reqmsg->ibm_type, libcfs_nid2str(nid));
2208 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2209 IBLND_MSG_QUEUE_SIZE(version)) {
2210 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2211 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2212 IBLND_MSG_QUEUE_SIZE(version));
2214 if (version == IBLND_MSG_VERSION)
2215 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2220 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2221 IBLND_RDMA_FRAGS(version)) {
2222 CERROR("Can't accept %s(version %x): "
2223 "incompatible max_frags %d (%d wanted)\n",
2224 libcfs_nid2str(nid), version,
2225 reqmsg->ibm_u.connparams.ibcp_max_frags,
2226 IBLND_RDMA_FRAGS(version));
2228 if (version == IBLND_MSG_VERSION)
2229 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2235 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2236 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2237 libcfs_nid2str(nid),
2238 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2243 /* assume 'nid' is a new peer; create */
2244 rc = kiblnd_create_peer(ni, &peer, nid);
2246 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2247 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2251 cfs_write_lock_irqsave(g_lock, flags);
2253 peer2 = kiblnd_find_peer_locked(nid);
2254 if (peer2 != NULL) {
2255 if (peer2->ibp_version == 0) {
2256 peer2->ibp_version = version;
2257 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2260 /* not the guy I've talked with */
2261 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2262 peer2->ibp_version != version) {
2263 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2264 cfs_write_unlock_irqrestore(g_lock, flags);
2266 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2267 libcfs_nid2str(nid), peer2->ibp_version, version);
2269 kiblnd_peer_decref(peer);
2270 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2274 /* tie-break connection race in favour of the higher NID */
2275 if (peer2->ibp_connecting != 0 &&
2277 cfs_write_unlock_irqrestore(g_lock, flags);
2279 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2281 kiblnd_peer_decref(peer);
2282 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2286 peer2->ibp_accepting++;
2287 kiblnd_peer_addref(peer2);
2289 cfs_write_unlock_irqrestore(g_lock, flags);
2290 kiblnd_peer_decref(peer);
2293 /* Brand new peer */
2294 LASSERT (peer->ibp_accepting == 0);
2295 LASSERT (peer->ibp_version == 0 &&
2296 peer->ibp_incarnation == 0);
2298 peer->ibp_accepting = 1;
2299 peer->ibp_version = version;
2300 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2302 /* I have a ref on ni that prevents it being shutdown */
2303 LASSERT (net->ibn_shutdown == 0);
2305 kiblnd_peer_addref(peer);
2306 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2308 cfs_write_unlock_irqrestore(g_lock, flags);
2311 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2313 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2314 kiblnd_peer_decref(peer);
2315 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2319 /* conn now "owns" cmid, so I return success from here on to ensure the
2320 * CM callback doesn't destroy cmid. */
2322 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2323 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2324 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2325 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2326 <= IBLND_RX_MSGS(version));
2328 ackmsg = &conn->ibc_connvars->cv_msg;
2329 memset(ackmsg, 0, sizeof(*ackmsg));
2331 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2332 sizeof(ackmsg->ibm_u.connparams));
2333 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2334 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2335 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2337 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2339 memset(&cp, 0, sizeof(cp));
2340 cp.private_data = ackmsg;
2341 cp.private_data_len = ackmsg->ibm_nob;
2342 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2343 cp.initiator_depth = 0;
2344 cp.flow_control = 1;
2345 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2346 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2348 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2350 rc = rdma_accept(cmid, &cp);
2352 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2353 rej.ibr_version = version;
2354 rej.ibr_why = IBLND_REJECT_FATAL;
2356 kiblnd_reject(cmid, &rej);
2357 kiblnd_connreq_done(conn, rc);
2358 kiblnd_conn_decref(conn);
2368 rej.ibr_version = version;
2369 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2370 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2371 kiblnd_reject(cmid, &rej);
2373 return -ECONNREFUSED;
2377 kiblnd_reconnect (kib_conn_t *conn, int version,
2378 __u64 incarnation, int why, kib_connparams_t *cp)
2380 kib_peer_t *peer = conn->ibc_peer;
2383 unsigned long flags;
2385 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2386 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2388 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2390 /* retry connection if it's still needed and no other connection
2391 * attempts (active or passive) are in progress
2392 * NB: reconnect is still needed even when ibp_tx_queue is
2393 * empty if ibp_version != version because reconnect may be
2394 * initiated by kiblnd_query() */
2395 if ((!cfs_list_empty(&peer->ibp_tx_queue) ||
2396 peer->ibp_version != version) &&
2397 peer->ibp_connecting == 1 &&
2398 peer->ibp_accepting == 0) {
2400 peer->ibp_connecting++;
2402 peer->ibp_version = version;
2403 peer->ibp_incarnation = incarnation;
2406 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2416 case IBLND_REJECT_CONN_STALE:
2420 case IBLND_REJECT_CONN_RACE:
2421 reason = "conn race";
2424 case IBLND_REJECT_CONN_UNCOMPAT:
2425 reason = "version negotiation";
2429 CNETERR("%s: retrying (%s), %x, %x, "
2430 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2431 libcfs_nid2str(peer->ibp_nid),
2432 reason, IBLND_MSG_VERSION, version,
2433 cp != NULL? cp->ibcp_queue_depth :IBLND_MSG_QUEUE_SIZE(version),
2434 cp != NULL? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2435 cp != NULL? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2437 kiblnd_connect_peer(peer);
2441 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2443 kib_peer_t *peer = conn->ibc_peer;
2445 LASSERT (!cfs_in_interrupt());
2446 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2449 case IB_CM_REJ_STALE_CONN:
2450 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2451 IBLND_REJECT_CONN_STALE, NULL);
2454 case IB_CM_REJ_INVALID_SERVICE_ID:
2455 CNETERR("%s rejected: no listener at %d\n",
2456 libcfs_nid2str(peer->ibp_nid),
2457 *kiblnd_tunables.kib_service);
2460 case IB_CM_REJ_CONSUMER_DEFINED:
2461 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2462 kib_rej_t *rej = priv;
2463 kib_connparams_t *cp = NULL;
2465 __u64 incarnation = -1;
2467 /* NB. default incarnation is -1 because:
2468 * a) V1 will ignore dst incarnation in connreq.
2469 * b) V2 will provide incarnation while rejecting me,
2470 * -1 will be overwrote.
2472 * if I try to connect to a V1 peer with V2 protocol,
2473 * it rejected me then upgrade to V2, I have no idea
2474 * about the upgrading and try to reconnect with V1,
2475 * in this case upgraded V2 can find out I'm trying to
2476 * talk to the old guy and reject me(incarnation is -1).
2479 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2480 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2481 __swab32s(&rej->ibr_magic);
2482 __swab16s(&rej->ibr_version);
2486 if (priv_nob >= sizeof(kib_rej_t) &&
2487 rej->ibr_version > IBLND_MSG_VERSION_1) {
2488 /* priv_nob is always 148 in current version
2489 * of OFED, so we still need to check version.
2490 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2494 __swab64s(&rej->ibr_incarnation);
2495 __swab16s(&cp->ibcp_queue_depth);
2496 __swab16s(&cp->ibcp_max_frags);
2497 __swab32s(&cp->ibcp_max_msg_size);
2500 incarnation = rej->ibr_incarnation;
2503 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2504 rej->ibr_magic != LNET_PROTO_MAGIC) {
2505 CERROR("%s rejected: consumer defined fatal error\n",
2506 libcfs_nid2str(peer->ibp_nid));
2510 if (rej->ibr_version != IBLND_MSG_VERSION &&
2511 rej->ibr_version != IBLND_MSG_VERSION_1) {
2512 CERROR("%s rejected: o2iblnd version %x error\n",
2513 libcfs_nid2str(peer->ibp_nid),
2518 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2519 rej->ibr_version == IBLND_MSG_VERSION_1) {
2520 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2521 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2523 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2524 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2527 switch (rej->ibr_why) {
2528 case IBLND_REJECT_CONN_RACE:
2529 case IBLND_REJECT_CONN_STALE:
2530 case IBLND_REJECT_CONN_UNCOMPAT:
2531 kiblnd_reconnect(conn, rej->ibr_version,
2532 incarnation, rej->ibr_why, cp);
2535 case IBLND_REJECT_MSG_QUEUE_SIZE:
2536 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2537 libcfs_nid2str(peer->ibp_nid), cp->ibcp_queue_depth,
2538 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2541 case IBLND_REJECT_RDMA_FRAGS:
2542 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2543 libcfs_nid2str(peer->ibp_nid), cp->ibcp_max_frags,
2544 IBLND_RDMA_FRAGS(conn->ibc_version));
2547 case IBLND_REJECT_NO_RESOURCES:
2548 CERROR("%s rejected: o2iblnd no resources\n",
2549 libcfs_nid2str(peer->ibp_nid));
2552 case IBLND_REJECT_FATAL:
2553 CERROR("%s rejected: o2iblnd fatal error\n",
2554 libcfs_nid2str(peer->ibp_nid));
2558 CERROR("%s rejected: o2iblnd reason %d\n",
2559 libcfs_nid2str(peer->ibp_nid),
2567 CNETERR("%s rejected: reason %d, size %d\n",
2568 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2572 kiblnd_connreq_done(conn, -ECONNREFUSED);
2576 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2578 kib_peer_t *peer = conn->ibc_peer;
2579 lnet_ni_t *ni = peer->ibp_ni;
2580 kib_net_t *net = ni->ni_data;
2581 kib_msg_t *msg = priv;
2582 int ver = conn->ibc_version;
2583 int rc = kiblnd_unpack_msg(msg, priv_nob);
2584 unsigned long flags;
2586 LASSERT (net != NULL);
2589 CERROR("Can't unpack connack from %s: %d\n",
2590 libcfs_nid2str(peer->ibp_nid), rc);
2594 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2595 CERROR("Unexpected message %d from %s\n",
2596 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2601 if (ver != msg->ibm_version) {
2602 CERROR("%s replied version %x is different with "
2603 "requested version %x\n",
2604 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2609 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2610 IBLND_MSG_QUEUE_SIZE(ver)) {
2611 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2612 libcfs_nid2str(peer->ibp_nid),
2613 msg->ibm_u.connparams.ibcp_queue_depth,
2614 IBLND_MSG_QUEUE_SIZE(ver));
2619 if (msg->ibm_u.connparams.ibcp_max_frags !=
2620 IBLND_RDMA_FRAGS(ver)) {
2621 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2622 libcfs_nid2str(peer->ibp_nid),
2623 msg->ibm_u.connparams.ibcp_max_frags,
2624 IBLND_RDMA_FRAGS(ver));
2629 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2630 CERROR("%s max message size %d too big (%d max)\n",
2631 libcfs_nid2str(peer->ibp_nid),
2632 msg->ibm_u.connparams.ibcp_max_msg_size,
2638 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2639 if (msg->ibm_dstnid == ni->ni_nid &&
2640 msg->ibm_dststamp == net->ibn_incarnation)
2644 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2647 CERROR("Bad connection reply from %s, rc = %d, "
2648 "version: %x max_frags: %d\n",
2649 libcfs_nid2str(peer->ibp_nid), rc,
2650 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2654 conn->ibc_incarnation = msg->ibm_srcstamp;
2656 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2657 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2658 <= IBLND_RX_MSGS(ver));
2660 kiblnd_connreq_done(conn, 0);
2664 /* NB My QP has already established itself, so I handle anything going
2665 * wrong here by setting ibc_comms_error.
2666 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2667 * immediately tears it down. */
2670 conn->ibc_comms_error = rc;
2671 kiblnd_connreq_done(conn, 0);
2675 kiblnd_active_connect (struct rdma_cm_id *cmid)
2677 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2680 struct rdma_conn_param cp;
2683 unsigned long flags;
2686 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2688 incarnation = peer->ibp_incarnation;
2689 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION : peer->ibp_version;
2691 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2693 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2695 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2696 kiblnd_peer_decref(peer); /* lose cmid's ref */
2700 /* conn "owns" cmid now, so I return success from here on to ensure the
2701 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2704 msg = &conn->ibc_connvars->cv_msg;
2706 memset(msg, 0, sizeof(*msg));
2707 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2708 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2709 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2710 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2712 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2713 0, peer->ibp_nid, incarnation);
2715 memset(&cp, 0, sizeof(cp));
2716 cp.private_data = msg;
2717 cp.private_data_len = msg->ibm_nob;
2718 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2719 cp.initiator_depth = 0;
2720 cp.flow_control = 1;
2721 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2722 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2724 LASSERT(cmid->context == (void *)conn);
2725 LASSERT(conn->ibc_cmid == cmid);
2727 rc = rdma_connect(cmid, &cp);
2729 CERROR("Can't connect to %s: %d\n",
2730 libcfs_nid2str(peer->ibp_nid), rc);
2731 kiblnd_connreq_done(conn, rc);
2732 kiblnd_conn_decref(conn);
2739 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2745 switch (event->event) {
2747 CERROR("Unexpected event: %d, status: %d\n",
2748 event->event, event->status);
2751 case RDMA_CM_EVENT_CONNECT_REQUEST:
2752 /* destroy cmid on failure */
2753 rc = kiblnd_passive_connect(cmid,
2754 (void *)KIBLND_CONN_PARAM(event),
2755 KIBLND_CONN_PARAM_LEN(event));
2756 CDEBUG(D_NET, "connreq: %d\n", rc);
2759 case RDMA_CM_EVENT_ADDR_ERROR:
2760 peer = (kib_peer_t *)cmid->context;
2761 CNETERR("%s: ADDR ERROR %d\n",
2762 libcfs_nid2str(peer->ibp_nid), event->status);
2763 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2764 kiblnd_peer_decref(peer);
2765 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2767 case RDMA_CM_EVENT_ADDR_RESOLVED:
2768 peer = (kib_peer_t *)cmid->context;
2770 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2771 libcfs_nid2str(peer->ibp_nid), event->status);
2773 if (event->status != 0) {
2774 CNETERR("Can't resolve address for %s: %d\n",
2775 libcfs_nid2str(peer->ibp_nid), event->status);
2778 rc = rdma_resolve_route(
2779 cmid, *kiblnd_tunables.kib_timeout * 1000);
2782 /* Can't initiate route resolution */
2783 CERROR("Can't resolve route for %s: %d\n",
2784 libcfs_nid2str(peer->ibp_nid), rc);
2786 kiblnd_peer_connect_failed(peer, 1, rc);
2787 kiblnd_peer_decref(peer);
2788 return rc; /* rc != 0 destroys cmid */
2790 case RDMA_CM_EVENT_ROUTE_ERROR:
2791 peer = (kib_peer_t *)cmid->context;
2792 CNETERR("%s: ROUTE ERROR %d\n",
2793 libcfs_nid2str(peer->ibp_nid), event->status);
2794 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2795 kiblnd_peer_decref(peer);
2796 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2798 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2799 peer = (kib_peer_t *)cmid->context;
2800 CDEBUG(D_NET,"%s Route resolved: %d\n",
2801 libcfs_nid2str(peer->ibp_nid), event->status);
2803 if (event->status == 0)
2804 return kiblnd_active_connect(cmid);
2806 CNETERR("Can't resolve route for %s: %d\n",
2807 libcfs_nid2str(peer->ibp_nid), event->status);
2808 kiblnd_peer_connect_failed(peer, 1, event->status);
2809 kiblnd_peer_decref(peer);
2810 return event->status; /* rc != 0 destroys cmid */
2812 case RDMA_CM_EVENT_UNREACHABLE:
2813 conn = (kib_conn_t *)cmid->context;
2814 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2815 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2816 CNETERR("%s: UNREACHABLE %d\n",
2817 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2818 kiblnd_connreq_done(conn, -ENETDOWN);
2819 kiblnd_conn_decref(conn);
2822 case RDMA_CM_EVENT_CONNECT_ERROR:
2823 conn = (kib_conn_t *)cmid->context;
2824 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2825 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2826 CNETERR("%s: CONNECT ERROR %d\n",
2827 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2828 kiblnd_connreq_done(conn, -ENOTCONN);
2829 kiblnd_conn_decref(conn);
2832 case RDMA_CM_EVENT_REJECTED:
2833 conn = (kib_conn_t *)cmid->context;
2834 switch (conn->ibc_state) {
2838 case IBLND_CONN_PASSIVE_WAIT:
2839 CERROR ("%s: REJECTED %d\n",
2840 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2842 kiblnd_connreq_done(conn, -ECONNRESET);
2845 case IBLND_CONN_ACTIVE_CONNECT:
2846 kiblnd_rejected(conn, event->status,
2847 (void *)KIBLND_CONN_PARAM(event),
2848 KIBLND_CONN_PARAM_LEN(event));
2851 kiblnd_conn_decref(conn);
2854 case RDMA_CM_EVENT_ESTABLISHED:
2855 conn = (kib_conn_t *)cmid->context;
2856 switch (conn->ibc_state) {
2860 case IBLND_CONN_PASSIVE_WAIT:
2861 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2862 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2863 kiblnd_connreq_done(conn, 0);
2866 case IBLND_CONN_ACTIVE_CONNECT:
2867 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2868 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2869 kiblnd_check_connreply(conn,
2870 (void *)KIBLND_CONN_PARAM(event),
2871 KIBLND_CONN_PARAM_LEN(event));
2874 /* net keeps its ref on conn! */
2877 #ifdef HAVE_OFED_RDMA_CMEV_TIMEWAIT_EXIT
2878 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2879 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2882 case RDMA_CM_EVENT_DISCONNECTED:
2883 conn = (kib_conn_t *)cmid->context;
2884 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2885 CERROR("%s DISCONNECTED\n",
2886 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2887 kiblnd_connreq_done(conn, -ECONNRESET);
2889 kiblnd_close_conn(conn, 0);
2891 kiblnd_conn_decref(conn);
2892 cmid->context = NULL;
2895 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2896 LCONSOLE_ERROR_MSG(0x131,
2897 "Received notification of device removal\n"
2898 "Please shutdown LNET to allow this to proceed\n");
2899 /* Can't remove network from underneath LNET for now, so I have
2903 #ifdef HAVE_OFED_RDMA_CMEV_ADDRCHANGE
2904 case RDMA_CM_EVENT_ADDR_CHANGE:
2905 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2912 kiblnd_check_txs (kib_conn_t *conn, cfs_list_t *txs)
2918 cfs_spin_lock(&conn->ibc_lock);
2920 cfs_list_for_each (ttmp, txs) {
2921 tx = cfs_list_entry (ttmp, kib_tx_t, tx_list);
2923 if (txs != &conn->ibc_active_txs) {
2924 LASSERT (tx->tx_queued);
2926 LASSERT (!tx->tx_queued);
2927 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2930 if (cfs_time_aftereq (jiffies, tx->tx_deadline)) {
2932 CERROR("Timed out tx: %s, %lu seconds\n",
2933 kiblnd_queue2str(conn, txs),
2934 cfs_duration_sec(jiffies - tx->tx_deadline));
2939 cfs_spin_unlock(&conn->ibc_lock);
2944 kiblnd_conn_timed_out (kib_conn_t *conn)
2946 return kiblnd_check_txs(conn, &conn->ibc_tx_queue) ||
2947 kiblnd_check_txs(conn, &conn->ibc_tx_noops) ||
2948 kiblnd_check_txs(conn, &conn->ibc_tx_queue_rsrvd) ||
2949 kiblnd_check_txs(conn, &conn->ibc_tx_queue_nocred) ||
2950 kiblnd_check_txs(conn, &conn->ibc_active_txs);
2954 kiblnd_check_conns (int idx)
2956 cfs_list_t *peers = &kiblnd_data.kib_peers[idx];
2961 unsigned long flags;
2964 /* NB. We expect to have a look at all the peers and not find any
2965 * rdmas to time out, so we just use a shared lock while we
2967 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2969 cfs_list_for_each (ptmp, peers) {
2970 peer = cfs_list_entry (ptmp, kib_peer_t, ibp_list);
2972 cfs_list_for_each (ctmp, &peer->ibp_conns) {
2973 conn = cfs_list_entry (ctmp, kib_conn_t, ibc_list);
2975 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
2977 /* In case we have enough credits to return via a
2978 * NOOP, but there were no non-blocking tx descs
2979 * free to do it last time... */
2980 kiblnd_check_sends(conn);
2982 if (!kiblnd_conn_timed_out(conn))
2985 /* Handle timeout by closing the whole connection. We
2986 * can only be sure RDMA activity has ceased once the
2987 * QP has been modified. */
2989 kiblnd_conn_addref(conn); /* 1 ref for me... */
2991 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2994 CERROR("Timed out RDMA with %s (%lu)\n",
2995 libcfs_nid2str(peer->ibp_nid),
2996 cfs_duration_sec(cfs_time_current() -
2997 peer->ibp_last_alive));
2999 kiblnd_close_conn(conn, -ETIMEDOUT);
3000 kiblnd_conn_decref(conn); /* ...until here */
3002 /* start again now I've dropped the lock */
3007 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3011 kiblnd_disconnect_conn (kib_conn_t *conn)
3013 LASSERT (!cfs_in_interrupt());
3014 LASSERT (current == kiblnd_data.kib_connd);
3015 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3017 rdma_disconnect(conn->ibc_cmid);
3018 kiblnd_finalise_conn(conn);
3020 kiblnd_peer_notify(conn->ibc_peer);
3024 kiblnd_connd (void *arg)
3026 cfs_waitlink_t wait;
3027 unsigned long flags;
3033 unsigned long deadline = jiffies;
3035 cfs_daemonize ("kiblnd_connd");
3036 cfs_block_allsigs ();
3038 cfs_waitlink_init (&wait);
3039 kiblnd_data.kib_connd = current;
3041 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3043 while (!kiblnd_data.kib_shutdown) {
3047 if (!cfs_list_empty (&kiblnd_data.kib_connd_zombies)) {
3048 conn = cfs_list_entry(kiblnd_data. \
3049 kib_connd_zombies.next,
3050 kib_conn_t, ibc_list);
3051 cfs_list_del(&conn->ibc_list);
3053 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3057 kiblnd_destroy_conn(conn);
3059 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3063 if (!cfs_list_empty (&kiblnd_data.kib_connd_conns)) {
3064 conn = cfs_list_entry (kiblnd_data.kib_connd_conns.next,
3065 kib_conn_t, ibc_list);
3066 cfs_list_del(&conn->ibc_list);
3068 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3072 kiblnd_disconnect_conn(conn);
3073 kiblnd_conn_decref(conn);
3075 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3079 /* careful with the jiffy wrap... */
3080 timeout = (int)(deadline - jiffies);
3084 int chunk = kiblnd_data.kib_peer_hash_size;
3086 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3089 /* Time to check for RDMA timeouts on a few more
3090 * peers: I do checks every 'p' seconds on a
3091 * proportion of the peer table and I need to check
3092 * every connection 'n' times within a timeout
3093 * interval, to ensure I detect a timeout on any
3094 * connection within (n+1)/n times the timeout
3097 if (*kiblnd_tunables.kib_timeout > n * p)
3098 chunk = (chunk * n * p) /
3099 *kiblnd_tunables.kib_timeout;
3103 for (i = 0; i < chunk; i++) {
3104 kiblnd_check_conns(peer_index);
3105 peer_index = (peer_index + 1) %
3106 kiblnd_data.kib_peer_hash_size;
3109 deadline += p * CFS_HZ;
3110 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock,
3117 /* Nothing to do for 'timeout' */
3118 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
3119 cfs_waitq_add (&kiblnd_data.kib_connd_waitq, &wait);
3120 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3122 cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE, timeout);
3124 cfs_set_current_state (CFS_TASK_RUNNING);
3125 cfs_waitq_del (&kiblnd_data.kib_connd_waitq, &wait);
3126 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
3129 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3131 kiblnd_thread_fini();
3136 kiblnd_qp_event(struct ib_event *event, void *arg)
3138 kib_conn_t *conn = arg;
3140 switch (event->event) {
3141 case IB_EVENT_COMM_EST:
3142 CDEBUG(D_NET, "%s established\n",
3143 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3147 CERROR("%s: Async QP event type %d\n",
3148 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3154 kiblnd_complete (struct ib_wc *wc)
3156 switch (kiblnd_wreqid2type(wc->wr_id)) {
3160 case IBLND_WID_RDMA:
3161 /* We only get RDMA completion notification if it fails. All
3162 * subsequent work items, including the final SEND will fail
3163 * too. However we can't print out any more info about the
3164 * failing RDMA because 'tx' might be back on the idle list or
3165 * even reused already if we didn't manage to post all our work
3167 CNETERR("RDMA (tx: %p) failed: %d\n",
3168 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3172 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3176 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3183 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3185 /* NB I'm not allowed to schedule this conn once its refcount has
3186 * reached 0. Since fundamentally I'm racing with scheduler threads
3187 * consuming my CQ I could be called after all completions have
3188 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3189 * and this CQ is about to be destroyed so I NOOP. */
3190 kib_conn_t *conn = (kib_conn_t *)arg;
3191 unsigned long flags;
3193 LASSERT (cq == conn->ibc_cq);
3195 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3197 conn->ibc_ready = 1;
3199 if (!conn->ibc_scheduled &&
3200 (conn->ibc_nrx > 0 ||
3201 conn->ibc_nsends_posted > 0)) {
3202 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3203 conn->ibc_scheduled = 1;
3204 cfs_list_add_tail(&conn->ibc_sched_list,
3205 &kiblnd_data.kib_sched_conns);
3206 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3209 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3213 kiblnd_cq_event(struct ib_event *event, void *arg)
3215 kib_conn_t *conn = arg;
3217 CERROR("%s: async CQ event type %d\n",
3218 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3222 kiblnd_scheduler(void *arg)
3224 long id = (long)arg;
3225 cfs_waitlink_t wait;
3227 unsigned long flags;
3234 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3235 cfs_daemonize(name);
3236 cfs_block_allsigs();
3238 cfs_waitlink_init(&wait);
3240 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3242 while (!kiblnd_data.kib_shutdown) {
3243 if (busy_loops++ >= IBLND_RESCHED) {
3244 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3250 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3256 if (!cfs_list_empty(&kiblnd_data.kib_sched_conns)) {
3257 conn = cfs_list_entry(kiblnd_data.kib_sched_conns.next,
3258 kib_conn_t, ibc_sched_list);
3259 /* take over kib_sched_conns' ref on conn... */
3260 LASSERT(conn->ibc_scheduled);
3261 cfs_list_del(&conn->ibc_sched_list);
3262 conn->ibc_ready = 0;
3264 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3267 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3269 rc = ib_req_notify_cq(conn->ibc_cq,
3272 CWARN("%s: ib_req_notify_cq failed: %d, "
3273 "closing connection\n",
3274 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3275 kiblnd_close_conn(conn, -EIO);
3276 kiblnd_conn_decref(conn);
3277 cfs_spin_lock_irqsave(&kiblnd_data. \
3283 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3287 CWARN("%s: ib_poll_cq failed: %d, "
3288 "closing connection\n",
3289 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3291 kiblnd_close_conn(conn, -EIO);
3292 kiblnd_conn_decref(conn);
3293 cfs_spin_lock_irqsave(&kiblnd_data. \
3294 kib_sched_lock, flags);
3298 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3301 if (rc != 0 || conn->ibc_ready) {
3302 /* There may be another completion waiting; get
3303 * another scheduler to check while I handle
3305 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3306 cfs_list_add_tail(&conn->ibc_sched_list,
3307 &kiblnd_data.kib_sched_conns);
3308 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3310 conn->ibc_scheduled = 0;
3314 cfs_spin_unlock_irqrestore(&kiblnd_data. \
3318 kiblnd_complete(&wc);
3320 cfs_spin_lock_irqsave(&kiblnd_data. \
3325 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3332 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3333 cfs_waitq_add_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3334 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3336 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
3339 cfs_waitq_del(&kiblnd_data.kib_sched_waitq, &wait);
3340 cfs_set_current_state(CFS_TASK_RUNNING);
3341 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3344 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3346 kiblnd_thread_fini();
3351 kiblnd_failover_thread(void *arg)
3353 cfs_rwlock_t *glock = &kiblnd_data.kib_global_lock;
3355 cfs_waitlink_t wait;
3356 unsigned long flags;
3359 LASSERT (*kiblnd_tunables.kib_dev_failover != 0);
3361 cfs_daemonize ("kiblnd_failover");
3362 cfs_block_allsigs ();
3364 cfs_waitlink_init(&wait);
3365 cfs_write_lock_irqsave(glock, flags);
3367 while (!kiblnd_data.kib_shutdown) {
3368 int do_failover = 0;
3371 cfs_list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3373 if (cfs_time_before(cfs_time_current(),
3374 dev->ibd_next_failover))
3381 cfs_list_del_init(&dev->ibd_fail_list);
3382 dev->ibd_failover = 1;
3383 cfs_write_unlock_irqrestore(glock, flags);
3385 rc = kiblnd_dev_failover(dev);
3387 cfs_write_lock_irqsave(glock, flags);
3389 LASSERT (dev->ibd_failover);
3390 dev->ibd_failover = 0;
3391 if (rc >= 0) { /* Device is OK or failover succeed */
3392 dev->ibd_next_failover = cfs_time_shift(3);
3396 /* failed to failover, retry later */
3397 dev->ibd_next_failover =
3398 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3399 if (kiblnd_dev_can_failover(dev)) {
3400 cfs_list_add_tail(&dev->ibd_fail_list,
3401 &kiblnd_data.kib_failed_devs);
3407 /* long sleep if no more pending failover */
3408 long_sleep = cfs_list_empty(&kiblnd_data.kib_failed_devs);
3410 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3411 cfs_waitq_add(&kiblnd_data.kib_failover_waitq, &wait);
3412 cfs_write_unlock_irqrestore(glock, flags);
3414 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3415 cfs_time_seconds(1));
3416 cfs_set_current_state(CFS_TASK_RUNNING);
3417 cfs_waitq_del(&kiblnd_data.kib_failover_waitq, &wait);
3418 cfs_write_lock_irqsave(glock, flags);
3420 if (!long_sleep || rc != 0)
3423 /* have a long sleep, routine check all active devices,
3424 * we need checking like this because if there is not active
3425 * connection on the dev and no SEND from local, we may listen
3426 * on wrong HCA for ever while there is a bonding failover */
3427 cfs_list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3428 if (kiblnd_dev_can_failover(dev)) {
3429 cfs_list_add_tail(&dev->ibd_fail_list,
3430 &kiblnd_data.kib_failed_devs);
3435 cfs_write_unlock_irqrestore(glock, flags);
3437 kiblnd_thread_fini();