diff mbox series

[net,1/3] net: allow to ask per-net netdevice notifier to follow netdev dynamically

Message ID 20230509100939.760867-2-jiri@resnulli.us (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series [net,1/3] net: allow to ask per-net netdevice notifier to follow netdev dynamically | expand

Checks

Context Check Description
netdev/series_format warning Series does not have a cover letter
netdev/tree_selection success Clearly marked for net, async
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 4193 this patch: 4193
netdev/cc_maintainers success CCed 5 of 5 maintainers
netdev/build_clang success Errors and warnings before: 921 this patch: 921
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 4414 this patch: 4414
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 64 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Jiri Pirko May 9, 2023, 10:09 a.m. UTC
From: Jiri Pirko <jiri@nvidia.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@nvidia.com>
---
 include/linux/netdevice.h |  6 ++++++
 net/core/dev.c            | 34 +++++++++++++++++++++++++++++-----
 2 files changed, 35 insertions(+), 5 deletions(-)
diff mbox series

Patch

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;