Skip to contentSkip to conversation

vdpa/mlx5: update mlx_features with driver state check

PATCH Superseded Cindy Lu 3 participants 10 messages Dec 29, 2025 – Jan 19

Subsystems (5)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Cindy Lu

author
#0 lore raw

PATCH v2 1/3 vdpa/mlx5: update mlx_features with driver state check

Add logic in mlx5_vdpa_set_attr() to ensure the VIRTIO_NET_F_MAC feature bit is properly set only when the device is not yet in the DRIVER_OK (running) state.

This makes the MAC address visible in the output of:

vdpa dev config show -jp

when the device is created without an initial MAC address.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +11 -2
index ddaa1366704b..6e42bae7c9a1 100644
@@ -4049,7 +4049,7 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 	struct mlx5_vdpa_dev *mvdev;
 	struct mlx5_vdpa_net *ndev;
 	struct mlx5_core_dev *mdev;
-	int err = -EOPNOTSUPP;
+	int err = 0;
 
 	mvdev = to_mvdev(dev);
 	ndev = to_mlx5_vdpa_ndev(mvdev);
@@ -4057,13 +4057,22 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 	config = &ndev->config;
 
 	down_write(&ndev->reslock);
-	if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+
+	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+		if (!(ndev->mvdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
+			ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_MAC);
+		} else {
+			mlx5_vdpa_warn(mvdev, "device running, skip updating MAC\n");
+			err = -EBUSY;
+			goto out;
+		}
 		pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
 		err = mlx5_mpfs_add_mac(pfmdev, config->mac);
 		if (!err)
 			ether_addr_copy(config->mac, add_config->net.mac);
 	}
 
+out:
 	up_write(&ndev->reslock);
 	return err;
 }
Signature · 1 line
2.45.0
author
#1 lore raw

PATCH v2 2/3 vdpa/mlx5: reuse common function for MAC address updates

Factor out MAC address update logic and reuse it from handle_ctrl_mac().

This ensures that old MAC entries are removed from the MPFS table before adding a new one and that the forwarding rules are updated accordingly. If updating the flow table fails, the original MAC and rules are restored as much as possible to keep the software and hardware state consistent.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 95 +++++++++++++++++--------------
 1 file changed, 53 insertions(+), 42 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +53 -42
index 6e42bae7c9a1..c87e6395b060 100644
@@ -2125,62 +2125,48 @@ static void teardown_steering(struct mlx5_vdpa_net *ndev)
 	mlx5_destroy_flow_table(ndev->rxft);
 }
 
-static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
+static int mlx5_vdpa_change_new_mac(struct mlx5_vdpa_net *ndev,
+				    struct mlx5_core_dev *pfmdev,
+				    const u8 *new_mac)
 {
-	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
-	struct mlx5_control_vq *cvq = &mvdev->cvq;
-	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
-	struct mlx5_core_dev *pfmdev;
-	size_t read;
-	u8 mac[ETH_ALEN], mac_back[ETH_ALEN];
-
-	pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
-	switch (cmd) {
-	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
-		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)mac, ETH_ALEN);
-		if (read != ETH_ALEN)
-			break;
-
-		if (!memcmp(ndev->config.mac, mac, 6)) {
-			status = VIRTIO_NET_OK;
-			break;
-		}
+	struct mlx5_vdpa_dev *mvdev = &ndev->mvdev;
+	u8 old_mac[ETH_ALEN];
 
-		if (is_zero_ether_addr(mac))
-			break;
+	if (is_zero_ether_addr(new_mac))
+		return -EINVAL;
 
-		if (!is_zero_ether_addr(ndev->config.mac)) {
-			if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
-				mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
-					       ndev->config.mac);
-				break;
-			}
+	if (!is_zero_ether_addr(ndev->config.mac)) {
+		if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
+			mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
+				       ndev->config.mac);
+			return -EIO;
 		}
+	}
 
-		if (mlx5_mpfs_add_mac(pfmdev, mac)) {
-			mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
-				       mac);
-			break;
-		}
+	if (mlx5_mpfs_add_mac(pfmdev, (u8 *)new_mac)) {
+		mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
+			       new_mac);
+		return -EIO;
+	}
 
 		/* backup the original mac address so that if failed to add the forward rules
 		 * we could restore it
 		 */
-		memcpy(mac_back, ndev->config.mac, ETH_ALEN);
+		memcpy(old_mac, ndev->config.mac, ETH_ALEN);
 
-		memcpy(ndev->config.mac, mac, ETH_ALEN);
+		memcpy(ndev->config.mac, new_mac, ETH_ALEN);
 
 		/* Need recreate the flow table entry, so that the packet could forward back
 		 */
