[PATCH v2 5/6] bpf: support commands BPF_LINK_GET_*_ID

Paul Chaignon paul at isovalent.com
Thu Dec 30 10:59:53 UTC 2021


* src/bpf.c (BEGIN_BPF_CMD_DECODER(BPF_LINK_GET_FD_BY_ID): Decode the
BPF_LINK_GET_FD_BY_ID command.
(decode_BPF_BTF_GET_NEXT_ID): New macro.
(SYS_FUNC(bpf)): Decode the commands.
* src/bpf_attr.h (BPF_LINK_GET_FD_BY_ID_struct): New structure introduced
by Linux commit v5.8-rc1~165^2~343^2~16^2~8.
(BPF_LINK_GET_FD_BY_ID_struct_size): New macro.
* NEWS: Mention this.
* tests/bpf.c (union bpf_attr_data): Add BPF_LINK_GET_FD_BY_ID.
(BPF_LINK_GET_FD_BY_ID_checks): Test for the BPF_LINK_GET_FD_BY_ID
command.
(BPF_LINK_GET_NEXT_ID_checks): New macro.
(main) <checks>: Add BPF_LINK_GET_NEXT_ID and BPF_LINK_GET_FD_BY_ID.

Signed-off-by: Paul Chaignon <paul at isovalent.com>
---
 NEWS           |  2 ++
 src/bpf.c      | 10 ++++++++++
 src/bpf_attr.h |  8 ++++++++
 tests/bpf.c    | 12 ++++++++++++
 4 files changed, 32 insertions(+)

diff --git a/NEWS b/NEWS
index d40e7c8bb..ed121d07a 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,8 @@ Noteworthy changes in release ?.?? (????-??-??)
 ===============================================
 
 * Improvements
+  * Implement decoding of BPF_LINK_GET_NEXT_ID and BPF_LINK_GET_FD_BY_ID bpf
+    syscall commands.
   * Enhanced decoding of BPF_MAP_CREATE, BPF_PROG_TEST_RUN, and BPF_PROG_LOAD
     bpf syscall commands.
   * Updated lists of BPF_* constants.
diff --git a/src/bpf.c b/src/bpf.c
index 44f0191a3..d1ac17f0a 100644
--- a/src/bpf.c
+++ b/src/bpf.c
@@ -539,6 +539,7 @@ END_BPF_CMD_DECODER(RVAL_DECODED)
 
 #define decode_BPF_MAP_GET_NEXT_ID decode_BPF_PROG_GET_NEXT_ID
 #define decode_BPF_BTF_GET_NEXT_ID decode_BPF_PROG_GET_NEXT_ID
+#define decode_BPF_LINK_GET_NEXT_ID decode_BPF_PROG_GET_NEXT_ID
 
 BEGIN_BPF_CMD_DECODER(BPF_PROG_GET_FD_BY_ID)
 {
@@ -1320,6 +1321,13 @@ BEGIN_BPF_CMD_DECODER(BPF_LINK_UPDATE)
 }
 END_BPF_CMD_DECODER(RVAL_DECODED)
 
+BEGIN_BPF_CMD_DECODER(BPF_LINK_GET_FD_BY_ID)
+{
+	tprint_struct_begin();
+	PRINT_FIELD_U(attr, link_id);
+}
+END_BPF_CMD_DECODER(RVAL_DECODED | RVAL_FD)
+
 SYS_FUNC(bpf)
 {
 	static const bpf_cmd_decoder_t bpf_cmd_decoders[] = {
@@ -1353,6 +1361,8 @@ SYS_FUNC(bpf)
 		BPF_CMD_ENTRY(BPF_MAP_DELETE_BATCH),
 		BPF_CMD_ENTRY(BPF_LINK_CREATE),
 		BPF_CMD_ENTRY(BPF_LINK_UPDATE),
+		BPF_CMD_ENTRY(BPF_LINK_GET_NEXT_ID),
+		BPF_CMD_ENTRY(BPF_LINK_GET_FD_BY_ID),
 	};
 
 	const unsigned int cmd = tcp->u_arg[0];
diff --git a/src/bpf_attr.h b/src/bpf_attr.h
index 9f1618264..ca686c6cc 100644
--- a/src/bpf_attr.h
+++ b/src/bpf_attr.h
@@ -414,4 +414,12 @@ struct BPF_LINK_UPDATE_struct /* link_update */ {
 	sizeof(struct BPF_LINK_UPDATE_struct)
 # define expected_BPF_LINK_UPDATE_struct_size 16
 
+struct BPF_LINK_GET_FD_BY_ID_struct {
+	uint32_t link_id;
+};
+
+# define BPF_LINK_GET_FD_BY_ID_struct_size \
+	sizeof(struct BPF_LINK_GET_FD_BY_ID_struct)
+# define expected_BPF_LINK_GET_FD_BY_ID_struct_size 4
+
 #endif /* !STRACE_BPF_ATTR_H */
diff --git a/tests/bpf.c b/tests/bpf.c
index b61e23bc2..bb1beb58f 100644
--- a/tests/bpf.c
+++ b/tests/bpf.c
@@ -86,6 +86,7 @@ union bpf_attr_data {
 	BPF_ATTR_DATA_FIELD(BPF_MAP_DELETE_BATCH);
 	BPF_ATTR_DATA_FIELD(BPF_LINK_CREATE);
 	BPF_ATTR_DATA_FIELD(BPF_LINK_UPDATE);
+	BPF_ATTR_DATA_FIELD(BPF_LINK_GET_FD_BY_ID);
 	char char_data[256];
 };
 
@@ -1006,6 +1007,7 @@ static const struct bpf_attr_check BPF_PROG_GET_NEXT_ID_checks[] = {
 
 #define BPF_MAP_GET_NEXT_ID_checks BPF_PROG_GET_NEXT_ID_checks
 #define BPF_BTF_GET_NEXT_ID_checks BPF_PROG_GET_NEXT_ID_checks
+#define BPF_LINK_GET_NEXT_ID_checks BPF_PROG_GET_NEXT_ID_checks
 
 static const struct bpf_attr_check BPF_PROG_GET_FD_BY_ID_checks[] = {
 	{
@@ -1459,6 +1461,14 @@ static const struct bpf_attr_check BPF_LINK_UPDATE_checks[] = {
 	}
 };
 
+static const struct bpf_attr_check BPF_LINK_GET_FD_BY_ID_checks[] = {
+	{
+		.data = { .BPF_LINK_GET_FD_BY_ID_data = { .link_id = 0xdeadbeef } },
+		.size = offsetofend(struct BPF_LINK_GET_FD_BY_ID_struct, link_id),
+		.str = "link_id=3735928559"
+	}
+};
+
 
 #define CHK(cmd_) \
 	{ \
@@ -1500,6 +1510,8 @@ main(void)
 		CHK(BPF_MAP_DELETE_BATCH),
 		CHK(BPF_LINK_CREATE),
 		CHK(BPF_LINK_UPDATE),
+		CHK(BPF_LINK_GET_NEXT_ID),
+		CHK(BPF_LINK_GET_FD_BY_ID),
 	};
 
 	page_size = get_page_size();
-- 
2.25.1



More information about the Strace-devel mailing list