]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
1 package PVE
::QMPClient
;
7 use POSIX
qw(EINTR EAGAIN);
9 use Time
::HiRes
qw(usleep gettimeofday tv_interval);
13 # Qemu Monitor Protocol (QMP) client.
15 # This implementation uses IO::Multiplex (libio-multiplex-perl) and
16 # allows you to issue qmp commands to different VMs in parallel.
18 # Note: kvm can onyl handle 1 connection, so we close connections asap
21 my ($class, $eventcb) = @_;
23 my $mux = new IO
::Multiplex
;
27 fhs
=> {}, # $vmid => fh
28 fhs_lookup
=> {}, # $fh => $vmid
34 $self->{eventcb
} = $eventcb if $eventcb;
36 $mux->set_callback_object($self);
41 # add a single command to the queue for later execution
42 # with queue_execute()
44 my ($self, $vmid, $callback, $execute, %params) = @_;
47 $cmd->{execute
} = $execute;
48 $cmd->{arguments
} = \
%params;
49 $cmd->{callback
} = $callback;
51 push @{$self->{queue
}->{$vmid}}, $cmd;
54 # execute a single command
56 my ($self, $vmid, $cmd, $timeout) = @_;
61 my ($vmid, $resp) = @_;
62 $result = $resp->{'return'};
65 die "no command specified" if !($cmd && $cmd->{execute
});
67 $cmd->{callback
} = $callback;
68 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
70 $self->{queue
}->{$vmid} = [ $cmd ];
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
78 } elsif ($cmd->{execute
} eq 'snapshot-start' ||
79 $cmd->{execute
} eq 'snapshot-end' ||
80 $cmd->{execute
} eq 'delete-drive-snapshot' ||
81 $cmd->{execute
} eq 'snapshot-drive' ) {
82 $timeout = 10*60; # 10 mins ?
84 $timeout = 3; # default
88 $self->queue_execute($timeout);
90 my $cmdstr = $cmd->{execute
} || '';
91 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
92 if defined($self->{errors
}->{$vmid});
98 my $next_cmdid = sub {
100 return "$$:$cmdid_seq";
103 my $close_connection = sub {
104 my ($self, $vmid) = @_;
106 my $fh = $self->{fhs
}->{$vmid};
109 delete $self->{fhs
}->{$vmid};
110 delete $self->{fhs_lookup
}->{$fh};
112 $self->{mux
}->close($fh);
115 my $open_connection = sub {
116 my ($self, $vmid) = @_;
118 my $sname = PVE
::QemuServer
::qmp_socket
($vmid);
121 my $starttime = [gettimeofday
];
125 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
127 if ($! != EINTR
&& $! != EAGAIN
) {
128 die "unable to connect to VM $vmid socket - $!\n";
130 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
132 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
137 $self->{fhs
}->{$vmid} = $fh;
138 $self->{fhs_lookup
}->{$fh} = $vmid;
139 $self->{mux
}->add($fh);
144 my $check_queue = sub {
149 foreach my $vmid (keys %{$self->{queue
}}) {
150 my $fh = $self->{fhs
}->{$vmid};
153 if ($self->{errors
}->{$vmid}) {
154 &$close_connection($self, $vmid);
158 if ($self->{current
}->{$vmid}) { # command running, waiting for response
163 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
164 &$close_connection($self, $vmid);
170 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
171 $cmd->{id
} = &$next_cmdid();
173 my $qmpcmd = to_json
({
174 execute
=> $cmd->{execute
},
175 arguments
=> $cmd->{arguments
},
178 $self->{mux
}->write($fh, $qmpcmd);
181 $self->{errors
}->{$vmid} = $err;
187 $self->{mux
}->endloop() if !$running;
192 # execute all queued command
194 my ($self, $timeout) = @_;
196 $timeout = 3 if !$timeout;
198 $self->{current
} = {};
199 $self->{errors
} = {};
201 # open all necessary connections
202 foreach my $vmid (keys %{$self->{queue
}}) {
203 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
206 my $fh = &$open_connection($self, $vmid);
207 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
208 unshift @{$self->{queue
}->{$vmid}}, $cmd;
209 $self->{mux
}->set_timeout($fh, $timeout);
213 $self->{errors
}->{$vmid} = $err;
221 $running = &$check_queue($self);
228 # make sure we close everything
229 foreach my $vmid (keys %{$self->{fhs
}}) {
230 &$close_connection($self, $vmid);
233 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
236 # mux_input is called when input is available on one of
239 my ($self, $mux, $fh, $input) = @_;
241 return if $$input !~ m/}\r\n$/;
245 # Remove the input from the input buffer.
248 my $vmid = $self->{fhs_lookup
}->{$fh};
250 warn "internal error - unable to lookup vmid";
255 my @jsons = split("\n", $raw);
257 foreach my $json (@jsons) {
258 my $obj = from_json
($json);
259 next if defined($obj->{QMP
}); # skip monitor greeting
261 if (exists($obj->{error
}->{desc
})) {
262 my $desc = $obj->{error
}->{desc
};
264 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
268 if (defined($obj->{event
})) {
269 if (my $eventcb = $self->{eventcb
}) {
275 my $cmdid = $obj->{id
};
276 die "received responsed without command id\n" if !$cmdid;
278 my $curcmd = $self->{current
}->{$vmid};
279 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
281 delete $self->{current
}->{$vmid};
283 if ($curcmd->{id
} ne $cmdid) {
284 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
287 if (my $callback = $curcmd->{callback
}) {
288 &$callback($vmid, $obj);
293 $self->{errors
}->{$vmid} = $err;
296 &$check_queue($self);
299 # This gets called every second to update player info, etc...
301 my ($self, $mux, $fh) = @_;
303 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
304 $self->{errors
}->{$vmid} = "got timeout\n";
307 &$check_queue($self);