From nobody Thu Nov 14 23:10:10 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1666344252843993.5429664545316; Fri, 21 Oct 2022 02:24:12 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloG9-0007Xq-GV for importer@patchew.org; Fri, 21 Oct 2022 05:24:10 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2q-00053G-Dm for importer@patchew.org; Fri, 21 Oct 2022 05:10:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2U-0003fs-NB for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:02 -0400 Received: from mout.kundenserver.de ([217.72.192.75]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2Q-00022b-GU for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:02 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MFspV-1otE9s3MGV-00HN31; Fri, 21 Oct 2022 11:09:48 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Thomas Huth , xen-devel@lists.xenproject.org, "Dr. David Alan Gilbert" , Laurent Vivier , Anthony Perard , Stefan Weil , David Gibson , Stefano Stabellini , Paul Durrant , Eric Blake , "Michael S. Tsirkin" , Jason Wang , Paolo Bonzini , Markus Armbruster , Samuel Thibault , Greg Kurz , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v14 15/17] net: stream: move to QIO to enable additional parameters Date: Fri, 21 Oct 2022 11:09:20 +0200 Message-Id: <20221021090922.170074-16-lvivier@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20221021090922.170074-1-lvivier@redhat.com> References: <20221021090922.170074-1-lvivier@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:ogVtRU/Y8L0DEFpJBE7pyNsAFn6O11+IaF6Qh70AAhF4CewGHia TwEr/vrVnn42KpXnWvAfKIt9AJZXfonMR0LD9WOyYBj0E1ypLh0CoiQcASOLNVYyMuy5E3v M3kCzvk2E45Hke1xKhyGvHvYk+j8XJtXzNhFGsPDje3KRXwdEtySNhqkfdeyksJ9pU6DViQ 8njk5A+4lJML2xUUlGUng== X-UI-Out-Filterresults: notjunk:1;V03:K0:d0GFfGQb3tw=:CJZXm63r4DH3ggK54l2r2Y F0Hq6Gb1/Tf2hc0CepLLNNUa1+i3Hmowpr31nDMovaXRcwqUXdLQQeDCTyS1TiHGHWNjFKdyb jUSs0LZX0KB3sx0vjFnGFp1fhRAYeqSWvvs+1XQIIURpk8Rmesjz5fhhy1R8IxYMNuLWny0ak +YOm11ZrWJdLO36hpBG27At5cWo3Xbv6DJUoBts3ba1t/3skd7AJZ0HXRrtRBokKxBbVVR+/G ZQH5DrlJ9DZh5E6MAl2ngthXxsogMb8i2YFb6YnkhoHOrIU9RAOeV0/b3AelNKwLzPkFhN96p WqXG7LtdVt1+qsKJXYUoBH6IGoTijK8QfqHXtZdO31aKxMEdp5XkVe6lNt/fevsgElKPywEt2 YcXw8ZVrlwTqiwrJAr92bCxj4FQryRoFBn1VqHKabiwxtWSpVRxbGxTKyzie5BWmkGx17kUVE MXbxXjTpuwTTF6IcDtZmBurucj7ynRv+xrGb5Mu6dfYnqutx0J6Z1Wom22aF1W2A+nQGzoREe STxeloc0zSKldnop3diocK6B7L1JlqX+4673PCudQhZbkzBKkj1ukCzGyEZ4xi60FQ4kGJoDD 1mZHjW6O98TL3Arwt2u186grZ1Hs0De3j/TzuF5mTLa6IhMMFqlXxlYLB0H/7r2xfPss4f0kN Gwp6xuTh3etKS+1fAvzjmDnN2C4GpwSB8cdCNdZnCLNN18fMVtMbZTEu+8/Ki34lpoRfCT5xO grdJF+crzXp7lw7W7/Hd2qWeEkUaeMMDqNJ/9hCMXTy/mJ+S/yBSnzmwGXNzFzdpYKrQ2ibv5 o6Zd2Vs Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: permerror client-ip=217.72.192.75; envelope-from=lvivier@redhat.com; helo=mout.kundenserver.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, SPF_FAIL=0.001, SPF_HELO_NONE=0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666344253649100001 Content-Type: text/plain; charset="utf-8" Use QIOChannel, QIOChannelSocket and QIONetListener. This allows net/stream to use all the available parameters provided by SocketAddress. Signed-off-by: Laurent Vivier Acked-by: Michael S. Tsirkin --- net/stream.c | 492 +++++++++++++++++------------------------------- qemu-options.hx | 4 +- 2 files changed, 178 insertions(+), 318 deletions(-) diff --git a/net/stream.c b/net/stream.c index 884f473018da..54c67e14d240 100644 --- a/net/stream.c +++ b/net/stream.c @@ -35,48 +35,36 @@ #include "qemu/iov.h" #include "qemu/main-loop.h" #include "qemu/cutils.h" +#include "io/channel.h" +#include "io/channel-socket.h" +#include "io/net-listener.h" =20 typedef struct NetStreamState { NetClientState nc; - int listen_fd; - int fd; + QIOChannel *listen_ioc; + QIONetListener *listener; + QIOChannel *ioc; + guint ioc_read_tag; + guint ioc_write_tag; SocketReadState rs; unsigned int send_index; /* number of bytes sent*/ - bool read_poll; /* waiting to receive data? */ - bool write_poll; /* waiting to transmit data? */ } NetStreamState; =20 -static void net_stream_send(void *opaque); -static void net_stream_accept(void *opaque); -static void net_stream_writable(void *opaque); +static void net_stream_listen(QIONetListener *listener, + QIOChannelSocket *cioc, + void *opaque); =20 -static void net_stream_update_fd_handler(NetStreamState *s) +static gboolean net_stream_writable(QIOChannel *ioc, + GIOCondition condition, + gpointer data) { - qemu_set_fd_handler(s->fd, - s->read_poll ? net_stream_send : NULL, - s->write_poll ? net_stream_writable : NULL, - s); -} - -static void net_stream_read_poll(NetStreamState *s, bool enable) -{ - s->read_poll =3D enable; - net_stream_update_fd_handler(s); -} - -static void net_stream_write_poll(NetStreamState *s, bool enable) -{ - s->write_poll =3D enable; - net_stream_update_fd_handler(s); -} - -static void net_stream_writable(void *opaque) -{ - NetStreamState *s =3D opaque; + NetStreamState *s =3D data; =20 - net_stream_write_poll(s, false); + s->ioc_write_tag =3D 0; =20 qemu_flush_queued_packets(&s->nc); + + return G_SOURCE_REMOVE; } =20 static ssize_t net_stream_receive(NetClientState *nc, const uint8_t *buf, @@ -93,13 +81,15 @@ static ssize_t net_stream_receive(NetClientState *nc, c= onst uint8_t *buf, .iov_len =3D size, }, }; + struct iovec local_iov[2]; + unsigned int nlocal_iov; size_t remaining; ssize_t ret; =20 remaining =3D iov_size(iov, 2) - s->send_index; - ret =3D iov_send(s->fd, iov, 2, s->send_index, remaining); - - if (ret =3D=3D -1 && errno =3D=3D EAGAIN) { + nlocal_iov =3D iov_copy(local_iov, 2, iov, 2, s->send_index, remaining= ); + ret =3D qio_channel_writev(s->ioc, local_iov, nlocal_iov, NULL); + if (ret =3D=3D QIO_CHANNEL_ERR_BLOCK) { ret =3D 0; /* handled further down */ } if (ret =3D=3D -1) { @@ -108,19 +98,25 @@ static ssize_t net_stream_receive(NetClientState *nc, = const uint8_t *buf, } if (ret < (ssize_t)remaining) { s->send_index +=3D ret; - net_stream_write_poll(s, true); + s->ioc_write_tag =3D qio_channel_add_watch(s->ioc, G_IO_OUT, + net_stream_writable, s, N= ULL); return 0; } s->send_index =3D 0; return size; } =20 +static gboolean net_stream_send(QIOChannel *ioc, + GIOCondition condition, + gpointer data); + static void net_stream_send_completed(NetClientState *nc, ssize_t len) { NetStreamState *s =3D DO_UPCAST(NetStreamState, nc, nc); =20 - if (!s->read_poll) { - net_stream_read_poll(s, true); + if (!s->ioc_read_tag) { + s->ioc_read_tag =3D qio_channel_add_watch(s->ioc, G_IO_IN, + net_stream_send, s, NULL); } } =20 @@ -131,19 +127,24 @@ static void net_stream_rs_finalize(SocketReadState *r= s) if (qemu_send_packet_async(&s->nc, rs->buf, rs->packet_len, net_stream_send_completed) =3D=3D 0) { - net_stream_read_poll(s, false); + if (s->ioc_read_tag) { + g_source_remove(s->ioc_read_tag); + s->ioc_read_tag =3D 0; + } } } =20 -static void net_stream_send(void *opaque) +static gboolean net_stream_send(QIOChannel *ioc, + GIOCondition condition, + gpointer data) { - NetStreamState *s =3D opaque; + NetStreamState *s =3D data; int size; int ret; - uint8_t buf1[NET_BUFSIZE]; - const uint8_t *buf; + char buf1[NET_BUFSIZE]; + const char *buf; =20 - size =3D recv(s->fd, buf1, sizeof(buf1), 0); + size =3D qio_channel_read(s->ioc, buf1, sizeof(buf1), NULL); if (size < 0) { if (errno !=3D EWOULDBLOCK) { goto eoc; @@ -151,51 +152,63 @@ static void net_stream_send(void *opaque) } else if (size =3D=3D 0) { /* end of connection */ eoc: - net_stream_read_poll(s, false); - net_stream_write_poll(s, false); - if (s->listen_fd !=3D -1) { - qemu_set_fd_handler(s->listen_fd, net_stream_accept, NULL, s); + s->ioc_read_tag =3D 0; + if (s->ioc_write_tag) { + g_source_remove(s->ioc_write_tag); + s->ioc_write_tag =3D 0; } - closesocket(s->fd); + if (s->listener) { + qio_net_listener_set_client_func(s->listener, net_stream_liste= n, + s, NULL); + } + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; =20 - s->fd =3D -1; net_socket_rs_init(&s->rs, net_stream_rs_finalize, false); s->nc.link_down =3D true; qemu_set_info_str(&s->nc, ""); =20 - return; + return G_SOURCE_REMOVE; } buf =3D buf1; =20 - ret =3D net_fill_rstate(&s->rs, buf, size); + ret =3D net_fill_rstate(&s->rs, (const uint8_t *)buf, size); =20 if (ret =3D=3D -1) { goto eoc; } + + return G_SOURCE_CONTINUE; } =20 static void net_stream_cleanup(NetClientState *nc) { NetStreamState *s =3D DO_UPCAST(NetStreamState, nc, nc); - if (s->fd !=3D -1) { - net_stream_read_poll(s, false); - net_stream_write_poll(s, false); - close(s->fd); - s->fd =3D -1; + if (s->ioc) { + if (QIO_CHANNEL_SOCKET(s->ioc)->fd !=3D -1) { + if (s->ioc_read_tag) { + g_source_remove(s->ioc_read_tag); + s->ioc_read_tag =3D 0; + } + if (s->ioc_write_tag) { + g_source_remove(s->ioc_write_tag); + s->ioc_write_tag =3D 0; + } + } + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; } - if (s->listen_fd !=3D -1) { - qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); - closesocket(s->listen_fd); - s->listen_fd =3D -1; + if (s->listen_ioc) { + if (s->listener) { + qio_net_listener_disconnect(s->listener); + object_unref(OBJECT(s->listener)); + s->listener =3D NULL; + } + object_unref(OBJECT(s->listen_ioc)); + s->listen_ioc =3D NULL; } } =20 -static void net_stream_connect(void *opaque) -{ - NetStreamState *s =3D opaque; - net_stream_read_poll(s, true); -} - static NetClientInfo net_stream_info =3D { .type =3D NET_CLIENT_DRIVER_STREAM, .size =3D sizeof(NetStreamState), @@ -203,76 +216,66 @@ static NetClientInfo net_stream_info =3D { .cleanup =3D net_stream_cleanup, }; =20 -static NetStreamState *net_stream_fd_init(NetClientState *peer, - const char *model, - const char *name, - int fd, int is_connected) +static void net_stream_listen(QIONetListener *listener, + QIOChannelSocket *cioc, + void *opaque) { - NetClientState *nc; - NetStreamState *s; - - nc =3D qemu_new_net_client(&net_stream_info, peer, model, name); + NetStreamState *s =3D opaque; + SocketAddress *addr; + char *uri; =20 - qemu_set_info_str(nc, "fd=3D%d", fd); + object_ref(OBJECT(cioc)); =20 - s =3D DO_UPCAST(NetStreamState, nc, nc); + qio_net_listener_set_client_func(s->listener, NULL, s, NULL); =20 - s->fd =3D fd; - s->listen_fd =3D -1; - net_socket_rs_init(&s->rs, net_stream_rs_finalize, false); + s->ioc =3D QIO_CHANNEL(cioc); + qio_channel_set_name(s->ioc, "stream-server"); + s->nc.link_down =3D false; =20 - /* Disable Nagle algorithm on TCP sockets to reduce latency */ - socket_set_nodelay(fd); + s->ioc_read_tag =3D qio_channel_add_watch(s->ioc, G_IO_IN, net_stream_= send, + s, NULL); =20 - if (is_connected) { - net_stream_connect(s); + if (cioc->localAddr.ss_family =3D=3D AF_UNIX) { + addr =3D qio_channel_socket_get_local_address(cioc, NULL); } else { - qemu_set_fd_handler(s->fd, NULL, net_stream_connect, s); + addr =3D qio_channel_socket_get_remote_address(cioc, NULL); } - return s; + g_assert(addr !=3D NULL); + uri =3D socket_uri(addr); + qemu_set_info_str(&s->nc, uri); + g_free(uri); + qapi_free_SocketAddress(addr); } =20 -static void net_stream_accept(void *opaque) +static void net_stream_server_listening(QIOTask *task, gpointer opaque) { NetStreamState *s =3D opaque; - struct sockaddr_storage saddr; - socklen_t len; - int fd; - - for (;;) { - len =3D sizeof(saddr); - fd =3D qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len); - if (fd < 0 && errno !=3D EINTR) { - return; - } else if (fd >=3D 0) { - qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); - break; - } - } + QIOChannelSocket *listen_sioc =3D QIO_CHANNEL_SOCKET(s->listen_ioc); + SocketAddress *addr; + int ret; =20 - s->fd =3D fd; - s->nc.link_down =3D false; - net_stream_connect(s); - switch (saddr.ss_family) { - case AF_INET: { - struct sockaddr_in *saddr_in =3D (struct sockaddr_in *)&saddr; - - qemu_set_info_str(&s->nc, "connection from %s:%d", - inet_ntoa(saddr_in->sin_addr), - ntohs(saddr_in->sin_port)); - break; + if (listen_sioc->fd < 0) { + qemu_set_info_str(&s->nc, "connection error"); + return; } - case AF_UNIX: { - struct sockaddr_un saddr_un; =20 - len =3D sizeof(saddr_un); - getsockname(s->listen_fd, (struct sockaddr *)&saddr_un, &len); - qemu_set_info_str(&s->nc, "connect from %s", saddr_un.sun_path); - break; - } - default: - g_assert_not_reached(); + addr =3D qio_channel_socket_get_local_address(listen_sioc, NULL); + g_assert(addr !=3D NULL); + ret =3D qemu_socket_try_set_nonblock(listen_sioc->fd); + if (addr->type =3D=3D SOCKET_ADDRESS_TYPE_FD && ret < 0) { + qemu_set_info_str(&s->nc, "can't use file descriptor %s (errno %d)= ", + addr->u.fd.str, -ret); + return; } + g_assert(ret =3D=3D 0); + qapi_free_SocketAddress(addr); + + s->nc.link_down =3D true; + s->listener =3D qio_net_listener_new(); + + net_socket_rs_init(&s->rs, net_stream_rs_finalize, false); + qio_net_listener_set_client_func(s->listener, net_stream_listen, s, NU= LL); + qio_net_listener_add(s->listener, listen_sioc); } =20 static int net_stream_server_init(NetClientState *peer, @@ -283,105 +286,61 @@ static int net_stream_server_init(NetClientState *pe= er, { NetClientState *nc; NetStreamState *s; - int fd, ret; + QIOChannelSocket *listen_sioc =3D qio_channel_socket_new(); =20 - switch (addr->type) { - case SOCKET_ADDRESS_TYPE_INET: { - struct sockaddr_in saddr_in; - - if (convert_host_port(&saddr_in, addr->u.inet.host, addr->u.inet.p= ort, - errp) < 0) { - return -1; - } - - fd =3D qemu_socket(PF_INET, SOCK_STREAM, 0); - if (fd < 0) { - error_setg_errno(errp, errno, "can't create stream socket"); - return -1; - } - qemu_socket_set_nonblock(fd); + nc =3D qemu_new_net_client(&net_stream_info, peer, model, name); + s =3D DO_UPCAST(NetStreamState, nc, nc); =20 - socket_set_fast_reuse(fd); + s->listen_ioc =3D QIO_CHANNEL(listen_sioc); + qio_channel_socket_listen_async(listen_sioc, addr, 0, + net_stream_server_listening, s, + NULL, NULL); =20 - ret =3D bind(fd, (struct sockaddr *)&saddr_in, sizeof(saddr_in)); - if (ret < 0) { - error_setg_errno(errp, errno, "can't bind ip=3D%s to socket", - inet_ntoa(saddr_in.sin_addr)); - closesocket(fd); - return -1; - } - break; - } - case SOCKET_ADDRESS_TYPE_UNIX: { - struct sockaddr_un saddr_un; - - ret =3D unlink(addr->u.q_unix.path); - if (ret < 0 && errno !=3D ENOENT) { - error_setg_errno(errp, errno, "failed to unlink socket %s", - addr->u.q_unix.path); - return -1; - } + return 0; +} =20 - saddr_un.sun_family =3D PF_UNIX; - ret =3D snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s= ", - addr->u.q_unix.path); - if (ret < 0 || ret >=3D sizeof(saddr_un.sun_path)) { - error_setg(errp, "UNIX socket path '%s' is too long", - addr->u.q_unix.path); - error_append_hint(errp, "Path must be less than %zu bytes\n", - sizeof(saddr_un.sun_path)); - return -1; - } +static void net_stream_client_connected(QIOTask *task, gpointer opaque) +{ + NetStreamState *s =3D opaque; + QIOChannelSocket *sioc =3D QIO_CHANNEL_SOCKET(s->ioc); + SocketAddress *addr; + gchar *uri; + int ret; =20 - fd =3D qemu_socket(PF_UNIX, SOCK_STREAM, 0); - if (fd < 0) { - error_setg_errno(errp, errno, "can't create stream socket"); - return -1; - } - qemu_socket_set_nonblock(fd); - - ret =3D bind(fd, (struct sockaddr *)&saddr_un, sizeof(saddr_un)); - if (ret < 0) { - error_setg_errno(errp, errno, "can't create socket with path: = %s", - saddr_un.sun_path); - closesocket(fd); - return -1; - } - break; - } - case SOCKET_ADDRESS_TYPE_FD: - fd =3D monitor_fd_param(monitor_cur(), addr->u.fd.str, errp); - if (fd =3D=3D -1) { - return -1; - } - ret =3D qemu_socket_try_set_nonblock(fd); - if (ret < 0) { - error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d= ", - name, fd); - return -1; - } - break; - default: - error_setg(errp, "only support inet or fd type"); - return -1; + if (sioc->fd < 0) { + qemu_set_info_str(&s->nc, "connection error"); + goto error; } =20 - ret =3D listen(fd, 0); - if (ret < 0) { - error_setg_errno(errp, errno, "can't listen on socket"); - closesocket(fd); - return -1; + addr =3D qio_channel_socket_get_remote_address(sioc, NULL); + g_assert(addr !=3D NULL); + uri =3D socket_uri(addr); + qemu_set_info_str(&s->nc, uri); + g_free(uri); + + ret =3D qemu_socket_try_set_nonblock(sioc->fd); + if (addr->type =3D=3D SOCKET_ADDRESS_TYPE_FD && ret < 0) { + qemu_set_info_str(&s->nc, "can't use file descriptor %s (errno %d)= ", + addr->u.fd.str, -ret); + qapi_free_SocketAddress(addr); + goto error; } + g_assert(ret =3D=3D 0); =20 - nc =3D qemu_new_net_client(&net_stream_info, peer, model, name); - s =3D DO_UPCAST(NetStreamState, nc, nc); - s->fd =3D -1; - s->listen_fd =3D fd; - s->nc.link_down =3D true; net_socket_rs_init(&s->rs, net_stream_rs_finalize, false); =20 - qemu_set_fd_handler(s->listen_fd, net_stream_accept, NULL, s); - return 0; + /* Disable Nagle algorithm on TCP sockets to reduce latency */ + qio_channel_set_delay(s->ioc, false); + + s->ioc_read_tag =3D qio_channel_add_watch(s->ioc, G_IO_IN, net_stream_= send, + s, NULL); + s->nc.link_down =3D false; + qapi_free_SocketAddress(addr); + + return; +error: + object_unref(OBJECT(s->ioc)); + s->ioc =3D NULL; } =20 static int net_stream_client_init(NetClientState *peer, @@ -391,118 +350,19 @@ static int net_stream_client_init(NetClientState *pe= er, Error **errp) { NetStreamState *s; - struct sockaddr_in saddr_in; - struct sockaddr_un saddr_un; - int fd, connected, ret; - - switch (addr->type) { - case SOCKET_ADDRESS_TYPE_INET: - if (convert_host_port(&saddr_in, addr->u.inet.host, addr->u.inet.p= ort, - errp) < 0) { - return -1; - } + NetClientState *nc; + QIOChannelSocket *sioc =3D qio_channel_socket_new(); =20 - fd =3D qemu_socket(PF_INET, SOCK_STREAM, 0); - if (fd < 0) { - error_setg_errno(errp, errno, "can't create stream socket"); - return -1; - } - qemu_socket_set_nonblock(fd); - - connected =3D 0; - for (;;) { - ret =3D connect(fd, (struct sockaddr *)&saddr_in, sizeof(saddr= _in)); - if (ret < 0) { - if (errno =3D=3D EINTR || errno =3D=3D EWOULDBLOCK) { - /* continue */ - } else if (errno =3D=3D EINPROGRESS || - errno =3D=3D EALREADY) { - break; - } else { - error_setg_errno(errp, errno, "can't connect socket"); - closesocket(fd); - return -1; - } - } else { - connected =3D 1; - break; - } - } - break; - case SOCKET_ADDRESS_TYPE_UNIX: - saddr_un.sun_family =3D PF_UNIX; - ret =3D snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s= ", - addr->u.q_unix.path); - if (ret < 0 || ret >=3D sizeof(saddr_un.sun_path)) { - error_setg(errp, "UNIX socket path '%s' is too long", - addr->u.q_unix.path); - error_append_hint(errp, "Path must be less than %zu bytes\n", - sizeof(saddr_un.sun_path)); - return -1; - } + nc =3D qemu_new_net_client(&net_stream_info, peer, model, name); + s =3D DO_UPCAST(NetStreamState, nc, nc); =20 - fd =3D qemu_socket(PF_UNIX, SOCK_STREAM, 0); - if (fd < 0) { - error_setg_errno(errp, errno, "can't create stream socket"); - return -1; - } - qemu_socket_set_nonblock(fd); - - connected =3D 0; - for (;;) { - ret =3D connect(fd, (struct sockaddr *)&saddr_un, sizeof(saddr= _un)); - if (ret < 0) { - if (errno =3D=3D EINTR || errno =3D=3D EWOULDBLOCK) { - /* continue */ - } else if (errno =3D=3D EAGAIN || - errno =3D=3D EALREADY) { - break; - } else { - error_setg_errno(errp, errno, "can't connect socket"); - closesocket(fd); - return -1; - } - } else { - connected =3D 1; - break; - } - } - break; - case SOCKET_ADDRESS_TYPE_FD: - fd =3D monitor_fd_param(monitor_cur(), addr->u.fd.str, errp); - if (fd =3D=3D -1) { - return -1; - } - ret =3D qemu_socket_try_set_nonblock(fd); - if (ret < 0) { - error_setg_errno(errp, -ret, "%s: Can't use file descriptor %d= ", - name, fd); - return -1; - } - connected =3D 1; - break; - default: - error_setg(errp, "only support inet, unix or fd type"); - return -1; - } + s->ioc =3D QIO_CHANNEL(sioc); + s->nc.link_down =3D true; + + qio_channel_socket_connect_async(sioc, addr, + net_stream_client_connected, s, + NULL, NULL); =20 - s =3D net_stream_fd_init(peer, model, name, fd, connected); - - switch (addr->type) { - case SOCKET_ADDRESS_TYPE_INET: - qemu_set_info_str(&s->nc, "connect to %s:%d", - inet_ntoa(saddr_in.sin_addr), - ntohs(saddr_in.sin_port)); - break; - case SOCKET_ADDRESS_TYPE_UNIX: - qemu_set_info_str(&s->nc, " connect to %s", saddr_un.sun_path); - break; - case SOCKET_ADDRESS_TYPE_FD: - qemu_set_info_str(&s->nc, "connect to fd %d", fd); - break; - default: - g_assert_not_reached(); - } return 0; } =20 diff --git a/qemu-options.hx b/qemu-options.hx index fafb214cb89f..a91f96a264cc 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2772,8 +2772,8 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, "-netdev socket,id=3Dstr[,fd=3Dh][,udp=3Dhost:port][,localaddr=3Dhost:= port]\n" " configure a network backend to connect to another net= work\n" " using an UDP tunnel\n" - "-netdev stream,id=3Dstr[,server=3Don|off],addr.type=3Dinet,addr.host= =3Dhost,addr.port=3Dport\n" - "-netdev stream,id=3Dstr[,server=3Don|off],addr.type=3Dunix,addr.path= =3Dpath\n" + "-netdev stream,id=3Dstr[,server=3Don|off],addr.type=3Dinet,addr.host= =3Dhost,addr.port=3Dport[,to=3Dmaxport][,numeric=3Don|off][,keep-alive=3Don= |off][,mptcp=3Don|off][,addr.ipv4=3Don|off][,addr.ipv6=3Don|off]\n" + "-netdev stream,id=3Dstr[,server=3Don|off],addr.type=3Dunix,addr.path= =3Dpath[,abstract=3Don|off][,tight=3Don|off]\n" "-netdev stream,id=3Dstr[,server=3Don|off],addr.type=3Dfd,addr.str=3Df= ile-descriptor\n" " configure a network backend to connect to another net= work\n" " using a socket connection in stream mode.\n" --=20 2.37.3