bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stanislav Fomichev <sdf@google.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Networking <netdev@vger.kernel.org>, bpf <bpf@vger.kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: Re: [PATCH bpf-next 4/4] selftests/bpf: test BPF_CGROUP_INET_SOCK_RELEASE
Date: Fri, 26 Jun 2020 16:52:01 -0700	[thread overview]
Message-ID: <CAKH8qBtKOrOg5-4KgS2ZqkvWApdM19rqe-BNB4_kes_VzJtWGQ@mail.gmail.com> (raw)
In-Reply-To: <CAEf4BzaeEKvw0S5oMe7N+mUOyeEzaU3bPbaMPtMXrQ1CnVHXBw@mail.gmail.com>

On Fri, Jun 26, 2020 at 3:06 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Jun 25, 2020 at 5:13 PM Stanislav Fomichev <sdf@google.com> wrote:
> >
> > Simple test that enforces a single SOCK_DGRAM socker per cgroup.
> >
> > Signed-off-by: Stanislav Fomichev <sdf@google.com>
> > ---
> >  .../selftests/bpf/prog_tests/udp_limit.c      | 71 +++++++++++++++++++
> >  tools/testing/selftests/bpf/progs/udp_limit.c | 42 +++++++++++
> >  2 files changed, 113 insertions(+)
> >  create mode 100644 tools/testing/selftests/bpf/prog_tests/udp_limit.c
> >  create mode 100644 tools/testing/selftests/bpf/progs/udp_limit.c
> >
> > diff --git a/tools/testing/selftests/bpf/prog_tests/udp_limit.c b/tools/testing/selftests/bpf/prog_tests/udp_limit.c
> > new file mode 100644
> > index 000000000000..fe359a927d92
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/prog_tests/udp_limit.c
> > @@ -0,0 +1,71 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +#include <test_progs.h>
> > +#include "udp_limit.skel.h"
> > +
> > +#include <sys/types.h>
> > +#include <sys/socket.h>
> > +
> > +void test_udp_limit(void)
> > +{
> > +       struct udp_limit *skel;
> > +       int cgroup_fd;
> > +       int fd1, fd2;
> > +       int err;
> > +
> > +       cgroup_fd = test__join_cgroup("/udp_limit");
> > +       if (CHECK_FAIL(cgroup_fd < 0))
> > +               return;
> > +
> > +       skel = udp_limit__open_and_load();
> > +       if (CHECK_FAIL(!skel))
> > +               goto close_cgroup_fd;
> > +
> > +       err = bpf_prog_attach(bpf_program__fd(skel->progs.sock),
> > +                             cgroup_fd, BPF_CGROUP_INET_SOCK_CREATE, 0);
> > +       if (CHECK_FAIL(err))
> > +               goto close_skeleton;
> > +
> > +       err = bpf_prog_attach(bpf_program__fd(skel->progs.sock_release),
> > +                             cgroup_fd, BPF_CGROUP_INET_SOCK_RELEASE, 0);
> > +       if (CHECK_FAIL(err))
> > +               goto close_skeleton;
>
> Have you tried:
>
> skel->links.sock = bpf_program__attach_cgroup(skel->progs.sock);
>
> and similarly for sock_release?
Ack, I can try that, thanks!

> > +       /* BPF program enforces a single UDP socket per cgroup,
> > +        * verify that.
> > +        */
> > +       fd1 = socket(AF_INET, SOCK_DGRAM, 0);
> > +       if (CHECK_FAIL(fd1 < 0))
> > +               goto close_skeleton;
> > +
> > +       fd2 = socket(AF_INET, SOCK_DGRAM, 0);
> > +       if (CHECK_FAIL(fd2 != -1))
> > +               goto close_fd1;
> > +
> > +       /* We can reopen again after close. */
> > +       close(fd1);
> > +
> > +       fd1 = socket(AF_INET, SOCK_DGRAM, 0);
> > +       if (CHECK_FAIL(fd1 < 0))
> > +               goto close_skeleton;
> > +
> > +       /* Make sure the program was invoked the expected
> > +        * number of times:
> > +        * - open fd1           - BPF_CGROUP_INET_SOCK_CREATE
> > +        * - attempt to openfd2 - BPF_CGROUP_INET_SOCK_CREATE
> > +        * - close fd1          - BPF_CGROUP_INET_SOCK_RELEASE
> > +        * - open fd1 again     - BPF_CGROUP_INET_SOCK_CREATE
> > +        */
> > +       if (CHECK_FAIL(skel->bss->invocations != 4))
> > +               goto close_fd1;
> > +
> > +       /* We should still have a single socket in use */
> > +       if (CHECK_FAIL(skel->bss->in_use != 1))
> > +               goto close_fd1;
>
> Please use a non-silent CHECK() macro for everything that's a proper
> and not a high-frequency check. That generates "a log trail" when
> running the test in verbose mode, so it's easier to pinpoint where the
> failure happened.
IIRC, the problem with CHECK() is that it requires a 'duration'
argument to be defined.
Do you suggest defining it somewhere just to make CHECK() happy?

> > +
> > +close_fd1:
> > +       close(fd1);
> > +close_skeleton:
> > +       udp_limit__destroy(skel);
> > +close_cgroup_fd:
> > +       close(cgroup_fd);
> > +}
> > diff --git a/tools/testing/selftests/bpf/progs/udp_limit.c b/tools/testing/selftests/bpf/progs/udp_limit.c
> > new file mode 100644
> > index 000000000000..98fe294d9c21
> > --- /dev/null
> > +++ b/tools/testing/selftests/bpf/progs/udp_limit.c
> > @@ -0,0 +1,42 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +
> > +#include <sys/socket.h>
> > +#include <linux/bpf.h>
> > +#include <bpf/bpf_helpers.h>
> > +
> > +int invocations, in_use;
> > +
> > +SEC("cgroup/sock")
> > +int sock(struct bpf_sock *ctx)
> > +{
> > +       __u32 key;
> > +
> > +       if (ctx->type != SOCK_DGRAM)
> > +               return 1;
> > +
> > +       __sync_fetch_and_add(&invocations, 1);
> > +
> > +       if (&in_use > 0) {
>
>
> &in_use is supposed to return an address of a variable... this looks
> weird and probably not what you wanted?
Oh, good catch! I was about to ask myself "how did the test pass with
that?", but the test fails as well :-/
Not sure how it creeped in and how I ran my tests, sorry about that.

  reply	other threads:[~2020-06-26 23:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-26  0:09 [PATCH bpf-next 1/4] bpf: add BPF_CGROUP_INET_SOCK_RELEASE hook Stanislav Fomichev
2020-06-26  0:09 ` [PATCH bpf-next 2/4] libbpf: add support for BPF_CGROUP_INET_SOCK_RELEASE Stanislav Fomichev
2020-06-26 22:02   ` Andrii Nakryiko
2020-06-26 23:51     ` Stanislav Fomichev
2020-06-26  0:09 ` [PATCH bpf-next 3/4] bpftool: support BPF_CGROUP_INET_SOCK_RELEASE Stanislav Fomichev
2020-06-26  0:09 ` [PATCH bpf-next 4/4] selftests/bpf: test BPF_CGROUP_INET_SOCK_RELEASE Stanislav Fomichev
2020-06-26 22:06   ` Andrii Nakryiko
2020-06-26 23:52     ` Stanislav Fomichev [this message]
2020-06-27  0:57       ` Andrii Nakryiko
2020-06-26  2:30 ` [PATCH bpf-next 1/4] bpf: add BPF_CGROUP_INET_SOCK_RELEASE hook kernel test robot
2020-06-26  7:00 ` kernel test robot

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=CAKH8qBtKOrOg5-4KgS2ZqkvWApdM19rqe-BNB4_kes_VzJtWGQ@mail.gmail.com \
    --to=sdf@google.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=netdev@vger.kernel.org \
    /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).