]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
d026f4d4c3012203d96660a311b1890e84e6aa18
1 package PVE
::QMPClient
;
7 use POSIX
qw(EINTR EAGAIN);
9 use Time
::HiRes
qw(usleep gettimeofday tv_interval);
13 # Qemu Monitor Protocol (QMP) client.
15 # This implementation uses IO::Multiplex (libio-multiplex-perl) and
16 # allows you to issue qmp commands to different VMs in parallel.
18 # Note: kvm can onyl handle 1 connection, so we close connections asap
21 my ($class, $eventcb) = @_;
23 my $mux = new IO
::Multiplex
;
27 fhs
=> {}, # $vmid => fh
28 fhs_lookup
=> {}, # $fh => $vmid
34 $self->{eventcb
} = $eventcb if $eventcb;
36 $mux->set_callback_object($self);
41 # add a single command to the queue for later execution
42 # with queue_execute()
44 my ($self, $vmid, $callback, $execute, %params) = @_;
47 $cmd->{execute
} = $execute;
48 $cmd->{arguments
} = \
%params;
49 $cmd->{callback
} = $callback;
51 push @{$self->{queue
}->{$vmid}}, $cmd;
54 # execute a single command
56 my ($self, $vmid, $cmd, $timeout) = @_;
61 my ($vmid, $resp) = @_;
62 $result = $resp->{'return'};
65 die "no command specified" if !($cmd && $cmd->{execute
});
67 $cmd->{callback
} = $callback;
68 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
70 $self->{queue
}->{$vmid} = [ $cmd ];
73 # hack: monitor sometime blocks
74 if ($cmd->{execute
} eq 'query-migrate') {
75 $timeout = 60*60; # 1 hour
76 } elsif ($cmd->{execute
} =~ m/^(eject|change)/) {
77 $timeout = 60; # note: cdrom mount command is slow
78 } elsif ($cmd->{execute
} eq 'savevm-start' ||
79 $cmd->{execute
} eq 'savevm-end' ||
80 $cmd->{execute
} eq 'query-savevm' ||
81 $cmd->{execute
} eq 'delete-drive-snapshot' ||
82 $cmd->{execute
} eq 'snapshot-drive' ) {
83 $timeout = 10*60; # 10 mins ?
85 $timeout = 3; # default
89 $self->queue_execute($timeout);
91 my $cmdstr = $cmd->{execute
} || '';
92 die "VM $vmid qmp command '$cmdstr' failed - $self->{errors}->{$vmid}"
93 if defined($self->{errors
}->{$vmid});
99 my $next_cmdid = sub {
101 return "$$:$cmdid_seq";
104 my $close_connection = sub {
105 my ($self, $vmid) = @_;
107 my $fh = $self->{fhs
}->{$vmid};
110 delete $self->{fhs
}->{$vmid};
111 delete $self->{fhs_lookup
}->{$fh};
113 $self->{mux
}->close($fh);
116 my $open_connection = sub {
117 my ($self, $vmid) = @_;
119 my $sname = PVE
::QemuServer
::qmp_socket
($vmid);
122 my $starttime = [gettimeofday
];
126 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
128 if ($! != EINTR
&& $! != EAGAIN
) {
129 die "unable to connect to VM $vmid socket - $!\n";
131 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
133 die "unable to connect to VM $vmid socket - timeout after $count retries\n";
138 $self->{fhs
}->{$vmid} = $fh;
139 $self->{fhs_lookup
}->{$fh} = $vmid;
140 $self->{mux
}->add($fh);
145 my $check_queue = sub {
150 foreach my $vmid (keys %{$self->{queue
}}) {
151 my $fh = $self->{fhs
}->{$vmid};
154 if ($self->{errors
}->{$vmid}) {
155 &$close_connection($self, $vmid);
159 if ($self->{current
}->{$vmid}) { # command running, waiting for response
164 if (!scalar(@{$self->{queue
}->{$vmid}})) { # no more commands for the VM
165 &$close_connection($self, $vmid);
171 my $cmd = $self->{current
}->{$vmid} = shift @{$self->{queue
}->{$vmid}};
172 $cmd->{id
} = &$next_cmdid();
174 my $qmpcmd = to_json
({
175 execute
=> $cmd->{execute
},
176 arguments
=> $cmd->{arguments
},
179 $self->{mux
}->write($fh, $qmpcmd);
182 $self->{errors
}->{$vmid} = $err;
188 $self->{mux
}->endloop() if !$running;
193 # execute all queued command
195 my ($self, $timeout) = @_;
197 $timeout = 3 if !$timeout;
199 $self->{current
} = {};
200 $self->{errors
} = {};
202 # open all necessary connections
203 foreach my $vmid (keys %{$self->{queue
}}) {
204 next if !scalar(@{$self->{queue
}->{$vmid}}); # no commands for the VM
207 my $fh = &$open_connection($self, $vmid);
208 my $cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
209 unshift @{$self->{queue
}->{$vmid}}, $cmd;
210 $self->{mux
}->set_timeout($fh, $timeout);
214 $self->{errors
}->{$vmid} = $err;
222 $running = &$check_queue($self);
229 # make sure we close everything
230 foreach my $vmid (keys %{$self->{fhs
}}) {
231 &$close_connection($self, $vmid);
234 $self->{queue
} = $self->{current
} = $self->{fhs
} = $self->{fhs_lookup
} = {};
237 # mux_input is called when input is available on one of
240 my ($self, $mux, $fh, $input) = @_;
242 return if $$input !~ m/}\r\n$/;
246 # Remove the input from the input buffer.
249 my $vmid = $self->{fhs_lookup
}->{$fh};
251 warn "internal error - unable to lookup vmid";
256 my @jsons = split("\n", $raw);
258 foreach my $json (@jsons) {
259 my $obj = from_json
($json);
260 next if defined($obj->{QMP
}); # skip monitor greeting
262 if (exists($obj->{error
}->{desc
})) {
263 my $desc = $obj->{error
}->{desc
};
265 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
269 if (defined($obj->{event
})) {
270 if (my $eventcb = $self->{eventcb
}) {
276 my $cmdid = $obj->{id
};
277 die "received responsed without command id\n" if !$cmdid;
279 my $curcmd = $self->{current
}->{$vmid};
280 die "unable to lookup current command for VM $vmid\n" if !$curcmd;
282 delete $self->{current
}->{$vmid};
284 if ($curcmd->{id
} ne $cmdid) {
285 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
288 if (my $callback = $curcmd->{callback
}) {
289 &$callback($vmid, $obj);
294 $self->{errors
}->{$vmid} = $err;
297 &$check_queue($self);
300 # This gets called every second to update player info, etc...
302 my ($self, $mux, $fh) = @_;
304 if (my $vmid = $self->{fhs_lookup
}->{$fh}) {
305 $self->{errors
}->{$vmid} = "got timeout\n";
308 &$check_queue($self);