From: Manu Bretelle <chantr4@gmail.com>
To: bpf@vger.kernel.org, quentin@isovalent.com, andrii@kernel.org,
daniel@iogearbox.net, ast@kernel.org, martin.lau@linux.dev,
song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org,
sdf@google.com, haoluo@google.com, jolsa@kernel.org
Subject: [PATCH v1 bpf-next 7/9] bpftool: Add struct_ops tests
Date: Thu, 16 Nov 2023 11:42:34 -0800 [thread overview]
Message-ID: <20231116194236.1345035-8-chantr4@gmail.com> (raw)
In-Reply-To: <20231116194236.1345035-1-chantr4@gmail.com>
Add test to verify that we can:
- unregister a struct_ops by id and name
- list struct_ops
- dump a struct_ops by name
$ RUST_TEST_THREADS=1 BPFTOOL_PATH=../tools/build/bpftool/bpftool
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="sudo -E" cargo test --
--nocapture
Finished test [unoptimized + debuginfo] target(s) in 0.05s
Running unittests src/main.rs
(target/debug/deps/bpftool_tests-afa5a7eef3cdeafb)
running 11 tests
test bpftool_tests::run_bpftool ... Running command
"../tools/build/bpftool/bpftool" "version"
ok
test bpftool_tests::run_bpftool_map_dump_id ... Running command
"../tools/build/bpftool/bpftool" "map" "dump" "id" "1851884" "--json"
ok
test bpftool_tests::run_bpftool_map_list ... Running command
"../tools/build/bpftool/bpftool" "map" "list" "--json"
ok
test bpftool_tests::run_bpftool_map_pids ... Running command
"../tools/build/bpftool/bpftool" "map" "list" "--json"
ok
test bpftool_tests::run_bpftool_prog_list ... Running command
"../tools/build/bpftool/bpftool" "prog" "list" "--json"
ok
test bpftool_tests::run_bpftool_prog_pids ... Running command
"../tools/build/bpftool/bpftool" "prog" "list" "--json"
ok
test bpftool_tests::run_bpftool_prog_show_id ... Running command
"../tools/build/bpftool/bpftool" "prog" "show" "id" "3166535" "--json"
ok
test bpftool_tests::run_bpftool_struct_ops_can_unregister_id ... Running
command "../tools/build/bpftool/bpftool" "struct_ops" "dump" "name"
"bt_e2e_tco" "--pretty"
Running command "../tools/build/bpftool/bpftool" "struct_ops"
"unregister" "id" "1851939"
ok
test bpftool_tests::run_bpftool_struct_ops_can_unregister_name ...
Running command "../tools/build/bpftool/bpftool" "struct_ops" "dump"
"name" "bt_e2e_tco" "--pretty"
Running command "../tools/build/bpftool/bpftool" "struct_ops"
"unregister" "name" "bt_e2e_tco"
ok
test bpftool_tests::run_bpftool_struct_ops_dump_name ... Running command
"../tools/build/bpftool/bpftool" "struct_ops" "dump" "name" "bt_e2e_tco"
"--pretty"
ok
test bpftool_tests::run_bpftool_struct_ops_list ... Running command
"../tools/build/bpftool/bpftool" "struct_ops" "list" "--json"
ok
test result: ok. 11 passed; 0 failed; 0 ignored; 0 measured; 0 filtered
out; finished in 1.84s
Signed-off-by: Manu Bretelle <chantr4@gmail.com>
---
.../bpftool_tests/src/bpf/bpftool_tests.bpf.c | 55 +++++++
.../bpf/bpftool_tests/src/bpftool_tests.rs | 145 ++++++++++++++++++
2 files changed, 200 insertions(+)
diff --git a/tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c b/tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c
index a90c8921b4ee..c59df9d947ed 100644
--- a/tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c
+++ b/tools/testing/selftests/bpf/bpftool_tests/src/bpf/bpftool_tests.bpf.c
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
char LICENSE[] SEC("license") = "Dual BSD/GPL";
@@ -32,3 +34,56 @@ int handle_tp_sys_enter_write(void *ctx)
return 0;
}
+
+// struct_ops example
+
+#define BPF_STRUCT_OPS(name, args...) \
+ SEC("struct_ops/" #name) \
+ BPF_PROG(name, args)
+
+void BPF_STRUCT_OPS(tcp_init, struct sock *sk)
+{
+ return;
+}
+
+void BPF_STRUCT_OPS(in_ack_event, struct sock *sk, __u32 flags)
+{
+ return;
+}
+
+__u32 BPF_STRUCT_OPS(ssthresh, struct sock *sk)
+{
+ return 0;
+}
+
+void BPF_STRUCT_OPS(set_state, struct sock *sk, __u8 new_state)
+{
+ return;
+}
+
+void BPF_STRUCT_OPS(cwnd_event, struct sock *sk, enum tcp_ca_event ev)
+{
+ return;
+}
+
+__u32 BPF_STRUCT_OPS(cwnd_undo, struct sock *sk)
+{
+ return 0;
+}
+
+void BPF_STRUCT_OPS(cong_avoid, struct sock *sk, __u32 ack, __u32 acked)
+{
+ return;
+}
+
+SEC(".struct_ops")
+struct tcp_congestion_ops bt_e2e_tco = {
+ .init = (void *)tcp_init,
+ .in_ack_event = (void *)in_ack_event,
+ .cwnd_event = (void *)cwnd_event,
+ .ssthresh = (void *)ssthresh,
+ .cong_avoid = (void *)cong_avoid,
+ .undo_cwnd = (void *)cwnd_undo,
+ .set_state = (void *)set_state,
+ .name = "bt_e2e_tco",
+};
diff --git a/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs b/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
index 90187152c1d1..ecb9e92d7bac 100644
--- a/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
+++ b/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
@@ -7,6 +7,7 @@ mod bpftool_tests_skel {
use bpftool_tests_skel::BpftoolTestsSkel;
use bpftool_tests_skel::BpftoolTestsSkelBuilder;
use libbpf_rs::skel::OpenSkel;
+use libbpf_rs::skel::Skel;
use libbpf_rs::skel::SkelBuilder;
use libbpf_rs::Program;
use serde::Deserialize;
@@ -16,6 +17,7 @@ mod bpftool_tests_skel {
const BPFTOOL_PATH_ENV: &str = "BPFTOOL_PATH";
const BPFTOOL_PATH: &str = "/usr/sbin/bpftool";
+const STRUCT_OPS_MAP_NAME: &str = "bt_e2e_tco";
/// A struct representing a pid entry from map/prog dump
#[derive(Serialize, Deserialize, Debug)]
@@ -62,6 +64,34 @@ struct MapItem {
formatted: Option<FormattedMapItem>,
}
+/// A struct representing the map info from `bpftool struct_ops dump id <id>`
+#[derive(Serialize, Deserialize, Debug)]
+struct MapInfo {
+ name: String,
+ id: u64,
+}
+
+/// A struct representing a struct_ops entry from `bpftool struct_ops list -j`
+#[derive(Serialize, Deserialize, Debug)]
+struct StructOps {
+ name: String,
+ id: u64,
+ kernel_struct_ops: String,
+}
+
+/// A struct representing a struct_ops entry from `bpftool struct_ops dump id <id>`
+#[derive(Serialize, Deserialize, Debug)]
+struct StructOpsCongestion {}
+
+/// A struct representing a struct_ops entry from `bpftool struct_ops dump id <id>`
+/// The returned json seems to be a tuple of two elements.
+/// the first one being a MapInfo, the second one being a StructOpsCongestion.
+#[derive(Serialize, Deserialize, Debug)]
+struct StructOpsDump {
+ bpf_map_info: Option<MapInfo>,
+ bpf_struct_ops_tcp_congestion_ops: Option<StructOpsCongestion>,
+}
+
/// A helper function to convert a vector of strings as returned by bpftool
/// into a vector of bytes.
/// bpftool returns key/value in the form of a sequence of strings
@@ -264,3 +294,118 @@ fn run_bpftool_prog_show_id() {
);
assert_eq!("tracepoint", prog.r#type);
}
+
+/// A test to validate that we can list struct_ops using bpftool
+#[test]
+fn run_bpftool_struct_ops_list() {
+ let _skel = setup().expect("Failed to set up BPF program");
+ let output = run_bpftool_command(&["struct_ops", "list", "--json"]);
+
+ let maps =
+ serde_json::from_slice::<Vec<StructOps>>(&output.stdout).expect("Failed to parse JSON");
+
+ assert!(output.status.success(), "bpftool returned an error.");
+ assert!(!maps.is_empty(), "No maps were listed");
+}
+
+/// A test to validate that we can dump a struct_ops program using its name
+#[test]
+fn run_bpftool_struct_ops_dump_name() {
+ let _skel = setup().expect("Failed to set up BPF program");
+ let output = run_bpftool_command(&[
+ "struct_ops",
+ "dump",
+ "name",
+ STRUCT_OPS_MAP_NAME,
+ "--pretty",
+ ]);
+
+ assert!(output.status.success(), "bpftool returned an error.");
+
+ let struct_ops =
+ serde_json::from_slice::<Vec<StructOpsDump>>(&output.stdout).expect("Failed to parse JSON");
+ assert!(!struct_ops.is_empty(), "No struct_ops were found");
+
+ assert_eq!(
+ struct_ops[0]
+ .bpf_map_info
+ .as_ref()
+ .expect("Missing bpf_map_info field")
+ .name,
+ STRUCT_OPS_MAP_NAME
+ );
+}
+
+/// A test to validate that we can unregister a struct_ops using its id
+#[test]
+fn run_bpftool_struct_ops_can_unregister_id() {
+ let skel = setup().expect("Failed to set up BPF program");
+ let output = run_bpftool_command(&[
+ "struct_ops",
+ "dump",
+ "name",
+ STRUCT_OPS_MAP_NAME,
+ "--pretty",
+ ]);
+
+ let _link = skel
+ .object()
+ .map(STRUCT_OPS_MAP_NAME)
+ .unwrap_or_else(|| panic!("Could not find map {}", STRUCT_OPS_MAP_NAME))
+ .attach_struct_ops()
+ .expect("Could not attach to struct_ops");
+
+ assert!(output.status.success(), "bpftool returned an error.");
+
+ let struct_ops =
+ serde_json::from_slice::<Vec<StructOpsDump>>(&output.stdout).expect("Failed to parse JSON");
+
+ assert!(!struct_ops.is_empty(), "No struct_ops were found");
+
+ let id = struct_ops[0]
+ .bpf_map_info
+ .as_ref()
+ .expect("Missing bpf_map_info field")
+ .id;
+
+ let output = run_bpftool_command(&["struct_ops", "unregister", "id", &id.to_string()]);
+ assert!(
+ output.status.success(),
+ "Failed to unregister struct_ops program: {:?}",
+ output
+ );
+}
+
+/// A test to validate that we can unregister a struct_ops using its name
+#[test]
+fn run_bpftool_struct_ops_can_unregister_name() {
+ let skel = setup().expect("Failed to set up BPF program");
+ let output = run_bpftool_command(&[
+ "struct_ops",
+ "dump",
+ "name",
+ STRUCT_OPS_MAP_NAME,
+ "--pretty",
+ ]);
+
+ let _link = skel
+ .object()
+ .map(STRUCT_OPS_MAP_NAME)
+ .unwrap_or_else(|| panic!("Could not find map {}", STRUCT_OPS_MAP_NAME))
+ .attach_struct_ops()
+ .expect("Could not attach to struct_ops");
+
+ assert!(output.status.success(), "bpftool returned an error.");
+
+ let struct_ops =
+ serde_json::from_slice::<Vec<StructOpsDump>>(&output.stdout).expect("Failed to parse JSON");
+
+ assert!(!struct_ops.is_empty(), "No struct_ops were found");
+
+ let output = run_bpftool_command(&["struct_ops", "unregister", "name", STRUCT_OPS_MAP_NAME]);
+ assert!(
+ output.status.success(),
+ "Failed to unregister struct_ops program: {:?}",
+ output
+ );
+}
--
2.39.3
next prev parent reply other threads:[~2023-11-16 19:43 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-16 19:42 [PATCH v1 bpf-next 0/9] bpftool: Add end-to-end testing Manu Bretelle
2023-11-16 19:42 ` [PATCH v1 bpf-next 1/9] bpftool: add testing skeleton Manu Bretelle
2023-11-21 1:37 ` Alexei Starovoitov
2023-11-21 16:26 ` Quentin Monnet
2023-11-21 16:42 ` Alexei Starovoitov
2023-11-21 19:50 ` Andrii Nakryiko
2023-11-27 17:07 ` Quentin Monnet
2023-11-27 18:39 ` Andrii Nakryiko
2023-12-15 6:26 ` Manu Bretelle
2023-11-27 17:07 ` Quentin Monnet
2023-12-15 6:37 ` Manu Bretelle
2023-11-21 16:26 ` Quentin Monnet
2023-11-16 19:42 ` [PATCH v1 bpf-next 2/9] bpftool: add libbpf-rs dependency and minimal bpf program Manu Bretelle
2023-11-16 19:42 ` [PATCH v1 bpf-next 3/9] bpftool: open and load bpf object Manu Bretelle
2023-11-16 19:42 ` [PATCH v1 bpf-next 4/9] bpftool: Add test to verify that pids are associated to maps Manu Bretelle
2023-11-21 16:26 ` Quentin Monnet
2023-11-16 19:42 ` [PATCH v1 bpf-next 5/9] bpftool: add test for bpftool prog Manu Bretelle
2023-11-21 16:26 ` Quentin Monnet
2023-11-16 19:42 ` [PATCH v1 bpf-next 6/9] bpftool: test that we can dump and read the content of a map Manu Bretelle
2023-11-21 16:26 ` Quentin Monnet
2023-11-16 19:42 ` Manu Bretelle [this message]
2023-11-16 19:42 ` [PATCH v1 bpf-next 8/9] bpftool: Add bpftool_tests README.md Manu Bretelle
2023-11-21 16:26 ` Quentin Monnet
2023-11-16 19:42 ` [PATCH v1 bpf-next 9/9] bpftool: Add Makefile to facilitate bpftool_tests usage Manu Bretelle
2023-11-21 16:26 ` Quentin Monnet
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231116194236.1345035-8-chantr4@gmail.com \
--to=chantr4@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kpsingh@kernel.org \
--cc=martin.lau@linux.dev \
--cc=quentin@isovalent.com \
--cc=sdf@google.com \
--cc=song@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox