1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 #include <network_helpers.h>
4 
5 void test_xdp_noinline(void)
6 {
7 	const char *file = "./test_xdp_noinline.o";
8 	unsigned int nr_cpus = bpf_num_possible_cpus();
9 	struct vip key = {.protocol = 6};
10 	struct vip_meta {
11 		__u32 flags;
12 		__u32 vip_num;
13 	} value = {.vip_num = VIP_NUM};
14 	__u32 stats_key = VIP_NUM;
15 	struct vip_stats {
16 		__u64 bytes;
17 		__u64 pkts;
18 	} stats[nr_cpus];
19 	struct real_definition {
20 		union {
21 			__be32 dst;
22 			__be32 dstv6[4];
23 		};
24 		__u8 flags;
25 	} real_def = {.dst = MAGIC_VAL};
26 	__u32 ch_key = 11, real_num = 3;
27 	__u32 duration, retval, size;
28 	int err, i, prog_fd, map_fd;
29 	__u64 bytes = 0, pkts = 0;
30 	struct bpf_object *obj;
31 	char buf[128];
32 	u32 *magic = (u32 *)buf;
33 
34 	err = bpf_prog_load(file, BPF_PROG_TYPE_XDP, &obj, &prog_fd);
35 	if (CHECK_FAIL(err))
36 		return;
37 
38 	map_fd = bpf_find_map(__func__, obj, "vip_map");
39 	if (map_fd < 0)
40 		goto out;
41 	bpf_map_update_elem(map_fd, &key, &value, 0);
42 
43 	map_fd = bpf_find_map(__func__, obj, "ch_rings");
44 	if (map_fd < 0)
45 		goto out;
46 	bpf_map_update_elem(map_fd, &ch_key, &real_num, 0);
47 
48 	map_fd = bpf_find_map(__func__, obj, "reals");
49 	if (map_fd < 0)
50 		goto out;
51 	bpf_map_update_elem(map_fd, &real_num, &real_def, 0);
52 
53 	err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4),
54 				buf, &size, &retval, &duration);
55 	CHECK(err || retval != 1 || size != 54 ||
56 	      *magic != MAGIC_VAL, "ipv4",
57 	      "err %d errno %d retval %d size %d magic %x\n",
58 	      err, errno, retval, size, *magic);
59 
60 	err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6),
61 				buf, &size, &retval, &duration);
62 	CHECK(err || retval != 1 || size != 74 ||
63 	      *magic != MAGIC_VAL, "ipv6",
64 	      "err %d errno %d retval %d size %d magic %x\n",
65 	      err, errno, retval, size, *magic);
66 
67 	map_fd = bpf_find_map(__func__, obj, "stats");
68 	if (map_fd < 0)
69 		goto out;
70 	bpf_map_lookup_elem(map_fd, &stats_key, stats);
71 	for (i = 0; i < nr_cpus; i++) {
72 		bytes += stats[i].bytes;
73 		pkts += stats[i].pkts;
74 	}
75 	if (CHECK_FAIL(bytes != MAGIC_BYTES * NUM_ITER * 2 ||
76 		       pkts != NUM_ITER * 2)) {
77 		printf("test_xdp_noinline:FAIL:stats %lld %lld\n",
78 		       bytes, pkts);
79 	}
80 out:
81 	bpf_object__close(obj);
82 }
83