]> git.proxmox.com Git - qemu-server.git/blame - PVE/QMPClient.pm
schema: mention that migration with VNC clipboard is not yet supported
[qemu-server.git] / PVE / QMPClient.pm
CommitLineData
30a3378a
DM
1package PVE::QMPClient;
2
3use strict;
990fc5e2 4use warnings;
7c2d9b40 5
30a3378a
DM
6use IO::Multiplex;
7use JSON;
7c2d9b40 8use POSIX qw(EINTR EAGAIN);
b3ea07f7 9use Scalar::Util qw(weaken);
7c2d9b40 10use Time::HiRes qw(usleep gettimeofday tv_interval);
2ae10d4e 11
7c2d9b40 12use PVE::IPCC;
d036e418 13use PVE::QemuServer::Helpers;
30a3378a 14
7bd9abd2 15# QEMU Monitor Protocol (QMP) client.
30a3378a
DM
16#
17# This implementation uses IO::Multiplex (libio-multiplex-perl) and
7a6c2150 18# allows you to issue qmp and qga commands to different VMs in parallel.
30a3378a 19
f7d1505b 20# Note: qemu can only handle 1 connection, so we close connections asap
30a3378a
DM
21
22sub new {
c5a07de5 23 my ($class, $eventcb) = @_;
30a3378a 24
f7d1505b 25 my $mux = IO::Multiplex->new();
30a3378a
DM
26
27 my $self = bless {
28 mux => $mux,
7a6c2150
DM
29 queue_lookup => {}, # $fh => $queue_info
30 queue_info => {},
30a3378a
DM
31 }, $class;
32
33 $self->{eventcb} = $eventcb if $eventcb;
34
35 $mux->set_callback_object($self);
36
558f1644 37 # make sure perl doesn't believe this is a circular reference as we
b3ea07f7
DM
38 # delete mux in DESTROY
39 weaken($mux->{_object});
40
30a3378a
DM
41 return $self;
42}
43
7a6c2150
DM
44# Note: List of special QGA command. Those commands can close the connection
45# without sending a response.
46
47my $qga_allow_close_cmds = {
48 'guest-shutdown' => 1,
49 'guest-suspend-ram' => 1,
50 'guest-suspend-disk' => 1,
51 'guest-suspend-hybrid' => 1,
52};
53
54my $push_cmd_to_queue = sub {
55 my ($self, $vmid, $cmd) = @_;
56
57 my $execute = $cmd->{execute} || die "no command name specified";
58
59 my $qga = ($execute =~ /^guest\-+/) ? 1 : 0;
a022e3fd 60
d036e418 61 my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid, $qga);
7a6c2150 62
a022e3fd 63 $self->{queue_info}->{$sname} = { qga => $qga, vmid => $vmid, sname => $sname, cmds => [] }
7a6c2150
DM
64 if !$self->{queue_info}->{$sname};
65
66 push @{$self->{queue_info}->{$sname}->{cmds}}, $cmd;
67
68 return $self->{queue_info}->{$sname};
69};
70
558f1644 71# add a single command to the queue for later execution
30a3378a
DM
72# with queue_execute()
73sub queue_cmd {
74 my ($self, $vmid, $callback, $execute, %params) = @_;
75
76 my $cmd = {};
77 $cmd->{execute} = $execute;
78 $cmd->{arguments} = \%params;
79 $cmd->{callback} = $callback;
80
7a6c2150
DM
81 &$push_cmd_to_queue($self, $vmid, $cmd);
82
d1c1af4b 83 return;
30a3378a
DM
84}
85
86# execute a single command
87sub cmd {
f0002f62 88 my ($self, $vmid, $cmd, $timeout) = @_;
30a3378a
DM
89
90 my $result;
91
92 my $callback = sub {
93 my ($vmid, $resp) = @_;
94 $result = $resp->{'return'};
1928c201 95 $result = { error => $resp->{'error'} } if !defined($result) && $resp->{'error'};
30a3378a
DM
96 };
97
7a6c2150 98 die "no command specified" if !($cmd && $cmd->{execute});
f0002f62 99
30a3378a
DM
100 $cmd->{callback} = $callback;
101 $cmd->{arguments} = {} if !defined($cmd->{arguments});
102
7a6c2150 103 my $queue_info = &$push_cmd_to_queue($self, $vmid, $cmd);
30a3378a 104
f0002f62
DM
105 if (!$timeout) {
106 # hack: monitor sometime blocks
107 if ($cmd->{execute} eq 'query-migrate') {
108 $timeout = 60*60; # 1 hour
109 } elsif ($cmd->{execute} =~ m/^(eject|change)/) {
110 $timeout = 60; # note: cdrom mount command is slow
cfb7a701
TL
111 } elsif ($cmd->{execute} eq 'guest-fsfreeze-freeze') {
112 # freeze syncs all guest FS, if we kill it it stays in an unfreezable
113 # locked state with high probability, so use an generous timeout
114 $timeout = 60*60; # 1 hour
115 } elsif ($cmd->{execute} eq 'guest-fsfreeze-thaw') {
d11391ff
FE
116 # While it should return instantly or never (dead locked) for Linux guests,
117 # the variance for Windows guests can be big. And there might be hook scripts
118 # that are executed upon thaw, so use 3 minutes to be on the safe side.
119 $timeout = 3 * 60;
46336bd2
TL
120 } elsif (
121 $cmd->{execute} eq 'savevm-start' ||
122 $cmd->{execute} eq 'savevm-end' ||
123 $cmd->{execute} eq 'query-backup' ||
124 $cmd->{execute} eq 'query-block-jobs' ||
125 $cmd->{execute} eq 'block-job-cancel' ||
126 $cmd->{execute} eq 'block-job-complete' ||
127 $cmd->{execute} eq 'backup-cancel' ||
128 $cmd->{execute} eq 'query-savevm' ||
50164179 129 $cmd->{execute} eq 'guest-fstrim' ||
46336bd2
TL
130 $cmd->{execute} eq 'guest-shutdown' ||
131 $cmd->{execute} eq 'blockdev-snapshot-internal-sync' ||
5674d198 132 $cmd->{execute} eq 'blockdev-snapshot-delete-internal-sync'
46336bd2 133 ) {
9d689077 134 $timeout = 10*60; # 10 mins ?
14db5366 135 } else {
8174a894
TL
136 # NOTE: if you came here as user and want to change this, try using IO-Threads first
137 # which move out quite some processing of the main thread, leaving more time for QMP
138 $timeout = 5; # default
f0002f62
DM
139 }
140 }
141
c8125172 142 $self->queue_execute($timeout, 2);
7a6c2150
DM
143
144 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
145 if defined($queue_info->{error});
30a3378a
DM
146
147 return $result;
148};
149
150my $cmdid_seq = 0;
c5a07de5 151my $cmdid_seq_qga = 0;
7a6c2150 152
30a3378a 153my $next_cmdid = sub {
c5a07de5
WL
154 my ($qga) = @_;
155
156 if($qga){
157 $cmdid_seq_qga++;
158 return "$$"."0".$cmdid_seq_qga;
159 } else {
160 $cmdid_seq++;
161 return "$$:$cmdid_seq";
162 }
30a3378a
DM
163};
164
7a6c2150
DM
165my $lookup_queue_info = sub {
166 my ($self, $fh, $noerr) = @_;
558f1644 167
a022e3fd 168 my $queue_info = $self->{queue_lookup}->{$fh};
7a6c2150
DM
169 if (!$queue_info) {
170 warn "internal error - unable to lookup queue info" if !$noerr;
d1c1af4b 171 return;
7a6c2150
DM
172 }
173 return $queue_info;
174};
558f1644 175
7a6c2150
DM
176my $close_connection = sub {
177 my ($self, $queue_info) = @_;
30a3378a 178
7a6c2150
DM
179 if (my $fh = delete $queue_info->{fh}) {
180 delete $self->{queue_lookup}->{$fh};
c8125172 181 $self->{mux}->close($fh);
a022e3fd 182 }
30a3378a
DM
183};
184
185my $open_connection = sub {
7a6c2150
DM
186 my ($self, $queue_info, $timeout) = @_;
187
188 die "duplicate call to open" if defined($queue_info->{fh});
189
190 my $vmid = $queue_info->{vmid};
191 my $qga = $queue_info->{qga};
30a3378a 192
d036e418 193 my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid, $qga);
30a3378a 194
6d042176
DM
195 $timeout = 1 if !$timeout;
196
2ae10d4e
DM
197 my $fh;
198 my $starttime = [gettimeofday];
199 my $count = 0;
7a6c2150
DM
200
201 my $sotype = $qga ? 'qga' : 'qmp';
202
2ae10d4e
DM
203 for (;;) {
204 $count++;
205 $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1);
206 last if $fh;
207 if ($! != EINTR && $! != EAGAIN) {
7a6c2150 208 die "unable to connect to VM $vmid $sotype socket - $!\n";
2ae10d4e
DM
209 }
210 my $elapsed = tv_interval($starttime, [gettimeofday]);
6d042176 211 if ($elapsed >= $timeout) {
7a6c2150 212 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
2ae10d4e
DM
213 }
214 usleep(100000);
215 }
30a3378a 216
7a6c2150
DM
217 $queue_info->{fh} = $fh;
218
219 $self->{queue_lookup}->{$fh} = $queue_info;
220
30a3378a 221 $self->{mux}->add($fh);
7a6c2150 222 $self->{mux}->set_timeout($fh, $timeout);
558f1644 223
30a3378a
DM
224 return $fh;
225};
226
227my $check_queue = sub {
228 my ($self) = @_;
229
230 my $running = 0;
558f1644 231
7a6c2150
DM
232 foreach my $sname (keys %{$self->{queue_info}}) {
233 my $queue_info = $self->{queue_info}->{$sname};
234 my $fh = $queue_info->{fh};
30a3378a
DM
235 next if !$fh;
236
7a6c2150
DM
237 my $qga = $queue_info->{qga};
238
239 if ($queue_info->{error}) {
240 &$close_connection($self, $queue_info);
30a3378a
DM
241 next;
242 }
243
7a6c2150 244 if ($queue_info->{current}) { # command running, waiting for response
30a3378a
DM
245 $running++;
246 next;
247 }
248
7a6c2150
DM
249 if (!scalar(@{$queue_info->{cmds}})) { # no more commands
250 &$close_connection($self, $queue_info);
30a3378a
DM
251 next;
252 }
253
254 eval {
255
7a6c2150
DM
256 my $cmd = $queue_info->{current} = shift @{$queue_info->{cmds}};
257 $cmd->{id} = &$next_cmdid($qga);
30a3378a 258
558f1644 259 my $fd = -1;
a0e7a5d0 260 if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') {
558f1644
DM
261 $fd = $cmd->{arguments}->{fd};
262 delete $cmd->{arguments}->{fd};
263 }
264
7a6c2150 265 my $qmpcmd;
a45a14fc 266
7a6c2150 267 if ($qga) {
a45a14fc 268
a022e3fd 269 $qmpcmd = to_json({ execute => 'guest-sync-delimited',
148850f6
KT
270 arguments => { id => int($cmd->{id})}}) . "\n" .
271 to_json({ execute => $cmd->{execute}, arguments => $cmd->{arguments}}) . "\n";
a45a14fc 272
7a6c2150 273 } else {
a45a14fc
AD
274
275 $qmpcmd = to_json({
276 execute => $cmd->{execute},
277 arguments => $cmd->{arguments},
278 id => $cmd->{id}});
279 }
30a3378a 280
558f1644
DM
281 if ($fd >= 0) {
282 my $ret = PVE::IPCC::sendfd(fileno($fh), $fd, $qmpcmd);
283 die "sendfd failed" if $ret < 0;
284 } else {
285 $self->{mux}->write($fh, $qmpcmd);
286 }
30a3378a
DM
287 };
288 if (my $err = $@) {
7a6c2150 289 $queue_info->{error} = $err;
30a3378a
DM
290 } else {
291 $running++;
292 }
293 }
294
295 $self->{mux}->endloop() if !$running;
296
297 return $running;
298};
299
300# execute all queued command
c8125172 301
30a3378a 302sub queue_execute {
c8125172 303 my ($self, $timeout, $noerr) = @_;
30a3378a
DM
304
305 $timeout = 3 if !$timeout;
306
30a3378a 307 # open all necessary connections
7a6c2150
DM
308 foreach my $sname (keys %{$self->{queue_info}}) {
309 my $queue_info = $self->{queue_info}->{$sname};
310 next if !scalar(@{$queue_info->{cmds}}); # no commands
a022e3fd 311
7a6c2150
DM
312 $queue_info->{error} = undef;
313 $queue_info->{current} = undef;
c5a07de5 314
a022e3fd 315 eval {
7a6c2150 316 &$open_connection($self, $queue_info, $timeout);
c6fb6a69 317
7a6c2150
DM
318 if (!$queue_info->{qga}) {
319 my $cap_cmd = { execute => 'qmp_capabilities', arguments => {} };
320 unshift @{$queue_info->{cmds}}, $cap_cmd;
c6fb6a69 321 }
30a3378a
DM
322 };
323 if (my $err = $@) {
7a6c2150 324 $queue_info->{error} = $err;
30a3378a
DM
325 }
326 }
327
328 my $running;
329
330 for (;;) {
331
332 $running = &$check_queue($self);
333
334 last if !$running;
335
336 $self->{mux}->loop;
337 }
338
339 # make sure we close everything
c8125172 340 my $errors = '';
7a6c2150 341 foreach my $sname (keys %{$self->{queue_info}}) {
c8125172
DM
342 my $queue_info = $self->{queue_info}->{$sname};
343 &$close_connection($self, $queue_info);
344 if ($queue_info->{error}) {
345 if ($noerr) {
346 warn $queue_info->{error} if $noerr < 2;
347 } else {
348 $errors .= $queue_info->{error}
349 }
350 }
30a3378a
DM
351 }
352
7a6c2150 353 $self->{queue_info} = $self->{queue_lookup} = {};
c8125172
DM
354
355 die $errors if $errors;
30a3378a
DM
356}
357
b006e70b
DM
358sub mux_close {
359 my ($self, $mux, $fh) = @_;
360
a022e3fd 361 my $queue_info = &$lookup_queue_info($self, $fh, 1);
7a6c2150 362 return if !$queue_info;
b006e70b 363
a022e3fd 364 $queue_info->{error} = "client closed connection\n"
7a6c2150 365 if !$queue_info->{error};
b006e70b
DM
366}
367
c8125172 368# mux_input is called when input is available on one of the descriptors.
30a3378a
DM
369sub mux_input {
370 my ($self, $mux, $fh, $input) = @_;
371
a022e3fd 372 my $queue_info = &$lookup_queue_info($self, $fh);
7a6c2150
DM
373 return if !$queue_info;
374
a022e3fd
AL
375 my $sname = $queue_info->{sname};
376 my $vmid = $queue_info->{vmid};
7a6c2150 377 my $qga = $queue_info->{qga};
c5a07de5 378
7a6c2150
DM
379 my $curcmd = $queue_info->{current};
380 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
a022e3fd 381
bcfbc40b
AD
382 my $raw;
383
7a6c2150 384 if ($qga) {
5cb6fe54 385 return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so;
bcfbc40b 386 $raw = $1;
c5a07de5 387 } else {
edb52f14 388 return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so;
bcfbc40b
AD
389 $raw = $1;
390 }
30a3378a 391
30a3378a
DM
392 eval {
393 my @jsons = split("\n", $raw);
394
7a6c2150 395 if ($qga) {
bcfbc40b
AD
396
397 die "response is not complete" if @jsons != 2 ;
398
399 my $obj = from_json($jsons[0]);
c8125172 400
7a6c2150 401 my $cmdid = $obj->{'return'};
bcfbc40b 402 die "received responsed without command id\n" if !$cmdid;
5cb6fe54
DM
403
404 # skip results fro previous commands
405 return if $cmdid < $curcmd->{id};
a022e3fd 406
bcfbc40b
AD
407 if ($curcmd->{id} ne $cmdid) {
408 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
409 }
410
c8125172
DM
411 delete $queue_info->{current};
412
bcfbc40b
AD
413 $obj = from_json($jsons[1]);
414
415 if (my $callback = $curcmd->{callback}) {
416 &$callback($vmid, $obj);
417 }
418
419 return;
420 }
421
30a3378a
DM
422 foreach my $json (@jsons) {
423 my $obj = from_json($json);
424 next if defined($obj->{QMP}); # skip monitor greeting
425
426 if (exists($obj->{error}->{desc})) {
427 my $desc = $obj->{error}->{desc};
428 chomp $desc;
429 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
430 next;
431 }
432
30a3378a
DM
433 if (defined($obj->{event})) {
434 if (my $eventcb = $self->{eventcb}) {
435 &$eventcb($obj);
436 }
437 next;
438 }
439
440 my $cmdid = $obj->{id};
441 die "received responsed without command id\n" if !$cmdid;
442
30a3378a
DM
443 if ($curcmd->{id} ne $cmdid) {
444 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
445 }
446
c8125172
DM
447 delete $queue_info->{current};
448
30a3378a
DM
449 if (my $callback = $curcmd->{callback}) {
450 &$callback($vmid, $obj);
451 }
452 }
453 };
454 if (my $err = $@) {
7a6c2150 455 $queue_info->{error} = $err;
30a3378a
DM
456 }
457
458 &$check_queue($self);
459}
460
461# This gets called every second to update player info, etc...
462sub mux_timeout {
463 my ($self, $mux, $fh) = @_;
464
a022e3fd 465 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
7a6c2150 466 $queue_info->{error} = "got timeout\n";
c8125172 467 $self->{mux}->inbuffer($fh, ''); # clear to avoid warnings
30a3378a
DM
468 }
469
470 &$check_queue($self);
471}
472
1c0c1c17
WL
473sub mux_eof {
474 my ($self, $mux, $fh, $input) = @_;
c8125172 475
7a6c2150
DM
476 my $queue_info = &$lookup_queue_info($self, $fh);
477 return if !$queue_info;
1c0c1c17 478
a022e3fd
AL
479 my $sname = $queue_info->{sname};
480 my $vmid = $queue_info->{vmid};
7a6c2150 481 my $qga = $queue_info->{qga};
a022e3fd 482
7a6c2150
DM
483 my $curcmd = $queue_info->{current};
484 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
1c0c1c17 485
7a6c2150 486 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute}}) {
1c0c1c17 487
5cb6fe54 488 return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so;
1c0c1c17 489
c8125172
DM
490 my $raw = $1;
491
492 eval {
493 my $obj = from_json($raw);
1c0c1c17 494
c8125172
DM
495 my $cmdid = $obj->{'return'};
496 die "received responsed without command id\n" if !$cmdid;
7a6c2150 497
c8125172 498 delete $queue_info->{current};
7a6c2150 499
c8125172
DM
500 if (my $callback = $curcmd->{callback}) {
501 &$callback($vmid, undef);
502 }
503 };
504 if (my $err = $@) {
505 $queue_info->{error} = $err;
506 }
1c0c1c17 507
7a6c2150 508 &$close_connection($self, $queue_info);
c8125172
DM
509
510 if (scalar(@{$queue_info->{cmds}}) && !$queue_info->{error}) {
511 $queue_info->{error} = "Got EOF but command queue is not empty.\n";
512 }
1c0c1c17 513 }
1c0c1c17
WL
514}
515
56afd466
WB
516sub DESTROY {
517 my ($self) = @_;
518
519 foreach my $sname (keys %{$self->{queue_info}}) {
520 my $queue_info = $self->{queue_info}->{$sname};
521 $close_connection->($self, $queue_info);
522 }
523}
524
26f11676 5251;