]>
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'};
97 die "no command specified" if !($cmd && $cmd->{execute
});
99 $cmd->{callback
} = $callback;
100 $cmd->{arguments
} = {} if !defined($cmd->{arguments
});
102 my $queue_info = &$push_cmd_to_queue($self, $vmid, $cmd);
105 # hack: monitor sometime blocks
106 if ($cmd->{execute
} eq 'query-migrate') {
107 $timeout = 60*60; # 1 hour
108 } elsif ($cmd->{execute
} =~ m/^(eject|change)/) {
109 $timeout = 60; # note: cdrom mount command is slow
110 } elsif ($cmd->{execute
} eq 'guest-fsfreeze-freeze' ||
111 $cmd->{execute
} eq 'guest-fsfreeze-thaw') {
113 } elsif ($cmd->{execute
} eq 'savevm-start' ||
114 $cmd->{execute
} eq 'savevm-end' ||
115 $cmd->{execute
} eq 'query-backup' ||
116 $cmd->{execute
} eq 'query-block-jobs' ||
117 $cmd->{execute
} eq 'backup-cancel' ||
118 $cmd->{execute
} eq 'query-savevm' ||
119 $cmd->{execute
} eq 'delete-drive-snapshot' ||
120 $cmd->{execute
} eq 'guest-shutdown' ||
121 $cmd->{execute
} eq 'snapshot-drive' ) {
122 $timeout = 10*60; # 10 mins ?
124 $timeout = 3; # default
128 $self->queue_execute($timeout, 2);
130 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
131 if defined($queue_info->{error
});
137 my $cmdid_seq_qga = 0;
139 my $next_cmdid = sub {
144 return "$$"."0".$cmdid_seq_qga;
147 return "$$:$cmdid_seq";
151 my $lookup_queue_info = sub {
152 my ($self, $fh, $noerr) = @_;
154 my $queue_info = $self->{queue_lookup
}->{$fh};
156 warn "internal error - unable to lookup queue info" if !$noerr;
162 my $close_connection = sub {
163 my ($self, $queue_info) = @_;
165 if (my $fh = delete $queue_info->{fh
}) {
166 delete $self->{queue_lookup
}->{$fh};
167 $self->{mux
}->close($fh);
171 my $open_connection = sub {
172 my ($self, $queue_info, $timeout) = @_;
174 die "duplicate call to open" if defined($queue_info->{fh
});
176 my $vmid = $queue_info->{vmid
};
177 my $qga = $queue_info->{qga
};
179 my $sname = PVE
::QemuServer
::qmp_socket
($vmid, $qga);
181 $timeout = 1 if !$timeout;
184 my $starttime = [gettimeofday
];
187 my $sotype = $qga ?
'qga' : 'qmp';
191 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
193 if ($! != EINTR
&& $! != EAGAIN
) {
194 die "unable to connect to VM $vmid $sotype socket - $!\n";
196 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
197 if ($elapsed >= $timeout) {
198 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
203 $queue_info->{fh
} = $fh;
205 $self->{queue_lookup
}->{$fh} = $queue_info;
207 $self->{mux
}->add($fh);
208 $self->{mux
}->set_timeout($fh, $timeout);
213 my $check_queue = sub {
218 foreach my $sname (keys %{$self->{queue_info
}}) {
219 my $queue_info = $self->{queue_info
}->{$sname};
220 my $fh = $queue_info->{fh
};
223 my $qga = $queue_info->{qga
};
225 if ($queue_info->{error
}) {
226 &$close_connection($self, $queue_info);
230 if ($queue_info->{current
}) { # command running, waiting for response
235 if (!scalar(@{$queue_info->{cmds
}})) { # no more commands
236 &$close_connection($self, $queue_info);
242 my $cmd = $queue_info->{current
} = shift @{$queue_info->{cmds
}};
243 $cmd->{id
} = &$next_cmdid($qga);
246 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
247 $fd = $cmd->{arguments
}->{fd
};
248 delete $cmd->{arguments
}->{fd
};
255 $qmpcmd = to_json
({ execute
=> 'guest-sync-delimited',
256 arguments
=> { id
=> int($cmd->{id
})}}) .
257 to_json
({ execute
=> $cmd->{execute
}, arguments
=> $cmd->{arguments
}});
262 execute
=> $cmd->{execute
},
263 arguments
=> $cmd->{arguments
},
268 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
269 die "sendfd failed" if $ret < 0;
271 $self->{mux
}->write($fh, $qmpcmd);
275 $queue_info->{error
} = $err;
281 $self->{mux
}->endloop() if !$running;
286 # execute all queued command
289 my ($self, $timeout, $noerr) = @_;
291 $timeout = 3 if !$timeout;
293 # open all necessary connections
294 foreach my $sname (keys %{$self->{queue_info
}}) {
295 my $queue_info = $self->{queue_info
}->{$sname};
296 next if !scalar(@{$queue_info->{cmds
}}); # no commands
298 $queue_info->{error
} = undef;
299 $queue_info->{current
} = undef;
302 &$open_connection($self, $queue_info, $timeout);
304 if (!$queue_info->{qga
}) {
305 my $cap_cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
306 unshift @{$queue_info->{cmds
}}, $cap_cmd;
310 $queue_info->{error
} = $err;
318 $running = &$check_queue($self);
325 # make sure we close everything
327 foreach my $sname (keys %{$self->{queue_info
}}) {
328 my $queue_info = $self->{queue_info
}->{$sname};
329 &$close_connection($self, $queue_info);
330 if ($queue_info->{error
}) {
332 warn $queue_info->{error
} if $noerr < 2;
334 $errors .= $queue_info->{error
}
339 $self->{queue_info
} = $self->{queue_lookup
} = {};
341 die $errors if $errors;
345 my ($self, $mux, $fh) = @_;
347 my $queue_info = &$lookup_queue_info($self, $fh, 1);
348 return if !$queue_info;
350 $queue_info->{error
} = "client closed connection\n"
351 if !$queue_info->{error
};
354 # mux_input is called when input is available on one of the descriptors.
356 my ($self, $mux, $fh, $input) = @_;
358 my $queue_info = &$lookup_queue_info($self, $fh);
359 return if !$queue_info;
361 my $sname = $queue_info->{sname
};
362 my $vmid = $queue_info->{vmid
};
363 my $qga = $queue_info->{qga
};
365 my $curcmd = $queue_info->{current
};
366 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
371 return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so;
374 return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so;
379 my @jsons = split("\n", $raw);
383 die "response is not complete" if @jsons != 2 ;
385 my $obj = from_json
($jsons[0]);
387 my $cmdid = $obj->{'return'};
388 die "received responsed without command id\n" if !$cmdid;
390 # skip results fro previous commands
391 return if $cmdid < $curcmd->{id
};
393 if ($curcmd->{id
} ne $cmdid) {
394 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
397 delete $queue_info->{current
};
399 $obj = from_json
($jsons[1]);
401 if (my $callback = $curcmd->{callback
}) {
402 &$callback($vmid, $obj);
408 foreach my $json (@jsons) {
409 my $obj = from_json
($json);
410 next if defined($obj->{QMP
}); # skip monitor greeting
412 if (exists($obj->{error
}->{desc
})) {
413 my $desc = $obj->{error
}->{desc
};
415 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
419 if (defined($obj->{event
})) {
420 if (my $eventcb = $self->{eventcb
}) {
426 my $cmdid = $obj->{id
};
427 die "received responsed without command id\n" if !$cmdid;
429 if ($curcmd->{id
} ne $cmdid) {
430 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
433 delete $queue_info->{current
};
435 if (my $callback = $curcmd->{callback
}) {
436 &$callback($vmid, $obj);
441 $queue_info->{error
} = $err;
444 &$check_queue($self);
447 # This gets called every second to update player info, etc...
449 my ($self, $mux, $fh) = @_;
451 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
452 $queue_info->{error
} = "got timeout\n";
453 $self->{mux
}->inbuffer($fh, ''); # clear to avoid warnings
456 &$check_queue($self);
460 my ($self, $mux, $fh, $input) = @_;
462 my $queue_info = &$lookup_queue_info($self, $fh);
463 return if !$queue_info;
465 my $sname = $queue_info->{sname
};
466 my $vmid = $queue_info->{vmid
};
467 my $qga = $queue_info->{qga
};
469 my $curcmd = $queue_info->{current
};
470 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
472 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute
}}) {
474 return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so;
479 my $obj = from_json
($raw);
481 my $cmdid = $obj->{'return'};
482 die "received responsed without command id\n" if !$cmdid;
484 delete $queue_info->{current
};
486 if (my $callback = $curcmd->{callback
}) {
487 &$callback($vmid, undef);
491 $queue_info->{error
} = $err;
494 &$close_connection($self, $queue_info);
496 if (scalar(@{$queue_info->{cmds
}}) && !$queue_info->{error
}) {
497 $queue_info->{error
} = "Got EOF but command queue is not empty.\n";