[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230509100939.760867-2-jiri@resnulli.us>
Date: Tue, 9 May 2023 12:09:37 +0200
From: Jiri Pirko <jiri@...nulli.us>
To: netdev@...r.kernel.org
Cc: kuba@...nel.org,
pabeni@...hat.com,
davem@...emloft.net,
edumazet@...gle.com,
jacob.e.keller@...el.com,
saeedm@...dia.com,
moshe@...dia.com
Subject: [patch net 1/3] net: allow to ask per-net netdevice notifier to follow netdev dynamically
From: Jiri Pirko <jiri@...dia.com>
Currently, it is possible to register netdev notifier to get only
events related to a selected namespace. This could be done by:
register_netdevice_notifier_net()
Another extension which currently exists is to register netdev notifier
that receives events related to a namespace, where a netdev is. The
notifier moves from namespace to namespace with the selected netdev.
This could be done by:
register_netdevice_notifier_dev_net()
Devlink has a usecase to monitor a namespace and whenever certain netdev
appears in this namespace, it needs to get notifications even in case
netdev moves to a different namespace. It's basically a combination of
the two described above.
Introduce a pair of functions netdev_net_notifier_follow() and
netdev_net_notifier_unfollow() to be called on previously registered
per-net notifier asking to follow the given netdev.
Signed-off-by: Jiri Pirko <jiri@...dia.com>
---
include/linux/netdevice.h | 6 ++++++
net/core/dev.c | 34 +++++++++++++++++++++++++++++-----
2 files changed, 35 insertions(+), 5 deletions(-)
diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index 08fbd4622ccf..63376dad8464 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -2890,6 +2890,12 @@ int unregister_netdevice_notifier(struct notifier_block *nb);
int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb);
int unregister_netdevice_notifier_net(struct net *net,
struct notifier_block *nb);
+void netdev_net_notifier_follow(struct net_device *dev,
+ struct notifier_block *nb,
+ struct netdev_net_notifier *nn);
+void netdev_net_notifier_unfollow(struct net_device *dev,
+ struct netdev_net_notifier *nn,
+ struct net *net);
int register_netdevice_notifier_dev_net(struct net_device *dev,
struct notifier_block *nb,
struct netdev_net_notifier *nn);
diff --git a/net/core/dev.c b/net/core/dev.c
index 735096d42c1d..3458ed8f98f2 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1868,6 +1868,32 @@ static void __move_netdevice_notifier_net(struct net *src_net,
__register_netdevice_notifier_net(dst_net, nb, true);
}
+void netdev_net_notifier_follow(struct net_device *dev,
+ struct notifier_block *nb,
+ struct netdev_net_notifier *nn)
+{
+ ASSERT_RTNL();
+ nn->nb = nb;
+ list_add(&nn->list, &dev->net_notifier_list);
+}
+EXPORT_SYMBOL(netdev_net_notifier_follow);
+
+static void __netdev_net_notifier_unfollow(struct netdev_net_notifier *nn)
+{
+ list_del(&nn->list);
+}
+
+void netdev_net_notifier_unfollow(struct net_device *dev,
+ struct netdev_net_notifier *nn,
+ struct net *net)
+{
+ ASSERT_RTNL();
+ __netdev_net_notifier_unfollow(nn);
+ if (!net_eq(dev_net(dev), net))
+ __move_netdevice_notifier_net(dev_net(dev), net, nn->nb);
+}
+EXPORT_SYMBOL(netdev_net_notifier_unfollow);
+
int register_netdevice_notifier_dev_net(struct net_device *dev,
struct notifier_block *nb,
struct netdev_net_notifier *nn)
@@ -1876,10 +1902,8 @@ int register_netdevice_notifier_dev_net(struct net_device *dev,
rtnl_lock();
err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
- if (!err) {
- nn->nb = nb;
- list_add(&nn->list, &dev->net_notifier_list);
- }
+ if (!err)
+ netdev_net_notifier_follow(dev, nb, nn);
rtnl_unlock();
return err;
}
@@ -1892,7 +1916,7 @@ int unregister_netdevice_notifier_dev_net(struct net_device *dev,
int err;
rtnl_lock();
- list_del(&nn->list);
+ __netdev_net_notifier_unfollow(nn);
err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
rtnl_unlock();
return err;
--
2.39.2
Powered by blists - more mailing lists