Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp876823rwb; Thu, 19 Jan 2023 03:59:58 -0800 (PST) X-Google-Smtp-Source: AMrXdXtWK8MvK52JYidvARKQcP5BQrP7kkbpq4uMUmeKDtEJnWKHAmBl97HGIqSKUt7kGvHeUOrZ X-Received: by 2002:a05:6402:219:b0:499:70a8:f915 with SMTP id t25-20020a056402021900b0049970a8f915mr10283094edv.21.1674129598465; Thu, 19 Jan 2023 03:59:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674129598; cv=none; d=google.com; s=arc-20160816; b=RSl52Da21pngD2h56OgUL1+bQ6ipUj7NGX0vGA+vitbrfbsgjJUza/Nq6OQTPD8F32 bAvV69oFgzyTHnuAL/8xkDLUwGfhdIEgepCrILpXns4QdyyTBQ5c6C5D8cWb5Ip/dhRr r7hFWaXkjGHPkh2TxZS0HEtFEU8ngF3W8XsGJoRDFIa42zV9+xwQMY0Tbrg42mz2cwZa fQNixXderBUnEv6znb4DHYatOwzECt9eYr8lAElrxcTk93pQ/l538qrushG2ReopbStt Ek8IyJgrTY3jfIvdvXL7lesP4eRmBAbNWgJ6JmD0Dk1WikvlSfMwDIGa3L19Cd1X77T0 w8yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=0v4ROUKBN8AgC62/AUUCjqZ8SOsKWzPjsz1NY4yvxkM=; b=Oq4fLKCAg5kXOUIg6CJJ5QqlGiI5JoDzLg3RnzxnT4zdwB6ndQunDnShKjUNqvyq7M ZZz0fauv8jqKSIGChLEjocH9vK8LtXJTAWibw7q/p70/BaMVt4PJDjHyuItf8XDVWlrK 5d0PwBlY6mxo2elIdUvj2oFYnv56neuyGhp7xfObUg2uJYMQlD0AQmBfVOwa4uUiWfR0 7tiWJ6UWlcizwxthcxpgrisNB8csnPaAyCqGCDdsUHIOWmT36BepHP27ok6S3SEkgKpd 3s5seiODb8cGL92FhD/qX1zSguPs9ZRVTL8WCHbCgtMtTxNb0ZH172KN2ulDhNTGBGEw 99+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RKaktask; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l16-20020a056402255000b0046197a8b7a1si34566506edb.485.2023.01.19.03.59.47; Thu, 19 Jan 2023 03:59:58 -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=@redhat.com header.s=mimecast20190719 header.b=RKaktask; 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=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230146AbjASKtS (ORCPT + 44 others); Thu, 19 Jan 2023 05:49:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229776AbjASKtI (ORCPT ); Thu, 19 Jan 2023 05:49:08 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A463630EE for ; Thu, 19 Jan 2023 02:48:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1674125303; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=0v4ROUKBN8AgC62/AUUCjqZ8SOsKWzPjsz1NY4yvxkM=; b=RKaktaskl/1bidlG7nmW2VM2YUNlf/RHs0Ha3KAQfQ4PmITLhcZ+mSgf7iVcdW5KQsl1In UeOICv/lcqXLsKIXWXbuyLNpH3ISb0lpDbfgVOMcU9D8JcriZEs+a481iR0pJ3MCC54ut0 vmboNeFvQydheRK1xP1wZzLxBEiMVPY= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-284-a_nfdMfMNBGkIbj2bn9JWA-1; Thu, 19 Jan 2023 05:48:22 -0500 X-MC-Unique: a_nfdMfMNBGkIbj2bn9JWA-1 Received: by mail-qt1-f197.google.com with SMTP id w25-20020ac86b19000000b003b692f65ca2so253236qts.20 for ; Thu, 19 Jan 2023 02:48:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0v4ROUKBN8AgC62/AUUCjqZ8SOsKWzPjsz1NY4yvxkM=; b=Z/BfoPYDi3ftWYucMFNqMwMymS5umIwAk8cLxkRgZ/ajR67GADL1GRitCwHM7ZBol0 iFAA5EoSC+0qagVpq5kGr20zUPo9luYZ/WZL3AmtAoxHtQLEJF56zqykI1Ex1wYtaBMP yLlYP6Bn7h6Xdi90Z1d+mgtWw1fzRUTEo73uUfWD+UWZrki/RnzFQ2YGr7ruEC86PEab CB+WS09MUE1S/99+/bZ2+mliTIlghVz7nV6Hgkm2oYwmQG1CVODBYoRqPw2xQE6cHevs K2JNOjBmIaHVIQvR+e14RcQXauj9NnpieZEt3rQFFQGB3NfknS2xG7xotfqZBwRNLE4m 6Jrw== X-Gm-Message-State: AFqh2koeJq651pCV8W6lWBzw3M4hUQ5P/QG1WFSriY5+zjS1y6JcnIrN b9uY4m15iUTJlBhYdbfEy6myi0s63raTXhjYi0da7H5JjLOfwA2LwlfIj82NDQB+1QEZ1Zy2pIM vcnPiPknTFXpq0xYsTjNk8rnY X-Received: by 2002:ac8:5b90:0:b0:3a8:30c9:ba8f with SMTP id a16-20020ac85b90000000b003a830c9ba8fmr21187470qta.28.1674125301888; Thu, 19 Jan 2023 02:48:21 -0800 (PST) X-Received: by 2002:ac8:5b90:0:b0:3a8:30c9:ba8f with SMTP id a16-20020ac85b90000000b003a830c9ba8fmr21187434qta.28.1674125301602; Thu, 19 Jan 2023 02:48:21 -0800 (PST) Received: from sgarzare-redhat (host-82-57-51-245.retail.telecomitalia.it. [82.57.51.245]) by smtp.gmail.com with ESMTPSA id cb15-20020a05622a1f8f00b003ab43dabfb1sm5887379qtb.55.2023.01.19.02.48.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jan 2023 02:48:21 -0800 (PST) Date: Thu, 19 Jan 2023 11:48:13 +0100 From: Stefano Garzarella To: Bobby Eshleman Cc: Stefan Hajnoczi , "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 , 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, Cong Wang Subject: Re: [PATCH RFC 3/3] selftests/bpf: Add a test case for vsock sockmap Message-ID: <20230119104813.2bkmb3t43eq63i3o@sgarzare-redhat> References: <20230118-support-vsock-sockmap-connectible-v1-0-d47e6294827b@bytedance.com> <20230118-support-vsock-sockmap-connectible-v1-3-d47e6294827b@bytedance.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20230118-support-vsock-sockmap-connectible-v1-3-d47e6294827b@bytedance.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 On Wed, Jan 18, 2023 at 12:27:41PM -0800, Bobby Eshleman wrote: >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; Wait, IIUC we only use loopback, so why do we need to attach the vhost-vsock-pci device to QEMU? At that point if we add CONFIG_VSOCKETS_LOOPBACK in all configurations, it should also work with aarch64 and s390x. Thanks, Stefano >+ *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); > } > >-- >2.30.2 >