From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, berrange@redhat.com,
qemu-devel@nongnu.org, mreitz@redhat.com, kraxel@redhat.com,
den@openvz.org
Subject: [PATCH 3/4] io/channel-socket: implement non-blocking connect
Date: Mon, 20 Jul 2020 21:07:14 +0300 [thread overview]
Message-ID: <20200720180715.10521-4-vsementsov@virtuozzo.com> (raw)
In-Reply-To: <20200720180715.10521-1-vsementsov@virtuozzo.com>
Utilize new socket API to make a non-blocking connect for inet sockets.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
---
include/io/channel-socket.h | 14 +++++++
io/channel-socket.c | 74 +++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+)
diff --git a/include/io/channel-socket.h b/include/io/channel-socket.h
index 777ff5954e..82e868bc02 100644
--- a/include/io/channel-socket.h
+++ b/include/io/channel-socket.h
@@ -94,6 +94,20 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
SocketAddress *addr,
Error **errp);
+/**
+ * qio_channel_socket_connect_non_blocking_sync:
+ * @ioc: the socket channel object
+ * @addr: the address to connect to
+ * @errp: pointer to a NULL-initialized error object
+ *
+ * Attempt to connect to the address @addr using non-blocking mode of
+ * the socket. Function is synchronous, but being called from
+ * coroutine context will yield during connect operation.
+ */
+int qio_channel_socket_connect_non_blocking_sync(QIOChannelSocket *ioc,
+ SocketAddress *addr,
+ Error **errp);
+
/**
* qio_channel_socket_connect_async:
* @ioc: the socket channel object
diff --git a/io/channel-socket.c b/io/channel-socket.c
index e1b4667087..076de7578a 100644
--- a/io/channel-socket.c
+++ b/io/channel-socket.c
@@ -22,6 +22,7 @@
#include "qapi/error.h"
#include "qapi/qapi-visit-sockets.h"
#include "qemu/module.h"
+#include "qemu/sockets.h"
#include "io/channel-socket.h"
#include "io/channel-watch.h"
#include "trace.h"
@@ -29,6 +30,8 @@
#define SOCKET_MAX_FDS 16
+static int qio_channel_socket_close(QIOChannel *ioc, Error **errp);
+
SocketAddress *
qio_channel_socket_get_local_address(QIOChannelSocket *ioc,
Error **errp)
@@ -157,6 +160,77 @@ int qio_channel_socket_connect_sync(QIOChannelSocket *ioc,
return 0;
}
+static int qio_channel_inet_connect_non_blocking_sync(QIOChannelSocket *ioc,
+ InetSocketAddress *addr, Error **errp)
+{
+ Error *local_err = NULL;
+ struct addrinfo *infos, *info;
+ int sock = -1;
+
+ infos = inet_parse_connect_saddr(addr, errp);
+ if (!infos) {
+ return -1;
+ }
+
+ for (info = infos; info != NULL; info = info->ai_next) {
+ bool in_progress;
+
+ error_free(local_err);
+ local_err = NULL;
+
+ sock = inet_connect_addr(addr, info, false, &in_progress, &local_err);
+ if (sock < 0) {
+ continue;
+ }
+
+ if (qio_channel_socket_set_fd(ioc, sock, &local_err) < 0) {
+ close(sock);
+ continue;
+ }
+
+ if (in_progress) {
+ if (qemu_in_coroutine()) {
+ qio_channel_yield(QIO_CHANNEL(ioc), G_IO_OUT);
+ } else {
+ qio_channel_wait(QIO_CHANNEL(ioc), G_IO_OUT);
+ }
+ if (socket_check(sock, &local_err) < 0) {
+ qio_channel_socket_close(QIO_CHANNEL(ioc), NULL);
+ continue;
+ }
+ }
+
+ break;
+ }
+
+ freeaddrinfo(infos);
+
+ error_propagate(errp, local_err);
+ return sock;
+}
+
+int qio_channel_socket_connect_non_blocking_sync(QIOChannelSocket *ioc,
+ SocketAddress *addr,
+ Error **errp)
+{
+ if (addr->type == SOCKET_ADDRESS_TYPE_INET) {
+ return qio_channel_inet_connect_non_blocking_sync(ioc, &addr->u.inet,
+ errp);
+ } else {
+ /*
+ * TODO: implement non-blocking connect for other socket types.
+ * For now just use blocking connect, and then make socket non-blocking
+ * for consistancy.
+ */
+ int ret = qio_channel_socket_connect_sync(ioc, addr, errp);
+
+ if (ret < 0) {
+ return ret;
+ }
+
+ return qio_channel_set_blocking(QIO_CHANNEL(ioc), false, errp);
+ }
+}
static void qio_channel_socket_connect_worker(QIOTask *task,
gpointer opaque)
--
2.21.0
next prev parent reply other threads:[~2020-07-20 18:12 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-20 18:07 [PATCH for-5.1? 0/4] non-blocking connect Vladimir Sementsov-Ogievskiy
2020-07-20 18:07 ` [PATCH 1/4] qemu-sockets: refactor inet_connect_addr Vladimir Sementsov-Ogievskiy
2020-07-20 18:07 ` [PATCH 2/4] qemu-sockets: implement non-blocking connect interface Vladimir Sementsov-Ogievskiy
2020-07-20 18:07 ` Vladimir Sementsov-Ogievskiy [this message]
2020-07-20 18:29 ` [PATCH 3/4] io/channel-socket: implement non-blocking connect Daniel P. Berrangé
2020-07-22 11:00 ` Vladimir Sementsov-Ogievskiy
2020-07-22 11:21 ` Daniel P. Berrangé
2020-07-22 12:43 ` Vladimir Sementsov-Ogievskiy
2020-07-22 12:53 ` Daniel P. Berrangé
2020-07-22 13:47 ` Vladimir Sementsov-Ogievskiy
2020-07-22 15:04 ` Vladimir Sementsov-Ogievskiy
2020-07-22 15:21 ` Daniel P. Berrangé
2020-07-22 15:40 ` Vladimir Sementsov-Ogievskiy
2020-07-22 15:43 ` Daniel P. Berrangé
2020-07-22 15:56 ` Vladimir Sementsov-Ogievskiy
2020-07-20 18:07 ` [PATCH 4/4] block/nbd: use non-blocking connect: fix vm hang on connect() Vladimir Sementsov-Ogievskiy
2020-07-23 19:35 ` [PATCH for-5.1? 0/4] non-blocking connect Eric Blake
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=20200720180715.10521-4-vsementsov@virtuozzo.com \
--to=vsementsov@virtuozzo.com \
--cc=berrange@redhat.com \
--cc=den@openvz.org \
--cc=kraxel@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--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).