Whamcloud - gitweb
LU-13566 socklnd: fix local interface binding
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd.c
index 4ed0458..5249227 100644 (file)
@@ -37,8 +37,8 @@
  * Author: Eric Barton <eric@bartonsoftware.com>
  */
 
-#include "socklnd.h"
 #include <linux/inetdevice.h>
+#include "socklnd.h"
 
 static const struct lnet_lnd the_ksocklnd;
 struct ksock_nal_data ksocknal_data;
@@ -61,6 +61,58 @@ ksocknal_ip2iface(struct lnet_ni *ni, __u32 ip)
        return NULL;
 }
 
+static struct ksock_interface *
+ksocknal_index2iface(struct lnet_ni *ni, int index)
+{
+       struct ksock_net *net = ni->ni_data;
+       int i;
+       struct ksock_interface *iface;
+
+       for (i = 0; i < net->ksnn_ninterfaces; i++) {
+               LASSERT(i < LNET_INTERFACES_NUM);
+               iface = &net->ksnn_interfaces[i];
+
+               if (iface->ksni_index == index)
+                       return iface;
+       }
+
+       return NULL;
+}
+
+static int ksocknal_ip2index(__u32 ipaddress, struct lnet_ni *ni)
+{
+       struct net_device *dev;
+       int ret = -1;
+       DECLARE_CONST_IN_IFADDR(ifa);
+
+       rcu_read_lock();
+       for_each_netdev(ni->ni_net_ns, dev) {
+               int flags = dev_get_flags(dev);
+               struct in_device *in_dev;
+
+               if (flags & IFF_LOOPBACK) /* skip the loopback IF */
+                       continue;
+
+               if (!(flags & IFF_UP))
+                       continue;
+
+               in_dev = __in_dev_get_rcu(dev);
+               if (!in_dev)
+                       continue;
+
+               in_dev_for_each_ifa_rcu(ifa, in_dev) {
+                       if (ntohl(ifa->ifa_local) == ipaddress)
+                               ret = dev->ifindex;
+               }
+               endfor_ifa(in_dev);
+               if (ret >= 0)
+                       break;
+       }
+       rcu_read_unlock();
+
+       return ret;
+}
+
 static struct ksock_route *
 ksocknal_create_route(__u32 ipaddr, int port)
 {
@@ -74,15 +126,16 @@ ksocknal_create_route(__u32 ipaddr, int port)
        route->ksnr_peer = NULL;
        route->ksnr_retry_interval = 0;         /* OK to connect at any time */
        route->ksnr_ipaddr = ipaddr;
-        route->ksnr_port = port;
-        route->ksnr_scheduled = 0;
-        route->ksnr_connecting = 0;
-        route->ksnr_connected = 0;
-        route->ksnr_deleted = 0;
-        route->ksnr_conn_count = 0;
-        route->ksnr_share_count = 0;
-
-        return (route);
+       route->ksnr_myiface = -1;
+       route->ksnr_port = port;
+       route->ksnr_scheduled = 0;
+       route->ksnr_connecting = 0;
+       route->ksnr_connected = 0;
+       route->ksnr_deleted = 0;
+       route->ksnr_conn_count = 0;
+       route->ksnr_share_count = 0;
+
+       return route;
 }
 
 void
@@ -158,7 +211,8 @@ ksocknal_destroy_peer(struct ksock_peer_ni *peer_ni)
         * state to do with this peer_ni has been cleaned up when its refcount
         * drops to zero.
         */
-       atomic_dec(&net->ksnn_npeers);
+       if (atomic_dec_and_test(&net->ksnn_npeers))
+               wake_up_var(&net->ksnn_npeers);
 }
 
 struct ksock_peer_ni *
@@ -287,12 +341,13 @@ ksocknal_get_peer_info(struct lnet_ni *ni, int index,
                                           ksnr_list);
 
                        *id = peer_ni->ksnp_id;
-                       *myip = route->ksnr_myipaddr;
+                       rc = choose_ipv4_src(myip, route->ksnr_myiface,
+                                            route->ksnr_ipaddr,
+                                            ni->ni_net_ns);
                        *peer_ip = route->ksnr_ipaddr;
                        *port = route->ksnr_port;
                        *conn_count = route->ksnr_conn_count;
                        *share_count = route->ksnr_share_count;
-                       rc = 0;
                        goto out;
                }
        }
@@ -302,47 +357,52 @@ out:
 }
 
 static void
