Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

Hengqi Chen and committed by
Alexei Starovoitov
b6c4e715 9eeb3aa3

+94
+54
tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* Copyright (c) 2021 Hengqi Chen */ 3 + 4 + #include <test_progs.h> 5 + #include <sys/un.h> 6 + #include "test_skc_to_unix_sock.skel.h" 7 + 8 + static const char *sock_path = "@skc_to_unix_sock"; 9 + 10 + void test_skc_to_unix_sock(void) 11 + { 12 + struct test_skc_to_unix_sock *skel; 13 + struct sockaddr_un sockaddr; 14 + int err, sockfd = 0; 15 + 16 + skel = test_skc_to_unix_sock__open(); 17 + if (!ASSERT_OK_PTR(skel, "could not open BPF object")) 18 + return; 19 + 20 + skel->rodata->my_pid = getpid(); 21 + 22 + err = test_skc_to_unix_sock__load(skel); 23 + if (!ASSERT_OK(err, "could not load BPF object")) 24 + goto cleanup; 25 + 26 + err = test_skc_to_unix_sock__attach(skel); 27 + if (!ASSERT_OK(err, "could not attach BPF object")) 28 + goto cleanup; 29 + 30 + /* trigger unix_listen */ 31 + sockfd = socket(AF_UNIX, SOCK_STREAM, 0); 32 + if (!ASSERT_GT(sockfd, 0, "socket failed")) 33 + goto cleanup; 34 + 35 + memset(&sockaddr, 0, sizeof(sockaddr)); 36 + sockaddr.sun_family = AF_UNIX; 37 + strncpy(sockaddr.sun_path, sock_path, strlen(sock_path)); 38 + sockaddr.sun_path[0] = '\0'; 39 + 40 + err = bind(sockfd, (struct sockaddr *)&sockaddr, sizeof(sockaddr)); 41 + if (!ASSERT_OK(err, "bind failed")) 42 + goto cleanup; 43 + 44 + err = listen(sockfd, 1); 45 + if (!ASSERT_OK(err, "listen failed")) 46 + goto cleanup; 47 + 48 + ASSERT_EQ(strcmp(skel->bss->path, sock_path), 0, "bpf_skc_to_unix_sock failed"); 49 + 50 + cleanup: 51 + if (sockfd) 52 + close(sockfd); 53 + test_skc_to_unix_sock__destroy(skel); 54 + }
+40
tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* Copyright (c) 2021 Hengqi Chen */ 3 + 4 + #include "vmlinux.h" 5 + #include <bpf/bpf_helpers.h> 6 + #include <bpf/bpf_tracing.h> 7 + #include "bpf_tracing_net.h" 8 + 9 + const volatile pid_t my_pid = 0; 10 + char path[256] = {}; 11 + 12 + SEC("fentry/unix_listen") 13 + int BPF_PROG(unix_listen, struct socket *sock, int backlog) 14 + { 15 + pid_t pid = bpf_get_current_pid_tgid() >> 32; 16 + struct unix_sock *unix_sk; 17 + int i, len; 18 + 19 + if (pid != my_pid) 20 + return 0; 21 + 22 + unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock(sock->sk); 23 + if (!unix_sk) 24 + return 0; 25 + 26 + if (!UNIX_ABSTRACT(unix_sk)) 27 + return 0; 28 + 29 + len = unix_sk->addr->len - sizeof(short); 30 + path[0] = '@'; 31 + for (i = 1; i < len; i++) { 32 + if (i >= sizeof(struct sockaddr_un)) 33 + break; 34 + 35 + path[i] = unix_sk->addr->name->sun_path[i]; 36 + } 37 + return 0; 38 + } 39 + 40 + char _license[] SEC("license") = "GPL";