From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>,
"'Richard W . M . Jones'" <rjones@redhat.com>,
Minxi Hou <mhou@redhat.com>
Subject: [PATCH 4/8] tap: Split tap_sock_unix_init() into opening and listening parts
Date: Wed, 22 May 2024 22:59:07 +0200 [thread overview]
Message-ID: <20240522205911.261325-5-sbrivio@redhat.com> (raw)
In-Reply-To: <20240522205911.261325-1-sbrivio@redhat.com>
We'll need to open and bind the socket a while before listening to it,
so split that into two different functions. No functional changes
intended.
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
tap.c | 39 +++++++++++++++++++++++++++------------
1 file changed, 27 insertions(+), 12 deletions(-)
diff --git a/tap.c b/tap.c
index cb6df5a..c9f580e 100644
--- a/tap.c
+++ b/tap.c
@@ -1095,14 +1095,14 @@ restart:
}
/**
- * tap_sock_unix_init() - Create and bind AF_UNIX socket, listen for connection
- * @c: Execution context
+ * tap_sock_unix_open() - Create and bind AF_UNIX socket
+ * @sock_path: Socket path. If empty, set on return (UNIX_SOCK_PATH as prefix)
+ *
+ * Return: socket descriptor on success, won't return on failure
*/
-static void tap_sock_unix_init(struct ctx *c)
+static int tap_sock_unix_open(char *sock_path)
{
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
- union epoll_ref ref = { .type = EPOLL_TYPE_TAP_LISTEN };
- struct epoll_event ev = { 0 };
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
};
@@ -1115,8 +1115,8 @@ static void tap_sock_unix_init(struct ctx *c)
char *path = addr.sun_path;
int ex, ret;
- if (*c->sock_path)
- memcpy(path, c->sock_path, UNIX_PATH_MAX);
+ if (*sock_path)
+ memcpy(path, sock_path, UNIX_PATH_MAX);
else
snprintf(path, UNIX_PATH_MAX - 1, UNIX_SOCK_PATH, i);
@@ -1127,7 +1127,7 @@ static void tap_sock_unix_init(struct ctx *c)
ret = connect(ex, (const struct sockaddr *)&addr, sizeof(addr));
if (!ret || (errno != ENOENT && errno != ECONNREFUSED &&
errno != EACCES)) {
- if (*c->sock_path)
+ if (*sock_path)
die("Socket path %s already in use", path);
close(ex);
@@ -1137,7 +1137,7 @@ static void tap_sock_unix_init(struct ctx *c)
unlink(path);
if (!bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) ||
- *c->sock_path)
+ *sock_path)
break;
}
@@ -1145,17 +1145,31 @@ static void tap_sock_unix_init(struct ctx *c)
die("UNIX socket bind: %s", strerror(errno));
info("UNIX domain socket bound at %s\n", addr.sun_path);
+ if (!*sock_path)
+ memcpy(sock_path, addr.sun_path, UNIX_PATH_MAX);
+
+ return fd;
+}
+
+/**
+ * tap_sock_unix_init() - Start listening for connections on AF_UNIX socket
+ * @c: Execution context
+ */
+static void tap_sock_unix_init(struct ctx *c)
+{
+ union epoll_ref ref = { .type = EPOLL_TYPE_TAP_LISTEN };
+ struct epoll_event ev = { 0 };
- listen(fd, 0);
+ listen(c->fd_tap_listen, 0);
- ref.fd = c->fd_tap_listen = fd;
+ ref.fd = c->fd_tap_listen;
ev.events = EPOLLIN | EPOLLET;
ev.data.u64 = ref.u64;
epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap_listen, &ev);
info("You can now start qemu (>= 7.2, with commit 13c6be96618c):");
info(" kvm ... -device virtio-net-pci,netdev=s -netdev stream,id=s,server=off,addr.type=unix,addr.path=%s",
- addr.sun_path);
+ c->sock_path);
info("or qrap, for earlier qemu versions:");
info(" ./qrap 5 kvm ... -net socket,fd=5 -net nic,model=virtio");
}
@@ -1304,6 +1318,7 @@ void tap_sock_init(struct ctx *c)
}
if (c->mode == MODE_PASST) {
+ c->fd_tap_listen = tap_sock_unix_open(c->sock_path);
tap_sock_unix_init(c);
/* In passt mode, we don't know the guest's MAC address until it
--
@@ -1095,14 +1095,14 @@ restart:
}
/**
- * tap_sock_unix_init() - Create and bind AF_UNIX socket, listen for connection
- * @c: Execution context
+ * tap_sock_unix_open() - Create and bind AF_UNIX socket
+ * @sock_path: Socket path. If empty, set on return (UNIX_SOCK_PATH as prefix)
+ *
+ * Return: socket descriptor on success, won't return on failure
*/
-static void tap_sock_unix_init(struct ctx *c)
+static int tap_sock_unix_open(char *sock_path)
{
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
- union epoll_ref ref = { .type = EPOLL_TYPE_TAP_LISTEN };
- struct epoll_event ev = { 0 };
struct sockaddr_un addr = {
.sun_family = AF_UNIX,
};
@@ -1115,8 +1115,8 @@ static void tap_sock_unix_init(struct ctx *c)
char *path = addr.sun_path;
int ex, ret;
- if (*c->sock_path)
- memcpy(path, c->sock_path, UNIX_PATH_MAX);
+ if (*sock_path)
+ memcpy(path, sock_path, UNIX_PATH_MAX);
else
snprintf(path, UNIX_PATH_MAX - 1, UNIX_SOCK_PATH, i);
@@ -1127,7 +1127,7 @@ static void tap_sock_unix_init(struct ctx *c)
ret = connect(ex, (const struct sockaddr *)&addr, sizeof(addr));
if (!ret || (errno != ENOENT && errno != ECONNREFUSED &&
errno != EACCES)) {
- if (*c->sock_path)
+ if (*sock_path)
die("Socket path %s already in use", path);
close(ex);
@@ -1137,7 +1137,7 @@ static void tap_sock_unix_init(struct ctx *c)
unlink(path);
if (!bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) ||
- *c->sock_path)
+ *sock_path)
break;
}
@@ -1145,17 +1145,31 @@ static void tap_sock_unix_init(struct ctx *c)
die("UNIX socket bind: %s", strerror(errno));
info("UNIX domain socket bound at %s\n", addr.sun_path);
+ if (!*sock_path)
+ memcpy(sock_path, addr.sun_path, UNIX_PATH_MAX);
+
+ return fd;
+}
+
+/**
+ * tap_sock_unix_init() - Start listening for connections on AF_UNIX socket
+ * @c: Execution context
+ */
+static void tap_sock_unix_init(struct ctx *c)
+{
+ union epoll_ref ref = { .type = EPOLL_TYPE_TAP_LISTEN };
+ struct epoll_event ev = { 0 };
- listen(fd, 0);
+ listen(c->fd_tap_listen, 0);
- ref.fd = c->fd_tap_listen = fd;
+ ref.fd = c->fd_tap_listen;
ev.events = EPOLLIN | EPOLLET;
ev.data.u64 = ref.u64;
epoll_ctl(c->epollfd, EPOLL_CTL_ADD, c->fd_tap_listen, &ev);
info("You can now start qemu (>= 7.2, with commit 13c6be96618c):");
info(" kvm ... -device virtio-net-pci,netdev=s -netdev stream,id=s,server=off,addr.type=unix,addr.path=%s",
- addr.sun_path);
+ c->sock_path);
info("or qrap, for earlier qemu versions:");
info(" ./qrap 5 kvm ... -net socket,fd=5 -net nic,model=virtio");
}
@@ -1304,6 +1318,7 @@ void tap_sock_init(struct ctx *c)
}
if (c->mode == MODE_PASST) {
+ c->fd_tap_listen = tap_sock_unix_open(c->sock_path);
tap_sock_unix_init(c);
/* In passt mode, we don't know the guest's MAC address until it
--
2.43.0
next prev parent reply other threads:[~2024-05-22 20:59 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-22 20:59 [PATCH 0/8] Open socket and PID files as root, before switching Stefano Brivio
2024-05-22 20:59 ` [PATCH 1/8] conf: Don't lecture user about starting us as root Stefano Brivio
2024-05-23 1:45 ` David Gibson
2024-05-23 9:52 ` Richard W.M. Jones
2024-05-22 20:59 ` [PATCH 2/8] tap: Move all-ones initialisation of mac_guest to tap_sock_init() Stefano Brivio
2024-05-23 1:46 ` David Gibson
2024-05-23 9:59 ` Richard W.M. Jones
2024-05-23 10:03 ` Richard W.M. Jones
2024-05-22 20:59 ` [PATCH 3/8] passt, tap: Don't use -1 as uninitialised value for fd_tap_listen Stefano Brivio
2024-05-23 1:48 ` David Gibson
2024-05-22 20:59 ` Stefano Brivio [this message]
2024-05-23 10:05 ` [PATCH 4/8] tap: Split tap_sock_unix_init() into opening and listening parts Richard W.M. Jones
2024-05-28 7:01 ` David Gibson
2024-05-22 20:59 ` [PATCH 5/8] util: Rename write_pidfile() to pidfile_write() Stefano Brivio
2024-05-23 10:06 ` Richard W.M. Jones
2024-05-22 20:59 ` [PATCH 6/8] passt, util: Move opening of PID file to its own function Stefano Brivio
2024-05-23 10:06 ` Richard W.M. Jones
2024-05-28 7:04 ` David Gibson
2024-05-22 20:59 ` [PATCH 7/8] conf, passt, tap: Open socket and PID files before switching UID/GID Stefano Brivio
2024-05-23 10:10 ` Richard W.M. Jones
2024-05-29 2:35 ` David Gibson
2024-06-20 11:30 ` Richard W.M. Jones
2024-06-20 12:12 ` Stefano Brivio
2024-06-20 12:47 ` Richard W.M. Jones
2024-06-20 14:22 ` Stefano Brivio
2024-06-21 1:02 ` David Gibson
2024-05-22 20:59 ` [PATCH 8/8] conf, passt.h: Rename pid_file in struct ctx to pidfile Stefano Brivio
2024-05-23 10:11 ` Richard W.M. Jones
2024-05-28 7:07 ` David Gibson
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=20240522205911.261325-5-sbrivio@redhat.com \
--to=sbrivio@redhat.com \
--cc=david@gibson.dropbear.id.au \
--cc=mhou@redhat.com \
--cc=passt-dev@passt.top \
--cc=rjones@redhat.com \
/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.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
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 IMAP folder(s).