From patchwork Mon Apr 26 02:49:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 427760 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=-15.7 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,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 477DAC433ED for ; Mon, 26 Apr 2021 02:50:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2633761249 for ; Mon, 26 Apr 2021 02:50:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231751AbhDZCu5 (ORCPT ); Sun, 25 Apr 2021 22:50:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231502AbhDZCuz (ORCPT ); Sun, 25 Apr 2021 22:50:55 -0400 Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D65A5C061574; Sun, 25 Apr 2021 19:50:13 -0700 (PDT) Received: by mail-qk1-x72b.google.com with SMTP id x11so55199548qkp.11; Sun, 25 Apr 2021 19:50:13 -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=6pETDKNtrqYDuNN8d+r94G6x7BZ9WY+62HbnbQyw20g=; b=Rjj+MrygE3aJpWLiQ+HufxVt6CIVIviLDWL1w8Idnw2/g3zk6BuRPMCVUzzsM5ZeTm Sk+VkRgz0k0xMJE0UWHB/mVioQ5yx0J8Cj+kTmUOH3JEsOF68LqGmumNuJM3Rwq8K6Wx emUItn5JTUI95DkHSwjiQrxBsvuzaM86/IG2026NKTJWrmRD/HfcOS5pCVZuFi8VW8wW 9GwD+iQ5HDa0Xvs98N9cHNiI3niaMBpcQzj6IMAiQYfzTo9xhHRoWR1HXFIG1gtXNIdT NyhkVnJdtWWt1T2RENKd2O8+iCtwS9a32Po+jL0IfZuc4Ke56TMfxWZOfRBX+TblfvjZ rdLg== 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=6pETDKNtrqYDuNN8d+r94G6x7BZ9WY+62HbnbQyw20g=; b=QI9c69+DCkZEafCG1GM+j9Z7JuFRSsyG5zRI1rZ+b+O4Xn0h9zvlOOzyOERGzi7y3k 3cFn6840EPB1MqmZjpXkorTZ8EV+KiT6YiLrpSWuHXTHRoVfjWo2hO2g3bWK2s4yZ416 tnUzOGY4QbxvY7bpP0mcpFyhvb/Me8ukIQsyMjQn0335njaoXVzcfPTYEeOY2vht8O6s 3OSdGggkCmBC3z0Zhn5ObSoj2MQCwqeRjRQCeEu2GZbF4U6vbclQTGDm1LFJRrfLBLJw deAybDF35LpyfkbEpSnPmNbJ9E3Mh8WlTKMKg7jsWOPI0IuNJTPW/Xu3FZIRXkowzhya nDgg== X-Gm-Message-State: AOAM531PbeXdtja/n01JLfi/+Yb2ZBTFA9nK9hQH4bX+CCHwhyfEHsm1 r4Rfn0Xz5Ajo527QdhA3YHa5vR8LBT0m8w== X-Google-Smtp-Source: ABdhPJwkwNBg1MuRSAnSYL4B9SKvuoFxR6UqzAj7N1OU1WBmmZxqQJaZttLMSys9+M8nVr8KOjwK5g== X-Received: by 2002:a37:8ec4:: with SMTP id q187mr15358397qkd.381.1619405412960; Sun, 25 Apr 2021 19:50:12 -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.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:12 -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 01/10] sock_map: relax config dependency to CONFIG_NET Date: Sun, 25 Apr 2021 19:49:52 -0700 Message-Id: <20210426025001.7899-2-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 Currently sock_map still has hard dependency on CONFIG_INET, but there is no actual functional dependency on it after we introduce ->psock_update_sk_prot(). We have to extend it to CONFIG_NET now as we are going to support AF_UNIX. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- include/linux/bpf.h | 38 ++++++++++++++++++++------------------ init/Kconfig | 2 +- net/core/Makefile | 2 -- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index f8a45f109e96..27986021214d 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1824,6 +1824,12 @@ static inline bool bpf_map_is_dev_bound(struct bpf_map *map) struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr); void bpf_map_offload_map_free(struct bpf_map *map); + +int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog); +int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype); +int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags); +void sock_map_unhash(struct sock *sk); +void sock_map_close(struct sock *sk, long timeout); #else static inline int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr) @@ -1849,24 +1855,6 @@ static inline struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr) static inline void bpf_map_offload_map_free(struct bpf_map *map) { } -#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */ - -#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) -int sock_map_get_from_fd(const union bpf_attr *attr, struct bpf_prog *prog); -int sock_map_prog_detach(const union bpf_attr *attr, enum bpf_prog_type ptype); -int sock_map_update_elem_sys(struct bpf_map *map, void *key, void *value, u64 flags); -void sock_map_unhash(struct sock *sk); -void sock_map_close(struct sock *sk, long timeout); - -void bpf_sk_reuseport_detach(struct sock *sk); -int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key, - void *value); -int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, - void *value, u64 map_flags); -#else -static inline void bpf_sk_reuseport_detach(struct sock *sk) -{ -} #ifdef CONFIG_BPF_SYSCALL static inline int sock_map_get_from_fd(const union bpf_attr *attr, @@ -1886,7 +1874,21 @@ static inline int sock_map_update_elem_sys(struct bpf_map *map, void *key, void { return -EOPNOTSUPP; } +#endif /* CONFIG_BPF_SYSCALL */ +#endif /* CONFIG_NET && CONFIG_BPF_SYSCALL */ +#if defined(CONFIG_INET) && defined(CONFIG_BPF_SYSCALL) +void bpf_sk_reuseport_detach(struct sock *sk); +int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key, + void *value); +int bpf_fd_reuseport_array_update_elem(struct bpf_map *map, void *key, + void *value, u64 map_flags); +#else +static inline void bpf_sk_reuseport_detach(struct sock *sk) +{ +} + +#ifdef CONFIG_BPF_SYSCALL static inline int bpf_fd_reuseport_array_lookup_elem(struct bpf_map *map, void *key, void *value) { diff --git a/init/Kconfig b/init/Kconfig index 5deae45b8d81..161d0f19cdd9 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1708,7 +1708,7 @@ config BPF_SYSCALL select BPF select IRQ_WORK select TASKS_TRACE_RCU - select NET_SOCK_MSG if INET + select NET_SOCK_MSG if NET default n help Enable the bpf() system call that allows to manipulate eBPF diff --git a/net/core/Makefile b/net/core/Makefile index 0c2233c826fd..4b5a5a22386a 100644 --- a/net/core/Makefile +++ b/net/core/Makefile @@ -32,8 +32,6 @@ obj-$(CONFIG_HWBM) += hwbm.o obj-$(CONFIG_NET_DEVLINK) += devlink.o obj-$(CONFIG_GRO_CELLS) += gro_cells.o obj-$(CONFIG_FAILOVER) += failover.o -ifeq ($(CONFIG_INET),y) obj-$(CONFIG_NET_SOCK_MSG) += skmsg.o obj-$(CONFIG_BPF_SYSCALL) += sock_map.o -endif obj-$(CONFIG_BPF_SYSCALL) += bpf_sk_storage.o 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.. */ From patchwork Mon Apr 26 02:49:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 428434 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 3A6A6C43470 for ; Mon, 26 Apr 2021 02:50:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 195F36128A for ; Mon, 26 Apr 2021 02:50:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231786AbhDZCvB (ORCPT ); Sun, 25 Apr 2021 22:51:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231758AbhDZCu7 (ORCPT ); Sun, 25 Apr 2021 22:50:59 -0400 Received: from mail-qt1-x835.google.com (mail-qt1-x835.google.com [IPv6:2607:f8b0:4864:20::835]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C6FB8C06138B; Sun, 25 Apr 2021 19:50:16 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id o21so12528683qtp.7; Sun, 25 Apr 2021 19:50:16 -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=9RuAWLNCN2JVn8Ey2mCpSMGRVaX3wYjjsWZPW7i+saA=; b=iuTxBUa/7HnPdhECQDN3rFrc6qyx6a8p4OlKO8gsud+f4JX1JmyTU7Ya6pTVp74xXR KddtwktaMn4Y6JbtVTQEDoumiMipBLBx80nlwcdyPtLDqJP8kD1VKuVmApZfaPIKMKHU BqgncSUGZPsq1X6HCA4L0O9FVaVIhGHzjYqoBmCsErXYCq8g4zv2mJ84T4HIUuTDCBN2 yMRVSsySrGb9O4Ag9WFcP3LMhfqOxK+K2EIhTd0mdnew4vpT0MdBs/BA8htW5YH7E4aX Bmv+3NGSo8sKV29nDL1tgbxTvUA84alxQnbtmUO9ocRSjKXXd4JSYD/WwDc73W/7BISz 1FAA== 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=9RuAWLNCN2JVn8Ey2mCpSMGRVaX3wYjjsWZPW7i+saA=; b=s28X59kqOpAYFq1HOlLMxcwQr5AOeODFjkBmvOA2yUzQ2jbnSoNmmzJ6CyUd6trx8q 4FgoOtGAzYG3aFKBCsg++sCko0nop1awF75PdJ/fO7at9QlNldShqrS7n7P0vwo69v6x Uh8SRSB1U+SN41PB68KGEp1vaPS+T9ZOvujUFCYy4T22+ovMPBJJMX/9zC2g2hmouziw u02sYjXnsG/Fg7Iqb2qrB1SQidprO2l51GzQq6E/d53rz029OLAku3sWdhnGYTtq5An2 wMnWEXOqbRCW/BoHvXw2yqqIbrXmzwtnTCNHiTsfIrO05LOvLk4WMxIS9HNvURrGY8yD PBcQ== X-Gm-Message-State: AOAM530/WFE7TjsLnwPIMTqbbPGf4R2Fp+jeKImeFpQiI6kw2hN2j3vI vIaiOvn4MsfrJqRILWFxVaddWWeD7kdF5w== X-Google-Smtp-Source: ABdhPJwlCvnxkKp3E27lWFZ+Mfyp7t0yInemraRTL/I6ikHobw5gZTBONy67nmNN13yST14QHZ1sUg== X-Received: by 2002:ac8:7d4d:: with SMTP id h13mr1816175qtb.263.1619405415842; Sun, 25 Apr 2021 19:50:15 -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.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:15 -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 03/10] af_unix: implement ->psock_update_sk_prot() Date: Sun, 25 Apr 2021 19:49:54 -0700 Message-Id: <20210426025001.7899-4-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 unix_proto is special, it is very different from INET proto, which even does not have a ->close(). We have to add a dummy one to satisfy sockmap. And now we can implement unix_bpf_update_proto() to update sk_prot. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- MAINTAINERS | 1 + include/net/af_unix.h | 10 +++++++++ net/core/sock_map.c | 1 + net/unix/Makefile | 1 + net/unix/af_unix.c | 12 ++++++++++- net/unix/unix_bpf.c | 47 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 net/unix/unix_bpf.c diff --git a/MAINTAINERS b/MAINTAINERS index 217c7470bfa9..02532e11da5b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10000,6 +10000,7 @@ F: net/core/skmsg.c F: net/core/sock_map.c F: net/ipv4/tcp_bpf.c F: net/ipv4/udp_bpf.c +F: net/unix/unix_bpf.c LANTIQ / INTEL Ethernet drivers M: Hauke Mehrtens diff --git a/include/net/af_unix.h b/include/net/af_unix.h index f42fdddecd41..cca645846af1 100644 --- a/include/net/af_unix.h +++ b/include/net/af_unix.h @@ -89,4 +89,14 @@ void unix_sysctl_unregister(struct net *net); static inline int unix_sysctl_register(struct net *net) { return 0; } static inline void unix_sysctl_unregister(struct net *net) {} #endif + +#ifdef CONFIG_BPF_SYSCALL +extern struct proto unix_proto; + +int unix_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore); +void __init unix_bpf_build_proto(void); +#else +static inline void __init unix_bpf_build_proto(void) +{} +#endif #endif diff --git a/net/core/sock_map.c b/net/core/sock_map.c index 6f1b82b8ad49..1107c9dcc969 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -1536,6 +1536,7 @@ void sock_map_close(struct sock *sk, long timeout) release_sock(sk); saved_close(sk, timeout); } +EXPORT_SYMBOL_GPL(sock_map_close); static int sock_map_iter_attach_target(struct bpf_prog *prog, union bpf_iter_link_info *linfo, diff --git a/net/unix/Makefile b/net/unix/Makefile index 54e58cc4f945..20491825b4d0 100644 --- a/net/unix/Makefile +++ b/net/unix/Makefile @@ -7,6 +7,7 @@ obj-$(CONFIG_UNIX) += unix.o unix-y := af_unix.o garbage.o unix-$(CONFIG_SYSCTL) += sysctl_net_unix.o +unix-$(CONFIG_BPF_SYSCALL) += unix_bpf.o obj-$(CONFIG_UNIX_DIAG) += unix_diag.o unix_diag-y := diag.o diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index f4dc22db371d..8968ed44a89f 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -772,10 +772,18 @@ static const struct proto_ops unix_seqpacket_ops = { .show_fdinfo = unix_show_fdinfo, }; -static struct proto unix_proto = { +static void unix_close(struct sock *sk, long timeout) +{ +} + +struct proto unix_proto = { .name = "UNIX", .owner = THIS_MODULE, .obj_size = sizeof(struct unix_sock), + .close = unix_close, +#ifdef CONFIG_BPF_SYSCALL + .psock_update_sk_prot = unix_bpf_update_proto, +#endif }; static struct sock *unix_create1(struct net *net, struct socket *sock, int kern) @@ -859,6 +867,7 @@ static int unix_release(struct socket *sock) if (!sk) return 0; + sk->sk_prot->close(sk, 0); unix_release_sock(sk, 0); sock->sk = NULL; @@ -2958,6 +2967,7 @@ static int __init af_unix_init(void) sock_register(&unix_family_ops); register_pernet_subsys(&unix_net_ops); + unix_bpf_build_proto(); out: return rc; } diff --git a/net/unix/unix_bpf.c b/net/unix/unix_bpf.c new file mode 100644 index 000000000000..b1582a659427 --- /dev/null +++ b/net/unix/unix_bpf.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Cong Wang */ + +#include +#include +#include +#include + +static struct proto *unix_prot_saved __read_mostly; +static DEFINE_SPINLOCK(unix_prot_lock); +static struct proto unix_bpf_prot; + +static void unix_bpf_rebuild_protos(struct proto *prot, const struct proto *base) +{ + *prot = *base; + prot->close = sock_map_close; +} + +static void unix_bpf_check_needs_rebuild(struct proto *ops) +{ + if (unlikely(ops != smp_load_acquire(&unix_prot_saved))) { + spin_lock_bh(&unix_prot_lock); + if (likely(ops != unix_prot_saved)) { + unix_bpf_rebuild_protos(&unix_bpf_prot, ops); + smp_store_release(&unix_prot_saved, ops); + } + spin_unlock_bh(&unix_prot_lock); + } +} + +int unix_bpf_update_proto(struct sock *sk, struct sk_psock *psock, bool restore) +{ + if (restore) { + sk->sk_write_space = psock->saved_write_space; + WRITE_ONCE(sk->sk_prot, psock->sk_proto); + return 0; + } + + unix_bpf_check_needs_rebuild(psock->sk_proto); + WRITE_ONCE(sk->sk_prot, &unix_bpf_prot); + return 0; +} + +void __init unix_bpf_build_proto(void) +{ + unix_bpf_rebuild_protos(&unix_bpf_prot, &unix_proto); +} From patchwork Mon Apr 26 02:49:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 428435 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=-15.7 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,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 15F48C433ED for ; Mon, 26 Apr 2021 02:50:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E98F361263 for ; Mon, 26 Apr 2021 02:50:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231797AbhDZCvC (ORCPT ); Sun, 25 Apr 2021 22:51:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231502AbhDZCvA (ORCPT ); Sun, 25 Apr 2021 22:51:00 -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 2D0F4C06138C; Sun, 25 Apr 2021 19:50:18 -0700 (PDT) Received: by mail-qk1-x733.google.com with SMTP id y136so23156057qkb.1; Sun, 25 Apr 2021 19:50:18 -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=VAkAGTv3tSKkDyojScl6Kn+k3PxHxCfUkX5R08qmgI0=; b=Ou3ORZUI1zZi5B4D+Zm1eX0z9lombyn5UDnb+hyRQnxjMtbklLKUQyTPNipIWv4+xn fg/N+KJuGmB7wR+/u6dy25m/ERe0GTHHsBWFECrif1vmaqueegnxUVVSxOUSYMxjkzzk 2MQbKSlXCy5pF7T/aAbVSnO0cqneUqW8KWTUmQsLcAlQYTsSXR443Y0zFwO/zRhSu6mG K7KatwP/J3Pc6Q0bXyRpIn9eKCvSjCFVJrCvDqrU/zHiSQTg8VzHFBB1sfK1wF5NZF/f jVjW0mzYQrf5OtLvbWdBRDpPQkaNmuAq30lE2eY/2j0BSJCAb92xp5HYTjbwUAlOA7Of 1JXg== 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=VAkAGTv3tSKkDyojScl6Kn+k3PxHxCfUkX5R08qmgI0=; b=oOvu0DmQVZuxPxyl49Tixa9NAEw/c3DOwvVeKgm7sLNPaBWaQoR/+PwCMfKHv5FglM g4JjeLq/cBCIBhNUvfO70zliY/cGYn0PjRaGexQgztgVoQO62pZbOSJZwLCnZ5s10c3z gOfH5UD19EP8XZaOz10qAA/cs1YGVrnnk5jJZzsgNZt8d8k3mD9Ogq/uZGjNKQFpMPie uiyv90HZnEstGLKAmKgKx2//GUesOtc9IAqAt6PF40dOgfjb4So9hQYteIY8c77YAVy8 HwjeMJ/ay4PZiHbQQHsjbH6iIesZoOB4cFRYSWPxTkZsIuy9IwQOpVg0VNq5IZHIUe4n yg1Q== X-Gm-Message-State: AOAM533ljJTx3Af3qW5QwIfBpyDUF2trpa07jzhPJ015IaAPiseNhPM/ ClQzsqJrlb4HyQYN1pDFGzck/4gfSqWoDQ== X-Google-Smtp-Source: ABdhPJza3esOR9GPSlrL0GmbTSkkJyh2aBdlV280iuWeseqfMNm94K6qtaY+BGJODctnhmCkB2+NFA== X-Received: by 2002:a37:4242:: with SMTP id p63mr5398094qka.107.1619405417285; Sun, 25 Apr 2021 19:50:17 -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.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:16 -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 04/10] af_unix: set TCP_ESTABLISHED for datagram sockets too Date: Sun, 25 Apr 2021 19:49:55 -0700 Message-Id: <20210426025001.7899-5-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 Currently only unix stream socket sets TCP_ESTABLISHED, datagram socket can set this too when they connect to its peer socket. At least __ip4_datagram_connect() does the same. This will be used by the next patch to determine whether an AF_UNIX datagram socket can be redirected in sockmap. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/unix/af_unix.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 8968ed44a89f..c4afc5fbe137 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -1206,6 +1206,8 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, unix_peer(sk) = other; unix_state_double_unlock(sk, other); } + + sk->sk_state = other->sk_state = TCP_ESTABLISHED; return 0; out_unlock: @@ -1438,12 +1440,10 @@ static int unix_socketpair(struct socket *socka, struct socket *sockb) init_peercred(ska); init_peercred(skb); - if (ska->sk_type != SOCK_DGRAM) { - ska->sk_state = TCP_ESTABLISHED; - skb->sk_state = TCP_ESTABLISHED; - socka->state = SS_CONNECTED; - sockb->state = SS_CONNECTED; - } + ska->sk_state = TCP_ESTABLISHED; + skb->sk_state = TCP_ESTABLISHED; + socka->state = SS_CONNECTED; + sockb->state = SS_CONNECTED; return 0; } From patchwork Mon Apr 26 02:49:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 428433 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=-15.7 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,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 17C5CC43462 for ; Mon, 26 Apr 2021 02:50:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EFB33611C0 for ; Mon, 26 Apr 2021 02:50:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231829AbhDZCvG (ORCPT ); Sun, 25 Apr 2021 22:51:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231502AbhDZCvD (ORCPT ); Sun, 25 Apr 2021 22:51:03 -0400 Received: from mail-qt1-x82d.google.com (mail-qt1-x82d.google.com [IPv6:2607:f8b0:4864:20::82d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A71FC061574; Sun, 25 Apr 2021 19:50:19 -0700 (PDT) Received: by mail-qt1-x82d.google.com with SMTP id c6so40676308qtc.1; Sun, 25 Apr 2021 19:50:19 -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=bfekuNjnyKYKhGsJqGrH3eiIes2smtIR3feKc4k2GqE=; b=spVkHnLcD8VKS1J5NQIVnx3Sw+13u5iDEfeSCk+6yTWxSlhqUiIJtPB2T9J1xYVywY HCBTsBJJ6pL3i+MkIKGm9wgBiV8H27JNyauawAVI9guegQpPs2ovXfdWhaqSRbOpXqje evNhNVkzyvcZvlhUU/XoNSPjesvpxkeeDxYf6OoeIspUcZ6gDcoPSlVmsFFOtLdc/Wp/ sUVLPFf/3gf0a0mh5E+ssty/B/w+1xAYdASiUKmPjF/T/3Bqdiyclg9NHmz5aSrchg4M TPqxfM9ZWYZKm9XNz6uC/0+EXrCrM9gLtRQf0KiY2L4uo+JgabO+fRiI3j+Ft20ITkbb chKQ== 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=bfekuNjnyKYKhGsJqGrH3eiIes2smtIR3feKc4k2GqE=; b=pQ7m2KVBhDBfikfZU3/yXU3MEKP90uW6SJrVOZs7QFYYCDKCg9nsGwpDTJkHu9D6S2 J9RlxjhjJNTISikz6IPYUhPTIrhhyss21wFbzexnM8KnqNrvt9U4vuDzrpFwQqb4vTBU njXKPU81i+sT1me6PK+2tq5JTYM5nrFH5vXtmr2gb6WMwQfFjcYQBSnKlogLOOAKwEPC ud/e0+I+ly0rb2ANUau5sl3/CgeKdhCa8tzUqVk4kV+gT58RicJCsYTQD+OZ9pYd0Vr/ 3aI+upG0NtxaAobcs1GLsDwEKP0M7q/8TyghYFxAdZh7FH0tMy+sT4+FpJTFU2lnlCzh fmzw== X-Gm-Message-State: AOAM532jU/GYyNymwukEcSI8i4f5ri0zJgXcZfEMlN55swUm074OzhVG 0+HyzpfyFC/8nPbrx1QBVOzyLRbAaR2Vyw== X-Google-Smtp-Source: ABdhPJykK68o8hf2Yig1mFmnYf723vV6uVkUXyykZGkPuJfh2J30dGHndqhq386xeRxCtiP8fFW3cg== X-Received: by 2002:ac8:7d4b:: with SMTP id h11mr14787976qtb.61.1619405418730; Sun, 25 Apr 2021 19:50:18 -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.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:18 -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 05/10] af_unix: implement unix_dgram_bpf_recvmsg() Date: Sun, 25 Apr 2021 19:49:56 -0700 Message-Id: <20210426025001.7899-6-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 We have to implement unix_dgram_bpf_recvmsg() to replace the original ->recvmsg() to retrieve skmsg from ingress_msg. AF_UNIX is again special here because the lack of sk_prot->recvmsg(). I simply add a special case inside unix_dgram_recvmsg() to call sk->sk_prot->recvmsg() directly. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- include/net/af_unix.h | 3 +++ net/unix/af_unix.c | 21 ++++++++++++++++--- net/unix/unix_bpf.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/include/net/af_unix.h b/include/net/af_unix.h index cca645846af1..e524c82794c9 100644 --- a/include/net/af_unix.h +++ b/include/net/af_unix.h @@ -82,6 +82,9 @@ static inline struct unix_sock *unix_sk(const struct sock *sk) long unix_inq_len(struct sock *sk); long unix_outq_len(struct sock *sk); +int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, + int nonblock, int flags, int *addr_len); + #ifdef CONFIG_SYSCTL int unix_sysctl_register(struct net *net); void unix_sysctl_unregister(struct net *net); diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index c4afc5fbe137..08458fa9f48b 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2088,11 +2088,11 @@ static void unix_copy_addr(struct msghdr *msg, struct sock *sk) } } -static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, - size_t size, int flags) +int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, + int nonblock, int flags, int *addr_len) { struct scm_cookie scm; - struct sock *sk = sock->sk; + struct socket *sock = sk->sk_socket; struct unix_sock *u = unix_sk(sk); struct sk_buff *skb, *last; long timeo; @@ -2195,6 +2195,21 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, return err; } +static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, + int flags) +{ + struct sock *sk = sock->sk; + int addr_len = 0; + +#ifdef CONFIG_BPF_SYSCALL + if (sk->sk_prot != &unix_proto) + return sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT, + flags & ~MSG_DONTWAIT, &addr_len); +#endif + return __unix_dgram_recvmsg(sk, msg, size, flags & MSG_DONTWAIT, + flags, &addr_len); +} + static int unix_read_sock(struct sock *sk, read_descriptor_t *desc, sk_read_actor_t recv_actor) { diff --git a/net/unix/unix_bpf.c b/net/unix/unix_bpf.c index b1582a659427..b2c34aeb848f 100644 --- a/net/unix/unix_bpf.c +++ b/net/unix/unix_bpf.c @@ -6,6 +6,54 @@ #include #include +static int unix_dgram_bpf_recvmsg(struct sock *sk, struct msghdr *msg, + size_t len, int nonblock, int flags, + int *addr_len) +{ + struct sk_psock *psock; + int copied, ret; + + psock = sk_psock_get(sk); + if (unlikely(!psock)) + return __unix_dgram_recvmsg(sk, msg, len, nonblock, flags, + addr_len); + + lock_sock(sk); + if (!skb_queue_empty(&sk->sk_receive_queue) && + sk_psock_queue_empty(psock)) { + ret = __unix_dgram_recvmsg(sk, msg, len, nonblock, flags, + addr_len); + goto out; + } + +msg_bytes_ready: + copied = sk_msg_recvmsg(sk, psock, msg, len, flags); + if (!copied) { + int data, err = 0; + long timeo; + + timeo = sock_rcvtimeo(sk, nonblock); + data = sk_msg_wait_data(sk, psock, flags, timeo, &err); + if (data) { + if (!sk_psock_queue_empty(psock)) + goto msg_bytes_ready; + ret = __unix_dgram_recvmsg(sk, msg, len, nonblock, + flags, addr_len); + goto out; + } + if (err) { + ret = err; + goto out; + } + copied = -EAGAIN; + } + ret = copied; +out: + release_sock(sk); + sk_psock_put(sk, psock); + return ret; +} + static struct proto *unix_prot_saved __read_mostly; static DEFINE_SPINLOCK(unix_prot_lock); static struct proto unix_bpf_prot; @@ -14,6 +62,7 @@ static void unix_bpf_rebuild_protos(struct proto *prot, const struct proto *base { *prot = *base; prot->close = sock_map_close; + prot->recvmsg = unix_dgram_bpf_recvmsg; } static void unix_bpf_check_needs_rebuild(struct proto *ops) From patchwork Mon Apr 26 02:49:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 427758 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=-15.7 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,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 48737C433ED for ; Mon, 26 Apr 2021 02:50:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 22BFD61263 for ; Mon, 26 Apr 2021 02:50:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231817AbhDZCvE (ORCPT ); Sun, 25 Apr 2021 22:51:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231800AbhDZCvC (ORCPT ); Sun, 25 Apr 2021 22:51:02 -0400 Received: from mail-qt1-x82c.google.com (mail-qt1-x82c.google.com [IPv6:2607:f8b0:4864:20::82c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08E12C061760; Sun, 25 Apr 2021 19:50:21 -0700 (PDT) Received: by mail-qt1-x82c.google.com with SMTP id o21so12528736qtp.7; Sun, 25 Apr 2021 19:50:20 -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=KQf9mcOLrtUNLG51UOBy0gl3Ky2lVF6jZ0E+cVTDSzA=; b=OAkYA5NtvIu2ARz5LFEj9W72Jc24tarzGN8pR8W2skRtWMTE6aQQ5iDiLlKEcse5T/ vlEH672TTLYduv/ipplDx3sovMHER4ply6+1RBh3ChSGTM1fN0ngpOtnyOtiQl79xTgd +ah89Op7G7DVi4yY7DCP1zk8SqoDiI6JuayceeiIjZn7kcTC33RxEFfFqj24Mfe3xHtv wXn4rVhjxGmT8RwpvC03HRhaY2wfUWilQIiRP+6gVYerkHmbqpi44qniDqo715gKKIfB 4s+JzXkYPLTeLK5apfKHiFaE23sle7/+jLT4fYxz0IPrNcT2Z0J8qVcWJiBM+y6KairL LVKQ== 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=KQf9mcOLrtUNLG51UOBy0gl3Ky2lVF6jZ0E+cVTDSzA=; b=KBvjaY8QKrSM8eHOKD7RLLwk+ig8yAZbyVejNoS79lVgAnYE9IZeRw84eM0tYSNPM7 l0OMaWd3hvnqr8Y1U/vfTAPA22WRNDK5rGKIYWP2XVIY2b3r8SwtDeY4/bFd8ylFPqyA hnbkJLlMv6hgS5wR5NCJZz9dwcGDG+zWFx+1ArU+96N2060UKYVoZi0Vz2HsXdDyckmO pc/2Hmrw1fajv+DGmRjXVJd4L1FSKJLtfGC3wP7PHPzdsTlNi7n3zX6Al0UPafd2sWV5 9I7eQSOY9ptArLKRwPak6NGWZ7yw1lL3LBASLSbBs/M1u6fLgxwxHbxOJ6qrIvgEdXxR fWxw== X-Gm-Message-State: AOAM532FYgI+6fv9UuesJN0komZZ69+8WNuHfrk/79vblymxI8MOT1wA KVxQfXsG/FfdK1+GSBv2/PSAgTPlojdHdQ== X-Google-Smtp-Source: ABdhPJyYyTKU5amydWOnuzv7uJu5d4Njqqsyobg3YA0+1YeSPC7qBTCBCCYvqzMN6ngMH7w1LCixRg== X-Received: by 2002:a05:622a:94:: with SMTP id o20mr15096508qtw.158.1619405420163; Sun, 25 Apr 2021 19:50:20 -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.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:19 -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 06/10] sock_map: update sock type checks for AF_UNIX Date: Sun, 25 Apr 2021 19:49:57 -0700 Message-Id: <20210426025001.7899-7-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 Now AF_UNIX datagram supports sockmap and redirection, we can update the sock type checks for them accordingly. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/core/sock_map.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/net/core/sock_map.c b/net/core/sock_map.c index 1107c9dcc969..2acdd848a895 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -533,6 +533,12 @@ static bool sk_is_udp(const struct sock *sk) sk->sk_protocol == IPPROTO_UDP; } +static bool sk_is_unix(const struct sock *sk) +{ + return sk->sk_type == SOCK_DGRAM && + sk->sk_family == AF_UNIX; +} + static bool sock_map_redirect_allowed(const struct sock *sk) { if (sk_is_tcp(sk)) @@ -552,6 +558,8 @@ static bool sock_map_sk_state_allowed(const struct sock *sk) return (1 << sk->sk_state) & (TCPF_ESTABLISHED | TCPF_LISTEN); else if (sk_is_udp(sk)) return sk_hashed(sk); + else if (sk_is_unix(sk)) + return sk->sk_state == TCP_ESTABLISHED; return false; } From patchwork Mon Apr 26 02:49:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 427757 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=-15.7 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,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 B597DC433B4 for ; Mon, 26 Apr 2021 02:50:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 95D476128A for ; Mon, 26 Apr 2021 02:50:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231840AbhDZCvH (ORCPT ); Sun, 25 Apr 2021 22:51:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231804AbhDZCvE (ORCPT ); Sun, 25 Apr 2021 22:51:04 -0400 Received: from mail-qk1-x72d.google.com (mail-qk1-x72d.google.com [IPv6:2607:f8b0:4864:20::72d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BB97C061761; Sun, 25 Apr 2021 19:50:22 -0700 (PDT) Received: by mail-qk1-x72d.google.com with SMTP id z2so18267993qkb.9; Sun, 25 Apr 2021 19:50:22 -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=ZLt69FAUdQ5bTjcW1qMZRDUzZdTqyuja2OTZzrCIJKA=; b=odGv/gLpOobU4dSVOTLEg7AXxfl9qLEsDhNXLyb/oSDqJtjUp7v71PPXP7eVSAkbKW oGlzdOIfEeZ3CMeISognNd2UfWDqQiaRlOnXpLqP3h2nh38nf8IwbMVfY5dgGVcOrEX5 ZDTb6mH+v6dNJUNJlX2WwVhDZMKpf2FH/QKtj3xMu5sriuMZ70RFhNW0VkKfAqATQ1g6 RqnaLVL+MeceSKMs3D3ZnxOaOxOXmrzgMdOVvtyCwCH6J0TRWmE1gKY4yfbhWx8ubcbj Lxy+04A7qqFAiB0ZHY34rWKUG1x/ikaMczM6xSrMA1Y7MsVCnvVW0qxJ9ePe9Gv/yqO0 L9Tw== 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=ZLt69FAUdQ5bTjcW1qMZRDUzZdTqyuja2OTZzrCIJKA=; b=uWUEGV0tV5S/IJO0FOTwr+j96U9bSvKyuq9EIPOnIndXH5ZQu3As475YcK1tOCtN93 7ybQqH+cXPQhSlaBI3yyA+DmpI8EKSN+aek6LKYdr8/N/53w4Q5RuU7KBBbPFlLzH6rQ jLIZCr/e4pViJkDiS6QpgFaxgerDBg2mhHSIr/WAkeqfdWELkLwpp4/vBHd3Mb1xqcfx hX/y7ci7znL30qtyO55tiTPcMN1zWb61S4YGjCbECzzx47q0f7bwlGI9c5ceeMhh+KHM UokkUn3ulBK7XrwQCbjfi5OOsoigjLiCUjieeQnD7kIP2OT8FEe639yKpMw8fYm43TJa FQ5Q== X-Gm-Message-State: AOAM531o6EYL3va8EknwYuJ9p58MTzmak4Pqg9H+zU8hGmwO0ouUnjOA 1UjiKUjQDqxq0hBcyqsTa1WqG+1TzAMPOA== X-Google-Smtp-Source: ABdhPJw+bDo1qvYnYuELepnzRxRFsKuJwvy1YWH+BJseyLcMmLKVAqmAUfOl/4OtOgAAXDWNN37lMA== X-Received: by 2002:a05:620a:6ce:: with SMTP id 14mr15534201qky.423.1619405421595; Sun, 25 Apr 2021 19:50:21 -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.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:21 -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 07/10] selftests/bpf: factor out udp_socketpair() Date: Sun, 25 Apr 2021 19:49:58 -0700 Message-Id: <20210426025001.7899-8-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 Factor out a common helper udp_socketpair() which creates a pair of connected UDP sockets. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- .../selftests/bpf/prog_tests/sockmap_listen.c | 76 ++++++++++--------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index 648d9ae898d2..3d9907bcf132 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -1603,32 +1603,27 @@ static void test_reuseport(struct test_sockmap_listen *skel, } } -static void udp_redir_to_connected(int family, int sotype, int sock_mapfd, - int verd_mapfd, enum redir_mode mode) +static int udp_socketpair(int family, int *s, int *c) { - const char *log_prefix = redir_mode_str(mode); struct sockaddr_storage addr; - int c0, c1, p0, p1; - unsigned int pass; socklen_t len; - int err, n; - u64 value; - u32 key; - char b; - - zero_verdict_count(verd_mapfd); + int p0, c0; + int err; - p0 = socket_loopback(family, sotype | SOCK_NONBLOCK); + p0 = socket_loopback(family, SOCK_DGRAM | SOCK_NONBLOCK); if (p0 < 0) - return; + return p0; + len = sizeof(addr); err = xgetsockname(p0, sockaddr(&addr), &len); if (err) goto close_peer0; - c0 = xsocket(family, sotype | SOCK_NONBLOCK, 0); - if (c0 < 0) + c0 = xsocket(family, SOCK_DGRAM | SOCK_NONBLOCK, 0); + if (c0 < 0) { + err = c0; goto close_peer0; + } err = xconnect(c0, sockaddr(&addr), len); if (err) goto close_cli0; @@ -1639,25 +1634,36 @@ static void udp_redir_to_connected(int family, int sotype, int sock_mapfd, if (err) goto close_cli0; - p1 = socket_loopback(family, sotype | SOCK_NONBLOCK); - if (p1 < 0) - goto close_cli0; - err = xgetsockname(p1, sockaddr(&addr), &len); - if (err) - goto close_cli0; + *s = p0; + *c = c0; + return 0; - c1 = xsocket(family, sotype | SOCK_NONBLOCK, 0); - if (c1 < 0) - goto close_peer1; - err = xconnect(c1, sockaddr(&addr), len); - if (err) - goto close_cli1; - err = xgetsockname(c1, sockaddr(&addr), &len); +close_cli0: + xclose(c0); +close_peer0: + xclose(p0); + return err; +} + +static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, + enum redir_mode mode) +{ + const char *log_prefix = redir_mode_str(mode); + int c0, c1, p0, p1; + unsigned int pass; + int err, n; + u64 value; + u32 key; + char b; + + zero_verdict_count(verd_mapfd); + + err = udp_socketpair(family, &p0, &c0); if (err) - goto close_cli1; - err = xconnect(p1, sockaddr(&addr), len); + return; + err = udp_socketpair(family, &p1, &c1); if (err) - goto close_cli1; + goto close_cli0; key = 0; value = p0; @@ -1694,11 +1700,9 @@ static void udp_redir_to_connected(int family, int sotype, int sock_mapfd, close_cli1: xclose(c1); -close_peer1: xclose(p1); close_cli0: xclose(c0); -close_peer0: xclose(p0); } @@ -1715,11 +1719,9 @@ static void udp_skb_redir_to_connected(struct test_sockmap_listen *skel, return; skel->bss->test_ingress = false; - udp_redir_to_connected(family, SOCK_DGRAM, sock_map, verdict_map, - REDIR_EGRESS); + udp_redir_to_connected(family, sock_map, verdict_map, REDIR_EGRESS); skel->bss->test_ingress = true; - udp_redir_to_connected(family, SOCK_DGRAM, sock_map, verdict_map, - REDIR_INGRESS); + udp_redir_to_connected(family, sock_map, verdict_map, REDIR_INGRESS); xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT); } From patchwork Mon Apr 26 02:49:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 428432 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=-15.7 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,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 C31B0C433ED for ; Mon, 26 Apr 2021 02:50:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A396E61263 for ; Mon, 26 Apr 2021 02:50:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231851AbhDZCvI (ORCPT ); Sun, 25 Apr 2021 22:51:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33628 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231826AbhDZCvF (ORCPT ); Sun, 25 Apr 2021 22:51:05 -0400 Received: from mail-qt1-x830.google.com (mail-qt1-x830.google.com [IPv6:2607:f8b0:4864:20::830]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE5EDC061574; Sun, 25 Apr 2021 19:50:23 -0700 (PDT) Received: by mail-qt1-x830.google.com with SMTP id q4so12280331qtn.5; Sun, 25 Apr 2021 19:50:23 -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=3zBurI5w9nWmqgTlS8ZlPUjsf6vWN5Rp/hn9WFSBN8k=; b=R9pfFkkhT02yf40b4IpUf0VAeZoFzKR0FBQKG2ZBLtwRrz5YrePJa9gian7FsxLule s5uRqzMYid1ET7OTt9vKTyOTC3HzV03FO1jt47y6oLFfKoktUiDbUdleFRfFC6gK0khH 9YYScFciqysvCLpkS7VgfCLIsj+eQbJmNWqrOJlZ1frR9IAqeV/BJxww/Ok9xbPdb3Ex tqUefrQ0vGRSnqd2vARFJYNg2cMuCIC53b8WJ/iNxdjyvh55bBUHNKR1vrDbO/oLNMHC Gv/jO9wy49FEpgLJPklH6GH2qP8jDQXkS/frKk4TS6IEuQCC+cHhjDZvt30pVknwQq20 x5hg== 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=3zBurI5w9nWmqgTlS8ZlPUjsf6vWN5Rp/hn9WFSBN8k=; b=dUhuuj3/yyyLaJ6BCCBGYvuFnKQdQ7uv/tWUJyO2bwtDe20cgRA4FOARrYJzrFHPFY Gj9qTQUHyZEsBiXL09H/kDtVOPJTdWH+hRxdQX+xfcrfTtNsouE5pfbLaRH2s5RnJ1Vn XKanhLI9cNzunNFCAABzwmOSLjxOZC2K7nYrjO+r7nREkUvEJr+BqpwmWZdElCQqypHN 35NkyyPOPxch3/t5NLqaKKfB+2Dib6sSC7+vc0dLnCYLXWVGmurdEj55PU+EXL8ZB4rk YvzY/SCmx5IMDsiXnyi29WI08nBF4gK3AWbmcnRxyCAcO2JpeNwBssPT6EpeqUsdmbnL 31qw== X-Gm-Message-State: AOAM533mrwsbQKilGSvfObmNkHtpn2OC9tlPFan6Ml3BBIY90Ssc3R+7 kin6/65YOZrcdDNOxMjjf/CIT2/Kd9zRfw== X-Google-Smtp-Source: ABdhPJzxOZ9HRb/PJcPbTSOahT7uATxGjdaDavz82n6K3YDiYvTdB/7FKuza+WCO4Tx32T0ejD4N8g== X-Received: by 2002:ac8:5a16:: with SMTP id n22mr2545833qta.103.1619405423007; Sun, 25 Apr 2021 19:50:23 -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.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:22 -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 08/10] selftests/bpf: factor out add_to_sockmap() Date: Sun, 25 Apr 2021 19:49:59 -0700 Message-Id: <20210426025001.7899-9-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 Factor out a common helper add_to_sockmap() which adds two sockets into a sockmap. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- .../selftests/bpf/prog_tests/sockmap_listen.c | 59 +++++++------------ 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index 3d9907bcf132..ee017278fae4 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -919,6 +919,23 @@ static const char *redir_mode_str(enum redir_mode mode) } } +static int add_to_sockmap(int sock_mapfd, int fd1, int fd2) +{ + u64 value; + u32 key; + int err; + + key = 0; + value = fd1; + err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); + if (err) + return err; + + key = 1; + value = fd2; + return xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); +} + static void redir_to_connected(int family, int sotype, int sock_mapfd, int verd_mapfd, enum redir_mode mode) { @@ -928,7 +945,6 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd, unsigned int pass; socklen_t len; int err, n; - u64 value; u32 key; char b; @@ -965,15 +981,7 @@ static void redir_to_connected(int family, int sotype, int sock_mapfd, if (p1 < 0) goto close_cli1; - key = 0; - value = p0; - err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); - if (err) - goto close_peer1; - - key = 1; - value = p1; - err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); + err = add_to_sockmap(sock_mapfd, p0, p1); if (err) goto close_peer1; @@ -1061,7 +1069,6 @@ static void redir_to_listening(int family, int sotype, int sock_mapfd, int s, c, p, err, n; unsigned int drop; socklen_t len; - u64 value; u32 key; zero_verdict_count(verd_mapfd); @@ -1086,15 +1093,7 @@ static void redir_to_listening(int family, int sotype, int sock_mapfd, if (p < 0) goto close_cli; - key = 0; - value = s; - err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); - if (err) - goto close_peer; - - key = 1; - value = p; - err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); + err = add_to_sockmap(sock_mapfd, s, p); if (err) goto close_peer; @@ -1346,7 +1345,6 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map, int s1, s2, c, err; unsigned int drop; socklen_t len; - u64 value; u32 key; zero_verdict_count(verd_map); @@ -1360,16 +1358,10 @@ static void test_reuseport_mixed_groups(int family, int sotype, int sock_map, if (s2 < 0) goto close_srv1; - key = 0; - value = s1; - err = xbpf_map_update_elem(sock_map, &key, &value, BPF_NOEXIST); + err = add_to_sockmap(sock_map, s1, s2); if (err) goto close_srv2; - key = 1; - value = s2; - err = xbpf_map_update_elem(sock_map, &key, &value, BPF_NOEXIST); - /* Connect to s2, reuseport BPF selects s1 via sock_map[0] */ len = sizeof(addr); err = xgetsockname(s2, sockaddr(&addr), &len); @@ -1652,7 +1644,6 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, int c0, c1, p0, p1; unsigned int pass; int err, n; - u64 value; u32 key; char b; @@ -1665,15 +1656,7 @@ static void udp_redir_to_connected(int family, int sock_mapfd, int verd_mapfd, if (err) goto close_cli0; - key = 0; - value = p0; - err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); - if (err) - goto close_cli1; - - key = 1; - value = p1; - err = xbpf_map_update_elem(sock_mapfd, &key, &value, BPF_NOEXIST); + err = add_to_sockmap(sock_mapfd, p0, p1); if (err) goto close_cli1; From patchwork Mon Apr 26 02:50:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 428431 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=-15.7 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,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 F01FCC433ED for ; Mon, 26 Apr 2021 02:50:31 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CE9EF61263 for ; Mon, 26 Apr 2021 02:50:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231868AbhDZCvL (ORCPT ); Sun, 25 Apr 2021 22:51:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231831AbhDZCvG (ORCPT ); Sun, 25 Apr 2021 22:51:06 -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 7374CC061760; Sun, 25 Apr 2021 19:50:25 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id q136so34285382qka.7; Sun, 25 Apr 2021 19:50:25 -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=UjK9BI5v2k7+AWEzGv15pygYECWaieRAsq8bft/a118=; b=AVHhP3fAlCLw/c+++DMSLdzfqIBy1SltDKqp3PAUTK+54gbVcXajpcSCMXAWfUpgg2 fd1s6/2dgu3ZMQjPxM3etBPe6asoqrri6aHAOuymvCNu8gu27LvQImjSiN8xK0mHL/Rp rWaT4X7dnYeT4UBcJJLeoIawD32p9+aiXnYFrcDr4h788rZF+RJNUko5D3zAABBEzPum 2xWATBZ9eippyt/ozmwLFbEgsfiVCt3otq63PF4Gkzq73OVxJzWbYfMdTVBpYZH3EA1B jijKBCWrIR75tR843nAw/B1u1faVQtX5pTu8wRs/i23FWbF1D4uD+kOyagJnKEuvaIZe 7ivw== 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=UjK9BI5v2k7+AWEzGv15pygYECWaieRAsq8bft/a118=; b=ogZ/A0h9LodxhY2c2X5HEqH+J9FCNjdKSjZKW0k5kTX6JrLjbX09+pA7aa2YfvxavO jT/WcOPq8ivkCjC9hBWCE0TXiZTMVRQDsb9ZpjmGOZkOg9Sn8NBlXw5uFnbYpzF7tgU2 2uCslwPLnO/IQf4dphuQL58RFlYDRn1PzqdAyFODnzxhJJkEuA9AF6+QR1X8JpFWsA5x 8SfwgOW2Fy3tnPJTvdPKt1YVHzVEVVM9vy4S0DhfasGs+IkYTmwlIgRmvnjNM/ApVfVU aatHSKBCcZbtN55qR+YVIo8TBL6nFRRnb2sQxV6SUDqAQx7Wz4f/8bXVLAsnkuxw34zc Zb/w== X-Gm-Message-State: AOAM532e45GEhHf1wLDpEWVsWng6k7wdsHPyleUQ+sewM3zXKdGC5mMO /Po45HL48fHbQ/Ap64H7r8vQ6QInOdhGlg== X-Google-Smtp-Source: ABdhPJzYa768AX/K1iy4vAbTfGJeG0N7YRoP/nCaxvTb3klHLAnjHRjiMnpbqKvQrishLwKDqsch2g== X-Received: by 2002:ae9:eb56:: with SMTP id b83mr3950823qkg.350.1619405424427; Sun, 25 Apr 2021 19:50:24 -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.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:24 -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 09/10] selftests/bpf: add a test case for unix sockmap Date: Sun, 25 Apr 2021 19:50:00 -0700 Message-Id: <20210426025001.7899-10-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 Add a test case to ensure redirection between two AF_UNIX datagram sockets work. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- .../selftests/bpf/prog_tests/sockmap_listen.c | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index ee017278fae4..2b1bdb8fa48d 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -1433,6 +1433,8 @@ static const char *family_str(sa_family_t family) return "IPv4"; case AF_INET6: return "IPv6"; + case AF_UNIX: + return "Unix"; default: return "unknown"; } @@ -1555,6 +1557,94 @@ static void test_redir(struct test_sockmap_listen *skel, struct bpf_map *map, } } +static void unix_redir_to_connected(int sotype, int sock_mapfd, + int verd_mapfd, enum redir_mode mode) +{ + const char *log_prefix = redir_mode_str(mode); + int c0, c1, p0, p1; + unsigned int pass; + int err, n; + int sfd[2]; + u32 key; + char b; + + zero_verdict_count(verd_mapfd); + + if (socketpair(AF_UNIX, sotype | SOCK_NONBLOCK, 0, sfd)) + return; + c0 = sfd[0], p0 = sfd[1]; + + if (socketpair(AF_UNIX, sotype | SOCK_NONBLOCK, 0, sfd)) + goto close0; + c1 = sfd[0], p1 = sfd[1]; + + err = add_to_sockmap(sock_mapfd, p0, p1); + if (err) + goto close; + + n = write(c1, "a", 1); + if (n < 0) + FAIL_ERRNO("%s: write", log_prefix); + if (n == 0) + FAIL("%s: incomplete write", log_prefix); + if (n < 1) + goto close; + + key = SK_PASS; + err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass); + if (err) + goto close; + if (pass != 1) + FAIL("%s: want pass count 1, have %d", log_prefix, pass); + + n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); + if (n < 0) + FAIL_ERRNO("%s: read", log_prefix); + if (n == 0) + FAIL("%s: incomplete read", log_prefix); + +close: + xclose(c1); + xclose(p1); +close0: + xclose(c0); + xclose(p0); +} + +static void unix_skb_redir_to_connected(struct test_sockmap_listen *skel, + struct bpf_map *inner_map, int sotype) +{ + int verdict = bpf_program__fd(skel->progs.prog_skb_verdict); + int verdict_map = bpf_map__fd(skel->maps.verdict_map); + int sock_map = bpf_map__fd(inner_map); + int err; + + err = xbpf_prog_attach(verdict, sock_map, BPF_SK_SKB_VERDICT, 0); + if (err) + return; + + skel->bss->test_ingress = false; + unix_redir_to_connected(sotype, sock_map, verdict_map, REDIR_EGRESS); + skel->bss->test_ingress = true; + unix_redir_to_connected(sotype, sock_map, verdict_map, REDIR_INGRESS); + + xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT); +} + +static void test_unix_redir(struct test_sockmap_listen *skel, struct bpf_map *map, + int sotype) +{ + const char *family_name, *map_name; + char s[MAX_TEST_NAME]; + + family_name = family_str(AF_UNIX); + map_name = map_type_str(map); + snprintf(s, sizeof(s), "%s %s %s", map_name, family_name, __func__); + if (!test__start_subtest(s)) + return; + unix_skb_redir_to_connected(skel, map, sotype); +} + static void test_reuseport(struct test_sockmap_listen *skel, struct bpf_map *map, int family, int sotype) { @@ -1747,10 +1837,12 @@ void test_sockmap_listen(void) skel->bss->test_sockmap = true; run_tests(skel, skel->maps.sock_map, AF_INET); run_tests(skel, skel->maps.sock_map, AF_INET6); + test_unix_redir(skel, skel->maps.sock_map, SOCK_DGRAM); skel->bss->test_sockmap = false; run_tests(skel, skel->maps.sock_hash, AF_INET); run_tests(skel, skel->maps.sock_hash, AF_INET6); + test_unix_redir(skel, skel->maps.sock_hash, SOCK_DGRAM); test_sockmap_listen__destroy(skel); } From patchwork Mon Apr 26 02:50:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 427756 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=-15.7 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,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 39298C433B4 for ; Mon, 26 Apr 2021 02:50:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F70261263 for ; Mon, 26 Apr 2021 02:50:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231859AbhDZCvJ (ORCPT ); Sun, 25 Apr 2021 22:51:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231804AbhDZCvH (ORCPT ); Sun, 25 Apr 2021 22:51:07 -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 CFA39C061761; Sun, 25 Apr 2021 19:50:26 -0700 (PDT) Received: by mail-qk1-x72c.google.com with SMTP id 8so21507548qkv.8; Sun, 25 Apr 2021 19:50:26 -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=GwgRYiRyslo628iscBvsP7av/19EwrhlvyUG9PJn1s0=; b=J2LzrXgMlx4zBH1RzvBV6CejWskknV9ns40Pnxg9ir8UU2LYoLubsgKT2e2ZqFtrhJ eq01rxaiEtHtNPR4kF2ok8rrdKE9pN7e/A+5JTs7sJXBpoSxZW1I5FamSXMEkqbO0om2 J7pj3kfB2a0H9gaBzuRUoCSsZ02SZ431jRWlanDmLjPypkrALXzJ447B2FuVyqpRinNq cwHUCAYNVZNZbjWl15lFvkSx4TqJj0UmKnZkBJAGbyRE0c7aOVxuy1gtdP7L6eh+uIaI dseAdf968/X1a9RvFV0pt4BuepqM/QiVglHdgbpACyyKiScv1IZcQPxV+0x4B8SX0sm5 Rzhg== 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=GwgRYiRyslo628iscBvsP7av/19EwrhlvyUG9PJn1s0=; b=RHOLu/Qvxf3lHdFaTglSVIRDnvScsAI6U5MzaD4S+YoWUmvLYkDn5Z2FGBLxVF2/vI Hi0rzMWMs0izVqQOv0IOYqDnqFCnTiUkfFo7j6Q+tUthkb6C1leZZ00Ce8/bZmFpHR0v qPofcxjUHgD3cqCy0P6t6dPwjB3+FpAS50oJjIfGMt5uQ7rZoKf5eT9/CWAQQ7eOn6nM CtJHO1xKsaNvvkXVUpJjduxgDFsw2S80BihJKBZz1Gg+ttkW2olU8pdPyfoIKvkh/ltz HOqtJCSwOEoh+NM7mCpgKTXbOPxvzRzs+5d0tDxSi41L/rK/OhAPRdE5DtDic8jJoL0Q G34g== X-Gm-Message-State: AOAM533w4kXHA0xVal77ysNdHSjN5GZ5hqruFv4ZhsrCcfdUM/Yv3lVG seqn59qI0VajjiZNQYXKYH68WfvnQFg9Hw== X-Google-Smtp-Source: ABdhPJzI8gNdFrtz7xLlNRhZe/gkVXOgDrTi36tKoxswbiLMzkHPqRwGACHxz5Q5/eYHndW4TXeKaw== X-Received: by 2002:a05:620a:7d4:: with SMTP id 20mr15126539qkb.58.1619405425930; Sun, 25 Apr 2021 19:50:25 -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.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 25 Apr 2021 19:50:25 -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 10/10] selftests/bpf: add test cases for redirection between udp and unix Date: Sun, 25 Apr 2021 19:50:01 -0700 Message-Id: <20210426025001.7899-11-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 Add two test cases to ensure redirection between udp and unix work bidirectionally. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- .../selftests/bpf/prog_tests/sockmap_listen.c | 165 ++++++++++++++++++ 1 file changed, 165 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c index 2b1bdb8fa48d..01c052e15a83 100644 --- a/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_listen.c @@ -1813,6 +1813,170 @@ static void test_udp_redir(struct test_sockmap_listen *skel, struct bpf_map *map udp_skb_redir_to_connected(skel, map, family); } +static void udp_unix_redir_to_connected(int family, int sock_mapfd, + int verd_mapfd, enum redir_mode mode) +{ + const char *log_prefix = redir_mode_str(mode); + int c0, c1, p0, p1; + unsigned int pass; + int err, n; + int sfd[2]; + u32 key; + char b; + + zero_verdict_count(verd_mapfd); + + if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, sfd)) + return; + c0 = sfd[0], p0 = sfd[1]; + + err = udp_socketpair(family, &p1, &c1); + if (err) + goto close; + + err = add_to_sockmap(sock_mapfd, p0, p1); + if (err) + goto close_cli1; + + n = write(c1, "a", 1); + if (n < 0) + FAIL_ERRNO("%s: write", log_prefix); + if (n == 0) + FAIL("%s: incomplete write", log_prefix); + if (n < 1) + goto close_cli1; + + key = SK_PASS; + err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass); + if (err) + goto close_cli1; + if (pass != 1) + FAIL("%s: want pass count 1, have %d", log_prefix, pass); + + n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); + if (n < 0) + FAIL_ERRNO("%s: read", log_prefix); + if (n == 0) + FAIL("%s: incomplete read", log_prefix); + +close_cli1: + xclose(c1); + xclose(p1); +close: + xclose(c0); + xclose(p0); +} + +static void udp_unix_skb_redir_to_connected(struct test_sockmap_listen *skel, + struct bpf_map *inner_map, int family) +{ + int verdict = bpf_program__fd(skel->progs.prog_skb_verdict); + int verdict_map = bpf_map__fd(skel->maps.verdict_map); + int sock_map = bpf_map__fd(inner_map); + int err; + + err = xbpf_prog_attach(verdict, sock_map, BPF_SK_SKB_VERDICT, 0); + if (err) + return; + + skel->bss->test_ingress = false; + udp_unix_redir_to_connected(family, sock_map, verdict_map, REDIR_EGRESS); + skel->bss->test_ingress = true; + udp_unix_redir_to_connected(family, sock_map, verdict_map, REDIR_INGRESS); + + xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT); +} + +static void unix_udp_redir_to_connected(int family, int sock_mapfd, + int verd_mapfd, enum redir_mode mode) +{ + const char *log_prefix = redir_mode_str(mode); + int c0, c1, p0, p1; + unsigned int pass; + int err, n; + int sfd[2]; + u32 key; + char b; + + zero_verdict_count(verd_mapfd); + + err = udp_socketpair(family, &p0, &c0); + if (err) + return; + + if (socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, sfd)) + goto close_cli0; + c1 = sfd[0], p1 = sfd[1]; + + err = add_to_sockmap(sock_mapfd, p0, p1); + if (err) + goto close; + + n = write(c1, "a", 1); + if (n < 0) + FAIL_ERRNO("%s: write", log_prefix); + if (n == 0) + FAIL("%s: incomplete write", log_prefix); + if (n < 1) + goto close; + + key = SK_PASS; + err = xbpf_map_lookup_elem(verd_mapfd, &key, &pass); + if (err) + goto close; + if (pass != 1) + FAIL("%s: want pass count 1, have %d", log_prefix, pass); + + n = read(mode == REDIR_INGRESS ? p0 : c0, &b, 1); + if (n < 0) + FAIL_ERRNO("%s: read", log_prefix); + if (n == 0) + FAIL("%s: incomplete read", log_prefix); + +close: + xclose(c1); + xclose(p1); +close_cli0: + xclose(c0); + xclose(p0); + +} + +static void unix_udp_skb_redir_to_connected(struct test_sockmap_listen *skel, + struct bpf_map *inner_map, int family) +{ + int verdict = bpf_program__fd(skel->progs.prog_skb_verdict); + int verdict_map = bpf_map__fd(skel->maps.verdict_map); + int sock_map = bpf_map__fd(inner_map); + int err; + + err = xbpf_prog_attach(verdict, sock_map, BPF_SK_SKB_VERDICT, 0); + if (err) + return; + + skel->bss->test_ingress = false; + unix_udp_redir_to_connected(family, sock_map, verdict_map, REDIR_EGRESS); + skel->bss->test_ingress = true; + unix_udp_redir_to_connected(family, sock_map, verdict_map, REDIR_INGRESS); + + xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT); +} + +static void test_udp_unix_redir(struct test_sockmap_listen *skel, struct bpf_map *map, + int family) +{ + const char *family_name, *map_name; + char s[MAX_TEST_NAME]; + + family_name = family_str(family); + map_name = map_type_str(map); + snprintf(s, sizeof(s), "%s %s %s", map_name, family_name, __func__); + if (!test__start_subtest(s)) + return; + udp_unix_skb_redir_to_connected(skel, map, family); + unix_udp_skb_redir_to_connected(skel, map, family); +} + static void run_tests(struct test_sockmap_listen *skel, struct bpf_map *map, int family) { @@ -1822,6 +1986,7 @@ static void run_tests(struct test_sockmap_listen *skel, struct bpf_map *map, test_reuseport(skel, map, family, SOCK_STREAM); test_reuseport(skel, map, family, SOCK_DGRAM); test_udp_redir(skel, map, family); + test_udp_unix_redir(skel, map, family); } void test_sockmap_listen(void)