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 size)
32 ksock_tx_t *tx = NULL;
34 if (size == KSOCK_NOOP_TX_SIZE) {
35 /* searching for a noop tx in free list */
36 spin_lock(&ksocknal_data.ksnd_tx_lock);
38 if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
39 tx = list_entry(ksocknal_data.ksnd_idle_noop_txs.next,
41 LASSERT(tx->tx_desc_size == size);
42 list_del(&tx->tx_list);
45 spin_unlock(&ksocknal_data.ksnd_tx_lock);
49 LIBCFS_ALLOC(tx, size);
54 atomic_set(&tx->tx_refcount, 1);
55 tx->tx_desc_size = size;
56 atomic_inc(&ksocknal_data.ksnd_nactive_txs);
62 ksocknal_free_tx (ksock_tx_t *tx)
64 atomic_dec(&ksocknal_data.ksnd_nactive_txs);
66 if (tx->tx_desc_size == KSOCK_NOOP_TX_SIZE) {
68 spin_lock(&ksocknal_data.ksnd_tx_lock);
70 list_add(&tx->tx_list, &ksocknal_data.ksnd_idle_noop_txs);
72 spin_unlock(&ksocknal_data.ksnd_tx_lock);
74 LIBCFS_FREE(tx, tx->tx_desc_size);
79 ksocknal_init_msg(ksock_msg_t *msg, int type)
83 msg->ksm_zc_req_cookie = 0;
84 msg->ksm_zc_ack_cookie = 0;
88 ksocknal_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
90 struct iovec *iov = tx->tx_iov;
94 LASSERT (tx->tx_niov > 0);
96 /* Never touch tx->tx_iov inside ksocknal_lib_send_iov() */
97 rc = ksocknal_lib_send_iov(conn, tx);
99 if (rc <= 0) /* sent nothing? */
103 LASSERT (nob <= tx->tx_resid);
108 LASSERT (tx->tx_niov > 0);
110 if (nob < (int) iov->iov_len) {
111 iov->iov_base = (void *)((char *)iov->iov_base + nob);
125 ksocknal_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
127 lnet_kiov_t *kiov = tx->tx_kiov;
131 LASSERT (tx->tx_niov == 0);
132 LASSERT (tx->tx_nkiov > 0);
134 /* Never touch tx->tx_kiov inside ksocknal_lib_send_kiov() */
135 rc = ksocknal_lib_send_kiov(conn, tx);
137 if (rc <= 0) /* sent nothing? */
141 LASSERT (nob <= tx->tx_resid);
146 LASSERT(tx->tx_nkiov > 0);
148 if (nob < (int)kiov->kiov_len) {
149 kiov->kiov_offset += nob;
150 kiov->kiov_len -= nob;
154 nob -= (int)kiov->kiov_len;
155 tx->tx_kiov = ++kiov;
163 ksocknal_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
168 if (ksocknal_data.ksnd_stall_tx != 0) {
169 cfs_pause(cfs_time_seconds(ksocknal_data.ksnd_stall_tx));
172 LASSERT (tx->tx_resid != 0);
174 rc = ksocknal_connsock_addref(conn);
176 LASSERT (conn->ksnc_closing);
181 if (ksocknal_data.ksnd_enomem_tx > 0) {
183 ksocknal_data.ksnd_enomem_tx--;
185 } else if (tx->tx_niov != 0) {
186 rc = ksocknal_send_iov (conn, tx);
188 rc = ksocknal_send_kiov (conn, tx);
191 bufnob = SOCK_WMEM_QUEUED(conn->ksnc_sock);
192 if (rc > 0) /* sent something? */
193 conn->ksnc_tx_bufnob += rc; /* account it */
195 if (bufnob < conn->ksnc_tx_bufnob) {
196 /* allocated send buffer bytes < computed; infer
197 * something got ACKed */
198 conn->ksnc_tx_deadline =
199 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
200 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
201 conn->ksnc_tx_bufnob = bufnob;
205 if (rc <= 0) { /* Didn't write anything? */
206 ksock_sched_t *sched;
208 if (rc == 0) /* some stacks return 0 instead of -EAGAIN */
214 /* Check if EAGAIN is due to memory pressure */
216 sched = conn->ksnc_scheduler;
217 spin_lock_bh (&sched->kss_lock);
219 if (!SOCK_TEST_NOSPACE(conn->ksnc_sock) &&
220 !conn->ksnc_tx_ready) {
221 /* SOCK_NOSPACE is set when the socket fills
222 * and cleared in the write_space callback
223 * (which also sets ksnc_tx_ready). If
224 * SOCK_NOSPACE and ksnc_tx_ready are BOTH
225 * zero, I didn't fill the socket and
226 * write_space won't reschedule me, so I
227 * return -ENOMEM to get my caller to retry
232 spin_unlock_bh (&sched->kss_lock);
236 /* socket's wmem_queued now includes 'rc' bytes */
237 atomic_sub (rc, &conn->ksnc_tx_nob);
240 } while (tx->tx_resid != 0);
242 ksocknal_connsock_decref(conn);
247 ksocknal_recv_iov (ksock_conn_t *conn)
249 struct iovec *iov = conn->ksnc_rx_iov;
253 LASSERT (conn->ksnc_rx_niov > 0);
255 /* Never touch conn->ksnc_rx_iov or change connection
256 * status inside ksocknal_lib_recv_iov */
257 rc = ksocknal_lib_recv_iov(conn);
262 /* received something... */
265 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
266 conn->ksnc_rx_deadline =
267 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
268 mb(); /* order with setting rx_started */
269 conn->ksnc_rx_started = 1;
271 conn->ksnc_rx_nob_wanted -= nob;
272 conn->ksnc_rx_nob_left -= nob;
275 LASSERT (conn->ksnc_rx_niov > 0);
277 if (nob < (int)iov->iov_len) {
279 iov->iov_base = (void *)((char *)iov->iov_base + nob);
284 conn->ksnc_rx_iov = ++iov;
285 conn->ksnc_rx_niov--;
292 ksocknal_recv_kiov (ksock_conn_t *conn)
294 lnet_kiov_t *kiov = conn->ksnc_rx_kiov;
297 LASSERT (conn->ksnc_rx_nkiov > 0);
299 /* Never touch conn->ksnc_rx_kiov or change connection
300 * status inside ksocknal_lib_recv_iov */
301 rc = ksocknal_lib_recv_kiov(conn);
306 /* received something... */
309 conn->ksnc_peer->ksnp_last_alive = cfs_time_current();
310 conn->ksnc_rx_deadline =
311 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
312 mb(); /* order with setting rx_started */
313 conn->ksnc_rx_started = 1;
315 conn->ksnc_rx_nob_wanted -= nob;
316 conn->ksnc_rx_nob_left -= nob;
319 LASSERT (conn->ksnc_rx_nkiov > 0);
321 if (nob < (int) kiov->kiov_len) {
322 kiov->kiov_offset += nob;
323 kiov->kiov_len -= nob;
327 nob -= kiov->kiov_len;
328 conn->ksnc_rx_kiov = ++kiov;
329 conn->ksnc_rx_nkiov--;
336 ksocknal_receive (ksock_conn_t *conn)
338 /* Return 1 on success, 0 on EOF, < 0 on error.
339 * Caller checks ksnc_rx_nob_wanted to determine
340 * progress/completion. */
344 if (ksocknal_data.ksnd_stall_rx != 0) {
345 cfs_pause(cfs_time_seconds (ksocknal_data.ksnd_stall_rx));
348 rc = ksocknal_connsock_addref(conn);
350 LASSERT (conn->ksnc_closing);
355 if (conn->ksnc_rx_niov != 0)
356 rc = ksocknal_recv_iov (conn);
358 rc = ksocknal_recv_kiov (conn);
361 /* error/EOF or partial receive */
364 } else if (rc == 0 && conn->ksnc_rx_started) {
365 /* EOF in the middle of a message */
371 /* Completed a fragment */
373 if (conn->ksnc_rx_nob_wanted == 0) {
379 ksocknal_connsock_decref(conn);
384 ksocknal_tx_done (lnet_ni_t *ni, ksock_tx_t *tx)
386 lnet_msg_t *lnetmsg = tx->tx_lnetmsg;
387 int rc = (tx->tx_resid == 0) ? 0 : -EIO;
390 LASSERT(ni != NULL || tx->tx_conn != NULL);
392 if (tx->tx_conn != NULL)
393 ksocknal_conn_decref(tx->tx_conn);
395 if (ni == NULL && tx->tx_conn != NULL)
396 ni = tx->tx_conn->ksnc_peer->ksnp_ni;
398 ksocknal_free_tx (tx);
399 if (lnetmsg != NULL) /* KSOCK_MSG_NOOP go without lnetmsg */
400 lnet_finalize (ni, lnetmsg, rc);
406 ksocknal_txlist_done (lnet_ni_t *ni, struct list_head *txlist, int error)
410 while (!list_empty (txlist)) {
411 tx = list_entry (txlist->next, ksock_tx_t, tx_list);
413 if (error && tx->tx_lnetmsg != NULL) {
414 CDEBUG (D_NETERROR, "Deleting packet type %d len %d %s->%s\n",
415 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.type),
416 le32_to_cpu (tx->tx_lnetmsg->msg_hdr.payload_length),
417 libcfs_nid2str(le64_to_cpu(tx->tx_lnetmsg->msg_hdr.src_nid)),
418 libcfs_nid2str(le64_to_cpu (tx->tx_lnetmsg->msg_hdr.dest_nid)));
420 CDEBUG (D_NETERROR, "Deleting noop packet\n");
423 list_del (&tx->tx_list);
425 LASSERT (atomic_read(&tx->tx_refcount) == 1);
426 ksocknal_tx_done (ni, tx);
431 ksocknal_check_zc_req(ksock_tx_t *tx)
433 ksock_conn_t *conn = tx->tx_conn;
434 ksock_peer_t *peer = conn->ksnc_peer;
435 lnet_kiov_t *kiov = tx->tx_kiov;
436 int nkiov = tx->tx_nkiov;
438 /* Set tx_msg.ksm_zc_req_cookie to a unique non-zero cookie and add tx
439 * to ksnp_zc_req_list if some fragment of this message should be sent
440 * zero-copy. Our peer will send an ACK containing this cookie when
441 * she has received this message to tell us we can signal completion.
442 * tx_msg.ksm_zc_req_cookie remains non-zero while tx is on
443 * ksnp_zc_req_list. */
445 if (conn->ksnc_proto != &ksocknal_protocol_v2x ||
446 !conn->ksnc_zc_capable)
450 if (kiov->kiov_len >= *ksocknal_tunables.ksnd_zc_min_frag)
459 /* assign cookie and queue tx to pending list, it will be released when
460 * a matching ack is received. See ksocknal_handle_zc_ack() */
462 ksocknal_tx_addref(tx);
464 spin_lock(&peer->ksnp_lock);
466 /* ZC_REQ is going to be pinned to the peer */
468 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
470 LASSERT (tx->tx_msg.ksm_zc_req_cookie == 0);
471 tx->tx_msg.ksm_zc_req_cookie = peer->ksnp_zc_next_cookie++;
472 list_add_tail(&tx->tx_zc_list, &peer->ksnp_zc_req_list);
474 spin_unlock(&peer->ksnp_lock);
478 ksocknal_unzc_req(ksock_tx_t *tx)
480 ksock_peer_t *peer = tx->tx_conn->ksnc_peer;
482 spin_lock(&peer->ksnp_lock);
484 if (tx->tx_msg.ksm_zc_req_cookie == 0) {
485 /* Not waiting for an ACK */
486 spin_unlock(&peer->ksnp_lock);
490 tx->tx_msg.ksm_zc_req_cookie = 0;
491 list_del(&tx->tx_zc_list);
493 spin_unlock(&peer->ksnp_lock);
495 ksocknal_tx_decref(tx);
499 ksocknal_process_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
503 if (!tx->tx_checked_zc) {
504 tx->tx_checked_zc = 1;
505 ksocknal_check_zc_req(tx);
508 rc = ksocknal_transmit (conn, tx);
510 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
512 if (tx->tx_resid == 0) {
513 /* Sent everything OK */
525 counter++; /* exponential backoff warnings */
526 if ((counter & (-counter)) == counter)
527 CWARN("%u ENOMEM tx %p (%u allocated)\n",
528 counter, conn, atomic_read(&libcfs_kmemory));
530 /* Queue on ksnd_enomem_conns for retry after a timeout */
531 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
533 /* enomem list takes over scheduler's ref... */
534 LASSERT (conn->ksnc_tx_scheduled);
535 list_add_tail(&conn->ksnc_tx_list,
536 &ksocknal_data.ksnd_enomem_conns);
537 if (!cfs_time_aftereq(cfs_time_add(cfs_time_current(),
538 SOCKNAL_ENOMEM_RETRY),
539 ksocknal_data.ksnd_reaper_waketime))
540 cfs_waitq_signal (&ksocknal_data.ksnd_reaper_waitq);
542 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
549 if (!conn->ksnc_closing) {
552 LCONSOLE_WARN("Host %u.%u.%u.%u reset our connection "
553 "while we were sending data; it may have "
555 HIPQUAD(conn->ksnc_ipaddr));
558 LCONSOLE_WARN("There was an unexpected network error "
559 "while writing to %u.%u.%u.%u: %d.\n",
560 HIPQUAD(conn->ksnc_ipaddr), rc);
563 CDEBUG(D_NET, "[%p] Error %d on write to %s"
564 " ip %d.%d.%d.%d:%d\n", conn, rc,
565 libcfs_id2str(conn->ksnc_peer->ksnp_id),
566 HIPQUAD(conn->ksnc_ipaddr),
570 ksocknal_unzc_req(tx);
572 /* it's not an error if conn is being closed */
573 ksocknal_close_conn_and_siblings (conn,
574 (conn->ksnc_closing) ? 0 : rc);
580 ksocknal_launch_connection_locked (ksock_route_t *route)
583 /* called holding write lock on ksnd_global_lock */
585 LASSERT (!route->ksnr_scheduled);
586 LASSERT (!route->ksnr_connecting);
587 LASSERT ((ksocknal_route_mask() & ~route->ksnr_connected) != 0);
589 route->ksnr_scheduled = 1; /* scheduling conn for connd */
590 ksocknal_route_addref(route); /* extra ref for connd */
592 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
594 list_add_tail (&route->ksnr_connd_list,
595 &ksocknal_data.ksnd_connd_routes);
596 cfs_waitq_signal (&ksocknal_data.ksnd_connd_waitq);
598 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
602 ksocknal_find_conn_locked (int payload_nob, ksock_peer_t *peer)
604 struct list_head *tmp;
605 ksock_conn_t *typed = NULL;
607 ksock_conn_t *fallback = NULL;
611 list_for_each (tmp, &peer->ksnp_conns) {
612 ksock_conn_t *c = list_entry(tmp, ksock_conn_t, ksnc_list);
614 #if SOCKNAL_ROUND_ROBIN
617 int nob = atomic_read(&c->ksnc_tx_nob) +
618 SOCK_WMEM_QUEUED(c->ksnc_sock);
620 LASSERT (!c->ksnc_closing);
621 LASSERT (c->ksnc_proto != NULL);
623 if (fallback == NULL || nob < fnob) {
628 if (!*ksocknal_tunables.ksnd_typed_conns)
631 if (payload_nob == 0) {
633 hdr_nob = offsetof(ksock_msg_t, ksm_u);
636 hdr_nob = (c->ksnc_proto == &ksocknal_protocol_v2x)?
637 sizeof(ksock_msg_t) : sizeof(lnet_hdr_t);
640 switch (c->ksnc_type) {
642 CERROR("ksnc_type bad: %u\n", c->ksnc_type);
644 case SOCKLND_CONN_ANY:
646 case SOCKLND_CONN_BULK_IN:
648 case SOCKLND_CONN_BULK_OUT:
649 if ((hdr_nob + payload_nob) < *ksocknal_tunables.ksnd_min_bulk)
652 case SOCKLND_CONN_CONTROL:
653 if ((hdr_nob + payload_nob) >= *ksocknal_tunables.ksnd_min_bulk)
658 if (typed == NULL || nob < tnob) {
664 /* prefer the typed selection */
665 conn = (typed != NULL) ? typed : fallback;
667 #if SOCKNAL_ROUND_ROBIN
669 /* round-robin all else being equal */
670 list_del (&conn->ksnc_list);
671 list_add_tail (&conn->ksnc_list, &peer->ksnp_conns);
678 ksocknal_next_mono_tx(ksock_conn_t *conn)
680 ksock_tx_t *tx = conn->ksnc_tx_mono;
682 /* Called holding BH lock: conn->ksnc_scheduler->kss_lock */
683 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v2x);
684 LASSERT(!list_empty(&conn->ksnc_tx_queue));
687 if (tx->tx_list.next == &conn->ksnc_tx_queue) {
688 /* no more packets queued */
689 conn->ksnc_tx_mono = NULL;
691 conn->ksnc_tx_mono = list_entry(tx->tx_list.next, ksock_tx_t, tx_list);
692 LASSERT(conn->ksnc_tx_mono->tx_msg.ksm_type == tx->tx_msg.ksm_type);
697 ksocknal_piggyback_zcack(ksock_conn_t *conn, __u64 cookie)
699 ksock_tx_t *tx = conn->ksnc_tx_mono;
701 /* Called holding BH lock: conn->ksnc_scheduler->kss_lock */
706 if (tx->tx_msg.ksm_type == KSOCK_MSG_NOOP) {
707 /* tx is noop zc-ack, can't piggyback zc-ack cookie */
711 LASSERT(tx->tx_msg.ksm_type == KSOCK_MSG_LNET);
712 LASSERT(tx->tx_msg.ksm_zc_ack_cookie == 0);
714 /* piggyback the zc-ack cookie */
715 tx->tx_msg.ksm_zc_ack_cookie = cookie;
716 ksocknal_next_mono_tx(conn);
722 ksocknal_queue_tx_locked (ksock_tx_t *tx, ksock_conn_t *conn)
724 ksock_sched_t *sched = conn->ksnc_scheduler;
725 ksock_msg_t *msg = &tx->tx_msg;
729 /* called holding global lock (read or irq-write) and caller may
730 * not have dropped this lock between finding conn and calling me,
731 * so we don't need the {get,put}connsock dance to deref
733 LASSERT(!conn->ksnc_closing);
735 CDEBUG (D_NET, "Sending to %s ip %d.%d.%d.%d:%d\n",
736 libcfs_id2str(conn->ksnc_peer->ksnp_id),
737 HIPQUAD(conn->ksnc_ipaddr),
740 tx->tx_checked_zc = 0;
741 conn->ksnc_proto->pro_pack(tx);
743 /* Ensure the frags we've been given EXACTLY match the number of
744 * bytes we want to send. Many TCP/IP stacks disregard any total
745 * size parameters passed to them and just look at the frags.
747 * We always expect at least 1 mapped fragment containing the
748 * complete ksocknal message header. */
749 LASSERT (lnet_iov_nob (tx->tx_niov, tx->tx_iov) +
750 lnet_kiov_nob(tx->tx_nkiov, tx->tx_kiov) ==
751 (unsigned int)tx->tx_nob);
752 LASSERT (tx->tx_niov >= 1);
753 LASSERT (tx->tx_resid == tx->tx_nob);
755 CDEBUG (D_NET, "Packet %p type %d, nob %d niov %d nkiov %d\n",
756 tx, (tx->tx_lnetmsg != NULL)? tx->tx_lnetmsg->msg_hdr.type:
758 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
760 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
762 ksocknal_conn_addref(conn); /* +1 ref for tx */
765 * FIXME: SOCK_WMEM_QUEUED and SOCK_ERROR could block in __DARWIN8__
766 * but they're used inside spinlocks a lot.
768 bufnob = SOCK_WMEM_QUEUED(conn->ksnc_sock);
769 spin_lock_bh (&sched->kss_lock);
771 if (list_empty(&conn->ksnc_tx_queue) && bufnob == 0) {
772 /* First packet starts the timeout */
773 conn->ksnc_tx_deadline =
774 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
775 conn->ksnc_tx_bufnob = 0;
776 mb(); /* order with adding to tx_queue */
781 if (msg->ksm_type == KSOCK_MSG_NOOP) {
782 /* The packet is noop ZC ACK, try to piggyback the ack_cookie
783 * on a normal packet so I don't need to send it */
784 LASSERT(msg->ksm_zc_req_cookie == 0);
785 LASSERT(msg->ksm_zc_ack_cookie != 0);
787 if (conn->ksnc_tx_mono != NULL) {
788 if (ksocknal_piggyback_zcack(conn, msg->ksm_zc_ack_cookie)) {
789 /* zc-ack cookie is piggybacked */
790 atomic_sub (tx->tx_nob, &conn->ksnc_tx_nob);
791 ztx = tx; /* Put to freelist later */
793 /* no packet can piggyback zc-ack cookie */
794 list_add_tail (&tx->tx_list, &conn->ksnc_tx_queue);
797 /* It's the first mono-packet */
798 conn->ksnc_tx_mono = tx;
799 list_add_tail (&tx->tx_list, &conn->ksnc_tx_queue);
803 /* It's a normal packet - can it piggback a noop zc-ack that
804 * has been queued already? */
805 LASSERT(msg->ksm_zc_ack_cookie == 0);
807 if (conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x packet */
808 conn->ksnc_tx_mono != NULL) {
809 if (conn->ksnc_tx_mono->tx_msg.ksm_type == KSOCK_MSG_NOOP) {
810 /* There is a noop zc-ack can be piggybacked */
811 ztx = conn->ksnc_tx_mono;
813 msg->ksm_zc_ack_cookie = ztx->tx_msg.ksm_zc_ack_cookie;
814 ksocknal_next_mono_tx(conn);
816 /* use tx to replace the noop zc-ack packet, ztx will
817 * be put to freelist later */
818 list_add(&tx->tx_list, &ztx->tx_list);
819 list_del(&ztx->tx_list);
821 atomic_sub (ztx->tx_nob, &conn->ksnc_tx_nob);
823 /* no noop zc-ack packet, just enqueue it */
824 LASSERT(conn->ksnc_tx_mono->tx_msg.ksm_type == KSOCK_MSG_LNET);
825 list_add_tail (&tx->tx_list, &conn->ksnc_tx_queue);
828 } else if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
829 /* it's the first mono-packet, enqueue it */
830 conn->ksnc_tx_mono = tx;
831 list_add_tail (&tx->tx_list, &conn->ksnc_tx_queue);
833 /* V1.x packet, just enqueue it */
834 list_add_tail (&tx->tx_list, &conn->ksnc_tx_queue);
839 list_add_tail(&ztx->tx_list, &sched->kss_zombie_noop_txs);
841 if (conn->ksnc_tx_ready && /* able to send */
842 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
843 /* +1 ref for scheduler */
844 ksocknal_conn_addref(conn);
845 list_add_tail (&conn->ksnc_tx_list,
846 &sched->kss_tx_conns);
847 conn->ksnc_tx_scheduled = 1;
848 cfs_waitq_signal (&sched->kss_waitq);
851 spin_unlock_bh (&sched->kss_lock);
855 ksocknal_find_connectable_route_locked (ksock_peer_t *peer)
857 struct list_head *tmp;
858 ksock_route_t *route;
860 list_for_each (tmp, &peer->ksnp_routes) {
861 route = list_entry (tmp, ksock_route_t, ksnr_list);
863 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
865 if (route->ksnr_scheduled) /* connections being established */
868 /* all route types connected ? */
869 if ((ksocknal_route_mask() & ~route->ksnr_connected) == 0)
872 /* too soon to retry this guy? */
873 if (!(route->ksnr_retry_interval == 0 || /* first attempt */
874 cfs_time_aftereq (cfs_time_current(),
875 route->ksnr_timeout)))
885 ksocknal_find_connecting_route_locked (ksock_peer_t *peer)
887 struct list_head *tmp;
888 ksock_route_t *route;
890 list_for_each (tmp, &peer->ksnp_routes) {
891 route = list_entry (tmp, ksock_route_t, ksnr_list);
893 LASSERT (!route->ksnr_connecting || route->ksnr_scheduled);
895 if (route->ksnr_scheduled)
903 ksocknal_launch_packet (lnet_ni_t *ni, ksock_tx_t *tx, lnet_process_id_t id)
907 ksock_route_t *route;
912 LASSERT (tx->tx_conn == NULL);
913 LASSERT (tx->tx_lnetmsg != NULL);
915 g_lock = &ksocknal_data.ksnd_global_lock;
917 for (retry = 0;; retry = 1) {
918 #if !SOCKNAL_ROUND_ROBIN
920 peer = ksocknal_find_peer_locked(ni, id);
922 if (ksocknal_find_connectable_route_locked(peer) == NULL) {
923 conn = ksocknal_find_conn_locked (tx->tx_lnetmsg->msg_len, peer);
925 /* I've got no routes that need to be
926 * connecting and I do have an actual
928 ksocknal_queue_tx_locked (tx, conn);
929 read_unlock (g_lock);
935 /* I'll need a write lock... */
936 read_unlock (g_lock);
938 write_lock_bh (g_lock);
940 peer = ksocknal_find_peer_locked(ni, id);
944 write_unlock_bh (g_lock);
946 if ((id.pid & LNET_PID_USERFLAG) != 0) {
947 CERROR("Refusing to create a connection to "
948 "userspace process %s\n", libcfs_id2str(id));
949 return -EHOSTUNREACH;
953 CERROR("Can't find peer %s\n", libcfs_id2str(id));
954 return -EHOSTUNREACH;
957 rc = ksocknal_add_peer(ni, id,
958 LNET_NIDADDR(id.nid),
959 lnet_acceptor_port());
961 CERROR("Can't add peer %s: %d\n",
962 libcfs_id2str(id), rc);
968 /* launch any/all connections that need it */
969 route = ksocknal_find_connectable_route_locked (peer);
973 ksocknal_launch_connection_locked (route);
976 conn = ksocknal_find_conn_locked (tx->tx_lnetmsg->msg_len, peer);
978 /* Connection exists; queue message on it */
979 ksocknal_queue_tx_locked (tx, conn);
980 write_unlock_bh (g_lock);
984 if (peer->ksnp_accepting > 0 ||
985 ksocknal_find_connecting_route_locked (peer) != NULL) {
986 /* the message is going to be pinned to the peer */
988 cfs_time_shift(*ksocknal_tunables.ksnd_timeout);
990 /* Queue the message until a connection is established */
991 list_add_tail (&tx->tx_list, &peer->ksnp_tx_queue);
992 write_unlock_bh (g_lock);
996 write_unlock_bh (g_lock);
998 /* NB Routes may be ignored if connections to them failed recently */
999 CDEBUG(D_NETERROR, "No usable routes to %s\n", libcfs_id2str(id));
1000 return (-EHOSTUNREACH);
1004 ksocknal_send(lnet_ni_t *ni, void *private, lnet_msg_t *lntmsg)
1006 int type = lntmsg->msg_type;
1007 lnet_process_id_t target = lntmsg->msg_target;
1008 unsigned int payload_niov = lntmsg->msg_niov;
1009 struct iovec *payload_iov = lntmsg->msg_iov;
1010 lnet_kiov_t *payload_kiov = lntmsg->msg_kiov;
1011 unsigned int payload_offset = lntmsg->msg_offset;
1012 unsigned int payload_nob = lntmsg->msg_len;
1017 /* NB 'private' is different depending on what we're sending.
1018 * Just ignore it... */
1020 CDEBUG(D_NET, "sending %u bytes in %d frags to %s\n",
1021 payload_nob, payload_niov, libcfs_id2str(target));
1023 LASSERT (payload_nob == 0 || payload_niov > 0);
1024 LASSERT (payload_niov <= LNET_MAX_IOV);
1025 /* payload is either all vaddrs or all pages */
1026 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1027 LASSERT (!in_interrupt ());
1029 if (payload_iov != NULL)
1030 desc_size = offsetof(ksock_tx_t,
1031 tx_frags.virt.iov[1 + payload_niov]);
1033 desc_size = offsetof(ksock_tx_t,
1034 tx_frags.paged.kiov[payload_niov]);
1036 tx = ksocknal_alloc_tx(desc_size);
1038 CERROR("Can't allocate tx desc type %d size %d\n",
1043 tx->tx_conn = NULL; /* set when assigned a conn */
1044 tx->tx_lnetmsg = lntmsg;
1046 if (payload_iov != NULL) {
1049 tx->tx_iov = tx->tx_frags.virt.iov;
1051 lnet_extract_iov(payload_niov, &tx->tx_iov[1],
1052 payload_niov, payload_iov,
1053 payload_offset, payload_nob);
1056 tx->tx_iov = &tx->tx_frags.paged.iov;
1057 tx->tx_kiov = tx->tx_frags.paged.kiov;
1058 tx->tx_nkiov = lnet_extract_kiov(payload_niov, tx->tx_kiov,
1059 payload_niov, payload_kiov,
1060 payload_offset, payload_nob);
1063 ksocknal_init_msg(&tx->tx_msg, KSOCK_MSG_LNET);
1065 /* The first fragment will be set later in pro_pack */
1066 rc = ksocknal_launch_packet(ni, tx, target);
1070 ksocknal_free_tx(tx);
1075 ksocknal_thread_start (int (*fn)(void *arg), void *arg)
1077 long pid = cfs_kernel_thread (fn, arg, 0);
1082 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1083 ksocknal_data.ksnd_nthreads++;
1084 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1089 ksocknal_thread_fini (void)
1091 write_lock_bh (&ksocknal_data.ksnd_global_lock);
1092 ksocknal_data.ksnd_nthreads--;
1093 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
1097 ksocknal_new_packet (ksock_conn_t *conn, int nob_to_skip)
1099 static char ksocknal_slop_buffer[4096];
1105 LASSERT(conn->ksnc_proto != NULL);
1107 if ((*ksocknal_tunables.ksnd_eager_ack & conn->ksnc_type) != 0) {
1108 /* Remind the socket to ack eagerly... */
1109 ksocknal_lib_eager_ack(conn);
1112 if (nob_to_skip == 0) { /* right at next packet boundary now */
1113 conn->ksnc_rx_started = 0;
1114 mb (); /* racing with timeout thread */
1116 switch (conn->ksnc_proto->pro_version) {
1117 case KSOCK_PROTO_V2:
1118 conn->ksnc_rx_state = SOCKNAL_RX_KSM_HEADER;
1119 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1120 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg;
1122 conn->ksnc_rx_nob_wanted = offsetof(ksock_msg_t, ksm_u);
1123 conn->ksnc_rx_nob_left = offsetof(ksock_msg_t, ksm_u);
1124 conn->ksnc_rx_iov[0].iov_len = offsetof(ksock_msg_t, ksm_u);
1127 case KSOCK_PROTO_V1:
1128 /* Receiving bare lnet_hdr_t */
1129 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1130 conn->ksnc_rx_nob_wanted = sizeof(lnet_hdr_t);
1131 conn->ksnc_rx_nob_left = sizeof(lnet_hdr_t);
1133 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1134 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1135 conn->ksnc_rx_iov[0].iov_len = sizeof (lnet_hdr_t);
1141 conn->ksnc_rx_niov = 1;
1143 conn->ksnc_rx_kiov = NULL;
1144 conn->ksnc_rx_nkiov = 0;
1145 conn->ksnc_rx_csum = ~0;
1149 /* Set up to skip as much as possible now. If there's more left
1150 * (ran out of iov entries) we'll get called again */
1152 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1153 conn->ksnc_rx_nob_left = nob_to_skip;
1154 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1159 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1161 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1162 conn->ksnc_rx_iov[niov].iov_len = nob;
1167 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1168 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof (struct iovec));
1170 conn->ksnc_rx_niov = niov;
1171 conn->ksnc_rx_kiov = NULL;
1172 conn->ksnc_rx_nkiov = 0;
1173 conn->ksnc_rx_nob_wanted = skipped;
1177 /* (Sink) handle incoming ZC request from sender */
1179 ksocknal_handle_zc_req(ksock_peer_t *peer, __u64 cookie)
1183 ksock_sched_t *sched;
1186 read_lock (&ksocknal_data.ksnd_global_lock);
1188 conn = ksocknal_find_conn_locked (0, peer);
1190 read_unlock (&ksocknal_data.ksnd_global_lock);
1191 CERROR("Can't find connection to send zcack.\n");
1195 sched = conn->ksnc_scheduler;
1197 spin_lock_bh (&sched->kss_lock);
1198 rc = ksocknal_piggyback_zcack(conn, cookie);
1199 spin_unlock_bh (&sched->kss_lock);
1201 read_unlock (&ksocknal_data.ksnd_global_lock);
1203 /* Ack cookie is piggybacked */
1207 tx = ksocknal_alloc_tx(KSOCK_NOOP_TX_SIZE);
1209 CERROR("Can't allocate noop tx desc\n");
1214 tx->tx_lnetmsg = NULL;
1217 tx->tx_iov = tx->tx_frags.virt.iov;
1220 ksocknal_init_msg(&tx->tx_msg, KSOCK_MSG_NOOP);
1221 tx->tx_msg.ksm_zc_ack_cookie = cookie; /* incoming cookie */
1223 read_lock (&ksocknal_data.ksnd_global_lock);
1225 conn = ksocknal_find_conn_locked (0, peer);
1227 read_unlock (&ksocknal_data.ksnd_global_lock);
1228 ksocknal_free_tx(tx);
1229 CERROR("Can't find connection to send zcack.\n");
1232 ksocknal_queue_tx_locked(tx, conn);
1234 read_unlock (&ksocknal_data.ksnd_global_lock);
1239 /* (Sender) handle ZC_ACK from sink */
1241 ksocknal_handle_zc_ack(ksock_peer_t *peer, __u64 cookie)
1244 struct list_head *ctmp;
1246 spin_lock(&peer->ksnp_lock);
1248 list_for_each(ctmp, &peer->ksnp_zc_req_list) {
1249 tx = list_entry (ctmp, ksock_tx_t, tx_zc_list);
1250 if (tx->tx_msg.ksm_zc_req_cookie != cookie)
1253 tx->tx_msg.ksm_zc_req_cookie = 0;
1254 list_del(&tx->tx_zc_list);
1256 spin_unlock(&peer->ksnp_lock);
1258 ksocknal_tx_decref(tx);
1261 spin_unlock(&peer->ksnp_lock);
1267 ksocknal_process_receive (ksock_conn_t *conn)
1271 LASSERT (atomic_read(&conn->ksnc_conn_refcount) > 0);
1273 /* NB: sched lock NOT held */
1274 /* SOCKNAL_RX_LNET_HEADER is here for backward compatability */
1275 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_KSM_HEADER ||
1276 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD ||
1277 conn->ksnc_rx_state == SOCKNAL_RX_LNET_HEADER ||
1278 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1280 if (conn->ksnc_rx_nob_wanted != 0) {
1281 rc = ksocknal_receive(conn);
1284 LASSERT (rc != -EAGAIN);
1287 CDEBUG (D_NET, "[%p] EOF from %s"
1288 " ip %d.%d.%d.%d:%d\n", conn,
1289 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1290 HIPQUAD(conn->ksnc_ipaddr),
1292 else if (!conn->ksnc_closing)
1293 CERROR ("[%p] Error %d on read from %s"
1294 " ip %d.%d.%d.%d:%d\n",
1296 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1297 HIPQUAD(conn->ksnc_ipaddr),
1300 /* it's not an error if conn is being closed */
1301 ksocknal_close_conn_and_siblings (conn,
1302 (conn->ksnc_closing) ? 0 : rc);
1303 return (rc == 0 ? -ESHUTDOWN : rc);
1306 if (conn->ksnc_rx_nob_wanted != 0) {
1311 switch (conn->ksnc_rx_state) {
1312 case SOCKNAL_RX_KSM_HEADER:
1313 if (conn->ksnc_flip) {
1314 __swab32s(&conn->ksnc_msg.ksm_type);
1315 __swab32s(&conn->ksnc_msg.ksm_csum);
1316 __swab64s(&conn->ksnc_msg.ksm_zc_req_cookie);
1317 __swab64s(&conn->ksnc_msg.ksm_zc_ack_cookie);
1320 if (conn->ksnc_msg.ksm_type != KSOCK_MSG_NOOP &&
1321 conn->ksnc_msg.ksm_type != KSOCK_MSG_LNET) {
1322 CERROR("%s: Unknown message type: %x\n",
1323 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1324 conn->ksnc_msg.ksm_type);
1325 ksocknal_new_packet(conn, 0);
1326 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1330 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP &&
1331 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1332 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1333 /* NOOP Checksum error */
1334 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1335 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1336 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1337 ksocknal_new_packet(conn, 0);
1338 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1342 if (conn->ksnc_msg.ksm_zc_ack_cookie != 0) {
1343 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v2x);
1345 rc = ksocknal_handle_zc_ack(conn->ksnc_peer,
1346 conn->ksnc_msg.ksm_zc_ack_cookie);
1348 CERROR("%s: Unknown zero copy ACK cookie: "LPU64"\n",
1349 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1350 conn->ksnc_msg.ksm_zc_ack_cookie);
1351 ksocknal_new_packet(conn, 0);
1352 ksocknal_close_conn_and_siblings(conn, -EPROTO);
1357 if (conn->ksnc_msg.ksm_type == KSOCK_MSG_NOOP) {
1358 ksocknal_new_packet (conn, 0);
1359 return 0; /* NOOP is done and just return */
1362 conn->ksnc_rx_state = SOCKNAL_RX_LNET_HEADER;
1363 conn->ksnc_rx_nob_wanted = sizeof(ksock_lnet_msg_t);
1364 conn->ksnc_rx_nob_left = sizeof(ksock_lnet_msg_t);
1366 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1367 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_msg.ksm_u.lnetmsg;
1368 conn->ksnc_rx_iov[0].iov_len = sizeof(ksock_lnet_msg_t);
1370 conn->ksnc_rx_niov = 1;
1371 conn->ksnc_rx_kiov = NULL;
1372 conn->ksnc_rx_nkiov = 0;
1374 goto again; /* read lnet header now */
1376 case SOCKNAL_RX_LNET_HEADER:
1377 /* unpack message header */
1378 conn->ksnc_proto->pro_unpack(&conn->ksnc_msg);
1380 if ((conn->ksnc_peer->ksnp_id.pid & LNET_PID_USERFLAG) != 0) {
1381 /* Userspace peer */
1382 lnet_process_id_t *id = &conn->ksnc_peer->ksnp_id;
1383 lnet_hdr_t *lhdr = &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr;
1385 /* Substitute process ID assigned at connection time */
1386 lhdr->src_pid = cpu_to_le32(id->pid);
1387 lhdr->src_nid = cpu_to_le64(id->nid);
1390 conn->ksnc_rx_state = SOCKNAL_RX_PARSE;
1391 ksocknal_conn_addref(conn); /* ++ref while parsing */
1393 rc = lnet_parse(conn->ksnc_peer->ksnp_ni,
1394 &conn->ksnc_msg.ksm_u.lnetmsg.ksnm_hdr,
1395 conn->ksnc_peer->ksnp_id.nid, conn, 0);
1397 /* I just received garbage: give up on this conn */
1398 ksocknal_new_packet(conn, 0);
1399 ksocknal_close_conn_and_siblings (conn, rc);
1400 ksocknal_conn_decref(conn);
1404 /* I'm racing with ksocknal_recv() */
1405 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_PARSE ||
1406 conn->ksnc_rx_state == SOCKNAL_RX_LNET_PAYLOAD);
1408 if (conn->ksnc_rx_state != SOCKNAL_RX_LNET_PAYLOAD)
1411 /* ksocknal_recv() got called */
1414 case SOCKNAL_RX_LNET_PAYLOAD:
1415 /* payload all received */
1418 if (conn->ksnc_rx_nob_left == 0 && /* not truncating */
1419 conn->ksnc_msg.ksm_csum != 0 && /* has checksum */
1420 conn->ksnc_msg.ksm_csum != conn->ksnc_rx_csum) {
1421 CERROR("%s: Checksum error, wire:0x%08X data:0x%08X\n",
1422 libcfs_id2str(conn->ksnc_peer->ksnp_id),
1423 conn->ksnc_msg.ksm_csum, conn->ksnc_rx_csum);
1427 lnet_finalize(conn->ksnc_peer->ksnp_ni, conn->ksnc_cookie, rc);
1429 if (rc == 0 && conn->ksnc_msg.ksm_zc_req_cookie != 0) {
1430 LASSERT(conn->ksnc_proto == &ksocknal_protocol_v2x);
1431 rc = ksocknal_handle_zc_req(conn->ksnc_peer,
1432 conn->ksnc_msg.ksm_zc_req_cookie);
1436 ksocknal_new_packet(conn, 0);
1437 ksocknal_close_conn_and_siblings (conn, rc);
1442 case SOCKNAL_RX_SLOP:
1443 /* starting new packet? */
1444 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1445 return 0; /* come back later */
1446 goto again; /* try to finish reading slop now */
1454 return (-EINVAL); /* keep gcc happy */
1458 ksocknal_recv (lnet_ni_t *ni, void *private, lnet_msg_t *msg, int delayed,
1459 unsigned int niov, struct iovec *iov, lnet_kiov_t *kiov,
1460 unsigned int offset, unsigned int mlen, unsigned int rlen)
1462 ksock_conn_t *conn = (ksock_conn_t *)private;
1463 ksock_sched_t *sched = conn->ksnc_scheduler;
1465 LASSERT (mlen <= rlen);
1466 LASSERT (niov <= LNET_MAX_IOV);
1468 conn->ksnc_cookie = msg;
1469 conn->ksnc_rx_nob_wanted = mlen;
1470 conn->ksnc_rx_nob_left = rlen;
1472 if (mlen == 0 || iov != NULL) {
1473 conn->ksnc_rx_nkiov = 0;
1474 conn->ksnc_rx_kiov = NULL;
1475 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1476 conn->ksnc_rx_niov =
1477 lnet_extract_iov(LNET_MAX_IOV, conn->ksnc_rx_iov,
1478 niov, iov, offset, mlen);
1480 conn->ksnc_rx_niov = 0;
1481 conn->ksnc_rx_iov = NULL;
1482 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1483 conn->ksnc_rx_nkiov =
1484 lnet_extract_kiov(LNET_MAX_IOV, conn->ksnc_rx_kiov,
1485 niov, kiov, offset, mlen);
1489 lnet_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1490 lnet_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1492 LASSERT (conn->ksnc_rx_scheduled);
1494 spin_lock_bh (&sched->kss_lock);
1496 switch (conn->ksnc_rx_state) {
1497 case SOCKNAL_RX_PARSE_WAIT:
1498 list_add_tail(&conn->ksnc_rx_list, &sched->kss_rx_conns);
1499 cfs_waitq_signal (&sched->kss_waitq);
1500 LASSERT (conn->ksnc_rx_ready);
1503 case SOCKNAL_RX_PARSE:
1504 /* scheduler hasn't noticed I'm parsing yet */
1508 conn->ksnc_rx_state = SOCKNAL_RX_LNET_PAYLOAD;
1510 spin_unlock_bh (&sched->kss_lock);
1511 ksocknal_conn_decref(conn);
1516 ksocknal_sched_cansleep(ksock_sched_t *sched)
1520 spin_lock_bh (&sched->kss_lock);
1522 rc = (!ksocknal_data.ksnd_shuttingdown &&
1523 list_empty(&sched->kss_rx_conns) &&
1524 list_empty(&sched->kss_tx_conns));
1526 spin_unlock_bh (&sched->kss_lock);
1530 int ksocknal_scheduler (void *arg)
1532 ksock_sched_t *sched = (ksock_sched_t *)arg;
1537 int id = (int)(sched - ksocknal_data.ksnd_schedulers);
1540 snprintf (name, sizeof (name),"socknal_sd%02d", id);
1541 cfs_daemonize (name);
1542 cfs_block_allsigs ();
1544 #if defined(CONFIG_SMP) && defined(CPU_AFFINITY)
1545 id = ksocknal_sched2cpu(id);
1546 if (cpu_online(id)) {
1547 cpumask_t m = CPU_MASK_NONE;
1549 set_cpus_allowed(current, m);
1551 CERROR ("Can't set CPU affinity for %s to %d\n", name, id);
1553 #endif /* CONFIG_SMP && CPU_AFFINITY */
1555 spin_lock_bh (&sched->kss_lock);
1557 while (!ksocknal_data.ksnd_shuttingdown) {
1558 int did_something = 0;
1560 /* Ensure I progress everything semi-fairly */
1562 if (!list_empty (&sched->kss_rx_conns)) {
1563 conn = list_entry(sched->kss_rx_conns.next,
1564 ksock_conn_t, ksnc_rx_list);
1565 list_del(&conn->ksnc_rx_list);
1567 LASSERT(conn->ksnc_rx_scheduled);
1568 LASSERT(conn->ksnc_rx_ready);
1570 /* clear rx_ready in case receive isn't complete.
1571 * Do it BEFORE we call process_recv, since
1572 * data_ready can set it any time after we release
1574 conn->ksnc_rx_ready = 0;
1575 spin_unlock_bh (&sched->kss_lock);
1577 rc = ksocknal_process_receive(conn);
1579 spin_lock_bh (&sched->kss_lock);
1581 /* I'm the only one that can clear this flag */
1582 LASSERT(conn->ksnc_rx_scheduled);
1584 /* Did process_receive get everything it wanted? */
1586 conn->ksnc_rx_ready = 1;
1588 if (conn->ksnc_rx_state == SOCKNAL_RX_PARSE) {
1589 /* Conn blocked waiting for ksocknal_recv()
1590 * I change its state (under lock) to signal
1591 * it can be rescheduled */
1592 conn->ksnc_rx_state = SOCKNAL_RX_PARSE_WAIT;
1593 } else if (conn->ksnc_rx_ready) {
1594 /* reschedule for rx */
1595 list_add_tail (&conn->ksnc_rx_list,
1596 &sched->kss_rx_conns);
1598 conn->ksnc_rx_scheduled = 0;
1600 ksocknal_conn_decref(conn);
1606 if (!list_empty (&sched->kss_tx_conns)) {
1607 CFS_LIST_HEAD (zlist);
1609 if (!list_empty(&sched->kss_zombie_noop_txs)) {
1610 list_add(&zlist, &sched->kss_zombie_noop_txs);
1611 list_del_init(&sched->kss_zombie_noop_txs);
1614 conn = list_entry(sched->kss_tx_conns.next,
1615 ksock_conn_t, ksnc_tx_list);
1616 list_del (&conn->ksnc_tx_list);
1618 LASSERT(conn->ksnc_tx_scheduled);
1619 LASSERT(conn->ksnc_tx_ready);
1620 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1622 tx = list_entry(conn->ksnc_tx_queue.next,
1623 ksock_tx_t, tx_list);
1625 if (conn->ksnc_tx_mono == tx)
1626 ksocknal_next_mono_tx(conn);
1628 /* dequeue now so empty list => more to send */
1629 list_del(&tx->tx_list);
1631 /* Clear tx_ready in case send isn't complete. Do
1632 * it BEFORE we call process_transmit, since
1633 * write_space can set it any time after we release
1635 conn->ksnc_tx_ready = 0;
1636 spin_unlock_bh (&sched->kss_lock);
1638 if (!list_empty(&zlist)) {
1639 /* free zombie noop txs, it's fast because
1640 * noop txs are just put in freelist */
1641 ksocknal_txlist_done(NULL, &zlist, 0);
1644 rc = ksocknal_process_transmit(conn, tx);
1646 if (rc == -ENOMEM || rc == -EAGAIN) {
1647 /* Incomplete send: replace tx on HEAD of tx_queue */
1648 spin_lock_bh (&sched->kss_lock);
1649 list_add (&tx->tx_list, &conn->ksnc_tx_queue);
1651 /* Complete send; tx -ref */
1652 ksocknal_tx_decref (tx);
1654 spin_lock_bh (&sched->kss_lock);
1655 /* assume space for more */
1656 conn->ksnc_tx_ready = 1;
1659 if (rc == -ENOMEM) {
1660 /* Do nothing; after a short timeout, this
1661 * conn will be reposted on kss_tx_conns. */
1662 } else if (conn->ksnc_tx_ready &&
1663 !list_empty (&conn->ksnc_tx_queue)) {
1664 /* reschedule for tx */
1665 list_add_tail (&conn->ksnc_tx_list,
1666 &sched->kss_tx_conns);
1668 conn->ksnc_tx_scheduled = 0;
1670 ksocknal_conn_decref(conn);
1675 if (!did_something || /* nothing to do */
1676 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1677 spin_unlock_bh (&sched->kss_lock);
1681 if (!did_something) { /* wait for something to do */
1682 cfs_wait_event_interruptible_exclusive(
1684 !ksocknal_sched_cansleep(sched), rc);
1690 spin_lock_bh (&sched->kss_lock);
1694 spin_unlock_bh (&sched->kss_lock);
1695 ksocknal_thread_fini ();
1700 * Add connection to kss_rx_conns of scheduler
1701 * and wakeup the scheduler.
1703 void ksocknal_read_callback (ksock_conn_t *conn)
1705 ksock_sched_t *sched;
1708 sched = conn->ksnc_scheduler;
1710 spin_lock_bh (&sched->kss_lock);
1712 conn->ksnc_rx_ready = 1;
1714 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1715 list_add_tail(&conn->ksnc_rx_list,
1716 &sched->kss_rx_conns);
1717 conn->ksnc_rx_scheduled = 1;
1718 /* extra ref for scheduler */
1719 ksocknal_conn_addref(conn);
1721 cfs_waitq_signal (&sched->kss_waitq);
1723 spin_unlock_bh (&sched->kss_lock);
1729 * Add connection to kss_tx_conns of scheduler
1730 * and wakeup the scheduler.
1732 void ksocknal_write_callback (ksock_conn_t *conn)
1734 ksock_sched_t *sched;
1737 sched = conn->ksnc_scheduler;
1739 spin_lock_bh (&sched->kss_lock);
1741 conn->ksnc_tx_ready = 1;
1743 if (!conn->ksnc_tx_scheduled && // not being progressed
1744 !list_empty(&conn->ksnc_tx_queue)){//packets to send
1745 list_add_tail (&conn->ksnc_tx_list,
1746 &sched->kss_tx_conns);
1747 conn->ksnc_tx_scheduled = 1;
1748 /* extra ref for scheduler */
1749 ksocknal_conn_addref(conn);
1751 cfs_waitq_signal (&sched->kss_waitq);
1754 spin_unlock_bh (&sched->kss_lock);
1760 ksocknal_parse_proto_version (ksock_hello_msg_t *hello)
1762 if ((hello->kshm_magic == LNET_PROTO_MAGIC &&
1763 hello->kshm_version == KSOCK_PROTO_V2) ||
1764 (hello->kshm_magic == __swab32(LNET_PROTO_MAGIC) &&
1765 hello->kshm_version == __swab32(KSOCK_PROTO_V2))) {
1766 #if SOCKNAL_VERSION_DEBUG
1767 if (*ksocknal_tunables.ksnd_protocol != 2)
1770 return &ksocknal_protocol_v2x;
1773 if (hello->kshm_magic == le32_to_cpu(LNET_PROTO_TCP_MAGIC)) {
1774 lnet_magicversion_t *hmv = (lnet_magicversion_t *)hello;
1776 CLASSERT (sizeof (lnet_magicversion_t) ==
1777 offsetof (ksock_hello_msg_t, kshm_src_nid));
1779 if (hmv->version_major == cpu_to_le16 (KSOCK_PROTO_V1_MAJOR) &&
1780 hmv->version_minor == cpu_to_le16 (KSOCK_PROTO_V1_MINOR))
1781 return &ksocknal_protocol_v1x;
1788 ksocknal_send_hello_v1 (ksock_conn_t *conn, ksock_hello_msg_t *hello)
1790 cfs_socket_t *sock = conn->ksnc_sock;
1792 lnet_magicversion_t *hmv;
1796 CLASSERT(sizeof(lnet_magicversion_t) == offsetof(lnet_hdr_t, src_nid));
1798 LIBCFS_ALLOC(hdr, sizeof(*hdr));
1800 CERROR("Can't allocate lnet_hdr_t\n");
1804 hmv = (lnet_magicversion_t *)&hdr->dest_nid;
1806 /* Re-organize V2.x message header to V1.x (lnet_hdr_t)
1807 * header and send out */
1808 hmv->magic = cpu_to_le32 (LNET_PROTO_TCP_MAGIC);
1809 hmv->version_major = cpu_to_le16 (KSOCK_PROTO_V1_MAJOR);
1810 hmv->version_minor = cpu_to_le16 (KSOCK_PROTO_V1_MINOR);
1812 if (the_lnet.ln_testprotocompat != 0) {
1813 /* single-shot proto check */
1815 if ((the_lnet.ln_testprotocompat & 1) != 0) {
1816 hmv->version_major++; /* just different! */
1817 the_lnet.ln_testprotocompat &= ~1;
1819 if ((the_lnet.ln_testprotocompat & 2) != 0) {
1820 hmv->magic = LNET_PROTO_MAGIC;
1821 the_lnet.ln_testprotocompat &= ~2;
1826 hdr->src_nid = cpu_to_le64 (hello->kshm_src_nid);
1827 hdr->src_pid = cpu_to_le32 (hello->kshm_src_pid);
1828 hdr->type = cpu_to_le32 (LNET_MSG_HELLO);
1829 hdr->payload_length = cpu_to_le32 (hello->kshm_nips * sizeof(__u32));
1830 hdr->msg.hello.type = cpu_to_le32 (hello->kshm_ctype);
1831 hdr->msg.hello.incarnation = cpu_to_le64 (hello->kshm_src_incarnation);
1833 rc = libcfs_sock_write(sock, hdr, sizeof(*hdr), lnet_acceptor_timeout());
1836 CDEBUG (D_NETERROR, "Error %d sending HELLO hdr to %u.%u.%u.%u/%d\n",
1837 rc, HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1841 if (hello->kshm_nips == 0)
1844 for (i = 0; i < (int) hello->kshm_nips; i++) {
1845 hello->kshm_ips[i] = __cpu_to_le32 (hello->kshm_ips[i]);
1848 rc = libcfs_sock_write(sock, hello->kshm_ips,
1849 hello->kshm_nips * sizeof(__u32),
1850 lnet_acceptor_timeout());
1852 CDEBUG (D_NETERROR, "Error %d sending HELLO payload (%d)"
1853 " to %u.%u.%u.%u/%d\n", rc, hello->kshm_nips,
1854 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1857 LIBCFS_FREE(hdr, sizeof(*hdr));
1863 ksocknal_send_hello_v2 (ksock_conn_t *conn, ksock_hello_msg_t *hello)
1865 cfs_socket_t *sock = conn->ksnc_sock;
1868 hello->kshm_magic = LNET_PROTO_MAGIC;
1869 hello->kshm_version = KSOCK_PROTO_V2;
1871 if (the_lnet.ln_testprotocompat != 0) {
1872 /* single-shot proto check */
1874 if ((the_lnet.ln_testprotocompat & 1) != 0) {
1875 hello->kshm_version++; /* just different! */
1876 the_lnet.ln_testprotocompat &= ~1;
1881 rc = libcfs_sock_write(sock, hello, offsetof(ksock_hello_msg_t, kshm_ips),
1882 lnet_acceptor_timeout());
1885 CDEBUG (D_NETERROR, "Error %d sending HELLO hdr to %u.%u.%u.%u/%d\n",
1886 rc, HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1890 if (hello->kshm_nips == 0)
1893 rc = libcfs_sock_write(sock, hello->kshm_ips,
1894 hello->kshm_nips * sizeof(__u32),
1895 lnet_acceptor_timeout());
1897 CDEBUG (D_NETERROR, "Error %d sending HELLO payload (%d)"
1898 " to %u.%u.%u.%u/%d\n", rc, hello->kshm_nips,
1899 HIPQUAD(conn->ksnc_ipaddr), conn->ksnc_port);
1906 ksocknal_recv_hello_v1(ksock_conn_t *conn, ksock_hello_msg_t *hello,int timeout)
1908 cfs_socket_t *sock = conn->ksnc_sock;
1913 LIBCFS_ALLOC(hdr, sizeof(*hdr));
1915 CERROR("Can't allocate lnet_hdr_t\n");
1919 rc = libcfs_sock_read(sock, &hdr->src_nid,
1920 sizeof (*hdr) - offsetof (lnet_hdr_t, src_nid),
1923 CERROR ("Error %d reading rest of HELLO hdr from %u.%u.%u.%u\n",
1924 rc, HIPQUAD(conn->ksnc_ipaddr));
1925 LASSERT (rc < 0 && rc != -EALREADY);
1929 /* ...and check we got what we expected */
1930 if (hdr->type != cpu_to_le32 (LNET_MSG_HELLO)) {
1931 CERROR ("Expecting a HELLO hdr,"
1932 " but got type %d from %u.%u.%u.%u\n",
1933 le32_to_cpu (hdr->type),
1934 HIPQUAD(conn->ksnc_ipaddr));
1939 hello->kshm_src_nid = le64_to_cpu (hdr->src_nid);
1940 hello->kshm_src_pid = le32_to_cpu (hdr->src_pid);
1941 hello->kshm_src_incarnation = le64_to_cpu (hdr->msg.hello.incarnation);
1942 hello->kshm_ctype = le32_to_cpu (hdr->msg.hello.type);
1943 hello->kshm_nips = le32_to_cpu (hdr->payload_length) /
1946 if (hello->kshm_nips > LNET_MAX_INTERFACES) {
1947 CERROR("Bad nips %d from ip %u.%u.%u.%u\n",
1948 hello->kshm_nips, HIPQUAD(conn->ksnc_ipaddr));
1953 if (hello->kshm_nips == 0)
1956 rc = libcfs_sock_read(sock, hello->kshm_ips,
1957 hello->kshm_nips * sizeof(__u32), timeout);
1959 CERROR ("Error %d reading IPs from ip %u.%u.%u.%u\n",
1960 rc, HIPQUAD(conn->ksnc_ipaddr));
1961 LASSERT (rc < 0 && rc != -EALREADY);
1965 for (i = 0; i < (int) hello->kshm_nips; i++) {
1966 hello->kshm_ips[i] = __le32_to_cpu(hello->kshm_ips[i]);
1968 if (hello->kshm_ips[i] == 0) {
1969 CERROR("Zero IP[%d] from ip %u.%u.%u.%u\n",
1970 i, HIPQUAD(conn->ksnc_ipaddr));
1976 LIBCFS_FREE(hdr, sizeof(*hdr));
1982 ksocknal_recv_hello_v2 (ksock_conn_t *conn, ksock_hello_msg_t *hello, int timeout)
1984 cfs_socket_t *sock = conn->ksnc_sock;
1988 if (hello->kshm_magic == LNET_PROTO_MAGIC)
1989 conn->ksnc_flip = 0;
1991 conn->ksnc_flip = 1;
1993 rc = libcfs_sock_read(sock, &hello->kshm_src_nid,
1994 offsetof(ksock_hello_msg_t, kshm_ips) -
1995 offsetof(ksock_hello_msg_t, kshm_src_nid),
1998 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
1999 rc, HIPQUAD(conn->ksnc_ipaddr));
2000 LASSERT (rc < 0 && rc != -EALREADY);
2004 if (conn->ksnc_flip) {
2005 __swab32s(&hello->kshm_src_pid);
2006 __swab64s(&hello->kshm_src_nid);
2007 __swab32s(&hello->kshm_dst_pid);
2008 __swab64s(&hello->kshm_dst_nid);
2009 __swab64s(&hello->kshm_src_incarnation);
2010 __swab64s(&hello->kshm_dst_incarnation);
2011 __swab32s(&hello->kshm_ctype);
2012 __swab32s(&hello->kshm_nips);
2015 if (hello->kshm_nips > LNET_MAX_INTERFACES) {
2016 CERROR("Bad nips %d from ip %u.%u.%u.%u\n",
2017 hello->kshm_nips, HIPQUAD(conn->ksnc_ipaddr));
2021 if (hello->kshm_nips == 0)
2024 rc = libcfs_sock_read(sock, hello->kshm_ips,
2025 hello->kshm_nips * sizeof(__u32), timeout);
2027 CERROR ("Error %d reading IPs from ip %u.%u.%u.%u\n",
2028 rc, HIPQUAD(conn->ksnc_ipaddr));
2029 LASSERT (rc < 0 && rc != -EALREADY);
2033 for (i = 0; i < (int) hello->kshm_nips; i++) {
2034 if (conn->ksnc_flip)
2035 __swab32s(&hello->kshm_ips[i]);
2037 if (hello->kshm_ips[i] == 0) {
2038 CERROR("Zero IP[%d] from ip %u.%u.%u.%u\n",
2039 i, HIPQUAD(conn->ksnc_ipaddr));
2048 ksocknal_pack_msg_v1(ksock_tx_t *tx)
2050 /* V1.x has no KSOCK_MSG_NOOP */
2051 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
2052 LASSERT(tx->tx_lnetmsg != NULL);
2054 tx->tx_iov[0].iov_base = (void *)&tx->tx_lnetmsg->msg_hdr;
2055 tx->tx_iov[0].iov_len = sizeof(lnet_hdr_t);
2057 tx->tx_resid = tx->tx_nob = tx->tx_lnetmsg->msg_len + sizeof(lnet_hdr_t);
2061 ksocknal_pack_msg_v2(ksock_tx_t *tx)
2063 tx->tx_iov[0].iov_base = (void *)&tx->tx_msg;
2065 if (tx->tx_lnetmsg != NULL) {
2066 LASSERT(tx->tx_msg.ksm_type != KSOCK_MSG_NOOP);
2068 tx->tx_msg.ksm_u.lnetmsg.ksnm_hdr = tx->tx_lnetmsg->msg_hdr;
2069 tx->tx_iov[0].iov_len = sizeof(ksock_msg_t);
2070 tx->tx_resid = tx->tx_nob = sizeof(ksock_msg_t) + tx->tx_lnetmsg->msg_len;
2072 LASSERT(tx->tx_msg.ksm_type == KSOCK_MSG_NOOP);
2074 tx->tx_iov[0].iov_len = offsetof(ksock_msg_t, ksm_u.lnetmsg.ksnm_hdr);
2075 tx->tx_resid = tx->tx_nob = offsetof(ksock_msg_t, ksm_u.lnetmsg.ksnm_hdr);
2077 /* Don't checksum before start sending, because packet can be piggybacked with ACK */
2081 ksocknal_unpack_msg_v1(ksock_msg_t *msg)
2083 msg->ksm_type = KSOCK_MSG_LNET;
2085 msg->ksm_zc_req_cookie = 0;
2086 msg->ksm_zc_ack_cookie = 0;
2090 ksocknal_unpack_msg_v2(ksock_msg_t *msg)
2092 return; /* Do nothing */
2095 ksock_proto_t ksocknal_protocol_v1x =
2098 ksocknal_send_hello_v1,
2099 ksocknal_recv_hello_v1,
2100 ksocknal_pack_msg_v1,
2101 ksocknal_unpack_msg_v1
2104 ksock_proto_t ksocknal_protocol_v2x =
2107 ksocknal_send_hello_v2,
2108 ksocknal_recv_hello_v2,
2109 ksocknal_pack_msg_v2,
2110 ksocknal_unpack_msg_v2
2114 ksocknal_send_hello (lnet_ni_t *ni, ksock_conn_t *conn,
2115 lnet_nid_t peer_nid, ksock_hello_msg_t *hello)
2117 /* CAVEAT EMPTOR: this byte flips 'ipaddrs' */
2118 ksock_net_t *net = (ksock_net_t *)ni->ni_data;
2120 LASSERT (hello->kshm_nips <= LNET_MAX_INTERFACES);
2122 /* rely on caller to hold a ref on socket so it wouldn't disappear */
2123 LASSERT (conn->ksnc_proto != NULL);
2125 hello->kshm_src_nid = ni->ni_nid;
2126 hello->kshm_dst_nid = peer_nid;
2127 hello->kshm_src_pid = the_lnet.ln_pid;
2129 hello->kshm_src_incarnation = net->ksnn_incarnation;
2130 hello->kshm_ctype = conn->ksnc_type;
2132 return conn->ksnc_proto->pro_send_hello(conn, hello);
2136 ksocknal_invert_type(int type)
2140 case SOCKLND_CONN_ANY:
2141 case SOCKLND_CONN_CONTROL:
2143 case SOCKLND_CONN_BULK_IN:
2144 return SOCKLND_CONN_BULK_OUT;
2145 case SOCKLND_CONN_BULK_OUT:
2146 return SOCKLND_CONN_BULK_IN;
2148 return (SOCKLND_CONN_NONE);
2153 ksocknal_recv_hello (lnet_ni_t *ni, ksock_conn_t *conn,
2154 ksock_hello_msg_t *hello, lnet_process_id_t *peerid,
2157 /* Return < 0 fatal error
2159 * EALREADY lost connection race
2160 * EPROTO protocol version mismatch
2162 cfs_socket_t *sock = conn->ksnc_sock;
2163 int active = (conn->ksnc_proto != NULL);
2167 ksock_proto_t *proto;
2168 lnet_process_id_t recv_id;
2170 /* socket type set on active connections - not set on passive */
2171 LASSERT (!active == !(conn->ksnc_type != SOCKLND_CONN_NONE));
2173 timeout = active ? *ksocknal_tunables.ksnd_timeout :
2174 lnet_acceptor_timeout();
2176 rc = libcfs_sock_read(sock, &hello->kshm_magic, sizeof (hello->kshm_magic), timeout);
2178 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
2179 rc, HIPQUAD(conn->ksnc_ipaddr));
2184 if (hello->kshm_magic != LNET_PROTO_MAGIC &&
2185 hello->kshm_magic != __swab32(LNET_PROTO_MAGIC) &&
2186 hello->kshm_magic != le32_to_cpu (LNET_PROTO_TCP_MAGIC)) {
2187 /* Unexpected magic! */
2188 CERROR ("Bad magic(1) %#08x (%#08x expected) from "
2189 "%u.%u.%u.%u\n", __cpu_to_le32 (hello->kshm_magic),
2190 LNET_PROTO_TCP_MAGIC,
2191 HIPQUAD(conn->ksnc_ipaddr));
2195 rc = libcfs_sock_read(sock, &hello->kshm_version,
2196 sizeof(hello->kshm_version), timeout);
2198 CERROR ("Error %d reading HELLO from %u.%u.%u.%u\n",
2199 rc, HIPQUAD(conn->ksnc_ipaddr));
2204 proto = ksocknal_parse_proto_version(hello);
2205 if (proto == NULL) {
2207 /* unknown protocol from peer, tell peer my protocol */
2208 conn->ksnc_proto = &ksocknal_protocol_v2x;
2209 #if SOCKNAL_VERSION_DEBUG
2210 if (*ksocknal_tunables.ksnd_protocol != 2)
2211 conn->ksnc_proto = &ksocknal_protocol_v1x;
2213 hello->kshm_nips = 0;
2214 ksocknal_send_hello(ni, conn, ni->ni_nid, hello);
2217 CERROR ("Unknown protocol version (%d.x expected)"
2218 " from %u.%u.%u.%u\n",
2219 conn->ksnc_proto->pro_version,
2220 HIPQUAD(conn->ksnc_ipaddr));
2225 proto_match = (conn->ksnc_proto == proto);
2226 conn->ksnc_proto = proto;
2228 /* receive the rest of hello message anyway */
2229 rc = conn->ksnc_proto->pro_recv_hello(conn, hello, timeout);
2231 CERROR("Error %d reading or checking hello from from %u.%u.%u.%u\n",
2232 rc, HIPQUAD(conn->ksnc_ipaddr));
2237 *incarnation = hello->kshm_src_incarnation;
2239 if (hello->kshm_src_nid == LNET_NID_ANY) {
2240 CERROR("Expecting a HELLO hdr with a NID, but got LNET_NID_ANY"
2241 "from %u.%u.%u.%u\n", HIPQUAD(conn->ksnc_ipaddr));
2246 conn->ksnc_port > LNET_ACCEPTOR_MAX_RESERVED_PORT) {
2247 /* Userspace NAL assigns peer process ID from socket */
2248 recv_id.pid = conn->ksnc_port | LNET_PID_USERFLAG;
2249 recv_id.nid = LNET_MKNID(LNET_NIDNET(ni->ni_nid), conn->ksnc_ipaddr);
2251 recv_id.nid = hello->kshm_src_nid;
2252 recv_id.pid = hello->kshm_src_pid;
2258 /* peer determines type */
2259 conn->ksnc_type = ksocknal_invert_type(hello->kshm_ctype);
2260 if (conn->ksnc_type == SOCKLND_CONN_NONE) {
2261 CERROR ("Unexpected type %d from %s ip %u.%u.%u.%u\n",
2262 hello->kshm_ctype, libcfs_id2str(*peerid),
2263 HIPQUAD(conn->ksnc_ipaddr));
2270 if (peerid->pid != recv_id.pid ||
2271 peerid->nid != recv_id.nid) {
2272 LCONSOLE_ERROR_MSG(0x130, "Connected successfully to %s on host"
2273 " %u.%u.%u.%u, but they claimed they were "
2274 "%s; please check your Lustre "
2276 libcfs_id2str(*peerid),
2277 HIPQUAD(conn->ksnc_ipaddr),
2278 libcfs_id2str(recv_id));
2282 if (hello->kshm_ctype == SOCKLND_CONN_NONE) {
2283 /* Possible protocol mismatch or I lost the connection race */
2284 return proto_match ? EALREADY : EPROTO;
2287 if (ksocknal_invert_type(hello->kshm_ctype) != conn->ksnc_type) {
2288 CERROR ("Mismatched types: me %d, %s ip %u.%u.%u.%u %d\n",
2289 conn->ksnc_type, libcfs_id2str(*peerid),
2290 HIPQUAD(conn->ksnc_ipaddr),
2299 ksocknal_connect (ksock_route_t *route)
2301 CFS_LIST_HEAD (zombies);
2302 ksock_peer_t *peer = route->ksnr_peer;
2306 cfs_time_t deadline;
2307 int retry_later = 0;
2310 deadline = cfs_time_add(cfs_time_current(),
2311 cfs_time_seconds(*ksocknal_tunables.ksnd_timeout));
2313 write_lock_bh (&ksocknal_data.ksnd_global_lock);
2315 LASSERT (route->ksnr_scheduled);
2316 LASSERT (!route->ksnr_connecting);
2318 route->ksnr_connecting = 1;
2321 wanted = ksocknal_route_mask() & ~route->ksnr_connected;
2323 /* stop connecting if peer/route got closed under me, or
2324 * route got connected while queued */
2325 if (peer->ksnp_closing || route->ksnr_deleted ||
2331 /* reschedule if peer is connecting to me */
2332 if (peer->ksnp_accepting > 0) {
2334 "peer %s(%d) already connecting to me, retry later.\n",
2335 libcfs_nid2str(peer->ksnp_id.nid), peer->ksnp_accepting);
2339 if (retry_later) /* needs reschedule */
2342 if ((wanted & (1 << SOCKLND_CONN_ANY)) != 0) {
2343 type = SOCKLND_CONN_ANY;
2344 } else if ((wanted & (1 << SOCKLND_CONN_CONTROL)) != 0) {
2345 type = SOCKLND_CONN_CONTROL;
2346 } else if ((wanted & (1 << SOCKLND_CONN_BULK_IN)) != 0) {
2347 type = SOCKLND_CONN_BULK_IN;
2349 LASSERT ((wanted & (1 << SOCKLND_CONN_BULK_OUT)) != 0);
2350 type = SOCKLND_CONN_BULK_OUT;
2353 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2355 if (cfs_time_aftereq(cfs_time_current(), deadline)) {
2357 lnet_connect_console_error(rc, peer->ksnp_id.nid,
2363 rc = lnet_connect(&sock, peer->ksnp_id.nid,
2364 route->ksnr_myipaddr,
2365 route->ksnr_ipaddr, route->ksnr_port);
2369 rc = ksocknal_create_conn(peer->ksnp_ni, route, sock, type);
2371 lnet_connect_console_error(rc, peer->ksnp_id.nid,
2377 /* A +ve RC means I have to retry because I lost the connection
2378 * race or I have to renegotiate protocol version */
2379 retry_later = (rc != 0);
2381 CDEBUG(D_NET, "peer %s: conn race, retry later.\n",
2382 libcfs_nid2str(peer->ksnp_id.nid));
2384 write_lock_bh (&ksocknal_data.ksnd_global_lock);
2387 route->ksnr_scheduled = 0;
2388 route->ksnr_connecting = 0;
2391 /* re-queue for attention; this frees me up to handle
2392 * the peer's incoming connection request */
2393 ksocknal_launch_connection_locked(route);
2396 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2400 write_lock_bh (&ksocknal_data.ksnd_global_lock);
2402 route->ksnr_scheduled = 0;
2403 route->ksnr_connecting = 0;
2405 /* This is a retry rather than a new connection */
2406 route->ksnr_retry_interval *= 2;
2407 route->ksnr_retry_interval =
2408 MAX(route->ksnr_retry_interval,
2409 cfs_time_seconds(*ksocknal_tunables.ksnd_min_reconnectms)/1000);
2410 route->ksnr_retry_interval =
2411 MIN(route->ksnr_retry_interval,
2412 cfs_time_seconds(*ksocknal_tunables.ksnd_max_reconnectms)/1000);
2414 LASSERT (route->ksnr_retry_interval != 0);
2415 route->ksnr_timeout = cfs_time_add(cfs_time_current(),
2416 route->ksnr_retry_interval);
2418 if (!list_empty(&peer->ksnp_tx_queue) &&
2419 peer->ksnp_accepting == 0 &&
2420 ksocknal_find_connecting_route_locked(peer) == NULL) {
2421 /* ksnp_tx_queue is queued on a conn on successful
2423 LASSERT (list_empty (&peer->ksnp_conns));
2425 /* take all the blocked packets while I've got the lock and
2426 * complete below... */
2427 list_add(&zombies, &peer->ksnp_tx_queue);
2428 list_del_init(&peer->ksnp_tx_queue);
2431 #if 0 /* irrelevent with only eager routes */
2432 if (!route->ksnr_deleted) {
2433 /* make this route least-favourite for re-selection */
2434 list_del(&route->ksnr_list);
2435 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
2438 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2440 ksocknal_peer_failed(peer);
2441 ksocknal_txlist_done(peer->ksnp_ni, &zombies, 1);
2445 ksocknal_connd_connect_route_locked(void)
2447 /* Only handle an outgoing connection request if there is someone left
2448 * to handle incoming connections */
2449 return !list_empty(&ksocknal_data.ksnd_connd_routes) &&
2450 ((ksocknal_data.ksnd_connd_connecting + 1) <
2451 *ksocknal_tunables.ksnd_nconnds);
2455 ksocknal_connd_ready(void)
2459 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2461 rc = ksocknal_data.ksnd_shuttingdown ||
2462 !list_empty(&ksocknal_data.ksnd_connd_connreqs) ||
2463 ksocknal_connd_connect_route_locked();
2465 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2471 ksocknal_connd (void *arg)
2473 long id = (long)(long_ptr_t)arg;
2475 ksock_connreq_t *cr;
2476 ksock_route_t *route;
2479 snprintf (name, sizeof (name), "socknal_cd%02ld", id);
2480 cfs_daemonize (name);
2481 cfs_block_allsigs ();
2483 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2485 while (!ksocknal_data.ksnd_shuttingdown) {
2487 if (!list_empty(&ksocknal_data.ksnd_connd_connreqs)) {
2488 /* Connection accepted by the listener */
2489 cr = list_entry(ksocknal_data.ksnd_connd_connreqs.next,
2490 ksock_connreq_t, ksncr_list);
2492 list_del(&cr->ksncr_list);
2493 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2495 ksocknal_create_conn(cr->ksncr_ni, NULL,
2496 cr->ksncr_sock, SOCKLND_CONN_NONE);
2497 lnet_ni_decref(cr->ksncr_ni);
2498 LIBCFS_FREE(cr, sizeof(*cr));
2500 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2503 if (ksocknal_connd_connect_route_locked()) {
2504 /* Connection request */
2505 route = list_entry (ksocknal_data.ksnd_connd_routes.next,
2506 ksock_route_t, ksnr_connd_list);
2508 list_del (&route->ksnr_connd_list);
2509 ksocknal_data.ksnd_connd_connecting++;
2510 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2512 ksocknal_connect (route);
2513 ksocknal_route_decref(route);
2515 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2516 ksocknal_data.ksnd_connd_connecting--;
2519 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2521 cfs_wait_event_interruptible_exclusive(
2522 ksocknal_data.ksnd_connd_waitq,
2523 ksocknal_connd_ready(), rc);
2525 spin_lock_bh (&ksocknal_data.ksnd_connd_lock);
2528 spin_unlock_bh (&ksocknal_data.ksnd_connd_lock);
2530 ksocknal_thread_fini ();
2535 ksocknal_find_timed_out_conn (ksock_peer_t *peer)
2537 /* We're called with a shared lock on ksnd_global_lock */
2539 struct list_head *ctmp;
2541 list_for_each (ctmp, &peer->ksnp_conns) {
2543 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
2545 /* Don't need the {get,put}connsock dance to deref ksnc_sock */
2546 LASSERT (!conn->ksnc_closing);
2548 /* SOCK_ERROR will reset error code of socket in
2549 * some platform (like Darwin8.x) */
2550 error = SOCK_ERROR(conn->ksnc_sock);
2552 ksocknal_conn_addref(conn);
2556 CDEBUG(D_NETERROR, "A connection with %s "
2557 "(%u.%u.%u.%u:%d) was reset; "
2558 "it may have rebooted.\n",
2559 libcfs_id2str(peer->ksnp_id),
2560 HIPQUAD(conn->ksnc_ipaddr),
2564 CDEBUG(D_NETERROR, "A connection with %s "
2565 "(%u.%u.%u.%u:%d) timed out; the "
2566 "network or node may be down.\n",
2567 libcfs_id2str(peer->ksnp_id),
2568 HIPQUAD(conn->ksnc_ipaddr),
2572 CDEBUG(D_NETERROR, "An unexpected network error %d "
2574 "(%u.%u.%u.%u:%d\n", error,
2575 libcfs_id2str(peer->ksnp_id),
2576 HIPQUAD(conn->ksnc_ipaddr),
2584 if (conn->ksnc_rx_started &&
2585 cfs_time_aftereq(cfs_time_current(),
2586 conn->ksnc_rx_deadline)) {
2587 /* Timed out incomplete incoming message */
2588 ksocknal_conn_addref(conn);
2589 CDEBUG(D_NETERROR, "Timeout receiving from %s "
2590 "(%u.%u.%u.%u:%d), state %d wanted %d left %d\n",
2591 libcfs_id2str(peer->ksnp_id),
2592 HIPQUAD(conn->ksnc_ipaddr),
2594 conn->ksnc_rx_state,
2595 conn->ksnc_rx_nob_wanted,
2596 conn->ksnc_rx_nob_left);
2600 if ((!list_empty(&conn->ksnc_tx_queue) ||
2601 SOCK_WMEM_QUEUED(conn->ksnc_sock) != 0) &&
2602 cfs_time_aftereq(cfs_time_current(),
2603 conn->ksnc_tx_deadline)) {
2604 /* Timed out messages queued for sending or
2605 * buffered in the socket's send buffer */
2606 ksocknal_conn_addref(conn);
2607 CDEBUG(D_NETERROR, "Timeout sending data to %s "
2608 "(%u.%u.%u.%u:%d) the network or that "
2609 "node may be down.\n",
2610 libcfs_id2str(peer->ksnp_id),
2611 HIPQUAD(conn->ksnc_ipaddr),
2621 ksocknal_flush_stale_txs(ksock_peer_t *peer)
2624 CFS_LIST_HEAD (stale_txs);
2626 write_lock_bh (&ksocknal_data.ksnd_global_lock);
2628 while (!list_empty (&peer->ksnp_tx_queue)) {
2629 tx = list_entry (peer->ksnp_tx_queue.next,
2630 ksock_tx_t, tx_list);
2632 if (!cfs_time_aftereq(cfs_time_current(),
2636 list_del (&tx->tx_list);
2637 list_add_tail (&tx->tx_list, &stale_txs);
2640 write_unlock_bh (&ksocknal_data.ksnd_global_lock);
2642 ksocknal_txlist_done(peer->ksnp_ni, &stale_txs, 1);
2646 ksocknal_check_peer_timeouts (int idx)
2648 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2649 struct list_head *ptmp;
2654 /* NB. We expect to have a look at all the peers and not find any
2655 * connections to time out, so we just use a shared lock while we
2657 read_lock (&ksocknal_data.ksnd_global_lock);
2659 list_for_each (ptmp, peers) {
2660 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
2661 conn = ksocknal_find_timed_out_conn (peer);
2664 read_unlock (&ksocknal_data.ksnd_global_lock);
2666 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2668 /* NB we won't find this one again, but we can't
2669 * just proceed with the next peer, since we dropped
2670 * ksnd_global_lock and it might be dead already! */
2671 ksocknal_conn_decref(conn);
2675 /* we can't process stale txs right here because we're
2676 * holding only shared lock */
2677 if (!list_empty (&peer->ksnp_tx_queue)) {
2678 ksock_tx_t *tx = list_entry (peer->ksnp_tx_queue.next,
2679 ksock_tx_t, tx_list);
2681 if (cfs_time_aftereq(cfs_time_current(),
2684 ksocknal_peer_addref(peer);
2685 read_unlock (&ksocknal_data.ksnd_global_lock);
2687 ksocknal_flush_stale_txs(peer);
2689 ksocknal_peer_decref(peer);
2695 /* print out warnings about stale ZC_REQs */
2696 cfs_list_for_each_entry_typed(peer, peers, ksock_peer_t, ksnp_list) {
2700 cfs_list_for_each_entry_typed(tx, &peer->ksnp_zc_req_list,
2701 ksock_tx_t, tx_zc_list) {
2702 if (!cfs_time_aftereq(cfs_time_current(),
2709 tx = list_entry (peer->ksnp_zc_req_list.next,
2710 ksock_tx_t, tx_zc_list);
2711 CWARN("Stale ZC_REQs for peer %s detected: %d; the "
2712 "oldest (%p) timed out %ld secs ago\n",
2713 libcfs_nid2str(peer->ksnp_id.nid), n, tx,
2714 cfs_duration_sec(cfs_time_current() -
2719 read_unlock (&ksocknal_data.ksnd_global_lock);
2723 ksocknal_reaper (void *arg)
2725 cfs_waitlink_t wait;
2727 ksock_sched_t *sched;
2728 struct list_head enomem_conns;
2730 cfs_duration_t timeout;
2733 cfs_time_t deadline = cfs_time_current();
2735 cfs_daemonize ("socknal_reaper");
2736 cfs_block_allsigs ();
2738 CFS_INIT_LIST_HEAD(&enomem_conns);
2739 cfs_waitlink_init (&wait);
2741 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2743 while (!ksocknal_data.ksnd_shuttingdown) {
2745 if (!list_empty (&ksocknal_data.ksnd_deathrow_conns)) {
2746 conn = list_entry (ksocknal_data.ksnd_deathrow_conns.next,
2747 ksock_conn_t, ksnc_list);
2748 list_del (&conn->ksnc_list);
2750 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2752 ksocknal_terminate_conn (conn);
2753 ksocknal_conn_decref(conn);
2755 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2759 if (!list_empty (&ksocknal_data.ksnd_zombie_conns)) {
2760 conn = list_entry (ksocknal_data.ksnd_zombie_conns.next,
2761 ksock_conn_t, ksnc_list);
2762 list_del (&conn->ksnc_list);
2764 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2766 ksocknal_destroy_conn (conn);
2768 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2772 if (!list_empty (&ksocknal_data.ksnd_enomem_conns)) {
2773 list_add(&enomem_conns, &ksocknal_data.ksnd_enomem_conns);
2774 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2777 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2779 /* reschedule all the connections that stalled with ENOMEM... */
2781 while (!list_empty (&enomem_conns)) {
2782 conn = list_entry (enomem_conns.next,
2783 ksock_conn_t, ksnc_tx_list);
2784 list_del (&conn->ksnc_tx_list);
2786 sched = conn->ksnc_scheduler;
2788 spin_lock_bh (&sched->kss_lock);
2790 LASSERT (conn->ksnc_tx_scheduled);
2791 conn->ksnc_tx_ready = 1;
2792 list_add_tail(&conn->ksnc_tx_list, &sched->kss_tx_conns);
2793 cfs_waitq_signal (&sched->kss_waitq);
2795 spin_unlock_bh (&sched->kss_lock);
2799 /* careful with the jiffy wrap... */
2800 while ((timeout = cfs_time_sub(deadline,
2801 cfs_time_current())) <= 0) {
2804 int chunk = ksocknal_data.ksnd_peer_hash_size;
2806 /* Time to check for timeouts on a few more peers: I do
2807 * checks every 'p' seconds on a proportion of the peer
2808 * table and I need to check every connection 'n' times
2809 * within a timeout interval, to ensure I detect a
2810 * timeout on any connection within (n+1)/n times the
2811 * timeout interval. */
2813 if (*ksocknal_tunables.ksnd_timeout > n * p)
2814 chunk = (chunk * n * p) /
2815 *ksocknal_tunables.ksnd_timeout;
2819 for (i = 0; i < chunk; i++) {
2820 ksocknal_check_peer_timeouts (peer_index);
2821 peer_index = (peer_index + 1) %
2822 ksocknal_data.ksnd_peer_hash_size;
2825 deadline = cfs_time_add(deadline, cfs_time_seconds(p));
2828 if (nenomem_conns != 0) {
2829 /* Reduce my timeout if I rescheduled ENOMEM conns.
2830 * This also prevents me getting woken immediately
2831 * if any go back on my enomem list. */
2832 timeout = SOCKNAL_ENOMEM_RETRY;
2834 ksocknal_data.ksnd_reaper_waketime =
2835 cfs_time_add(cfs_time_current(), timeout);
2837 set_current_state (TASK_INTERRUPTIBLE);
2838 cfs_waitq_add (&ksocknal_data.ksnd_reaper_waitq, &wait);
2840 if (!ksocknal_data.ksnd_shuttingdown &&
2841 list_empty (&ksocknal_data.ksnd_deathrow_conns) &&
2842 list_empty (&ksocknal_data.ksnd_zombie_conns))
2843 cfs_waitq_timedwait (&wait, CFS_TASK_INTERRUPTIBLE, timeout);
2845 set_current_state (TASK_RUNNING);
2846 cfs_waitq_del (&ksocknal_data.ksnd_reaper_waitq, &wait);
2848 spin_lock_bh (&ksocknal_data.ksnd_reaper_lock);
2851 spin_unlock_bh (&ksocknal_data.ksnd_reaper_lock);
2853 ksocknal_thread_fini ();