Skip to content

Commit

Permalink
selftests/bpf: Add bpf_loop test
Browse files Browse the repository at this point in the history
Add test for bpf_loop testing a variety of cases:
various nr_loops, null callback ctx, invalid flags, nested callbacks.

Signed-off-by: Joanne Koong <joannekoong@fb.com>
  • Loading branch information
jkoong-fb authored and intel-lab-lkp committed Nov 29, 2021
1 parent 36deec7 commit ed6ffe9
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
138 changes: 138 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/bpf_loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */

#include <test_progs.h>
#include <network_helpers.h>
#include "bpf_loop.skel.h"

static void check_nr_loops(struct bpf_loop *skel)
{
__u32 retval, duration;
int err;

/* test 0 loops */
skel->bss->nr_loops = 0;
err = bpf_prog_test_run(bpf_program__fd(skel->progs.test_prog),
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
&retval, &duration);
if (!ASSERT_OK(err, "err") || !ASSERT_OK(retval, "retval"))
return;
ASSERT_EQ(skel->bss->nr_loops_returned, skel->bss->nr_loops,
"0 loops");

/* test 500 loops */
skel->bss->nr_loops = 500;
err = bpf_prog_test_run(bpf_program__fd(skel->progs.test_prog),
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
&retval, &duration);
if (!ASSERT_OK(err, "err") ||
!ASSERT_OK(retval, "retval"))
return;
ASSERT_EQ(skel->bss->nr_loops_returned, skel->bss->nr_loops,
"500 loops");
ASSERT_EQ(skel->bss->g_output, (500 * 499) / 2, "g_output");

/* test exceeding the max limit */
skel->bss->nr_loops = -1;
err = bpf_prog_test_run(bpf_program__fd(skel->progs.test_prog),
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
&retval, &duration);
if (!ASSERT_OK(err, "err") || !ASSERT_OK(retval, "retval"))
return;
ASSERT_EQ(skel->bss->err, -E2BIG, "over max limit");
}

static void check_callback_fn_stop(struct bpf_loop *skel)
{
__u32 retval, duration;
int err;

skel->bss->nr_loops = 400;
skel->data->stop_index = 50;

/* testing that loop is stopped when callback_fn returns 1 */
err = bpf_prog_test_run(bpf_program__fd(skel->progs.test_prog),
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
&retval, &duration);

if (!ASSERT_OK(err, "err") || !ASSERT_OK(retval, "retval"))
return;

ASSERT_EQ(skel->bss->nr_loops_returned, skel->data->stop_index + 1,
"nr_loops_returned");
ASSERT_EQ(skel->bss->g_output, (50 * 49) / 2,
"g_output");
}

static void check_null_callback_ctx(struct bpf_loop *skel)
{
__u32 retval, duration;
int err;

skel->bss->nr_loops = 10;

/* check that user is able to pass in a null callback_ctx */
err = bpf_prog_test_run(bpf_program__fd(skel->progs.prog_null_ctx),
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
&retval, &duration);

if (!ASSERT_OK(err, "err") || !ASSERT_OK(retval, "retval"))
return;

ASSERT_EQ(skel->bss->nr_loops_returned, skel->bss->nr_loops,
"nr_loops_returned");
}

static void check_invalid_flags(struct bpf_loop *skel)
{
__u32 retval, duration;
int err;

/* check that passing in non-zero flags returns -EINVAL */
err = bpf_prog_test_run(bpf_program__fd(skel->progs.prog_invalid_flags),
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
&retval, &duration);

if (!ASSERT_OK(err, "err") || !ASSERT_OK(retval, "retval"))
return;

ASSERT_EQ(skel->bss->err, -EINVAL, "err");
}

static void check_nested_calls(struct bpf_loop *skel)
{
__u32 nr_loops = 100, nested_callback_nr_loops = 4;
__u32 retval, duration;
int err;

skel->bss->nr_loops = nr_loops;
skel->bss->nested_callback_nr_loops = nested_callback_nr_loops;

/* check that nested calls are supported */
err = bpf_prog_test_run(bpf_program__fd(skel->progs.prog_nested_calls),
1, &pkt_v4, sizeof(pkt_v4), NULL, NULL,
&retval, &duration);
if (!ASSERT_OK(err, "err") || !ASSERT_OK(retval, "retval"))
return;
ASSERT_EQ(skel->bss->nr_loops_returned, nr_loops * nested_callback_nr_loops
* nested_callback_nr_loops, "nr_loops_returned");
ASSERT_EQ(skel->bss->g_output, (4 * 3) / 2 * nested_callback_nr_loops
* nr_loops, "g_output");
}

void test_bpf_loop(void)
{
struct bpf_loop *skel;

skel = bpf_loop__open_and_load();
if (!ASSERT_OK_PTR(skel, "bpf_loop__open_and_load"))
return;

check_nr_loops(skel);
check_callback_fn_stop(skel);
check_null_callback_ctx(skel);
check_invalid_flags(skel);
check_nested_calls(skel);

bpf_loop__destroy(skel);
}
99 changes: 99 additions & 0 deletions tools/testing/selftests/bpf/progs/bpf_loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2021 Facebook */

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>

char _license[] SEC("license") = "GPL";

struct callback_ctx {
int output;
};

/* These should be set by the user program */
u32 nested_callback_nr_loops;
u32 stop_index = -1;
u32 nr_loops;

/* Making these global variables so that the userspace program
* can verify the output through the skeleton
*/
int nr_loops_returned;
int g_output;
int err;

static int callback(__u32 index, void *data)
{
struct callback_ctx *ctx = data;

if (index >= stop_index)
return 1;

ctx->output += index;

return 0;
}

static int empty_callback(__u32 index, void *data)
{
return 0;
}

static int nested_callback2(__u32 index, void *data)
{
nr_loops_returned += bpf_loop(nested_callback_nr_loops, callback, data, 0);

return 0;
}

static int nested_callback1(__u32 index, void *data)
{
bpf_loop(nested_callback_nr_loops, nested_callback2, data, 0);
return 0;
}

SEC("tc")
int test_prog(struct __sk_buff *skb)
{
struct callback_ctx data = {};

nr_loops_returned = bpf_loop(nr_loops, callback, &data, 0);

if (nr_loops_returned < 0)
err = nr_loops_returned;
else
g_output = data.output;

return 0;
}

SEC("tc")
int prog_null_ctx(struct __sk_buff *skb)
{
nr_loops_returned = bpf_loop(nr_loops, empty_callback, NULL, 0);

return 0;
}

SEC("tc")
int prog_invalid_flags(struct __sk_buff *skb)
{
struct callback_ctx data = {};

err = bpf_loop(nr_loops, callback, &data, 1);

return 0;
}

SEC("tc")
int prog_nested_calls(struct __sk_buff *skb)
{
struct callback_ctx data = {};

nr_loops_returned = 0;
bpf_loop(nr_loops, nested_callback1, &data, 0);

g_output = data.output;

return 0;
}

0 comments on commit ed6ffe9

Please sign in to comment.