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