[PATCH 5/6] bpf: support commands BPF_LINK_GET_*_ID
Paul Chaignon
paul at isovalent.com
Wed Dec 29 18:25:45 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 78c6b747d..6ae395e4a 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,8 @@ Noteworthy changes in release ?.?? (????-??-??)
* Updated lists of BPF_* constants.
* Enhanced decoding of BPF_MAP_CREATE, BPF_PROG_TEST_RUN, and BPF_PROG_LOAD
bpf syscall commands.
+ * Implement decoding of BPF_LINK_GET_NEXT_ID and BPF_LINK_GET_FD_BY_ID bpf
+ syscall commands.
* Bug fixes
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 c192a3044..d82acbcab 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