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