Skip to contentSkip to conversation

RFC: net/socket: learn to talk with a unix dgram socket

PATCH In review Marc-André Lureau 3 participants 6 messages Nov 9, 2018 – Mar 8, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 RFC: net/socket: learn to talk with a unix dgram socket

-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)

(to make slirp a seperate project altogether, we would have to have some compatibility code and/or deprecate various options & HMP commands for dynamic port forwarding etc - but this looks like a reachable goal)

---
 net/socket.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
net/socket.c +21 -4
index 7095eb749f..8a9c30892d 100644
@@ -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) {
@@ -322,6 +326,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()
@@ -365,8 +378,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;
Signature · 1 line
2.19.1.708.g4ede3d42df
#1 lore raw
Quoted 20 lines · Marc-André Lureau

-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)

Quoted 6 lines

(to make slirp a seperate project altogether, we would have to have some compatibility code and/or deprecate various options & HMP commands for dynamic port forwarding etc - but this looks like a reachable goal)

I believe instead of supporting unnamed sockets, we should also support named one through cli?

Quoted 12 lines
---
  net/socket.c | 25 +++++++++++++++++++++----
  1 file changed, 21 insertions(+), 4 deletions(-)
net/socket.c +0 -0
index 7095eb749f..8a9c30892d 100644
@@ -119,9 +119,13 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,
      ssize_t ret;
 
      do {

net/socket.c static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, · line 128

Quoted 10 lines
net/socket.c +7 -3
-        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);
+        }

Any reason that send is a must here? send(2) said:
       call

           send(sockfd, buf, len, flags);

       is equivalent to

           sendto(sockfd, buf, len, flags, NULL, 0);

Thanks

Quoted 33 lines
net/socket.c +14 -1
      } while (ret == -1 && errno == EINTR);
 
      if (ret == -1 && errno == EAGAIN) {
@@ -322,6 +326,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()
@@ -365,8 +378,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 lore raw

Hi

Quoted 28 lines · Jason Wang
Quoted 25 lines · Marc-André Lureau

-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)

(to make slirp a seperate project altogether, we would have to have some compatibility code and/or deprecate various options & HMP commands for dynamic port forwarding etc - but this looks like a reachable goal)

Quoted 5 lines
Quoted 1 line

I believe instead of supporting unnamed sockets, we should also support named one through cli?

This could be a later patch, I have no need for it yet. Perhaps it should be a chardev then?

Quoted 25 lines
Quoted 12 lines
---
  net/socket.c | 25 +++++++++++++++++++++----
  1 file changed, 21 insertions(+), 4 deletions(-)
net/socket.c +0 -0
index 7095eb749f..8a9c30892d 100644
@@ -119,9 +119,13 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,
      ssize_t ret;
 
      do {

net/socket.c static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, · line 128

Quoted 10 lines
net/socket.c +7 -3
-        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);
+        }

Any reason that send is a must here? send(2) said:

Quoted 7 lines
        call

            send(sockfd, buf, len, flags);

        is equivalent to

            sendto(sockfd, buf, len, flags, NULL, 0);

Yes they should be equivalent, but then we need to add ?: operators for the dest arguments. I preferred to have an if() instead.

thanks

Quoted 34 lines
Quoted 34 lines
net/socket.c +14 -1
      } while (ret == -1 && errno == EINTR);
 
      if (ret == -1 && errno == EAGAIN) {
@@ -322,6 +326,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()
@@ -365,8 +378,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;
#3 lore raw
Quoted 30 lines · Marc-André Lureau

Hi

Quoted 27 lines · Jason Wang
Quoted 25 lines · Marc-André Lureau

-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)

(to make slirp a seperate project altogether, we would have to have some compatibility code and/or deprecate various options & HMP commands for dynamic port forwarding etc - but this looks like a reachable goal)

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I believe instead of supporting unnamed sockets, we should also support named one through cli?

This could be a later patch, I have no need for it yet. Perhaps it should be a chardev then?

I mean something like: -socket id=ud0,path=/tmp/XXX

Quoted 29 lines
Quoted 29 lines
Quoted 13 lines
---
   net/socket.c | 25 +++++++++++++++++++++----
   1 file changed, 21 insertions(+), 4 deletions(-)
net/socket.c +0 -0
index 7095eb749f..8a9c30892d 100644
@@ -119,9 +119,13 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,
       ssize_t ret;
 
       do {

net/socket.c static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, · line 128

Quoted 10 lines
net/socket.c +7 -3
-        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);
+        }

