[PATCH v2 8/9] net/pcap: support MAC address set

Stephen Hemminger
2026-01-09T01:18:50Z

Use rte_ether_addr structure to avoid memcpy and void *.

When using pcap on a single interface, it is possible for
driver to proxy the mac address set operation.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
drivers/net/pcap/pcap_ethdev.c | 19 ++++++++++++++---
drivers/net/pcap/pcap_osdep.h | 1 +
drivers/net/pcap/pcap_osdep_freebsd.c | 23 ++++++++++++++++++++
drivers/net/pcap/pcap_osdep_linux.c | 30 ++++++++++++++++++++++++++-
4 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/drivers/net/pcap/pcap_ethdev.c b/drivers/net/pcap/pcap_ethdev.c
index bf03f431dd..824cdabab6 100644
--- a/drivers/net/pcap/pcap_ethdev.c
+++ b/drivers/net/pcap/pcap_ethdev.c
@@ -1086,6 +1086,18 @@ eth_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
return 0;
}

+static int
+eth_dev_macaddr_set(struct rte_eth_dev *dev, struct rte_ether_addr *addr)
+{
+ struct pmd_internals *internals = dev->data->dev_private;
+
+ if (internals->single_iface)
+ return osdep_iface_mac_set(internals->if_index, addr);
+ else
+ return -ENOTSUP;
+}
+
+
/* Timestamp values in receive packets from libpcap are in UTC */
static int
eth_rx_clock(struct rte_eth_dev *dev __rte_unused, uint64_t *timestamp)
@@ -1110,6 +1122,7 @@ static const struct eth_dev_ops ops = {
.rx_queue_stop = eth_rx_queue_stop,
.tx_queue_stop = eth_tx_queue_stop,
.link_update = eth_link_update,
+ .mac_addr_set = eth_dev_macaddr_set,
.mtu_set = eth_mtu_set,
.stats_get = eth_stats_get,
.stats_reset = eth_stats_reset,
@@ -1357,9 +1370,9 @@ pmd_init_internals(struct rte_vdev_device *vdev,

static int
eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
- const unsigned int numa_node)
+ const unsigned int numa_node)
{
- void *mac_addrs;
+ struct rte_ether_addr *mac_addrs;
struct rte_ether_addr mac;

if (osdep_iface_mac_get(if_name, &mac) < 0)
@@ -1370,7 +1383,7 @@ eth_pcap_update_mac(const char *if_name, struct rte_eth_dev *eth_dev,
return -1;

PMD_LOG(INFO, "Setting phy MAC for %s", if_name);
- rte_memcpy(mac_addrs, mac.addr_bytes, RTE_ETHER_ADDR_LEN);
+ rte_ether_addr_copy(&mac, mac_addrs);
eth_dev->data->mac_addrs = mac_addrs;
return 0;
}
diff --git a/drivers/net/pcap/pcap_osdep.h b/drivers/net/pcap/pcap_osdep.h
index 3c8b7ff27b..00944e0843 100644
--- a/drivers/net/pcap/pcap_osdep.h
+++ b/drivers/net/pcap/pcap_osdep.h
@@ -14,6 +14,7 @@ extern int eth_pcap_logtype;

int osdep_iface_index_get(const char *name);
int osdep_iface_mac_get(const char *name, struct rte_ether_addr *mac);
+int osdep_iface_mac_set(int ifindex, const struct rte_ether_addr *mac);
int osdep_iface_mtu_set(int index, uint16_t mtu);

#endif
diff --git a/drivers/net/pcap/pcap_osdep_freebsd.c b/drivers/net/pcap/pcap_osdep_freebsd.c
index 0279dbf00b..39227da63a 100644
--- a/drivers/net/pcap/pcap_osdep_freebsd.c
+++ b/drivers/net/pcap/pcap_osdep_freebsd.c
@@ -58,6 +58,29 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)
return 0;
}

+int
+osdep_iface_mac_set(int ifindex, const struct rte_ether_addr *mac)
+{
+ char ifname[IFNAMSIZ];
+
+ if (if_indextoname(ifindex, ifname) == NULL)
+ return -errno;
+
+ int s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0)
+ return -errno;
+
+ struct ifreq ifr = { 0 };
+ strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+ ifr.ifr_addr.sa_family = AF_LINK;
+ memcpy(ifr.ifr_addr.sa_data, mac, sizeof(*mac));
+
+ int ret = ioctl(s, SIOCSIFLLADDR, &ifr);
+ close(s);
+
+ return (ret < 0) ? -errno : 0;
+}
+
int osdep_iface_mtu_set(int ifindex, uint16_t mtu)
{
char ifname[IFNAMSIZ];
diff --git a/drivers/net/pcap/pcap_osdep_linux.c b/drivers/net/pcap/pcap_osdep_linux.c
index d180e9b4b4..d5a3855a0a 100644
--- a/drivers/net/pcap/pcap_osdep_linux.c
+++ b/drivers/net/pcap/pcap_osdep_linux.c
@@ -4,13 +4,18 @@
* All rights reserved.
*/

+#include <string.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <errno.h>
#include <net/if.h>
+#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
-#include <unistd.h>

#include <rte_memcpy.h>
#include <rte_string_fns.h>
+#include <rte_ether.h>

#include "pcap_osdep.h"

@@ -41,6 +46,29 @@ osdep_iface_mac_get(const char *if_name, struct rte_ether_addr *mac)
return 0;
}

+int
+osdep_iface_mac_set(int ifindex, const struct rte_ether_addr *mac)
+{
+ char ifname[IFNAMSIZ];
+
+ if (if_indextoname(ifindex, ifname) == NULL)
+ return -errno;
+
+ int s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0)
+ return -errno;
+
+ struct ifreq ifr = { 0 };
+ strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+ ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+ memcpy(ifr.ifr_hwaddr.sa_data, mac, sizeof(*mac));
+
+ int ret = ioctl(s, SIOCSIFHWADDR, &ifr);
+ close(s);
+
+ return (ret < 0) ? -errno : 0;
+}
+
int
osdep_iface_mtu_set(int ifindex, uint16_t mtu)
{
--
2.51.0