From: Jiri Pirko <j...@mellanox.com>

Introduce devlink_compat_switch_id_get() helper which fills up switch_id
according to passed netdev pointer. Call it directly from
dev_get_port_parent_id() as a fallback when ndo_get_port_parent_id
is not defined for given netdev.

Signed-off-by: Jiri Pirko <j...@mellanox.com>
---
 include/net/devlink.h |  9 +++++++++
 net/core/dev.c        |  8 ++++++--
 net/core/devlink.c    | 25 +++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/include/net/devlink.h b/include/net/devlink.h
index 1eebbb832cb3..34c78485b050 100644
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -733,6 +733,8 @@ void devlink_compat_running_version(struct net_device *dev,
 int devlink_compat_flash_update(struct net_device *dev, const char *file_name);
 int devlink_compat_phys_port_name_get(struct net_device *dev,
                                      char *name, size_t len);
+int devlink_compat_switch_id_get(struct net_device *dev,
+                                struct netdev_phys_item_id *ppid);
 
 #else
 
@@ -1230,6 +1232,13 @@ devlink_compat_phys_port_name_get(struct net_device *dev,
 {
        return -EOPNOTSUPP;
 }
+
+static inline int
+int devlink_compat_switch_id_get(struct net_device *dev,
+                                struct netdev_phys_item_id *ppid)
+{
+       return -EOPNOTSUPP;
+}
 #endif
 
 #endif /* _NET_DEVLINK_H_ */
diff --git a/net/core/dev.c b/net/core/dev.c
index a614b26b0842..7401b45738f2 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -7894,14 +7894,18 @@ int dev_get_port_parent_id(struct net_device *dev,
        struct netdev_phys_item_id first = { };
        struct net_device *lower_dev;
        struct list_head *iter;
-       int err = -EOPNOTSUPP;
+       int err;
 
        if (ops->ndo_get_port_parent_id)
                return ops->ndo_get_port_parent_id(dev, ppid);
 
-       if (!recurse)
+       err = devlink_compat_switch_id_get(dev, ppid);
+       if (!err || err != -EOPNOTSUPP)
                return err;
 
+       if (!recurse)
+               return -EOPNOTSUPP;
+
        netdev_for_each_lower_dev(dev, lower_dev, iter) {
                err = dev_get_port_parent_id(lower_dev, ppid, recurse);
                if (err)
diff --git a/net/core/devlink.c b/net/core/devlink.c
index 1a011431694a..0964ee319af5 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -6505,6 +6505,31 @@ int devlink_compat_phys_port_name_get(struct net_device 
*dev,
        return err;
 }
 
+int devlink_compat_switch_id_get(struct net_device *dev,
+                                struct netdev_phys_item_id *ppid)
+{
+       struct devlink_port *devlink_port;
+       struct devlink_port_attrs *attrs;
+       int err = -EOPNOTSUPP;
+
+       mutex_lock(&devlink_mutex);
+       devlink_port = netdev_to_devlink_port(dev);
+       if (!devlink_port)
+               goto unlock_list;
+
+       mutex_lock(&devlink_port->devlink->lock);
+       attrs = &devlink_port->attrs;
+       if (attrs->switch_port) {
+               memcpy(ppid, &attrs->switch_id, sizeof(*ppid));
+               err = 0;
+       }
+       mutex_unlock(&devlink_port->devlink->lock);
+unlock_list:
+       mutex_unlock(&devlink_mutex);
+
+       return err;
+}
+
 static int __init devlink_init(void)
 {
        return genl_register_family(&devlink_nl_family);
-- 
2.14.5

Reply via email to