net: expose devlink port over rtnetlink

Expose devlink port handle related to netdev over rtnetlink. Introduce a
new nested IFLA attribute to carry the info. Call into devlink code to
fill-up the nest with existing devlink attributes that are used over
devlink netlink.

Signed-off-by: Jiri Pirko <jiri@nvidia.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jiri Pirko 2022-11-02 17:02:11 +01:00 committed by Jakub Kicinski
parent 77df1db80d
commit dca56c3038
4 changed files with 73 additions and 0 deletions

View File

@ -1873,6 +1873,9 @@ int devlink_compat_phys_port_name_get(struct net_device *dev,
int devlink_compat_switch_id_get(struct net_device *dev,
struct netdev_phys_item_id *ppid);
int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port);
size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port);
#else
static inline struct devlink *devlink_try_get(struct devlink *devlink)
@ -1909,6 +1912,17 @@ devlink_compat_switch_id_get(struct net_device *dev,
return -EOPNOTSUPP;
}
static inline int
devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
{
return 0;
}
static inline size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
{
return 0;
}
#endif
#endif /* _NET_DEVLINK_H_ */

View File

@ -372,6 +372,8 @@ enum {
IFLA_TSO_MAX_SEGS,
IFLA_ALLMULTI, /* Allmulti count: > 0 means acts ALLMULTI */
IFLA_DEVLINK_PORT,
__IFLA_MAX
};

View File

@ -880,6 +880,24 @@ nla_put_failure:
return -EMSGSIZE;
}
int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
{
if (devlink_nl_put_handle(msg, devlink_port->devlink))
return -EMSGSIZE;
if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
return -EMSGSIZE;
return 0;
}
size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
{
struct devlink *devlink = devlink_port->devlink;
return nla_total_size(strlen(devlink->dev->bus->name) + 1) /* DEVLINK_ATTR_BUS_NAME */
+ nla_total_size(strlen(dev_name(devlink->dev)) + 1) /* DEVLINK_ATTR_DEV_NAME */
+ nla_total_size(4); /* DEVLINK_ATTR_PORT_INDEX */
}
struct devlink_reload_combination {
enum devlink_reload_action action;
enum devlink_reload_limit limit;

View File

@ -53,6 +53,7 @@
#include <net/fib_rules.h>
#include <net/rtnetlink.h>
#include <net/net_namespace.h>
#include <net/devlink.h>
#include "dev.h"
@ -1038,6 +1039,16 @@ static size_t rtnl_proto_down_size(const struct net_device *dev)
return size;
}
static size_t rtnl_devlink_port_size(const struct net_device *dev)
{
size_t size = nla_total_size(0); /* nest IFLA_DEVLINK_PORT */
if (dev->devlink_port)
size += devlink_nl_port_handle_size(dev->devlink_port);
return size;
}
static noinline size_t if_nlmsg_size(const struct net_device *dev,
u32 ext_filter_mask)
{
@ -1091,6 +1102,7 @@ static noinline size_t if_nlmsg_size(const struct net_device *dev,
+ nla_total_size(4) /* IFLA_MAX_MTU */
+ rtnl_prop_list_size(dev)
+ nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
+ rtnl_devlink_port_size(dev)
+ 0;
}
@ -1728,6 +1740,30 @@ nla_put_failure:
return -EMSGSIZE;
}
static int rtnl_fill_devlink_port(struct sk_buff *skb,
const struct net_device *dev)
{
struct nlattr *devlink_port_nest;
int ret;
devlink_port_nest = nla_nest_start(skb, IFLA_DEVLINK_PORT);
if (!devlink_port_nest)
return -EMSGSIZE;
if (dev->devlink_port) {
ret = devlink_nl_port_handle_fill(skb, dev->devlink_port);
if (ret < 0)
goto nest_cancel;
}
nla_nest_end(skb, devlink_port_nest);
return 0;
nest_cancel:
nla_nest_cancel(skb, devlink_port_nest);
return ret;
}
static int rtnl_fill_ifinfo(struct sk_buff *skb,
struct net_device *dev, struct net *src_net,
int type, u32 pid, u32 seq, u32 change,
@ -1865,6 +1901,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb,
dev->dev.parent->bus->name))
goto nla_put_failure;
if (rtnl_fill_devlink_port(skb, dev))
goto nla_put_failure;
nlmsg_end(skb, nlh);
return 0;