Skip to content

Commit ece5489

Browse files
ahduyckborkmann
authored andcommitted
selftest/bpf: Use global variables instead of maps for test_tcpbpf_kern
Use global variables instead of global_map and sockopt_results_map to track test data. Doing this greatly simplifies the code as there is not need to take the extra steps of updating the maps or looking up elements. Suggested-by: Martin KaFai Lau <[email protected]> Signed-off-by: Alexander Duyck <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/160417035730.2823.6697632421519908152.stgit@localhost.localdomain
1 parent c47c158 commit ece5489

File tree

3 files changed

+31
-110
lines changed

3 files changed

+31
-110
lines changed

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

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
static __u32 duration;
1313

14-
static void verify_result(int map_fd, int sock_map_fd)
14+
static void verify_result(struct tcpbpf_globals *result)
1515
{
1616
__u32 expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
1717
(1 << BPF_SOCK_OPS_RWND_INIT) |
@@ -21,48 +21,31 @@ static void verify_result(int map_fd, int sock_map_fd)
2121
(1 << BPF_SOCK_OPS_NEEDS_ECN) |
2222
(1 << BPF_SOCK_OPS_STATE_CB) |
2323
(1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
24-
struct tcpbpf_globals result = { 0 };
25-
__u32 key = 0;
26-
int res;
27-
int rv;
28-
29-
rv = bpf_map_lookup_elem(map_fd, &key, &result);
30-
if (CHECK(rv, "bpf_map_lookup_elem(map_fd)", "err:%d errno:%d",
31-
rv, errno))
32-
return;
3324

3425
/* check global map */
35-
CHECK(expected_events != result.event_map, "event_map",
26+
CHECK(expected_events != result->event_map, "event_map",
3627
"unexpected event_map: actual %#" PRIx32" != expected %#" PRIx32 "\n",
37-
result.event_map, expected_events);
28+
result->event_map, expected_events);
3829

39-
ASSERT_EQ(result.bytes_received, 501, "bytes_received");
40-
ASSERT_EQ(result.bytes_acked, 1002, "bytes_acked");
41-
ASSERT_EQ(result.data_segs_in, 1, "data_segs_in");
42-
ASSERT_EQ(result.data_segs_out, 1, "data_segs_out");
43-
ASSERT_EQ(result.bad_cb_test_rv, 0x80, "bad_cb_test_rv");
44-
ASSERT_EQ(result.good_cb_test_rv, 0, "good_cb_test_rv");
45-
ASSERT_EQ(result.num_listen, 1, "num_listen");
30+
ASSERT_EQ(result->bytes_received, 501, "bytes_received");
31+
ASSERT_EQ(result->bytes_acked, 1002, "bytes_acked");
32+
ASSERT_EQ(result->data_segs_in, 1, "data_segs_in");
33+
ASSERT_EQ(result->data_segs_out, 1, "data_segs_out");
34+
ASSERT_EQ(result->bad_cb_test_rv, 0x80, "bad_cb_test_rv");
35+
ASSERT_EQ(result->good_cb_test_rv, 0, "good_cb_test_rv");
36+
ASSERT_EQ(result->num_listen, 1, "num_listen");
4637

4738
/* 3 comes from one listening socket + both ends of the connection */
48-
ASSERT_EQ(result.num_close_events, 3, "num_close_events");
39+
ASSERT_EQ(result->num_close_events, 3, "num_close_events");
4940

5041
/* check setsockopt for SAVE_SYN */
51-
key = 0;
52-
rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
53-
CHECK(rv, "bpf_map_lookup_elem(sock_map_fd)", "err:%d errno:%d",
54-
rv, errno);
55-
ASSERT_EQ(res, 0, "bpf_setsockopt(TCP_SAVE_SYN)");
42+
ASSERT_EQ(result->tcp_save_syn, 0, "tcp_save_syn");
5643

5744
/* check getsockopt for SAVED_SYN */
58-
key = 1;
59-
rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
60-
CHECK(rv, "bpf_map_lookup_elem(sock_map_fd)", "err:%d errno:%d",
61-
rv, errno);
62-
ASSERT_EQ(res, 1, "bpf_getsockopt(TCP_SAVED_SYN)");
45+
ASSERT_EQ(result->tcp_saved_syn, 1, "tcp_saved_syn");
6346
}
6447

65-
static void run_test(int map_fd, int sock_map_fd)
48+
static void run_test(struct tcpbpf_globals *result)
6649
{
6750
int listen_fd = -1, cli_fd = -1, accept_fd = -1;
6851
char buf[1000];
@@ -129,13 +112,12 @@ static void run_test(int map_fd, int sock_map_fd)
129112
close(listen_fd);
130113

131114
if (!err)
132-
verify_result(map_fd, sock_map_fd);
115+
verify_result(result);
133116
}
134117

135118
void test_tcpbpf_user(void)
136119
{
137120
struct test_tcpbpf_kern *skel;
138-
int map_fd, sock_map_fd;
139121
int cg_fd = -1;
140122

141123
skel = test_tcpbpf_kern__open_and_load();
@@ -147,14 +129,11 @@ void test_tcpbpf_user(void)
147129
"cg_fd:%d errno:%d", cg_fd, errno))
148130
goto cleanup_skel;
149131

150-
map_fd = bpf_map__fd(skel->maps.global_map);
151-
sock_map_fd = bpf_map__fd(skel->maps.sockopt_results);
152-
153132
skel->links.bpf_testcb = bpf_program__attach_cgroup(skel->progs.bpf_testcb, cg_fd);
154133
if (ASSERT_OK_PTR(skel->links.bpf_testcb, "attach_cgroup(bpf_testcb)"))
155134
goto cleanup_namespace;
156135

