]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
648c51ae956214a5072c9e1138ec20ab40663608
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, $qga) = @_;
25 my $mux = new IO
::Multiplex
;
29 fhs
=> {}, # $vmid => fh
30 fhs_lookup
=> {}, # $fh => $vmid
36 $self->{eventcb
} = $eventcb if $eventcb;
37 $self->{qga
} = $qga if $qga;
39 $mux->set_callback_object($self);
41 # make sure perl doesn't believe this is a circular reference as we
42 # delete mux in DESTROY
43 weaken
($mux->{_object
});
48 # add a single command to the queue for later execution
49 # with queue_execute()
51 my ($self, $vmid, $callback, $execute, %params) = @_;
54 $cmd->{execute
} = $execute;
55 $cmd->{arguments
} = \
%params;
56 $cmd->{callback
} = $callback;
58 push @{$self->{queue
}->{$vmid}}, $cmd;
61 # execute a single command
63 my ($self, $vmid, $cmd, $timeout) = @_;
68 my ($vmid, $resp) = @_;
69 $result = $resp->{'return'};
72 die "no command specified" if !($cmd && $cmd->{execute
});
74 $cmd->{callback
} = $callback;
75 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
77 $self->{queue
}->{$vmid} = [ $cmd ];
80 # hack: monitor sometime blocks
81 if ($cmd->{execute
} eq 'query-migrate') {
82 $timeout = 60*60; # 1 hour
83 } elsif ($cmd->{execute
} =~ m/^(eject|change)/) {
84 $timeout = 60; # note: cdrom mount command is slow
85 } elsif ($cmd->{execute
} eq 'savevm-start' ||
86 $cmd->{execute
} eq 'savevm-end' ||
87 $cmd->{execute
} eq 'query-backup' ||
88 $cmd->{execute
} eq 'query-block-jobs' ||
89 $cmd->{execute
} eq 'backup-cancel' ||
90 $cmd->{execute
} eq 'query-savevm' ||
91 $cmd->{execute
} eq 'delete-drive-snapshot' ||
92 $cmd->{execute
} eq 'snapshot-drive' ) {
93 $timeout = 10*60; # 10 mins ?
95 $timeout = 3; # default
99 $self->queue_execute($timeout);
101 my $cmdstr = $cmd->{execute
} || '';
102 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
103 if defined($self->{errors
}->{$vmid});
109 my $next_cmdid = sub {
111 return "$$"."0".$cmdid_seq;
114 my $close_connection = sub {
115 my ($self, $vmid) = @_;
117 my $fh = $self->{fhs
}->{$vmid};
120 delete $self->{fhs
}->{$vmid};
121 delete $self->{fhs_lookup
}->{$fh};
123 $self->{mux
}->close($fh);
126 my $open_connection = sub {
127 my ($self, $vmid, $timeout) = @_;
129 my $sname = PVE
::QemuServer
::qmp_socket
($vmid, $self->{qga
});
131 $timeout = 1 if !$timeout;
134 my $starttime = [gettimeofday
];
138 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
140 if ($! != EINTR
&& $! != EAGAIN
) {
141 die "unable to connect to VM $vmid socket - $!\n";
143 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
144 if ($elapsed >= $timeout) {
145 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
150 $self->{fhs
}->{$vmid} = $fh;
151 $self->{fhs_lookup
}->{$fh} = $vmid;
152 $self->{mux
}->add($fh);
157 my $check_queue = sub {
162 foreach my $vmid (keys %{$self->{queue
}}) {
163 my $fh = $self->{fhs
}->{$vmid};
166 if ($self->{errors
}->{$vmid}) {
167 &$close_connection($self, $vmid);
171 if ($self->{current
}->{$vmid}) { # command running, waiting for response
176 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
177 &$close_connection($self, $vmid);
183 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
184 $cmd->{id
} = &$next_cmdid();
187 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
188 $fd = $cmd->{arguments
}->{fd
};
189 delete $cmd->{arguments
}->{fd
};
192 my $qmpcmd = to_json
({
193 execute
=> $cmd->{execute
},
194 arguments
=> $cmd->{arguments
},
198 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
199 die "sendfd failed" if $ret < 0;
201 $self->{mux
}->write($fh, $qmpcmd);
205 $self->{errors
}->{$vmid} = $err;
211 $self->{mux
}->endloop() if !$running;
216 # execute all queued command
218 my ($self, $timeout) = @_;
220 $timeout = 3 if !$timeout;
222 $self->{current
} = {};
223 $self->{errors
} = {};
225 # open all necessary connections
226 foreach my $vmid (keys %{$self->{queue
}}) {
227 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
230 my $fh = &$open_connection($self, $vmid, $timeout);
231 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
232 unshift @{$self->{queue
}->{$vmid}}, $cmd;
233 $self->{mux
}->set_timeout($fh, $timeout);
237 $self->{errors
}->{$vmid} = $err;
245 $running = &$check_queue($self);
252 # make sure we close everything
253 foreach my $vmid (keys %{$self->{fhs
}}) {
254 &$close_connection($self, $vmid);
257 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
261 my ($self, $mux, $fh) = @_;
263 my $vmid = $self->{fhs_lookup
}->{$fh} || 'undef';
264 return if !defined($vmid);
266 $self->{errors
}->{$vmid} = "client closed connection\n" if !$self->{errors
}->{$vmid};
269 # mux_input is called when input is available on one of
272 my ($self, $mux, $fh, $input) = @_;
274 return if $$input !~ s/^(.*})\r\n(.*)$/$2/so;
278 my $vmid = $self->{fhs_lookup
}->{$fh};
280 warn "internal error - unable to lookup vmid";
285 my @jsons = split("\n", $raw);
287 foreach my $json (@jsons) {
288 my $obj = from_json
($json);
289 next if defined($obj->{QMP
}); # skip monitor greeting
291 if (exists($obj->{error
}->{desc
})) {
292 my $desc = $obj->{error
}->{desc
};
294 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
298 if (defined($obj->{event
})) {
299 if (my $eventcb = $self->{eventcb
}) {
305 my $cmdid = $obj->{id
};
306 die "received responsed without command id\n" if !$cmdid;
308 my $curcmd = $self->{current
}->{$vmid};
309 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
311 delete $self->{current
}->{$vmid};
313 if ($curcmd->{id
} ne $cmdid) {
314 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
317 if (my $callback = $curcmd->{callback
}) {
318 &$callback($vmid, $obj);
323 $self->{errors
}->{$vmid} = $err;
326 &$check_queue($self);
329 # This gets called every second to update player info, etc...
331 my ($self, $mux, $fh) = @_;
333 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
334 $self->{errors
}->{$vmid} = "got timeout\n";
337 &$check_queue($self);