Skip to contentSkip to conversation

vdpa: support set mac address from vdpa tool

PATCH Superseded Cindy Lu 2 participants 6 messages Jul 1, 2024 – Jul 4, 2024

Subsystems (3)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

v2 cover vdpa: support set mac address from vdpa tool

Add support for setting the MAC address using the VDPA tool. This feature will allow setting the MAC address using the VDPA tool. For example, in vdpa_sim_net, the implementation sets the MAC address to the config space. However, for other drivers, they can implement their own function, not limited to the config space.

Changelog v2
- Changed the function name to prevent misunderstanding
- Added check for blk device
- Addressed the comments

Cindy Lu (2):
vdpa: support set mac address from vdpa tool
vdpa_sim_net: Add the support of set mac address

 drivers/vdpa/vdpa.c                  | 73 ++++++++++++++++++++++++++++
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 18 ++++++-
 include/linux/vdpa.h                 |  2 +
 include/uapi/linux/vdpa.h            |  1 +
 4 files changed, 93 insertions(+), 1 deletion(-)
Signature · 1 line
2.45.0
author
#1 lore raw

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

Add new UAPI to support the mac address from vdpa tool Function vdpa_nl_cmd_dev_attr_set_doit() will get the new 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 example:

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
        }
    }
}
---
 drivers/vdpa/vdpa.c       | 73 +++++++++++++++++++++++++++++++++++++++
 include/linux/vdpa.h      |  2 ++
 include/uapi/linux/vdpa.h |  1 +
 3 files changed, 76 insertions(+)
drivers/vdpa/vdpa.c +73 -0
index a7612e0783b3..0b70610a4c7f 100644
@@ -1149,6 +1149,74 @@ static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info
 	return err;
 }
 
