Skip to content

Commit

Permalink
Manual bugfixes: makefile, configuration options, opaque types, namin…
Browse files Browse the repository at this point in the history
…g for vhost_user
  • Loading branch information
lacraig2 committed Jan 22, 2024
1 parent c1c3eff commit 4b6da13
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 10 deletions.
2 changes: 1 addition & 1 deletion hw/virtio/Makefile.objs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-common.o vhost-vsock.o
ifeq ($(CONFIG_PCI),y)
obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-pci.o
endif
obj-$(CONFIG_VHOST_USER_VSOCK) += vhost-vsock-common.o vhost-user-vsock.o
obj-$(CONFIG_VHOST_USER_VSOCK) += vhost-vsock-common.o vhost-user-vsock.o vhost-user-vsock-pci.o
obj-y += virtio-crypto.o
obj-$(CONFIG_VIRTIO_PCI) += virtio-crypto-pci.o
endif
Expand Down
87 changes: 87 additions & 0 deletions hw/virtio/vhost-user-vsock-pci.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Vhost-user vsock PCI Bindings
*
* Copyright 2020 Red Hat, Inc.
*
* This work is licensed under the terms of the GNU GPL, version 2 or
* (at your option) any later version. See the COPYING file in the
* top-level directory.
*/

#include "qemu/osdep.h"

#include "hw/virtio/virtio-pci.h"
#include "hw/qdev-properties.h"
#include "hw/virtio/vhost-user-vsock.h"
#include "qom/object.h"

typedef struct VHostUserVSockPCI VHostUserVSockPCI;

/*
* vhost-user-vsock-pci: This extends VirtioPCIProxy.
*/
#define TYPE_VHOST_USER_VSOCK_PCI "vhost-user-vsock-pci-base"
#define VHOST_USER_VSOCK_PCI(obj) \
OBJECT_CHECK(VHostUserVSockPCI, (obj), TYPE_VHOST_USER_VSOCK_PCI)

struct VHostUserVSockPCI {
VirtIOPCIProxy parent_obj;
VHostUserVSock vdev;
};

/* vhost-user-vsock-pci */

static Property vhost_user_vsock_pci_properties[] = {
DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
DEFINE_PROP_END_OF_LIST(),
};

static void vhost_user_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
{
VHostUserVSockPCI *dev = VHOST_USER_VSOCK_PCI(vpci_dev);
DeviceState *vdev = DEVICE(&dev->vdev);

/* unlike vhost-vsock, we do not need to care about pre-5.1 compat */
virtio_pci_force_virtio_1(vpci_dev);

qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
object_property_set_bool(OBJECT(vdev), true, "realized", errp);
}

static void vhost_user_vsock_pci_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
k->realize = vhost_user_vsock_pci_realize;
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
dc->props = vhost_user_vsock_pci_properties;
pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
pcidev_k->revision = 0x00;
pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
}

static void vhost_user_vsock_pci_instance_init(Object *obj)
{
VHostUserVSockPCI *dev = VHOST_USER_VSOCK_PCI(obj);

virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
TYPE_VHOST_USER_VSOCK);
}

static const VirtioPCIDeviceTypeInfo vhost_user_vsock_pci_info = {
.base_name = TYPE_VHOST_USER_VSOCK_PCI,
.generic_name = "vhost-user-vsock-pci",
.non_transitional_name = "vhost-user-vsock-pci-non-transitional",
.instance_size = sizeof(VHostUserVSockPCI),
.instance_init = vhost_user_vsock_pci_instance_init,
.class_init = vhost_user_vsock_pci_class_init,
};

static void virtio_pci_vhost_register(void)
{
virtio_pci_types_register(&vhost_user_vsock_pci_info);
}

type_init(virtio_pci_vhost_register)
6 changes: 4 additions & 2 deletions hw/virtio/vhost-user-vsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ static void vuv_device_realize(DeviceState *dev, Error **errp)

vhost_vsock_common_realize(vdev, "vhost-user-vsock");

ret = vhost_set_backend_type(&vvc->vhost_dev, VHOST_BACKEND_TYPE_USER);
assert(ret >= 0);
vhost_dev_set_config_notifier(&vvc->vhost_dev, &vsock_ops);

ret = vhost_dev_init(&vvc->vhost_dev, &vsock->vhost_user,
Expand All @@ -130,7 +132,7 @@ static void vuv_device_realize(DeviceState *dev, Error **errp)
return;
}

static void vuv_device_unrealize(DeviceState *dev)
static void vuv_device_unrealize(DeviceState *dev, Error **errp)
{
VHostVSockCommon *vvc = VHOST_VSOCK_COMMON(dev);
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
Expand All @@ -157,7 +159,7 @@ static void vuv_class_init(ObjectClass *klass, void *data)
DeviceClass *dc = DEVICE_CLASS(klass);
VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);

device_class_set_props(dc, vuv_properties);
dc->props = vuv_properties;
dc->vmsd = &vuv_vmstate;
vdc->realize = vuv_device_realize;
vdc->unrealize = vuv_device_unrealize;
Expand Down
72 changes: 65 additions & 7 deletions hw/virtio/vhost-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sys/socket.h>
#include <sys/un.h>
#include <linux/vhost.h>
#include "hw/virtio/vhost-user.h"

#define VHOST_MEMORY_MAX_NREGIONS 8
#define VHOST_USER_F_PROTOCOL_FEATURES 30
Expand All @@ -40,7 +41,15 @@ enum VhostUserProtocolFeature {
VHOST_USER_PROTOCOL_F_NET_MTU = 4,
VHOST_USER_PROTOCOL_F_SLAVE_REQ = 5,
VHOST_USER_PROTOCOL_F_CROSS_ENDIAN = 6,

VHOST_USER_PROTOCOL_F_CRYPTO_SESSION = 7,
VHOST_USER_PROTOCOL_F_PAGEFAULT = 8,
VHOST_USER_PROTOCOL_F_CONFIG = 9,
VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD = 10,
VHOST_USER_PROTOCOL_F_HOST_NOTIFIER = 11,
VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD = 12,
VHOST_USER_PROTOCOL_F_RESET_DEVICE = 13,
/* Feature 14 reserved for VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS. */
VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS = 15,
VHOST_USER_PROTOCOL_F_MAX
};

Expand Down Expand Up @@ -73,6 +82,19 @@ typedef enum VhostUserRequest {
VHOST_USER_SET_VRING_ENDIAN = 23,
VHOST_USER_GET_CONFIG = 24,
VHOST_USER_SET_CONFIG = 25,
VHOST_USER_CREATE_CRYPTO_SESSION = 26,
VHOST_USER_CLOSE_CRYPTO_SESSION = 27,
VHOST_USER_POSTCOPY_ADVISE = 28,
VHOST_USER_POSTCOPY_LISTEN = 29,
VHOST_USER_POSTCOPY_END = 30,
VHOST_USER_GET_INFLIGHT_FD = 31,
VHOST_USER_SET_INFLIGHT_FD = 32,
VHOST_USER_GPU_SET_SOCKET = 33,
VHOST_USER_RESET_DEVICE = 34,
/* Message number 35 reserved for VHOST_USER_VRING_KICK. */
VHOST_USER_GET_MAX_MEM_SLOTS = 36,
VHOST_USER_ADD_MEM_REG = 37,
VHOST_USER_REM_MEM_REG = 38,
VHOST_USER_MAX
} VhostUserRequest;

Expand Down Expand Up @@ -145,6 +167,8 @@ static VhostUserMsg m __attribute__ ((unused));
#define VHOST_USER_VERSION (0x1)

struct vhost_user {
struct vhost_dev *dev;
VhostUserState *user;
CharBackend *chr;
int slave_fd;
};
Expand All @@ -156,7 +180,8 @@ static bool ioeventfd_enabled(void)

static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)
{
CharBackend *chr = dev->opaque;
struct vhost_user *u = dev->opaque;
CharBackend *chr = u->user->chr;
uint8_t *p = (uint8_t *) msg;
int r, size = VHOST_USER_HDR_SIZE;

Expand Down Expand Up @@ -241,7 +266,8 @@ static bool vhost_user_one_time_request(VhostUserRequest request)
static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg,
int *fds, int fd_num)
{
CharBackend *chr = dev->opaque;
struct vhost_user *u = dev->opaque;
CharBackend *chr = u->user->chr;
int ret, size = VHOST_USER_HDR_SIZE + msg->size;

/*
Expand Down Expand Up @@ -757,18 +783,50 @@ static int vhost_setup_slave_channel(struct vhost_dev *dev)

return ret;
}
bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
{
if (user->chr) {
error_setg(errp, "Cannot initialize vhost-user state");
return false;
}
user->chr = chr;
return true;
}

void vhost_user_cleanup(VhostUserState *user)
{
int i;

if (!user->chr) {
return;
}

for (i = 0; i < VIRTIO_QUEUE_MAX; i++) {
if (user->notifier[i].addr) {
object_unparent(OBJECT(&user->notifier[i].mr));
munmap(user->notifier[i].addr, qemu_real_host_page_size);
user->notifier[i].addr = NULL;
}
}
user->chr = NULL;
}


static int vhost_user_init(struct vhost_dev *dev, void *opaque)
static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)
{
uint64_t features, protocol_features;
struct vhost_user *u;
VhostUserState *vus = (VhostUserState *)opaque;
int err;

assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);

u = g_new0(struct vhost_user, 1);

u->chr = opaque;
u->user = vus;
u->slave_fd = -1;
u->dev = dev;
dev->opaque = u;

err = vhost_user_get_features(dev, &features);
Expand Down Expand Up @@ -828,7 +886,7 @@ static int vhost_user_init(struct vhost_dev *dev, void *opaque)
return 0;
}

static int vhost_user_cleanup(struct vhost_dev *dev)
static int vhost_user_backend_cleanup(struct vhost_dev *dev)
{
struct vhost_user *u;

Expand Down Expand Up @@ -1049,8 +1107,8 @@ static int vhost_user_send_device_iotlb_msg(struct vhost_dev *dev,

const VhostOps user_ops = {
.backend_type = VHOST_BACKEND_TYPE_USER,
.vhost_backend_init = vhost_user_init,
.vhost_backend_cleanup = vhost_user_cleanup,
.vhost_backend_init = vhost_user_backend_init,
.vhost_backend_cleanup = vhost_user_backend_cleanup,
.vhost_backend_memslots_limit = vhost_user_memslots_limit,
.vhost_set_log_base = vhost_user_set_log_base,
.vhost_set_mem_table = vhost_user_set_mem_table,
Expand Down

0 comments on commit 4b6da13

Please sign in to comment.