]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QMPClient.pm
qmpclient-qga : mux_input : parse qga result
[qemu-server.git] / PVE / QMPClient.pm
index cc975abe693ee868b5abac74940c606a60417ff0..51f7c185d6f5821d8c6d8bc1ef12a143518f82f9 100755 (executable)
@@ -1,10 +1,15 @@
 package PVE::QMPClient;
 
 use strict;
-#use PVE::SafeSyslog;
+use warnings;
 use PVE::QemuServer;
 use IO::Multiplex;
+use POSIX qw(EINTR EAGAIN);
 use JSON;
+use Time::HiRes qw(usleep gettimeofday tv_interval);
+use Scalar::Util qw(weaken);
+use PVE::IPCC;
+
 use Data::Dumper;
 
 # Qemu Monitor Protocol (QMP) client.
@@ -15,7 +20,7 @@ use Data::Dumper;
 # Note: kvm can onyl handle 1 connection, so we close connections asap
 
 sub new {
-    my ($class, $eventcb) = @_;
+    my ($class, $eventcb, $qga) = @_;
 
     my $mux = new IO::Multiplex;
 
@@ -29,13 +34,18 @@ sub new {
     }, $class;
 
     $self->{eventcb} = $eventcb if $eventcb;
+    $self->{qga} = $qga if $qga;
 
     $mux->set_callback_object($self);
 
+    # make sure perl doesn't believe this is a circular reference as we
+    # delete mux in DESTROY
+    weaken($mux->{_object});
+
     return $self;
 }
 
