From patchwork Wed Jul 19 00:50: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: 122335 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2111588vqt; Tue, 18 Jul 2023 17:52:23 -0700 (PDT) X-Google-Smtp-Source: APBJJlFrXHlA+MZPfmoPeGjnmSGKaDVz4PQdYPw8GpDnbuWzCJqJKeJMOIChd8pTvylumEew1MsE X-Received: by 2002:a05:6a20:7291:b0:125:3445:8af0 with SMTP id o17-20020a056a20729100b0012534458af0mr851938pzk.7.1689727943440; Tue, 18 Jul 2023 17:52:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689727943; cv=none; d=google.com; s=arc-20160816; b=E8KRiPD0VEG6xaCHnloC8q5b5RGinP7QUMfM89W7GKH0SuxS2et6M49V/PlXGhrv+7 pq+2TA8BXP5/p4vObVZD9jJDAa/WL9LPUuo7d1esUirBwPSwWkqXAFBTGeNvj6kfWlRO V5rbIaC08tAdMJNuCpCaowSmibmQMRaP8VeMxZAwCzTHTV54gZNmYOUsIm3hdyS3W6ez A2rxFM5HcB95IlPn8jBttrcWklc2PdJP+p+nkmo1ExqsmC3M8wjCklcnZguRmiy2V50p qazyzbbWkxo3W6BcJdNs8ZDE624xd+eItxWLkJTX5atiFzp7/X0kZfXVprLBa8AUUtRn gVjg== 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=XE7xx9iLHROSi//2aw1ggMmi4lxgJG8eDx6KZfuDE2w=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=w0ATCcFHsPI+uKqbKyVJGc1ApT4/CAs9Wm2OiMpaIFKidNx3Y63GU4l7LXaCLqB8gH Fg3JVrCIOwqWOO9xrhewS/uaDH54SDHn2mQt7dngdX/9sKDke4Oatr8uzSs0hmJV+Udr Ti4cTTwjot+XHWJI4JZN5Lp2ag+JcubV4CAmDhbTkAJTtRbnGiUfC2We0eDmvCu2X0gJ bmXFKppNrWHG69ZoyphIQSYGgUWxOc7MJyMBuv5s5DmCFMWAkMNjsG2k/ORrh3T4XCU7 U3WuUUnD0l0YU0OuhNqVEvgWwsF4GZU4a8ZvuuwcwJH9vW1fZpDXmpkmAWHgNrnJDg0O f8gw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=jbFfw6yW; 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 j27-20020a633c1b000000b005574bef6f54si2345043pga.486.2023.07.18.17.52.10; Tue, 18 Jul 2023 17:52:23 -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=jbFfw6yW; 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 S230188AbjGSAvD (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46548 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229982AbjGSAun (ORCPT ); Tue, 18 Jul 2023 20:50:43 -0400 Received: from mail-qk1-x72a.google.com (mail-qk1-x72a.google.com [IPv6:2607:f8b0:4864:20::72a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A2C6198C for ; Tue, 18 Jul 2023 17:50:14 -0700 (PDT) Received: by mail-qk1-x72a.google.com with SMTP id af79cd13be357-765a311a7a9so15019485a.0 for ; Tue, 18 Jul 2023 17:50:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727813; x=1692319813; 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=XE7xx9iLHROSi//2aw1ggMmi4lxgJG8eDx6KZfuDE2w=; b=jbFfw6yWrqm8g/M0Mcm+rEYLs48/1lNS5DfgbjSPZLziZ1zzRSoFeF6UAVq0kp0nF5 Cs2hs25n+QV8K9l49XImvLmuUoDjQG6ucRk/M5Ys0kVrVbZ0dXRZ/9agC3Hj6z0TBJ+3 6D1woGsbITvafqZe8OjSVxhjUN6E5zS25TjNZo+AAOcg2Pqy6UQRL2n5J2y25oBX9mo6 /vaj5R627b1+5VJNai3wzRCLJPlv10FwPHVU6ohOnSErLHaWoVgFC7w+mQxHKT/2DOTL SWBf5dVuwcel47Dkk9qjotggT8IfTLddlywb9KQNSHIYUjxQUlMEe6GtCL58viKLFd+/ sUKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727813; x=1692319813; 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=XE7xx9iLHROSi//2aw1ggMmi4lxgJG8eDx6KZfuDE2w=; b=QA2R40CmY5T8rkOzPL2yLeJ8aCbZuogLT0z9OtAm7wf4TJPE7sEIgjK/fXLXGzMJO3 wxaoiaFp6ctItujTfJIcHBr4d5ad6r2HnLKNRdDCGp0gnCp+O08Pt3sjlDy/dLL00uVa rQVC7Isi7KKqe6foL/UcJwfPaBM7bSg7TM9Hxe9bQ3nqEbq15BirmgE8Pjjvx5Bi7pVC V51IWVN0kMnvx/LGcCzc/jELK6Eg9IK5Q4LJISIAuTBx8Ln1dX4tAupP1tNEf+ivMCsE STqmgNvGL/jUprY5+VeAUn8FcJ7y+WMPR1QEEyQ1lYatczH53MkKt1ARmhLKTziSzVOi SSRw== X-Gm-Message-State: ABy/qLa0Jv5feQ9hpA1bM8WanqXBGPQfO6Mya675sjZC4SaIGEZA0X6j FjlOcRIz64sst6eHOXICkiz3TQ== X-Received: by 2002:a05:620a:2489:b0:766:f9a5:c7ec with SMTP id i9-20020a05620a248900b00766f9a5c7ecmr810938qkn.18.1689727812816; Tue, 18 Jul 2023 17:50:12 -0700 (PDT) Received: from [172.17.0.7] ([130.44.212.112]) by smtp.gmail.com with ESMTPSA id c5-20020a05620a11a500b0076738337cd1sm968696qkk.1.2023.07.18.17.50.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:12 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:08 +0000 Subject: [PATCH RFC net-next v5 04/14] af_vsock: generalize bind table functions MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-4-581bd37fdb26@bytedance.com> References: <20230413-b4-vsock-dgram-v5-0-581bd37fdb26@bytedance.com> In-Reply-To: <20230413-b4-vsock-dgram-v5-0-581bd37fdb26@bytedance.com> To: Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , "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: Dan Carpenter , Simon Horman , Krasnov Arseniy , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, bpf@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=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: INBOX X-GMAIL-THRID: 1771808168152730412 X-GMAIL-MSGID: 1771808168152730412 This commit makes the bind table management functions in vsock usable for different bind tables. Future work will introduce a new table for datagrams to avoid address collisions, and these functions will be used there. Signed-off-by: Bobby Eshleman --- net/vmw_vsock/af_vsock.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 26c97b33d55a..88100154156c 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -231,11 +231,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) +static 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); @@ -248,6 +249,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) { @@ -647,12 +653,18 @@ static void vsock_pending_work(struct work_struct *work) /**** SOCKET OPERATIONS ****/ -static int __vsock_bind_connectible(struct vsock_sock *vsk, - struct sockaddr_vm *addr) +static 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 (WARN_ONCE(table_size < VSOCK_HASH_SIZE, + "table size too small, may cause overflow")) + return -EINVAL; + if (!port) port = get_random_u32_above(LAST_RESERVED_PORT); @@ -668,7 +680,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; } @@ -685,7 +698,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; } @@ -697,11 +711,17 @@ 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); +} + static int __vsock_bind_dgram(struct vsock_sock *vsk, struct sockaddr_vm *addr) {