]>
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 'savevm-start' ||
111 $cmd->{execute
} eq 'savevm-end' ||
112 $cmd->{execute
} eq 'query-backup' ||
113 $cmd->{execute
} eq 'query-block-jobs' ||
114 $cmd->{execute
} eq 'backup-cancel' ||
115 $cmd->{execute
} eq 'query-savevm' ||
116 $cmd->{execute
} eq 'delete-drive-snapshot' ||
117 $cmd->{execute
} eq 'guest-shutdown' ||
118 $cmd->{execute
} eq 'snapshot-drive' ) {
119 $timeout = 10*60; # 10 mins ?
121 $timeout = 3; # default
125 $self->queue_execute($timeout, 2);
127 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
128 if defined($queue_info->{error
});
134 my $cmdid_seq_qga = 0;
136 my $next_cmdid = sub {
141 return "$$"."0".$cmdid_seq_qga;
144 return "$$:$cmdid_seq";
148 my $lookup_queue_info = sub {
149 my ($self, $fh, $noerr) = @_;
151 my $queue_info = $self->{queue_lookup
}->{$fh};
153 warn "internal error - unable to lookup queue info" if !$noerr;
159 my $close_connection = sub {
160 my ($self, $queue_info) = @_;
162 if (my $fh = delete $queue_info->{fh
}) {
163 delete $self->{queue_lookup
}->{$fh};
164 $self->{mux
}->close($fh);
168 my $open_connection = sub {
169 my ($self, $queue_info, $timeout) = @_;
171 die "duplicate call to open" if defined($queue_info->{fh
});
173 my $vmid = $queue_info->{vmid
};
174 my $qga = $queue_info->{qga
};
176 my $sname = PVE
::QemuServer
::qmp_socket
($vmid, $qga);
178 $timeout = 1 if !$timeout;
181 my $starttime = [gettimeofday
];
184 my $sotype = $qga ?
'qga' : 'qmp';
188 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
190 if ($! != EINTR
&& $! != EAGAIN
) {
191 die "unable to connect to VM $vmid $sotype socket - $!\n";
193 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
194 if ($elapsed >= $timeout) {
195 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
200 $queue_info->{fh
} = $fh;
202 $self->{queue_lookup
}->{$fh} = $queue_info;
204 $self->{mux
}->add($fh);
205 $self->{mux
}->set_timeout($fh, $timeout);
210 my $check_queue = sub {
215 foreach my $sname (keys %{$self->{queue_info
}}) {
216 my $queue_info = $self->{queue_info
}->{$sname};
217 my $fh = $queue_info->{fh
};
220 my $qga = $queue_info->{qga
};
222 if ($queue_info->{error
}) {
223 &$close_connection($self, $queue_info);
227 if ($queue_info->{current
}) { # command running, waiting for response
232 if (!scalar(@{$queue_info->{cmds
}})) { # no more commands
233 &$close_connection($self, $queue_info);
239 my $cmd = $queue_info->{current
} = shift @{$queue_info->{cmds
}};
240 $cmd->{id
} = &$next_cmdid($qga);
243 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
244 $fd = $cmd->{arguments
}->{fd
};
245 delete $cmd->{arguments
}->{fd
};
252 $qmpcmd = to_json
({ execute
=> 'guest-sync', arguments
=> { id
=> int($cmd->{id
})}}) .
253 to_json
({ execute
=> $cmd->{execute
}, arguments
=> $cmd->{arguments
}});
258 execute
=> $cmd->{execute
},
259 arguments
=> $cmd->{arguments
},
264 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
265 die "sendfd failed" if $ret < 0;
267 $self->{mux
}->write($fh, $qmpcmd);
271 $queue_info->{error
} = $err;
277 $self->{mux
}->endloop() if !$running;
282 # execute all queued command
285 my ($self, $timeout, $noerr) = @_;
287 $timeout = 3 if !$timeout;
289 # open all necessary connections
290 foreach my $sname (keys %{$self->{queue_info
}}) {
291 my $queue_info = $self->{queue_info
}->{$sname};
292 next if !scalar(@{$queue_info->{cmds
}}); # no commands
294 $queue_info->{error
} = undef;
295 $queue_info->{current
} = undef;
298 &$open_connection($self, $queue_info, $timeout);
300 if (!$queue_info->{qga
}) {
301 my $cap_cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
302 unshift @{$queue_info->{cmds
}}, $cap_cmd;
306 $queue_info->{error
} = $err;
314 $running = &$check_queue($self);
321 # make sure we close everything
323 foreach my $sname (keys %{$self->{queue_info
}}) {
324 my $queue_info = $self->{queue_info
}->{$sname};
325 &$close_connection($self, $queue_info);
326 if ($queue_info->{error
}) {
328 warn $queue_info->{error
} if $noerr < 2;
330 $errors .= $queue_info->{error
}
335 $self->{queue_info
} = $self->{queue_lookup
} = {};
337 die $errors if $errors;
341 my ($self, $mux, $fh) = @_;
343 my $queue_info = &$lookup_queue_info($self, $fh, 1);
344 return if !$queue_info;
346 $queue_info->{error
} = "client closed connection\n"
347 if !$queue_info->{error
};
350 # mux_input is called when input is available on one of the descriptors.
352 my ($self, $mux, $fh, $input) = @_;
354 my $queue_info = &$lookup_queue_info($self, $fh);
355 return if !$queue_info;
357 my $sname = $queue_info->{sname
};
358 my $vmid = $queue_info->{vmid
};
359 my $qga = $queue_info->{qga
};
361 my $curcmd = $queue_info->{current
};
362 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
367 return if $$input !~ s/^([^\n]+}\n[^\n]+})\n(.*)$/$2/so;
370 return if $$input !~ s/^([^\n]+})\r?\n(.*)$/$2/so;
375 my @jsons = split("\n", $raw);
379 die "response is not complete" if @jsons != 2 ;
381 my $obj = from_json
($jsons[0]);
383 my $cmdid = $obj->{'return'};
384 die "received responsed without command id\n" if !$cmdid;
386 if ($curcmd->{id
} ne $cmdid) {
387 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
390 delete $queue_info->{current
};
392 $obj = from_json
($jsons[1]);
394 if (my $callback = $curcmd->{callback
}) {
395 &$callback($vmid, $obj);
401 foreach my $json (@jsons) {
402 my $obj = from_json
($json);
403 next if defined($obj->{QMP
}); # skip monitor greeting
405 if (exists($obj->{error
}->{desc
})) {
406 my $desc = $obj->{error
}->{desc
};
408 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
412 if (defined($obj->{event
})) {
413 if (my $eventcb = $self->{eventcb
}) {
419 my $cmdid = $obj->{id
};
420 die "received responsed without command id\n" if !$cmdid;
422 if ($curcmd->{id
} ne $cmdid) {
423 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
426 delete $queue_info->{current
};
428 if (my $callback = $curcmd->{callback
}) {
429 &$callback($vmid, $obj);
434 $queue_info->{error
} = $err;
437 &$check_queue($self);
440 # This gets called every second to update player info, etc...
442 my ($self, $mux, $fh) = @_;
444 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
445 $queue_info->{error
} = "got timeout\n";
446 $self->{mux
}->inbuffer($fh, ''); # clear to avoid warnings
449 &$check_queue($self);
453 my ($self, $mux, $fh, $input) = @_;
455 my $queue_info = &$lookup_queue_info($self, $fh);
456 return if !$queue_info;
458 my $sname = $queue_info->{sname
};
459 my $vmid = $queue_info->{vmid
};
460 my $qga = $queue_info->{qga
};
462 my $curcmd = $queue_info->{current
};
463 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
465 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute
}}) {
467 return if $$input !~ s/^([^\n]+})\n(.*)$/$2/so;
472 my $obj = from_json
($raw);
474 my $cmdid = $obj->{'return'};
475 die "received responsed without command id\n" if !$cmdid;
477 delete $queue_info->{current
};
479 if (my $callback = $curcmd->{callback
}) {
480 &$callback($vmid, undef);
484 $queue_info->{error
} = $err;
487 &$close_connection($self, $queue_info);
489 if (scalar(@{$queue_info->{cmds
}}) && !$queue_info->{error
}) {
490 $queue_info->{error
} = "Got EOF but command queue is not empty.\n";