Commit b25acdaf authored by Yafang Shao's avatar Yafang Shao Committed by Andrii Nakryiko

samples/bpf: Use libbpf 1.0 API mode instead of RLIMIT_MEMLOCK

We have switched to memcg-based memory accouting and thus the rlimit is
not needed any more. LIBBPF_STRICT_AUTO_RLIMIT_MEMLOCK was introduced in
libbpf for backward compatibility, so we can use it instead now.

This patch also removes the useless header sys/resource.h from many files
in samples/bpf.
Signed-off-by: default avatarYafang Shao <laoar.shao@gmail.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20220409125958.92629-2-laoar.shao@gmail.com
parent d252a4a4
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
......
...@@ -34,7 +34,6 @@ ...@@ -34,7 +34,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <sys/resource.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
...@@ -46,7 +45,6 @@ ...@@ -46,7 +45,6 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <getopt.h> #include <getopt.h>
#include "bpf_rlimit.h"
#include "cgroup_helpers.h" #include "cgroup_helpers.h"
#include "hbm.h" #include "hbm.h"
#include "bpf_util.h" #include "bpf_util.h"
...@@ -510,5 +508,8 @@ int main(int argc, char **argv) ...@@ -510,5 +508,8 @@ int main(int argc, char **argv)
prog = argv[optind]; prog = argv[optind];
printf("HBM prog: %s\n", prog != NULL ? prog : "NULL"); printf("HBM prog: %s\n", prog != NULL ? prog : "NULL");
/* Use libbpf 1.0 API mode */
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
return run_bpf_prog(prog, cg_id); return run_bpf_prog(prog, cg_id);
} }
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <limits.h> #include <limits.h>
#include <sys/resource.h>
#include <getopt.h> #include <getopt.h>
#include <net/if.h> #include <net/if.h>
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <sys/resource.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <errno.h> #include <errno.h>
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <linux/perf_event.h> #include <linux/perf_event.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/resource.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "trace_helpers.h" #include "trace_helpers.h"
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include "sock_example.h" #include "sock_example.h"
#include <unistd.h> #include <unistd.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/resource.h>
struct pair { struct pair {
__u64 packets; __u64 packets;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
#include "sock_example.h" #include "sock_example.h"
#include <unistd.h> #include <unistd.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/resource.h>
struct flow_key_record { struct flow_key_record {
__be32 src; __be32 src;
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <sys/resource.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "trace_helpers.h" #include "trace_helpers.h"
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <string.h> #include <string.h>
#include <linux/perf_event.h> #include <linux/perf_event.h>
#include <errno.h> #include <errno.h>
#include <sys/resource.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <linux/bpf.h> #include <linux/bpf.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <linux/perf_event.h> #include <linux/perf_event.h>
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include <sched.h> #include <sched.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/resource.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
/* /*
* Copyright (c) 2017 Facebook * Copyright (c) 2017 Facebook
*/ */
#include <sys/resource.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <stdint.h> #include <stdint.h>
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <linux/bpf.h> #include <linux/bpf.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <sys/resource.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <sys/resource.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include <unistd.h> #include <unistd.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <sys/resource.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <sys/resource.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
#include <sys/prctl.h> #include <sys/prctl.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <sys/resource.h>
#include "trace_helpers.h" #include "trace_helpers.h"
#ifdef __mips__ #ifdef __mips__
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <libgen.h> #include <libgen.h>
#include <sys/resource.h>
#include <net/if.h> #include <net/if.h>
#include "bpf_util.h" #include "bpf_util.h"
......
...@@ -14,7 +14,6 @@ ...@@ -14,7 +14,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <net/if.h> #include <net/if.h>
#include <sys/resource.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/ether.h> #include <netinet/ether.h>
#include <unistd.h> #include <unistd.h>
......
...@@ -17,7 +17,6 @@ static const char *__doc_err_only__= ...@@ -17,7 +17,6 @@ static const char *__doc_err_only__=
#include <ctype.h> #include <ctype.h>
#include <unistd.h> #include <unistd.h>
#include <locale.h> #include <locale.h>
#include <sys/resource.h>
#include <getopt.h> #include <getopt.h>
#include <net/if.h> #include <net/if.h>
#include <time.h> #include <time.h>
......
...@@ -21,7 +21,6 @@ static const char *__doc__ = ...@@ -21,7 +21,6 @@ static const char *__doc__ =
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <locale.h> #include <locale.h>
#include <sys/resource.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <getopt.h> #include <getopt.h>
#include <net/if.h> #include <net/if.h>
......
...@@ -15,7 +15,6 @@ static const char *__doc__ = ...@@ -15,7 +15,6 @@ static const char *__doc__ =
#include <net/if.h> #include <net/if.h>
#include <unistd.h> #include <unistd.h>
#include <libgen.h> #include <libgen.h>
#include <sys/resource.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
......
...@@ -18,7 +18,6 @@ static const char *__doc__ = ...@@ -18,7 +18,6 @@ static const char *__doc__ =
#include <unistd.h> #include <unistd.h>
#include <libgen.h> #include <libgen.h>
#include <getopt.h> #include <getopt.h>
#include <sys/resource.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "bpf_util.h" #include "bpf_util.h"
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include "bpf_util.h" #include "bpf_util.h"
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <sys/resource.h>
#include <libgen.h> #include <libgen.h>
#include <getopt.h> #include <getopt.h>
#include <pthread.h> #include <pthread.h>
......
...@@ -14,7 +14,6 @@ static const char *__doc__ = " XDP RX-queue info extract example\n\n" ...@@ -14,7 +14,6 @@ static const char *__doc__ = " XDP RX-queue info extract example\n\n"
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <locale.h> #include <locale.h>
#include <sys/resource.h>
#include <getopt.h> #include <getopt.h>
#include <net/if.h> #include <net/if.h>
#include <time.h> #include <time.h>
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include <signal.h> #include <signal.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <sys/resource.h>
#include <libgen.h> #include <libgen.h>
#include <linux/if_link.h> #include <linux/if_link.h>
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#include <string.h> #include <string.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/resource.h>
#include <sys/signalfd.h> #include <sys/signalfd.h>
#include <sys/sysinfo.h> #include <sys/sysinfo.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <net/if.h> #include <net/if.h>
#include <sys/resource.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/ether.h> #include <netinet/ether.h>
#include <unistd.h> #include <unistd.h>
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#include <string.h> #include <string.h>
#include <sys/capability.h> #include <sys/capability.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/un.h> #include <sys/un.h>
...@@ -1886,7 +1885,6 @@ int main(int argc, char **argv) ...@@ -1886,7 +1885,6 @@ int main(int argc, char **argv)
{ {
struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3, 0 }; struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3, 0 };
struct __user_cap_data_struct data[2] = { { 0 } }; struct __user_cap_data_struct data[2] = { { 0 } };
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
bool rx = false, tx = false; bool rx = false, tx = false;
struct sched_param schparam; struct sched_param schparam;
struct xsk_umem_info *umem; struct xsk_umem_info *umem;
...@@ -1917,11 +1915,8 @@ int main(int argc, char **argv) ...@@ -1917,11 +1915,8 @@ int main(int argc, char **argv)
data[1].effective, data[1].inheritable, data[1].permitted); data[1].effective, data[1].inheritable, data[1].permitted);
} }
} else { } else {
if (setrlimit(RLIMIT_MEMLOCK, &r)) { /* Use libbpf 1.0 API mode */
fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n", libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
strerror(errno));
exit(EXIT_FAILURE);
}
if (opt_num_xsks > 1) if (opt_num_xsks > 1)
load_xdp_program(argv, &obj); load_xdp_program(argv, &obj);
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
...@@ -131,7 +130,6 @@ static struct bpool * ...@@ -131,7 +130,6 @@ static struct bpool *
bpool_init(struct bpool_params *params, bpool_init(struct bpool_params *params,
struct xsk_umem_config *umem_cfg) struct xsk_umem_config *umem_cfg)
{ {
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
u64 n_slabs, n_slabs_reserved, n_buffers, n_buffers_reserved; u64 n_slabs, n_slabs_reserved, n_buffers, n_buffers_reserved;
u64 slabs_size, slabs_reserved_size; u64 slabs_size, slabs_reserved_size;
u64 buffers_size, buffers_reserved_size; u64 buffers_size, buffers_reserved_size;
...@@ -140,9 +138,8 @@ bpool_init(struct bpool_params *params, ...@@ -140,9 +138,8 @@ bpool_init(struct bpool_params *params,
u8 *p; u8 *p;
int status; int status;
/* mmap prep. */ /* Use libbpf 1.0 API mode */
if (setrlimit(RLIMIT_MEMLOCK, &r)) libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
return NULL;
/* bpool internals dimensioning. */ /* bpool internals dimensioning. */
n_slabs = (params->n_buffers + params->n_buffers_per_slab - 1) / n_slabs = (params->n_buffers + params->n_buffers_per_slab - 1) /
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment