Re: [PATCH bpf-next 2/2] selftests/bpf: add a test case for sock_ops perf-event notification

2018-11-07 Thread Daniel Borkmann
On 11/06/2018 09:28 PM, Sowmini Varadhan wrote:
> This patch provides a tcp_bpf based eBPF sample. The test
> - ncat(1) as the TCP client program to connect() to a port
>   with the intention of triggerring SYN retransmissions: we
>   first install an iptables DROP rule to make sure ncat SYNs are
>   resent (instead of aborting instantly after a TCP RST)
> - has a bpf kernel module that sends a perf-event notification for
>   each TCP retransmit, and also tracks the number of such notifications
>   sent in the global_map
> The test passes when the number of event notifications intercepted
> in user-space matches the value in the global_map.
> 
> Signed-off-by: Sowmini Varadhan 
> ---
>  tools/testing/selftests/bpf/Makefile  |4 +-
>  tools/testing/selftests/bpf/perf-sys.h|   74 
>  tools/testing/selftests/bpf/test_tcpnotify.h  |   19 ++
>  tools/testing/selftests/bpf/test_tcpnotify_kern.c |   95 +++
>  tools/testing/selftests/bpf/test_tcpnotify_user.c |  186 
> +
>  5 files changed, 377 insertions(+), 1 deletions(-)
>  create mode 100644 tools/testing/selftests/bpf/perf-sys.h
>  create mode 100644 tools/testing/selftests/bpf/test_tcpnotify.h
>  create mode 100644 tools/testing/selftests/bpf/test_tcpnotify_kern.c
>  create mode 100644 tools/testing/selftests/bpf/test_tcpnotify_user.c
> 
> diff --git a/tools/testing/selftests/bpf/Makefile 
> b/tools/testing/selftests/bpf/Makefile
> index e39dfb4..6c94048 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
> @@ -24,12 +24,13 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps 
> test_lru_map test_lpm_map test
>   test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
>   test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user 
> \
>   test_socket_cookie test_cgroup_storage test_select_reuseport 
> test_section_names \
> - test_netcnt
> + test_netcnt test_tcpnotify_user
>  
>  TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o 
> test_obj_id.o \
>   test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o 
> sockmap_parse_prog.o \
>   sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
>   test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
> + test_tcpnotify_kern.o \
>   sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
>   sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o 
> test_adjust_tail.o \
>   test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o 
> \
> @@ -74,6 +75,7 @@ $(OUTPUT)/test_sock_addr: cgroup_helpers.c
>  $(OUTPUT)/test_socket_cookie: cgroup_helpers.c
>  $(OUTPUT)/test_sockmap: cgroup_helpers.c
>  $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
> +$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
>  $(OUTPUT)/test_progs: trace_helpers.c
>  $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
>  $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
> diff --git a/tools/testing/selftests/bpf/perf-sys.h 
> b/tools/testing/selftests/bpf/perf-sys.h
> new file mode 100644
> index 000..3eb7a39
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/perf-sys.h
> @@ -0,0 +1,74 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +#ifndef _PERF_SYS_H
> +#define _PERF_SYS_H
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#ifdef __powerpc__
> +#define CPUINFO_PROC {"cpu"}
> +#endif
> +
> +#ifdef __s390__
> +#define CPUINFO_PROC {"vendor_id"}
> +#endif
> +
> +#ifdef __sh__
> +#define CPUINFO_PROC {"cpu type"}
> +#endif
> +
> +#ifdef __hppa__
> +#define CPUINFO_PROC {"cpu"}
> +#endif
> +
> +#ifdef __sparc__
> +#define CPUINFO_PROC {"cpu"}
> +#endif
> +
> +#ifdef __alpha__
> +#define CPUINFO_PROC {"cpu model"}
> +#endif
> +
> +#ifdef __arm__
> +#define CPUINFO_PROC {"model name", "Processor"}
> +#endif
> +
> +#ifdef __mips__
> +#define CPUINFO_PROC {"cpu model"}
> +#endif
> +
> +#ifdef __arc__
> +#define CPUINFO_PROC {"Processor"}
> +#endif
> +
> +#ifdef __xtensa__
> +#define CPUINFO_PROC {"core ID"}
> +#endif
> +
> +#ifndef CPUINFO_PROC
> +#define CPUINFO_PROC { "model name", }
> +#endif
> +
> +static inline int
> +sys_perf_event_open(struct perf_event_attr *attr,
> +   pid_t pid, int cpu, int group_fd,
> +   unsigned long flags)
> +{
> + int fd;
> +
> + fd = syscall(__NR_perf_event_open, attr, pid, cpu,
> +  group_fd, flags);
> +
> +#ifdef HAVE_ATTR_TEST
> + if (unlikely(test_attr__enabled))
> + test_attr__open(attr, pid, cpu, fd, group_fd, flags);
> +#endif
> + return fd;
> +}

I would prefer if we could avoid adding whole perf-sys duplicate right
into BPF kselftest directory. Agree it would be nice to have the mini
wrapper somewhere, but then lets make that a separate commit and place
the wrapper-only somewhere as tools/include/linux/perf.h that all 

[PATCH bpf-next 2/2] selftests/bpf: add a test case for sock_ops perf-event notification

2018-11-06 Thread Sowmini Varadhan
This patch provides a tcp_bpf based eBPF sample. The test
- ncat(1) as the TCP client program to connect() to a port
  with the intention of triggerring SYN retransmissions: we
  first install an iptables DROP rule to make sure ncat SYNs are
  resent (instead of aborting instantly after a TCP RST)
