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, 2014, 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);
52 static void kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx);
55 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
57 lnet_msg_t *lntmsg[2];
58 kib_net_t *net = ni->ni_data;
62 LASSERT (net != NULL);
63 LASSERT (!in_interrupt());
64 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
65 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
66 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
67 LASSERT (tx->tx_pool != NULL);
69 kiblnd_unmap_tx(ni, tx);
71 /* tx may have up to 2 lnet msgs to finalise */
72 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
73 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
76 if (tx->tx_conn != NULL) {
77 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
79 kiblnd_conn_decref(tx->tx_conn);
86 kiblnd_pool_free_node(&tx->tx_pool->tpo_pool, &tx->tx_list);
88 /* delay finalize until my descs have been freed */
89 for (i = 0; i < 2; i++) {
90 if (lntmsg[i] == NULL)
93 lnet_finalize(ni, lntmsg[i], rc);
98 kiblnd_txlist_done(lnet_ni_t *ni, struct list_head *txlist, int status)
102 while (!list_empty(txlist)) {
103 tx = list_entry(txlist->next, kib_tx_t, tx_list);
105 list_del(&tx->tx_list);
108 tx->tx_status = status;
109 kiblnd_tx_done(ni, tx);
114 kiblnd_get_idle_tx(lnet_ni_t *ni, lnet_nid_t target)
116 kib_net_t *net = (kib_net_t *)ni->ni_data;
117 struct list_head *node;
119 kib_tx_poolset_t *tps;
121 tps = net->ibn_tx_ps[lnet_cpt_of_nid(target)];
122 node = kiblnd_pool_alloc_node(&tps->tps_poolset);
125 tx = container_of(node, kib_tx_t, tx_list);
127 LASSERT (tx->tx_nwrq == 0);
128 LASSERT (!tx->tx_queued);
129 LASSERT (tx->tx_sending == 0);
130 LASSERT (!tx->tx_waiting);
131 LASSERT (tx->tx_status == 0);
132 LASSERT (tx->tx_conn == NULL);
133 LASSERT (tx->tx_lntmsg[0] == NULL);
134 LASSERT (tx->tx_lntmsg[1] == NULL);
135 LASSERT (tx->tx_u.pmr == NULL);
136 LASSERT (tx->tx_nfrags == 0);
142 kiblnd_drop_rx(kib_rx_t *rx)
144 kib_conn_t *conn = rx->rx_conn;
145 struct kib_sched_info *sched = conn->ibc_sched;
148 spin_lock_irqsave(&sched->ibs_lock, flags);
149 LASSERT(conn->ibc_nrx > 0);
151 spin_unlock_irqrestore(&sched->ibs_lock, flags);
153 kiblnd_conn_decref(conn);
157 kiblnd_post_rx (kib_rx_t *rx, int credit)
159 kib_conn_t *conn = rx->rx_conn;
160 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
161 struct ib_recv_wr *bad_wrq = NULL;
165 LASSERT (net != NULL);
166 LASSERT (!in_interrupt());
167 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
168 credit == IBLND_POSTRX_PEER_CREDIT ||
169 credit == IBLND_POSTRX_RSRVD_CREDIT);
171 mr = kiblnd_find_dma_mr(conn->ibc_hdev, rx->rx_msgaddr, IBLND_MSG_SIZE);
172 LASSERT (mr != NULL);
174 rx->rx_sge.lkey = mr->lkey;
175 rx->rx_sge.addr = rx->rx_msgaddr;
176 rx->rx_sge.length = IBLND_MSG_SIZE;
178 rx->rx_wrq.next = NULL;
179 rx->rx_wrq.sg_list = &rx->rx_sge;
180 rx->rx_wrq.num_sge = 1;
181 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
183 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
184 LASSERT (rx->rx_nob >= 0); /* not posted */
186 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
187 kiblnd_drop_rx(rx); /* No more posts for this rx */
191 rx->rx_nob = -1; /* flag posted */
193 /* NB: need an extra reference after ib_post_recv because we don't
194 * own this rx (and rx::rx_conn) anymore, LU-5678.
196 kiblnd_conn_addref(conn);
197 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
198 if (unlikely(rc != 0)) {
199 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
200 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc, bad_wrq);
204 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
207 if (unlikely(rc != 0)) {
208 kiblnd_close_conn(conn, rc);
209 kiblnd_drop_rx(rx); /* No more posts for this rx */
213 if (credit == IBLND_POSTRX_NO_CREDIT)
216 spin_lock(&conn->ibc_lock);
217 if (credit == IBLND_POSTRX_PEER_CREDIT)
218 conn->ibc_outstanding_credits++;
220 conn->ibc_reserved_credits++;
221 spin_unlock(&conn->ibc_lock);
223 kiblnd_check_sends(conn);
225 kiblnd_conn_decref(conn);
230 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
232 struct list_head *tmp;
234 list_for_each(tmp, &conn->ibc_active_txs) {
235 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
237 LASSERT(!tx->tx_queued);
238 LASSERT(tx->tx_sending != 0 || tx->tx_waiting);
240 if (tx->tx_cookie != cookie)
243 if (tx->tx_waiting &&
244 tx->tx_msg->ibm_type == txtype)
247 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
248 tx->tx_waiting ? "" : "NOT ",
249 tx->tx_msg->ibm_type, txtype);
255 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
258 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
261 spin_lock(&conn->ibc_lock);
263 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
265 spin_unlock(&conn->ibc_lock);
267 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
268 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
269 kiblnd_close_conn(conn, -EPROTO);
273 if (tx->tx_status == 0) { /* success so far */
274 if (status < 0) { /* failed? */
275 tx->tx_status = status;
276 } else if (txtype == IBLND_MSG_GET_REQ) {
277 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
283 idle = !tx->tx_queued && (tx->tx_sending == 0);
285 list_del(&tx->tx_list);
287 spin_unlock(&conn->ibc_lock);
290 kiblnd_tx_done(ni, tx);
294 kiblnd_send_completion(kib_conn_t *conn, int type, int status, __u64 cookie)
296 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
297 kib_tx_t *tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
300 CERROR("Can't get tx for completion %x for %s\n",
301 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
305 tx->tx_msg->ibm_u.completion.ibcm_status = status;
306 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
307 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
309 kiblnd_queue_tx(tx, conn);
313 kiblnd_handle_rx (kib_rx_t *rx)
315 kib_msg_t *msg = rx->rx_msg;
316 kib_conn_t *conn = rx->rx_conn;
317 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
318 int credits = msg->ibm_credits;
324 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
326 CDEBUG (D_NET, "Received %x[%d] from %s\n",
327 msg->ibm_type, credits,
328 libcfs_nid2str(conn->ibc_peer->ibp_nid));
331 /* Have I received credits that will let me send? */
332 spin_lock(&conn->ibc_lock);
334 if (conn->ibc_credits + credits >
335 IBLND_MSG_QUEUE_SIZE(conn->ibc_version)) {
336 rc2 = conn->ibc_credits;
337 spin_unlock(&conn->ibc_lock);
339 CERROR("Bad credits from %s: %d + %d > %d\n",
340 libcfs_nid2str(conn->ibc_peer->ibp_nid),
342 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
344 kiblnd_close_conn(conn, -EPROTO);
345 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
349 conn->ibc_credits += credits;
351 /* This ensures the credit taken by NOOP can be returned */
352 if (msg->ibm_type == IBLND_MSG_NOOP &&
353 !IBLND_OOB_CAPABLE(conn->ibc_version)) /* v1 only */
354 conn->ibc_outstanding_credits++;
356 spin_unlock(&conn->ibc_lock);
357 kiblnd_check_sends(conn);
360 switch (msg->ibm_type) {
362 CERROR("Bad IBLND message type %x from %s\n",
363 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
364 post_credit = IBLND_POSTRX_NO_CREDIT;
369 if (IBLND_OOB_CAPABLE(conn->ibc_version)) {
370 post_credit = IBLND_POSTRX_NO_CREDIT;
374 if (credits != 0) /* credit already posted */
375 post_credit = IBLND_POSTRX_NO_CREDIT;
376 else /* a keepalive NOOP */
377 post_credit = IBLND_POSTRX_PEER_CREDIT;
380 case IBLND_MSG_IMMEDIATE:
381 post_credit = IBLND_POSTRX_DONT_POST;
382 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
383 msg->ibm_srcnid, rx, 0);
384 if (rc < 0) /* repost on error */
385 post_credit = IBLND_POSTRX_PEER_CREDIT;
388 case IBLND_MSG_PUT_REQ:
389 post_credit = IBLND_POSTRX_DONT_POST;
390 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
391 msg->ibm_srcnid, rx, 1);
392 if (rc < 0) /* repost on error */
393 post_credit = IBLND_POSTRX_PEER_CREDIT;
396 case IBLND_MSG_PUT_NAK:
397 CWARN ("PUT_NACK from %s\n",
398 libcfs_nid2str(conn->ibc_peer->ibp_nid));
399 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
400 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
401 msg->ibm_u.completion.ibcm_status,
402 msg->ibm_u.completion.ibcm_cookie);
405 case IBLND_MSG_PUT_ACK:
406 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
408 spin_lock(&conn->ibc_lock);
409 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
410 msg->ibm_u.putack.ibpam_src_cookie);
412 list_del(&tx->tx_list);
413 spin_unlock(&conn->ibc_lock);
416 CERROR("Unmatched PUT_ACK from %s\n",
417 libcfs_nid2str(conn->ibc_peer->ibp_nid));
422 LASSERT (tx->tx_waiting);
423 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
424 * (a) I can overwrite tx_msg since my peer has received it!
425 * (b) tx_waiting set tells tx_complete() it's not done. */
427 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
429 rc2 = kiblnd_init_rdma(conn, tx, IBLND_MSG_PUT_DONE,
430 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
431 &msg->ibm_u.putack.ibpam_rd,
432 msg->ibm_u.putack.ibpam_dst_cookie);
434 CERROR("Can't setup rdma for PUT to %s: %d\n",
435 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
437 spin_lock(&conn->ibc_lock);
438 tx->tx_waiting = 0; /* clear waiting and queue atomically */
439 kiblnd_queue_tx_locked(tx, conn);
440 spin_unlock(&conn->ibc_lock);
443 case IBLND_MSG_PUT_DONE:
444 post_credit = IBLND_POSTRX_PEER_CREDIT;
445 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
446 msg->ibm_u.completion.ibcm_status,
447 msg->ibm_u.completion.ibcm_cookie);
450 case IBLND_MSG_GET_REQ:
451 post_credit = IBLND_POSTRX_DONT_POST;
452 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
453 msg->ibm_srcnid, rx, 1);
454 if (rc < 0) /* repost on error */
455 post_credit = IBLND_POSTRX_PEER_CREDIT;
458 case IBLND_MSG_GET_DONE:
459 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
460 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
461 msg->ibm_u.completion.ibcm_status,
462 msg->ibm_u.completion.ibcm_cookie);
466 if (rc < 0) /* protocol error */
467 kiblnd_close_conn(conn, rc);
469 if (post_credit != IBLND_POSTRX_DONT_POST)
470 kiblnd_post_rx(rx, post_credit);
474 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
476 kib_msg_t *msg = rx->rx_msg;
477 kib_conn_t *conn = rx->rx_conn;
478 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
479 kib_net_t *net = ni->ni_data;
483 LASSERT (net != NULL);
484 LASSERT (rx->rx_nob < 0); /* was posted */
485 rx->rx_nob = 0; /* isn't now */
487 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
490 if (status != IB_WC_SUCCESS) {
491 CNETERR("Rx from %s failed: %d\n",
492 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
499 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
501 CERROR ("Error %d unpacking rx from %s\n",
502 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
506 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
507 msg->ibm_dstnid != ni->ni_nid ||
508 msg->ibm_srcstamp != conn->ibc_incarnation ||
509 msg->ibm_dststamp != net->ibn_incarnation) {
510 CERROR ("Stale rx from %s\n",
511 libcfs_nid2str(conn->ibc_peer->ibp_nid));
516 /* set time last known alive */
517 kiblnd_peer_alive(conn->ibc_peer);
519 /* racing with connection establishment/teardown! */
521 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
522 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
525 write_lock_irqsave(g_lock, flags);
526 /* must check holding global lock to eliminate race */
527 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
528 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
529 write_unlock_irqrestore(g_lock, flags);
532 write_unlock_irqrestore(g_lock, flags);
534 kiblnd_handle_rx(rx);
538 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
539 kiblnd_close_conn(conn, err);
541 kiblnd_drop_rx(rx); /* Don't re-post rx. */
545 kiblnd_kvaddr_to_page (unsigned long vaddr)
549 if (is_vmalloc_addr((void *)vaddr)) {
550 page = vmalloc_to_page ((void *)vaddr);
551 LASSERT (page != NULL);
554 #ifdef CONFIG_HIGHMEM
555 if (vaddr >= PKMAP_BASE &&
556 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
557 /* No highmem pages only used for bulk (kiov) I/O */
558 CERROR("find page for address in highmem\n");
562 page = virt_to_page (vaddr);
563 LASSERT (page != NULL);
568 kiblnd_fmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
571 __u64 *pages = tx->tx_pages;
572 kib_fmr_poolset_t *fps;
579 LASSERT(tx->tx_pool != NULL);
580 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
582 hdev = tx->tx_pool->tpo_hdev;
584 for (i = 0, npages = 0; i < rd->rd_nfrags; i++) {
585 for (size = 0; size < rd->rd_frags[i].rf_nob;
586 size += hdev->ibh_page_size) {
587 pages[npages ++] = (rd->rd_frags[i].rf_addr &
588 hdev->ibh_page_mask) + size;
592 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
594 fps = net->ibn_fmr_ps[cpt];
595 rc = kiblnd_fmr_pool_map(fps, pages, npages, 0, &tx->tx_u.fmr);
597 CERROR ("Can't map %d pages: %d\n", npages, rc);
601 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
603 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.fmr.fmr_pfmr->fmr->rkey :
604 tx->tx_u.fmr.fmr_pfmr->fmr->lkey;
605 rd->rd_frags[0].rf_addr &= ~hdev->ibh_page_mask;
606 rd->rd_frags[0].rf_nob = nob;
613 kiblnd_pmr_map_tx(kib_net_t *net, kib_tx_t *tx, kib_rdma_desc_t *rd, int nob)
616 kib_pmr_poolset_t *pps;
621 LASSERT(tx->tx_pool != NULL);
622 LASSERT(tx->tx_pool->tpo_pool.po_owner != NULL);
624 hdev = tx->tx_pool->tpo_hdev;
626 iova = rd->rd_frags[0].rf_addr & ~hdev->ibh_page_mask;
628 cpt = tx->tx_pool->tpo_pool.po_owner->ps_cpt;
630 pps = net->ibn_pmr_ps[cpt];
631 rc = kiblnd_pmr_pool_map(pps, hdev, rd, &iova, &tx->tx_u.pmr);
633 CERROR("Failed to create MR by phybuf: %d\n", rc);
637 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
639 rd->rd_key = (rd != tx->tx_rd) ? tx->tx_u.pmr->pmr_mr->rkey :
640 tx->tx_u.pmr->pmr_mr->lkey;
642 rd->rd_frags[0].rf_addr = iova;
643 rd->rd_frags[0].rf_nob = nob;
649 kiblnd_unmap_tx(lnet_ni_t *ni, kib_tx_t *tx)
651 kib_net_t *net = ni->ni_data;
653 LASSERT(net != NULL);
655 if (net->ibn_fmr_ps != NULL && tx->tx_u.fmr.fmr_pfmr != NULL) {
656 kiblnd_fmr_pool_unmap(&tx->tx_u.fmr, tx->tx_status);
657 tx->tx_u.fmr.fmr_pfmr = NULL;
659 } else if (net->ibn_pmr_ps != NULL && tx->tx_u.pmr != NULL) {
660 kiblnd_pmr_pool_unmap(tx->tx_u.pmr);
664 if (tx->tx_nfrags != 0) {
665 kiblnd_dma_unmap_sg(tx->tx_pool->tpo_hdev->ibh_ibdev,
666 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
672 kiblnd_map_tx(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd, int nfrags)
674 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
675 kib_net_t *net = ni->ni_data;
676 struct ib_mr *mr = NULL;
680 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
682 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
683 tx->tx_nfrags = nfrags;
685 rd->rd_nfrags = kiblnd_dma_map_sg(hdev->ibh_ibdev, tx->tx_frags,
686 tx->tx_nfrags, tx->tx_dmadir);
688 for (i = 0, nob = 0; i < rd->rd_nfrags; i++) {
689 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
690 hdev->ibh_ibdev, &tx->tx_frags[i]);
691 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
692 hdev->ibh_ibdev, &tx->tx_frags[i]);
693 nob += rd->rd_frags[i].rf_nob;
696 /* looking for pre-mapping MR */
697 mr = kiblnd_find_rd_dma_mr(hdev, rd);
699 /* found pre-mapping MR */
700 rd->rd_key = (rd != tx->tx_rd) ? mr->rkey : mr->lkey;
704 if (net->ibn_fmr_ps != NULL)
705 return kiblnd_fmr_map_tx(net, tx, rd, nob);
706 else if (net->ibn_pmr_ps != NULL)
707 return kiblnd_pmr_map_tx(net, tx, rd, nob);
714 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
715 unsigned int niov, struct iovec *iov, int offset, int nob)
717 kib_net_t *net = ni->ni_data;
719 struct scatterlist *sg;
726 LASSERT (net != NULL);
728 while (offset >= iov->iov_len) {
729 offset -= iov->iov_len;
739 vaddr = ((unsigned long)iov->iov_base) + offset;
740 page_offset = vaddr & (PAGE_SIZE - 1);
741 page = kiblnd_kvaddr_to_page(vaddr);
743 CERROR ("Can't find page\n");
747 fragnob = min((int)(iov->iov_len - offset), nob);
748 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
750 sg_set_page(sg, page, fragnob, page_offset);
753 if (offset + fragnob < iov->iov_len) {
763 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
767 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
768 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
770 kib_net_t *net = ni->ni_data;
771 struct scatterlist *sg;
774 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
778 LASSERT (net != NULL);
780 while (offset >= kiov->kiov_len) {
781 offset -= kiov->kiov_len;
791 fragnob = min((int)(kiov->kiov_len - offset), nob);
793 sg_set_page(sg, kiov->kiov_page, fragnob,
794 kiov->kiov_offset + offset);
803 return kiblnd_map_tx(ni, tx, rd, sg - tx->tx_frags);
807 kiblnd_post_tx_locked (kib_conn_t *conn, kib_tx_t *tx, int credit)
808 __must_hold(&conn->ibc_lock)
810 kib_msg_t *msg = tx->tx_msg;
811 kib_peer_t *peer = conn->ibc_peer;
812 int ver = conn->ibc_version;
815 struct ib_send_wr *bad_wrq;
817 LASSERT (tx->tx_queued);
818 /* We rely on this for QP sizing */
819 LASSERT (tx->tx_nwrq > 0);
820 LASSERT (tx->tx_nwrq <= 1 + IBLND_RDMA_FRAGS(ver));
822 LASSERT (credit == 0 || credit == 1);
823 LASSERT (conn->ibc_outstanding_credits >= 0);
824 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE(ver));
825 LASSERT (conn->ibc_credits >= 0);
826 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE(ver));
828 if (conn->ibc_nsends_posted == IBLND_CONCURRENT_SENDS(ver)) {
829 /* tx completions outstanding... */
830 CDEBUG(D_NET, "%s: posted enough\n",
831 libcfs_nid2str(peer->ibp_nid));
835 if (credit != 0 && conn->ibc_credits == 0) { /* no credits */
836 CDEBUG(D_NET, "%s: no credits\n",
837 libcfs_nid2str(peer->ibp_nid));
841 if (credit != 0 && !IBLND_OOB_CAPABLE(ver) &&
842 conn->ibc_credits == 1 && /* last credit reserved */
843 msg->ibm_type != IBLND_MSG_NOOP) { /* for NOOP */
844 CDEBUG(D_NET, "%s: not using last credit\n",
845 libcfs_nid2str(peer->ibp_nid));
849 /* NB don't drop ibc_lock before bumping tx_sending */
850 list_del(&tx->tx_list);
853 if (msg->ibm_type == IBLND_MSG_NOOP &&
854 (!kiblnd_need_noop(conn) || /* redundant NOOP */
855 (IBLND_OOB_CAPABLE(ver) && /* posted enough NOOP */
856 conn->ibc_noops_posted == IBLND_OOB_MSGS(ver)))) {
857 /* OK to drop when posted enough NOOPs, since
858 * kiblnd_check_sends will queue NOOP again when
859 * posted NOOPs complete */
860 spin_unlock(&conn->ibc_lock);
861 kiblnd_tx_done(peer->ibp_ni, tx);
862 spin_lock(&conn->ibc_lock);
863 CDEBUG(D_NET, "%s(%d): redundant or enough NOOP\n",
864 libcfs_nid2str(peer->ibp_nid),
865 conn->ibc_noops_posted);
869 kiblnd_pack_msg(peer->ibp_ni, msg, ver, conn->ibc_outstanding_credits,
870 peer->ibp_nid, conn->ibc_incarnation);
872 conn->ibc_credits -= credit;
873 conn->ibc_outstanding_credits = 0;
874 conn->ibc_nsends_posted++;
875 if (msg->ibm_type == IBLND_MSG_NOOP)
876 conn->ibc_noops_posted++;
878 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
879 * PUT. If so, it was first queued here as a PUT_REQ, sent and
880 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
881 * and then re-queued here. It's (just) possible that
882 * tx_sending is non-zero if we've not done the tx_complete()
883 * from the first send; hence the ++ rather than = below. */
885 list_add(&tx->tx_list, &conn->ibc_active_txs);
887 /* I'm still holding ibc_lock! */
888 if (conn->ibc_state != IBLND_CONN_ESTABLISHED) {
890 } else if (tx->tx_pool->tpo_pool.po_failed ||
891 conn->ibc_hdev != tx->tx_pool->tpo_hdev) {
892 /* close_conn will launch failover */
895 rc = ib_post_send(conn->ibc_cmid->qp,
896 tx->tx_wrq, &bad_wrq);
899 conn->ibc_last_send = jiffies;
904 /* NB credits are transferred in the actual
905 * message, which can only be the last work item */
906 conn->ibc_credits += credit;
907 conn->ibc_outstanding_credits += msg->ibm_credits;
908 conn->ibc_nsends_posted--;
909 if (msg->ibm_type == IBLND_MSG_NOOP)
910 conn->ibc_noops_posted--;
916 done = (tx->tx_sending == 0);
918 list_del(&tx->tx_list);
920 spin_unlock(&conn->ibc_lock);
922 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
923 CERROR("Error %d posting transmit to %s\n",
924 rc, libcfs_nid2str(peer->ibp_nid));
926 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
927 rc, libcfs_nid2str(peer->ibp_nid));
929 kiblnd_close_conn(conn, rc);
932 kiblnd_tx_done(peer->ibp_ni, tx);
934 spin_lock(&conn->ibc_lock);
940 kiblnd_check_sends (kib_conn_t *conn)
942 int ver = conn->ibc_version;
943 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
946 /* Don't send anything until after the connection is established */
947 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
948 CDEBUG(D_NET, "%s too soon\n",
949 libcfs_nid2str(conn->ibc_peer->ibp_nid));
953 spin_lock(&conn->ibc_lock);
955 LASSERT (conn->ibc_nsends_posted <= IBLND_CONCURRENT_SENDS(ver));
956 LASSERT (!IBLND_OOB_CAPABLE(ver) ||
957 conn->ibc_noops_posted <= IBLND_OOB_MSGS(ver));
958 LASSERT (conn->ibc_reserved_credits >= 0);
960 while (conn->ibc_reserved_credits > 0 &&
961 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
962 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
964 list_del(&tx->tx_list);
965 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
966 conn->ibc_reserved_credits--;
969 if (kiblnd_need_noop(conn)) {
970 spin_unlock(&conn->ibc_lock);
972 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
974 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
976 spin_lock(&conn->ibc_lock);
978 kiblnd_queue_tx_locked(tx, conn);
981 kiblnd_conn_addref(conn); /* 1 ref for me.... (see b21911) */
986 if (!list_empty(&conn->ibc_tx_queue_nocred)) {
988 tx = list_entry(conn->ibc_tx_queue_nocred.next,
990 } else if (!list_empty(&conn->ibc_tx_noops)) {
991 LASSERT (!IBLND_OOB_CAPABLE(ver));
993 tx = list_entry(conn->ibc_tx_noops.next,
995 } else if (!list_empty(&conn->ibc_tx_queue)) {
997 tx = list_entry(conn->ibc_tx_queue.next,
1002 if (kiblnd_post_tx_locked(conn, tx, credit) != 0)
1006 spin_unlock(&conn->ibc_lock);
1008 kiblnd_conn_decref(conn); /* ...until here */
1012 kiblnd_tx_complete (kib_tx_t *tx, int status)
1014 int failed = (status != IB_WC_SUCCESS);
1015 kib_conn_t *conn = tx->tx_conn;
1018 LASSERT (tx->tx_sending > 0);
1021 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1022 CNETERR("Tx -> %s cookie "LPX64
1023 " sending %d waiting %d: failed %d\n",
1024 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1025 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1028 kiblnd_close_conn(conn, -EIO);
1030 kiblnd_peer_alive(conn->ibc_peer);
1033 spin_lock(&conn->ibc_lock);
1035 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1036 * gets to free it, which also drops its ref on 'conn'. */
1039 conn->ibc_nsends_posted--;
1040 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP)
1041 conn->ibc_noops_posted--;
1044 tx->tx_waiting = 0; /* don't wait for peer */
1045 tx->tx_status = -EIO;
1048 idle = (tx->tx_sending == 0) && /* This is the final callback */
1049 !tx->tx_waiting && /* Not waiting for peer */
1050 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1052 list_del(&tx->tx_list);
1054 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1056 spin_unlock(&conn->ibc_lock);
1059 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1061 kiblnd_check_sends(conn);
1063 kiblnd_conn_decref(conn); /* ...until here */
1067 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1069 kib_hca_dev_t *hdev = tx->tx_pool->tpo_hdev;
1070 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1071 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1072 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1075 LASSERT (tx->tx_nwrq >= 0);
1076 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1077 LASSERT (nob <= IBLND_MSG_SIZE);
1079 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1081 mr = kiblnd_find_dma_mr(hdev, tx->tx_msgaddr, nob);
1082 LASSERT (mr != NULL);
1084 sge->lkey = mr->lkey;
1085 sge->addr = tx->tx_msgaddr;
1088 memset(wrq, 0, sizeof(*wrq));
1091 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1094 wrq->opcode = IB_WR_SEND;
1095 wrq->send_flags = IB_SEND_SIGNALED;
1101 kiblnd_init_rdma(kib_conn_t *conn, kib_tx_t *tx, int type,
1102 int resid, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1104 kib_msg_t *ibmsg = tx->tx_msg;
1105 kib_rdma_desc_t *srcrd = tx->tx_rd;
1106 struct ib_sge *sge = &tx->tx_sge[0];
1107 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1113 LASSERT (!in_interrupt());
1114 LASSERT (tx->tx_nwrq == 0);
1115 LASSERT (type == IBLND_MSG_GET_DONE ||
1116 type == IBLND_MSG_PUT_DONE);
1118 srcidx = dstidx = 0;
1121 if (srcidx >= srcrd->rd_nfrags) {
1122 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1127 if (dstidx == dstrd->rd_nfrags) {
1128 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1133 if (tx->tx_nwrq == IBLND_RDMA_FRAGS(conn->ibc_version)) {
1134 CERROR("RDMA too fragmented for %s (%d): "
1135 "%d/%d src %d/%d dst frags\n",
1136 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1137 IBLND_RDMA_FRAGS(conn->ibc_version),
1138 srcidx, srcrd->rd_nfrags,
1139 dstidx, dstrd->rd_nfrags);
1144 wrknob = MIN(MIN(kiblnd_rd_frag_size(srcrd, srcidx),
1145 kiblnd_rd_frag_size(dstrd, dstidx)), resid);
1147 sge = &tx->tx_sge[tx->tx_nwrq];
1148 sge->addr = kiblnd_rd_frag_addr(srcrd, srcidx);
1149 sge->lkey = kiblnd_rd_frag_key(srcrd, srcidx);
1150 sge->length = wrknob;
1152 wrq = &tx->tx_wrq[tx->tx_nwrq];
1154 wrq->next = wrq + 1;
1155 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1158 wrq->opcode = IB_WR_RDMA_WRITE;
1159 wrq->send_flags = 0;
1161 wrq->wr.rdma.remote_addr = kiblnd_rd_frag_addr(dstrd, dstidx);
1162 wrq->wr.rdma.rkey = kiblnd_rd_frag_key(dstrd, dstidx);
1164 srcidx = kiblnd_rd_consume_frag(srcrd, srcidx, wrknob);
1165 dstidx = kiblnd_rd_consume_frag(dstrd, dstidx, wrknob);
1174 if (rc < 0) /* no RDMA if completing with failure */
1177 ibmsg->ibm_u.completion.ibcm_status = rc;
1178 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1179 kiblnd_init_tx_msg(conn->ibc_peer->ibp_ni, tx,
1180 type, sizeof (kib_completion_msg_t));
1186 kiblnd_queue_tx_locked(kib_tx_t *tx, kib_conn_t *conn)
1188 struct list_head *q;
1190 LASSERT(tx->tx_nwrq > 0); /* work items set up */
1191 LASSERT(!tx->tx_queued); /* not queued for sending already */
1192 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1195 tx->tx_deadline = jiffies +
1196 msecs_to_jiffies(*kiblnd_tunables.kib_timeout *
1199 if (tx->tx_conn == NULL) {
1200 kiblnd_conn_addref(conn);
1202 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1204 /* PUT_DONE first attached to conn as a PUT_REQ */
1205 LASSERT (tx->tx_conn == conn);
1206 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1209 switch (tx->tx_msg->ibm_type) {
1213 case IBLND_MSG_PUT_REQ:
1214 case IBLND_MSG_GET_REQ:
1215 q = &conn->ibc_tx_queue_rsrvd;
1218 case IBLND_MSG_PUT_NAK:
1219 case IBLND_MSG_PUT_ACK:
1220 case IBLND_MSG_PUT_DONE:
1221 case IBLND_MSG_GET_DONE:
1222 q = &conn->ibc_tx_queue_nocred;
1225 case IBLND_MSG_NOOP:
1226 if (IBLND_OOB_CAPABLE(conn->ibc_version))
1227 q = &conn->ibc_tx_queue_nocred;
1229 q = &conn->ibc_tx_noops;
1232 case IBLND_MSG_IMMEDIATE:
1233 q = &conn->ibc_tx_queue;
1237 list_add_tail(&tx->tx_list, q);
1241 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1243 spin_lock(&conn->ibc_lock);
1244 kiblnd_queue_tx_locked(tx, conn);
1245 spin_unlock(&conn->ibc_lock);
1247 kiblnd_check_sends(conn);
1250 static int kiblnd_resolve_addr(struct rdma_cm_id *cmid,
1251 struct sockaddr_in *srcaddr,
1252 struct sockaddr_in *dstaddr,
1255 unsigned short port;
1258 /* allow the port to be reused */
1259 rc = rdma_set_reuseaddr(cmid, 1);
1261 CERROR("Unable to set reuse on cmid: %d\n", rc);
1265 /* look for a free privileged port */
1266 for (port = PROT_SOCK-1; port > 0; port--) {
1267 srcaddr->sin_port = htons(port);
1268 rc = rdma_resolve_addr(cmid,
1269 (struct sockaddr *)srcaddr,
1270 (struct sockaddr *)dstaddr,
1273 CDEBUG(D_NET, "bound to port %hu\n", port);
1275 } else if (rc == -EADDRINUSE || rc == -EADDRNOTAVAIL) {
1276 CDEBUG(D_NET, "bind to port %hu failed: %d\n",
1283 CERROR("Failed to bind to a free privileged port\n");
1288 kiblnd_connect_peer (kib_peer_t *peer)
1290 struct rdma_cm_id *cmid;
1292 kib_net_t *net = peer->ibp_ni->ni_data;
1293 struct sockaddr_in srcaddr;
1294 struct sockaddr_in dstaddr;
1297 LASSERT (net != NULL);
1298 LASSERT (peer->ibp_connecting > 0);
1300 cmid = kiblnd_rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP,
1304 CERROR("Can't create CMID for %s: %ld\n",
1305 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1311 memset(&srcaddr, 0, sizeof(srcaddr));
1312 srcaddr.sin_family = AF_INET;
1313 srcaddr.sin_addr.s_addr = htonl(dev->ibd_ifip);
1315 memset(&dstaddr, 0, sizeof(dstaddr));
1316 dstaddr.sin_family = AF_INET;
1317 dstaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1318 dstaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1320 kiblnd_peer_addref(peer); /* cmid's ref */
1322 if (*kiblnd_tunables.kib_use_priv_port) {
1323 rc = kiblnd_resolve_addr(cmid, &srcaddr, &dstaddr,
1324 *kiblnd_tunables.kib_timeout * 1000);
1326 rc = rdma_resolve_addr(cmid,
1327 (struct sockaddr *)&srcaddr,
1328 (struct sockaddr *)&dstaddr,
1329 *kiblnd_tunables.kib_timeout * 1000);
1332 /* Can't initiate address resolution: */
1333 CERROR("Can't resolve addr for %s: %d\n",
1334 libcfs_nid2str(peer->ibp_nid), rc);
1338 LASSERT (cmid->device != NULL);
1339 CDEBUG(D_NET, "%s: connection bound to %s:%pI4h:%s\n",
1340 libcfs_nid2str(peer->ibp_nid), dev->ibd_ifname,
1341 &dev->ibd_ifip, cmid->device->name);
1346 kiblnd_peer_decref(peer); /* cmid's ref */
1347 rdma_destroy_id(cmid);
1349 kiblnd_peer_connect_failed(peer, 1, rc);
1353 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1358 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1359 unsigned long flags;
1362 /* If I get here, I've committed to send, so I complete the tx with
1363 * failure on any problems */
1365 LASSERT (tx == NULL || tx->tx_conn == NULL); /* only set when assigned a conn */
1366 LASSERT (tx == NULL || tx->tx_nwrq > 0); /* work items have been set up */
1368 /* First time, just use a read lock since I expect to find my peer
1370 read_lock_irqsave(g_lock, flags);
1372 peer = kiblnd_find_peer_locked(nid);
1373 if (peer != NULL && !list_empty(&peer->ibp_conns)) {
1374 /* Found a peer with an established connection */
1375 conn = kiblnd_get_conn_locked(peer);
1376 kiblnd_conn_addref(conn); /* 1 ref for me... */
1378 read_unlock_irqrestore(g_lock, flags);
1381 kiblnd_queue_tx(tx, conn);
1382 kiblnd_conn_decref(conn); /* ...to here */
1386 read_unlock(g_lock);
1387 /* Re-try with a write lock */
1390 peer = kiblnd_find_peer_locked(nid);
1392 if (list_empty(&peer->ibp_conns)) {
1393 /* found a peer, but it's still connecting... */
1394 LASSERT (peer->ibp_connecting != 0 ||
1395 peer->ibp_accepting != 0);
1397 list_add_tail(&tx->tx_list,
1398 &peer->ibp_tx_queue);
1399 write_unlock_irqrestore(g_lock, flags);
1401 conn = kiblnd_get_conn_locked(peer);
1402 kiblnd_conn_addref(conn); /* 1 ref for me... */
1404 write_unlock_irqrestore(g_lock, flags);
1407 kiblnd_queue_tx(tx, conn);
1408 kiblnd_conn_decref(conn); /* ...to here */
1413 write_unlock_irqrestore(g_lock, flags);
1415 /* Allocate a peer ready to add to the peer table and retry */
1416 rc = kiblnd_create_peer(ni, &peer, nid);
1418 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1420 tx->tx_status = -EHOSTUNREACH;
1422 kiblnd_tx_done(ni, tx);
1427 write_lock_irqsave(g_lock, flags);
1429 peer2 = kiblnd_find_peer_locked(nid);
1430 if (peer2 != NULL) {
1431 if (list_empty(&peer2->ibp_conns)) {
1432 /* found a peer, but it's still connecting... */
1433 LASSERT (peer2->ibp_connecting != 0 ||
1434 peer2->ibp_accepting != 0);
1436 list_add_tail(&tx->tx_list,
1437 &peer2->ibp_tx_queue);
1438 write_unlock_irqrestore(g_lock, flags);
1440 conn = kiblnd_get_conn_locked(peer2);
1441 kiblnd_conn_addref(conn); /* 1 ref for me... */
1443 write_unlock_irqrestore(g_lock, flags);
1446 kiblnd_queue_tx(tx, conn);
1447 kiblnd_conn_decref(conn); /* ...to here */
1450 kiblnd_peer_decref(peer);
1454 /* Brand new peer */
1455 LASSERT (peer->ibp_connecting == 0);
1456 peer->ibp_connecting = 1;
1458 /* always called with a ref on ni, which prevents ni being shutdown */
1459 LASSERT (((kib_net_t *)ni->ni_data)->ibn_shutdown == 0);
1462 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1464 kiblnd_peer_addref(peer);
1465 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1467 write_unlock_irqrestore(g_lock, flags);
1469 kiblnd_connect_peer(peer);
1470 kiblnd_peer_decref(peer);
1474 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1476 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1477 int type = lntmsg->msg_type;
1478 lnet_process_id_t target = lntmsg->msg_target;
1479 int target_is_router = lntmsg->msg_target_is_router;
1480 int routing = lntmsg->msg_routing;
1481 unsigned int payload_niov = lntmsg->msg_niov;
1482 struct iovec *payload_iov = lntmsg->msg_iov;
1483 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1484 unsigned int payload_offset = lntmsg->msg_offset;
1485 unsigned int payload_nob = lntmsg->msg_len;
1487 kib_rdma_desc_t *rd;
1492 /* NB 'private' is different depending on what we're sending.... */
1494 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1495 payload_nob, payload_niov, libcfs_id2str(target));
1497 LASSERT (payload_nob == 0 || payload_niov > 0);
1498 LASSERT (payload_niov <= LNET_MAX_IOV);
1500 /* Thread context */
1501 LASSERT (!in_interrupt());
1502 /* payload is either all vaddrs or all pages */
1503 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1511 LASSERT (payload_nob == 0);
1515 if (routing || target_is_router)
1516 break; /* send IMMEDIATE */
1518 /* is the REPLY message too small for RDMA? */
1519 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1520 if (nob <= IBLND_MSG_SIZE)
1521 break; /* send IMMEDIATE */
1523 tx = kiblnd_get_idle_tx(ni, target.nid);
1525 CERROR("Can't allocate txd for GET to %s\n",
1526 libcfs_nid2str(target.nid));
1531 rd = &ibmsg->ibm_u.get.ibgm_rd;
1532 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1533 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1534 lntmsg->msg_md->md_niov,
1535 lntmsg->msg_md->md_iov.iov,
1536 0, lntmsg->msg_md->md_length);
1538 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1539 lntmsg->msg_md->md_niov,
1540 lntmsg->msg_md->md_iov.kiov,
1541 0, lntmsg->msg_md->md_length);
1543 CERROR("Can't setup GET sink for %s: %d\n",
1544 libcfs_nid2str(target.nid), rc);
1545 kiblnd_tx_done(ni, tx);
1549 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[rd->rd_nfrags]);
1550 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1551 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1553 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1555 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1556 if (tx->tx_lntmsg[1] == NULL) {
1557 CERROR("Can't create reply for GET -> %s\n",
1558 libcfs_nid2str(target.nid));
1559 kiblnd_tx_done(ni, tx);
1563 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1564 tx->tx_waiting = 1; /* waiting for GET_DONE */
1565 kiblnd_launch_tx(ni, tx, target.nid);
1568 case LNET_MSG_REPLY:
1570 /* Is the payload small enough not to need RDMA? */
1571 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1572 if (nob <= IBLND_MSG_SIZE)
1573 break; /* send IMMEDIATE */
1575 tx = kiblnd_get_idle_tx(ni, target.nid);
1577 CERROR("Can't allocate %s txd for %s\n",
1578 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1579 libcfs_nid2str(target.nid));
1583 if (payload_kiov == NULL)
1584 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1585 payload_niov, payload_iov,
1586 payload_offset, payload_nob);
1588 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1589 payload_niov, payload_kiov,
1590 payload_offset, payload_nob);
1592 CERROR("Can't setup PUT src for %s: %d\n",
1593 libcfs_nid2str(target.nid), rc);
1594 kiblnd_tx_done(ni, tx);
1599 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1600 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1601 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1603 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1604 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1605 kiblnd_launch_tx(ni, tx, target.nid);
1609 /* send IMMEDIATE */
1611 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1614 tx = kiblnd_get_idle_tx(ni, target.nid);
1616 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1617 type, libcfs_nid2str(target.nid));
1622 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1624 if (payload_kiov != NULL)
1625 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1626 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1627 payload_niov, payload_kiov,
1628 payload_offset, payload_nob);
1630 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1631 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1632 payload_niov, payload_iov,
1633 payload_offset, payload_nob);
1635 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1636 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1638 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1639 kiblnd_launch_tx(ni, tx, target.nid);
1644 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1646 lnet_process_id_t target = lntmsg->msg_target;
1647 unsigned int niov = lntmsg->msg_niov;
1648 struct iovec *iov = lntmsg->msg_iov;
1649 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1650 unsigned int offset = lntmsg->msg_offset;
1651 unsigned int nob = lntmsg->msg_len;
1655 tx = kiblnd_get_idle_tx(ni, rx->rx_conn->ibc_peer->ibp_nid);
1657 CERROR("Can't get tx for REPLY to %s\n",
1658 libcfs_nid2str(target.nid));
1664 else if (kiov == NULL)
1665 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1666 niov, iov, offset, nob);
1668 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1669 niov, kiov, offset, nob);
1672 CERROR("Can't setup GET src for %s: %d\n",
1673 libcfs_nid2str(target.nid), rc);
1677 rc = kiblnd_init_rdma(rx->rx_conn, tx,
1678 IBLND_MSG_GET_DONE, nob,
1679 &rx->rx_msg->ibm_u.get.ibgm_rd,
1680 rx->rx_msg->ibm_u.get.ibgm_cookie);
1682 CERROR("Can't setup rdma for GET from %s: %d\n",
1683 libcfs_nid2str(target.nid), rc);
1688 /* No RDMA: local completion may happen now! */
1689 lnet_finalize(ni, lntmsg, 0);
1691 /* RDMA: lnet_finalize(lntmsg) when it
1693 tx->tx_lntmsg[0] = lntmsg;
1696 kiblnd_queue_tx(tx, rx->rx_conn);
1700 kiblnd_tx_done(ni, tx);
1702 lnet_finalize(ni, lntmsg, -EIO);
1706 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1707 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1708 unsigned int offset, unsigned int mlen, unsigned int rlen)
1710 kib_rx_t *rx = private;
1711 kib_msg_t *rxmsg = rx->rx_msg;
1712 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 kib_rdma_desc_t *rd;
1755 lnet_finalize(ni, lntmsg, 0);
1756 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1757 rxmsg->ibm_u.putreq.ibprm_cookie);
1761 tx = kiblnd_get_idle_tx(ni, conn->ibc_peer->ibp_nid);
1763 CERROR("Can't allocate tx for %s\n",
1764 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1765 /* Not replying will break the connection */
1771 rd = &txmsg->ibm_u.putack.ibpam_rd;
1773 rc = kiblnd_setup_rd_iov(ni, tx, rd,
1774 niov, iov, offset, mlen);
1776 rc = kiblnd_setup_rd_kiov(ni, tx, rd,
1777 niov, kiov, offset, mlen);
1779 CERROR("Can't setup PUT sink for %s: %d\n",
1780 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
1781 kiblnd_tx_done(ni, tx);
1782 /* tell peer it's over */
1783 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, rc,
1784 rxmsg->ibm_u.putreq.ibprm_cookie);
1788 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[rd->rd_nfrags]);
1789 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1790 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1792 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1794 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1795 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1796 kiblnd_queue_tx(tx, conn);
1798 /* reposted buffer reserved for PUT_DONE */
1799 post_credit = IBLND_POSTRX_NO_CREDIT;
1803 case IBLND_MSG_GET_REQ:
1804 if (lntmsg != NULL) {
1805 /* Optimized GET; RDMA lntmsg's payload */
1806 kiblnd_reply(ni, rx, lntmsg);
1808 /* GET didn't match anything */
1809 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1811 rxmsg->ibm_u.get.ibgm_cookie);
1816 kiblnd_post_rx(rx, post_credit);
1821 kiblnd_thread_start(int (*fn)(void *arg), void *arg, char *name)
1823 struct task_struct *task = kthread_run(fn, arg, name);
1826 return PTR_ERR(task);
1828 atomic_inc(&kiblnd_data.kib_nthreads);
1833 kiblnd_thread_fini (void)
1835 atomic_dec (&kiblnd_data.kib_nthreads);
1839 kiblnd_peer_alive (kib_peer_t *peer)
1841 /* This is racy, but everyone's only writing cfs_time_current() */
1842 peer->ibp_last_alive = cfs_time_current();
1847 kiblnd_peer_notify (kib_peer_t *peer)
1850 cfs_time_t last_alive = 0;
1851 unsigned long flags;
1853 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1855 if (list_empty(&peer->ibp_conns) &&
1856 peer->ibp_accepting == 0 &&
1857 peer->ibp_connecting == 0 &&
1858 peer->ibp_error != 0) {
1859 error = peer->ibp_error;
1860 peer->ibp_error = 0;
1862 last_alive = peer->ibp_last_alive;
1865 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1868 lnet_notify(peer->ibp_ni,
1869 peer->ibp_nid, 0, last_alive);
1873 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1875 /* This just does the immediate housekeeping. 'error' is zero for a
1876 * normal shutdown which can happen only after the connection has been
1877 * established. If the connection is established, schedule the
1878 * connection to be finished off by the connd. Otherwise the connd is
1879 * already dealing with it (either to set it up or tear it down).
1880 * Caller holds kib_global_lock exclusively in irq context */
1881 kib_peer_t *peer = conn->ibc_peer;
1883 unsigned long flags;
1885 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1887 if (error != 0 && conn->ibc_comms_error == 0)
1888 conn->ibc_comms_error = error;
1890 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1891 return; /* already being handled */
1894 list_empty(&conn->ibc_tx_noops) &&
1895 list_empty(&conn->ibc_tx_queue) &&
1896 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1897 list_empty(&conn->ibc_tx_queue_nocred) &&
1898 list_empty(&conn->ibc_active_txs)) {
1899 CDEBUG(D_NET, "closing conn to %s\n",
1900 libcfs_nid2str(peer->ibp_nid));
1902 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1903 libcfs_nid2str(peer->ibp_nid), error,
1904 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1905 list_empty(&conn->ibc_tx_noops) ? "" : "(sending_noops)",
1906 list_empty(&conn->ibc_tx_queue_rsrvd) ?
1907 "" : "(sending_rsrvd)",
1908 list_empty(&conn->ibc_tx_queue_nocred) ?
1909 "" : "(sending_nocred)",
1910 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1913 dev = ((kib_net_t *)peer->ibp_ni->ni_data)->ibn_dev;
1914 list_del(&conn->ibc_list);
1915 /* connd (see below) takes over ibc_list's ref */
1917 if (list_empty(&peer->ibp_conns) && /* no more conns */
1918 kiblnd_peer_active(peer)) { /* still in peer table */
1919 kiblnd_unlink_peer_locked(peer);
1921 /* set/clear error on last conn */
1922 peer->ibp_error = conn->ibc_comms_error;
1925 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1928 kiblnd_dev_can_failover(dev)) {
1929 list_add_tail(&dev->ibd_fail_list,
1930 &kiblnd_data.kib_failed_devs);
1931 wake_up(&kiblnd_data.kib_failover_waitq);
1934 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1936 list_add_tail(&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1937 wake_up(&kiblnd_data.kib_connd_waitq);
1939 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1943 kiblnd_close_conn(kib_conn_t *conn, int error)
1945 unsigned long flags;
1947 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1949 kiblnd_close_conn_locked(conn, error);
1951 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1955 kiblnd_handle_early_rxs(kib_conn_t *conn)
1957 unsigned long flags;
1960 LASSERT(!in_interrupt());
1961 LASSERT(conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1963 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1964 while (!list_empty(&conn->ibc_early_rxs)) {
1965 rx = list_entry(conn->ibc_early_rxs.next,
1967 list_del(&rx->rx_list);
1968 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1970 kiblnd_handle_rx(rx);
1972 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1974 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1978 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1980 struct list_head zombies = LIST_HEAD_INIT(zombies);
1981 struct list_head *tmp;
1982 struct list_head *nxt;
1985 spin_lock(&conn->ibc_lock);
1987 list_for_each_safe(tmp, nxt, txs) {
1988 tx = list_entry(tmp, kib_tx_t, tx_list);
1990 if (txs == &conn->ibc_active_txs) {
1991 LASSERT(!tx->tx_queued);
1992 LASSERT(tx->tx_waiting ||
1993 tx->tx_sending != 0);
1995 LASSERT(tx->tx_queued);
1998 tx->tx_status = -ECONNABORTED;
2001 if (tx->tx_sending == 0) {
2003 list_del(&tx->tx_list);
2004 list_add(&tx->tx_list, &zombies);
2008 spin_unlock(&conn->ibc_lock);
2010 kiblnd_txlist_done(conn->ibc_peer->ibp_ni, &zombies, -ECONNABORTED);
2014 kiblnd_finalise_conn (kib_conn_t *conn)
2016 LASSERT (!in_interrupt());
2017 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2019 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2021 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2022 * for connections that didn't get as far as being connected, because
2023 * rdma_disconnect() does this for free. */
2024 kiblnd_abort_receives(conn);
2026 /* Complete all tx descs not waiting for sends to complete.
2027 * NB we should be safe from RDMA now that the QP has changed state */
2029 kiblnd_abort_txs(conn, &conn->ibc_tx_noops);
2030 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2031 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2032 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2033 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2035 kiblnd_handle_early_rxs(conn);
2039 kiblnd_peer_connect_failed(kib_peer_t *peer, int active, int error)
2041 struct list_head zombies = LIST_HEAD_INIT(zombies);
2042 unsigned long flags;
2044 LASSERT (error != 0);
2045 LASSERT (!in_interrupt());
2047 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2050 LASSERT (peer->ibp_connecting > 0);
2051 peer->ibp_connecting--;
2053 LASSERT (peer->ibp_accepting > 0);
2054 peer->ibp_accepting--;
2057 if (peer->ibp_connecting != 0 ||
2058 peer->ibp_accepting != 0) {
2059 /* another connection attempt under way... */
2060 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2065 if (list_empty(&peer->ibp_conns)) {
2066 /* Take peer's blocked transmits to complete with error */
2067 list_add(&zombies, &peer->ibp_tx_queue);
2068 list_del_init(&peer->ibp_tx_queue);
2070 if (kiblnd_peer_active(peer))
2071 kiblnd_unlink_peer_locked(peer);
2073 peer->ibp_error = error;
2075 /* Can't have blocked transmits if there are connections */
2076 LASSERT(list_empty(&peer->ibp_tx_queue));
2079 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2081 kiblnd_peer_notify(peer);
2083 if (list_empty(&zombies))
2086 CNETERR("Deleting messages for %s: connection failed\n",
2087 libcfs_nid2str(peer->ibp_nid));
2089 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2093 kiblnd_connreq_done(kib_conn_t *conn, int status)
2095 kib_peer_t *peer = conn->ibc_peer;
2097 struct list_head txs;
2098 unsigned long flags;
2101 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2103 CDEBUG(D_NET,"%s: active(%d), version(%x), status(%d)\n",
2104 libcfs_nid2str(peer->ibp_nid), active,
2105 conn->ibc_version, status);
2107 LASSERT (!in_interrupt());
2108 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2109 peer->ibp_connecting > 0) ||
2110 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2111 peer->ibp_accepting > 0));
2113 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2114 conn->ibc_connvars = NULL;
2117 /* failed to establish connection */
2118 kiblnd_peer_connect_failed(peer, active, status);
2119 kiblnd_finalise_conn(conn);
2123 /* connection established */
2124 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2126 conn->ibc_last_send = jiffies;
2127 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2128 kiblnd_peer_alive(peer);
2130 /* Add conn to peer's list and nuke any dangling conns from a different
2131 * peer instance... */
2132 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2133 list_add(&conn->ibc_list, &peer->ibp_conns);
2135 peer->ibp_connecting--;
2137 peer->ibp_accepting--;
2139 if (peer->ibp_version == 0) {
2140 peer->ibp_version = conn->ibc_version;
2141 peer->ibp_incarnation = conn->ibc_incarnation;
2144 if (peer->ibp_version != conn->ibc_version ||
2145 peer->ibp_incarnation != conn->ibc_incarnation) {
2146 kiblnd_close_stale_conns_locked(peer, conn->ibc_version,
2147 conn->ibc_incarnation);
2148 peer->ibp_version = conn->ibc_version;
2149 peer->ibp_incarnation = conn->ibc_incarnation;
2152 /* grab pending txs while I have the lock */
2153 list_add(&txs, &peer->ibp_tx_queue);
2154 list_del_init(&peer->ibp_tx_queue);
2156 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2157 conn->ibc_comms_error != 0) { /* error has happened already */
2158 lnet_ni_t *ni = peer->ibp_ni;
2160 /* start to shut down connection */
2161 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2162 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2164 kiblnd_txlist_done(ni, &txs, -ECONNABORTED);
2169 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2171 /* Schedule blocked txs */
2172 spin_lock(&conn->ibc_lock);
2173 while (!list_empty(&txs)) {
2174 tx = list_entry(txs.next, kib_tx_t, tx_list);
2175 list_del(&tx->tx_list);
2177 kiblnd_queue_tx_locked(tx, conn);
2179 spin_unlock(&conn->ibc_lock);
2181 kiblnd_check_sends(conn);
2183 /* schedule blocked rxs */
2184 kiblnd_handle_early_rxs(conn);
2188 kiblnd_reject(struct rdma_cm_id *cmid, kib_rej_t *rej)
2192 rc = rdma_reject(cmid, rej, sizeof(*rej));
2195 CWARN("Error %d sending reject\n", rc);
2199 kiblnd_passive_connect(struct rdma_cm_id *cmid, void *priv, int priv_nob)
2201 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2202 kib_msg_t *reqmsg = priv;
2208 lnet_ni_t *ni = NULL;
2209 kib_net_t *net = NULL;
2211 struct rdma_conn_param cp;
2213 int version = IBLND_MSG_VERSION;
2214 unsigned long flags;
2216 struct sockaddr_in *peer_addr;
2217 LASSERT (!in_interrupt());
2219 /* cmid inherits 'context' from the corresponding listener id */
2220 ibdev = (kib_dev_t *)cmid->context;
2221 LASSERT (ibdev != NULL);
2223 memset(&rej, 0, sizeof(rej));
2224 rej.ibr_magic = IBLND_MSG_MAGIC;
2225 rej.ibr_why = IBLND_REJECT_FATAL;
2226 rej.ibr_cp.ibcp_max_msg_size = IBLND_MSG_SIZE;
2228 peer_addr = (struct sockaddr_in *)&(cmid->route.addr.dst_addr);
2229 if (*kiblnd_tunables.kib_require_priv_port &&
2230 ntohs(peer_addr->sin_port) >= PROT_SOCK) {
2231 __u32 ip = ntohl(peer_addr->sin_addr.s_addr);
2232 CERROR("Peer's port (%pI4h:%hu) is not privileged\n",
2233 &ip, ntohs(peer_addr->sin_port));
2237 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2238 CERROR("Short connection request\n");
2242 /* Future protocol version compatibility support! If the
2243 * o2iblnd-specific protocol changes, or when LNET unifies
2244 * protocols over all LNDs, the initial connection will
2245 * negotiate a protocol version. I trap this here to avoid
2246 * console errors; the reject tells the peer which protocol I
2248 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2249 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC))
2251 if (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2252 reqmsg->ibm_version != IBLND_MSG_VERSION &&
2253 reqmsg->ibm_version != IBLND_MSG_VERSION_1)
2255 if (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2256 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION) &&
2257 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION_1))
2260 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2262 CERROR("Can't parse connection request: %d\n", rc);
2266 nid = reqmsg->ibm_srcnid;
2267 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2270 net = (kib_net_t *)ni->ni_data;
2271 rej.ibr_incarnation = net->ibn_incarnation;
2274 if (ni == NULL || /* no matching net */
2275 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2276 net->ibn_dev != ibdev) { /* wrong device */
2277 CERROR("Can't accept %s on %s (%s:%d:%pI4h): "
2278 "bad dst nid %s\n", libcfs_nid2str(nid),
2279 ni == NULL ? "NA" : libcfs_nid2str(ni->ni_nid),
2280 ibdev->ibd_ifname, ibdev->ibd_nnets,
2282 libcfs_nid2str(reqmsg->ibm_dstnid));
2287 /* check time stamp as soon as possible */
2288 if (reqmsg->ibm_dststamp != 0 &&
2289 reqmsg->ibm_dststamp != net->ibn_incarnation) {
2290 CWARN("Stale connection request\n");
2291 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2295 /* I can accept peer's version */
2296 version = reqmsg->ibm_version;
2298 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2299 CERROR("Unexpected connreq msg type: %x from %s\n",
2300 reqmsg->ibm_type, libcfs_nid2str(nid));
2304 if (reqmsg->ibm_u.connparams.ibcp_queue_depth !=
2305 IBLND_MSG_QUEUE_SIZE(version)) {
2306 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2307 libcfs_nid2str(nid), reqmsg->ibm_u.connparams.ibcp_queue_depth,
2308 IBLND_MSG_QUEUE_SIZE(version));
2310 if (version == IBLND_MSG_VERSION)
2311 rej.ibr_why = IBLND_REJECT_MSG_QUEUE_SIZE;
2316 if (reqmsg->ibm_u.connparams.ibcp_max_frags !=
2317 IBLND_RDMA_FRAGS(version)) {
2318 CERROR("Can't accept %s(version %x): "
2319 "incompatible max_frags %d (%d wanted)\n",
2320 libcfs_nid2str(nid), version,
2321 reqmsg->ibm_u.connparams.ibcp_max_frags,
2322 IBLND_RDMA_FRAGS(version));
2324 if (version == IBLND_MSG_VERSION)
2325 rej.ibr_why = IBLND_REJECT_RDMA_FRAGS;
2331 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2332 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2333 libcfs_nid2str(nid),
2334 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2339 /* assume 'nid' is a new peer; create */
2340 rc = kiblnd_create_peer(ni, &peer, nid);
2342 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2343 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2347 write_lock_irqsave(g_lock, flags);
2349 peer2 = kiblnd_find_peer_locked(nid);
2350 if (peer2 != NULL) {
2351 if (peer2->ibp_version == 0) {
2352 peer2->ibp_version = version;
2353 peer2->ibp_incarnation = reqmsg->ibm_srcstamp;
2356 /* not the guy I've talked with */
2357 if (peer2->ibp_incarnation != reqmsg->ibm_srcstamp ||
2358 peer2->ibp_version != version) {
2359 kiblnd_close_peer_conns_locked(peer2, -ESTALE);
2360 write_unlock_irqrestore(g_lock, flags);
2362 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2363 libcfs_nid2str(nid), peer2->ibp_version, version);
2365 kiblnd_peer_decref(peer);
2366 rej.ibr_why = IBLND_REJECT_CONN_STALE;
2370 /* tie-break connection race in favour of the higher NID */
2371 if (peer2->ibp_connecting != 0 &&
2373 write_unlock_irqrestore(g_lock, flags);
2375 CWARN("Conn race %s\n", libcfs_nid2str(peer2->ibp_nid));
2377 kiblnd_peer_decref(peer);
2378 rej.ibr_why = IBLND_REJECT_CONN_RACE;
2382 peer2->ibp_accepting++;
2383 kiblnd_peer_addref(peer2);
2385 write_unlock_irqrestore(g_lock, flags);
2386 kiblnd_peer_decref(peer);
2389 /* Brand new peer */
2390 LASSERT (peer->ibp_accepting == 0);
2391 LASSERT (peer->ibp_version == 0 &&
2392 peer->ibp_incarnation == 0);
2394 peer->ibp_accepting = 1;
2395 peer->ibp_version = version;
2396 peer->ibp_incarnation = reqmsg->ibm_srcstamp;
2398 /* I have a ref on ni that prevents it being shutdown */
2399 LASSERT (net->ibn_shutdown == 0);
2401 kiblnd_peer_addref(peer);
2402 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2404 write_unlock_irqrestore(g_lock, flags);
2407 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT, version);
2409 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2410 kiblnd_peer_decref(peer);
2411 rej.ibr_why = IBLND_REJECT_NO_RESOURCES;
2415 /* conn now "owns" cmid, so I return success from here on to ensure the
2416 * CM callback doesn't destroy cmid. */
2418 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2419 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE(version);
2420 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(version);
2421 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(version)
2422 <= IBLND_RX_MSGS(version));
2424 ackmsg = &conn->ibc_connvars->cv_msg;
2425 memset(ackmsg, 0, sizeof(*ackmsg));
2427 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2428 sizeof(ackmsg->ibm_u.connparams));
2429 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2430 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2431 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2433 kiblnd_pack_msg(ni, ackmsg, version, 0, nid, reqmsg->ibm_srcstamp);
2435 memset(&cp, 0, sizeof(cp));
2436 cp.private_data = ackmsg;
2437 cp.private_data_len = ackmsg->ibm_nob;
2438 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2439 cp.initiator_depth = 0;
2440 cp.flow_control = 1;
2441 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2442 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2444 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2446 rc = rdma_accept(cmid, &cp);
2448 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2449 rej.ibr_version = version;
2450 rej.ibr_why = IBLND_REJECT_FATAL;
2452 kiblnd_reject(cmid, &rej);
2453 kiblnd_connreq_done(conn, rc);
2454 kiblnd_conn_decref(conn);
2464 rej.ibr_version = version;
2465 rej.ibr_cp.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2466 rej.ibr_cp.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2467 kiblnd_reject(cmid, &rej);
2469 return -ECONNREFUSED;
2473 kiblnd_reconnect (kib_conn_t *conn, int version,
2474 __u64 incarnation, int why, kib_connparams_t *cp)
2476 kib_peer_t *peer = conn->ibc_peer;
2479 unsigned long flags;
2481 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2482 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2484 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2486 /* retry connection if it's still needed and no other connection
2487 * attempts (active or passive) are in progress
2488 * NB: reconnect is still needed even when ibp_tx_queue is
2489 * empty if ibp_version != version because reconnect may be
2490 * initiated by kiblnd_query() */
2491 if ((!list_empty(&peer->ibp_tx_queue) ||
2492 peer->ibp_version != version) &&
2493 peer->ibp_connecting == 1 &&
2494 peer->ibp_accepting == 0) {
2495 if (why == IBLND_REJECT_CONN_RACE) {
2496 /* don't reconnect immediately, intensive reconnecting
2497 * may consume a lot of memory. kiblnd_destroy_conn
2498 * will reconnect after releasing all resources of
2499 * this connection */
2500 conn->ibc_conn_race = 1;
2504 peer->ibp_connecting++;
2506 peer->ibp_version = version;
2507 peer->ibp_incarnation = incarnation;
2510 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2520 case IBLND_REJECT_CONN_STALE:
2524 case IBLND_REJECT_CONN_UNCOMPAT:
2525 reason = "version negotiation";
2529 CNETERR("%s: retrying (%s), %x, %x, "
2530 "queue_dep: %d, max_frag: %d, msg_size: %d\n",
2531 libcfs_nid2str(peer->ibp_nid),
2532 reason, IBLND_MSG_VERSION, version,
2533 cp != NULL? cp->ibcp_queue_depth :IBLND_MSG_QUEUE_SIZE(version),
2534 cp != NULL? cp->ibcp_max_frags : IBLND_RDMA_FRAGS(version),
2535 cp != NULL? cp->ibcp_max_msg_size: IBLND_MSG_SIZE);
2537 kiblnd_connect_peer(peer);
2541 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2543 kib_peer_t *peer = conn->ibc_peer;
2545 LASSERT (!in_interrupt());
2546 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2549 case IB_CM_REJ_STALE_CONN:
2550 kiblnd_reconnect(conn, IBLND_MSG_VERSION, 0,
2551 IBLND_REJECT_CONN_STALE, NULL);
2554 case IB_CM_REJ_INVALID_SERVICE_ID:
2555 CNETERR("%s rejected: no listener at %d\n",
2556 libcfs_nid2str(peer->ibp_nid),
2557 *kiblnd_tunables.kib_service);
2560 case IB_CM_REJ_CONSUMER_DEFINED:
2561 if (priv_nob >= offsetof(kib_rej_t, ibr_padding)) {
2562 kib_rej_t *rej = priv;
2563 kib_connparams_t *cp = NULL;
2565 __u64 incarnation = -1;
2567 /* NB. default incarnation is -1 because:
2568 * a) V1 will ignore dst incarnation in connreq.
2569 * b) V2 will provide incarnation while rejecting me,
2570 * -1 will be overwrote.
2572 * if I try to connect to a V1 peer with V2 protocol,
2573 * it rejected me then upgrade to V2, I have no idea
2574 * about the upgrading and try to reconnect with V1,
2575 * in this case upgraded V2 can find out I'm trying to
2576 * talk to the old guy and reject me(incarnation is -1).
2579 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2580 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2581 __swab32s(&rej->ibr_magic);
2582 __swab16s(&rej->ibr_version);
2586 if (priv_nob >= sizeof(kib_rej_t) &&
2587 rej->ibr_version > IBLND_MSG_VERSION_1) {
2588 /* priv_nob is always 148 in current version
2589 * of OFED, so we still need to check version.
2590 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2594 __swab64s(&rej->ibr_incarnation);
2595 __swab16s(&cp->ibcp_queue_depth);
2596 __swab16s(&cp->ibcp_max_frags);
2597 __swab32s(&cp->ibcp_max_msg_size);
2600 incarnation = rej->ibr_incarnation;
2603 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2604 rej->ibr_magic != LNET_PROTO_MAGIC) {
2605 CERROR("%s rejected: consumer defined fatal error\n",
2606 libcfs_nid2str(peer->ibp_nid));
2610 if (rej->ibr_version != IBLND_MSG_VERSION &&
2611 rej->ibr_version != IBLND_MSG_VERSION_1) {
2612 CERROR("%s rejected: o2iblnd version %x error\n",
2613 libcfs_nid2str(peer->ibp_nid),
2618 if (rej->ibr_why == IBLND_REJECT_FATAL &&
2619 rej->ibr_version == IBLND_MSG_VERSION_1) {
2620 CDEBUG(D_NET, "rejected by old version peer %s: %x\n",
2621 libcfs_nid2str(peer->ibp_nid), rej->ibr_version);
2623 if (conn->ibc_version != IBLND_MSG_VERSION_1)
2624 rej->ibr_why = IBLND_REJECT_CONN_UNCOMPAT;
2627 switch (rej->ibr_why) {
2628 case IBLND_REJECT_CONN_RACE:
2629 case IBLND_REJECT_CONN_STALE:
2630 case IBLND_REJECT_CONN_UNCOMPAT:
2631 kiblnd_reconnect(conn, rej->ibr_version,
2632 incarnation, rej->ibr_why, cp);
2635 case IBLND_REJECT_MSG_QUEUE_SIZE:
2636 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2637 libcfs_nid2str(peer->ibp_nid),
2638 cp != NULL ? cp->ibcp_queue_depth :
2639 IBLND_MSG_QUEUE_SIZE(rej->ibr_version),
2640 IBLND_MSG_QUEUE_SIZE(conn->ibc_version));
2643 case IBLND_REJECT_RDMA_FRAGS:
2644 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2645 libcfs_nid2str(peer->ibp_nid),
2646 cp != NULL ? cp->ibcp_max_frags :
2647 IBLND_RDMA_FRAGS(rej->ibr_version),
2648 IBLND_RDMA_FRAGS(conn->ibc_version));
2651 case IBLND_REJECT_NO_RESOURCES:
2652 CERROR("%s rejected: o2iblnd no resources\n",
2653 libcfs_nid2str(peer->ibp_nid));
2656 case IBLND_REJECT_FATAL:
2657 CERROR("%s rejected: o2iblnd fatal error\n",
2658 libcfs_nid2str(peer->ibp_nid));
2662 CERROR("%s rejected: o2iblnd reason %d\n",
2663 libcfs_nid2str(peer->ibp_nid),
2671 CNETERR("%s rejected: reason %d, size %d\n",
2672 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2676 kiblnd_connreq_done(conn, -ECONNREFUSED);
2680 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2682 kib_peer_t *peer = conn->ibc_peer;
2683 lnet_ni_t *ni = peer->ibp_ni;
2684 kib_net_t *net = ni->ni_data;
2685 kib_msg_t *msg = priv;
2686 int ver = conn->ibc_version;
2687 int rc = kiblnd_unpack_msg(msg, priv_nob);
2688 unsigned long flags;
2690 LASSERT (net != NULL);
2693 CERROR("Can't unpack connack from %s: %d\n",
2694 libcfs_nid2str(peer->ibp_nid), rc);
2698 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2699 CERROR("Unexpected message %d from %s\n",
2700 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2705 if (ver != msg->ibm_version) {
2706 CERROR("%s replied version %x is different with "
2707 "requested version %x\n",
2708 libcfs_nid2str(peer->ibp_nid), msg->ibm_version, ver);
2713 if (msg->ibm_u.connparams.ibcp_queue_depth !=
2714 IBLND_MSG_QUEUE_SIZE(ver)) {
2715 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2716 libcfs_nid2str(peer->ibp_nid),
2717 msg->ibm_u.connparams.ibcp_queue_depth,
2718 IBLND_MSG_QUEUE_SIZE(ver));
2723 if (msg->ibm_u.connparams.ibcp_max_frags !=
2724 IBLND_RDMA_FRAGS(ver)) {
2725 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2726 libcfs_nid2str(peer->ibp_nid),
2727 msg->ibm_u.connparams.ibcp_max_frags,
2728 IBLND_RDMA_FRAGS(ver));
2733 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2734 CERROR("%s max message size %d too big (%d max)\n",
2735 libcfs_nid2str(peer->ibp_nid),
2736 msg->ibm_u.connparams.ibcp_max_msg_size,
2742 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2743 if (msg->ibm_dstnid == ni->ni_nid &&
2744 msg->ibm_dststamp == net->ibn_incarnation)
2748 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2751 CERROR("Bad connection reply from %s, rc = %d, "
2752 "version: %x max_frags: %d\n",
2753 libcfs_nid2str(peer->ibp_nid), rc,
2754 msg->ibm_version, msg->ibm_u.connparams.ibcp_max_frags);
2758 conn->ibc_incarnation = msg->ibm_srcstamp;
2760 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE(ver);
2761 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits + IBLND_OOB_MSGS(ver)
2762 <= IBLND_RX_MSGS(ver));
2764 kiblnd_connreq_done(conn, 0);
2768 /* NB My QP has already established itself, so I handle anything going
2769 * wrong here by setting ibc_comms_error.
2770 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2771 * immediately tears it down. */
2774 conn->ibc_comms_error = rc;
2775 kiblnd_connreq_done(conn, 0);
2779 kiblnd_active_connect (struct rdma_cm_id *cmid)
2781 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2784 struct rdma_conn_param cp;
2787 unsigned long flags;
2790 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2792 incarnation = peer->ibp_incarnation;
2793 version = (peer->ibp_version == 0) ? IBLND_MSG_VERSION :
2796 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2798 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT, version);
2800 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2801 kiblnd_peer_decref(peer); /* lose cmid's ref */
2805 /* conn "owns" cmid now, so I return success from here on to ensure the
2806 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2809 msg = &conn->ibc_connvars->cv_msg;
2811 memset(msg, 0, sizeof(*msg));
2812 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2813 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE(version);
2814 msg->ibm_u.connparams.ibcp_max_frags = IBLND_RDMA_FRAGS(version);
2815 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2817 kiblnd_pack_msg(peer->ibp_ni, msg, version,
2818 0, peer->ibp_nid, incarnation);
2820 memset(&cp, 0, sizeof(cp));
2821 cp.private_data = msg;
2822 cp.private_data_len = msg->ibm_nob;
2823 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2824 cp.initiator_depth = 0;
2825 cp.flow_control = 1;
2826 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2827 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2829 LASSERT(cmid->context == (void *)conn);
2830 LASSERT(conn->ibc_cmid == cmid);
2832 rc = rdma_connect(cmid, &cp);
2834 CERROR("Can't connect to %s: %d\n",
2835 libcfs_nid2str(peer->ibp_nid), rc);
2836 kiblnd_connreq_done(conn, rc);
2837 kiblnd_conn_decref(conn);
2844 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2850 switch (event->event) {
2852 CERROR("Unexpected event: %d, status: %d\n",
2853 event->event, event->status);
2856 case RDMA_CM_EVENT_CONNECT_REQUEST:
2857 /* destroy cmid on failure */
2858 rc = kiblnd_passive_connect(cmid,
2859 (void *)KIBLND_CONN_PARAM(event),
2860 KIBLND_CONN_PARAM_LEN(event));
2861 CDEBUG(D_NET, "connreq: %d\n", rc);
2864 case RDMA_CM_EVENT_ADDR_ERROR:
2865 peer = (kib_peer_t *)cmid->context;
2866 CNETERR("%s: ADDR ERROR %d\n",
2867 libcfs_nid2str(peer->ibp_nid), event->status);
2868 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2869 kiblnd_peer_decref(peer);
2870 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2872 case RDMA_CM_EVENT_ADDR_RESOLVED:
2873 peer = (kib_peer_t *)cmid->context;
2875 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2876 libcfs_nid2str(peer->ibp_nid), event->status);
2878 if (event->status != 0) {
2879 CNETERR("Can't resolve address for %s: %d\n",
2880 libcfs_nid2str(peer->ibp_nid), event->status);
2883 rc = rdma_resolve_route(
2884 cmid, *kiblnd_tunables.kib_timeout * 1000);
2887 /* Can't initiate route resolution */
2888 CERROR("Can't resolve route for %s: %d\n",
2889 libcfs_nid2str(peer->ibp_nid), rc);
2891 kiblnd_peer_connect_failed(peer, 1, rc);
2892 kiblnd_peer_decref(peer);
2893 return rc; /* rc != 0 destroys cmid */
2895 case RDMA_CM_EVENT_ROUTE_ERROR:
2896 peer = (kib_peer_t *)cmid->context;
2897 CNETERR("%s: ROUTE ERROR %d\n",
2898 libcfs_nid2str(peer->ibp_nid), event->status);
2899 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2900 kiblnd_peer_decref(peer);
2901 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2903 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2904 peer = (kib_peer_t *)cmid->context;
2905 CDEBUG(D_NET,"%s Route resolved: %d\n",
2906 libcfs_nid2str(peer->ibp_nid), event->status);
2908 if (event->status == 0)
2909 return kiblnd_active_connect(cmid);
2911 CNETERR("Can't resolve route for %s: %d\n",
2912 libcfs_nid2str(peer->ibp_nid), event->status);
2913 kiblnd_peer_connect_failed(peer, 1, event->status);
2914 kiblnd_peer_decref(peer);
2915 return event->status; /* rc != 0 destroys cmid */
2917 case RDMA_CM_EVENT_UNREACHABLE:
2918 conn = (kib_conn_t *)cmid->context;
2919 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2920 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2921 CNETERR("%s: UNREACHABLE %d\n",
2922 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2923 kiblnd_connreq_done(conn, -ENETDOWN);
2924 kiblnd_conn_decref(conn);
2927 case RDMA_CM_EVENT_CONNECT_ERROR:
2928 conn = (kib_conn_t *)cmid->context;
2929 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2930 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2931 CNETERR("%s: CONNECT ERROR %d\n",
2932 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2933 kiblnd_connreq_done(conn, -ENOTCONN);
2934 kiblnd_conn_decref(conn);
2937 case RDMA_CM_EVENT_REJECTED:
2938 conn = (kib_conn_t *)cmid->context;
2939 switch (conn->ibc_state) {
2943 case IBLND_CONN_PASSIVE_WAIT:
2944 CERROR ("%s: REJECTED %d\n",
2945 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2947 kiblnd_connreq_done(conn, -ECONNRESET);
2950 case IBLND_CONN_ACTIVE_CONNECT:
2951 kiblnd_rejected(conn, event->status,
2952 (void *)KIBLND_CONN_PARAM(event),
2953 KIBLND_CONN_PARAM_LEN(event));
2956 kiblnd_conn_decref(conn);
2959 case RDMA_CM_EVENT_ESTABLISHED:
2960 conn = (kib_conn_t *)cmid->context;
2961 switch (conn->ibc_state) {
2965 case IBLND_CONN_PASSIVE_WAIT:
2966 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2967 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2968 kiblnd_connreq_done(conn, 0);
2971 case IBLND_CONN_ACTIVE_CONNECT:
2972 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2973 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2974 kiblnd_check_connreply(conn,
2975 (void *)KIBLND_CONN_PARAM(event),
2976 KIBLND_CONN_PARAM_LEN(event));
2979 /* net keeps its ref on conn! */
2982 case RDMA_CM_EVENT_TIMEWAIT_EXIT:
2983 CDEBUG(D_NET, "Ignore TIMEWAIT_EXIT event\n");
2986 case RDMA_CM_EVENT_DISCONNECTED:
2987 conn = (kib_conn_t *)cmid->context;
2988 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2989 CERROR("%s DISCONNECTED\n",
2990 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2991 kiblnd_connreq_done(conn, -ECONNRESET);
2993 kiblnd_close_conn(conn, 0);
2995 kiblnd_conn_decref(conn);
2996 cmid->context = NULL;
2999 case RDMA_CM_EVENT_DEVICE_REMOVAL:
3000 LCONSOLE_ERROR_MSG(0x131,
3001 "Received notification of device removal\n"
3002 "Please shutdown LNET to allow this to proceed\n");
3003 /* Can't remove network from underneath LNET for now, so I have
3007 case RDMA_CM_EVENT_ADDR_CHANGE:
3008 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
3014 kiblnd_check_txs_locked(kib_conn_t *conn, struct list_head *txs)
3017 struct list_head *ttmp;
3019 list_for_each(ttmp, txs) {
3020 tx = list_entry(ttmp, kib_tx_t, tx_list);
3022 if (txs != &conn->ibc_active_txs) {
3023 LASSERT(tx->tx_queued);
3025 LASSERT(!tx->tx_queued);
3026 LASSERT(tx->tx_waiting || tx->tx_sending != 0);
3029 if (cfs_time_aftereq(jiffies, tx->tx_deadline)) {
3030 CERROR("Timed out tx: %s, %lu seconds\n",
3031 kiblnd_queue2str(conn, txs),
3032 cfs_duration_sec(jiffies - tx->tx_deadline));
3041 kiblnd_conn_timed_out_locked(kib_conn_t *conn)
3043 return kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue) ||
3044 kiblnd_check_txs_locked(conn, &conn->ibc_tx_noops) ||
3045 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_rsrvd) ||
3046 kiblnd_check_txs_locked(conn, &conn->ibc_tx_queue_nocred) ||
3047 kiblnd_check_txs_locked(conn, &conn->ibc_active_txs);
3051 kiblnd_check_conns (int idx)
3053 struct list_head closes = LIST_HEAD_INIT(closes);
3054 struct list_head checksends = LIST_HEAD_INIT(checksends);
3055 struct list_head *peers = &kiblnd_data.kib_peers[idx];
3056 struct list_head *ptmp;
3059 struct list_head *ctmp;
3060 unsigned long flags;
3062 /* NB. We expect to have a look at all the peers and not find any
3063 * RDMAs to time out, so we just use a shared lock while we
3065 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
3067 list_for_each(ptmp, peers) {
3068 peer = list_entry(ptmp, kib_peer_t, ibp_list);
3070 list_for_each(ctmp, &peer->ibp_conns) {
3074 conn = list_entry(ctmp, kib_conn_t, ibc_list);
3076 LASSERT(conn->ibc_state == IBLND_CONN_ESTABLISHED);
3078 spin_lock(&conn->ibc_lock);
3080 sendnoop = kiblnd_need_noop(conn);
3081 timedout = kiblnd_conn_timed_out_locked(conn);
3082 if (!sendnoop && !timedout) {
3083 spin_unlock(&conn->ibc_lock);
3088 CERROR("Timed out RDMA with %s (%lu): "
3089 "c: %u, oc: %u, rc: %u\n",
3090 libcfs_nid2str(peer->ibp_nid),
3091 cfs_duration_sec(cfs_time_current() -
3092 peer->ibp_last_alive),
3094 conn->ibc_outstanding_credits,
3095 conn->ibc_reserved_credits);
3096 list_add(&conn->ibc_connd_list, &closes);
3098 list_add(&conn->ibc_connd_list, &checksends);
3100 /* +ref for 'closes' or 'checksends' */
3101 kiblnd_conn_addref(conn);
3103 spin_unlock(&conn->ibc_lock);
3107 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
3109 /* Handle timeout by closing the whole
3110 * connection. We can only be sure RDMA activity
3111 * has ceased once the QP has been modified. */
3112 while (!list_empty(&closes)) {
3113 conn = list_entry(closes.next,
3114 kib_conn_t, ibc_connd_list);
3115 list_del(&conn->ibc_connd_list);
3116 kiblnd_close_conn(conn, -ETIMEDOUT);
3117 kiblnd_conn_decref(conn);
3120 /* In case we have enough credits to return via a
3121 * NOOP, but there were no non-blocking tx descs
3122 * free to do it last time... */
3123 while (!list_empty(&checksends)) {
3124 conn = list_entry(checksends.next,
3125 kib_conn_t, ibc_connd_list);
3126 list_del(&conn->ibc_connd_list);
3127 kiblnd_check_sends(conn);
3128 kiblnd_conn_decref(conn);
3133 kiblnd_disconnect_conn (kib_conn_t *conn)
3135 LASSERT (!in_interrupt());
3136 LASSERT (current == kiblnd_data.kib_connd);
3137 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
3139 rdma_disconnect(conn->ibc_cmid);
3140 kiblnd_finalise_conn(conn);
3142 kiblnd_peer_notify(conn->ibc_peer);
3146 kiblnd_connd (void *arg)
3149 unsigned long flags;
3155 unsigned long deadline = jiffies;
3157 cfs_block_allsigs();
3159 init_waitqueue_entry(&wait, current);
3160 kiblnd_data.kib_connd = current;
3162 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3164 while (!kiblnd_data.kib_shutdown) {
3168 if (!list_empty(&kiblnd_data.kib_connd_zombies)) {
3169 conn = list_entry(kiblnd_data. \
3170 kib_connd_zombies.next,
3171 kib_conn_t, ibc_list);
3172 list_del(&conn->ibc_list);
3174 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3178 kiblnd_destroy_conn(conn);
3180 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3183 if (!list_empty(&kiblnd_data.kib_connd_conns)) {
3184 conn = list_entry(kiblnd_data.kib_connd_conns.next,
3185 kib_conn_t, ibc_list);
3186 list_del(&conn->ibc_list);
3188 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock,
3192 kiblnd_disconnect_conn(conn);
3193 kiblnd_conn_decref(conn);
3195 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3198 /* careful with the jiffy wrap... */
3199 timeout = (int)(deadline - jiffies);
3203 int chunk = kiblnd_data.kib_peer_hash_size;
3205 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3208 /* Time to check for RDMA timeouts on a few more
3209 * peers: I do checks every 'p' seconds on a
3210 * proportion of the peer table and I need to check
3211 * every connection 'n' times within a timeout
3212 * interval, to ensure I detect a timeout on any
3213 * connection within (n+1)/n times the timeout
3216 if (*kiblnd_tunables.kib_timeout > n * p)
3217 chunk = (chunk * n * p) /
3218 *kiblnd_tunables.kib_timeout;
3222 for (i = 0; i < chunk; i++) {
3223 kiblnd_check_conns(peer_index);
3224 peer_index = (peer_index + 1) %
3225 kiblnd_data.kib_peer_hash_size;
3228 deadline += msecs_to_jiffies(p * MSEC_PER_SEC);
3229 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3235 /* Nothing to do for 'timeout' */
3236 set_current_state(TASK_INTERRUPTIBLE);
3237 add_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3238 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3240 schedule_timeout(timeout);
3242 set_current_state(TASK_RUNNING);
3243 remove_wait_queue(&kiblnd_data.kib_connd_waitq, &wait);
3244 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
3247 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
3249 kiblnd_thread_fini();
3254 kiblnd_qp_event(struct ib_event *event, void *arg)
3256 kib_conn_t *conn = arg;
3258 switch (event->event) {
3259 case IB_EVENT_COMM_EST:
3260 CDEBUG(D_NET, "%s established\n",
3261 libcfs_nid2str(conn->ibc_peer->ibp_nid));
3265 CERROR("%s: Async QP event type %d\n",
3266 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3272 kiblnd_complete (struct ib_wc *wc)
3274 switch (kiblnd_wreqid2type(wc->wr_id)) {
3278 case IBLND_WID_RDMA:
3279 /* We only get RDMA completion notification if it fails. All
3280 * subsequent work items, including the final SEND will fail
3281 * too. However we can't print out any more info about the
3282 * failing RDMA because 'tx' might be back on the idle list or
3283 * even reused already if we didn't manage to post all our work
3285 CNETERR("RDMA (tx: %p) failed: %d\n",
3286 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3290 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3294 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3301 kiblnd_cq_completion(struct ib_cq *cq, void *arg)
3303 /* NB I'm not allowed to schedule this conn once its refcount has
3304 * reached 0. Since fundamentally I'm racing with scheduler threads
3305 * consuming my CQ I could be called after all completions have
3306 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3307 * and this CQ is about to be destroyed so I NOOP. */
3308 kib_conn_t *conn = (kib_conn_t *)arg;
3309 struct kib_sched_info *sched = conn->ibc_sched;
3310 unsigned long flags;
3312 LASSERT(cq == conn->ibc_cq);
3314 spin_lock_irqsave(&sched->ibs_lock, flags);
3316 conn->ibc_ready = 1;
3318 if (!conn->ibc_scheduled &&
3319 (conn->ibc_nrx > 0 ||
3320 conn->ibc_nsends_posted > 0)) {
3321 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3322 conn->ibc_scheduled = 1;
3323 list_add_tail(&conn->ibc_sched_list, &sched->ibs_conns);
3325 if (waitqueue_active(&sched->ibs_waitq))
3326 wake_up(&sched->ibs_waitq);
3329 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3333 kiblnd_cq_event(struct ib_event *event, void *arg)
3335 kib_conn_t *conn = arg;
3337 CERROR("%s: async CQ event type %d\n",
3338 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3342 kiblnd_scheduler(void *arg)
3344 long id = (long)arg;
3345 struct kib_sched_info *sched;
3348 unsigned long flags;
3354 cfs_block_allsigs();
3356 init_waitqueue_entry(&wait, current);
3358 sched = kiblnd_data.kib_scheds[KIB_THREAD_CPT(id)];
3360 rc = cfs_cpt_bind(lnet_cpt_table(), sched->ibs_cpt);
3362 CWARN("Failed to bind on CPT %d, please verify whether "
3363 "all CPUs are healthy and reload modules if necessary, "
3364 "otherwise your system might under risk of low "
3365 "performance\n", sched->ibs_cpt);
3368 spin_lock_irqsave(&sched->ibs_lock, flags);
3370 while (!kiblnd_data.kib_shutdown) {
3371 if (busy_loops++ >= IBLND_RESCHED) {
3372 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3377 spin_lock_irqsave(&sched->ibs_lock, flags);
3382 if (!list_empty(&sched->ibs_conns)) {
3383 conn = list_entry(sched->ibs_conns.next,
3384 kib_conn_t, ibc_sched_list);
3385 /* take over kib_sched_conns' ref on conn... */
3386 LASSERT(conn->ibc_scheduled);
3387 list_del(&conn->ibc_sched_list);
3388 conn->ibc_ready = 0;
3390 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3392 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3394 rc = ib_req_notify_cq(conn->ibc_cq,
3397 CWARN("%s: ib_req_notify_cq failed: %d, "
3398 "closing connection\n",
3399 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
3400 kiblnd_close_conn(conn, -EIO);
3401 kiblnd_conn_decref(conn);
3402 spin_lock_irqsave(&sched->ibs_lock,
3407 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3411 CWARN("%s: ib_poll_cq failed: %d, "
3412 "closing connection\n",
3413 libcfs_nid2str(conn->ibc_peer->ibp_nid),
3415 kiblnd_close_conn(conn, -EIO);
3416 kiblnd_conn_decref(conn);
3417 spin_lock_irqsave(&sched->ibs_lock, flags);
3421 spin_lock_irqsave(&sched->ibs_lock, flags);
3423 if (rc != 0 || conn->ibc_ready) {
3424 /* There may be another completion waiting; get
3425 * another scheduler to check while I handle
3427 /* +1 ref for sched_conns */
3428 kiblnd_conn_addref(conn);
3429 list_add_tail(&conn->ibc_sched_list,
3431 if (waitqueue_active(&sched->ibs_waitq))
3432 wake_up(&sched->ibs_waitq);
3434 conn->ibc_scheduled = 0;
3438 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3439 kiblnd_complete(&wc);
3441 spin_lock_irqsave(&sched->ibs_lock, flags);
3444 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3451 set_current_state(TASK_INTERRUPTIBLE);
3452 add_wait_queue_exclusive(&sched->ibs_waitq, &wait);
3453 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3458 remove_wait_queue(&sched->ibs_waitq, &wait);
3459 set_current_state(TASK_RUNNING);
3460 spin_lock_irqsave(&sched->ibs_lock, flags);
3463 spin_unlock_irqrestore(&sched->ibs_lock, flags);
3465 kiblnd_thread_fini();
3470 kiblnd_failover_thread(void *arg)
3472 rwlock_t *glock = &kiblnd_data.kib_global_lock;
3475 unsigned long flags;
3478 LASSERT(*kiblnd_tunables.kib_dev_failover != 0);
3480 cfs_block_allsigs();
3482 init_waitqueue_entry(&wait, current);
3483 write_lock_irqsave(glock, flags);
3485 while (!kiblnd_data.kib_shutdown) {
3486 int do_failover = 0;
3489 list_for_each_entry(dev, &kiblnd_data.kib_failed_devs,
3491 if (cfs_time_before(cfs_time_current(),
3492 dev->ibd_next_failover))
3499 list_del_init(&dev->ibd_fail_list);
3500 dev->ibd_failover = 1;
3501 write_unlock_irqrestore(glock, flags);
3503 rc = kiblnd_dev_failover(dev);
3505 write_lock_irqsave(glock, flags);
3507 LASSERT (dev->ibd_failover);
3508 dev->ibd_failover = 0;
3509 if (rc >= 0) { /* Device is OK or failover succeed */
3510 dev->ibd_next_failover = cfs_time_shift(3);
3514 /* failed to failover, retry later */
3515 dev->ibd_next_failover =
3516 cfs_time_shift(min(dev->ibd_failed_failover, 10));
3517 if (kiblnd_dev_can_failover(dev)) {
3518 list_add_tail(&dev->ibd_fail_list,
3519 &kiblnd_data.kib_failed_devs);
3525 /* long sleep if no more pending failover */
3526 long_sleep = list_empty(&kiblnd_data.kib_failed_devs);
3528 set_current_state(TASK_INTERRUPTIBLE);
3529 add_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3530 write_unlock_irqrestore(glock, flags);
3532 rc = schedule_timeout(long_sleep ? cfs_time_seconds(10) :
3533 cfs_time_seconds(1));
3534 set_current_state(TASK_RUNNING);
3535 remove_wait_queue(&kiblnd_data.kib_failover_waitq, &wait);
3536 write_lock_irqsave(glock, flags);
3538 if (!long_sleep || rc != 0)
3541 /* have a long sleep, routine check all active devices,
3542 * we need checking like this because if there is not active
3543 * connection on the dev and no SEND from local, we may listen
3544 * on wrong HCA for ever while there is a bonding failover */
3545 list_for_each_entry(dev, &kiblnd_data.kib_devs, ibd_list) {
3546 if (kiblnd_dev_can_failover(dev)) {
3547 list_add_tail(&dev->ibd_fail_list,
3548 &kiblnd_data.kib_failed_devs);
3553 write_unlock_irqrestore(glock, flags);
3555 kiblnd_thread_fini();