From patchwork Wed May 31 00:35:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 101167 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2553739vqr; Tue, 30 May 2023 17:36:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7t8Qy4ZjiumysJSdFqAMOg1E5frK2Eug/8r1BKvOiAXVY7BKgHKcyNSRxT5IH8X8ky/x11 X-Received: by 2002:a05:6a00:1389:b0:645:1081:98ec with SMTP id t9-20020a056a00138900b00645108198ecmr5242039pfg.13.1685493406840; Tue, 30 May 2023 17:36:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685493406; cv=none; d=google.com; s=arc-20160816; b=HwcS9PXmh151BIJ/AHPyyU86t7fv/RlHwcZF+p8qWzMDwT0xnsvdo2X9DqS3ZN8JfE hEXVr+7fNWlB1QJbZ80yUXUah5+JVOpwLcwixqRftVDgVNY0kNT3bmMP8s1gtXZBIcr2 rhOhhj1hmBBbdH2hiiUD/BXcuhtazt7a9ey4IFFdaNjgV0NKiE+MCB3cLR8wKTHKuZG6 0r7XjtyLAUxbElUKnMNRagMJwPyOp7bVQClt3CrTcIQ/uXvJ/lYIxkXHT8YDHB/IjuOP 4v0GJTFPXInX+RuZrU9ycxoDYyFJ+APA1ok4a1JauaQmWCPdgNGQTMsFfYuZUgFnROmT UKwQ== 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=1blZp32leas5kXJi+UOuXoD7JhXTUgNgwlQOmeJG49g=; b=i932dgJIdPQS9P1CYX1fsQS00gFArf3eZnNCA8ZD/+wlEJxv7mYfuLkEaJInf9fo1J PUBGe/UjPlavgga/yc1C4jxQmVKCQZt+qzAxmXEQzHzAvk1vMFzhybWtR2ZJ06l0GAW1 SEam+Me2AFtCjPAo3lBYjZBlETcsvzk7mHQNCsV0OCLYc9RpNz8rfnl9TxZBPMzYu1xi 12gEY5M4u89163F3OSuZ3yiP+1OLKwDfJcMgKJ1GqNgsFMT9di2sCCyzGe0NuUZ5uFP9 j2aZGR4lKd+/Z0NlMvkIej6Lpq6pJ0TtU2f9CCIoL7T91Ygcxdh7Y0Zsw8+b9koafW/G Odlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=a2xdvGEp; 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=QUARANTINE sp=QUARANTINE 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 e18-20020aa798d2000000b0064d5dd5adbfsi2589089pfm.292.2023.05.30.17.36.34; Tue, 30 May 2023 17:36:46 -0700 (PDT) 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 header.s=google header.b=a2xdvGEp; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233563AbjEaAgC (ORCPT + 99 others); Tue, 30 May 2023 20:36:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51228 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233537AbjEaAf5 (ORCPT ); Tue, 30 May 2023 20:35:57 -0400 Received: from mail-pf1-x42d.google.com (mail-pf1-x42d.google.com [IPv6:2607:f8b0:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E2EA10C for ; Tue, 30 May 2023 17:35:27 -0700 (PDT) Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-64d2da69fdfso5921443b3a.0 for ; Tue, 30 May 2023 17:35:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1685493313; x=1688085313; 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=1blZp32leas5kXJi+UOuXoD7JhXTUgNgwlQOmeJG49g=; b=a2xdvGEpuMoly584VYJhtJ3a65HlJ4dm4abWKMTkPlSryuRFTN6VQ2nv5O338oXWHn JVL8m0YyZifXMedOOTURwPS/LCWOE3crjtMJO0JRBwLl5w4AsGquJ9k/FQafY8LFGiRG U98GIjnqV8HTdVBmj7WtYs1GOkcbvBmyyET2Y054EGHAMuhNqo2sFGZ58QrxSP5lWYhx CX2AN09+nXB6JTyq+jZUHINYqEMc3eTd14eFqMrRMrmmaaFdpwrs+SqJ0OaFOi2HsjC+ q7QBsBJsTK0OHXwkjIQc1H69opBqAWxcchcCJbHZYMvd1MBB2E8BEeEpY2EkcJLXvwO2 RqgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685493313; x=1688085313; 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=1blZp32leas5kXJi+UOuXoD7JhXTUgNgwlQOmeJG49g=; b=hN7AnTYoyxolAfEPt7IqX+UACCYvxuPaRicfHbY5Z+rABxODsbpoiuLNmDjE+gqxGg crdAV348oea7i1YLEoAvu5H/i03Ncwhlcs1to2LtpR1Zhs2Z2OWKnQ5u5zli7+NhMKL2 hNsY9w1TEDfw0brInTO4dwn9TbUkAh/45ug8Uzmco8aUd+fgMyCGfBx2SRw9bDWuUaNB nZpjTOt6qLnR7vqbgG+WzEHjLUTk/fc2qprmYIzkNDS3HnYtj8r+YL+njmgPgD4NC/5y 8MlGveSokR5s5kuakGVvltrbOOxObDguPjIcF9k8BeSmtqDWpIM2QzHC7Oj7yd4dbP8t sTvA== X-Gm-Message-State: AC+VfDyWObobW1o8XRhv+OchI2ftvCHkRE8FoM7YqkBBe5UBGRww39fQ m55VuR20N6YfimWKKKubiAr1fQ== X-Received: by 2002:a05:6a21:339a:b0:10c:9e35:857a with SMTP id yy26-20020a056a21339a00b0010c9e35857amr4322582pzb.49.1685493313118; Tue, 30 May 2023 17:35:13 -0700 (PDT) Received: from [172.17.0.2] (c-67-170-131-147.hsd1.wa.comcast.net. [67.170.131.147]) by smtp.gmail.com with ESMTPSA id j12-20020a62b60c000000b0064cb0845c77sm2151340pff.122.2023.05.30.17.35.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 May 2023 17:35:12 -0700 (PDT) From: Bobby Eshleman Date: Wed, 31 May 2023 00:35:08 +0000 Subject: [PATCH RFC net-next v3 4/8] vsock: make vsock bind reusable MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v3-4-c2414413ef6a@bytedance.com> References: <20230413-b4-vsock-dgram-v3-0-c2414413ef6a@bytedance.com> In-Reply-To: <20230413-b4-vsock-dgram-v3-0-c2414413ef6a@bytedance.com> To: Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , VMware PV-Drivers Reviewers Cc: kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, Bobby Eshleman X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767367934926792324?= X-GMAIL-MSGID: =?utf-8?q?1767367934926792324?= This commit makes the bind table management functions in vsock usable for different bind tables. For use by datagrams in a future patch. Signed-off-by: Bobby Eshleman --- net/vmw_vsock/af_vsock.c | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 578272a987be..ed02a5592e43 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -230,11 +230,12 @@ static void __vsock_remove_connected(struct vsock_sock *vsk) sock_put(&vsk->sk); } -static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr) +struct sock *vsock_find_bound_socket_common(struct sockaddr_vm *addr, + struct list_head *bind_table) { struct vsock_sock *vsk; - list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) { + list_for_each_entry(vsk, bind_table, bound_table) { if (vsock_addr_equals_addr(addr, &vsk->local_addr)) return sk_vsock(vsk); @@ -247,6 +248,11 @@ static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr) return NULL; } +static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr) +{ + return vsock_find_bound_socket_common(addr, vsock_bound_sockets(addr)); +} + static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src, struct sockaddr_vm *dst) { @@ -643,12 +649,17 @@ static void vsock_pending_work(struct work_struct *work) /**** SOCKET OPERATIONS ****/ -static int __vsock_bind_connectible(struct vsock_sock *vsk, - struct sockaddr_vm *addr) +int vsock_bind_common(struct vsock_sock *vsk, + struct sockaddr_vm *addr, + struct list_head *bind_table, + size_t table_size) { static u32 port; struct sockaddr_vm new_addr; + if (table_size < VSOCK_HASH_SIZE) + return -1; + if (!port) port = get_random_u32_above(LAST_RESERVED_PORT); @@ -664,7 +675,8 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, new_addr.svm_port = port++; - if (!__vsock_find_bound_socket(&new_addr)) { + if (!vsock_find_bound_socket_common(&new_addr, + &bind_table[VSOCK_HASH(addr)])) { found = true; break; } @@ -681,7 +693,8 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, return -EACCES; } - if (__vsock_find_bound_socket(&new_addr)) + if (vsock_find_bound_socket_common(&new_addr, + &bind_table[VSOCK_HASH(addr)])) return -EADDRINUSE; } @@ -693,11 +706,30 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, * by AF_UNIX. */ __vsock_remove_bound(vsk); - __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk); + __vsock_insert_bound(&bind_table[VSOCK_HASH(&vsk->local_addr)], vsk); return 0; } +static int __vsock_bind_connectible(struct vsock_sock *vsk, + struct sockaddr_vm *addr) +{ + return vsock_bind_common(vsk, addr, vsock_bind_table, VSOCK_HASH_SIZE + 1); +} + +int vsock_bind_stream(struct vsock_sock *vsk, + struct sockaddr_vm *addr) +{ + int retval; + + spin_lock_bh(&vsock_table_lock); + retval = __vsock_bind_connectible(vsk, addr); + spin_unlock_bh(&vsock_table_lock); + + return retval; +} +EXPORT_SYMBOL(vsock_bind_stream); + static int __vsock_bind_dgram(struct vsock_sock *vsk, struct sockaddr_vm *addr) {