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