-net socket has a fd argument, and may be passed pre-opened sockets.
TCP sockets use framing.
UDP sockets have datagram boundaries.
When given a unix dgram socket, it will be able to read from it, but
will attempt to send on the dgram_dst, which is unset. The other end
will not receive the data.
Let's teach -net socket to recognize a UNIX DGRAM socket, and use the
regular send() command (without dgram_dst).
This makes running slirp out-of-process possible that
way (python pseudo-code):
a, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
subprocess.Popen('qemu -net socket,fd=%d -net user' % a.fileno(), shell=True)
subprocess.Popen('qemu ... -net nic -net socket,fd=%d' % b.fileno(), shell=True)
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
net/socket.c | 25 +++++++++++++++++++++----
1 file changed, 21 insertions(+), 4 deletions(-)
diff --git a/net/socket.c b/net/socket.c
index 90ef3517be..c92354049b 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -119,9 +119,13 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,
ssize_t ret;
do {
- ret = qemu_sendto(s->fd, buf, size, 0,
- (struct sockaddr *)&s->dgram_dst,
- sizeof(s->dgram_dst));
+ if (s->dgram_dst.sin_family != AF_UNIX) {
+ ret = qemu_sendto(s->fd, buf, size, 0,
+ (struct sockaddr *)&s->dgram_dst,
+ sizeof(s->dgram_dst));
+ } else {
+ ret = send(s->fd, buf, size, 0);
+ }
} while (ret == -1 && errno == EINTR);
if (ret == -1 && errno == EAGAIN) {
@@ -336,6 +340,15 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
int newfd;
NetClientState *nc;
NetSocketState *s;
+ SocketAddress *sa;
+ SocketAddressType sa_type;
+
+ sa = socket_local_address(fd, errp);
+ if (!sa) {
+ return NULL;
+ }
+ sa_type = 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" process, datagrams would be recv()
@@ -379,8 +392,12 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
"socket: fd=%d (cloned mcast=%s:%d)",
fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
} else {
+ if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) {
+ s->dgram_dst.sin_family = AF_UNIX;
+ }
+
snprintf(nc->info_str, sizeof(nc->info_str),
- "socket: fd=%d", fd);
+ "socket: fd=%d %s", fd, SocketAddressType_str(sa_type));
}
return s;
--
2.21.0.4.g36eb1cb9cf
On 2019/3/12 上午1:25, Marc-André Lureau wrote:
> -net socket has a fd argument, and may be passed pre-opened sockets.
>
> TCP sockets use framing.
> UDP sockets have datagram boundaries.
>
> When given a unix dgram socket, it will be able to read from it, but
> will attempt to send on the dgram_dst, which is unset. The other end
> will not receive the data.
>
> Let's teach -net socket to recognize a UNIX DGRAM socket, and use the
> regular send() command (without dgram_dst).
>
> This makes running slirp out-of-process possible that
> way (python pseudo-code):
>
> a, b = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
>
> subprocess.Popen('qemu -net socket,fd=%d -net user' % a.fileno(), shell=True)
> subprocess.Popen('qemu ... -net nic -net socket,fd=%d' % b.fileno(), shell=True)
>
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
> net/socket.c | 25 +++++++++++++++++++++----
> 1 file changed, 21 insertions(+), 4 deletions(-)
>
> diff --git a/net/socket.c b/net/socket.c
> index 90ef3517be..c92354049b 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -119,9 +119,13 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,
> ssize_t ret;
>
> do {
> - ret = qemu_sendto(s->fd, buf, size, 0,
> - (struct sockaddr *)&s->dgram_dst,
> - sizeof(s->dgram_dst));
> + if (s->dgram_dst.sin_family != AF_UNIX) {
> + ret = qemu_sendto(s->fd, buf, size, 0,
> + (struct sockaddr *)&s->dgram_dst,
> + sizeof(s->dgram_dst));
> + } else {
> + ret = send(s->fd, buf, size, 0);
> + }
> } while (ret == -1 && errno == EINTR);
>
> if (ret == -1 && errno == EAGAIN) {
> @@ -336,6 +340,15 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
> int newfd;
> NetClientState *nc;
> NetSocketState *s;
> + SocketAddress *sa;
> + SocketAddressType sa_type;
> +
> + sa = socket_local_address(fd, errp);
> + if (!sa) {
> + return NULL;
> + }
> + sa_type = 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" process, datagrams would be recv()
> @@ -379,8 +392,12 @@ static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer,
> "socket: fd=%d (cloned mcast=%s:%d)",
> fd, inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
> } else {
> + if (sa_type == SOCKET_ADDRESS_TYPE_UNIX) {
> + s->dgram_dst.sin_family = AF_UNIX;
> + }
> +
> snprintf(nc->info_str, sizeof(nc->info_str),
> - "socket: fd=%d", fd);
> + "socket: fd=%d %s", fd, SocketAddressType_str(sa_type));
> }
>
> return s;
Applied.
Thanks
© 2016 - 2026 Red Hat, Inc.