From nobody Thu Nov 14 18:13:17 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 166634446822965.19594743656626; Fri, 21 Oct 2022 02:27:48 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloJd-0005TR-Mb for importer@patchew.org; Fri, 21 Oct 2022 05:27:45 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2g-00046y-8D for importer@patchew.org; Fri, 21 Oct 2022 05:10:14 -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 1olo2C-0003FN-Cb for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:46 -0400 Received: from mout.kundenserver.de ([212.227.17.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2A-0001QE-CZ for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:44 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MsIbU-1p58Kj0saw-00tmMU; Fri, 21 Oct 2022 11:09:27 +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?= , Stefano Brivio Subject: [PATCH v14 01/17] net: introduce convert_host_port() Date: Fri, 21 Oct 2022 11:09:06 +0200 Message-Id: <20221021090922.170074-2-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:UNmkNBqxdJnZvFveDMtqcTp2IHg/srXUbTn7wvcQ6FXdR9eHdCI RymESjpbgHGgZLXTi7Cr9OYSRxR6uLn3c2MfVil4zr7qdmCHx/8lgPGug/eoDENeWDMVo/3 TIxSdTTk8O6yHb+ZZxasoaOAg3Uv/EhMd2YNxEoDuLo6ALd/Kd5hqO3zWj/PLYOw9ijj75m g5tzGHRaQlEIpdcpZHJuA== X-UI-Out-Filterresults: notjunk:1;V03:K0:s9mbqRMoCWg=:Vvu4jEFi7gSp1MxnnyOqdQ uNVgBU52KTHRU95T8ixNi4iL9ETl0St7D/EOr0FJi5lFVS1hzmI2SPUvcXK0SNzVUwJtonrDh pqI4uuRGIAS2liEZgp6rppwm39GXsLJhuW6qWSbLK6teWiWiIrg2bmxTDXab1itBBXETmjAoK AhCr6dkliXdZpRkDTD29Qw7KH3gbaQc/SmqBmizotxYRxJYUNW07+5x+tIQwY6pHaCxjGgL+y 4G37XXWiJklks26D84xZK9kiD3ZPpFOsUfpQJn2dD3LvzkWwrr9sygivnXhK7eVEQDd6s0qo+ sb8Kni9LeChuhiuvTjganF4tP4xpUP8sUkxXc+UwUuAH41TIa/3k45feMgJlYR2eh8GQv/kQf OlyuJeEbTmMjB4fI2sJPAJYZq95X0bJYxSu5u9LCojvchYBoCJdK+BS6ajh/HlnY374JzPu60 KgLBPJq/+cIM8TzgnXlk+dCaiEUx3I+pSjOX9BtDWaZ9eXK8abjfTVyhArsrla4/tTlTr8qlh 73J36nKWoYZjwE+oz5Nli9yGAO8QItb4+Oug4w3KiC/WGNGHres/DGo97Odnz1OEg2Icdozu9 xOVc/CUsBkjaJNNgJX1I+9sqvwSZxhmvkFZAw+9j/6M4/B+Lt6qQ6qGq4npvzBXrCpBzgYKLb FtchoTESY4u1Mn4+n4furBDxIUWaJGOOxDakOdC+9XxYVoZvU/MWcxSN8iHz02f1PzI9hzLJ2 2aHH+UOSsKFnPzLTe35+GiAYKU/1nVTWL/OYdbDpRDmRnFS1HHNdGVaJUwJ89DsA/+PF/z3N1 1zf05mt 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=212.227.17.13; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666344470290100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin --- include/qemu/sockets.h | 2 ++ net/net.c | 62 ++++++++++++++++++++++-------------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index 036745e58653..db4bedb6fa20 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -65,6 +65,8 @@ void socket_listen_cleanup(int fd, Error **errp); int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp= ); =20 /* Old, ipv4 only bits. Don't use for new code. */ +int convert_host_port(struct sockaddr_in *saddr, const char *host, + const char *port, Error **errp); int parse_host_port(struct sockaddr_in *saddr, const char *str, Error **errp); int socket_init(void); diff --git a/net/net.c b/net/net.c index 2db160e0634d..d2288bd3a929 100644 --- a/net/net.c +++ b/net/net.c @@ -66,55 +66,57 @@ static QTAILQ_HEAD(, NetClientState) net_clients; /***********************************************************/ /* network device redirectors */ =20 -int parse_host_port(struct sockaddr_in *saddr, const char *str, - Error **errp) +int convert_host_port(struct sockaddr_in *saddr, const char *host, + const char *port, Error **errp) { - gchar **substrings; struct hostent *he; - const char *addr, *p, *r; - int port, ret =3D 0; + const char *r; + long p; =20 memset(saddr, 0, sizeof(*saddr)); =20 - substrings =3D g_strsplit(str, ":", 2); - if (!substrings || !substrings[0] || !substrings[1]) { - error_setg(errp, "host address '%s' doesn't contain ':' " - "separating host from port", str); - ret =3D -1; - goto out; - } - - addr =3D substrings[0]; - p =3D substrings[1]; - saddr->sin_family =3D AF_INET; - if (addr[0] =3D=3D '\0') { + if (host[0] =3D=3D '\0') { saddr->sin_addr.s_addr =3D 0; } else { - if (qemu_isdigit(addr[0])) { - if (!inet_aton(addr, &saddr->sin_addr)) { + if (qemu_isdigit(host[0])) { + if (!inet_aton(host, &saddr->sin_addr)) { error_setg(errp, "host address '%s' is not a valid " - "IPv4 address", addr); - ret =3D -1; - goto out; + "IPv4 address", host); + return -1; } } else { - he =3D gethostbyname(addr); + he =3D gethostbyname(host); if (he =3D=3D NULL) { - error_setg(errp, "can't resolve host address '%s'", addr); - ret =3D -1; - goto out; + error_setg(errp, "can't resolve host address '%s'", host); + return -1; } saddr->sin_addr =3D *(struct in_addr *)he->h_addr; } } - port =3D strtol(p, (char **)&r, 0); - if (r =3D=3D p) { - error_setg(errp, "port number '%s' is invalid", p); + if (qemu_strtol(port, &r, 0, &p) !=3D 0) { + error_setg(errp, "port number '%s' is invalid", port); + return -1; + } + saddr->sin_port =3D htons(p); + return 0; +} + +int parse_host_port(struct sockaddr_in *saddr, const char *str, + Error **errp) +{ + gchar **substrings; + int ret; + + substrings =3D g_strsplit(str, ":", 2); + if (!substrings || !substrings[0] || !substrings[1]) { + error_setg(errp, "host address '%s' doesn't contain ':' " + "separating host from port", str); ret =3D -1; goto out; } - saddr->sin_port =3D htons(port); + + ret =3D convert_host_port(saddr, substrings[0], substrings[1], errp); =20 out: g_strfreev(substrings); --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666345365724219.72451860037881; Fri, 21 Oct 2022 02:42:45 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloY7-00066p-9q for importer@patchew.org; Fri, 21 Oct 2022 05:42:43 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2U-0003fS-On for importer@patchew.org; Fri, 21 Oct 2022 05:10:02 -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 1olo2D-0003GV-Eu for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:46 -0400 Received: from mout.kundenserver.de ([212.227.17.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2A-0001QS-DP for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:45 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1M2wXK-1op7Po2PFM-003KhQ; Fri, 21 Oct 2022 11:09:28 +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 02/17] net: remove the @errp argument of net_client_inits() Date: Fri, 21 Oct 2022 11:09:07 +0200 Message-Id: <20221021090922.170074-3-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:tXshrKVL+pGTR64qZzdDP2mgssWy1XD4N5JLIlGU4LQUuEupVwC 6Hv53zCa79ErbrYWRyuwZ6Zb3npWe+TZCCzKsbFf2vHGaKHBcjZ07zaJiqvhrjyiG2LsWYo MvvItkTUGQ78LwP3Oe67eQXJa7mVHIu9kaVzfwTZYpQ4YtxRzjafpO6EQx21pWtj+Zb4MYU OVKCUTKs1om02e5AF0c0A== X-UI-Out-Filterresults: notjunk:1;V03:K0:yh17WmPlzdo=:ZrtEojMwzm5gUGxZ5oS2y1 dS6Q/ZaN1X7zaqbjNcFyzDoCJ06/DQp0jG60URfoXiMJzbCLSbtCzxrY2c49MvYsu9v48ZiNy hv5IKcr2fjF0cue9raRghSRmBgugbLDe4qkIn7gyApUZwqBX/ZHU6PvBoCGowiwGgBMQz8UP5 PT1YEvho0ej11ikypGW+48BcWjYantXzw703V3+QzpHV5YFBpte5Pf1AQtXxs2hVhmCHhTjTE 3YZzphZ5TRFaYC4fxNE5hPjgqaEWU/g4OpwnLglck57PuKQZfsIQ081ITf75RExgPh1LuhjeA Jyfo3fpXuJX1zlb1JPLnlJRK6RrlWPVB3h0oMV0Q5X6IAKgsaZmmbQ0TY4DCVrZEsmKozqjy1 xGvYxYfbr93VPPZ9vEXHV8cJKIj7fXkLm0ziBy89gXdz261jM71hYhDTCXPJ6ykHXO+zJR2w3 PiAZ5gQc4/935X/+Dd96WuFkZnQ5vp5AulzKXzeV+aBDUrToYFtgLncS8eHcSijOHinpHfX0I 9DBE5bv/8RbRq/dJzmfyD5FoQsCCKD8cUI6qFzl5IfBzMpq2YnnogOvfQIxFlTUZ+FNXUixxI 41cG1egq0DMmv/1zZxuiij36Wa7XmHj715GfgIu2bL/SAEGH6MSxEz+80vVEkw9zBAf6lQ0ei DDGyT558YAVOnSt/sJNC528iBkq3rvJqiv7HJIUmso5UYN7POHBoNSpHrsW77ELz8NW+ocruE owzJNfsELDBFFctnXKInCJJnXvZ3D30Y5BaOcgl157Eoy3pswvRQP/h/JZcZG4l3+fBlX7SGq Zm8q12M 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=212.227.17.13; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666345367238100003 Content-Type: text/plain; charset="utf-8" The only caller passes &error_fatal, so use this directly in the function. It's what we do for -blockdev, -device, and -object. Suggested-by: Markus Armbruster Signed-off-by: Laurent Vivier Reviewed-by: Markus Armbruster Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin --- include/net/net.h | 2 +- net/net.c | 20 +++++++------------- softmmu/vl.c | 2 +- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/include/net/net.h b/include/net/net.h index 81d0b21defce..c1c34a58f849 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -222,7 +222,7 @@ extern const char *host_net_devices[]; /* from net.c */ int net_client_parse(QemuOptsList *opts_list, const char *str); void show_netdevs(void); -int net_init_clients(Error **errp); +void net_init_clients(void); void net_check_clients(void); void net_cleanup(void); void hmp_host_net_add(Monitor *mon, const QDict *qdict); diff --git a/net/net.c b/net/net.c index d2288bd3a929..15958f881776 100644 --- a/net/net.c +++ b/net/net.c @@ -1562,27 +1562,21 @@ out: return ret; } =20 -int net_init_clients(Error **errp) +void net_init_clients(void) { net_change_state_entry =3D qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL= ); =20 QTAILQ_INIT(&net_clients); =20 - if (qemu_opts_foreach(qemu_find_opts("netdev"), - net_init_netdev, NULL, errp)) { - return -1; - } - - if (qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, errp= )) { - return -1; - } + qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, + &error_fatal); =20 - if (qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, er= rp)) { - return -1; - } + qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL, + &error_fatal); =20 - return 0; + qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL, + &error_fatal); } =20 int net_client_parse(QemuOptsList *opts_list, const char *optarg) diff --git a/softmmu/vl.c b/softmmu/vl.c index b464da25bcde..a4ae131e4d61 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -1904,7 +1904,7 @@ static void qemu_create_late_backends(void) qtest_server_init(qtest_chrdev, qtest_log, &error_fatal); } =20 - net_init_clients(&error_fatal); + net_init_clients(); =20 object_option_foreach_add(object_create_late); =20 --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666345442913304.6095564144863; Fri, 21 Oct 2022 02:44:02 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloZG-00025q-9W for importer@patchew.org; Fri, 21 Oct 2022 05:43:56 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2f-0003vA-4w for importer@patchew.org; Fri, 21 Oct 2022 05:10:13 -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 1olo2R-0003UV-9u for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:59 -0400 Received: from mout.kundenserver.de ([217.72.192.73]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2B-0001QQ-15 for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:58 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MRSdf-1oR8M03m6v-00NRsT; Fri, 21 Oct 2022 11:09:30 +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 03/17] net: simplify net_client_parse() error management Date: Fri, 21 Oct 2022 11:09:08 +0200 Message-Id: <20221021090922.170074-4-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:IKic31ZwZSDUlRtZ5W/oJ6x9m74VEHK+o7kLTExWX9a8SaM3uhN GkxTsbGIwSTcbmCyxldyFEjYzzTi+ti+OLJC5OlyLSrsUrjeq2luwqMC1eP4Bv4cz83XnBT NkQ18fCd+4tAi+RuO3gYgMhwspdKX2WDeTk7ejR+8xaH1WMilCQlR25sTfFBVT+q4slUoor Xutz9Omr+F0o07fJRStMA== X-UI-Out-Filterresults: notjunk:1;V03:K0:ug7mRa7M/zM=:/oMTRGSRN5urzOJnTQ1pG5 TOn9OFGoETM9Vlcx3K86rB+Tx9Qvcprrlou0F4jCyt4jZts1jg2rGWigl8+4YJWYLliXMZCKN gKeSSLYij1XRV89VSfbHOuSiRQZsu9H2Iz6HVdyCSHZCup6ZKGOS5P16FUrAGzf+ZnB21eAQL 8tqLs5tHwHdQRzHo7cDiVcbEB/Ylrp6srETEzKXFvoIuTSfPzYz/0skDk+72Yk/KyA6ykQv5E VdUGPLTdlREWXp1ybkVL/oyBEu+usG2vasOs+LcehkhEbvQsLtoDhLTTm1O5W3x5OXU9dlLH3 gAL8iuUMZex3N6MtdZ24H/CtmrwL7o1epWE2GciVamYdiFXTmrrouypREshx6DtAyv3DxiX3B G4I84HArw7xcEngyJfLAMXHSx5QSeTO57XBnQYMlbOTstkPSdlpppDVjXczpiElk3ab06+X6y WhfvQOAW36O7JH6K+3YKYe1NeUP9Oin22ViTpe/KaUdevtiQxLHL65ZoWeN1KfwGBbuL97qiu Or7rV2c72M33MqVujY5T3AzOJ7dqVZbcNbEyj6TSP/5MeNr4jq+w6vle5NgHX8Yv894wYexyc 9j928T1XeeZOsXt0nE1+lt5MbiQD8KtEu/VWRdjQ08Qo+BsXmYeOa70fv/30OJeeVN5dZ9smN 3RHpt8ZUGy7XR0J3nd4KZ2pHh9f4NF3r0bQOk7A1Gm1E7f6sI+s4zDF+2ziOxgnItANLCbfw8 gLGvM+sceoBMdXekPT6KXFfqHN1p9J+WrbJXylRe0llWLVB1DQPFyeSPiZAjbeANgcWwe5KiX TliuWE2 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.73; 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, 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: 1666345443557100001 Content-Type: text/plain; charset="utf-8" All net_client_parse() callers exit in case of error. Move exit(1) to net_client_parse() and remove error checking from the callers. Suggested-by: Markus Armbruster Signed-off-by: Laurent Vivier Reviewed-by: Markus Armbruster Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin --- include/net/net.h | 2 +- net/net.c | 6 ++---- softmmu/vl.c | 12 +++--------- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/include/net/net.h b/include/net/net.h index c1c34a58f849..55023e7e9fa9 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -220,7 +220,7 @@ extern NICInfo nd_table[MAX_NICS]; extern const char *host_net_devices[]; =20 /* from net.c */ -int net_client_parse(QemuOptsList *opts_list, const char *str); +void net_client_parse(QemuOptsList *opts_list, const char *str); void show_netdevs(void); void net_init_clients(void); void net_check_clients(void); diff --git a/net/net.c b/net/net.c index 15958f881776..f056e8aebfb2 100644 --- a/net/net.c +++ b/net/net.c @@ -1579,13 +1579,11 @@ void net_init_clients(void) &error_fatal); } =20 -int net_client_parse(QemuOptsList *opts_list, const char *optarg) +void net_client_parse(QemuOptsList *opts_list, const char *optarg) { if (!qemu_opts_parse_noisily(opts_list, optarg, true)) { - return -1; + exit(1); } - - return 0; } =20 /* From FreeBSD */ diff --git a/softmmu/vl.c b/softmmu/vl.c index a4ae131e4d61..e69aa43de469 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -2801,21 +2801,15 @@ void qemu_init(int argc, char **argv) break; case QEMU_OPTION_netdev: default_net =3D 0; - if (net_client_parse(qemu_find_opts("netdev"), optarg) =3D= =3D -1) { - exit(1); - } + net_client_parse(qemu_find_opts("netdev"), optarg); break; case QEMU_OPTION_nic: default_net =3D 0; - if (net_client_parse(qemu_find_opts("nic"), optarg) =3D=3D= -1) { - exit(1); - } + net_client_parse(qemu_find_opts("nic"), optarg); break; case QEMU_OPTION_net: default_net =3D 0; - if (net_client_parse(qemu_find_opts("net"), optarg) =3D=3D= -1) { - exit(1); - } + net_client_parse(qemu_find_opts("net"), optarg); break; #ifdef CONFIG_LIBISCSI case QEMU_OPTION_iscsi: --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666345483425608.9545542788304; Fri, 21 Oct 2022 02:44:43 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloa1-0004jV-1E for importer@patchew.org; Fri, 21 Oct 2022 05:44:41 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2p-00052n-O0 for importer@patchew.org; Fri, 21 Oct 2022 05:10:23 -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 1olo2R-0003UW-AD for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:59 -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 1olo2A-0001QL-K8 for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:58 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MZCOl-1oYnKk0xZB-00VB6P; Fri, 21 Oct 2022 11:09:31 +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 04/17] qapi: net: introduce a way to bypass qemu_opts_parse_noisily() Date: Fri, 21 Oct 2022 11:09:09 +0200 Message-Id: <20221021090922.170074-5-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:zbG1JYCFOKqctxtEiVdg+gddOmVeMfSH+Cb/QzJlwc7bIvDVcvo B81fAqeBKxRhjvrzjUzb1lB4siNjb+IX0+t/6QBQuexzGjpuSLRIncmd3vTCbJt4N15uiOs 5RiWC7dGi06ZBg6LX8jXbhZNWpF7OOUhGAEKpeOe/Ad5oJRggNcx4SOh3onniF213lr7ZIX 1A8bm8wEpT4O55KutZL/g== X-UI-Out-Filterresults: notjunk:1;V03:K0:6I6MKr2rA3E=:8iZAY7kw5shZUVvyNwuXXM zv3SyJvx9Mx8olYxmrJ427vyu3FHkqeRLM50HNsobbjYlzJ9C3TnHnAU7wazy50u7if9YtB2o usbeotygGXA0Dbh/LLQKsBpUEB71kOeuLzi74lbrybwY74DFn0BsmJrSCO768z79I8wRs51Xg ZE2fueJm/KQx/JPRjjOQqo0kTHnJloK+ILgeTnSSagWuXqeA0d1Bb9Eva1BXfL9cIa6VFH7jQ WlW/Ry0VIF/ANSPLvGRcEp9mnqKA6JPtg3+m8IhRbWvLwG1OSJVz4jAXbbvLLHP4mAnN92Y9s 0VmuPXb+IoeVkpRduZE1ykjFXT6c4ZsfL6Up2UCUpuamIDa0L6xNpIwvYh+rHrKaZlFb9e03F qPeKg4rDwSVuGfD+ZPXXy2BEtftjm4shJ4E5f2l3ognuipMD2nSVuym6d0kv1mQDdD2n+Qo61 700ggZK+fOtb4qxw6BOSUdqblOUpNTa4XA5xanepW5iuAcwSCUGRe9jSZ4GfxGbQJrtPxTLlX gSmO37gQRZoo+jUWJnSrwHlX0ztk9chUrN0bDF2dF/rKM9gw7VJNI2U3HH1tZy0waa145wuq1 KjO7g847umQY+H5jdw7Z0yX88yvFM3CnDP5wviyJPMo1DaReDyBC2rIcLAUhMjgykLBouWx0l M+JaMjarGd8t+fdykKzQicYt5MJqSGXlQVuMP3WzSAB7llUNavQ8E/MAnsTYyqLzN/X4MBN38 rzeD5dzjn359r8uUF3G2CmHl5vRyoYo+cG7HDdzBB+35Om7GyN+/ai0DTuzecP4Spv6zhkuTY EaHmpmS 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, 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: 1666345483686100003 Content-Type: text/plain; charset="utf-8" As qemu_opts_parse_noisily() flattens the QAPI structures ("type" field of Netdev structure can collides with "type" field of SocketAddress), we introduce a way to bypass qemu_opts_parse_noisily() and use directly visit_type_Netdev() to parse the backend parameters. More details from Markus: qemu_init() passes the argument of -netdev, -nic, and -net to net_client_parse(). net_client_parse() parses with qemu_opts_parse_noisily(), passing QemuOptsList qemu_netdev_opts for -netdev, qemu_nic_opts for -nic, and qemu_net_opts for -net. Their desc[] are all empty, which means any keys are accepted. The result of the parse (a QemuOpts) is stored in the QemuOptsList. Note that QemuOpts is flat by design. In some places, we layer non-flat on top using dotted keys convention, but not here. net_init_clients() iterates over the stored QemuOpts, and passes them to net_init_netdev(), net_param_nic(), or net_init_client(), respectively. These functions pass the QemuOpts to net_client_init(). They also do other things with the QemuOpts, which we can ignore here. net_client_init() uses the opts visitor to convert the (flat) QemOpts to a (non-flat) QAPI object Netdev. Netdev is also the argument of QMP command netdev_add. The opts visitor was an early attempt to support QAPI in (QemuOpts-based) CLI. It restricts QAPI types to a certain shape; see commit eb7ee2cbeb "qapi: introduce OptsVisitor". A more modern way to support QAPI is qobject_input_visitor_new_str(). It uses keyval_parse() instead of QemuOpts for KEY=3DVALUE,... syntax, and it also supports JSON syntax. The former isn't quite as expressive as JSON, but it's a lot closer than QemuOpts + opts visitor. This commit paves the way to use of the modern way instead. Signed-off-by: Laurent Vivier Reviewed-by: Markus Armbruster Acked-by: Michael S. Tsirkin --- include/net/net.h | 2 ++ net/net.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++ softmmu/vl.c | 6 ++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/include/net/net.h b/include/net/net.h index 55023e7e9fa9..025dbf1e143b 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -220,6 +220,8 @@ extern NICInfo nd_table[MAX_NICS]; extern const char *host_net_devices[]; =20 /* from net.c */ +bool netdev_is_modern(const char *optarg); +void netdev_parse_modern(const char *optarg); void net_client_parse(QemuOptsList *opts_list, const char *str); void show_netdevs(void); void net_init_clients(void); diff --git a/net/net.c b/net/net.c index f056e8aebfb2..ffe3e5a2cf1d 100644 --- a/net/net.c +++ b/net/net.c @@ -54,6 +54,7 @@ #include "net/colo-compare.h" #include "net/filter.h" #include "qapi/string-output-visitor.h" +#include "qapi/qobject-input-visitor.h" =20 /* Net bridge is currently not supported for W32. */ #if !defined(_WIN32) @@ -63,6 +64,16 @@ static VMChangeStateEntry *net_change_state_entry; static QTAILQ_HEAD(, NetClientState) net_clients; =20 +typedef struct NetdevQueueEntry { + Netdev *nd; + Location loc; + QSIMPLEQ_ENTRY(NetdevQueueEntry) entry; +} NetdevQueueEntry; + +typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue; + +static NetdevQueue nd_queue =3D QSIMPLEQ_HEAD_INITIALIZER(nd_queue); + /***********************************************************/ /* network device redirectors */ =20 @@ -1562,6 +1573,20 @@ out: return ret; } =20 +static void netdev_init_modern(void) +{ + while (!QSIMPLEQ_EMPTY(&nd_queue)) { + NetdevQueueEntry *nd =3D QSIMPLEQ_FIRST(&nd_queue); + + QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry); + loc_push_restore(&nd->loc); + net_client_init1(nd->nd, true, &error_fatal); + loc_pop(&nd->loc); + qapi_free_Netdev(nd->nd); + g_free(nd); + } +} + void net_init_clients(void) { net_change_state_entry =3D @@ -1569,6 +1594,8 @@ void net_init_clients(void) =20 QTAILQ_INIT(&net_clients); =20 + netdev_init_modern(); + qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, &error_fatal); =20 @@ -1579,6 +1606,36 @@ void net_init_clients(void) &error_fatal); } =20 +/* + * Does this -netdev argument use modern rather than traditional syntax? + * Modern syntax is to be parsed with netdev_parse_modern(). + * Traditional syntax is to be parsed with net_client_parse(). + */ +bool netdev_is_modern(const char *optarg) +{ + return false; +} + +/* + * netdev_parse_modern() uses modern, more expressive syntax than + * net_client_parse(), but supports only the -netdev option. + * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse() + * appends to @qemu_netdev_opts. + */ +void netdev_parse_modern(const char *optarg) +{ + Visitor *v; + NetdevQueueEntry *nd; + + v =3D qobject_input_visitor_new_str(optarg, "type", &error_fatal); + nd =3D g_new(NetdevQueueEntry, 1); + visit_type_Netdev(v, NULL, &nd->nd, &error_fatal); + visit_free(v); + loc_save(&nd->loc); + + QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry); +} + void net_client_parse(QemuOptsList *opts_list, const char *optarg) { if (!qemu_opts_parse_noisily(opts_list, optarg, true)) { diff --git a/softmmu/vl.c b/softmmu/vl.c index e69aa43de469..99fb49c7b0ee 100644 --- a/softmmu/vl.c +++ b/softmmu/vl.c @@ -2801,7 +2801,11 @@ void qemu_init(int argc, char **argv) break; case QEMU_OPTION_netdev: default_net =3D 0; - net_client_parse(qemu_find_opts("netdev"), optarg); + if (netdev_is_modern(optarg)) { + netdev_parse_modern(optarg); + } else { + net_client_parse(qemu_find_opts("netdev"), optarg); + } break; case QEMU_OPTION_nic: default_net =3D 0; --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666346516932829.3729066999718; Fri, 21 Oct 2022 03:01:56 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloqf-0000Uq-NE for importer@patchew.org; Fri, 21 Oct 2022 06:01:53 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo3I-0005ja-QX for importer@patchew.org; Fri, 21 Oct 2022 05:10:52 -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 1olo2M-0003O5-4G for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:54 -0400 Received: from mout.kundenserver.de ([212.227.17.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2I-0001de-46 for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:53 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1Mk178-1pW79A3UPw-00kOfw; Fri, 21 Oct 2022 11:09:33 +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?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v14 05/17] net: introduce qemu_set_info_str() function Date: Fri, 21 Oct 2022 11:09:10 +0200 Message-Id: <20221021090922.170074-6-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:HV5arl1n+1db0QtPAJHmemsxaG/kvjCaYzpUsg/H8krvhcQnrVh Xcl7Ti71o5RHMTKMCPOwRg+okTqAutBRx50d6kgYv6ulWQYLTw9UdU1VGG9+774pvIEVsPZ gCaW3Coz2CaxnBXnmNGQCdtNm/Ci/pQcaifQg0br0nJ3Mo8JaeHVUDO2HJY0+plJMN91n2u rYY65OmS8TJfeXNztpecw== X-UI-Out-Filterresults: notjunk:1;V03:K0:eL9GGFc6iOk=:IgtKfPM4aRXnHN3VR0Xiyt Dge1Q2RPz7wJe5f6QIucAYjicE6UnPEhPg0yQuGHUfeGybCqZ4ffkLZWmfH3U9NRACxJxkbFq 6HYSU6waqcl9nuX+I5jCOO8YtiAY6npy/XjEBCmORdvoepdxM3niUNGVY9xQLU9y8j17FcHBF +gXqyIT2Vm1DW+U+0Vobyu20rap/HQ3OAP5XrF+Voyxo6XNgRJpAN4sYMcReQOMaatEyNCvjo VN3XJMGxRKP0e2FE8wQq29vIlDX0+SJKCDlBr6gbxOPta7VVrcCXj8QlF/0IJ34ZiwE6jWRmj xicVACbvErHiQMj2F8bYdghP+FlV0t2NUV3y+FpRmORwPCrIXhnBmUR/6J9jyMY/yZTYccGVg lI0vzA65lgR77Jft0eHOlcVRfyKxCyIH1iWoIUDRKh/+kkImLfaObEGwmNxcFNBfH3rTQQi35 rGljpwkaVP7nj5Y9/WgPNimSrILPKYmUDDstTZZ0fr+E+sTrL28tKDFeGbmJT+Lmdf38Z3kVK 7P3C2RlTdkYfhuebV/U5nGk6aelex4XZL6p7AMTx8Bpyh9R+MH4oyP1YzBlefN283G62V3K82 ntd/NSDH2TZlX1CKtaz5PcIvfo1Ch+BMWUCDiBxrBXig3LOw3RCls8R9908g5nZfn+OOea6pF pGwkTbVRkUozsn9B32Tp8VG90YTYeEcSapppicZfa/8vQYqdZygeadbRj12l8O0CGfdqM0vzr +Qu1g76WJMPaugyl3m7DFQDM5k2pcttUEbXetigfeOjhgP8AHSFbUknpSU0Z52eQ+28fDkSq1 MITDvpc 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=212.227.17.10; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666346518416100001 Embed the setting of info_str in a function. Signed-off-by: Laurent Vivier Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/net/xen_nic.c | 5 ++--- include/net/net.h | 1 + net/l2tpv3.c | 3 +-- net/net.c | 17 ++++++++++++----- net/slirp.c | 5 ++--- net/socket.c | 33 ++++++++++++++------------------- net/tap-win32.c | 3 +-- net/tap.c | 13 +++++-------- net/vde.c | 3 +-- net/vhost-user.c | 3 +-- net/vhost-vdpa.c | 2 +- 11 files changed, 41 insertions(+), 47 deletions(-) diff --git a/hw/net/xen_nic.c b/hw/net/xen_nic.c index 5c815b4f0c52..7d92c2d022b0 100644 --- a/hw/net/xen_nic.c +++ b/hw/net/xen_nic.c @@ -296,9 +296,8 @@ static int net_init(struct XenLegacyDevice *xendev) netdev->nic =3D qemu_new_nic(&net_xen_info, &netdev->conf, "xen", NULL, netdev); =20 - snprintf(qemu_get_queue(netdev->nic)->info_str, - sizeof(qemu_get_queue(netdev->nic)->info_str), - "nic: xenbus vif macaddr=3D%s", netdev->mac); + qemu_set_info_str(qemu_get_queue(netdev->nic), + "nic: xenbus vif macaddr=3D%s", netdev->mac); =20 /* fill info */ xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1); diff --git a/include/net/net.h b/include/net/net.h index 025dbf1e143b..3db75ff841ff 100644 --- a/include/net/net.h +++ b/include/net/net.h @@ -177,6 +177,7 @@ ssize_t qemu_send_packet_async(NetClientState *nc, cons= t uint8_t *buf, void qemu_purge_queued_packets(NetClientState *nc); void qemu_flush_queued_packets(NetClientState *nc); void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge); +void qemu_set_info_str(NetClientState *nc, const char *fmt, ...); void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]); bool qemu_has_ufo(NetClientState *nc); bool qemu_has_vnet_hdr(NetClientState *nc); diff --git a/net/l2tpv3.c b/net/l2tpv3.c index af373e5c300c..350041a0d6c0 100644 --- a/net/l2tpv3.c +++ b/net/l2tpv3.c @@ -723,8 +723,7 @@ int net_init_l2tpv3(const Netdev *netdev, =20 l2tpv3_read_poll(s, true); =20 - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "l2tpv3: connected"); + qemu_set_info_str(&s->nc, "l2tpv3: connected"); return 0; outerr: qemu_del_net_client(nc); diff --git a/net/net.c b/net/net.c index ffe3e5a2cf1d..41e05137d431 100644 --- a/net/net.c +++ b/net/net.c @@ -141,13 +141,20 @@ char *qemu_mac_strdup_printf(const uint8_t *macaddr) macaddr[3], macaddr[4], macaddr[5]); } =20 +void qemu_set_info_str(NetClientState *nc, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vsnprintf(nc->info_str, sizeof(nc->info_str), fmt, ap); + va_end(ap); +} + void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6]) { - snprintf(nc->info_str, sizeof(nc->info_str), - "model=3D%s,macaddr=3D%02x:%02x:%02x:%02x:%02x:%02x", - nc->model, - macaddr[0], macaddr[1], macaddr[2], - macaddr[3], macaddr[4], macaddr[5]); + qemu_set_info_str(nc, "model=3D%s,macaddr=3D%02x:%02x:%02x:%02x:%02x:%= 02x", + nc->model, macaddr[0], macaddr[1], macaddr[2], + macaddr[3], macaddr[4], macaddr[5]); } =20 static int mac_table[256] =3D {0}; diff --git a/net/slirp.c b/net/slirp.c index 8679be644420..14a8d592774c 100644 --- a/net/slirp.c +++ b/net/slirp.c @@ -611,9 +611,8 @@ static int net_slirp_init(NetClientState *peer, const c= har *model, =20 nc =3D qemu_new_net_client(&net_slirp_info, peer, model, name); =20 - snprintf(nc->info_str, sizeof(nc->info_str), - "net=3D%s,restrict=3D%s", inet_ntoa(net), - restricted ? "on" : "off"); + qemu_set_info_str(nc, "net=3D%s,restrict=3D%s", inet_ntoa(net), + restricted ? "on" : "off"); =20 s =3D DO_UPCAST(SlirpState, nc, nc); =20 diff --git a/net/socket.c b/net/socket.c index bfd8596250c4..ade1ecf38b87 100644 --- a/net/socket.c +++ b/net/socket.c @@ -179,7 +179,7 @@ static void net_socket_send(void *opaque) s->fd =3D -1; net_socket_rs_init(&s->rs, net_socket_rs_finalize, false); s->nc.link_down =3D true; - memset(s->nc.info_str, 0, sizeof(s->nc.info_str)); + qemu_set_info_str(&s->nc, ""); =20 return; } @@ -387,16 +387,15 @@ static NetSocketState *net_socket_fd_init_dgram(NetCl= ientState *peer, /* mcast: save bound address as dst */ if (is_connected && mcast !=3D NULL) { s->dgram_dst =3D saddr; - snprintf(nc->info_str, sizeof(nc->info_str), - "socket: fd=3D%d (cloned mcast=3D%s:%d)", - fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + qemu_set_info_str(nc, "socket: fd=3D%d (cloned mcast=3D%s:%d)", fd, + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)= ); } else { if (sa_type =3D=3D SOCKET_ADDRESS_TYPE_UNIX) { s->dgram_dst.sin_family =3D AF_UNIX; } =20 - snprintf(nc->info_str, sizeof(nc->info_str), - "socket: fd=3D%d %s", fd, SocketAddressType_str(sa_type)); + qemu_set_info_str(nc, "socket: fd=3D%d %s", fd, + SocketAddressType_str(sa_type)); } =20 return s; @@ -430,7 +429,7 @@ static NetSocketState *net_socket_fd_init_stream(NetCli= entState *peer, =20 nc =3D qemu_new_net_client(&net_socket_info, peer, model, name); =20 - snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=3D%d", fd); + qemu_set_info_str(nc, "socket: fd=3D%d", fd); =20 s =3D DO_UPCAST(NetSocketState, nc, nc); =20 @@ -497,9 +496,8 @@ static void net_socket_accept(void *opaque) s->fd =3D fd; s->nc.link_down =3D false; net_socket_connect(s); - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "socket: connection from %s:%d", - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + qemu_set_info_str(&s->nc, "socket: connection from %s:%d", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); } =20 static int net_socket_listen_init(NetClientState *peer, @@ -597,9 +595,8 @@ static int net_socket_connect_init(NetClientState *peer, return -1; } =20 - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "socket: connect to %s:%d", - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + qemu_set_info_str(&s->nc, "socket: connect to %s:%d", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); return 0; } =20 @@ -642,9 +639,8 @@ static int net_socket_mcast_init(NetClientState *peer, =20 s->dgram_dst =3D saddr; =20 - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "socket: mcast=3D%s:%d", - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); + qemu_set_info_str(&s->nc, "socket: mcast=3D%s:%d", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); return 0; =20 } @@ -697,9 +693,8 @@ static int net_socket_udp_init(NetClientState *peer, =20 s->dgram_dst =3D raddr; =20 - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "socket: udp=3D%s:%d", - inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); + qemu_set_info_str(&s->nc, "socket: udp=3D%s:%d", inet_ntoa(raddr.sin_a= ddr), + ntohs(raddr.sin_port)); return 0; } =20 diff --git a/net/tap-win32.c b/net/tap-win32.c index 7466f22e77a4..a49c28ba5dc5 100644 --- a/net/tap-win32.c +++ b/net/tap-win32.c @@ -789,8 +789,7 @@ static int tap_win32_init(NetClientState *peer, const c= har *model, =20 s =3D DO_UPCAST(TAPState, nc, nc); =20 - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "tap: ifname=3D%s", ifname); + qemu_set_info_str(&s->nc, "tap: ifname=3D%s", ifname); =20 s->handle =3D handle; =20 diff --git a/net/tap.c b/net/tap.c index e203d07a1216..1210a0436de8 100644 --- a/net/tap.c +++ b/net/tap.c @@ -630,8 +630,7 @@ int net_init_bridge(const Netdev *netdev, const char *n= ame, } s =3D net_tap_fd_init(peer, "bridge", name, fd, vnet_hdr); =20 - snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=3D%s,br=3D%s"= , helper, - br); + qemu_set_info_str(&s->nc, "helper=3D%s,br=3D%s", helper, br); =20 return 0; } @@ -690,14 +689,12 @@ static void net_init_tap_one(const NetdevTapOptions *= tap, NetClientState *peer, } =20 if (tap->has_fd || tap->has_fds) { - snprintf(s->nc.info_str, sizeof(s->nc.info_str), "fd=3D%d", fd); + qemu_set_info_str(&s->nc, "fd=3D%d", fd); } else if (tap->has_helper) { - snprintf(s->nc.info_str, sizeof(s->nc.info_str), "helper=3D%s", - tap->helper); + qemu_set_info_str(&s->nc, "helper=3D%s", tap->helper); } else { - snprintf(s->nc.info_str, sizeof(s->nc.info_str), - "ifname=3D%s,script=3D%s,downscript=3D%s", ifname, script, - downscript); + qemu_set_info_str(&s->nc, "ifname=3D%s,script=3D%s,downscript=3D%s= ", ifname, + script, downscript); =20 if (strcmp(downscript, "no") !=3D 0) { snprintf(s->down_script, sizeof(s->down_script), "%s", downscr= ipt); diff --git a/net/vde.c b/net/vde.c index 1083916bcf52..c0a08662cc30 100644 --- a/net/vde.c +++ b/net/vde.c @@ -98,8 +98,7 @@ static int net_vde_init(NetClientState *peer, const char = *model, =20 nc =3D qemu_new_net_client(&net_vde_info, peer, model, name); =20 - snprintf(nc->info_str, sizeof(nc->info_str), "sock=3D%s,fd=3D%d", - sock, vde_datafd(vde)); + qemu_set_info_str(nc, "sock=3D%s,fd=3D%d", sock, vde_datafd(vde)); =20 s =3D DO_UPCAST(VDEState, nc, nc); =20 diff --git a/net/vhost-user.c b/net/vhost-user.c index b1a0247b5981..3a6b90da8661 100644 --- a/net/vhost-user.c +++ b/net/vhost-user.c @@ -341,8 +341,7 @@ static int net_vhost_user_init(NetClientState *peer, co= nst char *device, user =3D g_new0(struct VhostUserState, 1); for (i =3D 0; i < queues; i++) { nc =3D qemu_new_net_client(&net_vhost_user_info, peer, device, nam= e); - snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s", - i, chr->label); + qemu_set_info_str(nc, "vhost-user%d to %s", i, chr->label); nc->queue_index =3D i; if (!nc0) { nc0 =3D nc; diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c index 4bc3fd01a878..e1865bea6aa3 100644 --- a/net/vhost-vdpa.c +++ b/net/vhost-vdpa.c @@ -593,7 +593,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientSta= te *peer, nc =3D qemu_new_net_control_client(&net_vhost_vdpa_cvq_info, peer, device, name); } - snprintf(nc->info_str, sizeof(nc->info_str), TYPE_VHOST_VDPA); + qemu_set_info_str(nc, TYPE_VHOST_VDPA); s =3D DO_UPCAST(VhostVDPAState, nc, nc); =20 s->vhost_vdpa.device_fd =3D vdpa_device_fd; --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 166634470976394.94695261682307; Fri, 21 Oct 2022 02:31:49 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloNW-0001M2-Qe for importer@patchew.org; Fri, 21 Oct 2022 05:31:46 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2q-00053H-Qt 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 1olo2M-0003O6-4B for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:54 -0400 Received: from mout.kundenserver.de ([212.227.17.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2F-0001Zq-Rp for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:52 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1N7hrw-1p7J8e1Zcw-014mpu; Fri, 21 Oct 2022 11:09:35 +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?= , Stefano Brivio Subject: [PATCH v14 06/17] qapi: net: add stream and dgram netdevs Date: Fri, 21 Oct 2022 11:09:11 +0200 Message-Id: <20221021090922.170074-7-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:4HM3F/3qzZvU4v2oE57bkyf1Ov3n5xl8+MWMURMKEagxbzyWo9r RweJYrAoE9+LyzCG7Y6VCnml1euJC4WMyy/dYkfLWDPgFQy9EnVUqoLDy/zjaTN4y0Sfayr EThQa/du+kj+wXtY8u7IITwPYLfg/UrEkb5wOgblHtktdquGgKaZyfkUq8hm9Hh5MVUN+Ws fk6M27cwr9R55Uyxv8TLQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:XLY2hcw5K7A=:7P8u81qKbyMM1kKhnOe06O 7/I2ry1mX0uToaxLEJ9JsZsKCYUTKNbxtiCaAmuhwtbhtRgzoEjJnYxuMtnvIk1COjkXeeIAW X8XQGwq7ZJh0JaE7plwu10jEJZ0UDS8wBzXYxS6hK5FHWnD+DYit/B3OyRK4MqgoRDH4BtFNL EdbaBOtQJasZpRMW9uQqZDrc5hH0RPoYKE5jNGVFOg/VpYiP8GMpxMVdBX9+sOOqLpIs6kgSc Aw8War0c8JJRoS/tI+MsEEGQqBrsfMSvfiR5t34hBOQHW8fhugFBmovPHSKGNoahVeShtClUj WsdMXo3vkc9AhF+nTbxNorDUHRm4NzvCI/TZFgxeR419sP90hRh4vsbHcBHVE1ZFL0gledTl7 zNmFtZJahsNfcS927wTxz+RRdhktJFR4NjAXdZerRXRHl+TTE7Fm+hpvJO9lBsC07ewRbXmhx 61g9GbAwBzjSPK/Z4iZdSS2WuhSPKvVJrl2U8EltTe+4M0jiywz+V7IQhcnTqg/lUKt6avbcK 9eS7DvwYfZVficTucFzJZUG5sFTKAkz0NtcG5T4JeUIi6iG8kwThLG280WE7XJBe/0YbynurG HaR0pRNFklMRV8PxVh5BaiZyHfzFL4RGPJ6mXZuavGG3Jq6K9Rehw2EbZdm7DCwWicn6PE9xQ 3Ye00YAWxdfzPXC4bKaRxhIiczwTi0hOtEWXO1P/1pofUQQzUIlzwKm+u5EGc9LEkwwIS2Vrm 12GuKEtjnnvNrRT1BGmZOropZoxxEyW1+VuiD/seG6R8XakAHhFZ+UqoAHdM5UkvPWX2nkyt5 0ecufW7 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=212.227.17.10; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666344711862100003 Content-Type: text/plain; charset="utf-8" Copied from socket netdev file and modified to use SocketAddress to be able to introduce new features like unix socket. "udp" and "mcast" are squashed into dgram netdev, multicast is detected according to the IP address type. "listen" and "connect" modes are managed by stream netdev. An optional parameter "server" defines the mode (off by default) The two new types need to be parsed the modern way with -netdev, because with the traditional way, the "type" field of netdev structure collides with the "type" field of SocketAddress and prevents the correct evaluation of the command line option. Moreover the traditional way doesn't allow to use the same type (SocketAddress) several times with the -netdev option (needed to specify "local" and "remote" addresses). The previous commit paved the way for parsing the modern way, but omitted one detail: how to pick modern vs. traditional, in netdev_is_modern(). We want to pick based on the value of parameter "type". But how to extract it from the option argument? Parsing the option argument, either the modern or the traditional way, extracts it for us, but only if parsing succeeds. If parsing fails, there is no good option. No matter which parser we pick, it'll be the wrong one for some arguments, and the error reporting will be confusing. Fortunately, the traditional parser accepts *anything* when called in a certain way. This maximizes our chance to extract the value of "type", and in turn minimizes the risk of confusing error reporting. Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Acked-by: Markus Armbruster Acked-by: Michael S. Tsirkin --- hmp-commands.hx | 2 +- net/clients.h | 6 + net/dgram.c | 537 ++++++++++++++++++++++++++++++++++++++++++++++++ net/hub.c | 2 + net/meson.build | 2 + net/net.c | 30 ++- net/stream.c | 425 ++++++++++++++++++++++++++++++++++++++ qapi/net.json | 66 +++++- qemu-options.hx | 12 ++ 9 files changed, 1078 insertions(+), 4 deletions(-) create mode 100644 net/dgram.c create mode 100644 net/stream.c diff --git a/hmp-commands.hx b/hmp-commands.hx index 12b6d4e2dcac..673e39a69797 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1276,7 +1276,7 @@ ERST { .name =3D "netdev_add", .args_type =3D "netdev:O", - .params =3D "[user|tap|socket|vde|bridge|hubport|netmap|vhost-= user" + .params =3D "[user|tap|socket|stream|dgram|vde|bridge|hubport|= netmap|vhost-user" #ifdef CONFIG_VMNET "|vmnet-host|vmnet-shared|vmnet-bridged" #endif diff --git a/net/clients.h b/net/clients.h index c9157789f2ce..ed8bdfff1e7c 100644 --- a/net/clients.h +++ b/net/clients.h @@ -40,6 +40,12 @@ int net_init_hubport(const Netdev *netdev, const char *n= ame, int net_init_socket(const Netdev *netdev, const char *name, NetClientState *peer, Error **errp); =20 +int net_init_stream(const Netdev *netdev, const char *name, + NetClientState *peer, Error **errp); + +int net_init_dgram(const Netdev *netdev, const char *name, + NetClientState *peer, Error **errp); + int net_init_tap(const Netdev *netdev, const char *name, NetClientState *peer, Error **errp); =20 diff --git a/net/dgram.c b/net/dgram.c new file mode 100644 index 000000000000..5339585b8202 --- /dev/null +++ b/net/dgram.c @@ -0,0 +1,537 @@ +/* + * QEMU System Emulator + * + * Copyright (c) 2003-2008 Fabrice Bellard + * Copyright (c) 2022 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" + +#include "net/net.h" +#include "clients.h" +#include "monitor/monitor.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/option.h" +#include "qemu/sockets.h" +#include "qemu/iov.h" +#include "qemu/main-loop.h" +#include "qemu/cutils.h" + +typedef struct NetDgramState { + NetClientState nc; + int fd; + SocketReadState rs; + struct sockaddr_in dgram_dst; /* contains destination iff connectionle= ss */ + bool read_poll; /* waiting to receive data? */ + bool write_poll; /* waiting to transmit data? */ +} NetDgramState; + +static void net_dgram_send(void *opaque); +static void net_dgram_writable(void *opaque); + +static void net_dgram_update_fd_handler(NetDgramState *s) +{ + qemu_set_fd_handler(s->fd, + s->read_poll ? net_dgram_send : NULL, + s->write_poll ? net_dgram_writable : NULL, + s); +} + +static void net_dgram_read_poll(NetDgramState *s, bool enable) +{ + s->read_poll =3D enable; + net_dgram_update_fd_handler(s); +} + +static void net_dgram_write_poll(NetDgramState *s, bool enable) +{ + s->write_poll =3D enable; + net_dgram_update_fd_handler(s); +} + +static void net_dgram_writable(void *opaque) +{ + NetDgramState *s =3D opaque; + + net_dgram_write_poll(s, false); + + qemu_flush_queued_packets(&s->nc); +} + +static ssize_t net_dgram_receive(NetClientState *nc, + const uint8_t *buf, size_t size) +{ + NetDgramState *s =3D DO_UPCAST(NetDgramState, nc, nc); + ssize_t ret; + + do { + if (s->dgram_dst.sin_family !=3D AF_UNIX) { + ret =3D sendto(s->fd, buf, size, 0, + (struct sockaddr *)&s->dgram_dst, + sizeof(s->dgram_dst)); + } else { + ret =3D send(s->fd, buf, size, 0); + } + } while (ret =3D=3D -1 && errno =3D=3D EINTR); + + if (ret =3D=3D -1 && errno =3D=3D EAGAIN) { + net_dgram_write_poll(s, true); + return 0; + } + return ret; +} + +static void net_dgram_send_completed(NetClientState *nc, ssize_t len) +{ + NetDgramState *s =3D DO_UPCAST(NetDgramState, nc, nc); + + if (!s->read_poll) { + net_dgram_read_poll(s, true); + } +} + +static void net_dgram_rs_finalize(SocketReadState *rs) +{ + NetDgramState *s =3D container_of(rs, NetDgramState, rs); + + if (qemu_send_packet_async(&s->nc, rs->buf, + rs->packet_len, + net_dgram_send_completed) =3D=3D 0) { + net_dgram_read_poll(s, false); + } +} + +static void net_dgram_send(void *opaque) +{ + NetDgramState *s =3D opaque; + int size; + + size =3D recv(s->fd, s->rs.buf, sizeof(s->rs.buf), 0); + if (size < 0) { + return; + } + if (size =3D=3D 0) { + /* end of connection */ + net_dgram_read_poll(s, false); + net_dgram_write_poll(s, false); + return; + } + if (qemu_send_packet_async(&s->nc, s->rs.buf, size, + net_dgram_send_completed) =3D=3D 0) { + net_dgram_read_poll(s, false); + } +} + +static int net_dgram_mcast_create(struct sockaddr_in *mcastaddr, + struct in_addr *localaddr, + Error **errp) +{ + struct ip_mreq imr; + int fd; + int val, ret; +#ifdef __OpenBSD__ + unsigned char loop; +#else + int loop; +#endif + + if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { + error_setg(errp, "specified mcastaddr %s (0x%08x) " + "does not contain a multicast address", + inet_ntoa(mcastaddr->sin_addr), + (int)ntohl(mcastaddr->sin_addr.s_addr)); + return -1; + } + + fd =3D qemu_socket(PF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + error_setg_errno(errp, errno, "can't create datagram socket"); + return -1; + } + + /* + * Allow multiple sockets to bind the same multicast ip and port by se= tting + * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should = be set + * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSE= ADDR + * only on posix systems. + */ + val =3D 1; + ret =3D setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); + if (ret < 0) { + error_setg_errno(errp, errno, "can't set socket option SO_REUSEADD= R"); + goto fail; + } + + ret =3D bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); + if (ret < 0) { + error_setg_errno(errp, errno, "can't bind ip=3D%s to socket", + inet_ntoa(mcastaddr->sin_addr)); + goto fail; + } + + /* Add host to multicast group */ + imr.imr_multiaddr =3D mcastaddr->sin_addr; + if (localaddr) { + imr.imr_interface =3D *localaddr; + } else { + imr.imr_interface.s_addr =3D htonl(INADDR_ANY); + } + + ret =3D setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, + &imr, sizeof(struct ip_mreq)); + if (ret < 0) { + error_setg_errno(errp, errno, + "can't add socket to multicast group %s", + inet_ntoa(imr.imr_multiaddr)); + goto fail; + } + + /* Force mcast msgs to loopback (eg. several QEMUs in same host */ + loop =3D 1; + ret =3D setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, + &loop, sizeof(loop)); + if (ret < 0) { + error_setg_errno(errp, errno, + "can't force multicast message to loopback"); + goto fail; + } + + /* If a bind address is given, only send packets from that address */ + if (localaddr !=3D NULL) { + ret =3D setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, + localaddr, sizeof(*localaddr)); + if (ret < 0) { + error_setg_errno(errp, errno, + "can't set the default network send interface= "); + goto fail; + } + } + + qemu_socket_set_nonblock(fd); + return fd; +fail: + if (fd >=3D 0) { + closesocket(fd); + } + return -1; +} + +static void net_dgram_cleanup(NetClientState *nc) +{ + NetDgramState *s =3D DO_UPCAST(NetDgramState, nc, nc); + if (s->fd !=3D -1) { + net_dgram_read_poll(s, false); + net_dgram_write_poll(s, false); + close(s->fd); + s->fd =3D -1; + } +} + +static NetClientInfo net_dgram_socket_info =3D { + .type =3D NET_CLIENT_DRIVER_DGRAM, + .size =3D sizeof(NetDgramState), + .receive =3D net_dgram_receive, + .cleanup =3D net_dgram_cleanup, +}; + +static NetDgramState *net_dgram_fd_init(NetClientState *peer, + const char *model, + const char *name, + int fd, int is_fd, + SocketAddress *mcast, + Error **errp) +{ + struct sockaddr_in saddr; + int newfd; + NetClientState *nc; + NetDgramState *s; + SocketAddress *sa; + SocketAddressType sa_type; + + sa =3D socket_local_address(fd, errp); + if (!sa) { + return NULL; + } + sa_type =3D sa->type; + qapi_free_SocketAddress(sa); + + /* + * fd passed: multicast: "learn" dgram_dst address from bound address = and + * save it. Because this may be "shared" socket from a "master" proces= s, + * datagrams would be recv() by ONLY ONE process: we must "clone" this + * dgram socket --jjo + */ + + if (is_fd && mcast !=3D NULL) { + if (convert_host_port(&saddr, mcast->u.inet.host, + mcast->u.inet.port, errp) < 0) { + goto err; + } + /* must be bound */ + if (saddr.sin_addr.s_addr =3D=3D 0) { + error_setg(errp, "can't setup multicast destination addres= s"); + goto err; + } + /* clone dgram socket */ + newfd =3D net_dgram_mcast_create(&saddr, NULL, errp); + if (newfd < 0) { + goto err; + } + /* clone newfd to fd, close newfd */ + dup2(newfd, fd); + close(newfd); + + } + + nc =3D qemu_new_net_client(&net_dgram_socket_info, peer, model, name); + + s =3D DO_UPCAST(NetDgramState, nc, nc); + + s->fd =3D fd; + net_socket_rs_init(&s->rs, net_dgram_rs_finalize, false); + net_dgram_read_poll(s, true); + + /* mcast: save bound address as dst */ + if (is_fd && mcast !=3D NULL) { + s->dgram_dst =3D saddr; + qemu_set_info_str(nc, "fd=3D%d (cloned mcast=3D%s:%d)", fd, + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)= ); + } else { + if (sa_type =3D=3D SOCKET_ADDRESS_TYPE_UNIX) { + s->dgram_dst.sin_family =3D AF_UNIX; + } + + qemu_set_info_str(nc, "fd=3D%d %s", fd, SocketAddressType_str(sa_t= ype)); + } + + return s; + +err: + closesocket(fd); + return NULL; +} + +static int net_dgram_mcast_init(NetClientState *peer, + const char *model, + const char *name, + SocketAddress *remote, + SocketAddress *local, + Error **errp) +{ + NetDgramState *s; + int fd, ret; + struct sockaddr_in saddr; + + if (remote->type !=3D SOCKET_ADDRESS_TYPE_INET) { + error_setg(errp, "multicast only support inet type"); + return -1; + } + + if (convert_host_port(&saddr, remote->u.inet.host, remote->u.inet.port, + errp) < 0) { + return -1; + } + + if (!local) { + fd =3D net_dgram_mcast_create(&saddr, NULL, errp); + if (fd < 0) { + return -1; + } + } else { + switch (local->type) { + case SOCKET_ADDRESS_TYPE_INET: { + struct in_addr localaddr; + + if (inet_aton(local->u.inet.host, &localaddr) =3D=3D 0) { + error_setg(errp, "localaddr '%s' is not a valid IPv4 addre= ss", + local->u.inet.host); + return -1; + } + + fd =3D net_dgram_mcast_create(&saddr, &localaddr, errp); + if (fd < 0) { + return -1; + } + break; + } + case SOCKET_ADDRESS_TYPE_FD: + fd =3D monitor_fd_param(monitor_cur(), local->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 descripto= r %d", + name, fd); + return -1; + } + break; + default: + error_setg(errp, "only support inet or fd type for local"); + return -1; + } + } + + s =3D net_dgram_fd_init(peer, model, name, fd, + local->type =3D=3D SOCKET_ADDRESS_TYPE_FD, + remote, errp); + if (!s) { + return -1; + } + + s->dgram_dst =3D saddr; + + qemu_set_info_str(&s->nc, "mcast=3D%s:%d", inet_ntoa(saddr.sin_addr), + ntohs(saddr.sin_port)); + return 0; + +} + + +int net_init_dgram(const Netdev *netdev, const char *name, + NetClientState *peer, Error **errp) +{ + NetDgramState *s; + int fd, ret; + struct sockaddr_in raddr_in; + struct sockaddr_in laddr_in; + SocketAddress *remote, *local; + + assert(netdev->type =3D=3D NET_CLIENT_DRIVER_DGRAM); + + remote =3D netdev->u.dgram.remote; + local =3D netdev->u.dgram.local; + + /* detect multicast address */ + if (remote && remote->type =3D=3D SOCKET_ADDRESS_TYPE_INET) { + struct sockaddr_in mcastaddr; + + if (convert_host_port(&mcastaddr, remote->u.inet.host, + remote->u.inet.port, errp) < 0) { + return -1; + } + + if (IN_MULTICAST(ntohl(mcastaddr.sin_addr.s_addr))) { + return net_dgram_mcast_init(peer, "dram", name, remote, local, + errp); + } + } + + /* unicast address */ + if (!local) { + error_setg(errp, "dgram requires local=3D parameter"); + return -1; + } + + if (remote) { + if (local->type =3D=3D SOCKET_ADDRESS_TYPE_FD) { + error_setg(errp, "don't set remote with local.fd"); + return -1; + } + if (remote->type !=3D local->type) { + error_setg(errp, "remote and local types must be the same"); + return -1; + } + } else { + if (local->type !=3D SOCKET_ADDRESS_TYPE_FD) { + error_setg(errp, "type=3Dinet requires remote parameter"); + return -1; + } + } + + switch (local->type) { + case SOCKET_ADDRESS_TYPE_INET: + if (convert_host_port(&laddr_in, local->u.inet.host, local->u.inet= .port, + errp) < 0) { + return -1; + } + + if (convert_host_port(&raddr_in, remote->u.inet.host, + remote->u.inet.port, errp) < 0) { + return -1; + } + + fd =3D qemu_socket(PF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + error_setg_errno(errp, errno, "can't create datagram socket"); + return -1; + } + + ret =3D socket_set_fast_reuse(fd); + if (ret < 0) { + error_setg_errno(errp, errno, + "can't set socket option SO_REUSEADDR"); + closesocket(fd); + return -1; + } + ret =3D bind(fd, (struct sockaddr *)&laddr_in, sizeof(laddr_in)); + if (ret < 0) { + error_setg_errno(errp, errno, "can't bind ip=3D%s to socket", + inet_ntoa(laddr_in.sin_addr)); + closesocket(fd); + return -1; + } + qemu_socket_set_nonblock(fd); + break; + case SOCKET_ADDRESS_TYPE_FD: + fd =3D monitor_fd_param(monitor_cur(), local->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 for local"); + return -1; + } + + s =3D net_dgram_fd_init(peer, "dgram", name, fd, 0, NULL, errp); + if (!s) { + return -1; + } + + if (remote) { + s->dgram_dst =3D raddr_in; + } + + switch (local->type) { + case SOCKET_ADDRESS_TYPE_INET: + qemu_set_info_str(&s->nc, "udp=3D%s:%d/%s:%d", + inet_ntoa(laddr_in.sin_addr), + ntohs(laddr_in.sin_port), + inet_ntoa(raddr_in.sin_addr), + ntohs(raddr_in.sin_port)); + break; + case SOCKET_ADDRESS_TYPE_FD: + qemu_set_info_str(&s->nc, "fd=3D%d", fd); + break; + default: + g_assert_not_reached(); + } + + return 0; +} diff --git a/net/hub.c b/net/hub.c index 1375738bf121..67ca53485638 100644 --- a/net/hub.c +++ b/net/hub.c @@ -313,6 +313,8 @@ void net_hub_check_clients(void) case NET_CLIENT_DRIVER_USER: case NET_CLIENT_DRIVER_TAP: case NET_CLIENT_DRIVER_SOCKET: + case NET_CLIENT_DRIVER_STREAM: + case NET_CLIENT_DRIVER_DGRAM: case NET_CLIENT_DRIVER_VDE: case NET_CLIENT_DRIVER_VHOST_USER: has_host_dev =3D 1; diff --git a/net/meson.build b/net/meson.build index d1be76daf361..6cd1e3dab3a6 100644 --- a/net/meson.build +++ b/net/meson.build @@ -13,6 +13,8 @@ softmmu_ss.add(files( 'net.c', 'queue.c', 'socket.c', + 'stream.c', + 'dgram.c', 'util.c', )) =20 diff --git a/net/net.c b/net/net.c index 41e05137d431..dd063d71b7b3 100644 --- a/net/net.c +++ b/net/net.c @@ -48,6 +48,7 @@ #include "qemu/qemu-print.h" #include "qemu/main-loop.h" #include "qemu/option.h" +#include "qemu/keyval.h" #include "qapi/error.h" #include "qapi/opts-visitor.h" #include "sysemu/runstate.h" @@ -1021,6 +1022,8 @@ static int (* const net_client_init_fun[NET_CLIENT_DR= IVER__MAX])( #endif [NET_CLIENT_DRIVER_TAP] =3D net_init_tap, [NET_CLIENT_DRIVER_SOCKET] =3D net_init_socket, + [NET_CLIENT_DRIVER_STREAM] =3D net_init_stream, + [NET_CLIENT_DRIVER_DGRAM] =3D net_init_dgram, #ifdef CONFIG_VDE [NET_CLIENT_DRIVER_VDE] =3D net_init_vde, #endif @@ -1108,6 +1111,8 @@ void show_netdevs(void) int idx; const char *available_netdevs[] =3D { "socket", + "stream", + "dgram", "hubport", "tap", #ifdef CONFIG_SLIRP @@ -1620,7 +1625,30 @@ void net_init_clients(void) */ bool netdev_is_modern(const char *optarg) { - return false; + QemuOpts *opts; + bool is_modern; + const char *type; + static QemuOptsList dummy_opts =3D { + .name =3D "netdev", + .implied_opt_name =3D "type", + .head =3D QTAILQ_HEAD_INITIALIZER(dummy_opts.head), + .desc =3D { { } }, + }; + + if (optarg[0] =3D=3D '{') { + /* This is JSON, which means it's modern syntax */ + return true; + } + + opts =3D qemu_opts_create(&dummy_opts, NULL, false, &error_abort); + qemu_opts_do_parse(opts, optarg, dummy_opts.implied_opt_name, + &error_abort); + type =3D qemu_opt_get(opts, "type"); + is_modern =3D !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram"); + + qemu_opts_reset(&dummy_opts); + + return is_modern; } =20 /* diff --git a/net/stream.c b/net/stream.c new file mode 100644 index 000000000000..0a7e84749a3f --- /dev/null +++ b/net/stream.c @@ -0,0 +1,425 @@ +/* + * QEMU System Emulator + * + * Copyright (c) 2003-2008 Fabrice Bellard + * Copyright (c) 2022 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" + +#include "net/net.h" +#include "clients.h" +#include "monitor/monitor.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/option.h" +#include "qemu/sockets.h" +#include "qemu/iov.h" +#include "qemu/main-loop.h" +#include "qemu/cutils.h" + +typedef struct NetStreamState { + NetClientState nc; + int listen_fd; + int fd; + 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; + +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_update_fd_handler(NetStreamState *s) +{ + 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; + + net_stream_write_poll(s, false); + + qemu_flush_queued_packets(&s->nc); +} + +static ssize_t net_stream_receive(NetClientState *nc, const uint8_t *buf, + size_t size) +{ + NetStreamState *s =3D DO_UPCAST(NetStreamState, nc, nc); + uint32_t len =3D htonl(size); + struct iovec iov[] =3D { + { + .iov_base =3D &len, + .iov_len =3D sizeof(len), + }, { + .iov_base =3D (void *)buf, + .iov_len =3D size, + }, + }; + size_t remaining; + ssize_t ret; + + 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) { + ret =3D 0; /* handled further down */ + } + if (ret =3D=3D -1) { + s->send_index =3D 0; + return -errno; + } + if (ret < (ssize_t)remaining) { + s->send_index +=3D ret; + net_stream_write_poll(s, true); + return 0; + } + s->send_index =3D 0; + return size; +} + +static void net_stream_send_completed(NetClientState *nc, ssize_t len) +{ + NetStreamState *s =3D DO_UPCAST(NetStreamState, nc, nc); + + if (!s->read_poll) { + net_stream_read_poll(s, true); + } +} + +static void net_stream_rs_finalize(SocketReadState *rs) +{ + NetStreamState *s =3D container_of(rs, NetStreamState, rs); + + 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); + } +} + +static void net_stream_send(void *opaque) +{ + NetStreamState *s =3D opaque; + int size; + int ret; + uint8_t buf1[NET_BUFSIZE]; + const uint8_t *buf; + + size =3D recv(s->fd, buf1, sizeof(buf1), 0); + if (size < 0) { + if (errno !=3D EWOULDBLOCK) { + goto eoc; + } + } 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); + } + closesocket(s->fd); + + 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, ""); + + return; + } + buf =3D buf1; + + ret =3D net_fill_rstate(&s->rs, buf, size); + + if (ret =3D=3D -1) { + goto eoc; + } +} + +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->listen_fd !=3D -1) { + qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); + closesocket(s->listen_fd); + s->listen_fd =3D -1; + } +} + +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), + .receive =3D net_stream_receive, + .cleanup =3D net_stream_cleanup, +}; + +static NetStreamState *net_stream_fd_init(NetClientState *peer, + const char *model, + const char *name, + int fd, int is_connected) +{ + NetClientState *nc; + NetStreamState *s; + + nc =3D qemu_new_net_client(&net_stream_info, peer, model, name); + + qemu_set_info_str(nc, "fd=3D%d", fd); + + s =3D DO_UPCAST(NetStreamState, nc, nc); + + s->fd =3D fd; + s->listen_fd =3D -1; + net_socket_rs_init(&s->rs, net_stream_rs_finalize, false); + + /* Disable Nagle algorithm on TCP sockets to reduce latency */ + socket_set_nodelay(fd); + + if (is_connected) { + net_stream_connect(s); + } else { + qemu_set_fd_handler(s->fd, NULL, net_stream_connect, s); + } + return s; +} + +static void net_stream_accept(void *opaque) +{ + NetStreamState *s =3D opaque; + struct sockaddr_in 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; + } + } + + s->fd =3D fd; + s->nc.link_down =3D false; + net_stream_connect(s); + qemu_set_info_str(&s->nc, "connection from %s:%d", + inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); +} + +static int net_stream_server_init(NetClientState *peer, + const char *model, + const char *name, + SocketAddress *addr, + Error **errp) +{ + NetClientState *nc; + NetStreamState *s; + int fd, ret; + + 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); + + socket_set_fast_reuse(fd); + + 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_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; + } + + ret =3D listen(fd, 0); + if (ret < 0) { + error_setg_errno(errp, errno, "can't listen on socket"); + closesocket(fd); + return -1; + } + + 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); + + qemu_set_fd_handler(s->listen_fd, net_stream_accept, NULL, s); + return 0; +} + +static int net_stream_client_init(NetClientState *peer, + const char *model, + const char *name, + SocketAddress *addr, + Error **errp) +{ + NetStreamState *s; + struct sockaddr_in saddr_in; + 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; + } + + 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 || + errno =3D=3D EINVAL) { + 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 or fd type"); + return -1; + } + + 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_FD: + qemu_set_info_str(&s->nc, "connect to fd %d", fd); + break; + default: + g_assert_not_reached(); + } + + return 0; +} + +int net_init_stream(const Netdev *netdev, const char *name, + NetClientState *peer, Error **errp) +{ + const NetdevStreamOptions *sock; + + assert(netdev->type =3D=3D NET_CLIENT_DRIVER_STREAM); + sock =3D &netdev->u.stream; + + if (!sock->has_server || !sock->server) { + return net_stream_client_init(peer, "stream", name, sock->addr, er= rp); + } + return net_stream_server_init(peer, "stream", name, sock->addr, errp); +} diff --git a/qapi/net.json b/qapi/net.json index dd088c09c509..185748cc6c6c 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -7,6 +7,7 @@ ## =20 { 'include': 'common.json' } +{ 'include': 'sockets.json' } =20 ## # @set_link: @@ -573,6 +574,60 @@ '*isolated': 'bool' }, 'if': 'CONFIG_VMNET' } =20 +## +# @NetdevStreamOptions: +# +# Configuration info for stream socket netdev +# +# @addr: socket address to listen on (server=3Dtrue) +# or connect to (server=3Dfalse) +# @server: create server socket (default: false) +# +# Only SocketAddress types 'inet' and 'fd' are supported. +# +# Since: 7.2 +## +{ 'struct': 'NetdevStreamOptions', + 'data': { + 'addr': 'SocketAddress', + '*server': 'bool' } } + +## +# @NetdevDgramOptions: +# +# Configuration info for datagram socket netdev. +# +# @remote: remote address +# @local: local address +# +# Only SocketAddress types 'inet' and 'fd' are supported. +# +# If remote address is present and it's a multicast address, local address +# is optional. Otherwise local address is required and remote address is +# optional. +# +# .. table:: Valid parameters combination table +# :widths: auto +# +# =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D +# remote local okay? +# =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D +# absent absent no +# absent not fd no +# absent fd yes +# multicast absent yes +# multicast present yes +# not multicast absent no +# not multicast present yes +# =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D = =3D=3D=3D=3D=3D +# +# Since: 7.2 +## +{ 'struct': 'NetdevDgramOptions', + 'data': { + '*local': 'SocketAddress', + '*remote': 'SocketAddress' } } + ## # @NetClientDriver: # @@ -584,10 +639,13 @@ # @vmnet-host since 7.1 # @vmnet-shared since 7.1 # @vmnet-bridged since 7.1 +# @stream since 7.2 +# @dgram since 7.2 ## { 'enum': 'NetClientDriver', - 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'vde', - 'bridge', 'hubport', 'netmap', 'vhost-user', 'vhost-vdpa', + 'data': [ 'none', 'nic', 'user', 'tap', 'l2tpv3', 'socket', 'stream', + 'dgram', 'vde', 'bridge', 'hubport', 'netmap', 'vhost-user', + 'vhost-vdpa', { 'name': 'vmnet-host', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-shared', 'if': 'CONFIG_VMNET' }, { 'name': 'vmnet-bridged', 'if': 'CONFIG_VMNET' }] } @@ -607,6 +665,8 @@ # 'vmnet-host' - since 7.1 # 'vmnet-shared' - since 7.1 # 'vmnet-bridged' - since 7.1 +# 'stream' since 7.2 +# 'dgram' since 7.2 ## { 'union': 'Netdev', 'base': { 'id': 'str', 'type': 'NetClientDriver' }, @@ -617,6 +677,8 @@ 'tap': 'NetdevTapOptions', 'l2tpv3': 'NetdevL2TPv3Options', 'socket': 'NetdevSocketOptions', + 'stream': 'NetdevStreamOptions', + 'dgram': 'NetdevDgramOptions', 'vde': 'NetdevVdeOptions', 'bridge': 'NetdevBridgeOptions', 'hubport': 'NetdevHubPortOptions', diff --git a/qemu-options.hx b/qemu-options.hx index eb38e5dc40bc..396c1d11e1e2 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2772,6 +2772,18 @@ 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=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" + "-netdev dgram,id=3Dstr,remote.type=3Dinet,remote.host=3Dmaddr,remote.= port=3Dport[,local.type=3Dinet,local.host=3Daddr]\n" + "-netdev dgram,id=3Dstr,remote.type=3Dinet,remote.host=3Dmaddr,remote.= port=3Dport[,local.type=3Dfd,local.str=3Dfile-descriptor]\n" + " configure a network backend to connect to a multicast= maddr and port\n" + " use ``local.host=3Daddr`` to specify the host address= to send packets from\n" + "-netdev dgram,id=3Dstr,local.type=3Dinet,local.host=3Daddr,local.port= =3Dport[,remote.type=3Dinet,remote.host=3Daddr,remote.port=3Dport]\n" + "-netdev dgram,id=3Dstr,local.type=3Dfd,local.str=3Dfile-descriptor\n" + " configure a network backend to connect to another net= work\n" + " using an UDP tunnel\n" #ifdef CONFIG_VDE "-netdev vde,id=3Dstr[,sock=3Dsocketpath][,port=3Dn][,group=3Dgroupnam= e][,mode=3Doctalmode]\n" " configure a network backend to connect to port 'n' of= a vde switch\n" --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666343756629991.8713798869745; Fri, 21 Oct 2022 02:15:56 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo83-00031h-P6 for importer@patchew.org; Fri, 21 Oct 2022 05:15:51 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2Z-0003is-9s for importer@patchew.org; Fri, 21 Oct 2022 05:10:07 -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 1olo2I-0003ME-5V for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:53 -0400 Received: from mout.kundenserver.de ([217.72.192.73]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2E-0001Zk-Mk for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:49 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MX0Xf-1ob0Fl1bC5-00XO1p; Fri, 21 Oct 2022 11:09:36 +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?= , Stefano Brivio Subject: [PATCH v14 07/17] net: socket: Don't ignore EINVAL on netdev socket connection Date: Fri, 21 Oct 2022 11:09:12 +0200 Message-Id: <20221021090922.170074-8-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:bd+GEfX3VxfGbU9uQxgyK0I9xZKcP9lU6sbZ1cQiIG52z4WS3Es /LQbm/MXYJjkhbFCwEv/TgM9JyjYO8vYk9NLrNikDpZkleiObUICWJxLrjrxZWHTBwk47cN UX/vbrW2C1vbbJz3iiDT/01KoLue3SLSdmUxC1Iy2YLBUe9n3YdUaJ250nUfdueVygOl7Pu osvRgKANdZAEbxTToGTag== X-UI-Out-Filterresults: notjunk:1;V03:K0:mkmviekNp8c=:WQ+FEdo19ymXo60ydZcbR4 3kP52n0hTugu7HLO6S8Tm2slcIlNzQow+XtKta52lhGWgNdXDH/gP+HzolVeIXZeJ4uDIgPXt FziRx5AtAhxgBmRN2o13w4ryjg2DdmdM4SgfJVAc6njlP3htiy6f4U+PdRWm27uznXyEJa5AQ /iFCr4rHkBAvXR7z7+YcVU+JB0PjJYjov+0jTWFigcXAeE+KkNbfANYBKBggDIDgwCAZUhMs1 CIl3990dkUjrnBamOR7CWB3etSydekTdkU16QqR/oMOE+y2S59s5H43ZA/A74XUhsrxpFLJq+ usA7EfOJ+VCUyAZHL7b+m+EYy9I0yoxMmUts6TLdMUgeLEY/d9a4TKdAUCwY2+/Nv9GANBFk7 O7JrBLq2u1QLOZIby7ANX5NMdhlD8dOFEp8SWjhg9KcZnWjpdoKvRQS8nhYyiqXo+jvwmRAAI Z+IB0D7bug+ybUjSEcd0OPsTtRaWlX5omRafca+I2DdlPTKJ/AamlE7mTcN3xUlbb9T7/zi1M AWd96D0lg+/enp9ktxESHAogVZWNoMChx9mTjPZ/RnfDG5YCxxps/y9BGK4dPgUyly6ha8JpN xdqUf82wK5vr7NhuB2O+QCidEE8bQ8AHtYbJXWlFBBrrUtZW7SbLTuQGvSIgf0Th10yL4U+5d +2zdDvk1SePidq68hJzOwCm5coxLPXKs1BTfRTX+ZUbJ3uu7KgP3+B6M4V/Y6DEd0N4w5QFja /NthcMxGH+FamiDIb7zr2xVNeCFHH6pZF0CRrluDy1oaDCeeI+RFGCD31S1Ah22Dfye4pjRh+ aq3XA/a 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.73; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666343758786100001 Content-Type: text/plain; charset="utf-8" From: Stefano Brivio Other errors are treated as failure by net_socket_connect_init(), but if connect() returns EINVAL, we'll fail silently. Remove the related exception. Signed-off-by: Stefano Brivio Signed-off-by: Laurent Vivier Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin --- net/socket.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/net/socket.c b/net/socket.c index ade1ecf38b87..4944bb70d580 100644 --- a/net/socket.c +++ b/net/socket.c @@ -577,8 +577,7 @@ static int net_socket_connect_init(NetClientState *peer, if (errno =3D=3D EINTR || errno =3D=3D EWOULDBLOCK) { /* continue */ } else if (errno =3D=3D EINPROGRESS || - errno =3D=3D EALREADY || - errno =3D=3D EINVAL) { + errno =3D=3D EALREADY) { break; } else { error_setg_errno(errp, errno, "can't connect socket"); --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666344591595497.5142251591692; Fri, 21 Oct 2022 02:29:51 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloLd-0003o6-BK for importer@patchew.org; Fri, 21 Oct 2022 05:29:49 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2Z-0003ii-7A for importer@patchew.org; Fri, 21 Oct 2022 05:10:07 -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 1olo2I-0003MD-4t for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:53 -0400 Received: from mout.kundenserver.de ([212.227.17.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2F-0001Zs-GO for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:49 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1N9d91-1p9EXc3FBd-015cJh; Fri, 21 Oct 2022 11:09:38 +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?= , Stefano Brivio Subject: [PATCH v14 08/17] net: stream: Don't ignore EINVAL on netdev socket connection Date: Fri, 21 Oct 2022 11:09:13 +0200 Message-Id: <20221021090922.170074-9-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:B8MiZnq3mijX2C60eMitMUnHfhjAsmiSOOLIMe8CGzGcyXH8Amn R6kmli/R29qVZGFwW99SSIIHnpIJSCUUtm3YMS7x1N49hx7YaIIR+TucE17slkVtkbbuqak 3L8Tq+Hp1uQVUAmPvXgxl5HsqB5f8GVHQOiUJKQmaUpo6ftUkNYfPzFaEbWDnNWYD6xp/k0 V0XP7hgAHtmYUEkdRF2mw== X-UI-Out-Filterresults: notjunk:1;V03:K0:1qn1Ptr2aeU=:x2FfOD5n7oRGv2YOTnEI0V UT9fLRdX2EJk4NczaChet0yCQWq+P+BbdoV2S0xQ1+04VmTVjt4Lk4i0JIf6RPEfkh5f/aFHc UtymMMuvcy+OLtF31Pj+E1W0QhicSiY7XrS2kzTuLZLrY8SLo/7QHX6qLNQ2rZ3RuRZ2wqOwH gDPkpLkTZ3vDIvk598vomL8O1JjWKdAIQLcK3kXOnpNfYYwzWO4mxzTMohMKGTzv+o3uQlLlf SVjL2fs3Fc/EGtKsND3cOOVq6Fihtrq7PuofITaZrsxwsUVqKH5ZjmfpeGTnP3JxLAqUfFOuD 5M+986X7hmvdMMLWzDFRNL/GBtzg9uhNYEvN27tXviwzoLsSdgGMu5HxVQVCG7EQVzrE9dx+3 8vloEszKyHwFlPFdrR7I2CP8i2LmWUn2S5klGmmyA63IaQElXwJuiusYcvj4NxE5AZ2a6sp1D 66j/Dv9hyYbCR1h+ybOLewTNmoxYTiXHZtzsoMRvfPLtL3On9ZKMkyKltTOETH0kWjgGXC/4G U64Bp2P7ag/jGOyrCgkOLeo+5nFkBYhHEbQvkBN4Ii0ezjQUP9mz2m3GclYxU8Dh/DIXgZBwK ulI0n6FwRzDn5b9QSU8F/iEhhEhv+wNvK2Bl+i4x8LM603qkVz7HDe4wIvDIqziBVaJligIbW Cs2ybbI7bb419EM15aGZ6AOH6aE/5mEXf08kUakPsoPceYHVf0O4V8xFAgRRcp8srZy5Zpi2w LzJmQVots76Ir2ay+qhRkyT0fWAj+Nk9HLhSxqjGycYKq/r9mwFcBJujEwQSS4p/W5HdGtezI fMS0jZ+ 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=212.227.17.13; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666344592947100003 From: Stefano Brivio Other errors are treated as failure by net_stream_client_init(), but if connect() returns EINVAL, we'll fail silently. Remove the related exception. Signed-off-by: Stefano Brivio [lvivier: applied to net/stream.c] Signed-off-by: Laurent Vivier Reviewed-by: Daniel P. Berrang=C3=A9 Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin --- net/stream.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/net/stream.c b/net/stream.c index 0a7e84749a3f..e4388fe7e45c 100644 --- a/net/stream.c +++ b/net/stream.c @@ -360,8 +360,7 @@ static int net_stream_client_init(NetClientState *peer, if (errno =3D=3D EINTR || errno =3D=3D EWOULDBLOCK) { /* continue */ } else if (errno =3D=3D EINPROGRESS || - errno =3D=3D EALREADY || - errno =3D=3D EINVAL) { + errno =3D=3D EALREADY) { break; } else { error_setg_errno(errp, errno, "can't connect socket"); --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666344064137263.4440383899323; Fri, 21 Oct 2022 02:21:04 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloD6-0002Nw-7u for importer@patchew.org; Fri, 21 Oct 2022 05:21:00 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2Z-0003iu-MB for importer@patchew.org; Fri, 21 Oct 2022 05:10:07 -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 1olo2J-0003Mv-RU for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:54 -0400 Received: from mout.kundenserver.de ([212.227.17.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2H-0001ap-US for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:51 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MDgxt-1ovOxI0zPb-00AmPP; Fri, 21 Oct 2022 11:09:39 +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?= , Stefano Brivio , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v14 09/17] net: stream: add unix socket Date: Fri, 21 Oct 2022 11:09:14 +0200 Message-Id: <20221021090922.170074-10-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:AV2lG9hA5qh6jdvV7vYHx4BwdiWeVyu1SsntwDCDTMWUkAJgckP 0RP5ZhNCfXz/NO/1I4l2Wxn5kkHFc3nx0i0QhLkMFo7Io7anJjws9zS1kxXwhD2HSVPxq64 PJnv13nvgwe/MFm0SpUJwr3d7YSX3xU8DOGlJMaDmH4QxLlaqpQLfwCb7oGMCu437tOT8uj SbBO50VMgJ+kA3O/aqusg== X-UI-Out-Filterresults: notjunk:1;V03:K0:eKAce30NA6o=:P7ekYlHk/wHXznaJR1BW3U Y5Pj19NLWe5uCShE/HjsuoQ2mpZhCLFvoq7nD1PY+Jt4En7yU32KDAtfLf+o6E0T7nlLMed7d PW8rJoezqtrulLMawc/kEB652YrQJRW8S9JlWqnnUMYiAKTV/bw6cHmGZOc3jPeBY2kqnQ74f aZ3Jd5+yxdQnqDGxwrtV5vHAub6xQjipVL++tyxMuTRAhjWhsRs45avr+FukrC1fuXpvRIkc/ rZ0p9560tYDMOBU22l3bVHmmHZxQdWrqvTXE3v293wScyHv8ipaDKMwugW/2PHxD2RYanQh7B NN2fHUDFt3PLj946P5RU7mjEMGJLHPnP2ROPC8HmAY0IiqTm8xDAKHDYm5IdMj9/FeVmyFa7A 8NDif0WxWMGvZjXCNIWbCwrWRu0+A/H6Q/e75TGE+ywAg1BWsu0u6FZvlxJTiWKqFcctvzZew 2x6Qds7cY7sfwiFxMESZjJ7cm9vwtYBCiKH/b9Lw/cXV7uUZUVpY9X9UrFYDGfgXakigV2zym zJcFwIsz2W8T72uUXPeQWy5GqQzwJYMjaDkN+PSFOsmSPDwKmh5NslLUdPEYAh2qHsSP8+2BL J5XJ3p1dc8Jlpx0wsvawSyEsrhJLmcG5XRL8iEiklijAWm4toKjU/2jWoES0RCPMYrPRGFCcO SZUCln8J5bylvga0YpXrS6nvn5ZOViZ0gmxlDeAmRaOMUMQ+DMR+DaD9Q5sct/88c3WRu/yvp jGXbG1OxhKIRzdFj08ZUxoMeZAuUvnn5at5szzusgf8I6cExB5YwOZd1kpcvq8BYKGEiyeelG Wot34X7 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=212.227.17.13; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666344065841100001 Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Acked-by: Michael S. Tsirkin Acked-by: Markus Armbruster (QAPI schema) Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- net/stream.c | 107 +++++++++++++++++++++++++++++++++++++++++++++--- qapi/net.json | 2 +- qemu-options.hx | 1 + 3 files changed, 104 insertions(+), 6 deletions(-) diff --git a/net/stream.c b/net/stream.c index e4388fe7e45c..884f473018da 100644 --- a/net/stream.c +++ b/net/stream.c @@ -235,7 +235,7 @@ static NetStreamState *net_stream_fd_init(NetClientStat= e *peer, static void net_stream_accept(void *opaque) { NetStreamState *s =3D opaque; - struct sockaddr_in saddr; + struct sockaddr_storage saddr; socklen_t len; int fd; =20 @@ -253,8 +253,26 @@ static void net_stream_accept(void *opaque) s->fd =3D fd; s->nc.link_down =3D false; net_stream_connect(s); - qemu_set_info_str(&s->nc, "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 =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; + } + case AF_UNIX: { + struct sockaddr_un saddr_un; + + 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(); + } } =20 static int net_stream_server_init(NetClientState *peer, @@ -294,6 +312,43 @@ static int net_stream_server_init(NetClientState *peer, } 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; + } + + 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; + } + + 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) { @@ -337,6 +392,7 @@ static int net_stream_client_init(NetClientState *peer, { NetStreamState *s; struct sockaddr_in saddr_in; + struct sockaddr_un saddr_un; int fd, connected, ret; =20 switch (addr->type) { @@ -373,6 +429,45 @@ static int net_stream_client_init(NetClientState *peer, } } 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; + } + + 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) { @@ -387,7 +482,7 @@ static int net_stream_client_init(NetClientState *peer, connected =3D 1; break; default: - error_setg(errp, "only support inet or fd type"); + error_setg(errp, "only support inet, unix or fd type"); return -1; } =20 @@ -399,13 +494,15 @@ static int net_stream_client_init(NetClientState *pee= r, 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/qapi/net.json b/qapi/net.json index 185748cc6c6c..aed4ce1a97ff 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -583,7 +583,7 @@ # or connect to (server=3Dfalse) # @server: create server socket (default: false) # -# Only SocketAddress types 'inet' and 'fd' are supported. +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # # Since: 7.2 ## diff --git a/qemu-options.hx b/qemu-options.hx index 396c1d11e1e2..98e2595df93b 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2773,6 +2773,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " 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=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 From nobody Thu Nov 14 18:13:17 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 1666348777769865.8204055281841; Fri, 21 Oct 2022 03:39:37 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olpR6-0001jp-TB for importer@patchew.org; Fri, 21 Oct 2022 06:39:34 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2c-0003qk-2L for importer@patchew.org; Fri, 21 Oct 2022 05:10:10 -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 1olo2M-0003RW-OK for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:57 -0400 Received: from mout.kundenserver.de ([217.72.192.73]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2I-0001eU-BS for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:54 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MYN3E-1oXyNi3IgM-00VS5K; Fri, 21 Oct 2022 11:09:41 +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?= , Stefano Brivio , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v14 10/17] net: dgram: make dgram_dst generic Date: Fri, 21 Oct 2022 11:09:15 +0200 Message-Id: <20221021090922.170074-11-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:mVD/M1LlMvHfxLyz+io1y8fQZ+rSM1FQEF+POP1VXOpyJrA8Ppz UfU9pvOBQhjYVxGt8Ekh8r26kukC+rIyR09Zjm5PyUuOd8zBuOkCjCM3j8uIp6dQG811qw/ IW5YO8fncXi4SWcz4alhom/gLum3z0+8JqBNn73FuD0zIGU1dw601PHGRkNcjzjIZGskqcr 0H3Tg8adsdHE36OnDTPOw== X-UI-Out-Filterresults: notjunk:1;V03:K0:o2nAJVUvtSo=:ws5laaF9m86nNqe2XOMp4F M61/rlcA+DCYltjAVsPue0Ca5FpJDqvcwPtE/jkqv8RJz7+unX8RqXv8hn+U/B1QlfTqh5UCi quge/NGCcOveyJ6R1pApaLNr5TOFcK76tV4z5wWvTgaiuKFJTRb8sb8B7vMR2037f2GY9f/CD KUOlsXkYP/ZCPzoOqyITEiVyqMBIRyFdSp3Oi+Ta5GOlSfULYdIr4/eG+PdLvMmewtnRvzW5O /W48OGCSUu9IgYwVdi4SPK1snGT936zHHTA7MOAO7v4n1VCnYS+KIaKRqq+ONp0gLHswxoaaV SKmBwSa27A+et2lnKw9HZPjf4YqjYSwvj5FKwy1ZTbydhjHJc1lwxSWmKlB+GxA5EusZ0K4t6 tGvKYEnZ0C3J8UUucWpSIRHPr+h+66WOWtVhsCysB63zGfxScXl3kUTrC6/WpmDQIJYzq598e /2vDo0NTG69nBctGcw0GOrArlUncOxfzys3nPi8rDDDkoH7m6CnEg9xK+GWTmlQ7Xdyt/yWrP tU3LD08n5e3NJ+9/BaPdtJKg6n1/LtLi7jdLAl7zFLrzrODZsmWXmWrCIBJW/c2wwOJNyPqpF arY2zhCyslydGtoM58SDwNj3IxPSxFgFE6QeiI2/OMLPQhPBSBZDA7MvzQ11gCY7n30eMk6Lw 7/ZFfJo5HZGumR2uWA9w4nTYlTCdYLoSCeYwLGClVD/N165SezruRUJNySdI9P1IoQKIGhL3m ISz+m1gPXVA6O5xBmf/1PWfuDg6Z4psVkc+L0z2vWm8ko/cjPb8Dvoo85Z9g104mKIw/k7ZpE c2QsAnr 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.73; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666348779722100003 dgram_dst is a sockaddr_in structure. To be able to use it with unix socket, use a pointer to a generic sockaddr structure. Rename it dest_addr, and store socket length in dest_len. Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Acked-by: Michael S. Tsirkin Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- net/dgram.c | 82 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/net/dgram.c b/net/dgram.c index 5339585b8202..e20be9ca79d3 100644 --- a/net/dgram.c +++ b/net/dgram.c @@ -40,9 +40,11 @@ typedef struct NetDgramState { NetClientState nc; int fd; SocketReadState rs; - struct sockaddr_in dgram_dst; /* contains destination iff connectionle= ss */ bool read_poll; /* waiting to receive data? */ bool write_poll; /* waiting to transmit data? */ + /* contains destination iff connectionless */ + struct sockaddr *dest_addr; + socklen_t dest_len; } NetDgramState; =20 static void net_dgram_send(void *opaque); @@ -84,10 +86,8 @@ static ssize_t net_dgram_receive(NetClientState *nc, ssize_t ret; =20 do { - if (s->dgram_dst.sin_family !=3D AF_UNIX) { - ret =3D sendto(s->fd, buf, size, 0, - (struct sockaddr *)&s->dgram_dst, - sizeof(s->dgram_dst)); + if (s->dest_addr) { + ret =3D sendto(s->fd, buf, size, 0, s->dest_addr, s->dest_len); } else { ret =3D send(s->fd, buf, size, 0); } @@ -244,6 +244,9 @@ static void net_dgram_cleanup(NetClientState *nc) close(s->fd); s->fd =3D -1; } + g_free(s->dest_addr); + s->dest_addr =3D NULL; + s->dest_len =3D 0; } =20 static NetClientInfo net_dgram_socket_info =3D { @@ -260,7 +263,7 @@ static NetDgramState *net_dgram_fd_init(NetClientState = *peer, SocketAddress *mcast, Error **errp) { - struct sockaddr_in saddr; + struct sockaddr_in *saddr =3D NULL; int newfd; NetClientState *nc; NetDgramState *s; @@ -275,31 +278,32 @@ static NetDgramState *net_dgram_fd_init(NetClientStat= e *peer, qapi_free_SocketAddress(sa); =20 /* - * fd passed: multicast: "learn" dgram_dst address from bound address = and + * fd passed: multicast: "learn" dest_addr address from bound address = and * save it. Because this may be "shared" socket from a "master" proces= s, * datagrams would be recv() by ONLY ONE process: we must "clone" this * dgram socket --jjo */ =20 if (is_fd && mcast !=3D NULL) { - if (convert_host_port(&saddr, mcast->u.inet.host, - mcast->u.inet.port, errp) < 0) { + saddr =3D g_new(struct sockaddr_in, 1); + + if (convert_host_port(saddr, mcast->u.inet.host, mcast->u.inet= .port, + errp) < 0) { goto err; } /* must be bound */ - if (saddr.sin_addr.s_addr =3D=3D 0) { + if (saddr->sin_addr.s_addr =3D=3D 0) { error_setg(errp, "can't setup multicast destination addres= s"); goto err; } /* clone dgram socket */ - newfd =3D net_dgram_mcast_create(&saddr, NULL, errp); + newfd =3D net_dgram_mcast_create(saddr, NULL, errp); if (newfd < 0) { goto err; } /* clone newfd to fd, close newfd */ dup2(newfd, fd); close(newfd); - } =20 nc =3D qemu_new_net_client(&net_dgram_socket_info, peer, model, name); @@ -311,21 +315,20 @@ static NetDgramState *net_dgram_fd_init(NetClientStat= e *peer, net_dgram_read_poll(s, true); =20 /* mcast: save bound address as dst */ - if (is_fd && mcast !=3D NULL) { - s->dgram_dst =3D saddr; + if (saddr) { + g_assert(s->dest_addr =3D=3D NULL); + s->dest_addr =3D (struct sockaddr *)saddr; + s->dest_len =3D sizeof(*saddr); qemu_set_info_str(nc, "fd=3D%d (cloned mcast=3D%s:%d)", fd, - inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)= ); + inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_por= t)); } else { - if (sa_type =3D=3D SOCKET_ADDRESS_TYPE_UNIX) { - s->dgram_dst.sin_family =3D AF_UNIX; - } - qemu_set_info_str(nc, "fd=3D%d %s", fd, SocketAddressType_str(sa_t= ype)); } =20 return s; =20 err: + g_free(saddr); closesocket(fd); return NULL; } @@ -339,21 +342,24 @@ static int net_dgram_mcast_init(NetClientState *peer, { NetDgramState *s; int fd, ret; - struct sockaddr_in saddr; + struct sockaddr_in *saddr; =20 if (remote->type !=3D SOCKET_ADDRESS_TYPE_INET) { error_setg(errp, "multicast only support inet type"); return -1; } =20 - if (convert_host_port(&saddr, remote->u.inet.host, remote->u.inet.port, + saddr =3D g_new(struct sockaddr_in, 1); + if (convert_host_port(saddr, remote->u.inet.host, remote->u.inet.port, errp) < 0) { + g_free(saddr); return -1; } =20 if (!local) { - fd =3D net_dgram_mcast_create(&saddr, NULL, errp); + fd =3D net_dgram_mcast_create(saddr, NULL, errp); if (fd < 0) { + g_free(saddr); return -1; } } else { @@ -362,13 +368,15 @@ static int net_dgram_mcast_init(NetClientState *peer, struct in_addr localaddr; =20 if (inet_aton(local->u.inet.host, &localaddr) =3D=3D 0) { + g_free(saddr); error_setg(errp, "localaddr '%s' is not a valid IPv4 addre= ss", local->u.inet.host); return -1; } =20 - fd =3D net_dgram_mcast_create(&saddr, &localaddr, errp); + fd =3D net_dgram_mcast_create(saddr, &localaddr, errp); if (fd < 0) { + g_free(saddr); return -1; } break; @@ -376,16 +384,19 @@ static int net_dgram_mcast_init(NetClientState *peer, case SOCKET_ADDRESS_TYPE_FD: fd =3D monitor_fd_param(monitor_cur(), local->u.fd.str, errp); if (fd =3D=3D -1) { + g_free(saddr); return -1; } ret =3D qemu_socket_try_set_nonblock(fd); if (ret < 0) { + g_free(saddr); error_setg_errno(errp, -ret, "%s: Can't use file descripto= r %d", name, fd); return -1; } break; default: + g_free(saddr); error_setg(errp, "only support inet or fd type for local"); return -1; } @@ -395,13 +406,17 @@ static int net_dgram_mcast_init(NetClientState *peer, local->type =3D=3D SOCKET_ADDRESS_TYPE_FD, remote, errp); if (!s) { + g_free(saddr); return -1; } =20 - s->dgram_dst =3D saddr; + g_assert(s->dest_addr =3D=3D NULL); + s->dest_addr =3D (struct sockaddr *)saddr; + s->dest_len =3D sizeof(*saddr); + + qemu_set_info_str(&s->nc, "mcast=3D%s:%d", inet_ntoa(saddr->sin_addr), + ntohs(saddr->sin_port)); =20 - qemu_set_info_str(&s->nc, "mcast=3D%s:%d", inet_ntoa(saddr.sin_addr), - ntohs(saddr.sin_port)); return 0; =20 } @@ -412,9 +427,10 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, { NetDgramState *s; int fd, ret; - struct sockaddr_in raddr_in; - struct sockaddr_in laddr_in; SocketAddress *remote, *local; + struct sockaddr *dest_addr; + struct sockaddr_in laddr_in, raddr_in; + socklen_t dest_len; =20 assert(netdev->type =3D=3D NET_CLIENT_DRIVER_DGRAM); =20 @@ -491,6 +507,10 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, return -1; } qemu_socket_set_nonblock(fd); + + dest_len =3D sizeof(raddr_in); + dest_addr =3D g_malloc(dest_len); + memcpy(dest_addr, &raddr_in, dest_len); break; case SOCKET_ADDRESS_TYPE_FD: fd =3D monitor_fd_param(monitor_cur(), local->u.fd.str, errp); @@ -503,6 +523,8 @@ int net_init_dgram(const Netdev *netdev, const char *na= me, name, fd); return -1; } + dest_addr =3D NULL; + dest_len =3D 0; break; default: error_setg(errp, "only support inet or fd type for local"); @@ -515,7 +537,9 @@ int net_init_dgram(const Netdev *netdev, const char *na= me, } =20 if (remote) { - s->dgram_dst =3D raddr_in; + g_assert(s->dest_addr =3D=3D NULL); + s->dest_addr =3D dest_addr; + s->dest_len =3D dest_len; } =20 switch (local->type) { --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666347322014113.79216102124633; Fri, 21 Oct 2022 03:15:22 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olp3O-0001tf-0p for importer@patchew.org; Fri, 21 Oct 2022 06:15:05 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo3K-0005rV-46 for importer@patchew.org; Fri, 21 Oct 2022 05:10:54 -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 1olo2P-0003U7-Rx for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:58 -0400 Received: from mout.kundenserver.de ([217.72.192.74]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2L-0001oE-Gj for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:56 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1N1xdf-1pEij9193J-012L4D; Fri, 21 Oct 2022 11:09:42 +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?= , Stefano Brivio Subject: [PATCH v14 11/17] net: dgram: move mcast specific code from net_socket_fd_init_dgram() Date: Fri, 21 Oct 2022 11:09:16 +0200 Message-Id: <20221021090922.170074-12-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:AG9a+5hWp6nqnd7XCaT+idPjrAU59PHHTv8owSBq7y0rLdM6ANi 5Ay2XeS93iAZr1ep7jkLpAO/EhDl4ExQUNQApDpYhgkP0r5/UPsejZ2TFJX/WLtrOIdujo4 FUGAHuL6SFlPcQMv+ZQs0+NI3th/Fu67M+RUQB2zd4ZUWSTPMHVLH9l+hupMXcu7VgMaByx 1pDE2TK2hfHTbPsxhADgA== X-UI-Out-Filterresults: notjunk:1;V03:K0:497+hJmsvb0=:rCu7k/Gp8SbIFqYv3heujJ Q76Bzu1OtnCKeXwDwA/7suDvXNpi0UaZoTDOExbXxqR3u/ejOAGhoA+ozgIahznJpV6H6CsnB u+ouNX/LWX2GN0V+sXN3JOaEgtImEYkwU61DcqgsCI7BlOaCOyibQUGfszVE5U+xC1CLGUAci xWvYOP1ocXj6mfzHfl7+ILPds0rF86DhJm929grGUFoL2iDtqfocldeyzSHil2OARZj5rrpjL GG7vc4JDp1wPTCE+kWNq7cSxgGYkTcKfUdsz26CtErbAyrBagpgA3vf5B1axSyelGq80DD23Z gZRC5pynRBJMybJG+7fu3RKNLtbxNgQGoEZYtXLEFsrQdrXN8N/aDkGAODy4NVZD7ENH00+Mj 5J0+VNRn/ODhVRzehjp1x/jYy9K5M5pzJHLjd9VkSVjZO+qxtW36K6DHmrHY1NRImKdoc0SPT nFI2L3UqU92qw6iHUu6gcSyg8fEX2fS+3MjQf90LXjFC1DFoXVSyLqEFFehAm/hrohCODziK9 Jmq6ZEkWWV3nZwdTVpz8expaelIBhZ0tqYzTPcNFsuT2xG+PjrWTYp63JjjAdMURF8WnrphnA STDvairDlf62lbtZC2K+lpF/dC7SnWPXvylonMGawUDm3W6aaTZcJWX+L+nfz4qaQnJoTBFgE UGLwwzVDLQ/GWezLu2Vz6i65mW4rktep4tnqxsYOioTQVFJfdSgRycddQjkKzFGYxF6NJ9a66 nOpZZrd2D3p/iDWLWmRuQTrvt1VrvIZrO2z6vl1Jv1TUvK9uqSwEddyojpoXp/y5q2XDCGyWD I4lDKk1 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.74; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666347324256100001 Content-Type: text/plain; charset="utf-8" It is less complex to manage special cases directly in net_dgram_mcast_init() and net_dgram_udp_init(). Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Acked-by: Michael S. Tsirkin --- net/dgram.c | 143 ++++++++++++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 67 deletions(-) diff --git a/net/dgram.c b/net/dgram.c index e20be9ca79d3..e581cc62f39f 100644 --- a/net/dgram.c +++ b/net/dgram.c @@ -259,52 +259,11 @@ static NetClientInfo net_dgram_socket_info =3D { static NetDgramState *net_dgram_fd_init(NetClientState *peer, const char *model, const char *name, - int fd, int is_fd, - SocketAddress *mcast, + int fd, Error **errp) { - struct sockaddr_in *saddr =3D NULL; - int newfd; NetClientState *nc; NetDgramState *s; - SocketAddress *sa; - SocketAddressType sa_type; - - sa =3D socket_local_address(fd, errp); - if (!sa) { - return NULL; - } - sa_type =3D sa->type; - qapi_free_SocketAddress(sa); - - /* - * fd passed: multicast: "learn" dest_addr address from bound address = and - * save it. Because this may be "shared" socket from a "master" proces= s, - * datagrams would be recv() by ONLY ONE process: we must "clone" this - * dgram socket --jjo - */ - - if (is_fd && mcast !=3D NULL) { - saddr =3D g_new(struct sockaddr_in, 1); - - if (convert_host_port(saddr, mcast->u.inet.host, mcast->u.inet= .port, - errp) < 0) { - goto err; - } - /* must be bound */ - if (saddr->sin_addr.s_addr =3D=3D 0) { - error_setg(errp, "can't setup multicast destination addres= s"); - goto err; - } - /* clone dgram socket */ - newfd =3D net_dgram_mcast_create(saddr, NULL, errp); - if (newfd < 0) { - goto err; - } - /* clone newfd to fd, close newfd */ - dup2(newfd, fd); - close(newfd); - } =20 nc =3D qemu_new_net_client(&net_dgram_socket_info, peer, model, name); =20 @@ -314,23 +273,7 @@ static NetDgramState *net_dgram_fd_init(NetClientState= *peer, net_socket_rs_init(&s->rs, net_dgram_rs_finalize, false); net_dgram_read_poll(s, true); =20 - /* mcast: save bound address as dst */ - if (saddr) { - g_assert(s->dest_addr =3D=3D NULL); - s->dest_addr =3D (struct sockaddr *)saddr; - s->dest_len =3D sizeof(*saddr); - qemu_set_info_str(nc, "fd=3D%d (cloned mcast=3D%s:%d)", fd, - inet_ntoa(saddr->sin_addr), ntohs(saddr->sin_por= t)); - } else { - qemu_set_info_str(nc, "fd=3D%d %s", fd, SocketAddressType_str(sa_t= ype)); - } - return s; - -err: - g_free(saddr); - closesocket(fd); - return NULL; } =20 static int net_dgram_mcast_init(NetClientState *peer, @@ -381,7 +324,9 @@ static int net_dgram_mcast_init(NetClientState *peer, } break; } - case SOCKET_ADDRESS_TYPE_FD: + case SOCKET_ADDRESS_TYPE_FD: { + int newfd; + fd =3D monitor_fd_param(monitor_cur(), local->u.fd.str, errp); if (fd =3D=3D -1) { g_free(saddr); @@ -394,7 +339,42 @@ static int net_dgram_mcast_init(NetClientState *peer, name, fd); return -1; } + + /* + * fd passed: multicast: "learn" dest_addr address from bound + * address and save it. Because this may be "shared" socket fr= om a + * "master" process, datagrams would be recv() by ONLY ONE pro= cess: + * we must "clone" this dgram socket --jjo + */ + + saddr =3D g_new(struct sockaddr_in, 1); + + if (convert_host_port(saddr, local->u.inet.host, local->u.inet= .port, + errp) < 0) { + g_free(saddr); + closesocket(fd); + return -1; + } + + /* must be bound */ + if (saddr->sin_addr.s_addr =3D=3D 0) { + error_setg(errp, "can't setup multicast destination addres= s"); + g_free(saddr); + closesocket(fd); + return -1; + } + /* clone dgram socket */ + newfd =3D net_dgram_mcast_create(saddr, NULL, errp); + if (newfd < 0) { + g_free(saddr); + closesocket(fd); + return -1; + } + /* clone newfd to fd, close newfd */ + dup2(newfd, fd); + close(newfd); break; + } default: g_free(saddr); error_setg(errp, "only support inet or fd type for local"); @@ -402,9 +382,7 @@ static int net_dgram_mcast_init(NetClientState *peer, } } =20 - s =3D net_dgram_fd_init(peer, model, name, fd, - local->type =3D=3D SOCKET_ADDRESS_TYPE_FD, - remote, errp); + s =3D net_dgram_fd_init(peer, model, name, fd, errp); if (!s) { g_free(saddr); return -1; @@ -414,8 +392,26 @@ static int net_dgram_mcast_init(NetClientState *peer, s->dest_addr =3D (struct sockaddr *)saddr; s->dest_len =3D sizeof(*saddr); =20 - qemu_set_info_str(&s->nc, "mcast=3D%s:%d", inet_ntoa(saddr->sin_addr), - ntohs(saddr->sin_port)); + if (!local) { + qemu_set_info_str(&s->nc, "mcast=3D%s:%d", + inet_ntoa(saddr->sin_addr), + ntohs(saddr->sin_port)); + } else { + switch (local->type) { + case SOCKET_ADDRESS_TYPE_INET: + qemu_set_info_str(&s->nc, "mcast=3D%s:%d", + inet_ntoa(saddr->sin_addr), + ntohs(saddr->sin_port)); + break; + case SOCKET_ADDRESS_TYPE_FD: + qemu_set_info_str(&s->nc, "fd=3D%d (cloned mcast=3D%s:%d)", + fd, inet_ntoa(saddr->sin_addr), + ntohs(saddr->sin_port)); + break; + default: + g_assert_not_reached(); + } + } =20 return 0; =20 @@ -531,7 +527,7 @@ int net_init_dgram(const Netdev *netdev, const char *na= me, return -1; } =20 - s =3D net_dgram_fd_init(peer, "dgram", name, fd, 0, NULL, errp); + s =3D net_dgram_fd_init(peer, "dgram", name, fd, errp); if (!s) { return -1; } @@ -550,9 +546,22 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, inet_ntoa(raddr_in.sin_addr), ntohs(raddr_in.sin_port)); break; - case SOCKET_ADDRESS_TYPE_FD: - qemu_set_info_str(&s->nc, "fd=3D%d", fd); + case SOCKET_ADDRESS_TYPE_FD: { + SocketAddress *sa; + SocketAddressType sa_type; + + sa =3D socket_local_address(fd, errp); + if (sa) { + sa_type =3D sa->type; + qapi_free_SocketAddress(sa); + + qemu_set_info_str(&s->nc, "fd=3D%d %s", fd, + SocketAddressType_str(sa_type)); + } else { + qemu_set_info_str(&s->nc, "fd=3D%d", fd); + } break; + } default: g_assert_not_reached(); } --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666347151610267.9453327699597; Fri, 21 Oct 2022 03:12:31 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olp0u-00074g-SE for importer@patchew.org; Fri, 21 Oct 2022 06:12:29 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo3K-0005s4-7E for importer@patchew.org; Fri, 21 Oct 2022 05:10:54 -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 1olo2e-00044g-2F for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:12 -0400 Received: from mout.kundenserver.de ([212.227.17.24]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2N-0001xu-Mu for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:11 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MiZof-1pHaXg30Zr-00ffBX; Fri, 21 Oct 2022 11:09:43 +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?= , Stefano Brivio Subject: [PATCH v14 12/17] net: dgram: add unix socket Date: Fri, 21 Oct 2022 11:09:17 +0200 Message-Id: <20221021090922.170074-13-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:plAtbOq7N1vqh85nD7nLk6P7PLf2d34vKgInHBtGPREI/Tar53b 0MN6/T5us3//M++U/mCcZ29HpvkdndwotX17ipWj9nGm7gZvuKVzX27IHeeJ26FnSFWpTXt PrWcriYhtMuU4jmr5rpJ6RIxnRmzUlN23SSZ9izZtjNnuxgJdwUZAAszeEJ2j1wAEMBMIM9 0hFOwrJtw5VUlWGUDx1Mg== X-UI-Out-Filterresults: notjunk:1;V03:K0:MBe1IeTIDoU=:LB4a1GVaMy39FnOeTHMlas BBckVBiRGEM1soJUdAGmoQHyHnhBw6meiupphkkHYmOI/+sfL0Jwe3MGmhCiqiIz6yweXKNAr PC23pfzgIozdb8vdV8H5G24Mi3ChQKFsRmo4FYoAwi85pUzvx/epwMNw4v7sZbKl/6yvVzG0b eGZsvtaxXge+rac9/tBcTRFXYmZd+fz1t8loGkXepcdRd8iNSnGA+XB35dB5hiZOoUbR3PnkK GyQTtDpmWiQ0CXoHyYFg0nLLK5srM5QyNW19a4huYwe52KrfvmqiIDKaf/SMw1dTnjXvpRc3H JiH6Nmue8dCQ9HxlqBCzTrD1tqFSZEMV9vJpecNbDPDre80XmK+9HB+Of1b8JU34SGk1K/4KS aTrxF8xfU+6xJ9QNysMLy7aKm5c+Z3pekc/z2MulDI88QKBF0jBJJllyHimlDQRql8dMhmheZ o+/DZpObwPOHnKodueLuAUYMu8oFmIyBAoI7k+Bfod7NX/ry0/gRU6APVXQhRu1WhXC2JEv3k eTB+KR4lXH/IRLAV2gsVAKQfSiWkfU2VfaFVcIKUum24zeO2rkc1yuLHLCAxzVasEZyFnRyxC 3pCwABY+gAvyvC5XhA1daYkFGsXGJME3YtabfB4E+wmItG5cs0xTaCM1xluYeHzHhSbHoBIHl 4rIr2EfLjbQE4Kc0j7ES9Lyx3odwja0X8LyOwnK/GeG0ohekZXR9avGsXmn81/hXsyeIP50IC n5TuOspfnRuUFMGKnMmYmAbgh6F844AZAut2Vplp2I/hGvX1rdjR0yn3g3EhWpsDzc6PGqFVN qvUR39p 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=212.227.17.24; 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, 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: 1666347153765100003 Content-Type: text/plain; charset="utf-8" Signed-off-by: Laurent Vivier Reviewed-by: Stefano Brivio Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin Acked-by: Markus Armbruster (QAPI schema) --- net/dgram.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- qapi/net.json | 2 +- qemu-options.hx | 1 + 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/net/dgram.c b/net/dgram.c index e581cc62f39f..9f7bf3837653 100644 --- a/net/dgram.c +++ b/net/dgram.c @@ -426,6 +426,7 @@ int net_init_dgram(const Netdev *netdev, const char *na= me, SocketAddress *remote, *local; struct sockaddr *dest_addr; struct sockaddr_in laddr_in, raddr_in; + struct sockaddr_un laddr_un, raddr_un; socklen_t dest_len; =20 assert(netdev->type =3D=3D NET_CLIENT_DRIVER_DGRAM); @@ -465,7 +466,8 @@ int net_init_dgram(const Netdev *netdev, const char *na= me, } } else { if (local->type !=3D SOCKET_ADDRESS_TYPE_FD) { - error_setg(errp, "type=3Dinet requires remote parameter"); + error_setg(errp, + "type=3Dinet or type=3Dunix requires remote paramet= er"); return -1; } } @@ -508,6 +510,53 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, dest_addr =3D g_malloc(dest_len); memcpy(dest_addr, &raddr_in, dest_len); break; + case SOCKET_ADDRESS_TYPE_UNIX: + ret =3D unlink(local->u.q_unix.path); + if (ret < 0 && errno !=3D ENOENT) { + error_setg_errno(errp, errno, "failed to unlink socket %s", + local->u.q_unix.path); + return -1; + } + + laddr_un.sun_family =3D PF_UNIX; + ret =3D snprintf(laddr_un.sun_path, sizeof(laddr_un.sun_path), "%s= ", + local->u.q_unix.path); + if (ret < 0 || ret >=3D sizeof(laddr_un.sun_path)) { + error_setg(errp, "UNIX socket path '%s' is too long", + local->u.q_unix.path); + error_append_hint(errp, "Path must be less than %zu bytes\n", + sizeof(laddr_un.sun_path)); + } + + raddr_un.sun_family =3D PF_UNIX; + ret =3D snprintf(raddr_un.sun_path, sizeof(raddr_un.sun_path), "%s= ", + remote->u.q_unix.path); + if (ret < 0 || ret >=3D sizeof(raddr_un.sun_path)) { + error_setg(errp, "UNIX socket path '%s' is too long", + remote->u.q_unix.path); + error_append_hint(errp, "Path must be less than %zu bytes\n", + sizeof(raddr_un.sun_path)); + } + + fd =3D qemu_socket(PF_UNIX, SOCK_DGRAM, 0); + if (fd < 0) { + error_setg_errno(errp, errno, "can't create datagram socket"); + return -1; + } + + ret =3D bind(fd, (struct sockaddr *)&laddr_un, sizeof(laddr_un)); + if (ret < 0) { + error_setg_errno(errp, errno, "can't bind unix=3D%s to socket", + laddr_un.sun_path); + closesocket(fd); + return -1; + } + qemu_socket_set_nonblock(fd); + + dest_len =3D sizeof(raddr_un); + dest_addr =3D g_malloc(dest_len); + memcpy(dest_addr, &raddr_un, dest_len); + break; case SOCKET_ADDRESS_TYPE_FD: fd =3D monitor_fd_param(monitor_cur(), local->u.fd.str, errp); if (fd =3D=3D -1) { @@ -546,6 +595,10 @@ int net_init_dgram(const Netdev *netdev, const char *n= ame, inet_ntoa(raddr_in.sin_addr), ntohs(raddr_in.sin_port)); break; + case SOCKET_ADDRESS_TYPE_UNIX: + qemu_set_info_str(&s->nc, "udp=3D%s:%s", + laddr_un.sun_path, raddr_un.sun_path); + break; case SOCKET_ADDRESS_TYPE_FD: { SocketAddress *sa; SocketAddressType sa_type; diff --git a/qapi/net.json b/qapi/net.json index aed4ce1a97ff..39388b1b6c41 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -600,7 +600,7 @@ # @remote: remote address # @local: local address # -# Only SocketAddress types 'inet' and 'fd' are supported. +# Only SocketAddress types 'unix', 'inet' and 'fd' are supported. # # If remote address is present and it's a multicast address, local address # is optional. Otherwise local address is required and remote address is diff --git a/qemu-options.hx b/qemu-options.hx index 98e2595df93b..fafb214cb89f 100644 --- a/qemu-options.hx +++ b/qemu-options.hx @@ -2782,6 +2782,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " configure a network backend to connect to a multicast= maddr and port\n" " use ``local.host=3Daddr`` to specify the host address= to send packets from\n" "-netdev dgram,id=3Dstr,local.type=3Dinet,local.host=3Daddr,local.port= =3Dport[,remote.type=3Dinet,remote.host=3Daddr,remote.port=3Dport]\n" + "-netdev dgram,id=3Dstr,local.type=3Dunix,local.path=3Dpath[,remote.ty= pe=3Dunix,remote.path=3Dpath]\n" "-netdev dgram,id=3Dstr,local.type=3Dfd,local.str=3Dfile-descriptor\n" " configure a network backend to connect to another net= work\n" " using an UDP tunnel\n" --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 16663457366184.132341636155843; Fri, 21 Oct 2022 02:48:56 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloe5-00005n-Nt for importer@patchew.org; Fri, 21 Oct 2022 05:48:54 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2i-0004HF-1V for importer@patchew.org; Fri, 21 Oct 2022 05:10:16 -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 1olo2Q-0003UR-TN for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:59 -0400 Received: from mout.kundenserver.de ([212.227.17.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2N-0001uG-CS for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:09:58 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1N1PPJ-1pEAl80De7-012t23; Fri, 21 Oct 2022 11:09:45 +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 13/17] qemu-sockets: move and rename SocketAddress_to_str() Date: Fri, 21 Oct 2022 11:09:18 +0200 Message-Id: <20221021090922.170074-14-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:SGPHMmhVZPB1uoH0LDFHdWcUM+GySHvjZxMiffkQ7slHZxfXZXv Kvhz8CVVZTwbaVvFTHUrSjL5viL3UhWsPcQIjdsFcSQ7rHx5z7it1iiQO3LN/5z7Pn0Kd8G cpoDSMI1/ePp0lU93Zna7iBljpdDgpF9O1iJqwYrLUZPlHe9fUXiptvek29gZZwmy/hDxdX Zkc4O74vEqsGG7WV5WL/w== X-UI-Out-Filterresults: notjunk:1;V03:K0:urGg64dbi7w=:2Dx/2yFQV+NL74Kry8YAl1 Z/KESyqz19PveuclRrBliY8Cy/Fpjih0XjGTxXtJ52GCeaZ/Ke1kcxIkc9asSiPyNbuhLrK3s uljGH3C5SHYZK48NQnv+UZVkfM4TVQWkNtgyROIf9FxpGxe2OSB8fVnF96wUDmSCw0vUwHIlG xYrgMulHnFv8iBJl0zPneHckQ+XpJuSUqcVVyJshtbeSwTjwAHZxu4fVylFxsuSYihBVKCqim AZNoxoy1nrsy9pOcM+puw8mGQcoBilUGpIugoG37tSBiSIQDHJP42qMNAMbmq3fTHuFMoNzpu +U7iCw0JjCBMHbdXA1GJI8wPu65/uGBiOIhtF18fMnN6C2io5XsVHHS705T6NRqwfhHuBVtSh W+uViBYVTJ862NIsvAUFajuIRO4v+3LF+VYaPn4hIsUy/8sG1rvvTVGvwMeJFItZ6jokEECfu 8j3ksaTDnxeIUDAmHAGWaUPbBDbFggJGoRTpsvONPfSwrrBz8mNOkiDBoHxm35NYEIybx0B4R qn1ffa1qyTWD9TeI798CVSdQpaM8nYv1//HnE/XK8ldI/C1m37bsgecMg/Vkw+7SY/ROkRfVX Vs4rNu+tN99wOQDE6Yh8UG8zfFgZDN7VSTERky6T9Kv5lzLPGpLP2wGKic+N4kmAmo34razG+ esvLYWdHDL8KKWdyLeOuzi7BQ/ofVENXijFdLJTx94b2crTHBL9GrCqOJw8f/wJ8+OB9lM6Sq 65kQVyZmovBsBafS6x1+0gzrQivoc9hV5hDex3AV9t6XsOxoa+39teDWdPImDGpmx5IyimLVd OamzIaS 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=212.227.17.10; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666345738179100001 Content-Type: text/plain; charset="utf-8" Rename SocketAddress_to_str() to socket_uri() and move it to util/qemu-sockets.c close to socket_parse(). socket_uri() generates a string from a SocketAddress while socket_parse() generates a SocketAddress from a string. Signed-off-by: Laurent Vivier Reviewed-by: David Gibson Reviewed-by: Dr. David Alan Gilbert Acked-by: Michael S. Tsirkin Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- include/qemu/sockets.h | 2 +- monitor/hmp-cmds.c | 23 +---------------------- util/qemu-sockets.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/qemu/sockets.h b/include/qemu/sockets.h index db4bedb6fa20..214058d8e307 100644 --- a/include/qemu/sockets.h +++ b/include/qemu/sockets.h @@ -58,6 +58,7 @@ NetworkAddressFamily inet_netfamily(int family); int unix_listen(const char *path, Error **errp); int unix_connect(const char *path, Error **errp); =20 +char *socket_uri(SocketAddress *addr); SocketAddress *socket_parse(const char *str, Error **errp); int socket_connect(SocketAddress *addr, Error **errp); int socket_listen(SocketAddress *addr, int num, Error **errp); @@ -141,5 +142,4 @@ SocketAddress *socket_address_flatten(SocketAddressLega= cy *addr); * Return 0 on success. */ int socket_address_parse_named_fd(SocketAddress *addr, Error **errp); - #endif /* QEMU_SOCKETS_H */ diff --git a/monitor/hmp-cmds.c b/monitor/hmp-cmds.c index bab86c5537e1..01b789a79e62 100644 --- a/monitor/hmp-cmds.c +++ b/monitor/hmp-cmds.c @@ -199,27 +199,6 @@ void hmp_info_mice(Monitor *mon, const QDict *qdict) qapi_free_MouseInfoList(mice_list); } =20 -static char *SocketAddress_to_str(SocketAddress *addr) -{ - switch (addr->type) { - case SOCKET_ADDRESS_TYPE_INET: - return g_strdup_printf("tcp:%s:%s", - addr->u.inet.host, - addr->u.inet.port); - case SOCKET_ADDRESS_TYPE_UNIX: - return g_strdup_printf("unix:%s", - addr->u.q_unix.path); - case SOCKET_ADDRESS_TYPE_FD: - return g_strdup_printf("fd:%s", addr->u.fd.str); - case SOCKET_ADDRESS_TYPE_VSOCK: - return g_strdup_printf("tcp:%s:%s", - addr->u.vsock.cid, - addr->u.vsock.port); - default: - return g_strdup("unknown address type"); - } -} - void hmp_info_migrate(Monitor *mon, const QDict *qdict) { MigrationInfo *info; @@ -382,7 +361,7 @@ void hmp_info_migrate(Monitor *mon, const QDict *qdict) monitor_printf(mon, "socket address: [\n"); =20 for (addr =3D info->socket_address; addr; addr =3D addr->next) { - char *s =3D SocketAddress_to_str(addr->value); + char *s =3D socket_uri(addr->value); monitor_printf(mon, "\t%s\n", s); g_free(s); } diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 83f4bd6fd211..9f6f655fd526 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -1077,6 +1077,26 @@ int unix_connect(const char *path, Error **errp) return sock; } =20 +char *socket_uri(SocketAddress *addr) +{ + switch (addr->type) { + case SOCKET_ADDRESS_TYPE_INET: + return g_strdup_printf("tcp:%s:%s", + addr->u.inet.host, + addr->u.inet.port); + case SOCKET_ADDRESS_TYPE_UNIX: + return g_strdup_printf("unix:%s", + addr->u.q_unix.path); + case SOCKET_ADDRESS_TYPE_FD: + return g_strdup_printf("fd:%s", addr->u.fd.str); + case SOCKET_ADDRESS_TYPE_VSOCK: + return g_strdup_printf("tcp:%s:%s", + addr->u.vsock.cid, + addr->u.vsock.port); + default: + return g_strdup("unknown address type"); + } +} =20 SocketAddress *socket_parse(const char *str, Error **errp) { --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666345425020810.357476006714; Fri, 21 Oct 2022 02:43:45 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloZ5-0001EE-Gf for importer@patchew.org; Fri, 21 Oct 2022 05:43:43 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2o-00050g-IG for importer@patchew.org; Fri, 21 Oct 2022 05:10:22 -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 1olo2V-0003fi-7F for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:05 -0400 Received: from mout.kundenserver.de ([212.227.17.13]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2N-0001xr-JG for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:00 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MpUlO-1pSptO1ifc-00psqq; Fri, 21 Oct 2022 11:09:46 +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 14/17] qemu-sockets: update socket_uri() and socket_parse() to be consistent Date: Fri, 21 Oct 2022 11:09:19 +0200 Message-Id: <20221021090922.170074-15-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:IlbjGyBRLP3bwFE38/66E2QOC+4jmzRhUTvPDJprnXGju0Xs3m/ ZSn7mUgHb33GOOso/Sy9gfu9GrxaemOiliAfeQM4Hs08f10PsWEdCQ8nUuWpE0lP2ECUvMd cDF5ZT5mbXqk6sE1ySNAKptZVSCcX3RlA0lbb84Jo8TesMytwMEVvKCWZ6QebVU/GwFWntS cs9ndA9jEbpDip2iqTX8w== X-UI-Out-Filterresults: notjunk:1;V03:K0:PUEEo2WJJ2I=:SeIJ3r7pHTb3tnyE+jhkOY d0HwTUPIyYxf67h2zDeW9WRPXuGwOW8U+J8g5kn9xeIKJc9JZMT8RBn0QTy+cyhBQl1hMuKAe fNfzmHKMmKkjUMNtcBBNyY4u6L63+eqeX2wRvB7myBffQAugV+BDP6cVfHG6yaYAOvvA+syPa GT1gmGM5TwPSEXJJn+e0DIQfYl60/EIQn/t8FayptZNUKHKQYunozXtjPFMay3rh+dmiVgjND Hi/z6c5dkPhAcjf9hjyIn0xPWO3Mn30l7iiCfpnUOwfBrhZpHBurFaKroMNMs2BpkZCcosgWH toXAYfAm0CIcChqjabeAvglqJzVud2as6OC+Xn0LRXZn48d401HIp1g8uU7dzrus+WB1KvEtt vVCENJi+gi35fvRw8X5NTsJzCTHIravfVEOOjCfXJK6Bd2hkbM+qmG4Ni5yAL79dlII02XDJb r5mT9fpKjBVBu0gm24uDMrmNtWRsKJ2s1vd61WDwd2kU7/GPVp/Ea5PLwrYp0ADNzPoa3Lg/X nN4iJfM5x6DPbHSA6yfj/QAhG42zEITJ+FXqdzfXCpaEk8bq+125aK9dlU3Xz1lWcgt7DX7Ob 2jAktwDP787HjfpbKJoZDaqrdRiV1I7nFg1Oc3+XohqYS2FAnrAL0k2OgDsiHGJMnmQgo3wcw MhwvjcWnag/soxMczzfnbV5+a9wuENB/D3rJ8Qhzqy/2+vM2bQBq/dIWSuenTwOchIzvwfP8I SC1VHL1LnWQ0ExG1WbIwePtRSxC0dA/RsaW5HPyaEqJ/fJXJ71yN4kpf8iccEFUfGTU0n5m5g cagRz2Q 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=212.227.17.13; 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZM-MESSAGEID: 1666345425525100001 Content-Type: text/plain; charset="utf-8" To be consistent with socket_uri(), add 'tcp:' prefix for inet type in socket_parse(), by default socket_parse() use tcp when no prefix is provided (format is host:port). In socket_uri(), use 'vsock:' prefix for vsock type rather than 'tcp:' because it makes a vsock address look like an inet address with CID misinterpreted as host. Goes back to commit 9aca82ba31 "migration: Create socket-address parameter" Signed-off-by: Laurent Vivier Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Markus Armbruster Reviewed-by: David Gibson Acked-by: Michael S. Tsirkin --- util/qemu-sockets.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 9f6f655fd526..a9926af714c4 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -1090,7 +1090,7 @@ char *socket_uri(SocketAddress *addr) case SOCKET_ADDRESS_TYPE_FD: return g_strdup_printf("fd:%s", addr->u.fd.str); case SOCKET_ADDRESS_TYPE_VSOCK: - return g_strdup_printf("tcp:%s:%s", + return g_strdup_printf("vsock:%s:%s", addr->u.vsock.cid, addr->u.vsock.port); default: @@ -1124,6 +1124,11 @@ SocketAddress *socket_parse(const char *str, Error *= *errp) if (vsock_parse(&addr->u.vsock, str + strlen("vsock:"), errp)) { goto fail; } + } else if (strstart(str, "tcp:", NULL)) { + addr->type =3D SOCKET_ADDRESS_TYPE_INET; + if (inet_parse(&addr->u.inet, str + strlen("tcp:"), errp)) { + goto fail; + } } else { addr->type =3D SOCKET_ADDRESS_TYPE_INET; if (inet_parse(&addr->u.inet, str, errp)) { --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 From nobody Thu Nov 14 18:13:17 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 1666348512507445.6334392984419; Fri, 21 Oct 2022 03:35:12 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olpMs-0007R1-9N for importer@patchew.org; Fri, 21 Oct 2022 06:35:10 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo3J-0005jc-3C for importer@patchew.org; Fri, 21 Oct 2022 05:10:53 -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 1olo2j-0004eC-Qh for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:18 -0400 Received: from mout.kundenserver.de ([212.227.17.10]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2S-00023C-Tp for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:17 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1Mn1mb-1pUjhy2Rkd-00k79z; Fri, 21 Oct 2022 11:09:50 +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 16/17] tests/qtest: netdev: test stream and dgram backends Date: Fri, 21 Oct 2022 11:09:21 +0200 Message-Id: <20221021090922.170074-17-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:CxcRoKY6c8c99dv+UPGTV3ROz1rs1O37uiVFETGeabLzE8wDu4N srZFopyGGK3oExWmCMR6kTd0cDl7KXL3ZERhAP7wIhJ2QmHv9Py67pzArEOs6EnDYM5Gqts hJw6jxbNWHjeuil20WbikCeD4C5kNazhXmIrUSnSPSyE3H0a9uWIappyojNFqHYDsJEvzt4 DH5LTWYPHXec18BG1ue0w== X-UI-Out-Filterresults: notjunk:1;V03:K0:SRsdiqASwZw=:rkqVSVoamyy/aOi9wtGIY5 TuCcfWMpRIlWbgyRTyGrjxk1XQozGWJ8Z72hyZXHyTXQYixZ8U7rZ6XXy6ansxjfBugpYZii8 Hq3mU5pAz1yL8QkfiLlFydFy+vPuV5aIVFZywf7zdaCqVIDO0ISWD4WbL8scLwKdYnLg/grrj pUr5osE+nBd2qD6+pWt4iYaARXrIJS4kJXAooyWEDQZuePzAJuBQdnbuf+9sgiOFUIXt2DnKX Sa5bz0zZzXf+OJyQmw3KnBniCbWi1Q+b5hXtKi/ow0A0I8/vMEL4o8XgPxED1V2C4OJ2z54YH y12HYp8DTXAV2dsvx59uQOJ0gO2ePt/fm5h8vMLm3qF8JImm5XbSHzYactrggRjCY+JdEfrIw PZGtDP8LoHNo8Ee1QlF+i6NvG/uaGP471UnI+Eve1EH8cGA3CdHzY8OzACyrPK95SYHyT+Osg PNSLES2fWLej5Fjl4nVduikMSnXwxTctEPwh16jYxQ9Ltrq6lVLPTQMg+yLrZ/BpBf+YumoqC dIhHkSLQLIjAtA3Yk+jWOPQkDEnqQ9ukbnm/9sZF3cQbJM/R7zLoBLuX+w8UeT0+jIFoYKJ7Z gwZ54cyYdGEklnpcbj5TVHzidAF7U3/9X8HQcGnkrS05JJBApvOXwDuWEqfFDeeoyGFweybQY ThZHoslcNP8Ll31q/VFlUI5BFvZg3hVEmuQrtpdoGPVwqTg7WqAd2+HT0P8NSAv1fRnAEIzqC oM+v31kuuoPbMasIW8SiOBoRMv1YO23/uUiWIEVI80NsqxJhqHRfnPAHUjzmfE7jYuru3cZdm BIiRuHx 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=212.227.17.10; 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, 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: 1666348514125100001 Content-Type: text/plain; charset="utf-8" Signed-off-by: Laurent Vivier Acked-by: Michael S. Tsirkin --- tests/qtest/meson.build | 1 + tests/qtest/netdev-socket.c | 420 ++++++++++++++++++++++++++++++++++++ 2 files changed, 421 insertions(+) create mode 100644 tests/qtest/netdev-socket.c diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build index c07a5b1a5f43..6953797e4e3e 100644 --- a/tests/qtest/meson.build +++ b/tests/qtest/meson.build @@ -27,6 +27,7 @@ qtests_generic =3D [ 'test-hmp', 'qos-test', 'readconfig-test', + 'netdev-socket', ] if config_host.has_key('CONFIG_MODULES') qtests_generic +=3D [ 'modules-test' ] diff --git a/tests/qtest/netdev-socket.c b/tests/qtest/netdev-socket.c new file mode 100644 index 000000000000..b24c0819b9ac --- /dev/null +++ b/tests/qtest/netdev-socket.c @@ -0,0 +1,420 @@ +/* + * QTest testcase for netdev stream and dgram + * + * Copyright (c) 2022 Red Hat, Inc. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "libqtest.h" + +#define CONNECTION_TIMEOUT 5 + +#define EXPECT_STATE(q, e, t) \ +do { \ + char *resp =3D qtest_hmp(q, "info network"); \ + if (t) { \ + strrchr(resp, t)[0] =3D 0; \ + } \ + g_test_timer_start(); \ + while (g_test_timer_elapsed() < CONNECTION_TIMEOUT) { \ + if (strcmp(resp, e) =3D=3D 0) { \ + break; \ + } \ + g_free(resp); \ + resp =3D qtest_hmp(q, "info network"); \ + if (t) { \ + strrchr(resp, t)[0] =3D 0; \ + } \ + } \ + g_assert_cmpstr(resp, =3D=3D, e); \ + g_free(resp); \ +} while (0) + +static int inet_get_free_port_socket(int sock) +{ + struct sockaddr_in addr; + socklen_t len; + + memset(&addr, 0, sizeof(addr)); + addr.sin_family =3D AF_INET; + addr.sin_addr.s_addr =3D INADDR_ANY; + addr.sin_port =3D 0; + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { + return -1; + } + + len =3D sizeof(addr); + if (getsockname(sock, (struct sockaddr *)&addr, &len) < 0) { + return -1; + } + + return ntohs(addr.sin_port); +} + +static int inet_get_free_port_multiple(int nb, int *port) +{ + int sock[nb]; + int i; + + for (i =3D 0; i < nb; i++) { + sock[i] =3D socket(AF_INET, SOCK_STREAM, 0); + if (sock[i] < 0) { + break; + } + port[i] =3D inet_get_free_port_socket(sock[i]); + } + + nb =3D i; + for (i =3D 0; i < nb; i++) { + closesocket(sock[i]); + } + + return nb; +} + +static int inet_get_free_port(void) +{ + int nb, port; + + nb =3D inet_get_free_port_multiple(1, &port); + g_assert_cmpint(nb, =3D=3D, 1); + + return port; +} + +static void test_stream_inet_ipv4(void) +{ + QTestState *qts0, *qts1; + char *expect; + int port; + + port =3D inet_get_free_port(); + qts0 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dtrue,addr.type=3D= inet," + "addr.ipv4=3Don,addr.ipv6=3Doff," + "addr.host=3Dlocalhost,addr.port=3D%d", port); + + EXPECT_STATE(qts0, "st0: index=3D0,type=3Dstream,\r\n", 0); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev stream,server=3Dfalse,id=3Dst0,addr.type= =3Dinet," + "addr.ipv4=3Don,addr.ipv6=3Doff," + "addr.host=3Dlocalhost,addr.port=3D%d", port); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Dstream,tcp:127.0.0.1= :%d\r\n", + port); + EXPECT_STATE(qts1, expect, 0); + g_free(expect); + + /* the port is unknown, check only the address */ + EXPECT_STATE(qts0, "st0: index=3D0,type=3Dstream,tcp:127.0.0.1", ':'); + + qtest_quit(qts1); + qtest_quit(qts0); +} + +static void test_stream_inet_ipv6(void) +{ + QTestState *qts0, *qts1; + char *expect; + int port; + + port =3D inet_get_free_port(); + qts0 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dtrue,addr.type=3D= inet," + "addr.ipv4=3Doff,addr.ipv6=3Don," + "addr.host=3Dlocalhost,addr.port=3D%d", port); + + EXPECT_STATE(qts0, "st0: index=3D0,type=3Dstream,\r\n", 0); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev stream,server=3Dfalse,id=3Dst0,addr.type= =3Dinet," + "addr.ipv4=3Doff,addr.ipv6=3Don," + "addr.host=3Dlocalhost,addr.port=3D%d", port); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Dstream,tcp:::1:%d\r\= n", + port); + EXPECT_STATE(qts1, expect, 0); + g_free(expect); + + /* the port is unknown, check only the address */ + EXPECT_STATE(qts0, "st0: index=3D0,type=3Dstream,tcp:::1", ':'); + + qtest_quit(qts1); + qtest_quit(qts0); +} + +static void test_stream_unix(void) +{ + QTestState *qts0, *qts1; + char *expect; + gchar *path; + int ret; + + ret =3D g_file_open_tmp("netdev-XXXXXX", &path, NULL); + g_assert_true(ret >=3D 0); + close(ret); + + qts0 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dtrue," + "addr.type=3Dunix,addr.path=3D%s,", + path); + + EXPECT_STATE(qts0, "st0: index=3D0,type=3Dstream,\r\n", 0); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dfalse," + "addr.type=3Dunix,addr.path=3D%s", + path); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Dstream,unix:%s\r\n",= path); + EXPECT_STATE(qts1, expect, 0); + EXPECT_STATE(qts0, expect, 0); + g_free(expect); + unlink(path); + g_free(path); + + qtest_quit(qts1); + qtest_quit(qts0); +} + +static void test_stream_unix_abstract(void) +{ + QTestState *qts0, *qts1; + char *expect; + gchar *path; + int ret; + + ret =3D g_file_open_tmp("netdev-XXXXXX", &path, NULL); + g_assert_true(ret >=3D 0); + close(ret); + + qts0 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dtrue," + "addr.type=3Dunix,addr.path=3D%s," + "addr.abstract=3Don", + path); + + EXPECT_STATE(qts0, "st0: index=3D0,type=3Dstream,\r\n", 0); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dfalse," + "addr.type=3Dunix,addr.path=3D%s,addr.abstract=3Don= ", + path); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Dstream,unix:%s\r\n",= path); + EXPECT_STATE(qts1, expect, 0); + EXPECT_STATE(qts0, expect, 0); + g_free(expect); + unlink(path); + g_free(path); + + qtest_quit(qts1); + qtest_quit(qts0); +} + +static void test_stream_fd(void) +{ + QTestState *qts0, *qts1; + char *expect; + int ret, sock0, sock1; + struct sockaddr_un addr; + gchar *path; + + ret =3D g_file_open_tmp("netdev-XXXXXX", &path, NULL); + g_assert_true(ret >=3D 0); + close(ret); + addr.sun_family =3D AF_UNIX; + strcpy(addr.sun_path, path); + + unlink(addr.sun_path); + sock0 =3D socket(AF_LOCAL, SOCK_STREAM, 0); + g_assert_cmpint(sock0, !=3D, -1); + + ret =3D bind(sock0, (struct sockaddr *)&addr, sizeof(addr)); + g_assert_cmpint(ret, !=3D, -1); + + qts0 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dtrue," + "addr.type=3Dfd,addr.str=3D%d", + sock0); + + EXPECT_STATE(qts0, "st0: index=3D0,type=3Dstream,\r\n", 0); + + sock1 =3D socket(AF_LOCAL, SOCK_STREAM, 0); + g_assert_cmpint(sock1, !=3D, -1); + + ret =3D connect(sock1, (struct sockaddr *)&addr, sizeof(addr)); + g_assert_cmpint(ret, !=3D, -1); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev stream,id=3Dst0,server=3Dfalse,addr.type= =3Dfd,addr.str=3D%d", + sock1); + + + expect =3D g_strdup_printf("st0: index=3D0,type=3Dstream,unix:%s\r\n",= path); + EXPECT_STATE(qts1, expect, 0); + EXPECT_STATE(qts0, expect, 0); + g_free(expect); + + qtest_quit(qts1); + qtest_quit(qts0); + + closesocket(sock0); + closesocket(sock1); + + g_free(path); +} + +static void test_dgram_inet(void) +{ + QTestState *qts0, *qts1; + char *expect; + int port[2]; + int nb; + + nb =3D inet_get_free_port_multiple(2, port); + g_assert_cmpint(nb, =3D=3D, 2); + + qts0 =3D qtest_initf("-nodefaults " + "-netdev dgram,id=3Dst0," + "local.type=3Dinet,local.host=3Dlocalhost,local.por= t=3D%d," + "remote.type=3Dinet,remote.host=3Dlocalhost,remote.= port=3D%d", + port[0], port[1]); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Ddgram," + "udp=3D127.0.0.1:%d/127.0.0.1:%d\r\n", + port[0], port[1]); + EXPECT_STATE(qts0, expect, 0); + g_free(expect); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev dgram,id=3Dst0," + "local.type=3Dinet,local.host=3Dlocalhost,local.por= t=3D%d," + "remote.type=3Dinet,remote.host=3Dlocalhost,remote.= port=3D%d", + port[1], port[0]); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Ddgram," + "udp=3D127.0.0.1:%d/127.0.0.1:%d\r\n", + port[1], port[0]); + EXPECT_STATE(qts1, expect, 0); + g_free(expect); + + qtest_quit(qts1); + qtest_quit(qts0); +} + +static void test_dgram_mcast(void) +{ + QTestState *qts; + + qts =3D qtest_initf("-nodefaults " + "-netdev dgram,id=3Dst0," + "remote.type=3Dinet,remote.host=3D230.0.0.1,remote.= port=3D1234"); + + EXPECT_STATE(qts, "st0: index=3D0,type=3Ddgram,mcast=3D230.0.0.1:1234\= r\n", 0); + + qtest_quit(qts); +} + +static void test_dgram_unix(void) +{ + QTestState *qts0, *qts1; + char *expect; + gchar *path0, *path1; + int ret; + + ret =3D g_file_open_tmp("netdev-XXXXXX", &path0, NULL); + g_assert_true(ret >=3D 0); + close(ret); + + ret =3D g_file_open_tmp("netdev-XXXXXX", &path1, NULL); + g_assert_true(ret >=3D 0); + close(ret); + + qts0 =3D qtest_initf("-nodefaults " + "-netdev dgram,id=3Dst0,local.type=3Dunix,local.pat= h=3D%s," + "remote.type=3Dunix,remote.path=3D%s", + path0, path1); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Ddgram,udp=3D%s:%s\r\= n", + path0, path1); + EXPECT_STATE(qts0, expect, 0); + g_free(expect); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev dgram,id=3Dst0,local.type=3Dunix,local.pat= h=3D%s," + "remote.type=3Dunix,remote.path=3D%s", + path1, path0); + + + expect =3D g_strdup_printf("st0: index=3D0,type=3Ddgram,udp=3D%s:%s\r\= n", + path1, path0); + EXPECT_STATE(qts1, expect, 0); + g_free(expect); + + unlink(path0); + g_free(path0); + unlink(path1); + g_free(path1); + + qtest_quit(qts1); + qtest_quit(qts0); +} + +static void test_dgram_fd(void) +{ + QTestState *qts0, *qts1; + char *expect; + int ret; + int sv[2]; + + ret =3D socketpair(PF_UNIX, SOCK_DGRAM, 0, sv); + g_assert_cmpint(ret, !=3D, -1); + + qts0 =3D qtest_initf("-nodefaults " + "-netdev dgram,id=3Dst0,local.type=3Dfd,local.str= =3D%d", + sv[0]); + + expect =3D g_strdup_printf("st0: index=3D0,type=3Ddgram,fd=3D%d unix\r= \n", sv[0]); + EXPECT_STATE(qts0, expect, 0); + g_free(expect); + + qts1 =3D qtest_initf("-nodefaults " + "-netdev dgram,id=3Dst0,local.type=3Dfd,local.str= =3D%d", + sv[1]); + + + expect =3D g_strdup_printf("st0: index=3D0,type=3Ddgram,fd=3D%d unix\r= \n", sv[1]); + EXPECT_STATE(qts1, expect, 0); + g_free(expect); + + qtest_quit(qts1); + qtest_quit(qts0); + + closesocket(sv[0]); + closesocket(sv[1]); +} + +int main(int argc, char **argv) +{ + int ret; + + g_test_init(&argc, &argv, NULL); + + qtest_add_func("/netdev/stream/inet/ipv4", test_stream_inet_ipv4); + qtest_add_func("/netdev/stream/inet/ipv6", test_stream_inet_ipv6); + qtest_add_func("/netdev/stream/unix", test_stream_unix); + qtest_add_func("/netdev/stream/unix/abstract", test_stream_unix_abstra= ct); + qtest_add_func("/netdev/stream/fd", test_stream_fd); + qtest_add_func("/netdev/dgram/inet", test_dgram_inet); + qtest_add_func("/netdev/dgram/mcast", test_dgram_mcast); + qtest_add_func("/netdev/dgram/unix", test_dgram_unix); + qtest_add_func("/netdev/dgram/fd", test_dgram_fd); + + ret =3D g_test_run(); + + return ret; +} --=20 2.37.3 From nobody Thu Nov 14 18:13:17 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 1666345480919548.2199369617396; Fri, 21 Oct 2022 02:44:40 -0700 (PDT) Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oloZz-0004OV-BM for importer@patchew.org; Fri, 21 Oct 2022 05:44:39 -0400 Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olo2p-00052p-ND for importer@patchew.org; Fri, 21 Oct 2022 05:10:23 -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 1olo2b-0003tU-5N for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:09 -0400 Received: from mout.kundenserver.de ([217.72.192.74]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olo2V-000254-0x for qemu-devel@nongnu.org; Fri, 21 Oct 2022 05:10:07 -0400 Received: from lenovo-t14s.redhat.com ([82.142.8.70]) by mrelayeu.kundenserver.de (mreue106 [212.227.15.183]) with ESMTPSA (Nemesis) id 1N2VKr-1pFHDk0Xh1-013zUl; Fri, 21 Oct 2022 11:09:51 +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 17/17] net: stream: add QAPI events to report connection state Date: Fri, 21 Oct 2022 11:09:22 +0200 Message-Id: <20221021090922.170074-18-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:3qigyZi+KVz96ySGS6bqVJaYaPxXAMgTyYwclTCtKTSb7A3gsIt SdMTbioFbJo7FqHB+14qedMmyEF7GpvmUNv8gPVARyqLvd32BdEC90dhWYdE+8RSLucnhOf TkRD8Tz10832VhOljwKfSqVSJYrOC2g8Lvaw7lA1g6tcnygpoYyRsuG4TonEL6gEu1y98Yk Ui4PfpZmyHLzuXBbrc3Wg== X-UI-Out-Filterresults: notjunk:1;V03:K0:u2SBYcxc29k=:Z/SFRNQr3jfmeh0w45tVSV vtMdPTAgRWe4h7V3llXabx44mm5VsuCmFpmdi73loD6zH9Q+mqGGkgmOhFI9v1CNxWODjYUn3 UfjXRSwNrbsCXActkVXz6RNxTGtjaJMqexh4Dn+qjpdUIUKVgBslww8jC6iYHXw4zQZYRfrlQ 7ombF82cX2ef/cKES/02n5QVwbr5KaK7rLNjR0EJel0UEvRskLQLiQ5nznxrX35EiCN+I7BHa CYXDkuGzi2eU0viRa/mwuT44neFg1fUfCCJcDJ72b00U/24x8285y4ya6CXyyPe9p32Ub85XT K0LST8UIFl+Ep78xuZ0tibVLYCCFVLTBRxpbCwn+zUYZrR3CL0BcH9j7sPaOqwDHA/GpqmcNT 8PmbtUe72tNOYQPoM6yGfbn2lq7GMx89lebNLgPlki35gOICljAqYBPZXqIKv2vOc98pGbB8t IN29Pari/OtGwyQ2uEgWA3Bac01JpTF4mkun7knlh91EuqCkPpoCvUxpAYvIwrlE0pvdAsfjB hkKCghIg1POfKRIr10I7yW5eVJsQVYgiYniiFC8s5qvyiKwC/IYTRivWmGIQvUeo7QyR67rP0 qbteljVsGy2lSXwn6Uyevhdy4qq1YhSSRmWXaDvBjrs5LKJK5BumKqbDV2m+W7YPMf08O8JJv NXnWaeNeZsPQue6u3ZVBmd8CXEbkzShFXps/YBFfWCedxvteoZHgl7traR7zVEp9cHcAu5Bjx HR6terX2Ega5aoWEvbvVT7Gdrq4dUfICGgx/GrkhfptRslCOIgPx73MetcMgm1TZRHb8j1zXp +tEC/mn 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.74; 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_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=ham 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: 1666345481673100001 Content-Type: text/plain; charset="utf-8" The netdev reports NETDEV_STREAM_CONNECTED event when the backend is connected, and NETDEV_STREAM_DISCONNECTED when it is disconnected. The NETDEV_STREAM_CONNECTED event includes the destination address. This allows a system manager like libvirt to detect when the server fails. For instance with passt: { 'execute': 'qmp_capabilities' } { "return": { } } { "timestamp": { "seconds": 1666341395, "microseconds": 505347 }, "event": "NETDEV_STREAM_CONNECTED", "data": { "netdev-id": "netdev0", "addr": { "path": "/tmp/passt_1.socket", "type": "unix" } } } [killing passt here] { "timestamp": { "seconds": 1666341430, "microseconds": 968694 }, "event": "NETDEV_STREAM_DISCONNECTED", "data": { "netdev-id": "netdev0" } } Signed-off-by: Laurent Vivier Acked-by: Michael S. Tsirkin Acked-by: Markus Armbruster --- net/stream.c | 5 +++++ qapi/net.json | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/net/stream.c b/net/stream.c index 54c67e14d240..53b7040cc417 100644 --- a/net/stream.c +++ b/net/stream.c @@ -38,6 +38,7 @@ #include "io/channel.h" #include "io/channel-socket.h" #include "io/net-listener.h" +#include "qapi/qapi-events-net.h" =20 typedef struct NetStreamState { NetClientState nc; @@ -168,6 +169,8 @@ static gboolean net_stream_send(QIOChannel *ioc, s->nc.link_down =3D true; qemu_set_info_str(&s->nc, ""); =20 + qapi_event_send_netdev_stream_disconnected(s->nc.name); + return G_SOURCE_REMOVE; } buf =3D buf1; @@ -244,6 +247,7 @@ static void net_stream_listen(QIONetListener *listener, uri =3D socket_uri(addr); qemu_set_info_str(&s->nc, uri); g_free(uri); + qapi_event_send_netdev_stream_connected(s->nc.name, addr); qapi_free_SocketAddress(addr); } =20 @@ -335,6 +339,7 @@ static void net_stream_client_connected(QIOTask *task, = gpointer opaque) 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_event_send_netdev_stream_connected(s->nc.name, addr); qapi_free_SocketAddress(addr); =20 return; diff --git a/qapi/net.json b/qapi/net.json index 39388b1b6c41..c37b24717382 100644 --- a/qapi/net.json +++ b/qapi/net.json @@ -895,3 +895,52 @@ ## { 'event': 'FAILOVER_NEGOTIATED', 'data': {'device-id': 'str'} } + +## +# @NETDEV_STREAM_CONNECTED: +# +# Emitted when the netdev stream backend is connected +# +# @netdev-id: QEMU netdev id that is connected +# @addr: The destination address +# +# Since: 7.2 +# +# Example: +# +# <- { "event": "NETDEV_STREAM_CONNECTED", +# "data": { "netdev-id": "netdev0", +# "addr": { "port": "47666", "ipv6": true, +# "host": "::1", "type": "inet" } }, +# "timestamp": { "seconds": 1666269863, "microseconds": 311222 } } +# +# or +# +# <- { "event": "NETDEV_STREAM_CONNECTED", +# "data": { "netdev-id": "netdev0", +# "addr": { "path": "/tmp/qemu0", "type": "unix" } }, +# "timestamp": { "seconds": 1666269706, "microseconds": 413651 } } +# +## +{ 'event': 'NETDEV_STREAM_CONNECTED', + 'data': { 'netdev-id': 'str', + 'addr': 'SocketAddress' } } + +## +# @NETDEV_STREAM_DISCONNECTED: +# +# Emitted when the netdev stream backend is disconnected +# +# @netdev-id: QEMU netdev id that is disconnected +# +# Since: 7.2 +# +# Example: +# +# <- { 'event': 'NETDEV_STREAM_DISCONNECTED', +# 'data': {'netdev-id': 'netdev0'}, +# 'timestamp': {'seconds': 1663330937, 'microseconds': 526695} } +# +## +{ 'event': 'NETDEV_STREAM_DISCONNECTED', + 'data': { 'netdev-id': 'str' } } --=20 2.37.3