From 9c183d96f7bba6c6ff0e8349241a6dd1418e8d4d Mon Sep 17 00:00:00 2001
From: "Dmitry V. Levin" <ldv@altlinux.org>
Date: Wed, 13 Mar 2019 18:38:51 +0000
Subject: [PATCH 275/293] bpf: implement decoding of BPF_BTF_GET_FD_BY_ID
 command

BPF_BTF_GET_FD_BY_ID command was introduced by Linux commit
v4.18-rc1~114^2~223^2~21^2~4.

* bpf_attr.h (struct BPF_BTF_GET_FD_BY_ID_struct): New type.
(BPF_BTF_GET_FD_BY_ID_struct_size,
expected_BPF_BTF_GET_FD_BY_ID_struct_size): New macros.
* bpf.c (BEGIN_BPF_CMD_DECODER(BPF_BTF_GET_FD_BY_ID)): New bpf command
decoder.
(SYS_FUNC(bpf)) <bpf_cmd_decoders[]>: Add
BPF_CMD_ENTRY(BPF_BTF_GET_FD_BY_ID).
* tests/bpf.c (union bpf_attr_data): Add
BPF_ATTR_DATA_FIELD(BPF_BTF_GET_FD_BY_ID).
(BPF_BTF_GET_FD_BY_ID_checks): New checks array.
(main) <checks>: Add CHK(BPF_BTF_GET_FD_BY_ID).
---
 bpf.c       |  7 +++++++
 bpf_attr.h  |  8 ++++++++
 tests/bpf.c | 10 ++++++++++
 3 files changed, 25 insertions(+)

diff --git a/bpf.c b/bpf.c
index 5fedbd1..69c9a4f 100644
--- a/bpf.c
+++ b/bpf.c
@@ -837,6 +837,12 @@ BEGIN_BPF_CMD_DECODER(BPF_BTF_LOAD)
 }
 END_BPF_CMD_DECODER(RVAL_DECODED | RVAL_FD)
 
+BEGIN_BPF_CMD_DECODER(BPF_BTF_GET_FD_BY_ID)
+{
+	PRINT_FIELD_U("{", attr, btf_id);
+}
+END_BPF_CMD_DECODER(RVAL_DECODED | RVAL_FD)
+
 SYS_FUNC(bpf)
 {
 	static const bpf_cmd_decoder_t bpf_cmd_decoders[] = {
@@ -859,6 +865,7 @@ SYS_FUNC(bpf)
 		BPF_CMD_ENTRY(BPF_PROG_QUERY),
 		BPF_CMD_ENTRY(BPF_RAW_TRACEPOINT_OPEN),
 		BPF_CMD_ENTRY(BPF_BTF_LOAD),
+		BPF_CMD_ENTRY(BPF_BTF_GET_FD_BY_ID),
 	};
 
 	const unsigned int cmd = tcp->u_arg[0];
diff --git a/bpf_attr.h b/bpf_attr.h
index a04f79e..37ddc41 100644
--- a/bpf_attr.h
+++ b/bpf_attr.h
@@ -239,6 +239,14 @@ struct BPF_BTF_LOAD_struct {
 	offsetofend(struct BPF_BTF_LOAD_struct, btf_log_level)
 # define expected_BPF_BTF_LOAD_struct_size 28
 
+struct BPF_BTF_GET_FD_BY_ID_struct {
+	uint32_t btf_id;
+};
+
+# define BPF_BTF_GET_FD_BY_ID_struct_size \
+	sizeof(struct BPF_BTF_GET_FD_BY_ID_struct)
+# define expected_BPF_BTF_GET_FD_BY_ID_struct_size 4
+
 struct bpf_map_info_struct {
 	uint32_t type;
 	uint32_t id;
diff --git a/tests/bpf.c b/tests/bpf.c
index 85b27d5..8f887fb 100644
--- a/tests/bpf.c
+++ b/tests/bpf.c
@@ -72,6 +72,7 @@ union bpf_attr_data {
 	BPF_ATTR_DATA_FIELD(BPF_PROG_QUERY);
 	BPF_ATTR_DATA_FIELD(BPF_RAW_TRACEPOINT_OPEN);
 	BPF_ATTR_DATA_FIELD(BPF_BTF_LOAD);
+	BPF_ATTR_DATA_FIELD(BPF_BTF_GET_FD_BY_ID);
 	char char_data[256];
 };
 
@@ -1117,6 +1118,14 @@ static struct bpf_attr_check BPF_BTF_LOAD_checks[] = {
 	}
 };
 
+static const struct bpf_attr_check BPF_BTF_GET_FD_BY_ID_checks[] = {
+	{
+		.data = { .BPF_BTF_GET_FD_BY_ID_data = { .btf_id = 0xdeadbeef } },
+		.size = offsetofend(struct BPF_BTF_GET_FD_BY_ID_struct, btf_id),
+		.str = "btf_id=3735928559"
+	}
+};
+
 
 #define CHK(cmd_) \
 	{ \
@@ -1148,6 +1157,7 @@ main(void)
 		CHK(BPF_PROG_QUERY),
 		CHK(BPF_RAW_TRACEPOINT_OPEN),
 		CHK(BPF_BTF_LOAD),
+		CHK(BPF_BTF_GET_FD_BY_ID),
 	};
 
 	page_size = get_page_size();
-- 
1.7.12.4

