libbpf: introduce bpf_map_lookup_elem_flags()
Subsystems (6)
Alexei Starovoitov
maintainerPATCH v4 8/9 libbpf: introduce bpf_map_lookup_elem_flags()
Introduce
int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, __u64 flags) helper to lookup array/hash/cgroup_local_storage elements with BPF_F_LOCK flag.
Signed-off-by Alexei Starovoitov <[email protected]>
--- tools/lib/bpf/bpf.c | 13 +++++++++++++ tools/lib/bpf/bpf.h | 2 ++ tools/lib/bpf/libbpf.map | 1 + 3 files changed, 16 insertions(+)
tools/lib/bpf/bpf.c +13 -0
@@ -357,6 +357,19 @@ int bpf_map_lookup_elem(int fd, const void *key, void *value)return sys_bpf (BPF_MAP_LOOKUP_ELEM , &attr,sizeof (attr)); } +int bpf_map_lookup_elem_flags (int fd,const void *key,void *value,__u64 flags) +{ +union bpf_attr attr; + +bzero (&attr,sizeof (attr)); + attr.map_fd = fd; + attr.key =ptr_to_u64 (key); + attr.value =ptr_to_u64 (value); + attr.flags = flags; + +return sys_bpf (BPF_MAP_LOOKUP_ELEM , &attr,sizeof (attr)); +} +int bpf_map_lookup_and_delete_elem (int fd,const void *key,void *value) {union bpf_attr attr;
tools/lib/bpf/bpf.h +2 -0
@@ -110,6 +110,8 @@ LIBBPF_API int bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags); LIBBPF_API int bpf_map_lookup_elem(int fd, const void *key, void *value); +LIBBPF_API int bpf_map_lookup_elem_flags(int fd, const void *key, void *value, +__u64 flags); LIBBPF_API int bpf_map_lookup_and_delete_elem(int fd, const void *key,void *value); LIBBPF_API int bpf_map_delete_elem(int fd, const void *key);
tools/lib/bpf/libbpf.map +1 -0
@@ -130,4 +130,5 @@ LIBBPF_0.0.2 { bpf_probe_helper; bpf_probe_map_type; bpf_probe_prog_type; + bpf_map_lookup_elem_flags; } LIBBPF_0.0.1;
Signature · 1 line
2.17.1
Alexei Starovoitov
maintainerPATCH v4 9/9 selftests/bpf: test for BPF_F_LOCK
Add C based test that runs 4 bpf programs in parallel
that update the same hash and array maps.
And another 2 threads that read from these two maps
via lookup(key, value, BPF_F_LOCK) api
to make sure the user space sees consistent value in both hash and array elements while user space races with kernel bpf progs.
Signed-off-by Alexei Starovoitov <[email protected]>
--- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/test_map_lock.c | 66 ++++++++++++++++++ tools/testing/selftests/bpf/test_progs.c | 74 +++++++++++++++++++++ 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_map_lock.c
tools/testing/selftests/bpf/Makefile +1 -1
@@ -39,7 +39,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \ test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o \ test_sk_lookup_kern.o test_xdp_vlan.o test_queue_map.o test_stack_map.o \ - xdp_dummy.o test_map_in_map.o test_spin_lock.o + xdp_dummy.o test_map_in_map.o test_spin_lock.o test_map_lock.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
tools/testing/selftests/bpf/test_map_lock.c new file +66 -0
66 lines not on this page · raw message
tools/testing/selftests/bpf/test_progs.c +74 -0
86 lines not on this page · raw message
Signature · 1 line
2.17.1