From patchwork Fri Sep 24 22:05: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: 12516899 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 23D05C4332F for ; Fri, 24 Sep 2021 22:05:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0B30A61212 for ; Fri, 24 Sep 2021 22:05:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344683AbhIXWHB (ORCPT ); Fri, 24 Sep 2021 18:07:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59018 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344595AbhIXWHA (ORCPT ); Fri, 24 Sep 2021 18:07:00 -0400 Received: from mail-qk1-x72f.google.com (mail-qk1-x72f.google.com [IPv6:2607:f8b0:4864:20::72f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4A48C061571; Fri, 24 Sep 2021 15:05:26 -0700 (PDT) Received: by mail-qk1-x72f.google.com with SMTP id 138so29405468qko.10; Fri, 24 Sep 2021 15:05:26 -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=+Its/Gyc2Kb4yZVwCQrfT/u79ExJ0kh4zkvYOSK5lAA=; b=mc1X2VLQeh1eGsSRKDL9XjlSFnWHqglrqo/gmlNEwrmntjBq5HbUk7ACSheufZl0/m ApjD82WwtBCCCrz+Thc2Nf2Ivtg5LUDYRN8dksv8I8/tTLk8re/yUQyx6u266B0Izync 23IHH9xeBT0n3NXVMTN/woL2o1dlhtPBSey0iphU3Xch+unfbNuF3wvjGO0ajjgXRhM4 YuybKsrWiU8cxV5+zB5YPAxTxmUWZIdm2cfwB/gtqqwbR7+5WmY5GRMGc4rXGE57wGKi /cdfBfp/QznI4SyegxipfQU1rARS0k0vh9SOwuyftOz71KvJ9878RKuOfpIW4UFBU5Qr /tNA== 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=+Its/Gyc2Kb4yZVwCQrfT/u79ExJ0kh4zkvYOSK5lAA=; b=2Kab3TUGvSNJccbPQfS4GFmGqtGQ04d3TJ8aVAyaVFAhHaz2izZyjZlZYB+0NK7Ysz vU4rAzNSJbGxoAJ26I8MC5Rd3flVN88Y3IiCd/t+Zyhsqv4NVeVP8yAE/EQAD0RdYxMo Iy8h5qTYLyim1g8J7y/UDDPAx2PRRv0wGlCWGBRvxlywItuLIMh/mdnsHu/Q/8B4d3jM Or5Rtk2ie6jDLbH0DtAhOpBFsqKK/9R9aFobviKU3LCv1XabBDv4RUTovnHCT9fdtkJA bcUnnggH9ecR3ceYkaHfirdt5PBzkd8mTpSAwGJciKov/8asreOJA138LedLmbSpvH6S TPAA== X-Gm-Message-State: AOAM533KllLFfLSFPLMOFbJ8GSn9UNtqV43tNFm/Ch6ufD99VoaKx+fF LWmlDzTLqaG8R5/Q/Ty1yCcELlnq+q4= X-Google-Smtp-Source: ABdhPJyrnz/+AF6JmH7VyISYC0jQkfOWgSx94yIFPxNiLDq1jR21VoLJPDp+WlcUI8VtjBEtOdphVQ== X-Received: by 2002:a37:b0c6:: with SMTP id z189mr13169831qke.344.1632521125943; Fri, 24 Sep 2021 15:05:25 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:c4dc:647c:f35b:bfc4]) by smtp.gmail.com with ESMTPSA id h2sm7895683qkf.106.2021.09.24.15.05.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 15:05:25 -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 1/3] skmsg: introduce sk_psock_get_checked() Date: Fri, 24 Sep 2021 15:05:05 -0700 Message-Id: <20210924220507.24543-2-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210924220507.24543-1-xiyou.wangcong@gmail.com> References: <20210924220507.24543-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net From: Cong Wang Although we have sk_psock_get(), it assumes the psock retrieved from sk_user_data is for sockmap, this is not sufficient if we call it outside of sockmap, for example, reuseport_array. Fortunately sock_map_psock_get_checked() is more strict and checks for sock_map_close before using psock. So we can refactor it and rename it to sk_psock_get_checked(), which can be safely called outside of sockmap. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- include/linux/skmsg.h | 20 ++++++++++++++++++++ net/core/sock_map.c | 22 +--------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index 14ab0c0bc924..d47097f2c8c0 100644 --- a/include/linux/skmsg.h +++ b/include/linux/skmsg.h @@ -452,6 +452,26 @@ static inline struct sk_psock *sk_psock_get(struct sock *sk) return psock; } +static inline struct sk_psock *sk_psock_get_checked(struct sock *sk) +{ + struct sk_psock *psock; + + rcu_read_lock(); + psock = sk_psock(sk); + if (psock) { + if (sk->sk_prot->close != sock_map_close) { + psock = ERR_PTR(-EBUSY); + goto out; + } + + if (!refcount_inc_not_zero(&psock->refcnt)) + psock = ERR_PTR(-EBUSY); + } +out: + rcu_read_unlock(); + return psock; +} + void sk_psock_drop(struct sock *sk, struct sk_psock *psock); static inline void sk_psock_put(struct sock *sk, struct sk_psock *psock) diff --git a/net/core/sock_map.c b/net/core/sock_map.c index e252b8ec2b85..6612bb0b95b5 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -191,26 +191,6 @@ static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock) return sk->sk_prot->psock_update_sk_prot(sk, psock, false); } -static struct sk_psock *sock_map_psock_get_checked(struct sock *sk) -{ - struct sk_psock *psock; - - rcu_read_lock(); - psock = sk_psock(sk); - if (psock) { - if (sk->sk_prot->close != sock_map_close) { - psock = ERR_PTR(-EBUSY); - goto out; - } - - if (!refcount_inc_not_zero(&psock->refcnt)) - psock = ERR_PTR(-EBUSY); - } -out: - rcu_read_unlock(); - return psock; -} - static int sock_map_link(struct bpf_map *map, struct sock *sk) { struct sk_psock_progs *progs = sock_map_progs(map); @@ -255,7 +235,7 @@ static int sock_map_link(struct bpf_map *map, struct sock *sk) } } - psock = sock_map_psock_get_checked(sk); + psock = sk_psock_get_checked(sk); if (IS_ERR(psock)) { ret = PTR_ERR(psock); goto out_progs; From patchwork Fri Sep 24 22:05: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: 12516901 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 D3296C433F5 for ; Fri, 24 Sep 2021 22:05:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C052D610D1 for ; Fri, 24 Sep 2021 22:05:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344595AbhIXWHC (ORCPT ); Fri, 24 Sep 2021 18:07:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59026 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344739AbhIXWHB (ORCPT ); Fri, 24 Sep 2021 18:07:01 -0400 Received: from mail-qk1-x731.google.com (mail-qk1-x731.google.com [IPv6:2607:f8b0:4864:20::731]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FFD4C061571; Fri, 24 Sep 2021 15:05:28 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id c7so29999861qka.2; Fri, 24 Sep 2021 15:05:28 -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=9AaGoiEPLlbptgntBN+mJpO46ns+attsaZXlwJ1z2hk=; b=SwFdHxrA1axmOZxfJkxsuFkd/PYieFc5voQ0gOYYpQ069Rgiu2q4+vwGQc/ExNKVHL NB4Imvcm6mSiuO5t3+NDfWvrsty73rQEGaTQZop+zPimvc8bcZ7sLybtYfDukpCYQA25 KGV8DK4Yy2Vqg0RR5Dkd/lxQinXUZfanHBokhf5QL8lrP4+mFu1IAvzUCpot5IMprfHN vdpr6Isx512fGpGlrEYqJh3ZkLkaoTmupMoiDVRwkj5wjM0yI+WMhjLTdw7tS/1T25Y2 59xGh9UwlR5/MqvDsRy4J5mDWv+1f++oNuU6Tlf4M13K+lMKXLUrYVfWFM+RvuxbUWzB I2Jg== 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=9AaGoiEPLlbptgntBN+mJpO46ns+attsaZXlwJ1z2hk=; b=DwM/uiBMR05ncnfPlVfN4FfYzeFYfO6OnI2OrbBaVUw2090AWToqUZ06GbWu8E6Hcp f1xoxL2swHU15PqshNllQHJ3zkP330XSbnfdrk/mG8yerYMXYNcAqoIktIoqZGopVoaU I6/vyk2phSz8gBF16E9/Uy3pyAKz2pSxh0geCjqPdXrNtp9t31E8mZhtMdG+ZcWxsh9e WAnK/U7ffiINFQPPxbyVNYX/m2ZdZcDqJWjQA/Dpx0jXGSFEHAwv9vPA3aiOydxzIQmK IWMpEZIGfV2dWNNHrrkhAxytC8kMNKvtVMP1my9YZxg5AK4osfRNThIwKGHDTD+/dpzQ k/rA== X-Gm-Message-State: AOAM531ByKchsyJvO2VsuFReD8PtX9Zu7212jofPKbwFCEh9ycS2yMz6 Msjnc+SxIChLm1BmXoFhVRcPgURly0c= X-Google-Smtp-Source: ABdhPJwrhlm5tiahG/Rui/WhldLGX3vY7bPxj6GZQzeUfb359FqsO9hAKoDvoso6kE3KRtYnW9a/jg== X-Received: by 2002:a37:6596:: with SMTP id z144mr13295209qkb.292.1632521127163; Fri, 24 Sep 2021 15:05:27 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:c4dc:647c:f35b:bfc4]) by smtp.gmail.com with ESMTPSA id h2sm7895683qkf.106.2021.09.24.15.05.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 15:05:26 -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 2/3] net: poll psock queues too for sockmap sockets Date: Fri, 24 Sep 2021 15:05:06 -0700 Message-Id: <20210924220507.24543-3-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210924220507.24543-1-xiyou.wangcong@gmail.com> References: <20210924220507.24543-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@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(). We can not overwrite ->poll() as it is in struct proto_ops, not in struct proto. So introduce sk_msg_poll() to poll psock ingress_msg queue and let sockets which support sockmap invoke it directly. Reported-by: Yucong Sun Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- include/linux/skmsg.h | 6 ++++++ net/core/skmsg.c | 15 +++++++++++++++ net/ipv4/tcp.c | 2 ++ net/ipv4/udp.c | 2 ++ net/unix/af_unix.c | 5 +++++ 5 files changed, 30 insertions(+) diff --git a/include/linux/skmsg.h b/include/linux/skmsg.h index d47097f2c8c0..163b0cc1703a 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); +__poll_t sk_msg_poll(struct sock *sk); static inline void sk_msg_check_to_free(struct sk_msg *msg, u32 i, u32 bytes) { @@ -562,5 +563,10 @@ static inline void skb_bpf_redirect_clear(struct sk_buff *skb) { skb->_sk_redir = 0; } +#else +static inline __poll_t sk_msg_poll(struct sock *sk) +{ + return 0; +} #endif /* CONFIG_NET_SOCK_MSG */ #endif /* _LINUX_SKMSG_H */ diff --git a/net/core/skmsg.c b/net/core/skmsg.c index 2d6249b28928..8e6d7ea43eca 100644 --- a/net/core/skmsg.c +++ b/net/core/skmsg.c @@ -474,6 +474,21 @@ int sk_msg_recvmsg(struct sock *sk, struct sk_psock *psock, struct msghdr *msg, } EXPORT_SYMBOL_GPL(sk_msg_recvmsg); +__poll_t sk_msg_poll(struct sock *sk) +{ + struct sk_psock *psock; + __poll_t mask = 0; + + psock = sk_psock_get_checked(sk); + if (IS_ERR_OR_NULL(psock)) + return 0; + if (!sk_psock_queue_empty(psock)) + mask |= EPOLLIN | EPOLLRDNORM; + sk_psock_put(sk, psock); + return mask; +} +EXPORT_SYMBOL_GPL(sk_msg_poll); + static struct sk_msg *sk_psock_create_ingress_msg(struct sock *sk, struct sk_buff *skb) { diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c index e8b48df73c85..2eb1a87ba056 100644 --- a/net/ipv4/tcp.c +++ b/net/ipv4/tcp.c @@ -280,6 +280,7 @@ #include #include #include +#include /* Track pending CMSGs. */ enum { @@ -563,6 +564,7 @@ __poll_t tcp_poll(struct file *file, struct socket *sock, poll_table *wait) if (tcp_stream_is_readable(sk, target)) mask |= EPOLLIN | EPOLLRDNORM; + mask |= sk_msg_poll(sk); if (!(sk->sk_shutdown & SEND_SHUTDOWN)) { if (__sk_stream_is_writeable(sk, 1)) { diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 8851c9463b4b..fbc989d27388 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -97,6 +97,7 @@ #include #include #include +#include #include #include #include @@ -2866,6 +2867,7 @@ __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); + mask |= sk_msg_poll(sk); return mask; } diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 92345c9bb60c..5d705541d082 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -114,6 +114,7 @@ #include #include #include +#include #include "scm.h" @@ -3015,6 +3016,8 @@ static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wa if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) mask |= EPOLLIN | EPOLLRDNORM; + mask |= sk_msg_poll(sk); + /* Connection-based need to check for termination and startup */ if ((sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) && sk->sk_state == TCP_CLOSE) @@ -3054,6 +3057,8 @@ static __poll_t unix_dgram_poll(struct file *file, struct socket *sock, if (!skb_queue_empty_lockless(&sk->sk_receive_queue)) mask |= EPOLLIN | EPOLLRDNORM; + mask |= sk_msg_poll(sk); + /* Connection-based need to check for termination and startup */ if (sk->sk_type == SOCK_SEQPACKET) { if (sk->sk_state == TCP_CLOSE) From patchwork Fri Sep 24 22:05:07 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 12516903 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 0CEE3C433EF for ; Fri, 24 Sep 2021 22:05:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EAFDF611C8 for ; Fri, 24 Sep 2021 22:05:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344893AbhIXWHD (ORCPT ); Fri, 24 Sep 2021 18:07:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344814AbhIXWHC (ORCPT ); Fri, 24 Sep 2021 18:07:02 -0400 Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BD29C061571; Fri, 24 Sep 2021 15:05:29 -0700 (PDT) Received: by mail-qt1-x833.google.com with SMTP id t13so4968057qtc.7; Fri, 24 Sep 2021 15:05:29 -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=D8GRaF+Cd4vVYumfiCzzhE1rcgBnfSGVXamJ55BCvaM=; b=OZZLSyaqSJDyoWyRJjpJrBy6jcssEJuXeaxFzTDn519cyxpiuy4lzA1h+bDQbKuTVM 0Xh2k4k4dlG4DNZkXMdMFZskop+8id58l5dNWQrPI0MnpbrdmRXa9hxl3Dx74GIIXwdd JEkYYJSBMiNciofawt2YghY3CgSN48xl835ZGR1H12QWfXC4ehp/KfaNfOpq7Qifd6nT aKcj42pNnEtTNZmHR0mU0tX9kntB4KHLuTMBqcxbFOH55BERaFkqpx/MTSaneQbbfX9E R7Szh5DGsvG+X2Uoo9iFTa1Mo6cN4zyneCqS1NGcYQAr0yaRcsp8fPKJ1TDxVZSf1SSk 3+KA== 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=D8GRaF+Cd4vVYumfiCzzhE1rcgBnfSGVXamJ55BCvaM=; b=dTcdHIkzN7B0y9UBtJKmi82OTIIKMV2UHh2GVm5Nrsyc8longipXYL82qiyZTpGVXy mZ3n3TfuesCKOC84kDvWbYEXBC/R2DjP7PoOfd0Bmo7cuv/keKxlHUpZaRGrg1JV68Gy 8ddMAVUMHvwV/4XLMgTMddHeF5bd4eWhG7URj+/AZ3FPGY7T5kVXmFBTobyhfwvnmzh9 0vstCByXlycqTr1EL2xllrRPn2Y3Ur1ab94/Lf0yPWDFbPaevpxkA3NQXgIjVTywOT1V fxUqHHO0K9t/j1hUIGcdHcn8SgeTJPX+TUR85d1oaylMDrheE7Fy9OTbfdV9uuX+xUFp fm0A== X-Gm-Message-State: AOAM532s2KRbymdctU9MN7uNDoufty1Qo45F8VSV1HPyZQXuDPwGee5z B6DvqQIv65PoRelmoYRkEPiJndHFqvQ= X-Google-Smtp-Source: ABdhPJxFQOSpXr1uOAYl59s+XdY5R0Osn1u0EyhkocvSlUiwJuDXmBUWfkk8J1ckJHJ2rqcyI7IDqQ== X-Received: by 2002:ac8:6b98:: with SMTP id z24mr6895443qts.107.1632521128454; Fri, 24 Sep 2021 15:05:28 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:c4dc:647c:f35b:bfc4]) by smtp.gmail.com with ESMTPSA id h2sm7895683qkf.106.2021.09.24.15.05.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 15:05:28 -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 3/3] selftests/bpf: use recv_timeout() instead of retries Date: Fri, 24 Sep 2021 15:05:07 -0700 Message-Id: <20210924220507.24543-4-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210924220507.24543-1-xiyou.wangcong@gmail.com> References: <20210924220507.24543-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@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() for 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);