Skip to content

Commit e81fbd4

Browse files
danobiborkmann
authored andcommitted
selftests/bpf: Add existing connection bpf_*_ct_lookup() test
Add a test where we do a conntrack lookup on an existing connection. This is nice because it's a more realistic test than artifically creating a ct entry and looking it up afterwards. Signed-off-by: Daniel Xu <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/bpf/de5a617832f38f8b5631cc87e2a836da7c94d497.1660254747.git.dxu@dxuuu.xyz
1 parent cea5588 commit e81fbd4

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,34 @@ enum {
2424
TEST_TC_BPF,
2525
};
2626

27+
#define TIMEOUT_MS 3000
28+
29+
static int connect_to_server(int srv_fd)
30+
{
31+
int fd = -1;
32+
33+
fd = socket(AF_INET, SOCK_STREAM, 0);
34+
if (!ASSERT_GE(fd, 0, "socket"))
35+
goto out;
36+
37+
if (!ASSERT_EQ(connect_fd_to_fd(fd, srv_fd, TIMEOUT_MS), 0, "connect_fd_to_fd")) {
38+
close(fd);
39+
fd = -1;
40+
}
41+
out:
42+
return fd;
43+
}
44+
2745
static void test_bpf_nf_ct(int mode)
2846
{
47+
const char *iptables = "iptables -t raw %s PREROUTING -j CT";
48+
int srv_fd = -1, client_fd = -1, srv_client_fd = -1;
49+
struct sockaddr_in peer_addr = {};
2950
struct test_bpf_nf *skel;
3051
int prog_fd, err;
52+
socklen_t len;
53+
u16 srv_port;
54+
char cmd[64];
3155
LIBBPF_OPTS(bpf_test_run_opts, topts,
3256
.data_in = &pkt_v4,
3357
.data_size_in = sizeof(pkt_v4),
@@ -38,6 +62,32 @@ static void test_bpf_nf_ct(int mode)
3862
if (!ASSERT_OK_PTR(skel, "test_bpf_nf__open_and_load"))
3963
return;
4064

65+
/* Enable connection tracking */
66+
snprintf(cmd, sizeof(cmd), iptables, "-A");
67+
if (!ASSERT_OK(system(cmd), "iptables"))
68+
goto end;
69+
70+
srv_port = (mode == TEST_XDP) ? 5005 : 5006;
71+
srv_fd = start_server(AF_INET, SOCK_STREAM, "127.0.0.1", srv_port, TIMEOUT_MS);
72+
if (!ASSERT_GE(srv_fd, 0, "start_server"))
73+
goto end;
74+
75+
client_fd = connect_to_server(srv_fd);
76+
if (!ASSERT_GE(client_fd, 0, "connect_to_server"))
77+
goto end;
78+
79+
len = sizeof(peer_addr);
80+
srv_client_fd = accept(srv_fd, (struct sockaddr *)&peer_addr, &len);
81+
if (!ASSERT_GE(srv_client_fd, 0, "accept"))
82+
goto end;
83+
if (!ASSERT_EQ(len, sizeof(struct sockaddr_in), "sockaddr len"))
84+
goto end;
85+
86+
skel->bss->saddr = peer_addr.sin_addr.s_addr;
87+
skel->bss->sport = peer_addr.sin_port;
88+
skel->bss->daddr = peer_addr.sin_addr.s_addr;
89+
skel->bss->dport = htons(srv_port);
90+
4191
if (mode == TEST_XDP)
4292
prog_fd = bpf_program__fd(skel->progs.nf_xdp_ct_test);
4393
else
@@ -63,7 +113,16 @@ static void test_bpf_nf_ct(int mode)
63113
ASSERT_LE(skel->bss->test_delta_timeout, 10, "Test for max ct timeout update");
64114
/* expected status is IPS_SEEN_REPLY */
65115
ASSERT_EQ(skel->bss->test_status, 2, "Test for ct status update ");
116+
ASSERT_EQ(skel->data->test_exist_lookup, 0, "Test existing connection lookup");
66117
end:
118+
if (srv_client_fd != -1)
119+
close(srv_client_fd);
120+
if (client_fd != -1)
121+
close(client_fd);
122+
if (srv_fd != -1)
123+
close(srv_fd);
124+
snprintf(cmd, sizeof(cmd), iptables, "-D");
125+
system(cmd);
67126
test_bpf_nf__destroy(skel);
68127
}
69128

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ int test_insert_entry = -EAFNOSUPPORT;
2323
int test_succ_lookup = -ENOENT;
2424
u32 test_delta_timeout = 0;
2525
u32 test_status = 0;
26+
__be32 saddr = 0;
27+
__be16 sport = 0;
28+
__be32 daddr = 0;
29+
__be16 dport = 0;
30+
int test_exist_lookup = -ENOENT;
2631

2732
struct nf_conn;
2833

@@ -160,6 +165,19 @@ nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,
160165
}
161166
test_alloc_entry = 0;
162167
}
168+
169+
bpf_tuple.ipv4.saddr = saddr;
170+
bpf_tuple.ipv4.daddr = daddr;
171+
bpf_tuple.ipv4.sport = sport;
172+
bpf_tuple.ipv4.dport = dport;
173+
ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
174+
sizeof(opts_def));
175+
if (ct) {
176+
test_exist_lookup = 0;
177+
bpf_ct_release(ct);
178+
} else {
179+
test_exist_lookup = opts_def.error;
180+
}
163181
}
164182

165183
SEC("xdp")

0 commit comments

Comments
 (0)