selftest/bpf: Implement sample UNIX domain socket iterator program.
authorKuniyuki Iwashima <kuniyu@amazon.co.jp>
Sat, 14 Aug 2021 01:57:17 +0000 (10:57 +0900)
committerAndrii Nakryiko <andrii@kernel.org>
Sun, 15 Aug 2021 07:19:06 +0000 (00:19 -0700)
The iterator can output almost the same result compared to /proc/net/unix.
The header line is aligned, and the Inode column uses "%8lu" because "%5lu"
can be easily overflown.

  # cat /sys/fs/bpf/unix
  Num               RefCount Protocol Flags    Type St    Inode Path
  ffff963c0668980000000002 00000000 00010000 0001 01    18697 private/defer
  ffff963c7c979c0000000002 00000000 00000000 0001 01   598245 @Hello@World@

  # cat /proc/net/unix
  Num       RefCount Protocol Flags    Type St Inode Path
  ffff963c0668980000000002 00000000 00010000 0001 01 18697 private/defer
  ffff963c7c979c0000000002 00000000 00000000 0001 01 598245 @Hello@World@

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210814015718.42704-4-kuniyu@amazon.co.jp
tools/testing/selftests/bpf/prog_tests/bpf_iter.c
tools/testing/selftests/bpf/progs/bpf_iter.h
tools/testing/selftests/bpf/progs/bpf_iter_unix.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/bpf_tracing_net.h

index 1f1aade..77ac24b 100644 (file)
@@ -13,6 +13,7 @@
 #include "bpf_iter_tcp6.skel.h"
 #include "bpf_iter_udp4.skel.h"
 #include "bpf_iter_udp6.skel.h"
+#include "bpf_iter_unix.skel.h"
 #include "bpf_iter_test_kern1.skel.h"
 #include "bpf_iter_test_kern2.skel.h"
 #include "bpf_iter_test_kern3.skel.h"
@@ -313,6 +314,19 @@ static void test_udp6(void)
        bpf_iter_udp6__destroy(skel);
 }
 
+static void test_unix(void)
+{
+       struct bpf_iter_unix *skel;
+
+       skel = bpf_iter_unix__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "bpf_iter_unix__open_and_load"))
+               return;
+
+       do_dummy_read(skel->progs.dump_unix);
+
+       bpf_iter_unix__destroy(skel);
+}
+
 /* The expected string is less than 16 bytes */
 static int do_read_with_fd(int iter_fd, const char *expected,
                           bool read_one_char)
@@ -1255,6 +1269,8 @@ void test_bpf_iter(void)
                test_udp4();
        if (test__start_subtest("udp6"))
                test_udp6();
+       if (test__start_subtest("unix"))
+               test_unix();
        if (test__start_subtest("anon"))
                test_anon_iter(false);
        if (test__start_subtest("anon-read-one-char"))
index 3d83b18..8cfaeba 100644 (file)
@@ -12,6 +12,7 @@
 #define tcp6_sock tcp6_sock___not_used
 #define bpf_iter__udp bpf_iter__udp___not_used
 #define udp6_sock udp6_sock___not_used
+#define bpf_iter__unix bpf_iter__unix___not_used
 #define bpf_iter__bpf_map_elem bpf_iter__bpf_map_elem___not_used
 #define bpf_iter__bpf_sk_storage_map bpf_iter__bpf_sk_storage_map___not_used
 #define bpf_iter__sockmap bpf_iter__sockmap___not_used
@@ -32,6 +33,7 @@
 #undef tcp6_sock
 #undef bpf_iter__udp
 #undef udp6_sock
+#undef bpf_iter__unix
 #undef bpf_iter__bpf_map_elem
 #undef bpf_iter__bpf_sk_storage_map
 #undef bpf_iter__sockmap
@@ -103,6 +105,12 @@ struct udp6_sock {
        struct ipv6_pinfo inet6;
 } __attribute__((preserve_access_index));
 
