2018-11-08 03:12:02 +03:00
// SPDX-License-Identifier: GPL-2.0
# define _GNU_SOURCE
# include <pthread.h>
# include <inttypes.h>
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <asm/types.h>
# include <sys/syscall.h>
# include <errno.h>
# include <string.h>
# include <linux/bpf.h>
# include <sys/socket.h>
# include <bpf/bpf.h>
# include <bpf/libbpf.h>
# include <sys/ioctl.h>
# include <linux/rtnetlink.h>
# include <signal.h>
# include <linux/perf_event.h>
2019-07-24 00:34:42 +03:00
# include <linux/err.h>
2018-11-08 03:12:02 +03:00
# include "bpf_util.h"
# include "cgroup_helpers.h"
# include "test_tcpnotify.h"
# include "trace_helpers.h"
2021-11-04 01:08:44 +03:00
# include "testing_helpers.h"
2018-11-08 03:12:02 +03:00
# define SOCKET_BUFFER_SIZE (getpagesize() < 8192L ? getpagesize() : 8192L)
pthread_t tid ;
int rx_callbacks ;
2019-07-24 00:34:42 +03:00
static void dummyfn ( void * ctx , int cpu , void * data , __u32 size )
2018-11-08 03:12:02 +03:00
{
struct tcp_notifier * t = data ;
if ( t - > type ! = 0xde | | t - > subtype ! = 0xad | |
t - > source ! = 0xbe | | t - > hash ! = 0xef )
2019-07-24 00:34:42 +03:00
return ;
2018-11-08 03:12:02 +03:00
rx_callbacks + + ;
}
2019-07-24 00:34:42 +03:00
void tcp_notifier_poller ( struct perf_buffer * pb )
2018-11-08 03:12:02 +03:00
{
2019-07-24 00:34:42 +03:00
int err ;
while ( 1 ) {
err = perf_buffer__poll ( pb , 100 ) ;
if ( err < 0 & & err ! = - EINTR ) {
printf ( " failed perf_buffer__poll: %d \n " , err ) ;
return ;
}
}
2018-11-08 03:12:02 +03:00
}
static void * poller_thread ( void * arg )
{
2019-07-24 00:34:42 +03:00
struct perf_buffer * pb = arg ;
2018-11-08 03:12:02 +03:00
2019-07-24 00:34:42 +03:00
tcp_notifier_poller ( pb ) ;
2018-11-08 03:12:02 +03:00
return arg ;
}
int verify_result ( const struct tcpnotify_globals * result )
{
return ( result - > ncalls > 0 & & result - > ncalls = = rx_callbacks ? 0 : 1 ) ;
}
int main ( int argc , char * * argv )
{
2022-09-02 01:22:53 +03:00
const char * file = " test_tcpnotify_kern.bpf.o " ;
2019-07-24 00:34:42 +03:00
struct bpf_map * perf_map , * global_map ;
2018-11-08 03:12:02 +03:00
struct tcpnotify_globals g = { 0 } ;
2019-07-24 00:34:42 +03:00
struct perf_buffer * pb = NULL ;
2018-11-08 03:12:02 +03:00
const char * cg_path = " /foo " ;
2019-07-24 00:34:42 +03:00
int prog_fd , rv , cg_fd = - 1 ;
2018-11-08 03:12:02 +03:00
int error = EXIT_FAILURE ;
struct bpf_object * obj ;
char test_script [ 80 ] ;
cpu_set_t cpuset ;
2019-07-24 00:34:42 +03:00
__u32 key = 0 ;
2018-11-08 03:12:02 +03:00
2021-05-25 06:59:32 +03:00
libbpf_set_strict_mode ( LIBBPF_STRICT_ALL ) ;
2018-11-08 03:12:02 +03:00
CPU_ZERO ( & cpuset ) ;
CPU_SET ( 0 , & cpuset ) ;
pthread_setaffinity_np ( pthread_self ( ) , sizeof ( cpu_set_t ) , & cpuset ) ;
2020-08-01 01:09:14 +03:00
cg_fd = cgroup_setup_and_join ( cg_path ) ;
2019-01-07 20:46:46 +03:00
if ( cg_fd < 0 )
2018-11-08 03:12:02 +03:00
goto err ;
2021-11-04 01:08:44 +03:00
if ( bpf_prog_test_load ( file , BPF_PROG_TYPE_SOCK_OPS , & obj , & prog_fd ) ) {
2018-11-08 03:12:02 +03:00
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 ;
}
2019-07-24 00:34:42 +03:00
perf_map = bpf_object__find_map_by_name ( obj , " perf_event_map " ) ;
if ( ! perf_map ) {
printf ( " FAIL:map '%s' not found \n " , " perf_event_map " ) ;
2018-11-08 03:12:02 +03:00
goto err ;
2019-07-24 00:34:42 +03:00
}
2018-11-08 03:12:02 +03:00
2019-07-24 00:34:42 +03:00
global_map = bpf_object__find_map_by_name ( obj , " global_map " ) ;
if ( ! global_map ) {
printf ( " FAIL:map '%s' not found \n " , " global_map " ) ;
return - 1 ;
}
2018-11-08 03:12:02 +03:00
2021-11-11 08:36:21 +03:00
pb = perf_buffer__new ( bpf_map__fd ( perf_map ) , 8 , dummyfn , NULL , NULL , NULL ) ;
2021-05-25 06:59:32 +03:00
if ( ! pb )
2018-11-08 03:12:02 +03:00
goto err ;
2019-07-24 00:34:42 +03:00
pthread_create ( & tid , NULL , poller_thread , pb ) ;
2018-11-08 03:12:02 +03:00
sprintf ( test_script ,
2019-01-17 22:56:12 +03:00
" iptables -A INPUT -p tcp --dport %d -j DROP " ,
2018-11-08 03:12:02 +03:00
TESTPORT ) ;
bpf: Fix compilation warning of selftests
Clang compiler version: 12.0.0
The following warning appears during the selftests/bpf compilation:
prog_tests/send_signal.c:51:3: warning: ignoring return value of ‘write’,
declared with attribute warn_unused_result [-Wunused-result]
51 | write(pipe_c2p[1], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
prog_tests/send_signal.c:54:3: warning: ignoring return value of ‘read’,
declared with attribute warn_unused_result [-Wunused-result]
54 | read(pipe_p2c[0], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
......
prog_tests/stacktrace_build_id_nmi.c:13:2: warning: ignoring return value
of ‘fscanf’,declared with attribute warn_unused_result [-Wunused-resul]
13 | fscanf(f, "%llu", &sample_freq);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:133:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
133 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:138:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
138 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:143:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
143 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
Add code that fix compilation warning about ignoring return value and
handles any errors; Check return value of library`s API make the code
more secure.
Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200806104224.95306-1-Jianlin.Lv@arm.com
2020-08-06 13:42:24 +03:00
if ( system ( test_script ) ) {
printf ( " FAILED: execute command: %s, err %d \n " , test_script , - errno ) ;
goto err ;
}
2018-11-08 03:12:02 +03:00
sprintf ( test_script ,
2019-01-17 22:56:12 +03:00
" nc 127.0.0.1 %d < /etc/passwd > /dev/null 2>&1 " ,
2018-11-08 03:12:02 +03:00
TESTPORT ) ;
bpf: Fix compilation warning of selftests
Clang compiler version: 12.0.0
The following warning appears during the selftests/bpf compilation:
prog_tests/send_signal.c:51:3: warning: ignoring return value of ‘write’,
declared with attribute warn_unused_result [-Wunused-result]
51 | write(pipe_c2p[1], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
prog_tests/send_signal.c:54:3: warning: ignoring return value of ‘read’,
declared with attribute warn_unused_result [-Wunused-result]
54 | read(pipe_p2c[0], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
......
prog_tests/stacktrace_build_id_nmi.c:13:2: warning: ignoring return value
of ‘fscanf’,declared with attribute warn_unused_result [-Wunused-resul]
13 | fscanf(f, "%llu", &sample_freq);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:133:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
133 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:138:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
138 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:143:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
143 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
Add code that fix compilation warning about ignoring return value and
handles any errors; Check return value of library`s API make the code
more secure.
Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200806104224.95306-1-Jianlin.Lv@arm.com
2020-08-06 13:42:24 +03:00
if ( system ( test_script ) )
printf ( " execute command: %s, err %d \n " , test_script , - errno ) ;
2018-11-08 03:12:02 +03:00
sprintf ( test_script ,
2019-01-17 22:56:12 +03:00
" iptables -D INPUT -p tcp --dport %d -j DROP " ,
2018-11-08 03:12:02 +03:00
TESTPORT ) ;
bpf: Fix compilation warning of selftests
Clang compiler version: 12.0.0
The following warning appears during the selftests/bpf compilation:
prog_tests/send_signal.c:51:3: warning: ignoring return value of ‘write’,
declared with attribute warn_unused_result [-Wunused-result]
51 | write(pipe_c2p[1], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~
prog_tests/send_signal.c:54:3: warning: ignoring return value of ‘read’,
declared with attribute warn_unused_result [-Wunused-result]
54 | read(pipe_p2c[0], buf, 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~
......
prog_tests/stacktrace_build_id_nmi.c:13:2: warning: ignoring return value
of ‘fscanf’,declared with attribute warn_unused_result [-Wunused-resul]
13 | fscanf(f, "%llu", &sample_freq);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:133:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
133 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:138:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
138 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
test_tcpnotify_user.c:143:2: warning:ignoring return value of ‘system’,
declared with attribute warn_unused_result [-Wunused-result]
143 | system(test_script);
| ^~~~~~~~~~~~~~~~~~~
Add code that fix compilation warning about ignoring return value and
handles any errors; Check return value of library`s API make the code
more secure.
Signed-off-by: Jianlin Lv <Jianlin.Lv@arm.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200806104224.95306-1-Jianlin.Lv@arm.com
2020-08-06 13:42:24 +03:00
if ( system ( test_script ) ) {
printf ( " FAILED: execute command: %s, err %d \n " , test_script , - errno ) ;
goto err ;
}
2018-11-08 03:12:02 +03:00
2019-07-24 00:34:42 +03:00
rv = bpf_map_lookup_elem ( bpf_map__fd ( global_map ) , & key , & g ) ;
2018-11-08 03:12:02 +03:00
if ( rv ! = 0 ) {
printf ( " FAILED: bpf_map_lookup_elem returns %d \n " , rv ) ;
goto err ;
}
sleep ( 10 ) ;
if ( verify_result ( & g ) ) {
printf ( " FAILED: Wrong stats Expected %d calls, got %d \n " ,
g . ncalls , rx_callbacks ) ;
goto err ;
}
printf ( " PASSED! \n " ) ;
error = 0 ;
err :
bpf_prog_detach ( cg_fd , BPF_CGROUP_SOCK_OPS ) ;
close ( cg_fd ) ;
cleanup_cgroup_environment ( ) ;
2021-05-25 06:59:32 +03:00
perf_buffer__free ( pb ) ;
2018-11-08 03:12:02 +03:00
return error ;
}