diff mbox series

[bpf-next,v3,5/5] bpf: selftests, test probe_* helpers from SCHED_CLS

Message ID 159007177838.10695.12211214514015683724.stgit@john-Precision-5820-Tower
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series bpf: Add sk_msg and networking helpers | expand

Commit Message

John Fastabend May 21, 2020, 2:36 p.m. UTC
Lets test using probe* in SCHED_CLS network programs as well just
to be sure these keep working. Its cheap to add the extra test
and provides a second context to test outside of sk_msg after
we generalized probe* helpers to all networking types.

Signed-off-by: John Fastabend <john.fastabend@gmail.com>
---
 .../testing/selftests/bpf/prog_tests/skb_helpers.c |   30 ++++++++++++++++++
 .../testing/selftests/bpf/progs/test_skb_helpers.c |   33 ++++++++++++++++++++
 2 files changed, 63 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/skb_helpers.c
 create mode 100644 tools/testing/selftests/bpf/progs/test_skb_helpers.c

Comments

Yonghong Song May 21, 2020, 6:32 p.m. UTC | #1
On 5/21/20 7:36 AM, John Fastabend wrote:
> Lets test using probe* in SCHED_CLS network programs as well just
> to be sure these keep working. Its cheap to add the extra test
> and provides a second context to test outside of sk_msg after
> we generalized probe* helpers to all networking types.
> 
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>

Ack with a minor nit below.

Acked-by: Yonghong Song <yhs@fb.com>

