Whamcloud - gitweb
LU-10391 socklnd: use interface index to track local addr
[fs/lustre-release.git] / lnet / klnds / socklnd / socklnd.c
index 094e840..5a907fc 100644 (file)
@@ -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
@@ -288,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;
                }
        }
@@ -303,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
@@ -408,10 +467,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--;
        }
 
@@ -905,7 +964,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;
@@ -913,34 +972,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
@@ -1889,6 +1948,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;
@@ -1904,7 +1964,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++;
                        }
                }
@@ -1921,7 +1982,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;
@@ -1942,16 +2004,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);
@@ -1969,9 +2031,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++) {
@@ -1994,7 +2059,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);
                }
        }