Any reason that send is a must here? send(2) said:

         call

             send(sockfd, buf, len, flags);
Quoted 7 lines
Quoted 3 lines

is equivalent to

sendto(sockfd, buf, len, flags, NULL, 0);

Yes they should be equivalent, but then we need to add ?: operators for the dest arguments. I preferred to have an if() instead.

thanks

One possible issue here is I'm not sure there's a equivalent send() in e.g non POSIX system.

Thanks

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
net/socket.c +14 -1
       } while (ret == -1 && errno == EINTR);
 
       if (ret == -1 && errno == EAGAIN) {
@@ -322,6 +326,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()
@@ -365,8 +378,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;
#4 lore raw

Hi Jason

Quoted 35 lines · Jason Wang
Quoted 32 lines · Marc-André Lureau

Hi

Quoted 29 lines · Jason Wang
Quoted 26 lines · Marc-André Lureau

-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)

(to make slirp a seperate project altogether, we would have to have some compatibility code and/or deprecate various options & HMP commands for dynamic port forwarding etc - but this looks like a reachable goal)

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I believe instead of supporting unnamed sockets, we should also support named one through cli?

This could be a later patch, I have no need for it yet. Perhaps it should be a chardev then?

I mean something like: -socket id=ud0,path=/tmp/XXX

Why not, but I have no need for it. If somebody has, he can make a patch.

Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 13 lines
---
   net/socket.c | 25 +++++++++++++++++++++----
   1 file changed, 21 insertions(+), 4 deletions(-)
net/socket.c +0 -0
index 7095eb749f..8a9c30892d 100644
@@ -119,9 +119,13 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,
       ssize_t ret;
 
       do {

net/socket.c static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, · line 128

Quoted 10 lines
net/socket.c +7 -3
-        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);
+        }

Any reason that send is a must here? send(2) said:

         call

             send(sockfd, buf, len, flags);

         is equivalent to

             sendto(sockfd, buf, len, flags, NULL, 0);
Quoted 8 lines
Quoted 4 lines

Yes they should be equivalent, but then we need to add ?: operators for the dest arguments. I preferred to have an if() instead.

thanks

One possible issue here is I'm not sure there's a equivalent send() in e.g non POSIX system.

send() should be as common as sendto().

Should I resend the patch without RFC? no other changes needed?

thanks!

Quoted 39 lines

Thanks

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
net/socket.c +14 -1
       } while (ret == -1 && errno == EINTR);
 
       if (ret == -1 && errno == EAGAIN) {
@@ -322,6 +326,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()
@@ -365,8 +378,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;
Signature · 1 line
Marc-André Lureau
#5 lore raw
Quoted 75 lines · Marc-André Lureau

Hi Jason

Quoted 30 lines · Jason Wang
Quoted 29 lines · Marc-André Lureau

Hi

Quoted 26 lines · Jason Wang
Quoted 25 lines · Marc-André Lureau

-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)

(to make slirp a seperate project altogether, we would have to have some compatibility code and/or deprecate various options & HMP commands for dynamic port forwarding etc - but this looks like a reachable goal)

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I believe instead of supporting unnamed sockets, we should also support named one through cli?

This could be a later patch, I have no need for it yet. Perhaps it should be a chardev then?

I mean something like: -socket id=ud0,path=/tmp/XXX

Why not, but I have no need for it. If somebody has, he can make a patch.

Quoted 33 lines
Quoted 33 lines
Quoted 23 lines
Quoted 12 lines
---
    net/socket.c | 25 +++++++++++++++++++++----
    1 file changed, 21 insertions(+), 4 deletions(-)
net/socket.c +0 -0
index 7095eb749f..8a9c30892d 100644
@@ -119,9 +119,13 @@ static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf,
        ssize_t ret;
 
        do {

net/socket.c static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, · line 128

Quoted 10 lines
net/socket.c +7 -3
-        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);
+        }

Any reason that send is a must here? send(2) said:

Quoted 7 lines
          call

              send(sockfd, buf, len, flags);

          is equivalent to

              sendto(sockfd, buf, len, flags, NULL, 0);

Yes they should be equivalent, but then we need to add ?: operators for the dest arguments. I preferred to have an if() instead.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

thanks

One possible issue here is I'm not sure there's a equivalent send() in e.g non POSIX system.

send() should be as common as sendto().

Should I resend the patch without RFC?

Yes please.

Quoted 1 line

no other changes needed?

No.

Thanks

Quoted 3 lines

thanks!