]> git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
use new QMPClient code
[qemu-server.git] / PVE / QMPClient.pm
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 {
53 my ($self, $vmid, $cmd) = @_;
54
55 my $result;
56
57 my $callback = sub {
58 my ($vmid, $resp) = @_;
59 $result = $resp->{'return'};
60 };
61
62 $cmd->{callback} = $callback;
63 $cmd->{arguments} = {} if !defined($cmd->{arguments});
64
65 $self->{queue}->{$vmid} = [ $cmd ];
66
67 $self->queue_execute();
68
69 my $cmdstr = $cmd->{execute} || '';
70 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
71 if defined($self->{errors}->{$vmid});
72
73 return $result;
74 };
75
76 my $cmdid_seq = 0;
77 my $next_cmdid = sub {
78 $cmdid_seq++;
79 return "$$:$cmdid_seq";
80 };
81
82 my $close_connection = sub {
83 my ($self, $vmid) = @_;
84
85 my $fh = $self->{fhs}->{$vmid};
86 return if !$fh;
87
88 delete $self->{fhs}->{$vmid};
89 delete $self->{fhs_lookup}->{$fh};
90
91 $self->{mux}->close($fh);
92 };
93
94 my $open_connection = sub {
95 my ($self, $vmid) = @_;
96
97 my $sname = PVE::QemuServer::qmp_socket($vmid);
98
99 my $fh = IO::Socket::UNIX->new(Peer => $sname, Blocking => 0, Timeout => 1) ||
100 die "unable to connect to VM $vmid socket - $!\n";
101
102 $self->{fhs}->{$vmid} = $fh;
103 $self->{fhs_lookup}->{$fh} = $vmid;
104 $self->{mux}->add($fh);
105
106 return $fh;
107 };
108
109 my $check_queue = sub {
110 my ($self) = @_;
111
112 my $running = 0;
113
114 foreach my $vmid (keys %{$self->{queue}}) {
115 my $fh = $self->{fhs}->{$vmid};
116 next if !$fh;
117
118 if ($self->{errors}->{$vmid}) {
119 &$close_connection($self, $vmid);
120 next;
121 }
122
123 if ($self->{current}->{$vmid}) { # command running, waiting for response
124 $running++;
125 next;
126 }
127
128 if (!scalar(@{$self->{queue}->{$vmid}})) { # no more commands for the VM
129 &$close_connection($self, $vmid);
130 next;
131 }
132
133 eval {
134
135 my $cmd = $self->{current}->{$vmid} = shift @{$self->{queue}->{$vmid}};
136 $cmd->{id} = &$next_cmdid();
137
138 my $qmpcmd = to_json({
139 execute => $cmd->{execute},
140 arguments => $cmd->{arguments},
141 id => $cmd->{id}});
142
143 $self->{mux}->write($fh, $qmpcmd);
144 };
145 if (my $err = $@) {
146 $self->{errors}->{$vmid} = $err;
147 } else {
148 $running++;
149 }
150 }
151
152 $self->{mux}->endloop() if !$running;
153
154 return $running;
155 };
156
157 # execute all queued command
158 sub queue_execute {
159 my ($self, $timeout) = @_;
160
161 $timeout = 3 if !$timeout;
162
163 $self->{current} = {};
164 $self->{errors} = {};
165
166 # open all necessary connections
167 foreach my $vmid (keys %{$self->{queue}}) {
168 next if !scalar(@{$self->{queue}->{$vmid}}); # no commands for the VM
169
170 eval {
171 my $fh = &$open_connection($self, $vmid);
172 my $cmd = { execute => 'qmp_capabilities', arguments => {} };
173 unshift @{$self->{queue}->{$vmid}}, $cmd;
174 $self->{mux}->set_timeout($fh, $timeout);
175 };
176 if (my $err = $@) {
177 warn $err;
178 $self->{errors}->{$vmid} = $err;
179 }
180 }
181
182 my $running;
183
184 for (;;) {
185
186 $running = &$check_queue($self);
187
188 last if !$running;
189
190 $self->{mux}->loop;
191 }
192
193 # make sure we close everything
194 foreach my $vmid (keys %{$self->{fhs}}) {
195 &$close_connection($self, $vmid);
196 }
197
198 $self->{queue} = $self->{current} = $self->{fhs} = $self->{fhs_lookup} = {};
199 }
200
201 # mux_input is called when input is available on one of
202 # the descriptors.
203 sub mux_input {
204 my ($self, $mux, $fh, $input) = @_;
205
206 return if $$input !~ m/}\r\n$/;
207
208 my $raw = $$input;
209
210 # Remove the input from the input buffer.
211 $$input = '';
212
213 my $vmid = $self->{fhs_lookup}->{$fh};
214 if (!$vmid) {
215 warn "internal error - unable to lookup vmid";
216 return;
217 }
218
219 eval {
220 my @jsons = split("\n", $raw);
221
222 foreach my $json (@jsons) {
223 my $obj = from_json($json);
224 next if defined($obj->{QMP}); # skip monitor greeting
225
226 if (exists($obj->{error}->{desc})) {
227 my $desc = $obj->{error}->{desc};
228 chomp $desc;
229 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
230 next;
231 }
232
233 if (defined($obj->{event})) {
234 if (my $eventcb = $self->{eventcb}) {
235 &$eventcb($obj);
236 }
237 next;
238 }
239
240 my $cmdid = $obj->{id};
241 die "received responsed without command id\n" if !$cmdid;
242
243 my $curcmd = $self->{current}->{$vmid};
244 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
245
246 delete $self->{current}->{$vmid};
247
248 if ($curcmd->{id} ne $cmdid) {
249 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
250 }
251
252 if (my $callback = $curcmd->{callback}) {
253 &$callback($vmid, $obj);
254 }
255 }
256 };
257 if (my $err = $@) {
258 $self->{errors}->{$vmid} = $err;
259 }
260
261 &$check_queue($self);
262 }
263
264 # This gets called every second to update player info, etc...
265 sub mux_timeout {
266 my ($self, $mux, $fh) = @_;
267
268 if (my $vmid = $self->{fhs_lookup}->{$fh}) {
269 $self->{errors}->{$vmid} = "got timeout\n";
270 }
271
272 &$check_queue($self);
273 }
274
275 1;