Skip to content

Commit

Permalink
linux/helper-function Add example for bpf_for_each_map_elem
Browse files Browse the repository at this point in the history
  • Loading branch information
sajadzirak authored and dylandreimerink committed Sep 29, 2024
1 parent 5b51176 commit 38e64d2
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions docs/linux/helper-function/bpf_for_each_map_elem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
```

0 comments on commit 38e64d2

Please sign in to comment.