aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/bpf/prog_tests/sk_storage_tracing.c
blob: 547ae53cde74645398a0f3d92123c56d0e59f77f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */

#include <sys/types.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include "test_progs.h"
#include "network_helpers.h"
#include "test_sk_storage_trace_itself.skel.h"
#include "test_sk_storage_tracing.skel.h"

#define LO_ADDR6 "::1"
#define TEST_COMM "test_progs"

struct sk_stg {
	__u32 pid;
	__u32 last_notclose_state;
	char comm[16];
};

static struct test_sk_storage_tracing *skel;
static __u32 duration;
static pid_t my_pid;

static int check_sk_stg(int sk_fd, __u32 expected_state)
{
	struct sk_stg sk_stg;
	int err;

	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_stg_map), &sk_fd,
				  &sk_stg);
	if (!ASSERT_OK(err, "map_lookup(sk_stg_map)"))
		return -1;

	if (!ASSERT_EQ(sk_stg.last_notclose_state, expected_state,
		       "last_notclose_state"))
		return -1;

	if (!ASSERT_EQ(sk_stg.pid, my_pid, "pid"))
		return -1;

	if (!ASSERT_STREQ(sk_stg.comm, skel->bss->task_comm, "task_comm"))
		return -1;

	return 0;
}

static void do_test(void)
{
	int listen_fd = -1, passive_fd = -1, active_fd = -1, value = 1, err;
	char abyte;

	listen_fd = start_server(AF_INET6, SOCK_STREAM, LO_ADDR6, 0, 0);
	if (CHECK(listen_fd == -1, "start_server",
		  "listen_fd:%d errno:%d\n", listen_fd, errno))
		return;

	active_fd = connect_to_fd(listen_fd, 0);
	if (CHECK(active_fd == -1, "connect_to_fd", "active_fd:%d errno:%d\n",
		  active_fd, errno))
		goto out;

	err = bpf_map_update_elem(bpf_map__fd(skel->maps.del_sk_stg_map),
				  &active_fd, &value, 0);
	if (!ASSERT_OK(err, "map_update(del_sk_stg_map)"))
		goto out;

	passive_fd = accept(listen_fd, NULL, 0);
	if (CHECK(passive_fd == -1, "accept", "passive_fd:%d errno:%d\n",
		  passive_fd, errno))
		goto out;

	shutdown(active_fd, SHUT_WR);
	err = read(passive_fd, &abyte, 1);
	if (!ASSERT_OK(err, "read(passive_fd)"))
		goto out;

	shutdown(passive_fd, SHUT_WR);
	err = read(active_fd, &abyte, 1);
	if (!ASSERT_OK(err, "read(active_fd)"))
		goto out;

	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.del_sk_stg_map),
				  &active_fd, &value);
	if (!ASSERT_ERR(err, "map_lookup(del_sk_stg_map)"))
		goto out;

	err = check_sk_stg(listen_fd, BPF_TCP_LISTEN);
	if (!ASSERT_OK(err, "listen_fd sk_stg"))
		goto out;

	err = check_sk_stg(active_fd, BPF_TCP_FIN_WAIT2);
	if (!ASSERT_OK(err, "active_fd sk_stg"))
		goto out;

	err = check_sk_stg(passive_fd, BPF_TCP_LAST_ACK);
	ASSERT_OK(err, "passive_fd sk_stg");

out:
	if (active_fd != -1)
		close(active_fd);
	if (passive_fd != -1)
		close(passive_fd);
	if (listen_fd != -1)
		close(listen_fd);
}

void serial_test_sk_storage_tracing(void)
{
	struct test_sk_storage_trace_itself *skel_itself;
	int err;

	my_pid = getpid();

	skel_itself = test_sk_storage_trace_itself__open_and_load();

	if (!ASSERT_NULL(skel_itself, "test_sk_storage_trace_itself")) {
		test_sk_storage_trace_itself__destroy(skel_itself);
		return;
	}

	skel = test_sk_storage_tracing__open_and_load();
	if (!ASSERT_OK_PTR(skel, "test_sk_storage_tracing"))
		return;

	err = test_sk_storage_tracing__attach(skel);
	if (!ASSERT_OK(err, "test_sk_storage_tracing__attach")) {
		test_sk_storage_tracing__destroy(skel);
		return;
	}

	do_test();

	test_sk_storage_tracing__destroy(skel);
}