* [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations
@ 2022-05-18 7:39 Vitaly Cheptsov
2022-05-18 7:51 ` Vitaly Cheptsov
2022-05-31 7:01 ` Jason Wang
0 siblings, 2 replies; 5+ messages in thread
From: Vitaly Cheptsov @ 2022-05-18 7:39 UTC (permalink / raw)
To: qemu-devel
Cc: Vitaly Cheptsov, Jason Wang, Daniel P . Berrange,
Philippe Mathieu-Daudé
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 *mcastaddr,
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));
+#else
/* 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 set
* 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 *mcastaddr,
}
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
+#endif
+
if (ret < 0) {
error_setg_errno(errp, errno, "can't bind ip=%s to socket",
inet_ntoa(mcastaddr->sin_addr));
--
2.32.1 (Apple Git-133)
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations
2022-05-18 7:39 [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations Vitaly Cheptsov
@ 2022-05-18 7:51 ` Vitaly Cheptsov
2022-05-31 7:01 ` Jason Wang
1 sibling, 0 replies; 5+ messages in thread
From: Vitaly Cheptsov @ 2022-05-18 7:51 UTC (permalink / raw)
To: QEMU Developers
Cc: Jason Wang, Daniel P . Berrange, Philippe Mathieu-Daudé
[-- Attachment #1: Type: text/plain, Size: 2018 bytes --]
V2 version of the previous patch ensures to keep compatibility with non-Apple platforms to avoid any potential compatibility issues with e.g. Windows mentioned in the review.
> On 18 May 2022, at 10:39, Vitaly Cheptsov <cheptsov@ispras.ru> wrote:
>
> 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 *mcastaddr,
> 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));
> +#else
> /* 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 set
> * 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 *mcastaddr,
> }
>
> ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
> +#endif
> +
> if (ret < 0) {
> error_setg_errno(errp, errno, "can't bind ip=%s to socket",
> inet_ntoa(mcastaddr->sin_addr));
> --
> 2.32.1 (Apple Git-133)
>
[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations
2022-05-18 7:39 [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations Vitaly Cheptsov
2022-05-18 7:51 ` Vitaly Cheptsov
@ 2022-05-31 7:01 ` Jason Wang
2022-05-31 11:28 ` Vitaly Cheptsov
1 sibling, 1 reply; 5+ messages in thread
From: Jason Wang @ 2022-05-31 7:01 UTC (permalink / raw)
To: Vitaly Cheptsov
Cc: qemu-devel, Daniel P . Berrange, Philippe Mathieu-Daudé
On Wed, May 18, 2022 at 3:40 PM Vitaly Cheptsov <cheptsov@ispras.ru> wrote:
>
Hi Vitaly:
We need a changelog to describe why we need this.
Thanks
> 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 *mcastaddr,
> 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));
> +#else
> /* 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 set
> * 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 *mcastaddr,
> }
>
> ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
> +#endif
> +
> if (ret < 0) {
> error_setg_errno(errp, errno, "can't bind ip=%s to socket",
> inet_ntoa(mcastaddr->sin_addr));
> --
> 2.32.1 (Apple Git-133)
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations
2022-05-31 7:01 ` Jason Wang
@ 2022-05-31 11:28 ` Vitaly Cheptsov
2022-06-01 2:43 ` Jason Wang
0 siblings, 1 reply; 5+ messages in thread
From: Vitaly Cheptsov @ 2022-05-31 11:28 UTC (permalink / raw)
To: Jason Wang; +Cc: qemu-devel, Daniel P . Berrange, Philippe Mathieu-Daudé
Hi Jason,
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=230.0.0.1:1234,model=virtio-net-pci" from QEMU or macOS itself.
Best regards,
Vitaly
> On 31 May 2022, at 10:02, Jason Wang <jasowang@redhat.com> wrote:
>
> On Wed, May 18, 2022 at 3:40 PM Vitaly Cheptsov <cheptsov@ispras.ru> wrote:
>>
>
> Hi Vitaly:
>
> We need a changelog to describe why we need this.
>
> Thanks
>
>> 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 *mcastaddr,
>> 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));
>> +#else
>> /* 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 set
>> * 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 *mcastaddr,
>> }
>>
>> ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
>> +#endif
>> +
>> if (ret < 0) {
>> error_setg_errno(errp, errno, "can't bind ip=%s to socket",
>> inet_ntoa(mcastaddr->sin_addr));
>> --
>> 2.32.1 (Apple Git-133)
>>
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations
2022-05-31 11:28 ` Vitaly Cheptsov
@ 2022-06-01 2:43 ` Jason Wang
0 siblings, 0 replies; 5+ messages in thread
From: Jason Wang @ 2022-06-01 2:43 UTC (permalink / raw)
To: Vitaly Cheptsov
Cc: qemu-devel, Daniel P . Berrange, Philippe Mathieu-Daudé
On Tue, May 31, 2022 at 7:28 PM Vitaly Cheptsov <cheptsov@ispras.ru> wrote:
>
> Hi Jason,
>
> 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=230.0.0.1:1234,model=virtio-net-pci" from QEMU or macOS itself.
>
> Best regards,
> Vitaly
Would you mind resending the patch with the above as the changelog?
Thanks
>
> > On 31 May 2022, at 10:02, Jason Wang <jasowang@redhat.com> wrote:
> >
> > On Wed, May 18, 2022 at 3:40 PM Vitaly Cheptsov <cheptsov@ispras.ru> wrote:
> >>
> >
> > Hi Vitaly:
> >
> > We need a changelog to describe why we need this.
> >
> > Thanks
> >
> >> 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 *mcastaddr,
> >> 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));
> >> +#else
> >> /* 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 set
> >> * 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 *mcastaddr,
> >> }
> >>
> >> ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
> >> +#endif
> >> +
> >> if (ret < 0) {
> >> error_setg_errno(errp, errno, "can't bind ip=%s to socket",
> >> inet_ntoa(mcastaddr->sin_addr));
> >> --
> >> 2.32.1 (Apple Git-133)
> >>
> >
>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-06-01 2:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-05-18 7:39 [PATCH v2] net: fix multicast support with BSD (macOS) socket implementations Vitaly Cheptsov
2022-05-18 7:51 ` Vitaly Cheptsov
2022-05-31 7:01 ` Jason Wang
2022-05-31 11:28 ` Vitaly Cheptsov
2022-06-01 2:43 ` Jason Wang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).