Skip to content

Commit

Permalink
tests: increase the bpf test's RLIMIT_MEMLOCK value
Browse files Browse the repository at this point in the history
Some architectures, aarch64 specifically, need a higher
RLIMIT_MEMLOCK than may commonly be set by the distribution.  This
patch attempts to increase the limit to "infinity" to solve this
problem.

Signed-off-by: Paul Moore <[email protected]>
  • Loading branch information
pcmoore committed Mar 10, 2020
1 parent 7d630ae commit 2067663
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion tests/bpf/bpf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <errno.h>
#include <string.h>
#include <linux/filter.h>
#include <linux/bpf.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/resource.h>

#define BPF_EXIT_INSN() \
((struct bpf_insn) { .code = BPF_JMP | BPF_EXIT, .dst_reg = 0, .src_reg = 0, .off = 0, .imm = 0 })
Expand All @@ -15,18 +18,38 @@ static inline __u64 ptr_to_u64(const void *ptr)
return (__u64) (unsigned long) ptr;
}

int rlimit_fix(void)
{
struct rlimit limit = {
.rlim_cur = RLIM_INFINITY, .rlim_max = RLIM_INFINITY
};

if (setrlimit(RLIMIT_MEMLOCK, &limit))
return errno;

return 0;
}

int main(int argc, char **argv)
{
int rc;
struct bpf_insn insns[] = {
BPF_MOV64_IMM(BPF_REG_0, 0),
BPF_EXIT_INSN(),
};
union bpf_attr attr;

rc = rlimit_fix();
if (rc)
return rc;

memset(&attr, 0, sizeof(attr));
attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
attr.insn_cnt = 2;
attr.insns = ptr_to_u64(insns);
attr.license = ptr_to_u64("GPL");
return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)) >= 0 ? 0 : -1;
if (syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)))
return errno;

return 0;
}

0 comments on commit 2067663

Please sign in to comment.