qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Vivier <lvivier@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Daniel P. Berrangé" <berrange@redhat.com>,
	xen-devel@lists.xenproject.org, "Eric Blake" <eblake@redhat.com>,
	"Stefano Stabellini" <sstabellini@kernel.org>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Greg Kurz" <groug@kaod.org>, "Jason Wang" <jasowang@redhat.com>,
	"Anthony Perard" <anthony.perard@citrix.com>,
	"Thomas Huth" <thuth@redhat.com>,
	"David Gibson" <david@gibson.dropbear.id.au>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Laurent Vivier" <lvivier@redhat.com>,
	"Samuel Thibault" <samuel.thibault@ens-lyon.org>,
	"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
	"Paul Durrant" <paul@xen.org>, "Stefan Weil" <sw@weilnetz.de>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Stefano Brivio" <sbrivio@redhat.com>
Subject: [PATCH v12 12/17] net: dgram: add unix socket
Date: Thu, 20 Oct 2022 11:16:19 +0200	[thread overview]
Message-ID: <20221020091624.48368-13-lvivier@redhat.com> (raw)
In-Reply-To: <20221020091624.48368-1-lvivier@redhat.com>

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
Reviewed-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
QAPI schema
Acked-by: Markus Armbruster <armbru@redhat.com>
---
 net/dgram.c     | 54 ++++++++++++++++++++++++++++++++++++++++++++++++-
 qapi/net.json   |  2 +-
 qemu-options.hx |  1 +
 3 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/net/dgram.c b/net/dgram.c
index e581cc62f39f..02a189e36358 100644
--- a/net/dgram.c
+++ b/net/dgram.c
@@ -426,6 +426,7 @@ int net_init_dgram(const Netdev *netdev, const char *name,
     SocketAddress *remote, *local;
     struct sockaddr *dest_addr;
     struct sockaddr_in laddr_in, raddr_in;
+    struct sockaddr_un laddr_un, raddr_un;
     socklen_t dest_len;
 
     assert(netdev->type == NET_CLIENT_DRIVER_DGRAM);
@@ -465,7 +466,7 @@ int net_init_dgram(const Netdev *netdev, const char *name,
         }
     } else {
         if (local->type != SOCKET_ADDRESS_TYPE_FD) {
-            error_setg(errp, "type=inet requires remote parameter");
+            error_setg(errp, "type=inet or unix require remote parameter");
             return -1;
         }
     }
@@ -508,6 +509,53 @@ int net_init_dgram(const Netdev *netdev, const char *name,
         dest_addr = g_malloc(dest_len);
         memcpy(dest_addr, &raddr_in, dest_len);
         break;
+    case SOCKET_ADDRESS_TYPE_UNIX:
+        ret = unlink(local->u.q_unix.path);
+        if (ret < 0 && errno != ENOENT) {
+            error_setg_errno(errp, errno, "failed to unlink socket %s",
+                             local->u.q_unix.path);
+            return -1;
+        }
+
+        laddr_un.sun_family = PF_UNIX;
+        ret = snprintf(laddr_un.sun_path, sizeof(laddr_un.sun_path), "%s",
+                       local->u.q_unix.path);
+        if (ret < 0 || ret >= sizeof(laddr_un.sun_path)) {
+            error_setg(errp, "UNIX socket path '%s' is too long",
+                       local->u.q_unix.path);
+            error_append_hint(errp, "Path must be less than %zu bytes\n",
+                              sizeof(laddr_un.sun_path));
+        }
+
+        raddr_un.sun_family = PF_UNIX;
+        ret = snprintf(raddr_un.sun_path, sizeof(raddr_un.sun_path), "%s",
+                       remote->u.q_unix.path);
+        if (ret < 0 || ret >= sizeof(raddr_un.sun_path)) {
+            error_setg(errp, "UNIX socket path '%s' is too long",
+                       remote->u.q_unix.path);
+            error_append_hint(errp, "Path must be less than %zu bytes\n",
+                              sizeof(raddr_un.sun_path));
+        }
+
+        fd = qemu_socket(PF_UNIX, SOCK_DGRAM, 0);
+        if (fd < 0) {
+            error_setg_errno(errp, errno, "can't create datagram socket");
+            return -1;
+        }
+
+        ret = bind(fd, (struct sockaddr *)&laddr_un, sizeof(laddr_un));
+        if (ret < 0) {
+            error_setg_errno(errp, errno, "can't bind unix=%s to socket",
+                             laddr_un.sun_path);
+            closesocket(fd);
+            return -1;
+        }
+        qemu_socket_set_nonblock(fd);
+
+        dest_len = sizeof(raddr_un);
+        dest_addr = g_malloc(dest_len);
+        memcpy(dest_addr, &raddr_un, dest_len);
+        break;
     case SOCKET_ADDRESS_TYPE_FD:
         fd = monitor_fd_param(monitor_cur(), local->u.fd.str, errp);
         if (fd == -1) {
@@ -546,6 +594,10 @@ int net_init_dgram(const Netdev *netdev, const char *name,
                           inet_ntoa(raddr_in.sin_addr),
                           ntohs(raddr_in.sin_port));
         break;
+    case SOCKET_ADDRESS_TYPE_UNIX:
+        qemu_set_info_str(&s->nc, "udp=%s:%s",
+                          laddr_un.sun_path, raddr_un.sun_path);
+        break;
     case SOCKET_ADDRESS_TYPE_FD: {
         SocketAddress *sa;
         SocketAddressType sa_type;
diff --git a/qapi/net.json b/qapi/net.json
index aed4ce1a97ff..39388b1b6c41 100644
--- a/qapi/net.json
+++ b/qapi/net.json
@@ -600,7 +600,7 @@
 # @remote: remote address
 # @local: local address
 #
-# Only SocketAddress types 'inet' and 'fd' are supported.
+# Only SocketAddress types 'unix', 'inet' and 'fd' are supported.
 #
 # If remote address is present and it's a multicast address, local address
 # is optional. Otherwise local address is required and remote address is
diff --git a/qemu-options.hx b/qemu-options.hx
index 98e2595df93b..fafb214cb89f 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -2782,6 +2782,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev,
     "                configure a network backend to connect to a multicast maddr and port\n"
     "                use ``local.host=addr`` to specify the host address to send packets from\n"
     "-netdev dgram,id=str,local.type=inet,local.host=addr,local.port=port[,remote.type=inet,remote.host=addr,remote.port=port]\n"
+    "-netdev dgram,id=str,local.type=unix,local.path=path[,remote.type=unix,remote.path=path]\n"
     "-netdev dgram,id=str,local.type=fd,local.str=file-descriptor\n"
     "                configure a network backend to connect to another network\n"
     "                using an UDP tunnel\n"
-- 
2.37.3



  parent reply	other threads:[~2022-10-20 10:04 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-20  9:16 [PATCH v12 00/17] qapi: net: add unix socket type support to netdev backend Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 01/17] net: introduce convert_host_port() Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 02/17] net: remove the @errp argument of net_client_inits() Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 03/17] net: simplify net_client_parse() error management Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 04/17] qapi: net: introduce a way to bypass qemu_opts_parse_noisily() Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 05/17] net: introduce qemu_set_info_str() function Laurent Vivier
