From patchwork Fri Oct 8 20:33:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12546399 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D7FCFC43217 for ; Fri, 8 Oct 2021 20:33:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BA99A61100 for ; Fri, 8 Oct 2021 20:33:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242435AbhJHUfQ (ORCPT ); Fri, 8 Oct 2021 16:35:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232005AbhJHUfP (ORCPT ); Fri, 8 Oct 2021 16:35:15 -0400 Received: from mail-qt1-x82b.google.com (mail-qt1-x82b.google.com [IPv6:2607:f8b0:4864:20::82b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94AC5C061570; Fri, 8 Oct 2021 13:33:19 -0700 (PDT) Received: by mail-qt1-x82b.google.com with SMTP id w8so1545393qts.4; Fri, 08 Oct 2021 13:33:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IdU19VoYv8u6XKYT/+9p1qJAfQZ7wnKNCZ1jGYbz6BI=; b=bKbD+iYl6k1tQWG6rO2t2Xg+YPHnactL8/QoxmMx9YvairsjB6NVIbl5b1Pto7+YJt RcbdvqViM34Bpo3S8CLTSst1UVdmZSiR2osKqWey41yhHoMxd3EFyEtgHHI0HOuTN1mn vz9jSbulPCVhqK7JGGEP8S07NIkSu7syDrwVL/0F7X5xIU4l+YcMZvI+pMw02avONFfJ VdQUd8c8J9K8LzP0BcgcTyF8wjixmE36uIeKbG4Z1V89P4uyX6yzSrLsxXvkv4/4xaiX c86FH9E10B+vkeuxKfCw9nsnmmBjVUa/pJNuFtxFGRjoLy36HZfBX2Pd4OpKF/dZjJs3 Nrww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IdU19VoYv8u6XKYT/+9p1qJAfQZ7wnKNCZ1jGYbz6BI=; b=JqNQQTEbtkJu0ZdgqYmwmOgLZ1gQgM2Gz0Ez9vy3EfBzz+jK8HfB2BCnWt94ne/9hi enDg0VMKlKPlJ2e8VH8pNMiDvWzy8x4HHZOXtjE4WHSYBQZc1cmblPmIt87QsMIjMVfq GgZF5nMLht+ggTHFnuyMhQE7I2XgyFzNhkAfK5dasxI50dGwUdJCqa3kz2Pdb93GKZup o7CeJQA1r22QIfGsPEGLHzuwJMzSUWXOVYvwaoH5AO+uhTtOs2yn8udQsVF6c/1kbCne t/9j7WJU18TXF4yjDvtwf2vuQzIUcOb6Yp58+9BPcrt6IV+DWWqlZkFD2aYpM8uFualj 6EFg== X-Gm-Message-State: AOAM533+f92G6Oz74WxekNEXvVN0XK26/Unylk56lpbP8fMnAXTBpvwQ xz643bY/7y4q8/Eao7PISlzlH8rpgsA= X-Google-Smtp-Source: ABdhPJziOFpnw/ERXN7fcLcyryXVe11BCV2WY4Y49Io1PIZc7fTHO22oZN0tUsrxW28YNQLLZZtBWA== X-Received: by 2002:ac8:5884:: with SMTP id t4mr537517qta.174.1633725198702; Fri, 08 Oct 2021 13:33:18 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:795f:367d:1f1e:4801]) by smtp.gmail.com with ESMTPSA id c8sm381945qtb.9.2021.10.08.13.33.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 13:33:18 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v4 1/4] net: rename ->stream_memory_read to ->sock_is_readable Date: Fri, 8 Oct 2021 13:33:03 -0700 Message-Id: <20211008203306.37525-2-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211008203306.37525-1-xiyou.wangcong@gmail.com> References: <20211008203306.37525-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang The proto ops ->stream_memory_read() is currently only used by TCP to check whether psock queue is empty or not. We need to rename it before reusing it for non-TCP protocols, and adjust the exsiting users accordingly. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- include/net/sock.h | 8 +++++++- include/net/tls.h | 2 +- net/ipv4/tcp.c | 5 +---- net/ipv4/tcp_bpf.c | 4 ++-- net/tls/tls_main.c | 4 ++-- net/tls/tls_sw.c | 2 +- 6 files changed, 14 insertions(+), 11 deletions(-) diff --git a/include/net/sock.h b/include/net/sock.h index ea6fbc88c8f9..463f390d90b3 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -1208,7 +1208,7 @@ struct proto { #endif bool (*stream_memory_free)(const struct sock *sk, int wake); - bool (*stream_memory_read)(const struct sock *sk); + bool (*sock_is_readable)(struct sock *sk); /* Memory pressure */ void (*enter_memory_pressure)(struct sock *sk); void (*leave_memory_pressure)(struct sock *sk); @@ -2820,4 +2820,10 @@ void sock_set_sndtimeo(struct sock *sk, s64 secs); int sock_bind_add(struct sock *sk, struct sockaddr *addr, int addr_len); +static inline bool sk_is_readable(struct sock *sk) +{ + if (sk->sk_prot->sock_is_readable) + return sk->sk_prot->sock_is_readable(sk); + return false; +} #endif /* _SOCK_H */ diff --git a/include/net/tls.h b/include/net/tls.h index be4b3e1cac46..01d2e3744393 100644 --- a/include/net/tls.h +++ b/include/net/tls.h @@ -375,7 +375,7 @@ void tls_sw_release_resources_rx(struct sock *sk); void tls_sw_free_ctx_rx(struct tls_context *tls_ctx); int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int flags, int *addr_len); -bool tls_sw_stream_read(const struct sock *sk); +bool tls_sw_sock_is_readable(struct sock *sk); ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags); diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index e8b48df73c85..f5c336f8b0c8 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -486,10 +486,7 @@ static bool tcp_stream_is_readable(struct sock *sk, int target) { if (tcp_epollin_ready(sk, target)) return true; - - if (sk->sk_prot->stream_memory_read) - return sk->sk_prot->stream_memory_read(sk); - return false; + return sk_is_readable(sk); } /* diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index d3e9386b493e..0175dbcb7722 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -150,7 +150,7 @@ int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg, EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir); #ifdef CONFIG_BPF_SYSCALL -static bool tcp_bpf_stream_read(const struct sock *sk) +static bool tcp_bpf_sock_is_readable(struct sock *sk) { struct sk_psock *psock; bool empty = true; @@ -479,7 +479,7 @@ static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS], prot[TCP_BPF_BASE].unhash = sock_map_unhash; prot[TCP_BPF_BASE].close = sock_map_close; prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg; - prot[TCP_BPF_BASE].stream_memory_read = tcp_bpf_stream_read; + prot[TCP_BPF_BASE].sock_is_readable = tcp_bpf_sock_is_readable; prot[TCP_BPF_TX] = prot[TCP_BPF_BASE]; prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg; diff --git a/net/tls/tls_main.c b/net/tls/tls_main.c index fde56ff49163..9ab81db8a654 100644 --- a/net/tls/tls_main.c +++ b/net/tls/tls_main.c @@ -681,12 +681,12 @@ static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG], prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE]; prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg; - prot[TLS_BASE][TLS_SW].stream_memory_read = tls_sw_stream_read; + prot[TLS_BASE][TLS_SW].sock_is_readable = tls_sw_sock_is_readable; prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close; prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE]; prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg; - prot[TLS_SW][TLS_SW].stream_memory_read = tls_sw_stream_read; + prot[TLS_SW][TLS_SW].sock_is_readable = tls_sw_sock_is_readable; prot[TLS_SW][TLS_SW].close = tls_sk_proto_close; #ifdef CONFIG_TLS_DEVICE diff --git a/net/tls/tls_sw.c b/net/tls/tls_sw.c index 4feb95e34b64..d5d09bd817b7 100644 --- a/net/tls/tls_sw.c +++ b/net/tls/tls_sw.c @@ -2026,7 +2026,7 @@ ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, return copied ? : err; } -bool tls_sw_stream_read(const struct sock *sk) +bool tls_sw_sock_is_readable(struct sock *sk) { struct tls_context *tls_ctx = tls_get_ctx(sk); struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); From patchwork Fri Oct 8 20:33:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12546401 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41993C433EF for ; Fri, 8 Oct 2021 20:33:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1FBEB60F5A for ; Fri, 8 Oct 2021 20:33:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242765AbhJHUfS (ORCPT ); Fri, 8 Oct 2021 16:35:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242279AbhJHUfQ (ORCPT ); Fri, 8 Oct 2021 16:35:16 -0400 Received: from mail-qk1-x729.google.com (mail-qk1-x729.google.com [IPv6:2607:f8b0:4864:20::729]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8C6DC061570; Fri, 8 Oct 2021 13:33:20 -0700 (PDT) Received: by mail-qk1-x729.google.com with SMTP id p4so10733966qki.3; Fri, 08 Oct 2021 13:33:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mjh66l7EjiKQtKP9c2a3KtPXE8PrCUDOCdt6/27IAc4=; b=LpPIRW7LFjwatmS0X/2Ogdhky7Za9m+BHxGsmYrGbuORhJcSv/ih3FsGb0UN1+2Grb OoQU/ExLUQGh+5w2tRSLYEu7j2Ohi4y+zOX5vXOpXcvjXxhwybkczKh9wozmAxp0RmM/ JrL++z5f0TF/IOSY7dfXP2fcFtHP+rasl4NYTfGva2fZiAWyz03ymnusybEr/R80yn3n J0YoOpG8KPZMwkDz0MarDIyUN4EIGuEHli/HPIJouxFk4nBwHO+2AtHEoBj/G9ky2Wh6 WSctMLTpZgfn3I+Fbz2+bx7Hn4h+tWjte4KdS3KwFzrMZpXyklTODkxm0SgLdxrAavJ5 5XEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mjh66l7EjiKQtKP9c2a3KtPXE8PrCUDOCdt6/27IAc4=; b=zXAAvVKhE5dTSEvptUr6KIFB9JbZriHPs8BttUQCyoydzz58hbuGYxumMlleRmeV5J 8bomR/pQ4CBUYSXKhKg2jHKk990tQxJr1Mu+dzaWA+oc0Rr68USAN2U6lY3dIe3mDQSP Y3SvXB6IPA+TR0icYAq40aziBuppa98aBvFl8DKccSOxbeVdYLyY7tEZAvxrG0Yyfiy1 bhz1P5lqrVvEFasxrsTZcAPt7w691FDzKSwsfnSaCoPlrxmBGzWz8zgu2f5VXZq+5FtN ZJ5mo0+JaYs2l0br5t+dr3XGdQKOgzCE757RaIFLnke4jxElBKnBVp2J1VkO9vQXIQeu KgZQ== X-Gm-Message-State: AOAM532JkNg3WA+VqaNgh9XmGX5SMaTBEDRWeSdsI0iWdGd7YrgQjhLl LDYhkWOUY7XLnaucTwtbKIHKoej2lqE= X-Google-Smtp-Source: ABdhPJwtWOAns0TANabseZMUaUr3A+07qUEJ2zIEofPWG2sZrnQqEkWZqrMhxv4ZvvTj5ODTutHnxQ== X-Received: by 2002:ae9:eb06:: with SMTP id b6mr4540793qkg.417.1633725199865; Fri, 08 Oct 2021 13:33:19 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:795f:367d:1f1e:4801]) by smtp.gmail.com with ESMTPSA id c8sm381945qtb.9.2021.10.08.13.33.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 13:33:19 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v4 2/4] skmsg: extract and reuse sk_msg_is_readable() Date: Fri, 8 Oct 2021 13:33:04 -0700 Message-Id: <20211008203306.37525-3-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211008203306.37525-1-xiyou.wangcong@gmail.com> References: <20211008203306.37525-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang tcp_bpf_sock_is_readable() is pretty much generic, we can extract it and reuse it for non-TCP sockets. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- include/linux/skmsg.h | 1 + net/core/skmsg.c | 14 ++++++++++++++ net/ipv4/tcp_bpf.c | 15 +-------------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index 14ab0c0bc924..1ce9a9eb223b 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -128,6 +128,7 @@ int sk_msg_memcopy_from_iter(struct sock *sk, struct iov_iter *from, struct sk_msg *msg, u32 bytes); int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg, int len, int flags); +bool sk_msg_is_readable(struct sock *sk); static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes) { diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 2d6249b28928..a86ef7e844f8 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -474,6 +474,20 @@ int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg, } EXPORT_SYMBOL_GPL(sk_msg_recvmsg); +bool sk_msg_is_readable(struct sock *sk) +{ + struct sk_psock *psock; + bool empty = true; + + rcu_read_lock(); + psock = sk_psock(sk); + if (likely(psock)) + empty = list_empty(&psock->ingress_msg); + rcu_read_unlock(); + return !empty; +} +EXPORT_SYMBOL_GPL(sk_msg_is_readable); + static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk, struct sk_buff *skb) { diff --git a/net/ipv4/tcp_bpf.c b/net/ipv4/tcp_bpf.c index 0175dbcb7722..dabbc93e31b6 100644 --- a/net/ipv4/tcp_bpf.c +++ b/net/ipv4/tcp_bpf.c @@ -150,19 +150,6 @@ int tcp_bpf_sendmsg_redir(struct sock *sk, struct sk_msg *msg, EXPORT_SYMBOL_GPL(tcp_bpf_sendmsg_redir); #ifdef CONFIG_BPF_SYSCALL -static bool tcp_bpf_sock_is_readable(struct sock *sk) -{ - struct sk_psock *psock; - bool empty = true; - - rcu_read_lock(); - psock = sk_psock(sk); - if (likely(psock)) - empty = list_empty(&psock->ingress_msg); - rcu_read_unlock(); - return !empty; -} - static int tcp_msg_wait_data(struct sock *sk, struct sk_psock *psock, long timeo) { @@ -479,7 +466,7 @@ static void tcp_bpf_rebuild_protos(struct proto prot[TCP_BPF_NUM_CFGS], prot[TCP_BPF_BASE].unhash = sock_map_unhash; prot[TCP_BPF_BASE].close = sock_map_close; prot[TCP_BPF_BASE].recvmsg = tcp_bpf_recvmsg; - prot[TCP_BPF_BASE].sock_is_readable = tcp_bpf_sock_is_readable; + prot[TCP_BPF_BASE].sock_is_readable = sk_msg_is_readable; prot[TCP_BPF_TX] = prot[TCP_BPF_BASE]; prot[TCP_BPF_TX].sendmsg = tcp_bpf_sendmsg; From patchwork Fri Oct 8 20:33:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12546403 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8DC8C433F5 for ; Fri, 8 Oct 2021 20:33:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C178A61040 for ; Fri, 8 Oct 2021 20:33:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242880AbhJHUfU (ORCPT ); Fri, 8 Oct 2021 16:35:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242687AbhJHUfR (ORCPT ); Fri, 8 Oct 2021 16:35:17 -0400 Received: from mail-qk1-x733.google.com (mail-qk1-x733.google.com [IPv6:2607:f8b0:4864:20::733]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 218ECC061570; Fri, 8 Oct 2021 13:33:22 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id bk7so1669636qkb.13; Fri, 08 Oct 2021 13:33:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CYvMgF3Mg6V2OX7HnsOshqPQnZjQe8a0VLiCDJf1sy8=; b=H3P8aXRB9l5j4qQlBtSvUFf3J+Bfw8CHV3NpqyKZxkK7mYVIKJmcwqzatHQzYYVJOt YLcwwYllLsOVA0rs4DBq60Tr8oWTUxG63FxjRhUQFAsqPsWiBvx199wg/nSgrUR2HJ1L /SH9npr56DrN8r4OHUrTmBcunXA08SCDJNuevi5rWaaOzVLmxXgmm/F6VoPgFwzElKKU j5iTD5v9abh6l3DVSx7LUlhAs3Ds/3ED6VCDs/tsSHXSiWi+b29rXJRydFsRfXN09Vbe 3QpIBSvSyK76ixXBqdv5aGbk0igK9G4M76kPCcdpXq375CrNqMkQku0mB2t5JD68ZkDp ndUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CYvMgF3Mg6V2OX7HnsOshqPQnZjQe8a0VLiCDJf1sy8=; b=FehE4IItKXQ95zFNgEdeCXGQ1ol/mpCcEkDVVnqQoYSYikz0dtCroEsyyo15pV1VC9 gyOmqGlGmAzqdJoXSS296iaOnYagXKHeQ5DfmCPCEsFHXf5R3CX1mCbo/sS2zvyllvXK 62CJNou7Kz5fwc0Mh5nL9o+Zv2Z+aKBGx2ySzLK1o+ybs/1hIovibeNVcAAVY3C96Gbf KlTR0k8mjjsNY53HbhBwpe+EN41F85gCPfjbuq6Ra30LMbS28vMB6GdwtAbV1aIcIKBu Av9nfkNQRYpil2xqkQ7WOr3kq18q8OZCj58oqpRYxqN5xrbcXqSRolf/erlV2HclQOwV mvEQ== X-Gm-Message-State: AOAM530/8vqY9His86a6Bw+JoLPBuRSFSiemHO9m/WGEYVqkQWEwxqZc GgmR9Q4bwwUyneWl4Ehb8jS8mTJBk2Y= X-Google-Smtp-Source: ABdhPJzrWWXH+h7JaIsRQpOWRR4LdVjzGpQrvobGBDFmoigDqibKoBQyvywykHvloAF5R9COgfNLSA== X-Received: by 2002:a37:a944:: with SMTP id s65mr4708867qke.263.1633725201025; Fri, 08 Oct 2021 13:33:21 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:795f:367d:1f1e:4801]) by smtp.gmail.com with ESMTPSA id c8sm381945qtb.9.2021.10.08.13.33.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 13:33:20 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Cong Wang , Yucong Sun , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer Subject: [Patch bpf v4 3/4] net: implement ->sock_is_readable() for UDP and AF_UNIX Date: Fri, 8 Oct 2021 13:33:05 -0700 Message-Id: <20211008203306.37525-4-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211008203306.37525-1-xiyou.wangcong@gmail.com> References: <20211008203306.37525-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang Yucong noticed we can't poll() sockets in sockmap even when they are the destination sockets of redirections. This is because we never poll any psock queues in ->poll(), except for TCP. With ->sock_is_readable() now we can overwrite >sock_is_readable(), invoke and implement it for both UDP and AF_UNIX sockets. Reported-by: Yucong Sun Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/ipv4/udp.c | 3 +++ net/ipv4/udp_bpf.c | 1 + net/unix/af_unix.c | 4 ++++ net/unix/unix_bpf.c | 2 ++ 4 files changed, 10 insertions(+) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 8536b2a7210b..2fffcf2b54f3 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -2867,6 +2867,9 @@ __poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait) !(sk->sk_shutdown & RCV_SHUTDOWN) && first_packet_length(sk) == -1) mask &= ~(EPOLLIN | EPOLLRDNORM); + /* psock ingress_msg queue should not contain any bad checksum frames */ + if (sk_is_readable(sk)) + mask |= EPOLLIN | EPOLLRDNORM; return mask; } diff --git a/net/ipv4/udp_bpf.c b/net/ipv4/udp_bpf.c index 7a1d5f473878..bbe6569c9ad3 100644 --- a/net/ipv4/udp_bpf.c +++ b/net/ipv4/udp_bpf.c @@ -114,6 +114,7 @@ static void udp_bpf_rebuild_protos(struct proto *prot, const struct proto *base) *prot = *base; prot->close = sock_map_close; prot->recvmsg = udp_bpf_recvmsg; + prot->sock_is_readable = sk_msg_is_readable; } static void udp_bpf_check_v6_needs_rebuild(struct proto *ops) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 0878ab86597b..9e62d83ffd3e 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -3052,6 +3052,8 @@ static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wa /* readable? */ if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) mask |= EPOLLIN | EPOLLRDNORM; + if (sk_is_readable(sk)) + mask |= EPOLLIN | EPOLLRDNORM; /* Connection-based need to check for termination and startup */ if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && @@ -3091,6 +3093,8 @@ static __poll_t unix_dgram_poll(struct file *file, struct socket *sock, /* readable? */ if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) mask |= EPOLLIN | EPOLLRDNORM; + if (sk_is_readable(sk)) + mask |= EPOLLIN | EPOLLRDNORM; /* Connection-based need to check for termination and startup */ if (sk->sk_type == SOCK_SEQPACKET) { diff --git a/net/unix/unix_bpf.c b/net/unix/unix_bpf.c index b927e2baae50..452376c6f419 100644 --- a/net/unix/unix_bpf.c +++ b/net/unix/unix_bpf.c @@ -102,6 +102,7 @@ static void unix_dgram_bpf_rebuild_protos(struct proto *prot, const struct proto *prot = *base; prot->close = sock_map_close; prot->recvmsg = unix_bpf_recvmsg; + prot->sock_is_readable = sk_msg_is_readable; } static void unix_stream_bpf_rebuild_protos(struct proto *prot, @@ -110,6 +111,7 @@ static void unix_stream_bpf_rebuild_protos(struct proto *prot, *prot = *base; prot->close = sock_map_close; prot->recvmsg = unix_bpf_recvmsg; + prot->sock_is_readable = sk_msg_is_readable; prot->unhash = sock_map_unhash; } From patchwork Fri Oct 8 20:33:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12546405 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CA5CAC433FE for ; Fri, 8 Oct 2021 20:33:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AEF80610E5 for ; Fri, 8 Oct 2021 20:33:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242932AbhJHUfW (ORCPT ); Fri, 8 Oct 2021 16:35:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242856AbhJHUfT (ORCPT ); Fri, 8 Oct 2021 16:35:19 -0400 Received: from mail-qv1-xf31.google.com (mail-qv1-xf31.google.com [IPv6:2607:f8b0:4864:20::f31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C712C061570; Fri, 8 Oct 2021 13:33:24 -0700 (PDT) Received: by mail-qv1-xf31.google.com with SMTP id o13so7167372qvm.4; Fri, 08 Oct 2021 13:33:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ysNfuXl1HWktNtOSEF03F01sMlIv53eUMXjaR2LS9EI=; b=AJnVX/Dn9IYIB0wCnMlDtfrso2E5XPJNmW/3HDfnruiUzKZ3lQsgeS4qq5vvKCll21 3mqRcHREFFSLKhkkACmHGpaeodhLM7IPC+2I25nejcokZepJhggL0vhv452fJy1I81k4 Uekb3Cwm8sYPVNZGq+O2Dn2xhBTLfhU/kOly9gTJWqwE7NexHIoggFT0tAC3g0/GVeeg hdHX18vJi0mwmWgmGZ385LvTaE8Vlli/atk+7wDtMZVvg5SviydqcLbW6AzGbL4RBCVQ 6o4uqlI+5Nznx69t57Zt2Xw7i47hqC6vHsmBQGxHIN9H6ojQKeUpSs+W6gyQTtwg38AQ 4SbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ysNfuXl1HWktNtOSEF03F01sMlIv53eUMXjaR2LS9EI=; b=5cEiGbH5/3dTFwMD2K0xCa2rtGaUBgN8M4CVMhgylif/lL4cJFkCvgwsed9VCH4tFa 6ozHwIN2BgDMDb8H7SBI+NP2rjna1nc8Gfeb/ZS3Q93dVpgnxRzqDGZrtJ0WWWnTGmCO 9qoZMtJ9LnS6gp1la/MUQiCxFdeO3iFMDHHwiIoMdEMXLiV0P25K1u5XVWXPk78xt9c3 uzySH0llSJ6PGXC7PkKMJMlYmo36ixUvlTPQPTmSaE/bWu1CUEDnk2k07wiiwxBU1to5 49HgFWlijIO6MX2GNPfsFo19ZmeWq1MKfImEFv+0CibkqqEg1Q/7+tWEt6DPqer/RQ6Q 2wzw== X-Gm-Message-State: AOAM532XYVpzwv4MrdFIBmnyD1hV0u+MXxAWCt5DtmlRBNzbMH2FQt5Q qnXSn5x61ZpyAlAHaMXCSS0469ANYfI= X-Google-Smtp-Source: ABdhPJz2dHX7v8GUadyuk/rjP/VqwBho7FYSAVVJVSCv+dWjZN/nXz3zAWDI2i/KGWkWIY+QoieI4A== X-Received: by 2002:a05:6214:a4d:: with SMTP id ee13mr12078196qvb.6.1633725202220; Fri, 08 Oct 2021 13:33:22 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:795f:367d:1f1e:4801]) by smtp.gmail.com with ESMTPSA id c8sm381945qtb.9.2021.10.08.13.33.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 13:33:21 -0700 (PDT) From: Cong Wang To: netdev@vger.kernel.org Cc: bpf@vger.kernel.org, Yucong Sun , John Fastabend , Daniel Borkmann , Jakub Sitnicki , Lorenz Bauer , Cong Wang Subject: [Patch bpf v4 4/4] selftests/bpf: use recv_timeout() instead of retries Date: Fri, 8 Oct 2021 13:33:06 -0700 Message-Id: <20211008203306.37525-5-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211008203306.37525-1-xiyou.wangcong@gmail.com> References: <20211008203306.37525-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Yucong Sun We use non-blocking sockets in those tests, retrying for EAGAIN is ugly because there is no upper bound for the packet arrival time, at least in theory. After we fix poll() on sockmap sockets, now we can switch to select()+recv(). Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Yucong Sun Signed-off-by: Cong Wang --- .../selftests/bpf/prog_tests/sockmap_listen.c | 75 +++++-------------- 1 file changed, 20 insertions(+), 55 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index 5c5979046523..d88bb65b74cc 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -949,7 +949,6 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd, int err, n; u32 key; char b; - int retries = 100; zero_verdict_count(verd_mapfd); @@ -1002,17 +1001,11 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd, goto close_peer1; if (pass != 1) FAIL("%s: want pass count 1, have %d", log_prefix, pass); -again: - n = read(c0, &b, 1); - if (n < 0) { - if (errno == EAGAIN && retries--) { - usleep(1000); - goto again; - } - FAIL_ERRNO("%s: read", log_prefix); - } + n = recv_timeout(c0, &b, 1, 0, IO_TIMEOUT_SEC); + if (n < 0) + FAIL_ERRNO("%s: recv_timeout", log_prefix); if (n == 0) - FAIL("%s: incomplete read", log_prefix); + FAIL("%s: incomplete recv", log_prefix); close_peer1: xclose(p1); @@ -1571,7 +1564,6 @@ static void unix_redir_to_connected(int sotype, int sock_mapfd, const char *log_prefix = redir_mode_str(mode); int c0, c1, p0, p1; unsigned int pass; - int retries = 100; int err, n; int sfd[2]; u32 key; @@ -1606,17 +1598,11 @@ static void unix_redir_to_connected(int sotype, int sock_mapfd, if (pass != 1) FAIL("%s: want pass count 1, have %d", log_prefix, pass); -again: - n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); - if (n < 0) { - if (errno == EAGAIN && retries--) { - usleep(1000); - goto again; - } - FAIL_ERRNO("%s: read", log_prefix); - } + n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC); + if (n < 0) + FAIL_ERRNO("%s: recv_timeout", log_prefix); if (n == 0) - FAIL("%s: incomplete read", log_prefix); + FAIL("%s: incomplete recv", log_prefix); close: xclose(c1); @@ -1748,7 +1734,6 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, const char *log_prefix = redir_mode_str(mode); int c0, c1, p0, p1; unsigned int pass; - int retries = 100; int err, n; u32 key; char b; @@ -1781,17 +1766,11 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, if (pass != 1) FAIL("%s: want pass count 1, have %d", log_prefix, pass); -again: - n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); - if (n < 0) { - if (errno == EAGAIN && retries--) { - usleep(1000); - goto again; - } - FAIL_ERRNO("%s: read", log_prefix); - } + n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC); + if (n < 0) + FAIL_ERRNO("%s: recv_timeout", log_prefix); if (n == 0) - FAIL("%s: incomplete read", log_prefix); + FAIL("%s: incomplete recv", log_prefix); close_cli1: xclose(c1); @@ -1841,7 +1820,6 @@ static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd, const char *log_prefix = redir_mode_str(mode); int c0, c1, p0, p1; unsigned int pass; - int retries = 100; int err, n; int sfd[2]; u32 key; @@ -1876,17 +1854,11 @@ static void inet_unix_redir_to_connected(int family, int type, int sock_mapfd, if (pass != 1) FAIL("%s: want pass count 1, have %d", log_prefix, pass); -again: - n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); - if (n < 0) { - if (errno == EAGAIN && retries--) { - usleep(1000); - goto again; - } - FAIL_ERRNO("%s: read", log_prefix); - } + n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC); + if (n < 0) + FAIL_ERRNO("%s: recv_timeout", log_prefix); if (n == 0) - FAIL("%s: incomplete read", log_prefix); + FAIL("%s: incomplete recv", log_prefix); close_cli1: xclose(c1); @@ -1932,7 +1904,6 @@ static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd, int sfd[2]; u32 key; char b; - int retries = 100; zero_verdict_count(verd_mapfd); @@ -1963,17 +1934,11 @@ static void unix_inet_redir_to_connected(int family, int type, int sock_mapfd, if (pass != 1) FAIL("%s: want pass count 1, have %d", log_prefix, pass); -again: - n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); - if (n < 0) { - if (errno == EAGAIN && retries--) { - usleep(1000); - goto again; - } - FAIL_ERRNO("%s: read", log_prefix); - } + n = recv_timeout(mode == REDIR_INGRESS ? p0 : c0, &b, 1, 0, IO_TIMEOUT_SEC); + if (n < 0) + FAIL_ERRNO("%s: recv_timeout", log_prefix); if (n == 0) - FAIL("%s: incomplete read", log_prefix); + FAIL("%s: incomplete recv", log_prefix); close: xclose(c1);