2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2012, Intel Corporation.
6 * Author: Zach Brown <zab@zabbo.net>
7 * Author: Peter J. Braam <braam@clusterfs.com>
8 * Author: Phil Schwan <phil@clusterfs.com>
9 * Author: Eric Barton <eric@bartonsoftware.com>
11 * This file is part of Portals, http://www.sf.net/projects/sandiaportals/
13 * Portals is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * Portals is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Portals; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 ksocknal_alloc_tx(int type
, int size
)
32 ksock_tx_t
*tx
= NULL
;
34 if (type
== KSOCK_MSG_NOOP
) {
35 LASSERT(size
== KSOCK_NOOP_TX_SIZE
);
37 /* searching for a noop tx in free list */
38 spin_lock(&ksocknal_data
.ksnd_tx_lock
);
40 if (!list_empty(&ksocknal_data
.ksnd_idle_noop_txs
)) {
41 tx
= list_entry(ksocknal_data
.ksnd_idle_noop_txs
. \
42 next
, ksock_tx_t
, tx_list
);
43 LASSERT(tx
->tx_desc_size
== size
);
44 list_del(&tx
->tx_list
);
47 spin_unlock(&ksocknal_data
.ksnd_tx_lock
);
51 LIBCFS_ALLOC(tx
, size
);
56 atomic_set(&tx
->tx_refcount
, 1);
57 tx
->tx_zc_aborted
= 0;
58 tx
->tx_zc_capable
= 0;
59 tx
->tx_zc_checked
= 0;
60 tx
->tx_desc_size
= size
;
62 atomic_inc(&ksocknal_data
.ksnd_nactive_txs
);
68 ksocknal_alloc_tx_noop(__u64 cookie
, int nonblk
)
72 tx
= ksocknal_alloc_tx(KSOCK_MSG_NOOP
, KSOCK_NOOP_TX_SIZE
);
74 CERROR("Can't allocate noop tx desc\n");
79 tx
->tx_lnetmsg
= NULL
;
82 tx
->tx_iov
= tx
->tx_frags
.virt
.iov
;
84 tx
->tx_nonblk
= nonblk
;
86 socklnd_init_msg(&tx
->tx_msg
, KSOCK_MSG_NOOP
);
87 tx
->tx_msg
.ksm_zc_cookies
[1] = cookie
;
94 ksocknal_free_tx (ksock_tx_t
*tx
)
96 atomic_dec(&ksocknal_data
.ksnd_nactive_txs
);
98 if (tx
->tx_lnetmsg
== NULL
&& tx
->tx_desc_size
== KSOCK_NOOP_TX_SIZE
) {
100 spin_lock(&ksocknal_data
.ksnd_tx_lock
);
102 list_add(&tx
->tx_list
, &ksocknal_data
.ksnd_idle_noop_txs
);
104 spin_unlock(&ksocknal_data
.ksnd_tx_lock
);
106 LIBCFS_FREE(tx
, tx
->tx_desc_size
);
111 ksocknal_send_iov (ksock_conn_t
*conn
, ksock_tx_t
*tx
)
113 struct iovec
*iov
= tx
->tx_iov
;
117 LASSERT (tx
->tx_niov
> 0);
119 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
120 rc
= ksocknal_lib_send_iov(conn
, tx
);
122 if (rc
<= 0) /* sent nothing? */
126 LASSERT (nob
<= tx
->tx_resid
);
131 LASSERT (tx
->tx_niov
> 0);
133 if (nob
< (int) iov
->iov_len
) {
134 iov
->iov_base
= (void *)((char *)iov
->iov_base
+ nob
);
148 ksocknal_send_kiov (ksock_conn_t
*conn
, ksock_tx_t
*tx
)
150 lnet_kiov_t
*kiov
= tx
->tx_kiov
;
154 LASSERT (tx
->tx_niov
== 0);
155 LASSERT (tx
->tx_nkiov
> 0);
157 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
158 rc
= ksocknal_lib_send_kiov(conn
, tx
);
160 if (rc
<= 0) /* sent nothing? */
164 LASSERT (nob
<= tx
->tx_resid
);
169 LASSERT(tx
->tx_nkiov
> 0);
171 if (nob
< (int)kiov
->kiov_len
) {
172 kiov
->kiov_offset
+= nob
;
173 kiov
->kiov_len
-= nob
;
177 nob
-= (int)kiov
->kiov_len
;
178 tx
->tx_kiov
= ++kiov
;
186 ksocknal_transmit (ksock_conn_t
*conn
, ksock_tx_t
*tx
)
191 if (ksocknal_data
.ksnd_stall_tx
!= 0) {
192 set_current_state(TASK_UNINTERRUPTIBLE
);
193 schedule_timeout(cfs_time_seconds(ksocknal_data
.ksnd_stall_tx
));
196 LASSERT (tx
->tx_resid
!= 0);
198 rc
= ksocknal_connsock_addref(conn
);
200 LASSERT (conn
->ksnc_closing
);
205 if (ksocknal_data
.ksnd_enomem_tx
> 0) {
207 ksocknal_data
.ksnd_enomem_tx
--;
209 } else if (tx
->tx_niov
!= 0) {
210 rc
= ksocknal_send_iov (conn
, tx
);
212 rc
= ksocknal_send_kiov (conn
, tx
);
215 bufnob
= conn
->ksnc_sock
->sk
->sk_wmem_queued
;
216 if (rc
> 0) /* sent something? */
217 conn
->ksnc_tx_bufnob
+= rc
; /* account it */
219 if (bufnob
< conn
->ksnc_tx_bufnob
) {
220 /* allocated send buffer bytes < computed; infer
221 * something got ACKed */
222 conn
->ksnc_tx_deadline
=
223 cfs_time_shift(*ksocknal_tunables
.ksnd_timeout
);
224 conn
->ksnc_peer
->ksnp_last_alive
= cfs_time_current();
225 conn
->ksnc_tx_bufnob
= bufnob
;
229 if (rc
<= 0) { /* Didn't write anything? */
231 if (rc
== 0) /* some stacks return 0 instead of -EAGAIN */
234 /* Check if EAGAIN is due to memory pressure */
235 if (rc
== -EAGAIN
&& ksocknal_lib_memory_pressure(conn
))
241 /* socket's wmem_queued now includes 'rc' bytes */
242 atomic_sub (rc
, &conn
->ksnc_tx_nob
);
245 } while (tx
->tx_resid
!= 0);
247 ksocknal_connsock_decref(conn
);
252 ksocknal_recv_iov (ksock_conn_t
*conn
)
254 struct iovec
*iov
= conn
->ksnc_rx_iov
;
258 LASSERT (conn
->ksnc_rx_niov
> 0);
260 /* Never touch conn->ksnc_rx_iov or change connection
261 * status inside ksocknal_lib_recv_iov */
262 rc
= ksocknal_lib_recv_iov(conn
);
267 /* received something... */
270 conn
->ksnc_peer
->ksnp_last_alive
= cfs_time_current();
271 conn
->ksnc_rx_deadline
=
272 cfs_time_shift(*ksocknal_tunables
.ksnd_timeout
);
273 mb(); /* order with setting rx_started */
274 conn
->ksnc_rx_started
= 1;
276 conn
->ksnc_rx_nob_wanted
-= nob
;
277 conn
->ksnc_rx_nob_left
-= nob
;
280 LASSERT (conn
->ksnc_rx_niov
> 0);
282 if (nob
< (int)iov
->iov_len
) {
284 iov
->iov_base
= (void *)((char *)iov
->iov_base
+ nob
);
289 conn
->ksnc_rx_iov
= ++iov
;
290 conn
->ksnc_rx_niov
--;
297 ksocknal_recv_kiov (ksock_conn_t
*conn
)
299 lnet_kiov_t
*kiov
= conn
->ksnc_rx_kiov
;
302 LASSERT (conn
->ksnc_rx_nkiov
> 0);
304 /* Never touch conn->ksnc_rx_kiov or change connection
305 * status inside ksocknal_lib_recv_iov */
306 rc
= ksocknal_lib_recv_kiov(conn
);
311 /* received something... */
314 conn
->ksnc_peer
->ksnp_last_alive
= cfs_time_current();
315 conn
->ksnc_rx_deadline
=
316 cfs_time_shift(*ksocknal_tunables
.ksnd_timeout
);
317 mb(); /* order with setting rx_started */
318 conn
->ksnc_rx_started
= 1;
320 conn
->ksnc_rx_nob_wanted
-= nob
;
321 conn
->ksnc_rx_nob_left
-= nob
;
324 LASSERT (conn
->ksnc_rx_nkiov
> 0);
326 if (nob
< (int) kiov
->kiov_len
) {
327 kiov
->kiov_offset
+= nob
;
328 kiov
->kiov_len
-= nob
;
332 nob
-= kiov
->kiov_len
;
333 conn
->ksnc_rx_kiov
= ++kiov
;
334 conn
->ksnc_rx_nkiov
--;
341 ksocknal_receive (ksock_conn_t
*conn
)
343 /* Return 1 on success, 0 on EOF, < 0 on error.
344 * Caller checks ksnc_rx_nob_wanted to determine
345 * progress/completion. */
348 if (ksocknal_data
.ksnd_stall_rx
!= 0) {
349 set_current_state(TASK_UNINTERRUPTIBLE
);
350 schedule_timeout(cfs_time_seconds(ksocknal_data
.ksnd_stall_rx
));
353 rc
= ksocknal_connsock_addref(conn
);
355 LASSERT (conn
->ksnc_closing
);
360 if (conn
->ksnc_rx_niov
!= 0)
361 rc
= ksocknal_recv_iov (conn
);
363 rc
= ksocknal_recv_kiov (conn
);
366 /* error/EOF or partial receive */
369 } else if (rc
== 0 && conn
->ksnc_rx_started
) {
370 /* EOF in the middle of a message */
376 /* Completed a fragment */
378 if (conn
->ksnc_rx_nob_wanted
== 0) {
384 ksocknal_connsock_decref(conn
);
389 ksocknal_tx_done (lnet_ni_t
*ni
, ksock_tx_t
*tx
)
391 lnet_msg_t
*lnetmsg
= tx
->tx_lnetmsg
;
392 int rc
= (tx
->tx_resid
== 0 && !tx
->tx_zc_aborted
) ? 0 : -EIO
;
394 LASSERT(ni
!= NULL
|| tx
->tx_conn
!= NULL
);
396 if (tx
->tx_conn
!= NULL
)
397 ksocknal_conn_decref(tx
->tx_conn
);
399 if (ni
== NULL
&& tx
->tx_conn
!= NULL
)
400 ni
= tx
->tx_conn
->ksnc_peer
->ksnp_ni
;
402 ksocknal_free_tx (tx
);
403 if (lnetmsg
!= NULL
) /* KSOCK_MSG_NOOP go without lnetmsg */
404 lnet_finalize (ni
, lnetmsg
, rc
);
408 ksocknal_txlist_done (lnet_ni_t
*ni
, struct list_head
*txlist
, int error
)
412 while (!list_empty (txlist
)) {
413 tx
= list_entry (txlist
->next
, ksock_tx_t
, tx_list
);
415 if (error
&& tx
->tx_lnetmsg
!= NULL
) {
416 CNETERR("Deleting packet type %d len %d %s->%s\n",
417 le32_to_cpu (tx
->tx_lnetmsg
->msg_hdr
.type
),
418 le32_to_cpu (tx
->tx_lnetmsg
->msg_hdr
.payload_length
),
419 libcfs_nid2str(le64_to_cpu(tx
->tx_lnetmsg
->msg_hdr
.src_nid
)),
420 libcfs_nid2str(le64_to_cpu(tx
->tx_lnetmsg
->msg_hdr
.dest_nid
)));
422 CNETERR("Deleting noop packet\n");
425 list_del (&tx
->tx_list
);
427 LASSERT (atomic_read(&tx
->tx_refcount
) == 1);
428 ksocknal_tx_done (ni
, tx
);
433 ksocknal_check_zc_req(ksock_tx_t
*tx
)
435 ksock_conn_t
*conn
= tx
->tx_conn
;
436 ksock_peer_t
*peer
= conn
->ksnc_peer
;
438 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
439 * to ksnp_zc_req_list if some fragment of this message should be sent
440 * zero-copy. Our peer will send an ACK containing this cookie when
441 * she has received this message to tell us we can signal completion.
442 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
443 * ksnp_zc_req_list. */
444 LASSERT (tx
->tx_msg
.ksm_type
!= KSOCK_MSG_NOOP
);
445 LASSERT (tx
->tx_zc_capable
);
447 tx
->tx_zc_checked
= 1;
449 if (conn
->ksnc_proto
== &ksocknal_protocol_v1x
||
450 !conn
->ksnc_zc_capable
)
453 /* assign cookie and queue tx to pending list, it will be released when
454 * a matching ack is received. See ksocknal_handle_zcack() */
456 ksocknal_tx_addref(tx
);
458 spin_lock(&peer
->ksnp_lock
);
460 /* ZC_REQ is going to be pinned to the peer */
462 cfs_time_shift(*ksocknal_tunables
.ksnd_timeout
);
464 LASSERT (tx
->tx_msg
.ksm_zc_cookies
[0] == 0);
466 tx
->tx_msg
.ksm_zc_cookies
[0] = peer
->ksnp_zc_next_cookie
++;
468 if (peer
->ksnp_zc_next_cookie
== 0)
469 peer
->ksnp_zc_next_cookie
= SOCKNAL_KEEPALIVE_PING
+ 1;
471 list_add_tail(&tx
->tx_zc_list
, &peer
->ksnp_zc_req_list
);
473 spin_unlock(&peer
->ksnp_lock
);
477 ksocknal_uncheck_zc_req(ksock_tx_t
*tx
)
479 ksock_peer_t
*peer
= tx
->tx_conn
->ksnc_peer
;
481 LASSERT(tx
->tx_msg
.ksm_type
!= KSOCK_MSG_NOOP
);
482 LASSERT(tx
->tx_zc_capable
);
484 tx
->tx_zc_checked
= 0;
486 spin_lock(&peer
->ksnp_lock
);
488 if (tx
->tx_msg
.ksm_zc_cookies
[0] == 0) {
489 /* Not waiting for an ACK */
490 spin_unlock(&peer
->ksnp_lock
);
494 tx
->tx_msg
.ksm_zc_cookies
[0] = 0;
495 list_del(&tx
->tx_zc_list
);
497 spin_unlock(&peer
->ksnp_lock
);
499 ksocknal_tx_decref(tx
);
503 ksocknal_process_transmit (ksock_conn_t
*conn
, ksock_tx_t
*tx
)
507 if (tx
->tx_zc_capable
&& !tx
->tx_zc_checked
)
508 ksocknal_check_zc_req(tx
);
510 rc
= ksocknal_transmit (conn
, tx
);
512 CDEBUG (D_NET
, "send(%d) %d\n", tx
->tx_resid
, rc
);
514 if (tx
->tx_resid
== 0) {
515 /* Sent everything OK */
527 counter
++; /* exponential backoff warnings */
528 if ((counter
& (-counter
)) == counter
)
529 CWARN("%u ENOMEM tx %p (%u allocated)\n",
530 counter
, conn
, atomic_read(&libcfs_kmemory
));
532 /* Queue on ksnd_enomem_conns for retry after a timeout */
533 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
535 /* enomem list takes over scheduler's ref... */
536 LASSERT (conn
->ksnc_tx_scheduled
);
537 list_add_tail(&conn
->ksnc_tx_list
,
538 &ksocknal_data
.ksnd_enomem_conns
);
539 if (!cfs_time_aftereq(cfs_time_add(cfs_time_current(),
540 SOCKNAL_ENOMEM_RETRY
),
541 ksocknal_data
.ksnd_reaper_waketime
))
542 wake_up (&ksocknal_data
.ksnd_reaper_waitq
);
544 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
551 if (!conn
->ksnc_closing
) {
554 LCONSOLE_WARN("Host %pI4h reset our connection "
555 "while we were sending data; it may have "
560 LCONSOLE_WARN("There was an unexpected network error "
561 "while writing to %pI4h: %d.\n",
562 &conn
->ksnc_ipaddr
, rc
);
565 CDEBUG(D_NET
, "[%p] Error %d on write to %s"
566 " ip %pI4h:%d\n", conn
, rc
,
567 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
572 if (tx
->tx_zc_checked
)
573 ksocknal_uncheck_zc_req(tx
);
575 /* it's not an error if conn is being closed */
576 ksocknal_close_conn_and_siblings (conn
,
577 (conn
->ksnc_closing
) ? 0 : rc
);
583 ksocknal_launch_connection_locked (ksock_route_t
*route
)
586 /* called holding write lock on ksnd_global_lock */
588 LASSERT (!route
->ksnr_scheduled
);
589 LASSERT (!route
->ksnr_connecting
);
590 LASSERT ((ksocknal_route_mask() & ~route
->ksnr_connected
) != 0);
592 route
->ksnr_scheduled
= 1; /* scheduling conn for connd */
593 ksocknal_route_addref(route
); /* extra ref for connd */
595 spin_lock_bh(&ksocknal_data
.ksnd_connd_lock
);
597 list_add_tail(&route
->ksnr_connd_list
,
598 &ksocknal_data
.ksnd_connd_routes
);
599 wake_up(&ksocknal_data
.ksnd_connd_waitq
);
601 spin_unlock_bh(&ksocknal_data
.ksnd_connd_lock
);
605 ksocknal_launch_all_connections_locked (ksock_peer_t
*peer
)
607 ksock_route_t
*route
;
609 /* called holding write lock on ksnd_global_lock */
611 /* launch any/all connections that need it */
612 route
= ksocknal_find_connectable_route_locked(peer
);
616 ksocknal_launch_connection_locked(route
);
621 ksocknal_find_conn_locked(ksock_peer_t
*peer
, ksock_tx_t
*tx
, int nonblk
)
623 struct list_head
*tmp
;
625 ksock_conn_t
*typed
= NULL
;
626 ksock_conn_t
*fallback
= NULL
;
630 list_for_each (tmp
, &peer
->ksnp_conns
) {
631 ksock_conn_t
*c
= list_entry(tmp
, ksock_conn_t
, ksnc_list
);
632 int nob
= atomic_read(&c
->ksnc_tx_nob
) +
633 c
->ksnc_sock
->sk
->sk_wmem_queued
;
636 LASSERT (!c
->ksnc_closing
);
637 LASSERT (c
->ksnc_proto
!= NULL
&&
638 c
->ksnc_proto
->pro_match_tx
!= NULL
);
640 rc
= c
->ksnc_proto
->pro_match_tx(c
, tx
, nonblk
);
645 case SOCKNAL_MATCH_NO
: /* protocol rejected the tx */
648 case SOCKNAL_MATCH_YES
: /* typed connection */
649 if (typed
== NULL
|| tnob
> nob
||
650 (tnob
== nob
&& *ksocknal_tunables
.ksnd_round_robin
&&
651 cfs_time_after(typed
->ksnc_tx_last_post
, c
->ksnc_tx_last_post
))) {
657 case SOCKNAL_MATCH_MAY
: /* fallback connection */
658 if (fallback
== NULL
|| fnob
> nob
||
659 (fnob
== nob
&& *ksocknal_tunables
.ksnd_round_robin
&&
660 cfs_time_after(fallback
->ksnc_tx_last_post
, c
->ksnc_tx_last_post
))) {
668 /* prefer the typed selection */
669 conn
= (typed
!= NULL
) ? typed
: fallback
;
672 conn
->ksnc_tx_last_post
= cfs_time_current();
678 ksocknal_tx_prep(ksock_conn_t
*conn
, ksock_tx_t
*tx
)
680 conn
->ksnc_proto
->pro_pack(tx
);
682 atomic_add (tx
->tx_nob
, &conn
->ksnc_tx_nob
);
683 ksocknal_conn_addref(conn
); /* +1 ref for tx */
688 ksocknal_queue_tx_locked (ksock_tx_t
*tx
, ksock_conn_t
*conn
)
690 ksock_sched_t
*sched
= conn
->ksnc_scheduler
;
691 ksock_msg_t
*msg
= &tx
->tx_msg
;
692 ksock_tx_t
*ztx
= NULL
;
695 /* called holding global lock (read or irq-write) and caller may
696 * not have dropped this lock between finding conn and calling me,
697 * so we don't need the {get,put}connsock dance to deref
699 LASSERT(!conn
->ksnc_closing
);
701 CDEBUG(D_NET
, "Sending to %s ip %pI4h:%d\n",
702 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
706 ksocknal_tx_prep(conn
, tx
);
708 /* Ensure the frags we've been given EXACTLY match the number of
709 * bytes we want to send. Many TCP/IP stacks disregard any total
710 * size parameters passed to them and just look at the frags.
712 * We always expect at least 1 mapped fragment containing the
713 * complete ksocknal message header. */
714 LASSERT (lnet_iov_nob (tx
->tx_niov
, tx
->tx_iov
) +
715 lnet_kiov_nob(tx
->tx_nkiov
, tx
->tx_kiov
) ==
716 (unsigned int)tx
->tx_nob
);
717 LASSERT (tx
->tx_niov
>= 1);
718 LASSERT (tx
->tx_resid
== tx
->tx_nob
);
720 CDEBUG (D_NET
, "Packet %p type %d, nob %d niov %d nkiov %d\n",
721 tx
, (tx
->tx_lnetmsg
!= NULL
) ? tx
->tx_lnetmsg
->msg_hdr
.type
:
723 tx
->tx_nob
, tx
->tx_niov
, tx
->tx_nkiov
);
726 * FIXME: SOCK_WMEM_QUEUED and SOCK_ERROR could block in __DARWIN8__
727 * but they're used inside spinlocks a lot.
729 bufnob
= conn
->ksnc_sock
->sk
->sk_wmem_queued
;
730 spin_lock_bh(&sched
->kss_lock
);
732 if (list_empty(&conn
->ksnc_tx_queue
) && bufnob
== 0) {
733 /* First packet starts the timeout */
734 conn
->ksnc_tx_deadline
=
735 cfs_time_shift(*ksocknal_tunables
.ksnd_timeout
);
736 if (conn
->ksnc_tx_bufnob
> 0) /* something got ACKed */
737 conn
->ksnc_peer
->ksnp_last_alive
= cfs_time_current();
738 conn
->ksnc_tx_bufnob
= 0;
739 mb(); /* order with adding to tx_queue */
742 if (msg
->ksm_type
== KSOCK_MSG_NOOP
) {
743 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
744 * on a normal packet so I don't need to send it */
745 LASSERT (msg
->ksm_zc_cookies
[1] != 0);
746 LASSERT (conn
->ksnc_proto
->pro_queue_tx_zcack
!= NULL
);
748 if (conn
->ksnc_proto
->pro_queue_tx_zcack(conn
, tx
, 0))
749 ztx
= tx
; /* ZC ACK piggybacked on ztx release tx later */
752 /* It's a normal packet - can it piggback a noop zc-ack that
753 * has been queued already? */
754 LASSERT (msg
->ksm_zc_cookies
[1] == 0);
755 LASSERT (conn
->ksnc_proto
->pro_queue_tx_msg
!= NULL
);
757 ztx
= conn
->ksnc_proto
->pro_queue_tx_msg(conn
, tx
);
758 /* ztx will be released later */
762 atomic_sub (ztx
->tx_nob
, &conn
->ksnc_tx_nob
);
763 list_add_tail(&ztx
->tx_list
, &sched
->kss_zombie_noop_txs
);
766 if (conn
->ksnc_tx_ready
&& /* able to send */
767 !conn
->ksnc_tx_scheduled
) { /* not scheduled to send */
768 /* +1 ref for scheduler */
769 ksocknal_conn_addref(conn
);
770 list_add_tail (&conn
->ksnc_tx_list
,
771 &sched
->kss_tx_conns
);
772 conn
->ksnc_tx_scheduled
= 1;
773 wake_up (&sched
->kss_waitq
);
776 spin_unlock_bh(&sched
->kss_lock
);
781 ksocknal_find_connectable_route_locked (ksock_peer_t
*peer
)
783 unsigned long now
= cfs_time_current();
784 struct list_head
*tmp
;
785 ksock_route_t
*route
;
787 list_for_each (tmp
, &peer
->ksnp_routes
) {
788 route
= list_entry (tmp
, ksock_route_t
, ksnr_list
);
790 LASSERT (!route
->ksnr_connecting
|| route
->ksnr_scheduled
);
792 if (route
->ksnr_scheduled
) /* connections being established */
795 /* all route types connected ? */
796 if ((ksocknal_route_mask() & ~route
->ksnr_connected
) == 0)
799 if (!(route
->ksnr_retry_interval
== 0 || /* first attempt */
800 cfs_time_aftereq(now
, route
->ksnr_timeout
))) {
802 "Too soon to retry route %pI4h "
803 "(cnted %d, interval %ld, %ld secs later)\n",
805 route
->ksnr_connected
,
806 route
->ksnr_retry_interval
,
807 cfs_duration_sec(route
->ksnr_timeout
- now
));
818 ksocknal_find_connecting_route_locked (ksock_peer_t
*peer
)
820 struct list_head
*tmp
;
821 ksock_route_t
*route
;
823 list_for_each (tmp
, &peer
->ksnp_routes
) {
824 route
= list_entry (tmp
, ksock_route_t
, ksnr_list
);
826 LASSERT (!route
->ksnr_connecting
|| route
->ksnr_scheduled
);
828 if (route
->ksnr_scheduled
)
836 ksocknal_launch_packet (lnet_ni_t
*ni
, ksock_tx_t
*tx
, lnet_process_id_t id
)
844 LASSERT (tx
->tx_conn
== NULL
);
846 g_lock
= &ksocknal_data
.ksnd_global_lock
;
848 for (retry
= 0;; retry
= 1) {
850 peer
= ksocknal_find_peer_locked(ni
, id
);
852 if (ksocknal_find_connectable_route_locked(peer
) == NULL
) {
853 conn
= ksocknal_find_conn_locked(peer
, tx
, tx
->tx_nonblk
);
855 /* I've got no routes that need to be
856 * connecting and I do have an actual
858 ksocknal_queue_tx_locked (tx
, conn
);
865 /* I'll need a write lock... */
868 write_lock_bh(g_lock
);
870 peer
= ksocknal_find_peer_locked(ni
, id
);
874 write_unlock_bh(g_lock
);
876 if ((id
.pid
& LNET_PID_USERFLAG
) != 0) {
877 CERROR("Refusing to create a connection to "
878 "userspace process %s\n", libcfs_id2str(id
));
879 return -EHOSTUNREACH
;
883 CERROR("Can't find peer %s\n", libcfs_id2str(id
));
884 return -EHOSTUNREACH
;
887 rc
= ksocknal_add_peer(ni
, id
,
888 LNET_NIDADDR(id
.nid
),
889 lnet_acceptor_port());
891 CERROR("Can't add peer %s: %d\n",
892 libcfs_id2str(id
), rc
);
897 ksocknal_launch_all_connections_locked(peer
);
899 conn
= ksocknal_find_conn_locked(peer
, tx
, tx
->tx_nonblk
);
901 /* Connection exists; queue message on it */
902 ksocknal_queue_tx_locked (tx
, conn
);
903 write_unlock_bh(g_lock
);
907 if (peer
->ksnp_accepting
> 0 ||
908 ksocknal_find_connecting_route_locked (peer
) != NULL
) {
909 /* the message is going to be pinned to the peer */
911 cfs_time_shift(*ksocknal_tunables
.ksnd_timeout
);
913 /* Queue the message until a connection is established */
914 list_add_tail (&tx
->tx_list
, &peer
->ksnp_tx_queue
);
915 write_unlock_bh(g_lock
);
919 write_unlock_bh(g_lock
);
921 /* NB Routes may be ignored if connections to them failed recently */
922 CNETERR("No usable routes to %s\n", libcfs_id2str(id
));
923 return -EHOSTUNREACH
;
927 ksocknal_send(lnet_ni_t
*ni
, void *private, lnet_msg_t
*lntmsg
)
930 int type
= lntmsg
->msg_type
;
931 lnet_process_id_t target
= lntmsg
->msg_target
;
932 unsigned int payload_niov
= lntmsg
->msg_niov
;
933 struct iovec
*payload_iov
= lntmsg
->msg_iov
;
934 lnet_kiov_t
*payload_kiov
= lntmsg
->msg_kiov
;
935 unsigned int payload_offset
= lntmsg
->msg_offset
;
936 unsigned int payload_nob
= lntmsg
->msg_len
;
941 /* NB 'private' is different depending on what we're sending.
942 * Just ignore it... */
944 CDEBUG(D_NET
, "sending %u bytes in %d frags to %s\n",
945 payload_nob
, payload_niov
, libcfs_id2str(target
));
947 LASSERT (payload_nob
== 0 || payload_niov
> 0);
948 LASSERT (payload_niov
<= LNET_MAX_IOV
);
949 /* payload is either all vaddrs or all pages */
950 LASSERT (!(payload_kiov
!= NULL
&& payload_iov
!= NULL
));
951 LASSERT (!in_interrupt ());
953 if (payload_iov
!= NULL
)
954 desc_size
= offsetof(ksock_tx_t
,
955 tx_frags
.virt
.iov
[1 + payload_niov
]);
957 desc_size
= offsetof(ksock_tx_t
,
958 tx_frags
.paged
.kiov
[payload_niov
]);
960 if (lntmsg
->msg_vmflush
)
961 mpflag
= cfs_memory_pressure_get_and_set();
962 tx
= ksocknal_alloc_tx(KSOCK_MSG_LNET
, desc_size
);
964 CERROR("Can't allocate tx desc type %d size %d\n",
966 if (lntmsg
->msg_vmflush
)
967 cfs_memory_pressure_restore(mpflag
);
971 tx
->tx_conn
= NULL
; /* set when assigned a conn */
972 tx
->tx_lnetmsg
= lntmsg
;
974 if (payload_iov
!= NULL
) {
977 tx
->tx_iov
= tx
->tx_frags
.virt
.iov
;
979 lnet_extract_iov(payload_niov
, &tx
->tx_iov
[1],
980 payload_niov
, payload_iov
,
981 payload_offset
, payload_nob
);
984 tx
->tx_iov
= &tx
->tx_frags
.paged
.iov
;
985 tx
->tx_kiov
= tx
->tx_frags
.paged
.kiov
;
986 tx
->tx_nkiov
= lnet_extract_kiov(payload_niov
, tx
->tx_kiov
,
987 payload_niov
, payload_kiov
,
988 payload_offset
, payload_nob
);
990 if (payload_nob
>= *ksocknal_tunables
.ksnd_zc_min_payload
)
991 tx
->tx_zc_capable
= 1;
994 socklnd_init_msg(&tx
->tx_msg
, KSOCK_MSG_LNET
);
996 /* The first fragment will be set later in pro_pack */
997 rc
= ksocknal_launch_packet(ni
, tx
, target
);
999 cfs_memory_pressure_restore(mpflag
);
1004 ksocknal_free_tx(tx
);
1009 ksocknal_thread_start(int (*fn
)(void *arg
), void *arg
, char *name
)
1011 struct task_struct
*task
= kthread_run(fn
, arg
, "%s", name
);
1014 return PTR_ERR(task
);
1016 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1017 ksocknal_data
.ksnd_nthreads
++;
1018 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
1023 ksocknal_thread_fini (void)
1025 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1026 ksocknal_data
.ksnd_nthreads
--;
1027 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
1031 ksocknal_new_packet (ksock_conn_t
*conn
, int nob_to_skip
)
1033 static char ksocknal_slop_buffer
[4096];
1039 LASSERT(conn
->ksnc_proto
!= NULL
);
1041 if ((*ksocknal_tunables
.ksnd_eager_ack
& conn
->ksnc_type
) != 0) {
1042 /* Remind the socket to ack eagerly... */
1043 ksocknal_lib_eager_ack(conn
);
1046 if (nob_to_skip
== 0) { /* right at next packet boundary now */
1047 conn
->ksnc_rx_started
= 0;
1048 mb(); /* racing with timeout thread */
1050 switch (conn
->ksnc_proto
->pro_version
) {
1051 case KSOCK_PROTO_V2
:
1052 case KSOCK_PROTO_V3
:
1053 conn
->ksnc_rx_state
= SOCKNAL_RX_KSM_HEADER
;
1054 conn
->ksnc_rx_iov
= (struct iovec
*)&conn
->ksnc_rx_iov_space
;
1055 conn
->ksnc_rx_iov
[0].iov_base
= (char *)&conn
->ksnc_msg
;
1057 conn
->ksnc_rx_nob_wanted
= offsetof(ksock_msg_t
, ksm_u
);
1058 conn
->ksnc_rx_nob_left
= offsetof(ksock_msg_t
, ksm_u
);
1059 conn
->ksnc_rx_iov
[0].iov_len
= offsetof(ksock_msg_t
, ksm_u
);
1062 case KSOCK_PROTO_V1
:
1063 /* Receiving bare lnet_hdr_t */
1064 conn
->ksnc_rx_state
= SOCKNAL_RX_LNET_HEADER
;
1065 conn
->ksnc_rx_nob_wanted
= sizeof(lnet_hdr_t
);
1066 conn
->ksnc_rx_nob_left
= sizeof(lnet_hdr_t
);
1068 conn
->ksnc_rx_iov
= (struct iovec
*)&conn
->ksnc_rx_iov_space
;
1069 conn
->ksnc_rx_iov
[0].iov_base
= (char *)&conn
->ksnc_msg
.ksm_u
.lnetmsg
;
1070 conn
->ksnc_rx_iov
[0].iov_len
= sizeof (lnet_hdr_t
);
1076 conn
->ksnc_rx_niov
= 1;
1078 conn
->ksnc_rx_kiov
= NULL
;
1079 conn
->ksnc_rx_nkiov
= 0;
1080 conn
->ksnc_rx_csum
= ~0;
1084 /* Set up to skip as much as possible now. If there's more left
1085 * (ran out of iov entries) we'll get called again */
1087 conn
->ksnc_rx_state
= SOCKNAL_RX_SLOP
;
1088 conn
->ksnc_rx_nob_left
= nob_to_skip
;
1089 conn
->ksnc_rx_iov
= (struct iovec
*)&conn
->ksnc_rx_iov_space
;
1094 nob
= MIN (nob_to_skip
, sizeof (ksocknal_slop_buffer
));
1096 conn
->ksnc_rx_iov
[niov
].iov_base
= ksocknal_slop_buffer
;
1097 conn
->ksnc_rx_iov
[niov
].iov_len
= nob
;
1102 } while (nob_to_skip
!= 0 && /* mustn't overflow conn's rx iov */
1103 niov
< sizeof(conn
->ksnc_rx_iov_space
) / sizeof (struct iovec
));
1105 conn
->ksnc_rx_niov
= niov
;
1106 conn
->ksnc_rx_kiov
= NULL
;
1107 conn
->ksnc_rx_nkiov
= 0;
1108 conn
->ksnc_rx_nob_wanted
= skipped
;
1113 ksocknal_process_receive (ksock_conn_t
*conn
)
1116 lnet_process_id_t
*id
;
1119 LASSERT (atomic_read(&conn
->ksnc_conn_refcount
) > 0);
1121 /* NB: sched lock NOT held */
1122 /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */
1123 LASSERT (conn
->ksnc_rx_state
== SOCKNAL_RX_KSM_HEADER
||
1124 conn
->ksnc_rx_state
== SOCKNAL_RX_LNET_PAYLOAD
||
1125 conn
->ksnc_rx_state
== SOCKNAL_RX_LNET_HEADER
||
1126 conn
->ksnc_rx_state
== SOCKNAL_RX_SLOP
);
1128 if (conn
->ksnc_rx_nob_wanted
!= 0) {
1129 rc
= ksocknal_receive(conn
);
1132 LASSERT (rc
!= -EAGAIN
);
1135 CDEBUG(D_NET
, "[%p] EOF from %s"
1136 " ip %pI4h:%d\n", conn
,
1137 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
1140 else if (!conn
->ksnc_closing
)
1141 CERROR("[%p] Error %d on read from %s"
1144 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
1148 /* it's not an error if conn is being closed */
1149 ksocknal_close_conn_and_siblings (conn
,
1150 (conn
->ksnc_closing
) ? 0 : rc
);
1151 return (rc
== 0 ? -ESHUTDOWN
: rc
);
1154 if (conn
->ksnc_rx_nob_wanted
!= 0) {
1159 switch (conn
->ksnc_rx_state
) {
1160 case SOCKNAL_RX_KSM_HEADER
:
1161 if (conn
->ksnc_flip
) {
1162 __swab32s(&conn
->ksnc_msg
.ksm_type
);
1163 __swab32s(&conn
->ksnc_msg
.ksm_csum
);
1164 __swab64s(&conn
->ksnc_msg
.ksm_zc_cookies
[0]);
1165 __swab64s(&conn
->ksnc_msg
.ksm_zc_cookies
[1]);
1168 if (conn
->ksnc_msg
.ksm_type
!= KSOCK_MSG_NOOP
&&
1169 conn
->ksnc_msg
.ksm_type
!= KSOCK_MSG_LNET
) {
1170 CERROR("%s: Unknown message type: %x\n",
1171 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
1172 conn
->ksnc_msg
.ksm_type
);
1173 ksocknal_new_packet(conn
, 0);
1174 ksocknal_close_conn_and_siblings(conn
, -EPROTO
);
1178 if (conn
->ksnc_msg
.ksm_type
== KSOCK_MSG_NOOP
&&
1179 conn
->ksnc_msg
.ksm_csum
!= 0 && /* has checksum */
1180 conn
->ksnc_msg
.ksm_csum
!= conn
->ksnc_rx_csum
) {
1181 /* NOOP Checksum error */
1182 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1183 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
1184 conn
->ksnc_msg
.ksm_csum
, conn
->ksnc_rx_csum
);
1185 ksocknal_new_packet(conn
, 0);
1186 ksocknal_close_conn_and_siblings(conn
, -EPROTO
);
1190 if (conn
->ksnc_msg
.ksm_zc_cookies
[1] != 0) {
1193 LASSERT (conn
->ksnc_proto
!= &ksocknal_protocol_v1x
);
1195 if (conn
->ksnc_msg
.ksm_type
== KSOCK_MSG_NOOP
)
1196 cookie
= conn
->ksnc_msg
.ksm_zc_cookies
[0];
1198 rc
= conn
->ksnc_proto
->pro_handle_zcack(conn
, cookie
,
1199 conn
->ksnc_msg
.ksm_zc_cookies
[1]);
1202 CERROR("%s: Unknown ZC-ACK cookie: %llu, %llu\n",
1203 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
1204 cookie
, conn
->ksnc_msg
.ksm_zc_cookies
[1]);
1205 ksocknal_new_packet(conn
, 0);
1206 ksocknal_close_conn_and_siblings(conn
, -EPROTO
);
1211 if (conn
->ksnc_msg
.ksm_type
== KSOCK_MSG_NOOP
) {
1212 ksocknal_new_packet (conn
, 0);
1213 return 0; /* NOOP is done and just return */
1216 conn
->ksnc_rx_state
= SOCKNAL_RX_LNET_HEADER
;
1217 conn
->ksnc_rx_nob_wanted
= sizeof(ksock_lnet_msg_t
);
1218 conn
->ksnc_rx_nob_left
= sizeof(ksock_lnet_msg_t
);
1220 conn
->ksnc_rx_iov
= (struct iovec
*)&conn
->ksnc_rx_iov_space
;
1221 conn
->ksnc_rx_iov
[0].iov_base
= (char *)&conn
->ksnc_msg
.ksm_u
.lnetmsg
;
1222 conn
->ksnc_rx_iov
[0].iov_len
= sizeof(ksock_lnet_msg_t
);
1224 conn
->ksnc_rx_niov
= 1;
1225 conn
->ksnc_rx_kiov
= NULL
;
1226 conn
->ksnc_rx_nkiov
= 0;
1228 goto again
; /* read lnet header now */
1230 case SOCKNAL_RX_LNET_HEADER
:
1231 /* unpack message header */
1232 conn
->ksnc_proto
->pro_unpack(&conn
->ksnc_msg
);
1234 if ((conn
->ksnc_peer
->ksnp_id
.pid
& LNET_PID_USERFLAG
) != 0) {
1235 /* Userspace peer */
1236 lhdr
= &conn
->ksnc_msg
.ksm_u
.lnetmsg
.ksnm_hdr
;
1237 id
= &conn
->ksnc_peer
->ksnp_id
;
1239 /* Substitute process ID assigned at connection time */
1240 lhdr
->src_pid
= cpu_to_le32(id
->pid
);
1241 lhdr
->src_nid
= cpu_to_le64(id
->nid
);
1244 conn
->ksnc_rx_state
= SOCKNAL_RX_PARSE
;
1245 ksocknal_conn_addref(conn
); /* ++ref while parsing */
1247 rc
= lnet_parse(conn
->ksnc_peer
->ksnp_ni
,
1248 &conn
->ksnc_msg
.ksm_u
.lnetmsg
.ksnm_hdr
,
1249 conn
->ksnc_peer
->ksnp_id
.nid
, conn
, 0);
1251 /* I just received garbage: give up on this conn */
1252 ksocknal_new_packet(conn
, 0);
1253 ksocknal_close_conn_and_siblings (conn
, rc
);
1254 ksocknal_conn_decref(conn
);
1258 /* I'm racing with ksocknal_recv() */
1259 LASSERT (conn
->ksnc_rx_state
== SOCKNAL_RX_PARSE
||
1260 conn
->ksnc_rx_state
== SOCKNAL_RX_LNET_PAYLOAD
);
1262 if (conn
->ksnc_rx_state
!= SOCKNAL_RX_LNET_PAYLOAD
)
1265 /* ksocknal_recv() got called */
1268 case SOCKNAL_RX_LNET_PAYLOAD
:
1269 /* payload all received */
1272 if (conn
->ksnc_rx_nob_left
== 0 && /* not truncating */
1273 conn
->ksnc_msg
.ksm_csum
!= 0 && /* has checksum */
1274 conn
->ksnc_msg
.ksm_csum
!= conn
->ksnc_rx_csum
) {
1275 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1276 libcfs_id2str(conn
->ksnc_peer
->ksnp_id
),
1277 conn
->ksnc_msg
.ksm_csum
, conn
->ksnc_rx_csum
);
1281 if (rc
== 0 && conn
->ksnc_msg
.ksm_zc_cookies
[0] != 0) {
1282 LASSERT(conn
->ksnc_proto
!= &ksocknal_protocol_v1x
);
1284 lhdr
= &conn
->ksnc_msg
.ksm_u
.lnetmsg
.ksnm_hdr
;
1285 id
= &conn
->ksnc_peer
->ksnp_id
;
1287 rc
= conn
->ksnc_proto
->pro_handle_zcreq(conn
,
1288 conn
->ksnc_msg
.ksm_zc_cookies
[0],
1289 *ksocknal_tunables
.ksnd_nonblk_zcack
||
1290 le64_to_cpu(lhdr
->src_nid
) != id
->nid
);
1293 lnet_finalize(conn
->ksnc_peer
->ksnp_ni
, conn
->ksnc_cookie
, rc
);
1296 ksocknal_new_packet(conn
, 0);
1297 ksocknal_close_conn_and_siblings (conn
, rc
);
1302 case SOCKNAL_RX_SLOP
:
1303 /* starting new packet? */
1304 if (ksocknal_new_packet (conn
, conn
->ksnc_rx_nob_left
))
1305 return 0; /* come back later */
1306 goto again
; /* try to finish reading slop now */
1314 return -EINVAL
; /* keep gcc happy */
1318 ksocknal_recv (lnet_ni_t
*ni
, void *private, lnet_msg_t
*msg
, int delayed
,
1319 unsigned int niov
, struct iovec
*iov
, lnet_kiov_t
*kiov
,
1320 unsigned int offset
, unsigned int mlen
, unsigned int rlen
)
1322 ksock_conn_t
*conn
= (ksock_conn_t
*)private;
1323 ksock_sched_t
*sched
= conn
->ksnc_scheduler
;
1325 LASSERT (mlen
<= rlen
);
1326 LASSERT (niov
<= LNET_MAX_IOV
);
1328 conn
->ksnc_cookie
= msg
;
1329 conn
->ksnc_rx_nob_wanted
= mlen
;
1330 conn
->ksnc_rx_nob_left
= rlen
;
1332 if (mlen
== 0 || iov
!= NULL
) {
1333 conn
->ksnc_rx_nkiov
= 0;
1334 conn
->ksnc_rx_kiov
= NULL
;
1335 conn
->ksnc_rx_iov
= conn
->ksnc_rx_iov_space
.iov
;
1336 conn
->ksnc_rx_niov
=
1337 lnet_extract_iov(LNET_MAX_IOV
, conn
->ksnc_rx_iov
,
1338 niov
, iov
, offset
, mlen
);
1340 conn
->ksnc_rx_niov
= 0;
1341 conn
->ksnc_rx_iov
= NULL
;
1342 conn
->ksnc_rx_kiov
= conn
->ksnc_rx_iov_space
.kiov
;
1343 conn
->ksnc_rx_nkiov
=
1344 lnet_extract_kiov(LNET_MAX_IOV
, conn
->ksnc_rx_kiov
,
1345 niov
, kiov
, offset
, mlen
);
1349 lnet_iov_nob (conn
->ksnc_rx_niov
, conn
->ksnc_rx_iov
) +
1350 lnet_kiov_nob (conn
->ksnc_rx_nkiov
, conn
->ksnc_rx_kiov
));
1352 LASSERT (conn
->ksnc_rx_scheduled
);
1354 spin_lock_bh(&sched
->kss_lock
);
1356 switch (conn
->ksnc_rx_state
) {
1357 case SOCKNAL_RX_PARSE_WAIT
:
1358 list_add_tail(&conn
->ksnc_rx_list
, &sched
->kss_rx_conns
);
1359 wake_up (&sched
->kss_waitq
);
1360 LASSERT (conn
->ksnc_rx_ready
);
1363 case SOCKNAL_RX_PARSE
:
1364 /* scheduler hasn't noticed I'm parsing yet */
1368 conn
->ksnc_rx_state
= SOCKNAL_RX_LNET_PAYLOAD
;
1370 spin_unlock_bh(&sched
->kss_lock
);
1371 ksocknal_conn_decref(conn
);
1376 ksocknal_sched_cansleep(ksock_sched_t
*sched
)
1380 spin_lock_bh(&sched
->kss_lock
);
1382 rc
= (!ksocknal_data
.ksnd_shuttingdown
&&
1383 list_empty(&sched
->kss_rx_conns
) &&
1384 list_empty(&sched
->kss_tx_conns
));
1386 spin_unlock_bh(&sched
->kss_lock
);
1390 int ksocknal_scheduler(void *arg
)
1392 struct ksock_sched_info
*info
;
1393 ksock_sched_t
*sched
;
1398 long id
= (long)arg
;
1400 info
= ksocknal_data
.ksnd_sched_info
[KSOCK_THREAD_CPT(id
)];
1401 sched
= &info
->ksi_scheds
[KSOCK_THREAD_SID(id
)];
1403 cfs_block_allsigs();
1405 rc
= cfs_cpt_bind(lnet_cpt_table(), info
->ksi_cpt
);
1407 CERROR("Can't set CPT affinity to %d: %d\n",
1411 spin_lock_bh(&sched
->kss_lock
);
1413 while (!ksocknal_data
.ksnd_shuttingdown
) {
1414 int did_something
= 0;
1416 /* Ensure I progress everything semi-fairly */
1418 if (!list_empty (&sched
->kss_rx_conns
)) {
1419 conn
= list_entry(sched
->kss_rx_conns
.next
,
1420 ksock_conn_t
, ksnc_rx_list
);
1421 list_del(&conn
->ksnc_rx_list
);
1423 LASSERT(conn
->ksnc_rx_scheduled
);
1424 LASSERT(conn
->ksnc_rx_ready
);
1426 /* clear rx_ready in case receive isn't complete.
1427 * Do it BEFORE we call process_recv, since
1428 * data_ready can set it any time after we release
1430 conn
->ksnc_rx_ready
= 0;
1431 spin_unlock_bh(&sched
->kss_lock
);
1433 rc
= ksocknal_process_receive(conn
);
1435 spin_lock_bh(&sched
->kss_lock
);
1437 /* I'm the only one that can clear this flag */
1438 LASSERT(conn
->ksnc_rx_scheduled
);
1440 /* Did process_receive get everything it wanted? */
1442 conn
->ksnc_rx_ready
= 1;
1444 if (conn
->ksnc_rx_state
== SOCKNAL_RX_PARSE
) {
1445 /* Conn blocked waiting for ksocknal_recv()
1446 * I change its state (under lock) to signal
1447 * it can be rescheduled */
1448 conn
->ksnc_rx_state
= SOCKNAL_RX_PARSE_WAIT
;
1449 } else if (conn
->ksnc_rx_ready
) {
1450 /* reschedule for rx */
1451 list_add_tail (&conn
->ksnc_rx_list
,
1452 &sched
->kss_rx_conns
);
1454 conn
->ksnc_rx_scheduled
= 0;
1456 ksocknal_conn_decref(conn
);
1462 if (!list_empty (&sched
->kss_tx_conns
)) {
1465 if (!list_empty(&sched
->kss_zombie_noop_txs
)) {
1467 &sched
->kss_zombie_noop_txs
);
1468 list_del_init(&sched
->kss_zombie_noop_txs
);
1471 conn
= list_entry(sched
->kss_tx_conns
.next
,
1472 ksock_conn_t
, ksnc_tx_list
);
1473 list_del (&conn
->ksnc_tx_list
);
1475 LASSERT(conn
->ksnc_tx_scheduled
);
1476 LASSERT(conn
->ksnc_tx_ready
);
1477 LASSERT(!list_empty(&conn
->ksnc_tx_queue
));
1479 tx
= list_entry(conn
->ksnc_tx_queue
.next
,
1480 ksock_tx_t
, tx_list
);
1482 if (conn
->ksnc_tx_carrier
== tx
)
1483 ksocknal_next_tx_carrier(conn
);
1485 /* dequeue now so empty list => more to send */
1486 list_del(&tx
->tx_list
);
1488 /* Clear tx_ready in case send isn't complete. Do
1489 * it BEFORE we call process_transmit, since
1490 * write_space can set it any time after we release
1492 conn
->ksnc_tx_ready
= 0;
1493 spin_unlock_bh(&sched
->kss_lock
);
1495 if (!list_empty(&zlist
)) {
1496 /* free zombie noop txs, it's fast because
1497 * noop txs are just put in freelist */
1498 ksocknal_txlist_done(NULL
, &zlist
, 0);
1501 rc
= ksocknal_process_transmit(conn
, tx
);
1503 if (rc
== -ENOMEM
|| rc
== -EAGAIN
) {
1504 /* Incomplete send: replace tx on HEAD of tx_queue */
1505 spin_lock_bh(&sched
->kss_lock
);
1506 list_add(&tx
->tx_list
,
1507 &conn
->ksnc_tx_queue
);
1509 /* Complete send; tx -ref */
1510 ksocknal_tx_decref(tx
);
1512 spin_lock_bh(&sched
->kss_lock
);
1513 /* assume space for more */
1514 conn
->ksnc_tx_ready
= 1;
1517 if (rc
== -ENOMEM
) {
1518 /* Do nothing; after a short timeout, this
1519 * conn will be reposted on kss_tx_conns. */
1520 } else if (conn
->ksnc_tx_ready
&&
1521 !list_empty (&conn
->ksnc_tx_queue
)) {
1522 /* reschedule for tx */
1523 list_add_tail (&conn
->ksnc_tx_list
,
1524 &sched
->kss_tx_conns
);
1526 conn
->ksnc_tx_scheduled
= 0;
1528 ksocknal_conn_decref(conn
);
1533 if (!did_something
|| /* nothing to do */
1534 ++nloops
== SOCKNAL_RESCHED
) { /* hogging CPU? */
1535 spin_unlock_bh(&sched
->kss_lock
);
1539 if (!did_something
) { /* wait for something to do */
1540 rc
= wait_event_interruptible_exclusive(
1542 !ksocknal_sched_cansleep(sched
));
1548 spin_lock_bh(&sched
->kss_lock
);
1552 spin_unlock_bh(&sched
->kss_lock
);
1553 ksocknal_thread_fini();
1558 * Add connection to kss_rx_conns of scheduler
1559 * and wakeup the scheduler.
1561 void ksocknal_read_callback (ksock_conn_t
*conn
)
1563 ksock_sched_t
*sched
;
1565 sched
= conn
->ksnc_scheduler
;
1567 spin_lock_bh(&sched
->kss_lock
);
1569 conn
->ksnc_rx_ready
= 1;
1571 if (!conn
->ksnc_rx_scheduled
) { /* not being progressed */
1572 list_add_tail(&conn
->ksnc_rx_list
,
1573 &sched
->kss_rx_conns
);
1574 conn
->ksnc_rx_scheduled
= 1;
1575 /* extra ref for scheduler */
1576 ksocknal_conn_addref(conn
);
1578 wake_up (&sched
->kss_waitq
);
1580 spin_unlock_bh(&sched
->kss_lock
);
1584 * Add connection to kss_tx_conns of scheduler
1585 * and wakeup the scheduler.
1587 void ksocknal_write_callback (ksock_conn_t
*conn
)
1589 ksock_sched_t
*sched
;
1591 sched
= conn
->ksnc_scheduler
;
1593 spin_lock_bh(&sched
->kss_lock
);
1595 conn
->ksnc_tx_ready
= 1;
1597 if (!conn
->ksnc_tx_scheduled
&& /* not being progressed */
1598 !list_empty(&conn
->ksnc_tx_queue
)) { /* packets to send */
1599 list_add_tail (&conn
->ksnc_tx_list
,
1600 &sched
->kss_tx_conns
);
1601 conn
->ksnc_tx_scheduled
= 1;
1602 /* extra ref for scheduler */
1603 ksocknal_conn_addref(conn
);
1605 wake_up (&sched
->kss_waitq
);
1608 spin_unlock_bh(&sched
->kss_lock
);
1611 static ksock_proto_t
*
1612 ksocknal_parse_proto_version (ksock_hello_msg_t
*hello
)
1616 if (hello
->kshm_magic
== LNET_PROTO_MAGIC
)
1617 version
= hello
->kshm_version
;
1618 else if (hello
->kshm_magic
== __swab32(LNET_PROTO_MAGIC
))
1619 version
= __swab32(hello
->kshm_version
);
1622 #if SOCKNAL_VERSION_DEBUG
1623 if (*ksocknal_tunables
.ksnd_protocol
== 1)
1626 if (*ksocknal_tunables
.ksnd_protocol
== 2 &&
1627 version
== KSOCK_PROTO_V3
)
1630 if (version
== KSOCK_PROTO_V2
)
1631 return &ksocknal_protocol_v2x
;
1633 if (version
== KSOCK_PROTO_V3
)
1634 return &ksocknal_protocol_v3x
;
1639 if (hello
->kshm_magic
== le32_to_cpu(LNET_PROTO_TCP_MAGIC
)) {
1640 lnet_magicversion_t
*hmv
= (lnet_magicversion_t
*)hello
;
1642 CLASSERT (sizeof (lnet_magicversion_t
) ==
1643 offsetof (ksock_hello_msg_t
, kshm_src_nid
));
1645 if (hmv
->version_major
== cpu_to_le16 (KSOCK_PROTO_V1_MAJOR
) &&
1646 hmv
->version_minor
== cpu_to_le16 (KSOCK_PROTO_V1_MINOR
))
1647 return &ksocknal_protocol_v1x
;
1654 ksocknal_send_hello (lnet_ni_t
*ni
, ksock_conn_t
*conn
,
1655 lnet_nid_t peer_nid
, ksock_hello_msg_t
*hello
)
1657 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1658 ksock_net_t
*net
= (ksock_net_t
*)ni
->ni_data
;
1660 LASSERT (hello
->kshm_nips
<= LNET_MAX_INTERFACES
);
1662 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1663 LASSERT (conn
->ksnc_proto
!= NULL
);
1665 hello
->kshm_src_nid
= ni
->ni_nid
;
1666 hello
->kshm_dst_nid
= peer_nid
;
1667 hello
->kshm_src_pid
= the_lnet
.ln_pid
;
1669 hello
->kshm_src_incarnation
= net
->ksnn_incarnation
;
1670 hello
->kshm_ctype
= conn
->ksnc_type
;
1672 return conn
->ksnc_proto
->pro_send_hello(conn
, hello
);
1676 ksocknal_invert_type(int type
)
1679 case SOCKLND_CONN_ANY
:
1680 case SOCKLND_CONN_CONTROL
:
1682 case SOCKLND_CONN_BULK_IN
:
1683 return SOCKLND_CONN_BULK_OUT
;
1684 case SOCKLND_CONN_BULK_OUT
:
1685 return SOCKLND_CONN_BULK_IN
;
1687 return SOCKLND_CONN_NONE
;
1692 ksocknal_recv_hello (lnet_ni_t
*ni
, ksock_conn_t
*conn
,
1693 ksock_hello_msg_t
*hello
, lnet_process_id_t
*peerid
,
1696 /* Return < 0 fatal error
1698 * EALREADY lost connection race
1699 * EPROTO protocol version mismatch
1701 struct socket
*sock
= conn
->ksnc_sock
;
1702 int active
= (conn
->ksnc_proto
!= NULL
);
1706 ksock_proto_t
*proto
;
1707 lnet_process_id_t recv_id
;
1709 /* socket type set on active connections - not set on passive */
1710 LASSERT (!active
== !(conn
->ksnc_type
!= SOCKLND_CONN_NONE
));
1712 timeout
= active
? *ksocknal_tunables
.ksnd_timeout
:
1713 lnet_acceptor_timeout();
1715 rc
= libcfs_sock_read(sock
, &hello
->kshm_magic
, sizeof (hello
->kshm_magic
), timeout
);
1717 CERROR("Error %d reading HELLO from %pI4h\n",
1718 rc
, &conn
->ksnc_ipaddr
);
1723 if (hello
->kshm_magic
!= LNET_PROTO_MAGIC
&&
1724 hello
->kshm_magic
!= __swab32(LNET_PROTO_MAGIC
) &&
1725 hello
->kshm_magic
!= le32_to_cpu (LNET_PROTO_TCP_MAGIC
)) {
1726 /* Unexpected magic! */
1727 CERROR("Bad magic(1) %#08x (%#08x expected) from "
1728 "%pI4h\n", __cpu_to_le32 (hello
->kshm_magic
),
1729 LNET_PROTO_TCP_MAGIC
,
1730 &conn
->ksnc_ipaddr
);
1734 rc
= libcfs_sock_read(sock
, &hello
->kshm_version
,
1735 sizeof(hello
->kshm_version
), timeout
);
1737 CERROR("Error %d reading HELLO from %pI4h\n",
1738 rc
, &conn
->ksnc_ipaddr
);
1743 proto
= ksocknal_parse_proto_version(hello
);
1744 if (proto
== NULL
) {
1746 /* unknown protocol from peer, tell peer my protocol */
1747 conn
->ksnc_proto
= &ksocknal_protocol_v3x
;
1748 #if SOCKNAL_VERSION_DEBUG
1749 if (*ksocknal_tunables
.ksnd_protocol
== 2)
1750 conn
->ksnc_proto
= &ksocknal_protocol_v2x
;
1751 else if (*ksocknal_tunables
.ksnd_protocol
== 1)
1752 conn
->ksnc_proto
= &ksocknal_protocol_v1x
;
1754 hello
->kshm_nips
= 0;
1755 ksocknal_send_hello(ni
, conn
, ni
->ni_nid
, hello
);
1758 CERROR("Unknown protocol version (%d.x expected)"
1760 conn
->ksnc_proto
->pro_version
,
1761 &conn
->ksnc_ipaddr
);
1766 proto_match
= (conn
->ksnc_proto
== proto
);
1767 conn
->ksnc_proto
= proto
;
1769 /* receive the rest of hello message anyway */
1770 rc
= conn
->ksnc_proto
->pro_recv_hello(conn
, hello
, timeout
);
1772 CERROR("Error %d reading or checking hello from from %pI4h\n",
1773 rc
, &conn
->ksnc_ipaddr
);
1778 *incarnation
= hello
->kshm_src_incarnation
;
1780 if (hello
->kshm_src_nid
== LNET_NID_ANY
) {
1781 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
1782 "from %pI4h\n", &conn
->ksnc_ipaddr
);
1787 conn
->ksnc_port
> LNET_ACCEPTOR_MAX_RESERVED_PORT
) {
1788 /* Userspace NAL assigns peer process ID from socket */
1789 recv_id
.pid
= conn
->ksnc_port
| LNET_PID_USERFLAG
;
1790 recv_id
.nid
= LNET_MKNID(LNET_NIDNET(ni
->ni_nid
), conn
->ksnc_ipaddr
);
1792 recv_id
.nid
= hello
->kshm_src_nid
;
1793 recv_id
.pid
= hello
->kshm_src_pid
;
1799 /* peer determines type */
1800 conn
->ksnc_type
= ksocknal_invert_type(hello
->kshm_ctype
);
1801 if (conn
->ksnc_type
== SOCKLND_CONN_NONE
) {
1802 CERROR("Unexpected type %d from %s ip %pI4h\n",
1803 hello
->kshm_ctype
, libcfs_id2str(*peerid
),
1804 &conn
->ksnc_ipaddr
);
1811 if (peerid
->pid
!= recv_id
.pid
||
1812 peerid
->nid
!= recv_id
.nid
) {
1813 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1814 " %pI4h, but they claimed they were "
1815 "%s; please check your Lustre "
1817 libcfs_id2str(*peerid
),
1819 libcfs_id2str(recv_id
));
1823 if (hello
->kshm_ctype
== SOCKLND_CONN_NONE
) {
1824 /* Possible protocol mismatch or I lost the connection race */
1825 return proto_match
? EALREADY
: EPROTO
;
1828 if (ksocknal_invert_type(hello
->kshm_ctype
) != conn
->ksnc_type
) {
1829 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
1830 conn
->ksnc_type
, libcfs_id2str(*peerid
),
1840 ksocknal_connect (ksock_route_t
*route
)
1842 LIST_HEAD (zombies
);
1843 ksock_peer_t
*peer
= route
->ksnr_peer
;
1846 struct socket
*sock
;
1847 unsigned long deadline
;
1848 int retry_later
= 0;
1851 deadline
= cfs_time_add(cfs_time_current(),
1852 cfs_time_seconds(*ksocknal_tunables
.ksnd_timeout
));
1854 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1856 LASSERT (route
->ksnr_scheduled
);
1857 LASSERT (!route
->ksnr_connecting
);
1859 route
->ksnr_connecting
= 1;
1862 wanted
= ksocknal_route_mask() & ~route
->ksnr_connected
;
1864 /* stop connecting if peer/route got closed under me, or
1865 * route got connected while queued */
1866 if (peer
->ksnp_closing
|| route
->ksnr_deleted
||
1872 /* reschedule if peer is connecting to me */
1873 if (peer
->ksnp_accepting
> 0) {
1875 "peer %s(%d) already connecting to me, retry later.\n",
1876 libcfs_nid2str(peer
->ksnp_id
.nid
), peer
->ksnp_accepting
);
1880 if (retry_later
) /* needs reschedule */
1883 if ((wanted
& (1 << SOCKLND_CONN_ANY
)) != 0) {
1884 type
= SOCKLND_CONN_ANY
;
1885 } else if ((wanted
& (1 << SOCKLND_CONN_CONTROL
)) != 0) {
1886 type
= SOCKLND_CONN_CONTROL
;
1887 } else if ((wanted
& (1 << SOCKLND_CONN_BULK_IN
)) != 0) {
1888 type
= SOCKLND_CONN_BULK_IN
;
1890 LASSERT ((wanted
& (1 << SOCKLND_CONN_BULK_OUT
)) != 0);
1891 type
= SOCKLND_CONN_BULK_OUT
;
1894 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
1896 if (cfs_time_aftereq(cfs_time_current(), deadline
)) {
1898 lnet_connect_console_error(rc
, peer
->ksnp_id
.nid
,
1904 rc
= lnet_connect(&sock
, peer
->ksnp_id
.nid
,
1905 route
->ksnr_myipaddr
,
1906 route
->ksnr_ipaddr
, route
->ksnr_port
);
1910 rc
= ksocknal_create_conn(peer
->ksnp_ni
, route
, sock
, type
);
1912 lnet_connect_console_error(rc
, peer
->ksnp_id
.nid
,
1918 /* A +ve RC means I have to retry because I lost the connection
1919 * race or I have to renegotiate protocol version */
1920 retry_later
= (rc
!= 0);
1922 CDEBUG(D_NET
, "peer %s: conn race, retry later.\n",
1923 libcfs_nid2str(peer
->ksnp_id
.nid
));
1925 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1928 route
->ksnr_scheduled
= 0;
1929 route
->ksnr_connecting
= 0;
1932 /* re-queue for attention; this frees me up to handle
1933 * the peer's incoming connection request */
1935 if (rc
== EALREADY
||
1936 (rc
== 0 && peer
->ksnp_accepting
> 0)) {
1937 /* We want to introduce a delay before next
1938 * attempt to connect if we lost conn race,
1939 * but the race is resolved quickly usually,
1940 * so min_reconnectms should be good heuristic */
1941 route
->ksnr_retry_interval
=
1942 cfs_time_seconds(*ksocknal_tunables
.ksnd_min_reconnectms
)/1000;
1943 route
->ksnr_timeout
= cfs_time_add(cfs_time_current(),
1944 route
->ksnr_retry_interval
);
1947 ksocknal_launch_connection_locked(route
);
1950 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
1954 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1956 route
->ksnr_scheduled
= 0;
1957 route
->ksnr_connecting
= 0;
1959 /* This is a retry rather than a new connection */
1960 route
->ksnr_retry_interval
*= 2;
1961 route
->ksnr_retry_interval
=
1962 MAX(route
->ksnr_retry_interval
,
1963 cfs_time_seconds(*ksocknal_tunables
.ksnd_min_reconnectms
)/1000);
1964 route
->ksnr_retry_interval
=
1965 MIN(route
->ksnr_retry_interval
,
1966 cfs_time_seconds(*ksocknal_tunables
.ksnd_max_reconnectms
)/1000);
1968 LASSERT (route
->ksnr_retry_interval
!= 0);
1969 route
->ksnr_timeout
= cfs_time_add(cfs_time_current(),
1970 route
->ksnr_retry_interval
);
1972 if (!list_empty(&peer
->ksnp_tx_queue
) &&
1973 peer
->ksnp_accepting
== 0 &&
1974 ksocknal_find_connecting_route_locked(peer
) == NULL
) {
1977 /* ksnp_tx_queue is queued on a conn on successful
1978 * connection for V1.x and V2.x */
1979 if (!list_empty (&peer
->ksnp_conns
)) {
1980 conn
= list_entry(peer
->ksnp_conns
.next
,
1981 ksock_conn_t
, ksnc_list
);
1982 LASSERT (conn
->ksnc_proto
== &ksocknal_protocol_v3x
);
1985 /* take all the blocked packets while I've got the lock and
1986 * complete below... */
1987 list_splice_init(&peer
->ksnp_tx_queue
, &zombies
);
1990 #if 0 /* irrelevant with only eager routes */
1991 if (!route
->ksnr_deleted
) {
1992 /* make this route least-favourite for re-selection */
1993 list_del(&route
->ksnr_list
);
1994 list_add_tail(&route
->ksnr_list
, &peer
->ksnp_routes
);
1997 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
1999 ksocknal_peer_failed(peer
);
2000 ksocknal_txlist_done(peer
->ksnp_ni
, &zombies
, 1);
2005 * check whether we need to create more connds.
2006 * It will try to create new thread if it's necessary, @timeout can
2007 * be updated if failed to create, so caller wouldn't keep try while
2008 * running out of resource.
2011 ksocknal_connd_check_start(long sec
, long *timeout
)
2015 int total
= ksocknal_data
.ksnd_connd_starting
+
2016 ksocknal_data
.ksnd_connd_running
;
2018 if (unlikely(ksocknal_data
.ksnd_init
< SOCKNAL_INIT_ALL
)) {
2019 /* still in initializing */
2023 if (total
>= *ksocknal_tunables
.ksnd_nconnds_max
||
2024 total
> ksocknal_data
.ksnd_connd_connecting
+ SOCKNAL_CONND_RESV
) {
2025 /* can't create more connd, or still have enough
2026 * threads to handle more connecting */
2030 if (list_empty(&ksocknal_data
.ksnd_connd_routes
)) {
2031 /* no pending connecting request */
2035 if (sec
- ksocknal_data
.ksnd_connd_failed_stamp
<= 1) {
2036 /* may run out of resource, retry later */
2037 *timeout
= cfs_time_seconds(1);
2041 if (ksocknal_data
.ksnd_connd_starting
> 0) {
2042 /* serialize starting to avoid flood */
2046 ksocknal_data
.ksnd_connd_starting_stamp
= sec
;
2047 ksocknal_data
.ksnd_connd_starting
++;
2048 spin_unlock_bh(&ksocknal_data
.ksnd_connd_lock
);
2050 /* NB: total is the next id */
2051 snprintf(name
, sizeof(name
), "socknal_cd%02d", total
);
2052 rc
= ksocknal_thread_start(ksocknal_connd
, NULL
, name
);
2054 spin_lock_bh(&ksocknal_data
.ksnd_connd_lock
);
2059 LASSERT(ksocknal_data
.ksnd_connd_starting
> 0);
2060 ksocknal_data
.ksnd_connd_starting
--;
2061 ksocknal_data
.ksnd_connd_failed_stamp
= get_seconds();
2067 * check whether current thread can exit, it will return 1 if there are too
2068 * many threads and no creating in past 120 seconds.
2069 * Also, this function may update @timeout to make caller come back
2070 * again to recheck these conditions.
2073 ksocknal_connd_check_stop(long sec
, long *timeout
)
2077 if (unlikely(ksocknal_data
.ksnd_init
< SOCKNAL_INIT_ALL
)) {
2078 /* still in initializing */
2082 if (ksocknal_data
.ksnd_connd_starting
> 0) {
2083 /* in progress of starting new thread */
2087 if (ksocknal_data
.ksnd_connd_running
<=
2088 *ksocknal_tunables
.ksnd_nconnds
) { /* can't shrink */
2092 /* created thread in past 120 seconds? */
2093 val
= (int)(ksocknal_data
.ksnd_connd_starting_stamp
+
2094 SOCKNAL_CONND_TIMEOUT
- sec
);
2096 *timeout
= (val
> 0) ? cfs_time_seconds(val
) :
2097 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT
);
2101 /* no creating in past 120 seconds */
2103 return ksocknal_data
.ksnd_connd_running
>
2104 ksocknal_data
.ksnd_connd_connecting
+ SOCKNAL_CONND_RESV
;
2107 /* Go through connd_routes queue looking for a route that we can process
2108 * right now, @timeout_p can be updated if we need to come back later */
2109 static ksock_route_t
*
2110 ksocknal_connd_get_route_locked(signed long *timeout_p
)
2112 ksock_route_t
*route
;
2115 now
= cfs_time_current();
2117 /* connd_routes can contain both pending and ordinary routes */
2118 list_for_each_entry (route
, &ksocknal_data
.ksnd_connd_routes
,
2121 if (route
->ksnr_retry_interval
== 0 ||
2122 cfs_time_aftereq(now
, route
->ksnr_timeout
))
2125 if (*timeout_p
== MAX_SCHEDULE_TIMEOUT
||
2126 (int)*timeout_p
> (int)(route
->ksnr_timeout
- now
))
2127 *timeout_p
= (int)(route
->ksnr_timeout
- now
);
2134 ksocknal_connd (void *arg
)
2136 spinlock_t
*connd_lock
= &ksocknal_data
.ksnd_connd_lock
;
2137 ksock_connreq_t
*cr
;
2142 cfs_block_allsigs ();
2144 init_waitqueue_entry(&wait
, current
);
2146 spin_lock_bh(connd_lock
);
2148 LASSERT(ksocknal_data
.ksnd_connd_starting
> 0);
2149 ksocknal_data
.ksnd_connd_starting
--;
2150 ksocknal_data
.ksnd_connd_running
++;
2152 while (!ksocknal_data
.ksnd_shuttingdown
) {
2153 ksock_route_t
*route
= NULL
;
2154 long sec
= get_seconds();
2155 long timeout
= MAX_SCHEDULE_TIMEOUT
;
2156 int dropped_lock
= 0;
2158 if (ksocknal_connd_check_stop(sec
, &timeout
)) {
2159 /* wakeup another one to check stop */
2160 wake_up(&ksocknal_data
.ksnd_connd_waitq
);
2164 if (ksocknal_connd_check_start(sec
, &timeout
)) {
2165 /* created new thread */
2169 if (!list_empty(&ksocknal_data
.ksnd_connd_connreqs
)) {
2170 /* Connection accepted by the listener */
2171 cr
= list_entry(ksocknal_data
.ksnd_connd_connreqs
. \
2172 next
, ksock_connreq_t
, ksncr_list
);
2174 list_del(&cr
->ksncr_list
);
2175 spin_unlock_bh(connd_lock
);
2178 ksocknal_create_conn(cr
->ksncr_ni
, NULL
,
2179 cr
->ksncr_sock
, SOCKLND_CONN_NONE
);
2180 lnet_ni_decref(cr
->ksncr_ni
);
2181 LIBCFS_FREE(cr
, sizeof(*cr
));
2183 spin_lock_bh(connd_lock
);
2186 /* Only handle an outgoing connection request if there
2187 * is a thread left to handle incoming connections and
2188 * create new connd */
2189 if (ksocknal_data
.ksnd_connd_connecting
+ SOCKNAL_CONND_RESV
<
2190 ksocknal_data
.ksnd_connd_running
) {
2191 route
= ksocknal_connd_get_route_locked(&timeout
);
2193 if (route
!= NULL
) {
2194 list_del (&route
->ksnr_connd_list
);
2195 ksocknal_data
.ksnd_connd_connecting
++;
2196 spin_unlock_bh(connd_lock
);
2199 if (ksocknal_connect(route
)) {
2200 /* consecutive retry */
2201 if (cons_retry
++ > SOCKNAL_INSANITY_RECONN
) {
2202 CWARN("massive consecutive "
2203 "re-connecting to %pI4h\n",
2204 &route
->ksnr_ipaddr
);
2211 ksocknal_route_decref(route
);
2213 spin_lock_bh(connd_lock
);
2214 ksocknal_data
.ksnd_connd_connecting
--;
2218 if (++nloops
< SOCKNAL_RESCHED
)
2220 spin_unlock_bh(connd_lock
);
2223 spin_lock_bh(connd_lock
);
2227 /* Nothing to do for 'timeout' */
2228 set_current_state(TASK_INTERRUPTIBLE
);
2229 add_wait_queue_exclusive(&ksocknal_data
.ksnd_connd_waitq
, &wait
);
2230 spin_unlock_bh(connd_lock
);
2233 schedule_timeout(timeout
);
2235 set_current_state(TASK_RUNNING
);
2236 remove_wait_queue(&ksocknal_data
.ksnd_connd_waitq
, &wait
);
2237 spin_lock_bh(connd_lock
);
2239 ksocknal_data
.ksnd_connd_running
--;
2240 spin_unlock_bh(connd_lock
);
2242 ksocknal_thread_fini();
2246 static ksock_conn_t
*
2247 ksocknal_find_timed_out_conn (ksock_peer_t
*peer
)
2249 /* We're called with a shared lock on ksnd_global_lock */
2251 struct list_head
*ctmp
;
2253 list_for_each (ctmp
, &peer
->ksnp_conns
) {
2255 conn
= list_entry (ctmp
, ksock_conn_t
, ksnc_list
);
2257 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2258 LASSERT (!conn
->ksnc_closing
);
2260 /* SOCK_ERROR will reset error code of socket in
2261 * some platform (like Darwin8.x) */
2262 error
= conn
->ksnc_sock
->sk
->sk_err
;
2264 ksocknal_conn_addref(conn
);
2268 CNETERR("A connection with %s "
2269 "(%pI4h:%d) was reset; "
2270 "it may have rebooted.\n",
2271 libcfs_id2str(peer
->ksnp_id
),
2276 CNETERR("A connection with %s "
2277 "(%pI4h:%d) timed out; the "
2278 "network or node may be down.\n",
2279 libcfs_id2str(peer
->ksnp_id
),
2284 CNETERR("An unexpected network error %d "
2286 "(%pI4h:%d\n", error
,
2287 libcfs_id2str(peer
->ksnp_id
),
2296 if (conn
->ksnc_rx_started
&&
2297 cfs_time_aftereq(cfs_time_current(),
2298 conn
->ksnc_rx_deadline
)) {
2299 /* Timed out incomplete incoming message */
2300 ksocknal_conn_addref(conn
);
2301 CNETERR("Timeout receiving from %s (%pI4h:%d), "
2302 "state %d wanted %d left %d\n",
2303 libcfs_id2str(peer
->ksnp_id
),
2306 conn
->ksnc_rx_state
,
2307 conn
->ksnc_rx_nob_wanted
,
2308 conn
->ksnc_rx_nob_left
);
2312 if ((!list_empty(&conn
->ksnc_tx_queue
) ||
2313 conn
->ksnc_sock
->sk
->sk_wmem_queued
!= 0) &&
2314 cfs_time_aftereq(cfs_time_current(),
2315 conn
->ksnc_tx_deadline
)) {
2316 /* Timed out messages queued for sending or
2317 * buffered in the socket's send buffer */
2318 ksocknal_conn_addref(conn
);
2319 CNETERR("Timeout sending data to %s (%pI4h:%d) "
2320 "the network or that node may be down.\n",
2321 libcfs_id2str(peer
->ksnp_id
),
2332 ksocknal_flush_stale_txs(ksock_peer_t
*peer
)
2335 LIST_HEAD (stale_txs
);
2337 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
2339 while (!list_empty (&peer
->ksnp_tx_queue
)) {
2340 tx
= list_entry (peer
->ksnp_tx_queue
.next
,
2341 ksock_tx_t
, tx_list
);
2343 if (!cfs_time_aftereq(cfs_time_current(),
2347 list_del (&tx
->tx_list
);
2348 list_add_tail (&tx
->tx_list
, &stale_txs
);
2351 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
2353 ksocknal_txlist_done(peer
->ksnp_ni
, &stale_txs
, 1);
2357 ksocknal_send_keepalive_locked(ksock_peer_t
*peer
)
2359 ksock_sched_t
*sched
;
2363 if (list_empty(&peer
->ksnp_conns
)) /* last_alive will be updated by create_conn */
2366 if (peer
->ksnp_proto
!= &ksocknal_protocol_v3x
)
2369 if (*ksocknal_tunables
.ksnd_keepalive
<= 0 ||
2370 time_before(cfs_time_current(),
2371 cfs_time_add(peer
->ksnp_last_alive
,
2372 cfs_time_seconds(*ksocknal_tunables
.ksnd_keepalive
))))
2375 if (time_before(cfs_time_current(), peer
->ksnp_send_keepalive
))
2378 /* retry 10 secs later, so we wouldn't put pressure
2379 * on this peer if we failed to send keepalive this time */
2380 peer
->ksnp_send_keepalive
= cfs_time_shift(10);
2382 conn
= ksocknal_find_conn_locked(peer
, NULL
, 1);
2384 sched
= conn
->ksnc_scheduler
;
2386 spin_lock_bh(&sched
->kss_lock
);
2387 if (!list_empty(&conn
->ksnc_tx_queue
)) {
2388 spin_unlock_bh(&sched
->kss_lock
);
2389 /* there is an queued ACK, don't need keepalive */
2393 spin_unlock_bh(&sched
->kss_lock
);
2396 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2398 /* cookie = 1 is reserved for keepalive PING */
2399 tx
= ksocknal_alloc_tx_noop(1, 1);
2401 read_lock(&ksocknal_data
.ksnd_global_lock
);
2405 if (ksocknal_launch_packet(peer
->ksnp_ni
, tx
, peer
->ksnp_id
) == 0) {
2406 read_lock(&ksocknal_data
.ksnd_global_lock
);
2410 ksocknal_free_tx(tx
);
2411 read_lock(&ksocknal_data
.ksnd_global_lock
);
2418 ksocknal_check_peer_timeouts (int idx
)
2420 struct list_head
*peers
= &ksocknal_data
.ksnd_peers
[idx
];
2426 /* NB. We expect to have a look at all the peers and not find any
2427 * connections to time out, so we just use a shared lock while we
2429 read_lock(&ksocknal_data
.ksnd_global_lock
);
2431 list_for_each_entry(peer
, peers
, ksnp_list
) {
2432 unsigned long deadline
= 0;
2436 if (ksocknal_send_keepalive_locked(peer
) != 0) {
2437 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2441 conn
= ksocknal_find_timed_out_conn (peer
);
2444 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2446 ksocknal_close_conn_and_siblings (conn
, -ETIMEDOUT
);
2448 /* NB we won't find this one again, but we can't
2449 * just proceed with the next peer, since we dropped
2450 * ksnd_global_lock and it might be dead already! */
2451 ksocknal_conn_decref(conn
);
2455 /* we can't process stale txs right here because we're
2456 * holding only shared lock */
2457 if (!list_empty (&peer
->ksnp_tx_queue
)) {
2459 list_entry (peer
->ksnp_tx_queue
.next
,
2460 ksock_tx_t
, tx_list
);
2462 if (cfs_time_aftereq(cfs_time_current(),
2465 ksocknal_peer_addref(peer
);
2466 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2468 ksocknal_flush_stale_txs(peer
);
2470 ksocknal_peer_decref(peer
);
2475 if (list_empty(&peer
->ksnp_zc_req_list
))
2478 spin_lock(&peer
->ksnp_lock
);
2479 list_for_each_entry(tx
, &peer
->ksnp_zc_req_list
, tx_zc_list
) {
2480 if (!cfs_time_aftereq(cfs_time_current(),
2483 /* ignore the TX if connection is being closed */
2484 if (tx
->tx_conn
->ksnc_closing
)
2490 spin_unlock(&peer
->ksnp_lock
);
2494 tx
= list_entry(peer
->ksnp_zc_req_list
.next
,
2495 ksock_tx_t
, tx_zc_list
);
2496 deadline
= tx
->tx_deadline
;
2497 resid
= tx
->tx_resid
;
2499 ksocknal_conn_addref(conn
);
2501 spin_unlock(&peer
->ksnp_lock
);
2502 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2504 CERROR("Total %d stale ZC_REQs for peer %s detected; the "
2505 "oldest(%p) timed out %ld secs ago, "
2506 "resid: %d, wmem: %d\n",
2507 n
, libcfs_nid2str(peer
->ksnp_id
.nid
), tx
,
2508 cfs_duration_sec(cfs_time_current() - deadline
),
2509 resid
, conn
->ksnc_sock
->sk
->sk_wmem_queued
);
2511 ksocknal_close_conn_and_siblings (conn
, -ETIMEDOUT
);
2512 ksocknal_conn_decref(conn
);
2516 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2520 ksocknal_reaper (void *arg
)
2524 ksock_sched_t
*sched
;
2525 struct list_head enomem_conns
;
2530 unsigned long deadline
= cfs_time_current();
2532 cfs_block_allsigs ();
2534 INIT_LIST_HEAD(&enomem_conns
);
2535 init_waitqueue_entry(&wait
, current
);
2537 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2539 while (!ksocknal_data
.ksnd_shuttingdown
) {
2541 if (!list_empty (&ksocknal_data
.ksnd_deathrow_conns
)) {
2542 conn
= list_entry (ksocknal_data
. \
2543 ksnd_deathrow_conns
.next
,
2544 ksock_conn_t
, ksnc_list
);
2545 list_del (&conn
->ksnc_list
);
2547 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2549 ksocknal_terminate_conn(conn
);
2550 ksocknal_conn_decref(conn
);
2552 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2556 if (!list_empty (&ksocknal_data
.ksnd_zombie_conns
)) {
2557 conn
= list_entry (ksocknal_data
.ksnd_zombie_conns
.\
2558 next
, ksock_conn_t
, ksnc_list
);
2559 list_del (&conn
->ksnc_list
);
2561 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2563 ksocknal_destroy_conn(conn
);
2565 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2569 if (!list_empty (&ksocknal_data
.ksnd_enomem_conns
)) {
2570 list_add(&enomem_conns
,
2571 &ksocknal_data
.ksnd_enomem_conns
);
2572 list_del_init(&ksocknal_data
.ksnd_enomem_conns
);
2575 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2577 /* reschedule all the connections that stalled with ENOMEM... */
2579 while (!list_empty (&enomem_conns
)) {
2580 conn
= list_entry (enomem_conns
.next
,
2581 ksock_conn_t
, ksnc_tx_list
);
2582 list_del (&conn
->ksnc_tx_list
);
2584 sched
= conn
->ksnc_scheduler
;
2586 spin_lock_bh(&sched
->kss_lock
);
2588 LASSERT(conn
->ksnc_tx_scheduled
);
2589 conn
->ksnc_tx_ready
= 1;
2590 list_add_tail(&conn
->ksnc_tx_list
,
2591 &sched
->kss_tx_conns
);
2592 wake_up(&sched
->kss_waitq
);
2594 spin_unlock_bh(&sched
->kss_lock
);
2598 /* careful with the jiffy wrap... */
2599 while ((timeout
= cfs_time_sub(deadline
,
2600 cfs_time_current())) <= 0) {
2603 int chunk
= ksocknal_data
.ksnd_peer_hash_size
;
2605 /* Time to check for timeouts on a few more peers: I do
2606 * checks every 'p' seconds on a proportion of the peer
2607 * table and I need to check every connection 'n' times
2608 * within a timeout interval, to ensure I detect a
2609 * timeout on any connection within (n+1)/n times the
2610 * timeout interval. */
2612 if (*ksocknal_tunables
.ksnd_timeout
> n
* p
)
2613 chunk
= (chunk
* n
* p
) /
2614 *ksocknal_tunables
.ksnd_timeout
;
2618 for (i
= 0; i
< chunk
; i
++) {
2619 ksocknal_check_peer_timeouts (peer_index
);
2620 peer_index
= (peer_index
+ 1) %
2621 ksocknal_data
.ksnd_peer_hash_size
;
2624 deadline
= cfs_time_add(deadline
, cfs_time_seconds(p
));
2627 if (nenomem_conns
!= 0) {
2628 /* Reduce my timeout if I rescheduled ENOMEM conns.
2629 * This also prevents me getting woken immediately
2630 * if any go back on my enomem list. */
2631 timeout
= SOCKNAL_ENOMEM_RETRY
;
2633 ksocknal_data
.ksnd_reaper_waketime
=
2634 cfs_time_add(cfs_time_current(), timeout
);
2636 set_current_state (TASK_INTERRUPTIBLE
);
2637 add_wait_queue (&ksocknal_data
.ksnd_reaper_waitq
, &wait
);
2639 if (!ksocknal_data
.ksnd_shuttingdown
&&
2640 list_empty (&ksocknal_data
.ksnd_deathrow_conns
) &&
2641 list_empty (&ksocknal_data
.ksnd_zombie_conns
))
2642 schedule_timeout(timeout
);
2644 set_current_state (TASK_RUNNING
);
2645 remove_wait_queue (&ksocknal_data
.ksnd_reaper_waitq
, &wait
);
2647 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2650 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2652 ksocknal_thread_fini();