1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020 Facebook */
3 
4 #include <sys/types.h>
5 #include <bpf/bpf.h>
6 #include <bpf/libbpf.h>
7 #include "test_progs.h"
8 #include "network_helpers.h"
9 #include "test_sk_storage_trace_itself.skel.h"
10 #include "test_sk_storage_tracing.skel.h"
11 
12 #define LO_ADDR6 "::1"
13 #define TEST_COMM "test_progs"
14 
15 struct sk_stg {
16 	__u32 pid;
17 	__u32 last_notclose_state;
18 	char comm[16];
19 };
20 
21 static struct test_sk_storage_tracing *skel;
22 static __u32 duration;
23 static pid_t my_pid;
24 
check_sk_stg(int sk_fd,__u32 expected_state)25 static int check_sk_stg(int sk_fd, __u32 expected_state)
26 {
27 	struct sk_stg sk_stg;
28 	int err;
29 
30 	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_stg_map), &sk_fd,
31 				  &sk_stg);
32 	if (!ASSERT_OK(err, "map_lookup(sk_stg_map)"))
33 		return -1;
34 
35 	if (!ASSERT_EQ(sk_stg.last_notclose_state, expected_state,
36 		       "last_notclose_state"))
37 		return -1;
38 
39 	if (!ASSERT_EQ(sk_stg.pid, my_pid, "pid"))
40 		return -1;
41 
42 	if (!ASSERT_STREQ(sk_stg.comm, skel->bss->task_comm, "task_comm"))
43 		return -1;
44 
45 	return 0;
46 }
47 
do_test(void)48 static void do_test(void)
49 {
50 	int listen_fd = -1, passive_fd = -1, active_fd = -1, value = 1, err;
51 	char abyte;
52 
53 	listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
54 	if (CHECK(listen_fd == -1, "start_server",
55 		  "listen_fd:%d errno:%d\n", listen_fd, errno))
56 		return;
57 
58 	active_fd = connect_to_fd(listen_fd, 0);
59 	if (CHECK(active_fd == -1, "connect_to_fd", "active_fd:%d errno:%d\n",
60 		  active_fd, errno))
61 		goto out;
62 
63 	err = bpf_map_update_elem(bpf_map__fd(skel->maps.del_sk_stg_map),
64 				  &active_fd, &value, 0);
65 	if (!ASSERT_OK(err, "map_update(del_sk_stg_map)"))
66 		goto out;
67 
68 	passive_fd = accept(listen_fd, NULL, 0);
69 	if (CHECK(passive_fd == -1, "accept", "passive_fd:%d errno:%d\n",
70 		  passive_fd, errno))
71 		goto out;
72 
73 	shutdown(active_fd, SHUT_WR);
74 	err = read(passive_fd, &abyte, 1);
75 	if (!ASSERT_OK(err, "read(passive_fd)"))
76 		goto out;
77 
78 	shutdown(passive_fd, SHUT_WR);
79 	err = read(active_fd, &abyte, 1);
80 	if (!ASSERT_OK(err, "read(active_fd)"))
81 		goto out;
82 
83 	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.del_sk_stg_map),
84 				  &active_fd, &value);
85 	if (!ASSERT_ERR(err, "map_lookup(del_sk_stg_map)"))
86 		goto out;
87 
88 	err = check_sk_stg(listen_fd, BPF_TCP_LISTEN);
89 	if (!ASSERT_OK(err, "listen_fd sk_stg"))
90 		goto out;
91 
92 	err = check_sk_stg(active_fd, BPF_TCP_FIN_WAIT2);
93 	if (!ASSERT_OK(err, "active_fd sk_stg"))
94 		goto out;
95 
96 	err = check_sk_stg(passive_fd, BPF_TCP_LAST_ACK);
97 	ASSERT_OK(err, "passive_fd sk_stg");
98 
99 out:
100 	if (active_fd != -1)
101 		close(active_fd);
102 	if (passive_fd != -1)
103 		close(passive_fd);
104 	if (listen_fd != -1)
105 		close(listen_fd);
106 }
107 
serial_test_sk_storage_tracing(void)108 void serial_test_sk_storage_tracing(void)
109 {
110 	struct test_sk_storage_trace_itself *skel_itself;
111 	int err;
112 
113 	my_pid = getpid();
114 
115 	skel_itself = test_sk_storage_trace_itself__open_and_load();
116 
117 	if (!ASSERT_NULL(skel_itself, "test_sk_storage_trace_itself")) {
118 		test_sk_storage_trace_itself__destroy(skel_itself);
119 		return;
120 	}
121 
122 	skel = test_sk_storage_tracing__open_and_load();
123 	if (!ASSERT_OK_PTR(skel, "test_sk_storage_tracing"))
124 		return;
125 
126 	err = test_sk_storage_tracing__attach(skel);
127 	if (!ASSERT_OK(err, "test_sk_storage_tracing__attach")) {
128 		test_sk_storage_tracing__destroy(skel);
129 		return;
130 	}
131 
132 	do_test();
133 
134 	test_sk_storage_tracing__destroy(skel);
135 }
136