[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v3] net: fix multicast support with BSD (macOS) socket implementa

From: Vitaly Cheptsov
Subject: [PATCH v3] net: fix multicast support with BSD (macOS) socket implementations
Date: Wed, 1 Jun 2022 19:45:07 +0300

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=,model=virtio-net-pci"
from QEMU or macOS itself.

Cc: Jason Wang <jasowang@redhat.com>
Cc: Daniel P. Berrange <berrange@redhat.com>
Cc: Philippe Mathieu-Daudé <f4bug@amsat.org>
Signed-off-by: Vitaly Cheptsov <cheptsov@ispras.ru>
 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 
         return -1;
+#ifdef __APPLE__
+    val = 1;
+    ret = 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 = AF_INET;
+    bindaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+    bindaddr.sin_port = mcastaddr->sin_port;
+    ret = bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr));
     /* Allow multiple sockets to bind the same multicast ip and port by setting
      * SO_REUSEADDR. This is the only situation where SO_REUSEADDR should be 
      * on windows. Use socket_set_fast_reuse otherwise as it sets SO_REUSEADDR
@@ -253,6 +269,8 @@ static int net_socket_mcast_create(struct sockaddr_in 
     ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
     if (ret < 0) {
         error_setg_errno(errp, errno, "can't bind ip=%s to socket",
2.32.1 (Apple Git-133)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]