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