[PATCH v2 2/2] bpf: support new fields for BPF_PROG_LOAD

Paul Chaignon paul.chaignon at gmail.com
Tue Dec 10 09:47:18 UTC 2019


* bpf_attr.h (BPF_PROG_LOAD_struct): New fields introduced in Linux
commits v5.5-rc1~174^2~310^2~19^2~7 and v5.5-rc1~174^2~49^2~12^2~3.
* bpf.c (BEGIN_BPF_CMD_DECODER(BPF_PROG_LOAD)): Decode the new fields.
* tests/bpf.c (BPF_PROG_LOAD_checks): Update expected outputs.
* tests/bpf-obj_get_info_by_fd.c (print_prog_load): Likewise.
* tests/kernel_version.c (print_bpf_attr): Likewise.
* NEWS: Mention this.

Signed-off-by: Paul Chaignon <paul.chaignon at gmail.com>
---
Changelogs:
  Changes in v2:
    - Added NEWS entry.

 NEWS                           |  1 +
 bpf.c                          | 10 ++++++++++
 bpf_attr.h                     |  6 ++++--
 tests/bpf-obj_get_info_by_fd.c |  4 ++++
 tests/bpf.c                    | 14 +++++++++-----
 tests/kernel_version.c         |  4 +++-
 6 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/NEWS b/NEWS
index fa0a734d..b3e92e63 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ Noteworthy changes in release ?.? (????-??-??)
 ==============================================
 
 * Improvements
+  * Enhanced decoding of BPF_PROG_LOAD bpf syscall command.
   * Updated lists of BPF_* constants.
 
 * Bug fixes
diff --git a/bpf.c b/bpf.c
index 9b6fb488..b42cf29d 100644
--- a/bpf.c
+++ b/bpf.c
@@ -343,6 +343,16 @@ BEGIN_BPF_CMD_DECODER(BPF_PROG_LOAD)
 	PRINT_FIELD_U(", ", attr, line_info_rec_size);
 	PRINT_FIELD_ADDR64(", ", attr, line_info);
 	PRINT_FIELD_U(", ", attr, line_info_cnt);
+
+	/* attach_btf_id was added in Linux commit v5.5-rc1~174^2~310^2~19^2~7 */
+	if (len <= offsetof(struct BPF_PROG_LOAD_struct, attach_btf_id))
+		break;
+	PRINT_FIELD_U(", ", attr, attach_btf_id);
+
+	/* attach_prog_fd was added in Linux commit v5.5-rc1~174^2~49^2~12^2~3 */
+	if (len <= offsetof(struct BPF_PROG_LOAD_struct, attach_prog_fd))
+		break;
+	PRINT_FIELD_FD(", ", attr, attach_prog_fd, tcp);
 }
 END_BPF_CMD_DECODER(RVAL_DECODED | RVAL_FD)
 
diff --git a/bpf_attr.h b/bpf_attr.h
index 6695a2fe..33dd54af 100644
--- a/bpf_attr.h
+++ b/bpf_attr.h
@@ -124,11 +124,13 @@ struct BPF_PROG_LOAD_struct {
 	uint32_t line_info_rec_size;
 	uint64_t ATTRIBUTE_ALIGNED(8) line_info;
 	uint32_t line_info_cnt;
+	uint32_t attach_btf_id;
+	uint32_t attach_prog_fd;
 };
 
 # define BPF_PROG_LOAD_struct_size \
