]> git.proxmox.com Git - qemu-server.git/blame - PVE/QMPClient.pm
qmpclient: now if the QMP command starts with guest-+ , it will bind dynamicly to...
[qemu-server.git] / PVE / QMPClient.pm
CommitLineData
30a3378a
DM
1package PVE::QMPClient;
2
3use strict;
990fc5e2 4use warnings;
30a3378a
DM
5use PVE::QemuServer;
6use IO::Multiplex;
2ae10d4e 7use POSIX qw(EINTR EAGAIN);
30a3378a 8use JSON;
2ae10d4e 9use Time::HiRes qw(usleep gettimeofday tv_interval);
b3ea07f7 10use Scalar::Util qw(weaken);
558f1644 11use PVE::IPCC;
2ae10d4e 12
30a3378a
DM
13use Data::Dumper;
14
15# Qemu Monitor Protocol (QMP) client.
16#
17# This implementation uses IO::Multiplex (libio-multiplex-perl) and
18# allows you to issue qmp commands to different VMs in parallel.
19
20# Note: kvm can onyl handle 1 connection, so we close connections asap
21
22sub new {
c5a07de5 23 my ($class, $eventcb) = @_;
30a3378a
DM
24
25 my $mux = new IO::Multiplex;
26
27 my $self = bless {
28 mux => $mux,
29 fhs => {}, # $vmid => fh
30 fhs_lookup => {}, # $fh => $vmid
31 queue => {},
32 current => {},
33 errors => {},
34 }, $class;
35
36 $self->{eventcb} = $eventcb if $eventcb;
37
38 $mux->set_callback_object($self);
39
558f1644 40 # make sure perl doesn't believe this is a circular reference as we
b3ea07f7
DM
41 # delete mux in DESTROY
42 weaken($mux->{_object});
43
30a3378a
DM
44 return $self;
45}
46
558f1644 47# add a single command to the queue for later execution
30a3378a
DM
48# with queue_execute()
49sub queue_cmd {
50 my ($self, $vmid, $callback, $execute, %params) = @_;
51
52 my $cmd = {};
53 $cmd->{execute} = $execute;
54 $cmd->{arguments} = \%params;
55 $cmd->{callback} = $callback;
56
57 push @{$self->{queue}->{$vmid}}, $cmd;
58}
59
60# execute a single command
61sub cmd {
f0002f62 62 my ($self, $vmid, $cmd, $timeout) = @_;
30a3378a
DM
63
64 my $result;
65
66 my $callback = sub {
67 my ($vmid, $resp) = @_;
68 $result = $resp->{'return'};
69 };
70
f0002f62
DM
71 die "no command specified" if !($cmd && $cmd->{execute});
72
30a3378a
DM
73 $cmd->{callback} = $callback;
74 $cmd->{arguments} = {} if !defined($cmd->{arguments});
75
76 $self->{queue}->{$vmid} = [ $cmd ];
77
f0002f62
DM
78 if (!$timeout) {
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
9dcf4909
DM
84 } elsif ($cmd->{execute} eq 'savevm-start' ||
85 $cmd->{execute} eq 'savevm-end' ||
558f1644 86 $cmd->{execute} eq 'query-backup' ||
4fca0153 87 $cmd->{execute} eq 'query-block-jobs' ||
fc97ae27 88 $cmd->{execute} eq 'backup-cancel' ||
9dcf4909 89 $cmd->{execute} eq 'query-savevm' ||
9d689077
DM
90 $cmd->{execute} eq 'delete-drive-snapshot' ||
91 $cmd->{execute} eq 'snapshot-drive' ) {
92 $timeout = 10*60; # 10 mins ?
14db5366
DM
93 } else {
94 $timeout = 3; # default
f0002f62
DM
95 }
96 }
97
98 $self->queue_execute($timeout);
30a3378a
DM
99
100 my $cmdstr = $cmd->{execute} || '';
101 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
558f1644 102 if defined($self->{errors}->{$vmid});
30a3378a
DM
103
104 return $result;
105};
106
107my $cmdid_seq = 0;
c5a07de5 108my $cmdid_seq_qga = 0;
30a3378a 109my $next_cmdid = sub {
c5a07de5
WL
110 my ($qga) = @_;
111
112 if($qga){
113 $cmdid_seq_qga++;
114 return "$$"."0".$cmdid_seq_qga;
115 } else {
116 $cmdid_seq++;
117 return "$$:$cmdid_seq";
118 }
30a3378a
DM
119};
120
121my $close_connection = sub {
122 my ($self, $vmid) = @_;
558f1644 123
30a3378a
DM
124 my $fh = $self->{fhs}->{$vmid};
125 return if !$fh;
558f1644 126
30a3378a
DM
127 delete $self->{fhs}->{$vmid};
128 delete $self->{fhs_lookup}->{$fh};
129
130 $self->{mux}->close($fh);
30a3378a
DM
131};
132
133my $open_connection = sub {
c5a07de5 134 my ($self, $vmid, $timeout, $qga) = @_;
30a3378a 135
c5a07de5 136 my $sname = PVE::QemuServer::qmp_socket($vmid, $qga);
30a3378a 137
6d042176
DM
138 $timeout = 1 if !$timeout;
139
2ae10d4e
DM
140 my $fh;
141 my $starttime = [gettimeofday];
142 my $count = 0;
143 for (;;) {
144 $count++;
145 $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1);
146 last if $fh;
147 if ($! != EINTR && $! != EAGAIN) {
148 die "unable to connect to VM $vmid socket - $!\n";
149 }
150 my $elapsed = tv_interval($starttime, [gettimeofday]);
6d042176 151 if ($elapsed >= $timeout) {
2ae10d4e
DM
152 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
153 }
154 usleep(100000);
155 }
30a3378a 156
30a3378a
DM
157 $self->{fhs}->{$vmid} = $fh;
158 $self->{fhs_lookup}->{$fh} = $vmid;
159 $self->{mux}->add($fh);
558f1644 160
30a3378a
DM
161 return $fh;
162};
163
164my $check_queue = sub {
165 my ($self) = @_;
166
167 my $running = 0;
558f1644 168
30a3378a
DM
169 foreach my $vmid (keys %{$self->{queue}}) {
170 my $fh = $self->{fhs}->{$vmid};
171 next if !$fh;
172
173 if ($self->{errors}->{$vmid}) {
174 &$close_connection($self, $vmid);
175 next;
176 }
177
178 if ($self->{current}->{$vmid}) { # command running, waiting for response
179 $running++;
180 next;
181 }
182
183 if (!scalar(@{$self->{queue}->{$vmid}})) { # no more commands for the VM
184 &$close_connection($self, $vmid);
185 next;
186 }
187
188 eval {
189
190 my $cmd = $self->{current}->{$vmid} = shift @{$self->{queue}->{$vmid}};
c5a07de5 191 $cmd->{id} = &$next_cmdid($cmd->{qga});
30a3378a 192
558f1644 193 my $fd = -1;
a0e7a5d0 194 if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') {
558f1644
DM
195 $fd = $cmd->{arguments}->{fd};
196 delete $cmd->{arguments}->{fd};
197 }
198
a45a14fc
AD
199 my $qmpcmd = undef;
200
c5a07de5 201 if($self->{current}->{$vmid}->{qga}){
a45a14fc
AD
202
203 my $qmpcmdid =to_json({
204 execute => 'guest-sync',
205 arguments => { id => int($cmd->{id})}});
206
207 $qmpcmd = to_json({
208 execute => $cmd->{execute},
209 arguments => $cmd->{arguments}});
210
211 $qmpcmd = $qmpcmdid.$qmpcmd;
212
213 }else{
214
215 $qmpcmd = to_json({
216 execute => $cmd->{execute},
217 arguments => $cmd->{arguments},
218 id => $cmd->{id}});
219 }
30a3378a 220
558f1644
DM
221 if ($fd >= 0) {
222 my $ret = PVE::IPCC::sendfd(fileno($fh), $fd, $qmpcmd);
223 die "sendfd failed" if $ret < 0;
224 } else {
225 $self->{mux}->write($fh, $qmpcmd);
226 }
30a3378a
DM
227 };
228 if (my $err = $@) {
229 $self->{errors}->{$vmid} = $err;
30a3378a
DM
230 } else {
231 $running++;
232 }
233 }
234
235 $self->{mux}->endloop() if !$running;
236
237 return $running;
238};
239
240# execute all queued command
241sub queue_execute {
242 my ($self, $timeout) = @_;
243
244 $timeout = 3 if !$timeout;
245
30a3378a
DM
246 $self->{current} = {};
247 $self->{errors} = {};
248
249 # open all necessary connections
250 foreach my $vmid (keys %{$self->{queue}}) {
251 next if !scalar(@{$self->{queue}->{$vmid}}); # no commands for the VM
252
c5a07de5
WL
253 if ($self->{queue}->{$vmid}[0]->{execute} =~ /^guest\-+/){
254 $self->{queue}->{$vmid}[0]->{qga} = "1";
255 }
256
257 eval {
258 my $fh = &$open_connection($self, $vmid, $timeout, $self->{queue}->{$vmid}[0]->{qga});
c6fb6a69 259
c5a07de5 260 if(!$self->{queue}->{$vmid}[0]->{qga}){
c6fb6a69
AD
261 my $cmd = { execute => 'qmp_capabilities', arguments => {} };
262 unshift @{$self->{queue}->{$vmid}}, $cmd;
263 }
264
30a3378a
DM
265 $self->{mux}->set_timeout($fh, $timeout);
266 };
267 if (my $err = $@) {
268 warn $err;
269 $self->{errors}->{$vmid} = $err;
270 }
271 }
272
273 my $running;
274
275 for (;;) {
276
277 $running = &$check_queue($self);
278
279 last if !$running;
280
281 $self->{mux}->loop;
282 }
283
284 # make sure we close everything
285 foreach my $vmid (keys %{$self->{fhs}}) {
286 &$close_connection($self, $vmid);
287 }
288
289 $self->{queue} = $self->{current} = $self->{fhs} = $self->{fhs_lookup} = {};
30a3378a
DM
290}
291
b006e70b
DM
292sub mux_close {
293 my ($self, $mux, $fh) = @_;
294
295 my $vmid = $self->{fhs_lookup}->{$fh} || 'undef';
296 return if !defined($vmid);
297
298 $self->{errors}->{$vmid} = "client closed connection\n" if !$self->{errors}->{$vmid};
299}
300
30a3378a
DM
301# mux_input is called when input is available on one of
302# the descriptors.
303sub mux_input {
304 my ($self, $mux, $fh, $input) = @_;
305
c5a07de5
WL
306 my $vmid = $self->{fhs_lookup}->{$fh};
307 if (!$vmid) {
308 warn "internal error - unable to lookup vmid";
309 return;
310 }
311
312 my $curcmd = $self->{current}->{$vmid};
313 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
314
bcfbc40b
AD
315 my $raw;
316
c5a07de5 317 if ($curcmd->{qga}) {
bcfbc40b
AD
318 return if $$input !~ s/^([^\n]+}\n[^\n]+})\n(.*)$/$2/so;
319 $raw = $1;
c5a07de5 320 } else {
bcfbc40b
AD
321 return if $$input !~ s/^([^\n]+})\r?\n(.*)$/$2/so;
322 $raw = $1;
323 }
30a3378a 324
30a3378a
DM
325 eval {
326 my @jsons = split("\n", $raw);
327
c5a07de5 328 if ($curcmd->{qga}) {
bcfbc40b
AD
329
330 die "response is not complete" if @jsons != 2 ;
331
332 my $obj = from_json($jsons[0]);
333 my $cmdid = $obj->{return};
334 die "received responsed without command id\n" if !$cmdid;
335
bcfbc40b
AD
336 delete $self->{current}->{$vmid};
337
338 if ($curcmd->{id} ne $cmdid) {
339 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
340 }
341
342 $obj = from_json($jsons[1]);
343
344 if (my $callback = $curcmd->{callback}) {
345 &$callback($vmid, $obj);
346 }
347
348 return;
349 }
350
30a3378a
DM
351 foreach my $json (@jsons) {
352 my $obj = from_json($json);
353 next if defined($obj->{QMP}); # skip monitor greeting
354
355 if (exists($obj->{error}->{desc})) {
356 my $desc = $obj->{error}->{desc};
357 chomp $desc;
358 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
359 next;
360 }
361
30a3378a
DM
362 if (defined($obj->{event})) {
363 if (my $eventcb = $self->{eventcb}) {
364 &$eventcb($obj);
365 }
366 next;
367 }
368
369 my $cmdid = $obj->{id};
370 die "received responsed without command id\n" if !$cmdid;
371
372 my $curcmd = $self->{current}->{$vmid};
373 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
374
375 delete $self->{current}->{$vmid};
558f1644 376
30a3378a
DM
377 if ($curcmd->{id} ne $cmdid) {
378 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
379 }
380
381 if (my $callback = $curcmd->{callback}) {
382 &$callback($vmid, $obj);
383 }
384 }
385 };
386 if (my $err = $@) {
387 $self->{errors}->{$vmid} = $err;
388 }
389
390 &$check_queue($self);
391}
392
393# This gets called every second to update player info, etc...
394sub mux_timeout {
395 my ($self, $mux, $fh) = @_;
396
397 if (my $vmid = $self->{fhs_lookup}->{$fh}) {
30a3378a
DM
398 $self->{errors}->{$vmid} = "got timeout\n";
399 }
400
401 &$check_queue($self);
402}
403
26f11676 4041;