1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright 2008 Sun Microsystems, Inc. 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_capable = 0;
58 tx->tx_zc_checked = 0;
59 tx->tx_desc_size = size;
61 cfs_atomic_inc(&ksocknal_data.ksnd_nactive_txs);
67 ksocknal_alloc_tx_noop(__u64 cookie, int nonblk)
71 tx = ksocknal_alloc_tx(KSOCK_MSG_NOOP, KSOCK_NOOP_TX_SIZE);
73 CERROR("Can't allocate noop tx desc\n");
78 tx->tx_lnetmsg = NULL;
81 tx->tx_iov = tx->tx_frags.virt.iov;
83 tx->tx_nonblk = nonblk;
85 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_NOOP);
86 tx->tx_msg.ksm_zc_cookies[1] = cookie;
93 ksocknal_free_tx (ksock_tx_t *tx)
95 cfs_atomic_dec(&ksocknal_data.ksnd_nactive_txs);
97 if (tx->tx_lnetmsg == NULL && tx->tx_desc_size == KSOCK_NOOP_TX_SIZE) {
99 cfs_spin_lock(&ksocknal_data.ksnd_tx_lock);
101 cfs_list_add(&tx->tx_list, &ksocknal_data.ksnd_idle_noop_txs);
103 cfs_spin_unlock(&ksocknal_data.ksnd_tx_lock);
105 LIBCFS_FREE(tx, tx->tx_desc_size);
110 ksocknal_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
112 struct iovec *iov = tx->tx_iov;
116 LASSERT (tx->tx_niov > 0);
118 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
119 rc = ksocknal_lib_send_iov(conn, tx);
121 if (rc <= 0) /* sent nothing? */
125 LASSERT (nob <= tx->tx_resid);
130 LASSERT (tx->tx_niov > 0);
132 if (nob < (int) iov->iov_len) {
133 iov->iov_base = (void *)((char *)iov->iov_base + nob);
147 ksocknal_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
149 lnet_kiov_t *kiov = tx->tx_kiov;
153 LASSERT (tx->tx_niov == 0);
154 LASSERT (tx->tx_nkiov > 0);
156 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
157 rc = ksocknal_lib_send_kiov(conn, tx);
159 if (rc <= 0) /* sent nothing? */
163 LASSERT (nob <= tx->tx_resid);
168 LASSERT(tx->tx_nkiov > 0);
170 if (nob < (int)kiov->kiov_len) {
171 kiov->kiov_offset += nob;
172 kiov->kiov_len -= nob;
176 nob -= (int)kiov->kiov_len;
177 tx->tx_kiov = ++kiov;
185 ksocknal_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
190 if (ksocknal_data.ksnd_stall_tx != 0) {
191 cfs_pause(cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
194 LASSERT (tx->tx_resid != 0);
196 rc = ksocknal_connsock_addref(conn);
198 LASSERT (conn->ksnc_closing);
203 if (ksocknal_data.ksnd_enomem_tx > 0) {
205 ksocknal_data.ksnd_enomem_tx--;
207 } else if (tx->tx_niov != 0) {
208 rc = ksocknal_send_iov (conn, tx);
210 rc = ksocknal_send_kiov (conn, tx);
213 bufnob = libcfs_sock_wmem_queued(conn->ksnc_sock);
214 if (rc > 0) /* sent something? */
215 conn->ksnc_tx_bufnob += rc; /* account it */
217 if (bufnob < conn->ksnc_tx_bufnob) {
218 /* allocated send buffer bytes < computed; infer
219 * something got ACKed */
220 conn->ksnc_tx_deadline =
221 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
222 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
223 conn->ksnc_tx_bufnob = bufnob;
227 if (rc <= 0) { /* Didn't write anything? */
229 if (rc == 0) /* some stacks return 0 instead of -EAGAIN */
232 /* Check if EAGAIN is due to memory pressure */
233 if(rc == -EAGAIN && ksocknal_lib_memory_pressure(conn))
239 /* socket's wmem_queued now includes 'rc' bytes */
240 cfs_atomic_sub (rc, &conn->ksnc_tx_nob);
243 } while (tx->tx_resid != 0);
245 ksocknal_connsock_decref(conn);
250 ksocknal_recv_iov (ksock_conn_t *conn)
252 struct iovec *iov = conn->ksnc_rx_iov;
256 LASSERT (conn->ksnc_rx_niov > 0);
258 /* Never touch conn->ksnc_rx_iov or change connection
259 * status inside ksocknal_lib_recv_iov */
260 rc = ksocknal_lib_recv_iov(conn);
265 /* received something... */
268 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
269 conn->ksnc_rx_deadline =
270 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
271 cfs_mb(); /* order with setting rx_started */
272 conn->ksnc_rx_started = 1;
274 conn->ksnc_rx_nob_wanted -= nob;
275 conn->ksnc_rx_nob_left -= nob;
278 LASSERT (conn->ksnc_rx_niov > 0);
280 if (nob < (int)iov->iov_len) {
282 iov->iov_base = (void *)((char *)iov->iov_base + nob);
287 conn->ksnc_rx_iov = ++iov;
288 conn->ksnc_rx_niov--;
295 ksocknal_recv_kiov (ksock_conn_t *conn)
297 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
300 LASSERT (conn->ksnc_rx_nkiov > 0);
302 /* Never touch conn->ksnc_rx_kiov or change connection
303 * status inside ksocknal_lib_recv_iov */
304 rc = ksocknal_lib_recv_kiov(conn);
309 /* received something... */
312 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
313 conn->ksnc_rx_deadline =
314 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
315 cfs_mb(); /* order with setting rx_started */
316 conn->ksnc_rx_started = 1;
318 conn->ksnc_rx_nob_wanted -= nob;
319 conn->ksnc_rx_nob_left -= nob;
322 LASSERT (conn->ksnc_rx_nkiov > 0);
324 if (nob < (int) kiov->kiov_len) {
325 kiov->kiov_offset += nob;
326 kiov->kiov_len -= nob;
330 nob -= kiov->kiov_len;
331 conn->ksnc_rx_kiov = ++kiov;
332 conn->ksnc_rx_nkiov--;
339 ksocknal_receive (ksock_conn_t *conn)
341 /* Return 1 on success, 0 on EOF, < 0 on error.
342 * Caller checks ksnc_rx_nob_wanted to determine
343 * progress/completion. */
347 if (ksocknal_data.ksnd_stall_rx != 0) {
348 cfs_pause(cfs_time_seconds (ksocknal_data.ksnd_stall_rx));
351 rc = ksocknal_connsock_addref(conn);
353 LASSERT (conn->ksnc_closing);
358 if (conn->ksnc_rx_niov != 0)
359 rc = ksocknal_recv_iov (conn);
361 rc = ksocknal_recv_kiov (conn);
364 /* error/EOF or partial receive */
367 } else if (rc == 0 && conn->ksnc_rx_started) {
368 /* EOF in the middle of a message */
374 /* Completed a fragment */
376 if (conn->ksnc_rx_nob_wanted == 0) {
382 ksocknal_connsock_decref(conn);
387 ksocknal_tx_done (lnet_ni_t *ni, ksock_tx_t *tx)
389 lnet_msg_t *lnetmsg = tx->tx_lnetmsg;
390 int rc = (tx->tx_resid == 0) ? 0 : -EIO;
393 LASSERT(ni != NULL || tx->tx_conn != NULL);
395 if (tx->tx_conn != NULL)
396 ksocknal_conn_decref(tx->tx_conn);
398 if (ni == NULL && tx->tx_conn != NULL)
399 ni = tx->tx_conn->ksnc_peer->ksnp_ni;
401 ksocknal_free_tx (tx);
402 if (lnetmsg != NULL) /* KSOCK_MSG_NOOP go without lnetmsg */
403 lnet_finalize (ni, lnetmsg, rc);
409 ksocknal_txlist_done (lnet_ni_t *ni, cfs_list_t *txlist, int error)
413 while (!cfs_list_empty (txlist)) {
414 tx = cfs_list_entry (txlist->next, ksock_tx_t, tx_list);
416 if (error && tx->tx_lnetmsg != NULL) {
417 CDEBUG (D_NETERROR, "Deleting packet type %d len %d %s->%s\n",
418 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.type),
419 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.payload_length),
420 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
421 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.dest_nid)));
423 CDEBUG (D_NETERROR, "Deleting noop packet\n");
426 cfs_list_del (&tx->tx_list);
428 LASSERT (cfs_atomic_read(&tx->tx_refcount) == 1);
429 ksocknal_tx_done (ni, tx);
434 ksocknal_check_zc_req(ksock_tx_t *tx)
436 ksock_conn_t *conn = tx->tx_conn;
437 ksock_peer_t *peer = conn->ksnc_peer;
439 /* Set tx_msg.ksm_zc_cookies[0] to a unique non-zero cookie and add tx
440 * to ksnp_zc_req_list if some fragment of this message should be sent
441 * zero-copy. Our peer will send an ACK containing this cookie when
442 * she has received this message to tell us we can signal completion.
443 * tx_msg.ksm_zc_cookies[0] remains non-zero while tx is on
444 * ksnp_zc_req_list. */
445 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
446 LASSERT (tx->tx_zc_capable);
448 tx->tx_zc_checked = 1;
450 if (conn->ksnc_proto == &ksocknal_protocol_v1x ||
451 !conn->ksnc_zc_capable)
454 /* assign cookie and queue tx to pending list, it will be released when
455 * a matching ack is received. See ksocknal_handle_zcack() */
457 ksocknal_tx_addref(tx);
459 cfs_spin_lock(&peer->ksnp_lock);
461 /* ZC_REQ is going to be pinned to the peer */
463 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
465 LASSERT (tx->tx_msg.ksm_zc_cookies[0] == 0);
467 tx->tx_msg.ksm_zc_cookies[0] = peer->ksnp_zc_next_cookie++;
469 if (peer->ksnp_zc_next_cookie == 0)
470 peer->ksnp_zc_next_cookie = SOCKNAL_KEEPALIVE_PING + 1;
472 cfs_list_add_tail(&tx->tx_zc_list, &peer->ksnp_zc_req_list);
474 cfs_spin_unlock(&peer->ksnp_lock);
478 ksocknal_uncheck_zc_req(ksock_tx_t *tx)
480 ksock_peer_t *peer = tx->tx_conn->ksnc_peer;
482 LASSERT (tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
483 LASSERT (tx->tx_zc_capable);
485 tx->tx_zc_checked = 0;
487 cfs_spin_lock(&peer->ksnp_lock);
489 if (tx->tx_msg.ksm_zc_cookies[0] == 0) {
490 /* Not waiting for an ACK */
491 cfs_spin_unlock(&peer->ksnp_lock);
495 tx->tx_msg.ksm_zc_cookies[0] = 0;
496 cfs_list_del(&tx->tx_zc_list);
498 cfs_spin_unlock(&peer->ksnp_lock);
500 ksocknal_tx_decref(tx);
504 ksocknal_process_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
508 if (tx->tx_zc_capable && !tx->tx_zc_checked)
509 ksocknal_check_zc_req(tx);
511 rc = ksocknal_transmit (conn, tx);
513 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
515 if (tx->tx_resid == 0) {
516 /* Sent everything OK */
528 counter++; /* exponential backoff warnings */
529 if ((counter & (-counter)) == counter)
530 CWARN("%u ENOMEM tx %p (%u allocated)\n",
531 counter, conn, cfs_atomic_read(&libcfs_kmemory));
533 /* Queue on ksnd_enomem_conns for retry after a timeout */
534 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
536 /* enomem list takes over scheduler's ref... */
537 LASSERT (conn->ksnc_tx_scheduled);
538 cfs_list_add_tail(&conn->ksnc_tx_list,
539 &ksocknal_data.ksnd_enomem_conns);
540 if (!cfs_time_aftereq(cfs_time_add(cfs_time_current(),
541 SOCKNAL_ENOMEM_RETRY),
542 ksocknal_data.ksnd_reaper_waketime))
543 cfs_waitq_signal (&ksocknal_data.ksnd_reaper_waitq);
545 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
552 if (!conn->ksnc_closing) {
555 LCONSOLE_WARN("Host %u.%u.%u.%u reset our connection "
556 "while we were sending data; it may have "
558 HIPQUAD(conn->ksnc_ipaddr));
561 LCONSOLE_WARN("There was an unexpected network error "
562 "while writing to %u.%u.%u.%u: %d.\n",
563 HIPQUAD(conn->ksnc_ipaddr), rc);
566 CDEBUG(D_NET, "[%p] Error %d on write to %s"
567 " ip %d.%d.%d.%d:%d\n", conn, rc,
568 libcfs_id2str(conn->ksnc_peer->ksnp_id),
569 HIPQUAD(conn->ksnc_ipaddr),
573 if (tx->tx_zc_checked)
574 ksocknal_uncheck_zc_req(tx);
576 /* it's not an error if conn is being closed */
577 ksocknal_close_conn_and_siblings (conn,
578 (conn->ksnc_closing) ? 0 : rc);
584 ksocknal_launch_connection_locked (ksock_route_t *route)
587 /* called holding write lock on ksnd_global_lock */
589 LASSERT (!route->ksnr_scheduled);
590 LASSERT (!route->ksnr_connecting);
591 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
593 route->ksnr_scheduled = 1; /* scheduling conn for connd */
594 ksocknal_route_addref(route); /* extra ref for connd */
596 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
598 cfs_list_add_tail (&route->ksnr_connd_list,
599 &ksocknal_data.ksnd_connd_routes);
600 cfs_waitq_signal (&ksocknal_data.ksnd_connd_waitq);
602 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
606 ksocknal_launch_all_connections_locked (ksock_peer_t *peer)
608 ksock_route_t *route;
610 /* called holding write lock on ksnd_global_lock */
612 /* launch any/all connections that need it */
613 route = ksocknal_find_connectable_route_locked(peer);
617 ksocknal_launch_connection_locked(route);
622 ksocknal_find_conn_locked(ksock_peer_t *peer, ksock_tx_t *tx, int nonblk)
626 ksock_conn_t *typed = NULL;
627 ksock_conn_t *fallback = NULL;
631 cfs_list_for_each (tmp, &peer->ksnp_conns) {
632 ksock_conn_t *c = cfs_list_entry(tmp, ksock_conn_t, ksnc_list);
633 int nob = cfs_atomic_read(&c->ksnc_tx_nob) +
634 libcfs_sock_wmem_queued(c->ksnc_sock);
637 LASSERT (!c->ksnc_closing);
638 LASSERT (c->ksnc_proto != NULL &&
639 c->ksnc_proto->pro_match_tx != NULL);
641 rc = c->ksnc_proto->pro_match_tx(c, tx, nonblk);
646 case SOCKNAL_MATCH_NO: /* protocol rejected the tx */
649 case SOCKNAL_MATCH_YES: /* typed connection */
650 if (typed == NULL || tnob > nob ||
651 (tnob == nob && *ksocknal_tunables.ksnd_round_robin &&
652 cfs_time_after(typed->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
658 case SOCKNAL_MATCH_MAY: /* fallback connection */
659 if (fallback == NULL || fnob > nob ||
660 (fnob == nob && *ksocknal_tunables.ksnd_round_robin &&
661 cfs_time_after(fallback->ksnc_tx_last_post, c->ksnc_tx_last_post))) {
669 /* prefer the typed selection */
670 conn = (typed != NULL) ? typed : fallback;
673 conn->ksnc_tx_last_post = cfs_time_current();
679 ksocknal_tx_prep(ksock_conn_t *conn, ksock_tx_t *tx)
681 conn->ksnc_proto->pro_pack(tx);
683 cfs_atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
684 ksocknal_conn_addref(conn); /* +1 ref for tx */
689 ksocknal_queue_tx_locked (ksock_tx_t *tx, ksock_conn_t *conn)
691 ksock_sched_t *sched = conn->ksnc_scheduler;
692 ksock_msg_t *msg = &tx->tx_msg;
693 ksock_tx_t *ztx = NULL;
696 /* called holding global lock (read or irq-write) and caller may
697 * not have dropped this lock between finding conn and calling me,
698 * so we don't need the {get,put}connsock dance to deref
700 LASSERT(!conn->ksnc_closing);
702 CDEBUG (D_NET, "Sending to %s ip %d.%d.%d.%d:%d\n",
703 libcfs_id2str(conn->ksnc_peer->ksnp_id),
704 HIPQUAD(conn->ksnc_ipaddr),
707 ksocknal_tx_prep(conn, tx);
709 /* Ensure the frags we've been given EXACTLY match the number of
710 * bytes we want to send. Many TCP/IP stacks disregard any total
711 * size parameters passed to them and just look at the frags.
713 * We always expect at least 1 mapped fragment containing the
714 * complete ksocknal message header. */
715 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
716 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
717 (unsigned int)tx->tx_nob);
718 LASSERT (tx->tx_niov >= 1);
719 LASSERT (tx->tx_resid == tx->tx_nob);
721 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
722 tx, (tx->tx_lnetmsg != NULL) ? tx->tx_lnetmsg->msg_hdr.type:
724 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
727 * FIXME: SOCK_WMEM_QUEUED and SOCK_ERROR could block in __DARWIN8__
728 * but they're used inside spinlocks a lot.
730 bufnob = libcfs_sock_wmem_queued(conn->ksnc_sock);
731 cfs_spin_lock_bh (&sched->kss_lock);
733 if (cfs_list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
734 /* First packet starts the timeout */
735 conn->ksnc_tx_deadline =
736 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
737 if (conn->ksnc_tx_bufnob > 0) /* something got ACKed */
738 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
739 conn->ksnc_tx_bufnob = 0;
740 cfs_mb(); /* order with adding to tx_queue */
743 if (msg->ksm_type == KSOCK_MSG_NOOP) {
744 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
745 * on a normal packet so I don't need to send it */
746 LASSERT (msg->ksm_zc_cookies[1] != 0);
747 LASSERT (conn->ksnc_proto->pro_queue_tx_zcack != NULL);
749 if (conn->ksnc_proto->pro_queue_tx_zcack(conn, tx, 0))
750 ztx = tx; /* ZC ACK piggybacked on ztx release tx later */
753 /* It's a normal packet - can it piggback a noop zc-ack that
754 * has been queued already? */
755 LASSERT (msg->ksm_zc_cookies[1] == 0);
756 LASSERT (conn->ksnc_proto->pro_queue_tx_msg != NULL);
758 ztx = conn->ksnc_proto->pro_queue_tx_msg(conn, tx);
759 /* ztx will be released later */
763 cfs_atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
764 cfs_list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
767 if (conn->ksnc_tx_ready && /* able to send */
768 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
769 /* +1 ref for scheduler */
770 ksocknal_conn_addref(conn);
771 cfs_list_add_tail (&conn->ksnc_tx_list,
772 &sched->kss_tx_conns);
773 conn->ksnc_tx_scheduled = 1;
774 cfs_waitq_signal (&sched->kss_waitq);
777 cfs_spin_unlock_bh (&sched->kss_lock);
782 ksocknal_find_connectable_route_locked (ksock_peer_t *peer)
785 ksock_route_t *route;
787 cfs_list_for_each (tmp, &peer->ksnp_routes) {
788 route = cfs_list_entry (tmp, ksock_route_t, ksnr_list);
790 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
792 if (route->ksnr_scheduled) /* connections being established */
795 /* all route types connected ? */
796 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
799 /* too soon to retry this guy? */
800 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
801 cfs_time_aftereq (cfs_time_current(),
802 route->ksnr_timeout)))
812 ksocknal_find_connecting_route_locked (ksock_peer_t *peer)
815 ksock_route_t *route;
817 cfs_list_for_each (tmp, &peer->ksnp_routes) {
818 route = cfs_list_entry (tmp, ksock_route_t, ksnr_list);
820 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
822 if (route->ksnr_scheduled)
830 ksocknal_launch_packet (lnet_ni_t *ni, ksock_tx_t *tx, lnet_process_id_t id)
834 cfs_rwlock_t *g_lock;
838 LASSERT (tx->tx_conn == NULL);
840 g_lock = &ksocknal_data.ksnd_global_lock;
842 for (retry = 0;; retry = 1) {
843 cfs_read_lock (g_lock);
844 peer = ksocknal_find_peer_locked(ni, id);
846 if (ksocknal_find_connectable_route_locked(peer) == NULL) {
847 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
849 /* I've got no routes that need to be
850 * connecting and I do have an actual
852 ksocknal_queue_tx_locked (tx, conn);
853 cfs_read_unlock (g_lock);
859 /* I'll need a write lock... */
860 cfs_read_unlock (g_lock);
862 cfs_write_lock_bh (g_lock);
864 peer = ksocknal_find_peer_locked(ni, id);
868 cfs_write_unlock_bh (g_lock);
870 if ((id.pid & LNET_PID_USERFLAG) != 0) {
871 CERROR("Refusing to create a connection to "
872 "userspace process %s\n", libcfs_id2str(id));
873 return -EHOSTUNREACH;
877 CERROR("Can't find peer %s\n", libcfs_id2str(id));
878 return -EHOSTUNREACH;
881 rc = ksocknal_add_peer(ni, id,
882 LNET_NIDADDR(id.nid),
883 lnet_acceptor_port());
885 CERROR("Can't add peer %s: %d\n",
886 libcfs_id2str(id), rc);
891 ksocknal_launch_all_connections_locked(peer);
893 conn = ksocknal_find_conn_locked(peer, tx, tx->tx_nonblk);
895 /* Connection exists; queue message on it */
896 ksocknal_queue_tx_locked (tx, conn);
897 cfs_write_unlock_bh (g_lock);
901 if (peer->ksnp_accepting > 0 ||
902 ksocknal_find_connecting_route_locked (peer) != NULL) {
903 /* the message is going to be pinned to the peer */
905 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
907 /* Queue the message until a connection is established */
908 cfs_list_add_tail (&tx->tx_list, &peer->ksnp_tx_queue);
909 cfs_write_unlock_bh (g_lock);
913 cfs_write_unlock_bh (g_lock);
915 /* NB Routes may be ignored if connections to them failed recently */
916 CDEBUG(D_NETERROR, "No usable routes to %s\n", libcfs_id2str(id));
917 return (-EHOSTUNREACH);
921 ksocknal_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
923 int type = lntmsg->msg_type;
924 lnet_process_id_t target = lntmsg->msg_target;
925 unsigned int payload_niov = lntmsg->msg_niov;
926 struct iovec *payload_iov = lntmsg->msg_iov;
927 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
928 unsigned int payload_offset = lntmsg->msg_offset;
929 unsigned int payload_nob = lntmsg->msg_len;
934 /* NB 'private' is different depending on what we're sending.
935 * Just ignore it... */
937 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
938 payload_nob, payload_niov, libcfs_id2str(target));
940 LASSERT (payload_nob == 0 || payload_niov > 0);
941 LASSERT (payload_niov <= LNET_MAX_IOV);
942 /* payload is either all vaddrs or all pages */
943 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
944 LASSERT (!cfs_in_interrupt ());
946 if (payload_iov != NULL)
947 desc_size = offsetof(ksock_tx_t,
948 tx_frags.virt.iov[1 + payload_niov]);
950 desc_size = offsetof(ksock_tx_t,
951 tx_frags.paged.kiov[payload_niov]);
953 tx = ksocknal_alloc_tx(KSOCK_MSG_LNET, desc_size);
955 CERROR("Can't allocate tx desc type %d size %d\n",
960 tx->tx_conn = NULL; /* set when assigned a conn */
961 tx->tx_lnetmsg = lntmsg;
963 if (payload_iov != NULL) {
966 tx->tx_iov = tx->tx_frags.virt.iov;
968 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
969 payload_niov, payload_iov,
970 payload_offset, payload_nob);
973 tx->tx_iov = &tx->tx_frags.paged.iov;
974 tx->tx_kiov = tx->tx_frags.paged.kiov;
975 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
976 payload_niov, payload_kiov,
977 payload_offset, payload_nob);
979 if (payload_nob >= *ksocknal_tunables.ksnd_zc_min_payload)
980 tx->tx_zc_capable = 1;
983 socklnd_init_msg(&tx->tx_msg, KSOCK_MSG_LNET);
985 /* The first fragment will be set later in pro_pack */
986 rc = ksocknal_launch_packet(ni, tx, target);
990 ksocknal_free_tx(tx);
995 ksocknal_thread_start (int (*fn)(void *arg), void *arg)
997 long pid = cfs_kernel_thread (fn, arg, 0);
1002 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1003 ksocknal_data.ksnd_nthreads++;
1004 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1009 ksocknal_thread_fini (void)
1011 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1012 ksocknal_data.ksnd_nthreads--;
1013 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1017 ksocknal_new_packet (ksock_conn_t *conn, int nob_to_skip)
1019 static char ksocknal_slop_buffer[4096];
1025 LASSERT(conn->ksnc_proto != NULL);
1027 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1028 /* Remind the socket to ack eagerly... */
1029 ksocknal_lib_eager_ack(conn);
1032 if (nob_to_skip == 0) { /* right at next packet boundary now */
1033 conn->ksnc_rx_started = 0;
1034 cfs_mb(); /* racing with timeout thread */
1036 switch (conn->ksnc_proto->pro_version) {
1037 case KSOCK_PROTO_V2:
1038 case KSOCK_PROTO_V3:
1039 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1040 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1041 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1043 conn->ksnc_rx_nob_wanted = offsetof(ksock_msg_t, ksm_u);
1044 conn->ksnc_rx_nob_left = offsetof(ksock_msg_t, ksm_u);
1045 conn->ksnc_rx_iov[0].iov_len = offsetof(ksock_msg_t, ksm_u);
1048 case KSOCK_PROTO_V1:
1049 /* Receiving bare lnet_hdr_t */
1050 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1051 conn->ksnc_rx_nob_wanted = sizeof(lnet_hdr_t);
1052 conn->ksnc_rx_nob_left = sizeof(lnet_hdr_t);
1054 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1055 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1056 conn->ksnc_rx_iov[0].iov_len = sizeof (lnet_hdr_t);
1062 conn->ksnc_rx_niov = 1;
1064 conn->ksnc_rx_kiov = NULL;
1065 conn->ksnc_rx_nkiov = 0;
1066 conn->ksnc_rx_csum = ~0;
1070 /* Set up to skip as much as possible now. If there's more left
1071 * (ran out of iov entries) we'll get called again */
1073 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1074 conn->ksnc_rx_nob_left = nob_to_skip;
1075 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1080 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1082 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1083 conn->ksnc_rx_iov[niov].iov_len = nob;
1088 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1089 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof (struct iovec));
1091 conn->ksnc_rx_niov = niov;
1092 conn->ksnc_rx_kiov = NULL;
1093 conn->ksnc_rx_nkiov = 0;
1094 conn->ksnc_rx_nob_wanted = skipped;
1099 ksocknal_process_receive (ksock_conn_t *conn)
1102 lnet_process_id_t *id;
1105 LASSERT (cfs_atomic_read(&conn->ksnc_conn_refcount) > 0);
1107 /* NB: sched lock NOT held */
1108 /* SOCKNAL_RX_LNET_HEADER is here for backward compatability */
1109 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1110 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1111 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1112 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1114 if (conn->ksnc_rx_nob_wanted != 0) {
1115 rc = ksocknal_receive(conn);
1118 LASSERT (rc != -EAGAIN);
1121 CDEBUG (D_NET, "[%p] EOF from %s"
1122 " ip %d.%d.%d.%d:%d\n", conn,
1123 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1124 HIPQUAD(conn->ksnc_ipaddr),
1126 else if (!conn->ksnc_closing)
1127 CERROR ("[%p] Error %d on read from %s"
1128 " ip %d.%d.%d.%d:%d\n",
1130 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1131 HIPQUAD(conn->ksnc_ipaddr),
1134 /* it's not an error if conn is being closed */
1135 ksocknal_close_conn_and_siblings (conn,
1136 (conn->ksnc_closing) ? 0 : rc);
1137 return (rc == 0 ? -ESHUTDOWN : rc);
1140 if (conn->ksnc_rx_nob_wanted != 0) {
1145 switch (conn->ksnc_rx_state) {
1146 case SOCKNAL_RX_KSM_HEADER:
1147 if (conn->ksnc_flip) {
1148 __swab32s(&conn->ksnc_msg.ksm_type);
1149 __swab32s(&conn->ksnc_msg.ksm_csum);
1150 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[0]);
1151 __swab64s(&conn->ksnc_msg.ksm_zc_cookies[1]);
1154 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1155 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1156 CERROR("%s: Unknown message type: %x\n",
1157 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1158 conn->ksnc_msg.ksm_type);
1159 ksocknal_new_packet(conn, 0);
1160 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1164 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1165 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1166 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1167 /* NOOP Checksum error */
1168 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1169 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1170 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1171 ksocknal_new_packet(conn, 0);
1172 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1176 if (conn->ksnc_msg.ksm_zc_cookies[1] != 0) {
1179 LASSERT (conn->ksnc_proto != &ksocknal_protocol_v1x);
1181 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP)
1182 cookie = conn->ksnc_msg.ksm_zc_cookies[0];
1184 rc = conn->ksnc_proto->pro_handle_zcack(conn, cookie,
1185 conn->ksnc_msg.ksm_zc_cookies[1]);
1188 CERROR("%s: Unknown ZC-ACK cookie: "LPU64", "LPU64"\n",
1189 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1190 cookie, conn->ksnc_msg.ksm_zc_cookies[1]);
1191 ksocknal_new_packet(conn, 0);
1192 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1197 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1198 ksocknal_new_packet (conn, 0);
1199 return 0; /* NOOP is done and just return */
1202 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1203 conn->ksnc_rx_nob_wanted = sizeof(ksock_lnet_msg_t);
1204 conn->ksnc_rx_nob_left = sizeof(ksock_lnet_msg_t);
1206 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1207 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1208 conn->ksnc_rx_iov[0].iov_len = sizeof(ksock_lnet_msg_t);
1210 conn->ksnc_rx_niov = 1;
1211 conn->ksnc_rx_kiov = NULL;
1212 conn->ksnc_rx_nkiov = 0;
1214 goto again; /* read lnet header now */
1216 case SOCKNAL_RX_LNET_HEADER:
1217 /* unpack message header */
1218 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1220 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1221 /* Userspace peer */
1222 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1223 id = &conn->ksnc_peer->ksnp_id;
1225 /* Substitute process ID assigned at connection time */
1226 lhdr->src_pid = cpu_to_le32(id->pid);
1227 lhdr->src_nid = cpu_to_le64(id->nid);
1230 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1231 ksocknal_conn_addref(conn); /* ++ref while parsing */
1233 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1234 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1235 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1237 /* I just received garbage: give up on this conn */
1238 ksocknal_new_packet(conn, 0);
1239 ksocknal_close_conn_and_siblings (conn, rc);
1240 ksocknal_conn_decref(conn);
1244 /* I'm racing with ksocknal_recv() */
1245 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1246 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1248 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1251 /* ksocknal_recv() got called */
1254 case SOCKNAL_RX_LNET_PAYLOAD:
1255 /* payload all received */
1258 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1259 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1260 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1261 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1262 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1263 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1267 if (rc == 0 && conn->ksnc_msg.ksm_zc_cookies[0] != 0) {
1268 LASSERT(conn->ksnc_proto != &ksocknal_protocol_v1x);
1270 lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1271 id = &conn->ksnc_peer->ksnp_id;
1273 rc = conn->ksnc_proto->pro_handle_zcreq(conn,
1274 conn->ksnc_msg.ksm_zc_cookies[0],
1275 *ksocknal_tunables.ksnd_nonblk_zcack ||
1276 le64_to_cpu(lhdr->src_nid) != id->nid);
1279 lnet_finalize(conn->ksnc_peer->ksnp_ni, conn->ksnc_cookie, rc);
1282 ksocknal_new_packet(conn, 0);
1283 ksocknal_close_conn_and_siblings (conn, rc);
1288 case SOCKNAL_RX_SLOP:
1289 /* starting new packet? */
1290 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1291 return 0; /* come back later */
1292 goto again; /* try to finish reading slop now */
1300 return (-EINVAL); /* keep gcc happy */
1304 ksocknal_recv (lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
1305 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1306 unsigned int offset, unsigned int mlen, unsigned int rlen)
1308 ksock_conn_t *conn = (ksock_conn_t *)private;
1309 ksock_sched_t *sched = conn->ksnc_scheduler;
1311 LASSERT (mlen <= rlen);
1312 LASSERT (niov <= LNET_MAX_IOV);
1314 conn->ksnc_cookie = msg;
1315 conn->ksnc_rx_nob_wanted = mlen;
1316 conn->ksnc_rx_nob_left = rlen;
1318 if (mlen == 0 || iov != NULL) {
1319 conn->ksnc_rx_nkiov = 0;
1320 conn->ksnc_rx_kiov = NULL;
1321 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1322 conn->ksnc_rx_niov =
1323 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1324 niov, iov, offset, mlen);
1326 conn->ksnc_rx_niov = 0;
1327 conn->ksnc_rx_iov = NULL;
1328 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1329 conn->ksnc_rx_nkiov =
1330 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1331 niov, kiov, offset, mlen);
1335 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1336 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1338 LASSERT (conn->ksnc_rx_scheduled);
1340 cfs_spin_lock_bh (&sched->kss_lock);
1342 switch (conn->ksnc_rx_state) {
1343 case SOCKNAL_RX_PARSE_WAIT:
1344 cfs_list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1345 cfs_waitq_signal (&sched->kss_waitq);
1346 LASSERT (conn->ksnc_rx_ready);
1349 case SOCKNAL_RX_PARSE:
1350 /* scheduler hasn't noticed I'm parsing yet */
1354 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1356 cfs_spin_unlock_bh (&sched->kss_lock);
1357 ksocknal_conn_decref(conn);
1362 ksocknal_sched_cansleep(ksock_sched_t *sched)
1366 cfs_spin_lock_bh (&sched->kss_lock);
1368 rc = (!ksocknal_data.ksnd_shuttingdown &&
1369 cfs_list_empty(&sched->kss_rx_conns) &&
1370 cfs_list_empty(&sched->kss_tx_conns));
1372 cfs_spin_unlock_bh (&sched->kss_lock);
1376 int ksocknal_scheduler (void *arg)
1378 ksock_sched_t *sched = (ksock_sched_t *)arg;
1383 int id = (int)(sched - ksocknal_data.ksnd_schedulers);
1386 snprintf (name, sizeof (name),"socknal_sd%02d", id);
1387 cfs_daemonize (name);
1388 cfs_block_allsigs ();
1390 if (ksocknal_lib_bind_thread_to_cpu(id))
1391 CERROR ("Can't set CPU affinity for %s to %d\n", name, id);
1393 cfs_spin_lock_bh (&sched->kss_lock);
1395 while (!ksocknal_data.ksnd_shuttingdown) {
1396 int did_something = 0;
1398 /* Ensure I progress everything semi-fairly */
1400 if (!cfs_list_empty (&sched->kss_rx_conns)) {
1401 conn = cfs_list_entry(sched->kss_rx_conns.next,
1402 ksock_conn_t, ksnc_rx_list);
1403 cfs_list_del(&conn->ksnc_rx_list);
1405 LASSERT(conn->ksnc_rx_scheduled);
1406 LASSERT(conn->ksnc_rx_ready);
1408 /* clear rx_ready in case receive isn't complete.
1409 * Do it BEFORE we call process_recv, since
1410 * data_ready can set it any time after we release
1412 conn->ksnc_rx_ready = 0;
1413 cfs_spin_unlock_bh (&sched->kss_lock);
1415 rc = ksocknal_process_receive(conn);
1417 cfs_spin_lock_bh (&sched->kss_lock);
1419 /* I'm the only one that can clear this flag */
1420 LASSERT(conn->ksnc_rx_scheduled);
1422 /* Did process_receive get everything it wanted? */
1424 conn->ksnc_rx_ready = 1;
1426 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1427 /* Conn blocked waiting for ksocknal_recv()
1428 * I change its state (under lock) to signal
1429 * it can be rescheduled */
1430 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1431 } else if (conn->ksnc_rx_ready) {
1432 /* reschedule for rx */
1433 cfs_list_add_tail (&conn->ksnc_rx_list,
1434 &sched->kss_rx_conns);
1436 conn->ksnc_rx_scheduled = 0;
1438 ksocknal_conn_decref(conn);
1444 if (!cfs_list_empty (&sched->kss_tx_conns)) {
1445 CFS_LIST_HEAD (zlist);
1447 if (!cfs_list_empty(&sched->kss_zombie_noop_txs)) {
1448 cfs_list_add(&zlist,
1449 &sched->kss_zombie_noop_txs);
1450 cfs_list_del_init(&sched->kss_zombie_noop_txs);
1453 conn = cfs_list_entry(sched->kss_tx_conns.next,
1454 ksock_conn_t, ksnc_tx_list);
1455 cfs_list_del (&conn->ksnc_tx_list);
1457 LASSERT(conn->ksnc_tx_scheduled);
1458 LASSERT(conn->ksnc_tx_ready);
1459 LASSERT(!cfs_list_empty(&conn->ksnc_tx_queue));
1461 tx = cfs_list_entry(conn->ksnc_tx_queue.next,
1462 ksock_tx_t, tx_list);
1464 if (conn->ksnc_tx_carrier == tx)
1465 ksocknal_next_tx_carrier(conn);
1467 /* dequeue now so empty list => more to send */
1468 cfs_list_del(&tx->tx_list);
1470 /* Clear tx_ready in case send isn't complete. Do
1471 * it BEFORE we call process_transmit, since
1472 * write_space can set it any time after we release
1474 conn->ksnc_tx_ready = 0;
1475 cfs_spin_unlock_bh (&sched->kss_lock);
1477 if (!cfs_list_empty(&zlist)) {
1478 /* free zombie noop txs, it's fast because
1479 * noop txs are just put in freelist */
1480 ksocknal_txlist_done(NULL, &zlist, 0);
1483 rc = ksocknal_process_transmit(conn, tx);
1485 if (rc == -ENOMEM || rc == -EAGAIN) {
1486 /* Incomplete send: replace tx on HEAD of tx_queue */
1487 cfs_spin_lock_bh (&sched->kss_lock);
1488 cfs_list_add (&tx->tx_list,
1489 &conn->ksnc_tx_queue);
1491 /* Complete send; tx -ref */
1492 ksocknal_tx_decref (tx);
1494 cfs_spin_lock_bh (&sched->kss_lock);
1495 /* assume space for more */
1496 conn->ksnc_tx_ready = 1;
1499 if (rc == -ENOMEM) {
1500 /* Do nothing; after a short timeout, this
1501 * conn will be reposted on kss_tx_conns. */
1502 } else if (conn->ksnc_tx_ready &&
1503 !cfs_list_empty (&conn->ksnc_tx_queue)) {
1504 /* reschedule for tx */
1505 cfs_list_add_tail (&conn->ksnc_tx_list,
1506 &sched->kss_tx_conns);
1508 conn->ksnc_tx_scheduled = 0;
1510 ksocknal_conn_decref(conn);
1515 if (!did_something || /* nothing to do */
1516 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1517 cfs_spin_unlock_bh (&sched->kss_lock);
1521 if (!did_something) { /* wait for something to do */
1522 cfs_wait_event_interruptible_exclusive(
1524 !ksocknal_sched_cansleep(sched), rc);
1530 cfs_spin_lock_bh (&sched->kss_lock);
1534 cfs_spin_unlock_bh (&sched->kss_lock);
1535 ksocknal_thread_fini ();
1540 * Add connection to kss_rx_conns of scheduler
1541 * and wakeup the scheduler.
1543 void ksocknal_read_callback (ksock_conn_t *conn)
1545 ksock_sched_t *sched;
1548 sched = conn->ksnc_scheduler;
1550 cfs_spin_lock_bh (&sched->kss_lock);
1552 conn->ksnc_rx_ready = 1;
1554 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1555 cfs_list_add_tail(&conn->ksnc_rx_list,
1556 &sched->kss_rx_conns);
1557 conn->ksnc_rx_scheduled = 1;
1558 /* extra ref for scheduler */
1559 ksocknal_conn_addref(conn);
1561 cfs_waitq_signal (&sched->kss_waitq);
1563 cfs_spin_unlock_bh (&sched->kss_lock);
1569 * Add connection to kss_tx_conns of scheduler
1570 * and wakeup the scheduler.
1572 void ksocknal_write_callback (ksock_conn_t *conn)
1574 ksock_sched_t *sched;
1577 sched = conn->ksnc_scheduler;
1579 cfs_spin_lock_bh (&sched->kss_lock);
1581 conn->ksnc_tx_ready = 1;
1583 if (!conn->ksnc_tx_scheduled && // not being progressed
1584 !cfs_list_empty(&conn->ksnc_tx_queue)){//packets to send
1585 cfs_list_add_tail (&conn->ksnc_tx_list,
1586 &sched->kss_tx_conns);
1587 conn->ksnc_tx_scheduled = 1;
1588 /* extra ref for scheduler */
1589 ksocknal_conn_addref(conn);
1591 cfs_waitq_signal (&sched->kss_waitq);
1594 cfs_spin_unlock_bh (&sched->kss_lock);
1600 ksocknal_parse_proto_version (ksock_hello_msg_t *hello)
1604 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1605 version = hello->kshm_version;
1606 else if (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC))
1607 version = __swab32(hello->kshm_version);
1610 #if SOCKNAL_VERSION_DEBUG
1611 if (*ksocknal_tunables.ksnd_protocol == 1)
1614 if (*ksocknal_tunables.ksnd_protocol == 2 &&
1615 version == KSOCK_PROTO_V3)
1618 if (version == KSOCK_PROTO_V2)
1619 return &ksocknal_protocol_v2x;
1621 if (version == KSOCK_PROTO_V3)
1622 return &ksocknal_protocol_v3x;
1627 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1628 lnet_magicversion_t *hmv = (lnet_magicversion_t *)hello;
1630 CLASSERT (sizeof (lnet_magicversion_t) ==
1631 offsetof (ksock_hello_msg_t, kshm_src_nid));
1633 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1634 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1635 return &ksocknal_protocol_v1x;
1642 ksocknal_send_hello (lnet_ni_t *ni, ksock_conn_t *conn,
1643 lnet_nid_t peer_nid, ksock_hello_msg_t *hello)
1645 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
1646 ksock_net_t *net = (ksock_net_t *)ni->ni_data;
1648 LASSERT (hello->kshm_nips <= LNET_MAX_INTERFACES);
1650 /* rely on caller to hold a ref on socket so it wouldn't disappear */
1651 LASSERT (conn->ksnc_proto != NULL);
1653 hello->kshm_src_nid = ni->ni_nid;
1654 hello->kshm_dst_nid = peer_nid;
1655 hello->kshm_src_pid = the_lnet.ln_pid;
1657 hello->kshm_src_incarnation = net->ksnn_incarnation;
1658 hello->kshm_ctype = conn->ksnc_type;
1660 return conn->ksnc_proto->pro_send_hello(conn, hello);
1664 ksocknal_invert_type(int type)
1668 case SOCKLND_CONN_ANY:
1669 case SOCKLND_CONN_CONTROL:
1671 case SOCKLND_CONN_BULK_IN:
1672 return SOCKLND_CONN_BULK_OUT;
1673 case SOCKLND_CONN_BULK_OUT:
1674 return SOCKLND_CONN_BULK_IN;
1676 return (SOCKLND_CONN_NONE);
1681 ksocknal_recv_hello (lnet_ni_t *ni, ksock_conn_t *conn,
1682 ksock_hello_msg_t *hello, lnet_process_id_t *peerid,
1685 /* Return < 0 fatal error
1687 * EALREADY lost connection race
1688 * EPROTO protocol version mismatch
1690 cfs_socket_t *sock = conn->ksnc_sock;
1691 int active = (conn->ksnc_proto != NULL);
1695 ksock_proto_t *proto;
1696 lnet_process_id_t recv_id;
1698 /* socket type set on active connections - not set on passive */
1699 LASSERT (!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
1701 timeout = active ? *ksocknal_tunables.ksnd_timeout :
1702 lnet_acceptor_timeout();
1704 rc = libcfs_sock_read(sock, &hello->kshm_magic, sizeof (hello->kshm_magic), timeout);
1706 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
1707 rc, HIPQUAD(conn->ksnc_ipaddr));
1712 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
1713 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
1714 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
1715 /* Unexpected magic! */
1716 CERROR ("Bad magic(1) %#08x (%#08x expected) from "
1717 "%u.%u.%u.%u\n", __cpu_to_le32 (hello->kshm_magic),
1718 LNET_PROTO_TCP_MAGIC,
1719 HIPQUAD(conn->ksnc_ipaddr));
1723 rc = libcfs_sock_read(sock, &hello->kshm_version,
1724 sizeof(hello->kshm_version), timeout);
1726 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
1727 rc, HIPQUAD(conn->ksnc_ipaddr));
1732 proto = ksocknal_parse_proto_version(hello);
1733 if (proto == NULL) {
1735 /* unknown protocol from peer, tell peer my protocol */
1736 conn->ksnc_proto = &ksocknal_protocol_v3x;
1737 #if SOCKNAL_VERSION_DEBUG
1738 if (*ksocknal_tunables.ksnd_protocol == 2)
1739 conn->ksnc_proto = &ksocknal_protocol_v2x;
1740 else if (*ksocknal_tunables.ksnd_protocol == 1)
1741 conn->ksnc_proto = &ksocknal_protocol_v1x;
1743 hello->kshm_nips = 0;
1744 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
1747 CERROR ("Unknown protocol version (%d.x expected)"
1748 " from %u.%u.%u.%u\n",
1749 conn->ksnc_proto->pro_version,
1750 HIPQUAD(conn->ksnc_ipaddr));
1755 proto_match = (conn->ksnc_proto == proto);
1756 conn->ksnc_proto = proto;
1758 /* receive the rest of hello message anyway */
1759 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
1761 CERROR("Error %d reading or checking hello from from %u.%u.%u.%u\n",
1762 rc, HIPQUAD(conn->ksnc_ipaddr));
1767 *incarnation = hello->kshm_src_incarnation;
1769 if (hello->kshm_src_nid == LNET_NID_ANY) {
1770 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
1771 "from %u.%u.%u.%u\n", HIPQUAD(conn->ksnc_ipaddr));
1776 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
1777 /* Userspace NAL assigns peer process ID from socket */
1778 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
1779 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
1781 recv_id.nid = hello->kshm_src_nid;
1782 recv_id.pid = hello->kshm_src_pid;
1788 /* peer determines type */
1789 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
1790 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
1791 CERROR ("Unexpected type %d from %s ip %u.%u.%u.%u\n",
1792 hello->kshm_ctype, libcfs_id2str(*peerid),
1793 HIPQUAD(conn->ksnc_ipaddr));
1800 if (peerid->pid != recv_id.pid ||
1801 peerid->nid != recv_id.nid) {
1802 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
1803 " %u.%u.%u.%u, but they claimed they were "
1804 "%s; please check your Lustre "
1806 libcfs_id2str(*peerid),
1807 HIPQUAD(conn->ksnc_ipaddr),
1808 libcfs_id2str(recv_id));
1812 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
1813 /* Possible protocol mismatch or I lost the connection race */
1814 return proto_match ? EALREADY : EPROTO;
1817 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
1818 CERROR ("Mismatched types: me %d, %s ip %u.%u.%u.%u %d\n",
1819 conn->ksnc_type, libcfs_id2str(*peerid),
1820 HIPQUAD(conn->ksnc_ipaddr),
1829 ksocknal_connect (ksock_route_t *route)
1831 CFS_LIST_HEAD (zombies);
1832 ksock_peer_t *peer = route->ksnr_peer;
1836 cfs_time_t deadline;
1837 int retry_later = 0;
1840 deadline = cfs_time_add(cfs_time_current(),
1841 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout));
1843 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1845 LASSERT (route->ksnr_scheduled);
1846 LASSERT (!route->ksnr_connecting);
1848 route->ksnr_connecting = 1;
1851 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
1853 /* stop connecting if peer/route got closed under me, or
1854 * route got connected while queued */
1855 if (peer->ksnp_closing || route->ksnr_deleted ||
1861 /* reschedule if peer is connecting to me */
1862 if (peer->ksnp_accepting > 0) {
1864 "peer %s(%d) already connecting to me, retry later.\n",
1865 libcfs_nid2str(peer->ksnp_id.nid), peer->ksnp_accepting);
1869 if (retry_later) /* needs reschedule */
1872 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
1873 type = SOCKLND_CONN_ANY;
1874 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
1875 type = SOCKLND_CONN_CONTROL;
1876 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
1877 type = SOCKLND_CONN_BULK_IN;
1879 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
1880 type = SOCKLND_CONN_BULK_OUT;
1883 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1885 if (cfs_time_aftereq(cfs_time_current(), deadline)) {
1887 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1893 rc = lnet_connect(&sock, peer->ksnp_id.nid,
1894 route->ksnr_myipaddr,
1895 route->ksnr_ipaddr, route->ksnr_port);
1899 rc = ksocknal_create_conn(peer->ksnp_ni, route, sock, type);
1901 lnet_connect_console_error(rc, peer->ksnp_id.nid,
1907 /* A +ve RC means I have to retry because I lost the connection
1908 * race or I have to renegotiate protocol version */
1909 retry_later = (rc != 0);
1911 CDEBUG(D_NET, "peer %s: conn race, retry later.\n",
1912 libcfs_nid2str(peer->ksnp_id.nid));
1914 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1917 route->ksnr_scheduled = 0;
1918 route->ksnr_connecting = 0;
1921 /* re-queue for attention; this frees me up to handle
1922 * the peer's incoming connection request */
1924 if (rc == EALREADY ||
1925 (rc == 0 && peer->ksnp_accepting > 0)) {
1926 /* We want to introduce a delay before next
1927 * attempt to connect if we lost conn race,
1928 * but the race is resolved quickly usually,
1929 * so min_reconnectms should be good heuristic */
1930 route->ksnr_retry_interval =
1931 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms)/1000;
1932 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
1933 route->ksnr_retry_interval);
1936 ksocknal_launch_connection_locked(route);
1939 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1943 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
1945 route->ksnr_scheduled = 0;
1946 route->ksnr_connecting = 0;
1948 /* This is a retry rather than a new connection */
1949 route->ksnr_retry_interval *= 2;
1950 route->ksnr_retry_interval =
1951 MAX(route->ksnr_retry_interval,
1952 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms)/1000);
1953 route->ksnr_retry_interval =
1954 MIN(route->ksnr_retry_interval,
1955 cfs_time_seconds(*ksocknal_tunables.ksnd_max_reconnectms)/1000);
1957 LASSERT (route->ksnr_retry_interval != 0);
1958 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
1959 route->ksnr_retry_interval);
1961 if (!cfs_list_empty(&peer->ksnp_tx_queue) &&
1962 peer->ksnp_accepting == 0 &&
1963 ksocknal_find_connecting_route_locked(peer) == NULL) {
1966 /* ksnp_tx_queue is queued on a conn on successful
1967 * connection for V1.x and V2.x */
1968 if (!cfs_list_empty (&peer->ksnp_conns)) {
1969 conn = cfs_list_entry(peer->ksnp_conns.next,
1970 ksock_conn_t, ksnc_list);
1971 LASSERT (conn->ksnc_proto == &ksocknal_protocol_v3x);
1974 /* take all the blocked packets while I've got the lock and
1975 * complete below... */
1976 cfs_list_splice_init(&peer->ksnp_tx_queue, &zombies);
1979 #if 0 /* irrelevent with only eager routes */
1980 if (!route->ksnr_deleted) {
1981 /* make this route least-favourite for re-selection */
1982 cfs_list_del(&route->ksnr_list);
1983 cfs_list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
1986 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1988 ksocknal_peer_failed(peer);
1989 ksocknal_txlist_done(peer->ksnp_ni, &zombies, 1);
1993 /* Go through connd_routes queue looking for a route that
1994 we can process right now */
1995 static ksock_route_t *
1996 ksocknal_connd_get_route_locked(signed long *timeout_p)
1998 ksock_route_t *route;
2001 /* Only handle an outgoing connection request if there
2002 * is someone left to handle incoming connections */
2003 if ((ksocknal_data.ksnd_connd_connecting + 1) >=
2004 *ksocknal_tunables.ksnd_nconnds)
2007 now = cfs_time_current();
2009 /* connd_routes can contain both pending and ordinary routes */
2010 cfs_list_for_each_entry (route, &ksocknal_data.ksnd_connd_routes,
2013 if (route->ksnr_retry_interval == 0 ||
2014 cfs_time_aftereq(now, route->ksnr_timeout))
2017 if (*timeout_p == CFS_MAX_SCHEDULE_TIMEOUT ||
2018 (int)*timeout_p > (int)(route->ksnr_timeout - now))
2019 *timeout_p = (int)(route->ksnr_timeout - now);
2026 ksocknal_connd (void *arg)
2028 long id = (long)(long_ptr_t)arg;
2030 ksock_connreq_t *cr;
2031 ksock_route_t *route;
2032 cfs_waitlink_t wait;
2033 signed long timeout;
2038 snprintf (name, sizeof (name), "socknal_cd%02ld", id);
2039 cfs_daemonize (name);
2040 cfs_block_allsigs ();
2042 cfs_waitlink_init (&wait);
2044 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2046 while (!ksocknal_data.ksnd_shuttingdown) {
2050 if (!cfs_list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2051 /* Connection accepted by the listener */
2052 cr = cfs_list_entry(ksocknal_data.ksnd_connd_connreqs. \
2053 next, ksock_connreq_t, ksncr_list);
2055 cfs_list_del(&cr->ksncr_list);
2056 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2059 ksocknal_create_conn(cr->ksncr_ni, NULL,
2060 cr->ksncr_sock, SOCKLND_CONN_NONE);
2061 lnet_ni_decref(cr->ksncr_ni);
2062 LIBCFS_FREE(cr, sizeof(*cr));
2064 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2067 /* Sleep till explicit wake_up if no pending routes present */
2068 timeout = CFS_MAX_SCHEDULE_TIMEOUT;
2070 /* Connection request */
2071 route = ksocknal_connd_get_route_locked(&timeout);
2073 if (route != NULL) {
2074 cfs_list_del (&route->ksnr_connd_list);
2075 ksocknal_data.ksnd_connd_connecting++;
2076 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2079 if (ksocknal_connect(route)) {
2080 /* consecutive retry */
2081 if (cons_retry++ > SOCKNAL_INSANITY_RECONN) {
2082 CWARN("massive consecutive "
2083 "re-connecting to %u.%u.%u.%u\n",
2084 HIPQUAD(route->ksnr_ipaddr));
2091 ksocknal_route_decref(route);
2093 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2094 ksocknal_data.ksnd_connd_connecting--;
2098 if (++nloops < SOCKNAL_RESCHED)
2100 cfs_spin_unlock_bh(&ksocknal_data.ksnd_connd_lock);
2103 cfs_spin_lock_bh(&ksocknal_data.ksnd_connd_lock);
2107 /* Nothing to do for 'timeout' */
2108 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
2109 cfs_waitq_add_exclusive (&ksocknal_data.ksnd_connd_waitq,
2111 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2114 cfs_waitq_timedwait (&wait, CFS_TASK_INTERRUPTIBLE, timeout);
2116 cfs_set_current_state (CFS_TASK_RUNNING);
2117 cfs_waitq_del (&ksocknal_data.ksnd_connd_waitq, &wait);
2118 cfs_spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2121 cfs_spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2123 ksocknal_thread_fini ();
2128 ksocknal_find_timed_out_conn (ksock_peer_t *peer)
2130 /* We're called with a shared lock on ksnd_global_lock */
2134 cfs_list_for_each (ctmp, &peer->ksnp_conns) {
2136 conn = cfs_list_entry (ctmp, ksock_conn_t, ksnc_list);
2138 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2139 LASSERT (!conn->ksnc_closing);
2141 /* SOCK_ERROR will reset error code of socket in
2142 * some platform (like Darwin8.x) */
2143 error = libcfs_sock_error(conn->ksnc_sock);
2145 ksocknal_conn_addref(conn);
2149 CDEBUG(D_NETERROR, "A connection with %s "
2150 "(%u.%u.%u.%u:%d) was reset; "
2151 "it may have rebooted.\n",
2152 libcfs_id2str(peer->ksnp_id),
2153 HIPQUAD(conn->ksnc_ipaddr),
2157 CDEBUG(D_NETERROR, "A connection with %s "
2158 "(%u.%u.%u.%u:%d) timed out; the "
2159 "network or node may be down.\n",
2160 libcfs_id2str(peer->ksnp_id),
2161 HIPQUAD(conn->ksnc_ipaddr),
2165 CDEBUG(D_NETERROR, "An unexpected network error %d "
2167 "(%u.%u.%u.%u:%d\n", error,
2168 libcfs_id2str(peer->ksnp_id),
2169 HIPQUAD(conn->ksnc_ipaddr),
2177 if (conn->ksnc_rx_started &&
2178 cfs_time_aftereq(cfs_time_current(),
2179 conn->ksnc_rx_deadline)) {
2180 /* Timed out incomplete incoming message */
2181 ksocknal_conn_addref(conn);
2182 CDEBUG(D_NETERROR, "Timeout receiving from %s "
2183 "(%u.%u.%u.%u:%d), state %d wanted %d left %d\n",
2184 libcfs_id2str(peer->ksnp_id),
2185 HIPQUAD(conn->ksnc_ipaddr),
2187 conn->ksnc_rx_state,
2188 conn->ksnc_rx_nob_wanted,
2189 conn->ksnc_rx_nob_left);
2193 if ((!cfs_list_empty(&conn->ksnc_tx_queue) ||
2194 libcfs_sock_wmem_queued(conn->ksnc_sock) != 0) &&
2195 cfs_time_aftereq(cfs_time_current(),
2196 conn->ksnc_tx_deadline)) {
2197 /* Timed out messages queued for sending or
2198 * buffered in the socket's send buffer */
2199 ksocknal_conn_addref(conn);
2200 CDEBUG(D_NETERROR, "Timeout sending data to %s "
2201 "(%u.%u.%u.%u:%d) the network or that "
2202 "node may be down.\n",
2203 libcfs_id2str(peer->ksnp_id),
2204 HIPQUAD(conn->ksnc_ipaddr),
2214 ksocknal_flush_stale_txs(ksock_peer_t *peer)
2217 CFS_LIST_HEAD (stale_txs);
2219 cfs_write_lock_bh (&ksocknal_data.ksnd_global_lock);
2221 while (!cfs_list_empty (&peer->ksnp_tx_queue)) {
2222 tx = cfs_list_entry (peer->ksnp_tx_queue.next,
2223 ksock_tx_t, tx_list);
2225 if (!cfs_time_aftereq(cfs_time_current(),
2229 cfs_list_del (&tx->tx_list);
2230 cfs_list_add_tail (&tx->tx_list, &stale_txs);
2233 cfs_write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2235 ksocknal_txlist_done(peer->ksnp_ni, &stale_txs, 1);
2239 ksocknal_send_keepalive_locked(ksock_peer_t *peer)
2241 ksock_sched_t *sched;
2245 if (cfs_list_empty(&peer->ksnp_conns)) /* last_alive will be updated by create_conn */
2248 if (peer->ksnp_proto != &ksocknal_protocol_v3x)
2251 if (*ksocknal_tunables.ksnd_keepalive <= 0 ||
2252 cfs_time_before(cfs_time_current(),
2253 cfs_time_add(peer->ksnp_last_alive,
2254 cfs_time_seconds(*ksocknal_tunables.ksnd_keepalive))))
2257 if (cfs_time_before(cfs_time_current(),
2258 peer->ksnp_send_keepalive))
2261 /* retry 10 secs later, so we wouldn't put pressure
2262 * on this peer if we failed to send keepalive this time */
2263 peer->ksnp_send_keepalive = cfs_time_shift(10);
2265 conn = ksocknal_find_conn_locked(peer, NULL, 1);
2267 sched = conn->ksnc_scheduler;
2269 cfs_spin_lock_bh (&sched->kss_lock);
2270 if (!cfs_list_empty(&conn->ksnc_tx_queue)) {
2271 cfs_spin_unlock_bh(&sched->kss_lock);
2272 /* there is an queued ACK, don't need keepalive */
2276 cfs_spin_unlock_bh(&sched->kss_lock);
2279 cfs_read_unlock(&ksocknal_data.ksnd_global_lock);
2281 /* cookie = 1 is reserved for keepalive PING */
2282 tx = ksocknal_alloc_tx_noop(1, 1);
2284 cfs_read_lock(&ksocknal_data.ksnd_global_lock);
2288 if (ksocknal_launch_packet(peer->ksnp_ni, tx, peer->ksnp_id) == 0) {
2289 cfs_read_lock(&ksocknal_data.ksnd_global_lock);
2293 ksocknal_free_tx(tx);
2294 cfs_read_lock(&ksocknal_data.ksnd_global_lock);
2301 ksocknal_check_peer_timeouts (int idx)
2303 cfs_list_t *peers = &ksocknal_data.ksnd_peers[idx];
2309 /* NB. We expect to have a look at all the peers and not find any
2310 * connections to time out, so we just use a shared lock while we
2312 cfs_read_lock (&ksocknal_data.ksnd_global_lock);
2314 cfs_list_for_each_entry_typed(peer, peers, ksock_peer_t, ksnp_list) {
2315 cfs_time_t deadline = 0;
2319 if (ksocknal_send_keepalive_locked(peer) != 0) {
2320 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2324 conn = ksocknal_find_timed_out_conn (peer);
2327 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2329 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2331 /* NB we won't find this one again, but we can't
2332 * just proceed with the next peer, since we dropped
2333 * ksnd_global_lock and it might be dead already! */
2334 ksocknal_conn_decref(conn);
2338 /* we can't process stale txs right here because we're
2339 * holding only shared lock */
2340 if (!cfs_list_empty (&peer->ksnp_tx_queue)) {
2342 cfs_list_entry (peer->ksnp_tx_queue.next,
2343 ksock_tx_t, tx_list);
2345 if (cfs_time_aftereq(cfs_time_current(),
2348 ksocknal_peer_addref(peer);
2349 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2351 ksocknal_flush_stale_txs(peer);
2353 ksocknal_peer_decref(peer);
2358 if (cfs_list_empty(&peer->ksnp_zc_req_list))
2361 cfs_spin_lock(&peer->ksnp_lock);
2362 cfs_list_for_each_entry_typed(tx, &peer->ksnp_zc_req_list,
2363 ksock_tx_t, tx_zc_list) {
2364 if (!cfs_time_aftereq(cfs_time_current(),
2367 /* ignore the TX if connection is being closed */
2368 if (tx->tx_conn->ksnc_closing)
2374 cfs_spin_unlock(&peer->ksnp_lock);
2378 tx = cfs_list_entry(peer->ksnp_zc_req_list.next,
2379 ksock_tx_t, tx_zc_list);
2380 deadline = tx->tx_deadline;
2381 resid = tx->tx_resid;
2383 ksocknal_conn_addref(conn);
2385 cfs_spin_unlock(&peer->ksnp_lock);
2386 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2388 CERROR("Total %d stale ZC_REQs for peer %s detected; the "
2389 "oldest(%p) timed out %ld secs ago, "
2390 "resid: %d, wmem: %d\n",
2391 n, libcfs_nid2str(peer->ksnp_id.nid), tx,
2392 cfs_duration_sec(cfs_time_current() - deadline),
2393 resid, libcfs_sock_wmem_queued(conn->ksnc_sock));
2395 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2396 ksocknal_conn_decref(conn);
2400 cfs_read_unlock (&ksocknal_data.ksnd_global_lock);
2404 ksocknal_reaper (void *arg)
2406 cfs_waitlink_t wait;
2408 ksock_sched_t *sched;
2409 cfs_list_t enomem_conns;
2411 cfs_duration_t timeout;
2414 cfs_time_t deadline = cfs_time_current();
2416 cfs_daemonize ("socknal_reaper");
2417 cfs_block_allsigs ();
2419 CFS_INIT_LIST_HEAD(&enomem_conns);
2420 cfs_waitlink_init (&wait);
2422 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2424 while (!ksocknal_data.ksnd_shuttingdown) {
2426 if (!cfs_list_empty (&ksocknal_data.ksnd_deathrow_conns)) {
2427 conn = cfs_list_entry (ksocknal_data. \
2428 ksnd_deathrow_conns.next,
2429 ksock_conn_t, ksnc_list);
2430 cfs_list_del (&conn->ksnc_list);
2432 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2434 ksocknal_terminate_conn (conn);
2435 ksocknal_conn_decref(conn);
2437 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2441 if (!cfs_list_empty (&ksocknal_data.ksnd_zombie_conns)) {
2442 conn = cfs_list_entry (ksocknal_data.ksnd_zombie_conns.\
2443 next, ksock_conn_t, ksnc_list);
2444 cfs_list_del (&conn->ksnc_list);
2446 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2448 ksocknal_destroy_conn (conn);
2450 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2454 if (!cfs_list_empty (&ksocknal_data.ksnd_enomem_conns)) {
2455 cfs_list_add(&enomem_conns,
2456 &ksocknal_data.ksnd_enomem_conns);
2457 cfs_list_del_init(&ksocknal_data.ksnd_enomem_conns);
2460 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2462 /* reschedule all the connections that stalled with ENOMEM... */
2464 while (!cfs_list_empty (&enomem_conns)) {
2465 conn = cfs_list_entry (enomem_conns.next,
2466 ksock_conn_t, ksnc_tx_list);
2467 cfs_list_del (&conn->ksnc_tx_list);
2469 sched = conn->ksnc_scheduler;
2471 cfs_spin_lock_bh (&sched->kss_lock);
2473 LASSERT (conn->ksnc_tx_scheduled);
2474 conn->ksnc_tx_ready = 1;
2475 cfs_list_add_tail(&conn->ksnc_tx_list,
2476 &sched->kss_tx_conns);
2477 cfs_waitq_signal (&sched->kss_waitq);
2479 cfs_spin_unlock_bh (&sched->kss_lock);
2483 /* careful with the jiffy wrap... */
2484 while ((timeout = cfs_time_sub(deadline,
2485 cfs_time_current())) <= 0) {
2488 int chunk = ksocknal_data.ksnd_peer_hash_size;
2490 /* Time to check for timeouts on a few more peers: I do
2491 * checks every 'p' seconds on a proportion of the peer
2492 * table and I need to check every connection 'n' times
2493 * within a timeout interval, to ensure I detect a
2494 * timeout on any connection within (n+1)/n times the
2495 * timeout interval. */
2497 if (*ksocknal_tunables.ksnd_timeout > n * p)
2498 chunk = (chunk * n * p) /
2499 *ksocknal_tunables.ksnd_timeout;
2503 for (i = 0; i < chunk; i++) {
2504 ksocknal_check_peer_timeouts (peer_index);
2505 peer_index = (peer_index + 1) %
2506 ksocknal_data.ksnd_peer_hash_size;
2509 deadline = cfs_time_add(deadline, cfs_time_seconds(p));
2512 if (nenomem_conns != 0) {
2513 /* Reduce my timeout if I rescheduled ENOMEM conns.
2514 * This also prevents me getting woken immediately
2515 * if any go back on my enomem list. */
2516 timeout = SOCKNAL_ENOMEM_RETRY;
2518 ksocknal_data.ksnd_reaper_waketime =
2519 cfs_time_add(cfs_time_current(), timeout);
2521 cfs_set_current_state (CFS_TASK_INTERRUPTIBLE);
2522 cfs_waitq_add (&ksocknal_data.ksnd_reaper_waitq, &wait);
2524 if (!ksocknal_data.ksnd_shuttingdown &&
2525 cfs_list_empty (&ksocknal_data.ksnd_deathrow_conns) &&
2526 cfs_list_empty (&ksocknal_data.ksnd_zombie_conns))
2527 cfs_waitq_timedwait (&wait, CFS_TASK_INTERRUPTIBLE,
2530 cfs_set_current_state (CFS_TASK_RUNNING);
2531 cfs_waitq_del (&ksocknal_data.ksnd_reaper_waitq, &wait);
2533 cfs_spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2536 cfs_spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2538 ksocknal_thread_fini ();