From patchwork Wed Jul 19 00:50:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bobby Eshleman X-Patchwork-Id: 122347 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp2119681vqt; Tue, 18 Jul 2023 18:11:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlE5XciARGQLi6ioBq7I2nvs7LUn9ZPV56KPGVE3OmyXgFvMBcTzfN7bP+6QY1TbnkzRvlDJ X-Received: by 2002:a17:906:3b0e:b0:957:2e48:5657 with SMTP id g14-20020a1709063b0e00b009572e485657mr908749ejf.68.1689729119346; Tue, 18 Jul 2023 18:11:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689729119; cv=none; d=google.com; s=arc-20160816; b=vfgq2/4up9kSdXVdL6PHKyvZTblKhEdKsshj9cKrhDwonpHgUO29T6E3HS5mTkFm9K xJKEdy1vp2cPP51wVBjJjji8YUMswH3VNf6rreKqBzdn2apLrhyvZkSALGTKgsZIhzFZ W4b6Cl9Y43nkAQ2GiCHg/7GzIKxsTKsAF02txSzGglRoM+nzLVVVYlUN1ugpwaRXeW01 sAr++HXxZ2N+ZW+LU9wKu/MURnzihWOpz95oHAhs7RrkEcNzJ+4NEW2GfDbvzJbPruPm n8JQ8BPi+EgjDY5aqed2EToMLEuLTnZna5JPvnPRqWW6Qwn2Cl5SKicZEeJlF/gPsqH9 fjlA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:in-reply-to:references:message-id :content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=M+JsHAVeQKqCNEF0CRYlofIAoy5flsel9yo94Vo/+Eo=; fh=jejeGpHkr9vVlEyvdVBCSLFut92eNP6vAI+O1tx9vp4=; b=o9AHx1WSm187s4KVGgGX67g3e2Du574t5LIUoRT9PPsYHX3CnFeTikbVKYkcv+8BWf Zl9JnL/KpsmgvfgY4BCjBVCz0WeoFUP+qr4K3E+zB93x3arc1J8a8oZ7Ie4DwnI+ZSrV Ow7T92p9DGqkIjdwugICwT+Vl+XXI/zbz+k9mhBx4WmR0seyf+xiY2AhTEGCRP9/Qdzu XdQaBXflSIWJ4eXSc0thPLADIa/JAY1RUIs8LakeBsReYPUNP5Q9+KQ+VQPr1XL6z8i6 TOQ2oTRrASTbnmHmAFav+BAfW4T1tgLtRgUcaNAs2XpqVqdHltBai1COZ6Kr+Xoisi0E 34cA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=k3FRG1PM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pv28-20020a170907209c00b0099331931d5esi2012766ejb.71.2023.07.18.18.11.35; Tue, 18 Jul 2023 18:11:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance.com header.s=google header.b=k3FRG1PM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230164AbjGSAv1 (ORCPT + 99 others); Tue, 18 Jul 2023 20:51:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229994AbjGSAvC (ORCPT ); Tue, 18 Jul 2023 20:51:02 -0400 Received: from mail-qk1-x72c.google.com (mail-qk1-x72c.google.com [IPv6:2607:f8b0:4864:20::72c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EED81BDB for ; Tue, 18 Jul 2023 17:50:17 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id af79cd13be357-76714caf466so582918485a.1 for ; Tue, 18 Jul 2023 17:50:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1689727817; x=1692319817; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=M+JsHAVeQKqCNEF0CRYlofIAoy5flsel9yo94Vo/+Eo=; b=k3FRG1PMx0ejaOyKjb+qIvS2h2qh7nI25WPMOBcv4beZE+hxTM+Z5dNRyCx59FHH/b 19EQavh8h8Fe/FtWPDeJXA2VB/UD3SZAStjnuS428zwlVMgS4h5Cq8YubGwGoC2Y0DgJ ICRqDvKNoFWj4AXdVQ2AM/hZY+upLLwgtrtt9r+MrZLkxQf43EBC7BF+Kb+Xw2Wa+mDY UFfWUjaqqRe2UeFbdp4qVgUravI0Bcl2TuHpPBiDMVj98CXTkvOzV7f4w2SIpgP57UvU 6H9DIVWaVTJQibyeFjCoc6gw8ufkYUDdCcNyG1e/HxzCIUUXnqCllHDoaMmiQ2uMFzoD UYzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689727817; x=1692319817; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=M+JsHAVeQKqCNEF0CRYlofIAoy5flsel9yo94Vo/+Eo=; b=Hb5p+LBy+ZOyrvf8SptMcFdZ6YcqZkJa4ANjpRazLTLta+L0qMNxWnOSvrPe/Vw4XL NkUMG+SN1puj6E88xxXCX3BlTGoBfmoOdiSHNzPUEUR6r8iKrAFDoguoX1L17GXg5EIl IAMPR2E1ob0LXB1v83hlVBihx1nlFC6YW8eBcJPK7WJ/FsBnP3gu3Uzh6EJ5dxtClvGX XEtzFBpwNVBDRRPDjng6OHzejSlSi2C4OKsfPRIBJpvtw7Mk9gj7eFv/1nticKvmrOPg XNH8gcqLWwaszn6wISs3uxhMmBPlTzKzQFwc2mvTCigr5XtV1sdu+rlPS7VwMTBkWRBc Sdiw== X-Gm-Message-State: ABy/qLa+xw27kej4xB6AGfmbQLn3s16cP8OFtgQBQMljW6Ky6u8aT1Sk QFZFnmyQ2hGzj3LfP3wEdaApRw== X-Received: by 2002:a05:620a:2588:b0:767:18b5:f6d6 with SMTP id x8-20020a05620a258800b0076718b5f6d6mr20219811qko.2.1689727816973; Tue, 18 Jul 2023 17:50:16 -0700 (PDT) Received: from [172.17.0.7] ([130.44.212.112]) by smtp.gmail.com with ESMTPSA id c5-20020a05620a11a500b0076738337cd1sm968696qkk.1.2023.07.18.17.50.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 17:50:16 -0700 (PDT) From: Bobby Eshleman Date: Wed, 19 Jul 2023 00:50:13 +0000 Subject: [PATCH RFC net-next v5 09/14] virtio/vsock: add common datagram recv path MIME-Version: 1.0 Message-Id: <20230413-b4-vsock-dgram-v5-9-581bd37fdb26@bytedance.com> References: <20230413-b4-vsock-dgram-v5-0-581bd37fdb26@bytedance.com> In-Reply-To: <20230413-b4-vsock-dgram-v5-0-581bd37fdb26@bytedance.com> To: Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , Xuan Zhuo , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui , Bryan Tan , Vishnu Dasa , VMware PV-Drivers Reviewers Cc: Dan Carpenter , Simon Horman , Krasnov Arseniy , kvm@vger.kernel.org, virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hyperv@vger.kernel.org, bpf@vger.kernel.org, Bobby Eshleman X-Mailer: b4 0.12.2 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771809401317054366 X-GMAIL-MSGID: 1771809401317054366 This commit adds the common datagram receive functionality for virtio transports. It does not add the vhost/virtio users of that functionality. This functionality includes: - changes to the virtio_transport_recv_pkt() path for finding the bound socket receiver for incoming packets. - a virtio_transport_dgram_addr_init() function to be used as the ->dgram_addr_init callback for initializing sockaddr_vm inside the generic recvmsg() caller. Signed-off-by: Bobby Eshleman --- include/linux/virtio_vsock.h | 2 + net/vmw_vsock/virtio_transport_common.c | 92 ++++++++++++++++++++++++++++----- 2 files changed, 81 insertions(+), 13 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 7632552bee58..b3856b8a42b3 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -212,6 +212,8 @@ u64 virtio_transport_stream_rcvhiwat(struct vsock_sock *vsk); bool virtio_transport_stream_is_active(struct vsock_sock *vsk); bool virtio_transport_stream_allow(u32 cid, u32 port); bool virtio_transport_dgram_allow(u32 cid, u32 port); +void virtio_transport_dgram_addr_init(struct sk_buff *skb, + struct sockaddr_vm *addr); int virtio_transport_connect(struct vsock_sock *vsk); diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio_transport_common.c index 3bfaff758433..96118e258097 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -183,7 +183,9 @@ EXPORT_SYMBOL_GPL(virtio_transport_deliver_tap_pkt); static u16 virtio_transport_get_type(struct sock *sk) { - if (sk->sk_type == SOCK_STREAM) + if (sk->sk_type == SOCK_DGRAM) + return VIRTIO_VSOCK_TYPE_DGRAM; + else if (sk->sk_type == SOCK_STREAM) return VIRTIO_VSOCK_TYPE_STREAM; else return VIRTIO_VSOCK_TYPE_SEQPACKET; @@ -1184,6 +1186,35 @@ virtio_transport_recv_enqueue(struct vsock_sock *vsk, kfree_skb(skb); } +static void +virtio_transport_dgram_kfree_skb(struct sk_buff *skb, int err) +{ + if (err == -ENOMEM) + kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_RCVBUFF); + else if (err == -ENOBUFS) + kfree_skb_reason(skb, SKB_DROP_REASON_PROTO_MEM); + else + kfree_skb(skb); +} + +/* This function takes ownership of the skb. + * + * It either places the skb on the sk_receive_queue or frees it. + */ +static void +virtio_transport_recv_dgram(struct sock *sk, struct sk_buff *skb) +{ + int err; + + err = sock_queue_rcv_skb(sk, skb); + if (err) { + virtio_transport_dgram_kfree_skb(skb, err); + return; + } + + sk->sk_data_ready(sk); +} + static int virtio_transport_recv_connected(struct sock *sk, struct sk_buff *skb) @@ -1347,7 +1378,8 @@ virtio_transport_recv_listen(struct sock *sk, struct sk_buff *skb, static bool virtio_transport_valid_type(u16 type) { return (type == VIRTIO_VSOCK_TYPE_STREAM) || - (type == VIRTIO_VSOCK_TYPE_SEQPACKET); + (type == VIRTIO_VSOCK_TYPE_SEQPACKET) || + (type == VIRTIO_VSOCK_TYPE_DGRAM); } /* We are under the virtio-vsock's vsock->rx_lock or vhost-vsock's vq->mutex @@ -1361,40 +1393,52 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, struct vsock_sock *vsk; struct sock *sk; bool space_available; + u16 type; vsock_addr_init(&src, le64_to_cpu(hdr->src_cid), le32_to_cpu(hdr->src_port)); vsock_addr_init(&dst, le64_to_cpu(hdr->dst_cid), le32_to_cpu(hdr->dst_port)); + type = le16_to_cpu(hdr->type); + trace_virtio_transport_recv_pkt(src.svm_cid, src.svm_port, dst.svm_cid, dst.svm_port, le32_to_cpu(hdr->len), - le16_to_cpu(hdr->type), + type, le16_to_cpu(hdr->op), le32_to_cpu(hdr->flags), le32_to_cpu(hdr->buf_alloc), le32_to_cpu(hdr->fwd_cnt)); - if (!virtio_transport_valid_type(le16_to_cpu(hdr->type))) { + if (!virtio_transport_valid_type(type)) { (void)virtio_transport_reset_no_sock(t, skb); goto free_pkt; } - /* The socket must be in connected or bound table - * otherwise send reset back + /* For stream/seqpacket, the socket must be in connected or bound table + * otherwise send reset back. + * + * For datagrams, no reset is sent back. */ sk = vsock_find_connected_socket(&src, &dst); if (!sk) { - sk = vsock_find_bound_socket(&dst); - if (!sk) { - (void)virtio_transport_reset_no_sock(t, skb); - goto free_pkt; + if (type == VIRTIO_VSOCK_TYPE_DGRAM) { + sk = vsock_find_bound_dgram_socket(&dst); + if (!sk) + goto free_pkt; + } else { + sk = vsock_find_bound_socket(&dst); + if (!sk) { + (void)virtio_transport_reset_no_sock(t, skb); + goto free_pkt; + } } } - if (virtio_transport_get_type(sk) != le16_to_cpu(hdr->type)) { - (void)virtio_transport_reset_no_sock(t, skb); + if (virtio_transport_get_type(sk) != type) { + if (type != VIRTIO_VSOCK_TYPE_DGRAM) + (void)virtio_transport_reset_no_sock(t, skb); sock_put(sk); goto free_pkt; } @@ -1410,12 +1454,18 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, /* Check if sk has been closed before lock_sock */ if (sock_flag(sk, SOCK_DONE)) { - (void)virtio_transport_reset_no_sock(t, skb); + if (type != VIRTIO_VSOCK_TYPE_DGRAM) + (void)virtio_transport_reset_no_sock(t, skb); release_sock(sk); sock_put(sk); goto free_pkt; } + if (sk->sk_type == SOCK_DGRAM) { + virtio_transport_recv_dgram(sk, skb); + goto out; + } + space_available = virtio_transport_space_update(sk, skb); /* Update CID in case it has changed after a transport reset event */ @@ -1447,6 +1497,7 @@ void virtio_transport_recv_pkt(struct virtio_transport *t, break; } +out: release_sock(sk); /* Release refcnt obtained when we fetched this socket out of the @@ -1515,6 +1566,21 @@ int virtio_transport_read_skb(struct vsock_sock *vsk, skb_read_actor_t recv_acto } EXPORT_SYMBOL_GPL(virtio_transport_read_skb); +void virtio_transport_dgram_addr_init(struct sk_buff *skb, + struct sockaddr_vm *addr) +{ + struct virtio_vsock_hdr *hdr; + unsigned int cid, port; + + WARN_ONCE(skb->head == skb->data, "virtio vsock bug: bad dgram skb"); + + hdr = virtio_vsock_hdr(skb); + cid = le64_to_cpu(hdr->src_cid); + port = le32_to_cpu(hdr->src_port); + vsock_addr_init(addr, cid, port); +} +EXPORT_SYMBOL_GPL(virtio_transport_dgram_addr_init); + MODULE_LICENSE("GPL v2"); MODULE_AUTHOR("Asias He"); MODULE_DESCRIPTION("common code for virtio vsock");