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