bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: Martin KaFai Lau <martin.lau@linux.dev>
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	kernel-team@fb.com, Martin KaFai Lau <martin.lau@kernel.org>,
	Yury Namgung <ynamgung@meta.com>,
	bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 4/5] selftests/bpf: Add a cgroup prog bpf_get_ns_current_pid_tgid() test
Date: Fri, 8 Mar 2024 16:04:40 -0800	[thread overview]
Message-ID: <ff89127f-2c2c-42db-948f-74d524243fc4@linux.dev> (raw)
In-Reply-To: <83a6001f-8ed4-4e31-92bd-282c34e6782f@linux.dev>


On 3/8/24 11:08 AM, Martin KaFai Lau wrote:
> On 3/7/24 3:27 PM, Yonghong Song wrote:
>> Add a cgroup bpf program test where the bpf program is running
>> in a pid namespace. The test is successfully:
>>    #165/3   ns_current_pid_tgid/new_ns_cgrp:OK
>>
>> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
>> ---
>>   .../bpf/prog_tests/ns_current_pid_tgid.c      | 54 +++++++++++++++++++
>>   .../bpf/progs/test_ns_current_pid_tgid.c      |  7 +++
>>   2 files changed, 61 insertions(+)
>>
>> diff --git 
>> a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c 
>> b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
>> index 847d7b70e290..a307303e01ee 100644
>> --- a/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
>> +++ b/tools/testing/selftests/bpf/prog_tests/ns_current_pid_tgid.c
>> @@ -12,6 +12,7 @@
>>   #include <sys/wait.h>
>>   #include <sys/mount.h>
>>   #include <sys/fcntl.h>
>> +#include "network_helpers.h"
>>     #define STACK_SIZE (1024 * 1024)
>>   static char child_stack[STACK_SIZE];
>> @@ -74,6 +75,50 @@ static int test_current_pid_tgid_tp(void *args)
>>       return ret;
>>   }
>>   +static int test_current_pid_tgid_cgrp(void *args)
>> +{
>> +    struct test_ns_current_pid_tgid__bss *bss;
>> +    struct test_ns_current_pid_tgid *skel;
>> +    int server_fd = -1, ret = -1, err;
>> +    int cgroup_fd = *(int *)args;
>> +    pid_t tgid, pid;
>> +
>> +    skel = test_ns_current_pid_tgid__open();
>> +    if (!ASSERT_OK_PTR(skel, "test_ns_current_pid_tgid__open"))
>> +        return ret;
>> +
>> +    bpf_program__set_autoload(skel->progs.cgroup_bind4, true);
>> +
>> +    err = test_ns_current_pid_tgid__load(skel);
>> +    if (!ASSERT_OK(err, "test_ns_current_pid_tgid__load"))
>> +        goto cleanup;
>> +
>> +    bss = skel->bss;
>> +    if (get_pid_tgid(&pid, &tgid, bss))
>> +        goto cleanup;
>> +
>> +    skel->links.cgroup_bind4 = bpf_program__attach_cgroup(
>> +        skel->progs.cgroup_bind4, cgroup_fd);
>> +    if (!ASSERT_OK_PTR(skel->links.cgroup_bind4, 
>> "bpf_program__attach_cgroup"))
>> +        goto cleanup;
>> +
>> +    server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
>
> nit. This binds to ADDR_ANY. Regardless, it is a good idea to create a 
> netns for this subtest. It will be one less thing to worry about when 
> removing "serial_" from this test eventually. The same for the subtest 
> in patch 5 also.

Good point. Will do.


>
>> +    if (!ASSERT_GE(server_fd, 0, "start_server"))
>> +        goto cleanup;
>> +
>> +    if (!ASSERT_EQ(bss->user_pid, pid, "pid"))
>> +        goto cleanup;
>> +    if (!ASSERT_EQ(bss->user_tgid, tgid, "tgid"))
>> +        goto cleanup;
>> +    ret = 0;
>> +
>> +cleanup:
>> +    if (server_fd >= 0)
>> +        close(server_fd);
>> +    test_ns_current_pid_tgid__destroy(skel);
>> +    return ret;
>> +}
>> +
>>   static void test_ns_current_pid_tgid_new_ns(int (*fn)(void *), void 
>> *arg)
>>   {
>>       int wstatus;
>> @@ -102,4 +147,13 @@ void serial_test_ns_current_pid_tgid(void)
>>           test_current_pid_tgid_tp(NULL);
>>       if (test__start_subtest("new_ns_tp"))
>> test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_tp, NULL);
>> +    if (test__start_subtest("new_ns_cgrp")) {
>> +        int cgroup_fd = -1;
>
> nit. no need to "= -1;"
Ack. My mistake. Will change.
>
> Other patches lgtm.
>
>> +
>> +        cgroup_fd = test__join_cgroup("/sock_addr");
>> +        if (ASSERT_GE(cgroup_fd, 0, "join_cgroup")) {
>> + test_ns_current_pid_tgid_new_ns(test_current_pid_tgid_cgrp, 
>> &cgroup_fd);
>> +            close(cgroup_fd);
>> +        }
>> +    }
>>   }
>> diff --git 
>> a/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c 
>> b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
>> index aa3ec7ca16d9..d0010e698f66 100644
>> --- a/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
>> +++ b/tools/testing/selftests/bpf/progs/test_ns_current_pid_tgid.c
>> @@ -28,4 +28,11 @@ int tp_handler(const void *ctx)
>>       return 0;
>>   }
>>   +SEC("?cgroup/bind4")
>> +int cgroup_bind4(struct bpf_sock_addr *ctx)
>> +{
>> +    get_pid_tgid();
>> +    return 1;
>> +}
>> +
>>   char _license[] SEC("license") = "GPL";
>

  reply	other threads:[~2024-03-09  0:04 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-07 23:26 [PATCH bpf-next 0/5] bpf: Allow helper bpf_get_ns_current_pid_tgid() in cgroup/sk_msg programs Yonghong Song
2024-03-07 23:27 ` [PATCH bpf-next 1/5] bpf: Allow helper bpf_get_ns_current_pid_tgid() in cgroup and sk_msg programs Yonghong Song
2024-03-09  1:06   ` Andrii Nakryiko
2024-03-09 18:39     ` Yonghong Song
2024-03-09 19:10       ` Alexei Starovoitov
2024-03-09 21:03         ` Yonghong Song
2024-03-07 23:27 ` [PATCH bpf-next 2/5] selftests/bpf: Replace CHECK with ASSERT_* in ns_current_pid_tgid test Yonghong Song
2024-03-07 23:27 ` [PATCH bpf-next 3/5] selftests/bpf: Refactor out some functions " Yonghong Song
2024-03-07 23:27 ` [PATCH bpf-next 4/5] selftests/bpf: Add a cgroup prog bpf_get_ns_current_pid_tgid() test Yonghong Song
2024-03-08 19:08   ` Martin KaFai Lau
2024-03-09  0:04     ` Yonghong Song [this message]
2024-03-07 23:27 ` [PATCH bpf-next 5/5] selftests/bpf: Add a sk_msg " Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ff89127f-2c2c-42db-948f-74d524243fc4@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=john.fastabend@gmail.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=ynamgung@meta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).