static int send_vhost_reply(int sockfd, struct VhostUserMsg *msg);
static int read_vhost_message(int sockfd, struct VhostUserMsg *msg);
+static void
+close_msg_fds(struct VhostUserMsg *msg)
+{
+ int i;
+
+ for (i = 0; i < msg->fd_num; i++)
+ close(msg->fds[i]);
+}
+
+/*
+ * Ensure the expected number of FDs is received,
+ * close all FDs and return an error if this is not the case.
+ */
+static int
+validate_msg_fds(struct VhostUserMsg *msg, int expected_fds)
+{
+ if (msg->fd_num == expected_fds)
+ return 0;
+
+ VHOST_LOG_CONFIG(ERR,
+ " Expect %d FDs for request %s, received %d\n",
+ expected_fds,
+ vhost_message_str[msg->request.master],
+ msg->fd_num);
+
+ close_msg_fds(msg);
+
+ return -1;
+}
+
static uint64_t
get_blk_size(int fd)
{
*/
static int
vhost_user_set_owner(struct virtio_net **pdev __rte_unused,
- struct VhostUserMsg *msg __rte_unused,
+ struct VhostUserMsg *msg,
int main_fd __rte_unused)
{
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
return RTE_VHOST_MSG_RESULT_OK;
}
static int
vhost_user_reset_owner(struct virtio_net **pdev,
- struct VhostUserMsg *msg __rte_unused,
+ struct VhostUserMsg *msg,
int main_fd __rte_unused)
{
struct virtio_net *dev = *pdev;
+
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
vhost_destroy_device_notify(dev);
cleanup_device(dev, 0);
struct virtio_net *dev = *pdev;
uint64_t features = 0;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
rte_vhost_driver_get_features(dev->ifname, &features);
msg->payload.u64 = features;
struct virtio_net *dev = *pdev;
uint32_t queue_num = 0;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
msg->payload.u64 = (uint64_t)queue_num;
struct rte_vdpa_device *vdpa_dev;
int did = -1;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
rte_vhost_driver_get_features(dev->ifname, &vhost_features);
if (features & ~vhost_features) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"(%d) received invalid negotiated features.\n",
dev->vid);
return RTE_VHOST_MSG_RESULT_ERR;
* is enabled when the live-migration starts.
*/
if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"(%d) features changed while device is running.\n",
dev->vid);
return RTE_VHOST_MSG_RESULT_ERR;
} else {
dev->vhost_hlen = sizeof(struct virtio_net_hdr);
}
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"negotiated Virtio features: 0x%" PRIx64 "\n", dev->features);
- VHOST_LOG_DEBUG(VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) mergeable RX buffers %s, virtio 1 %s\n",
dev->vid,
(dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
struct virtio_net *dev = *pdev;
struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
vq->size = msg->payload.state.num;
/* VIRTIO 1.0, 2.4 Virtqueues says:
*/
if (!vq_is_packed(dev)) {
if (vq->size & (vq->size - 1)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"invalid virtqueue size %u\n", vq->size);
return RTE_VHOST_MSG_RESULT_ERR;
}
}
if (vq->size > 32768) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"invalid virtqueue size %u\n", vq->size);
return RTE_VHOST_MSG_RESULT_ERR;
}
vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
sizeof(struct zcopy_mbuf), 0);
if (vq->zmbufs == NULL) {
- RTE_LOG(WARNING, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(WARNING,
"failed to allocate mem for zero copy; "
"zero copy is force disabled\n");
dev->dequeue_zero_copy = 0;
sizeof(struct vring_used_elem_packed),
RTE_CACHE_LINE_SIZE);
if (!vq->shadow_used_packed) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to allocate memory for shadow used ring.\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
vq->size * sizeof(struct vring_used_elem),
RTE_CACHE_LINE_SIZE);
if (!vq->shadow_used_split) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to allocate memory for shadow used ring.\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
vq->size * sizeof(struct batch_copy_elem),
RTE_CACHE_LINE_SIZE);
if (!vq->batch_copy_elems) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to allocate memory for batching copy.\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
MPOL_F_NODE | MPOL_F_ADDR);
if (ret) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Unable to get vq numa information.\n");
return dev;
}
if (oldnode != newnode) {
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"reallocate vq from %d to %d node\n", oldnode, newnode);
vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
if (!vq)
ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
MPOL_F_NODE | MPOL_F_ADDR);
if (ret) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Unable to get dev numa information.\n");
goto out;
}
if (oldnode != newnode) {
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"reallocate dev from %d to %d node\n",
oldnode, newnode);
dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
{
if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
uint64_t vva;
- uint64_t req_size = *size;
- vva = vhost_user_iotlb_cache_find(vq, ra,
+ vhost_user_iotlb_rd_lock(vq);
+ vva = vhost_iova_to_vva(dev, vq, ra,
size, VHOST_ACCESS_RW);
- if (req_size != *size)
- vhost_user_iotlb_miss(dev, (ra + *size),
- VHOST_ACCESS_RW);
+ vhost_user_iotlb_rd_unlock(vq);
return vva;
}
return qva_to_vva(dev, ra, size);
}
-/*
- * Converts vring log address to GPA
- * If IOMMU is enabled, the log address is IOVA
- * If IOMMU not enabled, the log address is already GPA
- */
static uint64_t
-translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
- uint64_t log_addr)
+log_addr_to_gpa(struct virtio_net *dev, struct vhost_virtqueue *vq)
{
- if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
- const uint64_t exp_size = sizeof(struct vring_used) +
- sizeof(struct vring_used_elem) * vq->size;
- uint64_t hva, gpa;
- uint64_t size = exp_size;
-
- hva = vhost_iova_to_vva(dev, vq, log_addr,
- &size, VHOST_ACCESS_RW);
- if (size != exp_size)
- return 0;
+ uint64_t log_gpa;
- gpa = hva_to_gpa(dev, hva, exp_size);
- if (!gpa) {
- RTE_LOG(ERR, VHOST_CONFIG,
- "VQ: Failed to find GPA for log_addr: 0x%" PRIx64 " hva: 0x%" PRIx64 "\n",
- log_addr, hva);
- return 0;
- }
- return gpa;
+ vhost_user_iotlb_rd_lock(vq);
+ log_gpa = translate_log_addr(dev, vq, vq->ring_addrs.log_guest_addr);
+ vhost_user_iotlb_rd_unlock(vq);
- } else
- return log_addr;
+ return log_gpa;
}
static struct virtio_net *
if (addr->flags & (1 << VHOST_VRING_F_LOG)) {
vq->log_guest_addr =
- translate_log_addr(dev, vq, addr->log_guest_addr);
+ log_addr_to_gpa(dev, vq);
if (vq->log_guest_addr == 0) {
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) failed to map log_guest_addr.\n",
dev->vid);
return dev;
if (vq->desc_packed == NULL ||
len != sizeof(struct vring_packed_desc) *
vq->size) {
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) failed to map desc_packed ring.\n",
dev->vid);
return dev;
vq, addr->avail_user_addr, &len);
if (vq->driver_event == NULL ||
len != sizeof(struct vring_packed_desc_event)) {
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) failed to find driver area address.\n",
dev->vid);
return dev;
vq, addr->used_user_addr, &len);
if (vq->device_event == NULL ||
len != sizeof(struct vring_packed_desc_event)) {
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) failed to find device area address.\n",
dev->vid);
return dev;
vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
vq, addr->desc_user_addr, &len);
if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) failed to map desc ring.\n",
dev->vid);
return dev;
vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
vq, addr->avail_user_addr, &len);
if (vq->avail == 0 || len != expected_len) {
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) failed to map avail ring.\n",
dev->vid);
return dev;
vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
vq, addr->used_user_addr, &len);
if (vq->used == 0 || len != expected_len) {
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"(%d) failed to map used ring.\n",
dev->vid);
return dev;
}
if (vq->last_used_idx != vq->used->idx) {
- RTE_LOG(WARNING, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(WARNING,
"last_used_idx (%u) and vq->used->idx (%u) mismatches; "
"some packets maybe resent for Tx and dropped for Rx\n",
vq->last_used_idx, vq->used->idx);
vq->access_ok = 1;
- VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
+ VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address desc: %p\n",
dev->vid, vq->desc);
- VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
+ VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address avail: %p\n",
dev->vid, vq->avail);
- VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
+ VHOST_LOG_CONFIG(DEBUG, "(%d) mapped address used: %p\n",
dev->vid, vq->used);
- VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
+ VHOST_LOG_CONFIG(DEBUG, "(%d) log_guest_addr: %" PRIx64 "\n",
dev->vid, vq->log_guest_addr);
return dev;
struct vhost_vring_addr *addr = &msg->payload.addr;
bool access_ok;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (dev->mem == NULL)
return RTE_VHOST_MSG_RESULT_ERR;
struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
uint64_t val = msg->payload.state.num;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (vq_is_packed(dev)) {
/*
* Bit[0:14]: avail index
dev->guest_pages = realloc(dev->guest_pages,
dev->max_guest_pages * sizeof(*page));
if (!dev->guest_pages) {
- RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
+ VHOST_LOG_CONFIG(ERR, "cannot realloc guest_pages\n");
free(old_pages);
return -1;
}
for (i = 0; i < dev->nr_guest_pages; i++) {
page = &dev->guest_pages[i];
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"guest physical page region %u\n"
"\t guest_phys_addr: %" PRIx64 "\n"
"\t host_phys_addr : %" PRIx64 "\n"
int populate;
int fd;
+ if (validate_msg_fds(msg, memory->nregions) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (memory->nregions > VHOST_MEMORY_MAX_NREGIONS) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"too many memory regions (%u)\n", memory->nregions);
return RTE_VHOST_MSG_RESULT_ERR;
}
if (dev->mem && !vhost_memory_changed(memory, dev->mem)) {
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"(%d) memory regions not changed\n", dev->vid);
- for (i = 0; i < memory->nregions; i++)
- close(msg->fds[i]);
+ close_msg_fds(msg);
return RTE_VHOST_MSG_RESULT_OK;
}
dev->guest_pages = malloc(dev->max_guest_pages *
sizeof(struct guest_page));
if (dev->guest_pages == NULL) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"(%d) failed to allocate memory "
"for dev->guest_pages\n",
dev->vid);
dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
sizeof(struct rte_vhost_mem_region) * memory->nregions, 0);
if (dev->mem == NULL) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"(%d) failed to allocate memory for dev->mem\n",
dev->vid);
return RTE_VHOST_MSG_RESULT_ERR;
/* Check for memory_size + mmap_offset overflow */
if (mmap_offset >= -reg->size) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"mmap_offset (%#"PRIx64") and memory_size "
"(%#"PRIx64") overflow\n",
mmap_offset, reg->size);
*/
alignment = get_blk_size(fd);
if (alignment == (uint64_t)-1) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"couldn't get hugepage size through fstat\n");
goto err_mmap;
}
mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
+ if (mmap_size == 0) {
+ /*
+ * It could happen if initial mmap_size + alignment
+ * overflows the sizeof uint64, which could happen if
+ * either mmap_size or alignment value is wrong.
+ *
+ * mmap() kernel implementation would return an error,
+ * but better catch it before and provide useful info
+ * in the logs.
+ */
+ VHOST_LOG_CONFIG(ERR, "mmap size (0x%" PRIx64 ") "
+ "or alignment (0x%" PRIx64 ") is invalid\n",
+ reg->size + mmap_offset, alignment);
+ goto err_mmap;
+ }
populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
MAP_SHARED | populate, fd, 0);
if (mmap_addr == MAP_FAILED) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"mmap region %u failed.\n", i);
goto err_mmap;
}
if (dev->dequeue_zero_copy)
if (add_guest_pages(dev, reg, alignment) < 0) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"adding guest pages to region %u failed.\n",
i);
goto err_mmap;
}
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"guest memory region %u, size: 0x%" PRIx64 "\n"
"\t guest physical addr: 0x%" PRIx64 "\n"
"\t guest virtual addr: 0x%" PRIx64 "\n"
*/
VhostUserMsg ack_msg;
if (read_vhost_message(main_fd, &ack_msg) <= 0) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Failed to read qemu ack on postcopy set-mem-table\n");
goto err_mmap;
}
+
+ if (validate_msg_fds(&ack_msg, 0) != 0)
+ goto err_mmap;
+
if (ack_msg.request.master != VHOST_USER_SET_MEM_TABLE) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Bad qemu ack on postcopy set-mem-table (%d)\n",
ack_msg.request.master);
goto err_mmap;
if (ioctl(dev->postcopy_ufd, UFFDIO_REGISTER,
®_struct)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Failed to register ufd for region %d: (ufd = %d) %s\n",
i, dev->postcopy_ufd,
strerror(errno));
goto err_mmap;
}
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"\t userfaultfd registered for range : "
"%" PRIx64 " - %" PRIx64 "\n",
(uint64_t)reg_struct.range.start,
return false;
if (vq_is_packed(dev))
- rings_ok = !!vq->desc_packed;
+ rings_ok = vq->desc_packed && vq->driver_event &&
+ vq->device_event;
else
rings_ok = vq->desc && vq->avail && vq->used;
return 0;
}
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"virtio is now ready for processing.\n");
return 1;
}
if (mfd == -1) {
mfd = mkstemp(fname);
if (mfd == -1) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to get inflight buffer fd\n");
return NULL;
}
}
if (ftruncate(mfd, size) == -1) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to alloc inflight buffer\n");
close(mfd);
return NULL;
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
if (ptr == MAP_FAILED) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to mmap inflight buffer\n");
close(mfd);
return NULL;
void *addr;
if (msg->size != sizeof(msg->payload.inflight)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"invalid get_inflight_fd message size is %d\n",
msg->size);
return RTE_VHOST_MSG_RESULT_ERR;
dev->inflight_info = calloc(1,
sizeof(struct inflight_mem_info));
if (!dev->inflight_info) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to alloc dev inflight area\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
num_queues = msg->payload.inflight.num_queues;
queue_size = msg->payload.inflight.queue_size;
- RTE_LOG(INFO, VHOST_CONFIG, "get_inflight_fd num_queues: %u\n",
+ VHOST_LOG_CONFIG(INFO, "get_inflight_fd num_queues: %u\n",
msg->payload.inflight.num_queues);
- RTE_LOG(INFO, VHOST_CONFIG, "get_inflight_fd queue_size: %u\n",
+ VHOST_LOG_CONFIG(INFO, "get_inflight_fd queue_size: %u\n",
msg->payload.inflight.queue_size);
if (vq_is_packed(dev))
mmap_size = num_queues * pervq_inflight_size;
addr = inflight_mem_alloc("vhost-inflight", mmap_size, &fd);
if (!addr) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to alloc vhost inflight area\n");
msg->payload.inflight.mmap_size = 0;
return RTE_VHOST_MSG_RESULT_ERR;
}
}
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"send inflight mmap_size: %"PRIu64"\n",
msg->payload.inflight.mmap_size);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"send inflight mmap_offset: %"PRIu64"\n",
msg->payload.inflight.mmap_offset);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"send inflight fd: %d\n", msg->fds[0]);
return RTE_VHOST_MSG_RESULT_REPLY;
fd = msg->fds[0];
if (msg->size != sizeof(msg->payload.inflight) || fd < 0) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"invalid set_inflight_fd message size is %d,fd is %d\n",
msg->size, fd);
return RTE_VHOST_MSG_RESULT_ERR;
else
pervq_inflight_size = get_pervq_shm_size_split(queue_size);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"set_inflight_fd mmap_size: %"PRIu64"\n", mmap_size);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"set_inflight_fd mmap_offset: %"PRIu64"\n", mmap_offset);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"set_inflight_fd num_queues: %u\n", num_queues);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"set_inflight_fd queue_size: %u\n", queue_size);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"set_inflight_fd fd: %d\n", fd);
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"set_inflight_fd pervq_inflight_size: %d\n",
pervq_inflight_size);
dev->inflight_info = calloc(1,
sizeof(struct inflight_mem_info));
if (dev->inflight_info == NULL) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to alloc dev inflight area\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
addr = mmap(0, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED,
fd, mmap_offset);
if (addr == MAP_FAILED) {
- RTE_LOG(ERR, VHOST_CONFIG, "failed to mmap share memory.\n");
+ VHOST_LOG_CONFIG(ERR, "failed to mmap share memory.\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
struct virtio_net *dev = *pdev;
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
+ int expected_fds;
+
+ expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
+ if (validate_msg_fds(msg, expected_fds) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
file.fd = msg->fds[0];
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"vring call idx:%d file:%d\n", file.index, file.fd);
vq = dev->virtqueue[file.index];
struct VhostUserMsg *msg,
int main_fd __rte_unused)
{
+ int expected_fds;
+
+ expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
+ if (validate_msg_fds(msg, expected_fds) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (!(msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK))
close(msg->fds[0]);
- RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
+ VHOST_LOG_CONFIG(INFO, "not implemented\n");
return RTE_VHOST_MSG_RESULT_OK;
}
(1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
return RTE_VHOST_MSG_RESULT_OK;
+ /* The frontend may still not support the inflight feature
+ * although we negotiate the protocol feature.
+ */
if ((!vq->inflight_split))
- return RTE_VHOST_MSG_RESULT_ERR;
+ return RTE_VHOST_MSG_RESULT_OK;
if (!vq->inflight_split->version) {
vq->inflight_split->version = INFLIGHT_VERSION;
if (resubmit_num) {
resubmit = calloc(1, sizeof(struct rte_vhost_resubmit_info));
if (!resubmit) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to allocate memory for resubmit info.\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
resubmit->resubmit_list = calloc(resubmit_num,
sizeof(struct rte_vhost_resubmit_desc));
if (!resubmit->resubmit_list) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to allocate memory for inflight desc.\n");
free(resubmit);
return RTE_VHOST_MSG_RESULT_ERR;
(1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
return RTE_VHOST_MSG_RESULT_OK;
+ /* The frontend may still not support the inflight feature
+ * although we negotiate the protocol feature.
+ */
if ((!vq->inflight_packed))
- return RTE_VHOST_MSG_RESULT_ERR;
+ return RTE_VHOST_MSG_RESULT_OK;
if (!vq->inflight_packed->version) {
vq->inflight_packed->version = INFLIGHT_VERSION;
if (resubmit_num) {
resubmit = calloc(1, sizeof(struct rte_vhost_resubmit_info));
if (resubmit == NULL) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to allocate memory for resubmit info.\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
resubmit->resubmit_list = calloc(resubmit_num,
sizeof(struct rte_vhost_resubmit_desc));
if (resubmit->resubmit_list == NULL) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to allocate memory for resubmit desc.\n");
free(resubmit);
return RTE_VHOST_MSG_RESULT_ERR;
struct virtio_net *dev = *pdev;
struct vhost_vring_file file;
struct vhost_virtqueue *vq;
+ int expected_fds;
+
+ expected_fds = (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK) ? 0 : 1;
+ if (validate_msg_fds(msg, expected_fds) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
file.index = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
if (msg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
file.fd = VIRTIO_INVALID_EVENTFD;
else
file.fd = msg->fds[0];
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"vring kick idx:%d file:%d\n", file.index, file.fd);
/* Interpret ring addresses only when ring is started. */
if (vq_is_packed(dev)) {
if (vhost_check_queue_inflights_packed(dev, vq)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to inflights for vq: %d\n", file.index);
return RTE_VHOST_MSG_RESULT_ERR;
}
} else {
if (vhost_check_queue_inflights_split(dev, vq)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to inflights for vq: %d\n", file.index);
return RTE_VHOST_MSG_RESULT_ERR;
}
struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
uint64_t val;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
/* We have to stop the queue (virtio) if it is running. */
vhost_destroy_device_notify(dev);
msg->payload.state.num = vq->last_avail_idx;
}
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"vring base idx:%d file:%d\n", msg->payload.state.index,
msg->payload.state.num);
/*
struct rte_vdpa_device *vdpa_dev;
int did = -1;
- RTE_LOG(INFO, VHOST_CONFIG,
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
+ VHOST_LOG_CONFIG(INFO,
"set queue enable: %d to qp idx: %d\n",
enable, index);
struct virtio_net *dev = *pdev;
uint64_t features, protocol_features;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
rte_vhost_driver_get_features(dev->ifname, &features);
rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
uint64_t protocol_features = msg->payload.u64;
uint64_t slave_protocol_features = 0;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
rte_vhost_driver_get_protocol_features(dev->ifname,
&slave_protocol_features);
if (protocol_features & ~slave_protocol_features) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"(%d) received invalid protocol features.\n",
dev->vid);
return RTE_VHOST_MSG_RESULT_ERR;
}
dev->protocol_features = protocol_features;
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"negotiated Vhost-user protocol features: 0x%" PRIx64 "\n",
dev->protocol_features);
uint64_t size, off;
void *addr;
+ if (validate_msg_fds(msg, 1) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (fd < 0) {
- RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
+ VHOST_LOG_CONFIG(ERR, "invalid log fd: %d\n", fd);
return RTE_VHOST_MSG_RESULT_ERR;
}
if (msg->size != sizeof(VhostUserLog)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"invalid log base msg size: %"PRId32" != %d\n",
msg->size, (int)sizeof(VhostUserLog));
return RTE_VHOST_MSG_RESULT_ERR;
/* Don't allow mmap_offset to point outside the mmap region */
if (off > size) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"log offset %#"PRIx64" exceeds log size %#"PRIx64"\n",
off, size);
return RTE_VHOST_MSG_RESULT_ERR;
}
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"log mmap size: %"PRId64", offset: %"PRId64"\n",
size, off);
addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
close(fd);
if (addr == MAP_FAILED) {
- RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
+ VHOST_LOG_CONFIG(ERR, "mmap log base failed!\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
struct VhostUserMsg *msg,
int main_fd __rte_unused)
{
+ if (validate_msg_fds(msg, 1) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
close(msg->fds[0]);
- RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
+ VHOST_LOG_CONFIG(INFO, "not implemented.\n");
return RTE_VHOST_MSG_RESULT_OK;
}
struct rte_vdpa_device *vdpa_dev;
int did = -1;
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
+ VHOST_LOG_CONFIG(DEBUG,
":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
memcpy(dev->mac.addr_bytes, mac, 6);
int main_fd __rte_unused)
{
struct virtio_net *dev = *pdev;
+
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (msg->payload.u64 < VIRTIO_MIN_MTU ||
msg->payload.u64 > VIRTIO_MAX_MTU) {
- RTE_LOG(ERR, VHOST_CONFIG, "Invalid MTU size (%"PRIu64")\n",
+ VHOST_LOG_CONFIG(ERR, "Invalid MTU size (%"PRIu64")\n",
msg->payload.u64);
return RTE_VHOST_MSG_RESULT_ERR;
struct virtio_net *dev = *pdev;
int fd = msg->fds[0];
+ if (validate_msg_fds(msg, 1) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (fd < 0) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Invalid file descriptor for slave channel (%d)\n",
fd);
return RTE_VHOST_MSG_RESULT_ERR;
if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
return 1;
+ if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
+ len = sizeof(uint64_t);
+ if (ra->log_guest_addr < end &&
+ (ra->log_guest_addr + len) > start)
+ return 1;
+ }
+
return 0;
}
if (ra->used_user_addr < end && (ra->used_user_addr + len) > start)
return 1;
+ if (ra->flags & (1 << VHOST_VRING_F_LOG)) {
+ len = sizeof(uint64_t);
+ if (ra->log_guest_addr < end &&
+ (ra->log_guest_addr + len) > start)
+ return 1;
+ }
+
return 0;
}
uint16_t i;
uint64_t vva, len;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
switch (imsg->type) {
case VHOST_IOTLB_UPDATE:
len = imsg->size;
}
break;
default:
- RTE_LOG(ERR, VHOST_CONFIG, "Invalid IOTLB message type (%d)\n",
+ VHOST_LOG_CONFIG(ERR, "Invalid IOTLB message type (%d)\n",
imsg->type);
return RTE_VHOST_MSG_RESULT_ERR;
}
#ifdef RTE_LIBRTE_VHOST_POSTCOPY
struct uffdio_api api_struct;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
if (dev->postcopy_ufd == -1) {
- RTE_LOG(ERR, VHOST_CONFIG, "Userfaultfd not available: %s\n",
+ VHOST_LOG_CONFIG(ERR, "Userfaultfd not available: %s\n",
strerror(errno));
return RTE_VHOST_MSG_RESULT_ERR;
}
api_struct.api = UFFD_API;
api_struct.features = 0;
if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
- RTE_LOG(ERR, VHOST_CONFIG, "UFFDIO_API ioctl failure: %s\n",
+ VHOST_LOG_CONFIG(ERR, "UFFDIO_API ioctl failure: %s\n",
strerror(errno));
close(dev->postcopy_ufd);
dev->postcopy_ufd = -1;
{
struct virtio_net *dev = *pdev;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
if (dev->mem && dev->mem->nregions) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Regions already registered at postcopy-listen\n");
return RTE_VHOST_MSG_RESULT_ERR;
}
{
struct virtio_net *dev = *pdev;
+ if (validate_msg_fds(msg, 0) != 0)
+ return RTE_VHOST_MSG_RESULT_ERR;
+
dev->postcopy_listening = 0;
if (dev->postcopy_ufd >= 0) {
close(dev->postcopy_ufd);
ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
msg->fds, VHOST_MEMORY_MAX_NREGIONS, &msg->fd_num);
- if (ret <= 0)
+ if (ret <= 0) {
return ret;
+ } else if (ret != VHOST_USER_HDR_SIZE) {
+ VHOST_LOG_CONFIG(ERR, "Unexpected header size read\n");
+ close_msg_fds(msg);
+ return -1;
+ }
if (msg->size) {
if (msg->size > sizeof(msg->payload)) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"invalid msg size: %d\n", msg->size);
return -1;
}
if (ret <= 0)
return ret;
if (ret != (int)msg->size) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"read control message failed\n");
return -1;
}
}
if (vring_idx >= VHOST_MAX_VRING) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"invalid vring index: %u\n", vring_idx);
return -1;
}
if (!dev->notify_ops) {
dev->notify_ops = vhost_driver_callback_get(dev->ifname);
if (!dev->notify_ops) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to get callback ops for driver %s\n",
dev->ifname);
return -1;
ret = read_vhost_message(fd, &msg);
if (ret <= 0) {
if (ret < 0)
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"vhost read message failed\n");
else
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"vhost peer closed\n");
return -1;
if (request > VHOST_USER_NONE && request < VHOST_USER_MAX &&
vhost_message_str[request]) {
if (request != VHOST_USER_IOTLB_MSG)
- RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
+ VHOST_LOG_CONFIG(INFO, "read message %s\n",
vhost_message_str[request]);
else
- RTE_LOG(DEBUG, VHOST_CONFIG, "read message %s\n",
+ VHOST_LOG_CONFIG(DEBUG, "read message %s\n",
vhost_message_str[request]);
} else {
- RTE_LOG(DEBUG, VHOST_CONFIG, "External request %d\n", request);
+ VHOST_LOG_CONFIG(DEBUG, "External request %d\n", request);
}
ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
if (ret < 0) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"failed to alloc queue\n");
return -1;
}
switch (ret) {
case RTE_VHOST_MSG_RESULT_ERR:
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Processing %s failed.\n",
vhost_message_str[request]);
handled = true;
break;
case RTE_VHOST_MSG_RESULT_OK:
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"Processing %s succeeded.\n",
vhost_message_str[request]);
handled = true;
break;
case RTE_VHOST_MSG_RESULT_REPLY:
- RTE_LOG(DEBUG, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(DEBUG,
"Processing %s succeeded and needs reply.\n",
vhost_message_str[request]);
send_vhost_reply(fd, &msg);
/* If message was not handled at this stage, treat it as an error */
if (!handled) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"vhost message (req: %d) was not handled.\n", request);
+ close_msg_fds(&msg);
ret = RTE_VHOST_MSG_RESULT_ERR;
}
msg.fd_num = 0;
send_vhost_reply(fd, &msg);
} else if (ret == RTE_VHOST_MSG_RESULT_ERR) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"vhost message handling failed.\n");
return -1;
}
if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
if (dev->dequeue_zero_copy) {
- RTE_LOG(INFO, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(INFO,
"dequeue zero copy is enabled\n");
}
}
if (msg_reply.request.slave != msg->request.slave) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Received unexpected msg type (%u), expected %u\n",
msg_reply.request.slave, msg->request.slave);
ret = -1;
ret = send_vhost_message(dev->slave_req_fd, &msg);
if (ret < 0) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Failed to send IOTLB miss message (%d)\n",
ret);
return ret;
return 0;
}
+static int
+vhost_user_slave_config_change(struct virtio_net *dev, bool need_reply)
+{
+ int ret;
+ struct VhostUserMsg msg = {
+ .request.slave = VHOST_USER_SLAVE_CONFIG_CHANGE_MSG,
+ .flags = VHOST_USER_VERSION,
+ .size = 0,
+ };
+
+ if (need_reply)
+ msg.flags |= VHOST_USER_NEED_REPLY;
+
+ ret = send_vhost_slave_message(dev, &msg);
+ if (ret < 0) {
+ VHOST_LOG_CONFIG(ERR,
+ "Failed to send config change (%d)\n",
+ ret);
+ return ret;
+ }
+
+ return process_slave_message_reply(dev, &msg);
+}
+
+int
+rte_vhost_slave_config_change(int vid, bool need_reply)
+{
+ struct virtio_net *dev;
+
+ dev = get_device(vid);
+ if (!dev)
+ return -ENODEV;
+
+ return vhost_user_slave_config_change(dev, need_reply);
+}
+
static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
int index, int fd,
uint64_t offset,
ret = send_vhost_slave_message(dev, &msg);
if (ret < 0) {
- RTE_LOG(ERR, VHOST_CONFIG,
+ VHOST_LOG_CONFIG(ERR,
"Failed to set host notifier (%d)\n", ret);
return ret;
}