[PATCH 1/2] vdpa: support set mac address from vdpa tool

From: Cindy Lu
Date: Tue Jun 11 2024 - 01:36:13 EST


Add new UAPI to support the mac address from vdpa tool
Function vdpa_nl_cmd_dev_config_set_doit() will get the
MAC address from the vdpa tool and then set it to the device.

The usage is: vdpa dev set name vdpa_name mac **:**:**:**:**:**

Here is sample:
root@L1# vdpa -jp dev config show vdpa0
{
"config": {
"vdpa0": {
"mac": "82:4d:e9:5d:d7:e6",
"link ": "up",
"link_announce ": false,
"mtu": 1500
}
}
}

root@L1# vdpa dev set name vdpa0 mac 00:11:22:33:44:55

root@L1# vdpa -jp dev config show vdpa0
{
"config": {
"vdpa0": {
"mac": "00:11:22:33:44:55",
"link ": "up",
"link_announce ": false,
"mtu": 1500
}
}
}

Signed-off-by: Cindy Lu <lulu@xxxxxxxxxx>
---
drivers/vdpa/vdpa.c | 71 +++++++++++++++++++++++++++++++++++++++
include/linux/vdpa.h | 2 ++
include/uapi/linux/vdpa.h | 1 +
3 files changed, 74 insertions(+)

diff --git a/drivers/vdpa/vdpa.c b/drivers/vdpa/vdpa.c
index a7612e0783b3..347ae6e7749d 100644
--- a/drivers/vdpa/vdpa.c
+++ b/drivers/vdpa/vdpa.c
@@ -1149,6 +1149,72 @@ static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info
return err;
}

+static int vdpa_nl_cmd_dev_config_set_doit(struct sk_buff *skb,
+ struct genl_info *info)
+{
+ struct vdpa_dev_set_config set_config = {};
+ struct nlattr **nl_attrs = info->attrs;
+ struct vdpa_mgmt_dev *mdev;
+ const u8 *macaddr;
+ const char *name;
+ int err = 0;
+ struct device *dev;
+ struct vdpa_device *vdev;
+
+ if (!info->attrs[VDPA_ATTR_DEV_NAME])
+ return -EINVAL;
+
+ name = nla_data(info->attrs[VDPA_ATTR_DEV_NAME]);
+
+ down_write(&vdpa_dev_lock);
+ dev = bus_find_device(&vdpa_bus, NULL, name, vdpa_name_match);
+ if (!dev) {
+ NL_SET_ERR_MSG_MOD(info->extack, "device not found");
+ err = -ENODEV;
+ goto dev_err;
+ }
+ vdev = container_of(dev, struct vdpa_device, dev);
+ if (!vdev->mdev) {
+ NL_SET_ERR_MSG_MOD(
+ info->extack,
+ "Fail to find the specified management device");
+ err = -EINVAL;
+ goto mdev_err;
+ }
+ mdev = vdev->mdev;
+ if (nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]) {
+ if (!(mdev->supported_features & BIT_ULL(VIRTIO_NET_F_MAC))) {
+ NL_SET_ERR_MSG_FMT_MOD(
+ info->extack,
+ "Missing features 0x%llx for provided attributes",
+ BIT_ULL(VIRTIO_NET_F_MAC));
+ err = -EINVAL;
+ goto mdev_err;
+ }
+ macaddr = nla_data(nl_attrs[VDPA_ATTR_DEV_NET_CFG_MACADDR]);
+ memcpy(set_config.net.mac, macaddr, ETH_ALEN);
+ set_config.mask |= BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR);
+ if (mdev->ops->set_mac) {
+ err = mdev->ops->set_mac(mdev, vdev, &set_config);
+ } else {
+ NL_SET_ERR_MSG_FMT_MOD(
+ info->extack,
+ "%s device not support set mac address ", name);
+ }
+
+ } else {
+ NL_SET_ERR_MSG_FMT_MOD(info->extack,
+ "%s device not support this config ",
+ name);
+ }
+
+mdev_err:
+ put_device(dev);
+dev_err:
+ up_write(&vdpa_dev_lock);
+ return err;
+}
+
static int vdpa_dev_config_dump(struct device *dev, void *data)
{
struct vdpa_device *vdev = container_of(dev, struct vdpa_device, dev);
@@ -1285,6 +1351,11 @@ static const struct genl_ops vdpa_nl_ops[] = {
.doit = vdpa_nl_cmd_dev_stats_get_doit,
.flags = GENL_ADMIN_PERM,
},
+ {
+ .cmd = VDPA_CMD_DEV_CONFIG_SET,
+ .doit = vdpa_nl_cmd_dev_config_set_doit,
+ .flags = GENL_ADMIN_PERM,
+ },
};

static struct genl_family vdpa_nl_family __ro_after_init = {
diff --git a/include/linux/vdpa.h b/include/linux/vdpa.h
index db15ac07f8a6..c97f4f1da753 100644
--- a/include/linux/vdpa.h
+++ b/include/linux/vdpa.h
@@ -581,6 +581,8 @@ struct vdpa_mgmtdev_ops {
int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
const struct vdpa_dev_set_config *config);
void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
+ int (*set_mac)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev,
+ const struct vdpa_dev_set_config *config);
};

/**
diff --git a/include/uapi/linux/vdpa.h b/include/uapi/linux/vdpa.h
index 54b649ab0f22..53f249fb26bc 100644
--- a/include/uapi/linux/vdpa.h
+++ b/include/uapi/linux/vdpa.h
@@ -19,6 +19,7 @@ enum vdpa_command {
VDPA_CMD_DEV_GET, /* can dump */
VDPA_CMD_DEV_CONFIG_GET, /* can dump */
VDPA_CMD_DEV_VSTATS_GET,
+ VDPA_CMD_DEV_CONFIG_SET,
};

enum vdpa_attr {
--
2.45.0