selftests/bpf: Add tests for ring_buffer__ring
authorMartin Kelly <martin.kelly@crowdstrike.com>
Mon, 25 Sep 2023 21:50:35 +0000 (14:50 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Mon, 25 Sep 2023 23:22:42 +0000 (16:22 -0700)
Add tests for the new API ring_buffer__ring.

Signed-off-by: Martin Kelly <martin.kelly@crowdstrike.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20230925215045.2375758-5-martin.kelly@crowdstrike.com
tools/testing/selftests/bpf/prog_tests/ringbuf_multi.c

index 1455911d9fcbe7d769306382229e8ea3fd8902dc..58522195081b39afc24b986e1f0df4b25c64da9c 100644 (file)
@@ -42,6 +42,8 @@ void test_ringbuf_multi(void)
 {
        struct test_ringbuf_multi *skel;
        struct ring_buffer *ringbuf = NULL;
+       struct ring *ring_old;
+       struct ring *ring;
        int err;
        int page_size = getpagesize();
        int proto_fd = -1;
@@ -84,11 +86,24 @@ void test_ringbuf_multi(void)
        if (CHECK(!ringbuf, "ringbuf_create", "failed to create ringbuf\n"))
                goto cleanup;
 
+       /* verify ring_buffer__ring returns expected results */
+       ring = ring_buffer__ring(ringbuf, 0);
+       if (!ASSERT_OK_PTR(ring, "ring_buffer__ring_idx_0"))
+               goto cleanup;
+       ring_old = ring;
+       ring = ring_buffer__ring(ringbuf, 1);
+       ASSERT_ERR_PTR(ring, "ring_buffer__ring_idx_1");
+
        err = ring_buffer__add(ringbuf, bpf_map__fd(skel->maps.ringbuf2),
                              process_sample, (void *)(long)2);
        if (CHECK(err, "ringbuf_add", "failed to add another ring\n"))
                goto cleanup;
 
+       /* verify adding a new ring didn't invalidate our older pointer */
+       ring = ring_buffer__ring(ringbuf, 0);
+       if (!ASSERT_EQ(ring, ring_old, "ring_buffer__ring_again"))
+               goto cleanup;
+
        err = test_ringbuf_multi__attach(skel);
        if (CHECK(err, "skel_attach", "skeleton attachment failed: %d\n", err))
                goto cleanup;