-		mac_vlan_del(ndev, mac_back, 0, false);
+		mac_vlan_del(ndev, old_mac, 0, false);
 
 		if (mac_vlan_add(ndev, ndev->config.mac, 0, false)) {
 			mlx5_vdpa_warn(mvdev, "failed to insert forward rules, try to restore\n");
 
 			/* Although it hardly run here, we still need double check */
-			if (is_zero_ether_addr(mac_back)) {
+			if (is_zero_ether_addr(old_mac)) {
 				mlx5_vdpa_warn(mvdev, "restore mac failed: Original MAC is zero\n");
-				break;
+				return -EIO;
 			}
 
 			/* Try to restore original mac address to MFPS table, and try to restore
@@ -2191,20 +2177,45 @@ static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
 					       ndev->config.mac);
 			}
 
-			if (mlx5_mpfs_add_mac(pfmdev, mac_back)) {
+			if (mlx5_mpfs_add_mac(pfmdev, old_mac)) {
 				mlx5_vdpa_warn(mvdev, "restore mac failed: insert old MAC %pM into MPFS table failed\n",
-					       mac_back);
+					       old_mac);
 			}
 
-			memcpy(ndev->config.mac, mac_back, ETH_ALEN);
+			memcpy(ndev->config.mac, old_mac, ETH_ALEN);
 
 			if (mac_vlan_add(ndev, ndev->config.mac, 0, false))
 				mlx5_vdpa_warn(mvdev, "restore forward rules failed: insert forward rules failed\n");
 
-			break;
+			return -EIO;
 		}
 
-		status = VIRTIO_NET_OK;
+		return 0;
+}
+
+static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
+{
+	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
+	struct mlx5_control_vq *cvq = &mvdev->cvq;
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	struct mlx5_core_dev *pfmdev;
+	size_t read;
+	u8 mac[ETH_ALEN];
+
+	pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
+	switch (cmd) {
+	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov,
+					     (void *)mac, ETH_ALEN);
+		if (read != ETH_ALEN)
+			break;
+
+		if (!memcmp(ndev->config.mac, mac, 6)) {
+			status = VIRTIO_NET_OK;
+			break;
+		}
+		status = mlx5_vdpa_change_new_mac(ndev, pfmdev, mac) ? VIRTIO_NET_ERR :
+								       VIRTIO_NET_OK;
 		break;
 
 	default:
Signature · 1 line
2.45.0
maintainer Reviewed-by on v2 2/3
#2 lore raw

Re: [PATCH v2 2/3] vdpa/mlx5: reuse common function for MAC address updates

Quoted 18 lines · Cindy Lu

Factor out MAC address update logic and reuse it from handle_ctrl_mac().

This ensures that old MAC entries are removed from the MPFS table before adding a new one and that the forwarding rules are updated accordingly. If updating the flow table fails, the original MAC and rules are restored as much as possible to keep the software and hardware state consistent.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 95 +++++++++++++++++--------------
 1 file changed, 53 insertions(+), 42 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -0
index 6e42bae7c9a1..c87e6395b060 100644

drivers/vdpa/mlx5/net/mlx5_vnet.c static void teardown_steering(struct mlx5_vdpa_net *ndev) · line 2130

Quoted 7 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +3 -1
@@ -2125,62 +2125,48 @@ static void teardown_steering(struct mlx5_vdpa_net *ndev)
 	mlx5_destroy_flow_table(ndev->rxft);
 }
 
-static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
+static int mlx5_vdpa_change_new_mac(struct mlx5_vdpa_net *ndev,
+				    struct mlx5_core_dev *pfmdev,
+				    const u8 *new_mac)

Nit: I would drop the "new" and leave it mlx5_vdpa_change_mac?

Quoted 119 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +43 -41
 {
-	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
-	struct mlx5_control_vq *cvq = &mvdev->cvq;
-	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
-	struct mlx5_core_dev *pfmdev;
-	size_t read;
-	u8 mac[ETH_ALEN], mac_back[ETH_ALEN];
-
-	pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
-	switch (cmd) {
-	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
-		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)mac, ETH_ALEN);
-		if (read != ETH_ALEN)
-			break;
-
-		if (!memcmp(ndev->config.mac, mac, 6)) {
-			status = VIRTIO_NET_OK;
-			break;
-		}
+	struct mlx5_vdpa_dev *mvdev = &ndev->mvdev;
+	u8 old_mac[ETH_ALEN];
 
-		if (is_zero_ether_addr(mac))
-			break;
+	if (is_zero_ether_addr(new_mac))
+		return -EINVAL;
 
-		if (!is_zero_ether_addr(ndev->config.mac)) {
-			if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
-				mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
-					       ndev->config.mac);
-				break;
-			}
+	if (!is_zero_ether_addr(ndev->config.mac)) {
+		if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
+			mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
+				       ndev->config.mac);
+			return -EIO;
 		}
+	}
 
-		if (mlx5_mpfs_add_mac(pfmdev, mac)) {
-			mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
-				       mac);
-			break;
-		}
+	if (mlx5_mpfs_add_mac(pfmdev, (u8 *)new_mac)) {
+		mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
+			       new_mac);
+		return -EIO;
+	}
 
 		/* backup the original mac address so that if failed to add the forward rules
 		 * we could restore it
 		 */
-		memcpy(mac_back, ndev->config.mac, ETH_ALEN);
+		memcpy(old_mac, ndev->config.mac, ETH_ALEN);
 
-		memcpy(ndev->config.mac, mac, ETH_ALEN);
+		memcpy(ndev->config.mac, new_mac, ETH_ALEN);
 
 		/* Need recreate the flow table entry, so that the packet could forward back
 		 */
-		mac_vlan_del(ndev, mac_back, 0, false);
+		mac_vlan_del(ndev, old_mac, 0, false);
 
 		if (mac_vlan_add(ndev, ndev->config.mac, 0, false)) {
 			mlx5_vdpa_warn(mvdev, "failed to insert forward rules, try to restore\n");
 
 			/* Although it hardly run here, we still need double check */
-			if (is_zero_ether_addr(mac_back)) {
+			if (is_zero_ether_addr(old_mac)) {
 				mlx5_vdpa_warn(mvdev, "restore mac failed: Original MAC is zero\n");
-				break;
+				return -EIO;
 			}
 
 			/* Try to restore original mac address to MFPS table, and try to restore
@@ -2191,20 +2177,45 @@ static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
 					       ndev->config.mac);
 			}
 
-			if (mlx5_mpfs_add_mac(pfmdev, mac_back)) {
+			if (mlx5_mpfs_add_mac(pfmdev, old_mac)) {
 				mlx5_vdpa_warn(mvdev, "restore mac failed: insert old MAC %pM into MPFS table failed\n",
-					       mac_back);
+					       old_mac);
 			}
 
-			memcpy(ndev->config.mac, mac_back, ETH_ALEN);
+			memcpy(ndev->config.mac, old_mac, ETH_ALEN);
 
 			if (mac_vlan_add(ndev, ndev->config.mac, 0, false))
 				mlx5_vdpa_warn(mvdev, "restore forward rules failed: insert forward rules failed\n");
 
-			break;
+			return -EIO;
 		}
 
-		status = VIRTIO_NET_OK;
+		return 0;
+}
+
+static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
+{
+	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
+	struct mlx5_control_vq *cvq = &mvdev->cvq;
+	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
+	struct mlx5_core_dev *pfmdev;
+	size_t read;
+	u8 mac[ETH_ALEN];
+
+	pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
+	switch (cmd) {
+	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
+		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov,
+					     (void *)mac, ETH_ALEN);
+		if (read != ETH_ALEN)
+			break;

drivers/vdpa/mlx5/net/mlx5_vnet.c static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd) · line 2221

Quoted 10 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +7 -0
+
+		if (!memcmp(ndev->config.mac, mac, 6)) {
+			status = VIRTIO_NET_OK;
+			break;
+		}
+		status = mlx5_vdpa_change_new_mac(ndev, pfmdev, mac) ? VIRTIO_NET_ERR :
+								       VIRTIO_NET_OK;
 		break;
 
 	default:

Besides the nit, the code looks good. You can add in the next version:

Thanks,
Dragos

reviewer
#3 lore raw

Re: [PATCH v2 2/3] vdpa/mlx5: reuse common function for MAC address updates

In reply to Cindy Lu
Quoted 75 lines · Cindy Lu

Factor out MAC address update logic and reuse it from handle_ctrl_mac().

This ensures that old MAC entries are removed from the MPFS table before adding a new one and that the forwarding rules are updated accordingly. If updating the flow table fails, the original MAC and rules are restored as much as possible to keep the software and hardware state consistent.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 95 +++++++++++++++++--------------
 1 file changed, 53 insertions(+), 42 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +17 -32
index 6e42bae7c9a1..c87e6395b060 100644
@@ -2125,62 +2125,48 @@ static void teardown_steering(struct mlx5_vdpa_net *ndev)
 	mlx5_destroy_flow_table(ndev->rxft);
 }
 
-static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
+static int mlx5_vdpa_change_new_mac(struct mlx5_vdpa_net *ndev,
+				    struct mlx5_core_dev *pfmdev,
+				    const u8 *new_mac)
 {
-	struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
-	struct mlx5_control_vq *cvq = &mvdev->cvq;
-	virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
-	struct mlx5_core_dev *pfmdev;
-	size_t read;
-	u8 mac[ETH_ALEN], mac_back[ETH_ALEN];
-
-	pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
-	switch (cmd) {
-	case VIRTIO_NET_CTRL_MAC_ADDR_SET:
-		read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)mac, ETH_ALEN);
-		if (read != ETH_ALEN)
-			break;
-
-		if (!memcmp(ndev->config.mac, mac, 6)) {
-			status = VIRTIO_NET_OK;
-			break;
-		}
+	struct mlx5_vdpa_dev *mvdev = &ndev->mvdev;
+	u8 old_mac[ETH_ALEN];
 
-		if (is_zero_ether_addr(mac))
-			break;
+	if (is_zero_ether_addr(new_mac))
+		return -EINVAL;
 
-		if (!is_zero_ether_addr(ndev->config.mac)) {
-			if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
-				mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
-					       ndev->config.mac);
-				break;
-			}
+	if (!is_zero_ether_addr(ndev->config.mac)) {
+		if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
+			mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
+				       ndev->config.mac);
+			return -EIO;
 		}
+	}
 
-		if (mlx5_mpfs_add_mac(pfmdev, mac)) {
-			mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
-				       mac);
-			break;
-		}
+	if (mlx5_mpfs_add_mac(pfmdev, (u8 *)new_mac)) {
+		mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
+			       new_mac);
+		return -EIO;

drivers/vdpa/mlx5/net/mlx5_vnet.c static void teardown_steering(struct mlx5_vdpa_net *ndev) · line 2157

Quoted 10 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +3 -2
+	}
 
 		/* backup the original mac address so that if failed to add the forward rules
 		 * we could restore it
 		 */
-		memcpy(mac_back, ndev->config.mac, ETH_ALEN);
+		memcpy(old_mac, ndev->config.mac, ETH_ALEN);
 
-		memcpy(ndev->config.mac, mac, ETH_ALEN);
+		memcpy(ndev->config.mac, new_mac, ETH_ALEN);

...

Hi Cindy,

I realise that this makes the diffstat significantly more verbose. And hides material changes. So perhaps there is a nicer way to do this.

But with the current arrangement of this patch, I think that the indentation from just above, until the end of this function needs to be updated.

I.e. the following incremental patch on top of this one.

This was flagged by Smatch.

drivers/vdpa/mlx5/net/mlx5_vnet.c +32 -32
index c87e6395b060..c796f502b604 100644
@@ -2149,48 +2149,48 @@ static int mlx5_vdpa_change_new_mac(struct mlx5_vdpa_net *ndev,
 		return -EIO;
 	}
 
-		/* backup the original mac address so that if failed to add the forward rules
-		 * we could restore it
-		 */
-		memcpy(old_mac, ndev->config.mac, ETH_ALEN);
+	/* backup the original mac address so that if failed to add the forward rules
+	 * we could restore it
+	 */
+	memcpy(old_mac, ndev->config.mac, ETH_ALEN);
 
-		memcpy(ndev->config.mac, new_mac, ETH_ALEN);
+	memcpy(ndev->config.mac, new_mac, ETH_ALEN);
 
-		/* Need recreate the flow table entry, so that the packet could forward back
-		 */
-		mac_vlan_del(ndev, old_mac, 0, false);
+	/* Need recreate the flow table entry, so that the packet could forward back
+	 */
+	mac_vlan_del(ndev, old_mac, 0, false);
 
-		if (mac_vlan_add(ndev, ndev->config.mac, 0, false)) {
-			mlx5_vdpa_warn(mvdev, "failed to insert forward rules, try to restore\n");
+	if (mac_vlan_add(ndev, ndev->config.mac, 0, false)) {
+		mlx5_vdpa_warn(mvdev, "failed to insert forward rules, try to restore\n");
 
-			/* Although it hardly run here, we still need double check */
-			if (is_zero_ether_addr(old_mac)) {
-				mlx5_vdpa_warn(mvdev, "restore mac failed: Original MAC is zero\n");
-				return -EIO;
-			}
+		/* Although it hardly run here, we still need double check */
+		if (is_zero_ether_addr(old_mac)) {
+			mlx5_vdpa_warn(mvdev, "restore mac failed: Original MAC is zero\n");
+			return -EIO;
+		}
 
-			/* Try to restore original mac address to MFPS table, and try to restore
-			 * the forward rule entry.
-			 */
-			if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
-				mlx5_vdpa_warn(mvdev, "restore mac failed: delete MAC %pM from MPFS table failed\n",
-					       ndev->config.mac);
-			}
+		/* Try to restore original mac address to MFPS table, and try to restore
+		 * the forward rule entry.
+		 */
+		if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
+			mlx5_vdpa_warn(mvdev, "restore mac failed: delete MAC %pM from MPFS table failed\n",
+				       ndev->config.mac);
+		}
 
-			if (mlx5_mpfs_add_mac(pfmdev, old_mac)) {
-				mlx5_vdpa_warn(mvdev, "restore mac failed: insert old MAC %pM into MPFS table failed\n",
-					       old_mac);
-			}
+		if (mlx5_mpfs_add_mac(pfmdev, old_mac)) {
+			mlx5_vdpa_warn(mvdev, "restore mac failed: insert old MAC %pM into MPFS table failed\n",
+				       old_mac);
+		}
 
-			memcpy(ndev->config.mac, old_mac, ETH_ALEN);
+		memcpy(ndev->config.mac, old_mac, ETH_ALEN);
 
-			if (mac_vlan_add(ndev, ndev->config.mac, 0, false))
-				mlx5_vdpa_warn(mvdev, "restore forward rules failed: insert forward rules failed\n");
+		if (mac_vlan_add(ndev, ndev->config.mac, 0, false))
+			mlx5_vdpa_warn(mvdev, "restore forward rules failed: insert forward rules failed\n");
 
-			return -EIO;
-		}
+		return -EIO;
+	}
 
-		return 0;
+	return 0;
 }
 
 static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
author
#4 lore raw

Re: [PATCH v2 2/3] vdpa/mlx5: reuse common function for MAC address updates

Quoted 179 lines · Simon Horman
Quoted 75 lines · Cindy Lu

Factor out MAC address update logic and reuse it from handle_ctrl_mac().

This ensures that old MAC entries are removed from the MPFS table before adding a new one and that the forwarding rules are updated accordingly. If updating the flow table fails, the original MAC and rules are restored as much as possible to keep the software and hardware state consistent.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 95 +++++++++++++++++--------------
 1 file changed, 53 insertions(+), 42 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +17 -32
index 6e42bae7c9a1..c87e6395b060 100644
@@ -2125,62 +2125,48 @@ static void teardown_steering(struct mlx5_vdpa_net *ndev)
      mlx5_destroy_flow_table(ndev->rxft);
 }
 
