From patchwork Mon Apr 19 17:55: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: 424327 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, URIBL_BLOCKED, 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 8B47DC43461 for ; Mon, 19 Apr 2021 17:56:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5436461246 for ; Mon, 19 Apr 2021 17:56:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240376AbhDSR5S (ORCPT ); Mon, 19 Apr 2021 13:57:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233851AbhDSR5M (ORCPT ); Mon, 19 Apr 2021 13:57:12 -0400 Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5FAEC061761; Mon, 19 Apr 2021 10:56:40 -0700 (PDT) Received: by mail-pj1-x1033.google.com with SMTP id m6-20020a17090a8586b02901507e1acf0fso3492224pjn.3; Mon, 19 Apr 2021 10:56:40 -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=KoooA+wJKm09yw9keUKD9QS45BXENny998i7q2qmpKg=; b=Oo869W6qJhAtm8VZSiVH1nMLjI/1FlSXATRsf90ErCaqFUySsmS1rpLK8ASr0EzkVc xu4jzbqJxWOc0llPz38OUPbu669Lwb+0T7BwuDNnkOUcFoZ/lE/TIqdWzyMVj+kI1g8Y Q/uWPDyegd7DHk/WfILIDvjxiHn6p7tME0LjxaE3tUz60pojRaFbuN5abIZQ5HctyeOu 0GdZRurIFBuUINxLSq0T2WxJ2yw2XxVJXw29iMuJpa6dIAuYNhYZ31qEkJ0gcGOK8Lo6 ctEHJhqDynchldiecaFO1LzMxZhh67CDTTJfmpQDOEYY+Pd8kiidUiApAU4uoRaebkXB wuKA== 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=KoooA+wJKm09yw9keUKD9QS45BXENny998i7q2qmpKg=; b=DBXf/xqj7WMEqwDabHRXdZH6CiPvsrRPXzFktzUcRUVfBFFe5r3LJLyK9IRk1jIPAv 2LTJymY1WEZIDxHOU+xsc+AyAsXAj/l96S3BsKK1yFGZSDh53lZs0YQZm6QVBGPPDFFJ KkhnqXleJPj8VMf6gzu55vi3pi3+Bcu8SYgYrFFspu0q4PO4ieVZcDI3Jn5KTV1nF+BS X1qBUwlkmj/rHvZtSXyOLNvhpzhJEUICaW/tN/pKKojeactNDq5jXMwn6QZBlCy7qsnM d8WoQLebW4eHL/6MhR1M0iBXC5CVwM+Lv/M689Z6mXgmBeRJSEHVyDUwRO5hJgyxnbbl p4iQ== X-Gm-Message-State: AOAM532iGkuon2DhDzV4gn+F8nqJVZL+C8UEIMF0woata8H5ZqtOXWuX 7vWoqv3PaJnKnxYS+wA+NUYtxnMqsa62uw== X-Google-Smtp-Source: ABdhPJxpgcAprKCSKg+A7ecFr/ita/Sjx0mp8YXtDK7as3VRTW2Q/SPD9xth/yv2fi0ghqLDaudEPw== X-Received: by 2002:a17:90b:188e:: with SMTP id mn14mr310886pjb.148.1618855000415; Mon, 19 Apr 2021 10:56:40 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:40 -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 v2 1/9] af_unix: implement ->read_sock() for sockmap Date: Mon, 19 Apr 2021 10:55:55 -0700 Message-Id: <20210419175603.19378-2-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 5a31307ceb76..966359b64a56 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -661,6 +661,7 @@ 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); +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 +739,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 +2185,41 @@ static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg, return err; } +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 19 17:55: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: 425086 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, URIBL_BLOCKED, 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 D974CC43600 for ; Mon, 19 Apr 2021 17:56:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B1549611F2 for ; Mon, 19 Apr 2021 17:56:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240347AbhDSR5Q (ORCPT ); Mon, 19 Apr 2021 13:57:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240259AbhDSR5L (ORCPT ); Mon, 19 Apr 2021 13:57:11 -0400 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F4BBC06138A; Mon, 19 Apr 2021 10:56:41 -0700 (PDT) Received: by mail-pg1-x536.google.com with SMTP id t22so24829476pgu.0; Mon, 19 Apr 2021 10:56:41 -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=Nl87MwqpfRvd50+82J1Wpx63mRm0ruM72/35snEyNsY=; b=bBabVM2F2YuiLi0ib2vrjCHbKCKQH94mkBDN42j4GI20KRHqbK0v4fzwxQwye7EAPj b0GYA+vyiBuyW5W7GmzjE35UljCRoNW4qKtlTkfPMuXoFR/jKLS43ST3F6SQ/zRmKaWt IY3/9Cf2/HDp3tqRROHhOeiiDlCAK6fxGTL+zPyNZAzCMKjQtZxRRoNSQp7uujTZyw/r pgXPjUqbF3THBf262Ogn/+A34SfZc8LqzsXWfiLBZ/Pm2Dn42H5N+vmGx87oXK9G7Evx u0y669Pt7ZA57ASf6rVQ9FHYw2EsfweZz45/13u81WqxA+P2APgpV4qKxWUvT7sYo5tD A+6g== 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=Nl87MwqpfRvd50+82J1Wpx63mRm0ruM72/35snEyNsY=; b=REgauBEHUQJRk8+sgv51xRitdSeH6cnGQrOX9amgi78ulcAmMh719IFqMbtY6/MSj/ 4ZDb5Q7QVKd7maM+Jsf9v1nF06W+AUqKyOpg5pZAn6DJtc6Dzkj0Z01yVW8JIycN4yjG waQ2vmkRh57JwFKZIQQsXVeduTYFwwzUZskJxDKHt5M0JfpQzLE0nUjF7YUl+RwPKMdg nT+Idm/JnjflqKztjBgoqG42r9ItYaFmtVfINZ3DvGtWMZ8rG7XtlYU4kSoEvjoTQAJp eG3Xm0mAucsGFEDQfRzyJ/EXWrGs21JLt3eo9B9ptzDBTWSxTNxWOfDlXOEZ157h4fgm TAjQ== X-Gm-Message-State: AOAM530IeID7yQs+44wVjaP/9n6HG/EEZp63gPO+3OXfh7wWskaF7rEu TcTfuP0p+idC1t7o1HaxpjaU0CZzIk51KQ== X-Google-Smtp-Source: ABdhPJzRhno4l2BJ9KxYdg8JuWd5Id9MjniuGjQAFCoYiWG8NPJ/WLbNhP9z3B1yFDbrUlw6CrdvLw== X-Received: by 2002:aa7:8103:0:b029:247:74a8:e54d with SMTP id b3-20020aa781030000b029024774a8e54dmr20806305pfi.60.1618855001116; Mon, 19 Apr 2021 10:56:41 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:40 -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 v2 2/9] af_unix: implement ->psock_update_sk_prot() Date: Mon, 19 Apr 2021 10:55:56 -0700 Message-Id: <20210419175603.19378-3-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 | 46 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 70 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 966359b64a56..97dfb747e052 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -771,10 +771,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) return 0; unix_release_sock(sk, 0); + sk->sk_prot->close(sk, 0); sock->sk = NULL; return 0; @@ -2957,6 +2966,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..8ce7651893f3 --- /dev/null +++ b/net/unix/unix_bpf.c @@ -0,0 +1,46 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Cong Wang */ + +#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 19 17:55: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: 425085 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, URIBL_BLOCKED, 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 E937DC433B4 for ; Mon, 19 Apr 2021 17:56:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A45A961246 for ; Mon, 19 Apr 2021 17:56:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240364AbhDSR5R (ORCPT ); Mon, 19 Apr 2021 13:57:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240278AbhDSR5M (ORCPT ); Mon, 19 Apr 2021 13:57:12 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CFF7C06174A; Mon, 19 Apr 2021 10:56:42 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id g16so10413557pfq.5; Mon, 19 Apr 2021 10:56:42 -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=wyCrkBvt3y2Wz6sLvGE/dTRK0nVA6L+NQj8plRBJmJk=; b=pxoJFl656uU2tx9KRIXooUcwv4KrH5NOZmcVIQHtZzAU9ubXcXhDCm+9Jjw1ZNsiJQ azR6v1ssZSQ4h/X1kRXB/463Jdz8I/9mvrjeiVMyuSq+0dlbJ8N0ct04BXQYGN1a2BfL XL03/mLkY2GICYVvI+bDRbgC6bDCCw/sprdlX22/WNg7011RMfmFwANXUa2uAOS8l+/3 AKkUiT6MR9MF8pmNBRMMsdqr2NL3U6rwxpR0Pg2IiBjEmlpcBdsQ+mJuLq5/C8zmf9Fy DwrcNGrcG8qZIqnhmaZVKEGEWor8oH6aYxo4TpDedYhvSARmZTp/ONGCJjHvzmzqir8N nJ8w== 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=wyCrkBvt3y2Wz6sLvGE/dTRK0nVA6L+NQj8plRBJmJk=; b=rbofv9Ug46xMt6wj23D9ggLSW1NIrvBpgc1pS/fhVxXzgEDLaD83HODEMS/zE9Ezpz BSl8avDlf2SxUmBxC9LWX0w0ba4zeSLjZcl0BjpDPcjYNnTqe9GBitgjbwk/zmI0PHFE ILllhwE8r+pRICTOa/KQwOZ0+o5UvydMlciIwbU6gRo5kBvNmMdjT0cv+5ANyrOkVSnJ nOjKBuh58z3aBEbkQK0bVnIoPvT29lTFzbrR+n7QDfq5ryj92pLaHOQmF1D/uvoZL2b/ 9c14bVr9qCjrJKm+loZ+vnO/7xPEqvGMUABiYkn5D+K8ebmfQuCZqXYXnayZFT/7zqhI lF3g== X-Gm-Message-State: AOAM533W7SMUpM3xIyKD/Up6t0vgJoMUmZzh8l7sAarqWPvlvtmXnXs+ UJpygkIuJgNYHdxWyjxJxFFPmQH81ssygA== X-Google-Smtp-Source: ABdhPJxdjqHIv00/cm3RGDg8YRJXYzMg47xBFAinPEPh4ifEA9eeaVaJICELN15nxEe1YaIqvlaXBw== X-Received: by 2002:a63:e541:: with SMTP id z1mr13376489pgj.59.1618855001813; Mon, 19 Apr 2021 10:56:41 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:41 -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 v2 3/9] af_unix: set TCP_ESTABLISHED for datagram sockets too Date: Mon, 19 Apr 2021 10:55:57 -0700 Message-Id: <20210419175603.19378-4-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 97dfb747e052..183d132e363a 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -1205,6 +1205,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: @@ -1437,12 +1439,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 19 17:55: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: 424326 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, URIBL_BLOCKED, 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 C26A8C43460 for ; Mon, 19 Apr 2021 17:56:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9069061246 for ; Mon, 19 Apr 2021 17:56:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240388AbhDSR5U (ORCPT ); Mon, 19 Apr 2021 13:57:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240295AbhDSR5O (ORCPT ); Mon, 19 Apr 2021 13:57:14 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3272C061763; Mon, 19 Apr 2021 10:56:42 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id w8so20241027pfn.9; Mon, 19 Apr 2021 10:56:42 -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=9iR6ZItzpdN83qAvxZislpVnk6+5Sbzg48c1AiMXRc4=; b=eEEKzCTOjvguck7SUeqE1bZ6mjwhWhzmIfjbPJPmQd2yG73URPd/IDZbSjJ7LtYPXE jY2qpJwWWmJSSWsLyjNeGNEJLUbSlTiCmStqAWd3m5+ySvqJomr2eWzX87bAVLhkYrKF OFXlJniKMX5M1fk7aYhSfG4u5dqabflnZAOz5Xqke80dVcptfUciHM3OnTDqA5Nc8Nz5 Ltg8NajHs+z6DpWq0rHGwC1NyAJ+tD59TKGL6Zl7QgRVvIgjV6AwgDq+EICyobrtPucr mg1VstUv8PLL4TcqM1Se4iO/VrYWL/dOqgjc6j7f6MLb9sQyxFMqZfHQxSJlqhlQQb3h R+LA== 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=9iR6ZItzpdN83qAvxZislpVnk6+5Sbzg48c1AiMXRc4=; b=SJmjoFpHjlGl4uTnp2s5H4wCHL8/yXzhRTTJ5mGUSZnXxDv8CHSBXnSCMxP1Xm+Pye NxbMwdttJ6ns2jiei1aT3Bo9bk6ips8XQX0Xmf8NqLh3edwe7bfStS+WsyF9485ijN9v thu4LR1XR+PPBa82Lpp18F7mS5HrmBAnmtwQF5mufR+49p6QppMVotz8lZ4016lDNVSd L+sqXWO7GOxNedLaxkjWqlK2cd8AHkWOYdOx0SsPFlV4TvFUr1c8lgAZuL1c1QoJdugi TSy01W4/oKcLV8J+QUlwmIXum1BS+7tSPDQ3w+jrpL1NsnJS86k3ssfAC85ChZD5ucRX f4Hw== X-Gm-Message-State: AOAM532Nd+9x1ybipd4PJNzWCmLUtCGaMNtGS/EBaBztp74N76KzyteK ZI0+SYGRZ9yX5aQGlvGBI07Y/qjBBnXyMg== X-Google-Smtp-Source: ABdhPJyJLqfpfaz1dTAW2zHRGZ4w5cAmsuGsatbUeSXMwSfWWc4qPgCIsVqeeS44P+hzXHeSxYZPUA== X-Received: by 2002:a65:48c7:: with SMTP id o7mr13316801pgs.90.1618855002469; Mon, 19 Apr 2021 10:56:42 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:42 -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 v2 4/9] af_unix: implement unix_dgram_bpf_recvmsg() Date: Mon, 19 Apr 2021 10:55:58 -0700 Message-Id: <20210419175603.19378-5-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 183d132e363a..1fb118a8caa9 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2087,11 +2087,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; @@ -2194,6 +2194,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); +} + 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 8ce7651893f3..83e905e1cec9 100644 --- a/net/unix/unix_bpf.c +++ b/net/unix/unix_bpf.c @@ -5,6 +5,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; @@ -13,6 +61,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 19 17:55: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: 425084 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, URIBL_BLOCKED, 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 5F47AC43470 for ; Mon, 19 Apr 2021 17:56:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2DC5D611F2 for ; Mon, 19 Apr 2021 17:56:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240403AbhDSR5V (ORCPT ); Mon, 19 Apr 2021 13:57:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240297AbhDSR5O (ORCPT ); Mon, 19 Apr 2021 13:57:14 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97A9BC06174A; Mon, 19 Apr 2021 10:56:43 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id d124so23672418pfa.13; Mon, 19 Apr 2021 10:56:43 -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=pnRru+odH83Tbsz6aQxAt6s0wPZaPwnfbEV14srN6nojtS77CdwBa8TXnZw7ZSRE1V YXpwvTZMYP2crraRl8YkifzDQCOJxdb8q9lC5OzfJTCGQpnhFGKv6eFuQXpNJ1H2WnEL pxypOQKeqz34HfJgQUZRzZIBvWP+6fG+BPsn08iMRPQlAR32m8cH74vgWLnM8jdZif7o uZt4kTvrMlz7N2d1HXQA9691dDCvngIpTH/+2kGCKlrVe3zh9rq7WY3bCoOkxGZcbvQs d2Z7KxA7CJYFR3FIfUxjLX3PYhVpRb5TrPnOOSramfNpaasy/Vm2efMJVmj0nZYPk6VT HvZg== 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=crIRi60jdL95OuuGbIzsQldd/h5+Pp5EalSNdXKgOQ/rT6j6vaBFtTs0dKDv0FmIW5 2BMk64smwshm8M4XTPpLFsHBaRsDRP35m6naF+f2WBF30obWU5URJX2VpXW3wJ8yv/qo iwgsw/zbNJvQHO37xQ2++Fh4pRUMYFMhtHOb23v7bLyM5pw5GHL4JmzXtoO6cYX3xY4m o7roB1tbTGnp6oODhaHHARm6ekgfA6lTUspkg3KNZEQGlINfZys+I+teVeHMoGHzGBTE onnlFXaYoJcQzLgXa3KV3imt5/72uGxwutmOILxftb7sKCpXarJsWY+BZNS5bTewqnRv 7Pug== X-Gm-Message-State: AOAM530AHPcKywi8qCT4ONEiFBsQSgUM31Q8dnujKjhPKY8yQgo4Ybgp CW4ejaF50W8EVGphlZ/4GaQjfw907Wi5ag== X-Google-Smtp-Source: ABdhPJwQZcIo+zcZf2Q6bBm/SYl3Fbjm0hF7pVFetnQ28SbBTSCG/qnORySs7j8Pqru0P2EWy7qIkA== X-Received: by 2002:a63:e706:: with SMTP id b6mr2185486pgi.302.1618855003122; Mon, 19 Apr 2021 10:56:43 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:42 -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 v2 5/9] sock_map: update sock type checks for AF_UNIX Date: Mon, 19 Apr 2021 10:55:59 -0700 Message-Id: <20210419175603.19378-6-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 19 17:56: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: 424325 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, URIBL_BLOCKED, 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 4330FC433ED for ; Mon, 19 Apr 2021 17:56:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F8E8611F2 for ; Mon, 19 Apr 2021 17:56:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240410AbhDSR5W (ORCPT ); Mon, 19 Apr 2021 13:57:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35924 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240300AbhDSR5O (ORCPT ); Mon, 19 Apr 2021 13:57:14 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F742C061761; Mon, 19 Apr 2021 10:56:44 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id w6so9169328pfc.8; Mon, 19 Apr 2021 10:56:44 -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=KqStVPu7kGUp3igF/rrr/zEa/fQ+FeLsboaSWYSEgZITr07bGEFP1s/JzimOY1BVs2 gxlkws416k8Wks7vWJy4vLQDGWF3+M1Ve7BuwaVGdCVyKL5Fl3WbBBU7d1krf/3eqwr+ bQ/pTwM0cN1V5BsSvfHmTM1t2gSaMdDJdqXMWO+us48I4XCj7r6yAyn3Hag5tl+c9pYf qympZz/Nuk8xFueXXklH3Q50obpmWAlrS5JafjYFaslqQkdB6X7w09JAfbvsyCP9oYmE TucBJXoYE16pA5P+emtp12qd1FHjwqEbo7y4bw5s4Ahu3QcrvuqHCr+tkZjI4IRtyLRu B9uA== 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=gu2jSOKLAPl7ysQ2QIzZAuVzKd6kd2CmitUN0tL+nqfSIslKLIXG84ePN/LjbTQkp3 50WSS9XAE0aYNvKzefVZg7Qz/Qan41nUjezO2kuqzngIPks73ojncE4ctXeysDAN4Faw +ovg3UEE1wzzeo+WJXIrtJr79sdbobVpXHK+XuxEAWYFpzybSM0dO1PaAe2wQkwK7lPy ec3E79omuO6Jtua7O4z2XJ6mqFd5hcUgq7wZo/63ReD5A/VivKQkJexTbddJpmXGIEF2 pNd9k3tsdwIM5MlJxslLhPDOI8Nv/DvJiwW4ygNh21VUGVgXxEegSQr/dBLh004gODU2 u37w== X-Gm-Message-State: AOAM531tjon+6s+QiCHC9n2sWE4X5uMuB+XsSsjnn+5pqWU5cMBvTpG3 r0eSpRm1npnKL5Oz0pnfHVabwQnUal23+A== X-Google-Smtp-Source: ABdhPJwZLEczsuXh/AZU9XHgKLVayuxtbPiifjkkXswB24EPwrVwh2n8RMFh+66QYf1nqgAO2KBwPA== X-Received: by 2002:a65:5b85:: with SMTP id i5mr12563275pgr.269.1618855003771; Mon, 19 Apr 2021 10:56:43 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:43 -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 v2 6/9] selftests/bpf: factor out udp_socketpair() Date: Mon, 19 Apr 2021 10:56:00 -0700 Message-Id: <20210419175603.19378-7-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 19 17:56: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: 425083 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, URIBL_BLOCKED, 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 65912C433B4 for ; Mon, 19 Apr 2021 17:56:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3786D61246 for ; Mon, 19 Apr 2021 17:56:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240422AbhDSR5X (ORCPT ); Mon, 19 Apr 2021 13:57:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240241AbhDSR5P (ORCPT ); Mon, 19 Apr 2021 13:57:15 -0400 Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1567C06138A; Mon, 19 Apr 2021 10:56:44 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id c3so4909391pfo.3; Mon, 19 Apr 2021 10:56:44 -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=lu6domuYkslLddbgRhjwQ4yvhxbOGbHMj5hlBxkdT+UXBOgdBadlaFGyd8EMZxPAda UZJCGdTF/j3MGGMG0kt288/MChUmnss+wenOgIcvqEZitHu0E2oCGBHrqGoNMA/yvrfS FA56iL+47+EmRGfjjc8XlTEvNTrqb9uEHOxpDz93eV7aVyPeWh/qEecipCgQLSAg8+uy /+w0GzHp3C5AKFxqOtEZvXZaEtiUMpNXP5DKuxyiLN1xEbX7PLpmTUEiz4sLr8ll8YOy DnceXlf1ZBlifaJfmiXRjzHb4m7enxw5K0V5gVDD+mnuLTSCqAE4c46nAjMBf5n5xcEU urUQ== 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=Sy4DevW2K7sWBo9dzkojIAVrg3xF2FxsoIYRBDPWbE8mmAf5YpAnq4gns8oBAdWIOl bwqPCkdd7ZG4XA8uYsKZKQH66H45djCzwRNFVicZ85YyNfIrv+JdaUf4C1kX+17B7pcN m1P8ayf4twyo/gbYaEHvM5JR7xGZijzWTvpDeCddAGr217HwfHzUt1vM2CxVV9A1HnVX HyHRQnG1eD52IyZ1DIsX04Vupi+ZdXXGuno7ZuvXZAiuRhckN85sYGPnJ637U3lU9Yfi JndTRtNuORpZmZEOZArDzB5BG7HYp/xI4L+HlvywE/B1pOgWqVQG3jkotcw3zcaseOzE ROVQ== X-Gm-Message-State: AOAM532yNGiCwOLLGw8MJVxdVmU4aHl4dJhz8j7WiwGp73R+mvyiULVQ 9OCARYzru/BP8aF4UYujlkQ/7Gi01qVuMQ== X-Google-Smtp-Source: ABdhPJz+9Ph77yuZoQ5/SL3jxnUvNhroqLeug089H9S6riKMWxXVyuqtu1tY/79Jxb31YvTrNSL7WA== X-Received: by 2002:a63:4763:: with SMTP id w35mr12979491pgk.226.1618855004418; Mon, 19 Apr 2021 10:56:44 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:44 -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 v2 7/9] selftests/bpf: factor out add_to_sockmap() Date: Mon, 19 Apr 2021 10:56:01 -0700 Message-Id: <20210419175603.19378-8-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 19 17:56:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 424324 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, URIBL_BLOCKED, 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 3A8BFC433B4 for ; Mon, 19 Apr 2021 17:56:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0C03461246 for ; Mon, 19 Apr 2021 17:56:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240454AbhDSR5Z (ORCPT ); Mon, 19 Apr 2021 13:57:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240327AbhDSR5P (ORCPT ); Mon, 19 Apr 2021 13:57:15 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DA64C06174A; Mon, 19 Apr 2021 10:56:45 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id c3so4909413pfo.3; Mon, 19 Apr 2021 10:56:45 -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=fjHqK2j9PvkkBB7g1m2ZXQrqqM2AnEkeLzTlPKDykTJjPeWuo1UNTsCUd0eQa5H3G6 fvrucmwbzaCk6FjMQUs/zbON2oRJ3tNvFZopHPk5jIuu96ktuGcFFL7ofqlnGiYfow01 BqfgFbQOXvr/EcDXaiLUvZTxrl9JLFpKOV86YUtQcljsKd3gg9flCR/OAHq1/L4ev0D2 rvzvyKBL9ERQQRO1p+EKgzlsZgywdGtUYPSzd+HHWSF8c1082q5c1SGdadgNCDxlbS3K aZ3+iMCj7KplWBUuKsewcHBZIOXESPBn1n9+UwBZD3aOHYhWiV2nYFOQ5ys1M6O8CVJs bOuw== 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=q1xbyAblbi7czk+cQSUZJcOzYgEY0CpkypxapdwGlMAUuW2Q33lFRuR0L07k6QqzQf OqNG5nN6r6v2xqWfyMkFTHtKV/jBMLye3E/hv3L47p/yxoJUxlnRq2OIAMNFaKbhHKsh G/KlHV35nzYvLzpV81C2zv4Zy7DiGSxLQrIRq97MfPCjZQAsHbkn9i9DI4moq+ihvBcm VBCNsT+1mBpd9VipduluiGSZSxewDQZ+Cq17W421BwHibYhmBiiRzQw3y/p23qmLlMS5 hGbhtjHJXKl0t/irLGLfdseItnJdnVyoVlRacIGiNQakU/uUk3ve6BK3UM0imw49dNoE HO1A== X-Gm-Message-State: AOAM533StNy79Dle7iBuiBL3mJfGnL+/AF5Mw7+6IrhAVRYFgEreoafn CKiyztCKhlgZG/8vukQF18LKTGkJ+K1uTw== X-Google-Smtp-Source: ABdhPJymSdmvJ5tg9lARzsKNoEQgKKCn8Cqe89+r8lN86o+MIhtz657x3HWoR8EhI8W9ildQH21/yQ== X-Received: by 2002:a62:2c46:0:b029:245:6391:b631 with SMTP id s67-20020a622c460000b02902456391b631mr20601638pfs.67.1618855005070; Mon, 19 Apr 2021 10:56:45 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:44 -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 v2 8/9] selftests/bpf: add a test case for unix sockmap Date: Mon, 19 Apr 2021 10:56:02 -0700 Message-Id: <20210419175603.19378-9-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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 19 17:56:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 425082 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, URIBL_BLOCKED, 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 6D1B9C433B4 for ; Mon, 19 Apr 2021 17:56:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3D6D1611F2 for ; Mon, 19 Apr 2021 17:56:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240455AbhDSR51 (ORCPT ); Mon, 19 Apr 2021 13:57:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240345AbhDSR5Q (ORCPT ); Mon, 19 Apr 2021 13:57:16 -0400 Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39A17C06174A; Mon, 19 Apr 2021 10:56:46 -0700 (PDT) Received: by mail-pf1-x42e.google.com with SMTP id h15so6023028pfv.2; Mon, 19 Apr 2021 10:56:46 -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=V1frqc/rr1547KFdoJ60IXGgtOAt9a3BG8Mqi2jtTpVQ20y50ZpjstfztZEu3vhTXC bPW9nGGlbd3HF6XfsVQTVZVMTYpeuZz/Cy5w0P4n/UFN2rt/gnH5nfWbrEOL+EgAseKz v8P0OgrD5UYai7gpHIB48u93uYCLjHf1E43+s7we+rUJqW3hQq5XZ1LRTVRVuNFPp+QJ /td0L9n5hpkFE1pALpUOXn5ID3+LyaicWP5hbaIBgPj9Tuj/i8Hi8bBR6dlSmgBQFLjA 7n/1YF/3+J1NJhpfC4prmnYEMJlpWIkwjyMWotAxcYslWpJnfVfySpvgmX0Jrpsdujnt s1bQ== 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=cs0TAqoOrKpBKzyczKkgJoDSRNwxuf3vT131u9LiPk8jILsQwnev6xDre+ttEsw2AO /VJGUUpEzP5zrzk1CZI6DxD/Dcyh4bRz8ewttYXIQfoYJFVqxZoycFuj1gOycb4dUe2C 2OhO2rVzqRlKNiAb82YtKsgZ7d+skg/FORBQ8A2qbo4YRkD3UHzMNig9NsR5h5q8zFmW yCajMZpUNAPpnpQNlIwPyR46Vbk3jFAgTncZgXo6x1mdp0raIExmbOh8I6q4eFFzfAdN n9U5K81+6m2Nyonczrjdt6f8v0ugtTPZC0EF34cM7OAPPBTBizvDCREEy4+5FvrxsRVO 76/g== X-Gm-Message-State: AOAM533YXrwSHzHSkcrXUAWBEyAWDlhxUDhbzdQ/CX2B1KYYo/F+pBYT +VSuodTvtmMrHmvop9xJ6ymvTPQaSIKkRQ== X-Google-Smtp-Source: ABdhPJyKTVzcZVpE+DzJePyBzNwdE7fW6ZWHZgX5dSpOKAs5dTX1Kl1QbLIDwiZnB1/a3/qVjDLJ3Q== X-Received: by 2002:a63:5458:: with SMTP id e24mr12929781pgm.170.1618855005759; Mon, 19 Apr 2021 10:56:45 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:e9c9:f92c:88fa:755e]) by smtp.gmail.com with ESMTPSA id g2sm119660pju.18.2021.04.19.10.56.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Apr 2021 10:56:45 -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 v2 9/9] selftests/bpf: add test cases for redirection between udp and unix Date: Mon, 19 Apr 2021 10:56:03 -0700 Message-Id: <20210419175603.19378-10-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210419175603.19378-1-xiyou.wangcong@gmail.com> References: <20210419175603.19378-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)