[PATCH RFC bpf-next 1/3] bpf: add mapper macro for bpf_cmd enum

Andrii Nakryiko andrii at kernel.org
Thu Dec 7 22:27:53 UTC 2023


Use similar approach to enum bpf_func_id and generate enumerators using
a macro with macro callback. This approach allows to generate derivative
tables for string-based lookups and whatnot. In this particular case,
this mapper macro will be used for parsing BPF FS delegate_cmds mount
option and their human-readable output format in mount info.

Validated no regressions using before/after BTF through
`bpftool btf dump <file> format c` command.

Signed-off-by: Andrii Nakryiko <andrii at kernel.org>
---
 include/uapi/linux/bpf.h       | 81 ++++++++++++++++++----------------
 tools/include/uapi/linux/bpf.h | 81 ++++++++++++++++++----------------
 2 files changed, 86 insertions(+), 76 deletions(-)

diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h
index e0545201b55f..d05ea24ace3f 100644
--- a/include/uapi/linux/bpf.h
+++ b/include/uapi/linux/bpf.h
@@ -893,47 +893,52 @@ union bpf_iter_link_info {
  *	to the object have been closed and no references remain pinned to the
  *	filesystem or attached (for example, bound to a program or device).
  */
+#define __BPF_CMD_MAPPER(FN, ctx...)					\
+	FN(BPF_MAP_CREATE, 0)						\
+	FN(BPF_MAP_LOOKUP_ELEM, 1)					\
+	FN(BPF_MAP_UPDATE_ELEM, 2)					\
+	FN(BPF_MAP_DELETE_ELEM, 3)					\
+	FN(BPF_MAP_GET_NEXT_KEY, 4)					\
+	FN(BPF_PROG_LOAD, 5)						\
+	FN(BPF_OBJ_PIN, 6)						\
+	FN(BPF_OBJ_GET, 7)						\
+	FN(BPF_PROG_ATTACH, 8)						\
+	FN(BPF_PROG_DETACH, 9)						\
+	FN(BPF_PROG_TEST_RUN, 10)					\
+	FN(BPF_PROG_RUN, 10) /* alias for BPF_PROG_TEST_RUN */		\
+	FN(BPF_PROG_GET_NEXT_ID, 11)					\
+	FN(BPF_MAP_GET_NEXT_ID, 12)					\
+	FN(BPF_PROG_GET_FD_BY_ID, 13)					\
+	FN(BPF_MAP_GET_FD_BY_ID, 14)					\
+	FN(BPF_OBJ_GET_INFO_BY_FD, 15)					\
+	FN(BPF_PROG_QUERY, 16)						\
+	FN(BPF_RAW_TRACEPOINT_OPEN, 17)					\
+	FN(BPF_BTF_LOAD, 18)						\
+	FN(BPF_BTF_GET_FD_BY_ID, 19)					\
+	FN(BPF_TASK_FD_QUERY, 20)					\
+	FN(BPF_MAP_LOOKUP_AND_DELETE_ELEM, 21)				\
+	FN(BPF_MAP_FREEZE, 22)						\
+	FN(BPF_BTF_GET_NEXT_ID, 23)					\
+	FN(BPF_MAP_LOOKUP_BATCH, 24)					\
+	FN(BPF_MAP_LOOKUP_AND_DELETE_BATCH, 25)				\
+	FN(BPF_MAP_UPDATE_BATCH, 26)					\
+	FN(BPF_MAP_DELETE_BATCH, 27)					\
+	FN(BPF_LINK_CREATE, 28)						\
+	FN(BPF_LINK_UPDATE, 29)						\
+	FN(BPF_LINK_GET_FD_BY_ID, 30)					\
+	FN(BPF_LINK_GET_NEXT_ID, 31)					\
+	FN(BPF_ENABLE_STATS, 32)					\
+	FN(BPF_ITER_CREATE, 33)						\
+	FN(BPF_LINK_DETACH, 34)						\
+	FN(BPF_PROG_BIND_MAP, 35)					\
+	FN(BPF_TOKEN_CREATE, 36)					\
+	/* */
+#define __BPF_CMD_FN(x, y) x = y,
 enum bpf_cmd {
-	BPF_MAP_CREATE,
-	BPF_MAP_LOOKUP_ELEM,
-	BPF_MAP_UPDATE_ELEM,
-	BPF_MAP_DELETE_ELEM,
-	BPF_MAP_GET_NEXT_KEY,
-	BPF_PROG_LOAD,
-	BPF_OBJ_PIN,
-	BPF_OBJ_GET,
-	BPF_PROG_ATTACH,
-	BPF_PROG_DETACH,
-	BPF_PROG_TEST_RUN,
-	BPF_PROG_RUN = BPF_PROG_TEST_RUN,
-	BPF_PROG_GET_NEXT_ID,
-	BPF_MAP_GET_NEXT_ID,
-	BPF_PROG_GET_FD_BY_ID,
-	BPF_MAP_GET_FD_BY_ID,
-	BPF_OBJ_GET_INFO_BY_FD,
-	BPF_PROG_QUERY,
-	BPF_RAW_TRACEPOINT_OPEN,
-	BPF_BTF_LOAD,
-	BPF_BTF_GET_FD_BY_ID,
-	BPF_TASK_FD_QUERY,
-	BPF_MAP_LOOKUP_AND_DELETE_ELEM,
-	BPF_MAP_FREEZE,
-	BPF_BTF_GET_NEXT_ID,
-	BPF_MAP_LOOKUP_BATCH,
-	BPF_MAP_LOOKUP_AND_DELETE_BATCH,
-	BPF_MAP_UPDATE_BATCH,
-	BPF_MAP_DELETE_BATCH,
-	BPF_LINK_CREATE,
-	BPF_LINK_UPDATE,
-	BPF_LINK_GET_FD_BY_ID,
-	BPF_LINK_GET_NEXT_ID,
-	BPF_ENABLE_STATS,
-	BPF_ITER_CREATE,
-	BPF_LINK_DETACH,
-	BPF_PROG_BIND_MAP,
-	BPF_TOKEN_CREATE,
+	__BPF_CMD_MAPPER(__BPF_CMD_FN)
 	__MAX_BPF_CMD,
 };
+#undef __BPF_CMD_FN
 
 enum bpf_map_type {
 	BPF_MAP_TYPE_UNSPEC,
diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index e0545201b55f..d05ea24ace3f 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -893,47 +893,52 @@ union bpf_iter_link_info {
  *	to the object have been closed and no references remain pinned to the
  *	filesystem or attached (for example, bound to a program or device).
  */
+#define __BPF_CMD_MAPPER(FN, ctx...)					\
+	FN(BPF_MAP_CREATE, 0)						\
+	FN(BPF_MAP_LOOKUP_ELEM, 1)					\
+	FN(BPF_MAP_UPDATE_ELEM, 2)					\
+	FN(BPF_MAP_DELETE_ELEM, 3)					\
+	FN(BPF_MAP_GET_NEXT_KEY, 4)					\
+	FN(BPF_PROG_LOAD, 5)						\
+	FN(BPF_OBJ_PIN, 6)						\
+	FN(BPF_OBJ_GET, 7)						\
+	FN(BPF_PROG_ATTACH, 8)						\
+	FN(BPF_PROG_DETACH, 9)						\
+	FN(BPF_PROG_TEST_RUN, 10)					\
+	FN(BPF_PROG_RUN, 10) /* alias for BPF_PROG_TEST_RUN */		\
+	FN(BPF_PROG_GET_NEXT_ID, 11)					\
+	FN(BPF_MAP_GET_NEXT_ID, 12)					\
+	FN(BPF_PROG_GET_FD_BY_ID, 13)					\
+	FN(BPF_MAP_GET_FD_BY_ID, 14)					\
+	FN(BPF_OBJ_GET_INFO_BY_FD, 15)					\
+	FN(BPF_PROG_QUERY, 16)						\
+	FN(BPF_RAW_TRACEPOINT_OPEN, 17)					\
+	FN(BPF_BTF_LOAD, 18)						\
+	FN(BPF_BTF_GET_FD_BY_ID, 19)					\
+	FN(BPF_TASK_FD_QUERY, 20)					\
+	FN(BPF_MAP_LOOKUP_AND_DELETE_ELEM, 21)				\
+	FN(BPF_MAP_FREEZE, 22)						\
+	FN(BPF_BTF_GET_NEXT_ID, 23)					\
+	FN(BPF_MAP_LOOKUP_BATCH, 24)					\
+	FN(BPF_MAP_LOOKUP_AND_DELETE_BATCH, 25)				\
+	FN(BPF_MAP_UPDATE_BATCH, 26)					\
+	FN(BPF_MAP_DELETE_BATCH, 27)					\
+	FN(BPF_LINK_CREATE, 28)						\
+	FN(BPF_LINK_UPDATE, 29)						\
+	FN(BPF_LINK_GET_FD_BY_ID, 30)					\
+	FN(BPF_LINK_GET_NEXT_ID, 31)					\
+	FN(BPF_ENABLE_STATS, 32)					\
+	FN(BPF_ITER_CREATE, 33)						\
+	FN(BPF_LINK_DETACH, 34)						\
+	FN(BPF_PROG_BIND_MAP, 35)					\
+	FN(BPF_TOKEN_CREATE, 36)					\
+	/* */
+#define __BPF_CMD_FN(x, y) x = y,
 enum bpf_cmd {
-	BPF_MAP_CREATE,
-	BPF_MAP_LOOKUP_ELEM,
-	BPF_MAP_UPDATE_ELEM,
-	BPF_MAP_DELETE_ELEM,
-	BPF_MAP_GET_NEXT_KEY,
-	BPF_PROG_LOAD,
-	BPF_OBJ_PIN,
-	BPF_OBJ_GET,
-	BPF_PROG_ATTACH,
-	BPF_PROG_DETACH,
-	BPF_PROG_TEST_RUN,
-	BPF_PROG_RUN = BPF_PROG_TEST_RUN,
-	BPF_PROG_GET_NEXT_ID,
-	BPF_MAP_GET_NEXT_ID,
-	BPF_PROG_GET_FD_BY_ID,
-	BPF_MAP_GET_FD_BY_ID,
-	BPF_OBJ_GET_INFO_BY_FD,
-	BPF_PROG_QUERY,
-	BPF_RAW_TRACEPOINT_OPEN,
-	BPF_BTF_LOAD,
-	BPF_BTF_GET_FD_BY_ID,
-	BPF_TASK_FD_QUERY,
-	BPF_MAP_LOOKUP_AND_DELETE_ELEM,
-	BPF_MAP_FREEZE,
-	BPF_BTF_GET_NEXT_ID,
-	BPF_MAP_LOOKUP_BATCH,
-	BPF_MAP_LOOKUP_AND_DELETE_BATCH,
-	BPF_MAP_UPDATE_BATCH,
-	BPF_MAP_DELETE_BATCH,
-	BPF_LINK_CREATE,
-	BPF_LINK_UPDATE,
-	BPF_LINK_GET_FD_BY_ID,
-	BPF_LINK_GET_NEXT_ID,
-	BPF_ENABLE_STATS,
-	BPF_ITER_CREATE,
-	BPF_LINK_DETACH,
-	BPF_PROG_BIND_MAP,
-	BPF_TOKEN_CREATE,
+	__BPF_CMD_MAPPER(__BPF_CMD_FN)
 	__MAX_BPF_CMD,
 };
+#undef __BPF_CMD_FN
 
 enum bpf_map_type {
 	BPF_MAP_TYPE_UNSPEC,
-- 
2.34.1




More information about the Linux-security-module-archive mailing list