]>
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);
10 use Scalar
::Util
qw(weaken);
15 # Qemu Monitor Protocol (QMP) client.
17 # This implementation uses IO::Multiplex (libio-multiplex-perl) and
18 # allows you to issue qmp commands to different VMs in parallel.
20 # Note: kvm can onyl handle 1 connection, so we close connections asap
23 my ($class, $eventcb) = @_;
25 my $mux = new IO
::Multiplex
;
29 fhs
=> {}, # $vmid => fh
30 fhs_lookup
=> {}, # $fh => $vmid
36 $self->{eventcb
} = $eventcb if $eventcb;
38 $mux->set_callback_object($self);
40 # make sure perl doesn't believe this is a circular reference as we
41 # delete mux in DESTROY
42 weaken
($mux->{_object
});
47 # add a single command to the queue for later execution
48 # with queue_execute()
50 my ($self, $vmid, $callback, $execute, %params) = @_;
53 $cmd->{execute
} = $execute;
54 $cmd->{arguments
} = \
%params;
55 $cmd->{callback
} = $callback;
57 push @{$self->{queue
}->{$vmid}}, $cmd;
60 # execute a single command
62 my ($self, $vmid, $cmd, $timeout) = @_;
67 my ($vmid, $resp) = @_;
68 $result = $resp->{'return'};
71 die "no command specified" if !($cmd && $cmd->{execute
});
73 $cmd->{callback
} = $callback;
74 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
76 $self->{queue
}->{$vmid} = [ $cmd ];
79 # hack: monitor sometime blocks
80 if ($cmd->{execute
} eq 'query-migrate') {
81 $timeout = 60*60; # 1 hour
82 } elsif ($cmd->{execute
} =~ m/^(eject|change)/) {
83 $timeout = 60; # note: cdrom mount command is slow
84 } elsif ($cmd->{execute
} eq 'savevm-start' ||
85 $cmd->{execute
} eq 'savevm-end' ||
86 $cmd->{execute
} eq 'query-backup' ||
87 $cmd->{execute
} eq 'query-block-jobs' ||
88 $cmd->{execute
} eq 'backup-cancel' ||
89 $cmd->{execute
} eq 'query-savevm' ||
90 $cmd->{execute
} eq 'delete-drive-snapshot' ||
91 $cmd->{execute
} eq 'snapshot-drive' ) {
92 $timeout = 10*60; # 10 mins ?
94 $timeout = 3; # default
98 $self->queue_execute($timeout);
100 my $cmdstr = $cmd->{execute
} || '';
101 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
102 if defined($self->{errors
}->{$vmid});
108 my $next_cmdid = sub {
110 return "$$:$cmdid_seq";
113 my $close_connection = sub {
114 my ($self, $vmid) = @_;
116 my $fh = $self->{fhs
}->{$vmid};
119 delete $self->{fhs
}->{$vmid};
120 delete $self->{fhs_lookup
}->{$fh};
122 $self->{mux
}->close($fh);
125 my $open_connection = sub {
126 my ($self, $vmid, $timeout) = @_;
128 my $sname = PVE
::QemuServer
::qmp_socket
($vmid);
130 $timeout = 1 if !$timeout;
133 my $starttime = [gettimeofday
];
137 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
139 if ($! != EINTR
&& $! != EAGAIN
) {
140 die "unable to connect to VM $vmid socket - $!\n";
142 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
143 if ($elapsed >= $timeout) {
144 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
149 $self->{fhs
}->{$vmid} = $fh;
150 $self->{fhs_lookup
}->{$fh} = $vmid;
151 $self->{mux
}->add($fh);
156 my $check_queue = sub {
161 foreach my $vmid (keys %{$self->{queue
}}) {
162 my $fh = $self->{fhs
}->{$vmid};
165 if ($self->{errors
}->{$vmid}) {
166 &$close_connection($self, $vmid);
170 if ($self->{current
}->{$vmid}) { # command running, waiting for response
175 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
176 &$close_connection($self, $vmid);
182 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
183 $cmd->{id
} = &$next_cmdid();
186 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
187 $fd = $cmd->{arguments
}->{fd
};
188 delete $cmd->{arguments
}->{fd
};
191 my $qmpcmd = to_json
({
192 execute
=> $cmd->{execute
},
193 arguments
=> $cmd->{arguments
},
197 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
198 die "sendfd failed" if $ret < 0;
200 $self->{mux
}->write($fh, $qmpcmd);
204 $self->{errors
}->{$vmid} = $err;
210 $self->{mux
}->endloop() if !$running;
215 # execute all queued command
217 my ($self, $timeout) = @_;
219 $timeout = 3 if !$timeout;
221 $self->{current
} = {};
222 $self->{errors
} = {};
224 # open all necessary connections
225 foreach my $vmid (keys %{$self->{queue
}}) {
226 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
229 my $fh = &$open_connection($self, $vmid, $timeout);
230 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
231 unshift @{$self->{queue
}->{$vmid}}, $cmd;
232 $self->{mux
}->set_timeout($fh, $timeout);
236 $self->{errors
}->{$vmid} = $err;
244 $running = &$check_queue($self);
251 # make sure we close everything
252 foreach my $vmid (keys %{$self->{fhs
}}) {
253 &$close_connection($self, $vmid);
256 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
260 my ($self, $mux, $fh) = @_;
262 my $vmid = $self->{fhs_lookup
}->{$fh} || 'undef';
263 return if !defined($vmid);
265 $self->{errors
}->{$vmid} = "client closed connection\n" if !$self->{errors
}->{$vmid};
268 # mux_input is called when input is available on one of
271 my ($self, $mux, $fh, $input) = @_;
273 return if $$input !~ s/^(.*})\r\n(.*)$/$2/so;
277 my $vmid = $self->{fhs_lookup
}->{$fh};
279 warn "internal error - unable to lookup vmid";
284 my @jsons = split("\n", $raw);
286 foreach my $json (@jsons) {
287 my $obj = from_json
($json);
288 next if defined($obj->{QMP
}); # skip monitor greeting
290 if (exists($obj->{error
}->{desc
})) {
291 my $desc = $obj->{error
}->{desc
};
293 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
297 if (defined($obj->{event
})) {
298 if (my $eventcb = $self->{eventcb
}) {
304 my $cmdid = $obj->{id
};
305 die "received responsed without command id\n" if !$cmdid;
307 my $curcmd = $self->{current
}->{$vmid};
308 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
310 delete $self->{current
}->{$vmid};
312 if ($curcmd->{id
} ne $cmdid) {
313 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
316 if (my $callback = $curcmd->{callback
}) {
317 &$callback($vmid, $obj);
322 $self->{errors
}->{$vmid} = $err;
325 &$check_queue($self);
328 # This gets called every second to update player info, etc...
330 my ($self, $mux, $fh) = @_;
332 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
333 $self->{errors
}->{$vmid} = "got timeout\n";
336 &$check_queue($self);