-ksocknal_associate_route_conn_locked(struct ksock_route *route, struct ksock_conn *conn)
+ksocknal_associate_route_conn_locked(struct ksock_route *route,
+                                    struct ksock_conn *conn)
 {
        struct ksock_peer_ni *peer_ni = route->ksnr_peer;
        int type = conn->ksnc_type;
        struct ksock_interface *iface;
+       int conn_iface = ksocknal_ip2index(conn->ksnc_myipaddr,
+                                          route->ksnr_peer->ksnp_ni);
 
        conn->ksnc_route = route;
        ksocknal_route_addref(route);
 
-       if (route->ksnr_myipaddr != conn->ksnc_myipaddr) {
-               if (route->ksnr_myipaddr == 0) {
+       if (route->ksnr_myiface != conn_iface) {
+               if (route->ksnr_myiface < 0) {
                        /* route wasn't bound locally yet (the initial route) */
-                       CDEBUG(D_NET, "Binding %s %pI4h to %pI4h\n",
+                       CDEBUG(D_NET, "Binding %s %pI4h to interface %d\n",
                               libcfs_id2str(peer_ni->ksnp_id),
                               &route->ksnr_ipaddr,
-                              &conn->ksnc_myipaddr);
+                              conn_iface);
                } else {
-                       CDEBUG(D_NET, "Rebinding %s %pI4h from %pI4h "
-                              "to %pI4h\n", libcfs_id2str(peer_ni->ksnp_id),
+                       CDEBUG(D_NET,
+                              "Rebinding %s %pI4h from interface %d to %d\n",
+                              libcfs_id2str(peer_ni->ksnp_id),
                               &route->ksnr_ipaddr,
-                              &route->ksnr_myipaddr,
-                              &conn->ksnc_myipaddr);
+                              route->ksnr_myiface,
+                              conn_iface);
 
-                        iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
-                                                  route->ksnr_myipaddr);
-                        if (iface != NULL)
-                                iface->ksni_nroutes--;
-                }
-                route->ksnr_myipaddr = conn->ksnc_myipaddr;
-                iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
-                                          route->ksnr_myipaddr);
-                if (iface != NULL)
-                        iface->ksni_nroutes++;
-        }
+                       iface = ksocknal_index2iface(route->ksnr_peer->ksnp_ni,
+                                                    route->ksnr_myiface);
+                       if (iface)
+                               iface->ksni_nroutes--;
+               }
+               route->ksnr_myiface = conn_iface;
+               iface = ksocknal_index2iface(route->ksnr_peer->ksnp_ni,
+                                            route->ksnr_myiface);
+               if (iface)
+                       iface->ksni_nroutes++;
+       }
 
-        route->ksnr_connected |= (1<<type);
-        route->ksnr_conn_count++;
+       route->ksnr_connected |= (1<<type);
+       route->ksnr_conn_count++;
 
-        /* Successful connection => further attempts can
-         * proceed immediately */
-        route->ksnr_retry_interval = 0;
+       /* Successful connection => further attempts can
+        * proceed immediately
+        */
+       route->ksnr_retry_interval = 0;
 }
 
 static void
