]>
Commit | Line | Data |
---|---|---|
1 | package PVE::QMPClient; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use PVE::QemuServer; | |
6 | use IO::Multiplex; | |
7 | use POSIX qw(EINTR EAGAIN); | |
8 | use JSON; | |
9 | use Time::HiRes qw(usleep gettimeofday tv_interval); | |
10 | use Scalar::Util qw(weaken); | |
11 | use PVE::IPCC; | |
12 | ||
13 | use Data::Dumper; | |
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 onyl handle 1 connection, so we close connections asap | |
21 | ||
22 | sub new { | |
23 | my ($class, $eventcb) = @_; | |
24 | ||
25 | my $mux = new IO::Multiplex; | |
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::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 undef; | |
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 | }; | |
96 | ||
97 | die "no command specified" if !($cmd && $cmd->{execute}); | |
98 | ||
99 | $cmd->{callback} = $callback; | |
100 | $cmd->{arguments} = {} if !defined($cmd->{arguments}); | |
101 | ||
102 | my $queue_info = &$push_cmd_to_queue($self, $vmid, $cmd); | |
103 | ||
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 | |
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) | |
117 | $timeout = 10; | |
118 | } elsif ($cmd->{execute} eq 'savevm-start' || | |
119 | $cmd->{execute} eq 'savevm-end' || | |
120 | $cmd->{execute} eq 'query-backup' || | |
121 | $cmd->{execute} eq 'query-block-jobs' || | |
122 | $cmd->{execute} eq 'block-job-cancel' || | |
123 | $cmd->{execute} eq 'block-job-complete' || | |
124 | $cmd->{execute} eq 'backup-cancel' || | |
125 | $cmd->{execute} eq 'query-savevm' || | |
126 | $cmd->{execute} eq 'delete-drive-snapshot' || | |
127 | $cmd->{execute} eq 'guest-shutdown' || | |
128 | $cmd->{execute} eq 'snapshot-drive' ) { | |
129 | $timeout = 10*60; # 10 mins ? | |
130 | } else { | |
131 | $timeout = 3; # default | |
132 | } | |
133 | } | |
134 | ||
135 | $self->queue_execute($timeout, 2); | |
136 | ||
137 | die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}" | |
138 | if defined($queue_info->{error}); | |
139 | ||
140 | return $result; | |
141 | }; | |
142 | ||
143 | my $cmdid_seq = 0; | |
144 | my $cmdid_seq_qga = 0; | |
145 | ||
146 | my $next_cmdid = sub { | |
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 | } | |
156 | }; | |
157 | ||
158 | my $lookup_queue_info = sub { | |
159 | my ($self, $fh, $noerr) = @_; | |
160 | ||
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 | }; | |
168 | ||
169 | my $close_connection = sub { | |
170 | my ($self, $queue_info) = @_; | |
171 | ||
172 | if (my $fh = delete $queue_info->{fh}) { | |
173 | delete $self->{queue_lookup}->{$fh}; | |
174 | $self->{mux}->close($fh); | |
175 | } | |
176 | }; | |
177 | ||
178 | my $open_connection = sub { | |
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}; | |
185 | ||
186 | my $sname = PVE::QemuServer::qmp_socket($vmid, $qga); | |
187 | ||
188 | $timeout = 1 if !$timeout; | |
189 | ||
190 | my $fh; | |
191 | my $starttime = [gettimeofday]; | |
192 | my $count = 0; | |
193 | ||
194 | my $sotype = $qga ? 'qga' : 'qmp'; | |
195 | ||
196 | for (;;) { | |
197 | $count++; | |
198 | $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1); | |
199 | last if $fh; | |
200 | if ($! != EINTR && $! != EAGAIN) { | |
201 | die "unable to connect to VM $vmid $sotype socket - $!\n"; | |
202 | } | |
203 | my $elapsed = tv_interval($starttime, [gettimeofday]); | |
204 | if ($elapsed >= $timeout) { | |
205 | die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n"; | |
206 | } | |
207 | usleep(100000); | |
208 | } | |
209 | ||
210 | $queue_info->{fh} = $fh; | |
211 | ||
212 | $self->{queue_lookup}->{$fh} = $queue_info; | |
213 | ||
214 | $self->{mux}->add($fh); | |
215 | $self->{mux}->set_timeout($fh, $timeout); | |
216 | ||
217 | return $fh; | |
218 | }; | |
219 | ||
220 | my $check_queue = sub { | |
221 | my ($self) = @_; | |
222 | ||
223 | my $running = 0; | |
224 | ||
225 | foreach my $sname (keys %{$self->{queue_info}}) { | |
226 | my $queue_info = $self->{queue_info}->{$sname}; | |
227 | my $fh = $queue_info->{fh}; | |
228 | next if !$fh; | |
229 | ||
230 | my $qga = $queue_info->{qga}; | |
231 | ||
232 | if ($queue_info->{error}) { | |
233 | &$close_connection($self, $queue_info); | |
234 | next; | |
235 | } | |
236 | ||
237 | if ($queue_info->{current}) { # command running, waiting for response | |
238 | $running++; | |
239 | next; | |
240 | } | |
241 | ||
242 | if (!scalar(@{$queue_info->{cmds}})) { # no more commands | |
243 | &$close_connection($self, $queue_info); | |
244 | next; | |
245 | } | |
246 | ||
247 | eval { | |
248 | ||
249 | my $cmd = $queue_info->{current} = shift @{$queue_info->{cmds}}; | |
250 | $cmd->{id} = &$next_cmdid($qga); | |
251 | ||
252 | my $fd = -1; | |
253 | if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') { | |
254 | $fd = $cmd->{arguments}->{fd}; | |
255 | delete $cmd->{arguments}->{fd}; | |
256 | } | |
257 | ||
258 | my $qmpcmd; | |
259 | ||
260 | if ($qga) { | |
261 | ||
262 | $qmpcmd = to_json({ execute => 'guest-sync-delimited', | |
263 | arguments => { id => int($cmd->{id})}}) . | |
264 | to_json({ execute => $cmd->{execute}, arguments => $cmd->{arguments}}); | |
265 | ||
266 | } else { | |
267 | ||
268 | $qmpcmd = to_json({ | |
269 | execute => $cmd->{execute}, | |
270 | arguments => $cmd->{arguments}, | |
271 | id => $cmd->{id}}); | |
272 | } | |
273 | ||
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 | } | |
280 | }; | |
281 | if (my $err = $@) { | |
282 | $queue_info->{error} = $err; | |
283 | } else { | |
284 | $running++; | |
285 | } | |
286 | } | |
287 | ||
288 | $self->{mux}->endloop() if !$running; | |
289 | ||
290 | return $running; | |
291 | }; | |
292 | ||
293 | # execute all queued command | |
294 | ||
295 | sub queue_execute { | |
296 | my ($self, $timeout, $noerr) = @_; | |
297 | ||
298 | $timeout = 3 if !$timeout; | |
299 | ||
300 | # open all necessary connections | |
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; | |
307 | ||
308 | eval { | |
309 | &$open_connection($self, $queue_info, $timeout); | |
310 | ||
311 | if (!$queue_info->{qga}) { | |
312 | my $cap_cmd = { execute => 'qmp_capabilities', arguments => {} }; | |
313 | unshift @{$queue_info->{cmds}}, $cap_cmd; | |
314 | } | |
315 | }; | |
316 | if (my $err = $@) { | |
317 | $queue_info->{error} = $err; | |
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 | |
333 | my $errors = ''; | |
334 | foreach my $sname (keys %{$self->{queue_info}}) { | |
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 | } | |
344 | } | |
345 | ||
346 | $self->{queue_info} = $self->{queue_lookup} = {}; | |
347 | ||
348 | die $errors if $errors; | |
349 | } | |
350 | ||
351 | sub mux_close { | |
352 | my ($self, $mux, $fh) = @_; | |
353 | ||
354 | my $queue_info = &$lookup_queue_info($self, $fh, 1); | |
355 | return if !$queue_info; | |
356 | ||
357 | $queue_info->{error} = "client closed connection\n" | |
358 | if !$queue_info->{error}; | |
359 | } | |
360 | ||
361 | # mux_input is called when input is available on one of the descriptors. | |
362 | sub mux_input { | |
363 | my ($self, $mux, $fh, $input) = @_; | |
364 | ||
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}; | |
371 | ||
372 | my $curcmd = $queue_info->{current}; | |
373 | die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd; | |
374 | ||
375 | my $raw; | |
376 | ||
377 | if ($qga) { | |
378 | return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so; | |
379 | $raw = $1; | |
380 | } else { | |
381 | return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so; | |
382 | $raw = $1; | |
383 | } | |
384 | ||
385 | eval { | |
386 | my @jsons = split("\n", $raw); | |
387 | ||
388 | if ($qga) { | |
389 | ||
390 | die "response is not complete" if @jsons != 2 ; | |
391 | ||
392 | my $obj = from_json($jsons[0]); | |
393 | ||
394 | my $cmdid = $obj->{'return'}; | |
395 | die "received responsed without command id\n" if !$cmdid; | |
396 | ||
397 | # skip results fro previous commands | |
398 | return if $cmdid < $curcmd->{id}; | |
399 | ||
400 | if ($curcmd->{id} ne $cmdid) { | |
401 | die "got wrong command id '$cmdid' (expected $curcmd->{id})\n"; | |
402 | } | |
403 | ||
404 | delete $queue_info->{current}; | |
405 | ||
406 | $obj = from_json($jsons[1]); | |
407 | ||
408 | if (my $callback = $curcmd->{callback}) { | |
409 | &$callback($vmid, $obj); | |
410 | } | |
411 | ||
412 | return; | |
413 | } | |
414 | ||
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 | ||
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 | ||
436 | if ($curcmd->{id} ne $cmdid) { | |
437 | die "got wrong command id '$cmdid' (expected $curcmd->{id})\n"; | |
438 | } | |
439 | ||
440 | delete $queue_info->{current}; | |
441 | ||
442 | if (my $callback = $curcmd->{callback}) { | |
443 | &$callback($vmid, $obj); | |
444 | } | |
445 | } | |
446 | }; | |
447 | if (my $err = $@) { | |
448 | $queue_info->{error} = $err; | |
449 | } | |
450 | ||
451 | &$check_queue($self); | |
452 | } | |
453 | ||
454 | # This gets called every second to update player info, etc... | |
455 | sub mux_timeout { | |
456 | my ($self, $mux, $fh) = @_; | |
457 | ||
458 | if (my $queue_info = &$lookup_queue_info($self, $fh)) { | |
459 | $queue_info->{error} = "got timeout\n"; | |
460 | $self->{mux}->inbuffer($fh, ''); # clear to avoid warnings | |
461 | } | |
462 | ||
463 | &$check_queue($self); | |
464 | } | |
465 | ||
466 | sub mux_eof { | |
467 | my ($self, $mux, $fh, $input) = @_; | |
468 | ||
469 | my $queue_info = &$lookup_queue_info($self, $fh); | |
470 | return if !$queue_info; | |
471 | ||
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; | |
478 | ||
479 | if ($qga && $qga_allow_close_cmds->{$curcmd->{execute}}) { | |
480 | ||
481 | return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so; | |
482 | ||
483 | my $raw = $1; | |
484 | ||
485 | eval { | |
486 | my $obj = from_json($raw); | |
487 | ||
488 | my $cmdid = $obj->{'return'}; | |
489 | die "received responsed without command id\n" if !$cmdid; | |
490 | ||
491 | delete $queue_info->{current}; | |
492 | ||
493 | if (my $callback = $curcmd->{callback}) { | |
494 | &$callback($vmid, undef); | |
495 | } | |
496 | }; | |
497 | if (my $err = $@) { | |
498 | $queue_info->{error} = $err; | |
499 | } | |
500 | ||
501 | &$close_connection($self, $queue_info); | |
502 | ||
503 | if (scalar(@{$queue_info->{cmds}}) && !$queue_info->{error}) { | |
504 | $queue_info->{error} = "Got EOF but command queue is not empty.\n"; | |
505 | } | |
506 | } | |
507 | } | |
508 | ||
509 | 1; |