#include "vhost.h"
#include "virtio_user_dev.h"
+struct vhost_vdpa_data {
+ int vhostfd;
+ uint64_t protocol_features;
+};
+
#define VHOST_VDPA_SUPPORTED_BACKEND_FEATURES \
(1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2 | \
1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
static int
vhost_vdpa_set_owner(struct virtio_user_dev *dev)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_OWNER, NULL);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_OWNER, NULL);
}
static int
vhost_vdpa_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_BACKEND_FEATURES, features);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_BACKEND_FEATURES, features);
}
static int
vhost_vdpa_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_BACKEND_FEATURES, &features);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_BACKEND_FEATURES, &features);
}
static int
vhost_vdpa_get_features(struct virtio_user_dev *dev, uint64_t *features)
{
+ struct vhost_vdpa_data *data = dev->backend_data;
int ret;
- ret = vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_FEATURES, features);
+ ret = vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_FEATURES, features);
if (ret) {
PMD_DRV_LOG(ERR, "Failed to get features");
return -1;
*features &= ~(1ULL << VIRTIO_NET_F_MQ);
/* Negotiated vDPA backend features */
- ret = vhost_vdpa_get_protocol_features(dev, &dev->protocol_features);
+ ret = vhost_vdpa_get_protocol_features(dev, &data->protocol_features);
if (ret < 0) {
PMD_DRV_LOG(ERR, "Failed to get backend features");
return -1;
}
- dev->protocol_features &= VHOST_VDPA_SUPPORTED_BACKEND_FEATURES;
+ data->protocol_features &= VHOST_VDPA_SUPPORTED_BACKEND_FEATURES;
- ret = vhost_vdpa_set_protocol_features(dev, dev->protocol_features);
+ ret = vhost_vdpa_set_protocol_features(dev, data->protocol_features);
if (ret < 0) {
PMD_DRV_LOG(ERR, "Failed to set backend features");
return -1;
static int
vhost_vdpa_set_features(struct virtio_user_dev *dev, uint64_t features)
{
+ struct vhost_vdpa_data *data = dev->backend_data;
+
/* WORKAROUND */
features |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_FEATURES, &features);
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_FEATURES, &features);
}
static int
vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
{
+ struct vhost_vdpa_data *data = dev->backend_data;
struct vhost_msg msg = {};
- if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
+ if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
return 0;
- if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+ if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
return -1;
}
msg.type = VHOST_IOTLB_MSG_V2;
msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
- if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+ if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
strerror(errno));
return -1;
static int
vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
{
+ struct vhost_vdpa_data *data = dev->backend_data;
struct vhost_msg msg = {};
- if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
+ if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
return 0;
- if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+ if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
return -1;
}
msg.type = VHOST_IOTLB_MSG_V2;
msg.iotlb.type = VHOST_IOTLB_BATCH_END;
- if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+ if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
strerror(errno));
return -1;
vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
uint64_t iova, size_t len)
{
+ struct vhost_vdpa_data *data = dev->backend_data;
struct vhost_msg msg = {};
- if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+ if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
return -1;
}
PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
__func__, iova, addr, len);
- if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+ if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
strerror(errno));
return -1;
vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
uint64_t iova, size_t len)
{
+ struct vhost_vdpa_data *data = dev->backend_data;
struct vhost_msg msg = {};
- if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+ if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
return -1;
}
PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
__func__, iova, len);
- if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+ if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
strerror(errno));
return -1;
static int
vhost_vdpa_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_SET_VRING_ENABLE, state);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_SET_VRING_ENABLE, state);
}
static int
vhost_vdpa_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_NUM, state);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_NUM, state);
}
static int
vhost_vdpa_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_BASE, state);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_BASE, state);
}
static int
vhost_vdpa_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_GET_VRING_BASE, state);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_VRING_BASE, state);
}
static int
vhost_vdpa_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_CALL, file);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_CALL, file);
}
static int
vhost_vdpa_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_KICK, file);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_KICK, file);
}
static int
vhost_vdpa_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_VRING_ADDR, addr);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_ADDR, addr);
}
static int
vhost_vdpa_get_status(struct virtio_user_dev *dev, uint8_t *status)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_GET_STATUS, status);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_GET_STATUS, status);
}
static int
vhost_vdpa_set_status(struct virtio_user_dev *dev, uint8_t status)
{
- return vhost_vdpa_ioctl(dev->vhostfd, VHOST_VDPA_SET_STATUS, &status);
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_SET_STATUS, &status);
}
/**
static int
vhost_vdpa_setup(struct virtio_user_dev *dev)
{
+ struct vhost_vdpa_data *data;
uint32_t did = (uint32_t)-1;
- dev->vhostfd = open(dev->path, O_RDWR);
- if (dev->vhostfd < 0) {
+ data = malloc(sizeof(*data));
+ if (!data) {
+ PMD_DRV_LOG(ERR, "(%s) Faidle to allocate backend data", dev->path);
+ return -1;
+ }
+
+ data->vhostfd = open(dev->path, O_RDWR);
+ if (data->vhostfd < 0) {
PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
dev->path, strerror(errno));
+ free(data);
return -1;
}
- if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
+ if (ioctl(data->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
did != VIRTIO_ID_NETWORK) {
PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
+ close(data->vhostfd);
+ free(data);
return -1;
}
+ dev->backend_data = data;
+
return 0;
}
static int
-vhost_vdpa_destroy(struct virtio_user_dev *dev __rte_unused)
+vhost_vdpa_destroy(struct virtio_user_dev *dev)
{
+ struct vhost_vdpa_data *data = dev->backend_data;
+
+ if (!data)
+ return 0;
+
+ close(data->vhostfd);
+
+ free(data);
+ dev->backend_data = NULL;
+
return 0;
}