Commit b1fc28b3 authored by Linkui Xiao's avatar Linkui Xiao Committed by Andrii Nakryiko

samples: bpf: Replace sizeof(arr)/sizeof(arr[0]) with ARRAY_SIZE

The ARRAY_SIZE macro is more compact and more formal in linux source.
Signed-off-by: default avatarLinkui Xiao <xiaolinkui@kylinos.cn>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Acked-by: default avatarJiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20220712072302.13761-1-xiaolinkui@kylinos.cn
parent 8ab4cdcf
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "bpf_insn.h" #include "bpf_insn.h"
#include "sock_example.h" #include "sock_example.h"
#include "bpf_util.h"
#define BPF_F_PIN (1 << 0) #define BPF_F_PIN (1 << 0)
#define BPF_F_GET (1 << 1) #define BPF_F_GET (1 << 1)
...@@ -52,7 +53,7 @@ static int bpf_prog_create(const char *object) ...@@ -52,7 +53,7 @@ static int bpf_prog_create(const char *object)
BPF_MOV64_IMM(BPF_REG_0, 1), BPF_MOV64_IMM(BPF_REG_0, 1),
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
size_t insns_cnt = sizeof(insns) / sizeof(struct bpf_insn); size_t insns_cnt = ARRAY_SIZE(insns);
struct bpf_object *obj; struct bpf_object *obj;
int err; int err;
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "bpf_insn.h" #include "bpf_insn.h"
#include "sock_example.h" #include "sock_example.h"
#include "bpf_util.h"
char bpf_log_buf[BPF_LOG_BUF_SIZE]; char bpf_log_buf[BPF_LOG_BUF_SIZE];
...@@ -58,7 +59,7 @@ static int test_sock(void) ...@@ -58,7 +59,7 @@ static int test_sock(void)
BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */ BPF_MOV64_IMM(BPF_REG_0, 0), /* r0 = 0 */
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = ARRAY_SIZE(prog);
LIBBPF_OPTS(bpf_prog_load_opts, opts, LIBBPF_OPTS(bpf_prog_load_opts, opts,
.log_buf = bpf_log_buf, .log_buf = bpf_log_buf,
.log_size = BPF_LOG_BUF_SIZE, .log_size = BPF_LOG_BUF_SIZE,
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include "bpf_insn.h" #include "bpf_insn.h"
#include "bpf_util.h"
enum { enum {
MAP_KEY_PACKETS, MAP_KEY_PACKETS,
...@@ -70,7 +71,7 @@ static int prog_load(int map_fd, int verdict) ...@@ -70,7 +71,7 @@ static int prog_load(int map_fd, int verdict)
BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */ BPF_MOV64_IMM(BPF_REG_0, verdict), /* r0 = verdict */
BPF_EXIT_INSN(), BPF_EXIT_INSN(),
}; };
size_t insns_cnt = sizeof(prog) / sizeof(struct bpf_insn); size_t insns_cnt = ARRAY_SIZE(prog);
LIBBPF_OPTS(bpf_prog_load_opts, opts, LIBBPF_OPTS(bpf_prog_load_opts, opts,
.log_buf = bpf_log_buf, .log_buf = bpf_log_buf,
.log_size = BPF_LOG_BUF_SIZE, .log_size = BPF_LOG_BUF_SIZE,
......
...@@ -523,7 +523,7 @@ int main(int argc, char **argv) ...@@ -523,7 +523,7 @@ int main(int argc, char **argv)
return -1; return -1;
} }
for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) { for (f = 0; f < ARRAY_SIZE(map_flags); f++) {
test_lru_loss0(BPF_MAP_TYPE_LRU_HASH, map_flags[f]); test_lru_loss0(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
test_lru_loss1(BPF_MAP_TYPE_LRU_HASH, map_flags[f]); test_lru_loss1(BPF_MAP_TYPE_LRU_HASH, map_flags[f]);
test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH, map_flags[f], test_parallel_lru_loss(BPF_MAP_TYPE_LRU_HASH, map_flags[f],
......
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "bpf_util.h"
static int map_fd[7]; static int map_fd[7];
#define PORT_A (map_fd[0]) #define PORT_A (map_fd[0])
...@@ -28,7 +30,7 @@ static const char * const test_names[] = { ...@@ -28,7 +30,7 @@ static const char * const test_names[] = {
"Hash of Hash", "Hash of Hash",
}; };
#define NR_TESTS (sizeof(test_names) / sizeof(*test_names)) #define NR_TESTS ARRAY_SIZE(test_names)
static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key) static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
{ {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <bpf/bpf.h> #include <bpf/bpf.h>
#include <bpf/libbpf.h> #include <bpf/libbpf.h>
#include "trace_helpers.h" #include "trace_helpers.h"
#include "bpf_util.h"
#ifdef __mips__ #ifdef __mips__
#define MAX_ENTRIES 6000 /* MIPS n64 syscalls start at 5000 */ #define MAX_ENTRIES 6000 /* MIPS n64 syscalls start at 5000 */
...@@ -24,7 +25,7 @@ static void install_accept_all_seccomp(void) ...@@ -24,7 +25,7 @@ static void install_accept_all_seccomp(void)
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW), BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
}; };
struct sock_fprog prog = { struct sock_fprog prog = {
.len = (unsigned short)(sizeof(filter)/sizeof(filter[0])), .len = (unsigned short)ARRAY_SIZE(filter),
.filter = filter, .filter = filter,
}; };
if (prctl(PR_SET_SECCOMP, 2, &prog)) if (prctl(PR_SET_SECCOMP, 2, &prog))
......
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