Skip to content

Commit 2d8aed6

Browse files
LorenzoBianconiKernel Patches Daemon
authored andcommitted
selftests/bpf: add selftest for bpf_ct_refresh_timeout kfunc
Install a new ct entry in order to perform a successful lookup and test bpf_ct_refresh_timeout kfunc helper. Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
1 parent 203a14e commit 2d8aed6

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_nf.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ void test_bpf_nf_ct(int mode)
1818
.repeat = 1,
1919
);
2020

21+
/* Flush previous nft ct entries */
22+
ASSERT_OK(system("conntrack -F"), "flush ct entries");
23+
/* Let's create a nft ct entry to perform lookup */
24+
ASSERT_OK(system("conntrack -I -s 1.1.1.1 -d 2.2.2.2 --protonum 6 \
25+
--state ESTABLISHED --timeout 3600 --sport 12345 \
26+
--dport 1000 --zone 0"), "create ct entry");
27+
2128
skel = test_bpf_nf__open_and_load();
2229
if (!ASSERT_OK_PTR(skel, "test_bpf_nf__open_and_load"))
2330
return;
@@ -39,6 +46,9 @@ void test_bpf_nf_ct(int mode)
3946
ASSERT_EQ(skel->bss->test_enonet_netns_id, -ENONET, "Test ENONET for bad but valid netns_id");
4047
ASSERT_EQ(skel->bss->test_enoent_lookup, -ENOENT, "Test ENOENT for failed lookup");
4148
ASSERT_EQ(skel->bss->test_eafnosupport, -EAFNOSUPPORT, "Test EAFNOSUPPORT for invalid len__tuple");
49+
ASSERT_EQ(skel->bss->test_succ_lookup, 0, "Test for successful lookup");
50+
ASSERT_EQ(skel->bss->test_delta_timeout, 10, "Test for ct timeout update");
51+
4252
end:
4353
test_bpf_nf__destroy(skel);
4454
}

tools/testing/selftests/bpf/progs/test_bpf_nf.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <vmlinux.h>
33
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_endian.h>
45

56
#define EAFNOSUPPORT 97
67
#define EPROTO 71
78
#define ENONET 64
89
#define EINVAL 22
910
#define ENOENT 2
1011

12+
extern unsigned long CONFIG_HZ __kconfig;
13+
1114
int test_einval_bpf_tuple = 0;
1215
int test_einval_reserved = 0;
1316
int test_einval_netns_id = 0;
@@ -16,6 +19,8 @@ int test_eproto_l4proto = 0;
1619
int test_enonet_netns_id = 0;
1720
int test_enoent_lookup = 0;
1821
int test_eafnosupport = 0;
22+
int test_succ_lookup = 0;
23+
u32 test_delta_timeout = 0;
1924

2025
struct nf_conn;
2126

@@ -31,6 +36,7 @@ struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
3136
struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
3237
struct bpf_ct_opts___local *, u32) __ksym;
3338
void bpf_ct_release(struct nf_conn *) __ksym;
39+
void bpf_ct_refresh_timeout(struct nf_conn *, u32) __ksym;
3440

3541
static __always_inline void
3642
nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32,
@@ -99,6 +105,22 @@ nf_ct_test(struct nf_conn *(*func)(void *, struct bpf_sock_tuple *, u32,
99105
bpf_ct_release(ct);
100106
else
101107
test_eafnosupport = opts_def.error;
108+
109+
bpf_tuple.ipv4.saddr = 0x01010101; /* src IP 1.1.1.1 */
110+
bpf_tuple.ipv4.daddr = 0x02020202; /* dst IP 2.2.2.2 */
111+
bpf_tuple.ipv4.sport = bpf_htons(12345); /* src port */
112+
bpf_tuple.ipv4.dport = bpf_htons(1000); /* dst port */
113+
ct = func(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
114+
sizeof(opts_def));
115+
if (ct) {
116+
/* update ct entry timeout */
117+
bpf_ct_refresh_timeout(ct, 10000);
118+
test_delta_timeout = ct->timeout - bpf_jiffies64();
119+
test_delta_timeout /= CONFIG_HZ;
120+
bpf_ct_release(ct);
121+
} else {
122+
test_succ_lookup = opts_def.error;
123+
}
102124
}
103125

104126
SEC("xdp")

0 commit comments

Comments
 (0)