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 6/9] bpftool: test that we can dump and read the content of a map
Date: Thu, 16 Nov 2023 11:42:33 -0800 [thread overview]
Message-ID: <20231116194236.1345035-7-chantr4@gmail.com> (raw)
In-Reply-To: <20231116194236.1345035-1-chantr4@gmail.com>
This test adds a hardcoded key/value pair to a test map and verify
that bpftool is able to dump its content and read/format it.
$ BPFTOOL_PATH=../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 7 tests
Running command "../bpftool" "version"
test bpftool_tests::run_bpftool ... ok
Running command "../bpftool" "map" "dump" "id" "1848713" "--json"
Running command "../bpftool" "prog" "list" "--json"
Running command "../bpftool" "map" "list" "--json"
Running command "../bpftool" "prog" "list" "--json"
Running command "../bpftool" "prog" "show" "id" "3160759" "--json"
Running command "../bpftool" "map" "list" "--json"
test bpftool_tests::run_bpftool_map_dump_id ... ok
test bpftool_tests::run_bpftool_prog_show_id ... ok
test bpftool_tests::run_bpftool_map_pids ... ok
test bpftool_tests::run_bpftool_prog_pids ... ok
test bpftool_tests::run_bpftool_prog_list ... ok
test bpftool_tests::run_bpftool_map_list ... ok
test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured; 0 filtered
out; finished in 0.22s
Signed-off-by: Manu Bretelle <chantr4@gmail.com>
---
.../bpftool_tests/src/bpf/bpftool_tests.bpf.c | 7 ++
.../bpf/bpftool_tests/src/bpftool_tests.rs | 86 +++++++++++++++++++
2 files changed, 93 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 e2b18dd36207..a90c8921b4ee 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
@@ -11,6 +11,13 @@ struct {
__type(value, u64);
} pid_write_calls SEC(".maps");
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __uint(max_entries, 10);
+ __type(key, char[6]);
+ __type(value, char[6]);
+} bpftool_test_map SEC(".maps");
+
int my_pid = 0;
SEC("tp/syscalls/sys_enter_write")
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 695a1cbc5be8..90187152c1d1 100644
--- a/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
+++ b/tools/testing/selftests/bpf/bpftool_tests/src/bpftool_tests.rs
@@ -45,6 +45,37 @@ struct Map {
pids: Vec<Pid>,
}
+/// A struct representing a formatted map entry from `bpftool map dump -j`
+#[derive(Serialize, Deserialize, Debug)]
+struct FormattedMapItem {
+ key: String,
+ value: String,
+}
+
+type MapVecString = Vec<String>;
+
+/// A struct representing a map entry from `bpftool map dump -j`
+#[derive(Serialize, Deserialize, Debug)]
+struct MapItem {
+ key: MapVecString,
+ value: MapVecString,
+ formatted: Option<FormattedMapItem>,
+}
+
+/// 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
+/// hexadecimal numbers. We need to convert them back to bytes.
+/// for instance, the value of the key "key" is represented as ["0x6b","0x65","0x79"]
+fn to_vec_u8(m: &MapVecString) -> Vec<u8> {
+ m.iter()
+ .map(|s| {
+ u8::from_str_radix(s.trim_start_matches("0x"), 16)
+ .unwrap_or_else(|_| panic!("Failed to parse {:?}", s))
+ })
+ .collect()
+}
+
/// Setup our bpftool_tests.bpf.c program.
/// Open and load and return an opened object.
fn setup() -> Result<BpftoolTestsSkel<'static>> {
@@ -114,6 +145,61 @@ fn run_bpftool_map_pids() {
);
}
+/// A test to validate that we can run `bpftool map dump <id>`
+/// and extract the expected information.
+/// The test adds a key, value pair to the map and then dumps it.
+/// The test validates that the dumped data matches what was added.
+/// It also validate that bpftool was able to format the key/value pairs.
+#[test]
+fn run_bpftool_map_dump_id() {
+ // By having key/value null terminated, we can check that bpftool also returns the
+ // formatted content.
+ let key = b"key\0\0\0";
+ let value = b"value\0";
+ let skel = setup().expect("Failed to set up BPF program");
+ let binding = skel.maps();
+ let bpftool_test_map_map = binding.bpftool_test_map();
+ bpftool_test_map_map
+ .update(key, value, libbpf_rs::MapFlags::NO_EXIST)
+ .expect("Failed to update map");
+ let map_id = bpftool_test_map_map
+ .info()
+ .expect("Failed to get map info")
+ .info
+ .id;
+
+ let output = run_bpftool_command(&["map", "dump", "id", &map_id.to_string(), "--json"]);
+ assert!(output.status.success(), "bpftool returned an error.");
+
+ let items =
+ serde_json::from_slice::<Vec<MapItem>>(&output.stdout).expect("Failed to parse JSON");
+
+ assert_eq!(items.len(), 1);
+
+ let item = items.first().expect("Expected a map item");
+ assert_eq!(to_vec_u8(&item.key), key);
+ assert_eq!(to_vec_u8(&item.value), value);
+
+ // Validate "formatted" values.
+ // The keys and values are null terminated so we need to trim them before comparing.
+ let formatted = item
+ .formatted
+ .as_ref()
+ .expect("Formatted values are missing");
+ assert_eq!(
+ formatted.key,
+ std::str::from_utf8(key)
+ .expect("Invalid UTF-8")
+ .trim_end_matches('\0'),
+ );
+ assert_eq!(
+ formatted.value,
+ std::str::from_utf8(value)
+ .expect("Invalid UTF-8")
+ .trim_end_matches('\0'),
+ );
+}
+
/// A test to validate that we can list programs using bpftool
#[test]
fn run_bpftool_prog_list() {
--
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 ` Manu Bretelle [this message]
2023-11-21 16:26 ` [PATCH v1 bpf-next 6/9] bpftool: test that we can dump and read the content of a map Quentin Monnet
2023-11-16 19:42 ` [PATCH v1 bpf-next 7/9] bpftool: Add struct_ops tests Manu Bretelle
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-7-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