1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
4  * Copyright 2020 Google LLC.
5  */
6 
7 #include <test_progs.h>
8 #include <network_helpers.h>
9 
10 void test_load_bytes_relative(void)
11 {
12 	int server_fd, cgroup_fd, prog_fd, map_fd, client_fd;
13 	int err;
14 	struct bpf_object *obj;
15 	struct bpf_program *prog;
16 	struct bpf_map *test_result;
17 	__u32 duration = 0;
18 
19 	__u32 map_key = 0;
20 	__u32 map_value = 0;
21 
22 	cgroup_fd = test__join_cgroup("/load_bytes_relative");
23 	if (CHECK_FAIL(cgroup_fd < 0))
24 		return;
25 
26 	server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
27 	if (CHECK_FAIL(server_fd < 0))
28 		goto close_cgroup_fd;
29 
30 	err = bpf_prog_load("./load_bytes_relative.o", BPF_PROG_TYPE_CGROUP_SKB,
31 			    &obj, &prog_fd);
32 	if (CHECK_FAIL(err))
33 		goto close_server_fd;
34 
35 	test_result = bpf_object__find_map_by_name(obj, "test_result");
36 	if (CHECK_FAIL(!test_result))
37 		goto close_bpf_object;
38 
39 	map_fd = bpf_map__fd(test_result);
40 	if (map_fd < 0)
41 		goto close_bpf_object;
42 
43 	prog = bpf_object__find_program_by_name(obj, "load_bytes_relative");
44 	if (CHECK_FAIL(!prog))
45 		goto close_bpf_object;
46 
47 	err = bpf_prog_attach(prog_fd, cgroup_fd, BPF_CGROUP_INET_EGRESS,
48 			      BPF_F_ALLOW_MULTI);
49 	if (CHECK_FAIL(err))
50 		goto close_bpf_object;
51 
52 	client_fd = connect_to_fd(server_fd, 0);
53 	if (CHECK_FAIL(client_fd < 0))
54 		goto close_bpf_object;
55 	close(client_fd);
56 
57 	err = bpf_map_lookup_elem(map_fd, &map_key, &map_value);
58 	if (CHECK_FAIL(err))
59 		goto close_bpf_object;
60 
61 	CHECK(map_value != 1, "bpf", "bpf program returned failure");
62 
63 close_bpf_object:
64 	bpf_object__close(obj);
65 
66 close_server_fd:
67 	close(server_fd);
68 
69 close_cgroup_fd:
70 	close(cgroup_fd);
71 }
72