]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
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 and qga commands to different VMs in parallel.
20 # Note: qemu can onyl handle 1 connection, so we close connections asap
23 my ($class, $eventcb) = @_;
25 my $mux = new IO
::Multiplex
;
29 queue_lookup
=> {}, # $fh => $queue_info
33 $self->{eventcb
} = $eventcb if $eventcb;
35 $mux->set_callback_object($self);
37 # make sure perl doesn't believe this is a circular reference as we
38 # delete mux in DESTROY
39 weaken
($mux->{_object
});
44 # Note: List of special QGA command. Those commands can close the connection
45 # without sending a response.
47 my $qga_allow_close_cmds = {
48 'guest-shutdown' => 1,
49 'guest-suspend-ram' => 1,
50 'guest-suspend-disk' => 1,
51 'guest-suspend-hybrid' => 1,
54 my $push_cmd_to_queue = sub {
55 my ($self, $vmid, $cmd) = @_;
57 my $execute = $cmd->{execute
} || die "no command name specified";
59 my $qga = ($execute =~ /^guest\-+/) ?
1 : 0;
61 my $sname = PVE
::QemuServer
::qmp_socket
($vmid, $qga);
63 $self->{queue_info
}->{$sname} = { qga
=> $qga, vmid
=> $vmid, sname
=> $sname, cmds
=> [] }
64 if !$self->{queue_info
}->{$sname};
66 push @{$self->{queue_info
}->{$sname}->{cmds
}}, $cmd;
68 return $self->{queue_info
}->{$sname};
71 # add a single command to the queue for later execution
72 # with queue_execute()
74 my ($self, $vmid, $callback, $execute, %params) = @_;
77 $cmd->{execute
} = $execute;
78 $cmd->{arguments
} = \
%params;
79 $cmd->{callback
} = $callback;
81 &$push_cmd_to_queue($self, $vmid, $cmd);
86 # execute a single command
88 my ($self, $vmid, $cmd, $timeout) = @_;
93 my ($vmid, $resp) = @_;
94 $result = $resp->{'return'};
95 $result = { error
=> $resp->{'error'} } if !defined($result) && $resp->{'error'};
98 die "no command specified" if !($cmd && $cmd->{execute
});
100 $cmd->{callback
} = $callback;
101 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
103 my $queue_info = &$push_cmd_to_queue($self, $vmid, $cmd);
106 # hack: monitor sometime blocks
107 if ($cmd->{execute
} eq 'query-migrate') {
108 $timeout = 60*60; # 1 hour
109 } elsif ($cmd->{execute
} =~ m/^(eject|change)/) {
110 $timeout = 60; # note: cdrom mount command is slow
111 } elsif ($cmd->{execute
} eq 'guest-fsfreeze-freeze') {
112 # freeze syncs all guest FS, if we kill it it stays in an unfreezable
113 # locked state with high probability, so use an generous timeout
114 $timeout = 60*60; # 1 hour
115 } elsif ($cmd->{execute
} eq 'guest-fsfreeze-thaw') {
116 # thaw has no possible long blocking actions, either it returns
117 # instantly or never (dead locked)
119 } elsif ($cmd->{execute
} eq 'savevm-start' ||
120 $cmd->{execute
} eq 'savevm-end' ||
121 $cmd->{execute
} eq 'query-backup' ||
122 $cmd->{execute
} eq 'query-block-jobs' ||
123 $cmd->{execute
} eq 'block-job-cancel' ||
124 $cmd->{execute
} eq 'block-job-complete' ||
125 $cmd->{execute
} eq 'backup-cancel' ||
126 $cmd->{execute
} eq 'query-savevm' ||
127 $cmd->{execute
} eq 'delete-drive-snapshot' ||
128 $cmd->{execute
} eq 'guest-shutdown' ||
129 $cmd->{execute
} eq 'blockdev-snapshot-internal-sync' ||
130 $cmd->{execute
} eq 'blockdev-snapshot-delete-internal-sync' ||
131 $cmd->{execute
} eq 'snapshot-drive' ) {
132 $timeout = 10*60; # 10 mins ?
134 $timeout = 3; # default
138 $self->queue_execute($timeout, 2);
140 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
141 if defined($queue_info->{error
});
147 my $cmdid_seq_qga = 0;
149 my $next_cmdid = sub {
154 return "$$"."0".$cmdid_seq_qga;
157 return "$$:$cmdid_seq";
161 my $lookup_queue_info = sub {
162 my ($self, $fh, $noerr) = @_;
164 my $queue_info = $self->{queue_lookup
}->{$fh};
166 warn "internal error - unable to lookup queue info" if !$noerr;
172 my $close_connection = sub {
173 my ($self, $queue_info) = @_;
175 if (my $fh = delete $queue_info->{fh
}) {
176 delete $self->{queue_lookup
}->{$fh};
177 $self->{mux
}->close($fh);
181 my $open_connection = sub {
182 my ($self, $queue_info, $timeout) = @_;
184 die "duplicate call to open" if defined($queue_info->{fh
});
186 my $vmid = $queue_info->{vmid
};
187 my $qga = $queue_info->{qga
};
189 my $sname = PVE
::QemuServer
::qmp_socket
($vmid, $qga);
191 $timeout = 1 if !$timeout;
194 my $starttime = [gettimeofday
];
197 my $sotype = $qga ?
'qga' : 'qmp';
201 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
203 if ($! != EINTR
&& $! != EAGAIN
) {
204 die "unable to connect to VM $vmid $sotype socket - $!\n";
206 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
207 if ($elapsed >= $timeout) {
208 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
213 $queue_info->{fh
} = $fh;
215 $self->{queue_lookup
}->{$fh} = $queue_info;
217 $self->{mux
}->add($fh);
218 $self->{mux
}->set_timeout($fh, $timeout);
223 my $check_queue = sub {
228 foreach my $sname (keys %{$self->{queue_info
}}) {
229 my $queue_info = $self->{queue_info
}->{$sname};
230 my $fh = $queue_info->{fh
};
233 my $qga = $queue_info->{qga
};
235 if ($queue_info->{error
}) {
236 &$close_connection($self, $queue_info);
240 if ($queue_info->{current
}) { # command running, waiting for response
245 if (!scalar(@{$queue_info->{cmds
}})) { # no more commands
246 &$close_connection($self, $queue_info);
252 my $cmd = $queue_info->{current
} = shift @{$queue_info->{cmds
}};
253 $cmd->{id
} = &$next_cmdid($qga);
256 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
257 $fd = $cmd->{arguments
}->{fd
};
258 delete $cmd->{arguments
}->{fd
};
265 $qmpcmd = to_json
({ execute
=> 'guest-sync-delimited',
266 arguments
=> { id
=> int($cmd->{id
})}}) .
267 to_json
({ execute
=> $cmd->{execute
}, arguments
=> $cmd->{arguments
}});
272 execute
=> $cmd->{execute
},
273 arguments
=> $cmd->{arguments
},
278 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
279 die "sendfd failed" if $ret < 0;
281 $self->{mux
}->write($fh, $qmpcmd);
285 $queue_info->{error
} = $err;
291 $self->{mux
}->endloop() if !$running;
296 # execute all queued command
299 my ($self, $timeout, $noerr) = @_;
301 $timeout = 3 if !$timeout;
303 # open all necessary connections
304 foreach my $sname (keys %{$self->{queue_info
}}) {
305 my $queue_info = $self->{queue_info
}->{$sname};
306 next if !scalar(@{$queue_info->{cmds
}}); # no commands
308 $queue_info->{error
} = undef;
309 $queue_info->{current
} = undef;
312 &$open_connection($self, $queue_info, $timeout);
314 if (!$queue_info->{qga
}) {
315 my $cap_cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
316 unshift @{$queue_info->{cmds
}}, $cap_cmd;
320 $queue_info->{error
} = $err;
328 $running = &$check_queue($self);
335 # make sure we close everything
337 foreach my $sname (keys %{$self->{queue_info
}}) {
338 my $queue_info = $self->{queue_info
}->{$sname};
339 &$close_connection($self, $queue_info);
340 if ($queue_info->{error
}) {
342 warn $queue_info->{error
} if $noerr < 2;
344 $errors .= $queue_info->{error
}
349 $self->{queue_info
} = $self->{queue_lookup
} = {};
351 die $errors if $errors;
355 my ($self, $mux, $fh) = @_;
357 my $queue_info = &$lookup_queue_info($self, $fh, 1);
358 return if !$queue_info;
360 $queue_info->{error
} = "client closed connection\n"
361 if !$queue_info->{error
};
364 # mux_input is called when input is available on one of the descriptors.
366 my ($self, $mux, $fh, $input) = @_;
368 my $queue_info = &$lookup_queue_info($self, $fh);
369 return if !$queue_info;
371 my $sname = $queue_info->{sname
};
372 my $vmid = $queue_info->{vmid
};
373 my $qga = $queue_info->{qga
};
375 my $curcmd = $queue_info->{current
};
376 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
381 return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so;
384 return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so;
389 my @jsons = split("\n", $raw);
393 die "response is not complete" if @jsons != 2 ;
395 my $obj = from_json
($jsons[0]);
397 my $cmdid = $obj->{'return'};
398 die "received responsed without command id\n" if !$cmdid;
400 # skip results fro previous commands
401 return if $cmdid < $curcmd->{id
};
403 if ($curcmd->{id
} ne $cmdid) {
404 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
407 delete $queue_info->{current
};
409 $obj = from_json
($jsons[1]);
411 if (my $callback = $curcmd->{callback
}) {
412 &$callback($vmid, $obj);
418 foreach my $json (@jsons) {
419 my $obj = from_json
($json);
420 next if defined($obj->{QMP
}); # skip monitor greeting
422 if (exists($obj->{error
}->{desc
})) {
423 my $desc = $obj->{error
}->{desc
};
425 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
429 if (defined($obj->{event
})) {
430 if (my $eventcb = $self->{eventcb
}) {
436 my $cmdid = $obj->{id
};
437 die "received responsed without command id\n" if !$cmdid;
439 if ($curcmd->{id
} ne $cmdid) {
440 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
443 delete $queue_info->{current
};
445 if (my $callback = $curcmd->{callback
}) {
446 &$callback($vmid, $obj);
451 $queue_info->{error
} = $err;
454 &$check_queue($self);
457 # This gets called every second to update player info, etc...
459 my ($self, $mux, $fh) = @_;
461 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
462 $queue_info->{error
} = "got timeout\n";
463 $self->{mux
}->inbuffer($fh, ''); # clear to avoid warnings
466 &$check_queue($self);
470 my ($self, $mux, $fh, $input) = @_;
472 my $queue_info = &$lookup_queue_info($self, $fh);
473 return if !$queue_info;
475 my $sname = $queue_info->{sname
};
476 my $vmid = $queue_info->{vmid
};
477 my $qga = $queue_info->{qga
};
479 my $curcmd = $queue_info->{current
};
480 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
482 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute
}}) {
484 return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so;
489 my $obj = from_json
($raw);
491 my $cmdid = $obj->{'return'};
492 die "received responsed without command id\n" if !$cmdid;
494 delete $queue_info->{current
};
496 if (my $callback = $curcmd->{callback
}) {
497 &$callback($vmid, undef);
501 $queue_info->{error
} = $err;
504 &$close_connection($self, $queue_info);
506 if (scalar(@{$queue_info->{cmds
}}) && !$queue_info->{error
}) {
507 $queue_info->{error
} = "Got EOF but command queue is not empty.\n";