exec: memory notifiers
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/15 exec: memory notifiers
This adds notifiers for phys memory changes: a set of callbacks that vhost can register and update kernel accordingly. Down the road, kvm code can be switched to use these as well, instead of calling kvm code directly from exec.c as is done now.
--- cpu-common.h | 19 ++++++++++ exec.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 129 insertions(+), 3 deletions(-)
cpu-common.h +19 -0
@@ -8,6 +8,7 @@#endif #include "bswap.h" +#include "qemu-queue.h" /* address in the RAM (different from a physical address) */ typedef unsigned long ram_addr_t ;
@@ -61,6 +62,24 @@ void cpu_physical_memory_unmap(void *buffer, target_phys_addr_t len,void *cpu_register_map_client (void *opaque,void (*callback)(void *opaque));void cpu_unregister_map_client (void *cookie); +struct CPUPhysMemoryClient ; +typedef struct CPUPhysMemoryClient CPUPhysMemoryClient ; +struct CPUPhysMemoryClient { +void (*set_memory)(struct CPUPhysMemoryClient *client, +target_phys_addr_t start_addr, +ram_addr_t size, +ram_addr_t phys_offset); +int (*sync_dirty_bitmap)(struct CPUPhysMemoryClient *client, +target_phys_addr_t start_addr, +target_phys_addr_t end_addr); +int (*migration_log)(struct CPUPhysMemoryClient *client, +int enable); +QLIST_ENTRY (CPUPhysMemoryClient) list; +}; + +void cpu_register_phys_memory_client (CPUPhysMemoryClient *); +void cpu_unregister_phys_memory_client (CPUPhysMemoryClient *); +uint32_t ldub_phys (target_phys_addr_t addr);uint32_t lduw_phys (target_phys_addr_t addr);uint32_t ldl_phys (target_phys_addr_t addr);
exec.c +110 -3
141 lines not on this page · raw message
Signature · 1 line
1.6.6.144.g5c3af
PATCH 2/15 kvm: move kvm_set_phys_mem around
In reply to a message not in the archivemove kvm_set_phys_mem so that it will
be later available earlier in the file. needed for next patch using memory notifiers.
--- kvm-all.c | 276 ++++++++++++++++++++++++++++++------------------------------ 1 files changed, 138 insertions(+), 138 deletions(-)
kvm-all.c +138 -138
@@ -394,6 +394,144 @@ int kvm_check_extension(KVMState *s, unsigned int extension)return ret; } +void kvm_set_phys_mem (target_phys_addr_t start_addr, +ram_addr_t size, +ram_addr_t phys_offset) +{ +KVMState *s = kvm_state; +ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK ; +KVMSlot *mem, old; +int err; + +if (start_addr & ~TARGET_PAGE_MASK ) { +if (flags >=IO_MEM_UNASSIGNED ) { +if (!kvm_lookup_overlapping_slot (s, start_addr, + start_addr + size)) { +return ; + } +fprintf (stderr,"Unaligned split of a KVM memory slot\n" ); + }else { +fprintf (stderr,"Only page-aligned memory slots supported\n" ); + } +abort (); + } + +/* KVM does not support read-only slots */ + phys_offset &= ~IO_MEM_ROM ; + +while (1 ) { + mem =kvm_lookup_overlapping_slot (s, start_addr, start_addr + size); +if (!mem) { +break ; + } + +if (flags <IO_MEM_UNASSIGNED && start_addr >= mem->start_addr && + (start_addr + size <= mem->start_addr + mem->memory_size) && + (phys_offset - start_addr == mem->phys_offset - mem->start_addr)) { +/* The new slot fits into the existing one and comes with +* identical parameters - nothing to be done. */ +return ; + } + + old = *mem; + +/* unregister the overlapping slot */ + mem->memory_size =0 ; + err =kvm_set_user_memory_region (s, mem); +if (err) { +fprintf (stderr,"%s: error unregistering overlapping slot: %s\n" , + __func__,strerror (-err)); +abort (); + } + +/* Workaround for older KVM versions: we can't join slots, even not by +* unregistering the previous ones and then registering the larger +* slot. We have to maintain the existing fragmentation. Sigh. +* +* This workaround assumes that the new slot starts at the same +* address as the first existing one. If not or if some overlapping +* slot comes around later, we will fail (not seen in practice so far) +* - and actually require a recent KVM version. */ +if (s->broken_set_mem_region && + old.start_addr == start_addr && old.memory_size < size && + flags <IO_MEM_UNASSIGNED ) { + mem =kvm_alloc_slot (s); + mem->memory_size = old.memory_size; + mem->start_addr = old.start_addr; + mem->phys_offset = old.phys_offset; + mem->flags =0 ; + + err =kvm_set_user_memory_region (s, mem); +if (err) { +fprintf (stderr,"%s: error updating slot: %s\n" , __func__, +strerror (-err)); +abort (); + } + + start_addr += old.memory_size; + phys_offset += old.memory_size; + size -= old.memory_size; +continue ; + } + +/* register prefix slot */ +if (old.start_addr < start_addr) { + mem =kvm_alloc_slot (s); + mem->memory_size = start_addr - old.start_addr; + mem->start_addr = old.start_addr; + mem->phys_offset = old.phys_offset; + mem->flags =0 ; + + err =kvm_set_user_memory_region (s, mem); +if (err) { +fprintf (stderr,"%s: error registering prefix slot: %s\n" , + __func__,strerror (-err)); +abort (); + } + } + +/* register suffix slot */ +if (old.start_addr + old.memory_size > start_addr + size) { +ram_addr_t size_delta; + + mem =kvm_alloc_slot (s); + mem->start_addr = start_addr + size; + size_delta = mem->start_addr - old.start_addr; + mem->memory_size = old.memory_size - size_delta; + mem->phys_offset = old.phys_offset + size_delta; + mem->flags =0 ; + + err =kvm_set_user_memory_region (s, mem); +if (err) { +fprintf (stderr,"%s: error registering suffix slot: %s\n" , + __func__,strerror (-err)); +abort (); + } + } + } + +/* in case the KVM bug workaround already "consumed" the new slot */ +if (!size) +return ; + +/* KVM does not need to know about this memory */ +if (flags >=IO_MEM_UNASSIGNED ) +return ; + + mem =kvm_alloc_slot (s); + mem->memory_size = size; + mem->start_addr = start_addr; + mem->phys_offset = phys_offset; + mem->flags =0 ; + + err =kvm_set_user_memory_region (s, mem); +if (err) { +fprintf (stderr,"%s: error registering slot: %s\n" , __func__, +strerror (-err)); +abort (); + } +} +int kvm_init (int smp_cpus) { static const char upgrade_note[] =
@@ -674,144 +812,6 @@ int kvm_cpu_exec(CPUState *env)return ret; } -void kvm_set_phys_mem (target_phys_addr_t start_addr, -ram_addr_t size, -ram_addr_t phys_offset) -{ -KVMState *s = kvm_state; -ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK ; -KVMSlot *mem, old; -int err; - -if (start_addr & ~TARGET_PAGE_MASK ) { -if (flags >=IO_MEM_UNASSIGNED ) { -if (!kvm_lookup_overlapping_slot (s, start_addr, - start_addr + size)) { -return ; - } -fprintf (stderr,"Unaligned split of a KVM memory slot\n" ); - }else { -fprintf (stderr,"Only page-aligned memory slots supported\n" ); - } -abort (); - } - -/* KVM does not support read-only slots */ - phys_offset &= ~IO_MEM_ROM ; - -while (1 ) { - mem =kvm_lookup_overlapping_slot (s, start_addr, start_addr + size); -if (!mem) { -break ; - } - -if (flags <IO_MEM_UNASSIGNED && start_addr >= mem->start_addr && - (start_addr + size <= mem->start_addr + mem->memory_size) && - (phys_offset - start_addr == mem->phys_offset - mem->start_addr)) { -/* The new slot fits into the existing one and comes with -* identical parameters - nothing to be done. */ -return ; - } - - old = *mem; - -/* unregister the overlapping slot */ - mem->memory_size =0 ; - err =kvm_set_user_memory_region (s, mem); -if (err) { -fprintf (stderr,"%s: error unregistering overlapping slot: %s\n" , - __func__,strerror (-err)); -abort (); - } - -/* Workaround for older KVM versions: we can't join slots, even not by -* unregistering the previous ones and then registering the larger -* slot. We have to maintain the existing fragmentation. Sigh. -* -* This workaround assumes that the new slot starts at the same -* address as the first existing one. If not or if some overlapping -* slot comes around later, we will fail (not seen in practice so far) -* - and actually require a recent KVM version. */ -if (s->broken_set_mem_region && - old.start_addr == start_addr && old.memory_size < size && - flags <IO_MEM_UNASSIGNED ) { - mem =kvm_alloc_slot (s); - mem->memory_size = old.memory_size; - mem->start_addr = old.start_addr; - mem->phys_offset = old.phys_offset; - mem->flags =0 ; - - err =kvm_set_user_memory_region (s, mem); -if (err) { -fprintf (stderr,"%s: error updating slot: %s\n" , __func__, -strerror (-err)); -abort (); - } - - start_addr += old.memory_size; - phys_offset += old.memory_size; - size -= old.memory_size; -continue ; - } - -/* register prefix slot */ -if (old.start_addr < start_addr) { - mem =kvm_alloc_slot (s); - mem->memory_size = start_addr - old.start_addr; - mem->start_addr = old.start_addr; - mem->phys_offset = old.phys_offset; - mem->flags =0 ; - - err =kvm_set_user_memory_region (s, mem); -if (err) { -fprintf (stderr,"%s: error registering prefix slot: %s\n" , - __func__,strerror (-err)); -abort (); - } - } - -/* register suffix slot */ -if (old.start_addr + old.memory_size > start_addr + size) { -ram_addr_t size_delta; - - mem =kvm_alloc_slot (s); - mem->start_addr = start_addr + size; - size_delta = mem->start_addr - old.start_addr; - mem->memory_size = old.memory_size - size_delta; - mem->phys_offset = old.phys_offset + size_delta; - mem->flags =0 ; - - err =kvm_set_user_memory_region (s, mem); -if (err) { -fprintf (stderr,"%s: error registering suffix slot: %s\n" , - __func__,strerror (-err)); -abort (); - } - } - } - -/* in case the KVM bug workaround already "consumed" the new slot */ -if (!size) -return ; - -/* KVM does not need to know about this memory */ -if (flags >=IO_MEM_UNASSIGNED ) -return ; - - mem =kvm_alloc_slot (s); - mem->memory_size = size; - mem->start_addr = start_addr; - mem->phys_offset = phys_offset; - mem->flags =0 ; - - err =kvm_set_user_memory_region (s, mem); -if (err) { -fprintf (stderr,"%s: error registering slot: %s\n" , __func__, -strerror (-err)); -abort (); - } -} -int kvm_ioctl (KVMState *s,int type, ...) {int ret;
Signature · 1 line
1.6.6.144.g5c3af
PATCH 3/15 kvm: move kvm to use memory notifiers
In reply to a message not in the archiveremove direct kvm calls from exec.c, make
kvm use memory notifiers framework instead.
--- exec.c | 17 +---------------- kvm-all.c | 40 ++++++++++++++++++++++++++++++++++------ kvm.h | 8 -------- 3 files changed, 35 insertions(+), 30 deletions(-)
exec.c +1 -16
@@ -1979,12 +1979,6 @@ int cpu_physical_memory_set_dirty_tracking(int enable) {int ret =0 ; in_migration = enable; -if (kvm_enabled ()) { - ret =kvm_set_migration_log (enable); - } -if (ret <0 ) { -return ret; - } ret =cpu_notify_migration_log (!!enable);return ret; }
@@ -1997,14 +1991,8 @@ int cpu_physical_memory_get_dirty_tracking(void)int cpu_physical_sync_dirty_bitmap (target_phys_addr_t start_addr,target_phys_addr_t end_addr) { -int ret =0 ; +int ret; -if (kvm_enabled ()) { - ret =kvm_physical_sync_dirty_bitmap (start_addr, end_addr); - } -if (ret <0 ) { -return ret; - } ret =cpu_notify_sync_dirty_bitmap (start_addr, end_addr);return ret; }
@@ -2417,9 +2405,6 @@ void cpu_register_physical_memory_offset(target_phys_addr_t start_addr,ram_addr_t orig_size = size;void *subpage; -if (kvm_enabled ()) -kvm_set_phys_mem (start_addr, size, phys_offset); -cpu_notify_set_memory (start_addr, size, phys_offset);if (phys_offset ==IO_MEM_UNASSIGNED ) {
kvm-all.c +34 -6
@@ -257,7 +257,7 @@ int kvm_log_stop(target_phys_addr_t phys_addr, ram_addr_t size) KVM_MEM_LOG_DIRTY_PAGES); } -int kvm_set_migration_log (int enable) +static int kvm_set_migration_log (int enable) {KVMState *s = kvm_state;KVMSlot *mem;
@@ -292,8 +292,8 @@ static int test_le_bit(unsigned long nr, unsigned char *addr)* @start_add: start of logged region. * @end_addr: end of logged region. */ -int kvm_physical_sync_dirty_bitmap (target_phys_addr_t start_addr, -target_phys_addr_t end_addr) +static int kvm_physical_sync_dirty_bitmap (target_phys_addr_t start_addr, +target_phys_addr_t end_addr) {KVMState *s = kvm_state;unsigned long size, allocated_size =0 ;
@@ -394,9 +394,9 @@ int kvm_check_extension(KVMState *s, unsigned int extension)return ret; } -void kvm_set_phys_mem (target_phys_addr_t start_addr, -ram_addr_t size, -ram_addr_t phys_offset) +static void kvm_set_phys_mem (target_phys_addr_t start_addr, +ram_addr_t size, +ram_addr_t phys_offset) {KVMState *s = kvm_state;ram_addr_t flags = phys_offset & ~TARGET_PAGE_MASK ;
@@ -532,6 +532,33 @@ void kvm_set_phys_mem(target_phys_addr_t start_addr, } } +static void kvm_client_set_memory (struct CPUPhysMemoryClient *client, +target_phys_addr_t start_addr, +ram_addr_t size, +ram_addr_t phys_offset) +{ +kvm_set_phys_mem (start_addr, size, phys_offset); +} + +static int kvm_client_sync_dirty_bitmap (struct CPUPhysMemoryClient *client, +target_phys_addr_t start_addr, +target_phys_addr_t end_addr) +{ +return kvm_physical_sync_dirty_bitmap (start_addr, end_addr); +} + +static int kvm_client_migration_log (struct CPUPhysMemoryClient *client, +int enable) +{ +return kvm_set_migration_log (enable); +} + +static CPUPhysMemoryClient kvm_cpu_phys_memory_client = { + .set_memory = kvm_client_set_memory, + .sync_dirty_bitmap = kvm_client_sync_dirty_bitmap, + .migration_log = kvm_client_migration_log, +}; +int kvm_init (int smp_cpus) { static const char upgrade_note[] =
@@ -628,6 +655,7 @@ int kvm_init(int smp_cpus) goto err; kvm_state = s; +cpu_register_phys_memory_client (&kvm_cpu_phys_memory_client);return 0 ;
kvm.h +0 -8
@@ -35,16 +35,8 @@ int kvm_init_vcpu(CPUState *env);int kvm_cpu_exec (CPUState *env); -void kvm_set_phys_mem (target_phys_addr_t start_addr, -ram_addr_t size, -ram_addr_t phys_offset); - -int kvm_physical_sync_dirty_bitmap (target_phys_addr_t start_addr, -target_phys_addr_t end_addr); -int kvm_log_start (target_phys_addr_t phys_addr,ram_addr_t size);int kvm_log_stop (target_phys_addr_t phys_addr,ram_addr_t size); -int kvm_set_migration_log (int enable);int kvm_has_sync_mmu (void );int kvm_has_vcpu_events (void );
Signature · 1 line
1.6.6.144.g5c3af
PATCH 4/15 kvm: add API to set ioeventfd
In reply to a message not in the archiveThis adds API to set ioeventfd to kvm,
as well as stubs for non-eventfd case,
making it possible for users to use this API
without ifdefs.
--- kvm-all.c | 20 ++++++++++++++++++++ kvm.h | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 0 deletions(-)
kvm-all.c +20 -0
@@ -1113,3 +1113,23 @@ void kvm_remove_all_breakpoints(CPUState *current_env) { }#endif /* !KVM_CAP_SET_GUEST_DEBUG */ + +#ifdef KVM_IOEVENTFD +int kvm_set_ioeventfd (uint16_t addr,uint16_t data,int fd,bool assigned) +{ +struct kvm_ioeventfd kick = { + .datamatch = data, + .addr = addr, + .len =2 , + .flags =KVM_IOEVENTFD_FLAG_DATAMATCH |KVM_IOEVENTFD_FLAG_PIO , + .fd = fd, + }; +int r; +if (!assigned) + kick.flags |=KVM_IOEVENTFD_FLAG_DEASSIGN ; + r =kvm_vm_ioctl (kvm_state,KVM_IOEVENTFD , &kick); +if (r <0 ) +return r; +return 0 ; +} +#endif
kvm.h +16 -0
@@ -14,10 +14,16 @@#ifndef QEMU_KVM_H #define QEMU_KVM_H +#include <stdbool.h> +#include <errno.h> #include "config.h" #include "qemu-queue.h" #ifdef CONFIG_KVM +#include <linux/kvm.h> +#endif + +#ifdef CONFIG_KVM extern int kvm_allowed;#define kvm_enabled () (kvm_allowed)
@@ -131,4 +137,14 @@ static inline void cpu_synchronize_state(CPUState *env) } } +#if defined(KVM_IOEVENTFD ) && defined(CONFIG_KVM ) +int kvm_set_ioeventfd (uint16_t addr,uint16_t data,int fd,bool assigned); +#else +static inline +int kvm_set_ioeventfd (uint16_t data,uint16_t addr,int fd,bool assigned) +{ +return -ENOSYS ; +} +#endif +#endif
Signature · 1 line
1.6.6.144.g5c3af
PATCH 5/15 notifier: event notifier implementation
In reply to a message not in the archiveevent notifiers are slightly generalized eventfd descriptors. Current implementation depends on eventfd because vhost is the only user, and vhost depends on eventfd anyway, but a stub is provided for non-eventfd case.
We'll be able to further generalize this when another user comes along and we see how to best do this.
--- Makefile.target | 1 + hw/notifier.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/notifier.h | 16 ++++++++++++++++ qemu-common.h | 1 + 4 files changed, 68 insertions(+), 0 deletions(-) create mode 100644 hw/notifier.c create mode 100644 hw/notifier.h
Makefile.target +1 -0
@@ -173,6 +173,7 @@ obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o # virtio has to be here due to weird dependency between PCI and virtio-net. # need to fix this properly obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o virtio-serial-bus.o +obj-y += notifier.o obj-$(CONFIG_KVM) += kvm.o kvm-all.o obj-$(CONFIG_ISA_MMIO) += isa_mmio.o LIBS+=-lz
hw/notifier.c new file +50 -0
50 lines not on this page · raw message
hw/notifier.h new file +16 -0
@@ -0,0 +1,16 @@ +#ifndef QEMU_EVENT_NOTIFIER_H +#define QEMU_EVENT_NOTIFIER_H + +#include "qemu-common.h" + +struct EventNotifier { +int fd; +}; + +int event_notifier_init (EventNotifier *,int active); +void event_notifier_cleanup (EventNotifier *); +int event_notifier_get_fd (EventNotifier *); +int event_notifier_test_and_clear (EventNotifier *); +int event_notifier_test (EventNotifier *); + +#endif
qemu-common.h +1 -0
@@ -224,6 +224,7 @@ typedef struct uWireSlave uWireSlave;typedef struct I2SCodec I2SCodec ;typedef struct DeviceState DeviceState ;typedef struct SSIBus SSIBus ; +typedef struct EventNotifier EventNotifier ;/* CPU save/load. */ void cpu_save (QEMUFile *f,void *opaque);
Signature · 1 line
1.6.6.144.g5c3af
PATCH 6/15 virtio: add notifier support
In reply to a message not in the archiveAdd binding API to set host/guest notifiers.
Will be used by vhost.
--- hw/virtio.c | 13 ++++++++++--- hw/virtio.h | 5 ++++- 2 files changed, 14 insertions(+), 4 deletions(-)
hw/virtio.c +10 -3
@@ -592,6 +592,12 @@ VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,return &vdev->vq[i]; } +void virtio_irq (VirtIODevice *vdev,VirtQueue *vq) +{ + vdev->isr |=0x01 ; +virtio_notify_vector (vdev, vq->vector); +} +void virtio_notify (VirtIODevice *vdev,VirtQueue *vq) {/* Always notify when queue is empty (when feature acknowledge) */
@@ -600,8 +606,7 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) (vq->inuse || vring_avail_idx(vq) != vq->last_avail_idx))) return; - vdev->isr |=0x01 ; -virtio_notify_vector (vdev, vq->vector); +virtio_irq (vdev, vq); }void virtio_notify_config (VirtIODevice *vdev)
@@ -714,8 +719,10 @@ VirtIODevice *virtio_common_init(const char *name, uint16_t device_id, vdev->queue_sel =0 ; vdev->config_vector =VIRTIO_NO_VECTOR ; vdev->vq =qemu_mallocz (sizeof (VirtQueue) *VIRTIO_PCI_QUEUE_MAX ); -for (i =0 ; i <VIRTIO_PCI_QUEUE_MAX ; i++) +for (i =0 ; i <VIRTIO_PCI_QUEUE_MAX ; i++) { vdev->vq[i].vector =VIRTIO_NO_VECTOR ; + vdev->vq[i].vdev = vdev; + } vdev->name = name; vdev->config_len = config_size;
hw/virtio.h +4 -1
@@ -18,6 +18,7 @@#include "net.h" #include "qdev.h" #include "sysemu.h" +#include "notifier.h" /* from Linux's linux/virtio_config.h */
@@ -88,6 +89,8 @@ typedef struct {int (*load_config)(void * opaque,QEMUFile *f);int (*load_queue)(void * opaque,int n,QEMUFile *f);unsigned (*get_features)(void * opaque); +int (*guest_notifier)(void * opaque,int n,bool assigned); +int (*host_notifier)(void * opaque,int n,bool assigned); } VirtIOBindings;#define VIRTIO_PCI_QUEUE_MAX 64
@@ -180,5 +183,5 @@ void virtio_net_exit(VirtIODevice *vdev);DEFINE_PROP_BIT ("indirect_desc" , _state, _field, \ VIRTIO_RING_F_INDIRECT_DESC, true) - +void virtio_irq (VirtIODevice *vdev,VirtQueue *vq);#endif
Signature · 1 line
1.6.6.144.g5c3af
PATCH 7/15 virtio: add APIs for queue fields
In reply to a message not in the archivevhost needs physical addresses for ring and other queue fields, so add APIs for these.
--- hw/virtio.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- hw/virtio.h | 10 +++++++++- 2 files changed, 56 insertions(+), 5 deletions(-)
hw/virtio.c +47 -4
@@ -73,6 +73,9 @@ struct VirtQueueint inuse;uint16_t vector;void (*handle_output)(VirtIODevice *vdev,VirtQueue *vq); +VirtIODevice *vdev; +EventNotifier guest_notifier; +EventNotifier host_notifier; };/* virt queue functions */
@@ -592,10 +595,10 @@ VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,return &vdev->vq[i]; } -void virtio_irq (VirtIODevice *vdev,VirtQueue *vq) +void virtio_irq (VirtQueue *vq) { - vdev->isr |=0x01 ; -virtio_notify_vector (vdev, vq->vector); + vq->vdev->isr |=0x01 ; +virtio_notify_vector (vq->vdev, vq->vector); }void virtio_notify (VirtIODevice *vdev,VirtQueue *vq)
@@ -606,7 +609,8 @@ void virtio_notify(VirtIODevice *vdev, VirtQueue *vq) (vq->inuse || vring_avail_idx(vq) != vq->last_avail_idx))) return; -virtio_irq (vdev, vq); + vdev->isr |=0x01 ; +virtio_notify_vector (vdev, vq->vector); }void virtio_notify_config (VirtIODevice *vdev)
@@ -740,3 +744,42 @@ void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding, vdev->binding = binding; vdev->binding_opaque = opaque; } + +target_phys_addr_t virtio_queue_get_desc (VirtIODevice *vdev,int n) +{ +return vdev->vq[n].vring.desc; +} + +target_phys_addr_t virtio_queue_get_avail (VirtIODevice *vdev,int n) +{ +return vdev->vq[n].vring.avail; +} + +target_phys_addr_t virtio_queue_get_used (VirtIODevice *vdev,int n) +{ +return vdev->vq[n].vring.used; +} + +uint16_t virtio_queue_last_avail_idx (VirtIODevice *vdev,int n) +{ +return vdev->vq[n].last_avail_idx; +} + +void virtio_queue_set_last_avail_idx (VirtIODevice *vdev,int n,uint16_t idx) +{ + vdev->vq[n].last_avail_idx = idx; +} + +VirtQueue *virtio_queue (VirtIODevice *vdev,int n) +{ +return vdev->vq + n; +} + +EventNotifier *virtio_queue_guest_notifier (VirtQueue *vq) +{ +return &vq->guest_notifier; +} +EventNotifier *virtio_queue_host_notifier (VirtQueue *vq) +{ +return &vq->host_notifier; +}
hw/virtio.h +9 -1
@@ -183,5 +183,13 @@ void virtio_net_exit(VirtIODevice *vdev);DEFINE_PROP_BIT ("indirect_desc" , _state, _field, \ VIRTIO_RING_F_INDIRECT_DESC, true) -void virtio_irq (VirtIODevice *vdev,VirtQueue *vq); +target_phys_addr_t virtio_queue_get_desc (VirtIODevice *vdev,int n); +target_phys_addr_t virtio_queue_get_avail (VirtIODevice *vdev,int n); +target_phys_addr_t virtio_queue_get_used (VirtIODevice *vdev,int n); +uint16_t virtio_queue_last_avail_idx (VirtIODevice *vdev,int n); +void virtio_queue_set_last_avail_idx (VirtIODevice *vdev,int n,uint16_t idx); +VirtQueue *virtio_queue (VirtIODevice *vdev,int n); +EventNotifier *virtio_queue_guest_notifier (VirtQueue *vq); +EventNotifier *virtio_queue_host_notifier (VirtQueue *vq); +void virtio_irq (VirtQueue *vq);#endif
Signature · 1 line
1.6.6.144.g5c3af
PATCH 8/15 virtio: add status change callback
In reply to a message not in the archivevhost net backend needs to be notified when
frontend status changes. Add a callback.
--- hw/s390-virtio-bus.c | 3 +++ hw/syborg_virtio.c | 2 ++ hw/virtio-pci.c | 6 ++++++ hw/virtio.h | 1 + 4 files changed, 12 insertions(+), 0 deletions(-)
hw/s390-virtio-bus.c +3 -0
@@ -243,6 +243,9 @@ void s390_virtio_device_update_status(VirtIOS390Device *dev)uint32_t features; vdev->status =ldub_phys (dev->dev_offs +VIRTIO_DEV_OFFS_STATUS ); +if (vdev->set_status) { + vdev->set_status (vdev); + }/* Update guest supported feature bitmap */
hw/syborg_virtio.c +2 -0
@@ -152,6 +152,8 @@ static void syborg_virtio_writel(void *opaque, target_phys_addr_t offset, vdev->status = value &0xFF ;if (vdev->status ==0 )virtio_reset (vdev); +if (vdev->set_status) + vdev->set_status (vdev);break ;case SYBORG_VIRTIO_INT_ENABLE : s->int_enable = value;
hw/virtio-pci.c +6 -0
@@ -210,6 +210,9 @@ static void virtio_ioport_write(void *opaque, uint32_t addr, uint32_t val)virtio_reset (proxy->vdev);msix_unuse_all_vectors (&proxy->pci_dev); } +if (vdev->set_status) { + vdev->set_status (vdev); + }break ;case VIRTIO_MSI_CONFIG_VECTOR :msix_vector_unuse (&proxy->pci_dev, vdev->config_vector);
@@ -377,6 +380,9 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,if (PCI_COMMAND == address) {if (!(val &PCI_COMMAND_MASTER )) { proxy->vdev->status &= ~VIRTIO_CONFIG_S_DRIVER_OK ; +if (proxy->vdev->set_status) { + proxy->vdev->set_status (proxy->vdev); + } } }
hw/virtio.h +1 -0
@@ -114,6 +114,7 @@ struct VirtIODevicevoid (*get_config)(VirtIODevice *vdev,uint8_t *config);void (*set_config)(VirtIODevice *vdev,const uint8_t *config);void (*reset)(VirtIODevice *vdev); +void (*set_status)(VirtIODevice *vdev);VirtQueue *vq;const VirtIOBindings *binding;void *binding_opaque;
Signature · 1 line
1.6.6.144.g5c3af
PATCH 9/15 virtio: move typedef to qemu-common
In reply to a message not in the archivemake it possible to use type without header include
--- hw/virtio.h | 1 - qemu-common.h | 1 + 2 files changed, 1 insertions(+), 1 deletions(-)
hw/virtio.h +0 -1
@@ -68,7 +68,6 @@ static inline target_phys_addr_t vring_align(target_phys_addr_t addr, }typedef struct VirtQueue VirtQueue ; -typedef struct VirtIODevice VirtIODevice ;#define VIRTQUEUE_MAX_SIZE 1024
qemu-common.h +1 -0
@@ -225,6 +225,7 @@ typedef struct I2SCodec I2SCodec;typedef struct DeviceState DeviceState ;typedef struct SSIBus SSIBus ;typedef struct EventNotifier EventNotifier ; +typedef struct VirtIODevice VirtIODevice ;/* CPU save/load. */ void cpu_save (QEMUFile *f,void *opaque);
Signature · 1 line
1.6.6.144.g5c3af
PATCH 10/15 virtio-pci: fill in notifier support
In reply to a message not in the archiveSupport host/guest notifiers in virtio-pci.
The last one only with kvm, that's okay
because vhost relies on kvm anyway.
--- hw/virtio-pci.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 62 insertions(+), 0 deletions(-)
hw/virtio-pci.c +62 -0
@@ -23,6 +23,7 @@#include "msix.h" #include "net.h" #include "loader.h" +#include "kvm.h" /* from Linux's linux/virtio_pci.h */
@@ -396,6 +397,65 @@ static unsigned virtio_pci_get_features(void *opaque)return proxy->host_features; } +static void virtio_pci_guest_notifier_read (void *opaque) +{ +VirtQueue *vq = opaque; +EventNotifier *n =virtio_queue_guest_notifier (vq); +if (event_notifier_test_and_clear (n)) { +virtio_irq (vq); + } +} + +static int virtio_pci_guest_notifier (void *opaque,int n,bool assign) +{ +VirtIOPCIProxy *proxy = opaque; +VirtQueue *vq =virtio_queue (proxy->vdev, n); +EventNotifier *notifier =virtio_queue_guest_notifier (vq); + +if (assign) { +int r =event_notifier_init (notifier,0 ); +if (r <0 ) +return r; +qemu_set_fd_handler (event_notifier_get_fd (notifier), + virtio_pci_guest_notifier_read,NULL , vq); + }else { +qemu_set_fd_handler (event_notifier_get_fd (notifier), +NULL ,NULL , vq); +event_notifier_cleanup (notifier); + } + +return 0 ; +} + +static int virtio_pci_host_notifier (void *opaque,int n,bool assign) +{ +VirtIOPCIProxy *proxy = opaque; +VirtQueue *vq =virtio_queue (proxy->vdev, n); +EventNotifier *notifier =virtio_queue_host_notifier (vq); +int r; +if (assign) { + r =event_notifier_init (notifier,1 ); +if (r <0 ) { +return r; + } + r =kvm_set_ioeventfd (proxy->addr +VIRTIO_PCI_QUEUE_NOTIFY , + n,event_notifier_get_fd (notifier), + assign); +if (r <0 ) { +event_notifier_cleanup (notifier); + } + }else { + r =kvm_set_ioeventfd (proxy->addr +VIRTIO_PCI_QUEUE_NOTIFY , + n,event_notifier_get_fd (notifier), + assign); +if (r <0 ) { +return r; + } +event_notifier_cleanup (notifier); + } +return r; +} +static const VirtIOBindings virtio_pci_bindings = { .notify = virtio_pci_notify, .save_config = virtio_pci_save_config,
@@ -403,6 +463,8 @@ static const VirtIOBindings virtio_pci_bindings = { .save_queue = virtio_pci_save_queue, .load_queue = virtio_pci_load_queue, .get_features = virtio_pci_get_features, + .host_notifier = virtio_pci_host_notifier, + .guest_notifier = virtio_pci_guest_notifier, }; static void virtio_init_pci(VirtIOPCIProxy *proxy, VirtIODevice *vdev,
Signature · 1 line
1.6.6.144.g5c3af
PATCH 11/15 tap: add interface to get device fd
In reply to a message not in the archiveWill be used by vhost to attach/detach to backend.
--- net/tap.c | 7 +++++++ net/tap.h | 2 ++ 2 files changed, 9 insertions(+), 0 deletions(-)
net/tap.c +7 -0
@@ -269,6 +269,13 @@ static void tap_poll(VLANClientState *nc, bool enable)tap_write_poll (s, enable); } +int tap_get_fd (VLANClientState *nc) +{ +TAPState *s =DO_UPCAST (TAPState, nc, nc); +assert (nc->info->type ==NET_CLIENT_TYPE_TAP ); +return s->fd; +} +/* fd support */ static NetClientInfo net_tap_info = {
net/tap.h +2 -0
@@ -48,4 +48,6 @@ int tap_probe_vnet_hdr(int fd);int tap_probe_has_ufo (int fd);void tap_fd_set_offload (int fd,int csum,int tso4,int tso6,int ecn,int ufo); +int tap_get_fd (VLANClientState *vc); +#endif /* QEMU_NET_TAP_H */
Signature · 1 line
1.6.6.144.g5c3af
PATCH 12/15 vhost: vhost net support
In reply to a message not in the archiveThis adds vhost net support in qemu. Will be tied to tap device and virtio by following patches. Raw backend is currently missing, will be worked on/submitted separately.
--- Makefile.target | 1 + hw/vhost.c | 603 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vhost.h | 44 ++++ hw/vhost_net.c | 147 ++++++++++++++ hw/vhost_net.h | 20 ++ 5 files changed, 815 insertions(+), 0 deletions(-) create mode 100644 hw/vhost.c create mode 100644 hw/vhost.h create mode 100644 hw/vhost_net.c create mode 100644 hw/vhost_net.h
Makefile.target +1 -0
@@ -174,6 +174,7 @@ obj-y = vl.o async.o monitor.o pci.o pci_host.o pcie_host.o machine.o gdbstub.o # need to fix this properly obj-y += virtio-blk.o virtio-balloon.o virtio-net.o virtio-pci.o virtio-serial-bus.o obj-y += notifier.o +obj-y += vhost_net.o vhost.o obj-$(CONFIG_KVM) += kvm.o kvm-all.o obj-$(CONFIG_ISA_MMIO) += isa_mmio.o LIBS+=-lz
hw/vhost.c new file +603 -0
603 lines not on this page · raw message
hw/vhost.h new file +44 -0
44 lines not on this page · raw message
hw/vhost_net.c new file +147 -0
147 lines not on this page · raw message
hw/vhost_net.h new file +20 -0
@@ -0,0 +1,20 @@ +#ifndef VHOST_NET_H +#define VHOST_NET_H + +#include "net.h" + +struct vhost_net ; + +struct vhost_net *vhost_net_init (VLANClientState *backend,int devfd); + +int vhost_net_start (struct vhost_net *net, +VirtIODevice *dev); +void vhost_net_stop (struct vhost_net *net, +VirtIODevice *dev); + +void vhost_net_cleanup (struct vhost_net *net); + +unsigned vhost_net_get_features (struct vhost_net *net,unsigned features); +void vhost_net_ack_features (struct vhost_net *net,unsigned features); + +#endif
Signature · 1 line
1.6.6.144.g5c3af
PATCH 13/15 tap: add vhost/vhostfd options
In reply to a message not in the archive--- net.c | 8 ++++++++ net/tap.c | 29 +++++++++++++++++++++++++++++ qemu-options.hx | 4 +++- 3 files changed, 40 insertions(+), 1 deletions(-)
net.c +8 -0
@@ -976,6 +976,14 @@ static struct { .name = "vnet_hdr", .type = QEMU_OPT_BOOL, .help = "enable the IFF_VNET_HDR flag on the tap interface" + }, { + .name = "vhost", + .type = QEMU_OPT_BOOL, + .help = "enable vhost-net network accelerator", + }, { + .name = "vhostfd", + .type = QEMU_OPT_STRING, + .help = "file descriptor of an already opened vhost net device", },#endif /* _WIN32 */ {/* end of list */ }
net/tap.c +29 -0
@@ -41,6 +41,8 @@#include "net/tap-linux.h" +#include "hw/vhost_net.h" +/* Maximum GSO packet size (64k) plus plenty of room for * the ethernet and virtio_net headers */
@@ -57,6 +59,7 @@ typedef struct TAPState { unsigned int has_vnet_hdr : 1; unsigned int using_vnet_hdr : 1; unsigned int has_ufo: 1; +struct vhost_net *vhost_net; } TAPState;static int launch_script (const char *setup_script,const char *ifname,int fd);
@@ -252,6 +255,10 @@ static void tap_cleanup(VLANClientState *nc) {TAPState *s =DO_UPCAST (TAPState, nc, nc); +if (s->vhost_net) { +vhost_net_cleanup (s->vhost_net); + } +qemu_purge_queued_packets (nc);if (s->down_script[0 ])
@@ -307,6 +314,7 @@ static TAPState *net_tap_fd_init(VLANState *vlan, s->has_ufo =tap_probe_has_ufo (s->fd);tap_set_offload (&s->nc,0 ,0 ,0 ,0 ,0 );tap_read_poll (s,1 ); + s->vhost_net =NULL ;return s; }
@@ -456,6 +464,27 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlan } } +if (qemu_opt_get_bool (opts,"vhost" ,0 )) { +int vhostfd, r; +if (qemu_opt_get (opts,"vhostfd" )) { + r =net_handle_fd_param (mon,qemu_opt_get (opts,"vhostfd" )); +if (r ==-1 ) { +return -1 ; + } + vhostfd = r; + }else { + vhostfd =-1 ; + } + s->vhost_net =vhost_net_init (&s->nc, vhostfd); +if (!s->vhost_net) { +qemu_error ("vhost-net requested but could not be initialized\n" ); +return -1 ; + } + }else if (qemu_opt_get (opts,"vhostfd" )) { +qemu_error ("vhostfd= is not valid without vhost\n" ); +return -1 ; + } +if (vlan) { vlan->nb_host_devs++; }
qemu-options.hx +3 -1
@@ -831,7 +831,7 @@ DEF("net", HAS_ARG, QEMU_OPTION_net, "-net tap[,vlan=n][,name=str],ifname=name\n" " connect the host TAP network interface to VLAN 'n'\n" #else - "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off]\n" + "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile][,sndbuf=nbytes][,vnet_hdr=on|off][,vhost=on|off][,vhostfd=h]\n" " connect the host TAP network interface to VLAN 'n' and use the\n" " network scripts 'file' (default=%s)\n" " and 'dfile' (default=%s)\n"
@@ -841,6 +841,8 @@ DEF("net", HAS_ARG, QEMU_OPTION_net, " default of 'sndbuf=1048576' can be disabled using 'sndbuf=0')\n" " use vnet_hdr=off to avoid enabling the IFF_VNET_HDR tap flag\n" " use vnet_hdr=on to make the lack of IFF_VNET_HDR support an error condition\n" + " use vhost=on to enable experimental in kernel accelerator\n" + " use 'vhostfd=h' to connect to an already opened vhost net device\n" #endif "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n" " connect the vlan 'n' to another VLAN using a socket connection\n"
Signature · 1 line
1.6.6.144.g5c3af
PATCH 14/15 tap: add API to retrieve vhost net header
In reply to a message not in the archivewill be used by virtio-net for vhost net support
--- net/tap.c | 7 +++++++ net/tap.h | 3 +++ 2 files changed, 10 insertions(+), 0 deletions(-)
net/tap.c +7 -0
@@ -491,3 +491,10 @@ int net_init_tap(QemuOpts *opts, Monitor *mon, const char *name, VLANState *vlanreturn 0 ; } + +struct vhost_net *tap_get_vhost_net (VLANClientState *nc) +{ +TAPState *s =DO_UPCAST (TAPState, nc, nc); +assert (nc->info->type ==NET_CLIENT_TYPE_TAP ); +return s->vhost_net; +}
net/tap.h +3 -0
@@ -50,4 +50,7 @@ void tap_fd_set_offload(int fd, int csum, int tso4, int tso6, int ecn, int ufo);int tap_get_fd (VLANClientState *vc); +struct vhost_net ; +struct vhost_net *tap_get_vhost_net (VLANClientState *vc); +#endif /* QEMU_NET_TAP_H */
Signature · 1 line
1.6.6.144.g5c3af
PATCH 15/15 virtio-net: vhost net support
In reply to a message not in the archiveThis connects virtio-net to vhost net backend.
The code is structured in a way analogous to what we have with vnet header capability in tap. We start/stop backend on driver start/stop as well as on save and vm start (for migration).
--- hw/virtio-net.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 65 insertions(+), 2 deletions(-)
hw/virtio-net.c +65 -2
@@ -17,6 +17,7 @@#include "net/tap.h" #include "qemu-timer.h" #include "virtio-net.h" +#include "vhost_net.h" #define VIRTIO_NET_VM_VERSION 11
@@ -47,6 +48,8 @@ typedef struct VirtIONetuint8_t nomulti;uint8_t nouni;uint8_t nobcast; +uint8_t vhost_started; +VMChangeStateEntry *vmstate;struct {int in_use;int first_multi;
@@ -114,6 +117,10 @@ static void virtio_net_reset(VirtIODevice *vdev) n->nomulti =0 ; n->nouni =0 ; n->nobcast =0 ; +if (n->vhost_started) { +vhost_net_stop (tap_get_vhost_net (n->nic->nc.peer), vdev); + n->vhost_started =0 ; + }/* Flush any MAC and VLAN filter table state */ n->mac_table.in_use =0 ;
@@ -172,7 +179,10 @@ static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features) features &= ~(0x1 <<VIRTIO_NET_F_HOST_UFO ); } -return features; +if (!tap_get_vhost_net (n->nic->nc.peer)) { +return features; + } +return vhost_net_get_features (tap_get_vhost_net (n->nic->nc.peer), features); }static uint32_t virtio_net_bad_features (VirtIODevice *vdev)
@@ -690,6 +700,12 @@ static void virtio_net_save(QEMUFile *f, void *opaque) {VirtIONet *n = opaque; +if (n->vhost_started) { +/* TODO: should we really stop the backend? +* If we don't, it might keep writing to memory. */ +vhost_net_stop (tap_get_vhost_net (n->nic->nc.peer), &n->vdev); + n->vhost_started =0 ; + }virtio_save (&n->vdev, f);qemu_put_buffer (f, n->mac,ETH_ALEN );
@@ -802,7 +818,6 @@ static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)qemu_mod_timer (n->tx_timer,qemu_get_clock (vm_clock) +TX_TIMER_INTERVAL ); } -return 0 ; }
@@ -822,6 +837,47 @@ static NetClientInfo net_virtio_info = { .link_status_changed = virtio_net_set_link_status, }; +static void virtio_net_set_status (struct VirtIODevice *vdev) +{ +VirtIONet *n =to_virtio_net (vdev); +if (!n->nic->nc.peer) { +return ; + } +if (n->nic->nc.peer->info->type !=NET_CLIENT_TYPE_TAP ) { +return ; + } + +if (!tap_get_vhost_net (n->nic->nc.peer)) { +return ; + } +if (!!n->vhost_started == !!(vdev->status &VIRTIO_CONFIG_S_DRIVER_OK )) { +return ; + } +if (vdev->status &VIRTIO_CONFIG_S_DRIVER_OK ) { +int r =vhost_net_start (tap_get_vhost_net (n->nic->nc.peer), vdev); +if (r <0 ) { +fprintf (stderr,"unable to start vhost net: %d: " +"falling back on userspace virtio\n" , -r); + }else { + n->vhost_started =1 ; + } + }else { +vhost_net_stop (tap_get_vhost_net (n->nic->nc.peer), vdev); + n->vhost_started =0 ; + } +} + +static void virtio_net_vmstate_change (void *opaque,int running,int reason) +{ +VirtIONet *n = opaque; +if (!running) { +return ; + } +/* This is called when vm is started, it will start vhost backend if it +* appropriate e.g. after migration. */ +virtio_net_set_status (&n->vdev); +} +VirtIODevice *virtio_net_init (DeviceState *dev,NICConf *conf) {VirtIONet *n;
@@ -837,6 +893,7 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf) n->vdev.set_features = virtio_net_set_features; n->vdev.bad_features = virtio_net_bad_features; n->vdev.reset = virtio_net_reset; + n->vdev.set_status = virtio_net_set_status; n->rx_vq =virtio_add_queue (&n->vdev,256 , virtio_net_handle_rx); n->tx_vq =virtio_add_queue (&n->vdev,256 , virtio_net_handle_tx); n->ctrl_vq =virtio_add_queue (&n->vdev,64 , virtio_net_handle_ctrl);
@@ -859,6 +916,7 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)register_savevm ("virtio-net" , virtio_net_id++,VIRTIO_NET_VM_VERSION , virtio_net_save, virtio_net_load, n); + n->vmstate =qemu_add_vm_change_state_handler (virtio_net_vmstate_change, n);return &n->vdev; }
@@ -866,6 +924,11 @@ VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf)void virtio_net_exit (VirtIODevice *vdev) {VirtIONet *n =DO_UPCAST (VirtIONet, vdev, vdev); +qemu_del_vm_change_state_handler (n->vmstate); + +if (n->vhost_started) { +vhost_net_stop (tap_get_vhost_net (n->nic->nc.peer), vdev); + }qemu_purge_queued_packets (&n->nic->nc);
Signature · 1 line
1.6.6.144.g5c3af