[PATCH 2/6] bpf: implement decoding of BPF_MAP_CREATE map_extra field

Paul Chaignon paul at isovalent.com
Wed Dec 29 18:25:00 UTC 2021


* src/bpf_attr.h (BPF_MAP_CREATE_struct): Add map_extra field introduced
by Linux kernel commit v5.6-rc1~151^2~46^2~37^2~5.
(bpf_map_info_struct): Likewise.
(expected_BPF_MAP_CREATE_struct_size, expected_bpf_map_info_struct_size):
Update.
* src/bpf.c (BEGIN_BPF_CMD_DECODER(BPF_MAP_CREATE)): Decode the new field.
* NEWS: Mention this.
* tests/bpf-obj_get_info_by_fd.c (print_map_create): Update expected
output.
* tests/bpf.c (BPF_MAP_CREATE_checks): Add a check that includes the
map_extra field.

Signed-off-by: Paul Chaignon <paul at isovalent.com>
---
 NEWS                           |  1 +
 src/bpf.c                      |  9 +++++++++
 src/bpf_attr.h                 |  9 ++++++---
 tests/bpf-obj_get_info_by_fd.c |  4 ++++
 tests/bpf.c                    | 20 ++++++++++++++++++++
 5 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index c54ae6e87..a890f6226 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,7 @@ Noteworthy changes in release ?.?? (????-??-??)
 * Improvements
   * Updated lists of ioctl commands from Linux 5.16.
   * Updated lists of BPF_* constants.
+  * Enhanced decoding of BPF_MAP_CREATE bpf syscall command.
 
 * Bug fixes
 
diff --git a/src/bpf.c b/src/bpf.c
index 7feb814fb..5c1e7d7ba 100644
--- a/src/bpf.c
+++ b/src/bpf.c
@@ -244,6 +244,15 @@ BEGIN_BPF_CMD_DECODER(BPF_MAP_CREATE)
 		break;
 	tprint_struct_next();
 	PRINT_FIELD_U(attr, btf_vmlinux_value_type_id);
+
+	/*
+	 * The following field was introduced by Linux commit
+	 * v5.16-rc1~159^2~2^2~20^2~4.
+	 */
+	if (len <= offsetof(struct BPF_MAP_CREATE_struct, map_extra))
+		break;
+	tprint_struct_next();
+	PRINT_FIELD_U64(attr, map_extra);
 }
 END_BPF_CMD_DECODER(RVAL_DECODED | RVAL_FD)
 
diff --git a/src/bpf_attr.h b/src/bpf_attr.h
index 54216cabb..7dad083dd 100644
--- a/src/bpf_attr.h
+++ b/src/bpf_attr.h
@@ -50,11 +50,12 @@ struct BPF_MAP_CREATE_struct {
 	uint32_t btf_key_type_id;
 	uint32_t btf_value_type_id;
 	uint32_t btf_vmlinux_value_type_id;
+	uint64_t map_extra;
 };
 
 # define BPF_MAP_CREATE_struct_size \
 	sizeof(struct BPF_MAP_CREATE_struct)
-# define expected_BPF_MAP_CREATE_struct_size 64
+# define expected_BPF_MAP_CREATE_struct_size 72
 
 struct BPF_MAP_LOOKUP_ELEM_struct {
 	uint32_t map_fd;
@@ -310,11 +311,13 @@ struct bpf_map_info_struct {
 	uint32_t btf_id;
 	uint32_t btf_key_type_id;
 	uint32_t btf_value_type_id;
+	uint32_t pad;
+	uint64_t map_extra;
 };
 
 # define bpf_map_info_struct_size \
-	offsetofend(struct bpf_map_info_struct, btf_value_type_id)
-# define expected_bpf_map_info_struct_size 76
+	sizeof(struct bpf_map_info_struct)
+# define expected_bpf_map_info_struct_size 88
 
 struct bpf_prog_info_struct {
 	uint32_t type;
diff --git a/tests/bpf-obj_get_info_by_fd.c b/tests/bpf-obj_get_info_by_fd.c
index c7326aa8a..e2123eeee 100644
--- a/tests/bpf-obj_get_info_by_fd.c
+++ b/tests/bpf-obj_get_info_by_fd.c
@@ -92,6 +92,10 @@ print_map_create(void *attr_void, size_t size, long rc)
 			    btf_vmlinux_value_type_id)) {
 		printf(", btf_vmlinux_value_type_id=0");
 	}
+	if (size > offsetof(struct BPF_MAP_CREATE_struct,
+			    map_extra)) {
+		printf(", map_extra=0");
+	}
 	printf("}, %zu) = ", size);
 	if (rc >= 0)
 		printf("%ld<anon_inode:bpf-map>\n", rc);
diff --git a/tests/bpf.c b/tests/bpf.c
index 31ed3b742..befb34cbd 100644
--- a/tests/bpf.c
+++ b/tests/bpf.c
@@ -469,6 +469,26 @@ static struct bpf_attr_check BPF_MAP_CREATE_checks[] = {
 		       ", btf_value_type_id=3405705229"
 		       ", btf_vmlinux_value_type_id=3735929054"
 	},
+	{ /* 10 */
+		.data = { .BPF_MAP_CREATE_data = {
+			.map_type = BPF_MAP_TYPE_BLOOM_FILTER,
+			.map_extra = 4
+		} },
+		.size = offsetofend(struct BPF_MAP_CREATE_struct, map_extra),
+		.str = "map_type=BPF_MAP_TYPE_BLOOM_FILTER"
+		       ", key_size=0"
+		       ", value_size=0"
+		       ", max_entries=0"
+		       ", map_flags=0"
+		       ", inner_map_fd=0" FD0_PATH
+		       ", map_name=\"\""
+		       ", map_ifindex=0"
+		       ", btf_fd=0" FD0_PATH
+		       ", btf_key_type_id=0"
+		       ", btf_value_type_id=0"
+		       ", btf_vmlinux_value_type_id=0"
+		       ", map_extra=4"
+	},
 };
 
 static const struct bpf_attr_check BPF_MAP_LOOKUP_ELEM_checks[] = {
-- 
2.25.1



More information about the Strace-devel mailing list