diff mbox series

[bpf,2/2] selftests: bpf: add test for skb_load_bytes

Message ID 20220411130255.385520-3-liujian56@huawei.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series Enlarge offset check value in bpf_skb_load_bytes | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf, async
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers warning 2 maintainers not CCed: linux-kselftest@vger.kernel.org shuah@kernel.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch warning CHECK: Alignment should match open parenthesis CHECK: Comparison to NULL could be written "!offset" WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? WARNING: line length of 85 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-PR success PR summary
bpf/vmtest-bpf-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest + selftests
bpf/vmtest-bpf-VM_Test-2 success Logs for Kernel LATEST on z15 + selftests

Commit Message

Liu Jian April 11, 2022, 1:02 p.m. UTC
Use bpf_prog_test_run_opts to test the skb_load_bytes function.
Tests behavior when offset is greater than INT_MAX or a normal value.

Signed-off-by: Liu Jian <liujian56@huawei.com>
---
 .../selftests/bpf/prog_tests/skb_load_bytes.c | 65 +++++++++++++++++++
 .../selftests/bpf/progs/skb_load_bytes.c      | 37 +++++++++++
 2 files changed, 102 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c
 create mode 100644 tools/testing/selftests/bpf/progs/skb_load_bytes.c

Comments

Song Liu April 11, 2022, 4:37 p.m. UTC | #1
> On Apr 11, 2022, at 6:02 AM, Liu Jian <liujian56@huawei.com> wrote:
> 
> Use bpf_prog_test_run_opts to test the skb_load_bytes function.
> Tests behavior when offset is greater than INT_MAX or a normal value.
> 
> Signed-off-by: Liu Jian <liujian56@huawei.com>
> ---
> .../selftests/bpf/prog_tests/skb_load_bytes.c | 65 +++++++++++++++++++
> .../selftests/bpf/progs/skb_load_bytes.c      | 37 +++++++++++
> 2 files changed, 102 insertions(+)
> create mode 100644 tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c
> create mode 100644 tools/testing/selftests/bpf/progs/skb_load_bytes.c
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c b/tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c
> new file mode 100644
> index 000000000000..2e86f81d85f1
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c
> @@ -0,0 +1,65 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include <test_progs.h>
> +#include <network_helpers.h>
> +
> +void test_skb_load_bytes(void)
> +{
> +	struct bpf_map *test_result;
> +
> +	__u32 map_key = 0;
> +	__u32 map_value = 0;
> +
> +	struct bpf_object *obj;
> +	int err, map_fd, prog_fd;
> +
> +	struct __sk_buff skb = { 0 };
> +
> +	LIBBPF_OPTS(bpf_test_run_opts, tattr,
> +			.data_in = &pkt_v4,
> +			.data_size_in = sizeof(pkt_v4),
> +			.ctx_in = &skb,
> +			.ctx_size_in = sizeof(skb),
> +		   );
> +
> +	err = bpf_prog_test_load("./skb_load_bytes.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
> +			&prog_fd);

Why don't we use bpf skeleton? 

> +	if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
> +		return;
> +
> +	test_result = bpf_object__find_map_by_name(obj, "test_result");
> +	if (CHECK_FAIL(!test_result))
> +		goto close_bpf_object;
> +
> +	map_fd = bpf_map__fd(test_result);
> +	if (map_fd < 0)
> +		goto close_bpf_object;
> +
> +	map_key = 0;
> +	map_value = -1;
> +	err = bpf_map_update_elem(map_fd, &map_key, &map_value, BPF_ANY);
> +	if (CHECK_FAIL(err))
> +		goto close_bpf_object;
> +	tattr.data_out = NULL;
> +	tattr.data_size_out = 0;
> +	err = bpf_prog_test_run_opts(prog_fd, &tattr);
> +	CHECK_ATTR(err != 0, "offset -1", "err %d errno %d\n", err, errno);
> +	map_key = 1;
> +	bpf_map_lookup_elem(map_fd, &map_key, &map_value);
> +	CHECK_ATTR(map_value != -14, "offset -1", "get result error\n");
> +
> +	map_key = 0;
> +	map_value = 10;
> +	err = bpf_map_update_elem(map_fd, &map_key, &map_value, BPF_ANY);
> +	if (CHECK_FAIL(err))
> +		goto close_bpf_object;
> +	tattr.data_out = NULL;
> +	tattr.data_size_out = 0;
> +	err = bpf_prog_test_run_opts(prog_fd, &tattr);
> +	CHECK_ATTR(err != 0, "offset 10", "err %d errno %d\n", err, errno);
> +	map_key = 1;
> +	bpf_map_lookup_elem(map_fd, &map_key, &map_value);
> +	CHECK_ATTR(map_value != 0, "offset 10", "get result error\n");
> +
> +close_bpf_object:
> +	bpf_object__close(obj);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/skb_load_bytes.c b/tools/testing/selftests/bpf/progs/skb_load_bytes.c
> new file mode 100644
> index 000000000000..1652540aa45d
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/skb_load_bytes.c
> @@ -0,0 +1,37 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <linux/bpf.h>
> +#include <bpf/bpf_helpers.h>
> +
> +char _license[] SEC("license") = "GPL";
> +
> +struct {
> +	__uint(type, BPF_MAP_TYPE_ARRAY);
> +	__uint(max_entries, 2);
> +	__type(key, __u32);
> +	__type(value, __u32);
> +} test_result SEC(".maps");

We can simplify the code with BPF global variable for these. It is 
technical equivalent to a map, but much easier to use. 

> +
> +SEC("tc")
> +int skb_process(struct __sk_buff *skb)
> +{
> +	char buf[16];
> +	int ret = 0;
> +	__u32 map_key = 0;
> +	__u32 *offset = NULL;
> +
> +	offset = bpf_map_lookup_elem(&test_result, &map_key);
> +	if (offset == NULL) {
> +		bpf_printk("get offset failed\n");

bpf_printk doesn't add much value in selftests. 

> +		map_key = 1;
> +		ret = -1;
> +		bpf_map_update_elem(&test_result, &map_key, &ret, BPF_ANY);
> +		return ret;
> +	}
> +
> +	ret = bpf_skb_load_bytes(skb, *offset, buf, 10);
> +	map_key = 1;
> +	bpf_map_update_elem(&test_result, &map_key, &ret, BPF_ANY);
> +
> +	return 0;
> +}
> -- 
> 2.17.1
>
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c b/tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c
new file mode 100644
index 000000000000..2e86f81d85f1
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/skb_load_bytes.c
@@ -0,0 +1,65 @@ 
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include <network_helpers.h>
+
+void test_skb_load_bytes(void)
+{
+	struct bpf_map *test_result;
+
+	__u32 map_key = 0;
+	__u32 map_value = 0;
+
+	struct bpf_object *obj;
+	int err, map_fd, prog_fd;
+
+	struct __sk_buff skb = { 0 };
+
+	LIBBPF_OPTS(bpf_test_run_opts, tattr,
+			.data_in = &pkt_v4,
+			.data_size_in = sizeof(pkt_v4),
+			.ctx_in = &skb,
+			.ctx_size_in = sizeof(skb),
+		   );
+
+	err = bpf_prog_test_load("./skb_load_bytes.o", BPF_PROG_TYPE_SCHED_CLS, &obj,
+			&prog_fd);
+	if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
+		return;
+
+	test_result = bpf_object__find_map_by_name(obj, "test_result");
+	if (CHECK_FAIL(!test_result))
+		goto close_bpf_object;
+
+	map_fd = bpf_map__fd(test_result);
+	if (map_fd < 0)
+		goto close_bpf_object;
+
+	map_key = 0;
+	map_value = -1;
+	err = bpf_map_update_elem(map_fd, &map_key, &map_value, BPF_ANY);
+	if (CHECK_FAIL(err))
+		goto close_bpf_object;
+	tattr.data_out = NULL;
+	tattr.data_size_out = 0;
+	err = bpf_prog_test_run_opts(prog_fd, &tattr);
+	CHECK_ATTR(err != 0, "offset -1", "err %d errno %d\n", err, errno);
+	map_key = 1;
+	bpf_map_lookup_elem(map_fd, &map_key, &map_value);
+	CHECK_ATTR(map_value != -14, "offset -1", "get result error\n");
+
+	map_key = 0;
+	map_value = 10;
+	err = bpf_map_update_elem(map_fd, &map_key, &map_value, BPF_ANY);
+	if (CHECK_FAIL(err))
+		goto close_bpf_object;
+	tattr.data_out = NULL;
+	tattr.data_size_out = 0;
+	err = bpf_prog_test_run_opts(prog_fd, &tattr);
+	CHECK_ATTR(err != 0, "offset 10", "err %d errno %d\n", err, errno);
+	map_key = 1;
+	bpf_map_lookup_elem(map_fd, &map_key, &map_value);
+	CHECK_ATTR(map_value != 0, "offset 10", "get result error\n");
+
+close_bpf_object:
+	bpf_object__close(obj);
+}
diff --git a/tools/testing/selftests/bpf/progs/skb_load_bytes.c b/tools/testing/selftests/bpf/progs/skb_load_bytes.c
new file mode 100644
index 000000000000..1652540aa45d
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/skb_load_bytes.c
@@ -0,0 +1,37 @@ 
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(max_entries, 2);
+	__type(key, __u32);
+	__type(value, __u32);
+} test_result SEC(".maps");
+
+SEC("tc")
+int skb_process(struct __sk_buff *skb)
+{
+	char buf[16];
+	int ret = 0;
+	__u32 map_key = 0;
+	__u32 *offset = NULL;
+
+	offset = bpf_map_lookup_elem(&test_result, &map_key);
+	if (offset == NULL) {
+		bpf_printk("get offset failed\n");
+		map_key = 1;
+		ret = -1;
+		bpf_map_update_elem(&test_result, &map_key, &ret, BPF_ANY);
+		return ret;
+	}
+
+	ret = bpf_skb_load_bytes(skb, *offset, buf, 10);
+	map_key = 1;
+	bpf_map_update_elem(&test_result, &map_key, &ret, BPF_ANY);
+
+	return 0;
+}