Add a tc-based helper program that updates a map value containing both
bpf_spin_lock and bpf_timer and records errors for invalid flag
combinations.

Extend the existing map_lock tests to cover array and hash maps. Verify
that BPF_NOEXIST|BPF_EXIST is rejected with -EINVAL/-EEXIST, and that
BPF_F_LOCK returns -EOPNOTSUPP when mixed with other special fields.

Signed-off-by: Leon Hwang <[email protected]>
---
 .../selftests/bpf/prog_tests/map_lock.c       | 70 +++++++++++++++++++
 .../selftests/bpf/progs/test_map_lock.c       | 31 +++++++-
 2 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/map_lock.c 
b/tools/testing/selftests/bpf/prog_tests/map_lock.c
index 1d6726f01dd2..aafae8d02e1d 100644
--- a/tools/testing/selftests/bpf/prog_tests/map_lock.c
+++ b/tools/testing/selftests/bpf/prog_tests/map_lock.c
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <test_progs.h>
 #include <network_helpers.h>
+#include "test_map_lock.skel.h"
 
 static void *spin_lock_thread(void *arg)
 {
@@ -90,3 +91,72 @@ void test_map_lock(void)
 close_prog:
        bpf_object__close(obj);
 }
+
+struct map_value {
+       struct bpf_spin_lock lock;
+       struct bpf_timer timer;
+       __u64 payload;
+};
+
+static void test_map_lock_update_elem(enum bpf_map_type map_type, int 
err_exist)
+{
+       struct map_value val = {};
+       struct test_map_lock *skel;
+       int prog_fd, err;
+       u32 key = 0;
+       char buff[128] = {};
+       LIBBPF_OPTS(bpf_test_run_opts, topts,
+                   .data_in = buff,
+                   .data_size_in = sizeof(buff),
+                   .repeat = 1,
+       );
+
+       skel = test_map_lock__open();
+       if (!ASSERT_OK_PTR(skel, "test_map_lock__open"))
+               return;
+
+       bpf_map__set_type(skel->maps.map, map_type);
+
+       err = test_map_lock__load(skel);
+       if (!ASSERT_OK(err, "test_map_lock__load"))
+               goto out;
+
+       err = bpf_map__update_elem(skel->maps.map, &key, sizeof(key), &val, 
sizeof(val),
+                                  BPF_NOEXIST | BPF_EXIST);
+       if (!ASSERT_EQ(err, -EINVAL, "err_exist"))
+               goto out;
+
+       err = bpf_map__update_elem(skel->maps.map, &key, sizeof(key), &val, 
sizeof(val),
+                                  BPF_F_LOCK);
+       if (!ASSERT_EQ(err, -EOPNOTSUPP, "err_lock"))
+               goto out;
+
+       prog_fd = bpf_program__fd(skel->progs.map_update);
+       err = bpf_prog_test_run_opts(prog_fd, &topts);
+       if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
+               goto out;
+
+       ASSERT_EQ(skel->bss->err_exist, err_exist, "err_exist");
+       ASSERT_EQ(skel->bss->err_lock, -EOPNOTSUPP, "err_lock");
+
+out:
+       test_map_lock__destroy(skel);
+}
+
+static void test_array_map_lock_update_elem(void)
+{
+       test_map_lock_update_elem(BPF_MAP_TYPE_ARRAY, -EEXIST);
+}
+
+static void test_hash_map_lock_update_elem(void)
+{
+       test_map_lock_update_elem(BPF_MAP_TYPE_HASH, -EINVAL);
+}
+
+void test_map_lock_flag(void)
+{
+       if (test__start_subtest("array_map"))
+               test_array_map_lock_update_elem();
+       if (test__start_subtest("hash_map"))
+               test_hash_map_lock_update_elem();
+}
diff --git a/tools/testing/selftests/bpf/progs/test_map_lock.c 
b/tools/testing/selftests/bpf/progs/test_map_lock.c
index 1c02511b73cd..f1b7b741795c 100644
--- a/tools/testing/selftests/bpf/progs/test_map_lock.c
+++ b/tools/testing/selftests/bpf/progs/test_map_lock.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 // Copyright (c) 2019 Facebook
-#include <linux/bpf.h>
+#include "vmlinux.h"
 #include <linux/version.h>
 #include <bpf/bpf_helpers.h>
 
@@ -59,4 +59,33 @@ int bpf_map_lock_test(struct __sk_buff *skb)
 err:
        return err;
 }
+
+int err_exist;
+int err_lock;
+
+struct map_value {
+       struct bpf_spin_lock lock;
+       struct bpf_timer timer;
+       __u64 payload;
+};
+
+struct {
+       __uint(type, BPF_MAP_TYPE_ARRAY);
+       __type(key, u32);
+       __type(value, struct map_value);
+       __uint(max_entries, 1);
+} map SEC(".maps");
+
+SEC("tc")
+int map_update(struct __sk_buff *skb)
+{
+       struct map_value val = {};
+       u32 key = 0;
+
+       val.payload = 0xDEADBEEF;
+       err_exist = bpf_map_update_elem(&map, &key, &val, BPF_NOEXIST | 
BPF_EXIST);
+       err_lock = bpf_map_update_elem(&map, &key, &val, BPF_F_LOCK);
+       return BPF_OK;
+}
+
 char _license[] SEC("license") = "GPL";
-- 
2.52.0


Reply via email to