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.
30 ksocknal_alloc_tx(int type, int size)
32 struct ksock_tx *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.next,
42 struct ksock_tx, 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_hstatus = LNET_MSG_STATUS_OK;
61 tx->tx_desc_size = size;
63 atomic_inc(&ksocknal_data.ksnd_nactive_txs);
69 ksocknal_alloc_tx_noop(__u64 cookie, int nonblk)
73 tx = ksocknal_alloc_tx(KSOCK_MSG_NOOP, KSOCK_NOOP_TX_SIZE);
75 CERROR("Can't allocate noop tx desc\n");
80 tx->tx_lnetmsg = NULL;
83 tx->tx_iov = tx->tx_frags.virt.iov;
85 tx->tx_nonblk = nonblk;
87 tx->tx_msg.ksm_csum = 0;
88 tx->tx_msg.ksm_type = KSOCK_MSG_NOOP;
89 tx->tx_msg.ksm_zc_cookies[0] = 0;
90 tx->tx_msg.ksm_zc_cookies[1] = cookie;
97 ksocknal_free_tx(struct ksock_tx *tx)
99 atomic_dec(&ksocknal_data.ksnd_nactive_txs);
101 if (tx->tx_lnetmsg == NULL && tx->tx_desc_size == KSOCK_NOOP_TX_SIZE) {
103 spin_lock(&ksocknal_data.ksnd_tx_lock);
105 list_add(&tx->tx_list, &ksocknal_data.ksnd_idle_noop_txs);
107 spin_unlock(&ksocknal_data.ksnd_tx_lock);
109 LIBCFS_FREE(tx, tx->tx_desc_size);
114 ksocknal_send_iov(struct ksock_conn *conn, struct ksock_tx *tx)
116 struct kvec *iov = tx->tx_iov;
120 LASSERT (tx->tx_niov > 0);
122 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
123 rc = ksocknal_lib_send_iov(conn, tx);
125 if (rc <= 0) /* sent nothing? */
129 LASSERT (nob <= tx->tx_resid);
134 LASSERT (tx->tx_niov > 0);
136 if (nob < (int) iov->iov_len) {
137 iov->iov_base += nob;
151 ksocknal_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx)
153 lnet_kiov_t *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);
163 if (rc <= 0) /* sent nothing? */
167 LASSERT (nob <= tx->tx_resid);
172 LASSERT(tx->tx_nkiov > 0);
174 if (nob < (int)kiov->kiov_len) {
175 kiov->kiov_offset += nob;
176 kiov->kiov_len -= nob;
180 nob -= (int)kiov->kiov_len;
181 tx->tx_kiov = ++kiov;
189 ksocknal_transmit(struct ksock_conn *conn, struct ksock_tx *tx)
194 if (ksocknal_data.ksnd_stall_tx != 0) {
195 set_current_state(TASK_UNINTERRUPTIBLE);
196 schedule_timeout(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_iov (conn, tx);
215 rc = ksocknal_send_kiov (conn, tx);
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() +
226 lnet_get_lnd_timeout();
227 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
228 conn->ksnc_tx_bufnob = bufnob;
232 if (rc <= 0) { /* Didn't write anything? */
234 if (rc == 0) /* 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)
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);
270 /* received something... */
273 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
274 conn->ksnc_rx_deadline = ktime_get_seconds() +
275 lnet_get_lnd_timeout();
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)
302 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
305 LASSERT (conn->ksnc_rx_nkiov > 0);
307 /* Never touch conn->ksnc_rx_kiov or change connection
308 * status inside ksocknal_lib_recv_iov */
309 rc = ksocknal_lib_recv_kiov(conn);
314 /* received something... */
317 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
318 conn->ksnc_rx_deadline = ktime_get_seconds() +
319 lnet_get_lnd_timeout();
320 smp_mb(); /* order with setting rx_started */
321 conn->ksnc_rx_started = 1;
323 conn->ksnc_rx_nob_wanted -= nob;
324 conn->ksnc_rx_nob_left -= nob;
327 LASSERT (conn->ksnc_rx_nkiov > 0);
329 if (nob < (int) kiov->kiov_len) {
330 kiov->kiov_offset += nob;
331 kiov->kiov_len -= nob;
335 nob -= kiov->kiov_len;
336 conn->ksnc_rx_kiov = ++kiov;
337 conn->ksnc_rx_nkiov--;
344 ksocknal_receive(struct ksock_conn *conn)
346 /* Return 1 on success, 0 on EOF, < 0 on error.
347 * Caller checks ksnc_rx_nob_wanted to determine
348 * progress/completion. */
352 if (ksocknal_data.ksnd_stall_rx != 0) {
353 set_current_state(TASK_UNINTERRUPTIBLE);
354 schedule_timeout(cfs_time_seconds(ksocknal_data.ksnd_stall_rx));
357 rc = ksocknal_connsock_addref(conn);
359 LASSERT (conn->ksnc_closing);
364 if (conn->ksnc_rx_niov != 0)
365 rc = ksocknal_recv_iov (conn);
367 rc = ksocknal_recv_kiov (conn);
370 /* error/EOF or partial receive */
373 } else if (rc == 0 && conn->ksnc_rx_started) {
374 /* EOF in the middle of a message */
380 /* Completed a fragment */
382 if (conn->ksnc_rx_nob_wanted == 0) {
388 ksocknal_connsock_decref(conn);
393 ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx, int rc)
395 struct lnet_msg *lnetmsg = tx->tx_lnetmsg;
396 enum lnet_msg_hstatus hstatus = tx->tx_hstatus;
399 LASSERT(ni != NULL || tx->tx_conn != NULL);
401 if (!rc && (tx->tx_resid != 0 || tx->tx_zc_aborted)) {
403 if (hstatus == LNET_MSG_STATUS_OK)
404 hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
407 if (tx->tx_conn != NULL)
408 ksocknal_conn_decref(tx->tx_conn);
410 ksocknal_free_tx(tx);
411 if (lnetmsg != NULL) { /* KSOCK_MSG_NOOP go without lnetmsg */
413 CERROR("tx failure rc = %d, hstatus = %d\n", rc,
415 lnetmsg->msg_health_status = hstatus;
416 lnet_finalize(lnetmsg, rc);
423 ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist, int error)
427 while (!list_empty(txlist)) {
428 tx = list_entry(txlist->next, struct ksock_tx, tx_list);
430 if (error && tx->tx_lnetmsg != NULL) {
431 CNETERR("Deleting packet type %d len %d %s->%s\n",
432 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.type),
433 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.payload_length),
434 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
435 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
437 CNETERR("Deleting noop packet\n");
440 list_del(&tx->tx_list);
442 if (tx->tx_hstatus == LNET_MSG_STATUS_OK) {
443 if (error == -ETIMEDOUT)
445 LNET_MSG_STATUS_LOCAL_TIMEOUT;
446 else if (error == -ENETDOWN ||
447 error == -EHOSTUNREACH ||
448 error == -ENETUNREACH)
449 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_DROPPED;
451 * for all other errors we don't want to
455 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
458 LASSERT(atomic_read(&tx->tx_refcount) == 1);
459 ksocknal_tx_done(ni, tx, error);
464 ksocknal_check_zc_req(struct ksock_tx *tx)
466 struct ksock_conn *conn = tx->tx_conn;
467 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
469 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
470 * to ksnp_zc_req_list if some fragment of this message should be sent
471 * zero-copy. Our peer_ni will send an ACK containing this cookie when
472 * she has received this message to tell us we can signal completion.
473 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
474 * ksnp_zc_req_list. */
475 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
476 LASSERT (tx->tx_zc_capable);
478 tx->tx_zc_checked = 1;
480 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
481 !conn->ksnc_zc_capable)
484 /* assign cookie and queue tx to pending list, it will be released when
485 * a matching ack is received. See ksocknal_handle_zcack() */
487 ksocknal_tx_addref(tx);
489 spin_lock(&peer_ni->ksnp_lock);
491 /* ZC_REQ is going to be pinned to the peer_ni */
492 tx->tx_deadline = ktime_get_seconds() +
493 lnet_get_lnd_timeout();
495 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
497 tx->tx_msg.ksm_zc_cookies[0] = peer_ni->ksnp_zc_next_cookie++;
499 if (peer_ni->ksnp_zc_next_cookie == 0)
500 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
502 list_add_tail(&tx->tx_zc_list, &peer_ni->ksnp_zc_req_list);
504 spin_unlock(&peer_ni->ksnp_lock);
508 ksocknal_uncheck_zc_req(struct ksock_tx *tx)
510 struct ksock_peer_ni *peer_ni = tx->tx_conn->ksnc_peer;
512 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
513 LASSERT(tx->tx_zc_capable);
515 tx->tx_zc_checked = 0;
517 spin_lock(&peer_ni->ksnp_lock);
519 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
520 /* Not waiting for an ACK */
521 spin_unlock(&peer_ni->ksnp_lock);
525 tx->tx_msg.ksm_zc_cookies[0] = 0;
526 list_del(&tx->tx_zc_list);
528 spin_unlock(&peer_ni->ksnp_lock);
530 ksocknal_tx_decref(tx);
534 ksocknal_process_transmit(struct ksock_conn *conn, struct ksock_tx *tx)
537 bool error_sim = false;
539 if (lnet_send_error_simulation(tx->tx_lnetmsg, &tx->tx_hstatus)) {
545 if (tx->tx_zc_capable && !tx->tx_zc_checked)
546 ksocknal_check_zc_req(tx);
548 rc = ksocknal_transmit (conn, tx);
550 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
552 if (tx->tx_resid == 0) {
553 /* Sent everything OK */
565 counter++; /* exponential backoff warnings */
566 if ((counter & (-counter)) == counter)
567 CWARN("%u ENOMEM tx %p (%u allocated)\n",
568 counter, conn, atomic_read(&libcfs_kmemory));
570 /* Queue on ksnd_enomem_conns for retry after a timeout */
571 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
573 /* enomem list takes over scheduler's ref... */
574 LASSERT (conn->ksnc_tx_scheduled);
575 list_add_tail(&conn->ksnc_tx_list,
576 &ksocknal_data.ksnd_enomem_conns);
577 if (ktime_get_seconds() + SOCKNAL_ENOMEM_RETRY <
578 ksocknal_data.ksnd_reaper_waketime)
579 wake_up(&ksocknal_data.ksnd_reaper_waitq);
581 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
584 * set the health status of the message which determines
585 * whether we should retry the transmit
587 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
598 * set the health status of the message which determines
599 * whether we should retry the transmit
601 if (rc == -ETIMEDOUT)
602 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_TIMEOUT;
604 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
607 if (!conn->ksnc_closing) {
610 LCONSOLE_WARN("Host %pI4h reset our connection "
611 "while we were sending data; it may have "
616 LCONSOLE_WARN("There was an unexpected network error "
617 "while writing to %pI4h: %d.\n",
618 &conn->ksnc_ipaddr, rc);
621 CDEBUG(D_NET, "[%p] Error %d on write to %s ip %pI4h:%d\n",
622 conn, rc, libcfs_id2str(conn->ksnc_peer->ksnp_id),
623 &conn->ksnc_ipaddr, conn->ksnc_port);
626 if (tx->tx_zc_checked)
627 ksocknal_uncheck_zc_req(tx);
629 /* it's not an error if conn is being closed */
630 ksocknal_close_conn_and_siblings(conn,
631 (conn->ksnc_closing) ? 0 : rc);
637 ksocknal_launch_connection_locked(struct ksock_route *route)
640 /* called holding write lock on ksnd_global_lock */
642 LASSERT (!route->ksnr_scheduled);
643 LASSERT (!route->ksnr_connecting);
644 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
646 route->ksnr_scheduled = 1; /* scheduling conn for connd */
647 ksocknal_route_addref(route); /* extra ref for connd */
649 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
651 list_add_tail(&route->ksnr_connd_list,
652 &ksocknal_data.ksnd_connd_routes);
653 wake_up(&ksocknal_data.ksnd_connd_waitq);
655 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
659 ksocknal_launch_all_connections_locked(struct ksock_peer_ni *peer_ni)
661 struct ksock_route *route;
663 /* called holding write lock on ksnd_global_lock */
665 /* launch any/all connections that need it */
666 route = ksocknal_find_connectable_route_locked(peer_ni);
670 ksocknal_launch_connection_locked(route);
675 ksocknal_find_conn_locked(struct ksock_peer_ni *peer_ni, struct ksock_tx *tx, int nonblk)
677 struct list_head *tmp;
678 struct ksock_conn *conn;
679 struct ksock_conn *typed = NULL;
680 struct ksock_conn *fallback = NULL;
684 list_for_each(tmp, &peer_ni->ksnp_conns) {
685 struct ksock_conn *c = list_entry(tmp, struct ksock_conn,
687 int nob = atomic_read(&c->ksnc_tx_nob) +
688 c->ksnc_sock->sk->sk_wmem_queued;
691 LASSERT (!c->ksnc_closing);
692 LASSERT (c->ksnc_proto != NULL &&
693 c->ksnc_proto->pro_match_tx != NULL);
695 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
700 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
703 case SOCKNAL_MATCH_YES: /* typed connection */
704 if (typed == NULL || tnob > nob ||
705 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
706 typed->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
712 case SOCKNAL_MATCH_MAY: /* fallback connection */
713 if (fallback == NULL || fnob > nob ||
714 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
715 fallback->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
723 /* prefer the typed selection */
724 conn = (typed != NULL) ? typed : fallback;
727 conn->ksnc_tx_last_post = ktime_get_seconds();
733 ksocknal_tx_prep(struct ksock_conn *conn, struct ksock_tx *tx)
735 conn->ksnc_proto->pro_pack(tx);
737 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
738 ksocknal_conn_addref(conn); /* +1 ref for tx */
743 ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn)
745 struct ksock_sched *sched = conn->ksnc_scheduler;
746 struct ksock_msg *msg = &tx->tx_msg;
747 struct ksock_tx *ztx = NULL;
750 /* called holding global lock (read or irq-write) and caller may
751 * not have dropped this lock between finding conn and calling me,
752 * so we don't need the {get,put}connsock dance to deref
754 LASSERT(!conn->ksnc_closing);
756 CDEBUG(D_NET, "Sending to %s ip %pI4h:%d\n",
757 libcfs_id2str(conn->ksnc_peer->ksnp_id),
758 &conn->ksnc_ipaddr, conn->ksnc_port);
760 ksocknal_tx_prep(conn, tx);
762 /* Ensure the frags we've been given EXACTLY match the number of
763 * bytes we want to send. Many TCP/IP stacks disregard any total
764 * size parameters passed to them and just look at the frags.
766 * We always expect at least 1 mapped fragment containing the
767 * complete ksocknal message header. */
768 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
769 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
770 (unsigned int)tx->tx_nob);
771 LASSERT (tx->tx_niov >= 1);
772 LASSERT (tx->tx_resid == tx->tx_nob);
774 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
775 tx, (tx->tx_lnetmsg != NULL) ? tx->tx_lnetmsg->msg_hdr.type:
777 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
779 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
780 spin_lock_bh(&sched->kss_lock);
782 if (list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
783 /* First packet starts the timeout */
784 conn->ksnc_tx_deadline = ktime_get_seconds() +
785 lnet_get_lnd_timeout();
786 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
787 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
788 conn->ksnc_tx_bufnob = 0;
789 smp_mb(); /* order with adding to tx_queue */
792 if (msg->ksm_type == KSOCK_MSG_NOOP) {
793 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
794 * on a normal packet so I don't need to send it */
795 LASSERT (msg->ksm_zc_cookies[1] != 0);
796 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
798 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
799 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
802 /* It's a normal packet - can it piggback a noop zc-ack that
803 * has been queued already? */
804 LASSERT (msg->ksm_zc_cookies[1] == 0);
805 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
807 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
808 /* ztx will be released later */
812 atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
813 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
816 if (conn->ksnc_tx_ready && /* able to send */
817 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
818 /* +1 ref for scheduler */
819 ksocknal_conn_addref(conn);
820 list_add_tail(&conn->ksnc_tx_list,
821 &sched->kss_tx_conns);
822 conn->ksnc_tx_scheduled = 1;
823 wake_up(&sched->kss_waitq);
826 spin_unlock_bh(&sched->kss_lock);
831 ksocknal_find_connectable_route_locked(struct ksock_peer_ni *peer_ni)
833 time64_t now = ktime_get_seconds();
834 struct list_head *tmp;
835 struct ksock_route *route;
837 list_for_each(tmp, &peer_ni->ksnp_routes) {
838 route = list_entry(tmp, struct ksock_route, ksnr_list);
840 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
842 if (route->ksnr_scheduled) /* connections being established */
845 /* all route types connected ? */
846 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
849 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
850 now >= route->ksnr_timeout)) {
852 "Too soon to retry route %pI4h "
853 "(cnted %d, interval %lld, %lld secs later)\n",
855 route->ksnr_connected,
856 route->ksnr_retry_interval,
857 route->ksnr_timeout - now);
868 ksocknal_find_connecting_route_locked(struct ksock_peer_ni *peer_ni)
870 struct list_head *tmp;
871 struct ksock_route *route;
873 list_for_each(tmp, &peer_ni->ksnp_routes) {
874 route = list_entry(tmp, struct ksock_route, ksnr_list);
876 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
878 if (route->ksnr_scheduled)
886 ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
887 struct lnet_process_id id)
889 struct ksock_peer_ni *peer_ni;
890 struct ksock_conn *conn;
895 LASSERT (tx->tx_conn == NULL);
897 g_lock = &ksocknal_data.ksnd_global_lock;
899 for (retry = 0;; retry = 1) {
901 peer_ni = ksocknal_find_peer_locked(ni, id);
902 if (peer_ni != NULL) {
903 if (ksocknal_find_connectable_route_locked(peer_ni) == NULL) {
904 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
906 /* I've got no routes that need to be
907 * connecting and I do have an actual
909 ksocknal_queue_tx_locked (tx, conn);
916 /* I'll need a write lock... */
919 write_lock_bh(g_lock);
921 peer_ni = ksocknal_find_peer_locked(ni, id);
925 write_unlock_bh(g_lock);
927 if ((id.pid & LNET_PID_USERFLAG) != 0) {
928 CERROR("Refusing to create a connection to "
929 "userspace process %s\n", libcfs_id2str(id));
930 return -EHOSTUNREACH;
934 CERROR("Can't find peer_ni %s\n", libcfs_id2str(id));
935 return -EHOSTUNREACH;
938 rc = ksocknal_add_peer(ni, id,
939 LNET_NIDADDR(id.nid),
940 lnet_acceptor_port());
942 CERROR("Can't add peer_ni %s: %d\n",
943 libcfs_id2str(id), rc);
948 ksocknal_launch_all_connections_locked(peer_ni);
950 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
952 /* Connection exists; queue message on it */
953 ksocknal_queue_tx_locked (tx, conn);
954 write_unlock_bh(g_lock);
958 if (peer_ni->ksnp_accepting > 0 ||
959 ksocknal_find_connecting_route_locked (peer_ni) != NULL) {
960 /* the message is going to be pinned to the peer_ni */
961 tx->tx_deadline = ktime_get_seconds() +
962 lnet_get_lnd_timeout();
964 /* Queue the message until a connection is established */
965 list_add_tail(&tx->tx_list, &peer_ni->ksnp_tx_queue);
966 write_unlock_bh(g_lock);
970 write_unlock_bh(g_lock);
972 /* NB Routes may be ignored if connections to them failed recently */
973 CNETERR("No usable routes to %s\n", libcfs_id2str(id));
974 return (-EHOSTUNREACH);
978 ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
981 int type = lntmsg->msg_type;
982 struct lnet_process_id target = lntmsg->msg_target;
983 unsigned int payload_niov = lntmsg->msg_niov;
984 struct kvec *payload_iov = lntmsg->msg_iov;
985 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
986 unsigned int payload_offset = lntmsg->msg_offset;
987 unsigned int payload_nob = lntmsg->msg_len;
992 /* NB 'private' is different depending on what we're sending.
993 * Just ignore it... */
995 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
996 payload_nob, payload_niov, libcfs_id2str(target));
998 LASSERT (payload_nob == 0 || payload_niov > 0);
999 LASSERT (payload_niov <= LNET_MAX_IOV);
1000 /* payload is either all vaddrs or all pages */
1001 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1002 LASSERT (!in_interrupt ());
1004 if (payload_iov != NULL)
1005 desc_size = offsetof(struct ksock_tx,
1006 tx_frags.virt.iov[1 + payload_niov]);
1008 desc_size = offsetof(struct ksock_tx,
1009 tx_frags.paged.kiov[payload_niov]);
1011 if (lntmsg->msg_vmflush)
1012 mpflag = cfs_memory_pressure_get_and_set();
1013 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
1015 CERROR("Can't allocate tx desc type %d size %d\n",
1017 if (lntmsg->msg_vmflush)
1018 cfs_memory_pressure_restore(mpflag);
1022 tx->tx_conn = NULL; /* set when assigned a conn */
1023 tx->tx_lnetmsg = lntmsg;
1025 if (payload_iov != NULL) {
1028 tx->tx_iov = tx->tx_frags.virt.iov;
1030 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
1031 payload_niov, payload_iov,
1032 payload_offset, payload_nob);
1035 tx->tx_iov = &tx->tx_frags.paged.iov;
1036 tx->tx_kiov = tx->tx_frags.paged.kiov;
1037 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
1038 payload_niov, payload_kiov,
1039 payload_offset, payload_nob);
1041 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
1042 tx->tx_zc_capable = 1;
1045 tx->tx_msg.ksm_csum = 0;
1046 tx->tx_msg.ksm_type = KSOCK_MSG_LNET;
1047 tx->tx_msg.ksm_zc_cookies[0] = 0;
1048 tx->tx_msg.ksm_zc_cookies[1] = 0;
1050 /* The first fragment will be set later in pro_pack */
1051 rc = ksocknal_launch_packet(ni, tx, target);
1053 cfs_memory_pressure_restore(mpflag);
1058 ksocknal_free_tx(tx);
1063 ksocknal_thread_start(int (*fn)(void *arg), void *arg, char *name)
1065 struct task_struct *task = kthread_run(fn, arg, name);
1068 return PTR_ERR(task);
1070 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1071 ksocknal_data.ksnd_nthreads++;
1072 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1077 ksocknal_thread_fini (void)
1079 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1080 ksocknal_data.ksnd_nthreads--;
1081 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1085 ksocknal_new_packet(struct ksock_conn *conn, int nob_to_skip)
1087 static char ksocknal_slop_buffer[4096];
1092 LASSERT(conn->ksnc_proto != NULL);
1094 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1095 /* Remind the socket to ack eagerly... */
1096 ksocknal_lib_eager_ack(conn);
1099 if (nob_to_skip == 0) { /* right at next packet boundary now */
1100 conn->ksnc_rx_started = 0;
1101 smp_mb(); /* racing with timeout thread */
1103 switch (conn->ksnc_proto->pro_version) {
1104 case KSOCK_PROTO_V2:
1105 case KSOCK_PROTO_V3:
1106 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1107 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1108 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1110 conn->ksnc_rx_nob_wanted = offsetof(struct ksock_msg, ksm_u);
1111 conn->ksnc_rx_nob_left = offsetof(struct ksock_msg, ksm_u);
1112 conn->ksnc_rx_iov[0].iov_len = offsetof(struct ksock_msg, ksm_u);
1115 case KSOCK_PROTO_V1:
1116 /* Receiving bare struct lnet_hdr */
1117 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1118 conn->ksnc_rx_nob_wanted = sizeof(struct lnet_hdr);
1119 conn->ksnc_rx_nob_left = sizeof(struct lnet_hdr);
1121 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1122 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1123 conn->ksnc_rx_iov[0].iov_len = sizeof(struct lnet_hdr);
1129 conn->ksnc_rx_niov = 1;
1131 conn->ksnc_rx_kiov = NULL;
1132 conn->ksnc_rx_nkiov = 0;
1133 conn->ksnc_rx_csum = ~0;
1137 /* Set up to skip as much as possible now. If there's more left
1138 * (ran out of iov entries) we'll get called again */
1140 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1141 conn->ksnc_rx_nob_left = nob_to_skip;
1142 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1147 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1149 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1150 conn->ksnc_rx_iov[niov].iov_len = nob;
1155 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1156 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof(struct kvec));
1158 conn->ksnc_rx_niov = niov;
1159 conn->ksnc_rx_kiov = NULL;
1160 conn->ksnc_rx_nkiov = 0;
1161 conn->ksnc_rx_nob_wanted = skipped;
1166 ksocknal_process_receive(struct ksock_conn *conn)
1168 struct lnet_hdr *lhdr;
1169 struct lnet_process_id *id;
1172 LASSERT (atomic_read(&conn->ksnc_conn_refcount) > 0);
1174 /* NB: sched lock NOT held */
1175 /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */
1176 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1177 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1178 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1179 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1181 if (conn->ksnc_rx_nob_wanted != 0) {
1182 rc = ksocknal_receive(conn);
1185 struct lnet_process_id ksnp_id;
1187 ksnp_id = conn->ksnc_peer->ksnp_id;
1189 LASSERT(rc != -EAGAIN);
1191 CDEBUG(D_NET, "[%p] EOF from %s "
1192 "ip %pI4h:%d\n", conn,
1193 libcfs_id2str(ksnp_id),
1196 else if (!conn->ksnc_closing)
1197 CERROR("[%p] Error %d on read from %s "
1198 "ip %pI4h:%d\n", conn, rc,
1199 libcfs_id2str(ksnp_id),
1203 /* it's not an error if conn is being closed */
1204 ksocknal_close_conn_and_siblings (conn,
1205 (conn->ksnc_closing) ? 0 : rc);
1206 return (rc == 0 ? -ESHUTDOWN : rc);
1209 if (conn->ksnc_rx_nob_wanted != 0) {
1214 switch (conn->ksnc_rx_state) {
1215 case SOCKNAL_RX_KSM_HEADER:
1216 if (conn->ksnc_flip) {
1217 __swab32s(&conn->ksnc_msg.ksm_type);
1218 __swab32s(&conn->ksnc_msg.ksm_csum);
1219 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1220 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1223 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1224 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1225 CERROR("%s: Unknown message type: %x\n",
1226 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1227 conn->ksnc_msg.ksm_type);
1228 ksocknal_new_packet(conn, 0);
1229 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1233 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1234 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1235 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1236 /* NOOP Checksum error */
1237 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1238 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1239 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1240 ksocknal_new_packet(conn, 0);
1241 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1245 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1248 LASSERT (conn->ksnc_proto != &ksocknal_protocol_v1x);
1250 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1251 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1253 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1254 conn->ksnc_msg.ksm_zc_cookies[1]);
1257 CERROR("%s: Unknown ZC-ACK cookie: %llu, %llu\n",
1258 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1259 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1260 ksocknal_new_packet(conn, 0);
1261 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1266 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1267 ksocknal_new_packet (conn, 0);
1268 return 0; /* NOOP is done and just return */
1271 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1272 conn->ksnc_rx_nob_wanted = sizeof(struct ksock_lnet_msg);
1273 conn->ksnc_rx_nob_left = sizeof(struct ksock_lnet_msg);
1275 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1276 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1277 conn->ksnc_rx_iov[0].iov_len = sizeof(struct ksock_lnet_msg);
1279 conn->ksnc_rx_niov = 1;
1280 conn->ksnc_rx_kiov = NULL;
1281 conn->ksnc_rx_nkiov = 0;
1283 goto again; /* read lnet header now */
1285 case SOCKNAL_RX_LNET_HEADER:
1286 /* unpack message header */
1287 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1289 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1290 /* Userspace peer_ni */
1291 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1292 id = &conn->ksnc_peer->ksnp_id;
1294 /* Substitute process ID assigned at connection time */
1295 lhdr->src_pid = cpu_to_le32(id->pid);
1296 lhdr->src_nid = cpu_to_le64(id->nid);
1299 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1300 ksocknal_conn_addref(conn); /* ++ref while parsing */
1302 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1303 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1304 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1306 /* I just received garbage: give up on this conn */
1307 ksocknal_new_packet(conn, 0);
1308 ksocknal_close_conn_and_siblings (conn, rc);
1309 ksocknal_conn_decref(conn);
1313 /* I'm racing with ksocknal_recv() */
1314 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1315 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1317 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1320 /* ksocknal_recv() got called */
1323 case SOCKNAL_RX_LNET_PAYLOAD:
1324 /* payload all received */
1327 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1328 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1329 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1330 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1331 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1332 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1336 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1337 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1339 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1340 id = &conn->ksnc_peer->ksnp_id;
1342 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1343 conn->ksnc_msg.ksm_zc_cookies[0],
1344 *ksocknal_tunables.ksnd_nonblk_zcack ||
1345 le64_to_cpu(lhdr->src_nid) != id->nid);
1348 lnet_finalize(conn->ksnc_cookie, rc);
1351 ksocknal_new_packet(conn, 0);
1352 ksocknal_close_conn_and_siblings (conn, rc);
1357 case SOCKNAL_RX_SLOP:
1358 /* starting new packet? */
1359 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1360 return 0; /* come back later */
1361 goto again; /* try to finish reading slop now */
1369 return (-EINVAL); /* keep gcc happy */
1373 ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
1374 int delayed, unsigned int niov, struct kvec *iov,
1375 lnet_kiov_t *kiov, unsigned int offset, unsigned int mlen,
1378 struct ksock_conn *conn = private;
1379 struct ksock_sched *sched = conn->ksnc_scheduler;
1381 LASSERT (mlen <= rlen);
1382 LASSERT (niov <= LNET_MAX_IOV);
1384 conn->ksnc_cookie = msg;
1385 conn->ksnc_rx_nob_wanted = mlen;
1386 conn->ksnc_rx_nob_left = rlen;
1388 if (mlen == 0 || iov != NULL) {
1389 conn->ksnc_rx_nkiov = 0;
1390 conn->ksnc_rx_kiov = NULL;
1391 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1392 conn->ksnc_rx_niov =
1393 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1394 niov, iov, offset, mlen);
1396 conn->ksnc_rx_niov = 0;
1397 conn->ksnc_rx_iov = NULL;
1398 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1399 conn->ksnc_rx_nkiov =
1400 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1401 niov, kiov, offset, mlen);
1405 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1406 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1408 LASSERT (conn->ksnc_rx_scheduled);
1410 spin_lock_bh(&sched->kss_lock);
1412 switch (conn->ksnc_rx_state) {
1413 case SOCKNAL_RX_PARSE_WAIT:
1414 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1415 wake_up(&sched->kss_waitq);
1416 LASSERT(conn->ksnc_rx_ready);
1419 case SOCKNAL_RX_PARSE:
1420 /* scheduler hasn't noticed I'm parsing yet */
1424 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1426 spin_unlock_bh(&sched->kss_lock);
1427 ksocknal_conn_decref(conn);
1432 ksocknal_sched_cansleep(struct ksock_sched *sched)
1436 spin_lock_bh(&sched->kss_lock);
1438 rc = (!ksocknal_data.ksnd_shuttingdown &&
1439 list_empty(&sched->kss_rx_conns) &&
1440 list_empty(&sched->kss_tx_conns));
1442 spin_unlock_bh(&sched->kss_lock);
1446 int ksocknal_scheduler(void *arg)
1448 struct ksock_sched_info *info;
1449 struct ksock_sched *sched;
1450 struct ksock_conn *conn;
1451 struct ksock_tx *tx;
1454 long id = (long)arg;
1456 info = ksocknal_data.ksnd_sched_info[KSOCK_THREAD_CPT(id)];
1457 sched = &info->ksi_scheds[KSOCK_THREAD_SID(id)];
1459 cfs_block_allsigs();
1461 rc = cfs_cpt_bind(lnet_cpt_table(), info->ksi_cpt);
1463 CWARN("Can't set CPU partition affinity to %d: %d\n",
1467 spin_lock_bh(&sched->kss_lock);
1469 while (!ksocknal_data.ksnd_shuttingdown) {
1470 int did_something = 0;
1472 /* Ensure I progress everything semi-fairly */
1474 if (!list_empty(&sched->kss_rx_conns)) {
1475 conn = list_entry(sched->kss_rx_conns.next,
1476 struct ksock_conn, ksnc_rx_list);
1477 list_del(&conn->ksnc_rx_list);
1479 LASSERT(conn->ksnc_rx_scheduled);
1480 LASSERT(conn->ksnc_rx_ready);
1482 /* clear rx_ready in case receive isn't complete.
1483 * Do it BEFORE we call process_recv, since
1484 * data_ready can set it any time after we release
1486 conn->ksnc_rx_ready = 0;
1487 spin_unlock_bh(&sched->kss_lock);
1489 rc = ksocknal_process_receive(conn);
1491 spin_lock_bh(&sched->kss_lock);
1493 /* I'm the only one that can clear this flag */
1494 LASSERT(conn->ksnc_rx_scheduled);
1496 /* Did process_receive get everything it wanted? */
1498 conn->ksnc_rx_ready = 1;
1500 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1501 /* Conn blocked waiting for ksocknal_recv()
1502 * I change its state (under lock) to signal
1503 * it can be rescheduled */
1504 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1505 } else if (conn->ksnc_rx_ready) {
1506 /* reschedule for rx */
1507 list_add_tail(&conn->ksnc_rx_list,
1508 &sched->kss_rx_conns);
1510 conn->ksnc_rx_scheduled = 0;
1512 ksocknal_conn_decref(conn);
1518 if (!list_empty(&sched->kss_tx_conns)) {
1519 struct list_head zlist = LIST_HEAD_INIT(zlist);
1521 if (!list_empty(&sched->kss_zombie_noop_txs)) {
1523 &sched->kss_zombie_noop_txs);
1524 list_del_init(&sched->kss_zombie_noop_txs);
1527 conn = list_entry(sched->kss_tx_conns.next,
1528 struct ksock_conn, ksnc_tx_list);
1529 list_del(&conn->ksnc_tx_list);
1531 LASSERT(conn->ksnc_tx_scheduled);
1532 LASSERT(conn->ksnc_tx_ready);
1533 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1535 tx = list_entry(conn->ksnc_tx_queue.next,
1536 struct ksock_tx, tx_list);
1538 if (conn->ksnc_tx_carrier == tx)
1539 ksocknal_next_tx_carrier(conn);
1541 /* dequeue now so empty list => more to send */
1542 list_del(&tx->tx_list);
1544 /* Clear tx_ready in case send isn't complete. Do
1545 * it BEFORE we call process_transmit, since
1546 * write_space can set it any time after we release
1548 conn->ksnc_tx_ready = 0;
1549 spin_unlock_bh(&sched->kss_lock);
1551 if (!list_empty(&zlist)) {
1552 /* free zombie noop txs, it's fast because
1553 * noop txs are just put in freelist */
1554 ksocknal_txlist_done(NULL, &zlist, 0);
1557 rc = ksocknal_process_transmit(conn, tx);
1559 if (rc == -ENOMEM || rc == -EAGAIN) {
1560 /* Incomplete send: replace tx on HEAD of tx_queue */
1561 spin_lock_bh(&sched->kss_lock);
1562 list_add(&tx->tx_list,
1563 &conn->ksnc_tx_queue);
1565 /* Complete send; tx -ref */
1566 ksocknal_tx_decref(tx);
1568 spin_lock_bh(&sched->kss_lock);
1569 /* assume space for more */
1570 conn->ksnc_tx_ready = 1;
1573 if (rc == -ENOMEM) {
1574 /* Do nothing; after a short timeout, this
1575 * conn will be reposted on kss_tx_conns. */
1576 } else if (conn->ksnc_tx_ready &&
1577 !list_empty(&conn->ksnc_tx_queue)) {
1578 /* reschedule for tx */
1579 list_add_tail(&conn->ksnc_tx_list,
1580 &sched->kss_tx_conns);
1582 conn->ksnc_tx_scheduled = 0;
1584 ksocknal_conn_decref(conn);
1589 if (!did_something || /* nothing to do */
1590 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1591 spin_unlock_bh(&sched->kss_lock);
1595 if (!did_something) { /* wait for something to do */
1596 rc = wait_event_interruptible_exclusive(
1598 !ksocknal_sched_cansleep(sched));
1604 spin_lock_bh(&sched->kss_lock);
1608 spin_unlock_bh(&sched->kss_lock);
1609 ksocknal_thread_fini();
1614 * Add connection to kss_rx_conns of scheduler
1615 * and wakeup the scheduler.
1617 void ksocknal_read_callback(struct ksock_conn *conn)
1619 struct ksock_sched *sched;
1622 sched = conn->ksnc_scheduler;
1624 spin_lock_bh(&sched->kss_lock);
1626 conn->ksnc_rx_ready = 1;
1628 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1629 list_add_tail(&conn->ksnc_rx_list,
1630 &sched->kss_rx_conns);
1631 conn->ksnc_rx_scheduled = 1;
1632 /* extra ref for scheduler */
1633 ksocknal_conn_addref(conn);
1635 wake_up (&sched->kss_waitq);
1637 spin_unlock_bh(&sched->kss_lock);
1643 * Add connection to kss_tx_conns of scheduler
1644 * and wakeup the scheduler.
1646 void ksocknal_write_callback(struct ksock_conn *conn)
1648 struct ksock_sched *sched;
1651 sched = conn->ksnc_scheduler;
1653 spin_lock_bh(&sched->kss_lock);
1655 conn->ksnc_tx_ready = 1;
1657 if (!conn->ksnc_tx_scheduled && /* not being progressed */
1658 !list_empty(&conn->ksnc_tx_queue)) { /* packets to send */
1659 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
1660 conn->ksnc_tx_scheduled = 1;
1661 /* extra ref for scheduler */
1662 ksocknal_conn_addref(conn);
1664 wake_up(&sched->kss_waitq);
1667 spin_unlock_bh(&sched->kss_lock);
1672 static struct ksock_proto *
1673 ksocknal_parse_proto_version (struct ksock_hello_msg *hello)
1677 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1678 version = hello->kshm_version;
1679 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1680 version = __swab32(hello->kshm_version);
1683 #if SOCKNAL_VERSION_DEBUG
1684 if (*ksocknal_tunables.ksnd_protocol == 1)
1687 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1688 version == KSOCK_PROTO_V3)
1691 if (version == KSOCK_PROTO_V2)
1692 return &ksocknal_protocol_v2x;
1694 if (version == KSOCK_PROTO_V3)
1695 return &ksocknal_protocol_v3x;
1700 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1701 struct lnet_magicversion *hmv;
1703 CLASSERT(sizeof(struct lnet_magicversion) ==
1704 offsetof(struct ksock_hello_msg, kshm_src_nid));
1706 hmv = (struct lnet_magicversion *)hello;
1708 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1709 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1710 return &ksocknal_protocol_v1x;
1717 ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1718 lnet_nid_t peer_nid, struct ksock_hello_msg *hello)
1720 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1721 struct ksock_net *net = (struct ksock_net *)ni->ni_data;
1723 LASSERT(hello->kshm_nips <= LNET_INTERFACES_NUM);
1725 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1726 LASSERT(conn->ksnc_proto != NULL);
1728 hello->kshm_src_nid = ni->ni_nid;
1729 hello->kshm_dst_nid = peer_nid;
1730 hello->kshm_src_pid = the_lnet.ln_pid;
1732 hello->kshm_src_incarnation = net->ksnn_incarnation;
1733 hello->kshm_ctype = conn->ksnc_type;
1735 return conn->ksnc_proto->pro_send_hello(conn, hello);
1739 ksocknal_invert_type(int type)
1743 case SOCKLND_CONN_ANY:
1744 case SOCKLND_CONN_CONTROL:
1746 case SOCKLND_CONN_BULK_IN:
1747 return SOCKLND_CONN_BULK_OUT;
1748 case SOCKLND_CONN_BULK_OUT:
1749 return SOCKLND_CONN_BULK_IN;
1751 return (SOCKLND_CONN_NONE);
1756 ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1757 struct ksock_hello_msg *hello,
1758 struct lnet_process_id *peerid,
1761 /* Return < 0 fatal error
1763 * EALREADY lost connection race
1764 * EPROTO protocol version mismatch
1766 struct socket *sock = conn->ksnc_sock;
1767 int active = (conn->ksnc_proto != NULL);
1771 struct ksock_proto *proto;
1772 struct lnet_process_id recv_id;
1774 /* socket type set on active connections - not set on passive */
1775 LASSERT(!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1777 timeout = active ? lnet_get_lnd_timeout() :
1778 lnet_acceptor_timeout();
1780 rc = lnet_sock_read(sock, &hello->kshm_magic,
1781 sizeof(hello->kshm_magic), timeout);
1783 CERROR("Error %d reading HELLO from %pI4h\n",
1784 rc, &conn->ksnc_ipaddr);
1789 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1790 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1791 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
1792 /* Unexpected magic! */
1793 CERROR ("Bad magic(1) %#08x (%#08x expected) from "
1794 "%pI4h\n", __cpu_to_le32 (hello->kshm_magic),
1795 LNET_PROTO_TCP_MAGIC, &conn->ksnc_ipaddr);
1799 rc = lnet_sock_read(sock, &hello->kshm_version,
1800 sizeof(hello->kshm_version), timeout);
1802 CERROR("Error %d reading HELLO from %pI4h\n",
1803 rc, &conn->ksnc_ipaddr);
1808 proto = ksocknal_parse_proto_version(hello);
1809 if (proto == NULL) {
1811 /* unknown protocol from peer_ni, tell peer_ni my protocol */
1812 conn->ksnc_proto = &ksocknal_protocol_v3x;
1813 #if SOCKNAL_VERSION_DEBUG
1814 if (*ksocknal_tunables.ksnd_protocol == 2)
1815 conn->ksnc_proto = &ksocknal_protocol_v2x;
1816 else if (*ksocknal_tunables.ksnd_protocol == 1)
1817 conn->ksnc_proto = &ksocknal_protocol_v1x;
1819 hello->kshm_nips = 0;
1820 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1823 CERROR("Unknown protocol version (%d.x expected) from %pI4h\n",
1824 conn->ksnc_proto->pro_version, &conn->ksnc_ipaddr);
1829 proto_match = (conn->ksnc_proto == proto);
1830 conn->ksnc_proto = proto;
1832 /* receive the rest of hello message anyway */
1833 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1835 CERROR("Error %d reading or checking hello from from %pI4h\n",
1836 rc, &conn->ksnc_ipaddr);
1841 *incarnation = hello->kshm_src_incarnation;
1843 if (hello->kshm_src_nid == LNET_NID_ANY) {
1844 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
1845 "from %pI4h\n", &conn->ksnc_ipaddr);
1850 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1851 /* Userspace NAL assigns peer_ni process ID from socket */
1852 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1853 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
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 %pI4h\n",
1866 hello->kshm_ctype, libcfs_id2str(*peerid),
1867 &conn->ksnc_ipaddr);
1873 if (peerid->pid != recv_id.pid ||
1874 peerid->nid != recv_id.nid) {
1875 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1876 " %pI4h, but they claimed they were "
1877 "%s; please check your Lustre "
1879 libcfs_id2str(*peerid),
1881 libcfs_id2str(recv_id));
1885 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1886 /* Possible protocol mismatch or I lost the connection race */
1887 return proto_match ? EALREADY : EPROTO;
1890 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1891 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
1892 conn->ksnc_type, libcfs_id2str(*peerid),
1901 ksocknal_connect(struct ksock_route *route)
1903 struct list_head zombies = LIST_HEAD_INIT(zombies);
1904 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
1907 struct socket *sock;
1909 int retry_later = 0;
1912 deadline = ktime_get_seconds() + lnet_get_lnd_timeout();
1914 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1916 LASSERT (route->ksnr_scheduled);
1917 LASSERT (!route->ksnr_connecting);
1919 route->ksnr_connecting = 1;
1922 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1924 /* stop connecting if peer_ni/route got closed under me, or
1925 * route got connected while queued */
1926 if (peer_ni->ksnp_closing || route->ksnr_deleted ||
1932 /* reschedule if peer_ni is connecting to me */
1933 if (peer_ni->ksnp_accepting > 0) {
1935 "peer_ni %s(%d) already connecting to me, retry later.\n",
1936 libcfs_nid2str(peer_ni->ksnp_id.nid), peer_ni->ksnp_accepting);
1940 if (retry_later) /* needs reschedule */
1943 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
1944 type = SOCKLND_CONN_ANY;
1945 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
1946 type = SOCKLND_CONN_CONTROL;
1947 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
1948 type = SOCKLND_CONN_BULK_IN;
1950 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
1951 type = SOCKLND_CONN_BULK_OUT;
1954 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1956 if (ktime_get_seconds() >= deadline) {
1958 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
1964 rc = lnet_connect(&sock, peer_ni->ksnp_id.nid,
1965 route->ksnr_myipaddr,
1966 route->ksnr_ipaddr, route->ksnr_port);
1970 rc = ksocknal_create_conn(peer_ni->ksnp_ni, route, sock, type);
1972 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
1978 /* A +ve RC means I have to retry because I lost the connection
1979 * race or I have to renegotiate protocol version */
1980 retry_later = (rc != 0);
1982 CDEBUG(D_NET, "peer_ni %s: conn race, retry later.\n",
1983 libcfs_nid2str(peer_ni->ksnp_id.nid));
1985 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1988 route->ksnr_scheduled = 0;
1989 route->ksnr_connecting = 0;
1992 /* re-queue for attention; this frees me up to handle
1993 * the peer_ni's incoming connection request */
1995 if (rc == EALREADY ||
1996 (rc == 0 && peer_ni->ksnp_accepting > 0)) {
1997 /* We want to introduce a delay before next
1998 * attempt to connect if we lost conn race,
1999 * but the race is resolved quickly usually,
2000 * so min_reconnectms should be good heuristic */
2001 route->ksnr_retry_interval = *ksocknal_tunables.ksnd_min_reconnectms / 1000;
2002 route->ksnr_timeout = ktime_get_seconds() +
2003 route->ksnr_retry_interval;
2006 ksocknal_launch_connection_locked(route);
2009 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2013 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2015 route->ksnr_scheduled = 0;
2016 route->ksnr_connecting = 0;
2018 /* This is a retry rather than a new connection */
2019 route->ksnr_retry_interval *= 2;
2020 route->ksnr_retry_interval =
2021 max_t(time64_t, route->ksnr_retry_interval,
2022 *ksocknal_tunables.ksnd_min_reconnectms / 1000);
2023 route->ksnr_retry_interval =
2024 min_t(time64_t, route->ksnr_retry_interval,
2025 *ksocknal_tunables.ksnd_max_reconnectms / 1000);
2027 LASSERT(route->ksnr_retry_interval);
2028 route->ksnr_timeout = ktime_get_seconds() + route->ksnr_retry_interval;
2030 if (!list_empty(&peer_ni->ksnp_tx_queue) &&
2031 peer_ni->ksnp_accepting == 0 &&
2032 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
2033 struct ksock_conn *conn;
2035 /* ksnp_tx_queue is queued on a conn on successful
2036 * connection for V1.x and V2.x */
2037 if (!list_empty(&peer_ni->ksnp_conns)) {
2038 conn = list_entry(peer_ni->ksnp_conns.next,
2039 struct ksock_conn, ksnc_list);
2040 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
2043 /* take all the blocked packets while I've got the lock and
2044 * complete below... */
2045 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
2048 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2050 ksocknal_peer_failed(peer_ni);
2051 ksocknal_txlist_done(peer_ni->ksnp_ni, &zombies, rc);
2056 * check whether we need to create more connds.
2057 * It will try to create new thread if it's necessary, @timeout can
2058 * be updated if failed to create, so caller wouldn't keep try while
2059 * running out of resource.
2062 ksocknal_connd_check_start(time64_t sec, long *timeout)
2066 int total = ksocknal_data.ksnd_connd_starting +
2067 ksocknal_data.ksnd_connd_running;
2069 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2070 /* still in initializing */
2074 if (total >= *ksocknal_tunables.ksnd_nconnds_max ||
2075 total > ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV) {
2076 /* can't create more connd, or still have enough
2077 * threads to handle more connecting */
2081 if (list_empty(&ksocknal_data.ksnd_connd_routes)) {
2082 /* no pending connecting request */
2086 if (sec - ksocknal_data.ksnd_connd_failed_stamp <= 1) {
2087 /* may run out of resource, retry later */
2088 *timeout = cfs_time_seconds(1);
2092 if (ksocknal_data.ksnd_connd_starting > 0) {
2093 /* serialize starting to avoid flood */
2097 ksocknal_data.ksnd_connd_starting_stamp = sec;
2098 ksocknal_data.ksnd_connd_starting++;
2099 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2101 /* NB: total is the next id */
2102 snprintf(name, sizeof(name), "socknal_cd%02d", total);
2103 rc = ksocknal_thread_start(ksocknal_connd, NULL, name);
2105 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2110 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2111 ksocknal_data.ksnd_connd_starting--;
2112 ksocknal_data.ksnd_connd_failed_stamp = ktime_get_real_seconds();
2118 * check whether current thread can exit, it will return 1 if there are too
2119 * many threads and no creating in past 120 seconds.
2120 * Also, this function may update @timeout to make caller come back
2121 * again to recheck these conditions.
2124 ksocknal_connd_check_stop(time64_t sec, long *timeout)
2128 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2129 /* still in initializing */
2133 if (ksocknal_data.ksnd_connd_starting > 0) {
2134 /* in progress of starting new thread */
2138 if (ksocknal_data.ksnd_connd_running <=
2139 *ksocknal_tunables.ksnd_nconnds) { /* can't shrink */
2143 /* created thread in past 120 seconds? */
2144 val = (int)(ksocknal_data.ksnd_connd_starting_stamp +
2145 SOCKNAL_CONND_TIMEOUT - sec);
2147 *timeout = (val > 0) ? cfs_time_seconds(val) :
2148 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT);
2152 /* no creating in past 120 seconds */
2154 return ksocknal_data.ksnd_connd_running >
2155 ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV;
2158 /* Go through connd_routes queue looking for a route that we can process
2159 * right now, @timeout_p can be updated if we need to come back later */
2160 static struct ksock_route *
2161 ksocknal_connd_get_route_locked(signed long *timeout_p)
2163 time64_t now = ktime_get_seconds();
2164 struct ksock_route *route;
2166 /* connd_routes can contain both pending and ordinary routes */
2167 list_for_each_entry(route, &ksocknal_data.ksnd_connd_routes,
2170 if (route->ksnr_retry_interval == 0 ||
2171 now >= route->ksnr_timeout)
2174 if (*timeout_p == MAX_SCHEDULE_TIMEOUT ||
2175 *timeout_p > cfs_time_seconds(route->ksnr_timeout - now))
2176 *timeout_p = cfs_time_seconds(route->ksnr_timeout - now);
2183 ksocknal_connd(void *arg)
2185 spinlock_t *connd_lock = &ksocknal_data.ksnd_connd_lock;
2186 struct ksock_connreq *cr;
2187 wait_queue_entry_t wait;
2191 cfs_block_allsigs();
2193 init_waitqueue_entry(&wait, current);
2195 spin_lock_bh(connd_lock);
2197 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2198 ksocknal_data.ksnd_connd_starting--;
2199 ksocknal_data.ksnd_connd_running++;
2201 while (!ksocknal_data.ksnd_shuttingdown) {
2202 struct ksock_route *route = NULL;
2203 time64_t sec = ktime_get_real_seconds();
2204 long timeout = MAX_SCHEDULE_TIMEOUT;
2205 int dropped_lock = 0;
2207 if (ksocknal_connd_check_stop(sec, &timeout)) {
2208 /* wakeup another one to check stop */
2209 wake_up(&ksocknal_data.ksnd_connd_waitq);
2213 if (ksocknal_connd_check_start(sec, &timeout)) {
2214 /* created new thread */
2218 if (!list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2219 /* Connection accepted by the listener */
2220 cr = list_entry(ksocknal_data.ksnd_connd_connreqs.next,
2221 struct ksock_connreq, ksncr_list);
2223 list_del(&cr->ksncr_list);
2224 spin_unlock_bh(connd_lock);
2227 ksocknal_create_conn(cr->ksncr_ni, NULL,
2228 cr->ksncr_sock, SOCKLND_CONN_NONE);
2229 lnet_ni_decref(cr->ksncr_ni);
2230 LIBCFS_FREE(cr, sizeof(*cr));
2232 spin_lock_bh(connd_lock);
2235 /* Only handle an outgoing connection request if there
2236 * is a thread left to handle incoming connections and
2237 * create new connd */
2238 if (ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV <
2239 ksocknal_data.ksnd_connd_running) {
2240 route = ksocknal_connd_get_route_locked(&timeout);
2242 if (route != NULL) {
2243 list_del(&route->ksnr_connd_list);
2244 ksocknal_data.ksnd_connd_connecting++;
2245 spin_unlock_bh(connd_lock);
2248 if (ksocknal_connect(route)) {
2249 /* consecutive retry */
2250 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2251 CWARN("massive consecutive "
2252 "re-connecting to %pI4h\n",
2253 &route->ksnr_ipaddr);
2260 ksocknal_route_decref(route);
2262 spin_lock_bh(connd_lock);
2263 ksocknal_data.ksnd_connd_connecting--;
2267 if (++nloops < SOCKNAL_RESCHED)
2269 spin_unlock_bh(connd_lock);
2272 spin_lock_bh(connd_lock);
2276 /* Nothing to do for 'timeout' */
2277 set_current_state(TASK_INTERRUPTIBLE);
2278 add_wait_queue_exclusive(&ksocknal_data.ksnd_connd_waitq, &wait);
2279 spin_unlock_bh(connd_lock);
2282 schedule_timeout(timeout);
2284 set_current_state(TASK_RUNNING);
2285 remove_wait_queue(&ksocknal_data.ksnd_connd_waitq, &wait);
2286 spin_lock_bh(connd_lock);
2288 ksocknal_data.ksnd_connd_running--;
2289 spin_unlock_bh(connd_lock);
2291 ksocknal_thread_fini();
2295 static struct ksock_conn *
2296 ksocknal_find_timed_out_conn(struct ksock_peer_ni *peer_ni)
2298 /* We're called with a shared lock on ksnd_global_lock */
2299 struct ksock_conn *conn;
2300 struct list_head *ctmp;
2301 struct ksock_tx *tx;
2303 list_for_each(ctmp, &peer_ni->ksnp_conns) {
2306 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
2308 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2309 LASSERT (!conn->ksnc_closing);
2311 error = conn->ksnc_sock->sk->sk_err;
2313 ksocknal_conn_addref(conn);
2317 CNETERR("A connection with %s "
2318 "(%pI4h:%d) was reset; "
2319 "it may have rebooted.\n",
2320 libcfs_id2str(peer_ni->ksnp_id),
2325 CNETERR("A connection with %s "
2326 "(%pI4h:%d) timed out; the "
2327 "network or node may be down.\n",
2328 libcfs_id2str(peer_ni->ksnp_id),
2333 CNETERR("An unexpected network error %d "
2335 "(%pI4h:%d\n", error,
2336 libcfs_id2str(peer_ni->ksnp_id),
2345 if (conn->ksnc_rx_started &&
2346 ktime_get_seconds() >= conn->ksnc_rx_deadline) {
2347 /* Timed out incomplete incoming message */
2348 ksocknal_conn_addref(conn);
2349 CNETERR("Timeout receiving from %s (%pI4h:%d), "
2350 "state %d wanted %d left %d\n",
2351 libcfs_id2str(peer_ni->ksnp_id),
2354 conn->ksnc_rx_state,
2355 conn->ksnc_rx_nob_wanted,
2356 conn->ksnc_rx_nob_left);
2360 if ((!list_empty(&conn->ksnc_tx_queue) ||
2361 conn->ksnc_sock->sk->sk_wmem_queued != 0) &&
2362 ktime_get_seconds() >= conn->ksnc_tx_deadline) {
2363 /* Timed out messages queued for sending or
2364 * buffered in the socket's send buffer */
2365 ksocknal_conn_addref(conn);
2366 list_for_each_entry(tx, &conn->ksnc_tx_queue,
2369 LNET_MSG_STATUS_LOCAL_TIMEOUT;
2370 CNETERR("Timeout sending data to %s (%pI4h:%d) "
2371 "the network or that node may be down.\n",
2372 libcfs_id2str(peer_ni->ksnp_id),
2373 &conn->ksnc_ipaddr, conn->ksnc_port);
2382 ksocknal_flush_stale_txs(struct ksock_peer_ni *peer_ni)
2384 struct ksock_tx *tx;
2385 struct list_head stale_txs = LIST_HEAD_INIT(stale_txs);
2387 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2389 while (!list_empty(&peer_ni->ksnp_tx_queue)) {
2390 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2391 struct ksock_tx, tx_list);
2393 if (ktime_get_seconds() < tx->tx_deadline)
2396 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2398 list_del(&tx->tx_list);
2399 list_add_tail(&tx->tx_list, &stale_txs);
2402 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2404 ksocknal_txlist_done(peer_ni->ksnp_ni, &stale_txs, -ETIMEDOUT);
2408 ksocknal_send_keepalive_locked(struct ksock_peer_ni *peer_ni)
2409 __must_hold(&ksocknal_data.ksnd_global_lock)
2411 struct ksock_sched *sched;
2412 struct ksock_conn *conn;
2413 struct ksock_tx *tx;
2415 /* last_alive will be updated by create_conn */
2416 if (list_empty(&peer_ni->ksnp_conns))
2419 if (peer_ni->ksnp_proto != &ksocknal_protocol_v3x)
2422 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2423 ktime_get_seconds() < peer_ni->ksnp_last_alive +
2424 *ksocknal_tunables.ksnd_keepalive)
2427 if (ktime_get_seconds() < peer_ni->ksnp_send_keepalive)
2430 /* retry 10 secs later, so we wouldn't put pressure
2431 * on this peer_ni if we failed to send keepalive this time */
2432 peer_ni->ksnp_send_keepalive = ktime_get_seconds() + 10;
2434 conn = ksocknal_find_conn_locked(peer_ni, NULL, 1);
2436 sched = conn->ksnc_scheduler;
2438 spin_lock_bh(&sched->kss_lock);
2439 if (!list_empty(&conn->ksnc_tx_queue)) {
2440 spin_unlock_bh(&sched->kss_lock);
2441 /* there is an queued ACK, don't need keepalive */
2445 spin_unlock_bh(&sched->kss_lock);
2448 read_unlock(&ksocknal_data.ksnd_global_lock);
2450 /* cookie = 1 is reserved for keepalive PING */
2451 tx = ksocknal_alloc_tx_noop(1, 1);
2453 read_lock(&ksocknal_data.ksnd_global_lock);
2457 if (ksocknal_launch_packet(peer_ni->ksnp_ni, tx, peer_ni->ksnp_id) == 0) {
2458 read_lock(&ksocknal_data.ksnd_global_lock);
2462 ksocknal_free_tx(tx);
2463 read_lock(&ksocknal_data.ksnd_global_lock);
2470 ksocknal_check_peer_timeouts(int idx)
2472 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2473 struct ksock_peer_ni *peer_ni;
2474 struct ksock_conn *conn;
2475 struct ksock_tx *tx;
2478 /* NB. We expect to have a look at all the peers and not find any
2479 * connections to time out, so we just use a shared lock while we
2481 read_lock(&ksocknal_data.ksnd_global_lock);
2483 list_for_each_entry(peer_ni, peers, ksnp_list) {
2484 struct ksock_tx *tx_stale;
2485 time64_t deadline = 0;
2489 if (ksocknal_send_keepalive_locked(peer_ni) != 0) {
2490 read_unlock(&ksocknal_data.ksnd_global_lock);
2494 conn = ksocknal_find_timed_out_conn (peer_ni);
2497 read_unlock(&ksocknal_data.ksnd_global_lock);
2499 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2501 /* NB we won't find this one again, but we can't
2502 * just proceed with the next peer_ni, since we dropped
2503 * 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 */
2510 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
2511 struct ksock_tx *tx;
2513 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2514 struct ksock_tx, tx_list);
2515 if (ktime_get_seconds() >= tx->tx_deadline) {
2516 ksocknal_peer_addref(peer_ni);
2517 read_unlock(&ksocknal_data.ksnd_global_lock);
2519 ksocknal_flush_stale_txs(peer_ni);
2521 ksocknal_peer_decref(peer_ni);
2526 if (list_empty(&peer_ni->ksnp_zc_req_list))
2530 spin_lock(&peer_ni->ksnp_lock);
2531 list_for_each_entry(tx, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
2532 if (ktime_get_seconds() < tx->tx_deadline)
2534 /* ignore the TX if connection is being closed */
2535 if (tx->tx_conn->ksnc_closing)
2538 if (tx_stale == NULL)
2542 if (tx_stale == NULL) {
2543 spin_unlock(&peer_ni->ksnp_lock);
2547 deadline = tx_stale->tx_deadline;
2548 resid = tx_stale->tx_resid;
2549 conn = tx_stale->tx_conn;
2550 ksocknal_conn_addref(conn);
2552 spin_unlock(&peer_ni->ksnp_lock);
2553 read_unlock(&ksocknal_data.ksnd_global_lock);
2555 CERROR("Total %d stale ZC_REQs for peer_ni %s detected; the "
2556 "oldest(%p) timed out %lld secs ago, "
2557 "resid: %d, wmem: %d\n",
2558 n, libcfs_nid2str(peer_ni->ksnp_id.nid), tx_stale,
2559 ktime_get_seconds() - deadline,
2560 resid, conn->ksnc_sock->sk->sk_wmem_queued);
2562 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2563 ksocknal_conn_decref(conn);
2567 read_unlock(&ksocknal_data.ksnd_global_lock);
2570 int ksocknal_reaper(void *arg)
2572 wait_queue_entry_t wait;
2573 struct ksock_conn *conn;
2574 struct ksock_sched *sched;
2575 struct list_head enomem_conns;
2580 time64_t deadline = ktime_get_seconds();
2582 cfs_block_allsigs ();
2584 INIT_LIST_HEAD(&enomem_conns);
2585 init_waitqueue_entry(&wait, current);
2587 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2589 while (!ksocknal_data.ksnd_shuttingdown) {
2590 if (!list_empty(&ksocknal_data.ksnd_deathrow_conns)) {
2591 conn = list_entry(ksocknal_data.ksnd_deathrow_conns.next,
2592 struct ksock_conn, ksnc_list);
2593 list_del(&conn->ksnc_list);
2595 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2597 ksocknal_terminate_conn(conn);
2598 ksocknal_conn_decref(conn);
2600 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2604 if (!list_empty(&ksocknal_data.ksnd_zombie_conns)) {
2605 conn = list_entry(ksocknal_data.ksnd_zombie_conns.next,
2606 struct ksock_conn, ksnc_list);
2607 list_del(&conn->ksnc_list);
2609 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2611 ksocknal_destroy_conn(conn);
2613 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2617 if (!list_empty(&ksocknal_data.ksnd_enomem_conns)) {
2618 list_add(&enomem_conns,
2619 &ksocknal_data.ksnd_enomem_conns);
2620 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2623 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2625 /* reschedule all the connections that stalled with ENOMEM... */
2627 while (!list_empty(&enomem_conns)) {
2628 conn = list_entry(enomem_conns.next,
2629 struct ksock_conn, ksnc_tx_list);
2630 list_del(&conn->ksnc_tx_list);
2632 sched = conn->ksnc_scheduler;
2634 spin_lock_bh(&sched->kss_lock);
2636 LASSERT(conn->ksnc_tx_scheduled);
2637 conn->ksnc_tx_ready = 1;
2638 list_add_tail(&conn->ksnc_tx_list,
2639 &sched->kss_tx_conns);
2640 wake_up(&sched->kss_waitq);
2642 spin_unlock_bh(&sched->kss_lock);
2646 /* careful with the jiffy wrap... */
2647 while ((timeout = deadline - ktime_get_seconds()) <= 0) {
2650 int chunk = ksocknal_data.ksnd_peer_hash_size;
2651 unsigned int lnd_timeout;
2653 /* Time to check for timeouts on a few more peers: I do
2654 * checks every 'p' seconds on a proportion of the peer_ni
2655 * table and I need to check every connection 'n' times
2656 * within a timeout interval, to ensure I detect a
2657 * timeout on any connection within (n+1)/n times the
2658 * timeout interval. */
2660 lnd_timeout = lnet_get_lnd_timeout();
2661 if (lnd_timeout > n * p)
2662 chunk = (chunk * n * p) / lnd_timeout;
2666 for (i = 0; i < chunk; i++) {
2667 ksocknal_check_peer_timeouts (peer_index);
2668 peer_index = (peer_index + 1) %
2669 ksocknal_data.ksnd_peer_hash_size;
2675 if (nenomem_conns != 0) {
2676 /* Reduce my timeout if I rescheduled ENOMEM conns.
2677 * This also prevents me getting woken immediately
2678 * if any go back on my enomem list. */
2679 timeout = SOCKNAL_ENOMEM_RETRY;
2681 ksocknal_data.ksnd_reaper_waketime = ktime_get_seconds() +
2684 set_current_state(TASK_INTERRUPTIBLE);
2685 add_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2687 if (!ksocknal_data.ksnd_shuttingdown &&
2688 list_empty(&ksocknal_data.ksnd_deathrow_conns) &&
2689 list_empty(&ksocknal_data.ksnd_zombie_conns))
2690 schedule_timeout(cfs_time_seconds(timeout));
2692 set_current_state(TASK_RUNNING);
2693 remove_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2695 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2698 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2700 ksocknal_thread_fini();