1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5 * Author: Zach Brown <zab@zabbo.net>
6 * Author: Peter J. Braam <braam@clusterfs.com>
7 * Author: Phil Schwan <phil@clusterfs.com>
8 * Author: Eric Barton <eric@bartonsoftware.com>
10 * This file is part of Portals, http://www.sf.net/projects/sandiaportals/
12 * Portals is free software; you can redistribute it and/or
13 * modify it under the terms of version 2 of the GNU General Public
14 * License as published by the Free Software Foundation.
16 * Portals is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Portals; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 * LIB functions follow
33 ksocknal_read(nal_cb_t *nal, void *private, void *dst_addr,
34 user_ptr src_addr, size_t len)
36 CDEBUG(D_NET, LPX64": reading %ld bytes from %p -> %p\n",
37 nal->ni.nid, (long)len, src_addr, dst_addr);
39 memcpy( dst_addr, src_addr, len );
44 ksocknal_write(nal_cb_t *nal, void *private, user_ptr dst_addr,
45 void *src_addr, size_t len)
47 CDEBUG(D_NET, LPX64": writing %ld bytes from %p -> %p\n",
48 nal->ni.nid, (long)len, src_addr, dst_addr);
50 memcpy( dst_addr, src_addr, len );
55 ksocknal_malloc(nal_cb_t *nal, size_t len)
59 PORTAL_ALLOC(buf, len);
68 ksocknal_free(nal_cb_t *nal, void *buf, size_t len)
70 PORTAL_FREE(buf, len);
74 ksocknal_printf(nal_cb_t *nal, const char *fmt, ...)
80 vsnprintf (msg, sizeof (msg), fmt, ap); /* sprint safely */
83 msg[sizeof (msg) - 1] = 0; /* ensure terminated */
85 CDEBUG (D_NET, "%s", msg);
89 ksocknal_cli(nal_cb_t *nal, unsigned long *flags)
91 ksock_nal_data_t *data = nal->nal_data;
93 spin_lock(&data->ksnd_nal_cb_lock);
97 ksocknal_sti(nal_cb_t *nal, unsigned long *flags)
99 ksock_nal_data_t *data;
100 data = nal->nal_data;
102 spin_unlock(&data->ksnd_nal_cb_lock);
106 ksocknal_dist(nal_cb_t *nal, ptl_nid_t nid, unsigned long *dist)
108 /* I would guess that if ksocknal_get_peer (nid) == NULL,
109 and we're not routing, then 'nid' is very distant :) */
110 if ( nal->ni.nid == nid ) {
120 ksocknal_free_ltx (ksock_ltx_t *ltx)
122 atomic_dec(&ksocknal_data.ksnd_nactive_ltxs);
123 PORTAL_FREE(ltx, ltx->ltx_desc_size);
126 #if (SOCKNAL_ZC && SOCKNAL_VADDR_ZC)
128 ksocknal_kvaddr_to_page (unsigned long vaddr)
132 if (vaddr >= VMALLOC_START &&
134 page = vmalloc_to_page ((void *)vaddr);
136 else if (vaddr >= PKMAP_BASE &&
137 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE))
138 page = vmalloc_to_page ((void *)vaddr);
139 /* in 2.4 ^ just walks the page tables */
142 page = virt_to_page (vaddr);
153 ksocknal_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
155 struct socket *sock = conn->ksnc_sock;
156 struct iovec *iov = tx->tx_iov;
157 int fragsize = iov->iov_len;
158 unsigned long vaddr = (unsigned long)iov->iov_base;
159 int more = (tx->tx_niov > 1) ||
160 (tx->tx_nkiov > 0) ||
161 (!list_empty (&conn->ksnc_tx_queue));
162 #if (SOCKNAL_ZC && SOCKNAL_VADDR_ZC)
163 int offset = vaddr & (PAGE_SIZE - 1);
164 int zcsize = MIN (fragsize, PAGE_SIZE - offset);
169 /* NB we can't trust socket ops to either consume our iovs
170 * or leave them alone, so we only send 1 frag at a time. */
171 LASSERT (fragsize <= tx->tx_resid);
172 LASSERT (tx->tx_niov > 0);
174 #if (SOCKNAL_ZC && SOCKNAL_VADDR_ZC)
175 if (zcsize >= ksocknal_data.ksnd_zc_min_frag &&
176 (sock->sk->route_caps & NETIF_F_SG) &&
177 (sock->sk->route_caps & (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)) &&
178 (page = ksocknal_kvaddr_to_page (vaddr)) != NULL) {
180 CDEBUG(D_NET, "vaddr %p, page %p->%p + offset %x for %d\n",
181 (void *)vaddr, page, page_address(page), offset, zcsize);
183 if (fragsize > zcsize) {
188 rc = tcp_sendpage_zccd(sock, page, offset, zcsize,
189 more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT,
194 /* NB don't pass tx's iov; sendmsg may or may not update it */
195 struct iovec fragiov = { .iov_base = (void *)vaddr,
196 .iov_len = fragsize};
197 struct msghdr msg = {
204 .msg_flags = more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT
206 mm_segment_t oldmm = get_fs();
209 rc = sock_sendmsg(sock, &msg, fragsize);
216 if (rc < iov->iov_len) {
217 /* didn't send whole iov entry... */
218 iov->iov_base = (void *)(vaddr + rc);
230 ksocknal_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
232 struct socket *sock = conn->ksnc_sock;
233 ptl_kiov_t *kiov = tx->tx_kiov;
234 int fragsize = kiov->kiov_len;
235 struct page *page = kiov->kiov_page;
236 int offset = kiov->kiov_offset;
237 int more = (tx->tx_nkiov > 1) ||
238 (!list_empty (&conn->ksnc_tx_queue));
241 /* NB we can't trust socket ops to either consume our iovs
242 * or leave them alone, so we only send 1 frag at a time. */
243 LASSERT (fragsize <= tx->tx_resid);
244 LASSERT (offset + fragsize <= PAGE_SIZE);
245 LASSERT (tx->tx_niov == 0);
246 LASSERT (tx->tx_nkiov > 0);
249 if (fragsize >= ksocknal_data.ksnd_zc_min_frag &&
250 (sock->sk->route_caps & NETIF_F_SG) &&
251 (sock->sk->route_caps & (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM))) {
253 CDEBUG(D_NET, "page %p + offset %x for %d\n",
254 page, offset, fragsize);
256 rc = tcp_sendpage_zccd(sock, page, offset, fragsize,
257 more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT,
262 char *addr = ((char *)kmap (page)) + offset;
263 struct iovec fragiov = {.iov_base = addr,
264 .iov_len = fragsize};
265 struct msghdr msg = {
272 .msg_flags = more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT
274 mm_segment_t oldmm = get_fs();
277 rc = sock_sendmsg(sock, &msg, fragsize);
287 kiov->kiov_offset = offset + rc;
288 kiov->kiov_len = fragsize - rc;
299 ksocknal_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
303 if (ksocknal_data.ksnd_stall_tx != 0) {
304 set_current_state (TASK_UNINTERRUPTIBLE);
305 schedule_timeout (ksocknal_data.ksnd_stall_tx * HZ);
308 LASSERT (tx->tx_resid != 0);
310 rc = ksocknal_getconnsock (conn);
312 LASSERT (conn->ksnc_closing);
317 if (ksocknal_data.ksnd_enomem_tx > 0) {
319 ksocknal_data.ksnd_enomem_tx--;
321 } else if (tx->tx_niov != 0) {
322 rc = ksocknal_send_iov (conn, tx);
324 rc = ksocknal_send_kiov (conn, tx);
328 /* Didn't write anything.
330 * NB: rc == 0 and rc == -EAGAIN both mean try
331 * again later (linux stack returns -EAGAIN for
332 * this, but Adaptech TOE returns 0).
334 * Also, sends never fail with -ENOMEM, just
335 * -EAGAIN, but with the added bonus that we can't
336 * expect write_space() to call us back to tell us
337 * when to try sending again. We use the
338 * SOCK_NOSPACE flag to diagnose... */
340 LASSERT(rc != -ENOMEM);
342 if (rc == 0 || rc == -EAGAIN) {
343 if (test_bit(SOCK_NOSPACE,
344 &conn->ksnc_sock->flags)) {
350 if ((counter & (-counter)) == counter)
351 CWARN("%d ENOMEM tx %p\n",
361 /* Consider the connection alive since we managed to chuck
362 * more data into it. Really, we'd like to consider it
363 * alive only when the peer ACKs something, but
364 * write_space() only gets called back while SOCK_NOSPACE
365 * is set. Instead, we presume peer death has occurred if
366 * the socket doesn't drain within a timout */
367 conn->ksnc_tx_deadline = jiffies +
368 ksocknal_data.ksnd_io_timeout * HZ;
369 conn->ksnc_peer->ksnp_last_alive = jiffies;
371 } while (tx->tx_resid != 0);
373 ksocknal_putconnsock (conn);
378 ksocknal_eager_ack (ksock_conn_t *conn)
381 mm_segment_t oldmm = get_fs();
382 struct socket *sock = conn->ksnc_sock;
384 /* Remind the socket to ACK eagerly. If I don't, the socket might
385 * think I'm about to send something it could piggy-back the ACK
386 * on, introducing delay in completing zero-copy sends in my
390 sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
391 (char *)&opt, sizeof (opt));
396 ksocknal_recv_iov (ksock_conn_t *conn)
398 struct iovec *iov = conn->ksnc_rx_iov;
399 int fragsize = iov->iov_len;
400 unsigned long vaddr = (unsigned long)iov->iov_base;
401 struct iovec fragiov = { .iov_base = (void *)vaddr,
402 .iov_len = fragsize};
403 struct msghdr msg = {
412 mm_segment_t oldmm = get_fs();
415 /* NB we can't trust socket ops to either consume our iovs
416 * or leave them alone, so we only receive 1 frag at a time. */
417 LASSERT (conn->ksnc_rx_niov > 0);
418 LASSERT (fragsize <= conn->ksnc_rx_nob_wanted);
421 rc = sock_recvmsg (conn->ksnc_sock, &msg, fragsize, MSG_DONTWAIT);
422 /* NB this is just a boolean............................^ */
428 /* received something... */
429 conn->ksnc_peer->ksnp_last_alive = jiffies;
430 conn->ksnc_rx_deadline = jiffies +
431 ksocknal_data.ksnd_io_timeout * HZ;
432 mb(); /* order with setting rx_started */
433 conn->ksnc_rx_started = 1;
435 conn->ksnc_rx_nob_wanted -= rc;
436 conn->ksnc_rx_nob_left -= rc;
439 iov->iov_base = (void *)(vaddr + rc);
440 iov->iov_len = fragsize - rc;
445 conn->ksnc_rx_niov--;
450 ksocknal_recv_kiov (ksock_conn_t *conn)
452 ptl_kiov_t *kiov = conn->ksnc_rx_kiov;
453 struct page *page = kiov->kiov_page;
454 int offset = kiov->kiov_offset;
455 int fragsize = kiov->kiov_len;
456 unsigned long vaddr = ((unsigned long)kmap (page)) + offset;
457 struct iovec fragiov = { .iov_base = (void *)vaddr,
458 .iov_len = fragsize};
459 struct msghdr msg = {
468 mm_segment_t oldmm = get_fs();
471 /* NB we can't trust socket ops to either consume our iovs
472 * or leave them alone, so we only receive 1 frag at a time. */
473 LASSERT (fragsize <= conn->ksnc_rx_nob_wanted);
474 LASSERT (conn->ksnc_rx_nkiov > 0);
475 LASSERT (offset + fragsize <= PAGE_SIZE);
478 rc = sock_recvmsg (conn->ksnc_sock, &msg, fragsize, MSG_DONTWAIT);
479 /* NB this is just a boolean............................^ */
487 /* received something... */
488 conn->ksnc_peer->ksnp_last_alive = jiffies;
489 conn->ksnc_rx_deadline = jiffies +
490 ksocknal_data.ksnd_io_timeout * HZ;
491 mb(); /* order with setting rx_started */
492 conn->ksnc_rx_started = 1;
494 conn->ksnc_rx_nob_wanted -= rc;
495 conn->ksnc_rx_nob_left -= rc;
498 kiov->kiov_offset = offset + rc;
499 kiov->kiov_len = fragsize - rc;
503 conn->ksnc_rx_kiov++;
504 conn->ksnc_rx_nkiov--;
509 ksocknal_receive (ksock_conn_t *conn)
511 /* Return 1 on success, 0 on EOF, < 0 on error.
512 * Caller checks ksnc_rx_nob_wanted to determine
513 * progress/completion. */
517 if (ksocknal_data.ksnd_stall_rx != 0) {
518 set_current_state (TASK_UNINTERRUPTIBLE);
519 schedule_timeout (ksocknal_data.ksnd_stall_rx * HZ);
522 rc = ksocknal_getconnsock (conn);
524 LASSERT (conn->ksnc_closing);
529 if (conn->ksnc_rx_niov != 0)
530 rc = ksocknal_recv_iov (conn);
532 rc = ksocknal_recv_kiov (conn);
535 /* error/EOF or partial receive */
538 } else if (rc == 0 && conn->ksnc_rx_started) {
539 /* EOF in the middle of a message */
545 /* Completed a fragment */
547 if (conn->ksnc_rx_nob_wanted == 0) {
548 /* Completed a message segment (header or payload) */
549 if ((ksocknal_data.ksnd_eager_ack & conn->ksnc_type) != 0 &&
550 (conn->ksnc_rx_state == SOCKNAL_RX_BODY ||
551 conn->ksnc_rx_state == SOCKNAL_RX_BODY_FWD)) {
552 /* Remind the socket to ack eagerly... */
553 ksocknal_eager_ack(conn);
560 ksocknal_putconnsock (conn);
566 ksocknal_zc_callback (zccd_t *zcd)
568 ksock_tx_t *tx = KSOCK_ZCCD_2_TX(zcd);
569 ksock_sched_t *sched = tx->tx_conn->ksnc_scheduler;
573 /* Schedule tx for cleanup (can't do it now due to lock conflicts) */
575 spin_lock_irqsave (&sched->kss_lock, flags);
577 list_add_tail (&tx->tx_list, &sched->kss_zctxdone_list);
578 wake_up (&sched->kss_waitq);
580 spin_unlock_irqrestore (&sched->kss_lock, flags);
586 ksocknal_tx_done (ksock_tx_t *tx, int asynch)
591 if (tx->tx_conn != NULL) {
592 /* This tx got queued on a conn; do the accounting... */
593 atomic_sub (tx->tx_nob, &tx->tx_conn->ksnc_tx_nob);
595 /* zero copy completion isn't always from
596 * process_transmit() so it needs to keep a ref on
599 ksocknal_put_conn (tx->tx_conn);
605 if (tx->tx_isfwd) { /* was a forwarded packet? */
606 kpr_fwd_done (&ksocknal_data.ksnd_router,
607 KSOCK_TX_2_KPR_FWD_DESC (tx),
608 (tx->tx_resid == 0) ? 0 : -ECONNABORTED);
614 ltx = KSOCK_TX_2_KSOCK_LTX (tx);
616 lib_finalize (&ksocknal_lib, ltx->ltx_private, ltx->ltx_cookie,
617 (tx->tx_resid == 0) ? PTL_OK : PTL_FAIL);
619 ksocknal_free_ltx (ltx);
624 ksocknal_tx_launched (ksock_tx_t *tx)
627 if (atomic_read (&tx->tx_zccd.zccd_count) != 1) {
628 ksock_conn_t *conn = tx->tx_conn;
630 /* zccd skbufs are still in-flight. First take a ref on
631 * conn, so it hangs about for ksocknal_tx_done... */
632 atomic_inc (&conn->ksnc_refcount);
634 /* ...then drop the initial ref on zccd, so the zero copy
635 * callback can occur */
636 zccd_put (&tx->tx_zccd);
640 /* Any zero-copy-ness (if any) has completed; I can complete the
641 * transmit now, avoiding an extra schedule */
642 ksocknal_tx_done (tx, 0);
646 ksocknal_process_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
651 rc = ksocknal_transmit (conn, tx);
653 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
655 if (tx->tx_resid == 0) {
656 /* Sent everything OK */
659 ksocknal_tx_launched (tx);
667 /* Queue on ksnd_enomem_conns for retry after a timeout */
668 spin_lock_irqsave(&ksocknal_data.ksnd_reaper_lock, flags);
670 /* enomem list takes over scheduler's ref... */
671 LASSERT (conn->ksnc_tx_scheduled);
672 list_add_tail(&conn->ksnc_tx_list,
673 &ksocknal_data.ksnd_enomem_conns);
674 if (!time_after_eq(jiffies + SOCKNAL_ENOMEM_RETRY,
675 ksocknal_data.ksnd_reaper_waketime))
676 wake_up (&ksocknal_data.ksnd_reaper_waitq);
678 spin_unlock_irqrestore(&ksocknal_data.ksnd_reaper_lock, flags);
685 if (!conn->ksnc_closing)
686 CERROR("[%p] Error %d on write to "LPX64
687 " ip %d.%d.%d.%d:%d\n", conn, rc,
688 conn->ksnc_peer->ksnp_nid,
689 HIPQUAD(conn->ksnc_ipaddr),
692 ksocknal_close_conn_and_siblings (conn, rc);
693 ksocknal_tx_launched (tx);
699 ksocknal_launch_autoconnect_locked (ksock_route_t *route)
703 /* called holding write lock on ksnd_global_lock */
705 LASSERT (!route->ksnr_deleted);
706 LASSERT ((route->ksnr_connected & (1 << SOCKNAL_CONN_ANY)) == 0);
707 LASSERT ((route->ksnr_connected & KSNR_TYPED_ROUTES) != KSNR_TYPED_ROUTES);
708 LASSERT (!route->ksnr_connecting);
710 if (ksocknal_data.ksnd_typed_conns)
711 route->ksnr_connecting =
712 KSNR_TYPED_ROUTES & ~route->ksnr_connected;
714 route->ksnr_connecting = (1 << SOCKNAL_CONN_ANY);
716 atomic_inc (&route->ksnr_refcount); /* extra ref for asynchd */
718 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
720 list_add_tail (&route->ksnr_connect_list,
721 &ksocknal_data.ksnd_autoconnectd_routes);
722 wake_up (&ksocknal_data.ksnd_autoconnectd_waitq);
724 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
728 ksocknal_find_target_peer_locked (ksock_tx_t *tx, ptl_nid_t nid)
730 char ipbuf[PTL_NALFMT_SIZE];
731 ptl_nid_t target_nid;
733 ksock_peer_t *peer = ksocknal_find_peer_locked (nid);
739 CERROR ("Can't send packet to "LPX64
740 " %s: routed target is not a peer\n",
741 nid, portals_nid2str(SOCKNAL, nid, ipbuf));
745 rc = kpr_lookup (&ksocknal_data.ksnd_router, nid, tx->tx_nob,
748 CERROR ("Can't route to "LPX64" %s: router error %d\n",
749 nid, portals_nid2str(SOCKNAL, nid, ipbuf), rc);
753 peer = ksocknal_find_peer_locked (target_nid);
757 CERROR ("Can't send packet to "LPX64" %s: no peer entry\n",
758 target_nid, portals_nid2str(SOCKNAL, target_nid, ipbuf));
763 ksocknal_find_conn_locked (ksock_tx_t *tx, ksock_peer_t *peer)
765 struct list_head *tmp;
766 ksock_conn_t *typed = NULL;
768 ksock_conn_t *fallback = NULL;
771 /* Find the conn with the shortest tx queue */
772 list_for_each (tmp, &peer->ksnp_conns) {
773 ksock_conn_t *c = list_entry(tmp, ksock_conn_t, ksnc_list);
774 int nob = atomic_read(&c->ksnc_tx_nob) +
775 c->ksnc_sock->sk->sk_wmem_queued;
777 LASSERT (!c->ksnc_closing);
779 if (fallback == NULL || nob < fnob) {
784 if (!ksocknal_data.ksnd_typed_conns)
787 switch (c->ksnc_type) {
790 case SOCKNAL_CONN_ANY:
792 case SOCKNAL_CONN_BULK_IN:
794 case SOCKNAL_CONN_BULK_OUT:
795 if (tx->tx_nob < ksocknal_data.ksnd_min_bulk)
798 case SOCKNAL_CONN_CONTROL:
799 if (tx->tx_nob >= ksocknal_data.ksnd_min_bulk)
804 if (typed == NULL || nob < tnob) {
810 /* prefer the typed selection */
811 return ((typed != NULL) ? typed : fallback);
815 ksocknal_queue_tx_locked (ksock_tx_t *tx, ksock_conn_t *conn)
818 ksock_sched_t *sched = conn->ksnc_scheduler;
820 /* called holding global lock (read or irq-write) and caller may
821 * not have dropped this lock between finding conn and calling me,
822 * so we don't need the {get,put}connsock dance to deref
824 LASSERT(!conn->ksnc_closing);
825 LASSERT(tx->tx_resid == tx->tx_nob);
827 CDEBUG (D_NET, "Sending to "LPX64" ip %d.%d.%d.%d:%d\n",
828 conn->ksnc_peer->ksnp_nid,
829 HIPQUAD(conn->ksnc_ipaddr),
832 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
836 zccd_init (&tx->tx_zccd, ksocknal_zc_callback);
837 /* NB this sets 1 ref on zccd, so the callback can only occur after
838 * I've released this ref. */
840 spin_lock_irqsave (&sched->kss_lock, flags);
842 conn->ksnc_tx_deadline = jiffies +
843 ksocknal_data.ksnd_io_timeout * HZ;
844 mb(); /* order with list_add_tail */
846 list_add_tail (&tx->tx_list, &conn->ksnc_tx_queue);
848 if (conn->ksnc_tx_ready && /* able to send */
849 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
850 /* +1 ref for scheduler */
851 atomic_inc (&conn->ksnc_refcount);
852 list_add_tail (&conn->ksnc_tx_list,
853 &sched->kss_tx_conns);
854 conn->ksnc_tx_scheduled = 1;
855 wake_up (&sched->kss_waitq);
858 spin_unlock_irqrestore (&sched->kss_lock, flags);
862 ksocknal_find_connectable_route_locked (ksock_peer_t *peer)
864 struct list_head *tmp;
865 ksock_route_t *route;
866 ksock_route_t *candidate = NULL;
870 list_for_each (tmp, &peer->ksnp_routes) {
871 route = list_entry (tmp, ksock_route_t, ksnr_list);
872 bits = route->ksnr_connected;
874 if ((bits & KSNR_TYPED_ROUTES) == KSNR_TYPED_ROUTES ||
875 (bits & (1 << SOCKNAL_CONN_ANY)) != 0 ||
876 route->ksnr_connecting != 0) {
877 /* All typed connections have been established, or
878 * an untyped connection has been established, or
879 * connections are currently being established */
884 /* too soon to retry this guy? */
885 if (!time_after_eq (jiffies, route->ksnr_timeout))
888 /* always do eager routes */
889 if (route->ksnr_eager)
892 if (candidate == NULL) {
893 /* If we don't find any other route that is fully
894 * connected or connecting, the first connectable
895 * route is returned. If it fails to connect, it
896 * will get placed at the end of the list */
901 return (found ? NULL : candidate);
905 ksocknal_find_connecting_route_locked (ksock_peer_t *peer)
907 struct list_head *tmp;
908 ksock_route_t *route;
910 list_for_each (tmp, &peer->ksnp_routes) {
911 route = list_entry (tmp, ksock_route_t, ksnr_list);
913 if (route->ksnr_connecting != 0)
921 ksocknal_launch_packet (ksock_tx_t *tx, ptl_nid_t nid)
926 ksock_route_t *route;
929 /* Ensure the frags we've been given EXACTLY match the number of
930 * bytes we want to send. Many TCP/IP stacks disregard any total
931 * size parameters passed to them and just look at the frags.
933 * We always expect at least 1 mapped fragment containing the
934 * complete portals header. */
935 LASSERT (lib_iov_nob (tx->tx_niov, tx->tx_iov) +
936 lib_kiov_nob (tx->tx_nkiov, tx->tx_kiov) == tx->tx_nob);
937 LASSERT (tx->tx_niov >= 1);
938 LASSERT (tx->tx_iov[0].iov_len >= sizeof (ptl_hdr_t));
940 CDEBUG (D_NET, "packet %p type %d, nob %d niov %d nkiov %d\n",
941 tx, ((ptl_hdr_t *)tx->tx_iov[0].iov_base)->type,
942 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
944 tx->tx_conn = NULL; /* only set when assigned a conn */
945 tx->tx_resid = tx->tx_nob;
946 tx->tx_hdr = (ptl_hdr_t *)tx->tx_iov[0].iov_base;
948 g_lock = &ksocknal_data.ksnd_global_lock;
951 peer = ksocknal_find_target_peer_locked (tx, nid);
953 read_unlock (g_lock);
954 return (-EHOSTUNREACH);
957 if (ksocknal_find_connectable_route_locked(peer) == NULL) {
958 conn = ksocknal_find_conn_locked (tx, peer);
960 /* I've got no autoconnect routes that need to be
961 * connecting and I do have an actual connection... */
962 ksocknal_queue_tx_locked (tx, conn);
963 read_unlock (g_lock);
968 /* Making one or more connections; I'll need a write lock... */
970 atomic_inc (&peer->ksnp_refcount); /* +1 ref for me while I unlock */
971 read_unlock (g_lock);
972 write_lock_irqsave (g_lock, flags);
974 if (peer->ksnp_closing) { /* peer deleted as I blocked! */
975 write_unlock_irqrestore (g_lock, flags);
976 ksocknal_put_peer (peer);
977 return (-EHOSTUNREACH);
979 ksocknal_put_peer (peer); /* drop ref I got above */
982 /* launch any/all autoconnections that need it */
983 route = ksocknal_find_connectable_route_locked (peer);
987 ksocknal_launch_autoconnect_locked (route);
990 conn = ksocknal_find_conn_locked (tx, peer);
992 /* Connection exists; queue message on it */
993 ksocknal_queue_tx_locked (tx, conn);
994 write_unlock_irqrestore (g_lock, flags);
998 route = ksocknal_find_connecting_route_locked (peer);
1000 /* At least 1 connection is being established; queue the
1002 list_add_tail (&tx->tx_list, &peer->ksnp_tx_queue);
1003 write_unlock_irqrestore (g_lock, flags);
1007 write_unlock_irqrestore (g_lock, flags);
1008 return (-EHOSTUNREACH);
1012 ksocknal_sendmsg(nal_cb_t *nal,
1019 unsigned int payload_niov,
1020 struct iovec *payload_iov,
1021 ptl_kiov_t *payload_kiov,
1022 size_t payload_offset,
1029 /* NB 'private' is different depending on what we're sending.
1030 * Just ignore it... */
1032 CDEBUG(D_NET, "sending "LPSZ" bytes in %d frags to nid:"LPX64
1033 " pid %d\n", payload_nob, payload_niov, nid , pid);
1035 LASSERT (payload_nob == 0 || payload_niov > 0);
1036 LASSERT (payload_niov <= PTL_MD_MAX_IOV);
1038 /* It must be OK to kmap() if required */
1039 LASSERT (payload_kiov == NULL || !in_interrupt ());
1040 /* payload is either all vaddrs or all pages */
1041 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1043 if (payload_iov != NULL)
1044 desc_size = offsetof(ksock_ltx_t, ltx_iov[1 + payload_niov]);
1046 desc_size = offsetof(ksock_ltx_t, ltx_kiov[payload_niov]);
1048 if (in_interrupt() ||
1049 type == PTL_MSG_ACK ||
1050 type == PTL_MSG_REPLY) {
1051 /* Can't block if in interrupt or responding to an incoming
1053 PORTAL_ALLOC_ATOMIC(ltx, desc_size);
1055 PORTAL_ALLOC(ltx, desc_size);
1059 CERROR("Can't allocate tx desc type %d size %d %s\n",
1060 type, desc_size, in_interrupt() ? "(intr)" : "");
1061 return (PTL_NOSPACE);
1064 atomic_inc(&ksocknal_data.ksnd_nactive_ltxs);
1066 ltx->ltx_desc_size = desc_size;
1068 /* We always have 1 mapped frag for the header */
1069 ltx->ltx_tx.tx_iov = ltx->ltx_iov;
1070 ltx->ltx_iov[0].iov_base = <x->ltx_hdr;
1071 ltx->ltx_iov[0].iov_len = sizeof(*hdr);
1072 ltx->ltx_hdr = *hdr;
1074 ltx->ltx_private = private;
1075 ltx->ltx_cookie = cookie;
1077 ltx->ltx_tx.tx_isfwd = 0;
1078 ltx->ltx_tx.tx_nob = sizeof (*hdr) + payload_nob;
1080 if (payload_iov != NULL) {
1081 /* payload is all mapped */
1082 ltx->ltx_tx.tx_kiov = NULL;
1083 ltx->ltx_tx.tx_nkiov = 0;
1085 ltx->ltx_tx.tx_niov =
1086 1 + lib_extract_iov(payload_niov, <x->ltx_iov[1],
1087 payload_niov, payload_iov,
1088 payload_offset, payload_nob);
1090 /* payload is all pages */
1091 ltx->ltx_tx.tx_niov = 1;
1093 ltx->ltx_tx.tx_kiov = ltx->ltx_kiov;
1094 ltx->ltx_tx.tx_nkiov =
1095 lib_extract_kiov(payload_niov, ltx->ltx_kiov,
1096 payload_niov, payload_kiov,
1097 payload_offset, payload_nob);
1100 rc = ksocknal_launch_packet(<x->ltx_tx, nid);
1104 ksocknal_free_ltx(ltx);
1109 ksocknal_send (nal_cb_t *nal, void *private, lib_msg_t *cookie,
1110 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
1111 unsigned int payload_niov, struct iovec *payload_iov,
1112 size_t payload_offset, size_t payload_len)
1114 return (ksocknal_sendmsg(nal, private, cookie,
1115 hdr, type, nid, pid,
1116 payload_niov, payload_iov, NULL,
1117 payload_offset, payload_len));
1121 ksocknal_send_pages (nal_cb_t *nal, void *private, lib_msg_t *cookie,
1122 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
1123 unsigned int payload_niov, ptl_kiov_t *payload_kiov,
1124 size_t payload_offset, size_t payload_len)
1126 return (ksocknal_sendmsg(nal, private, cookie,
1127 hdr, type, nid, pid,
1128 payload_niov, NULL, payload_kiov,
1129 payload_offset, payload_len));
1133 ksocknal_fwd_packet (void *arg, kpr_fwd_desc_t *fwd)
1135 ptl_nid_t nid = fwd->kprfd_gateway_nid;
1136 ksock_ftx_t *ftx = (ksock_ftx_t *)&fwd->kprfd_scratch;
1139 CDEBUG (D_NET, "Forwarding [%p] -> "LPX64" ("LPX64"))\n", fwd,
1140 fwd->kprfd_gateway_nid, fwd->kprfd_target_nid);
1142 /* I'm the gateway; must be the last hop */
1143 if (nid == ksocknal_lib.ni.nid)
1144 nid = fwd->kprfd_target_nid;
1146 /* setup iov for hdr */
1147 ftx->ftx_iov.iov_base = fwd->kprfd_hdr;
1148 ftx->ftx_iov.iov_len = sizeof(ptl_hdr_t);
1150 ftx->ftx_tx.tx_isfwd = 1; /* This is a forwarding packet */
1151 ftx->ftx_tx.tx_nob = sizeof(ptl_hdr_t) + fwd->kprfd_nob;
1152 ftx->ftx_tx.tx_niov = 1;
1153 ftx->ftx_tx.tx_iov = &ftx->ftx_iov;
1154 ftx->ftx_tx.tx_nkiov = fwd->kprfd_niov;
1155 ftx->ftx_tx.tx_kiov = fwd->kprfd_kiov;
1157 rc = ksocknal_launch_packet (&ftx->ftx_tx, nid);
1159 kpr_fwd_done (&ksocknal_data.ksnd_router, fwd, rc);
1163 ksocknal_thread_start (int (*fn)(void *arg), void *arg)
1165 long pid = kernel_thread (fn, arg, 0);
1170 atomic_inc (&ksocknal_data.ksnd_nthreads);
1175 ksocknal_thread_fini (void)
1177 atomic_dec (&ksocknal_data.ksnd_nthreads);
1181 ksocknal_fmb_callback (void *arg, int error)
1183 ksock_fmb_t *fmb = (ksock_fmb_t *)arg;
1184 ksock_fmb_pool_t *fmp = fmb->fmb_pool;
1185 ptl_hdr_t *hdr = (ptl_hdr_t *)page_address(fmb->fmb_kiov[0].kiov_page);
1186 ksock_conn_t *conn = NULL;
1187 ksock_sched_t *sched;
1188 unsigned long flags;
1189 char ipbuf[PTL_NALFMT_SIZE];
1190 char ipbuf2[PTL_NALFMT_SIZE];
1193 CERROR("Failed to route packet from "
1194 LPX64" %s to "LPX64" %s: %d\n",
1195 NTOH__u64(hdr->src_nid),
1196 portals_nid2str(SOCKNAL, NTOH__u64(hdr->src_nid), ipbuf),
1197 NTOH__u64(hdr->dest_nid),
1198 portals_nid2str(SOCKNAL, NTOH__u64(hdr->dest_nid), ipbuf2),
1201 CDEBUG (D_NET, "routed packet from "LPX64" to "LPX64": OK\n",
1202 NTOH__u64 (hdr->src_nid), NTOH__u64 (hdr->dest_nid));
1204 /* drop peer ref taken on init */
1205 ksocknal_put_peer (fmb->fmb_peer);
1207 spin_lock_irqsave (&fmp->fmp_lock, flags);
1209 list_add (&fmb->fmb_list, &fmp->fmp_idle_fmbs);
1210 fmp->fmp_nactive_fmbs--;
1212 if (!list_empty (&fmp->fmp_blocked_conns)) {
1213 conn = list_entry (fmb->fmb_pool->fmp_blocked_conns.next,
1214 ksock_conn_t, ksnc_rx_list);
1215 list_del (&conn->ksnc_rx_list);
1218 spin_unlock_irqrestore (&fmp->fmp_lock, flags);
1223 CDEBUG (D_NET, "Scheduling conn %p\n", conn);
1224 LASSERT (conn->ksnc_rx_scheduled);
1225 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_FMB_SLEEP);
1227 conn->ksnc_rx_state = SOCKNAL_RX_GET_FMB;
1229 sched = conn->ksnc_scheduler;
1231 spin_lock_irqsave (&sched->kss_lock, flags);
1233 list_add_tail (&conn->ksnc_rx_list, &sched->kss_rx_conns);
1234 wake_up (&sched->kss_waitq);
1236 spin_unlock_irqrestore (&sched->kss_lock, flags);
1240 ksocknal_get_idle_fmb (ksock_conn_t *conn)
1242 int payload_nob = conn->ksnc_rx_nob_left;
1243 unsigned long flags;
1244 ksock_fmb_pool_t *pool;
1247 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_GET_FMB);
1248 LASSERT (kpr_routing(&ksocknal_data.ksnd_router));
1250 if (payload_nob <= SOCKNAL_SMALL_FWD_PAGES * PAGE_SIZE)
1251 pool = &ksocknal_data.ksnd_small_fmp;
1253 pool = &ksocknal_data.ksnd_large_fmp;
1255 spin_lock_irqsave (&pool->fmp_lock, flags);
1257 if (!list_empty (&pool->fmp_idle_fmbs)) {
1258 fmb = list_entry(pool->fmp_idle_fmbs.next,
1259 ksock_fmb_t, fmb_list);
1260 list_del (&fmb->fmb_list);
1261 pool->fmp_nactive_fmbs++;
1262 spin_unlock_irqrestore (&pool->fmp_lock, flags);
1267 /* deschedule until fmb free */
1269 conn->ksnc_rx_state = SOCKNAL_RX_FMB_SLEEP;
1271 list_add_tail (&conn->ksnc_rx_list,
1272 &pool->fmp_blocked_conns);
1274 spin_unlock_irqrestore (&pool->fmp_lock, flags);
1279 ksocknal_init_fmb (ksock_conn_t *conn, ksock_fmb_t *fmb)
1281 int payload_nob = conn->ksnc_rx_nob_left;
1282 ptl_nid_t dest_nid = NTOH__u64 (conn->ksnc_hdr.dest_nid);
1284 int nob = payload_nob;
1286 LASSERT (conn->ksnc_rx_scheduled);
1287 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_GET_FMB);
1288 LASSERT (conn->ksnc_rx_nob_wanted == conn->ksnc_rx_nob_left);
1289 LASSERT (payload_nob >= 0);
1290 LASSERT (payload_nob <= fmb->fmb_pool->fmp_buff_pages * PAGE_SIZE);
1291 LASSERT (sizeof (ptl_hdr_t) < PAGE_SIZE);
1292 LASSERT (fmb->fmb_kiov[0].kiov_offset == 0);
1294 /* Take a ref on the conn's peer to prevent module unload before
1295 * forwarding completes. */
1296 fmb->fmb_peer = conn->ksnc_peer;
1297 atomic_inc (&conn->ksnc_peer->ksnp_refcount);
1299 /* Copy the header we just read into the forwarding buffer. If
1300 * there's payload, start reading reading it into the buffer,
1301 * otherwise the forwarding buffer can be kicked off
1303 fmb->fmb_hdr = conn->ksnc_hdr;
1306 LASSERT (niov < fmb->fmb_pool->fmp_buff_pages);
1307 LASSERT (fmb->fmb_kiov[niov].kiov_offset == 0);
1308 fmb->fmb_kiov[niov].kiov_len = MIN (PAGE_SIZE, nob);
1313 kpr_fwd_init(&fmb->fmb_fwd, dest_nid, &fmb->fmb_hdr,
1314 payload_nob, niov, fmb->fmb_kiov,
1315 ksocknal_fmb_callback, fmb);
1317 if (payload_nob == 0) { /* got complete packet already */
1318 CDEBUG (D_NET, "%p "LPX64"->"LPX64" fwd_start (immediate)\n",
1319 conn, NTOH__u64 (conn->ksnc_hdr.src_nid), dest_nid);
1321 kpr_fwd_start (&ksocknal_data.ksnd_router, &fmb->fmb_fwd);
1323 ksocknal_new_packet (conn, 0); /* on to next packet */
1327 conn->ksnc_cookie = fmb; /* stash fmb for later */
1328 conn->ksnc_rx_state = SOCKNAL_RX_BODY_FWD; /* read in the payload */
1330 /* Set up conn->ksnc_rx_kiov to read the payload into fmb's kiov-ed
1332 LASSERT (niov <= sizeof(conn->ksnc_rx_iov_space)/sizeof(ptl_kiov_t));
1334 conn->ksnc_rx_niov = 0;
1335 conn->ksnc_rx_nkiov = niov;
1336 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1337 memcpy(conn->ksnc_rx_kiov, fmb->fmb_kiov, niov * sizeof(ptl_kiov_t));
1339 CDEBUG (D_NET, "%p "LPX64"->"LPX64" %d reading body\n", conn,
1340 NTOH__u64 (conn->ksnc_hdr.src_nid), dest_nid, payload_nob);
1345 ksocknal_fwd_parse (ksock_conn_t *conn)
1348 ptl_nid_t dest_nid = NTOH__u64 (conn->ksnc_hdr.dest_nid);
1349 ptl_nid_t src_nid = NTOH__u64 (conn->ksnc_hdr.src_nid);
1350 int body_len = NTOH__u32 (conn->ksnc_hdr.payload_length);
1351 char str[PTL_NALFMT_SIZE];
1352 char str2[PTL_NALFMT_SIZE];
1354 CDEBUG (D_NET, "%p "LPX64"->"LPX64" %d parsing header\n", conn,
1355 src_nid, dest_nid, conn->ksnc_rx_nob_left);
1357 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_HEADER);
1358 LASSERT (conn->ksnc_rx_scheduled);
1360 if (body_len < 0) { /* length corrupt (overflow) */
1361 CERROR("dropping packet from "LPX64" (%s) for "LPX64" (%s): "
1362 "packet size %d illegal\n",
1363 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1364 dest_nid, portals_nid2str(TCPNAL, dest_nid, str2),
1367 ksocknal_new_packet (conn, 0); /* on to new packet */
1371 if (!kpr_routing(&ksocknal_data.ksnd_router)) { /* not forwarding */
1372 CERROR("dropping packet from "LPX64" (%s) for "LPX64
1373 " (%s): not forwarding\n",
1374 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1375 dest_nid, portals_nid2str(TCPNAL, dest_nid, str2));
1376 /* on to new packet (skip this one's body) */
1377 ksocknal_new_packet (conn, body_len);
1381 if (body_len > PTL_MTU) { /* too big to forward */
1382 CERROR ("dropping packet from "LPX64" (%s) for "LPX64
1383 "(%s): packet size %d too big\n",
1384 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1385 dest_nid, portals_nid2str(TCPNAL, dest_nid, str2),
1387 /* on to new packet (skip this one's body) */
1388 ksocknal_new_packet (conn, body_len);
1392 /* should have gone direct */
1393 peer = ksocknal_get_peer (conn->ksnc_hdr.dest_nid);
1395 CERROR ("dropping packet from "LPX64" (%s) for "LPX64
1396 "(%s): target is a peer\n",
1397 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1398 dest_nid, portals_nid2str(TCPNAL, dest_nid, str2));
1399 ksocknal_put_peer (peer); /* drop ref from get above */
1401 /* on to next packet (skip this one's body) */
1402 ksocknal_new_packet (conn, body_len);
1406 conn->ksnc_rx_state = SOCKNAL_RX_GET_FMB; /* Getting FMB now */
1407 conn->ksnc_rx_nob_left = body_len; /* stash packet size */
1408 conn->ksnc_rx_nob_wanted = body_len; /* (no slop) */
1412 ksocknal_new_packet (ksock_conn_t *conn, int nob_to_skip)
1414 static char ksocknal_slop_buffer[4096];
1420 if (nob_to_skip == 0) { /* right at next packet boundary now */
1421 conn->ksnc_rx_started = 0;
1422 mb (); /* racing with timeout thread */
1424 conn->ksnc_rx_state = SOCKNAL_RX_HEADER;
1425 conn->ksnc_rx_nob_wanted = sizeof (ptl_hdr_t);
1426 conn->ksnc_rx_nob_left = sizeof (ptl_hdr_t);
1428 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1429 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_hdr;
1430 conn->ksnc_rx_iov[0].iov_len = sizeof (ptl_hdr_t);
1431 conn->ksnc_rx_niov = 1;
1433 conn->ksnc_rx_kiov = NULL;
1434 conn->ksnc_rx_nkiov = 0;
1438 /* Set up to skip as much a possible now. If there's more left
1439 * (ran out of iov entries) we'll get called again */
1441 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1442 conn->ksnc_rx_nob_left = nob_to_skip;
1443 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1448 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1450 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1451 conn->ksnc_rx_iov[niov].iov_len = nob;
1456 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1457 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof (struct iovec));
1459 conn->ksnc_rx_niov = niov;
1460 conn->ksnc_rx_kiov = NULL;
1461 conn->ksnc_rx_nkiov = 0;
1462 conn->ksnc_rx_nob_wanted = skipped;
1467 ksocknal_process_receive (ksock_conn_t *conn)
1472 LASSERT (atomic_read (&conn->ksnc_refcount) > 0);
1474 /* doesn't need a forwarding buffer */
1475 if (conn->ksnc_rx_state != SOCKNAL_RX_GET_FMB)
1479 fmb = ksocknal_get_idle_fmb (conn);
1481 /* conn descheduled waiting for idle fmb */
1485 if (ksocknal_init_fmb (conn, fmb)) {
1486 /* packet forwarded */
1491 /* NB: sched lock NOT held */
1492 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_HEADER ||
1493 conn->ksnc_rx_state == SOCKNAL_RX_BODY ||
1494 conn->ksnc_rx_state == SOCKNAL_RX_BODY_FWD ||
1495 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1497 LASSERT (conn->ksnc_rx_nob_wanted > 0);
1499 rc = ksocknal_receive(conn);
1502 LASSERT (rc != -EAGAIN);
1505 CWARN ("[%p] EOF from "LPX64" ip %d.%d.%d.%d:%d\n",
1506 conn, conn->ksnc_peer->ksnp_nid,
1507 HIPQUAD(conn->ksnc_ipaddr),
1509 else if (!conn->ksnc_closing)
1510 CERROR ("[%p] Error %d on read from "LPX64
1511 " ip %d.%d.%d.%d:%d\n",
1512 conn, rc, conn->ksnc_peer->ksnp_nid,
1513 HIPQUAD(conn->ksnc_ipaddr),
1516 ksocknal_close_conn_and_siblings (conn, rc);
1517 return (rc == 0 ? -ESHUTDOWN : rc);
1520 if (conn->ksnc_rx_nob_wanted != 0) {
1525 switch (conn->ksnc_rx_state) {
1526 case SOCKNAL_RX_HEADER:
1527 if (conn->ksnc_hdr.type != HTON__u32(PTL_MSG_HELLO) &&
1528 NTOH__u64(conn->ksnc_hdr.dest_nid) != ksocknal_lib.ni.nid) {
1529 /* This packet isn't for me */
1530 ksocknal_fwd_parse (conn);
1531 switch (conn->ksnc_rx_state) {
1532 case SOCKNAL_RX_HEADER: /* skipped (zero payload) */
1533 return (0); /* => come back later */
1534 case SOCKNAL_RX_SLOP: /* skipping packet's body */
1535 goto try_read; /* => go read it */
1536 case SOCKNAL_RX_GET_FMB: /* forwarding */
1537 goto get_fmb; /* => go get a fwd msg buffer */
1544 /* sets wanted_len, iovs etc */
1545 lib_parse(&ksocknal_lib, &conn->ksnc_hdr, conn);
1547 if (conn->ksnc_rx_nob_wanted != 0) { /* need to get payload? */
1548 conn->ksnc_rx_state = SOCKNAL_RX_BODY;
1549 goto try_read; /* go read the payload */
1551 /* Fall through (completed packet for me) */
1553 case SOCKNAL_RX_BODY:
1554 /* payload all received */
1555 lib_finalize(&ksocknal_lib, NULL, conn->ksnc_cookie, PTL_OK);
1558 case SOCKNAL_RX_SLOP:
1559 /* starting new packet? */
1560 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1561 return (0); /* come back later */
1562 goto try_read; /* try to finish reading slop now */
1564 case SOCKNAL_RX_BODY_FWD:
1565 /* payload all received */
1566 CDEBUG (D_NET, "%p "LPX64"->"LPX64" %d fwd_start (got body)\n",
1567 conn, NTOH__u64 (conn->ksnc_hdr.src_nid),
1568 NTOH__u64 (conn->ksnc_hdr.dest_nid),
1569 conn->ksnc_rx_nob_left);
1571 /* forward the packet. NB ksocknal_init_fmb() put fmb into
1572 * conn->ksnc_cookie */
1573 fmb = (ksock_fmb_t *)conn->ksnc_cookie;
1574 kpr_fwd_start (&ksocknal_data.ksnd_router, &fmb->fmb_fwd);
1576 /* no slop in forwarded packets */
1577 LASSERT (conn->ksnc_rx_nob_left == 0);
1579 ksocknal_new_packet (conn, 0); /* on to next packet */
1580 return (0); /* (later) */
1588 return (-EINVAL); /* keep gcc happy */
1592 ksocknal_recv (nal_cb_t *nal, void *private, lib_msg_t *msg,
1593 unsigned int niov, struct iovec *iov,
1594 size_t offset, size_t mlen, size_t rlen)
1596 ksock_conn_t *conn = (ksock_conn_t *)private;
1598 LASSERT (mlen <= rlen);
1599 LASSERT (niov <= PTL_MD_MAX_IOV);
1601 conn->ksnc_cookie = msg;
1602 conn->ksnc_rx_nob_wanted = mlen;
1603 conn->ksnc_rx_nob_left = rlen;
1605 conn->ksnc_rx_nkiov = 0;
1606 conn->ksnc_rx_kiov = NULL;
1607 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1608 conn->ksnc_rx_niov =
1609 lib_extract_iov(PTL_MD_MAX_IOV, conn->ksnc_rx_iov,
1610 niov, iov, offset, mlen);
1613 lib_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1614 lib_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1620 ksocknal_recv_pages (nal_cb_t *nal, void *private, lib_msg_t *msg,
1621 unsigned int niov, ptl_kiov_t *kiov,
1622 size_t offset, size_t mlen, size_t rlen)
1624 ksock_conn_t *conn = (ksock_conn_t *)private;
1626 LASSERT (mlen <= rlen);
1627 LASSERT (niov <= PTL_MD_MAX_IOV);
1629 conn->ksnc_cookie = msg;
1630 conn->ksnc_rx_nob_wanted = mlen;
1631 conn->ksnc_rx_nob_left = rlen;
1633 conn->ksnc_rx_niov = 0;
1634 conn->ksnc_rx_iov = NULL;
1635 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1636 conn->ksnc_rx_nkiov =
1637 lib_extract_kiov(PTL_MD_MAX_IOV, conn->ksnc_rx_kiov,
1638 niov, kiov, offset, mlen);
1641 lib_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1642 lib_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1647 int ksocknal_scheduler (void *arg)
1649 ksock_sched_t *sched = (ksock_sched_t *)arg;
1652 unsigned long flags;
1655 int id = sched - ksocknal_data.ksnd_schedulers;
1658 snprintf (name, sizeof (name),"ksocknald_%02d", id);
1659 kportal_daemonize (name);
1660 kportal_blockallsigs ();
1662 #if (CONFIG_SMP && CPU_AFFINITY)
1663 if ((cpu_online_map & (1 << id)) != 0) {
1665 current->cpus_allowed = (1 << id);
1667 set_cpus_allowed (current, 1<<id);
1670 CERROR ("Can't set CPU affinity for %s\n", name);
1672 #endif /* CONFIG_SMP && CPU_AFFINITY */
1674 spin_lock_irqsave (&sched->kss_lock, flags);
1676 while (!ksocknal_data.ksnd_shuttingdown) {
1677 int did_something = 0;
1679 /* Ensure I progress everything semi-fairly */
1681 if (!list_empty (&sched->kss_rx_conns)) {
1682 conn = list_entry(sched->kss_rx_conns.next,
1683 ksock_conn_t, ksnc_rx_list);
1684 list_del(&conn->ksnc_rx_list);
1686 LASSERT(conn->ksnc_rx_scheduled);
1687 LASSERT(conn->ksnc_rx_ready);
1689 /* clear rx_ready in case receive isn't complete.
1690 * Do it BEFORE we call process_recv, since
1691 * data_ready can set it any time after we release
1693 conn->ksnc_rx_ready = 0;
1694 spin_unlock_irqrestore(&sched->kss_lock, flags);
1696 rc = ksocknal_process_receive(conn);
1698 spin_lock_irqsave(&sched->kss_lock, flags);
1700 /* I'm the only one that can clear this flag */
1701 LASSERT(conn->ksnc_rx_scheduled);
1703 /* Did process_receive get everything it wanted? */
1705 conn->ksnc_rx_ready = 1;
1707 if (conn->ksnc_rx_state == SOCKNAL_RX_FMB_SLEEP ||
1708 conn->ksnc_rx_state == SOCKNAL_RX_GET_FMB) {
1709 /* Conn blocked for a forwarding buffer.
1710 * It will get queued for my attention when
1711 * one becomes available (and it might just
1712 * already have been!). Meanwhile my ref
1713 * on it stays put. */
1714 } else if (conn->ksnc_rx_ready) {
1715 /* reschedule for rx */
1716 list_add_tail (&conn->ksnc_rx_list,
1717 &sched->kss_rx_conns);
1719 conn->ksnc_rx_scheduled = 0;
1721 ksocknal_put_conn(conn);
1727 if (!list_empty (&sched->kss_tx_conns)) {
1728 conn = list_entry(sched->kss_tx_conns.next,
1729 ksock_conn_t, ksnc_tx_list);
1730 list_del (&conn->ksnc_tx_list);
1732 LASSERT(conn->ksnc_tx_scheduled);
1733 LASSERT(conn->ksnc_tx_ready);
1734 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1736 tx = list_entry(conn->ksnc_tx_queue.next,
1737 ksock_tx_t, tx_list);
1738 /* dequeue now so empty list => more to send */
1739 list_del(&tx->tx_list);
1741 /* Clear tx_ready in case send isn't complete. Do
1742 * it BEFORE we call process_transmit, since
1743 * write_space can set it any time after we release
1745 conn->ksnc_tx_ready = 0;
1746 spin_unlock_irqrestore (&sched->kss_lock, flags);
1748 rc = ksocknal_process_transmit(conn, tx);
1750 spin_lock_irqsave (&sched->kss_lock, flags);
1752 if (rc == -ENOMEM || rc == -EAGAIN) {
1753 /* Incomplete send: replace tx on HEAD of tx_queue */
1754 list_add (&tx->tx_list, &conn->ksnc_tx_queue);
1756 /* Complete send; assume space for more */
1757 conn->ksnc_tx_ready = 1;
1760 if (rc == -ENOMEM) {
1761 /* Do nothing; after a short timeout, this
1762 * conn will be reposted on kss_tx_conns. */
1763 } else if (conn->ksnc_tx_ready &&
1764 !list_empty (&conn->ksnc_tx_queue)) {
1765 /* reschedule for tx */
1766 list_add_tail (&conn->ksnc_tx_list,
1767 &sched->kss_tx_conns);
1769 conn->ksnc_tx_scheduled = 0;
1771 ksocknal_put_conn (conn);
1777 if (!list_empty (&sched->kss_zctxdone_list)) {
1779 list_entry(sched->kss_zctxdone_list.next,
1780 ksock_tx_t, tx_list);
1783 list_del (&tx->tx_list);
1784 spin_unlock_irqrestore (&sched->kss_lock, flags);
1786 ksocknal_tx_done (tx, 1);
1788 spin_lock_irqsave (&sched->kss_lock, flags);
1791 if (!did_something || /* nothing to do */
1792 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1793 spin_unlock_irqrestore (&sched->kss_lock, flags);
1797 if (!did_something) { /* wait for something to do */
1799 rc = wait_event_interruptible (sched->kss_waitq,
1800 ksocknal_data.ksnd_shuttingdown ||
1801 !list_empty(&sched->kss_rx_conns) ||
1802 !list_empty(&sched->kss_tx_conns) ||
1803 !list_empty(&sched->kss_zctxdone_list));
1805 rc = wait_event_interruptible (sched->kss_waitq,
1806 ksocknal_data.ksnd_shuttingdown ||
1807 !list_empty(&sched->kss_rx_conns) ||
1808 !list_empty(&sched->kss_tx_conns));
1814 spin_lock_irqsave (&sched->kss_lock, flags);
1818 spin_unlock_irqrestore (&sched->kss_lock, flags);
1819 ksocknal_thread_fini ();
1824 ksocknal_data_ready (struct sock *sk, int n)
1826 unsigned long flags;
1828 ksock_sched_t *sched;
1831 /* interleave correctly with closing sockets... */
1832 read_lock (&ksocknal_data.ksnd_global_lock);
1834 conn = sk->sk_user_data;
1835 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
1836 LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
1837 sk->sk_data_ready (sk, n);
1839 sched = conn->ksnc_scheduler;
1841 spin_lock_irqsave (&sched->kss_lock, flags);
1843 conn->ksnc_rx_ready = 1;
1845 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1846 list_add_tail(&conn->ksnc_rx_list,
1847 &sched->kss_rx_conns);
1848 conn->ksnc_rx_scheduled = 1;
1849 /* extra ref for scheduler */
1850 atomic_inc (&conn->ksnc_refcount);
1852 wake_up (&sched->kss_waitq);
1855 spin_unlock_irqrestore (&sched->kss_lock, flags);
1858 read_unlock (&ksocknal_data.ksnd_global_lock);
1864 ksocknal_write_space (struct sock *sk)
1866 unsigned long flags;
1868 ksock_sched_t *sched;
1870 /* interleave correctly with closing sockets... */
1871 read_lock (&ksocknal_data.ksnd_global_lock);
1873 conn = sk->sk_user_data;
1875 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
1876 sk, tcp_wspace(sk), SOCKNAL_TX_LOW_WATER(sk), conn,
1877 (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
1878 " ready" : " blocked"),
1879 (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
1880 " scheduled" : " idle"),
1881 (conn == NULL) ? "" : (list_empty (&conn->ksnc_tx_queue) ?
1882 " empty" : " queued"));
1884 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
1885 LASSERT (sk->sk_write_space != &ksocknal_write_space);
1886 sk->sk_write_space (sk);
1888 read_unlock (&ksocknal_data.ksnd_global_lock);
1892 if (tcp_wspace(sk) >= SOCKNAL_TX_LOW_WATER(sk)) { /* got enough space */
1893 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
1895 sched = conn->ksnc_scheduler;
1897 spin_lock_irqsave (&sched->kss_lock, flags);
1899 conn->ksnc_tx_ready = 1;
1901 if (!conn->ksnc_tx_scheduled && // not being progressed
1902 !list_empty(&conn->ksnc_tx_queue)){//packets to send
1903 list_add_tail (&conn->ksnc_tx_list,
1904 &sched->kss_tx_conns);
1905 conn->ksnc_tx_scheduled = 1;
1906 /* extra ref for scheduler */
1907 atomic_inc (&conn->ksnc_refcount);
1909 wake_up (&sched->kss_waitq);
1912 spin_unlock_irqrestore (&sched->kss_lock, flags);
1915 read_unlock (&ksocknal_data.ksnd_global_lock);
1919 ksocknal_sock_write (struct socket *sock, void *buffer, int nob)
1922 mm_segment_t oldmm = get_fs();
1925 struct iovec iov = {
1929 struct msghdr msg = {
1934 .msg_control = NULL,
1935 .msg_controllen = 0,
1940 rc = sock_sendmsg (sock, &msg, iov.iov_len);
1947 CERROR ("Unexpected zero rc\n");
1948 return (-ECONNABORTED);
1951 buffer = ((char *)buffer) + rc;
1959 ksocknal_sock_read (struct socket *sock, void *buffer, int nob)
1962 mm_segment_t oldmm = get_fs();
1965 struct iovec iov = {
1969 struct msghdr msg = {
1974 .msg_control = NULL,
1975 .msg_controllen = 0,
1980 rc = sock_recvmsg (sock, &msg, iov.iov_len, 0);
1987 return (-ECONNABORTED);
1989 buffer = ((char *)buffer) + rc;
1997 ksocknal_hello (struct socket *sock, ptl_nid_t *nid, int *type,
2002 ptl_magicversion_t *hmv = (ptl_magicversion_t *)&hdr.dest_nid;
2003 char ipbuf[PTL_NALFMT_SIZE];
2004 char ipbuf2[PTL_NALFMT_SIZE];
2006 LASSERT (sizeof (*hmv) == sizeof (hdr.dest_nid));
2008 memset (&hdr, 0, sizeof (hdr));
2009 hmv->magic = __cpu_to_le32 (PORTALS_PROTO_MAGIC);
2010 hmv->version_major = __cpu_to_le16 (PORTALS_PROTO_VERSION_MAJOR);
2011 hmv->version_minor = __cpu_to_le16 (PORTALS_PROTO_VERSION_MINOR);
2013 hdr.src_nid = __cpu_to_le64 (ksocknal_lib.ni.nid);
2014 hdr.type = __cpu_to_le32 (PTL_MSG_HELLO);
2016 hdr.msg.hello.type = __cpu_to_le32 (*type);
2017 hdr.msg.hello.incarnation =
2018 __cpu_to_le64 (ksocknal_data.ksnd_incarnation);
2020 /* Assume sufficient socket buffering for this message */
2021 rc = ksocknal_sock_write (sock, &hdr, sizeof (hdr));
2023 CERROR ("Error %d sending HELLO to "LPX64" %s\n",
2024 rc, *nid, portals_nid2str(SOCKNAL, *nid, ipbuf));
2028 rc = ksocknal_sock_read (sock, hmv, sizeof (*hmv));
2030 CERROR ("Error %d reading HELLO from "LPX64" %s\n",
2031 rc, *nid, portals_nid2str(SOCKNAL, *nid, ipbuf));
2035 if (hmv->magic != __le32_to_cpu (PORTALS_PROTO_MAGIC)) {
2036 CERROR ("Bad magic %#08x (%#08x expected) from "LPX64" %s\n",
2037 __cpu_to_le32 (hmv->magic), PORTALS_PROTO_MAGIC, *nid,
2038 portals_nid2str(SOCKNAL, *nid, ipbuf));
2042 if (hmv->version_major != __cpu_to_le16 (PORTALS_PROTO_VERSION_MAJOR) ||
2043 hmv->version_minor != __cpu_to_le16 (PORTALS_PROTO_VERSION_MINOR)) {
2044 CERROR ("Incompatible protocol version %d.%d (%d.%d expected)"
2045 " from "LPX64" %s\n",
2046 __le16_to_cpu (hmv->version_major),
2047 __le16_to_cpu (hmv->version_minor),
2048 PORTALS_PROTO_VERSION_MAJOR,
2049 PORTALS_PROTO_VERSION_MINOR,
2050 *nid, portals_nid2str(SOCKNAL, *nid, ipbuf));
2054 #if (PORTALS_PROTO_VERSION_MAJOR != 0)
2055 # error "This code only understands protocol version 0.x"
2057 /* version 0 sends magic/version as the dest_nid of a 'hello' header,
2058 * so read the rest of it in now... */
2060 rc = ksocknal_sock_read (sock, hmv + 1, sizeof (hdr) - sizeof (*hmv));
2062 CERROR ("Error %d reading rest of HELLO hdr from "LPX64" %s\n",
2063 rc, *nid, portals_nid2str(SOCKNAL, *nid, ipbuf));
2067 /* ...and check we got what we expected */
2068 if (hdr.type != __cpu_to_le32 (PTL_MSG_HELLO) ||
2069 hdr.payload_length != __cpu_to_le32 (0)) {
2070 CERROR ("Expecting a HELLO hdr with 0 payload,"
2071 " but got type %d with %d payload from "LPX64" %s\n",
2072 __le32_to_cpu (hdr.type),
2073 __le32_to_cpu (hdr.payload_length), *nid,
2074 portals_nid2str(SOCKNAL, *nid, ipbuf));
2078 if (__le64_to_cpu(hdr.src_nid) == PTL_NID_ANY) {
2079 CERROR("Expecting a HELLO hdr with a NID, but got PTL_NID_ANY\n");
2083 if (*nid == PTL_NID_ANY) { /* don't know peer's nid yet */
2084 *nid = __le64_to_cpu(hdr.src_nid);
2085 } else if (*nid != __le64_to_cpu (hdr.src_nid)) {
2086 CERROR ("Connected to nid "LPX64" %s, but expecting "LPX64" %s\n",
2087 __le64_to_cpu (hdr.src_nid),
2088 portals_nid2str(SOCKNAL,
2089 __le64_to_cpu(hdr.src_nid),
2091 *nid, portals_nid2str(SOCKNAL, *nid, ipbuf2));
2095 if (*type == SOCKNAL_CONN_NONE) {
2096 /* I've accepted this connection; peer determines type */
2097 *type = __le32_to_cpu(hdr.msg.hello.type);
2099 case SOCKNAL_CONN_ANY:
2100 case SOCKNAL_CONN_CONTROL:
2102 case SOCKNAL_CONN_BULK_IN:
2103 *type = SOCKNAL_CONN_BULK_OUT;
2105 case SOCKNAL_CONN_BULK_OUT:
2106 *type = SOCKNAL_CONN_BULK_IN;
2109 CERROR ("Unexpected type %d from "LPX64" %s\n",
2111 portals_nid2str(SOCKNAL, *nid, ipbuf));
2114 } else if (__le32_to_cpu(hdr.msg.hello.type) != SOCKNAL_CONN_NONE) {
2115 CERROR ("Mismatched types: me %d "LPX64" %s %d\n",
2116 *type, *nid, portals_nid2str(SOCKNAL, *nid, ipbuf),
2117 __le32_to_cpu(hdr.msg.hello.type));
2121 *incarnation = __le64_to_cpu(hdr.msg.hello.incarnation);
2127 ksocknal_setup_sock (struct socket *sock)
2129 mm_segment_t oldmm = get_fs ();
2132 struct linger linger;
2134 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
2135 sock->sk->sk_allocation = GFP_NOFS;
2137 sock->sk->allocation = GFP_NOFS;
2140 /* Ensure this socket aborts active sends immediately when we close
2144 linger.l_linger = 0;
2147 rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
2148 (char *)&linger, sizeof (linger));
2151 CERROR ("Can't set SO_LINGER: %d\n", rc);
2157 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
2158 (char *)&option, sizeof (option));
2161 CERROR ("Can't set SO_LINGER2: %d\n", rc);
2165 #if SOCKNAL_USE_KEEPALIVES
2166 /* Keepalives: If 3/4 of the timeout elapses, start probing every
2167 * second until the timeout elapses. */
2169 option = (ksocknal_data.ksnd_io_timeout * 3) / 4;
2171 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
2172 (char *)&option, sizeof (option));
2175 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
2181 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
2182 (char *)&option, sizeof (option));
2185 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
2189 option = ksocknal_data.ksnd_io_timeout / 4;
2191 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
2192 (char *)&option, sizeof (option));
2195 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
2201 rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
2202 (char *)&option, sizeof (option));
2205 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
2213 ksocknal_connect_peer (ksock_route_t *route, int type)
2215 struct sockaddr_in peer_addr;
2216 mm_segment_t oldmm = get_fs();
2219 struct socket *sock;
2221 char ipbuf[PTL_NALFMT_SIZE];
2223 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
2225 CERROR ("Can't create autoconnect socket: %d\n", rc);
2229 /* Ugh; have to map_fd for compatibility with sockets passed in
2230 * from userspace. And we actually need the sock->file refcounting
2231 * that this gives you :) */
2233 fd = sock_map_fd (sock);
2235 sock_release (sock);
2236 CERROR ("sock_map_fd error %d\n", fd);
2240 /* NB the fd now owns the ref on sock->file */
2241 LASSERT (sock->file != NULL);
2242 LASSERT (file_count(sock->file) == 1);
2244 /* Set the socket timeouts, so our connection attempt completes in
2246 tv.tv_sec = ksocknal_data.ksnd_io_timeout;
2250 rc = sock_setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO,
2251 (char *)&tv, sizeof (tv));
2254 CERROR ("Can't set send timeout %d: %d\n",
2255 ksocknal_data.ksnd_io_timeout, rc);
2260 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO,
2261 (char *)&tv, sizeof (tv));
2264 CERROR ("Can't set receive timeout %d: %d\n",
2265 ksocknal_data.ksnd_io_timeout, rc);
2273 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
2274 (char *)&option, sizeof (option));
2277 CERROR ("Can't disable nagle: %d\n", rc);
2282 if (route->ksnr_buffer_size != 0) {
2283 int option = route->ksnr_buffer_size;
2286 rc = sock_setsockopt (sock, SOL_SOCKET, SO_SNDBUF,
2287 (char *)&option, sizeof (option));
2290 CERROR ("Can't set send buffer %d: %d\n",
2291 route->ksnr_buffer_size, rc);
2296 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
2297 (char *)&option, sizeof (option));
2300 CERROR ("Can't set receive buffer %d: %d\n",
2301 route->ksnr_buffer_size, rc);
2306 memset (&peer_addr, 0, sizeof (peer_addr));
2307 peer_addr.sin_family = AF_INET;
2308 peer_addr.sin_port = htons (route->ksnr_port);
2309 peer_addr.sin_addr.s_addr = htonl (route->ksnr_ipaddr);
2311 rc = sock->ops->connect (sock, (struct sockaddr *)&peer_addr,
2312 sizeof (peer_addr), sock->file->f_flags);
2314 CERROR ("Error %d connecting to "LPX64" %s\n", rc,
2315 route->ksnr_peer->ksnp_nid,
2316 portals_nid2str(SOCKNAL,
2317 route->ksnr_peer->ksnp_nid,
2322 rc = ksocknal_create_conn (route, sock, route->ksnr_irq_affinity, type);
2324 /* Take an extra ref on sock->file to compensate for the
2325 * upcoming close which will lose fd's ref on it. */
2326 get_file (sock->file);
2335 ksocknal_autoconnect (ksock_route_t *route)
2337 LIST_HEAD (zombies);
2340 unsigned long flags;
2345 for (type = 0; type < SOCKNAL_CONN_NTYPES; type++)
2346 if ((route->ksnr_connecting & (1 << type)) != 0)
2348 LASSERT (type < SOCKNAL_CONN_NTYPES);
2350 rc = ksocknal_connect_peer (route, type);
2355 /* successfully autoconnected: create_conn did the
2356 * route/conn binding and scheduled any blocked packets */
2358 if (route->ksnr_connecting == 0) {
2359 /* No more connections required */
2364 /* Connection attempt failed */
2366 write_lock_irqsave (&ksocknal_data.ksnd_global_lock, flags);
2368 peer = route->ksnr_peer;
2369 route->ksnr_connecting = 0;
2371 /* This is a retry rather than a new connection */
2372 LASSERT (route->ksnr_retry_interval != 0);
2373 route->ksnr_timeout = jiffies + route->ksnr_retry_interval;
2374 route->ksnr_retry_interval = MIN (route->ksnr_retry_interval * 2,
2375 SOCKNAL_MAX_RECONNECT_INTERVAL);
2377 if (!list_empty (&peer->ksnp_tx_queue) &&
2378 ksocknal_find_connecting_route_locked (peer) == NULL) {
2379 LASSERT (list_empty (&peer->ksnp_conns));
2381 /* None of the connections that the blocked packets are
2382 * waiting for have been successful. Complete them now... */
2384 tx = list_entry (peer->ksnp_tx_queue.next,
2385 ksock_tx_t, tx_list);
2386 list_del (&tx->tx_list);
2387 list_add_tail (&tx->tx_list, &zombies);
2388 } while (!list_empty (&peer->ksnp_tx_queue));
2391 /* make this route least-favourite for re-selection */
2392 if (!route->ksnr_deleted) {
2393 list_del(&route->ksnr_list);
2394 list_add_tail(&route->ksnr_list, &peer->ksnp_routes);
2397 write_unlock_irqrestore (&ksocknal_data.ksnd_global_lock, flags);
2399 while (!list_empty (&zombies)) {
2400 char ipbuf[PTL_NALFMT_SIZE];
2401 char ipbuf2[PTL_NALFMT_SIZE];
2402 tx = list_entry (zombies.next, ksock_tx_t, tx_list);
2404 CERROR ("Deleting packet type %d len %d ("LPX64" %s->"LPX64" %s)\n",
2405 NTOH__u32 (tx->tx_hdr->type),
2406 NTOH__u32 (tx->tx_hdr->payload_length),
2407 NTOH__u64 (tx->tx_hdr->src_nid),
2408 portals_nid2str(SOCKNAL,
2409 NTOH__u64(tx->tx_hdr->src_nid),
2411 NTOH__u64 (tx->tx_hdr->dest_nid),
2412 portals_nid2str(SOCKNAL,
2413 NTOH__u64(tx->tx_hdr->src_nid),
2416 list_del (&tx->tx_list);
2418 ksocknal_tx_done (tx, 0);
2423 ksocknal_autoconnectd (void *arg)
2425 long id = (long)arg;
2427 unsigned long flags;
2428 ksock_route_t *route;
2431 snprintf (name, sizeof (name), "ksocknal_ad%02ld", id);
2432 kportal_daemonize (name);
2433 kportal_blockallsigs ();
2435 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2437 while (!ksocknal_data.ksnd_shuttingdown) {
2439 if (!list_empty (&ksocknal_data.ksnd_autoconnectd_routes)) {
2440 route = list_entry (ksocknal_data.ksnd_autoconnectd_routes.next,
2441 ksock_route_t, ksnr_connect_list);
2443 list_del (&route->ksnr_connect_list);
2444 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2446 ksocknal_autoconnect (route);
2447 ksocknal_put_route (route);
2449 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2453 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2455 rc = wait_event_interruptible (ksocknal_data.ksnd_autoconnectd_waitq,
2456 ksocknal_data.ksnd_shuttingdown ||
2457 !list_empty (&ksocknal_data.ksnd_autoconnectd_routes));
2459 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2462 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2464 ksocknal_thread_fini ();
2469 ksocknal_find_timed_out_conn (ksock_peer_t *peer)
2471 /* We're called with a shared lock on ksnd_global_lock */
2473 struct list_head *ctmp;
2474 ksock_sched_t *sched;
2476 list_for_each (ctmp, &peer->ksnp_conns) {
2477 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
2478 sched = conn->ksnc_scheduler;
2480 /* Don't need the {get,put}connsock dance to deref ksnc_sock... */
2481 LASSERT (!conn->ksnc_closing);
2483 if (conn->ksnc_rx_started &&
2484 time_after_eq (jiffies, conn->ksnc_rx_deadline)) {
2485 /* Timed out incomplete incoming message */
2486 atomic_inc (&conn->ksnc_refcount);
2487 CERROR ("Timed out RX from "LPX64" %p %d.%d.%d.%d\n",
2488 peer->ksnp_nid, conn, HIPQUAD(conn->ksnc_ipaddr));
2492 if ((!list_empty (&conn->ksnc_tx_queue) ||
2493 conn->ksnc_sock->sk->sk_wmem_queued != 0) &&
2494 time_after_eq (jiffies, conn->ksnc_tx_deadline)) {
2495 /* Timed out messages queued for sending, or
2496 * messages buffered in the socket's send buffer */
2497 atomic_inc (&conn->ksnc_refcount);
2498 CERROR ("Timed out TX to "LPX64" %s%d %p %d.%d.%d.%d\n",
2500 list_empty (&conn->ksnc_tx_queue) ? "" : "Q ",
2501 conn->ksnc_sock->sk->sk_wmem_queued, conn,
2502 HIPQUAD(conn->ksnc_ipaddr));
2511 ksocknal_check_peer_timeouts (int idx)
2513 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2514 struct list_head *ptmp;
2519 /* NB. We expect to have a look at all the peers and not find any
2520 * connections to time out, so we just use a shared lock while we
2522 read_lock (&ksocknal_data.ksnd_global_lock);
2524 list_for_each (ptmp, peers) {
2525 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
2526 conn = ksocknal_find_timed_out_conn (peer);
2529 read_unlock (&ksocknal_data.ksnd_global_lock);
2531 CERROR ("Timeout out conn->"LPX64" ip %d.%d.%d.%d:%d\n",
2533 HIPQUAD(conn->ksnc_ipaddr),
2535 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2537 /* NB we won't find this one again, but we can't
2538 * just proceed with the next peer, since we dropped
2539 * ksnd_global_lock and it might be dead already! */
2540 ksocknal_put_conn (conn);
2545 read_unlock (&ksocknal_data.ksnd_global_lock);
2549 ksocknal_reaper (void *arg)
2552 unsigned long flags;
2554 ksock_sched_t *sched;
2555 struct list_head enomem_conns;
2560 unsigned long deadline = jiffies;
2562 kportal_daemonize ("ksocknal_reaper");
2563 kportal_blockallsigs ();
2565 INIT_LIST_HEAD(&enomem_conns);
2566 init_waitqueue_entry (&wait, current);
2568 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2570 while (!ksocknal_data.ksnd_shuttingdown) {
2572 if (!list_empty (&ksocknal_data.ksnd_deathrow_conns)) {
2573 conn = list_entry (ksocknal_data.ksnd_deathrow_conns.next,
2574 ksock_conn_t, ksnc_list);
2575 list_del (&conn->ksnc_list);
2577 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2579 ksocknal_terminate_conn (conn);
2580 ksocknal_put_conn (conn);
2582 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2586 if (!list_empty (&ksocknal_data.ksnd_zombie_conns)) {
2587 conn = list_entry (ksocknal_data.ksnd_zombie_conns.next,
2588 ksock_conn_t, ksnc_list);
2589 list_del (&conn->ksnc_list);
2591 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2593 ksocknal_destroy_conn (conn);
2595 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2599 if (!list_empty (&ksocknal_data.ksnd_enomem_conns)) {
2600 list_add(&enomem_conns, &ksocknal_data.ksnd_enomem_conns);
2601 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2604 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2606 /* reschedule all the connections that stalled with ENOMEM... */
2608 while (!list_empty (&enomem_conns)) {
2609 conn = list_entry (enomem_conns.next,
2610 ksock_conn_t, ksnc_tx_list);
2611 list_del (&conn->ksnc_tx_list);
2613 sched = conn->ksnc_scheduler;
2615 spin_lock_irqsave (&sched->kss_lock, flags);
2617 LASSERT (conn->ksnc_tx_scheduled);
2618 conn->ksnc_tx_ready = 1;
2619 list_add_tail (&conn->ksnc_tx_list, &sched->kss_tx_conns);
2620 wake_up (&sched->kss_waitq);
2622 spin_unlock_irqrestore (&sched->kss_lock, flags);
2626 /* careful with the jiffy wrap... */
2627 while ((timeout = (int)(deadline - jiffies)) <= 0) {
2630 int chunk = ksocknal_data.ksnd_peer_hash_size;
2632 /* Time to check for timeouts on a few more peers: I do
2633 * checks every 'p' seconds on a proportion of the peer
2634 * table and I need to check every connection 'n' times
2635 * within a timeout interval, to ensure I detect a
2636 * timeout on any connection within (n+1)/n times the
2637 * timeout interval. */
2639 if (ksocknal_data.ksnd_io_timeout > n * p)
2640 chunk = (chunk * n * p) /
2641 ksocknal_data.ksnd_io_timeout;
2645 for (i = 0; i < chunk; i++) {
2646 ksocknal_check_peer_timeouts (peer_index);
2647 peer_index = (peer_index + 1) %
2648 ksocknal_data.ksnd_peer_hash_size;
2654 if (nenomem_conns != 0) {
2655 /* Reduce my timeout if I rescheduled ENOMEM conns.
2656 * This also prevents me getting woken immediately
2657 * if any go back on my enomem list. */
2658 timeout = SOCKNAL_ENOMEM_RETRY;
2660 ksocknal_data.ksnd_reaper_waketime = jiffies + timeout;
2662 add_wait_queue (&ksocknal_data.ksnd_reaper_waitq, &wait);
2663 set_current_state (TASK_INTERRUPTIBLE);
2665 if (!ksocknal_data.ksnd_shuttingdown &&
2666 list_empty (&ksocknal_data.ksnd_deathrow_conns) &&
2667 list_empty (&ksocknal_data.ksnd_zombie_conns))
2668 schedule_timeout (timeout);
2670 set_current_state (TASK_RUNNING);
2671 remove_wait_queue (&ksocknal_data.ksnd_reaper_waitq, &wait);
2673 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2676 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2678 ksocknal_thread_fini ();
2682 nal_cb_t ksocknal_lib = {
2683 nal_data: &ksocknal_data, /* NAL private data */
2684 cb_send: ksocknal_send,
2685 cb_send_pages: ksocknal_send_pages,
2686 cb_recv: ksocknal_recv,
2687 cb_recv_pages: ksocknal_recv_pages,
2688 cb_read: ksocknal_read,
2689 cb_write: ksocknal_write,
2690 cb_malloc: ksocknal_malloc,
2691 cb_free: ksocknal_free,
2692 cb_printf: ksocknal_printf,
2693 cb_cli: ksocknal_cli,
2694 cb_sti: ksocknal_sti,
2695 cb_dist: ksocknal_dist