diff mbox series

[RFC,net-next,17/22] nexthop: Replay nexthops when registering a notifier

Message ID 20200908091037.2709823-18-idosch@idosch.org
State Superseded
Headers show
Series nexthop: Add support for nexthop objects offload | expand

Commit Message

Ido Schimmel Sept. 8, 2020, 9:10 a.m. UTC
From: Ido Schimmel <idosch@nvidia.com>

When registering a new notifier to the nexthop notification chain,
replay all the existing nexthops to the new notifier so that it will
have a complete picture of the available nexthops.

Signed-off-by: Ido Schimmel <idosch@nvidia.com>
---
 net/ipv4/nexthop.c | 54 ++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)

Comments

David Ahern Sept. 8, 2020, 3:37 p.m. UTC | #1
On 9/8/20 3:10 AM, Ido Schimmel wrote:
> From: Ido Schimmel <idosch@nvidia.com>
> 
> When registering a new notifier to the nexthop notification chain,
> replay all the existing nexthops to the new notifier so that it will
> have a complete picture of the available nexthops.
> 
> Signed-off-by: Ido Schimmel <idosch@nvidia.com>
> ---
>  net/ipv4/nexthop.c | 54 ++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 52 insertions(+), 2 deletions(-)
> 
> diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
> index b40c343ca969..6505a0a28df2 100644
> --- a/net/ipv4/nexthop.c
> +++ b/net/ipv4/nexthop.c
> @@ -156,6 +156,27 @@ static int call_nexthop_notifiers(struct net *net,
>  	return notifier_to_errno(err);
>  }
>  
> +static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
> +				 enum nexthop_event_type event_type,
> +				 struct nexthop *nh,
> +				 struct netlink_ext_ack *extack)
> +{
> +	struct nh_notifier_info info = {
> +		.net = net,
> +		.extack = extack,
> +	};
> +	int err;
> +
> +	err = nh_notifier_info_init(&info, nh);
> +	if (err)
> +		return err;
> +
> +	err = nb->notifier_call(nb, event_type, &info);
> +	nh_notifier_info_fini(&info);
> +
> +	return notifier_to_errno(err);
> +}
> +
>  static unsigned int nh_dev_hashfn(unsigned int val)
>  {
>  	unsigned int mask = NH_DEV_HASHSIZE - 1;
> @@ -2116,11 +2137,40 @@ static struct notifier_block nh_netdev_notifier = {
>  	.notifier_call = nh_netdev_event,
>  };
>  
> +static int nexthops_dump(struct net *net, struct notifier_block *nb,
> +			 struct netlink_ext_ack *extack)
> +{
> +	struct rb_root *root = &net->nexthop.rb_root;
> +	struct rb_node *node;
> +	int err = 0;
> +
> +	for (node = rb_first(root); node; node = rb_next(node)) {
> +		struct nexthop *nh;
> +
> +		nh = rb_entry(node, struct nexthop, rb_node);
> +		err = call_nexthop_notifier(nb, net, NEXTHOP_EVENT_REPLACE, nh,
> +					    extack);
> +		if (err)
> +			break;
> +	}
> +
> +	return err;
> +}
> +
>  int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
>  			      struct netlink_ext_ack *extack)
>  {
> -	return blocking_notifier_chain_register(&net->nexthop.notifier_chain,
> -						nb);
> +	int err;
> +
> +	rtnl_lock();
> +	err = nexthops_dump(net, nb, extack);

can the unlock be moved here? register function below should not need it.

> +	if (err)
> +		goto unlock;
> +	err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
> +					       nb);
> +unlock:
> +	rtnl_unlock();
> +	return err;
>  }
>  EXPORT_SYMBOL(register_nexthop_notifier);
>  
>
Ido Schimmel Sept. 11, 2020, 4:40 p.m. UTC | #2
On Tue, Sep 08, 2020 at 09:37:10AM -0600, David Ahern wrote:
> On 9/8/20 3:10 AM, Ido Schimmel wrote:
> > From: Ido Schimmel <idosch@nvidia.com>
> > 
> > When registering a new notifier to the nexthop notification chain,
> > replay all the existing nexthops to the new notifier so that it will
> > have a complete picture of the available nexthops.
> > 
> > Signed-off-by: Ido Schimmel <idosch@nvidia.com>
> > ---
> >  net/ipv4/nexthop.c | 54 ++++++++++++++++++++++++++++++++++++++++++++--
> >  1 file changed, 52 insertions(+), 2 deletions(-)
> > 
> > diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
> > index b40c343ca969..6505a0a28df2 100644
> > --- a/net/ipv4/nexthop.c
> > +++ b/net/ipv4/nexthop.c
> > @@ -156,6 +156,27 @@ static int call_nexthop_notifiers(struct net *net,
> >  	return notifier_to_errno(err);
> >  }
> >  
> > +static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
> > +				 enum nexthop_event_type event_type,
> > +				 struct nexthop *nh,
> > +				 struct netlink_ext_ack *extack)
> > +{
> > +	struct nh_notifier_info info = {
> > +		.net = net,
> > +		.extack = extack,
> > +	};
> > +	int err;
> > +
> > +	err = nh_notifier_info_init(&info, nh);
> > +	if (err)
> > +		return err;
> > +
> > +	err = nb->notifier_call(nb, event_type, &info);
> > +	nh_notifier_info_fini(&info);
> > +
> > +	return notifier_to_errno(err);
> > +}
> > +
> >  static unsigned int nh_dev_hashfn(unsigned int val)
> >  {
> >  	unsigned int mask = NH_DEV_HASHSIZE - 1;
> > @@ -2116,11 +2137,40 @@ static struct notifier_block nh_netdev_notifier = {
> >  	.notifier_call = nh_netdev_event,
> >  };
> >  
> > +static int nexthops_dump(struct net *net, struct notifier_block *nb,
> > +			 struct netlink_ext_ack *extack)
> > +{
> > +	struct rb_root *root = &net->nexthop.rb_root;
> > +	struct rb_node *node;
> > +	int err = 0;
> > +
> > +	for (node = rb_first(root); node; node = rb_next(node)) {
> > +		struct nexthop *nh;
> > +
> > +		nh = rb_entry(node, struct nexthop, rb_node);
> > +		err = call_nexthop_notifier(nb, net, NEXTHOP_EVENT_REPLACE, nh,
> > +					    extack);
> > +		if (err)
> > +			break;
> > +	}
> > +
> > +	return err;
> > +}
> > +
> >  int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
> >  			      struct netlink_ext_ack *extack)
> >  {
> > -	return blocking_notifier_chain_register(&net->nexthop.notifier_chain,
> > -						nb);
> > +	int err;
> > +
> > +	rtnl_lock();
> > +	err = nexthops_dump(net, nb, extack);
> 
> can the unlock be moved here? register function below should not need it.

It can result in this unlikely race:

<t0> - rtnl_lock(); nexthops_dump(); rtnl_unlock()
<t1> - Nexthop is added / deleted
<t2> - blocking_notifier_chain_register()

It is possible to flip the order:

<t0> - blocking_notifier_chain_register()
<t1> - rtnl_lock(); nexthops_dump(); rtnl_unlock()

Worst case:

<t0> - blocking_notifier_chain_register()
<t1> - Nexthop is added / deleted
<t2> - rtnl_lock(); nexthops_dump(); rtnl_unlock()

Which is OK. If we get a delete notification for a nexthop we don't
know, we ignore it. If we get two replace notifications for the same
nexthop we just "update" it.

> 
> > +	if (err)
> > +		goto unlock;
> > +	err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
> > +					       nb);
> > +unlock:
> > +	rtnl_unlock();
> > +	return err;
> >  }
> >  EXPORT_SYMBOL(register_nexthop_notifier);
> >  
> > 
>
diff mbox series

