diff mbox series

[v1,bpf-next,7/9] bpftool: Add struct_ops tests

Message ID 20231116194236.1345035-8-chantr4@gmail.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series bpftool: Add end-to-end testing | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers warning 4 maintainers not CCed: yonghong.song@linux.dev linux-kselftest@vger.kernel.org mykolal@fb.com shuah@kernel.org
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch fail ERROR: Macros with complex values should be enclosed in parentheses WARNING: line length of 100 exceeds 80 columns WARNING: line length of 83 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns WARNING: line length of 96 exceeds 80 columns WARNING: line length of 97 exceeds 80 columns WARNING: void function return statements are not generally useful
netdev/build_clang_rust fail Link
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-12 success Logs for s390x-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Validate matrix.py
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-3 success Logs for aarch64-gcc / build / build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-0 success Logs for Lint
bpf/vmtest-bpf-next-VM_Test-8 success Logs for aarch64-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-10 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-11 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-6 success Logs for aarch64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for s390x-gcc / build / build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for aarch64-gcc / test (test_verifier, false, 360) / test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for aarch64-gcc / test (test_maps, false, 360) / test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for s390x-gcc / veristat
bpf/vmtest-bpf-next-VM_Test-15 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-16 success Logs for x86_64-gcc / build / build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for x86_64-gcc / test (test_maps, false, 360) / test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-20 success Logs for x86_64-gcc / test (test_progs_no_alu32_parallel, true, 30) / test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for x86_64-gcc / test (test_progs_parallel, true, 30) / test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for x86_64-gcc / test (test_verifier, false, 360) / test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-23 success Logs for x86_64-gcc / veristat / veristat on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for x86_64-llvm-16 / build / build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-28 success Logs for x86_64-llvm-16 / test (test_verifier, false, 360) / test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for x86_64-llvm-16 / veristat
bpf/vmtest-bpf-next-VM_Test-5 success Logs for aarch64-gcc / test (test_progs, false, 360) / test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for x86_64-gcc / test (test_progs, false, 360) / test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for x86_64-gcc / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for x86_64-llvm-16 / test (test_maps, false, 360) / test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-26 success Logs for x86_64-llvm-16 / test (test_progs, false, 360) / test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for x86_64-llvm-16 / test (test_progs_no_alu32, false, 360) / test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-13 success Logs for s390x-gcc / test (test_verifier, false, 360) / test_verifier on s390x with gcc

Commit Message

Manu Bretelle Nov. 16, 2023, 7:42 p.m. UTC
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 mbox series

Patch

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
+    );
+}