return -1;
}
-static inline void
-parse_mac(struct virtio_user_dev *dev, const char *mac)
+int
+virtio_user_dev_set_mac(struct virtio_user_dev *dev)
{
- struct rte_ether_addr tmp;
+ int ret = 0;
- if (!mac)
- return;
+ if (!(dev->device_features & (1ULL << VIRTIO_NET_F_MAC)))
+ return -ENOTSUP;
+
+ if (!dev->ops->set_config)
+ return -ENOTSUP;
+
+ ret = dev->ops->set_config(dev, dev->mac_addr,
+ offsetof(struct virtio_net_config, mac),
+ RTE_ETHER_ADDR_LEN);
+ if (ret)
+ PMD_DRV_LOG(ERR, "(%s) Failed to set MAC address in device", dev->path);
+
+ return ret;
+}
+
+int
+virtio_user_dev_get_mac(struct virtio_user_dev *dev)
+{
+ int ret = 0;
+
+ if (!(dev->device_features & (1ULL << VIRTIO_NET_F_MAC)))
+ return -ENOTSUP;
+
+ if (!dev->ops->get_config)
+ return -ENOTSUP;
+
+ ret = dev->ops->get_config(dev, dev->mac_addr,
+ offsetof(struct virtio_net_config, mac),
+ RTE_ETHER_ADDR_LEN);
+ if (ret)
+ PMD_DRV_LOG(ERR, "(%s) Failed to get MAC address from device", dev->path);
+
+ return ret;
+}
+
+static void
+virtio_user_dev_init_mac(struct virtio_user_dev *dev, const char *mac)
+{
+ struct rte_ether_addr cmdline_mac;
+ char buf[RTE_ETHER_ADDR_FMT_SIZE];
+ int ret;
- if (rte_ether_unformat_addr(mac, &tmp) == 0) {
- memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN);
+ if (mac && rte_ether_unformat_addr(mac, &cmdline_mac) == 0) {
+ /*
+ * MAC address was passed from command-line, try to store
+ * it in the device if it supports it. Otherwise try to use
+ * the device one.
+ */
+ memcpy(dev->mac_addr, &cmdline_mac, RTE_ETHER_ADDR_LEN);
dev->mac_specified = 1;
+
+ /* Setting MAC may fail, continue to get the device one in this case */
+ virtio_user_dev_set_mac(dev);
+ ret = virtio_user_dev_get_mac(dev);
+ if (ret == -ENOTSUP)
+ goto out;
+
+ if (memcmp(&cmdline_mac, dev->mac_addr, RTE_ETHER_ADDR_LEN))
+ PMD_DRV_LOG(INFO, "(%s) Device MAC update failed", dev->path);
} else {
- /* ignore the wrong mac, use random mac */
- PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
+ ret = virtio_user_dev_get_mac(dev);
+ if (ret) {
+ PMD_DRV_LOG(ERR, "(%s) No valid MAC in devargs or device, use random",
+ dev->path);
+ return;
+ }
+
+ dev->mac_specified = 1;
}
+out:
+ rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE,
+ (struct rte_ether_addr *)dev->mac_addr);
+ PMD_DRV_LOG(INFO, "(%s) MAC %s specified", dev->path, buf);
}
static int
dev->unsupported_features = 0;
dev->backend_type = backend_type;
- parse_mac(dev, mac);
-
if (*ifname) {
dev->ifname = *ifname;
*ifname = NULL;
return -1;
}
+ virtio_user_dev_init_mac(dev, mac);
+
if (!mrg_rxbuf)
dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);