xref: /linux/tools/testing/selftests/bpf/prog_tests/xdp_noinline.c (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 #include "test_xdp_noinline.skel.h"
5 
6 void test_xdp_noinline(void)
7 {
8 	unsigned int nr_cpus = bpf_num_possible_cpus();
9 	struct test_xdp_noinline *skel;
10 	struct vip key = {.protocol = 6};
11 	struct vip_meta {
12 		__u32 flags;
13 		__u32 vip_num;
14 	} value = {.vip_num = VIP_NUM};
15 	__u32 stats_key = VIP_NUM;
16 	struct vip_stats {
17 		__u64 bytes;
18 		__u64 pkts;
19 	} stats[nr_cpus];
20 	struct real_definition {
21 		union {
22 			__be32 dst;
23 			__be32 dstv6[4];
24 		};
25 		__u8 flags;
26 	} real_def = {.dst = MAGIC_VAL};
27 	__u32 ch_key = 11, real_num = 3;
28 	int err, i;
29 	__u64 bytes = 0, pkts = 0;
30 	char buf[128];
31 	u32 *magic = (u32 *)buf;
32 	LIBBPF_OPTS(bpf_test_run_opts, topts,
33 		.data_in = &pkt_v4,
34 		.data_size_in = sizeof(pkt_v4),
35 		.data_out = buf,
36 		.data_size_out = sizeof(buf),
37 		.repeat = NUM_ITER,
38 	);
39 
40 	skel = test_xdp_noinline__open_and_load();
41 	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
42 		return;
43 
44 	bpf_map_update_elem(bpf_map__fd(skel->maps.vip_map), &key, &value, 0);
45 	bpf_map_update_elem(bpf_map__fd(skel->maps.ch_rings), &ch_key, &real_num, 0);
46 	bpf_map_update_elem(bpf_map__fd(skel->maps.reals), &real_num, &real_def, 0);
47 
48 	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.balancer_ingress_v4), &topts);
49 	ASSERT_OK(err, "ipv4 test_run");
50 	ASSERT_EQ(topts.retval, 1, "ipv4 test_run retval");
51 	ASSERT_EQ(topts.data_size_out, 54, "ipv4 test_run data_size_out");
52 	ASSERT_EQ(*magic, MAGIC_VAL, "ipv4 test_run magic");
53 
54 	topts.data_in = &pkt_v6;
55 	topts.data_size_in = sizeof(pkt_v6);
56 	topts.data_out = buf;
57 	topts.data_size_out = sizeof(buf);
58 
59 	err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.balancer_ingress_v6), &topts);
60 	ASSERT_OK(err, "ipv6 test_run");
61 	ASSERT_EQ(topts.retval, 1, "ipv6 test_run retval");
62 	ASSERT_EQ(topts.data_size_out, 74, "ipv6 test_run data_size_out");
63 	ASSERT_EQ(*magic, MAGIC_VAL, "ipv6 test_run magic");
64 
65 	bpf_map_lookup_elem(bpf_map__fd(skel->maps.stats), &stats_key, stats);
66 	for (i = 0; i < nr_cpus; i++) {
67 		bytes += stats[i].bytes;
68 		pkts += stats[i].pkts;
69 	}
70 	ASSERT_EQ(bytes, MAGIC_BYTES * NUM_ITER * 2, "stats bytes");
71 	ASSERT_EQ(pkts, NUM_ITER * 2, "stats pkts");
72 	test_xdp_noinline__destroy(skel);
73 }
74