From: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
To: "David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>,
Eric Dumazet <edumazet@google.com>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Martin KaFai Lau <kafai@fb.com>
Cc: Benjamin Herrenschmidt <benh@amazon.com>,
Kuniyuki Iwashima <kuniyu@amazon.co.jp>,
Kuniyuki Iwashima <kuni1840@gmail.com>,
<osa-contribution-log@amazon.com>, <bpf@vger.kernel.org>,
<netdev@vger.kernel.org>, <linux-kernel@vger.kernel.org>
Subject: [PATCH v1 bpf-next 11/11] bpf: Test BPF_SK_REUSEPORT_SELECT_OR_MIGRATE.
Date: Tue, 1 Dec 2020 23:44:18 +0900 [thread overview]
Message-ID: <20201201144418.35045-12-kuniyu@amazon.co.jp> (raw)
In-Reply-To: <20201201144418.35045-1-kuniyu@amazon.co.jp>
This patch adds a test for BPF_SK_REUSEPORT_SELECT_OR_MIGRATE.
Reviewed-by: Benjamin Herrenschmidt <benh@amazon.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
---
.../bpf/prog_tests/migrate_reuseport.c | 164 ++++++++++++++++++
.../bpf/progs/test_migrate_reuseport_kern.c | 54 ++++++
2 files changed, 218 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c
create mode 100644 tools/testing/selftests/bpf/progs/test_migrate_reuseport_kern.c
diff --git a/tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c b/tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c
new file mode 100644
index 000000000000..87c72d9ccadd
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/migrate_reuseport.c
@@ -0,0 +1,164 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Check if we can migrate child sockets.
+ *
+ * 1. call listen() for 5 server sockets.
+ * 2. update a map to migrate all child socket
+ * to the last server socket (migrate_map[cookie] = 4)
+ * 3. call connect() for 25 client sockets.
+ * 4. call close() for first 4 server sockets.
+ * 5. call accept() for the last server socket.
+ *
+ * Author: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <linux/bpf.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+
+#define NUM_SOCKS 5
+#define LOCALHOST "127.0.0.1"
+#define err_exit(condition, message) \
+ do { \
+ if (condition) { \
+ perror("ERROR: " message " "); \
+ exit(1); \
+ } \
+ } while (0)
+
+__u64 server_fds[NUM_SOCKS];
+int prog_fd, reuseport_map_fd, migrate_map_fd;
+
+
+void setup_bpf(void)
+{
+ struct bpf_object *obj;
+ struct bpf_program *prog;
+ struct bpf_map *reuseport_map, *migrate_map;
+ int err;
+
+ obj = bpf_object__open("test_migrate_reuseport_kern.o");
+ err_exit(libbpf_get_error(obj), "opening BPF object file failed");
+
+ err = bpf_object__load(obj);
+ err_exit(err, "loading BPF object failed");
+
+ prog = bpf_program__next(NULL, obj);
+ err_exit(!prog, "loading BPF program failed");
+
+ reuseport_map = bpf_object__find_map_by_name(obj, "reuseport_map");
+ err_exit(!reuseport_map, "loading BPF reuseport_map failed");
+
+ migrate_map = bpf_object__find_map_by_name(obj, "migrate_map");
+ err_exit(!migrate_map, "loading BPF migrate_map failed");
+
+ prog_fd = bpf_program__fd(prog);
+ reuseport_map_fd = bpf_map__fd(reuseport_map);
+ migrate_map_fd = bpf_map__fd(migrate_map);
+}
+
+void test_listen(void)
+{
+ struct sockaddr_in addr;
+ socklen_t addr_len = sizeof(addr);
+ int i, err, optval = 1, migrated_to = NUM_SOCKS - 1;
+ __u64 value;
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(80);
+ inet_pton(AF_INET, LOCALHOST, &addr.sin_addr.s_addr);
+
+ for (i = 0; i < NUM_SOCKS; i++) {
+ server_fds[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ err_exit(server_fds[i] == -1, "socket() for listener sockets failed");
+
+ err = setsockopt(server_fds[i], SOL_SOCKET, SO_REUSEPORT,
+ &optval, sizeof(optval));
+ err_exit(err == -1, "setsockopt() for SO_REUSEPORT failed");
+
+ if (i == 0) {
+ err = setsockopt(server_fds[i], SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF,
+ &prog_fd, sizeof(prog_fd));
+ err_exit(err == -1, "setsockopt() for SO_ATTACH_REUSEPORT_EBPF failed");
+ }
+
+ err = bind(server_fds[i], (struct sockaddr *)&addr, addr_len);
+ err_exit(err == -1, "bind() failed");
+
+ err = listen(server_fds[i], 32);
+ err_exit(err == -1, "listen() failed");
+
+ err = bpf_map_update_elem(reuseport_map_fd, &i, &server_fds[i], BPF_NOEXIST);
+ err_exit(err == -1, "updating BPF reuseport_map failed");
+
+ err = bpf_map_lookup_elem(reuseport_map_fd, &i, &value);
+ err_exit(err == -1, "looking up BPF reuseport_map failed");
+
+ printf("fd[%d] (cookie: %llu) -> fd[%d]\n", i, value, migrated_to);
+ err = bpf_map_update_elem(migrate_map_fd, &value, &migrated_to, BPF_NOEXIST);
+ err_exit(err == -1, "updating BPF migrate_map failed");
+ }
+}
+
+void test_connect(void)
+{
+ struct sockaddr_in addr;
+ socklen_t addr_len = sizeof(addr);
+ int i, err, client_fd;
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(80);
+ inet_pton(AF_INET, LOCALHOST, &addr.sin_addr.s_addr);
+
+ for (i = 0; i < NUM_SOCKS * 5; i++) {
+ client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+ err_exit(client_fd == -1, "socket() for listener sockets failed");
+
+ err = connect(client_fd, (struct sockaddr *)&addr, addr_len);
+ err_exit(err == -1, "connect() failed");
+
+ close(client_fd);
+ }
+}
+
+void test_close(void)
+{
+ int i;
+
+ for (i = 0; i < NUM_SOCKS - 1; i++)
+ close(server_fds[i]);
+}
+
+void test_accept(void)
+{
+ struct sockaddr_in addr;
+ socklen_t addr_len = sizeof(addr);
+ int cnt, client_fd;
+
+ fcntl(server_fds[NUM_SOCKS - 1], F_SETFL, O_NONBLOCK);
+
+ for (cnt = 0; cnt < NUM_SOCKS * 5; cnt++) {
+ client_fd = accept(server_fds[NUM_SOCKS - 1], (struct sockaddr *)&addr, &addr_len);
+ err_exit(client_fd == -1, "accept() failed");
+ }
+
+ printf("%d accepted, %d is expected\n", cnt, NUM_SOCKS * 5);
+}
+
+int main(void)
+{
+ setup_bpf();
+ test_listen();
+ test_connect();
+ test_close();
+ test_accept();
+ close(server_fds[NUM_SOCKS - 1]);
+ return 0;
+}
diff --git a/tools/testing/selftests/bpf/progs/test_migrate_reuseport_kern.c b/tools/testing/selftests/bpf/progs/test_migrate_reuseport_kern.c
new file mode 100644
index 000000000000..28d007b3a7a7
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_migrate_reuseport_kern.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Check if we can migrate child sockets.
+ *
+ * 1. If reuse_md->migration is 0 (SYN packet),
+ * return SK_PASS without selecting a listener.
+ * 2. If reuse_md->migration is not 0 (socket migration),
+ * select a listener (reuseport_map[migrate_map[cookie]])
+ *
+ * Author: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
+ */
+
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+
+#define NULL ((void *)0)
+
+int _version SEC("version") = 1;
+
+struct bpf_map_def SEC("maps") reuseport_map = {
+ .type = BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
+ .key_size = sizeof(int),
+ .value_size = sizeof(__u64),
+ .max_entries = 256,
+};
+
+struct bpf_map_def SEC("maps") migrate_map = {
+ .type = BPF_MAP_TYPE_HASH,
+ .key_size = sizeof(__u64),
+ .value_size = sizeof(int),
+ .max_entries = 256,
+};
+
+SEC("sk_reuseport/migrate")
+int select_by_skb_data(struct sk_reuseport_md *reuse_md)
+{
+ int *key, flags = 0;
+ __u64 cookie;
+
+ if (!reuse_md->migration)
+ return SK_PASS;
+
+ cookie = bpf_get_socket_cookie(reuse_md->sk);
+
+ key = bpf_map_lookup_elem(&migrate_map, &cookie);
+ if (key == NULL)
+ return SK_DROP;
+
+ bpf_sk_select_reuseport(reuse_md, &reuseport_map, key, flags);
+
+ return SK_PASS;
+}
+
+char _license[] SEC("license") = "GPL";
--
2.17.2 (Apple Git-113)
next prev parent reply other threads:[~2020-12-01 14:48 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-01 14:44 [PATCH v1 bpf-next 00/11] Socket migration for SO_REUSEPORT Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 01/11] tcp: Keep TCP_CLOSE sockets in the reuseport group Kuniyuki Iwashima
2020-12-05 1:31 ` Martin KaFai Lau
2020-12-06 4:38 ` Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 02/11] bpf: Define migration types for SO_REUSEPORT Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 03/11] tcp: Migrate TCP_ESTABLISHED/TCP_SYN_RECV sockets in accept queues Kuniyuki Iwashima
2020-12-01 15:25 ` Eric Dumazet
2020-12-03 14:14 ` Kuniyuki Iwashima
2020-12-03 14:31 ` Eric Dumazet
2020-12-03 15:41 ` Kuniyuki Iwashima
2020-12-07 20:33 ` Martin KaFai Lau
2020-12-08 6:31 ` Kuniyuki Iwashima
2020-12-08 7:34 ` Martin KaFai Lau
2020-12-08 8:17 ` Kuniyuki Iwashima
2020-12-09 3:09 ` Martin KaFai Lau
2020-12-09 8:05 ` Kuniyuki Iwashima
2020-12-09 16:57 ` Kuniyuki Iwashima
2020-12-10 1:53 ` Martin KaFai Lau
2020-12-10 5:58 ` Kuniyuki Iwashima
2020-12-10 19:33 ` Martin KaFai Lau
2020-12-14 17:16 ` Kuniyuki Iwashima
2020-12-05 1:42 ` Martin KaFai Lau
2020-12-06 4:41 ` Kuniyuki Iwashima
[not found] ` <20201205160307.91179-1-kuniyu@amazon.co.jp>
2020-12-07 20:14 ` Martin KaFai Lau
2020-12-08 6:27 ` Kuniyuki Iwashima
2020-12-08 8:13 ` Martin KaFai Lau
2020-12-08 9:02 ` Kuniyuki Iwashima
2020-12-08 6:54 ` Martin KaFai Lau
2020-12-08 7:42 ` Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 04/11] tcp: Migrate TFO requests causing RST during TCP_SYN_RECV Kuniyuki Iwashima
2020-12-01 15:30 ` Eric Dumazet
2020-12-01 14:44 ` [PATCH v1 bpf-next 05/11] tcp: Migrate TCP_NEW_SYN_RECV requests Kuniyuki Iwashima
2020-12-01 15:13 ` Eric Dumazet
2020-12-03 14:12 ` Kuniyuki Iwashima
2020-12-10 0:07 ` Martin KaFai Lau
2020-12-10 5:15 ` Kuniyuki Iwashima
2020-12-10 18:49 ` Martin KaFai Lau
2020-12-14 17:03 ` Kuniyuki Iwashima
2020-12-15 2:58 ` Martin KaFai Lau
2020-12-16 16:41 ` Kuniyuki Iwashima
2020-12-16 22:24 ` Martin KaFai Lau
2020-12-01 14:44 ` [PATCH v1 bpf-next 06/11] bpf: Introduce two attach types for BPF_PROG_TYPE_SK_REUSEPORT Kuniyuki Iwashima
2020-12-02 2:04 ` Andrii Nakryiko
2020-12-02 19:19 ` Martin KaFai Lau
2020-12-03 4:24 ` Martin KaFai Lau
2020-12-03 14:16 ` Kuniyuki Iwashima
2020-12-04 5:56 ` Martin KaFai Lau
2020-12-06 4:32 ` Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 07/11] libbpf: Set expected_attach_type " Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 08/11] bpf: Add migration to sk_reuseport_(kern|md) Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 09/11] bpf: Support bpf_get_socket_cookie_sock() for BPF_PROG_TYPE_SK_REUSEPORT Kuniyuki Iwashima
2020-12-04 19:58 ` Martin KaFai Lau
2020-12-06 4:36 ` Kuniyuki Iwashima
2020-12-01 14:44 ` [PATCH v1 bpf-next 10/11] bpf: Call bpf_run_sk_reuseport() for socket migration Kuniyuki Iwashima
2020-12-01 14:44 ` Kuniyuki Iwashima [this message]
2020-12-05 1:50 ` [PATCH v1 bpf-next 11/11] bpf: Test BPF_SK_REUSEPORT_SELECT_OR_MIGRATE Martin KaFai Lau
2020-12-06 4:43 ` Kuniyuki Iwashima
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201201144418.35045-12-kuniyu@amazon.co.jp \
--to=kuniyu@amazon.co.jp \
--cc=ast@kernel.org \
--cc=benh@amazon.com \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=kafai@fb.com \
--cc=kuba@kernel.org \
--cc=kuni1840@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=osa-contribution-log@amazon.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®