Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6a413f5c authored by David S. Miller's avatar David S. Miller
Browse files

Merge branch 'ipv6-addrconf-hash-improvements-and-cleanups'



Eric Dumazet says:

====================
ipv6: addrconf: hash improvements and cleanups

Remove unecessary BH blocking, and bring IPv6 addrconf to modern world,
with per netns hash perturbation and decent hash size.
====================

Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents fa6e23e2 4e5f47ab
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -58,7 +58,7 @@ struct in6_validator_info {
	struct netlink_ext_ack	*extack;
};

#define IN6_ADDR_HSIZE_SHIFT	4
#define IN6_ADDR_HSIZE_SHIFT	8
#define IN6_ADDR_HSIZE		(1 << IN6_ADDR_HSIZE_SHIFT)

int addrconf_init(void);
+45 −49
Original line number Diff line number Diff line
@@ -192,8 +192,6 @@ static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);

static void inet6_prefix_notify(int event, struct inet6_dev *idev,
				struct prefix_info *pinfo);
static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
			       struct net_device *dev);

static struct ipv6_devconf ipv6_devconf __read_mostly = {
	.forwarding		= 0,
@@ -952,30 +950,44 @@ ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
	list_add_tail_rcu(&ifp->if_list, p);
}

static u32 inet6_addr_hash(const struct in6_addr *addr)
static u32 inet6_addr_hash(const struct net *net, const struct in6_addr *addr)
{
	return hash_32(ipv6_addr_hash(addr), IN6_ADDR_HSIZE_SHIFT);
	u32 val = ipv6_addr_hash(addr) ^ net_hash_mix(net);

	return hash_32(val, IN6_ADDR_HSIZE_SHIFT);
}

static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
			       struct net_device *dev, unsigned int hash)
{
	struct inet6_ifaddr *ifp;

	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
		if (!net_eq(dev_net(ifp->idev->dev), net))
			continue;
		if (ipv6_addr_equal(&ifp->addr, addr)) {
			if (!dev || ifp->idev->dev == dev)
				return true;
		}
	}
	return false;
}

static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
{
	unsigned int hash;
	unsigned int hash = inet6_addr_hash(dev_net(dev), &ifa->addr);
	int err = 0;

	spin_lock(&addrconf_hash_lock);

	/* Ignore adding duplicate addresses on an interface */
	if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev)) {
	if (ipv6_chk_same_addr(dev_net(dev), &ifa->addr, dev, hash)) {
		ADBG("ipv6_add_addr: already assigned\n");
		err = -EEXIST;
		goto out;
	}

	/* Add to big hash table */
	hash = inet6_addr_hash(&ifa->addr);
	} else {
		hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
	}

out:
	spin_unlock(&addrconf_hash_lock);

	return err;
@@ -1828,11 +1840,11 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
			    const struct net_device *dev, int strict,
			    u32 banned_flags)
{
	unsigned int hash = inet6_addr_hash(net, addr);
	struct inet6_ifaddr *ifp;
	unsigned int hash = inet6_addr_hash(addr);
	u32 ifp_flags;

	rcu_read_lock_bh();
	rcu_read_lock();
	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
		if (!net_eq(dev_net(ifp->idev->dev), net))
			continue;
@@ -1846,32 +1858,16 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
		    !(ifp_flags&banned_flags) &&
		    (!dev || ifp->idev->dev == dev ||
		     !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
			rcu_read_unlock_bh();
			rcu_read_unlock();
			return 1;
		}
	}

	rcu_read_unlock_bh();
	rcu_read_unlock();
	return 0;
}
EXPORT_SYMBOL(ipv6_chk_addr_and_flags);

static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
			       struct net_device *dev)
{
	unsigned int hash = inet6_addr_hash(addr);
	struct inet6_ifaddr *ifp;

