]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
1 package PVE
::QMPClient
;
7 use POSIX
qw(EINTR EAGAIN);
9 use Time
::HiRes
qw(usleep gettimeofday tv_interval);
10 use Scalar
::Util
qw(weaken);
15 # Qemu Monitor Protocol (QMP) client.
17 # This implementation uses IO::Multiplex (libio-multiplex-perl) and
18 # allows you to issue qmp commands to different VMs in parallel.
20 # Note: kvm can onyl handle 1 connection, so we close connections asap
23 my ($class, $eventcb) = @_;
25 my $mux = new IO
::Multiplex
;
29 fhs
=> {}, # $vmid => fh
30 fhs_lookup
=> {}, # $fh => $vmid
36 $self->{eventcb
} = $eventcb if $eventcb;
38 $mux->set_callback_object($self);
40 # make sure perl doesn't believe this is a circular reference as we
41 # delete mux in DESTROY
42 weaken
($mux->{_object
});
47 # add a single command to the queue for later execution
48 # with queue_execute()
50 my ($self, $vmid, $callback, $execute, %params) = @_;
53 $cmd->{execute
} = $execute;
54 $cmd->{arguments
} = \
%params;
55 $cmd->{callback
} = $callback;
57 push @{$self->{queue
}->{$vmid}}, $cmd;
60 # execute a single command
62 my ($self, $vmid, $cmd, $timeout) = @_;
67 my ($vmid, $resp) = @_;
68 $result = $resp->{'return'};
71 die "no command specified" if !($cmd && $cmd->{execute
});
73 $cmd->{callback
} = $callback;
74 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
76 $self->{queue
}->{$vmid} = [ $cmd ];
79 # hack: monitor sometime blocks
80 if ($cmd->{execute
} eq 'query-migrate') {
81 $timeout = 60*60; # 1 hour
82 } elsif ($cmd->{execute
} =~ m/^(eject|change)/) {
83 $timeout = 60; # note: cdrom mount command is slow
84 } elsif ($cmd->{execute
} eq 'savevm-start' ||
85 $cmd->{execute
} eq 'savevm-end' ||
86 $cmd->{execute
} eq 'query-backup' ||
87 $cmd->{execute
} eq 'query-block-jobs' ||
88 $cmd->{execute
} eq 'backup-cancel' ||
89 $cmd->{execute
} eq 'query-savevm' ||
90 $cmd->{execute
} eq 'delete-drive-snapshot' ||
91 $cmd->{execute
} eq 'guest-shutdown' ||
92 $cmd->{execute
} eq 'snapshot-drive' ) {
93 $timeout = 10*60; # 10 mins ?
95 $timeout = 3; # default
99 $self->queue_execute($timeout);
101 my $cmdstr = $cmd->{execute
} || '';
102 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
103 if defined($self->{errors
}->{$vmid});
109 my $cmdid_seq_qga = 0;
110 my $next_cmdid = sub {
115 return "$$"."0".$cmdid_seq_qga;
118 return "$$:$cmdid_seq";
122 my $close_connection = sub {
123 my ($self, $vmid) = @_;
125 my $fh = $self->{fhs
}->{$vmid};
128 delete $self->{fhs
}->{$vmid};
129 delete $self->{fhs_lookup
}->{$fh};
131 $self->{mux
}->close($fh);
134 my $open_connection = sub {
135 my ($self, $vmid, $timeout, $qga) = @_;
137 my $sname = PVE
::QemuServer
::qmp_socket
($vmid, $qga);
139 $timeout = 1 if !$timeout;
142 my $starttime = [gettimeofday
];
146 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
148 if ($! != EINTR
&& $! != EAGAIN
) {
149 die "unable to connect to VM $vmid socket - $!\n";
151 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
152 if ($elapsed >= $timeout) {
153 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
158 $self->{fhs
}->{$vmid} = $fh;
159 $self->{fhs_lookup
}->{$fh} = $vmid;
160 $self->{mux
}->add($fh);
165 my $check_queue = sub {
170 foreach my $vmid (keys %{$self->{queue
}}) {
171 my $fh = $self->{fhs
}->{$vmid};
174 if ($self->{errors
}->{$vmid}) {
175 &$close_connection($self, $vmid);
179 if ($self->{current
}->{$vmid}) { # command running, waiting for response
184 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
185 &$close_connection($self, $vmid);
191 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
192 $cmd->{id
} = &$next_cmdid($cmd->{qga
});
195 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
196 $fd = $cmd->{arguments
}->{fd
};
197 delete $cmd->{arguments
}->{fd
};
202 if($self->{current
}->{$vmid}->{qga
}){
204 my $qmpcmdid =to_json
({
205 execute
=> 'guest-sync',
206 arguments
=> { id
=> int($cmd->{id
})}});
209 execute
=> $cmd->{execute
},
210 arguments
=> $cmd->{arguments
}});
212 $qmpcmd = $qmpcmdid.$qmpcmd;
217 execute
=> $cmd->{execute
},
218 arguments
=> $cmd->{arguments
},
223 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
224 die "sendfd failed" if $ret < 0;
226 $self->{mux
}->write($fh, $qmpcmd);
230 $self->{errors
}->{$vmid} = $err;
236 $self->{mux
}->endloop() if !$running;
241 # execute all queued command
243 my ($self, $timeout) = @_;
245 $timeout = 3 if !$timeout;
247 $self->{current
} = {};
248 $self->{errors
} = {};
250 # open all necessary connections
251 foreach my $vmid (keys %{$self->{queue
}}) {
252 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
254 if ($self->{queue
}->{$vmid}[0]->{execute
} =~ /^guest\-+/){
255 $self->{queue
}->{$vmid}[0]->{qga
} = "1";
259 my $fh = &$open_connection($self, $vmid, $timeout, $self->{queue
}->{$vmid}[0]->{qga
});
261 if(!$self->{queue
}->{$vmid}[0]->{qga
}){
262 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
263 unshift @{$self->{queue
}->{$vmid}}, $cmd;
265 $self->{mux
}->set_timeout($fh, $timeout);
269 $self->{errors
}->{$vmid} = $err;
277 $running = &$check_queue($self);
284 # make sure we close everything
285 foreach my $vmid (keys %{$self->{fhs
}}) {
286 &$close_connection($self, $vmid);
289 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
293 my ($self, $mux, $fh) = @_;
295 my $vmid = $self->{fhs_lookup
}->{$fh} || 'undef';
296 return if !defined($vmid);
298 if(!$self->{no_answer
}){
299 $self->{errors
}->{$vmid} = "client closed connection\n" if !$self->{errors
}->{$vmid};
301 delete $self->{no_anwser
};
305 # mux_input is called when input is available on one of
308 my ($self, $mux, $fh, $input) = @_;
310 my $vmid = $self->{fhs_lookup
}->{$fh};
312 warn "internal error - unable to lookup vmid";
316 my $curcmd = $self->{current
}->{$vmid};
317 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
321 if ($curcmd->{qga
}) {
322 return if $$input !~ s/^([^\n]+}\n[^\n]+})\n(.*)$/$2/so;
325 return if $$input !~ s/^([^\n]+})\r?\n(.*)$/$2/so;
330 my @jsons = split("\n", $raw);
332 if ($curcmd->{qga
}) {
334 die "response is not complete" if @jsons != 2 ;
336 my $obj = from_json
($jsons[0]);
337 my $cmdid = $obj->{return};
338 die "received responsed without command id\n" if !$cmdid;
340 delete $self->{current
}->{$vmid};
342 if ($curcmd->{id
} ne $cmdid) {
343 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
346 $obj = from_json
($jsons[1]);
348 if (my $callback = $curcmd->{callback
}) {
349 &$callback($vmid, $obj);
355 foreach my $json (@jsons) {
356 my $obj = from_json
($json);
357 next if defined($obj->{QMP
}); # skip monitor greeting
359 if (exists($obj->{error
}->{desc
})) {
360 my $desc = $obj->{error
}->{desc
};
362 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
366 if (defined($obj->{event
})) {
367 if (my $eventcb = $self->{eventcb
}) {
373 my $cmdid = $obj->{id
};
374 die "received responsed without command id\n" if !$cmdid;
376 my $curcmd = $self->{current
}->{$vmid};
377 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
379 delete $self->{current
}->{$vmid};
381 if ($curcmd->{id
} ne $cmdid) {
382 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
385 if (my $callback = $curcmd->{callback
}) {
386 &$callback($vmid, $obj);
391 $self->{errors
}->{$vmid} = $err;
394 &$check_queue($self);
397 # This gets called every second to update player info, etc...
399 my ($self, $mux, $fh) = @_;
401 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
402 $self->{errors
}->{$vmid} = "got timeout\n";
405 &$check_queue($self);
409 my ($self, $mux, $fh, $input) = @_;
411 my $vmid = $self->{fhs_lookup
}->{$fh};
412 if(check_no_answer
($self->{current
}->{$vmid}->{execute
})){
413 my @jsons = split("\n", $$input);
415 my $obj = from_json
($jsons[0]);
417 my $cmdid = $obj->{return};
418 die "received responsed without command id\n" if !$cmdid;
420 my $curcmd = $self->{current
}->{$vmid};
421 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
423 delete $self->{current
}->{$vmid};
425 $self->{no_answer
} = 1;
429 sub check_no_answer
{
432 if ($cmd eq 'guest-shutdown'){
434 } elsif ($cmd eq 'guest-suspend-ram'){
436 } elsif ($cmd eq 'guest-suspend-disk'){
438 } elsif ($cmd eq 'guest-suspend-hybrid'){