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 */
412 lnetmsg->msg_health_status = hstatus;
413 lnet_finalize(lnetmsg, rc);
420 ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist, int error)
424 while (!list_empty(txlist)) {
425 tx = list_entry(txlist->next, struct ksock_tx, tx_list);
427 if (error && tx->tx_lnetmsg != NULL) {
428 CNETERR("Deleting packet type %d len %d %s->%s\n",
429 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.type),
430 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.payload_length),
431 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
432 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
434 CNETERR("Deleting noop packet\n");
437 list_del(&tx->tx_list);
439 if (tx->tx_hstatus == LNET_MSG_STATUS_OK) {
440 if (error == -ETIMEDOUT)
442 LNET_MSG_STATUS_LOCAL_TIMEOUT;
443 else if (error == -ENETDOWN ||
444 error == -EHOSTUNREACH ||
445 error == -ENETUNREACH ||
446 error == -ECONNREFUSED ||
447 error == -ECONNRESET)
448 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
450 * for all other errors we don't want to
454 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
457 LASSERT(atomic_read(&tx->tx_refcount) == 1);
458 ksocknal_tx_done(ni, tx, error);
463 ksocknal_check_zc_req(struct ksock_tx *tx)
465 struct ksock_conn *conn = tx->tx_conn;
466 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
468 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
469 * to ksnp_zc_req_list if some fragment of this message should be sent
470 * zero-copy. Our peer_ni will send an ACK containing this cookie when
471 * she has received this message to tell us we can signal completion.
472 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
473 * ksnp_zc_req_list. */
474 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
475 LASSERT (tx->tx_zc_capable);
477 tx->tx_zc_checked = 1;
479 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
480 !conn->ksnc_zc_capable)
483 /* assign cookie and queue tx to pending list, it will be released when
484 * a matching ack is received. See ksocknal_handle_zcack() */
486 ksocknal_tx_addref(tx);
488 spin_lock(&peer_ni->ksnp_lock);
490 /* ZC_REQ is going to be pinned to the peer_ni */
491 tx->tx_deadline = ktime_get_seconds() +
492 lnet_get_lnd_timeout();
494 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
496 tx->tx_msg.ksm_zc_cookies[0] = peer_ni->ksnp_zc_next_cookie++;
498 if (peer_ni->ksnp_zc_next_cookie == 0)
499 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
501 list_add_tail(&tx->tx_zc_list, &peer_ni->ksnp_zc_req_list);
503 spin_unlock(&peer_ni->ksnp_lock);
507 ksocknal_uncheck_zc_req(struct ksock_tx *tx)
509 struct ksock_peer_ni *peer_ni = tx->tx_conn->ksnc_peer;
511 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
512 LASSERT(tx->tx_zc_capable);
514 tx->tx_zc_checked = 0;
516 spin_lock(&peer_ni->ksnp_lock);
518 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
519 /* Not waiting for an ACK */
520 spin_unlock(&peer_ni->ksnp_lock);
524 tx->tx_msg.ksm_zc_cookies[0] = 0;
525 list_del(&tx->tx_zc_list);
527 spin_unlock(&peer_ni->ksnp_lock);
529 ksocknal_tx_decref(tx);
533 ksocknal_process_transmit(struct ksock_conn *conn, struct ksock_tx *tx)
536 bool error_sim = false;
538 if (lnet_send_error_simulation(tx->tx_lnetmsg, &tx->tx_hstatus)) {
544 if (tx->tx_zc_capable && !tx->tx_zc_checked)
545 ksocknal_check_zc_req(tx);
547 rc = ksocknal_transmit (conn, tx);
549 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
551 if (tx->tx_resid == 0) {
552 /* Sent everything OK */
564 counter++; /* exponential backoff warnings */
565 if ((counter & (-counter)) == counter)
566 CWARN("%u ENOMEM tx %p (%u allocated)\n",
567 counter, conn, atomic_read(&libcfs_kmemory));
569 /* Queue on ksnd_enomem_conns for retry after a timeout */
570 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
572 /* enomem list takes over scheduler's ref... */
573 LASSERT (conn->ksnc_tx_scheduled);
574 list_add_tail(&conn->ksnc_tx_list,
575 &ksocknal_data.ksnd_enomem_conns);
576 if (ktime_get_seconds() + SOCKNAL_ENOMEM_RETRY <
577 ksocknal_data.ksnd_reaper_waketime)
578 wake_up(&ksocknal_data.ksnd_reaper_waitq);
580 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
583 * set the health status of the message which determines
584 * whether we should retry the transmit
586 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
597 * set the health status of the message which determines
598 * whether we should retry the transmit
600 if (rc == -ETIMEDOUT)
601 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_TIMEOUT;
603 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
606 if (!conn->ksnc_closing) {
609 LCONSOLE_WARN("Host %pI4h reset our connection "
610 "while we were sending data; it may have "
615 LCONSOLE_WARN("There was an unexpected network error "
616 "while writing to %pI4h: %d.\n",
617 &conn->ksnc_ipaddr, rc);
620 CDEBUG(D_NET, "[%p] Error %d on write to %s ip %pI4h:%d\n",
621 conn, rc, libcfs_id2str(conn->ksnc_peer->ksnp_id),
622 &conn->ksnc_ipaddr, conn->ksnc_port);
625 if (tx->tx_zc_checked)
626 ksocknal_uncheck_zc_req(tx);
628 /* it's not an error if conn is being closed */
629 ksocknal_close_conn_and_siblings(conn,
630 (conn->ksnc_closing) ? 0 : rc);
636 ksocknal_launch_connection_locked(struct ksock_route *route)
639 /* called holding write lock on ksnd_global_lock */
641 LASSERT (!route->ksnr_scheduled);
642 LASSERT (!route->ksnr_connecting);
643 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
645 route->ksnr_scheduled = 1; /* scheduling conn for connd */
646 ksocknal_route_addref(route); /* extra ref for connd */
648 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
650 list_add_tail(&route->ksnr_connd_list,
651 &ksocknal_data.ksnd_connd_routes);
652 wake_up(&ksocknal_data.ksnd_connd_waitq);
654 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
658 ksocknal_launch_all_connections_locked(struct ksock_peer_ni *peer_ni)
660 struct ksock_route *route;
662 /* called holding write lock on ksnd_global_lock */
664 /* launch any/all connections that need it */
665 route = ksocknal_find_connectable_route_locked(peer_ni);
669 ksocknal_launch_connection_locked(route);
674 ksocknal_find_conn_locked(struct ksock_peer_ni *peer_ni, struct ksock_tx *tx, int nonblk)
676 struct list_head *tmp;
677 struct ksock_conn *conn;
678 struct ksock_conn *typed = NULL;
679 struct ksock_conn *fallback = NULL;
683 list_for_each(tmp, &peer_ni->ksnp_conns) {
684 struct ksock_conn *c = list_entry(tmp, struct ksock_conn,
686 int nob = atomic_read(&c->ksnc_tx_nob) +
687 c->ksnc_sock->sk->sk_wmem_queued;
690 LASSERT (!c->ksnc_closing);
691 LASSERT (c->ksnc_proto != NULL &&
692 c->ksnc_proto->pro_match_tx != NULL);
694 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
699 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
702 case SOCKNAL_MATCH_YES: /* typed connection */
703 if (typed == NULL || tnob > nob ||
704 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
705 typed->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
711 case SOCKNAL_MATCH_MAY: /* fallback connection */
712 if (fallback == NULL || fnob > nob ||
713 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
714 fallback->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
722 /* prefer the typed selection */
723 conn = (typed != NULL) ? typed : fallback;
726 conn->ksnc_tx_last_post = ktime_get_seconds();
732 ksocknal_tx_prep(struct ksock_conn *conn, struct ksock_tx *tx)
734 conn->ksnc_proto->pro_pack(tx);
736 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
737 ksocknal_conn_addref(conn); /* +1 ref for tx */
742 ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn)
744 struct ksock_sched *sched = conn->ksnc_scheduler;
745 struct ksock_msg *msg = &tx->tx_msg;
746 struct ksock_tx *ztx = NULL;
749 /* called holding global lock (read or irq-write) and caller may
750 * not have dropped this lock between finding conn and calling me,
751 * so we don't need the {get,put}connsock dance to deref
753 LASSERT(!conn->ksnc_closing);
755 CDEBUG(D_NET, "Sending to %s ip %pI4h:%d\n",
756 libcfs_id2str(conn->ksnc_peer->ksnp_id),
757 &conn->ksnc_ipaddr, conn->ksnc_port);
759 ksocknal_tx_prep(conn, tx);
761 /* Ensure the frags we've been given EXACTLY match the number of
762 * bytes we want to send. Many TCP/IP stacks disregard any total
763 * size parameters passed to them and just look at the frags.
765 * We always expect at least 1 mapped fragment containing the
766 * complete ksocknal message header. */
767 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
768 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
769 (unsigned int)tx->tx_nob);
770 LASSERT (tx->tx_niov >= 1);
771 LASSERT (tx->tx_resid == tx->tx_nob);
773 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
774 tx, (tx->tx_lnetmsg != NULL) ? tx->tx_lnetmsg->msg_hdr.type:
776 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
778 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
779 spin_lock_bh(&sched->kss_lock);
781 if (list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
782 /* First packet starts the timeout */
783 conn->ksnc_tx_deadline = ktime_get_seconds() +
784 lnet_get_lnd_timeout();
785 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
786 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
787 conn->ksnc_tx_bufnob = 0;
788 smp_mb(); /* order with adding to tx_queue */
791 if (msg->ksm_type == KSOCK_MSG_NOOP) {
792 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
793 * on a normal packet so I don't need to send it */
794 LASSERT (msg->ksm_zc_cookies[1] != 0);
795 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
797 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
798 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
801 /* It's a normal packet - can it piggback a noop zc-ack that
802 * has been queued already? */
803 LASSERT (msg->ksm_zc_cookies[1] == 0);
804 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
806 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
807 /* ztx will be released later */
811 atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
812 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
815 if (conn->ksnc_tx_ready && /* able to send */
816 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
817 /* +1 ref for scheduler */
818 ksocknal_conn_addref(conn);
819 list_add_tail(&conn->ksnc_tx_list,
820 &sched->kss_tx_conns);
821 conn->ksnc_tx_scheduled = 1;
822 wake_up(&sched->kss_waitq);
825 spin_unlock_bh(&sched->kss_lock);
830 ksocknal_find_connectable_route_locked(struct ksock_peer_ni *peer_ni)
832 time64_t now = ktime_get_seconds();
833 struct list_head *tmp;
834 struct ksock_route *route;
836 list_for_each(tmp, &peer_ni->ksnp_routes) {
837 route = list_entry(tmp, struct ksock_route, ksnr_list);
839 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
841 if (route->ksnr_scheduled) /* connections being established */
844 /* all route types connected ? */
845 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
848 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
849 now >= route->ksnr_timeout)) {
851 "Too soon to retry route %pI4h "
852 "(cnted %d, interval %lld, %lld secs later)\n",
854 route->ksnr_connected,
855 route->ksnr_retry_interval,
856 route->ksnr_timeout - now);
867 ksocknal_find_connecting_route_locked(struct ksock_peer_ni *peer_ni)
869 struct list_head *tmp;
870 struct ksock_route *route;
872 list_for_each(tmp, &peer_ni->ksnp_routes) {
873 route = list_entry(tmp, struct ksock_route, ksnr_list);
875 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
877 if (route->ksnr_scheduled)
885 ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
886 struct lnet_process_id id)
888 struct ksock_peer_ni *peer_ni;
889 struct ksock_conn *conn;
894 LASSERT (tx->tx_conn == NULL);
896 g_lock = &ksocknal_data.ksnd_global_lock;
898 for (retry = 0;; retry = 1) {
900 peer_ni = ksocknal_find_peer_locked(ni, id);
901 if (peer_ni != NULL) {
902 if (ksocknal_find_connectable_route_locked(peer_ni) == NULL) {
903 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
905 /* I've got no routes that need to be
906 * connecting and I do have an actual
908 ksocknal_queue_tx_locked (tx, conn);
915 /* I'll need a write lock... */
918 write_lock_bh(g_lock);
920 peer_ni = ksocknal_find_peer_locked(ni, id);
924 write_unlock_bh(g_lock);
926 if ((id.pid & LNET_PID_USERFLAG) != 0) {
927 CERROR("Refusing to create a connection to "
928 "userspace process %s\n", libcfs_id2str(id));
929 return -EHOSTUNREACH;
933 CERROR("Can't find peer_ni %s\n", libcfs_id2str(id));
934 return -EHOSTUNREACH;
937 rc = ksocknal_add_peer(ni, id,
938 LNET_NIDADDR(id.nid),
939 lnet_acceptor_port());
941 CERROR("Can't add peer_ni %s: %d\n",
942 libcfs_id2str(id), rc);
947 ksocknal_launch_all_connections_locked(peer_ni);
949 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
951 /* Connection exists; queue message on it */
952 ksocknal_queue_tx_locked (tx, conn);
953 write_unlock_bh(g_lock);
957 if (peer_ni->ksnp_accepting > 0 ||
958 ksocknal_find_connecting_route_locked (peer_ni) != NULL) {
959 /* the message is going to be pinned to the peer_ni */
960 tx->tx_deadline = ktime_get_seconds() +
961 lnet_get_lnd_timeout();
963 /* Queue the message until a connection is established */
964 list_add_tail(&tx->tx_list, &peer_ni->ksnp_tx_queue);
965 write_unlock_bh(g_lock);
969 write_unlock_bh(g_lock);
971 /* NB Routes may be ignored if connections to them failed recently */
972 CNETERR("No usable routes to %s\n", libcfs_id2str(id));
973 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
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 lntmsg->msg_health_status = tx->tx_hstatus;
1059 ksocknal_free_tx(tx);
1064 ksocknal_thread_start(int (*fn)(void *arg), void *arg, char *name)
1066 struct task_struct *task = kthread_run(fn, arg, name);
1069 return PTR_ERR(task);
1071 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1072 ksocknal_data.ksnd_nthreads++;
1073 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1078 ksocknal_thread_fini (void)
1080 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1081 ksocknal_data.ksnd_nthreads--;
1082 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1086 ksocknal_new_packet(struct ksock_conn *conn, int nob_to_skip)
1088 static char ksocknal_slop_buffer[4096];
1093 LASSERT(conn->ksnc_proto != NULL);
1095 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1096 /* Remind the socket to ack eagerly... */
1097 ksocknal_lib_eager_ack(conn);
1100 if (nob_to_skip == 0) { /* right at next packet boundary now */
1101 conn->ksnc_rx_started = 0;
1102 smp_mb(); /* racing with timeout thread */
1104 switch (conn->ksnc_proto->pro_version) {
1105 case KSOCK_PROTO_V2:
1106 case KSOCK_PROTO_V3:
1107 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1108 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1109 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1111 conn->ksnc_rx_nob_wanted = offsetof(struct ksock_msg, ksm_u);
1112 conn->ksnc_rx_nob_left = offsetof(struct ksock_msg, ksm_u);
1113 conn->ksnc_rx_iov[0].iov_len = offsetof(struct ksock_msg, ksm_u);
1116 case KSOCK_PROTO_V1:
1117 /* Receiving bare struct lnet_hdr */
1118 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1119 conn->ksnc_rx_nob_wanted = sizeof(struct lnet_hdr);
1120 conn->ksnc_rx_nob_left = sizeof(struct lnet_hdr);
1122 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1123 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1124 conn->ksnc_rx_iov[0].iov_len = sizeof(struct lnet_hdr);
1130 conn->ksnc_rx_niov = 1;
1132 conn->ksnc_rx_kiov = NULL;
1133 conn->ksnc_rx_nkiov = 0;
1134 conn->ksnc_rx_csum = ~0;
1138 /* Set up to skip as much as possible now. If there's more left
1139 * (ran out of iov entries) we'll get called again */
1141 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1142 conn->ksnc_rx_nob_left = nob_to_skip;
1143 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1148 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1150 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1151 conn->ksnc_rx_iov[niov].iov_len = nob;
1156 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1157 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof(struct kvec));
1159 conn->ksnc_rx_niov = niov;
1160 conn->ksnc_rx_kiov = NULL;
1161 conn->ksnc_rx_nkiov = 0;
1162 conn->ksnc_rx_nob_wanted = skipped;
1167 ksocknal_process_receive(struct ksock_conn *conn)
1169 struct lnet_hdr *lhdr;
1170 struct lnet_process_id *id;
1173 LASSERT (atomic_read(&conn->ksnc_conn_refcount) > 0);
1175 /* NB: sched lock NOT held */
1176 /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */
1177 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1178 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1179 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1180 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1182 if (conn->ksnc_rx_nob_wanted != 0) {
1183 rc = ksocknal_receive(conn);
1186 struct lnet_process_id ksnp_id;
1188 ksnp_id = conn->ksnc_peer->ksnp_id;
1190 LASSERT(rc != -EAGAIN);
1192 CDEBUG(D_NET, "[%p] EOF from %s "
1193 "ip %pI4h:%d\n", conn,
1194 libcfs_id2str(ksnp_id),
1197 else if (!conn->ksnc_closing)
1198 CERROR("[%p] Error %d on read from %s "
1199 "ip %pI4h:%d\n", conn, rc,
1200 libcfs_id2str(ksnp_id),
1204 /* it's not an error if conn is being closed */
1205 ksocknal_close_conn_and_siblings (conn,
1206 (conn->ksnc_closing) ? 0 : rc);
1207 return (rc == 0 ? -ESHUTDOWN : rc);
1210 if (conn->ksnc_rx_nob_wanted != 0) {
1215 switch (conn->ksnc_rx_state) {
1216 case SOCKNAL_RX_KSM_HEADER:
1217 if (conn->ksnc_flip) {
1218 __swab32s(&conn->ksnc_msg.ksm_type);
1219 __swab32s(&conn->ksnc_msg.ksm_csum);
1220 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1221 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1224 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1225 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1226 CERROR("%s: Unknown message type: %x\n",
1227 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1228 conn->ksnc_msg.ksm_type);
1229 ksocknal_new_packet(conn, 0);
1230 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1234 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1235 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1236 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1237 /* NOOP Checksum error */
1238 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1239 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1240 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1241 ksocknal_new_packet(conn, 0);
1242 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1246 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1249 LASSERT (conn->ksnc_proto != &ksocknal_protocol_v1x);
1251 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1252 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1254 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1255 conn->ksnc_msg.ksm_zc_cookies[1]);
1258 CERROR("%s: Unknown ZC-ACK cookie: %llu, %llu\n",
1259 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1260 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1261 ksocknal_new_packet(conn, 0);
1262 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1267 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1268 ksocknal_new_packet (conn, 0);
1269 return 0; /* NOOP is done and just return */
1272 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1273 conn->ksnc_rx_nob_wanted = sizeof(struct ksock_lnet_msg);
1274 conn->ksnc_rx_nob_left = sizeof(struct ksock_lnet_msg);
1276 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1277 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1278 conn->ksnc_rx_iov[0].iov_len = sizeof(struct ksock_lnet_msg);
1280 conn->ksnc_rx_niov = 1;
1281 conn->ksnc_rx_kiov = NULL;
1282 conn->ksnc_rx_nkiov = 0;
1284 goto again; /* read lnet header now */
1286 case SOCKNAL_RX_LNET_HEADER:
1287 /* unpack message header */
1288 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1290 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1291 /* Userspace peer_ni */
1292 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1293 id = &conn->ksnc_peer->ksnp_id;
1295 /* Substitute process ID assigned at connection time */
1296 lhdr->src_pid = cpu_to_le32(id->pid);
1297 lhdr->src_nid = cpu_to_le64(id->nid);
1300 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1301 ksocknal_conn_addref(conn); /* ++ref while parsing */
1303 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1304 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1305 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1307 /* I just received garbage: give up on this conn */
1308 ksocknal_new_packet(conn, 0);
1309 ksocknal_close_conn_and_siblings (conn, rc);
1310 ksocknal_conn_decref(conn);
1314 /* I'm racing with ksocknal_recv() */
1315 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1316 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1318 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1321 /* ksocknal_recv() got called */
1324 case SOCKNAL_RX_LNET_PAYLOAD:
1325 /* payload all received */
1328 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1329 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1330 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1331 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1332 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1333 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1337 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1338 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1340 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1341 id = &conn->ksnc_peer->ksnp_id;
1343 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1344 conn->ksnc_msg.ksm_zc_cookies[0],
1345 *ksocknal_tunables.ksnd_nonblk_zcack ||
1346 le64_to_cpu(lhdr->src_nid) != id->nid);
1349 if (rc && conn->ksnc_lnet_msg)
1350 conn->ksnc_lnet_msg->msg_health_status =
1351 LNET_MSG_STATUS_REMOTE_ERROR;
1352 lnet_finalize(conn->ksnc_lnet_msg, rc);
1355 ksocknal_new_packet(conn, 0);
1356 ksocknal_close_conn_and_siblings (conn, rc);
1361 case SOCKNAL_RX_SLOP:
1362 /* starting new packet? */
1363 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1364 return 0; /* come back later */
1365 goto again; /* try to finish reading slop now */
1373 return (-EINVAL); /* keep gcc happy */
1377 ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
1378 int delayed, unsigned int niov, struct kvec *iov,
1379 lnet_kiov_t *kiov, unsigned int offset, unsigned int mlen,
1382 struct ksock_conn *conn = private;
1383 struct ksock_sched *sched = conn->ksnc_scheduler;
1385 LASSERT (mlen <= rlen);
1386 LASSERT (niov <= LNET_MAX_IOV);
1388 conn->ksnc_lnet_msg = msg;
1389 conn->ksnc_rx_nob_wanted = mlen;
1390 conn->ksnc_rx_nob_left = rlen;
1392 if (mlen == 0 || iov != NULL) {
1393 conn->ksnc_rx_nkiov = 0;
1394 conn->ksnc_rx_kiov = NULL;
1395 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1396 conn->ksnc_rx_niov =
1397 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1398 niov, iov, offset, mlen);
1400 conn->ksnc_rx_niov = 0;
1401 conn->ksnc_rx_iov = NULL;
1402 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1403 conn->ksnc_rx_nkiov =
1404 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1405 niov, kiov, offset, mlen);
1409 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1410 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1412 LASSERT (conn->ksnc_rx_scheduled);
1414 spin_lock_bh(&sched->kss_lock);
1416 switch (conn->ksnc_rx_state) {
1417 case SOCKNAL_RX_PARSE_WAIT:
1418 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1419 wake_up(&sched->kss_waitq);
1420 LASSERT(conn->ksnc_rx_ready);
1423 case SOCKNAL_RX_PARSE:
1424 /* scheduler hasn't noticed I'm parsing yet */
1428 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1430 spin_unlock_bh(&sched->kss_lock);
1431 ksocknal_conn_decref(conn);
1436 ksocknal_sched_cansleep(struct ksock_sched *sched)
1440 spin_lock_bh(&sched->kss_lock);
1442 rc = (!ksocknal_data.ksnd_shuttingdown &&
1443 list_empty(&sched->kss_rx_conns) &&
1444 list_empty(&sched->kss_tx_conns));
1446 spin_unlock_bh(&sched->kss_lock);
1450 int ksocknal_scheduler(void *arg)
1452 struct ksock_sched_info *info;
1453 struct ksock_sched *sched;
1454 struct ksock_conn *conn;
1455 struct ksock_tx *tx;
1458 long id = (long)arg;
1460 info = ksocknal_data.ksnd_sched_info[KSOCK_THREAD_CPT(id)];
1461 sched = &info->ksi_scheds[KSOCK_THREAD_SID(id)];
1463 cfs_block_allsigs();
1465 rc = cfs_cpt_bind(lnet_cpt_table(), info->ksi_cpt);
1467 CWARN("Can't set CPU partition affinity to %d: %d\n",
1471 spin_lock_bh(&sched->kss_lock);
1473 while (!ksocknal_data.ksnd_shuttingdown) {
1474 int did_something = 0;
1476 /* Ensure I progress everything semi-fairly */
1478 if (!list_empty(&sched->kss_rx_conns)) {
1479 conn = list_entry(sched->kss_rx_conns.next,
1480 struct ksock_conn, ksnc_rx_list);
1481 list_del(&conn->ksnc_rx_list);
1483 LASSERT(conn->ksnc_rx_scheduled);
1484 LASSERT(conn->ksnc_rx_ready);
1486 /* clear rx_ready in case receive isn't complete.
1487 * Do it BEFORE we call process_recv, since
1488 * data_ready can set it any time after we release
1490 conn->ksnc_rx_ready = 0;
1491 spin_unlock_bh(&sched->kss_lock);
1493 rc = ksocknal_process_receive(conn);
1495 spin_lock_bh(&sched->kss_lock);
1497 /* I'm the only one that can clear this flag */
1498 LASSERT(conn->ksnc_rx_scheduled);
1500 /* Did process_receive get everything it wanted? */
1502 conn->ksnc_rx_ready = 1;
1504 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1505 /* Conn blocked waiting for ksocknal_recv()
1506 * I change its state (under lock) to signal
1507 * it can be rescheduled */
1508 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1509 } else if (conn->ksnc_rx_ready) {
1510 /* reschedule for rx */
1511 list_add_tail(&conn->ksnc_rx_list,
1512 &sched->kss_rx_conns);
1514 conn->ksnc_rx_scheduled = 0;
1516 ksocknal_conn_decref(conn);
1522 if (!list_empty(&sched->kss_tx_conns)) {
1523 struct list_head zlist = LIST_HEAD_INIT(zlist);
1525 if (!list_empty(&sched->kss_zombie_noop_txs)) {
1527 &sched->kss_zombie_noop_txs);
1528 list_del_init(&sched->kss_zombie_noop_txs);
1531 conn = list_entry(sched->kss_tx_conns.next,
1532 struct ksock_conn, ksnc_tx_list);
1533 list_del(&conn->ksnc_tx_list);
1535 LASSERT(conn->ksnc_tx_scheduled);
1536 LASSERT(conn->ksnc_tx_ready);
1537 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1539 tx = list_entry(conn->ksnc_tx_queue.next,
1540 struct ksock_tx, tx_list);
1542 if (conn->ksnc_tx_carrier == tx)
1543 ksocknal_next_tx_carrier(conn);
1545 /* dequeue now so empty list => more to send */
1546 list_del(&tx->tx_list);
1548 /* Clear tx_ready in case send isn't complete. Do
1549 * it BEFORE we call process_transmit, since
1550 * write_space can set it any time after we release
1552 conn->ksnc_tx_ready = 0;
1553 spin_unlock_bh(&sched->kss_lock);
1555 if (!list_empty(&zlist)) {
1556 /* free zombie noop txs, it's fast because
1557 * noop txs are just put in freelist */
1558 ksocknal_txlist_done(NULL, &zlist, 0);
1561 rc = ksocknal_process_transmit(conn, tx);
1563 if (rc == -ENOMEM || rc == -EAGAIN) {
1564 /* Incomplete send: replace tx on HEAD of tx_queue */
1565 spin_lock_bh(&sched->kss_lock);
1566 list_add(&tx->tx_list,
1567 &conn->ksnc_tx_queue);
1569 /* Complete send; tx -ref */
1570 ksocknal_tx_decref(tx);
1572 spin_lock_bh(&sched->kss_lock);
1573 /* assume space for more */
1574 conn->ksnc_tx_ready = 1;
1577 if (rc == -ENOMEM) {
1578 /* Do nothing; after a short timeout, this
1579 * conn will be reposted on kss_tx_conns. */
1580 } else if (conn->ksnc_tx_ready &&
1581 !list_empty(&conn->ksnc_tx_queue)) {
1582 /* reschedule for tx */
1583 list_add_tail(&conn->ksnc_tx_list,
1584 &sched->kss_tx_conns);
1586 conn->ksnc_tx_scheduled = 0;
1588 ksocknal_conn_decref(conn);
1593 if (!did_something || /* nothing to do */
1594 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1595 spin_unlock_bh(&sched->kss_lock);
1599 if (!did_something) { /* wait for something to do */
1600 rc = wait_event_interruptible_exclusive(
1602 !ksocknal_sched_cansleep(sched));
1608 spin_lock_bh(&sched->kss_lock);
1612 spin_unlock_bh(&sched->kss_lock);
1613 ksocknal_thread_fini();
1618 * Add connection to kss_rx_conns of scheduler
1619 * and wakeup the scheduler.
1621 void ksocknal_read_callback(struct ksock_conn *conn)
1623 struct ksock_sched *sched;
1626 sched = conn->ksnc_scheduler;
1628 spin_lock_bh(&sched->kss_lock);
1630 conn->ksnc_rx_ready = 1;
1632 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1633 list_add_tail(&conn->ksnc_rx_list,
1634 &sched->kss_rx_conns);
1635 conn->ksnc_rx_scheduled = 1;
1636 /* extra ref for scheduler */
1637 ksocknal_conn_addref(conn);
1639 wake_up (&sched->kss_waitq);
1641 spin_unlock_bh(&sched->kss_lock);
1647 * Add connection to kss_tx_conns of scheduler
1648 * and wakeup the scheduler.
1650 void ksocknal_write_callback(struct ksock_conn *conn)
1652 struct ksock_sched *sched;
1655 sched = conn->ksnc_scheduler;
1657 spin_lock_bh(&sched->kss_lock);
1659 conn->ksnc_tx_ready = 1;
1661 if (!conn->ksnc_tx_scheduled && /* not being progressed */
1662 !list_empty(&conn->ksnc_tx_queue)) { /* packets to send */
1663 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
1664 conn->ksnc_tx_scheduled = 1;
1665 /* extra ref for scheduler */
1666 ksocknal_conn_addref(conn);
1668 wake_up(&sched->kss_waitq);
1671 spin_unlock_bh(&sched->kss_lock);
1676 static struct ksock_proto *
1677 ksocknal_parse_proto_version (struct ksock_hello_msg *hello)
1681 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1682 version = hello->kshm_version;
1683 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1684 version = __swab32(hello->kshm_version);
1687 #if SOCKNAL_VERSION_DEBUG
1688 if (*ksocknal_tunables.ksnd_protocol == 1)
1691 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1692 version == KSOCK_PROTO_V3)
1695 if (version == KSOCK_PROTO_V2)
1696 return &ksocknal_protocol_v2x;
1698 if (version == KSOCK_PROTO_V3)
1699 return &ksocknal_protocol_v3x;
1704 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1705 struct lnet_magicversion *hmv;
1707 CLASSERT(sizeof(struct lnet_magicversion) ==
1708 offsetof(struct ksock_hello_msg, kshm_src_nid));
1710 hmv = (struct lnet_magicversion *)hello;
1712 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1713 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1714 return &ksocknal_protocol_v1x;
1721 ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1722 lnet_nid_t peer_nid, struct ksock_hello_msg *hello)
1724 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1725 struct ksock_net *net = (struct ksock_net *)ni->ni_data;
1727 LASSERT(hello->kshm_nips <= LNET_INTERFACES_NUM);
1729 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1730 LASSERT(conn->ksnc_proto != NULL);
1732 hello->kshm_src_nid = ni->ni_nid;
1733 hello->kshm_dst_nid = peer_nid;
1734 hello->kshm_src_pid = the_lnet.ln_pid;
1736 hello->kshm_src_incarnation = net->ksnn_incarnation;
1737 hello->kshm_ctype = conn->ksnc_type;
1739 return conn->ksnc_proto->pro_send_hello(conn, hello);
1743 ksocknal_invert_type(int type)
1747 case SOCKLND_CONN_ANY:
1748 case SOCKLND_CONN_CONTROL:
1750 case SOCKLND_CONN_BULK_IN:
1751 return SOCKLND_CONN_BULK_OUT;
1752 case SOCKLND_CONN_BULK_OUT:
1753 return SOCKLND_CONN_BULK_IN;
1755 return (SOCKLND_CONN_NONE);
1760 ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1761 struct ksock_hello_msg *hello,
1762 struct lnet_process_id *peerid,
1765 /* Return < 0 fatal error
1767 * EALREADY lost connection race
1768 * EPROTO protocol version mismatch
1770 struct socket *sock = conn->ksnc_sock;
1771 int active = (conn->ksnc_proto != NULL);
1775 struct ksock_proto *proto;
1776 struct lnet_process_id recv_id;
1778 /* socket type set on active connections - not set on passive */
1779 LASSERT(!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1781 timeout = active ? lnet_get_lnd_timeout() :
1782 lnet_acceptor_timeout();
1784 rc = lnet_sock_read(sock, &hello->kshm_magic,
1785 sizeof(hello->kshm_magic), timeout);
1787 CERROR("Error %d reading HELLO from %pI4h\n",
1788 rc, &conn->ksnc_ipaddr);
1793 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1794 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1795 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
1796 /* Unexpected magic! */
1797 CERROR ("Bad magic(1) %#08x (%#08x expected) from "
1798 "%pI4h\n", __cpu_to_le32 (hello->kshm_magic),
1799 LNET_PROTO_TCP_MAGIC, &conn->ksnc_ipaddr);
1803 rc = lnet_sock_read(sock, &hello->kshm_version,
1804 sizeof(hello->kshm_version), timeout);
1806 CERROR("Error %d reading HELLO from %pI4h\n",
1807 rc, &conn->ksnc_ipaddr);
1812 proto = ksocknal_parse_proto_version(hello);
1813 if (proto == NULL) {
1815 /* unknown protocol from peer_ni, tell peer_ni my protocol */
1816 conn->ksnc_proto = &ksocknal_protocol_v3x;
1817 #if SOCKNAL_VERSION_DEBUG
1818 if (*ksocknal_tunables.ksnd_protocol == 2)
1819 conn->ksnc_proto = &ksocknal_protocol_v2x;
1820 else if (*ksocknal_tunables.ksnd_protocol == 1)
1821 conn->ksnc_proto = &ksocknal_protocol_v1x;
1823 hello->kshm_nips = 0;
1824 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1827 CERROR("Unknown protocol version (%d.x expected) from %pI4h\n",
1828 conn->ksnc_proto->pro_version, &conn->ksnc_ipaddr);
1833 proto_match = (conn->ksnc_proto == proto);
1834 conn->ksnc_proto = proto;
1836 /* receive the rest of hello message anyway */
1837 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1839 CERROR("Error %d reading or checking hello from from %pI4h\n",
1840 rc, &conn->ksnc_ipaddr);
1845 *incarnation = hello->kshm_src_incarnation;
1847 if (hello->kshm_src_nid == LNET_NID_ANY) {
1848 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
1849 "from %pI4h\n", &conn->ksnc_ipaddr);
1854 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1855 /* Userspace NAL assigns peer_ni process ID from socket */
1856 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1857 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
1859 recv_id.nid = hello->kshm_src_nid;
1860 recv_id.pid = hello->kshm_src_pid;
1866 /* peer_ni determines type */
1867 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1868 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
1869 CERROR("Unexpected type %d from %s ip %pI4h\n",
1870 hello->kshm_ctype, libcfs_id2str(*peerid),
1871 &conn->ksnc_ipaddr);
1877 if (peerid->pid != recv_id.pid ||
1878 peerid->nid != recv_id.nid) {
1879 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1880 " %pI4h, but they claimed they were "
1881 "%s; please check your Lustre "
1883 libcfs_id2str(*peerid),
1885 libcfs_id2str(recv_id));
1889 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1890 /* Possible protocol mismatch or I lost the connection race */
1891 return proto_match ? EALREADY : EPROTO;
1894 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1895 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
1896 conn->ksnc_type, libcfs_id2str(*peerid),
1905 ksocknal_connect(struct ksock_route *route)
1907 struct list_head zombies = LIST_HEAD_INIT(zombies);
1908 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
1911 struct socket *sock;
1913 int retry_later = 0;
1916 deadline = ktime_get_seconds() + lnet_get_lnd_timeout();
1918 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1920 LASSERT (route->ksnr_scheduled);
1921 LASSERT (!route->ksnr_connecting);
1923 route->ksnr_connecting = 1;
1926 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1928 /* stop connecting if peer_ni/route got closed under me, or
1929 * route got connected while queued */
1930 if (peer_ni->ksnp_closing || route->ksnr_deleted ||
1936 /* reschedule if peer_ni is connecting to me */
1937 if (peer_ni->ksnp_accepting > 0) {
1939 "peer_ni %s(%d) already connecting to me, retry later.\n",
1940 libcfs_nid2str(peer_ni->ksnp_id.nid), peer_ni->ksnp_accepting);
1944 if (retry_later) /* needs reschedule */
1947 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
1948 type = SOCKLND_CONN_ANY;
1949 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
1950 type = SOCKLND_CONN_CONTROL;
1951 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
1952 type = SOCKLND_CONN_BULK_IN;
1954 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
1955 type = SOCKLND_CONN_BULK_OUT;
1958 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1960 if (ktime_get_seconds() >= deadline) {
1962 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
1968 rc = lnet_connect(&sock, peer_ni->ksnp_id.nid,
1969 route->ksnr_myipaddr,
1970 route->ksnr_ipaddr, route->ksnr_port);
1974 rc = ksocknal_create_conn(peer_ni->ksnp_ni, route, sock, type);
1976 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
1982 /* A +ve RC means I have to retry because I lost the connection
1983 * race or I have to renegotiate protocol version */
1984 retry_later = (rc != 0);
1986 CDEBUG(D_NET, "peer_ni %s: conn race, retry later.\n",
1987 libcfs_nid2str(peer_ni->ksnp_id.nid));
1989 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1992 route->ksnr_scheduled = 0;
1993 route->ksnr_connecting = 0;
1996 /* re-queue for attention; this frees me up to handle
1997 * the peer_ni's incoming connection request */
1999 if (rc == EALREADY ||
2000 (rc == 0 && peer_ni->ksnp_accepting > 0)) {
2001 /* We want to introduce a delay before next
2002 * attempt to connect if we lost conn race,
2003 * but the race is resolved quickly usually,
2004 * so min_reconnectms should be good heuristic */
2005 route->ksnr_retry_interval = *ksocknal_tunables.ksnd_min_reconnectms / 1000;
2006 route->ksnr_timeout = ktime_get_seconds() +
2007 route->ksnr_retry_interval;
2010 ksocknal_launch_connection_locked(route);
2013 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2017 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2019 route->ksnr_scheduled = 0;
2020 route->ksnr_connecting = 0;
2022 /* This is a retry rather than a new connection */
2023 route->ksnr_retry_interval *= 2;
2024 route->ksnr_retry_interval =
2025 max_t(time64_t, route->ksnr_retry_interval,
2026 *ksocknal_tunables.ksnd_min_reconnectms / 1000);
2027 route->ksnr_retry_interval =
2028 min_t(time64_t, route->ksnr_retry_interval,
2029 *ksocknal_tunables.ksnd_max_reconnectms / 1000);
2031 LASSERT(route->ksnr_retry_interval);
2032 route->ksnr_timeout = ktime_get_seconds() + route->ksnr_retry_interval;
2034 if (!list_empty(&peer_ni->ksnp_tx_queue) &&
2035 peer_ni->ksnp_accepting == 0 &&
2036 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
2037 struct ksock_conn *conn;
2039 /* ksnp_tx_queue is queued on a conn on successful
2040 * connection for V1.x and V2.x */
2041 if (!list_empty(&peer_ni->ksnp_conns)) {
2042 conn = list_entry(peer_ni->ksnp_conns.next,
2043 struct ksock_conn, ksnc_list);
2044 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
2047 /* take all the blocked packets while I've got the lock and
2048 * complete below... */
2049 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
2052 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2054 ksocknal_peer_failed(peer_ni);
2055 ksocknal_txlist_done(peer_ni->ksnp_ni, &zombies, rc);
2060 * check whether we need to create more connds.
2061 * It will try to create new thread if it's necessary, @timeout can
2062 * be updated if failed to create, so caller wouldn't keep try while
2063 * running out of resource.
2066 ksocknal_connd_check_start(time64_t sec, long *timeout)
2070 int total = ksocknal_data.ksnd_connd_starting +
2071 ksocknal_data.ksnd_connd_running;
2073 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2074 /* still in initializing */
2078 if (total >= *ksocknal_tunables.ksnd_nconnds_max ||
2079 total > ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV) {
2080 /* can't create more connd, or still have enough
2081 * threads to handle more connecting */
2085 if (list_empty(&ksocknal_data.ksnd_connd_routes)) {
2086 /* no pending connecting request */
2090 if (sec - ksocknal_data.ksnd_connd_failed_stamp <= 1) {
2091 /* may run out of resource, retry later */
2092 *timeout = cfs_time_seconds(1);
2096 if (ksocknal_data.ksnd_connd_starting > 0) {
2097 /* serialize starting to avoid flood */
2101 ksocknal_data.ksnd_connd_starting_stamp = sec;
2102 ksocknal_data.ksnd_connd_starting++;
2103 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2105 /* NB: total is the next id */
2106 snprintf(name, sizeof(name), "socknal_cd%02d", total);
2107 rc = ksocknal_thread_start(ksocknal_connd, NULL, name);
2109 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2114 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2115 ksocknal_data.ksnd_connd_starting--;
2116 ksocknal_data.ksnd_connd_failed_stamp = ktime_get_real_seconds();
2122 * check whether current thread can exit, it will return 1 if there are too
2123 * many threads and no creating in past 120 seconds.
2124 * Also, this function may update @timeout to make caller come back
2125 * again to recheck these conditions.
2128 ksocknal_connd_check_stop(time64_t sec, long *timeout)
2132 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2133 /* still in initializing */
2137 if (ksocknal_data.ksnd_connd_starting > 0) {
2138 /* in progress of starting new thread */
2142 if (ksocknal_data.ksnd_connd_running <=
2143 *ksocknal_tunables.ksnd_nconnds) { /* can't shrink */
2147 /* created thread in past 120 seconds? */
2148 val = (int)(ksocknal_data.ksnd_connd_starting_stamp +
2149 SOCKNAL_CONND_TIMEOUT - sec);
2151 *timeout = (val > 0) ? cfs_time_seconds(val) :
2152 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT);
2156 /* no creating in past 120 seconds */
2158 return ksocknal_data.ksnd_connd_running >
2159 ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV;
2162 /* Go through connd_routes queue looking for a route that we can process
2163 * right now, @timeout_p can be updated if we need to come back later */
2164 static struct ksock_route *
2165 ksocknal_connd_get_route_locked(signed long *timeout_p)
2167 time64_t now = ktime_get_seconds();
2168 struct ksock_route *route;
2170 /* connd_routes can contain both pending and ordinary routes */
2171 list_for_each_entry(route, &ksocknal_data.ksnd_connd_routes,
2174 if (route->ksnr_retry_interval == 0 ||
2175 now >= route->ksnr_timeout)
2178 if (*timeout_p == MAX_SCHEDULE_TIMEOUT ||
2179 *timeout_p > cfs_time_seconds(route->ksnr_timeout - now))
2180 *timeout_p = cfs_time_seconds(route->ksnr_timeout - now);
2187 ksocknal_connd(void *arg)
2189 spinlock_t *connd_lock = &ksocknal_data.ksnd_connd_lock;
2190 struct ksock_connreq *cr;
2191 wait_queue_entry_t wait;
2195 cfs_block_allsigs();
2197 init_waitqueue_entry(&wait, current);
2199 spin_lock_bh(connd_lock);
2201 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2202 ksocknal_data.ksnd_connd_starting--;
2203 ksocknal_data.ksnd_connd_running++;
2205 while (!ksocknal_data.ksnd_shuttingdown) {
2206 struct ksock_route *route = NULL;
2207 time64_t sec = ktime_get_real_seconds();
2208 long timeout = MAX_SCHEDULE_TIMEOUT;
2209 int dropped_lock = 0;
2211 if (ksocknal_connd_check_stop(sec, &timeout)) {
2212 /* wakeup another one to check stop */
2213 wake_up(&ksocknal_data.ksnd_connd_waitq);
2217 if (ksocknal_connd_check_start(sec, &timeout)) {
2218 /* created new thread */
2222 if (!list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2223 /* Connection accepted by the listener */
2224 cr = list_entry(ksocknal_data.ksnd_connd_connreqs.next,
2225 struct ksock_connreq, ksncr_list);
2227 list_del(&cr->ksncr_list);
2228 spin_unlock_bh(connd_lock);
2231 ksocknal_create_conn(cr->ksncr_ni, NULL,
2232 cr->ksncr_sock, SOCKLND_CONN_NONE);
2233 lnet_ni_decref(cr->ksncr_ni);
2234 LIBCFS_FREE(cr, sizeof(*cr));
2236 spin_lock_bh(connd_lock);
2239 /* Only handle an outgoing connection request if there
2240 * is a thread left to handle incoming connections and
2241 * create new connd */
2242 if (ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV <
2243 ksocknal_data.ksnd_connd_running) {
2244 route = ksocknal_connd_get_route_locked(&timeout);
2246 if (route != NULL) {
2247 list_del(&route->ksnr_connd_list);
2248 ksocknal_data.ksnd_connd_connecting++;
2249 spin_unlock_bh(connd_lock);
2252 if (ksocknal_connect(route)) {
2253 /* consecutive retry */
2254 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2255 CWARN("massive consecutive "
2256 "re-connecting to %pI4h\n",
2257 &route->ksnr_ipaddr);
2264 ksocknal_route_decref(route);
2266 spin_lock_bh(connd_lock);
2267 ksocknal_data.ksnd_connd_connecting--;
2271 if (++nloops < SOCKNAL_RESCHED)
2273 spin_unlock_bh(connd_lock);
2276 spin_lock_bh(connd_lock);
2280 /* Nothing to do for 'timeout' */
2281 set_current_state(TASK_INTERRUPTIBLE);
2282 add_wait_queue_exclusive(&ksocknal_data.ksnd_connd_waitq, &wait);
2283 spin_unlock_bh(connd_lock);
2286 schedule_timeout(timeout);
2288 set_current_state(TASK_RUNNING);
2289 remove_wait_queue(&ksocknal_data.ksnd_connd_waitq, &wait);
2290 spin_lock_bh(connd_lock);
2292 ksocknal_data.ksnd_connd_running--;
2293 spin_unlock_bh(connd_lock);
2295 ksocknal_thread_fini();
2299 static struct ksock_conn *
2300 ksocknal_find_timed_out_conn(struct ksock_peer_ni *peer_ni)
2302 /* We're called with a shared lock on ksnd_global_lock */
2303 struct ksock_conn *conn;
2304 struct list_head *ctmp;
2305 struct ksock_tx *tx;
2307 list_for_each(ctmp, &peer_ni->ksnp_conns) {
2310 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
2312 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2313 LASSERT (!conn->ksnc_closing);
2315 error = conn->ksnc_sock->sk->sk_err;
2317 ksocknal_conn_addref(conn);
2321 CNETERR("A connection with %s "
2322 "(%pI4h:%d) was reset; "
2323 "it may have rebooted.\n",
2324 libcfs_id2str(peer_ni->ksnp_id),
2329 CNETERR("A connection with %s "
2330 "(%pI4h:%d) timed out; the "
2331 "network or node may be down.\n",
2332 libcfs_id2str(peer_ni->ksnp_id),
2337 CNETERR("An unexpected network error %d "
2339 "(%pI4h:%d\n", error,
2340 libcfs_id2str(peer_ni->ksnp_id),
2349 if (conn->ksnc_rx_started &&
2350 ktime_get_seconds() >= conn->ksnc_rx_deadline) {
2351 /* Timed out incomplete incoming message */
2352 ksocknal_conn_addref(conn);
2353 CNETERR("Timeout receiving from %s (%pI4h:%d), "
2354 "state %d wanted %d left %d\n",
2355 libcfs_id2str(peer_ni->ksnp_id),
2358 conn->ksnc_rx_state,
2359 conn->ksnc_rx_nob_wanted,
2360 conn->ksnc_rx_nob_left);
2364 if ((!list_empty(&conn->ksnc_tx_queue) ||
2365 conn->ksnc_sock->sk->sk_wmem_queued != 0) &&
2366 ktime_get_seconds() >= conn->ksnc_tx_deadline) {
2367 /* Timed out messages queued for sending or
2368 * buffered in the socket's send buffer */
2369 ksocknal_conn_addref(conn);
2370 list_for_each_entry(tx, &conn->ksnc_tx_queue,
2373 LNET_MSG_STATUS_LOCAL_TIMEOUT;
2374 CNETERR("Timeout sending data to %s (%pI4h:%d) "
2375 "the network or that node may be down.\n",
2376 libcfs_id2str(peer_ni->ksnp_id),
2377 &conn->ksnc_ipaddr, conn->ksnc_port);
2386 ksocknal_flush_stale_txs(struct ksock_peer_ni *peer_ni)
2388 struct ksock_tx *tx;
2389 struct list_head stale_txs = LIST_HEAD_INIT(stale_txs);
2391 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2393 while (!list_empty(&peer_ni->ksnp_tx_queue)) {
2394 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2395 struct ksock_tx, tx_list);
2397 if (ktime_get_seconds() < tx->tx_deadline)
2400 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2402 list_del(&tx->tx_list);
2403 list_add_tail(&tx->tx_list, &stale_txs);
2406 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2408 ksocknal_txlist_done(peer_ni->ksnp_ni, &stale_txs, -ETIMEDOUT);
2412 ksocknal_send_keepalive_locked(struct ksock_peer_ni *peer_ni)
2413 __must_hold(&ksocknal_data.ksnd_global_lock)
2415 struct ksock_sched *sched;
2416 struct ksock_conn *conn;
2417 struct ksock_tx *tx;
2419 /* last_alive will be updated by create_conn */
2420 if (list_empty(&peer_ni->ksnp_conns))
2423 if (peer_ni->ksnp_proto != &ksocknal_protocol_v3x)
2426 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2427 ktime_get_seconds() < peer_ni->ksnp_last_alive +
2428 *ksocknal_tunables.ksnd_keepalive)
2431 if (ktime_get_seconds() < peer_ni->ksnp_send_keepalive)
2434 /* retry 10 secs later, so we wouldn't put pressure
2435 * on this peer_ni if we failed to send keepalive this time */
2436 peer_ni->ksnp_send_keepalive = ktime_get_seconds() + 10;
2438 conn = ksocknal_find_conn_locked(peer_ni, NULL, 1);
2440 sched = conn->ksnc_scheduler;
2442 spin_lock_bh(&sched->kss_lock);
2443 if (!list_empty(&conn->ksnc_tx_queue)) {
2444 spin_unlock_bh(&sched->kss_lock);
2445 /* there is an queued ACK, don't need keepalive */
2449 spin_unlock_bh(&sched->kss_lock);
2452 read_unlock(&ksocknal_data.ksnd_global_lock);
2454 /* cookie = 1 is reserved for keepalive PING */
2455 tx = ksocknal_alloc_tx_noop(1, 1);
2457 read_lock(&ksocknal_data.ksnd_global_lock);
2461 if (ksocknal_launch_packet(peer_ni->ksnp_ni, tx, peer_ni->ksnp_id) == 0) {
2462 read_lock(&ksocknal_data.ksnd_global_lock);
2466 ksocknal_free_tx(tx);
2467 read_lock(&ksocknal_data.ksnd_global_lock);
2474 ksocknal_check_peer_timeouts(int idx)
2476 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2477 struct ksock_peer_ni *peer_ni;
2478 struct ksock_conn *conn;
2479 struct ksock_tx *tx;
2482 /* NB. We expect to have a look at all the peers and not find any
2483 * connections to time out, so we just use a shared lock while we
2485 read_lock(&ksocknal_data.ksnd_global_lock);
2487 list_for_each_entry(peer_ni, peers, ksnp_list) {
2488 struct ksock_tx *tx_stale;
2489 time64_t deadline = 0;
2493 if (ksocknal_send_keepalive_locked(peer_ni) != 0) {
2494 read_unlock(&ksocknal_data.ksnd_global_lock);
2498 conn = ksocknal_find_timed_out_conn (peer_ni);
2501 read_unlock(&ksocknal_data.ksnd_global_lock);
2503 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2505 /* NB we won't find this one again, but we can't
2506 * just proceed with the next peer_ni, since we dropped
2507 * ksnd_global_lock and it might be dead already! */
2508 ksocknal_conn_decref(conn);
2512 /* we can't process stale txs right here because we're
2513 * holding only shared lock */
2514 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
2515 struct ksock_tx *tx;
2517 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2518 struct ksock_tx, tx_list);
2519 if (ktime_get_seconds() >= tx->tx_deadline) {
2520 ksocknal_peer_addref(peer_ni);
2521 read_unlock(&ksocknal_data.ksnd_global_lock);
2523 ksocknal_flush_stale_txs(peer_ni);
2525 ksocknal_peer_decref(peer_ni);
2530 if (list_empty(&peer_ni->ksnp_zc_req_list))
2534 spin_lock(&peer_ni->ksnp_lock);
2535 list_for_each_entry(tx, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
2536 if (ktime_get_seconds() < tx->tx_deadline)
2538 /* ignore the TX if connection is being closed */
2539 if (tx->tx_conn->ksnc_closing)
2542 if (tx_stale == NULL)
2546 if (tx_stale == NULL) {
2547 spin_unlock(&peer_ni->ksnp_lock);
2551 deadline = tx_stale->tx_deadline;
2552 resid = tx_stale->tx_resid;
2553 conn = tx_stale->tx_conn;
2554 ksocknal_conn_addref(conn);
2556 spin_unlock(&peer_ni->ksnp_lock);
2557 read_unlock(&ksocknal_data.ksnd_global_lock);
2559 CERROR("Total %d stale ZC_REQs for peer_ni %s detected; the "
2560 "oldest(%p) timed out %lld secs ago, "
2561 "resid: %d, wmem: %d\n",
2562 n, libcfs_nid2str(peer_ni->ksnp_id.nid), tx_stale,
2563 ktime_get_seconds() - deadline,
2564 resid, conn->ksnc_sock->sk->sk_wmem_queued);
2566 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2567 ksocknal_conn_decref(conn);
2571 read_unlock(&ksocknal_data.ksnd_global_lock);
2574 int ksocknal_reaper(void *arg)
2576 wait_queue_entry_t wait;
2577 struct ksock_conn *conn;
2578 struct ksock_sched *sched;
2579 struct list_head enomem_conns;
2584 time64_t deadline = ktime_get_seconds();
2586 cfs_block_allsigs ();
2588 INIT_LIST_HEAD(&enomem_conns);
2589 init_waitqueue_entry(&wait, current);
2591 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2593 while (!ksocknal_data.ksnd_shuttingdown) {
2594 if (!list_empty(&ksocknal_data.ksnd_deathrow_conns)) {
2595 conn = list_entry(ksocknal_data.ksnd_deathrow_conns.next,
2596 struct ksock_conn, ksnc_list);
2597 list_del(&conn->ksnc_list);
2599 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2601 ksocknal_terminate_conn(conn);
2602 ksocknal_conn_decref(conn);
2604 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2608 if (!list_empty(&ksocknal_data.ksnd_zombie_conns)) {
2609 conn = list_entry(ksocknal_data.ksnd_zombie_conns.next,
2610 struct ksock_conn, ksnc_list);
2611 list_del(&conn->ksnc_list);
2613 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2615 ksocknal_destroy_conn(conn);
2617 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2621 if (!list_empty(&ksocknal_data.ksnd_enomem_conns)) {
2622 list_add(&enomem_conns,
2623 &ksocknal_data.ksnd_enomem_conns);
2624 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2627 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2629 /* reschedule all the connections that stalled with ENOMEM... */
2631 while (!list_empty(&enomem_conns)) {
2632 conn = list_entry(enomem_conns.next,
2633 struct ksock_conn, ksnc_tx_list);
2634 list_del(&conn->ksnc_tx_list);
2636 sched = conn->ksnc_scheduler;
2638 spin_lock_bh(&sched->kss_lock);
2640 LASSERT(conn->ksnc_tx_scheduled);
2641 conn->ksnc_tx_ready = 1;
2642 list_add_tail(&conn->ksnc_tx_list,
2643 &sched->kss_tx_conns);
2644 wake_up(&sched->kss_waitq);
2646 spin_unlock_bh(&sched->kss_lock);
2650 /* careful with the jiffy wrap... */
2651 while ((timeout = deadline - ktime_get_seconds()) <= 0) {
2654 int chunk = ksocknal_data.ksnd_peer_hash_size;
2655 unsigned int lnd_timeout;
2657 /* Time to check for timeouts on a few more peers: I do
2658 * checks every 'p' seconds on a proportion of the peer_ni
2659 * table and I need to check every connection 'n' times
2660 * within a timeout interval, to ensure I detect a
2661 * timeout on any connection within (n+1)/n times the
2662 * timeout interval. */
2664 lnd_timeout = lnet_get_lnd_timeout();
2665 if (lnd_timeout > n * p)
2666 chunk = (chunk * n * p) / lnd_timeout;
2670 for (i = 0; i < chunk; i++) {
2671 ksocknal_check_peer_timeouts (peer_index);
2672 peer_index = (peer_index + 1) %
2673 ksocknal_data.ksnd_peer_hash_size;
2679 if (nenomem_conns != 0) {
2680 /* Reduce my timeout if I rescheduled ENOMEM conns.
2681 * This also prevents me getting woken immediately
2682 * if any go back on my enomem list. */
2683 timeout = SOCKNAL_ENOMEM_RETRY;
2685 ksocknal_data.ksnd_reaper_waketime = ktime_get_seconds() +
2688 set_current_state(TASK_INTERRUPTIBLE);
2689 add_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2691 if (!ksocknal_data.ksnd_shuttingdown &&
2692 list_empty(&ksocknal_data.ksnd_deathrow_conns) &&
2693 list_empty(&ksocknal_data.ksnd_zombie_conns))
2694 schedule_timeout(cfs_time_seconds(timeout));
2696 set_current_state(TASK_RUNNING);
2697 remove_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2699 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2702 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2704 ksocknal_thread_fini();