test_tcpbpf_user.c 3.9 KB
Newer Older
L
Lawrence Brakmo 已提交
1
// SPDX-License-Identifier: GPL-2.0
2
#include <inttypes.h>
L
Lawrence Brakmo 已提交
3 4 5 6 7 8 9 10 11
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <linux/bpf.h>
#include <sys/types.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
12

13
#include "bpf_rlimit.h"
14 15 16
#include "bpf_util.h"
#include "cgroup_helpers.h"

L
Lawrence Brakmo 已提交
17 18
#include "test_tcpbpf.h"

19 20 21
/* 3 comes from one listening socket + both ends of the connection */
#define EXPECTED_CLOSE_EVENTS		3

22 23 24 25 26 27 28
#define EXPECT_EQ(expected, actual, fmt)			\
	do {							\
		if ((expected) != (actual)) {			\
			printf("  Value of: " #actual "\n"	\
			       "    Actual: %" fmt "\n"		\
			       "  Expected: %" fmt "\n",	\
			       (actual), (expected));		\
29
			ret--;					\
30 31 32 33 34 35
		}						\
	} while (0)

int verify_result(const struct tcpbpf_globals *result)
{
	__u32 expected_events;
36
	int ret = 0;
37 38 39 40 41 42 43

	expected_events = ((1 << BPF_SOCK_OPS_TIMEOUT_INIT) |
			   (1 << BPF_SOCK_OPS_RWND_INIT) |
			   (1 << BPF_SOCK_OPS_TCP_CONNECT_CB) |
			   (1 << BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB) |
			   (1 << BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB) |
			   (1 << BPF_SOCK_OPS_NEEDS_ECN) |
44 45
			   (1 << BPF_SOCK_OPS_STATE_CB) |
			   (1 << BPF_SOCK_OPS_TCP_LISTEN_CB));
46 47 48 49 50 51 52 53

	EXPECT_EQ(expected_events, result->event_map, "#" PRIx32);
	EXPECT_EQ(501ULL, result->bytes_received, "llu");
	EXPECT_EQ(1002ULL, result->bytes_acked, "llu");
	EXPECT_EQ(1, result->data_segs_in, PRIu32);
	EXPECT_EQ(1, result->data_segs_out, PRIu32);
	EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32);
	EXPECT_EQ(0, result->good_cb_test_rv, PRIu32);
54
	EXPECT_EQ(1, result->num_listen, PRIu32);
55
	EXPECT_EQ(EXPECTED_CLOSE_EVENTS, result->num_close_events, PRIu32);
56

57
	return ret;
58 59
}

60 61 62
int verify_sockopt_result(int sock_map_fd)
{
	__u32 key = 0;
63
	int ret = 0;
64 65 66 67 68 69 70 71 72 73 74 75
	int res;
	int rv;

	/* check setsockopt for SAVE_SYN */
	rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
	EXPECT_EQ(0, rv, "d");
	EXPECT_EQ(0, res, "d");
	key = 1;
	/* check getsockopt for SAVED_SYN */
	rv = bpf_map_lookup_elem(sock_map_fd, &key, &res);
	EXPECT_EQ(0, rv, "d");
	EXPECT_EQ(1, res, "d");
76
	return ret;
77 78
}

L
Lawrence Brakmo 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static int bpf_find_map(const char *test, struct bpf_object *obj,
			const char *name)
{
	struct bpf_map *map;

	map = bpf_object__find_map_by_name(obj, name);
	if (!map) {
		printf("%s:FAIL:map '%s' not found\n", test, name);
		return -1;
	}
	return bpf_map__fd(map);
}

int main(int argc, char **argv)
{
	const char *file = "test_tcpbpf_kern.o";
95
	int prog_fd, map_fd, sock_map_fd;
L
Lawrence Brakmo 已提交
96
	struct tcpbpf_globals g = {0};
97
	const char *cg_path = "/foo";
L
Lawrence Brakmo 已提交
98 99
	int error = EXIT_FAILURE;
	struct bpf_object *obj;
100
	int cg_fd = -1;
101
	int retry = 10;
L
Lawrence Brakmo 已提交
102 103 104
	__u32 key = 0;
	int rv;

105
	cg_fd = cgroup_setup_and_join(cg_path);
106
	if (cg_fd < 0)
107
		goto err;
L
Lawrence Brakmo 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120

	if (bpf_prog_load(file, BPF_PROG_TYPE_SOCK_OPS, &obj, &prog_fd)) {
		printf("FAILED: load_bpf_file failed for: %s\n", file);
		goto err;
	}

	rv = bpf_prog_attach(prog_fd, cg_fd, BPF_CGROUP_SOCK_OPS, 0);
	if (rv) {
		printf("FAILED: bpf_prog_attach: %d (%s)\n",
		       error, strerror(errno));
		goto err;
	}

121 122 123 124
	if (system("./tcp_server.py")) {
		printf("FAILED: TCP server\n");
		goto err;
	}
L
Lawrence Brakmo 已提交
125 126 127 128 129

	map_fd = bpf_find_map(__func__, obj, "global_map");
	if (map_fd < 0)
		goto err;

130 131 132 133
	sock_map_fd = bpf_find_map(__func__, obj, "sockopt_results");
	if (sock_map_fd < 0)
		goto err;

134
retry_lookup:
L
Lawrence Brakmo 已提交
135 136 137 138 139 140
	rv = bpf_map_lookup_elem(map_fd, &key, &g);
	if (rv != 0) {
		printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
		goto err;
	}

141 142 143 144 145 146 147
	if (g.num_close_events != EXPECTED_CLOSE_EVENTS && retry--) {
		printf("Unexpected number of close events (%d), retrying!\n",
		       g.num_close_events);
		usleep(100);
		goto retry_lookup;
	}

148
	if (verify_result(&g)) {
L
Lawrence Brakmo 已提交
149 150 151
		printf("FAILED: Wrong stats\n");
		goto err;
	}
152

153 154 155 156 157
	if (verify_sockopt_result(sock_map_fd)) {
		printf("FAILED: Wrong sockopt stats\n");
		goto err;
	}

L
Lawrence Brakmo 已提交
158 159 160 161
	printf("PASSED!\n");
	error = 0;
err:
	bpf_prog_detach(cg_fd, BPF_CGROUP_SOCK_OPS);
162 163
	close(cg_fd);
	cleanup_cgroup_environment();
L
Lawrence Brakmo 已提交
164 165
	return error;
}