WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / load_bytes_relative.c
blob5a2a689dbb683f50e24cb69c86d2e49386e9f36b
1 // SPDX-License-Identifier: GPL-2.0-only
3 /*
4 * Copyright 2020 Google LLC.
5 */
7 #include <test_progs.h>
8 #include <network_helpers.h>
10 void test_load_bytes_relative(void)
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;
19 __u32 map_key = 0;
20 __u32 map_value = 0;
22 cgroup_fd = test__join_cgroup("/load_bytes_relative");
23 if (CHECK_FAIL(cgroup_fd < 0))
24 return;
26 server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
27 if (CHECK_FAIL(server_fd < 0))
28 goto close_cgroup_fd;
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;
35 test_result = bpf_object__find_map_by_name(obj, "test_result");
36 if (CHECK_FAIL(!test_result))
37 goto close_bpf_object;
39 map_fd = bpf_map__fd(test_result);
40 if (map_fd < 0)
41 goto close_bpf_object;
43 prog = bpf_object__find_program_by_name(obj, "load_bytes_relative");
44 if (CHECK_FAIL(!prog))
45 goto close_bpf_object;
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;
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);
57 err = bpf_map_lookup_elem(map_fd, &map_key, &map_value);
58 if (CHECK_FAIL(err))
59 goto close_bpf_object;
61 CHECK(map_value != 1, "bpf", "bpf program returned failure");
63 close_bpf_object:
64 bpf_object__close(obj);
66 close_server_fd:
67 close(server_fd);
69 close_cgroup_fd:
70 close(cgroup_fd);