Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftest/bpf: Test batching and bpf_(get|set)sockopt in bpf unix iter.

This patch adds a test for the batching and bpf_(get|set)sockopt in bpf
unix iter.

It does the following.

1. Creates an abstract UNIX domain socket
2. Call bpf_setsockopt()
3. Call bpf_getsockopt() and save the value
4. Call setsockopt()
5. Call getsockopt() and save the value
6. Compare the saved values

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
Link: https://lore.kernel.org/r/20220113002849.4384-5-kuniyu@amazon.co.jp
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Kuniyuki Iwashima and committed by
Alexei Starovoitov
7ff8985c eb7d8f1d

+162
+100
tools/testing/selftests/bpf/prog_tests/bpf_iter_setsockopt_unix.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright Amazon.com Inc. or its affiliates. */ 3 + #include <sys/socket.h> 4 + #include <sys/un.h> 5 + #include <test_progs.h> 6 + #include "bpf_iter_setsockopt_unix.skel.h" 7 + 8 + #define NR_CASES 5 9 + 10 + static int create_unix_socket(struct bpf_iter_setsockopt_unix *skel) 11 + { 12 + struct sockaddr_un addr = { 13 + .sun_family = AF_UNIX, 14 + .sun_path = "", 15 + }; 16 + socklen_t len; 17 + int fd, err; 18 + 19 + fd = socket(AF_UNIX, SOCK_STREAM, 0); 20 + if (!ASSERT_NEQ(fd, -1, "socket")) 21 + return -1; 22 + 23 + len = offsetof(struct sockaddr_un, sun_path); 24 + err = bind(fd, (struct sockaddr *)&addr, len); 25 + if (!ASSERT_OK(err, "bind")) 26 + return -1; 27 + 28 + len = sizeof(addr); 29 + err = getsockname(fd, (struct sockaddr *)&addr, &len); 30 + if (!ASSERT_OK(err, "getsockname")) 31 + return -1; 32 + 33 + memcpy(&skel->bss->sun_path, &addr.sun_path, 34 + len - offsetof(struct sockaddr_un, sun_path)); 35 + 36 + return fd; 37 + } 38 + 39 + static void test_sndbuf(struct bpf_iter_setsockopt_unix *skel, int fd) 40 + { 41 + socklen_t optlen; 42 + int i, err; 43 + 44 + for (i = 0; i < NR_CASES; i++) { 45 + if (!ASSERT_NEQ(skel->data->sndbuf_getsockopt[i], -1, 46 + "bpf_(get|set)sockopt")) 47 + return; 48 + 49 + err = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 50 + &(skel->data->sndbuf_setsockopt[i]), 51 + sizeof(skel->data->sndbuf_setsockopt[i])); 52 + if (!ASSERT_OK(err, "setsockopt")) 53 + return; 54 + 55 + optlen = sizeof(skel->bss->sndbuf_getsockopt_expected[i]); 56 + err = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, 57 + &(skel->bss->sndbuf_getsockopt_expected[i]), 58 + &optlen); 59 + if (!ASSERT_OK(err, "getsockopt")) 60 + return; 61 + 62 + if (!ASSERT_EQ(skel->data->sndbuf_getsockopt[i], 63 + skel->bss->sndbuf_getsockopt_expected[i], 64 + "bpf_(get|set)sockopt")) 65 + return; 66 + } 67 + } 68 + 69 + void test_bpf_iter_setsockopt_unix(void) 70 + { 71 + struct bpf_iter_setsockopt_unix *skel; 72 + int err, unix_fd, iter_fd; 73 + char buf; 74 + 75 + skel = bpf_iter_setsockopt_unix__open_and_load(); 76 + if (!ASSERT_OK_PTR(skel, "open_and_load")) 77 + return; 78 + 79 + unix_fd = create_unix_socket(skel); 80 + if (!ASSERT_NEQ(unix_fd, -1, "create_unix_server")) 81 + goto destroy; 82 + 83 + skel->links.change_sndbuf = bpf_program__attach_iter(skel->progs.change_sndbuf, NULL); 84 + if (!ASSERT_OK_PTR(skel->links.change_sndbuf, "bpf_program__attach_iter")) 85 + goto destroy; 86 + 87 + iter_fd = bpf_iter_create(bpf_link__fd(skel->links.change_sndbuf)); 88 + if (!ASSERT_GE(iter_fd, 0, "bpf_iter_create")) 89 + goto destroy; 90 + 91 + while ((err = read(iter_fd, &buf, sizeof(buf))) == -1 && 92 + errno == EAGAIN) 93 + ; 94 + if (!ASSERT_OK(err, "read iter error")) 95 + goto destroy; 96 + 97 + test_sndbuf(skel, unix_fd); 98 + destroy: 99 + bpf_iter_setsockopt_unix__destroy(skel); 100 + }
+60
tools/testing/selftests/bpf/progs/bpf_iter_setsockopt_unix.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright Amazon.com Inc. or its affiliates. */ 3 + #include "bpf_iter.h" 4 + #include "bpf_tracing_net.h" 5 + #include <bpf/bpf_helpers.h> 6 + #include <limits.h> 7 + 8 + #define AUTOBIND_LEN 6 9 + char sun_path[AUTOBIND_LEN]; 10 + 11 + #define NR_CASES 5 12 + int sndbuf_setsockopt[NR_CASES] = {-1, 0, 8192, INT_MAX / 2, INT_MAX}; 13 + int sndbuf_getsockopt[NR_CASES] = {-1, -1, -1, -1, -1}; 14 + int sndbuf_getsockopt_expected[NR_CASES]; 15 + 16 + static inline int cmpname(struct unix_sock *unix_sk) 17 + { 18 + int i; 19 + 20 + for (i = 0; i < AUTOBIND_LEN; i++) { 21 + if (unix_sk->addr->name->sun_path[i] != sun_path[i]) 22 + return -1; 23 + } 24 + 25 + return 0; 26 + } 27 + 28 + SEC("iter/unix") 29 + int change_sndbuf(struct bpf_iter__unix *ctx) 30 + { 31 + struct unix_sock *unix_sk = ctx->unix_sk; 32 + int i, err; 33 + 34 + if (!unix_sk || !unix_sk->addr) 35 + return 0; 36 + 37 + if (unix_sk->addr->name->sun_path[0]) 38 + return 0; 39 + 40 + if (cmpname(unix_sk)) 41 + return 0; 42 + 43 + for (i = 0; i < NR_CASES; i++) { 44 + err = bpf_setsockopt(unix_sk, SOL_SOCKET, SO_SNDBUF, 45 + &sndbuf_setsockopt[i], 46 + sizeof(sndbuf_setsockopt[i])); 47 + if (err) 48 + break; 49 + 50 + err = bpf_getsockopt(unix_sk, SOL_SOCKET, SO_SNDBUF, 51 + &sndbuf_getsockopt[i], 52 + sizeof(sndbuf_getsockopt[i])); 53 + if (err) 54 + break; 55 + } 56 + 57 + return 0; 58 + } 59 + 60 + char _license[] SEC("license") = "GPL";
+2
tools/testing/selftests/bpf/progs/bpf_tracing_net.h
··· 5 5 #define AF_INET 2 6 6 #define AF_INET6 10 7 7 8 + #define SOL_SOCKET 1 9 + #define SO_SNDBUF 7 8 10 #define __SO_ACCEPTCON (1 << 16) 9 11 10 12 #define SOL_TCP 6