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 schedule_timeout_uninterruptible(
199 cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
201 LASSERT(tx->tx_resid != 0);
203 rc = ksocknal_connsock_addref(conn);
205 LASSERT(conn->ksnc_closing);
210 if (ksocknal_data.ksnd_enomem_tx > 0) {
212 ksocknal_data.ksnd_enomem_tx--;
214 } else if (tx->tx_niov != 0) {
215 rc = ksocknal_send_iov(conn, tx, scratch_iov);
217 rc = ksocknal_send_kiov(conn, tx, scratch_iov);
220 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
221 if (rc > 0) /* sent something? */
222 conn->ksnc_tx_bufnob += rc; /* account it */
224 if (bufnob < conn->ksnc_tx_bufnob) {
225 /* allocated send buffer bytes < computed; infer
226 * something got ACKed */
227 conn->ksnc_tx_deadline = ktime_get_seconds() +
228 lnet_get_lnd_timeout();
229 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
230 conn->ksnc_tx_bufnob = bufnob;
234 if (rc <= 0) { /* Didn't write anything? */
235 /* some stacks return 0 instead of -EAGAIN */
239 /* Check if EAGAIN is due to memory pressure */
240 if (rc == -EAGAIN && ksocknal_lib_memory_pressure(conn))
246 /* socket's wmem_queued now includes 'rc' bytes */
247 atomic_sub (rc, &conn->ksnc_tx_nob);
250 } while (tx->tx_resid != 0);
252 ksocknal_connsock_decref(conn);
257 ksocknal_recv_iov(struct ksock_conn *conn, struct kvec *scratchiov)
259 struct kvec *iov = conn->ksnc_rx_iov;
263 LASSERT(conn->ksnc_rx_niov > 0);
265 /* Never touch conn->ksnc_rx_iov or change connection
266 * status inside ksocknal_lib_recv_iov */
267 rc = ksocknal_lib_recv_iov(conn, scratchiov);
272 /* received something... */
275 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
276 conn->ksnc_rx_deadline = ktime_get_seconds() +
277 lnet_get_lnd_timeout();
278 smp_mb(); /* order with setting rx_started */
279 conn->ksnc_rx_started = 1;
281 conn->ksnc_rx_nob_wanted -= nob;
282 conn->ksnc_rx_nob_left -= nob;
285 LASSERT(conn->ksnc_rx_niov > 0);
287 if (nob < (int)iov->iov_len) {
289 iov->iov_base += nob;
294 conn->ksnc_rx_iov = ++iov;
295 conn->ksnc_rx_niov--;
302 ksocknal_recv_kiov(struct ksock_conn *conn, struct page **rx_scratch_pgs,
303 struct kvec *scratch_iov)
305 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
308 LASSERT(conn->ksnc_rx_nkiov > 0);
310 /* Never touch conn->ksnc_rx_kiov or change connection
311 * status inside ksocknal_lib_recv_iov */
312 rc = ksocknal_lib_recv_kiov(conn, rx_scratch_pgs, scratch_iov);
317 /* received something... */
320 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
321 conn->ksnc_rx_deadline = ktime_get_seconds() +
322 lnet_get_lnd_timeout();
323 smp_mb(); /* order with setting rx_started */
324 conn->ksnc_rx_started = 1;
326 conn->ksnc_rx_nob_wanted -= nob;
327 conn->ksnc_rx_nob_left -= nob;
330 LASSERT(conn->ksnc_rx_nkiov > 0);
332 if (nob < (int) kiov->kiov_len) {
333 kiov->kiov_offset += nob;
334 kiov->kiov_len -= nob;
338 nob -= kiov->kiov_len;
339 conn->ksnc_rx_kiov = ++kiov;
340 conn->ksnc_rx_nkiov--;
347 ksocknal_receive(struct ksock_conn *conn, struct page **rx_scratch_pgs,
348 struct kvec *scratch_iov)
350 /* Return 1 on success, 0 on EOF, < 0 on error.
351 * Caller checks ksnc_rx_nob_wanted to determine
352 * progress/completion. */
356 if (ksocknal_data.ksnd_stall_rx != 0)
357 schedule_timeout_uninterruptible(
358 cfs_time_seconds(ksocknal_data.ksnd_stall_rx));
360 rc = ksocknal_connsock_addref(conn);
362 LASSERT(conn->ksnc_closing);
367 if (conn->ksnc_rx_niov != 0)
368 rc = ksocknal_recv_iov(conn, scratch_iov);
370 rc = ksocknal_recv_kiov(conn, rx_scratch_pgs,
374 /* error/EOF or partial receive */
377 } else if (rc == 0 && conn->ksnc_rx_started) {
378 /* EOF in the middle of a message */
384 /* Completed a fragment */
386 if (conn->ksnc_rx_nob_wanted == 0) {
392 ksocknal_connsock_decref(conn);
397 ksocknal_tx_done(struct lnet_ni *ni, struct ksock_tx *tx, int rc)
399 struct lnet_msg *lnetmsg = tx->tx_lnetmsg;
400 enum lnet_msg_hstatus hstatus = tx->tx_hstatus;
403 LASSERT(ni != NULL || tx->tx_conn != NULL);
405 if (!rc && (tx->tx_resid != 0 || tx->tx_zc_aborted)) {
407 if (hstatus == LNET_MSG_STATUS_OK)
408 hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
411 if (tx->tx_conn != NULL)
412 ksocknal_conn_decref(tx->tx_conn);
414 ksocknal_free_tx(tx);
415 if (lnetmsg != NULL) { /* KSOCK_MSG_NOOP go without lnetmsg */
416 lnetmsg->msg_health_status = hstatus;
417 lnet_finalize(lnetmsg, rc);
424 ksocknal_txlist_done(struct lnet_ni *ni, struct list_head *txlist, int error)
428 while (!list_empty(txlist)) {
429 tx = list_entry(txlist->next, struct ksock_tx, tx_list);
431 if (error && tx->tx_lnetmsg != NULL) {
432 CNETERR("Deleting packet type %d len %d %s->%s\n",
433 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.type),
434 le32_to_cpu(tx->tx_lnetmsg->msg_hdr.payload_length),
435 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
436 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
438 CNETERR("Deleting noop packet\n");
441 list_del(&tx->tx_list);
443 if (tx->tx_hstatus == LNET_MSG_STATUS_OK) {
444 if (error == -ETIMEDOUT)
446 LNET_MSG_STATUS_LOCAL_TIMEOUT;
447 else if (error == -ENETDOWN ||
448 error == -EHOSTUNREACH ||
449 error == -ENETUNREACH ||
450 error == -ECONNREFUSED ||
451 error == -ECONNRESET)
452 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_DROPPED;
454 * for all other errors we don't want to
458 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
461 LASSERT(atomic_read(&tx->tx_refcount) == 1);
462 ksocknal_tx_done(ni, tx, error);
467 ksocknal_check_zc_req(struct ksock_tx *tx)
469 struct ksock_conn *conn = tx->tx_conn;
470 struct ksock_peer_ni *peer_ni = conn->ksnc_peer;
472 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
473 * to ksnp_zc_req_list if some fragment of this message should be sent
474 * zero-copy. Our peer_ni will send an ACK containing this cookie when
475 * she has received this message to tell us we can signal completion.
476 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
477 * ksnp_zc_req_list. */
478 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
479 LASSERT (tx->tx_zc_capable);
481 tx->tx_zc_checked = 1;
483 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
484 !conn->ksnc_zc_capable)
487 /* assign cookie and queue tx to pending list, it will be released when
488 * a matching ack is received. See ksocknal_handle_zcack() */
490 ksocknal_tx_addref(tx);
492 spin_lock(&peer_ni->ksnp_lock);
494 /* ZC_REQ is going to be pinned to the peer_ni */
495 tx->tx_deadline = ktime_get_seconds() +
496 lnet_get_lnd_timeout();
498 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
500 tx->tx_msg.ksm_zc_cookies[0] = peer_ni->ksnp_zc_next_cookie++;
502 if (peer_ni->ksnp_zc_next_cookie == 0)
503 peer_ni->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
505 list_add_tail(&tx->tx_zc_list, &peer_ni->ksnp_zc_req_list);
507 spin_unlock(&peer_ni->ksnp_lock);
511 ksocknal_uncheck_zc_req(struct ksock_tx *tx)
513 struct ksock_peer_ni *peer_ni = tx->tx_conn->ksnc_peer;
515 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
516 LASSERT(tx->tx_zc_capable);
518 tx->tx_zc_checked = 0;
520 spin_lock(&peer_ni->ksnp_lock);
522 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
523 /* Not waiting for an ACK */
524 spin_unlock(&peer_ni->ksnp_lock);
528 tx->tx_msg.ksm_zc_cookies[0] = 0;
529 list_del(&tx->tx_zc_list);
531 spin_unlock(&peer_ni->ksnp_lock);
533 ksocknal_tx_decref(tx);
537 ksocknal_process_transmit(struct ksock_conn *conn, struct ksock_tx *tx,
538 struct kvec *scratch_iov)
541 bool error_sim = false;
543 if (lnet_send_error_simulation(tx->tx_lnetmsg, &tx->tx_hstatus)) {
549 if (tx->tx_zc_capable && !tx->tx_zc_checked)
550 ksocknal_check_zc_req(tx);
552 rc = ksocknal_transmit(conn, tx, scratch_iov);
554 CDEBUG(D_NET, "send(%d) %d\n", tx->tx_resid, rc);
556 if (tx->tx_resid == 0) {
557 /* Sent everything OK */
569 counter++; /* exponential backoff warnings */
570 if ((counter & (-counter)) == counter)
571 CWARN("%u ENOMEM tx %p (%u allocated)\n",
572 counter, conn, atomic_read(&libcfs_kmemory));
574 /* Queue on ksnd_enomem_conns for retry after a timeout */
575 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
577 /* enomem list takes over scheduler's ref... */
578 LASSERT(conn->ksnc_tx_scheduled);
579 list_add_tail(&conn->ksnc_tx_list,
580 &ksocknal_data.ksnd_enomem_conns);
581 if (ktime_get_seconds() + SOCKNAL_ENOMEM_RETRY <
582 ksocknal_data.ksnd_reaper_waketime)
583 wake_up(&ksocknal_data.ksnd_reaper_waitq);
585 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
588 * set the health status of the message which determines
589 * whether we should retry the transmit
591 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
602 * set the health status of the message which determines
603 * whether we should retry the transmit
605 if (rc == -ETIMEDOUT)
606 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_TIMEOUT;
608 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_ERROR;
611 if (!conn->ksnc_closing) {
614 LCONSOLE_WARN("Host %pI4h reset our connection "
615 "while we were sending data; it may have "
620 LCONSOLE_WARN("There was an unexpected network error "
621 "while writing to %pI4h: %d.\n",
622 &conn->ksnc_ipaddr, rc);
625 CDEBUG(D_NET, "[%p] Error %d on write to %s ip %pI4h:%d\n",
626 conn, rc, libcfs_id2str(conn->ksnc_peer->ksnp_id),
627 &conn->ksnc_ipaddr, conn->ksnc_port);
630 if (tx->tx_zc_checked)
631 ksocknal_uncheck_zc_req(tx);
633 /* it's not an error if conn is being closed */
634 ksocknal_close_conn_and_siblings(conn,
635 (conn->ksnc_closing) ? 0 : rc);
641 ksocknal_launch_connection_locked(struct ksock_route *route)
644 /* called holding write lock on ksnd_global_lock */
646 LASSERT (!route->ksnr_scheduled);
647 LASSERT (!route->ksnr_connecting);
648 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
650 route->ksnr_scheduled = 1; /* scheduling conn for connd */
651 ksocknal_route_addref(route); /* extra ref for connd */
653 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
655 list_add_tail(&route->ksnr_connd_list,
656 &ksocknal_data.ksnd_connd_routes);
657 wake_up(&ksocknal_data.ksnd_connd_waitq);
659 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
663 ksocknal_launch_all_connections_locked(struct ksock_peer_ni *peer_ni)
665 struct ksock_route *route;
667 /* called holding write lock on ksnd_global_lock */
669 /* launch any/all connections that need it */
670 route = ksocknal_find_connectable_route_locked(peer_ni);
674 ksocknal_launch_connection_locked(route);
679 ksocknal_find_conn_locked(struct ksock_peer_ni *peer_ni, struct ksock_tx *tx, int nonblk)
681 struct list_head *tmp;
682 struct ksock_conn *conn;
683 struct ksock_conn *typed = NULL;
684 struct ksock_conn *fallback = NULL;
688 list_for_each(tmp, &peer_ni->ksnp_conns) {
689 struct ksock_conn *c = list_entry(tmp, struct ksock_conn,
691 int nob = atomic_read(&c->ksnc_tx_nob) +
692 c->ksnc_sock->sk->sk_wmem_queued;
695 LASSERT (!c->ksnc_closing);
696 LASSERT (c->ksnc_proto != NULL &&
697 c->ksnc_proto->pro_match_tx != NULL);
699 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
704 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
707 case SOCKNAL_MATCH_YES: /* typed connection */
708 if (typed == NULL || tnob > nob ||
709 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
710 typed->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
716 case SOCKNAL_MATCH_MAY: /* fallback connection */
717 if (fallback == NULL || fnob > nob ||
718 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
719 fallback->ksnc_tx_last_post > c->ksnc_tx_last_post)) {
727 /* prefer the typed selection */
728 conn = (typed != NULL) ? typed : fallback;
731 conn->ksnc_tx_last_post = ktime_get_seconds();
737 ksocknal_tx_prep(struct ksock_conn *conn, struct ksock_tx *tx)
739 conn->ksnc_proto->pro_pack(tx);
741 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
742 ksocknal_conn_addref(conn); /* +1 ref for tx */
747 ksocknal_queue_tx_locked(struct ksock_tx *tx, struct ksock_conn *conn)
749 struct ksock_sched *sched = conn->ksnc_scheduler;
750 struct ksock_msg *msg = &tx->tx_msg;
751 struct ksock_tx *ztx = NULL;
754 /* called holding global lock (read or irq-write) and caller may
755 * not have dropped this lock between finding conn and calling me,
756 * so we don't need the {get,put}connsock dance to deref
758 LASSERT(!conn->ksnc_closing);
760 CDEBUG(D_NET, "Sending to %s ip %pI4h:%d\n",
761 libcfs_id2str(conn->ksnc_peer->ksnp_id),
762 &conn->ksnc_ipaddr, conn->ksnc_port);
764 ksocknal_tx_prep(conn, tx);
766 /* Ensure the frags we've been given EXACTLY match the number of
767 * bytes we want to send. Many TCP/IP stacks disregard any total
768 * size parameters passed to them and just look at the frags.
770 * We always expect at least 1 mapped fragment containing the
771 * complete ksocknal message header. */
772 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
773 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
774 (unsigned int)tx->tx_nob);
775 LASSERT (tx->tx_niov >= 1);
776 LASSERT (tx->tx_resid == tx->tx_nob);
778 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
779 tx, (tx->tx_lnetmsg != NULL) ? tx->tx_lnetmsg->msg_hdr.type:
781 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
783 bufnob = conn->ksnc_sock->sk->sk_wmem_queued;
784 spin_lock_bh(&sched->kss_lock);
786 if (list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
787 /* First packet starts the timeout */
788 conn->ksnc_tx_deadline = ktime_get_seconds() +
789 lnet_get_lnd_timeout();
790 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
791 conn->ksnc_peer->ksnp_last_alive = ktime_get_seconds();
792 conn->ksnc_tx_bufnob = 0;
793 smp_mb(); /* order with adding to tx_queue */
796 if (msg->ksm_type == KSOCK_MSG_NOOP) {
797 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
798 * on a normal packet so I don't need to send it */
799 LASSERT (msg->ksm_zc_cookies[1] != 0);
800 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
802 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
803 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
806 /* It's a normal packet - can it piggback a noop zc-ack that
807 * has been queued already? */
808 LASSERT (msg->ksm_zc_cookies[1] == 0);
809 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
811 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
812 /* ztx will be released later */
816 atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
817 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
820 if (conn->ksnc_tx_ready && /* able to send */
821 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
822 /* +1 ref for scheduler */
823 ksocknal_conn_addref(conn);
824 list_add_tail(&conn->ksnc_tx_list,
825 &sched->kss_tx_conns);
826 conn->ksnc_tx_scheduled = 1;
827 wake_up(&sched->kss_waitq);
830 spin_unlock_bh(&sched->kss_lock);
835 ksocknal_find_connectable_route_locked(struct ksock_peer_ni *peer_ni)
837 time64_t now = ktime_get_seconds();
838 struct list_head *tmp;
839 struct ksock_route *route;
841 list_for_each(tmp, &peer_ni->ksnp_routes) {
842 route = list_entry(tmp, struct ksock_route, ksnr_list);
844 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
846 if (route->ksnr_scheduled) /* connections being established */
849 /* all route types connected ? */
850 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
853 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
854 now >= route->ksnr_timeout)) {
856 "Too soon to retry route %pI4h "
857 "(cnted %d, interval %lld, %lld secs later)\n",
859 route->ksnr_connected,
860 route->ksnr_retry_interval,
861 route->ksnr_timeout - now);
872 ksocknal_find_connecting_route_locked(struct ksock_peer_ni *peer_ni)
874 struct list_head *tmp;
875 struct ksock_route *route;
877 list_for_each(tmp, &peer_ni->ksnp_routes) {
878 route = list_entry(tmp, struct ksock_route, ksnr_list);
880 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
882 if (route->ksnr_scheduled)
890 ksocknal_launch_packet(struct lnet_ni *ni, struct ksock_tx *tx,
891 struct lnet_process_id id)
893 struct ksock_peer_ni *peer_ni;
894 struct ksock_conn *conn;
899 LASSERT (tx->tx_conn == NULL);
901 g_lock = &ksocknal_data.ksnd_global_lock;
903 for (retry = 0;; retry = 1) {
905 peer_ni = ksocknal_find_peer_locked(ni, id);
906 if (peer_ni != NULL) {
907 if (ksocknal_find_connectable_route_locked(peer_ni) == NULL) {
908 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
910 /* I've got no routes that need to be
911 * connecting and I do have an actual
913 ksocknal_queue_tx_locked (tx, conn);
920 /* I'll need a write lock... */
923 write_lock_bh(g_lock);
925 peer_ni = ksocknal_find_peer_locked(ni, id);
929 write_unlock_bh(g_lock);
931 if ((id.pid & LNET_PID_USERFLAG) != 0) {
932 CERROR("Refusing to create a connection to "
933 "userspace process %s\n", libcfs_id2str(id));
934 return -EHOSTUNREACH;
938 CERROR("Can't find peer_ni %s\n", libcfs_id2str(id));
939 return -EHOSTUNREACH;
942 rc = ksocknal_add_peer(ni, id,
943 LNET_NIDADDR(id.nid),
944 lnet_acceptor_port());
946 CERROR("Can't add peer_ni %s: %d\n",
947 libcfs_id2str(id), rc);
952 ksocknal_launch_all_connections_locked(peer_ni);
954 conn = ksocknal_find_conn_locked(peer_ni, tx, tx->tx_nonblk);
956 /* Connection exists; queue message on it */
957 ksocknal_queue_tx_locked (tx, conn);
958 write_unlock_bh(g_lock);
962 if (peer_ni->ksnp_accepting > 0 ||
963 ksocknal_find_connecting_route_locked (peer_ni) != NULL) {
964 /* the message is going to be pinned to the peer_ni */
965 tx->tx_deadline = ktime_get_seconds() +
966 lnet_get_lnd_timeout();
968 /* Queue the message until a connection is established */
969 list_add_tail(&tx->tx_list, &peer_ni->ksnp_tx_queue);
970 write_unlock_bh(g_lock);
974 write_unlock_bh(g_lock);
976 /* NB Routes may be ignored if connections to them failed recently */
977 CNETERR("No usable routes to %s\n", libcfs_id2str(id));
978 tx->tx_hstatus = LNET_MSG_STATUS_REMOTE_ERROR;
979 return (-EHOSTUNREACH);
983 ksocknal_send(struct lnet_ni *ni, void *private, struct lnet_msg *lntmsg)
986 int type = lntmsg->msg_type;
987 struct lnet_process_id target = lntmsg->msg_target;
988 unsigned int payload_niov = lntmsg->msg_niov;
989 struct kvec *payload_iov = lntmsg->msg_iov;
990 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
991 unsigned int payload_offset = lntmsg->msg_offset;
992 unsigned int payload_nob = lntmsg->msg_len;
997 /* NB 'private' is different depending on what we're sending.
998 * Just ignore it... */
1000 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
1001 payload_nob, payload_niov, libcfs_id2str(target));
1003 LASSERT (payload_nob == 0 || payload_niov > 0);
1004 LASSERT (payload_niov <= LNET_MAX_IOV);
1005 /* payload is either all vaddrs or all pages */
1006 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1007 LASSERT (!in_interrupt ());
1009 if (payload_iov != NULL)
1010 desc_size = offsetof(struct ksock_tx,
1011 tx_frags.virt.iov[1 + payload_niov]);
1013 desc_size = offsetof(struct ksock_tx,
1014 tx_frags.paged.kiov[payload_niov]);
1016 if (lntmsg->msg_vmflush)
1017 mpflag = cfs_memory_pressure_get_and_set();
1018 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
1020 CERROR("Can't allocate tx desc type %d size %d\n",
1022 if (lntmsg->msg_vmflush)
1023 cfs_memory_pressure_restore(mpflag);
1027 tx->tx_conn = NULL; /* set when assigned a conn */
1028 tx->tx_lnetmsg = lntmsg;
1030 if (payload_iov != NULL) {
1033 tx->tx_iov = tx->tx_frags.virt.iov;
1035 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
1036 payload_niov, payload_iov,
1037 payload_offset, payload_nob);
1040 tx->tx_iov = &tx->tx_frags.paged.iov;
1041 tx->tx_kiov = tx->tx_frags.paged.kiov;
1042 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
1043 payload_niov, payload_kiov,
1044 payload_offset, payload_nob);
1046 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
1047 tx->tx_zc_capable = 1;
1050 tx->tx_msg.ksm_csum = 0;
1051 tx->tx_msg.ksm_type = KSOCK_MSG_LNET;
1052 tx->tx_msg.ksm_zc_cookies[0] = 0;
1053 tx->tx_msg.ksm_zc_cookies[1] = 0;
1055 /* The first fragment will be set later in pro_pack */
1056 rc = ksocknal_launch_packet(ni, tx, target);
1058 cfs_memory_pressure_restore(mpflag);
1063 lntmsg->msg_health_status = tx->tx_hstatus;
1064 ksocknal_free_tx(tx);
1069 ksocknal_thread_start(int (*fn)(void *arg), void *arg, char *name)
1071 struct task_struct *task = kthread_run(fn, arg, name);
1074 return PTR_ERR(task);
1076 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1077 ksocknal_data.ksnd_nthreads++;
1078 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1083 ksocknal_thread_fini (void)
1085 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1086 ksocknal_data.ksnd_nthreads--;
1087 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1091 ksocknal_new_packet(struct ksock_conn *conn, int nob_to_skip)
1093 static char ksocknal_slop_buffer[4096];
1098 LASSERT(conn->ksnc_proto != NULL);
1100 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1101 /* Remind the socket to ack eagerly... */
1102 ksocknal_lib_eager_ack(conn);
1105 if (nob_to_skip == 0) { /* right at next packet boundary now */
1106 conn->ksnc_rx_started = 0;
1107 smp_mb(); /* racing with timeout thread */
1109 switch (conn->ksnc_proto->pro_version) {
1110 case KSOCK_PROTO_V2:
1111 case KSOCK_PROTO_V3:
1112 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1113 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1114 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1116 conn->ksnc_rx_nob_wanted = offsetof(struct ksock_msg, ksm_u);
1117 conn->ksnc_rx_nob_left = offsetof(struct ksock_msg, ksm_u);
1118 conn->ksnc_rx_iov[0].iov_len = offsetof(struct ksock_msg, ksm_u);
1121 case KSOCK_PROTO_V1:
1122 /* Receiving bare struct lnet_hdr */
1123 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1124 conn->ksnc_rx_nob_wanted = sizeof(struct lnet_hdr);
1125 conn->ksnc_rx_nob_left = sizeof(struct lnet_hdr);
1127 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1128 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1129 conn->ksnc_rx_iov[0].iov_len = sizeof(struct lnet_hdr);
1135 conn->ksnc_rx_niov = 1;
1137 conn->ksnc_rx_kiov = NULL;
1138 conn->ksnc_rx_nkiov = 0;
1139 conn->ksnc_rx_csum = ~0;
1143 /* Set up to skip as much as possible now. If there's more left
1144 * (ran out of iov entries) we'll get called again */
1146 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1147 conn->ksnc_rx_nob_left = nob_to_skip;
1148 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1153 nob = min_t(int, nob_to_skip, sizeof(ksocknal_slop_buffer));
1155 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1156 conn->ksnc_rx_iov[niov].iov_len = nob;
1161 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1162 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof(struct kvec));
1164 conn->ksnc_rx_niov = niov;
1165 conn->ksnc_rx_kiov = NULL;
1166 conn->ksnc_rx_nkiov = 0;
1167 conn->ksnc_rx_nob_wanted = skipped;
1172 ksocknal_process_receive(struct ksock_conn *conn,
1173 struct page **rx_scratch_pgs,
1174 struct kvec *scratch_iov)
1176 struct lnet_hdr *lhdr;
1177 struct lnet_process_id *id;
1180 LASSERT (atomic_read(&conn->ksnc_conn_refcount) > 0);
1182 /* NB: sched lock NOT held */
1183 /* SOCKNAL_RX_LNET_HEADER is here for backward compatibility */
1184 LASSERT(conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1185 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1186 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1187 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1189 if (conn->ksnc_rx_nob_wanted != 0) {
1190 rc = ksocknal_receive(conn, rx_scratch_pgs,
1194 struct lnet_process_id ksnp_id;
1196 ksnp_id = conn->ksnc_peer->ksnp_id;
1198 LASSERT(rc != -EAGAIN);
1200 CDEBUG(D_NET, "[%p] EOF from %s "
1201 "ip %pI4h:%d\n", conn,
1202 libcfs_id2str(ksnp_id),
1205 else if (!conn->ksnc_closing)
1206 CERROR("[%p] Error %d on read from %s "
1207 "ip %pI4h:%d\n", conn, rc,
1208 libcfs_id2str(ksnp_id),
1212 /* it's not an error if conn is being closed */
1213 ksocknal_close_conn_and_siblings (conn,
1214 (conn->ksnc_closing) ? 0 : rc);
1215 return (rc == 0 ? -ESHUTDOWN : rc);
1218 if (conn->ksnc_rx_nob_wanted != 0) {
1223 switch (conn->ksnc_rx_state) {
1224 case SOCKNAL_RX_KSM_HEADER:
1225 if (conn->ksnc_flip) {
1226 __swab32s(&conn->ksnc_msg.ksm_type);
1227 __swab32s(&conn->ksnc_msg.ksm_csum);
1228 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1229 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1232 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1233 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1234 CERROR("%s: Unknown message type: %x\n",
1235 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1236 conn->ksnc_msg.ksm_type);
1237 ksocknal_new_packet(conn, 0);
1238 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1242 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1243 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1244 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1245 /* NOOP Checksum error */
1246 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1247 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1248 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1249 ksocknal_new_packet(conn, 0);
1250 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1254 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1257 LASSERT (conn->ksnc_proto != &ksocknal_protocol_v1x);
1259 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1260 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1262 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1263 conn->ksnc_msg.ksm_zc_cookies[1]);
1266 CERROR("%s: Unknown ZC-ACK cookie: %llu, %llu\n",
1267 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1268 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1269 ksocknal_new_packet(conn, 0);
1270 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1275 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1276 ksocknal_new_packet (conn, 0);
1277 return 0; /* NOOP is done and just return */
1280 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1281 conn->ksnc_rx_nob_wanted = sizeof(struct ksock_lnet_msg);
1282 conn->ksnc_rx_nob_left = sizeof(struct ksock_lnet_msg);
1284 conn->ksnc_rx_iov = (struct kvec *)&conn->ksnc_rx_iov_space;
1285 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1286 conn->ksnc_rx_iov[0].iov_len = sizeof(struct ksock_lnet_msg);
1288 conn->ksnc_rx_niov = 1;
1289 conn->ksnc_rx_kiov = NULL;
1290 conn->ksnc_rx_nkiov = 0;
1292 goto again; /* read lnet header now */
1294 case SOCKNAL_RX_LNET_HEADER:
1295 /* unpack message header */
1296 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1298 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1299 /* Userspace peer_ni */
1300 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1301 id = &conn->ksnc_peer->ksnp_id;
1303 /* Substitute process ID assigned at connection time */
1304 lhdr->src_pid = cpu_to_le32(id->pid);
1305 lhdr->src_nid = cpu_to_le64(id->nid);
1308 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1309 ksocknal_conn_addref(conn); /* ++ref while parsing */
1311 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1312 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1313 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1315 /* I just received garbage: give up on this conn */
1316 ksocknal_new_packet(conn, 0);
1317 ksocknal_close_conn_and_siblings (conn, rc);
1318 ksocknal_conn_decref(conn);
1322 /* I'm racing with ksocknal_recv() */
1323 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1324 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1326 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1329 /* ksocknal_recv() got called */
1332 case SOCKNAL_RX_LNET_PAYLOAD:
1333 /* payload all received */
1336 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1337 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1338 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1339 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1340 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1341 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1345 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1346 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1348 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1349 id = &conn->ksnc_peer->ksnp_id;
1351 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1352 conn->ksnc_msg.ksm_zc_cookies[0],
1353 *ksocknal_tunables.ksnd_nonblk_zcack ||
1354 le64_to_cpu(lhdr->src_nid) != id->nid);
1357 if (rc && conn->ksnc_lnet_msg)
1358 conn->ksnc_lnet_msg->msg_health_status =
1359 LNET_MSG_STATUS_REMOTE_ERROR;
1360 lnet_finalize(conn->ksnc_lnet_msg, rc);
1363 ksocknal_new_packet(conn, 0);
1364 ksocknal_close_conn_and_siblings (conn, rc);
1369 case SOCKNAL_RX_SLOP:
1370 /* starting new packet? */
1371 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1372 return 0; /* come back later */
1373 goto again; /* try to finish reading slop now */
1381 return (-EINVAL); /* keep gcc happy */
1385 ksocknal_recv(struct lnet_ni *ni, void *private, struct lnet_msg *msg,
1386 int delayed, unsigned int niov, struct kvec *iov,
1387 lnet_kiov_t *kiov, unsigned int offset, unsigned int mlen,
1390 struct ksock_conn *conn = private;
1391 struct ksock_sched *sched = conn->ksnc_scheduler;
1393 LASSERT (mlen <= rlen);
1394 LASSERT (niov <= LNET_MAX_IOV);
1396 conn->ksnc_lnet_msg = msg;
1397 conn->ksnc_rx_nob_wanted = mlen;
1398 conn->ksnc_rx_nob_left = rlen;
1400 if (mlen == 0 || iov != NULL) {
1401 conn->ksnc_rx_nkiov = 0;
1402 conn->ksnc_rx_kiov = NULL;
1403 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1404 conn->ksnc_rx_niov =
1405 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1406 niov, iov, offset, mlen);
1408 conn->ksnc_rx_niov = 0;
1409 conn->ksnc_rx_iov = NULL;
1410 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1411 conn->ksnc_rx_nkiov =
1412 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1413 niov, kiov, offset, mlen);
1417 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1418 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1420 LASSERT (conn->ksnc_rx_scheduled);
1422 spin_lock_bh(&sched->kss_lock);
1424 switch (conn->ksnc_rx_state) {
1425 case SOCKNAL_RX_PARSE_WAIT:
1426 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1427 wake_up(&sched->kss_waitq);
1428 LASSERT(conn->ksnc_rx_ready);
1431 case SOCKNAL_RX_PARSE:
1432 /* scheduler hasn't noticed I'm parsing yet */
1436 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1438 spin_unlock_bh(&sched->kss_lock);
1439 ksocknal_conn_decref(conn);
1444 ksocknal_sched_cansleep(struct ksock_sched *sched)
1448 spin_lock_bh(&sched->kss_lock);
1450 rc = (!ksocknal_data.ksnd_shuttingdown &&
1451 list_empty(&sched->kss_rx_conns) &&
1452 list_empty(&sched->kss_tx_conns));
1454 spin_unlock_bh(&sched->kss_lock);
1458 int ksocknal_scheduler(void *arg)
1460 struct ksock_sched *sched;
1461 struct ksock_conn *conn;
1462 struct ksock_tx *tx;
1465 long id = (long)arg;
1466 struct page **rx_scratch_pgs;
1467 struct kvec *scratch_iov;
1469 sched = ksocknal_data.ksnd_schedulers[KSOCK_THREAD_CPT(id)];
1471 LIBCFS_CPT_ALLOC(rx_scratch_pgs, lnet_cpt_table(), sched->kss_cpt,
1472 sizeof(*rx_scratch_pgs) * LNET_MAX_IOV);
1473 if (!rx_scratch_pgs) {
1474 CERROR("Unable to allocate scratch pages\n");
1478 LIBCFS_CPT_ALLOC(scratch_iov, lnet_cpt_table(), sched->kss_cpt,
1479 sizeof(*scratch_iov) * LNET_MAX_IOV);
1481 CERROR("Unable to allocate scratch iov\n");
1485 rc = cfs_cpt_bind(lnet_cpt_table(), sched->kss_cpt);
1487 CWARN("Can't set CPU partition affinity to %d: %d\n",
1488 sched->kss_cpt, rc);
1491 spin_lock_bh(&sched->kss_lock);
1493 while (!ksocknal_data.ksnd_shuttingdown) {
1494 int did_something = 0;
1496 /* Ensure I progress everything semi-fairly */
1498 if (!list_empty(&sched->kss_rx_conns)) {
1499 conn = list_entry(sched->kss_rx_conns.next,
1500 struct ksock_conn, ksnc_rx_list);
1501 list_del(&conn->ksnc_rx_list);
1503 LASSERT(conn->ksnc_rx_scheduled);
1504 LASSERT(conn->ksnc_rx_ready);
1506 /* clear rx_ready in case receive isn't complete.
1507 * Do it BEFORE we call process_recv, since
1508 * data_ready can set it any time after we release
1510 conn->ksnc_rx_ready = 0;
1511 spin_unlock_bh(&sched->kss_lock);
1513 rc = ksocknal_process_receive(conn, rx_scratch_pgs,
1516 spin_lock_bh(&sched->kss_lock);
1518 /* I'm the only one that can clear this flag */
1519 LASSERT(conn->ksnc_rx_scheduled);
1521 /* Did process_receive get everything it wanted? */
1523 conn->ksnc_rx_ready = 1;
1525 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1526 /* Conn blocked waiting for ksocknal_recv()
1527 * I change its state (under lock) to signal
1528 * it can be rescheduled */
1529 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1530 } else if (conn->ksnc_rx_ready) {
1531 /* reschedule for rx */
1532 list_add_tail(&conn->ksnc_rx_list,
1533 &sched->kss_rx_conns);
1535 conn->ksnc_rx_scheduled = 0;
1537 ksocknal_conn_decref(conn);
1543 if (!list_empty(&sched->kss_tx_conns)) {
1546 list_splice_init(&sched->kss_zombie_noop_txs, &zlist);
1548 conn = list_entry(sched->kss_tx_conns.next,
1549 struct ksock_conn, ksnc_tx_list);
1550 list_del(&conn->ksnc_tx_list);
1552 LASSERT(conn->ksnc_tx_scheduled);
1553 LASSERT(conn->ksnc_tx_ready);
1554 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1556 tx = list_entry(conn->ksnc_tx_queue.next,
1557 struct ksock_tx, tx_list);
1559 if (conn->ksnc_tx_carrier == tx)
1560 ksocknal_next_tx_carrier(conn);
1562 /* dequeue now so empty list => more to send */
1563 list_del(&tx->tx_list);
1565 /* Clear tx_ready in case send isn't complete. Do
1566 * it BEFORE we call process_transmit, since
1567 * write_space can set it any time after we release
1569 conn->ksnc_tx_ready = 0;
1570 spin_unlock_bh(&sched->kss_lock);
1572 if (!list_empty(&zlist)) {
1573 /* free zombie noop txs, it's fast because
1574 * noop txs are just put in freelist */
1575 ksocknal_txlist_done(NULL, &zlist, 0);
1578 rc = ksocknal_process_transmit(conn, tx, scratch_iov);
1580 if (rc == -ENOMEM || rc == -EAGAIN) {
1581 /* Incomplete send: replace tx on HEAD of tx_queue */
1582 spin_lock_bh(&sched->kss_lock);
1583 list_add(&tx->tx_list,
1584 &conn->ksnc_tx_queue);
1586 /* Complete send; tx -ref */
1587 ksocknal_tx_decref(tx);
1589 spin_lock_bh(&sched->kss_lock);
1590 /* assume space for more */
1591 conn->ksnc_tx_ready = 1;
1594 if (rc == -ENOMEM) {
1595 /* Do nothing; after a short timeout, this
1596 * conn will be reposted on kss_tx_conns. */
1597 } else if (conn->ksnc_tx_ready &&
1598 !list_empty(&conn->ksnc_tx_queue)) {
1599 /* reschedule for tx */
1600 list_add_tail(&conn->ksnc_tx_list,
1601 &sched->kss_tx_conns);
1603 conn->ksnc_tx_scheduled = 0;
1605 ksocknal_conn_decref(conn);
1610 if (!did_something || /* nothing to do */
1611 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1612 spin_unlock_bh(&sched->kss_lock);
1616 if (!did_something) { /* wait for something to do */
1617 rc = wait_event_interruptible_exclusive(
1619 !ksocknal_sched_cansleep(sched));
1625 spin_lock_bh(&sched->kss_lock);
1629 spin_unlock_bh(&sched->kss_lock);
1630 CFS_FREE_PTR_ARRAY(rx_scratch_pgs, LNET_MAX_IOV);
1631 CFS_FREE_PTR_ARRAY(scratch_iov, LNET_MAX_IOV);
1632 ksocknal_thread_fini();
1637 * Add connection to kss_rx_conns of scheduler
1638 * and wakeup the scheduler.
1640 void ksocknal_read_callback(struct ksock_conn *conn)
1642 struct ksock_sched *sched;
1645 sched = conn->ksnc_scheduler;
1647 spin_lock_bh(&sched->kss_lock);
1649 conn->ksnc_rx_ready = 1;
1651 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1652 list_add_tail(&conn->ksnc_rx_list,
1653 &sched->kss_rx_conns);
1654 conn->ksnc_rx_scheduled = 1;
1655 /* extra ref for scheduler */
1656 ksocknal_conn_addref(conn);
1658 wake_up (&sched->kss_waitq);
1660 spin_unlock_bh(&sched->kss_lock);
1666 * Add connection to kss_tx_conns of scheduler
1667 * and wakeup the scheduler.
1669 void ksocknal_write_callback(struct ksock_conn *conn)
1671 struct ksock_sched *sched;
1674 sched = conn->ksnc_scheduler;
1676 spin_lock_bh(&sched->kss_lock);
1678 conn->ksnc_tx_ready = 1;
1680 if (!conn->ksnc_tx_scheduled && /* not being progressed */
1681 !list_empty(&conn->ksnc_tx_queue)) { /* packets to send */
1682 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
1683 conn->ksnc_tx_scheduled = 1;
1684 /* extra ref for scheduler */
1685 ksocknal_conn_addref(conn);
1687 wake_up(&sched->kss_waitq);
1690 spin_unlock_bh(&sched->kss_lock);
1695 static const struct ksock_proto *
1696 ksocknal_parse_proto_version(struct ksock_hello_msg *hello)
1700 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1701 version = hello->kshm_version;
1702 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1703 version = __swab32(hello->kshm_version);
1706 #if SOCKNAL_VERSION_DEBUG
1707 if (*ksocknal_tunables.ksnd_protocol == 1)
1710 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1711 version == KSOCK_PROTO_V3)
1714 if (version == KSOCK_PROTO_V2)
1715 return &ksocknal_protocol_v2x;
1717 if (version == KSOCK_PROTO_V3)
1718 return &ksocknal_protocol_v3x;
1723 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1724 struct lnet_magicversion *hmv;
1726 BUILD_BUG_ON(sizeof(struct lnet_magicversion) !=
1727 offsetof(struct ksock_hello_msg, kshm_src_nid));
1729 hmv = (struct lnet_magicversion *)hello;
1731 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1732 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1733 return &ksocknal_protocol_v1x;
1740 ksocknal_send_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1741 lnet_nid_t peer_nid, struct ksock_hello_msg *hello)
1743 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1744 struct ksock_net *net = (struct ksock_net *)ni->ni_data;
1746 LASSERT(hello->kshm_nips <= LNET_INTERFACES_NUM);
1748 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1749 LASSERT(conn->ksnc_proto != NULL);
1751 hello->kshm_src_nid = ni->ni_nid;
1752 hello->kshm_dst_nid = peer_nid;
1753 hello->kshm_src_pid = the_lnet.ln_pid;
1755 hello->kshm_src_incarnation = net->ksnn_incarnation;
1756 hello->kshm_ctype = conn->ksnc_type;
1758 return conn->ksnc_proto->pro_send_hello(conn, hello);
1762 ksocknal_invert_type(int type)
1766 case SOCKLND_CONN_ANY:
1767 case SOCKLND_CONN_CONTROL:
1769 case SOCKLND_CONN_BULK_IN:
1770 return SOCKLND_CONN_BULK_OUT;
1771 case SOCKLND_CONN_BULK_OUT:
1772 return SOCKLND_CONN_BULK_IN;
1774 return (SOCKLND_CONN_NONE);
1779 ksocknal_recv_hello(struct lnet_ni *ni, struct ksock_conn *conn,
1780 struct ksock_hello_msg *hello,
1781 struct lnet_process_id *peerid,
1784 /* Return < 0 fatal error
1786 * EALREADY lost connection race
1787 * EPROTO protocol version mismatch
1789 struct socket *sock = conn->ksnc_sock;
1790 int active = (conn->ksnc_proto != NULL);
1794 const struct ksock_proto *proto;
1795 struct lnet_process_id recv_id;
1797 /* socket type set on active connections - not set on passive */
1798 LASSERT(!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1800 timeout = active ? lnet_get_lnd_timeout() :
1801 lnet_acceptor_timeout();
1803 rc = lnet_sock_read(sock, &hello->kshm_magic,
1804 sizeof(hello->kshm_magic), timeout);
1806 CERROR("Error %d reading HELLO from %pI4h\n",
1807 rc, &conn->ksnc_ipaddr);
1812 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1813 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1814 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
1815 /* Unexpected magic! */
1816 CERROR ("Bad magic(1) %#08x (%#08x expected) from "
1817 "%pI4h\n", __cpu_to_le32 (hello->kshm_magic),
1818 LNET_PROTO_TCP_MAGIC, &conn->ksnc_ipaddr);
1822 rc = lnet_sock_read(sock, &hello->kshm_version,
1823 sizeof(hello->kshm_version), timeout);
1825 CERROR("Error %d reading HELLO from %pI4h\n",
1826 rc, &conn->ksnc_ipaddr);
1831 proto = ksocknal_parse_proto_version(hello);
1832 if (proto == NULL) {
1834 /* unknown protocol from peer_ni, tell peer_ni my protocol */
1835 conn->ksnc_proto = &ksocknal_protocol_v3x;
1836 #if SOCKNAL_VERSION_DEBUG
1837 if (*ksocknal_tunables.ksnd_protocol == 2)
1838 conn->ksnc_proto = &ksocknal_protocol_v2x;
1839 else if (*ksocknal_tunables.ksnd_protocol == 1)
1840 conn->ksnc_proto = &ksocknal_protocol_v1x;
1842 hello->kshm_nips = 0;
1843 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1846 CERROR("Unknown protocol version (%d.x expected) from %pI4h\n",
1847 conn->ksnc_proto->pro_version, &conn->ksnc_ipaddr);
1852 proto_match = (conn->ksnc_proto == proto);
1853 conn->ksnc_proto = proto;
1855 /* receive the rest of hello message anyway */
1856 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1858 CERROR("Error %d reading or checking hello from from %pI4h\n",
1859 rc, &conn->ksnc_ipaddr);
1864 *incarnation = hello->kshm_src_incarnation;
1866 if (hello->kshm_src_nid == LNET_NID_ANY) {
1867 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY from %pI4h\n",
1868 &conn->ksnc_ipaddr);
1873 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1874 /* Userspace NAL assigns peer_ni process ID from socket */
1875 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1876 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
1878 recv_id.nid = hello->kshm_src_nid;
1879 recv_id.pid = hello->kshm_src_pid;
1885 /* peer_ni determines type */
1886 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1887 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
1888 CERROR("Unexpected type %d from %s ip %pI4h\n",
1889 hello->kshm_ctype, libcfs_id2str(*peerid),
1890 &conn->ksnc_ipaddr);
1896 if (peerid->pid != recv_id.pid ||
1897 peerid->nid != recv_id.nid) {
1898 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1899 " %pI4h, but they claimed they were "
1900 "%s; please check your Lustre "
1902 libcfs_id2str(*peerid),
1904 libcfs_id2str(recv_id));
1908 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1909 /* Possible protocol mismatch or I lost the connection race */
1910 return proto_match ? EALREADY : EPROTO;
1913 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1914 CERROR("Mismatched types: me %d, %s ip %pI4h %d\n",
1915 conn->ksnc_type, libcfs_id2str(*peerid),
1924 ksocknal_connect(struct ksock_route *route)
1927 struct ksock_peer_ni *peer_ni = route->ksnr_peer;
1930 struct socket *sock;
1932 int retry_later = 0;
1935 deadline = ktime_get_seconds() + lnet_get_lnd_timeout();
1937 write_lock_bh(&ksocknal_data.ksnd_global_lock);
1939 LASSERT (route->ksnr_scheduled);
1940 LASSERT (!route->ksnr_connecting);
1942 route->ksnr_connecting = 1;
1945 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1947 /* stop connecting if peer_ni/route got closed under me, or
1948 * route got connected while queued */
1949 if (peer_ni->ksnp_closing || route->ksnr_deleted ||
1955 /* reschedule if peer_ni is connecting to me */
1956 if (peer_ni->ksnp_accepting > 0) {
1958 "peer_ni %s(%d) already connecting to me, retry later.\n",
1959 libcfs_nid2str(peer_ni->ksnp_id.nid), peer_ni->ksnp_accepting);
1963 if (retry_later) /* needs reschedule */
1966 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
1967 type = SOCKLND_CONN_ANY;
1968 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
1969 type = SOCKLND_CONN_CONTROL;
1970 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
1971 type = SOCKLND_CONN_BULK_IN;
1973 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
1974 type = SOCKLND_CONN_BULK_OUT;
1977 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
1979 if (ktime_get_seconds() >= deadline) {
1981 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
1987 rc = lnet_connect(&sock, peer_ni->ksnp_id.nid,
1988 route->ksnr_myipaddr,
1989 route->ksnr_ipaddr, route->ksnr_port,
1990 peer_ni->ksnp_ni->ni_net_ns);
1994 rc = ksocknal_create_conn(peer_ni->ksnp_ni, route, sock, type);
1996 lnet_connect_console_error(rc, peer_ni->ksnp_id.nid,
2002 /* A +ve RC means I have to retry because I lost the connection
2003 * race or I have to renegotiate protocol version */
2004 retry_later = (rc != 0);
2006 CDEBUG(D_NET, "peer_ni %s: conn race, retry later.\n",
2007 libcfs_nid2str(peer_ni->ksnp_id.nid));
2009 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2012 route->ksnr_scheduled = 0;
2013 route->ksnr_connecting = 0;
2016 /* re-queue for attention; this frees me up to handle
2017 * the peer_ni's incoming connection request */
2019 if (rc == EALREADY ||
2020 (rc == 0 && peer_ni->ksnp_accepting > 0)) {
2021 /* We want to introduce a delay before next
2022 * attempt to connect if we lost conn race,
2023 * but the race is resolved quickly usually,
2024 * so min_reconnectms should be good heuristic */
2025 route->ksnr_retry_interval = *ksocknal_tunables.ksnd_min_reconnectms / 1000;
2026 route->ksnr_timeout = ktime_get_seconds() +
2027 route->ksnr_retry_interval;
2030 ksocknal_launch_connection_locked(route);
2033 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2037 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2039 route->ksnr_scheduled = 0;
2040 route->ksnr_connecting = 0;
2042 /* This is a retry rather than a new connection */
2043 route->ksnr_retry_interval *= 2;
2044 route->ksnr_retry_interval =
2045 max_t(time64_t, route->ksnr_retry_interval,
2046 *ksocknal_tunables.ksnd_min_reconnectms / 1000);
2047 route->ksnr_retry_interval =
2048 min_t(time64_t, route->ksnr_retry_interval,
2049 *ksocknal_tunables.ksnd_max_reconnectms / 1000);
2051 LASSERT(route->ksnr_retry_interval);
2052 route->ksnr_timeout = ktime_get_seconds() + route->ksnr_retry_interval;
2054 if (!list_empty(&peer_ni->ksnp_tx_queue) &&
2055 peer_ni->ksnp_accepting == 0 &&
2056 ksocknal_find_connecting_route_locked(peer_ni) == NULL) {
2057 struct ksock_conn *conn;
2059 /* ksnp_tx_queue is queued on a conn on successful
2060 * connection for V1.x and V2.x */
2061 if (!list_empty(&peer_ni->ksnp_conns)) {
2062 conn = list_entry(peer_ni->ksnp_conns.next,
2063 struct ksock_conn, ksnc_list);
2064 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
2067 /* take all the blocked packets while I've got the lock and
2068 * complete below... */
2069 list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
2072 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2074 ksocknal_peer_failed(peer_ni);
2075 ksocknal_txlist_done(peer_ni->ksnp_ni, &zombies, rc);
2080 * check whether we need to create more connds.
2081 * It will try to create new thread if it's necessary, @timeout can
2082 * be updated if failed to create, so caller wouldn't keep try while
2083 * running out of resource.
2086 ksocknal_connd_check_start(time64_t sec, long *timeout)
2090 int total = ksocknal_data.ksnd_connd_starting +
2091 ksocknal_data.ksnd_connd_running;
2093 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2094 /* still in initializing */
2098 if (total >= *ksocknal_tunables.ksnd_nconnds_max ||
2099 total > ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV) {
2100 /* can't create more connd, or still have enough
2101 * threads to handle more connecting */
2105 if (list_empty(&ksocknal_data.ksnd_connd_routes)) {
2106 /* no pending connecting request */
2110 if (sec - ksocknal_data.ksnd_connd_failed_stamp <= 1) {
2111 /* may run out of resource, retry later */
2112 *timeout = cfs_time_seconds(1);
2116 if (ksocknal_data.ksnd_connd_starting > 0) {
2117 /* serialize starting to avoid flood */
2121 ksocknal_data.ksnd_connd_starting_stamp = sec;
2122 ksocknal_data.ksnd_connd_starting++;
2123 spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2125 /* NB: total is the next id */
2126 snprintf(name, sizeof(name), "socknal_cd%02d", total);
2127 rc = ksocknal_thread_start(ksocknal_connd, NULL, name);
2129 spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2134 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2135 ksocknal_data.ksnd_connd_starting--;
2136 ksocknal_data.ksnd_connd_failed_stamp = ktime_get_real_seconds();
2142 * check whether current thread can exit, it will return 1 if there are too
2143 * many threads and no creating in past 120 seconds.
2144 * Also, this function may update @timeout to make caller come back
2145 * again to recheck these conditions.
2148 ksocknal_connd_check_stop(time64_t sec, long *timeout)
2152 if (unlikely(ksocknal_data.ksnd_init < SOCKNAL_INIT_ALL)) {
2153 /* still in initializing */
2157 if (ksocknal_data.ksnd_connd_starting > 0) {
2158 /* in progress of starting new thread */
2162 if (ksocknal_data.ksnd_connd_running <=
2163 *ksocknal_tunables.ksnd_nconnds) { /* can't shrink */
2167 /* created thread in past 120 seconds? */
2168 val = (int)(ksocknal_data.ksnd_connd_starting_stamp +
2169 SOCKNAL_CONND_TIMEOUT - sec);
2171 *timeout = (val > 0) ? cfs_time_seconds(val) :
2172 cfs_time_seconds(SOCKNAL_CONND_TIMEOUT);
2176 /* no creating in past 120 seconds */
2178 return ksocknal_data.ksnd_connd_running >
2179 ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV;
2182 /* Go through connd_routes queue looking for a route that we can process
2183 * right now, @timeout_p can be updated if we need to come back later */
2184 static struct ksock_route *
2185 ksocknal_connd_get_route_locked(signed long *timeout_p)
2187 time64_t now = ktime_get_seconds();
2188 struct ksock_route *route;
2190 /* connd_routes can contain both pending and ordinary routes */
2191 list_for_each_entry(route, &ksocknal_data.ksnd_connd_routes,
2194 if (route->ksnr_retry_interval == 0 ||
2195 now >= route->ksnr_timeout)
2198 if (*timeout_p == MAX_SCHEDULE_TIMEOUT ||
2199 *timeout_p > cfs_time_seconds(route->ksnr_timeout - now))
2200 *timeout_p = cfs_time_seconds(route->ksnr_timeout - now);
2207 ksocknal_connd(void *arg)
2209 spinlock_t *connd_lock = &ksocknal_data.ksnd_connd_lock;
2210 struct ksock_connreq *cr;
2211 wait_queue_entry_t wait;
2215 init_waitqueue_entry(&wait, current);
2217 spin_lock_bh(connd_lock);
2219 LASSERT(ksocknal_data.ksnd_connd_starting > 0);
2220 ksocknal_data.ksnd_connd_starting--;
2221 ksocknal_data.ksnd_connd_running++;
2223 while (!ksocknal_data.ksnd_shuttingdown) {
2224 struct ksock_route *route = NULL;
2225 time64_t sec = ktime_get_real_seconds();
2226 long timeout = MAX_SCHEDULE_TIMEOUT;
2227 int dropped_lock = 0;
2229 if (ksocknal_connd_check_stop(sec, &timeout)) {
2230 /* wakeup another one to check stop */
2231 wake_up(&ksocknal_data.ksnd_connd_waitq);
2235 if (ksocknal_connd_check_start(sec, &timeout)) {
2236 /* created new thread */
2240 if (!list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2241 /* Connection accepted by the listener */
2242 cr = list_entry(ksocknal_data.ksnd_connd_connreqs.next,
2243 struct ksock_connreq, ksncr_list);
2245 list_del(&cr->ksncr_list);
2246 spin_unlock_bh(connd_lock);
2249 ksocknal_create_conn(cr->ksncr_ni, NULL,
2250 cr->ksncr_sock, SOCKLND_CONN_NONE);
2251 lnet_ni_decref(cr->ksncr_ni);
2252 LIBCFS_FREE(cr, sizeof(*cr));
2254 spin_lock_bh(connd_lock);
2257 /* Only handle an outgoing connection request if there
2258 * is a thread left to handle incoming connections and
2259 * create new connd */
2260 if (ksocknal_data.ksnd_connd_connecting + SOCKNAL_CONND_RESV <
2261 ksocknal_data.ksnd_connd_running) {
2262 route = ksocknal_connd_get_route_locked(&timeout);
2264 if (route != NULL) {
2265 list_del(&route->ksnr_connd_list);
2266 ksocknal_data.ksnd_connd_connecting++;
2267 spin_unlock_bh(connd_lock);
2270 if (ksocknal_connect(route)) {
2271 /* consecutive retry */
2272 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2273 CWARN("massive consecutive "
2274 "re-connecting to %pI4h\n",
2275 &route->ksnr_ipaddr);
2282 ksocknal_route_decref(route);
2284 spin_lock_bh(connd_lock);
2285 ksocknal_data.ksnd_connd_connecting--;
2289 if (++nloops < SOCKNAL_RESCHED)
2291 spin_unlock_bh(connd_lock);
2294 spin_lock_bh(connd_lock);
2298 /* Nothing to do for 'timeout' */
2299 set_current_state(TASK_INTERRUPTIBLE);
2300 add_wait_queue_exclusive(&ksocknal_data.ksnd_connd_waitq, &wait);
2301 spin_unlock_bh(connd_lock);
2304 schedule_timeout(timeout);
2306 remove_wait_queue(&ksocknal_data.ksnd_connd_waitq, &wait);
2307 spin_lock_bh(connd_lock);
2309 ksocknal_data.ksnd_connd_running--;
2310 spin_unlock_bh(connd_lock);
2312 ksocknal_thread_fini();
2316 static struct ksock_conn *
2317 ksocknal_find_timed_out_conn(struct ksock_peer_ni *peer_ni)
2319 /* We're called with a shared lock on ksnd_global_lock */
2320 struct ksock_conn *conn;
2321 struct list_head *ctmp;
2322 struct ksock_tx *tx;
2324 list_for_each(ctmp, &peer_ni->ksnp_conns) {
2327 conn = list_entry(ctmp, struct ksock_conn, ksnc_list);
2329 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2330 LASSERT (!conn->ksnc_closing);
2332 error = conn->ksnc_sock->sk->sk_err;
2334 ksocknal_conn_addref(conn);
2338 CNETERR("A connection with %s "
2339 "(%pI4h:%d) was reset; "
2340 "it may have rebooted.\n",
2341 libcfs_id2str(peer_ni->ksnp_id),
2346 CNETERR("A connection with %s "
2347 "(%pI4h:%d) timed out; the "
2348 "network or node may be down.\n",
2349 libcfs_id2str(peer_ni->ksnp_id),
2354 CNETERR("An unexpected network error %d "
2356 "(%pI4h:%d\n", error,
2357 libcfs_id2str(peer_ni->ksnp_id),
2366 if (conn->ksnc_rx_started &&
2367 ktime_get_seconds() >= conn->ksnc_rx_deadline) {
2368 /* Timed out incomplete incoming message */
2369 ksocknal_conn_addref(conn);
2370 CNETERR("Timeout receiving from %s (%pI4h:%d), "
2371 "state %d wanted %d left %d\n",
2372 libcfs_id2str(peer_ni->ksnp_id),
2375 conn->ksnc_rx_state,
2376 conn->ksnc_rx_nob_wanted,
2377 conn->ksnc_rx_nob_left);
2381 if ((!list_empty(&conn->ksnc_tx_queue) ||
2382 conn->ksnc_sock->sk->sk_wmem_queued != 0) &&
2383 ktime_get_seconds() >= conn->ksnc_tx_deadline) {
2384 /* Timed out messages queued for sending or
2385 * buffered in the socket's send buffer */
2386 ksocknal_conn_addref(conn);
2387 list_for_each_entry(tx, &conn->ksnc_tx_queue,
2390 LNET_MSG_STATUS_LOCAL_TIMEOUT;
2391 CNETERR("Timeout sending data to %s (%pI4h:%d) "
2392 "the network or that node may be down.\n",
2393 libcfs_id2str(peer_ni->ksnp_id),
2394 &conn->ksnc_ipaddr, conn->ksnc_port);
2403 ksocknal_flush_stale_txs(struct ksock_peer_ni *peer_ni)
2405 struct ksock_tx *tx;
2406 LIST_HEAD(stale_txs);
2408 write_lock_bh(&ksocknal_data.ksnd_global_lock);
2410 while (!list_empty(&peer_ni->ksnp_tx_queue)) {
2411 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2412 struct ksock_tx, tx_list);
2414 if (ktime_get_seconds() < tx->tx_deadline)
2417 tx->tx_hstatus = LNET_MSG_STATUS_LOCAL_TIMEOUT;
2419 list_move_tail(&tx->tx_list, &stale_txs);
2422 write_unlock_bh(&ksocknal_data.ksnd_global_lock);
2424 ksocknal_txlist_done(peer_ni->ksnp_ni, &stale_txs, -ETIMEDOUT);
2428 ksocknal_send_keepalive_locked(struct ksock_peer_ni *peer_ni)
2429 __must_hold(&ksocknal_data.ksnd_global_lock)
2431 struct ksock_sched *sched;
2432 struct ksock_conn *conn;
2433 struct ksock_tx *tx;
2435 /* last_alive will be updated by create_conn */
2436 if (list_empty(&peer_ni->ksnp_conns))
2439 if (peer_ni->ksnp_proto != &ksocknal_protocol_v3x)
2442 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2443 ktime_get_seconds() < peer_ni->ksnp_last_alive +
2444 *ksocknal_tunables.ksnd_keepalive)
2447 if (ktime_get_seconds() < peer_ni->ksnp_send_keepalive)
2450 /* retry 10 secs later, so we wouldn't put pressure
2451 * on this peer_ni if we failed to send keepalive this time */
2452 peer_ni->ksnp_send_keepalive = ktime_get_seconds() + 10;
2454 conn = ksocknal_find_conn_locked(peer_ni, NULL, 1);
2456 sched = conn->ksnc_scheduler;
2458 spin_lock_bh(&sched->kss_lock);
2459 if (!list_empty(&conn->ksnc_tx_queue)) {
2460 spin_unlock_bh(&sched->kss_lock);
2461 /* there is an queued ACK, don't need keepalive */
2465 spin_unlock_bh(&sched->kss_lock);
2468 read_unlock(&ksocknal_data.ksnd_global_lock);
2470 /* cookie = 1 is reserved for keepalive PING */
2471 tx = ksocknal_alloc_tx_noop(1, 1);
2473 read_lock(&ksocknal_data.ksnd_global_lock);
2477 if (ksocknal_launch_packet(peer_ni->ksnp_ni, tx, peer_ni->ksnp_id) == 0) {
2478 read_lock(&ksocknal_data.ksnd_global_lock);
2482 ksocknal_free_tx(tx);
2483 read_lock(&ksocknal_data.ksnd_global_lock);
2490 ksocknal_check_peer_timeouts(int idx)
2492 struct hlist_head *peers = &ksocknal_data.ksnd_peers[idx];
2493 struct ksock_peer_ni *peer_ni;
2494 struct ksock_conn *conn;
2495 struct ksock_tx *tx;
2498 /* NB. We expect to have a look at all the peers and not find any
2499 * connections to time out, so we just use a shared lock while we
2502 read_lock(&ksocknal_data.ksnd_global_lock);
2504 hlist_for_each_entry(peer_ni, peers, ksnp_list) {
2505 struct ksock_tx *tx_stale;
2506 time64_t deadline = 0;
2510 if (ksocknal_send_keepalive_locked(peer_ni) != 0) {
2511 read_unlock(&ksocknal_data.ksnd_global_lock);
2515 conn = ksocknal_find_timed_out_conn(peer_ni);
2518 read_unlock(&ksocknal_data.ksnd_global_lock);
2520 ksocknal_close_conn_and_siblings(conn, -ETIMEDOUT);
2522 /* NB we won't find this one again, but we can't
2523 * just proceed with the next peer_ni, since we dropped
2524 * ksnd_global_lock and it might be dead already!
2526 ksocknal_conn_decref(conn);
2530 /* we can't process stale txs right here because we're
2531 * holding only shared lock
2533 if (!list_empty(&peer_ni->ksnp_tx_queue)) {
2534 struct ksock_tx *tx;
2536 tx = list_entry(peer_ni->ksnp_tx_queue.next,
2537 struct ksock_tx, tx_list);
2538 if (ktime_get_seconds() >= tx->tx_deadline) {
2539 ksocknal_peer_addref(peer_ni);
2540 read_unlock(&ksocknal_data.ksnd_global_lock);
2542 ksocknal_flush_stale_txs(peer_ni);
2544 ksocknal_peer_decref(peer_ni);
2549 if (list_empty(&peer_ni->ksnp_zc_req_list))
2553 spin_lock(&peer_ni->ksnp_lock);
2554 list_for_each_entry(tx, &peer_ni->ksnp_zc_req_list, tx_zc_list) {
2555 if (ktime_get_seconds() < tx->tx_deadline)
2557 /* ignore the TX if connection is being closed */
2558 if (tx->tx_conn->ksnc_closing)
2561 if (tx_stale == NULL)
2565 if (tx_stale == NULL) {
2566 spin_unlock(&peer_ni->ksnp_lock);
2570 deadline = tx_stale->tx_deadline;
2571 resid = tx_stale->tx_resid;
2572 conn = tx_stale->tx_conn;
2573 ksocknal_conn_addref(conn);
2575 spin_unlock(&peer_ni->ksnp_lock);
2576 read_unlock(&ksocknal_data.ksnd_global_lock);
2578 CERROR("Total %d stale ZC_REQs for peer_ni %s detected; the "
2579 "oldest(%p) timed out %lld secs ago, "
2580 "resid: %d, wmem: %d\n",
2581 n, libcfs_nid2str(peer_ni->ksnp_id.nid), tx_stale,
2582 ktime_get_seconds() - deadline,
2583 resid, conn->ksnc_sock->sk->sk_wmem_queued);
2585 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2586 ksocknal_conn_decref(conn);
2590 read_unlock(&ksocknal_data.ksnd_global_lock);
2593 int ksocknal_reaper(void *arg)
2595 wait_queue_entry_t wait;
2596 struct ksock_conn *conn;
2597 struct ksock_sched *sched;
2598 LIST_HEAD(enomem_conns);
2603 time64_t deadline = ktime_get_seconds();
2605 init_waitqueue_entry(&wait, current);
2607 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2609 while (!ksocknal_data.ksnd_shuttingdown) {
2610 if (!list_empty(&ksocknal_data.ksnd_deathrow_conns)) {
2611 conn = list_entry(ksocknal_data.ksnd_deathrow_conns.next,
2612 struct ksock_conn, ksnc_list);
2613 list_del(&conn->ksnc_list);
2615 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2617 ksocknal_terminate_conn(conn);
2618 ksocknal_conn_decref(conn);
2620 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2624 if (!list_empty(&ksocknal_data.ksnd_zombie_conns)) {
2625 conn = list_entry(ksocknal_data.ksnd_zombie_conns.next,
2626 struct ksock_conn, ksnc_list);
2627 list_del(&conn->ksnc_list);
2629 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2631 ksocknal_destroy_conn(conn);
2633 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2637 list_splice_init(&ksocknal_data.ksnd_enomem_conns,
2640 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2642 /* reschedule all the connections that stalled with ENOMEM... */
2644 while (!list_empty(&enomem_conns)) {
2645 conn = list_entry(enomem_conns.next,
2646 struct ksock_conn, ksnc_tx_list);
2647 list_del(&conn->ksnc_tx_list);
2649 sched = conn->ksnc_scheduler;
2651 spin_lock_bh(&sched->kss_lock);
2653 LASSERT(conn->ksnc_tx_scheduled);
2654 conn->ksnc_tx_ready = 1;
2655 list_add_tail(&conn->ksnc_tx_list,
2656 &sched->kss_tx_conns);
2657 wake_up(&sched->kss_waitq);
2659 spin_unlock_bh(&sched->kss_lock);
2663 /* careful with the jiffy wrap... */
2664 while ((timeout = deadline - ktime_get_seconds()) <= 0) {
2667 int chunk = HASH_SIZE(ksocknal_data.ksnd_peers);
2668 unsigned int lnd_timeout;
2670 /* Time to check for timeouts on a few more peers: I
2671 * do checks every 'p' seconds on a proportion of the
2672 * peer_ni table and I need to check every connection
2673 * 'n' times within a timeout interval, to ensure I
2674 * detect a timeout on any connection within (n+1)/n
2675 * times the timeout interval.
2678 lnd_timeout = lnet_get_lnd_timeout();
2679 if (lnd_timeout > n * p)
2680 chunk = (chunk * n * p) / lnd_timeout;
2684 for (i = 0; i < chunk; i++) {
2685 ksocknal_check_peer_timeouts(peer_index);
2686 peer_index = (peer_index + 1) %
2687 HASH_SIZE(ksocknal_data.ksnd_peers);
2693 if (nenomem_conns != 0) {
2694 /* Reduce my timeout if I rescheduled ENOMEM conns.
2695 * This also prevents me getting woken immediately
2696 * if any go back on my enomem list. */
2697 timeout = SOCKNAL_ENOMEM_RETRY;
2699 ksocknal_data.ksnd_reaper_waketime = ktime_get_seconds() +
2702 set_current_state(TASK_INTERRUPTIBLE);
2703 add_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2705 if (!ksocknal_data.ksnd_shuttingdown &&
2706 list_empty(&ksocknal_data.ksnd_deathrow_conns) &&
2707 list_empty(&ksocknal_data.ksnd_zombie_conns))
2708 schedule_timeout(cfs_time_seconds(timeout));
2710 set_current_state(TASK_RUNNING);
2711 remove_wait_queue(&ksocknal_data.ksnd_reaper_waitq, &wait);
2713 spin_lock_bh(&ksocknal_data.ksnd_reaper_lock);
2716 spin_unlock_bh(&ksocknal_data.ksnd_reaper_lock);
2718 ksocknal_thread_fini();