Skip to content

Commit 6a54576

Browse files
Maciej Fijalkowskiborkmann
authored andcommitted
samples/bpf: Extend RLIMIT_MEMLOCK for xdp_{sample_pkts, router_ipv4}
There is a common problem with xdp samples that happens when user wants to run a particular sample and some bpf program is already loaded. The default 64kb RLIMIT_MEMLOCK resource limit will cause a following error (assuming that xdp sample that is failing was converted to libbpf usage): libbpf: Error in bpf_object__probe_name():Operation not permitted(1). Couldn't load basic 'r0 = 0' BPF program. libbpf: failed to load object './xdp_sample_pkts_kern.o' Fix it in xdp_sample_pkts and xdp_router_ipv4 by setting RLIMIT_MEMLOCK to RLIM_INFINITY. Signed-off-by: Maciej Fijalkowski <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> Acked-by: Jesper Dangaard Brouer <[email protected]> Acked-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent bbaf602 commit 6a54576

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

samples/bpf/xdp_router_ipv4_user.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <sys/syscall.h>
2626
#include "bpf_util.h"
2727
#include "bpf/libbpf.h"
28+
#include <sys/resource.h>
2829

2930
int sock, sock_arp, flags = 0;
3031
static int total_ifindex;
@@ -609,6 +610,7 @@ static int monitor_route(void)
609610

610611
int main(int ac, char **argv)
611612
{
613+
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
612614
struct bpf_prog_load_attr prog_load_attr = {
613615
.prog_type = BPF_PROG_TYPE_XDP,
614616
};
@@ -635,6 +637,11 @@ int main(int ac, char **argv)
635637
ifname_list = (argv + 1);
636638
}
637639

640+
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
641+
perror("setrlimit(RLIMIT_MEMLOCK)");
642+
return 1;
643+
}
644+
638645
if (bpf_prog_load_xattr(&prog_load_attr, &obj, &prog_fd))
639646
return 1;
640647

samples/bpf/xdp_sample_pkts_user.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <signal.h>
1313
#include <libbpf.h>
1414
#include <bpf/bpf.h>
15+
#include <sys/resource.h>
1516

1617
#include "perf-sys.h"
1718
#include "trace_helpers.h"
@@ -99,6 +100,7 @@ static void sig_handler(int signo)
99100

100101
int main(int argc, char **argv)
101102
{
103+
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
102104
struct bpf_prog_load_attr prog_load_attr = {
103105
.prog_type = BPF_PROG_TYPE_XDP,
104106
};
@@ -114,6 +116,11 @@ int main(int argc, char **argv)
114116
return 1;
115117
}
116118

119+
if (setrlimit(RLIMIT_MEMLOCK, &r)) {
120+
perror("setrlimit(RLIMIT_MEMLOCK)");
121+
return 1;
122+
}
123+
117124
numcpus = get_nprocs();
118125
if (numcpus > MAX_CPUS)
119126
numcpus = MAX_CPUS;

0 commit comments

Comments
 (0)