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