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

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
...@@ -58,7 +58,7 @@ struct in6_validator_info { ...@@ -58,7 +58,7 @@ struct in6_validator_info {
struct netlink_ext_ack *extack; 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) #define IN6_ADDR_HSIZE (1 << IN6_ADDR_HSIZE_SHIFT)
int addrconf_init(void); int addrconf_init(void);
......
...@@ -192,8 +192,6 @@ static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); ...@@ -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, static void inet6_prefix_notify(int event, struct inet6_dev *idev,
struct prefix_info *pinfo); 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 = { static struct ipv6_devconf ipv6_devconf __read_mostly = {
.forwarding = 0, .forwarding = 0,
...@@ -952,30 +950,44 @@ ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp) ...@@ -952,30 +950,44 @@ ipv6_link_dev_addr(struct inet6_dev *idev, struct inet6_ifaddr *ifp)
list_add_tail_rcu(&ifp->if_list, p); 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) 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; int err = 0;
spin_lock(&addrconf_hash_lock); spin_lock(&addrconf_hash_lock);
/* Ignore adding duplicate addresses on an interface */ /* 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"); ADBG("ipv6_add_addr: already assigned\n");
err = -EEXIST; err = -EEXIST;
goto out; } else {
hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
} }
/* Add to big hash table */
hash = inet6_addr_hash(&ifa->addr);
hlist_add_head_rcu(&ifa->addr_lst, &inet6_addr_lst[hash]);
out:
spin_unlock(&addrconf_hash_lock); spin_unlock(&addrconf_hash_lock);
return err; return err;
...@@ -1828,11 +1840,11 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, ...@@ -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, const struct net_device *dev, int strict,
u32 banned_flags) u32 banned_flags)
{ {
unsigned int hash = inet6_addr_hash(net, addr);
struct inet6_ifaddr *ifp; struct inet6_ifaddr *ifp;
unsigned int hash = inet6_addr_hash(addr);
u32 ifp_flags; u32 ifp_flags;
rcu_read_lock_bh(); rcu_read_lock();
hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) { hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
if (!net_eq(dev_net(ifp->idev->dev), net)) if (!net_eq(dev_net(ifp->idev->dev), net))
continue; continue;
...@@ -1846,32 +1858,16 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr, ...@@ -1846,32 +1858,16 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
!(ifp_flags&banned_flags) && !(ifp_flags&banned_flags) &&
(!dev || ifp->idev->dev == dev || (!dev || ifp->idev->dev == dev ||
!(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
rcu_read_unlock_bh(); rcu_read_unlock();
return 1; return 1;
} }
} }
rcu_read_unlock_bh(); rcu_read_unlock();
return 0; return 0;
} }
EXPORT_SYMBOL(ipv6_chk_addr_and_flags); 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. /* Compares an address/prefix_len with addresses on device @dev.
* If one is found it returns true. * If one is found it returns true.
...@@ -1923,11 +1919,11 @@ EXPORT_SYMBOL(ipv6_chk_prefix); ...@@ -1923,11 +1919,11 @@ EXPORT_SYMBOL(ipv6_chk_prefix);
struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr, struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *addr,
struct net_device *dev, int strict) struct net_device *dev, int strict)
{ {
unsigned int hash = inet6_addr_hash(net, addr);
struct inet6_ifaddr *ifp, *result = NULL; struct inet6_ifaddr *ifp, *result = NULL;
unsigned int hash = inet6_addr_hash(addr);
rcu_read_lock_bh(); rcu_read_lock();
hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
if (!net_eq(dev_net(ifp->idev->dev), net)) if (!net_eq(dev_net(ifp->idev->dev), net))
continue; continue;
if (ipv6_addr_equal(&ifp->addr, addr)) { 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 ...@@ -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; return result;
} }
...@@ -4101,9 +4097,9 @@ struct if6_iter_state { ...@@ -4101,9 +4097,9 @@ struct if6_iter_state {
static struct inet6_ifaddr *if6_get_first(struct seq_file *seq, loff_t pos) 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 if6_iter_state *state = seq->private;
struct net *net = seq_file_net(seq); struct net *net = seq_file_net(seq);
struct inet6_ifaddr *ifa = NULL;
int p = 0; int p = 0;
/* initial bucket if pos is 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) ...@@ -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) { 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) { addr_lst) {
if (!net_eq(dev_net(ifa->idev->dev), net)) if (!net_eq(dev_net(ifa->idev->dev), net))
continue; continue;
...@@ -4139,7 +4135,7 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, ...@@ -4139,7 +4135,7 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
struct if6_iter_state *state = seq->private; struct if6_iter_state *state = seq->private;
struct net *net = seq_file_net(seq); 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)) if (!net_eq(dev_net(ifa->idev->dev), net))
continue; continue;
state->offset++; state->offset++;
...@@ -4148,7 +4144,7 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, ...@@ -4148,7 +4144,7 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq,
while (++state->bucket < IN6_ADDR_HSIZE) { while (++state->bucket < IN6_ADDR_HSIZE) {
state->offset = 0; state->offset = 0;
hlist_for_each_entry_rcu_bh(ifa, hlist_for_each_entry_rcu(ifa,
&inet6_addr_lst[state->bucket], addr_lst) { &inet6_addr_lst[state->bucket], addr_lst) {
if (!net_eq(dev_net(ifa->idev->dev), net)) if (!net_eq(dev_net(ifa->idev->dev), net))
continue; continue;
...@@ -4161,9 +4157,9 @@ static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, ...@@ -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) 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); return if6_get_first(seq, *pos);
} }
...@@ -4177,9 +4173,9 @@ static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *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) 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) static int if6_seq_show(struct seq_file *seq, void *v)
...@@ -4248,12 +4244,12 @@ void if6_proc_exit(void) ...@@ -4248,12 +4244,12 @@ void if6_proc_exit(void)
/* Check if address is a home address configured on any interface. */ /* 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 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; struct inet6_ifaddr *ifp = NULL;
unsigned int hash = inet6_addr_hash(addr); int ret = 0;
rcu_read_lock_bh(); rcu_read_lock();
hlist_for_each_entry_rcu_bh(ifp, &inet6_addr_lst[hash], addr_lst) { hlist_for_each_entry_rcu(ifp, &inet6_addr_lst[hash], addr_lst) {
if (!net_eq(dev_net(ifp->idev->dev), net)) if (!net_eq(dev_net(ifp->idev->dev), net))
continue; continue;
if (ipv6_addr_equal(&ifp->addr, addr) && if (ipv6_addr_equal(&ifp->addr, addr) &&
...@@ -4262,7 +4258,7 @@ int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr) ...@@ -4262,7 +4258,7 @@ int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr)
break; break;
} }
} }
rcu_read_unlock_bh(); rcu_read_unlock();
return ret; return ret;
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment