aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorYonghong Song <yhs@fb.com>2020-05-09 10:59:19 -0700
committerAlexei Starovoitov <ast@kernel.org>2020-05-09 17:05:27 -0700
commit5fbc220862fc7a53a0455ccd2d96c82141e222d4 (patch)
treea163830ff0f8a707eb45743aa95b3947857e0d1f
parenttools/libbpf: Add bpf_iter support (diff)
downloadwireguard-linux-5fbc220862fc7a53a0455ccd2d96c82141e222d4.tar.xz
wireguard-linux-5fbc220862fc7a53a0455ccd2d96c82141e222d4.zip
tools/libpf: Add offsetof/container_of macro in bpf_helpers.h
These two helpers will be used later in bpf_iter bpf program bpf_iter_netlink.c. Put them in bpf_helpers.h since they could be useful in other cases. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Link: https://lore.kernel.org/bpf/20200509175919.2477104-1-yhs@fb.com
-rw-r--r--tools/lib/bpf/bpf_helpers.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index da00b87aa199..f67dce2af802 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -37,6 +37,20 @@
#endif
/*
+ * Helper macro to manipulate data structures
+ */
+#ifndef offsetof
+#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
+#endif
+#ifndef container_of
+#define container_of(ptr, type, member) \
+ ({ \
+ void *__mptr = (void *)(ptr); \
+ ((type *)(__mptr - offsetof(type, member))); \
+ })
+#endif
+
+/*
* Helper structure used by eBPF C program
* to describe BPF map attributes to libbpf loader
*/