Skip to content

Commit b0b04fc

Browse files
4astborkmann
authored andcommitted
selftests/bpf: add xdp noinline test
add large semi-artificial XDP test with 18 functions to stress test bpf call verification logic Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 3bc35c6 commit b0b04fc

File tree

3 files changed

+916
-1
lines changed

3 files changed

+916
-1
lines changed

tools/testing/selftests/bpf/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps test_lru_map test_lpm_map test
1818
TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test_obj_id.o \
1919
test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o sockmap_parse_prog.o \
2020
sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
21-
test_l4lb_noinline.o
21+
test_l4lb_noinline.o test_xdp_noinline.o
2222

2323
TEST_PROGS := test_kmod.sh test_xdp_redirect.sh test_xdp_meta.sh \
2424
test_offload.py
@@ -54,6 +54,7 @@ CLANG_FLAGS = -I. -I./include/uapi -I../../../include/uapi \
5454
-Wno-compare-distinct-pointer-types
5555

5656
$(OUTPUT)/test_l4lb_noinline.o: CLANG_FLAGS += -fno-inline
57+
$(OUTPUT)/test_xdp_noinline.o: CLANG_FLAGS += -fno-inline
5758

5859
%.o: %.c
5960
$(CLANG) $(CLANG_FLAGS) \

tools/testing/selftests/bpf/test_progs.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,86 @@ static void test_l4lb_all(void)
257257
test_l4lb(file2);
258258
}
259259

260+
static void test_xdp_noinline(void)
261+
{
262+
const char *file = "./test_xdp_noinline.o";
263+
unsigned int nr_cpus = bpf_num_possible_cpus();
264+
struct vip key = {.protocol = 6};
265+
struct vip_meta {
266+
__u32 flags;
267+
__u32 vip_num;
268+
} value = {.vip_num = VIP_NUM};
269+
__u32 stats_key = VIP_NUM;
270+
struct vip_stats {
271+
__u64 bytes;
272+
__u64 pkts;
273+
} stats[nr_cpus];
274+
struct real_definition {
275+
union {
276+
__be32 dst;
277+
__be32 dstv6[4];
278+
};
279+
__u8 flags;
280+
} real_def = {.dst = MAGIC_VAL};
281+
__u32 ch_key = 11, real_num = 3;
282+
__u32 duration, retval, size;
283+
int err, i, prog_fd, map_fd;
284+
__u64 bytes = 0, pkts = 0;
285+
struct bpf_object *obj;
286+
char buf[128];
287+
u32 *magic = (u32 *)buf;
288+
289+
err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
290+
if (err) {
291+
error_cnt++;
292+
return;
293+
}
294+
295+
map_fd = bpf_find_map(__func__, obj, "vip_map");
296+
if (map_fd < 0)
297+
goto out;
298+
bpf_map_update_elem(map_fd, &key, &value, 0);
299+
300+
map_fd = bpf_find_map(__func__, obj, "ch_rings");
301+
if (map_fd < 0)
302+
goto out;
303+
bpf_map_update_elem(map_fd, &ch_key, &real_num, 0);
304+
305+
map_fd = bpf_find_map(__func__, obj, "reals");
306+
if (map_fd < 0)
307+
goto out;
308+
bpf_map_update_elem(map_fd, &real_num, &real_def, 0);
309+
310+
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4),
311+
buf, &size, &retval, &duration);
312+
CHECK(err || errno || retval != 1 || size != 54 ||
313+
*magic != MAGIC_VAL, "ipv4",
314+
"err %d errno %d retval %d size %d magic %x\n",
315+
err, errno, retval, size, *magic);
316+
317+
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6),
318+
buf, &size, &retval, &duration);
319+
CHECK(err || errno || retval != 1 || size != 74 ||
320+
*magic != MAGIC_VAL, "ipv6",
321+
"err %d errno %d retval %d size %d magic %x\n",
322+
err, errno, retval, size, *magic);
323+
324+
map_fd = bpf_find_map(__func__, obj, "stats");
325+
if (map_fd < 0)
326+
goto out;
327+
bpf_map_lookup_elem(map_fd, &stats_key, stats);
328+
for (i = 0; i < nr_cpus; i++) {
329+
bytes += stats[i].bytes;
330+
pkts += stats[i].pkts;
331+
}
332+
if (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) {
333+
error_cnt++;
334+
printf("test_xdp_noinline:FAIL:stats %lld %lld\n", bytes, pkts);
335+
}
336+
out:
337+
bpf_object__close(obj);
338+
}
339+
260340
static void test_tcp_estats(void)
261341
{
262342
const char *file = "./test_tcp_estats.o";
@@ -766,6 +846,7 @@ int main(void)
766846
test_pkt_access();
767847
test_xdp();
768848
test_l4lb_all();
849+
test_xdp_noinline();
769850
test_tcp_estats();
770851
test_bpf_obj_id();
771852
test_pkt_md_access();

0 commit comments

Comments
 (0)