]>
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 # freeze syncs all guest FS, if we kill it it stays in an unfreezable
112 # locked state with high probability, so use an generous timeout
113 $timeout = 60*60; # 1 hour
114 } elsif ($cmd->{execute
} eq 'guest-fsfreeze-thaw') {
115 # thaw has no possible long blocking actions, either it returns
116 # instantly or never (dead locked)
118 } elsif ($cmd->{execute
} eq 'savevm-start' ||
119 $cmd->{execute
} eq 'savevm-end' ||
120 $cmd->{execute
} eq 'query-backup' ||
121 $cmd->{execute
} eq 'query-block-jobs' ||
122 $cmd->{execute
} eq 'block-job-cancel' ||
123 $cmd->{execute
} eq 'block-job-complete' ||
124 $cmd->{execute
} eq 'backup-cancel' ||
125 $cmd->{execute
} eq 'query-savevm' ||
126 $cmd->{execute
} eq 'delete-drive-snapshot' ||
127 $cmd->{execute
} eq 'guest-shutdown' ||
128 $cmd->{execute
} eq 'snapshot-drive' ) {
129 $timeout = 10*60; # 10 mins ?
131 $timeout = 3; # default
135 $self->queue_execute($timeout, 2);
137 die "VM $vmid qmp command '$cmd->{execute}' failed - $queue_info->{error}"
138 if defined($queue_info->{error
});
144 my $cmdid_seq_qga = 0;
146 my $next_cmdid = sub {
151 return "$$"."0".$cmdid_seq_qga;
154 return "$$:$cmdid_seq";
158 my $lookup_queue_info = sub {
159 my ($self, $fh, $noerr) = @_;
161 my $queue_info = $self->{queue_lookup
}->{$fh};
163 warn "internal error - unable to lookup queue info" if !$noerr;
169 my $close_connection = sub {
170 my ($self, $queue_info) = @_;
172 if (my $fh = delete $queue_info->{fh
}) {
173 delete $self->{queue_lookup
}->{$fh};
174 $self->{mux
}->close($fh);
178 my $open_connection = sub {
179 my ($self, $queue_info, $timeout) = @_;
181 die "duplicate call to open" if defined($queue_info->{fh
});
183 my $vmid = $queue_info->{vmid
};
184 my $qga = $queue_info->{qga
};
186 my $sname = PVE
::QemuServer
::qmp_socket
($vmid, $qga);
188 $timeout = 1 if !$timeout;
191 my $starttime = [gettimeofday
];
194 my $sotype = $qga ?
'qga' : 'qmp';
198 $fh = IO
::Socket
::UNIX-
>new(Peer
=> $sname, Blocking
=> 0, Timeout
=> 1);
200 if ($! != EINTR
&& $! != EAGAIN
) {
201 die "unable to connect to VM $vmid $sotype socket - $!\n";
203 my $elapsed = tv_interval
($starttime, [gettimeofday
]);
204 if ($elapsed >= $timeout) {
205 die "unable to connect to VM $vmid $sotype socket - timeout after $count retries\n";
210 $queue_info->{fh
} = $fh;
212 $self->{queue_lookup
}->{$fh} = $queue_info;
214 $self->{mux
}->add($fh);
215 $self->{mux
}->set_timeout($fh, $timeout);
220 my $check_queue = sub {
225 foreach my $sname (keys %{$self->{queue_info
}}) {
226 my $queue_info = $self->{queue_info
}->{$sname};
227 my $fh = $queue_info->{fh
};
230 my $qga = $queue_info->{qga
};
232 if ($queue_info->{error
}) {
233 &$close_connection($self, $queue_info);
237 if ($queue_info->{current
}) { # command running, waiting for response
242 if (!scalar(@{$queue_info->{cmds
}})) { # no more commands
243 &$close_connection($self, $queue_info);
249 my $cmd = $queue_info->{current
} = shift @{$queue_info->{cmds
}};
250 $cmd->{id
} = &$next_cmdid($qga);
253 if ($cmd->{execute
} eq 'add-fd' || $cmd->{execute
} eq 'getfd') {
254 $fd = $cmd->{arguments
}->{fd
};
255 delete $cmd->{arguments
}->{fd
};
262 $qmpcmd = to_json
({ execute
=> 'guest-sync-delimited',
263 arguments
=> { id
=> int($cmd->{id
})}}) .
264 to_json
({ execute
=> $cmd->{execute
}, arguments
=> $cmd->{arguments
}});
269 execute
=> $cmd->{execute
},
270 arguments
=> $cmd->{arguments
},
275 my $ret = PVE
::IPCC
::sendfd
(fileno($fh), $fd, $qmpcmd);
276 die "sendfd failed" if $ret < 0;
278 $self->{mux
}->write($fh, $qmpcmd);
282 $queue_info->{error
} = $err;
288 $self->{mux
}->endloop() if !$running;
293 # execute all queued command
296 my ($self, $timeout, $noerr) = @_;
298 $timeout = 3 if !$timeout;
300 # open all necessary connections
301 foreach my $sname (keys %{$self->{queue_info
}}) {
302 my $queue_info = $self->{queue_info
}->{$sname};
303 next if !scalar(@{$queue_info->{cmds
}}); # no commands
305 $queue_info->{error
} = undef;
306 $queue_info->{current
} = undef;
309 &$open_connection($self, $queue_info, $timeout);
311 if (!$queue_info->{qga
}) {
312 my $cap_cmd = { execute
=> 'qmp_capabilities', arguments
=> {} };
313 unshift @{$queue_info->{cmds
}}, $cap_cmd;
317 $queue_info->{error
} = $err;
325 $running = &$check_queue($self);
332 # make sure we close everything
334 foreach my $sname (keys %{$self->{queue_info
}}) {
335 my $queue_info = $self->{queue_info
}->{$sname};
336 &$close_connection($self, $queue_info);
337 if ($queue_info->{error
}) {
339 warn $queue_info->{error
} if $noerr < 2;
341 $errors .= $queue_info->{error
}
346 $self->{queue_info
} = $self->{queue_lookup
} = {};
348 die $errors if $errors;
352 my ($self, $mux, $fh) = @_;
354 my $queue_info = &$lookup_queue_info($self, $fh, 1);
355 return if !$queue_info;
357 $queue_info->{error
} = "client closed connection\n"
358 if !$queue_info->{error
};
361 # mux_input is called when input is available on one of the descriptors.
363 my ($self, $mux, $fh, $input) = @_;
365 my $queue_info = &$lookup_queue_info($self, $fh);
366 return if !$queue_info;
368 my $sname = $queue_info->{sname
};
369 my $vmid = $queue_info->{vmid
};
370 my $qga = $queue_info->{qga
};
372 my $curcmd = $queue_info->{current
};
373 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
378 return if $$input !~ s/^.*\xff([^\n]+}\r?\n[^\n]+})\r?\n(.*)$/$2/so;
381 return if $$input !~ s/^(.*})\r?\n(.*)$/$2/so;
386 my @jsons = split("\n", $raw);
390 die "response is not complete" if @jsons != 2 ;
392 my $obj = from_json
($jsons[0]);
394 my $cmdid = $obj->{'return'};
395 die "received responsed without command id\n" if !$cmdid;
397 # skip results fro previous commands
398 return if $cmdid < $curcmd->{id
};
400 if ($curcmd->{id
} ne $cmdid) {
401 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
404 delete $queue_info->{current
};
406 $obj = from_json
($jsons[1]);
408 if (my $callback = $curcmd->{callback
}) {
409 &$callback($vmid, $obj);
415 foreach my $json (@jsons) {
416 my $obj = from_json
($json);
417 next if defined($obj->{QMP
}); # skip monitor greeting
419 if (exists($obj->{error
}->{desc
})) {
420 my $desc = $obj->{error
}->{desc
};
422 die "$desc\n" if $desc !~ m/Connection can not be completed immediately/;
426 if (defined($obj->{event
})) {
427 if (my $eventcb = $self->{eventcb
}) {
433 my $cmdid = $obj->{id
};
434 die "received responsed without command id\n" if !$cmdid;
436 if ($curcmd->{id
} ne $cmdid) {
437 die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
440 delete $queue_info->{current
};
442 if (my $callback = $curcmd->{callback
}) {
443 &$callback($vmid, $obj);
448 $queue_info->{error
} = $err;
451 &$check_queue($self);
454 # This gets called every second to update player info, etc...
456 my ($self, $mux, $fh) = @_;
458 if (my $queue_info = &$lookup_queue_info($self, $fh)) {
459 $queue_info->{error
} = "got timeout\n";
460 $self->{mux
}->inbuffer($fh, ''); # clear to avoid warnings
463 &$check_queue($self);
467 my ($self, $mux, $fh, $input) = @_;
469 my $queue_info = &$lookup_queue_info($self, $fh);
470 return if !$queue_info;
472 my $sname = $queue_info->{sname
};
473 my $vmid = $queue_info->{vmid
};
474 my $qga = $queue_info->{qga
};
476 my $curcmd = $queue_info->{current
};
477 die "unable to lookup current command for VM $vmid ($sname)\n" if !$curcmd;
479 if ($qga && $qga_allow_close_cmds->{$curcmd->{execute
}}) {
481 return if $$input !~ s/^.*\xff([^\n]+})\r?\n(.*)$/$2/so;
486 my $obj = from_json
($raw);
488 my $cmdid = $obj->{'return'};
489 die "received responsed without command id\n" if !$cmdid;
491 delete $queue_info->{current
};
493 if (my $callback = $curcmd->{callback
}) {
494 &$callback($vmid, undef);
498 $queue_info->{error
} = $err;
501 &$close_connection($self, $queue_info);
503 if (scalar(@{$queue_info->{cmds
}}) && !$queue_info->{error
}) {
504 $queue_info->{error
} = "Got EOF but command queue is not empty.\n";