-static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)
+static int mlx5_vdpa_change_new_mac(struct mlx5_vdpa_net *ndev,
+                                 struct mlx5_core_dev *pfmdev,
+                                 const u8 *new_mac)
 {
-     struct mlx5_vdpa_net *ndev = to_mlx5_vdpa_ndev(mvdev);
-     struct mlx5_control_vq *cvq = &mvdev->cvq;
-     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
-     struct mlx5_core_dev *pfmdev;
-     size_t read;
-     u8 mac[ETH_ALEN], mac_back[ETH_ALEN];
-
-     pfmdev = pci_get_drvdata(pci_physfn(mvdev->mdev->pdev));
-     switch (cmd) {
-     case VIRTIO_NET_CTRL_MAC_ADDR_SET:
-             read = vringh_iov_pull_iotlb(&cvq->vring, &cvq->riov, (void *)mac, ETH_ALEN);
-             if (read != ETH_ALEN)
-                     break;
-
-             if (!memcmp(ndev->config.mac, mac, 6)) {
-                     status = VIRTIO_NET_OK;
-                     break;
-             }
+     struct mlx5_vdpa_dev *mvdev = &ndev->mvdev;
+     u8 old_mac[ETH_ALEN];
 
-             if (is_zero_ether_addr(mac))
-                     break;
+     if (is_zero_ether_addr(new_mac))
+             return -EINVAL;
 
-             if (!is_zero_ether_addr(ndev->config.mac)) {
-                     if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
-                             mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
-                                            ndev->config.mac);
-                             break;
-                     }
+     if (!is_zero_ether_addr(ndev->config.mac)) {
+             if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
+                     mlx5_vdpa_warn(mvdev, "failed to delete old MAC %pM from MPFS table\n",
+                                    ndev->config.mac);
+                     return -EIO;
              }
+     }
 
-             if (mlx5_mpfs_add_mac(pfmdev, mac)) {
-                     mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
-                                    mac);
-                     break;
-             }
+     if (mlx5_mpfs_add_mac(pfmdev, (u8 *)new_mac)) {
+             mlx5_vdpa_warn(mvdev, "failed to insert new MAC %pM into MPFS table\n",
+                            new_mac);
+             return -EIO;

drivers/vdpa/mlx5/net/mlx5_vnet.c static void teardown_steering(struct mlx5_vdpa_net *ndev) · line 2157

Quoted 10 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +3 -2
+     }
 
              /* backup the original mac address so that if failed to add the forward rules
               * we could restore it
               */
-             memcpy(mac_back, ndev->config.mac, ETH_ALEN);
+             memcpy(old_mac, ndev->config.mac, ETH_ALEN);
 
-             memcpy(ndev->config.mac, mac, ETH_ALEN);
+             memcpy(ndev->config.mac, new_mac, ETH_ALEN);

...

Hi Cindy,

I realise that this makes the diffstat significantly more verbose. And hides material changes. So perhaps there is a nicer way to do this.

But with the current arrangement of this patch, I think that the indentation from just above, until the end of this function needs to be updated.

I.e. the following incremental patch on top of this one.

This was flagged by Smatch.

drivers/vdpa/mlx5/net/mlx5_vnet.c +29 -29
index c87e6395b060..c796f502b604 100644
@@ -2149,48 +2149,48 @@ static int mlx5_vdpa_change_new_mac(struct mlx5_vdpa_net *ndev,
                return -EIO;
        }
 
-               /* backup the original mac address so that if failed to add the forward rules
-                * we could restore it
-                */
-               memcpy(old_mac, ndev->config.mac, ETH_ALEN);
+       /* backup the original mac address so that if failed to add the forward rules
+        * we could restore it
+        */
+       memcpy(old_mac, ndev->config.mac, ETH_ALEN);
 
-               memcpy(ndev->config.mac, new_mac, ETH_ALEN);
+       memcpy(ndev->config.mac, new_mac, ETH_ALEN);
 
-               /* Need recreate the flow table entry, so that the packet could forward back
-                */
-               mac_vlan_del(ndev, old_mac, 0, false);
+       /* Need recreate the flow table entry, so that the packet could forward back
+        */
+       mac_vlan_del(ndev, old_mac, 0, false);
 
-               if (mac_vlan_add(ndev, ndev->config.mac, 0, false)) {
-                       mlx5_vdpa_warn(mvdev, "failed to insert forward rules, try to restore\n");
+       if (mac_vlan_add(ndev, ndev->config.mac, 0, false)) {
+               mlx5_vdpa_warn(mvdev, "failed to insert forward rules, try to restore\n");
 
-                       /* Although it hardly run here, we still need double check */
-                       if (is_zero_ether_addr(old_mac)) {
-                               mlx5_vdpa_warn(mvdev, "restore mac failed: Original MAC is zero\n");
-                               return -EIO;
-                       }
+               /* Although it hardly run here, we still need double check */
+               if (is_zero_ether_addr(old_mac)) {
+                       mlx5_vdpa_warn(mvdev, "restore mac failed: Original MAC is zero\n");
+                       return -EIO;
+               }
 
-                       /* Try to restore original mac address to MFPS table, and try to restore
-                        * the forward rule entry.
-                        */
-                       if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
-                               mlx5_vdpa_warn(mvdev, "restore mac failed: delete MAC %pM from MPFS table failed\n",
-                                              ndev->config.mac);
-                       }
+               /* Try to restore original mac address to MFPS table, and try to restore
+                * the forward rule entry.
+                */
+               if (mlx5_mpfs_del_mac(pfmdev, ndev->config.mac)) {
+                       mlx5_vdpa_warn(mvdev, "restore mac failed: delete MAC %pM from MPFS table failed\n",
+                                      ndev->config.mac);
+               }
 
-                       if (mlx5_mpfs_add_mac(pfmdev, old_mac)) {
-                               mlx5_vdpa_warn(mvdev, "restore mac failed: insert old MAC %pM into MPFS table failed\n",
-                                              old_mac);
-                       }
+               if (mlx5_mpfs_add_mac(pfmdev, old_mac)) {
+                       mlx5_vdpa_warn(mvdev, "restore mac failed: insert old MAC %pM into MPFS table failed\n",
+                                      old_mac);
+               }
 
-                       memcpy(ndev->config.mac, old_mac, ETH_ALEN);
+               memcpy(ndev->config.mac, old_mac, ETH_ALEN);
 
-                       if (mac_vlan_add(ndev, ndev->config.mac, 0, false))
-                               mlx5_vdpa_warn(mvdev, "restore forward rules failed: insert forward rules failed\n");
+               if (mac_vlan_add(ndev, ndev->config.mac, 0, false))
+                       mlx5_vdpa_warn(mvdev, "restore forward rules failed: insert forward rules failed\n");
 

drivers/vdpa/mlx5/net/mlx5_vnet.c static int mlx5_vdpa_change_new_mac(struct mlx5_vdpa_net *ndev, · line 2196

Quoted 10 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +3 -3
-                       return -EIO;
-               }
+               return -EIO;
+       }
 
-               return 0;
+       return 0;
 }
 
 static virtio_net_ctrl_ack handle_ctrl_mac(struct mlx5_vdpa_dev *mvdev, u8 cmd)

Thanks Simon, will change this

author
#5 lore raw

PATCH v2 3/3 vdpa/mlx5: update MAC address handling in mlx5_vdpa_set_attr()

In reply to Cindy Lu

Improve MAC address handling in mlx5_vdpa_set_attr() to ensure that old MAC entries are properly removed from the MPFS table before adding a new one. The new MAC address is then added to both the MPFS and VLAN tables.

This change fixes an issue where the updated MAC address would not take effect until QEMU was rebooted.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +1 -5
index c87e6395b060..a75788ace401 100644
@@ -4055,7 +4055,6 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *dev,
 			      const struct vdpa_dev_set_config *add_config)
 {
-	struct virtio_net_config *config;
 	struct mlx5_core_dev *pfmdev;
 	struct mlx5_vdpa_dev *mvdev;
 	struct mlx5_vdpa_net *ndev;
@@ -4065,7 +4064,6 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 	mvdev = to_mvdev(dev);
 	ndev = to_mlx5_vdpa_ndev(mvdev);
 	mdev = mvdev->mdev;
-	config = &ndev->config;
 
 	down_write(&ndev->reslock);
 
@@ -4078,9 +4076,7 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 			goto out;
 		}
 		pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
