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