]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
51f7c185d6f5821d8c6d8bc1ef12a143518f82f9
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
};
196 my $qmpcmdid =to_json
({
197 execute
=> 'guest-sync',
198 arguments
=> { id
=> int($cmd->{id
})}});
201 execute
=> $cmd->{execute
},
202 arguments
=> $cmd->{arguments
}});
204 $qmpcmd = $qmpcmdid.$qmpcmd;
209 execute
=> $cmd->{execute
},
210 arguments
=> $cmd->{arguments
},
215 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
216 die "sendfd failed" if $ret < 0;
218 $self->{mux
}->write($fh, $qmpcmd);
222 $self->{errors
}->{$vmid} = $err;
228 $self->{mux
}->endloop() if !$running;
233 # execute all queued command
235 my ($self, $timeout) = @_;
237 $timeout = 3 if !$timeout;
239 $self->{current
} = {};
240 $self->{errors
} = {};
242 # open all necessary connections
243 foreach my $vmid (keys %{$self->{queue
}}) {
244 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
247 my $fh = &$open_connection($self, $vmid, $timeout);
250 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
251 unshift @{$self->{queue
}->{$vmid}}, $cmd;
254 $self->{mux
}->set_timeout($fh, $timeout);
258 $self->{errors
}->{$vmid} = $err;
266 $running = &$check_queue($self);
273 # make sure we close everything
274 foreach my $vmid (keys %{$self->{fhs
}}) {
275 &$close_connection($self, $vmid);
278 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
282 my ($self, $mux, $fh) = @_;
284 my $vmid = $self->{fhs_lookup
}->{$fh} || 'undef';
285 return if !defined($vmid);
287 $self->{errors
}->{$vmid} = "client closed connection\n" if !$self->{errors
}->{$vmid};
290 # mux_input is called when input is available on one of
293 my ($self, $mux, $fh, $input) = @_;
298 return if $$input !~ s/^([^\n]+}\n[^\n]+})\n(.*)$/$2/so;
301 return if $$input !~ s/^([^\n]+})\r?\n(.*)$/$2/so;
305 my $vmid = $self->{fhs_lookup
}->{$fh};
307 warn "internal error - unable to lookup vmid";
312 my @jsons = split("\n", $raw);
316 die "response is not complete" if @jsons != 2 ;
318 my $obj = from_json
($jsons[0]);
319 my $cmdid = $obj->{return};
320 die "received responsed without command id\n" if !$cmdid;
322 my $curcmd = $self->{current
}->{$vmid};
323 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
325 delete $self->{current
}->{$vmid};
327 if ($curcmd->{id
} ne $cmdid) {
328 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
331 $obj = from_json
($jsons[1]);
333 if (my $callback = $curcmd->{callback
}) {
334 &$callback($vmid, $obj);
340 foreach my $json (@jsons) {
341 my $obj = from_json
($json);
342 next if defined($obj->{QMP
}); # skip monitor greeting
344 if (exists($obj->{error
}->{desc
})) {
345 my $desc = $obj->{error
}->{desc
};
347 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
351 if (defined($obj->{event
})) {
352 if (my $eventcb = $self->{eventcb
}) {
358 my $cmdid = $obj->{id
};
359 die "received responsed without command id\n" if !$cmdid;
361 my $curcmd = $self->{current
}->{$vmid};
362 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
364 delete $self->{current
}->{$vmid};
366 if ($curcmd->{id
} ne $cmdid) {
367 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
370 if (my $callback = $curcmd->{callback
}) {
371 &$callback($vmid, $obj);
376 $self->{errors
}->{$vmid} = $err;
379 &$check_queue($self);
382 # This gets called every second to update player info, etc...
384 my ($self, $mux, $fh) = @_;
386 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
387 $self->{errors
}->{$vmid} = "got timeout\n";
390 &$check_queue($self);