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 2008 Sun Microsystems, Inc. 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 (!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, struct list_head *txlist, int status)
91 while (!list_empty (txlist)) {
92 tx = list_entry (txlist->next, kib_tx_t, tx_list);
94 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;
106 struct list_head *node;
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 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
135 LASSERT (conn->ibc_nrx > 0);
137 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 (!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(net, 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 spin_lock(&conn->ibc_lock);
199 if (credit == IBLND_POSTRX_PEER_CREDIT)
200 conn->ibc_outstanding_credits++;
202 conn->ibc_reserved_credits++;
203 spin_unlock(&conn->ibc_lock);
205 kiblnd_check_sends(conn);
210 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
212 struct list_head *tmp;
214 list_for_each(tmp, &conn->ibc_active_txs) {
215 kib_tx_t *tx = 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 spin_lock(&conn->ibc_lock);
243 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
245 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 list_del(&tx->tx_list);
267 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, libcfs_nid2str(conn->ibc_peer->ibp_nid));
310 /* Have I received credits that will let me send? */
311 spin_lock(&conn->ibc_lock);
313 if (conn->ibc_credits + credits >
314 IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
315 rc2 = conn->ibc_credits;
316 spin_unlock(&conn->ibc_lock);
318 CERROR("Bad credits from %s: %d + %d > %d\n",
319 libcfs_nid2str(conn->ibc_peer->ibp_nid),
320 rc2, credits, IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
322 kiblnd_close_conn(conn, -EPROTO);
323 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
327 conn->ibc_credits += credits;
329 spin_unlock(&conn->ibc_lock);
330 kiblnd_check_sends(conn);
333 switch (msg->ibm_type) {
335 CERROR("Bad IBLND message type %x from %s\n",
336 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
337 post_credit = IBLND_POSTRX_NO_CREDIT;
342 if (IBLND_OOB_CAPABLE(conn->ibc_version))
343 post_credit = IBLND_POSTRX_NO_CREDIT;
345 post_credit = IBLND_POSTRX_PEER_CREDIT;
348 case IBLND_MSG_IMMEDIATE:
349 post_credit = IBLND_POSTRX_DONT_POST;
350 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
351 msg->ibm_srcnid, rx, 0);
352 if (rc < 0) /* repost on error */
353 post_credit = IBLND_POSTRX_PEER_CREDIT;
356 case IBLND_MSG_PUT_REQ:
357 post_credit = IBLND_POSTRX_DONT_POST;
358 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
359 msg->ibm_srcnid, rx, 1);
360 if (rc < 0) /* repost on error */
361 post_credit = IBLND_POSTRX_PEER_CREDIT;
364 case IBLND_MSG_PUT_NAK:
365 CWARN ("PUT_NACK from %s\n", libcfs_nid2str(conn->ibc_peer->ibp_nid));
366 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
367 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
368 msg->ibm_u.completion.ibcm_status,
369 msg->ibm_u.completion.ibcm_cookie);
372 case IBLND_MSG_PUT_ACK:
373 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
375 spin_lock(&conn->ibc_lock);
376 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
377 msg->ibm_u.putack.ibpam_src_cookie);
379 list_del(&tx->tx_list);
380 spin_unlock(&conn->ibc_lock);
383 CERROR("Unmatched PUT_ACK from %s\n",
384 libcfs_nid2str(conn->ibc_peer->ibp_nid));
389 LASSERT (tx->tx_waiting);
390 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
391 * (a) I can overwrite tx_msg since my peer has received it!
392 * (b) tx_waiting set tells tx_complete() it's not done. */
394 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
396 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
397 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
398 &msg->ibm_u.putack.ibpam_rd,
399 msg->ibm_u.putack.ibpam_dst_cookie);
401 CERROR("Can't setup rdma for PUT to %s: %d\n",
402 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
404 spin_lock(&conn->ibc_lock);
405 tx->tx_waiting = 0; /* clear waiting and queue atomically */
406 kiblnd_queue_tx_locked(tx, conn);
407 spin_unlock(&conn->ibc_lock);
410 case IBLND_MSG_PUT_DONE:
411 post_credit = IBLND_POSTRX_PEER_CREDIT;
412 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
413 msg->ibm_u.completion.ibcm_status,
414 msg->ibm_u.completion.ibcm_cookie);
417 case IBLND_MSG_GET_REQ:
418 post_credit = IBLND_POSTRX_DONT_POST;
419 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
420 msg->ibm_srcnid, rx, 1);
421 if (rc < 0) /* repost on error */
422 post_credit = IBLND_POSTRX_PEER_CREDIT;
425 case IBLND_MSG_GET_DONE:
426 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
427 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
428 msg->ibm_u.completion.ibcm_status,
429 msg->ibm_u.completion.ibcm_cookie);
433 if (rc < 0) /* protocol error */
434 kiblnd_close_conn(conn, rc);
436 if (post_credit != IBLND_POSTRX_DONT_POST)
437 kiblnd_post_rx(rx, post_credit);
441 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
443 kib_msg_t *msg = rx->rx_msg;
444 kib_conn_t *conn = rx->rx_conn;
445 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
446 kib_net_t *net = ni->ni_data;
450 LASSERT (net != NULL);
451 LASSERT (rx->rx_nob < 0); /* was posted */
452 rx->rx_nob = 0; /* isn't now */
454 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
457 if (status != IB_WC_SUCCESS) {
458 CDEBUG(D_NETERROR, "Rx from %s failed: %d\n",
459 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
466 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
468 CERROR ("Error %d unpacking rx from %s\n",
469 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
473 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
474 msg->ibm_dstnid != ni->ni_nid ||
475 msg->ibm_srcstamp != conn->ibc_incarnation ||
476 msg->ibm_dststamp != net->ibn_incarnation) {
477 CERROR ("Stale rx from %s\n",
478 libcfs_nid2str(conn->ibc_peer->ibp_nid));
483 /* set time last known alive */
484 kiblnd_peer_alive(conn->ibc_peer);
486 /* racing with connection establishment/teardown! */
488 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
489 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
492 write_lock_irqsave(g_lock, flags);
493 /* must check holding global lock to eliminate race */
494 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
495 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
496 write_unlock_irqrestore(g_lock, flags);
499 write_unlock_irqrestore(g_lock, flags);
501 kiblnd_handle_rx(rx);
505 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
506 kiblnd_close_conn(conn, err);
508 kiblnd_drop_rx(rx); /* Don't re-post rx. */
512 kiblnd_kvaddr_to_page (unsigned long vaddr)
516 if (vaddr >= VMALLOC_START &&
517 vaddr < VMALLOC_END) {
518 page = vmalloc_to_page ((void *)vaddr);
519 LASSERT (page != NULL);
522 #ifdef CONFIG_HIGHMEM
523 if (vaddr >= PKMAP_BASE &&
524 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
525 /* No highmem pages only used for bulk (kiov) I/O */
526 CERROR("find page for address in highmem\n");
530 page = virt_to_page (vaddr);
531 LASSERT (page != NULL);
536 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
538 kib_dev_t *ibdev = net->ibn_dev;
539 __u64 *pages = tx->tx_pages;
545 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
546 for (size = 0; size < rd->rd_frags[i].rf_nob;
547 size += ibdev->ibd_page_size) {
548 pages[npages ++] = (rd->rd_frags[i].rf_addr &
549 ibdev->ibd_page_mask) + size;
553 rc = kiblnd_fmr_pool_map(&net->ibn_fmr_ps, pages, npages, 0, &tx->tx_u.fmr);
555 CERROR ("Can't map %d pages: %d\n", npages, rc);
559 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
561 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.fmr.fmr_pfmr->fmr->rkey :
562 tx->tx_u.fmr.fmr_pfmr->fmr->lkey;
563 rd->rd_frags[0].rf_addr &= ~ibdev->ibd_page_mask;
564 rd->rd_frags[0].rf_nob = nob;
571 kiblnd_pmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
576 iova = rd->rd_frags[0].rf_addr & ~net->ibn_dev->ibd_page_mask;
578 rc = kiblnd_pmr_pool_map(&net->ibn_pmr_ps, rd, &iova, &tx->tx_u.pmr);
580 CERROR("Failed to create MR by phybuf: %d\n", rc);
584 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
586 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.pmr->pmr_mr->rkey :
587 tx->tx_u.pmr->pmr_mr->lkey;
589 rd->rd_frags[0].rf_addr = iova;
590 rd->rd_frags[0].rf_nob = nob;
596 kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
598 kib_net_t *net = ni->ni_data;
600 LASSERT (net != NULL);
602 if (net->ibn_with_fmr && tx->tx_u.fmr.fmr_pfmr != NULL) {
603 kiblnd_fmr_pool_unmap(&tx->tx_u.fmr, tx->tx_status);
604 tx->tx_u.fmr.fmr_pfmr = NULL;
605 } else if (net->ibn_with_pmr && tx->tx_u.pmr != NULL) {
606 kiblnd_pmr_pool_unmap(tx->tx_u.pmr);
610 if (tx->tx_nfrags != 0) {
611 kiblnd_dma_unmap_sg(net->ibn_dev->ibd_cmid->device,
612 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
618 kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx,
619 kib_rdma_desc_t *rd, int nfrags)
621 kib_net_t *net = ni->ni_data;
622 struct ib_mr *mr = NULL;
626 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
628 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
629 tx->tx_nfrags = nfrags;
632 kiblnd_dma_map_sg(net->ibn_dev->ibd_cmid->device,
633 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
635 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
636 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
637 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
638 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
639 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
640 nob += rd->rd_frags[i].rf_nob;
643 /* looking for pre-mapping MR */
644 mr = kiblnd_find_rd_dma_mr(net, rd);
646 /* found pre-mapping MR */
647 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
651 if (net->ibn_with_fmr)
652 return kiblnd_fmr_map_tx(net, tx, rd, nob);
653 else if (net->ibn_with_pmr)
654 return kiblnd_pmr_map_tx(net, tx, rd, nob);
661 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
662 unsigned int niov, struct iovec *iov, int offset, int nob)
664 kib_net_t *net = ni->ni_data;
666 struct scatterlist *sg;
673 LASSERT (net != NULL);
675 while (offset >= iov->iov_len) {
676 offset -= iov->iov_len;
686 vaddr = ((unsigned long)iov->iov_base) + offset;
687 page_offset = vaddr & (PAGE_SIZE - 1);
688 page = kiblnd_kvaddr_to_page(vaddr);
690 CERROR ("Can't find page\n");
694 fragnob = min((int)(iov->iov_len - offset), nob);
695 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
697 sg_set_page(sg, page, fragnob, page_offset);
700 if (offset + fragnob < iov->iov_len) {
710 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
714 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
715 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
717 kib_net_t *net = ni->ni_data;
718 struct scatterlist *sg;
721 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
725 LASSERT (net != NULL);
727 while (offset >= kiov->kiov_len) {
728 offset -= kiov->kiov_len;
738 fragnob = min((int)(kiov->kiov_len - offset), nob);
740 memset(sg, 0, sizeof(*sg));
741 sg_set_page(sg, kiov->kiov_page, fragnob,
742 kiov->kiov_offset + offset);
751 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
755 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
757 kib_msg_t *msg = tx->tx_msg;
758 kib_peer_t *peer = conn->ibc_peer;
759 int ver = conn->ibc_version;
762 struct ib_send_wr *bad_wrq;
764 LASSERT (tx->tx_queued);
765 /* We rely on this for QP sizing */
766 LASSERT (tx->tx_nwrq > 0);
767 LASSERT (tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
769 LASSERT (credit == 0 || credit == 1);
770 LASSERT (conn->ibc_outstanding_credits >= 0);
771 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
772 LASSERT (conn->ibc_credits >= 0);
773 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
775 if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
776 /* tx completions outstanding... */
777 CDEBUG(D_NET, "%s: posted enough\n",
778 libcfs_nid2str(peer->ibp_nid));
782 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
783 CDEBUG(D_NET, "%s: no credits\n",
784 libcfs_nid2str(peer->ibp_nid));
788 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
789 conn->ibc_credits == 1 && /* last credit reserved for */
790 conn->ibc_outstanding_credits == 0) { /* giving back credits */
791 CDEBUG(D_NET, "%s: not using last credit\n",
792 libcfs_nid2str(peer->ibp_nid));
796 /* NB don't drop ibc_lock before bumping tx_sending */
797 list_del(&tx->tx_list);
800 if (msg->ibm_type == IBLND_MSG_NOOP &&
801 (!kiblnd_send_noop(conn) || /* redundant NOOP */
802 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
803 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
804 /* OK to drop when posted enough NOOPs, since
805 * kiblnd_check_sends will queue NOOP again when
806 * posted NOOPs complete */
807 spin_unlock(&conn->ibc_lock);
808 kiblnd_tx_done(peer->ibp_ni, tx);
809 spin_lock(&conn->ibc_lock);
810 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
811 libcfs_nid2str(peer->ibp_nid),
812 conn->ibc_noops_posted);
816 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
817 peer->ibp_nid, conn->ibc_incarnation);
819 conn->ibc_credits -= credit;
820 conn->ibc_outstanding_credits = 0;
821 conn->ibc_nsends_posted++;
822 if (msg->ibm_type == IBLND_MSG_NOOP)
823 conn->ibc_noops_posted++;
825 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
826 * PUT. If so, it was first queued here as a PUT_REQ, sent and
827 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
828 * and then re-queued here. It's (just) possible that
829 * tx_sending is non-zero if we've not done the tx_complete()
830 * from the first send; hence the ++ rather than = below. */
832 list_add(&tx->tx_list, &conn->ibc_active_txs);
834 /* I'm still holding ibc_lock! */
835 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
838 rc = ib_post_send(conn->ibc_cmid->qp,
839 tx->tx_wrq, &bad_wrq);
840 conn->ibc_last_send = jiffies;
845 /* NB credits are transferred in the actual
846 * message, which can only be the last work item */
847 conn->ibc_credits += credit;
848 conn->ibc_outstanding_credits += msg->ibm_credits;
849 conn->ibc_nsends_posted--;
850 if (msg->ibm_type == IBLND_MSG_NOOP)
851 conn->ibc_noops_posted--;
857 done = (tx->tx_sending == 0);
859 list_del(&tx->tx_list);
861 spin_unlock(&conn->ibc_lock);
863 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
864 CERROR("Error %d posting transmit to %s\n",
865 rc, libcfs_nid2str(peer->ibp_nid));
867 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
868 rc, libcfs_nid2str(peer->ibp_nid));
870 kiblnd_close_conn(conn, rc);
873 kiblnd_tx_done(peer->ibp_ni, tx);
878 kiblnd_check_sends (kib_conn_t *conn)
880 int ver = conn->ibc_version;
881 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
884 /* Don't send anything until after the connection is established */
885 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
886 CDEBUG(D_NET, "%s too soon\n",
887 libcfs_nid2str(conn->ibc_peer->ibp_nid));
891 spin_lock(&conn->ibc_lock);
893 LASSERT (conn->ibc_nsends_posted <= IBLND_CONCURRENT_SENDS(ver));
894 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
895 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
896 LASSERT (conn->ibc_reserved_credits >= 0);
898 while (conn->ibc_reserved_credits > 0 &&
899 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
900 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
902 list_del(&tx->tx_list);
903 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
904 conn->ibc_reserved_credits--;
907 if (kiblnd_send_noop(conn)) {
908 spin_unlock(&conn->ibc_lock);
910 tx = kiblnd_get_idle_tx(ni);
912 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
914 spin_lock(&conn->ibc_lock);
916 kiblnd_queue_tx_locked(tx, conn);
922 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
924 tx = list_entry(conn->ibc_tx_queue_nocred.next,
926 } else if (!list_empty(&conn->ibc_tx_queue)) {
928 tx = list_entry(conn->ibc_tx_queue.next,
933 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
937 spin_unlock(&conn->ibc_lock);
941 kiblnd_tx_complete (kib_tx_t *tx, int status)
943 int failed = (status != IB_WC_SUCCESS);
944 kib_conn_t *conn = tx->tx_conn;
947 LASSERT (tx->tx_sending > 0);
950 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
951 CDEBUG(D_NETERROR, "Tx -> %s cookie "LPX64
952 " sending %d waiting %d: failed %d\n",
953 libcfs_nid2str(conn->ibc_peer->ibp_nid),
954 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
957 kiblnd_close_conn(conn, -EIO);
959 kiblnd_peer_alive(conn->ibc_peer);
962 spin_lock(&conn->ibc_lock);
964 /* I could be racing with rdma completion. Whoever makes 'tx' idle
965 * gets to free it, which also drops its ref on 'conn'. */
968 conn->ibc_nsends_posted--;
969 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
970 conn->ibc_noops_posted--;
973 tx->tx_waiting = 0; /* don't wait for peer */
974 tx->tx_status = -EIO;
977 idle = (tx->tx_sending == 0) && /* This is the final callback */
978 !tx->tx_waiting && /* Not waiting for peer */
979 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
981 list_del(&tx->tx_list);
983 kiblnd_conn_addref(conn); /* 1 ref for me.... */
985 spin_unlock(&conn->ibc_lock);
988 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
990 kiblnd_check_sends(conn);
992 kiblnd_conn_decref(conn); /* ...until here */
996 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
998 kib_net_t *net = ni->ni_data;
999 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1000 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1001 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1004 LASSERT (net != NULL);
1005 LASSERT (tx->tx_nwrq >= 0);
1006 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1007 LASSERT (nob <= IBLND_MSG_SIZE);
1009 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1011 mr = kiblnd_find_dma_mr(net, tx->tx_msgaddr, nob);
1012 LASSERT (mr != NULL);
1014 sge->lkey = mr->lkey;
1015 sge->addr = tx->tx_msgaddr;
1018 memset(wrq, 0, sizeof(*wrq));
1021 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1024 wrq->opcode = IB_WR_SEND;
1025 wrq->send_flags = IB_SEND_SIGNALED;
1031 kiblnd_init_rdma (kib_conn_t *conn, kib_tx_t *tx, int type,
1032 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1034 kib_msg_t *ibmsg = tx->tx_msg;
1035 kib_rdma_desc_t *srcrd = tx->tx_rd;
1036 struct ib_sge *sge = &tx->tx_sge[0];
1037 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1043 LASSERT (!in_interrupt());
1044 LASSERT (tx->tx_nwrq == 0);
1045 LASSERT (type == IBLND_MSG_GET_DONE ||
1046 type == IBLND_MSG_PUT_DONE);
1048 srcidx = dstidx = 0;
1051 if (srcidx >= srcrd->rd_nfrags) {
1052 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1057 if (dstidx == dstrd->rd_nfrags) {
1058 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1063 if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
1064 CERROR("RDMA too fragmented for %s (%d): "
1065 "%d/%d src %d/%d dst frags\n",
1066 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1067 IBLND_RDMA_FRAGS(conn->ibc_version),
1068 srcidx, srcrd->rd_nfrags,
1069 dstidx, dstrd->rd_nfrags);
1074 wrknob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1075 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1077 sge = &tx->tx_sge[tx->tx_nwrq];
1078 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1079 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1080 sge->length = wrknob;
1082 wrq = &tx->tx_wrq[tx->tx_nwrq];
1084 wrq->next = wrq + 1;
1085 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1088 wrq->opcode = IB_WR_RDMA_WRITE;
1089 wrq->send_flags = 0;
1091 wrq->wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1092 wrq->wr.rdma.rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1094 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1095 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1104 if (rc < 0) /* no RDMA if completing with failure */
1107 ibmsg->ibm_u.completion.ibcm_status = rc;
1108 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1109 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1110 type, sizeof (kib_completion_msg_t));
1116 kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
1118 struct list_head *q;
1120 LASSERT (tx->tx_nwrq > 0); /* work items set up */
1121 LASSERT (!tx->tx_queued); /* not queued for sending already */
1122 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1125 tx->tx_deadline = jiffies + (*kiblnd_tunables.kib_timeout * HZ);
1127 if (tx->tx_conn == NULL) {
1128 kiblnd_conn_addref(conn);
1130 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1132 /* PUT_DONE first attached to conn as a PUT_REQ */
1133 LASSERT (tx->tx_conn == conn);
1134 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1137 switch (tx->tx_msg->ibm_type) {
1141 case IBLND_MSG_PUT_REQ:
1142 case IBLND_MSG_GET_REQ:
1143 q = &conn->ibc_tx_queue_rsrvd;
1146 case IBLND_MSG_PUT_NAK:
1147 case IBLND_MSG_PUT_ACK:
1148 case IBLND_MSG_PUT_DONE:
1149 case IBLND_MSG_GET_DONE:
1150 q = &conn->ibc_tx_queue_nocred;
1153 case IBLND_MSG_NOOP:
1154 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1155 q = &conn->ibc_tx_queue_nocred;
1157 q = &conn->ibc_tx_queue;
1160 case IBLND_MSG_IMMEDIATE:
1161 q = &conn->ibc_tx_queue;
1165 list_add_tail(&tx->tx_list, q);
1169 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1171 spin_lock(&conn->ibc_lock);
1172 kiblnd_queue_tx_locked(tx, conn);
1173 spin_unlock(&conn->ibc_lock);
1175 kiblnd_check_sends(conn);
1179 kiblnd_connect_peer (kib_peer_t *peer)
1181 struct rdma_cm_id *cmid;
1183 kib_net_t *net = peer->ibp_ni->ni_data;
1184 struct sockaddr_in srcaddr;
1185 struct sockaddr_in dstaddr;
1188 LASSERT (net != NULL);
1189 LASSERT (peer->ibp_connecting > 0);
1191 cmid = rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP);
1193 CERROR("Can't create CMID for %s: %ld\n",
1194 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1200 memset(&srcaddr, 0, sizeof(srcaddr));
1201 srcaddr.sin_family = AF_INET;
1202 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1204 memset(&dstaddr, 0, sizeof(dstaddr));
1205 dstaddr.sin_family = AF_INET;
1206 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1207 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1209 kiblnd_peer_addref(peer); /* cmid's ref */
1211 rc = rdma_resolve_addr(cmid,
1212 (struct sockaddr *)&srcaddr,
1213 (struct sockaddr *)&dstaddr,
1214 *kiblnd_tunables.kib_timeout * 1000);
1216 LASSERT (cmid->device != NULL);
1217 CDEBUG(D_NET, "%s: connection bound to %s:%u.%u.%u.%u:%s\n",
1218 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1219 HIPQUAD(dev->ibd_ifip), cmid->device->name);
1223 /* Can't initiate address resolution: */
1224 CERROR("Can't resolve addr for %s: %d\n",
1225 libcfs_nid2str(peer->ibp_nid), rc);
1227 kiblnd_peer_decref(peer); /* cmid's ref */
1228 rdma_destroy_id(cmid);
1230 kiblnd_peer_connect_failed(peer, 1, rc);
1234 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1239 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1240 unsigned long flags;
1243 /* If I get here, I've committed to send, so I complete the tx with
1244 * failure on any problems */
1246 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1247 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1249 /* First time, just use a read lock since I expect to find my peer
1251 read_lock_irqsave(g_lock, flags);
1253 peer = kiblnd_find_peer_locked(nid);
1254 if (peer != NULL && !list_empty(&peer->ibp_conns)) {
1255 /* Found a peer with an established connection */
1256 conn = kiblnd_get_conn_locked(peer);
1257 kiblnd_conn_addref(conn); /* 1 ref for me... */
1259 read_unlock_irqrestore(g_lock, flags);
1262 kiblnd_queue_tx(tx, conn);
1263 kiblnd_conn_decref(conn); /* ...to here */
1267 read_unlock(g_lock);
1268 /* Re-try with a write lock */
1271 peer = kiblnd_find_peer_locked(nid);
1273 if (list_empty(&peer->ibp_conns)) {
1274 /* found a peer, but it's still connecting... */
1275 LASSERT (peer->ibp_connecting != 0 ||
1276 peer->ibp_accepting != 0);
1278 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1279 write_unlock_irqrestore(g_lock, flags);
1281 conn = kiblnd_get_conn_locked(peer);
1282 kiblnd_conn_addref(conn); /* 1 ref for me... */
1284 write_unlock_irqrestore(g_lock, flags);
1287 kiblnd_queue_tx(tx, conn);
1288 kiblnd_conn_decref(conn); /* ...to here */
1293 write_unlock_irqrestore(g_lock, flags);
1295 /* Allocate a peer ready to add to the peer table and retry */
1296 rc = kiblnd_create_peer(ni, &peer, nid);
1298 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1300 tx->tx_status = -EHOSTUNREACH;
1302 kiblnd_tx_done(ni, tx);
1307 write_lock_irqsave(g_lock, flags);
1309 peer2 = kiblnd_find_peer_locked(nid);
1310 if (peer2 != NULL) {
1311 if (list_empty(&peer2->ibp_conns)) {
1312 /* found a peer, but it's still connecting... */
1313 LASSERT (peer2->ibp_connecting != 0 ||
1314 peer2->ibp_accepting != 0);
1316 list_add_tail(&tx->tx_list, &peer2->ibp_tx_queue);
1317 write_unlock_irqrestore(g_lock, flags);
1319 conn = kiblnd_get_conn_locked(peer2);
1320 kiblnd_conn_addref(conn); /* 1 ref for me... */
1322 write_unlock_irqrestore(g_lock, flags);
1325 kiblnd_queue_tx(tx, conn);
1326 kiblnd_conn_decref(conn); /* ...to here */
1329 kiblnd_peer_decref(peer);
1333 /* Brand new peer */
1334 LASSERT (peer->ibp_connecting == 0);
1335 peer->ibp_connecting = 1;
1337 /* always called with a ref on ni, which prevents ni being shutdown */
1338 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1341 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1343 kiblnd_peer_addref(peer);
1344 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1346 write_unlock_irqrestore(g_lock, flags);
1348 kiblnd_connect_peer(peer);
1349 kiblnd_peer_decref(peer);
1353 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1355 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1356 int type = lntmsg->msg_type;
1357 lnet_process_id_t target = lntmsg->msg_target;
1358 int target_is_router = lntmsg->msg_target_is_router;
1359 int routing = lntmsg->msg_routing;
1360 unsigned int payload_niov = lntmsg->msg_niov;
1361 struct iovec *payload_iov = lntmsg->msg_iov;
1362 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1363 unsigned int payload_offset = lntmsg->msg_offset;
1364 unsigned int payload_nob = lntmsg->msg_len;
1370 /* NB 'private' is different depending on what we're sending.... */
1372 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1373 payload_nob, payload_niov, libcfs_id2str(target));
1375 LASSERT (payload_nob == 0 || payload_niov > 0);
1376 LASSERT (payload_niov <= LNET_MAX_IOV);
1378 /* Thread context */
1379 LASSERT (!in_interrupt());
1380 /* payload is either all vaddrs or all pages */
1381 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1389 LASSERT (payload_nob == 0);
1393 if (routing || target_is_router)
1394 break; /* send IMMEDIATE */
1396 /* is the REPLY message too small for RDMA? */
1397 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1398 if (nob <= IBLND_MSG_SIZE)
1399 break; /* send IMMEDIATE */
1401 tx = kiblnd_get_idle_tx(ni);
1403 CERROR("Can't allocate txd for GET to %s: \n",
1404 libcfs_nid2str(target.nid));
1410 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1411 rc = kiblnd_setup_rd_iov(ni, tx,
1412 &ibmsg->ibm_u.get.ibgm_rd,
1413 lntmsg->msg_md->md_niov,
1414 lntmsg->msg_md->md_iov.iov,
1415 0, lntmsg->msg_md->md_length);
1417 rc = kiblnd_setup_rd_kiov(ni, tx,
1418 &ibmsg->ibm_u.get.ibgm_rd,
1419 lntmsg->msg_md->md_niov,
1420 lntmsg->msg_md->md_iov.kiov,
1421 0, lntmsg->msg_md->md_length);
1423 CERROR("Can't setup GET sink for %s: %d\n",
1424 libcfs_nid2str(target.nid), rc);
1425 kiblnd_tx_done(ni, tx);
1429 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1430 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1431 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1433 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1435 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1436 if (tx->tx_lntmsg[1] == NULL) {
1437 CERROR("Can't create reply for GET -> %s\n",
1438 libcfs_nid2str(target.nid));
1439 kiblnd_tx_done(ni, tx);
1443 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1444 tx->tx_waiting = 1; /* waiting for GET_DONE */
1445 kiblnd_launch_tx(ni, tx, target.nid);
1448 case LNET_MSG_REPLY:
1450 /* Is the payload small enough not to need RDMA? */
1451 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1452 if (nob <= IBLND_MSG_SIZE)
1453 break; /* send IMMEDIATE */
1455 tx = kiblnd_get_idle_tx(ni);
1457 CERROR("Can't allocate %s txd for %s\n",
1458 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1459 libcfs_nid2str(target.nid));
1463 if (payload_kiov == NULL)
1464 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1465 payload_niov, payload_iov,
1466 payload_offset, payload_nob);
1468 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1469 payload_niov, payload_kiov,
1470 payload_offset, payload_nob);
1472 CERROR("Can't setup PUT src for %s: %d\n",
1473 libcfs_nid2str(target.nid), rc);
1474 kiblnd_tx_done(ni, tx);
1479 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1480 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1481 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1483 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1484 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1485 kiblnd_launch_tx(ni, tx, target.nid);
1489 /* send IMMEDIATE */
1491 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1494 tx = kiblnd_get_idle_tx(ni);
1496 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1497 type, libcfs_nid2str(target.nid));
1502 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1504 if (payload_kiov != NULL)
1505 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1506 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1507 payload_niov, payload_kiov,
1508 payload_offset, payload_nob);
1510 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1511 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1512 payload_niov, payload_iov,
1513 payload_offset, payload_nob);
1515 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1516 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1518 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1519 kiblnd_launch_tx(ni, tx, target.nid);
1524 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1526 lnet_process_id_t target = lntmsg->msg_target;
1527 unsigned int niov = lntmsg->msg_niov;
1528 struct iovec *iov = lntmsg->msg_iov;
1529 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1530 unsigned int offset = lntmsg->msg_offset;
1531 unsigned int nob = lntmsg->msg_len;
1535 tx = kiblnd_get_idle_tx(ni);
1537 CERROR("Can't get tx for REPLY to %s\n",
1538 libcfs_nid2str(target.nid));
1544 else if (kiov == NULL)
1545 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1546 niov, iov, offset, nob);
1548 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1549 niov, kiov, offset, nob);
1552 CERROR("Can't setup GET src for %s: %d\n",
1553 libcfs_nid2str(target.nid), rc);
1557 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1558 IBLND_MSG_GET_DONE, nob,
1559 &rx->rx_msg->ibm_u.get.ibgm_rd,
1560 rx->rx_msg->ibm_u.get.ibgm_cookie);
1562 CERROR("Can't setup rdma for GET from %s: %d\n",
1563 libcfs_nid2str(target.nid), rc);
1568 /* No RDMA: local completion may happen now! */
1569 lnet_finalize(ni, lntmsg, 0);
1571 /* RDMA: lnet_finalize(lntmsg) when it
1573 tx->tx_lntmsg[0] = lntmsg;
1576 kiblnd_queue_tx(tx, rx->rx_conn);
1580 kiblnd_tx_done(ni, tx);
1582 lnet_finalize(ni, lntmsg, -EIO);
1586 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1587 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1588 unsigned int offset, unsigned int mlen, unsigned int rlen)
1590 kib_rx_t *rx = private;
1591 kib_msg_t *rxmsg = rx->rx_msg;
1592 kib_conn_t *conn = rx->rx_conn;
1596 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1599 LASSERT (mlen <= rlen);
1600 LASSERT (!in_interrupt());
1601 /* Either all pages or all vaddrs */
1602 LASSERT (!(kiov != NULL && iov != NULL));
1604 switch (rxmsg->ibm_type) {
1608 case IBLND_MSG_IMMEDIATE:
1609 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1610 if (nob > rx->rx_nob) {
1611 CERROR ("Immediate message from %s too big: %d(%d)\n",
1612 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1619 lnet_copy_flat2kiov(niov, kiov, offset,
1620 IBLND_MSG_SIZE, rxmsg,
1621 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1624 lnet_copy_flat2iov(niov, iov, offset,
1625 IBLND_MSG_SIZE, rxmsg,
1626 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1628 lnet_finalize (ni, lntmsg, 0);
1631 case IBLND_MSG_PUT_REQ:
1633 lnet_finalize(ni, lntmsg, 0);
1634 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1635 rxmsg->ibm_u.putreq.ibprm_cookie);
1639 tx = kiblnd_get_idle_tx(ni);
1641 CERROR("Can't allocate tx for %s\n",
1642 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1643 /* Not replying will break the connection */
1650 rc = kiblnd_setup_rd_iov(ni, tx,
1651 &txmsg->ibm_u.putack.ibpam_rd,
1652 niov, iov, offset, mlen);
1654 rc = kiblnd_setup_rd_kiov(ni, tx,
1655 &txmsg->ibm_u.putack.ibpam_rd,
1656 niov, kiov, offset, mlen);
1658 CERROR("Can't setup PUT sink for %s: %d\n",
1659 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1660 kiblnd_tx_done(ni, tx);
1661 /* tell peer it's over */
1662 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1663 rxmsg->ibm_u.putreq.ibprm_cookie);
1667 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1668 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1669 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1671 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1673 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1674 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1675 kiblnd_queue_tx(tx, conn);
1677 /* reposted buffer reserved for PUT_DONE */
1678 post_credit = IBLND_POSTRX_NO_CREDIT;
1681 case IBLND_MSG_GET_REQ:
1682 if (lntmsg != NULL) {
1683 /* Optimized GET; RDMA lntmsg's payload */
1684 kiblnd_reply(ni, rx, lntmsg);
1686 /* GET didn't match anything */
1687 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1689 rxmsg->ibm_u.get.ibgm_cookie);
1694 kiblnd_post_rx(rx, post_credit);
1699 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1701 long pid = kernel_thread (fn, arg, 0);
1706 atomic_inc (&kiblnd_data.kib_nthreads);
1711 kiblnd_thread_fini (void)
1713 atomic_dec (&kiblnd_data.kib_nthreads);
1717 kiblnd_peer_alive (kib_peer_t *peer)
1719 /* This is racy, but everyone's only writing cfs_time_current() */
1720 peer->ibp_last_alive = cfs_time_current();
1725 kiblnd_peer_notify (kib_peer_t *peer)
1728 cfs_time_t last_alive = 0;
1729 unsigned long flags;
1731 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1733 if (list_empty(&peer->ibp_conns) &&
1734 peer->ibp_accepting == 0 &&
1735 peer->ibp_connecting == 0 &&
1736 peer->ibp_error != 0) {
1737 error = peer->ibp_error;
1738 peer->ibp_error = 0;
1740 last_alive = peer->ibp_last_alive;
1743 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1746 lnet_notify(peer->ibp_ni,
1747 peer->ibp_nid, 0, last_alive);
1751 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1753 /* This just does the immediate housekeeping. 'error' is zero for a
1754 * normal shutdown which can happen only after the connection has been
1755 * established. If the connection is established, schedule the
1756 * connection to be finished off by the connd. Otherwise the connd is
1757 * already dealing with it (either to set it up or tear it down).
1758 * Caller holds kib_global_lock exclusively in irq context */
1759 unsigned long flags;
1760 kib_peer_t *peer = conn->ibc_peer;
1762 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1764 if (error != 0 && conn->ibc_comms_error == 0)
1765 conn->ibc_comms_error = error;
1767 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1768 return; /* already being handled */
1771 list_empty(&conn->ibc_tx_queue) &&
1772 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1773 list_empty(&conn->ibc_tx_queue_nocred) &&
1774 list_empty(&conn->ibc_active_txs)) {
1775 CDEBUG(D_NET, "closing conn to %s\n",
1776 libcfs_nid2str(peer->ibp_nid));
1778 CDEBUG(D_NETERROR, "Closing conn to %s: error %d%s%s%s%s\n",
1779 libcfs_nid2str(peer->ibp_nid), error,
1780 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1781 list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1782 list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1783 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1786 list_del(&conn->ibc_list);
1787 /* connd (see below) takes over ibc_list's ref */
1789 if (list_empty (&peer->ibp_conns) && /* no more conns */
1790 kiblnd_peer_active(peer)) { /* still in peer table */
1791 kiblnd_unlink_peer_locked(peer);
1793 /* set/clear error on last conn */
1794 peer->ibp_error = conn->ibc_comms_error;
1797 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1799 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1801 list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1802 wake_up (&kiblnd_data.kib_connd_waitq);
1804 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1808 kiblnd_close_conn (kib_conn_t *conn, int error)
1810 unsigned long flags;
1812 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1814 kiblnd_close_conn_locked(conn, error);
1816 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1820 kiblnd_handle_early_rxs(kib_conn_t *conn)
1822 unsigned long flags;
1825 LASSERT (!in_interrupt());
1826 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1828 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1829 while (!list_empty(&conn->ibc_early_rxs)) {
1830 rx = list_entry(conn->ibc_early_rxs.next,
1832 list_del(&rx->rx_list);
1833 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1835 kiblnd_handle_rx(rx);
1837 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1839 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1843 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1845 LIST_HEAD (zombies);
1846 struct list_head *tmp;
1847 struct list_head *nxt;
1850 spin_lock(&conn->ibc_lock);
1852 list_for_each_safe (tmp, nxt, txs) {
1853 tx = list_entry (tmp, kib_tx_t, tx_list);
1855 if (txs == &conn->ibc_active_txs) {
1856 LASSERT (!tx->tx_queued);
1857 LASSERT (tx->tx_waiting ||
1858 tx->tx_sending != 0);
1860 LASSERT (tx->tx_queued);
1863 tx->tx_status = -ECONNABORTED;
1866 if (tx->tx_sending == 0) {
1868 list_del (&tx->tx_list);
1869 list_add (&tx->tx_list, &zombies);
1873 spin_unlock(&conn->ibc_lock);
1875 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
1876 &zombies, -ECONNABORTED);
1880 kiblnd_finalise_conn (kib_conn_t *conn)
1882 LASSERT (!in_interrupt());
1883 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
1885 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
1887 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
1888 * for connections that didn't get as far as being connected, because
1889 * rdma_disconnect() does this for free. */
1890 kiblnd_abort_receives(conn);
1892 /* Complete all tx descs not waiting for sends to complete.
1893 * NB we should be safe from RDMA now that the QP has changed state */
1895 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
1896 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
1897 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
1898 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
1900 kiblnd_handle_early_rxs(conn);
1904 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
1906 LIST_HEAD (zombies);
1907 unsigned long flags;
1909 LASSERT (error != 0);
1910 LASSERT (!in_interrupt());
1912 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1915 LASSERT (peer->ibp_connecting > 0);
1916 peer->ibp_connecting--;
1918 LASSERT (peer->ibp_accepting > 0);
1919 peer->ibp_accepting--;
1922 if (peer->ibp_connecting != 0 ||
1923 peer->ibp_accepting != 0) {
1924 /* another connection attempt under way... */
1925 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1929 if (list_empty(&peer->ibp_conns)) {
1930 /* Take peer's blocked transmits to complete with error */
1931 list_add(&zombies, &peer->ibp_tx_queue);
1932 list_del_init(&peer->ibp_tx_queue);
1934 if (kiblnd_peer_active(peer))
1935 kiblnd_unlink_peer_locked(peer);
1937 peer->ibp_error = error;
1939 /* Can't have blocked transmits if there are connections */
1940 LASSERT (list_empty(&peer->ibp_tx_queue));
1943 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1945 kiblnd_peer_notify(peer);
1947 if (list_empty (&zombies))
1950 CDEBUG (D_NETERROR, "Deleting messages for %s: connection failed\n",
1951 libcfs_nid2str(peer->ibp_nid));
1953 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
1957 kiblnd_connreq_done(kib_conn_t *conn, int status)
1959 kib_peer_t *peer = conn->ibc_peer;
1961 struct list_head txs;
1962 unsigned long flags;
1965 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
1967 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
1968 libcfs_nid2str(peer->ibp_nid), active,
1969 conn->ibc_version, status);
1971 LASSERT (!in_interrupt());
1972 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
1973 peer->ibp_connecting > 0) ||
1974 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
1975 peer->ibp_accepting > 0));
1977 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
1978 conn->ibc_connvars = NULL;
1981 /* failed to establish connection */
1982 kiblnd_peer_connect_failed(peer, active, status);
1983 kiblnd_finalise_conn(conn);
1987 /* connection established */
1988 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1990 conn->ibc_last_send = jiffies;
1991 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
1992 kiblnd_peer_alive(peer);
1994 /* Add conn to peer's list and nuke any dangling conns from a different
1995 * peer instance... */
1996 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
1997 list_add(&conn->ibc_list, &peer->ibp_conns);
1999 peer->ibp_connecting--;
2001 peer->ibp_accepting--;
2003 if (peer->ibp_version == 0) {
2004 peer->ibp_version = conn->ibc_version;
2005 peer->ibp_incarnation = conn->ibc_incarnation;
2008 if (peer->ibp_version != conn->ibc_version ||
2009 peer->ibp_incarnation != conn->ibc_incarnation) {
2010 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2011 conn->ibc_incarnation);
2012 peer->ibp_version = conn->ibc_version;
2013 peer->ibp_incarnation = conn->ibc_incarnation;
2016 /* grab pending txs while I have the lock */
2017 list_add(&txs, &peer->ibp_tx_queue);
2018 list_del_init(&peer->ibp_tx_queue);
2020 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2021 conn->ibc_comms_error != 0) { /* error has happened already */
2022 lnet_ni_t *ni = peer->ibp_ni;
2024 /* start to shut down connection */
2025 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2026 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2028 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2033 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2035 /* Schedule blocked txs */
2036 spin_lock (&conn->ibc_lock);
2037 while (!list_empty (&txs)) {
2038 tx = list_entry (txs.next, kib_tx_t, tx_list);
2039 list_del(&tx->tx_list);
2041 kiblnd_queue_tx_locked(tx, conn);
2043 spin_unlock (&conn->ibc_lock);
2045 kiblnd_check_sends(conn);
2047 /* schedule blocked rxs */
2048 kiblnd_handle_early_rxs(conn);
2052 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2056 rc = rdma_reject(cmid, rej, sizeof(*rej));
2059 CWARN("Error %d sending reject\n", rc);
2063 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2065 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2066 kib_msg_t *reqmsg = priv;
2072 lnet_ni_t *ni = NULL;
2073 kib_net_t *net = NULL;
2075 struct rdma_conn_param cp;
2077 int version = IBLND_MSG_VERSION;
2078 unsigned long flags;
2081 LASSERT (!in_interrupt());
2083 /* cmid inherits 'context' from the corresponding listener id */
2084 ibdev = (kib_dev_t *)cmid->context;
2085 LASSERT (ibdev != NULL);
2087 memset(&rej, 0, sizeof(rej));
2088 rej.ibr_magic = IBLND_MSG_MAGIC;
2089 rej.ibr_why = IBLND_REJECT_FATAL;
2090 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2092 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2093 CERROR("Short connection request\n");
2097 /* Future protocol version compatibility support! If the
2098 * o2iblnd-specific protocol changes, or when LNET unifies
2099 * protocols over all LNDs, the initial connection will
2100 * negotiate a protocol version. I trap this here to avoid
2101 * console errors; the reject tells the peer which protocol I
2103 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2104 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2106 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2107 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2108 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2110 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2111 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2112 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2115 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2117 CERROR("Can't parse connection request: %d\n", rc);
2121 nid = reqmsg->ibm_srcnid;
2122 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2125 net = (kib_net_t *)ni->ni_data;
2126 rej.ibr_incarnation = net->ibn_incarnation;
2129 if (ni == NULL || /* no matching net */
2130 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2131 net->ibn_dev != ibdev) { /* wrong device */
2132 CERROR("Can't accept %s on %s (%s:%d:%u.%u.%u.%u): "
2133 "bad dst nid %s\n", libcfs_nid2str(nid),
2134 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2135 ibdev->ibd_ifname, ibdev->ibd_nnets,
2136 HIPQUAD(ibdev->ibd_ifip),
2137 libcfs_nid2str(reqmsg->ibm_dstnid));
2142 /* check time stamp as soon as possible */
2143 if (reqmsg->ibm_dststamp != 0 &&
2144 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2145 CWARN("Stale connection request\n");
2146 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2150 /* I can accept peer's version */
2151 version = reqmsg->ibm_version;
2153 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2154 CERROR("Unexpected connreq msg type: %x from %s\n",
2155 reqmsg->ibm_type, libcfs_nid2str(nid));
2159 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2160 IBLND_MSG_QUEUE_SIZE(version)) {
2161 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2162 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2163 IBLND_MSG_QUEUE_SIZE(version));
2165 if (version == IBLND_MSG_VERSION)
2166 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2171 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2172 IBLND_RDMA_FRAGS(version)) {
2173 CERROR("Can't accept %s(version %x): "
2174 "incompatible max_frags %d (%d wanted)\n",
2175 libcfs_nid2str(nid), version,
2176 reqmsg->ibm_u.connparams.ibcp_max_frags,
2177 IBLND_RDMA_FRAGS(version));
2179 if (version == IBLND_MSG_VERSION)
2180 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2186 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2187 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2188 libcfs_nid2str(nid),
2189 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2194 /* assume 'nid' is a new peer; create */
2195 rc = kiblnd_create_peer(ni, &peer, nid);
2197 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2198 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2202 write_lock_irqsave(g_lock, flags);
2204 peer2 = kiblnd_find_peer_locked(nid);
2205 if (peer2 != NULL) {
2206 if (peer2->ibp_version == 0) {
2207 peer2->ibp_version = version;
2208 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2211 /* not the guy I've talked with */
2212 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2213 peer2->ibp_version != version) {
2214 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2215 write_unlock_irqrestore(g_lock, flags);
2217 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2218 libcfs_nid2str(nid), peer2->ibp_version, version);
2220 kiblnd_peer_decref(peer);
2221 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2225 /* tie-break connection race in favour of the higher NID */
2226 if (peer2->ibp_connecting != 0 &&
2228 write_unlock_irqrestore(g_lock, flags);
2230 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2232 kiblnd_peer_decref(peer);
2233 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2237 peer2->ibp_accepting++;
2238 kiblnd_peer_addref(peer2);
2240 write_unlock_irqrestore(g_lock, flags);
2241 kiblnd_peer_decref(peer);
2244 /* Brand new peer */
2245 LASSERT (peer->ibp_accepting == 0);
2246 LASSERT (peer->ibp_version == 0 &&
2247 peer->ibp_incarnation == 0);
2249 peer->ibp_accepting = 1;
2250 peer->ibp_version = version;
2251 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2253 /* I have a ref on ni that prevents it being shutdown */
2254 LASSERT (net->ibn_shutdown == 0);
2256 kiblnd_peer_addref(peer);
2257 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2259 write_unlock_irqrestore(g_lock, flags);
2262 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2264 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2265 kiblnd_peer_decref(peer);
2266 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2270 /* conn now "owns" cmid, so I return success from here on to ensure the
2271 * CM callback doesn't destroy cmid. */
2273 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2274 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2275 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2276 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2277 <= IBLND_RX_MSGS(version));
2279 ackmsg = &conn->ibc_connvars->cv_msg;
2280 memset(ackmsg, 0, sizeof(*ackmsg));
2282 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2283 sizeof(ackmsg->ibm_u.connparams));
2284 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2285 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2286 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2288 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2290 memset(&cp, 0, sizeof(cp));
2291 cp.private_data = ackmsg;
2292 cp.private_data_len = ackmsg->ibm_nob;
2293 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2294 cp.initiator_depth = 0;
2295 cp.flow_control = 1;
2296 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2297 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2299 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2301 rc = rdma_accept(cmid, &cp);
2303 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2304 rej.ibr_version = version;
2305 rej.ibr_why = IBLND_REJECT_FATAL;
2307 kiblnd_reject(cmid, &rej);
2308 kiblnd_connreq_done(conn, rc);
2309 kiblnd_conn_decref(conn);
2319 rej.ibr_version = version;
2320 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2321 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2322 kiblnd_reject(cmid, &rej);
2324 return -ECONNREFUSED;
2328 kiblnd_reconnect (kib_conn_t *conn, int version,
2329 __u64 incarnation, int why, kib_connparams_t *cp)
2331 kib_peer_t *peer = conn->ibc_peer;
2334 unsigned long flags;
2336 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2337 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2339 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2341 /* retry connection if it's still needed and no other connection
2342 * attempts (active or passive) are in progress */
2343 if (!list_empty(&peer->ibp_tx_queue) &&
2344 peer->ibp_connecting == 1 &&
2345 peer->ibp_accepting == 0) {
2347 peer->ibp_connecting++;
2349 peer->ibp_version = version;
2350 peer->ibp_incarnation = incarnation;
2353 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2363 case IBLND_REJECT_CONN_STALE:
2367 case IBLND_REJECT_CONN_RACE:
2368 reason = "conn race";
2371 case IBLND_REJECT_CONN_UNCOMPAT:
2372 reason = "version negotiation";
2376 CDEBUG(D_NETERROR, "%s: retrying (%s), %x, %x, "
2377 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2378 libcfs_nid2str(peer->ibp_nid),
2379 reason, IBLND_MSG_VERSION, version,
2380 cp != NULL ? cp->ibcp_queue_depth : IBLND_MSG_QUEUE_SIZE(version),
2381 cp != NULL ? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2382 cp != NULL ? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2384 kiblnd_connect_peer(peer);
2388 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2390 kib_peer_t *peer = conn->ibc_peer;
2392 LASSERT (!in_interrupt());
2393 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2396 case IB_CM_REJ_STALE_CONN:
2397 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2398 IBLND_REJECT_CONN_STALE, NULL);
2401 case IB_CM_REJ_INVALID_SERVICE_ID:
2402 CDEBUG(D_NETERROR, "%s rejected: no listener at %d\n",
2403 libcfs_nid2str(peer->ibp_nid),
2404 *kiblnd_tunables.kib_service);
2407 case IB_CM_REJ_CONSUMER_DEFINED:
2408 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2409 kib_rej_t *rej = priv;
2410 kib_connparams_t *cp = NULL;
2412 __u64 incarnation = -1;
2414 /* NB. default incarnation is -1 because:
2415 * a) V1 will ignore dst incarnation in connreq.
2416 * b) V2 will provide incarnation while rejecting me,
2417 * -1 will be overwrote.
2419 * if I try to connect to a V1 peer with V2 protocol,
2420 * it rejected me then upgrade to V2, I have no idea
2421 * about the upgrading and try to reconnect with V1,
2422 * in this case upgraded V2 can find out I'm trying to
2423 * talk to the old guy and reject me(incarnation is -1).
2426 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2427 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2428 __swab32s(&rej->ibr_magic);
2429 __swab16s(&rej->ibr_version);
2433 if (priv_nob >= sizeof(kib_rej_t) &&
2434 rej->ibr_version > IBLND_MSG_VERSION_1) {
2435 /* priv_nob is always 148 in current version
2436 * of OFED, so we still need to check version.
2437 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2441 __swab64s(&rej->ibr_incarnation);
2442 __swab16s(&cp->ibcp_queue_depth);
2443 __swab16s(&cp->ibcp_max_frags);
2444 __swab32s(&cp->ibcp_max_msg_size);
2447 incarnation = rej->ibr_incarnation;
2450 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2451 rej->ibr_magic != LNET_PROTO_MAGIC) {
2452 CERROR("%s rejected: consumer defined fatal error\n",
2453 libcfs_nid2str(peer->ibp_nid));
2457 if (rej->ibr_version != IBLND_MSG_VERSION &&
2458 rej->ibr_version != IBLND_MSG_VERSION_1) {
2459 CERROR("%s rejected: o2iblnd version %x error\n",
2460 libcfs_nid2str(peer->ibp_nid),
2465 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2466 rej->ibr_version == IBLND_MSG_VERSION_1) {
2467 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2468 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2470 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2471 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2474 switch (rej->ibr_why) {
2475 case IBLND_REJECT_CONN_RACE:
2476 case IBLND_REJECT_CONN_STALE:
2477 case IBLND_REJECT_CONN_UNCOMPAT:
2478 kiblnd_reconnect(conn, rej->ibr_version,
2479 incarnation, rej->ibr_why, cp);
2482 case IBLND_REJECT_MSG_QUEUE_SIZE:
2483 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2484 libcfs_nid2str(peer->ibp_nid), cp->ibcp_queue_depth,
2485 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2488 case IBLND_REJECT_RDMA_FRAGS:
2489 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2490 libcfs_nid2str(peer->ibp_nid), cp->ibcp_max_frags,
2491 IBLND_RDMA_FRAGS(conn->ibc_version));
2494 case IBLND_REJECT_NO_RESOURCES:
2495 CERROR("%s rejected: o2iblnd no resources\n",
2496 libcfs_nid2str(peer->ibp_nid));
2499 case IBLND_REJECT_FATAL:
2500 CERROR("%s rejected: o2iblnd fatal error\n",
2501 libcfs_nid2str(peer->ibp_nid));
2505 CERROR("%s rejected: o2iblnd reason %d\n",
2506 libcfs_nid2str(peer->ibp_nid),
2514 CDEBUG(D_NETERROR, "%s rejected: reason %d, size %d\n",
2515 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2519 kiblnd_connreq_done(conn, -ECONNREFUSED);
2523 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2525 kib_peer_t *peer = conn->ibc_peer;
2526 lnet_ni_t *ni = peer->ibp_ni;
2527 kib_net_t *net = ni->ni_data;
2528 kib_msg_t *msg = priv;
2529 int ver = conn->ibc_version;
2530 int rc = kiblnd_unpack_msg(msg, priv_nob);
2531 unsigned long flags;
2533 LASSERT (net != NULL);
2536 CERROR("Can't unpack connack from %s: %d\n",
2537 libcfs_nid2str(peer->ibp_nid), rc);
2541 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2542 CERROR("Unexpected message %d from %s\n",
2543 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2548 if (ver != msg->ibm_version) {
2549 CERROR("%s replied version %x is different with "
2550 "requested version %x\n",
2551 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2556 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2557 IBLND_MSG_QUEUE_SIZE(ver)) {
2558 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2559 libcfs_nid2str(peer->ibp_nid),
2560 msg->ibm_u.connparams.ibcp_queue_depth,
2561 IBLND_MSG_QUEUE_SIZE(ver));
2566 if (msg->ibm_u.connparams.ibcp_max_frags !=
2567 IBLND_RDMA_FRAGS(ver)) {
2568 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2569 libcfs_nid2str(peer->ibp_nid),
2570 msg->ibm_u.connparams.ibcp_max_frags,
2571 IBLND_RDMA_FRAGS(ver));
2576 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2577 CERROR("%s max message size %d too big (%d max)\n",
2578 libcfs_nid2str(peer->ibp_nid),
2579 msg->ibm_u.connparams.ibcp_max_msg_size,
2585 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2586 if (msg->ibm_dstnid == ni->ni_nid &&
2587 msg->ibm_dststamp == net->ibn_incarnation)
2591 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2594 CERROR("Bad connection reply from %s, rc = %d, "
2595 "version: %x max_frags: %d\n",
2596 libcfs_nid2str(peer->ibp_nid), rc,
2597 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2601 conn->ibc_incarnation = msg->ibm_srcstamp;
2603 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2604 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2605 <= IBLND_RX_MSGS(ver));
2607 kiblnd_connreq_done(conn, 0);
2611 /* NB My QP has already established itself, so I handle anything going
2612 * wrong here by setting ibc_comms_error.
2613 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2614 * immediately tears it down. */
2617 conn->ibc_comms_error = rc;
2618 kiblnd_connreq_done(conn, 0);
2622 kiblnd_active_connect (struct rdma_cm_id *cmid)
2624 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2627 struct rdma_conn_param cp;
2630 unsigned long flags;
2633 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2635 incarnation = peer->ibp_incarnation;
2636 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION : peer->ibp_version;
2638 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2640 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2642 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2643 kiblnd_peer_decref(peer); /* lose cmid's ref */
2647 /* conn "owns" cmid now, so I return success from here on to ensure the
2648 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2651 msg = &conn->ibc_connvars->cv_msg;
2653 memset(msg, 0, sizeof(*msg));
2654 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2655 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2656 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2657 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2659 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2660 0, peer->ibp_nid, incarnation);
2662 memset(&cp, 0, sizeof(cp));
2663 cp.private_data = msg;
2664 cp.private_data_len = msg->ibm_nob;
2665 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2666 cp.initiator_depth = 0;
2667 cp.flow_control = 1;
2668 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2669 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2671 LASSERT(cmid->context == (void *)conn);
2672 LASSERT(conn->ibc_cmid == cmid);
2674 rc = rdma_connect(cmid, &cp);
2676 CERROR("Can't connect to %s: %d\n",
2677 libcfs_nid2str(peer->ibp_nid), rc);
2678 kiblnd_connreq_done(conn, rc);
2679 kiblnd_conn_decref(conn);
2686 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2692 switch (event->event) {
2694 CERROR("Unexpected event: %d, status: %d\n",
2695 event->event, event->status);
2698 case RDMA_CM_EVENT_CONNECT_REQUEST:
2699 /* destroy cmid on failure */
2700 rc = kiblnd_passive_connect(cmid,
2701 (void *)KIBLND_CONN_PARAM(event),
2702 KIBLND_CONN_PARAM_LEN(event));
2703 CDEBUG(D_NET, "connreq: %d\n", rc);
2706 case RDMA_CM_EVENT_ADDR_ERROR:
2707 peer = (kib_peer_t *)cmid->context;
2708 CDEBUG(D_NETERROR, "%s: ADDR ERROR %d\n",
2709 libcfs_nid2str(peer->ibp_nid), event->status);
2710 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2711 kiblnd_peer_decref(peer);
2712 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2714 case RDMA_CM_EVENT_ADDR_RESOLVED:
2715 peer = (kib_peer_t *)cmid->context;
2717 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2718 libcfs_nid2str(peer->ibp_nid), event->status);
2720 if (event->status != 0) {
2721 CDEBUG(D_NETERROR, "Can't resolve address for %s: %d\n",
2722 libcfs_nid2str(peer->ibp_nid), event->status);
2725 rc = rdma_resolve_route(
2726 cmid, *kiblnd_tunables.kib_timeout * 1000);
2729 /* Can't initiate route resolution */
2730 CERROR("Can't resolve route for %s: %d\n",
2731 libcfs_nid2str(peer->ibp_nid), rc);
2733 kiblnd_peer_connect_failed(peer, 1, rc);
2734 kiblnd_peer_decref(peer);
2735 return rc; /* rc != 0 destroys cmid */
2737 case RDMA_CM_EVENT_ROUTE_ERROR:
2738 peer = (kib_peer_t *)cmid->context;
2739 CDEBUG(D_NETERROR, "%s: ROUTE ERROR %d\n",
2740 libcfs_nid2str(peer->ibp_nid), event->status);
2741 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2742 kiblnd_peer_decref(peer);
2743 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2745 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2746 peer = (kib_peer_t *)cmid->context;
2747 CDEBUG(D_NET,"%s Route resolved: %d\n",
2748 libcfs_nid2str(peer->ibp_nid), event->status);
2750 if (event->status == 0)
2751 return kiblnd_active_connect(cmid);
2753 CDEBUG(D_NETERROR, "Can't resolve route for %s: %d\n",
2754 libcfs_nid2str(peer->ibp_nid), event->status);
2755 kiblnd_peer_connect_failed(peer, 1, event->status);
2756 kiblnd_peer_decref(peer);
2757 return event->status; /* rc != 0 destroys cmid */
2759 case RDMA_CM_EVENT_UNREACHABLE:
2760 conn = (kib_conn_t *)cmid->context;
2761 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2762 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2763 CDEBUG(D_NETERROR, "%s: UNREACHABLE %d\n",
2764 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2765 kiblnd_connreq_done(conn, -ENETDOWN);
2766 kiblnd_conn_decref(conn);
2769 case RDMA_CM_EVENT_CONNECT_ERROR:
2770 conn = (kib_conn_t *)cmid->context;
2771 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2772 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2773 CDEBUG(D_NETERROR, "%s: CONNECT ERROR %d\n",
2774 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2775 kiblnd_connreq_done(conn, -ENOTCONN);
2776 kiblnd_conn_decref(conn);
2779 case RDMA_CM_EVENT_REJECTED:
2780 conn = (kib_conn_t *)cmid->context;
2781 switch (conn->ibc_state) {
2785 case IBLND_CONN_PASSIVE_WAIT:
2786 CERROR ("%s: REJECTED %d\n",
2787 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2789 kiblnd_connreq_done(conn, -ECONNRESET);
2792 case IBLND_CONN_ACTIVE_CONNECT:
2793 kiblnd_rejected(conn, event->status,
2794 (void *)KIBLND_CONN_PARAM(event),
2795 KIBLND_CONN_PARAM_LEN(event));
2798 kiblnd_conn_decref(conn);
2801 case RDMA_CM_EVENT_ESTABLISHED:
2802 conn = (kib_conn_t *)cmid->context;
2803 switch (conn->ibc_state) {
2807 case IBLND_CONN_PASSIVE_WAIT:
2808 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2809 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2810 kiblnd_connreq_done(conn, 0);
2813 case IBLND_CONN_ACTIVE_CONNECT:
2814 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2815 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2816 kiblnd_check_connreply(conn,
2817 (void *)KIBLND_CONN_PARAM(event),
2818 KIBLND_CONN_PARAM_LEN(event));
2821 /* net keeps its ref on conn! */
2824 #ifdef HAVE_OFED_RDMA_CMEV_TIMEWAIT_EXIT
2825 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2826 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2829 case RDMA_CM_EVENT_DISCONNECTED:
2830 conn = (kib_conn_t *)cmid->context;
2831 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2832 CERROR("%s DISCONNECTED\n",
2833 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2834 kiblnd_connreq_done(conn, -ECONNRESET);
2836 kiblnd_close_conn(conn, 0);
2838 kiblnd_conn_decref(conn);
2839 cmid->context = NULL;
2842 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2843 LCONSOLE_ERROR_MSG(0x131,
2844 "Received notification of device removal\n"
2845 "Please shutdown LNET to allow this to proceed\n");
2846 /* Can't remove network from underneath LNET for now, so I have
2850 #ifdef HAVE_OFED_RDMA_CMEV_ADDRCHANGE
2851 case RDMA_CM_EVENT_ADDR_CHANGE:
2852 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2859 kiblnd_check_txs (kib_conn_t *conn, struct list_head *txs)
2862 struct list_head *ttmp;
2865 spin_lock(&conn->ibc_lock);
2867 list_for_each (ttmp, txs) {
2868 tx = list_entry (ttmp, kib_tx_t, tx_list);
2870 if (txs != &conn->ibc_active_txs) {
2871 LASSERT (tx->tx_queued);
2873 LASSERT (!tx->tx_queued);
2874 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2877 if (time_after_eq (jiffies, tx->tx_deadline)) {
2879 CERROR("Timed out tx: %s, %lu seconds\n",
2880 kiblnd_queue2str(conn, txs),
2881 cfs_duration_sec(jiffies - tx->tx_deadline));
2886 spin_unlock(&conn->ibc_lock);
2891 kiblnd_conn_timed_out (kib_conn_t *conn)
2893 return kiblnd_check_txs(conn, &conn->ibc_tx_queue) ||
2894 kiblnd_check_txs(conn, &conn->ibc_tx_queue_rsrvd) ||
2895 kiblnd_check_txs(conn, &conn->ibc_tx_queue_nocred) ||
2896 kiblnd_check_txs(conn, &conn->ibc_active_txs);
2900 kiblnd_check_conns (int idx)
2902 struct list_head *peers = &kiblnd_data.kib_peers[idx];
2903 struct list_head *ptmp;
2906 struct list_head *ctmp;
2907 unsigned long flags;
2910 /* NB. We expect to have a look at all the peers and not find any
2911 * rdmas to time out, so we just use a shared lock while we
2913 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2915 list_for_each (ptmp, peers) {
2916 peer = list_entry (ptmp, kib_peer_t, ibp_list);
2918 list_for_each (ctmp, &peer->ibp_conns) {
2919 conn = list_entry (ctmp, kib_conn_t, ibc_list);
2921 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
2923 /* In case we have enough credits to return via a
2924 * NOOP, but there were no non-blocking tx descs
2925 * free to do it last time... */
2926 kiblnd_check_sends(conn);
2928 if (!kiblnd_conn_timed_out(conn))
2931 /* Handle timeout by closing the whole connection. We
2932 * can only be sure RDMA activity has ceased once the
2933 * QP has been modified. */
2935 kiblnd_conn_addref(conn); /* 1 ref for me... */
2937 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2940 CERROR("Timed out RDMA with %s (%lu)\n",
2941 libcfs_nid2str(peer->ibp_nid),
2942 cfs_duration_sec(cfs_time_current() -
2943 peer->ibp_last_alive));
2945 kiblnd_close_conn(conn, -ETIMEDOUT);
2946 kiblnd_conn_decref(conn); /* ...until here */
2948 /* start again now I've dropped the lock */
2953 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2957 kiblnd_disconnect_conn (kib_conn_t *conn)
2959 LASSERT (!in_interrupt());
2960 LASSERT (current == kiblnd_data.kib_connd);
2961 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
2963 rdma_disconnect(conn->ibc_cmid);
2964 kiblnd_finalise_conn(conn);
2966 kiblnd_peer_notify(conn->ibc_peer);
2970 kiblnd_connd (void *arg)
2973 unsigned long flags;
2979 unsigned long deadline = jiffies;
2981 cfs_daemonize ("kiblnd_connd");
2982 cfs_block_allsigs ();
2984 init_waitqueue_entry (&wait, current);
2985 kiblnd_data.kib_connd = current;
2987 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2989 while (!kiblnd_data.kib_shutdown) {
2993 if (!list_empty (&kiblnd_data.kib_connd_zombies)) {
2994 conn = list_entry (kiblnd_data.kib_connd_zombies.next,
2995 kib_conn_t, ibc_list);
2996 list_del(&conn->ibc_list);
2998 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3001 kiblnd_destroy_conn(conn);
3003 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
3006 if (!list_empty (&kiblnd_data.kib_connd_conns)) {
3007 conn = list_entry (kiblnd_data.kib_connd_conns.next,
3008 kib_conn_t, ibc_list);
3009 list_del(&conn->ibc_list);
3011 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3014 kiblnd_disconnect_conn(conn);
3015 kiblnd_conn_decref(conn);
3017 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
3020 /* careful with the jiffy wrap... */
3021 timeout = (int)(deadline - jiffies);
3025 int chunk = kiblnd_data.kib_peer_hash_size;
3027 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3030 /* Time to check for RDMA timeouts on a few more
3031 * peers: I do checks every 'p' seconds on a
3032 * proportion of the peer table and I need to check
3033 * every connection 'n' times within a timeout
3034 * interval, to ensure I detect a timeout on any
3035 * connection within (n+1)/n times the timeout
3038 if (*kiblnd_tunables.kib_timeout > n * p)
3039 chunk = (chunk * n * p) /
3040 *kiblnd_tunables.kib_timeout;
3044 for (i = 0; i < chunk; i++) {
3045 kiblnd_check_conns(peer_index);
3046 peer_index = (peer_index + 1) %
3047 kiblnd_data.kib_peer_hash_size;
3051 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3057 /* Nothing to do for 'timeout' */
3058 set_current_state (TASK_INTERRUPTIBLE);
3059 add_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
3060 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3062 schedule_timeout (timeout);
3064 set_current_state (TASK_RUNNING);
3065 remove_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
3066 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
3069 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
3071 kiblnd_thread_fini();
3076 kiblnd_qp_event(struct ib_event *event, void *arg)
3078 kib_conn_t *conn = arg;
3080 switch (event->event) {
3081 case IB_EVENT_COMM_EST:
3082 CDEBUG(D_NET, "%s established\n",
3083 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3087 CERROR("%s: Async QP event type %d\n",
3088 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3094 kiblnd_complete (struct ib_wc *wc)
3096 switch (kiblnd_wreqid2type(wc->wr_id)) {
3100 case IBLND_WID_RDMA:
3101 /* We only get RDMA completion notification if it fails. All
3102 * subsequent work items, including the final SEND will fail
3103 * too. However we can't print out any more info about the
3104 * failing RDMA because 'tx' might be back on the idle list or
3105 * even reused already if we didn't manage to post all our work
3107 CDEBUG(D_NETERROR, "RDMA (tx: %p) failed: %d\n",
3108 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3112 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3116 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3123 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3125 /* NB I'm not allowed to schedule this conn once its refcount has
3126 * reached 0. Since fundamentally I'm racing with scheduler threads
3127 * consuming my CQ I could be called after all completions have
3128 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3129 * and this CQ is about to be destroyed so I NOOP. */
3130 kib_conn_t *conn = (kib_conn_t *)arg;
3131 unsigned long flags;
3133 LASSERT (cq == conn->ibc_cq);
3135 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3137 conn->ibc_ready = 1;
3139 if (!conn->ibc_scheduled &&
3140 (conn->ibc_nrx > 0 ||
3141 conn->ibc_nsends_posted > 0)) {
3142 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3143 conn->ibc_scheduled = 1;
3144 list_add_tail(&conn->ibc_sched_list,
3145 &kiblnd_data.kib_sched_conns);
3146 wake_up(&kiblnd_data.kib_sched_waitq);
3149 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3153 kiblnd_cq_event(struct ib_event *event, void *arg)
3155 kib_conn_t *conn = arg;
3157 CERROR("%s: async CQ event type %d\n",
3158 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3162 kiblnd_scheduler(void *arg)
3164 long id = (long)arg;
3167 unsigned long flags;
3174 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3175 cfs_daemonize(name);
3176 cfs_block_allsigs();
3178 init_waitqueue_entry(&wait, current);
3180 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3182 while (!kiblnd_data.kib_shutdown) {
3183 if (busy_loops++ >= IBLND_RESCHED) {
3184 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3190 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3195 if (!list_empty(&kiblnd_data.kib_sched_conns)) {
3196 conn = list_entry(kiblnd_data.kib_sched_conns.next,
3197 kib_conn_t, ibc_sched_list);
3198 /* take over kib_sched_conns' ref on conn... */
3199 LASSERT(conn->ibc_scheduled);
3200 list_del(&conn->ibc_sched_list);
3201 conn->ibc_ready = 0;
3203 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3206 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3208 rc = ib_req_notify_cq(conn->ibc_cq,
3211 CWARN("%s: ib_req_notify_cq failed: %d, "
3212 "closing connection\n",
3213 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3214 kiblnd_close_conn(conn, -EIO);
3215 kiblnd_conn_decref(conn);
3216 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3220 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3224 CWARN("%s: ib_poll_cq failed: %d, "
3225 "closing connection\n",
3226 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3227 kiblnd_close_conn(conn, -EIO);
3228 kiblnd_conn_decref(conn);
3229 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3233 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3236 if (rc != 0 || conn->ibc_ready) {
3237 /* There may be another completion waiting; get
3238 * another scheduler to check while I handle
3240 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3241 list_add_tail(&conn->ibc_sched_list,
3242 &kiblnd_data.kib_sched_conns);
3243 wake_up(&kiblnd_data.kib_sched_waitq);
3245 conn->ibc_scheduled = 0;
3249 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3252 kiblnd_complete(&wc);
3254 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3258 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3265 set_current_state(TASK_INTERRUPTIBLE);
3266 add_wait_queue_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3267 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3272 remove_wait_queue(&kiblnd_data.kib_sched_waitq, &wait);
3273 set_current_state(TASK_RUNNING);
3274 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3277 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3279 kiblnd_thread_fini();