summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/bpf_util.h
diff options
context:
space:
mode:
authorDaniel Borkmann2017-04-27 01:39:35 +0200
committerDavid S. Miller2017-04-28 21:48:15 +0200
commitf3515b5d0b718cceae9e67802cfa20d5e6f9b567 (patch)
treef36c473e0e53ef2018334abd8cffcf42b4dd70b6 /tools/testing/selftests/bpf/bpf_util.h
parentbpf: fix _htons occurences in test_progs (diff)
downloadkernel-qcow2-linux-f3515b5d0b718cceae9e67802cfa20d5e6f9b567.tar.gz
kernel-qcow2-linux-f3515b5d0b718cceae9e67802cfa20d5e6f9b567.tar.xz
kernel-qcow2-linux-f3515b5d0b718cceae9e67802cfa20d5e6f9b567.zip
bpf: provide a generic macro for percpu values for selftests
To overcome bugs as described and fixed in 89087c456fb5 ("bpf: Fix values type used in test_maps"), provide a generic BPF_DECLARE_PERCPU() and bpf_percpu() accessor macro for all percpu map values used in tests. Declaring variables works as follows (also works for structs): BPF_DECLARE_PERCPU(uint32_t, my_value); They can then be accessed normally as uint32_t type through: bpf_percpu(my_value, <cpu_nr>) For example: bpf_percpu(my_value, 0)++; Implicitly, we make sure that the passed type is allocated and aligned by gcc at least on a 8-byte boundary, so that it works together with the map lookup/update syscall for percpu maps. We use it as a usage example in test_maps, so that others are free to adapt this into their code when necessary. Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'tools/testing/selftests/bpf/bpf_util.h')
-rw-r--r--tools/testing/selftests/bpf/bpf_util.h7
1 files changed, 7 insertions, 0 deletions
diff --git a/tools/testing/selftests/bpf/bpf_util.h b/tools/testing/selftests/bpf/bpf_util.h
index 7de27966103d..369e7d7bba80 100644
--- a/tools/testing/selftests/bpf/bpf_util.h
+++ b/tools/testing/selftests/bpf/bpf_util.h
@@ -54,4 +54,11 @@ static inline unsigned int bpf_num_possible_cpus(void)
return possible_cpus;
}
+#define __bpf_percpu_val_align __attribute__((__aligned__(8)))
+
+#define BPF_DECLARE_PERCPU(type, name) \
+ struct { type v; /* padding */ } __bpf_percpu_val_align \
+ name[bpf_num_possible_cpus()]
+#define bpf_percpu(name, cpu) name[(cpu)].v
+
#endif /* __BPF_UTIL__ */