From patchwork Mon Apr 26 02:49:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 427759 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57856C43462 for ; Mon, 26 Apr 2021 02:50:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 316DE61360 for ; Mon, 26 Apr 2021 02:50:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231761AbhDZCvB (ORCPT ); Sun, 25 Apr 2021 22:51:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231766AbhDZCu6 (ORCPT ); Sun, 25 Apr 2021 22:50:58 -0400 Received: from mail-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45A6BC061760; Sun, 25 Apr 2021 19:50:15 -0700 (PDT) Received: by mail-qt1-x832.google.com with SMTP id q4so12280179qtn.5; Sun, 25 Apr 2021 19:50:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=qzD/xWdYKCAtAGq/PFWrbtTo6WB0A0cwxYkJe/1XiYo=; b=JwioIylIXCdnBwVwnOlwx9rTpEHy9ANtriYvilBZsbUJlP8rc1bi3rBf1+9zCS75Z5 EcGotz5Nii6JAIsTQwHnVccuuPN17GSKOXn4JZq31KMeIDKx35HI7a+3xMauxfQnLYgX mECXBhIxTvu5Qk2wgn0YaQB4NPAyXjxNloVFY6uSiiXSwwiNrw41RdkifzJAX0I1gpEt k0g36VAYiYsfibOy96B7Qqv/a/3kceFBrcjiS3W1hUw0eWufB/VpVDkujzauYyAWiVtU 9zK8vW1+kto7caocPcBBtcTsCkJ73QF+JoX3ie+OqHBPoyTbqigbMeyttlmqdmrMP94C pFCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=qzD/xWdYKCAtAGq/PFWrbtTo6WB0A0cwxYkJe/1XiYo=; b=dZJ6bFUzNm2pzD1Z0dVb1RztKTY//X3K4NQluuLY3mCthDB5e1DAu3/qJtWH8jAmCc /7GsWqVKxE5HSholOktlbJFe7kEAFJo+ud3Cithd5A6RkrqZQI1PQkC15Th47G9w2VoK 5mLlnjNs511H45kApuc9wl5aEXyWWDCQOXi+Xv4M4fDRFbqdkhsMhsT3GlCIi6oJPtjS ER8aW6Q6lKwLsFdowhrxVfM8Y7iPN8+YT6rDmSciSIUgyPMSefjUVLuaEHSSGlZUYufu Ntxj17tXU+WKDE8oLNrbJ9YWGR3ldqMP1wgy8reUjjOdgJ9BL48NJAfP7wD55R1OmsVP 1Q6Q== X-Gm-Message-State: AOAM530e5eX/ImIlSjaKjD3qYpMKyPR3Yq9uxgaNZtxemGcMI+FY8R1+ jN0kIxHw/vr0rZzDBqTkvkLLt6j5/v1fRA== X-Google-Smtp-Source: ABdhPJwzEnzIHVeMo0pKppHtgiNCufGwmU57hpySHw0P12rlwWG4ffPnpCS7byP4IB4SZiy/RILqoA== X-Received: by 2002:ac8:6685:: with SMTP id d5mr14756078qtp.60.1619405414373; Sun, 25 Apr 2021 19:50:14 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:9050:63f8:875d:8edf]) by smtp.gmail.com with ESMTPSA id e15sm9632969qkm.129.2021.04.25.19.50.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:14 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, jiang.wang@bytedance.com, duanxiongchun@bytedance.com, wangdongdong.6@bytedance.com, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf-next v3 02/10] af_unix: implement ->read_sock() for sockmap Date: Sun, 25 Apr 2021 19:49:53 -0700 Message-Id: <20210426025001.7899-3-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210426025001.7899-1-xiyou.wangcong@gmail.com> References: <20210426025001.7899-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Cong Wang Implement ->read_sock() for AF_UNIX datagram socket, it is pretty much similar to udp_read_sock(). Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/unix/af_unix.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 5a31307ceb76..f4dc22db371d 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -661,6 +661,8 @@ static ssize_t unix_stream_splice_read(struct socket *, loff_t *ppos, unsigned int flags); static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t); static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int); +static int unix_read_sock(struct sock *sk, read_descriptor_t *desc, + sk_read_actor_t recv_actor); static int unix_dgram_connect(struct socket *, struct sockaddr *, int, int); static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t); @@ -738,6 +740,7 @@ static const struct proto_ops unix_dgram_ops = { .listen = sock_no_listen, .shutdown = unix_shutdown, .sendmsg = unix_dgram_sendmsg, + .read_sock = unix_read_sock, .recvmsg = unix_dgram_recvmsg, .mmap = sock_no_mmap, .sendpage = sock_no_sendpage, @@ -2183,6 +2186,41 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, return err; } +static int unix_read_sock(struct sock *sk, read_descriptor_t *desc, + sk_read_actor_t recv_actor) +{ + int copied = 0; + + while (1) { + struct unix_sock *u = unix_sk(sk); + struct sk_buff *skb; + int used, err; + + mutex_lock(&u->iolock); + skb = skb_recv_datagram(sk, 0, 1, &err); + if (!skb) { + mutex_unlock(&u->iolock); + return err; + } + + used = recv_actor(desc, skb, 0, skb->len); + if (used <= 0) { + if (!copied) + copied = used; + mutex_unlock(&u->iolock); + break; + } else if (used <= skb->len) { + copied += used; + } + mutex_unlock(&u->iolock); + + if (!desc->count) + break; + } + + return copied; +} + /* * Sleep until more data has arrived. But check for races.. */