]> git.proxmox.com Git - qemu-server.git/blame - PVE/QMPClient.pm
qmpclient-qga : build qga command
[qemu-server.git] / PVE / QMPClient.pm
CommitLineData
30a3378a
DM
1package PVE::QMPClient;
2
3use strict;
990fc5e2 4use warnings;
30a3378a
DM
5use PVE::QemuServer;
6use IO::Multiplex;
2ae10d4e 7use POSIX qw(EINTR EAGAIN);
30a3378a 8use JSON;
2ae10d4e 9use Time::HiRes qw(usleep gettimeofday tv_interval);
b3ea07f7 10use Scalar::Util qw(weaken);
558f1644 11use PVE::IPCC;
2ae10d4e 12
30a3378a
DM
13use 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 commands to different VMs in parallel.
19
20# Note: kvm can onyl handle 1 connection, so we close connections asap
21
22sub new {
d64ee87e 23 my ($class, $eventcb, $qga) = @_;
30a3378a
DM
24
25 my $mux = new IO::Multiplex;
26
27 my $self = bless {
28 mux => $mux,
29 fhs => {}, # $vmid => fh
30 fhs_lookup => {}, # $fh => $vmid
31 queue => {},
32 current => {},
33 errors => {},
34 }, $class;
35
36 $self->{eventcb} = $eventcb if $eventcb;
d64ee87e 37 $self->{qga} = $qga if $qga;
30a3378a
DM
38
39 $mux->set_callback_object($self);
40
558f1644 41 # make sure perl doesn't believe this is a circular reference as we
b3ea07f7
DM
42 # delete mux in DESTROY
43 weaken($mux->{_object});
44
30a3378a
DM
45 return $self;
46}
47
558f1644 48# add a single command to the queue for later execution
30a3378a
DM
49# with queue_execute()
50sub queue_cmd {
51 my ($self, $vmid, $callback, $execute, %params) = @_;
52
53 my $cmd = {};
54 $cmd->{execute} = $execute;
55 $cmd->{arguments} = \%params;
56 $cmd->{callback} = $callback;
57
58 push @{$self->{queue}->{$vmid}}, $cmd;
59}
60
61# execute a single command
62sub cmd {
f0002f62 63 my ($self, $vmid, $cmd, $timeout) = @_;
30a3378a
DM
64
65 my $result;
66
67 my $callback = sub {
68 my ($vmid, $resp) = @_;
69 $result = $resp->{'return'};
70 };
71
f0002f62
DM
72 die "no command specified" if !($cmd && $cmd->{execute});
73
30a3378a
DM
74 $cmd->{callback} = $callback;
75 $cmd->{arguments} = {} if !defined($cmd->{arguments});
76
77 $self->{queue}->{$vmid} = [ $cmd ];
78
f0002f62
DM
79 if (!$timeout) {
80 # hack: monitor sometime blocks
81 if ($cmd->{execute} eq 'query-migrate') {
82 $timeout = 60*60; # 1 hour
83 } elsif ($cmd->{execute} =~ m/^(eject|change)/) {
84 $timeout = 60; # note: cdrom mount command is slow
9dcf4909
DM
85 } elsif ($cmd->{execute} eq 'savevm-start' ||
86 $cmd->{execute} eq 'savevm-end' ||
558f1644 87 $cmd->{execute} eq 'query-backup' ||
4fca0153 88 $cmd->{execute} eq 'query-block-jobs' ||
fc97ae27 89 $cmd->{execute} eq 'backup-cancel' ||
9dcf4909 90 $cmd->{execute} eq 'query-savevm' ||
9d689077
DM
91 $cmd->{execute} eq 'delete-drive-snapshot' ||
92 $cmd->{execute} eq 'snapshot-drive' ) {
93 $timeout = 10*60; # 10 mins ?
14db5366
DM
94 } else {
95 $timeout = 3; # default
f0002f62
DM
96 }
97 }
98
99 $self->queue_execute($timeout);
30a3378a
DM
100
101 my $cmdstr = $cmd->{execute} || '';
102 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
558f1644 103 if defined($self->{errors}->{$vmid});
30a3378a
DM
104
105 return $result;
106};
107
108my $cmdid_seq = 0;
109my $next_cmdid = sub {
110 $cmdid_seq++;
b1d8a6d4 111 return "$$"."0".$cmdid_seq;
30a3378a
DM
112};
113
114my $close_connection = sub {
115 my ($self, $vmid) = @_;
558f1644 116
30a3378a
DM
117 my $fh = $self->{fhs}->{$vmid};
118 return if !$fh;
558f1644 119
30a3378a
DM
120 delete $self->{fhs}->{$vmid};
121 delete $self->{fhs_lookup}->{$fh};
122
123 $self->{mux}->close($fh);
30a3378a
DM
124};
125
126my $open_connection = sub {
6d042176 127 my ($self, $vmid, $timeout) = @_;
30a3378a 128
f1f36aca 129 my $sname = PVE::QemuServer::qmp_socket($vmid, $self->{qga});
30a3378a 130
6d042176
DM
131 $timeout = 1 if !$timeout;
132
2ae10d4e
DM
133 my $fh;
134 my $starttime = [gettimeofday];
135 my $count = 0;
136 for (;;) {
137 $count++;
138 $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1);
139 last if $fh;
140 if ($! != EINTR && $! != EAGAIN) {
141 die "unable to connect to VM $vmid socket - $!\n";
142 }
143 my $elapsed = tv_interval($starttime, [gettimeofday]);
6d042176 144 if ($elapsed >= $timeout) {
2ae10d4e
DM
145 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
146 }
147 usleep(100000);
148 }
30a3378a 149
30a3378a
DM
150 $self->{fhs}->{$vmid} = $fh;
151 $self->{fhs_lookup}->{$fh} = $vmid;
152 $self->{mux}->add($fh);
558f1644 153
30a3378a
DM
154 return $fh;
155};
156
157my $check_queue = sub {
158 my ($self) = @_;
159
160 my $running = 0;
558f1644 161
30a3378a
DM
162 foreach my $vmid (keys %{$self->{queue}}) {
163 my $fh = $self->{fhs}->{$vmid};
164 next if !$fh;
165
166 if ($self->{errors}->{$vmid}) {
167 &$close_connection($self, $vmid);
168 next;
169 }
170
171 if ($self->{current}->{$vmid}) { # command running, waiting for response
172 $running++;
173 next;
174 }
175
176 if (!scalar(@{$self->{queue}->{$vmid}})) { # no more commands for the VM
177 &$close_connection($self, $vmid);
178 next;
179 }
180
181 eval {
182
183 my $cmd = $self->{current}->{$vmid} = shift @{$self->{queue}->{$vmid}};
184 $cmd->{id} = &$next_cmdid();
185
558f1644 186 my $fd = -1;
a0e7a5d0 187 if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') {
558f1644
DM
188 $fd = $cmd->{arguments}->{fd};
189 delete $cmd->{arguments}->{fd};
190 }
191
a45a14fc
AD
192 my $qmpcmd = undef;
193
194 if($self->{qga}){
195
196 my $qmpcmdid =to_json({
197 execute => 'guest-sync',
198 arguments => { id => int($cmd->{id})}});
199
200 $qmpcmd = to_json({
201 execute => $cmd->{execute},
202 arguments => $cmd->{arguments}});
203
204 $qmpcmd = $qmpcmdid.$qmpcmd;
205
206 }else{
207
208 $qmpcmd = to_json({
209 execute => $cmd->{execute},
210 arguments => $cmd->{arguments},
211 id => $cmd->{id}});
212 }
30a3378a 213
558f1644
DM
214 if ($fd >= 0) {
215 my $ret = PVE::IPCC::sendfd(fileno($fh), $fd, $qmpcmd);
216 die "sendfd failed" if $ret < 0;
217 } else {
218 $self->{mux}->write($fh, $qmpcmd);
219 }
30a3378a
DM
220 };
221 if (my $err = $@) {
222 $self->{errors}->{$vmid} = $err;
30a3378a
DM
223 } else {
224 $running++;
225 }
226 }
227
228 $self->{mux}->endloop() if !$running;
229
230 return $running;
231};
232
233# execute all queued command
234sub queue_execute {
235 my ($self, $timeout) = @_;
236
237 $timeout = 3 if !$timeout;
238
30a3378a
DM
239 $self->{current} = {};
240 $self->{errors} = {};
241
242 # open all necessary connections
243 foreach my $vmid (keys %{$self->{queue}}) {
244 next if !scalar(@{$self->{queue}->{$vmid}}); # no commands for the VM
245
246 eval {
6d042176 247 my $fh = &$open_connection($self, $vmid, $timeout);
c6fb6a69
AD
248
249 if(!$self->{qga}){
250 my $cmd = { execute => 'qmp_capabilities', arguments => {} };
251 unshift @{$self->{queue}->{$vmid}}, $cmd;
252 }
253
30a3378a
DM
254 $self->{mux}->set_timeout($fh, $timeout);
255 };
256 if (my $err = $@) {
257 warn $err;
258 $self->{errors}->{$vmid} = $err;
259 }
260 }
261
262 my $running;
263
264 for (;;) {
265
266 $running = &$check_queue($self);
267
268 last if !$running;
269
270 $self->{mux}->loop;
271 }
272
273 # make sure we close everything
274 foreach my $vmid (keys %{$self->{fhs}}) {
275 &$close_connection($self, $vmid);
276 }
277
278 $self->{queue} = $self->{current} = $self->{fhs} = $self->{fhs_lookup} = {};
30a3378a
DM
279}
280
b006e70b
DM
281sub mux_close {
282 my ($self, $mux, $fh) = @_;
283
284 my $vmid = $self->{fhs_lookup}->{$fh} || 'undef';
285 return if !defined($vmid);
286
287 $self->{errors}->{$vmid} = "client closed connection\n" if !$self->{errors}->{$vmid};
288}
289
30a3378a
DM
290# mux_input is called when input is available on one of
291# the descriptors.
292sub mux_input {
293 my ($self, $mux, $fh, $input) = @_;
294
f4fde4d3 295 return if $$input !~ s/^(.*})\r\n(.*)$/$2/so;
30a3378a 296
f4fde4d3 297 my $raw = $1;
30a3378a
DM
298
299 my $vmid = $self->{fhs_lookup}->{$fh};
300 if (!$vmid) {
301 warn "internal error - unable to lookup vmid";
302 return;
303 }
304
305 eval {
306 my @jsons = split("\n", $raw);
307
308 foreach my $json (@jsons) {
309 my $obj = from_json($json);
310 next if defined($obj->{QMP}); # skip monitor greeting
311
312 if (exists($obj->{error}->{desc})) {
313 my $desc = $obj->{error}->{desc};
314 chomp $desc;
315 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
316 next;
317 }
318
30a3378a
DM
319 if (defined($obj->{event})) {
320 if (my $eventcb = $self->{eventcb}) {
321 &$eventcb($obj);
322 }
323 next;
324 }
325
326 my $cmdid = $obj->{id};
327 die "received responsed without command id\n" if !$cmdid;
328
329 my $curcmd = $self->{current}->{$vmid};
330 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
331
332 delete $self->{current}->{$vmid};
558f1644 333
30a3378a
DM
334 if ($curcmd->{id} ne $cmdid) {
335 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
336 }
337
338 if (my $callback = $curcmd->{callback}) {
339 &$callback($vmid, $obj);
340 }
341 }
342 };
343 if (my $err = $@) {
344 $self->{errors}->{$vmid} = $err;
345 }
346
347 &$check_queue($self);
348}
349
350# This gets called every second to update player info, etc...
351sub mux_timeout {
352 my ($self, $mux, $fh) = @_;
353
354 if (my $vmid = $self->{fhs_lookup}->{$fh}) {
30a3378a
DM
355 $self->{errors}->{$vmid} = "got timeout\n";
356 }
357
358 &$check_queue($self);
359}
360
26f11676 3611;