* 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;
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)
{
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
* 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 *
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;
}
}
}
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
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) {
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);
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--;
}
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;
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
/* 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);
if (!peer_ni->ksnp_closing &&
list_empty(&peer_ni->ksnp_conns) &&
list_empty(&peer_ni->ksnp_routes)) {
- list_add(&zombies, &peer_ni->ksnp_tx_queue);
- list_del_init(&peer_ni->ksnp_tx_queue);
+ list_splice_init(&peer_ni->ksnp_tx_queue, &zombies);
ksocknal_unlink_peer_locked(peer_ni);
}
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) {
conn2 = NULL;
}
if (conn2 == NULL)
- route->ksnr_connected &= ~(1 << conn->ksnc_type);
+ route->ksnr_connected &= ~BIT(conn->ksnc_type);
conn->ksnc_route = NULL;
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",
} 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;
struct ksock_route,
ksnr_list);
- if (route->ksnr_myipaddr == ipaddress)
+ if (route->ksnr_myiface ==
+ iface->ksni_index)
iface->ksni_nroutes++;
}
}
}
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;
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);
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++) {
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);
}
}
spin_lock(&ksocknal_data.ksnd_tx_lock);
if (!list_empty(&ksocknal_data.ksnd_idle_noop_txs)) {
- struct list_head zlist;
+ LIST_HEAD(zlist);
struct ksock_tx *tx;
- list_add(&zlist, &ksocknal_data.ksnd_idle_noop_txs);
- list_del_init(&ksocknal_data.ksnd_idle_noop_txs);
+ list_splice_init(&ksocknal_data.ksnd_idle_noop_txs, &zlist);
spin_unlock(&ksocknal_data.ksnd_tx_lock);
while (!list_empty(&zlist)) {
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);
- set_current_state(TASK_UNINTERRUPTIBLE);
- schedule_timeout(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));
return -ENETDOWN;
}
-static void
+static int
ksocknal_debug_peerhash(struct lnet_ni *ni)
{
struct ksock_peer_ni *peer_ni;
}
read_unlock(&ksocknal_data.ksnd_global_lock);
+ return 0;
}
void
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);
- set_current_state(TASK_UNINTERRUPTIBLE);
- schedule_timeout(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);
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
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));
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));