From patchwork Mon Sep 26 19:50:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Vivier X-Patchwork-Id: 12989371 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 59E31C6FA8E for ; Mon, 26 Sep 2022 19:56:28 +0000 (UTC) Received: from localhost ([::1]:35438 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ocuDL-0002rP-5Z for qemu-devel@archiver.kernel.org; Mon, 26 Sep 2022 15:56:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34278) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ocu8E-0008LC-IH for qemu-devel@nongnu.org; Mon, 26 Sep 2022 15:51:11 -0400 Received: from mout.kundenserver.de ([212.227.126.187]:50227) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ocu8A-0005wH-Ub for qemu-devel@nongnu.org; Mon, 26 Sep 2022 15:51:10 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue012 [212.227.15.167]) with ESMTPSA (Nemesis) id 1M3Eqr-1ogUdF00Cb-003a02; Mon, 26 Sep 2022 21:51:00 +0200 From: Laurent Vivier To: qemu-devel@nongnu.org Cc: Jason Wang , Greg Kurz , David Gibson , Thomas Huth , Eric Blake , "Dr. David Alan Gilbert" , Laurent Vivier , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Markus Armbruster , Stefano Brivio Subject: [PATCH v9 08/16] net: stream: add unix socket Date: Mon, 26 Sep 2022 21:50:40 +0200 Message-Id: <20220926195048.487915-9-lvivier@redhat.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220926195048.487915-1-lvivier@redhat.com> References: <20220926195048.487915-1-lvivier@redhat.com> MIME-Version: 1.0 X-Provags-ID: V03:K1:dbXNx+GsWPHudJxIjpwwryrzFJA/XS2ZZQoWT8BNT/os9195pDf 30xJ/l6KsWHvlgRLlidH34Cg3JXoMkfYJh4f3tVx7nu8AkS8ZReRCHD09z3qcqfWr7Pe+7m VvAl7A5CGgwZxsVefn9XElfUvyP3VKOJeIstxk2O+A1/rtmsxXSLExI9mDYbkFHRnuzFysY P8BySCXdszXXydnyIQBow== X-UI-Out-Filterresults: notjunk:1;V03:K0:xI11t9nzJ28=:PuOFjI2uTObq2SxNNivMMF TH1V988kYYmNNcY3wS4A+O03bumeglGWxLXxEmOfFVQNqSG+7VrlGHvGr90Cm9w91OJmqRZQC GZw0uTIzfWOeVyVUjGAaw6RXhBtjrceMTNavZH0F7iNP7VEHzA9yvnrxlMgABZp2uQTQcXKHw 2+SxUmX1uVLGin4ZNvaCnsDFWwuYzO4iSQ5oM8VkDPnY1YVvLuwc8b8jjKsmhg78nK46rtoFv U8WcGY9DpVS2x+QJtSSfG4caA4juMMTThufPiAgkKaVfPf9xPAS6fwBXDJAn/lRsGyd7nm/2z mg2Lmnv5F8r27QpKLPGuRNkPwDtmWBn636MlttIA3nVXpT3ETK861L4cBFeFZ6+/VTNFCgUZT RWsRqrRG/2h334RYh8qQY8+WW5/mU+JyLVFhOb5h7IBywDsDTknYAhM7weXsiTaKC9I0N4C79 or8iF4m8NEAxmSCy0N4U/tKgKzRQnQRw+t4D39t90zaL7rd+uJclIoC6E+GvP0qWDsH2wSCjc uXY8JQj4wtK52L/Cln3C1by9TACLOS/5u6Pg+GEtXAERH9Xbkon2/hoUmislAXiPxqgjkzoy3 mBJ09mIa5pazOOQ+z3eVXpe9BnjUMjs4D79wCNIzv/yMrXf7vpi0IuSrCeXR+5U5VLm/A72dd KFOi1oE6yjO8LfZyAMa+2Ec99Yhc06G6/mUKFoD9YMZAkrylZ9lYktq1AHAjMXUlaZZDuJhKX +/LHw8XzMWCdUcONH8thkfQ4jKsU0NmUel1OYuz/46Ig+c26OgMO9aHDUb8jWSSGn9mf/Wb/0 ZHB1lNv Received-SPF: permerror client-ip=212.227.126.187; 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, RCVD_IN_MSPIKE_H2=-0.001, 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio --- net/stream.c | 108 +++++++++++++++++++++++++++++++++++++++++++++--- qapi/net.json | 2 +- qemu-options.hx | 1 + 3 files changed, 105 insertions(+), 6 deletions(-) diff --git a/net/stream.c b/net/stream.c index 3fb899df5d2d..02967c284efc 100644 --- a/net/stream.c +++ b/net/stream.c @@ -235,7 +235,7 @@ static NetStreamState *net_stream_fd_init_stream(NetClientState *peer, static void net_stream_accept(void *opaque) { NetStreamState *s = opaque; - struct sockaddr_in saddr; + struct sockaddr_storage saddr; socklen_t len; int fd; @@ -253,9 +253,27 @@ static void net_stream_accept(void *opaque) s->fd = fd; s->nc.link_down = false; net_stream_connect(s); - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "connection from %s:%d", - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + switch (saddr.ss_family) { + case AF_INET: { + struct sockaddr_in *saddr_in = (struct sockaddr_in *)&saddr; + + snprintf(s->nc.info_str, sizeof(s->nc.info_str), + "connection from %s:%d", + inet_ntoa(saddr_in->sin_addr), ntohs(saddr_in->sin_port)); + break; + } + case AF_UNIX: { + struct sockaddr_un saddr_un; + + len = sizeof(saddr_un); + getsockname(s->listen_fd, (struct sockaddr *)&saddr_un, &len); + snprintf(s->nc.info_str, sizeof(s->nc.info_str), + "connect from %s", saddr_un.sun_path); + break; + } + default: + g_assert_not_reached(); + } } static int net_stream_server_init(NetClientState *peer, @@ -295,6 +313,43 @@ static int net_stream_server_init(NetClientState *peer, } break; } + case SOCKET_ADDRESS_TYPE_UNIX: { + struct sockaddr_un saddr_un; + + ret = unlink(addr->u.q_unix.path); + if (ret < 0 && errno != ENOENT) { + error_setg_errno(errp, errno, "failed to unlink socket %s", + addr->u.q_unix.path); + return -1; + } + + saddr_un.sun_family = PF_UNIX; + ret = snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s", + addr->u.q_unix.path); + if (ret < 0 || ret >= 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; + } + + fd = 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 = 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 = monitor_fd_param(monitor_cur(), addr->u.fd.str, errp); if (fd == -1) { @@ -380,6 +435,49 @@ static int net_stream_client_init(NetClientState *peer, ntohs(saddr_in.sin_port)); break; } + case SOCKET_ADDRESS_TYPE_UNIX: { + struct sockaddr_un saddr_un; + + saddr_un.sun_family = PF_UNIX; + ret = snprintf(saddr_un.sun_path, sizeof(saddr_un.sun_path), "%s", + addr->u.q_unix.path); + if (ret < 0 || ret >= 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; + } + + fd = 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 = 0; + for (;;) { + ret = connect(fd, (struct sockaddr *)&saddr_un, sizeof(saddr_un)); + if (ret < 0) { + if (errno == EINTR || errno == EWOULDBLOCK) { + /* continue */ + } else if (errno == EAGAIN || + errno == EALREADY) { + break; + } else { + error_setg_errno(errp, errno, "can't connect socket"); + closesocket(fd); + return -1; + } + } else { + connected = 1; + break; + } + } + info_str = g_strdup_printf(" connect to %s", saddr_un.sun_path); + break; + } case SOCKET_ADDRESS_TYPE_FD: fd = monitor_fd_param(monitor_cur(), addr->u.fd.str, errp); if (fd == -1) { @@ -395,7 +493,7 @@ static int net_stream_client_init(NetClientState *peer, info_str = g_strdup_printf("connect to fd %d", fd); break; default: - error_setg(errp, "only support inet or fd type"); + error_setg(errp, "only support inet, unix or fd type"); return -1; } diff --git a/qapi/net.json b/qapi/net.json index e02e8001a000..bb96701a49a7 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -583,7 +583,7 @@ # or connect to (server=false) # @server: create server socket (default: true) # -# Only SocketAddress types 'inet' and 'fd' are supported. +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # # Since: 7.1 ## diff --git a/qemu-options.hx b/qemu-options.hx index 8c765f345da8..7a34022ac651 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2735,6 +2735,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " configure a network backend to connect to another network\n" " using an UDP tunnel\n" "-netdev stream,id=str[,server=on|off],addr.type=inet,addr.host=host,addr.port=port\n" + "-netdev stream,id=str[,server=on|off],addr.type=unix,addr.path=path\n" "-netdev stream,id=str[,server=on|off],addr.type=fd,addr.str=h\n" " configure a network backend to connect to another network\n" " using a socket connection in stream mode.\n"