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);
1210 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1211 struct sockaddr_in *srcaddr,
1212 struct sockaddr_in *dstaddr,
1215 unsigned short port;
1218 #ifdef HAVE_OFED_RDMA_SET_REUSEADDR
1219 /* allow the port to be reused */
1220 rc = rdma_set_reuseaddr(cmid, 1);
1222 CERROR("Unable to set reuse on cmid: %d\n", rc);
1227 /* look for a free privileged port */
1228 for (port = PROT_SOCK-1; port > 0; port--) {
1229 srcaddr->sin_port = htons(port);
1230 rc = rdma_resolve_addr(cmid,
1231 (struct sockaddr *)srcaddr,
1232 (struct sockaddr *)dstaddr,
1235 CDEBUG(D_NET, "bound to port %hu\n", port);
1237 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1238 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1245 CERROR("Failed to bind to a free privileged port\n");
1246 #ifndef HAVE_OFED_RDMA_SET_REUSEADDR
1247 CERROR("You may need IB verbs that supports rdma_set_reuseaddr()\n");
1253 kiblnd_connect_peer (kib_peer_t *peer)
1255 struct rdma_cm_id *cmid;
1257 kib_net_t *net = peer->ibp_ni->ni_data;
1258 struct sockaddr_in srcaddr;
1259 struct sockaddr_in dstaddr;
1262 LASSERT (net != NULL);
1263 LASSERT (peer->ibp_connecting > 0);
1265 cmid = rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP);
1267 CERROR("Can't create CMID for %s: %ld\n",
1268 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1274 memset(&srcaddr, 0, sizeof(srcaddr));
1275 srcaddr.sin_family = AF_INET;
1276 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1278 memset(&dstaddr, 0, sizeof(dstaddr));
1279 dstaddr.sin_family = AF_INET;
1280 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1281 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1283 kiblnd_peer_addref(peer); /* cmid's ref */
1285 if (*kiblnd_tunables.kib_use_priv_port) {
1286 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1287 *kiblnd_tunables.kib_timeout * 1000);
1289 rc = rdma_resolve_addr(cmid,
1290 (struct sockaddr *)&srcaddr,
1291 (struct sockaddr *)&dstaddr,
1292 *kiblnd_tunables.kib_timeout * 1000);
1295 /* Can't initiate address resolution: */
1296 CERROR("Can't resolve addr for %s: %d\n",
1297 libcfs_nid2str(peer->ibp_nid), rc);
1301 LASSERT (cmid->device != NULL);
1302 CDEBUG(D_NET, "%s: connection bound to %s:%u.%u.%u.%u:%s\n",
1303 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1304 HIPQUAD(dev->ibd_ifip), cmid->device->name);
1309 kiblnd_peer_decref(peer); /* cmid's ref */
1310 rdma_destroy_id(cmid);
1312 kiblnd_peer_connect_failed(peer, 1, rc);
1316 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1321 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1322 unsigned long flags;
1325 /* If I get here, I've committed to send, so I complete the tx with
1326 * failure on any problems */
1328 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1329 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1331 /* First time, just use a read lock since I expect to find my peer
1333 cfs_read_lock_irqsave(g_lock, flags);
1335 peer = kiblnd_find_peer_locked(nid);
1336 if (peer != NULL && !cfs_list_empty(&peer->ibp_conns)) {
1337 /* Found a peer with an established connection */
1338 conn = kiblnd_get_conn_locked(peer);
1339 kiblnd_conn_addref(conn); /* 1 ref for me... */
1341 cfs_read_unlock_irqrestore(g_lock, flags);
1344 kiblnd_queue_tx(tx, conn);
1345 kiblnd_conn_decref(conn); /* ...to here */
1349 cfs_read_unlock(g_lock);
1350 /* Re-try with a write lock */
1351 cfs_write_lock(g_lock);
1353 peer = kiblnd_find_peer_locked(nid);
1355 if (cfs_list_empty(&peer->ibp_conns)) {
1356 /* found a peer, but it's still connecting... */
1357 LASSERT (peer->ibp_connecting != 0 ||
1358 peer->ibp_accepting != 0);
1360 cfs_list_add_tail(&tx->tx_list,
1361 &peer->ibp_tx_queue);
1362 cfs_write_unlock_irqrestore(g_lock, flags);
1364 conn = kiblnd_get_conn_locked(peer);
1365 kiblnd_conn_addref(conn); /* 1 ref for me... */
1367 cfs_write_unlock_irqrestore(g_lock, flags);
1370 kiblnd_queue_tx(tx, conn);
1371 kiblnd_conn_decref(conn); /* ...to here */
1376 cfs_write_unlock_irqrestore(g_lock, flags);
1378 /* Allocate a peer ready to add to the peer table and retry */
1379 rc = kiblnd_create_peer(ni, &peer, nid);
1381 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1383 tx->tx_status = -EHOSTUNREACH;
1385 kiblnd_tx_done(ni, tx);
1390 cfs_write_lock_irqsave(g_lock, flags);
1392 peer2 = kiblnd_find_peer_locked(nid);
1393 if (peer2 != NULL) {
1394 if (cfs_list_empty(&peer2->ibp_conns)) {
1395 /* found a peer, but it's still connecting... */
1396 LASSERT (peer2->ibp_connecting != 0 ||
1397 peer2->ibp_accepting != 0);
1399 cfs_list_add_tail(&tx->tx_list,
1400 &peer2->ibp_tx_queue);
1401 cfs_write_unlock_irqrestore(g_lock, flags);
1403 conn = kiblnd_get_conn_locked(peer2);
1404 kiblnd_conn_addref(conn); /* 1 ref for me... */
1406 cfs_write_unlock_irqrestore(g_lock, flags);
1409 kiblnd_queue_tx(tx, conn);
1410 kiblnd_conn_decref(conn); /* ...to here */
1413 kiblnd_peer_decref(peer);
1417 /* Brand new peer */
1418 LASSERT (peer->ibp_connecting == 0);
1419 peer->ibp_connecting = 1;
1421 /* always called with a ref on ni, which prevents ni being shutdown */
1422 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1425 cfs_list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1427 kiblnd_peer_addref(peer);
1428 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1430 cfs_write_unlock_irqrestore(g_lock, flags);
1432 kiblnd_connect_peer(peer);
1433 kiblnd_peer_decref(peer);
1437 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1439 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1440 int type = lntmsg->msg_type;
1441 lnet_process_id_t target = lntmsg->msg_target;
1442 int target_is_router = lntmsg->msg_target_is_router;
1443 int routing = lntmsg->msg_routing;
1444 unsigned int payload_niov = lntmsg->msg_niov;
1445 struct iovec *payload_iov = lntmsg->msg_iov;
1446 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1447 unsigned int payload_offset = lntmsg->msg_offset;
1448 unsigned int payload_nob = lntmsg->msg_len;
1454 /* NB 'private' is different depending on what we're sending.... */
1456 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1457 payload_nob, payload_niov, libcfs_id2str(target));
1459 LASSERT (payload_nob == 0 || payload_niov > 0);
1460 LASSERT (payload_niov <= LNET_MAX_IOV);
1462 /* Thread context */
1463 LASSERT (!cfs_in_interrupt());
1464 /* payload is either all vaddrs or all pages */
1465 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1473 LASSERT (payload_nob == 0);
1477 if (routing || target_is_router)
1478 break; /* send IMMEDIATE */
1480 /* is the REPLY message too small for RDMA? */
1481 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1482 if (nob <= IBLND_MSG_SIZE)
1483 break; /* send IMMEDIATE */
1485 tx = kiblnd_get_idle_tx(ni);
1487 CERROR("Can't allocate txd for GET to %s: \n",
1488 libcfs_nid2str(target.nid));
1494 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1495 rc = kiblnd_setup_rd_iov(ni, tx,
1496 &ibmsg->ibm_u.get.ibgm_rd,
1497 lntmsg->msg_md->md_niov,
1498 lntmsg->msg_md->md_iov.iov,
1499 0, lntmsg->msg_md->md_length);
1501 rc = kiblnd_setup_rd_kiov(ni, tx,
1502 &ibmsg->ibm_u.get.ibgm_rd,
1503 lntmsg->msg_md->md_niov,
1504 lntmsg->msg_md->md_iov.kiov,
1505 0, lntmsg->msg_md->md_length);
1507 CERROR("Can't setup GET sink for %s: %d\n",
1508 libcfs_nid2str(target.nid), rc);
1509 kiblnd_tx_done(ni, tx);
1513 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1514 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1515 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1517 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1519 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1520 if (tx->tx_lntmsg[1] == NULL) {
1521 CERROR("Can't create reply for GET -> %s\n",
1522 libcfs_nid2str(target.nid));
1523 kiblnd_tx_done(ni, tx);
1527 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1528 tx->tx_waiting = 1; /* waiting for GET_DONE */
1529 kiblnd_launch_tx(ni, tx, target.nid);
1532 case LNET_MSG_REPLY:
1534 /* Is the payload small enough not to need RDMA? */
1535 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1536 if (nob <= IBLND_MSG_SIZE)
1537 break; /* send IMMEDIATE */
1539 tx = kiblnd_get_idle_tx(ni);
1541 CERROR("Can't allocate %s txd for %s\n",
1542 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1543 libcfs_nid2str(target.nid));
1547 if (payload_kiov == NULL)
1548 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1549 payload_niov, payload_iov,
1550 payload_offset, payload_nob);
1552 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1553 payload_niov, payload_kiov,
1554 payload_offset, payload_nob);
1556 CERROR("Can't setup PUT src for %s: %d\n",
1557 libcfs_nid2str(target.nid), rc);
1558 kiblnd_tx_done(ni, tx);
1563 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1564 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1565 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1567 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1568 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1569 kiblnd_launch_tx(ni, tx, target.nid);
1573 /* send IMMEDIATE */
1575 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1578 tx = kiblnd_get_idle_tx(ni);
1580 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1581 type, libcfs_nid2str(target.nid));
1586 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1588 if (payload_kiov != NULL)
1589 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1590 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1591 payload_niov, payload_kiov,
1592 payload_offset, payload_nob);
1594 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1595 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1596 payload_niov, payload_iov,
1597 payload_offset, payload_nob);
1599 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1600 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1602 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1603 kiblnd_launch_tx(ni, tx, target.nid);
1608 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1610 lnet_process_id_t target = lntmsg->msg_target;
1611 unsigned int niov = lntmsg->msg_niov;
1612 struct iovec *iov = lntmsg->msg_iov;
1613 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1614 unsigned int offset = lntmsg->msg_offset;
1615 unsigned int nob = lntmsg->msg_len;
1619 tx = kiblnd_get_idle_tx(ni);
1621 CERROR("Can't get tx for REPLY to %s\n",
1622 libcfs_nid2str(target.nid));
1628 else if (kiov == NULL)
1629 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1630 niov, iov, offset, nob);
1632 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1633 niov, kiov, offset, nob);
1636 CERROR("Can't setup GET src for %s: %d\n",
1637 libcfs_nid2str(target.nid), rc);
1641 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1642 IBLND_MSG_GET_DONE, nob,
1643 &rx->rx_msg->ibm_u.get.ibgm_rd,
1644 rx->rx_msg->ibm_u.get.ibgm_cookie);
1646 CERROR("Can't setup rdma for GET from %s: %d\n",
1647 libcfs_nid2str(target.nid), rc);
1652 /* No RDMA: local completion may happen now! */
1653 lnet_finalize(ni, lntmsg, 0);
1655 /* RDMA: lnet_finalize(lntmsg) when it
1657 tx->tx_lntmsg[0] = lntmsg;
1660 kiblnd_queue_tx(tx, rx->rx_conn);
1664 kiblnd_tx_done(ni, tx);
1666 lnet_finalize(ni, lntmsg, -EIO);
1670 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1671 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1672 unsigned int offset, unsigned int mlen, unsigned int rlen)
1674 kib_rx_t *rx = private;
1675 kib_msg_t *rxmsg = rx->rx_msg;
1676 kib_conn_t *conn = rx->rx_conn;
1680 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1683 LASSERT (mlen <= rlen);
1684 LASSERT (!cfs_in_interrupt());
1685 /* Either all pages or all vaddrs */
1686 LASSERT (!(kiov != NULL && iov != NULL));
1688 switch (rxmsg->ibm_type) {
1692 case IBLND_MSG_IMMEDIATE:
1693 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1694 if (nob > rx->rx_nob) {
1695 CERROR ("Immediate message from %s too big: %d(%d)\n",
1696 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1703 lnet_copy_flat2kiov(niov, kiov, offset,
1704 IBLND_MSG_SIZE, rxmsg,
1705 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1708 lnet_copy_flat2iov(niov, iov, offset,
1709 IBLND_MSG_SIZE, rxmsg,
1710 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1712 lnet_finalize (ni, lntmsg, 0);
1715 case IBLND_MSG_PUT_REQ:
1717 lnet_finalize(ni, lntmsg, 0);
1718 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1719 rxmsg->ibm_u.putreq.ibprm_cookie);
1723 tx = kiblnd_get_idle_tx(ni);
1725 CERROR("Can't allocate tx for %s\n",
1726 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1727 /* Not replying will break the connection */
1734 rc = kiblnd_setup_rd_iov(ni, tx,
1735 &txmsg->ibm_u.putack.ibpam_rd,
1736 niov, iov, offset, mlen);
1738 rc = kiblnd_setup_rd_kiov(ni, tx,
1739 &txmsg->ibm_u.putack.ibpam_rd,
1740 niov, kiov, offset, mlen);
1742 CERROR("Can't setup PUT sink for %s: %d\n",
1743 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1744 kiblnd_tx_done(ni, tx);
1745 /* tell peer it's over */
1746 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1747 rxmsg->ibm_u.putreq.ibprm_cookie);
1751 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1752 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1753 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1755 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1757 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1758 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1759 kiblnd_queue_tx(tx, conn);
1761 /* reposted buffer reserved for PUT_DONE */
1762 post_credit = IBLND_POSTRX_NO_CREDIT;
1765 case IBLND_MSG_GET_REQ:
1766 if (lntmsg != NULL) {
1767 /* Optimized GET; RDMA lntmsg's payload */
1768 kiblnd_reply(ni, rx, lntmsg);
1770 /* GET didn't match anything */
1771 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1773 rxmsg->ibm_u.get.ibgm_cookie);
1778 kiblnd_post_rx(rx, post_credit);
1783 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1785 long pid = cfs_kernel_thread (fn, arg, 0);
1790 cfs_atomic_inc (&kiblnd_data.kib_nthreads);
1795 kiblnd_thread_fini (void)
1797 cfs_atomic_dec (&kiblnd_data.kib_nthreads);
1801 kiblnd_peer_alive (kib_peer_t *peer)
1803 /* This is racy, but everyone's only writing cfs_time_current() */
1804 peer->ibp_last_alive = cfs_time_current();
1809 kiblnd_peer_notify (kib_peer_t *peer)
1812 cfs_time_t last_alive = 0;
1813 unsigned long flags;
1815 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1817 if (cfs_list_empty(&peer->ibp_conns) &&
1818 peer->ibp_accepting == 0 &&
1819 peer->ibp_connecting == 0 &&
1820 peer->ibp_error != 0) {
1821 error = peer->ibp_error;
1822 peer->ibp_error = 0;
1824 last_alive = peer->ibp_last_alive;
1827 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1830 lnet_notify(peer->ibp_ni,
1831 peer->ibp_nid, 0, last_alive);
1835 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1837 /* This just does the immediate housekeeping. 'error' is zero for a
1838 * normal shutdown which can happen only after the connection has been
1839 * established. If the connection is established, schedule the
1840 * connection to be finished off by the connd. Otherwise the connd is
1841 * already dealing with it (either to set it up or tear it down).
1842 * Caller holds kib_global_lock exclusively in irq context */
1843 kib_peer_t *peer = conn->ibc_peer;
1845 unsigned long flags;
1847 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1849 if (error != 0 && conn->ibc_comms_error == 0)
1850 conn->ibc_comms_error = error;
1852 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1853 return; /* already being handled */
1856 cfs_list_empty(&conn->ibc_tx_noops) &&
1857 cfs_list_empty(&conn->ibc_tx_queue) &&
1858 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) &&
1859 cfs_list_empty(&conn->ibc_tx_queue_nocred) &&
1860 cfs_list_empty(&conn->ibc_active_txs)) {
1861 CDEBUG(D_NET, "closing conn to %s\n",
1862 libcfs_nid2str(peer->ibp_nid));
1864 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1865 libcfs_nid2str(peer->ibp_nid), error,
1866 cfs_list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1867 cfs_list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1868 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1869 cfs_list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1870 cfs_list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1873 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1874 cfs_list_del(&conn->ibc_list);
1875 /* connd (see below) takes over ibc_list's ref */
1877 if (cfs_list_empty (&peer->ibp_conns) && /* no more conns */
1878 kiblnd_peer_active(peer)) { /* still in peer table */
1879 kiblnd_unlink_peer_locked(peer);
1881 /* set/clear error on last conn */
1882 peer->ibp_error = conn->ibc_comms_error;
1885 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1888 kiblnd_dev_can_failover(dev)) {
1889 cfs_list_add_tail(&dev->ibd_fail_list,
1890 &kiblnd_data.kib_failed_devs);
1891 cfs_waitq_signal(&kiblnd_data.kib_failover_waitq);
1894 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1896 cfs_list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1897 cfs_waitq_signal (&kiblnd_data.kib_connd_waitq);
1899 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1903 kiblnd_close_conn (kib_conn_t *conn, int error)
1905 unsigned long flags;
1907 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1909 kiblnd_close_conn_locked(conn, error);
1911 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1915 kiblnd_handle_early_rxs(kib_conn_t *conn)
1917 unsigned long flags;
1920 LASSERT (!cfs_in_interrupt());
1921 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1923 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1924 while (!cfs_list_empty(&conn->ibc_early_rxs)) {
1925 rx = cfs_list_entry(conn->ibc_early_rxs.next,
1927 cfs_list_del(&rx->rx_list);
1928 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
1931 kiblnd_handle_rx(rx);
1933 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1935 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1939 kiblnd_abort_txs(kib_conn_t *conn, cfs_list_t *txs)
1941 CFS_LIST_HEAD (zombies);
1946 cfs_spin_lock(&conn->ibc_lock);
1948 cfs_list_for_each_safe (tmp, nxt, txs) {
1949 tx = cfs_list_entry (tmp, kib_tx_t, tx_list);
1951 if (txs == &conn->ibc_active_txs) {
1952 LASSERT (!tx->tx_queued);
1953 LASSERT (tx->tx_waiting ||
1954 tx->tx_sending != 0);
1956 LASSERT (tx->tx_queued);
1959 tx->tx_status = -ECONNABORTED;
1962 if (tx->tx_sending == 0) {
1964 cfs_list_del (&tx->tx_list);
1965 cfs_list_add (&tx->tx_list, &zombies);
1969 cfs_spin_unlock(&conn->ibc_lock);
1971 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
1972 &zombies, -ECONNABORTED);
1976 kiblnd_finalise_conn (kib_conn_t *conn)
1978 LASSERT (!cfs_in_interrupt());
1979 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
1981 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
1983 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
1984 * for connections that didn't get as far as being connected, because
1985 * rdma_disconnect() does this for free. */
1986 kiblnd_abort_receives(conn);
1988 /* Complete all tx descs not waiting for sends to complete.
1989 * NB we should be safe from RDMA now that the QP has changed state */
1991 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
1992 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
1993 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
1994 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
1995 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
1997 kiblnd_handle_early_rxs(conn);
2001 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
2003 CFS_LIST_HEAD (zombies);
2004 unsigned long flags;
2006 LASSERT (error != 0);
2007 LASSERT (!cfs_in_interrupt());
2009 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2012 LASSERT (peer->ibp_connecting > 0);
2013 peer->ibp_connecting--;
2015 LASSERT (peer->ibp_accepting > 0);
2016 peer->ibp_accepting--;
2019 if (peer->ibp_connecting != 0 ||
2020 peer->ibp_accepting != 0) {
2021 /* another connection attempt under way... */
2022 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2027 if (cfs_list_empty(&peer->ibp_conns)) {
2028 /* Take peer's blocked transmits to complete with error */
2029 cfs_list_add(&zombies, &peer->ibp_tx_queue);
2030 cfs_list_del_init(&peer->ibp_tx_queue);
2032 if (kiblnd_peer_active(peer))
2033 kiblnd_unlink_peer_locked(peer);
2035 peer->ibp_error = error;
2037 /* Can't have blocked transmits if there are connections */
2038 LASSERT (cfs_list_empty(&peer->ibp_tx_queue));
2041 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2043 kiblnd_peer_notify(peer);
2045 if (cfs_list_empty (&zombies))
2048 CNETERR("Deleting messages for %s: connection failed\n",
2049 libcfs_nid2str(peer->ibp_nid));
2051 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2055 kiblnd_connreq_done(kib_conn_t *conn, int status)
2057 kib_peer_t *peer = conn->ibc_peer;
2060 unsigned long flags;
2063 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2065 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2066 libcfs_nid2str(peer->ibp_nid), active,
2067 conn->ibc_version, status);
2069 LASSERT (!cfs_in_interrupt());
2070 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2071 peer->ibp_connecting > 0) ||
2072 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2073 peer->ibp_accepting > 0));
2075 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2076 conn->ibc_connvars = NULL;
2079 /* failed to establish connection */
2080 kiblnd_peer_connect_failed(peer, active, status);
2081 kiblnd_finalise_conn(conn);
2085 /* connection established */
2086 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2088 conn->ibc_last_send = jiffies;
2089 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2090 kiblnd_peer_alive(peer);
2092 /* Add conn to peer's list and nuke any dangling conns from a different
2093 * peer instance... */
2094 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2095 cfs_list_add(&conn->ibc_list, &peer->ibp_conns);
2097 peer->ibp_connecting--;
2099 peer->ibp_accepting--;
2101 if (peer->ibp_version == 0) {
2102 peer->ibp_version = conn->ibc_version;
2103 peer->ibp_incarnation = conn->ibc_incarnation;
2106 if (peer->ibp_version != conn->ibc_version ||
2107 peer->ibp_incarnation != conn->ibc_incarnation) {
2108 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2109 conn->ibc_incarnation);
2110 peer->ibp_version = conn->ibc_version;
2111 peer->ibp_incarnation = conn->ibc_incarnation;
2114 /* grab pending txs while I have the lock */
2115 cfs_list_add(&txs, &peer->ibp_tx_queue);
2116 cfs_list_del_init(&peer->ibp_tx_queue);
2118 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2119 conn->ibc_comms_error != 0) { /* error has happened already */
2120 lnet_ni_t *ni = peer->ibp_ni;
2122 /* start to shut down connection */
2123 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2124 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2127 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2132 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2134 /* Schedule blocked txs */
2135 cfs_spin_lock (&conn->ibc_lock);
2136 while (!cfs_list_empty (&txs)) {
2137 tx = cfs_list_entry (txs.next, kib_tx_t, tx_list);
2138 cfs_list_del(&tx->tx_list);
2140 kiblnd_queue_tx_locked(tx, conn);
2142 cfs_spin_unlock (&conn->ibc_lock);
2144 kiblnd_check_sends(conn);
2146 /* schedule blocked rxs */
2147 kiblnd_handle_early_rxs(conn);
2151 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2155 rc = rdma_reject(cmid, rej, sizeof(*rej));
2158 CWARN("Error %d sending reject\n", rc);
2162 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2164 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2165 kib_msg_t *reqmsg = priv;
2171 lnet_ni_t *ni = NULL;
2172 kib_net_t *net = NULL;
2174 struct rdma_conn_param cp;
2176 int version = IBLND_MSG_VERSION;
2177 unsigned long flags;
2179 struct sockaddr_in *peer_addr;
2180 LASSERT (!cfs_in_interrupt());
2182 /* cmid inherits 'context' from the corresponding listener id */
2183 ibdev = (kib_dev_t *)cmid->context;
2184 LASSERT (ibdev != NULL);
2186 memset(&rej, 0, sizeof(rej));
2187 rej.ibr_magic = IBLND_MSG_MAGIC;
2188 rej.ibr_why = IBLND_REJECT_FATAL;
2189 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2191 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2192 if (*kiblnd_tunables.kib_require_priv_port &&
2193 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2194 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2195 CERROR("Peer's port (%u.%u.%u.%u:%hu) is not privileged\n",
2196 HIPQUAD(ip), ntohs(peer_addr->sin_port));
2200 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2201 CERROR("Short connection request\n");
2205 /* Future protocol version compatibility support! If the
2206 * o2iblnd-specific protocol changes, or when LNET unifies
2207 * protocols over all LNDs, the initial connection will
2208 * negotiate a protocol version. I trap this here to avoid
2209 * console errors; the reject tells the peer which protocol I
2211 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2212 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2214 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2215 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2216 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2218 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2219 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2220 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2223 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2225 CERROR("Can't parse connection request: %d\n", rc);
2229 nid = reqmsg->ibm_srcnid;
2230 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2233 net = (kib_net_t *)ni->ni_data;
2234 rej.ibr_incarnation = net->ibn_incarnation;
2237 if (ni == NULL || /* no matching net */
2238 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2239 net->ibn_dev != ibdev) { /* wrong device */
2240 CERROR("Can't accept %s on %s (%s:%d:%u.%u.%u.%u): "
2241 "bad dst nid %s\n", libcfs_nid2str(nid),
2242 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2243 ibdev->ibd_ifname, ibdev->ibd_nnets,
2244 HIPQUAD(ibdev->ibd_ifip),
2245 libcfs_nid2str(reqmsg->ibm_dstnid));
2250 /* check time stamp as soon as possible */
2251 if (reqmsg->ibm_dststamp != 0 &&
2252 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2253 CWARN("Stale connection request\n");
2254 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2258 /* I can accept peer's version */
2259 version = reqmsg->ibm_version;
2261 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2262 CERROR("Unexpected connreq msg type: %x from %s\n",
2263 reqmsg->ibm_type, libcfs_nid2str(nid));
2267 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2268 IBLND_MSG_QUEUE_SIZE(version)) {
2269 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2270 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2271 IBLND_MSG_QUEUE_SIZE(version));
2273 if (version == IBLND_MSG_VERSION)
2274 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2279 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2280 IBLND_RDMA_FRAGS(version)) {
2281 CERROR("Can't accept %s(version %x): "
2282 "incompatible max_frags %d (%d wanted)\n",
2283 libcfs_nid2str(nid), version,
2284 reqmsg->ibm_u.connparams.ibcp_max_frags,
2285 IBLND_RDMA_FRAGS(version));
2287 if (version == IBLND_MSG_VERSION)
2288 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2294 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2295 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2296 libcfs_nid2str(nid),
2297 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2302 /* assume 'nid' is a new peer; create */
2303 rc = kiblnd_create_peer(ni, &peer, nid);
2305 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2306 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2310 cfs_write_lock_irqsave(g_lock, flags);
2312 peer2 = kiblnd_find_peer_locked(nid);
2313 if (peer2 != NULL) {
2314 if (peer2->ibp_version == 0) {
2315 peer2->ibp_version = version;
2316 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2319 /* not the guy I've talked with */
2320 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2321 peer2->ibp_version != version) {
2322 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2323 cfs_write_unlock_irqrestore(g_lock, flags);
2325 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2326 libcfs_nid2str(nid), peer2->ibp_version, version);
2328 kiblnd_peer_decref(peer);
2329 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2333 /* tie-break connection race in favour of the higher NID */
2334 if (peer2->ibp_connecting != 0 &&
2336 cfs_write_unlock_irqrestore(g_lock, flags);
2338 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2340 kiblnd_peer_decref(peer);
2341 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2345 peer2->ibp_accepting++;
2346 kiblnd_peer_addref(peer2);
2348 cfs_write_unlock_irqrestore(g_lock, flags);
2349 kiblnd_peer_decref(peer);
2352 /* Brand new peer */
2353 LASSERT (peer->ibp_accepting == 0);
2354 LASSERT (peer->ibp_version == 0 &&
2355 peer->ibp_incarnation == 0);
2357 peer->ibp_accepting = 1;
2358 peer->ibp_version = version;
2359 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2361 /* I have a ref on ni that prevents it being shutdown */
2362 LASSERT (net->ibn_shutdown == 0);
2364 kiblnd_peer_addref(peer);
2365 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2367 cfs_write_unlock_irqrestore(g_lock, flags);
2370 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2372 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2373 kiblnd_peer_decref(peer);
2374 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2378 /* conn now "owns" cmid, so I return success from here on to ensure the
2379 * CM callback doesn't destroy cmid. */
2381 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2382 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2383 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2384 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2385 <= IBLND_RX_MSGS(version));
2387 ackmsg = &conn->ibc_connvars->cv_msg;
2388 memset(ackmsg, 0, sizeof(*ackmsg));
2390 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2391 sizeof(ackmsg->ibm_u.connparams));
2392 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2393 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2394 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2396 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2398 memset(&cp, 0, sizeof(cp));
2399 cp.private_data = ackmsg;
2400 cp.private_data_len = ackmsg->ibm_nob;
2401 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2402 cp.initiator_depth = 0;
2403 cp.flow_control = 1;
2404 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2405 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2407 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2409 rc = rdma_accept(cmid, &cp);
2411 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2412 rej.ibr_version = version;
2413 rej.ibr_why = IBLND_REJECT_FATAL;
2415 kiblnd_reject(cmid, &rej);
2416 kiblnd_connreq_done(conn, rc);
2417 kiblnd_conn_decref(conn);
2427 rej.ibr_version = version;
2428 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2429 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2430 kiblnd_reject(cmid, &rej);
2432 return -ECONNREFUSED;
2436 kiblnd_reconnect (kib_conn_t *conn, int version,
2437 __u64 incarnation, int why, kib_connparams_t *cp)
2439 kib_peer_t *peer = conn->ibc_peer;
2442 unsigned long flags;
2444 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2445 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2447 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2449 /* retry connection if it's still needed and no other connection
2450 * attempts (active or passive) are in progress
2451 * NB: reconnect is still needed even when ibp_tx_queue is
2452 * empty if ibp_version != version because reconnect may be
2453 * initiated by kiblnd_query() */
2454 if ((!cfs_list_empty(&peer->ibp_tx_queue) ||
2455 peer->ibp_version != version) &&
2456 peer->ibp_connecting == 1 &&
2457 peer->ibp_accepting == 0) {
2459 peer->ibp_connecting++;
2461 peer->ibp_version = version;
2462 peer->ibp_incarnation = incarnation;
2465 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2475 case IBLND_REJECT_CONN_STALE:
2479 case IBLND_REJECT_CONN_RACE:
2480 reason = "conn race";
2483 case IBLND_REJECT_CONN_UNCOMPAT:
2484 reason = "version negotiation";
2488 CNETERR("%s: retrying (%s), %x, %x, "
2489 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2490 libcfs_nid2str(peer->ibp_nid),
2491 reason, IBLND_MSG_VERSION, version,
2492 cp != NULL? cp->ibcp_queue_depth :IBLND_MSG_QUEUE_SIZE(version),
2493 cp != NULL? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2494 cp != NULL? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2496 kiblnd_connect_peer(peer);
2500 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2502 kib_peer_t *peer = conn->ibc_peer;
2504 LASSERT (!cfs_in_interrupt());
2505 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2508 case IB_CM_REJ_STALE_CONN:
2509 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2510 IBLND_REJECT_CONN_STALE, NULL);
2513 case IB_CM_REJ_INVALID_SERVICE_ID:
2514 CNETERR("%s rejected: no listener at %d\n",
2515 libcfs_nid2str(peer->ibp_nid),
2516 *kiblnd_tunables.kib_service);
2519 case IB_CM_REJ_CONSUMER_DEFINED:
2520 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2521 kib_rej_t *rej = priv;
2522 kib_connparams_t *cp = NULL;
2524 __u64 incarnation = -1;
2526 /* NB. default incarnation is -1 because:
2527 * a) V1 will ignore dst incarnation in connreq.
2528 * b) V2 will provide incarnation while rejecting me,
2529 * -1 will be overwrote.
2531 * if I try to connect to a V1 peer with V2 protocol,
2532 * it rejected me then upgrade to V2, I have no idea
2533 * about the upgrading and try to reconnect with V1,
2534 * in this case upgraded V2 can find out I'm trying to
2535 * talk to the old guy and reject me(incarnation is -1).
2538 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2539 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2540 __swab32s(&rej->ibr_magic);
2541 __swab16s(&rej->ibr_version);
2545 if (priv_nob >= sizeof(kib_rej_t) &&
2546 rej->ibr_version > IBLND_MSG_VERSION_1) {
2547 /* priv_nob is always 148 in current version
2548 * of OFED, so we still need to check version.
2549 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2553 __swab64s(&rej->ibr_incarnation);
2554 __swab16s(&cp->ibcp_queue_depth);
2555 __swab16s(&cp->ibcp_max_frags);
2556 __swab32s(&cp->ibcp_max_msg_size);
2559 incarnation = rej->ibr_incarnation;
2562 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2563 rej->ibr_magic != LNET_PROTO_MAGIC) {
2564 CERROR("%s rejected: consumer defined fatal error\n",
2565 libcfs_nid2str(peer->ibp_nid));
2569 if (rej->ibr_version != IBLND_MSG_VERSION &&
2570 rej->ibr_version != IBLND_MSG_VERSION_1) {
2571 CERROR("%s rejected: o2iblnd version %x error\n",
2572 libcfs_nid2str(peer->ibp_nid),
2577 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2578 rej->ibr_version == IBLND_MSG_VERSION_1) {
2579 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2580 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2582 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2583 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2586 switch (rej->ibr_why) {
2587 case IBLND_REJECT_CONN_RACE:
2588 case IBLND_REJECT_CONN_STALE:
2589 case IBLND_REJECT_CONN_UNCOMPAT:
2590 kiblnd_reconnect(conn, rej->ibr_version,
2591 incarnation, rej->ibr_why, cp);
2594 case IBLND_REJECT_MSG_QUEUE_SIZE:
2595 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2596 libcfs_nid2str(peer->ibp_nid), cp->ibcp_queue_depth,
2597 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2600 case IBLND_REJECT_RDMA_FRAGS:
2601 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2602 libcfs_nid2str(peer->ibp_nid), cp->ibcp_max_frags,
2603 IBLND_RDMA_FRAGS(conn->ibc_version));
2606 case IBLND_REJECT_NO_RESOURCES:
2607 CERROR("%s rejected: o2iblnd no resources\n",
2608 libcfs_nid2str(peer->ibp_nid));
2611 case IBLND_REJECT_FATAL:
2612 CERROR("%s rejected: o2iblnd fatal error\n",
2613 libcfs_nid2str(peer->ibp_nid));
2617 CERROR("%s rejected: o2iblnd reason %d\n",
2618 libcfs_nid2str(peer->ibp_nid),
2626 CNETERR("%s rejected: reason %d, size %d\n",
2627 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2631 kiblnd_connreq_done(conn, -ECONNREFUSED);
2635 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2637 kib_peer_t *peer = conn->ibc_peer;
2638 lnet_ni_t *ni = peer->ibp_ni;
2639 kib_net_t *net = ni->ni_data;
2640 kib_msg_t *msg = priv;
2641 int ver = conn->ibc_version;
2642 int rc = kiblnd_unpack_msg(msg, priv_nob);
2643 unsigned long flags;
2645 LASSERT (net != NULL);
2648 CERROR("Can't unpack connack from %s: %d\n",
2649 libcfs_nid2str(peer->ibp_nid), rc);
2653 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2654 CERROR("Unexpected message %d from %s\n",
2655 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2660 if (ver != msg->ibm_version) {
2661 CERROR("%s replied version %x is different with "
2662 "requested version %x\n",
2663 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2668 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2669 IBLND_MSG_QUEUE_SIZE(ver)) {
2670 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2671 libcfs_nid2str(peer->ibp_nid),
2672 msg->ibm_u.connparams.ibcp_queue_depth,
2673 IBLND_MSG_QUEUE_SIZE(ver));
2678 if (msg->ibm_u.connparams.ibcp_max_frags !=
2679 IBLND_RDMA_FRAGS(ver)) {
2680 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2681 libcfs_nid2str(peer->ibp_nid),
2682 msg->ibm_u.connparams.ibcp_max_frags,
2683 IBLND_RDMA_FRAGS(ver));
2688 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2689 CERROR("%s max message size %d too big (%d max)\n",
2690 libcfs_nid2str(peer->ibp_nid),
2691 msg->ibm_u.connparams.ibcp_max_msg_size,
2697 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2698 if (msg->ibm_dstnid == ni->ni_nid &&
2699 msg->ibm_dststamp == net->ibn_incarnation)
2703 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2706 CERROR("Bad connection reply from %s, rc = %d, "
2707 "version: %x max_frags: %d\n",
2708 libcfs_nid2str(peer->ibp_nid), rc,
2709 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2713 conn->ibc_incarnation = msg->ibm_srcstamp;
2715 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2716 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2717 <= IBLND_RX_MSGS(ver));
2719 kiblnd_connreq_done(conn, 0);
2723 /* NB My QP has already established itself, so I handle anything going
2724 * wrong here by setting ibc_comms_error.
2725 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2726 * immediately tears it down. */
2729 conn->ibc_comms_error = rc;
2730 kiblnd_connreq_done(conn, 0);
2734 kiblnd_active_connect (struct rdma_cm_id *cmid)
2736 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2739 struct rdma_conn_param cp;
2742 unsigned long flags;
2745 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2747 incarnation = peer->ibp_incarnation;
2748 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION : peer->ibp_version;
2750 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2752 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2754 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2755 kiblnd_peer_decref(peer); /* lose cmid's ref */
2759 /* conn "owns" cmid now, so I return success from here on to ensure the
2760 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2763 msg = &conn->ibc_connvars->cv_msg;
2765 memset(msg, 0, sizeof(*msg));
2766 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2767 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2768 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2769 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2771 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2772 0, peer->ibp_nid, incarnation);
2774 memset(&cp, 0, sizeof(cp));
2775 cp.private_data = msg;
2776 cp.private_data_len = msg->ibm_nob;
2777 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2778 cp.initiator_depth = 0;
2779 cp.flow_control = 1;
2780 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2781 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2783 LASSERT(cmid->context == (void *)conn);
2784 LASSERT(conn->ibc_cmid == cmid);
2786 rc = rdma_connect(cmid, &cp);
2788 CERROR("Can't connect to %s: %d\n",
2789 libcfs_nid2str(peer->ibp_nid), rc);
2790 kiblnd_connreq_done(conn, rc);
2791 kiblnd_conn_decref(conn);
2798 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2804 switch (event->event) {
2806 CERROR("Unexpected event: %d, status: %d\n",
2807 event->event, event->status);
2810 case RDMA_CM_EVENT_CONNECT_REQUEST:
2811 /* destroy cmid on failure */
2812 rc = kiblnd_passive_connect(cmid,
2813 (void *)KIBLND_CONN_PARAM(event),
2814 KIBLND_CONN_PARAM_LEN(event));
2815 CDEBUG(D_NET, "connreq: %d\n", rc);
2818 case RDMA_CM_EVENT_ADDR_ERROR:
2819 peer = (kib_peer_t *)cmid->context;
2820 CNETERR("%s: ADDR ERROR %d\n",
2821 libcfs_nid2str(peer->ibp_nid), event->status);
2822 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2823 kiblnd_peer_decref(peer);
2824 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2826 case RDMA_CM_EVENT_ADDR_RESOLVED:
2827 peer = (kib_peer_t *)cmid->context;
2829 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2830 libcfs_nid2str(peer->ibp_nid), event->status);
2832 if (event->status != 0) {
2833 CNETERR("Can't resolve address for %s: %d\n",
2834 libcfs_nid2str(peer->ibp_nid), event->status);
2837 rc = rdma_resolve_route(
2838 cmid, *kiblnd_tunables.kib_timeout * 1000);
2841 /* Can't initiate route resolution */
2842 CERROR("Can't resolve route for %s: %d\n",
2843 libcfs_nid2str(peer->ibp_nid), rc);
2845 kiblnd_peer_connect_failed(peer, 1, rc);
2846 kiblnd_peer_decref(peer);
2847 return rc; /* rc != 0 destroys cmid */
2849 case RDMA_CM_EVENT_ROUTE_ERROR:
2850 peer = (kib_peer_t *)cmid->context;
2851 CNETERR("%s: ROUTE ERROR %d\n",
2852 libcfs_nid2str(peer->ibp_nid), event->status);
2853 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2854 kiblnd_peer_decref(peer);
2855 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2857 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2858 peer = (kib_peer_t *)cmid->context;
2859 CDEBUG(D_NET,"%s Route resolved: %d\n",
2860 libcfs_nid2str(peer->ibp_nid), event->status);
2862 if (event->status == 0)
2863 return kiblnd_active_connect(cmid);
2865 CNETERR("Can't resolve route for %s: %d\n",
2866 libcfs_nid2str(peer->ibp_nid), event->status);
2867 kiblnd_peer_connect_failed(peer, 1, event->status);
2868 kiblnd_peer_decref(peer);
2869 return event->status; /* rc != 0 destroys cmid */
2871 case RDMA_CM_EVENT_UNREACHABLE:
2872 conn = (kib_conn_t *)cmid->context;
2873 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2874 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2875 CNETERR("%s: UNREACHABLE %d\n",
2876 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2877 kiblnd_connreq_done(conn, -ENETDOWN);
2878 kiblnd_conn_decref(conn);
2881 case RDMA_CM_EVENT_CONNECT_ERROR:
2882 conn = (kib_conn_t *)cmid->context;
2883 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2884 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2885 CNETERR("%s: CONNECT ERROR %d\n",
2886 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2887 kiblnd_connreq_done(conn, -ENOTCONN);
2888 kiblnd_conn_decref(conn);
2891 case RDMA_CM_EVENT_REJECTED:
2892 conn = (kib_conn_t *)cmid->context;
2893 switch (conn->ibc_state) {
2897 case IBLND_CONN_PASSIVE_WAIT:
2898 CERROR ("%s: REJECTED %d\n",
2899 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2901 kiblnd_connreq_done(conn, -ECONNRESET);
2904 case IBLND_CONN_ACTIVE_CONNECT:
2905 kiblnd_rejected(conn, event->status,
2906 (void *)KIBLND_CONN_PARAM(event),
2907 KIBLND_CONN_PARAM_LEN(event));
2910 kiblnd_conn_decref(conn);
2913 case RDMA_CM_EVENT_ESTABLISHED:
2914 conn = (kib_conn_t *)cmid->context;
2915 switch (conn->ibc_state) {
2919 case IBLND_CONN_PASSIVE_WAIT:
2920 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2921 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2922 kiblnd_connreq_done(conn, 0);
2925 case IBLND_CONN_ACTIVE_CONNECT:
2926 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2927 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2928 kiblnd_check_connreply(conn,
2929 (void *)KIBLND_CONN_PARAM(event),
2930 KIBLND_CONN_PARAM_LEN(event));
2933 /* net keeps its ref on conn! */
2936 #ifdef HAVE_OFED_RDMA_CMEV_TIMEWAIT_EXIT
2937 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2938 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2941 case RDMA_CM_EVENT_DISCONNECTED:
2942 conn = (kib_conn_t *)cmid->context;
2943 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2944 CERROR("%s DISCONNECTED\n",
2945 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2946 kiblnd_connreq_done(conn, -ECONNRESET);
2948 kiblnd_close_conn(conn, 0);
2950 kiblnd_conn_decref(conn);
2951 cmid->context = NULL;
2954 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2955 LCONSOLE_ERROR_MSG(0x131,
2956 "Received notification of device removal\n"
2957 "Please shutdown LNET to allow this to proceed\n");
2958 /* Can't remove network from underneath LNET for now, so I have
2962 #ifdef HAVE_OFED_RDMA_CMEV_ADDRCHANGE
2963 case RDMA_CM_EVENT_ADDR_CHANGE:
2964 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2971 kiblnd_check_txs (kib_conn_t *conn, cfs_list_t *txs)
2977 cfs_spin_lock(&conn->ibc_lock);
2979 cfs_list_for_each (ttmp, txs) {
2980 tx = cfs_list_entry (ttmp, kib_tx_t, tx_list);
2982 if (txs != &conn->ibc_active_txs) {
2983 LASSERT (tx->tx_queued);
2985 LASSERT (!tx->tx_queued);
2986 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2989 if (cfs_time_aftereq (jiffies, tx->tx_deadline)) {
2991 CERROR("Timed out tx: %s, %lu seconds\n",
2992 kiblnd_queue2str(conn, txs),
2993 cfs_duration_sec(jiffies - tx->tx_deadline));
2998 cfs_spin_unlock(&conn->ibc_lock);
3003 kiblnd_conn_timed_out (kib_conn_t *conn)
3005 return kiblnd_check_txs(conn, &conn->ibc_tx_queue) ||
3006 kiblnd_check_txs(conn, &conn->ibc_tx_noops) ||
3007 kiblnd_check_txs(conn, &conn->ibc_tx_queue_rsrvd) ||
3008 kiblnd_check_txs(conn, &conn->ibc_tx_queue_nocred) ||
3009 kiblnd_check_txs(conn, &conn->ibc_active_txs);
3013 kiblnd_check_conns (int idx)
3015 cfs_list_t *peers = &kiblnd_data.kib_peers[idx];
3020 unsigned long flags;
3023 /* NB. We expect to have a look at all the peers and not find any
3024 * rdmas to time out, so we just use a shared lock while we
3026 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3028 cfs_list_for_each (ptmp, peers) {
3029 peer = cfs_list_entry (ptmp, kib_peer_t, ibp_list);
3031 cfs_list_for_each (ctmp, &peer->ibp_conns) {
3032 conn = cfs_list_entry (ctmp, kib_conn_t, ibc_list);
3034 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
3036 /* In case we have enough credits to return via a
3037 * NOOP, but there were no non-blocking tx descs
3038 * free to do it last time... */
3039 kiblnd_check_sends(conn);
3041 if (!kiblnd_conn_timed_out(conn))
3044 /* Handle timeout by closing the whole connection. We
3045 * can only be sure RDMA activity has ceased once the
3046 * QP has been modified. */
3048 kiblnd_conn_addref(conn); /* 1 ref for me... */
3050 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
3053 CERROR("Timed out RDMA with %s (%lu)\n",
3054 libcfs_nid2str(peer->ibp_nid),
3055 cfs_duration_sec(cfs_time_current() -
3056 peer->ibp_last_alive));
3058 kiblnd_close_conn(conn, -ETIMEDOUT);
3059 kiblnd_conn_decref(conn); /* ...until here */
3061 /* start again now I've dropped the lock */
3066 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3070 kiblnd_disconnect_conn (kib_conn_t *conn)
3072 LASSERT (!cfs_in_interrupt());
3073 LASSERT (current == kiblnd_data.kib_connd);
3074 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3076 rdma_disconnect(conn->ibc_cmid);
3077 kiblnd_finalise_conn(conn);
3079 kiblnd_peer_notify(conn->ibc_peer);
3083 kiblnd_connd (void *arg)
3085 cfs_waitlink_t wait;
3086 unsigned long flags;
3092 unsigned long deadline = jiffies;
3094 cfs_daemonize ("kiblnd_connd");
3095 cfs_block_allsigs ();
3097 cfs_waitlink_init (&wait);
3098 kiblnd_data.kib_connd = current;
3100 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3102 while (!kiblnd_data.kib_shutdown) {
3106 if (!cfs_list_empty (&kiblnd_data.kib_connd_zombies)) {
3107 conn = cfs_list_entry(kiblnd_data. \
3108 kib_connd_zombies.next,
3109 kib_conn_t, ibc_list);
3110 cfs_list_del(&conn->ibc_list);
3112 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3116 kiblnd_destroy_conn(conn);
3118 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3122 if (!cfs_list_empty (&kiblnd_data.kib_connd_conns)) {
3123 conn = cfs_list_entry (kiblnd_data.kib_connd_conns.next,
3124 kib_conn_t, ibc_list);
3125 cfs_list_del(&conn->ibc_list);
3127 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3131 kiblnd_disconnect_conn(conn);
3132 kiblnd_conn_decref(conn);
3134 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3138 /* careful with the jiffy wrap... */
3139 timeout = (int)(deadline - jiffies);
3143 int chunk = kiblnd_data.kib_peer_hash_size;
3145 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3148 /* Time to check for RDMA timeouts on a few more
3149 * peers: I do checks every 'p' seconds on a
3150 * proportion of the peer table and I need to check
3151 * every connection 'n' times within a timeout
3152 * interval, to ensure I detect a timeout on any
3153 * connection within (n+1)/n times the timeout
3156 if (*kiblnd_tunables.kib_timeout > n * p)
3157 chunk = (chunk * n * p) /
3158 *kiblnd_tunables.kib_timeout;
3162 for (i = 0; i < chunk; i++) {
3163 kiblnd_check_conns(peer_index);
3164 peer_index = (peer_index + 1) %
3165 kiblnd_data.kib_peer_hash_size;
3168 deadline += p * CFS_HZ;
3169 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock,
3176 /* Nothing to do for 'timeout' */
3177 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
3178 cfs_waitq_add (&kiblnd_data.kib_connd_waitq, &wait);
3179 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3181 cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE, timeout);
3183 cfs_set_current_state (CFS_TASK_RUNNING);
3184 cfs_waitq_del (&kiblnd_data.kib_connd_waitq, &wait);
3185 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
3188 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3190 kiblnd_thread_fini();
3195 kiblnd_qp_event(struct ib_event *event, void *arg)
3197 kib_conn_t *conn = arg;
3199 switch (event->event) {
3200 case IB_EVENT_COMM_EST:
3201 CDEBUG(D_NET, "%s established\n",
3202 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3206 CERROR("%s: Async QP event type %d\n",
3207 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3213 kiblnd_complete (struct ib_wc *wc)
3215 switch (kiblnd_wreqid2type(wc->wr_id)) {
3219 case IBLND_WID_RDMA:
3220 /* We only get RDMA completion notification if it fails. All
3221 * subsequent work items, including the final SEND will fail
3222 * too. However we can't print out any more info about the
3223 * failing RDMA because 'tx' might be back on the idle list or
3224 * even reused already if we didn't manage to post all our work
3226 CNETERR("RDMA (tx: %p) failed: %d\n",
3227 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3231 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3235 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3242 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3244 /* NB I'm not allowed to schedule this conn once its refcount has
3245 * reached 0. Since fundamentally I'm racing with scheduler threads
3246 * consuming my CQ I could be called after all completions have
3247 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3248 * and this CQ is about to be destroyed so I NOOP. */
3249 kib_conn_t *conn = (kib_conn_t *)arg;
3250 unsigned long flags;
3252 LASSERT (cq == conn->ibc_cq);
3254 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3256 conn->ibc_ready = 1;
3258 if (!conn->ibc_scheduled &&
3259 (conn->ibc_nrx > 0 ||
3260 conn->ibc_nsends_posted > 0)) {
3261 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3262 conn->ibc_scheduled = 1;
3263 cfs_list_add_tail(&conn->ibc_sched_list,
3264 &kiblnd_data.kib_sched_conns);
3265 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3268 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3272 kiblnd_cq_event(struct ib_event *event, void *arg)
3274 kib_conn_t *conn = arg;
3276 CERROR("%s: async CQ event type %d\n",
3277 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3281 kiblnd_scheduler(void *arg)
3283 long id = (long)arg;
3284 cfs_waitlink_t wait;
3286 unsigned long flags;
3293 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3294 cfs_daemonize(name);
3295 cfs_block_allsigs();
3297 cfs_waitlink_init(&wait);
3299 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3301 while (!kiblnd_data.kib_shutdown) {
3302 if (busy_loops++ >= IBLND_RESCHED) {
3303 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3309 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3315 if (!cfs_list_empty(&kiblnd_data.kib_sched_conns)) {
3316 conn = cfs_list_entry(kiblnd_data.kib_sched_conns.next,
3317 kib_conn_t, ibc_sched_list);
3318 /* take over kib_sched_conns' ref on conn... */
3319 LASSERT(conn->ibc_scheduled);
3320 cfs_list_del(&conn->ibc_sched_list);
3321 conn->ibc_ready = 0;
3323 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3326 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3328 rc = ib_req_notify_cq(conn->ibc_cq,
3331 CWARN("%s: ib_req_notify_cq failed: %d, "
3332 "closing connection\n",
3333 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3334 kiblnd_close_conn(conn, -EIO);
3335 kiblnd_conn_decref(conn);
3336 cfs_spin_lock_irqsave(&kiblnd_data. \
3342 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3346 CWARN("%s: ib_poll_cq failed: %d, "
3347 "closing connection\n",
3348 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3350 kiblnd_close_conn(conn, -EIO);
3351 kiblnd_conn_decref(conn);
3352 cfs_spin_lock_irqsave(&kiblnd_data. \
3353 kib_sched_lock, flags);
3357 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3360 if (rc != 0 || conn->ibc_ready) {
3361 /* There may be another completion waiting; get
3362 * another scheduler to check while I handle
3364 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3365 cfs_list_add_tail(&conn->ibc_sched_list,
3366 &kiblnd_data.kib_sched_conns);
3367 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3369 conn->ibc_scheduled = 0;
3373 cfs_spin_unlock_irqrestore(&kiblnd_data. \
3377 kiblnd_complete(&wc);
3379 cfs_spin_lock_irqsave(&kiblnd_data. \
3384 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3391 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3392 cfs_waitq_add_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3393 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3395 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
3398 cfs_waitq_del(&kiblnd_data.kib_sched_waitq, &wait);
3399 cfs_set_current_state(CFS_TASK_RUNNING);
3400 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3403 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3405 kiblnd_thread_fini();
3410 kiblnd_failover_thread(void *arg)
3412 cfs_rwlock_t *glock = &kiblnd_data.kib_global_lock;
3414 cfs_waitlink_t wait;
3415 unsigned long flags;
3418 LASSERT (*kiblnd_tunables.kib_dev_failover != 0);
3420 cfs_daemonize ("kiblnd_failover");
3421 cfs_block_allsigs ();
3423 cfs_waitlink_init(&wait);
3424 cfs_write_lock_irqsave(glock, flags);
3426 while (!kiblnd_data.kib_shutdown) {
3427 int do_failover = 0;
3430 cfs_list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3432 if (cfs_time_before(cfs_time_current(),
3433 dev->ibd_next_failover))
3440 cfs_list_del_init(&dev->ibd_fail_list);
3441 dev->ibd_failover = 1;
3442 cfs_write_unlock_irqrestore(glock, flags);
3444 rc = kiblnd_dev_failover(dev);
3446 cfs_write_lock_irqsave(glock, flags);
3448 LASSERT (dev->ibd_failover);
3449 dev->ibd_failover = 0;
3450 if (rc >= 0) { /* Device is OK or failover succeed */
3451 dev->ibd_next_failover = cfs_time_shift(3);
3455 /* failed to failover, retry later */
3456 dev->ibd_next_failover =
3457 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3458 if (kiblnd_dev_can_failover(dev)) {
3459 cfs_list_add_tail(&dev->ibd_fail_list,
3460 &kiblnd_data.kib_failed_devs);
3466 /* long sleep if no more pending failover */
3467 long_sleep = cfs_list_empty(&kiblnd_data.kib_failed_devs);
3469 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3470 cfs_waitq_add(&kiblnd_data.kib_failover_waitq, &wait);
3471 cfs_write_unlock_irqrestore(glock, flags);
3473 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3474 cfs_time_seconds(1));
3475 cfs_set_current_state(CFS_TASK_RUNNING);
3476 cfs_waitq_del(&kiblnd_data.kib_failover_waitq, &wait);
3477 cfs_write_lock_irqsave(glock, flags);
3479 if (!long_sleep || rc != 0)
3482 /* have a long sleep, routine check all active devices,
3483 * we need checking like this because if there is not active
3484 * connection on the dev and no SEND from local, we may listen
3485 * on wrong HCA for ever while there is a bonding failover */
3486 cfs_list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3487 if (kiblnd_dev_can_failover(dev)) {
3488 cfs_list_add_tail(&dev->ibd_fail_list,
3489 &kiblnd_data.kib_failed_devs);
3494 cfs_write_unlock_irqrestore(glock, flags);
3496 kiblnd_thread_fini();