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.
30 * Copyright (c) 2012, Intel Corporation.
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>
43 static void kiblnd_peer_alive(kib_peer_t *peer);
44 static void kiblnd_peer_connect_failed(kib_peer_t *peer, int active, int error);
45 static void kiblnd_check_sends(kib_conn_t *conn);
46 static void kiblnd_init_tx_msg(lnet_ni_t *ni, kib_tx_t *tx,
47 int type, int body_nob);
48 static int kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
49 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie);
50 static void kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn);
51 static void kiblnd_queue_tx(kib_tx_t *tx, kib_conn_t *conn);
54 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
56 lnet_msg_t *lntmsg[2];
57 kib_net_t *net = ni->ni_data;
61 LASSERT (net != NULL);
62 LASSERT (!in_interrupt());
63 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
64 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
65 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
66 LASSERT (tx->tx_pool != NULL);
68 kiblnd_unmap_tx(ni, tx);
70 /* tx may have up to 2 lnet msgs to finalise */
71 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
72 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
75 if (tx->tx_conn != NULL) {
76 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
78 kiblnd_conn_decref(tx->tx_conn);
85 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
87 /* delay finalize until my descs have been freed */
88 for (i = 0; i < 2; i++) {
89 if (lntmsg[i] == NULL)
92 lnet_finalize(ni, lntmsg[i], rc);
97 kiblnd_txlist_done(lnet_ni_t *ni, struct list_head *txlist, int status)
101 while (!list_empty(txlist)) {
102 tx = list_entry(txlist->next, kib_tx_t, tx_list);
104 list_del(&tx->tx_list);
107 tx->tx_status = status;
108 kiblnd_tx_done(ni, tx);
113 kiblnd_get_idle_tx(lnet_ni_t *ni, lnet_nid_t target)
115 kib_net_t *net = (kib_net_t *)ni->ni_data;
116 struct list_head *node;
118 kib_tx_poolset_t *tps;
120 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target)];
121 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
124 tx = container_of(node, kib_tx_t, tx_list);
126 LASSERT (tx->tx_nwrq == 0);
127 LASSERT (!tx->tx_queued);
128 LASSERT (tx->tx_sending == 0);
129 LASSERT (!tx->tx_waiting);
130 LASSERT (tx->tx_status == 0);
131 LASSERT (tx->tx_conn == NULL);
132 LASSERT (tx->tx_lntmsg[0] == NULL);
133 LASSERT (tx->tx_lntmsg[1] == NULL);
134 LASSERT (tx->tx_u.pmr == NULL);
135 LASSERT (tx->tx_nfrags == 0);
141 kiblnd_drop_rx(kib_rx_t *rx)
143 kib_conn_t *conn = rx->rx_conn;
144 struct kib_sched_info *sched = conn->ibc_sched;
147 spin_lock_irqsave(&sched->ibs_lock, flags);
148 LASSERT(conn->ibc_nrx > 0);
150 spin_unlock_irqrestore(&sched->ibs_lock, flags);
152 kiblnd_conn_decref(conn);
156 kiblnd_post_rx (kib_rx_t *rx, int credit)
158 kib_conn_t *conn = rx->rx_conn;
159 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
160 struct ib_recv_wr *bad_wrq = NULL;
164 LASSERT (net != NULL);
165 LASSERT (!in_interrupt());
166 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
167 credit == IBLND_POSTRX_PEER_CREDIT ||
168 credit == IBLND_POSTRX_RSRVD_CREDIT);
170 mr = kiblnd_find_dma_mr(conn->ibc_hdev, rx->rx_msgaddr, IBLND_MSG_SIZE);
171 LASSERT (mr != NULL);
173 rx->rx_sge.lkey = mr->lkey;
174 rx->rx_sge.addr = rx->rx_msgaddr;
175 rx->rx_sge.length = IBLND_MSG_SIZE;
177 rx->rx_wrq.next = NULL;
178 rx->rx_wrq.sg_list = &rx->rx_sge;
179 rx->rx_wrq.num_sge = 1;
180 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
182 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
183 LASSERT (rx->rx_nob >= 0); /* not posted */
185 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
186 kiblnd_drop_rx(rx); /* No more posts for this rx */
190 rx->rx_nob = -1; /* flag posted */
192 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
194 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
195 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
199 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
203 kiblnd_close_conn(conn, rc);
204 kiblnd_drop_rx(rx); /* No more posts for this rx */
208 if (credit == IBLND_POSTRX_NO_CREDIT)
211 spin_lock(&conn->ibc_lock);
212 if (credit == IBLND_POSTRX_PEER_CREDIT)
213 conn->ibc_outstanding_credits++;
215 conn->ibc_reserved_credits++;
216 spin_unlock(&conn->ibc_lock);
218 kiblnd_check_sends(conn);
223 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
225 struct list_head *tmp;
227 list_for_each(tmp, &conn->ibc_active_txs) {
228 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
230 LASSERT(!tx->tx_queued);
231 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
233 if (tx->tx_cookie != cookie)
236 if (tx->tx_waiting &&
237 tx->tx_msg->ibm_type == txtype)
240 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
241 tx->tx_waiting ? "" : "NOT ",
242 tx->tx_msg->ibm_type, txtype);
248 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
251 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
254 spin_lock(&conn->ibc_lock);
256 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
258 spin_unlock(&conn->ibc_lock);
260 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
261 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
262 kiblnd_close_conn(conn, -EPROTO);
266 if (tx->tx_status == 0) { /* success so far */
267 if (status < 0) { /* failed? */
268 tx->tx_status = status;
269 } else if (txtype == IBLND_MSG_GET_REQ) {
270 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
276 idle = !tx->tx_queued && (tx->tx_sending == 0);
278 list_del(&tx->tx_list);
280 spin_unlock(&conn->ibc_lock);
283 kiblnd_tx_done(ni, tx);
287 kiblnd_send_completion(kib_conn_t *conn, int type, int status, __u64 cookie)
289 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
290 kib_tx_t *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
293 CERROR("Can't get tx for completion %x for %s\n",
294 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
298 tx->tx_msg->ibm_u.completion.ibcm_status = status;
299 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
300 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
302 kiblnd_queue_tx(tx, conn);
306 kiblnd_handle_rx (kib_rx_t *rx)
308 kib_msg_t *msg = rx->rx_msg;
309 kib_conn_t *conn = rx->rx_conn;
310 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
311 int credits = msg->ibm_credits;
317 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
319 CDEBUG (D_NET, "Received %x[%d] from %s\n",
320 msg->ibm_type, credits,
321 libcfs_nid2str(conn->ibc_peer->ibp_nid));
324 /* Have I received credits that will let me send? */
325 spin_lock(&conn->ibc_lock);
327 if (conn->ibc_credits + credits >
328 IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
329 rc2 = conn->ibc_credits;
330 spin_unlock(&conn->ibc_lock);
332 CERROR("Bad credits from %s: %d + %d > %d\n",
333 libcfs_nid2str(conn->ibc_peer->ibp_nid),
335 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
337 kiblnd_close_conn(conn, -EPROTO);
338 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
342 conn->ibc_credits += credits;
344 /* This ensures the credit taken by NOOP can be returned */
345 if (msg->ibm_type == IBLND_MSG_NOOP &&
346 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
347 conn->ibc_outstanding_credits++;
349 spin_unlock(&conn->ibc_lock);
350 kiblnd_check_sends(conn);
353 switch (msg->ibm_type) {
355 CERROR("Bad IBLND message type %x from %s\n",
356 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
357 post_credit = IBLND_POSTRX_NO_CREDIT;
362 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
363 post_credit = IBLND_POSTRX_NO_CREDIT;
367 if (credits != 0) /* credit already posted */
368 post_credit = IBLND_POSTRX_NO_CREDIT;
369 else /* a keepalive NOOP */
370 post_credit = IBLND_POSTRX_PEER_CREDIT;
373 case IBLND_MSG_IMMEDIATE:
374 post_credit = IBLND_POSTRX_DONT_POST;
375 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
376 msg->ibm_srcnid, rx, 0);
377 if (rc < 0) /* repost on error */
378 post_credit = IBLND_POSTRX_PEER_CREDIT;
381 case IBLND_MSG_PUT_REQ:
382 post_credit = IBLND_POSTRX_DONT_POST;
383 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
384 msg->ibm_srcnid, rx, 1);
385 if (rc < 0) /* repost on error */
386 post_credit = IBLND_POSTRX_PEER_CREDIT;
389 case IBLND_MSG_PUT_NAK:
390 CWARN ("PUT_NACK from %s\n",
391 libcfs_nid2str(conn->ibc_peer->ibp_nid));
392 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
393 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
394 msg->ibm_u.completion.ibcm_status,
395 msg->ibm_u.completion.ibcm_cookie);
398 case IBLND_MSG_PUT_ACK:
399 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
401 spin_lock(&conn->ibc_lock);
402 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
403 msg->ibm_u.putack.ibpam_src_cookie);
405 list_del(&tx->tx_list);
406 spin_unlock(&conn->ibc_lock);
409 CERROR("Unmatched PUT_ACK from %s\n",
410 libcfs_nid2str(conn->ibc_peer->ibp_nid));
415 LASSERT (tx->tx_waiting);
416 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
417 * (a) I can overwrite tx_msg since my peer has received it!
418 * (b) tx_waiting set tells tx_complete() it's not done. */
420 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
422 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
423 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
424 &msg->ibm_u.putack.ibpam_rd,
425 msg->ibm_u.putack.ibpam_dst_cookie);
427 CERROR("Can't setup rdma for PUT to %s: %d\n",
428 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
430 spin_lock(&conn->ibc_lock);
431 tx->tx_waiting = 0; /* clear waiting and queue atomically */
432 kiblnd_queue_tx_locked(tx, conn);
433 spin_unlock(&conn->ibc_lock);
436 case IBLND_MSG_PUT_DONE:
437 post_credit = IBLND_POSTRX_PEER_CREDIT;
438 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
439 msg->ibm_u.completion.ibcm_status,
440 msg->ibm_u.completion.ibcm_cookie);
443 case IBLND_MSG_GET_REQ:
444 post_credit = IBLND_POSTRX_DONT_POST;
445 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
446 msg->ibm_srcnid, rx, 1);
447 if (rc < 0) /* repost on error */
448 post_credit = IBLND_POSTRX_PEER_CREDIT;
451 case IBLND_MSG_GET_DONE:
452 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
453 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
454 msg->ibm_u.completion.ibcm_status,
455 msg->ibm_u.completion.ibcm_cookie);
459 if (rc < 0) /* protocol error */
460 kiblnd_close_conn(conn, rc);
462 if (post_credit != IBLND_POSTRX_DONT_POST)
463 kiblnd_post_rx(rx, post_credit);
467 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
469 kib_msg_t *msg = rx->rx_msg;
470 kib_conn_t *conn = rx->rx_conn;
471 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
472 kib_net_t *net = ni->ni_data;
476 LASSERT (net != NULL);
477 LASSERT (rx->rx_nob < 0); /* was posted */
478 rx->rx_nob = 0; /* isn't now */
480 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
483 if (status != IB_WC_SUCCESS) {
484 CNETERR("Rx from %s failed: %d\n",
485 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
492 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
494 CERROR ("Error %d unpacking rx from %s\n",
495 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
499 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
500 msg->ibm_dstnid != ni->ni_nid ||
501 msg->ibm_srcstamp != conn->ibc_incarnation ||
502 msg->ibm_dststamp != net->ibn_incarnation) {
503 CERROR ("Stale rx from %s\n",
504 libcfs_nid2str(conn->ibc_peer->ibp_nid));
509 /* set time last known alive */
510 kiblnd_peer_alive(conn->ibc_peer);
512 /* racing with connection establishment/teardown! */
514 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
515 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
518 write_lock_irqsave(g_lock, flags);
519 /* must check holding global lock to eliminate race */
520 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
521 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
522 write_unlock_irqrestore(g_lock, flags);
525 write_unlock_irqrestore(g_lock, flags);
527 kiblnd_handle_rx(rx);
531 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
532 kiblnd_close_conn(conn, err);
534 kiblnd_drop_rx(rx); /* Don't re-post rx. */
538 kiblnd_kvaddr_to_page (unsigned long vaddr)
542 if (is_vmalloc_addr((void *)vaddr)) {
543 page = vmalloc_to_page ((void *)vaddr);
544 LASSERT (page != NULL);
547 #ifdef CONFIG_HIGHMEM
548 if (vaddr >= PKMAP_BASE &&
549 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
550 /* No highmem pages only used for bulk (kiov) I/O */
551 CERROR("find page for address in highmem\n");
555 page = virt_to_page (vaddr);
556 LASSERT (page != NULL);
561 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
564 __u64 *pages = tx->tx_pages;
565 kib_fmr_poolset_t *fps;
572 LASSERT(tx->tx_pool != NULL);
573 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
575 hdev = tx->tx_pool->tpo_hdev;
577 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
578 for (size = 0; size < rd->rd_frags[i].rf_nob;
579 size += hdev->ibh_page_size) {
580 pages[npages ++] = (rd->rd_frags[i].rf_addr &
581 hdev->ibh_page_mask) + size;
585 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
587 fps = net->ibn_fmr_ps[cpt];
588 rc = kiblnd_fmr_pool_map(fps, pages, npages, 0, &tx->tx_u.fmr);
590 CERROR ("Can't map %d pages: %d\n", npages, rc);
594 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
596 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.fmr.fmr_pfmr->fmr->rkey :
597 tx->tx_u.fmr.fmr_pfmr->fmr->lkey;
598 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
599 rd->rd_frags[0].rf_nob = nob;
606 kiblnd_pmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
609 kib_pmr_poolset_t *pps;
614 LASSERT(tx->tx_pool != NULL);
615 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
617 hdev = tx->tx_pool->tpo_hdev;
619 iova = rd->rd_frags[0].rf_addr & ~hdev->ibh_page_mask;
621 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
623 pps = net->ibn_pmr_ps[cpt];
624 rc = kiblnd_pmr_pool_map(pps, hdev, rd, &iova, &tx->tx_u.pmr);
626 CERROR("Failed to create MR by phybuf: %d\n", rc);
630 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
632 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.pmr->pmr_mr->rkey :
633 tx->tx_u.pmr->pmr_mr->lkey;
635 rd->rd_frags[0].rf_addr = iova;
636 rd->rd_frags[0].rf_nob = nob;
642 kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
644 kib_net_t *net = ni->ni_data;
646 LASSERT(net != NULL);
648 if (net->ibn_fmr_ps != NULL && tx->tx_u.fmr.fmr_pfmr != NULL) {
649 kiblnd_fmr_pool_unmap(&tx->tx_u.fmr, tx->tx_status);
650 tx->tx_u.fmr.fmr_pfmr = NULL;
652 } else if (net->ibn_pmr_ps != NULL && tx->tx_u.pmr != NULL) {
653 kiblnd_pmr_pool_unmap(tx->tx_u.pmr);
657 if (tx->tx_nfrags != 0) {
658 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
659 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
665 kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx,
666 kib_rdma_desc_t *rd, int nfrags)
668 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
669 kib_net_t *net = ni->ni_data;
670 struct ib_mr *mr = NULL;
674 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
676 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
677 tx->tx_nfrags = nfrags;
680 kiblnd_dma_map_sg(hdev->ibh_ibdev,
681 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
683 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
684 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
685 hdev->ibh_ibdev, &tx->tx_frags[i]);
686 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
687 hdev->ibh_ibdev, &tx->tx_frags[i]);
688 nob += rd->rd_frags[i].rf_nob;
691 /* looking for pre-mapping MR */
692 mr = kiblnd_find_rd_dma_mr(hdev, rd);
694 /* found pre-mapping MR */
695 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
699 if (net->ibn_fmr_ps != NULL)
700 return kiblnd_fmr_map_tx(net, tx, rd, nob);
701 else if (net->ibn_pmr_ps != NULL)
702 return kiblnd_pmr_map_tx(net, tx, rd, nob);
709 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
710 unsigned int niov, struct iovec *iov, int offset, int nob)
712 kib_net_t *net = ni->ni_data;
714 struct scatterlist *sg;
721 LASSERT (net != NULL);
723 while (offset >= iov->iov_len) {
724 offset -= iov->iov_len;
734 vaddr = ((unsigned long)iov->iov_base) + offset;
735 page_offset = vaddr & (PAGE_SIZE - 1);
736 page = kiblnd_kvaddr_to_page(vaddr);
738 CERROR ("Can't find page\n");
742 fragnob = min((int)(iov->iov_len - offset), nob);
743 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
745 sg_set_page(sg, page, fragnob, page_offset);
748 if (offset + fragnob < iov->iov_len) {
758 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
762 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
763 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
765 kib_net_t *net = ni->ni_data;
766 struct scatterlist *sg;
769 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
773 LASSERT (net != NULL);
775 while (offset >= kiov->kiov_len) {
776 offset -= kiov->kiov_len;
786 fragnob = min((int)(kiov->kiov_len - offset), nob);
788 sg_set_page(sg, kiov->kiov_page, fragnob,
789 kiov->kiov_offset + offset);
798 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
802 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
803 __must_hold(&conn->ibc_lock)
805 kib_msg_t *msg = tx->tx_msg;
806 kib_peer_t *peer = conn->ibc_peer;
807 int ver = conn->ibc_version;
810 struct ib_send_wr *bad_wrq;
812 LASSERT (tx->tx_queued);
813 /* We rely on this for QP sizing */
814 LASSERT (tx->tx_nwrq > 0);
815 LASSERT (tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
817 LASSERT (credit == 0 || credit == 1);
818 LASSERT (conn->ibc_outstanding_credits >= 0);
819 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
820 LASSERT (conn->ibc_credits >= 0);
821 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
823 if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
824 /* tx completions outstanding... */
825 CDEBUG(D_NET, "%s: posted enough\n",
826 libcfs_nid2str(peer->ibp_nid));
830 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
831 CDEBUG(D_NET, "%s: no credits\n",
832 libcfs_nid2str(peer->ibp_nid));
836 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
837 conn->ibc_credits == 1 && /* last credit reserved */
838 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
839 CDEBUG(D_NET, "%s: not using last credit\n",
840 libcfs_nid2str(peer->ibp_nid));
844 /* NB don't drop ibc_lock before bumping tx_sending */
845 list_del(&tx->tx_list);
848 if (msg->ibm_type == IBLND_MSG_NOOP &&
849 (!kiblnd_need_noop(conn) || /* redundant NOOP */
850 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
851 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
852 /* OK to drop when posted enough NOOPs, since
853 * kiblnd_check_sends will queue NOOP again when
854 * posted NOOPs complete */
855 spin_unlock(&conn->ibc_lock);
856 kiblnd_tx_done(peer->ibp_ni, tx);
857 spin_lock(&conn->ibc_lock);
858 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
859 libcfs_nid2str(peer->ibp_nid),
860 conn->ibc_noops_posted);
864 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
865 peer->ibp_nid, conn->ibc_incarnation);
867 conn->ibc_credits -= credit;
868 conn->ibc_outstanding_credits = 0;
869 conn->ibc_nsends_posted++;
870 if (msg->ibm_type == IBLND_MSG_NOOP)
871 conn->ibc_noops_posted++;
873 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
874 * PUT. If so, it was first queued here as a PUT_REQ, sent and
875 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
876 * and then re-queued here. It's (just) possible that
877 * tx_sending is non-zero if we've not done the tx_complete()
878 * from the first send; hence the ++ rather than = below. */
880 list_add(&tx->tx_list, &conn->ibc_active_txs);
882 /* I'm still holding ibc_lock! */
883 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
885 } else if (tx->tx_pool->tpo_pool.po_failed ||
886 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
887 /* close_conn will launch failover */
890 rc = ib_post_send(conn->ibc_cmid->qp,
891 tx->tx_wrq, &bad_wrq);
894 conn->ibc_last_send = jiffies;
899 /* NB credits are transferred in the actual
900 * message, which can only be the last work item */
901 conn->ibc_credits += credit;
902 conn->ibc_outstanding_credits += msg->ibm_credits;
903 conn->ibc_nsends_posted--;
904 if (msg->ibm_type == IBLND_MSG_NOOP)
905 conn->ibc_noops_posted--;
911 done = (tx->tx_sending == 0);
913 list_del(&tx->tx_list);
915 spin_unlock(&conn->ibc_lock);
917 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
918 CERROR("Error %d posting transmit to %s\n",
919 rc, libcfs_nid2str(peer->ibp_nid));
921 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
922 rc, libcfs_nid2str(peer->ibp_nid));
924 kiblnd_close_conn(conn, rc);
927 kiblnd_tx_done(peer->ibp_ni, tx);
929 spin_lock(&conn->ibc_lock);
935 kiblnd_check_sends (kib_conn_t *conn)
937 int ver = conn->ibc_version;
938 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
941 /* Don't send anything until after the connection is established */
942 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
943 CDEBUG(D_NET, "%s too soon\n",
944 libcfs_nid2str(conn->ibc_peer->ibp_nid));
948 spin_lock(&conn->ibc_lock);
950 LASSERT (conn->ibc_nsends_posted <= IBLND_CONCURRENT_SENDS(ver));
951 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
952 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
953 LASSERT (conn->ibc_reserved_credits >= 0);
955 while (conn->ibc_reserved_credits > 0 &&
956 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
957 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
959 list_del(&tx->tx_list);
960 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
961 conn->ibc_reserved_credits--;
964 if (kiblnd_need_noop(conn)) {
965 spin_unlock(&conn->ibc_lock);
967 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
969 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
971 spin_lock(&conn->ibc_lock);
973 kiblnd_queue_tx_locked(tx, conn);
976 kiblnd_conn_addref(conn); /* 1 ref for me.... (see b21911) */
981 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
983 tx = list_entry(conn->ibc_tx_queue_nocred.next,
985 } else if (!list_empty(&conn->ibc_tx_noops)) {
986 LASSERT (!IBLND_OOB_CAPABLE(ver));
988 tx = list_entry(conn->ibc_tx_noops.next,
990 } else if (!list_empty(&conn->ibc_tx_queue)) {
992 tx = list_entry(conn->ibc_tx_queue.next,
997 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1001 spin_unlock(&conn->ibc_lock);
1003 kiblnd_conn_decref(conn); /* ...until here */
1007 kiblnd_tx_complete (kib_tx_t *tx, int status)
1009 int failed = (status != IB_WC_SUCCESS);
1010 kib_conn_t *conn = tx->tx_conn;
1013 LASSERT (tx->tx_sending > 0);
1016 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1017 CNETERR("Tx -> %s cookie "LPX64
1018 " sending %d waiting %d: failed %d\n",
1019 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1020 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1023 kiblnd_close_conn(conn, -EIO);
1025 kiblnd_peer_alive(conn->ibc_peer);
1028 spin_lock(&conn->ibc_lock);
1030 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1031 * gets to free it, which also drops its ref on 'conn'. */
1034 conn->ibc_nsends_posted--;
1035 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1036 conn->ibc_noops_posted--;
1039 tx->tx_waiting = 0; /* don't wait for peer */
1040 tx->tx_status = -EIO;
1043 idle = (tx->tx_sending == 0) && /* This is the final callback */
1044 !tx->tx_waiting && /* Not waiting for peer */
1045 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1047 list_del(&tx->tx_list);
1049 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1051 spin_unlock(&conn->ibc_lock);
1054 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1056 kiblnd_check_sends(conn);
1058 kiblnd_conn_decref(conn); /* ...until here */
1062 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1064 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
1065 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1066 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1067 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1070 LASSERT (tx->tx_nwrq >= 0);
1071 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1072 LASSERT (nob <= IBLND_MSG_SIZE);
1074 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1076 mr = kiblnd_find_dma_mr(hdev, tx->tx_msgaddr, nob);
1077 LASSERT (mr != NULL);
1079 sge->lkey = mr->lkey;
1080 sge->addr = tx->tx_msgaddr;
1083 memset(wrq, 0, sizeof(*wrq));
1086 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1089 wrq->opcode = IB_WR_SEND;
1090 wrq->send_flags = IB_SEND_SIGNALED;
1096 kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
1097 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1099 kib_msg_t *ibmsg = tx->tx_msg;
1100 kib_rdma_desc_t *srcrd = tx->tx_rd;
1101 struct ib_sge *sge = &tx->tx_sge[0];
1102 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1108 LASSERT (!in_interrupt());
1109 LASSERT (tx->tx_nwrq == 0);
1110 LASSERT (type == IBLND_MSG_GET_DONE ||
1111 type == IBLND_MSG_PUT_DONE);
1113 srcidx = dstidx = 0;
1116 if (srcidx >= srcrd->rd_nfrags) {
1117 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1122 if (dstidx == dstrd->rd_nfrags) {
1123 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1128 if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
1129 CERROR("RDMA too fragmented for %s (%d): "
1130 "%d/%d src %d/%d dst frags\n",
1131 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1132 IBLND_RDMA_FRAGS(conn->ibc_version),
1133 srcidx, srcrd->rd_nfrags,
1134 dstidx, dstrd->rd_nfrags);
1139 wrknob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1140 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1142 sge = &tx->tx_sge[tx->tx_nwrq];
1143 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1144 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1145 sge->length = wrknob;
1147 wrq = &tx->tx_wrq[tx->tx_nwrq];
1149 wrq->next = wrq + 1;
1150 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1153 wrq->opcode = IB_WR_RDMA_WRITE;
1154 wrq->send_flags = 0;
1156 wrq->wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1157 wrq->wr.rdma.rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1159 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1160 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1169 if (rc < 0) /* no RDMA if completing with failure */
1172 ibmsg->ibm_u.completion.ibcm_status = rc;
1173 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1174 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1175 type, sizeof (kib_completion_msg_t));
1181 kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn)
1183 struct list_head *q;
1185 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1186 LASSERT(!tx->tx_queued); /* not queued for sending already */
1187 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1190 tx->tx_deadline = jiffies + (*kiblnd_tunables.kib_timeout * HZ);
1192 if (tx->tx_conn == NULL) {
1193 kiblnd_conn_addref(conn);
1195 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1197 /* PUT_DONE first attached to conn as a PUT_REQ */
1198 LASSERT (tx->tx_conn == conn);
1199 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1202 switch (tx->tx_msg->ibm_type) {
1206 case IBLND_MSG_PUT_REQ:
1207 case IBLND_MSG_GET_REQ:
1208 q = &conn->ibc_tx_queue_rsrvd;
1211 case IBLND_MSG_PUT_NAK:
1212 case IBLND_MSG_PUT_ACK:
1213 case IBLND_MSG_PUT_DONE:
1214 case IBLND_MSG_GET_DONE:
1215 q = &conn->ibc_tx_queue_nocred;
1218 case IBLND_MSG_NOOP:
1219 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1220 q = &conn->ibc_tx_queue_nocred;
1222 q = &conn->ibc_tx_noops;
1225 case IBLND_MSG_IMMEDIATE:
1226 q = &conn->ibc_tx_queue;
1230 list_add_tail(&tx->tx_list, q);
1234 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1236 spin_lock(&conn->ibc_lock);
1237 kiblnd_queue_tx_locked(tx, conn);
1238 spin_unlock(&conn->ibc_lock);
1240 kiblnd_check_sends(conn);
1243 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1244 struct sockaddr_in *srcaddr,
1245 struct sockaddr_in *dstaddr,
1248 unsigned short port;
1251 #ifdef HAVE_OFED_RDMA_SET_REUSEADDR
1252 /* allow the port to be reused */
1253 rc = rdma_set_reuseaddr(cmid, 1);
1255 CERROR("Unable to set reuse on cmid: %d\n", rc);
1260 /* look for a free privileged port */
1261 for (port = PROT_SOCK-1; port > 0; port--) {
1262 srcaddr->sin_port = htons(port);
1263 rc = rdma_resolve_addr(cmid,
1264 (struct sockaddr *)srcaddr,
1265 (struct sockaddr *)dstaddr,
1268 CDEBUG(D_NET, "bound to port %hu\n", port);
1270 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1271 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1278 CERROR("Failed to bind to a free privileged port\n");
1279 #ifndef HAVE_OFED_RDMA_SET_REUSEADDR
1280 CERROR("You may need IB verbs that supports rdma_set_reuseaddr()\n");
1286 kiblnd_connect_peer (kib_peer_t *peer)
1288 struct rdma_cm_id *cmid;
1290 kib_net_t *net = peer->ibp_ni->ni_data;
1291 struct sockaddr_in srcaddr;
1292 struct sockaddr_in dstaddr;
1295 LASSERT (net != NULL);
1296 LASSERT (peer->ibp_connecting > 0);
1298 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP,
1302 CERROR("Can't create CMID for %s: %ld\n",
1303 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1309 memset(&srcaddr, 0, sizeof(srcaddr));
1310 srcaddr.sin_family = AF_INET;
1311 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1313 memset(&dstaddr, 0, sizeof(dstaddr));
1314 dstaddr.sin_family = AF_INET;
1315 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1316 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1318 kiblnd_peer_addref(peer); /* cmid's ref */
1320 if (*kiblnd_tunables.kib_use_priv_port) {
1321 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1322 *kiblnd_tunables.kib_timeout * 1000);
1324 rc = rdma_resolve_addr(cmid,
1325 (struct sockaddr *)&srcaddr,
1326 (struct sockaddr *)&dstaddr,
1327 *kiblnd_tunables.kib_timeout * 1000);
1330 /* Can't initiate address resolution: */
1331 CERROR("Can't resolve addr for %s: %d\n",
1332 libcfs_nid2str(peer->ibp_nid), rc);
1336 LASSERT (cmid->device != NULL);
1337 CDEBUG(D_NET, "%s: connection bound to %s:%u.%u.%u.%u:%s\n",
1338 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1339 HIPQUAD(dev->ibd_ifip), cmid->device->name);
1344 kiblnd_peer_decref(peer); /* cmid's ref */
1345 rdma_destroy_id(cmid);
1347 kiblnd_peer_connect_failed(peer, 1, rc);
1351 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1356 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1357 unsigned long flags;
1360 /* If I get here, I've committed to send, so I complete the tx with
1361 * failure on any problems */
1363 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1364 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1366 /* First time, just use a read lock since I expect to find my peer
1368 read_lock_irqsave(g_lock, flags);
1370 peer = kiblnd_find_peer_locked(nid);
1371 if (peer != NULL && !list_empty(&peer->ibp_conns)) {
1372 /* Found a peer with an established connection */
1373 conn = kiblnd_get_conn_locked(peer);
1374 kiblnd_conn_addref(conn); /* 1 ref for me... */
1376 read_unlock_irqrestore(g_lock, flags);
1379 kiblnd_queue_tx(tx, conn);
1380 kiblnd_conn_decref(conn); /* ...to here */
1384 read_unlock(g_lock);
1385 /* Re-try with a write lock */
1388 peer = kiblnd_find_peer_locked(nid);
1390 if (list_empty(&peer->ibp_conns)) {
1391 /* found a peer, but it's still connecting... */
1392 LASSERT (peer->ibp_connecting != 0 ||
1393 peer->ibp_accepting != 0);
1395 list_add_tail(&tx->tx_list,
1396 &peer->ibp_tx_queue);
1397 write_unlock_irqrestore(g_lock, flags);
1399 conn = kiblnd_get_conn_locked(peer);
1400 kiblnd_conn_addref(conn); /* 1 ref for me... */
1402 write_unlock_irqrestore(g_lock, flags);
1405 kiblnd_queue_tx(tx, conn);
1406 kiblnd_conn_decref(conn); /* ...to here */
1411 write_unlock_irqrestore(g_lock, flags);
1413 /* Allocate a peer ready to add to the peer table and retry */
1414 rc = kiblnd_create_peer(ni, &peer, nid);
1416 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1418 tx->tx_status = -EHOSTUNREACH;
1420 kiblnd_tx_done(ni, tx);
1425 write_lock_irqsave(g_lock, flags);
1427 peer2 = kiblnd_find_peer_locked(nid);
1428 if (peer2 != NULL) {
1429 if (list_empty(&peer2->ibp_conns)) {
1430 /* found a peer, but it's still connecting... */
1431 LASSERT (peer2->ibp_connecting != 0 ||
1432 peer2->ibp_accepting != 0);
1434 list_add_tail(&tx->tx_list,
1435 &peer2->ibp_tx_queue);
1436 write_unlock_irqrestore(g_lock, flags);
1438 conn = kiblnd_get_conn_locked(peer2);
1439 kiblnd_conn_addref(conn); /* 1 ref for me... */
1441 write_unlock_irqrestore(g_lock, flags);
1444 kiblnd_queue_tx(tx, conn);
1445 kiblnd_conn_decref(conn); /* ...to here */
1448 kiblnd_peer_decref(peer);
1452 /* Brand new peer */
1453 LASSERT (peer->ibp_connecting == 0);
1454 peer->ibp_connecting = 1;
1456 /* always called with a ref on ni, which prevents ni being shutdown */
1457 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1460 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1462 kiblnd_peer_addref(peer);
1463 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1465 write_unlock_irqrestore(g_lock, flags);
1467 kiblnd_connect_peer(peer);
1468 kiblnd_peer_decref(peer);
1472 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1474 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1475 int type = lntmsg->msg_type;
1476 lnet_process_id_t target = lntmsg->msg_target;
1477 int target_is_router = lntmsg->msg_target_is_router;
1478 int routing = lntmsg->msg_routing;
1479 unsigned int payload_niov = lntmsg->msg_niov;
1480 struct iovec *payload_iov = lntmsg->msg_iov;
1481 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1482 unsigned int payload_offset = lntmsg->msg_offset;
1483 unsigned int payload_nob = lntmsg->msg_len;
1489 /* NB 'private' is different depending on what we're sending.... */
1491 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1492 payload_nob, payload_niov, libcfs_id2str(target));
1494 LASSERT (payload_nob == 0 || payload_niov > 0);
1495 LASSERT (payload_niov <= LNET_MAX_IOV);
1497 /* Thread context */
1498 LASSERT (!in_interrupt());
1499 /* payload is either all vaddrs or all pages */
1500 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1508 LASSERT (payload_nob == 0);
1512 if (routing || target_is_router)
1513 break; /* send IMMEDIATE */
1515 /* is the REPLY message too small for RDMA? */
1516 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1517 if (nob <= IBLND_MSG_SIZE)
1518 break; /* send IMMEDIATE */
1520 tx = kiblnd_get_idle_tx(ni, target.nid);
1522 CERROR("Can't allocate txd for GET to %s\n",
1523 libcfs_nid2str(target.nid));
1529 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1530 rc = kiblnd_setup_rd_iov(ni, tx,
1531 &ibmsg->ibm_u.get.ibgm_rd,
1532 lntmsg->msg_md->md_niov,
1533 lntmsg->msg_md->md_iov.iov,
1534 0, lntmsg->msg_md->md_length);
1536 rc = kiblnd_setup_rd_kiov(ni, tx,
1537 &ibmsg->ibm_u.get.ibgm_rd,
1538 lntmsg->msg_md->md_niov,
1539 lntmsg->msg_md->md_iov.kiov,
1540 0, lntmsg->msg_md->md_length);
1542 CERROR("Can't setup GET sink for %s: %d\n",
1543 libcfs_nid2str(target.nid), rc);
1544 kiblnd_tx_done(ni, tx);
1548 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1549 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1550 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1552 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1554 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1555 if (tx->tx_lntmsg[1] == NULL) {
1556 CERROR("Can't create reply for GET -> %s\n",
1557 libcfs_nid2str(target.nid));
1558 kiblnd_tx_done(ni, tx);
1562 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1563 tx->tx_waiting = 1; /* waiting for GET_DONE */
1564 kiblnd_launch_tx(ni, tx, target.nid);
1567 case LNET_MSG_REPLY:
1569 /* Is the payload small enough not to need RDMA? */
1570 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1571 if (nob <= IBLND_MSG_SIZE)
1572 break; /* send IMMEDIATE */
1574 tx = kiblnd_get_idle_tx(ni, target.nid);
1576 CERROR("Can't allocate %s txd for %s\n",
1577 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1578 libcfs_nid2str(target.nid));
1582 if (payload_kiov == NULL)
1583 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1584 payload_niov, payload_iov,
1585 payload_offset, payload_nob);
1587 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1588 payload_niov, payload_kiov,
1589 payload_offset, payload_nob);
1591 CERROR("Can't setup PUT src for %s: %d\n",
1592 libcfs_nid2str(target.nid), rc);
1593 kiblnd_tx_done(ni, tx);
1598 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1599 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1600 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1602 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1603 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1604 kiblnd_launch_tx(ni, tx, target.nid);
1608 /* send IMMEDIATE */
1610 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1613 tx = kiblnd_get_idle_tx(ni, target.nid);
1615 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1616 type, libcfs_nid2str(target.nid));
1621 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1623 if (payload_kiov != NULL)
1624 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1625 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1626 payload_niov, payload_kiov,
1627 payload_offset, payload_nob);
1629 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1630 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1631 payload_niov, payload_iov,
1632 payload_offset, payload_nob);
1634 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1635 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1637 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1638 kiblnd_launch_tx(ni, tx, target.nid);
1643 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1645 lnet_process_id_t target = lntmsg->msg_target;
1646 unsigned int niov = lntmsg->msg_niov;
1647 struct iovec *iov = lntmsg->msg_iov;
1648 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1649 unsigned int offset = lntmsg->msg_offset;
1650 unsigned int nob = lntmsg->msg_len;
1654 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1656 CERROR("Can't get tx for REPLY to %s\n",
1657 libcfs_nid2str(target.nid));
1663 else if (kiov == NULL)
1664 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1665 niov, iov, offset, nob);
1667 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1668 niov, kiov, offset, nob);
1671 CERROR("Can't setup GET src for %s: %d\n",
1672 libcfs_nid2str(target.nid), rc);
1676 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1677 IBLND_MSG_GET_DONE, nob,
1678 &rx->rx_msg->ibm_u.get.ibgm_rd,
1679 rx->rx_msg->ibm_u.get.ibgm_cookie);
1681 CERROR("Can't setup rdma for GET from %s: %d\n",
1682 libcfs_nid2str(target.nid), rc);
1687 /* No RDMA: local completion may happen now! */
1688 lnet_finalize(ni, lntmsg, 0);
1690 /* RDMA: lnet_finalize(lntmsg) when it
1692 tx->tx_lntmsg[0] = lntmsg;
1695 kiblnd_queue_tx(tx, rx->rx_conn);
1699 kiblnd_tx_done(ni, tx);
1701 lnet_finalize(ni, lntmsg, -EIO);
1705 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1706 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1707 unsigned int offset, unsigned int mlen, unsigned int rlen)
1709 kib_rx_t *rx = private;
1710 kib_msg_t *rxmsg = rx->rx_msg;
1711 kib_conn_t *conn = rx->rx_conn;
1715 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1718 LASSERT (mlen <= rlen);
1719 LASSERT (!in_interrupt());
1720 /* Either all pages or all vaddrs */
1721 LASSERT (!(kiov != NULL && iov != NULL));
1723 switch (rxmsg->ibm_type) {
1727 case IBLND_MSG_IMMEDIATE:
1728 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1729 if (nob > rx->rx_nob) {
1730 CERROR ("Immediate message from %s too big: %d(%d)\n",
1731 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1738 lnet_copy_flat2kiov(niov, kiov, offset,
1739 IBLND_MSG_SIZE, rxmsg,
1740 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1743 lnet_copy_flat2iov(niov, iov, offset,
1744 IBLND_MSG_SIZE, rxmsg,
1745 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1747 lnet_finalize (ni, lntmsg, 0);
1750 case IBLND_MSG_PUT_REQ:
1752 lnet_finalize(ni, lntmsg, 0);
1753 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1754 rxmsg->ibm_u.putreq.ibprm_cookie);
1758 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1760 CERROR("Can't allocate tx for %s\n",
1761 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1762 /* Not replying will break the connection */
1769 rc = kiblnd_setup_rd_iov(ni, tx,
1770 &txmsg->ibm_u.putack.ibpam_rd,
1771 niov, iov, offset, mlen);
1773 rc = kiblnd_setup_rd_kiov(ni, tx,
1774 &txmsg->ibm_u.putack.ibpam_rd,
1775 niov, kiov, offset, mlen);
1777 CERROR("Can't setup PUT sink for %s: %d\n",
1778 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1779 kiblnd_tx_done(ni, tx);
1780 /* tell peer it's over */
1781 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1782 rxmsg->ibm_u.putreq.ibprm_cookie);
1786 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1787 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1788 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1790 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1792 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1793 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1794 kiblnd_queue_tx(tx, conn);
1796 /* reposted buffer reserved for PUT_DONE */
1797 post_credit = IBLND_POSTRX_NO_CREDIT;
1800 case IBLND_MSG_GET_REQ:
1801 if (lntmsg != NULL) {
1802 /* Optimized GET; RDMA lntmsg's payload */
1803 kiblnd_reply(ni, rx, lntmsg);
1805 /* GET didn't match anything */
1806 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1808 rxmsg->ibm_u.get.ibgm_cookie);
1813 kiblnd_post_rx(rx, post_credit);
1818 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1820 struct task_struct *task = kthread_run(fn, arg, name);
1823 return PTR_ERR(task);
1825 atomic_inc(&kiblnd_data.kib_nthreads);
1830 kiblnd_thread_fini (void)
1832 atomic_dec (&kiblnd_data.kib_nthreads);
1836 kiblnd_peer_alive (kib_peer_t *peer)
1838 /* This is racy, but everyone's only writing cfs_time_current() */
1839 peer->ibp_last_alive = cfs_time_current();
1844 kiblnd_peer_notify (kib_peer_t *peer)
1847 cfs_time_t last_alive = 0;
1848 unsigned long flags;
1850 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1852 if (list_empty(&peer->ibp_conns) &&
1853 peer->ibp_accepting == 0 &&
1854 peer->ibp_connecting == 0 &&
1855 peer->ibp_error != 0) {
1856 error = peer->ibp_error;
1857 peer->ibp_error = 0;
1859 last_alive = peer->ibp_last_alive;
1862 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1865 lnet_notify(peer->ibp_ni,
1866 peer->ibp_nid, 0, last_alive);
1870 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1872 /* This just does the immediate housekeeping. 'error' is zero for a
1873 * normal shutdown which can happen only after the connection has been
1874 * established. If the connection is established, schedule the
1875 * connection to be finished off by the connd. Otherwise the connd is
1876 * already dealing with it (either to set it up or tear it down).
1877 * Caller holds kib_global_lock exclusively in irq context */
1878 kib_peer_t *peer = conn->ibc_peer;
1880 unsigned long flags;
1882 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1884 if (error != 0 && conn->ibc_comms_error == 0)
1885 conn->ibc_comms_error = error;
1887 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1888 return; /* already being handled */
1891 list_empty(&conn->ibc_tx_noops) &&
1892 list_empty(&conn->ibc_tx_queue) &&
1893 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1894 list_empty(&conn->ibc_tx_queue_nocred) &&
1895 list_empty(&conn->ibc_active_txs)) {
1896 CDEBUG(D_NET, "closing conn to %s\n",
1897 libcfs_nid2str(peer->ibp_nid));
1899 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1900 libcfs_nid2str(peer->ibp_nid), error,
1901 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1902 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1903 list_empty(&conn->ibc_tx_queue_rsrvd) ?
1904 "" : "(sending_rsrvd)",
1905 list_empty(&conn->ibc_tx_queue_nocred) ?
1906 "" : "(sending_nocred)",
1907 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1910 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1911 list_del(&conn->ibc_list);
1912 /* connd (see below) takes over ibc_list's ref */
1914 if (list_empty(&peer->ibp_conns) && /* no more conns */
1915 kiblnd_peer_active(peer)) { /* still in peer table */
1916 kiblnd_unlink_peer_locked(peer);
1918 /* set/clear error on last conn */
1919 peer->ibp_error = conn->ibc_comms_error;
1922 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1925 kiblnd_dev_can_failover(dev)) {
1926 list_add_tail(&dev->ibd_fail_list,
1927 &kiblnd_data.kib_failed_devs);
1928 wake_up(&kiblnd_data.kib_failover_waitq);
1931 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1933 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1934 wake_up(&kiblnd_data.kib_connd_waitq);
1936 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1940 kiblnd_close_conn(kib_conn_t *conn, int error)
1942 unsigned long flags;
1944 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1946 kiblnd_close_conn_locked(conn, error);
1948 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1952 kiblnd_handle_early_rxs(kib_conn_t *conn)
1954 unsigned long flags;
1957 LASSERT(!in_interrupt());
1958 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1960 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1961 while (!list_empty(&conn->ibc_early_rxs)) {
1962 rx = list_entry(conn->ibc_early_rxs.next,
1964 list_del(&rx->rx_list);
1965 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1967 kiblnd_handle_rx(rx);
1969 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1971 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1975 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1977 struct list_head zombies = LIST_HEAD_INIT(zombies);
1978 struct list_head *tmp;
1979 struct list_head *nxt;
1982 spin_lock(&conn->ibc_lock);
1984 list_for_each_safe(tmp, nxt, txs) {
1985 tx = list_entry(tmp, kib_tx_t, tx_list);
1987 if (txs == &conn->ibc_active_txs) {
1988 LASSERT(!tx->tx_queued);
1989 LASSERT(tx->tx_waiting ||
1990 tx->tx_sending != 0);
1992 LASSERT(tx->tx_queued);
1995 tx->tx_status = -ECONNABORTED;
1998 if (tx->tx_sending == 0) {
2000 list_del(&tx->tx_list);
2001 list_add(&tx->tx_list, &zombies);
2005 spin_unlock(&conn->ibc_lock);
2007 kiblnd_txlist_done(conn->ibc_peer->ibp_ni, &zombies, -ECONNABORTED);
2011 kiblnd_finalise_conn (kib_conn_t *conn)
2013 LASSERT (!in_interrupt());
2014 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2016 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2018 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2019 * for connections that didn't get as far as being connected, because
2020 * rdma_disconnect() does this for free. */
2021 kiblnd_abort_receives(conn);
2023 /* Complete all tx descs not waiting for sends to complete.
2024 * NB we should be safe from RDMA now that the QP has changed state */
2026 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2027 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2028 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2029 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2030 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2032 kiblnd_handle_early_rxs(conn);
2036 kiblnd_peer_connect_failed(kib_peer_t *peer, int active, int error)
2038 struct list_head zombies = LIST_HEAD_INIT(zombies);
2039 unsigned long flags;
2041 LASSERT (error != 0);
2042 LASSERT (!in_interrupt());
2044 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2047 LASSERT (peer->ibp_connecting > 0);
2048 peer->ibp_connecting--;
2050 LASSERT (peer->ibp_accepting > 0);
2051 peer->ibp_accepting--;
2054 if (peer->ibp_connecting != 0 ||
2055 peer->ibp_accepting != 0) {
2056 /* another connection attempt under way... */
2057 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2062 if (list_empty(&peer->ibp_conns)) {
2063 /* Take peer's blocked transmits to complete with error */
2064 list_add(&zombies, &peer->ibp_tx_queue);
2065 list_del_init(&peer->ibp_tx_queue);
2067 if (kiblnd_peer_active(peer))
2068 kiblnd_unlink_peer_locked(peer);
2070 peer->ibp_error = error;
2072 /* Can't have blocked transmits if there are connections */
2073 LASSERT(list_empty(&peer->ibp_tx_queue));
2076 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2078 kiblnd_peer_notify(peer);
2080 if (list_empty(&zombies))
2083 CNETERR("Deleting messages for %s: connection failed\n",
2084 libcfs_nid2str(peer->ibp_nid));
2086 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2090 kiblnd_connreq_done(kib_conn_t *conn, int status)
2092 kib_peer_t *peer = conn->ibc_peer;
2094 struct list_head txs;
2095 unsigned long flags;
2098 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2100 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2101 libcfs_nid2str(peer->ibp_nid), active,
2102 conn->ibc_version, status);
2104 LASSERT (!in_interrupt());
2105 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2106 peer->ibp_connecting > 0) ||
2107 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2108 peer->ibp_accepting > 0));
2110 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2111 conn->ibc_connvars = NULL;
2114 /* failed to establish connection */
2115 kiblnd_peer_connect_failed(peer, active, status);
2116 kiblnd_finalise_conn(conn);
2120 /* connection established */
2121 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2123 conn->ibc_last_send = jiffies;
2124 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2125 kiblnd_peer_alive(peer);
2127 /* Add conn to peer's list and nuke any dangling conns from a different
2128 * peer instance... */
2129 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2130 list_add(&conn->ibc_list, &peer->ibp_conns);
2132 peer->ibp_connecting--;
2134 peer->ibp_accepting--;
2136 if (peer->ibp_version == 0) {
2137 peer->ibp_version = conn->ibc_version;
2138 peer->ibp_incarnation = conn->ibc_incarnation;
2141 if (peer->ibp_version != conn->ibc_version ||
2142 peer->ibp_incarnation != conn->ibc_incarnation) {
2143 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2144 conn->ibc_incarnation);
2145 peer->ibp_version = conn->ibc_version;
2146 peer->ibp_incarnation = conn->ibc_incarnation;
2149 /* grab pending txs while I have the lock */
2150 list_add(&txs, &peer->ibp_tx_queue);
2151 list_del_init(&peer->ibp_tx_queue);
2153 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2154 conn->ibc_comms_error != 0) { /* error has happened already */
2155 lnet_ni_t *ni = peer->ibp_ni;
2157 /* start to shut down connection */
2158 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2159 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2161 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2166 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2168 /* Schedule blocked txs */
2169 spin_lock(&conn->ibc_lock);
2170 while (!list_empty(&txs)) {
2171 tx = list_entry(txs.next, kib_tx_t, tx_list);
2172 list_del(&tx->tx_list);
2174 kiblnd_queue_tx_locked(tx, conn);
2176 spin_unlock(&conn->ibc_lock);
2178 kiblnd_check_sends(conn);
2180 /* schedule blocked rxs */
2181 kiblnd_handle_early_rxs(conn);
2185 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2189 rc = rdma_reject(cmid, rej, sizeof(*rej));
2192 CWARN("Error %d sending reject\n", rc);
2196 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2198 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2199 kib_msg_t *reqmsg = priv;
2205 lnet_ni_t *ni = NULL;
2206 kib_net_t *net = NULL;
2208 struct rdma_conn_param cp;
2210 int version = IBLND_MSG_VERSION;
2211 unsigned long flags;
2213 struct sockaddr_in *peer_addr;
2214 LASSERT (!in_interrupt());
2216 /* cmid inherits 'context' from the corresponding listener id */
2217 ibdev = (kib_dev_t *)cmid->context;
2218 LASSERT (ibdev != NULL);
2220 memset(&rej, 0, sizeof(rej));
2221 rej.ibr_magic = IBLND_MSG_MAGIC;
2222 rej.ibr_why = IBLND_REJECT_FATAL;
2223 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2225 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2226 if (*kiblnd_tunables.kib_require_priv_port &&
2227 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2228 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2229 CERROR("Peer's port (%u.%u.%u.%u:%hu) is not privileged\n",
2230 HIPQUAD(ip), ntohs(peer_addr->sin_port));
2234 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2235 CERROR("Short connection request\n");
2239 /* Future protocol version compatibility support! If the
2240 * o2iblnd-specific protocol changes, or when LNET unifies
2241 * protocols over all LNDs, the initial connection will
2242 * negotiate a protocol version. I trap this here to avoid
2243 * console errors; the reject tells the peer which protocol I
2245 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2246 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2248 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2249 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2250 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2252 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2253 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2254 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2257 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2259 CERROR("Can't parse connection request: %d\n", rc);
2263 nid = reqmsg->ibm_srcnid;
2264 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2267 net = (kib_net_t *)ni->ni_data;
2268 rej.ibr_incarnation = net->ibn_incarnation;
2271 if (ni == NULL || /* no matching net */
2272 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2273 net->ibn_dev != ibdev) { /* wrong device */
2274 CERROR("Can't accept %s on %s (%s:%d:%u.%u.%u.%u): "
2275 "bad dst nid %s\n", libcfs_nid2str(nid),
2276 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2277 ibdev->ibd_ifname, ibdev->ibd_nnets,
2278 HIPQUAD(ibdev->ibd_ifip),
2279 libcfs_nid2str(reqmsg->ibm_dstnid));
2284 /* check time stamp as soon as possible */
2285 if (reqmsg->ibm_dststamp != 0 &&
2286 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2287 CWARN("Stale connection request\n");
2288 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2292 /* I can accept peer's version */
2293 version = reqmsg->ibm_version;
2295 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2296 CERROR("Unexpected connreq msg type: %x from %s\n",
2297 reqmsg->ibm_type, libcfs_nid2str(nid));
2301 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2302 IBLND_MSG_QUEUE_SIZE(version)) {
2303 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2304 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2305 IBLND_MSG_QUEUE_SIZE(version));
2307 if (version == IBLND_MSG_VERSION)
2308 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2313 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2314 IBLND_RDMA_FRAGS(version)) {
2315 CERROR("Can't accept %s(version %x): "
2316 "incompatible max_frags %d (%d wanted)\n",
2317 libcfs_nid2str(nid), version,
2318 reqmsg->ibm_u.connparams.ibcp_max_frags,
2319 IBLND_RDMA_FRAGS(version));
2321 if (version == IBLND_MSG_VERSION)
2322 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2328 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2329 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2330 libcfs_nid2str(nid),
2331 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2336 /* assume 'nid' is a new peer; create */
2337 rc = kiblnd_create_peer(ni, &peer, nid);
2339 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2340 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2344 write_lock_irqsave(g_lock, flags);
2346 peer2 = kiblnd_find_peer_locked(nid);
2347 if (peer2 != NULL) {
2348 if (peer2->ibp_version == 0) {
2349 peer2->ibp_version = version;
2350 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2353 /* not the guy I've talked with */
2354 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2355 peer2->ibp_version != version) {
2356 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2357 write_unlock_irqrestore(g_lock, flags);
2359 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2360 libcfs_nid2str(nid), peer2->ibp_version, version);
2362 kiblnd_peer_decref(peer);
2363 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2367 /* tie-break connection race in favour of the higher NID */
2368 if (peer2->ibp_connecting != 0 &&
2370 write_unlock_irqrestore(g_lock, flags);
2372 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2374 kiblnd_peer_decref(peer);
2375 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2379 peer2->ibp_accepting++;
2380 kiblnd_peer_addref(peer2);
2382 write_unlock_irqrestore(g_lock, flags);
2383 kiblnd_peer_decref(peer);
2386 /* Brand new peer */
2387 LASSERT (peer->ibp_accepting == 0);
2388 LASSERT (peer->ibp_version == 0 &&
2389 peer->ibp_incarnation == 0);
2391 peer->ibp_accepting = 1;
2392 peer->ibp_version = version;
2393 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2395 /* I have a ref on ni that prevents it being shutdown */
2396 LASSERT (net->ibn_shutdown == 0);
2398 kiblnd_peer_addref(peer);
2399 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2401 write_unlock_irqrestore(g_lock, flags);
2404 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2406 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2407 kiblnd_peer_decref(peer);
2408 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2412 /* conn now "owns" cmid, so I return success from here on to ensure the
2413 * CM callback doesn't destroy cmid. */
2415 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2416 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2417 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2418 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2419 <= IBLND_RX_MSGS(version));
2421 ackmsg = &conn->ibc_connvars->cv_msg;
2422 memset(ackmsg, 0, sizeof(*ackmsg));
2424 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2425 sizeof(ackmsg->ibm_u.connparams));
2426 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2427 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2428 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2430 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2432 memset(&cp, 0, sizeof(cp));
2433 cp.private_data = ackmsg;
2434 cp.private_data_len = ackmsg->ibm_nob;
2435 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2436 cp.initiator_depth = 0;
2437 cp.flow_control = 1;
2438 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2439 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2441 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2443 rc = rdma_accept(cmid, &cp);
2445 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2446 rej.ibr_version = version;
2447 rej.ibr_why = IBLND_REJECT_FATAL;
2449 kiblnd_reject(cmid, &rej);
2450 kiblnd_connreq_done(conn, rc);
2451 kiblnd_conn_decref(conn);
2461 rej.ibr_version = version;
2462 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2463 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2464 kiblnd_reject(cmid, &rej);
2466 return -ECONNREFUSED;
2470 kiblnd_reconnect (kib_conn_t *conn, int version,
2471 __u64 incarnation, int why, kib_connparams_t *cp)
2473 kib_peer_t *peer = conn->ibc_peer;
2476 unsigned long flags;
2478 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2479 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2481 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2483 /* retry connection if it's still needed and no other connection
2484 * attempts (active or passive) are in progress
2485 * NB: reconnect is still needed even when ibp_tx_queue is
2486 * empty if ibp_version != version because reconnect may be
2487 * initiated by kiblnd_query() */
2488 if ((!list_empty(&peer->ibp_tx_queue) ||
2489 peer->ibp_version != version) &&
2490 peer->ibp_connecting == 1 &&
2491 peer->ibp_accepting == 0) {
2493 peer->ibp_connecting++;
2495 peer->ibp_version = version;
2496 peer->ibp_incarnation = incarnation;
2499 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2509 case IBLND_REJECT_CONN_STALE:
2513 case IBLND_REJECT_CONN_RACE:
2514 reason = "conn race";
2517 case IBLND_REJECT_CONN_UNCOMPAT:
2518 reason = "version negotiation";
2522 CNETERR("%s: retrying (%s), %x, %x, "
2523 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2524 libcfs_nid2str(peer->ibp_nid),
2525 reason, IBLND_MSG_VERSION, version,
2526 cp != NULL? cp->ibcp_queue_depth :IBLND_MSG_QUEUE_SIZE(version),
2527 cp != NULL? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2528 cp != NULL? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2530 kiblnd_connect_peer(peer);
2534 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2536 kib_peer_t *peer = conn->ibc_peer;
2538 LASSERT (!in_interrupt());
2539 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2542 case IB_CM_REJ_STALE_CONN:
2543 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2544 IBLND_REJECT_CONN_STALE, NULL);
2547 case IB_CM_REJ_INVALID_SERVICE_ID:
2548 CNETERR("%s rejected: no listener at %d\n",
2549 libcfs_nid2str(peer->ibp_nid),
2550 *kiblnd_tunables.kib_service);
2553 case IB_CM_REJ_CONSUMER_DEFINED:
2554 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2555 kib_rej_t *rej = priv;
2556 kib_connparams_t *cp = NULL;
2558 __u64 incarnation = -1;
2560 /* NB. default incarnation is -1 because:
2561 * a) V1 will ignore dst incarnation in connreq.
2562 * b) V2 will provide incarnation while rejecting me,
2563 * -1 will be overwrote.
2565 * if I try to connect to a V1 peer with V2 protocol,
2566 * it rejected me then upgrade to V2, I have no idea
2567 * about the upgrading and try to reconnect with V1,
2568 * in this case upgraded V2 can find out I'm trying to
2569 * talk to the old guy and reject me(incarnation is -1).
2572 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2573 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2574 __swab32s(&rej->ibr_magic);
2575 __swab16s(&rej->ibr_version);
2579 if (priv_nob >= sizeof(kib_rej_t) &&
2580 rej->ibr_version > IBLND_MSG_VERSION_1) {
2581 /* priv_nob is always 148 in current version
2582 * of OFED, so we still need to check version.
2583 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2587 __swab64s(&rej->ibr_incarnation);
2588 __swab16s(&cp->ibcp_queue_depth);
2589 __swab16s(&cp->ibcp_max_frags);
2590 __swab32s(&cp->ibcp_max_msg_size);
2593 incarnation = rej->ibr_incarnation;
2596 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2597 rej->ibr_magic != LNET_PROTO_MAGIC) {
2598 CERROR("%s rejected: consumer defined fatal error\n",
2599 libcfs_nid2str(peer->ibp_nid));
2603 if (rej->ibr_version != IBLND_MSG_VERSION &&
2604 rej->ibr_version != IBLND_MSG_VERSION_1) {
2605 CERROR("%s rejected: o2iblnd version %x error\n",
2606 libcfs_nid2str(peer->ibp_nid),
2611 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2612 rej->ibr_version == IBLND_MSG_VERSION_1) {
2613 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2614 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2616 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2617 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2620 switch (rej->ibr_why) {
2621 case IBLND_REJECT_CONN_RACE:
2622 case IBLND_REJECT_CONN_STALE:
2623 case IBLND_REJECT_CONN_UNCOMPAT:
2624 kiblnd_reconnect(conn, rej->ibr_version,
2625 incarnation, rej->ibr_why, cp);
2628 case IBLND_REJECT_MSG_QUEUE_SIZE:
2629 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2630 libcfs_nid2str(peer->ibp_nid),
2631 cp != NULL ? cp->ibcp_queue_depth :
2632 IBLND_MSG_QUEUE_SIZE(rej->ibr_version),
2633 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2636 case IBLND_REJECT_RDMA_FRAGS:
2637 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2638 libcfs_nid2str(peer->ibp_nid),
2639 cp != NULL ? cp->ibcp_max_frags :
2640 IBLND_RDMA_FRAGS(rej->ibr_version),
2641 IBLND_RDMA_FRAGS(conn->ibc_version));
2644 case IBLND_REJECT_NO_RESOURCES:
2645 CERROR("%s rejected: o2iblnd no resources\n",
2646 libcfs_nid2str(peer->ibp_nid));
2649 case IBLND_REJECT_FATAL:
2650 CERROR("%s rejected: o2iblnd fatal error\n",
2651 libcfs_nid2str(peer->ibp_nid));
2655 CERROR("%s rejected: o2iblnd reason %d\n",
2656 libcfs_nid2str(peer->ibp_nid),
2664 CNETERR("%s rejected: reason %d, size %d\n",
2665 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2669 kiblnd_connreq_done(conn, -ECONNREFUSED);
2673 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2675 kib_peer_t *peer = conn->ibc_peer;
2676 lnet_ni_t *ni = peer->ibp_ni;
2677 kib_net_t *net = ni->ni_data;
2678 kib_msg_t *msg = priv;
2679 int ver = conn->ibc_version;
2680 int rc = kiblnd_unpack_msg(msg, priv_nob);
2681 unsigned long flags;
2683 LASSERT (net != NULL);
2686 CERROR("Can't unpack connack from %s: %d\n",
2687 libcfs_nid2str(peer->ibp_nid), rc);
2691 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2692 CERROR("Unexpected message %d from %s\n",
2693 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2698 if (ver != msg->ibm_version) {
2699 CERROR("%s replied version %x is different with "
2700 "requested version %x\n",
2701 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2706 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2707 IBLND_MSG_QUEUE_SIZE(ver)) {
2708 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2709 libcfs_nid2str(peer->ibp_nid),
2710 msg->ibm_u.connparams.ibcp_queue_depth,
2711 IBLND_MSG_QUEUE_SIZE(ver));
2716 if (msg->ibm_u.connparams.ibcp_max_frags !=
2717 IBLND_RDMA_FRAGS(ver)) {
2718 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2719 libcfs_nid2str(peer->ibp_nid),
2720 msg->ibm_u.connparams.ibcp_max_frags,
2721 IBLND_RDMA_FRAGS(ver));
2726 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2727 CERROR("%s max message size %d too big (%d max)\n",
2728 libcfs_nid2str(peer->ibp_nid),
2729 msg->ibm_u.connparams.ibcp_max_msg_size,
2735 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2736 if (msg->ibm_dstnid == ni->ni_nid &&
2737 msg->ibm_dststamp == net->ibn_incarnation)
2741 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2744 CERROR("Bad connection reply from %s, rc = %d, "
2745 "version: %x max_frags: %d\n",
2746 libcfs_nid2str(peer->ibp_nid), rc,
2747 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2751 conn->ibc_incarnation = msg->ibm_srcstamp;
2753 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2754 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2755 <= IBLND_RX_MSGS(ver));
2757 kiblnd_connreq_done(conn, 0);
2761 /* NB My QP has already established itself, so I handle anything going
2762 * wrong here by setting ibc_comms_error.
2763 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2764 * immediately tears it down. */
2767 conn->ibc_comms_error = rc;
2768 kiblnd_connreq_done(conn, 0);
2772 kiblnd_active_connect (struct rdma_cm_id *cmid)
2774 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2777 struct rdma_conn_param cp;
2780 unsigned long flags;
2783 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2785 incarnation = peer->ibp_incarnation;
2786 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION :
2789 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2791 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2793 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2794 kiblnd_peer_decref(peer); /* lose cmid's ref */
2798 /* conn "owns" cmid now, so I return success from here on to ensure the
2799 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2802 msg = &conn->ibc_connvars->cv_msg;
2804 memset(msg, 0, sizeof(*msg));
2805 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2806 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2807 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2808 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2810 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2811 0, peer->ibp_nid, incarnation);
2813 memset(&cp, 0, sizeof(cp));
2814 cp.private_data = msg;
2815 cp.private_data_len = msg->ibm_nob;
2816 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2817 cp.initiator_depth = 0;
2818 cp.flow_control = 1;
2819 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2820 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2822 LASSERT(cmid->context == (void *)conn);
2823 LASSERT(conn->ibc_cmid == cmid);
2825 rc = rdma_connect(cmid, &cp);
2827 CERROR("Can't connect to %s: %d\n",
2828 libcfs_nid2str(peer->ibp_nid), rc);
2829 kiblnd_connreq_done(conn, rc);
2830 kiblnd_conn_decref(conn);
2837 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2843 switch (event->event) {
2845 CERROR("Unexpected event: %d, status: %d\n",
2846 event->event, event->status);
2849 case RDMA_CM_EVENT_CONNECT_REQUEST:
2850 /* destroy cmid on failure */
2851 rc = kiblnd_passive_connect(cmid,
2852 (void *)KIBLND_CONN_PARAM(event),
2853 KIBLND_CONN_PARAM_LEN(event));
2854 CDEBUG(D_NET, "connreq: %d\n", rc);
2857 case RDMA_CM_EVENT_ADDR_ERROR:
2858 peer = (kib_peer_t *)cmid->context;
2859 CNETERR("%s: ADDR ERROR %d\n",
2860 libcfs_nid2str(peer->ibp_nid), event->status);
2861 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2862 kiblnd_peer_decref(peer);
2863 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2865 case RDMA_CM_EVENT_ADDR_RESOLVED:
2866 peer = (kib_peer_t *)cmid->context;
2868 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2869 libcfs_nid2str(peer->ibp_nid), event->status);
2871 if (event->status != 0) {
2872 CNETERR("Can't resolve address for %s: %d\n",
2873 libcfs_nid2str(peer->ibp_nid), event->status);
2876 rc = rdma_resolve_route(
2877 cmid, *kiblnd_tunables.kib_timeout * 1000);
2880 /* Can't initiate route resolution */
2881 CERROR("Can't resolve route for %s: %d\n",
2882 libcfs_nid2str(peer->ibp_nid), rc);
2884 kiblnd_peer_connect_failed(peer, 1, rc);
2885 kiblnd_peer_decref(peer);
2886 return rc; /* rc != 0 destroys cmid */
2888 case RDMA_CM_EVENT_ROUTE_ERROR:
2889 peer = (kib_peer_t *)cmid->context;
2890 CNETERR("%s: ROUTE ERROR %d\n",
2891 libcfs_nid2str(peer->ibp_nid), event->status);
2892 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2893 kiblnd_peer_decref(peer);
2894 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2896 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2897 peer = (kib_peer_t *)cmid->context;
2898 CDEBUG(D_NET,"%s Route resolved: %d\n",
2899 libcfs_nid2str(peer->ibp_nid), event->status);
2901 if (event->status == 0)
2902 return kiblnd_active_connect(cmid);
2904 CNETERR("Can't resolve route for %s: %d\n",
2905 libcfs_nid2str(peer->ibp_nid), event->status);
2906 kiblnd_peer_connect_failed(peer, 1, event->status);
2907 kiblnd_peer_decref(peer);
2908 return event->status; /* rc != 0 destroys cmid */
2910 case RDMA_CM_EVENT_UNREACHABLE:
2911 conn = (kib_conn_t *)cmid->context;
2912 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2913 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2914 CNETERR("%s: UNREACHABLE %d\n",
2915 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2916 kiblnd_connreq_done(conn, -ENETDOWN);
2917 kiblnd_conn_decref(conn);
2920 case RDMA_CM_EVENT_CONNECT_ERROR:
2921 conn = (kib_conn_t *)cmid->context;
2922 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2923 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2924 CNETERR("%s: CONNECT ERROR %d\n",
2925 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2926 kiblnd_connreq_done(conn, -ENOTCONN);
2927 kiblnd_conn_decref(conn);
2930 case RDMA_CM_EVENT_REJECTED:
2931 conn = (kib_conn_t *)cmid->context;
2932 switch (conn->ibc_state) {
2936 case IBLND_CONN_PASSIVE_WAIT:
2937 CERROR ("%s: REJECTED %d\n",
2938 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2940 kiblnd_connreq_done(conn, -ECONNRESET);
2943 case IBLND_CONN_ACTIVE_CONNECT:
2944 kiblnd_rejected(conn, event->status,
2945 (void *)KIBLND_CONN_PARAM(event),
2946 KIBLND_CONN_PARAM_LEN(event));
2949 kiblnd_conn_decref(conn);
2952 case RDMA_CM_EVENT_ESTABLISHED:
2953 conn = (kib_conn_t *)cmid->context;
2954 switch (conn->ibc_state) {
2958 case IBLND_CONN_PASSIVE_WAIT:
2959 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2960 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2961 kiblnd_connreq_done(conn, 0);
2964 case IBLND_CONN_ACTIVE_CONNECT:
2965 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2966 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2967 kiblnd_check_connreply(conn,
2968 (void *)KIBLND_CONN_PARAM(event),
2969 KIBLND_CONN_PARAM_LEN(event));
2972 /* net keeps its ref on conn! */
2975 #ifdef HAVE_OFED_RDMA_CMEV_TIMEWAIT_EXIT
2976 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2977 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2980 case RDMA_CM_EVENT_DISCONNECTED:
2981 conn = (kib_conn_t *)cmid->context;
2982 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2983 CERROR("%s DISCONNECTED\n",
2984 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2985 kiblnd_connreq_done(conn, -ECONNRESET);
2987 kiblnd_close_conn(conn, 0);
2989 kiblnd_conn_decref(conn);
2990 cmid->context = NULL;
2993 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2994 LCONSOLE_ERROR_MSG(0x131,
2995 "Received notification of device removal\n"
2996 "Please shutdown LNET to allow this to proceed\n");
2997 /* Can't remove network from underneath LNET for now, so I have
3001 #ifdef HAVE_OFED_RDMA_CMEV_ADDRCHANGE
3002 case RDMA_CM_EVENT_ADDR_CHANGE:
3003 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3010 kiblnd_check_txs_locked(kib_conn_t *conn, struct list_head *txs)
3013 struct list_head *ttmp;
3015 list_for_each(ttmp, txs) {
3016 tx = list_entry(ttmp, kib_tx_t, tx_list);
3018 if (txs != &conn->ibc_active_txs) {
3019 LASSERT(tx->tx_queued);
3021 LASSERT(!tx->tx_queued);
3022 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3025 if (cfs_time_aftereq(jiffies, tx->tx_deadline)) {
3026 CERROR("Timed out tx: %s, %lu seconds\n",
3027 kiblnd_queue2str(conn, txs),
3028 cfs_duration_sec(jiffies - tx->tx_deadline));
3037 kiblnd_conn_timed_out_locked(kib_conn_t *conn)
3039 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3040 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3041 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3042 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3043 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3047 kiblnd_check_conns (int idx)
3049 struct list_head closes = LIST_HEAD_INIT(closes);
3050 struct list_head checksends = LIST_HEAD_INIT(checksends);
3051 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3052 struct list_head *ptmp;
3055 struct list_head *ctmp;
3056 unsigned long flags;
3058 /* NB. We expect to have a look at all the peers and not find any
3059 * RDMAs to time out, so we just use a shared lock while we
3061 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3063 list_for_each(ptmp, peers) {
3064 peer = list_entry(ptmp, kib_peer_t, ibp_list);
3066 list_for_each(ctmp, &peer->ibp_conns) {
3070 conn = list_entry(ctmp, kib_conn_t, ibc_list);
3072 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3074 spin_lock(&conn->ibc_lock);
3076 sendnoop = kiblnd_need_noop(conn);
3077 timedout = kiblnd_conn_timed_out_locked(conn);
3078 if (!sendnoop && !timedout) {
3079 spin_unlock(&conn->ibc_lock);
3084 CERROR("Timed out RDMA with %s (%lu): "
3085 "c: %u, oc: %u, rc: %u\n",
3086 libcfs_nid2str(peer->ibp_nid),
3087 cfs_duration_sec(cfs_time_current() -
3088 peer->ibp_last_alive),
3090 conn->ibc_outstanding_credits,
3091 conn->ibc_reserved_credits);
3092 list_add(&conn->ibc_connd_list, &closes);
3094 list_add(&conn->ibc_connd_list, &checksends);
3096 /* +ref for 'closes' or 'checksends' */
3097 kiblnd_conn_addref(conn);
3099 spin_unlock(&conn->ibc_lock);
3103 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3105 /* Handle timeout by closing the whole
3106 * connection. We can only be sure RDMA activity
3107 * has ceased once the QP has been modified. */
3108 while (!list_empty(&closes)) {
3109 conn = list_entry(closes.next,
3110 kib_conn_t, ibc_connd_list);
3111 list_del(&conn->ibc_connd_list);
3112 kiblnd_close_conn(conn, -ETIMEDOUT);
3113 kiblnd_conn_decref(conn);
3116 /* In case we have enough credits to return via a
3117 * NOOP, but there were no non-blocking tx descs
3118 * free to do it last time... */
3119 while (!list_empty(&checksends)) {
3120 conn = list_entry(checksends.next,
3121 kib_conn_t, ibc_connd_list);
3122 list_del(&conn->ibc_connd_list);
3123 kiblnd_check_sends(conn);
3124 kiblnd_conn_decref(conn);
3129 kiblnd_disconnect_conn (kib_conn_t *conn)
3131 LASSERT (!in_interrupt());
3132 LASSERT (current == kiblnd_data.kib_connd);
3133 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3135 rdma_disconnect(conn->ibc_cmid);
3136 kiblnd_finalise_conn(conn);
3138 kiblnd_peer_notify(conn->ibc_peer);
3142 kiblnd_connd (void *arg)
3145 unsigned long flags;
3151 unsigned long deadline = jiffies;
3153 cfs_block_allsigs ();
3155 init_waitqueue_entry_current (&wait);
3156 kiblnd_data.kib_connd = current;
3158 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3160 while (!kiblnd_data.kib_shutdown) {
3164 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3165 conn = list_entry(kiblnd_data. \
3166 kib_connd_zombies.next,
3167 kib_conn_t, ibc_list);
3168 list_del(&conn->ibc_list);
3170 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3174 kiblnd_destroy_conn(conn);
3176 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3179 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3180 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3181 kib_conn_t, ibc_list);
3182 list_del(&conn->ibc_list);
3184 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3188 kiblnd_disconnect_conn(conn);
3189 kiblnd_conn_decref(conn);
3191 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3194 /* careful with the jiffy wrap... */
3195 timeout = (int)(deadline - jiffies);
3199 int chunk = kiblnd_data.kib_peer_hash_size;
3201 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3204 /* Time to check for RDMA timeouts on a few more
3205 * peers: I do checks every 'p' seconds on a
3206 * proportion of the peer table and I need to check
3207 * every connection 'n' times within a timeout
3208 * interval, to ensure I detect a timeout on any
3209 * connection within (n+1)/n times the timeout
3212 if (*kiblnd_tunables.kib_timeout > n * p)
3213 chunk = (chunk * n * p) /
3214 *kiblnd_tunables.kib_timeout;
3218 for (i = 0; i < chunk; i++) {
3219 kiblnd_check_conns(peer_index);
3220 peer_index = (peer_index + 1) %
3221 kiblnd_data.kib_peer_hash_size;
3225 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3231 /* Nothing to do for 'timeout' */
3232 set_current_state(TASK_INTERRUPTIBLE);
3233 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3234 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3236 waitq_timedwait(&wait, TASK_INTERRUPTIBLE, timeout);
3238 set_current_state(TASK_RUNNING);
3239 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3240 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3243 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3245 kiblnd_thread_fini();
3250 kiblnd_qp_event(struct ib_event *event, void *arg)
3252 kib_conn_t *conn = arg;
3254 switch (event->event) {
3255 case IB_EVENT_COMM_EST:
3256 CDEBUG(D_NET, "%s established\n",
3257 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3261 CERROR("%s: Async QP event type %d\n",
3262 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3268 kiblnd_complete (struct ib_wc *wc)
3270 switch (kiblnd_wreqid2type(wc->wr_id)) {
3274 case IBLND_WID_RDMA:
3275 /* We only get RDMA completion notification if it fails. All
3276 * subsequent work items, including the final SEND will fail
3277 * too. However we can't print out any more info about the
3278 * failing RDMA because 'tx' might be back on the idle list or
3279 * even reused already if we didn't manage to post all our work
3281 CNETERR("RDMA (tx: %p) failed: %d\n",
3282 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3286 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3290 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3297 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3299 /* NB I'm not allowed to schedule this conn once its refcount has
3300 * reached 0. Since fundamentally I'm racing with scheduler threads
3301 * consuming my CQ I could be called after all completions have
3302 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3303 * and this CQ is about to be destroyed so I NOOP. */
3304 kib_conn_t *conn = (kib_conn_t *)arg;
3305 struct kib_sched_info *sched = conn->ibc_sched;
3306 unsigned long flags;
3308 LASSERT(cq == conn->ibc_cq);
3310 spin_lock_irqsave(&sched->ibs_lock, flags);
3312 conn->ibc_ready = 1;
3314 if (!conn->ibc_scheduled &&
3315 (conn->ibc_nrx > 0 ||
3316 conn->ibc_nsends_posted > 0)) {
3317 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3318 conn->ibc_scheduled = 1;
3319 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3321 if (waitqueue_active(&sched->ibs_waitq))
3322 wake_up(&sched->ibs_waitq);
3325 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3329 kiblnd_cq_event(struct ib_event *event, void *arg)
3331 kib_conn_t *conn = arg;
3333 CERROR("%s: async CQ event type %d\n",
3334 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3338 kiblnd_scheduler(void *arg)
3340 long id = (long)arg;
3341 struct kib_sched_info *sched;
3344 unsigned long flags;
3350 cfs_block_allsigs();
3352 init_waitqueue_entry_current(&wait);
3354 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3356 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3358 CWARN("Failed to bind on CPT %d, please verify whether "
3359 "all CPUs are healthy and reload modules if necessary, "
3360 "otherwise your system might under risk of low "
3361 "performance\n", sched->ibs_cpt);
3364 spin_lock_irqsave(&sched->ibs_lock, flags);
3366 while (!kiblnd_data.kib_shutdown) {
3367 if (busy_loops++ >= IBLND_RESCHED) {
3368 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3373 spin_lock_irqsave(&sched->ibs_lock, flags);
3378 if (!list_empty(&sched->ibs_conns)) {
3379 conn = list_entry(sched->ibs_conns.next,
3380 kib_conn_t, ibc_sched_list);
3381 /* take over kib_sched_conns' ref on conn... */
3382 LASSERT(conn->ibc_scheduled);
3383 list_del(&conn->ibc_sched_list);
3384 conn->ibc_ready = 0;
3386 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3388 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3390 rc = ib_req_notify_cq(conn->ibc_cq,
3393 CWARN("%s: ib_req_notify_cq failed: %d, "
3394 "closing connection\n",
3395 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3396 kiblnd_close_conn(conn, -EIO);
3397 kiblnd_conn_decref(conn);
3398 spin_lock_irqsave(&sched->ibs_lock,
3403 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3407 CWARN("%s: ib_poll_cq failed: %d, "
3408 "closing connection\n",
3409 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3411 kiblnd_close_conn(conn, -EIO);
3412 kiblnd_conn_decref(conn);
3413 spin_lock_irqsave(&sched->ibs_lock, flags);
3417 spin_lock_irqsave(&sched->ibs_lock, flags);
3419 if (rc != 0 || conn->ibc_ready) {
3420 /* There may be another completion waiting; get
3421 * another scheduler to check while I handle
3423 /* +1 ref for sched_conns */
3424 kiblnd_conn_addref(conn);
3425 list_add_tail(&conn->ibc_sched_list,
3427 if (waitqueue_active(&sched->ibs_waitq))
3428 wake_up(&sched->ibs_waitq);
3430 conn->ibc_scheduled = 0;
3434 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3435 kiblnd_complete(&wc);
3437 spin_lock_irqsave(&sched->ibs_lock, flags);
3440 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3447 set_current_state(TASK_INTERRUPTIBLE);
3448 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3449 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3451 waitq_wait(&wait, TASK_INTERRUPTIBLE);
3454 remove_wait_queue(&sched->ibs_waitq, &wait);
3455 set_current_state(TASK_RUNNING);
3456 spin_lock_irqsave(&sched->ibs_lock, flags);
3459 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3461 kiblnd_thread_fini();
3466 kiblnd_failover_thread(void *arg)
3468 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3471 unsigned long flags;
3474 LASSERT (*kiblnd_tunables.kib_dev_failover != 0);
3476 cfs_block_allsigs ();
3478 init_waitqueue_entry_current(&wait);
3479 write_lock_irqsave(glock, flags);
3481 while (!kiblnd_data.kib_shutdown) {
3482 int do_failover = 0;
3485 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3487 if (cfs_time_before(cfs_time_current(),
3488 dev->ibd_next_failover))
3495 list_del_init(&dev->ibd_fail_list);
3496 dev->ibd_failover = 1;
3497 write_unlock_irqrestore(glock, flags);
3499 rc = kiblnd_dev_failover(dev);
3501 write_lock_irqsave(glock, flags);
3503 LASSERT (dev->ibd_failover);
3504 dev->ibd_failover = 0;
3505 if (rc >= 0) { /* Device is OK or failover succeed */
3506 dev->ibd_next_failover = cfs_time_shift(3);
3510 /* failed to failover, retry later */
3511 dev->ibd_next_failover =
3512 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3513 if (kiblnd_dev_can_failover(dev)) {
3514 list_add_tail(&dev->ibd_fail_list,
3515 &kiblnd_data.kib_failed_devs);
3521 /* long sleep if no more pending failover */
3522 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3524 set_current_state(TASK_INTERRUPTIBLE);
3525 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3526 write_unlock_irqrestore(glock, flags);
3528 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3529 cfs_time_seconds(1));
3530 set_current_state(TASK_RUNNING);
3531 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3532 write_lock_irqsave(glock, flags);
3534 if (!long_sleep || rc != 0)
3537 /* have a long sleep, routine check all active devices,
3538 * we need checking like this because if there is not active
3539 * connection on the dev and no SEND from local, we may listen
3540 * on wrong HCA for ever while there is a bonding failover */
3541 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3542 if (kiblnd_dev_can_failover(dev)) {
3543 list_add_tail(&dev->ibd_fail_list,
3544 &kiblnd_data.kib_failed_devs);
3549 write_unlock_irqrestore(glock, flags);
3551 kiblnd_thread_fini();