diff mbox series

[bpf-next,2/2] selftests/bpf: Test bpf_skc_to_unix_sock()

Message ID 20211006040623.401527-3-hengqi.chen@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Add bpf_skc_to_unix_sock() helper | expand

Checks

Context Check Description
bpf/vmtest-bpf-next success VM_Test
bpf/vmtest-bpf-next-PR success PR summary
netdev/cover_letter success Series has a cover letter
netdev/fixes_present success Fixes tag not required for -next series
netdev/patch_count success Link
netdev/tree_selection success Clearly marked for bpf-next
netdev/subject_prefix success Link
netdev/cc_maintainers warning 4 maintainers not CCed: linux-kselftest@vger.kernel.org kpsingh@kernel.org shuah@kernel.org netdev@vger.kernel.org
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success No Fixes tag
netdev/checkpatch warning WARNING: Improper SPDX comment style for 'tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c', please use '//' instead WARNING: Improper SPDX comment style for 'tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c', please use '//' instead WARNING: Missing or malformed SPDX-License-Identifier tag in line 1 WARNING: Use of volatile is usually wrong: see Documentation/process/volatile-considered-harmful.rst WARNING: added, moved or deleted file(s), does MAINTAINERS need updating?
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/header_inline success No static functions without inline keyword in header files

Commit Message

Hengqi Chen Oct. 6, 2021, 4:06 a.m. UTC
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>
---
 .../bpf/prog_tests/skc_to_unix_sock.c         | 54 +++++++++++++++++++
 .../bpf/progs/test_skc_to_unix_sock.c         | 28 ++++++++++
 2 files changed, 82 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c

--
2.25.1

Comments

Song Liu Oct. 6, 2021, 5:09 a.m. UTC | #1
On Tue, Oct 5, 2021 at 9:08 PM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>
> 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>
> ---
>  .../bpf/prog_tests/skc_to_unix_sock.c         | 54 +++++++++++++++++++
>  .../bpf/progs/test_skc_to_unix_sock.c         | 28 ++++++++++
>  2 files changed, 82 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
> new file mode 100644
> index 000000000000..5d8ed76a71dc
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
> @@ -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 = "/tmp/test.sock";
> +
> +void test_skc_to_unix_sock(void)
> +{
> +       struct test_skc_to_unix_sock *skel;
> +       struct sockaddr_un sockaddr;
> +       int err, len, 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;
> +
> +       sockaddr.sun_family = AF_UNIX;
> +       strcpy(sockaddr.sun_path, sock_path);
> +       len = sizeof(sockaddr);
> +       unlink(sock_path);
> +
> +       err = bind(sockfd, (struct sockaddr *)&sockaddr, len);
> +       if (!ASSERT_OK(err, "bind failed"))
> +               goto cleanup;
> +
> +       err = listen(sockfd, 1);
> +       if (!ASSERT_OK(err, "listen failed"))
> +               goto cleanup;
> +
> +       ASSERT_GT(skel->bss->ret, 0, "bpf_skc_to_unix_sock failed");
> +
> +cleanup:
> +       if (sockfd)
> +               close(sockfd);
> +       test_skc_to_unix_sock__destroy(skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
> new file mode 100644
> index 000000000000..544d2ed56d7e
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
> @@ -0,0 +1,28 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/* Copyright (c) 2021 Hengqi Chen */
> +
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +const volatile pid_t my_pid = 0;
> +__u64 ret = 0;
> +
> +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;
> +
> +       if (pid != my_pid)
> +               return 0;
> +
> +       unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock(sock->sk);
> +       if (!unix_sk)
> +               return 0;
> +
> +       ret = (__u64)unix_sk;

Other than ret !=0, can we verify unix_sk we get is expected? May we can
verify unix_sock-> path matches /tmp/test.sock?

Thanks,
Song
Hengqi Chen Oct. 7, 2021, 4:05 a.m. UTC | #2
On 10/6/21 1:09 PM, Song Liu wrote:
> On Tue, Oct 5, 2021 at 9:08 PM Hengqi Chen <hengqi.chen@gmail.com> wrote:
>>
>> 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>
>> ---
>>  .../bpf/prog_tests/skc_to_unix_sock.c         | 54 +++++++++++++++++++
>>  .../bpf/progs/test_skc_to_unix_sock.c         | 28 ++++++++++
>>  2 files changed, 82 insertions(+)
>>  create mode 100644 tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
>>  create mode 100644 tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
>>
>> diff --git a/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
>> new file mode 100644
>> index 000000000000..5d8ed76a71dc
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
>> @@ -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 = "/tmp/test.sock";
>> +
>> +void test_skc_to_unix_sock(void)
>> +{
>> +       struct test_skc_to_unix_sock *skel;
>> +       struct sockaddr_un sockaddr;
>> +       int err, len, 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;
>> +
>> +       sockaddr.sun_family = AF_UNIX;
>> +       strcpy(sockaddr.sun_path, sock_path);
>> +       len = sizeof(sockaddr);
>> +       unlink(sock_path);
>> +
>> +       err = bind(sockfd, (struct sockaddr *)&sockaddr, len);
>> +       if (!ASSERT_OK(err, "bind failed"))
>> +               goto cleanup;
>> +
>> +       err = listen(sockfd, 1);
>> +       if (!ASSERT_OK(err, "listen failed"))
>> +               goto cleanup;
>> +
>> +       ASSERT_GT(skel->bss->ret, 0, "bpf_skc_to_unix_sock failed");
>> +
>> +cleanup:
>> +       if (sockfd)
>> +               close(sockfd);
>> +       test_skc_to_unix_sock__destroy(skel);
>> +}
>> diff --git a/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
>> new file mode 100644
>> index 000000000000..544d2ed56d7e
>> --- /dev/null
>> +++ b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
>> @@ -0,0 +1,28 @@
>> +/* SPDX-License-Identifier: GPL-2.0 */
>> +/* Copyright (c) 2021 Hengqi Chen */
>> +
>> +#include "vmlinux.h"
>> +#include <bpf/bpf_helpers.h>
>> +#include <bpf/bpf_tracing.h>
>> +
>> +const volatile pid_t my_pid = 0;
>> +__u64 ret = 0;
>> +
>> +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;
>> +
>> +       if (pid != my_pid)
>> +               return 0;
>> +
>> +       unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock(sock->sk);
>> +       if (!unix_sk)
>> +               return 0;
>> +
>> +       ret = (__u64)unix_sk;
> 
> Other than ret !=0, can we verify unix_sk we get is expected? May we can
> verify unix_sock-> path matches /tmp/test.sock?
> 

Yes, that would be much more precise. Will do.

> Thanks,
> Song
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
new file mode 100644
index 000000000000..5d8ed76a71dc
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/skc_to_unix_sock.c
@@ -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 = "/tmp/test.sock";
+
+void test_skc_to_unix_sock(void)
+{
+	struct test_skc_to_unix_sock *skel;
+	struct sockaddr_un sockaddr;
+	int err, len, 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;
+
+	sockaddr.sun_family = AF_UNIX;
+	strcpy(sockaddr.sun_path, sock_path);
+	len = sizeof(sockaddr);
+	unlink(sock_path);
+
+	err = bind(sockfd, (struct sockaddr *)&sockaddr, len);
+	if (!ASSERT_OK(err, "bind failed"))
+		goto cleanup;
+
+	err = listen(sockfd, 1);
+	if (!ASSERT_OK(err, "listen failed"))
+		goto cleanup;
+
+	ASSERT_GT(skel->bss->ret, 0, "bpf_skc_to_unix_sock failed");
+
+cleanup:
+	if (sockfd)
+		close(sockfd);
+	test_skc_to_unix_sock__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
new file mode 100644
index 000000000000..544d2ed56d7e
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_skc_to_unix_sock.c
@@ -0,0 +1,28 @@ 
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (c) 2021 Hengqi Chen */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+const volatile pid_t my_pid = 0;
+__u64 ret = 0;
+
+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;
+
+	if (pid != my_pid)
+		return 0;
+
+	unix_sk = (struct unix_sock *)bpf_skc_to_unix_sock(sock->sk);
+	if (!unix_sk)
+		return 0;
+
+	ret = (__u64)unix_sk;
+	return 0;
+}
+
+char _license[] SEC("license") = "GPL";