Patch

diff --git a/net/ipv4/nexthop.c b/net/ipv4/nexthop.c
index b40c343ca969..6505a0a28df2 100644
--- a/net/ipv4/nexthop.c
+++ b/net/ipv4/nexthop.c
@@ -156,6 +156,27 @@  static int call_nexthop_notifiers(struct net *net,
 	return notifier_to_errno(err);
 }
 
+static int call_nexthop_notifier(struct notifier_block *nb, struct net *net,
+				 enum nexthop_event_type event_type,
+				 struct nexthop *nh,
+				 struct netlink_ext_ack *extack)
+{
+	struct nh_notifier_info info = {
+		.net = net,
+		.extack = extack,
+	};
+	int err;
+
+	err = nh_notifier_info_init(&info, nh);
+	if (err)
+		return err;
+
+	err = nb->notifier_call(nb, event_type, &info);
+	nh_notifier_info_fini(&info);
+
+	return notifier_to_errno(err);
+}
+
 static unsigned int nh_dev_hashfn(unsigned int val)
 {
 	unsigned int mask = NH_DEV_HASHSIZE - 1;
@@ -2116,11 +2137,40 @@  static struct notifier_block nh_netdev_notifier = {
 	.notifier_call = nh_netdev_event,
 };
 
+static int nexthops_dump(struct net *net, struct notifier_block *nb,
+			 struct netlink_ext_ack *extack)
+{
+	struct rb_root *root = &net->nexthop.rb_root;
+	struct rb_node *node;
+	int err = 0;
+
+	for (node = rb_first(root); node; node = rb_next(node)) {
+		struct nexthop *nh;
+
+		nh = rb_entry(node, struct nexthop, rb_node);
+		err = call_nexthop_notifier(nb, net, NEXTHOP_EVENT_REPLACE, nh,
+					    extack);
+		if (err)
+			break;
+	}
+
+	return err;
+}
+
 int register_nexthop_notifier(struct net *net, struct notifier_block *nb,
 			      struct netlink_ext_ack *extack)
 {
-	return blocking_notifier_chain_register(&net->nexthop.notifier_chain,
-						nb);
+	int err;
+
+	rtnl_lock();
+	err = nexthops_dump(net, nb, extack);
+	if (err)
+		goto unlock;
+	err = blocking_notifier_chain_register(&net->nexthop.notifier_chain,
+					       nb);
+unlock:
+	rtnl_unlock();
+	return err;
 }
 EXPORT_SYMBOL(register_nexthop_notifier);