-# add a single command to the queue for later execution 
+# add a single command to the queue for later execution
 # with queue_execute()
 sub queue_cmd {
     my ($self, $vmid, $callback, $execute, %params) = @_;
@@ -72,6 +82,15 @@ sub cmd {
            $timeout = 60*60; # 1 hour
        } elsif ($cmd->{execute} =~ m/^(eject|change)/) {
            $timeout = 60; # note: cdrom mount command is slow
+       } elsif ($cmd->{execute} eq 'savevm-start' ||
+                $cmd->{execute} eq 'savevm-end' ||
+                $cmd->{execute} eq 'query-backup' ||
+                $cmd->{execute} eq 'query-block-jobs' ||
+                $cmd->{execute} eq 'backup-cancel' ||
+                $cmd->{execute} eq 'query-savevm' ||
+                $cmd->{execute} eq 'delete-drive-snapshot' ||
+                $cmd->{execute} eq 'snapshot-drive'  ) {
+           $timeout = 10*60; # 10 mins ?
        } else {
            $timeout = 3; # default
        }
@@ -81,7 +100,7 @@ sub cmd {
 
     my $cmdstr = $cmd->{execute} || '';
     die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
-       if defined($self->{errors}->{$vmid});    
+       if defined($self->{errors}->{$vmid});
 
     return $result;
 };
@@ -89,15 +108,15 @@ sub cmd {
 my $cmdid_seq = 0;
 my $next_cmdid = sub {
     $cmdid_seq++;
-    return "$$:$cmdid_seq";
+    return "$$"."0".$cmdid_seq;
 };
 
 my $close_connection = sub {
     my ($self, $vmid) = @_;
-           
+
     my $fh = $self->{fhs}->{$vmid};
     return if !$fh;
+
     delete $self->{fhs}->{$vmid};
     delete $self->{fhs_lookup}->{$fh};
 
@@ -105,17 +124,33 @@ my $close_connection = sub {
 };
 
 my $open_connection = sub {
-    my ($self, $vmid) = @_;
+    my ($self, $vmid, $timeout) = @_;
+
+    my $sname = PVE::QemuServer::qmp_socket($vmid, $self->{qga});
 
-    my $sname = PVE::QemuServer::qmp_socket($vmid);
+    $timeout = 1 if !$timeout;
 
-    my $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1) ||
-       die "unable to connect to VM $vmid socket - $!\n";
+    my $fh;
+    my $starttime = [gettimeofday];
+    my $count = 0;
+    for (;;) {
+       $count++;
+       $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1);
+       last if $fh;
+       if ($! != EINTR && $! != EAGAIN) {
+           die "unable to connect to VM $vmid socket - $!\n";
+       }
+       my $elapsed = tv_interval($starttime, [gettimeofday]);
+       if ($elapsed >= $timeout) {
+           die "unable to connect to VM $vmid socket - timeout after $count retries\n";
+       }
+       usleep(100000);
+    }
 
     $self->{fhs}->{$vmid} = $fh;
     $self->{fhs_lookup}->{$fh} = $vmid;
     $self->{mux}->add($fh);
+
     return $fh;
 };
 
@@ -123,7 +158,7 @@ my $check_queue = sub {
     my ($self) = @_;
 
     my $running = 0;
-       
+
     foreach my $vmid (keys %{$self->{queue}}) {
        my $fh = $self->{fhs}->{$vmid};
        next if !$fh;
@@ -148,12 +183,40 @@ my $check_queue = sub {
            my $cmd = $self->{current}->{$vmid} = shift @{$self->{queue}->{$vmid}};
            $cmd->{id} = &$next_cmdid();
 
-           my $qmpcmd = to_json({
-               execute => $cmd->{execute},
-               arguments => $cmd->{arguments},
-               id => $cmd->{id}});
+           my $fd = -1;
+           if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') {
+               $fd = $cmd->{arguments}->{fd};
+               delete $cmd->{arguments}->{fd};
+           }
+
+           my $qmpcmd = undef;
 
-           $self->{mux}->write($fh, $qmpcmd);
+           if($self->{qga}){
+
+               my $qmpcmdid =to_json({
+                   execute => 'guest-sync',
+                   arguments => { id => int($cmd->{id})}});
+
+               $qmpcmd = to_json({
+                   execute => $cmd->{execute},
+                   arguments => $cmd->{arguments}});
+
+               $qmpcmd = $qmpcmdid.$qmpcmd;
+
+           }else{
+
+               $qmpcmd = to_json({
+                   execute => $cmd->{execute},
+                   arguments => $cmd->{arguments},
+                   id => $cmd->{id}});
+           }
+
+           if ($fd >= 0) {
+               my $ret = PVE::IPCC::sendfd(fileno($fh), $fd, $qmpcmd);
+               die "sendfd failed" if $ret < 0;
+           } else {
+               $self->{mux}->write($fh, $qmpcmd);
+           }
        };
        if (my $err = $@) {
            $self->{errors}->{$vmid} = $err;
@@ -181,9 +244,13 @@ sub queue_execute {
        next if !scalar(@{$self->{queue}->{$vmid}}); # no commands for the VM
 
        eval {
-           my $fh = &$open_connection($self, $vmid);
-           my $cmd = { execute => 'qmp_capabilities', arguments => {} };
-           unshift @{$self->{queue}->{$vmid}}, $cmd;
+           my $fh = &$open_connection($self, $vmid, $timeout);
+
+           if(!$self->{qga}){
+               my $cmd = { execute => 'qmp_capabilities', arguments => {} };
+               unshift @{$self->{queue}->{$vmid}}, $cmd;
+           }
+
            $self->{mux}->set_timeout($fh, $timeout);
        };
        if (my $err = $@) {
@@ -211,17 +278,29 @@ sub queue_execute {
     $self->{queue} = $self->{current} = $self->{fhs} = $self->{fhs_lookup} = {};
 }
 
+sub mux_close {
+    my ($self, $mux, $fh) = @_;
+
+    my $vmid = $self->{fhs_lookup}->{$fh} || 'undef';
+    return if !defined($vmid);
+
+    $self->{errors}->{$vmid} = "client closed connection\n" if !$self->{errors}->{$vmid};
+}
+
 # mux_input is called when input is available on one of
 # the descriptors.
 sub mux_input {
     my ($self, $mux, $fh, $input) = @_;
 
-    return if $$input !~ m/}\r\n$/;
-
-    my $raw = $$input;
+    my $raw;
 
-    # Remove the input from the input buffer.
-    $$input = '';
+    if($self->{qga}){
+       return if $$input !~ s/^([^\n]+}\n[^\n]+})\n(.*)$/$2/so;
+       $raw = $1;
+    }else{
+       return if $$input !~ s/^([^\n]+})\r?\n(.*)$/$2/so;
+       $raw = $1;
+    }
 
     my $vmid = $self->{fhs_lookup}->{$fh};
     if (!$vmid) {
@@ -232,6 +311,32 @@ sub mux_input {
     eval {
        my @jsons = split("\n", $raw);
 
+       if($self->{qga}){
+
+           die "response is not complete" if @jsons != 2 ;
+
+           my $obj = from_json($jsons[0]);
+           my $cmdid = $obj->{return};
+           die "received responsed without command id\n" if !$cmdid;
+
+           my $curcmd = $self->{current}->{$vmid};
+           die "unable to lookup current command for VM $vmid\n" if !$curcmd;
+
+           delete $self->{current}->{$vmid};
+
+           if ($curcmd->{id} ne $cmdid) {
+               die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
+           }
+
+           $obj = from_json($jsons[1]);
+
+           if (my $callback = $curcmd->{callback}) {
+               &$callback($vmid, $obj);
+           }
+
+           return;
+       }
+
        foreach my $json (@jsons) {
            my $obj = from_json($json);
            next if defined($obj->{QMP}); # skip monitor greeting
@@ -257,7 +362,7 @@ sub mux_input {
            die "unable to lookup current command for VM $vmid\n" if !$curcmd;
 
            delete $self->{current}->{$vmid};
-           
+
            if ($curcmd->{id} ne $cmdid) {
                die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
            }