+static int vdpa_nl_cmd_dev_attr_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;
+	u64 classes;
+
+	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;
+	classes = vdpa_mgmtdev_get_classes(mdev, NULL);
+	if ((classes & BIT_ULL(VIRTIO_ID_NET)) &&
+	    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->dev_set_attr) {
+			err = mdev->ops->dev_set_attr(mdev, vdev, &set_config);
+		} else {
+			NL_SET_ERR_MSG_FMT_MOD(info->extack,
+					       "features 0x%llx not supported",
+					       BIT_ULL(VIRTIO_NET_F_MAC));
+		}
+
+	} else {
+		NL_SET_ERR_MSG_FMT_MOD(info->extack, "%s device not supported",
+				       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 +1353,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_ATTR_SET,
+		.doit = vdpa_nl_cmd_dev_attr_set_doit,
+		.flags = GENL_ADMIN_PERM,
+	},
 };
 
 static struct genl_family vdpa_nl_family __ro_after_init = {
include/linux/vdpa.h +2 -0
index db15ac07f8a6..e8bb274887ef 100644
@@ -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 (*dev_set_attr)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev,
+			    const struct vdpa_dev_set_config *config);
 };
 
 /**
include/uapi/linux/vdpa.h +1 -0
index 54b649ab0f22..76b3a1fd13f3 100644
@@ -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_ATTR_SET,
 };
 
 enum vdpa_attr {
Signature · 1 line
2.45.0
#2 lore raw
Quoted 137 lines · Cindy Lu

Add new UAPI to support the mac address from vdpa tool Function vdpa_nl_cmd_dev_attr_set_doit() will get the new 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 example:

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
        }
    }
}
---
 drivers/vdpa/vdpa.c       | 73 +++++++++++++++++++++++++++++++++++++++
 include/linux/vdpa.h      |  2 ++
 include/uapi/linux/vdpa.h |  1 +
 3 files changed, 76 insertions(+)
drivers/vdpa/vdpa.c +73 -0
index a7612e0783b3..0b70610a4c7f 100644
@@ -1149,6 +1149,74 @@ static int vdpa_nl_cmd_dev_config_get_doit(struct sk_buff *skb, struct genl_info
        return err;
 }
 
+static int vdpa_nl_cmd_dev_attr_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;
+       u64 classes;
+
+       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;
+       classes = vdpa_mgmtdev_get_classes(mdev, NULL);
+       if ((classes & BIT_ULL(VIRTIO_ID_NET)) &&
+           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->dev_set_attr) {
+                       err = mdev->ops->dev_set_attr(mdev, vdev, &set_config);
+               } else {
+                       NL_SET_ERR_MSG_FMT_MOD(info->extack,
+                                              "features 0x%llx not supported",
+                                              BIT_ULL(VIRTIO_NET_F_MAC));
+               }
+
+       } else {
+               NL_SET_ERR_MSG_FMT_MOD(info->extack, "%s device not supported",
+                                      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 +1353,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_ATTR_SET,
+               .doit = vdpa_nl_cmd_dev_attr_set_doit,
+               .flags = GENL_ADMIN_PERM,
+       },
 };
 
 static struct genl_family vdpa_nl_family __ro_after_init = {
include/linux/vdpa.h +0 -0
index db15ac07f8a6..e8bb274887ef 100644

include/linux/vdpa.h struct vdpa_mgmtdev_ops { · line 585

Quoted 5 lines
include/linux/vdpa.h +2 -0
@@ -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 (*dev_set_attr)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev,
+                           const struct vdpa_dev_set_config *config);

Let's add a doc for this new op.

Others look good.

Thanks

Quoted 18 lines
include/linux/vdpa.h +0 -0
 };
 
 /**
include/uapi/linux/vdpa.h +1 -1
index 54b649ab0f22..76b3a1fd13f3 100644
@@ -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_ATTR_SET,
 };
 
 enum vdpa_attr {
--

2.45.0

author
#3 lore raw

PATCH v2 2/2 vdpa_sim_net: Add the support of set mac address

In reply to Cindy Lu

Add the function to support setting the MAC address. For vdpa_sim_net, the driver will write the MAC address to the config space, and other devices can implement their own functions to support this.

---
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim_net.c +17 -1
index cfe962911804..e1e545d6490e 100644
@@ -414,6 +414,21 @@ static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
 	net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
 }
 
+static int vdpasim_net_set_attr(struct vdpa_mgmt_dev *mdev,
+				struct vdpa_device *dev,
+				const struct vdpa_dev_set_config *config)
+{
+	struct vdpasim *vdpasim = container_of(dev, struct vdpasim, vdpa);
+
+	struct virtio_net_config *vio_config = vdpasim->config;
+
+	if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+		memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
+		return 0;
+	}
+	return -EINVAL;
+}
+
 static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
 				     const struct vdpa_dev_set_config *config)
 {
@@ -510,7 +525,8 @@ static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
 
 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
 	.dev_add = vdpasim_net_dev_add,
-	.dev_del = vdpasim_net_dev_del
+	.dev_del = vdpasim_net_dev_del,
+	.dev_set_attr = vdpasim_net_set_attr
 };
 
 static struct virtio_device_id id_table[] = {
Signature · 1 line
2.45.0
#4 lore raw

Re: [PATCH v2 2/2] vdpa_sim_net: Add the support of set mac address

Quoted 21 lines · Cindy Lu

Add the function to support setting the MAC address. For vdpa_sim_net, the driver will write the MAC address to the config space, and other devices can implement their own functions to support this.

---
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim_net.c +2 -0
index cfe962911804..e1e545d6490e 100644
@@ -414,6 +414,21 @@ static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
        net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
 }
 
+static int vdpasim_net_set_attr(struct vdpa_mgmt_dev *mdev,
+                               struct vdpa_device *dev,

drivers/vdpa/vdpa_sim/vdpa_sim_net.c static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config) · line 428

Quoted 10 lines
drivers/vdpa/vdpa_sim/vdpa_sim_net.c +10 -0
+                               const struct vdpa_dev_set_config *config)
+{
+       struct vdpasim *vdpasim = container_of(dev, struct vdpasim, vdpa);
+
+       struct virtio_net_config *vio_config = vdpasim->config;
+
+       if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+               memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
+               return 0;
+       }

We should synchronize this with the mac set from the guest.

Thanks

Quoted 19 lines
drivers/vdpa/vdpa_sim/vdpa_sim_net.c +5 -2
+       return -EINVAL;
+}
+
 static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
                                     const struct vdpa_dev_set_config *config)
 {
@@ -510,7 +525,8 @@ static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
 
 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
        .dev_add = vdpasim_net_dev_add,
-       .dev_del = vdpasim_net_dev_del
+       .dev_del = vdpasim_net_dev_del,
+       .dev_set_attr = vdpasim_net_set_attr
 };
 
 static struct virtio_device_id id_table[] = {
--

2.45.0

author
#5 lore raw

Re: [PATCH v2 2/2] vdpa_sim_net: Add the support of set mac address

Quoted 36 lines · Jason Wang
Quoted 21 lines · Cindy Lu

Add the function to support setting the MAC address. For vdpa_sim_net, the driver will write the MAC address to the config space, and other devices can implement their own functions to support this.

---
 drivers/vdpa/vdpa_sim/vdpa_sim_net.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/vdpa/vdpa_sim/vdpa_sim_net.c +2 -0
index cfe962911804..e1e545d6490e 100644
@@ -414,6 +414,21 @@ static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config)
        net_config->status = cpu_to_vdpasim16(vdpasim, VIRTIO_NET_S_LINK_UP);
 }
 
+static int vdpasim_net_set_attr(struct vdpa_mgmt_dev *mdev,
+                               struct vdpa_device *dev,

drivers/vdpa/vdpa_sim/vdpa_sim_net.c static void vdpasim_net_get_config(struct vdpasim *vdpasim, void *config) · line 428

Quoted 10 lines
drivers/vdpa/vdpa_sim/vdpa_sim_net.c +10 -0
+                               const struct vdpa_dev_set_config *config)
+{
+       struct vdpasim *vdpasim = container_of(dev, struct vdpasim, vdpa);
+
+       struct virtio_net_config *vio_config = vdpasim->config;
+
+       if (config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+               memcpy(vio_config->mac, config->net.mac, ETH_ALEN);
+               return 0;
+       }

We should synchronize this with the mac set from the guest.

Quoted 2 lines

Thanks

sure, Will fix this
thanks
cindy

Quoted 20 lines
Quoted 19 lines
drivers/vdpa/vdpa_sim/vdpa_sim_net.c +5 -2
+       return -EINVAL;
+}
+
 static void vdpasim_net_setup_config(struct vdpasim *vdpasim,
                                     const struct vdpa_dev_set_config *config)
 {
@@ -510,7 +525,8 @@ static void vdpasim_net_dev_del(struct vdpa_mgmt_dev *mdev,
 
 static const struct vdpa_mgmtdev_ops vdpasim_net_mgmtdev_ops = {
        .dev_add = vdpasim_net_dev_add,
-       .dev_del = vdpasim_net_dev_del
+       .dev_del = vdpasim_net_dev_del,
+       .dev_set_attr = vdpasim_net_set_attr
 };
 
 static struct virtio_device_id id_table[] = {
--

2.45.0