+struct bpf_iter__unix {
+       struct bpf_iter_meta *meta;
+       struct unix_sock *unix_sk;
+       uid_t uid;
+} __attribute__((preserve_access_index));
+
 struct bpf_iter__bpf_map_elem {
        struct bpf_iter_meta *meta;
        struct bpf_map *map;
diff --git a/tools/testing/selftests/bpf/progs/bpf_iter_unix.c b/tools/testing/selftests/bpf/progs/bpf_iter_unix.c
new file mode 100644 (file)
index 0000000..9442390
--- /dev/null
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright Amazon.com Inc. or its affiliates. */
+#include "bpf_iter.h"
+#include "bpf_tracing_net.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_endian.h>
+
+char _license[] SEC("license") = "GPL";
+
+static long sock_i_ino(const struct sock *sk)
+{
+       const struct socket *sk_socket = sk->sk_socket;
+       const struct inode *inode;
+       unsigned long ino;
+
+       if (!sk_socket)
+               return 0;
+
+       inode = &container_of(sk_socket, struct socket_alloc, socket)->vfs_inode;
+       bpf_probe_read_kernel(&ino, sizeof(ino), &inode->i_ino);
+       return ino;
+}
+
+SEC("iter/unix")
+int dump_unix(struct bpf_iter__unix *ctx)
+{
+       struct unix_sock *unix_sk = ctx->unix_sk;
+       struct sock *sk = (struct sock *)unix_sk;
+       struct seq_file *seq;
+       __u32 seq_num;
+
+       if (!unix_sk)
+               return 0;
+
+       seq = ctx->meta->seq;
+       seq_num = ctx->meta->seq_num;
+       if (seq_num == 0)
+               BPF_SEQ_PRINTF(seq, "Num               RefCount Protocol Flags    Type St    Inode Path\n");
+
+       BPF_SEQ_PRINTF(seq, "%pK: %08X %08X %08X %04X %02X %8lu",
+                      unix_sk,
+                      sk->sk_refcnt.refs.counter,
+                      0,
+                      sk->sk_state == TCP_LISTEN ? __SO_ACCEPTCON : 0,
+                      sk->sk_type,
+                      sk->sk_socket ?
+                      (sk->sk_state == TCP_ESTABLISHED ? SS_CONNECTED : SS_UNCONNECTED) :
+                      (sk->sk_state == TCP_ESTABLISHED ? SS_CONNECTING : SS_DISCONNECTING),
+                      sock_i_ino(sk));
+
+       if (unix_sk->addr) {
+               if (!UNIX_ABSTRACT(unix_sk)) {
+                       BPF_SEQ_PRINTF(seq, " %s", unix_sk->addr->name->sun_path);
+               } else {
+                       /* The name of the abstract UNIX domain socket starts
+                        * with '\0' and can contain '\0'.  The null bytes
+                        * should be escaped as done in unix_seq_show().
+                        */
+                       __u64 i, len;
+
+                       len = unix_sk->addr->len - sizeof(short);
+
+                       BPF_SEQ_PRINTF(seq, " @");
+
+                       for (i = 1; i < len; i++) {
+                               /* unix_mkname() tests this upper bound. */
+                               if (i >= sizeof(struct sockaddr_un))
+                                       break;
+
+                               BPF_SEQ_PRINTF(seq, "%c",
+                                              unix_sk->addr->name->sun_path[i] ?:
+                                              '@');
+                       }
+               }
+       }
+
+       BPF_SEQ_PRINTF(seq, "\n");
+
+       return 0;
+}
index 3af0998..eef5646 100644 (file)
@@ -5,6 +5,10 @@
 #define AF_INET                        2
 #define AF_INET6               10
 
+#define __SO_ACCEPTCON         (1 << 16)
+#define UNIX_HASH_SIZE         256
+#define UNIX_ABSTRACT(unix_sk) (unix_sk->addr->hash < UNIX_HASH_SIZE)
+
 #define SOL_TCP                        6
 #define TCP_CONGESTION         13
 #define TCP_CA_NAME_MAX                16