1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
3 
4 static void test_l4lb(const char *file)
5 {
6 	unsigned int nr_cpus = bpf_num_possible_cpus();
7 	struct vip key = {.protocol = 6};
8 	struct vip_meta {
9 		__u32 flags;
10 		__u32 vip_num;
11 	} value = {.vip_num = VIP_NUM};
12 	__u32 stats_key = VIP_NUM;
13 	struct vip_stats {
14 		__u64 bytes;
15 		__u64 pkts;
16 	} stats[nr_cpus];
17 	struct real_definition {
18 		union {
19 			__be32 dst;
20 			__be32 dstv6[4];
21 		};
22 		__u8 flags;
23 	} real_def = {.dst = MAGIC_VAL};
24 	__u32 ch_key = 11, real_num = 3;
25 	__u32 duration, retval, size;
26 	int err, i, prog_fd, map_fd;
27 	__u64 bytes = 0, pkts = 0;
28 	struct bpf_object *obj;
29 	char buf[128];
30 	u32 *magic = (u32 *)buf;
31 
32 	err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
33 	if (err) {
34 		error_cnt++;
35 		return;
36 	}
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 != 7/*TC_ACT_REDIRECT*/ || 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 != 7/*TC_ACT_REDIRECT*/ || 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 (bytes != MAGIC_BYTES * NUM_ITER * 2 || pkts != NUM_ITER * 2) {
76 		error_cnt++;
77 		printf("test_l4lb:FAIL:stats %lld %lld\n", bytes, pkts);
78 	}
79 out:
80 	bpf_object__close(obj);
81 }
82 
83 void test_l4lb_all(void)
84 {
85 	const char *file1 = "./test_l4lb.o";
86 	const char *file2 = "./test_l4lb_noinline.o";
87 
88 	test_l4lb(file1);
89 	test_l4lb(file2);
90 }
91