[PATCH v2] bpf: support new commands BPF_LINK_{CREATE,UPDATE}

Paul Chaignon paul at cilium.io
Sat Apr 18 13:58:15 UTC 2020


* xlat/bpf_commands.in (BPF_LINK_CREATE): New constant introduced by Linux
commit v5.7-rc1~146^2~10^2^2~3.
(BPF_LINK_UPDATE): New constant introduced by Linux commit
v5.7-rc1~146^2~10^2^2~2.
* bpf.c (BEGIN_BPF_CMD_DECODER(BPF_LINK_CREATE),
BEGIN_BPF_CMD_DECODER(BPF_LINK_UPDATE)): Decode the new commands.
(SYS_FUNC(bpf)): Decode the new commands.
* bpf_attr.h (BPF_LINK_CREATE_struct): New structure introduced by Linux
commit v5.7-rc1~146^2~10^2^2~3.
(BPF_LINK_UPDATE_struct): New structure introduced by Linux commit
v5.7-rc1~146^2~10^2^2~2.
(BPF_LINK_CREATE_struct_size, BPF_LINK_UPDATE_struct_size): New macros.
* NEWS: Mention this.
* tests/bpf.c (BPF_LINK_CREATE_checks, BPF_LINK_UPDATE_checks): Tests for
the new commands.

Signed-off-by: Paul Chaignon <paul at cilium.io>
---
Changelogs:
  Changes in v2:
  - Do not print old_prog_fd if BPF_F_REPLACE isn't set.
  - Update test output accordingly.

 NEWS                 |  2 ++
 bpf.c                | 24 +++++++++++++++++++++++
 bpf_attr.h           | 22 +++++++++++++++++++++
 tests/bpf.c          | 46 ++++++++++++++++++++++++++++++++++++++++++++
 xlat/bpf_commands.in |  2 ++
 5 files changed, 96 insertions(+)

diff --git a/NEWS b/NEWS
index 6292e1c3..51617029 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,8 @@ Noteworthy changes in release ?.? (????-??-??)
 ==============================================
 
 * Improvements
+  * Implemented decoding of BPF_LINK_CREATE and BPF_LINK_UPDATE bpf
+    syscall commands.
   * Updated lists of BPF_* constants.
 
 Noteworthy changes in release 5.6 (2020-04-07)
diff --git a/bpf.c b/bpf.c
index 0a47766e..469f809a 100644
--- a/bpf.c
+++ b/bpf.c
@@ -1038,6 +1038,28 @@ BEGIN_BPF_CMD_DECODER(BPF_MAP_DELETE_BATCH)
 }
 END_BPF_CMD_DECODER(0)
 