> ---
>   .../testing/selftests/bpf/prog_tests/skb_helpers.c |   30 ++++++++++++++++++
>   .../testing/selftests/bpf/progs/test_skb_helpers.c |   33 ++++++++++++++++++++
>   2 files changed, 63 insertions(+)
>   create mode 100644 tools/testing/selftests/bpf/prog_tests/skb_helpers.c
>   create mode 100644 tools/testing/selftests/bpf/progs/test_skb_helpers.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/skb_helpers.c b/tools/testing/selftests/bpf/prog_tests/skb_helpers.c
> new file mode 100644
> index 0000000..5a865c4
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/skb_helpers.c
> @@ -0,0 +1,30 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +#include <network_helpers.h>
> +
> +void test_skb_helpers(void)
> +{
> +	struct __sk_buff skb = {
> +		.wire_len = 100,
> +		.gso_segs = 8,
> +		.gso_size = 10,
> +	};
> +	struct bpf_prog_test_run_attr tattr = {
> +		.data_in = &pkt_v4,
> +		.data_size_in = sizeof(pkt_v4),
> +		.ctx_in = &skb,
> +		.ctx_size_in = sizeof(skb),
> +		.ctx_out = &skb,
> +		.ctx_size_out = sizeof(skb),
> +	};
> +	struct bpf_object *obj;
> +	int err;
> +
> +	err = bpf_prog_load("./test_skb_helpers.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
> +			    &tattr.prog_fd);
> +	if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
> +		return;
> +
> +	err = bpf_prog_test_run_xattr(&tattr);
> +	CHECK_ATTR(err != 0, "len", "err %d errno %d\n", err, errno);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_skb_helpers.c b/tools/testing/selftests/bpf/progs/test_skb_helpers.c
> new file mode 100644
> index 0000000..05a1260
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_skb_helpers.c
> @@ -0,0 +1,33 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_endian.h>
> +
> +int _version SEC("version") = 1;
> +
> +#define TEST_COMM_LEN 10
> +
> +struct bpf_map_def SEC("maps") cgroup_map = {
> +	.type			= BPF_MAP_TYPE_CGROUP_ARRAY,
> +	.key_size		= sizeof(u32),
> +	.value_size		= sizeof(u32),
> +	.max_entries	= 1,
> +};
> +
> +char _license[] SEC("license") = "GPL";
> +
> +SEC("classifier/test_skb_helpers")
> +int test_skb_helpers(struct __sk_buff *skb)
> +{
> +	struct task_struct *task;
> +	char *comm[TEST_COMM_LEN];
> +	__u32 tpid;
> +	int ctask;
> +
> +	ctask = bpf_current_task_under_cgroup(&cgroup_map, 0);

ctask is not used. Could you test ctask against expected value?

> +	task = (struct task_struct *)bpf_get_current_task();
> +
> +	bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
> +	bpf_probe_read_kernel_str(&comm, sizeof(comm), &task->comm);
> +	return 0;
> +}
>
Andrii Nakryiko May 21, 2020, 6:47 p.m. UTC | #2
On Thu, May 21, 2020 at 7:36 AM John Fastabend <john.fastabend@gmail.com> wrote:
>
> Lets test using probe* in SCHED_CLS network programs as well just
> to be sure these keep working. Its cheap to add the extra test
> and provides a second context to test outside of sk_msg after
> we generalized probe* helpers to all networking types.
>
> Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> ---
>  .../testing/selftests/bpf/prog_tests/skb_helpers.c |   30 ++++++++++++++++++
>  .../testing/selftests/bpf/progs/test_skb_helpers.c |   33 ++++++++++++++++++++
>  2 files changed, 63 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/skb_helpers.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_skb_helpers.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/skb_helpers.c b/tools/testing/selftests/bpf/prog_tests/skb_helpers.c
> new file mode 100644
> index 0000000..5a865c4
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/skb_helpers.c
> @@ -0,0 +1,30 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +#include <network_helpers.h>
> +
> +void test_skb_helpers(void)
> +{
> +       struct __sk_buff skb = {
> +               .wire_len = 100,
> +               .gso_segs = 8,
> +               .gso_size = 10,
> +       };
> +       struct bpf_prog_test_run_attr tattr = {
> +               .data_in = &pkt_v4,
> +               .data_size_in = sizeof(pkt_v4),
> +               .ctx_in = &skb,
> +               .ctx_size_in = sizeof(skb),
> +               .ctx_out = &skb,
> +               .ctx_size_out = sizeof(skb),
> +       };
> +       struct bpf_object *obj;
> +       int err;
> +
> +       err = bpf_prog_load("./test_skb_helpers.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
> +                           &tattr.prog_fd);

hm... who's destroying bpf_object?


> +       if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
> +               return;
> +
> +       err = bpf_prog_test_run_xattr(&tattr);
> +       CHECK_ATTR(err != 0, "len", "err %d errno %d\n", err, errno);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_skb_helpers.c b/tools/testing/selftests/bpf/progs/test_skb_helpers.c
> new file mode 100644
> index 0000000..05a1260
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_skb_helpers.c
> @@ -0,0 +1,33 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_endian.h>
> +
> +int _version SEC("version") = 1;

version is not needed

> +
> +#define TEST_COMM_LEN 10

doesn't matter for this test, but it's 16 everywhere, let's stay consistent

> +
> +struct bpf_map_def SEC("maps") cgroup_map = {
> +       .type                   = BPF_MAP_TYPE_CGROUP_ARRAY,
> +       .key_size               = sizeof(u32),
> +       .value_size             = sizeof(u32),
> +       .max_entries    = 1,
> +};
> +

Please use new BTF syntax for maps

> +char _license[] SEC("license") = "GPL";
> +
> +SEC("classifier/test_skb_helpers")
> +int test_skb_helpers(struct __sk_buff *skb)
> +{
> +       struct task_struct *task;
> +       char *comm[TEST_COMM_LEN];

this is array of pointer, not array of chars

> +       __u32 tpid;
> +       int ctask;
> +
> +       ctask = bpf_current_task_under_cgroup(&cgroup_map, 0);

compiler might complain that ctask is written, but not read. Let's
assign it to some global variable?

> +       task = (struct task_struct *)bpf_get_current_task();
> +
> +       bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
> +       bpf_probe_read_kernel_str(&comm, sizeof(comm), &task->comm);
> +       return 0;
> +}
>
John Fastabend May 21, 2020, 7:11 p.m. UTC | #3
Andrii Nakryiko wrote:
> On Thu, May 21, 2020 at 7:36 AM John Fastabend <john.fastabend@gmail.com> wrote:
> >
> > Lets test using probe* in SCHED_CLS network programs as well just
> > to be sure these keep working. Its cheap to add the extra test
> > and provides a second context to test outside of sk_msg after
> > we generalized probe* helpers to all networking types.
> >
> > Signed-off-by: John Fastabend <john.fastabend@gmail.com>
> > ---

[...]

> > +++ b/tools/testing/selftests/bpf/progs/test_skb_helpers.c
> > @@ -0,0 +1,33 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +#include "vmlinux.h"
> > +#include <bpf/bpf_helpers.h>
> > +#include <bpf/bpf_endian.h>
> > +
> > +int _version SEC("version") = 1;
> 
> version is not needed
> 
> > +
> > +#define TEST_COMM_LEN 10
> 
> doesn't matter for this test, but it's 16 everywhere, let's stay consistent
> 
> > +
> > +struct bpf_map_def SEC("maps") cgroup_map = {
> > +       .type                   = BPF_MAP_TYPE_CGROUP_ARRAY,
> > +       .key_size               = sizeof(u32),
> > +       .value_size             = sizeof(u32),
> > +       .max_entries    = 1,
> > +};
> > +
> 
> Please use new BTF syntax for maps
> 
> > +char _license[] SEC("license") = "GPL";
> > +
> > +SEC("classifier/test_skb_helpers")
> > +int test_skb_helpers(struct __sk_buff *skb)
> > +{
> > +       struct task_struct *task;
> > +       char *comm[TEST_COMM_LEN];
> 
> this is array of pointer, not array of chars
> 
> > +       __u32 tpid;
> > +       int ctask;
> > +
> > +       ctask = bpf_current_task_under_cgroup(&cgroup_map, 0);
> 
> compiler might complain that ctask is written, but not read. Let's
> assign it to some global variable?

I'll do a read here and check the value then it should be fine.

Will fold in the above comments as well.

> > +       task = (struct task_struct *)bpf_get_current_task();
> > +
> > +       bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
> > +       bpf_probe_read_kernel_str(&comm, sizeof(comm), &task->comm);
> > +       return 0;
> > +}
> >
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/skb_helpers.c b/tools/testing/selftests/bpf/prog_tests/skb_helpers.c
new file mode 100644
index 0000000..5a865c4
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/skb_helpers.c
@@ -0,0 +1,30 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+
+void test_skb_helpers(void)
+{
+	struct __sk_buff skb = {
+		.wire_len = 100,
+		.gso_segs = 8,
+		.gso_size = 10,
+	};
+	struct bpf_prog_test_run_attr tattr = {
+		.data_in = &pkt_v4,
+		.data_size_in = sizeof(pkt_v4),
+		.ctx_in = &skb,
+		.ctx_size_in = sizeof(skb),
+		.ctx_out = &skb,
+		.ctx_size_out = sizeof(skb),
+	};
+	struct bpf_object *obj;
+	int err;
+
+	err = bpf_prog_load("./test_skb_helpers.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
+			    &tattr.prog_fd);
+	if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
+		return;
+
+	err = bpf_prog_test_run_xattr(&tattr);
+	CHECK_ATTR(err != 0, "len", "err %d errno %d\n", err, errno);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_skb_helpers.c b/tools/testing/selftests/bpf/progs/test_skb_helpers.c
new file mode 100644
index 0000000..05a1260
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_skb_helpers.c
@@ -0,0 +1,33 @@ 
+// SPDX-License-Identifier: GPL-2.0-only
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_endian.h>
+
+int _version SEC("version") = 1;
+
+#define TEST_COMM_LEN 10
+
+struct bpf_map_def SEC("maps") cgroup_map = {
+	.type			= BPF_MAP_TYPE_CGROUP_ARRAY,
+	.key_size		= sizeof(u32),
+	.value_size		= sizeof(u32),
+	.max_entries	= 1,
+};
+
+char _license[] SEC("license") = "GPL";
+
+SEC("classifier/test_skb_helpers")
+int test_skb_helpers(struct __sk_buff *skb)
+{
+	struct task_struct *task;
+	char *comm[TEST_COMM_LEN];
+	__u32 tpid;
+	int ctask;
+
+	ctask = bpf_current_task_under_cgroup(&cgroup_map, 0);
+	task = (struct task_struct *)bpf_get_current_task();
+
+	bpf_probe_read_kernel(&tpid , sizeof(tpid), &task->tgid);
+	bpf_probe_read_kernel_str(&comm, sizeof(comm), &task->comm);
+	return 0;
+}