Skip to content

Instantly share code, notes, and snippets.

@marxlaml
Created June 22, 2022 19:23
Show Gist options
  • Save marxlaml/2edff67b8178d5ac385d572efec8c732 to your computer and use it in GitHub Desktop.
Save marxlaml/2edff67b8178d5ac385d572efec8c732 to your computer and use it in GitHub Desktop.
BPF consume_skb loader
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <sys/resource.h>
#include <bpf/libbpf.h>
#include "skb.skel.h"
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args)
{
if (level == LIBBPF_DEBUG)
return 0;
return vfprintf(stderr, format, args);
}
int main(int argc, char **argv)
{
struct skb_bpf *skel;
int err;
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
/* Set up libbpf errors and debug info callback */
libbpf_set_print(libbpf_print_fn);
/* Load and verify BPF application */
skel = skb_bpf__open();
if (!skel) {
fprintf(stderr, "Failed to open and load BPF skeleton\n");
return 1;
}
/* Load & verify BPF programs */
err = skb_bpf__load(skel);
if (err) {
fprintf(stderr, "Failed to load and verify BPF skeleton\n");
goto cleanup;
}
/* Attach tracepoints */
err = skb_bpf__attach(skel);
if (err) {
fprintf(stderr, "Failed to attach BPF skeleton\n");
goto cleanup;
}
int fmap = bpf_map__fd(skel->maps.fmap);
if (!fmap) {
err = -1;
fprintf(stderr, "Failed to create ring buffer\n");
goto cleanup;
}
int *val;
const int zero = 0;
int i = 0;
while (true) {
bpf_map_lookup_elem(fmap, &zero, &val);
if (&val == 1) {
bpf_map_update_elem(fmap, &zero, &zero, BPF_ANY);
}
printf("%d\n",val);
}
cleanup:
/* Clean up */
skb_bpf__destroy(skel);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment