kvm tools: Rename 'self' variables
PATCH 1/2 kvm tools: Rename 'self' variables
Give proper names to vars named 'self'.
--- tools/kvm/8250-serial.c | 20 ++-- tools/kvm/cpuid.c | 6 +- tools/kvm/disk-image.c | 78 +++++++------- tools/kvm/include/kvm/disk-image.h | 36 +++--- tools/kvm/include/kvm/interrupt.h | 6 +- tools/kvm/include/kvm/ioport.h | 4 +- tools/kvm/include/kvm/kvm-cpu.h | 16 ++-- tools/kvm/include/kvm/kvm.h | 32 +++--- tools/kvm/include/kvm/virtio-blk.h | 2 +- tools/kvm/include/kvm/virtio-console.h | 4 +- tools/kvm/include/kvm/virtio-net.h | 2 +- tools/kvm/interrupt.c | 14 +- tools/kvm/ioport.c | 12 +- tools/kvm/kvm-cpu.c | 196 ++++++++++++++++---------------- tools/kvm/kvm-run.c | 2 +- tools/kvm/kvm.c | 146 ++++++++++++------------ tools/kvm/mmio.c | 2 +- tools/kvm/mptable.c | 2 +- tools/kvm/pci.c | 8 +- tools/kvm/qcow.c | 12 +- tools/kvm/rtc.c | 8 +- tools/kvm/virtio/blk.c | 16 ++-- tools/kvm/virtio/console.c | 26 ++-- tools/kvm/virtio/net.c | 36 +++--- 24 files changed, 343 insertions(+), 343 deletions(-)
tools/kvm/8250-serial.c +10 -10
@@ -70,7 +70,7 @@ static struct serial8250_device devices[] = {static int sysrq_pending; -static void serial8250__sysrq (struct kvm *self,struct serial8250_device *dev) +static void serial8250__sysrq (struct kvm *kvm,struct serial8250_device *dev) {switch (sysrq_pending) {case SYSRQ_PENDING_BREAK :
@@ -87,7 +87,7 @@ static void serial8250__sysrq(struct kvm *self, struct serial8250_device *dev) } } -static void serial8250__receive (struct kvm *self,struct serial8250_device *dev) +static void serial8250__receive (struct kvm *kvm,struct serial8250_device *dev) {int c;
@@ -95,7 +95,7 @@ static void serial8250__receive(struct kvm *self, struct serial8250_device *dev)return ;if (sysrq_pending) { -serial8250__sysrq (self, dev); +serial8250__sysrq (kvm, dev);return ; }
@@ -114,13 +114,13 @@ static void serial8250__receive(struct kvm *self, struct serial8250_device *dev)/* * Interrupts are injected for ttyS0 only. */ -void serial8250__inject_interrupt (struct kvm *self) +void serial8250__inject_interrupt (struct kvm *kvm) {struct serial8250_device *dev = &devices[0 ];mutex_lock (&dev->mutex); -serial8250__receive (self, dev); +serial8250__receive (kvm, dev);if (dev->ier &UART_IER_RDI && dev->lsr &UART_LSR_DR ) dev->iir =UART_IIR_RDI ;
@@ -130,14 +130,14 @@ void serial8250__inject_interrupt(struct kvm *self) dev->iir =UART_IIR_NO_INT ;if (dev->iir !=UART_IIR_NO_INT ) { -kvm__irq_line (self, dev->irq,0 ); -kvm__irq_line (self, dev->irq,1 ); +kvm__irq_line (kvm, dev->irq,0 ); +kvm__irq_line (kvm, dev->irq,1 ); }mutex_unlock (&dev->mutex); } -void serial8250__inject_sysrq (struct kvm *self) +void serial8250__inject_sysrq (struct kvm *kvm) { sysrq_pending =SYSRQ_PENDING_BREAK ; }
@@ -155,7 +155,7 @@ static struct serial8250_device *find_device(u16 port)return NULL ; } -static bool serial8250_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool serial8250_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {struct serial8250_device *dev;u16 offset;
@@ -243,7 +243,7 @@ out_unlock:return ret; } -static bool serial8250_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool serial8250_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {struct serial8250_device *dev;u16 offset;
tools/kvm/cpuid.c +3 -3
@@ -32,19 +32,19 @@ static void filter_cpuid(struct kvm_cpuid2 *kvm_cpuid) } } -void kvm_cpu__setup_cpuid (struct kvm_cpu *self) +void kvm_cpu__setup_cpuid (struct kvm_cpu *vcpu) {struct kvm_cpuid2 *kvm_cpuid; kvm_cpuid =calloc (1 ,sizeof (*kvm_cpuid) +MAX_KVM_CPUID_ENTRIES *sizeof (*kvm_cpuid->entries)); kvm_cpuid->nent =MAX_KVM_CPUID_ENTRIES ; -if (ioctl (self->kvm->sys_fd,KVM_GET_SUPPORTED_CPUID , kvm_cpuid) <0 ) +if (ioctl (vcpu->kvm->sys_fd,KVM_GET_SUPPORTED_CPUID , kvm_cpuid) <0 )die_perror ("KVM_GET_SUPPORTED_CPUID failed" );filter_cpuid (kvm_cpuid); -if (ioctl (self->vcpu_fd,KVM_SET_CPUID2 , kvm_cpuid) <0 ) +if (ioctl (vcpu->vcpu_fd,KVM_SET_CPUID2 , kvm_cpuid) <0 )die_perror ("KVM_SET_CPUID2 failed" );free (kvm_cpuid);
tools/kvm/disk-image.c +39 -39
150 lines not on this page · raw message
tools/kvm/include/kvm/disk-image.h +18 -18
@@ -11,11 +11,11 @@struct disk_image ;struct disk_image_operations { -int (*read_sector)(struct disk_image *self,u64 sector,void *dst,u32 dst_len); -int (*write_sector)(struct disk_image *self,u64 sector,void *src,u32 src_len); -ssize_t (*read_sector_iov)(struct disk_image *self,u64 sector,const struct iovec *iov,int iovcount); -ssize_t (*write_sector_iov)(struct disk_image *self,u64 sector,const struct iovec *iov,int iovcount); -void (*close)(struct disk_image *self); +int (*read_sector)(struct disk_image *disk,u64 sector,void *dst,u32 dst_len); +int (*write_sector)(struct disk_image *disk,u64 sector,void *src,u32 src_len); +ssize_t (*read_sector_iov)(struct disk_image *disk,u64 sector,const struct iovec *iov,int iovcount); +ssize_t (*write_sector_iov)(struct disk_image *disk,u64 sector,const struct iovec *iov,int iovcount); +void (*close)(struct disk_image *disk); };struct disk_image {
@@ -28,25 +28,25 @@ struct disk_image {struct disk_image *disk_image__open (const char *filename,bool readonly);struct disk_image *disk_image__new (int fd,u64 size,struct disk_image_operations *ops);struct disk_image *disk_image__new_readonly (int fd,u64 size,struct disk_image_operations *ops); -void disk_image__close (struct disk_image *self); +void disk_image__close (struct disk_image *disk); -static inline int disk_image__read_sector (struct disk_image *self,u64 sector,void *dst,u32 dst_len) +static inline int disk_image__read_sector (struct disk_image *disk,u64 sector,void *dst,u32 dst_len) { -return self->ops->read_sector (self, sector, dst, dst_len); +return disk->ops->read_sector (disk, sector, dst, dst_len); } -static inline int disk_image__write_sector (struct disk_image *self,u64 sector,void *src,u32 src_len) +static inline int disk_image__write_sector (struct disk_image *disk,u64 sector,void *src,u32 src_len) { -return self->ops->write_sector (self, sector, src, src_len); +return disk->ops->write_sector (disk, sector, src, src_len); } -static inline ssize_t disk_image__read_sector_iov (struct disk_image *self,u64 sector,const struct iovec *iov,int iovcount) +static inline ssize_t disk_image__read_sector_iov (struct disk_image *disk,u64 sector,const struct iovec *iov,int iovcount) { -if (self->ops->read_sector_iov) -return self->ops->read_sector_iov (self, sector, iov, iovcount); +if (disk->ops->read_sector_iov) +return disk->ops->read_sector_iov (disk, sector, iov, iovcount);while (iovcount--) { - self->ops->read_sector (self, sector, iov->iov_base, iov->iov_len); + disk->ops->read_sector (disk, sector, iov->iov_base, iov->iov_len); sector += iov->iov_len >>SECTOR_SHIFT ; iov++; }
@@ -54,13 +54,13 @@ static inline ssize_t disk_image__read_sector_iov(struct disk_image *self, u64 sreturn sector <<SECTOR_SHIFT ; } -static inline ssize_t disk_image__write_sector_iov (struct disk_image *self,u64 sector,const struct iovec *iov,int iovcount) +static inline ssize_t disk_image__write_sector_iov (struct disk_image *disk,u64 sector,const struct iovec *iov,int iovcount) { -if (self->ops->write_sector_iov) -return self->ops->write_sector_iov (self, sector, iov, iovcount); +if (disk->ops->write_sector_iov) +return disk->ops->write_sector_iov (disk, sector, iov, iovcount);while (iovcount--) { - self->ops->write_sector (self, sector, iov->iov_base, iov->iov_len); + disk->ops->write_sector (disk, sector, iov->iov_base, iov->iov_len); sector += iov->iov_len >>SECTOR_SHIFT ; iov++; }
tools/kvm/include/kvm/interrupt.h +3 -3
@@ -19,8 +19,8 @@ struct interrupt_table { struct real_intr_desc entries[REAL_INTR_VECTORS]; }; -void interrupt_table__copy (struct interrupt_table *self,void *dst,unsigned int size); -void interrupt_table__setup (struct interrupt_table *self,struct real_intr_desc *entry); -void interrupt_table__set (struct interrupt_table *self,struct real_intr_desc *entry,unsigned int num); +void interrupt_table__copy (struct interrupt_table *itable,void *dst,unsigned int size); +void interrupt_table__setup (struct interrupt_table *itable,struct real_intr_desc *entry); +void interrupt_table__set (struct interrupt_table *itable,struct real_intr_desc *entry,unsigned int num);#endif /* KVM__INTERRUPT_H */
tools/kvm/include/kvm/ioport.h +2 -2
@@ -19,8 +19,8 @@struct kvm ;struct ioport_operations { -bool (*io_in)(struct kvm *self,u16 port,void *data,int size,u32 count); -bool (*io_out)(struct kvm *self,u16 port,void *data,int size,u32 count); +bool (*io_in)(struct kvm *kvm,u16 port,void *data,int size,u32 count); +bool (*io_out)(struct kvm *kvm,u16 port,void *data,int size,u32 count); };void ioport__setup_legacy (void );
tools/kvm/include/kvm/kvm-cpu.h +8 -8
@@ -24,15 +24,15 @@ struct kvm_cpu { };struct kvm_cpu *kvm_cpu__init (struct kvm *kvm,unsigned long cpu_id); -void kvm_cpu__delete (struct kvm_cpu *self); -void kvm_cpu__reset_vcpu (struct kvm_cpu *self); -void kvm_cpu__setup_cpuid (struct kvm_cpu *self); -void kvm_cpu__enable_singlestep (struct kvm_cpu *self); -void kvm_cpu__run (struct kvm_cpu *self); +void kvm_cpu__delete (struct kvm_cpu *vcpu); +void kvm_cpu__reset_vcpu (struct kvm_cpu *vcpu); +void kvm_cpu__setup_cpuid (struct kvm_cpu *vcpu); +void kvm_cpu__enable_singlestep (struct kvm_cpu *vcpu); +void kvm_cpu__run (struct kvm_cpu *vcpu);int kvm_cpu__start (struct kvm_cpu *cpu); -void kvm_cpu__show_code (struct kvm_cpu *self); -void kvm_cpu__show_registers (struct kvm_cpu *self); -void kvm_cpu__show_page_tables (struct kvm_cpu *self); +void kvm_cpu__show_code (struct kvm_cpu *vcpu); +void kvm_cpu__show_registers (struct kvm_cpu *vcpu); +void kvm_cpu__show_page_tables (struct kvm_cpu *vcpu);#endif /* KVM__KVM_CPU_H */
tools/kvm/include/kvm/kvm.h +16 -16
@@ -31,28 +31,28 @@ struct kvm { };struct kvm *kvm__init (const char *kvm_dev,unsigned long ram_size); -int kvm__max_cpus (struct kvm *self); -void kvm__init_ram (struct kvm *self); -void kvm__delete (struct kvm *self); +int kvm__max_cpus (struct kvm *kvm); +void kvm__init_ram (struct kvm *kvm); +void kvm__delete (struct kvm *kvm);bool kvm__load_kernel (struct kvm *kvm,const char *kernel_filename,const char *initrd_filename,const char *kernel_cmdline); -void kvm__setup_bios (struct kvm *self); -void kvm__start_timer (struct kvm *self); -void kvm__stop_timer (struct kvm *self); -void kvm__irq_line (struct kvm *self,int irq,int level); -bool kvm__emulate_io (struct kvm *self,u16 port,void *data,int direction,int size,u32 count); -bool kvm__emulate_mmio (struct kvm *self,u64 phys_addr,u8 *data,u32 len,u8 is_write); +void kvm__setup_bios (struct kvm *kvm); +void kvm__start_timer (struct kvm *kvm); +void kvm__stop_timer (struct kvm *kvm); +void kvm__irq_line (struct kvm *kvm,int irq,int level); +bool kvm__emulate_io (struct kvm *kvm,u16 port,void *data,int direction,int size,u32 count); +bool kvm__emulate_mmio (struct kvm *kvm,u64 phys_addr,u8 *data,u32 len,u8 is_write);/* * Debugging */ -void kvm__dump_mem (struct kvm *self,unsigned long addr,unsigned long size); +void kvm__dump_mem (struct kvm *kvm,unsigned long addr,unsigned long size);extern const char *kvm_exit_reasons[]; -static inline bool host_ptr_in_ram (struct kvm *self,void *p) +static inline bool host_ptr_in_ram (struct kvm *kvm,void *p) { -return self->ram_start <= p && p < (self->ram_start + self->ram_size); +return kvm->ram_start <= p && p < (kvm->ram_start + kvm->ram_size); }static inline u32 segment_to_flat (u16 selector,u16 offset)
@@ -60,16 +60,16 @@ static inline u32 segment_to_flat(u16 selector, u16 offset) return ((u32)selector << 4) + (u32) offset; } -static inline void *guest_flat_to_host (struct kvm *self,unsigned long offset) +static inline void *guest_flat_to_host (struct kvm *kvm,unsigned long offset) { -return self->ram_start + offset; +return kvm->ram_start + offset; } -static inline void *guest_real_to_host (struct kvm *self,u16 selector,u16 offset) +static inline void *guest_real_to_host (struct kvm *kvm,u16 selector,u16 offset) {unsigned long flat =segment_to_flat (selector, offset); -return guest_flat_to_host (self, flat); +return guest_flat_to_host (kvm, flat); }#endif /* KVM__KVM_H */
tools/kvm/include/kvm/virtio-blk.h +1 -1
@@ -5,6 +5,6 @@struct kvm ; -void virtio_blk__init (struct kvm *self,struct disk_image *disk); +void virtio_blk__init (struct kvm *kvm,struct disk_image *disk);#endif /* KVM__BLK_VIRTIO_H */
tools/kvm/include/kvm/virtio-console.h +2 -2
@@ -3,7 +3,7 @@struct kvm ; -void virtio_console__init (struct kvm *self); -void virtio_console__inject_interrupt (struct kvm *self); +void virtio_console__init (struct kvm *kvm); +void virtio_console__inject_interrupt (struct kvm *kvm);#endif /* KVM__CONSOLE_VIRTIO_H */
tools/kvm/include/kvm/virtio-net.h +1 -1
@@ -4,7 +4,7 @@struct kvm ;struct virtio_net_parameters { -struct kvm *self; +struct kvm *kvm;const char *host_ip;char guest_mac[6 ];const char *script;
tools/kvm/interrupt.c +7 -7
@@ -4,24 +4,24 @@#include <string.h> -void interrupt_table__copy (struct interrupt_table *self,void *dst,unsigned int size) +void interrupt_table__copy (struct interrupt_table *itable,void *dst,unsigned int size) { -if (size <sizeof (self->entries)) +if (size <sizeof (itable->entries))die ("An attempt to overwrite host memory" ); -memcpy (dst, self->entries,sizeof (self->entries)); +memcpy (dst, itable->entries,sizeof (itable->entries)); } -void interrupt_table__setup (struct interrupt_table *self,struct real_intr_desc *entry) +void interrupt_table__setup (struct interrupt_table *itable,struct real_intr_desc *entry) {unsigned int i;for (i =0 ; i <REAL_INTR_VECTORS ; i++) - self->entries[i] = *entry; + itable->entries[i] = *entry; } -void interrupt_table__set (struct interrupt_table *self,struct real_intr_desc *entry,unsigned int num) +void interrupt_table__set (struct interrupt_table *itable,struct real_intr_desc *entry,unsigned int num) {if (num <REAL_INTR_VECTORS ) - self->entries[num] = *entry; + itable->entries[num] = *entry; }
tools/kvm/ioport.c +6 -6
@@ -13,7 +13,7 @@bool ioport_debug; -static bool debug_io_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool debug_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {exit (EXIT_SUCCESS ); }
@@ -22,12 +22,12 @@ static struct ioport_operations debug_ops = { .io_out = debug_io_out, }; -static bool dummy_io_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool dummy_io_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {return true; } -static bool dummy_io_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool dummy_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {return true; }
@@ -64,7 +64,7 @@ static void ioport_error(u16 port, void *data, int direction, int size, u32 counfprintf (stderr,"IO error: %s port=%x, size=%d, count=%u\n" ,to_direction (direction), port, size, count); } -bool kvm__emulate_io (struct kvm *self,u16 port,void *data,int direction,int size,u32 count) +bool kvm__emulate_io (struct kvm *kvm,u16 port,void *data,int direction,int size,u32 count) {struct ioport_operations *ops = ioport_ops[port];bool ret;
@@ -76,14 +76,14 @@ bool kvm__emulate_io(struct kvm *self, u16 port, void *data, int direction, intif (!ops->io_in) goto error; - ret = ops->io_in (self, port, data, size, count); + ret = ops->io_in (kvm, port, data, size, count);if (!ret) goto error; }else {if (!ops->io_out) goto error; - ret = ops->io_out (self, port, data, size, count); + ret = ops->io_out (kvm, port, data, size, count);if (!ret) goto error; }
tools/kvm/kvm-cpu.c +98 -98
385 lines not on this page · raw message
tools/kvm/kvm-run.c +1 -1
@@ -496,7 +496,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)if (!strncmp (network,"virtio" ,6 )) { net_params = (struct virtio_net_parameters ) { .host_ip = host_ip_addr, - .self = kvm, + .kvm = kvm, .script = script }; sscanf(guest_mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
tools/kvm/kvm.c +73 -73
376 lines not on this page · raw message
tools/kvm/mmio.c +1 -1
@@ -11,7 +11,7 @@ static const char *to_direction(u8 is_write)return "read" ; } -bool kvm__emulate_mmio (struct kvm *self,u64 phys_addr,u8 *data,u32 len,u8 is_write) +bool kvm__emulate_mmio (struct kvm *kvm,u64 phys_addr,u8 *data,u32 len,u8 is_write) {fprintf (stderr,"Warning: Ignoring MMIO %s at %016llx (length %u)\n" ,to_direction (is_write), phys_addr, len);
tools/kvm/mptable.c +1 -1
@@ -260,7 +260,7 @@ void mptable_setup(struct kvm *kvm, unsigned int ncpus)/* * We will copy the whole table, no need to separate -* floating structure and table itself. +* floating structure and table itkvm. */ size = (unsigned long )mpf_intel +sizeof (*mpf_intel) - (unsigned long )mpc_table;
tools/kvm/pci.c +4 -4
@@ -21,7 +21,7 @@ static void *pci_config_address_ptr(u16 port)return base + offset; } -static bool pci_config_address_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool pci_config_address_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {void *p =pci_config_address_ptr (port);
@@ -30,7 +30,7 @@ static bool pci_config_address_out(struct kvm *self, u16 port, void *data, int sreturn true; } -static bool pci_config_address_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool pci_config_address_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {void *p =pci_config_address_ptr (port);
@@ -44,7 +44,7 @@ static struct ioport_operations pci_config_address_ops = { .io_out = pci_config_address_out, }; -static bool pci_config_data_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool pci_config_data_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {return true; }
@@ -67,7 +67,7 @@ static bool pci_device_exists(u8 bus_number, u8 device_number, u8 function_numbereturn dev !=NULL ; } -static bool pci_config_data_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool pci_config_data_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {unsigned long start;u8 dev_num;
tools/kvm/qcow.c +6 -6
@@ -101,10 +101,10 @@ out_error: goto out; } -static int qcow1_read_sector (struct disk_image *self,u64 sector, +static int qcow1_read_sector (struct disk_image *disk,u64 sector,void *dst,u32 dst_len) { -struct qcow *q = self->priv; +struct qcow *q = disk->priv;struct qcow_header *header = q->header;char *buf = dst;u64 offset;
@@ -130,19 +130,19 @@ out_error:return -1 ; } -static int qcow1_write_sector (struct disk_image *self,u64 sector,void *src,u32 src_len) +static int qcow1_write_sector (struct disk_image *disk,u64 sector,void *src,u32 src_len) {return -1 ; } -static void qcow1_disk_close (struct disk_image *self) +static void qcow1_disk_close (struct disk_image *disk) {struct qcow *q; -if (!self) +if (!disk)return ; - q = self->priv; + q = disk->priv;free (q->table.l1_table);free (q->header);
tools/kvm/rtc.c +4 -4
@@ -19,7 +19,7 @@ static inline unsigned char bin2bcd(unsigned val)return ((val /10 ) <<4 ) + val %10 ; } -static bool cmos_ram_data_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool cmos_ram_data_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {struct tm *tm;time_t ti;
@@ -52,7 +52,7 @@ static bool cmos_ram_data_in(struct kvm *self, u16 port, void *data, int size, ureturn true; } -static bool cmos_ram_data_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool cmos_ram_data_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {return true; }
@@ -62,13 +62,13 @@ static struct ioport_operations cmos_ram_data_ioport_ops = { .io_in = cmos_ram_data_in, }; -static bool cmos_ram_index_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool cmos_ram_index_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {u8 value; value =ioport__read8 (data); - self->nmi_disabled = value & (1UL <<7 ); + kvm->nmi_disabled = value & (1UL <<7 ); cmos_index = value & ~(1UL <<7 );
tools/kvm/virtio/blk.c +8 -8
@@ -69,7 +69,7 @@ static void virtio_blk_port2dev(u16 port, u16 base, u16 size, u16 *dev_idx, u16 *offset = port - (base + *dev_idx * size); } -static bool virtio_blk_pci_io_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool virtio_blk_pci_io_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {struct blk_dev *bdev;u16 offset, dev_idx;
@@ -103,7 +103,7 @@ static bool virtio_blk_pci_io_in(struct kvm *self, u16 port, void *data, int sizbreak ;case VIRTIO_PCI_ISR :ioport__write8 (data, bdev->isr); -kvm__irq_line (self, bdev->pci_hdr.irq_line,VIRTIO_IRQ_LOW ); +kvm__irq_line (kvm, bdev->pci_hdr.irq_line,VIRTIO_IRQ_LOW ); bdev->isr =VIRTIO_IRQ_LOW ;break ;case VIRTIO_MSI_CONFIG_VECTOR :
@@ -119,7 +119,7 @@ static bool virtio_blk_pci_io_in(struct kvm *self, u16 port, void *data, int sizreturn ret; } -static bool virtio_blk_do_io_request (struct kvm *self, +static bool virtio_blk_do_io_request (struct kvm *kvm,struct blk_dev *bdev,struct virt_queue *queue) {
@@ -129,7 +129,7 @@ static bool virtio_blk_do_io_request(struct kvm *self,u16 out, in, head;u8 *status; - head =virt_queue__get_iov (queue, iov, &out, &in, self); + head =virt_queue__get_iov (queue, iov, &out, &in, kvm);/* head */ req = iov[0 ].iov_base;
@@ -171,7 +171,7 @@ static void virtio_blk_do_io(struct kvm *kvm, void *param)virt_queue__trigger_irq (vq, bdev->pci_hdr.irq_line, &bdev->isr, kvm); } -static bool virtio_blk_pci_io_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool virtio_blk_pci_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {struct blk_dev *bdev;u16 offset, dev_idx;
@@ -196,7 +196,7 @@ static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int si queue = &bdev->vqs[bdev->queue_selector]; queue->pfn =ioport__read32 (data); - p =guest_pfn_to_host (self, queue->pfn); + p =guest_pfn_to_host (kvm, queue->pfn);vring_init (&queue->vring,VIRTIO_BLK_QUEUE_SIZE , p,VIRTIO_PCI_VRING_ALIGN );
@@ -205,7 +205,7 @@ static bool virtio_blk_pci_io_out(struct kvm *self, u16 port, void *data, int si .bdev = bdev, }; - job->job_id =thread_pool__add_job (self, virtio_blk_do_io, job); + job->job_id =thread_pool__add_job (kvm, virtio_blk_do_io, job);break ; }
@@ -255,7 +255,7 @@ static int virtio_blk_find_empty_dev(void)return -1 ; } -void virtio_blk__init (struct kvm *self,struct disk_image *disk) +void virtio_blk__init (struct kvm *kvm,struct disk_image *disk) {u16 blk_dev_base_addr;u8 dev, pin, line;
tools/kvm/virtio/console.c +13 -13
@@ -69,7 +69,7 @@ static struct con_dev cdev = {/* * Interrupts are injected for hvc0 only. */ -static void virtio_console__inject_interrupt_callback (struct kvm *self,void *param) +static void virtio_console__inject_interrupt_callback (struct kvm *kvm,void *param) {struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE ];struct virt_queue *vq;
@@ -82,16 +82,16 @@ static void virtio_console__inject_interrupt_callback(struct kvm *self, void *pa vq = param;if (term_readable (CONSOLE_VIRTIO ) &&virt_queue__available (vq)) { - head =virt_queue__get_iov (vq, iov, &out, &in, self); + head =virt_queue__get_iov (vq, iov, &out, &in, kvm); len =term_getc_iov (CONSOLE_VIRTIO , iov, in);virt_queue__set_used_elem (vq, head, len); -virt_queue__trigger_irq (vq, virtio_console_pci_device.irq_line, &cdev.isr, self); +virt_queue__trigger_irq (vq, virtio_console_pci_device.irq_line, &cdev.isr, kvm); }mutex_unlock (&cdev.mutex); } -void virtio_console__inject_interrupt (struct kvm *self) +void virtio_console__inject_interrupt (struct kvm *kvm) {thread_pool__do_job (cdev.jobs[VIRTIO_CONSOLE_RX_QUEUE ]); }
@@ -111,7 +111,7 @@ static bool virtio_console_pci_io_device_specific_in(void *data, unsigned long oreturn true; } -static bool virtio_console_pci_io_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool virtio_console_pci_io_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {unsigned long offset = port -IOPORT_VIRTIO_CONSOLE ;bool ret = true;
@@ -140,7 +140,7 @@ static bool virtio_console_pci_io_in(struct kvm *self, u16 port, void *data, intbreak ;case VIRTIO_PCI_ISR :ioport__write8 (data, cdev.isr); -kvm__irq_line (self, virtio_console_pci_device.irq_line,VIRTIO_IRQ_LOW ); +kvm__irq_line (kvm, virtio_console_pci_device.irq_line,VIRTIO_IRQ_LOW ); cdev.isr =VIRTIO_IRQ_LOW ;break ;case VIRTIO_MSI_CONFIG_VECTOR :
@@ -155,7 +155,7 @@ static bool virtio_console_pci_io_in(struct kvm *self, u16 port, void *data, intreturn ret; } -static void virtio_console_handle_callback (struct kvm *self,void *param) +static void virtio_console_handle_callback (struct kvm *kvm,void *param) {struct iovec iov[VIRTIO_CONSOLE_QUEUE_SIZE ];struct virt_queue *vq;
@@ -172,14 +172,14 @@ static void virtio_console_handle_callback(struct kvm *self, void *param)*/ while (virt_queue__available (vq)) { - head =virt_queue__get_iov (vq, iov, &out, &in, self); + head =virt_queue__get_iov (vq, iov, &out, &in, kvm); len =term_putc_iov (CONSOLE_VIRTIO , iov, out);virt_queue__set_used_elem (vq, head, len); } } -static bool virtio_console_pci_io_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool virtio_console_pci_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {unsigned long offset = port -IOPORT_VIRTIO_CONSOLE ;bool ret = true;
@@ -198,14 +198,14 @@ static bool virtio_console_pci_io_out(struct kvm *self, u16 port, void *data, in queue = &cdev.vqs[cdev.queue_selector]; queue->pfn =ioport__read32 (data); - p =guest_pfn_to_host (self, queue->pfn); + p =guest_pfn_to_host (kvm, queue->pfn);vring_init (&queue->vring,VIRTIO_CONSOLE_QUEUE_SIZE , p,VIRTIO_PCI_VRING_ALIGN );if (cdev.queue_selector ==VIRTIO_CONSOLE_TX_QUEUE ) - cdev.jobs[cdev.queue_selector] =thread_pool__add_job (self, virtio_console_handle_callback, queue); + cdev.jobs[cdev.queue_selector] =thread_pool__add_job (kvm, virtio_console_handle_callback, queue);else if (cdev.queue_selector ==VIRTIO_CONSOLE_RX_QUEUE ) - cdev.jobs[cdev.queue_selector] =thread_pool__add_job (self, virtio_console__inject_interrupt_callback, queue); + cdev.jobs[cdev.queue_selector] =thread_pool__add_job (kvm, virtio_console__inject_interrupt_callback, queue);break ; }
@@ -240,7 +240,7 @@ static struct ioport_operations virtio_console_io_ops = { .io_out = virtio_console_pci_io_out, }; -void virtio_console__init (struct kvm *self) +void virtio_console__init (struct kvm *kvm) {u8 dev, line, pin;
tools/kvm/virtio/net.c +18 -18
@@ -82,12 +82,12 @@ static void *virtio_net_rx_thread(void *p) {struct iovec iov[VIRTIO_NET_QUEUE_SIZE ];struct virt_queue *vq; -struct kvm *self; +struct kvm *kvm;u16 out, in;u16 head;int len; - self = p; + kvm = p; vq = &net_device.vqs[VIRTIO_NET_RX_QUEUE ];while (1 ) {
@@ -97,12 +97,12 @@ static void *virtio_net_rx_thread(void *p)mutex_unlock (&net_device.io_rx_mutex);while (virt_queue__available (vq)) { - head =virt_queue__get_iov (vq, iov, &out, &in, self); + head =virt_queue__get_iov (vq, iov, &out, &in, kvm); len =readv (net_device.tap_fd, iov, in);virt_queue__set_used_elem (vq, head, len);/* We should interrupt guest right now, otherwise latency is huge. */ -virt_queue__trigger_irq (vq, virtio_net_pci_device.irq_line, &net_device.isr, self); +virt_queue__trigger_irq (vq, virtio_net_pci_device.irq_line, &net_device.isr, kvm); } }
@@ -116,12 +116,12 @@ static void *virtio_net_tx_thread(void *p) {struct iovec iov[VIRTIO_NET_QUEUE_SIZE ];struct virt_queue *vq; -struct kvm *self; +struct kvm *kvm;u16 out, in;u16 head;int len; - self = p; + kvm = p; vq = &net_device.vqs[VIRTIO_NET_TX_QUEUE ];while (1 ) {
@@ -131,12 +131,12 @@ static void *virtio_net_tx_thread(void *p)mutex_unlock (&net_device.io_tx_mutex);while (virt_queue__available (vq)) { - head =virt_queue__get_iov (vq, iov, &out, &in, self); + head =virt_queue__get_iov (vq, iov, &out, &in, kvm); len =writev (net_device.tap_fd, iov, out);virt_queue__set_used_elem (vq, head, len); } -virt_queue__trigger_irq (vq, virtio_net_pci_device.irq_line, &net_device.isr, self); +virt_queue__trigger_irq (vq, virtio_net_pci_device.irq_line, &net_device.isr, kvm); }
@@ -161,7 +161,7 @@ static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offsereturn true; } -static bool virtio_net_pci_io_in (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool virtio_net_pci_io_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) {unsigned long offset = port -IOPORT_VIRTIO_NET ;bool ret = true;
@@ -190,7 +190,7 @@ static bool virtio_net_pci_io_in(struct kvm *self, u16 port, void *data, int sizbreak ;case VIRTIO_PCI_ISR :ioport__write8 (data, net_device.isr); -kvm__irq_line (self, virtio_net_pci_device.irq_line,VIRTIO_IRQ_LOW ); +kvm__irq_line (kvm, virtio_net_pci_device.irq_line,VIRTIO_IRQ_LOW ); net_device.isr =VIRTIO_IRQ_LOW ;break ;case VIRTIO_MSI_CONFIG_VECTOR :
@@ -205,7 +205,7 @@ static bool virtio_net_pci_io_in(struct kvm *self, u16 port, void *data, int sizreturn ret; } -static void virtio_net_handle_callback (struct kvm *self,u16 queue_index) +static void virtio_net_handle_callback (struct kvm *kvm,u16 queue_index) {switch (queue_index) {case VIRTIO_NET_TX_QUEUE : {
@@ -225,7 +225,7 @@ static void virtio_net_handle_callback(struct kvm *self, u16 queue_index) } } -static bool virtio_net_pci_io_out (struct kvm *self,u16 port,void *data,int size,u32 count) +static bool virtio_net_pci_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) {unsigned long offset = port -IOPORT_VIRTIO_NET ;bool ret = true;
@@ -244,7 +244,7 @@ static bool virtio_net_pci_io_out(struct kvm *self, u16 port, void *data, int si queue = &net_device.vqs[net_device.queue_selector]; queue->pfn =ioport__read32 (data); - p =guest_pfn_to_host (self, queue->pfn); + p =guest_pfn_to_host (kvm, queue->pfn);vring_init (&queue->vring,VIRTIO_NET_QUEUE_SIZE , p,VIRTIO_PCI_VRING_ALIGN );
@@ -256,7 +256,7 @@ static bool virtio_net_pci_io_out(struct kvm *self, u16 port, void *data, int sicase VIRTIO_PCI_QUEUE_NOTIFY : {u16 queue_index; queue_index =ioport__read16 (data); -virtio_net_handle_callback (self, queue_index); +virtio_net_handle_callback (kvm, queue_index);break ; }case VIRTIO_PCI_STATUS :
@@ -367,7 +367,7 @@ fail:return 0 ; } -static void virtio_net__io_thread_init (struct kvm *self) +static void virtio_net__io_thread_init (struct kvm *kvm) {pthread_mutex_init (&net_device.io_rx_mutex,NULL );pthread_cond_init (&net_device.io_tx_cond,NULL );
@@ -375,8 +375,8 @@ static void virtio_net__io_thread_init(struct kvm *self)pthread_mutex_init (&net_device.io_rx_mutex,NULL );pthread_cond_init (&net_device.io_tx_cond,NULL ); -pthread_create (&net_device.io_rx_thread,NULL , virtio_net_rx_thread, (void *)self); -pthread_create (&net_device.io_tx_thread,NULL , virtio_net_tx_thread, (void *)self); +pthread_create (&net_device.io_rx_thread,NULL , virtio_net_rx_thread, (void *)kvm); +pthread_create (&net_device.io_tx_thread,NULL , virtio_net_tx_thread, (void *)kvm); }void virtio_net__init (const struct virtio_net_parameters *params)
@@ -392,6 +392,6 @@ void virtio_net__init(const struct virtio_net_parameters *params)pci__register (&virtio_net_pci_device, dev);ioport__register (IOPORT_VIRTIO_NET , &virtio_net_io_ops,IOPORT_VIRTIO_NET_SIZE ); -virtio_net__io_thread_init (params->self); +virtio_net__io_thread_init (params->kvm); } }
Signature · 1 line
1.7.5.rc3
PATCH 2/2 kvm tools: Use constants for commonly used mmap flags
--- tools/kvm/disk-image.c | 2 +- tools/kvm/include/kvm/util.h | 3 +++ tools/kvm/kvm-cpu.c | 2 +- tools/kvm/kvm.c | 4 ++-- 4 files changed, 7 insertions(+), 4 deletions(-)
tools/kvm/disk-image.c +1 -1
@@ -39,7 +39,7 @@ struct disk_image *disk_image__new_readonly(int fd, u64 size, struct disk_image_if (!disk)return NULL ; - disk->priv =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_NORESERVE , fd,0 ); + disk->priv =mmap (NULL , size,PROT_RW ,MAP_PRIVATE |MAP_NORESERVE , fd,0 );if (disk->priv ==MAP_FAILED )die ("mmap() failed" );return disk;
tools/kvm/include/kvm/util.h +3 -0
@@ -29,6 +29,9 @@#endif #endif +#define PROT_RW (PROT_READ|PROT_WRITE) +#define MAP_ANON_NORESERVE (MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE) + extern void die(const char *err, ...) NORETURN __attribute__((format (printf, 1, 2))); extern void die_perror(const char *s) NORETURN;extern int error (const char *err, ...) __attribute__((format (printf,1 ,2 )));
tools/kvm/kvm-cpu.c +1 -1
@@ -81,7 +81,7 @@ struct kvm_cpu *kvm_cpu__init(struct kvm *kvm, unsigned long cpu_id)if (mmap_size <0 )die_perror ("KVM_GET_VCPU_MMAP_SIZE ioctl" ); - vcpu->kvm_run =mmap (NULL , mmap_size,PROT_READ |PROT_WRITE ,MAP_SHARED , vcpu->vcpu_fd,0 ); + vcpu->kvm_run =mmap (NULL , mmap_size,PROT_RW ,MAP_SHARED , vcpu->vcpu_fd,0 );if (vcpu->kvm_run ==MAP_FAILED )die ("unable to mmap vcpu fd" );
tools/kvm/kvm.c +2 -2
@@ -267,9 +267,9 @@ struct kvm *kvm__init(const char *kvm_dev, unsigned long ram_size) kvm->ram_size = ram_size;if (kvm->ram_size <KVM_32BIT_GAP_START ) { - kvm->ram_start =mmap (NULL , ram_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS |MAP_NORESERVE ,-1 ,0 ); + kvm->ram_start =mmap (NULL , ram_size,PROT_RW ,MAP_ANON_NORESERVE ,-1 ,0 ); }else { - kvm->ram_start =mmap (NULL , ram_size +KVM_32BIT_GAP_SIZE ,PROT_READ |PROT_WRITE ,MAP_PRIVATE |MAP_ANONYMOUS |MAP_NORESERVE ,-1 ,0 ); + kvm->ram_start =mmap (NULL , ram_size +KVM_32BIT_GAP_SIZE ,PROT_RW ,MAP_ANON_NORESERVE ,-1 ,0 );if (kvm->ram_start !=MAP_FAILED ) {/* * We mprotect the gap (see kvm__init_ram() for details) PROT_NONE so that
Signature · 1 line
1.7.5.rc3
PATCH 1/1 kvm tools: Use standardized style for the virtio/net.c driver
In reply to Sasha LevinQuoted 21 lines · Sasha Levin
Give proper names to vars named 'self'.
Signed-off-by Sasha Levin <[email protected]>--- tools/kvm/8250-serial.c | 20 ++-- tools/kvm/cpuid.c | 6 +- tools/kvm/disk-image.c | 78 +++++++------- tools/kvm/include/kvm/disk-image.h | 36 +++--- tools/kvm/include/kvm/interrupt.h | 6 +- tools/kvm/include/kvm/ioport.h | 4 +- tools/kvm/include/kvm/kvm-cpu.h | 16 ++-- tools/kvm/include/kvm/kvm.h | 32 +++--- tools/kvm/include/kvm/virtio-blk.h | 2 +- tools/kvm/include/kvm/virtio-console.h | 4 +- tools/kvm/include/kvm/virtio-net.h | 2 +- tools/kvm/interrupt.c | 14 +- tools/kvm/ioport.c | 12 +- tools/kvm/kvm-cpu.c | 196 ++++++++++++++++---------------- tools/kvm/kvm-run.c | 2 +- tools/kvm/kvm.c | 146 ++++++++++++------------ tools/kvm/mmio.c | 2 +-
Quoted 8 lines
tools/kvm/mptable.c | 2 +- tools/kvm/pci.c | 8 +- tools/kvm/qcow.c | 12 +- tools/kvm/rtc.c | 8 +- tools/kvm/virtio/blk.c | 16 ++-- tools/kvm/virtio/console.c | 26 ++-- tools/kvm/virtio/net.c | 36 +++--- 24 files changed, 343 insertions(+), 343 deletions(-)
nice, that was really quick! :-)
I had a quick look at virtio/net.c and it still had quite many style inefficiencies - all of which are patterns which i pointed out before:
- use short names for devices within the driver, so not 'net_device' but 'ndev' - everyone hacking net.c knows that this is the network driver so 'ndev' is a self-explanatory (and very short) term of art ...
- use 'pci_header' instead of the ambiguous and misleading 'virtio_net_pci_device' naming.
- do not repeat 'net' in struct net_device fields! So rename ndev->net_config to ndev->config.
- In the kernel we generally use _lock names for mutexes. This is conceptually more generic. So rename the net device mutexes accordingly.
- group #include lines in a topical way instead of a random mess
- fix vertical alignment mismatches
I have build-tested this patch but have not tested networking. (i'd expect it to still workt hough)
Other virtio drivers have similar problems as well - does anyone want to fix those?
Thanks,
Ingo
tools/kvm/virtio/net.c +88 -84
@@ -11,14 +11,17 @@#include <linux/virtio_net.h> #include <linux/if_tun.h> + +#include <arpa/inet.h> #include <net/if.h> -#include <sys/ioctl.h> + +#include <unistd.h> #include <assert.h> #include <fcntl.h> -#include <arpa/inet.h> -#include <sys/types.h> +#include <sys/socket.h> -#include <unistd.h> +#include <sys/ioctl.h> +#include <sys/types.h> #include <sys/wait.h> #define VIRTIO_NET_QUEUE_SIZE 128
@@ -26,22 +29,22 @@#define VIRTIO_NET_RX_QUEUE 0#define VIRTIO_NET_TX_QUEUE 1 -static struct pci_device_header virtio_net_pci_device = { - .vendor_id =PCI_VENDOR_ID_REDHAT_QUMRANET , - .device_id =PCI_DEVICE_ID_VIRTIO_NET , - .header_type =PCI_HEADER_TYPE_NORMAL , - .revision_id =0 , - .class =0x020000 , - .subsys_vendor_id =PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET , - .subsys_id =PCI_SUBSYSTEM_ID_VIRTIO_NET , - .bar[0 ] =IOPORT_VIRTIO_NET |PCI_BASE_ADDRESS_SPACE_IO , +static struct pci_device_header pci_header = { + .vendor_id =PCI_VENDOR_ID_REDHAT_QUMRANET , + .device_id =PCI_DEVICE_ID_VIRTIO_NET , + .header_type =PCI_HEADER_TYPE_NORMAL , + .revision_id =0 , + .class =0x020000 , + .subsys_vendor_id =PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET , + .subsys_id =PCI_SUBSYSTEM_ID_VIRTIO_NET , + .bar[0 ] =IOPORT_VIRTIO_NET |PCI_BASE_ADDRESS_SPACE_IO , };struct net_device {pthread_mutex_t mutex;struct virt_queue vqs[VIRTIO_NET_NUM_QUEUES ]; -struct virtio_net_config net_config; +struct virtio_net_config config;u32 host_features;u32 guest_features;u16 config_vector;
@@ -50,21 +53,21 @@ struct net_device {u16 queue_selector;pthread_t io_rx_thread; -pthread_mutex_t io_rx_mutex; +pthread_mutex_t io_rx_lock;pthread_cond_t io_rx_cond;pthread_t io_tx_thread; -pthread_mutex_t io_tx_mutex; +pthread_mutex_t io_tx_lock;pthread_cond_t io_tx_cond;int tap_fd;char tap_name[IFNAMSIZ ]; }; -static struct net_device net_device = { +static struct net_device ndev = { .mutex =PTHREAD_MUTEX_INITIALIZER , - .net_config = { + .config = { .mac = {0x00 ,0x11 ,0x22 ,0x33 ,0x44 ,0x55 }, .status =VIRTIO_NET_S_LINK_UP , },
@@ -88,21 +91,21 @@ static void *virtio_net_rx_thread(void *p)int len; kvm = p; - vq = &net_device.vqs[VIRTIO_NET_RX_QUEUE ]; + vq = &ndev.vqs[VIRTIO_NET_RX_QUEUE ];while (1 ) { -mutex_lock (&net_device.io_rx_mutex); +mutex_lock (&ndev.io_rx_lock);if (!virt_queue__available (vq)) -pthread_cond_wait (&net_device.io_rx_cond, &net_device.io_rx_mutex); -mutex_unlock (&net_device.io_rx_mutex); +pthread_cond_wait (&ndev.io_rx_cond, &ndev.io_rx_lock); +mutex_unlock (&ndev.io_rx_lock);while (virt_queue__available (vq)) { - head =virt_queue__get_iov (vq, iov, &out, &in, kvm); - len =readv (net_device.tap_fd, iov, in); + head =virt_queue__get_iov (vq, iov, &out, &in, kvm); + len =readv (ndev.tap_fd, iov, in);virt_queue__set_used_elem (vq, head, len);/* We should interrupt guest right now, otherwise latency is huge. */ -virt_queue__trigger_irq (vq, virtio_net_pci_device.irq_line, &net_device.isr, kvm); +virt_queue__trigger_irq (vq, pci_header.irq_line, &ndev.isr, kvm); } }
@@ -122,21 +125,21 @@ static void *virtio_net_tx_thread(void *p)int len; kvm = p; - vq = &net_device.vqs[VIRTIO_NET_TX_QUEUE ]; + vq = &ndev.vqs[VIRTIO_NET_TX_QUEUE ];while (1 ) { -mutex_lock (&net_device.io_tx_mutex); +mutex_lock (&ndev.io_tx_lock);if (!virt_queue__available (vq)) -pthread_cond_wait (&net_device.io_tx_cond, &net_device.io_tx_mutex); -mutex_unlock (&net_device.io_tx_mutex); +pthread_cond_wait (&ndev.io_tx_cond, &ndev.io_tx_lock); +mutex_unlock (&ndev.io_tx_lock);while (virt_queue__available (vq)) { - head =virt_queue__get_iov (vq, iov, &out, &in, kvm); - len =writev (net_device.tap_fd, iov, out); + head =virt_queue__get_iov (vq, iov, &out, &in, kvm); + len =writev (ndev.tap_fd, iov, out);virt_queue__set_used_elem (vq, head, len); } -virt_queue__trigger_irq (vq, virtio_net_pci_device.irq_line, &net_device.isr, kvm); +virt_queue__trigger_irq (vq, pci_header.irq_line, &ndev.isr, kvm); }
@@ -148,7 +151,7 @@ static void *virtio_net_tx_thread(void *p)static bool virtio_net_pci_io_device_specific_in (void *data,unsigned long offset,int size,u32 count) { -u8 *config_space = (u8 *) &net_device.net_config; +u8 *config_space = (u8 *)&ndev.config;if (size !=1 || count !=1 )return false;
@@ -166,17 +169,17 @@ static bool virtio_net_pci_io_in(struct kvm *kvm, u16 port, void *data, int sizeunsigned long offset = port -IOPORT_VIRTIO_NET ;bool ret = true; -mutex_lock (&net_device.mutex); +mutex_lock (&ndev.mutex);switch (offset) {case VIRTIO_PCI_HOST_FEATURES : -ioport__write32 (data, net_device.host_features); +ioport__write32 (data, ndev.host_features);break ;case VIRTIO_PCI_GUEST_FEATURES : ret = false;break ;case VIRTIO_PCI_QUEUE_PFN : -ioport__write32 (data, net_device.vqs[net_device.queue_selector].pfn); +ioport__write32 (data, ndev.vqs[ndev.queue_selector].pfn);break ;case VIRTIO_PCI_QUEUE_NUM :ioport__write16 (data,VIRTIO_NET_QUEUE_SIZE );
@@ -186,21 +189,21 @@ static bool virtio_net_pci_io_in(struct kvm *kvm, u16 port, void *data, int size ret = false;break ;case VIRTIO_PCI_STATUS : -ioport__write8 (data, net_device.status); +ioport__write8 (data, ndev.status);break ;case VIRTIO_PCI_ISR : -ioport__write8 (data, net_device.isr); -kvm__irq_line (kvm, virtio_net_pci_device.irq_line,VIRTIO_IRQ_LOW ); - net_device.isr =VIRTIO_IRQ_LOW ; +ioport__write8 (data, ndev.isr); +kvm__irq_line (kvm, pci_header.irq_line,VIRTIO_IRQ_LOW ); + ndev.isr =VIRTIO_IRQ_LOW ;break ;case VIRTIO_MSI_CONFIG_VECTOR : -ioport__write16 (data, net_device.config_vector); +ioport__write16 (data, ndev.config_vector);break ;default : ret =virtio_net_pci_io_device_specific_in (data, offset, size, count); }; -mutex_unlock (&net_device.mutex); +mutex_unlock (&ndev.mutex);return ret; }
@@ -209,15 +212,15 @@ static void virtio_net_handle_callback(struct kvm *kvm, u16 queue_index) {switch (queue_index) {case VIRTIO_NET_TX_QUEUE : { -mutex_lock (&net_device.io_tx_mutex); -pthread_cond_signal (&net_device.io_tx_cond); -mutex_unlock (&net_device.io_tx_mutex); +mutex_lock (&ndev.io_tx_lock); +pthread_cond_signal (&ndev.io_tx_cond); +mutex_unlock (&ndev.io_tx_lock);break ; }case VIRTIO_NET_RX_QUEUE : { -mutex_lock (&net_device.io_rx_mutex); -pthread_cond_signal (&net_device.io_rx_cond); -mutex_unlock (&net_device.io_rx_mutex); +mutex_lock (&ndev.io_rx_lock); +pthread_cond_signal (&ndev.io_rx_cond); +mutex_unlock (&ndev.io_rx_lock);break ; }default :
@@ -227,51 +230,52 @@ static void virtio_net_handle_callback(struct kvm *kvm, u16 queue_index)static bool virtio_net_pci_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) { -unsigned long offset = port -IOPORT_VIRTIO_NET ; -bool ret = true; +unsigned long offset = port -IOPORT_VIRTIO_NET ; +bool ret = true; -mutex_lock (&net_device.mutex); +mutex_lock (&ndev.mutex);switch (offset) {case VIRTIO_PCI_GUEST_FEATURES : - net_device.guest_features =ioport__read32 (data); + ndev.guest_features =ioport__read32 (data);break ;case VIRTIO_PCI_QUEUE_PFN : {struct virt_queue *queue;void *p; -assert (net_device.queue_selector <VIRTIO_NET_NUM_QUEUES ); +assert (ndev.queue_selector <VIRTIO_NET_NUM_QUEUES ); - queue = &net_device.vqs[net_device.queue_selector]; - queue->pfn =ioport__read32 (data); - p =guest_pfn_to_host (kvm, queue->pfn); + queue = &ndev.vqs[ndev.queue_selector]; + queue->pfn =ioport__read32 (data); + p =guest_pfn_to_host (kvm, queue->pfn);vring_init (&queue->vring,VIRTIO_NET_QUEUE_SIZE , p,VIRTIO_PCI_VRING_ALIGN );break ; }case VIRTIO_PCI_QUEUE_SEL : - net_device.queue_selector =ioport__read16 (data); + ndev.queue_selector =ioport__read16 (data);break ;case VIRTIO_PCI_QUEUE_NOTIFY : {u16 queue_index; - queue_index =ioport__read16 (data); + + queue_index =ioport__read16 (data);virtio_net_handle_callback (kvm, queue_index);break ; }case VIRTIO_PCI_STATUS : - net_device.status =ioport__read8 (data); + ndev.status =ioport__read8 (data);break ;case VIRTIO_MSI_CONFIG_VECTOR : - net_device.config_vector =VIRTIO_MSI_NO_VECTOR ; + ndev.config_vector =VIRTIO_MSI_NO_VECTOR ;break ;case VIRTIO_MSI_QUEUE_VECTOR :break ;default : - ret = false; + ret = false; }; -mutex_unlock (&net_device.mutex); +mutex_unlock (&ndev.mutex);return ret; }
@@ -289,36 +293,36 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params)struct ifreq ifr;for (i =0 ; i <6 ; i++) - net_device.net_config.mac[i] = params->guest_mac[i]; + ndev.config.mac[i] = params->guest_mac[i]; - net_device.tap_fd =open ("/dev/net/tun" ,O_RDWR ); -if (net_device.tap_fd <0 ) { + ndev.tap_fd =open ("/dev/net/tun" ,O_RDWR ); +if (ndev.tap_fd <0 ) {warning ("Unable to open /dev/net/tun" ); goto fail; }memset (&ifr,0 ,sizeof (ifr)); ifr.ifr_flags =IFF_TAP |IFF_NO_PI |IFF_VNET_HDR ; -if (ioctl (net_device.tap_fd,TUNSETIFF , &ifr) <0 ) { +if (ioctl (ndev.tap_fd,TUNSETIFF , &ifr) <0 ) {warning ("Config tap device error. Are you root?" ); goto fail; } -strncpy (net_device.tap_name, ifr.ifr_name,sizeof (net_device.tap_name)); +strncpy (ndev.tap_name, ifr.ifr_name,sizeof (ndev.tap_name)); -if (ioctl (net_device.tap_fd,TUNSETNOCSUM ,1 ) <0 ) { +if (ioctl (ndev.tap_fd,TUNSETNOCSUM ,1 ) <0 ) {warning ("Config tap device TUNSETNOCSUM error" ); goto fail; } hdr_len =sizeof (struct virtio_net_hdr ); -if (ioctl (net_device.tap_fd,TUNSETVNETHDRSZ , &hdr_len) <0 ) { +if (ioctl (ndev.tap_fd,TUNSETVNETHDRSZ , &hdr_len) <0 ) {warning ("Config tap device TUNSETVNETHDRSZ error" ); goto fail; } offload =TUN_F_CSUM |TUN_F_TSO4 |TUN_F_TSO6 |TUN_F_UFO ; -if (ioctl (net_device.tap_fd,TUNSETOFFLOAD , offload) <0 ) { +if (ioctl (ndev.tap_fd,TUNSETOFFLOAD , offload) <0 ) {warning ("Config tap device TUNSETOFFLOAD error" ); goto fail; }
@@ -326,7 +330,7 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params)if (strcmp (params->script,"none" )) { pid =fork ();if (pid ==0 ) { -execl (params->script, params->script, net_device.tap_name,NULL ); +execl (params->script, params->script, ndev.tap_name,NULL );_exit (1 ); }else {waitpid (pid, &status,0 );
@@ -337,7 +341,7 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params) } }else {memset (&ifr,0 ,sizeof (ifr)); -strncpy (ifr.ifr_name, net_device.tap_name,sizeof (net_device.tap_name)); +strncpy (ifr.ifr_name, ndev.tap_name,sizeof (ndev.tap_name)); sin.sin_addr.s_addr =inet_addr (params->host_ip);memcpy (&(ifr.ifr_addr), &sin,sizeof (ifr.ifr_addr)); ifr.ifr_addr.sa_family =AF_INET ;
@@ -348,7 +352,7 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params) }memset (&ifr,0 ,sizeof (ifr)); -strncpy (ifr.ifr_name, net_device.tap_name,sizeof (net_device.tap_name)); +strncpy (ifr.ifr_name, ndev.tap_name,sizeof (ndev.tap_name));ioctl (sock,SIOCGIFFLAGS , &ifr); ifr.ifr_flags |=IFF_UP |IFF_RUNNING ;if (ioctl (sock,SIOCSIFFLAGS , &ifr) <0 )
@@ -361,22 +365,22 @@ static bool virtio_net__tap_init(const struct virtio_net_parameters *params) fail:if (sock >=0 )close (sock); -if (net_device.tap_fd >=0 ) -close (net_device.tap_fd); +if (ndev.tap_fd >=0 ) +close (ndev.tap_fd);return 0 ; }static void virtio_net__io_thread_init (struct kvm *kvm) { -pthread_mutex_init (&net_device.io_rx_mutex,NULL ); -pthread_cond_init (&net_device.io_tx_cond,NULL ); +pthread_mutex_init (&ndev.io_rx_lock,NULL ); +pthread_cond_init (&ndev.io_tx_cond,NULL ); -pthread_mutex_init (&net_device.io_rx_mutex,NULL ); -pthread_cond_init (&net_device.io_tx_cond,NULL ); +pthread_mutex_init (&ndev.io_rx_lock,NULL ); +pthread_cond_init (&ndev.io_tx_cond,NULL ); -pthread_create (&net_device.io_rx_thread,NULL , virtio_net_rx_thread, (void *)kvm); -pthread_create (&net_device.io_tx_thread,NULL , virtio_net_tx_thread, (void *)kvm); +pthread_create (&ndev.io_rx_thread,NULL , virtio_net_rx_thread, (void *)kvm); +pthread_create (&ndev.io_tx_thread,NULL , virtio_net_tx_thread, (void *)kvm); }void virtio_net__init (const struct virtio_net_parameters *params)
@@ -387,9 +391,9 @@ void virtio_net__init(const struct virtio_net_parameters *params)if (irq__register_device (PCI_DEVICE_ID_VIRTIO_NET , &dev, &pin, &line) <0 )return ; - virtio_net_pci_device.irq_pin = pin; - virtio_net_pci_device.irq_line = line; -pci__register (&virtio_net_pci_device, dev); + pci_header.irq_pin = pin; + pci_header.irq_line = line; +pci__register (&pci_header, dev);ioport__register (IOPORT_VIRTIO_NET , &virtio_net_io_ops,IOPORT_VIRTIO_NET_SIZE );virtio_net__io_thread_init (params->kvm);