]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
9829986ae77e82d340974e4d4128741ef85b4a0e
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 'savevm-start' ||
79 $cmd->{execute
} eq 'savevm-end' ||
80 $cmd->{execute
} eq 'query-savevm' ||
81 $cmd->{execute
} eq 'delete-drive-snapshot' ||
82 $cmd->{execute
} eq 'snapshot-drive' ) {
83 $timeout = 10*60; # 10 mins ?
85 $timeout = 3; # default
89 $self->queue_execute($timeout);
91 my $cmdstr = $cmd->{execute
} || '';
92 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
93 if defined($self->{errors
}->{$vmid});
99 my $next_cmdid = sub {
101 return "$$:$cmdid_seq";
104 my $close_connection = sub {
105 my ($self, $vmid) = @_;
107 my $fh = $self->{fhs
}->{$vmid};
110 delete $self->{fhs
}->{$vmid};
111 delete $self->{fhs_lookup
}->{$fh};
113 $self->{mux
}->close($fh);
116 my $open_connection = sub {
117 my ($self, $vmid, $timeout) = @_;
119 my $sname = PVE
::QemuServer
::qmp_socket
($vmid);
121 $timeout = 1 if !$timeout;
124 my $starttime = [gettimeofday
];
128 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
130 if ($! != EINTR
&& $! != EAGAIN
) {
131 die "unable to connect to VM $vmid socket - $!\n";
133 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
134 if ($elapsed >= $timeout) {
135 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
140 $self->{fhs
}->{$vmid} = $fh;
141 $self->{fhs_lookup
}->{$fh} = $vmid;
142 $self->{mux
}->add($fh);
147 my $check_queue = sub {
152 foreach my $vmid (keys %{$self->{queue
}}) {
153 my $fh = $self->{fhs
}->{$vmid};
156 if ($self->{errors
}->{$vmid}) {
157 &$close_connection($self, $vmid);
161 if ($self->{current
}->{$vmid}) { # command running, waiting for response
166 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
167 &$close_connection($self, $vmid);
173 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
174 $cmd->{id
} = &$next_cmdid();
176 my $qmpcmd = to_json
({
177 execute
=> $cmd->{execute
},
178 arguments
=> $cmd->{arguments
},
181 $self->{mux
}->write($fh, $qmpcmd);
184 $self->{errors
}->{$vmid} = $err;
190 $self->{mux
}->endloop() if !$running;
195 # execute all queued command
197 my ($self, $timeout) = @_;
199 $timeout = 3 if !$timeout;
201 $self->{current
} = {};
202 $self->{errors
} = {};
204 # open all necessary connections
205 foreach my $vmid (keys %{$self->{queue
}}) {
206 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
209 my $fh = &$open_connection($self, $vmid, $timeout);
210 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
211 unshift @{$self->{queue
}->{$vmid}}, $cmd;
212 $self->{mux
}->set_timeout($fh, $timeout);
216 $self->{errors
}->{$vmid} = $err;
224 $running = &$check_queue($self);
231 # make sure we close everything
232 foreach my $vmid (keys %{$self->{fhs
}}) {
233 &$close_connection($self, $vmid);
236 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
239 # mux_input is called when input is available on one of
242 my ($self, $mux, $fh, $input) = @_;
244 return if $$input !~ m/}\r\n$/;
248 # Remove the input from the input buffer.
251 my $vmid = $self->{fhs_lookup
}->{$fh};
253 warn "internal error - unable to lookup vmid";
258 my @jsons = split("\n", $raw);
260 foreach my $json (@jsons) {
261 my $obj = from_json
($json);
262 next if defined($obj->{QMP
}); # skip monitor greeting
264 if (exists($obj->{error
}->{desc
})) {
265 my $desc = $obj->{error
}->{desc
};
267 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
271 if (defined($obj->{event
})) {
272 if (my $eventcb = $self->{eventcb
}) {
278 my $cmdid = $obj->{id
};
279 die "received responsed without command id\n" if !$cmdid;
281 my $curcmd = $self->{current
}->{$vmid};
282 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
284 delete $self->{current
}->{$vmid};
286 if ($curcmd->{id
} ne $cmdid) {
287 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
290 if (my $callback = $curcmd->{callback
}) {
291 &$callback($vmid, $obj);
296 $self->{errors
}->{$vmid} = $err;
299 &$check_queue($self);
302 # This gets called every second to update player info, etc...
304 my ($self, $mux, $fh) = @_;
306 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
307 $self->{errors
}->{$vmid} = "got timeout\n";
310 &$check_queue($self);