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_need_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_need_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 = kiblnd_rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP,
1269 CERROR("Can't create CMID for %s: %ld\n",
1270 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1276 memset(&srcaddr, 0, sizeof(srcaddr));
1277 srcaddr.sin_family = AF_INET;
1278 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1280 memset(&dstaddr, 0, sizeof(dstaddr));
1281 dstaddr.sin_family = AF_INET;
1282 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1283 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1285 kiblnd_peer_addref(peer); /* cmid's ref */
1287 if (*kiblnd_tunables.kib_use_priv_port) {
1288 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1289 *kiblnd_tunables.kib_timeout * 1000);
1291 rc = rdma_resolve_addr(cmid,
1292 (struct sockaddr *)&srcaddr,
1293 (struct sockaddr *)&dstaddr,
1294 *kiblnd_tunables.kib_timeout * 1000);
1297 /* Can't initiate address resolution: */
1298 CERROR("Can't resolve addr for %s: %d\n",
1299 libcfs_nid2str(peer->ibp_nid), rc);
1303 LASSERT (cmid->device != NULL);
1304 CDEBUG(D_NET, "%s: connection bound to %s:%u.%u.%u.%u:%s\n",
1305 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1306 HIPQUAD(dev->ibd_ifip), cmid->device->name);
1311 kiblnd_peer_decref(peer); /* cmid's ref */
1312 rdma_destroy_id(cmid);
1314 kiblnd_peer_connect_failed(peer, 1, rc);
1318 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1323 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1324 unsigned long flags;
1327 /* If I get here, I've committed to send, so I complete the tx with
1328 * failure on any problems */
1330 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1331 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1333 /* First time, just use a read lock since I expect to find my peer
1335 cfs_read_lock_irqsave(g_lock, flags);
1337 peer = kiblnd_find_peer_locked(nid);
1338 if (peer != NULL && !cfs_list_empty(&peer->ibp_conns)) {
1339 /* Found a peer with an established connection */
1340 conn = kiblnd_get_conn_locked(peer);
1341 kiblnd_conn_addref(conn); /* 1 ref for me... */
1343 cfs_read_unlock_irqrestore(g_lock, flags);
1346 kiblnd_queue_tx(tx, conn);
1347 kiblnd_conn_decref(conn); /* ...to here */
1351 cfs_read_unlock(g_lock);
1352 /* Re-try with a write lock */
1353 cfs_write_lock(g_lock);
1355 peer = kiblnd_find_peer_locked(nid);
1357 if (cfs_list_empty(&peer->ibp_conns)) {
1358 /* found a peer, but it's still connecting... */
1359 LASSERT (peer->ibp_connecting != 0 ||
1360 peer->ibp_accepting != 0);
1362 cfs_list_add_tail(&tx->tx_list,
1363 &peer->ibp_tx_queue);
1364 cfs_write_unlock_irqrestore(g_lock, flags);
1366 conn = kiblnd_get_conn_locked(peer);
1367 kiblnd_conn_addref(conn); /* 1 ref for me... */
1369 cfs_write_unlock_irqrestore(g_lock, flags);
1372 kiblnd_queue_tx(tx, conn);
1373 kiblnd_conn_decref(conn); /* ...to here */
1378 cfs_write_unlock_irqrestore(g_lock, flags);
1380 /* Allocate a peer ready to add to the peer table and retry */
1381 rc = kiblnd_create_peer(ni, &peer, nid);
1383 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1385 tx->tx_status = -EHOSTUNREACH;
1387 kiblnd_tx_done(ni, tx);
1392 cfs_write_lock_irqsave(g_lock, flags);
1394 peer2 = kiblnd_find_peer_locked(nid);
1395 if (peer2 != NULL) {
1396 if (cfs_list_empty(&peer2->ibp_conns)) {
1397 /* found a peer, but it's still connecting... */
1398 LASSERT (peer2->ibp_connecting != 0 ||
1399 peer2->ibp_accepting != 0);
1401 cfs_list_add_tail(&tx->tx_list,
1402 &peer2->ibp_tx_queue);
1403 cfs_write_unlock_irqrestore(g_lock, flags);
1405 conn = kiblnd_get_conn_locked(peer2);
1406 kiblnd_conn_addref(conn); /* 1 ref for me... */
1408 cfs_write_unlock_irqrestore(g_lock, flags);
1411 kiblnd_queue_tx(tx, conn);
1412 kiblnd_conn_decref(conn); /* ...to here */
1415 kiblnd_peer_decref(peer);
1419 /* Brand new peer */
1420 LASSERT (peer->ibp_connecting == 0);
1421 peer->ibp_connecting = 1;
1423 /* always called with a ref on ni, which prevents ni being shutdown */
1424 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1427 cfs_list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1429 kiblnd_peer_addref(peer);
1430 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1432 cfs_write_unlock_irqrestore(g_lock, flags);
1434 kiblnd_connect_peer(peer);
1435 kiblnd_peer_decref(peer);
1439 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1441 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1442 int type = lntmsg->msg_type;
1443 lnet_process_id_t target = lntmsg->msg_target;
1444 int target_is_router = lntmsg->msg_target_is_router;
1445 int routing = lntmsg->msg_routing;
1446 unsigned int payload_niov = lntmsg->msg_niov;
1447 struct iovec *payload_iov = lntmsg->msg_iov;
1448 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1449 unsigned int payload_offset = lntmsg->msg_offset;
1450 unsigned int payload_nob = lntmsg->msg_len;
1456 /* NB 'private' is different depending on what we're sending.... */
1458 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1459 payload_nob, payload_niov, libcfs_id2str(target));
1461 LASSERT (payload_nob == 0 || payload_niov > 0);
1462 LASSERT (payload_niov <= LNET_MAX_IOV);
1464 /* Thread context */
1465 LASSERT (!cfs_in_interrupt());
1466 /* payload is either all vaddrs or all pages */
1467 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1475 LASSERT (payload_nob == 0);
1479 if (routing || target_is_router)
1480 break; /* send IMMEDIATE */
1482 /* is the REPLY message too small for RDMA? */
1483 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1484 if (nob <= IBLND_MSG_SIZE)
1485 break; /* send IMMEDIATE */
1487 tx = kiblnd_get_idle_tx(ni);
1489 CERROR("Can't allocate txd for GET to %s: \n",
1490 libcfs_nid2str(target.nid));
1496 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1497 rc = kiblnd_setup_rd_iov(ni, tx,
1498 &ibmsg->ibm_u.get.ibgm_rd,
1499 lntmsg->msg_md->md_niov,
1500 lntmsg->msg_md->md_iov.iov,
1501 0, lntmsg->msg_md->md_length);
1503 rc = kiblnd_setup_rd_kiov(ni, tx,
1504 &ibmsg->ibm_u.get.ibgm_rd,
1505 lntmsg->msg_md->md_niov,
1506 lntmsg->msg_md->md_iov.kiov,
1507 0, lntmsg->msg_md->md_length);
1509 CERROR("Can't setup GET sink for %s: %d\n",
1510 libcfs_nid2str(target.nid), rc);
1511 kiblnd_tx_done(ni, tx);
1515 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1516 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1517 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1519 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1521 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1522 if (tx->tx_lntmsg[1] == NULL) {
1523 CERROR("Can't create reply for GET -> %s\n",
1524 libcfs_nid2str(target.nid));
1525 kiblnd_tx_done(ni, tx);
1529 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1530 tx->tx_waiting = 1; /* waiting for GET_DONE */
1531 kiblnd_launch_tx(ni, tx, target.nid);
1534 case LNET_MSG_REPLY:
1536 /* Is the payload small enough not to need RDMA? */
1537 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1538 if (nob <= IBLND_MSG_SIZE)
1539 break; /* send IMMEDIATE */
1541 tx = kiblnd_get_idle_tx(ni);
1543 CERROR("Can't allocate %s txd for %s\n",
1544 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1545 libcfs_nid2str(target.nid));
1549 if (payload_kiov == NULL)
1550 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1551 payload_niov, payload_iov,
1552 payload_offset, payload_nob);
1554 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1555 payload_niov, payload_kiov,
1556 payload_offset, payload_nob);
1558 CERROR("Can't setup PUT src for %s: %d\n",
1559 libcfs_nid2str(target.nid), rc);
1560 kiblnd_tx_done(ni, tx);
1565 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1566 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1567 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1569 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1570 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1571 kiblnd_launch_tx(ni, tx, target.nid);
1575 /* send IMMEDIATE */
1577 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1580 tx = kiblnd_get_idle_tx(ni);
1582 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1583 type, libcfs_nid2str(target.nid));
1588 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1590 if (payload_kiov != NULL)
1591 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1592 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1593 payload_niov, payload_kiov,
1594 payload_offset, payload_nob);
1596 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1597 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1598 payload_niov, payload_iov,
1599 payload_offset, payload_nob);
1601 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1602 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1604 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1605 kiblnd_launch_tx(ni, tx, target.nid);
1610 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1612 lnet_process_id_t target = lntmsg->msg_target;
1613 unsigned int niov = lntmsg->msg_niov;
1614 struct iovec *iov = lntmsg->msg_iov;
1615 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1616 unsigned int offset = lntmsg->msg_offset;
1617 unsigned int nob = lntmsg->msg_len;
1621 tx = kiblnd_get_idle_tx(ni);
1623 CERROR("Can't get tx for REPLY to %s\n",
1624 libcfs_nid2str(target.nid));
1630 else if (kiov == NULL)
1631 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1632 niov, iov, offset, nob);
1634 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1635 niov, kiov, offset, nob);
1638 CERROR("Can't setup GET src for %s: %d\n",
1639 libcfs_nid2str(target.nid), rc);
1643 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1644 IBLND_MSG_GET_DONE, nob,
1645 &rx->rx_msg->ibm_u.get.ibgm_rd,
1646 rx->rx_msg->ibm_u.get.ibgm_cookie);
1648 CERROR("Can't setup rdma for GET from %s: %d\n",
1649 libcfs_nid2str(target.nid), rc);
1654 /* No RDMA: local completion may happen now! */
1655 lnet_finalize(ni, lntmsg, 0);
1657 /* RDMA: lnet_finalize(lntmsg) when it
1659 tx->tx_lntmsg[0] = lntmsg;
1662 kiblnd_queue_tx(tx, rx->rx_conn);
1666 kiblnd_tx_done(ni, tx);
1668 lnet_finalize(ni, lntmsg, -EIO);
1672 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1673 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1674 unsigned int offset, unsigned int mlen, unsigned int rlen)
1676 kib_rx_t *rx = private;
1677 kib_msg_t *rxmsg = rx->rx_msg;
1678 kib_conn_t *conn = rx->rx_conn;
1682 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1685 LASSERT (mlen <= rlen);
1686 LASSERT (!cfs_in_interrupt());
1687 /* Either all pages or all vaddrs */
1688 LASSERT (!(kiov != NULL && iov != NULL));
1690 switch (rxmsg->ibm_type) {
1694 case IBLND_MSG_IMMEDIATE:
1695 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1696 if (nob > rx->rx_nob) {
1697 CERROR ("Immediate message from %s too big: %d(%d)\n",
1698 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1705 lnet_copy_flat2kiov(niov, kiov, offset,
1706 IBLND_MSG_SIZE, rxmsg,
1707 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1710 lnet_copy_flat2iov(niov, iov, offset,
1711 IBLND_MSG_SIZE, rxmsg,
1712 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1714 lnet_finalize (ni, lntmsg, 0);
1717 case IBLND_MSG_PUT_REQ:
1719 lnet_finalize(ni, lntmsg, 0);
1720 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1721 rxmsg->ibm_u.putreq.ibprm_cookie);
1725 tx = kiblnd_get_idle_tx(ni);
1727 CERROR("Can't allocate tx for %s\n",
1728 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1729 /* Not replying will break the connection */
1736 rc = kiblnd_setup_rd_iov(ni, tx,
1737 &txmsg->ibm_u.putack.ibpam_rd,
1738 niov, iov, offset, mlen);
1740 rc = kiblnd_setup_rd_kiov(ni, tx,
1741 &txmsg->ibm_u.putack.ibpam_rd,
1742 niov, kiov, offset, mlen);
1744 CERROR("Can't setup PUT sink for %s: %d\n",
1745 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1746 kiblnd_tx_done(ni, tx);
1747 /* tell peer it's over */
1748 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1749 rxmsg->ibm_u.putreq.ibprm_cookie);
1753 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1754 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1755 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1757 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1759 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1760 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1761 kiblnd_queue_tx(tx, conn);
1763 /* reposted buffer reserved for PUT_DONE */
1764 post_credit = IBLND_POSTRX_NO_CREDIT;
1767 case IBLND_MSG_GET_REQ:
1768 if (lntmsg != NULL) {
1769 /* Optimized GET; RDMA lntmsg's payload */
1770 kiblnd_reply(ni, rx, lntmsg);
1772 /* GET didn't match anything */
1773 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1775 rxmsg->ibm_u.get.ibgm_cookie);
1780 kiblnd_post_rx(rx, post_credit);
1785 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1787 long pid = cfs_create_thread (fn, arg, 0);
1792 cfs_atomic_inc (&kiblnd_data.kib_nthreads);
1797 kiblnd_thread_fini (void)
1799 cfs_atomic_dec (&kiblnd_data.kib_nthreads);
1803 kiblnd_peer_alive (kib_peer_t *peer)
1805 /* This is racy, but everyone's only writing cfs_time_current() */
1806 peer->ibp_last_alive = cfs_time_current();
1811 kiblnd_peer_notify (kib_peer_t *peer)
1814 cfs_time_t last_alive = 0;
1815 unsigned long flags;
1817 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1819 if (cfs_list_empty(&peer->ibp_conns) &&
1820 peer->ibp_accepting == 0 &&
1821 peer->ibp_connecting == 0 &&
1822 peer->ibp_error != 0) {
1823 error = peer->ibp_error;
1824 peer->ibp_error = 0;
1826 last_alive = peer->ibp_last_alive;
1829 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1832 lnet_notify(peer->ibp_ni,
1833 peer->ibp_nid, 0, last_alive);
1837 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1839 /* This just does the immediate housekeeping. 'error' is zero for a
1840 * normal shutdown which can happen only after the connection has been
1841 * established. If the connection is established, schedule the
1842 * connection to be finished off by the connd. Otherwise the connd is
1843 * already dealing with it (either to set it up or tear it down).
1844 * Caller holds kib_global_lock exclusively in irq context */
1845 kib_peer_t *peer = conn->ibc_peer;
1847 unsigned long flags;
1849 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1851 if (error != 0 && conn->ibc_comms_error == 0)
1852 conn->ibc_comms_error = error;
1854 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1855 return; /* already being handled */
1858 cfs_list_empty(&conn->ibc_tx_noops) &&
1859 cfs_list_empty(&conn->ibc_tx_queue) &&
1860 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) &&
1861 cfs_list_empty(&conn->ibc_tx_queue_nocred) &&
1862 cfs_list_empty(&conn->ibc_active_txs)) {
1863 CDEBUG(D_NET, "closing conn to %s\n",
1864 libcfs_nid2str(peer->ibp_nid));
1866 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1867 libcfs_nid2str(peer->ibp_nid), error,
1868 cfs_list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1869 cfs_list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1870 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1871 cfs_list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1872 cfs_list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1875 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1876 cfs_list_del(&conn->ibc_list);
1877 /* connd (see below) takes over ibc_list's ref */
1879 if (cfs_list_empty (&peer->ibp_conns) && /* no more conns */
1880 kiblnd_peer_active(peer)) { /* still in peer table */
1881 kiblnd_unlink_peer_locked(peer);
1883 /* set/clear error on last conn */
1884 peer->ibp_error = conn->ibc_comms_error;
1887 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1890 kiblnd_dev_can_failover(dev)) {
1891 cfs_list_add_tail(&dev->ibd_fail_list,
1892 &kiblnd_data.kib_failed_devs);
1893 cfs_waitq_signal(&kiblnd_data.kib_failover_waitq);
1896 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1898 cfs_list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1899 cfs_waitq_signal (&kiblnd_data.kib_connd_waitq);
1901 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1905 kiblnd_close_conn (kib_conn_t *conn, int error)
1907 unsigned long flags;
1909 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1911 kiblnd_close_conn_locked(conn, error);
1913 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1917 kiblnd_handle_early_rxs(kib_conn_t *conn)
1919 unsigned long flags;
1922 LASSERT (!cfs_in_interrupt());
1923 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1925 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1926 while (!cfs_list_empty(&conn->ibc_early_rxs)) {
1927 rx = cfs_list_entry(conn->ibc_early_rxs.next,
1929 cfs_list_del(&rx->rx_list);
1930 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
1933 kiblnd_handle_rx(rx);
1935 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1937 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1941 kiblnd_abort_txs(kib_conn_t *conn, cfs_list_t *txs)
1943 CFS_LIST_HEAD (zombies);
1948 cfs_spin_lock(&conn->ibc_lock);
1950 cfs_list_for_each_safe (tmp, nxt, txs) {
1951 tx = cfs_list_entry (tmp, kib_tx_t, tx_list);
1953 if (txs == &conn->ibc_active_txs) {
1954 LASSERT (!tx->tx_queued);
1955 LASSERT (tx->tx_waiting ||
1956 tx->tx_sending != 0);
1958 LASSERT (tx->tx_queued);
1961 tx->tx_status = -ECONNABORTED;
1964 if (tx->tx_sending == 0) {
1966 cfs_list_del (&tx->tx_list);
1967 cfs_list_add (&tx->tx_list, &zombies);
1971 cfs_spin_unlock(&conn->ibc_lock);
1973 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
1974 &zombies, -ECONNABORTED);
1978 kiblnd_finalise_conn (kib_conn_t *conn)
1980 LASSERT (!cfs_in_interrupt());
1981 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
1983 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
1985 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
1986 * for connections that didn't get as far as being connected, because
1987 * rdma_disconnect() does this for free. */
1988 kiblnd_abort_receives(conn);
1990 /* Complete all tx descs not waiting for sends to complete.
1991 * NB we should be safe from RDMA now that the QP has changed state */
1993 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
1994 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
1995 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
1996 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
1997 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
1999 kiblnd_handle_early_rxs(conn);
2003 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
2005 CFS_LIST_HEAD (zombies);
2006 unsigned long flags;
2008 LASSERT (error != 0);
2009 LASSERT (!cfs_in_interrupt());
2011 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2014 LASSERT (peer->ibp_connecting > 0);
2015 peer->ibp_connecting--;
2017 LASSERT (peer->ibp_accepting > 0);
2018 peer->ibp_accepting--;
2021 if (peer->ibp_connecting != 0 ||
2022 peer->ibp_accepting != 0) {
2023 /* another connection attempt under way... */
2024 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2029 if (cfs_list_empty(&peer->ibp_conns)) {
2030 /* Take peer's blocked transmits to complete with error */
2031 cfs_list_add(&zombies, &peer->ibp_tx_queue);
2032 cfs_list_del_init(&peer->ibp_tx_queue);
2034 if (kiblnd_peer_active(peer))
2035 kiblnd_unlink_peer_locked(peer);
2037 peer->ibp_error = error;
2039 /* Can't have blocked transmits if there are connections */
2040 LASSERT (cfs_list_empty(&peer->ibp_tx_queue));
2043 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2045 kiblnd_peer_notify(peer);
2047 if (cfs_list_empty (&zombies))
2050 CNETERR("Deleting messages for %s: connection failed\n",
2051 libcfs_nid2str(peer->ibp_nid));
2053 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2057 kiblnd_connreq_done(kib_conn_t *conn, int status)
2059 kib_peer_t *peer = conn->ibc_peer;
2062 unsigned long flags;
2065 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2067 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2068 libcfs_nid2str(peer->ibp_nid), active,
2069 conn->ibc_version, status);
2071 LASSERT (!cfs_in_interrupt());
2072 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2073 peer->ibp_connecting > 0) ||
2074 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2075 peer->ibp_accepting > 0));
2077 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2078 conn->ibc_connvars = NULL;
2081 /* failed to establish connection */
2082 kiblnd_peer_connect_failed(peer, active, status);
2083 kiblnd_finalise_conn(conn);
2087 /* connection established */
2088 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2090 conn->ibc_last_send = jiffies;
2091 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2092 kiblnd_peer_alive(peer);
2094 /* Add conn to peer's list and nuke any dangling conns from a different
2095 * peer instance... */
2096 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2097 cfs_list_add(&conn->ibc_list, &peer->ibp_conns);
2099 peer->ibp_connecting--;
2101 peer->ibp_accepting--;
2103 if (peer->ibp_version == 0) {
2104 peer->ibp_version = conn->ibc_version;
2105 peer->ibp_incarnation = conn->ibc_incarnation;
2108 if (peer->ibp_version != conn->ibc_version ||
2109 peer->ibp_incarnation != conn->ibc_incarnation) {
2110 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2111 conn->ibc_incarnation);
2112 peer->ibp_version = conn->ibc_version;
2113 peer->ibp_incarnation = conn->ibc_incarnation;
2116 /* grab pending txs while I have the lock */
2117 cfs_list_add(&txs, &peer->ibp_tx_queue);
2118 cfs_list_del_init(&peer->ibp_tx_queue);
2120 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2121 conn->ibc_comms_error != 0) { /* error has happened already */
2122 lnet_ni_t *ni = peer->ibp_ni;
2124 /* start to shut down connection */
2125 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2126 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2129 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2134 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2136 /* Schedule blocked txs */
2137 cfs_spin_lock (&conn->ibc_lock);
2138 while (!cfs_list_empty (&txs)) {
2139 tx = cfs_list_entry (txs.next, kib_tx_t, tx_list);
2140 cfs_list_del(&tx->tx_list);
2142 kiblnd_queue_tx_locked(tx, conn);
2144 cfs_spin_unlock (&conn->ibc_lock);
2146 kiblnd_check_sends(conn);
2148 /* schedule blocked rxs */
2149 kiblnd_handle_early_rxs(conn);
2153 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2157 rc = rdma_reject(cmid, rej, sizeof(*rej));
2160 CWARN("Error %d sending reject\n", rc);
2164 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2166 cfs_rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2167 kib_msg_t *reqmsg = priv;
2173 lnet_ni_t *ni = NULL;
2174 kib_net_t *net = NULL;
2176 struct rdma_conn_param cp;
2178 int version = IBLND_MSG_VERSION;
2179 unsigned long flags;
2181 struct sockaddr_in *peer_addr;
2182 LASSERT (!cfs_in_interrupt());
2184 /* cmid inherits 'context' from the corresponding listener id */
2185 ibdev = (kib_dev_t *)cmid->context;
2186 LASSERT (ibdev != NULL);
2188 memset(&rej, 0, sizeof(rej));
2189 rej.ibr_magic = IBLND_MSG_MAGIC;
2190 rej.ibr_why = IBLND_REJECT_FATAL;
2191 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2193 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2194 if (*kiblnd_tunables.kib_require_priv_port &&
2195 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2196 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2197 CERROR("Peer's port (%u.%u.%u.%u:%hu) is not privileged\n",
2198 HIPQUAD(ip), ntohs(peer_addr->sin_port));
2202 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2203 CERROR("Short connection request\n");
2207 /* Future protocol version compatibility support! If the
2208 * o2iblnd-specific protocol changes, or when LNET unifies
2209 * protocols over all LNDs, the initial connection will
2210 * negotiate a protocol version. I trap this here to avoid
2211 * console errors; the reject tells the peer which protocol I
2213 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2214 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2216 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2217 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2218 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2220 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2221 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2222 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2225 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2227 CERROR("Can't parse connection request: %d\n", rc);
2231 nid = reqmsg->ibm_srcnid;
2232 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2235 net = (kib_net_t *)ni->ni_data;
2236 rej.ibr_incarnation = net->ibn_incarnation;
2239 if (ni == NULL || /* no matching net */
2240 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2241 net->ibn_dev != ibdev) { /* wrong device */
2242 CERROR("Can't accept %s on %s (%s:%d:%u.%u.%u.%u): "
2243 "bad dst nid %s\n", libcfs_nid2str(nid),
2244 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2245 ibdev->ibd_ifname, ibdev->ibd_nnets,
2246 HIPQUAD(ibdev->ibd_ifip),
2247 libcfs_nid2str(reqmsg->ibm_dstnid));
2252 /* check time stamp as soon as possible */
2253 if (reqmsg->ibm_dststamp != 0 &&
2254 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2255 CWARN("Stale connection request\n");
2256 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2260 /* I can accept peer's version */
2261 version = reqmsg->ibm_version;
2263 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2264 CERROR("Unexpected connreq msg type: %x from %s\n",
2265 reqmsg->ibm_type, libcfs_nid2str(nid));
2269 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2270 IBLND_MSG_QUEUE_SIZE(version)) {
2271 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2272 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2273 IBLND_MSG_QUEUE_SIZE(version));
2275 if (version == IBLND_MSG_VERSION)
2276 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2281 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2282 IBLND_RDMA_FRAGS(version)) {
2283 CERROR("Can't accept %s(version %x): "
2284 "incompatible max_frags %d (%d wanted)\n",
2285 libcfs_nid2str(nid), version,
2286 reqmsg->ibm_u.connparams.ibcp_max_frags,
2287 IBLND_RDMA_FRAGS(version));
2289 if (version == IBLND_MSG_VERSION)
2290 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2296 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2297 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2298 libcfs_nid2str(nid),
2299 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2304 /* assume 'nid' is a new peer; create */
2305 rc = kiblnd_create_peer(ni, &peer, nid);
2307 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2308 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2312 cfs_write_lock_irqsave(g_lock, flags);
2314 peer2 = kiblnd_find_peer_locked(nid);
2315 if (peer2 != NULL) {
2316 if (peer2->ibp_version == 0) {
2317 peer2->ibp_version = version;
2318 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2321 /* not the guy I've talked with */
2322 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2323 peer2->ibp_version != version) {
2324 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2325 cfs_write_unlock_irqrestore(g_lock, flags);
2327 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2328 libcfs_nid2str(nid), peer2->ibp_version, version);
2330 kiblnd_peer_decref(peer);
2331 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2335 /* tie-break connection race in favour of the higher NID */
2336 if (peer2->ibp_connecting != 0 &&
2338 cfs_write_unlock_irqrestore(g_lock, flags);
2340 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2342 kiblnd_peer_decref(peer);
2343 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2347 peer2->ibp_accepting++;
2348 kiblnd_peer_addref(peer2);
2350 cfs_write_unlock_irqrestore(g_lock, flags);
2351 kiblnd_peer_decref(peer);
2354 /* Brand new peer */
2355 LASSERT (peer->ibp_accepting == 0);
2356 LASSERT (peer->ibp_version == 0 &&
2357 peer->ibp_incarnation == 0);
2359 peer->ibp_accepting = 1;
2360 peer->ibp_version = version;
2361 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2363 /* I have a ref on ni that prevents it being shutdown */
2364 LASSERT (net->ibn_shutdown == 0);
2366 kiblnd_peer_addref(peer);
2367 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2369 cfs_write_unlock_irqrestore(g_lock, flags);
2372 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2374 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2375 kiblnd_peer_decref(peer);
2376 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2380 /* conn now "owns" cmid, so I return success from here on to ensure the
2381 * CM callback doesn't destroy cmid. */
2383 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2384 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2385 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2386 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2387 <= IBLND_RX_MSGS(version));
2389 ackmsg = &conn->ibc_connvars->cv_msg;
2390 memset(ackmsg, 0, sizeof(*ackmsg));
2392 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2393 sizeof(ackmsg->ibm_u.connparams));
2394 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2395 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2396 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2398 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2400 memset(&cp, 0, sizeof(cp));
2401 cp.private_data = ackmsg;
2402 cp.private_data_len = ackmsg->ibm_nob;
2403 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2404 cp.initiator_depth = 0;
2405 cp.flow_control = 1;
2406 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2407 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2409 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2411 rc = rdma_accept(cmid, &cp);
2413 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2414 rej.ibr_version = version;
2415 rej.ibr_why = IBLND_REJECT_FATAL;
2417 kiblnd_reject(cmid, &rej);
2418 kiblnd_connreq_done(conn, rc);
2419 kiblnd_conn_decref(conn);
2429 rej.ibr_version = version;
2430 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2431 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2432 kiblnd_reject(cmid, &rej);
2434 return -ECONNREFUSED;
2438 kiblnd_reconnect (kib_conn_t *conn, int version,
2439 __u64 incarnation, int why, kib_connparams_t *cp)
2441 kib_peer_t *peer = conn->ibc_peer;
2444 unsigned long flags;
2446 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2447 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2449 cfs_write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2451 /* retry connection if it's still needed and no other connection
2452 * attempts (active or passive) are in progress
2453 * NB: reconnect is still needed even when ibp_tx_queue is
2454 * empty if ibp_version != version because reconnect may be
2455 * initiated by kiblnd_query() */
2456 if ((!cfs_list_empty(&peer->ibp_tx_queue) ||
2457 peer->ibp_version != version) &&
2458 peer->ibp_connecting == 1 &&
2459 peer->ibp_accepting == 0) {
2461 peer->ibp_connecting++;
2463 peer->ibp_version = version;
2464 peer->ibp_incarnation = incarnation;
2467 cfs_write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2477 case IBLND_REJECT_CONN_STALE:
2481 case IBLND_REJECT_CONN_RACE:
2482 reason = "conn race";
2485 case IBLND_REJECT_CONN_UNCOMPAT:
2486 reason = "version negotiation";
2490 CNETERR("%s: retrying (%s), %x, %x, "
2491 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2492 libcfs_nid2str(peer->ibp_nid),
2493 reason, IBLND_MSG_VERSION, version,
2494 cp != NULL? cp->ibcp_queue_depth :IBLND_MSG_QUEUE_SIZE(version),
2495 cp != NULL? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2496 cp != NULL? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2498 kiblnd_connect_peer(peer);
2502 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2504 kib_peer_t *peer = conn->ibc_peer;
2506 LASSERT (!cfs_in_interrupt());
2507 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2510 case IB_CM_REJ_STALE_CONN:
2511 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2512 IBLND_REJECT_CONN_STALE, NULL);
2515 case IB_CM_REJ_INVALID_SERVICE_ID:
2516 CNETERR("%s rejected: no listener at %d\n",
2517 libcfs_nid2str(peer->ibp_nid),
2518 *kiblnd_tunables.kib_service);
2521 case IB_CM_REJ_CONSUMER_DEFINED:
2522 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2523 kib_rej_t *rej = priv;
2524 kib_connparams_t *cp = NULL;
2526 __u64 incarnation = -1;
2528 /* NB. default incarnation is -1 because:
2529 * a) V1 will ignore dst incarnation in connreq.
2530 * b) V2 will provide incarnation while rejecting me,
2531 * -1 will be overwrote.
2533 * if I try to connect to a V1 peer with V2 protocol,
2534 * it rejected me then upgrade to V2, I have no idea
2535 * about the upgrading and try to reconnect with V1,
2536 * in this case upgraded V2 can find out I'm trying to
2537 * talk to the old guy and reject me(incarnation is -1).
2540 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2541 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2542 __swab32s(&rej->ibr_magic);
2543 __swab16s(&rej->ibr_version);
2547 if (priv_nob >= sizeof(kib_rej_t) &&
2548 rej->ibr_version > IBLND_MSG_VERSION_1) {
2549 /* priv_nob is always 148 in current version
2550 * of OFED, so we still need to check version.
2551 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2555 __swab64s(&rej->ibr_incarnation);
2556 __swab16s(&cp->ibcp_queue_depth);
2557 __swab16s(&cp->ibcp_max_frags);
2558 __swab32s(&cp->ibcp_max_msg_size);
2561 incarnation = rej->ibr_incarnation;
2564 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2565 rej->ibr_magic != LNET_PROTO_MAGIC) {
2566 CERROR("%s rejected: consumer defined fatal error\n",
2567 libcfs_nid2str(peer->ibp_nid));
2571 if (rej->ibr_version != IBLND_MSG_VERSION &&
2572 rej->ibr_version != IBLND_MSG_VERSION_1) {
2573 CERROR("%s rejected: o2iblnd version %x error\n",
2574 libcfs_nid2str(peer->ibp_nid),
2579 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2580 rej->ibr_version == IBLND_MSG_VERSION_1) {
2581 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2582 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2584 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2585 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2588 switch (rej->ibr_why) {
2589 case IBLND_REJECT_CONN_RACE:
2590 case IBLND_REJECT_CONN_STALE:
2591 case IBLND_REJECT_CONN_UNCOMPAT:
2592 kiblnd_reconnect(conn, rej->ibr_version,
2593 incarnation, rej->ibr_why, cp);
2596 case IBLND_REJECT_MSG_QUEUE_SIZE:
2597 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2598 libcfs_nid2str(peer->ibp_nid), cp->ibcp_queue_depth,
2599 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2602 case IBLND_REJECT_RDMA_FRAGS:
2603 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2604 libcfs_nid2str(peer->ibp_nid), cp->ibcp_max_frags,
2605 IBLND_RDMA_FRAGS(conn->ibc_version));
2608 case IBLND_REJECT_NO_RESOURCES:
2609 CERROR("%s rejected: o2iblnd no resources\n",
2610 libcfs_nid2str(peer->ibp_nid));
2613 case IBLND_REJECT_FATAL:
2614 CERROR("%s rejected: o2iblnd fatal error\n",
2615 libcfs_nid2str(peer->ibp_nid));
2619 CERROR("%s rejected: o2iblnd reason %d\n",
2620 libcfs_nid2str(peer->ibp_nid),
2628 CNETERR("%s rejected: reason %d, size %d\n",
2629 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2633 kiblnd_connreq_done(conn, -ECONNREFUSED);
2637 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2639 kib_peer_t *peer = conn->ibc_peer;
2640 lnet_ni_t *ni = peer->ibp_ni;
2641 kib_net_t *net = ni->ni_data;
2642 kib_msg_t *msg = priv;
2643 int ver = conn->ibc_version;
2644 int rc = kiblnd_unpack_msg(msg, priv_nob);
2645 unsigned long flags;
2647 LASSERT (net != NULL);
2650 CERROR("Can't unpack connack from %s: %d\n",
2651 libcfs_nid2str(peer->ibp_nid), rc);
2655 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2656 CERROR("Unexpected message %d from %s\n",
2657 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2662 if (ver != msg->ibm_version) {
2663 CERROR("%s replied version %x is different with "
2664 "requested version %x\n",
2665 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2670 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2671 IBLND_MSG_QUEUE_SIZE(ver)) {
2672 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2673 libcfs_nid2str(peer->ibp_nid),
2674 msg->ibm_u.connparams.ibcp_queue_depth,
2675 IBLND_MSG_QUEUE_SIZE(ver));
2680 if (msg->ibm_u.connparams.ibcp_max_frags !=
2681 IBLND_RDMA_FRAGS(ver)) {
2682 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2683 libcfs_nid2str(peer->ibp_nid),
2684 msg->ibm_u.connparams.ibcp_max_frags,
2685 IBLND_RDMA_FRAGS(ver));
2690 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2691 CERROR("%s max message size %d too big (%d max)\n",
2692 libcfs_nid2str(peer->ibp_nid),
2693 msg->ibm_u.connparams.ibcp_max_msg_size,
2699 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2700 if (msg->ibm_dstnid == ni->ni_nid &&
2701 msg->ibm_dststamp == net->ibn_incarnation)
2705 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2708 CERROR("Bad connection reply from %s, rc = %d, "
2709 "version: %x max_frags: %d\n",
2710 libcfs_nid2str(peer->ibp_nid), rc,
2711 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2715 conn->ibc_incarnation = msg->ibm_srcstamp;
2717 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2718 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2719 <= IBLND_RX_MSGS(ver));
2721 kiblnd_connreq_done(conn, 0);
2725 /* NB My QP has already established itself, so I handle anything going
2726 * wrong here by setting ibc_comms_error.
2727 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2728 * immediately tears it down. */
2731 conn->ibc_comms_error = rc;
2732 kiblnd_connreq_done(conn, 0);
2736 kiblnd_active_connect (struct rdma_cm_id *cmid)
2738 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2741 struct rdma_conn_param cp;
2744 unsigned long flags;
2747 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2749 incarnation = peer->ibp_incarnation;
2750 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION : peer->ibp_version;
2752 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2754 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2756 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2757 kiblnd_peer_decref(peer); /* lose cmid's ref */
2761 /* conn "owns" cmid now, so I return success from here on to ensure the
2762 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2765 msg = &conn->ibc_connvars->cv_msg;
2767 memset(msg, 0, sizeof(*msg));
2768 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2769 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2770 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2771 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2773 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2774 0, peer->ibp_nid, incarnation);
2776 memset(&cp, 0, sizeof(cp));
2777 cp.private_data = msg;
2778 cp.private_data_len = msg->ibm_nob;
2779 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2780 cp.initiator_depth = 0;
2781 cp.flow_control = 1;
2782 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2783 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2785 LASSERT(cmid->context == (void *)conn);
2786 LASSERT(conn->ibc_cmid == cmid);
2788 rc = rdma_connect(cmid, &cp);
2790 CERROR("Can't connect to %s: %d\n",
2791 libcfs_nid2str(peer->ibp_nid), rc);
2792 kiblnd_connreq_done(conn, rc);
2793 kiblnd_conn_decref(conn);
2800 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2806 switch (event->event) {
2808 CERROR("Unexpected event: %d, status: %d\n",
2809 event->event, event->status);
2812 case RDMA_CM_EVENT_CONNECT_REQUEST:
2813 /* destroy cmid on failure */
2814 rc = kiblnd_passive_connect(cmid,
2815 (void *)KIBLND_CONN_PARAM(event),
2816 KIBLND_CONN_PARAM_LEN(event));
2817 CDEBUG(D_NET, "connreq: %d\n", rc);
2820 case RDMA_CM_EVENT_ADDR_ERROR:
2821 peer = (kib_peer_t *)cmid->context;
2822 CNETERR("%s: ADDR ERROR %d\n",
2823 libcfs_nid2str(peer->ibp_nid), event->status);
2824 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2825 kiblnd_peer_decref(peer);
2826 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2828 case RDMA_CM_EVENT_ADDR_RESOLVED:
2829 peer = (kib_peer_t *)cmid->context;
2831 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2832 libcfs_nid2str(peer->ibp_nid), event->status);
2834 if (event->status != 0) {
2835 CNETERR("Can't resolve address for %s: %d\n",
2836 libcfs_nid2str(peer->ibp_nid), event->status);
2839 rc = rdma_resolve_route(
2840 cmid, *kiblnd_tunables.kib_timeout * 1000);
2843 /* Can't initiate route resolution */
2844 CERROR("Can't resolve route for %s: %d\n",
2845 libcfs_nid2str(peer->ibp_nid), rc);
2847 kiblnd_peer_connect_failed(peer, 1, rc);
2848 kiblnd_peer_decref(peer);
2849 return rc; /* rc != 0 destroys cmid */
2851 case RDMA_CM_EVENT_ROUTE_ERROR:
2852 peer = (kib_peer_t *)cmid->context;
2853 CNETERR("%s: ROUTE ERROR %d\n",
2854 libcfs_nid2str(peer->ibp_nid), event->status);
2855 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2856 kiblnd_peer_decref(peer);
2857 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2859 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2860 peer = (kib_peer_t *)cmid->context;
2861 CDEBUG(D_NET,"%s Route resolved: %d\n",
2862 libcfs_nid2str(peer->ibp_nid), event->status);
2864 if (event->status == 0)
2865 return kiblnd_active_connect(cmid);
2867 CNETERR("Can't resolve route for %s: %d\n",
2868 libcfs_nid2str(peer->ibp_nid), event->status);
2869 kiblnd_peer_connect_failed(peer, 1, event->status);
2870 kiblnd_peer_decref(peer);
2871 return event->status; /* rc != 0 destroys cmid */
2873 case RDMA_CM_EVENT_UNREACHABLE:
2874 conn = (kib_conn_t *)cmid->context;
2875 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2876 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2877 CNETERR("%s: UNREACHABLE %d\n",
2878 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2879 kiblnd_connreq_done(conn, -ENETDOWN);
2880 kiblnd_conn_decref(conn);
2883 case RDMA_CM_EVENT_CONNECT_ERROR:
2884 conn = (kib_conn_t *)cmid->context;
2885 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2886 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2887 CNETERR("%s: CONNECT ERROR %d\n",
2888 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2889 kiblnd_connreq_done(conn, -ENOTCONN);
2890 kiblnd_conn_decref(conn);
2893 case RDMA_CM_EVENT_REJECTED:
2894 conn = (kib_conn_t *)cmid->context;
2895 switch (conn->ibc_state) {
2899 case IBLND_CONN_PASSIVE_WAIT:
2900 CERROR ("%s: REJECTED %d\n",
2901 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2903 kiblnd_connreq_done(conn, -ECONNRESET);
2906 case IBLND_CONN_ACTIVE_CONNECT:
2907 kiblnd_rejected(conn, event->status,
2908 (void *)KIBLND_CONN_PARAM(event),
2909 KIBLND_CONN_PARAM_LEN(event));
2912 kiblnd_conn_decref(conn);
2915 case RDMA_CM_EVENT_ESTABLISHED:
2916 conn = (kib_conn_t *)cmid->context;
2917 switch (conn->ibc_state) {
2921 case IBLND_CONN_PASSIVE_WAIT:
2922 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2923 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2924 kiblnd_connreq_done(conn, 0);
2927 case IBLND_CONN_ACTIVE_CONNECT:
2928 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2929 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2930 kiblnd_check_connreply(conn,
2931 (void *)KIBLND_CONN_PARAM(event),
2932 KIBLND_CONN_PARAM_LEN(event));
2935 /* net keeps its ref on conn! */
2938 #ifdef HAVE_OFED_RDMA_CMEV_TIMEWAIT_EXIT
2939 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2940 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2943 case RDMA_CM_EVENT_DISCONNECTED:
2944 conn = (kib_conn_t *)cmid->context;
2945 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2946 CERROR("%s DISCONNECTED\n",
2947 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2948 kiblnd_connreq_done(conn, -ECONNRESET);
2950 kiblnd_close_conn(conn, 0);
2952 kiblnd_conn_decref(conn);
2953 cmid->context = NULL;
2956 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2957 LCONSOLE_ERROR_MSG(0x131,
2958 "Received notification of device removal\n"
2959 "Please shutdown LNET to allow this to proceed\n");
2960 /* Can't remove network from underneath LNET for now, so I have
2964 #ifdef HAVE_OFED_RDMA_CMEV_ADDRCHANGE
2965 case RDMA_CM_EVENT_ADDR_CHANGE:
2966 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2973 kiblnd_check_txs_locked(kib_conn_t *conn, cfs_list_t *txs)
2978 cfs_list_for_each (ttmp, txs) {
2979 tx = cfs_list_entry (ttmp, kib_tx_t, tx_list);
2981 if (txs != &conn->ibc_active_txs) {
2982 LASSERT (tx->tx_queued);
2984 LASSERT (!tx->tx_queued);
2985 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2988 if (cfs_time_aftereq (jiffies, tx->tx_deadline)) {
2989 CERROR("Timed out tx: %s, %lu seconds\n",
2990 kiblnd_queue2str(conn, txs),
2991 cfs_duration_sec(jiffies - tx->tx_deadline));
3000 kiblnd_conn_timed_out_locked(kib_conn_t *conn)
3002 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3003 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3004 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3005 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3006 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3010 kiblnd_check_conns (int idx)
3012 CFS_LIST_HEAD (closes);
3013 CFS_LIST_HEAD (checksends);
3014 cfs_list_t *peers = &kiblnd_data.kib_peers[idx];
3019 unsigned long flags;
3021 /* NB. We expect to have a look at all the peers and not find any
3022 * RDMAs to time out, so we just use a shared lock while we
3024 cfs_read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3026 cfs_list_for_each (ptmp, peers) {
3027 peer = cfs_list_entry (ptmp, kib_peer_t, ibp_list);
3029 cfs_list_for_each (ctmp, &peer->ibp_conns) {
3033 conn = cfs_list_entry(ctmp, kib_conn_t, ibc_list);
3035 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
3037 cfs_spin_lock(&conn->ibc_lock);
3039 sendnoop = kiblnd_need_noop(conn);
3040 timedout = kiblnd_conn_timed_out_locked(conn);
3041 if (!sendnoop && !timedout) {
3042 cfs_spin_unlock(&conn->ibc_lock);
3047 CERROR("Timed out RDMA with %s (%lu): "
3048 "c: %u, oc: %u, rc: %u\n",
3049 libcfs_nid2str(peer->ibp_nid),
3050 cfs_duration_sec(cfs_time_current() -
3051 peer->ibp_last_alive),
3053 conn->ibc_outstanding_credits,
3054 conn->ibc_reserved_credits);
3055 cfs_list_add(&conn->ibc_connd_list, &closes);
3057 cfs_list_add(&conn->ibc_connd_list,
3060 /* +ref for 'closes' or 'checksends' */
3061 kiblnd_conn_addref(conn);
3063 cfs_spin_unlock(&conn->ibc_lock);
3067 cfs_read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3069 /* Handle timeout by closing the whole
3070 * connection. We can only be sure RDMA activity
3071 * has ceased once the QP has been modified. */
3072 while (!cfs_list_empty(&closes)) {
3073 conn = cfs_list_entry(closes.next,
3074 kib_conn_t, ibc_connd_list);
3075 cfs_list_del(&conn->ibc_connd_list);
3076 kiblnd_close_conn(conn, -ETIMEDOUT);
3077 kiblnd_conn_decref(conn);
3080 /* In case we have enough credits to return via a
3081 * NOOP, but there were no non-blocking tx descs
3082 * free to do it last time... */
3083 while (!cfs_list_empty(&checksends)) {
3084 conn = cfs_list_entry(checksends.next,
3085 kib_conn_t, ibc_connd_list);
3086 cfs_list_del(&conn->ibc_connd_list);
3087 kiblnd_check_sends(conn);
3088 kiblnd_conn_decref(conn);
3093 kiblnd_disconnect_conn (kib_conn_t *conn)
3095 LASSERT (!cfs_in_interrupt());
3096 LASSERT (current == kiblnd_data.kib_connd);
3097 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3099 rdma_disconnect(conn->ibc_cmid);
3100 kiblnd_finalise_conn(conn);
3102 kiblnd_peer_notify(conn->ibc_peer);
3106 kiblnd_connd (void *arg)
3108 cfs_waitlink_t wait;
3109 unsigned long flags;
3115 unsigned long deadline = jiffies;
3117 cfs_daemonize ("kiblnd_connd");
3118 cfs_block_allsigs ();
3120 cfs_waitlink_init (&wait);
3121 kiblnd_data.kib_connd = current;
3123 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3125 while (!kiblnd_data.kib_shutdown) {
3129 if (!cfs_list_empty (&kiblnd_data.kib_connd_zombies)) {
3130 conn = cfs_list_entry(kiblnd_data. \
3131 kib_connd_zombies.next,
3132 kib_conn_t, ibc_list);
3133 cfs_list_del(&conn->ibc_list);
3135 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3139 kiblnd_destroy_conn(conn);
3141 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3145 if (!cfs_list_empty (&kiblnd_data.kib_connd_conns)) {
3146 conn = cfs_list_entry (kiblnd_data.kib_connd_conns.next,
3147 kib_conn_t, ibc_list);
3148 cfs_list_del(&conn->ibc_list);
3150 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock,
3154 kiblnd_disconnect_conn(conn);
3155 kiblnd_conn_decref(conn);
3157 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock,
3161 /* careful with the jiffy wrap... */
3162 timeout = (int)(deadline - jiffies);
3166 int chunk = kiblnd_data.kib_peer_hash_size;
3168 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3171 /* Time to check for RDMA timeouts on a few more
3172 * peers: I do checks every 'p' seconds on a
3173 * proportion of the peer table and I need to check
3174 * every connection 'n' times within a timeout
3175 * interval, to ensure I detect a timeout on any
3176 * connection within (n+1)/n times the timeout
3179 if (*kiblnd_tunables.kib_timeout > n * p)
3180 chunk = (chunk * n * p) /
3181 *kiblnd_tunables.kib_timeout;
3185 for (i = 0; i < chunk; i++) {
3186 kiblnd_check_conns(peer_index);
3187 peer_index = (peer_index + 1) %
3188 kiblnd_data.kib_peer_hash_size;
3191 deadline += p * CFS_HZ;
3192 cfs_spin_lock_irqsave(&kiblnd_data.kib_connd_lock,
3199 /* Nothing to do for 'timeout' */
3200 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
3201 cfs_waitq_add (&kiblnd_data.kib_connd_waitq, &wait);
3202 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3204 cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE, timeout);
3206 cfs_set_current_state (CFS_TASK_RUNNING);
3207 cfs_waitq_del (&kiblnd_data.kib_connd_waitq, &wait);
3208 cfs_spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
3211 cfs_spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3213 kiblnd_thread_fini();
3218 kiblnd_qp_event(struct ib_event *event, void *arg)
3220 kib_conn_t *conn = arg;
3222 switch (event->event) {
3223 case IB_EVENT_COMM_EST:
3224 CDEBUG(D_NET, "%s established\n",
3225 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3229 CERROR("%s: Async QP event type %d\n",
3230 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3236 kiblnd_complete (struct ib_wc *wc)
3238 switch (kiblnd_wreqid2type(wc->wr_id)) {
3242 case IBLND_WID_RDMA:
3243 /* We only get RDMA completion notification if it fails. All
3244 * subsequent work items, including the final SEND will fail
3245 * too. However we can't print out any more info about the
3246 * failing RDMA because 'tx' might be back on the idle list or
3247 * even reused already if we didn't manage to post all our work
3249 CNETERR("RDMA (tx: %p) failed: %d\n",
3250 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3254 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3258 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3265 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3267 /* NB I'm not allowed to schedule this conn once its refcount has
3268 * reached 0. Since fundamentally I'm racing with scheduler threads
3269 * consuming my CQ I could be called after all completions have
3270 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3271 * and this CQ is about to be destroyed so I NOOP. */
3272 kib_conn_t *conn = (kib_conn_t *)arg;
3273 unsigned long flags;
3275 LASSERT (cq == conn->ibc_cq);
3277 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3279 conn->ibc_ready = 1;
3281 if (!conn->ibc_scheduled &&
3282 (conn->ibc_nrx > 0 ||
3283 conn->ibc_nsends_posted > 0)) {
3284 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3285 conn->ibc_scheduled = 1;
3286 cfs_list_add_tail(&conn->ibc_sched_list,
3287 &kiblnd_data.kib_sched_conns);
3288 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3291 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3295 kiblnd_cq_event(struct ib_event *event, void *arg)
3297 kib_conn_t *conn = arg;
3299 CERROR("%s: async CQ event type %d\n",
3300 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3304 kiblnd_scheduler(void *arg)
3306 long id = (long)arg;
3307 cfs_waitlink_t wait;
3309 unsigned long flags;
3316 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3317 cfs_daemonize(name);
3318 cfs_block_allsigs();
3320 cfs_waitlink_init(&wait);
3322 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3324 while (!kiblnd_data.kib_shutdown) {
3325 if (busy_loops++ >= IBLND_RESCHED) {
3326 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3332 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3338 if (!cfs_list_empty(&kiblnd_data.kib_sched_conns)) {
3339 conn = cfs_list_entry(kiblnd_data.kib_sched_conns.next,
3340 kib_conn_t, ibc_sched_list);
3341 /* take over kib_sched_conns' ref on conn... */
3342 LASSERT(conn->ibc_scheduled);
3343 cfs_list_del(&conn->ibc_sched_list);
3344 conn->ibc_ready = 0;
3346 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3349 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3351 rc = ib_req_notify_cq(conn->ibc_cq,
3354 CWARN("%s: ib_req_notify_cq failed: %d, "
3355 "closing connection\n",
3356 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3357 kiblnd_close_conn(conn, -EIO);
3358 kiblnd_conn_decref(conn);
3359 cfs_spin_lock_irqsave(&kiblnd_data. \
3365 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3369 CWARN("%s: ib_poll_cq failed: %d, "
3370 "closing connection\n",
3371 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3373 kiblnd_close_conn(conn, -EIO);
3374 kiblnd_conn_decref(conn);
3375 cfs_spin_lock_irqsave(&kiblnd_data. \
3376 kib_sched_lock, flags);
3380 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3383 if (rc != 0 || conn->ibc_ready) {
3384 /* There may be another completion waiting; get
3385 * another scheduler to check while I handle
3387 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3388 cfs_list_add_tail(&conn->ibc_sched_list,
3389 &kiblnd_data.kib_sched_conns);
3390 cfs_waitq_signal(&kiblnd_data.kib_sched_waitq);
3392 conn->ibc_scheduled = 0;
3396 cfs_spin_unlock_irqrestore(&kiblnd_data. \
3400 kiblnd_complete(&wc);
3402 cfs_spin_lock_irqsave(&kiblnd_data. \
3407 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3414 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3415 cfs_waitq_add_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3416 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3418 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
3421 cfs_waitq_del(&kiblnd_data.kib_sched_waitq, &wait);
3422 cfs_set_current_state(CFS_TASK_RUNNING);
3423 cfs_spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3426 cfs_spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3428 kiblnd_thread_fini();
3433 kiblnd_failover_thread(void *arg)
3435 cfs_rwlock_t *glock = &kiblnd_data.kib_global_lock;
3437 cfs_waitlink_t wait;
3438 unsigned long flags;
3441 LASSERT (*kiblnd_tunables.kib_dev_failover != 0);
3443 cfs_daemonize ("kiblnd_failover");
3444 cfs_block_allsigs ();
3446 cfs_waitlink_init(&wait);
3447 cfs_write_lock_irqsave(glock, flags);
3449 while (!kiblnd_data.kib_shutdown) {
3450 int do_failover = 0;
3453 cfs_list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3455 if (cfs_time_before(cfs_time_current(),
3456 dev->ibd_next_failover))
3463 cfs_list_del_init(&dev->ibd_fail_list);
3464 dev->ibd_failover = 1;
3465 cfs_write_unlock_irqrestore(glock, flags);
3467 rc = kiblnd_dev_failover(dev);
3469 cfs_write_lock_irqsave(glock, flags);
3471 LASSERT (dev->ibd_failover);
3472 dev->ibd_failover = 0;
3473 if (rc >= 0) { /* Device is OK or failover succeed */
3474 dev->ibd_next_failover = cfs_time_shift(3);
3478 /* failed to failover, retry later */
3479 dev->ibd_next_failover =
3480 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3481 if (kiblnd_dev_can_failover(dev)) {
3482 cfs_list_add_tail(&dev->ibd_fail_list,
3483 &kiblnd_data.kib_failed_devs);
3489 /* long sleep if no more pending failover */
3490 long_sleep = cfs_list_empty(&kiblnd_data.kib_failed_devs);
3492 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3493 cfs_waitq_add(&kiblnd_data.kib_failover_waitq, &wait);
3494 cfs_write_unlock_irqrestore(glock, flags);
3496 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3497 cfs_time_seconds(1));
3498 cfs_set_current_state(CFS_TASK_RUNNING);
3499 cfs_waitq_del(&kiblnd_data.kib_failover_waitq, &wait);
3500 cfs_write_lock_irqsave(glock, flags);
3502 if (!long_sleep || rc != 0)
3505 /* have a long sleep, routine check all active devices,
3506 * we need checking like this because if there is not active
3507 * connection on the dev and no SEND from local, we may listen
3508 * on wrong HCA for ever while there is a bonding failover */
3509 cfs_list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3510 if (kiblnd_dev_can_failover(dev)) {
3511 cfs_list_add_tail(&dev->ibd_fail_list,
3512 &kiblnd_data.kib_failed_devs);
3517 cfs_write_unlock_irqrestore(glock, flags);
3519 kiblnd_thread_fini();