Skip to content

Instantly share code, notes, and snippets.

@YutaroHayakawa
Last active August 3, 2020 05:37
Show Gist options
  • Save YutaroHayakawa/258ff38b50c4a6d4d249e623cb925509 to your computer and use it in GitHub Desktop.
Save YutaroHayakawa/258ff38b50c4a6d4d249e623cb925509 to your computer and use it in GitHub Desktop.
Brief usage of the libgbpf (not tested, sorry...)
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <gbpf/gbpf.h>
struct my_data {
int mymap;
int myprog;
};
/*
* Callback when the program found from the ELF file.
* At this moment, the relocation for the maps are already
* done. So, what we need is just load the program and
* pick the file descriptor.
*/
void
on_prog(GBPFElfWalker *walker, const char *name,
struct ebpf_inst *prog, uint32_t prog_len)
{
struct my_data *data = (struct my_data *)walker->data;
printf("Found program: %s\n", name);
data->myprog = gbpf_load_prog(walker->driver, EBPF_PROG_TYPE_XXX,
prog, prog_len);
assert(data->myprog != -1);
}
/*
* Callback when the map found from the ELF file.
* At this moment, the maps is already "created".
* So, what we need to do is pick the file descriptor.
*/
void
on_map(GBPFElfWalker *walker, const char *name, int desc,
struct ebpf_map_def *map)
{
struct my_data *data = (struct my_data *)walker->data;
printf("Found map: %s\n", name);
data->mymap = desc;
}
int
main(int argc, char **argv)
{
int error;
struct my_data data = {0};
EBPFDevDriver *driver;
driver = ebpf_dev_driver_create();
assert(driver != NULL);
GBPFElfWalker walker = {
.driver = driver,
.on_prog = on_prog,
.on_map = on_map,
.data = &data
};
error = gbpf_walk_elf(&walker, driver, argv[1]);
assert(error == 0);
gbpf_driver_destoy(driver);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment