1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Author: Eric Barton <eric@bartonsoftware.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 kiblnd_msgtype2str(int type)
30 case IBLND_MSG_CONNREQ:
33 case IBLND_MSG_CONNACK:
39 case IBLND_MSG_IMMEDIATE:
42 case IBLND_MSG_PUT_REQ:
45 case IBLND_MSG_PUT_NAK:
48 case IBLND_MSG_PUT_ACK:
51 case IBLND_MSG_PUT_DONE:
54 case IBLND_MSG_GET_REQ:
57 case IBLND_MSG_GET_DONE:
66 kiblnd_tx_done (lnet_ni_t *ni, kib_tx_t *tx)
68 lnet_msg_t *lntmsg[2];
69 kib_net_t *net = ni->ni_data;
73 LASSERT (net != NULL);
74 LASSERT (!in_interrupt());
75 LASSERT (!tx->tx_queued); /* mustn't be queued for sending */
76 LASSERT (tx->tx_sending == 0); /* mustn't be awaiting sent callback */
77 LASSERT (!tx->tx_waiting); /* mustn't be awaiting peer response */
79 #if IBLND_MAP_ON_DEMAND
80 if (tx->tx_fmr != NULL) {
81 rc = ib_fmr_pool_unmap(tx->tx_fmr);
84 if (tx->tx_status != 0) {
85 rc = ib_flush_fmr_pool(net->ibn_fmrpool);
92 if (tx->tx_nfrags != 0) {
93 kiblnd_dma_unmap_sg(net->ibn_dev->ibd_cmid->device,
94 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
98 /* tx may have up to 2 lnet msgs to finalise */
99 lntmsg[0] = tx->tx_lntmsg[0]; tx->tx_lntmsg[0] = NULL;
100 lntmsg[1] = tx->tx_lntmsg[1]; tx->tx_lntmsg[1] = NULL;
103 if (tx->tx_conn != NULL) {
104 LASSERT (ni == tx->tx_conn->ibc_peer->ibp_ni);
106 kiblnd_conn_decref(tx->tx_conn);
113 spin_lock(&net->ibn_tx_lock);
115 list_add(&tx->tx_list, &net->ibn_idle_txs);
117 spin_unlock(&net->ibn_tx_lock);
119 /* delay finalize until my descs have been freed */
120 for (i = 0; i < 2; i++) {
121 if (lntmsg[i] == NULL)
124 lnet_finalize(ni, lntmsg[i], rc);
129 kiblnd_txlist_done (lnet_ni_t *ni, struct list_head *txlist, int status)
133 while (!list_empty (txlist)) {
134 tx = list_entry (txlist->next, kib_tx_t, tx_list);
136 list_del (&tx->tx_list);
139 tx->tx_status = status;
140 kiblnd_tx_done(ni, tx);
145 kiblnd_get_idle_tx (lnet_ni_t *ni)
147 kib_net_t *net = ni->ni_data;
150 LASSERT (net != NULL);
152 spin_lock(&net->ibn_tx_lock);
154 if (list_empty(&net->ibn_idle_txs)) {
155 spin_unlock(&net->ibn_tx_lock);
159 tx = list_entry(net->ibn_idle_txs.next, kib_tx_t, tx_list);
160 list_del(&tx->tx_list);
162 /* Allocate a new completion cookie. It might not be needed,
163 * but we've got a lock right now and we're unlikely to
165 tx->tx_cookie = kiblnd_data.kib_next_tx_cookie++;
167 spin_unlock(&net->ibn_tx_lock);
169 LASSERT (tx->tx_nwrq == 0);
170 LASSERT (!tx->tx_queued);
171 LASSERT (tx->tx_sending == 0);
172 LASSERT (!tx->tx_waiting);
173 LASSERT (tx->tx_status == 0);
174 LASSERT (tx->tx_conn == NULL);
175 LASSERT (tx->tx_lntmsg[0] == NULL);
176 LASSERT (tx->tx_lntmsg[1] == NULL);
177 #if IBLND_MAP_ON_DEMAND
178 LASSERT (tx->tx_fmr == NULL);
180 LASSERT (tx->tx_nfrags == 0);
187 kiblnd_drop_rx (kib_rx_t *rx)
189 kib_conn_t *conn = rx->rx_conn;
192 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
193 LASSERT (conn->ibc_nrx > 0);
195 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
197 kiblnd_conn_decref(conn);
201 kiblnd_post_rx (kib_rx_t *rx, int credit)
203 kib_conn_t *conn = rx->rx_conn;
204 kib_net_t *net = conn->ibc_peer->ibp_ni->ni_data;
205 struct ib_recv_wr *bad_wrq;
208 LASSERT (net != NULL);
209 LASSERT (!in_interrupt());
210 LASSERT (credit == IBLND_POSTRX_NO_CREDIT ||
211 credit == IBLND_POSTRX_PEER_CREDIT ||
212 credit == IBLND_POSTRX_RSRVD_CREDIT);
214 rx->rx_sge.length = IBLND_MSG_SIZE;
215 rx->rx_sge.lkey = net->ibn_dev->ibd_mr->lkey;
216 rx->rx_sge.addr = rx->rx_msgaddr;
218 rx->rx_wrq.next = NULL;
219 rx->rx_wrq.sg_list = &rx->rx_sge;
220 rx->rx_wrq.num_sge = 1;
221 rx->rx_wrq.wr_id = kiblnd_ptr2wreqid(rx, IBLND_WID_RX);
223 LASSERT (conn->ibc_state >= IBLND_CONN_INIT);
224 LASSERT (rx->rx_nob >= 0); /* not posted */
226 if (conn->ibc_state > IBLND_CONN_ESTABLISHED) {
227 kiblnd_drop_rx(rx); /* No more posts for this rx */
231 rx->rx_nob = -1; /* flag posted */
233 rc = ib_post_recv(conn->ibc_cmid->qp, &rx->rx_wrq, &bad_wrq);
235 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) /* Initial post */
239 CERROR("Can't post rx for %s: %d\n",
240 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc);
241 kiblnd_close_conn(conn, rc);
242 kiblnd_drop_rx(rx); /* No more posts for this rx */
246 if (credit == IBLND_POSTRX_NO_CREDIT)
249 spin_lock(&conn->ibc_lock);
250 if (credit == IBLND_POSTRX_PEER_CREDIT)
251 conn->ibc_outstanding_credits++;
253 conn->ibc_reserved_credits++;
254 spin_unlock(&conn->ibc_lock);
256 kiblnd_check_sends(conn);
261 kiblnd_find_waiting_tx_locked(kib_conn_t *conn, int txtype, __u64 cookie)
263 struct list_head *tmp;
265 list_for_each(tmp, &conn->ibc_active_txs) {
266 kib_tx_t *tx = list_entry(tmp, kib_tx_t, tx_list);
268 LASSERT (!tx->tx_queued);
269 LASSERT (tx->tx_sending != 0 || tx->tx_waiting);
271 if (tx->tx_cookie != cookie)
274 if (tx->tx_waiting &&
275 tx->tx_msg->ibm_type == txtype)
278 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
279 tx->tx_waiting ? "" : "NOT ",
280 tx->tx_msg->ibm_type, txtype);
286 kiblnd_handle_completion(kib_conn_t *conn, int txtype, int status, __u64 cookie)
289 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
292 spin_lock(&conn->ibc_lock);
294 tx = kiblnd_find_waiting_tx_locked(conn, txtype, cookie);
296 spin_unlock(&conn->ibc_lock);
298 CWARN("Unmatched completion type %x cookie "LPX64" from %s\n",
299 txtype, cookie, libcfs_nid2str(conn->ibc_peer->ibp_nid));
300 kiblnd_close_conn(conn, -EPROTO);
304 if (tx->tx_status == 0) { /* success so far */
305 if (status < 0) { /* failed? */
306 tx->tx_status = status;
307 } else if (txtype == IBLND_MSG_GET_REQ) {
308 lnet_set_reply_msg_len(ni, tx->tx_lntmsg[1], status);
314 idle = !tx->tx_queued && (tx->tx_sending == 0);
316 list_del(&tx->tx_list);
318 spin_unlock(&conn->ibc_lock);
321 kiblnd_tx_done(ni, tx);
325 kiblnd_send_completion (kib_conn_t *conn, int type, int status, __u64 cookie)
327 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
328 kib_tx_t *tx = kiblnd_get_idle_tx(ni);
331 CERROR("Can't get tx for completion %x for %s\n",
332 type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
336 tx->tx_msg->ibm_u.completion.ibcm_status = status;
337 tx->tx_msg->ibm_u.completion.ibcm_cookie = cookie;
338 kiblnd_init_tx_msg(ni, tx, type, sizeof(kib_completion_msg_t));
340 kiblnd_queue_tx(tx, conn);
344 kiblnd_handle_rx (kib_rx_t *rx)
346 kib_msg_t *msg = rx->rx_msg;
347 kib_conn_t *conn = rx->rx_conn;
348 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
349 int credits = msg->ibm_credits;
355 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
357 CDEBUG (D_NET, "Received %x[%d] from %s\n",
358 msg->ibm_type, credits, libcfs_nid2str(conn->ibc_peer->ibp_nid));
361 /* Have I received credits that will let me send? */
362 spin_lock(&conn->ibc_lock);
364 if (conn->ibc_credits + credits > IBLND_MSG_QUEUE_SIZE) {
365 rc2 = conn->ibc_credits;
366 spin_unlock(&conn->ibc_lock);
368 CERROR("Bad credits from %s: %d + %d > %d\n",
369 libcfs_nid2str(conn->ibc_peer->ibp_nid),
370 rc2, credits, IBLND_MSG_QUEUE_SIZE);
372 kiblnd_close_conn(conn, -EPROTO);
373 kiblnd_post_rx(rx, IBLND_POSTRX_NO_CREDIT);
377 conn->ibc_credits += credits;
379 spin_unlock(&conn->ibc_lock);
380 kiblnd_check_sends(conn);
383 switch (msg->ibm_type) {
385 CERROR("Bad IBLND message type %x from %s\n",
386 msg->ibm_type, libcfs_nid2str(conn->ibc_peer->ibp_nid));
387 post_credit = IBLND_POSTRX_NO_CREDIT;
392 post_credit = IBLND_POSTRX_PEER_CREDIT;
395 case IBLND_MSG_IMMEDIATE:
396 post_credit = IBLND_POSTRX_DONT_POST;
397 rc = lnet_parse(ni, &msg->ibm_u.immediate.ibim_hdr,
398 msg->ibm_srcnid, rx, 0);
399 if (rc < 0) /* repost on error */
400 post_credit = IBLND_POSTRX_PEER_CREDIT;
403 case IBLND_MSG_PUT_REQ:
404 post_credit = IBLND_POSTRX_DONT_POST;
405 rc = lnet_parse(ni, &msg->ibm_u.putreq.ibprm_hdr,
406 msg->ibm_srcnid, rx, 1);
407 if (rc < 0) /* repost on error */
408 post_credit = IBLND_POSTRX_PEER_CREDIT;
411 case IBLND_MSG_PUT_NAK:
412 CWARN ("PUT_NACK from %s\n", libcfs_nid2str(conn->ibc_peer->ibp_nid));
413 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
414 kiblnd_handle_completion(conn, IBLND_MSG_PUT_REQ,
415 msg->ibm_u.completion.ibcm_status,
416 msg->ibm_u.completion.ibcm_cookie);
419 case IBLND_MSG_PUT_ACK:
420 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
422 spin_lock(&conn->ibc_lock);
423 tx = kiblnd_find_waiting_tx_locked(conn, IBLND_MSG_PUT_REQ,
424 msg->ibm_u.putack.ibpam_src_cookie);
426 list_del(&tx->tx_list);
427 spin_unlock(&conn->ibc_lock);
430 CERROR("Unmatched PUT_ACK from %s\n",
431 libcfs_nid2str(conn->ibc_peer->ibp_nid));
436 LASSERT (tx->tx_waiting);
437 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
438 * (a) I can overwrite tx_msg since my peer has received it!
439 * (b) tx_waiting set tells tx_complete() it's not done. */
441 tx->tx_nwrq = 0; /* overwrite PUT_REQ */
443 rc2 = kiblnd_init_rdma(ni, tx, IBLND_MSG_PUT_DONE,
444 kiblnd_rd_size(&msg->ibm_u.putack.ibpam_rd),
445 &msg->ibm_u.putack.ibpam_rd,
446 msg->ibm_u.putack.ibpam_dst_cookie);
448 CERROR("Can't setup rdma for PUT to %s: %d\n",
449 libcfs_nid2str(conn->ibc_peer->ibp_nid), rc2);
451 spin_lock(&conn->ibc_lock);
452 tx->tx_waiting = 0; /* clear waiting and queue atomically */
453 kiblnd_queue_tx_locked(tx, conn);
454 spin_unlock(&conn->ibc_lock);
457 case IBLND_MSG_PUT_DONE:
458 post_credit = IBLND_POSTRX_PEER_CREDIT;
459 kiblnd_handle_completion(conn, IBLND_MSG_PUT_ACK,
460 msg->ibm_u.completion.ibcm_status,
461 msg->ibm_u.completion.ibcm_cookie);
464 case IBLND_MSG_GET_REQ:
465 post_credit = IBLND_POSTRX_DONT_POST;
466 rc = lnet_parse(ni, &msg->ibm_u.get.ibgm_hdr,
467 msg->ibm_srcnid, rx, 1);
468 if (rc < 0) /* repost on error */
469 post_credit = IBLND_POSTRX_PEER_CREDIT;
472 case IBLND_MSG_GET_DONE:
473 post_credit = IBLND_POSTRX_RSRVD_CREDIT;
474 kiblnd_handle_completion(conn, IBLND_MSG_GET_REQ,
475 msg->ibm_u.completion.ibcm_status,
476 msg->ibm_u.completion.ibcm_cookie);
480 if (rc < 0) /* protocol error */
481 kiblnd_close_conn(conn, rc);
483 if (post_credit != IBLND_POSTRX_DONT_POST)
484 kiblnd_post_rx(rx, post_credit);
488 kiblnd_rx_complete (kib_rx_t *rx, int status, int nob)
490 kib_msg_t *msg = rx->rx_msg;
491 kib_conn_t *conn = rx->rx_conn;
492 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
493 kib_net_t *net = ni->ni_data;
498 LASSERT (net != NULL);
499 LASSERT (rx->rx_nob < 0); /* was posted */
500 rx->rx_nob = 0; /* isn't now */
502 if (conn->ibc_state > IBLND_CONN_ESTABLISHED)
505 if (status != IB_WC_SUCCESS) {
506 CDEBUG(D_NETERROR, "Rx from %s failed: %d\n",
507 libcfs_nid2str(conn->ibc_peer->ibp_nid), status);
514 rc = kiblnd_unpack_msg(msg, rx->rx_nob);
516 CERROR ("Error %d unpacking rx from %s\n",
517 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
521 if (msg->ibm_srcnid != conn->ibc_peer->ibp_nid ||
522 msg->ibm_dstnid != ni->ni_nid ||
523 msg->ibm_srcstamp != conn->ibc_incarnation ||
524 msg->ibm_dststamp != net->ibn_incarnation) {
525 CERROR ("Stale rx from %s\n",
526 libcfs_nid2str(conn->ibc_peer->ibp_nid));
531 /* set time last known alive */
532 kiblnd_peer_alive(conn->ibc_peer);
534 /* racing with connection establishment/teardown! */
536 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
537 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
538 /* must check holding global lock to eliminate race */
539 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
540 list_add_tail(&rx->rx_list, &conn->ibc_early_rxs);
541 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
545 write_unlock_irqrestore(&kiblnd_data.kib_global_lock,
548 kiblnd_handle_rx(rx);
552 CDEBUG(D_NET, "rx %p conn %p\n", rx, conn);
553 kiblnd_close_conn(conn, err);
555 kiblnd_drop_rx(rx); /* Don't re-post rx. */
559 kiblnd_kvaddr_to_page (unsigned long vaddr)
563 if (vaddr >= VMALLOC_START &&
564 vaddr < VMALLOC_END) {
565 page = vmalloc_to_page ((void *)vaddr);
566 LASSERT (page != NULL);
569 #ifdef CONFIG_HIGHMEM
570 if (vaddr >= PKMAP_BASE &&
571 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE)) {
572 /* No highmem pages only used for bulk (kiov) I/O */
573 CERROR("find page for address in highmem\n");
577 page = virt_to_page (vaddr);
578 LASSERT (page != NULL);
582 #if !IBLND_MAP_ON_DEMAND
584 kiblnd_setup_rd_iov(lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
585 unsigned int niov, struct iovec *iov, int offset, int nob)
588 struct scatterlist *sg;
594 kib_net_t *net = ni->ni_data;
598 LASSERT (net != NULL);
600 while (offset >= iov->iov_len) {
601 offset -= iov->iov_len;
611 vaddr = ((unsigned long)iov->iov_base) + offset;
612 page_offset = vaddr & (PAGE_SIZE - 1);
613 page = kiblnd_kvaddr_to_page(vaddr);
615 CERROR ("Can't find page\n");
619 fragnob = min((int)(iov->iov_len - offset), nob);
620 fragnob = min(fragnob, (int)PAGE_SIZE - page_offset);
623 sg->offset = page_offset;
624 sg->length = fragnob;
627 if (offset + fragnob < iov->iov_len) {
637 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
639 tx->tx_nfrags = sg - tx->tx_frags;
640 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
642 rd->rd_nfrags = kiblnd_dma_map_sg(net->ibn_dev->ibd_cmid->device,
643 tx->tx_frags, tx->tx_nfrags,
645 rd->rd_key = (rd != tx->tx_rd) ?
646 net->ibn_dev->ibd_mr->rkey : net->ibn_dev->ibd_mr->lkey;
648 for (i = 0; i < rd->rd_nfrags; i++) {
649 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
650 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
651 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
652 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
659 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
660 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
662 struct scatterlist *sg;
665 kib_net_t *net = ni->ni_data;
667 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
671 LASSERT (net != NULL);
673 while (offset >= kiov->kiov_len) {
674 offset -= kiov->kiov_len;
684 fragnob = min((int)(kiov->kiov_len - offset), nob);
686 memset(sg, 0, sizeof(*sg));
687 sg->page = kiov->kiov_page;
688 sg->offset = kiov->kiov_offset + offset;
689 sg->length = fragnob;
698 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
700 tx->tx_nfrags = sg - tx->tx_frags;
701 tx->tx_dmadir = (rd != tx->tx_rd) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
703 rd->rd_nfrags = kiblnd_dma_map_sg(net->ibn_dev->ibd_cmid->device,
704 tx->tx_frags, tx->tx_nfrags, tx->tx_dmadir);
705 rd->rd_key = (rd != tx->tx_rd) ?
706 net->ibn_dev->ibd_mr->rkey : net->ibn_dev->ibd_mr->lkey;
708 for (i = 0; i < tx->tx_nfrags; i++) {
709 rd->rd_frags[i].rf_nob = kiblnd_sg_dma_len(
710 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
711 rd->rd_frags[i].rf_addr = kiblnd_sg_dma_address(
712 net->ibn_dev->ibd_cmid->device, &tx->tx_frags[i]);
714 CDEBUG(D_WARNING,"frag[%d]: "LPX64" for %d\n",
715 i, rd->rd_frags[i].rf_addr, rd->rd_frags[i].rf_nob);
723 kiblnd_map_tx (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
724 int npages, unsigned long page_offset, int nob)
726 struct ib_pool_fmr *fmr;
727 kib_net_t *net = ni->ni_data;
729 LASSERT (net != NULL);
730 LASSERT (tx->tx_fmr == NULL);
731 LASSERT (page_offset < PAGE_SIZE);
732 LASSERT (npages >= (1 + ((page_offset + nob - 1)>>PAGE_SHIFT)));
733 LASSERT (npages <= LNET_MAX_IOV);
737 fmr = ib_fmr_pool_map_phys(net->ibn_fmrpool, tx->tx_pages,
738 npages, rd->rd_addr);
740 CERROR ("Can't map %d pages: %ld\n", npages, PTR_ERR(fmr));
744 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
747 rd->rd_key = (rd != tx->tx_rd) ? fmr->fmr->rkey : fmr->fmr->lkey;
755 kiblnd_setup_rd_iov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
756 unsigned int niov, struct iovec *iov, int offset, int nob)
763 unsigned long page_offset;
769 while (offset >= iov->iov_len) {
770 offset -= iov->iov_len;
776 if (nob > iov->iov_len - offset) {
777 CERROR ("Can't map multiple vaddr fragments\n");
781 vaddr = ((unsigned long)iov->iov_base) + offset;
783 page_offset = vaddr & (PAGE_SIZE - 1);
788 LASSERT (npages < LNET_MAX_IOV);
790 page = kiblnd_kvaddr_to_page(vaddr);
792 CERROR("Can't find page for %lu\n", vaddr);
796 tx->tx_pages[npages++] = lnet_page2phys(page);
798 fragnob = PAGE_SIZE - (vaddr & (PAGE_SIZE - 1));
804 return kiblnd_map_tx(ni, tx, rd, npages, page_offset, nob);
808 kiblnd_setup_rd_kiov (lnet_ni_t *ni, kib_tx_t *tx, kib_rdma_desc_t *rd,
809 int nkiov, lnet_kiov_t *kiov, int offset, int nob)
813 unsigned long page_offset;
815 CDEBUG(D_NET, "niov %d offset %d nob %d\n", nkiov, offset, nob);
819 LASSERT (nkiov <= LNET_MAX_IOV);
821 while (offset >= kiov->kiov_len) {
822 offset -= kiov->kiov_len;
828 page_offset = kiov->kiov_offset + offset;
830 resid = offset + nob;
834 LASSERT (npages < LNET_MAX_IOV);
837 if ((npages > 0 && kiov->kiov_offset != 0) ||
838 (resid > kiov->kiov_len &&
839 (kiov->kiov_offset + kiov->kiov_len) != PAGE_SIZE)) {
840 /* Can't have gaps */
841 CERROR ("Can't make payload contiguous in I/O VM:"
842 "page %d, offset %d, len %d \n",
843 npages, kiov->kiov_offset, kiov->kiov_len);
848 tx->tx_pages[npages++] = lnet_page2phys(kiov->kiov_page);
849 resid -= kiov->kiov_len;
854 return kiblnd_map_tx(ni, tx, rd, npages, page_offset, nob);
859 kiblnd_check_sends (kib_conn_t *conn)
862 lnet_ni_t *ni = conn->ibc_peer->ibp_ni;
864 int consume_cred = 0;
865 struct ib_send_wr *bad_wrq;
868 /* Don't send anything until after the connection is established */
869 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
870 CDEBUG(D_NET, "%s too soon\n",
871 libcfs_nid2str(conn->ibc_peer->ibp_nid));
875 spin_lock(&conn->ibc_lock);
877 LASSERT (conn->ibc_nsends_posted <=
878 *kiblnd_tunables.kib_concurrent_sends);
879 LASSERT (conn->ibc_reserved_credits >= 0);
881 while (conn->ibc_reserved_credits > 0 &&
882 !list_empty(&conn->ibc_tx_queue_rsrvd)) {
883 tx = list_entry(conn->ibc_tx_queue_rsrvd.next,
885 list_del(&tx->tx_list);
886 list_add_tail(&tx->tx_list, &conn->ibc_tx_queue);
887 conn->ibc_reserved_credits--;
890 if (list_empty(&conn->ibc_tx_queue) &&
891 list_empty(&conn->ibc_tx_queue_nocred) &&
892 (conn->ibc_outstanding_credits >= IBLND_CREDIT_HIGHWATER ||
893 kiblnd_send_keepalive(conn))) {
894 spin_unlock(&conn->ibc_lock);
896 tx = kiblnd_get_idle_tx(ni);
898 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_NOOP, 0);
900 spin_lock(&conn->ibc_lock);
903 kiblnd_queue_tx_locked(tx, conn);
907 if (!list_empty (&conn->ibc_tx_queue_nocred)) {
908 tx = list_entry (conn->ibc_tx_queue_nocred.next,
911 } else if (!list_empty (&conn->ibc_tx_queue)) {
912 tx = list_entry (conn->ibc_tx_queue.next,
916 /* nothing to send right now */
920 LASSERT (tx->tx_queued);
921 /* We rely on this for QP sizing */
922 LASSERT (tx->tx_nwrq > 0 &&
923 tx->tx_nwrq <= 1 + IBLND_MAX_RDMA_FRAGS);
925 LASSERT (conn->ibc_outstanding_credits >= 0);
926 LASSERT (conn->ibc_outstanding_credits <= IBLND_MSG_QUEUE_SIZE);
927 LASSERT (conn->ibc_credits >= 0);
928 LASSERT (conn->ibc_credits <= IBLND_MSG_QUEUE_SIZE);
930 if (conn->ibc_nsends_posted ==
931 *kiblnd_tunables.kib_concurrent_sends) {
932 /* tx completions outstanding... */
933 CDEBUG(D_NET, "%s: posted enough\n",
934 libcfs_nid2str(conn->ibc_peer->ibp_nid));
939 if (conn->ibc_credits == 0) { /* no credits */
940 CDEBUG(D_NET, "%s: no credits\n",
941 libcfs_nid2str(conn->ibc_peer->ibp_nid));
945 if (conn->ibc_credits == 1 && /* last credit reserved for */
946 conn->ibc_outstanding_credits == 0) { /* giving back credits */
947 CDEBUG(D_NET, "%s: not using last credit\n",
948 libcfs_nid2str(conn->ibc_peer->ibp_nid));
953 list_del (&tx->tx_list);
956 /* NB don't drop ibc_lock before bumping tx_sending */
958 if (tx->tx_msg->ibm_type == IBLND_MSG_NOOP &&
959 (!list_empty(&conn->ibc_tx_queue) ||
960 !list_empty(&conn->ibc_tx_queue_nocred) ||
961 (conn->ibc_outstanding_credits < IBLND_CREDIT_HIGHWATER &&
962 !kiblnd_send_keepalive(conn)))) {
964 spin_unlock(&conn->ibc_lock);
965 kiblnd_tx_done(ni, tx);
966 spin_lock(&conn->ibc_lock);
967 CDEBUG(D_NET, "%s: redundant noop\n",
968 libcfs_nid2str(conn->ibc_peer->ibp_nid));
972 kiblnd_pack_msg(ni, tx->tx_msg, conn->ibc_outstanding_credits,
973 conn->ibc_peer->ibp_nid, conn->ibc_incarnation);
975 conn->ibc_outstanding_credits = 0;
976 conn->ibc_nsends_posted++;
980 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
981 * PUT. If so, it was first queued here as a PUT_REQ, sent and
982 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
983 * and then re-queued here. It's (just) possible that
984 * tx_sending is non-zero if we've not done the tx_complete() from
985 * the first send; hence the ++ rather than = below. */
988 list_add (&tx->tx_list, &conn->ibc_active_txs);
993 for (i = 0; i < tx->tx_nwrq - 1; i++) {
994 LASSERT (tx->tx_wrq[i].opcode == IB_WR_RDMA_WRITE);
995 LASSERT (tx->tx_wrq[i].next == &tx->tx_wrq[i+1]);
996 LASSERT (tx->tx_wrq[i].sg_list == &tx->tx_sge[i]);
998 CDEBUG(D_WARNING, "WORK[%d]: RDMA "LPX64
999 " for %d k %x -> "LPX64" k %x\n", i,
1000 tx->tx_wrq[i].sg_list->addr,
1001 tx->tx_wrq[i].sg_list->length,
1002 tx->tx_wrq[i].sg_list->lkey,
1003 tx->tx_wrq[i].wr.rdma.remote_addr,
1004 tx->tx_wrq[i].wr.rdma.rkey);
1007 LASSERT (tx->tx_wrq[i].opcode == IB_WR_SEND);
1008 LASSERT (tx->tx_wrq[i].next == NULL);
1009 LASSERT (tx->tx_wrq[i].sg_list == &tx->tx_sge[i]);
1011 CDEBUG(D_WARNING, "WORK[%d]: SEND "LPX64" for %d k %x\n", i,
1012 tx->tx_wrq[i].sg_list->addr,
1013 tx->tx_wrq[i].sg_list->length,
1014 tx->tx_wrq[i].sg_list->lkey);
1017 /* I'm still holding ibc_lock! */
1018 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1021 rc = ib_post_send(conn->ibc_cmid->qp, tx->tx_wrq, &bad_wrq);
1023 conn->ibc_last_send = jiffies;
1026 /* NB credits are transferred in the actual
1027 * message, which can only be the last work item */
1028 conn->ibc_outstanding_credits += tx->tx_msg->ibm_credits;
1030 conn->ibc_credits++;
1031 conn->ibc_nsends_posted--;
1037 done = (tx->tx_sending == 0);
1039 list_del (&tx->tx_list);
1041 spin_unlock(&conn->ibc_lock);
1043 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1044 CERROR("Error %d posting transmit to %s\n",
1045 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
1047 CDEBUG(D_NET, "Error %d posting transmit to %s\n",
1048 rc, libcfs_nid2str(conn->ibc_peer->ibp_nid));
1050 kiblnd_close_conn(conn, rc);
1053 kiblnd_tx_done(ni, tx);
1058 spin_unlock(&conn->ibc_lock);
1062 kiblnd_tx_complete (kib_tx_t *tx, int status)
1064 int failed = (status != IB_WC_SUCCESS);
1065 kib_conn_t *conn = tx->tx_conn;
1068 LASSERT (tx->tx_sending > 0);
1071 if (conn->ibc_state == IBLND_CONN_ESTABLISHED)
1072 CDEBUG(D_NETERROR, "Tx -> %s cookie "LPX64
1073 "sending %d waiting %d: failed %d\n",
1074 libcfs_nid2str(conn->ibc_peer->ibp_nid),
1075 tx->tx_cookie, tx->tx_sending, tx->tx_waiting,
1078 kiblnd_close_conn(conn, -EIO);
1080 kiblnd_peer_alive(conn->ibc_peer);
1083 spin_lock(&conn->ibc_lock);
1085 /* I could be racing with rdma completion. Whoever makes 'tx' idle
1086 * gets to free it, which also drops its ref on 'conn'. */
1089 conn->ibc_nsends_posted--;
1092 tx->tx_waiting = 0; /* don't wait for peer */
1093 tx->tx_status = -EIO;
1096 idle = (tx->tx_sending == 0) && /* This is the final callback */
1097 !tx->tx_waiting && /* Not waiting for peer */
1098 !tx->tx_queued; /* Not re-queued (PUT_DONE) */
1100 list_del(&tx->tx_list);
1102 kiblnd_conn_addref(conn); /* 1 ref for me.... */
1104 spin_unlock(&conn->ibc_lock);
1107 kiblnd_tx_done(conn->ibc_peer->ibp_ni, tx);
1109 kiblnd_check_sends(conn);
1111 kiblnd_conn_decref(conn); /* ...until here */
1115 kiblnd_init_tx_msg (lnet_ni_t *ni, kib_tx_t *tx, int type, int body_nob)
1117 kib_net_t *net = ni->ni_data;
1118 struct ib_sge *sge = &tx->tx_sge[tx->tx_nwrq];
1119 struct ib_send_wr *wrq = &tx->tx_wrq[tx->tx_nwrq];
1120 int nob = offsetof (kib_msg_t, ibm_u) + body_nob;
1122 LASSERT (net != NULL);
1123 LASSERT (tx->tx_nwrq >= 0);
1124 LASSERT (tx->tx_nwrq < IBLND_MAX_RDMA_FRAGS + 1);
1125 LASSERT (nob <= IBLND_MSG_SIZE);
1127 kiblnd_init_msg(tx->tx_msg, type, body_nob);
1129 sge->addr = tx->tx_msgaddr;
1130 sge->lkey = net->ibn_dev->ibd_mr->lkey;
1133 memset(wrq, 0, sizeof(*wrq));
1136 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_TX);
1139 wrq->opcode = IB_WR_SEND;
1140 wrq->send_flags = IB_SEND_SIGNALED;
1146 kiblnd_init_rdma (lnet_ni_t *ni, kib_tx_t *tx, int type,
1147 int nob, kib_rdma_desc_t *dstrd, __u64 dstcookie)
1149 kib_msg_t *ibmsg = tx->tx_msg;
1150 kib_rdma_desc_t *srcrd = tx->tx_rd;
1151 struct ib_sge *sge = &tx->tx_sge[0];
1152 struct ib_send_wr *wrq = &tx->tx_wrq[0];
1155 #if IBLND_MAP_ON_DEMAND
1156 LASSERT (!in_interrupt());
1157 LASSERT (tx->tx_nwrq == 0);
1158 LASSERT (type == IBLND_MSG_GET_DONE ||
1159 type == IBLND_MSG_PUT_DONE);
1161 sge->addr = srcrd->rd_addr;
1162 sge->lkey = srcrd->rd_key;
1165 wrq = &tx->tx_wrq[0];
1167 wrq->next = &tx->tx_wrq[1];
1168 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1171 wrq->opcode = IB_WR_RDMA_WRITE;
1172 wrq->send_flags = 0;
1174 wrq->wr.rdma.remote_addr = dstrd->rd_addr;
1175 wrq->wr.rdma.rkey = dstrd->rd_key;
1179 /* CAVEAT EMPTOR: this 'consumes' the frags in 'dstrd' */
1181 kib_rdma_frag_t *srcfrag;
1183 kib_rdma_frag_t *dstfrag;
1187 LASSERT (!in_interrupt());
1188 LASSERT (tx->tx_nwrq == 0);
1189 LASSERT (type == IBLND_MSG_GET_DONE ||
1190 type == IBLND_MSG_PUT_DONE);
1192 srcidx = dstidx = 0;
1193 srcfrag = &srcrd->rd_frags[0];
1194 dstfrag = &dstrd->rd_frags[0];
1197 if (srcidx >= srcrd->rd_nfrags) {
1198 CERROR("Src buffer exhausted: %d frags\n", srcidx);
1203 if (dstidx == dstrd->rd_nfrags) {
1204 CERROR("Dst buffer exhausted: %d frags\n", dstidx);
1209 if (tx->tx_nwrq == IBLND_MAX_RDMA_FRAGS) {
1210 CERROR("RDMA too fragmented: %d/%d src %d/%d dst frags\n",
1211 srcidx, srcrd->rd_nfrags,
1212 dstidx, dstrd->rd_nfrags);
1217 wrknob = MIN(MIN(srcfrag->rf_nob, dstfrag->rf_nob), resid);
1219 sge = &tx->tx_sge[tx->tx_nwrq];
1220 sge->addr = srcfrag->rf_addr;
1221 sge->length = wrknob;
1222 sge->lkey = srcrd->rd_key;
1224 wrq = &tx->tx_wrq[tx->tx_nwrq];
1226 wrq->next = wrq + 1;
1227 wrq->wr_id = kiblnd_ptr2wreqid(tx, IBLND_WID_RDMA);
1230 wrq->opcode = IB_WR_RDMA_WRITE;
1231 wrq->send_flags = 0;
1233 wrq->wr.rdma.remote_addr = dstfrag->rf_addr;
1234 wrq->wr.rdma.rkey = dstrd->rd_key;
1240 if (wrknob < srcfrag->rf_nob) {
1241 srcfrag->rf_nob -= wrknob;
1242 srcfrag->rf_addr += wrknob;
1248 if (wrknob < dstfrag->rf_nob) {
1249 dstfrag->rf_nob -= wrknob;
1250 dstfrag->rf_addr += wrknob;
1259 if (rc < 0) /* no RDMA if completing with failure */
1262 ibmsg->ibm_u.completion.ibcm_status = rc;
1263 ibmsg->ibm_u.completion.ibcm_cookie = dstcookie;
1264 kiblnd_init_tx_msg(ni, tx, type, sizeof (kib_completion_msg_t));
1270 kiblnd_queue_tx_locked (kib_tx_t *tx, kib_conn_t *conn)
1272 struct list_head *q;
1274 LASSERT (tx->tx_nwrq > 0); /* work items set up */
1275 LASSERT (!tx->tx_queued); /* not queued for sending already */
1278 tx->tx_deadline = jiffies + (*kiblnd_tunables.kib_timeout * HZ);
1280 if (tx->tx_conn == NULL) {
1281 kiblnd_conn_addref(conn);
1283 LASSERT (tx->tx_msg->ibm_type != IBLND_MSG_PUT_DONE);
1285 /* PUT_DONE first attached to conn as a PUT_REQ */
1286 LASSERT (tx->tx_conn == conn);
1287 LASSERT (tx->tx_msg->ibm_type == IBLND_MSG_PUT_DONE);
1290 switch (tx->tx_msg->ibm_type) {
1294 case IBLND_MSG_PUT_REQ:
1295 case IBLND_MSG_GET_REQ:
1296 q = &conn->ibc_tx_queue_rsrvd;
1299 case IBLND_MSG_PUT_NAK:
1300 case IBLND_MSG_PUT_ACK:
1301 case IBLND_MSG_PUT_DONE:
1302 case IBLND_MSG_GET_DONE:
1303 q = &conn->ibc_tx_queue_nocred;
1306 case IBLND_MSG_NOOP:
1307 case IBLND_MSG_IMMEDIATE:
1308 q = &conn->ibc_tx_queue;
1312 list_add_tail(&tx->tx_list, q);
1316 kiblnd_queue_tx (kib_tx_t *tx, kib_conn_t *conn)
1318 spin_lock(&conn->ibc_lock);
1319 kiblnd_queue_tx_locked(tx, conn);
1320 spin_unlock(&conn->ibc_lock);
1322 kiblnd_check_sends(conn);
1326 kiblnd_connect_peer (kib_peer_t *peer)
1328 struct rdma_cm_id *cmid;
1329 struct sockaddr_in sockaddr;
1332 LASSERT (peer->ibp_connecting > 0);
1334 cmid = rdma_create_id(kiblnd_cm_callback, peer, RDMA_PS_TCP);
1336 CERROR("Can't create CMID for %s: %ld\n",
1337 libcfs_nid2str(peer->ibp_nid), PTR_ERR(cmid));
1342 memset(&sockaddr, 0, sizeof(sockaddr));
1343 sockaddr.sin_family = AF_INET;
1344 sockaddr.sin_port = htons(*kiblnd_tunables.kib_service);
1345 sockaddr.sin_addr.s_addr = htonl(LNET_NIDADDR(peer->ibp_nid));
1347 kiblnd_peer_addref(peer); /* cmid's ref */
1349 rc = rdma_resolve_addr(cmid, NULL, (struct sockaddr *)&sockaddr,
1350 *kiblnd_tunables.kib_timeout * 1000);
1354 /* Can't initiate address resolution: */
1355 CERROR("Can't resolve addr for %s: %d\n",
1356 libcfs_nid2str(peer->ibp_nid), rc);
1358 kiblnd_peer_decref(peer); /* cmid's ref */
1359 rdma_destroy_id(cmid);
1361 kiblnd_peer_connect_failed(peer, 1, rc);
1365 kiblnd_launch_tx (lnet_ni_t *ni, kib_tx_t *tx, lnet_nid_t nid)
1370 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
1371 unsigned long flags;
1374 /* If I get here, I've committed to send, so I complete the tx with
1375 * failure on any problems */
1377 LASSERT (tx->tx_conn == NULL); /* only set when assigned a conn */
1378 LASSERT (tx->tx_nwrq > 0); /* work items have been set up */
1380 /* First time, just use a read lock since I expect to find my peer
1382 read_lock_irqsave(g_lock, flags);
1384 peer = kiblnd_find_peer_locked(nid);
1385 if (peer != NULL && !list_empty(&peer->ibp_conns)) {
1386 /* Found a peer with an established connection */
1387 conn = kiblnd_get_conn_locked(peer);
1388 kiblnd_conn_addref(conn); /* 1 ref for me... */
1390 read_unlock_irqrestore(g_lock, flags);
1392 kiblnd_queue_tx(tx, conn);
1393 kiblnd_conn_decref(conn); /* ...to here */
1397 read_unlock(g_lock);
1398 /* Re-try with a write lock */
1401 peer = kiblnd_find_peer_locked(nid);
1403 if (list_empty(&peer->ibp_conns)) {
1404 /* found a peer, but it's still connecting... */
1405 LASSERT (peer->ibp_connecting != 0 ||
1406 peer->ibp_accepting != 0);
1407 list_add_tail (&tx->tx_list, &peer->ibp_tx_queue);
1408 write_unlock_irqrestore(g_lock, flags);
1410 conn = kiblnd_get_conn_locked(peer);
1411 kiblnd_conn_addref(conn); /* 1 ref for me... */
1413 write_unlock_irqrestore(g_lock, flags);
1415 kiblnd_queue_tx(tx, conn);
1416 kiblnd_conn_decref(conn); /* ...to here */
1421 write_unlock_irqrestore(g_lock, flags);
1423 /* Allocate a peer ready to add to the peer table and retry */
1424 rc = kiblnd_create_peer(ni, &peer, nid);
1426 CERROR("Can't create peer %s\n", libcfs_nid2str(nid));
1427 tx->tx_status = -EHOSTUNREACH;
1429 kiblnd_tx_done(ni, tx);
1433 write_lock_irqsave(g_lock, flags);
1435 peer2 = kiblnd_find_peer_locked(nid);
1436 if (peer2 != NULL) {
1437 if (list_empty(&peer2->ibp_conns)) {
1438 /* found a peer, but it's still connecting... */
1439 LASSERT (peer2->ibp_connecting != 0 ||
1440 peer2->ibp_accepting != 0);
1441 list_add_tail (&tx->tx_list, &peer2->ibp_tx_queue);
1442 write_unlock_irqrestore(g_lock, flags);
1444 conn = kiblnd_get_conn_locked(peer2);
1445 kiblnd_conn_addref(conn); /* 1 ref for me... */
1447 write_unlock_irqrestore(g_lock, flags);
1449 kiblnd_queue_tx(tx, conn);
1450 kiblnd_conn_decref(conn); /* ...to here */
1453 kiblnd_peer_decref(peer);
1457 /* Brand new peer */
1458 LASSERT (peer->ibp_connecting == 0);
1459 peer->ibp_connecting = 1;
1461 list_add_tail(&tx->tx_list, &peer->ibp_tx_queue);
1463 kiblnd_peer_addref(peer);
1464 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
1466 write_unlock_irqrestore(g_lock, flags);
1468 kiblnd_connect_peer(peer);
1469 kiblnd_peer_decref(peer);
1473 kiblnd_send (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1475 lnet_hdr_t *hdr = &lntmsg->msg_hdr;
1476 int type = lntmsg->msg_type;
1477 lnet_process_id_t target = lntmsg->msg_target;
1478 int target_is_router = lntmsg->msg_target_is_router;
1479 int routing = lntmsg->msg_routing;
1480 unsigned int payload_niov = lntmsg->msg_niov;
1481 struct iovec *payload_iov = lntmsg->msg_iov;
1482 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1483 unsigned int payload_offset = lntmsg->msg_offset;
1484 unsigned int payload_nob = lntmsg->msg_len;
1490 /* NB 'private' is different depending on what we're sending.... */
1492 CDEBUG(D_NET, "sending %d bytes in %d frags to %s\n",
1493 payload_nob, payload_niov, libcfs_id2str(target));
1495 LASSERT (payload_nob == 0 || payload_niov > 0);
1496 LASSERT (payload_niov <= LNET_MAX_IOV);
1498 /* Thread context */
1499 LASSERT (!in_interrupt());
1500 /* payload is either all vaddrs or all pages */
1501 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1509 LASSERT (payload_nob == 0);
1513 if (routing || target_is_router)
1514 break; /* send IMMEDIATE */
1516 /* is the REPLY message too small for RDMA? */
1517 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[lntmsg->msg_md->md_length]);
1518 if (nob <= IBLND_MSG_SIZE)
1519 break; /* send IMMEDIATE */
1521 tx = kiblnd_get_idle_tx(ni);
1523 CERROR("Can allocate txd for GET to %s: \n",
1524 libcfs_nid2str(target.nid));
1529 ibmsg->ibm_u.get.ibgm_hdr = *hdr;
1530 ibmsg->ibm_u.get.ibgm_cookie = tx->tx_cookie;
1532 if ((lntmsg->msg_md->md_options & LNET_MD_KIOV) == 0)
1533 rc = kiblnd_setup_rd_iov(ni, tx,
1534 &ibmsg->ibm_u.get.ibgm_rd,
1535 lntmsg->msg_md->md_niov,
1536 lntmsg->msg_md->md_iov.iov,
1537 0, lntmsg->msg_md->md_length);
1539 rc = kiblnd_setup_rd_kiov(ni, tx,
1540 &ibmsg->ibm_u.get.ibgm_rd,
1541 lntmsg->msg_md->md_niov,
1542 lntmsg->msg_md->md_iov.kiov,
1543 0, lntmsg->msg_md->md_length);
1545 CERROR("Can't setup GET sink for %s: %d\n",
1546 libcfs_nid2str(target.nid), rc);
1547 kiblnd_tx_done(ni, tx);
1550 #if IBLND_MAP_ON_DEMAND
1551 nob = sizeof(kib_get_msg_t);
1553 nob = offsetof(kib_get_msg_t, ibgm_rd.rd_frags[tx->tx_nfrags]);
1555 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_GET_REQ, nob);
1557 tx->tx_lntmsg[1] = lnet_create_reply_msg(ni, lntmsg);
1558 if (tx->tx_lntmsg[1] == NULL) {
1559 CERROR("Can't create reply for GET -> %s\n",
1560 libcfs_nid2str(target.nid));
1561 kiblnd_tx_done(ni, tx);
1565 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg[0,1] on completion */
1566 tx->tx_waiting = 1; /* waiting for GET_DONE */
1567 kiblnd_launch_tx(ni, tx, target.nid);
1570 case LNET_MSG_REPLY:
1572 /* Is the payload small enough not to need RDMA? */
1573 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob]);
1574 if (nob <= IBLND_MSG_SIZE)
1575 break; /* send IMMEDIATE */
1577 tx = kiblnd_get_idle_tx(ni);
1579 CERROR("Can't allocate %s txd for %s\n",
1580 type == LNET_MSG_PUT ? "PUT" : "REPLY",
1581 libcfs_nid2str(target.nid));
1585 if (payload_kiov == NULL)
1586 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1587 payload_niov, payload_iov,
1588 payload_offset, payload_nob);
1590 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1591 payload_niov, payload_kiov,
1592 payload_offset, payload_nob);
1594 CERROR("Can't setup PUT src for %s: %d\n",
1595 libcfs_nid2str(target.nid), rc);
1596 kiblnd_tx_done(ni, tx);
1601 ibmsg->ibm_u.putreq.ibprm_hdr = *hdr;
1602 ibmsg->ibm_u.putreq.ibprm_cookie = tx->tx_cookie;
1603 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_REQ, sizeof(kib_putreq_msg_t));
1605 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1606 tx->tx_waiting = 1; /* waiting for PUT_{ACK,NAK} */
1607 kiblnd_launch_tx(ni, tx, target.nid);
1611 /* send IMMEDIATE */
1613 LASSERT (offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[payload_nob])
1616 tx = kiblnd_get_idle_tx(ni);
1618 CERROR ("Can't send %d to %s: tx descs exhausted\n",
1619 type, libcfs_nid2str(target.nid));
1624 ibmsg->ibm_u.immediate.ibim_hdr = *hdr;
1626 if (payload_kiov != NULL)
1627 lnet_copy_kiov2flat(IBLND_MSG_SIZE, ibmsg,
1628 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1629 payload_niov, payload_kiov,
1630 payload_offset, payload_nob);
1632 lnet_copy_iov2flat(IBLND_MSG_SIZE, ibmsg,
1633 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1634 payload_niov, payload_iov,
1635 payload_offset, payload_nob);
1637 nob = offsetof(kib_immediate_msg_t, ibim_payload[payload_nob]);
1638 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_IMMEDIATE, nob);
1640 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1641 kiblnd_launch_tx(ni, tx, target.nid);
1646 kiblnd_reply (lnet_ni_t *ni, kib_rx_t *rx, lnet_msg_t *lntmsg)
1648 lnet_process_id_t target = lntmsg->msg_target;
1649 unsigned int niov = lntmsg->msg_niov;
1650 struct iovec *iov = lntmsg->msg_iov;
1651 lnet_kiov_t *kiov = lntmsg->msg_kiov;
1652 unsigned int offset = lntmsg->msg_offset;
1653 unsigned int nob = lntmsg->msg_len;
1657 tx = kiblnd_get_idle_tx(ni);
1659 CERROR("Can't get tx for REPLY to %s\n",
1660 libcfs_nid2str(target.nid));
1666 else if (kiov == NULL)
1667 rc = kiblnd_setup_rd_iov(ni, tx, tx->tx_rd,
1668 niov, iov, offset, nob);
1670 rc = kiblnd_setup_rd_kiov(ni, tx, tx->tx_rd,
1671 niov, kiov, offset, nob);
1674 CERROR("Can't setup GET src for %s: %d\n",
1675 libcfs_nid2str(target.nid), rc);
1679 rc = kiblnd_init_rdma(ni, tx, IBLND_MSG_GET_DONE, nob,
1680 &rx->rx_msg->ibm_u.get.ibgm_rd,
1681 rx->rx_msg->ibm_u.get.ibgm_cookie);
1683 CERROR("Can't setup rdma for GET from %s: %d\n",
1684 libcfs_nid2str(target.nid), rc);
1689 /* No RDMA: local completion may happen now! */
1690 lnet_finalize(ni, lntmsg, 0);
1692 /* RDMA: lnet_finalize(lntmsg) when it
1694 tx->tx_lntmsg[0] = lntmsg;
1697 kiblnd_queue_tx(tx, rx->rx_conn);
1701 kiblnd_tx_done(ni, tx);
1703 lnet_finalize(ni, lntmsg, -EIO);
1707 kiblnd_recv (lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg, int delayed,
1708 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1709 unsigned int offset, unsigned int mlen, unsigned int rlen)
1711 kib_rx_t *rx = private;
1712 kib_msg_t *rxmsg = rx->rx_msg;
1713 kib_conn_t *conn = rx->rx_conn;
1717 int post_credit = IBLND_POSTRX_PEER_CREDIT;
1720 LASSERT (mlen <= rlen);
1721 LASSERT (!in_interrupt());
1722 /* Either all pages or all vaddrs */
1723 LASSERT (!(kiov != NULL && iov != NULL));
1725 switch (rxmsg->ibm_type) {
1729 case IBLND_MSG_IMMEDIATE:
1730 nob = offsetof(kib_msg_t, ibm_u.immediate.ibim_payload[rlen]);
1731 if (nob > rx->rx_nob) {
1732 CERROR ("Immediate message from %s too big: %d(%d)\n",
1733 libcfs_nid2str(rxmsg->ibm_u.immediate.ibim_hdr.src_nid),
1740 lnet_copy_flat2kiov(niov, kiov, offset,
1741 IBLND_MSG_SIZE, rxmsg,
1742 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1745 lnet_copy_flat2iov(niov, iov, offset,
1746 IBLND_MSG_SIZE, rxmsg,
1747 offsetof(kib_msg_t, ibm_u.immediate.ibim_payload),
1749 lnet_finalize (ni, lntmsg, 0);
1752 case IBLND_MSG_PUT_REQ:
1754 lnet_finalize(ni, lntmsg, 0);
1755 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_PUT_NAK, 0,
1756 rxmsg->ibm_u.putreq.ibprm_cookie);
1760 tx = kiblnd_get_idle_tx(ni);
1762 CERROR("Can't allocate tx for %s\n",
1763 libcfs_nid2str(conn->ibc_peer->ibp_nid));
1764 /* Not replying will break the connection */
1771 rc = kiblnd_setup_rd_iov(ni, tx,
1772 &txmsg->ibm_u.putack.ibpam_rd,
1773 niov, iov, offset, mlen);
1775 rc = kiblnd_setup_rd_kiov(ni, tx,
1776 &txmsg->ibm_u.putack.ibpam_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 txmsg->ibm_u.putack.ibpam_src_cookie = rxmsg->ibm_u.putreq.ibprm_cookie;
1789 txmsg->ibm_u.putack.ibpam_dst_cookie = tx->tx_cookie;
1790 #if IBLND_MAP_ON_DEMAND
1791 nob = sizeof(kib_putack_msg_t);
1793 nob = offsetof(kib_putack_msg_t, ibpam_rd.rd_frags[tx->tx_nfrags]);
1795 kiblnd_init_tx_msg(ni, tx, IBLND_MSG_PUT_ACK, nob);
1797 tx->tx_lntmsg[0] = lntmsg; /* finalise lntmsg on completion */
1798 tx->tx_waiting = 1; /* waiting for PUT_DONE */
1799 kiblnd_queue_tx(tx, conn);
1801 /* reposted buffer reserved for PUT_DONE */
1802 post_credit = IBLND_POSTRX_NO_CREDIT;
1805 case IBLND_MSG_GET_REQ:
1806 if (lntmsg != NULL) {
1807 /* Optimized GET; RDMA lntmsg's payload */
1808 kiblnd_reply(ni, rx, lntmsg);
1810 /* GET didn't match anything */
1811 kiblnd_send_completion(rx->rx_conn, IBLND_MSG_GET_DONE,
1813 rxmsg->ibm_u.get.ibgm_cookie);
1818 kiblnd_post_rx(rx, post_credit);
1823 kiblnd_thread_start (int (*fn)(void *arg), void *arg)
1825 long pid = kernel_thread (fn, arg, 0);
1830 atomic_inc (&kiblnd_data.kib_nthreads);
1835 kiblnd_thread_fini (void)
1837 atomic_dec (&kiblnd_data.kib_nthreads);
1841 kiblnd_peer_alive (kib_peer_t *peer)
1843 /* This is racy, but everyone's only writing cfs_time_current() */
1844 peer->ibp_last_alive = cfs_time_current();
1849 kiblnd_peer_notify (kib_peer_t *peer)
1851 time_t last_alive = 0;
1853 unsigned long flags;
1855 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1857 if (list_empty(&peer->ibp_conns) &&
1858 peer->ibp_accepting == 0 &&
1859 peer->ibp_connecting == 0 &&
1860 peer->ibp_error != 0) {
1861 error = peer->ibp_error;
1862 peer->ibp_error = 0;
1864 last_alive = cfs_time_current_sec() -
1865 cfs_duration_sec(cfs_time_current() -
1866 peer->ibp_last_alive);
1869 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1872 lnet_notify(peer->ibp_ni,
1873 peer->ibp_nid, 0, last_alive);
1877 kiblnd_close_conn_locked (kib_conn_t *conn, int error)
1879 /* This just does the immediate housekeeping. 'error' is zero for a
1880 * normal shutdown which can happen only after the connection has been
1881 * established. If the connection is established, schedule the
1882 * connection to be finished off by the connd. Otherwise the connd is
1883 * already dealing with it (either to set it up or tear it down).
1884 * Caller holds kib_global_lock exclusively in irq context */
1885 unsigned long flags;
1886 kib_peer_t *peer = conn->ibc_peer;
1888 LASSERT (error != 0 || conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1890 if (error != 0 && conn->ibc_comms_error == 0)
1891 conn->ibc_comms_error = error;
1893 if (conn->ibc_state != IBLND_CONN_ESTABLISHED)
1894 return; /* already being handled */
1897 list_empty(&conn->ibc_tx_queue) &&
1898 list_empty(&conn->ibc_tx_queue_rsrvd) &&
1899 list_empty(&conn->ibc_tx_queue_nocred) &&
1900 list_empty(&conn->ibc_active_txs)) {
1901 CDEBUG(D_NET, "closing conn to %s\n",
1902 libcfs_nid2str(peer->ibp_nid));
1904 CDEBUG(D_NETERROR, "Closing conn to %s: error %d%s%s%s%s\n",
1905 libcfs_nid2str(peer->ibp_nid), error,
1906 list_empty(&conn->ibc_tx_queue) ? "" : "(sending)",
1907 list_empty(&conn->ibc_tx_queue_rsrvd) ? "" : "(sending_rsrvd)",
1908 list_empty(&conn->ibc_tx_queue_nocred) ? "" : "(sending_nocred)",
1909 list_empty(&conn->ibc_active_txs) ? "" : "(waiting)");
1912 list_del (&conn->ibc_list);
1913 /* connd (see below) takes over ibc_list's ref */
1915 if (list_empty (&peer->ibp_conns) && /* no more conns */
1916 kiblnd_peer_active(peer)) { /* still in peer table */
1917 kiblnd_unlink_peer_locked(peer);
1919 /* set/clear error on last conn */
1920 peer->ibp_error = conn->ibc_comms_error;
1923 kiblnd_set_conn_state(conn, IBLND_CONN_CLOSING);
1925 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
1927 list_add_tail (&conn->ibc_list, &kiblnd_data.kib_connd_conns);
1928 wake_up (&kiblnd_data.kib_connd_waitq);
1930 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
1934 kiblnd_close_conn (kib_conn_t *conn, int error)
1936 unsigned long flags;
1938 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1940 kiblnd_close_conn_locked(conn, error);
1942 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1946 kiblnd_handle_early_rxs(kib_conn_t *conn)
1948 unsigned long flags;
1951 LASSERT (!in_interrupt());
1952 LASSERT (conn->ibc_state >= IBLND_CONN_ESTABLISHED);
1954 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1955 while (!list_empty(&conn->ibc_early_rxs)) {
1956 rx = list_entry(conn->ibc_early_rxs.next,
1958 list_del(&rx->rx_list);
1959 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1961 kiblnd_handle_rx(rx);
1963 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
1965 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
1969 kiblnd_abort_txs(kib_conn_t *conn, struct list_head *txs)
1971 LIST_HEAD (zombies);
1972 struct list_head *tmp;
1973 struct list_head *nxt;
1976 spin_lock(&conn->ibc_lock);
1978 list_for_each_safe (tmp, nxt, txs) {
1979 tx = list_entry (tmp, kib_tx_t, tx_list);
1981 if (txs == &conn->ibc_active_txs) {
1982 LASSERT (!tx->tx_queued);
1983 LASSERT (tx->tx_waiting ||
1984 tx->tx_sending != 0);
1986 LASSERT (tx->tx_queued);
1989 tx->tx_status = -ECONNABORTED;
1993 if (tx->tx_sending == 0) {
1994 list_del (&tx->tx_list);
1995 list_add (&tx->tx_list, &zombies);
1999 spin_unlock(&conn->ibc_lock);
2001 kiblnd_txlist_done(conn->ibc_peer->ibp_ni,
2002 &zombies, -ECONNABORTED);
2006 kiblnd_finalise_conn (kib_conn_t *conn)
2008 LASSERT (!in_interrupt());
2009 LASSERT (conn->ibc_state > IBLND_CONN_INIT);
2011 kiblnd_set_conn_state(conn, IBLND_CONN_DISCONNECTED);
2013 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
2014 * for connections that didn't get as far as being connected, because
2015 * rdma_disconnect() does this for free. */
2016 kiblnd_abort_receives(conn);
2018 /* Complete all tx descs not waiting for sends to complete.
2019 * NB we should be safe from RDMA now that the QP has changed state */
2021 kiblnd_abort_txs(conn, &conn->ibc_tx_queue);
2022 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_rsrvd);
2023 kiblnd_abort_txs(conn, &conn->ibc_tx_queue_nocred);
2024 kiblnd_abort_txs(conn, &conn->ibc_active_txs);
2026 kiblnd_handle_early_rxs(conn);
2030 kiblnd_peer_connect_failed (kib_peer_t *peer, int active, int error)
2032 LIST_HEAD (zombies);
2033 unsigned long flags;
2035 LASSERT (error != 0);
2036 LASSERT (!in_interrupt());
2038 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2041 LASSERT (peer->ibp_connecting > 0);
2042 peer->ibp_connecting--;
2044 LASSERT (peer->ibp_accepting > 0);
2045 peer->ibp_accepting--;
2048 if (peer->ibp_connecting != 0 ||
2049 peer->ibp_accepting != 0) {
2050 /* another connection attempt under way... */
2051 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2055 if (list_empty(&peer->ibp_conns)) {
2056 /* Take peer's blocked transmits to complete with error */
2057 list_add(&zombies, &peer->ibp_tx_queue);
2058 list_del_init(&peer->ibp_tx_queue);
2060 if (kiblnd_peer_active(peer))
2061 kiblnd_unlink_peer_locked(peer);
2063 peer->ibp_error = error;
2065 /* Can't have blocked transmits if there are connections */
2066 LASSERT (list_empty(&peer->ibp_tx_queue));
2069 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2071 kiblnd_peer_notify(peer);
2073 if (list_empty (&zombies))
2076 CDEBUG (D_NETERROR, "Deleting messages for %s: connection failed\n",
2077 libcfs_nid2str(peer->ibp_nid));
2079 kiblnd_txlist_done(peer->ibp_ni, &zombies, -EHOSTUNREACH);
2083 kiblnd_connreq_done(kib_conn_t *conn, int status)
2085 struct list_head txs;
2087 kib_peer_t *peer = conn->ibc_peer;
2089 unsigned long flags;
2092 active = (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2094 CDEBUG(D_NET,"%s: %d, %d\n", libcfs_nid2str(peer->ibp_nid),
2097 LASSERT (!in_interrupt());
2098 LASSERT ((conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT &&
2099 peer->ibp_connecting > 0) ||
2100 (conn->ibc_state == IBLND_CONN_PASSIVE_WAIT &&
2101 peer->ibp_accepting > 0));
2103 LIBCFS_FREE(conn->ibc_connvars, sizeof(*conn->ibc_connvars));
2104 conn->ibc_connvars = NULL;
2107 /* failed to establish connection */
2108 kiblnd_peer_connect_failed(conn->ibc_peer, active, status);
2109 kiblnd_finalise_conn(conn);
2113 /* connection established */
2114 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2116 conn->ibc_last_send = jiffies;
2117 kiblnd_set_conn_state(conn, IBLND_CONN_ESTABLISHED);
2118 kiblnd_peer_alive(peer);
2120 /* Add conn to peer's list and nuke any dangling conns from a different
2121 * peer instance... */
2122 kiblnd_conn_addref(conn); /* +1 ref for ibc_list */
2123 list_add(&conn->ibc_list, &peer->ibp_conns);
2125 peer->ibp_connecting--;
2127 peer->ibp_accepting--;
2129 kiblnd_close_stale_conns_locked(conn->ibc_peer,
2130 conn->ibc_incarnation);
2132 if (!kiblnd_peer_active(peer) || /* peer has been deleted */
2133 conn->ibc_comms_error != 0) { /* error has happened already */
2135 /* start to shut down connection */
2136 kiblnd_close_conn_locked(conn, -ECONNABORTED);
2137 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2141 /* grab pending txs while I have the lock */
2142 list_add(&txs, &peer->ibp_tx_queue);
2143 list_del_init(&peer->ibp_tx_queue);
2145 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2147 /* Schedule blocked txs */
2148 spin_lock (&conn->ibc_lock);
2149 while (!list_empty (&txs)) {
2150 tx = list_entry (txs.next, kib_tx_t, tx_list);
2151 list_del (&tx->tx_list);
2153 kiblnd_queue_tx_locked(tx, conn);
2155 spin_unlock (&conn->ibc_lock);
2157 kiblnd_check_sends(conn);
2159 /* schedule blocked rxs */
2160 kiblnd_handle_early_rxs(conn);
2164 kiblnd_reject(struct rdma_cm_id *cmid, int why)
2167 kib_rej_t rej = {.ibr_magic = IBLND_MSG_MAGIC,
2168 .ibr_version = IBLND_MSG_VERSION,
2171 rc = rdma_reject(cmid, &rej, sizeof(rej));
2174 CWARN("Error %d sending reject\n", rc);
2178 kiblnd_passive_connect (struct rdma_cm_id *cmid, void *priv, int priv_nob)
2181 kib_msg_t *reqmsg = priv;
2182 rwlock_t *g_lock = &kiblnd_data.kib_global_lock;
2183 struct rdma_conn_param cp;
2184 unsigned long flags;
2185 lnet_ni_t *ni = NULL;
2192 int rej = IBLND_REJECT_FATAL;
2194 LASSERT (!in_interrupt());
2196 /* cmid inherits 'context' from the corresponding listener id */
2197 ibdev = (kib_dev_t *)cmid->context;
2198 LASSERT (ibdev != NULL);
2200 if (priv_nob < offsetof(kib_msg_t, ibm_type)) {
2201 CERROR("Short connection request\n");
2205 if (reqmsg->ibm_magic == LNET_PROTO_MAGIC ||
2206 reqmsg->ibm_magic == __swab32(LNET_PROTO_MAGIC) ||
2207 (reqmsg->ibm_magic == IBLND_MSG_MAGIC &&
2208 reqmsg->ibm_version != IBLND_MSG_VERSION) ||
2209 (reqmsg->ibm_magic == __swab32(IBLND_MSG_MAGIC) &&
2210 reqmsg->ibm_version != __swab16(IBLND_MSG_VERSION))) {
2211 /* Future protocol version compatibility support! If the
2212 * o2iblnd-specific protocol changes, or when LNET unifies
2213 * protocols over all LNDs, the initial connection will
2214 * negotiate a protocol version. I trap this here to avoid
2215 * console errors; the reject tells the peer which protocol I
2220 rc = kiblnd_unpack_msg(reqmsg, priv_nob);
2222 CERROR("Can't parse connection request: %d\n", rc);
2226 nid = reqmsg->ibm_srcnid;
2228 if (reqmsg->ibm_type != IBLND_MSG_CONNREQ) {
2229 CERROR("Unexpected connreq msg type: %x from %s\n",
2230 reqmsg->ibm_type, libcfs_nid2str(nid));
2234 if (reqmsg->ibm_u.connparams.ibcp_queue_depth != IBLND_MSG_QUEUE_SIZE) {
2235 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2236 libcfs_nid2str(nid),
2237 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2238 IBLND_MSG_QUEUE_SIZE);
2242 if (reqmsg->ibm_u.connparams.ibcp_max_frags != IBLND_MAX_RDMA_FRAGS) {
2243 CERROR("Can't accept %s: incompatible max_frags %d (%d wanted)\n",
2244 libcfs_nid2str(nid),
2245 reqmsg->ibm_u.connparams.ibcp_queue_depth,
2246 IBLND_MSG_QUEUE_SIZE);
2250 if (reqmsg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2251 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2252 libcfs_nid2str(nid),
2253 reqmsg->ibm_u.connparams.ibcp_max_msg_size,
2258 ni = lnet_net2ni(LNET_NIDNET(reqmsg->ibm_dstnid));
2259 if (ni == NULL || /* no matching net */
2260 ni->ni_nid != reqmsg->ibm_dstnid || /* right NET, wrong NID! */
2261 ((kib_net_t*)ni->ni_data)->ibn_dev != ibdev) { /* wrong device */
2262 CERROR("Can't accept %s: bad dst nid %s\n",
2263 libcfs_nid2str(nid),
2264 libcfs_nid2str(reqmsg->ibm_dstnid));
2269 /* assume 'nid' is a new peer; create */
2270 rc = kiblnd_create_peer(ni, &peer, nid);
2272 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid));
2273 rej = IBLND_REJECT_NO_RESOURCES;
2277 write_lock_irqsave(g_lock, flags);
2279 peer2 = kiblnd_find_peer_locked(nid);
2280 if (peer2 != NULL) {
2281 /* tie-break connection race in favour of the higher NID */
2282 if (peer2->ibp_connecting != 0 &&
2284 write_unlock_irqrestore(g_lock, flags);
2286 CWARN("Conn race %s\n",
2287 libcfs_nid2str(peer2->ibp_nid));
2289 kiblnd_peer_decref(peer);
2290 rej = IBLND_REJECT_CONN_RACE;
2294 peer2->ibp_accepting++;
2295 kiblnd_peer_addref(peer2);
2297 write_unlock_irqrestore(g_lock, flags);
2298 kiblnd_peer_decref(peer);
2301 /* Brand new peer */
2302 LASSERT (peer->ibp_accepting == 0);
2303 peer->ibp_accepting = 1;
2305 kiblnd_peer_addref(peer);
2306 list_add_tail(&peer->ibp_list, kiblnd_nid2peerlist(nid));
2308 write_unlock_irqrestore(g_lock, flags);
2311 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_PASSIVE_WAIT);
2313 kiblnd_peer_connect_failed(peer, 0, -ENOMEM);
2314 kiblnd_peer_decref(peer);
2315 rej = IBLND_REJECT_NO_RESOURCES;
2319 /* conn now "owns" cmid, so I return success from here on to ensure the
2320 * CM callback doesn't destroy cmid. */
2322 conn->ibc_incarnation = reqmsg->ibm_srcstamp;
2323 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE;
2324 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE;
2325 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits
2328 ackmsg = &conn->ibc_connvars->cv_msg;
2329 memset(ackmsg, 0, sizeof(*ackmsg));
2331 kiblnd_init_msg(ackmsg, IBLND_MSG_CONNACK,
2332 sizeof(ackmsg->ibm_u.connparams));
2333 ackmsg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE;
2334 ackmsg->ibm_u.connparams.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2335 ackmsg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2336 kiblnd_pack_msg(ni, ackmsg, 0, nid, reqmsg->ibm_srcstamp);
2338 memset(&cp, 0, sizeof(cp));
2339 cp.private_data = ackmsg;
2340 cp.private_data_len = ackmsg->ibm_nob;
2341 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2342 cp.initiator_depth = 0;
2343 cp.flow_control = 1;
2344 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2345 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2347 CDEBUG(D_NET, "Accept %s\n", libcfs_nid2str(nid));
2349 rc = rdma_accept(cmid, &cp);
2351 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid), rc);
2352 kiblnd_reject(cmid, IBLND_REJECT_FATAL);
2353 kiblnd_connreq_done(conn, rc);
2354 kiblnd_conn_decref(conn);
2364 kiblnd_reject(cmid, rej);
2365 return -ECONNREFUSED;
2369 kiblnd_reconnect (kib_conn_t *conn, char *why)
2371 kib_peer_t *peer = conn->ibc_peer;
2373 unsigned long flags;
2375 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2376 LASSERT (peer->ibp_connecting > 0); /* 'conn' at least */
2378 write_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2380 /* retry connection if it's still needed and no other connection
2381 * attempts (active or passive) are in progress */
2382 if (!list_empty(&peer->ibp_tx_queue) &&
2383 peer->ibp_connecting == 1 &&
2384 peer->ibp_accepting == 0) {
2386 peer->ibp_connecting++;
2389 write_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2392 CDEBUG(D_NETERROR, "%s: retrying (%s)\n",
2393 libcfs_nid2str(peer->ibp_nid), why);
2394 kiblnd_connect_peer(peer);
2399 kiblnd_rejected (kib_conn_t *conn, int reason, void *priv, int priv_nob)
2401 kib_peer_t *peer = conn->ibc_peer;
2403 LASSERT (!in_interrupt());
2404 LASSERT (conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT);
2407 case IB_CM_REJ_STALE_CONN:
2408 kiblnd_reconnect(conn, "stale");
2411 case IB_CM_REJ_CONSUMER_DEFINED:
2412 if (priv_nob >= sizeof(kib_rej_t)) {
2413 kib_rej_t *rej = priv;
2415 if (rej->ibr_magic == __swab32(IBLND_MSG_MAGIC) ||
2416 rej->ibr_magic == __swab32(LNET_PROTO_MAGIC)) {
2417 __swab32s(&rej->ibr_magic);
2418 __swab16s(&rej->ibr_version);
2421 if (rej->ibr_magic != IBLND_MSG_MAGIC &&
2422 rej->ibr_magic != LNET_PROTO_MAGIC) {
2423 CERROR("%s rejected: consumer defined fatal error\n",
2424 libcfs_nid2str(peer->ibp_nid));
2428 if (rej->ibr_version != IBLND_MSG_VERSION) {
2429 CERROR("%s rejected: o2iblnd version %d error\n",
2430 libcfs_nid2str(peer->ibp_nid),
2435 switch (rej->ibr_why) {
2436 case IBLND_REJECT_CONN_RACE:
2437 kiblnd_reconnect(conn, "conn race");
2440 case IBLND_REJECT_NO_RESOURCES:
2441 CERROR("%s rejected: o2iblnd no resources\n",
2442 libcfs_nid2str(peer->ibp_nid));
2444 case IBLND_REJECT_FATAL:
2445 CERROR("%s rejected: o2iblnd fatal error\n",
2446 libcfs_nid2str(peer->ibp_nid));
2449 CERROR("%s rejected: o2iblnd reason %d\n",
2450 libcfs_nid2str(peer->ibp_nid),
2458 CDEBUG(D_NETERROR, "%s rejected: reason %d, size %d\n",
2459 libcfs_nid2str(peer->ibp_nid), reason, priv_nob);
2463 kiblnd_connreq_done(conn, -ECONNREFUSED);
2467 kiblnd_check_connreply (kib_conn_t *conn, void *priv, int priv_nob)
2469 kib_peer_t *peer = conn->ibc_peer;
2470 lnet_ni_t *ni = peer->ibp_ni;
2471 kib_net_t *net = ni->ni_data;
2472 kib_msg_t *msg = priv;
2473 int rc = kiblnd_unpack_msg(msg, priv_nob);
2474 unsigned long flags;
2476 LASSERT (net != NULL);
2479 CERROR("Can't unpack connack from %s: %d\n",
2480 libcfs_nid2str(peer->ibp_nid), rc);
2484 if (msg->ibm_type != IBLND_MSG_CONNACK) {
2485 CERROR("Unexpected message %d from %s\n",
2486 msg->ibm_type, libcfs_nid2str(peer->ibp_nid));
2491 if (msg->ibm_u.connparams.ibcp_queue_depth != IBLND_MSG_QUEUE_SIZE) {
2492 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2493 libcfs_nid2str(peer->ibp_nid),
2494 msg->ibm_u.connparams.ibcp_queue_depth,
2495 IBLND_MSG_QUEUE_SIZE);
2500 if (msg->ibm_u.connparams.ibcp_max_frags != IBLND_MAX_RDMA_FRAGS) {
2501 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2502 libcfs_nid2str(peer->ibp_nid),
2503 msg->ibm_u.connparams.ibcp_queue_depth,
2504 IBLND_MSG_QUEUE_SIZE);
2509 if (msg->ibm_u.connparams.ibcp_max_msg_size > IBLND_MSG_SIZE) {
2510 CERROR("%s max message size %d too big (%d max)\n",
2511 libcfs_nid2str(peer->ibp_nid),
2512 msg->ibm_u.connparams.ibcp_max_msg_size,
2518 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2519 if (msg->ibm_dstnid == ni->ni_nid &&
2520 msg->ibm_dststamp == net->ibn_incarnation)
2524 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2527 CERROR("Stale connection reply from %s\n",
2528 libcfs_nid2str(peer->ibp_nid));
2532 conn->ibc_incarnation = msg->ibm_srcstamp;
2533 conn->ibc_credits = IBLND_MSG_QUEUE_SIZE;
2534 conn->ibc_reserved_credits = IBLND_MSG_QUEUE_SIZE;
2535 LASSERT (conn->ibc_credits + conn->ibc_reserved_credits
2538 kiblnd_connreq_done(conn, 0);
2542 /* NB My QP has already established itself, so I handle anything going
2543 * wrong here by setting ibc_comms_error.
2544 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2545 * immediately tears it down. */
2548 conn->ibc_comms_error = rc;
2549 kiblnd_connreq_done(conn, 0);
2553 kiblnd_active_connect (struct rdma_cm_id *cmid)
2555 kib_peer_t *peer = (kib_peer_t *)cmid->context;
2558 struct rdma_conn_param cp;
2561 conn = kiblnd_create_conn(peer, cmid, IBLND_CONN_ACTIVE_CONNECT);
2563 kiblnd_peer_connect_failed(peer, 1, -ENOMEM);
2564 kiblnd_peer_decref(peer); /* lose cmid's ref */
2568 /* conn "owns" cmid now, so I return success from here on to ensure the
2569 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2572 msg = &conn->ibc_connvars->cv_msg;
2574 memset(msg, 0, sizeof(*msg));
2575 kiblnd_init_msg(msg, IBLND_MSG_CONNREQ, sizeof(msg->ibm_u.connparams));
2576 msg->ibm_u.connparams.ibcp_queue_depth = IBLND_MSG_QUEUE_SIZE;
2577 msg->ibm_u.connparams.ibcp_max_frags = IBLND_MAX_RDMA_FRAGS;
2578 msg->ibm_u.connparams.ibcp_max_msg_size = IBLND_MSG_SIZE;
2579 kiblnd_pack_msg(peer->ibp_ni, msg, 0, peer->ibp_nid, 0);
2581 memset(&cp, 0, sizeof(cp));
2582 cp.private_data = msg;
2583 cp.private_data_len = msg->ibm_nob;
2584 cp.responder_resources = 0; /* No atomic ops or RDMA reads */
2585 cp.initiator_depth = 0;
2586 cp.flow_control = 1;
2587 cp.retry_count = *kiblnd_tunables.kib_retry_count;
2588 cp.rnr_retry_count = *kiblnd_tunables.kib_rnr_retry_count;
2590 LASSERT(cmid->context == (void *)conn);
2591 LASSERT(conn->ibc_cmid == cmid);
2593 rc = rdma_connect(cmid, &cp);
2595 CERROR("Can't connect to %s: %d\n",
2596 libcfs_nid2str(peer->ibp_nid), rc);
2597 kiblnd_connreq_done(conn, rc);
2598 kiblnd_conn_decref(conn);
2605 kiblnd_cm_callback(struct rdma_cm_id *cmid, struct rdma_cm_event *event)
2611 switch (event->event) {
2615 case RDMA_CM_EVENT_CONNECT_REQUEST:
2616 /* destroy cmid on failure */
2617 rc = kiblnd_passive_connect(cmid,
2618 (void *)KIBLND_CONN_PARAM(event),
2619 KIBLND_CONN_PARAM_LEN(event));
2620 CDEBUG(D_NET, "connreq: %d\n", rc);
2623 case RDMA_CM_EVENT_ADDR_ERROR:
2624 peer = (kib_peer_t *)cmid->context;
2625 CDEBUG(D_NETERROR, "%s: ADDR ERROR %d\n",
2626 libcfs_nid2str(peer->ibp_nid), event->status);
2627 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2628 kiblnd_peer_decref(peer);
2629 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2631 case RDMA_CM_EVENT_ADDR_RESOLVED:
2632 peer = (kib_peer_t *)cmid->context;
2634 CDEBUG(D_NET,"%s Addr resolved: %d\n",
2635 libcfs_nid2str(peer->ibp_nid), event->status);
2637 if (event->status != 0) {
2638 CDEBUG(D_NETERROR, "Can't resolve address for %s: %d\n",
2639 libcfs_nid2str(peer->ibp_nid), event->status);
2642 rc = rdma_resolve_route(
2643 cmid, *kiblnd_tunables.kib_timeout * 1000);
2646 /* Can't initiate route resolution */
2647 CERROR("Can't resolve route for %s: %d\n",
2648 libcfs_nid2str(peer->ibp_nid), rc);
2650 kiblnd_peer_connect_failed(peer, 1, rc);
2651 kiblnd_peer_decref(peer);
2652 return rc; /* rc != 0 destroys cmid */
2654 case RDMA_CM_EVENT_ROUTE_ERROR:
2655 peer = (kib_peer_t *)cmid->context;
2656 CDEBUG(D_NETERROR, "%s: ROUTE ERROR %d\n",
2657 libcfs_nid2str(peer->ibp_nid), event->status);
2658 kiblnd_peer_connect_failed(peer, 1, -EHOSTUNREACH);
2659 kiblnd_peer_decref(peer);
2660 return -EHOSTUNREACH; /* rc != 0 destroys cmid */
2662 case RDMA_CM_EVENT_ROUTE_RESOLVED:
2663 peer = (kib_peer_t *)cmid->context;
2664 CDEBUG(D_NET,"%s Route resolved: %d\n",
2665 libcfs_nid2str(peer->ibp_nid), event->status);
2667 if (event->status == 0)
2668 return kiblnd_active_connect(cmid);
2670 CDEBUG(D_NETERROR, "Can't resolve route for %s: %d\n",
2671 libcfs_nid2str(peer->ibp_nid), event->status);
2672 kiblnd_peer_connect_failed(peer, 1, event->status);
2673 kiblnd_peer_decref(peer);
2674 return event->status; /* rc != 0 destroys cmid */
2676 case RDMA_CM_EVENT_UNREACHABLE:
2677 conn = (kib_conn_t *)cmid->context;
2678 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2679 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2680 CDEBUG(D_NETERROR, "%s: UNREACHABLE %d\n",
2681 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2682 kiblnd_connreq_done(conn, -ENETDOWN);
2683 kiblnd_conn_decref(conn);
2686 case RDMA_CM_EVENT_CONNECT_ERROR:
2687 conn = (kib_conn_t *)cmid->context;
2688 LASSERT(conn->ibc_state == IBLND_CONN_ACTIVE_CONNECT ||
2689 conn->ibc_state == IBLND_CONN_PASSIVE_WAIT);
2690 CDEBUG(D_NETERROR, "%s: CONNECT ERROR %d\n",
2691 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->status);
2692 kiblnd_connreq_done(conn, -ENOTCONN);
2693 kiblnd_conn_decref(conn);
2696 case RDMA_CM_EVENT_REJECTED:
2697 conn = (kib_conn_t *)cmid->context;
2698 switch (conn->ibc_state) {
2702 case IBLND_CONN_PASSIVE_WAIT:
2703 CERROR ("%s: REJECTED %d\n",
2704 libcfs_nid2str(conn->ibc_peer->ibp_nid),
2706 kiblnd_connreq_done(conn, -ECONNRESET);
2709 case IBLND_CONN_ACTIVE_CONNECT:
2710 kiblnd_rejected(conn, event->status,
2711 (void *)KIBLND_CONN_PARAM(event),
2712 KIBLND_CONN_PARAM_LEN(event));
2715 kiblnd_conn_decref(conn);
2718 case RDMA_CM_EVENT_ESTABLISHED:
2719 conn = (kib_conn_t *)cmid->context;
2720 switch (conn->ibc_state) {
2724 case IBLND_CONN_PASSIVE_WAIT:
2725 CDEBUG(D_NET, "ESTABLISHED (passive): %s\n",
2726 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2727 kiblnd_connreq_done(conn, 0);
2730 case IBLND_CONN_ACTIVE_CONNECT:
2731 CDEBUG(D_NET, "ESTABLISHED(active): %s\n",
2732 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2733 kiblnd_check_connreply(conn,
2734 (void *)KIBLND_CONN_PARAM(event),
2735 KIBLND_CONN_PARAM_LEN(event));
2738 /* net keeps its ref on conn! */
2741 case RDMA_CM_EVENT_DISCONNECTED:
2742 conn = (kib_conn_t *)cmid->context;
2743 if (conn->ibc_state < IBLND_CONN_ESTABLISHED) {
2744 CERROR("%s DISCONNECTED\n",
2745 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2746 kiblnd_connreq_done(conn, -ECONNRESET);
2748 kiblnd_close_conn(conn, 0);
2750 kiblnd_conn_decref(conn);
2753 case RDMA_CM_EVENT_DEVICE_REMOVAL:
2754 LCONSOLE_ERROR_MSG(0x131,
2755 "Received notification of device removal\n"
2756 "Please shutdown LNET to allow this to proceed\n");
2757 /* Can't remove network from underneath LNET for now, so I have
2764 kiblnd_check_txs (kib_conn_t *conn, struct list_head *txs)
2767 struct list_head *ttmp;
2770 spin_lock(&conn->ibc_lock);
2772 list_for_each (ttmp, txs) {
2773 tx = list_entry (ttmp, kib_tx_t, tx_list);
2775 if (txs != &conn->ibc_active_txs) {
2776 LASSERT (tx->tx_queued);
2778 LASSERT (!tx->tx_queued);
2779 LASSERT (tx->tx_waiting || tx->tx_sending != 0);
2782 if (time_after_eq (jiffies, tx->tx_deadline)) {
2788 spin_unlock(&conn->ibc_lock);
2793 kiblnd_conn_timed_out (kib_conn_t *conn)
2795 return kiblnd_check_txs(conn, &conn->ibc_tx_queue) ||
2796 kiblnd_check_txs(conn, &conn->ibc_tx_queue_rsrvd) ||
2797 kiblnd_check_txs(conn, &conn->ibc_tx_queue_nocred) ||
2798 kiblnd_check_txs(conn, &conn->ibc_active_txs);
2802 kiblnd_check_conns (int idx)
2804 struct list_head *peers = &kiblnd_data.kib_peers[idx];
2805 struct list_head *ptmp;
2808 struct list_head *ctmp;
2809 unsigned long flags;
2812 /* NB. We expect to have a look at all the peers and not find any
2813 * rdmas to time out, so we just use a shared lock while we
2815 read_lock_irqsave(&kiblnd_data.kib_global_lock, flags);
2817 list_for_each (ptmp, peers) {
2818 peer = list_entry (ptmp, kib_peer_t, ibp_list);
2820 list_for_each (ctmp, &peer->ibp_conns) {
2821 conn = list_entry (ctmp, kib_conn_t, ibc_list);
2823 LASSERT (conn->ibc_state == IBLND_CONN_ESTABLISHED);
2825 /* In case we have enough credits to return via a
2826 * NOOP, but there were no non-blocking tx descs
2827 * free to do it last time... */
2828 kiblnd_check_sends(conn);
2830 if (!kiblnd_conn_timed_out(conn))
2833 /* Handle timeout by closing the whole connection. We
2834 * can only be sure RDMA activity has ceased once the
2835 * QP has been modified. */
2837 kiblnd_conn_addref(conn); /* 1 ref for me... */
2839 read_unlock_irqrestore(&kiblnd_data.kib_global_lock,
2842 CERROR("Timed out RDMA with %s\n",
2843 libcfs_nid2str(peer->ibp_nid));
2845 kiblnd_close_conn(conn, -ETIMEDOUT);
2846 kiblnd_conn_decref(conn); /* ...until here */
2848 /* start again now I've dropped the lock */
2853 read_unlock_irqrestore(&kiblnd_data.kib_global_lock, flags);
2857 kiblnd_disconnect_conn (kib_conn_t *conn)
2859 LASSERT (!in_interrupt());
2860 LASSERT (current == kiblnd_data.kib_connd);
2861 LASSERT (conn->ibc_state == IBLND_CONN_CLOSING);
2863 rdma_disconnect(conn->ibc_cmid);
2864 kiblnd_finalise_conn(conn);
2866 kiblnd_peer_notify(conn->ibc_peer);
2870 kiblnd_connd (void *arg)
2873 unsigned long flags;
2879 unsigned long deadline = jiffies;
2881 cfs_daemonize ("kiblnd_connd");
2882 cfs_block_allsigs ();
2884 init_waitqueue_entry (&wait, current);
2885 kiblnd_data.kib_connd = current;
2887 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2889 while (!kiblnd_data.kib_shutdown) {
2893 if (!list_empty (&kiblnd_data.kib_connd_zombies)) {
2894 conn = list_entry (kiblnd_data.kib_connd_zombies.next,
2895 kib_conn_t, ibc_list);
2896 list_del (&conn->ibc_list);
2898 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2901 kiblnd_destroy_conn(conn);
2903 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2906 if (!list_empty (&kiblnd_data.kib_connd_conns)) {
2907 conn = list_entry (kiblnd_data.kib_connd_conns.next,
2908 kib_conn_t, ibc_list);
2909 list_del (&conn->ibc_list);
2911 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2914 kiblnd_disconnect_conn(conn);
2915 kiblnd_conn_decref(conn);
2917 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2920 /* careful with the jiffy wrap... */
2921 timeout = (int)(deadline - jiffies);
2925 int chunk = kiblnd_data.kib_peer_hash_size;
2927 spin_unlock_irqrestore(&kiblnd_data.kib_connd_lock, flags);
2930 /* Time to check for RDMA timeouts on a few more
2931 * peers: I do checks every 'p' seconds on a
2932 * proportion of the peer table and I need to check
2933 * every connection 'n' times within a timeout
2934 * interval, to ensure I detect a timeout on any
2935 * connection within (n+1)/n times the timeout
2938 if (*kiblnd_tunables.kib_timeout > n * p)
2939 chunk = (chunk * n * p) /
2940 *kiblnd_tunables.kib_timeout;
2944 for (i = 0; i < chunk; i++) {
2945 kiblnd_check_conns(peer_index);
2946 peer_index = (peer_index + 1) %
2947 kiblnd_data.kib_peer_hash_size;
2951 spin_lock_irqsave(&kiblnd_data.kib_connd_lock, flags);
2957 /* Nothing to do for 'timeout' */
2958 set_current_state (TASK_INTERRUPTIBLE);
2959 add_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
2960 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2962 schedule_timeout (timeout);
2964 set_current_state (TASK_RUNNING);
2965 remove_wait_queue (&kiblnd_data.kib_connd_waitq, &wait);
2966 spin_lock_irqsave (&kiblnd_data.kib_connd_lock, flags);
2969 spin_unlock_irqrestore (&kiblnd_data.kib_connd_lock, flags);
2971 kiblnd_thread_fini();
2976 kiblnd_qp_event(struct ib_event *event, void *arg)
2978 kib_conn_t *conn = arg;
2980 switch (event->event) {
2981 case IB_EVENT_COMM_EST:
2982 CDEBUG(D_NET, "%s established\n",
2983 libcfs_nid2str(conn->ibc_peer->ibp_nid));
2987 CERROR("%s: Async QP event type %d\n",
2988 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
2994 kiblnd_complete (struct ib_wc *wc)
2996 switch (kiblnd_wreqid2type(wc->wr_id)) {
3000 case IBLND_WID_RDMA:
3001 /* We only get RDMA completion notification if it fails. All
3002 * subsequent work items, including the final SEND will fail
3003 * too. However we can't print out any more info about the
3004 * failing RDMA because 'tx' might be back on the idle list or
3005 * even reused already if we didn't manage to post all our work
3007 CDEBUG(D_NETERROR, "RDMA (tx: %p) failed: %d\n",
3008 kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3012 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status);
3016 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc->wr_id), wc->status,
3023 kiblnd_cq_completion (struct ib_cq *cq, void *arg)
3025 /* NB I'm not allowed to schedule this conn once its refcount has
3026 * reached 0. Since fundamentally I'm racing with scheduler threads
3027 * consuming my CQ I could be called after all completions have
3028 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3029 * and this CQ is about to be destroyed so I NOOP. */
3030 kib_conn_t *conn = (kib_conn_t *)arg;
3031 unsigned long flags;
3033 LASSERT (cq == conn->ibc_cq);
3035 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3037 conn->ibc_ready = 1;
3039 if (!conn->ibc_scheduled &&
3040 (conn->ibc_nrx > 0 ||
3041 conn->ibc_nsends_posted > 0)) {
3042 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3043 conn->ibc_scheduled = 1;
3044 list_add_tail(&conn->ibc_sched_list,
3045 &kiblnd_data.kib_sched_conns);
3046 wake_up(&kiblnd_data.kib_sched_waitq);
3049 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3053 kiblnd_cq_event(struct ib_event *event, void *arg)
3055 kib_conn_t *conn = arg;
3057 CERROR("%s: async CQ event type %d\n",
3058 libcfs_nid2str(conn->ibc_peer->ibp_nid), event->event);
3062 kiblnd_scheduler(void *arg)
3064 long id = (long)arg;
3067 unsigned long flags;
3074 snprintf(name, sizeof(name), "kiblnd_sd_%02ld", id);
3075 cfs_daemonize(name);
3076 cfs_block_allsigs();
3078 init_waitqueue_entry(&wait, current);
3080 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3082 while (!kiblnd_data.kib_shutdown) {
3083 if (busy_loops++ >= IBLND_RESCHED) {
3084 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3090 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3095 if (!list_empty(&kiblnd_data.kib_sched_conns)) {
3096 conn = list_entry(kiblnd_data.kib_sched_conns.next,
3097 kib_conn_t, ibc_sched_list);
3098 /* take over kib_sched_conns' ref on conn... */
3099 LASSERT(conn->ibc_scheduled);
3100 list_del(&conn->ibc_sched_list);
3101 conn->ibc_ready = 0;
3103 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3106 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3108 rc = ib_req_notify_cq(conn->ibc_cq,
3112 rc = ib_poll_cq(conn->ibc_cq, 1, &wc);
3117 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3120 if (rc != 0 || conn->ibc_ready) {
3121 /* There may be another completion waiting; get
3122 * another scheduler to check while I handle
3124 kiblnd_conn_addref(conn); /* +1 ref for sched_conns */
3125 list_add_tail(&conn->ibc_sched_list,
3126 &kiblnd_data.kib_sched_conns);
3127 wake_up(&kiblnd_data.kib_sched_waitq);
3129 conn->ibc_scheduled = 0;
3133 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock,
3136 kiblnd_complete(&wc);
3138 spin_lock_irqsave(&kiblnd_data.kib_sched_lock,
3142 kiblnd_conn_decref(conn); /* ...drop my ref from above */
3149 set_current_state(TASK_INTERRUPTIBLE);
3150 add_wait_queue_exclusive(&kiblnd_data.kib_sched_waitq, &wait);
3151 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3156 remove_wait_queue(&kiblnd_data.kib_sched_waitq, &wait);
3157 set_current_state(TASK_RUNNING);
3158 spin_lock_irqsave(&kiblnd_data.kib_sched_lock, flags);
3161 spin_unlock_irqrestore(&kiblnd_data.kib_sched_lock, flags);
3163 kiblnd_thread_fini();