mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-04 17:44:14 +08:00
selftests/bpf: test_progs: convert test_sockopt_inherit
Move the files, adjust includes, remove entry from Makefile & .gitignore I also added pthread_cond_wait for the server thread startup. We don't want to connect to the server that's not yet up (for some reason this existing race is now more prominent with test_progs). Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
parent
3886bd7c9b
commit
e3e02e1d9c
1
tools/testing/selftests/bpf/.gitignore
vendored
1
tools/testing/selftests/bpf/.gitignore
vendored
@ -39,5 +39,4 @@ libbpf.so.*
|
||||
test_hashmap
|
||||
test_btf_dump
|
||||
xdping
|
||||
test_sockopt_inherit
|
||||
test_tcp_rtt
|
||||
|
@ -28,8 +28,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
|
||||
test_sock test_btf test_sockmap get_cgroup_id_user test_socket_cookie \
|
||||
test_cgroup_storage test_select_reuseport test_section_names \
|
||||
test_netcnt test_tcpnotify_user test_sock_fields test_sysctl test_hashmap \
|
||||
test_btf_dump test_cgroup_attach xdping \
|
||||
test_sockopt_inherit test_tcp_rtt
|
||||
test_btf_dump test_cgroup_attach xdping test_tcp_rtt
|
||||
|
||||
BPF_OBJ_FILES = $(patsubst %.c,%.o, $(notdir $(wildcard progs/*.c)))
|
||||
TEST_GEN_FILES = $(BPF_OBJ_FILES)
|
||||
@ -113,7 +112,6 @@ $(OUTPUT)/test_netcnt: cgroup_helpers.c
|
||||
$(OUTPUT)/test_sock_fields: cgroup_helpers.c
|
||||
$(OUTPUT)/test_sysctl: cgroup_helpers.c
|
||||
$(OUTPUT)/test_cgroup_attach: cgroup_helpers.c
|
||||
$(OUTPUT)/test_sockopt_inherit: cgroup_helpers.c
|
||||
$(OUTPUT)/test_tcp_rtt: cgroup_helpers.c
|
||||
|
||||
.PHONY: force
|
||||
|
@ -1,22 +1,7 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include <error.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include <linux/filter.h>
|
||||
#include <bpf/bpf.h>
|
||||
#include <bpf/libbpf.h>
|
||||
|
||||
#include "bpf_rlimit.h"
|
||||
#include "bpf_util.h"
|
||||
#include <test_progs.h>
|
||||
#include "cgroup_helpers.h"
|
||||
|
||||
#define CG_PATH "/sockopt_inherit"
|
||||
#define SOL_CUSTOM 0xdeadbeef
|
||||
#define CUSTOM_INHERIT1 0
|
||||
#define CUSTOM_INHERIT2 1
|
||||
@ -74,6 +59,9 @@ static int verify_sockopt(int fd, int optname, const char *msg, char expected)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
|
||||
static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
|
||||
|
||||
static void *server_thread(void *arg)
|
||||
{
|
||||
struct sockaddr_storage addr;
|
||||
@ -82,16 +70,26 @@ static void *server_thread(void *arg)
|
||||
int client_fd;
|
||||
int err = 0;
|
||||
|
||||
if (listen(fd, 1) < 0)
|
||||
error(1, errno, "Failed to listed on socket");
|
||||
err = listen(fd, 1);
|
||||
|
||||
pthread_mutex_lock(&server_started_mtx);
|
||||
pthread_cond_signal(&server_started);
|
||||
pthread_mutex_unlock(&server_started_mtx);
|
||||
|
||||
if (CHECK_FAIL(err < 0)) {
|
||||
perror("Failed to listed on socket");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err += verify_sockopt(fd, CUSTOM_INHERIT1, "listen", 1);
|
||||
err += verify_sockopt(fd, CUSTOM_INHERIT2, "listen", 1);
|
||||
err += verify_sockopt(fd, CUSTOM_LISTENER, "listen", 1);
|
||||
|
||||
client_fd = accept(fd, (struct sockaddr *)&addr, &len);
|
||||
if (client_fd < 0)
|
||||
error(1, errno, "Failed to accept client");
|
||||
if (CHECK_FAIL(client_fd < 0)) {
|
||||
perror("Failed to accept client");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
err += verify_sockopt(client_fd, CUSTOM_INHERIT1, "accept", 1);
|
||||
err += verify_sockopt(client_fd, CUSTOM_INHERIT2, "accept", 1);
|
||||
@ -167,7 +165,7 @@ static int prog_attach(struct bpf_object *obj, int cgroup_fd, const char *title)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int run_test(int cgroup_fd)
|
||||
static void run_test(int cgroup_fd)
|
||||
{
|
||||
struct bpf_prog_load_attr attr = {
|
||||
.file = "./sockopt_inherit.o",
|
||||
@ -180,40 +178,41 @@ static int run_test(int cgroup_fd)
|
||||
int err;
|
||||
|
||||
err = bpf_prog_load_xattr(&attr, &obj, &ignored);
|
||||
if (err) {
|
||||
log_err("Failed to load BPF object");
|
||||
return -1;
|
||||
}
|
||||
if (CHECK_FAIL(err))
|
||||
return;
|
||||
|
||||
err = prog_attach(obj, cgroup_fd, "cgroup/getsockopt");
|
||||
if (err)
|
||||
if (CHECK_FAIL(err))
|
||||
goto close_bpf_object;
|
||||
|
||||
err = prog_attach(obj, cgroup_fd, "cgroup/setsockopt");
|
||||
if (err)
|
||||
if (CHECK_FAIL(err))
|
||||
goto close_bpf_object;
|
||||
|
||||
server_fd = start_server();
|
||||
if (server_fd < 0) {
|
||||
err = -1;
|
||||
if (CHECK_FAIL(server_fd < 0))
|
||||
goto close_bpf_object;
|
||||
}
|
||||
|
||||
pthread_create(&tid, NULL, server_thread, (void *)&server_fd);
|
||||
if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
|
||||
(void *)&server_fd)))
|
||||
goto close_bpf_object;
|
||||
|
||||
pthread_mutex_lock(&server_started_mtx);
|
||||
pthread_cond_wait(&server_started, &server_started_mtx);
|
||||
pthread_mutex_unlock(&server_started_mtx);
|
||||
|
||||
client_fd = connect_to_server(server_fd);
|
||||
if (client_fd < 0) {
|
||||
err = -1;
|
||||
if (CHECK_FAIL(client_fd < 0))
|
||||
goto close_server_fd;
|
||||
}
|
||||
|
||||
err += verify_sockopt(client_fd, CUSTOM_INHERIT1, "connect", 0);
|
||||
err += verify_sockopt(client_fd, CUSTOM_INHERIT2, "connect", 0);
|
||||
err += verify_sockopt(client_fd, CUSTOM_LISTENER, "connect", 0);
|
||||
CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_INHERIT1, "connect", 0));
|
||||
CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_INHERIT2, "connect", 0));
|
||||
CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_LISTENER, "connect", 0));
|
||||
|
||||
pthread_join(tid, &server_err);
|
||||
|
||||
err += (int)(long)server_err;
|
||||
err = (int)(long)server_err;
|
||||
CHECK_FAIL(err);
|
||||
|
||||
close(client_fd);
|
||||
|
||||
@ -221,33 +220,16 @@ close_server_fd:
|
||||
close(server_fd);
|
||||
close_bpf_object:
|
||||
bpf_object__close(obj);
|
||||
return err;
|
||||
}
|
||||
|
||||
int main(int args, char **argv)
|
||||
void test_sockopt_inherit(void)
|
||||
{
|
||||
int cgroup_fd;
|
||||
int err = EXIT_SUCCESS;
|
||||
|
||||
if (setup_cgroup_environment())
|
||||
return err;
|
||||
cgroup_fd = test__join_cgroup("/sockopt_inherit");
|
||||
if (CHECK_FAIL(cgroup_fd < 0))
|
||||
return;
|
||||
|
||||
cgroup_fd = create_and_get_cgroup(CG_PATH);
|
||||
if (cgroup_fd < 0)
|
||||
goto cleanup_cgroup_env;
|
||||
|
||||
if (join_cgroup(CG_PATH))
|
||||
goto cleanup_cgroup;
|
||||
|
||||
if (run_test(cgroup_fd))
|
||||
err = EXIT_FAILURE;
|
||||
|
||||
printf("test_sockopt_inherit: %s\n",
|
||||
err == EXIT_SUCCESS ? "PASSED" : "FAILED");
|
||||
|
||||
cleanup_cgroup:
|
||||
run_test(cgroup_fd);
|
||||
close(cgroup_fd);
|
||||
cleanup_cgroup_env:
|
||||
cleanup_cgroup_environment();
|
||||
return err;
|
||||
}
|
Loading…
Reference in New Issue
Block a user