+BEGIN_BPF_CMD_DECODER(BPF_LINK_CREATE)
+{
+	PRINT_FIELD_FD("{link_create={", attr, prog_fd, tcp);
+	PRINT_FIELD_FD(", ", attr, target_fd, tcp);
+	PRINT_FIELD_XVAL(", ", attr, attach_type, bpf_attach_type, "BPF_???");
+	PRINT_FIELD_X(", ", attr, flags);
+	tprints("}");
+}
+END_BPF_CMD_DECODER(RVAL_DECODED | RVAL_FD)
+
+BEGIN_BPF_CMD_DECODER(BPF_LINK_UPDATE)
+{
+	PRINT_FIELD_FD("{link_update={", attr, link_fd, tcp);
+	PRINT_FIELD_FD(", ", attr, new_prog_fd, tcp);
+	PRINT_FIELD_FLAGS(", ", attr, flags, bpf_attach_flags,
+			  "BPF_F_???");
+	if (attr.flags & BPF_F_REPLACE)
+		PRINT_FIELD_FD(", ", attr, old_prog_fd, tcp);
+	tprints("}");
+}
+END_BPF_CMD_DECODER(RVAL_DECODED)
+
 SYS_FUNC(bpf)
 {
 	static const bpf_cmd_decoder_t bpf_cmd_decoders[] = {
@@ -1069,6 +1091,8 @@ SYS_FUNC(bpf)
 		BPF_CMD_ENTRY(BPF_MAP_LOOKUP_AND_DELETE_BATCH),
 		BPF_CMD_ENTRY(BPF_MAP_UPDATE_BATCH),
 		BPF_CMD_ENTRY(BPF_MAP_DELETE_BATCH),
+		BPF_CMD_ENTRY(BPF_LINK_CREATE),
+		BPF_CMD_ENTRY(BPF_LINK_UPDATE),
 	};
 
 	const unsigned int cmd = tcp->u_arg[0];
diff --git a/bpf_attr.h b/bpf_attr.h
index 6b66fb06..bed011bb 100644
--- a/bpf_attr.h
+++ b/bpf_attr.h
@@ -385,4 +385,26 @@ struct BPF_MAP_LOOKUP_BATCH_struct /* batch */ {
 # define BPF_MAP_DELETE_BATCH_struct BPF_MAP_LOOKUP_BATCH_struct
 # define BPF_MAP_DELETE_BATCH_struct_size BPF_MAP_LOOKUP_BATCH_struct_size
 
+struct BPF_LINK_CREATE_struct /* link_create */ {
+	uint32_t prog_fd;
+	uint32_t target_fd;
+	uint32_t attach_type;
+	uint32_t flags;
+};
+
+# define BPF_LINK_CREATE_struct_size \
+	sizeof(struct BPF_LINK_CREATE_struct)
+# define expected_BPF_LINK_CREATE_struct_size 16
+
+struct BPF_LINK_UPDATE_struct /* link_update */ {
+	uint32_t link_fd;
+	uint32_t new_prog_fd;
+	uint32_t flags;
+	uint32_t old_prog_fd;
+};
+
+# define BPF_LINK_UPDATE_struct_size \
+	sizeof(struct BPF_LINK_UPDATE_struct)
+# define expected_BPF_LINK_UPDATE_struct_size 16
+
 #endif /* !STRACE_BPF_ATTR_H */
diff --git a/tests/bpf.c b/tests/bpf.c
index 0d45f887..0b426d8a 100644
--- a/tests/bpf.c
+++ b/tests/bpf.c
@@ -83,6 +83,8 @@ union bpf_attr_data {
 	BPF_ATTR_DATA_FIELD(BPF_MAP_LOOKUP_BATCH);
 	BPF_ATTR_DATA_FIELD(BPF_MAP_UPDATE_BATCH);
 	BPF_ATTR_DATA_FIELD(BPF_MAP_DELETE_BATCH);
+	BPF_ATTR_DATA_FIELD(BPF_LINK_CREATE);
+	BPF_ATTR_DATA_FIELD(BPF_LINK_UPDATE);
 	char char_data[256];
 };
 
@@ -1388,6 +1390,48 @@ static const struct bpf_attr_check BPF_MAP_DELETE_BATCH_checks[] = {
 	}
 };
 
+static const struct bpf_attr_check BPF_LINK_CREATE_checks[] = {
+	{
+		.data = { .BPF_LINK_CREATE_data = { .prog_fd = -1, .target_fd = -2 } },
+		.size = offsetofend(struct BPF_LINK_CREATE_struct, flags),
+		.str = "link_create={prog_fd=-1, target_fd=-2"
+		       ", attach_type=BPF_CGROUP_INET_INGRESS, flags=0}"
+	},
+	{
+		.data = { .BPF_LINK_CREATE_data = {
+			.prog_fd = -1,
+			.target_fd = 0xdeadbeef,
+			.attach_type = 5,
+			.flags = 4
+		} },
+		.size = offsetofend(struct BPF_LINK_CREATE_struct, flags),
+		.str = "link_create={prog_fd=-1, target_fd=-559038737"
+		       ", attach_type=BPF_SK_SKB_STREAM_VERDICT, flags=0x4}"
+	}
+};
+
+static const struct bpf_attr_check BPF_LINK_UPDATE_checks[] = {
+	{
+		.data = { .BPF_LINK_UPDATE_data = {
+			.link_fd = -1,
+			.new_prog_fd = -2
+		} },
+		.size = offsetofend(struct BPF_LINK_UPDATE_struct, old_prog_fd),
+		.str = "link_update={link_fd=-1, new_prog_fd=-2, flags=0}"
+	},
+	{
+		.data = { .BPF_LINK_UPDATE_data = {
+			.link_fd = -1,
+			.new_prog_fd = 0xdeadbeef,
+			.flags = 4,
+			.old_prog_fd = 0xdeadf00d
+		} },
+		.size = offsetofend(struct BPF_LINK_UPDATE_struct, old_prog_fd),
+		.str = "link_update={link_fd=-1, new_prog_fd=-559038737"
+		       ", flags=BPF_F_REPLACE, old_prog_fd=-559026163}"
+	}
+};
+
 
 #define CHK(cmd_) \
 	{ \
@@ -1427,6 +1471,8 @@ main(void)
 		CHK(BPF_MAP_LOOKUP_BATCH),
 		CHK(BPF_MAP_UPDATE_BATCH),
 		CHK(BPF_MAP_DELETE_BATCH),
+		CHK(BPF_LINK_CREATE),
+		CHK(BPF_LINK_UPDATE),
 	};
 
 	page_size = get_page_size();
diff --git a/xlat/bpf_commands.in b/xlat/bpf_commands.in
index 2b95264b..1ff80f41 100644
--- a/xlat/bpf_commands.in
+++ b/xlat/bpf_commands.in
@@ -27,3 +27,5 @@ BPF_MAP_LOOKUP_BATCH 24
 BPF_MAP_LOOKUP_AND_DELETE_BATCH 25
 BPF_MAP_UPDATE_BATCH 26
 BPF_MAP_DELETE_BATCH 27
+BPF_LINK_CREATE 28
+BPF_LINK_UPDATE 29
-- 
2.20.1



More information about the Strace-devel mailing list