-		err = mlx5_mpfs_add_mac(pfmdev, config->mac);
-		if (!err)
-			ether_addr_copy(config->mac, add_config->net.mac);
+		err = mlx5_vdpa_change_new_mac(ndev, pfmdev, (u8 *)add_config->net.mac);
 	}
 
 out:
Signature · 1 line
2.45.0
maintainer Reviewed-by on v2 3/3
#6 lore raw

Re: [PATCH v2 3/3] vdpa/mlx5: update MAC address handling in mlx5_vdpa_set_attr()

Quoted 34 lines · Cindy Lu

Improve MAC address handling in mlx5_vdpa_set_attr() to ensure that old MAC entries are properly removed from the MPFS table before adding a new one. The new MAC address is then added to both the MPFS and VLAN tables.

This change fixes an issue where the updated MAC address would not take effect until QEMU was rebooted.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -2
index c87e6395b060..a75788ace401 100644
@@ -4055,7 +4055,6 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *dev,
 			      const struct vdpa_dev_set_config *add_config)
 {
-	struct virtio_net_config *config;
 	struct mlx5_core_dev *pfmdev;
 	struct mlx5_vdpa_dev *mvdev;
 	struct mlx5_vdpa_net *ndev;
@@ -4065,7 +4064,6 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 	mvdev = to_mvdev(dev);
 	ndev = to_mlx5_vdpa_ndev(mvdev);
 	mdev = mvdev->mdev;
-	config = &ndev->config;
 
 	down_write(&ndev->reslock);
 

drivers/vdpa/mlx5/net/mlx5_vnet.c static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device * · line 4082

Quoted 10 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +1 -3
@@ -4078,9 +4076,7 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 			goto out;
 		}
 		pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
-		err = mlx5_mpfs_add_mac(pfmdev, config->mac);
-		if (!err)
-			ether_addr_copy(config->mac, add_config->net.mac);
+		err = mlx5_vdpa_change_new_mac(ndev, pfmdev, (u8 *)add_config->net.mac);
 	}
 
 out:

Thanks for your patch. It looks much better like this.

Thanks,
Dragos

author
#7 lore raw

Re: [PATCH v2 3/3] vdpa/mlx5: update MAC address handling in mlx5_vdpa_set_attr()

Quoted 47 lines · Dragos Tatulea
Quoted 43 lines · Cindy Lu

Improve MAC address handling in mlx5_vdpa_set_attr() to ensure that old MAC entries are properly removed from the MPFS table before adding a new one. The new MAC address is then added to both the MPFS and VLAN tables.

This change fixes an issue where the updated MAC address would not take effect until QEMU was rebooted.

---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +1 -5
index c87e6395b060..a75788ace401 100644
@@ -4055,7 +4055,6 @@ static void mlx5_vdpa_dev_del(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *dev,
                            const struct vdpa_dev_set_config *add_config)
 {
-     struct virtio_net_config *config;
      struct mlx5_core_dev *pfmdev;
      struct mlx5_vdpa_dev *mvdev;
      struct mlx5_vdpa_net *ndev;
@@ -4065,7 +4064,6 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
      mvdev = to_mvdev(dev);
      ndev = to_mlx5_vdpa_ndev(mvdev);
      mdev = mvdev->mdev;
-     config = &ndev->config;
 
      down_write(&ndev->reslock);
 
@@ -4078,9 +4076,7 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
                      goto out;
              }
              pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
-             err = mlx5_mpfs_add_mac(pfmdev, config->mac);
-             if (!err)
-                     ether_addr_copy(config->mac, add_config->net.mac);
+             err = mlx5_vdpa_change_new_mac(ndev, pfmdev, (u8 *)add_config->net.mac);
      }
 