@@ -351,12 +411,14 @@ ksocknal_add_route_locked(struct ksock_peer_ni *peer_ni, struct ksock_route *rou
        struct list_head *tmp;
        struct ksock_conn *conn;
        struct ksock_route *route2;
+       struct ksock_net *net = peer_ni->ksnp_ni->ni_data;
 
        LASSERT(!peer_ni->ksnp_closing);
        LASSERT(route->ksnr_peer == NULL);
        LASSERT(!route->ksnr_scheduled);
        LASSERT(!route->ksnr_connecting);
        LASSERT(route->ksnr_connected == 0);
+       LASSERT(net->ksnn_ninterfaces > 0);
 
        /* LASSERT(unique) */
        list_for_each(tmp, &peer_ni->ksnp_routes) {
@@ -372,6 +434,11 @@ ksocknal_add_route_locked(struct ksock_peer_ni *peer_ni, struct ksock_route *rou
 
        route->ksnr_peer = peer_ni;
        ksocknal_peer_addref(peer_ni);
+
+       /* set the route's interface to the current net's interface */
+       route->ksnr_myiface = net->ksnn_interfaces[0].ksni_index;
+       net->ksnn_interfaces[0].ksni_nroutes++;
+
        /* peer_ni's routelist takes over my ref on 'route' */
        list_add_tail(&route->ksnr_list, &peer_ni->ksnp_routes);
 
@@ -407,10 +474,10 @@ ksocknal_del_route_locked(struct ksock_route *route)
                ksocknal_close_conn_locked(conn, 0);
        }
 
-       if (route->ksnr_myipaddr != 0) {
-               iface = ksocknal_ip2iface(route->ksnr_peer->ksnp_ni,
-                                         route->ksnr_myipaddr);
-               if (iface != NULL)
+       if (route->ksnr_myiface >= 0) {
+               iface = ksocknal_index2iface(route->ksnr_peer->ksnp_ni,
+                                            route->ksnr_myiface);
+               if (iface)
                        iface->ksni_nroutes--;
        }
 
@@ -904,7 +971,7 @@ ksocknal_create_routes(struct ksock_peer_ni *peer_ni, int port,
                                route = list_entry(rtmp, struct ksock_route,
                                                   ksnr_list);
 
-                               if (route->ksnr_myipaddr == iface->ksni_ipaddr)
+                               if (route->ksnr_myiface == iface->ksni_index)
                                        break;
 
                                route = NULL;
@@ -912,34 +979,34 @@ ksocknal_create_routes(struct ksock_peer_ni *peer_ni, int port,
                        if (route != NULL)
                                continue;
 
-                        this_netmatch = (((iface->ksni_ipaddr ^
-                                           newroute->ksnr_ipaddr) &
-                                           iface->ksni_netmask) == 0) ? 1 : 0;
+                       this_netmatch = (((iface->ksni_ipaddr ^
+                                          newroute->ksnr_ipaddr) &
+                                         iface->ksni_netmask) == 0) ? 1 : 0;
 
-                        if (!(best_iface == NULL ||
-                              best_netmatch < this_netmatch ||
-                              (best_netmatch == this_netmatch &&
-                               best_nroutes > iface->ksni_nroutes)))
-                                continue;
+                       if (!(best_iface == NULL ||
+                             best_netmatch < this_netmatch ||
+                             (best_netmatch == this_netmatch &&
+                              best_nroutes > iface->ksni_nroutes)))
+                               continue;
 
-                        best_iface = iface;
-                        best_netmatch = this_netmatch;
-                        best_nroutes = iface->ksni_nroutes;
-                }
+                       best_iface = iface;
+                       best_netmatch = this_netmatch;
+                       best_nroutes = iface->ksni_nroutes;
+               }
 
-                if (best_iface == NULL)
-                        continue;
+               if (best_iface == NULL)
+                       continue;
 
-                newroute->ksnr_myipaddr = best_iface->ksni_ipaddr;
-                best_iface->ksni_nroutes++;
+               newroute->ksnr_myiface = best_iface->ksni_index;
+               best_iface->ksni_nroutes++;
 
-                ksocknal_add_route_locked(peer_ni, newroute);
-                newroute = NULL;
-        }
+               ksocknal_add_route_locked(peer_ni, newroute);
+               newroute = NULL;
+       }
 
        write_unlock_bh(global_lock);
-        if (newroute != NULL)
-                ksocknal_route_decref(newroute);
+       if (newroute != NULL)
+               ksocknal_route_decref(newroute);
 }
 
 int
