4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lnet/klnds/o2iblnd/o2iblnd_cb.c
38 * Author: Eric Barton <eric@bartonsoftware.com>
44 kiblnd_tx_done(lnet_ni_t
*ni
, kib_tx_t
*tx
)
46 lnet_msg_t
*lntmsg
[2];
47 kib_net_t
*net
= ni
->ni_data
;
52 LASSERT(!in_interrupt());
53 LASSERT(!tx
->tx_queued
); /* mustn't be queued for sending */
54 LASSERT(tx
->tx_sending
== 0); /* mustn't be awaiting sent callback */
55 LASSERT(!tx
->tx_waiting
); /* mustn't be awaiting peer response */
56 LASSERT(tx
->tx_pool
!= NULL
);
58 kiblnd_unmap_tx(ni
, tx
);
60 /* tx may have up to 2 lnet msgs to finalise */
61 lntmsg
[0] = tx
->tx_lntmsg
[0]; tx
->tx_lntmsg
[0] = NULL
;
62 lntmsg
[1] = tx
->tx_lntmsg
[1]; tx
->tx_lntmsg
[1] = NULL
;
65 if (tx
->tx_conn
!= NULL
) {
66 LASSERT(ni
== tx
->tx_conn
->ibc_peer
->ibp_ni
);
68 kiblnd_conn_decref(tx
->tx_conn
);
75 kiblnd_pool_free_node(&tx
->tx_pool
->tpo_pool
, &tx
->tx_list
);
77 /* delay finalize until my descs have been freed */
78 for (i
= 0; i
< 2; i
++) {
79 if (lntmsg
[i
] == NULL
)
82 lnet_finalize(ni
, lntmsg
[i
], rc
);
87 kiblnd_txlist_done(lnet_ni_t
*ni
, struct list_head
*txlist
, int status
)
91 while (!list_empty(txlist
)) {
92 tx
= list_entry(txlist
->next
, kib_tx_t
, tx_list
);
94 list_del(&tx
->tx_list
);
97 tx
->tx_status
= status
;
98 kiblnd_tx_done(ni
, tx
);
103 kiblnd_get_idle_tx(lnet_ni_t
*ni
, lnet_nid_t target
)
105 kib_net_t
*net
= (kib_net_t
*)ni
->ni_data
;
106 struct list_head
*node
;
108 kib_tx_poolset_t
*tps
;
110 tps
= net
->ibn_tx_ps
[lnet_cpt_of_nid(target
)];
111 node
= kiblnd_pool_alloc_node(&tps
->tps_poolset
);
114 tx
= container_of(node
, kib_tx_t
, tx_list
);
116 LASSERT(tx
->tx_nwrq
== 0);
117 LASSERT(!tx
->tx_queued
);
118 LASSERT(tx
->tx_sending
== 0);
119 LASSERT(!tx
->tx_waiting
);
120 LASSERT(tx
->tx_status
== 0);
121 LASSERT(tx
->tx_conn
== NULL
);
122 LASSERT(tx
->tx_lntmsg
[0] == NULL
);
123 LASSERT(tx
->tx_lntmsg
[1] == NULL
);
124 LASSERT(tx
->tx_nfrags
== 0);
130 kiblnd_drop_rx(kib_rx_t
*rx
)
132 kib_conn_t
*conn
= rx
->rx_conn
;
133 struct kib_sched_info
*sched
= conn
->ibc_sched
;
136 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
137 LASSERT(conn
->ibc_nrx
> 0);
139 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
141 kiblnd_conn_decref(conn
);
145 kiblnd_post_rx(kib_rx_t
*rx
, int credit
)
147 kib_conn_t
*conn
= rx
->rx_conn
;
148 kib_net_t
*net
= conn
->ibc_peer
->ibp_ni
->ni_data
;
149 struct ib_recv_wr
*bad_wrq
= NULL
;
153 LASSERT(net
!= NULL
);
154 LASSERT(!in_interrupt());
155 LASSERT(credit
== IBLND_POSTRX_NO_CREDIT
||
156 credit
== IBLND_POSTRX_PEER_CREDIT
||
157 credit
== IBLND_POSTRX_RSRVD_CREDIT
);
159 mr
= kiblnd_find_dma_mr(conn
->ibc_hdev
, rx
->rx_msgaddr
, IBLND_MSG_SIZE
);
162 rx
->rx_sge
.lkey
= mr
->lkey
;
163 rx
->rx_sge
.addr
= rx
->rx_msgaddr
;
164 rx
->rx_sge
.length
= IBLND_MSG_SIZE
;
166 rx
->rx_wrq
.next
= NULL
;
167 rx
->rx_wrq
.sg_list
= &rx
->rx_sge
;
168 rx
->rx_wrq
.num_sge
= 1;
169 rx
->rx_wrq
.wr_id
= kiblnd_ptr2wreqid(rx
, IBLND_WID_RX
);
171 LASSERT(conn
->ibc_state
>= IBLND_CONN_INIT
);
172 LASSERT(rx
->rx_nob
>= 0); /* not posted */
174 if (conn
->ibc_state
> IBLND_CONN_ESTABLISHED
) {
175 kiblnd_drop_rx(rx
); /* No more posts for this rx */
179 rx
->rx_nob
= -1; /* flag posted */
181 rc
= ib_post_recv(conn
->ibc_cmid
->qp
, &rx
->rx_wrq
, &bad_wrq
);
183 CERROR("Can't post rx for %s: %d, bad_wrq: %p\n",
184 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), rc
, bad_wrq
);
188 if (conn
->ibc_state
< IBLND_CONN_ESTABLISHED
) /* Initial post */
192 kiblnd_close_conn(conn
, rc
);
193 kiblnd_drop_rx(rx
); /* No more posts for this rx */
197 if (credit
== IBLND_POSTRX_NO_CREDIT
)
200 spin_lock(&conn
->ibc_lock
);
201 if (credit
== IBLND_POSTRX_PEER_CREDIT
)
202 conn
->ibc_outstanding_credits
++;
204 conn
->ibc_reserved_credits
++;
205 spin_unlock(&conn
->ibc_lock
);
207 kiblnd_check_sends(conn
);
212 kiblnd_find_waiting_tx_locked(kib_conn_t
*conn
, int txtype
, __u64 cookie
)
214 struct list_head
*tmp
;
216 list_for_each(tmp
, &conn
->ibc_active_txs
) {
217 kib_tx_t
*tx
= list_entry(tmp
, kib_tx_t
, tx_list
);
219 LASSERT(!tx
->tx_queued
);
220 LASSERT(tx
->tx_sending
!= 0 || tx
->tx_waiting
);
222 if (tx
->tx_cookie
!= cookie
)
225 if (tx
->tx_waiting
&&
226 tx
->tx_msg
->ibm_type
== txtype
)
229 CWARN("Bad completion: %swaiting, type %x (wanted %x)\n",
230 tx
->tx_waiting
? "" : "NOT ",
231 tx
->tx_msg
->ibm_type
, txtype
);
237 kiblnd_handle_completion(kib_conn_t
*conn
, int txtype
, int status
, __u64 cookie
)
240 lnet_ni_t
*ni
= conn
->ibc_peer
->ibp_ni
;
243 spin_lock(&conn
->ibc_lock
);
245 tx
= kiblnd_find_waiting_tx_locked(conn
, txtype
, cookie
);
247 spin_unlock(&conn
->ibc_lock
);
249 CWARN("Unmatched completion type %x cookie %#llx from %s\n",
250 txtype
, cookie
, libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
251 kiblnd_close_conn(conn
, -EPROTO
);
255 if (tx
->tx_status
== 0) { /* success so far */
256 if (status
< 0) { /* failed? */
257 tx
->tx_status
= status
;
258 } else if (txtype
== IBLND_MSG_GET_REQ
) {
259 lnet_set_reply_msg_len(ni
, tx
->tx_lntmsg
[1], status
);
265 idle
= !tx
->tx_queued
&& (tx
->tx_sending
== 0);
267 list_del(&tx
->tx_list
);
269 spin_unlock(&conn
->ibc_lock
);
272 kiblnd_tx_done(ni
, tx
);
276 kiblnd_send_completion(kib_conn_t
*conn
, int type
, int status
, __u64 cookie
)
278 lnet_ni_t
*ni
= conn
->ibc_peer
->ibp_ni
;
279 kib_tx_t
*tx
= kiblnd_get_idle_tx(ni
, conn
->ibc_peer
->ibp_nid
);
282 CERROR("Can't get tx for completion %x for %s\n",
283 type
, libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
287 tx
->tx_msg
->ibm_u
.completion
.ibcm_status
= status
;
288 tx
->tx_msg
->ibm_u
.completion
.ibcm_cookie
= cookie
;
289 kiblnd_init_tx_msg(ni
, tx
, type
, sizeof(kib_completion_msg_t
));
291 kiblnd_queue_tx(tx
, conn
);
295 kiblnd_handle_rx(kib_rx_t
*rx
)
297 kib_msg_t
*msg
= rx
->rx_msg
;
298 kib_conn_t
*conn
= rx
->rx_conn
;
299 lnet_ni_t
*ni
= conn
->ibc_peer
->ibp_ni
;
300 int credits
= msg
->ibm_credits
;
306 LASSERT(conn
->ibc_state
>= IBLND_CONN_ESTABLISHED
);
308 CDEBUG(D_NET
, "Received %x[%d] from %s\n",
309 msg
->ibm_type
, credits
,
310 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
313 /* Have I received credits that will let me send? */
314 spin_lock(&conn
->ibc_lock
);
316 if (conn
->ibc_credits
+ credits
>
317 IBLND_MSG_QUEUE_SIZE(conn
->ibc_version
)) {
318 rc2
= conn
->ibc_credits
;
319 spin_unlock(&conn
->ibc_lock
);
321 CERROR("Bad credits from %s: %d + %d > %d\n",
322 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
),
324 IBLND_MSG_QUEUE_SIZE(conn
->ibc_version
));
326 kiblnd_close_conn(conn
, -EPROTO
);
327 kiblnd_post_rx(rx
, IBLND_POSTRX_NO_CREDIT
);
331 conn
->ibc_credits
+= credits
;
333 /* This ensures the credit taken by NOOP can be returned */
334 if (msg
->ibm_type
== IBLND_MSG_NOOP
&&
335 !IBLND_OOB_CAPABLE(conn
->ibc_version
)) /* v1 only */
336 conn
->ibc_outstanding_credits
++;
338 spin_unlock(&conn
->ibc_lock
);
339 kiblnd_check_sends(conn
);
342 switch (msg
->ibm_type
) {
344 CERROR("Bad IBLND message type %x from %s\n",
345 msg
->ibm_type
, libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
346 post_credit
= IBLND_POSTRX_NO_CREDIT
;
351 if (IBLND_OOB_CAPABLE(conn
->ibc_version
)) {
352 post_credit
= IBLND_POSTRX_NO_CREDIT
;
356 if (credits
!= 0) /* credit already posted */
357 post_credit
= IBLND_POSTRX_NO_CREDIT
;
358 else /* a keepalive NOOP */
359 post_credit
= IBLND_POSTRX_PEER_CREDIT
;
362 case IBLND_MSG_IMMEDIATE
:
363 post_credit
= IBLND_POSTRX_DONT_POST
;
364 rc
= lnet_parse(ni
, &msg
->ibm_u
.immediate
.ibim_hdr
,
365 msg
->ibm_srcnid
, rx
, 0);
366 if (rc
< 0) /* repost on error */
367 post_credit
= IBLND_POSTRX_PEER_CREDIT
;
370 case IBLND_MSG_PUT_REQ
:
371 post_credit
= IBLND_POSTRX_DONT_POST
;
372 rc
= lnet_parse(ni
, &msg
->ibm_u
.putreq
.ibprm_hdr
,
373 msg
->ibm_srcnid
, rx
, 1);
374 if (rc
< 0) /* repost on error */
375 post_credit
= IBLND_POSTRX_PEER_CREDIT
;
378 case IBLND_MSG_PUT_NAK
:
379 CWARN("PUT_NACK from %s\n",
380 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
381 post_credit
= IBLND_POSTRX_RSRVD_CREDIT
;
382 kiblnd_handle_completion(conn
, IBLND_MSG_PUT_REQ
,
383 msg
->ibm_u
.completion
.ibcm_status
,
384 msg
->ibm_u
.completion
.ibcm_cookie
);
387 case IBLND_MSG_PUT_ACK
:
388 post_credit
= IBLND_POSTRX_RSRVD_CREDIT
;
390 spin_lock(&conn
->ibc_lock
);
391 tx
= kiblnd_find_waiting_tx_locked(conn
, IBLND_MSG_PUT_REQ
,
392 msg
->ibm_u
.putack
.ibpam_src_cookie
);
394 list_del(&tx
->tx_list
);
395 spin_unlock(&conn
->ibc_lock
);
398 CERROR("Unmatched PUT_ACK from %s\n",
399 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
404 LASSERT(tx
->tx_waiting
);
405 /* CAVEAT EMPTOR: I could be racing with tx_complete, but...
406 * (a) I can overwrite tx_msg since my peer has received it!
407 * (b) tx_waiting set tells tx_complete() it's not done. */
409 tx
->tx_nwrq
= 0; /* overwrite PUT_REQ */
411 rc2
= kiblnd_init_rdma(conn
, tx
, IBLND_MSG_PUT_DONE
,
412 kiblnd_rd_size(&msg
->ibm_u
.putack
.ibpam_rd
),
413 &msg
->ibm_u
.putack
.ibpam_rd
,
414 msg
->ibm_u
.putack
.ibpam_dst_cookie
);
416 CERROR("Can't setup rdma for PUT to %s: %d\n",
417 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), rc2
);
419 spin_lock(&conn
->ibc_lock
);
420 tx
->tx_waiting
= 0; /* clear waiting and queue atomically */
421 kiblnd_queue_tx_locked(tx
, conn
);
422 spin_unlock(&conn
->ibc_lock
);
425 case IBLND_MSG_PUT_DONE
:
426 post_credit
= IBLND_POSTRX_PEER_CREDIT
;
427 kiblnd_handle_completion(conn
, IBLND_MSG_PUT_ACK
,
428 msg
->ibm_u
.completion
.ibcm_status
,
429 msg
->ibm_u
.completion
.ibcm_cookie
);
432 case IBLND_MSG_GET_REQ
:
433 post_credit
= IBLND_POSTRX_DONT_POST
;
434 rc
= lnet_parse(ni
, &msg
->ibm_u
.get
.ibgm_hdr
,
435 msg
->ibm_srcnid
, rx
, 1);
436 if (rc
< 0) /* repost on error */
437 post_credit
= IBLND_POSTRX_PEER_CREDIT
;
440 case IBLND_MSG_GET_DONE
:
441 post_credit
= IBLND_POSTRX_RSRVD_CREDIT
;
442 kiblnd_handle_completion(conn
, IBLND_MSG_GET_REQ
,
443 msg
->ibm_u
.completion
.ibcm_status
,
444 msg
->ibm_u
.completion
.ibcm_cookie
);
448 if (rc
< 0) /* protocol error */
449 kiblnd_close_conn(conn
, rc
);
451 if (post_credit
!= IBLND_POSTRX_DONT_POST
)
452 kiblnd_post_rx(rx
, post_credit
);
456 kiblnd_rx_complete(kib_rx_t
*rx
, int status
, int nob
)
458 kib_msg_t
*msg
= rx
->rx_msg
;
459 kib_conn_t
*conn
= rx
->rx_conn
;
460 lnet_ni_t
*ni
= conn
->ibc_peer
->ibp_ni
;
461 kib_net_t
*net
= ni
->ni_data
;
465 LASSERT(net
!= NULL
);
466 LASSERT(rx
->rx_nob
< 0); /* was posted */
467 rx
->rx_nob
= 0; /* isn't now */
469 if (conn
->ibc_state
> IBLND_CONN_ESTABLISHED
)
472 if (status
!= IB_WC_SUCCESS
) {
473 CNETERR("Rx from %s failed: %d\n",
474 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), status
);
481 rc
= kiblnd_unpack_msg(msg
, rx
->rx_nob
);
483 CERROR("Error %d unpacking rx from %s\n",
484 rc
, libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
488 if (msg
->ibm_srcnid
!= conn
->ibc_peer
->ibp_nid
||
489 msg
->ibm_dstnid
!= ni
->ni_nid
||
490 msg
->ibm_srcstamp
!= conn
->ibc_incarnation
||
491 msg
->ibm_dststamp
!= net
->ibn_incarnation
) {
492 CERROR("Stale rx from %s\n",
493 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
498 /* set time last known alive */
499 kiblnd_peer_alive(conn
->ibc_peer
);
501 /* racing with connection establishment/teardown! */
503 if (conn
->ibc_state
< IBLND_CONN_ESTABLISHED
) {
504 rwlock_t
*g_lock
= &kiblnd_data
.kib_global_lock
;
507 write_lock_irqsave(g_lock
, flags
);
508 /* must check holding global lock to eliminate race */
509 if (conn
->ibc_state
< IBLND_CONN_ESTABLISHED
) {
510 list_add_tail(&rx
->rx_list
, &conn
->ibc_early_rxs
);
511 write_unlock_irqrestore(g_lock
, flags
);
514 write_unlock_irqrestore(g_lock
, flags
);
516 kiblnd_handle_rx(rx
);
520 CDEBUG(D_NET
, "rx %p conn %p\n", rx
, conn
);
521 kiblnd_close_conn(conn
, err
);
523 kiblnd_drop_rx(rx
); /* Don't re-post rx. */
527 kiblnd_kvaddr_to_page(unsigned long vaddr
)
531 if (is_vmalloc_addr((void *)vaddr
)) {
532 page
= vmalloc_to_page((void *)vaddr
);
533 LASSERT(page
!= NULL
);
536 #ifdef CONFIG_HIGHMEM
537 if (vaddr
>= PKMAP_BASE
&&
538 vaddr
< (PKMAP_BASE
+ LAST_PKMAP
* PAGE_SIZE
)) {
539 /* No highmem pages only used for bulk (kiov) I/O */
540 CERROR("find page for address in highmem\n");
544 page
= virt_to_page(vaddr
);
545 LASSERT(page
!= NULL
);
550 kiblnd_fmr_map_tx(kib_net_t
*net
, kib_tx_t
*tx
, kib_rdma_desc_t
*rd
, int nob
)
553 __u64
*pages
= tx
->tx_pages
;
554 kib_fmr_poolset_t
*fps
;
561 LASSERT(tx
->tx_pool
!= NULL
);
562 LASSERT(tx
->tx_pool
->tpo_pool
.po_owner
!= NULL
);
564 hdev
= tx
->tx_pool
->tpo_hdev
;
566 for (i
= 0, npages
= 0; i
< rd
->rd_nfrags
; i
++) {
567 for (size
= 0; size
< rd
->rd_frags
[i
].rf_nob
;
568 size
+= hdev
->ibh_page_size
) {
569 pages
[npages
++] = (rd
->rd_frags
[i
].rf_addr
&
570 hdev
->ibh_page_mask
) + size
;
574 cpt
= tx
->tx_pool
->tpo_pool
.po_owner
->ps_cpt
;
576 fps
= net
->ibn_fmr_ps
[cpt
];
577 rc
= kiblnd_fmr_pool_map(fps
, pages
, npages
, 0, &tx
->fmr
);
579 CERROR("Can't map %d pages: %d\n", npages
, rc
);
583 /* If rd is not tx_rd, it's going to get sent to a peer, who will need
585 rd
->rd_key
= (rd
!= tx
->tx_rd
) ? tx
->fmr
.fmr_pfmr
->fmr
->rkey
:
586 tx
->fmr
.fmr_pfmr
->fmr
->lkey
;
587 rd
->rd_frags
[0].rf_addr
&= ~hdev
->ibh_page_mask
;
588 rd
->rd_frags
[0].rf_nob
= nob
;
595 kiblnd_unmap_tx(lnet_ni_t
*ni
, kib_tx_t
*tx
)
597 kib_net_t
*net
= ni
->ni_data
;
599 LASSERT(net
!= NULL
);
601 if (net
->ibn_fmr_ps
&& tx
->fmr
.fmr_pfmr
) {
602 kiblnd_fmr_pool_unmap(&tx
->fmr
, tx
->tx_status
);
603 tx
->fmr
.fmr_pfmr
= NULL
;
606 if (tx
->tx_nfrags
!= 0) {
607 kiblnd_dma_unmap_sg(tx
->tx_pool
->tpo_hdev
->ibh_ibdev
,
608 tx
->tx_frags
, tx
->tx_nfrags
, tx
->tx_dmadir
);
614 kiblnd_map_tx(lnet_ni_t
*ni
, kib_tx_t
*tx
,
615 kib_rdma_desc_t
*rd
, int nfrags
)
617 kib_hca_dev_t
*hdev
= tx
->tx_pool
->tpo_hdev
;
618 kib_net_t
*net
= ni
->ni_data
;
619 struct ib_mr
*mr
= NULL
;
623 /* If rd is not tx_rd, it's going to get sent to a peer and I'm the
625 tx
->tx_dmadir
= (rd
!= tx
->tx_rd
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
626 tx
->tx_nfrags
= nfrags
;
628 rd
->rd_nfrags
= kiblnd_dma_map_sg(hdev
->ibh_ibdev
, tx
->tx_frags
,
629 tx
->tx_nfrags
, tx
->tx_dmadir
);
631 for (i
= 0, nob
= 0; i
< rd
->rd_nfrags
; i
++) {
632 rd
->rd_frags
[i
].rf_nob
= kiblnd_sg_dma_len(
633 hdev
->ibh_ibdev
, &tx
->tx_frags
[i
]);
634 rd
->rd_frags
[i
].rf_addr
= kiblnd_sg_dma_address(
635 hdev
->ibh_ibdev
, &tx
->tx_frags
[i
]);
636 nob
+= rd
->rd_frags
[i
].rf_nob
;
639 /* looking for pre-mapping MR */
640 mr
= kiblnd_find_rd_dma_mr(hdev
, rd
);
642 /* found pre-mapping MR */
643 rd
->rd_key
= (rd
!= tx
->tx_rd
) ? mr
->rkey
: mr
->lkey
;
647 if (net
->ibn_fmr_ps
!= NULL
)
648 return kiblnd_fmr_map_tx(net
, tx
, rd
, nob
);
655 kiblnd_setup_rd_iov(lnet_ni_t
*ni
, kib_tx_t
*tx
, kib_rdma_desc_t
*rd
,
656 unsigned int niov
, struct kvec
*iov
, int offset
, int nob
)
658 kib_net_t
*net
= ni
->ni_data
;
660 struct scatterlist
*sg
;
667 LASSERT(net
!= NULL
);
669 while (offset
>= iov
->iov_len
) {
670 offset
-= iov
->iov_len
;
680 vaddr
= ((unsigned long)iov
->iov_base
) + offset
;
681 page_offset
= vaddr
& (PAGE_SIZE
- 1);
682 page
= kiblnd_kvaddr_to_page(vaddr
);
684 CERROR("Can't find page\n");
688 fragnob
= min((int)(iov
->iov_len
- offset
), nob
);
689 fragnob
= min(fragnob
, (int)PAGE_SIZE
- page_offset
);
691 sg_set_page(sg
, page
, fragnob
, page_offset
);
694 if (offset
+ fragnob
< iov
->iov_len
) {
704 return kiblnd_map_tx(ni
, tx
, rd
, sg
- tx
->tx_frags
);
708 kiblnd_setup_rd_kiov(lnet_ni_t
*ni
, kib_tx_t
*tx
, kib_rdma_desc_t
*rd
,
709 int nkiov
, lnet_kiov_t
*kiov
, int offset
, int nob
)
711 kib_net_t
*net
= ni
->ni_data
;
712 struct scatterlist
*sg
;
715 CDEBUG(D_NET
, "niov %d offset %d nob %d\n", nkiov
, offset
, nob
);
719 LASSERT(net
!= NULL
);
721 while (offset
>= kiov
->kiov_len
) {
722 offset
-= kiov
->kiov_len
;
732 fragnob
= min((int)(kiov
->kiov_len
- offset
), nob
);
734 sg_set_page(sg
, kiov
->kiov_page
, fragnob
,
735 kiov
->kiov_offset
+ offset
);
744 return kiblnd_map_tx(ni
, tx
, rd
, sg
- tx
->tx_frags
);
748 kiblnd_post_tx_locked(kib_conn_t
*conn
, kib_tx_t
*tx
, int credit
)
749 __releases(conn
->ibc_lock
)
750 __acquires(conn
->ibc_lock
)
752 kib_msg_t
*msg
= tx
->tx_msg
;
753 kib_peer_t
*peer
= conn
->ibc_peer
;
754 int ver
= conn
->ibc_version
;
757 struct ib_send_wr
*bad_wrq
;
759 LASSERT(tx
->tx_queued
);
760 /* We rely on this for QP sizing */
761 LASSERT(tx
->tx_nwrq
> 0);
762 LASSERT(tx
->tx_nwrq
<= 1 + IBLND_RDMA_FRAGS(ver
));
764 LASSERT(credit
== 0 || credit
== 1);
765 LASSERT(conn
->ibc_outstanding_credits
>= 0);
766 LASSERT(conn
->ibc_outstanding_credits
<= IBLND_MSG_QUEUE_SIZE(ver
));
767 LASSERT(conn
->ibc_credits
>= 0);
768 LASSERT(conn
->ibc_credits
<= IBLND_MSG_QUEUE_SIZE(ver
));
770 if (conn
->ibc_nsends_posted
== IBLND_CONCURRENT_SENDS(ver
)) {
771 /* tx completions outstanding... */
772 CDEBUG(D_NET
, "%s: posted enough\n",
773 libcfs_nid2str(peer
->ibp_nid
));
777 if (credit
!= 0 && conn
->ibc_credits
== 0) { /* no credits */
778 CDEBUG(D_NET
, "%s: no credits\n",
779 libcfs_nid2str(peer
->ibp_nid
));
783 if (credit
!= 0 && !IBLND_OOB_CAPABLE(ver
) &&
784 conn
->ibc_credits
== 1 && /* last credit reserved */
785 msg
->ibm_type
!= IBLND_MSG_NOOP
) { /* for NOOP */
786 CDEBUG(D_NET
, "%s: not using last credit\n",
787 libcfs_nid2str(peer
->ibp_nid
));
791 /* NB don't drop ibc_lock before bumping tx_sending */
792 list_del(&tx
->tx_list
);
795 if (msg
->ibm_type
== IBLND_MSG_NOOP
&&
796 (!kiblnd_need_noop(conn
) || /* redundant NOOP */
797 (IBLND_OOB_CAPABLE(ver
) && /* posted enough NOOP */
798 conn
->ibc_noops_posted
== IBLND_OOB_MSGS(ver
)))) {
799 /* OK to drop when posted enough NOOPs, since
800 * kiblnd_check_sends will queue NOOP again when
801 * posted NOOPs complete */
802 spin_unlock(&conn
->ibc_lock
);
803 kiblnd_tx_done(peer
->ibp_ni
, tx
);
804 spin_lock(&conn
->ibc_lock
);
805 CDEBUG(D_NET
, "%s(%d): redundant or enough NOOP\n",
806 libcfs_nid2str(peer
->ibp_nid
),
807 conn
->ibc_noops_posted
);
811 kiblnd_pack_msg(peer
->ibp_ni
, msg
, ver
, conn
->ibc_outstanding_credits
,
812 peer
->ibp_nid
, conn
->ibc_incarnation
);
814 conn
->ibc_credits
-= credit
;
815 conn
->ibc_outstanding_credits
= 0;
816 conn
->ibc_nsends_posted
++;
817 if (msg
->ibm_type
== IBLND_MSG_NOOP
)
818 conn
->ibc_noops_posted
++;
820 /* CAVEAT EMPTOR! This tx could be the PUT_DONE of an RDMA
821 * PUT. If so, it was first queued here as a PUT_REQ, sent and
822 * stashed on ibc_active_txs, matched by an incoming PUT_ACK,
823 * and then re-queued here. It's (just) possible that
824 * tx_sending is non-zero if we've not done the tx_complete()
825 * from the first send; hence the ++ rather than = below. */
827 list_add(&tx
->tx_list
, &conn
->ibc_active_txs
);
829 /* I'm still holding ibc_lock! */
830 if (conn
->ibc_state
!= IBLND_CONN_ESTABLISHED
) {
832 } else if (tx
->tx_pool
->tpo_pool
.po_failed
||
833 conn
->ibc_hdev
!= tx
->tx_pool
->tpo_hdev
) {
834 /* close_conn will launch failover */
837 rc
= ib_post_send(conn
->ibc_cmid
->qp
, tx
->tx_wrq
, &bad_wrq
);
840 conn
->ibc_last_send
= jiffies
;
845 /* NB credits are transferred in the actual
846 * message, which can only be the last work item */
847 conn
->ibc_credits
+= credit
;
848 conn
->ibc_outstanding_credits
+= msg
->ibm_credits
;
849 conn
->ibc_nsends_posted
--;
850 if (msg
->ibm_type
== IBLND_MSG_NOOP
)
851 conn
->ibc_noops_posted
--;
857 done
= (tx
->tx_sending
== 0);
859 list_del(&tx
->tx_list
);
861 spin_unlock(&conn
->ibc_lock
);
863 if (conn
->ibc_state
== IBLND_CONN_ESTABLISHED
)
864 CERROR("Error %d posting transmit to %s\n",
865 rc
, libcfs_nid2str(peer
->ibp_nid
));
867 CDEBUG(D_NET
, "Error %d posting transmit to %s\n",
868 rc
, libcfs_nid2str(peer
->ibp_nid
));
870 kiblnd_close_conn(conn
, rc
);
873 kiblnd_tx_done(peer
->ibp_ni
, tx
);
875 spin_lock(&conn
->ibc_lock
);
881 kiblnd_check_sends(kib_conn_t
*conn
)
883 int ver
= conn
->ibc_version
;
884 lnet_ni_t
*ni
= conn
->ibc_peer
->ibp_ni
;
887 /* Don't send anything until after the connection is established */
888 if (conn
->ibc_state
< IBLND_CONN_ESTABLISHED
) {
889 CDEBUG(D_NET
, "%s too soon\n",
890 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
894 spin_lock(&conn
->ibc_lock
);
896 LASSERT(conn
->ibc_nsends_posted
<= IBLND_CONCURRENT_SENDS(ver
));
897 LASSERT(!IBLND_OOB_CAPABLE(ver
) ||
898 conn
->ibc_noops_posted
<= IBLND_OOB_MSGS(ver
));
899 LASSERT(conn
->ibc_reserved_credits
>= 0);
901 while (conn
->ibc_reserved_credits
> 0 &&
902 !list_empty(&conn
->ibc_tx_queue_rsrvd
)) {
903 tx
= list_entry(conn
->ibc_tx_queue_rsrvd
.next
,
905 list_del(&tx
->tx_list
);
906 list_add_tail(&tx
->tx_list
, &conn
->ibc_tx_queue
);
907 conn
->ibc_reserved_credits
--;
910 if (kiblnd_need_noop(conn
)) {
911 spin_unlock(&conn
->ibc_lock
);
913 tx
= kiblnd_get_idle_tx(ni
, conn
->ibc_peer
->ibp_nid
);
915 kiblnd_init_tx_msg(ni
, tx
, IBLND_MSG_NOOP
, 0);
917 spin_lock(&conn
->ibc_lock
);
919 kiblnd_queue_tx_locked(tx
, conn
);
922 kiblnd_conn_addref(conn
); /* 1 ref for me.... (see b21911) */
927 if (!list_empty(&conn
->ibc_tx_queue_nocred
)) {
929 tx
= list_entry(conn
->ibc_tx_queue_nocred
.next
,
931 } else if (!list_empty(&conn
->ibc_tx_noops
)) {
932 LASSERT(!IBLND_OOB_CAPABLE(ver
));
934 tx
= list_entry(conn
->ibc_tx_noops
.next
,
936 } else if (!list_empty(&conn
->ibc_tx_queue
)) {
938 tx
= list_entry(conn
->ibc_tx_queue
.next
,
943 if (kiblnd_post_tx_locked(conn
, tx
, credit
) != 0)
947 spin_unlock(&conn
->ibc_lock
);
949 kiblnd_conn_decref(conn
); /* ...until here */
953 kiblnd_tx_complete(kib_tx_t
*tx
, int status
)
955 int failed
= (status
!= IB_WC_SUCCESS
);
956 kib_conn_t
*conn
= tx
->tx_conn
;
959 LASSERT(tx
->tx_sending
> 0);
962 if (conn
->ibc_state
== IBLND_CONN_ESTABLISHED
)
963 CNETERR("Tx -> %s cookie %#llx sending %d waiting %d: failed %d\n",
964 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
),
965 tx
->tx_cookie
, tx
->tx_sending
, tx
->tx_waiting
,
968 kiblnd_close_conn(conn
, -EIO
);
970 kiblnd_peer_alive(conn
->ibc_peer
);
973 spin_lock(&conn
->ibc_lock
);
975 /* I could be racing with rdma completion. Whoever makes 'tx' idle
976 * gets to free it, which also drops its ref on 'conn'. */
979 conn
->ibc_nsends_posted
--;
980 if (tx
->tx_msg
->ibm_type
== IBLND_MSG_NOOP
)
981 conn
->ibc_noops_posted
--;
984 tx
->tx_waiting
= 0; /* don't wait for peer */
985 tx
->tx_status
= -EIO
;
988 idle
= (tx
->tx_sending
== 0) && /* This is the final callback */
989 !tx
->tx_waiting
&& /* Not waiting for peer */
990 !tx
->tx_queued
; /* Not re-queued (PUT_DONE) */
992 list_del(&tx
->tx_list
);
994 kiblnd_conn_addref(conn
); /* 1 ref for me.... */
996 spin_unlock(&conn
->ibc_lock
);
999 kiblnd_tx_done(conn
->ibc_peer
->ibp_ni
, tx
);
1001 kiblnd_check_sends(conn
);
1003 kiblnd_conn_decref(conn
); /* ...until here */
1007 kiblnd_init_tx_msg(lnet_ni_t
*ni
, kib_tx_t
*tx
, int type
, int body_nob
)
1009 kib_hca_dev_t
*hdev
= tx
->tx_pool
->tpo_hdev
;
1010 struct ib_sge
*sge
= &tx
->tx_sge
[tx
->tx_nwrq
];
1011 struct ib_send_wr
*wrq
= &tx
->tx_wrq
[tx
->tx_nwrq
];
1012 int nob
= offsetof(kib_msg_t
, ibm_u
) + body_nob
;
1015 LASSERT(tx
->tx_nwrq
>= 0);
1016 LASSERT(tx
->tx_nwrq
< IBLND_MAX_RDMA_FRAGS
+ 1);
1017 LASSERT(nob
<= IBLND_MSG_SIZE
);
1019 kiblnd_init_msg(tx
->tx_msg
, type
, body_nob
);
1021 mr
= kiblnd_find_dma_mr(hdev
, tx
->tx_msgaddr
, nob
);
1022 LASSERT(mr
!= NULL
);
1024 sge
->lkey
= mr
->lkey
;
1025 sge
->addr
= tx
->tx_msgaddr
;
1028 memset(wrq
, 0, sizeof(*wrq
));
1031 wrq
->wr_id
= kiblnd_ptr2wreqid(tx
, IBLND_WID_TX
);
1034 wrq
->opcode
= IB_WR_SEND
;
1035 wrq
->send_flags
= IB_SEND_SIGNALED
;
1041 kiblnd_init_rdma(kib_conn_t
*conn
, kib_tx_t
*tx
, int type
,
1042 int resid
, kib_rdma_desc_t
*dstrd
, __u64 dstcookie
)
1044 kib_msg_t
*ibmsg
= tx
->tx_msg
;
1045 kib_rdma_desc_t
*srcrd
= tx
->tx_rd
;
1046 struct ib_sge
*sge
= &tx
->tx_sge
[0];
1047 struct ib_send_wr
*wrq
= &tx
->tx_wrq
[0];
1053 LASSERT(!in_interrupt());
1054 LASSERT(tx
->tx_nwrq
== 0);
1055 LASSERT(type
== IBLND_MSG_GET_DONE
||
1056 type
== IBLND_MSG_PUT_DONE
);
1058 srcidx
= dstidx
= 0;
1061 if (srcidx
>= srcrd
->rd_nfrags
) {
1062 CERROR("Src buffer exhausted: %d frags\n", srcidx
);
1067 if (dstidx
== dstrd
->rd_nfrags
) {
1068 CERROR("Dst buffer exhausted: %d frags\n", dstidx
);
1073 if (tx
->tx_nwrq
== IBLND_RDMA_FRAGS(conn
->ibc_version
)) {
1074 CERROR("RDMA too fragmented for %s (%d): %d/%d src %d/%d dst frags\n",
1075 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
),
1076 IBLND_RDMA_FRAGS(conn
->ibc_version
),
1077 srcidx
, srcrd
->rd_nfrags
,
1078 dstidx
, dstrd
->rd_nfrags
);
1083 wrknob
= min(min(kiblnd_rd_frag_size(srcrd
, srcidx
),
1084 kiblnd_rd_frag_size(dstrd
, dstidx
)),
1087 sge
= &tx
->tx_sge
[tx
->tx_nwrq
];
1088 sge
->addr
= kiblnd_rd_frag_addr(srcrd
, srcidx
);
1089 sge
->lkey
= kiblnd_rd_frag_key(srcrd
, srcidx
);
1090 sge
->length
= wrknob
;
1092 wrq
= &tx
->tx_wrq
[tx
->tx_nwrq
];
1094 wrq
->next
= wrq
+ 1;
1095 wrq
->wr_id
= kiblnd_ptr2wreqid(tx
, IBLND_WID_RDMA
);
1098 wrq
->opcode
= IB_WR_RDMA_WRITE
;
1099 wrq
->send_flags
= 0;
1101 wrq
->wr
.rdma
.remote_addr
= kiblnd_rd_frag_addr(dstrd
, dstidx
);
1102 wrq
->wr
.rdma
.rkey
= kiblnd_rd_frag_key(dstrd
, dstidx
);
1104 srcidx
= kiblnd_rd_consume_frag(srcrd
, srcidx
, wrknob
);
1105 dstidx
= kiblnd_rd_consume_frag(dstrd
, dstidx
, wrknob
);
1114 if (rc
< 0) /* no RDMA if completing with failure */
1117 ibmsg
->ibm_u
.completion
.ibcm_status
= rc
;
1118 ibmsg
->ibm_u
.completion
.ibcm_cookie
= dstcookie
;
1119 kiblnd_init_tx_msg(conn
->ibc_peer
->ibp_ni
, tx
,
1120 type
, sizeof(kib_completion_msg_t
));
1126 kiblnd_queue_tx_locked(kib_tx_t
*tx
, kib_conn_t
*conn
)
1128 struct list_head
*q
;
1130 LASSERT(tx
->tx_nwrq
> 0); /* work items set up */
1131 LASSERT(!tx
->tx_queued
); /* not queued for sending already */
1132 LASSERT(conn
->ibc_state
>= IBLND_CONN_ESTABLISHED
);
1135 tx
->tx_deadline
= jiffies
+ (*kiblnd_tunables
.kib_timeout
* HZ
);
1137 if (tx
->tx_conn
== NULL
) {
1138 kiblnd_conn_addref(conn
);
1140 LASSERT(tx
->tx_msg
->ibm_type
!= IBLND_MSG_PUT_DONE
);
1142 /* PUT_DONE first attached to conn as a PUT_REQ */
1143 LASSERT(tx
->tx_conn
== conn
);
1144 LASSERT(tx
->tx_msg
->ibm_type
== IBLND_MSG_PUT_DONE
);
1147 switch (tx
->tx_msg
->ibm_type
) {
1151 case IBLND_MSG_PUT_REQ
:
1152 case IBLND_MSG_GET_REQ
:
1153 q
= &conn
->ibc_tx_queue_rsrvd
;
1156 case IBLND_MSG_PUT_NAK
:
1157 case IBLND_MSG_PUT_ACK
:
1158 case IBLND_MSG_PUT_DONE
:
1159 case IBLND_MSG_GET_DONE
:
1160 q
= &conn
->ibc_tx_queue_nocred
;
1163 case IBLND_MSG_NOOP
:
1164 if (IBLND_OOB_CAPABLE(conn
->ibc_version
))
1165 q
= &conn
->ibc_tx_queue_nocred
;
1167 q
= &conn
->ibc_tx_noops
;
1170 case IBLND_MSG_IMMEDIATE
:
1171 q
= &conn
->ibc_tx_queue
;
1175 list_add_tail(&tx
->tx_list
, q
);
1179 kiblnd_queue_tx(kib_tx_t
*tx
, kib_conn_t
*conn
)
1181 spin_lock(&conn
->ibc_lock
);
1182 kiblnd_queue_tx_locked(tx
, conn
);
1183 spin_unlock(&conn
->ibc_lock
);
1185 kiblnd_check_sends(conn
);
1188 static int kiblnd_resolve_addr(struct rdma_cm_id
*cmid
,
1189 struct sockaddr_in
*srcaddr
,
1190 struct sockaddr_in
*dstaddr
,
1193 unsigned short port
;
1196 /* allow the port to be reused */
1197 rc
= rdma_set_reuseaddr(cmid
, 1);
1199 CERROR("Unable to set reuse on cmid: %d\n", rc
);
1203 /* look for a free privileged port */
1204 for (port
= PROT_SOCK
-1; port
> 0; port
--) {
1205 srcaddr
->sin_port
= htons(port
);
1206 rc
= rdma_resolve_addr(cmid
,
1207 (struct sockaddr
*)srcaddr
,
1208 (struct sockaddr
*)dstaddr
,
1211 CDEBUG(D_NET
, "bound to port %hu\n", port
);
1213 } else if (rc
== -EADDRINUSE
|| rc
== -EADDRNOTAVAIL
) {
1214 CDEBUG(D_NET
, "bind to port %hu failed: %d\n",
1221 CERROR("Failed to bind to a free privileged port\n");
1226 kiblnd_connect_peer(kib_peer_t
*peer
)
1228 struct rdma_cm_id
*cmid
;
1230 kib_net_t
*net
= peer
->ibp_ni
->ni_data
;
1231 struct sockaddr_in srcaddr
;
1232 struct sockaddr_in dstaddr
;
1235 LASSERT(net
!= NULL
);
1236 LASSERT(peer
->ibp_connecting
> 0);
1238 cmid
= kiblnd_rdma_create_id(kiblnd_cm_callback
, peer
, RDMA_PS_TCP
,
1242 CERROR("Can't create CMID for %s: %ld\n",
1243 libcfs_nid2str(peer
->ibp_nid
), PTR_ERR(cmid
));
1249 memset(&srcaddr
, 0, sizeof(srcaddr
));
1250 srcaddr
.sin_family
= AF_INET
;
1251 srcaddr
.sin_addr
.s_addr
= htonl(dev
->ibd_ifip
);
1253 memset(&dstaddr
, 0, sizeof(dstaddr
));
1254 dstaddr
.sin_family
= AF_INET
;
1255 dstaddr
.sin_port
= htons(*kiblnd_tunables
.kib_service
);
1256 dstaddr
.sin_addr
.s_addr
= htonl(LNET_NIDADDR(peer
->ibp_nid
));
1258 kiblnd_peer_addref(peer
); /* cmid's ref */
1260 if (*kiblnd_tunables
.kib_use_priv_port
) {
1261 rc
= kiblnd_resolve_addr(cmid
, &srcaddr
, &dstaddr
,
1262 *kiblnd_tunables
.kib_timeout
* 1000);
1264 rc
= rdma_resolve_addr(cmid
,
1265 (struct sockaddr
*)&srcaddr
,
1266 (struct sockaddr
*)&dstaddr
,
1267 *kiblnd_tunables
.kib_timeout
* 1000);
1270 /* Can't initiate address resolution: */
1271 CERROR("Can't resolve addr for %s: %d\n",
1272 libcfs_nid2str(peer
->ibp_nid
), rc
);
1276 LASSERT(cmid
->device
!= NULL
);
1277 CDEBUG(D_NET
, "%s: connection bound to %s:%pI4h:%s\n",
1278 libcfs_nid2str(peer
->ibp_nid
), dev
->ibd_ifname
,
1279 &dev
->ibd_ifip
, cmid
->device
->name
);
1284 kiblnd_peer_decref(peer
); /* cmid's ref */
1285 rdma_destroy_id(cmid
);
1287 kiblnd_peer_connect_failed(peer
, 1, rc
);
1291 kiblnd_launch_tx(lnet_ni_t
*ni
, kib_tx_t
*tx
, lnet_nid_t nid
)
1296 rwlock_t
*g_lock
= &kiblnd_data
.kib_global_lock
;
1297 unsigned long flags
;
1300 /* If I get here, I've committed to send, so I complete the tx with
1301 * failure on any problems */
1303 LASSERT(tx
== NULL
|| tx
->tx_conn
== NULL
); /* only set when assigned a conn */
1304 LASSERT(tx
== NULL
|| tx
->tx_nwrq
> 0); /* work items have been set up */
1306 /* First time, just use a read lock since I expect to find my peer
1308 read_lock_irqsave(g_lock
, flags
);
1310 peer
= kiblnd_find_peer_locked(nid
);
1311 if (peer
!= NULL
&& !list_empty(&peer
->ibp_conns
)) {
1312 /* Found a peer with an established connection */
1313 conn
= kiblnd_get_conn_locked(peer
);
1314 kiblnd_conn_addref(conn
); /* 1 ref for me... */
1316 read_unlock_irqrestore(g_lock
, flags
);
1319 kiblnd_queue_tx(tx
, conn
);
1320 kiblnd_conn_decref(conn
); /* ...to here */
1324 read_unlock(g_lock
);
1325 /* Re-try with a write lock */
1328 peer
= kiblnd_find_peer_locked(nid
);
1330 if (list_empty(&peer
->ibp_conns
)) {
1331 /* found a peer, but it's still connecting... */
1332 LASSERT(peer
->ibp_connecting
!= 0 ||
1333 peer
->ibp_accepting
!= 0);
1335 list_add_tail(&tx
->tx_list
,
1336 &peer
->ibp_tx_queue
);
1337 write_unlock_irqrestore(g_lock
, flags
);
1339 conn
= kiblnd_get_conn_locked(peer
);
1340 kiblnd_conn_addref(conn
); /* 1 ref for me... */
1342 write_unlock_irqrestore(g_lock
, flags
);
1345 kiblnd_queue_tx(tx
, conn
);
1346 kiblnd_conn_decref(conn
); /* ...to here */
1351 write_unlock_irqrestore(g_lock
, flags
);
1353 /* Allocate a peer ready to add to the peer table and retry */
1354 rc
= kiblnd_create_peer(ni
, &peer
, nid
);
1356 CERROR("Can't create peer %s\n", libcfs_nid2str(nid
));
1358 tx
->tx_status
= -EHOSTUNREACH
;
1360 kiblnd_tx_done(ni
, tx
);
1365 write_lock_irqsave(g_lock
, flags
);
1367 peer2
= kiblnd_find_peer_locked(nid
);
1368 if (peer2
!= NULL
) {
1369 if (list_empty(&peer2
->ibp_conns
)) {
1370 /* found a peer, but it's still connecting... */
1371 LASSERT(peer2
->ibp_connecting
!= 0 ||
1372 peer2
->ibp_accepting
!= 0);
1374 list_add_tail(&tx
->tx_list
,
1375 &peer2
->ibp_tx_queue
);
1376 write_unlock_irqrestore(g_lock
, flags
);
1378 conn
= kiblnd_get_conn_locked(peer2
);
1379 kiblnd_conn_addref(conn
); /* 1 ref for me... */
1381 write_unlock_irqrestore(g_lock
, flags
);
1384 kiblnd_queue_tx(tx
, conn
);
1385 kiblnd_conn_decref(conn
); /* ...to here */
1388 kiblnd_peer_decref(peer
);
1392 /* Brand new peer */
1393 LASSERT(peer
->ibp_connecting
== 0);
1394 peer
->ibp_connecting
= 1;
1396 /* always called with a ref on ni, which prevents ni being shutdown */
1397 LASSERT(((kib_net_t
*)ni
->ni_data
)->ibn_shutdown
== 0);
1400 list_add_tail(&tx
->tx_list
, &peer
->ibp_tx_queue
);
1402 kiblnd_peer_addref(peer
);
1403 list_add_tail(&peer
->ibp_list
, kiblnd_nid2peerlist(nid
));
1405 write_unlock_irqrestore(g_lock
, flags
);
1407 kiblnd_connect_peer(peer
);
1408 kiblnd_peer_decref(peer
);
1412 kiblnd_send(lnet_ni_t
*ni
, void *private, lnet_msg_t
*lntmsg
)
1414 lnet_hdr_t
*hdr
= &lntmsg
->msg_hdr
;
1415 int type
= lntmsg
->msg_type
;
1416 lnet_process_id_t target
= lntmsg
->msg_target
;
1417 int target_is_router
= lntmsg
->msg_target_is_router
;
1418 int routing
= lntmsg
->msg_routing
;
1419 unsigned int payload_niov
= lntmsg
->msg_niov
;
1420 struct kvec
*payload_iov
= lntmsg
->msg_iov
;
1421 lnet_kiov_t
*payload_kiov
= lntmsg
->msg_kiov
;
1422 unsigned int payload_offset
= lntmsg
->msg_offset
;
1423 unsigned int payload_nob
= lntmsg
->msg_len
;
1429 /* NB 'private' is different depending on what we're sending.... */
1431 CDEBUG(D_NET
, "sending %d bytes in %d frags to %s\n",
1432 payload_nob
, payload_niov
, libcfs_id2str(target
));
1434 LASSERT(payload_nob
== 0 || payload_niov
> 0);
1435 LASSERT(payload_niov
<= LNET_MAX_IOV
);
1437 /* Thread context */
1438 LASSERT(!in_interrupt());
1439 /* payload is either all vaddrs or all pages */
1440 LASSERT(!(payload_kiov
!= NULL
&& payload_iov
!= NULL
));
1448 LASSERT(payload_nob
== 0);
1452 if (routing
|| target_is_router
)
1453 break; /* send IMMEDIATE */
1455 /* is the REPLY message too small for RDMA? */
1456 nob
= offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
[lntmsg
->msg_md
->md_length
]);
1457 if (nob
<= IBLND_MSG_SIZE
)
1458 break; /* send IMMEDIATE */
1460 tx
= kiblnd_get_idle_tx(ni
, target
.nid
);
1462 CERROR("Can't allocate txd for GET to %s\n",
1463 libcfs_nid2str(target
.nid
));
1469 if ((lntmsg
->msg_md
->md_options
& LNET_MD_KIOV
) == 0)
1470 rc
= kiblnd_setup_rd_iov(ni
, tx
,
1471 &ibmsg
->ibm_u
.get
.ibgm_rd
,
1472 lntmsg
->msg_md
->md_niov
,
1473 lntmsg
->msg_md
->md_iov
.iov
,
1474 0, lntmsg
->msg_md
->md_length
);
1476 rc
= kiblnd_setup_rd_kiov(ni
, tx
,
1477 &ibmsg
->ibm_u
.get
.ibgm_rd
,
1478 lntmsg
->msg_md
->md_niov
,
1479 lntmsg
->msg_md
->md_iov
.kiov
,
1480 0, lntmsg
->msg_md
->md_length
);
1482 CERROR("Can't setup GET sink for %s: %d\n",
1483 libcfs_nid2str(target
.nid
), rc
);
1484 kiblnd_tx_done(ni
, tx
);
1488 nob
= offsetof(kib_get_msg_t
, ibgm_rd
.rd_frags
[tx
->tx_nfrags
]);
1489 ibmsg
->ibm_u
.get
.ibgm_cookie
= tx
->tx_cookie
;
1490 ibmsg
->ibm_u
.get
.ibgm_hdr
= *hdr
;
1492 kiblnd_init_tx_msg(ni
, tx
, IBLND_MSG_GET_REQ
, nob
);
1494 tx
->tx_lntmsg
[1] = lnet_create_reply_msg(ni
, lntmsg
);
1495 if (tx
->tx_lntmsg
[1] == NULL
) {
1496 CERROR("Can't create reply for GET -> %s\n",
1497 libcfs_nid2str(target
.nid
));
1498 kiblnd_tx_done(ni
, tx
);
1502 tx
->tx_lntmsg
[0] = lntmsg
; /* finalise lntmsg[0,1] on completion */
1503 tx
->tx_waiting
= 1; /* waiting for GET_DONE */
1504 kiblnd_launch_tx(ni
, tx
, target
.nid
);
1507 case LNET_MSG_REPLY
:
1509 /* Is the payload small enough not to need RDMA? */
1510 nob
= offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
[payload_nob
]);
1511 if (nob
<= IBLND_MSG_SIZE
)
1512 break; /* send IMMEDIATE */
1514 tx
= kiblnd_get_idle_tx(ni
, target
.nid
);
1516 CERROR("Can't allocate %s txd for %s\n",
1517 type
== LNET_MSG_PUT
? "PUT" : "REPLY",
1518 libcfs_nid2str(target
.nid
));
1522 if (payload_kiov
== NULL
)
1523 rc
= kiblnd_setup_rd_iov(ni
, tx
, tx
->tx_rd
,
1524 payload_niov
, payload_iov
,
1525 payload_offset
, payload_nob
);
1527 rc
= kiblnd_setup_rd_kiov(ni
, tx
, tx
->tx_rd
,
1528 payload_niov
, payload_kiov
,
1529 payload_offset
, payload_nob
);
1531 CERROR("Can't setup PUT src for %s: %d\n",
1532 libcfs_nid2str(target
.nid
), rc
);
1533 kiblnd_tx_done(ni
, tx
);
1538 ibmsg
->ibm_u
.putreq
.ibprm_hdr
= *hdr
;
1539 ibmsg
->ibm_u
.putreq
.ibprm_cookie
= tx
->tx_cookie
;
1540 kiblnd_init_tx_msg(ni
, tx
, IBLND_MSG_PUT_REQ
, sizeof(kib_putreq_msg_t
));
1542 tx
->tx_lntmsg
[0] = lntmsg
; /* finalise lntmsg on completion */
1543 tx
->tx_waiting
= 1; /* waiting for PUT_{ACK,NAK} */
1544 kiblnd_launch_tx(ni
, tx
, target
.nid
);
1548 /* send IMMEDIATE */
1550 LASSERT(offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
[payload_nob
])
1553 tx
= kiblnd_get_idle_tx(ni
, target
.nid
);
1555 CERROR("Can't send %d to %s: tx descs exhausted\n",
1556 type
, libcfs_nid2str(target
.nid
));
1561 ibmsg
->ibm_u
.immediate
.ibim_hdr
= *hdr
;
1563 if (payload_kiov
!= NULL
)
1564 lnet_copy_kiov2flat(IBLND_MSG_SIZE
, ibmsg
,
1565 offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
),
1566 payload_niov
, payload_kiov
,
1567 payload_offset
, payload_nob
);
1569 lnet_copy_iov2flat(IBLND_MSG_SIZE
, ibmsg
,
1570 offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
),
1571 payload_niov
, payload_iov
,
1572 payload_offset
, payload_nob
);
1574 nob
= offsetof(kib_immediate_msg_t
, ibim_payload
[payload_nob
]);
1575 kiblnd_init_tx_msg(ni
, tx
, IBLND_MSG_IMMEDIATE
, nob
);
1577 tx
->tx_lntmsg
[0] = lntmsg
; /* finalise lntmsg on completion */
1578 kiblnd_launch_tx(ni
, tx
, target
.nid
);
1583 kiblnd_reply(lnet_ni_t
*ni
, kib_rx_t
*rx
, lnet_msg_t
*lntmsg
)
1585 lnet_process_id_t target
= lntmsg
->msg_target
;
1586 unsigned int niov
= lntmsg
->msg_niov
;
1587 struct kvec
*iov
= lntmsg
->msg_iov
;
1588 lnet_kiov_t
*kiov
= lntmsg
->msg_kiov
;
1589 unsigned int offset
= lntmsg
->msg_offset
;
1590 unsigned int nob
= lntmsg
->msg_len
;
1594 tx
= kiblnd_get_idle_tx(ni
, rx
->rx_conn
->ibc_peer
->ibp_nid
);
1596 CERROR("Can't get tx for REPLY to %s\n",
1597 libcfs_nid2str(target
.nid
));
1603 else if (kiov
== NULL
)
1604 rc
= kiblnd_setup_rd_iov(ni
, tx
, tx
->tx_rd
,
1605 niov
, iov
, offset
, nob
);
1607 rc
= kiblnd_setup_rd_kiov(ni
, tx
, tx
->tx_rd
,
1608 niov
, kiov
, offset
, nob
);
1611 CERROR("Can't setup GET src for %s: %d\n",
1612 libcfs_nid2str(target
.nid
), rc
);
1616 rc
= kiblnd_init_rdma(rx
->rx_conn
, tx
,
1617 IBLND_MSG_GET_DONE
, nob
,
1618 &rx
->rx_msg
->ibm_u
.get
.ibgm_rd
,
1619 rx
->rx_msg
->ibm_u
.get
.ibgm_cookie
);
1621 CERROR("Can't setup rdma for GET from %s: %d\n",
1622 libcfs_nid2str(target
.nid
), rc
);
1627 /* No RDMA: local completion may happen now! */
1628 lnet_finalize(ni
, lntmsg
, 0);
1630 /* RDMA: lnet_finalize(lntmsg) when it
1632 tx
->tx_lntmsg
[0] = lntmsg
;
1635 kiblnd_queue_tx(tx
, rx
->rx_conn
);
1639 kiblnd_tx_done(ni
, tx
);
1641 lnet_finalize(ni
, lntmsg
, -EIO
);
1645 kiblnd_recv(lnet_ni_t
*ni
, void *private, lnet_msg_t
*lntmsg
, int delayed
,
1646 unsigned int niov
, struct kvec
*iov
, lnet_kiov_t
*kiov
,
1647 unsigned int offset
, unsigned int mlen
, unsigned int rlen
)
1649 kib_rx_t
*rx
= private;
1650 kib_msg_t
*rxmsg
= rx
->rx_msg
;
1651 kib_conn_t
*conn
= rx
->rx_conn
;
1655 int post_credit
= IBLND_POSTRX_PEER_CREDIT
;
1658 LASSERT(mlen
<= rlen
);
1659 LASSERT(!in_interrupt());
1660 /* Either all pages or all vaddrs */
1661 LASSERT(!(kiov
!= NULL
&& iov
!= NULL
));
1663 switch (rxmsg
->ibm_type
) {
1667 case IBLND_MSG_IMMEDIATE
:
1668 nob
= offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
[rlen
]);
1669 if (nob
> rx
->rx_nob
) {
1670 CERROR("Immediate message from %s too big: %d(%d)\n",
1671 libcfs_nid2str(rxmsg
->ibm_u
.immediate
.ibim_hdr
.src_nid
),
1678 lnet_copy_flat2kiov(niov
, kiov
, offset
,
1679 IBLND_MSG_SIZE
, rxmsg
,
1680 offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
),
1683 lnet_copy_flat2iov(niov
, iov
, offset
,
1684 IBLND_MSG_SIZE
, rxmsg
,
1685 offsetof(kib_msg_t
, ibm_u
.immediate
.ibim_payload
),
1687 lnet_finalize(ni
, lntmsg
, 0);
1690 case IBLND_MSG_PUT_REQ
:
1692 lnet_finalize(ni
, lntmsg
, 0);
1693 kiblnd_send_completion(rx
->rx_conn
, IBLND_MSG_PUT_NAK
, 0,
1694 rxmsg
->ibm_u
.putreq
.ibprm_cookie
);
1698 tx
= kiblnd_get_idle_tx(ni
, conn
->ibc_peer
->ibp_nid
);
1700 CERROR("Can't allocate tx for %s\n",
1701 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
1702 /* Not replying will break the connection */
1709 rc
= kiblnd_setup_rd_iov(ni
, tx
,
1710 &txmsg
->ibm_u
.putack
.ibpam_rd
,
1711 niov
, iov
, offset
, mlen
);
1713 rc
= kiblnd_setup_rd_kiov(ni
, tx
,
1714 &txmsg
->ibm_u
.putack
.ibpam_rd
,
1715 niov
, kiov
, offset
, mlen
);
1717 CERROR("Can't setup PUT sink for %s: %d\n",
1718 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), rc
);
1719 kiblnd_tx_done(ni
, tx
);
1720 /* tell peer it's over */
1721 kiblnd_send_completion(rx
->rx_conn
, IBLND_MSG_PUT_NAK
, rc
,
1722 rxmsg
->ibm_u
.putreq
.ibprm_cookie
);
1726 nob
= offsetof(kib_putack_msg_t
, ibpam_rd
.rd_frags
[tx
->tx_nfrags
]);
1727 txmsg
->ibm_u
.putack
.ibpam_src_cookie
= rxmsg
->ibm_u
.putreq
.ibprm_cookie
;
1728 txmsg
->ibm_u
.putack
.ibpam_dst_cookie
= tx
->tx_cookie
;
1730 kiblnd_init_tx_msg(ni
, tx
, IBLND_MSG_PUT_ACK
, nob
);
1732 tx
->tx_lntmsg
[0] = lntmsg
; /* finalise lntmsg on completion */
1733 tx
->tx_waiting
= 1; /* waiting for PUT_DONE */
1734 kiblnd_queue_tx(tx
, conn
);
1736 /* reposted buffer reserved for PUT_DONE */
1737 post_credit
= IBLND_POSTRX_NO_CREDIT
;
1740 case IBLND_MSG_GET_REQ
:
1741 if (lntmsg
!= NULL
) {
1742 /* Optimized GET; RDMA lntmsg's payload */
1743 kiblnd_reply(ni
, rx
, lntmsg
);
1745 /* GET didn't match anything */
1746 kiblnd_send_completion(rx
->rx_conn
, IBLND_MSG_GET_DONE
,
1748 rxmsg
->ibm_u
.get
.ibgm_cookie
);
1753 kiblnd_post_rx(rx
, post_credit
);
1758 kiblnd_thread_start(int (*fn
)(void *arg
), void *arg
, char *name
)
1760 struct task_struct
*task
= kthread_run(fn
, arg
, "%s", name
);
1763 return PTR_ERR(task
);
1765 atomic_inc(&kiblnd_data
.kib_nthreads
);
1770 kiblnd_thread_fini(void)
1772 atomic_dec(&kiblnd_data
.kib_nthreads
);
1776 kiblnd_peer_alive(kib_peer_t
*peer
)
1778 /* This is racy, but everyone's only writing cfs_time_current() */
1779 peer
->ibp_last_alive
= cfs_time_current();
1784 kiblnd_peer_notify(kib_peer_t
*peer
)
1787 unsigned long last_alive
= 0;
1788 unsigned long flags
;
1790 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1792 if (list_empty(&peer
->ibp_conns
) &&
1793 peer
->ibp_accepting
== 0 &&
1794 peer
->ibp_connecting
== 0 &&
1795 peer
->ibp_error
!= 0) {
1796 error
= peer
->ibp_error
;
1797 peer
->ibp_error
= 0;
1799 last_alive
= peer
->ibp_last_alive
;
1802 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1805 lnet_notify(peer
->ibp_ni
,
1806 peer
->ibp_nid
, 0, last_alive
);
1810 kiblnd_close_conn_locked(kib_conn_t
*conn
, int error
)
1812 /* This just does the immediate housekeeping. 'error' is zero for a
1813 * normal shutdown which can happen only after the connection has been
1814 * established. If the connection is established, schedule the
1815 * connection to be finished off by the connd. Otherwise the connd is
1816 * already dealing with it (either to set it up or tear it down).
1817 * Caller holds kib_global_lock exclusively in irq context */
1818 kib_peer_t
*peer
= conn
->ibc_peer
;
1820 unsigned long flags
;
1822 LASSERT(error
!= 0 || conn
->ibc_state
>= IBLND_CONN_ESTABLISHED
);
1824 if (error
!= 0 && conn
->ibc_comms_error
== 0)
1825 conn
->ibc_comms_error
= error
;
1827 if (conn
->ibc_state
!= IBLND_CONN_ESTABLISHED
)
1828 return; /* already being handled */
1831 list_empty(&conn
->ibc_tx_noops
) &&
1832 list_empty(&conn
->ibc_tx_queue
) &&
1833 list_empty(&conn
->ibc_tx_queue_rsrvd
) &&
1834 list_empty(&conn
->ibc_tx_queue_nocred
) &&
1835 list_empty(&conn
->ibc_active_txs
)) {
1836 CDEBUG(D_NET
, "closing conn to %s\n",
1837 libcfs_nid2str(peer
->ibp_nid
));
1839 CNETERR("Closing conn to %s: error %d%s%s%s%s%s\n",
1840 libcfs_nid2str(peer
->ibp_nid
), error
,
1841 list_empty(&conn
->ibc_tx_queue
) ? "" : "(sending)",
1842 list_empty(&conn
->ibc_tx_noops
) ? "" : "(sending_noops)",
1843 list_empty(&conn
->ibc_tx_queue_rsrvd
) ? "" : "(sending_rsrvd)",
1844 list_empty(&conn
->ibc_tx_queue_nocred
) ? "" : "(sending_nocred)",
1845 list_empty(&conn
->ibc_active_txs
) ? "" : "(waiting)");
1848 dev
= ((kib_net_t
*)peer
->ibp_ni
->ni_data
)->ibn_dev
;
1849 list_del(&conn
->ibc_list
);
1850 /* connd (see below) takes over ibc_list's ref */
1852 if (list_empty(&peer
->ibp_conns
) && /* no more conns */
1853 kiblnd_peer_active(peer
)) { /* still in peer table */
1854 kiblnd_unlink_peer_locked(peer
);
1856 /* set/clear error on last conn */
1857 peer
->ibp_error
= conn
->ibc_comms_error
;
1860 kiblnd_set_conn_state(conn
, IBLND_CONN_CLOSING
);
1863 kiblnd_dev_can_failover(dev
)) {
1864 list_add_tail(&dev
->ibd_fail_list
,
1865 &kiblnd_data
.kib_failed_devs
);
1866 wake_up(&kiblnd_data
.kib_failover_waitq
);
1869 spin_lock_irqsave(&kiblnd_data
.kib_connd_lock
, flags
);
1871 list_add_tail(&conn
->ibc_list
, &kiblnd_data
.kib_connd_conns
);
1872 wake_up(&kiblnd_data
.kib_connd_waitq
);
1874 spin_unlock_irqrestore(&kiblnd_data
.kib_connd_lock
, flags
);
1878 kiblnd_close_conn(kib_conn_t
*conn
, int error
)
1880 unsigned long flags
;
1882 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1884 kiblnd_close_conn_locked(conn
, error
);
1886 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1890 kiblnd_handle_early_rxs(kib_conn_t
*conn
)
1892 unsigned long flags
;
1896 LASSERT(!in_interrupt());
1897 LASSERT(conn
->ibc_state
>= IBLND_CONN_ESTABLISHED
);
1899 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1900 list_for_each_entry_safe(rx
, tmp
, &conn
->ibc_early_rxs
, rx_list
) {
1901 list_del(&rx
->rx_list
);
1902 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1904 kiblnd_handle_rx(rx
);
1906 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1908 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
1912 kiblnd_abort_txs(kib_conn_t
*conn
, struct list_head
*txs
)
1915 struct list_head
*tmp
;
1916 struct list_head
*nxt
;
1919 spin_lock(&conn
->ibc_lock
);
1921 list_for_each_safe(tmp
, nxt
, txs
) {
1922 tx
= list_entry(tmp
, kib_tx_t
, tx_list
);
1924 if (txs
== &conn
->ibc_active_txs
) {
1925 LASSERT(!tx
->tx_queued
);
1926 LASSERT(tx
->tx_waiting
||
1927 tx
->tx_sending
!= 0);
1929 LASSERT(tx
->tx_queued
);
1932 tx
->tx_status
= -ECONNABORTED
;
1935 if (tx
->tx_sending
== 0) {
1937 list_del(&tx
->tx_list
);
1938 list_add(&tx
->tx_list
, &zombies
);
1942 spin_unlock(&conn
->ibc_lock
);
1944 kiblnd_txlist_done(conn
->ibc_peer
->ibp_ni
, &zombies
, -ECONNABORTED
);
1948 kiblnd_finalise_conn(kib_conn_t
*conn
)
1950 LASSERT(!in_interrupt());
1951 LASSERT(conn
->ibc_state
> IBLND_CONN_INIT
);
1953 kiblnd_set_conn_state(conn
, IBLND_CONN_DISCONNECTED
);
1955 /* abort_receives moves QP state to IB_QPS_ERR. This is only required
1956 * for connections that didn't get as far as being connected, because
1957 * rdma_disconnect() does this for free. */
1958 kiblnd_abort_receives(conn
);
1960 /* Complete all tx descs not waiting for sends to complete.
1961 * NB we should be safe from RDMA now that the QP has changed state */
1963 kiblnd_abort_txs(conn
, &conn
->ibc_tx_noops
);
1964 kiblnd_abort_txs(conn
, &conn
->ibc_tx_queue
);
1965 kiblnd_abort_txs(conn
, &conn
->ibc_tx_queue_rsrvd
);
1966 kiblnd_abort_txs(conn
, &conn
->ibc_tx_queue_nocred
);
1967 kiblnd_abort_txs(conn
, &conn
->ibc_active_txs
);
1969 kiblnd_handle_early_rxs(conn
);
1973 kiblnd_peer_connect_failed(kib_peer_t
*peer
, int active
, int error
)
1976 unsigned long flags
;
1978 LASSERT(error
!= 0);
1979 LASSERT(!in_interrupt());
1981 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
1984 LASSERT(peer
->ibp_connecting
> 0);
1985 peer
->ibp_connecting
--;
1987 LASSERT(peer
->ibp_accepting
> 0);
1988 peer
->ibp_accepting
--;
1991 if (peer
->ibp_connecting
!= 0 ||
1992 peer
->ibp_accepting
!= 0) {
1993 /* another connection attempt under way... */
1994 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
,
1999 if (list_empty(&peer
->ibp_conns
)) {
2000 /* Take peer's blocked transmits to complete with error */
2001 list_add(&zombies
, &peer
->ibp_tx_queue
);
2002 list_del_init(&peer
->ibp_tx_queue
);
2004 if (kiblnd_peer_active(peer
))
2005 kiblnd_unlink_peer_locked(peer
);
2007 peer
->ibp_error
= error
;
2009 /* Can't have blocked transmits if there are connections */
2010 LASSERT(list_empty(&peer
->ibp_tx_queue
));
2013 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2015 kiblnd_peer_notify(peer
);
2017 if (list_empty(&zombies
))
2020 CNETERR("Deleting messages for %s: connection failed\n",
2021 libcfs_nid2str(peer
->ibp_nid
));
2023 kiblnd_txlist_done(peer
->ibp_ni
, &zombies
, -EHOSTUNREACH
);
2027 kiblnd_connreq_done(kib_conn_t
*conn
, int status
)
2029 kib_peer_t
*peer
= conn
->ibc_peer
;
2032 struct list_head txs
;
2033 unsigned long flags
;
2036 active
= (conn
->ibc_state
== IBLND_CONN_ACTIVE_CONNECT
);
2038 CDEBUG(D_NET
, "%s: active(%d), version(%x), status(%d)\n",
2039 libcfs_nid2str(peer
->ibp_nid
), active
,
2040 conn
->ibc_version
, status
);
2042 LASSERT(!in_interrupt());
2043 LASSERT((conn
->ibc_state
== IBLND_CONN_ACTIVE_CONNECT
&&
2044 peer
->ibp_connecting
> 0) ||
2045 (conn
->ibc_state
== IBLND_CONN_PASSIVE_WAIT
&&
2046 peer
->ibp_accepting
> 0));
2048 LIBCFS_FREE(conn
->ibc_connvars
, sizeof(*conn
->ibc_connvars
));
2049 conn
->ibc_connvars
= NULL
;
2052 /* failed to establish connection */
2053 kiblnd_peer_connect_failed(peer
, active
, status
);
2054 kiblnd_finalise_conn(conn
);
2058 /* connection established */
2059 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2061 conn
->ibc_last_send
= jiffies
;
2062 kiblnd_set_conn_state(conn
, IBLND_CONN_ESTABLISHED
);
2063 kiblnd_peer_alive(peer
);
2065 /* Add conn to peer's list and nuke any dangling conns from a different
2066 * peer instance... */
2067 kiblnd_conn_addref(conn
); /* +1 ref for ibc_list */
2068 list_add(&conn
->ibc_list
, &peer
->ibp_conns
);
2070 peer
->ibp_connecting
--;
2072 peer
->ibp_accepting
--;
2074 if (peer
->ibp_version
== 0) {
2075 peer
->ibp_version
= conn
->ibc_version
;
2076 peer
->ibp_incarnation
= conn
->ibc_incarnation
;
2079 if (peer
->ibp_version
!= conn
->ibc_version
||
2080 peer
->ibp_incarnation
!= conn
->ibc_incarnation
) {
2081 kiblnd_close_stale_conns_locked(peer
, conn
->ibc_version
,
2082 conn
->ibc_incarnation
);
2083 peer
->ibp_version
= conn
->ibc_version
;
2084 peer
->ibp_incarnation
= conn
->ibc_incarnation
;
2087 /* grab pending txs while I have the lock */
2088 list_add(&txs
, &peer
->ibp_tx_queue
);
2089 list_del_init(&peer
->ibp_tx_queue
);
2091 if (!kiblnd_peer_active(peer
) || /* peer has been deleted */
2092 conn
->ibc_comms_error
!= 0) { /* error has happened already */
2093 lnet_ni_t
*ni
= peer
->ibp_ni
;
2095 /* start to shut down connection */
2096 kiblnd_close_conn_locked(conn
, -ECONNABORTED
);
2097 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2099 kiblnd_txlist_done(ni
, &txs
, -ECONNABORTED
);
2104 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2106 /* Schedule blocked txs */
2107 spin_lock(&conn
->ibc_lock
);
2108 list_for_each_entry_safe(tx
, tmp
, &txs
, tx_list
) {
2109 list_del(&tx
->tx_list
);
2111 kiblnd_queue_tx_locked(tx
, conn
);
2113 spin_unlock(&conn
->ibc_lock
);
2115 kiblnd_check_sends(conn
);
2117 /* schedule blocked rxs */
2118 kiblnd_handle_early_rxs(conn
);
2122 kiblnd_reject(struct rdma_cm_id
*cmid
, kib_rej_t
*rej
)
2126 rc
= rdma_reject(cmid
, rej
, sizeof(*rej
));
2129 CWARN("Error %d sending reject\n", rc
);
2133 kiblnd_passive_connect(struct rdma_cm_id
*cmid
, void *priv
, int priv_nob
)
2135 rwlock_t
*g_lock
= &kiblnd_data
.kib_global_lock
;
2136 kib_msg_t
*reqmsg
= priv
;
2142 lnet_ni_t
*ni
= NULL
;
2143 kib_net_t
*net
= NULL
;
2145 struct rdma_conn_param cp
;
2147 int version
= IBLND_MSG_VERSION
;
2148 unsigned long flags
;
2150 struct sockaddr_in
*peer_addr
;
2151 LASSERT(!in_interrupt());
2153 /* cmid inherits 'context' from the corresponding listener id */
2154 ibdev
= (kib_dev_t
*)cmid
->context
;
2155 LASSERT(ibdev
!= NULL
);
2157 memset(&rej
, 0, sizeof(rej
));
2158 rej
.ibr_magic
= IBLND_MSG_MAGIC
;
2159 rej
.ibr_why
= IBLND_REJECT_FATAL
;
2160 rej
.ibr_cp
.ibcp_max_msg_size
= IBLND_MSG_SIZE
;
2162 peer_addr
= (struct sockaddr_in
*)&(cmid
->route
.addr
.dst_addr
);
2163 if (*kiblnd_tunables
.kib_require_priv_port
&&
2164 ntohs(peer_addr
->sin_port
) >= PROT_SOCK
) {
2165 __u32 ip
= ntohl(peer_addr
->sin_addr
.s_addr
);
2166 CERROR("Peer's port (%pI4h:%hu) is not privileged\n",
2167 &ip
, ntohs(peer_addr
->sin_port
));
2171 if (priv_nob
< offsetof(kib_msg_t
, ibm_type
)) {
2172 CERROR("Short connection request\n");
2176 /* Future protocol version compatibility support! If the
2177 * o2iblnd-specific protocol changes, or when LNET unifies
2178 * protocols over all LNDs, the initial connection will
2179 * negotiate a protocol version. I trap this here to avoid
2180 * console errors; the reject tells the peer which protocol I
2182 if (reqmsg
->ibm_magic
== LNET_PROTO_MAGIC
||
2183 reqmsg
->ibm_magic
== __swab32(LNET_PROTO_MAGIC
))
2185 if (reqmsg
->ibm_magic
== IBLND_MSG_MAGIC
&&
2186 reqmsg
->ibm_version
!= IBLND_MSG_VERSION
&&
2187 reqmsg
->ibm_version
!= IBLND_MSG_VERSION_1
)
2189 if (reqmsg
->ibm_magic
== __swab32(IBLND_MSG_MAGIC
) &&
2190 reqmsg
->ibm_version
!= __swab16(IBLND_MSG_VERSION
) &&
2191 reqmsg
->ibm_version
!= __swab16(IBLND_MSG_VERSION_1
))
2194 rc
= kiblnd_unpack_msg(reqmsg
, priv_nob
);
2196 CERROR("Can't parse connection request: %d\n", rc
);
2200 nid
= reqmsg
->ibm_srcnid
;
2201 ni
= lnet_net2ni(LNET_NIDNET(reqmsg
->ibm_dstnid
));
2204 net
= (kib_net_t
*)ni
->ni_data
;
2205 rej
.ibr_incarnation
= net
->ibn_incarnation
;
2208 if (ni
== NULL
|| /* no matching net */
2209 ni
->ni_nid
!= reqmsg
->ibm_dstnid
|| /* right NET, wrong NID! */
2210 net
->ibn_dev
!= ibdev
) { /* wrong device */
2211 CERROR("Can't accept %s on %s (%s:%d:%pI4h): bad dst nid %s\n",
2212 libcfs_nid2str(nid
),
2213 ni
== NULL
? "NA" : libcfs_nid2str(ni
->ni_nid
),
2214 ibdev
->ibd_ifname
, ibdev
->ibd_nnets
,
2216 libcfs_nid2str(reqmsg
->ibm_dstnid
));
2221 /* check time stamp as soon as possible */
2222 if (reqmsg
->ibm_dststamp
!= 0 &&
2223 reqmsg
->ibm_dststamp
!= net
->ibn_incarnation
) {
2224 CWARN("Stale connection request\n");
2225 rej
.ibr_why
= IBLND_REJECT_CONN_STALE
;
2229 /* I can accept peer's version */
2230 version
= reqmsg
->ibm_version
;
2232 if (reqmsg
->ibm_type
!= IBLND_MSG_CONNREQ
) {
2233 CERROR("Unexpected connreq msg type: %x from %s\n",
2234 reqmsg
->ibm_type
, libcfs_nid2str(nid
));
2238 if (reqmsg
->ibm_u
.connparams
.ibcp_queue_depth
!=
2239 IBLND_MSG_QUEUE_SIZE(version
)) {
2240 CERROR("Can't accept %s: incompatible queue depth %d (%d wanted)\n",
2241 libcfs_nid2str(nid
), reqmsg
->ibm_u
.connparams
.ibcp_queue_depth
,
2242 IBLND_MSG_QUEUE_SIZE(version
));
2244 if (version
== IBLND_MSG_VERSION
)
2245 rej
.ibr_why
= IBLND_REJECT_MSG_QUEUE_SIZE
;
2250 if (reqmsg
->ibm_u
.connparams
.ibcp_max_frags
!=
2251 IBLND_RDMA_FRAGS(version
)) {
2252 CERROR("Can't accept %s(version %x): incompatible max_frags %d (%d wanted)\n",
2253 libcfs_nid2str(nid
), version
,
2254 reqmsg
->ibm_u
.connparams
.ibcp_max_frags
,
2255 IBLND_RDMA_FRAGS(version
));
2257 if (version
== IBLND_MSG_VERSION
)
2258 rej
.ibr_why
= IBLND_REJECT_RDMA_FRAGS
;
2264 if (reqmsg
->ibm_u
.connparams
.ibcp_max_msg_size
> IBLND_MSG_SIZE
) {
2265 CERROR("Can't accept %s: message size %d too big (%d max)\n",
2266 libcfs_nid2str(nid
),
2267 reqmsg
->ibm_u
.connparams
.ibcp_max_msg_size
,
2272 /* assume 'nid' is a new peer; create */
2273 rc
= kiblnd_create_peer(ni
, &peer
, nid
);
2275 CERROR("Can't create peer for %s\n", libcfs_nid2str(nid
));
2276 rej
.ibr_why
= IBLND_REJECT_NO_RESOURCES
;
2280 write_lock_irqsave(g_lock
, flags
);
2282 peer2
= kiblnd_find_peer_locked(nid
);
2283 if (peer2
!= NULL
) {
2284 if (peer2
->ibp_version
== 0) {
2285 peer2
->ibp_version
= version
;
2286 peer2
->ibp_incarnation
= reqmsg
->ibm_srcstamp
;
2289 /* not the guy I've talked with */
2290 if (peer2
->ibp_incarnation
!= reqmsg
->ibm_srcstamp
||
2291 peer2
->ibp_version
!= version
) {
2292 kiblnd_close_peer_conns_locked(peer2
, -ESTALE
);
2293 write_unlock_irqrestore(g_lock
, flags
);
2295 CWARN("Conn stale %s [old ver: %x, new ver: %x]\n",
2296 libcfs_nid2str(nid
), peer2
->ibp_version
, version
);
2298 kiblnd_peer_decref(peer
);
2299 rej
.ibr_why
= IBLND_REJECT_CONN_STALE
;
2303 /* tie-break connection race in favour of the higher NID */
2304 if (peer2
->ibp_connecting
!= 0 &&
2306 write_unlock_irqrestore(g_lock
, flags
);
2308 CWARN("Conn race %s\n", libcfs_nid2str(peer2
->ibp_nid
));
2310 kiblnd_peer_decref(peer
);
2311 rej
.ibr_why
= IBLND_REJECT_CONN_RACE
;
2315 peer2
->ibp_accepting
++;
2316 kiblnd_peer_addref(peer2
);
2318 write_unlock_irqrestore(g_lock
, flags
);
2319 kiblnd_peer_decref(peer
);
2322 /* Brand new peer */
2323 LASSERT(peer
->ibp_accepting
== 0);
2324 LASSERT(peer
->ibp_version
== 0 &&
2325 peer
->ibp_incarnation
== 0);
2327 peer
->ibp_accepting
= 1;
2328 peer
->ibp_version
= version
;
2329 peer
->ibp_incarnation
= reqmsg
->ibm_srcstamp
;
2331 /* I have a ref on ni that prevents it being shutdown */
2332 LASSERT(net
->ibn_shutdown
== 0);
2334 kiblnd_peer_addref(peer
);
2335 list_add_tail(&peer
->ibp_list
, kiblnd_nid2peerlist(nid
));
2337 write_unlock_irqrestore(g_lock
, flags
);
2340 conn
= kiblnd_create_conn(peer
, cmid
, IBLND_CONN_PASSIVE_WAIT
, version
);
2342 kiblnd_peer_connect_failed(peer
, 0, -ENOMEM
);
2343 kiblnd_peer_decref(peer
);
2344 rej
.ibr_why
= IBLND_REJECT_NO_RESOURCES
;
2348 /* conn now "owns" cmid, so I return success from here on to ensure the
2349 * CM callback doesn't destroy cmid. */
2351 conn
->ibc_incarnation
= reqmsg
->ibm_srcstamp
;
2352 conn
->ibc_credits
= IBLND_MSG_QUEUE_SIZE(version
);
2353 conn
->ibc_reserved_credits
= IBLND_MSG_QUEUE_SIZE(version
);
2354 LASSERT(conn
->ibc_credits
+ conn
->ibc_reserved_credits
+ IBLND_OOB_MSGS(version
)
2355 <= IBLND_RX_MSGS(version
));
2357 ackmsg
= &conn
->ibc_connvars
->cv_msg
;
2358 memset(ackmsg
, 0, sizeof(*ackmsg
));
2360 kiblnd_init_msg(ackmsg
, IBLND_MSG_CONNACK
,
2361 sizeof(ackmsg
->ibm_u
.connparams
));
2362 ackmsg
->ibm_u
.connparams
.ibcp_queue_depth
= IBLND_MSG_QUEUE_SIZE(version
);
2363 ackmsg
->ibm_u
.connparams
.ibcp_max_msg_size
= IBLND_MSG_SIZE
;
2364 ackmsg
->ibm_u
.connparams
.ibcp_max_frags
= IBLND_RDMA_FRAGS(version
);
2366 kiblnd_pack_msg(ni
, ackmsg
, version
, 0, nid
, reqmsg
->ibm_srcstamp
);
2368 memset(&cp
, 0, sizeof(cp
));
2369 cp
.private_data
= ackmsg
;
2370 cp
.private_data_len
= ackmsg
->ibm_nob
;
2371 cp
.responder_resources
= 0; /* No atomic ops or RDMA reads */
2372 cp
.initiator_depth
= 0;
2373 cp
.flow_control
= 1;
2374 cp
.retry_count
= *kiblnd_tunables
.kib_retry_count
;
2375 cp
.rnr_retry_count
= *kiblnd_tunables
.kib_rnr_retry_count
;
2377 CDEBUG(D_NET
, "Accept %s\n", libcfs_nid2str(nid
));
2379 rc
= rdma_accept(cmid
, &cp
);
2381 CERROR("Can't accept %s: %d\n", libcfs_nid2str(nid
), rc
);
2382 rej
.ibr_version
= version
;
2383 rej
.ibr_why
= IBLND_REJECT_FATAL
;
2385 kiblnd_reject(cmid
, &rej
);
2386 kiblnd_connreq_done(conn
, rc
);
2387 kiblnd_conn_decref(conn
);
2397 rej
.ibr_version
= version
;
2398 rej
.ibr_cp
.ibcp_queue_depth
= IBLND_MSG_QUEUE_SIZE(version
);
2399 rej
.ibr_cp
.ibcp_max_frags
= IBLND_RDMA_FRAGS(version
);
2400 kiblnd_reject(cmid
, &rej
);
2402 return -ECONNREFUSED
;
2406 kiblnd_reconnect(kib_conn_t
*conn
, int version
,
2407 __u64 incarnation
, int why
, kib_connparams_t
*cp
)
2409 kib_peer_t
*peer
= conn
->ibc_peer
;
2412 unsigned long flags
;
2414 LASSERT(conn
->ibc_state
== IBLND_CONN_ACTIVE_CONNECT
);
2415 LASSERT(peer
->ibp_connecting
> 0); /* 'conn' at least */
2417 write_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2419 /* retry connection if it's still needed and no other connection
2420 * attempts (active or passive) are in progress
2421 * NB: reconnect is still needed even when ibp_tx_queue is
2422 * empty if ibp_version != version because reconnect may be
2423 * initiated by kiblnd_query() */
2424 if ((!list_empty(&peer
->ibp_tx_queue
) ||
2425 peer
->ibp_version
!= version
) &&
2426 peer
->ibp_connecting
== 1 &&
2427 peer
->ibp_accepting
== 0) {
2429 peer
->ibp_connecting
++;
2431 peer
->ibp_version
= version
;
2432 peer
->ibp_incarnation
= incarnation
;
2435 write_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2445 case IBLND_REJECT_CONN_STALE
:
2449 case IBLND_REJECT_CONN_RACE
:
2450 reason
= "conn race";
2453 case IBLND_REJECT_CONN_UNCOMPAT
:
2454 reason
= "version negotiation";
2458 CNETERR("%s: retrying (%s), %x, %x, queue_dep: %d, max_frag: %d, msg_size: %d\n",
2459 libcfs_nid2str(peer
->ibp_nid
),
2460 reason
, IBLND_MSG_VERSION
, version
,
2461 cp
!= NULL
? cp
->ibcp_queue_depth
: IBLND_MSG_QUEUE_SIZE(version
),
2462 cp
!= NULL
? cp
->ibcp_max_frags
: IBLND_RDMA_FRAGS(version
),
2463 cp
!= NULL
? cp
->ibcp_max_msg_size
: IBLND_MSG_SIZE
);
2465 kiblnd_connect_peer(peer
);
2469 kiblnd_rejected(kib_conn_t
*conn
, int reason
, void *priv
, int priv_nob
)
2471 kib_peer_t
*peer
= conn
->ibc_peer
;
2473 LASSERT(!in_interrupt());
2474 LASSERT(conn
->ibc_state
== IBLND_CONN_ACTIVE_CONNECT
);
2477 case IB_CM_REJ_STALE_CONN
:
2478 kiblnd_reconnect(conn
, IBLND_MSG_VERSION
, 0,
2479 IBLND_REJECT_CONN_STALE
, NULL
);
2482 case IB_CM_REJ_INVALID_SERVICE_ID
:
2483 CNETERR("%s rejected: no listener at %d\n",
2484 libcfs_nid2str(peer
->ibp_nid
),
2485 *kiblnd_tunables
.kib_service
);
2488 case IB_CM_REJ_CONSUMER_DEFINED
:
2489 if (priv_nob
>= offsetof(kib_rej_t
, ibr_padding
)) {
2490 kib_rej_t
*rej
= priv
;
2491 kib_connparams_t
*cp
= NULL
;
2493 __u64 incarnation
= -1;
2495 /* NB. default incarnation is -1 because:
2496 * a) V1 will ignore dst incarnation in connreq.
2497 * b) V2 will provide incarnation while rejecting me,
2498 * -1 will be overwrote.
2500 * if I try to connect to a V1 peer with V2 protocol,
2501 * it rejected me then upgrade to V2, I have no idea
2502 * about the upgrading and try to reconnect with V1,
2503 * in this case upgraded V2 can find out I'm trying to
2504 * talk to the old guy and reject me(incarnation is -1).
2507 if (rej
->ibr_magic
== __swab32(IBLND_MSG_MAGIC
) ||
2508 rej
->ibr_magic
== __swab32(LNET_PROTO_MAGIC
)) {
2509 __swab32s(&rej
->ibr_magic
);
2510 __swab16s(&rej
->ibr_version
);
2514 if (priv_nob
>= sizeof(kib_rej_t
) &&
2515 rej
->ibr_version
> IBLND_MSG_VERSION_1
) {
2516 /* priv_nob is always 148 in current version
2517 * of OFED, so we still need to check version.
2518 * (define of IB_CM_REJ_PRIVATE_DATA_SIZE) */
2522 __swab64s(&rej
->ibr_incarnation
);
2523 __swab16s(&cp
->ibcp_queue_depth
);
2524 __swab16s(&cp
->ibcp_max_frags
);
2525 __swab32s(&cp
->ibcp_max_msg_size
);
2528 incarnation
= rej
->ibr_incarnation
;
2531 if (rej
->ibr_magic
!= IBLND_MSG_MAGIC
&&
2532 rej
->ibr_magic
!= LNET_PROTO_MAGIC
) {
2533 CERROR("%s rejected: consumer defined fatal error\n",
2534 libcfs_nid2str(peer
->ibp_nid
));
2538 if (rej
->ibr_version
!= IBLND_MSG_VERSION
&&
2539 rej
->ibr_version
!= IBLND_MSG_VERSION_1
) {
2540 CERROR("%s rejected: o2iblnd version %x error\n",
2541 libcfs_nid2str(peer
->ibp_nid
),
2546 if (rej
->ibr_why
== IBLND_REJECT_FATAL
&&
2547 rej
->ibr_version
== IBLND_MSG_VERSION_1
) {
2548 CDEBUG(D_NET
, "rejected by old version peer %s: %x\n",
2549 libcfs_nid2str(peer
->ibp_nid
), rej
->ibr_version
);
2551 if (conn
->ibc_version
!= IBLND_MSG_VERSION_1
)
2552 rej
->ibr_why
= IBLND_REJECT_CONN_UNCOMPAT
;
2555 switch (rej
->ibr_why
) {
2556 case IBLND_REJECT_CONN_RACE
:
2557 case IBLND_REJECT_CONN_STALE
:
2558 case IBLND_REJECT_CONN_UNCOMPAT
:
2559 kiblnd_reconnect(conn
, rej
->ibr_version
,
2560 incarnation
, rej
->ibr_why
, cp
);
2563 case IBLND_REJECT_MSG_QUEUE_SIZE
:
2564 CERROR("%s rejected: incompatible message queue depth %d, %d\n",
2565 libcfs_nid2str(peer
->ibp_nid
),
2566 cp
!= NULL
? cp
->ibcp_queue_depth
:
2567 IBLND_MSG_QUEUE_SIZE(rej
->ibr_version
),
2568 IBLND_MSG_QUEUE_SIZE(conn
->ibc_version
));
2571 case IBLND_REJECT_RDMA_FRAGS
:
2572 CERROR("%s rejected: incompatible # of RDMA fragments %d, %d\n",
2573 libcfs_nid2str(peer
->ibp_nid
),
2574 cp
!= NULL
? cp
->ibcp_max_frags
:
2575 IBLND_RDMA_FRAGS(rej
->ibr_version
),
2576 IBLND_RDMA_FRAGS(conn
->ibc_version
));
2579 case IBLND_REJECT_NO_RESOURCES
:
2580 CERROR("%s rejected: o2iblnd no resources\n",
2581 libcfs_nid2str(peer
->ibp_nid
));
2584 case IBLND_REJECT_FATAL
:
2585 CERROR("%s rejected: o2iblnd fatal error\n",
2586 libcfs_nid2str(peer
->ibp_nid
));
2590 CERROR("%s rejected: o2iblnd reason %d\n",
2591 libcfs_nid2str(peer
->ibp_nid
),
2599 CNETERR("%s rejected: reason %d, size %d\n",
2600 libcfs_nid2str(peer
->ibp_nid
), reason
, priv_nob
);
2604 kiblnd_connreq_done(conn
, -ECONNREFUSED
);
2608 kiblnd_check_connreply(kib_conn_t
*conn
, void *priv
, int priv_nob
)
2610 kib_peer_t
*peer
= conn
->ibc_peer
;
2611 lnet_ni_t
*ni
= peer
->ibp_ni
;
2612 kib_net_t
*net
= ni
->ni_data
;
2613 kib_msg_t
*msg
= priv
;
2614 int ver
= conn
->ibc_version
;
2615 int rc
= kiblnd_unpack_msg(msg
, priv_nob
);
2616 unsigned long flags
;
2618 LASSERT(net
!= NULL
);
2621 CERROR("Can't unpack connack from %s: %d\n",
2622 libcfs_nid2str(peer
->ibp_nid
), rc
);
2626 if (msg
->ibm_type
!= IBLND_MSG_CONNACK
) {
2627 CERROR("Unexpected message %d from %s\n",
2628 msg
->ibm_type
, libcfs_nid2str(peer
->ibp_nid
));
2633 if (ver
!= msg
->ibm_version
) {
2634 CERROR("%s replied version %x is different with requested version %x\n",
2635 libcfs_nid2str(peer
->ibp_nid
), msg
->ibm_version
, ver
);
2640 if (msg
->ibm_u
.connparams
.ibcp_queue_depth
!=
2641 IBLND_MSG_QUEUE_SIZE(ver
)) {
2642 CERROR("%s has incompatible queue depth %d(%d wanted)\n",
2643 libcfs_nid2str(peer
->ibp_nid
),
2644 msg
->ibm_u
.connparams
.ibcp_queue_depth
,
2645 IBLND_MSG_QUEUE_SIZE(ver
));
2650 if (msg
->ibm_u
.connparams
.ibcp_max_frags
!=
2651 IBLND_RDMA_FRAGS(ver
)) {
2652 CERROR("%s has incompatible max_frags %d (%d wanted)\n",
2653 libcfs_nid2str(peer
->ibp_nid
),
2654 msg
->ibm_u
.connparams
.ibcp_max_frags
,
2655 IBLND_RDMA_FRAGS(ver
));
2660 if (msg
->ibm_u
.connparams
.ibcp_max_msg_size
> IBLND_MSG_SIZE
) {
2661 CERROR("%s max message size %d too big (%d max)\n",
2662 libcfs_nid2str(peer
->ibp_nid
),
2663 msg
->ibm_u
.connparams
.ibcp_max_msg_size
,
2669 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2670 if (msg
->ibm_dstnid
== ni
->ni_nid
&&
2671 msg
->ibm_dststamp
== net
->ibn_incarnation
)
2675 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2678 CERROR("Bad connection reply from %s, rc = %d, version: %x max_frags: %d\n",
2679 libcfs_nid2str(peer
->ibp_nid
), rc
,
2680 msg
->ibm_version
, msg
->ibm_u
.connparams
.ibcp_max_frags
);
2684 conn
->ibc_incarnation
= msg
->ibm_srcstamp
;
2686 conn
->ibc_reserved_credits
= IBLND_MSG_QUEUE_SIZE(ver
);
2687 LASSERT(conn
->ibc_credits
+ conn
->ibc_reserved_credits
+ IBLND_OOB_MSGS(ver
)
2688 <= IBLND_RX_MSGS(ver
));
2690 kiblnd_connreq_done(conn
, 0);
2694 /* NB My QP has already established itself, so I handle anything going
2695 * wrong here by setting ibc_comms_error.
2696 * kiblnd_connreq_done(0) moves the conn state to ESTABLISHED, but then
2697 * immediately tears it down. */
2700 conn
->ibc_comms_error
= rc
;
2701 kiblnd_connreq_done(conn
, 0);
2705 kiblnd_active_connect(struct rdma_cm_id
*cmid
)
2707 kib_peer_t
*peer
= (kib_peer_t
*)cmid
->context
;
2710 struct rdma_conn_param cp
;
2713 unsigned long flags
;
2716 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2718 incarnation
= peer
->ibp_incarnation
;
2719 version
= (peer
->ibp_version
== 0) ? IBLND_MSG_VERSION
:
2722 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
2724 conn
= kiblnd_create_conn(peer
, cmid
, IBLND_CONN_ACTIVE_CONNECT
, version
);
2726 kiblnd_peer_connect_failed(peer
, 1, -ENOMEM
);
2727 kiblnd_peer_decref(peer
); /* lose cmid's ref */
2731 /* conn "owns" cmid now, so I return success from here on to ensure the
2732 * CM callback doesn't destroy cmid. conn also takes over cmid's ref
2735 msg
= &conn
->ibc_connvars
->cv_msg
;
2737 memset(msg
, 0, sizeof(*msg
));
2738 kiblnd_init_msg(msg
, IBLND_MSG_CONNREQ
, sizeof(msg
->ibm_u
.connparams
));
2739 msg
->ibm_u
.connparams
.ibcp_queue_depth
= IBLND_MSG_QUEUE_SIZE(version
);
2740 msg
->ibm_u
.connparams
.ibcp_max_frags
= IBLND_RDMA_FRAGS(version
);
2741 msg
->ibm_u
.connparams
.ibcp_max_msg_size
= IBLND_MSG_SIZE
;
2743 kiblnd_pack_msg(peer
->ibp_ni
, msg
, version
,
2744 0, peer
->ibp_nid
, incarnation
);
2746 memset(&cp
, 0, sizeof(cp
));
2747 cp
.private_data
= msg
;
2748 cp
.private_data_len
= msg
->ibm_nob
;
2749 cp
.responder_resources
= 0; /* No atomic ops or RDMA reads */
2750 cp
.initiator_depth
= 0;
2751 cp
.flow_control
= 1;
2752 cp
.retry_count
= *kiblnd_tunables
.kib_retry_count
;
2753 cp
.rnr_retry_count
= *kiblnd_tunables
.kib_rnr_retry_count
;
2755 LASSERT(cmid
->context
== (void *)conn
);
2756 LASSERT(conn
->ibc_cmid
== cmid
);
2758 rc
= rdma_connect(cmid
, &cp
);
2760 CERROR("Can't connect to %s: %d\n",
2761 libcfs_nid2str(peer
->ibp_nid
), rc
);
2762 kiblnd_connreq_done(conn
, rc
);
2763 kiblnd_conn_decref(conn
);
2770 kiblnd_cm_callback(struct rdma_cm_id
*cmid
, struct rdma_cm_event
*event
)
2776 switch (event
->event
) {
2778 CERROR("Unexpected event: %d, status: %d\n",
2779 event
->event
, event
->status
);
2782 case RDMA_CM_EVENT_CONNECT_REQUEST
:
2783 /* destroy cmid on failure */
2784 rc
= kiblnd_passive_connect(cmid
,
2785 (void *)KIBLND_CONN_PARAM(event
),
2786 KIBLND_CONN_PARAM_LEN(event
));
2787 CDEBUG(D_NET
, "connreq: %d\n", rc
);
2790 case RDMA_CM_EVENT_ADDR_ERROR
:
2791 peer
= (kib_peer_t
*)cmid
->context
;
2792 CNETERR("%s: ADDR ERROR %d\n",
2793 libcfs_nid2str(peer
->ibp_nid
), event
->status
);
2794 kiblnd_peer_connect_failed(peer
, 1, -EHOSTUNREACH
);
2795 kiblnd_peer_decref(peer
);
2796 return -EHOSTUNREACH
; /* rc != 0 destroys cmid */
2798 case RDMA_CM_EVENT_ADDR_RESOLVED
:
2799 peer
= (kib_peer_t
*)cmid
->context
;
2801 CDEBUG(D_NET
, "%s Addr resolved: %d\n",
2802 libcfs_nid2str(peer
->ibp_nid
), event
->status
);
2804 if (event
->status
!= 0) {
2805 CNETERR("Can't resolve address for %s: %d\n",
2806 libcfs_nid2str(peer
->ibp_nid
), event
->status
);
2809 rc
= rdma_resolve_route(
2810 cmid
, *kiblnd_tunables
.kib_timeout
* 1000);
2813 /* Can't initiate route resolution */
2814 CERROR("Can't resolve route for %s: %d\n",
2815 libcfs_nid2str(peer
->ibp_nid
), rc
);
2817 kiblnd_peer_connect_failed(peer
, 1, rc
);
2818 kiblnd_peer_decref(peer
);
2819 return rc
; /* rc != 0 destroys cmid */
2821 case RDMA_CM_EVENT_ROUTE_ERROR
:
2822 peer
= (kib_peer_t
*)cmid
->context
;
2823 CNETERR("%s: ROUTE ERROR %d\n",
2824 libcfs_nid2str(peer
->ibp_nid
), event
->status
);
2825 kiblnd_peer_connect_failed(peer
, 1, -EHOSTUNREACH
);
2826 kiblnd_peer_decref(peer
);
2827 return -EHOSTUNREACH
; /* rc != 0 destroys cmid */
2829 case RDMA_CM_EVENT_ROUTE_RESOLVED
:
2830 peer
= (kib_peer_t
*)cmid
->context
;
2831 CDEBUG(D_NET
, "%s Route resolved: %d\n",
2832 libcfs_nid2str(peer
->ibp_nid
), event
->status
);
2834 if (event
->status
== 0)
2835 return kiblnd_active_connect(cmid
);
2837 CNETERR("Can't resolve route for %s: %d\n",
2838 libcfs_nid2str(peer
->ibp_nid
), event
->status
);
2839 kiblnd_peer_connect_failed(peer
, 1, event
->status
);
2840 kiblnd_peer_decref(peer
);
2841 return event
->status
; /* rc != 0 destroys cmid */
2843 case RDMA_CM_EVENT_UNREACHABLE
:
2844 conn
= (kib_conn_t
*)cmid
->context
;
2845 LASSERT(conn
->ibc_state
== IBLND_CONN_ACTIVE_CONNECT
||
2846 conn
->ibc_state
== IBLND_CONN_PASSIVE_WAIT
);
2847 CNETERR("%s: UNREACHABLE %d\n",
2848 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), event
->status
);
2849 kiblnd_connreq_done(conn
, -ENETDOWN
);
2850 kiblnd_conn_decref(conn
);
2853 case RDMA_CM_EVENT_CONNECT_ERROR
:
2854 conn
= (kib_conn_t
*)cmid
->context
;
2855 LASSERT(conn
->ibc_state
== IBLND_CONN_ACTIVE_CONNECT
||
2856 conn
->ibc_state
== IBLND_CONN_PASSIVE_WAIT
);
2857 CNETERR("%s: CONNECT ERROR %d\n",
2858 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), event
->status
);
2859 kiblnd_connreq_done(conn
, -ENOTCONN
);
2860 kiblnd_conn_decref(conn
);
2863 case RDMA_CM_EVENT_REJECTED
:
2864 conn
= (kib_conn_t
*)cmid
->context
;
2865 switch (conn
->ibc_state
) {
2869 case IBLND_CONN_PASSIVE_WAIT
:
2870 CERROR("%s: REJECTED %d\n",
2871 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
),
2873 kiblnd_connreq_done(conn
, -ECONNRESET
);
2876 case IBLND_CONN_ACTIVE_CONNECT
:
2877 kiblnd_rejected(conn
, event
->status
,
2878 (void *)KIBLND_CONN_PARAM(event
),
2879 KIBLND_CONN_PARAM_LEN(event
));
2882 kiblnd_conn_decref(conn
);
2885 case RDMA_CM_EVENT_ESTABLISHED
:
2886 conn
= (kib_conn_t
*)cmid
->context
;
2887 switch (conn
->ibc_state
) {
2891 case IBLND_CONN_PASSIVE_WAIT
:
2892 CDEBUG(D_NET
, "ESTABLISHED (passive): %s\n",
2893 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
2894 kiblnd_connreq_done(conn
, 0);
2897 case IBLND_CONN_ACTIVE_CONNECT
:
2898 CDEBUG(D_NET
, "ESTABLISHED(active): %s\n",
2899 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
2900 kiblnd_check_connreply(conn
,
2901 (void *)KIBLND_CONN_PARAM(event
),
2902 KIBLND_CONN_PARAM_LEN(event
));
2905 /* net keeps its ref on conn! */
2908 case RDMA_CM_EVENT_TIMEWAIT_EXIT
:
2909 CDEBUG(D_NET
, "Ignore TIMEWAIT_EXIT event\n");
2911 case RDMA_CM_EVENT_DISCONNECTED
:
2912 conn
= (kib_conn_t
*)cmid
->context
;
2913 if (conn
->ibc_state
< IBLND_CONN_ESTABLISHED
) {
2914 CERROR("%s DISCONNECTED\n",
2915 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
2916 kiblnd_connreq_done(conn
, -ECONNRESET
);
2918 kiblnd_close_conn(conn
, 0);
2920 kiblnd_conn_decref(conn
);
2921 cmid
->context
= NULL
;
2924 case RDMA_CM_EVENT_DEVICE_REMOVAL
:
2925 LCONSOLE_ERROR_MSG(0x131,
2926 "Received notification of device removal\n"
2927 "Please shutdown LNET to allow this to proceed\n");
2928 /* Can't remove network from underneath LNET for now, so I have
2932 case RDMA_CM_EVENT_ADDR_CHANGE
:
2933 LCONSOLE_INFO("Physical link changed (eg hca/port)\n");
2939 kiblnd_check_txs_locked(kib_conn_t
*conn
, struct list_head
*txs
)
2942 struct list_head
*ttmp
;
2944 list_for_each(ttmp
, txs
) {
2945 tx
= list_entry(ttmp
, kib_tx_t
, tx_list
);
2947 if (txs
!= &conn
->ibc_active_txs
) {
2948 LASSERT(tx
->tx_queued
);
2950 LASSERT(!tx
->tx_queued
);
2951 LASSERT(tx
->tx_waiting
|| tx
->tx_sending
!= 0);
2954 if (cfs_time_aftereq(jiffies
, tx
->tx_deadline
)) {
2955 CERROR("Timed out tx: %s, %lu seconds\n",
2956 kiblnd_queue2str(conn
, txs
),
2957 cfs_duration_sec(jiffies
- tx
->tx_deadline
));
2966 kiblnd_conn_timed_out_locked(kib_conn_t
*conn
)
2968 return kiblnd_check_txs_locked(conn
, &conn
->ibc_tx_queue
) ||
2969 kiblnd_check_txs_locked(conn
, &conn
->ibc_tx_noops
) ||
2970 kiblnd_check_txs_locked(conn
, &conn
->ibc_tx_queue_rsrvd
) ||
2971 kiblnd_check_txs_locked(conn
, &conn
->ibc_tx_queue_nocred
) ||
2972 kiblnd_check_txs_locked(conn
, &conn
->ibc_active_txs
);
2976 kiblnd_check_conns(int idx
)
2979 LIST_HEAD(checksends
);
2980 struct list_head
*peers
= &kiblnd_data
.kib_peers
[idx
];
2981 struct list_head
*ptmp
;
2985 struct list_head
*ctmp
;
2986 unsigned long flags
;
2988 /* NB. We expect to have a look at all the peers and not find any
2989 * RDMAs to time out, so we just use a shared lock while we
2991 read_lock_irqsave(&kiblnd_data
.kib_global_lock
, flags
);
2993 list_for_each(ptmp
, peers
) {
2994 peer
= list_entry(ptmp
, kib_peer_t
, ibp_list
);
2996 list_for_each(ctmp
, &peer
->ibp_conns
) {
3000 conn
= list_entry(ctmp
, kib_conn_t
, ibc_list
);
3002 LASSERT(conn
->ibc_state
== IBLND_CONN_ESTABLISHED
);
3004 spin_lock(&conn
->ibc_lock
);
3006 sendnoop
= kiblnd_need_noop(conn
);
3007 timedout
= kiblnd_conn_timed_out_locked(conn
);
3008 if (!sendnoop
&& !timedout
) {
3009 spin_unlock(&conn
->ibc_lock
);
3014 CERROR("Timed out RDMA with %s (%lu): c: %u, oc: %u, rc: %u\n",
3015 libcfs_nid2str(peer
->ibp_nid
),
3016 cfs_duration_sec(cfs_time_current() -
3017 peer
->ibp_last_alive
),
3019 conn
->ibc_outstanding_credits
,
3020 conn
->ibc_reserved_credits
);
3021 list_add(&conn
->ibc_connd_list
, &closes
);
3023 list_add(&conn
->ibc_connd_list
,
3026 /* +ref for 'closes' or 'checksends' */
3027 kiblnd_conn_addref(conn
);
3029 spin_unlock(&conn
->ibc_lock
);
3033 read_unlock_irqrestore(&kiblnd_data
.kib_global_lock
, flags
);
3035 /* Handle timeout by closing the whole
3036 * connection. We can only be sure RDMA activity
3037 * has ceased once the QP has been modified. */
3038 list_for_each_entry_safe(conn
, tmp
, &closes
, ibc_connd_list
) {
3039 list_del(&conn
->ibc_connd_list
);
3040 kiblnd_close_conn(conn
, -ETIMEDOUT
);
3041 kiblnd_conn_decref(conn
);
3044 /* In case we have enough credits to return via a
3045 * NOOP, but there were no non-blocking tx descs
3046 * free to do it last time... */
3047 while (!list_empty(&checksends
)) {
3048 conn
= list_entry(checksends
.next
,
3049 kib_conn_t
, ibc_connd_list
);
3050 list_del(&conn
->ibc_connd_list
);
3051 kiblnd_check_sends(conn
);
3052 kiblnd_conn_decref(conn
);
3057 kiblnd_disconnect_conn(kib_conn_t
*conn
)
3059 LASSERT(!in_interrupt());
3060 LASSERT(current
== kiblnd_data
.kib_connd
);
3061 LASSERT(conn
->ibc_state
== IBLND_CONN_CLOSING
);
3063 rdma_disconnect(conn
->ibc_cmid
);
3064 kiblnd_finalise_conn(conn
);
3066 kiblnd_peer_notify(conn
->ibc_peer
);
3070 kiblnd_connd(void *arg
)
3073 unsigned long flags
;
3079 unsigned long deadline
= jiffies
;
3081 cfs_block_allsigs();
3083 init_waitqueue_entry(&wait
, current
);
3084 kiblnd_data
.kib_connd
= current
;
3086 spin_lock_irqsave(&kiblnd_data
.kib_connd_lock
, flags
);
3088 while (!kiblnd_data
.kib_shutdown
) {
3092 if (!list_empty(&kiblnd_data
.kib_connd_zombies
)) {
3093 conn
= list_entry(kiblnd_data
.kib_connd_zombies
.next
,
3094 kib_conn_t
, ibc_list
);
3095 list_del(&conn
->ibc_list
);
3097 spin_unlock_irqrestore(&kiblnd_data
.kib_connd_lock
,
3101 kiblnd_destroy_conn(conn
);
3103 spin_lock_irqsave(&kiblnd_data
.kib_connd_lock
, flags
);
3106 if (!list_empty(&kiblnd_data
.kib_connd_conns
)) {
3107 conn
= list_entry(kiblnd_data
.kib_connd_conns
.next
,
3108 kib_conn_t
, ibc_list
);
3109 list_del(&conn
->ibc_list
);
3111 spin_unlock_irqrestore(&kiblnd_data
.kib_connd_lock
,
3115 kiblnd_disconnect_conn(conn
);
3116 kiblnd_conn_decref(conn
);
3118 spin_lock_irqsave(&kiblnd_data
.kib_connd_lock
, flags
);
3121 /* careful with the jiffy wrap... */
3122 timeout
= (int)(deadline
- jiffies
);
3126 int chunk
= kiblnd_data
.kib_peer_hash_size
;
3128 spin_unlock_irqrestore(&kiblnd_data
.kib_connd_lock
, flags
);
3131 /* Time to check for RDMA timeouts on a few more
3132 * peers: I do checks every 'p' seconds on a
3133 * proportion of the peer table and I need to check
3134 * every connection 'n' times within a timeout
3135 * interval, to ensure I detect a timeout on any
3136 * connection within (n+1)/n times the timeout
3139 if (*kiblnd_tunables
.kib_timeout
> n
* p
)
3140 chunk
= (chunk
* n
* p
) /
3141 *kiblnd_tunables
.kib_timeout
;
3145 for (i
= 0; i
< chunk
; i
++) {
3146 kiblnd_check_conns(peer_index
);
3147 peer_index
= (peer_index
+ 1) %
3148 kiblnd_data
.kib_peer_hash_size
;
3152 spin_lock_irqsave(&kiblnd_data
.kib_connd_lock
, flags
);
3158 /* Nothing to do for 'timeout' */
3159 set_current_state(TASK_INTERRUPTIBLE
);
3160 add_wait_queue(&kiblnd_data
.kib_connd_waitq
, &wait
);
3161 spin_unlock_irqrestore(&kiblnd_data
.kib_connd_lock
, flags
);
3163 schedule_timeout(timeout
);
3165 remove_wait_queue(&kiblnd_data
.kib_connd_waitq
, &wait
);
3166 spin_lock_irqsave(&kiblnd_data
.kib_connd_lock
, flags
);
3169 spin_unlock_irqrestore(&kiblnd_data
.kib_connd_lock
, flags
);
3171 kiblnd_thread_fini();
3176 kiblnd_qp_event(struct ib_event
*event
, void *arg
)
3178 kib_conn_t
*conn
= arg
;
3180 switch (event
->event
) {
3181 case IB_EVENT_COMM_EST
:
3182 CDEBUG(D_NET
, "%s established\n",
3183 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
));
3187 CERROR("%s: Async QP event type %d\n",
3188 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), event
->event
);
3194 kiblnd_complete(struct ib_wc
*wc
)
3196 switch (kiblnd_wreqid2type(wc
->wr_id
)) {
3200 case IBLND_WID_RDMA
:
3201 /* We only get RDMA completion notification if it fails. All
3202 * subsequent work items, including the final SEND will fail
3203 * too. However we can't print out any more info about the
3204 * failing RDMA because 'tx' might be back on the idle list or
3205 * even reused already if we didn't manage to post all our work
3207 CNETERR("RDMA (tx: %p) failed: %d\n",
3208 kiblnd_wreqid2ptr(wc
->wr_id
), wc
->status
);
3212 kiblnd_tx_complete(kiblnd_wreqid2ptr(wc
->wr_id
), wc
->status
);
3216 kiblnd_rx_complete(kiblnd_wreqid2ptr(wc
->wr_id
), wc
->status
,
3223 kiblnd_cq_completion(struct ib_cq
*cq
, void *arg
)
3225 /* NB I'm not allowed to schedule this conn once its refcount has
3226 * reached 0. Since fundamentally I'm racing with scheduler threads
3227 * consuming my CQ I could be called after all completions have
3228 * occurred. But in this case, ibc_nrx == 0 && ibc_nsends_posted == 0
3229 * and this CQ is about to be destroyed so I NOOP. */
3230 kib_conn_t
*conn
= (kib_conn_t
*)arg
;
3231 struct kib_sched_info
*sched
= conn
->ibc_sched
;
3232 unsigned long flags
;
3234 LASSERT(cq
== conn
->ibc_cq
);
3236 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
3238 conn
->ibc_ready
= 1;
3240 if (!conn
->ibc_scheduled
&&
3241 (conn
->ibc_nrx
> 0 ||
3242 conn
->ibc_nsends_posted
> 0)) {
3243 kiblnd_conn_addref(conn
); /* +1 ref for sched_conns */
3244 conn
->ibc_scheduled
= 1;
3245 list_add_tail(&conn
->ibc_sched_list
, &sched
->ibs_conns
);
3247 if (waitqueue_active(&sched
->ibs_waitq
))
3248 wake_up(&sched
->ibs_waitq
);
3251 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
3255 kiblnd_cq_event(struct ib_event
*event
, void *arg
)
3257 kib_conn_t
*conn
= arg
;
3259 CERROR("%s: async CQ event type %d\n",
3260 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), event
->event
);
3264 kiblnd_scheduler(void *arg
)
3266 long id
= (long)arg
;
3267 struct kib_sched_info
*sched
;
3270 unsigned long flags
;
3276 cfs_block_allsigs();
3278 init_waitqueue_entry(&wait
, current
);
3280 sched
= kiblnd_data
.kib_scheds
[KIB_THREAD_CPT(id
)];
3282 rc
= cfs_cpt_bind(lnet_cpt_table(), sched
->ibs_cpt
);
3284 CWARN("Failed to bind on CPT %d, please verify whether all CPUs are healthy and reload modules if necessary, otherwise your system might under risk of low performance\n",
3288 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
3290 while (!kiblnd_data
.kib_shutdown
) {
3291 if (busy_loops
++ >= IBLND_RESCHED
) {
3292 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
3297 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
3302 if (!list_empty(&sched
->ibs_conns
)) {
3303 conn
= list_entry(sched
->ibs_conns
.next
,
3304 kib_conn_t
, ibc_sched_list
);
3305 /* take over kib_sched_conns' ref on conn... */
3306 LASSERT(conn
->ibc_scheduled
);
3307 list_del(&conn
->ibc_sched_list
);
3308 conn
->ibc_ready
= 0;
3310 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
3312 rc
= ib_poll_cq(conn
->ibc_cq
, 1, &wc
);
3314 rc
= ib_req_notify_cq(conn
->ibc_cq
,
3317 CWARN("%s: ib_req_notify_cq failed: %d, closing connection\n",
3318 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
), rc
);
3319 kiblnd_close_conn(conn
, -EIO
);
3320 kiblnd_conn_decref(conn
);
3321 spin_lock_irqsave(&sched
->ibs_lock
,
3326 rc
= ib_poll_cq(conn
->ibc_cq
, 1, &wc
);
3330 CWARN("%s: ib_poll_cq failed: %d, closing connection\n",
3331 libcfs_nid2str(conn
->ibc_peer
->ibp_nid
),
3333 kiblnd_close_conn(conn
, -EIO
);
3334 kiblnd_conn_decref(conn
);
3335 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
3339 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
3341 if (rc
!= 0 || conn
->ibc_ready
) {
3342 /* There may be another completion waiting; get
3343 * another scheduler to check while I handle
3345 /* +1 ref for sched_conns */
3346 kiblnd_conn_addref(conn
);
3347 list_add_tail(&conn
->ibc_sched_list
,
3349 if (waitqueue_active(&sched
->ibs_waitq
))
3350 wake_up(&sched
->ibs_waitq
);
3352 conn
->ibc_scheduled
= 0;
3356 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
3357 kiblnd_complete(&wc
);
3359 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
3362 kiblnd_conn_decref(conn
); /* ...drop my ref from above */
3369 set_current_state(TASK_INTERRUPTIBLE
);
3370 add_wait_queue_exclusive(&sched
->ibs_waitq
, &wait
);
3371 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
3376 remove_wait_queue(&sched
->ibs_waitq
, &wait
);
3377 spin_lock_irqsave(&sched
->ibs_lock
, flags
);
3380 spin_unlock_irqrestore(&sched
->ibs_lock
, flags
);
3382 kiblnd_thread_fini();
3387 kiblnd_failover_thread(void *arg
)
3389 rwlock_t
*glock
= &kiblnd_data
.kib_global_lock
;
3392 unsigned long flags
;
3395 LASSERT(*kiblnd_tunables
.kib_dev_failover
!= 0);
3397 cfs_block_allsigs();
3399 init_waitqueue_entry(&wait
, current
);
3400 write_lock_irqsave(glock
, flags
);
3402 while (!kiblnd_data
.kib_shutdown
) {
3403 int do_failover
= 0;
3406 list_for_each_entry(dev
, &kiblnd_data
.kib_failed_devs
,
3408 if (time_before(cfs_time_current(),
3409 dev
->ibd_next_failover
))
3416 list_del_init(&dev
->ibd_fail_list
);
3417 dev
->ibd_failover
= 1;
3418 write_unlock_irqrestore(glock
, flags
);
3420 rc
= kiblnd_dev_failover(dev
);
3422 write_lock_irqsave(glock
, flags
);
3424 LASSERT(dev
->ibd_failover
);
3425 dev
->ibd_failover
= 0;
3426 if (rc
>= 0) { /* Device is OK or failover succeed */
3427 dev
->ibd_next_failover
= cfs_time_shift(3);
3431 /* failed to failover, retry later */
3432 dev
->ibd_next_failover
=
3433 cfs_time_shift(min(dev
->ibd_failed_failover
, 10));
3434 if (kiblnd_dev_can_failover(dev
)) {
3435 list_add_tail(&dev
->ibd_fail_list
,
3436 &kiblnd_data
.kib_failed_devs
);
3442 /* long sleep if no more pending failover */
3443 long_sleep
= list_empty(&kiblnd_data
.kib_failed_devs
);
3445 set_current_state(TASK_INTERRUPTIBLE
);
3446 add_wait_queue(&kiblnd_data
.kib_failover_waitq
, &wait
);
3447 write_unlock_irqrestore(glock
, flags
);
3449 rc
= schedule_timeout(long_sleep
? cfs_time_seconds(10) :
3450 cfs_time_seconds(1));
3451 remove_wait_queue(&kiblnd_data
.kib_failover_waitq
, &wait
);
3452 write_lock_irqsave(glock
, flags
);
3454 if (!long_sleep
|| rc
!= 0)
3457 /* have a long sleep, routine check all active devices,
3458 * we need checking like this because if there is not active
3459 * connection on the dev and no SEND from local, we may listen
3460 * on wrong HCA for ever while there is a bonding failover */
3461 list_for_each_entry(dev
, &kiblnd_data
.kib_devs
, ibd_list
) {
3462 if (kiblnd_dev_can_failover(dev
)) {
3463 list_add_tail(&dev
->ibd_fail_list
,
3464 &kiblnd_data
.kib_failed_devs
);
3469 write_unlock_irqrestore(glock
, flags
);
3471 kiblnd_thread_fini();