Helper function bpf_for_each_map_elem
For each element in map
, call callback_fn
function with map
, callback_ctx
and other map-specific parameters.
Definition
Copyright (c) 2015 The Libbpf Authors. All rights reserved.
static long (* const bpf_for_each_map_elem)(void *map, void *callback_fn, void *callback_ctx, __u64 flags) = (void *) 164;
Usage
The callback_fn
should be a static function with the following signature:
long (*callback_fn)(struct bpf_map *map, const void *key, void *value, void *ctx);
callback_ctx
should be a pointer to a variable on the stack, its type can be determined by the caller. The same context is shared between all calls and can so be used to get information back from the callback to the main program.
The flags
is used to control certain aspects of the helper. Currently, the flags
must be 0.
For per-CPU maps, the map_value
is the value on the cpu where the
bpf_prog
is running.
If callback_fn
return 0, the helper will continue to the next
element. If return value is 1, the helper will skip the rest of
elements and return. Other return values are not used now.
Returns
The number of traversed map elements for success, -EINVAL
for
invalid flags
.
Program types
This helper call can be used in the following program types:
BPF_PROG_TYPE_CGROUP_DEVICE
BPF_PROG_TYPE_CGROUP_SKB
BPF_PROG_TYPE_CGROUP_SOCK
BPF_PROG_TYPE_CGROUP_SOCKOPT
BPF_PROG_TYPE_CGROUP_SOCK_ADDR
BPF_PROG_TYPE_CGROUP_SYSCTL
BPF_PROG_TYPE_FLOW_DISSECTOR
BPF_PROG_TYPE_KPROBE
BPF_PROG_TYPE_LSM
BPF_PROG_TYPE_LWT_IN
BPF_PROG_TYPE_LWT_OUT
BPF_PROG_TYPE_LWT_SEG6LOCAL
BPF_PROG_TYPE_LWT_XMIT
BPF_PROG_TYPE_NETFILTER
BPF_PROG_TYPE_PERF_EVENT
BPF_PROG_TYPE_RAW_TRACEPOINT
BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE
BPF_PROG_TYPE_SCHED_ACT
BPF_PROG_TYPE_SCHED_CLS
BPF_PROG_TYPE_SK_LOOKUP
BPF_PROG_TYPE_SK_MSG
BPF_PROG_TYPE_SK_REUSEPORT
BPF_PROG_TYPE_SK_SKB
BPF_PROG_TYPE_SOCKET_FILTER
BPF_PROG_TYPE_SOCK_OPS
BPF_PROG_TYPE_STRUCT_OPS
BPF_PROG_TYPE_SYSCALL
BPF_PROG_TYPE_TRACEPOINT
BPF_PROG_TYPE_TRACING
BPF_PROG_TYPE_XDP
Map types
This helper call can be used with the following map types:
BPF_MAP_TYPE_ARRAY
BPF_MAP_TYPE_HASH
BPF_MAP_TYPE_LRU_HASH
BPF_MAP_TYPE_LRU_PERCPU_HASH
BPF_MAP_TYPE_PERCPU_ARRAY
BPF_MAP_TYPE_PERCPU_HASH
Example
static long callback_fn(struct bpf_map *map, const void *key, void *value, void *ctx)
{
bpf_printk("context value: %s\n", *(char **)ctx);
// delete elements with an odd key
if (*(__u32 *)key % 2)
bpf_map_delete_elem(map, key);
return 0;
}
int program(void *ctx)
{
/*
.
.
.
*/
char *context = "This string will pass to every callback call";
long (*cb_p)(struct bpf_map *, const void *, void *, void *) = &callback_fn;
bpf_for_each_map_elem(&my_map, cb_p, &context, 0);
}