]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
45fcba4befad6ad252b53a3472aa41bc5e1d452a
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 'backup-cancel' ||
87 $cmd->{execute
} eq 'query-savevm' ||
88 $cmd->{execute
} eq 'delete-drive-snapshot' ||
89 $cmd->{execute
} eq 'snapshot-drive' ) {
90 $timeout = 10*60; # 10 mins ?
92 $timeout = 3; # default
96 $self->queue_execute($timeout);
98 my $cmdstr = $cmd->{execute
} || '';
99 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
100 if defined($self->{errors
}->{$vmid});
106 my $next_cmdid = sub {
108 return "$$:$cmdid_seq";
111 my $close_connection = sub {
112 my ($self, $vmid) = @_;
114 my $fh = $self->{fhs
}->{$vmid};
117 delete $self->{fhs
}->{$vmid};
118 delete $self->{fhs_lookup
}->{$fh};
120 $self->{mux
}->close($fh);
123 my $open_connection = sub {
124 my ($self, $vmid, $timeout) = @_;
126 my $sname = PVE
::QemuServer
::qmp_socket
($vmid);
128 $timeout = 1 if !$timeout;
131 my $starttime = [gettimeofday
];
135 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
137 if ($! != EINTR
&& $! != EAGAIN
) {
138 die "unable to connect to VM $vmid socket - $!\n";
140 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
141 if ($elapsed >= $timeout) {
142 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
147 $self->{fhs
}->{$vmid} = $fh;
148 $self->{fhs_lookup
}->{$fh} = $vmid;
149 $self->{mux
}->add($fh);
154 my $check_queue = sub {
159 foreach my $vmid (keys %{$self->{queue
}}) {
160 my $fh = $self->{fhs
}->{$vmid};
163 if ($self->{errors
}->{$vmid}) {
164 &$close_connection($self, $vmid);
168 if ($self->{current
}->{$vmid}) { # command running, waiting for response
173 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
174 &$close_connection($self, $vmid);
180 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
181 $cmd->{id
} = &$next_cmdid();
184 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
185 $fd = $cmd->{arguments
}->{fd
};
186 delete $cmd->{arguments
}->{fd
};
189 my $qmpcmd = to_json
({
190 execute
=> $cmd->{execute
},
191 arguments
=> $cmd->{arguments
},
195 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
196 die "sendfd failed" if $ret < 0;
198 $self->{mux
}->write($fh, $qmpcmd);
202 $self->{errors
}->{$vmid} = $err;
208 $self->{mux
}->endloop() if !$running;
213 # execute all queued command
215 my ($self, $timeout) = @_;
217 $timeout = 3 if !$timeout;
219 $self->{current
} = {};
220 $self->{errors
} = {};
222 # open all necessary connections
223 foreach my $vmid (keys %{$self->{queue
}}) {
224 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
227 my $fh = &$open_connection($self, $vmid, $timeout);
228 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
229 unshift @{$self->{queue
}->{$vmid}}, $cmd;
230 $self->{mux
}->set_timeout($fh, $timeout);
234 $self->{errors
}->{$vmid} = $err;
242 $running = &$check_queue($self);
249 # make sure we close everything
250 foreach my $vmid (keys %{$self->{fhs
}}) {
251 &$close_connection($self, $vmid);
254 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
257 # mux_input is called when input is available on one of
260 my ($self, $mux, $fh, $input) = @_;
262 return if $$input !~ s/^(.*})\r\n(.*)$/$2/so;
266 my $vmid = $self->{fhs_lookup
}->{$fh};
268 warn "internal error - unable to lookup vmid";
273 my @jsons = split("\n", $raw);
275 foreach my $json (@jsons) {
276 my $obj = from_json
($json);
277 next if defined($obj->{QMP
}); # skip monitor greeting
279 if (exists($obj->{error
}->{desc
})) {
280 my $desc = $obj->{error
}->{desc
};
282 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
286 if (defined($obj->{event
})) {
287 if (my $eventcb = $self->{eventcb
}) {
293 my $cmdid = $obj->{id
};
294 die "received responsed without command id\n" if !$cmdid;
296 my $curcmd = $self->{current
}->{$vmid};
297 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
299 delete $self->{current
}->{$vmid};
301 if ($curcmd->{id
} ne $cmdid) {
302 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
305 if (my $callback = $curcmd->{callback
}) {
306 &$callback($vmid, $obj);
311 $self->{errors
}->{$vmid} = $err;
314 &$check_queue($self);
317 # This gets called every second to update player info, etc...
319 my ($self, $mux, $fh) = @_;
321 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
322 $self->{errors
}->{$vmid} = "got timeout\n";
325 &$check_queue($self);