]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer.pm
add virtio iothread option
[qemu-server.git] / PVE / QemuServer.pm
index c0d2de657557b28de086b2b0f574728c3f7f038b..2bd9256ecdee738de2a2c9caf173e8e4bb6740e9 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',
@@ -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', {
@@ -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);
@@ -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,7 +1143,7 @@ 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);
@@ -1431,6 +1426,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 +2397,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 +2646,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 +2669,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 +2858,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 +2881,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,11 +2902,15 @@ 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;
@@ -3099,6 +3104,10 @@ 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;
@@ -3141,6 +3150,11 @@ 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;
+       }
+
        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;
@@ -3336,15 +3350,17 @@ sub vm_deviceunplug {
         qemu_devicedelverify($vmid, $deviceid);
         qemu_drivedel($vmid, $deviceid);
    
-    } elsif ($deviceid =~ m/^(lsi)(\d+)$/) {
+    } elsif ($deviceid =~ m/^(scsihw)(\d+)$/) {
     
        qemu_devicedel($vmid, $deviceid);
+       qemu_devicedelverify($vmid, $deviceid);
     
     } elsif ($deviceid =~ m/^(scsi)(\d+)$/) {
 
         qemu_devicedel($vmid, $deviceid);
         qemu_drivedel($vmid, $deviceid);
-    
+       qemu_deletescsihw($conf, $vmid, $deviceid);  
+
     } elsif ($deviceid =~ m/^(net)(\d+)$/) {
 
         qemu_devicedel($vmid, $deviceid);
@@ -3393,6 +3409,7 @@ sub qemu_driveadd {
     my ($storecfg, $vmid, $device) = @_;
 
     my $drive = print_drive_full($storecfg, $vmid, $device);
+    $drive =~ s/\\/\\\\/g;
     my $ret = vm_human_monitor_command($vmid, "drive_add auto \"$drive\"");
 
     # If the command succeeds qemu prints: "OK"
@@ -3458,6 +3475,31 @@ sub qemu_findorcreatescsihw {
     return 1;
 }
 
+sub qemu_deletescsihw {
+    my ($conf, $vmid, $opt) = @_;
+
+    my $device = parse_drive($opt, $conf->{$opt});
+
+    my $maxdev = ($conf->{scsihw} && ($conf->{scsihw} !~ m/^lsi/)) ? 256 : 7;
+    my $controller = int($device->{index} / $maxdev);
+
+    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;
+}
+
 sub qemu_add_pci_bridge {
     my ($storecfg, $conf, $vmid, $device) = @_;
 
@@ -3754,6 +3796,7 @@ sub set_migration_caps {
 }
 
 my $fast_plug_option = {
+    'lock' => 1,
     'name' => 1,
     'onboot' => 1, 
     'shares' => 1,
@@ -3863,7 +3906,7 @@ sub vmconfig_hotplug_pending {
                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);
                }
@@ -4081,6 +4124,8 @@ sub vmconfig_update_disk {
                    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;
                }
+               
+               return 1;
            }
        }
     }
@@ -4191,10 +4236,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) {
@@ -4203,6 +4244,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});
+
     });
 }
 
@@ -5551,21 +5598,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};
@@ -6090,6 +6156,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 = {};