From: Bobby Eshleman <[email protected]> No shell tool can issue IOCTL_VM_SOCKETS_ASSIGN_G2H_NETNS, so anything that wants to move the guest's vsock device from a script needs a small program to do it.
It exits with the ioctl's errno so a caller can differentiate the reasons for failure. Signed-off-by: Bobby Eshleman <[email protected]> --- tools/testing/selftests/vsock/.gitignore | 1 + tools/testing/selftests/vsock/Makefile | 3 +- .../selftests/vsock/vsock_assign_g2h_netns.c | 45 ++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vsock/.gitignore b/tools/testing/selftests/vsock/.gitignore index 9c5bf379480f..ffca0d9c34b1 100644 --- a/tools/testing/selftests/vsock/.gitignore +++ b/tools/testing/selftests/vsock/.gitignore @@ -1,2 +1,3 @@ vmtest.log +vsock_assign_g2h_netns vsock_test diff --git a/tools/testing/selftests/vsock/Makefile b/tools/testing/selftests/vsock/Makefile index c407c0afd938..d7170a15150f 100644 --- a/tools/testing/selftests/vsock/Makefile +++ b/tools/testing/selftests/vsock/Makefile @@ -11,7 +11,6 @@ $(OUTPUT)/vsock_test: $(VSOCK_TEST_DIR)/vsock_test $(VSOCK_TEST_DIR)/vsock_test: $(VSOCK_TEST_SRCS) $(MAKE) -C $(VSOCK_TEST_DIR) vsock_test TEST_PROGS += vmtest.sh -TEST_GEN_FILES := vsock_test +TEST_GEN_FILES := vsock_test vsock_assign_g2h_netns include ../lib.mk - diff --git a/tools/testing/selftests/vsock/vsock_assign_g2h_netns.c b/tools/testing/selftests/vsock/vsock_assign_g2h_netns.c new file mode 100644 index 000000000000..6f15629af607 --- /dev/null +++ b/tools/testing/selftests/vsock/vsock_assign_g2h_netns.c @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Assign the guest->host vsock transport, i.e. the guest's virtio-vsock + * device, to the network namespace of the invoking process. + * + * Exits with the ioctl's errno, so that callers can tell why it was refused. + * + * Copyright (c) 2026 Meta Platforms, Inc. and affiliates + */ + +#include <errno.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <sys/ioctl.h> +#include <unistd.h> + +#include <linux/vm_sockets.h> + +#ifndef IOCTL_VM_SOCKETS_ASSIGN_G2H_NETNS +#define IOCTL_VM_SOCKETS_ASSIGN_G2H_NETNS _IO(7, 0xba) +#endif + +int main(void) +{ + int fd, ret; + + fd = open("/dev/vsock", O_RDONLY); + if (fd < 0) { + fprintf(stderr, "open /dev/vsock: %s\n", strerror(errno)); + return -1; + } + + ret = ioctl(fd, IOCTL_VM_SOCKETS_ASSIGN_G2H_NETNS); + if (ret < 0) { + ret = errno; + fprintf(stderr, + "IOCTL_VM_SOCKETS_ASSIGN_G2H_NETNS: %s (errno %d)\n", + strerror(errno), errno); + } + + close(fd); + + return ret; +} -- 2.53.0-Meta