2022-10-20 11:22   ` Philippe Mathieu-Daudé
2022-10-20  9:16 ` [PATCH v12 06/17] qapi: net: add stream and dgram netdevs Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 07/17] net: socket: Don't ignore EINVAL on netdev socket connection Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 08/17] net: stream: " Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 09/17] net: stream: add unix socket Laurent Vivier
2022-10-20 11:29   ` Philippe Mathieu-Daudé
2022-10-20  9:16 ` [PATCH v12 10/17] net: dgram: make dgram_dst generic Laurent Vivier
2022-10-20 11:17   ` Philippe Mathieu-Daudé
2022-10-20 15:08     ` Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 11/17] net: dgram: move mcast specific code from net_socket_fd_init_dgram() Laurent Vivier
2022-10-20  9:16 ` Laurent Vivier [this message]
2022-10-20 11:04   ` [PATCH v12 12/17] net: dgram: add unix socket Philippe Mathieu-Daudé
2022-10-20  9:16 ` [PATCH v12 13/17] qemu-sockets: move and rename SocketAddress_to_str() Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 14/17] qemu-sockets: update socket_uri() and socket_parse() to be consistent Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 15/17] net: stream: move to QIO to enable additional parameters Laurent Vivier
2022-10-20 11:05   ` Philippe Mathieu-Daudé
2022-10-20 13:09     ` Philippe Mathieu-Daudé
2022-10-20 13:09   ` Philippe Mathieu-Daudé
2022-10-20 15:23     ` Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 16/17] tests/qtest: netdev: test stream and dgram backends Laurent Vivier
2022-10-20 13:26   ` Philippe Mathieu-Daudé
2022-10-20 14:48     ` Laurent Vivier
2022-10-20  9:16 ` [PATCH v12 17/17] net: stream: add QAPI events to report connection state Laurent Vivier
2022-10-20 11:20   ` Philippe Mathieu-Daudé
2022-10-20 13:40 ` [RFC PATCH 18/17] util/qemu-sockets: Display IPv6 addresses within square brackets Philippe Mathieu-Daudé
2022-10-20 13:50   ` Daniel P. Berrangé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221020091624.48368-13-lvivier@redhat.com \
    --to=lvivier@redhat.com \
    --cc=anthony.perard@citrix.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=dgilbert@redhat.com \
    --cc=eblake@redhat.com \
    --cc=groug@kaod.org \
    --cc=jasowang@redhat.com \
    --cc=mst@redhat.com \
    --cc=paul@xen.org \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=samuel.thibault@ens-lyon.org \
    --cc=sbrivio@redhat.com \
    --cc=sstabellini@kernel.org \
    --cc=sw@weilnetz.de \
    --cc=thuth@redhat.com \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).