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