	hlist_for_each_entry(ifp, &inet6_addr_lst[hash], addr_lst) {
		if (!net_eq(dev_net(ifp->idev->dev), net))
			continue;
		if (ipv6_addr_equal(&ifp->addr, addr)) {
			if (!dev || ifp->idev->dev == dev)
				return true;
		}
	}
	return false;
}

/* Compares an address/prefix_len with addresses on device @dev.
 * If one is found it returns true.
@@ -1923,11 +1919,11 @@ EXPORT_SYMBOL(ipv6_chk_prefix);
struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
				     struct net_device *dev, int strict)
{
	unsigned int hash = inet6_addr_hash(net, addr);
	struct inet6_ifaddr *ifp, *result = NULL;
	unsigned int hash = inet6_addr_hash(addr);

	rcu_read_lock_bh();
	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
	rcu_read_lock();
	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
		if (!net_eq(dev_net(ifp->idev->dev), net))
			continue;
		if (ipv6_addr_equal(&ifp->addr, addr)) {
@@ -1939,7 +1935,7 @@ struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *add
			}
		}
	}
	rcu_read_unlock_bh();
	rcu_read_unlock();

	return result;
}
@@ -4101,9 +4097,9 @@ struct if6_iter_state {

static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
{
	struct inet6_ifaddr *ifa = NULL;
	struct if6_iter_state *state = seq->private;
	struct net *net = seq_file_net(seq);
	struct inet6_ifaddr *ifa = NULL;
	int p = 0;

	/* initial bucket if pos is 0 */
@@ -4113,7 +4109,7 @@ static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos)
	}

	for (; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
		hlist_for_each_entry_rcu_bh(ifa, &inet6_addr_lst[state->bucket],
		hlist_for_each_entry_rcu(ifa, &inet6_addr_lst[state->bucket],
					 addr_lst) {
			if (!net_eq(dev_net(ifa->idev->dev), net))
				continue;
@@ -4139,7 +4135,7 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
	struct if6_iter_state *state = seq->private;
	struct net *net = seq_file_net(seq);

	hlist_for_each_entry_continue_rcu_bh(ifa, addr_lst) {
	hlist_for_each_entry_continue_rcu(ifa, addr_lst) {
		if (!net_eq(dev_net(ifa->idev->dev), net))
			continue;
		state->offset++;
@@ -4148,7 +4144,7 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,

	while (++state->bucket < IN6_ADDR_HSIZE) {
		state->offset = 0;
		hlist_for_each_entry_rcu_bh(ifa,
		hlist_for_each_entry_rcu(ifa,
				     &inet6_addr_lst[state->bucket], addr_lst) {
			if (!net_eq(dev_net(ifa->idev->dev), net))
				continue;
@@ -4161,9 +4157,9 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
}

static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
	__acquires(rcu_bh)
	__acquires(rcu)
{
	rcu_read_lock_bh();
	rcu_read_lock();
	return if6_get_first(seq, *pos);
}

@@ -4177,9 +4173,9 @@ static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
}

static void if6_seq_stop(struct seq_file *seq, void *v)
	__releases(rcu_bh)
	__releases(rcu)
{
	rcu_read_unlock_bh();
	rcu_read_unlock();
}

static int if6_seq_show(struct seq_file *seq, void *v)
@@ -4248,12 +4244,12 @@ void if6_proc_exit(void)
/* Check if address is a home address configured on any interface. */
int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
{
	int ret = 0;
	unsigned int hash = inet6_addr_hash(net, addr);
	struct inet6_ifaddr *ifp = NULL;
	unsigned int hash = inet6_addr_hash(addr);
	int ret = 0;

	rcu_read_lock_bh();
	hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) {
	rcu_read_lock();
	hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
		if (!net_eq(dev_net(ifp->idev->dev), net))
			continue;
		if (ipv6_addr_equal(&ifp->addr, addr) &&
@@ -4262,7 +4258,7 @@ int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
			break;
		}
	}
	rcu_read_unlock_bh();
	rcu_read_unlock();
	return ret;
}
#endif