- has a bpf kernel module that sends a perf-event notification for
  each TCP retransmit, and also tracks the number of such notifications
  sent in the global_map
The test passes when the number of event notifications intercepted
in user-space matches the value in the global_map.

Signed-off-by: Sowmini Varadhan 
---
 tools/testing/selftests/bpf/Makefile  |4 +-
 tools/testing/selftests/bpf/perf-sys.h|   74 
 tools/testing/selftests/bpf/test_tcpnotify.h  |   19 ++
 tools/testing/selftests/bpf/test_tcpnotify_kern.c |   95 +++
 tools/testing/selftests/bpf/test_tcpnotify_user.c |  186 +
 5 files changed, 377 insertions(+), 1 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/perf-sys.h
 create mode 100644 tools/testing/selftests/bpf/test_tcpnotify.h
 create mode 100644 tools/testing/selftests/bpf/test_tcpnotify_kern.c
 create mode 100644 tools/testing/selftests/bpf/test_tcpnotify_user.c

diff --git a/tools/testing/selftests/bpf/Makefile 
b/tools/testing/selftests/bpf/Makefile
index e39dfb4..6c94048 100644
--- a/tools/testing/selftests/bpf/Makefile
+++ b/tools/testing/selftests/bpf/Makefile
@@ -24,12 +24,13 @@ TEST_GEN_PROGS = test_verifier test_tag test_maps 
test_lru_map test_lpm_map test
test_align test_verifier_log test_dev_cgroup test_tcpbpf_user \
test_sock test_btf test_sockmap test_lirc_mode2_user get_cgroup_id_user 
\
test_socket_cookie test_cgroup_storage test_select_reuseport 
test_section_names \
-   test_netcnt
+   test_netcnt test_tcpnotify_user
 
 TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o 
test_obj_id.o \
test_pkt_md_access.o test_xdp_redirect.o test_xdp_meta.o 
sockmap_parse_prog.o \
sockmap_verdict_prog.o dev_cgroup.o sample_ret0.o test_tracepoint.o \
test_l4lb_noinline.o test_xdp_noinline.o test_stacktrace_map.o \
+   test_tcpnotify_kern.o \
sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \
sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o 
test_adjust_tail.o \
test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o 
\
@@ -74,6 +75,7 @@ $(OUTPUT)/test_sock_addr: cgroup_helpers.c
 $(OUTPUT)/test_socket_cookie: cgroup_helpers.c
 $(OUTPUT)/test_sockmap: cgroup_helpers.c
 $(OUTPUT)/test_tcpbpf_user: cgroup_helpers.c
+$(OUTPUT)/test_tcpnotify_user: cgroup_helpers.c trace_helpers.c
 $(OUTPUT)/test_progs: trace_helpers.c
 $(OUTPUT)/get_cgroup_id_user: cgroup_helpers.c
 $(OUTPUT)/test_cgroup_storage: cgroup_helpers.c
diff --git a/tools/testing/selftests/bpf/perf-sys.h 
b/tools/testing/selftests/bpf/perf-sys.h
new file mode 100644
index 000..3eb7a39
--- /dev/null
+++ b/tools/testing/selftests/bpf/perf-sys.h
@@ -0,0 +1,74 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _PERF_SYS_H
+#define _PERF_SYS_H
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef __powerpc__
+#define CPUINFO_PROC   {"cpu"}
+#endif
+
+#ifdef __s390__
+#define CPUINFO_PROC   {"vendor_id"}
+#endif
+
+#ifdef __sh__
+#define CPUINFO_PROC   {"cpu type"}
+#endif
+
+#ifdef __hppa__
+#define CPUINFO_PROC   {"cpu"}
+#endif
+
+#ifdef __sparc__
+#define CPUINFO_PROC   {"cpu"}
+#endif
+
+#ifdef __alpha__
+#define CPUINFO_PROC   {"cpu model"}
+#endif
+
+#ifdef __arm__
+#define CPUINFO_PROC   {"model name", "Processor"}
+#endif
+
+#ifdef __mips__
+#define CPUINFO_PROC   {"cpu model"}
+#endif
+
+#ifdef __arc__
+#define CPUINFO_PROC   {"Processor"}
+#endif
+
+#ifdef __xtensa__
+#define CPUINFO_PROC   {"core ID"}
+#endif
+
+#ifndef CPUINFO_PROC
+#define CPUINFO_PROC   { "model name", }
+#endif
+
+static inline int
+sys_perf_event_open(struct perf_event_attr *attr,
+ pid_t pid, int cpu, int group_fd,
+ unsigned long flags)
+{
+   int fd;
+
+   fd = syscall(__NR_perf_event_open, attr, pid, cpu,
+group_fd, flags);
+
+#ifdef HAVE_ATTR_TEST
+   if (unlikely(test_attr__enabled))
+   test_attr__open(attr, pid, cpu, fd, group_fd, flags);
+#endif
+   return fd;
+}
+
+#endif /* _PERF_SYS_H */
diff --git a/tools/testing/selftests/bpf/test_tcpnotify.h 
b/tools/testing/selftests/bpf/test_tcpnotify.h
new file mode 100644
index 000..8b6cea0
--- /dev/null
+++ b/tools/testing/selftests/bpf/test_tcpnotify.h
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#ifndef _TEST_TCPBPF_H
+#define _TEST_TCPBPF_H
+
+struct tcpnotify_globals {
+   __u32 total_retrans;
+   __u32 ncalls;
+};
+
+struct tcp_notifier {
+   __u8type;
+   __u8subtype;
+   __u8source;
+   __u8