From nobody Mon May 13 03:33:35 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=ispras.ru Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1654102263904315.6217900735654; Wed, 1 Jun 2022 09:51:03 -0700 (PDT) Received: from localhost ([::1]:41508 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nwRYk-0004h2-U8 for importer@patchew.org; Wed, 01 Jun 2022 12:51:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:35172) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nwRTZ-0005OJ-PA for qemu-devel@nongnu.org; Wed, 01 Jun 2022 12:45:42 -0400 Received: from mail.ispras.ru ([83.149.199.84]:60642) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nwRTW-0001R9-W7 for qemu-devel@nongnu.org; Wed, 01 Jun 2022 12:45:41 -0400 Received: from localhost.localdomain (unknown [77.37.166.174]) by mail.ispras.ru (Postfix) with ESMTPSA id 1C346407624C; Wed, 1 Jun 2022 16:45:13 +0000 (UTC) From: Vitaly Cheptsov To: qemu-devel@nongnu.org Cc: Vitaly Cheptsov , Jason Wang , "Daniel P . Berrange" , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v3] net: fix multicast support with BSD (macOS) socket implementations Date: Wed, 1 Jun 2022 19:45:07 +0300 Message-Id: <20220601164507.51503-1-cheptsov@ispras.ru> X-Mailer: git-send-email 2.32.1 (Apple Git-133) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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: pass client-ip=83.149.199.84; envelope-from=cheptsov@ispras.ru; helo=mail.ispras.ru 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_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-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: 1654102265288100003 This patch fixes socket communication with QEMU -> host and QEMU <--> QEMU on macOS, which was originally impossible due to QEMU and host program having to bind to the same ip/port in a way not supported by BSD sockets. The change was tested on both Linux and macOS. Basically after applying this patch one will be able to communicate with QEMU when using "-nic socket,mcast=3D230.0.0.1:1234,model=3Dvirtio-net= -pci" from QEMU or macOS itself. Cc: Jason Wang Cc: Daniel P. Berrange Cc: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Vitaly Cheptsov --- net/socket.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/net/socket.c b/net/socket.c index bfd8596250..583f788a22 100644 --- a/net/socket.c +++ b/net/socket.c @@ -239,6 +239,22 @@ static int net_socket_mcast_create(struct sockaddr_in = *mcastaddr, return -1; } =20 +#ifdef __APPLE__ + val =3D 1; + ret =3D qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &val, sizeof(val= )); + if (ret < 0) { + error_setg_errno(errp, errno, + "can't set socket option SO_REUSEPORT"); + goto fail; + } + + struct sockaddr_in bindaddr; + memset(&bindaddr, 0, sizeof(bindaddr)); + bindaddr.sin_family =3D AF_INET; + bindaddr.sin_addr.s_addr =3D htonl(INADDR_ANY); + bindaddr.sin_port =3D mcastaddr->sin_port; + ret =3D bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)); +#else /* 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 @@ -253,6 +269,8 @@ static int net_socket_mcast_create(struct sockaddr_in *= mcastaddr, } =20 ret =3D bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); +#endif + if (ret < 0) { error_setg_errno(errp, errno, "can't bind ip=3D%s to socket", inet_ntoa(mcastaddr->sin_addr)); --=20 2.32.1 (Apple Git-133)