mirror of
https://github.com/torvalds/linux.git
synced 2024-12-14 07:02:23 +00:00
selftests/bpf: Test bpf_skc_to_unix_sock() helper
Add a new test which triggers unix_listen kernel function to test bpf_skc_to_unix_sock helper. Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20211021134752.1223426-3-hengqi.chen@gmail.com
This commit is contained in:
parent
9eeb3aa33a
commit
b6c4e71516
54
tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
Normal file
54
tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/* Copyright (c) 2021 Hengqi Chen */
|
||||
|
||||
#include <test_progs.h>
|
||||
#include <sys/un.h>
|
||||
#include "test_skc_to_unix_sock.skel.h"
|
||||
|
||||
static const char *sock_path = "@skc_to_unix_sock";
|
||||
|
||||
void test_skc_to_unix_sock(void)
|
||||
{
|
||||
struct test_skc_to_unix_sock *skel;
|
||||
struct sockaddr_un sockaddr;
|
||||
int err, sockfd = 0;
|
||||
|
||||
skel = test_skc_to_unix_sock__open();
|
||||
if (!ASSERT_OK_PTR(skel, "could not open BPF object"))
|
||||
return;
|
||||
|
||||
skel->rodata->my_pid = getpid();
|
||||
|
||||
err = test_skc_to_unix_sock__load(skel);
|
||||
if (!ASSERT_OK(err, "could not load BPF object"))
|
||||
goto cleanup;
|
||||
|
||||
err = test_skc_to_unix_sock__attach(skel);
|
||||
if (!ASSERT_OK(err, "could not attach BPF object"))
|
||||
goto cleanup;
|
||||
|
||||
/* trigger unix_listen */
|
||||
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (!ASSERT_GT(sockfd, 0, "socket failed"))
|
||||
goto cleanup;
|
||||
|
||||
memset(&sockaddr, 0, sizeof(sockaddr));
|
||||
sockaddr.sun_family = AF_UNIX;
|
||||
strncpy(sockaddr.sun_path, sock_path, strlen(sock_path));
|
||||
sockaddr.sun_path[0] = '\0';
|
||||
|
||||
err = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
|
||||
if (!ASSERT_OK(err, "bind failed"))
|
||||
goto cleanup;
|
||||
|
||||
err = listen(sockfd, 1);
|
||||
if (!ASSERT_OK(err, "listen failed"))
|
||||
goto cleanup;
|
||||
|
||||
ASSERT_EQ(strcmp(skel->bss->path, sock_path), 0, "bpf_skc_to_unix_sock failed");
|
||||
|
||||
cleanup:
|
||||
if (sockfd)
|
||||
close(sockfd);
|
||||
test_skc_to_unix_sock__destroy(skel);
|
||||
}
|
40
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
Normal file
40
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
Normal file
@ -0,0 +1,40 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0 */
|
||||
/* Copyright (c) 2021 Hengqi Chen */
|
||||
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include <bpf/bpf_tracing.h>
|
||||
#include "bpf_tracing_net.h"
|
||||
|
||||
const volatile pid_t my_pid = 0;
|
||||
char path[256] = {};
|
||||
|
||||
SEC("fentry/unix_listen")
|
||||
int BPF_PROG(unix_listen, struct socket *sock, int backlog)
|
||||
{
|
||||
pid_t pid = bpf_get_current_pid_tgid() >> 32;
|
||||
struct unix_sock *unix_sk;
|
||||
int i, len;
|
||||
|
||||
if (pid != my_pid)
|
||||
return 0;
|
||||
|
||||
unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock(sock->sk);
|
||||
if (!unix_sk)
|
||||
return 0;
|
||||
|
||||
if (!UNIX_ABSTRACT(unix_sk))
|
||||
return 0;
|
||||
|
||||
len = unix_sk->addr->len - sizeof(short);
|
||||
path[0] = '@';
|
||||
for (i = 1; i < len; i++) {
|
||||
if (i >= sizeof(struct sockaddr_un))
|
||||
break;
|
||||
|
||||
path[i] = unix_sk->addr->name->sun_path[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
Loading…
Reference in New Issue
Block a user