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,
115 struct kvec *scratch_iov)
117 struct kvec *iov = tx->tx_iov;
121 LASSERT(tx->tx_niov > 0);
123 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
124 rc = ksocknal_lib_send_iov(conn, tx, scratch_iov);
126 if (rc <= 0) /* sent nothing? */
130 LASSERT(nob <= tx->tx_resid);
135 LASSERT(tx->tx_niov > 0);
137 if (nob < (int) iov->iov_len) {
138 iov->iov_base += nob;
152 ksocknal_send_kiov(struct ksock_conn *conn, struct ksock_tx *tx,
153 struct kvec *scratch_iov)
155 lnet_kiov_t *kiov = tx->tx_kiov;
159 LASSERT(tx->tx_niov == 0);
160 LASSERT(tx->tx_nkiov > 0);
162 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
163 rc = ksocknal_lib_send_kiov(conn, tx, scratch_iov);
165 if (rc <= 0) /* sent nothing? */
169 LASSERT(nob <= tx->tx_resid);
174 LASSERT(tx->tx_nkiov > 0);
176 if (nob < (int)kiov->kiov_len) {
177 kiov->kiov_offset += nob;
178 kiov->kiov_len -= nob;
182 nob -= (int)kiov->kiov_len;
183 tx->tx_kiov = ++kiov;
191 ksocknal_transmit(struct ksock_conn *conn, struct ksock_tx *tx,
192 struct kvec *scratch_iov)
197 if (ksocknal_data.ksnd_stall_tx != 0) {
198 set_current_state(TASK_UNINTERRUPTIBLE);
199 schedule_timeout(cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
202 LASSERT(tx->tx_resid != 0);
204 rc = ksocknal_connsock_addref(conn);
206 LASSERT(conn->ksnc_closing);
211 if (ksocknal_data.ksnd_enomem_tx > 0) {
213 ksocknal_data.ksnd_enomem_tx--;
215 } else if (tx->tx_niov != 0) {
216 rc = ksocknal_send_iov(conn, tx, scratch_iov);
218 rc = ksocknal_send_kiov(conn, tx, scratch_iov);
221 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
222 if (rc > 0) /* sent something? */
223 conn->ksnc_tx_bufnob += rc; /* account it */
225 if (bufnob < conn->ksnc_tx_bufnob) {
226 /* allocated send buffer bytes < computed; infer
227 * something got ACKed */
228 conn->ksnc_tx_deadline = ktime_get_seconds() +
229 lnet_get_lnd_timeout();
230 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
231 conn->ksnc_tx_bufnob = bufnob;
235 if (rc <= 0) { /* Didn't write anything? */
236 /* some stacks return 0 instead of -EAGAIN */
240 /* Check if EAGAIN is due to memory pressure */
241 if (rc == -EAGAIN && ksocknal_lib_memory_pressure(conn))
247 /* socket's wmem_queued now includes 'rc' bytes */
248 atomic_sub (rc, &conn->ksnc_tx_nob);
251 } while (tx->tx_resid != 0);
253 ksocknal_connsock_decref(conn);
258 ksocknal_recv_iov(struct ksock_conn *conn, struct kvec *scratchiov)
260 struct kvec *iov = conn->ksnc_rx_iov;
264 LASSERT(conn->ksnc_rx_niov > 0);
266 /* Never touch conn->ksnc_rx_iov or change connection
267 * status inside ksocknal_lib_recv_iov */
268 rc = ksocknal_lib_recv_iov(conn, scratchiov);
273 /* received something... */
276 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
277 conn->ksnc_rx_deadline = ktime_get_seconds() +
278 lnet_get_lnd_timeout();
279 smp_mb(); /* order with setting rx_started */
280 conn->ksnc_rx_started = 1;
282 conn->ksnc_rx_nob_wanted -= nob;
283 conn->ksnc_rx_nob_left -= nob;
286 LASSERT(conn->ksnc_rx_niov > 0);
288 if (nob < (int)iov->iov_len) {
290 iov->iov_base += nob;
295 conn->ksnc_rx_iov = ++iov;
296 conn->ksnc_rx_niov--;
303 ksocknal_recv_kiov(struct ksock_conn *conn, struct page **rx_scratch_pgs,
304 struct kvec *scratch_iov)
306 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
309 LASSERT(conn->ksnc_rx_nkiov > 0);
311 /* Never touch conn->ksnc_rx_kiov or change connection
312 * status inside ksocknal_lib_recv_iov */
313 rc = ksocknal_lib_recv_kiov(conn, rx_scratch_pgs, scratch_iov);
318 /* received something... */
321 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
322 conn->ksnc_rx_deadline = ktime_get_seconds() +
323 lnet_get_lnd_timeout();
324 smp_mb(); /* order with setting rx_started */
325 conn->ksnc_rx_started = 1;
327 conn->ksnc_rx_nob_wanted -= nob;
328 conn->ksnc_rx_nob_left -= nob;
331 LASSERT(conn->ksnc_rx_nkiov > 0);
333 if (nob < (int) kiov->kiov_len) {
334 kiov->kiov_offset += nob;
335 kiov->kiov_len -= nob;
339 nob -= kiov->kiov_len;
340 conn->ksnc_rx_kiov = ++kiov;
341 conn->ksnc_rx_nkiov--;
348 ksocknal_receive(struct ksock_conn *conn, struct page **rx_scratch_pgs,
349 struct kvec *scratch_iov)
351 /* Return 1 on success, 0 on EOF, < 0 on error.
352 * Caller checks ksnc_rx_nob_wanted to determine
353 * progress/completion. */
357 if (ksocknal_data.ksnd_stall_rx != 0) {
358 set_current_state(TASK_UNINTERRUPTIBLE);
359 schedule_timeout(cfs_time_seconds(ksocknal_data.ksnd_stall_rx));
362 rc = ksocknal_connsock_addref(conn);
364 LASSERT(conn->ksnc_closing);
369 if (conn->ksnc_rx_niov != 0)
370 rc = ksocknal_recv_iov(conn, scratch_iov);
372 rc = ksocknal_recv_kiov(conn, rx_scratch_pgs,
376 /* error/EOF or partial receive */
379 } else if (rc == 0 && conn->ksnc_rx_started) {
380 /* EOF in the middle of a message */
386 /* Completed a fragment */
388 if (conn->ksnc_rx_nob_wanted == 0) {
394 ksocknal_connsock_decref(conn);
399 ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx, int rc)
401 struct lnet_msg *lnetmsg = tx->tx_lnetmsg;
402 enum lnet_msg_hstatus hstatus = tx->tx_hstatus;
405 LASSERT(ni != NULL || tx->tx_conn != NULL);
407 if (!rc && (tx->tx_resid != 0 || tx->tx_zc_aborted)) {
409 if (hstatus == LNET_MSG_STATUS_OK)
410 hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
413 if (tx->tx_conn != NULL)
414 ksocknal_conn_decref(tx->tx_conn);
416 ksocknal_free_tx(tx);
417 if (lnetmsg != NULL) { /* KSOCK_MSG_NOOP go without lnetmsg */
418 lnetmsg->msg_health_status = hstatus;
419 lnet_finalize(lnetmsg, rc);
426 ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist, int error)
430 while (!list_empty(txlist)) {
431 tx = list_entry(txlist->next, struct ksock_tx, tx_list);
433 if (error && tx->tx_lnetmsg != NULL) {
434 CNETERR("Deleting packet type %d len %d %s->%s\n",
435 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.type),
436 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.payload_length),
437 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
438 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
440 CNETERR("Deleting noop packet\n");
443 list_del(&tx->tx_list);
445 if (tx->tx_hstatus == LNET_MSG_STATUS_OK) {
446 if (error == -ETIMEDOUT)
448 LNET_MSG_STATUS_LOCAL_TIMEOUT;
449 else if (error == -ENETDOWN ||
450 error == -EHOSTUNREACH ||
451 error == -ENETUNREACH ||
452 error == -ECONNREFUSED ||
453 error == -ECONNRESET)
454 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
456 * for all other errors we don't want to
460 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
463 LASSERT(atomic_read(&tx->tx_refcount) == 1);
464 ksocknal_tx_done(ni, tx, error);
469 ksocknal_check_zc_req(struct ksock_tx *tx)
471 struct ksock_conn *conn = tx->tx_conn;
472 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
474 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
475 * to ksnp_zc_req_list if some fragment of this message should be sent
476 * zero-copy. Our peer_ni will send an ACK containing this cookie when
477 * she has received this message to tell us we can signal completion.
478 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
479 * ksnp_zc_req_list. */
480 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
481 LASSERT (tx->tx_zc_capable);
483 tx->tx_zc_checked = 1;
485 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
486 !conn->ksnc_zc_capable)
489 /* assign cookie and queue tx to pending list, it will be released when
490 * a matching ack is received. See ksocknal_handle_zcack() */
492 ksocknal_tx_addref(tx);
494 spin_lock(&peer_ni->ksnp_lock);
496 /* ZC_REQ is going to be pinned to the peer_ni */
497 tx->tx_deadline = ktime_get_seconds() +
498 lnet_get_lnd_timeout();
500 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
502 tx->tx_msg.ksm_zc_cookies[0] = peer_ni->ksnp_zc_next_cookie++;
504 if (peer_ni->ksnp_zc_next_cookie == 0)
505 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
507 list_add_tail(&tx->tx_zc_list, &peer_ni->ksnp_zc_req_list);
509 spin_unlock(&peer_ni->ksnp_lock);
513 ksocknal_uncheck_zc_req(struct ksock_tx *tx)
515 struct ksock_peer_ni *peer_ni = tx->tx_conn->ksnc_peer;
517 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
518 LASSERT(tx->tx_zc_capable);
520 tx->tx_zc_checked = 0;
522 spin_lock(&peer_ni->ksnp_lock);
524 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
525 /* Not waiting for an ACK */
526 spin_unlock(&peer_ni->ksnp_lock);
530 tx->tx_msg.ksm_zc_cookies[0] = 0;
531 list_del(&tx->tx_zc_list);
533 spin_unlock(&peer_ni->ksnp_lock);
535 ksocknal_tx_decref(tx);
539 ksocknal_process_transmit(struct ksock_conn *conn, struct ksock_tx *tx,
540 struct kvec *scratch_iov)
543 bool error_sim = false;
545 if (lnet_send_error_simulation(tx->tx_lnetmsg, &tx->tx_hstatus)) {
551 if (tx->tx_zc_capable && !tx->tx_zc_checked)
552 ksocknal_check_zc_req(tx);
554 rc = ksocknal_transmit(conn, tx, scratch_iov);
556 CDEBUG(D_NET, "send(%d) %d\n", tx->tx_resid, rc);
558 if (tx->tx_resid == 0) {
559 /* Sent everything OK */
571 counter++; /* exponential backoff warnings */
572 if ((counter & (-counter)) == counter)
573 CWARN("%u ENOMEM tx %p (%u allocated)\n",
574 counter, conn, atomic_read(&libcfs_kmemory));
576 /* Queue on ksnd_enomem_conns for retry after a timeout */
577 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
579 /* enomem list takes over scheduler's ref... */
580 LASSERT(conn->ksnc_tx_scheduled);
581 list_add_tail(&conn->ksnc_tx_list,
582 &ksocknal_data.ksnd_enomem_conns);
583 if (ktime_get_seconds() + SOCKNAL_ENOMEM_RETRY <
584 ksocknal_data.ksnd_reaper_waketime)
585 wake_up(&ksocknal_data.ksnd_reaper_waitq);
587 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
590 * set the health status of the message which determines
591 * whether we should retry the transmit
593 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
604 * set the health status of the message which determines
605 * whether we should retry the transmit
607 if (rc == -ETIMEDOUT)
608 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_TIMEOUT;
610 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
613 if (!conn->ksnc_closing) {
616 LCONSOLE_WARN("Host %pI4h reset our connection "
617 "while we were sending data; it may have "
622 LCONSOLE_WARN("There was an unexpected network error "
623 "while writing to %pI4h: %d.\n",
624 &conn->ksnc_ipaddr, rc);
627 CDEBUG(D_NET, "[%p] Error %d on write to %s ip %pI4h:%d\n",
628 conn, rc, libcfs_id2str(conn->ksnc_peer->ksnp_id),
629 &conn->ksnc_ipaddr, conn->ksnc_port);
632 if (tx->tx_zc_checked)
633 ksocknal_uncheck_zc_req(tx);
635 /* it's not an error if conn is being closed */
636 ksocknal_close_conn_and_siblings(conn,
637 (conn->ksnc_closing) ? 0 : rc);
643 ksocknal_launch_connection_locked(struct ksock_route *route)
646 /* called holding write lock on ksnd_global_lock */
648 LASSERT (!route->ksnr_scheduled);
649 LASSERT (!route->ksnr_connecting);
650 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
652 route->ksnr_scheduled = 1; /* scheduling conn for connd */
653 ksocknal_route_addref(route); /* extra ref for connd */
655 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
657 list_add_tail(&route->ksnr_connd_list,
658 &ksocknal_data.ksnd_connd_routes);
659 wake_up(&ksocknal_data.ksnd_connd_waitq);
661 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
665 ksocknal_launch_all_connections_locked(struct ksock_peer_ni *peer_ni)
667 struct ksock_route *route;
669 /* called holding write lock on ksnd_global_lock */
671 /* launch any/all connections that need it */
672 route = ksocknal_find_connectable_route_locked(peer_ni);
676 ksocknal_launch_connection_locked(route);
681 ksocknal_find_conn_locked(struct ksock_peer_ni *peer_ni, struct ksock_tx *tx, int nonblk)
683 struct list_head *tmp;
684 struct ksock_conn *conn;
685 struct ksock_conn *typed = NULL;
686 struct ksock_conn *fallback = NULL;
690 list_for_each(tmp, &peer_ni->ksnp_conns) {
691 struct ksock_conn *c = list_entry(tmp, struct ksock_conn,
693 int nob = atomic_read(&c->ksnc_tx_nob) +
694 c->ksnc_sock->sk->sk_wmem_queued;
697 LASSERT (!c->ksnc_closing);
698 LASSERT (c->ksnc_proto != NULL &&
699 c->ksnc_proto->pro_match_tx != NULL);
701 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
706 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
709 case SOCKNAL_MATCH_YES: /* typed connection */
710 if (typed == NULL || tnob > nob ||
711 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
712 typed->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
718 case SOCKNAL_MATCH_MAY: /* fallback connection */
719 if (fallback == NULL || fnob > nob ||
720 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
721 fallback->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
729 /* prefer the typed selection */
730 conn = (typed != NULL) ? typed : fallback;
733 conn->ksnc_tx_last_post = ktime_get_seconds();
739 ksocknal_tx_prep(struct ksock_conn *conn, struct ksock_tx *tx)
741 conn->ksnc_proto->pro_pack(tx);
743 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
744 ksocknal_conn_addref(conn); /* +1 ref for tx */
749 ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn)
751 struct ksock_sched *sched = conn->ksnc_scheduler;
752 struct ksock_msg *msg = &tx->tx_msg;
753 struct ksock_tx *ztx = NULL;
756 /* called holding global lock (read or irq-write) and caller may
757 * not have dropped this lock between finding conn and calling me,
758 * so we don't need the {get,put}connsock dance to deref
760 LASSERT(!conn->ksnc_closing);
762 CDEBUG(D_NET, "Sending to %s ip %pI4h:%d\n",
763 libcfs_id2str(conn->ksnc_peer->ksnp_id),
764 &conn->ksnc_ipaddr, conn->ksnc_port);
766 ksocknal_tx_prep(conn, tx);
768 /* Ensure the frags we've been given EXACTLY match the number of
769 * bytes we want to send. Many TCP/IP stacks disregard any total
770 * size parameters passed to them and just look at the frags.
772 * We always expect at least 1 mapped fragment containing the
773 * complete ksocknal message header. */
774 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
775 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
776 (unsigned int)tx->tx_nob);
777 LASSERT (tx->tx_niov >= 1);
778 LASSERT (tx->tx_resid == tx->tx_nob);
780 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
781 tx, (tx->tx_lnetmsg != NULL) ? tx->tx_lnetmsg->msg_hdr.type:
783 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
785 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
786 spin_lock_bh(&sched->kss_lock);
788 if (list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
789 /* First packet starts the timeout */
790 conn->ksnc_tx_deadline = ktime_get_seconds() +
791 lnet_get_lnd_timeout();
792 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
793 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
794 conn->ksnc_tx_bufnob = 0;
795 smp_mb(); /* order with adding to tx_queue */
798 if (msg->ksm_type == KSOCK_MSG_NOOP) {
799 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
800 * on a normal packet so I don't need to send it */
801 LASSERT (msg->ksm_zc_cookies[1] != 0);
802 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
804 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
805 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
808 /* It's a normal packet - can it piggback a noop zc-ack that
809 * has been queued already? */
810 LASSERT (msg->ksm_zc_cookies[1] == 0);
811 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
813 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
814 /* ztx will be released later */
818 atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
819 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
822 if (conn->ksnc_tx_ready && /* able to send */
823 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
824 /* +1 ref for scheduler */
825 ksocknal_conn_addref(conn);
826 list_add_tail(&conn->ksnc_tx_list,
827 &sched->kss_tx_conns);
828 conn->ksnc_tx_scheduled = 1;
829 wake_up(&sched->kss_waitq);
832 spin_unlock_bh(&sched->kss_lock);
837 ksocknal_find_connectable_route_locked(struct ksock_peer_ni *peer_ni)
839 time64_t now = ktime_get_seconds();
840 struct list_head *tmp;
841 struct ksock_route *route;
843 list_for_each(tmp, &peer_ni->ksnp_routes) {
844 route = list_entry(tmp, struct ksock_route, ksnr_list);
846 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
848 if (route->ksnr_scheduled) /* connections being established */
851 /* all route types connected ? */
852 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
855 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
856 now >= route->ksnr_timeout)) {
858 "Too soon to retry route %pI4h "
859 "(cnted %d, interval %lld, %lld secs later)\n",
861 route->ksnr_connected,
862 route->ksnr_retry_interval,
863 route->ksnr_timeout - now);
874 ksocknal_find_connecting_route_locked(struct ksock_peer_ni *peer_ni)
876 struct list_head *tmp;
877 struct ksock_route *route;
879 list_for_each(tmp, &peer_ni->ksnp_routes) {
880 route = list_entry(tmp, struct ksock_route, ksnr_list);
882 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
884 if (route->ksnr_scheduled)
892 ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
893 struct lnet_process_id id)
895 struct ksock_peer_ni *peer_ni;
896 struct ksock_conn *conn;
901 LASSERT (tx->tx_conn == NULL);
903 g_lock = &ksocknal_data.ksnd_global_lock;
905 for (retry = 0;; retry = 1) {
907 peer_ni = ksocknal_find_peer_locked(ni, id);
908 if (peer_ni != NULL) {
909 if (ksocknal_find_connectable_route_locked(peer_ni) == NULL) {
910 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
912 /* I've got no routes that need to be
913 * connecting and I do have an actual
915 ksocknal_queue_tx_locked (tx, conn);
922 /* I'll need a write lock... */
925 write_lock_bh(g_lock);
927 peer_ni = ksocknal_find_peer_locked(ni, id);
931 write_unlock_bh(g_lock);
933 if ((id.pid & LNET_PID_USERFLAG) != 0) {
934 CERROR("Refusing to create a connection to "
935 "userspace process %s\n", libcfs_id2str(id));
936 return -EHOSTUNREACH;
940 CERROR("Can't find peer_ni %s\n", libcfs_id2str(id));
941 return -EHOSTUNREACH;
944 rc = ksocknal_add_peer(ni, id,
945 LNET_NIDADDR(id.nid),
946 lnet_acceptor_port());
948 CERROR("Can't add peer_ni %s: %d\n",
949 libcfs_id2str(id), rc);
954 ksocknal_launch_all_connections_locked(peer_ni);
956 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
958 /* Connection exists; queue message on it */
959 ksocknal_queue_tx_locked (tx, conn);
960 write_unlock_bh(g_lock);
964 if (peer_ni->ksnp_accepting > 0 ||
965 ksocknal_find_connecting_route_locked (peer_ni) != NULL) {
966 /* the message is going to be pinned to the peer_ni */
967 tx->tx_deadline = ktime_get_seconds() +
968 lnet_get_lnd_timeout();
970 /* Queue the message until a connection is established */
971 list_add_tail(&tx->tx_list, &peer_ni->ksnp_tx_queue);
972 write_unlock_bh(g_lock);
976 write_unlock_bh(g_lock);
978 /* NB Routes may be ignored if connections to them failed recently */
979 CNETERR("No usable routes to %s\n", libcfs_id2str(id));
980 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
981 return (-EHOSTUNREACH);
985 ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
988 int type = lntmsg->msg_type;
989 struct lnet_process_id target = lntmsg->msg_target;
990 unsigned int payload_niov = lntmsg->msg_niov;
991 struct kvec *payload_iov = lntmsg->msg_iov;
992 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
993 unsigned int payload_offset = lntmsg->msg_offset;
994 unsigned int payload_nob = lntmsg->msg_len;
999 /* NB 'private' is different depending on what we're sending.
1000 * Just ignore it... */
1002 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
1003 payload_nob, payload_niov, libcfs_id2str(target));
1005 LASSERT (payload_nob == 0 || payload_niov > 0);
1006 LASSERT (payload_niov <= LNET_MAX_IOV);
1007 /* payload is either all vaddrs or all pages */
1008 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1009 LASSERT (!in_interrupt ());
1011 if (payload_iov != NULL)
1012 desc_size = offsetof(struct ksock_tx,
1013 tx_frags.virt.iov[1 + payload_niov]);
1015 desc_size = offsetof(struct ksock_tx,
1016 tx_frags.paged.kiov[payload_niov]);
1018 if (lntmsg->msg_vmflush)
1019 mpflag = cfs_memory_pressure_get_and_set();
1020 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
1022 CERROR("Can't allocate tx desc type %d size %d\n",
1024 if (lntmsg->msg_vmflush)
1025 cfs_memory_pressure_restore(mpflag);
1029 tx->tx_conn = NULL; /* set when assigned a conn */
1030 tx->tx_lnetmsg = lntmsg;
1032 if (payload_iov != NULL) {
1035 tx->tx_iov = tx->tx_frags.virt.iov;
1037 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
1038 payload_niov, payload_iov,
1039 payload_offset, payload_nob);
1042 tx->tx_iov = &tx->tx_frags.paged.iov;
1043 tx->tx_kiov = tx->tx_frags.paged.kiov;
1044 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
1045 payload_niov, payload_kiov,
1046 payload_offset, payload_nob);
1048 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
1049 tx->tx_zc_capable = 1;
1052 tx->tx_msg.ksm_csum = 0;
1053 tx->tx_msg.ksm_type = KSOCK_MSG_LNET;
1054 tx->tx_msg.ksm_zc_cookies[0] = 0;
1055 tx->tx_msg.ksm_zc_cookies[1] = 0;
1057 /* The first fragment will be set later in pro_pack */
1058 rc = ksocknal_launch_packet(ni, tx, target);
1060 cfs_memory_pressure_restore(mpflag);
1065 lntmsg->msg_health_status = tx->tx_hstatus;
1066 ksocknal_free_tx(tx);
1071 ksocknal_thread_start(int (*fn)(void *arg), void *arg, char *name)
1073 struct task_struct *task = kthread_run(fn, arg, name);
1076 return PTR_ERR(task);
1078 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1079 ksocknal_data.ksnd_nthreads++;
1080 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1085 ksocknal_thread_fini (void)
1087 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1088 ksocknal_data.ksnd_nthreads--;
1089 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1093 ksocknal_new_packet(struct ksock_conn *conn, int nob_to_skip)
1095 static char ksocknal_slop_buffer[4096];
1100 LASSERT(conn->ksnc_proto != NULL);
1102 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1103 /* Remind the socket to ack eagerly... */
1104 ksocknal_lib_eager_ack(conn);
1107 if (nob_to_skip == 0) { /* right at next packet boundary now */
1108 conn->ksnc_rx_started = 0;
1109 smp_mb(); /* racing with timeout thread */
1111 switch (conn->ksnc_proto->pro_version) {
1112 case KSOCK_PROTO_V2:
1113 case KSOCK_PROTO_V3:
1114 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1115 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1116 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1118 conn->ksnc_rx_nob_wanted = offsetof(struct ksock_msg, ksm_u);
1119 conn->ksnc_rx_nob_left = offsetof(struct ksock_msg, ksm_u);
1120 conn->ksnc_rx_iov[0].iov_len = offsetof(struct ksock_msg, ksm_u);
1123 case KSOCK_PROTO_V1:
1124 /* Receiving bare struct lnet_hdr */
1125 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1126 conn->ksnc_rx_nob_wanted = sizeof(struct lnet_hdr);
1127 conn->ksnc_rx_nob_left = sizeof(struct lnet_hdr);
1129 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1130 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1131 conn->ksnc_rx_iov[0].iov_len = sizeof(struct lnet_hdr);
1137 conn->ksnc_rx_niov = 1;
1139 conn->ksnc_rx_kiov = NULL;
1140 conn->ksnc_rx_nkiov = 0;
1141 conn->ksnc_rx_csum = ~0;
1145 /* Set up to skip as much as possible now. If there's more left
1146 * (ran out of iov entries) we'll get called again */
1148 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1149 conn->ksnc_rx_nob_left = nob_to_skip;
1150 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1155 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1157 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1158 conn->ksnc_rx_iov[niov].iov_len = nob;
1163 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1164 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof(struct kvec));
1166 conn->ksnc_rx_niov = niov;
1167 conn->ksnc_rx_kiov = NULL;
1168 conn->ksnc_rx_nkiov = 0;
1169 conn->ksnc_rx_nob_wanted = skipped;
1174 ksocknal_process_receive(struct ksock_conn *conn,
1175 struct page **rx_scratch_pgs,
1176 struct kvec *scratch_iov)
1178 struct lnet_hdr *lhdr;
1179 struct lnet_process_id *id;
1182 LASSERT (atomic_read(&conn->ksnc_conn_refcount) > 0);
1184 /* NB: sched lock NOT held */
1185 /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */
1186 LASSERT(conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1187 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1188 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1189 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1191 if (conn->ksnc_rx_nob_wanted != 0) {
1192 rc = ksocknal_receive(conn, rx_scratch_pgs,
1196 struct lnet_process_id ksnp_id;
1198 ksnp_id = conn->ksnc_peer->ksnp_id;
1200 LASSERT(rc != -EAGAIN);
1202 CDEBUG(D_NET, "[%p] EOF from %s "
1203 "ip %pI4h:%d\n", conn,
1204 libcfs_id2str(ksnp_id),
1207 else if (!conn->ksnc_closing)
1208 CERROR("[%p] Error %d on read from %s "
1209 "ip %pI4h:%d\n", conn, rc,
1210 libcfs_id2str(ksnp_id),
1214 /* it's not an error if conn is being closed */
1215 ksocknal_close_conn_and_siblings (conn,
1216 (conn->ksnc_closing) ? 0 : rc);
1217 return (rc == 0 ? -ESHUTDOWN : rc);
1220 if (conn->ksnc_rx_nob_wanted != 0) {
1225 switch (conn->ksnc_rx_state) {
1226 case SOCKNAL_RX_KSM_HEADER:
1227 if (conn->ksnc_flip) {
1228 __swab32s(&conn->ksnc_msg.ksm_type);
1229 __swab32s(&conn->ksnc_msg.ksm_csum);
1230 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1231 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1234 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1235 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1236 CERROR("%s: Unknown message type: %x\n",
1237 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1238 conn->ksnc_msg.ksm_type);
1239 ksocknal_new_packet(conn, 0);
1240 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1244 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1245 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1246 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1247 /* NOOP Checksum error */
1248 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1249 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1250 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1251 ksocknal_new_packet(conn, 0);
1252 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1256 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1259 LASSERT (conn->ksnc_proto != &ksocknal_protocol_v1x);
1261 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1262 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1264 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1265 conn->ksnc_msg.ksm_zc_cookies[1]);
1268 CERROR("%s: Unknown ZC-ACK cookie: %llu, %llu\n",
1269 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1270 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1271 ksocknal_new_packet(conn, 0);
1272 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1277 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1278 ksocknal_new_packet (conn, 0);
1279 return 0; /* NOOP is done and just return */
1282 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1283 conn->ksnc_rx_nob_wanted = sizeof(struct ksock_lnet_msg);
1284 conn->ksnc_rx_nob_left = sizeof(struct ksock_lnet_msg);
1286 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1287 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1288 conn->ksnc_rx_iov[0].iov_len = sizeof(struct ksock_lnet_msg);
1290 conn->ksnc_rx_niov = 1;
1291 conn->ksnc_rx_kiov = NULL;
1292 conn->ksnc_rx_nkiov = 0;
1294 goto again; /* read lnet header now */
1296 case SOCKNAL_RX_LNET_HEADER:
1297 /* unpack message header */
1298 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1300 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1301 /* Userspace peer_ni */
1302 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1303 id = &conn->ksnc_peer->ksnp_id;
1305 /* Substitute process ID assigned at connection time */
1306 lhdr->src_pid = cpu_to_le32(id->pid);
1307 lhdr->src_nid = cpu_to_le64(id->nid);
1310 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1311 ksocknal_conn_addref(conn); /* ++ref while parsing */
1313 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1314 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1315 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1317 /* I just received garbage: give up on this conn */
1318 ksocknal_new_packet(conn, 0);
1319 ksocknal_close_conn_and_siblings (conn, rc);
1320 ksocknal_conn_decref(conn);
1324 /* I'm racing with ksocknal_recv() */
1325 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1326 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1328 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1331 /* ksocknal_recv() got called */
1334 case SOCKNAL_RX_LNET_PAYLOAD:
1335 /* payload all received */
1338 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1339 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1340 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1341 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1342 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1343 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1347 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1348 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1350 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1351 id = &conn->ksnc_peer->ksnp_id;
1353 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1354 conn->ksnc_msg.ksm_zc_cookies[0],
1355 *ksocknal_tunables.ksnd_nonblk_zcack ||
1356 le64_to_cpu(lhdr->src_nid) != id->nid);
1359 if (rc && conn->ksnc_lnet_msg)
1360 conn->ksnc_lnet_msg->msg_health_status =
1361 LNET_MSG_STATUS_REMOTE_ERROR;
1362 lnet_finalize(conn->ksnc_lnet_msg, rc);
1365 ksocknal_new_packet(conn, 0);
1366 ksocknal_close_conn_and_siblings (conn, rc);
1371 case SOCKNAL_RX_SLOP:
1372 /* starting new packet? */
1373 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1374 return 0; /* come back later */
1375 goto again; /* try to finish reading slop now */
1383 return (-EINVAL); /* keep gcc happy */
1387 ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
1388 int delayed, unsigned int niov, struct kvec *iov,
1389 lnet_kiov_t *kiov, unsigned int offset, unsigned int mlen,
1392 struct ksock_conn *conn = private;
1393 struct ksock_sched *sched = conn->ksnc_scheduler;
1395 LASSERT (mlen <= rlen);
1396 LASSERT (niov <= LNET_MAX_IOV);
1398 conn->ksnc_lnet_msg = msg;
1399 conn->ksnc_rx_nob_wanted = mlen;
1400 conn->ksnc_rx_nob_left = rlen;
1402 if (mlen == 0 || iov != NULL) {
1403 conn->ksnc_rx_nkiov = 0;
1404 conn->ksnc_rx_kiov = NULL;
1405 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1406 conn->ksnc_rx_niov =
1407 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1408 niov, iov, offset, mlen);
1410 conn->ksnc_rx_niov = 0;
1411 conn->ksnc_rx_iov = NULL;
1412 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1413 conn->ksnc_rx_nkiov =
1414 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1415 niov, kiov, offset, mlen);
1419 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1420 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1422 LASSERT (conn->ksnc_rx_scheduled);
1424 spin_lock_bh(&sched->kss_lock);
1426 switch (conn->ksnc_rx_state) {
1427 case SOCKNAL_RX_PARSE_WAIT:
1428 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1429 wake_up(&sched->kss_waitq);
1430 LASSERT(conn->ksnc_rx_ready);
1433 case SOCKNAL_RX_PARSE:
1434 /* scheduler hasn't noticed I'm parsing yet */
1438 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1440 spin_unlock_bh(&sched->kss_lock);
1441 ksocknal_conn_decref(conn);
1446 ksocknal_sched_cansleep(struct ksock_sched *sched)
1450 spin_lock_bh(&sched->kss_lock);
1452 rc = (!ksocknal_data.ksnd_shuttingdown &&
1453 list_empty(&sched->kss_rx_conns) &&
1454 list_empty(&sched->kss_tx_conns));
1456 spin_unlock_bh(&sched->kss_lock);
1460 int ksocknal_scheduler(void *arg)
1462 struct ksock_sched *sched;
1463 struct ksock_conn *conn;
1464 struct ksock_tx *tx;
1467 long id = (long)arg;
1468 struct page **rx_scratch_pgs;
1469 struct kvec *scratch_iov;
1471 sched = ksocknal_data.ksnd_schedulers[KSOCK_THREAD_CPT(id)];
1473 LIBCFS_CPT_ALLOC(rx_scratch_pgs, lnet_cpt_table(), sched->kss_cpt,
1474 sizeof(*rx_scratch_pgs) * LNET_MAX_IOV);
1475 if (!rx_scratch_pgs) {
1476 CERROR("Unable to allocate scratch pages\n");
1480 LIBCFS_CPT_ALLOC(scratch_iov, lnet_cpt_table(), sched->kss_cpt,
1481 sizeof(*scratch_iov) * LNET_MAX_IOV);
1483 CERROR("Unable to allocate scratch iov\n");
1487 cfs_block_allsigs();
1489 rc = cfs_cpt_bind(lnet_cpt_table(), sched->kss_cpt);
1491 CWARN("Can't set CPU partition affinity to %d: %d\n",
1492 sched->kss_cpt, rc);
1495 spin_lock_bh(&sched->kss_lock);
1497 while (!ksocknal_data.ksnd_shuttingdown) {
1498 int did_something = 0;
1500 /* Ensure I progress everything semi-fairly */
1502 if (!list_empty(&sched->kss_rx_conns)) {
1503 conn = list_entry(sched->kss_rx_conns.next,
1504 struct ksock_conn, ksnc_rx_list);
1505 list_del(&conn->ksnc_rx_list);
1507 LASSERT(conn->ksnc_rx_scheduled);
1508 LASSERT(conn->ksnc_rx_ready);
1510 /* clear rx_ready in case receive isn't complete.
1511 * Do it BEFORE we call process_recv, since
1512 * data_ready can set it any time after we release
1514 conn->ksnc_rx_ready = 0;
1515 spin_unlock_bh(&sched->kss_lock);
1517 rc = ksocknal_process_receive(conn, rx_scratch_pgs,
1520 spin_lock_bh(&sched->kss_lock);
1522 /* I'm the only one that can clear this flag */
1523 LASSERT(conn->ksnc_rx_scheduled);
1525 /* Did process_receive get everything it wanted? */
1527 conn->ksnc_rx_ready = 1;
1529 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1530 /* Conn blocked waiting for ksocknal_recv()
1531 * I change its state (under lock) to signal
1532 * it can be rescheduled */
1533 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1534 } else if (conn->ksnc_rx_ready) {
1535 /* reschedule for rx */
1536 list_add_tail(&conn->ksnc_rx_list,
1537 &sched->kss_rx_conns);
1539 conn->ksnc_rx_scheduled = 0;
1541 ksocknal_conn_decref(conn);
1547 if (!list_empty(&sched->kss_tx_conns)) {
1550 if (!list_empty(&sched->kss_zombie_noop_txs)) {
1552 &sched->kss_zombie_noop_txs);
1553 list_del_init(&sched->kss_zombie_noop_txs);
1556 conn = list_entry(sched->kss_tx_conns.next,
1557 struct ksock_conn, ksnc_tx_list);
1558 list_del(&conn->ksnc_tx_list);
1560 LASSERT(conn->ksnc_tx_scheduled);
1561 LASSERT(conn->ksnc_tx_ready);
1562 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1564 tx = list_entry(conn->ksnc_tx_queue.next,
1565 struct ksock_tx, tx_list);
1567 if (conn->ksnc_tx_carrier == tx)
1568 ksocknal_next_tx_carrier(conn);
1570 /* dequeue now so empty list => more to send */
1571 list_del(&tx->tx_list);
1573 /* Clear tx_ready in case send isn't complete. Do
1574 * it BEFORE we call process_transmit, since
1575 * write_space can set it any time after we release
1577 conn->ksnc_tx_ready = 0;
1578 spin_unlock_bh(&sched->kss_lock);
1580 if (!list_empty(&zlist)) {
1581 /* free zombie noop txs, it's fast because
1582 * noop txs are just put in freelist */
1583 ksocknal_txlist_done(NULL, &zlist, 0);
1586 rc = ksocknal_process_transmit(conn, tx, scratch_iov);
1588 if (rc == -ENOMEM || rc == -EAGAIN) {
1589 /* Incomplete send: replace tx on HEAD of tx_queue */
1590 spin_lock_bh(&sched->kss_lock);
1591 list_add(&tx->tx_list,
1592 &conn->ksnc_tx_queue);
1594 /* Complete send; tx -ref */
1595 ksocknal_tx_decref(tx);
1597 spin_lock_bh(&sched->kss_lock);
1598 /* assume space for more */
1599 conn->ksnc_tx_ready = 1;
1602 if (rc == -ENOMEM) {
1603 /* Do nothing; after a short timeout, this
1604 * conn will be reposted on kss_tx_conns. */
1605 } else if (conn->ksnc_tx_ready &&
1606 !list_empty(&conn->ksnc_tx_queue)) {
1607 /* reschedule for tx */
1608 list_add_tail(&conn->ksnc_tx_list,
1609 &sched->kss_tx_conns);
1611 conn->ksnc_tx_scheduled = 0;
1613 ksocknal_conn_decref(conn);
1618 if (!did_something || /* nothing to do */
1619 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1620 spin_unlock_bh(&sched->kss_lock);
1624 if (!did_something) { /* wait for something to do */
1625 rc = wait_event_interruptible_exclusive(
1627 !ksocknal_sched_cansleep(sched));
1633 spin_lock_bh(&sched->kss_lock);
1637 spin_unlock_bh(&sched->kss_lock);
1638 LIBCFS_FREE(rx_scratch_pgs, sizeof(*rx_scratch_pgs) *
1640 LIBCFS_FREE(scratch_iov, sizeof(*scratch_iov) *
1642 ksocknal_thread_fini();
1647 * Add connection to kss_rx_conns of scheduler
1648 * and wakeup the scheduler.
1650 void ksocknal_read_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_rx_ready = 1;
1661 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1662 list_add_tail(&conn->ksnc_rx_list,
1663 &sched->kss_rx_conns);
1664 conn->ksnc_rx_scheduled = 1;
1665 /* extra ref for scheduler */
1666 ksocknal_conn_addref(conn);
1668 wake_up (&sched->kss_waitq);
1670 spin_unlock_bh(&sched->kss_lock);
1676 * Add connection to kss_tx_conns of scheduler
1677 * and wakeup the scheduler.
1679 void ksocknal_write_callback(struct ksock_conn *conn)
1681 struct ksock_sched *sched;
1684 sched = conn->ksnc_scheduler;
1686 spin_lock_bh(&sched->kss_lock);
1688 conn->ksnc_tx_ready = 1;
1690 if (!conn->ksnc_tx_scheduled && /* not being progressed */
1691 !list_empty(&conn->ksnc_tx_queue)) { /* packets to send */
1692 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
1693 conn->ksnc_tx_scheduled = 1;
1694 /* extra ref for scheduler */
1695 ksocknal_conn_addref(conn);
1697 wake_up(&sched->kss_waitq);
1700 spin_unlock_bh(&sched->kss_lock);
1705 static struct ksock_proto *
1706 ksocknal_parse_proto_version (struct ksock_hello_msg *hello)
1710 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1711 version = hello->kshm_version;
1712 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1713 version = __swab32(hello->kshm_version);
1716 #if SOCKNAL_VERSION_DEBUG
1717 if (*ksocknal_tunables.ksnd_protocol == 1)
1720 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1721 version == KSOCK_PROTO_V3)
1724 if (version == KSOCK_PROTO_V2)
1725 return &ksocknal_protocol_v2x;
1727 if (version == KSOCK_PROTO_V3)
1728 return &ksocknal_protocol_v3x;
1733 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1734 struct lnet_magicversion *hmv;
1736 BUILD_BUG_ON(sizeof(struct lnet_magicversion) !=
1737 offsetof(struct ksock_hello_msg, kshm_src_nid));
1739 hmv = (struct lnet_magicversion *)hello;
1741 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1742 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1743 return &ksocknal_protocol_v1x;
1750 ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1751 lnet_nid_t peer_nid, struct ksock_hello_msg *hello)
1753 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1754 struct ksock_net *net = (struct ksock_net *)ni->ni_data;
1756 LASSERT(hello->kshm_nips <= LNET_INTERFACES_NUM);
1758 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1759 LASSERT(conn->ksnc_proto != NULL);
1761 hello->kshm_src_nid = ni->ni_nid;
1762 hello->kshm_dst_nid = peer_nid;
1763 hello->kshm_src_pid = the_lnet.ln_pid;
1765 hello->kshm_src_incarnation = net->ksnn_incarnation;
1766 hello->kshm_ctype = conn->ksnc_type;
1768 return conn->ksnc_proto->pro_send_hello(conn, hello);
1772 ksocknal_invert_type(int type)
1776 case SOCKLND_CONN_ANY:
1777 case SOCKLND_CONN_CONTROL:
1779 case SOCKLND_CONN_BULK_IN:
1780 return SOCKLND_CONN_BULK_OUT;
1781 case SOCKLND_CONN_BULK_OUT:
1782 return SOCKLND_CONN_BULK_IN;
1784 return (SOCKLND_CONN_NONE);
1789 ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1790 struct ksock_hello_msg *hello,
1791 struct lnet_process_id *peerid,
1794 /* Return < 0 fatal error
1796 * EALREADY lost connection race
1797 * EPROTO protocol version mismatch
1799 struct socket *sock = conn->ksnc_sock;
1800 int active = (conn->ksnc_proto != NULL);
1804 struct ksock_proto *proto;
1805 struct lnet_process_id recv_id;
1807 /* socket type set on active connections - not set on passive */
1808 LASSERT(!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1810 timeout = active ? lnet_get_lnd_timeout() :
1811 lnet_acceptor_timeout();
1813 rc = lnet_sock_read(sock, &hello->kshm_magic,
1814 sizeof(hello->kshm_magic), timeout);
1816 CERROR("Error %d reading HELLO from %pI4h\n",
1817 rc, &conn->ksnc_ipaddr);
1822 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1823 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1824 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
1825 /* Unexpected magic! */
1826 CERROR ("Bad magic(1) %#08x (%#08x expected) from "
1827 "%pI4h\n", __cpu_to_le32 (hello->kshm_magic),
1828 LNET_PROTO_TCP_MAGIC, &conn->ksnc_ipaddr);
1832 rc = lnet_sock_read(sock, &hello->kshm_version,
1833 sizeof(hello->kshm_version), timeout);
1835 CERROR("Error %d reading HELLO from %pI4h\n",
1836 rc, &conn->ksnc_ipaddr);
1841 proto = ksocknal_parse_proto_version(hello);
1842 if (proto == NULL) {
1844 /* unknown protocol from peer_ni, tell peer_ni my protocol */
1845 conn->ksnc_proto = &ksocknal_protocol_v3x;
1846 #if SOCKNAL_VERSION_DEBUG
1847 if (*ksocknal_tunables.ksnd_protocol == 2)
1848 conn->ksnc_proto = &ksocknal_protocol_v2x;
1849 else if (*ksocknal_tunables.ksnd_protocol == 1)
1850 conn->ksnc_proto = &ksocknal_protocol_v1x;
1852 hello->kshm_nips = 0;
1853 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1856 CERROR("Unknown protocol version (%d.x expected) from %pI4h\n",
1857 conn->ksnc_proto->pro_version, &conn->ksnc_ipaddr);
1862 proto_match = (conn->ksnc_proto == proto);
1863 conn->ksnc_proto = proto;
1865 /* receive the rest of hello message anyway */
1866 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1868 CERROR("Error %d reading or checking hello from from %pI4h\n",
1869 rc, &conn->ksnc_ipaddr);
1874 *incarnation = hello->kshm_src_incarnation;
1876 if (hello->kshm_src_nid == LNET_NID_ANY) {
1877 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
1878 "from %pI4h\n", &conn->ksnc_ipaddr);
1883 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1884 /* Userspace NAL assigns peer_ni process ID from socket */
1885 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1886 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
1888 recv_id.nid = hello->kshm_src_nid;
1889 recv_id.pid = hello->kshm_src_pid;
1895 /* peer_ni determines type */
1896 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1897 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
1898 CERROR("Unexpected type %d from %s ip %pI4h\n",
1899 hello->kshm_ctype, libcfs_id2str(*peerid),
1900 &conn->ksnc_ipaddr);
1906 if (peerid->pid != recv_id.pid ||
1907 peerid->nid != recv_id.nid) {
1908 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1909 " %pI4h, but they claimed they were "
1910 "%s; please check your Lustre "
1912 libcfs_id2str(*peerid),
1914 libcfs_id2str(recv_id));
1918 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1919 /* Possible protocol mismatch or I lost the connection race */
1920 return proto_match ? EALREADY : EPROTO;
1923 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1924 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
1925 conn->ksnc_type, libcfs_id2str(*peerid),
1934 ksocknal_connect(struct ksock_route *route)
1937 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
1940 struct socket *sock;
1942 int retry_later = 0;
1945 deadline = ktime_get_seconds() + lnet_get_lnd_timeout();
1947 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1949 LASSERT (route->ksnr_scheduled);
1950 LASSERT (!route->ksnr_connecting);
1952 route->ksnr_connecting = 1;
1955 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1957 /* stop connecting if peer_ni/route got closed under me, or
1958 * route got connected while queued */
1959 if (peer_ni->ksnp_closing || route->ksnr_deleted ||
1965 /* reschedule if peer_ni is connecting to me */
1966 if (peer_ni->ksnp_accepting > 0) {
1968 "peer_ni %s(%d) already connecting to me, retry later.\n",
1969 libcfs_nid2str(peer_ni->ksnp_id.nid), peer_ni->ksnp_accepting);
1973 if (retry_later) /* needs reschedule */
1976 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
1977 type = SOCKLND_CONN_ANY;
1978 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
1979 type = SOCKLND_CONN_CONTROL;
1980 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
1981 type = SOCKLND_CONN_BULK_IN;
1983 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
1984 type = SOCKLND_CONN_BULK_OUT;
1987 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1989 if (ktime_get_seconds() >= deadline) {
1991 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
1997 rc = lnet_connect(&sock, peer_ni->ksnp_id.nid,
1998 route->ksnr_myipaddr,
1999 route->ksnr_ipaddr, route->ksnr_port,
2000 peer_ni->ksnp_ni->ni_net_ns);
2004 rc = ksocknal_create_conn(peer_ni->ksnp_ni, route, sock, type);
2006 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
2012 /* A +ve RC means I have to retry because I lost the connection
2013 * race or I have to renegotiate protocol version */
2014 retry_later = (rc != 0);
2016 CDEBUG(D_NET, "peer_ni %s: conn race, retry later.\n",
2017 libcfs_nid2str(peer_ni->ksnp_id.nid));
2019 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2022 route->ksnr_scheduled = 0;
2023 route->ksnr_connecting = 0;
2026 /* re-queue for attention; this frees me up to handle
2027 * the peer_ni's incoming connection request */
2029 if (rc == EALREADY ||
2030 (rc == 0 && peer_ni->ksnp_accepting > 0)) {
2031 /* We want to introduce a delay before next
2032 * attempt to connect if we lost conn race,
2033 * but the race is resolved quickly usually,
2034 * so min_reconnectms should be good heuristic */
2035 route->ksnr_retry_interval = *ksocknal_tunables.ksnd_min_reconnectms / 1000;
2036 route->ksnr_timeout = ktime_get_seconds() +
2037 route->ksnr_retry_interval;
2040 ksocknal_launch_connection_locked(route);
2043 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2047 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2049 route->ksnr_scheduled = 0;
2050 route->ksnr_connecting = 0;
2052 /* This is a retry rather than a new connection */
2053 route->ksnr_retry_interval *= 2;
2054 route->ksnr_retry_interval =
2055 max_t(time64_t, route->ksnr_retry_interval,
2056 *ksocknal_tunables.ksnd_min_reconnectms / 1000);
2057 route->ksnr_retry_interval =
2058 min_t(time64_t, route->ksnr_retry_interval,
2059 *ksocknal_tunables.ksnd_max_reconnectms / 1000);
2061 LASSERT(route->ksnr_retry_interval);
2062 route->ksnr_timeout = ktime_get_seconds() + route->ksnr_retry_interval;
2064 if (!list_empty(&peer_ni->ksnp_tx_queue) &&
2065 peer_ni->ksnp_accepting == 0 &&
2066 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
2067 struct ksock_conn *conn;
2069 /* ksnp_tx_queue is queued on a conn on successful
2070 * connection for V1.x and V2.x */
2071 if (!list_empty(&peer_ni->ksnp_conns)) {
2072 conn = list_entry(peer_ni->ksnp_conns.next,
2073 struct ksock_conn, ksnc_list);
2074 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
2077 /* take all the blocked packets while I've got the lock and
2078 * complete below... */
2079 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
2082 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2084 ksocknal_peer_failed(peer_ni);
2085 ksocknal_txlist_done(peer_ni->ksnp_ni, &zombies, rc);
2090 * check whether we need to create more connds.
2091 * It will try to create new thread if it's necessary, @timeout can
2092 * be updated if failed to create, so caller wouldn't keep try while
2093 * running out of resource.
2096 ksocknal_connd_check_start(time64_t sec, long *timeout)
2100 int total = ksocknal_data.ksnd_connd_starting +
2101 ksocknal_data.ksnd_connd_running;
2103 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2104 /* still in initializing */
2108 if (total >= *ksocknal_tunables.ksnd_nconnds_max ||
2109 total > ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV) {
2110 /* can't create more connd, or still have enough
2111 * threads to handle more connecting */
2115 if (list_empty(&ksocknal_data.ksnd_connd_routes)) {
2116 /* no pending connecting request */
2120 if (sec - ksocknal_data.ksnd_connd_failed_stamp <= 1) {
2121 /* may run out of resource, retry later */
2122 *timeout = cfs_time_seconds(1);
2126 if (ksocknal_data.ksnd_connd_starting > 0) {
2127 /* serialize starting to avoid flood */
2131 ksocknal_data.ksnd_connd_starting_stamp = sec;
2132 ksocknal_data.ksnd_connd_starting++;
2133 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2135 /* NB: total is the next id */
2136 snprintf(name, sizeof(name), "socknal_cd%02d", total);
2137 rc = ksocknal_thread_start(ksocknal_connd, NULL, name);
2139 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2144 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2145 ksocknal_data.ksnd_connd_starting--;
2146 ksocknal_data.ksnd_connd_failed_stamp = ktime_get_real_seconds();
2152 * check whether current thread can exit, it will return 1 if there are too
2153 * many threads and no creating in past 120 seconds.
2154 * Also, this function may update @timeout to make caller come back
2155 * again to recheck these conditions.
2158 ksocknal_connd_check_stop(time64_t sec, long *timeout)
2162 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2163 /* still in initializing */
2167 if (ksocknal_data.ksnd_connd_starting > 0) {
2168 /* in progress of starting new thread */
2172 if (ksocknal_data.ksnd_connd_running <=
2173 *ksocknal_tunables.ksnd_nconnds) { /* can't shrink */
2177 /* created thread in past 120 seconds? */
2178 val = (int)(ksocknal_data.ksnd_connd_starting_stamp +
2179 SOCKNAL_CONND_TIMEOUT - sec);
2181 *timeout = (val > 0) ? cfs_time_seconds(val) :
2182 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT);
2186 /* no creating in past 120 seconds */
2188 return ksocknal_data.ksnd_connd_running >
2189 ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV;
2192 /* Go through connd_routes queue looking for a route that we can process
2193 * right now, @timeout_p can be updated if we need to come back later */
2194 static struct ksock_route *
2195 ksocknal_connd_get_route_locked(signed long *timeout_p)
2197 time64_t now = ktime_get_seconds();
2198 struct ksock_route *route;
2200 /* connd_routes can contain both pending and ordinary routes */
2201 list_for_each_entry(route, &ksocknal_data.ksnd_connd_routes,
2204 if (route->ksnr_retry_interval == 0 ||
2205 now >= route->ksnr_timeout)
2208 if (*timeout_p == MAX_SCHEDULE_TIMEOUT ||
2209 *timeout_p > cfs_time_seconds(route->ksnr_timeout - now))
2210 *timeout_p = cfs_time_seconds(route->ksnr_timeout - now);
2217 ksocknal_connd(void *arg)
2219 spinlock_t *connd_lock = &ksocknal_data.ksnd_connd_lock;
2220 struct ksock_connreq *cr;
2221 wait_queue_entry_t wait;
2225 cfs_block_allsigs();
2227 init_waitqueue_entry(&wait, current);
2229 spin_lock_bh(connd_lock);
2231 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2232 ksocknal_data.ksnd_connd_starting--;
2233 ksocknal_data.ksnd_connd_running++;
2235 while (!ksocknal_data.ksnd_shuttingdown) {
2236 struct ksock_route *route = NULL;
2237 time64_t sec = ktime_get_real_seconds();
2238 long timeout = MAX_SCHEDULE_TIMEOUT;
2239 int dropped_lock = 0;
2241 if (ksocknal_connd_check_stop(sec, &timeout)) {
2242 /* wakeup another one to check stop */
2243 wake_up(&ksocknal_data.ksnd_connd_waitq);
2247 if (ksocknal_connd_check_start(sec, &timeout)) {
2248 /* created new thread */
2252 if (!list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2253 /* Connection accepted by the listener */
2254 cr = list_entry(ksocknal_data.ksnd_connd_connreqs.next,
2255 struct ksock_connreq, ksncr_list);
2257 list_del(&cr->ksncr_list);
2258 spin_unlock_bh(connd_lock);
2261 ksocknal_create_conn(cr->ksncr_ni, NULL,
2262 cr->ksncr_sock, SOCKLND_CONN_NONE);
2263 lnet_ni_decref(cr->ksncr_ni);
2264 LIBCFS_FREE(cr, sizeof(*cr));
2266 spin_lock_bh(connd_lock);
2269 /* Only handle an outgoing connection request if there
2270 * is a thread left to handle incoming connections and
2271 * create new connd */
2272 if (ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV <
2273 ksocknal_data.ksnd_connd_running) {
2274 route = ksocknal_connd_get_route_locked(&timeout);
2276 if (route != NULL) {
2277 list_del(&route->ksnr_connd_list);
2278 ksocknal_data.ksnd_connd_connecting++;
2279 spin_unlock_bh(connd_lock);
2282 if (ksocknal_connect(route)) {
2283 /* consecutive retry */
2284 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2285 CWARN("massive consecutive "
2286 "re-connecting to %pI4h\n",
2287 &route->ksnr_ipaddr);
2294 ksocknal_route_decref(route);
2296 spin_lock_bh(connd_lock);
2297 ksocknal_data.ksnd_connd_connecting--;
2301 if (++nloops < SOCKNAL_RESCHED)
2303 spin_unlock_bh(connd_lock);
2306 spin_lock_bh(connd_lock);
2310 /* Nothing to do for 'timeout' */
2311 set_current_state(TASK_INTERRUPTIBLE);
2312 add_wait_queue_exclusive(&ksocknal_data.ksnd_connd_waitq, &wait);
2313 spin_unlock_bh(connd_lock);
2316 schedule_timeout(timeout);
2318 set_current_state(TASK_RUNNING);
2319 remove_wait_queue(&ksocknal_data.ksnd_connd_waitq, &wait);
2320 spin_lock_bh(connd_lock);
2322 ksocknal_data.ksnd_connd_running--;
2323 spin_unlock_bh(connd_lock);
2325 ksocknal_thread_fini();
2329 static struct ksock_conn *
2330 ksocknal_find_timed_out_conn(struct ksock_peer_ni *peer_ni)
2332 /* We're called with a shared lock on ksnd_global_lock */
2333 struct ksock_conn *conn;
2334 struct list_head *ctmp;
2335 struct ksock_tx *tx;
2337 list_for_each(ctmp, &peer_ni->ksnp_conns) {
2340 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
2342 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2343 LASSERT (!conn->ksnc_closing);
2345 error = conn->ksnc_sock->sk->sk_err;
2347 ksocknal_conn_addref(conn);
2351 CNETERR("A connection with %s "
2352 "(%pI4h:%d) was reset; "
2353 "it may have rebooted.\n",
2354 libcfs_id2str(peer_ni->ksnp_id),
2359 CNETERR("A connection with %s "
2360 "(%pI4h:%d) timed out; the "
2361 "network or node may be down.\n",
2362 libcfs_id2str(peer_ni->ksnp_id),
2367 CNETERR("An unexpected network error %d "
2369 "(%pI4h:%d\n", error,
2370 libcfs_id2str(peer_ni->ksnp_id),
2379 if (conn->ksnc_rx_started &&
2380 ktime_get_seconds() >= conn->ksnc_rx_deadline) {
2381 /* Timed out incomplete incoming message */
2382 ksocknal_conn_addref(conn);
2383 CNETERR("Timeout receiving from %s (%pI4h:%d), "
2384 "state %d wanted %d left %d\n",
2385 libcfs_id2str(peer_ni->ksnp_id),
2388 conn->ksnc_rx_state,
2389 conn->ksnc_rx_nob_wanted,
2390 conn->ksnc_rx_nob_left);
2394 if ((!list_empty(&conn->ksnc_tx_queue) ||
2395 conn->ksnc_sock->sk->sk_wmem_queued != 0) &&
2396 ktime_get_seconds() >= conn->ksnc_tx_deadline) {
2397 /* Timed out messages queued for sending or
2398 * buffered in the socket's send buffer */
2399 ksocknal_conn_addref(conn);
2400 list_for_each_entry(tx, &conn->ksnc_tx_queue,
2403 LNET_MSG_STATUS_LOCAL_TIMEOUT;
2404 CNETERR("Timeout sending data to %s (%pI4h:%d) "
2405 "the network or that node may be down.\n",
2406 libcfs_id2str(peer_ni->ksnp_id),
2407 &conn->ksnc_ipaddr, conn->ksnc_port);
2416 ksocknal_flush_stale_txs(struct ksock_peer_ni *peer_ni)
2418 struct ksock_tx *tx;
2419 LIST_HEAD(stale_txs);
2421 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2423 while (!list_empty(&peer_ni->ksnp_tx_queue)) {
2424 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2425 struct ksock_tx, tx_list);
2427 if (ktime_get_seconds() < tx->tx_deadline)
2430 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2432 list_move_tail(&tx->tx_list, &stale_txs);
2435 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2437 ksocknal_txlist_done(peer_ni->ksnp_ni, &stale_txs, -ETIMEDOUT);
2441 ksocknal_send_keepalive_locked(struct ksock_peer_ni *peer_ni)
2442 __must_hold(&ksocknal_data.ksnd_global_lock)
2444 struct ksock_sched *sched;
2445 struct ksock_conn *conn;
2446 struct ksock_tx *tx;
2448 /* last_alive will be updated by create_conn */
2449 if (list_empty(&peer_ni->ksnp_conns))
2452 if (peer_ni->ksnp_proto != &ksocknal_protocol_v3x)
2455 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2456 ktime_get_seconds() < peer_ni->ksnp_last_alive +
2457 *ksocknal_tunables.ksnd_keepalive)
2460 if (ktime_get_seconds() < peer_ni->ksnp_send_keepalive)
2463 /* retry 10 secs later, so we wouldn't put pressure
2464 * on this peer_ni if we failed to send keepalive this time */
2465 peer_ni->ksnp_send_keepalive = ktime_get_seconds() + 10;
2467 conn = ksocknal_find_conn_locked(peer_ni, NULL, 1);
2469 sched = conn->ksnc_scheduler;
2471 spin_lock_bh(&sched->kss_lock);
2472 if (!list_empty(&conn->ksnc_tx_queue)) {
2473 spin_unlock_bh(&sched->kss_lock);
2474 /* there is an queued ACK, don't need keepalive */
2478 spin_unlock_bh(&sched->kss_lock);
2481 read_unlock(&ksocknal_data.ksnd_global_lock);
2483 /* cookie = 1 is reserved for keepalive PING */
2484 tx = ksocknal_alloc_tx_noop(1, 1);
2486 read_lock(&ksocknal_data.ksnd_global_lock);
2490 if (ksocknal_launch_packet(peer_ni->ksnp_ni, tx, peer_ni->ksnp_id) == 0) {
2491 read_lock(&ksocknal_data.ksnd_global_lock);
2495 ksocknal_free_tx(tx);
2496 read_lock(&ksocknal_data.ksnd_global_lock);
2503 ksocknal_check_peer_timeouts(int idx)
2505 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2506 struct ksock_peer_ni *peer_ni;
2507 struct ksock_conn *conn;
2508 struct ksock_tx *tx;
2511 /* NB. We expect to have a look at all the peers and not find any
2512 * connections to time out, so we just use a shared lock while we
2514 read_lock(&ksocknal_data.ksnd_global_lock);
2516 list_for_each_entry(peer_ni, peers, ksnp_list) {
2517 struct ksock_tx *tx_stale;
2518 time64_t deadline = 0;
2522 if (ksocknal_send_keepalive_locked(peer_ni) != 0) {
2523 read_unlock(&ksocknal_data.ksnd_global_lock);
2527 conn = ksocknal_find_timed_out_conn (peer_ni);
2530 read_unlock(&ksocknal_data.ksnd_global_lock);
2532 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2534 /* NB we won't find this one again, but we can't
2535 * just proceed with the next peer_ni, since we dropped
2536 * ksnd_global_lock and it might be dead already! */
2537 ksocknal_conn_decref(conn);
2541 /* we can't process stale txs right here because we're
2542 * holding only shared lock */
2543 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
2544 struct ksock_tx *tx;
2546 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2547 struct ksock_tx, tx_list);
2548 if (ktime_get_seconds() >= tx->tx_deadline) {
2549 ksocknal_peer_addref(peer_ni);
2550 read_unlock(&ksocknal_data.ksnd_global_lock);
2552 ksocknal_flush_stale_txs(peer_ni);
2554 ksocknal_peer_decref(peer_ni);
2559 if (list_empty(&peer_ni->ksnp_zc_req_list))
2563 spin_lock(&peer_ni->ksnp_lock);
2564 list_for_each_entry(tx, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
2565 if (ktime_get_seconds() < tx->tx_deadline)
2567 /* ignore the TX if connection is being closed */
2568 if (tx->tx_conn->ksnc_closing)
2571 if (tx_stale == NULL)
2575 if (tx_stale == NULL) {
2576 spin_unlock(&peer_ni->ksnp_lock);
2580 deadline = tx_stale->tx_deadline;
2581 resid = tx_stale->tx_resid;
2582 conn = tx_stale->tx_conn;
2583 ksocknal_conn_addref(conn);
2585 spin_unlock(&peer_ni->ksnp_lock);
2586 read_unlock(&ksocknal_data.ksnd_global_lock);
2588 CERROR("Total %d stale ZC_REQs for peer_ni %s detected; the "
2589 "oldest(%p) timed out %lld secs ago, "
2590 "resid: %d, wmem: %d\n",
2591 n, libcfs_nid2str(peer_ni->ksnp_id.nid), tx_stale,
2592 ktime_get_seconds() - deadline,
2593 resid, conn->ksnc_sock->sk->sk_wmem_queued);
2595 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2596 ksocknal_conn_decref(conn);
2600 read_unlock(&ksocknal_data.ksnd_global_lock);
2603 int ksocknal_reaper(void *arg)
2605 wait_queue_entry_t wait;
2606 struct ksock_conn *conn;
2607 struct ksock_sched *sched;
2608 struct list_head enomem_conns;
2613 time64_t deadline = ktime_get_seconds();
2615 cfs_block_allsigs ();
2617 INIT_LIST_HEAD(&enomem_conns);
2618 init_waitqueue_entry(&wait, current);
2620 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2622 while (!ksocknal_data.ksnd_shuttingdown) {
2623 if (!list_empty(&ksocknal_data.ksnd_deathrow_conns)) {
2624 conn = list_entry(ksocknal_data.ksnd_deathrow_conns.next,
2625 struct ksock_conn, ksnc_list);
2626 list_del(&conn->ksnc_list);
2628 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2630 ksocknal_terminate_conn(conn);
2631 ksocknal_conn_decref(conn);
2633 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2637 if (!list_empty(&ksocknal_data.ksnd_zombie_conns)) {
2638 conn = list_entry(ksocknal_data.ksnd_zombie_conns.next,
2639 struct ksock_conn, ksnc_list);
2640 list_del(&conn->ksnc_list);
2642 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2644 ksocknal_destroy_conn(conn);
2646 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2650 if (!list_empty(&ksocknal_data.ksnd_enomem_conns)) {
2651 list_add(&enomem_conns,
2652 &ksocknal_data.ksnd_enomem_conns);
2653 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2656 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2658 /* reschedule all the connections that stalled with ENOMEM... */
2660 while (!list_empty(&enomem_conns)) {
2661 conn = list_entry(enomem_conns.next,
2662 struct ksock_conn, ksnc_tx_list);
2663 list_del(&conn->ksnc_tx_list);
2665 sched = conn->ksnc_scheduler;
2667 spin_lock_bh(&sched->kss_lock);
2669 LASSERT(conn->ksnc_tx_scheduled);
2670 conn->ksnc_tx_ready = 1;
2671 list_add_tail(&conn->ksnc_tx_list,
2672 &sched->kss_tx_conns);
2673 wake_up(&sched->kss_waitq);
2675 spin_unlock_bh(&sched->kss_lock);
2679 /* careful with the jiffy wrap... */
2680 while ((timeout = deadline - ktime_get_seconds()) <= 0) {
2683 int chunk = ksocknal_data.ksnd_peer_hash_size;
2684 unsigned int lnd_timeout;
2686 /* Time to check for timeouts on a few more peers: I do
2687 * checks every 'p' seconds on a proportion of the peer_ni
2688 * table and I need to check every connection 'n' times
2689 * within a timeout interval, to ensure I detect a
2690 * timeout on any connection within (n+1)/n times the
2691 * timeout interval. */
2693 lnd_timeout = lnet_get_lnd_timeout();
2694 if (lnd_timeout > n * p)
2695 chunk = (chunk * n * p) / lnd_timeout;
2699 for (i = 0; i < chunk; i++) {
2700 ksocknal_check_peer_timeouts (peer_index);
2701 peer_index = (peer_index + 1) %
2702 ksocknal_data.ksnd_peer_hash_size;
2708 if (nenomem_conns != 0) {
2709 /* Reduce my timeout if I rescheduled ENOMEM conns.
2710 * This also prevents me getting woken immediately
2711 * if any go back on my enomem list. */
2712 timeout = SOCKNAL_ENOMEM_RETRY;
2714 ksocknal_data.ksnd_reaper_waketime = ktime_get_seconds() +
2717 set_current_state(TASK_INTERRUPTIBLE);
2718 add_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2720 if (!ksocknal_data.ksnd_shuttingdown &&
2721 list_empty(&ksocknal_data.ksnd_deathrow_conns) &&
2722 list_empty(&ksocknal_data.ksnd_zombie_conns))
2723 schedule_timeout(cfs_time_seconds(timeout));
2725 set_current_state(TASK_RUNNING);
2726 remove_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2728 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2731 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2733 ksocknal_thread_fini();