4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lnet/klnds/o2iblnd/o2iblnd_cb.c
36 * Author: Eric Barton <eric@bartonsoftware.com>
42 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
44 lnet_msg_t *lntmsg[2];
45 kib_net_t *net = ni->ni_data;
49 LASSERT (net != NULL);
50 LASSERT (!cfs_in_interrupt());
51 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
52 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
53 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
54 LASSERT (tx->tx_pool != NULL);
56 kiblnd_unmap_tx(ni, tx);
58 /* tx may have up to 2 lnet msgs to finalise */
59 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
60 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
63 if (tx->tx_conn != NULL) {
64 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
66 kiblnd_conn_decref(tx->tx_conn);
73 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
75 /* delay finalize until my descs have been freed */
76 for (i = 0; i < 2; i++) {
77 if (lntmsg[i] == NULL)
80 lnet_finalize(ni, lntmsg[i], rc);
85 kiblnd_txlist_done (lnet_ni_t *ni, cfs_list_t *txlist, int status)
89 while (!cfs_list_empty (txlist)) {
90 tx = cfs_list_entry (txlist->next, kib_tx_t, tx_list);
92 cfs_list_del(&tx->tx_list);
95 tx->tx_status = status;
96 kiblnd_tx_done(ni, tx);
101 kiblnd_get_idle_tx(lnet_ni_t *ni, lnet_nid_t target)
103 kib_net_t *net = (kib_net_t *)ni->ni_data;
106 kib_tx_poolset_t *tps;
108 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target)];
109 node = kiblnd_pool_alloc_node(&tps->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;
132 struct kib_sched_info *sched = conn->ibc_sched;
135 spin_lock_irqsave(&sched->ibs_lock, flags);
136 LASSERT(conn->ibc_nrx > 0);
138 spin_unlock_irqrestore(&sched->ibs_lock, flags);
140 kiblnd_conn_decref(conn);
144 kiblnd_post_rx (kib_rx_t *rx, int credit)
146 kib_conn_t *conn = rx->rx_conn;
147 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
148 struct ib_recv_wr *bad_wrq = NULL;
152 LASSERT (net != NULL);
153 LASSERT (!cfs_in_interrupt());
154 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
155 credit == IBLND_POSTRX_PEER_CREDIT ||
156 credit == IBLND_POSTRX_RSRVD_CREDIT);
158 mr = kiblnd_find_dma_mr(conn->ibc_hdev, rx->rx_msgaddr, IBLND_MSG_SIZE);
159 LASSERT (mr != NULL);
161 rx->rx_sge.lkey = mr->lkey;
162 rx->rx_sge.addr = rx->rx_msgaddr;
163 rx->rx_sge.length = IBLND_MSG_SIZE;
165 rx->rx_wrq.next = NULL;
166 rx->rx_wrq.sg_list = &rx->rx_sge;
167 rx->rx_wrq.num_sge = 1;
168 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
170 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
171 LASSERT (rx->rx_nob >= 0); /* not posted */
173 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
174 kiblnd_drop_rx(rx); /* No more posts for this rx */
178 rx->rx_nob = -1; /* flag posted */
180 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
182 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
183 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
187 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
191 kiblnd_close_conn(conn, rc);
192 kiblnd_drop_rx(rx); /* No more posts for this rx */
196 if (credit == IBLND_POSTRX_NO_CREDIT)
199 spin_lock(&conn->ibc_lock);
200 if (credit == IBLND_POSTRX_PEER_CREDIT)
201 conn->ibc_outstanding_credits++;
203 conn->ibc_reserved_credits++;
204 spin_unlock(&conn->ibc_lock);
206 kiblnd_check_sends(conn);
211 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
215 cfs_list_for_each(tmp, &conn->ibc_active_txs) {
216 kib_tx_t *tx = cfs_list_entry(tmp, kib_tx_t, tx_list);
218 LASSERT (!tx->tx_queued);
219 LASSERT (tx->tx_sending != 0 || tx->tx_waiting);
221 if (tx->tx_cookie != cookie)
224 if (tx->tx_waiting &&
225 tx->tx_msg->ibm_type == txtype)
228 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
229 tx->tx_waiting ? "" : "NOT ",
230 tx->tx_msg->ibm_type, txtype);
236 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
239 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
242 spin_lock(&conn->ibc_lock);
244 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
246 spin_unlock(&conn->ibc_lock);
248 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
249 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
250 kiblnd_close_conn(conn, -EPROTO);
254 if (tx->tx_status == 0) { /* success so far */
255 if (status < 0) { /* failed? */
256 tx->tx_status = status;
257 } else if (txtype == IBLND_MSG_GET_REQ) {
258 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
264 idle = !tx->tx_queued && (tx->tx_sending == 0);
266 cfs_list_del(&tx->tx_list);
268 spin_unlock(&conn->ibc_lock);
271 kiblnd_tx_done(ni, tx);
275 kiblnd_send_completion(kib_conn_t *conn, int type, int status, __u64 cookie)
277 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
278 kib_tx_t *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
281 CERROR("Can't get tx for completion %x for %s\n",
282 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
286 tx->tx_msg->ibm_u.completion.ibcm_status = status;
287 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
288 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
290 kiblnd_queue_tx(tx, conn);
294 kiblnd_handle_rx (kib_rx_t *rx)
296 kib_msg_t *msg = rx->rx_msg;
297 kib_conn_t *conn = rx->rx_conn;
298 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
299 int credits = msg->ibm_credits;
305 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
307 CDEBUG (D_NET, "Received %x[%d] from %s\n",
308 msg->ibm_type, credits,
309 libcfs_nid2str(conn->ibc_peer->ibp_nid));
312 /* Have I received credits that will let me send? */
313 spin_lock(&conn->ibc_lock);
315 if (conn->ibc_credits + credits >
316 IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
317 rc2 = conn->ibc_credits;
318 spin_unlock(&conn->ibc_lock);
320 CERROR("Bad credits from %s: %d + %d > %d\n",
321 libcfs_nid2str(conn->ibc_peer->ibp_nid),
323 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
325 kiblnd_close_conn(conn, -EPROTO);
326 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
330 conn->ibc_credits += credits;
332 /* This ensures the credit taken by NOOP can be returned */
333 if (msg->ibm_type == IBLND_MSG_NOOP &&
334 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
335 conn->ibc_outstanding_credits++;
337 spin_unlock(&conn->ibc_lock);
338 kiblnd_check_sends(conn);
341 switch (msg->ibm_type) {
343 CERROR("Bad IBLND message type %x from %s\n",
344 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
345 post_credit = IBLND_POSTRX_NO_CREDIT;
350 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
351 post_credit = IBLND_POSTRX_NO_CREDIT;
355 if (credits != 0) /* credit already posted */
356 post_credit = IBLND_POSTRX_NO_CREDIT;
357 else /* a keepalive NOOP */
358 post_credit = IBLND_POSTRX_PEER_CREDIT;
361 case IBLND_MSG_IMMEDIATE:
362 post_credit = IBLND_POSTRX_DONT_POST;
363 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
364 msg->ibm_srcnid, rx, 0);
365 if (rc < 0) /* repost on error */
366 post_credit = IBLND_POSTRX_PEER_CREDIT;
369 case IBLND_MSG_PUT_REQ:
370 post_credit = IBLND_POSTRX_DONT_POST;
371 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
372 msg->ibm_srcnid, rx, 1);
373 if (rc < 0) /* repost on error */
374 post_credit = IBLND_POSTRX_PEER_CREDIT;
377 case IBLND_MSG_PUT_NAK:
378 CWARN ("PUT_NACK from %s\n",
379 libcfs_nid2str(conn->ibc_peer->ibp_nid));
380 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
381 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
382 msg->ibm_u.completion.ibcm_status,
383 msg->ibm_u.completion.ibcm_cookie);
386 case IBLND_MSG_PUT_ACK:
387 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
389 spin_lock(&conn->ibc_lock);
390 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
391 msg->ibm_u.putack.ibpam_src_cookie);
393 cfs_list_del(&tx->tx_list);
394 spin_unlock(&conn->ibc_lock);
397 CERROR("Unmatched PUT_ACK from %s\n",
398 libcfs_nid2str(conn->ibc_peer->ibp_nid));
403 LASSERT (tx->tx_waiting);
404 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
405 * (a) I can overwrite tx_msg since my peer has received it!
406 * (b) tx_waiting set tells tx_complete() it's not done. */
408 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
410 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
411 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
412 &msg->ibm_u.putack.ibpam_rd,
413 msg->ibm_u.putack.ibpam_dst_cookie);
415 CERROR("Can't setup rdma for PUT to %s: %d\n",
416 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
418 spin_lock(&conn->ibc_lock);
419 tx->tx_waiting = 0; /* clear waiting and queue atomically */
420 kiblnd_queue_tx_locked(tx, conn);
421 spin_unlock(&conn->ibc_lock);
424 case IBLND_MSG_PUT_DONE:
425 post_credit = IBLND_POSTRX_PEER_CREDIT;
426 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
427 msg->ibm_u.completion.ibcm_status,
428 msg->ibm_u.completion.ibcm_cookie);
431 case IBLND_MSG_GET_REQ:
432 post_credit = IBLND_POSTRX_DONT_POST;
433 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
434 msg->ibm_srcnid, rx, 1);
435 if (rc < 0) /* repost on error */
436 post_credit = IBLND_POSTRX_PEER_CREDIT;
439 case IBLND_MSG_GET_DONE:
440 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
441 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
442 msg->ibm_u.completion.ibcm_status,
443 msg->ibm_u.completion.ibcm_cookie);
447 if (rc < 0) /* protocol error */
448 kiblnd_close_conn(conn, rc);
450 if (post_credit != IBLND_POSTRX_DONT_POST)
451 kiblnd_post_rx(rx, post_credit);
455 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
457 kib_msg_t *msg = rx->rx_msg;
458 kib_conn_t *conn = rx->rx_conn;
459 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
460 kib_net_t *net = ni->ni_data;
464 LASSERT (net != NULL);
465 LASSERT (rx->rx_nob < 0); /* was posted */
466 rx->rx_nob = 0; /* isn't now */
468 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
471 if (status != IB_WC_SUCCESS) {
472 CNETERR("Rx from %s failed: %d\n",
473 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
480 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
482 CERROR ("Error %d unpacking rx from %s\n",
483 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
487 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
488 msg->ibm_dstnid != ni->ni_nid ||
489 msg->ibm_srcstamp != conn->ibc_incarnation ||
490 msg->ibm_dststamp != net->ibn_incarnation) {
491 CERROR ("Stale rx from %s\n",
492 libcfs_nid2str(conn->ibc_peer->ibp_nid));
497 /* set time last known alive */
498 kiblnd_peer_alive(conn->ibc_peer);
500 /* racing with connection establishment/teardown! */
502 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
503 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
506 write_lock_irqsave(g_lock, flags);
507 /* must check holding global lock to eliminate race */
508 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
509 cfs_list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
510 write_unlock_irqrestore(g_lock, flags);
513 write_unlock_irqrestore(g_lock, flags);
515 kiblnd_handle_rx(rx);
519 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
520 kiblnd_close_conn(conn, err);
522 kiblnd_drop_rx(rx); /* Don't re-post rx. */
526 kiblnd_kvaddr_to_page (unsigned long vaddr)
530 if (vaddr >= VMALLOC_START &&
531 vaddr < VMALLOC_END) {
532 page = vmalloc_to_page ((void *)vaddr);
533 LASSERT (page != NULL);
536 #ifdef CONFIG_HIGHMEM
537 if (vaddr >= PKMAP_BASE &&
538 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
539 /* No highmem pages only used for bulk (kiov) I/O */
540 CERROR("find page for address in highmem\n");
544 page = virt_to_page (vaddr);
545 LASSERT (page != NULL);
550 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
552 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
553 __u64 *pages = tx->tx_pages;
554 kib_fmr_poolset_t *fps;
561 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
562 for (size = 0; size < rd->rd_frags[i].rf_nob;
563 size += hdev->ibh_page_size) {
564 pages[npages ++] = (rd->rd_frags[i].rf_addr &
565 hdev->ibh_page_mask) + size;
569 LASSERT(tx->tx_pool != NULL);
570 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
572 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
574 fps = net->ibn_fmr_ps[cpt];
575 rc = kiblnd_fmr_pool_map(fps, pages, npages, 0, &tx->tx_u.fmr);
577 CERROR ("Can't map %d pages: %d\n", npages, rc);
581 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
583 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.fmr.fmr_pfmr->fmr->rkey :
584 tx->tx_u.fmr.fmr_pfmr->fmr->lkey;
585 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
586 rd->rd_frags[0].rf_nob = nob;
593 kiblnd_pmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
595 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
596 kib_pmr_poolset_t *pps;
601 iova = rd->rd_frags[0].rf_addr & ~hdev->ibh_page_mask;
603 LASSERT(tx->tx_pool != NULL);
604 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
606 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
608 pps = net->ibn_pmr_ps[cpt];
609 rc = kiblnd_pmr_pool_map(pps, hdev, rd, &iova, &tx->tx_u.pmr);
611 CERROR("Failed to create MR by phybuf: %d\n", rc);
615 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
617 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.pmr->pmr_mr->rkey :
618 tx->tx_u.pmr->pmr_mr->lkey;
620 rd->rd_frags[0].rf_addr = iova;
621 rd->rd_frags[0].rf_nob = nob;
627 kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
629 kib_net_t *net = ni->ni_data;
631 LASSERT(net != NULL);
633 if (net->ibn_fmr_ps != NULL && tx->tx_u.fmr.fmr_pfmr != NULL) {
634 kiblnd_fmr_pool_unmap(&tx->tx_u.fmr, tx->tx_status);
635 tx->tx_u.fmr.fmr_pfmr = NULL;
637 } else if (net->ibn_pmr_ps != NULL && tx->tx_u.pmr != NULL) {
638 kiblnd_pmr_pool_unmap(tx->tx_u.pmr);
642 if (tx->tx_nfrags != 0) {
643 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
644 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
650 kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx,
651 kib_rdma_desc_t *rd, int nfrags)
653 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
654 kib_net_t *net = ni->ni_data;
655 struct ib_mr *mr = NULL;
659 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
661 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
662 tx->tx_nfrags = nfrags;
665 kiblnd_dma_map_sg(hdev->ibh_ibdev,
666 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
668 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
669 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
670 hdev->ibh_ibdev, &tx->tx_frags[i]);
671 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
672 hdev->ibh_ibdev, &tx->tx_frags[i]);
673 nob += rd->rd_frags[i].rf_nob;
676 /* looking for pre-mapping MR */
677 mr = kiblnd_find_rd_dma_mr(hdev, rd);
679 /* found pre-mapping MR */
680 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
684 if (net->ibn_fmr_ps != NULL)
685 return kiblnd_fmr_map_tx(net, tx, rd, nob);
686 else if (net->ibn_pmr_ps != NULL)
687 return kiblnd_pmr_map_tx(net, tx, rd, nob);
694 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
695 unsigned int niov, struct iovec *iov, int offset, int nob)
697 kib_net_t *net = ni->ni_data;
699 struct scatterlist *sg;
706 LASSERT (net != NULL);
708 while (offset >= iov->iov_len) {
709 offset -= iov->iov_len;
719 vaddr = ((unsigned long)iov->iov_base) + offset;
720 page_offset = vaddr & (PAGE_SIZE - 1);
721 page = kiblnd_kvaddr_to_page(vaddr);
723 CERROR ("Can't find page\n");
727 fragnob = min((int)(iov->iov_len - offset), nob);
728 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
730 sg_set_page(sg, page, fragnob, page_offset);
733 if (offset + fragnob < iov->iov_len) {
743 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
747 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
748 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
750 kib_net_t *net = ni->ni_data;
751 struct scatterlist *sg;
754 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
758 LASSERT (net != NULL);
760 while (offset >= kiov->kiov_len) {
761 offset -= kiov->kiov_len;
771 fragnob = min((int)(kiov->kiov_len - offset), nob);
773 sg_set_page(sg, kiov->kiov_page, fragnob,
774 kiov->kiov_offset + offset);
783 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
787 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
789 kib_msg_t *msg = tx->tx_msg;
790 kib_peer_t *peer = conn->ibc_peer;
791 int ver = conn->ibc_version;
794 struct ib_send_wr *bad_wrq;
796 LASSERT (tx->tx_queued);
797 /* We rely on this for QP sizing */
798 LASSERT (tx->tx_nwrq > 0);
799 LASSERT (tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
801 LASSERT (credit == 0 || credit == 1);
802 LASSERT (conn->ibc_outstanding_credits >= 0);
803 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
804 LASSERT (conn->ibc_credits >= 0);
805 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
807 if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
808 /* tx completions outstanding... */
809 CDEBUG(D_NET, "%s: posted enough\n",
810 libcfs_nid2str(peer->ibp_nid));
814 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
815 CDEBUG(D_NET, "%s: no credits\n",
816 libcfs_nid2str(peer->ibp_nid));
820 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
821 conn->ibc_credits == 1 && /* last credit reserved */
822 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
823 CDEBUG(D_NET, "%s: not using last credit\n",
824 libcfs_nid2str(peer->ibp_nid));
828 /* NB don't drop ibc_lock before bumping tx_sending */
829 cfs_list_del(&tx->tx_list);
832 if (msg->ibm_type == IBLND_MSG_NOOP &&
833 (!kiblnd_need_noop(conn) || /* redundant NOOP */
834 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
835 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
836 /* OK to drop when posted enough NOOPs, since
837 * kiblnd_check_sends will queue NOOP again when
838 * posted NOOPs complete */
839 spin_unlock(&conn->ibc_lock);
840 kiblnd_tx_done(peer->ibp_ni, tx);
841 spin_lock(&conn->ibc_lock);
842 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
843 libcfs_nid2str(peer->ibp_nid),
844 conn->ibc_noops_posted);
848 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
849 peer->ibp_nid, conn->ibc_incarnation);
851 conn->ibc_credits -= credit;
852 conn->ibc_outstanding_credits = 0;
853 conn->ibc_nsends_posted++;
854 if (msg->ibm_type == IBLND_MSG_NOOP)
855 conn->ibc_noops_posted++;
857 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
858 * PUT. If so, it was first queued here as a PUT_REQ, sent and
859 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
860 * and then re-queued here. It's (just) possible that
861 * tx_sending is non-zero if we've not done the tx_complete()
862 * from the first send; hence the ++ rather than = below. */
864 cfs_list_add(&tx->tx_list, &conn->ibc_active_txs);
866 /* I'm still holding ibc_lock! */
867 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
869 } else if (tx->tx_pool->tpo_pool.po_failed ||
870 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
871 /* close_conn will launch failover */
874 rc = ib_post_send(conn->ibc_cmid->qp,
875 tx->tx_wrq, &bad_wrq);
878 conn->ibc_last_send = jiffies;
883 /* NB credits are transferred in the actual
884 * message, which can only be the last work item */
885 conn->ibc_credits += credit;
886 conn->ibc_outstanding_credits += msg->ibm_credits;
887 conn->ibc_nsends_posted--;
888 if (msg->ibm_type == IBLND_MSG_NOOP)
889 conn->ibc_noops_posted--;
895 done = (tx->tx_sending == 0);
897 cfs_list_del(&tx->tx_list);
899 spin_unlock(&conn->ibc_lock);
901 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
902 CERROR("Error %d posting transmit to %s\n",
903 rc, libcfs_nid2str(peer->ibp_nid));
905 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
906 rc, libcfs_nid2str(peer->ibp_nid));
908 kiblnd_close_conn(conn, rc);
911 kiblnd_tx_done(peer->ibp_ni, tx);
913 spin_lock(&conn->ibc_lock);
919 kiblnd_check_sends (kib_conn_t *conn)
921 int ver = conn->ibc_version;
922 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
925 /* Don't send anything until after the connection is established */
926 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
927 CDEBUG(D_NET, "%s too soon\n",
928 libcfs_nid2str(conn->ibc_peer->ibp_nid));
932 spin_lock(&conn->ibc_lock);
934 LASSERT (conn->ibc_nsends_posted <= IBLND_CONCURRENT_SENDS(ver));
935 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
936 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
937 LASSERT (conn->ibc_reserved_credits >= 0);
939 while (conn->ibc_reserved_credits > 0 &&
940 !cfs_list_empty(&conn->ibc_tx_queue_rsrvd)) {
941 tx = cfs_list_entry(conn->ibc_tx_queue_rsrvd.next,
943 cfs_list_del(&tx->tx_list);
944 cfs_list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
945 conn->ibc_reserved_credits--;
948 if (kiblnd_need_noop(conn)) {
949 spin_unlock(&conn->ibc_lock);
951 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
953 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
955 spin_lock(&conn->ibc_lock);
957 kiblnd_queue_tx_locked(tx, conn);
960 kiblnd_conn_addref(conn); /* 1 ref for me.... (see b21911) */
965 if (!cfs_list_empty(&conn->ibc_tx_queue_nocred)) {
967 tx = cfs_list_entry(conn->ibc_tx_queue_nocred.next,
969 } else if (!cfs_list_empty(&conn->ibc_tx_noops)) {
970 LASSERT (!IBLND_OOB_CAPABLE(ver));
972 tx = cfs_list_entry(conn->ibc_tx_noops.next,
974 } else if (!cfs_list_empty(&conn->ibc_tx_queue)) {
976 tx = cfs_list_entry(conn->ibc_tx_queue.next,
981 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
985 spin_unlock(&conn->ibc_lock);
987 kiblnd_conn_decref(conn); /* ...until here */
991 kiblnd_tx_complete (kib_tx_t *tx, int status)
993 int failed = (status != IB_WC_SUCCESS);
994 kib_conn_t *conn = tx->tx_conn;
997 LASSERT (tx->tx_sending > 0);
1000 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1001 CNETERR("Tx -> %s cookie "LPX64
1002 " sending %d waiting %d: failed %d\n",
1003 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1004 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1007 kiblnd_close_conn(conn, -EIO);
1009 kiblnd_peer_alive(conn->ibc_peer);
1012 spin_lock(&conn->ibc_lock);
1014 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1015 * gets to free it, which also drops its ref on 'conn'. */
1018 conn->ibc_nsends_posted--;
1019 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1020 conn->ibc_noops_posted--;
1023 tx->tx_waiting = 0; /* don't wait for peer */
1024 tx->tx_status = -EIO;
1027 idle = (tx->tx_sending == 0) && /* This is the final callback */
1028 !tx->tx_waiting && /* Not waiting for peer */
1029 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1031 cfs_list_del(&tx->tx_list);
1033 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1035 spin_unlock(&conn->ibc_lock);
1038 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1040 kiblnd_check_sends(conn);
1042 kiblnd_conn_decref(conn); /* ...until here */
1046 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1048 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
1049 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1050 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1051 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1054 LASSERT (tx->tx_nwrq >= 0);
1055 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1056 LASSERT (nob <= IBLND_MSG_SIZE);
1058 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1060 mr = kiblnd_find_dma_mr(hdev, tx->tx_msgaddr, nob);
1061 LASSERT (mr != NULL);
1063 sge->lkey = mr->lkey;
1064 sge->addr = tx->tx_msgaddr;
1067 memset(wrq, 0, sizeof(*wrq));
1070 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1073 wrq->opcode = IB_WR_SEND;
1074 wrq->send_flags = IB_SEND_SIGNALED;
1080 kiblnd_init_rdma (kib_conn_t *conn, kib_tx_t *tx, int type,
1081 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1083 kib_msg_t *ibmsg = tx->tx_msg;
1084 kib_rdma_desc_t *srcrd = tx->tx_rd;
1085 struct ib_sge *sge = &tx->tx_sge[0];
1086 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1092 LASSERT (!cfs_in_interrupt());
1093 LASSERT (tx->tx_nwrq == 0);
1094 LASSERT (type == IBLND_MSG_GET_DONE ||
1095 type == IBLND_MSG_PUT_DONE);
1097 srcidx = dstidx = 0;
1100 if (srcidx >= srcrd->rd_nfrags) {
1101 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1106 if (dstidx == dstrd->rd_nfrags) {
1107 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1112 if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
1113 CERROR("RDMA too fragmented for %s (%d): "
1114 "%d/%d src %d/%d dst frags\n",
1115 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1116 IBLND_RDMA_FRAGS(conn->ibc_version),
1117 srcidx, srcrd->rd_nfrags,
1118 dstidx, dstrd->rd_nfrags);
1123 wrknob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1124 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1126 sge = &tx->tx_sge[tx->tx_nwrq];
1127 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1128 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1129 sge->length = wrknob;
1131 wrq = &tx->tx_wrq[tx->tx_nwrq];
1133 wrq->next = wrq + 1;
1134 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1137 wrq->opcode = IB_WR_RDMA_WRITE;
1138 wrq->send_flags = 0;
1140 wrq->wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1141 wrq->wr.rdma.rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1143 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1144 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1153 if (rc < 0) /* no RDMA if completing with failure */
1156 ibmsg->ibm_u.completion.ibcm_status = rc;
1157 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1158 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1159 type, sizeof (kib_completion_msg_t));
1165 kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
1169 LASSERT (tx->tx_nwrq > 0); /* work items set up */
1170 LASSERT (!tx->tx_queued); /* not queued for sending already */
1171 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1174 tx->tx_deadline = jiffies + (*kiblnd_tunables.kib_timeout * CFS_HZ);
1176 if (tx->tx_conn == NULL) {
1177 kiblnd_conn_addref(conn);
1179 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1181 /* PUT_DONE first attached to conn as a PUT_REQ */
1182 LASSERT (tx->tx_conn == conn);
1183 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1186 switch (tx->tx_msg->ibm_type) {
1190 case IBLND_MSG_PUT_REQ:
1191 case IBLND_MSG_GET_REQ:
1192 q = &conn->ibc_tx_queue_rsrvd;
1195 case IBLND_MSG_PUT_NAK:
1196 case IBLND_MSG_PUT_ACK:
1197 case IBLND_MSG_PUT_DONE:
1198 case IBLND_MSG_GET_DONE:
1199 q = &conn->ibc_tx_queue_nocred;
1202 case IBLND_MSG_NOOP:
1203 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1204 q = &conn->ibc_tx_queue_nocred;
1206 q = &conn->ibc_tx_noops;
1209 case IBLND_MSG_IMMEDIATE:
1210 q = &conn->ibc_tx_queue;
1214 cfs_list_add_tail(&tx->tx_list, q);
1218 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1220 spin_lock(&conn->ibc_lock);
1221 kiblnd_queue_tx_locked(tx, conn);
1222 spin_unlock(&conn->ibc_lock);
1224 kiblnd_check_sends(conn);
1227 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1228 struct sockaddr_in *srcaddr,
1229 struct sockaddr_in *dstaddr,
1232 unsigned short port;
1235 #ifdef HAVE_OFED_RDMA_SET_REUSEADDR
1236 /* allow the port to be reused */
1237 rc = rdma_set_reuseaddr(cmid, 1);
1239 CERROR("Unable to set reuse on cmid: %d\n", rc);
1244 /* look for a free privileged port */
1245 for (port = PROT_SOCK-1; port > 0; port--) {
1246 srcaddr->sin_port = htons(port);
1247 rc = rdma_resolve_addr(cmid,
1248 (struct sockaddr *)srcaddr,
1249 (struct sockaddr *)dstaddr,
1252 CDEBUG(D_NET, "bound to port %hu\n", port);
1254 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1255 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1262 CERROR("Failed to bind to a free privileged port\n");
1263 #ifndef HAVE_OFED_RDMA_SET_REUSEADDR
1264 CERROR("You may need IB verbs that supports rdma_set_reuseaddr()\n");
1270 kiblnd_connect_peer (kib_peer_t *peer)
1272 struct rdma_cm_id *cmid;
1274 kib_net_t *net = peer->ibp_ni->ni_data;
1275 struct sockaddr_in srcaddr;
1276 struct sockaddr_in dstaddr;
1279 LASSERT (net != NULL);
1280 LASSERT (peer->ibp_connecting > 0);
1282 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP,
1286 CERROR("Can't create CMID for %s: %ld\n",
1287 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1293 memset(&srcaddr, 0, sizeof(srcaddr));
1294 srcaddr.sin_family = AF_INET;
1295 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1297 memset(&dstaddr, 0, sizeof(dstaddr));
1298 dstaddr.sin_family = AF_INET;
1299 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1300 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1302 kiblnd_peer_addref(peer); /* cmid's ref */
1304 if (*kiblnd_tunables.kib_use_priv_port) {
1305 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1306 *kiblnd_tunables.kib_timeout * 1000);
1308 rc = rdma_resolve_addr(cmid,
1309 (struct sockaddr *)&srcaddr,
1310 (struct sockaddr *)&dstaddr,
1311 *kiblnd_tunables.kib_timeout * 1000);
1314 /* Can't initiate address resolution: */
1315 CERROR("Can't resolve addr for %s: %d\n",
1316 libcfs_nid2str(peer->ibp_nid), rc);
1320 LASSERT (cmid->device != NULL);
1321 CDEBUG(D_NET, "%s: connection bound to %s:%u.%u.%u.%u:%s\n",
1322 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1323 HIPQUAD(dev->ibd_ifip), cmid->device->name);
1328 kiblnd_peer_decref(peer); /* cmid's ref */
1329 rdma_destroy_id(cmid);
1331 kiblnd_peer_connect_failed(peer, 1, rc);
1335 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1340 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1341 unsigned long flags;
1344 /* If I get here, I've committed to send, so I complete the tx with
1345 * failure on any problems */
1347 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1348 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1350 /* First time, just use a read lock since I expect to find my peer
1352 read_lock_irqsave(g_lock, flags);
1354 peer = kiblnd_find_peer_locked(nid);
1355 if (peer != NULL && !cfs_list_empty(&peer->ibp_conns)) {
1356 /* Found a peer with an established connection */
1357 conn = kiblnd_get_conn_locked(peer);
1358 kiblnd_conn_addref(conn); /* 1 ref for me... */
1360 read_unlock_irqrestore(g_lock, flags);
1363 kiblnd_queue_tx(tx, conn);
1364 kiblnd_conn_decref(conn); /* ...to here */
1368 read_unlock(g_lock);
1369 /* Re-try with a write lock */
1372 peer = kiblnd_find_peer_locked(nid);
1374 if (cfs_list_empty(&peer->ibp_conns)) {
1375 /* found a peer, but it's still connecting... */
1376 LASSERT (peer->ibp_connecting != 0 ||
1377 peer->ibp_accepting != 0);
1379 cfs_list_add_tail(&tx->tx_list,
1380 &peer->ibp_tx_queue);
1381 write_unlock_irqrestore(g_lock, flags);
1383 conn = kiblnd_get_conn_locked(peer);
1384 kiblnd_conn_addref(conn); /* 1 ref for me... */
1386 write_unlock_irqrestore(g_lock, flags);
1389 kiblnd_queue_tx(tx, conn);
1390 kiblnd_conn_decref(conn); /* ...to here */
1395 write_unlock_irqrestore(g_lock, flags);
1397 /* Allocate a peer ready to add to the peer table and retry */
1398 rc = kiblnd_create_peer(ni, &peer, nid);
1400 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1402 tx->tx_status = -EHOSTUNREACH;
1404 kiblnd_tx_done(ni, tx);
1409 write_lock_irqsave(g_lock, flags);
1411 peer2 = kiblnd_find_peer_locked(nid);
1412 if (peer2 != NULL) {
1413 if (cfs_list_empty(&peer2->ibp_conns)) {
1414 /* found a peer, but it's still connecting... */
1415 LASSERT (peer2->ibp_connecting != 0 ||
1416 peer2->ibp_accepting != 0);
1418 cfs_list_add_tail(&tx->tx_list,
1419 &peer2->ibp_tx_queue);
1420 write_unlock_irqrestore(g_lock, flags);
1422 conn = kiblnd_get_conn_locked(peer2);
1423 kiblnd_conn_addref(conn); /* 1 ref for me... */
1425 write_unlock_irqrestore(g_lock, flags);
1428 kiblnd_queue_tx(tx, conn);
1429 kiblnd_conn_decref(conn); /* ...to here */
1432 kiblnd_peer_decref(peer);
1436 /* Brand new peer */
1437 LASSERT (peer->ibp_connecting == 0);
1438 peer->ibp_connecting = 1;
1440 /* always called with a ref on ni, which prevents ni being shutdown */
1441 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1444 cfs_list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1446 kiblnd_peer_addref(peer);
1447 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1449 write_unlock_irqrestore(g_lock, flags);
1451 kiblnd_connect_peer(peer);
1452 kiblnd_peer_decref(peer);
1456 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1458 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1459 int type = lntmsg->msg_type;
1460 lnet_process_id_t target = lntmsg->msg_target;
1461 int target_is_router = lntmsg->msg_target_is_router;
1462 int routing = lntmsg->msg_routing;
1463 unsigned int payload_niov = lntmsg->msg_niov;
1464 struct iovec *payload_iov = lntmsg->msg_iov;
1465 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1466 unsigned int payload_offset = lntmsg->msg_offset;
1467 unsigned int payload_nob = lntmsg->msg_len;
1473 /* NB 'private' is different depending on what we're sending.... */
1475 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1476 payload_nob, payload_niov, libcfs_id2str(target));
1478 LASSERT (payload_nob == 0 || payload_niov > 0);
1479 LASSERT (payload_niov <= LNET_MAX_IOV);
1481 /* Thread context */
1482 LASSERT (!cfs_in_interrupt());
1483 /* payload is either all vaddrs or all pages */
1484 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1492 LASSERT (payload_nob == 0);
1496 if (routing || target_is_router)
1497 break; /* send IMMEDIATE */
1499 /* is the REPLY message too small for RDMA? */
1500 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1501 if (nob <= IBLND_MSG_SIZE)
1502 break; /* send IMMEDIATE */
1504 tx = kiblnd_get_idle_tx(ni, target.nid);
1506 CERROR("Can't allocate txd for GET to %s\n",
1507 libcfs_nid2str(target.nid));
1513 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1514 rc = kiblnd_setup_rd_iov(ni, tx,
1515 &ibmsg->ibm_u.get.ibgm_rd,
1516 lntmsg->msg_md->md_niov,
1517 lntmsg->msg_md->md_iov.iov,
1518 0, lntmsg->msg_md->md_length);
1520 rc = kiblnd_setup_rd_kiov(ni, tx,
1521 &ibmsg->ibm_u.get.ibgm_rd,
1522 lntmsg->msg_md->md_niov,
1523 lntmsg->msg_md->md_iov.kiov,
1524 0, lntmsg->msg_md->md_length);
1526 CERROR("Can't setup GET sink for %s: %d\n",
1527 libcfs_nid2str(target.nid), rc);
1528 kiblnd_tx_done(ni, tx);
1532 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1533 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1534 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1536 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1538 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1539 if (tx->tx_lntmsg[1] == NULL) {
1540 CERROR("Can't create reply for GET -> %s\n",
1541 libcfs_nid2str(target.nid));
1542 kiblnd_tx_done(ni, tx);
1546 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1547 tx->tx_waiting = 1; /* waiting for GET_DONE */
1548 kiblnd_launch_tx(ni, tx, target.nid);
1551 case LNET_MSG_REPLY:
1553 /* Is the payload small enough not to need RDMA? */
1554 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1555 if (nob <= IBLND_MSG_SIZE)
1556 break; /* send IMMEDIATE */
1558 tx = kiblnd_get_idle_tx(ni, target.nid);
1560 CERROR("Can't allocate %s txd for %s\n",
1561 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1562 libcfs_nid2str(target.nid));
1566 if (payload_kiov == NULL)
1567 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1568 payload_niov, payload_iov,
1569 payload_offset, payload_nob);
1571 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1572 payload_niov, payload_kiov,
1573 payload_offset, payload_nob);
1575 CERROR("Can't setup PUT src for %s: %d\n",
1576 libcfs_nid2str(target.nid), rc);
1577 kiblnd_tx_done(ni, tx);
1582 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1583 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1584 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1586 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1587 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1588 kiblnd_launch_tx(ni, tx, target.nid);
1592 /* send IMMEDIATE */
1594 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1597 tx = kiblnd_get_idle_tx(ni, target.nid);
1599 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1600 type, libcfs_nid2str(target.nid));
1605 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1607 if (payload_kiov != NULL)
1608 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1609 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1610 payload_niov, payload_kiov,
1611 payload_offset, payload_nob);
1613 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1614 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1615 payload_niov, payload_iov,
1616 payload_offset, payload_nob);
1618 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1619 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1621 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1622 kiblnd_launch_tx(ni, tx, target.nid);
1627 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1629 lnet_process_id_t target = lntmsg->msg_target;
1630 unsigned int niov = lntmsg->msg_niov;
1631 struct iovec *iov = lntmsg->msg_iov;
1632 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1633 unsigned int offset = lntmsg->msg_offset;
1634 unsigned int nob = lntmsg->msg_len;
1638 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1640 CERROR("Can't get tx for REPLY to %s\n",
1641 libcfs_nid2str(target.nid));
1647 else if (kiov == NULL)
1648 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1649 niov, iov, offset, nob);
1651 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1652 niov, kiov, offset, nob);
1655 CERROR("Can't setup GET src for %s: %d\n",
1656 libcfs_nid2str(target.nid), rc);
1660 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1661 IBLND_MSG_GET_DONE, nob,
1662 &rx->rx_msg->ibm_u.get.ibgm_rd,
1663 rx->rx_msg->ibm_u.get.ibgm_cookie);
1665 CERROR("Can't setup rdma for GET from %s: %d\n",
1666 libcfs_nid2str(target.nid), rc);
1671 /* No RDMA: local completion may happen now! */
1672 lnet_finalize(ni, lntmsg, 0);
1674 /* RDMA: lnet_finalize(lntmsg) when it
1676 tx->tx_lntmsg[0] = lntmsg;
1679 kiblnd_queue_tx(tx, rx->rx_conn);
1683 kiblnd_tx_done(ni, tx);
1685 lnet_finalize(ni, lntmsg, -EIO);
1689 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1690 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1691 unsigned int offset, unsigned int mlen, unsigned int rlen)
1693 kib_rx_t *rx = private;
1694 kib_msg_t *rxmsg = rx->rx_msg;
1695 kib_conn_t *conn = rx->rx_conn;
1699 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1702 LASSERT (mlen <= rlen);
1703 LASSERT (!cfs_in_interrupt());
1704 /* Either all pages or all vaddrs */
1705 LASSERT (!(kiov != NULL && iov != NULL));
1707 switch (rxmsg->ibm_type) {
1711 case IBLND_MSG_IMMEDIATE:
1712 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1713 if (nob > rx->rx_nob) {
1714 CERROR ("Immediate message from %s too big: %d(%d)\n",
1715 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1722 lnet_copy_flat2kiov(niov, kiov, offset,
1723 IBLND_MSG_SIZE, rxmsg,
1724 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1727 lnet_copy_flat2iov(niov, iov, offset,
1728 IBLND_MSG_SIZE, rxmsg,
1729 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1731 lnet_finalize (ni, lntmsg, 0);
1734 case IBLND_MSG_PUT_REQ:
1736 lnet_finalize(ni, lntmsg, 0);
1737 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1738 rxmsg->ibm_u.putreq.ibprm_cookie);
1742 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1744 CERROR("Can't allocate tx for %s\n",
1745 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1746 /* Not replying will break the connection */
1753 rc = kiblnd_setup_rd_iov(ni, tx,
1754 &txmsg->ibm_u.putack.ibpam_rd,
1755 niov, iov, offset, mlen);
1757 rc = kiblnd_setup_rd_kiov(ni, tx,
1758 &txmsg->ibm_u.putack.ibpam_rd,
1759 niov, kiov, offset, mlen);
1761 CERROR("Can't setup PUT sink for %s: %d\n",
1762 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1763 kiblnd_tx_done(ni, tx);
1764 /* tell peer it's over */
1765 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1766 rxmsg->ibm_u.putreq.ibprm_cookie);
1770 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1771 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1772 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1774 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1776 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1777 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1778 kiblnd_queue_tx(tx, conn);
1780 /* reposted buffer reserved for PUT_DONE */
1781 post_credit = IBLND_POSTRX_NO_CREDIT;
1784 case IBLND_MSG_GET_REQ:
1785 if (lntmsg != NULL) {
1786 /* Optimized GET; RDMA lntmsg's payload */
1787 kiblnd_reply(ni, rx, lntmsg);
1789 /* GET didn't match anything */
1790 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1792 rxmsg->ibm_u.get.ibgm_cookie);
1797 kiblnd_post_rx(rx, post_credit);
1802 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1804 long pid = cfs_create_thread (fn, arg, 0);
1809 cfs_atomic_inc (&kiblnd_data.kib_nthreads);
1814 kiblnd_thread_fini (void)
1816 cfs_atomic_dec (&kiblnd_data.kib_nthreads);
1820 kiblnd_peer_alive (kib_peer_t *peer)
1822 /* This is racy, but everyone's only writing cfs_time_current() */
1823 peer->ibp_last_alive = cfs_time_current();
1828 kiblnd_peer_notify (kib_peer_t *peer)
1831 cfs_time_t last_alive = 0;
1832 unsigned long flags;
1834 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1836 if (cfs_list_empty(&peer->ibp_conns) &&
1837 peer->ibp_accepting == 0 &&
1838 peer->ibp_connecting == 0 &&
1839 peer->ibp_error != 0) {
1840 error = peer->ibp_error;
1841 peer->ibp_error = 0;
1843 last_alive = peer->ibp_last_alive;
1846 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1849 lnet_notify(peer->ibp_ni,
1850 peer->ibp_nid, 0, last_alive);
1854 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1856 /* This just does the immediate housekeeping. 'error' is zero for a
1857 * normal shutdown which can happen only after the connection has been
1858 * established. If the connection is established, schedule the
1859 * connection to be finished off by the connd. Otherwise the connd is
1860 * already dealing with it (either to set it up or tear it down).
1861 * Caller holds kib_global_lock exclusively in irq context */
1862 kib_peer_t *peer = conn->ibc_peer;
1864 unsigned long flags;
1866 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1868 if (error != 0 && conn->ibc_comms_error == 0)
1869 conn->ibc_comms_error = error;
1871 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1872 return; /* already being handled */
1875 cfs_list_empty(&conn->ibc_tx_noops) &&
1876 cfs_list_empty(&conn->ibc_tx_queue) &&
1877 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) &&
1878 cfs_list_empty(&conn->ibc_tx_queue_nocred) &&
1879 cfs_list_empty(&conn->ibc_active_txs)) {
1880 CDEBUG(D_NET, "closing conn to %s\n",
1881 libcfs_nid2str(peer->ibp_nid));
1883 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1884 libcfs_nid2str(peer->ibp_nid), error,
1885 cfs_list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1886 cfs_list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1887 cfs_list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1888 cfs_list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1889 cfs_list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1892 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1893 cfs_list_del(&conn->ibc_list);
1894 /* connd (see below) takes over ibc_list's ref */
1896 if (cfs_list_empty (&peer->ibp_conns) && /* no more conns */
1897 kiblnd_peer_active(peer)) { /* still in peer table */
1898 kiblnd_unlink_peer_locked(peer);
1900 /* set/clear error on last conn */
1901 peer->ibp_error = conn->ibc_comms_error;
1904 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1907 kiblnd_dev_can_failover(dev)) {
1908 cfs_list_add_tail(&dev->ibd_fail_list,
1909 &kiblnd_data.kib_failed_devs);
1910 cfs_waitq_signal(&kiblnd_data.kib_failover_waitq);
1913 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1915 cfs_list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1916 cfs_waitq_signal(&kiblnd_data.kib_connd_waitq);
1918 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1922 kiblnd_close_conn(kib_conn_t *conn, int error)
1924 unsigned long flags;
1926 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1928 kiblnd_close_conn_locked(conn, error);
1930 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1934 kiblnd_handle_early_rxs(kib_conn_t *conn)
1936 unsigned long flags;
1939 LASSERT(!cfs_in_interrupt());
1940 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1942 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1943 while (!cfs_list_empty(&conn->ibc_early_rxs)) {
1944 rx = cfs_list_entry(conn->ibc_early_rxs.next,
1946 cfs_list_del(&rx->rx_list);
1947 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1949 kiblnd_handle_rx(rx);
1951 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1953 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1957 kiblnd_abort_txs(kib_conn_t *conn, cfs_list_t *txs)
1959 CFS_LIST_HEAD (zombies);
1964 spin_lock(&conn->ibc_lock);
1966 cfs_list_for_each_safe (tmp, nxt, txs) {
1967 tx = cfs_list_entry (tmp, kib_tx_t, tx_list);
1969 if (txs == &conn->ibc_active_txs) {
1970 LASSERT (!tx->tx_queued);
1971 LASSERT (tx->tx_waiting ||
1972 tx->tx_sending != 0);
1974 LASSERT (tx->tx_queued);
1977 tx->tx_status = -ECONNABORTED;
1980 if (tx->tx_sending == 0) {
1982 cfs_list_del (&tx->tx_list);
1983 cfs_list_add (&tx->tx_list, &zombies);
1987 spin_unlock(&conn->ibc_lock);
1989 kiblnd_txlist_done(conn->ibc_peer->ibp_ni, &zombies, -ECONNABORTED);
1993 kiblnd_finalise_conn (kib_conn_t *conn)
1995 LASSERT (!cfs_in_interrupt());
1996 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
1998 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2000 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2001 * for connections that didn't get as far as being connected, because
2002 * rdma_disconnect() does this for free. */
2003 kiblnd_abort_receives(conn);
2005 /* Complete all tx descs not waiting for sends to complete.
2006 * NB we should be safe from RDMA now that the QP has changed state */
2008 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2009 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2010 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2011 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2012 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2014 kiblnd_handle_early_rxs(conn);
2018 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
2020 CFS_LIST_HEAD (zombies);
2021 unsigned long flags;
2023 LASSERT (error != 0);
2024 LASSERT (!cfs_in_interrupt());
2026 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2029 LASSERT (peer->ibp_connecting > 0);
2030 peer->ibp_connecting--;
2032 LASSERT (peer->ibp_accepting > 0);
2033 peer->ibp_accepting--;
2036 if (peer->ibp_connecting != 0 ||
2037 peer->ibp_accepting != 0) {
2038 /* another connection attempt under way... */
2039 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2044 if (cfs_list_empty(&peer->ibp_conns)) {
2045 /* Take peer's blocked transmits to complete with error */
2046 cfs_list_add(&zombies, &peer->ibp_tx_queue);
2047 cfs_list_del_init(&peer->ibp_tx_queue);
2049 if (kiblnd_peer_active(peer))
2050 kiblnd_unlink_peer_locked(peer);
2052 peer->ibp_error = error;
2054 /* Can't have blocked transmits if there are connections */
2055 LASSERT (cfs_list_empty(&peer->ibp_tx_queue));
2058 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2060 kiblnd_peer_notify(peer);
2062 if (cfs_list_empty (&zombies))
2065 CNETERR("Deleting messages for %s: connection failed\n",
2066 libcfs_nid2str(peer->ibp_nid));
2068 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2072 kiblnd_connreq_done(kib_conn_t *conn, int status)
2074 kib_peer_t *peer = conn->ibc_peer;
2077 unsigned long flags;
2080 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2082 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2083 libcfs_nid2str(peer->ibp_nid), active,
2084 conn->ibc_version, status);
2086 LASSERT (!cfs_in_interrupt());
2087 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2088 peer->ibp_connecting > 0) ||
2089 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2090 peer->ibp_accepting > 0));
2092 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2093 conn->ibc_connvars = NULL;
2096 /* failed to establish connection */
2097 kiblnd_peer_connect_failed(peer, active, status);
2098 kiblnd_finalise_conn(conn);
2102 /* connection established */
2103 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2105 conn->ibc_last_send = jiffies;
2106 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2107 kiblnd_peer_alive(peer);
2109 /* Add conn to peer's list and nuke any dangling conns from a different
2110 * peer instance... */
2111 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2112 cfs_list_add(&conn->ibc_list, &peer->ibp_conns);
2114 peer->ibp_connecting--;
2116 peer->ibp_accepting--;
2118 if (peer->ibp_version == 0) {
2119 peer->ibp_version = conn->ibc_version;
2120 peer->ibp_incarnation = conn->ibc_incarnation;
2123 if (peer->ibp_version != conn->ibc_version ||
2124 peer->ibp_incarnation != conn->ibc_incarnation) {
2125 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2126 conn->ibc_incarnation);
2127 peer->ibp_version = conn->ibc_version;
2128 peer->ibp_incarnation = conn->ibc_incarnation;
2131 /* grab pending txs while I have the lock */
2132 cfs_list_add(&txs, &peer->ibp_tx_queue);
2133 cfs_list_del_init(&peer->ibp_tx_queue);
2135 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2136 conn->ibc_comms_error != 0) { /* error has happened already */
2137 lnet_ni_t *ni = peer->ibp_ni;
2139 /* start to shut down connection */
2140 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2141 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2143 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2148 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2150 /* Schedule blocked txs */
2151 spin_lock(&conn->ibc_lock);
2152 while (!cfs_list_empty(&txs)) {
2153 tx = cfs_list_entry(txs.next, kib_tx_t, tx_list);
2154 cfs_list_del(&tx->tx_list);
2156 kiblnd_queue_tx_locked(tx, conn);
2158 spin_unlock(&conn->ibc_lock);
2160 kiblnd_check_sends(conn);
2162 /* schedule blocked rxs */
2163 kiblnd_handle_early_rxs(conn);
2167 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2171 rc = rdma_reject(cmid, rej, sizeof(*rej));
2174 CWARN("Error %d sending reject\n", rc);
2178 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2180 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2181 kib_msg_t *reqmsg = priv;
2187 lnet_ni_t *ni = NULL;
2188 kib_net_t *net = NULL;
2190 struct rdma_conn_param cp;
2192 int version = IBLND_MSG_VERSION;
2193 unsigned long flags;
2195 struct sockaddr_in *peer_addr;
2196 LASSERT (!cfs_in_interrupt());
2198 /* cmid inherits 'context' from the corresponding listener id */
2199 ibdev = (kib_dev_t *)cmid->context;
2200 LASSERT (ibdev != NULL);
2202 memset(&rej, 0, sizeof(rej));
2203 rej.ibr_magic = IBLND_MSG_MAGIC;
2204 rej.ibr_why = IBLND_REJECT_FATAL;
2205 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2207 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2208 if (*kiblnd_tunables.kib_require_priv_port &&
2209 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2210 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2211 CERROR("Peer's port (%u.%u.%u.%u:%hu) is not privileged\n",
2212 HIPQUAD(ip), ntohs(peer_addr->sin_port));
2216 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2217 CERROR("Short connection request\n");
2221 /* Future protocol version compatibility support! If the
2222 * o2iblnd-specific protocol changes, or when LNET unifies
2223 * protocols over all LNDs, the initial connection will
2224 * negotiate a protocol version. I trap this here to avoid
2225 * console errors; the reject tells the peer which protocol I
2227 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2228 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2230 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2231 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2232 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2234 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2235 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2236 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2239 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2241 CERROR("Can't parse connection request: %d\n", rc);
2245 nid = reqmsg->ibm_srcnid;
2246 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2249 net = (kib_net_t *)ni->ni_data;
2250 rej.ibr_incarnation = net->ibn_incarnation;
2253 if (ni == NULL || /* no matching net */
2254 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2255 net->ibn_dev != ibdev) { /* wrong device */
2256 CERROR("Can't accept %s on %s (%s:%d:%u.%u.%u.%u): "
2257 "bad dst nid %s\n", libcfs_nid2str(nid),
2258 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2259 ibdev->ibd_ifname, ibdev->ibd_nnets,
2260 HIPQUAD(ibdev->ibd_ifip),
2261 libcfs_nid2str(reqmsg->ibm_dstnid));
2266 /* check time stamp as soon as possible */
2267 if (reqmsg->ibm_dststamp != 0 &&
2268 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2269 CWARN("Stale connection request\n");
2270 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2274 /* I can accept peer's version */
2275 version = reqmsg->ibm_version;
2277 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2278 CERROR("Unexpected connreq msg type: %x from %s\n",
2279 reqmsg->ibm_type, libcfs_nid2str(nid));
2283 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2284 IBLND_MSG_QUEUE_SIZE(version)) {
2285 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2286 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2287 IBLND_MSG_QUEUE_SIZE(version));
2289 if (version == IBLND_MSG_VERSION)
2290 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2295 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2296 IBLND_RDMA_FRAGS(version)) {
2297 CERROR("Can't accept %s(version %x): "
2298 "incompatible max_frags %d (%d wanted)\n",
2299 libcfs_nid2str(nid), version,
2300 reqmsg->ibm_u.connparams.ibcp_max_frags,
2301 IBLND_RDMA_FRAGS(version));
2303 if (version == IBLND_MSG_VERSION)
2304 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2310 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2311 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2312 libcfs_nid2str(nid),
2313 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2318 /* assume 'nid' is a new peer; create */
2319 rc = kiblnd_create_peer(ni, &peer, nid);
2321 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2322 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2326 write_lock_irqsave(g_lock, flags);
2328 peer2 = kiblnd_find_peer_locked(nid);
2329 if (peer2 != NULL) {
2330 if (peer2->ibp_version == 0) {
2331 peer2->ibp_version = version;
2332 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2335 /* not the guy I've talked with */
2336 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2337 peer2->ibp_version != version) {
2338 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2339 write_unlock_irqrestore(g_lock, flags);
2341 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2342 libcfs_nid2str(nid), peer2->ibp_version, version);
2344 kiblnd_peer_decref(peer);
2345 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2349 /* tie-break connection race in favour of the higher NID */
2350 if (peer2->ibp_connecting != 0 &&
2352 write_unlock_irqrestore(g_lock, flags);
2354 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2356 kiblnd_peer_decref(peer);
2357 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2361 peer2->ibp_accepting++;
2362 kiblnd_peer_addref(peer2);
2364 write_unlock_irqrestore(g_lock, flags);
2365 kiblnd_peer_decref(peer);
2368 /* Brand new peer */
2369 LASSERT (peer->ibp_accepting == 0);
2370 LASSERT (peer->ibp_version == 0 &&
2371 peer->ibp_incarnation == 0);
2373 peer->ibp_accepting = 1;
2374 peer->ibp_version = version;
2375 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2377 /* I have a ref on ni that prevents it being shutdown */
2378 LASSERT (net->ibn_shutdown == 0);
2380 kiblnd_peer_addref(peer);
2381 cfs_list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2383 write_unlock_irqrestore(g_lock, flags);
2386 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2388 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2389 kiblnd_peer_decref(peer);
2390 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2394 /* conn now "owns" cmid, so I return success from here on to ensure the
2395 * CM callback doesn't destroy cmid. */
2397 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2398 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2399 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2400 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2401 <= IBLND_RX_MSGS(version));
2403 ackmsg = &conn->ibc_connvars->cv_msg;
2404 memset(ackmsg, 0, sizeof(*ackmsg));
2406 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2407 sizeof(ackmsg->ibm_u.connparams));
2408 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2409 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2410 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2412 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2414 memset(&cp, 0, sizeof(cp));
2415 cp.private_data = ackmsg;
2416 cp.private_data_len = ackmsg->ibm_nob;
2417 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2418 cp.initiator_depth = 0;
2419 cp.flow_control = 1;
2420 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2421 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2423 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2425 rc = rdma_accept(cmid, &cp);
2427 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2428 rej.ibr_version = version;
2429 rej.ibr_why = IBLND_REJECT_FATAL;
2431 kiblnd_reject(cmid, &rej);
2432 kiblnd_connreq_done(conn, rc);
2433 kiblnd_conn_decref(conn);
2443 rej.ibr_version = version;
2444 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2445 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2446 kiblnd_reject(cmid, &rej);
2448 return -ECONNREFUSED;
2452 kiblnd_reconnect (kib_conn_t *conn, int version,
2453 __u64 incarnation, int why, kib_connparams_t *cp)
2455 kib_peer_t *peer = conn->ibc_peer;
2458 unsigned long flags;
2460 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2461 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2463 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2465 /* retry connection if it's still needed and no other connection
2466 * attempts (active or passive) are in progress
2467 * NB: reconnect is still needed even when ibp_tx_queue is
2468 * empty if ibp_version != version because reconnect may be
2469 * initiated by kiblnd_query() */
2470 if ((!cfs_list_empty(&peer->ibp_tx_queue) ||
2471 peer->ibp_version != version) &&
2472 peer->ibp_connecting == 1 &&
2473 peer->ibp_accepting == 0) {
2475 peer->ibp_connecting++;
2477 peer->ibp_version = version;
2478 peer->ibp_incarnation = incarnation;
2481 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2491 case IBLND_REJECT_CONN_STALE:
2495 case IBLND_REJECT_CONN_RACE:
2496 reason = "conn race";
2499 case IBLND_REJECT_CONN_UNCOMPAT:
2500 reason = "version negotiation";
2504 CNETERR("%s: retrying (%s), %x, %x, "
2505 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2506 libcfs_nid2str(peer->ibp_nid),
2507 reason, IBLND_MSG_VERSION, version,
2508 cp != NULL? cp->ibcp_queue_depth :IBLND_MSG_QUEUE_SIZE(version),
2509 cp != NULL? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2510 cp != NULL? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2512 kiblnd_connect_peer(peer);
2516 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2518 kib_peer_t *peer = conn->ibc_peer;
2520 LASSERT (!cfs_in_interrupt());
2521 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2524 case IB_CM_REJ_STALE_CONN:
2525 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2526 IBLND_REJECT_CONN_STALE, NULL);
2529 case IB_CM_REJ_INVALID_SERVICE_ID:
2530 CNETERR("%s rejected: no listener at %d\n",
2531 libcfs_nid2str(peer->ibp_nid),
2532 *kiblnd_tunables.kib_service);
2535 case IB_CM_REJ_CONSUMER_DEFINED:
2536 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2537 kib_rej_t *rej = priv;
2538 kib_connparams_t *cp = NULL;
2540 __u64 incarnation = -1;
2542 /* NB. default incarnation is -1 because:
2543 * a) V1 will ignore dst incarnation in connreq.
2544 * b) V2 will provide incarnation while rejecting me,
2545 * -1 will be overwrote.
2547 * if I try to connect to a V1 peer with V2 protocol,
2548 * it rejected me then upgrade to V2, I have no idea
2549 * about the upgrading and try to reconnect with V1,
2550 * in this case upgraded V2 can find out I'm trying to
2551 * talk to the old guy and reject me(incarnation is -1).
2554 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2555 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2556 __swab32s(&rej->ibr_magic);
2557 __swab16s(&rej->ibr_version);
2561 if (priv_nob >= sizeof(kib_rej_t) &&
2562 rej->ibr_version > IBLND_MSG_VERSION_1) {
2563 /* priv_nob is always 148 in current version
2564 * of OFED, so we still need to check version.
2565 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2569 __swab64s(&rej->ibr_incarnation);
2570 __swab16s(&cp->ibcp_queue_depth);
2571 __swab16s(&cp->ibcp_max_frags);
2572 __swab32s(&cp->ibcp_max_msg_size);
2575 incarnation = rej->ibr_incarnation;
2578 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2579 rej->ibr_magic != LNET_PROTO_MAGIC) {
2580 CERROR("%s rejected: consumer defined fatal error\n",
2581 libcfs_nid2str(peer->ibp_nid));
2585 if (rej->ibr_version != IBLND_MSG_VERSION &&
2586 rej->ibr_version != IBLND_MSG_VERSION_1) {
2587 CERROR("%s rejected: o2iblnd version %x error\n",
2588 libcfs_nid2str(peer->ibp_nid),
2593 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2594 rej->ibr_version == IBLND_MSG_VERSION_1) {
2595 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2596 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2598 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2599 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2602 switch (rej->ibr_why) {
2603 case IBLND_REJECT_CONN_RACE:
2604 case IBLND_REJECT_CONN_STALE:
2605 case IBLND_REJECT_CONN_UNCOMPAT:
2606 kiblnd_reconnect(conn, rej->ibr_version,
2607 incarnation, rej->ibr_why, cp);
2610 case IBLND_REJECT_MSG_QUEUE_SIZE:
2611 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2612 libcfs_nid2str(peer->ibp_nid), cp->ibcp_queue_depth,
2613 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2616 case IBLND_REJECT_RDMA_FRAGS:
2617 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2618 libcfs_nid2str(peer->ibp_nid), cp->ibcp_max_frags,
2619 IBLND_RDMA_FRAGS(conn->ibc_version));
2622 case IBLND_REJECT_NO_RESOURCES:
2623 CERROR("%s rejected: o2iblnd no resources\n",
2624 libcfs_nid2str(peer->ibp_nid));
2627 case IBLND_REJECT_FATAL:
2628 CERROR("%s rejected: o2iblnd fatal error\n",
2629 libcfs_nid2str(peer->ibp_nid));
2633 CERROR("%s rejected: o2iblnd reason %d\n",
2634 libcfs_nid2str(peer->ibp_nid),
2642 CNETERR("%s rejected: reason %d, size %d\n",
2643 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2647 kiblnd_connreq_done(conn, -ECONNREFUSED);
2651 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2653 kib_peer_t *peer = conn->ibc_peer;
2654 lnet_ni_t *ni = peer->ibp_ni;
2655 kib_net_t *net = ni->ni_data;
2656 kib_msg_t *msg = priv;
2657 int ver = conn->ibc_version;
2658 int rc = kiblnd_unpack_msg(msg, priv_nob);
2659 unsigned long flags;
2661 LASSERT (net != NULL);
2664 CERROR("Can't unpack connack from %s: %d\n",
2665 libcfs_nid2str(peer->ibp_nid), rc);
2669 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2670 CERROR("Unexpected message %d from %s\n",
2671 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2676 if (ver != msg->ibm_version) {
2677 CERROR("%s replied version %x is different with "
2678 "requested version %x\n",
2679 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2684 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2685 IBLND_MSG_QUEUE_SIZE(ver)) {
2686 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2687 libcfs_nid2str(peer->ibp_nid),
2688 msg->ibm_u.connparams.ibcp_queue_depth,
2689 IBLND_MSG_QUEUE_SIZE(ver));
2694 if (msg->ibm_u.connparams.ibcp_max_frags !=
2695 IBLND_RDMA_FRAGS(ver)) {
2696 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2697 libcfs_nid2str(peer->ibp_nid),
2698 msg->ibm_u.connparams.ibcp_max_frags,
2699 IBLND_RDMA_FRAGS(ver));
2704 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2705 CERROR("%s max message size %d too big (%d max)\n",
2706 libcfs_nid2str(peer->ibp_nid),
2707 msg->ibm_u.connparams.ibcp_max_msg_size,
2713 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2714 if (msg->ibm_dstnid == ni->ni_nid &&
2715 msg->ibm_dststamp == net->ibn_incarnation)
2719 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2722 CERROR("Bad connection reply from %s, rc = %d, "
2723 "version: %x max_frags: %d\n",
2724 libcfs_nid2str(peer->ibp_nid), rc,
2725 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2729 conn->ibc_incarnation = msg->ibm_srcstamp;
2731 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2732 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2733 <= IBLND_RX_MSGS(ver));
2735 kiblnd_connreq_done(conn, 0);
2739 /* NB My QP has already established itself, so I handle anything going
2740 * wrong here by setting ibc_comms_error.
2741 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2742 * immediately tears it down. */
2745 conn->ibc_comms_error = rc;
2746 kiblnd_connreq_done(conn, 0);
2750 kiblnd_active_connect (struct rdma_cm_id *cmid)
2752 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2755 struct rdma_conn_param cp;
2758 unsigned long flags;
2761 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2763 incarnation = peer->ibp_incarnation;
2764 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION :
2767 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2769 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2771 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2772 kiblnd_peer_decref(peer); /* lose cmid's ref */
2776 /* conn "owns" cmid now, so I return success from here on to ensure the
2777 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2780 msg = &conn->ibc_connvars->cv_msg;
2782 memset(msg, 0, sizeof(*msg));
2783 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2784 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2785 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2786 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2788 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2789 0, peer->ibp_nid, incarnation);
2791 memset(&cp, 0, sizeof(cp));
2792 cp.private_data = msg;
2793 cp.private_data_len = msg->ibm_nob;
2794 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2795 cp.initiator_depth = 0;
2796 cp.flow_control = 1;
2797 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2798 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2800 LASSERT(cmid->context == (void *)conn);
2801 LASSERT(conn->ibc_cmid == cmid);
2803 rc = rdma_connect(cmid, &cp);
2805 CERROR("Can't connect to %s: %d\n",
2806 libcfs_nid2str(peer->ibp_nid), rc);
2807 kiblnd_connreq_done(conn, rc);
2808 kiblnd_conn_decref(conn);
2815 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2821 switch (event->event) {
2823 CERROR("Unexpected event: %d, status: %d\n",
2824 event->event, event->status);
2827 case RDMA_CM_EVENT_CONNECT_REQUEST:
2828 /* destroy cmid on failure */
2829 rc = kiblnd_passive_connect(cmid,
2830 (void *)KIBLND_CONN_PARAM(event),
2831 KIBLND_CONN_PARAM_LEN(event));
2832 CDEBUG(D_NET, "connreq: %d\n", rc);
2835 case RDMA_CM_EVENT_ADDR_ERROR:
2836 peer = (kib_peer_t *)cmid->context;
2837 CNETERR("%s: ADDR ERROR %d\n",
2838 libcfs_nid2str(peer->ibp_nid), event->status);
2839 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2840 kiblnd_peer_decref(peer);
2841 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2843 case RDMA_CM_EVENT_ADDR_RESOLVED:
2844 peer = (kib_peer_t *)cmid->context;
2846 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2847 libcfs_nid2str(peer->ibp_nid), event->status);
2849 if (event->status != 0) {
2850 CNETERR("Can't resolve address for %s: %d\n",
2851 libcfs_nid2str(peer->ibp_nid), event->status);
2854 rc = rdma_resolve_route(
2855 cmid, *kiblnd_tunables.kib_timeout * 1000);
2858 /* Can't initiate route resolution */
2859 CERROR("Can't resolve route for %s: %d\n",
2860 libcfs_nid2str(peer->ibp_nid), rc);
2862 kiblnd_peer_connect_failed(peer, 1, rc);
2863 kiblnd_peer_decref(peer);
2864 return rc; /* rc != 0 destroys cmid */
2866 case RDMA_CM_EVENT_ROUTE_ERROR:
2867 peer = (kib_peer_t *)cmid->context;
2868 CNETERR("%s: ROUTE ERROR %d\n",
2869 libcfs_nid2str(peer->ibp_nid), event->status);
2870 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2871 kiblnd_peer_decref(peer);
2872 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2874 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2875 peer = (kib_peer_t *)cmid->context;
2876 CDEBUG(D_NET,"%s Route resolved: %d\n",
2877 libcfs_nid2str(peer->ibp_nid), event->status);
2879 if (event->status == 0)
2880 return kiblnd_active_connect(cmid);
2882 CNETERR("Can't resolve route for %s: %d\n",
2883 libcfs_nid2str(peer->ibp_nid), event->status);
2884 kiblnd_peer_connect_failed(peer, 1, event->status);
2885 kiblnd_peer_decref(peer);
2886 return event->status; /* rc != 0 destroys cmid */
2888 case RDMA_CM_EVENT_UNREACHABLE:
2889 conn = (kib_conn_t *)cmid->context;
2890 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2891 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2892 CNETERR("%s: UNREACHABLE %d\n",
2893 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2894 kiblnd_connreq_done(conn, -ENETDOWN);
2895 kiblnd_conn_decref(conn);
2898 case RDMA_CM_EVENT_CONNECT_ERROR:
2899 conn = (kib_conn_t *)cmid->context;
2900 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2901 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2902 CNETERR("%s: CONNECT ERROR %d\n",
2903 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2904 kiblnd_connreq_done(conn, -ENOTCONN);
2905 kiblnd_conn_decref(conn);
2908 case RDMA_CM_EVENT_REJECTED:
2909 conn = (kib_conn_t *)cmid->context;
2910 switch (conn->ibc_state) {
2914 case IBLND_CONN_PASSIVE_WAIT:
2915 CERROR ("%s: REJECTED %d\n",
2916 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2918 kiblnd_connreq_done(conn, -ECONNRESET);
2921 case IBLND_CONN_ACTIVE_CONNECT:
2922 kiblnd_rejected(conn, event->status,
2923 (void *)KIBLND_CONN_PARAM(event),
2924 KIBLND_CONN_PARAM_LEN(event));
2927 kiblnd_conn_decref(conn);
2930 case RDMA_CM_EVENT_ESTABLISHED:
2931 conn = (kib_conn_t *)cmid->context;
2932 switch (conn->ibc_state) {
2936 case IBLND_CONN_PASSIVE_WAIT:
2937 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2938 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2939 kiblnd_connreq_done(conn, 0);
2942 case IBLND_CONN_ACTIVE_CONNECT:
2943 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2944 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2945 kiblnd_check_connreply(conn,
2946 (void *)KIBLND_CONN_PARAM(event),
2947 KIBLND_CONN_PARAM_LEN(event));
2950 /* net keeps its ref on conn! */
2953 #ifdef HAVE_OFED_RDMA_CMEV_TIMEWAIT_EXIT
2954 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2955 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2958 case RDMA_CM_EVENT_DISCONNECTED:
2959 conn = (kib_conn_t *)cmid->context;
2960 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2961 CERROR("%s DISCONNECTED\n",
2962 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2963 kiblnd_connreq_done(conn, -ECONNRESET);
2965 kiblnd_close_conn(conn, 0);
2967 kiblnd_conn_decref(conn);
2968 cmid->context = NULL;
2971 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2972 LCONSOLE_ERROR_MSG(0x131,
2973 "Received notification of device removal\n"
2974 "Please shutdown LNET to allow this to proceed\n");
2975 /* Can't remove network from underneath LNET for now, so I have
2979 #ifdef HAVE_OFED_RDMA_CMEV_ADDRCHANGE
2980 case RDMA_CM_EVENT_ADDR_CHANGE:
2981 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2988 kiblnd_check_txs_locked(kib_conn_t *conn, cfs_list_t *txs)
2993 cfs_list_for_each (ttmp, txs) {
2994 tx = cfs_list_entry (ttmp, kib_tx_t, tx_list);
2996 if (txs != &conn->ibc_active_txs) {
2997 LASSERT (tx->tx_queued);
2999 LASSERT (!tx->tx_queued);
3000 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
3003 if (cfs_time_aftereq (jiffies, tx->tx_deadline)) {
3004 CERROR("Timed out tx: %s, %lu seconds\n",
3005 kiblnd_queue2str(conn, txs),
3006 cfs_duration_sec(jiffies - tx->tx_deadline));
3015 kiblnd_conn_timed_out_locked(kib_conn_t *conn)
3017 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3018 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3019 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3020 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3021 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3025 kiblnd_check_conns (int idx)
3027 CFS_LIST_HEAD (closes);
3028 CFS_LIST_HEAD (checksends);
3029 cfs_list_t *peers = &kiblnd_data.kib_peers[idx];
3034 unsigned long flags;
3036 /* NB. We expect to have a look at all the peers and not find any
3037 * RDMAs to time out, so we just use a shared lock while we
3039 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3041 cfs_list_for_each (ptmp, peers) {
3042 peer = cfs_list_entry (ptmp, kib_peer_t, ibp_list);
3044 cfs_list_for_each (ctmp, &peer->ibp_conns) {
3048 conn = cfs_list_entry(ctmp, kib_conn_t, ibc_list);
3050 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
3052 spin_lock(&conn->ibc_lock);
3054 sendnoop = kiblnd_need_noop(conn);
3055 timedout = kiblnd_conn_timed_out_locked(conn);
3056 if (!sendnoop && !timedout) {
3057 spin_unlock(&conn->ibc_lock);
3062 CERROR("Timed out RDMA with %s (%lu): "
3063 "c: %u, oc: %u, rc: %u\n",
3064 libcfs_nid2str(peer->ibp_nid),
3065 cfs_duration_sec(cfs_time_current() -
3066 peer->ibp_last_alive),
3068 conn->ibc_outstanding_credits,
3069 conn->ibc_reserved_credits);
3070 cfs_list_add(&conn->ibc_connd_list, &closes);
3072 cfs_list_add(&conn->ibc_connd_list,
3075 /* +ref for 'closes' or 'checksends' */
3076 kiblnd_conn_addref(conn);
3078 spin_unlock(&conn->ibc_lock);
3082 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3084 /* Handle timeout by closing the whole
3085 * connection. We can only be sure RDMA activity
3086 * has ceased once the QP has been modified. */
3087 while (!cfs_list_empty(&closes)) {
3088 conn = cfs_list_entry(closes.next,
3089 kib_conn_t, ibc_connd_list);
3090 cfs_list_del(&conn->ibc_connd_list);
3091 kiblnd_close_conn(conn, -ETIMEDOUT);
3092 kiblnd_conn_decref(conn);
3095 /* In case we have enough credits to return via a
3096 * NOOP, but there were no non-blocking tx descs
3097 * free to do it last time... */
3098 while (!cfs_list_empty(&checksends)) {
3099 conn = cfs_list_entry(checksends.next,
3100 kib_conn_t, ibc_connd_list);
3101 cfs_list_del(&conn->ibc_connd_list);
3102 kiblnd_check_sends(conn);
3103 kiblnd_conn_decref(conn);
3108 kiblnd_disconnect_conn (kib_conn_t *conn)
3110 LASSERT (!cfs_in_interrupt());
3111 LASSERT (current == kiblnd_data.kib_connd);
3112 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3114 rdma_disconnect(conn->ibc_cmid);
3115 kiblnd_finalise_conn(conn);
3117 kiblnd_peer_notify(conn->ibc_peer);
3121 kiblnd_connd (void *arg)
3123 cfs_waitlink_t wait;
3124 unsigned long flags;
3130 unsigned long deadline = jiffies;
3132 cfs_daemonize ("kiblnd_connd");
3133 cfs_block_allsigs ();
3135 cfs_waitlink_init (&wait);
3136 kiblnd_data.kib_connd = current;
3138 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3140 while (!kiblnd_data.kib_shutdown) {
3144 if (!cfs_list_empty (&kiblnd_data.kib_connd_zombies)) {
3145 conn = cfs_list_entry(kiblnd_data. \
3146 kib_connd_zombies.next,
3147 kib_conn_t, ibc_list);
3148 cfs_list_del(&conn->ibc_list);
3150 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3154 kiblnd_destroy_conn(conn);
3156 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3159 if (!cfs_list_empty(&kiblnd_data.kib_connd_conns)) {
3160 conn = cfs_list_entry(kiblnd_data.kib_connd_conns.next,
3161 kib_conn_t, ibc_list);
3162 cfs_list_del(&conn->ibc_list);
3164 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3168 kiblnd_disconnect_conn(conn);
3169 kiblnd_conn_decref(conn);
3171 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3174 /* careful with the jiffy wrap... */
3175 timeout = (int)(deadline - jiffies);
3179 int chunk = kiblnd_data.kib_peer_hash_size;
3181 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3184 /* Time to check for RDMA timeouts on a few more
3185 * peers: I do checks every 'p' seconds on a
3186 * proportion of the peer table and I need to check
3187 * every connection 'n' times within a timeout
3188 * interval, to ensure I detect a timeout on any
3189 * connection within (n+1)/n times the timeout
3192 if (*kiblnd_tunables.kib_timeout > n * p)
3193 chunk = (chunk * n * p) /
3194 *kiblnd_tunables.kib_timeout;
3198 for (i = 0; i < chunk; i++) {
3199 kiblnd_check_conns(peer_index);
3200 peer_index = (peer_index + 1) %
3201 kiblnd_data.kib_peer_hash_size;
3204 deadline += p * CFS_HZ;
3205 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3211 /* Nothing to do for 'timeout' */
3212 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3213 cfs_waitq_add(&kiblnd_data.kib_connd_waitq, &wait);
3214 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3216 cfs_waitq_timedwait(&wait, CFS_TASK_INTERRUPTIBLE, timeout);
3218 cfs_set_current_state(CFS_TASK_RUNNING);
3219 cfs_waitq_del(&kiblnd_data.kib_connd_waitq, &wait);
3220 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3223 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3225 kiblnd_thread_fini();
3230 kiblnd_qp_event(struct ib_event *event, void *arg)
3232 kib_conn_t *conn = arg;
3234 switch (event->event) {
3235 case IB_EVENT_COMM_EST:
3236 CDEBUG(D_NET, "%s established\n",
3237 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3241 CERROR("%s: Async QP event type %d\n",
3242 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3248 kiblnd_complete (struct ib_wc *wc)
3250 switch (kiblnd_wreqid2type(wc->wr_id)) {
3254 case IBLND_WID_RDMA:
3255 /* We only get RDMA completion notification if it fails. All
3256 * subsequent work items, including the final SEND will fail
3257 * too. However we can't print out any more info about the
3258 * failing RDMA because 'tx' might be back on the idle list or
3259 * even reused already if we didn't manage to post all our work
3261 CNETERR("RDMA (tx: %p) failed: %d\n",
3262 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3266 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3270 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3277 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3279 /* NB I'm not allowed to schedule this conn once its refcount has
3280 * reached 0. Since fundamentally I'm racing with scheduler threads
3281 * consuming my CQ I could be called after all completions have
3282 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3283 * and this CQ is about to be destroyed so I NOOP. */
3284 kib_conn_t *conn = (kib_conn_t *)arg;
3285 struct kib_sched_info *sched = conn->ibc_sched;
3286 unsigned long flags;
3288 LASSERT(cq == conn->ibc_cq);
3290 spin_lock_irqsave(&sched->ibs_lock, flags);
3292 conn->ibc_ready = 1;
3294 if (!conn->ibc_scheduled &&
3295 (conn->ibc_nrx > 0 ||
3296 conn->ibc_nsends_posted > 0)) {
3297 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3298 conn->ibc_scheduled = 1;
3299 cfs_list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3301 if (cfs_waitq_active(&sched->ibs_waitq))
3302 cfs_waitq_signal(&sched->ibs_waitq);
3305 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3309 kiblnd_cq_event(struct ib_event *event, void *arg)
3311 kib_conn_t *conn = arg;
3313 CERROR("%s: async CQ event type %d\n",
3314 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3318 kiblnd_scheduler(void *arg)
3320 long id = (long)arg;
3321 struct kib_sched_info *sched;
3323 cfs_waitlink_t wait;
3324 unsigned long flags;
3331 snprintf(name, sizeof(name), "kiblnd_sd_%02ld_%02ld",
3332 KIB_THREAD_CPT(id), KIB_THREAD_TID(id));
3334 cfs_daemonize(name);
3335 cfs_block_allsigs();
3337 cfs_waitlink_init(&wait);
3339 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3341 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3343 CWARN("Failed to bind %s on CPT %d, please verify whether "
3344 "all CPUs are healthy and reload modules if necessary, "
3345 "otherwise your system might under risk of low "
3346 "performance\n", name, sched->ibs_cpt);
3349 spin_lock_irqsave(&sched->ibs_lock, flags);
3351 while (!kiblnd_data.kib_shutdown) {
3352 if (busy_loops++ >= IBLND_RESCHED) {
3353 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3358 spin_lock_irqsave(&sched->ibs_lock, flags);
3363 if (!cfs_list_empty(&sched->ibs_conns)) {
3364 conn = cfs_list_entry(sched->ibs_conns.next,
3365 kib_conn_t, ibc_sched_list);
3366 /* take over kib_sched_conns' ref on conn... */
3367 LASSERT(conn->ibc_scheduled);
3368 cfs_list_del(&conn->ibc_sched_list);
3369 conn->ibc_ready = 0;
3371 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3373 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3375 rc = ib_req_notify_cq(conn->ibc_cq,
3378 CWARN("%s: ib_req_notify_cq failed: %d, "
3379 "closing connection\n",
3380 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3381 kiblnd_close_conn(conn, -EIO);
3382 kiblnd_conn_decref(conn);
3383 spin_lock_irqsave(&sched->ibs_lock,
3388 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3392 CWARN("%s: ib_poll_cq failed: %d, "
3393 "closing connection\n",
3394 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3396 kiblnd_close_conn(conn, -EIO);
3397 kiblnd_conn_decref(conn);
3398 spin_lock_irqsave(&sched->ibs_lock, flags);
3402 spin_lock_irqsave(&sched->ibs_lock, flags);
3404 if (rc != 0 || conn->ibc_ready) {
3405 /* There may be another completion waiting; get
3406 * another scheduler to check while I handle
3408 /* +1 ref for sched_conns */
3409 kiblnd_conn_addref(conn);
3410 cfs_list_add_tail(&conn->ibc_sched_list,
3412 if (cfs_waitq_active(&sched->ibs_waitq))
3413 cfs_waitq_signal(&sched->ibs_waitq);
3415 conn->ibc_scheduled = 0;
3419 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3420 kiblnd_complete(&wc);
3422 spin_lock_irqsave(&sched->ibs_lock, flags);
3425 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3432 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3433 cfs_waitq_add_exclusive(&sched->ibs_waitq, &wait);
3434 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3436 cfs_waitq_wait(&wait, CFS_TASK_INTERRUPTIBLE);
3439 cfs_waitq_del(&sched->ibs_waitq, &wait);
3440 cfs_set_current_state(CFS_TASK_RUNNING);
3441 spin_lock_irqsave(&sched->ibs_lock, flags);
3444 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3446 kiblnd_thread_fini();
3451 kiblnd_failover_thread(void *arg)
3453 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3455 cfs_waitlink_t wait;
3456 unsigned long flags;
3459 LASSERT (*kiblnd_tunables.kib_dev_failover != 0);
3461 cfs_daemonize ("kiblnd_failover");
3462 cfs_block_allsigs ();
3464 cfs_waitlink_init(&wait);
3465 write_lock_irqsave(glock, flags);
3467 while (!kiblnd_data.kib_shutdown) {
3468 int do_failover = 0;
3471 cfs_list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3473 if (cfs_time_before(cfs_time_current(),
3474 dev->ibd_next_failover))
3481 cfs_list_del_init(&dev->ibd_fail_list);
3482 dev->ibd_failover = 1;
3483 write_unlock_irqrestore(glock, flags);
3485 rc = kiblnd_dev_failover(dev);
3487 write_lock_irqsave(glock, flags);
3489 LASSERT (dev->ibd_failover);
3490 dev->ibd_failover = 0;
3491 if (rc >= 0) { /* Device is OK or failover succeed */
3492 dev->ibd_next_failover = cfs_time_shift(3);
3496 /* failed to failover, retry later */
3497 dev->ibd_next_failover =
3498 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3499 if (kiblnd_dev_can_failover(dev)) {
3500 cfs_list_add_tail(&dev->ibd_fail_list,
3501 &kiblnd_data.kib_failed_devs);
3507 /* long sleep if no more pending failover */
3508 long_sleep = cfs_list_empty(&kiblnd_data.kib_failed_devs);
3510 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
3511 cfs_waitq_add(&kiblnd_data.kib_failover_waitq, &wait);
3512 write_unlock_irqrestore(glock, flags);
3514 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3515 cfs_time_seconds(1));
3516 cfs_set_current_state(CFS_TASK_RUNNING);
3517 cfs_waitq_del(&kiblnd_data.kib_failover_waitq, &wait);
3518 write_lock_irqsave(glock, flags);
3520 if (!long_sleep || rc != 0)
3523 /* have a long sleep, routine check all active devices,
3524 * we need checking like this because if there is not active
3525 * connection on the dev and no SEND from local, we may listen
3526 * on wrong HCA for ever while there is a bonding failover */
3527 cfs_list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3528 if (kiblnd_dev_can_failover(dev)) {
3529 cfs_list_add_tail(&dev->ibd_fail_list,
3530 &kiblnd_data.kib_failed_devs);
3535 write_unlock_irqrestore(glock, flags);
3537 kiblnd_thread_fini();