From patchwork Wed Jul 10 21:25:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729720 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0CEB5839F3; Wed, 10 Jul 2024 21:25:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646759; cv=none; b=a0W7+xVPE8VjCskddhHxtha8Y8cR/JH0dcT81Zeu7Q7q1uBOxPiNkDIQPgyObGRg4Vb1z6zgUiUpEUGQjCoT0yszdYKYP1Ikl0+ZurZ1s7xcUdPYZvd8ytse5iZu+PRd2xHt4epB+w/Xu7QXDWHuWGPdhn0Mj1qNRcM/c7qcgTk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646759; c=relaxed/simple; bh=2oNBKsr/zKHor9vzhBEXHNQLyKLNT6n5DiR1niLN3c8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VIQgkK7Xp0LRW/rXWSOtCZIVk5sdexX50seE96sQ9232iMIfHlMthasKtf1W4KNw0Zidxe7ILO10OVbe8+OurKRZSriOXFvcyCG2m5sChbZowrlOxrrXb7K4UlxZmQaHzzzkelKwDBoYZPvYIGbMUg6z9qqUSXFwmccOKi8CDpc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=XNrHr5JY; arc=none smtp.client-ip=209.85.222.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XNrHr5JY" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-7a05b4fa525so20546885a.1; Wed, 10 Jul 2024 14:25:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646757; x=1721251557; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KX8KIo3iV7NoMHa3zqkZ0Z+vhF8zraRE7TtvznvQNIg=; b=XNrHr5JYrR4eRytMW7+w6ry/02BpdPUbA3Ocgtn7kHaTvNR0/qw74f8y8ZNS7IhIQS Shr6QJKV0CzE9hXqvFgWEbitBT+4u9Ejv9ftYvCKl6oewGb9vlY6LdKXiYGPEETR2JUu dsz9b4ZQNjikDmyFfOjVqfl+G0cV2lmN5SaTPFy7e/+y3xX0aADMFV0goQ8JHBrue7Mo gBKAlj9v9cVsaWZnHuGIzUmeUmaDz1ZT5uNXnxYsxiZGrMEk40lGW/N/c69mQD/RprXy jZdJTVXx66sj7lPEth2NWViMEMJHxCnd948WLJ0LCF+/ZA/0Vq/qdQ2HI+xxyC9Wu45Y RA8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646757; x=1721251557; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KX8KIo3iV7NoMHa3zqkZ0Z+vhF8zraRE7TtvznvQNIg=; b=PdsN9iSeMSTWAczVlQu/urvOrlleINVYoDt3FhJ0bZM3gc/un6Csn2jCnWgj90kRxb 33hBBRmY8txuyEzcsMKq/BKbZQShUpSQ9Wi64q/uiBONzhV7JzKf1LPPsoSU6NP5M2GK mOx0y4TJK0inPc1IuYP5jp6j2Rh/uKYwEjwBCu/wrhnPsob7+ewZL1KquLy5wkkazRL/ 3B/NIzdjNTNq3W0942RzwZtxVT2x/zJm0iZ/yzSY9NqKgJp3NlI5frLRjAvMO6stkufs 4EC/3onlNqtrn0Qb0ngCwJ0bOTAXQ5b2bxiekh4KhhrmHY617v8iE9RJnrlPEZIl6RpO RvyQ== X-Forwarded-Encrypted: i=1; AJvYcCV0KwqrA3O00J0IbnDJDeppACs4CHnsiNUXqI/Kxdt757qtk0TSqhe+Pflv5vSR7/s6UjCYMncgnlG6P/EFd0/oYLhSdJ6rucQ1iI3m1aieEiGG8XgSTYiGwliyj6DFlmtKEMX/FXbJny5p8WvWGR+uivedPHApQsrRoaj9oR2dYSWeLFdL+nUq/WF5+0nYCZWJV9gj4cMResw8hCdejr282Id7ZKMaqwNLTOWd X-Gm-Message-State: AOJu0YxUquBgRgOpieSYVbv5kVwL2pXObffBq3y5eDZSpAq/bGAxJKNh GQ+7IEwpAf+X4aJx2uLVFfKv4nrKobj/O9ZQGc+8YgGu2Fg68tis X-Google-Smtp-Source: AGHT+IFSj7+MYM52RaS6eZWKXpFPukkOvw7LM6PzzibSNjmPqd57Dl57Ba5FqQJnizrjtsz6ZhSPkQ== X-Received: by 2002:a05:620a:2444:b0:79e:f9f4:3e99 with SMTP id af79cd13be357-7a1469a84d6mr197991885a.1.1720646756780; Wed, 10 Jul 2024 14:25:56 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.25.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:25:56 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 01/14] af_vsock: generalize vsock_dgram_recvmsg() to all transports Date: Wed, 10 Jul 2024 21:25:42 +0000 Message-Id: <20240710212555.1617795-2-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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: - transport in the receiving path now stores the cid and port into the control buffer of an skb when populating an skb. The information later is used to initialize sockaddr_vm structure in recvmsg() without referencing vsk->transport. - 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 Signed-off-by: Amery Hung --- drivers/vhost/vsock.c | 1 - include/linux/virtio_vsock.h | 5 --- include/net/af_vsock.h | 11 ++++- net/vmw_vsock/af_vsock.c | 42 +++++++++++++++++- 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 | 59 +++---------------------- net/vmw_vsock/vsock_loopback.c | 1 - 9 files changed, 55 insertions(+), 81 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index ec20ecff85c7..97fffa914e66 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -419,7 +419,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 c82089dee0c8..8b56b8a19ddd 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -177,11 +177,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 535701efc1e5..7aa1f5f2b1a5 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -120,8 +120,6 @@ 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); @@ -219,6 +217,15 @@ void vsock_for_each_connected_socket(struct vsock_transport *transport, int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk); bool vsock_find_cid(unsigned int cid); +struct vsock_skb_cb { + unsigned int src_cid; + unsigned int src_port; +}; + +static inline struct vsock_skb_cb *vsock_skb_cb(struct sk_buff *skb) { + return (struct vsock_skb_cb *)skb->cb; +}; + /**** TAP ****/ struct vsock_tap { diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index 4b040285aa78..5e7d4d99ea2c 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1273,11 +1273,15 @@ static int vsock_dgram_connect(struct socket *sock, int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t len, int flags) { + struct vsock_skb_cb *vsock_cb; #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); @@ -1288,7 +1292,43 @@ 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; + + if (unlikely(flags & MSG_ERRQUEUE)) + return sock_recv_errqueue(sk, msg, len, SOL_VSOCK, 0); + + /* 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); + + vsock_cb = vsock_skb_cb(skb); + vsock_addr_init(vm_addr, vsock_cb->src_cid, vsock_cb->src_port); + 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 e2157e387217..326dd41ee2d5 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 43d405298857..a8c97e95622a 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -508,7 +508,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 16ff976a86e3..4bf73d20c12a 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -810,15 +810,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..b39df3ed8c8d 100644 --- a/net/vmw_vsock/vmci_transport.c +++ b/net/vmw_vsock/vmci_transport.c @@ -610,6 +610,7 @@ vmci_transport_datagram_create_hnd(u32 resource_id, static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg) { + struct vsock_skb_cb *vsock_cb; struct sock *sk; size_t size; struct sk_buff *skb; @@ -637,10 +638,14 @@ static int vmci_transport_recv_dgram_cb(void *data, struct vmci_datagram *dg) if (!skb) return VMCI_ERROR_NO_MEM; + vsock_cb = vsock_skb_cb(skb); + vsock_cb->src_cid = dg->src.context; + vsock_cb->src_port = dg->src.resource; /* sk_receive_skb() will do a sock_put(), so hold here. */ 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,59 +1736,6 @@ 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) -{ - 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; - } - - /* 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; - -out: - skb_free_datagram(&vsk->sk, skb); - return err; -} - static bool vmci_transport_dgram_allow(u32 cid, u32 port) { if (cid == VMADDR_CID_HYPERVISOR) { @@ -2040,7 +1992,6 @@ 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, .stream_dequeue = vmci_transport_stream_dequeue, diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 6dea6119f5b2..11488887a5cc 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -66,7 +66,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 10 21:25:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729721 Received: from mail-yw1-f176.google.com (mail-yw1-f176.google.com [209.85.128.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 88A5284A36; Wed, 10 Jul 2024 21:25:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646760; cv=none; b=Q/x3ljVNmxqtDf0F2VlqtN01XgmyJxussvx0l6+USF4b9FIztGhFOZQcM1Pfu04DliNvxU0OnFnl3KhNXr3iheIIVeOldHj/Wa3GDeR4I5eiwbvHpt1G04TFMfeGDyAUTYYRTR6ETRIuNqOYEdUCUTGzryS5cmB2i+Usj9wC6a4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646760; c=relaxed/simple; bh=4OTZt5riyKFHkA+zfse2GDqT3SsWJIfaNWPsNxmi0Kw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WgN4Zzqx/Cbpsen1d7Q5njqNL4Bu9QgTovfOPe+KoLKnhe5QFcyLrrUX/XmgZ0O1hcWd1NshdChDaRkRRRbfDElFEqVjFvQcx66me+lrQ0I1UFsyOYEyuqjKaC/8aQjTBCutQMHr7rA84UHaopEKKSBympbgWFYKwd2eBmLleoc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=eOa1JYOf; arc=none smtp.client-ip=209.85.128.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eOa1JYOf" Received: by mail-yw1-f176.google.com with SMTP id 00721157ae682-64b9f11b92aso2047597b3.1; Wed, 10 Jul 2024 14:25:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646757; x=1721251557; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qHc1ID0rf+ZeWARkVYcA3IYS3VCCqmuUAjyfz62UlIY=; b=eOa1JYOfJinPIvZlnfvzG6kJkSuSEB378N1cxEpphYwKHZnEwvNs+ATu+YoN1Duzd9 TNlM8UVt1rikvT9nZ1ZQRsRBCM+NQhfU02FgRsyxNwYDORx2Qri86wgpsc4caVX88dFv MGSgMaOpjp/6BNNDDnnxLCDfiWuvejidwornql4tYL/ooSknnmRyc3EQwG+ad9eYzSEp us1zP87xibi4U1D6KE3h8cfphMBwk0uAhVcZwohJJZbkLLKVvkqBr9KYocoYqFahjhGt lLriA4y6UV2WwipJbG9mcsQUnarKjlthZ2f9dYg9fLmkoie1+oXJyB1Z6x+eeAeoUtS9 XSsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646757; x=1721251557; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qHc1ID0rf+ZeWARkVYcA3IYS3VCCqmuUAjyfz62UlIY=; b=uH7apLdmBgIwWXudb7CaF0j1bGdZSwJesclp6FJWlQ3cVsfWrZCJQb7TDIfYn6e7XG djtccWxdPcafL0lmB2Aal2gCacGvHdVYjrQiFphtGJCwav/fmRLPKx8TPpnRaOAkm8Lz ba2myFVPLRNsP78Bgb6bOfASeIIFzgAeI4nMEpEDyTka3pKalZG0GkNzERqGpB7RGP7Q wW1058QCunzx4ptBFzA52UQ4LtUMH46sjyt5eX/Oda8hKY3B488qwJuGFXDddaVzU3vC o3fy/y88N2w+b3EPYLspDliJtIgiwiYcfeaAV4zrfRhRvjk0+VdBXg91i+ezmcmPOB7Z K+tw== X-Forwarded-Encrypted: i=1; AJvYcCUWWQnCQrKTwConvYGOiqiNnvPz5G2Z3vy6PUw6tXAgUqm8JAyu/KCCvhzCdTHOrGAL6RFY/NxFkEh+6Al1DR58n2h2VqDyPMAXVvVzNzqV6NzVi6oogQOCCwDanvu4305I74xH5vcPibhZp+eLfUHw8tOWDKfD35HzIdGVOTvCU2b5G/O27dv7VS6SDQP/y4vX9OaptXUu1WjXAH4fv4vIDwdkXbEjtlf8Fnkp X-Gm-Message-State: AOJu0YxWOAEEu/fsaq8zfr7YAq0X1FhbQImt4K2FAXk5KgMyne+Du18n 9kxX+McjVLXx9Q15ZniabGR+K+LDzSryxYfWxbC5mjD+NE9QTQE7 X-Google-Smtp-Source: AGHT+IEjPkFnHsMfkPchn9B+Jdi+q1cCzl1WkWEy9p/OPucpa9Wk0d3OtoJt4Ti9KZQE0OkyHGT/ig== X-Received: by 2002:a81:6982:0:b0:627:a917:bae7 with SMTP id 00721157ae682-658ef43fb82mr66490157b3.30.1720646757528; Wed, 10 Jul 2024 14:25:57 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.25.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:25:57 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 02/14] af_vsock: refactor transport lookup code Date: Wed, 10 Jul 2024 21:25:43 +0000 Message-Id: <20240710212555.1617795-3-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 5e7d4d99ea2c..98d10cd30483 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -424,6 +424,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 @@ -464,13 +480,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 10 21:25:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729723 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8373413C3D5; Wed, 10 Jul 2024 21:25:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646761; cv=none; b=SQ9MhAQUfFktVbawjNalNI14YI6qaDduHp9a2dST+Sc1IgznHLvHohBVnqcMZIwbqz8LAxqkCq05hizeZV4Ytdr8NMpCBf0G+5uXtR0z66k5FNfWXCqavoWV4DQdGS34pWBlVTA0+jdduTepuVFjPmrmA8dJqamw3mhxBL2RL5Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646761; c=relaxed/simple; bh=D7bU0xTCYCxoEESWH79unr00SoYxZC59BgSUj/2UI3Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oTJo6ZGK2e2lYdwqKAV/GaUQctNbZN/9zo+u9CkmcG6Vc7y96IwK3waVMjnLo0/OH6FVsPMfxMYUONUUkY+wbOhNTM4hCy6ubKFl2Ey9bIZQD8cZmA2Xc2doeCqRDB/vgdg2mMeZ6k0JZoLhudKO1wIlSVTALyh3VPzxmjoivGA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hSy21nOG; arc=none smtp.client-ip=209.85.167.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hSy21nOG" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-3d93147ac6bso143967b6e.0; Wed, 10 Jul 2024 14:25:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646758; x=1721251558; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TkNP9Z6IAyHZ8EAIT6XWxpNToC+wJe3BhT7w/VKuCP0=; b=hSy21nOGEXJ+ChsCwn+Bw49UxDS11KqQPvT2Ejes8IOtNkt1FJ7ll4Ui6/bTaWXeX1 awVsRZp3VM5WXj7Y4CuEbuMseQmSam3z77XuB0DZJvrPDGelzj66qS3OO5fhx9DznXe+ KPN0Prq7TMbGku9/Wiq14p7k2MXWhQIchMXjTqcqaMLQqYv8DbnR/QmWpDi1zvq8bwd4 z6u76FAaYQQ1LO8X81o5mrsV7b8FHAGZAdqym9ma1AkEWqq3ca1bCC2jmnNprD6Ik7Co UKB8zBXqbEivw6SgswEdSFufCK3sdPL4S0OVXIbbxLwoebvoyjApmABOpJsifqvs6dYp Knrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646758; x=1721251558; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TkNP9Z6IAyHZ8EAIT6XWxpNToC+wJe3BhT7w/VKuCP0=; b=ZyjVfk/Oz+EkSFuyCYqrX0S/GTh8ywwP96M+/O1I99ONxLp0fskppEpANF8DqwmAKb f4iD0mSGtU8NhM8/B8yAva230rVKt4B8srVkT3+VjhI4vPKPVpR2vO3xF94Xr42qo61H BCSM4KU4G5aePmd/Bd5uWmtVAHnoV6hjhnTiThMUE6aWsArX9gentdDCytfttLnXlrUI p3y4W8y4fb972YjPwBO17vbHHqra54ElMoPecDuRctX/lLdHHzq8Enq3s9SOpmanDwiD oskSYibGi2aiiGOiWPl9dUzlxJc/4yHWYxQJxVvXoT4gcr3EX97EEKs8PUtp6O5aRn/y ZkaQ== X-Forwarded-Encrypted: i=1; AJvYcCUGTYkz+t7GNu/Ks/49HhSyOIKYsyfH8FRW7zkYh5GHEwt/I+UYzf2CRtsdsnyvNQWmLd5+GP5i1A/Mpw16vF6hl8Fsnnn1EgC7HjEPXMxT3jR+VVjo3Mz22H/rZ9G5aUJmU7hvBu+AhuHnSx5/nlcCab8U/zYtZ7UGI2Nplx5KMHWVLC5SyYiBxC3ydwL02UZlsKaJ+YzYLqVay0CSsyEzVBtMYAa+78alyyLG X-Gm-Message-State: AOJu0Yzyi2u6vUGQ1B53UVe8GM1D9PWzkIwloSiphaNw+olLQSMBCQu4 0CUM24F9bXUhJHtdRT6+T04kBcYJx6/ncxzGVTFtgexGjlVq+7h/W4ggAw== X-Google-Smtp-Source: AGHT+IHyv86SB1wKRp+SssFKGtx5File1fFaZVPfEkTbDGohBQbmnEazU/6Dk0ydOGV62SNpzW5JIg== X-Received: by 2002:a05:6808:bd4:b0:3d9:303a:fc6d with SMTP id 5614622812f47-3d93c08a1f4mr6990349b6e.41.1720646758332; Wed, 10 Jul 2024 14:25:58 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.25.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:25:57 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 03/14] af_vsock: support multi-transport datagrams Date: Wed, 10 Jul 2024 21:25:44 +0000 Message-Id: <20240710212555.1617795-4-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman This patch adds support for multi-transport datagrams. This includes: - Allow transport to be undecided (i.e., empty) for non-VMCI datagram use cases during socket creation. - connect() now assigns the transport for (similar to connectible sockets) - 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 * Dynamic transport lookup * virtio datagram will follow h2g/g2h paradigm. Since it is impossible to know which transport to use during socket creation, the transport is allowed to remain empty. The transport will be assigned only when connect() is called. Otherwise, in the sendmsg() path, if sendto() is used, the cid is used to lookup the transport that will be used. In the recvmsg() path, since the receiving method is generalized and shared by different transport, there is now no need to resolve the transport. Finally, a couple of checks for empty transport are added in other paths to prevent null-pointer dereference. * Compatibiliity with VMCI * 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 Signed-off-by: Amery Hung --- drivers/vhost/vsock.c | 1 - include/linux/virtio_vsock.h | 2 - include/net/af_vsock.h | 10 +- net/vmw_vsock/af_vsock.c | 127 +++++++++++++++++++----- 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, 107 insertions(+), 50 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 97fffa914e66..fa1aefb78016 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -419,7 +419,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 8b56b8a19ddd..f749a066af46 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -221,8 +221,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 7aa1f5f2b1a5..44db8f2c507d 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 98d10cd30483..acc15e11700c 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -140,8 +140,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); @@ -440,19 +440,20 @@ vsock_connectible_lookup_transport(unsigned int cid, __u8 flags) 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 - * is set (e.g. during the connect() or when a connection request on a listener - * socket is received). - * The vsk->remote_addr is used to decide which transport to use: - * - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if - * g2h is not loaded, will use local transport; - * - remote CID <= VMADDR_CID_HOST or h2g is not loaded or remote flags field - * includes VMADDR_FLAG_TO_HOST flag value, will use guest->host transport; - * - remote CID > VMADDR_CID_HOST will use host->guest transport; - */ -int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) +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; +} + +static int __vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk, + bool create_sock) { const struct vsock_transport *new_transport; struct sock *sk = sk_vsock(vsk); @@ -476,7 +477,21 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) switch (sk->sk_type) { case SOCK_DGRAM: - new_transport = transport_dgram; + /* During vsock_create(), the transport cannot be decided yet if + * using virtio. While for VMCI, it is transport_dgram_fallback. + * Therefore, we try to initialize it to transport_dgram_fallback + * so that we don't break VMCI. If VMCI is not present, it is okay + * to leave the transport empty since vsk->transport != NULL checks + * will be performed in send and receive paths. + * + * During vsock_dgram_connect(), since remote_cid is available, + * the right transport is assigned after lookup. + */ + if (create_sock) + new_transport = transport_dgram_fallback; + else + new_transport = vsock_dgram_lookup_transport(remote_cid, + remote_flags); break; case SOCK_STREAM: case SOCK_SEQPACKET: @@ -501,6 +516,10 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) vsock_deassign_transport(vsk); } + /* Only allow empty transport during vsock_create() for datagram */ + if (!new_transport && sk->sk_type == SOCK_DGRAM && create_sock) + return 0; + /* We increase the module refcnt to prevent the transport unloading * while there are open sockets assigned to it. */ @@ -525,6 +544,23 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) return 0; } + +/* 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 + * is set (e.g. during the connect() or when a connection request on a listener + * socket is received). + * The vsk->remote_addr is used to decide which transport to use: + * - remote CID == VMADDR_CID_LOCAL or g2h->local_cid or VMADDR_CID_HOST if + * g2h is not loaded, will use local transport; + * - remote CID <= VMADDR_CID_HOST or h2g is not loaded or remote flags field + * includes VMADDR_FLAG_TO_HOST flag value, will use guest->host transport; + * - remote CID > VMADDR_CID_HOST will use host->guest transport; + */ +int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk) +{ + return __vsock_assign_transport(vsk, psk, false); +} EXPORT_SYMBOL_GPL(vsock_assign_transport); bool vsock_find_cid(unsigned int cid) @@ -693,6 +729,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); } @@ -825,6 +864,9 @@ static void __vsock_release(struct sock *sk, int level) vsk->transport->release(vsk); else if (sock_type_connectible(sk->sk_type)) vsock_remove_sock(vsk); + else if (sk->sk_type == SOCK_DGRAM && + (!vsk->transport || !vsk->transport->dgram_bind)) + vsock_remove_sock(vsk); sock_orphan(sk); sk->sk_shutdown = SHUTDOWN_MASK; @@ -1152,6 +1194,9 @@ static int vsock_read_skb(struct sock *sk, skb_read_actor_t read_actor) { struct vsock_sock *vsk = vsock_sk(sk); + if (!vsk->transport) + return -EINVAL; + return vsk->transport->read_skb(vsk, read_actor); } @@ -1163,6 +1208,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; @@ -1174,19 +1220,40 @@ 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); + /* transport_dgram_fallback needs to be initialized to be called */ + if (transport == transport_dgram_fallback && transport != vsk->transport) { + err = -EINVAL; + goto out; + } + + 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. */ @@ -1201,6 +1268,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(); @@ -1225,6 +1293,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; } @@ -1257,13 +1327,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 != @@ -2406,7 +2481,7 @@ static int vsock_create(struct net *net, struct socket *sock, vsk = vsock_sk(sk); if (sock->type == SOCK_DGRAM) { - ret = vsock_assign_transport(vsk, NULL); + ret = __vsock_assign_transport(vsk, NULL, true); if (ret < 0) { sock_put(sk); return ret; @@ -2548,7 +2623,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) { @@ -2567,7 +2642,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; @@ -2585,7 +2660,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: @@ -2604,8 +2679,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 326dd41ee2d5..64ad87a3879c 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 a8c97e95622a..4891b845fcde 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -507,7 +507,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 4bf73d20c12a..a1c76836d798 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1008,13 +1008,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 b39df3ed8c8d..49aba9c48415 100644 --- a/net/vmw_vsock/vmci_transport.c +++ b/net/vmw_vsock/vmci_transport.c @@ -2061,7 +2061,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 11488887a5cc..4dd4886f29d1 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -65,7 +65,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 10 21:25:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729722 Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 310AB1422A8; Wed, 10 Jul 2024 21:26:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646761; cv=none; b=lRZoiQcQQs37426fOS9+F1wNzK463IGPuz7Z6qmAfGdOCkFyL5kCZVyxxbnaiu+Vzzm9Bpa2/xl4zX1GD+D+E2Ek4fn0MS9tHypbIYAGhyUoL44dh4A3CqX8o1C+f+qP6XUggwx2/x4defLVrsIlIItCMo4XmNx2fiYNMG2Qihg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646761; c=relaxed/simple; bh=uhCcNYibbkqcdwdVnInAQFJ9ST2PnHWFbtJHgHboauo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HIM5gYpv+q06qmYY+n0sIKAetSJ7PsPk3V8TnuWNKo/IOr5JFp3se4Kt+l18KK6/Ssrwg7j4wy3TjHsqTs/mehIl9oX85Pwg8Knp9hrbppUaL2UeozBuWI2HeDMFF0zUlqDh6qDBg+LYvIxiUQHL7GO4m35cQCTc8lfSeQEAJUY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=FFpMvWPJ; arc=none smtp.client-ip=209.85.167.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FFpMvWPJ" Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-3d9bcb47182so149545b6e.3; Wed, 10 Jul 2024 14:26:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646759; x=1721251559; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NmXSwxoqzjXnEZ3Wwvngpw8L/cu5lmFAezrwWUUIofg=; b=FFpMvWPJVIPc0AZ5ibgwLJy2z6P2pjrJVL1or2/9XNnMY1ZoaW3PkqVVXug7yk9j4c Xfwu/yH7ChcwXYNHWvm9fYy98GVo5oHm9vI58SOcUkgP2ui3bnZANhGRpQHu/lub1EQl hN4BXDE3eB/MvuEhoqCju2VU49/NxSFD4bXZWT6dfIPyHolzZeBRge9t2yN2oomJfnKQ LWzpx/Pvgopy8RJE12x16hPmrzhSBv6jj8H9VNW3uqcdGRWLp/2he+3TY47k4ao5B1x3 +PfW60nbwrB8apylEy1TnMKGWe9pDSNETY9vqpFAvxo1CBiKZyOs/Q93YGoaP6KG4lG1 EyLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646759; x=1721251559; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NmXSwxoqzjXnEZ3Wwvngpw8L/cu5lmFAezrwWUUIofg=; b=H1AhFnbQ3we/buO0rWbo7FgiJojHQKyNmcFn5m1dc4WyntQ22/FmM/vnOebUBWNBDj orTNT8b1BIYEuxm2TTNSmDzyVKXs5eeGwDKBvvS49k9Qtge9MBhWnRyamsIqAqWY+Sc6 lyi9SY5pXCUmwNoVMhk9TIEKDcrg4MafXKnaIBJ1X6tZrNEl5P1ABE0So3W7SQWgtiVU vrI9cR3erOBbKKCctJV6TIyY4w7Q5giv1PKBqT/QZ2bdf6ktGRsW2zJV6fIWomtwifDE 0w0m54hN6xR0sdoeFIZ0g6l4RuPNlEMMhWSolHQ3VXEhwOPV33tieyyOgDR5HhAsvLau MI/w== X-Forwarded-Encrypted: i=1; AJvYcCWat6svQWZLoayj3xcw3yiqndtIJFRVM3PDne31NQGe8Bz62X0eLNP9ja995SzMDjlO3fPyt5tfe4d6Q51qGqVWiGIc7NjlaJ4OjhKB8xdmm/jm3ys9I2DsfBWI3rF3JiJNKCnlE6L/rUtNCXOpYokNlQutxN5JBhkwltMXPkbUGFaFbW4JmSL98ICAYUuoKkcC06GouiWaKS6SlvnFLbv6xa5BzSyyDR6b3S40 X-Gm-Message-State: AOJu0YzJ5Q73z+L/lmfBGusEybDYnMuvEL2PYy22GPBWT7wvVl+roq8T Jcjze06wNDhdG0knIJWvQJme8C/MnJxK728Sb8TcORlsq66ipUww X-Google-Smtp-Source: AGHT+IHAi58F+5PJOYlliCTAylnZUgBqrqKTaxEPrlPEhkPkEh9JQbful2UvFJHyD7bxBrd2xbJ6sw== X-Received: by 2002:a05:6808:2129:b0:3d9:29c1:be4c with SMTP id 5614622812f47-3d93c07b10amr7068703b6e.38.1720646759158; Wed, 10 Jul 2024 14:25:59 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.25.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:25:58 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 04/14] af_vsock: generalize bind table functions Date: Wed, 10 Jul 2024 21:25:45 +0000 Message-Id: <20240710212555.1617795-5-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 acc15e11700c..d571be9cdbf0 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -232,11 +232,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); @@ -249,6 +250,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) { @@ -671,12 +677,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); @@ -692,7 +704,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; } @@ -709,7 +722,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; } @@ -721,11 +735,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 10 21:25:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729724 Received: from mail-qk1-f169.google.com (mail-qk1-f169.google.com [209.85.222.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 34CDD14A4C8; Wed, 10 Jul 2024 21:26:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646763; cv=none; b=sRinwG6RigwcbuNQqQsWYp+rgl4y7+LhfvMMgnn5CkfO2upwpahmJUSzKWCW3ppOfPS+33kZT0/46HJTZZH5oozwRcF2143kfrhe2s80MtPeREwvKMsvpokLoZ3wn7HaMShExZTNEcDVmyIM9ZLZKeEmjaVMyAP0Rvo3zSuVGgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646763; c=relaxed/simple; bh=LyJH2hBBhnOgPmwJMZXReqHovO0qHkbDTNVOj1OTcAE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=q1Q10HIlEGQwaNgXfpHJ74WTCwadtG0jUyyxBpa6czSbX2EskRF5NN99EIqXbIJ4X8dhrx5+ZaOPtOoWyvArCe5Q24fX1olA2dSShya0EpwBD5UP+JwEakZUeFLf9Hhbigjr5VMPmA1qY1fbObRn3s5hjuPnKvrT5Gk+Hr5Wtgc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hnfiiuMR; arc=none smtp.client-ip=209.85.222.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hnfiiuMR" Received: by mail-qk1-f169.google.com with SMTP id af79cd13be357-79f1be45ca8so14204685a.3; Wed, 10 Jul 2024 14:26:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646760; x=1721251560; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KxKcfYe0aTkG2Qtc9qYf7tn4S8PrvFN6AZU5pXvNhSU=; b=hnfiiuMR82fMXGM8xKw7K4Y1iR7LSqiGSRrHd3FW8keGIhyFU87+6b7TbLPN9b5OTS 9LUbR/5Gf6Ovw1HR39y+hDeddIH2Rtb4igELUunZmtml8Fn77t3z8pM5UA4ShgSzNz5d 25+La4cHsoweeJd1bVhx/CfBr0+MAaP7Y40xlc2JsWlMH/VgEaO2FAWYm29l71uLJE11 EvSfxSYGpXZMjLfY4c0CwLW7YdZ8ticyCfzWmwI4d7JjMEHHGaTwEQJlCQtVi6QhhItS n4kM0Hg8p8SIuw52+EterK7e+tk5oE0dmnYSsQ20v6naCj4Sx8mP0F4CqisdTn05DjtU pRmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646760; x=1721251560; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KxKcfYe0aTkG2Qtc9qYf7tn4S8PrvFN6AZU5pXvNhSU=; b=H6sDc6TRrrQYRvOYiI+EaBw1roUZTbaOGg8zmgSBSTRkUpTF6zYGpTUUQVF1M4SQ3A 84DOBwLziJ7rUNNxOqy+QuNP73MgxAKsMn5kbJcnTXEb9GoJWL16NjjEt1O3TGfc+rIA OA4WGjZQdAWgsRtsjLp1NUCs2FXUmPDfu7NvvKOUIhDYnrY4PYsKgUB4rHE3OF+03vzp mdCeKRZ1oqfl+alIkDqzCAi3CmcFuy8svpd6cbXHNf2zuaF8x/b767RYNKmZs3a6uT0P Co8kt3u9JPAcj7E47UweDtReGOch+3vjWSSgBHGRq/xFkUd/V7W5BqKPc32fhtSwsbyE 6cjg== X-Forwarded-Encrypted: i=1; AJvYcCX5ggMShb91EwO5HOIbfYE+he1N6sAv2sCIdzHwnmuSen1yyo3KRO3im86ycmzL6grSd1JIWgxWT+yfnsJZKh92SPHTuZWIVZag+qqIld0jFBXyENh+5LIC131K4uHpsELgJOgNswa5XlYQgRJgWkBvJZqQHvAgkH97wRnHcTSQaS8H/T4DILlnfiu4m9woEKaEkQD6FdpBA+mxUTyqC5Fl8+Wf1dzqn57XJh8z X-Gm-Message-State: AOJu0Yz3ZFnA2BDXVi+1/h97D3f24f6eZ4rZ60nD0TjkdDnAQIcfzvjY Bt8yMujz0+wx7bvf4u9lOI53R+ucB4OWNvtn2G8V7PO0zN5xkNiO X-Google-Smtp-Source: AGHT+IF6x1tIBL/6HRZ76/k84A0vB251Ob+fzT6he8zcBfDrLo9SHcbTtQSiMBBYeiAZryPfmYl48A== X-Received: by 2002:a05:620a:2186:b0:79c:119e:2b44 with SMTP id af79cd13be357-79f19a51d7fmr733109885a.3.1720646759964; Wed, 10 Jul 2024 14:25:59 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.25.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:25:59 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 05/14] af_vsock: use a separate dgram bind table Date: Wed, 10 Jul 2024 21:25:46 +0000 Message-Id: <20240710212555.1617795-6-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 d571be9cdbf0..ab08cd81720e 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 @@ -116,6 +121,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 = { @@ -152,21 +158,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) \ @@ -182,6 +192,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) @@ -204,6 +216,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, @@ -271,13 +286,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( @@ -289,6 +319,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); @@ -340,7 +378,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); @@ -746,11 +787,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); } @@ -781,7 +830,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 10 21:25:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729725 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D17F814B943; Wed, 10 Jul 2024 21:26:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646763; cv=none; b=N2IEf7ZwvAOOcYr4Q1h61hxdVsKB0eXcLnnqbFRFWPz1F+U237WmYtS/m/1KbYtitoxCHuljvXRBA7ClpeqaR3wrPDb2kdA1qXFR/RNEwBoDCKCitdck+3+BvYeUoXKHc+R5wbBBtq+W33nLfVM5U9pmcq+ZdTww3fDNaw+Nx2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646763; c=relaxed/simple; bh=UfwlPBtk149XzS/tcMuqUXOpP079VnWFi28qU26z4ck=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jcdLL/TOaz7o6XYgK5lHEh4oF8YlApVmC/PTjacrgkTWs9AGvSoOfLU4oaf9VDHvE0T4q61j81SvJO3+5NAgThEhbqDQmXAhc2zcDRoDQWSZI5lBnC9z+ab6nsIChjEvNdGxJpvjFk/6vP+FPTmfmKnm0/oKXO/yJnZutMpNoqM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=hREZIi62; arc=none smtp.client-ip=209.85.222.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hREZIi62" Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-7a05b7c8e38so15094085a.0; Wed, 10 Jul 2024 14:26:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646761; x=1721251561; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BJWi8Leq2jo3M3Yc8XcwnzkcrKsxdOFNGRCXzkErj3E=; b=hREZIi62+exxFbPdFihwK7zhI1JD35yBcnGfImJNcyYmsDZCZxWBu00lxjr4WTkEbM X//gdNVT4JBmwrww60uspbZkVYl1fXkbbWsSzt1c3aYj1D2OBobFxu7suadKXXq1WlY0 AjXBKLq7LjSnxeDfZrDoTjvhOEOBglUQAh6aagoxaYDec2YRFULy+K/7qLaA6VtqNxn3 VMpBKaMyWtdcRkAsF3VMXcglgkgQygS2GNjJAoPa+pg+PWXZjWSPgpogVFvawq+39Y3r vMyz6xXzGOEn1PvnmpZCsOnZZe+i1onYiBegZtuLEcBB9cs0vb6ZCjXd09rP186sZp23 mBGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646761; x=1721251561; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BJWi8Leq2jo3M3Yc8XcwnzkcrKsxdOFNGRCXzkErj3E=; b=sTMG2yFjSp1NnrTwriIGk2LWCPWY2EPnEczBZCYnevFVYcssV55OluAn9raMrhrBrX Ds8A2eHw8WbAwgg40cyNmUyUmY+eGdR4uIaUeYs+65zHmouTUgFrPtqS0uJd00H2Ouyr TQLn2JtrU3pd+PK7c1ytGu/c827pxHe1trdiaV1OzBgeTtJbAD3L+7FENi3vSW5GAuLY 4gAkaICj3ZCzxJEMSXwbDX+uU7rU46ClaXZFVbqKW3DX9qTJaVNcQYuw1jW5cboROQjI MuyVenW/EOkTAkQsszawsQK5743j1cOt5KdxA9Wtu9YpqnOysm855KikJD123RXyxIby rnNg== X-Forwarded-Encrypted: i=1; AJvYcCUk2XCuOWQUhEQ9TXuVAd/KyXRpuTiESGMq1f/GADsgO4xM853gtCx8bcxO8MxT4LwK2s1VsO8ow0K0llKDE2egLvLGVi+NGuZfpg1NgZVHoJxXDNg8fHFUr6Odw5LUHGGpLkSExzTSpAJbEZ2rHb2P7g15dACC3V4j+yHsLdakrZtaFEj5/T8GfyuH2Eo/eyoB4NFisVY51I8pL/ydxcFPwf7f5MuUp2S7QzMs X-Gm-Message-State: AOJu0Ywg5jRlGeU49WOPw66yDbnHbVfbSVYI65BgLFzmAtRAWzwqgy9K TAcPI5TzmWifM0/rmNX1hzNUyn+4tg/Vtzkh6LIzR8gdSuXld1cG X-Google-Smtp-Source: AGHT+IEeRGnHtiCEw+KM2q98xiDSRuWiiJOOgf2jpC5OHvHQB6LFE7ca10rhiE0Y5d3kMoOxfyhvtw== X-Received: by 2002:a05:620a:2186:b0:79c:119e:2b44 with SMTP id af79cd13be357-79f19a51d7fmr733112885a.3.1720646760834; Wed, 10 Jul 2024 14:26:00 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:00 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 06/14] virtio/vsock: add VIRTIO_VSOCK_TYPE_DGRAM Date: Wed, 10 Jul 2024 21:25:47 +0000 Message-Id: <20240710212555.1617795-7-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 10 21:25:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729727 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6652158A03; Wed, 10 Jul 2024 21:26:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646765; cv=none; b=FIEfE04xmKbdcbcoq3Lcsu3b3jUJuV8Id5hr5sGXRCghJR07rg+dBAxGei1u8bWi9EfMUKbhIDmrTASFqmUZuVgVBSux+hP3/LkWvGFdxynF7ljDXqhxal6Z594/cSffD6cMtuvIgdgY8g+vBePAgnCHHA60D9efLKUiRYN8YBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646765; c=relaxed/simple; bh=GXo6JSq/5CxjMKofrwy78dq6F+39cdkj30pXolPW1Us=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bSuic7JTt4iqvtcPNA8/whD5QWhCntM0DIfO+LtT6b1TExJG+Y9XGAHPNrFIVDpe1soEzS3wDhhUpEH6HekTzNRT0XN6HwAFGXEgaEvYaZKQdsMFY2vexpi/89zfW9MPljUuoItGzxQZysG4dWsI1L55Od/TjJzo5aE4+vgWxtk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ncEvEbJu; arc=none smtp.client-ip=209.85.222.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ncEvEbJu" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-79ef93865afso14097385a.1; Wed, 10 Jul 2024 14:26:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646761; x=1721251561; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=jcuXrJuADhlXXbo4cjnK+9NG13fjZ+fX0XEhslHQVp4=; b=ncEvEbJu9f/Gms65RjwE5Ns0Z4nmIYR931isiJPqRYFH9CxEwN08QBxxC1iWie6dGR fKr6LQ7kAF7VKndt0AM0yGk/8ioxy/cgV/3pvuAzApJffu7PlHtCfbE7L04soWUt0pJ2 fm/KHWWwB/QUptSQDbIFly7RLLMs+gBLyXGJ15ZOepmCYfparV7zAC8QLBq0mixZk3WI QMLG5TrxNutuH9WjPqn/Ef5nst0sxYi6WI7bicBpDD6hpYWUcspYEfkgNhvWmWCv1vsi FVPEgdvTRoVKNf6iNrqyscrv2iaRsJBk2uhh4vAJ8Dy6em3quzG5EQo/aoW4jgSL5QOr JIDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646761; x=1721251561; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jcuXrJuADhlXXbo4cjnK+9NG13fjZ+fX0XEhslHQVp4=; b=r+Xdnm32p0q0M0XnIRrsL14JDnXbwAnxB7Yv+U3Zk5Ke0U0+X/rfRtF1BSOc6K+UpB IZSWNjSYVtAf6KHZsJyDWUDiUCRQSJ8J8B66mb4xL/EezdXIo6jEzy6UcTIs4i+7fWCu AJsk8uSjleFCoeUTcaVrzapMr5D1jOpjgXdsEStxlHnLcMs/BByoWPBNEA+hmU7CDyqd 5k3ecC8SgndcSpK53Wme5NhkijJ7kqCaxWd3BTMbTvimypLNg7Kor1KJ9CrkG584A5eZ rg7PeW7yauC1LQ6ARry/vGgg4M/n/Zo1iVhpQ9OWAxeqglnuAx7BqKLEtN9/W1idFrbX /boA== X-Forwarded-Encrypted: i=1; AJvYcCXO0A+Eckkn+P263s80CTYe9NWfZLO+DdMVx+fkdqDaq4PvuxzwsewuShMxj59B9e59I0ROhEdg8Vh5BUTvBpplD2ffmPHJR+uwafZb2Oq1z1SzIcbVrD1Kam6U04pS1miFBDnqDM06GDu5cBY1q/GIFXSikj5tQygWKl9PP7XSCLsdvDMXV4ZauX9D/JT6jcKty/gvf+3BYzNLdofX+09MJ4CRiN2w0ou3PUCe X-Gm-Message-State: AOJu0Yz/c+5zKvl3/0S214VypwAm0Wn9fzgevjcTGofcfLCXOB0gByhF 5PLzzJBm/x90HAYQ9Gd5sekqezPWGRJ1o94ttBipBZroNvHqINta X-Google-Smtp-Source: AGHT+IFD1T5j41+YkZKjlX8tb+oypqzwGo0KjjclrVPhg6VzPZNTu72qGOLdEGcwfETG9bkgVsiXwg== X-Received: by 2002:a05:620a:5604:b0:79d:6169:7ab9 with SMTP id af79cd13be357-79f19c083f0mr801584485a.68.1720646761611; Wed, 10 Jul 2024 14:26:01 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:01 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 07/14] virtio/vsock: add common datagram send path Date: Wed, 10 Jul 2024 21:25:48 +0000 Message-Id: <20240710212555.1617795-8-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 Signed-off-by: Amery Hung --- include/linux/virtio_vsock.h | 1 + include/net/af_vsock.h | 2 + net/vmw_vsock/af_vsock.c | 2 +- net/vmw_vsock/virtio_transport_common.c | 87 ++++++++++++++++++++++++- 4 files changed, 90 insertions(+), 2 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index f749a066af46..4408749febd2 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -152,6 +152,7 @@ struct virtio_vsock_pkt_info { u16 op; u32 flags; bool reply; + u8 remote_flags; }; struct virtio_transport { diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index 44db8f2c507d..6e97d344ac75 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -216,6 +216,8 @@ 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); +const struct vsock_transport *vsock_dgram_lookup_transport(unsigned int cid, + __u8 flags); struct vsock_skb_cb { unsigned int src_cid; diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index ab08cd81720e..f83b655fdbe9 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -487,7 +487,7 @@ vsock_connectible_lookup_transport(unsigned int cid, __u8 flags) return transport; } -static const struct vsock_transport * +const struct vsock_transport * vsock_dgram_lookup_transport(unsigned int cid, __u8 flags) { const struct vsock_transport *transport; diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index a1c76836d798..46cd1807f8e3 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1040,13 +1040,98 @@ int virtio_transport_shutdown(struct vsock_sock *vsk, int mode) } EXPORT_SYMBOL_GPL(virtio_transport_shutdown); +static int virtio_transport_dgram_send_pkt_info(struct vsock_sock *vsk, + struct virtio_vsock_pkt_info *info) +{ + u32 src_cid, src_port, dst_cid, dst_port; + const struct vsock_transport *transport; + const struct virtio_transport *t_ops; + struct sock *sk = sk_vsock(vsk); + struct virtio_vsock_hdr *hdr; + struct sk_buff *skb; + void *payload; + int noblock = 0; + int err; + + info->type = virtio_transport_get_type(sk_vsock(vsk)); + + if (info->pkt_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) + return -EMSGSIZE; + + transport = vsock_dgram_lookup_transport(info->remote_cid, info->remote_flags); + t_ops = container_of(transport, struct virtio_transport, transport); + if (unlikely(!t_ops)) + return -EFAULT; + + if (info->msg) + noblock = info->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, info->pkt_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; + dst_cid = info->remote_cid; + dst_port = info->remote_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(dst_cid); + hdr->src_port = cpu_to_le32(src_port); + hdr->dst_port = cpu_to_le32(dst_port); + hdr->flags = cpu_to_le32(info->flags); + hdr->len = cpu_to_le32(info->pkt_len); + + if (info->msg && info->pkt_len > 0) { + payload = skb_put(skb, info->pkt_len); + err = memcpy_from_msg(payload, info->msg, info->pkt_len); + if (err) + goto out; + } + + trace_virtio_transport_alloc_pkt(src_cid, src_port, + dst_cid, dst_port, + info->pkt_len, + info->type, + info->op, + info->flags, + false); + + return t_ops->send_pkt(skb); +out: + kfree_skb(skb); + return err; +} + int virtio_transport_dgram_enqueue(struct vsock_sock *vsk, struct sockaddr_vm *remote_addr, 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 = { + .op = VIRTIO_VSOCK_OP_RW, + .remote_cid = remote_addr->svm_cid, + .remote_port = remote_addr->svm_port, + .remote_flags = remote_addr->svm_flags, + .msg = msg, + .vsk = vsk, + .pkt_len = dgram_len, + }; + + return virtio_transport_dgram_send_pkt_info(vsk, &info); } EXPORT_SYMBOL_GPL(virtio_transport_dgram_enqueue); From patchwork Wed Jul 10 21:25:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729726 Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60379156238; Wed, 10 Jul 2024 21:26:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646765; cv=none; b=DwcillEvtuVhXf+pkAJGa5zOSMqRFBRyUj2PauNjTU2vjB+59HYGOSh1salDLi0uxtvFSlbwn1qSSU938RbsdddS9O6sSjMkwBjWtalkGY75p5gSivH6+M6rnITizULzW723IVn+It2+s1Ic0RKTOKIxHyfGjaSjz36Z7CRWVvU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646765; c=relaxed/simple; bh=J/OYaM6+jTGguEhL1pMHM9vGNQQe8xO7deLzLD0Zrlw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sk9hROwv5BqPOpRhsg7/HYYlRU29CkzHTBAcpabeZglLJxcFOYZtR+V5yJlkE3djGGpCPBrhK8L+cdxKkOtPpdUIqTcFG4i/UgsHq+4uXE9MxkqvEDkOkwdt0qXyyHrAJP1NpPkCllOHYHN/772nLX4t58KLn+M6ze32RLs2LLI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=j1WwWfhD; arc=none smtp.client-ip=209.85.167.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j1WwWfhD" Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3d9bcb47182so149574b6e.3; Wed, 10 Jul 2024 14:26:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646762; x=1721251562; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IZum7xcXnx/YZciY9jm1etfeGDFDR11AzV+Fk8UpldY=; b=j1WwWfhDliyfqk7jkdNBFvxXeOzqA1npLhw6UgLs6h/JJLhRavoMeQUSfMWNc/y63Z 9Zd7utwewuW/TKx7TRnVBt1IVDefMPXFvss8zRYDafgF+poWvNvVaFYSzCFQNT/EWWOx 9oWodGlIvnhWlCpXSBHBO+sbYpram2krMDQQr0co3HJVvFn9C+MdADp8XFVev6B0TQEw q5ZOLPvww5UZalCFMFmIKGI5JtUuPovvKuxEQ5ny087BXA4j90VsaYJY3vLXk/aqmwdX /OlwQ+RLYnXIxex5gqXQAbTSDII4K30o0X1HUWm73ZUPfEj3j6b1eC8v+S7x9GLNNCex Rndg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646762; x=1721251562; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=IZum7xcXnx/YZciY9jm1etfeGDFDR11AzV+Fk8UpldY=; b=l36AfmQJ5R/KgyRTDKku7VAXwe75FrtZO9iWy6UyON4s+vYoLQxbHCibPLqZqHa9oL 1RpY8KtDe3pmSLkpvwhoHOYIzB9C3u9nfek1yMAQscxgu10qgb9I/7tInMYInax+ukh8 kSuWyu7jeEYbqv/D55e8Su8YAYk2RD70FnR5v4F6j+a465yeg8fzXjtNVIvhuTkQV4xw diqD7mgQZDjhii+KCV45wOL5ajoPNkyuoF+gKp/rudrt0iF7n8849yvxJp9ysI89ZqTQ 5m2f4qLnsBbRyL+6DZBYRmdW7aD6K7pSRStb5cPiQWUdraM1U3lCrNddisc4YrVw7v2T pLOQ== X-Forwarded-Encrypted: i=1; AJvYcCVna4Fr2sBTijJdYB3EKGiJwuphZlFDXysQ9WGE2tl37+X8KcBj5alQtGFROeDJpSAp8R21sLzxx2Zt4FdCNae6l+qbGv6lfHt5BY/5mw4nceyj7qmy+yCMjl4y+6Zn48kGBW785s1QdHP4XzBYqXqKAPD1hjz+gpNJOK12MbJ34M5tPlE19+154NshaP0PIjkd6crLNOZ2ahQDKKXIylVxus/0dmeLriMY6KKH X-Gm-Message-State: AOJu0YwYfN7A+5E1wXt1HUiaHZvri9ndjoqG/jHTH4dBlW6BdDtwFhJ6 uEuG7SD/+puF7BOEDeYFK3XJLnjMeCupxqqJTA+IWT3Vh+4mQYOR X-Google-Smtp-Source: AGHT+IEwAeV+NVV9eWvJ41Uh/5eeH740eMks+WRw93+ZS4FZF5Q7SDWMuOErRunlQoY//g+dgPPf/A== X-Received: by 2002:a05:6808:1455:b0:3d9:2925:cd37 with SMTP id 5614622812f47-3d93bddf767mr8767261b6e.2.1720646762507; Wed, 10 Jul 2024 14:26:02 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:02 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 08/14] af_vsock: add vsock_find_bound_dgram_socket() Date: Wed, 10 Jul 2024 21:25:49 +0000 Message-Id: <20240710212555.1617795-9-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 6e97d344ac75..9d0882b82bfa 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -218,6 +218,7 @@ int vsock_assign_transport(struct vsock_sock *vsk, struct vsock_sock *psk); bool vsock_find_cid(unsigned int cid); const struct vsock_transport *vsock_dgram_lookup_transport(unsigned int cid, __u8 flags); +struct sock *vsock_find_bound_dgram_socket(struct sockaddr_vm *addr); struct vsock_skb_cb { unsigned int src_cid; diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index f83b655fdbe9..f0e5db0eb43a 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -265,6 +265,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 10 21:25:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729728 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 526C512F38B; Wed, 10 Jul 2024 21:26:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646766; cv=none; b=Jvwo6e2KmOeYO4LDALX1uMeDEX6mRjIu+uN7PE3h5Wb2nOYCU3Y7NCuxlVihvlIwfA+0UIIcBUujD1UAzL0pFyCRqsIxQToCQjDGUuAQMxSW/Dc69LZAV9mbPXjziPGbJ514d3U4XVNPM7jMMbjV6hD7tteYg3rrDvY2ldrRg2A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646766; c=relaxed/simple; bh=n39Do9tgRbgw3ZexXlykzLjGhES5h4aEp9HSXPdUahY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GfTtxL1kF5I+u81dzwJUtuMKmSZi07/3rYi6oskcJm92t+1P+tIAgGYDgwfAZfFI/DUZxVyzIvJSiMKbHkNX04hyyU6HcKXwx8ZcTxGUbRS6HqYGytGvmcawuupanLmiww5T+KTfz0Li1s4JihkKpaK4nJgLkMPsYmM5R3nFZ+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=funhm5q2; arc=none smtp.client-ip=209.85.222.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="funhm5q2" Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-79f0c08aa45so16841685a.0; Wed, 10 Jul 2024 14:26:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646763; x=1721251563; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AFRj+5GbXZp5LYveWDQo7+PJsMaLqCxqCVhahBc+e9E=; b=funhm5q2MZaPE1b8E39hYwkfAImZj2KyTxfxUOx7H0beGiwkvY3qipHLGkS0mNNVlW kyVsdQCWPQodOFlp1ojHqKLdP2rWf0KUpeZ8fGfybCXXOhHiSEU8FD0W/VgducbtBcua 8tvaXokS2ytf0duv9qppdosBCD9QVUcN+RfvFOLaGPe1StHZ/TuIJHUtoSfh5fZ7yXN+ JDyGD1srFCguvkzMSOadC92pvIv4UFYRsvJzxY0OXyxa7TwqRR/pQ2GtPP95OPhUBiIp j8vZmvBwSykXQUnJXIhFvASu8aghgrWeRL9mh6HCmFbnAfTOiEssxTtQHnXb7ipjixtc baqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646763; x=1721251563; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AFRj+5GbXZp5LYveWDQo7+PJsMaLqCxqCVhahBc+e9E=; b=NX/9KtMNVXMnFMG4G2Ndybr7hnK/oLHtxJZqXg2vs8UNH5bWax+xnt57dG6MfPFhbr 4oWllD7KKItv1/7M7LFZwl9it7GG608Cj3peDgtR5ga0HbAr+sROQRfzdXCkClBYqle4 pGJ/0zeTkX2FOf8Fr6xrvX06nzs4ftFHDpu66JfGHC1TZ1CcbYRXJSzcDSKsZzAwbzZD 0bBRo4Ah15B7+WqNDVbdlGuQdQJUCrJMAckp3hfzNb24f8KDZPrqPxNtMdJQXV1I5zg+ EDcutOXkVM/AE67BVFvMh63wFvMo1uzgg/IxGEjXrr0U3TBDuKBfjzFdHQVzbp8xZAJI pKYw== X-Forwarded-Encrypted: i=1; AJvYcCVX6WBgW7w6+hr1OrC8pu1swgGoQDYNUXLDxFI8Y5ubt0mkOsYLi54AZypSpXV3Dcbt5SZD6QzOoRN1sE3o36EONhZLWDkonSMqqFehNd2cok+HG8kJyJcB0pdF/4pHjO99KxbgOVXxVQ18A+93HyxoLUgLouA3DgvtTNwx96/Drxdo4JSpoPvL9LEYAbSptuljAXYG+5Aj7IlqTKP5hIXWetIFtWDLOe28DPYP X-Gm-Message-State: AOJu0YzedcSirGa7Sy8Yj3oi76y2F1ehStSj5UPNY31e+GE8+a0OZkUp iU/NfGmdro3ud2bO8HAsbIWk3FPwCjGm/C6rmYiQwkj+chSzbwg+ X-Google-Smtp-Source: AGHT+IHqZShenZdCom11Cahq1mvTkynECydjnvU9rIy9/fCc1xw0zjvl3LB4oJWNPyqHWZq+r+EQ8A== X-Received: by 2002:a05:620a:1aa4:b0:79f:67b:4fdc with SMTP id af79cd13be357-79f19a36bb9mr1028884485a.2.1720646763205; Wed, 10 Jul 2024 14:26:03 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:02 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 09/14] virtio/vsock: add common datagram recv path Date: Wed, 10 Jul 2024 21:25:50 +0000 Message-Id: <20240710212555.1617795-10-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 - virtio_transport_recv_pkt() saves the source cid and port to the control buffer for recvmsg() to initialize sockaddr_vm structure when using datagram Signed-off-by: Bobby Eshleman Signed-off-by: Amery Hung --- net/vmw_vsock/virtio_transport_common.c | 79 +++++++++++++++++++++---- 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index 46cd1807f8e3..a571b575fde9 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -235,7 +235,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; @@ -1422,6 +1424,33 @@ 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; + } +} + static int virtio_transport_recv_connected(struct sock *sk, struct sk_buff *skb) @@ -1591,7 +1620,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 @@ -1601,44 +1631,57 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, struct sk_buff *skb) { struct virtio_vsock_hdr *hdr = virtio_vsock_hdr(skb); + struct vsock_skb_cb *vsock_cb; struct sockaddr_vm src, dst; 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; } @@ -1654,12 +1697,21 @@ 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) { + vsock_cb = vsock_skb_cb(skb); + vsock_cb->src_cid = src.svm_cid; + vsock_cb->src_port = src.svm_port; + 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 */ @@ -1691,6 +1743,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 From patchwork Wed Jul 10 21:25:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729729 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 026FE15ADA0; Wed, 10 Jul 2024 21:26:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646766; cv=none; b=UjDfHbyKqnYnpFAWgtCRqIfzqyOqIavyyxkIxo8iKMlDZclZwUxxugQNMvYgojtEP9XYQRQ86xpHB1zGG/HbJL374erIQ1XJ93/djN+x+wz3DIQ6i2Qiw6oQNKDgrn7PMqXC3zMHj1Liw/GeoeIzLB+deKPBlr8oWH6VCLYwH38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646766; c=relaxed/simple; bh=gwtanEC2pGzN60xOeGZgf6/7sgGtmktz1APYPkOQvwU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OoGJGVEKGyMHCmplKBqrGwx9/4xmQABxUR+NOhrqYRp0T72R0wU6nl+dKAj7jVMO1yNbcqiCj3oQxfRzzLBFi5pywpwL0K4x9GQdAMygkuMU9PM0JX2jRECDdXxWYU4+MluAHiwmhhGZU1x8x1J8XJaxq9qQ2NHID78zNxWraTI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LjOqV43h; arc=none smtp.client-ip=209.85.222.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LjOqV43h" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-79f16cad2a7so15057385a.0; Wed, 10 Jul 2024 14:26:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646764; x=1721251564; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RmWB6RITNr1rCqVWevMLNgD0qATYDnpZgKWdiv490xA=; b=LjOqV43hq3Bm7C0EIUGsX+/h6yM9baE89GWr1ZQ3kJMPRuVEHnvWQyTZADKwJ/Nc84 zr6AE8HGwCuAc0Fzbq5ViVTwqDX18rbMCOLmrYZMv304faKnIcEWwFvzALpyVdb4ljV3 cj3sB4/CBI3iLsAxFv/pVkUf6PObevinnqebX+1skVw0vlSCL/EiWjAqF1z5kTt4LzVn 8DxJj6kj9iPo3LywyxzLmtI8kF3/hpL2midlM9XCfD/Z/zwBLPzOwxx3USlcbPKGVNwV oG+ZFxgZjNlNa1muqUBBSFcZMlevZ6C9ljladXFbzlIVRPpYofWFgQIVRVNgErcjIn/H 6x1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646764; x=1721251564; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RmWB6RITNr1rCqVWevMLNgD0qATYDnpZgKWdiv490xA=; b=d1Fnvr/ZIww4DHJryiYm/Rd4E/yTebcJbqbooP44LnwwXb0anwBpDgA5gBykap4TkV u9qFf4tj1UngOphk19ojr9xiNoybH+OTRc3Xwb12KYYFE9oq/hpDS7cyrNAi8YeCCbZR NdVB118SHw1tW9PRWeOIQsaK6knTY+FyIC32oMrcP5GQrluo3fK8w6zdscRhPiFpHk+8 yXZe/majFk2XKX2RLbojNTuMH5HAAN+ysjh31cjtjS1ejmrCEka26v7gWWpS1gsYg3To ljG6efoezqUq4iG3XoxX6z4teFBgjN+6nH29T76LoIsp7I/ym4/wNqLC5V38WMRS0VL8 aNtg== X-Forwarded-Encrypted: i=1; AJvYcCUIfkzulaFYGtn2qSsjyQlwBznzMOSy1n6G8ZK5Rh+T3jmpy8OQ9IIovBpIIOg1CegZOlLYRCgqAYQOcz6sRHweVreHUab9OfssDW5/2Ymr5lrGVuOjTOnNGZ7Y1nV9Nz914lpJjv4gt16ZuapadALc+eEKa1hDl0sNOurvpHw6AK+2f7o4fw3yT2iprqNS3W1ew01gBlnrCm3z3IMmxoG2Iu/B+q3C2fLPT1vH X-Gm-Message-State: AOJu0YxQ4UWlV5dprlD5o9WQGEuh3orA/jZxtfpn+l9WvWkgPPGLLN1h 3cr0T3IEOZaIqeEdN/E2ta6rJqUUKLNqAtZQzDfydipjgw8QyHHT X-Google-Smtp-Source: AGHT+IG777NGw+WgU0m269DXuwpGYmePB845UKK+Uaz17xBp/iDpuOJlJWqnyMj0N84ol+h/zJHD9g== X-Received: by 2002:a05:620a:51d4:b0:79e:ffce:47df with SMTP id af79cd13be357-79f19c023e0mr644384385a.78.1720646763945; Wed, 10 Jul 2024 14:26:03 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:03 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 10/14] virtio/vsock: add VIRTIO_VSOCK_F_DGRAM feature bit Date: Wed, 10 Jul 2024 21:25:51 +0000 Message-Id: <20240710212555.1617795-11-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 10 21:25:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729730 Received: from mail-yw1-f169.google.com (mail-yw1-f169.google.com [209.85.128.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C2ACF15E5CF; Wed, 10 Jul 2024 21:26:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646767; cv=none; b=Wh9nniWm5G7ezrPyNh4TR+XjD4OJxpyXecuoNFnXUDI7xuH0Nziwh9WE1AdgUc/Y0hA56zix8Z6i12y1ZWl6hmzv/ZlxEkxjvKwHSgZVPkgIhV3eDQ7btGnB1RTceXZoli1Xpss3/FHi7yEQjrzvnxRNEvdf7lv7n6Iw1rgGI/w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646767; c=relaxed/simple; bh=zERDNd9JCYNnr+ycpOsWDKndLRgVVm/OgVyL7VGJ4wQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aizMC8wzn4nAPdYbqtJpdy3XwErOapf8e94fVm0SmRGOWPZooTXCu1K9HGFlDhdx/m4CJQ+ZFE86dKdoxQZC33l7eujYWYDNwAlfmC/CVpOqjJO1QHyupmWznqKq5DKM94UwOQy4i8ZWaKxfVJN0HNJEoPtQb5LoI7MsLcN7CrI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Ii06eIbs; arc=none smtp.client-ip=209.85.128.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Ii06eIbs" Received: by mail-yw1-f169.google.com with SMTP id 00721157ae682-64b0d45a7aaso2321177b3.0; Wed, 10 Jul 2024 14:26:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646765; x=1721251565; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sN+GRSmx+rskTAQOVFYDHoKP/o7w51RMiYsLeysrnrY=; b=Ii06eIbs37xb3sMkd5xaaRWyJjk6+197FdF7l8Z+ikSonNvwvljm26e8o4RU/B7Mf6 nv6JIw1fl8jCj5EWEdb/F41Vjs1MN/JSghHMrFAFa7BVKF67gc/TmtsutE+CAuCSREbX dvxCr0rUPIks8d+4d8R+DyD53B0lPRPUV5d0+EomBS5xABV1QiTrBFbIJqhXSdWFjOhm Vm+biOtp+DaES6ejTEjlfyxCTNME6Dl8KdQ60AHC44uqyHI7XtpikG11ODtvvSzRK/74 6iqOyQMm9Ddr04RnY59k3xY/i+4Dm61hLWv+rQ+QPCbH0+yJePJJP42RIYusy4x8SADh qC7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646765; x=1721251565; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sN+GRSmx+rskTAQOVFYDHoKP/o7w51RMiYsLeysrnrY=; b=iSNgtv8ufK4NtLDVlXNMMe2/8aoHlb80aWvIaO8a4fPDUxgZMbCwkSCILmnwo2VCR3 oP+EXJOh7+CpyZhi6b6j/H0dcCfmnCn0xhMuZzns19LF1eyXED0+VlcYzi0V0GsoLDGW dvz+XgT3ka1uyeMqS0bpAjWvp+hSEFkzptVdVbC1PoEIcV3c26IFYBZzZaOQ367Czp7L YsVTjHOoHE4qo8EISVzGfm9IgqtIEMZN8CxppH17UEybHKTfAJ5fh/QsV1Mb4rIidL8X 1YmvIf30YeTrU5/rZIKW2bXMe7ZlG0mB57Ql8MbDqGH6TdLI2fOhwRpKyJI9DTL9Ex/F vd2A== X-Forwarded-Encrypted: i=1; AJvYcCUXIM8jm0yivY6P4CxHNfs2rYd8Mc9gT2ipnMIBWKK4744ZePQkzJ/j1CImqdpn7L3jn5epyBsdEhqZNLmqLAC0pLrVJf6+B+W7DFRetQGh9kcReu8eSJaTT9a+FVmayMZQN1ZwfSkGGQVP7JWJkheKYFnKb/GFeNvsBqeyizE6gNwIpckY56QB1GTlAF1VwhqjDelLUsdJSPJB6br2l2/AHhfdTal49l9WW+I2 X-Gm-Message-State: AOJu0Yzp3vB6QWunA7gV8U/2EWgHPcpz16tUfrSR0sqRjZkymeAc5iXW gP+hDPIE6hDIrcJYY3vrTgL8ZzNlxk5dH9S1pSZfVWSndJ71X8zV X-Google-Smtp-Source: AGHT+IFdDJbzk9Eahvtm1VoZEbVdPnTfeK7msIL9TKHGtqINukZ8lIPGu27MsG6zHof1X+d3FW8ZCw== X-Received: by 2002:a05:690c:4d82:b0:650:f7a4:c70d with SMTP id 00721157ae682-658f02f55edmr94195357b3.41.1720646764730; Wed, 10 Jul 2024 14:26:04 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:04 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 11/14] vhost/vsock: implement datagram support Date: Wed, 10 Jul 2024 21:25:52 +0000 Message-Id: <20240710212555.1617795-12-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 Signed-off-by: Amery Hung --- drivers/vhost/vsock.c | 60 ++++++++++++++++++++++++++++++++++++++-- net/vmw_vsock/af_vsock.c | 2 +- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index fa1aefb78016..13c3cbff21da 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) + goto out; + + if (sock_queue_err_skb(sk, clone)) + kfree_skb(clone); + + sk->sk_err = err; + sk_error_report(sk); +out: + kfree_skb(skb); +} + static void vhost_transport_do_send_pkt(struct vhost_vsock *vsock, struct vhost_virtqueue *vq) @@ -162,9 +191,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 @@ -403,6 +438,22 @@ static bool vhost_transport_msgzerocopy_allow(void) return true; } +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); static struct virtio_transport vhost_transport = { @@ -419,7 +470,7 @@ 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, .stream_enqueue = virtio_transport_stream_enqueue, .stream_dequeue = virtio_transport_stream_dequeue, @@ -811,6 +862,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 f0e5db0eb43a..344db0f3a602 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -1463,7 +1463,7 @@ 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)) From patchwork Wed Jul 10 21:25:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729731 Received: from mail-yb1-f181.google.com (mail-yb1-f181.google.com [209.85.219.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 80BDC15EFC1; Wed, 10 Jul 2024 21:26:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646768; cv=none; b=REo0LuoDnYbPfwRaT68QzMBrZ4jsgQq6jct+OD8U2fSi7/3KEwKmLYFcB9Mee6rydEZy7g0Um6YSQA1M3x0rSjNTV7bJpCg+8NtyIWTPxT3YA1n2yTaPaySUybC3G7ET8qNURbVPvID+81gphRxaTTaMJ1GfECh89xeCj/ofqFQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646768; c=relaxed/simple; bh=aa9fCTbYzE+aisD6RqXIAVWYsXTP3Jh37dzQFu2SwWI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rw8GqsWBxRODVmpHk2DhJr8B8cI0pXe6H0SvsN4e9W441oRC7pfJWkzlg6R/7vIbK0B2vke3iZm2x/Z5VITr8x20hLb6SsOIQEz1WxWsSOLh5nnk0zGfu/1wqtQU0VygvEgcutMlzN6xHOVKz1HDu9lIdc9dk5m3V+/AmsYtF6E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=mKP5bwZ5; arc=none smtp.client-ip=209.85.219.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mKP5bwZ5" Received: by mail-yb1-f181.google.com with SMTP id 3f1490d57ef6-e04f1bcbf84so226886276.0; Wed, 10 Jul 2024 14:26:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646765; x=1721251565; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mNa2JyqzSfsQHlae80C+dAnyTQSrxK9qhyOYTj2h2/o=; b=mKP5bwZ5gbs29Qb1c82tMG5nFdVzqPhVaBmk5LsIY6TE8lpQ6UIKoPXpfWGzST9XSI zljfuLsUj9phOSv8h+tH1yAhlbXxEKF9SJCqFGSjVqQAcq+nxknaH3UPV0GkU7dPRogw sP2wKtJ53o5MyGqqYnpjtCOFUDN/lUDf7KUqXKFuXgO3aO11fs/Kj8YV0VvwrgD0P4q/ Btcxa2/IUfiQDfC4WpCXxKVtv1LsPMXvk/Gk4uMizoU1Ocoo9PmMCsE5ysLRmt4DMLDd DshXPAfbxWPs8ZxO7ART/G9lYwVMYoT6bOQNGoIikHBe/4X+AsSuoEUolpF9tYmFgqgr TWqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646765; x=1721251565; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mNa2JyqzSfsQHlae80C+dAnyTQSrxK9qhyOYTj2h2/o=; b=IysT0atNuH+SG/HX2d8mP/65FMMFd72PSN/gp2GxiYJ2x81Muej5qOz1J+xxX/z39H b+1N183vHfJ5A/oI5PDjZXcwuXGcloDG97XWv1nebYjQg5qeullyXXD2NTJCPYvh7Xr/ WS9TsaP+mBpgvjxFF9HVQp0mo03GNt1JkCxoTk0/eypbScM80EKDOsjH+QHredTYe99N 2emoWf2I5fr24Q/2CKsKT+INErWRGe76hozXjTS6kkuFkura5lOhKlQVe3dwFyJCp7Nj g1a/cVa7TzzFyz1bjy5e1hCEuTq8TzqfAv0gVt2o+qKsJd2Y+VV0N+H8/N1eFnQigPvG v0oA== X-Forwarded-Encrypted: i=1; AJvYcCUPSN/4rDurSas2hhr2JMZu5oYWRl1Q2C+r9/NwdX97fBarIr6n60phccyvY0/4NS1LfV+7D6r7CbMMJqMA/uKrPlKcadLEDzYS837GCJfEWfJ9AkxlGlbYuGK+t/T6OQNGSJeONrBwgD7iwyrKDZ58nLC5RMxWmYKmcVJA8IDA9xjHikW4II0mT0Q76Dh81dDCq15K98hocqvoI+7PEoTKnCSax0fXXexvEbGc X-Gm-Message-State: AOJu0YxQnYxRCtvUMFp3UlU0vwQT43c9SZbK+oNZQ2BVYPNKMWt4iiyd 3rOsQhWXZnM62tE5BVEVu8xJbqlFPe9Mt8PsiQcfrVFriprSbZ2U X-Google-Smtp-Source: AGHT+IF5ioq7BkP55ZrDd65MHpqGoaQfutI36NHEnLXbX07dGmO7XnmHfZ+Wg65ipK41BqyPE67tdw== X-Received: by 2002:a25:7416:0:b0:e03:adcb:f8e8 with SMTP id 3f1490d57ef6-e041b078c55mr7655917276.30.1720646765536; Wed, 10 Jul 2024 14:26:05 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:05 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 12/14] vsock/loopback: implement datagram support Date: Wed, 10 Jul 2024 21:25:53 +0000 Message-Id: <20240710212555.1617795-13-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman 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 Signed-off-by: Amery Hung --- net/vmw_vsock/vsock_loopback.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/net/vmw_vsock/vsock_loopback.c b/net/vmw_vsock/vsock_loopback.c index 4dd4886f29d1..0de4e2c8573c 100644 --- a/net/vmw_vsock/vsock_loopback.c +++ b/net/vmw_vsock/vsock_loopback.c @@ -46,6 +46,11 @@ static int vsock_loopback_cancel_pkt(struct vsock_sock *vsk) return 0; } +static bool vsock_loopback_dgram_allow(u32 cid, u32 port) +{ + return true; +} + static bool vsock_loopback_seqpacket_allow(u32 remote_cid); static bool vsock_loopback_msgzerocopy_allow(void) { @@ -66,7 +71,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, From patchwork Wed Jul 10 21:25:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729732 Received: from mail-qk1-f173.google.com (mail-qk1-f173.google.com [209.85.222.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56A8C1607B6; Wed, 10 Jul 2024 21:26:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646769; cv=none; b=GCCLrvq2+Th/hhgDlXIMtHtmpByT+RTrLn1VgKptG0E38WBU1KIVCj0mMJu4sekdiYzxZCrwVZxeiSwvFURwjBH67r1wy0mwhyfGW4G4ZQzooJjV8byIFDg//lMsBoEyy95iiJP7Tvd/WccuN2T8B6XlgXPMPKoXruSKOSU8v9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646769; c=relaxed/simple; bh=gGDd9b+eIPhJKP6IsUlyGBx9zga4xIilMlUsKve5RCs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Bk0+/1KWcMHBIDBAVYixwS+03+Nf688VYnN5gY4ifgJNdAN7pMehzW+4sZFVnezfFXwBnwzlAR3cKTkMIn1slhhNeIdpA+vJOHGl0/Z/CSKuJG02kTY6d0iRX4cEeUvIM3lXfJBfafmKDZJRgJyyK7bR6rUQqIqtLkDLn3y2NT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=KHRWoSok; arc=none smtp.client-ip=209.85.222.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KHRWoSok" Received: by mail-qk1-f173.google.com with SMTP id af79cd13be357-79f06c9c929so114618185a.0; Wed, 10 Jul 2024 14:26:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646766; x=1721251566; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=pVIqbNrhEav3W0++Ik3CGMaAVVSiaEFKng5Yf5v/RzU=; b=KHRWoSokpNQF/LrwUQhUBpeFAgqmYde6tEO/BlNtw8dXui/rmG2qpuOC1QZnQ4g3Xh 72CvDP+7K2fd8SvM9VXlbZEf+QzvY7cluI1w6+mzY8H7KeLWkl96rz3oTI+ZeOgkfNMz XBLJGqBvDzPvcHkRGOVCJlherkY+xT8qHPviWrotq/tiPiqeJ1/yj/Yx5ovKtH6f857E 9GOSLIID7ogUKpjdrFnUeQOnNy2y41FBcRcnEZZAv5qRyaj+8IWWwfywYLRk7MP3ZndH oI7lRkbFQoE8TWjFVaLDX7Rbj5+twQQiWs0Gfte3cp3w2RAswHUoveRPpkefWekuBZN6 8vMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646766; x=1721251566; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pVIqbNrhEav3W0++Ik3CGMaAVVSiaEFKng5Yf5v/RzU=; b=YgX3vFH/GXfXRxESxZL9R8WpD752N5LAGAok1fAZGWnUue4CoWWFTLnzWqUaJ1dvVN Pe9TfWMpkym7yn3B0+mHEufS/jMhyjcD+QMfn5yE1hREDKmrV3Pz6UKpsIwlGA8JMMv3 5gG4RH3HL8AgOgiswrvmIibDKXVRczLaAxhAiOEO4uJH7yYnQG2OqvRdSxMwLf9OGpHm Yw2SHWEV73WmnSK8ePekF8ySqd6EhOMZV0lihRWLf+gVY3soroomRFtUtX5XZmTXuJDN l7t4gb7Lrpq9zTu6Wjq6RNSGSzlie6CcNOu9vCfPa0B2sEn+NXDBQ45mofkbbCJegD2h qO6A== X-Forwarded-Encrypted: i=1; AJvYcCUU5rREEte7+MwFYzXJjuxhlZiaB62IdWx9mNIQksII0ZxNFLD8Q+1uDm6d/Z9nFVQAeunj0XLVJXGt0puvzIHVAz8epGBGbHXNvOsGdSFGnMfGzRxRS9ece0655JluQFlzgkBdXFgT0trgsgWgyzFro34oJsLIZT3TxysgbhtCbuqvS3kzuqVOVccqyAKdJDoGNQ2gDn4blhOdRQbEAi4WpHRiLCMd6r6uLuoj X-Gm-Message-State: AOJu0Yy7ZwWePLcvSdgGobSZ0iN0+4syCaImWPSlCOatxhKbJS10tn8A U8LeMdYSBwm8Aao22oc85Jlb+rwk+vRO98SQCFOPe2WD6S5pxqj+ X-Google-Smtp-Source: AGHT+IFI320bP+IbIdMehUjmnO755tB0kWKy6r95fV4jib7ke50hwNQJtvQqfOpqotKxyY96hgBfNw== X-Received: by 2002:a05:620a:4011:b0:79e:fb4c:2fb7 with SMTP id af79cd13be357-7a1469bce83mr198013985a.12.1720646766248; Wed, 10 Jul 2024 14:26:06 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:06 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 13/14] virtio/vsock: implement datagram support Date: Wed, 10 Jul 2024 21:25:54 +0000 Message-Id: <20240710212555.1617795-14-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman This commit implements datagram support with 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 Signed-off-by: Amery Hung --- include/linux/virtio_vsock.h | 1 - net/vmw_vsock/virtio_transport.c | 22 +++++++++++++++++++++- net/vmw_vsock/virtio_transport_common.c | 6 ------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 4408749febd2..fe8fa0a9669d 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -222,7 +222,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); int virtio_transport_connect(struct vsock_sock *vsk); diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index 4891b845fcde..4e1ed3b11e26 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; /* These fields are used only in tx path in function * 'virtio_transport_send_pkt_work()', so to save @@ -492,6 +493,21 @@ static bool virtio_transport_msgzerocopy_allow(void) return true; } +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); static struct virtio_transport virtio_transport = { @@ -753,6 +769,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); @@ -850,7 +869,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 a571b575fde9..52f671287fe3 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -1010,12 +1010,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 10 21:25:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Amery Hung X-Patchwork-Id: 13729733 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A5CB167DB9; Wed, 10 Jul 2024 21:26:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646771; cv=none; b=cOsg0OEtC/5eT2X6+kjaGLdG7ZmDLS0sPou1yDmDTG1R/HPxUfgIAS7HgSVxw4LHnf7wHB3wHDJNRL36KbDHeMrXCy0lhJZsHBn6oD2dKlpcImp5VnnywumZFqSQkrJZ/6PPae1WM+yfLnN2yLjcbsfci1HJL7tCCcsztImkCGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720646771; c=relaxed/simple; bh=sR557xhxwcyuxSZQML21sRSI0z3n25HwRI+vwVpW1cs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LvFQ5KuiTD/a3MtjMtO4y/Vvua+a1/86YcARJ42AOy4Uu3gyZ/0Sqd3Q0d4e8pQLZGWppeFt0YBbTI2whAmTLvU9+tT4rZDNxStLKJqgyNQx3OclObHOLoSu+/fjj/bxQc2VG+S12Qme/fvK9PMNVAXKwkvikFOycaVe5bAFWN0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kf3kZVHe; arc=none smtp.client-ip=209.85.222.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kf3kZVHe" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-79f08b01ba6so23494885a.0; Wed, 10 Jul 2024 14:26:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720646767; x=1721251567; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LswBaYYSMBfng2HelEjAHfOS4oI9VKZFcZ8sUgiEp7g=; b=kf3kZVHeW0qVV3j8+wvU/mt2XSlrk2R78KIRil2RTq1EFU+U65SoTH8iX1W9SHFNVB bGWPskRkXv9yCNcnpASLV1FnS8DslGjQP2Te3HOFURHonlm1DsS1+dJMRxFlqrhEc1f0 q+97YPiEYbo5aEiwleicX/xxOJSS4t2PEO0oG+BtBrwMkVLXwT/wokMhhHzsh+Hapnv8 lLTSnGSYmLHcDZbxnRqA4bKI/RUzD4NCXNhq6x/UXeYU7pUIiroA69XAMtETytNrV/zW LCQGW4Q8K8Mnb9DgY1X4wod2HMSAS5zieKXnTpw1mL3p3wzUKpuuL2faLgXk32a4ahtj cy3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720646767; x=1721251567; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LswBaYYSMBfng2HelEjAHfOS4oI9VKZFcZ8sUgiEp7g=; b=Je9vqkHKNgoGDxvQ7MVGWKQ72Tjj+/4GFIfGP1ZFJxVpmcp0xcTMpdcWuZtEylBJW5 Yw5nQJVHVNl9NG30Crl7DWU0K2lNFzwo2TYjhQnxcmkhY9qdITO47Qt07XNHcsuBW74R 2InDiSSQEQpOD8ucEWoJtczVaBPlsFerOEB4AfpmFOlFik+XdWpkciwKDjIJ+TaoGkE7 FyCaBdp5hRzb/7ykWqbBqkNk/KygAGkBpgxAjD1eMN+0l3iIdE/ZI1Vtu05DJ+tKRKoa aImJPjsAdm6/BgzVMNHl8vJ2+ZipikuyI4tS7F0sSQ0p5bpBxmUUbo7f6bOdf0VW+IIH DFgw== X-Forwarded-Encrypted: i=1; AJvYcCWipevcL6Npq9Pa5MPN6fH2GDblPqL8LlbzT1Ijj63o0zCFCNGyOIHS8kFdKqV6kSGLOZIDpgyXNJ2UR7igxcTQQHXZoOg/qlpgH4NZ/ODFaWhYsGEfVCWVWwEVuzN1ErpijB00Ad9YQNDTQNTUKmkZYpjFhqhQ7rpIMMxo+VzXxmq2riOE9+wiDrmS6UIm/984F+1si+cWcAnqKW0wFGMUT8A4fcFNta92rtRh X-Gm-Message-State: AOJu0YxYVOAPnSnbpTyHrN2hhdarQMMgZqMvHfreU3+gMIZ4HB7P/iVa ohIRAkJ2bIRUl4v2zs9BCV7GgIFrqGp3I/4FHlUCyQnNxjItq+jk X-Google-Smtp-Source: AGHT+IF6F6bBvVh8TU8NahoCtWH0KM8fFVbY1ljkchcwLXjsOcFl0z2Eaf8Jvw3BKu5aFIWwmTsHfw== X-Received: by 2002:a05:620a:814:b0:79f:255:c861 with SMTP id af79cd13be357-7a1469fa08fmr138503085a.35.1720646767068; Wed, 10 Jul 2024 14:26:07 -0700 (PDT) Received: from n36-183-057.byted.org ([130.44.215.118]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f190b0af1sm228791885a.122.2024.07.10.14.26.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 14:26:06 -0700 (PDT) From: Amery Hung X-Google-Original-From: Amery Hung To: stefanha@redhat.com, sgarzare@redhat.com, mst@redhat.com, jasowang@redhat.com, xuanzhuo@linux.alibaba.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, bryantan@vmware.com, vdasa@vmware.com, pv-drivers@vmware.com Cc: dan.carpenter@linaro.org, simon.horman@corigine.com, oxffffaa@gmail.com, 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@bytedance.com, jiang.wang@bytedance.com, amery.hung@bytedance.com, ameryhung@gmail.com, xiyou.wangcong@gmail.com Subject: [RFC PATCH net-next v6 14/14] test/vsock: add vsock dgram tests Date: Wed, 10 Jul 2024 21:25:55 +0000 Message-Id: <20240710212555.1617795-15-amery.hung@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20240710212555.1617795-1-amery.hung@bytedance.com> References: <20240710212555.1617795-1-amery.hung@bytedance.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Bobby Eshleman From: Jiang Wang This commit adds tests for vsock datagram. Signed-off-by: Bobby Eshleman Signed-off-by: Jiang Wang Signed-off-by: Amery Hung --- tools/testing/vsock/util.c | 177 ++++- tools/testing/vsock/util.h | 10 + tools/testing/vsock/vsock_test.c | 1032 ++++++++++++++++++++++++++---- 3 files changed, 1099 insertions(+), 120 deletions(-) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 554b290fefdc..14d6cd90ca15 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -154,7 +154,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); if (fd < 0) { @@ -189,6 +190,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 file descriptor. */ static int vsock_listen(unsigned int cid, unsigned int port, int type) { @@ -287,6 +293,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 bytes from a buffer and check the return value. * * expected_ret: @@ -425,6 +459,147 @@ void recv_byte(int fd, int expected_ret, int flags) } } +/* Transmit bytes to the given address from a buffer and check the return value. + * + * expected_ret: + * <0 Negative errno (for testing errors) + * 0 End-of-file + * >0 Success (bytes successfully written) + */ +void sendto_buf(int fd, void *buf, size_t len, struct sockaddr *dst, socklen_t addrlen, + int flags, ssize_t expected_ret) +{ + ssize_t nwritten = 0; + ssize_t ret; + + timeout_begin(TIMEOUT); + do { + ret = sendto(fd, buf + nwritten, len - nwritten, flags, dst, addrlen); + timeout_check("sendto"); + + if (ret == 0 || (ret < 0 && errno != EINTR)) + break; + + nwritten += ret; + } while (nwritten < len); + 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("sendto"); + exit(EXIT_FAILURE); + } + return; + } + + if (ret < 0) { + perror("sendto"); + exit(EXIT_FAILURE); + } + + if (nwritten != expected_ret) { + if (ret == 0) + fprintf(stderr, "unexpected EOF while sending bytes\n"); + + fprintf(stderr, "bogus sendto(2) bytes written %zd (expected %zd)\n", + nwritten, expected_ret); + exit(EXIT_FAILURE); + } +} + +/* Receive bytes from the given address in a buffer and check the return value. + * + * expected_ret: + * <0 Negative errno (for testing errors) + * 0 End-of-file + * >0 Success (bytes successfully read) + */ +void recvfrom_buf(int fd, void *buf, size_t len, struct sockaddr *src, socklen_t *addrlen, + int flags, ssize_t expected_ret) +{ + ssize_t nread = 0; + ssize_t ret; + + timeout_begin(TIMEOUT); + do { + ret = recvfrom(fd, buf + nread, len - nread, flags, src, addrlen); + timeout_check("recvfrom"); + + if (ret == 0 || (ret < 0 && errno != EINTR)) + break; + + nread += ret; + } while (nread < len); + 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("recvfrom"); + exit(EXIT_FAILURE); + } + return; + } + + if (ret < 0) { + perror("recvfrom"); + exit(EXIT_FAILURE); + } + + if (nread != expected_ret) { + if (ret == 0) + fprintf(stderr, "unexpected EOF while receiving bytes\n"); + + fprintf(stderr, "bogus recv(2) bytes read %zd (expected %zd)\n", + nread, expected_ret); + exit(EXIT_FAILURE); + } +} + +/* Transmit one byte to the given address and check the return value. + * + * expected_ret: + * <0 Negative errno (for testing errors) + * 0 End-of-file + * 1 Success + */ +void sendto_byte(int fd, struct sockaddr *dst, socklen_t addrlen, + int expected_ret, int flags) +{ + uint8_t byte = 'A'; + + sendto_buf(fd, &byte, sizeof(byte), dst, addrlen, flags, expected_ret); +} + +/* Receive one byte from the given address 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, socklen_t *addrlen, + int expected_ret, int flags) +{ + uint8_t byte; + + recvfrom_buf(fd, &byte, sizeof(byte), src, addrlen, flags, expected_ret); + + 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 e95e62485959..3367262b53c9 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -43,17 +43,27 @@ int vsock_stream_connect(unsigned int cid, unsigned int port); int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type); 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_stream_listen(unsigned int cid, unsigned int port); 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_buf(int fd, const void *buf, size_t len, int flags, ssize_t expected_ret); void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret); void send_byte(int fd, int expected_ret, int flags); void recv_byte(int fd, int expected_ret, int flags); +void sendto_buf(int fd, void *buf, size_t len, struct sockaddr *dst, + socklen_t addrlen, int flags, ssize_t expected_ret); +void recvfrom_buf(int fd, void *buf, size_t len, struct sockaddr *src, + socklen_t *addrlen, int flags, ssize_t expected_ret); +void sendto_byte(int fd, struct sockaddr *dst, socklen_t addrlen, + int expected_ret, int flags); +void recvfrom_byte(int fd, struct sockaddr *src, 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 f851f8961247..1e1576ca87d0 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 @@ -26,6 +27,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 { @@ -1403,125 +1410,912 @@ static void test_stream_cred_upd_on_set_rcvlowat(const struct test_opts *opts) test_stream_credit_update_test(opts, false); } -static struct test_case test_cases[] = { - { - .name = "SOCK_STREAM connection reset", - .run_client = test_stream_connection_reset, - }, - { - .name = "SOCK_STREAM bind only", - .run_client = test_stream_bind_only_client, - .run_server = test_stream_bind_only_server, - }, - { - .name = "SOCK_STREAM client close", - .run_client = test_stream_client_close_client, - .run_server = test_stream_client_close_server, - }, - { - .name = "SOCK_STREAM server close", - .run_client = test_stream_server_close_client, - .run_server = test_stream_server_close_server, - }, - { - .name = "SOCK_STREAM multiple connections", - .run_client = test_stream_multiconn_client, - .run_server = test_stream_multiconn_server, - }, - { - .name = "SOCK_STREAM MSG_PEEK", - .run_client = test_stream_msg_peek_client, - .run_server = test_stream_msg_peek_server, - }, - { - .name = "SOCK_SEQPACKET msg bounds", - .run_client = test_seqpacket_msg_bounds_client, - .run_server = test_seqpacket_msg_bounds_server, - }, - { - .name = "SOCK_SEQPACKET MSG_TRUNC flag", - .run_client = test_seqpacket_msg_trunc_client, - .run_server = test_seqpacket_msg_trunc_server, - }, - { - .name = "SOCK_SEQPACKET timeout", - .run_client = test_seqpacket_timeout_client, - .run_server = test_seqpacket_timeout_server, - }, - { - .name = "SOCK_SEQPACKET invalid receive buffer", - .run_client = test_seqpacket_invalid_rec_buffer_client, - .run_server = test_seqpacket_invalid_rec_buffer_server, - }, - { - .name = "SOCK_STREAM poll() + SO_RCVLOWAT", - .run_client = test_stream_poll_rcvlowat_client, - .run_server = test_stream_poll_rcvlowat_server, - }, - { - .name = "SOCK_SEQPACKET big message", - .run_client = test_seqpacket_bigmsg_client, - .run_server = test_seqpacket_bigmsg_server, - }, - { - .name = "SOCK_STREAM test invalid buffer", - .run_client = test_stream_inv_buf_client, - .run_server = test_stream_inv_buf_server, - }, - { - .name = "SOCK_SEQPACKET test invalid buffer", - .run_client = test_seqpacket_inv_buf_client, - .run_server = test_seqpacket_inv_buf_server, - }, - { - .name = "SOCK_STREAM virtio skb merge", - .run_client = test_stream_virtio_skb_merge_client, - .run_server = test_stream_virtio_skb_merge_server, - }, - { - .name = "SOCK_SEQPACKET MSG_PEEK", - .run_client = test_seqpacket_msg_peek_client, - .run_server = test_seqpacket_msg_peek_server, - }, - { - .name = "SOCK_STREAM SHUT_WR", - .run_client = test_stream_shutwr_client, - .run_server = test_stream_shutwr_server, - }, - { - .name = "SOCK_STREAM SHUT_RD", - .run_client = test_stream_shutrd_client, - .run_server = test_stream_shutrd_server, - }, - { - .name = "SOCK_STREAM MSG_ZEROCOPY", - .run_client = test_stream_msgzcopy_client, - .run_server = test_stream_msgzcopy_server, - }, - { - .name = "SOCK_SEQPACKET MSG_ZEROCOPY", - .run_client = test_seqpacket_msgzcopy_client, - .run_server = test_seqpacket_msgzcopy_server, - }, - { - .name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE", - .run_client = test_stream_msgzcopy_empty_errq_client, - .run_server = test_stream_msgzcopy_empty_errq_server, - }, - { - .name = "SOCK_STREAM double bind connect", - .run_client = test_double_bind_connect_client, - .run_server = test_double_bind_connect_server, - }, - { - .name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT", - .run_client = test_stream_rcvlowat_def_cred_upd_client, - .run_server = test_stream_cred_upd_on_set_rcvlowat, - }, - { - .name = "SOCK_STREAM virtio credit update + low rx_bytes", - .run_client = test_stream_rcvlowat_def_cred_upd_client, - .run_server = test_stream_cred_upd_on_low_rx_bytes, +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, + }, + }; + socklen_t addrlen = sizeof(addr.sa); + unsigned long sock_buf_size; + 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, &addrlen, 1, 0); + + /* Wait for the client to finish */ + control_expectln("DONE"); + + close(fd); +} + +static void test_dgram_sendto_auto_bind_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, + }, + }; + struct sockaddr_vm bind_addr; + socklen_t addrlen; + unsigned int port; + 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); + + /* Get auto-bound port after sendto */ + addrlen = sizeof(bind_addr); + if (getsockname(fd, (struct sockaddr *)&bind_addr, &addrlen)) { + perror("getsockname"); + exit(EXIT_FAILURE); + } + + /* Send the port number to the server */ + port = bind_addr.svm_port; + sendto_buf(fd, &port, sizeof(port), &addr.sa, sizeof(addr.svm), 0, sizeof(port)); + + addr.svm.svm_port = port; + recvfrom_byte(fd, &addr.sa, &addrlen, 1, 0); + + /* Notify the server that the client has finished */ + control_writeln("DONE"); + + close(fd); +} + +static void test_dgram_sendto_auto_bind_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, + }, + }; + socklen_t addrlen = sizeof(addr.sa); + unsigned long sock_buf_size; + unsigned int port; + 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, &addrlen, 1, 0); + + /* Receive the port the client is listening to */ + recvfrom_buf(fd, &port, sizeof(port), &addr.sa, &addrlen, 0, sizeof(port)); + + addr.svm.svm_port = port; + addr.svm.svm_cid = opts->peer_cid; + sendto_byte(fd, &addr.sa, sizeof(addr.svm), 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; + size_t max_msg_size; + 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(); + max_msg_size = MAX_MSG_PAGES * page_size; + + 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}; + 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_len = MAX_MSG_PAGES * getpagesize(); + iov.iov_base = malloc(iov.iov_len); + if (!iov.iov_base) { + perror("malloc"); + exit(EXIT_FAILURE); + } + + 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"); + + free(iov.iov_base); + 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", + .run_client = test_stream_connection_reset, + }, + { + .name = "SOCK_STREAM bind only", + .run_client = test_stream_bind_only_client, + .run_server = test_stream_bind_only_server, + }, + { + .name = "SOCK_STREAM client close", + .run_client = test_stream_client_close_client, + .run_server = test_stream_client_close_server, + }, + { + .name = "SOCK_STREAM server close", + .run_client = test_stream_server_close_client, + .run_server = test_stream_server_close_server, + }, + { + .name = "SOCK_STREAM multiple connections", + .run_client = test_stream_multiconn_client, + .run_server = test_stream_multiconn_server, + }, + { + .name = "SOCK_STREAM MSG_PEEK", + .run_client = test_stream_msg_peek_client, + .run_server = test_stream_msg_peek_server, + }, + { + .name = "SOCK_SEQPACKET msg bounds", + .run_client = test_seqpacket_msg_bounds_client, + .run_server = test_seqpacket_msg_bounds_server, + }, + { + .name = "SOCK_SEQPACKET MSG_TRUNC flag", + .run_client = test_seqpacket_msg_trunc_client, + .run_server = test_seqpacket_msg_trunc_server, + }, + { + .name = "SOCK_SEQPACKET timeout", + .run_client = test_seqpacket_timeout_client, + .run_server = test_seqpacket_timeout_server, + }, + { + .name = "SOCK_SEQPACKET invalid receive buffer", + .run_client = test_seqpacket_invalid_rec_buffer_client, + .run_server = test_seqpacket_invalid_rec_buffer_server, + }, + { + .name = "SOCK_STREAM poll() + SO_RCVLOWAT", + .run_client = test_stream_poll_rcvlowat_client, + .run_server = test_stream_poll_rcvlowat_server, + }, + { + .name = "SOCK_SEQPACKET big message", + .run_client = test_seqpacket_bigmsg_client, + .run_server = test_seqpacket_bigmsg_server, + }, + { + .name = "SOCK_STREAM test invalid buffer", + .run_client = test_stream_inv_buf_client, + .run_server = test_stream_inv_buf_server, + }, + { + .name = "SOCK_SEQPACKET test invalid buffer", + .run_client = test_seqpacket_inv_buf_client, + .run_server = test_seqpacket_inv_buf_server, + }, + { + .name = "SOCK_STREAM virtio skb merge", + .run_client = test_stream_virtio_skb_merge_client, + .run_server = test_stream_virtio_skb_merge_server, + }, + { + .name = "SOCK_SEQPACKET MSG_PEEK", + .run_client = test_seqpacket_msg_peek_client, + .run_server = test_seqpacket_msg_peek_server, + }, + { + .name = "SOCK_STREAM SHUT_WR", + .run_client = test_stream_shutwr_client, + .run_server = test_stream_shutwr_server, + }, + { + .name = "SOCK_STREAM SHUT_RD", + .run_client = test_stream_shutrd_client, + .run_server = test_stream_shutrd_server, + }, + { + .name = "SOCK_STREAM MSG_ZEROCOPY", + .run_client = test_stream_msgzcopy_client, + .run_server = test_stream_msgzcopy_server, + }, + { + .name = "SOCK_SEQPACKET MSG_ZEROCOPY", + .run_client = test_seqpacket_msgzcopy_client, + .run_server = test_seqpacket_msgzcopy_server, + }, + { + .name = "SOCK_STREAM MSG_ZEROCOPY empty MSG_ERRQUEUE", + .run_client = test_stream_msgzcopy_empty_errq_client, + .run_server = test_stream_msgzcopy_empty_errq_server, + }, + { + .name = "SOCK_STREAM double bind connect", + .run_client = test_double_bind_connect_client, + .run_server = test_double_bind_connect_server, + }, + { + .name = "SOCK_STREAM virtio credit update + SO_RCVLOWAT", + .run_client = test_stream_rcvlowat_def_cred_upd_client, + .run_server = test_stream_cred_upd_on_set_rcvlowat, + }, + { + .name = "SOCK_STREAM virtio credit update + low rx_bytes", + .run_client = test_stream_rcvlowat_def_cred_upd_client, + .run_server = test_stream_cred_upd_on_low_rx_bytes, + }, + { + .name = "SOCK_DGRAM client sendto", + .run_client = test_dgram_sendto_client, + .run_server = test_dgram_sendto_server, + }, + { + .name = "SOCK_DGRAM client sendto auto bind", + .run_client = test_dgram_sendto_auto_bind_client, + .run_server = test_dgram_sendto_auto_bind_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, }, {}, };