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_callback (nal_cb_t * nal, void *private, lib_eq_t *eq,
58 CDEBUG(D_NET, LPX64": callback eq %p ev %p\n",
61 if (eq->event_callback != NULL)
62 eq->event_callback(ev);
68 ksocknal_malloc(nal_cb_t *nal, size_t len)
72 PORTAL_ALLOC(buf, len);
81 ksocknal_free(nal_cb_t *nal, void *buf, size_t len)
83 PORTAL_FREE(buf, len);
87 ksocknal_printf(nal_cb_t *nal, const char *fmt, ...)
93 vsnprintf (msg, sizeof (msg), fmt, ap); /* sprint safely */
96 msg[sizeof (msg) - 1] = 0; /* ensure terminated */
98 CDEBUG (D_NET, "%s", msg);
102 ksocknal_cli(nal_cb_t *nal, unsigned long *flags)
104 ksock_nal_data_t *data = nal->nal_data;
106 spin_lock(&data->ksnd_nal_cb_lock);
110 ksocknal_sti(nal_cb_t *nal, unsigned long *flags)
112 ksock_nal_data_t *data;
113 data = nal->nal_data;
115 spin_unlock(&data->ksnd_nal_cb_lock);
119 ksocknal_dist(nal_cb_t *nal, ptl_nid_t nid, unsigned long *dist)
121 /* I would guess that if ksocknal_get_peer (nid) == NULL,
122 and we're not routing, then 'nid' is very distant :) */
123 if ( nal->ni.nid == nid ) {
133 ksocknal_free_ltx (ksock_ltx_t *ltx)
135 atomic_dec(&ksocknal_data.ksnd_nactive_ltxs);
136 PORTAL_FREE(ltx, ltx->ltx_desc_size);
141 ksocknal_kvaddr_to_page (unsigned long vaddr)
145 if (vaddr >= VMALLOC_START &&
147 page = vmalloc_to_page ((void *)vaddr);
149 else if (vaddr >= PKMAP_BASE &&
150 vaddr < (PKMAP_BASE + LAST_PKMAP * PAGE_SIZE))
151 page = vmalloc_to_page ((void *)vaddr);
152 /* in 2.4 ^ just walks the page tables */
155 page = virt_to_page (vaddr);
166 ksocknal_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
168 struct socket *sock = conn->ksnc_sock;
169 struct iovec *iov = tx->tx_iov;
170 int fragsize = iov->iov_len;
171 unsigned long vaddr = (unsigned long)iov->iov_base;
172 int more = (tx->tx_niov > 1) ||
173 (tx->tx_nkiov > 0) ||
174 (!list_empty (&conn->ksnc_tx_queue));
176 int offset = vaddr & (PAGE_SIZE - 1);
177 int zcsize = MIN (fragsize, PAGE_SIZE - offset);
182 /* NB we can't trust socket ops to either consume our iovs
183 * or leave them alone, so we only send 1 frag at a time. */
184 LASSERT (fragsize <= tx->tx_resid);
185 LASSERT (tx->tx_niov > 0);
188 if (zcsize >= ksocknal_data.ksnd_zc_min_frag &&
189 (sock->sk->route_caps & NETIF_F_SG) &&
190 (sock->sk->route_caps & (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM)) &&
191 (page = ksocknal_kvaddr_to_page (vaddr)) != NULL) {
193 CDEBUG(D_NET, "vaddr %p, page %p->%p + offset %x for %d\n",
194 (void *)vaddr, page, page_address(page), offset, zcsize);
196 if (fragsize > zcsize) {
201 rc = tcp_sendpage_zccd(sock, page, offset, zcsize,
202 more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT,
207 /* NB don't pass tx's iov; sendmsg may or may not update it */
208 struct iovec fragiov = { .iov_base = (void *)vaddr,
209 .iov_len = fragsize};
210 struct msghdr msg = {
217 .msg_flags = more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT
219 mm_segment_t oldmm = get_fs();
222 rc = sock_sendmsg(sock, &msg, fragsize);
229 if (rc < iov->iov_len) {
230 /* didn't send whole iov entry... */
231 iov->iov_base = (void *)(vaddr + rc);
243 ksocknal_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
245 struct socket *sock = conn->ksnc_sock;
246 ptl_kiov_t *kiov = tx->tx_kiov;
247 int fragsize = kiov->kiov_len;
248 struct page *page = kiov->kiov_page;
249 int offset = kiov->kiov_offset;
250 int more = (tx->tx_nkiov > 1) ||
251 (!list_empty (&conn->ksnc_tx_queue));
254 /* NB we can't trust socket ops to either consume our iovs
255 * or leave them alone, so we only send 1 frag at a time. */
256 LASSERT (fragsize <= tx->tx_resid);
257 LASSERT (offset + fragsize <= PAGE_SIZE);
258 LASSERT (tx->tx_niov == 0);
259 LASSERT (tx->tx_nkiov > 0);
262 if (fragsize >= ksocknal_data.ksnd_zc_min_frag &&
263 (sock->sk->route_caps & NETIF_F_SG) &&
264 (sock->sk->route_caps & (NETIF_F_IP_CSUM | NETIF_F_NO_CSUM | NETIF_F_HW_CSUM))) {
266 CDEBUG(D_NET, "page %p + offset %x for %d\n",
267 page, offset, fragsize);
269 rc = tcp_sendpage_zccd(sock, page, offset, fragsize,
270 more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT,
275 char *addr = ((char *)kmap (page)) + offset;
276 struct iovec fragiov = {.iov_base = addr,
277 .iov_len = fragsize};
278 struct msghdr msg = {
285 .msg_flags = more ? (MSG_DONTWAIT | MSG_MORE) : MSG_DONTWAIT
287 mm_segment_t oldmm = get_fs();
290 rc = sock_sendmsg(sock, &msg, fragsize);
300 kiov->kiov_offset = offset + rc;
301 kiov->kiov_len = fragsize - rc;
312 ksocknal_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
316 if (ksocknal_data.ksnd_stall_tx != 0) {
317 set_current_state (TASK_UNINTERRUPTIBLE);
318 schedule_timeout (ksocknal_data.ksnd_stall_tx * HZ);
321 LASSERT (tx->tx_resid != 0);
323 rc = ksocknal_getconnsock (conn);
325 LASSERT (conn->ksnc_closing);
330 if (ksocknal_data.ksnd_enomem_tx > 0) {
332 ksocknal_data.ksnd_enomem_tx--;
334 } else if (tx->tx_niov != 0) {
335 rc = ksocknal_send_iov (conn, tx);
337 rc = ksocknal_send_kiov (conn, tx);
341 /* Didn't write anything.
343 * NB: rc == 0 and rc == -EAGAIN both mean try
344 * again later (linux stack returns -EAGAIN for
345 * this, but Adaptech TOE returns 0).
347 * Also, sends never fail with -ENOMEM, just
348 * -EAGAIN, but with the added bonus that we can't
349 * expect write_space() to call us back to tell us
350 * when to try sending again. We use the
351 * SOCK_NOSPACE flag to diagnose... */
353 LASSERT(rc != -ENOMEM);
355 if (rc == 0 || rc == -EAGAIN) {
356 if (test_bit(SOCK_NOSPACE,
357 &conn->ksnc_sock->flags)) {
363 if ((counter & (-counter)) == counter)
364 CWARN("%d ENOMEM tx %p\n",
374 /* Consider the connection alive since we managed to chuck
375 * more data into it. Really, we'd like to consider it
376 * alive only when the peer ACKs something, but
377 * write_space() only gets called back while SOCK_NOSPACE
378 * is set. Instead, we presume peer death has occurred if
379 * the socket doesn't drain within a timout */
380 conn->ksnc_tx_deadline = jiffies +
381 ksocknal_data.ksnd_io_timeout * HZ;
382 conn->ksnc_peer->ksnp_last_alive = jiffies;
384 } while (tx->tx_resid != 0);
386 ksocknal_putconnsock (conn);
391 ksocknal_eager_ack (ksock_conn_t *conn)
394 mm_segment_t oldmm = get_fs();
395 struct socket *sock = conn->ksnc_sock;
397 /* Remind the socket to ACK eagerly. If I don't, the socket might
398 * think I'm about to send something it could piggy-back the ACK
399 * on, introducing delay in completing zero-copy sends in my
403 sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
404 (char *)&opt, sizeof (opt));
409 ksocknal_recv_iov (ksock_conn_t *conn)
411 struct iovec *iov = conn->ksnc_rx_iov;
412 int fragsize = iov->iov_len;
413 unsigned long vaddr = (unsigned long)iov->iov_base;
414 struct iovec fragiov = { .iov_base = (void *)vaddr,
415 .iov_len = fragsize};
416 struct msghdr msg = {
425 mm_segment_t oldmm = get_fs();
428 /* NB we can't trust socket ops to either consume our iovs
429 * or leave them alone, so we only receive 1 frag at a time. */
430 LASSERT (conn->ksnc_rx_niov > 0);
431 LASSERT (fragsize <= conn->ksnc_rx_nob_wanted);
434 rc = sock_recvmsg (conn->ksnc_sock, &msg, fragsize, MSG_DONTWAIT);
435 /* NB this is just a boolean............................^ */
441 /* received something... */
442 conn->ksnc_peer->ksnp_last_alive = jiffies;
443 conn->ksnc_rx_deadline = jiffies +
444 ksocknal_data.ksnd_io_timeout * HZ;
445 mb(); /* order with setting rx_started */
446 conn->ksnc_rx_started = 1;
448 conn->ksnc_rx_nob_wanted -= rc;
449 conn->ksnc_rx_nob_left -= rc;
452 iov->iov_base = (void *)(vaddr + rc);
453 iov->iov_len = fragsize - rc;
458 conn->ksnc_rx_niov--;
463 ksocknal_recv_kiov (ksock_conn_t *conn)
465 ptl_kiov_t *kiov = conn->ksnc_rx_kiov;
466 struct page *page = kiov->kiov_page;
467 int offset = kiov->kiov_offset;
468 int fragsize = kiov->kiov_len;
469 unsigned long vaddr = ((unsigned long)kmap (page)) + offset;
470 struct iovec fragiov = { .iov_base = (void *)vaddr,
471 .iov_len = fragsize};
472 struct msghdr msg = {
481 mm_segment_t oldmm = get_fs();
484 /* NB we can't trust socket ops to either consume our iovs
485 * or leave them alone, so we only receive 1 frag at a time. */
486 LASSERT (fragsize <= conn->ksnc_rx_nob_wanted);
487 LASSERT (conn->ksnc_rx_nkiov > 0);
488 LASSERT (offset + fragsize <= PAGE_SIZE);
491 rc = sock_recvmsg (conn->ksnc_sock, &msg, fragsize, MSG_DONTWAIT);
492 /* NB this is just a boolean............................^ */
500 /* received something... */
501 conn->ksnc_peer->ksnp_last_alive = jiffies;
502 conn->ksnc_rx_deadline = jiffies +
503 ksocknal_data.ksnd_io_timeout * HZ;
504 mb(); /* order with setting rx_started */
505 conn->ksnc_rx_started = 1;
507 conn->ksnc_rx_nob_wanted -= rc;
508 conn->ksnc_rx_nob_left -= rc;
511 kiov->kiov_offset = offset + rc;
512 kiov->kiov_len = fragsize - rc;
516 conn->ksnc_rx_kiov++;
517 conn->ksnc_rx_nkiov--;
522 ksocknal_receive (ksock_conn_t *conn)
524 /* Return 1 on success, 0 on EOF, < 0 on error.
525 * Caller checks ksnc_rx_nob_wanted to determine
526 * progress/completion. */
530 if (ksocknal_data.ksnd_stall_rx != 0) {
531 set_current_state (TASK_UNINTERRUPTIBLE);
532 schedule_timeout (ksocknal_data.ksnd_stall_rx * HZ);
535 rc = ksocknal_getconnsock (conn);
537 LASSERT (conn->ksnc_closing);
542 if (conn->ksnc_rx_niov != 0)
543 rc = ksocknal_recv_iov (conn);
545 rc = ksocknal_recv_kiov (conn);
548 /* error/EOF or partial receive */
551 } else if (rc == 0 && conn->ksnc_rx_started) {
552 /* EOF in the middle of a message */
558 /* Completed a fragment */
560 if (conn->ksnc_rx_nob_wanted == 0) {
561 /* Completed a message segment (header or payload) */
562 if ((ksocknal_data.ksnd_eager_ack & conn->ksnc_type) != 0 &&
563 (conn->ksnc_rx_state == SOCKNAL_RX_BODY ||
564 conn->ksnc_rx_state == SOCKNAL_RX_BODY_FWD)) {
565 /* Remind the socket to ack eagerly... */
566 ksocknal_eager_ack(conn);
573 ksocknal_putconnsock (conn);
579 ksocknal_zc_callback (zccd_t *zcd)
581 ksock_tx_t *tx = KSOCK_ZCCD_2_TX(zcd);
582 ksock_sched_t *sched = tx->tx_conn->ksnc_scheduler;
586 /* Schedule tx for cleanup (can't do it now due to lock conflicts) */
588 spin_lock_irqsave (&sched->kss_lock, flags);
590 list_add_tail (&tx->tx_list, &sched->kss_zctxdone_list);
591 wake_up (&sched->kss_waitq);
593 spin_unlock_irqrestore (&sched->kss_lock, flags);
599 ksocknal_tx_done (ksock_tx_t *tx, int asynch)
604 if (tx->tx_conn != NULL) {
605 /* This tx got queued on a conn; do the accounting... */
606 atomic_sub (tx->tx_nob, &tx->tx_conn->ksnc_tx_nob);
608 /* zero copy completion isn't always from
609 * process_transmit() so it needs to keep a ref on
612 ksocknal_put_conn (tx->tx_conn);
618 if (tx->tx_isfwd) { /* was a forwarded packet? */
619 kpr_fwd_done (&ksocknal_data.ksnd_router,
620 KSOCK_TX_2_KPR_FWD_DESC (tx), 0);
626 ltx = KSOCK_TX_2_KSOCK_LTX (tx);
628 lib_finalize (&ksocknal_lib, ltx->ltx_private, ltx->ltx_cookie);
630 ksocknal_free_ltx (ltx);
635 ksocknal_tx_launched (ksock_tx_t *tx)
638 if (atomic_read (&tx->tx_zccd.zccd_count) != 1) {
639 ksock_conn_t *conn = tx->tx_conn;
641 /* zccd skbufs are still in-flight. First take a ref on
642 * conn, so it hangs about for ksocknal_tx_done... */
643 atomic_inc (&conn->ksnc_refcount);
645 /* ...then drop the initial ref on zccd, so the zero copy
646 * callback can occur */
647 zccd_put (&tx->tx_zccd);
651 /* Any zero-copy-ness (if any) has completed; I can complete the
652 * transmit now, avoiding an extra schedule */
653 ksocknal_tx_done (tx, 0);
657 ksocknal_process_transmit (ksock_conn_t *conn, ksock_tx_t *tx)
662 rc = ksocknal_transmit (conn, tx);
664 CDEBUG (D_NET, "send(%d) %d\n", tx->tx_resid, rc);
666 if (tx->tx_resid == 0) {
667 /* Sent everything OK */
670 ksocknal_tx_launched (tx);
678 /* Queue on ksnd_enomem_conns for retry after a timeout */
679 spin_lock_irqsave(&ksocknal_data.ksnd_reaper_lock, flags);
681 /* enomem list takes over scheduler's ref... */
682 LASSERT (conn->ksnc_tx_scheduled);
683 list_add_tail(&conn->ksnc_tx_list,
684 &ksocknal_data.ksnd_enomem_conns);
685 if (!time_after_eq(jiffies + SOCKNAL_ENOMEM_RETRY,
686 ksocknal_data.ksnd_reaper_waketime))
687 wake_up (&ksocknal_data.ksnd_reaper_waitq);
689 spin_unlock_irqrestore(&ksocknal_data.ksnd_reaper_lock, flags);
696 if (!conn->ksnc_closing)
697 CERROR ("[%p] Error %d on write to "LPX64
698 " ip %08x:%d\n",conn, rc,
699 conn->ksnc_peer->ksnp_nid,
700 conn->ksnc_ipaddr, conn->ksnc_port);
702 ksocknal_close_conn_and_siblings (conn, rc);
703 ksocknal_tx_launched (tx);
709 ksocknal_launch_autoconnect_locked (ksock_route_t *route)
713 /* called holding write lock on ksnd_global_lock */
715 LASSERT (!route->ksnr_deleted);
716 LASSERT ((route->ksnr_connected & (1 << SOCKNAL_CONN_ANY)) == 0);
717 LASSERT ((route->ksnr_connected & KSNR_TYPED_ROUTES) != KSNR_TYPED_ROUTES);
718 LASSERT (!route->ksnr_connecting);
720 if (ksocknal_data.ksnd_typed_conns)
721 route->ksnr_connecting =
722 KSNR_TYPED_ROUTES & ~route->ksnr_connected;
724 route->ksnr_connecting = (1 << SOCKNAL_CONN_ANY);
726 atomic_inc (&route->ksnr_refcount); /* extra ref for asynchd */
728 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
730 list_add_tail (&route->ksnr_connect_list,
731 &ksocknal_data.ksnd_autoconnectd_routes);
732 wake_up (&ksocknal_data.ksnd_autoconnectd_waitq);
734 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
738 ksocknal_find_target_peer_locked (ksock_tx_t *tx, ptl_nid_t nid)
740 ptl_nid_t target_nid;
742 ksock_peer_t *peer = ksocknal_find_peer_locked (nid);
748 CERROR ("Can't send packet to "LPX64
749 ": routed target is not a peer\n", nid);
753 rc = kpr_lookup (&ksocknal_data.ksnd_router, nid, tx->tx_nob,
756 CERROR ("Can't route to "LPX64": router error %d\n", nid, rc);
760 peer = ksocknal_find_peer_locked (target_nid);
764 CERROR ("Can't send packet to "LPX64": no peer entry\n", target_nid);
769 ksocknal_find_conn_locked (ksock_tx_t *tx, ksock_peer_t *peer)
771 struct list_head *tmp;
772 ksock_conn_t *typed = NULL;
774 ksock_conn_t *fallback = NULL;
777 /* Find the conn with the shortest tx queue */
778 list_for_each (tmp, &peer->ksnp_conns) {
779 ksock_conn_t *c = list_entry(tmp, ksock_conn_t, ksnc_list);
780 int nob = atomic_read(&c->ksnc_tx_nob);
782 LASSERT (!c->ksnc_closing);
784 if (fallback == NULL || nob < fnob) {
789 if (!ksocknal_data.ksnd_typed_conns)
792 switch (c->ksnc_type) {
795 case SOCKNAL_CONN_ANY:
797 case SOCKNAL_CONN_BULK_IN:
799 case SOCKNAL_CONN_BULK_OUT:
800 if (tx->tx_nob < ksocknal_data.ksnd_min_bulk)
803 case SOCKNAL_CONN_CONTROL:
804 if (tx->tx_nob >= ksocknal_data.ksnd_min_bulk)
809 if (typed == NULL || nob < tnob) {
815 /* prefer the typed selection */
816 return ((typed != NULL) ? typed : fallback);
820 ksocknal_queue_tx_locked (ksock_tx_t *tx, ksock_conn_t *conn)
823 ksock_sched_t *sched = conn->ksnc_scheduler;
825 /* called holding global lock (read or irq-write) and caller may
826 * not have dropped this lock between finding conn and calling me,
827 * so we don't need the {get,put}connsock dance to deref
829 LASSERT(!conn->ksnc_closing);
830 LASSERT(tx->tx_resid == tx->tx_nob);
832 CDEBUG (D_NET, "Sending to "LPX64" on port %d\n",
833 conn->ksnc_peer->ksnp_nid, conn->ksnc_port);
835 atomic_add (tx->tx_nob, &conn->ksnc_tx_nob);
839 zccd_init (&tx->tx_zccd, ksocknal_zc_callback);
840 /* NB this sets 1 ref on zccd, so the callback can only occur after
841 * I've released this ref. */
843 spin_lock_irqsave (&sched->kss_lock, flags);
845 conn->ksnc_tx_deadline = jiffies +
846 ksocknal_data.ksnd_io_timeout * HZ;
847 mb(); /* order with list_add_tail */
849 list_add_tail (&tx->tx_list, &conn->ksnc_tx_queue);
851 if (conn->ksnc_tx_ready && /* able to send */
852 !conn->ksnc_tx_scheduled) { /* not scheduled to send */
853 /* +1 ref for scheduler */
854 atomic_inc (&conn->ksnc_refcount);
855 list_add_tail (&conn->ksnc_tx_list,
856 &sched->kss_tx_conns);
857 conn->ksnc_tx_scheduled = 1;
858 wake_up (&sched->kss_waitq);
861 spin_unlock_irqrestore (&sched->kss_lock, flags);
865 ksocknal_find_connectable_route_locked (ksock_peer_t *peer)
867 struct list_head *tmp;
868 ksock_route_t *route;
869 ksock_route_t *candidate = NULL;
873 list_for_each (tmp, &peer->ksnp_routes) {
874 route = list_entry (tmp, ksock_route_t, ksnr_list);
875 bits = route->ksnr_connected;
877 if ((bits & KSNR_TYPED_ROUTES) == KSNR_TYPED_ROUTES ||
878 (bits & (1 << SOCKNAL_CONN_ANY)) != 0 ||
879 route->ksnr_connecting != 0) {
880 /* All typed connections have been established, or
881 * an untyped connection has been established, or
882 * connections are currently being established */
887 /* too soon to retry this guy? */
888 if (!time_after_eq (jiffies, route->ksnr_timeout))
891 /* always do eager routes */
892 if (route->ksnr_eager)
895 if (candidate == NULL) {
896 /* If we don't find any other route that is fully
897 * connected or connecting, the first connectable
898 * route is returned. If it fails to connect, it
899 * will get placed at the end of the list */
904 return (found ? NULL : candidate);
908 ksocknal_find_connecting_route_locked (ksock_peer_t *peer)
910 struct list_head *tmp;
911 ksock_route_t *route;
913 list_for_each (tmp, &peer->ksnp_routes) {
914 route = list_entry (tmp, ksock_route_t, ksnr_list);
916 if (route->ksnr_connecting != 0)
924 ksocknal_launch_packet (ksock_tx_t *tx, ptl_nid_t nid)
929 ksock_route_t *route;
932 /* Ensure the frags we've been given EXACTLY match the number of
933 * bytes we want to send. Many TCP/IP stacks disregard any total
934 * size parameters passed to them and just look at the frags.
936 * We always expect at least 1 mapped fragment containing the
937 * complete portals header. */
938 LASSERT (lib_iov_nob (tx->tx_niov, tx->tx_iov) +
939 lib_kiov_nob (tx->tx_nkiov, tx->tx_kiov) == tx->tx_nob);
940 LASSERT (tx->tx_niov >= 1);
941 LASSERT (tx->tx_iov[0].iov_len >= sizeof (ptl_hdr_t));
943 CDEBUG (D_NET, "packet %p type %d, nob %d niov %d nkiov %d\n",
944 tx, ((ptl_hdr_t *)tx->tx_iov[0].iov_base)->type,
945 tx->tx_nob, tx->tx_niov, tx->tx_nkiov);
947 tx->tx_conn = NULL; /* only set when assigned a conn */
948 tx->tx_resid = tx->tx_nob;
949 tx->tx_hdr = (ptl_hdr_t *)tx->tx_iov[0].iov_base;
951 g_lock = &ksocknal_data.ksnd_global_lock;
954 peer = ksocknal_find_target_peer_locked (tx, nid);
956 read_unlock (g_lock);
957 return (-EHOSTUNREACH);
960 if (ksocknal_find_connectable_route_locked(peer) == NULL) {
961 conn = ksocknal_find_conn_locked (tx, peer);
963 /* I've got no autoconnect routes that need to be
964 * connecting and I do have an actual connection... */
965 ksocknal_queue_tx_locked (tx, conn);
966 read_unlock (g_lock);
971 /* Making one or more connections; I'll need a write lock... */
973 atomic_inc (&peer->ksnp_refcount); /* +1 ref for me while I unlock */
974 read_unlock (g_lock);
975 write_lock_irqsave (g_lock, flags);
977 if (peer->ksnp_closing) { /* peer deleted as I blocked! */
978 write_unlock_irqrestore (g_lock, flags);
979 ksocknal_put_peer (peer);
980 return (-EHOSTUNREACH);
982 ksocknal_put_peer (peer); /* drop ref I got above */
985 /* launch any/all autoconnections that need it */
986 route = ksocknal_find_connectable_route_locked (peer);
990 ksocknal_launch_autoconnect_locked (route);
993 conn = ksocknal_find_conn_locked (tx, peer);
995 /* Connection exists; queue message on it */
996 ksocknal_queue_tx_locked (tx, conn);
997 write_unlock_irqrestore (g_lock, flags);
1001 route = ksocknal_find_connecting_route_locked (peer);
1002 if (route != NULL) {
1003 /* At least 1 connection is being established; queue the
1005 list_add_tail (&tx->tx_list, &peer->ksnp_tx_queue);
1006 write_unlock_irqrestore (g_lock, flags);
1010 write_unlock_irqrestore (g_lock, flags);
1011 return (-EHOSTUNREACH);
1015 ksocknal_sendmsg(nal_cb_t *nal,
1022 unsigned int payload_niov,
1023 struct iovec *payload_iov,
1024 ptl_kiov_t *payload_kiov,
1031 /* NB 'private' is different depending on what we're sending.
1032 * Just ignore it... */
1034 CDEBUG(D_NET, "sending "LPSZ" bytes in %d frags to nid:"LPX64
1035 " pid %d\n", payload_nob, payload_niov, nid , pid);
1037 LASSERT (payload_nob == 0 || payload_niov > 0);
1038 LASSERT (payload_niov <= PTL_MD_MAX_IOV);
1040 /* It must be OK to kmap() if required */
1041 LASSERT (payload_kiov == NULL || !in_interrupt ());
1042 /* payload is either all vaddrs or all pages */
1043 LASSERT (!(payload_kiov != NULL && payload_iov != NULL));
1045 if (payload_iov != NULL)
1046 desc_size = offsetof(ksock_ltx_t, ltx_iov[1 + payload_niov]);
1048 desc_size = offsetof(ksock_ltx_t, ltx_kiov[payload_niov]);
1050 if (in_interrupt() ||
1051 type == PTL_MSG_ACK ||
1052 type == PTL_MSG_REPLY) {
1053 /* Can't block if in interrupt or responding to an incoming
1055 PORTAL_ALLOC_ATOMIC(ltx, desc_size);
1057 PORTAL_ALLOC(ltx, desc_size);
1061 CERROR("Can't allocate tx desc type %d size %d %s\n",
1062 type, desc_size, in_interrupt() ? "(intr)" : "");
1063 return (PTL_NOSPACE);
1066 atomic_inc(&ksocknal_data.ksnd_nactive_ltxs);
1068 ltx->ltx_desc_size = desc_size;
1070 /* We always have 1 mapped frag for the header */
1071 ltx->ltx_tx.tx_iov = ltx->ltx_iov;
1072 ltx->ltx_iov[0].iov_base = <x->ltx_hdr;
1073 ltx->ltx_iov[0].iov_len = sizeof(*hdr);
1074 ltx->ltx_hdr = *hdr;
1076 ltx->ltx_private = private;
1077 ltx->ltx_cookie = cookie;
1079 ltx->ltx_tx.tx_isfwd = 0;
1080 ltx->ltx_tx.tx_nob = sizeof (*hdr) + payload_nob;
1082 if (payload_iov != NULL) {
1083 /* payload is all mapped */
1084 ltx->ltx_tx.tx_kiov = NULL;
1085 ltx->ltx_tx.tx_nkiov = 0;
1087 ltx->ltx_tx.tx_niov = 1 + payload_niov;
1089 memcpy(ltx->ltx_iov + 1, payload_iov,
1090 payload_niov * sizeof (*payload_iov));
1093 /* payload is all pages */
1094 ltx->ltx_tx.tx_kiov = ltx->ltx_kiov;
1095 ltx->ltx_tx.tx_nkiov = payload_niov;
1097 ltx->ltx_tx.tx_niov = 1;
1099 memcpy(ltx->ltx_kiov, payload_kiov,
1100 payload_niov * sizeof (*payload_kiov));
1103 rc = ksocknal_launch_packet(<x->ltx_tx, nid);
1107 ksocknal_free_ltx(ltx);
1112 ksocknal_send (nal_cb_t *nal, void *private, lib_msg_t *cookie,
1113 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
1114 unsigned int payload_niov, struct iovec *payload_iov,
1117 return (ksocknal_sendmsg(nal, private, cookie,
1118 hdr, type, nid, pid,
1119 payload_niov, payload_iov, NULL,
1124 ksocknal_send_pages (nal_cb_t *nal, void *private, lib_msg_t *cookie,
1125 ptl_hdr_t *hdr, int type, ptl_nid_t nid, ptl_pid_t pid,
1126 unsigned int payload_niov, ptl_kiov_t *payload_kiov,
1129 return (ksocknal_sendmsg(nal, private, cookie,
1130 hdr, type, nid, pid,
1131 payload_niov, NULL, payload_kiov,
1136 ksocknal_fwd_packet (void *arg, kpr_fwd_desc_t *fwd)
1138 ptl_nid_t nid = fwd->kprfd_gateway_nid;
1139 ksock_tx_t *tx = (ksock_tx_t *)&fwd->kprfd_scratch;
1142 CDEBUG (D_NET, "Forwarding [%p] -> "LPX64" ("LPX64"))\n", fwd,
1143 fwd->kprfd_gateway_nid, fwd->kprfd_target_nid);
1145 /* I'm the gateway; must be the last hop */
1146 if (nid == ksocknal_lib.ni.nid)
1147 nid = fwd->kprfd_target_nid;
1149 tx->tx_isfwd = 1; /* This is a forwarding packet */
1150 tx->tx_nob = fwd->kprfd_nob;
1151 tx->tx_niov = fwd->kprfd_niov;
1152 tx->tx_iov = fwd->kprfd_iov;
1156 rc = ksocknal_launch_packet (tx, nid);
1158 kpr_fwd_done (&ksocknal_data.ksnd_router, fwd, rc);
1162 ksocknal_thread_start (int (*fn)(void *arg), void *arg)
1164 long pid = kernel_thread (fn, arg, 0);
1169 atomic_inc (&ksocknal_data.ksnd_nthreads);
1174 ksocknal_thread_fini (void)
1176 atomic_dec (&ksocknal_data.ksnd_nthreads);
1180 ksocknal_fmb_callback (void *arg, int error)
1182 ksock_fmb_t *fmb = (ksock_fmb_t *)arg;
1183 ksock_fmb_pool_t *fmp = fmb->fmb_pool;
1184 ptl_hdr_t *hdr = (ptl_hdr_t *) page_address(fmb->fmb_pages[0]);
1185 ksock_conn_t *conn = NULL;
1186 ksock_sched_t *sched;
1187 unsigned long flags;
1190 CERROR("Failed to route packet from "LPX64" to "LPX64": %d\n",
1191 NTOH__u64(hdr->src_nid), NTOH__u64(hdr->dest_nid),
1194 CDEBUG (D_NET, "routed packet from "LPX64" to "LPX64": OK\n",
1195 NTOH__u64 (hdr->src_nid), NTOH__u64 (hdr->dest_nid));
1197 /* drop peer ref taken on init */
1198 ksocknal_put_peer (fmb->fmb_peer);
1200 spin_lock_irqsave (&fmp->fmp_lock, flags);
1202 list_add (&fmb->fmb_list, &fmp->fmp_idle_fmbs);
1203 fmp->fmp_nactive_fmbs--;
1205 if (!list_empty (&fmp->fmp_blocked_conns)) {
1206 conn = list_entry (fmb->fmb_pool->fmp_blocked_conns.next,
1207 ksock_conn_t, ksnc_rx_list);
1208 list_del (&conn->ksnc_rx_list);
1211 spin_unlock_irqrestore (&fmp->fmp_lock, flags);
1216 CDEBUG (D_NET, "Scheduling conn %p\n", conn);
1217 LASSERT (conn->ksnc_rx_scheduled);
1218 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_FMB_SLEEP);
1220 conn->ksnc_rx_state = SOCKNAL_RX_GET_FMB;
1222 sched = conn->ksnc_scheduler;
1224 spin_lock_irqsave (&sched->kss_lock, flags);
1226 list_add_tail (&conn->ksnc_rx_list, &sched->kss_rx_conns);
1227 wake_up (&sched->kss_waitq);
1229 spin_unlock_irqrestore (&sched->kss_lock, flags);
1233 ksocknal_get_idle_fmb (ksock_conn_t *conn)
1235 int payload_nob = conn->ksnc_rx_nob_left;
1236 int packet_nob = sizeof (ptl_hdr_t) + payload_nob;
1237 unsigned long flags;
1238 ksock_fmb_pool_t *pool;
1241 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_GET_FMB);
1242 LASSERT (kpr_routing(&ksocknal_data.ksnd_router));
1244 if (packet_nob <= SOCKNAL_SMALL_FWD_PAGES * PAGE_SIZE)
1245 pool = &ksocknal_data.ksnd_small_fmp;
1247 pool = &ksocknal_data.ksnd_large_fmp;
1249 spin_lock_irqsave (&pool->fmp_lock, flags);
1251 if (!list_empty (&pool->fmp_idle_fmbs)) {
1252 fmb = list_entry(pool->fmp_idle_fmbs.next,
1253 ksock_fmb_t, fmb_list);
1254 list_del (&fmb->fmb_list);
1255 pool->fmp_nactive_fmbs++;
1256 spin_unlock_irqrestore (&pool->fmp_lock, flags);
1261 /* deschedule until fmb free */
1263 conn->ksnc_rx_state = SOCKNAL_RX_FMB_SLEEP;
1265 list_add_tail (&conn->ksnc_rx_list,
1266 &pool->fmp_blocked_conns);
1268 spin_unlock_irqrestore (&pool->fmp_lock, flags);
1273 ksocknal_init_fmb (ksock_conn_t *conn, ksock_fmb_t *fmb)
1275 int payload_nob = conn->ksnc_rx_nob_left;
1276 int packet_nob = sizeof (ptl_hdr_t) + payload_nob;
1277 ptl_nid_t dest_nid = NTOH__u64 (conn->ksnc_hdr.dest_nid);
1278 int niov; /* at least the header */
1281 LASSERT (conn->ksnc_rx_scheduled);
1282 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_GET_FMB);
1283 LASSERT (conn->ksnc_rx_nob_wanted == conn->ksnc_rx_nob_left);
1284 LASSERT (payload_nob >= 0);
1285 LASSERT (packet_nob <= fmb->fmb_npages * PAGE_SIZE);
1286 LASSERT (sizeof (ptl_hdr_t) < PAGE_SIZE);
1288 /* Got a forwarding buffer; copy the header we just read into the
1289 * forwarding buffer. If there's payload, start reading reading it
1290 * into the buffer, otherwise the forwarding buffer can be kicked
1293 * NB fmb->fmb_iov spans the WHOLE packet.
1294 * conn->ksnc_rx_iov spans just the payload.
1296 fmb->fmb_iov[0].iov_base = page_address (fmb->fmb_pages[0]);
1299 memcpy (fmb->fmb_iov[0].iov_base, &conn->ksnc_hdr, sizeof (ptl_hdr_t));
1301 /* Take a ref on the conn's peer to prevent module unload before
1302 * forwarding completes. NB we ref peer and not conn since because
1303 * all refs on conn after it has been closed must remove themselves
1305 fmb->fmb_peer = conn->ksnc_peer;
1306 atomic_inc (&conn->ksnc_peer->ksnp_refcount);
1308 if (payload_nob == 0) { /* got complete packet already */
1309 CDEBUG (D_NET, "%p "LPX64"->"LPX64" %d fwd_start (immediate)\n",
1310 conn, NTOH__u64 (conn->ksnc_hdr.src_nid),
1311 dest_nid, packet_nob);
1313 fmb->fmb_iov[0].iov_len = sizeof (ptl_hdr_t);
1315 kpr_fwd_init (&fmb->fmb_fwd, dest_nid,
1316 packet_nob, 1, fmb->fmb_iov,
1317 ksocknal_fmb_callback, fmb);
1319 /* forward it now */
1320 kpr_fwd_start (&ksocknal_data.ksnd_router, &fmb->fmb_fwd);
1322 ksocknal_new_packet (conn, 0); /* on to next packet */
1327 if (packet_nob <= PAGE_SIZE) { /* whole packet fits in first page */
1328 fmb->fmb_iov[0].iov_len = packet_nob;
1330 fmb->fmb_iov[0].iov_len = PAGE_SIZE;
1331 nob = packet_nob - PAGE_SIZE;
1334 LASSERT (niov < fmb->fmb_npages);
1335 fmb->fmb_iov[niov].iov_base =
1336 page_address (fmb->fmb_pages[niov]);
1337 fmb->fmb_iov[niov].iov_len = MIN (PAGE_SIZE, nob);
1343 kpr_fwd_init (&fmb->fmb_fwd, dest_nid,
1344 packet_nob, niov, fmb->fmb_iov,
1345 ksocknal_fmb_callback, fmb);
1347 conn->ksnc_cookie = fmb; /* stash fmb for later */
1348 conn->ksnc_rx_state = SOCKNAL_RX_BODY_FWD; /* read in the payload */
1350 /* payload is desc's iov-ed buffer, but skipping the hdr */
1351 LASSERT (niov <= sizeof (conn->ksnc_rx_iov_space) /
1352 sizeof (struct iovec));
1354 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1355 conn->ksnc_rx_iov[0].iov_base =
1356 (void *)(((unsigned long)fmb->fmb_iov[0].iov_base) +
1357 sizeof (ptl_hdr_t));
1358 conn->ksnc_rx_iov[0].iov_len =
1359 fmb->fmb_iov[0].iov_len - sizeof (ptl_hdr_t);
1362 memcpy(&conn->ksnc_rx_iov[1], &fmb->fmb_iov[1],
1363 (niov - 1) * sizeof (struct iovec));
1365 conn->ksnc_rx_niov = niov;
1367 CDEBUG (D_NET, "%p "LPX64"->"LPX64" %d reading body\n", conn,
1368 NTOH__u64 (conn->ksnc_hdr.src_nid), dest_nid, payload_nob);
1373 ksocknal_fwd_parse (ksock_conn_t *conn)
1376 ptl_nid_t dest_nid = NTOH__u64 (conn->ksnc_hdr.dest_nid);
1377 ptl_nid_t src_nid = NTOH__u64 (conn->ksnc_hdr.src_nid);
1378 int body_len = NTOH__u32 (conn->ksnc_hdr.payload_length);
1379 char str[PTL_NALFMT_SIZE];
1381 CDEBUG (D_NET, "%p "LPX64"->"LPX64" %d parsing header\n", conn,
1382 src_nid, dest_nid, conn->ksnc_rx_nob_left);
1384 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_HEADER);
1385 LASSERT (conn->ksnc_rx_scheduled);
1387 if (body_len < 0) { /* length corrupt (overflow) */
1388 CERROR("dropping packet from "LPX64" (%s) for "LPX64" (%s): "
1389 "packet size %d illegal\n",
1390 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1391 dest_nid, portals_nid2str(TCPNAL, dest_nid, str),
1394 ksocknal_new_packet (conn, 0); /* on to new packet */
1398 if (!kpr_routing(&ksocknal_data.ksnd_router)) { /* not forwarding */
1399 CERROR("dropping packet from "LPX64" (%s) for "LPX64
1400 " (%s): not forwarding\n",
1401 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1402 dest_nid, portals_nid2str(TCPNAL, dest_nid, str));
1403 /* on to new packet (skip this one's body) */
1404 ksocknal_new_packet (conn, body_len);
1408 if (body_len > PTL_MTU) { /* too big to forward */
1409 CERROR ("dropping packet from "LPX64" (%s) for "LPX64
1410 "(%s): packet size %d too big\n",
1411 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1412 dest_nid, portals_nid2str(TCPNAL, dest_nid, str),
1414 /* on to new packet (skip this one's body) */
1415 ksocknal_new_packet (conn, body_len);
1419 /* should have gone direct */
1420 peer = ksocknal_get_peer (conn->ksnc_hdr.dest_nid);
1422 CERROR ("dropping packet from "LPX64" (%s) for "LPX64
1423 "(%s): target is a peer\n",
1424 src_nid, portals_nid2str(TCPNAL, src_nid, str),
1425 dest_nid, portals_nid2str(TCPNAL, dest_nid, str));
1426 ksocknal_put_peer (peer); /* drop ref from get above */
1428 /* on to next packet (skip this one's body) */
1429 ksocknal_new_packet (conn, body_len);
1433 conn->ksnc_rx_state = SOCKNAL_RX_GET_FMB; /* Getting FMB now */
1434 conn->ksnc_rx_nob_left = body_len; /* stash packet size */
1435 conn->ksnc_rx_nob_wanted = body_len; /* (no slop) */
1439 ksocknal_new_packet (ksock_conn_t *conn, int nob_to_skip)
1441 static char ksocknal_slop_buffer[4096];
1447 if (nob_to_skip == 0) { /* right at next packet boundary now */
1448 conn->ksnc_rx_started = 0;
1449 mb (); /* racing with timeout thread */
1451 conn->ksnc_rx_state = SOCKNAL_RX_HEADER;
1452 conn->ksnc_rx_nob_wanted = sizeof (ptl_hdr_t);
1453 conn->ksnc_rx_nob_left = sizeof (ptl_hdr_t);
1455 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1456 conn->ksnc_rx_iov[0].iov_base = (char *)&conn->ksnc_hdr;
1457 conn->ksnc_rx_iov[0].iov_len = sizeof (ptl_hdr_t);
1458 conn->ksnc_rx_niov = 1;
1460 conn->ksnc_rx_kiov = NULL;
1461 conn->ksnc_rx_nkiov = 0;
1465 /* Set up to skip as much a possible now. If there's more left
1466 * (ran out of iov entries) we'll get called again */
1468 conn->ksnc_rx_state = SOCKNAL_RX_SLOP;
1469 conn->ksnc_rx_nob_left = nob_to_skip;
1470 conn->ksnc_rx_iov = (struct iovec *)&conn->ksnc_rx_iov_space;
1475 nob = MIN (nob_to_skip, sizeof (ksocknal_slop_buffer));
1477 conn->ksnc_rx_iov[niov].iov_base = ksocknal_slop_buffer;
1478 conn->ksnc_rx_iov[niov].iov_len = nob;
1483 } while (nob_to_skip != 0 && /* mustn't overflow conn's rx iov */
1484 niov < sizeof(conn->ksnc_rx_iov_space) / sizeof (struct iovec));
1486 conn->ksnc_rx_niov = niov;
1487 conn->ksnc_rx_kiov = NULL;
1488 conn->ksnc_rx_nkiov = 0;
1489 conn->ksnc_rx_nob_wanted = skipped;
1494 ksocknal_process_receive (ksock_conn_t *conn)
1499 LASSERT (atomic_read (&conn->ksnc_refcount) > 0);
1501 /* doesn't need a forwarding buffer */
1502 if (conn->ksnc_rx_state != SOCKNAL_RX_GET_FMB)
1506 fmb = ksocknal_get_idle_fmb (conn);
1508 /* conn descheduled waiting for idle fmb */
1512 if (ksocknal_init_fmb (conn, fmb)) {
1513 /* packet forwarded */
1518 /* NB: sched lock NOT held */
1519 LASSERT (conn->ksnc_rx_state == SOCKNAL_RX_HEADER ||
1520 conn->ksnc_rx_state == SOCKNAL_RX_BODY ||
1521 conn->ksnc_rx_state == SOCKNAL_RX_BODY_FWD ||
1522 conn->ksnc_rx_state == SOCKNAL_RX_SLOP);
1524 LASSERT (conn->ksnc_rx_nob_wanted > 0);
1526 rc = ksocknal_receive(conn);
1529 LASSERT (rc != -EAGAIN);
1532 CWARN ("[%p] EOF from "LPX64" ip %08x:%d\n",
1533 conn, conn->ksnc_peer->ksnp_nid,
1534 conn->ksnc_ipaddr, conn->ksnc_port);
1535 else if (!conn->ksnc_closing)
1536 CERROR ("[%p] Error %d on read from "LPX64" ip %08x:%d\n",
1537 conn, rc, conn->ksnc_peer->ksnp_nid,
1538 conn->ksnc_ipaddr, conn->ksnc_port);
1540 ksocknal_close_conn_and_siblings (conn, rc);
1541 return (rc == 0 ? -ESHUTDOWN : rc);
1544 if (conn->ksnc_rx_nob_wanted != 0) {
1549 switch (conn->ksnc_rx_state) {
1550 case SOCKNAL_RX_HEADER:
1551 if (conn->ksnc_hdr.type != HTON__u32(PTL_MSG_HELLO) &&
1552 NTOH__u64(conn->ksnc_hdr.dest_nid) != ksocknal_lib.ni.nid) {
1553 /* This packet isn't for me */
1554 ksocknal_fwd_parse (conn);
1555 switch (conn->ksnc_rx_state) {
1556 case SOCKNAL_RX_HEADER: /* skipped (zero payload) */
1557 return (0); /* => come back later */
1558 case SOCKNAL_RX_SLOP: /* skipping packet's body */
1559 goto try_read; /* => go read it */
1560 case SOCKNAL_RX_GET_FMB: /* forwarding */
1561 goto get_fmb; /* => go get a fwd msg buffer */
1568 /* sets wanted_len, iovs etc */
1569 lib_parse(&ksocknal_lib, &conn->ksnc_hdr, conn);
1571 if (conn->ksnc_rx_nob_wanted != 0) { /* need to get payload? */
1572 conn->ksnc_rx_state = SOCKNAL_RX_BODY;
1573 goto try_read; /* go read the payload */
1575 /* Fall through (completed packet for me) */
1577 case SOCKNAL_RX_BODY:
1578 /* payload all received */
1579 lib_finalize(&ksocknal_lib, NULL, conn->ksnc_cookie);
1582 case SOCKNAL_RX_SLOP:
1583 /* starting new packet? */
1584 if (ksocknal_new_packet (conn, conn->ksnc_rx_nob_left))
1585 return (0); /* come back later */
1586 goto try_read; /* try to finish reading slop now */
1588 case SOCKNAL_RX_BODY_FWD:
1589 /* payload all received */
1590 CDEBUG (D_NET, "%p "LPX64"->"LPX64" %d fwd_start (got body)\n",
1591 conn, NTOH__u64 (conn->ksnc_hdr.src_nid),
1592 NTOH__u64 (conn->ksnc_hdr.dest_nid),
1593 conn->ksnc_rx_nob_left);
1595 /* forward the packet. NB ksocknal_init_fmb() put fmb into
1596 * conn->ksnc_cookie */
1597 fmb = (ksock_fmb_t *)conn->ksnc_cookie;
1598 kpr_fwd_start (&ksocknal_data.ksnd_router, &fmb->fmb_fwd);
1600 /* no slop in forwarded packets */
1601 LASSERT (conn->ksnc_rx_nob_left == 0);
1603 ksocknal_new_packet (conn, 0); /* on to next packet */
1604 return (0); /* (later) */
1612 return (-EINVAL); /* keep gcc happy */
1616 ksocknal_recv (nal_cb_t *nal, void *private, lib_msg_t *msg,
1617 unsigned int niov, struct iovec *iov, size_t mlen, size_t rlen)
1619 ksock_conn_t *conn = (ksock_conn_t *)private;
1621 LASSERT (mlen <= rlen);
1622 LASSERT (niov <= PTL_MD_MAX_IOV);
1624 conn->ksnc_cookie = msg;
1625 conn->ksnc_rx_nob_wanted = mlen;
1626 conn->ksnc_rx_nob_left = rlen;
1628 conn->ksnc_rx_nkiov = 0;
1629 conn->ksnc_rx_kiov = NULL;
1630 conn->ksnc_rx_niov = niov;
1631 conn->ksnc_rx_iov = conn->ksnc_rx_iov_space.iov;
1632 memcpy (conn->ksnc_rx_iov, iov, niov * sizeof (*iov));
1635 lib_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1636 lib_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1642 ksocknal_recv_pages (nal_cb_t *nal, void *private, lib_msg_t *msg,
1643 unsigned int niov, ptl_kiov_t *kiov, size_t mlen, size_t rlen)
1645 ksock_conn_t *conn = (ksock_conn_t *)private;
1647 LASSERT (mlen <= rlen);
1648 LASSERT (niov <= PTL_MD_MAX_IOV);
1650 conn->ksnc_cookie = msg;
1651 conn->ksnc_rx_nob_wanted = mlen;
1652 conn->ksnc_rx_nob_left = rlen;
1654 conn->ksnc_rx_niov = 0;
1655 conn->ksnc_rx_iov = NULL;
1656 conn->ksnc_rx_nkiov = niov;
1657 conn->ksnc_rx_kiov = conn->ksnc_rx_iov_space.kiov;
1658 memcpy (conn->ksnc_rx_kiov, kiov, niov * sizeof (*kiov));
1661 lib_iov_nob (conn->ksnc_rx_niov, conn->ksnc_rx_iov) +
1662 lib_kiov_nob (conn->ksnc_rx_nkiov, conn->ksnc_rx_kiov));
1667 int ksocknal_scheduler (void *arg)
1669 ksock_sched_t *sched = (ksock_sched_t *)arg;
1672 unsigned long flags;
1675 int id = sched - ksocknal_data.ksnd_schedulers;
1678 snprintf (name, sizeof (name),"ksocknald_%02d", id);
1679 kportal_daemonize (name);
1680 kportal_blockallsigs ();
1682 current->flags |= PF_MEMALLOC;
1684 #if (CONFIG_SMP && CPU_AFFINITY)
1685 if ((cpu_online_map & (1 << id)) != 0) {
1687 current->cpus_allowed = (1 << id);
1689 set_cpus_allowed (current, 1<<id);
1692 CERROR ("Can't set CPU affinity for %s\n", name);
1694 #endif /* CONFIG_SMP && CPU_AFFINITY */
1696 spin_lock_irqsave (&sched->kss_lock, flags);
1698 while (!ksocknal_data.ksnd_shuttingdown) {
1699 int did_something = 0;
1701 /* Ensure I progress everything semi-fairly */
1703 if (!list_empty (&sched->kss_rx_conns)) {
1704 conn = list_entry(sched->kss_rx_conns.next,
1705 ksock_conn_t, ksnc_rx_list);
1706 list_del(&conn->ksnc_rx_list);
1708 LASSERT(conn->ksnc_rx_scheduled);
1709 LASSERT(conn->ksnc_rx_ready);
1711 /* clear rx_ready in case receive isn't complete.
1712 * Do it BEFORE we call process_recv, since
1713 * data_ready can set it any time after we release
1715 conn->ksnc_rx_ready = 0;
1716 spin_unlock_irqrestore(&sched->kss_lock, flags);
1718 rc = ksocknal_process_receive(conn);
1720 spin_lock_irqsave(&sched->kss_lock, flags);
1722 /* I'm the only one that can clear this flag */
1723 LASSERT(conn->ksnc_rx_scheduled);
1725 /* Did process_receive get everything it wanted? */
1727 conn->ksnc_rx_ready = 1;
1729 if (conn->ksnc_rx_state == SOCKNAL_RX_FMB_SLEEP ||
1730 conn->ksnc_rx_state == SOCKNAL_RX_GET_FMB) {
1731 /* Conn blocked for a forwarding buffer.
1732 * It will get queued for my attention when
1733 * one becomes available (and it might just
1734 * already have been!). Meanwhile my ref
1735 * on it stays put. */
1736 } else if (conn->ksnc_rx_ready) {
1737 /* reschedule for rx */
1738 list_add_tail (&conn->ksnc_rx_list,
1739 &sched->kss_rx_conns);
1741 conn->ksnc_rx_scheduled = 0;
1743 ksocknal_put_conn(conn);
1749 if (!list_empty (&sched->kss_tx_conns)) {
1750 conn = list_entry(sched->kss_tx_conns.next,
1751 ksock_conn_t, ksnc_tx_list);
1752 list_del (&conn->ksnc_tx_list);
1754 LASSERT(conn->ksnc_tx_scheduled);
1755 LASSERT(conn->ksnc_tx_ready);
1756 LASSERT(!list_empty(&conn->ksnc_tx_queue));
1758 tx = list_entry(conn->ksnc_tx_queue.next,
1759 ksock_tx_t, tx_list);
1760 /* dequeue now so empty list => more to send */
1761 list_del(&tx->tx_list);
1763 /* Clear tx_ready in case send isn't complete. Do
1764 * it BEFORE we call process_transmit, since
1765 * write_space can set it any time after we release
1767 conn->ksnc_tx_ready = 0;
1768 spin_unlock_irqrestore (&sched->kss_lock, flags);
1770 rc = ksocknal_process_transmit(conn, tx);
1772 spin_lock_irqsave (&sched->kss_lock, flags);
1774 if (rc == -ENOMEM || rc == -EAGAIN) {
1775 /* Incomplete send: replace tx on HEAD of tx_queue */
1776 list_add (&tx->tx_list, &conn->ksnc_tx_queue);
1778 /* Complete send; assume space for more */
1779 conn->ksnc_tx_ready = 1;
1782 if (rc == -ENOMEM) {
1783 /* Do nothing; after a short timeout, this
1784 * conn will be reposted on kss_tx_conns. */
1785 } else if (conn->ksnc_tx_ready &&
1786 !list_empty (&conn->ksnc_tx_queue)) {
1787 /* reschedule for tx */
1788 list_add_tail (&conn->ksnc_tx_list,
1789 &sched->kss_tx_conns);
1791 conn->ksnc_tx_scheduled = 0;
1793 ksocknal_put_conn (conn);
1799 if (!list_empty (&sched->kss_zctxdone_list)) {
1801 list_entry(sched->kss_zctxdone_list.next,
1802 ksock_tx_t, tx_list);
1805 list_del (&tx->tx_list);
1806 spin_unlock_irqrestore (&sched->kss_lock, flags);
1808 ksocknal_tx_done (tx, 1);
1810 spin_lock_irqsave (&sched->kss_lock, flags);
1813 if (!did_something || /* nothing to do */
1814 ++nloops == SOCKNAL_RESCHED) { /* hogging CPU? */
1815 spin_unlock_irqrestore (&sched->kss_lock, flags);
1819 if (!did_something) { /* wait for something to do */
1821 rc = wait_event_interruptible (sched->kss_waitq,
1822 ksocknal_data.ksnd_shuttingdown ||
1823 !list_empty(&sched->kss_rx_conns) ||
1824 !list_empty(&sched->kss_tx_conns) ||
1825 !list_empty(&sched->kss_zctxdone_list));
1827 rc = wait_event_interruptible (sched->kss_waitq,
1828 ksocknal_data.ksnd_shuttingdown ||
1829 !list_empty(&sched->kss_rx_conns) ||
1830 !list_empty(&sched->kss_tx_conns));
1836 spin_lock_irqsave (&sched->kss_lock, flags);
1840 spin_unlock_irqrestore (&sched->kss_lock, flags);
1841 ksocknal_thread_fini ();
1846 ksocknal_data_ready (struct sock *sk, int n)
1848 unsigned long flags;
1850 ksock_sched_t *sched;
1853 /* interleave correctly with closing sockets... */
1854 read_lock (&ksocknal_data.ksnd_global_lock);
1856 conn = sk->sk_user_data;
1857 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
1858 LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
1859 sk->sk_data_ready (sk, n);
1861 sched = conn->ksnc_scheduler;
1863 spin_lock_irqsave (&sched->kss_lock, flags);
1865 conn->ksnc_rx_ready = 1;
1867 if (!conn->ksnc_rx_scheduled) { /* not being progressed */
1868 list_add_tail(&conn->ksnc_rx_list,
1869 &sched->kss_rx_conns);
1870 conn->ksnc_rx_scheduled = 1;
1871 /* extra ref for scheduler */
1872 atomic_inc (&conn->ksnc_refcount);
1874 wake_up (&sched->kss_waitq);
1877 spin_unlock_irqrestore (&sched->kss_lock, flags);
1880 read_unlock (&ksocknal_data.ksnd_global_lock);
1886 ksocknal_write_space (struct sock *sk)
1888 unsigned long flags;
1890 ksock_sched_t *sched;
1892 /* interleave correctly with closing sockets... */
1893 read_lock (&ksocknal_data.ksnd_global_lock);
1895 conn = sk->sk_user_data;
1897 CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
1898 sk, tcp_wspace(sk), SOCKNAL_TX_LOW_WATER(sk), conn,
1899 (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
1900 " ready" : " blocked"),
1901 (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
1902 " scheduled" : " idle"),
1903 (conn == NULL) ? "" : (list_empty (&conn->ksnc_tx_queue) ?
1904 " empty" : " queued"));
1906 if (conn == NULL) { /* raced with ksocknal_terminate_conn */
1907 LASSERT (sk->sk_write_space != &ksocknal_write_space);
1908 sk->sk_write_space (sk);
1910 read_unlock (&ksocknal_data.ksnd_global_lock);
1914 if (tcp_wspace(sk) >= SOCKNAL_TX_LOW_WATER(sk)) { /* got enough space */
1915 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
1917 sched = conn->ksnc_scheduler;
1919 spin_lock_irqsave (&sched->kss_lock, flags);
1921 conn->ksnc_tx_ready = 1;
1923 if (!conn->ksnc_tx_scheduled && // not being progressed
1924 !list_empty(&conn->ksnc_tx_queue)){//packets to send
1925 list_add_tail (&conn->ksnc_tx_list,
1926 &sched->kss_tx_conns);
1927 conn->ksnc_tx_scheduled = 1;
1928 /* extra ref for scheduler */
1929 atomic_inc (&conn->ksnc_refcount);
1931 wake_up (&sched->kss_waitq);
1934 spin_unlock_irqrestore (&sched->kss_lock, flags);
1937 read_unlock (&ksocknal_data.ksnd_global_lock);
1941 ksocknal_sock_write (struct socket *sock, void *buffer, int nob)
1944 mm_segment_t oldmm = get_fs();
1947 struct iovec iov = {
1951 struct msghdr msg = {
1956 .msg_control = NULL,
1957 .msg_controllen = 0,
1962 rc = sock_sendmsg (sock, &msg, iov.iov_len);
1969 CERROR ("Unexpected zero rc\n");
1970 return (-ECONNABORTED);
1973 buffer = ((char *)buffer) + rc;
1981 ksocknal_sock_read (struct socket *sock, void *buffer, int nob)
1984 mm_segment_t oldmm = get_fs();
1987 struct iovec iov = {
1991 struct msghdr msg = {
1996 .msg_control = NULL,
1997 .msg_controllen = 0,
2002 rc = sock_recvmsg (sock, &msg, iov.iov_len, 0);
2009 return (-ECONNABORTED);
2011 buffer = ((char *)buffer) + rc;
2019 ksocknal_hello (struct socket *sock, ptl_nid_t *nid, int *type, __u64 *incarnation)
2023 ptl_magicversion_t *hmv = (ptl_magicversion_t *)&hdr.dest_nid;
2025 LASSERT (sizeof (*hmv) == sizeof (hdr.dest_nid));
2027 memset (&hdr, 0, sizeof (hdr));
2028 hmv->magic = __cpu_to_le32 (PORTALS_PROTO_MAGIC);
2029 hmv->version_major = __cpu_to_le32 (PORTALS_PROTO_VERSION_MAJOR);
2030 hmv->version_minor = __cpu_to_le32 (PORTALS_PROTO_VERSION_MINOR);
2032 hdr.src_nid = __cpu_to_le64 (ksocknal_lib.ni.nid);
2033 hdr.type = __cpu_to_le32 (PTL_MSG_HELLO);
2035 hdr.msg.hello.type = __cpu_to_le32 (*type);
2036 hdr.msg.hello.incarnation =
2037 __cpu_to_le64 (ksocknal_data.ksnd_incarnation);
2039 /* Assume sufficient socket buffering for this message */
2040 rc = ksocknal_sock_write (sock, &hdr, sizeof (hdr));
2042 CERROR ("Error %d sending HELLO to "LPX64"\n", rc, *nid);
2046 rc = ksocknal_sock_read (sock, hmv, sizeof (*hmv));
2048 CERROR ("Error %d reading HELLO from "LPX64"\n", rc, *nid);
2052 if (hmv->magic != __le32_to_cpu (PORTALS_PROTO_MAGIC)) {
2053 CERROR ("Bad magic %#08x (%#08x expected) from "LPX64"\n",
2054 __cpu_to_le32 (hmv->magic), PORTALS_PROTO_MAGIC, *nid);
2058 if (hmv->version_major != __cpu_to_le16 (PORTALS_PROTO_VERSION_MAJOR) ||
2059 hmv->version_minor != __cpu_to_le16 (PORTALS_PROTO_VERSION_MINOR)) {
2060 CERROR ("Incompatible protocol version %d.%d (%d.%d expected)"
2062 __le16_to_cpu (hmv->version_major),
2063 __le16_to_cpu (hmv->version_minor),
2064 PORTALS_PROTO_VERSION_MAJOR,
2065 PORTALS_PROTO_VERSION_MINOR,
2070 #if (PORTALS_PROTO_VERSION_MAJOR != 0)
2071 # error "This code only understands protocol version 0.x"
2073 /* version 0 sends magic/version as the dest_nid of a 'hello' header,
2074 * so read the rest of it in now... */
2076 rc = ksocknal_sock_read (sock, hmv + 1, sizeof (hdr) - sizeof (*hmv));
2078 CERROR ("Error %d reading rest of HELLO hdr from "LPX64"\n",
2083 /* ...and check we got what we expected */
2084 if (hdr.type != __cpu_to_le32 (PTL_MSG_HELLO) ||
2085 hdr.payload_length != __cpu_to_le32 (0)) {
2086 CERROR ("Expecting a HELLO hdr with 0 payload,"
2087 " but got type %d with %d payload from "LPX64"\n",
2088 __le32_to_cpu (hdr.type),
2089 __le32_to_cpu (hdr.payload_length), *nid);
2093 if (__le64_to_cpu(hdr.src_nid) == PTL_NID_ANY) {
2094 CERROR("Expecting a HELLO hdr with a NID, but got PTL_NID_ANY\n");
2098 if (*nid == PTL_NID_ANY) { /* don't know peer's nid yet */
2099 *nid = __le64_to_cpu(hdr.src_nid);
2100 } else if (*nid != __le64_to_cpu (hdr.src_nid)) {
2101 CERROR ("Connected to nid "LPX64", but expecting "LPX64"\n",
2102 __le64_to_cpu (hdr.src_nid), *nid);
2106 if (*type == SOCKNAL_CONN_NONE) {
2107 /* I've accepted this connection; peer determines type */
2108 *type = __le32_to_cpu(hdr.msg.hello.type);
2110 case SOCKNAL_CONN_ANY:
2111 case SOCKNAL_CONN_CONTROL:
2113 case SOCKNAL_CONN_BULK_IN:
2114 *type = SOCKNAL_CONN_BULK_OUT;
2116 case SOCKNAL_CONN_BULK_OUT:
2117 *type = SOCKNAL_CONN_BULK_IN;
2120 CERROR ("Unexpected type %d from "LPX64"\n", *type, *nid);
2123 } else if (__le32_to_cpu(hdr.msg.hello.type) != SOCKNAL_CONN_NONE) {
2124 CERROR ("Mismatched types: me %d "LPX64" %d\n",
2125 *type, *nid, __le32_to_cpu(hdr.msg.hello.type));
2129 *incarnation = __le64_to_cpu(hdr.msg.hello.incarnation);
2135 ksocknal_setup_sock (struct socket *sock)
2137 mm_segment_t oldmm = get_fs ();
2140 struct linger linger;
2142 sock->sk->allocation = GFP_MEMALLOC;
2144 /* Ensure this socket aborts active sends immediately when we close
2148 linger.l_linger = 0;
2151 rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
2152 (char *)&linger, sizeof (linger));
2155 CERROR ("Can't set SO_LINGER: %d\n", rc);
2161 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
2162 (char *)&option, sizeof (option));
2165 CERROR ("Can't set SO_LINGER2: %d\n", rc);
2169 #if SOCKNAL_USE_KEEPALIVES
2170 /* Keepalives: If 3/4 of the timeout elapses, start probing every
2171 * second until the timeout elapses. */
2173 option = (ksocknal_data.ksnd_io_timeout * 3) / 4;
2175 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
2176 (char *)&option, sizeof (option));
2179 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
2185 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
2186 (char *)&option, sizeof (option));
2189 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
2193 option = ksocknal_data.ksnd_io_timeout / 4;
2195 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
2196 (char *)&option, sizeof (option));
2199 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
2205 rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
2206 (char *)&option, sizeof (option));
2209 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
2217 ksocknal_connect_peer (ksock_route_t *route, int type)
2219 struct sockaddr_in peer_addr;
2220 mm_segment_t oldmm = get_fs();
2223 struct socket *sock;
2226 rc = sock_create (PF_INET, SOCK_STREAM, 0, &sock);
2228 CERROR ("Can't create autoconnect socket: %d\n", rc);
2232 /* Ugh; have to map_fd for compatibility with sockets passed in
2233 * from userspace. And we actually need the sock->file refcounting
2234 * that this gives you :) */
2236 fd = sock_map_fd (sock);
2238 sock_release (sock);
2239 CERROR ("sock_map_fd error %d\n", fd);
2243 /* NB the fd now owns the ref on sock->file */
2244 LASSERT (sock->file != NULL);
2245 LASSERT (file_count(sock->file) == 1);
2247 /* Set the socket timeouts, so our connection attempt completes in
2249 tv.tv_sec = ksocknal_data.ksnd_io_timeout;
2253 rc = sock_setsockopt (sock, SOL_SOCKET, SO_SNDTIMEO,
2254 (char *)&tv, sizeof (tv));
2257 CERROR ("Can't set send timeout %d: %d\n",
2258 ksocknal_data.ksnd_io_timeout, rc);
2263 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVTIMEO,
2264 (char *)&tv, sizeof (tv));
2267 CERROR ("Can't set receive timeout %d: %d\n",
2268 ksocknal_data.ksnd_io_timeout, rc);
2272 if (route->ksnr_nonagel) {
2276 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
2277 (char *)&option, sizeof (option));
2280 CERROR ("Can't disable nagel: %d\n", rc);
2285 if (route->ksnr_buffer_size != 0) {
2286 int option = route->ksnr_buffer_size;
2289 rc = sock_setsockopt (sock, SOL_SOCKET, SO_SNDBUF,
2290 (char *)&option, sizeof (option));
2293 CERROR ("Can't set send buffer %d: %d\n",
2294 route->ksnr_buffer_size, rc);
2299 rc = sock_setsockopt (sock, SOL_SOCKET, SO_RCVBUF,
2300 (char *)&option, sizeof (option));
2303 CERROR ("Can't set receive buffer %d: %d\n",
2304 route->ksnr_buffer_size, rc);
2309 memset (&peer_addr, 0, sizeof (peer_addr));
2310 peer_addr.sin_family = AF_INET;
2311 peer_addr.sin_port = htons (route->ksnr_port);
2312 peer_addr.sin_addr.s_addr = htonl (route->ksnr_ipaddr);
2314 rc = sock->ops->connect (sock, (struct sockaddr *)&peer_addr,
2315 sizeof (peer_addr), sock->file->f_flags);
2317 CERROR ("Error %d connecting to "LPX64"\n", rc,
2318 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 tx = list_entry (zombies.next, ksock_tx_t, tx_list);
2402 CERROR ("Deleting packet type %d len %d ("LPX64"->"LPX64")\n",
2403 NTOH__u32 (tx->tx_hdr->type),
2404 NTOH__u32 (tx->tx_hdr->payload_length),
2405 NTOH__u64 (tx->tx_hdr->src_nid),
2406 NTOH__u64 (tx->tx_hdr->dest_nid));
2408 list_del (&tx->tx_list);
2410 ksocknal_tx_done (tx, 0);
2415 ksocknal_autoconnectd (void *arg)
2417 long id = (long)arg;
2419 unsigned long flags;
2420 ksock_route_t *route;
2423 snprintf (name, sizeof (name), "ksocknal_ad%02ld", id);
2424 kportal_daemonize (name);
2425 kportal_blockallsigs ();
2427 current->flags |= PF_MEMALLOC;
2429 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2431 while (!ksocknal_data.ksnd_shuttingdown) {
2433 if (!list_empty (&ksocknal_data.ksnd_autoconnectd_routes)) {
2434 route = list_entry (ksocknal_data.ksnd_autoconnectd_routes.next,
2435 ksock_route_t, ksnr_connect_list);
2437 list_del (&route->ksnr_connect_list);
2438 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2440 ksocknal_autoconnect (route);
2441 ksocknal_put_route (route);
2443 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2447 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2449 rc = wait_event_interruptible (ksocknal_data.ksnd_autoconnectd_waitq,
2450 ksocknal_data.ksnd_shuttingdown ||
2451 !list_empty (&ksocknal_data.ksnd_autoconnectd_routes));
2453 spin_lock_irqsave (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2456 spin_unlock_irqrestore (&ksocknal_data.ksnd_autoconnectd_lock, flags);
2458 ksocknal_thread_fini ();
2463 ksocknal_find_timed_out_conn (ksock_peer_t *peer)
2465 /* We're called with a shared lock on ksnd_global_lock */
2467 struct list_head *ctmp;
2468 ksock_sched_t *sched;
2470 list_for_each (ctmp, &peer->ksnp_conns) {
2471 conn = list_entry (ctmp, ksock_conn_t, ksnc_list);
2472 sched = conn->ksnc_scheduler;
2474 /* Don't need the {get,put}connsock dance to deref ksnc_sock... */
2475 LASSERT (!conn->ksnc_closing);
2477 if (conn->ksnc_rx_started &&
2478 time_after_eq (jiffies, conn->ksnc_rx_deadline)) {
2479 /* Timed out incomplete incoming message */
2480 atomic_inc (&conn->ksnc_refcount);
2481 CERROR ("Timed out RX from "LPX64" %p\n",
2482 peer->ksnp_nid, conn);
2486 if ((!list_empty (&conn->ksnc_tx_queue) ||
2487 conn->ksnc_sock->sk->sk_wmem_queued != 0) &&
2488 time_after_eq (jiffies, conn->ksnc_tx_deadline)) {
2489 /* Timed out messages queued for sending, or
2490 * messages buffered in the socket's send buffer */
2491 atomic_inc (&conn->ksnc_refcount);
2492 CERROR ("Timed out TX to "LPX64" %s%d %p\n",
2494 list_empty (&conn->ksnc_tx_queue) ? "" : "Q ",
2495 conn->ksnc_sock->sk->sk_wmem_queued, conn);
2504 ksocknal_check_peer_timeouts (int idx)
2506 struct list_head *peers = &ksocknal_data.ksnd_peers[idx];
2507 struct list_head *ptmp;
2512 /* NB. We expect to have a look at all the peers and not find any
2513 * connections to time out, so we just use a shared lock while we
2515 read_lock (&ksocknal_data.ksnd_global_lock);
2517 list_for_each (ptmp, peers) {
2518 peer = list_entry (ptmp, ksock_peer_t, ksnp_list);
2519 conn = ksocknal_find_timed_out_conn (peer);
2522 read_unlock (&ksocknal_data.ksnd_global_lock);
2524 CERROR ("Timeout out conn->"LPX64" ip %x:%d\n",
2525 peer->ksnp_nid, conn->ksnc_ipaddr,
2527 ksocknal_close_conn_and_siblings (conn, -ETIMEDOUT);
2529 /* NB we won't find this one again, but we can't
2530 * just proceed with the next peer, since we dropped
2531 * ksnd_global_lock and it might be dead already! */
2532 ksocknal_put_conn (conn);
2537 read_unlock (&ksocknal_data.ksnd_global_lock);
2541 ksocknal_reaper (void *arg)
2544 unsigned long flags;
2546 ksock_sched_t *sched;
2547 struct list_head enomem_conns;
2552 unsigned long deadline = jiffies;
2554 kportal_daemonize ("ksocknal_reaper");
2555 kportal_blockallsigs ();
2557 INIT_LIST_HEAD(&enomem_conns);
2558 init_waitqueue_entry (&wait, current);
2560 current->flags |= PF_MEMALLOC;
2562 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2564 while (!ksocknal_data.ksnd_shuttingdown) {
2566 if (!list_empty (&ksocknal_data.ksnd_deathrow_conns)) {
2567 conn = list_entry (ksocknal_data.ksnd_deathrow_conns.next,
2568 ksock_conn_t, ksnc_list);
2569 list_del (&conn->ksnc_list);
2571 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2573 ksocknal_terminate_conn (conn);
2574 ksocknal_put_conn (conn);
2576 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2580 if (!list_empty (&ksocknal_data.ksnd_zombie_conns)) {
2581 conn = list_entry (ksocknal_data.ksnd_zombie_conns.next,
2582 ksock_conn_t, ksnc_list);
2583 list_del (&conn->ksnc_list);
2585 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2587 ksocknal_destroy_conn (conn);
2589 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2593 if (!list_empty (&ksocknal_data.ksnd_enomem_conns)) {
2594 list_add(&enomem_conns, &ksocknal_data.ksnd_enomem_conns);
2595 list_del_init(&ksocknal_data.ksnd_enomem_conns);
2598 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2600 /* reschedule all the connections that stalled with ENOMEM... */
2602 while (!list_empty (&enomem_conns)) {
2603 conn = list_entry (enomem_conns.next,
2604 ksock_conn_t, ksnc_tx_list);
2605 list_del (&conn->ksnc_tx_list);
2607 sched = conn->ksnc_scheduler;
2609 spin_lock_irqsave (&sched->kss_lock, flags);
2611 LASSERT (conn->ksnc_tx_scheduled);
2612 conn->ksnc_tx_ready = 1;
2613 list_add_tail (&conn->ksnc_tx_list, &sched->kss_tx_conns);
2614 wake_up (&sched->kss_waitq);
2616 spin_unlock_irqrestore (&sched->kss_lock, flags);
2620 /* careful with the jiffy wrap... */
2621 while ((timeout = (int)(deadline - jiffies)) <= 0) {
2624 int chunk = ksocknal_data.ksnd_peer_hash_size;
2626 /* Time to check for timeouts on a few more peers: I do
2627 * checks every 'p' seconds on a proportion of the peer
2628 * table and I need to check every connection 'n' times
2629 * within a timeout interval, to ensure I detect a
2630 * timeout on any connection within (n+1)/n times the
2631 * timeout interval. */
2633 if (ksocknal_data.ksnd_io_timeout > n * p)
2634 chunk = (chunk * n * p) /
2635 ksocknal_data.ksnd_io_timeout;
2639 for (i = 0; i < chunk; i++) {
2640 ksocknal_check_peer_timeouts (peer_index);
2641 peer_index = (peer_index + 1) %
2642 ksocknal_data.ksnd_peer_hash_size;
2648 if (nenomem_conns != 0) {
2649 /* Reduce my timeout if I rescheduled ENOMEM conns.
2650 * This also prevents me getting woken immediately
2651 * if any go back on my enomem list. */
2652 timeout = SOCKNAL_ENOMEM_RETRY;
2654 ksocknal_data.ksnd_reaper_waketime = jiffies + timeout;
2656 add_wait_queue (&ksocknal_data.ksnd_reaper_waitq, &wait);
2657 set_current_state (TASK_INTERRUPTIBLE);
2659 if (!ksocknal_data.ksnd_shuttingdown &&
2660 list_empty (&ksocknal_data.ksnd_deathrow_conns) &&
2661 list_empty (&ksocknal_data.ksnd_zombie_conns))
2662 schedule_timeout (timeout);
2664 set_current_state (TASK_RUNNING);
2665 remove_wait_queue (&ksocknal_data.ksnd_reaper_waitq, &wait);
2667 spin_lock_irqsave (&ksocknal_data.ksnd_reaper_lock, flags);
2670 spin_unlock_irqrestore (&ksocknal_data.ksnd_reaper_lock, flags);
2672 ksocknal_thread_fini ();
2676 nal_cb_t ksocknal_lib = {
2677 nal_data: &ksocknal_data, /* NAL private data */
2678 cb_send: ksocknal_send,
2679 cb_send_pages: ksocknal_send_pages,
2680 cb_recv: ksocknal_recv,
2681 cb_recv_pages: ksocknal_recv_pages,
2682 cb_read: ksocknal_read,
2683 cb_write: ksocknal_write,
2684 cb_callback: ksocknal_callback,
2685 cb_malloc: ksocknal_malloc,
2686 cb_free: ksocknal_free,
2687 cb_printf: ksocknal_printf,
2688 cb_cli: ksocknal_cli,
2689 cb_sti: ksocknal_sti,
2690 cb_dist: ksocknal_dist