From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
"Markus Armbruster" <armbru@redhat.com>,
"Eric Blake" <eblake@redhat.com>,
"Daniel P. Berrange" <berrange@redhat.com>
Subject: [Qemu-devel] [PATCH v4 9/9] char: allow passing pre-opened socket file descriptor at startup
Date: Mon, 5 Feb 2018 15:24:55 +0000 [thread overview]
Message-ID: <20180205152455.12088-10-berrange@redhat.com> (raw)
In-Reply-To: <20180205152455.12088-1-berrange@redhat.com>
From: "Daniel P. Berrange" <berrange@redhat.com>
When starting QEMU management apps will usually setup a monitor socket, and
then open it immediately after startup. If not using QEMU's own -daemonize
arg, this process can be troublesome to handle correctly. The mgmt app will
need to repeatedly call connect() until it succeeds, because it does not
know when QEMU has created the listener socket. If can't retry connect()
forever though, because an error might have caused QEMU to exit before it
even creates the monitor.
The obvious way to fix this kind of problem is to just pass in a pre-opened
socket file descriptor for the QEMU monitor to listen on. The management
app can now immediately call connect() just once. If connect() fails it
knows that QEMU has exited with an error.
The SocketAddress(Legacy) structs allow for FD passing via the monitor, and
now via inherited file descriptors from the process that spawned QEMU. The
final missing piece is adding a 'fd' parameter in the socket chardev
options.
This allows both HMP usage, pass any FD number with SCM_RIGHTS, then
running HMP commands:
getfd myfd
chardev-add socket,fd=myfd
Note that numeric FDs cannot be referenced directly in HMP, only named FDs.
And also CLI usage, by leak FD 3 from parent by clearing O_CLOEXEC, then
spawning QEMU with
-chardev socket,fd=3,id=mon
-mon chardev=mon,mode=control
Note that named FDs cannot be referenced in CLI args, only numeric FDs.
We do not wire this up in the legacy chardev syntax, so you cannot use FD
passing with '-qmp', you must use the modern '-mon' + '-chardev' pair.
When passing pre-opened FDs there is a restriction on use of TLS encryption.
It can be used on a server socket chardev, but cannot be used for a client
socket chardev. This is because when validating a server's certificate, the
client needs to have a hostname available to match against the certificate
identity.
An illustrative example of usage is:
#!/usr/bin/perl
use IO::Socket::UNIX;
use Fcntl;
unlink "/tmp/qmp";
my $srv = IO::Socket::UNIX->new(
Type => SOCK_STREAM(),
Local => "/tmp/qmp",
Listen => 1,
);
my $flags = fcntl $srv, F_GETFD, 0;
fcntl $srv, F_SETFD, $flags & ~FD_CLOEXEC;
my $fd = $srv->fileno();
exec "qemu-system-x86_64", \
"-chardev", "socket,fd=$fd,server,nowait,id=mon", \
"-mon", "chardev=mon,mode=control";
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
chardev/char-socket.c | 15 +++++++++++++--
chardev/char.c | 3 +++
tests/test-char.c | 47 ++++++++++++++++++++++++++++++++++++++++++++---
3 files changed, 60 insertions(+), 5 deletions(-)
diff --git a/chardev/char-socket.c b/chardev/char-socket.c
index 7d0e608e60..980b8f0815 100644
--- a/chardev/char-socket.c
+++ b/chardev/char-socket.c
@@ -978,13 +978,14 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
const char *path = qemu_opt_get(opts, "path");
const char *host = qemu_opt_get(opts, "host");
const char *port = qemu_opt_get(opts, "port");
+ const char *fd = qemu_opt_get(opts, "fd");
const char *tls_creds = qemu_opt_get(opts, "tls-creds");
SocketAddressLegacy *addr;
ChardevSocket *sock;
- if ((!!path + !!host) != 1) {
+ if ((!!path + !!fd + !!host) != 1) {
error_setg(errp,
- "Exactly one of 'path' or 'host' required");
+ "Exactly one of 'path', 'fd' or 'host' required");
return;
}
@@ -999,6 +1000,12 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
error_setg(errp, "chardev: socket: no port given");
return;
}
+ } else if (fd) {
+ /* We don't know what host to validate against when in client mode */
+ if (tls_creds && !is_listen) {
+ error_setg(errp, "TLS can not be used with pre-opened client FD");
+ return;
+ }
} else {
g_assert_not_reached();
}
@@ -1039,6 +1046,10 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
.has_ipv6 = qemu_opt_get(opts, "ipv6"),
.ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
};
+ } else if (fd) {
+ addr->type = SOCKET_ADDRESS_LEGACY_KIND_FD;
+ addr->u.fd.data = g_new(String, 1);
+ addr->u.fd.data->str = g_strdup(fd);
} else {
g_assert_not_reached();
}
diff --git a/chardev/char.c b/chardev/char.c
index 3e14de1920..4cdf240610 100644
--- a/chardev/char.c
+++ b/chardev/char.c
@@ -804,6 +804,9 @@ QemuOptsList qemu_chardev_opts = {
},{
.name = "port",
.type = QEMU_OPT_STRING,
+ },{
+ .name = "fd",
+ .type = QEMU_OPT_STRING,
},{
.name = "localaddr",
.type = QEMU_OPT_STRING,
diff --git a/tests/test-char.c b/tests/test-char.c
index 911e3f6e8d..ee9df3d171 100644
--- a/tests/test-char.c
+++ b/tests/test-char.c
@@ -301,9 +301,8 @@ static int socket_can_read_hello(void *opaque)
return 10;
}
-static void char_socket_test(void)
+static void char_socket_test_common(Chardev *chr)
{
- Chardev *chr = qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
Chardev *chr_client;
QObject *addr;
QDict *qdict;
@@ -358,6 +357,47 @@ static void char_socket_test(void)
object_unparent(OBJECT(chr));
}
+
+static void char_socket_basic_test(void)
+{
+ Chardev *chr = qemu_chr_new("server", "tcp:127.0.0.1:0,server,nowait");
+
+ char_socket_test_common(chr);
+}
+
+
+static void char_socket_fdpass_test(void)
+{
+ Chardev *chr;
+ char *optstr;
+ QemuOpts *opts;
+ int fd;
+ SocketAddress *addr = g_new0(SocketAddress, 1);
+
+ addr->type = SOCKET_ADDRESS_TYPE_INET;
+ addr->u.inet.host = g_strdup("127.0.0.1");
+ addr->u.inet.port = g_strdup("0");
+
+ fd = socket_listen(addr, &error_abort);
+ g_assert(fd >= 0);
+
+ qapi_free_SocketAddress(addr);
+
+ optstr = g_strdup_printf("socket,id=cdev,fd=%d,server,nowait", fd);
+
+ opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"),
+ optstr, true);
+ g_free(optstr);
+ g_assert_nonnull(opts);
+
+ chr = qemu_chr_new_from_opts(opts, &error_abort);
+
+ qemu_opts_del(opts);
+
+ char_socket_test_common(chr);
+}
+
+
#ifndef _WIN32
static void char_pipe_test(void)
{
@@ -774,7 +814,8 @@ int main(int argc, char **argv)
#ifndef _WIN32
g_test_add_func("/char/file-fifo", char_file_fifo_test);
#endif
- g_test_add_func("/char/socket", char_socket_test);
+ g_test_add_func("/char/socket/basic", char_socket_basic_test);
+ g_test_add_func("/char/socket/fdpass", char_socket_fdpass_test);
g_test_add_func("/char/udp", char_udp_test);
#ifdef HAVE_CHARDEV_SERIAL
g_test_add_func("/char/serial", char_serial_test);
--
2.14.3
next prev parent reply other threads:[~2018-02-05 15:27 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-05 15:24 [Qemu-devel] [PATCH v4 0/9] Enable passing pre-opened chardev socket FD Daniel P. Berrangé
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 1/9] char: don't silently skip tn3270 protocol init when TLS is enabled Daniel P. Berrangé
2018-02-05 16:10 ` Cornelia Huck
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 2/9] cutils: add qemu_strtoi & qemu_strtoui parsers for int/unsigned int types Daniel P. Berrangé
2018-02-05 19:37 ` Eric Blake
2018-02-07 16:29 ` Daniel P. Berrangé
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 3/9] sockets: pull code for testing IP availability out of specific test Daniel P. Berrangé
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 4/9] sockets: strengthen test suite IP protocol availability checks Daniel P. Berrangé
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 5/9] sockets: move fd_is_socket() into common sockets code Daniel P. Berrangé
2018-02-05 16:13 ` Marc-Andre Lureau
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 6/9] sockets: check that the named file descriptor is a socket Daniel P. Berrangé
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 7/9] sockets: allow SocketAddress 'fd' to reference numeric file descriptors Daniel P. Berrangé
2018-02-05 19:42 ` Eric Blake
2018-02-06 9:13 ` Daniel P. Berrangé
2018-02-06 14:48 ` Eric Blake
2018-03-12 12:44 ` Daniel P. Berrangé
2018-02-05 15:24 ` [Qemu-devel] [PATCH v4 8/9] char: refactor parsing of socket address information Daniel P. Berrangé
2018-02-05 15:24 ` Daniel P. Berrangé [this message]
2018-02-05 17:33 ` [Qemu-devel] [PATCH v4 0/9] Enable passing pre-opened chardev socket FD no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180205152455.12088-10-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).