1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
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 Portals, http://www.sf.net/projects/sandiaportals/
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 ksock_tx_t *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 cfs_spin_lock(&ksocknal_data.ksnd_tx_lock);
40 if (!cfs_list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
41 tx = cfs_list_entry(ksocknal_data.ksnd_idle_noop_txs. \
42 next, ksock_tx_t, tx_list);
43 LASSERT(tx->tx_desc_size == size);
44 cfs_list_del(&tx->tx_list);
47 cfs_spin_unlock(&ksocknal_data.ksnd_tx_lock);
51 LIBCFS_ALLOC(tx, size);
56 cfs_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_desc_size = size;
62 cfs_atomic_inc(&ksocknal_data.ksnd_nactive_txs);
68 ksocknal_alloc_tx_noop(__u64 cookie, int nonblk)
72 tx = ksocknal_alloc_tx(KSOCK_MSG_NOOP, KSOCK_NOOP_TX_SIZE);
74 CERROR("Can't allocate noop tx desc\n");
79 tx->tx_lnetmsg = NULL;
82 tx->tx_iov = tx->tx_frags.virt.iov;
84 tx->tx_nonblk = nonblk;
86 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_NOOP);
87 tx->tx_msg.ksm_zc_cookies[1] = cookie;
94 ksocknal_free_tx (ksock_tx_t *tx)
96 cfs_atomic_dec(&ksocknal_data.ksnd_nactive_txs);
98 if (tx->tx_lnetmsg == NULL && tx->tx_desc_size == KSOCK_NOOP_TX_SIZE) {
100 cfs_spin_lock(&ksocknal_data.ksnd_tx_lock);
102 cfs_list_add(&tx->tx_list, &ksocknal_data.ksnd_idle_noop_txs);
104 cfs_spin_unlock(&ksocknal_data.ksnd_tx_lock);
106 LIBCFS_FREE(tx, tx->tx_desc_size);
111 ksocknal_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
113 struct iovec *iov = tx->tx_iov;
117 LASSERT (tx->tx_niov > 0);
119 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
120 rc = ksocknal_lib_send_iov(conn, tx);
122 if (rc <= 0) /* sent nothing? */
126 LASSERT (nob <= tx->tx_resid);
131 LASSERT (tx->tx_niov > 0);
133 if (nob < (int) iov->iov_len) {
134 iov->iov_base = (void *)((char *)iov->iov_base + nob);
148 ksocknal_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
150 lnet_kiov_t *kiov = tx->tx_kiov;
154 LASSERT (tx->tx_niov == 0);
155 LASSERT (tx->tx_nkiov > 0);
157 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
158 rc = ksocknal_lib_send_kiov(conn, tx);
160 if (rc <= 0) /* sent nothing? */
164 LASSERT (nob <= tx->tx_resid);
169 LASSERT(tx->tx_nkiov > 0);
171 if (nob < (int)kiov->kiov_len) {
172 kiov->kiov_offset += nob;
173 kiov->kiov_len -= nob;
177 nob -= (int)kiov->kiov_len;
178 tx->tx_kiov = ++kiov;
186 ksocknal_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
191 if (ksocknal_data.ksnd_stall_tx != 0) {
192 cfs_pause(cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
195 LASSERT (tx->tx_resid != 0);
197 rc = ksocknal_connsock_addref(conn);
199 LASSERT (conn->ksnc_closing);
204 if (ksocknal_data.ksnd_enomem_tx > 0) {
206 ksocknal_data.ksnd_enomem_tx--;
208 } else if (tx->tx_niov != 0) {
209 rc = ksocknal_send_iov (conn, tx);
211 rc = ksocknal_send_kiov (conn, tx);
214 bufnob = libcfs_sock_wmem_queued(conn->ksnc_sock);
215 if (rc > 0) /* sent something? */
216 conn->ksnc_tx_bufnob += rc; /* account it */
218 if (bufnob < conn->ksnc_tx_bufnob) {
219 /* allocated send buffer bytes < computed; infer
220 * something got ACKed */
221 conn->ksnc_tx_deadline =
222 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
223 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
224 conn->ksnc_tx_bufnob = bufnob;
228 if (rc <= 0) { /* Didn't write anything? */
230 if (rc == 0) /* some stacks return 0 instead of -EAGAIN */
233 /* Check if EAGAIN is due to memory pressure */
234 if(rc == -EAGAIN && ksocknal_lib_memory_pressure(conn))
240 /* socket's wmem_queued now includes 'rc' bytes */
241 cfs_atomic_sub (rc, &conn->ksnc_tx_nob);
244 } while (tx->tx_resid != 0);
246 ksocknal_connsock_decref(conn);
251 ksocknal_recv_iov (ksock_conn_t *conn)
253 struct iovec *iov = conn->ksnc_rx_iov;
257 LASSERT (conn->ksnc_rx_niov > 0);
259 /* Never touch conn->ksnc_rx_iov or change connection
260 * status inside ksocknal_lib_recv_iov */
261 rc = ksocknal_lib_recv_iov(conn);
266 /* received something... */
269 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
270 conn->ksnc_rx_deadline =
271 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
272 cfs_mb(); /* order with setting rx_started */
273 conn->ksnc_rx_started = 1;
275 conn->ksnc_rx_nob_wanted -= nob;
276 conn->ksnc_rx_nob_left -= nob;
279 LASSERT (conn->ksnc_rx_niov > 0);
281 if (nob < (int)iov->iov_len) {
283 iov->iov_base = (void *)((char *)iov->iov_base + nob);
288 conn->ksnc_rx_iov = ++iov;
289 conn->ksnc_rx_niov--;
296 ksocknal_recv_kiov (ksock_conn_t *conn)
298 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
301 LASSERT (conn->ksnc_rx_nkiov > 0);
303 /* Never touch conn->ksnc_rx_kiov or change connection
304 * status inside ksocknal_lib_recv_iov */
305 rc = ksocknal_lib_recv_kiov(conn);
310 /* received something... */
313 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
314 conn->ksnc_rx_deadline =
315 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
316 cfs_mb(); /* order with setting rx_started */
317 conn->ksnc_rx_started = 1;
319 conn->ksnc_rx_nob_wanted -= nob;
320 conn->ksnc_rx_nob_left -= nob;
323 LASSERT (conn->ksnc_rx_nkiov > 0);
325 if (nob < (int) kiov->kiov_len) {
326 kiov->kiov_offset += nob;
327 kiov->kiov_len -= nob;
331 nob -= kiov->kiov_len;
332 conn->ksnc_rx_kiov = ++kiov;
333 conn->ksnc_rx_nkiov--;
340 ksocknal_receive (ksock_conn_t *conn)
342 /* Return 1 on success, 0 on EOF, < 0 on error.
343 * Caller checks ksnc_rx_nob_wanted to determine
344 * progress/completion. */
348 if (ksocknal_data.ksnd_stall_rx != 0) {
349 cfs_pause(cfs_time_seconds (ksocknal_data.ksnd_stall_rx));
352 rc = ksocknal_connsock_addref(conn);
354 LASSERT (conn->ksnc_closing);
359 if (conn->ksnc_rx_niov != 0)
360 rc = ksocknal_recv_iov (conn);
362 rc = ksocknal_recv_kiov (conn);
365 /* error/EOF or partial receive */
368 } else if (rc == 0 && conn->ksnc_rx_started) {
369 /* EOF in the middle of a message */
375 /* Completed a fragment */
377 if (conn->ksnc_rx_nob_wanted == 0) {
383 ksocknal_connsock_decref(conn);
388 ksocknal_tx_done (lnet_ni_t *ni, ksock_tx_t *tx)
390 lnet_msg_t *lnetmsg = tx->tx_lnetmsg;
391 int rc = (tx->tx_resid == 0 && !tx->tx_zc_aborted) ? 0 : -EIO;
394 LASSERT(ni != NULL || tx->tx_conn != NULL);
396 if (tx->tx_conn != NULL)
397 ksocknal_conn_decref(tx->tx_conn);
399 if (ni == NULL && tx->tx_conn != NULL)
400 ni = tx->tx_conn->ksnc_peer->ksnp_ni;
402 ksocknal_free_tx (tx);
403 if (lnetmsg != NULL) /* KSOCK_MSG_NOOP go without lnetmsg */
404 lnet_finalize (ni, lnetmsg, rc);
410 ksocknal_txlist_done (lnet_ni_t *ni, cfs_list_t *txlist, int error)
414 while (!cfs_list_empty (txlist)) {
415 tx = cfs_list_entry (txlist->next, ksock_tx_t, tx_list);
417 if (error && tx->tx_lnetmsg != NULL) {
418 CNETERR("Deleting packet type %d len %d %s->%s\n",
419 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.type),
420 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.payload_length),
421 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
422 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
424 CNETERR("Deleting noop packet\n");
427 cfs_list_del (&tx->tx_list);
429 LASSERT (cfs_atomic_read(&tx->tx_refcount) == 1);
430 ksocknal_tx_done (ni, tx);
435 ksocknal_check_zc_req(ksock_tx_t *tx)
437 ksock_conn_t *conn = tx->tx_conn;
438 ksock_peer_t *peer = conn->ksnc_peer;
440 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
441 * to ksnp_zc_req_list if some fragment of this message should be sent
442 * zero-copy. Our peer will send an ACK containing this cookie when
443 * she has received this message to tell us we can signal completion.
444 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
445 * ksnp_zc_req_list. */
446 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
447 LASSERT (tx->tx_zc_capable);
449 tx->tx_zc_checked = 1;
451 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
452 !conn->ksnc_zc_capable)
455 /* assign cookie and queue tx to pending list, it will be released when
456 * a matching ack is received. See ksocknal_handle_zcack() */
458 ksocknal_tx_addref(tx);
460 cfs_spin_lock(&peer->ksnp_lock);
462 /* ZC_REQ is going to be pinned to the peer */
464 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
466 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
468 tx->tx_msg.ksm_zc_cookies[0] = peer->ksnp_zc_next_cookie++;
470 if (peer->ksnp_zc_next_cookie == 0)
471 peer->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
473 cfs_list_add_tail(&tx->tx_zc_list, &peer->ksnp_zc_req_list);
475 cfs_spin_unlock(&peer->ksnp_lock);
479 ksocknal_uncheck_zc_req(ksock_tx_t *tx)
481 ksock_peer_t *peer = tx->tx_conn->ksnc_peer;
483 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
484 LASSERT (tx->tx_zc_capable);
486 tx->tx_zc_checked = 0;
488 cfs_spin_lock(&peer->ksnp_lock);
490 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
491 /* Not waiting for an ACK */
492 cfs_spin_unlock(&peer->ksnp_lock);
496 tx->tx_msg.ksm_zc_cookies[0] = 0;
497 cfs_list_del(&tx->tx_zc_list);
499 cfs_spin_unlock(&peer->ksnp_lock);
501 ksocknal_tx_decref(tx);
505 ksocknal_process_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
509 if (tx->tx_zc_capable && !tx->tx_zc_checked)
510 ksocknal_check_zc_req(tx);
512 rc = ksocknal_transmit (conn, tx);
514 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
516 if (tx->tx_resid == 0) {
517 /* Sent everything OK */
529 counter++; /* exponential backoff warnings */
530 if ((counter & (-counter)) == counter)
531 CWARN("%u ENOMEM tx %p (%u allocated)\n",
532 counter, conn, cfs_atomic_read(&libcfs_kmemory));
534 /* Queue on ksnd_enomem_conns for retry after a timeout */
535 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
537 /* enomem list takes over scheduler's ref... */
538 LASSERT (conn->ksnc_tx_scheduled);
539 cfs_list_add_tail(&conn->ksnc_tx_list,
540 &ksocknal_data.ksnd_enomem_conns);
541 if (!cfs_time_aftereq(cfs_time_add(cfs_time_current(),
542 SOCKNAL_ENOMEM_RETRY),
543 ksocknal_data.ksnd_reaper_waketime))
544 cfs_waitq_signal (&ksocknal_data.ksnd_reaper_waitq);
546 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
553 if (!conn->ksnc_closing) {
556 LCONSOLE_WARN("Host %u.%u.%u.%u reset our connection "
557 "while we were sending data; it may have "
559 HIPQUAD(conn->ksnc_ipaddr));
562 LCONSOLE_WARN("There was an unexpected network error "
563 "while writing to %u.%u.%u.%u: %d.\n",
564 HIPQUAD(conn->ksnc_ipaddr), rc);
567 CDEBUG(D_NET, "[%p] Error %d on write to %s"
568 " ip %d.%d.%d.%d:%d\n", conn, rc,
569 libcfs_id2str(conn->ksnc_peer->ksnp_id),
570 HIPQUAD(conn->ksnc_ipaddr),
574 if (tx->tx_zc_checked)
575 ksocknal_uncheck_zc_req(tx);
577 /* it's not an error if conn is being closed */
578 ksocknal_close_conn_and_siblings (conn,
579 (conn->ksnc_closing) ? 0 : rc);
585 ksocknal_launch_connection_locked (ksock_route_t *route)
588 /* called holding write lock on ksnd_global_lock */
590 LASSERT (!route->ksnr_scheduled);
591 LASSERT (!route->ksnr_connecting);
592 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
594 route->ksnr_scheduled = 1; /* scheduling conn for connd */
595 ksocknal_route_addref(route); /* extra ref for connd */
597 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
599 cfs_list_add_tail (&route->ksnr_connd_list,
600 &ksocknal_data.ksnd_connd_routes);
601 cfs_waitq_signal (&ksocknal_data.ksnd_connd_waitq);
603 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
607 ksocknal_launch_all_connections_locked (ksock_peer_t *peer)
609 ksock_route_t *route;
611 /* called holding write lock on ksnd_global_lock */
613 /* launch any/all connections that need it */
614 route = ksocknal_find_connectable_route_locked(peer);
618 ksocknal_launch_connection_locked(route);
623 ksocknal_find_conn_locked(ksock_peer_t *peer, ksock_tx_t *tx, int nonblk)
627 ksock_conn_t *typed = NULL;
628 ksock_conn_t *fallback = NULL;
632 cfs_list_for_each (tmp, &peer->ksnp_conns) {
633 ksock_conn_t *c = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
634 int nob = cfs_atomic_read(&c->ksnc_tx_nob) +
635 libcfs_sock_wmem_queued(c->ksnc_sock);
638 LASSERT (!c->ksnc_closing);
639 LASSERT (c->ksnc_proto != NULL &&
640 c->ksnc_proto->pro_match_tx != NULL);
642 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
647 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
650 case SOCKNAL_MATCH_YES: /* typed connection */
651 if (typed == NULL || tnob > nob ||
652 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
653 cfs_time_after(typed->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
659 case SOCKNAL_MATCH_MAY: /* fallback connection */
660 if (fallback == NULL || fnob > nob ||
661 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
662 cfs_time_after(fallback->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
670 /* prefer the typed selection */
671 conn = (typed != NULL) ? typed : fallback;
674 conn->ksnc_tx_last_post = cfs_time_current();
680 ksocknal_tx_prep(ksock_conn_t *conn, ksock_tx_t *tx)
682 conn->ksnc_proto->pro_pack(tx);
684 cfs_atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
685 ksocknal_conn_addref(conn); /* +1 ref for tx */
690 ksocknal_queue_tx_locked (ksock_tx_t *tx, ksock_conn_t *conn)
692 ksock_sched_t *sched = conn->ksnc_scheduler;
693 ksock_msg_t *msg = &tx->tx_msg;
694 ksock_tx_t *ztx = NULL;
697 /* called holding global lock (read or irq-write) and caller may
698 * not have dropped this lock between finding conn and calling me,
699 * so we don't need the {get,put}connsock dance to deref
701 LASSERT(!conn->ksnc_closing);
703 CDEBUG (D_NET, "Sending to %s ip %d.%d.%d.%d:%d\n",
704 libcfs_id2str(conn->ksnc_peer->ksnp_id),
705 HIPQUAD(conn->ksnc_ipaddr),
708 ksocknal_tx_prep(conn, tx);
710 /* Ensure the frags we've been given EXACTLY match the number of
711 * bytes we want to send. Many TCP/IP stacks disregard any total
712 * size parameters passed to them and just look at the frags.
714 * We always expect at least 1 mapped fragment containing the
715 * complete ksocknal message header. */
716 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
717 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
718 (unsigned int)tx->tx_nob);
719 LASSERT (tx->tx_niov >= 1);
720 LASSERT (tx->tx_resid == tx->tx_nob);
722 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
723 tx, (tx->tx_lnetmsg != NULL) ? tx->tx_lnetmsg->msg_hdr.type:
725 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
728 * FIXME: SOCK_WMEM_QUEUED and SOCK_ERROR could block in __DARWIN8__
729 * but they're used inside spinlocks a lot.
731 bufnob = libcfs_sock_wmem_queued(conn->ksnc_sock);
732 cfs_spin_lock_bh (&sched->kss_lock);
734 if (cfs_list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
735 /* First packet starts the timeout */
736 conn->ksnc_tx_deadline =
737 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
738 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
739 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
740 conn->ksnc_tx_bufnob = 0;
741 cfs_mb(); /* order with adding to tx_queue */
744 if (msg->ksm_type == KSOCK_MSG_NOOP) {
745 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
746 * on a normal packet so I don't need to send it */
747 LASSERT (msg->ksm_zc_cookies[1] != 0);
748 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
750 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
751 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
754 /* It's a normal packet - can it piggback a noop zc-ack that
755 * has been queued already? */
756 LASSERT (msg->ksm_zc_cookies[1] == 0);
757 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
759 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
760 /* ztx will be released later */
764 cfs_atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
765 cfs_list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
768 if (conn->ksnc_tx_ready && /* able to send */
769 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
770 /* +1 ref for scheduler */
771 ksocknal_conn_addref(conn);
772 cfs_list_add_tail (&conn->ksnc_tx_list,
773 &sched->kss_tx_conns);
774 conn->ksnc_tx_scheduled = 1;
775 cfs_waitq_signal (&sched->kss_waitq);
778 cfs_spin_unlock_bh (&sched->kss_lock);
783 ksocknal_find_connectable_route_locked (ksock_peer_t *peer)
785 cfs_time_t now = cfs_time_current();
787 ksock_route_t *route;
789 cfs_list_for_each (tmp, &peer->ksnp_routes) {
790 route = cfs_list_entry (tmp, ksock_route_t, ksnr_list);
792 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
794 if (route->ksnr_scheduled) /* connections being established */
797 /* all route types connected ? */
798 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
801 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
802 cfs_time_aftereq(now, route->ksnr_timeout))) {
804 "Too soon to retry route %u.%u.%u.%u "
805 "(cnted %d, interval %ld, %ld secs later)\n",
806 HIPQUAD(route->ksnr_ipaddr),
807 route->ksnr_connected,
808 route->ksnr_retry_interval,
809 cfs_duration_sec(route->ksnr_timeout - now));
820 ksocknal_find_connecting_route_locked (ksock_peer_t *peer)
823 ksock_route_t *route;
825 cfs_list_for_each (tmp, &peer->ksnp_routes) {
826 route = cfs_list_entry (tmp, ksock_route_t, ksnr_list);
828 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
830 if (route->ksnr_scheduled)
838 ksocknal_launch_packet (lnet_ni_t *ni, ksock_tx_t *tx, lnet_process_id_t id)
842 cfs_rwlock_t *g_lock;
846 LASSERT (tx->tx_conn == NULL);
848 g_lock = &ksocknal_data.ksnd_global_lock;
850 for (retry = 0;; retry = 1) {
851 cfs_read_lock (g_lock);
852 peer = ksocknal_find_peer_locked(ni, id);
854 if (ksocknal_find_connectable_route_locked(peer) == NULL) {
855 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
857 /* I've got no routes that need to be
858 * connecting and I do have an actual
860 ksocknal_queue_tx_locked (tx, conn);
861 cfs_read_unlock (g_lock);
867 /* I'll need a write lock... */
868 cfs_read_unlock (g_lock);
870 cfs_write_lock_bh (g_lock);
872 peer = ksocknal_find_peer_locked(ni, id);
876 cfs_write_unlock_bh (g_lock);
878 if ((id.pid & LNET_PID_USERFLAG) != 0) {
879 CERROR("Refusing to create a connection to "
880 "userspace process %s\n", libcfs_id2str(id));
881 return -EHOSTUNREACH;
885 CERROR("Can't find peer %s\n", libcfs_id2str(id));
886 return -EHOSTUNREACH;
889 rc = ksocknal_add_peer(ni, id,
890 LNET_NIDADDR(id.nid),
891 lnet_acceptor_port());
893 CERROR("Can't add peer %s: %d\n",
894 libcfs_id2str(id), rc);
899 ksocknal_launch_all_connections_locked(peer);
901 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
903 /* Connection exists; queue message on it */
904 ksocknal_queue_tx_locked (tx, conn);
905 cfs_write_unlock_bh (g_lock);
909 if (peer->ksnp_accepting > 0 ||
910 ksocknal_find_connecting_route_locked (peer) != NULL) {
911 /* the message is going to be pinned to the peer */
913 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
915 /* Queue the message until a connection is established */
916 cfs_list_add_tail (&tx->tx_list, &peer->ksnp_tx_queue);
917 cfs_write_unlock_bh (g_lock);
921 cfs_write_unlock_bh (g_lock);
923 /* NB Routes may be ignored if connections to them failed recently */
924 CNETERR("No usable routes to %s\n", libcfs_id2str(id));
925 return (-EHOSTUNREACH);
929 ksocknal_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
932 int type = lntmsg->msg_type;
933 lnet_process_id_t target = lntmsg->msg_target;
934 unsigned int payload_niov = lntmsg->msg_niov;
935 struct iovec *payload_iov = lntmsg->msg_iov;
936 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
937 unsigned int payload_offset = lntmsg->msg_offset;
938 unsigned int payload_nob = lntmsg->msg_len;
943 /* NB 'private' is different depending on what we're sending.
944 * Just ignore it... */
946 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
947 payload_nob, payload_niov, libcfs_id2str(target));
949 LASSERT (payload_nob == 0 || payload_niov > 0);
950 LASSERT (payload_niov <= LNET_MAX_IOV);
951 /* payload is either all vaddrs or all pages */
952 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
953 LASSERT (!cfs_in_interrupt ());
955 if (payload_iov != NULL)
956 desc_size = offsetof(ksock_tx_t,
957 tx_frags.virt.iov[1 + payload_niov]);
959 desc_size = offsetof(ksock_tx_t,
960 tx_frags.paged.kiov[payload_niov]);
962 if (lntmsg->msg_vmflush)
963 mpflag = cfs_memory_pressure_get_and_set();
964 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
966 CERROR("Can't allocate tx desc type %d size %d\n",
968 if (lntmsg->msg_vmflush)
969 cfs_memory_pressure_restore(mpflag);
973 tx->tx_conn = NULL; /* set when assigned a conn */
974 tx->tx_lnetmsg = lntmsg;
976 if (payload_iov != NULL) {
979 tx->tx_iov = tx->tx_frags.virt.iov;
981 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
982 payload_niov, payload_iov,
983 payload_offset, payload_nob);
986 tx->tx_iov = &tx->tx_frags.paged.iov;
987 tx->tx_kiov = tx->tx_frags.paged.kiov;
988 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
989 payload_niov, payload_kiov,
990 payload_offset, payload_nob);
992 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
993 tx->tx_zc_capable = 1;
996 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_LNET);
998 /* The first fragment will be set later in pro_pack */
999 rc = ksocknal_launch_packet(ni, tx, target);
1000 if (lntmsg->msg_vmflush)
1001 cfs_memory_pressure_restore(mpflag);
1005 ksocknal_free_tx(tx);
1010 ksocknal_thread_start (int (*fn)(void *arg), void *arg)
1012 long pid = cfs_kernel_thread (fn, arg, 0);
1017 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1018 ksocknal_data.ksnd_nthreads++;
1019 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1024 ksocknal_thread_fini (void)
1026 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1027 ksocknal_data.ksnd_nthreads--;
1028 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1032 ksocknal_new_packet (ksock_conn_t *conn, int nob_to_skip)
1034 static char ksocknal_slop_buffer[4096];
1040 LASSERT(conn->ksnc_proto != NULL);
1042 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1043 /* Remind the socket to ack eagerly... */
1044 ksocknal_lib_eager_ack(conn);
1047 if (nob_to_skip == 0) { /* right at next packet boundary now */
1048 conn->ksnc_rx_started = 0;
1049 cfs_mb(); /* racing with timeout thread */
1051 switch (conn->ksnc_proto->pro_version) {
1052 case KSOCK_PROTO_V2:
1053 case KSOCK_PROTO_V3:
1054 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1055 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1056 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1058 conn->ksnc_rx_nob_wanted = offsetof(ksock_msg_t, ksm_u);
1059 conn->ksnc_rx_nob_left = offsetof(ksock_msg_t, ksm_u);
1060 conn->ksnc_rx_iov[0].iov_len = offsetof(ksock_msg_t, ksm_u);
1063 case KSOCK_PROTO_V1:
1064 /* Receiving bare lnet_hdr_t */
1065 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1066 conn->ksnc_rx_nob_wanted = sizeof(lnet_hdr_t);
1067 conn->ksnc_rx_nob_left = sizeof(lnet_hdr_t);
1069 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1070 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1071 conn->ksnc_rx_iov[0].iov_len = sizeof (lnet_hdr_t);
1077 conn->ksnc_rx_niov = 1;
1079 conn->ksnc_rx_kiov = NULL;
1080 conn->ksnc_rx_nkiov = 0;
1081 conn->ksnc_rx_csum = ~0;
1085 /* Set up to skip as much as possible now. If there's more left
1086 * (ran out of iov entries) we'll get called again */
1088 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1089 conn->ksnc_rx_nob_left = nob_to_skip;
1090 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1095 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1097 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1098 conn->ksnc_rx_iov[niov].iov_len = nob;
1103 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1104 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof (struct iovec));
1106 conn->ksnc_rx_niov = niov;
1107 conn->ksnc_rx_kiov = NULL;
1108 conn->ksnc_rx_nkiov = 0;
1109 conn->ksnc_rx_nob_wanted = skipped;
1114 ksocknal_process_receive (ksock_conn_t *conn)
1117 lnet_process_id_t *id;
1120 LASSERT (cfs_atomic_read(&conn->ksnc_conn_refcount) > 0);
1122 /* NB: sched lock NOT held */
1123 /* SOCKNAL_RX_LNET_HEADER is here for backward compatability */
1124 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1125 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1126 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1127 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1129 if (conn->ksnc_rx_nob_wanted != 0) {
1130 rc = ksocknal_receive(conn);
1133 LASSERT (rc != -EAGAIN);
1136 CDEBUG (D_NET, "[%p] EOF from %s"
1137 " ip %d.%d.%d.%d:%d\n", conn,
1138 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1139 HIPQUAD(conn->ksnc_ipaddr),
1141 else if (!conn->ksnc_closing)
1142 CERROR ("[%p] Error %d on read from %s"
1143 " ip %d.%d.%d.%d:%d\n",
1145 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1146 HIPQUAD(conn->ksnc_ipaddr),
1149 /* it's not an error if conn is being closed */
1150 ksocknal_close_conn_and_siblings (conn,
1151 (conn->ksnc_closing) ? 0 : rc);
1152 return (rc == 0 ? -ESHUTDOWN : rc);
1155 if (conn->ksnc_rx_nob_wanted != 0) {
1160 switch (conn->ksnc_rx_state) {
1161 case SOCKNAL_RX_KSM_HEADER:
1162 if (conn->ksnc_flip) {
1163 __swab32s(&conn->ksnc_msg.ksm_type);
1164 __swab32s(&conn->ksnc_msg.ksm_csum);
1165 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1166 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1169 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1170 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1171 CERROR("%s: Unknown message type: %x\n",
1172 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1173 conn->ksnc_msg.ksm_type);
1174 ksocknal_new_packet(conn, 0);
1175 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1179 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1180 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1181 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1182 /* NOOP Checksum error */
1183 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1184 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1185 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1186 ksocknal_new_packet(conn, 0);
1187 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1191 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1194 LASSERT (conn->ksnc_proto != &ksocknal_protocol_v1x);
1196 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1197 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1199 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1200 conn->ksnc_msg.ksm_zc_cookies[1]);
1203 CERROR("%s: Unknown ZC-ACK cookie: "LPU64", "LPU64"\n",
1204 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1205 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1206 ksocknal_new_packet(conn, 0);
1207 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1212 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1213 ksocknal_new_packet (conn, 0);
1214 return 0; /* NOOP is done and just return */
1217 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1218 conn->ksnc_rx_nob_wanted = sizeof(ksock_lnet_msg_t);
1219 conn->ksnc_rx_nob_left = sizeof(ksock_lnet_msg_t);
1221 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1222 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1223 conn->ksnc_rx_iov[0].iov_len = sizeof(ksock_lnet_msg_t);
1225 conn->ksnc_rx_niov = 1;
1226 conn->ksnc_rx_kiov = NULL;
1227 conn->ksnc_rx_nkiov = 0;
1229 goto again; /* read lnet header now */
1231 case SOCKNAL_RX_LNET_HEADER:
1232 /* unpack message header */
1233 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1235 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1236 /* Userspace peer */
1237 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1238 id = &conn->ksnc_peer->ksnp_id;
1240 /* Substitute process ID assigned at connection time */
1241 lhdr->src_pid = cpu_to_le32(id->pid);
1242 lhdr->src_nid = cpu_to_le64(id->nid);
1245 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1246 ksocknal_conn_addref(conn); /* ++ref while parsing */
1248 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1249 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1250 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1252 /* I just received garbage: give up on this conn */
1253 ksocknal_new_packet(conn, 0);
1254 ksocknal_close_conn_and_siblings (conn, rc);
1255 ksocknal_conn_decref(conn);
1259 /* I'm racing with ksocknal_recv() */
1260 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1261 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1263 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1266 /* ksocknal_recv() got called */
1269 case SOCKNAL_RX_LNET_PAYLOAD:
1270 /* payload all received */
1273 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1274 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1275 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1276 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1277 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1278 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1282 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1283 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1285 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1286 id = &conn->ksnc_peer->ksnp_id;
1288 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1289 conn->ksnc_msg.ksm_zc_cookies[0],
1290 *ksocknal_tunables.ksnd_nonblk_zcack ||
1291 le64_to_cpu(lhdr->src_nid) != id->nid);
1294 lnet_finalize(conn->ksnc_peer->ksnp_ni, conn->ksnc_cookie, rc);
1297 ksocknal_new_packet(conn, 0);
1298 ksocknal_close_conn_and_siblings (conn, rc);
1303 case SOCKNAL_RX_SLOP:
1304 /* starting new packet? */
1305 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1306 return 0; /* come back later */
1307 goto again; /* try to finish reading slop now */
1315 return (-EINVAL); /* keep gcc happy */
1319 ksocknal_recv (lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
1320 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1321 unsigned int offset, unsigned int mlen, unsigned int rlen)
1323 ksock_conn_t *conn = (ksock_conn_t *)private;
1324 ksock_sched_t *sched = conn->ksnc_scheduler;
1326 LASSERT (mlen <= rlen);
1327 LASSERT (niov <= LNET_MAX_IOV);
1329 conn->ksnc_cookie = msg;
1330 conn->ksnc_rx_nob_wanted = mlen;
1331 conn->ksnc_rx_nob_left = rlen;
1333 if (mlen == 0 || iov != NULL) {
1334 conn->ksnc_rx_nkiov = 0;
1335 conn->ksnc_rx_kiov = NULL;
1336 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1337 conn->ksnc_rx_niov =
1338 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1339 niov, iov, offset, mlen);
1341 conn->ksnc_rx_niov = 0;
1342 conn->ksnc_rx_iov = NULL;
1343 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1344 conn->ksnc_rx_nkiov =
1345 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1346 niov, kiov, offset, mlen);
1350 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1351 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1353 LASSERT (conn->ksnc_rx_scheduled);
1355 cfs_spin_lock_bh (&sched->kss_lock);
1357 switch (conn->ksnc_rx_state) {
1358 case SOCKNAL_RX_PARSE_WAIT:
1359 cfs_list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1360 cfs_waitq_signal (&sched->kss_waitq);
1361 LASSERT (conn->ksnc_rx_ready);
1364 case SOCKNAL_RX_PARSE:
1365 /* scheduler hasn't noticed I'm parsing yet */
1369 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1371 cfs_spin_unlock_bh (&sched->kss_lock);
1372 ksocknal_conn_decref(conn);
1377 ksocknal_sched_cansleep(ksock_sched_t *sched)
1381 cfs_spin_lock_bh (&sched->kss_lock);
1383 rc = (!ksocknal_data.ksnd_shuttingdown &&
1384 cfs_list_empty(&sched->kss_rx_conns) &&
1385 cfs_list_empty(&sched->kss_tx_conns));
1387 cfs_spin_unlock_bh (&sched->kss_lock);
1391 int ksocknal_scheduler (void *arg)
1393 ksock_sched_t *sched = (ksock_sched_t *)arg;
1398 int id = (int)(sched - ksocknal_data.ksnd_schedulers);
1401 snprintf (name, sizeof (name),"socknal_sd%02d", id);
1402 cfs_daemonize (name);
1403 cfs_block_allsigs ();
1405 if (ksocknal_lib_bind_thread_to_cpu(id))
1406 CERROR ("Can't set CPU affinity for %s to %d\n", name, id);
1408 cfs_spin_lock_bh (&sched->kss_lock);
1410 while (!ksocknal_data.ksnd_shuttingdown) {
1411 int did_something = 0;
1413 /* Ensure I progress everything semi-fairly */
1415 if (!cfs_list_empty (&sched->kss_rx_conns)) {
1416 conn = cfs_list_entry(sched->kss_rx_conns.next,
1417 ksock_conn_t, ksnc_rx_list);
1418 cfs_list_del(&conn->ksnc_rx_list);
1420 LASSERT(conn->ksnc_rx_scheduled);
1421 LASSERT(conn->ksnc_rx_ready);
1423 /* clear rx_ready in case receive isn't complete.
1424 * Do it BEFORE we call process_recv, since
1425 * data_ready can set it any time after we release
1427 conn->ksnc_rx_ready = 0;
1428 cfs_spin_unlock_bh (&sched->kss_lock);
1430 rc = ksocknal_process_receive(conn);
1432 cfs_spin_lock_bh (&sched->kss_lock);
1434 /* I'm the only one that can clear this flag */
1435 LASSERT(conn->ksnc_rx_scheduled);
1437 /* Did process_receive get everything it wanted? */
1439 conn->ksnc_rx_ready = 1;
1441 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1442 /* Conn blocked waiting for ksocknal_recv()
1443 * I change its state (under lock) to signal
1444 * it can be rescheduled */
1445 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1446 } else if (conn->ksnc_rx_ready) {
1447 /* reschedule for rx */
1448 cfs_list_add_tail (&conn->ksnc_rx_list,
1449 &sched->kss_rx_conns);
1451 conn->ksnc_rx_scheduled = 0;
1453 ksocknal_conn_decref(conn);
1459 if (!cfs_list_empty (&sched->kss_tx_conns)) {
1460 CFS_LIST_HEAD (zlist);
1462 if (!cfs_list_empty(&sched->kss_zombie_noop_txs)) {
1463 cfs_list_add(&zlist,
1464 &sched->kss_zombie_noop_txs);
1465 cfs_list_del_init(&sched->kss_zombie_noop_txs);
1468 conn = cfs_list_entry(sched->kss_tx_conns.next,
1469 ksock_conn_t, ksnc_tx_list);
1470 cfs_list_del (&conn->ksnc_tx_list);
1472 LASSERT(conn->ksnc_tx_scheduled);
1473 LASSERT(conn->ksnc_tx_ready);
1474 LASSERT(!cfs_list_empty(&conn->ksnc_tx_queue));
1476 tx = cfs_list_entry(conn->ksnc_tx_queue.next,
1477 ksock_tx_t, tx_list);
1479 if (conn->ksnc_tx_carrier == tx)
1480 ksocknal_next_tx_carrier(conn);
1482 /* dequeue now so empty list => more to send */
1483 cfs_list_del(&tx->tx_list);
1485 /* Clear tx_ready in case send isn't complete. Do
1486 * it BEFORE we call process_transmit, since
1487 * write_space can set it any time after we release
1489 conn->ksnc_tx_ready = 0;
1490 cfs_spin_unlock_bh (&sched->kss_lock);
1492 if (!cfs_list_empty(&zlist)) {
1493 /* free zombie noop txs, it's fast because
1494 * noop txs are just put in freelist */
1495 ksocknal_txlist_done(NULL, &zlist, 0);
1498 rc = ksocknal_process_transmit(conn, tx);
1500 if (rc == -ENOMEM || rc == -EAGAIN) {
1501 /* Incomplete send: replace tx on HEAD of tx_queue */
1502 cfs_spin_lock_bh (&sched->kss_lock);
1503 cfs_list_add (&tx->tx_list,
1504 &conn->ksnc_tx_queue);
1506 /* Complete send; tx -ref */
1507 ksocknal_tx_decref (tx);
1509 cfs_spin_lock_bh (&sched->kss_lock);
1510 /* assume space for more */
1511 conn->ksnc_tx_ready = 1;
1514 if (rc == -ENOMEM) {
1515 /* Do nothing; after a short timeout, this
1516 * conn will be reposted on kss_tx_conns. */
1517 } else if (conn->ksnc_tx_ready &&
1518 !cfs_list_empty (&conn->ksnc_tx_queue)) {
1519 /* reschedule for tx */
1520 cfs_list_add_tail (&conn->ksnc_tx_list,
1521 &sched->kss_tx_conns);
1523 conn->ksnc_tx_scheduled = 0;
1525 ksocknal_conn_decref(conn);
1530 if (!did_something || /* nothing to do */
1531 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1532 cfs_spin_unlock_bh (&sched->kss_lock);
1536 if (!did_something) { /* wait for something to do */
1537 cfs_wait_event_interruptible_exclusive(
1539 !ksocknal_sched_cansleep(sched), rc);
1545 cfs_spin_lock_bh (&sched->kss_lock);
1549 cfs_spin_unlock_bh (&sched->kss_lock);
1550 ksocknal_thread_fini ();
1555 * Add connection to kss_rx_conns of scheduler
1556 * and wakeup the scheduler.
1558 void ksocknal_read_callback (ksock_conn_t *conn)
1560 ksock_sched_t *sched;
1563 sched = conn->ksnc_scheduler;
1565 cfs_spin_lock_bh (&sched->kss_lock);
1567 conn->ksnc_rx_ready = 1;
1569 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1570 cfs_list_add_tail(&conn->ksnc_rx_list,
1571 &sched->kss_rx_conns);
1572 conn->ksnc_rx_scheduled = 1;
1573 /* extra ref for scheduler */
1574 ksocknal_conn_addref(conn);
1576 cfs_waitq_signal (&sched->kss_waitq);
1578 cfs_spin_unlock_bh (&sched->kss_lock);
1584 * Add connection to kss_tx_conns of scheduler
1585 * and wakeup the scheduler.
1587 void ksocknal_write_callback (ksock_conn_t *conn)
1589 ksock_sched_t *sched;
1592 sched = conn->ksnc_scheduler;
1594 cfs_spin_lock_bh (&sched->kss_lock);
1596 conn->ksnc_tx_ready = 1;
1598 if (!conn->ksnc_tx_scheduled && // not being progressed
1599 !cfs_list_empty(&conn->ksnc_tx_queue)){//packets to send
1600 cfs_list_add_tail (&conn->ksnc_tx_list,
1601 &sched->kss_tx_conns);
1602 conn->ksnc_tx_scheduled = 1;
1603 /* extra ref for scheduler */
1604 ksocknal_conn_addref(conn);
1606 cfs_waitq_signal (&sched->kss_waitq);
1609 cfs_spin_unlock_bh (&sched->kss_lock);
1615 ksocknal_parse_proto_version (ksock_hello_msg_t *hello)
1619 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1620 version = hello->kshm_version;
1621 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1622 version = __swab32(hello->kshm_version);
1625 #if SOCKNAL_VERSION_DEBUG
1626 if (*ksocknal_tunables.ksnd_protocol == 1)
1629 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1630 version == KSOCK_PROTO_V3)
1633 if (version == KSOCK_PROTO_V2)
1634 return &ksocknal_protocol_v2x;
1636 if (version == KSOCK_PROTO_V3)
1637 return &ksocknal_protocol_v3x;
1642 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1643 lnet_magicversion_t *hmv = (lnet_magicversion_t *)hello;
1645 CLASSERT (sizeof (lnet_magicversion_t) ==
1646 offsetof (ksock_hello_msg_t, kshm_src_nid));
1648 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1649 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1650 return &ksocknal_protocol_v1x;
1657 ksocknal_send_hello (lnet_ni_t *ni, ksock_conn_t *conn,
1658 lnet_nid_t peer_nid, ksock_hello_msg_t *hello)
1660 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1661 ksock_net_t *net = (ksock_net_t *)ni->ni_data;
1663 LASSERT (hello->kshm_nips <= LNET_MAX_INTERFACES);
1665 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1666 LASSERT (conn->ksnc_proto != NULL);
1668 hello->kshm_src_nid = ni->ni_nid;
1669 hello->kshm_dst_nid = peer_nid;
1670 hello->kshm_src_pid = the_lnet.ln_pid;
1672 hello->kshm_src_incarnation = net->ksnn_incarnation;
1673 hello->kshm_ctype = conn->ksnc_type;
1675 return conn->ksnc_proto->pro_send_hello(conn, hello);
1679 ksocknal_invert_type(int type)
1683 case SOCKLND_CONN_ANY:
1684 case SOCKLND_CONN_CONTROL:
1686 case SOCKLND_CONN_BULK_IN:
1687 return SOCKLND_CONN_BULK_OUT;
1688 case SOCKLND_CONN_BULK_OUT:
1689 return SOCKLND_CONN_BULK_IN;
1691 return (SOCKLND_CONN_NONE);
1696 ksocknal_recv_hello (lnet_ni_t *ni, ksock_conn_t *conn,
1697 ksock_hello_msg_t *hello, lnet_process_id_t *peerid,
1700 /* Return < 0 fatal error
1702 * EALREADY lost connection race
1703 * EPROTO protocol version mismatch
1705 cfs_socket_t *sock = conn->ksnc_sock;
1706 int active = (conn->ksnc_proto != NULL);
1710 ksock_proto_t *proto;
1711 lnet_process_id_t recv_id;
1713 /* socket type set on active connections - not set on passive */
1714 LASSERT (!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1716 timeout = active ? *ksocknal_tunables.ksnd_timeout :
1717 lnet_acceptor_timeout();
1719 rc = libcfs_sock_read(sock, &hello->kshm_magic, sizeof (hello->kshm_magic), timeout);
1721 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
1722 rc, HIPQUAD(conn->ksnc_ipaddr));
1727 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1728 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1729 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
1730 /* Unexpected magic! */
1731 CERROR ("Bad magic(1) %#08x (%#08x expected) from "
1732 "%u.%u.%u.%u\n", __cpu_to_le32 (hello->kshm_magic),
1733 LNET_PROTO_TCP_MAGIC,
1734 HIPQUAD(conn->ksnc_ipaddr));
1738 rc = libcfs_sock_read(sock, &hello->kshm_version,
1739 sizeof(hello->kshm_version), timeout);
1741 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
1742 rc, HIPQUAD(conn->ksnc_ipaddr));
1747 proto = ksocknal_parse_proto_version(hello);
1748 if (proto == NULL) {
1750 /* unknown protocol from peer, tell peer my protocol */
1751 conn->ksnc_proto = &ksocknal_protocol_v3x;
1752 #if SOCKNAL_VERSION_DEBUG
1753 if (*ksocknal_tunables.ksnd_protocol == 2)
1754 conn->ksnc_proto = &ksocknal_protocol_v2x;
1755 else if (*ksocknal_tunables.ksnd_protocol == 1)
1756 conn->ksnc_proto = &ksocknal_protocol_v1x;
1758 hello->kshm_nips = 0;
1759 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1762 CERROR ("Unknown protocol version (%d.x expected)"
1763 " from %u.%u.%u.%u\n",
1764 conn->ksnc_proto->pro_version,
1765 HIPQUAD(conn->ksnc_ipaddr));
1770 proto_match = (conn->ksnc_proto == proto);
1771 conn->ksnc_proto = proto;
1773 /* receive the rest of hello message anyway */
1774 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1776 CERROR("Error %d reading or checking hello from from %u.%u.%u.%u\n",
1777 rc, HIPQUAD(conn->ksnc_ipaddr));
1782 *incarnation = hello->kshm_src_incarnation;
1784 if (hello->kshm_src_nid == LNET_NID_ANY) {
1785 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
1786 "from %u.%u.%u.%u\n", HIPQUAD(conn->ksnc_ipaddr));
1791 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1792 /* Userspace NAL assigns peer process ID from socket */
1793 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1794 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
1796 recv_id.nid = hello->kshm_src_nid;
1797 recv_id.pid = hello->kshm_src_pid;
1803 /* peer determines type */
1804 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1805 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
1806 CERROR ("Unexpected type %d from %s ip %u.%u.%u.%u\n",
1807 hello->kshm_ctype, libcfs_id2str(*peerid),
1808 HIPQUAD(conn->ksnc_ipaddr));
1815 if (peerid->pid != recv_id.pid ||
1816 peerid->nid != recv_id.nid) {
1817 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1818 " %u.%u.%u.%u, but they claimed they were "
1819 "%s; please check your Lustre "
1821 libcfs_id2str(*peerid),
1822 HIPQUAD(conn->ksnc_ipaddr),
1823 libcfs_id2str(recv_id));
1827 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1828 /* Possible protocol mismatch or I lost the connection race */
1829 return proto_match ? EALREADY : EPROTO;
1832 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1833 CERROR ("Mismatched types: me %d, %s ip %u.%u.%u.%u %d\n",
1834 conn->ksnc_type, libcfs_id2str(*peerid),
1835 HIPQUAD(conn->ksnc_ipaddr),
1844 ksocknal_connect (ksock_route_t *route)
1846 CFS_LIST_HEAD (zombies);
1847 ksock_peer_t *peer = route->ksnr_peer;
1851 cfs_time_t deadline;
1852 int retry_later = 0;
1855 deadline = cfs_time_add(cfs_time_current(),
1856 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout));
1858 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1860 LASSERT (route->ksnr_scheduled);
1861 LASSERT (!route->ksnr_connecting);
1863 route->ksnr_connecting = 1;
1866 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1868 /* stop connecting if peer/route got closed under me, or
1869 * route got connected while queued */
1870 if (peer->ksnp_closing || route->ksnr_deleted ||
1876 /* reschedule if peer is connecting to me */
1877 if (peer->ksnp_accepting > 0) {
1879 "peer %s(%d) already connecting to me, retry later.\n",
1880 libcfs_nid2str(peer->ksnp_id.nid), peer->ksnp_accepting);
1884 if (retry_later) /* needs reschedule */
1887 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
1888 type = SOCKLND_CONN_ANY;
1889 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
1890 type = SOCKLND_CONN_CONTROL;
1891 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
1892 type = SOCKLND_CONN_BULK_IN;
1894 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
1895 type = SOCKLND_CONN_BULK_OUT;
1898 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1900 if (cfs_time_aftereq(cfs_time_current(), deadline)) {
1902 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1908 rc = lnet_connect(&sock, peer->ksnp_id.nid,
1909 route->ksnr_myipaddr,
1910 route->ksnr_ipaddr, route->ksnr_port);
1914 rc = ksocknal_create_conn(peer->ksnp_ni, route, sock, type);
1916 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1922 /* A +ve RC means I have to retry because I lost the connection
1923 * race or I have to renegotiate protocol version */
1924 retry_later = (rc != 0);
1926 CDEBUG(D_NET, "peer %s: conn race, retry later.\n",
1927 libcfs_nid2str(peer->ksnp_id.nid));
1929 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1932 route->ksnr_scheduled = 0;
1933 route->ksnr_connecting = 0;
1936 /* re-queue for attention; this frees me up to handle
1937 * the peer's incoming connection request */
1939 if (rc == EALREADY ||
1940 (rc == 0 && peer->ksnp_accepting > 0)) {
1941 /* We want to introduce a delay before next
1942 * attempt to connect if we lost conn race,
1943 * but the race is resolved quickly usually,
1944 * so min_reconnectms should be good heuristic */
1945 route->ksnr_retry_interval =
1946 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms)/1000;
1947 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
1948 route->ksnr_retry_interval);
1951 ksocknal_launch_connection_locked(route);
1954 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1958 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1960 route->ksnr_scheduled = 0;
1961 route->ksnr_connecting = 0;
1963 /* This is a retry rather than a new connection */
1964 route->ksnr_retry_interval *= 2;
1965 route->ksnr_retry_interval =
1966 MAX(route->ksnr_retry_interval,
1967 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms)/1000);
1968 route->ksnr_retry_interval =
1969 MIN(route->ksnr_retry_interval,
1970 cfs_time_seconds(*ksocknal_tunables.ksnd_max_reconnectms)/1000);
1972 LASSERT (route->ksnr_retry_interval != 0);
1973 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
1974 route->ksnr_retry_interval);
1976 if (!cfs_list_empty(&peer->ksnp_tx_queue) &&
1977 peer->ksnp_accepting == 0 &&
1978 ksocknal_find_connecting_route_locked(peer) == NULL) {
1981 /* ksnp_tx_queue is queued on a conn on successful
1982 * connection for V1.x and V2.x */
1983 if (!cfs_list_empty (&peer->ksnp_conns)) {
1984 conn = cfs_list_entry(peer->ksnp_conns.next,
1985 ksock_conn_t, ksnc_list);
1986 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
1989 /* take all the blocked packets while I've got the lock and
1990 * complete below... */
1991 cfs_list_splice_init(&peer->ksnp_tx_queue, &zombies);
1994 #if 0 /* irrelevent with only eager routes */
1995 if (!route->ksnr_deleted) {
1996 /* make this route least-favourite for re-selection */
1997 cfs_list_del(&route->ksnr_list);
1998 cfs_list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
2001 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2003 ksocknal_peer_failed(peer);
2004 ksocknal_txlist_done(peer->ksnp_ni, &zombies, 1);
2008 /* Go through connd_routes queue looking for a route that
2009 we can process right now */
2010 static ksock_route_t *
2011 ksocknal_connd_get_route_locked(signed long *timeout_p)
2013 ksock_route_t *route;
2016 /* Only handle an outgoing connection request if there
2017 * is someone left to handle incoming connections */
2018 if ((ksocknal_data.ksnd_connd_connecting + 1) >=
2019 *ksocknal_tunables.ksnd_nconnds)
2022 now = cfs_time_current();
2024 /* connd_routes can contain both pending and ordinary routes */
2025 cfs_list_for_each_entry (route, &ksocknal_data.ksnd_connd_routes,
2028 if (route->ksnr_retry_interval == 0 ||
2029 cfs_time_aftereq(now, route->ksnr_timeout))
2032 if (*timeout_p == CFS_MAX_SCHEDULE_TIMEOUT ||
2033 (int)*timeout_p > (int)(route->ksnr_timeout - now))
2034 *timeout_p = (int)(route->ksnr_timeout - now);
2041 ksocknal_connd (void *arg)
2043 long id = (long)(long_ptr_t)arg;
2045 ksock_connreq_t *cr;
2046 ksock_route_t *route;
2047 cfs_waitlink_t wait;
2048 signed long timeout;
2053 snprintf (name, sizeof (name), "socknal_cd%02ld", id);
2054 cfs_daemonize (name);
2055 cfs_block_allsigs ();
2057 cfs_waitlink_init (&wait);
2059 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2061 while (!ksocknal_data.ksnd_shuttingdown) {
2065 if (!cfs_list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2066 /* Connection accepted by the listener */
2067 cr = cfs_list_entry(ksocknal_data.ksnd_connd_connreqs. \
2068 next, ksock_connreq_t, ksncr_list);
2070 cfs_list_del(&cr->ksncr_list);
2071 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2074 ksocknal_create_conn(cr->ksncr_ni, NULL,
2075 cr->ksncr_sock, SOCKLND_CONN_NONE);
2076 lnet_ni_decref(cr->ksncr_ni);
2077 LIBCFS_FREE(cr, sizeof(*cr));
2079 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2082 /* Sleep till explicit wake_up if no pending routes present */
2083 timeout = CFS_MAX_SCHEDULE_TIMEOUT;
2085 /* Connection request */
2086 route = ksocknal_connd_get_route_locked(&timeout);
2088 if (route != NULL) {
2089 cfs_list_del (&route->ksnr_connd_list);
2090 ksocknal_data.ksnd_connd_connecting++;
2091 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2094 if (ksocknal_connect(route)) {
2095 /* consecutive retry */
2096 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2097 CWARN("massive consecutive "
2098 "re-connecting to %u.%u.%u.%u\n",
2099 HIPQUAD(route->ksnr_ipaddr));
2106 ksocknal_route_decref(route);
2108 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2109 ksocknal_data.ksnd_connd_connecting--;
2113 if (++nloops < SOCKNAL_RESCHED)
2115 cfs_spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2118 cfs_spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2122 /* Nothing to do for 'timeout' */
2123 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
2124 cfs_waitq_add_exclusive (&ksocknal_data.ksnd_connd_waitq,
2126 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2129 cfs_waitq_timedwait (&wait, CFS_TASK_INTERRUPTIBLE, timeout);
2131 cfs_set_current_state (CFS_TASK_RUNNING);
2132 cfs_waitq_del (&ksocknal_data.ksnd_connd_waitq, &wait);
2133 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2136 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2138 ksocknal_thread_fini ();
2143 ksocknal_find_timed_out_conn (ksock_peer_t *peer)
2145 /* We're called with a shared lock on ksnd_global_lock */
2149 cfs_list_for_each (ctmp, &peer->ksnp_conns) {
2151 conn = cfs_list_entry (ctmp, ksock_conn_t, ksnc_list);
2153 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2154 LASSERT (!conn->ksnc_closing);
2156 /* SOCK_ERROR will reset error code of socket in
2157 * some platform (like Darwin8.x) */
2158 error = libcfs_sock_error(conn->ksnc_sock);
2160 ksocknal_conn_addref(conn);
2164 CNETERR("A connection with %s "
2165 "(%u.%u.%u.%u:%d) was reset; "
2166 "it may have rebooted.\n",
2167 libcfs_id2str(peer->ksnp_id),
2168 HIPQUAD(conn->ksnc_ipaddr),
2172 CNETERR("A connection with %s "
2173 "(%u.%u.%u.%u:%d) timed out; the "
2174 "network or node may be down.\n",
2175 libcfs_id2str(peer->ksnp_id),
2176 HIPQUAD(conn->ksnc_ipaddr),
2180 CNETERR("An unexpected network error %d "
2182 "(%u.%u.%u.%u:%d\n", error,
2183 libcfs_id2str(peer->ksnp_id),
2184 HIPQUAD(conn->ksnc_ipaddr),
2192 if (conn->ksnc_rx_started &&
2193 cfs_time_aftereq(cfs_time_current(),
2194 conn->ksnc_rx_deadline)) {
2195 /* Timed out incomplete incoming message */
2196 ksocknal_conn_addref(conn);
2197 CNETERR("Timeout receiving from %s (%u.%u.%u.%u:%d), "
2198 "state %d wanted %d left %d\n",
2199 libcfs_id2str(peer->ksnp_id),
2200 HIPQUAD(conn->ksnc_ipaddr),
2202 conn->ksnc_rx_state,
2203 conn->ksnc_rx_nob_wanted,
2204 conn->ksnc_rx_nob_left);
2208 if ((!cfs_list_empty(&conn->ksnc_tx_queue) ||
2209 libcfs_sock_wmem_queued(conn->ksnc_sock) != 0) &&
2210 cfs_time_aftereq(cfs_time_current(),
2211 conn->ksnc_tx_deadline)) {
2212 /* Timed out messages queued for sending or
2213 * buffered in the socket's send buffer */
2214 ksocknal_conn_addref(conn);
2215 CNETERR("Timeout sending data to %s (%u.%u.%u.%u:%d) "
2216 "the network or that node may be down.\n",
2217 libcfs_id2str(peer->ksnp_id),
2218 HIPQUAD(conn->ksnc_ipaddr),
2228 ksocknal_flush_stale_txs(ksock_peer_t *peer)
2231 CFS_LIST_HEAD (stale_txs);
2233 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
2235 while (!cfs_list_empty (&peer->ksnp_tx_queue)) {
2236 tx = cfs_list_entry (peer->ksnp_tx_queue.next,
2237 ksock_tx_t, tx_list);
2239 if (!cfs_time_aftereq(cfs_time_current(),
2243 cfs_list_del (&tx->tx_list);
2244 cfs_list_add_tail (&tx->tx_list, &stale_txs);
2247 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2249 ksocknal_txlist_done(peer->ksnp_ni, &stale_txs, 1);
2253 ksocknal_send_keepalive_locked(ksock_peer_t *peer)
2255 ksock_sched_t *sched;
2259 if (cfs_list_empty(&peer->ksnp_conns)) /* last_alive will be updated by create_conn */
2262 if (peer->ksnp_proto != &ksocknal_protocol_v3x)
2265 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2266 cfs_time_before(cfs_time_current(),
2267 cfs_time_add(peer->ksnp_last_alive,
2268 cfs_time_seconds(*ksocknal_tunables.ksnd_keepalive))))
2271 if (cfs_time_before(cfs_time_current(),
2272 peer->ksnp_send_keepalive))
2275 /* retry 10 secs later, so we wouldn't put pressure
2276 * on this peer if we failed to send keepalive this time */
2277 peer->ksnp_send_keepalive = cfs_time_shift(10);
2279 conn = ksocknal_find_conn_locked(peer, NULL, 1);
2281 sched = conn->ksnc_scheduler;
2283 cfs_spin_lock_bh (&sched->kss_lock);
2284 if (!cfs_list_empty(&conn->ksnc_tx_queue)) {
2285 cfs_spin_unlock_bh(&sched->kss_lock);
2286 /* there is an queued ACK, don't need keepalive */
2290 cfs_spin_unlock_bh(&sched->kss_lock);
2293 cfs_read_unlock(&ksocknal_data.ksnd_global_lock);
2295 /* cookie = 1 is reserved for keepalive PING */
2296 tx = ksocknal_alloc_tx_noop(1, 1);
2298 cfs_read_lock(&ksocknal_data.ksnd_global_lock);
2302 if (ksocknal_launch_packet(peer->ksnp_ni, tx, peer->ksnp_id) == 0) {
2303 cfs_read_lock(&ksocknal_data.ksnd_global_lock);
2307 ksocknal_free_tx(tx);
2308 cfs_read_lock(&ksocknal_data.ksnd_global_lock);
2315 ksocknal_check_peer_timeouts (int idx)
2317 cfs_list_t *peers = &ksocknal_data.ksnd_peers[idx];
2323 /* NB. We expect to have a look at all the peers and not find any
2324 * connections to time out, so we just use a shared lock while we
2326 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2328 cfs_list_for_each_entry_typed(peer, peers, ksock_peer_t, ksnp_list) {
2329 cfs_time_t deadline = 0;
2333 if (ksocknal_send_keepalive_locked(peer) != 0) {
2334 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2338 conn = ksocknal_find_timed_out_conn (peer);
2341 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2343 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2345 /* NB we won't find this one again, but we can't
2346 * just proceed with the next peer, since we dropped
2347 * ksnd_global_lock and it might be dead already! */
2348 ksocknal_conn_decref(conn);
2352 /* we can't process stale txs right here because we're
2353 * holding only shared lock */
2354 if (!cfs_list_empty (&peer->ksnp_tx_queue)) {
2356 cfs_list_entry (peer->ksnp_tx_queue.next,
2357 ksock_tx_t, tx_list);
2359 if (cfs_time_aftereq(cfs_time_current(),
2362 ksocknal_peer_addref(peer);
2363 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2365 ksocknal_flush_stale_txs(peer);
2367 ksocknal_peer_decref(peer);
2372 if (cfs_list_empty(&peer->ksnp_zc_req_list))
2375 cfs_spin_lock(&peer->ksnp_lock);
2376 cfs_list_for_each_entry_typed(tx, &peer->ksnp_zc_req_list,
2377 ksock_tx_t, tx_zc_list) {
2378 if (!cfs_time_aftereq(cfs_time_current(),
2381 /* ignore the TX if connection is being closed */
2382 if (tx->tx_conn->ksnc_closing)
2388 cfs_spin_unlock(&peer->ksnp_lock);
2392 tx = cfs_list_entry(peer->ksnp_zc_req_list.next,
2393 ksock_tx_t, tx_zc_list);
2394 deadline = tx->tx_deadline;
2395 resid = tx->tx_resid;
2397 ksocknal_conn_addref(conn);
2399 cfs_spin_unlock(&peer->ksnp_lock);
2400 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2402 CERROR("Total %d stale ZC_REQs for peer %s detected; the "
2403 "oldest(%p) timed out %ld secs ago, "
2404 "resid: %d, wmem: %d\n",
2405 n, libcfs_nid2str(peer->ksnp_id.nid), tx,
2406 cfs_duration_sec(cfs_time_current() - deadline),
2407 resid, libcfs_sock_wmem_queued(conn->ksnc_sock));
2409 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2410 ksocknal_conn_decref(conn);
2414 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2418 ksocknal_reaper (void *arg)
2420 cfs_waitlink_t wait;
2422 ksock_sched_t *sched;
2423 cfs_list_t enomem_conns;
2425 cfs_duration_t timeout;
2428 cfs_time_t deadline = cfs_time_current();
2430 cfs_daemonize ("socknal_reaper");
2431 cfs_block_allsigs ();
2433 CFS_INIT_LIST_HEAD(&enomem_conns);
2434 cfs_waitlink_init (&wait);
2436 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2438 while (!ksocknal_data.ksnd_shuttingdown) {
2440 if (!cfs_list_empty (&ksocknal_data.ksnd_deathrow_conns)) {
2441 conn = cfs_list_entry (ksocknal_data. \
2442 ksnd_deathrow_conns.next,
2443 ksock_conn_t, ksnc_list);
2444 cfs_list_del (&conn->ksnc_list);
2446 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2448 ksocknal_terminate_conn (conn);
2449 ksocknal_conn_decref(conn);
2451 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2455 if (!cfs_list_empty (&ksocknal_data.ksnd_zombie_conns)) {
2456 conn = cfs_list_entry (ksocknal_data.ksnd_zombie_conns.\
2457 next, ksock_conn_t, ksnc_list);
2458 cfs_list_del (&conn->ksnc_list);
2460 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2462 ksocknal_destroy_conn (conn);
2464 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2468 if (!cfs_list_empty (&ksocknal_data.ksnd_enomem_conns)) {
2469 cfs_list_add(&enomem_conns,
2470 &ksocknal_data.ksnd_enomem_conns);
2471 cfs_list_del_init(&ksocknal_data.ksnd_enomem_conns);
2474 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2476 /* reschedule all the connections that stalled with ENOMEM... */
2478 while (!cfs_list_empty (&enomem_conns)) {
2479 conn = cfs_list_entry (enomem_conns.next,
2480 ksock_conn_t, ksnc_tx_list);
2481 cfs_list_del (&conn->ksnc_tx_list);
2483 sched = conn->ksnc_scheduler;
2485 cfs_spin_lock_bh (&sched->kss_lock);
2487 LASSERT (conn->ksnc_tx_scheduled);
2488 conn->ksnc_tx_ready = 1;
2489 cfs_list_add_tail(&conn->ksnc_tx_list,
2490 &sched->kss_tx_conns);
2491 cfs_waitq_signal (&sched->kss_waitq);
2493 cfs_spin_unlock_bh (&sched->kss_lock);
2497 /* careful with the jiffy wrap... */
2498 while ((timeout = cfs_time_sub(deadline,
2499 cfs_time_current())) <= 0) {
2502 int chunk = ksocknal_data.ksnd_peer_hash_size;
2504 /* Time to check for timeouts on a few more peers: I do
2505 * checks every 'p' seconds on a proportion of the peer
2506 * table and I need to check every connection 'n' times
2507 * within a timeout interval, to ensure I detect a
2508 * timeout on any connection within (n+1)/n times the
2509 * timeout interval. */
2511 if (*ksocknal_tunables.ksnd_timeout > n * p)
2512 chunk = (chunk * n * p) /
2513 *ksocknal_tunables.ksnd_timeout;
2517 for (i = 0; i < chunk; i++) {
2518 ksocknal_check_peer_timeouts (peer_index);
2519 peer_index = (peer_index + 1) %
2520 ksocknal_data.ksnd_peer_hash_size;
2523 deadline = cfs_time_add(deadline, cfs_time_seconds(p));
2526 if (nenomem_conns != 0) {
2527 /* Reduce my timeout if I rescheduled ENOMEM conns.
2528 * This also prevents me getting woken immediately
2529 * if any go back on my enomem list. */
2530 timeout = SOCKNAL_ENOMEM_RETRY;
2532 ksocknal_data.ksnd_reaper_waketime =
2533 cfs_time_add(cfs_time_current(), timeout);
2535 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
2536 cfs_waitq_add (&ksocknal_data.ksnd_reaper_waitq, &wait);
2538 if (!ksocknal_data.ksnd_shuttingdown &&
2539 cfs_list_empty (&ksocknal_data.ksnd_deathrow_conns) &&
2540 cfs_list_empty (&ksocknal_data.ksnd_zombie_conns))
2541 cfs_waitq_timedwait (&wait, CFS_TASK_INTERRUPTIBLE,
2544 cfs_set_current_state (CFS_TASK_RUNNING);
2545 cfs_waitq_del (&ksocknal_data.ksnd_reaper_waitq, &wait);
2547 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2550 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2552 ksocknal_thread_fini ();