157-
run_test(map_fd, sock_map_fd);
136+
run_test(&skel->bss->global);
158137

159138
cleanup_namespace:
160139
if (cg_fd != -1)

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

Lines changed: 13 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,7 @@
1414
#include <bpf/bpf_endian.h>
1515
#include "test_tcpbpf.h"
1616

17-
struct {
18-
__uint(type, BPF_MAP_TYPE_ARRAY);
19-
__uint(max_entries, 4);
20-
__type(key, __u32);
21-
__type(value, struct tcpbpf_globals);
22-
} global_map SEC(".maps");
23-
24-
struct {
25-
__uint(type, BPF_MAP_TYPE_ARRAY);
26-
__uint(max_entries, 2);
27-
__type(key, __u32);
28-
__type(value, int);
29-
} sockopt_results SEC(".maps");
30-
31-
static inline void update_event_map(int event)
32-
{
33-
__u32 key = 0;
34-
struct tcpbpf_globals g, *gp;
35-
36-
gp = bpf_map_lookup_elem(&global_map, &key);
37-
if (gp == NULL) {
38-
struct tcpbpf_globals g = {0};
39-
40-
g.event_map |= (1 << event);
41-
bpf_map_update_elem(&global_map, &key, &g,
42-
BPF_ANY);
43-
} else {
44-
g = *gp;
45-
g.event_map |= (1 << event);
46-
bpf_map_update_elem(&global_map, &key, &g,
47-
BPF_ANY);
48-
}
49-
}
50-
17+
struct tcpbpf_globals global = { 0 };
5118
int _version SEC("version") = 1;
5219

5320
SEC("sockops")
@@ -105,29 +72,15 @@ int bpf_testcb(struct bpf_sock_ops *skops)
10572

10673
op = (int) skops->op;
10774

108-
update_event_map(op);
75+
global.event_map |= (1 << op);
10976

11077
switch (op) {
11178
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
11279
/* Test failure to set largest cb flag (assumes not defined) */
113-
bad_call_rv = bpf_sock_ops_cb_flags_set(skops, 0x80);
80+
global.bad_cb_test_rv = bpf_sock_ops_cb_flags_set(skops, 0x80);
11481
/* Set callback */
115-
good_call_rv = bpf_sock_ops_cb_flags_set(skops,
82+
global.good_cb_test_rv = bpf_sock_ops_cb_flags_set(skops,
11683
BPF_SOCK_OPS_STATE_CB_FLAG);
117-
/* Update results */
118-
{
119-
__u32 key = 0;
120-
struct tcpbpf_globals g, *gp;
121-
122-
gp = bpf_map_lookup_elem(&global_map, &key);
123-
if (!gp)
124-
break;
125-
g = *gp;
126-
g.bad_cb_test_rv = bad_call_rv;
127-
g.good_cb_test_rv = good_call_rv;
128-
bpf_map_update_elem(&global_map, &key, &g,
129-
BPF_ANY);
130-
}
13184
break;
13285
case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
13386
skops->sk_txhash = 0x12345f;
@@ -143,10 +96,8 @@ int bpf_testcb(struct bpf_sock_ops *skops)
14396

14497
thdr = (struct tcphdr *)(header + offset);
14598
v = thdr->syn;
146-
__u32 key = 1;
14799

148-
bpf_map_update_elem(&sockopt_results, &key, &v,
149-
BPF_ANY);
100+
global.tcp_saved_syn = v;
150101
}
151102
}
152103
break;
@@ -156,35 +107,24 @@ int bpf_testcb(struct bpf_sock_ops *skops)
156107
break;
157108
case BPF_SOCK_OPS_STATE_CB:
158109
if (skops->args[1] == BPF_TCP_CLOSE) {
159-
__u32 key = 0;
160-
struct tcpbpf_globals g, *gp;
161-
162-
gp = bpf_map_lookup_elem(&global_map, &key);
163-
if (!gp)
164-
break;
165-
g = *gp;
166110
if (skops->args[0] == BPF_TCP_LISTEN) {
167-
g.num_listen++;
111+
global.num_listen++;
168112
} else {
169-
g.total_retrans = skops->total_retrans;
170-
g.data_segs_in = skops->data_segs_in;
171-
g.data_segs_out = skops->data_segs_out;
172-
g.bytes_received = skops->bytes_received;
173-
g.bytes_acked = skops->bytes_acked;
113+
global.total_retrans = skops->total_retrans;
114+
global.data_segs_in = skops->data_segs_in;
115+
global.data_segs_out = skops->data_segs_out;
116+
global.bytes_received = skops->bytes_received;
117+
global.bytes_acked = skops->bytes_acked;
174118
}
175-
g.num_close_events++;
176-
bpf_map_update_elem(&global_map, &key, &g,
177-
BPF_ANY);
119+
global.num_close_events++;
178120
}
179121
break;
180122
case BPF_SOCK_OPS_TCP_LISTEN_CB:
181123
bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG);
182124
v = bpf_setsockopt(skops, IPPROTO_TCP, TCP_SAVE_SYN,
183125
&save_syn, sizeof(save_syn));
184126
/* Update global map w/ result of setsock opt */
185-
__u32 key = 0;
186-
187-
bpf_map_update_elem(&sockopt_results, &key, &v, BPF_ANY);
127+
global.tcp_save_syn = v;
188128
break;
189129
default:
190130
rv = -1;

tools/testing/selftests/bpf/test_tcpbpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ struct tcpbpf_globals {
1414
__u64 bytes_acked;
1515
__u32 num_listen;
1616
__u32 num_close_events;
17+
__u32 tcp_save_syn;
18+
__u32 tcp_saved_syn;
1719
};
1820
#endif

0 commit comments

Comments
 (0)