[PATCH bpf-next 4/5] selftests/bpf: Prepare test_sock_addr for extension

2018-05-18 Thread Andrey Ignatov
test_sock_addr was not easy to extend since it was focused on sys_bind
and sys_connect quite a bit.

Reorganized it so that it'll be easier to cover new test-cases for
`BPF_PROG_TYPE_CGROUP_SOCK_ADDR`:

- decouple test-cases so that only one BPF prog is tested at a time;

- check programmatically that local IP:port for sys_bind, source IP and
  destination IP:port for sys_connect are rewritten property by tested
  BPF programs.

The output of new version:
  # test_sock_addr.sh 2>/dev/null
  Wait for testing IPv4/IPv6 to become available ... OK
  Test case: bind4: load prog with wrong expected attach type .. [PASS]
  Test case: bind4: attach prog with wrong attach type .. [PASS]
  Test case: bind4: rewrite IP & TCP port in .. [PASS]
  Test case: bind4: rewrite IP & UDP port in .. [PASS]
  Test case: bind6: load prog with wrong expected attach type .. [PASS]
  Test case: bind6: attach prog with wrong attach type .. [PASS]
  Test case: bind6: rewrite IP & TCP port in .. [PASS]
  Test case: bind6: rewrite IP & UDP port in .. [PASS]
  Test case: connect4: load prog with wrong expected attach type .. [PASS]
  Test case: connect4: attach prog with wrong attach type .. [PASS]
  Test case: connect4: rewrite IP & TCP port .. [PASS]
  Test case: connect4: rewrite IP & UDP port .. [PASS]
  Test case: connect6: load prog with wrong expected attach type .. [PASS]
  Test case: connect6: attach prog with wrong attach type .. [PASS]
  Test case: connect6: rewrite IP & TCP port .. [PASS]
  Test case: connect6: rewrite IP & UDP port .. [PASS]
  Summary: 16 PASSED, 0 FAILED

(stderr contains errors from libbpf when testing load/attach with
invalid arguments)

Signed-off-by: Andrey Ignatov 
Acked-by: Alexei Starovoitov 
---
 tools/testing/selftests/bpf/test_sock_addr.c | 655 +++
 1 file changed, 460 insertions(+), 195 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_sock_addr.c 
b/tools/testing/selftests/bpf/test_sock_addr.c
index 2950f80..ed3e397 100644
--- a/tools/testing/selftests/bpf/test_sock_addr.c
+++ b/tools/testing/selftests/bpf/test_sock_addr.c
@@ -17,34 +17,292 @@
 #include "cgroup_helpers.h"
 #include "bpf_rlimit.h"
 
+#ifndef ARRAY_SIZE
+# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
+#endif
+
 #define CG_PATH"/foo"
 #define CONNECT4_PROG_PATH "./connect4_prog.o"
 #define CONNECT6_PROG_PATH "./connect6_prog.o"
 
 #define SERV4_IP   "192.168.1.254"
 #define SERV4_REWRITE_IP   "127.0.0.1"
+#define SRC4_REWRITE_IP"127.0.0.4"
 #define SERV4_PORT 4040
 #define SERV4_REWRITE_PORT 
 
 #define SERV6_IP   "face:b00c:1234:5678::abcd"
 #define SERV6_REWRITE_IP   "::1"
+#define SRC6_REWRITE_IP"::6"
 #define SERV6_PORT 6060
 #define SERV6_REWRITE_PORT 
 
 #define INET_NTOP_BUF  40
 
-typedef int (*load_fn)(enum bpf_attach_type, const char *comment);
+struct sock_addr_test;
+
+typedef int (*load_fn)(const struct sock_addr_test *test);
 typedef int (*info_fn)(int, struct sockaddr *, socklen_t *);
 
