Commit 247f0ec3 authored by Alexander Duyck's avatar Alexander Duyck Committed by Alexei Starovoitov

selftests/bpf: Drop python client/server in favor of threads

Drop the tcp_client/server.py files in favor of using a client and server
thread within the test case. Specifically we spawn a new thread to play the
role of the server, and the main testing thread plays the role of client.

Add logic to the end of the run_test function to guarantee that the sockets
are closed when we begin verifying results.

Doing this we are able to reduce overhead since we don't have two python
workers possibly floating around. In addition we don't have to worry about
synchronization issues and as such the retry loop waiting for the threads
to close the sockets can be dropped as we will have already closed the
sockets in the local executable and synchronized the server thread.
Signed-off-by: default avatarAlexander Duyck <alexanderduyck@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/160443929638.1086697.2430242340980315521.stgit@localhost.localdomain
parent aaf376bd
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include <inttypes.h> #include <inttypes.h>
#include <test_progs.h> #include <test_progs.h>
#include <network_helpers.h>
#include "test_tcpbpf.h" #include "test_tcpbpf.h"
#define LO_ADDR6 "::1"
#define CG_NAME "/tcpbpf-user-test" #define CG_NAME "/tcpbpf-user-test"
/* 3 comes from one listening socket + both ends of the connection */ static __u32 duration;
#define EXPECTED_CLOSE_EVENTS 3
#define EXPECT_EQ(expected, actual, fmt) \ #define EXPECT_EQ(expected, actual, fmt) \
do { \ do { \
...@@ -42,7 +43,9 @@ int verify_result(const struct tcpbpf_globals *result) ...@@ -42,7 +43,9 @@ int verify_result(const struct tcpbpf_globals *result)
EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32); EXPECT_EQ(0x80, result->bad_cb_test_rv, PRIu32);
EXPECT_EQ(0, result->good_cb_test_rv, PRIu32); EXPECT_EQ(0, result->good_cb_test_rv, PRIu32);
EXPECT_EQ(1, result->num_listen, PRIu32); EXPECT_EQ(1, result->num_listen, PRIu32);
EXPECT_EQ(EXPECTED_CLOSE_EVENTS, result->num_close_events, PRIu32);
/* 3 comes from one listening socket + both ends of the connection */
EXPECT_EQ(3, result->num_close_events, PRIu32);
return ret; return ret;
} }
...@@ -66,6 +69,75 @@ int verify_sockopt_result(int sock_map_fd) ...@@ -66,6 +69,75 @@ int verify_sockopt_result(int sock_map_fd)
return ret; return ret;
} }
static int run_test(void)
{
int listen_fd = -1, cli_fd = -1, accept_fd = -1;
char buf[1000];
int err = -1;
int i, rv;
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))
goto done;
cli_fd = connect_to_fd(listen_fd, 0);
if (CHECK(cli_fd == -1, "connect_to_fd(listen_fd)",
"cli_fd:%d errno:%d\n", cli_fd, errno))
goto done;
accept_fd = accept(listen_fd, NULL, NULL);
if (CHECK(accept_fd == -1, "accept(listen_fd)",
"accept_fd:%d errno:%d\n", accept_fd, errno))
goto done;
/* Send 1000B of '+'s from cli_fd -> accept_fd */
for (i = 0; i < 1000; i++)
buf[i] = '+';
rv = send(cli_fd, buf, 1000, 0);
if (CHECK(rv != 1000, "send(cli_fd)", "rv:%d errno:%d\n", rv, errno))
goto done;
rv = recv(accept_fd, buf, 1000, 0);
if (CHECK(rv != 1000, "recv(accept_fd)", "rv:%d errno:%d\n", rv, errno))
goto done;
/* Send 500B of '.'s from accept_fd ->cli_fd */
for (i = 0; i < 500; i++)
buf[i] = '.';
rv = send(accept_fd, buf, 500, 0);
if (CHECK(rv != 500, "send(accept_fd)", "rv:%d errno:%d\n", rv, errno))
goto done;
rv = recv(cli_fd, buf, 500, 0);
if (CHECK(rv != 500, "recv(cli_fd)", "rv:%d errno:%d\n", rv, errno))
goto done;
/*
* shutdown accept first to guarantee correct ordering for
* bytes_received and bytes_acked when we go to verify the results.
*/
shutdown(accept_fd, SHUT_WR);
err = recv(cli_fd, buf, 1, 0);
if (CHECK(err, "recv(cli_fd) for fin", "err:%d errno:%d\n", err, errno))
goto done;
shutdown(cli_fd, SHUT_WR);
err = recv(accept_fd, buf, 1, 0);
CHECK(err, "recv(accept_fd) for fin", "err:%d errno:%d\n", err, errno);
done:
if (accept_fd != -1)
close(accept_fd);
if (cli_fd != -1)
close(cli_fd);
if (listen_fd != -1)
close(listen_fd);
return err;
}
void test_tcpbpf_user(void) void test_tcpbpf_user(void)
{ {
const char *file = "test_tcpbpf_kern.o"; const char *file = "test_tcpbpf_kern.o";
...@@ -74,7 +146,6 @@ void test_tcpbpf_user(void) ...@@ -74,7 +146,6 @@ void test_tcpbpf_user(void)
int error = EXIT_FAILURE; int error = EXIT_FAILURE;
struct bpf_object *obj; struct bpf_object *obj;
int cg_fd = -1; int cg_fd = -1;
int retry = 10;
__u32 key = 0; __u32 key = 0;
int rv; int rv;
...@@ -94,11 +165,6 @@ void test_tcpbpf_user(void) ...@@ -94,11 +165,6 @@ void test_tcpbpf_user(void)
goto err; goto err;
} }
if (system("./tcp_server.py")) {
printf("FAILED: TCP server\n");
goto err;
}
map_fd = bpf_find_map(__func__, obj, "global_map"); map_fd = bpf_find_map(__func__, obj, "global_map");
if (map_fd < 0) if (map_fd < 0)
goto err; goto err;
...@@ -107,20 +173,15 @@ void test_tcpbpf_user(void) ...@@ -107,20 +173,15 @@ void test_tcpbpf_user(void)
if (sock_map_fd < 0) if (sock_map_fd < 0)
goto err; goto err;
retry_lookup: if (run_test())
goto err;
rv = bpf_map_lookup_elem(map_fd, &key, &g); rv = bpf_map_lookup_elem(map_fd, &key, &g);
if (rv != 0) { if (rv != 0) {
printf("FAILED: bpf_map_lookup_elem returns %d\n", rv); printf("FAILED: bpf_map_lookup_elem returns %d\n", rv);
goto err; goto err;
} }
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;
}
if (verify_result(&g)) { if (verify_result(&g)) {
printf("FAILED: Wrong stats\n"); printf("FAILED: Wrong stats\n");
goto err; goto err;
......
#!/usr/bin/env python3
#
# SPDX-License-Identifier: GPL-2.0
#
import sys, os, os.path, getopt
import socket, time
import subprocess
import select
def read(sock, n):
buf = b''
while len(buf) < n:
rem = n - len(buf)
try: s = sock.recv(rem)
except (socket.error) as e: return b''
buf += s
return buf
def send(sock, s):
total = len(s)
count = 0
while count < total:
try: n = sock.send(s)
except (socket.error) as e: n = 0
if n == 0:
return count;
count += n
return count
serverPort = int(sys.argv[1])
# create active socket
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
try:
sock.connect(('::1', serverPort))
except socket.error as e:
sys.exit(1)
buf = b''
n = 0
while n < 1000:
buf += b'+'
n += 1
sock.settimeout(1);
n = send(sock, buf)
n = read(sock, 500)
sys.exit(0)
#!/usr/bin/env python3
#
# SPDX-License-Identifier: GPL-2.0
#
import sys, os, os.path, getopt
import socket, time
import subprocess
import select
def read(sock, n):
buf = b''
while len(buf) < n:
rem = n - len(buf)
try: s = sock.recv(rem)
except (socket.error) as e: return b''
buf += s
return buf
def send(sock, s):
total = len(s)
count = 0
while count < total:
try: n = sock.send(s)
except (socket.error) as e: n = 0
if n == 0:
return count;
count += n
return count
SERVER_PORT = 12877
MAX_PORTS = 2
serverPort = SERVER_PORT
serverSocket = None
# create passive socket
serverSocket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
try: serverSocket.bind(('::1', 0))
except socket.error as msg:
print('bind fails: ' + str(msg))
sn = serverSocket.getsockname()
serverPort = sn[1]
cmdStr = ("./tcp_client.py %d &") % (serverPort)
os.system(cmdStr)
buf = b''
n = 0
while n < 500:
buf += b'.'
n += 1
serverSocket.listen(MAX_PORTS)
readList = [serverSocket]
while True:
readyRead, readyWrite, inError = \
select.select(readList, [], [], 2)
if len(readyRead) > 0:
waitCount = 0
for sock in readyRead:
if sock == serverSocket:
(clientSocket, address) = serverSocket.accept()
address = str(address[0])
readList.append(clientSocket)
else:
sock.settimeout(1);
s = read(sock, 1000)
n = send(sock, buf)
sock.close()
serverSocket.close()
sys.exit(0)
else:
print('Select timeout!')
sys.exit(1)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment