]> git.proxmox.com Git - qemu-server.git/blame - PVE/QMPClient.pm
QMP client: remove unnecessary question mark from comment
[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' ||
b0d1a00a 124 $cmd->{execute} eq 'drive-mirror' ||
46336bd2
TL
125 $cmd->{execute} eq 'query-block-jobs' ||
126 $cmd->{execute} eq 'block-job-cancel' ||
127 $cmd->{execute} eq 'block-job-complete' ||
128 $cmd->{execute} eq 'backup-cancel' ||
129 $cmd->{execute} eq 'query-savevm' ||
50164179 130 $cmd->{execute} eq 'guest-fstrim' ||
46336bd2
TL
131 $cmd->{execute} eq 'guest-shutdown' ||
132 $cmd->{execute} eq 'blockdev-snapshot-internal-sync' ||
5674d198 133 $cmd->{execute} eq 'blockdev-snapshot-delete-internal-sync'
46336bd2 134 ) {
0d00383d 135 $timeout = 10*60; # 10 mins
14db5366 136 } else {
8174a894
TL
137 # NOTE: if you came here as user and want to change this, try using IO-Threads first
138 # which move out quite some processing of the main thread, leaving more time for QMP
139 $timeout = 5; # default
f0002f62
DM
140 }
141 }
142
c8125172 143 $self->queue_execute($timeout, 2);
7a6c2150
DM
144
145 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
146 if defined($queue_info->{error});
30a3378a
DM
147
148 return $result;
149};
150
151my $cmdid_seq = 0;
c5a07de5 152my $cmdid_seq_qga = 0;
7a6c2150 153
30a3378a 154my $next_cmdid = sub {
c5a07de5
WL
155 my ($qga) = @_;
156
157 if($qga){
158 $cmdid_seq_qga++;
159 return "$$"."0".$cmdid_seq_qga;
160 } else {
161 $cmdid_seq++;
162 return "$$:$cmdid_seq";
163 }
30a3378a
DM
164};
165
7a6c2150
DM
166my $lookup_queue_info = sub {
167 my ($self, $fh, $noerr) = @_;
558f1644 168
a022e3fd 169 my $queue_info = $self->{queue_lookup}->{$fh};
7a6c2150
DM
170 if (!$queue_info) {
171 warn "internal error - unable to lookup queue info" if !$noerr;
d1c1af4b 172 return;
7a6c2150
DM
173 }
174 return $queue_info;
175};
558f1644 176
7a6c2150
DM
177my $close_connection = sub {
178 my ($self, $queue_info) = @_;
30a3378a 179
7a6c2150
DM
180 if (my $fh = delete $queue_info->{fh}) {
181 delete $self->{queue_lookup}->{$fh};
c8125172 182 $self->{mux}->close($fh);
a022e3fd 183 }
30a3378a
DM
184};
185
186my $open_connection = sub {
7a6c2150
DM
187 my ($self, $queue_info, $timeout) = @_;
188
189 die "duplicate call to open" if defined($queue_info->{fh});
190
191 my $vmid = $queue_info->{vmid};
192 my $qga = $queue_info->{qga};
30a3378a 193
d036e418 194 my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid, $qga);
30a3378a 195
6d042176
DM
196 $timeout = 1 if !$timeout;
197
2ae10d4e
DM
198 my $fh;
199 my $starttime = [gettimeofday];
200 my $count = 0;
7a6c2150
DM
201
202 my $sotype = $qga ? 'qga' : 'qmp';
203
2ae10d4e
DM
204 for (;;) {
205 $count++;
206 $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1);
207 last if $fh;
208 if ($! != EINTR && $! != EAGAIN) {
7a6c2150 209 die "unable to connect to VM $vmid $sotype socket - $!\n";
2ae10d4e
DM
210 }
211 my $elapsed = tv_interval($starttime, [gettimeofday]);
6d042176 212 if ($elapsed >= $timeout) {
7a6c2150 213 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
2ae10d4e
DM
214 }
215 usleep(100000);
216 }
30a3378a 217
7a6c2150
DM
218 $queue_info->{fh} = $fh;
219
220 $self->{queue_lookup}->{$fh} = $queue_info;
221
30a3378a 222 $self->{mux}->add($fh);
7a6c2150 223 $self->{mux}->set_timeout($fh, $timeout);
558f1644 224
30a3378a
DM
225 return $fh;
226};
227
228my $check_queue = sub {
229 my ($self) = @_;
230
231 my $running = 0;
558f1644 232
7a6c2150
DM
233 foreach my $sname (keys %{$self->{queue_info}}) {
234 my $queue_info = $self->{queue_info}->{$sname};
235 my $fh = $queue_info->{fh};
30a3378a
DM
236 next if !$fh;
237
7a6c2150
DM
238 my $qga = $queue_info->{qga};
239
240 if ($queue_info->{error}) {
241 &$close_connection($self, $queue_info);
30a3378a
DM
242 next;
243 }
244
7a6c2150 245 if ($queue_info->{current}) { # command running, waiting for response
30a3378a
DM
246 $running++;
247 next;
248 }
249
7a6c2150
DM
250 if (!scalar(@{$queue_info->{cmds}})) { # no more commands
251 &$close_connection($self, $queue_info);
30a3378a
DM
252 next;
253 }
254
255 eval {
256
7a6c2150
DM
257 my $cmd = $queue_info->{current} = shift @{$queue_info->{cmds}};
258 $cmd->{id} = &$next_cmdid($qga);
30a3378a 259
558f1644 260 my $fd = -1;
a0e7a5d0 261 if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') {
558f1644
DM
262 $fd = $cmd->{arguments}->{fd};
263 delete $cmd->{arguments}->{fd};
264 }
265
7a6c2150 266 my $qmpcmd;
a45a14fc 267
7a6c2150 268 if ($qga) {
a45a14fc 269
a022e3fd 270 $qmpcmd = to_json({ execute => 'guest-sync-delimited',
148850f6
KT
271 arguments => { id => int($cmd->{id})}}) . "\n" .
272 to_json({ execute => $cmd->{execute}, arguments => $cmd->{arguments}}) . "\n";
a45a14fc 273
7a6c2150 274 } else {
a45a14fc
AD
275
276 $qmpcmd = to_json({
277 execute => $cmd->{execute},
278 arguments => $cmd->{arguments},
279 id => $cmd->{id}});
280 }
30a3378a 281
558f1644
DM
282 if ($fd >= 0) {
283 my $ret = PVE::IPCC::sendfd(fileno($fh), $fd, $qmpcmd);
284 die "sendfd failed" if $ret < 0;
285 } else {
286 $self->{mux}->write($fh, $qmpcmd);
287 }
30a3378a
DM
288 };
289 if (my $err = $@) {
7a6c2150 290 $queue_info->{error} = $err;
30a3378a
DM
291 } else {
292 $running++;
293 }
294 }
295
296 $self->{mux}->endloop() if !$running;
297
298 return $running;
299};
300
301# execute all queued command
c8125172 302
30a3378a 303sub queue_execute {
c8125172 304 my ($self, $timeout, $noerr) = @_;
30a3378a
DM
305
306 $timeout = 3 if !$timeout;
307
30a3378a 308 # open all necessary connections
7a6c2150
DM
309 foreach my $sname (keys %{$self->{queue_info}}) {
310 my $queue_info = $self->{queue_info}->{$sname};
311 next if !scalar(@{$queue_info->{cmds}}); # no commands
a022e3fd 312
7a6c2150
DM
313 $queue_info->{error} = undef;
314 $queue_info->{current} = undef;
c5a07de5 315
a022e3fd 316 eval {
7a6c2150 317 &$open_connection($self, $queue_info, $timeout);
c6fb6a69 318
7a6c2150
DM
319 if (!$queue_info->{qga}) {
320 my $cap_cmd = { execute => 'qmp_capabilities', arguments => {} };
321 unshift @{$queue_info->{cmds}}, $cap_cmd;
c6fb6a69 322 }
30a3378a
DM
323 };
324 if (my $err = $@) {
7a6c2150 325 $queue_info->{error} = $err;
30a3378a
DM
326 }
327 }
328
329 my $running;
330
331 for (;;) {
332
333 $running = &$check_queue($self);
334
335 last if !$running;
336
337 $self->{mux}->loop;
338 }
339
340 # make sure we close everything
c8125172 341 my $errors = '';
7a6c2150 342 foreach my $sname (keys %{$self->{queue_info}}) {
c8125172
DM
343 my $queue_info = $self->{queue_info}->{$sname};
344 &$close_connection($self, $queue_info);
345 if ($queue_info->{error}) {
346 if ($noerr) {
347 warn $queue_info->{error} if $noerr < 2;
348 } else {
349 $errors .= $queue_info->{error}
350 }
351 }
30a3378a
DM
352 }
353
7a6c2150 354 $self->{queue_info} = $self->{queue_lookup} = {};
c8125172
DM
355
356 die $errors if $errors;
30a3378a
DM
357}
358
b006e70b
DM
359sub mux_close {
360 my ($self, $mux, $fh) = @_;
361
a022e3fd 362 my $queue_info = &$lookup_queue_info($self, $fh, 1);
7a6c2150 363 return if !$queue_info;
b006e70b 364
a022e3fd 365 $queue_info->{error} = "client closed connection\n"
7a6c2150 366 if !$queue_info->{error};
b006e70b
DM
367}
368
c8125172 369# mux_input is called when input is available on one of the descriptors.
30a3378a
DM
370sub mux_input {
371 my ($self, $mux, $fh, $input) = @_;
372
a022e3fd 373 my $queue_info = &$lookup_queue_info($self, $fh);
7a6c2150
DM
374 return if !$queue_info;
375
a022e3fd
AL
376 my $sname = $queue_info->{sname};
377 my $vmid = $queue_info->{vmid};
7a6c2150 378 my $qga = $queue_info->{qga};
c5a07de5 379
7a6c2150
DM
380 my $curcmd = $queue_info->{current};
381 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
a022e3fd 382
bcfbc40b
AD
383 my $raw;
384
7a6c2150 385 if ($qga) {
5cb6fe54 386 return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so;
bcfbc40b 387 $raw = $1;
c5a07de5 388 } else {
edb52f14 389 return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so;
bcfbc40b
AD
390 $raw = $1;
391 }
30a3378a 392
30a3378a
DM
393 eval {
394 my @jsons = split("\n", $raw);
395
7a6c2150 396 if ($qga) {
bcfbc40b
AD
397
398 die "response is not complete" if @jsons != 2 ;
399
400 my $obj = from_json($jsons[0]);
c8125172 401
7a6c2150 402 my $cmdid = $obj->{'return'};
bcfbc40b 403 die "received responsed without command id\n" if !$cmdid;
5cb6fe54
DM
404
405 # skip results fro previous commands
406 return if $cmdid < $curcmd->{id};
a022e3fd 407
bcfbc40b
AD
408 if ($curcmd->{id} ne $cmdid) {
409 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
410 }
411
c8125172
DM
412 delete $queue_info->{current};
413
bcfbc40b
AD
414 $obj = from_json($jsons[1]);
415
416 if (my $callback = $curcmd->{callback}) {
417 &$callback($vmid, $obj);
418 }
419
420 return;
421 }
422
30a3378a
DM
423 foreach my $json (@jsons) {
424 my $obj = from_json($json);
425 next if defined($obj->{QMP}); # skip monitor greeting
426
427 if (exists($obj->{error}->{desc})) {
428 my $desc = $obj->{error}->{desc};
429 chomp $desc;
430 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
431 next;
432 }
433
30a3378a
DM
434 if (defined($obj->{event})) {
435 if (my $eventcb = $self->{eventcb}) {
436 &$eventcb($obj);
437 }
438 next;
439 }
440
441 my $cmdid = $obj->{id};
442 die "received responsed without command id\n" if !$cmdid;
443
30a3378a
DM
444 if ($curcmd->{id} ne $cmdid) {
445 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
446 }
447
c8125172
DM
448 delete $queue_info->{current};
449
30a3378a
DM
450 if (my $callback = $curcmd->{callback}) {
451 &$callback($vmid, $obj);
452 }
453 }
454 };
455 if (my $err = $@) {
7a6c2150 456 $queue_info->{error} = $err;
30a3378a
DM
457 }
458
459 &$check_queue($self);
460}
461
462# This gets called every second to update player info, etc...
463sub mux_timeout {
464 my ($self, $mux, $fh) = @_;
465
a022e3fd 466 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
7a6c2150 467 $queue_info->{error} = "got timeout\n";
c8125172 468 $self->{mux}->inbuffer($fh, ''); # clear to avoid warnings
30a3378a
DM
469 }
470
471 &$check_queue($self);
472}
473
1c0c1c17
WL
474sub mux_eof {
475 my ($self, $mux, $fh, $input) = @_;
c8125172 476
7a6c2150
DM
477 my $queue_info = &$lookup_queue_info($self, $fh);
478 return if !$queue_info;
1c0c1c17 479
a022e3fd
AL
480 my $sname = $queue_info->{sname};
481 my $vmid = $queue_info->{vmid};
7a6c2150 482 my $qga = $queue_info->{qga};
a022e3fd 483
7a6c2150
DM
484 my $curcmd = $queue_info->{current};
485 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
1c0c1c17 486
7a6c2150 487 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute}}) {
1c0c1c17 488
5cb6fe54 489 return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so;
1c0c1c17 490
c8125172
DM
491 my $raw = $1;
492
493 eval {
494 my $obj = from_json($raw);
1c0c1c17 495
c8125172
DM
496 my $cmdid = $obj->{'return'};
497 die "received responsed without command id\n" if !$cmdid;
7a6c2150 498
c8125172 499 delete $queue_info->{current};
7a6c2150 500
c8125172
DM
501 if (my $callback = $curcmd->{callback}) {
502 &$callback($vmid, undef);
503 }
504 };
505 if (my $err = $@) {
506 $queue_info->{error} = $err;
507 }
1c0c1c17 508
7a6c2150 509 &$close_connection($self, $queue_info);
c8125172
DM
510
511 if (scalar(@{$queue_info->{cmds}}) && !$queue_info->{error}) {
512 $queue_info->{error} = "Got EOF but command queue is not empty.\n";
513 }
1c0c1c17 514 }
1c0c1c17
WL
515}
516
56afd466
WB
517sub DESTROY {
518 my ($self) = @_;
519
520 foreach my $sname (keys %{$self->{queue_info}}) {
521 my $queue_info = $self->{queue_info}->{$sname};
522 $close_connection->($self, $queue_info);
523 }
524}
525
26f11676 5261;