]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
1 package PVE
::QMPClient
;
6 use POSIX
qw(EINTR EAGAIN);
8 use Time
::HiRes
qw(usleep gettimeofday tv_interval);
9 use Scalar
::Util
qw(weaken);
14 # Qemu Monitor Protocol (QMP) client.
16 # This implementation uses IO::Multiplex (libio-multiplex-perl) and
17 # allows you to issue qmp commands to different VMs in parallel.
19 # Note: kvm can onyl handle 1 connection, so we close connections asap
22 my ($class, $eventcb) = @_;
24 my $mux = new IO
::Multiplex
;
28 fhs
=> {}, # $vmid => fh
29 fhs_lookup
=> {}, # $fh => $vmid
35 $self->{eventcb
} = $eventcb if $eventcb;
37 $mux->set_callback_object($self);
39 # make sure perl doesn't believe this is a circular reference as we
40 # delete mux in DESTROY
41 weaken
($mux->{_object
});
46 # add a single command to the queue for later execution
47 # with queue_execute()
49 my ($self, $vmid, $callback, $execute, %params) = @_;
52 $cmd->{execute
} = $execute;
53 $cmd->{arguments
} = \
%params;
54 $cmd->{callback
} = $callback;
56 push @{$self->{queue
}->{$vmid}}, $cmd;
59 # execute a single command
61 my ($self, $vmid, $cmd, $timeout) = @_;
66 my ($vmid, $resp) = @_;
67 $result = $resp->{'return'};
70 die "no command specified" if !($cmd && $cmd->{execute
});
72 $cmd->{callback
} = $callback;
73 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
75 $self->{queue
}->{$vmid} = [ $cmd ];
78 # hack: monitor sometime blocks
79 if ($cmd->{execute
} eq 'query-migrate') {
80 $timeout = 60*60; # 1 hour
81 } elsif ($cmd->{execute
} =~ m/^(eject|change)/) {
82 $timeout = 60; # note: cdrom mount command is slow
83 } elsif ($cmd->{execute
} eq 'savevm-start' ||
84 $cmd->{execute
} eq 'savevm-end' ||
85 $cmd->{execute
} eq 'query-backup' ||
86 $cmd->{execute
} eq 'query-block-jobs' ||
87 $cmd->{execute
} eq 'backup-cancel' ||
88 $cmd->{execute
} eq 'query-savevm' ||
89 $cmd->{execute
} eq 'delete-drive-snapshot' ||
90 $cmd->{execute
} eq 'snapshot-drive' ) {
91 $timeout = 10*60; # 10 mins ?
93 $timeout = 3; # default
97 $self->queue_execute($timeout);
99 my $cmdstr = $cmd->{execute
} || '';
100 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
101 if defined($self->{errors
}->{$vmid});
107 my $next_cmdid = sub {
109 return "$$:$cmdid_seq";
112 my $close_connection = sub {
113 my ($self, $vmid) = @_;
115 my $fh = $self->{fhs
}->{$vmid};
118 delete $self->{fhs
}->{$vmid};
119 delete $self->{fhs_lookup
}->{$fh};
121 $self->{mux
}->close($fh);
124 my $open_connection = sub {
125 my ($self, $vmid, $timeout) = @_;
127 my $sname = PVE
::QemuServer
::qmp_socket
($vmid);
129 $timeout = 1 if !$timeout;
132 my $starttime = [gettimeofday
];
136 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
138 if ($! != EINTR
&& $! != EAGAIN
) {
139 die "unable to connect to VM $vmid socket - $!\n";
141 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
142 if ($elapsed >= $timeout) {
143 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
148 $self->{fhs
}->{$vmid} = $fh;
149 $self->{fhs_lookup
}->{$fh} = $vmid;
150 $self->{mux
}->add($fh);
155 my $check_queue = sub {
160 foreach my $vmid (keys %{$self->{queue
}}) {
161 my $fh = $self->{fhs
}->{$vmid};
164 if ($self->{errors
}->{$vmid}) {
165 &$close_connection($self, $vmid);
169 if ($self->{current
}->{$vmid}) { # command running, waiting for response
174 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
175 &$close_connection($self, $vmid);
181 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
182 $cmd->{id
} = &$next_cmdid();
185 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
186 $fd = $cmd->{arguments
}->{fd
};
187 delete $cmd->{arguments
}->{fd
};
190 my $qmpcmd = to_json
({
191 execute
=> $cmd->{execute
},
192 arguments
=> $cmd->{arguments
},
196 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
197 die "sendfd failed" if $ret < 0;
199 $self->{mux
}->write($fh, $qmpcmd);
203 $self->{errors
}->{$vmid} = $err;
209 $self->{mux
}->endloop() if !$running;
214 # execute all queued command
216 my ($self, $timeout) = @_;
218 $timeout = 3 if !$timeout;
220 $self->{current
} = {};
221 $self->{errors
} = {};
223 # open all necessary connections
224 foreach my $vmid (keys %{$self->{queue
}}) {
225 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
228 my $fh = &$open_connection($self, $vmid, $timeout);
229 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
230 unshift @{$self->{queue
}->{$vmid}}, $cmd;
231 $self->{mux
}->set_timeout($fh, $timeout);
235 $self->{errors
}->{$vmid} = $err;
243 $running = &$check_queue($self);
250 # make sure we close everything
251 foreach my $vmid (keys %{$self->{fhs
}}) {
252 &$close_connection($self, $vmid);
255 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
259 my ($self, $mux, $fh) = @_;
261 my $vmid = $self->{fhs_lookup
}->{$fh} || 'undef';
262 return if !defined($vmid);
264 $self->{errors
}->{$vmid} = "client closed connection\n" if !$self->{errors
}->{$vmid};
267 # mux_input is called when input is available on one of
270 my ($self, $mux, $fh, $input) = @_;
272 return if $$input !~ s/^(.*})\r\n(.*)$/$2/so;
276 my $vmid = $self->{fhs_lookup
}->{$fh};
278 warn "internal error - unable to lookup vmid";
283 my @jsons = split("\n", $raw);
285 foreach my $json (@jsons) {
286 my $obj = from_json
($json);
287 next if defined($obj->{QMP
}); # skip monitor greeting
289 if (exists($obj->{error
}->{desc
})) {
290 my $desc = $obj->{error
}->{desc
};
292 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
296 if (defined($obj->{event
})) {
297 if (my $eventcb = $self->{eventcb
}) {
303 my $cmdid = $obj->{id
};
304 die "received responsed without command id\n" if !$cmdid;
306 my $curcmd = $self->{current
}->{$vmid};
307 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
309 delete $self->{current
}->{$vmid};
311 if ($curcmd->{id
} ne $cmdid) {
312 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
315 if (my $callback = $curcmd->{callback
}) {
316 &$callback($vmid, $obj);
321 $self->{errors
}->{$vmid} = $err;
324 &$check_queue($self);
327 # This gets called every second to update player info, etc...
329 my ($self, $mux, $fh) = @_;
331 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
332 $self->{errors
}->{$vmid} = "got timeout\n";
335 &$check_queue($self);