-struct program {
-   enum bpf_attach_type type;
-   load_fn loadfn;
-   int fd;
-   const char *name;
-   enum bpf_attach_type invalid_type;
+char bpf_log_buf[BPF_LOG_BUF_SIZE];
+
+struct sock_addr_test {
+   const char *descr;
+   /* BPF prog properties */
+   load_fn loadfn;
+   enum bpf_attach_type expected_attach_type;
+   enum bpf_attach_type attach_type;
+   /* Socket properties */
+   int domain;
+   int type;
+   /* IP:port pairs for BPF prog to override */
+   const char *requested_ip;
+   unsigned short requested_port;
+   const char *expected_ip;
+   unsigned short expected_port;
+   const char *expected_src_ip;
+   /* Expected test result */
+   enum {
+   LOAD_REJECT,
+   ATTACH_REJECT,
+   SUCCESS,
+   } expected_result;
 };
 
-char bpf_log_buf[BPF_LOG_BUF_SIZE];
+static int bind4_prog_load(const struct sock_addr_test *test);
+static int bind6_prog_load(const struct sock_addr_test *test);
+static int connect4_prog_load(const struct sock_addr_test *test);
+static int connect6_prog_load(const struct sock_addr_test *test);
+
+static struct sock_addr_test tests[] = {
+   /* bind */
+   {
+   "bind4: load prog with wrong expected attach type",
+   bind4_prog_load,
+   BPF_CGROUP_INET6_BIND,
+   BPF_CGROUP_INET4_BIND,
+   AF_INET,
+   SOCK_STREAM,
+   NULL,
+   0,
+   NULL,
+   0,
+   NULL,
+   LOAD_REJECT,
+   },
+   {
+   "bind4: attach prog with wrong attach type",
+   bind4_prog_load,
+   BPF_CGROUP_INET4_BIND,
+   BPF_CGROUP_INET6_BIND,
+   AF_INET,
+   SOCK_STREAM,
+   NULL,
+   0,
+   

Re: [PATCH bpf-next 4/5] selftests/bpf: Prepare test_sock_addr for extension

2018-05-22 Thread Martin KaFai Lau
On Fri, May 18, 2018 at 07:21:12PM -0700, Andrey Ignatov wrote:
> test_sock_addr was not easy to extend since it was focused on sys_bind
> and sys_connect quite a bit.
> 
> Reorganized it so that it'll be easier to cover new test-cases for
> `BPF_PROG_TYPE_CGROUP_SOCK_ADDR`:
> 
> - decouple test-cases so that only one BPF prog is tested at a time;
> 
> - check programmatically that local IP:port for sys_bind, source IP and
>   destination IP:port for sys_connect are rewritten property by tested
>   BPF programs.
> 
> The output of new version:
>   # test_sock_addr.sh 2>/dev/null
>   Wait for testing IPv4/IPv6 to become available ... OK
>   Test case: bind4: load prog with wrong expected attach type .. [PASS]
>   Test case: bind4: attach prog with wrong attach type .. [PASS]
>   Test case: bind4: rewrite IP & TCP port in .. [PASS]
>   Test case: bind4: rewrite IP & UDP port in .. [PASS]
>   Test case: bind6: load prog with wrong expected attach type .. [PASS]
>   Test case: bind6: attach prog with wrong attach type .. [PASS]
>   Test case: bind6: rewrite IP & TCP port in .. [PASS]
>   Test case: bind6: rewrite IP & UDP port in .. [PASS]
>   Test case: connect4: load prog with wrong expected attach type .. [PASS]
>   Test case: connect4: attach prog with wrong attach type .. [PASS]
>   Test case: connect4: rewrite IP & TCP port .. [PASS]
>   Test case: connect4: rewrite IP & UDP port .. [PASS]
>   Test case: connect6: load prog with wrong expected attach type .. [PASS]
>   Test case: connect6: attach prog with wrong attach type .. [PASS]
>   Test case: connect6: rewrite IP & TCP port .. [PASS]
>   Test case: connect6: rewrite IP & UDP port .. [PASS]
>   Summary: 16 PASSED, 0 FAILED
> 
> (stderr contains errors from libbpf when testing load/attach with
> invalid arguments)
> 
> Signed-off-by: Andrey Ignatov 
> Acked-by: Alexei Starovoitov 
Acked-by: Martin KaFai Lau