-	offsetofend(struct BPF_PROG_LOAD_struct, line_info_cnt)
-# define expected_BPF_PROG_LOAD_struct_size 108
+	offsetofend(struct BPF_PROG_LOAD_struct, attach_prog_fd)
+# define expected_BPF_PROG_LOAD_struct_size 116
 
 struct BPF_OBJ_PIN_struct {
 	uint64_t ATTRIBUTE_ALIGNED(8) pathname;
diff --git a/tests/bpf-obj_get_info_by_fd.c b/tests/bpf-obj_get_info_by_fd.c
index 3bfa6402..c486d77f 100644
--- a/tests/bpf-obj_get_info_by_fd.c
+++ b/tests/bpf-obj_get_info_by_fd.c
@@ -205,6 +205,10 @@ print_prog_load(void *attr_void, size_t size, long rc)
 		printf(", line_info=NULL");
 	if (size > offsetof(struct BPF_PROG_LOAD_struct, line_info_cnt))
 		printf(", line_info_cnt=0");
+	if (size > offsetof(struct BPF_PROG_LOAD_struct, attach_btf_id))
+		printf(", attach_btf_id=0");
+	if (size > offsetof(struct BPF_PROG_LOAD_struct, attach_prog_fd))
+		printf(", attach_prog_fd=0</dev/null>");
 	printf("}, %zu) = ", size);
 	if (rc >= 0)
 		printf("%ld<anon_inode:bpf-prog>\n", rc);
diff --git a/tests/bpf.c b/tests/bpf.c
index 5cf79531..24b70339 100644
--- a/tests/bpf.c
+++ b/tests/bpf.c
@@ -606,7 +606,7 @@ print_BPF_PROG_LOAD_attr4(const struct bpf_attr_check *check, unsigned long addr
 	       license, IFINDEX_LO_STR);
 }
 
-static_assert(ARRAY_SIZE(bpf_prog_types_xdata) == 26,
+static_assert(ARRAY_SIZE(bpf_prog_types_xdata) == 27,
 	      "The prog_type for test 5 below needs to be updated");
 static struct bpf_attr_check BPF_PROG_LOAD_checks[] = {
 	{
@@ -691,7 +691,7 @@ static struct bpf_attr_check BPF_PROG_LOAD_checks[] = {
 	},
 	{ /* 5 */
 		.data = { .BPF_PROG_LOAD_data = {
-			.prog_type = 25,
+			.prog_type = 26,
 			.insn_cnt = 0xbadc0ded,
 			.insns = 0xffffffff00000000,
 			.license = 0xffffffff00000000,
@@ -703,7 +703,7 @@ static struct bpf_attr_check BPF_PROG_LOAD_checks[] = {
 			.prog_name = "fedcba987654321",
 		} },
 		.size = offsetofend(struct BPF_PROG_LOAD_struct, prog_name),
-		.str = "prog_type=BPF_PROG_TYPE_CGROUP_SOCKOPT"
+		.str = "prog_type=BPF_PROG_TYPE_TRACING"
 		       ", insn_cnt=3134983661"
 		       ", insns=" BIG_ADDR("0xffffffff00000000", "NULL")
 		       ", license=" BIG_ADDR("0xffffffff00000000", "NULL")
@@ -723,10 +723,12 @@ static struct bpf_attr_check BPF_PROG_LOAD_checks[] = {
 			.func_info_cnt = 0xdad3bef4,
 			.line_info_rec_size = 0xdad5bef6,
 			.line_info = 0xfac5fed5fac7fed8,
-			.line_info_cnt = 0xdad7bef8
+			.line_info_cnt = 0xdad7bef8,
+			.attach_btf_id = 0xdad7befa,
+			.attach_prog_fd = 0xbadc0def,
 		} },
 		.size = offsetofend(struct BPF_PROG_LOAD_struct,
-				    line_info_cnt),
+				    attach_prog_fd),
 		.str = "prog_type=BPF_PROG_TYPE_UNSPEC"
 		       ", insn_cnt=0"
 		       ", insns=NULL"
@@ -746,6 +748,8 @@ static struct bpf_attr_check BPF_PROG_LOAD_checks[] = {
 		       ", line_info_rec_size=3671441142"
 		       ", line_info=0xfac5fed5fac7fed8"
 		       ", line_info_cnt=3671572216"
+		       ", attach_btf_id=3671572218"
+		       ", attach_prog_fd=-1159983633"
 	},
 };
 
diff --git a/tests/kernel_version.c b/tests/kernel_version.c
index 009f9fd5..a667b584 100644
--- a/tests/kernel_version.c
+++ b/tests/kernel_version.c
@@ -69,7 +69,9 @@ print_bpf_attr(void)
 		", func_info_cnt=0"
 		", line_info_rec_size=0"
 		", line_info=NULL"
-		", line_info_cnt=0}");
+		", line_info_cnt=0"
+		", attach_btf_id=0"
+		", attach_prog_fd=0}");
 }
 
 int
-- 
2.17.1



More information about the Strace-devel mailing list