From patchwork Wed Jul 19 00:50:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122348 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2120113vqt; Tue, 18 Jul 2023 18:13:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlGRHAeIXJitOfwzbTR5DqWv09oRaWjGWmxaO24xBiPalbYma3hcXrCfKYTechQgdIySH1wr X-Received: by 2002:a17:907:e8d:b0:992:345e:8319 with SMTP id ho13-20020a1709070e8d00b00992345e8319mr1033132ejc.58.1689729185468; Tue, 18 Jul 2023 18:13:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689729185; cv=none; d=google.com; s=arc-20160816; b=vPSnu8ESIAwlf3RYwctcRgaKEHlCLBdoqN5qktCR1BAmfrccbWTyuGn+cborMBMf0g GO/sRP/sRz3CKTgGRmgfh2aOzfT8nnTpjmQCYvkQEolFcshuqaOOcDWGrQbfVDYA9XLk ZZtteuefcH3m2ssrd8+lg8w+3ZJhaAboizrYwQp3OdOJK2YY7Ek6dSa69Ogc5MZb0dDv z2yDaV6BsXYncFBX5MAyEcuCmqXWEynQh6siecj0+QvrzX1ArXJHpCGZGaoQ1JsSGRzB gjJ6w5IqeFnan6AOkUrksb8n61wYWFt9sg4uYO2YpK7hMK0iNrIqrzex0KL5j9qIhRfz QBVw== 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=auk1noj/dkSN4CfNptro90m4Q3cm8nzxOnwzfPqqvIE=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=ilmwpxEUlic4UQdVrz7tdGJDJ/lhjrup227cRFPxEZ/0jSzLTBtTAkXsYdzGG3dq9a ysJB1ojQNjSj5uTxmxDonR5Nw4jwau/wwvUcIgLC6KEWeOhY6BTF3dWx13M1JBSwpqt3 tssJn/kRPzFv6jms4RTR9Wvh5WTWBok7NismDp/DLpcl1rDlUGSGZCanRyKW4zsHKJ+i 742te7hX5rNfBOsg81Zym1XlL+OWKs+LevCiH6YntO/oonYqGEFDx48cdlgQDDO39pet cFfK+0Giuv07EUdPdF+g23Eu2R6/gMnzQvZ8T7VpXdzgV4k/kD+Rq7DQsR4Q0IqeZZ+H TgBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=H1zk3nux; 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 lh17-20020a170906f8d100b00992f7e90360si2024357ejb.727.2023.07.18.18.12.41; Tue, 18 Jul 2023 18:13:05 -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=H1zk3nux; 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 S229966AbjGSAum (ORCPT + 99 others); Tue, 18 Jul 2023 20:50:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229817AbjGSAui (ORCPT ); Tue, 18 Jul 2023 20:50:38 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7394BE56 for ; Tue, 18 Jul 2023 17:50:11 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id af79cd13be357-767b6d6bb87so428666885a.2 for ; Tue, 18 Jul 2023 17:50:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727810; x=1692319810; 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=auk1noj/dkSN4CfNptro90m4Q3cm8nzxOnwzfPqqvIE=; b=H1zk3nuxyg/iVPscQ6kvOHZcLVvkmvByvhNGPcnnERpQDW3ulY0pxVN0ZrXu5S3b+2 j1JCBzzh5vpMURhvkS/g3fjwoaBGSvo1kv3JFrA+1w8OeG5NaX7U5Mram0Hte8iITWx8 t4ke42nGYb8fmflcJiDjmlgqu+r8XSCY3RtBdcNu4+CT4qG/X6aQB6h00ukR+7umTW/A RKkvlG1FsrBaO1iCCiUwxtLU8iapKgCKpI32fG01l3zeheqMbUe2FpxR7Xjq1fA1aQFi fhxVMATVH5sw+07Fapx85cmMsEbbhOL/upeEoRiw2yI+NKhw9RCX0KK2gz5qy2YoaLPR W3kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727810; x=1692319810; 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=auk1noj/dkSN4CfNptro90m4Q3cm8nzxOnwzfPqqvIE=; b=YTLh3rlD0ZsIj7UJVjkLMzdf6+bc+kXFo+G97+Ew/RPXZyuS3OGQduIQXE+6UvTnCW JvT3SCIliiL3pcfb97bJfcPXozRQNhYJ6IlPSG7W5KuWZCPW1a9oxEApo1N/ZGegLt1P qO79PI/yYfgrro44sLy63/cJF+pSqdwbbAI590kcF0p+7uiQgjjhKaZV5SSGVFaUEpgo FhJE0fhoTZSpIA/dGICKwZwCtVyXBkIKwero+u9hisVfLRx5kKTFa33nqNH+DuKrGFoq Yp7VQ1EEAtx4vkpk12FdtV+MQSsteq7YwNA+UE0QREjYl6QoC9rkZJ7I+etBluTCxcrI +Jww== X-Gm-Message-State: ABy/qLYBItU8YmH1I8WsXuXVm/wC9hOsXjWiBTSClL5fI01+9TbxSK7o 6Raqe4GN1OGu8E4ukyN4lCB6Ug== X-Received: by 2002:a05:620a:2848:b0:767:fbdc:5d49 with SMTP id h8-20020a05620a284800b00767fbdc5d49mr1327208qkp.70.1689727810255; Tue, 18 Jul 2023 17:50:10 -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.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:09 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:05 +0000 Subject: [PATCH RFC net-next v5 01/14] af_vsock: generalize vsock_dgram_recvmsg() to all transports MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-1-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_BLOCKED, 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: 1771809470410901841 X-GMAIL-MSGID: 1771809470410901841 This commit drops the transport->dgram_dequeue callback and makes vsock_dgram_recvmsg() generic to all transports. To make this possible, two transport-level changes are introduced: - implementation of the ->dgram_addr_init() callback to initialize the sockaddr_vm structure with data from incoming socket buffers. - transport implementations set the skb->data pointer to the beginning of the payload prior to adding the skb to the socket's receive queue. That is, they must use skb_pull() before enqueuing. This is an agreement between the transport and the socket layer that skb->data always points to the beginning of the payload (and not, for example, the packet header). Signed-off-by: Bobby Eshleman --- drivers/vhost/vsock.c | 1 - include/linux/virtio_vsock.h | 5 --- include/net/af_vsock.h | 3 +- net/vmw_vsock/af_vsock.c | 40 ++++++++++++++++++++++- net/vmw_vsock/hyperv_transport.c | 7 ---- net/vmw_vsock/virtio_transport.c | 1 - net/vmw_vsock/virtio_transport_common.c | 9 ----- net/vmw_vsock/vmci_transport.c | 58 ++++++--------------------------- net/vmw_vsock/vsock_loopback.c | 1 - 9 files changed, 50 insertions(+), 75 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 6578db78f0ae..ae8891598a48 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -410,7 +410,6 @@ static struct virtio_transport vhost_transport = { .cancel_pkt = vhost_transport_cancel_pkt, .dgram_enqueue = virtio_transport_dgram_enqueue, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_bind = virtio_transport_dgram_bind, .dgram_allow = virtio_transport_dgram_allow, diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index c58453699ee9..18cbe8d37fca 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -167,11 +167,6 @@ virtio_transport_stream_dequeue(struct vsock_sock *vsk, size_t len, int type); int -virtio_transport_dgram_dequeue(struct vsock_sock *vsk, - struct msghdr *msg, - size_t len, int flags); - -int virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk, struct msghdr *msg, size_t len); diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 0e7504a42925..305d57502e89 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -120,11 +120,10 @@ struct vsock_transport { /* DGRAM. */ int (*dgram_bind)(struct vsock_sock *, struct sockaddr_vm *); - int (*dgram_dequeue)(struct vsock_sock *vsk, struct msghdr *msg, - size_t len, int flags); int (*dgram_enqueue)(struct vsock_sock *, struct sockaddr_vm *, struct msghdr *, size_t len); bool (*dgram_allow)(u32 cid, u32 port); + void (*dgram_addr_init)(struct sk_buff *skb, struct sockaddr_vm *addr); /* STREAM. */ /* TODO: stream_bind() */ diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index deb72a8c44a7..ad71e084bf2f 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1272,11 +1272,15 @@ static int vsock_dgram_connect(struct socket *sock, int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, int flags) { + const struct vsock_transport *transport; #ifdef CONFIG_BPF_SYSCALL const struct proto *prot; #endif struct vsock_sock *vsk; + struct sk_buff *skb; + size_t payload_len; struct sock *sk; + int err; sk = sock->sk; vsk = vsock_sk(sk); @@ -1287,7 +1291,41 @@ int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, return prot->recvmsg(sk, msg, len, flags, NULL); #endif - return vsk->transport->dgram_dequeue(vsk, msg, len, flags); + if (flags & MSG_OOB || flags & MSG_ERRQUEUE) + return -EOPNOTSUPP; + + transport = vsk->transport; + + /* Retrieve the head sk_buff from the socket's receive queue. */ + err = 0; + skb = skb_recv_datagram(sk_vsock(vsk), flags, &err); + if (!skb) + return err; + + payload_len = skb->len; + + if (payload_len > len) { + payload_len = len; + msg->msg_flags |= MSG_TRUNC; + } + + /* Place the datagram payload in the user's iovec. */ + err = skb_copy_datagram_msg(skb, 0, msg, payload_len); + if (err) + goto out; + + if (msg->msg_name) { + /* Provide the address of the sender. */ + DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name); + + transport->dgram_addr_init(skb, vm_addr); + msg->msg_namelen = sizeof(*vm_addr); + } + err = payload_len; + +out: + skb_free_datagram(&vsk->sk, skb); + return err; } EXPORT_SYMBOL_GPL(vsock_dgram_recvmsg); diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c index 7cb1a9d2cdb4..7f1ea434656d 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -556,12 +556,6 @@ static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr) return -EOPNOTSUPP; } -static int hvs_dgram_dequeue(struct vsock_sock *vsk, struct msghdr *msg, - size_t len, int flags) -{ - return -EOPNOTSUPP; -} - static int hvs_dgram_enqueue(struct vsock_sock *vsk, struct sockaddr_vm *remote, struct msghdr *msg, size_t dgram_len) @@ -833,7 +827,6 @@ static struct vsock_transport hvs_transport = { .shutdown = hvs_shutdown, .dgram_bind = hvs_dgram_bind, - .dgram_dequeue = hvs_dgram_dequeue, .dgram_enqueue = hvs_dgram_enqueue, .dgram_allow = hvs_dgram_allow, diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index e95df847176b..66edffdbf303 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -429,7 +429,6 @@ static struct virtio_transport virtio_transport = { .cancel_pkt = virtio_transport_cancel_pkt, .dgram_bind = virtio_transport_dgram_bind, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow, diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index b769fc258931..01ea1402ad40 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -583,15 +583,6 @@ virtio_transport_seqpacket_enqueue(struct vsock_sock *vsk, } EXPORT_SYMBOL_GPL(virtio_transport_seqpacket_enqueue); -int -virtio_transport_dgram_dequeue(struct vsock_sock *vsk, - struct msghdr *msg, - size_t len, int flags) -{ - return -EOPNOTSUPP; -} -EXPORT_SYMBOL_GPL(virtio_transport_dgram_dequeue); - s64 virtio_transport_stream_has_data(struct vsock_sock *vsk) { struct virtio_vsock_sock *vvs = vsk->trans; diff --git a/net/vmw_vsock/vmci_transport.c b/net/vmw_vsock/vmci_transport.c index b370070194fa..0bbbdb222245 100644 --- a/net/vmw_vsock/vmci_transport.c +++ b/net/vmw_vsock/vmci_transport.c @@ -641,6 +641,7 @@ static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg) sock_hold(sk); skb_put(skb, size); memcpy(skb->data, dg, size); + skb_pull(skb, VMCI_DG_HEADERSIZE); sk_receive_skb(sk, skb, 0); return VMCI_SUCCESS; @@ -1731,57 +1732,18 @@ static int vmci_transport_dgram_enqueue( return err - sizeof(*dg); } -static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk, - struct msghdr *msg, size_t len, - int flags) +static void vmci_transport_dgram_addr_init(struct sk_buff *skb, + struct sockaddr_vm *addr) { - int err; struct vmci_datagram *dg; - size_t payload_len; - struct sk_buff *skb; - - if (flags & MSG_OOB || flags & MSG_ERRQUEUE) - return -EOPNOTSUPP; - - /* Retrieve the head sk_buff from the socket's receive queue. */ - err = 0; - skb = skb_recv_datagram(&vsk->sk, flags, &err); - if (!skb) - return err; - - dg = (struct vmci_datagram *)skb->data; - if (!dg) - /* err is 0, meaning we read zero bytes. */ - goto out; - - payload_len = dg->payload_size; - /* Ensure the sk_buff matches the payload size claimed in the packet. */ - if (payload_len != skb->len - sizeof(*dg)) { - err = -EINVAL; - goto out; - } - - if (payload_len > len) { - payload_len = len; - msg->msg_flags |= MSG_TRUNC; - } + unsigned int cid, port; - /* Place the datagram payload in the user's iovec. */ - err = skb_copy_datagram_msg(skb, sizeof(*dg), msg, payload_len); - if (err) - goto out; - - if (msg->msg_name) { - /* Provide the address of the sender. */ - DECLARE_SOCKADDR(struct sockaddr_vm *, vm_addr, msg->msg_name); - vsock_addr_init(vm_addr, dg->src.context, dg->src.resource); - msg->msg_namelen = sizeof(*vm_addr); - } - err = payload_len; + WARN_ONCE(skb->head == skb->data, "vmci vsock bug: bad dgram skb"); -out: - skb_free_datagram(&vsk->sk, skb); - return err; + dg = (struct vmci_datagram *)skb->head; + cid = dg->src.context; + port = dg->src.resource; + vsock_addr_init(addr, cid, port); } static bool vmci_transport_dgram_allow(u32 cid, u32 port) @@ -2040,9 +2002,9 @@ static struct vsock_transport vmci_transport = { .release = vmci_transport_release, .connect = vmci_transport_connect, .dgram_bind = vmci_transport_dgram_bind, - .dgram_dequeue = vmci_transport_dgram_dequeue, .dgram_enqueue = vmci_transport_dgram_enqueue, .dgram_allow = vmci_transport_dgram_allow, + .dgram_addr_init = vmci_transport_dgram_addr_init, .stream_dequeue = vmci_transport_stream_dequeue, .stream_enqueue = vmci_transport_stream_enqueue, .stream_has_data = vmci_transport_stream_has_data, diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 5c6360df1f31..2a59dd177c74 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -62,7 +62,6 @@ static struct virtio_transport loopback_transport = { .cancel_pkt = vsock_loopback_cancel_pkt, .dgram_bind = virtio_transport_dgram_bind, - .dgram_dequeue = virtio_transport_dgram_dequeue, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow, From patchwork Wed Jul 19 00:50:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122333 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2111498vqt; Tue, 18 Jul 2023 17:52:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlFlRhqsDlYRtTOwjIfzo9J9sKhn5/6uLa6w6Y0auCp+PpN5qfR176jfl52h9zfWdEiNRrmx X-Received: by 2002:a05:6808:200e:b0:3a4:2a13:71ca with SMTP id q14-20020a056808200e00b003a42a1371camr22650286oiw.16.1689727928781; Tue, 18 Jul 2023 17:52:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689727928; cv=none; d=google.com; s=arc-20160816; b=cDT060ZLuI1eSCB/rq+DrGn2AQY+2VJkuVBOCuR5Eh7KkAkoHjDVQwMkaBcJ/EMzcI ARGOKriZb5K8KI7vE8FV3dMTyyRnAJ1wrFOiHJyra8I8UbhtPAkrEQEA3zz4Y/fSMawq 90aBsCr3dIejleM9/GaHko8uhjqIZMbZ743wKpHRFzlNX2CSJBTEilLLZjLer0DMDFUa +8h51KRbjVAEIPFTxAHz8k1gyWi9DIoC+ylzQXqmoLI7qUTOz1qgkp4oqS6mdXhEXTl9 awtzqtIguEqOorwagnTWE4ptMjWbXZciWg4FKyoojkpaRY7f1MkgxK6xEMnhOmVug4g7 5zjQ== 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=RRgj32iUYcRPQa7+gTgJIjnpPYuGFjIY/Kz/A8nTowk=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=mTE3eLA1NNQB93KbHJb+hM3rXsvESsQY7c7KupSGvGURvEqJKf+Bd9/HSd56btskMh zbBrJ2T6ynJfukDN8+XgMGyBKm1uD0uCktk0GngHJVAoon4FtZSygzb++Ii/dun6+ZKL k+No7qiZHR2D/IM5TEME1qDnLRuQrAR3ia+pwc1IEdFOMqOszjJEJHVlFkoafnk2J0FJ xu9qGbl26tGChr6KzpgCkZiTt04oatobpYv5dxyvY1mS6QllRsVLUvuszCK0pqf8y+K1 eZnZKaF4fHDmmC/DsLgHsoyD80hxpQKyc2wPHKaTQEVW1vto9tjz8YI7M2OlnVwT59RV zAzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=IXZ5R7IW; 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 v2-20020a17090a898200b0025960d035c6si327878pjn.138.2023.07.18.17.51.55; Tue, 18 Jul 2023 17:52:08 -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=IXZ5R7IW; 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 S230057AbjGSAup (ORCPT + 99 others); Tue, 18 Jul 2023 20:50:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229809AbjGSAuj (ORCPT ); Tue, 18 Jul 2023 20:50:39 -0400 Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1289F171A for ; Tue, 18 Jul 2023 17:50:11 -0700 (PDT) Received: by mail-qt1-x829.google.com with SMTP id d75a77b69052e-4039f7e1d3aso51335861cf.0 for ; Tue, 18 Jul 2023 17:50:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727811; x=1692319811; 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=RRgj32iUYcRPQa7+gTgJIjnpPYuGFjIY/Kz/A8nTowk=; b=IXZ5R7IWCdcvitOGSq8k/EIMLrqwPMUB9D/Zi9EE+9sSo9Ibqe7l8BTzzztEfZzLWg B2G5CHGlUkVio9Re3XZr0aX1lfiEfU0+n5TSJiykLg5DMsjUemmLr97vWvbzPEH6VBPf wc6K6mPxmMC4VBezVPhsbsTL8crTwuA9GenLAUs2GbzMEAAnTjBwClQX40kOCEfno/vD rcs9gwkb9hLLHvZt5yWNJZejItpccGsrQ1MfVasQA+l2WX9ZDxvzqyQ5U2gycVRfZwT+ kyaQKcjHLuSQxTbn52xpRG8wpj3PWrLi+kBY7lEhlwtPa+rBSATIlhwiR2U+jI7y9txa 7ILA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727811; x=1692319811; 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=RRgj32iUYcRPQa7+gTgJIjnpPYuGFjIY/Kz/A8nTowk=; b=PTIG9LqEhAyuALFd/Sq4QDuFL3aISpVLAwJ+E5FfA1MpHshYooTpErF3ADlTnNmqxg 79/GydUjKf5c4q1qYIAv2ijMZ1GfM1ZLvyyEi8S4KJZu4A6nvQ1ohiY4vPr2DCK3SZIG nMS9QuOeLMSW34qI0iFQjwcpaaB3MCQkO+Mfzen84Ys9VusTR2Syl0R5YvSBnsu7lqUn iFH/oHnTO+0+ISKP7wp05h7bB0jfEOxXFvXB3OSyHx2W5kwMoTrHNh2l/lFWvJqmzE40 V4JwurtRPPeJ5tw+TgIjBHh/H4c3ukfnNS1/A+DnOF8mZsAXYFTw2zOSWLfzdaKAcjnm TlVg== X-Gm-Message-State: ABy/qLbYWIRWgPYtIWWkHyeF2wYbM67uQq3ZZqHYtfGHLiKFBrpopUoL dWZOGbjBixisWYQlKL6NWhr0eg== X-Received: by 2002:a05:622a:1788:b0:403:fe96:7af2 with SMTP id s8-20020a05622a178800b00403fe967af2mr2544945qtk.41.1689727811113; Tue, 18 Jul 2023 17:50:11 -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.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:10 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:06 +0000 Subject: [PATCH RFC net-next v5 02/14] af_vsock: refactor transport lookup code MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-2-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: 1771808152584583607 X-GMAIL-MSGID: 1771808152584583607 Introduce new reusable function vsock_connectible_lookup_transport() that performs the transport lookup logic. No functional change intended. Reviewed-by: Stefano Garzarella Signed-off-by: Bobby Eshleman --- net/vmw_vsock/af_vsock.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index ad71e084bf2f..ae5ac5531d96 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -423,6 +423,22 @@ static void vsock_deassign_transport(struct vsock_sock *vsk) vsk->transport = NULL; } +static const struct vsock_transport * +vsock_connectible_lookup_transport(unsigned int cid, __u8 flags) +{ + const struct vsock_transport *transport; + + if (vsock_use_local_transport(cid)) + transport = transport_local; + else if (cid <= VMADDR_CID_HOST || !transport_h2g || + (flags & VMADDR_FLAG_TO_HOST)) + transport = transport_g2h; + else + transport = transport_h2g; + + return transport; +} + /* Assign a transport to a socket and call the .init transport callback. * * Note: for connection oriented socket this must be called when vsk->remote_addr @@ -463,13 +479,8 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) break; case SOCK_STREAM: case SOCK_SEQPACKET: - if (vsock_use_local_transport(remote_cid)) - new_transport = transport_local; - else if (remote_cid <= VMADDR_CID_HOST || !transport_h2g || - (remote_flags & VMADDR_FLAG_TO_HOST)) - new_transport = transport_g2h; - else - new_transport = transport_h2g; + new_transport = vsock_connectible_lookup_transport(remote_cid, + remote_flags); break; default: return -ESOCKTNOSUPPORT; From patchwork Wed Jul 19 00:50:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122346 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2119671vqt; Tue, 18 Jul 2023 18:11:58 -0700 (PDT) X-Google-Smtp-Source: APBJJlGaYsJ8XBq0mWZKgRfdGCHlfM7b2jnYcNWyNgwP1mARLAI1lG1SdMLTSBw8C9VsaIjKLFXt X-Received: by 2002:a17:907:7d8a:b0:98d:5ae2:f1c with SMTP id oz10-20020a1709077d8a00b0098d5ae20f1cmr1240424ejc.34.1689729118468; Tue, 18 Jul 2023 18:11:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689729118; cv=none; d=google.com; s=arc-20160816; b=snEwb7W2qCmPp6nnK8p5MXxqx7k8NQD57SQKcysAw2uZ9K8NCTyDPqtXzqHr3l71ZW +9R0/7m5YSEpCHHCzw1JCBJYoKTk6AdRaXmH4/KPEL+7RLJha1qms6CVn/Rt/dEHXIjD fNaDTeBVQX9U5PwhSQakg29jXOVeTxg8L8Hxo0QcBnZbA5OANsIwKfVNgtPdk9I1/UY7 3Qo3hKW1+pBIt1eroONFACQEPD5AxMvWv3bpr/QWebhwMdhhhZYaVymdUrQ2uzRsFhi9 lEOBYOnLP6ZqsxMRTRsQBQIWCzc/A1TEEq78AG2PZCXY8G02gwQBWo9ch8BbvTW3ZiME bZYA== 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=fTdq57d4OfXfe2GYZwwn7VN2X/kQP6RwUR6JDFxPMEE=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=JvMzdE7+/6CJC9abRzK79mYHn5d2NnWc7bVfhJVxkw8QROzlypwqrzGOKz0rNFxlOV lsbyCal5NkrbsQUWTpHYB55JOVhIgr4aKaeAANSk7YvcauVVkIWw390XyPu2LfaAFf2E +luWKj6tCPScZDR5xENFm+zxWxzeVlM+LqocaPeIRcj4ik6Zb8ASVdtSt+rVNCfIuZfE vv46HsduZXDRsbGZVkjcV7uxn8XfRZ32fOV8lH9aXR+goz5glE2SES2ixbqrwsJYBLHl RRGRxkff7MDVew4L+3ja8qwqY0d4jAdPBxctfDyIVovoPRpCVQQ5DoutrfnM9NMgrhiy fN/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=CwqkbU54; 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 j2-20020a17090686c200b009930f844671si1937451ejy.963.2023.07.18.18.11.35; Tue, 18 Jul 2023 18:11:58 -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=CwqkbU54; 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 S229981AbjGSAuw (ORCPT + 99 others); Tue, 18 Jul 2023 20:50:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229825AbjGSAuk (ORCPT ); Tue, 18 Jul 2023 20:50:40 -0400 Received: from mail-qk1-x735.google.com (mail-qk1-x735.google.com [IPv6:2607:f8b0:4864:20::735]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14086194 for ; Tue, 18 Jul 2023 17:50:13 -0700 (PDT) Received: by mail-qk1-x735.google.com with SMTP id af79cd13be357-7673180224bso430068385a.0 for ; Tue, 18 Jul 2023 17:50:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727812; x=1692319812; 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=fTdq57d4OfXfe2GYZwwn7VN2X/kQP6RwUR6JDFxPMEE=; b=CwqkbU54GND2d0NRs1umFXWwgm1JIjPjAPvnk1NY83WcuEGBPYkuDQYXZFb7iBYMKA r3O8KmNKOULw1yvMv6pIMstrnYMj7AtDVb4D1Ob0dWGESArGh0wsM8p36Iq0Wib3Vfsa z9zbHt2B6ZRbhDvyRhpQhAsNDvr2JTcfC0Tv44w2+9lc9g6nb1WHD8/tLjH6mxq930JY QDLmg5IZchtTh3E0U79Zezbq6DpAQXO27VkJwNpECv4EmtcxWxYQj+GZNJSUmbsUrfuA M50BWMRsjrJ0p9mKh0R3elh37onXk8adWkQvdzGEvXgSaVvGXMPVmylx1mCqDObk9XFE y+pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727812; x=1692319812; 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=fTdq57d4OfXfe2GYZwwn7VN2X/kQP6RwUR6JDFxPMEE=; b=YP8O+hJqz2PbQJLZJazJE/ShSdOeSHASNo/Q6tMj30bQWaQdlKDw405p/n8uGpr3YO WfweU4bbk1xud42VO6ocVsPDhj/TtW3LEjoVMAqDc1vjpRmmFKYZBJEybd0IucCHHfEB mpd/fa27MXQwXQ3R+YK5z7WrGOp8Tt14RfTY1hfPwQ4QUq3Wlf2jIDwF+H04WgFUtoVP VhZRccI+AjZ02QwfPYmJAfV4LvXUqZPd1CkaBmBYHy4c6dkac6B/I1mkwaz3MYayruRd 1sNiX90Ig8lZFu2p65qtWS64B4TghSMQOg4q+qTyCkh+mG+b6JneoURm1meWervWHvbA jdTQ== X-Gm-Message-State: ABy/qLbIogIePpMs2W8ZiDpW0W6GBHvLzgj3HkZ2uwtGSyY3zscEpIAp JjicNMZ6kpzHHowRw6MGS8FJ9A== X-Received: by 2002:a05:620a:2902:b0:767:e73b:d051 with SMTP id m2-20020a05620a290200b00767e73bd051mr1699976qkp.32.1689727811917; Tue, 18 Jul 2023 17:50:11 -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.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:11 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:07 +0000 Subject: [PATCH RFC net-next v5 03/14] af_vsock: support multi-transport datagrams MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-3-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_BLOCKED, 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: 1771809400085931913 X-GMAIL-MSGID: 1771809400085931913 This patch adds support for multi-transport datagrams. This includes: - Per-packet lookup of transports when using sendto(sockaddr_vm) - Selecting H2G or G2H transport using VMADDR_FLAG_TO_HOST and CID in sockaddr_vm - rename VSOCK_TRANSPORT_F_DGRAM to VSOCK_TRANSPORT_F_DGRAM_FALLBACK - connect() now assigns the transport for (similar to connectible sockets) To preserve backwards compatibility with VMCI, some important changes are made. The "transport_dgram" / VSOCK_TRANSPORT_F_DGRAM is changed to be used for dgrams only if there is not yet a g2h or h2g transport that has been registered that can transmit the packet. If there is a g2h/h2g transport for that remote address, then that transport will be used and not "transport_dgram". This essentially makes "transport_dgram" a fallback transport for when h2g/g2h has not yet gone online, and so it is renamed "transport_dgram_fallback". VMCI implements this transport. The logic around "transport_dgram" needs to be retained to prevent breaking VMCI: 1) VMCI datagrams existed prior to h2g/g2h and so operate under a different paradigm. When the vmci transport comes online, it registers itself with the DGRAM feature, but not H2G/G2H. Only later when the transport has more information about its environment does it register H2G or G2H. In the case that a datagram socket is created after VSOCK_TRANSPORT_F_DGRAM registration but before G2H/H2G registration, the "transport_dgram" transport is the only registered transport and so needs to be used. 2) VMCI seems to require a special message be sent by the transport when a datagram socket calls bind(). Under the h2g/g2h model, the transport is selected using the remote_addr which is set by connect(). At bind time there is no remote_addr because often no connect() has been called yet: the transport is null. Therefore, with a null transport there doesn't seem to be any good way for a datagram socket to tell the VMCI transport that it has just had bind() called upon it. With the new fallback logic, after H2G/G2H comes online the socket layer will access the VMCI transport via transport_{h2g,g2h}. Prior to H2G/G2H coming online, the socket layer will access the VMCI transport via "transport_dgram_fallback". Only transports with a special datagram fallback use-case such as VMCI need to register VSOCK_TRANSPORT_F_DGRAM_FALLBACK. Signed-off-by: Bobby Eshleman --- drivers/vhost/vsock.c | 1 - include/linux/virtio_vsock.h | 2 -- include/net/af_vsock.h | 10 +++--- net/vmw_vsock/af_vsock.c | 64 ++++++++++++++++++++++++++------- net/vmw_vsock/hyperv_transport.c | 6 ---- net/vmw_vsock/virtio_transport.c | 1 - net/vmw_vsock/virtio_transport_common.c | 7 ---- net/vmw_vsock/vmci_transport.c | 2 +- net/vmw_vsock/vsock_loopback.c | 1 - 9 files changed, 58 insertions(+), 36 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index ae8891598a48..d5d6a3c3f273 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -410,7 +410,6 @@ static struct virtio_transport vhost_transport = { .cancel_pkt = vhost_transport_cancel_pkt, .dgram_enqueue = virtio_transport_dgram_enqueue, - .dgram_bind = virtio_transport_dgram_bind, .dgram_allow = virtio_transport_dgram_allow, .stream_enqueue = virtio_transport_stream_enqueue, diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 18cbe8d37fca..7632552bee58 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -211,8 +211,6 @@ void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val); u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk); bool virtio_transport_stream_is_active(struct vsock_sock *vsk); bool virtio_transport_stream_allow(u32 cid, u32 port); -int virtio_transport_dgram_bind(struct vsock_sock *vsk, - struct sockaddr_vm *addr); bool virtio_transport_dgram_allow(u32 cid, u32 port); int virtio_transport_connect(struct vsock_sock *vsk); diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 305d57502e89..f6a0ca9d7c3e 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -96,13 +96,13 @@ struct vsock_transport_send_notify_data { /* Transport features flags */ /* Transport provides host->guest communication */ -#define VSOCK_TRANSPORT_F_H2G 0x00000001 +#define VSOCK_TRANSPORT_F_H2G 0x00000001 /* Transport provides guest->host communication */ -#define VSOCK_TRANSPORT_F_G2H 0x00000002 -/* Transport provides DGRAM communication */ -#define VSOCK_TRANSPORT_F_DGRAM 0x00000004 +#define VSOCK_TRANSPORT_F_G2H 0x00000002 +/* Transport provides fallback for DGRAM communication */ +#define VSOCK_TRANSPORT_F_DGRAM_FALLBACK 0x00000004 /* Transport provides local (loopback) communication */ -#define VSOCK_TRANSPORT_F_LOCAL 0x00000008 +#define VSOCK_TRANSPORT_F_LOCAL 0x00000008 struct vsock_transport { struct module *module; diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index ae5ac5531d96..26c97b33d55a 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -139,8 +139,8 @@ struct proto vsock_proto = { static const struct vsock_transport *transport_h2g; /* Transport used for guest->host communication */ static const struct vsock_transport *transport_g2h; -/* Transport used for DGRAM communication */ -static const struct vsock_transport *transport_dgram; +/* Transport used as a fallback for DGRAM communication */ +static const struct vsock_transport *transport_dgram_fallback; /* Transport used for local communication */ static const struct vsock_transport *transport_local; static DEFINE_MUTEX(vsock_register_mutex); @@ -439,6 +439,18 @@ vsock_connectible_lookup_transport(unsigned int cid, __u8 flags) return transport; } +static const struct vsock_transport * +vsock_dgram_lookup_transport(unsigned int cid, __u8 flags) +{ + const struct vsock_transport *transport; + + transport = vsock_connectible_lookup_transport(cid, flags); + if (transport) + return transport; + + return transport_dgram_fallback; +} + /* Assign a transport to a socket and call the .init transport callback. * * Note: for connection oriented socket this must be called when vsk->remote_addr @@ -475,7 +487,8 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) switch (sk->sk_type) { case SOCK_DGRAM: - new_transport = transport_dgram; + new_transport = vsock_dgram_lookup_transport(remote_cid, + remote_flags); break; case SOCK_STREAM: case SOCK_SEQPACKET: @@ -692,6 +705,9 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, static int __vsock_bind_dgram(struct vsock_sock *vsk, struct sockaddr_vm *addr) { + if (!vsk->transport || !vsk->transport->dgram_bind) + return -EINVAL; + return vsk->transport->dgram_bind(vsk, addr); } @@ -1162,6 +1178,7 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, struct vsock_sock *vsk; struct sockaddr_vm *remote_addr; const struct vsock_transport *transport; + bool module_got = false; if (msg->msg_flags & MSG_OOB) return -EOPNOTSUPP; @@ -1173,19 +1190,34 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, lock_sock(sk); - transport = vsk->transport; - err = vsock_auto_bind(vsk); if (err) goto out; - /* If the provided message contains an address, use that. Otherwise * fall back on the socket's remote handle (if it has been connected). */ if (msg->msg_name && vsock_addr_cast(msg->msg_name, msg->msg_namelen, &remote_addr) == 0) { + transport = vsock_dgram_lookup_transport(remote_addr->svm_cid, + remote_addr->svm_flags); + if (!transport) { + err = -EINVAL; + goto out; + } + + if (!try_module_get(transport->module)) { + err = -ENODEV; + goto out; + } + + /* When looking up a transport dynamically and acquiring a + * reference on the module, we need to remember to release the + * reference later. + */ + module_got = true; + /* Ensure this address is of the right type and is a valid * destination. */ @@ -1200,6 +1232,7 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, } else if (sock->state == SS_CONNECTED) { remote_addr = &vsk->remote_addr; + transport = vsk->transport; if (remote_addr->svm_cid == VMADDR_CID_ANY) remote_addr->svm_cid = transport->get_local_cid(); @@ -1224,6 +1257,8 @@ static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg, err = transport->dgram_enqueue(vsk, remote_addr, msg, len); out: + if (module_got) + module_put(transport->module); release_sock(sk); return err; } @@ -1256,13 +1291,18 @@ static int vsock_dgram_connect(struct socket *sock, if (err) goto out; + memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr)); + + err = vsock_assign_transport(vsk, NULL); + if (err) + goto out; + if (!vsk->transport->dgram_allow(remote_addr->svm_cid, remote_addr->svm_port)) { err = -EINVAL; goto out; } - memcpy(&vsk->remote_addr, remote_addr, sizeof(vsk->remote_addr)); sock->state = SS_CONNECTED; /* sock map disallows redirection of non-TCP sockets with sk_state != @@ -2487,7 +2527,7 @@ int vsock_core_register(const struct vsock_transport *t, int features) t_h2g = transport_h2g; t_g2h = transport_g2h; - t_dgram = transport_dgram; + t_dgram = transport_dgram_fallback; t_local = transport_local; if (features & VSOCK_TRANSPORT_F_H2G) { @@ -2506,7 +2546,7 @@ int vsock_core_register(const struct vsock_transport *t, int features) t_g2h = t; } - if (features & VSOCK_TRANSPORT_F_DGRAM) { + if (features & VSOCK_TRANSPORT_F_DGRAM_FALLBACK) { if (t_dgram) { err = -EBUSY; goto err_busy; @@ -2524,7 +2564,7 @@ int vsock_core_register(const struct vsock_transport *t, int features) transport_h2g = t_h2g; transport_g2h = t_g2h; - transport_dgram = t_dgram; + transport_dgram_fallback = t_dgram; transport_local = t_local; err_busy: @@ -2543,8 +2583,8 @@ void vsock_core_unregister(const struct vsock_transport *t) if (transport_g2h == t) transport_g2h = NULL; - if (transport_dgram == t) - transport_dgram = NULL; + if (transport_dgram_fallback == t) + transport_dgram_fallback = NULL; if (transport_local == t) transport_local = NULL; diff --git a/net/vmw_vsock/hyperv_transport.c b/net/vmw_vsock/hyperv_transport.c index 7f1ea434656d..c29000f2612a 100644 --- a/net/vmw_vsock/hyperv_transport.c +++ b/net/vmw_vsock/hyperv_transport.c @@ -551,11 +551,6 @@ static void hvs_destruct(struct vsock_sock *vsk) kfree(hvs); } -static int hvs_dgram_bind(struct vsock_sock *vsk, struct sockaddr_vm *addr) -{ - return -EOPNOTSUPP; -} - static int hvs_dgram_enqueue(struct vsock_sock *vsk, struct sockaddr_vm *remote, struct msghdr *msg, size_t dgram_len) @@ -826,7 +821,6 @@ static struct vsock_transport hvs_transport = { .connect = hvs_connect, .shutdown = hvs_shutdown, - .dgram_bind = hvs_dgram_bind, .dgram_enqueue = hvs_dgram_enqueue, .dgram_allow = hvs_dgram_allow, diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index 66edffdbf303..ac2126c7dac5 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -428,7 +428,6 @@ static struct virtio_transport virtio_transport = { .shutdown = virtio_transport_shutdown, .cancel_pkt = virtio_transport_cancel_pkt, - .dgram_bind = virtio_transport_dgram_bind, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow, diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index 01ea1402ad40..ffcbdd77feaa 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -781,13 +781,6 @@ bool virtio_transport_stream_allow(u32 cid, u32 port) } EXPORT_SYMBOL_GPL(virtio_transport_stream_allow); -int virtio_transport_dgram_bind(struct vsock_sock *vsk, - struct sockaddr_vm *addr) -{ - return -EOPNOTSUPP; -} -EXPORT_SYMBOL_GPL(virtio_transport_dgram_bind); - bool virtio_transport_dgram_allow(u32 cid, u32 port) { return false; diff --git a/net/vmw_vsock/vmci_transport.c b/net/vmw_vsock/vmci_transport.c index 0bbbdb222245..857b0461f856 100644 --- a/net/vmw_vsock/vmci_transport.c +++ b/net/vmw_vsock/vmci_transport.c @@ -2072,7 +2072,7 @@ static int __init vmci_transport_init(void) /* Register only with dgram feature, other features (H2G, G2H) will be * registered when the first host or guest becomes active. */ - err = vsock_core_register(&vmci_transport, VSOCK_TRANSPORT_F_DGRAM); + err = vsock_core_register(&vmci_transport, VSOCK_TRANSPORT_F_DGRAM_FALLBACK); if (err < 0) goto err_unsubscribe; diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 2a59dd177c74..278235ea06c4 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -61,7 +61,6 @@ static struct virtio_transport loopback_transport = { .shutdown = virtio_transport_shutdown, .cancel_pkt = vsock_loopback_cancel_pkt, - .dgram_bind = virtio_transport_dgram_bind, .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow, 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) { From patchwork Wed Jul 19 00:50:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122341 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2117852vqt; Tue, 18 Jul 2023 18:07:41 -0700 (PDT) X-Google-Smtp-Source: APBJJlHhOeVBaAYBaMC4F3mqWYWX+hALAeGR2rpZTUI53+kbKY+dsGYtWWUqbmb4pu1ACia2+61z X-Received: by 2002:a5d:5151:0:b0:313:f1ec:f429 with SMTP id u17-20020a5d5151000000b00313f1ecf429mr14741226wrt.46.1689728861074; Tue, 18 Jul 2023 18:07:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689728861; cv=none; d=google.com; s=arc-20160816; b=UWvkxBq4w20SCr3HOBmPzr7NDIkRGiD3sAvFwiZ8slFyLWrRryPzpUkEUk8TAlAPRm 3n8LIec+QQxCQpCyEI8oswIXjs1buzcHlK8RvO88YXU86PmCnMdKQF740+WSIFrqyyqG bBPxyVuJWbMj8hL/QMkEYkGyN0WUm7+fmFcvVDsvVbqRupdgkinOk2LqgpBNKJY6NAB1 jn6SVaFWO5Ky94k5R/dqamwr0du9svZPuexU2Q7dHre0czIfx9VD6Um9ZBYYv0VHRw7l xBfMFZ3u9ihkrk5ukpYstSuZwg6SoB7p3pCar2OKJpGhByoyYupKjQqD6JdSLEIt08GH 1PgA== 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=YVG6/fd8/TheeZKmbnuDQmZdww8fyIwE/W3lpq/H1bc=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=IwDxRKX2nfKMEMR8EqEd+9JuDrqYmpYF6tCTPaNdSwYj+DPoTrCRrxZnNHD1wETjv5 fuxYXqlv1LwNg7pS5+Nsgd7ZJou93gPUZT/UZLW/vsYfxc5WgWGSgQ3rXTdRAxIR/oVZ dOHsMNVJz4ZmwCfmJhXW+azeIBPKgK6uK9oZ76jkt43Q+fYWY8RDEKLXZEgR/M06S/C3 B3msUsKNhOPF4bvW1RkgcC+wJmlDcat4XGHX52ivZkaIP7CjBc5go19B6WPPDsS9hKo3 uI5eGlX8vBzVskxOEdPYD8Gfm1xbmyOe+k167Tqj6DTjwNgS2hto9UgWk0fKcWOdpaTG mVLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=M1eAJpu3; 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 q8-20020aa7d448000000b0051da5244b7bsi2057536edr.469.2023.07.18.18.07.17; Tue, 18 Jul 2023 18:07:41 -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=M1eAJpu3; 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 S230290AbjGSAvU (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229977AbjGSAvA (ORCPT ); Tue, 18 Jul 2023 20:51:00 -0400 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FFDE1995 for ; Tue, 18 Jul 2023 17:50:14 -0700 (PDT) Received: by mail-qk1-x734.google.com with SMTP id af79cd13be357-7679d444210so521273485a.2 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=YVG6/fd8/TheeZKmbnuDQmZdww8fyIwE/W3lpq/H1bc=; b=M1eAJpu3CrxziEpHRP2XtbX8TUaYeVu0Z7OFMz3BDmc+5Zu6FVi4kVgFeg0IMOBIuK CbMVnZLS9QwWEcl2SBAlk6NmUL7L2NI5FNnpakeq9h2XDhWrIj91ZkPueFOVWfrnPRj7 3OCUpGxk/hRy2rDb1B6+dyRg1zS+an1cw2SxJJAU30HWMiSrwRDCrU+KAFJDSH0+VmLd PNKfLsyqBT491jiB8zcaZRd6P37+KFR2uOeseT5s+z8dYLH6G7S5tM8Muxl1fR1lzZbQ EKSEQfb1GYzVISzT3tiPw3q9u4FdlVzQ5uihYjuK3rqMwiT5cEbfDayk24sCb0Ct/mD8 UncA== 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=YVG6/fd8/TheeZKmbnuDQmZdww8fyIwE/W3lpq/H1bc=; b=T4ySjq1xw+HUZlzLAlwO9ad5Em137fMQHcibBuwodXk4eMgl5LHS388LHWXCWt+RvJ LxvRI19Jm6fYm1lGjJbsuVWWNky6gfKLtVb/v8LYkQCLGhvxzLlif2zKRdvi4Rs9TnVX ZQCFLqfw4TdG59Wg0oFqV/IUsQbRSYnWUfPgZDad2XLpPm2AArDkiiYRoR+vg11Jn5rH PcYYUxQc/lS4FJyzLVPLWnNKO5Ns5rBkFdMsNn8EtZy8YtOe0mUaShmeuxaHnPDgW4NE 7mwBFkHe3wKofUL03pR6VJWn900Dx9h9PghQddIoerUVHkv97n4xI/74SPj2klBorqB9 ml3A== X-Gm-Message-State: ABy/qLaq1w8TuSx0S9wCrVY/V9nY0xQBJ6GHp0DA6izB/0brqoIot2q/ Ht9VVL2QJrky5sQXglsjfkLVuw== X-Received: by 2002:a05:620a:45ab:b0:766:fa7b:8b20 with SMTP id bp43-20020a05620a45ab00b00766fa7b8b20mr868491qkb.50.1689727813660; Tue, 18 Jul 2023 17:50:13 -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:13 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:09 +0000 Subject: [PATCH RFC net-next v5 05/14] af_vsock: use a separate dgram bind table MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-5-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: 1771809130038259570 X-GMAIL-MSGID: 1771809130038259570 This commit adds support for bound dgram sockets to be tracked in a separate bind table from connectible sockets in order to avoid address collisions. With this commit, users can simultaneously bind a dgram socket and connectible socket to the same CID and port. Signed-off-by: Bobby Eshleman --- net/vmw_vsock/af_vsock.c | 103 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 27 deletions(-) diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 88100154156c..0895f4c1d340 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -10,18 +10,23 @@ * - There are two kinds of sockets: those created by user action (such as * calling socket(2)) and those created by incoming connection request packets. * - * - There are two "global" tables, one for bound sockets (sockets that have - * specified an address that they are responsible for) and one for connected - * sockets (sockets that have established a connection with another socket). - * These tables are "global" in that all sockets on the system are placed - * within them. - Note, though, that the bound table contains an extra entry - * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in - * that list. The bound table is used solely for lookup of sockets when packets - * are received and that's not necessary for SOCK_DGRAM sockets since we create - * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM - * sockets out of the bound hash buckets will reduce the chance of collisions - * when looking for SOCK_STREAM sockets and prevents us from having to check the - * socket type in the hash table lookups. + * - There are three "global" tables, one for bound connectible (stream / + * seqpacket) sockets, one for bound datagram sockets, and one for connected + * sockets. Bound sockets are sockets that have specified an address that + * they are responsible for. Connected sockets are sockets that have + * established a connection with another socket. These tables are "global" in + * that all sockets on the system are placed within them. - Note, though, + * that the bound tables contain an extra entry for a list of unbound + * sockets. The bound tables are used solely for lookup of sockets when packets + * are received. + * + * - There are separate bind tables for connectible and datagram sockets to avoid + * address collisions between stream/seqpacket sockets and datagram sockets. + * + * - Transports may elect to NOT use the global datagram bind table by + * implementing the ->dgram_bind() callback. If that callback is implemented, + * the global bind table is not used and the responsibility of bound datagram + * socket tracking is deferred to the transport. * * - Sockets created by user action will either be "client" sockets that * initiate a connection or "server" sockets that listen for connections; we do @@ -115,6 +120,7 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr); static void vsock_sk_destruct(struct sock *sk); static int vsock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); +static bool sock_type_connectible(u16 type); /* Protocol family. */ struct proto vsock_proto = { @@ -151,21 +157,25 @@ static DEFINE_MUTEX(vsock_register_mutex); * VSocket is stored in the connected hash table. * * Unbound sockets are all put on the same list attached to the end of the hash - * table (vsock_unbound_sockets). Bound sockets are added to the hash table in - * the bucket that their local address hashes to (vsock_bound_sockets(addr) - * represents the list that addr hashes to). + * tables (vsock_unbound_sockets/vsock_unbound_dgram_sockets). Bound sockets + * are added to the hash table in the bucket that their local address hashes to + * (vsock_bound_sockets(addr) and vsock_bound_dgram_sockets(addr) represents + * the list that addr hashes to). * - * Specifically, we initialize the vsock_bind_table array to a size of - * VSOCK_HASH_SIZE + 1 so that vsock_bind_table[0] through - * vsock_bind_table[VSOCK_HASH_SIZE - 1] are for bound sockets and - * vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. The hash function - * mods with VSOCK_HASH_SIZE to ensure this. + * Specifically, taking connectible sockets as an example we initialize the + * vsock_bind_table array to a size of VSOCK_HASH_SIZE + 1 so that + * vsock_bind_table[0] through vsock_bind_table[VSOCK_HASH_SIZE - 1] are for + * bound sockets and vsock_bind_table[VSOCK_HASH_SIZE] is for unbound sockets. + * The hash function mods with VSOCK_HASH_SIZE to ensure this. + * Datagrams and vsock_dgram_bind_table operate in the same way. */ #define MAX_PORT_RETRIES 24 #define VSOCK_HASH(addr) ((addr)->svm_port % VSOCK_HASH_SIZE) #define vsock_bound_sockets(addr) (&vsock_bind_table[VSOCK_HASH(addr)]) +#define vsock_bound_dgram_sockets(addr) (&vsock_dgram_bind_table[VSOCK_HASH(addr)]) #define vsock_unbound_sockets (&vsock_bind_table[VSOCK_HASH_SIZE]) +#define vsock_unbound_dgram_sockets (&vsock_dgram_bind_table[VSOCK_HASH_SIZE]) /* XXX This can probably be implemented in a better way. */ #define VSOCK_CONN_HASH(src, dst) \ @@ -181,6 +191,8 @@ struct list_head vsock_connected_table[VSOCK_HASH_SIZE]; EXPORT_SYMBOL_GPL(vsock_connected_table); DEFINE_SPINLOCK(vsock_table_lock); EXPORT_SYMBOL_GPL(vsock_table_lock); +static struct list_head vsock_dgram_bind_table[VSOCK_HASH_SIZE + 1]; +static DEFINE_SPINLOCK(vsock_dgram_table_lock); /* Autobind this socket to the local address if necessary. */ static int vsock_auto_bind(struct vsock_sock *vsk) @@ -203,6 +215,9 @@ static void vsock_init_tables(void) for (i = 0; i < ARRAY_SIZE(vsock_connected_table); i++) INIT_LIST_HEAD(&vsock_connected_table[i]); + + for (i = 0; i < ARRAY_SIZE(vsock_dgram_bind_table); i++) + INIT_LIST_HEAD(&vsock_dgram_bind_table[i]); } static void __vsock_insert_bound(struct list_head *list, @@ -270,13 +285,28 @@ static struct sock *__vsock_find_connected_socket(struct sockaddr_vm *src, return NULL; } -static void vsock_insert_unbound(struct vsock_sock *vsk) +static void __vsock_insert_dgram_unbound(struct vsock_sock *vsk) +{ + spin_lock_bh(&vsock_dgram_table_lock); + __vsock_insert_bound(vsock_unbound_dgram_sockets, vsk); + spin_unlock_bh(&vsock_dgram_table_lock); +} + +static void __vsock_insert_connectible_unbound(struct vsock_sock *vsk) { spin_lock_bh(&vsock_table_lock); __vsock_insert_bound(vsock_unbound_sockets, vsk); spin_unlock_bh(&vsock_table_lock); } +static void vsock_insert_unbound(struct vsock_sock *vsk) +{ + if (sock_type_connectible(sk_vsock(vsk)->sk_type)) + __vsock_insert_connectible_unbound(vsk); + else + __vsock_insert_dgram_unbound(vsk); +} + void vsock_insert_connected(struct vsock_sock *vsk) { struct list_head *list = vsock_connected_sockets( @@ -288,6 +318,14 @@ void vsock_insert_connected(struct vsock_sock *vsk) } EXPORT_SYMBOL_GPL(vsock_insert_connected); +static void vsock_remove_dgram_bound(struct vsock_sock *vsk) +{ + spin_lock_bh(&vsock_dgram_table_lock); + if (__vsock_in_bound_table(vsk)) + __vsock_remove_bound(vsk); + spin_unlock_bh(&vsock_dgram_table_lock); +} + void vsock_remove_bound(struct vsock_sock *vsk) { spin_lock_bh(&vsock_table_lock); @@ -339,7 +377,10 @@ EXPORT_SYMBOL_GPL(vsock_find_connected_socket); void vsock_remove_sock(struct vsock_sock *vsk) { - vsock_remove_bound(vsk); + if (sock_type_connectible(sk_vsock(vsk)->sk_type)) + vsock_remove_bound(vsk); + else + vsock_remove_dgram_bound(vsk); vsock_remove_connected(vsk); } EXPORT_SYMBOL_GPL(vsock_remove_sock); @@ -722,11 +763,19 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, 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) +static int vsock_bind_dgram(struct vsock_sock *vsk, + struct sockaddr_vm *addr) { - if (!vsk->transport || !vsk->transport->dgram_bind) - return -EINVAL; + if (!vsk->transport || !vsk->transport->dgram_bind) { + int retval; + + spin_lock_bh(&vsock_dgram_table_lock); + retval = vsock_bind_common(vsk, addr, vsock_dgram_bind_table, + VSOCK_HASH_SIZE); + spin_unlock_bh(&vsock_dgram_table_lock); + + return retval; + } return vsk->transport->dgram_bind(vsk, addr); } @@ -757,7 +806,7 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) break; case SOCK_DGRAM: - retval = __vsock_bind_dgram(vsk, addr); + retval = vsock_bind_dgram(vsk, addr); break; default: From patchwork Wed Jul 19 00:50:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122342 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2118848vqt; Tue, 18 Jul 2023 18:10:17 -0700 (PDT) X-Google-Smtp-Source: APBJJlFO5JMiNS6e4ZYzD0UAKhuW9tTlwoceb0hiv6C/RxwepMrJAgOxHU6INXsyZqmFPBcgznQB X-Received: by 2002:a05:6402:691:b0:51e:45b8:b0bb with SMTP id f17-20020a056402069100b0051e45b8b0bbmr1149262edy.22.1689729016765; Tue, 18 Jul 2023 18:10:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689729016; cv=none; d=google.com; s=arc-20160816; b=Yi9cyM+WrFgUZgrKjjOOAqa+FKoMvwZ38J3oPhLW4ix7TOsnd66z8T9N4C4AaZgszy rXx/WheUjxKNWLygbspZoC9SBVKvstQ5anJmAYD3Eo1hlfoFXbQaWaPVVaj8DpAjd2i3 qh2gYUrG6DYSjhcfPnrGokCi2bqDpTTGfSwb1xpyrkNPxc3AYFPmbDcTEvVd9VZPnfjC JGyM+TZtqeNw2XcExFC9ufHE5Dt6svcQ/myD5N7FoeaGSvrJLjW1RRXQiiqrcvjIXLw+ 4Qtkpt6rk9G63grwW5aRZUW1EyQGZubdihaOp5kiCq/MSkWdWsqKjSDydQvVKi0JngSr wJ6Q== 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=p5UDtnqRbXOwSk4kv7KpsqiuRSfPeujQxTt7caqoeag=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=zv9eUuFpjJiRUCef/w+aZDhzb+sP4iNNgj9ll5XWa3HekAYrBiB0mCRqUXfLQ8Hj9F mYM+8K0OY5IPrj0pL9fj00jWKof8b4x21elYNPBL+dgVz1Yak/akpCyoJ7Br8zYVMxyv 0pPRDCSqJLx4/Nw4T56ODznH7PTMejXKZ4W80K+ZueHZN3igSHSXgv7MOmxpbEs+vqOA 3fw+1k8LIyITquBUlnPtUXQrhCi4eGfM6rBBC11jZTIWmlZ9lEq9VHI8FtcTdGCix6ky ZSQ5irmxWovauU4Ej6AD3jtp8BA03HiCN+W0pUJCLteNTcUB4mL3vL8qwdN33Y79Kgdd SFxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Bzc5QYph; 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 by25-20020a0564021b1900b0052193eca238si2143696edb.139.2023.07.18.18.09.53; Tue, 18 Jul 2023 18:10:16 -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=Bzc5QYph; 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 S229919AbjGSAuk (ORCPT + 99 others); Tue, 18 Jul 2023 20:50:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbjGSAui (ORCPT ); Tue, 18 Jul 2023 20:50:38 -0400 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D5D519A9 for ; Tue, 18 Jul 2023 17:50:15 -0700 (PDT) Received: by mail-qk1-x734.google.com with SMTP id af79cd13be357-7680e39103bso364058685a.3 for ; Tue, 18 Jul 2023 17:50:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727814; x=1692319814; 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=p5UDtnqRbXOwSk4kv7KpsqiuRSfPeujQxTt7caqoeag=; b=Bzc5QYphkJxBJAAVGcKxoftfnIy9ixoapstjfZaDHt+8ignmoEtHuP1QMFfV/NgdZv 3sj5ZjysfQgDx4p0HKOtFfmPSLhf8q9ZrgG0m5jUcvca0sYjsElAG49iWpsKB4o7xNlw uAQ40QIRMt+3ilnWtx70o1O4HaBVrlCrik+wNvo96xyR4+3Q4cUVkFT5sX2tg+JhlBe+ hSQ//wu9r44PASXmqG6/Xmmv+nIq7JhrBez2ZRRnULIsZkr3/pE+EpOBNqOpyyWWX28B g5owX/BThJ1OiSg+ZKlYhgtlbmLJZLHH8juDYc38OwYMTHaeoKsA4gvu2ohtJ4f634Uk w1RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727814; x=1692319814; 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=p5UDtnqRbXOwSk4kv7KpsqiuRSfPeujQxTt7caqoeag=; b=IpUCNJqOTZUsGzedeaI/YhBYAuwSqr4o/sJ8awvGioEjsswsoODdmz1ZqxzGwDJ4+M uVczJC3oTiKuDPClHfvofQ+IymbZ2r5kVUkVp67Nw/KfJwtekujYgnP25DpC6U8UBza8 qA+RGC4R3bMVl9nkpYwN5pgXIeOQGLTpT7MFufU5GelOECyplS0dnjIqrmKhTHZeFgkJ fjKzJ1AZozIe+URZ+bwGui9fN14jsm086h0FES1lXyPVsWvMy9xmkKhcUkH7EEQjS41p zKW4E1Sn5jd91J8G26ebGKaL0G7Uj2z5qCA/b2KosfAwE/rr0eBaLiRnTfs87Z7aAU5A W75Q== X-Gm-Message-State: ABy/qLYY9+GET5nWUzm0stXR8hMhiZgbyF9BNefYTRSl8IW6tReMM66v EZ6Y0diZdVyFS9q964EuHtbr2Q== X-Received: by 2002:a05:620a:400b:b0:767:2a7e:1dbc with SMTP id h11-20020a05620a400b00b007672a7e1dbcmr1924213qko.17.1689727814453; Tue, 18 Jul 2023 17:50:14 -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.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:14 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:10 +0000 Subject: [PATCH RFC net-next v5 06/14] virtio/vsock: add VIRTIO_VSOCK_TYPE_DGRAM MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-6-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: 1771809293389909317 X-GMAIL-MSGID: 1771809293389909317 This commit adds the datagram packet type for inclusion in virtio vsock packet headers. It is included here as a standalone commit because multiple future but distinct commits depend on it. Signed-off-by: Bobby Eshleman --- include/uapi/linux/virtio_vsock.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h index 64738838bee5..331be28b1d30 100644 --- a/include/uapi/linux/virtio_vsock.h +++ b/include/uapi/linux/virtio_vsock.h @@ -69,6 +69,7 @@ struct virtio_vsock_hdr { enum virtio_vsock_type { VIRTIO_VSOCK_TYPE_STREAM = 1, VIRTIO_VSOCK_TYPE_SEQPACKET = 2, + VIRTIO_VSOCK_TYPE_DGRAM = 3, }; enum virtio_vsock_op { From patchwork Wed Jul 19 00:50:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122337 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2111808vqt; Tue, 18 Jul 2023 17:53:04 -0700 (PDT) X-Google-Smtp-Source: APBJJlFp5SXX5VprvhsygJBJlEX2/YxAaK2DInR+aGB6noGDeqggl6hWWnm6kUXk/5lDcJDYzubi X-Received: by 2002:a17:90b:3689:b0:263:312:611f with SMTP id mj9-20020a17090b368900b002630312611fmr14212817pjb.3.1689727983666; Tue, 18 Jul 2023 17:53:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689727983; cv=none; d=google.com; s=arc-20160816; b=wZsVeE8pMYkuLPYdpgJitBvzgQuKQDLGV5GY+RqV1AkzKb8XTrhjBpWbGQ2tBnZAV4 UWBjj7iPDaryu0ag7+PPvLrR2RxkVIHxf1OmsGE5MJVVRZL/T3rCuKiTypdw0of7zXn1 p2hWInd2tH1aM8DAu+QefqRX6a5fYIkj9DlyzK0hUZX9rlREYaTbm3DX55gAX3w0xw+T Q8Ws0k+CIdX0GRLgvEqN3hvzduFcYNKjVeecu1IGEKjCdOMXsTNEc4E/8osXwuA1nnSY L8DZA3DvTb9fFT5hOJEocDqQ4OS3yfvRIfW+oKWQf/rVl2Bu2y8IDpEAjL8bs50VixzH W/aQ== 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=obFj4SuWwNEDydCXYPra7zizeyW2jR3T9h3NMhd9EHc=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=OGXxt3txtrTyre/QwvIMFZ7zVoje/rF18YH8cRjzUQTP/uaVBRdHns4qBqDLK1MMaw NsMC7lSBNO9MKeCCzHyAdz0qWO99lac6PQ369gQg2tL2yWYV2xTEyw3VAuOIdSF6kS0f +9mrT4/CU9JqMrJr8Sn9101cwSv7oQx6Xcetv3oca5MU2h3O1lxc/WEhHvQ26x2t0Ir5 dd1myQTbBwWIU2NToxDGBI+CzhMxsgYykY7UmNEL6lQiHIEVz99c3xq7tw4iF5yPZgoZ G+qFZREfpZeL/VT7Fu4yQrAaweCTDo0bgFjS8mgpKo/GqOvcagFBwCj8e41zwTngUr8p PfCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=NUyO+OfS; 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 82-20020a630155000000b0055c8f4a56d5si2343609pgb.600.2023.07.18.17.52.50; Tue, 18 Jul 2023 17:53:03 -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=NUyO+OfS; 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 S230259AbjGSAvS (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229823AbjGSAvA (ORCPT ); Tue, 18 Jul 2023 20:51:00 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C66B19AD for ; Tue, 18 Jul 2023 17:50:16 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id af79cd13be357-7679ea01e16so574801485a.2 for ; Tue, 18 Jul 2023 17:50:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727815; x=1692319815; 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=obFj4SuWwNEDydCXYPra7zizeyW2jR3T9h3NMhd9EHc=; b=NUyO+OfSBwygJujqqAlusnhc8WL36GpDGlOSMNLrUMALVwy9C+eLf/gmADp5e2Cvdl RoxYMjnlm9to1SPFtrXu1eRiaDKIaHRn7U5Gw43KRFon2ohnUGIbnHrzI+y6hPywKwij PiwDJMc0PYE43Lp01hG48NJP7F9ojPjgzzPHD3GdMUhU4sCPOclubU6J2xW3pyzue4rk 7dMq/WYhfHxufcyjzThwQER4VlREp4hqAwyKaHW090tW2j8ZZ0/TwaXZNxzIJ7uHUpR8 wDT/venpG53iqv+C/PcE08F+s25Nk5YqUK+0bFTbp2Zw7AX18c2/2bHEiJ+3+x2r4T3m iRbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727815; x=1692319815; 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=obFj4SuWwNEDydCXYPra7zizeyW2jR3T9h3NMhd9EHc=; b=bOspej9igf1ltNkKUkZL0yGWuvYmyoRr01JOFhOAs5vwi0eZfKOqzYJoESRzAzfoae 28OAIn41lvNTxPVk6pJ1yL4n21yWsNTdqe0xHeZwsoKrry441N7SJm+R2+H/qtwe0qkh rd/XwLbeKzZxER/Kz++Ed41h5LEtbR6p76qa7d4pUZWLCq5fxr7nqGE8KgAKCguYjLYu k/189w+Be86P5zjQ2jZiMFgSJbjhCcEVdSI4cOn8bk8bD8xZS3rQmhsAVhz8wdwWAlLe JEKJH6pMRD8zUdcNCIQNPjk9+2kczqvyEw5Ag/89tdk1QeasW95w+Qp2DoTe/oGwRaCE q30w== X-Gm-Message-State: ABy/qLYe5UTmz2LJlRbZ/P+7BKnx80E6lGyF5QCn2WDZLrzPMq3ez8cK QfJNYwhF/HA5ORykahk46fbLng== X-Received: by 2002:a05:620a:2686:b0:767:32cd:5fe8 with SMTP id c6-20020a05620a268600b0076732cd5fe8mr19266292qkp.14.1689727815276; Tue, 18 Jul 2023 17:50:15 -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.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:14 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:11 +0000 Subject: [PATCH RFC net-next v5 07/14] virtio/vsock: add common datagram send path MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-7-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=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: INBOX X-GMAIL-THRID: 1771808210191710657 X-GMAIL-MSGID: 1771808210191710657 This commit implements the common function virtio_transport_dgram_enqueue for enqueueing datagrams. It does not add usage in either vhost or virtio yet. Signed-off-by: Bobby Eshleman --- net/vmw_vsock/virtio_transport_common.c | 76 ++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index ffcbdd77feaa..3bfaff758433 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -819,7 +819,81 @@ virtio_transport_dgram_enqueue(struct vsock_sock *vsk, struct msghdr *msg, size_t dgram_len) { - return -EOPNOTSUPP; + /* Here we are only using the info struct to retain style uniformity + * and to ease future refactoring and merging. + */ + struct virtio_vsock_pkt_info info_stack = { + .op = VIRTIO_VSOCK_OP_RW, + .msg = msg, + .vsk = vsk, + .type = VIRTIO_VSOCK_TYPE_DGRAM, + }; + const struct virtio_transport *t_ops; + struct virtio_vsock_pkt_info *info; + struct sock *sk = sk_vsock(vsk); + struct virtio_vsock_hdr *hdr; + u32 src_cid, src_port; + struct sk_buff *skb; + void *payload; + int noblock; + int err; + + info = &info_stack; + + if (dgram_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) + return -EMSGSIZE; + + t_ops = virtio_transport_get_ops(vsk); + if (unlikely(!t_ops)) + return -EFAULT; + + /* Unlike some of our other sending functions, this function is not + * intended for use without a msghdr. + */ + if (WARN_ONCE(!msg, "vsock dgram bug: no msghdr found for dgram enqueue\n")) + return -EFAULT; + + noblock = msg->msg_flags & MSG_DONTWAIT; + + /* Use sock_alloc_send_skb to throttle by sk_sndbuf. This helps avoid + * triggering the OOM. + */ + skb = sock_alloc_send_skb(sk, dgram_len + VIRTIO_VSOCK_SKB_HEADROOM, + noblock, &err); + if (!skb) + return err; + + skb_reserve(skb, VIRTIO_VSOCK_SKB_HEADROOM); + + src_cid = t_ops->transport.get_local_cid(); + src_port = vsk->local_addr.svm_port; + + hdr = virtio_vsock_hdr(skb); + hdr->type = cpu_to_le16(info->type); + hdr->op = cpu_to_le16(info->op); + hdr->src_cid = cpu_to_le64(src_cid); + hdr->dst_cid = cpu_to_le64(remote_addr->svm_cid); + hdr->src_port = cpu_to_le32(src_port); + hdr->dst_port = cpu_to_le32(remote_addr->svm_port); + hdr->flags = cpu_to_le32(info->flags); + hdr->len = cpu_to_le32(dgram_len); + + skb_set_owner_w(skb, sk); + + payload = skb_put(skb, dgram_len); + err = memcpy_from_msg(payload, msg, dgram_len); + if (err) + return err; + + trace_virtio_transport_alloc_pkt(src_cid, src_port, + remote_addr->svm_cid, + remote_addr->svm_port, + dgram_len, + info->type, + info->op, + 0); + + return t_ops->send_pkt(skb); } EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue); From patchwork Wed Jul 19 00:50:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122336 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2111772vqt; Tue, 18 Jul 2023 17:52:58 -0700 (PDT) X-Google-Smtp-Source: APBJJlFrua9dtRWubU0s+cj5nnsh0rgSwIsVE2ivFZcd04YljeOZQ1pR+aMTwPL/uMS2Asoy8ZlF X-Received: by 2002:a17:90a:d915:b0:263:e122:1bc7 with SMTP id c21-20020a17090ad91500b00263e1221bc7mr750064pjv.23.1689727977639; Tue, 18 Jul 2023 17:52:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689727977; cv=none; d=google.com; s=arc-20160816; b=rREBqUdkh00Oa6rbCymPXrt1G5ntbLe5n6A7cHuHVedP7yIL5SL2ho0ZzczDoOF4sd P/ETfjDaXX/v+JnwruhRGM83MQfsHkBy6TB842qkEueihV+qlv6IfYfB1p/c0L15pcwH wujDlwVSvaJoyrXs+dMKQKAc2yPAmCmLI1BWOTBcmOMnelggXbXN14S1APx9BrP67a6N AoXwysWz7oYwd7hK6GNJN/ru1MQ/SpCh/BCnMK/dk86ts2VLWnkHIW1iy4+uEI9fGdqh rmmd49rCFf/kCW7S7Ia1+a6vwPv2oalQtNBWFPbCl3LtLQ2SO8s/lFyubbvwWyrEeMXT 4WZg== 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=2jDdPbzH4onHNW5mQ+nHZzIsBXIDVnlx9OcaE5Mwsjc=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=YoT1nI6K32BTW94Jl6uFv4n02VbTq9ITYjtRdDA9g9aL+A4YH3wJfOkhfF7n+b7S8V hpTdnHy2lirdTnb+fFkGBUGtcwrkcvK/xayaH5sYJsAUffvm02N/4n67/JS3TTqhWYFY cCREv95efCDaQpUDDAduBRI3acWnaEQQWBdPUaDKHRNZhPrFMBvvdLxpuyS5plPfUBBF sQi/ihncep7+8rkZJw3FUSBHWS4RE8BpRfxohksTMNSjz2HnZ487xNJJj6mAD7TN0BKe n0XSJnu9QOqc5dzGjGPSdV7YRpPlwfFBug/B11qiMl5f+yZ5hw5zi06N6tDEMp49FPTA vcHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=eepobj0V; 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 a2-20020a17090ad80200b00259a3c7fb52si343316pjv.27.2023.07.18.17.52.44; Tue, 18 Jul 2023 17:52:57 -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=eepobj0V; 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 S229689AbjGSAvW (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230144AbjGSAvB (ORCPT ); Tue, 18 Jul 2023 20:51:01 -0400 Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A8C01A8 for ; Tue, 18 Jul 2023 17:50:17 -0700 (PDT) Received: by mail-qk1-x72e.google.com with SMTP id af79cd13be357-76571dae5feso589566385a.1 for ; Tue, 18 Jul 2023 17:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727816; x=1692319816; 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=2jDdPbzH4onHNW5mQ+nHZzIsBXIDVnlx9OcaE5Mwsjc=; b=eepobj0Vc1efSCz7ybajZtwpgJ5AghKHP5/txHgJFHHyY4FHoRmuQTm5LGfIesiqT/ PD2ZTDjYtuPJ8w+SRAsKlf9SW1Ib1pYpdFc84jBiO4Y0y16uLALHKiYXgvuGWkyfqOw2 oljI0n/oJhPluX28Ya2d//kILPfh1ZUF/MMYgawk3dKZjJ9oe5nOBGUQyNi8SIIIm7c0 DTD5w9IYzqxsaAfBozvvoYSNWWlOsx0KjrDpjNiA32/Tivs28ml9xEjAwGuMBfpc3bKv z6IhJoR+YcKaXykod/FSL6o2Y+i/IWqmY0UulvwuRxY56EXpN3w9sHfLd323IuK7M8Tu AOgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727816; x=1692319816; 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=2jDdPbzH4onHNW5mQ+nHZzIsBXIDVnlx9OcaE5Mwsjc=; b=SNzEwskg9rPm/Bl9CH1HSdwC26W7b8w8Nt0ZFgPA82knQu3f+MAf3F/RxW7BIXkdGr 1MTflwjNjyATClkawcGaZBVupWno5KstbXT1dkw6f7bliSJ9xfWEMrLFOA8qoc8sw+35 WAKn9a3VHP4NajC+exEjW0yBnr9MxkpH7T5emsmsLHzTOaUgcJqw7ywOjDn/56LRVeyY vken5GjOGWqk2QZCUsFjdTViMdkN5fpcCmXxsFef+iMNxKYAKWd371X1Y0PI8XCufR2X WTKFEPlosfPpdGbG/uREY8rJHE2XYJqEtXyiGn4HahIxgqIWEhS2nEBtJtVwgn3P9HN4 KCkw== X-Gm-Message-State: ABy/qLbFQS/FXP7CcGATSwUfONm1uLJzneJN3YRlVUtnLLN5VYBGEKEA hnQuQ5c9kVHdVqjKUp2GzLd+rA== X-Received: by 2002:a05:620a:3998:b0:767:2f4a:e07a with SMTP id ro24-20020a05620a399800b007672f4ae07amr1182364qkn.68.1689727816119; Tue, 18 Jul 2023 17:50:16 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:15 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:12 +0000 Subject: [PATCH RFC net-next v5 08/14] af_vsock: add vsock_find_bound_dgram_socket() MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-8-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_BLOCKED, 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: 1771808203905383095 X-GMAIL-MSGID: 1771808203905383095 This commit adds vsock_find_bound_dgram_socket() which allows transports to find bound dgram sockets in the global dgram bind table. It is intended to be used for "routing" incoming packets to the correct sockets if the transport uses the global bind table. Signed-off-by: Bobby Eshleman --- include/net/af_vsock.h | 1 + net/vmw_vsock/af_vsock.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index f6a0ca9d7c3e..ae6b6cdf6a4d 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -215,6 +215,7 @@ void vsock_for_each_connected_socket(struct vsock_transport *transport, void (*fn)(struct sock *sk)); int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk); bool vsock_find_cid(unsigned int cid); +struct sock *vsock_find_bound_dgram_socket(struct sockaddr_vm *addr); /**** TAP ****/ diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 0895f4c1d340..e73f3b2c52f1 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -264,6 +264,22 @@ static struct sock *vsock_find_bound_socket_common(struct sockaddr_vm *addr, return NULL; } +struct sock * +vsock_find_bound_dgram_socket(struct sockaddr_vm *addr) +{ + struct sock *sk; + + spin_lock_bh(&vsock_dgram_table_lock); + sk = vsock_find_bound_socket_common(addr, vsock_bound_dgram_sockets(addr)); + if (sk) + sock_hold(sk); + + spin_unlock_bh(&vsock_dgram_table_lock); + + return sk; +} +EXPORT_SYMBOL_GPL(vsock_find_bound_dgram_socket); + static struct sock *__vsock_find_bound_socket(struct sockaddr_vm *addr) { return vsock_find_bound_socket_common(addr, vsock_bound_sockets(addr)); From patchwork Wed Jul 19 00:50:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122347 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2119681vqt; Tue, 18 Jul 2023 18:11:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlE5XciARGQLi6ioBq7I2nvs7LUn9ZPV56KPGVE3OmyXgFvMBcTzfN7bP+6QY1TbnkzRvlDJ X-Received: by 2002:a17:906:3b0e:b0:957:2e48:5657 with SMTP id g14-20020a1709063b0e00b009572e485657mr908749ejf.68.1689729119346; Tue, 18 Jul 2023 18:11:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689729119; cv=none; d=google.com; s=arc-20160816; b=vfgq2/4up9kSdXVdL6PHKyvZTblKhEdKsshj9cKrhDwonpHgUO29T6E3HS5mTkFm9K xJKEdy1vp2cPP51wVBjJjji8YUMswH3VNf6rreKqBzdn2apLrhyvZkSALGTKgsZIhzFZ W4b6Cl9Y43nkAQ2GiCHg/7GzIKxsTKsAF02txSzGglRoM+nzLVVVYlUN1ugpwaRXeW01 sAr++HXxZ2N+ZW+LU9wKu/MURnzihWOpz95oHAhs7RrkEcNzJ+4NEW2GfDbvzJbPruPm n8JQ8BPi+EgjDY5aqed2EToMLEuLTnZna5JPvnPRqWW6Qwn2Cl5SKicZEeJlF/gPsqH9 fjlA== 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=M+JsHAVeQKqCNEF0CRYlofIAoy5flsel9yo94Vo/+Eo=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=o9AHx1WSm187s4KVGgGX67g3e2Du574t5LIUoRT9PPsYHX3CnFeTikbVKYkcv+8BWf Zl9JnL/KpsmgvfgY4BCjBVCz0WeoFUP+qr4K3E+zB93x3arc1J8a8oZ7Ie4DwnI+ZSrV Ow7T92p9DGqkIjdwugICwT+Vl+XXI/zbz+k9mhBx4WmR0seyf+xiY2AhTEGCRP9/Qdzu XdQaBXflSIWJ4eXSc0thPLADIa/JAY1RUIs8LakeBsReYPUNP5Q9+KQ+VQPr1XL6z8i6 TOQ2oTRrASTbnmHmAFav+BAfW4T1tgLtRgUcaNAs2XpqVqdHltBai1COZ6Kr+Xoisi0E 34cA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=k3FRG1PM; 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 pv28-20020a170907209c00b0099331931d5esi2012766ejb.71.2023.07.18.18.11.35; Tue, 18 Jul 2023 18:11:59 -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=k3FRG1PM; 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 S230164AbjGSAv1 (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229994AbjGSAvC (ORCPT ); Tue, 18 Jul 2023 20:51:02 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EED81BDB for ; Tue, 18 Jul 2023 17:50:17 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id af79cd13be357-76714caf466so582918485a.1 for ; Tue, 18 Jul 2023 17:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727817; x=1692319817; 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=M+JsHAVeQKqCNEF0CRYlofIAoy5flsel9yo94Vo/+Eo=; b=k3FRG1PMx0ejaOyKjb+qIvS2h2qh7nI25WPMOBcv4beZE+hxTM+Z5dNRyCx59FHH/b 19EQavh8h8Fe/FtWPDeJXA2VB/UD3SZAStjnuS428zwlVMgS4h5Cq8YubGwGoC2Y0DgJ ICRqDvKNoFWj4AXdVQ2AM/hZY+upLLwgtrtt9r+MrZLkxQf43EBC7BF+Kb+Xw2Wa+mDY UFfWUjaqqRe2UeFbdp4qVgUravI0Bcl2TuHpPBiDMVj98CXTkvOzV7f4w2SIpgP57UvU 6H9DIVWaVTJQibyeFjCoc6gw8ufkYUDdCcNyG1e/HxzCIUUXnqCllHDoaMmiQ2uMFzoD UYzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727817; x=1692319817; 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=M+JsHAVeQKqCNEF0CRYlofIAoy5flsel9yo94Vo/+Eo=; b=Hb5p+LBy+ZOyrvf8SptMcFdZ6YcqZkJa4ANjpRazLTLta+L0qMNxWnOSvrPe/Vw4XL NkUMG+SN1puj6E88xxXCX3BlTGoBfmoOdiSHNzPUEUR6r8iKrAFDoguoX1L17GXg5EIl IAMPR2E1ob0LXB1v83hlVBihx1nlFC6YW8eBcJPK7WJ/FsBnP3gu3Uzh6EJ5dxtClvGX XEtzFBpwNVBDRRPDjng6OHzejSlSi2C4OKsfPRIBJpvtw7Mk9gj7eFv/1nticKvmrOPg XNH8gcqLWwaszn6wISs3uxhMmBPlTzKzQFwc2mvTCigr5XtV1sdu+rlPS7VwMTBkWRBc Sdiw== X-Gm-Message-State: ABy/qLa+xw27kej4xB6AGfmbQLn3s16cP8OFtgQBQMljW6Ky6u8aT1Sk QFZFnmyQ2hGzj3LfP3wEdaApRw== X-Received: by 2002:a05:620a:2588:b0:767:18b5:f6d6 with SMTP id x8-20020a05620a258800b0076718b5f6d6mr20219811qko.2.1689727816973; Tue, 18 Jul 2023 17:50:16 -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.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:16 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:13 +0000 Subject: [PATCH RFC net-next v5 09/14] virtio/vsock: add common datagram recv path MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-9-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: 1771809401317054366 X-GMAIL-MSGID: 1771809401317054366 This commit adds the common datagram receive functionality for virtio transports. It does not add the vhost/virtio users of that functionality. This functionality includes: - changes to the virtio_transport_recv_pkt() path for finding the bound socket receiver for incoming packets. - a virtio_transport_dgram_addr_init() function to be used as the ->dgram_addr_init callback for initializing sockaddr_vm inside the generic recvmsg() caller. Signed-off-by: Bobby Eshleman --- include/linux/virtio_vsock.h | 2 + net/vmw_vsock/virtio_transport_common.c | 92 ++++++++++++++++++++++++++++----- 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 7632552bee58..b3856b8a42b3 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -212,6 +212,8 @@ u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk); bool virtio_transport_stream_is_active(struct vsock_sock *vsk); bool virtio_transport_stream_allow(u32 cid, u32 port); bool virtio_transport_dgram_allow(u32 cid, u32 port); +void virtio_transport_dgram_addr_init(struct sk_buff *skb, + struct sockaddr_vm *addr); int virtio_transport_connect(struct vsock_sock *vsk); diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index 3bfaff758433..96118e258097 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -183,7 +183,9 @@ EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt); static u16 virtio_transport_get_type(struct sock *sk) { - if (sk->sk_type == SOCK_STREAM) + if (sk->sk_type == SOCK_DGRAM) + return VIRTIO_VSOCK_TYPE_DGRAM; + else if (sk->sk_type == SOCK_STREAM) return VIRTIO_VSOCK_TYPE_STREAM; else return VIRTIO_VSOCK_TYPE_SEQPACKET; @@ -1184,6 +1186,35 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk, kfree_skb(skb); } +static void +virtio_transport_dgram_kfree_skb(struct sk_buff *skb, int err) +{ + if (err == -ENOMEM) + kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_RCVBUFF); + else if (err == -ENOBUFS) + kfree_skb_reason(skb, SKB_DROP_REASON_PROTO_MEM); + else + kfree_skb(skb); +} + +/* This function takes ownership of the skb. + * + * It either places the skb on the sk_receive_queue or frees it. + */ +static void +virtio_transport_recv_dgram(struct sock *sk, struct sk_buff *skb) +{ + int err; + + err = sock_queue_rcv_skb(sk, skb); + if (err) { + virtio_transport_dgram_kfree_skb(skb, err); + return; + } + + sk->sk_data_ready(sk); +} + static int virtio_transport_recv_connected(struct sock *sk, struct sk_buff *skb) @@ -1347,7 +1378,8 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb, static bool virtio_transport_valid_type(u16 type) { return (type == VIRTIO_VSOCK_TYPE_STREAM) || - (type == VIRTIO_VSOCK_TYPE_SEQPACKET); + (type == VIRTIO_VSOCK_TYPE_SEQPACKET) || + (type == VIRTIO_VSOCK_TYPE_DGRAM); } /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex @@ -1361,40 +1393,52 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, struct vsock_sock *vsk; struct sock *sk; bool space_available; + u16 type; vsock_addr_init(&src, le64_to_cpu(hdr->src_cid), le32_to_cpu(hdr->src_port)); vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid), le32_to_cpu(hdr->dst_port)); + type = le16_to_cpu(hdr->type); + trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port, dst.svm_cid, dst.svm_port, le32_to_cpu(hdr->len), - le16_to_cpu(hdr->type), + type, le16_to_cpu(hdr->op), le32_to_cpu(hdr->flags), le32_to_cpu(hdr->buf_alloc), le32_to_cpu(hdr->fwd_cnt)); - if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) { + if (!virtio_transport_valid_type(type)) { (void)virtio_transport_reset_no_sock(t, skb); goto free_pkt; } - /* The socket must be in connected or bound table - * otherwise send reset back + /* For stream/seqpacket, the socket must be in connected or bound table + * otherwise send reset back. + * + * For datagrams, no reset is sent back. */ sk = vsock_find_connected_socket(&src, &dst); if (!sk) { - sk = vsock_find_bound_socket(&dst); - if (!sk) { - (void)virtio_transport_reset_no_sock(t, skb); - goto free_pkt; + if (type == VIRTIO_VSOCK_TYPE_DGRAM) { + sk = vsock_find_bound_dgram_socket(&dst); + if (!sk) + goto free_pkt; + } else { + sk = vsock_find_bound_socket(&dst); + if (!sk) { + (void)virtio_transport_reset_no_sock(t, skb); + goto free_pkt; + } } } - if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) { - (void)virtio_transport_reset_no_sock(t, skb); + if (virtio_transport_get_type(sk) != type) { + if (type != VIRTIO_VSOCK_TYPE_DGRAM) + (void)virtio_transport_reset_no_sock(t, skb); sock_put(sk); goto free_pkt; } @@ -1410,12 +1454,18 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, /* Check if sk has been closed before lock_sock */ if (sock_flag(sk, SOCK_DONE)) { - (void)virtio_transport_reset_no_sock(t, skb); + if (type != VIRTIO_VSOCK_TYPE_DGRAM) + (void)virtio_transport_reset_no_sock(t, skb); release_sock(sk); sock_put(sk); goto free_pkt; } + if (sk->sk_type == SOCK_DGRAM) { + virtio_transport_recv_dgram(sk, skb); + goto out; + } + space_available = virtio_transport_space_update(sk, skb); /* Update CID in case it has changed after a transport reset event */ @@ -1447,6 +1497,7 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, break; } +out: release_sock(sk); /* Release refcnt obtained when we fetched this socket out of the @@ -1515,6 +1566,21 @@ int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t recv_acto } EXPORT_SYMBOL_GPL(virtio_transport_read_skb); +void virtio_transport_dgram_addr_init(struct sk_buff *skb, + struct sockaddr_vm *addr) +{ + struct virtio_vsock_hdr *hdr; + unsigned int cid, port; + + WARN_ONCE(skb->head == skb->data, "virtio vsock bug: bad dgram skb"); + + hdr = virtio_vsock_hdr(skb); + cid = le64_to_cpu(hdr->src_cid); + port = le32_to_cpu(hdr->src_port); + vsock_addr_init(addr, cid, port); +} +EXPORT_SYMBOL_GPL(virtio_transport_dgram_addr_init); + MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("Asias He"); MODULE_DESCRIPTION("common code for virtio vsock"); From patchwork Wed Jul 19 00:50:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122340 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2117808vqt; Tue, 18 Jul 2023 18:07:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlG5Bc9WHbLHETYCMelZRCwYT1mTRML/RWQcqe5vcPgNk0W64LM9AqNrpREh4temYPbJZrDl X-Received: by 2002:a2e:8710:0:b0:2b7:2ea:33c9 with SMTP id m16-20020a2e8710000000b002b702ea33c9mr729736lji.20.1689728855077; Tue, 18 Jul 2023 18:07:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689728855; cv=none; d=google.com; s=arc-20160816; b=m8EonyRaMteAwfXQ0+517c8K6RW3wcxVqygaH3u+0yh84U9KO6WKP4N13TZUNS8IeZ l/WnMcZrh2ejqRG7xLWCaNHCMj6v9YdDzZMsE0cKBCP91Y13q+bVb22OC8cCRjOfFeZS Uvgj1Uefz+vf/bWI+QQkxygpTBrOo3r9BkPkvtcJLuz4DECxKZxP8iMtYWwhfW29yb+h 9kVOvxS67IJei5JNTnm+L/F6cgk0KwnBD43bt2AbGx9+IMfBXPNAe//ZQINM5ZkpnvW8 TC9pz8zd5sI9pDd6dJxVU/NVv/freU0QC+flOokLpmb7l5kT/k/R/x8aPnS15kMczmYV Yzrw== 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=lELx1Jor3ryyghgQ+HYk7Hk999TnyRY5ELpyAOdCAo8=; fh=mG5Sox+GcIzYFDJZVrRRBLlD2ICT0t2fGmdMMv0dhmY=; b=Wd5JBi0DUT+BEBKsP6w7Lw+O79bE5P7rUokH+6vLwZr2mcag4ZbNCk3/Perf2NNF4z huV4XxOOh9HA3E4eLTlsNlcFpH4dCsiwn8b8D6c0gk1GT6QjxmS1YjmmQfCBbJ2Gk353 OhxPGIvQLvvZ8fMzfb0OpojKgNPPdXUAHU89Y1xjUMo+SzbiAbpy/UaGzFVhkelxvGwK EmoG8DcOzeiegVfL6drM4sd6bVVyu0V9zvqkswcpsAXt8OQqDrAivz+vCL9bcyvSy4CS pSYxhxdlmDPblqr76C73sam9krlAa6SGer6SWOtehuY1DqYeY2VQytlekTRxzP3aaKzI 4wIQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=kh51wAOb; 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 d16-20020a170906041000b009892631e251si2067895eja.770.2023.07.18.18.07.09; Tue, 18 Jul 2023 18:07:35 -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=kh51wAOb; 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 S229817AbjGSAvZ (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229989AbjGSAvC (ORCPT ); Tue, 18 Jul 2023 20:51:02 -0400 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C9393198E for ; Tue, 18 Jul 2023 17:50:18 -0700 (PDT) Received: by mail-qk1-x72b.google.com with SMTP id af79cd13be357-763a2e39b88so624562585a.1 for ; Tue, 18 Jul 2023 17:50:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727818; x=1692319818; 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=lELx1Jor3ryyghgQ+HYk7Hk999TnyRY5ELpyAOdCAo8=; b=kh51wAObPhNEmOAF6xUu3EksFG8Jzcd7WXjvKK6sY85ydx0oCAjbKfJr7SfAn8ZIn3 OGDI5Sza40VPT51V90ZS6M8PG8voBghdWBcI5NkF1Ln/E2NjdGFfN7ylSyhZB3H/2qSs Eux2cQM7uyHorES3YiVvkdVicwQZiumEj2S/+chWRKgcpZyK9fkMR/CLDlpe+s84ardD AKBPhERrtDXd34zmfEnd3tYdQcRIeQYrqvfg1/Y0DzpVit92JSl6lqih2sFM3FuYrzAZ fDskheDYTg3t3x7D1pKzM5RwsL+n/HiUxZZYtcVnsMAhT0q+Xp+oLec11WIcU0wG0jf8 03Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727818; x=1692319818; 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=lELx1Jor3ryyghgQ+HYk7Hk999TnyRY5ELpyAOdCAo8=; b=BirYATpAIsNkQ5CIYOTHtfv8pWB3SLZJt7fSlv663YP/QMeUvJCjKCyk6sTuwxGwVI 5kU4/8pl6qsXF8myK1wonmrQRyNB/dXZD4hFi8WcPwGbKPMpwLJWnLNeW0HBF5DrmKTk FAR7jW3I6kWXTUDt5u/HXbyrwaOSoOHHdmc47O7rK8700bMqcLmpp5/pOa3KxjwCxpZ1 LEWGFuYI8F+mN+6frAJN5HgGQsbKoa0Sf1CjbQe0x2CD+qNL8jRjq2JhOodG5OTu/Pbr 7NWkZ45D9dy0HgdSPn8zFnzQaplMiz93q3Or8QHD/MI92dIh0C1GH6aNTQJ4lO1U0oH1 vXjQ== X-Gm-Message-State: ABy/qLb5T9bvlQTWrjOCny4g5YugNmOB1INc5obMSW9KxmECyd/UtOPM jPYzv42/pQ4wViFlhLaM1oW10w== X-Received: by 2002:a05:620a:8c13:b0:765:9e6b:139e with SMTP id qz19-20020a05620a8c1300b007659e6b139emr1162065qkn.16.1689727817783; Tue, 18 Jul 2023 17:50:17 -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.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:17 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:14 +0000 Subject: [PATCH RFC net-next v5 10/14] virtio/vsock: add VIRTIO_VSOCK_F_DGRAM feature bit MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-10-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 , Jiang Wang 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: 1771809124414680583 X-GMAIL-MSGID: 1771809124414680583 This commit adds a feature bit for virtio vsock to support datagrams. Signed-off-by: Jiang Wang Signed-off-by: Bobby Eshleman --- include/uapi/linux/virtio_vsock.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/uapi/linux/virtio_vsock.h b/include/uapi/linux/virtio_vsock.h index 331be28b1d30..27b4b2b8bf13 100644 --- a/include/uapi/linux/virtio_vsock.h +++ b/include/uapi/linux/virtio_vsock.h @@ -40,6 +40,7 @@ /* The feature bitmap for virtio vsock */ #define VIRTIO_VSOCK_F_SEQPACKET 1 /* SOCK_SEQPACKET supported */ +#define VIRTIO_VSOCK_F_DGRAM 3 /* SOCK_DGRAM supported */ struct virtio_vsock_config { __le64 guest_cid; From patchwork Wed Jul 19 00:50:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122334 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2111513vqt; Tue, 18 Jul 2023 17:52:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlFjhuJSjjhBeYKpOm2oyrZEIaj0CVplbAPB9x+Ljsk63I2BmIFKior4faNBffpY7c/ScCu0 X-Received: by 2002:a05:6a20:4291:b0:11e:e940:441e with SMTP id o17-20020a056a20429100b0011ee940441emr15637053pzj.25.1689727930589; Tue, 18 Jul 2023 17:52:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689727930; cv=none; d=google.com; s=arc-20160816; b=lX1QAHHUFgyE9nnhLdJukjqWAtmVjfJ90hcUk+2pZ2pUoo7Vv/ZFpZRMgo0TawQ94J n6kkFFLyZ0FrVkoJzhQO3K7IQG/l/CE6KzLUqAviWSMa8pzhXX1RyGAXzHWX9dpLI6nw uPZAomerrgI5VUPuOLz+6Go83MEnzAGIotrb/Pd6E4t1lEA0p6oWTCh27nobd8KvmKbZ VRrcvHi+bhHomujJfRywo6/fEaDOahGXgir2DVtqfWe3Sv9Y+8eFcDzvpDuVkXO9rGZS rgPHexrQfzFAe5qQ2WDG98BO98izRr5spewlQitykCK8+ck0sx+LXdSJk6oq/pJ+Jgdf pPfg== 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=sPwhBmtD9OJVg+4DzOYL256xxaoXc/AZRhsCaKboFfQ=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=wCoF/HUMj6vteZQ66DiiSlV+J7po6FZY0vIpXwRhTjJTrcbC/rmNJF4DYzl4qpmggF Y3fRHpmb9v815d2/UMVKOfvjy8RiJpJ6bdfeC+fdFsbGS1l9EWUCt9SOJxW35K6YFgwY JGY54dT+kx03kr8jueFNTEnFaWxEcKbnBSSQx9LVizsX+99QfxTvqz7cK0MW8wl3ayTr YdINl1RBOkdwCUDtjzlWVyhmpiu2lqbcx8vQFThNmIKQt9tVjpHNhKFKIDCeqpv4131N JBIagugrY4DDEKKoguh9V9CHkSlc8YwZV0u/Umn5ReTwKBu7pru8SS7WDEWV/UPboE65 RL1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=Zfsg98WI; 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 cn11-20020a056a00340b00b00668873710d9si2309529pfb.162.2023.07.18.17.51.57; Tue, 18 Jul 2023 17:52:10 -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=Zfsg98WI; 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 S229915AbjGSAut (ORCPT + 99 others); Tue, 18 Jul 2023 20:50:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229918AbjGSAuj (ORCPT ); Tue, 18 Jul 2023 20:50:39 -0400 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D41551BF7 for ; Tue, 18 Jul 2023 17:50:19 -0700 (PDT) Received: by mail-qk1-x734.google.com with SMTP id af79cd13be357-7672303c831so582747685a.2 for ; Tue, 18 Jul 2023 17:50:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727818; x=1692319818; 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=sPwhBmtD9OJVg+4DzOYL256xxaoXc/AZRhsCaKboFfQ=; b=Zfsg98WIgY3dgqQOhdM9OAVFqPKJRz3yNitUZviHPLo8OICH8/ZXeMciH1E2lvVC1/ WQvvNe6PKKqimbaiIqj3LDH7lfrsKB75ErVF7KEisUxO81NDcKvEP1ksOG0gS0q1uina NKwbA9SMEAQ2Shk1tQNxk3aIFrV6Cb5K3YTvQcaifZDHat7ikr/1J3s/lnHkOZg7/7k0 uAqN7l2ohxoCr4PLTpodnRAgI7ZI8bktML3sVuatZHFX0PZII/lQpXB/KCJ69kPptn+Q Y7BvncJSgJ3X7J0wjOrbBcMg96cQK58DPadsZuENr1qj2c7auxRvhplDNYUOWhSiijFv mdew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727818; x=1692319818; 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=sPwhBmtD9OJVg+4DzOYL256xxaoXc/AZRhsCaKboFfQ=; b=AkoHdXeQGzhWJcrUCnn454SxXspQgdQzUL75YbZF7a/clvAahfKPEhMxGW3OPruJKV f/tU9Q8Ga2KOnUZZ/I9C4RdRxZq3ZuV9stce121g18N0X0tkJAyqbCsTJrr8IidceBLw xKSXrOAZ88qAFa4/WSTK8Ed1yKhqdXLrN+Md8Y2NxXl0Aa8SU/dcg6ReIw28lEtUbEyI MfhSaH+o/2N0AUTFx5zx9yi5d5QOv4QYE46qNbqCYjLmhmC28o0aABwEjzjKpVRuBtee 0XS1VcrDpAdgnnfaTfaYo4rDZtHqIK902UYv+9xDEikI+3TCEc/PtDX1a+yM7e4PfjJB UQEg== X-Gm-Message-State: ABy/qLYnwbXV8iOlxoeBc+sc5eRJzjFNS/gnOMp3uHrbSRBAWWVWSaUL BHjW5V0fctMu8TZo21GKwQhuvA== X-Received: by 2002:a05:620a:2447:b0:767:1293:f43e with SMTP id h7-20020a05620a244700b007671293f43emr22685654qkn.49.1689727818622; Tue, 18 Jul 2023 17:50:18 -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.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:18 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:15 +0000 Subject: [PATCH RFC net-next v5 11/14] vhost/vsock: implement datagram support MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-11-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: 1771808154599805622 X-GMAIL-MSGID: 1771808154599805622 This commit implements datagram support for vhost/vsock by teaching vhost to use the common virtio transport datagram functions. If the virtio RX buffer is too small, then the transmission is abandoned, the packet dropped, and EHOSTUNREACH is added to the socket's error queue. Signed-off-by: Bobby Eshleman --- drivers/vhost/vsock.c | 62 +++++++++++++++++++++++++++++++++++++++++++++--- net/vmw_vsock/af_vsock.c | 5 +++- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index d5d6a3c3f273..da14260c6654 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -8,6 +8,7 @@ */ #include #include +#include #include #include #include @@ -32,7 +33,8 @@ enum { VHOST_VSOCK_FEATURES = VHOST_FEATURES | (1ULL << VIRTIO_F_ACCESS_PLATFORM) | - (1ULL << VIRTIO_VSOCK_F_SEQPACKET) + (1ULL << VIRTIO_VSOCK_F_SEQPACKET) | + (1ULL << VIRTIO_VSOCK_F_DGRAM) }; enum { @@ -56,6 +58,7 @@ struct vhost_vsock { atomic_t queued_replies; u32 guest_cid; + bool dgram_allow; bool seqpacket_allow; }; @@ -86,6 +89,32 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid) return NULL; } +/* Claims ownership of the skb, do not free the skb after calling! */ +static void +vhost_transport_error(struct sk_buff *skb, int err) +{ + struct sock_exterr_skb *serr; + struct sock *sk = skb->sk; + struct sk_buff *clone; + + serr = SKB_EXT_ERR(skb); + memset(serr, 0, sizeof(*serr)); + serr->ee.ee_errno = err; + serr->ee.ee_origin = SO_EE_ORIGIN_NONE; + + clone = skb_clone(skb, GFP_KERNEL); + if (!clone) + return; + + if (sock_queue_err_skb(sk, clone)) + kfree_skb(clone); + + sk->sk_err = err; + sk_error_report(sk); + + kfree_skb(skb); +} + static void vhost_transport_do_send_pkt(struct vhost_vsock *vsock, struct vhost_virtqueue *vq) @@ -160,9 +189,15 @@ vhost_transport_do_send_pkt(struct vhost_vsock *vsock, hdr = virtio_vsock_hdr(skb); /* If the packet is greater than the space available in the - * buffer, we split it using multiple buffers. + * buffer, we split it using multiple buffers for connectible + * sockets and drop the packet for datagram sockets. */ if (payload_len > iov_len - sizeof(*hdr)) { + if (le16_to_cpu(hdr->type) == VIRTIO_VSOCK_TYPE_DGRAM) { + vhost_transport_error(skb, EHOSTUNREACH); + continue; + } + payload_len = iov_len - sizeof(*hdr); /* As we are copying pieces of large packet's buffer to @@ -394,6 +429,7 @@ static bool vhost_vsock_more_replies(struct vhost_vsock *vsock) return val < vq->num; } +static bool vhost_transport_dgram_allow(u32 cid, u32 port); static bool vhost_transport_seqpacket_allow(u32 remote_cid); static struct virtio_transport vhost_transport = { @@ -410,7 +446,8 @@ static struct virtio_transport vhost_transport = { .cancel_pkt = vhost_transport_cancel_pkt, .dgram_enqueue = virtio_transport_dgram_enqueue, - .dgram_allow = virtio_transport_dgram_allow, + .dgram_allow = vhost_transport_dgram_allow, + .dgram_addr_init = virtio_transport_dgram_addr_init, .stream_enqueue = virtio_transport_stream_enqueue, .stream_dequeue = virtio_transport_stream_dequeue, @@ -443,6 +480,22 @@ static struct virtio_transport vhost_transport = { .send_pkt = vhost_transport_send_pkt, }; +static bool vhost_transport_dgram_allow(u32 cid, u32 port) +{ + struct vhost_vsock *vsock; + bool dgram_allow = false; + + rcu_read_lock(); + vsock = vhost_vsock_get(cid); + + if (vsock) + dgram_allow = vsock->dgram_allow; + + rcu_read_unlock(); + + return dgram_allow; +} + static bool vhost_transport_seqpacket_allow(u32 remote_cid) { struct vhost_vsock *vsock; @@ -799,6 +852,9 @@ static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features) if (features & (1ULL << VIRTIO_VSOCK_F_SEQPACKET)) vsock->seqpacket_allow = true; + if (features & (1ULL << VIRTIO_VSOCK_F_DGRAM)) + vsock->dgram_allow = true; + for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) { vq = &vsock->vqs[i]; mutex_lock(&vq->mutex); diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index e73f3b2c52f1..449ed63ac2b0 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1427,9 +1427,12 @@ int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, return prot->recvmsg(sk, msg, len, flags, NULL); #endif - if (flags & MSG_OOB || flags & MSG_ERRQUEUE) + if (unlikely(flags & MSG_OOB)) return -EOPNOTSUPP; + if (unlikely(flags & MSG_ERRQUEUE)) + return sock_recv_errqueue(sk, msg, len, SOL_VSOCK, 0); + transport = vsk->transport; /* Retrieve the head sk_buff from the socket's receive queue. */ From patchwork Wed Jul 19 00:50:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122343 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2119225vqt; Tue, 18 Jul 2023 18:11:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlG6OIIKY7lGPwfE71PxwGkiou2Mfzu86sgMN5tUElae/ugJE2JpJB+7vcd66Q8OnmOe8Nj+ X-Received: by 2002:a17:907:d13:b0:988:fb2f:274e with SMTP id gn19-20020a1709070d1300b00988fb2f274emr925338ejc.27.1689729061145; Tue, 18 Jul 2023 18:11:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689729061; cv=none; d=google.com; s=arc-20160816; b=NcOHOm9/0JAiDKLWvT+zkZOlVBFjgG05m/ZNNLRvTFCt5CYc4auTVdy4ChkLffh/bb tpZ/hKgaBNCKW9qr9/fGe4rKfl0sH1i9aY3iB4pqWNbeAPswSSEfdpEog7JFngvzDiGJ KmrKpw/kMyfaUi78B7oJuXm7cRveiEMLwTKN2nokVJYbzmhhqXseMYsgOgpx7ZQsF+Xz rMhsYEzxDRk64Od9KXZAY2YeduhNuBxtD8A+sqcNfvVxu2jZzaLOy2R3i5SH4d8dYPVt dnH20z+8xBj0zx1OPA7alPMjNYCl4pyffkT4rTmJaIHfC+H/UpUMonpWGm0DkVnmpqbg 2CtA== 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=xWt1YKpD8soekZyHz3lCaTewqjrH2L6FY0Axo6M4fj8=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=SJy1+qkFQ7QswI63mOfD800hxa5IDRQEkE4/2tOzyMfVvg1Rjuo9BCCZHZoxXcP2gj huuaERq88fNdD/dDevEqooDb1qMD0mmAov/uJfGo1ZNT1siN6c3wQiPkAKlxOPtwTtxD /Bau0tEVKjDnqEafbNqFUSn0UgreGc001nQqfwaUn5btQVhSI1ghRjNFPasWsmT2MDAq uPRAf9fSn0KZObO57kNk0xnKoYGXqrq99pdFhpKVItbV/4z0Sce1nKleHnXn1ihoplfv meX6ru8Czty0rU3pXMf6WRv8ZX1jFx7M8lnfc7pYsm6B1gY8MuR9byTwWazloA9oAp0C ezQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=W6pVQ6BR; 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 gq15-20020a170906e24f00b0098886d50308si1999786ejb.490.2023.07.18.18.10.37; Tue, 18 Jul 2023 18:11:01 -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=W6pVQ6BR; 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 S230294AbjGSAv3 (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230200AbjGSAvE (ORCPT ); Tue, 18 Jul 2023 20:51:04 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED88718D for ; Tue, 18 Jul 2023 17:50:20 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-76731802203so582286485a.3 for ; Tue, 18 Jul 2023 17:50:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727819; x=1692319819; 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=xWt1YKpD8soekZyHz3lCaTewqjrH2L6FY0Axo6M4fj8=; b=W6pVQ6BRHiDGSffnATfPbkdw7LAkbCWNOgEeVD9GxenwUIT3vGKQ1XNpdCGV2dE5U0 lJeJ+JLDX+zXBHDl/VjOXmwS8BMhZ5XSG25WsyCdQ1L7J1PUQeS7wchneIdZXrXnew5c KKW0gqXZEEH92kEOE7IFf+JTQHYcqqcD5rraPp2jJH3XKCssNhQOiNEh9IpZfQ4eBiwF aDJXTPexVb1OPGgbnTlIM42X66c35BOEx7O4i0WJKVhEhHUmfAKxktnatfdWGkr0RmVV O4BHQmBBgupnNG76/x2nogVrYfJ8M7gCIWAYec8ZDmCl1QIBGbUBLQtvMORRGTgded45 YoTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727819; x=1692319819; 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=xWt1YKpD8soekZyHz3lCaTewqjrH2L6FY0Axo6M4fj8=; b=R8I84tkwpfczKlGhKTiTCV2JLh/4/l7U0Pzb2cCrTV0KX7BmVTUBQxj/1p2v22p3qs 3pgFGnHYODtwYu23brhPLEPHkFvQFal4vG5fDdMQOQSZs3KMmC4jIPIzlnX19JyVFi5c Y1alI7kRk3erP8NfobNPeMyDIP2ANZnOSm+bXhBGh9C0KG1wZGZmKLTf6ivF2aMfHmkX 0gofo9SIkoXk+AmLogrbM0fbQela3w5tgypWsANb6djFzScCiOYrcVXLpiUz5hcLmmdF WStQAVBiRrPdoZMKPiZCOBQpP0j1R5FE8YoxkcVUIGz67rZJnW+zuHcXmZsqcVT9pW9q Gdaw== X-Gm-Message-State: ABy/qLZvd1BQuk+moknq4VBS1LB/Ggqeo+a91Q+ct9/60CQn/r8eYZx+ B3ExNE4wfJSfz5Bp3u/TflR+sw== X-Received: by 2002:a05:620a:2401:b0:767:f2c2:7e64 with SMTP id d1-20020a05620a240100b00767f2c27e64mr19481527qkn.63.1689727819403; Tue, 18 Jul 2023 17:50:19 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:19 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:16 +0000 Subject: [PATCH RFC net-next v5 12/14] vsock/loopback: implement datagram support MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-12-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: 1771809339879683325 X-GMAIL-MSGID: 1771809339879683325 This commit implements datagram support for vsock loopback. Not much more than simply toggling on "dgram_allow" and continuing to use the common virtio functions. Signed-off-by: Bobby Eshleman --- net/vmw_vsock/vsock_loopback.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 278235ea06c4..0459b2bf7b15 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -46,6 +46,7 @@ static int vsock_loopback_cancel_pkt(struct vsock_sock *vsk) return 0; } +static bool vsock_loopback_dgram_allow(u32 cid, u32 port); static bool vsock_loopback_seqpacket_allow(u32 remote_cid); static struct virtio_transport loopback_transport = { @@ -62,7 +63,7 @@ static struct virtio_transport loopback_transport = { .cancel_pkt = vsock_loopback_cancel_pkt, .dgram_enqueue = virtio_transport_dgram_enqueue, - .dgram_allow = virtio_transport_dgram_allow, + .dgram_allow = vsock_loopback_dgram_allow, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue, @@ -95,6 +96,11 @@ static struct virtio_transport loopback_transport = { .send_pkt = vsock_loopback_send_pkt, }; +static bool vsock_loopback_dgram_allow(u32 cid, u32 port) +{ + return true; +} + static bool vsock_loopback_seqpacket_allow(u32 remote_cid) { return true; From patchwork Wed Jul 19 00:50:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122338 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2111959vqt; Tue, 18 Jul 2023 17:53:33 -0700 (PDT) X-Google-Smtp-Source: APBJJlGRHHDhCOwOgiwTZvtcQ6SUPGV1whFiZzjsGiXQ431vvJ5AGy4GV3c7KAFFS0tNTvGMiNjd X-Received: by 2002:a17:90a:de08:b0:263:856:fcdf with SMTP id m8-20020a17090ade0800b002630856fcdfmr13807852pjv.12.1689728012808; Tue, 18 Jul 2023 17:53:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689728012; cv=none; d=google.com; s=arc-20160816; b=dgJBxMNegyfkgwNL4TOopgM+dH5Omc2XiiPQSKphX+MPIGrAlfDw+nOYUgoIUeDE7C h0RexqRmzjWtelClkJyicqM5E/8+t4p1k75Mla4frFHcGB2OqEeadUa+1kxHNsmpccb0 9DsMBGWZlgoJAkXLD2LTGNll35NGXVBFS75QylSi2r1dcW+2zBEj6b2d/ac7QtI0GpjG 2IdYXkbCkKmM9aODJxRZOsXoScJcGlFKb5qoxuike0hwWXGBDd2U1cLk88XPgh4RE1nU Al0LkRopkFmAv49+GxDqHyRSjJ6y86sXCz0LYdcgNMy91stjkiQZGlr5fn4MwpjeHkga KC2w== 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=YwyL60j0LwW2T2Dkd6jqyPcZMdC5174wawCzrtawFxQ=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=g1P/One1cZMdWIbYR/HZV36ZzKw3GIZ5DKsSJ/vukx0pmwnt4/ADekJFaHoeUoXD5q K/L+Xxc1nk5AyjOXGDZ+ygS3+etYVwzfMbEGo+TNq1l4fLROa9h/EcI4DxzyLbEYHbGE DoKAPg2TbxsyOFMA5O/OpGVxfS1JEPjDl9I5cPFicyJ9ylY1USWBDD4lMZCvW3m5u3nj Hbe+G2IuGL2fi/7VYmee0KhKyDstXe+YgdfcluRDcGqfD/qf19q12cDbeSe51eHDgyGZ U7Duu/aLR3U5x+39rfpgnbTCAi2kiaQgbVygOH6k1givuS0gyymPWskIGbmYY28VCm7g 98Ew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=ZkFEdOwq; 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 c64-20020a17090a494600b0025e2c7b1808si347139pjh.53.2023.07.18.17.53.18; Tue, 18 Jul 2023 17:53:32 -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=ZkFEdOwq; 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 S230227AbjGSAvl (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229982AbjGSAvE (ORCPT ); Tue, 18 Jul 2023 20:51:04 -0400 Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 870831BCF for ; Tue, 18 Jul 2023 17:50:21 -0700 (PDT) Received: by mail-qk1-x72d.google.com with SMTP id af79cd13be357-7653bd3ff2fso692933885a.3 for ; Tue, 18 Jul 2023 17:50:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727820; x=1692319820; 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=YwyL60j0LwW2T2Dkd6jqyPcZMdC5174wawCzrtawFxQ=; b=ZkFEdOwq2hZ+UH9aki+OAvh12b03IsfbbmomV42Sr1oO//aaD/ebgnltz0if5zRzRw kCGRgBejlVJSBR9faXcL/Z8lb42tToOWyKuRZ+r3kWDkLsDHjG4IF9hdeXjcgq1Ar8jo lAo/AmRop4+65XfBpTe/ZmLw1ghMlUHtIR40H3XQ2eB2SPJpch9esUCy5wXwCZQw16Qo hNgaft6pooZorRPl7hGLkwGEPqVfd8l6hmVe5xLVgj5hO4IDDZ/55DxLee2KXx8fs05/ jWkktHeT52LbTUtU39jx9vsn5jSdLBBRa4eTV8RhXslnxpjudAQmXfWck7XOMm3qbTca Zvtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727820; x=1692319820; 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=YwyL60j0LwW2T2Dkd6jqyPcZMdC5174wawCzrtawFxQ=; b=OqEytgznZSSxSDRQiUKNJIdmUleRFzGLLTFVHYV7gZl3mnSJHmrjV4w8TI/KdpV6Do BX1OaKc4+KJNDuYxDsdWjsk48WKcXr0a6mjW+X9+xqGESN3Epz9gtjvwAcqay/bmv8rr NplXaJ99NEOFpwXb/r9cjIIDHKeiu6Hkgs8AQwtywH4BZgkuoCgi8QyQq8O4ERIWaw/m 4qBhifQtOXw1hjGV424W+qLe6ZD3wl1tTNTFgvDBsj1q1XNCVoeKAOguDudOblADcsT4 Td3KCsI2KmgxZDrQhOmxVhRd9N5Lp3Q69W/9Hk8Aksfso8ptWUk5d2aOqOmz0I4vKrSR wunA== X-Gm-Message-State: ABy/qLZFpNz6tMRHPmKIxYDWCrRUlBB9f1wXWLzrMlbQXgG2C4UF7zk5 vYjHe6bBSzxv4zVSx+0Ewa1p+g== X-Received: by 2002:a05:620a:2687:b0:767:3fa7:2afa with SMTP id c7-20020a05620a268700b007673fa72afamr20697364qkp.28.1689727820255; Tue, 18 Jul 2023 17:50:20 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:19 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:17 +0000 Subject: [PATCH RFC net-next v5 13/14] virtio/vsock: implement datagram support MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-13-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: 1771808240551120610 X-GMAIL-MSGID: 1771808240551120610 This commit implements datagram support for virtio/vsock by teaching virtio to use the general virtio transport ->dgram_addr_init() function and implementation a new version of ->dgram_allow(). Additionally, it drops virtio_transport_dgram_allow() as an exported symbol because it is no longer used in other transports. Signed-off-by: Bobby Eshleman --- include/linux/virtio_vsock.h | 1 - net/vmw_vsock/virtio_transport.c | 24 +++++++++++++++++++++++- net/vmw_vsock/virtio_transport_common.c | 6 ------ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index b3856b8a42b3..d0a4f08b12c1 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -211,7 +211,6 @@ void virtio_transport_notify_buffer_size(struct vsock_sock *vsk, u64 *val); u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk); bool virtio_transport_stream_is_active(struct vsock_sock *vsk); bool virtio_transport_stream_allow(u32 cid, u32 port); -bool virtio_transport_dgram_allow(u32 cid, u32 port); void virtio_transport_dgram_addr_init(struct sk_buff *skb, struct sockaddr_vm *addr); diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index ac2126c7dac5..713718861bd4 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -63,6 +63,7 @@ struct virtio_vsock { u32 guest_cid; bool seqpacket_allow; + bool dgram_allow; }; static u32 virtio_transport_get_local_cid(void) @@ -413,6 +414,7 @@ static void virtio_vsock_rx_done(struct virtqueue *vq) queue_work(virtio_vsock_workqueue, &vsock->rx_work); } +static bool virtio_transport_dgram_allow(u32 cid, u32 port); static bool virtio_transport_seqpacket_allow(u32 remote_cid); static struct virtio_transport virtio_transport = { @@ -430,6 +432,7 @@ static struct virtio_transport virtio_transport = { .dgram_enqueue = virtio_transport_dgram_enqueue, .dgram_allow = virtio_transport_dgram_allow, + .dgram_addr_init = virtio_transport_dgram_addr_init, .stream_dequeue = virtio_transport_stream_dequeue, .stream_enqueue = virtio_transport_stream_enqueue, @@ -462,6 +465,21 @@ static struct virtio_transport virtio_transport = { .send_pkt = virtio_transport_send_pkt, }; +static bool virtio_transport_dgram_allow(u32 cid, u32 port) +{ + struct virtio_vsock *vsock; + bool dgram_allow; + + dgram_allow = false; + rcu_read_lock(); + vsock = rcu_dereference(the_virtio_vsock); + if (vsock) + dgram_allow = vsock->dgram_allow; + rcu_read_unlock(); + + return dgram_allow; +} + static bool virtio_transport_seqpacket_allow(u32 remote_cid) { struct virtio_vsock *vsock; @@ -655,6 +673,9 @@ static int virtio_vsock_probe(struct virtio_device *vdev) if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_SEQPACKET)) vsock->seqpacket_allow = true; + if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_DGRAM)) + vsock->dgram_allow = true; + vdev->priv = vsock; ret = virtio_vsock_vqs_init(vsock); @@ -747,7 +768,8 @@ static struct virtio_device_id id_table[] = { }; static unsigned int features[] = { - VIRTIO_VSOCK_F_SEQPACKET + VIRTIO_VSOCK_F_SEQPACKET, + VIRTIO_VSOCK_F_DGRAM }; static struct virtio_driver virtio_vsock_driver = { diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index 96118e258097..77898f5325cd 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -783,12 +783,6 @@ bool virtio_transport_stream_allow(u32 cid, u32 port) } EXPORT_SYMBOL_GPL(virtio_transport_stream_allow); -bool virtio_transport_dgram_allow(u32 cid, u32 port) -{ - return false; -} -EXPORT_SYMBOL_GPL(virtio_transport_dgram_allow); - int virtio_transport_connect(struct vsock_sock *vsk) { struct virtio_vsock_pkt_info info = { From patchwork Wed Jul 19 00:50:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122349 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2122112vqt; Tue, 18 Jul 2023 18:18:21 -0700 (PDT) X-Google-Smtp-Source: APBJJlHrY88RM0d86kV87Ro+a4wOJmpdEqGIcyr+Sb6sH3k8lLYZuKCgaBWYW+yQunHYeaJsfspe X-Received: by 2002:a17:902:744b:b0:1b8:1c9e:442c with SMTP id e11-20020a170902744b00b001b81c9e442cmr12961867plt.20.1689729501519; Tue, 18 Jul 2023 18:18:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689729501; cv=none; d=google.com; s=arc-20160816; b=kC2A9aGmxrtTcXIrTfkwrhDTXO2C5YgQrrlhSfQd6jBxlkh/k5TP/n7UFv2aNamubL tVi4hHtR/PtVuGUexCVpIgfpeYMBPe2hYbFFNethGI/8c+HPuqedxPj9z8iHU8AcVt00 VDfAEVmtquQKC8hW9H28YA7r0WO+2iamaBPLbcdAjtQHCTZ2rEPzPpluLtqQSjzPnezb cPD9yET3kn+pvLZTuCMKJbXedDWzM9eIFWy9hq80FihIJPL+u2r81AhgpLwpEQpRDSqW 7V7oID/klByZtKCDy+NutBBw9JS0IxJQ4aHFXrp0VMX+tvgDtvJ+0vCV2kzshhSmTh0U n8IA== 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=Zcl4aN/43X4Lf9KimpBG0GZiJLNoqreLRjXoCG4dakY=; fh=mG5Sox+GcIzYFDJZVrRRBLlD2ICT0t2fGmdMMv0dhmY=; b=DZVIIw/cdAfhV6U2Mgxh199lvyCiSAELgx1ToR/pLHfsQlYkDmV/VqvdfQ4XB5/DfK 55RO5AgVgXxTR6LBx5dA5adgZJH6bBfc7IcPHgcSaUxHKHh9MqYkMivrgaHGQUI7SRxJ T7gel69vIpAeDAlhaGFWqxqKB3Mdoe3qw9gS27yHShfGxLAaK0y0Q8jLSzyzc/Ib2Rb5 Hk0huVEg6ZBimfEZVX7zxx0n7AtJOigLitxy2JLLCb+IX9+P53n/XBD3/oHoOLL7qx6Q tpXTTX6IQCJ8pW4kMEJ91ItaJAqaU52cfIsxjNJJdgoD3Bz9GiBgw+zBEN4dBTVtcWKm +rkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=HoM9cbyI; 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 t18-20020a17090340d200b001b25ade9ea3si2496018pld.337.2023.07.18.18.18.07; Tue, 18 Jul 2023 18:18:21 -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=HoM9cbyI; 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 S229661AbjGSAu7 (ORCPT + 99 others); Tue, 18 Jul 2023 20:50:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47022 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229875AbjGSAul (ORCPT ); Tue, 18 Jul 2023 20:50:41 -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 98B391FD3 for ; Tue, 18 Jul 2023 17:50:22 -0700 (PDT) Received: by mail-qk1-x72a.google.com with SMTP id af79cd13be357-7658430eb5dso692929385a.2 for ; Tue, 18 Jul 2023 17:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727821; x=1692319821; 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=Zcl4aN/43X4Lf9KimpBG0GZiJLNoqreLRjXoCG4dakY=; b=HoM9cbyI5odaUiKbIqWF35MzV6k1piobOfjvDaSFB9OOJS1Yx4Z+K6kIPk7ETDiTzw d/BEP+F2Vq7VE7SwglxHtkmOyah0R4rKrMTv1WFPOGAxmPlVkkAq9LYs8pnU2hpIYNq3 qxE/B+1A89StKhWCZoSo1xIRsAOj3ZfCQoq4j0WopnOueetRbBr0DyiAp/Cdr9i0Nxnv yF/7JsU1pTbRTnncKkWPb+HxalipkEp4pEspbEtwITisOatAEsoM0w55Ax7u7p5i1W9J koCu8uSPpJ4uZbCyo2CCJmHzuUUxbR2MbGgzDJxeC90hDwd2aKcwtx99mSZPUMZwi6ei AIMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727821; x=1692319821; 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=Zcl4aN/43X4Lf9KimpBG0GZiJLNoqreLRjXoCG4dakY=; b=J617rFo/QPTU/eejfrr6P0wX1mH+tU7sVunKioNqtm8um/CyW4pI9ZkceyYRnmlKca puIUX48b0/eIcJqrridTaNyqpivJlHxusHuJ+AekMTAkvijRLewGketo0YIIZcAmAepq alHzXJqr7X7es4/cAh4mNhswy1Uc3WXOh2ElYnicBcaM5ZPkfxOOsa+KIIRyjqrxADpU nCJqFFIjVoXmuUunZq6PfcKBhJYm8GcRDbc9db4HV7eTQPpEDAXXc5VLwJ6UGclAkE1x pA3xePfi8JkxS2jZHZhZnqnd5FTKebay3fKVFBkRZ7apiohCphwFMs6z0bLarAg572Z8 F87g== X-Gm-Message-State: ABy/qLZo0QH7W/k2sTtRTR3BryGsJODk+8TlBiNe6Meb5LWn40grM+OG D/0yw0ROL2FTn4nfN3fr/6FfwQ== X-Received: by 2002:a05:620a:1a09:b0:767:39c5:652d with SMTP id bk9-20020a05620a1a0900b0076739c5652dmr6310672qkb.64.1689727821151; Tue, 18 Jul 2023 17:50:21 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:20 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:18 +0000 Subject: [PATCH RFC net-next v5 14/14] test/vsock: add vsock dgram tests MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-14-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 , Jiang Wang 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: 1771809801711590154 X-GMAIL-MSGID: 1771809801711590154 From: Jiang Wang This commit adds tests for vsock datagram. Signed-off-by: Bobby Eshleman Signed-off-by: Jiang Wang --- tools/testing/vsock/util.c | 141 +++++++- tools/testing/vsock/util.h | 6 + tools/testing/vsock/vsock_test.c | 680 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 826 insertions(+), 1 deletion(-) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 01b636d3039a..811e70d7cf1e 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -99,7 +99,8 @@ static int vsock_connect(unsigned int cid, unsigned int port, int type) int ret; int fd; - control_expectln("LISTENING"); + if (type != SOCK_DGRAM) + control_expectln("LISTENING"); fd = socket(AF_VSOCK, type, 0); @@ -130,6 +131,11 @@ int vsock_seqpacket_connect(unsigned int cid, unsigned int port) return vsock_connect(cid, port, SOCK_SEQPACKET); } +int vsock_dgram_connect(unsigned int cid, unsigned int port) +{ + return vsock_connect(cid, port, SOCK_DGRAM); +} + /* Listen on and return the first incoming connection. The remote * address is stored to clientaddrp. clientaddrp may be NULL. */ @@ -211,6 +217,34 @@ int vsock_seqpacket_accept(unsigned int cid, unsigned int port, return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET); } +int vsock_dgram_bind(unsigned int cid, unsigned int port) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = port, + .svm_cid = cid, + }, + }; + int fd; + + fd = socket(AF_VSOCK, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + return fd; +} + /* Transmit one byte and check the return value. * * expected_ret: @@ -260,6 +294,57 @@ void send_byte(int fd, int expected_ret, int flags) } } +/* Transmit one byte and check the return value. + * + * expected_ret: + * <0 Negative errno (for testing errors) + * 0 End-of-file + * 1 Success + */ +void sendto_byte(int fd, const struct sockaddr *dest_addr, int len, int expected_ret, + int flags) +{ + const uint8_t byte = 'A'; + ssize_t nwritten; + + timeout_begin(TIMEOUT); + do { + nwritten = sendto(fd, &byte, sizeof(byte), flags, dest_addr, + len); + timeout_check("write"); + } while (nwritten < 0 && errno == EINTR); + timeout_end(); + + if (expected_ret < 0) { + if (nwritten != -1) { + fprintf(stderr, "bogus sendto(2) return value %zd\n", + nwritten); + exit(EXIT_FAILURE); + } + if (errno != -expected_ret) { + perror("write"); + exit(EXIT_FAILURE); + } + return; + } + + if (nwritten < 0) { + perror("write"); + exit(EXIT_FAILURE); + } + if (nwritten == 0) { + if (expected_ret == 0) + return; + + fprintf(stderr, "unexpected EOF while sending byte\n"); + exit(EXIT_FAILURE); + } + if (nwritten != sizeof(byte)) { + fprintf(stderr, "bogus sendto(2) return value %zd\n", nwritten); + exit(EXIT_FAILURE); + } +} + /* Receive one byte and check the return value. * * expected_ret: @@ -313,6 +398,60 @@ void recv_byte(int fd, int expected_ret, int flags) } } +/* Receive one byte and check the return value. + * + * expected_ret: + * <0 Negative errno (for testing errors) + * 0 End-of-file + * 1 Success + */ +void recvfrom_byte(int fd, struct sockaddr *src_addr, socklen_t *addrlen, + int expected_ret, int flags) +{ + uint8_t byte; + ssize_t nread; + + timeout_begin(TIMEOUT); + do { + nread = recvfrom(fd, &byte, sizeof(byte), flags, src_addr, addrlen); + timeout_check("read"); + } while (nread < 0 && errno == EINTR); + timeout_end(); + + if (expected_ret < 0) { + if (nread != -1) { + fprintf(stderr, "bogus recvfrom(2) return value %zd\n", + nread); + exit(EXIT_FAILURE); + } + if (errno != -expected_ret) { + perror("read"); + exit(EXIT_FAILURE); + } + return; + } + + if (nread < 0) { + perror("read"); + exit(EXIT_FAILURE); + } + if (nread == 0) { + if (expected_ret == 0) + return; + + fprintf(stderr, "unexpected EOF while receiving byte\n"); + exit(EXIT_FAILURE); + } + if (nread != sizeof(byte)) { + fprintf(stderr, "bogus recvfrom(2) return value %zd\n", nread); + exit(EXIT_FAILURE); + } + if (byte != 'A') { + fprintf(stderr, "unexpected byte read %c\n", byte); + exit(EXIT_FAILURE); + } +} + /* Run test cases. The program terminates if a failure occurs. */ void run_tests(const struct test_case *test_cases, const struct test_opts *opts) diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index fb99208a95ea..a69e128d120c 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -37,13 +37,19 @@ void init_signals(void); unsigned int parse_cid(const char *str); int vsock_stream_connect(unsigned int cid, unsigned int port); int vsock_seqpacket_connect(unsigned int cid, unsigned int port); +int vsock_dgram_connect(unsigned int cid, unsigned int port); int vsock_stream_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp); int vsock_seqpacket_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp); +int vsock_dgram_bind(unsigned int cid, unsigned int port); void vsock_wait_remote_close(int fd); void send_byte(int fd, int expected_ret, int flags); +void sendto_byte(int fd, const struct sockaddr *dest_addr, int len, int expected_ret, + int flags); void recv_byte(int fd, int expected_ret, int flags); +void recvfrom_byte(int fd, struct sockaddr *src_addr, socklen_t *addrlen, + int expected_ret, int flags); void run_tests(const struct test_case *test_cases, const struct test_opts *opts); void list_tests(const struct test_case *test_cases); diff --git a/tools/testing/vsock/vsock_test.c b/tools/testing/vsock/vsock_test.c index ac1bd3ac1533..c9904a3376ce 100644 --- a/tools/testing/vsock/vsock_test.c +++ b/tools/testing/vsock/vsock_test.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,12 @@ #include "control.h" #include "util.h" +#ifndef SOL_VSOCK +#define SOL_VSOCK 287 +#endif + +#define DGRAM_MSG_CNT 16 + static void test_stream_connection_reset(const struct test_opts *opts) { union { @@ -1053,6 +1060,644 @@ static void test_stream_virtio_skb_merge_server(const struct test_opts *opts) close(fd); } +static void test_dgram_sendto_client(const struct test_opts *opts) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = 1234, + .svm_cid = opts->peer_cid, + }, + }; + int fd; + + /* Wait for the server to be ready */ + control_expectln("BIND"); + + fd = socket(AF_VSOCK, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + sendto_byte(fd, &addr.sa, sizeof(addr.svm), 1, 0); + + /* Notify the server that the client has finished */ + control_writeln("DONE"); + + close(fd); +} + +static void test_dgram_sendto_server(const struct test_opts *opts) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = 1234, + .svm_cid = VMADDR_CID_ANY, + }, + }; + unsigned long sock_buf_size; + int len = sizeof(addr.sa); + int fd; + + fd = socket(AF_VSOCK, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + /* Set receive buffer to maximum */ + sock_buf_size = -1; + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, + &sock_buf_size, sizeof(sock_buf_size))) { + perror("setsockopt(SO_RECVBUF)"); + exit(EXIT_FAILURE); + } + + /* Notify the client that the server is ready */ + control_writeln("BIND"); + + recvfrom_byte(fd, &addr.sa, &len, 1, 0); + + /* Wait for the client to finish */ + control_expectln("DONE"); + + close(fd); +} + +static void test_dgram_connect_client(const struct test_opts *opts) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = 1234, + .svm_cid = opts->peer_cid, + }, + }; + int ret; + int fd; + + /* Wait for the server to be ready */ + control_expectln("BIND"); + + fd = socket(AF_VSOCK, SOCK_DGRAM, 0); + if (fd < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + ret = connect(fd, &addr.sa, sizeof(addr.svm)); + if (ret < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + send_byte(fd, 1, 0); + + /* Notify the server that the client has finished */ + control_writeln("DONE"); + + close(fd); +} + +static void test_dgram_connect_server(const struct test_opts *opts) +{ + test_dgram_sendto_server(opts); +} + +static void test_dgram_multiconn_sendto_client(const struct test_opts *opts) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = 1234, + .svm_cid = opts->peer_cid, + }, + }; + int fds[MULTICONN_NFDS]; + int i; + + /* Wait for the server to be ready */ + control_expectln("BIND"); + + for (i = 0; i < MULTICONN_NFDS; i++) { + fds[i] = socket(AF_VSOCK, SOCK_DGRAM, 0); + if (fds[i] < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + } + + for (i = 0; i < MULTICONN_NFDS; i++) { + sendto_byte(fds[i], &addr.sa, sizeof(addr.svm), 1, 0); + + /* This is here to make explicit the case of the test failing + * due to packet loss. The test fails when recv() times out + * otherwise, which is much more confusing. + */ + control_expectln("PKTRECV"); + } + + /* Notify the server that the client has finished */ + control_writeln("DONE"); + + for (i = 0; i < MULTICONN_NFDS; i++) + close(fds[i]); +} + +static void test_dgram_multiconn_sendto_server(const struct test_opts *opts) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = 1234, + .svm_cid = VMADDR_CID_ANY, + }, + }; + int len = sizeof(addr.sa); + int fd; + int i; + + fd = socket(AF_VSOCK, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + /* Notify the client that the server is ready */ + control_writeln("BIND"); + + for (i = 0; i < MULTICONN_NFDS; i++) { + recvfrom_byte(fd, &addr.sa, &len, 1, 0); + control_writeln("PKTRECV"); + } + + /* Wait for the client to finish */ + control_expectln("DONE"); + + close(fd); +} + +static void test_dgram_multiconn_send_client(const struct test_opts *opts) +{ + int fds[MULTICONN_NFDS]; + int i; + + /* Wait for the server to be ready */ + control_expectln("BIND"); + + for (i = 0; i < MULTICONN_NFDS; i++) { + fds[i] = vsock_dgram_connect(opts->peer_cid, 1234); + if (fds[i] < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + } + + for (i = 0; i < MULTICONN_NFDS; i++) { + send_byte(fds[i], 1, 0); + /* This is here to make explicit the case of the test failing + * due to packet loss. + */ + control_expectln("PKTRECV"); + } + + /* Notify the server that the client has finished */ + control_writeln("DONE"); + + for (i = 0; i < MULTICONN_NFDS; i++) + close(fds[i]); +} + +static void test_dgram_multiconn_send_server(const struct test_opts *opts) +{ + union { + struct sockaddr sa; + struct sockaddr_vm svm; + } addr = { + .svm = { + .svm_family = AF_VSOCK, + .svm_port = 1234, + .svm_cid = VMADDR_CID_ANY, + }, + }; + unsigned long sock_buf_size; + int fd; + int i; + + fd = socket(AF_VSOCK, SOCK_DGRAM, 0); + if (fd < 0) { + perror("socket"); + exit(EXIT_FAILURE); + } + + if (bind(fd, &addr.sa, sizeof(addr.svm)) < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + /* Set receive buffer to maximum */ + sock_buf_size = -1; + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, + &sock_buf_size, sizeof(sock_buf_size))) { + perror("setsockopt(SO_RECVBUF)"); + exit(EXIT_FAILURE); + } + + /* Notify the client that the server is ready */ + control_writeln("BIND"); + + for (i = 0; i < MULTICONN_NFDS; i++) { + recv_byte(fd, 1, 0); + control_writeln("PKTRECV"); + } + + /* Wait for the client to finish */ + control_expectln("DONE"); + + close(fd); +} + +/* + * This test is similar to the seqpacket msg bounds tests, but it is unreliable + * because it may also fail in the unlikely case that packets are dropped. + */ +static void test_dgram_bounds_unreliable_client(const struct test_opts *opts) +{ + unsigned long recv_buf_size; + unsigned long *hashes; + int page_size; + int fd; + int i; + + fd = vsock_dgram_connect(opts->peer_cid, 1234); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + hashes = malloc(DGRAM_MSG_CNT * sizeof(unsigned long)); + if (!hashes) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + /* Let the server know the client is ready */ + control_writeln("CLNTREADY"); + + /* Wait, until receiver sets buffer size. */ + control_expectln("SRVREADY"); + + recv_buf_size = control_readulong(); + + page_size = getpagesize(); + + for (i = 0; i < DGRAM_MSG_CNT; i++) { + ssize_t send_size; + size_t buf_size; + void *buf; + + /* Use "small" buffers and "big" buffers. */ + if (opts->peer_cid <= VMADDR_CID_HOST && (i & 1)) + buf_size = page_size + + (rand() % (MAX_MSG_SIZE - page_size)); + else + buf_size = 1 + (rand() % page_size); + + buf_size = min(buf_size, recv_buf_size); + + buf = malloc(buf_size); + + if (!buf) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + memset(buf, rand() & 0xff, buf_size); + + send_size = send(fd, buf, buf_size, 0); + if (send_size < 0) { + perror("send"); + exit(EXIT_FAILURE); + } + + if (send_size != buf_size) { + fprintf(stderr, "Invalid send size\n"); + exit(EXIT_FAILURE); + } + + /* In theory the implementation isn't required to transmit + * these packets in order, so we use this PKTSENT/PKTRECV + * message sequence so that server and client coordinate + * sending and receiving one packet at a time. The client sends + * a packet and waits until it has been received before sending + * another. + * + * Also in theory these packets can be lost and the test will + * fail for that reason. + */ + control_writeln("PKTSENT"); + control_expectln("PKTRECV"); + + /* Send the server a hash of the packet */ + hashes[i] = hash_djb2(buf, buf_size); + free(buf); + } + + control_writeln("SENDDONE"); + close(fd); + + for (i = 0; i < DGRAM_MSG_CNT; i++) { + if (hashes[i] != control_readulong()) + fprintf(stderr, "broken dgram message bounds or packet loss\n"); + } + free(hashes); +} + +static void test_dgram_bounds_unreliable_server(const struct test_opts *opts) +{ + unsigned long hashes[DGRAM_MSG_CNT]; + unsigned long sock_buf_size; + struct msghdr msg = {0}; + struct iovec iov = {0}; + char buf[MAX_MSG_SIZE]; + socklen_t len; + int fd; + int i; + + fd = vsock_dgram_bind(VMADDR_CID_ANY, 1234); + if (fd < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + /* Set receive buffer to maximum */ + sock_buf_size = -1; + if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, + &sock_buf_size, sizeof(sock_buf_size))) { + perror("setsockopt(SO_RECVBUF)"); + exit(EXIT_FAILURE); + } + + /* Retrieve the receive buffer size */ + len = sizeof(sock_buf_size); + if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, + &sock_buf_size, &len)) { + perror("getsockopt(SO_RECVBUF)"); + exit(EXIT_FAILURE); + } + + /* Client ready to receive parameters */ + control_expectln("CLNTREADY"); + + /* Ready to receive data. */ + control_writeln("SRVREADY"); + + if (opts->peer_cid > VMADDR_CID_HOST) + control_writeulong(sock_buf_size); + else + control_writeulong(getpagesize()); + + iov.iov_base = buf; + iov.iov_len = sizeof(buf); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + + for (i = 0; i < DGRAM_MSG_CNT; i++) { + ssize_t recv_size; + + control_expectln("PKTSENT"); + recv_size = recvmsg(fd, &msg, 0); + control_writeln("PKTRECV"); + + if (!recv_size) + break; + + if (recv_size < 0) { + perror("recvmsg"); + exit(EXIT_FAILURE); + } + + hashes[i] = hash_djb2(msg.msg_iov[0].iov_base, recv_size); + } + + control_expectln("SENDDONE"); + + close(fd); + + for (i = 0; i < DGRAM_MSG_CNT; i++) + control_writeulong(hashes[i]); +} + +#define POLL_TIMEOUT_MS 1000 +void vsock_recv_error(int fd) +{ + struct sock_extended_err *serr; + struct msghdr msg = { 0 }; + struct pollfd fds = { 0 }; + char cmsg_data[128]; + struct cmsghdr *cm; + ssize_t res; + + fds.fd = fd; + fds.events = 0; + + if (poll(&fds, 1, POLL_TIMEOUT_MS) < 0) { + perror("poll"); + exit(EXIT_FAILURE); + } + + if (!(fds.revents & POLLERR)) { + fprintf(stderr, "POLLERR expected\n"); + exit(EXIT_FAILURE); + } + + msg.msg_control = cmsg_data; + msg.msg_controllen = sizeof(cmsg_data); + + res = recvmsg(fd, &msg, MSG_ERRQUEUE); + if (res) { + fprintf(stderr, "failed to read error queue: %zi\n", res); + exit(EXIT_FAILURE); + } + + cm = CMSG_FIRSTHDR(&msg); + if (!cm) { + fprintf(stderr, "cmsg: no cmsg\n"); + exit(EXIT_FAILURE); + } + + if (cm->cmsg_level != SOL_VSOCK) { + fprintf(stderr, "cmsg: unexpected 'cmsg_level'\n"); + exit(EXIT_FAILURE); + } + + if (cm->cmsg_type != 0) { + fprintf(stderr, "cmsg: unexpected 'cmsg_type'\n"); + exit(EXIT_FAILURE); + } + + serr = (void *)CMSG_DATA(cm); + if (serr->ee_origin != 0) { + fprintf(stderr, "serr: unexpected 'ee_origin'\n"); + exit(EXIT_FAILURE); + } + + if (serr->ee_errno != EHOSTUNREACH) { + fprintf(stderr, "serr: wrong error code: %u\n", serr->ee_errno); + exit(EXIT_FAILURE); + } +} + +/* + * Attempt to send a packet larger than the client's RX buffer. Test that the + * packet was dropped and that there is an error in the error queue. + */ +static void test_dgram_drop_big_packets_server(const struct test_opts *opts) +{ + unsigned long client_rx_buf_size; + size_t buf_size; + void *buf; + int fd; + + if (opts->peer_cid <= VMADDR_CID_HOST) { + printf("The server's peer must be a guest (not CID %u), skipped...\n", + opts->peer_cid); + return; + } + + /* Wait for the server to be ready */ + control_expectln("READY"); + + fd = vsock_dgram_connect(opts->peer_cid, 1234); + if (fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + client_rx_buf_size = control_readulong(); + + buf_size = client_rx_buf_size + 1; + buf = malloc(buf_size); + if (!buf) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + /* Even though the buffer is exceeded, the send() should still succeed. */ + if (send(fd, buf, buf_size, 0) < 0) { + perror("send"); + exit(EXIT_FAILURE); + } + + vsock_recv_error(fd); + + /* Notify the server that the client has finished */ + control_writeln("DONE"); + + close(fd); +} + +static void test_dgram_drop_big_packets_client(const struct test_opts *opts) +{ + unsigned long buf_size = getpagesize(); + + if (opts->peer_cid > VMADDR_CID_HOST) { + printf("The client's peer must be the host (not CID %u), skipped...\n", + opts->peer_cid); + return; + } + + control_writeln("READY"); + control_writeulong(buf_size); + control_expectln("DONE"); +} + +static void test_stream_dgram_address_collision_client(const struct test_opts *opts) +{ + int dgram_fd, stream_fd; + + stream_fd = vsock_stream_connect(opts->peer_cid, 1234); + if (stream_fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + /* This simply tests if connect() causes address collision client-side. + * Keep in mind that there is no exchange of packets with the + * bound socket on the server. + */ + dgram_fd = vsock_dgram_connect(opts->peer_cid, 1234); + if (dgram_fd < 0) { + perror("connect"); + exit(EXIT_FAILURE); + } + + close(stream_fd); + close(dgram_fd); + + /* Notify the server that the client has finished */ + control_writeln("DONE"); +} + +static void test_stream_dgram_address_collision_server(const struct test_opts *opts) +{ + int dgram_fd, stream_fd; + struct sockaddr_vm addr; + socklen_t addrlen; + + stream_fd = vsock_stream_accept(VMADDR_CID_ANY, 1234, 0); + if (stream_fd < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + /* Retrieve the CID/port for re-use. */ + addrlen = sizeof(addr); + if (getsockname(stream_fd, (struct sockaddr *)&addr, &addrlen)) { + perror("getsockname"); + exit(EXIT_FAILURE); + } + + /* See not in the client function about the pairwise connect call. */ + dgram_fd = vsock_dgram_bind(addr.svm_cid, addr.svm_port); + if (dgram_fd < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } + + control_expectln("DONE"); + + close(stream_fd); + close(dgram_fd); +} + static struct test_case test_cases[] = { { .name = "SOCK_STREAM connection reset", @@ -1128,6 +1773,41 @@ static struct test_case test_cases[] = { .run_client = test_stream_virtio_skb_merge_client, .run_server = test_stream_virtio_skb_merge_server, }, + { + .name = "SOCK_DGRAM client sendto", + .run_client = test_dgram_sendto_client, + .run_server = test_dgram_sendto_server, + }, + { + .name = "SOCK_DGRAM client connect", + .run_client = test_dgram_connect_client, + .run_server = test_dgram_connect_server, + }, + { + .name = "SOCK_DGRAM multiple connections using sendto", + .run_client = test_dgram_multiconn_sendto_client, + .run_server = test_dgram_multiconn_sendto_server, + }, + { + .name = "SOCK_DGRAM multiple connections using send", + .run_client = test_dgram_multiconn_send_client, + .run_server = test_dgram_multiconn_send_server, + }, + { + .name = "SOCK_DGRAM msg bounds unreliable", + .run_client = test_dgram_bounds_unreliable_client, + .run_server = test_dgram_bounds_unreliable_server, + }, + { + .name = "SOCK_DGRAM drop big packets", + .run_client = test_dgram_drop_big_packets_client, + .run_server = test_dgram_drop_big_packets_server, + }, + { + .name = "SOCK_STREAM and SOCK_DGRAM address collision", + .run_client = test_stream_dgram_address_collision_client, + .run_server = test_stream_dgram_address_collision_server, + }, {}, };