2 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Copyright (c) 2011, 2017, 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 Lustre, https://wiki.whamcloud.com/
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.
27 #include <libcfs/linux/linux-mem.h>
29 #include <linux/sunrpc/addr.h>
32 ksocknal_alloc_tx(int type, int size)
34 struct ksock_tx *tx = NULL;
36 if (type == KSOCK_MSG_NOOP) {
37 LASSERT(size == KSOCK_NOOP_TX_SIZE);
39 /* searching for a noop tx in free list */
40 spin_lock(&ksocknal_data.ksnd_tx_lock);
42 tx = list_first_entry_or_null(&ksocknal_data.ksnd_idle_noop_txs,
43 struct ksock_tx, tx_list);
45 LASSERT(tx->tx_desc_size == size);
46 list_del(&tx->tx_list);
49 spin_unlock(&ksocknal_data.ksnd_tx_lock);
53 LIBCFS_ALLOC(tx, size);
58 refcount_set(&tx->tx_refcount, 1);
59 tx->tx_zc_aborted = 0;
60 tx->tx_zc_capable = 0;
61 tx->tx_zc_checked = 0;
62 tx->tx_hstatus = LNET_MSG_STATUS_OK;
63 tx->tx_desc_size = size;
65 atomic_inc(&ksocknal_data.ksnd_nactive_txs);
71 ksocknal_alloc_tx_noop(__u64 cookie, int nonblk)
75 tx = ksocknal_alloc_tx(KSOCK_MSG_NOOP, KSOCK_NOOP_TX_SIZE);
77 CERROR("Can't allocate noop tx desc\n");
82 tx->tx_lnetmsg = NULL;
86 tx->tx_nonblk = nonblk;
88 tx->tx_msg.ksm_csum = 0;
89 tx->tx_msg.ksm_type = KSOCK_MSG_NOOP;
90 tx->tx_msg.ksm_zc_cookies[0] = 0;
91 tx->tx_msg.ksm_zc_cookies[1] = cookie;
98 ksocknal_free_tx(struct ksock_tx *tx)
100 atomic_dec(&ksocknal_data.ksnd_nactive_txs);
102 if (tx->tx_lnetmsg == NULL && tx->tx_desc_size == KSOCK_NOOP_TX_SIZE) {
104 spin_lock(&ksocknal_data.ksnd_tx_lock);
106 list_add(&tx->tx_list, &ksocknal_data.ksnd_idle_noop_txs);
108 spin_unlock(&ksocknal_data.ksnd_tx_lock);
110 LIBCFS_FREE(tx, tx->tx_desc_size);
115 ksocknal_send_hdr(struct ksock_conn *conn, struct ksock_tx *tx,
116 struct kvec *scratch_iov)
118 struct kvec *iov = &tx->tx_hdr;
122 LASSERT(tx->tx_niov > 0);
124 /* Never touch tx->tx_hdr inside ksocknal_lib_send_hdr() */
125 rc = ksocknal_lib_send_hdr(conn, tx, scratch_iov);
127 if (rc <= 0) /* sent nothing? */
131 LASSERT(nob <= tx->tx_resid);
135 LASSERT(tx->tx_niov == 1);
137 if (nob < (int) iov->iov_len) {
138 iov->iov_base += nob;
143 LASSERT(nob == iov->iov_len);
150 ksocknal_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx,
151 struct kvec *scratch_iov)
153 struct bio_vec *kiov = tx->tx_kiov;
157 LASSERT(tx->tx_niov == 0);
158 LASSERT(tx->tx_nkiov > 0);
160 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
161 rc = ksocknal_lib_send_kiov(conn, tx, scratch_iov);
163 if (rc <= 0) /* sent nothing? */
167 LASSERT(nob <= tx->tx_resid);
172 LASSERT(tx->tx_nkiov > 0);
174 if (nob < (int)kiov->bv_len) {
175 kiov->bv_offset += nob;
180 nob -= (int)kiov->bv_len;
181 tx->tx_kiov = ++kiov;
189 ksocknal_transmit(struct ksock_conn *conn, struct ksock_tx *tx,
190 struct kvec *scratch_iov)
195 if (ksocknal_data.ksnd_stall_tx != 0)
196 schedule_timeout_uninterruptible(
197 cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
199 LASSERT(tx->tx_resid != 0);
201 rc = ksocknal_connsock_addref(conn);
203 LASSERT(conn->ksnc_closing);
208 if (ksocknal_data.ksnd_enomem_tx > 0) {
210 ksocknal_data.ksnd_enomem_tx--;
212 } else if (tx->tx_niov != 0) {
213 rc = ksocknal_send_hdr(conn, tx, scratch_iov);
215 rc = ksocknal_send_kiov(conn, tx, scratch_iov);
218 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
219 if (rc > 0) /* sent something? */
220 conn->ksnc_tx_bufnob += rc; /* account it */
222 if (bufnob < conn->ksnc_tx_bufnob) {
223 /* allocated send buffer bytes < computed; infer
224 * something got ACKed */
225 conn->ksnc_tx_deadline = ktime_get_seconds() +
227 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
228 conn->ksnc_tx_bufnob = bufnob;
232 if (rc <= 0) { /* Didn't write anything? */
233 /* some stacks return 0 instead of -EAGAIN */
237 /* Check if EAGAIN is due to memory pressure */
238 if (rc == -EAGAIN && ksocknal_lib_memory_pressure(conn))
244 /* socket's wmem_queued now includes 'rc' bytes */
245 atomic_sub (rc, &conn->ksnc_tx_nob);
248 } while (tx->tx_resid != 0);
250 ksocknal_connsock_decref(conn);
255 ksocknal_recv_iov(struct ksock_conn *conn, struct kvec *scratchiov)
257 struct kvec *iov = conn->ksnc_rx_iov;
261 LASSERT(conn->ksnc_rx_niov > 0);
263 /* Never touch conn->ksnc_rx_iov or change connection
264 * status inside ksocknal_lib_recv_iov */
265 rc = ksocknal_lib_recv_iov(conn, scratchiov);
270 /* received something... */
273 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
274 conn->ksnc_rx_deadline = ktime_get_seconds() +
276 smp_mb(); /* order with setting rx_started */
277 conn->ksnc_rx_started = 1;
279 conn->ksnc_rx_nob_wanted -= nob;
280 conn->ksnc_rx_nob_left -= nob;
283 LASSERT(conn->ksnc_rx_niov > 0);
285 if (nob < (int)iov->iov_len) {
287 iov->iov_base += nob;
292 conn->ksnc_rx_iov = ++iov;
293 conn->ksnc_rx_niov--;
300 ksocknal_recv_kiov(struct ksock_conn *conn, struct page **rx_scratch_pgs,
301 struct kvec *scratch_iov)
303 struct bio_vec *kiov = conn->ksnc_rx_kiov;
307 LASSERT(conn->ksnc_rx_nkiov > 0);
308 /* Never touch conn->ksnc_rx_kiov or change connection
309 * status inside ksocknal_lib_recv_iov */
310 rc = ksocknal_lib_recv_kiov(conn, rx_scratch_pgs, scratch_iov);
315 /* received something... */
318 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
319 conn->ksnc_rx_deadline = ktime_get_seconds() +
321 smp_mb(); /* order with setting rx_started */
322 conn->ksnc_rx_started = 1;
324 conn->ksnc_rx_nob_wanted -= nob;
325 conn->ksnc_rx_nob_left -= nob;
328 LASSERT(conn->ksnc_rx_nkiov > 0);
330 if (nob < (int) kiov->bv_len) {
331 kiov->bv_offset += nob;
337 conn->ksnc_rx_kiov = ++kiov;
338 conn->ksnc_rx_nkiov--;
345 ksocknal_receive(struct ksock_conn *conn, struct page **rx_scratch_pgs,
346 struct kvec *scratch_iov)
348 /* Return 1 on success, 0 on EOF, < 0 on error.
349 * Caller checks ksnc_rx_nob_wanted to determine
350 * progress/completion. */
354 if (ksocknal_data.ksnd_stall_rx != 0)
355 schedule_timeout_uninterruptible(
356 cfs_time_seconds(ksocknal_data.ksnd_stall_rx));
358 rc = ksocknal_connsock_addref(conn);
360 LASSERT(conn->ksnc_closing);
365 if (conn->ksnc_rx_niov != 0)
366 rc = ksocknal_recv_iov(conn, scratch_iov);
368 rc = ksocknal_recv_kiov(conn, rx_scratch_pgs,
372 /* error/EOF or partial receive */
375 } else if (rc == 0 && conn->ksnc_rx_started) {
376 /* EOF in the middle of a message */
382 /* Completed a fragment */
384 if (conn->ksnc_rx_nob_wanted == 0) {
390 ksocknal_connsock_decref(conn);
395 ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx, int rc)
397 struct lnet_msg *lnetmsg = tx->tx_lnetmsg;
398 enum lnet_msg_hstatus hstatus = tx->tx_hstatus;
400 LASSERT(ni != NULL || tx->tx_conn != NULL);
402 if (!rc && (tx->tx_resid != 0 || tx->tx_zc_aborted)) {
404 if (hstatus == LNET_MSG_STATUS_OK)
405 hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
408 if (tx->tx_conn != NULL)
409 ksocknal_conn_decref(tx->tx_conn);
411 ksocknal_free_tx(tx);
412 if (lnetmsg != NULL) { /* KSOCK_MSG_NOOP go without lnetmsg */
413 lnetmsg->msg_health_status = hstatus;
414 lnet_finalize(lnetmsg, rc);
419 ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist, int error)
423 while ((tx = list_first_entry_or_null(txlist, struct ksock_tx,
425 if (error && tx->tx_lnetmsg) {
426 CNETERR("Deleting packet type %d len %d %s->%s\n",
427 tx->tx_lnetmsg->msg_type,
428 tx->tx_lnetmsg->msg_len,
429 libcfs_nidstr(&tx->tx_lnetmsg->msg_initiator),
430 libcfs_nidstr(&tx->tx_lnetmsg->msg_target.nid));
432 CNETERR("Deleting noop packet\n");
435 list_del(&tx->tx_list);
437 if (tx->tx_hstatus == LNET_MSG_STATUS_OK) {
438 if (error == -ETIMEDOUT)
440 LNET_MSG_STATUS_LOCAL_TIMEOUT;
441 else if (error == -ENETDOWN ||
442 error == -EHOSTUNREACH ||
443 error == -ENETUNREACH ||
444 error == -ECONNREFUSED ||
445 error == -ECONNRESET)
446 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
448 * for all other errors we don't want to
452 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
455 LASSERT(refcount_read(&tx->tx_refcount) == 1);
456 ksocknal_tx_done(ni, tx, error);
461 ksocknal_check_zc_req(struct ksock_tx *tx)
463 struct ksock_conn *conn = tx->tx_conn;
464 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
466 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
467 * to ksnp_zc_req_list if some fragment of this message should be sent
468 * zero-copy. Our peer_ni will send an ACK containing this cookie when
469 * she has received this message to tell us we can signal completion.
470 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
471 * ksnp_zc_req_list. */
472 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
473 LASSERT (tx->tx_zc_capable);
475 tx->tx_zc_checked = 1;
477 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
478 !conn->ksnc_zc_capable)
481 /* assign cookie and queue tx to pending list, it will be released when
482 * a matching ack is received. See ksocknal_handle_zcack() */
484 ksocknal_tx_addref(tx);
486 spin_lock(&peer_ni->ksnp_lock);
488 /* ZC_REQ is going to be pinned to the peer_ni */
489 tx->tx_deadline = ktime_get_seconds() +
492 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
494 tx->tx_msg.ksm_zc_cookies[0] = peer_ni->ksnp_zc_next_cookie++;
496 if (peer_ni->ksnp_zc_next_cookie == 0)
497 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
499 list_add_tail(&tx->tx_zc_list, &peer_ni->ksnp_zc_req_list);
501 spin_unlock(&peer_ni->ksnp_lock);
505 ksocknal_uncheck_zc_req(struct ksock_tx *tx)
507 struct ksock_peer_ni *peer_ni = tx->tx_conn->ksnc_peer;
509 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
510 LASSERT(tx->tx_zc_capable);
512 tx->tx_zc_checked = 0;
514 spin_lock(&peer_ni->ksnp_lock);
516 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
517 /* Not waiting for an ACK */
518 spin_unlock(&peer_ni->ksnp_lock);
522 tx->tx_msg.ksm_zc_cookies[0] = 0;
523 list_del(&tx->tx_zc_list);
525 spin_unlock(&peer_ni->ksnp_lock);
527 ksocknal_tx_decref(tx);
531 ksocknal_process_transmit(struct ksock_conn *conn, struct ksock_tx *tx,
532 struct kvec *scratch_iov)
535 bool error_sim = false;
537 if (lnet_send_error_simulation(tx->tx_lnetmsg, &tx->tx_hstatus)) {
543 if (tx->tx_zc_capable && !tx->tx_zc_checked)
544 ksocknal_check_zc_req(tx);
546 rc = ksocknal_transmit(conn, tx, scratch_iov);
548 CDEBUG(D_NET, "send(%d) %d\n", tx->tx_resid, rc);
550 if (tx->tx_resid == 0) {
551 /* Sent everything OK */
563 counter++; /* exponential backoff warnings */
564 if ((counter & (-counter)) == counter)
565 CWARN("%u ENOMEM tx %p (%lld allocated)\n",
566 counter, conn, libcfs_kmem_read());
568 /* Queue on ksnd_enomem_conns for retry after a timeout */
569 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
571 /* enomem list takes over scheduler's ref... */
572 LASSERT(conn->ksnc_tx_scheduled);
573 list_add_tail(&conn->ksnc_tx_list,
574 &ksocknal_data.ksnd_enomem_conns);
575 if (ktime_get_seconds() + SOCKNAL_ENOMEM_RETRY <
576 ksocknal_data.ksnd_reaper_waketime)
577 wake_up(&ksocknal_data.ksnd_reaper_waitq);
579 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
582 * set the health status of the message which determines
583 * whether we should retry the transmit
585 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
596 * set the health status of the message which determines
597 * whether we should retry the transmit
599 if (rc == -ETIMEDOUT)
600 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_TIMEOUT;
602 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
605 if (!conn->ksnc_closing) {
608 LCONSOLE_WARN("Host %pIS reset our connection while we were sending data; it may have rebooted.\n",
609 &conn->ksnc_peeraddr);
612 LCONSOLE_WARN("There was an unexpected network error while writing to %pIS: %d.\n",
613 &conn->ksnc_peeraddr, rc);
616 CDEBUG(D_NET, "[%p] Error %d on write to %s ip %pISp\n",
617 conn, rc, libcfs_idstr(&conn->ksnc_peer->ksnp_id),
618 &conn->ksnc_peeraddr);
621 if (tx->tx_zc_checked)
622 ksocknal_uncheck_zc_req(tx);
624 /* it's not an error if conn is being closed */
625 ksocknal_close_conn_and_siblings(conn,
626 (conn->ksnc_closing) ? 0 : rc);
632 ksocknal_launch_connection_locked(struct ksock_conn_cb *conn_cb)
634 /* called holding write lock on ksnd_global_lock */
636 LASSERT(!conn_cb->ksnr_scheduled);
637 LASSERT(!conn_cb->ksnr_connecting);
638 LASSERT((ksocknal_conn_cb_mask() & ~conn_cb->ksnr_connected) != 0);
640 /* scheduling conn for connd */
641 conn_cb->ksnr_scheduled = 1;
643 /* extra ref for connd */
644 ksocknal_conn_cb_addref(conn_cb);
646 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
648 list_add_tail(&conn_cb->ksnr_connd_list,
649 &ksocknal_data.ksnd_connd_routes);
650 wake_up(&ksocknal_data.ksnd_connd_waitq);
652 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
656 ksocknal_launch_all_connections_locked(struct ksock_peer_ni *peer_ni)
658 struct ksock_conn_cb *conn_cb;
660 /* called holding write lock on ksnd_global_lock */
662 /* launch any/all connections that need it */
663 conn_cb = ksocknal_find_connectable_conn_cb_locked(peer_ni);
667 ksocknal_launch_connection_locked(conn_cb);
672 ksocknal_find_conn_locked(struct ksock_peer_ni *peer_ni, struct ksock_tx *tx, int nonblk)
674 struct ksock_conn *c;
675 struct ksock_conn *conn;
676 struct ksock_conn *typed = NULL;
677 struct ksock_conn *fallback = NULL;
681 list_for_each_entry(c, &peer_ni->ksnp_conns, ksnc_list) {
682 int nob = atomic_read(&c->ksnc_tx_nob) +
683 c->ksnc_sock->sk->sk_wmem_queued;
686 LASSERT (!c->ksnc_closing);
687 LASSERT (c->ksnc_proto != NULL &&
688 c->ksnc_proto->pro_match_tx != NULL);
690 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
695 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
698 case SOCKNAL_MATCH_YES: /* typed connection */
699 if (typed == NULL || tnob > nob ||
700 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
701 typed->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
707 case SOCKNAL_MATCH_MAY: /* fallback connection */
708 if (fallback == NULL || fnob > nob ||
709 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
710 fallback->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
718 /* prefer the typed selection */
719 conn = (typed != NULL) ? typed : fallback;
722 conn->ksnc_tx_last_post = ktime_get_seconds();
728 ksocknal_tx_prep(struct ksock_conn *conn, struct ksock_tx *tx)
730 conn->ksnc_proto->pro_pack(tx);
732 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
733 ksocknal_conn_addref(conn); /* +1 ref for tx */
738 ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn)
740 struct ksock_sched *sched = conn->ksnc_scheduler;
741 struct ksock_msg *msg = &tx->tx_msg;
742 struct ksock_tx *ztx = NULL;
745 /* called holding global lock (read or irq-write) and caller may
746 * not have dropped this lock between finding conn and calling me,
747 * so we don't need the {get,put}connsock dance to deref
749 LASSERT(!conn->ksnc_closing);
751 CDEBUG(D_NET, "Sending to %s ip %pISp\n",
752 libcfs_idstr(&conn->ksnc_peer->ksnp_id),
753 &conn->ksnc_peeraddr);
755 ksocknal_tx_prep(conn, tx);
757 /* Ensure the frags we've been given EXACTLY match the number of
758 * bytes we want to send. Many TCP/IP stacks disregard any total
759 * size parameters passed to them and just look at the frags.
761 * We always expect at least 1 mapped fragment containing the
762 * complete ksocknal message header.
764 LASSERT(lnet_iov_nob(tx->tx_niov, &tx->tx_hdr) +
765 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
766 (unsigned int)tx->tx_nob);
767 LASSERT(tx->tx_niov >= 1);
768 LASSERT(tx->tx_resid == tx->tx_nob);
770 CDEBUG(D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
771 tx, tx->tx_lnetmsg ? tx->tx_lnetmsg->msg_type : KSOCK_MSG_NOOP,
772 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
774 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
775 spin_lock_bh(&sched->kss_lock);
777 if (list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
778 /* First packet starts the timeout */
779 conn->ksnc_tx_deadline = ktime_get_seconds() +
781 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
782 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
783 conn->ksnc_tx_bufnob = 0;
784 smp_mb(); /* order with adding to tx_queue */
787 if (msg->ksm_type == KSOCK_MSG_NOOP) {
788 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
789 * on a normal packet so I don't need to send it */
790 LASSERT (msg->ksm_zc_cookies[1] != 0);
791 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
793 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
794 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
797 /* It's a normal packet - can it piggback a noop zc-ack that
798 * has been queued already? */
799 LASSERT (msg->ksm_zc_cookies[1] == 0);
800 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
802 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
803 /* ztx will be released later */
807 atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
808 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
811 if (conn->ksnc_tx_ready && /* able to send */
812 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
813 /* +1 ref for scheduler */
814 ksocknal_conn_addref(conn);
815 list_add_tail(&conn->ksnc_tx_list,
816 &sched->kss_tx_conns);
817 conn->ksnc_tx_scheduled = 1;
818 wake_up(&sched->kss_waitq);
821 spin_unlock_bh(&sched->kss_lock);
825 struct ksock_conn_cb *
826 ksocknal_find_connectable_conn_cb_locked(struct ksock_peer_ni *peer_ni)
828 time64_t now = ktime_get_seconds();
829 struct ksock_conn_cb *conn_cb;
831 conn_cb = peer_ni->ksnp_conn_cb;
835 LASSERT(!conn_cb->ksnr_connecting || conn_cb->ksnr_scheduled);
837 if (conn_cb->ksnr_scheduled) /* connections being established */
840 /* all conn types connected ? */
841 if ((ksocknal_conn_cb_mask() & ~conn_cb->ksnr_connected) == 0)
844 if (!(conn_cb->ksnr_retry_interval == 0 || /* first attempt */
845 now >= conn_cb->ksnr_timeout)) {
847 "Too soon to retry route %pIS (cnted %d, interval %lld, %lld secs later)\n",
849 conn_cb->ksnr_connected,
850 conn_cb->ksnr_retry_interval,
851 conn_cb->ksnr_timeout - now);
858 struct ksock_conn_cb *
859 ksocknal_find_connecting_conn_cb_locked(struct ksock_peer_ni *peer_ni)
861 struct ksock_conn_cb *conn_cb;
863 conn_cb = peer_ni->ksnp_conn_cb;
867 LASSERT(!conn_cb->ksnr_connecting || conn_cb->ksnr_scheduled);
869 return conn_cb->ksnr_scheduled ? conn_cb : NULL;
873 ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
874 struct lnet_processid *id)
876 struct ksock_peer_ni *peer_ni;
877 struct ksock_conn *conn;
878 struct sockaddr_in sa;
883 LASSERT(tx->tx_conn == NULL);
885 g_lock = &ksocknal_data.ksnd_global_lock;
887 for (retry = 0;; retry = 1) {
889 peer_ni = ksocknal_find_peer_locked(ni, id);
890 if (peer_ni != NULL) {
891 if (ksocknal_find_connectable_conn_cb_locked(peer_ni) == NULL) {
892 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
894 /* I've got nothing that need to be
895 * connecting and I do have an actual
898 ksocknal_queue_tx_locked(tx, conn);
905 /* I'll need a write lock... */
908 write_lock_bh(g_lock);
910 peer_ni = ksocknal_find_peer_locked(ni, id);
914 write_unlock_bh(g_lock);
916 if ((id->pid & LNET_PID_USERFLAG) != 0) {
917 CERROR("Refusing to create a connection to userspace process %s\n",
919 return -EHOSTUNREACH;
923 CERROR("Can't find peer_ni %s\n", libcfs_idstr(id));
924 return -EHOSTUNREACH;
927 memset(&sa, 0, sizeof(sa));
928 sa.sin_family = AF_INET;
929 sa.sin_addr.s_addr = id->nid.nid_addr[0];
930 sa.sin_port = htons(lnet_acceptor_port());
932 struct lnet_process_id id4 = {
934 .nid = lnet_nid_to_nid4(&id->nid),
936 rc = ksocknal_add_peer(ni, id4, (struct sockaddr *)&sa);
939 CERROR("Can't add peer_ni %s: %d\n",
940 libcfs_idstr(id), rc);
945 ksocknal_launch_all_connections_locked(peer_ni);
947 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
949 /* Connection exists; queue message on it */
950 ksocknal_queue_tx_locked (tx, conn);
951 write_unlock_bh(g_lock);
955 if (peer_ni->ksnp_accepting > 0 ||
956 ksocknal_find_connecting_conn_cb_locked(peer_ni) != NULL) {
957 /* the message is going to be pinned to the peer_ni */
958 tx->tx_deadline = ktime_get_seconds() +
961 /* Queue the message until a connection is established */
962 list_add_tail(&tx->tx_list, &peer_ni->ksnp_tx_queue);
963 write_unlock_bh(g_lock);
967 write_unlock_bh(g_lock);
969 /* NB Routes may be ignored if connections to them failed recently */
970 CNETERR("No usable routes to %s\n", libcfs_idstr(id));
971 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
972 return (-EHOSTUNREACH);
976 ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
978 /* '1' for consistency with code that checks !mpflag to restore */
979 unsigned int mpflag = 1;
980 int type = lntmsg->msg_type;
981 struct lnet_processid *target = &lntmsg->msg_target;
982 unsigned int payload_niov = lntmsg->msg_niov;
983 struct bio_vec *payload_kiov = lntmsg->msg_kiov;
984 unsigned int payload_offset = lntmsg->msg_offset;
985 unsigned int payload_nob = lntmsg->msg_len;
990 /* NB 'private' is different depending on what we're sending.
994 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
995 payload_nob, payload_niov, libcfs_idstr(target));
997 LASSERT (payload_nob == 0 || payload_niov > 0);
998 LASSERT (payload_niov <= LNET_MAX_IOV);
999 LASSERT (!in_interrupt ());
1001 desc_size = offsetof(struct ksock_tx,
1002 tx_payload[payload_niov]);
1004 if (lntmsg->msg_vmflush)
1005 mpflag = memalloc_noreclaim_save();
1007 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
1009 CERROR("Can't allocate tx desc type %d size %d\n",
1011 if (lntmsg->msg_vmflush)
1012 memalloc_noreclaim_restore(mpflag);
1016 tx->tx_conn = NULL; /* set when assigned a conn */
1017 tx->tx_lnetmsg = lntmsg;
1020 tx->tx_kiov = tx->tx_payload;
1021 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
1022 payload_niov, payload_kiov,
1023 payload_offset, payload_nob);
1025 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
1026 tx->tx_zc_capable = 1;
1028 tx->tx_msg.ksm_csum = 0;
1029 tx->tx_msg.ksm_type = KSOCK_MSG_LNET;
1030 tx->tx_msg.ksm_zc_cookies[0] = 0;
1031 tx->tx_msg.ksm_zc_cookies[1] = 0;
1033 /* The first fragment will be set later in pro_pack */
1034 rc = ksocknal_launch_packet(ni, tx, target);
1036 * We can't test lntsmg->msg_vmflush again as lntmsg may
1040 memalloc_noreclaim_restore(mpflag);
1045 lntmsg->msg_health_status = tx->tx_hstatus;
1046 ksocknal_free_tx(tx);
1051 ksocknal_thread_fini (void)
1053 if (atomic_dec_and_test(&ksocknal_data.ksnd_nthreads))
1054 wake_up_var(&ksocknal_data.ksnd_nthreads);
1058 ksocknal_new_packet(struct ksock_conn *conn, int nob_to_skip)
1060 static char ksocknal_slop_buffer[4096];
1065 LASSERT(conn->ksnc_proto != NULL);
1067 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1068 /* Remind the socket to ack eagerly... */
1069 ksocknal_lib_eager_ack(conn);
1072 if (nob_to_skip == 0) { /* right at next packet boundary now */
1073 conn->ksnc_rx_started = 0;
1074 smp_mb(); /* racing with timeout thread */
1076 switch (conn->ksnc_proto->pro_version) {
1077 case KSOCK_PROTO_V2:
1078 case KSOCK_PROTO_V3:
1079 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1080 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1081 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1083 conn->ksnc_rx_nob_wanted = sizeof(struct ksock_msg_hdr);
1084 conn->ksnc_rx_nob_left = sizeof(struct ksock_msg_hdr);
1085 conn->ksnc_rx_iov[0].iov_len =
1086 sizeof(struct ksock_msg_hdr);
1089 case KSOCK_PROTO_V1:
1090 /* Receiving bare struct lnet_hdr_nid4 */
1091 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1092 conn->ksnc_rx_nob_wanted = sizeof(struct lnet_hdr_nid4);
1093 conn->ksnc_rx_nob_left = sizeof(struct lnet_hdr_nid4);
1095 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1096 conn->ksnc_rx_iov[0].iov_base =
1097 (void *)&conn->ksnc_msg.ksm_u.lnetmsg_nid4;
1098 conn->ksnc_rx_iov[0].iov_len =
1099 sizeof(struct lnet_hdr_nid4);
1105 conn->ksnc_rx_niov = 1;
1107 conn->ksnc_rx_kiov = NULL;
1108 conn->ksnc_rx_nkiov = 0;
1109 conn->ksnc_rx_csum = ~0;
1113 /* Set up to skip as much as possible now. If there's more left
1114 * (ran out of iov entries) we'll get called again */
1116 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1117 conn->ksnc_rx_nob_left = nob_to_skip;
1118 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1123 nob = min_t(int, nob_to_skip, sizeof(ksocknal_slop_buffer));
1125 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1126 conn->ksnc_rx_iov[niov].iov_len = nob;
1131 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1132 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof(struct kvec));
1134 conn->ksnc_rx_niov = niov;
1135 conn->ksnc_rx_kiov = NULL;
1136 conn->ksnc_rx_nkiov = 0;
1137 conn->ksnc_rx_nob_wanted = skipped;
1142 ksocknal_process_receive(struct ksock_conn *conn,
1143 struct page **rx_scratch_pgs,
1144 struct kvec *scratch_iov)
1146 struct _lnet_hdr_nid4 *lhdr;
1147 struct lnet_processid *id;
1148 struct lnet_hdr hdr;
1151 LASSERT(refcount_read(&conn->ksnc_conn_refcount) > 0);
1153 /* NB: sched lock NOT held */
1154 /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */
1155 LASSERT(conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1156 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1157 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1158 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1160 if (conn->ksnc_rx_nob_wanted != 0) {
1161 rc = ksocknal_receive(conn, rx_scratch_pgs,
1165 struct lnet_processid *ksnp_id;
1167 ksnp_id = &conn->ksnc_peer->ksnp_id;
1169 LASSERT(rc != -EAGAIN);
1171 CDEBUG(D_NET, "[%p] EOF from %s ip %pISp\n",
1172 conn, libcfs_idstr(ksnp_id),
1173 &conn->ksnc_peeraddr);
1174 else if (!conn->ksnc_closing)
1175 CERROR("[%p] Error %d on read from %s ip %pISp\n",
1176 conn, rc, libcfs_idstr(ksnp_id),
1177 &conn->ksnc_peeraddr);
1179 /* it's not an error if conn is being closed */
1180 ksocknal_close_conn_and_siblings (conn,
1181 (conn->ksnc_closing) ? 0 : rc);
1182 return (rc == 0 ? -ESHUTDOWN : rc);
1185 if (conn->ksnc_rx_nob_wanted != 0) {
1190 switch (conn->ksnc_rx_state) {
1191 case SOCKNAL_RX_KSM_HEADER:
1192 if (conn->ksnc_flip) {
1193 __swab32s(&conn->ksnc_msg.ksm_type);
1194 __swab32s(&conn->ksnc_msg.ksm_csum);
1195 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1196 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1199 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1200 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1201 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1202 /* NOOP Checksum error */
1203 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1204 libcfs_idstr(&conn->ksnc_peer->ksnp_id),
1205 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1206 ksocknal_new_packet(conn, 0);
1207 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1211 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1214 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1216 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1217 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1219 rc = conn->ksnc_proto->pro_handle_zcack(
1220 conn, cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1223 CERROR("%s: Unknown ZC-ACK cookie: %llu, %llu\n",
1224 libcfs_idstr(&conn->ksnc_peer->ksnp_id),
1226 conn->ksnc_msg.ksm_zc_cookies[1]);
1227 ksocknal_new_packet(conn, 0);
1228 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1233 switch (conn->ksnc_msg.ksm_type) {
1234 case KSOCK_MSG_NOOP:
1235 ksocknal_new_packet(conn, 0);
1236 return 0; /* NOOP is done and just return */
1238 case KSOCK_MSG_LNET:
1240 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1241 conn->ksnc_rx_nob_wanted = sizeof(struct lnet_hdr_nid4);
1242 conn->ksnc_rx_nob_left = sizeof(struct lnet_hdr_nid4);
1244 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1245 conn->ksnc_rx_iov[0].iov_base =
1246 (void *)&conn->ksnc_msg.ksm_u.lnetmsg_nid4;
1247 conn->ksnc_rx_iov[0].iov_len =
1248 sizeof(struct lnet_hdr_nid4);
1250 conn->ksnc_rx_niov = 1;
1251 conn->ksnc_rx_kiov = NULL;
1252 conn->ksnc_rx_nkiov = 0;
1254 goto again; /* read lnet header now */
1257 CERROR("%s: Unknown message type: %x\n",
1258 libcfs_idstr(&conn->ksnc_peer->ksnp_id),
1259 conn->ksnc_msg.ksm_type);
1260 ksocknal_new_packet(conn, 0);
1261 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1265 case SOCKNAL_RX_LNET_HEADER:
1266 /* unpack message header */
1267 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1269 lnet_hdr_from_nid4(&hdr, &conn->ksnc_msg.ksm_u.lnetmsg_nid4);
1271 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1272 /* Userspace peer_ni */
1273 id = &conn->ksnc_peer->ksnp_id;
1275 /* Substitute process ID assigned at connection time */
1276 hdr.src_pid = id->pid;
1277 hdr.src_nid = lnet_nid_to_nid4(&id->nid);
1280 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1281 ksocknal_conn_addref(conn); /* ++ref while parsing */
1284 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1286 lnet_nid_to_nid4(&conn->ksnc_peer->ksnp_id.nid),
1289 /* I just received garbage: give up on this conn */
1290 ksocknal_new_packet(conn, 0);
1291 ksocknal_close_conn_and_siblings(conn, rc);
1292 ksocknal_conn_decref(conn);
1296 /* I'm racing with ksocknal_recv() */
1297 LASSERT(conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1298 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1300 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1303 /* ksocknal_recv() got called */
1306 case SOCKNAL_RX_LNET_PAYLOAD:
1307 /* payload all received */
1310 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1311 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1312 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1313 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1314 libcfs_idstr(&conn->ksnc_peer->ksnp_id),
1315 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1319 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1320 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1322 lhdr = (void *)&conn->ksnc_msg.ksm_u.lnetmsg_nid4;
1323 id = &conn->ksnc_peer->ksnp_id;
1325 rc = conn->ksnc_proto->pro_handle_zcreq(
1327 conn->ksnc_msg.ksm_zc_cookies[0],
1328 *ksocknal_tunables.ksnd_nonblk_zcack ||
1329 le64_to_cpu(lhdr->src_nid) !=
1330 lnet_nid_to_nid4(&id->nid));
1333 if (rc && conn->ksnc_lnet_msg)
1334 conn->ksnc_lnet_msg->msg_health_status =
1335 LNET_MSG_STATUS_REMOTE_ERROR;
1336 lnet_finalize(conn->ksnc_lnet_msg, rc);
1339 ksocknal_new_packet(conn, 0);
1340 ksocknal_close_conn_and_siblings(conn, rc);
1345 case SOCKNAL_RX_SLOP:
1346 /* starting new packet? */
1347 if (ksocknal_new_packet(conn, conn->ksnc_rx_nob_left))
1348 return 0; /* come back later */
1349 goto again; /* try to finish reading slop now */
1357 return (-EINVAL); /* keep gcc happy */
1361 ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
1362 int delayed, unsigned int niov,
1363 struct bio_vec *kiov, unsigned int offset, unsigned int mlen,
1366 struct ksock_conn *conn = private;
1367 struct ksock_sched *sched = conn->ksnc_scheduler;
1369 LASSERT (mlen <= rlen);
1370 LASSERT (niov <= LNET_MAX_IOV);
1372 conn->ksnc_lnet_msg = msg;
1373 conn->ksnc_rx_nob_wanted = mlen;
1374 conn->ksnc_rx_nob_left = rlen;
1377 conn->ksnc_rx_nkiov = 0;
1378 conn->ksnc_rx_kiov = NULL;
1379 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1380 conn->ksnc_rx_niov = 0;
1382 conn->ksnc_rx_niov = 0;
1383 conn->ksnc_rx_iov = NULL;
1384 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1385 conn->ksnc_rx_nkiov =
1386 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1387 niov, kiov, offset, mlen);
1391 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1392 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1394 LASSERT (conn->ksnc_rx_scheduled);
1396 spin_lock_bh(&sched->kss_lock);
1398 switch (conn->ksnc_rx_state) {
1399 case SOCKNAL_RX_PARSE_WAIT:
1400 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1401 wake_up(&sched->kss_waitq);
1402 LASSERT(conn->ksnc_rx_ready);
1405 case SOCKNAL_RX_PARSE:
1406 /* scheduler hasn't noticed I'm parsing yet */
1410 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1412 spin_unlock_bh(&sched->kss_lock);
1413 ksocknal_conn_decref(conn);
1418 ksocknal_sched_cansleep(struct ksock_sched *sched)
1422 spin_lock_bh(&sched->kss_lock);
1424 rc = (!ksocknal_data.ksnd_shuttingdown &&
1425 list_empty(&sched->kss_rx_conns) &&
1426 list_empty(&sched->kss_tx_conns));
1428 spin_unlock_bh(&sched->kss_lock);
1432 int ksocknal_scheduler(void *arg)
1434 struct ksock_sched *sched;
1435 struct ksock_conn *conn;
1436 struct ksock_tx *tx;
1438 long id = (long)arg;
1439 struct page **rx_scratch_pgs;
1440 struct kvec *scratch_iov;
1442 sched = ksocknal_data.ksnd_schedulers[KSOCK_THREAD_CPT(id)];
1444 LIBCFS_CPT_ALLOC(rx_scratch_pgs, lnet_cpt_table(), sched->kss_cpt,
1445 sizeof(*rx_scratch_pgs) * LNET_MAX_IOV);
1446 if (!rx_scratch_pgs) {
1447 CERROR("Unable to allocate scratch pages\n");
1451 LIBCFS_CPT_ALLOC(scratch_iov, lnet_cpt_table(), sched->kss_cpt,
1452 sizeof(*scratch_iov) * LNET_MAX_IOV);
1454 CERROR("Unable to allocate scratch iov\n");
1458 rc = cfs_cpt_bind(lnet_cpt_table(), sched->kss_cpt);
1460 CWARN("Can't set CPU partition affinity to %d: %d\n",
1461 sched->kss_cpt, rc);
1464 spin_lock_bh(&sched->kss_lock);
1466 while (!ksocknal_data.ksnd_shuttingdown) {
1467 bool did_something = false;
1469 /* Ensure I progress everything semi-fairly */
1470 conn = list_first_entry_or_null(&sched->kss_rx_conns,
1474 list_del(&conn->ksnc_rx_list);
1476 LASSERT(conn->ksnc_rx_scheduled);
1477 LASSERT(conn->ksnc_rx_ready);
1479 /* clear rx_ready in case receive isn't complete.
1480 * Do it BEFORE we call process_recv, since
1481 * data_ready can set it any time after we release
1483 conn->ksnc_rx_ready = 0;
1484 spin_unlock_bh(&sched->kss_lock);
1486 rc = ksocknal_process_receive(conn, rx_scratch_pgs,
1489 spin_lock_bh(&sched->kss_lock);
1491 /* I'm the only one that can clear this flag */
1492 LASSERT(conn->ksnc_rx_scheduled);
1494 /* Did process_receive get everything it wanted? */
1496 conn->ksnc_rx_ready = 1;
1498 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1499 /* Conn blocked waiting for ksocknal_recv()
1500 * I change its state (under lock) to signal
1501 * it can be rescheduled */
1502 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1503 } else if (conn->ksnc_rx_ready) {
1504 /* reschedule for rx */
1505 list_add_tail(&conn->ksnc_rx_list,
1506 &sched->kss_rx_conns);
1508 conn->ksnc_rx_scheduled = 0;
1510 ksocknal_conn_decref(conn);
1513 did_something = true;
1516 if (!list_empty(&sched->kss_tx_conns)) {
1519 list_splice_init(&sched->kss_zombie_noop_txs, &zlist);
1521 conn = list_first_entry(&sched->kss_tx_conns,
1524 list_del(&conn->ksnc_tx_list);
1526 LASSERT(conn->ksnc_tx_scheduled);
1527 LASSERT(conn->ksnc_tx_ready);
1528 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1530 tx = list_first_entry(&conn->ksnc_tx_queue,
1531 struct ksock_tx, tx_list);
1533 if (conn->ksnc_tx_carrier == tx)
1534 ksocknal_next_tx_carrier(conn);
1536 /* dequeue now so empty list => more to send */
1537 list_del(&tx->tx_list);
1539 /* Clear tx_ready in case send isn't complete. Do
1540 * it BEFORE we call process_transmit, since
1541 * write_space can set it any time after we release
1543 conn->ksnc_tx_ready = 0;
1544 spin_unlock_bh(&sched->kss_lock);
1546 if (!list_empty(&zlist)) {
1547 /* free zombie noop txs, it's fast because
1548 * noop txs are just put in freelist */
1549 ksocknal_txlist_done(NULL, &zlist, 0);
1552 rc = ksocknal_process_transmit(conn, tx, scratch_iov);
1554 if (rc == -ENOMEM || rc == -EAGAIN) {
1555 /* Incomplete send: replace tx on HEAD of tx_queue */
1556 spin_lock_bh(&sched->kss_lock);
1557 list_add(&tx->tx_list,
1558 &conn->ksnc_tx_queue);
1560 /* Complete send; tx -ref */
1561 ksocknal_tx_decref(tx);
1563 spin_lock_bh(&sched->kss_lock);
1564 /* assume space for more */
1565 conn->ksnc_tx_ready = 1;
1568 if (rc == -ENOMEM) {
1569 /* Do nothing; after a short timeout, this
1570 * conn will be reposted on kss_tx_conns. */
1571 } else if (conn->ksnc_tx_ready &&
1572 !list_empty(&conn->ksnc_tx_queue)) {
1573 /* reschedule for tx */
1574 list_add_tail(&conn->ksnc_tx_list,
1575 &sched->kss_tx_conns);
1577 conn->ksnc_tx_scheduled = 0;
1579 ksocknal_conn_decref(conn);
1582 did_something = true;
1584 if (!did_something || /* nothing to do */
1585 need_resched()) { /* hogging CPU? */
1586 spin_unlock_bh(&sched->kss_lock);
1588 if (!did_something) { /* wait for something to do */
1589 rc = wait_event_interruptible_exclusive(
1591 !ksocknal_sched_cansleep(sched));
1597 spin_lock_bh(&sched->kss_lock);
1601 spin_unlock_bh(&sched->kss_lock);
1602 CFS_FREE_PTR_ARRAY(rx_scratch_pgs, LNET_MAX_IOV);
1603 CFS_FREE_PTR_ARRAY(scratch_iov, LNET_MAX_IOV);
1604 ksocknal_thread_fini();
1609 * Add connection to kss_rx_conns of scheduler
1610 * and wakeup the scheduler.
1612 void ksocknal_read_callback(struct ksock_conn *conn)
1614 struct ksock_sched *sched;
1616 sched = conn->ksnc_scheduler;
1618 spin_lock_bh(&sched->kss_lock);
1620 conn->ksnc_rx_ready = 1;
1622 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1623 list_add_tail(&conn->ksnc_rx_list,
1624 &sched->kss_rx_conns);
1625 conn->ksnc_rx_scheduled = 1;
1626 /* extra ref for scheduler */
1627 ksocknal_conn_addref(conn);
1629 wake_up (&sched->kss_waitq);
1631 spin_unlock_bh(&sched->kss_lock);
1635 * Add connection to kss_tx_conns of scheduler
1636 * and wakeup the scheduler.
1638 void ksocknal_write_callback(struct ksock_conn *conn)
1640 struct ksock_sched *sched;
1642 sched = conn->ksnc_scheduler;
1644 spin_lock_bh(&sched->kss_lock);
1646 conn->ksnc_tx_ready = 1;
1648 if (!conn->ksnc_tx_scheduled && /* not being progressed */
1649 !list_empty(&conn->ksnc_tx_queue)) { /* packets to send */
1650 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
1651 conn->ksnc_tx_scheduled = 1;
1652 /* extra ref for scheduler */
1653 ksocknal_conn_addref(conn);
1655 wake_up(&sched->kss_waitq);
1658 spin_unlock_bh(&sched->kss_lock);
1661 static const struct ksock_proto *
1662 ksocknal_parse_proto_version(struct ksock_hello_msg *hello)
1666 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1667 version = hello->kshm_version;
1668 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1669 version = __swab32(hello->kshm_version);
1672 #if SOCKNAL_VERSION_DEBUG
1673 if (*ksocknal_tunables.ksnd_protocol == 1)
1676 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1677 version == KSOCK_PROTO_V3)
1680 if (version == KSOCK_PROTO_V2)
1681 return &ksocknal_protocol_v2x;
1683 if (version == KSOCK_PROTO_V3)
1684 return &ksocknal_protocol_v3x;
1689 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1690 struct lnet_magicversion *hmv;
1692 BUILD_BUG_ON(sizeof(struct lnet_magicversion) !=
1693 offsetof(struct ksock_hello_msg, kshm_src_nid));
1695 hmv = (struct lnet_magicversion *)hello;
1697 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1698 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1699 return &ksocknal_protocol_v1x;
1706 ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1707 lnet_nid_t peer_nid, struct ksock_hello_msg *hello)
1709 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1710 struct ksock_net *net = (struct ksock_net *)ni->ni_data;
1712 LASSERT(hello->kshm_nips <= LNET_INTERFACES_NUM);
1714 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1715 LASSERT(conn->ksnc_proto != NULL);
1717 hello->kshm_src_nid = lnet_nid_to_nid4(&ni->ni_nid);
1718 hello->kshm_dst_nid = peer_nid;
1719 hello->kshm_src_pid = the_lnet.ln_pid;
1721 hello->kshm_src_incarnation = net->ksnn_incarnation;
1722 hello->kshm_ctype = conn->ksnc_type;
1724 return conn->ksnc_proto->pro_send_hello(conn, hello);
1728 ksocknal_invert_type(int type)
1732 case SOCKLND_CONN_ANY:
1733 case SOCKLND_CONN_CONTROL:
1735 case SOCKLND_CONN_BULK_IN:
1736 return SOCKLND_CONN_BULK_OUT;
1737 case SOCKLND_CONN_BULK_OUT:
1738 return SOCKLND_CONN_BULK_IN;
1740 return (SOCKLND_CONN_NONE);
1745 ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1746 struct ksock_hello_msg *hello,
1747 struct lnet_process_id *peerid,
1750 /* Return < 0 fatal error
1752 * EALREADY lost connection race
1753 * EPROTO protocol version mismatch
1755 struct socket *sock = conn->ksnc_sock;
1756 int active = (conn->ksnc_proto != NULL);
1760 const struct ksock_proto *proto;
1761 struct lnet_process_id recv_id;
1763 /* socket type set on active connections - not set on passive */
1764 LASSERT(!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1766 timeout = active ? ksocknal_timeout() :
1767 lnet_acceptor_timeout();
1769 rc = lnet_sock_read(sock, &hello->kshm_magic,
1770 sizeof(hello->kshm_magic), timeout);
1772 CERROR("Error %d reading HELLO from %pIS\n",
1773 rc, &conn->ksnc_peeraddr);
1778 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1779 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1780 hello->kshm_magic != le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1781 /* Unexpected magic! */
1782 CERROR("Bad magic(1) %#08x (%#08x expected) from %pIS\n",
1783 __cpu_to_le32 (hello->kshm_magic),
1784 LNET_PROTO_TCP_MAGIC, &conn->ksnc_peeraddr);
1788 rc = lnet_sock_read(sock, &hello->kshm_version,
1789 sizeof(hello->kshm_version), timeout);
1791 CERROR("Error %d reading HELLO from %pIS\n",
1792 rc, &conn->ksnc_peeraddr);
1797 proto = ksocknal_parse_proto_version(hello);
1798 if (proto == NULL) {
1800 /* unknown protocol from peer_ni,
1801 * tell peer_ni my protocol.
1803 conn->ksnc_proto = &ksocknal_protocol_v3x;
1804 #if SOCKNAL_VERSION_DEBUG
1805 if (*ksocknal_tunables.ksnd_protocol == 2)
1806 conn->ksnc_proto = &ksocknal_protocol_v2x;
1807 else if (*ksocknal_tunables.ksnd_protocol == 1)
1808 conn->ksnc_proto = &ksocknal_protocol_v1x;
1810 hello->kshm_nips = 0;
1811 ksocknal_send_hello(ni, conn,
1812 lnet_nid_to_nid4(&ni->ni_nid),
1816 CERROR("Unknown protocol version (%d.x expected) from %pIS\n",
1817 conn->ksnc_proto->pro_version, &conn->ksnc_peeraddr);
1822 proto_match = (conn->ksnc_proto == proto);
1823 conn->ksnc_proto = proto;
1825 /* receive the rest of hello message anyway */
1826 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1828 CERROR("Error %d reading or checking hello from from %pIS\n",
1829 rc, &conn->ksnc_peeraddr);
1834 *incarnation = hello->kshm_src_incarnation;
1836 if (hello->kshm_src_nid == LNET_NID_ANY) {
1837 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY from %pIS\n",
1838 &conn->ksnc_peeraddr);
1843 rpc_get_port((struct sockaddr *)&conn->ksnc_peeraddr) >
1844 LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1845 /* Userspace NAL assigns peer_ni process ID from socket */
1846 recv_id.pid = rpc_get_port((struct sockaddr *)
1847 &conn->ksnc_peeraddr) |
1849 LASSERT(conn->ksnc_peeraddr.ss_family == AF_INET);
1850 recv_id.nid = LNET_MKNID(
1851 LNET_NID_NET(&ni->ni_nid),
1852 ntohl(((struct sockaddr_in *)
1853 &conn->ksnc_peeraddr)->sin_addr.s_addr));
1855 recv_id.nid = hello->kshm_src_nid;
1856 recv_id.pid = hello->kshm_src_pid;
1862 /* peer_ni determines type */
1863 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1864 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
1865 CERROR("Unexpected type %d from %s ip %pIS\n",
1866 hello->kshm_ctype, libcfs_id2str(*peerid),
1867 &conn->ksnc_peeraddr);
1873 if (peerid->pid != recv_id.pid ||
1874 peerid->nid != recv_id.nid) {
1875 LCONSOLE_ERROR_MSG(0x130,
1876 "Connected successfully to %s on host %pIS, but they claimed they were %s; please check your Lustre configuration.\n",
1877 libcfs_id2str(*peerid),
1878 &conn->ksnc_peeraddr,
1879 libcfs_id2str(recv_id));
1883 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1884 /* Possible protocol mismatch or I lost the connection race */
1885 return proto_match ? EALREADY : EPROTO;
1888 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1889 CERROR("Mismatched types: me %d, %s ip %pIS %d\n",
1890 conn->ksnc_type, libcfs_id2str(*peerid),
1891 &conn->ksnc_peeraddr,
1899 ksocknal_connect(struct ksock_conn_cb *conn_cb)
1902 struct ksock_peer_ni *peer_ni = conn_cb->ksnr_peer;
1905 struct socket *sock;
1907 bool retry_later = false;
1910 deadline = ktime_get_seconds() + ksocknal_timeout();
1912 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1914 LASSERT(conn_cb->ksnr_scheduled);
1915 LASSERT(!conn_cb->ksnr_connecting);
1917 conn_cb->ksnr_connecting = 1;
1920 wanted = ksocknal_conn_cb_mask() & ~conn_cb->ksnr_connected;
1922 /* stop connecting if peer_ni/cb got closed under me, or
1923 * conn cb got connected while queued
1925 if (peer_ni->ksnp_closing || conn_cb->ksnr_deleted ||
1927 retry_later = false;
1931 /* reschedule if peer_ni is connecting to me */
1932 if (peer_ni->ksnp_accepting > 0) {
1934 "peer_ni %s(%d) already connecting to me, retry later.\n",
1935 libcfs_nidstr(&peer_ni->ksnp_id.nid),
1936 peer_ni->ksnp_accepting);
1940 if (retry_later) /* needs reschedule */
1943 if ((wanted & BIT(SOCKLND_CONN_ANY)) != 0) {
1944 type = SOCKLND_CONN_ANY;
1945 } else if ((wanted & BIT(SOCKLND_CONN_CONTROL)) != 0) {
1946 type = SOCKLND_CONN_CONTROL;
1947 } else if ((wanted & BIT(SOCKLND_CONN_BULK_IN)) != 0 &&
1948 conn_cb->ksnr_blki_conn_count <= conn_cb->ksnr_blko_conn_count) {
1949 type = SOCKLND_CONN_BULK_IN;
1951 LASSERT ((wanted & BIT(SOCKLND_CONN_BULK_OUT)) != 0);
1952 type = SOCKLND_CONN_BULK_OUT;
1955 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1957 if (ktime_get_seconds() >= deadline) {
1959 lnet_connect_console_error(
1960 rc, &peer_ni->ksnp_id.nid,
1961 (struct sockaddr *)&conn_cb->ksnr_addr);
1965 sock = lnet_connect(&peer_ni->ksnp_id.nid,
1966 conn_cb->ksnr_myiface,
1967 (struct sockaddr *)&conn_cb->ksnr_addr,
1968 peer_ni->ksnp_ni->ni_net_ns);
1974 rc = ksocknal_create_conn(peer_ni->ksnp_ni, conn_cb, sock,
1977 lnet_connect_console_error(
1978 rc, &peer_ni->ksnp_id.nid,
1979 (struct sockaddr *)&conn_cb->ksnr_addr);
1983 /* A +ve RC means I have to retry because I lost the connection
1984 * race or I have to renegotiate protocol version
1986 retry_later = (rc != 0);
1988 CDEBUG(D_NET, "peer_ni %s: conn race, retry later.\n",
1989 libcfs_nidstr(&peer_ni->ksnp_id.nid));
1991 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1994 conn_cb->ksnr_scheduled = 0;
1995 conn_cb->ksnr_connecting = 0;
1998 /* re-queue for attention; this frees me up to handle
1999 * the peer_ni's incoming connection request
2002 if (rc == EALREADY ||
2003 (rc == 0 && peer_ni->ksnp_accepting > 0)) {
2004 /* We want to introduce a delay before next
2005 * attempt to connect if we lost conn race, but
2006 * the race is resolved quickly usually, so
2007 * min_reconnectms should be good heuristic
2009 conn_cb->ksnr_retry_interval =
2010 *ksocknal_tunables.ksnd_min_reconnectms / 1000;
2011 conn_cb->ksnr_timeout = ktime_get_seconds() +
2012 conn_cb->ksnr_retry_interval;
2015 ksocknal_launch_connection_locked(conn_cb);
2018 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2022 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2024 conn_cb->ksnr_scheduled = 0;
2025 conn_cb->ksnr_connecting = 0;
2027 /* This is a retry rather than a new connection */
2028 conn_cb->ksnr_retry_interval *= 2;
2029 conn_cb->ksnr_retry_interval =
2030 max_t(time64_t, conn_cb->ksnr_retry_interval,
2031 *ksocknal_tunables.ksnd_min_reconnectms / 1000);
2032 conn_cb->ksnr_retry_interval =
2033 min_t(time64_t, conn_cb->ksnr_retry_interval,
2034 *ksocknal_tunables.ksnd_max_reconnectms / 1000);
2036 LASSERT(conn_cb->ksnr_retry_interval);
2037 conn_cb->ksnr_timeout = ktime_get_seconds() +
2038 conn_cb->ksnr_retry_interval;
2040 if (!list_empty(&peer_ni->ksnp_tx_queue) &&
2041 peer_ni->ksnp_accepting == 0 &&
2042 !ksocknal_find_connecting_conn_cb_locked(peer_ni)) {
2043 struct ksock_conn *conn;
2045 /* ksnp_tx_queue is queued on a conn on successful
2046 * connection for V1.x and V2.x
2048 conn = list_first_entry_or_null(&peer_ni->ksnp_conns,
2049 struct ksock_conn, ksnc_list);
2051 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v3x);
2053 /* take all the blocked packets while I've got the lock and
2056 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
2059 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2061 ksocknal_peer_failed(peer_ni);
2062 ksocknal_txlist_done(peer_ni->ksnp_ni, &zombies, rc);
2067 * check whether we need to create more connds.
2068 * It will try to create new thread if it's necessary, @timeout can
2069 * be updated if failed to create, so caller wouldn't keep try while
2070 * running out of resource.
2073 ksocknal_connd_check_start(time64_t sec, long *timeout)
2076 int total = ksocknal_data.ksnd_connd_starting +
2077 ksocknal_data.ksnd_connd_running;
2079 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2080 /* still in initializing */
2084 if (total >= *ksocknal_tunables.ksnd_nconnds_max ||
2085 total > ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV) {
2086 /* can't create more connd, or still have enough
2087 * threads to handle more connecting */
2091 if (list_empty(&ksocknal_data.ksnd_connd_routes)) {
2092 /* no pending connecting request */
2096 if (sec - ksocknal_data.ksnd_connd_failed_stamp <= 1) {
2097 /* may run out of resource, retry later */
2098 *timeout = cfs_time_seconds(1);
2102 if (ksocknal_data.ksnd_connd_starting > 0) {
2103 /* serialize starting to avoid flood */
2107 ksocknal_data.ksnd_connd_starting_stamp = sec;
2108 ksocknal_data.ksnd_connd_starting++;
2109 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2111 /* NB: total is the next id */
2112 rc = ksocknal_thread_start(ksocknal_connd, NULL,
2113 "socknal_cd%02d", total);
2115 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2120 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2121 ksocknal_data.ksnd_connd_starting--;
2122 ksocknal_data.ksnd_connd_failed_stamp = ktime_get_real_seconds();
2128 * check whether current thread can exit, it will return 1 if there are too
2129 * many threads and no creating in past 120 seconds.
2130 * Also, this function may update @timeout to make caller come back
2131 * again to recheck these conditions.
2134 ksocknal_connd_check_stop(time64_t sec, long *timeout)
2138 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2139 /* still in initializing */
2143 if (ksocknal_data.ksnd_connd_starting > 0) {
2144 /* in progress of starting new thread */
2148 if (ksocknal_data.ksnd_connd_running <=
2149 *ksocknal_tunables.ksnd_nconnds) { /* can't shrink */
2153 /* created thread in past 120 seconds? */
2154 val = (int)(ksocknal_data.ksnd_connd_starting_stamp +
2155 SOCKNAL_CONND_TIMEOUT - sec);
2157 *timeout = (val > 0) ? cfs_time_seconds(val) :
2158 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT);
2162 /* no creating in past 120 seconds */
2164 return ksocknal_data.ksnd_connd_running >
2165 ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV;
2168 /* Go through connd_cbs queue looking for a conn_cb that we can process
2169 * right now, @timeout_p can be updated if we need to come back later */
2170 static struct ksock_conn_cb *
2171 ksocknal_connd_get_conn_cb_locked(signed long *timeout_p)
2173 time64_t now = ktime_get_seconds();
2174 time64_t conn_timeout;
2175 struct ksock_conn_cb *conn_cb;
2177 /* connd_routes can contain both pending and ordinary routes */
2178 list_for_each_entry(conn_cb, &ksocknal_data.ksnd_connd_routes,
2181 conn_timeout = conn_cb->ksnr_timeout;
2183 if (conn_cb->ksnr_retry_interval == 0 ||
2184 now >= conn_timeout)
2187 if (*timeout_p == MAX_SCHEDULE_TIMEOUT ||
2188 *timeout_p > cfs_time_seconds(conn_timeout - now))
2189 *timeout_p = cfs_time_seconds(conn_timeout - now);
2196 ksocknal_connd(void *arg)
2198 spinlock_t *connd_lock = &ksocknal_data.ksnd_connd_lock;
2199 struct ksock_connreq *cr;
2200 wait_queue_entry_t wait;
2205 spin_lock_bh(connd_lock);
2207 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2208 ksocknal_data.ksnd_connd_starting--;
2209 ksocknal_data.ksnd_connd_running++;
2211 while (!ksocknal_data.ksnd_shuttingdown) {
2212 struct ksock_conn_cb *conn_cb = NULL;
2213 time64_t sec = ktime_get_real_seconds();
2214 long timeout = MAX_SCHEDULE_TIMEOUT;
2215 bool dropped_lock = false;
2217 if (ksocknal_connd_check_stop(sec, &timeout)) {
2218 /* wakeup another one to check stop */
2219 wake_up(&ksocknal_data.ksnd_connd_waitq);
2223 if (ksocknal_connd_check_start(sec, &timeout)) {
2224 /* created new thread */
2225 dropped_lock = true;
2228 cr = list_first_entry_or_null(&ksocknal_data.ksnd_connd_connreqs,
2229 struct ksock_connreq, ksncr_list);
2231 /* Connection accepted by the listener */
2232 list_del(&cr->ksncr_list);
2233 spin_unlock_bh(connd_lock);
2234 dropped_lock = true;
2236 ksocknal_create_conn(cr->ksncr_ni, NULL,
2237 cr->ksncr_sock, SOCKLND_CONN_NONE);
2238 lnet_ni_decref(cr->ksncr_ni);
2239 LIBCFS_FREE(cr, sizeof(*cr));
2241 spin_lock_bh(connd_lock);
2244 /* Only handle an outgoing connection request if there
2245 * is a thread left to handle incoming connections and
2248 if (ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV <
2249 ksocknal_data.ksnd_connd_running)
2250 conn_cb = ksocknal_connd_get_conn_cb_locked(&timeout);
2253 list_del(&conn_cb->ksnr_connd_list);
2254 ksocknal_data.ksnd_connd_connecting++;
2255 spin_unlock_bh(connd_lock);
2256 dropped_lock = true;
2258 if (ksocknal_connect(conn_cb)) {
2259 /* consecutive retry */
2260 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2261 CWARN("massive consecutive re-connecting to %pIS\n",
2262 &conn_cb->ksnr_addr);
2269 ksocknal_conn_cb_decref(conn_cb);
2271 spin_lock_bh(connd_lock);
2272 ksocknal_data.ksnd_connd_connecting--;
2276 if (!need_resched())
2278 spin_unlock_bh(connd_lock);
2280 spin_lock_bh(connd_lock);
2284 /* Nothing to do for 'timeout' */
2285 set_current_state(TASK_INTERRUPTIBLE);
2286 add_wait_queue_exclusive(&ksocknal_data.ksnd_connd_waitq,
2288 spin_unlock_bh(connd_lock);
2290 schedule_timeout(timeout);
2292 remove_wait_queue(&ksocknal_data.ksnd_connd_waitq, &wait);
2293 spin_lock_bh(connd_lock);
2295 ksocknal_data.ksnd_connd_running--;
2296 spin_unlock_bh(connd_lock);
2298 ksocknal_thread_fini();
2302 static struct ksock_conn *
2303 ksocknal_find_timed_out_conn(struct ksock_peer_ni *peer_ni)
2305 /* We're called with a shared lock on ksnd_global_lock */
2306 struct ksock_conn *conn;
2307 struct ksock_tx *tx;
2308 struct ksock_sched *sched;
2310 list_for_each_entry(conn, &peer_ni->ksnp_conns, ksnc_list) {
2313 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2314 LASSERT (!conn->ksnc_closing);
2315 sched = conn->ksnc_scheduler;
2317 error = conn->ksnc_sock->sk->sk_err;
2319 ksocknal_conn_addref(conn);
2323 CNETERR("A connection with %s (%pISp) was reset; it may have rebooted.\n",
2324 libcfs_idstr(&peer_ni->ksnp_id),
2325 &conn->ksnc_peeraddr);
2328 CNETERR("A connection with %s (%pISp) timed out; the network or node may be down.\n",
2329 libcfs_idstr(&peer_ni->ksnp_id),
2330 &conn->ksnc_peeraddr);
2333 CNETERR("An unexpected network error %d occurred with %s (%pISp\n",
2335 libcfs_idstr(&peer_ni->ksnp_id),
2336 &conn->ksnc_peeraddr);
2343 if (conn->ksnc_rx_started &&
2344 ktime_get_seconds() >= conn->ksnc_rx_deadline) {
2345 /* Timed out incomplete incoming message */
2346 ksocknal_conn_addref(conn);
2347 CNETERR("Timeout receiving from %s (%pISp), state %d wanted %d left %d\n",
2348 libcfs_idstr(&peer_ni->ksnp_id),
2349 &conn->ksnc_peeraddr,
2350 conn->ksnc_rx_state,
2351 conn->ksnc_rx_nob_wanted,
2352 conn->ksnc_rx_nob_left);
2356 spin_lock_bh(&sched->kss_lock);
2357 if ((!list_empty(&conn->ksnc_tx_queue) ||
2358 conn->ksnc_sock->sk->sk_wmem_queued != 0) &&
2359 ktime_get_seconds() >= conn->ksnc_tx_deadline) {
2360 /* Timed out messages queued for sending or
2361 * buffered in the socket's send buffer
2363 ksocknal_conn_addref(conn);
2364 list_for_each_entry(tx, &conn->ksnc_tx_queue,
2367 LNET_MSG_STATUS_LOCAL_TIMEOUT;
2368 CNETERR("Timeout sending data to %s (%pISp) the network or that node may be down.\n",
2369 libcfs_idstr(&peer_ni->ksnp_id),
2370 &conn->ksnc_peeraddr);
2371 spin_unlock_bh(&sched->kss_lock);
2374 spin_unlock_bh(&sched->kss_lock);
2381 ksocknal_flush_stale_txs(struct ksock_peer_ni *peer_ni)
2383 struct ksock_tx *tx;
2384 LIST_HEAD(stale_txs);
2386 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2388 while ((tx = list_first_entry_or_null(&peer_ni->ksnp_tx_queue,
2390 tx_list)) != NULL) {
2391 if (ktime_get_seconds() < tx->tx_deadline)
2394 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2396 list_move_tail(&tx->tx_list, &stale_txs);
2399 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2401 ksocknal_txlist_done(peer_ni->ksnp_ni, &stale_txs, -ETIMEDOUT);
2405 ksocknal_send_keepalive_locked(struct ksock_peer_ni *peer_ni)
2406 __must_hold(&ksocknal_data.ksnd_global_lock)
2408 struct ksock_sched *sched;
2409 struct ksock_conn *conn;
2410 struct ksock_tx *tx;
2412 /* last_alive will be updated by create_conn */
2413 if (list_empty(&peer_ni->ksnp_conns))
2416 if (peer_ni->ksnp_proto != &ksocknal_protocol_v3x)
2419 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2420 ktime_get_seconds() < peer_ni->ksnp_last_alive +
2421 *ksocknal_tunables.ksnd_keepalive)
2424 if (ktime_get_seconds() < peer_ni->ksnp_send_keepalive)
2427 /* retry 10 secs later, so we wouldn't put pressure
2428 * on this peer_ni if we failed to send keepalive this time */
2429 peer_ni->ksnp_send_keepalive = ktime_get_seconds() + 10;
2431 conn = ksocknal_find_conn_locked(peer_ni, NULL, 1);
2433 sched = conn->ksnc_scheduler;
2435 spin_lock_bh(&sched->kss_lock);
2436 if (!list_empty(&conn->ksnc_tx_queue)) {
2437 spin_unlock_bh(&sched->kss_lock);
2438 /* there is an queued ACK, don't need keepalive */
2442 spin_unlock_bh(&sched->kss_lock);
2445 read_unlock(&ksocknal_data.ksnd_global_lock);
2447 /* cookie = 1 is reserved for keepalive PING */
2448 tx = ksocknal_alloc_tx_noop(1, 1);
2450 read_lock(&ksocknal_data.ksnd_global_lock);
2454 if (ksocknal_launch_packet(peer_ni->ksnp_ni, tx, &peer_ni->ksnp_id)
2456 read_lock(&ksocknal_data.ksnd_global_lock);
2460 ksocknal_free_tx(tx);
2461 read_lock(&ksocknal_data.ksnd_global_lock);
2468 ksocknal_check_peer_timeouts(int idx)
2470 struct hlist_head *peers = &ksocknal_data.ksnd_peers[idx];
2471 struct ksock_peer_ni *peer_ni;
2472 struct ksock_conn *conn;
2473 struct ksock_tx *tx;
2476 /* NB. We expect to have a look at all the peers and not find any
2477 * connections to time out, so we just use a shared lock while we
2480 read_lock(&ksocknal_data.ksnd_global_lock);
2482 hlist_for_each_entry(peer_ni, peers, ksnp_list) {
2483 struct ksock_tx *tx_stale;
2484 time64_t deadline = 0;
2488 if (ksocknal_send_keepalive_locked(peer_ni) != 0) {
2489 read_unlock(&ksocknal_data.ksnd_global_lock);
2493 conn = ksocknal_find_timed_out_conn(peer_ni);
2496 read_unlock(&ksocknal_data.ksnd_global_lock);
2498 ksocknal_close_conn_and_siblings(conn, -ETIMEDOUT);
2500 /* NB we won't find this one again, but we can't
2501 * just proceed with the next peer_ni, since we dropped
2502 * ksnd_global_lock and it might be dead already!
2504 ksocknal_conn_decref(conn);
2508 /* we can't process stale txs right here because we're
2509 * holding only shared lock
2511 tx = list_first_entry_or_null(&peer_ni->ksnp_tx_queue,
2512 struct ksock_tx, tx_list);
2513 if (tx && ktime_get_seconds() >= tx->tx_deadline) {
2514 ksocknal_peer_addref(peer_ni);
2515 read_unlock(&ksocknal_data.ksnd_global_lock);
2517 ksocknal_flush_stale_txs(peer_ni);
2519 ksocknal_peer_decref(peer_ni);
2523 if (list_empty(&peer_ni->ksnp_zc_req_list))
2527 spin_lock(&peer_ni->ksnp_lock);
2528 list_for_each_entry(tx, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
2529 if (ktime_get_seconds() < tx->tx_deadline)
2531 /* ignore the TX if connection is being closed */
2532 if (tx->tx_conn->ksnc_closing)
2535 if (tx_stale == NULL)
2539 if (tx_stale == NULL) {
2540 spin_unlock(&peer_ni->ksnp_lock);
2544 deadline = tx_stale->tx_deadline;
2545 resid = tx_stale->tx_resid;
2546 conn = tx_stale->tx_conn;
2547 ksocknal_conn_addref(conn);
2549 spin_unlock(&peer_ni->ksnp_lock);
2550 read_unlock(&ksocknal_data.ksnd_global_lock);
2552 CERROR("Total %d stale ZC_REQs for peer_ni %s detected; the "
2553 "oldest(%p) timed out %lld secs ago, "
2554 "resid: %d, wmem: %d\n",
2555 n, libcfs_nidstr(&peer_ni->ksnp_id.nid), tx_stale,
2556 ktime_get_seconds() - deadline,
2557 resid, conn->ksnc_sock->sk->sk_wmem_queued);
2559 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2560 ksocknal_conn_decref(conn);
2564 read_unlock(&ksocknal_data.ksnd_global_lock);
2567 int ksocknal_reaper(void *arg)
2569 wait_queue_entry_t wait;
2570 struct ksock_conn *conn;
2571 struct ksock_sched *sched;
2572 LIST_HEAD(enomem_conns);
2577 time64_t deadline = ktime_get_seconds();
2581 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2583 while (!ksocknal_data.ksnd_shuttingdown) {
2584 conn = list_first_entry_or_null(&ksocknal_data.ksnd_deathrow_conns,
2585 struct ksock_conn, ksnc_list);
2587 list_del(&conn->ksnc_list);
2589 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2591 ksocknal_terminate_conn(conn);
2592 ksocknal_conn_decref(conn);
2594 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2598 conn = list_first_entry_or_null(&ksocknal_data.ksnd_zombie_conns,
2599 struct ksock_conn, ksnc_list);
2601 list_del(&conn->ksnc_list);
2603 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2605 ksocknal_destroy_conn(conn);
2607 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2611 list_splice_init(&ksocknal_data.ksnd_enomem_conns,
2614 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2616 /* reschedule all the connections that stalled with ENOMEM... */
2618 while ((conn = list_first_entry_or_null(&enomem_conns,
2620 ksnc_tx_list)) != NULL) {
2621 list_del(&conn->ksnc_tx_list);
2623 sched = conn->ksnc_scheduler;
2625 spin_lock_bh(&sched->kss_lock);
2627 LASSERT(conn->ksnc_tx_scheduled);
2628 conn->ksnc_tx_ready = 1;
2629 list_add_tail(&conn->ksnc_tx_list,
2630 &sched->kss_tx_conns);
2631 wake_up(&sched->kss_waitq);
2633 spin_unlock_bh(&sched->kss_lock);
2637 /* careful with the jiffy wrap... */
2638 while ((timeout = deadline - ktime_get_seconds()) <= 0) {
2641 int chunk = HASH_SIZE(ksocknal_data.ksnd_peers);
2642 unsigned int lnd_timeout;
2644 /* Time to check for timeouts on a few more peers: I
2645 * do checks every 'p' seconds on a proportion of the
2646 * peer_ni table and I need to check every connection
2647 * 'n' times within a timeout interval, to ensure I
2648 * detect a timeout on any connection within (n+1)/n
2649 * times the timeout interval.
2652 lnd_timeout = ksocknal_timeout();
2653 if (lnd_timeout > n * p)
2654 chunk = (chunk * n * p) / lnd_timeout;
2658 for (i = 0; i < chunk; i++) {
2659 ksocknal_check_peer_timeouts(peer_index);
2660 peer_index = (peer_index + 1) %
2661 HASH_SIZE(ksocknal_data.ksnd_peers);
2667 if (nenomem_conns != 0) {
2668 /* Reduce my timeout if I rescheduled ENOMEM conns.
2669 * This also prevents me getting woken immediately
2670 * if any go back on my enomem list. */
2671 timeout = SOCKNAL_ENOMEM_RETRY;
2673 ksocknal_data.ksnd_reaper_waketime = ktime_get_seconds() +
2676 set_current_state(TASK_INTERRUPTIBLE);
2677 add_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2679 if (!ksocknal_data.ksnd_shuttingdown &&
2680 list_empty(&ksocknal_data.ksnd_deathrow_conns) &&
2681 list_empty(&ksocknal_data.ksnd_zombie_conns))
2682 schedule_timeout(cfs_time_seconds(timeout));
2684 set_current_state(TASK_RUNNING);
2685 remove_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2687 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2690 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2692 ksocknal_thread_fini();