OSDN Git Service

bpf: Add bpf_for_each_map_elem() helper
authorYonghong Song <yhs@fb.com>
Fri, 26 Feb 2021 20:49:25 +0000 (12:49 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 26 Feb 2021 21:23:52 +0000 (13:23 -0800)
commit69c087ba6225b574afb6e505b72cb75242a3d844
treef073f2287c054e09ece952a4e408a43e368c5925
parent282a0f46d6cda7cf843cd77c9b53b4d1d9e31302
bpf: Add bpf_for_each_map_elem() helper

The bpf_for_each_map_elem() helper is introduced which
iterates all map elements with a callback function. The
helper signature looks like
  long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
and for each map element, the callback_fn will be called. For example,
like hashmap, the callback signature may look like
  long callback_fn(map, key, val, callback_ctx)

There are two known use cases for this. One is from upstream ([1]) where
a for_each_map_elem helper may help implement a timeout mechanism
in a more generic way. Another is from our internal discussion
for a firewall use case where a map contains all the rules. The packet
data can be compared to all these rules to decide allow or deny
the packet.

For array maps, users can already use a bounded loop to traverse
elements. Using this helper can avoid using bounded loop. For other
type of maps (e.g., hash maps) where bounded loop is hard or
impossible to use, this helper provides a convenient way to
operate on all elements.

For callback_fn, besides map and map element, a callback_ctx,
allocated on caller stack, is also passed to the callback
function. This callback_ctx argument can provide additional
input and allow to write to caller stack for output.

If the callback_fn returns 0, the helper will iterate through next
element if available. If the callback_fn returns 1, the helper
will stop iterating and returns to the bpf program. Other return
values are not used for now.

Currently, this helper is only available with jit. It is possible
to make it work with interpreter with so effort but I leave it
as the future work.

[1]: https://lore.kernel.org/bpf/20210122205415.113822-1-xiyou.wangcong@gmail.com/

Signed-off-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210226204925.3884923-1-yhs@fb.com
include/linux/bpf.h
include/linux/bpf_verifier.h
include/uapi/linux/bpf.h
kernel/bpf/bpf_iter.c
kernel/bpf/helpers.c
kernel/bpf/verifier.c
kernel/trace/bpf_trace.c
tools/include/uapi/linux/bpf.h