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