From patchwork Wed Jan 18 20:27:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 45475 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2548584wrn; Wed, 18 Jan 2023 12:32:37 -0800 (PST) X-Google-Smtp-Source: AMrXdXvEsJshrq7Awd1NM8EJQTiXfy6w8pURctceiPJsarOedUzvDsDSpamEZiqIcVyYUCmxKlyn X-Received: by 2002:a17:902:b713:b0:194:3fa4:3f46 with SMTP id d19-20020a170902b71300b001943fa43f46mr7962031pls.29.1674073957402; Wed, 18 Jan 2023 12:32:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674073957; cv=none; d=google.com; s=arc-20160816; b=gu5OUKJwL/lbsce9QKXJdQ5mm1iip3LEwRd4PW1Bn1wazuz8gARkUUETp7SDBEwUC0 9V/G12D/hm5HbTtW0hqgMPWjlKS0fJaEM59C2ONmQhppmF0550M9jFFebh3P2ZLBskkQ QvdqnOWT4Dyji577kkFcvoU4WLWMzPsDFd2kWkfLQnhWyc+1AYs9BagE6QH4JbeWtm8g ObZ2cYbIqpn1JE98BbJcuE8jVNUGSHVyh6P990eO7VlyaBdzPrMisR7K08fbx0gV7Ipu +0pgM/3K9HH/e6eUaEBU4nwJwFTqVQ1FkDAFfklN+hac0H9z1L4ebafNQjCqToDomnNV eiiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=G3SSHfwD1JWrYtYUVCovQ7Td7i1weIvYDlK0fNQrT54=; b=ZUHXc2M2fyYU6jF9tGcwIwT5oy79/coOEKPSq6V70QMCpblzAH++7cn4eRUha9PaR6 6pcqvny+tADgCRt5hrwwOqu31tfGfm5//D9csYStp7VqtDDe1fHDUJzaHq2XK2Ravzon JsgwRrcjtwlTsTCDszAM9cAlGqx1E5ioPPeaI9UsUdbXT4G5eSNJWWA84rCEr7m7mdip 6ILVMt4paI8KeC26nHcktoS+rkp9+hqO7cn3Cz1oYgoeP64n32U0EG8z4JuyszDuTUKm MWLmDMYg3IZnAdu0Bpd8A9NPkgXGU+Yr8EGwdEEB59BlFUn76KUbBkdr7ExrZzD9FT0H gYMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=lqiHRIGe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k18-20020a170902d59200b00186892f9f29si2986889plh.488.2023.01.18.12.32.24; Wed, 18 Jan 2023 12:32:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=lqiHRIGe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230117AbjARUaO (ORCPT + 99 others); Wed, 18 Jan 2023 15:30:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230136AbjARU3z (ORCPT ); Wed, 18 Jan 2023 15:29:55 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C3D85EF96 for ; Wed, 18 Jan 2023 12:29:27 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id b10so260019pjo.1 for ; Wed, 18 Jan 2023 12:29:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=G3SSHfwD1JWrYtYUVCovQ7Td7i1weIvYDlK0fNQrT54=; b=lqiHRIGe8gj28v5IdrqBD149FOkVZQtrXO1HuNvbrlLaDvAuhbJY0jmZOtUC83FLlr MoJ4uWPuQq5mS5cWJZhqyMxyLYSi1+TEHqCBWfyb6a+WPYwcumKZ/fHWh6tfk4iH1KMr mmEmSLtHSkhaTM1Rbxq57fqW5lbW3J0l0cn99mTAxb5dUFvgIa8AHfnNjcMv7O5zeu6w pi6SSBZhCVgf4PFi/XGE4YlaEt6XQ+rK66MaPbQAJjWHHWw1r08yzkIdOtBgMtsJvd6c 9GsTb2PN6FOf8S4mQzvozkpa9oPE3Z2CVEQqpcjc9GjIGVThuFJkhZGealsuuiakVnx3 LMkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=G3SSHfwD1JWrYtYUVCovQ7Td7i1weIvYDlK0fNQrT54=; b=c9IqknlwoysP0tiVTHaNStqn79crJPQvdQn0ew+maZYltA9VZyP+V3rlumcqvCMgSX KfqY10/dmm52TxuZ7j8/52uuyx/IQatFsncYyyNyBm5SzlNH5KGa7k9vyvg28eZcjEAU f13f1uQ6rhosHfoVf7Ty1j/d3yBFVujI9+AkJhD6Bgo3b83emMusCPO1v0GLez5XFIa3 d0RQ/qckTvPk9T2PlaHdFLWT9l8mAKrYOApKegqLks8tYxhvjWrq8Q9g2IP6/tt34ztx IEPAkp9FeB0Cun1psGa5Denstnug35N5JmAcp+gA42tMsghLI7BLrEbr8HYqs+TRPtgj 0HLw== X-Gm-Message-State: AFqh2kpachrSlyUh7a7AsuugHTzr3nuLNyH2LrDhM+bX0RmdbWWPbrZE NUjjKd5xSLqVhimwXoo6BkZ9Fw== X-Received: by 2002:a17:90a:7408:b0:229:ca6a:464e with SMTP id a8-20020a17090a740800b00229ca6a464emr3121546pjg.36.1674073766884; Wed, 18 Jan 2023 12:29:26 -0800 (PST) Received: from [127.0.1.1] (c-73-164-155-12.hsd1.wa.comcast.net. [73.164.155.12]) by smtp.gmail.com with ESMTPSA id t13-20020a17090a3b4d00b002132f3e71c6sm1724948pjf.52.2023.01.18.12.29.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 12:29:26 -0800 (PST) From: Bobby Eshleman Date: Wed, 18 Jan 2023 12:27:41 -0800 Subject: [PATCH RFC 3/3] selftests/bpf: Add a test case for vsock sockmap MIME-Version: 1.0 Message-Id: <20230118-support-vsock-sockmap-connectible-v1-3-d47e6294827b@bytedance.com> References: <20230118-support-vsock-sockmap-connectible-v1-0-d47e6294827b@bytedance.com> In-Reply-To: <20230118-support-vsock-sockmap-connectible-v1-0-d47e6294827b@bytedance.com> To: Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Andrii Nakryiko , Mykola Lysenko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Shuah Khan Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, Bobby Eshleman , Cong Wang X-Mailer: b4 0.11.2 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1755393774384788732?= X-GMAIL-MSGID: =?utf-8?q?1755393774384788732?= Add a test case testing the redirection from connectible AF_VSOCK sockets to connectible AF_UNIX sockets. Signed-off-by: Bobby Eshleman --- .../selftests/bpf/prog_tests/sockmap_listen.c | 163 +++++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index 2cf0c7a3fe232..8b5a2e09c9ede 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -249,6 +250,16 @@ static void init_addr_loopback6(struct sockaddr_storage *ss, socklen_t *len) *len = sizeof(*addr6); } +static void init_addr_loopback_vsock(struct sockaddr_storage *ss, socklen_t *len) +{ + struct sockaddr_vm *addr = memset(ss, 0, sizeof(*ss)); + + addr->svm_family = AF_VSOCK; + addr->svm_port = VMADDR_PORT_ANY; + addr->svm_cid = VMADDR_CID_LOCAL; + *len = sizeof(*addr); +} + static void init_addr_loopback(int family, struct sockaddr_storage *ss, socklen_t *len) { @@ -259,6 +270,9 @@ static void init_addr_loopback(int family, struct sockaddr_storage *ss, case AF_INET6: init_addr_loopback6(ss, len); return; + case AF_VSOCK: + init_addr_loopback_vsock(ss, len); + return; default: FAIL("unsupported address family %d", family); } @@ -1434,6 +1448,8 @@ static const char *family_str(sa_family_t family) return "IPv6"; case AF_UNIX: return "Unix"; + case AF_VSOCK: + return "VSOCK"; default: return "unknown"; } @@ -1644,6 +1660,151 @@ static void test_unix_redir(struct test_sockmap_listen *skel, struct bpf_map *ma unix_skb_redir_to_connected(skel, map, sotype); } +/* Returns two connected loopback vsock sockets */ +static int vsock_socketpair_connectible(int sotype, int *v0, int *v1) +{ + struct sockaddr_storage addr; + socklen_t len = sizeof(addr); + int s, p, c; + + s = socket_loopback(AF_VSOCK, sotype); + if (s < 0) + return -1; + + c = xsocket(AF_VSOCK, sotype | SOCK_NONBLOCK, 0); + if (c == -1) + goto close_srv; + + if (getsockname(s, sockaddr(&addr), &len) < 0) + goto close_cli; + + if (connect(c, sockaddr(&addr), len) < 0 && errno != EINPROGRESS) { + FAIL_ERRNO("connect"); + goto close_cli; + } + + len = sizeof(addr); + p = accept_timeout(s, sockaddr(&addr), &len, IO_TIMEOUT_SEC); + if (p < 0) + goto close_cli; + + *v0 = p; + *v1 = c; + + return 0; + +close_cli: + close(c); +close_srv: + close(s); + + return -1; +} + +static void vsock_unix_redir_connectible(int sock_mapfd, int verd_mapfd, + enum redir_mode mode, int sotype) +{ + const char *log_prefix = redir_mode_str(mode); + char a = 'a', b = 'b'; + int u0, u1, v0, v1; + int sfd[2]; + unsigned int pass; + int err, n; + u32 key; + + zero_verdict_count(verd_mapfd); + + if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0, sfd)) + return; + + u0 = sfd[0]; + u1 = sfd[1]; + + err = vsock_socketpair_connectible(sotype, &v0, &v1); + if (err) { + FAIL("vsock_socketpair_connectible() failed"); + goto close_uds; + } + + err = add_to_sockmap(sock_mapfd, u0, v0); + if (err) { + FAIL("add_to_sockmap failed"); + goto close_vsock; + } + + n = write(v1, &a, sizeof(a)); + if (n < 0) + FAIL_ERRNO("%s: write", log_prefix); + if (n == 0) + FAIL("%s: incomplete write", log_prefix); + if (n < 1) + goto out; + + n = recv(mode == REDIR_INGRESS ? u0 : u1, &b, sizeof(b), MSG_DONTWAIT); + if (n < 0) + FAIL("%s: recv() err, errno=%d", log_prefix, errno); + if (n == 0) + FAIL("%s: incomplete recv", log_prefix); + if (b != a) + FAIL("%s: vsock socket map failed, %c != %c", log_prefix, a, b); + + key = SK_PASS; + err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass); + if (err) + goto out; + if (pass != 1) + FAIL("%s: want pass count 1, have %d", log_prefix, pass); +out: + key = 0; + bpf_map_delete_elem(sock_mapfd, &key); + key = 1; + bpf_map_delete_elem(sock_mapfd, &key); + +close_vsock: + close(v0); + close(v1); + +close_uds: + close(u0); + close(u1); +} + +static void vsock_unix_skb_redir_connectible(struct test_sockmap_listen *skel, + struct bpf_map *inner_map, + int sotype) +{ + int verdict = bpf_program__fd(skel->progs.prog_skb_verdict); + int verdict_map = bpf_map__fd(skel->maps.verdict_map); + int sock_map = bpf_map__fd(inner_map); + int err; + + err = xbpf_prog_attach(verdict, sock_map, BPF_SK_SKB_VERDICT, 0); + if (err) + return; + + skel->bss->test_ingress = false; + vsock_unix_redir_connectible(sock_map, verdict_map, REDIR_EGRESS, sotype); + skel->bss->test_ingress = true; + vsock_unix_redir_connectible(sock_map, verdict_map, REDIR_INGRESS, sotype); + + xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT); +} + +static void test_vsock_redir(struct test_sockmap_listen *skel, struct bpf_map *map) +{ + const char *family_name, *map_name; + char s[MAX_TEST_NAME]; + + family_name = family_str(AF_VSOCK); + map_name = map_type_str(map); + snprintf(s, sizeof(s), "%s %s %s", map_name, family_name, __func__); + if (!test__start_subtest(s)) + return; + + vsock_unix_skb_redir_connectible(skel, map, SOCK_STREAM); + vsock_unix_skb_redir_connectible(skel, map, SOCK_SEQPACKET); +} + static void test_reuseport(struct test_sockmap_listen *skel, struct bpf_map *map, int family, int sotype) { @@ -2015,12 +2176,14 @@ void serial_test_sockmap_listen(void) run_tests(skel, skel->maps.sock_map, AF_INET6); test_unix_redir(skel, skel->maps.sock_map, SOCK_DGRAM); test_unix_redir(skel, skel->maps.sock_map, SOCK_STREAM); + test_vsock_redir(skel, skel->maps.sock_map); skel->bss->test_sockmap = false; run_tests(skel, skel->maps.sock_hash, AF_INET); run_tests(skel, skel->maps.sock_hash, AF_INET6); test_unix_redir(skel, skel->maps.sock_hash, SOCK_DGRAM); test_unix_redir(skel, skel->maps.sock_hash, SOCK_STREAM); + test_vsock_redir(skel, skel->maps.sock_hash); test_sockmap_listen__destroy(skel); }