Skip to content

Commit

Permalink
ebpf: add fallback when bpf(2) fails with ENOSPC
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <[email protected]>
  • Loading branch information
giuseppe committed Jan 30, 2024
1 parent 983b400 commit 1183af4
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/libcrun/ebpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,17 +503,28 @@ libcrun_ebpf_load (struct bpf_program *program, int dirfd, const char *pin, libc
}
if (fd < 0)
{
const size_t log_size = 8192;
cleanup_free char *log = xmalloc (log_size);
const size_t max_log_size = 1 << 20;
cleanup_free char *log = NULL;
size_t log_size = 8192;

retry:
log = xrealloc (log, log_size);
log[0] = '\0';
attr.log_level = 1;
attr.log_buf = ptr_to_u64 (log);
attr.log_size = log_size;

fd = bpf (BPF_PROG_LOAD, &attr, sizeof (attr));
if (fd < 0)
return crun_make_error (err, errno, "bpf create `%s`", log);
{
if (errno == ENOSPC && log_size < max_log_size)
{
/* The provided buffer was not big enough. */
log_size *= 2;
goto retry;
}
return crun_make_error (err, errno, "bpf create `%s`", log);
}
}

ret = ebpf_attach_program (fd, dirfd, err);
Expand Down

0 comments on commit 1183af4

Please sign in to comment.