@@ -1251,7 +1318,7 @@ ksocknal_create_conn(struct lnet_ni *ni, struct ksock_route *route,
        /* Set the deadline for the outgoing HELLO to drain */
        conn->ksnc_tx_bufnob = sock->sk->sk_wmem_queued;
        conn->ksnc_tx_deadline = ktime_get_seconds() +
-                                lnet_get_lnd_timeout();
+                                ksocknal_timeout();
        smp_mb();   /* order with adding to peer_ni's conn list */
 
        list_add(&conn->ksnc_list, &peer_ni->ksnp_conns);
@@ -1412,7 +1479,7 @@ ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
        if (route != NULL) {
                /* dissociate conn from route... */
                LASSERT(!route->ksnr_deleted);
-               LASSERT((route->ksnr_connected & (1 << conn->ksnc_type)) != 0);
+               LASSERT((route->ksnr_connected & BIT(conn->ksnc_type)) != 0);
 
                conn2 = NULL;
                list_for_each(tmp, &peer_ni->ksnp_conns) {
@@ -1425,7 +1492,7 @@ ksocknal_close_conn_locked(struct ksock_conn *conn, int error)
                        conn2 = NULL;
                }
                if (conn2 == NULL)
-                       route->ksnr_connected &= ~(1 << conn->ksnc_type);
+                       route->ksnr_connected &= ~BIT(conn->ksnc_type);
 
                conn->ksnc_route = NULL;
 
@@ -1628,7 +1695,7 @@ ksocknal_destroy_conn(struct ksock_conn *conn)
         switch (conn->ksnc_rx_state) {
         case SOCKNAL_RX_LNET_PAYLOAD:
                 last_rcv = conn->ksnc_rx_deadline -
-                          lnet_get_lnd_timeout();
+                          ksocknal_timeout();
                CERROR("Completing partial receive from %s[%d], "
                       "ip %pI4h:%d, with error, wanted: %d, left: %d, "
                       "last alive is %lld secs ago\n",
@@ -1888,6 +1955,7 @@ ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
        } else {
                iface = &net->ksnn_interfaces[net->ksnn_ninterfaces++];
 
+               iface->ksni_index = ksocknal_ip2index(ipaddress, ni);
                iface->ksni_ipaddr = ipaddress;
                iface->ksni_netmask = netmask;
                iface->ksni_nroutes = 0;
@@ -1903,7 +1971,8 @@ ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
                                                   struct ksock_route,
                                                   ksnr_list);
 
-                               if (route->ksnr_myipaddr == ipaddress)
+                               if (route->ksnr_myiface ==
+                                           iface->ksni_index)
                                        iface->ksni_nroutes++;
                        }
                }
@@ -1920,7 +1989,8 @@ ksocknal_add_interface(struct lnet_ni *ni, __u32 ipaddress, __u32 netmask)
 }
 
 static void
-ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
+ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni,
+                                  __u32 ipaddr, int index)
 {
        struct list_head *tmp;
        struct list_head *nxt;
@@ -1941,16 +2011,16 @@ ksocknal_peer_del_interface_locked(struct ksock_peer_ni *peer_ni, __u32 ipaddr)
        list_for_each_safe(tmp, nxt, &peer_ni->ksnp_routes) {
                route = list_entry(tmp, struct ksock_route, ksnr_list);
 
-                if (route->ksnr_myipaddr != ipaddr)
-                        continue;
+               if (route->ksnr_myiface != index)
+                       continue;
 
-                if (route->ksnr_share_count != 0) {
-                        /* Manually created; keep, but unbind */
-                        route->ksnr_myipaddr = 0;
-                } else {
-                        ksocknal_del_route_locked(route);
-                }
-        }
+               if (route->ksnr_share_count != 0) {
+                       /* Manually created; keep, but unbind */
+                       route->ksnr_myiface = -1;
+               } else {
+                       ksocknal_del_route_locked(route);
+               }
+       }
 
        list_for_each_safe(tmp, nxt, &peer_ni->ksnp_conns) {
                conn = list_entry(tmp, struct ksock_conn, ksnc_list);
@@ -1968,9 +2038,12 @@ ksocknal_del_interface(struct lnet_ni *ni, __u32 ipaddress)
        struct hlist_node *nxt;
        struct ksock_peer_ni *peer_ni;
        u32 this_ip;
+       int index;
        int i;
        int j;
 
+       index = ksocknal_ip2index(ipaddress, ni);
+
        write_lock_bh(&ksocknal_data.ksnd_global_lock);
 
        for (i = 0; i < net->ksnn_ninterfaces; i++) {
@@ -1993,7 +2066,8 @@ ksocknal_del_interface(struct lnet_ni *ni, __u32 ipaddress)
                        if (peer_ni->ksnp_ni != ni)
                                continue;
 
-                       ksocknal_peer_del_interface_locked(peer_ni, this_ip);
+                       ksocknal_peer_del_interface_locked(peer_ni,
+                                                          this_ip, index);
                }
        }
 
@@ -2205,25 +2279,16 @@ ksocknal_base_shutdown(void)
                                        wake_up_all(&sched->kss_waitq);
                }
 
-               i = 4;
-               read_lock(&ksocknal_data.ksnd_global_lock);
-               while (ksocknal_data.ksnd_nthreads != 0) {
-                       i++;
-                       /* power of 2? */
-                       CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET,
-                               "waiting for %d threads to terminate\n",
-                               ksocknal_data.ksnd_nthreads);
-                       read_unlock(&ksocknal_data.ksnd_global_lock);
-                       schedule_timeout_uninterruptible(cfs_time_seconds(1));
-                       read_lock(&ksocknal_data.ksnd_global_lock);
-               }
-               read_unlock(&ksocknal_data.ksnd_global_lock);
+               wait_var_event_warning(&ksocknal_data.ksnd_nthreads,
+                                      ksocknal_data.ksnd_nthreads == 0,
+                                      "waiting for %d threads to terminate\n",
+                                      ksocknal_data.ksnd_nthreads);
 
-                ksocknal_free_buffers();
+               ksocknal_free_buffers();
 
-                ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
-                break;
-        }
+               ksocknal_data.ksnd_init = SOCKNAL_INIT_NOTHING;
+               break;
+       }
 
        CDEBUG(D_MALLOC, "after NAL cleanup: kmem %d\n",
               atomic_read (&libcfs_kmemory));
@@ -2352,7 +2417,7 @@ ksocknal_base_startup(void)
         return -ENETDOWN;
 }
 
-static void
+static int
 ksocknal_debug_peerhash(struct lnet_ni *ni)
 {
        struct ksock_peer_ni *peer_ni;
@@ -2394,6 +2459,7 @@ ksocknal_debug_peerhash(struct lnet_ni *ni)
        }
 
        read_unlock(&ksocknal_data.ksnd_global_lock);
+       return 0;
 }
 
 void
@@ -2416,16 +2482,13 @@ ksocknal_shutdown(struct lnet_ni *ni)
        ksocknal_del_peer(ni, anyid, 0);
 
        /* Wait for all peer_ni state to clean up */
-       i = 2;
-       while (atomic_read(&net->ksnn_npeers) > SOCKNAL_SHUTDOWN_BIAS) {
-               i++;
-               CDEBUG(((i & (-i)) == i) ? D_WARNING : D_NET, /* power of 2? */
-                      "waiting for %d peers to disconnect\n",
-                      atomic_read(&net->ksnn_npeers) - SOCKNAL_SHUTDOWN_BIAS);
-               schedule_timeout_uninterruptible(cfs_time_seconds(1));
-
-               ksocknal_debug_peerhash(ni);
-       }
+       wait_var_event_warning(&net->ksnn_npeers,
+                              atomic_read(&net->ksnn_npeers) ==
+                              SOCKNAL_SHUTDOWN_BIAS,
+                              "waiting for %d peers to disconnect\n",
+                              ksocknal_debug_peerhash(ni) +
+                              atomic_read(&net->ksnn_npeers) -
+                              SOCKNAL_SHUTDOWN_BIAS);
 
        for (i = 0; i < net->ksnn_ninterfaces; i++) {
                LASSERT(net->ksnn_interfaces[i].ksni_npeers == 0);
@@ -2435,9 +2498,9 @@ ksocknal_shutdown(struct lnet_ni *ni)
        list_del(&net->ksnn_list);
        LIBCFS_FREE(net, sizeof(*net));
 
-        ksocknal_data.ksnd_nnets--;
-        if (ksocknal_data.ksnd_nnets == 0)
-                ksocknal_base_shutdown();
+       ksocknal_data.ksnd_nnets--;
+       if (ksocknal_data.ksnd_nnets == 0)
+               ksocknal_base_shutdown();
 }
 
 static int
@@ -2612,6 +2675,7 @@ ksocknal_startup(struct lnet_ni *ni)
                net->ksnn_ninterfaces = 1;
                ni->ni_dev_cpt = ifaces[0].li_cpt;
                ksi->ksni_ipaddr = ifaces[0].li_ipaddr;
+               ksi->ksni_index = ksocknal_ip2index(ksi->ksni_ipaddr, ni);
                ksi->ksni_netmask = ifaces[0].li_netmask;
                strlcpy(ksi->ksni_name, ifaces[0].li_name,
                        sizeof(ksi->ksni_name));
@@ -2651,6 +2715,8 @@ ksocknal_startup(struct lnet_ni *ni)
                                ksi = &net->ksnn_interfaces[j];
                                ni->ni_dev_cpt = ifaces[j].li_cpt;
                                ksi->ksni_ipaddr = ifaces[j].li_ipaddr;
+                               ksi->ksni_index =
+                                 ksocknal_ip2index(ksi->ksni_ipaddr, ni);
                                ksi->ksni_netmask = ifaces[j].li_netmask;
                                strlcpy(ksi->ksni_name, ifaces[j].li_name,
                                        sizeof(ksi->ksni_name));