WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / bpf / prog_tests / probe_user.c
blob7aecfd9e87d159d1c3fc1abe3117fd3f8aa3a63b
1 // SPDX-License-Identifier: GPL-2.0
2 #include <test_progs.h>
4 void test_probe_user(void)
6 const char *prog_name = "kprobe/__sys_connect";
7 const char *obj_file = "./test_probe_user.o";
8 DECLARE_LIBBPF_OPTS(bpf_object_open_opts, opts, );
9 int err, results_map_fd, sock_fd, duration = 0;
10 struct sockaddr curr, orig, tmp;
11 struct sockaddr_in *in = (struct sockaddr_in *)&curr;
12 struct bpf_link *kprobe_link = NULL;
13 struct bpf_program *kprobe_prog;
14 struct bpf_object *obj;
15 static const int zero = 0;
17 obj = bpf_object__open_file(obj_file, &opts);
18 if (CHECK(IS_ERR(obj), "obj_open_file", "err %ld\n", PTR_ERR(obj)))
19 return;
21 kprobe_prog = bpf_object__find_program_by_title(obj, prog_name);
22 if (CHECK(!kprobe_prog, "find_probe",
23 "prog '%s' not found\n", prog_name))
24 goto cleanup;
26 err = bpf_object__load(obj);
27 if (CHECK(err, "obj_load", "err %d\n", err))
28 goto cleanup;
30 results_map_fd = bpf_find_map(__func__, obj, "test_pro.bss");
31 if (CHECK(results_map_fd < 0, "find_bss_map",
32 "err %d\n", results_map_fd))
33 goto cleanup;
35 kprobe_link = bpf_program__attach(kprobe_prog);
36 if (CHECK(IS_ERR(kprobe_link), "attach_kprobe",
37 "err %ld\n", PTR_ERR(kprobe_link))) {
38 kprobe_link = NULL;
39 goto cleanup;
42 memset(&curr, 0, sizeof(curr));
43 in->sin_family = AF_INET;
44 in->sin_port = htons(5555);
45 in->sin_addr.s_addr = inet_addr("255.255.255.255");
46 memcpy(&orig, &curr, sizeof(curr));
48 sock_fd = socket(AF_INET, SOCK_STREAM, 0);
49 if (CHECK(sock_fd < 0, "create_sock_fd", "err %d\n", sock_fd))
50 goto cleanup;
52 connect(sock_fd, &curr, sizeof(curr));
53 close(sock_fd);
55 err = bpf_map_lookup_elem(results_map_fd, &zero, &tmp);
56 if (CHECK(err, "get_kprobe_res",
57 "failed to get kprobe res: %d\n", err))
58 goto cleanup;
60 in = (struct sockaddr_in *)&tmp;
61 if (CHECK(memcmp(&tmp, &orig, sizeof(orig)), "check_kprobe_res",
62 "wrong kprobe res from probe read: %s:%u\n",
63 inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
64 goto cleanup;
66 memset(&tmp, 0xab, sizeof(tmp));
68 in = (struct sockaddr_in *)&curr;
69 if (CHECK(memcmp(&curr, &tmp, sizeof(tmp)), "check_kprobe_res",
70 "wrong kprobe res from probe write: %s:%u\n",
71 inet_ntoa(in->sin_addr), ntohs(in->sin_port)))
72 goto cleanup;
73 cleanup:
74 bpf_link__destroy(kprobe_link);
75 bpf_object__close(obj);