]>
Commit | Line | Data |
---|---|---|
1 | package PVE::QMPClient; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use IO::Multiplex; | |
7 | use JSON; | |
8 | use POSIX qw(EINTR EAGAIN); | |
9 | use Scalar::Util qw(weaken); | |
10 | use Time::HiRes qw(usleep gettimeofday tv_interval); | |
11 | ||
12 | use PVE::IPCC; | |
13 | use PVE::QemuServer::Helpers; | |
14 | ||
15 | # QEMU Monitor Protocol (QMP) client. | |
16 | # | |
17 | # This implementation uses IO::Multiplex (libio-multiplex-perl) and | |
18 | # allows you to issue qmp and qga commands to different VMs in parallel. | |
19 | ||
20 | # Note: qemu can only handle 1 connection, so we close connections asap | |
21 | ||
22 | sub new { | |
23 | my ($class, $eventcb) = @_; | |
24 | ||
25 | my $mux = IO::Multiplex->new(); | |
26 | ||
27 | my $self = bless { | |
28 | mux => $mux, | |
29 | queue_lookup => {}, # $fh => $queue_info | |
30 | queue_info => {}, | |
31 | }, $class; | |
32 | ||
33 | $self->{eventcb} = $eventcb if $eventcb; | |
34 | ||
35 | $mux->set_callback_object($self); | |
36 | ||
37 | # make sure perl doesn't believe this is a circular reference as we | |
38 | # delete mux in DESTROY | |
39 | weaken($mux->{_object}); | |
40 | ||
41 | return $self; | |
42 | } | |
43 | ||
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::Helpers::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 | ||
71 | # add a single command to the queue for later execution | |
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 | ||
81 | &$push_cmd_to_queue($self, $vmid, $cmd); | |
82 | ||
83 | return; | |
84 | } | |
85 | ||
86 | # execute a single command | |
87 | sub cmd { | |
88 | my ($self, $vmid, $cmd, $timeout) = @_; | |
89 | ||
90 | my $result; | |
91 | ||
92 | my $callback = sub { | |
93 | my ($vmid, $resp) = @_; | |
94 | $result = $resp->{'return'}; | |
95 | $result = { error => $resp->{'error'} } if !defined($result) && $resp->{'error'}; | |
96 | }; | |
97 | ||
98 | die "no command specified" if !($cmd && $cmd->{execute}); | |
99 | ||
100 | $cmd->{callback} = $callback; | |
101 | $cmd->{arguments} = {} if !defined($cmd->{arguments}); | |
102 | ||
103 | my $queue_info = &$push_cmd_to_queue($self, $vmid, $cmd); | |
104 | ||
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 | |
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 | # 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; | |
120 | } elsif ( | |
121 | $cmd->{execute} eq 'savevm-start' || | |
122 | $cmd->{execute} eq 'savevm-end' || | |
123 | $cmd->{execute} eq 'query-backup' || | |
124 | $cmd->{execute} eq 'query-block-jobs' || | |
125 | $cmd->{execute} eq 'block-job-cancel' || | |
126 | $cmd->{execute} eq 'block-job-complete' || | |
127 | $cmd->{execute} eq 'backup-cancel' || | |
128 | $cmd->{execute} eq 'query-savevm' || | |
129 | $cmd->{execute} eq 'delete-drive-snapshot' || | |
130 | $cmd->{execute} eq 'guest-fstrim' || | |
131 | $cmd->{execute} eq 'guest-shutdown' || | |
132 | $cmd->{execute} eq 'blockdev-snapshot-internal-sync' || | |
133 | $cmd->{execute} eq 'blockdev-snapshot-delete-internal-sync' || | |
134 | $cmd->{execute} eq 'snapshot-drive' | |
135 | ) { | |
136 | $timeout = 10*60; # 10 mins ? | |
137 | } else { | |
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 | |
141 | } | |
142 | } | |
143 | ||
144 | $self->queue_execute($timeout, 2); | |
145 | ||
146 | die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}" | |
147 | if defined($queue_info->{error}); | |
148 | ||
149 | return $result; | |
150 | }; | |
151 | ||
152 | my $cmdid_seq = 0; | |
153 | my $cmdid_seq_qga = 0; | |
154 | ||
155 | my $next_cmdid = sub { | |
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 | } | |
165 | }; | |
166 | ||
167 | my $lookup_queue_info = sub { | |
168 | my ($self, $fh, $noerr) = @_; | |
169 | ||
170 | my $queue_info = $self->{queue_lookup}->{$fh}; | |
171 | if (!$queue_info) { | |
172 | warn "internal error - unable to lookup queue info" if !$noerr; | |
173 | return; | |
174 | } | |
175 | return $queue_info; | |
176 | }; | |
177 | ||
178 | my $close_connection = sub { | |
179 | my ($self, $queue_info) = @_; | |
180 | ||
181 | if (my $fh = delete $queue_info->{fh}) { | |
182 | delete $self->{queue_lookup}->{$fh}; | |
183 | $self->{mux}->close($fh); | |
184 | } | |
185 | }; | |
186 | ||
187 | my $open_connection = sub { | |
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}; | |
194 | ||
195 | my $sname = PVE::QemuServer::Helpers::qmp_socket($vmid, $qga); | |
196 | ||
197 | $timeout = 1 if !$timeout; | |
198 | ||
199 | my $fh; | |
200 | my $starttime = [gettimeofday]; | |
201 | my $count = 0; | |
202 | ||
203 | my $sotype = $qga ? 'qga' : 'qmp'; | |
204 | ||
205 | for (;;) { | |
206 | $count++; | |
207 | $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1); | |
208 | last if $fh; | |
209 | if ($! != EINTR && $! != EAGAIN) { | |
210 | die "unable to connect to VM $vmid $sotype socket - $!\n"; | |
211 | } | |
212 | my $elapsed = tv_interval($starttime, [gettimeofday]); | |
213 | if ($elapsed >= $timeout) { | |
214 | die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n"; | |
215 | } | |
216 | usleep(100000); | |
217 | } | |
218 | ||
219 | $queue_info->{fh} = $fh; | |
220 | ||
221 | $self->{queue_lookup}->{$fh} = $queue_info; | |
222 | ||
223 | $self->{mux}->add($fh); | |
224 | $self->{mux}->set_timeout($fh, $timeout); | |
225 | ||
226 | return $fh; | |
227 | }; | |
228 | ||
229 | my $check_queue = sub { | |
230 | my ($self) = @_; | |
231 | ||
232 | my $running = 0; | |
233 | ||
234 | foreach my $sname (keys %{$self->{queue_info}}) { | |
235 | my $queue_info = $self->{queue_info}->{$sname}; | |
236 | my $fh = $queue_info->{fh}; | |
237 | next if !$fh; | |
238 | ||
239 | my $qga = $queue_info->{qga}; | |
240 | ||
241 | if ($queue_info->{error}) { | |
242 | &$close_connection($self, $queue_info); | |
243 | next; | |
244 | } | |
245 | ||
246 | if ($queue_info->{current}) { # command running, waiting for response | |
247 | $running++; | |
248 | next; | |
249 | } | |
250 | ||
251 | if (!scalar(@{$queue_info->{cmds}})) { # no more commands | |
252 | &$close_connection($self, $queue_info); | |
253 | next; | |
254 | } | |
255 | ||
256 | eval { | |
257 | ||
258 | my $cmd = $queue_info->{current} = shift @{$queue_info->{cmds}}; | |
259 | $cmd->{id} = &$next_cmdid($qga); | |
260 | ||
261 | my $fd = -1; | |
262 | if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') { | |
263 | $fd = $cmd->{arguments}->{fd}; | |
264 | delete $cmd->{arguments}->{fd}; | |
265 | } | |
266 | ||
267 | my $qmpcmd; | |
268 | ||
269 | if ($qga) { | |
270 | ||
271 | $qmpcmd = to_json({ execute => 'guest-sync-delimited', | |
272 | arguments => { id => int($cmd->{id})}}) . "\n" . | |
273 | to_json({ execute => $cmd->{execute}, arguments => $cmd->{arguments}}) . "\n"; | |
274 | ||
275 | } else { | |
276 | ||
277 | $qmpcmd = to_json({ | |
278 | execute => $cmd->{execute}, | |
279 | arguments => $cmd->{arguments}, | |
280 | id => $cmd->{id}}); | |
281 | } | |
282 | ||
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 | } | |
289 | }; | |
290 | if (my $err = $@) { | |
291 | $queue_info->{error} = $err; | |
292 | } else { | |
293 | $running++; | |
294 | } | |
295 | } | |
296 | ||
297 | $self->{mux}->endloop() if !$running; | |
298 | ||
299 | return $running; | |
300 | }; | |
301 | ||
302 | # execute all queued command | |
303 | ||
304 | sub queue_execute { | |
305 | my ($self, $timeout, $noerr) = @_; | |
306 | ||
307 | $timeout = 3 if !$timeout; | |
308 | ||
309 | # open all necessary connections | |
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 | |
313 | ||
314 | $queue_info->{error} = undef; | |
315 | $queue_info->{current} = undef; | |
316 | ||
317 | eval { | |
318 | &$open_connection($self, $queue_info, $timeout); | |
319 | ||
320 | if (!$queue_info->{qga}) { | |
321 | my $cap_cmd = { execute => 'qmp_capabilities', arguments => {} }; | |
322 | unshift @{$queue_info->{cmds}}, $cap_cmd; | |
323 | } | |
324 | }; | |
325 | if (my $err = $@) { | |
326 | $queue_info->{error} = $err; | |
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 | |
342 | my $errors = ''; | |
343 | foreach my $sname (keys %{$self->{queue_info}}) { | |
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 | } | |
353 | } | |
354 | ||
355 | $self->{queue_info} = $self->{queue_lookup} = {}; | |
356 | ||
357 | die $errors if $errors; | |
358 | } | |
359 | ||
360 | sub mux_close { | |
361 | my ($self, $mux, $fh) = @_; | |
362 | ||
363 | my $queue_info = &$lookup_queue_info($self, $fh, 1); | |
364 | return if !$queue_info; | |
365 | ||
366 | $queue_info->{error} = "client closed connection\n" | |
367 | if !$queue_info->{error}; | |
368 | } | |
369 | ||
370 | # mux_input is called when input is available on one of the descriptors. | |
371 | sub mux_input { | |
372 | my ($self, $mux, $fh, $input) = @_; | |
373 | ||
374 | my $queue_info = &$lookup_queue_info($self, $fh); | |
375 | return if !$queue_info; | |
376 | ||
377 | my $sname = $queue_info->{sname}; | |
378 | my $vmid = $queue_info->{vmid}; | |
379 | my $qga = $queue_info->{qga}; | |
380 | ||
381 | my $curcmd = $queue_info->{current}; | |
382 | die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd; | |
383 | ||
384 | my $raw; | |
385 | ||
386 | if ($qga) { | |
387 | return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so; | |
388 | $raw = $1; | |
389 | } else { | |
390 | return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so; | |
391 | $raw = $1; | |
392 | } | |
393 | ||
394 | eval { | |
395 | my @jsons = split("\n", $raw); | |
396 | ||
397 | if ($qga) { | |
398 | ||
399 | die "response is not complete" if @jsons != 2 ; | |
400 | ||
401 | my $obj = from_json($jsons[0]); | |
402 | ||
403 | my $cmdid = $obj->{'return'}; | |
404 | die "received responsed without command id\n" if !$cmdid; | |
405 | ||
406 | # skip results fro previous commands | |
407 | return if $cmdid < $curcmd->{id}; | |
408 | ||
409 | if ($curcmd->{id} ne $cmdid) { | |
410 | die "got wrong command id '$cmdid' (expected $curcmd->{id})\n"; | |
411 | } | |
412 | ||
413 | delete $queue_info->{current}; | |
414 | ||
415 | $obj = from_json($jsons[1]); | |
416 | ||
417 | if (my $callback = $curcmd->{callback}) { | |
418 | &$callback($vmid, $obj); | |
419 | } | |
420 | ||
421 | return; | |
422 | } | |
423 | ||
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 | ||
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 | ||
445 | if ($curcmd->{id} ne $cmdid) { | |
446 | die "got wrong command id '$cmdid' (expected $curcmd->{id})\n"; | |
447 | } | |
448 | ||
449 | delete $queue_info->{current}; | |
450 | ||
451 | if (my $callback = $curcmd->{callback}) { | |
452 | &$callback($vmid, $obj); | |
453 | } | |
454 | } | |
455 | }; | |
456 | if (my $err = $@) { | |
457 | $queue_info->{error} = $err; | |
458 | } | |
459 | ||
460 | &$check_queue($self); | |
461 | } | |
462 | ||
463 | # This gets called every second to update player info, etc... | |
464 | sub mux_timeout { | |
465 | my ($self, $mux, $fh) = @_; | |
466 | ||
467 | if (my $queue_info = &$lookup_queue_info($self, $fh)) { | |
468 | $queue_info->{error} = "got timeout\n"; | |
469 | $self->{mux}->inbuffer($fh, ''); # clear to avoid warnings | |
470 | } | |
471 | ||
472 | &$check_queue($self); | |
473 | } | |
474 | ||
475 | sub mux_eof { | |
476 | my ($self, $mux, $fh, $input) = @_; | |
477 | ||
478 | my $queue_info = &$lookup_queue_info($self, $fh); | |
479 | return if !$queue_info; | |
480 | ||
481 | my $sname = $queue_info->{sname}; | |
482 | my $vmid = $queue_info->{vmid}; | |
483 | my $qga = $queue_info->{qga}; | |
484 | ||
485 | my $curcmd = $queue_info->{current}; | |
486 | die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd; | |
487 | ||
488 | if ($qga && $qga_allow_close_cmds->{$curcmd->{execute}}) { | |
489 | ||
490 | return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so; | |
491 | ||
492 | my $raw = $1; | |
493 | ||
494 | eval { | |
495 | my $obj = from_json($raw); | |
496 | ||
497 | my $cmdid = $obj->{'return'}; | |
498 | die "received responsed without command id\n" if !$cmdid; | |
499 | ||
500 | delete $queue_info->{current}; | |
501 | ||
502 | if (my $callback = $curcmd->{callback}) { | |
503 | &$callback($vmid, undef); | |
504 | } | |
505 | }; | |
506 | if (my $err = $@) { | |
507 | $queue_info->{error} = $err; | |
508 | } | |
509 | ||
510 | &$close_connection($self, $queue_info); | |
511 | ||
512 | if (scalar(@{$queue_info->{cmds}}) && !$queue_info->{error}) { | |
513 | $queue_info->{error} = "Got EOF but command queue is not empty.\n"; | |
514 | } | |
515 | } | |
516 | } | |
517 | ||
518 | sub 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 | ||
527 | 1; |