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
= cfs_sock_wmem_queued(conn
->ksnc_sock
);
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 cfs_sock_wmem_queued(c
->ksnc_sock
);
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
= cfs_sock_wmem_queued(conn
->ksnc_sock
);
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 cfs_time_t 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: "LPU64
", "LPU64
"\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 cfs_wait_event_interruptible_exclusive(
1542 !ksocknal_sched_cansleep(sched
), rc
);
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
);
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
)
1680 case SOCKLND_CONN_ANY
:
1681 case SOCKLND_CONN_CONTROL
:
1683 case SOCKLND_CONN_BULK_IN
:
1684 return SOCKLND_CONN_BULK_OUT
;
1685 case SOCKLND_CONN_BULK_OUT
:
1686 return SOCKLND_CONN_BULK_IN
;
1688 return (SOCKLND_CONN_NONE
);
1693 ksocknal_recv_hello (lnet_ni_t
*ni
, ksock_conn_t
*conn
,
1694 ksock_hello_msg_t
*hello
, lnet_process_id_t
*peerid
,
1697 /* Return < 0 fatal error
1699 * EALREADY lost connection race
1700 * EPROTO protocol version mismatch
1702 socket_t
*sock
= conn
->ksnc_sock
;
1703 int active
= (conn
->ksnc_proto
!= NULL
);
1707 ksock_proto_t
*proto
;
1708 lnet_process_id_t recv_id
;
1710 /* socket type set on active connections - not set on passive */
1711 LASSERT (!active
== !(conn
->ksnc_type
!= SOCKLND_CONN_NONE
));
1713 timeout
= active
? *ksocknal_tunables
.ksnd_timeout
:
1714 lnet_acceptor_timeout();
1716 rc
= libcfs_sock_read(sock
, &hello
->kshm_magic
, sizeof (hello
->kshm_magic
), timeout
);
1718 CERROR("Error %d reading HELLO from %pI4h\n",
1719 rc
, &conn
->ksnc_ipaddr
);
1724 if (hello
->kshm_magic
!= LNET_PROTO_MAGIC
&&
1725 hello
->kshm_magic
!= __swab32(LNET_PROTO_MAGIC
) &&
1726 hello
->kshm_magic
!= le32_to_cpu (LNET_PROTO_TCP_MAGIC
)) {
1727 /* Unexpected magic! */
1728 CERROR("Bad magic(1) %#08x (%#08x expected) from "
1729 "%pI4h\n", __cpu_to_le32 (hello
->kshm_magic
),
1730 LNET_PROTO_TCP_MAGIC
,
1731 &conn
->ksnc_ipaddr
);
1735 rc
= libcfs_sock_read(sock
, &hello
->kshm_version
,
1736 sizeof(hello
->kshm_version
), timeout
);
1738 CERROR("Error %d reading HELLO from %pI4h\n",
1739 rc
, &conn
->ksnc_ipaddr
);
1744 proto
= ksocknal_parse_proto_version(hello
);
1745 if (proto
== NULL
) {
1747 /* unknown protocol from peer, tell peer my protocol */
1748 conn
->ksnc_proto
= &ksocknal_protocol_v3x
;
1749 #if SOCKNAL_VERSION_DEBUG
1750 if (*ksocknal_tunables
.ksnd_protocol
== 2)
1751 conn
->ksnc_proto
= &ksocknal_protocol_v2x
;
1752 else if (*ksocknal_tunables
.ksnd_protocol
== 1)
1753 conn
->ksnc_proto
= &ksocknal_protocol_v1x
;
1755 hello
->kshm_nips
= 0;
1756 ksocknal_send_hello(ni
, conn
, ni
->ni_nid
, hello
);
1759 CERROR("Unknown protocol version (%d.x expected)"
1761 conn
->ksnc_proto
->pro_version
,
1762 &conn
->ksnc_ipaddr
);
1767 proto_match
= (conn
->ksnc_proto
== proto
);
1768 conn
->ksnc_proto
= proto
;
1770 /* receive the rest of hello message anyway */
1771 rc
= conn
->ksnc_proto
->pro_recv_hello(conn
, hello
, timeout
);
1773 CERROR("Error %d reading or checking hello from from %pI4h\n",
1774 rc
, &conn
->ksnc_ipaddr
);
1779 *incarnation
= hello
->kshm_src_incarnation
;
1781 if (hello
->kshm_src_nid
== LNET_NID_ANY
) {
1782 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
1783 "from %pI4h\n", &conn
->ksnc_ipaddr
);
1788 conn
->ksnc_port
> LNET_ACCEPTOR_MAX_RESERVED_PORT
) {
1789 /* Userspace NAL assigns peer process ID from socket */
1790 recv_id
.pid
= conn
->ksnc_port
| LNET_PID_USERFLAG
;
1791 recv_id
.nid
= LNET_MKNID(LNET_NIDNET(ni
->ni_nid
), conn
->ksnc_ipaddr
);
1793 recv_id
.nid
= hello
->kshm_src_nid
;
1794 recv_id
.pid
= hello
->kshm_src_pid
;
1800 /* peer determines type */
1801 conn
->ksnc_type
= ksocknal_invert_type(hello
->kshm_ctype
);
1802 if (conn
->ksnc_type
== SOCKLND_CONN_NONE
) {
1803 CERROR("Unexpected type %d from %s ip %pI4h\n",
1804 hello
->kshm_ctype
, libcfs_id2str(*peerid
),
1805 &conn
->ksnc_ipaddr
);
1812 if (peerid
->pid
!= recv_id
.pid
||
1813 peerid
->nid
!= recv_id
.nid
) {
1814 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1815 " %pI4h, but they claimed they were "
1816 "%s; please check your Lustre "
1818 libcfs_id2str(*peerid
),
1820 libcfs_id2str(recv_id
));
1824 if (hello
->kshm_ctype
== SOCKLND_CONN_NONE
) {
1825 /* Possible protocol mismatch or I lost the connection race */
1826 return proto_match
? EALREADY
: EPROTO
;
1829 if (ksocknal_invert_type(hello
->kshm_ctype
) != conn
->ksnc_type
) {
1830 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
1831 conn
->ksnc_type
, libcfs_id2str(*peerid
),
1841 ksocknal_connect (ksock_route_t
*route
)
1843 LIST_HEAD (zombies
);
1844 ksock_peer_t
*peer
= route
->ksnr_peer
;
1848 cfs_time_t deadline
;
1849 int retry_later
= 0;
1852 deadline
= cfs_time_add(cfs_time_current(),
1853 cfs_time_seconds(*ksocknal_tunables
.ksnd_timeout
));
1855 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1857 LASSERT (route
->ksnr_scheduled
);
1858 LASSERT (!route
->ksnr_connecting
);
1860 route
->ksnr_connecting
= 1;
1863 wanted
= ksocknal_route_mask() & ~route
->ksnr_connected
;
1865 /* stop connecting if peer/route got closed under me, or
1866 * route got connected while queued */
1867 if (peer
->ksnp_closing
|| route
->ksnr_deleted
||
1873 /* reschedule if peer is connecting to me */
1874 if (peer
->ksnp_accepting
> 0) {
1876 "peer %s(%d) already connecting to me, retry later.\n",
1877 libcfs_nid2str(peer
->ksnp_id
.nid
), peer
->ksnp_accepting
);
1881 if (retry_later
) /* needs reschedule */
1884 if ((wanted
& (1 << SOCKLND_CONN_ANY
)) != 0) {
1885 type
= SOCKLND_CONN_ANY
;
1886 } else if ((wanted
& (1 << SOCKLND_CONN_CONTROL
)) != 0) {
1887 type
= SOCKLND_CONN_CONTROL
;
1888 } else if ((wanted
& (1 << SOCKLND_CONN_BULK_IN
)) != 0) {
1889 type
= SOCKLND_CONN_BULK_IN
;
1891 LASSERT ((wanted
& (1 << SOCKLND_CONN_BULK_OUT
)) != 0);
1892 type
= SOCKLND_CONN_BULK_OUT
;
1895 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
1897 if (cfs_time_aftereq(cfs_time_current(), deadline
)) {
1899 lnet_connect_console_error(rc
, peer
->ksnp_id
.nid
,
1905 rc
= lnet_connect(&sock
, peer
->ksnp_id
.nid
,
1906 route
->ksnr_myipaddr
,
1907 route
->ksnr_ipaddr
, route
->ksnr_port
);
1911 rc
= ksocknal_create_conn(peer
->ksnp_ni
, route
, sock
, type
);
1913 lnet_connect_console_error(rc
, peer
->ksnp_id
.nid
,
1919 /* A +ve RC means I have to retry because I lost the connection
1920 * race or I have to renegotiate protocol version */
1921 retry_later
= (rc
!= 0);
1923 CDEBUG(D_NET
, "peer %s: conn race, retry later.\n",
1924 libcfs_nid2str(peer
->ksnp_id
.nid
));
1926 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1929 route
->ksnr_scheduled
= 0;
1930 route
->ksnr_connecting
= 0;
1933 /* re-queue for attention; this frees me up to handle
1934 * the peer's incoming connection request */
1936 if (rc
== EALREADY
||
1937 (rc
== 0 && peer
->ksnp_accepting
> 0)) {
1938 /* We want to introduce a delay before next
1939 * attempt to connect if we lost conn race,
1940 * but the race is resolved quickly usually,
1941 * so min_reconnectms should be good heuristic */
1942 route
->ksnr_retry_interval
=
1943 cfs_time_seconds(*ksocknal_tunables
.ksnd_min_reconnectms
)/1000;
1944 route
->ksnr_timeout
= cfs_time_add(cfs_time_current(),
1945 route
->ksnr_retry_interval
);
1948 ksocknal_launch_connection_locked(route
);
1951 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
1955 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
1957 route
->ksnr_scheduled
= 0;
1958 route
->ksnr_connecting
= 0;
1960 /* This is a retry rather than a new connection */
1961 route
->ksnr_retry_interval
*= 2;
1962 route
->ksnr_retry_interval
=
1963 MAX(route
->ksnr_retry_interval
,
1964 cfs_time_seconds(*ksocknal_tunables
.ksnd_min_reconnectms
)/1000);
1965 route
->ksnr_retry_interval
=
1966 MIN(route
->ksnr_retry_interval
,
1967 cfs_time_seconds(*ksocknal_tunables
.ksnd_max_reconnectms
)/1000);
1969 LASSERT (route
->ksnr_retry_interval
!= 0);
1970 route
->ksnr_timeout
= cfs_time_add(cfs_time_current(),
1971 route
->ksnr_retry_interval
);
1973 if (!list_empty(&peer
->ksnp_tx_queue
) &&
1974 peer
->ksnp_accepting
== 0 &&
1975 ksocknal_find_connecting_route_locked(peer
) == NULL
) {
1978 /* ksnp_tx_queue is queued on a conn on successful
1979 * connection for V1.x and V2.x */
1980 if (!list_empty (&peer
->ksnp_conns
)) {
1981 conn
= list_entry(peer
->ksnp_conns
.next
,
1982 ksock_conn_t
, ksnc_list
);
1983 LASSERT (conn
->ksnc_proto
== &ksocknal_protocol_v3x
);
1986 /* take all the blocked packets while I've got the lock and
1987 * complete below... */
1988 list_splice_init(&peer
->ksnp_tx_queue
, &zombies
);
1991 #if 0 /* irrelevant with only eager routes */
1992 if (!route
->ksnr_deleted
) {
1993 /* make this route least-favourite for re-selection */
1994 list_del(&route
->ksnr_list
);
1995 list_add_tail(&route
->ksnr_list
, &peer
->ksnp_routes
);
1998 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
2000 ksocknal_peer_failed(peer
);
2001 ksocknal_txlist_done(peer
->ksnp_ni
, &zombies
, 1);
2006 * check whether we need to create more connds.
2007 * It will try to create new thread if it's necessary, @timeout can
2008 * be updated if failed to create, so caller wouldn't keep try while
2009 * running out of resource.
2012 ksocknal_connd_check_start(long sec
, long *timeout
)
2016 int total
= ksocknal_data
.ksnd_connd_starting
+
2017 ksocknal_data
.ksnd_connd_running
;
2019 if (unlikely(ksocknal_data
.ksnd_init
< SOCKNAL_INIT_ALL
)) {
2020 /* still in initializing */
2024 if (total
>= *ksocknal_tunables
.ksnd_nconnds_max
||
2025 total
> ksocknal_data
.ksnd_connd_connecting
+ SOCKNAL_CONND_RESV
) {
2026 /* can't create more connd, or still have enough
2027 * threads to handle more connecting */
2031 if (list_empty(&ksocknal_data
.ksnd_connd_routes
)) {
2032 /* no pending connecting request */
2036 if (sec
- ksocknal_data
.ksnd_connd_failed_stamp
<= 1) {
2037 /* may run out of resource, retry later */
2038 *timeout
= cfs_time_seconds(1);
2042 if (ksocknal_data
.ksnd_connd_starting
> 0) {
2043 /* serialize starting to avoid flood */
2047 ksocknal_data
.ksnd_connd_starting_stamp
= sec
;
2048 ksocknal_data
.ksnd_connd_starting
++;
2049 spin_unlock_bh(&ksocknal_data
.ksnd_connd_lock
);
2051 /* NB: total is the next id */
2052 snprintf(name
, sizeof(name
), "socknal_cd%02d", total
);
2053 rc
= ksocknal_thread_start(ksocknal_connd
, NULL
, name
);
2055 spin_lock_bh(&ksocknal_data
.ksnd_connd_lock
);
2060 LASSERT(ksocknal_data
.ksnd_connd_starting
> 0);
2061 ksocknal_data
.ksnd_connd_starting
--;
2062 ksocknal_data
.ksnd_connd_failed_stamp
= cfs_time_current_sec();
2068 * check whether current thread can exit, it will return 1 if there are too
2069 * many threads and no creating in past 120 seconds.
2070 * Also, this function may update @timeout to make caller come back
2071 * again to recheck these conditions.
2074 ksocknal_connd_check_stop(long sec
, long *timeout
)
2078 if (unlikely(ksocknal_data
.ksnd_init
< SOCKNAL_INIT_ALL
)) {
2079 /* still in initializing */
2083 if (ksocknal_data
.ksnd_connd_starting
> 0) {
2084 /* in progress of starting new thread */
2088 if (ksocknal_data
.ksnd_connd_running
<=
2089 *ksocknal_tunables
.ksnd_nconnds
) { /* can't shrink */
2093 /* created thread in past 120 seconds? */
2094 val
= (int)(ksocknal_data
.ksnd_connd_starting_stamp
+
2095 SOCKNAL_CONND_TIMEOUT
- sec
);
2097 *timeout
= (val
> 0) ? cfs_time_seconds(val
) :
2098 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT
);
2102 /* no creating in past 120 seconds */
2104 return ksocknal_data
.ksnd_connd_running
>
2105 ksocknal_data
.ksnd_connd_connecting
+ SOCKNAL_CONND_RESV
;
2108 /* Go through connd_routes queue looking for a route that we can process
2109 * right now, @timeout_p can be updated if we need to come back later */
2110 static ksock_route_t
*
2111 ksocknal_connd_get_route_locked(signed long *timeout_p
)
2113 ksock_route_t
*route
;
2116 now
= cfs_time_current();
2118 /* connd_routes can contain both pending and ordinary routes */
2119 list_for_each_entry (route
, &ksocknal_data
.ksnd_connd_routes
,
2122 if (route
->ksnr_retry_interval
== 0 ||
2123 cfs_time_aftereq(now
, route
->ksnr_timeout
))
2126 if (*timeout_p
== MAX_SCHEDULE_TIMEOUT
||
2127 (int)*timeout_p
> (int)(route
->ksnr_timeout
- now
))
2128 *timeout_p
= (int)(route
->ksnr_timeout
- now
);
2135 ksocknal_connd (void *arg
)
2137 spinlock_t
*connd_lock
= &ksocknal_data
.ksnd_connd_lock
;
2138 ksock_connreq_t
*cr
;
2143 cfs_block_allsigs ();
2145 init_waitqueue_entry(&wait
, current
);
2147 spin_lock_bh(connd_lock
);
2149 LASSERT(ksocknal_data
.ksnd_connd_starting
> 0);
2150 ksocknal_data
.ksnd_connd_starting
--;
2151 ksocknal_data
.ksnd_connd_running
++;
2153 while (!ksocknal_data
.ksnd_shuttingdown
) {
2154 ksock_route_t
*route
= NULL
;
2155 long sec
= cfs_time_current_sec();
2156 long timeout
= MAX_SCHEDULE_TIMEOUT
;
2157 int dropped_lock
= 0;
2159 if (ksocknal_connd_check_stop(sec
, &timeout
)) {
2160 /* wakeup another one to check stop */
2161 wake_up(&ksocknal_data
.ksnd_connd_waitq
);
2165 if (ksocknal_connd_check_start(sec
, &timeout
)) {
2166 /* created new thread */
2170 if (!list_empty(&ksocknal_data
.ksnd_connd_connreqs
)) {
2171 /* Connection accepted by the listener */
2172 cr
= list_entry(ksocknal_data
.ksnd_connd_connreqs
. \
2173 next
, ksock_connreq_t
, ksncr_list
);
2175 list_del(&cr
->ksncr_list
);
2176 spin_unlock_bh(connd_lock
);
2179 ksocknal_create_conn(cr
->ksncr_ni
, NULL
,
2180 cr
->ksncr_sock
, SOCKLND_CONN_NONE
);
2181 lnet_ni_decref(cr
->ksncr_ni
);
2182 LIBCFS_FREE(cr
, sizeof(*cr
));
2184 spin_lock_bh(connd_lock
);
2187 /* Only handle an outgoing connection request if there
2188 * is a thread left to handle incoming connections and
2189 * create new connd */
2190 if (ksocknal_data
.ksnd_connd_connecting
+ SOCKNAL_CONND_RESV
<
2191 ksocknal_data
.ksnd_connd_running
) {
2192 route
= ksocknal_connd_get_route_locked(&timeout
);
2194 if (route
!= NULL
) {
2195 list_del (&route
->ksnr_connd_list
);
2196 ksocknal_data
.ksnd_connd_connecting
++;
2197 spin_unlock_bh(connd_lock
);
2200 if (ksocknal_connect(route
)) {
2201 /* consecutive retry */
2202 if (cons_retry
++ > SOCKNAL_INSANITY_RECONN
) {
2203 CWARN("massive consecutive "
2204 "re-connecting to %pI4h\n",
2205 &route
->ksnr_ipaddr
);
2212 ksocknal_route_decref(route
);
2214 spin_lock_bh(connd_lock
);
2215 ksocknal_data
.ksnd_connd_connecting
--;
2219 if (++nloops
< SOCKNAL_RESCHED
)
2221 spin_unlock_bh(connd_lock
);
2224 spin_lock_bh(connd_lock
);
2228 /* Nothing to do for 'timeout' */
2229 set_current_state(TASK_INTERRUPTIBLE
);
2230 add_wait_queue_exclusive(&ksocknal_data
.ksnd_connd_waitq
, &wait
);
2231 spin_unlock_bh(connd_lock
);
2234 schedule_timeout(timeout
);
2236 set_current_state(TASK_RUNNING
);
2237 remove_wait_queue(&ksocknal_data
.ksnd_connd_waitq
, &wait
);
2238 spin_lock_bh(connd_lock
);
2240 ksocknal_data
.ksnd_connd_running
--;
2241 spin_unlock_bh(connd_lock
);
2243 ksocknal_thread_fini();
2248 ksocknal_find_timed_out_conn (ksock_peer_t
*peer
)
2250 /* We're called with a shared lock on ksnd_global_lock */
2252 struct list_head
*ctmp
;
2254 list_for_each (ctmp
, &peer
->ksnp_conns
) {
2256 conn
= list_entry (ctmp
, ksock_conn_t
, ksnc_list
);
2258 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2259 LASSERT (!conn
->ksnc_closing
);
2261 /* SOCK_ERROR will reset error code of socket in
2262 * some platform (like Darwin8.x) */
2263 error
= cfs_sock_error(conn
->ksnc_sock
);
2265 ksocknal_conn_addref(conn
);
2269 CNETERR("A connection with %s "
2270 "(%pI4h:%d) was reset; "
2271 "it may have rebooted.\n",
2272 libcfs_id2str(peer
->ksnp_id
),
2277 CNETERR("A connection with %s "
2278 "(%pI4h:%d) timed out; the "
2279 "network or node may be down.\n",
2280 libcfs_id2str(peer
->ksnp_id
),
2285 CNETERR("An unexpected network error %d "
2287 "(%pI4h:%d\n", error
,
2288 libcfs_id2str(peer
->ksnp_id
),
2297 if (conn
->ksnc_rx_started
&&
2298 cfs_time_aftereq(cfs_time_current(),
2299 conn
->ksnc_rx_deadline
)) {
2300 /* Timed out incomplete incoming message */
2301 ksocknal_conn_addref(conn
);
2302 CNETERR("Timeout receiving from %s (%pI4h:%d), "
2303 "state %d wanted %d left %d\n",
2304 libcfs_id2str(peer
->ksnp_id
),
2307 conn
->ksnc_rx_state
,
2308 conn
->ksnc_rx_nob_wanted
,
2309 conn
->ksnc_rx_nob_left
);
2313 if ((!list_empty(&conn
->ksnc_tx_queue
) ||
2314 cfs_sock_wmem_queued(conn
->ksnc_sock
) != 0) &&
2315 cfs_time_aftereq(cfs_time_current(),
2316 conn
->ksnc_tx_deadline
)) {
2317 /* Timed out messages queued for sending or
2318 * buffered in the socket's send buffer */
2319 ksocknal_conn_addref(conn
);
2320 CNETERR("Timeout sending data to %s (%pI4h:%d) "
2321 "the network or that node may be down.\n",
2322 libcfs_id2str(peer
->ksnp_id
),
2333 ksocknal_flush_stale_txs(ksock_peer_t
*peer
)
2336 LIST_HEAD (stale_txs
);
2338 write_lock_bh(&ksocknal_data
.ksnd_global_lock
);
2340 while (!list_empty (&peer
->ksnp_tx_queue
)) {
2341 tx
= list_entry (peer
->ksnp_tx_queue
.next
,
2342 ksock_tx_t
, tx_list
);
2344 if (!cfs_time_aftereq(cfs_time_current(),
2348 list_del (&tx
->tx_list
);
2349 list_add_tail (&tx
->tx_list
, &stale_txs
);
2352 write_unlock_bh(&ksocknal_data
.ksnd_global_lock
);
2354 ksocknal_txlist_done(peer
->ksnp_ni
, &stale_txs
, 1);
2358 ksocknal_send_keepalive_locked(ksock_peer_t
*peer
)
2360 ksock_sched_t
*sched
;
2364 if (list_empty(&peer
->ksnp_conns
)) /* last_alive will be updated by create_conn */
2367 if (peer
->ksnp_proto
!= &ksocknal_protocol_v3x
)
2370 if (*ksocknal_tunables
.ksnd_keepalive
<= 0 ||
2371 cfs_time_before(cfs_time_current(),
2372 cfs_time_add(peer
->ksnp_last_alive
,
2373 cfs_time_seconds(*ksocknal_tunables
.ksnd_keepalive
))))
2376 if (cfs_time_before(cfs_time_current(),
2377 peer
->ksnp_send_keepalive
))
2380 /* retry 10 secs later, so we wouldn't put pressure
2381 * on this peer if we failed to send keepalive this time */
2382 peer
->ksnp_send_keepalive
= cfs_time_shift(10);
2384 conn
= ksocknal_find_conn_locked(peer
, NULL
, 1);
2386 sched
= conn
->ksnc_scheduler
;
2388 spin_lock_bh(&sched
->kss_lock
);
2389 if (!list_empty(&conn
->ksnc_tx_queue
)) {
2390 spin_unlock_bh(&sched
->kss_lock
);
2391 /* there is an queued ACK, don't need keepalive */
2395 spin_unlock_bh(&sched
->kss_lock
);
2398 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2400 /* cookie = 1 is reserved for keepalive PING */
2401 tx
= ksocknal_alloc_tx_noop(1, 1);
2403 read_lock(&ksocknal_data
.ksnd_global_lock
);
2407 if (ksocknal_launch_packet(peer
->ksnp_ni
, tx
, peer
->ksnp_id
) == 0) {
2408 read_lock(&ksocknal_data
.ksnd_global_lock
);
2412 ksocknal_free_tx(tx
);
2413 read_lock(&ksocknal_data
.ksnd_global_lock
);
2420 ksocknal_check_peer_timeouts (int idx
)
2422 struct list_head
*peers
= &ksocknal_data
.ksnd_peers
[idx
];
2428 /* NB. We expect to have a look at all the peers and not find any
2429 * connections to time out, so we just use a shared lock while we
2431 read_lock(&ksocknal_data
.ksnd_global_lock
);
2433 list_for_each_entry(peer
, peers
, ksnp_list
) {
2434 cfs_time_t deadline
= 0;
2438 if (ksocknal_send_keepalive_locked(peer
) != 0) {
2439 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2443 conn
= ksocknal_find_timed_out_conn (peer
);
2446 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2448 ksocknal_close_conn_and_siblings (conn
, -ETIMEDOUT
);
2450 /* NB we won't find this one again, but we can't
2451 * just proceed with the next peer, since we dropped
2452 * ksnd_global_lock and it might be dead already! */
2453 ksocknal_conn_decref(conn
);
2457 /* we can't process stale txs right here because we're
2458 * holding only shared lock */
2459 if (!list_empty (&peer
->ksnp_tx_queue
)) {
2461 list_entry (peer
->ksnp_tx_queue
.next
,
2462 ksock_tx_t
, tx_list
);
2464 if (cfs_time_aftereq(cfs_time_current(),
2467 ksocknal_peer_addref(peer
);
2468 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2470 ksocknal_flush_stale_txs(peer
);
2472 ksocknal_peer_decref(peer
);
2477 if (list_empty(&peer
->ksnp_zc_req_list
))
2480 spin_lock(&peer
->ksnp_lock
);
2481 list_for_each_entry(tx
, &peer
->ksnp_zc_req_list
, tx_zc_list
) {
2482 if (!cfs_time_aftereq(cfs_time_current(),
2485 /* ignore the TX if connection is being closed */
2486 if (tx
->tx_conn
->ksnc_closing
)
2492 spin_unlock(&peer
->ksnp_lock
);
2496 tx
= list_entry(peer
->ksnp_zc_req_list
.next
,
2497 ksock_tx_t
, tx_zc_list
);
2498 deadline
= tx
->tx_deadline
;
2499 resid
= tx
->tx_resid
;
2501 ksocknal_conn_addref(conn
);
2503 spin_unlock(&peer
->ksnp_lock
);
2504 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2506 CERROR("Total %d stale ZC_REQs for peer %s detected; the "
2507 "oldest(%p) timed out %ld secs ago, "
2508 "resid: %d, wmem: %d\n",
2509 n
, libcfs_nid2str(peer
->ksnp_id
.nid
), tx
,
2510 cfs_duration_sec(cfs_time_current() - deadline
),
2511 resid
, cfs_sock_wmem_queued(conn
->ksnc_sock
));
2513 ksocknal_close_conn_and_siblings (conn
, -ETIMEDOUT
);
2514 ksocknal_conn_decref(conn
);
2518 read_unlock(&ksocknal_data
.ksnd_global_lock
);
2522 ksocknal_reaper (void *arg
)
2526 ksock_sched_t
*sched
;
2527 struct list_head enomem_conns
;
2529 cfs_duration_t timeout
;
2532 cfs_time_t deadline
= cfs_time_current();
2534 cfs_block_allsigs ();
2536 INIT_LIST_HEAD(&enomem_conns
);
2537 init_waitqueue_entry(&wait
, current
);
2539 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2541 while (!ksocknal_data
.ksnd_shuttingdown
) {
2543 if (!list_empty (&ksocknal_data
.ksnd_deathrow_conns
)) {
2544 conn
= list_entry (ksocknal_data
. \
2545 ksnd_deathrow_conns
.next
,
2546 ksock_conn_t
, ksnc_list
);
2547 list_del (&conn
->ksnc_list
);
2549 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2551 ksocknal_terminate_conn(conn
);
2552 ksocknal_conn_decref(conn
);
2554 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2558 if (!list_empty (&ksocknal_data
.ksnd_zombie_conns
)) {
2559 conn
= list_entry (ksocknal_data
.ksnd_zombie_conns
.\
2560 next
, ksock_conn_t
, ksnc_list
);
2561 list_del (&conn
->ksnc_list
);
2563 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2565 ksocknal_destroy_conn(conn
);
2567 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2571 if (!list_empty (&ksocknal_data
.ksnd_enomem_conns
)) {
2572 list_add(&enomem_conns
,
2573 &ksocknal_data
.ksnd_enomem_conns
);
2574 list_del_init(&ksocknal_data
.ksnd_enomem_conns
);
2577 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2579 /* reschedule all the connections that stalled with ENOMEM... */
2581 while (!list_empty (&enomem_conns
)) {
2582 conn
= list_entry (enomem_conns
.next
,
2583 ksock_conn_t
, ksnc_tx_list
);
2584 list_del (&conn
->ksnc_tx_list
);
2586 sched
= conn
->ksnc_scheduler
;
2588 spin_lock_bh(&sched
->kss_lock
);
2590 LASSERT(conn
->ksnc_tx_scheduled
);
2591 conn
->ksnc_tx_ready
= 1;
2592 list_add_tail(&conn
->ksnc_tx_list
,
2593 &sched
->kss_tx_conns
);
2594 wake_up(&sched
->kss_waitq
);
2596 spin_unlock_bh(&sched
->kss_lock
);
2600 /* careful with the jiffy wrap... */
2601 while ((timeout
= cfs_time_sub(deadline
,
2602 cfs_time_current())) <= 0) {
2605 int chunk
= ksocknal_data
.ksnd_peer_hash_size
;
2607 /* Time to check for timeouts on a few more peers: I do
2608 * checks every 'p' seconds on a proportion of the peer
2609 * table and I need to check every connection 'n' times
2610 * within a timeout interval, to ensure I detect a
2611 * timeout on any connection within (n+1)/n times the
2612 * timeout interval. */
2614 if (*ksocknal_tunables
.ksnd_timeout
> n
* p
)
2615 chunk
= (chunk
* n
* p
) /
2616 *ksocknal_tunables
.ksnd_timeout
;
2620 for (i
= 0; i
< chunk
; i
++) {
2621 ksocknal_check_peer_timeouts (peer_index
);
2622 peer_index
= (peer_index
+ 1) %
2623 ksocknal_data
.ksnd_peer_hash_size
;
2626 deadline
= cfs_time_add(deadline
, cfs_time_seconds(p
));
2629 if (nenomem_conns
!= 0) {
2630 /* Reduce my timeout if I rescheduled ENOMEM conns.
2631 * This also prevents me getting woken immediately
2632 * if any go back on my enomem list. */
2633 timeout
= SOCKNAL_ENOMEM_RETRY
;
2635 ksocknal_data
.ksnd_reaper_waketime
=
2636 cfs_time_add(cfs_time_current(), timeout
);
2638 set_current_state (TASK_INTERRUPTIBLE
);
2639 add_wait_queue (&ksocknal_data
.ksnd_reaper_waitq
, &wait
);
2641 if (!ksocknal_data
.ksnd_shuttingdown
&&
2642 list_empty (&ksocknal_data
.ksnd_deathrow_conns
) &&
2643 list_empty (&ksocknal_data
.ksnd_zombie_conns
))
2644 schedule_timeout(timeout
);
2646 set_current_state (TASK_RUNNING
);
2647 remove_wait_queue (&ksocknal_data
.ksnd_reaper_waitq
, &wait
);
2649 spin_lock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2652 spin_unlock_bh(&ksocknal_data
.ksnd_reaper_lock
);
2654 ksocknal_thread_fini();