Quoted 6 lines

drivers/vdpa/mlx5/net/mlx5_vnet.c static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device * · line 4082

Quoted 1 line
drivers/vdpa/mlx5/net/mlx5_vnet.c +0 -0
 out:

Thanks for your patch. It looks much better like this.

Thanks Dragos

Quoted 3 lines

Thanks,
Dragos

maintainer Reviewed-by on v2 1/3
#8 lore raw
In reply to Cindy Lu

Hi Cindy,

Thanks for your patch!

Quoted 11 lines · Cindy Lu

Add logic in mlx5_vdpa_set_attr() to ensure the VIRTIO_NET_F_MAC feature bit is properly set only when the device is not yet in the DRIVER_OK (running) state.

This makes the MAC address visible in the output of:

vdpa dev config show -jp

when the device is created without an initial MAC address.

Having a cover letter with the summary, history and links series would make the review process easier.

Quoted 31 lines
---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +10 -2
index ddaa1366704b..6e42bae7c9a1 100644
@@ -4049,7 +4049,7 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 	struct mlx5_vdpa_dev *mvdev;
 	struct mlx5_vdpa_net *ndev;
 	struct mlx5_core_dev *mdev;
-	int err = -EOPNOTSUPP;
+	int err = 0;
 
 	mvdev = to_mvdev(dev);
 	ndev = to_mlx5_vdpa_ndev(mvdev);
@@ -4057,13 +4057,22 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
 	config = &ndev->config;
 
 	down_write(&ndev->reslock);
-	if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+
+	if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+		if (!(ndev->mvdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
+			ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_MAC);
+		} else {
+			mlx5_vdpa_warn(mvdev, "device running, skip updating MAC\n");
+			err = -EBUSY;
+			goto out;
+		}

drivers/vdpa/mlx5/net/mlx5_vnet.c static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device * · line 4078

Quoted 10 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +1 -0
 		pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
 		err = mlx5_mpfs_add_mac(pfmdev, config->mac);
 		if (!err)
 			ether_addr_copy(config->mac, add_config->net.mac);
 	}
 
+out:
 	up_write(&ndev->reslock);
 	return err;
 }

The patch itself makes sense. For it you can add:

Thanks,
Dragos

author
#9 lore raw
Quoted 59 lines · Dragos Tatulea

Hi Cindy,

Thanks for your patch!

Quoted 11 lines · Cindy Lu

Add logic in mlx5_vdpa_set_attr() to ensure the VIRTIO_NET_F_MAC feature bit is properly set only when the device is not yet in the DRIVER_OK (running) state.

This makes the MAC address visible in the output of:

vdpa dev config show -jp

when the device is created without an initial MAC address.

Having a cover letter with the summary, history and links series would make the review process easier.

Quoted 37 lines
---
 drivers/vdpa/mlx5/net/mlx5_vnet.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
drivers/vdpa/mlx5/net/mlx5_vnet.c +10 -2
index ddaa1366704b..6e42bae7c9a1 100644
@@ -4049,7 +4049,7 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
      struct mlx5_vdpa_dev *mvdev;
      struct mlx5_vdpa_net *ndev;
      struct mlx5_core_dev *mdev;
-     int err = -EOPNOTSUPP;
+     int err = 0;
 
      mvdev = to_mvdev(dev);
      ndev = to_mlx5_vdpa_ndev(mvdev);
@@ -4057,13 +4057,22 @@ static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device *
      config = &ndev->config;
 
      down_write(&ndev->reslock);
-     if (add_config->mask & (1 << VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+
+     if (add_config->mask & BIT_ULL(VDPA_ATTR_DEV_NET_CFG_MACADDR)) {
+             if (!(ndev->mvdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
+                     ndev->mvdev.mlx_features |= BIT_ULL(VIRTIO_NET_F_MAC);
+             } else {
+                     mlx5_vdpa_warn(mvdev, "device running, skip updating MAC\n");
+                     err = -EBUSY;
+                     goto out;
+             }
              pfmdev = pci_get_drvdata(pci_physfn(mdev->pdev));
              err = mlx5_mpfs_add_mac(pfmdev, config->mac);
              if (!err)
                      ether_addr_copy(config->mac, add_config->net.mac);
      }
 
Quoted 8 lines

drivers/vdpa/mlx5/net/mlx5_vnet.c static int mlx5_vdpa_set_attr(struct vdpa_mgmt_dev *v_mdev, struct vdpa_device * · line 4078

Quoted 4 lines
drivers/vdpa/mlx5/net/mlx5_vnet.c +1 -0
+out:
      up_write(&ndev->reslock);
      return err;
 }

The patch itself makes sense. For it you can add:

Thanks Dragos, will add this

Quoted 3 lines

Thanks,
Dragos