Commit f59b1460 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

selftests/bpf: add number iterator tests

Add number iterator (bpf_iter_num_{new,next,destroy}()) tests,
validating the correct handling of various corner and common cases
*at runtime*.
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20230308184121.1165081-8-andrii@kernel.orgSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 57400dcc
......@@ -6,10 +6,59 @@
#include "iters.skel.h"
#include "iters_state_safety.skel.h"
#include "iters_looping.skel.h"
#include "iters_num.skel.h"
static void subtest_num_iters(void)
{
struct iters_num *skel;
int err;
skel = iters_num__open_and_load();
if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
return;
err = iters_num__attach(skel);
if (!ASSERT_OK(err, "skel_attach"))
goto cleanup;
usleep(1);
iters_num__detach(skel);
#define VALIDATE_CASE(case_name) \
ASSERT_EQ(skel->bss->res_##case_name, \
skel->rodata->exp_##case_name, \
#case_name)
VALIDATE_CASE(empty_zero);
VALIDATE_CASE(empty_int_min);
VALIDATE_CASE(empty_int_max);
VALIDATE_CASE(empty_minus_one);
VALIDATE_CASE(simple_sum);
VALIDATE_CASE(neg_sum);
VALIDATE_CASE(very_neg_sum);
VALIDATE_CASE(neg_pos_sum);
VALIDATE_CASE(invalid_range);
VALIDATE_CASE(max_range);
VALIDATE_CASE(e2big_range);
VALIDATE_CASE(succ_elem_cnt);
VALIDATE_CASE(overfetched_elem_cnt);
VALIDATE_CASE(fail_elem_cnt);
#undef VALIDATE_CASE
cleanup:
iters_num__destroy(skel);
}
void test_iters(void)
{
RUN_TESTS(iters_state_safety);
RUN_TESTS(iters_looping);
RUN_TESTS(iters);
if (test__start_subtest("num"))
subtest_num_iters();
}
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
#include <limits.h>
#include <linux/errno.h>
#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include "bpf_misc.h"
const volatile __s64 exp_empty_zero = 0 + 1;
__s64 res_empty_zero;
SEC("raw_tp/sys_enter")
int num_empty_zero(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, 0, 0) sum += i;
res_empty_zero = 1 + sum;
return 0;
}
const volatile __s64 exp_empty_int_min = 0 + 2;
__s64 res_empty_int_min;
SEC("raw_tp/sys_enter")
int num_empty_int_min(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, INT_MIN, INT_MIN) sum += i;
res_empty_int_min = 2 + sum;
return 0;
}
const volatile __s64 exp_empty_int_max = 0 + 3;
__s64 res_empty_int_max;
SEC("raw_tp/sys_enter")
int num_empty_int_max(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, INT_MAX, INT_MAX) sum += i;
res_empty_int_max = 3 + sum;
return 0;
}
const volatile __s64 exp_empty_minus_one = 0 + 4;
__s64 res_empty_minus_one;
SEC("raw_tp/sys_enter")
int num_empty_minus_one(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, -1, -1) sum += i;
res_empty_minus_one = 4 + sum;
return 0;
}
const volatile __s64 exp_simple_sum = 9 * 10 / 2;
__s64 res_simple_sum;
SEC("raw_tp/sys_enter")
int num_simple_sum(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, 0, 10) sum += i;
res_simple_sum = sum;
return 0;
}
const volatile __s64 exp_neg_sum = -11 * 10 / 2;
__s64 res_neg_sum;
SEC("raw_tp/sys_enter")
int num_neg_sum(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, -10, 0) sum += i;
res_neg_sum = sum;
return 0;
}
const volatile __s64 exp_very_neg_sum = INT_MIN + (__s64)(INT_MIN + 1);
__s64 res_very_neg_sum;
SEC("raw_tp/sys_enter")
int num_very_neg_sum(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, INT_MIN, INT_MIN + 2) sum += i;
res_very_neg_sum = sum;
return 0;
}
const volatile __s64 exp_very_big_sum = (__s64)(INT_MAX - 1) + (__s64)(INT_MAX - 2);
__s64 res_very_big_sum;
SEC("raw_tp/sys_enter")
int num_very_big_sum(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, INT_MAX - 2, INT_MAX) sum += i;
res_very_big_sum = sum;
return 0;
}
const volatile __s64 exp_neg_pos_sum = -3;
__s64 res_neg_pos_sum;
SEC("raw_tp/sys_enter")
int num_neg_pos_sum(const void *ctx)
{
__s64 sum = 0, i;
bpf_for(i, -3, 3) sum += i;
res_neg_pos_sum = sum;
return 0;
}
const volatile __s64 exp_invalid_range = -EINVAL;
__s64 res_invalid_range;
SEC("raw_tp/sys_enter")
int num_invalid_range(const void *ctx)
{
struct bpf_iter_num it;
res_invalid_range = bpf_iter_num_new(&it, 1, 0);
bpf_iter_num_destroy(&it);
return 0;
}
const volatile __s64 exp_max_range = 0 + 10;
__s64 res_max_range;
SEC("raw_tp/sys_enter")
int num_max_range(const void *ctx)
{
struct bpf_iter_num it;
res_max_range = 10 + bpf_iter_num_new(&it, 0, BPF_MAX_LOOPS);
bpf_iter_num_destroy(&it);
return 0;
}
const volatile __s64 exp_e2big_range = -E2BIG;
__s64 res_e2big_range;
SEC("raw_tp/sys_enter")
int num_e2big_range(const void *ctx)
{
struct bpf_iter_num it;
res_e2big_range = bpf_iter_num_new(&it, -1, BPF_MAX_LOOPS);
bpf_iter_num_destroy(&it);
return 0;
}
const volatile __s64 exp_succ_elem_cnt = 10;
__s64 res_succ_elem_cnt;
SEC("raw_tp/sys_enter")
int num_succ_elem_cnt(const void *ctx)
{
struct bpf_iter_num it;
int cnt = 0, *v;
bpf_iter_num_new(&it, 0, 10);
while ((v = bpf_iter_num_next(&it))) {
cnt++;
}
bpf_iter_num_destroy(&it);
res_succ_elem_cnt = cnt;
return 0;
}
const volatile __s64 exp_overfetched_elem_cnt = 5;
__s64 res_overfetched_elem_cnt;
SEC("raw_tp/sys_enter")
int num_overfetched_elem_cnt(const void *ctx)
{
struct bpf_iter_num it;
int cnt = 0, *v, i;
bpf_iter_num_new(&it, 0, 5);
for (i = 0; i < 10; i++) {
v = bpf_iter_num_next(&it);
if (v)
cnt++;
}
bpf_iter_num_destroy(&it);
res_overfetched_elem_cnt = cnt;
return 0;
}
const volatile __s64 exp_fail_elem_cnt = 20 + 0;
__s64 res_fail_elem_cnt;
SEC("raw_tp/sys_enter")
int num_fail_elem_cnt(const void *ctx)
{
struct bpf_iter_num it;
int cnt = 0, *v, i;
bpf_iter_num_new(&it, 100, 10);
for (i = 0; i < 10; i++) {
v = bpf_iter_num_next(&it);
if (v)
cnt++;
}
bpf_iter_num_destroy(&it);
res_fail_elem_cnt = 20 + cnt;
return 0;
}
char _license[] SEC("license") = "GPL";
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment