]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer.pm
use aio=native only with O_DIRECT (cache=none|directsync)
[qemu-server.git] / PVE / QemuServer.pm
index 52b1a1cfaffe7f2321bc9c39a7157712a731adf1..baf0e64df9b8fcb5563a7b98b31beba9cd4635fb 100644 (file)
@@ -152,12 +152,6 @@ mkdir $lock_dir;
 my $pcisysfs = "/sys/bus/pci";
 
 my $confdesc = {
-    iothread => {
-       optional => 1,
-       type => 'boolean',
-       description => "Enable iothread dataplane.",
-       default => 0,
-    },
     onboot => {
        optional => 1,
        type => 'boolean',
@@ -240,7 +234,7 @@ my $confdesc = {
        optional => 1,
        type => 'string',
        description => "scsi controller model",
-       enum => [qw(lsi lsi53c810 virtio-scsi-pci megasas pvscsi)],
+       enum => [qw(lsi lsi53c810 virtio-scsi-pci virtio-scsi-single megasas pvscsi)],
        default => 'lsi',
     },
     description => {
@@ -424,7 +418,7 @@ EODESCR
        optional => 1,
        description => "Emulated CPU type.",
        type => 'string',
-       enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom Conroe Penryn Nehalem Westmere SandyBridge Haswell Broadwell Opteron_G1 Opteron_G2 Opteron_G3 Opteron_G4 Opteron_G5 host) ],
+       enum => [ qw(486 athlon pentium pentium2 pentium3 coreduo core2duo kvm32 kvm64 qemu32 qemu64 phenom Conroe Penryn Nehalem Westmere SandyBridge IvyBridge Haswell Broadwell Opteron_G1 Opteron_G2 Opteron_G3 Opteron_G4 Opteron_G5 host) ],
        default => 'kvm64',
     },
     parent => get_standard_option('pve-snapshot-name', {
@@ -555,7 +549,7 @@ PVE::JSONSchema::register_standard_option("pve-qm-ide", $idedesc);
 my $scsidesc = {
     optional => 1,
     type => 'string', format => 'pve-qm-drive',
-    typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on]',
+    typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads] [,discard=ignore|on] [,iothread=on]',
     description => "Use volume as SCSI hard disk or CD-ROM (n is 0 to " . ($MAX_SCSI_DISKS - 1) . ").",
 };
 PVE::JSONSchema::register_standard_option("pve-qm-scsi", $scsidesc);
@@ -571,7 +565,7 @@ PVE::JSONSchema::register_standard_option("pve-qm-sata", $satadesc);
 my $virtiodesc = {
     optional => 1,
     type => 'string', format => 'pve-qm-drive',
-    typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads]  [,discard=ignore|on]',
+    typetext => '[volume=]volume,] [,media=cdrom|disk] [,cyls=c,heads=h,secs=s[,trans=t]] [,snapshot=on|off] [,cache=none|writethrough|writeback|unsafe|directsync] [,format=f] [,backup=yes|no] [,rerror=ignore|report|stop] [,werror=enospc|ignore|report|stop] [,aio=native|threads]  [,discard=ignore|on] [,iothread=on]',
     description => "Use volume as VIRTIO hard disk (n is 0 to " . ($MAX_VIRTIO_DISKS - 1) . ").",
 };
 PVE::JSONSchema::register_standard_option("pve-qm-virtio", $virtiodesc);
@@ -879,7 +873,7 @@ sub parse_hotplug_features {
     my $res = {};
 
     return $res if $data eq '0';
-    
+
     $data = $confdesc->{hotplug}->{default} if $data eq '1';
 
     foreach my $feature (PVE::Tools::split_list($data)) {
@@ -940,7 +934,7 @@ my $format_size = sub {
 # ideX = [volume=]volume-id[,media=d][,cyls=c,heads=h,secs=s[,trans=t]]
 #        [,snapshot=on|off][,cache=on|off][,format=f][,backup=yes|no]
 #        [,rerror=ignore|report|stop][,werror=enospc|ignore|report|stop]
-#        [,aio=native|threads][,discard=ignore|on]
+#        [,aio=native|threads][,discard=ignore|on][,iothread=on]
 
 sub parse_drive {
     my ($key, $data) = @_;
@@ -961,7 +955,7 @@ sub parse_drive {
     foreach my $p (split (/,/, $data)) {
        next if $p =~ m/^\s*$/;
 
-       if ($p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio|bps|mbps|mbps_max|bps_rd|mbps_rd|mbps_rd_max|bps_wr|mbps_wr|mbps_wr_max|iops|iops_max|iops_rd|iops_rd_max|iops_wr|iops_wr_max|size|discard)=(.+)$/) {
+       if ($p =~ m/^(file|volume|cyls|heads|secs|trans|media|snapshot|cache|format|rerror|werror|backup|aio|bps|mbps|mbps_max|bps_rd|mbps_rd|mbps_rd_max|bps_wr|mbps_wr|mbps_wr_max|iops|iops_max|iops_rd|iops_rd_max|iops_wr|iops_wr_max|size|discard|iothread)=(.+)$/) {
            my ($k, $v) = ($1, $2);
 
            $k = 'file' if $k eq 'volume';
@@ -1003,6 +997,7 @@ sub parse_drive {
     return undef if $res->{backup} && $res->{backup} !~ m/^(yes|no)$/;
     return undef if $res->{aio} && $res->{aio} !~ m/^(native|threads)$/;
     return undef if $res->{discard} && $res->{discard} !~ m/^(ignore|on)$/;
+    return undef if $res->{iothread} && $res->{iothread} !~ m/^(on)$/;
 
     return undef if $res->{mbps_rd} && $res->{mbps};
     return undef if $res->{mbps_wr} && $res->{mbps};
@@ -1050,7 +1045,7 @@ sub print_drive {
     my ($vmid, $drive) = @_;
 
     my $opts = '';
-    foreach my $o (@qemu_drive_options, 'mbps', 'mbps_rd', 'mbps_wr', 'mbps_max', 'mbps_rd_max', 'mbps_wr_max', 'backup') {
+    foreach my $o (@qemu_drive_options, 'mbps', 'mbps_rd', 'mbps_wr', 'mbps_max', 'mbps_rd_max', 'mbps_wr_max', 'backup', 'iothread') {
        $opts .= ",$o=$drive->{$o}" if $drive->{$o};
     }
 
@@ -1148,10 +1143,10 @@ sub print_drivedevice_full {
     if ($drive->{interface} eq 'virtio') {
        my $pciaddr = print_pci_addr("$drive->{interface}$drive->{index}", $bridges);
        $device = "virtio-blk-pci,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}$pciaddr";
-       $device .= ",iothread=iothread0" if $conf->{iothread};
+       $device .= ",iothread=iothread-$drive->{interface}$drive->{index}" if $drive->{iothread};
     } elsif ($drive->{interface} eq 'scsi') {
-       $maxdev = ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)) ? 256 : 7;
-       my $controller = int($drive->{index} / $maxdev);
+
+       my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
        my $unit = $drive->{index} % $maxdev;
        my $devicetype = 'hd';
         my $path = '';
@@ -1178,9 +1173,9 @@ sub print_drivedevice_full {
          }
 
         if (!$conf->{scsihw} || ($conf->{scsihw} =~ m/^lsi/)){
-            $device = "scsi-$devicetype,bus=scsihw$controller.0,scsi-id=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
+            $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,scsi-id=$unit,drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
         } else {
-            $device = "scsi-$devicetype,bus=scsihw$controller.0,channel=0,scsi-id=0,lun=$drive->{index},drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
+            $device = "scsi-$devicetype,bus=$controller_prefix$controller.0,channel=0,scsi-id=0,lun=$drive->{index},drive=drive-$drive->{interface}$drive->{index},id=$drive->{interface}$drive->{index}";
         }
 
     } elsif ($drive->{interface} eq 'ide'){
@@ -1234,8 +1229,15 @@ sub print_drive_full {
        $opts .= ",$o=" . int($v*1024*1024) if $v;
     }
 
-    # use linux-aio by default (qemu default is threads)
-    $opts .= ",aio=native" if !$drive->{aio};
+    # aio native works only with O_DIRECT
+    if (!$drive->{aio}) {
+       if(!$drive->{cache} || $drive->{cache} eq 'none' || $drive->{cache} eq 'directsync') {
+           $opts .= ",aio=native";
+       } else {
+           $opts .= ",aio=threads";
+       }
+    }
+
 
     my $path;
     my $volid = $drive->{file};
@@ -1431,6 +1433,7 @@ sub print_net {
     $res .= ",tag=$net->{tag}" if $net->{tag};
     $res .= ",firewall=1" if $net->{firewall};
     $res .= ",link_down=1" if $net->{link_down};
+    $res .= ",queues=$net->{queues}" if $net->{queues};
 
     return $res;
 }
@@ -2401,6 +2404,7 @@ sub vmstatus {
 
        $d->{cpus} = ($conf->{sockets} || 1) * ($conf->{cores} || 1);
        $d->{cpus} = $cpucount if $d->{cpus} > $cpucount;
+       $d->{cpus} = $conf->{vcpus} if $conf->{vcpus};
 
        $d->{name} = $conf->{name} || "VM $vmid";
        $d->{maxmem} = $conf->{memory} ? $conf->{memory}*(1024*1024) : 0;
@@ -2649,6 +2653,7 @@ sub config_to_command {
 
     my $q35 = machine_type_is_q35($conf);
     my $hotplug_features = parse_hotplug_features(defined($conf->{hotplug}) ? $conf->{hotplug} : '1');
+    my $machine_type = $forcemachine || $conf->{machine};
 
     push @$cmd, '/usr/bin/kvm';
 
@@ -2671,8 +2676,6 @@ sub config_to_command {
        push @$cmd, '-smbios', "type=1,$conf->{smbios1}";
     }
 
-    push @$cmd, '-object', "iothread,id=iothread0" if $conf->{iothread};
-
     if ($q35) {
        # the q35 chipset support native usb2, so we enable usb controller
        # by default for this machine type
@@ -2862,8 +2865,14 @@ sub config_to_command {
            $ost eq 'wvista') {
            push @$globalFlags, 'kvm-pit.lost_tick_policy=discard';
            push @$cmd, '-no-hpet';
-           #push @$cpuFlags , 'hv_vapic" if !$nokvm;  #fixme, my win2008R2 hang at boot with this
-           push @$cpuFlags , 'hv_spinlocks=0xffff' if !$nokvm;
+           if (qemu_machine_feature_enabled ($machine_type, $kvmver, 2, 3)) {
+               push @$cpuFlags , 'hv_spinlocks=0x1fff' if !$nokvm;
+               push @$cpuFlags , 'hv_vapic' if !$nokvm;
+               push @$cpuFlags , 'hv_time' if !$nokvm;
+
+           } else {
+               push @$cpuFlags , 'hv_spinlocks=0xffff' if !$nokvm;
+           }
        }
 
        if ($ost eq 'win7' || $ost eq 'win8') {
@@ -2879,7 +2888,6 @@ sub config_to_command {
        die "No accelerator found!\n" if !$cpuinfo->{hvm};
     }
 
-    my $machine_type = $forcemachine || $conf->{machine};
     if ($machine_type) {
        push @$machineFlags, "type=${machine_type}";
     }
@@ -2901,23 +2909,27 @@ sub config_to_command {
 
     push @$cpuFlags, '+sep' if $cpu eq 'kvm64' || $cpu eq 'kvm32';
 
+    if (qemu_machine_feature_enabled ($machine_type, $kvmver, 2, 3)) {
+
+       push @$cpuFlags , '+kvm_pv_unhalt' if !$nokvm;
+       push @$cpuFlags , '+kvm_pv_eoi' if !$nokvm;
+    }
+
     $cpu .= "," . join(',', @$cpuFlags) if scalar(@$cpuFlags);
 
-    # Note: enforce needs kernel 3.10, so we do not use it for now
-    # push @$cmd, '-cpu', "$cpu,enforce";
-    push @$cmd, '-cpu', $cpu;
+    push @$cmd, '-cpu', "$cpu,enforce";
 
     my $memory = $conf->{memory} || $defaults->{memory};
     my $static_memory = 0;
     my $dimm_memory = 0;
 
     if ($hotplug_features->{memory}) {
-       die "Numa need to be enabled for memory hotplug" if !$conf->{numa};
-       die "Total memory is bigger than $MAX_MEM MB" if $memory > $MAX_MEM;
+       die "Numa need to be enabled for memory hotplug\n" if !$conf->{numa};
+       die "Total memory is bigger than ${MAX_MEM}MB\n" if $memory > $MAX_MEM;
        $static_memory = $STATICMEM;
-       die "minimum memory must be $static_memory"."MB" if($memory < $static_memory);
+       die "minimum memory must be ${static_memory}MB\n" if($memory < $static_memory);
        $dimm_memory = $memory - $static_memory;
-       push @$cmd, '-m', "size=".$static_memory.",slots=255,maxmem=".$MAX_MEM."M";
+       push @$cmd, '-m', "size=${static_memory},slots=255,maxmem=${MAX_MEM}M";
 
     } else {
 
@@ -2936,7 +2948,7 @@ sub config_to_command {
            die "missing numa node$i memory value\n" if !$numa->{memory};
            my $numa_memory = $numa->{memory};
            $numa_totalmemory += $numa_memory;
-           my $numa_object = "memory-backend-ram,id=ram-node$i,size=$numa_memory"."M";
+           my $numa_object = "memory-backend-ram,id=ram-node$i,size=${numa_memory}M";
 
            # cpus
            my $cpus_start = $numa->{cpus}->{start};
@@ -2997,7 +3009,7 @@ sub config_to_command {
     if ($hotplug_features->{memory}) {
        foreach_dimm($conf, $vmid, $memory, $sockets, sub {
            my ($conf, $vmid, $name, $dimm_size, $numanode, $current_size, $memory) = @_;
-           push @$cmd, "-object" , "memory-backend-ram,id=mem-$name,size=$dimm_size"."M";
+           push @$cmd, "-object" , "memory-backend-ram,id=mem-$name,size=${dimm_size}M";
            push @$cmd, "-device", "pc-dimm,id=$name,memdev=mem-$name,node=$numanode";
 
            #if dimm_memory is not aligned to dimm map
@@ -3099,12 +3111,24 @@ sub config_to_command {
            }
        }
 
+       if($drive->{interface} eq 'virtio'){
+           push @$cmd, '-object', "iothread,id=iothread-$ds" if $drive->{iothread};
+       }
+
         if ($drive->{interface} eq 'scsi') {
 
-           my $maxdev = ($scsihw !~ m/^lsi/) ? 256 : 7;
-           my $controller = int($drive->{index} / $maxdev);
-           $pciaddr = print_pci_addr("scsihw$controller", $bridges);
-           push @$devices, '-device', "$scsihw,id=scsihw$controller$pciaddr" if !$scsicontroller->{$controller};
+           my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $drive);
+
+           $pciaddr = print_pci_addr("$controller_prefix$controller", $bridges);
+           my $scsihw_type = $scsihw =~ m/^virtio-scsi-single/ ? "virtio-scsi-pci" : $scsihw;
+
+           my $iothread = '';
+           if($conf->{scsihw} && $conf->{scsihw} eq "virtio-scsi-single" && $drive->{iothread}){
+               $iothread .= ",iothread=iothread-$controller_prefix$controller";
+               push @$cmd, '-object', "iothread,id=iothread-$controller_prefix$controller";
+           }
+
+           push @$devices, '-device', "$scsihw_type,id=$controller_prefix$controller$pciaddr$iothread" if !$scsicontroller->{$controller};
            $scsicontroller->{$controller}=1;
         }
 
@@ -3141,6 +3165,13 @@ sub config_to_command {
 
     if (!$q35) {
        # add pci bridges
+        if (qemu_machine_feature_enabled ($machine_type, $kvmver, 2, 3)) {
+          $bridges->{1} = 1;
+          $bridges->{2} = 1;
+       }
+
+       $bridges->{3} = 1 if $scsihw =~ m/^virtio-scsi-single/;
+
        while (my ($k, $v) = each %$bridges) {
            $pciaddr = print_pci_addr("pci.$k");
            unshift @$devices, '-device', "pci-bridge,id=pci.$k,chassis_nr=$k$pciaddr" if $k > 0;
@@ -3209,7 +3240,16 @@ sub vm_devices_list {
     foreach my $pcibus (@$res) {
        foreach my $device (@{$pcibus->{devices}}) {
            next if !$device->{'qdev_id'};
-           $devices->{$device->{'qdev_id'}} = 1;
+           if ($device->{'pci_bridge'}) {
+               $devices->{$device->{'qdev_id'}} = 1;
+               foreach my $bridge_device (@{$device->{'pci_bridge'}->{devices}}) {
+                   next if !$bridge_device->{'qdev_id'};
+                   $devices->{$bridge_device->{'qdev_id'}} = 1;
+                   $devices->{$device->{'qdev_id'}}++;
+               }
+           } else {
+               $devices->{$device->{'qdev_id'}} = 1;
+           }
        }
     }
 
@@ -3247,6 +3287,8 @@ sub vm_deviceplug {
 
     } elsif ($deviceid =~ m/^(virtio)(\d+)$/) {
 
+       qemu_iothread_add($vmid, $deviceid, $device);
+
         qemu_driveadd($storecfg, $vmid, $device);
         my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
 
@@ -3258,11 +3300,19 @@ sub vm_deviceplug {
            die $err;
         }
 
-    } elsif ($deviceid =~ m/^(scsihw)(\d+)$/) {
+    } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
+
 
         my $scsihw = defined($conf->{scsihw}) ? $conf->{scsihw} : "lsi";
         my $pciaddr = print_pci_addr($deviceid);
-        my $devicefull = "$scsihw,id=$deviceid$pciaddr";
+       my $scsihw_type = $scsihw eq 'virtio-scsi-single' ? "virtio-scsi-pci" : $scsihw;
+
+        my $devicefull = "$scsihw_type,id=$deviceid$pciaddr";
+
+       if($deviceid =~ m/^virtioscsi(\d+)$/ && $device->{iothread}) {
+           qemu_iothread_add($vmid, $deviceid, $device);
+           $devicefull .= ",iothread=iothread-$deviceid";
+       }
 
         qemu_deviceadd($vmid, $devicefull);
         qemu_deviceaddverify($vmid, $deviceid);
@@ -3271,7 +3321,7 @@ sub vm_deviceplug {
 
         qemu_findorcreatescsihw($storecfg,$conf, $vmid, $device);
         qemu_driveadd($storecfg, $vmid, $device);
-        
+
        my $devicefull = print_drivedevice_full($storecfg, $conf, $vmid, $device);
        eval { qemu_deviceadd($vmid, $devicefull); };
        if (my $err = $@) {
@@ -3297,12 +3347,12 @@ sub vm_deviceplug {
        my $bridgeid = $2;
        my $pciaddr = print_pci_addr($deviceid);
        my $devicefull = "pci-bridge,id=pci.$bridgeid,chassis_nr=$bridgeid$pciaddr";
-       
+
        qemu_deviceadd($vmid, $devicefull);
        qemu_deviceaddverify($vmid, $deviceid);
 
     } else {
-       die "can't hotplug device '$deviceid'\n";       
+       die "can't hotplug device '$deviceid'\n";
     }
 
     return 1;
@@ -3326,16 +3376,24 @@ sub vm_deviceunplug {
         qemu_devicedel($vmid, $deviceid);
         qemu_devicedelverify($vmid, $deviceid);
         qemu_drivedel($vmid, $deviceid);
-   
-    } elsif ($deviceid =~ m/^(lsi)(\d+)$/) {
-    
+       qemu_iothread_del($conf, $vmid, $deviceid);
+
+    } elsif ($deviceid =~ m/^(virtioscsi|scsihw)(\d+)$/) {
+
        qemu_devicedel($vmid, $deviceid);
-    
+       qemu_devicedelverify($vmid, $deviceid);
+       qemu_iothread_del($conf, $vmid, $deviceid);
+
     } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
 
+       #qemu 2.3 segfault on drive_del with virtioscsi + iothread
+       my $device = parse_drive($deviceid, $conf->{$deviceid});
+       die "virtioscsi with iothread is not hot-unplugglable currently" if $device->{iothread};
+
         qemu_devicedel($vmid, $deviceid);
         qemu_drivedel($vmid, $deviceid);
-    
+       qemu_deletescsihw($conf, $vmid, $deviceid);
+
     } elsif ($deviceid =~ m/^(net)(\d+)$/) {
 
         qemu_devicedel($vmid, $deviceid);
@@ -3364,6 +3422,25 @@ sub qemu_devicedel {
     my $ret = vm_mon_cmd($vmid, "device_del", id => $deviceid);
 }
 
+sub qemu_iothread_add {
+    my($vmid, $deviceid, $device) = @_;
+
+    if ($device->{iothread}) {
+       my $iothreads = vm_iothreads_list($vmid);
+       qemu_objectadd($vmid, "iothread-$deviceid", "iothread") if !$iothreads->{"iothread-$deviceid"};
+    }
+}
+
+sub qemu_iothread_del {
+    my($conf, $vmid, $deviceid) = @_;
+
+    my $device = parse_drive($deviceid, $conf->{$deviceid});
+    if ($device->{iothread}) {
+       my $iothreads = vm_iothreads_list($vmid);
+       qemu_objectdel($vmid, "iothread-$deviceid") if $iothreads->{"iothread-$deviceid"};
+    }
+}
+
 sub qemu_objectadd {
     my($vmid, $objectid, $qomtype) = @_;
 
@@ -3384,7 +3461,8 @@ sub qemu_driveadd {
     my ($storecfg, $vmid, $device) = @_;
 
     my $drive = print_drive_full($storecfg, $vmid, $device);
-    my $ret = vm_human_monitor_command($vmid, "drive_add auto $drive");
+    $drive =~ s/\\/\\\\/g;
+    my $ret = vm_human_monitor_command($vmid, "drive_add auto \"$drive\"");
 
     # If the command succeeds qemu prints: "OK"
     return 1 if $ret =~ m/OK/s;
@@ -3397,12 +3475,12 @@ sub qemu_drivedel {
 
     my $ret = vm_human_monitor_command($vmid, "drive_del drive-$deviceid");
     $ret =~ s/^\s+//;
-    
+
     return 1 if $ret eq "";
-  
+
     # NB: device not found errors mean the drive was auto-deleted and we ignore the error
-    return 1 if $ret =~ m/Device \'.*?\' not found/s; 
-    
+    return 1 if $ret =~ m/Device \'.*?\' not found/s;
+
     die "deleting drive $deviceid failed : $ret\n";
 }
 
@@ -3422,7 +3500,7 @@ sub qemu_deviceaddverify {
 sub qemu_devicedelverify {
     my ($vmid, $deviceid) = @_;
 
-    # need to verify that the device is correctly removed as device_del 
+    # need to verify that the device is correctly removed as device_del
     # is async and empty return is not reliable
 
     for (my $i = 0; $i <= 5; $i++) {
@@ -3437,15 +3515,44 @@ sub qemu_devicedelverify {
 sub qemu_findorcreatescsihw {
     my ($storecfg, $conf, $vmid, $device) = @_;
 
-    my $maxdev = ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)) ? 256 : 7;
-    my $controller = int($device->{index} / $maxdev);
-    my $scsihwid="scsihw$controller";
+    my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
+
+    my $scsihwid="$controller_prefix$controller";
     my $devices_list = vm_devices_list($vmid);
 
     if(!defined($devices_list->{$scsihwid})) {
-       vm_deviceplug($storecfg, $conf, $vmid, $scsihwid);
+       vm_deviceplug($storecfg, $conf, $vmid, $scsihwid, $device);
+    }
+
+    return 1;
+}
+
+sub qemu_deletescsihw {
+    my ($conf, $vmid, $opt) = @_;
+
+    my $device = parse_drive($opt, $conf->{$opt});
+
+    if ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
+       vm_deviceunplug($vmid, $conf, "virtioscsi$device->{index}");
+       return 1;
     }
 
+    my ($maxdev, $controller, $controller_prefix) = scsihw_infos($conf, $device);
+
+    my $devices_list = vm_devices_list($vmid);
+    foreach my $opt (keys %{$devices_list}) {
+       if (PVE::QemuServer::valid_drivename($opt)) {
+           my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
+           if($drive->{interface} eq 'scsi' && $drive->{index} < (($maxdev-1)*($controller+1))) {
+               return 1;
+           }
+       }
+    }
+
+    my $scsihwid="scsihw$controller";
+
+    vm_deviceunplug($vmid, $conf, $scsihwid);
+
     return 1;
 }
 
@@ -3476,7 +3583,7 @@ sub qemu_add_pci_bridge {
 sub qemu_set_link_status {
     my ($vmid, $device, $up) = @_;
 
-    vm_mon_cmd($vmid, "set_link", name => $device, 
+    vm_mon_cmd($vmid, "set_link", name => $device,
               up => $up ? JSON::true : JSON::false);
 }
 
@@ -3527,9 +3634,9 @@ sub qemu_memory_hotplug {
     my ($vmid, $conf, $defaults, $opt, $value) = @_;
 
     return $value if !check_running($vmid);
+
     my $memory = $conf->{memory} || $defaults->{memory};
-    $value = $defaults->{memory} if !$value; 
+    $value = $defaults->{memory} if !$value;
     return $value if $value == $memory;
 
     my $static_memory = $STATICMEM;
@@ -3745,9 +3852,9 @@ sub set_migration_caps {
 }
 
 my $fast_plug_option = {
+    'lock' => 1,
     'name' => 1,
-    'hotplug' => 1,
-    'onboot' => 1, 
+    'onboot' => 1,
     'shares' => 1,
     'startup' => 1,
 };
@@ -3790,7 +3897,9 @@ sub vmconfig_hotplug_pending {
     foreach my $opt (@delete) {
        next if $selection && !$selection->{$opt};
        eval {
-           if ($opt eq 'tablet') {
+           if ($opt eq 'hotplug') {
+               die "skip\n" if ($conf->{hotplug} =~ /memory/);
+           } elsif ($opt eq 'tablet') {
                die "skip\n" if !$hotplug_features->{usb};
                if ($defaults->{tablet}) {
                    vm_deviceplug($storecfg, $conf, $vmid, $opt);
@@ -3834,7 +3943,9 @@ sub vmconfig_hotplug_pending {
        next if $selection && !$selection->{$opt};
        my $value = $conf->{pending}->{$opt};
        eval {
-           if ($opt eq 'tablet') {
+           if ($opt eq 'hotplug') {
+               die "skip\n" if ($value =~ /memory/) || ($value !~ /memory/ && $conf->{hotplug} =~ /memory/);
+           } elsif ($opt eq 'tablet') {
                die "skip\n" if !$hotplug_features->{usb};
                if ($value == 1) {
                    vm_deviceplug($storecfg, $conf, $vmid, $opt);
@@ -3847,17 +3958,17 @@ sub vmconfig_hotplug_pending {
            } elsif ($opt eq 'balloon') {
                # enable/disable balloning device is not hotpluggable
                my $old_balloon_enabled =  !!(!defined($conf->{balloon}) || $conf->{balloon});
-               my $new_balloon_enabled =  !!(!defined($conf->{pending}->{balloon}) || $conf->{pending}->{balloon});            
+               my $new_balloon_enabled =  !!(!defined($conf->{pending}->{balloon}) || $conf->{pending}->{balloon});
                die "skip\n" if $old_balloon_enabled != $new_balloon_enabled;
 
                # allow manual ballooning if shares is set to zero
-               if (!(defined($conf->{shares}) && ($conf->{shares} == 0))) {
+               if ((defined($conf->{shares}) && ($conf->{shares} == 0))) {
                    my $balloon = $conf->{pending}->{balloon} || $conf->{memory} || $defaults->{memory};
                    vm_mon_cmd($vmid, "balloon", value => $balloon*1024*1024);
                }
-           } elsif ($opt =~ m/^net(\d+)$/) { 
+           } elsif ($opt =~ m/^net(\d+)$/) {
                # some changes can be done without hotplug
-               vmconfig_update_net($storecfg, $conf, $hotplug_features->{network}, 
+               vmconfig_update_net($storecfg, $conf, $hotplug_features->{network},
                                    $vmid, $opt, $value);
            } elsif (valid_drivename($opt)) {
                # some changes can be done without hotplug
@@ -3966,7 +4077,7 @@ sub vmconfig_update_net {
 
            die "internal error" if $opt !~ m/net(\d+)/;
            my $iface = "tap${vmid}i$1";
-               
+
            if (&$safe_num_ne($oldnet->{rate}, $newnet->{rate})) {
                PVE::Network::tap_rate_limit($iface, $newnet->{rate});
            }
@@ -3985,7 +4096,7 @@ sub vmconfig_update_net {
            return 1;
        }
     }
-    
+
     if ($hotplug) {
        vm_deviceplug($storecfg, $conf, $vmid, $opt, $newnet);
     } else {
@@ -4010,19 +4121,20 @@ sub vmconfig_update_disk {
 
            if (!drive_is_cdrom($old_drive)) {
 
-               if ($drive->{file} ne $old_drive->{file}) {  
+               if ($drive->{file} ne $old_drive->{file}) {
 
                    die "skip\n" if !$hotplug;
 
                    # unplug and register as unused
                    vm_deviceunplug($vmid, $conf, $opt);
                    vmconfig_register_unused_drive($storecfg, $vmid, $conf, $old_drive)
-       
+
                } else {
                    # update existing disk
 
                    # skip non hotpluggable value
-                   if (&$safe_num_ne($drive->{discard}, $old_drive->{discard}) || 
+                   if (&$safe_num_ne($drive->{discard}, $old_drive->{discard}) ||
+                       &$safe_string_ne($drive->{iothread}, $old_drive->{iothread}) ||
                        &$safe_string_ne($drive->{cache}, $old_drive->{cache})) {
                        die "skip\n";
                    }
@@ -4040,7 +4152,7 @@ sub vmconfig_update_disk {
                        &$safe_num_ne($drive->{iops_max}, $old_drive->{iops_max}) ||
                        &$safe_num_ne($drive->{iops_rd_max}, $old_drive->{iops_rd_max}) ||
                        &$safe_num_ne($drive->{iops_wr_max}, $old_drive->{iops_wr_max})) {
-                       
+
                        qemu_block_set_io_throttle($vmid,"drive-$opt",
                                                   ($drive->{mbps} || 0)*1024*1024,
                                                   ($drive->{mbps_rd} || 0)*1024*1024,
@@ -4056,28 +4168,28 @@ sub vmconfig_update_disk {
                                                   $drive->{iops_wr_max} || 0);
 
                    }
-                   
+
                    return 1;
                }
-           }
-       }
-    }
 
-    if (drive_is_cdrom($drive)) { # cdrom
+           } else { # cdrom
 
-       if ($drive->{file} eq 'none') {
-           vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
-       } else {
-           my $path = get_iso_path($storecfg, $vmid, $drive->{file});
-           vm_mon_cmd($vmid, "eject", force => JSON::true,device => "drive-$opt"); # force eject if locked
-           vm_mon_cmd($vmid, "change", device => "drive-$opt",target => "$path") if $path;
-       }
+               if ($drive->{file} eq 'none') {
+                   vm_mon_cmd($vmid, "eject",force => JSON::true,device => "drive-$opt");
+               } else {
+                   my $path = get_iso_path($storecfg, $vmid, $drive->{file});
+                   vm_mon_cmd($vmid, "eject", force => JSON::true,device => "drive-$opt"); # force eject if locked
+                   vm_mon_cmd($vmid, "change", device => "drive-$opt",target => "$path") if $path;
+               }
 
-    } else { 
-       die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;   
-       # hotplug new disks
-       vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
+               return 1;
+           }
+       }
     }
+
+    die "skip\n" if !$hotplug || $opt =~ m/(ide|sata)(\d+)/;
+    # hotplug new disks
+    vm_deviceplug($storecfg, $conf, $vmid, $opt, $drive);
 }
 
 sub vm_start {
@@ -4181,10 +4293,6 @@ sub vm_start {
            if (!$statefile && (!defined($conf->{balloon}) || $conf->{balloon})) {
                vm_mon_cmd_nocheck($vmid, "balloon", value => $conf->{balloon}*1024*1024)
                    if $conf->{balloon};
-               vm_mon_cmd_nocheck($vmid, 'qom-set',
-                           path => "machine/peripheral/balloon0",
-                           property => "guest-stats-polling-interval",
-                           value => 2);
            }
 
            foreach my $opt (keys %$conf) {
@@ -4193,6 +4301,12 @@ sub vm_start {
                qemu_set_link_status($vmid, $opt, 0) if $nicconf->{link_down};
            }
        }
+
+       vm_mon_cmd_nocheck($vmid, 'qom-set',
+                   path => "machine/peripheral/balloon0",
+                   property => "guest-stats-polling-interval",
+                   value => 2) if (!defined($conf->{balloon}) || $conf->{balloon});
+
     });
 }
 
@@ -4310,11 +4424,11 @@ sub vm_stop_cleanup {
            my $vollist = get_vm_volumes($conf);
            PVE::Storage::deactivate_volumes($storecfg, $vollist);
        }
-       
+
        foreach my $ext (qw(mon qmp pid vnc qga)) {
            unlink "/var/run/qemu-server/${vmid}.$ext";
        }
-       
+
        vmconfig_apply_pending($vmid, $conf, $storecfg) if $apply_pending_changes;
     };
     warn $@ if $@; # avoid errors - just warn
@@ -4597,6 +4711,11 @@ sub pci_dev_group_bind_to_vfio {
 
     foreach my $pciid (@devs) {
        $pciid =~ m/^([:\.\da-f]+)$/ or die "PCI ID $pciid not valid!\n";
+
+        # pci bridges, switches or root ports are not supported
+        # they have a pci_bus subdirectory so skip them
+        next if (-e "$pcisysfs/devices/$pciid/pci_bus");
+
        my $info = pci_device_info($1);
        pci_dev_bind_to_vfio($info) || die "Cannot bind $pciid to vfio\n";
     }
@@ -4614,6 +4733,7 @@ sub print_pci_addr {
        balloon0 => { bus => 0, addr => 3 },
        watchdog => { bus => 0, addr => 4 },
        scsihw0 => { bus => 0, addr => 5 },
+       'pci.3' => { bus => 0, addr => 5 }, #can also be used for virtio-scsi-single bridge
        scsihw1 => { bus => 0, addr => 6 },
        ahci0 => { bus => 0, addr => 7 },
        qga0 => { bus => 0, addr => 8 },
@@ -4676,6 +4796,38 @@ sub print_pci_addr {
        'virtio13' => { bus => 2, addr => 8 },
        'virtio14' => { bus => 2, addr => 9 },
        'virtio15' => { bus => 2, addr => 10 },
+       'virtioscsi0' => { bus => 3, addr => 1 },
+       'virtioscsi1' => { bus => 3, addr => 2 },
+       'virtioscsi2' => { bus => 3, addr => 3 },
+       'virtioscsi3' => { bus => 3, addr => 4 },
+       'virtioscsi4' => { bus => 3, addr => 5 },
+       'virtioscsi5' => { bus => 3, addr => 6 },
+       'virtioscsi6' => { bus => 3, addr => 7 },
+       'virtioscsi7' => { bus => 3, addr => 8 },
+       'virtioscsi8' => { bus => 3, addr => 9 },
+       'virtioscsi9' => { bus => 3, addr => 10 },
+       'virtioscsi10' => { bus => 3, addr => 11 },
+       'virtioscsi11' => { bus => 3, addr => 12 },
+       'virtioscsi12' => { bus => 3, addr => 13 },
+       'virtioscsi13' => { bus => 3, addr => 14 },
+       'virtioscsi14' => { bus => 3, addr => 15 },
+       'virtioscsi15' => { bus => 3, addr => 16 },
+       'virtioscsi16' => { bus => 3, addr => 17 },
+       'virtioscsi17' => { bus => 3, addr => 18 },
+       'virtioscsi18' => { bus => 3, addr => 19 },
+       'virtioscsi19' => { bus => 3, addr => 20 },
+       'virtioscsi20' => { bus => 3, addr => 21 },
+       'virtioscsi21' => { bus => 3, addr => 22 },
+       'virtioscsi22' => { bus => 3, addr => 23 },
+       'virtioscsi23' => { bus => 3, addr => 24 },
+       'virtioscsi24' => { bus => 3, addr => 25 },
+       'virtioscsi25' => { bus => 3, addr => 26 },
+       'virtioscsi26' => { bus => 3, addr => 27 },
+       'virtioscsi27' => { bus => 3, addr => 28 },
+       'virtioscsi28' => { bus => 3, addr => 29 },
+       'virtioscsi29' => { bus => 3, addr => 30 },
+       'virtioscsi30' => { bus => 3, addr => 31 },
+
     };
 
     if (defined($devices->{$id}->{bus}) && defined($devices->{$id}->{addr})) {
@@ -5536,21 +5688,40 @@ my $snapshot_commit = sub {
 sub snapshot_rollback {
     my ($vmid, $snapname) = @_;
 
-    my $snap;
-
     my $prepare = 1;
 
     my $storecfg = PVE::Storage::config();
 
-    my $updatefn = sub {
+    my $conf = load_config($vmid);
 
-       my $conf = load_config($vmid);
+    my $get_snapshot_config = sub {
 
        die "you can't rollback if vm is a template\n" if is_template($conf);
 
-       $snap = $conf->{snapshots}->{$snapname};
+       my $res = $conf->{snapshots}->{$snapname};
 
-       die "snapshot '$snapname' does not exist\n" if !defined($snap);
+       die "snapshot '$snapname' does not exist\n" if !defined($res);
+
+       return $res;
+    };
+
+    my $snap = &$get_snapshot_config();
+
+    foreach_drive($snap, sub {
+       my ($ds, $drive) = @_;
+
+       return if drive_is_cdrom($drive);
+
+       my $volid = $drive->{file};
+
+       PVE::Storage::volume_rollback_is_possible($storecfg, $volid, $snapname);
+    });
+
+    my $updatefn = sub {
+
+       $conf = load_config($vmid);
+
+       $snap = &$get_snapshot_config();
 
        die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
            if $snap->{snapstate};
@@ -6075,6 +6246,28 @@ sub get_current_qemu_machine {
     return $current || $default || 'pc';
 }
 
+sub qemu_machine_feature_enabled {
+    my ($machine, $kvmver, $version_major, $version_minor) = @_;
+
+    my $current_major;
+    my $current_minor;
+
+    if ($machine && $machine =~ m/^(pc(-i440fx|-q35)?-(\d+)\.(\d+))/) {
+
+       $current_major = $3;
+       $current_minor = $4;
+
+    } elsif ($kvmver =~ m/^(\d+)\.(\d+)/) {
+
+       $current_major = $1;
+       $current_minor = $2;
+    }
+
+    return 1 if $current_major >= $version_major && $current_minor >= $version_minor;
+
+
+}
+
 sub lspci {
 
     my $devices = {};
@@ -6088,4 +6281,36 @@ sub lspci {
     return $devices;
 }
 
+sub vm_iothreads_list {
+    my ($vmid) = @_;
+
+    my $res = vm_mon_cmd($vmid, 'query-iothreads');
+
+    my $iothreads = {};
+    foreach my $iothread (@$res) {
+       $iothreads->{ $iothread->{id} } = $iothread->{"thread-id"};
+    }
+
+    return $iothreads;
+}
+
+sub scsihw_infos {
+    my ($conf, $drive) = @_;
+
+    my $maxdev = 0;
+
+    if ($conf->{scsihw} && ($conf->{scsihw} =~ m/^lsi/)) {
+        $maxdev = 7;
+    } elsif ($conf->{scsihw} && ($conf->{scsihw} eq 'virtio-scsi-single')) {
+        $maxdev = 1;
+    } else {
+        $maxdev = 256;
+    }
+
+    my $controller = int($drive->{index} / $maxdev);
+    my $controller_prefix = ($conf->{scsihw} && $conf->{scsihw} eq 'virtio-scsi-single') ? "virtioscsi" : "scsihw";
+
+    return ($maxdev, $controller, $controller_prefix);
+}
+
 1;