From 38e64d2a0a3b833afacba006503d8478d215c402 Mon Sep 17 00:00:00 2001 From: sajadzirak Date: Sun, 29 Sep 2024 15:59:47 +0330 Subject: [PATCH] linux/helper-function Add example for bpf_for_each_map_elem --- .../helper-function/bpf_for_each_map_elem.md | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/docs/linux/helper-function/bpf_for_each_map_elem.md b/docs/linux/helper-function/bpf_for_each_map_elem.md index 60310ec5..c0da74aa 100644 --- a/docs/linux/helper-function/bpf_for_each_map_elem.md +++ b/docs/linux/helper-function/bpf_for_each_map_elem.md @@ -90,5 +90,26 @@ This helper call can be used with the following map types: ### Example -!!! example "Docs could be improved" - This part of the docs is incomplete, contributions are very welcome +```c +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); +} +```