]> git.proxmox.com Git - qemu-server.git/blame - PVE/QMPClient.pm
nbd mirror: no need applying full regex on volid string anymore
[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
7a6c2150 18# allows you to issue qmp and qga commands to different VMs in parallel.
30a3378a 19
7a6c2150 20# Note: qemu can onyl handle 1 connection, so we close connections asap
30a3378a
DM
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,
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;
60
61 my $sname = PVE::QemuServer::qmp_socket($vmid, $qga);
62
63 $self->{queue_info}->{$sname} = { qga => $qga, vmid => $vmid, sname => $sname, cmds => [] }
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
83 return undef;
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'};
95 };
96
7a6c2150 97 die "no command specified" if !($cmd && $cmd->{execute});
f0002f62 98
30a3378a
DM
99 $cmd->{callback} = $callback;
100 $cmd->{arguments} = {} if !defined($cmd->{arguments});
101
7a6c2150 102 my $queue_info = &$push_cmd_to_queue($self, $vmid, $cmd);
30a3378a 103
f0002f62
DM
104 if (!$timeout) {
105 # hack: monitor sometime blocks
106 if ($cmd->{execute} eq 'query-migrate') {
107 $timeout = 60*60; # 1 hour
108 } elsif ($cmd->{execute} =~ m/^(eject|change)/) {
109 $timeout = 60; # note: cdrom mount command is slow
cfb7a701
TL
110 } elsif ($cmd->{execute} eq 'guest-fsfreeze-freeze') {
111 # freeze syncs all guest FS, if we kill it it stays in an unfreezable
112 # locked state with high probability, so use an generous timeout
113 $timeout = 60*60; # 1 hour
114 } elsif ($cmd->{execute} eq 'guest-fsfreeze-thaw') {
115 # thaw has no possible long blocking actions, either it returns
116 # instantly or never (dead locked)
5cb6fe54 117 $timeout = 10;
9dcf4909
DM
118 } elsif ($cmd->{execute} eq 'savevm-start' ||
119 $cmd->{execute} eq 'savevm-end' ||
558f1644 120 $cmd->{execute} eq 'query-backup' ||
4fca0153 121 $cmd->{execute} eq 'query-block-jobs' ||
a9e7997e
AD
122 $cmd->{execute} eq 'block-job-cancel' ||
123 $cmd->{execute} eq 'block-job-complete' ||
fc97ae27 124 $cmd->{execute} eq 'backup-cancel' ||
9dcf4909 125 $cmd->{execute} eq 'query-savevm' ||
1c0c1c17
WL
126 $cmd->{execute} eq 'delete-drive-snapshot' ||
127 $cmd->{execute} eq 'guest-shutdown' ||
9d689077
DM
128 $cmd->{execute} eq 'snapshot-drive' ) {
129 $timeout = 10*60; # 10 mins ?
14db5366
DM
130 } else {
131 $timeout = 3; # default
f0002f62
DM
132 }
133 }
134
c8125172 135 $self->queue_execute($timeout, 2);
7a6c2150
DM
136
137 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
138 if defined($queue_info->{error});
30a3378a
DM
139
140 return $result;
141};
142
143my $cmdid_seq = 0;
c5a07de5 144my $cmdid_seq_qga = 0;
7a6c2150 145
30a3378a 146my $next_cmdid = sub {
c5a07de5
WL
147 my ($qga) = @_;
148
149 if($qga){
150 $cmdid_seq_qga++;
151 return "$$"."0".$cmdid_seq_qga;
152 } else {
153 $cmdid_seq++;
154 return "$$:$cmdid_seq";
155 }
30a3378a
DM
156};
157
7a6c2150
DM
158my $lookup_queue_info = sub {
159 my ($self, $fh, $noerr) = @_;
558f1644 160
7a6c2150
DM
161 my $queue_info = $self->{queue_lookup}->{$fh};
162 if (!$queue_info) {
163 warn "internal error - unable to lookup queue info" if !$noerr;
164 return undef;
165 }
166 return $queue_info;
167};
558f1644 168
7a6c2150
DM
169my $close_connection = sub {
170 my ($self, $queue_info) = @_;
30a3378a 171
7a6c2150
DM
172 if (my $fh = delete $queue_info->{fh}) {
173 delete $self->{queue_lookup}->{$fh};
c8125172 174 $self->{mux}->close($fh);
7a6c2150 175 }
30a3378a
DM
176};
177
178my $open_connection = sub {
7a6c2150
DM
179 my ($self, $queue_info, $timeout) = @_;
180
181 die "duplicate call to open" if defined($queue_info->{fh});
182
183 my $vmid = $queue_info->{vmid};
184 my $qga = $queue_info->{qga};
30a3378a 185
c5a07de5 186 my $sname = PVE::QemuServer::qmp_socket($vmid, $qga);
30a3378a 187
6d042176
DM
188 $timeout = 1 if !$timeout;
189
2ae10d4e
DM
190 my $fh;
191 my $starttime = [gettimeofday];
192 my $count = 0;
7a6c2150
DM
193
194 my $sotype = $qga ? 'qga' : 'qmp';
195
2ae10d4e
DM
196 for (;;) {
197 $count++;
198 $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1);
199 last if $fh;
200 if ($! != EINTR && $! != EAGAIN) {
7a6c2150 201 die "unable to connect to VM $vmid $sotype socket - $!\n";
2ae10d4e
DM
202 }
203 my $elapsed = tv_interval($starttime, [gettimeofday]);
6d042176 204 if ($elapsed >= $timeout) {
7a6c2150 205 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
2ae10d4e
DM
206 }
207 usleep(100000);
208 }
30a3378a 209
7a6c2150
DM
210 $queue_info->{fh} = $fh;
211
212 $self->{queue_lookup}->{$fh} = $queue_info;
213
30a3378a 214 $self->{mux}->add($fh);
7a6c2150 215 $self->{mux}->set_timeout($fh, $timeout);
558f1644 216
30a3378a
DM
217 return $fh;
218};
219
220my $check_queue = sub {
221 my ($self) = @_;
222
223 my $running = 0;
558f1644 224
7a6c2150
DM
225 foreach my $sname (keys %{$self->{queue_info}}) {
226 my $queue_info = $self->{queue_info}->{$sname};
227 my $fh = $queue_info->{fh};
30a3378a
DM
228 next if !$fh;
229
7a6c2150
DM
230 my $qga = $queue_info->{qga};
231
232 if ($queue_info->{error}) {
233 &$close_connection($self, $queue_info);
30a3378a
DM
234 next;
235 }
236
7a6c2150 237 if ($queue_info->{current}) { # command running, waiting for response
30a3378a
DM
238 $running++;
239 next;
240 }
241
7a6c2150
DM
242 if (!scalar(@{$queue_info->{cmds}})) { # no more commands
243 &$close_connection($self, $queue_info);
30a3378a
DM
244 next;
245 }
246
247 eval {
248
7a6c2150
DM
249 my $cmd = $queue_info->{current} = shift @{$queue_info->{cmds}};
250 $cmd->{id} = &$next_cmdid($qga);
30a3378a 251
558f1644 252 my $fd = -1;
a0e7a5d0 253 if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') {
558f1644
DM
254 $fd = $cmd->{arguments}->{fd};
255 delete $cmd->{arguments}->{fd};
256 }
257
7a6c2150 258 my $qmpcmd;
a45a14fc 259
7a6c2150 260 if ($qga) {
a45a14fc 261
5cb6fe54
DM
262 $qmpcmd = to_json({ execute => 'guest-sync-delimited',
263 arguments => { id => int($cmd->{id})}}) .
7a6c2150 264 to_json({ execute => $cmd->{execute}, arguments => $cmd->{arguments}});
a45a14fc 265
7a6c2150 266 } else {
a45a14fc
AD
267
268 $qmpcmd = to_json({
269 execute => $cmd->{execute},
270 arguments => $cmd->{arguments},
271 id => $cmd->{id}});
272 }
30a3378a 273
558f1644
DM
274 if ($fd >= 0) {
275 my $ret = PVE::IPCC::sendfd(fileno($fh), $fd, $qmpcmd);
276 die "sendfd failed" if $ret < 0;
277 } else {
278 $self->{mux}->write($fh, $qmpcmd);
279 }
30a3378a
DM
280 };
281 if (my $err = $@) {
7a6c2150 282 $queue_info->{error} = $err;
30a3378a
DM
283 } else {
284 $running++;
285 }
286 }
287
288 $self->{mux}->endloop() if !$running;
289
290 return $running;
291};
292
293# execute all queued command
c8125172 294
30a3378a 295sub queue_execute {
c8125172 296 my ($self, $timeout, $noerr) = @_;
30a3378a
DM
297
298 $timeout = 3 if !$timeout;
299
30a3378a 300 # open all necessary connections
7a6c2150
DM
301 foreach my $sname (keys %{$self->{queue_info}}) {
302 my $queue_info = $self->{queue_info}->{$sname};
303 next if !scalar(@{$queue_info->{cmds}}); # no commands
304
305 $queue_info->{error} = undef;
306 $queue_info->{current} = undef;
c5a07de5
WL
307
308 eval {
7a6c2150 309 &$open_connection($self, $queue_info, $timeout);
c6fb6a69 310
7a6c2150
DM
311 if (!$queue_info->{qga}) {
312 my $cap_cmd = { execute => 'qmp_capabilities', arguments => {} };
313 unshift @{$queue_info->{cmds}}, $cap_cmd;
c6fb6a69 314 }
30a3378a
DM
315 };
316 if (my $err = $@) {
7a6c2150 317 $queue_info->{error} = $err;
30a3378a
DM
318 }
319 }
320
321 my $running;
322
323 for (;;) {
324
325 $running = &$check_queue($self);
326
327 last if !$running;
328
329 $self->{mux}->loop;
330 }
331
332 # make sure we close everything
c8125172 333 my $errors = '';
7a6c2150 334 foreach my $sname (keys %{$self->{queue_info}}) {
c8125172
DM
335 my $queue_info = $self->{queue_info}->{$sname};
336 &$close_connection($self, $queue_info);
337 if ($queue_info->{error}) {
338 if ($noerr) {
339 warn $queue_info->{error} if $noerr < 2;
340 } else {
341 $errors .= $queue_info->{error}
342 }
343 }
30a3378a
DM
344 }
345
7a6c2150 346 $self->{queue_info} = $self->{queue_lookup} = {};
c8125172
DM
347
348 die $errors if $errors;
30a3378a
DM
349}
350
b006e70b
DM
351sub mux_close {
352 my ($self, $mux, $fh) = @_;
353
7a6c2150
DM
354 my $queue_info = &$lookup_queue_info($self, $fh, 1);
355 return if !$queue_info;
b006e70b 356
7a6c2150
DM
357 $queue_info->{error} = "client closed connection\n"
358 if !$queue_info->{error};
b006e70b
DM
359}
360
c8125172 361# mux_input is called when input is available on one of the descriptors.
30a3378a
DM
362sub mux_input {
363 my ($self, $mux, $fh, $input) = @_;
364
7a6c2150
DM
365 my $queue_info = &$lookup_queue_info($self, $fh);
366 return if !$queue_info;
367
368 my $sname = $queue_info->{sname};
369 my $vmid = $queue_info->{vmid};
370 my $qga = $queue_info->{qga};
c5a07de5 371
7a6c2150
DM
372 my $curcmd = $queue_info->{current};
373 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
374
bcfbc40b
AD
375 my $raw;
376
7a6c2150 377 if ($qga) {
5cb6fe54 378 return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so;
bcfbc40b 379 $raw = $1;
c5a07de5 380 } else {
edb52f14 381 return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so;
bcfbc40b
AD
382 $raw = $1;
383 }
30a3378a 384
30a3378a
DM
385 eval {
386 my @jsons = split("\n", $raw);
387
7a6c2150 388 if ($qga) {
bcfbc40b
AD
389
390 die "response is not complete" if @jsons != 2 ;
391
392 my $obj = from_json($jsons[0]);
c8125172 393
7a6c2150 394 my $cmdid = $obj->{'return'};
bcfbc40b 395 die "received responsed without command id\n" if !$cmdid;
5cb6fe54
DM
396
397 # skip results fro previous commands
398 return if $cmdid < $curcmd->{id};
7a6c2150 399
bcfbc40b
AD
400 if ($curcmd->{id} ne $cmdid) {
401 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
402 }
403
c8125172
DM
404 delete $queue_info->{current};
405
bcfbc40b
AD
406 $obj = from_json($jsons[1]);
407
408 if (my $callback = $curcmd->{callback}) {
409 &$callback($vmid, $obj);
410 }
411
412 return;
413 }
414
30a3378a
DM
415 foreach my $json (@jsons) {
416 my $obj = from_json($json);
417 next if defined($obj->{QMP}); # skip monitor greeting
418
419 if (exists($obj->{error}->{desc})) {
420 my $desc = $obj->{error}->{desc};
421 chomp $desc;
422 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
423 next;
424 }
425
30a3378a
DM
426 if (defined($obj->{event})) {
427 if (my $eventcb = $self->{eventcb}) {
428 &$eventcb($obj);
429 }
430 next;
431 }
432
433 my $cmdid = $obj->{id};
434 die "received responsed without command id\n" if !$cmdid;
435
30a3378a
DM
436 if ($curcmd->{id} ne $cmdid) {
437 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
438 }
439
c8125172
DM
440 delete $queue_info->{current};
441
30a3378a
DM
442 if (my $callback = $curcmd->{callback}) {
443 &$callback($vmid, $obj);
444 }
445 }
446 };
447 if (my $err = $@) {
7a6c2150 448 $queue_info->{error} = $err;
30a3378a
DM
449 }
450
451 &$check_queue($self);
452}
453
454# This gets called every second to update player info, etc...
455sub mux_timeout {
456 my ($self, $mux, $fh) = @_;
457
7a6c2150
DM
458 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
459 $queue_info->{error} = "got timeout\n";
c8125172 460 $self->{mux}->inbuffer($fh, ''); # clear to avoid warnings
30a3378a
DM
461 }
462
463 &$check_queue($self);
464}
465
1c0c1c17
WL
466sub mux_eof {
467 my ($self, $mux, $fh, $input) = @_;
c8125172 468
7a6c2150
DM
469 my $queue_info = &$lookup_queue_info($self, $fh);
470 return if !$queue_info;
1c0c1c17 471
7a6c2150
DM
472 my $sname = $queue_info->{sname};
473 my $vmid = $queue_info->{vmid};
474 my $qga = $queue_info->{qga};
475
476 my $curcmd = $queue_info->{current};
477 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
1c0c1c17 478
7a6c2150 479 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute}}) {
1c0c1c17 480
5cb6fe54 481 return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so;
1c0c1c17 482
c8125172
DM
483 my $raw = $1;
484
485 eval {
486 my $obj = from_json($raw);
1c0c1c17 487
c8125172
DM
488 my $cmdid = $obj->{'return'};
489 die "received responsed without command id\n" if !$cmdid;
7a6c2150 490
c8125172 491 delete $queue_info->{current};
7a6c2150 492
c8125172
DM
493 if (my $callback = $curcmd->{callback}) {
494 &$callback($vmid, undef);
495 }
496 };
497 if (my $err = $@) {
498 $queue_info->{error} = $err;
499 }
1c0c1c17 500
7a6c2150 501 &$close_connection($self, $queue_info);
c8125172
DM
502
503 if (scalar(@{$queue_info->{cmds}}) && !$queue_info->{error}) {
504 $queue_info->{error} = "Got EOF but command queue is not empty.\n";
505 }
1c0c1c17 506 }
1c0c1c17
WL
507}
508
26f11676 5091;