]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QMPClient.pm
1 package PVE
::QMPClient
;
8 use POSIX
qw(EINTR EAGAIN);
9 use Scalar
::Util
qw(weaken);
10 use Time
::HiRes
qw(usleep gettimeofday tv_interval);
13 use PVE
::QemuServer
::Helpers
;
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 only handle 1 connection, so we close connections asap
23 my ($class, $eventcb) = @_;
25 my $mux = IO
::Multiplex-
>new();
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
::Helpers
::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 # While it should return instantly or never (dead locked) for Linux guests,
117 # the variance for Windows guests can be big. And there might be hook scripts
118 # that are executed upon thaw, so use 3 minutes to be on the safe side.
121 $cmd->{execute
} eq 'savevm-start' ||
122 $cmd->{execute
} eq 'savevm-end' ||
123 $cmd->{execute
} eq 'query-backup' ||
124 $cmd->{execute
} eq 'query-block-jobs' ||
125 $cmd->{execute
} eq 'block-job-cancel' ||
126 $cmd->{execute
} eq 'block-job-complete' ||
127 $cmd->{execute
} eq 'backup-cancel' ||
128 $cmd->{execute
} eq 'query-savevm' ||
129 $cmd->{execute
} eq 'guest-fstrim' ||
130 $cmd->{execute
} eq 'guest-shutdown' ||
131 $cmd->{execute
} eq 'blockdev-snapshot-internal-sync' ||
132 $cmd->{execute
} eq 'blockdev-snapshot-delete-internal-sync'
134 $timeout = 10*60; # 10 mins ?
136 # NOTE: if you came here as user and want to change this, try using IO-Threads first
137 # which move out quite some processing of the main thread, leaving more time for QMP
138 $timeout = 5; # default
142 $self->queue_execute($timeout, 2);
144 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
145 if defined($queue_info->{error
});
151 my $cmdid_seq_qga = 0;
153 my $next_cmdid = sub {
158 return "$$"."0".$cmdid_seq_qga;
161 return "$$:$cmdid_seq";
165 my $lookup_queue_info = sub {
166 my ($self, $fh, $noerr) = @_;
168 my $queue_info = $self->{queue_lookup
}->{$fh};
170 warn "internal error - unable to lookup queue info" if !$noerr;
176 my $close_connection = sub {
177 my ($self, $queue_info) = @_;
179 if (my $fh = delete $queue_info->{fh
}) {
180 delete $self->{queue_lookup
}->{$fh};
181 $self->{mux
}->close($fh);
185 my $open_connection = sub {
186 my ($self, $queue_info, $timeout) = @_;
188 die "duplicate call to open" if defined($queue_info->{fh
});
190 my $vmid = $queue_info->{vmid
};
191 my $qga = $queue_info->{qga
};
193 my $sname = PVE
::QemuServer
::Helpers
::qmp_socket
($vmid, $qga);
195 $timeout = 1 if !$timeout;
198 my $starttime = [gettimeofday
];
201 my $sotype = $qga ?
'qga' : 'qmp';
205 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
207 if ($! != EINTR
&& $! != EAGAIN
) {
208 die "unable to connect to VM $vmid $sotype socket - $!\n";
210 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
211 if ($elapsed >= $timeout) {
212 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
217 $queue_info->{fh
} = $fh;
219 $self->{queue_lookup
}->{$fh} = $queue_info;
221 $self->{mux
}->add($fh);
222 $self->{mux
}->set_timeout($fh, $timeout);
227 my $check_queue = sub {
232 foreach my $sname (keys %{$self->{queue_info
}}) {
233 my $queue_info = $self->{queue_info
}->{$sname};
234 my $fh = $queue_info->{fh
};
237 my $qga = $queue_info->{qga
};
239 if ($queue_info->{error
}) {
240 &$close_connection($self, $queue_info);
244 if ($queue_info->{current
}) { # command running, waiting for response
249 if (!scalar(@{$queue_info->{cmds
}})) { # no more commands
250 &$close_connection($self, $queue_info);
256 my $cmd = $queue_info->{current
} = shift @{$queue_info->{cmds
}};
257 $cmd->{id
} = &$next_cmdid($qga);
260 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
261 $fd = $cmd->{arguments
}->{fd
};
262 delete $cmd->{arguments
}->{fd
};
269 $qmpcmd = to_json
({ execute
=> 'guest-sync-delimited',
270 arguments
=> { id
=> int($cmd->{id
})}}) . "\n" .
271 to_json
({ execute
=> $cmd->{execute
}, arguments
=> $cmd->{arguments
}}) . "\n";
276 execute
=> $cmd->{execute
},
277 arguments
=> $cmd->{arguments
},
282 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
283 die "sendfd failed" if $ret < 0;
285 $self->{mux
}->write($fh, $qmpcmd);
289 $queue_info->{error
} = $err;
295 $self->{mux
}->endloop() if !$running;
300 # execute all queued command
303 my ($self, $timeout, $noerr) = @_;
305 $timeout = 3 if !$timeout;
307 # open all necessary connections
308 foreach my $sname (keys %{$self->{queue_info
}}) {
309 my $queue_info = $self->{queue_info
}->{$sname};
310 next if !scalar(@{$queue_info->{cmds
}}); # no commands
312 $queue_info->{error
} = undef;
313 $queue_info->{current
} = undef;
316 &$open_connection($self, $queue_info, $timeout);
318 if (!$queue_info->{qga
}) {
319 my $cap_cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
320 unshift @{$queue_info->{cmds
}}, $cap_cmd;
324 $queue_info->{error
} = $err;
332 $running = &$check_queue($self);
339 # make sure we close everything
341 foreach my $sname (keys %{$self->{queue_info
}}) {
342 my $queue_info = $self->{queue_info
}->{$sname};
343 &$close_connection($self, $queue_info);
344 if ($queue_info->{error
}) {
346 warn $queue_info->{error
} if $noerr < 2;
348 $errors .= $queue_info->{error
}
353 $self->{queue_info
} = $self->{queue_lookup
} = {};
355 die $errors if $errors;
359 my ($self, $mux, $fh) = @_;
361 my $queue_info = &$lookup_queue_info($self, $fh, 1);
362 return if !$queue_info;
364 $queue_info->{error
} = "client closed connection\n"
365 if !$queue_info->{error
};
368 # mux_input is called when input is available on one of the descriptors.
370 my ($self, $mux, $fh, $input) = @_;
372 my $queue_info = &$lookup_queue_info($self, $fh);
373 return if !$queue_info;
375 my $sname = $queue_info->{sname
};
376 my $vmid = $queue_info->{vmid
};
377 my $qga = $queue_info->{qga
};
379 my $curcmd = $queue_info->{current
};
380 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
385 return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so;
388 return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so;
393 my @jsons = split("\n", $raw);
397 die "response is not complete" if @jsons != 2 ;
399 my $obj = from_json
($jsons[0]);
401 my $cmdid = $obj->{'return'};
402 die "received responsed without command id\n" if !$cmdid;
404 # skip results fro previous commands
405 return if $cmdid < $curcmd->{id
};
407 if ($curcmd->{id
} ne $cmdid) {
408 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
411 delete $queue_info->{current
};
413 $obj = from_json
($jsons[1]);
415 if (my $callback = $curcmd->{callback
}) {
416 &$callback($vmid, $obj);
422 foreach my $json (@jsons) {
423 my $obj = from_json
($json);
424 next if defined($obj->{QMP
}); # skip monitor greeting
426 if (exists($obj->{error
}->{desc
})) {
427 my $desc = $obj->{error
}->{desc
};
429 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
433 if (defined($obj->{event
})) {
434 if (my $eventcb = $self->{eventcb
}) {
440 my $cmdid = $obj->{id
};
441 die "received responsed without command id\n" if !$cmdid;
443 if ($curcmd->{id
} ne $cmdid) {
444 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
447 delete $queue_info->{current
};
449 if (my $callback = $curcmd->{callback
}) {
450 &$callback($vmid, $obj);
455 $queue_info->{error
} = $err;
458 &$check_queue($self);
461 # This gets called every second to update player info, etc...
463 my ($self, $mux, $fh) = @_;
465 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
466 $queue_info->{error
} = "got timeout\n";
467 $self->{mux
}->inbuffer($fh, ''); # clear to avoid warnings
470 &$check_queue($self);
474 my ($self, $mux, $fh, $input) = @_;
476 my $queue_info = &$lookup_queue_info($self, $fh);
477 return if !$queue_info;
479 my $sname = $queue_info->{sname
};
480 my $vmid = $queue_info->{vmid
};
481 my $qga = $queue_info->{qga
};
483 my $curcmd = $queue_info->{current
};
484 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
486 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute
}}) {
488 return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so;
493 my $obj = from_json
($raw);
495 my $cmdid = $obj->{'return'};
496 die "received responsed without command id\n" if !$cmdid;
498 delete $queue_info->{current
};
500 if (my $callback = $curcmd->{callback
}) {
501 &$callback($vmid, undef);
505 $queue_info->{error
} = $err;
508 &$close_connection($self, $queue_info);
510 if (scalar(@{$queue_info->{cmds
}}) && !$queue_info->{error
}) {
511 $queue_info->{error
} = "Got EOF but command queue is not empty.\n";
519 foreach my $sname (keys %{$self->{queue_info
}}) {
520 my $queue_info = $self->{queue_info
}->{$sname};
521 $close_connection->($self, $queue_info);