From patchwork Sat May 8 22:08:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432969 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.8 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 A948DC43461 for ; Sat, 8 May 2021 22:09:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 78AA061001 for ; Sat, 8 May 2021 22:09:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229641AbhEHWKM (ORCPT ); Sat, 8 May 2021 18:10:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229567AbhEHWKL (ORCPT ); Sat, 8 May 2021 18:10:11 -0400 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF9D2C06175F; Sat, 8 May 2021 15:09:08 -0700 (PDT) Received: by mail-qt1-x82e.google.com with SMTP id y12so9297230qtx.11; Sat, 08 May 2021 15:09:08 -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=1LfAQAnVkXCYeUY7D3NvE6dhD/53ZEfmaqU4/OROOBA=; b=DAeaYUanWHH9khG+lF+SCr8mTChgI+vPHkWiNvf8T/BqWSHp3mBhDA/TBT2qBrpFlA Ffgs4G4HM6Zj6ow7zi2MTl2igVrqZl/CRN3UuYGyNFEI+BuQLqUNcueDfFGIJB6hwigP cIyIMMjYNkVXfZaqPWcdQTIanqQ7uWtyjQNHjGjWjlUCF6QZwfIQVsAGlpIHWxBm/R0v q96FIWr7aAgthww+TYgHgi9GtIpSEX3g+EWQlyYKDysT4f1R86vVrsv6JN433MB1sKxY mNQA5qw0z6VIZq9t7ttZgY96JnOIku8a4uUDjPTgcn/QDTgUPp+ICde5Tc5aJzyfgAgA o3QQ== 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=1LfAQAnVkXCYeUY7D3NvE6dhD/53ZEfmaqU4/OROOBA=; b=dceBUKHyiWsvTDyfAuz20WR8MCyItJ5Oevc6dtvFWb9alxdrP04GyJa9pqciHkNZYi jSB3+gx+PDRuaCeiNd72StOH9V3I2Dz+FX7+X6kc7HWK/izqfz0VNvx7clvv3+QxRN2T L53Aw5Qax3jF0tyymkcNmXH6Jg6G3FlIE3wQwtM6FSRergpqZQBXwpOxZ0KYjaKnQJdv iBhEdJlsI0pd/dKx1M7dtUxJ3rlHTcmlydAh41DMkmJwbjEnb0UZe4sP6t+sBe7um+L7 XyjMGvTd7T/QNr8bNu32X/JWDzJw4s0i7soRG49JB1EdFmrOqQi+dO2LpNaqRyFZIH8p ivIA== X-Gm-Message-State: AOAM530TkUwXxrWnvwI2eXKvg0IEJbrfnABChFLDOMNXH32VuANrhrz5 VAxGA5enUmsP0BPEtrsrvi9L0IdURGPOyA== X-Google-Smtp-Source: ABdhPJw2LGpQODRu9JYprHauDvbUFx26mNS1tmUUIrn3VKn8upmcu6pRiFyhK5zxb9PcgTSgJ6mdSA== X-Received: by 2002:ac8:5810:: with SMTP id g16mr15091483qtg.135.1620511747813; Sat, 08 May 2021 15:09:07 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:07 -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 v4 01/12] sock_map: relax config dependency to CONFIG_NET Date: Sat, 8 May 2021 15:08:24 -0700 Message-Id: <20210508220835.53801-2-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 02b02cb29ce2..b818003739e8 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -1826,6 +1826,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) @@ -1851,24 +1857,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, @@ -1888,7 +1876,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 ca559ccdaa32..a8d9b0e05da1 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1723,7 +1723,7 @@ config BPF_SYSCALL select IRQ_WORK select TASKS_TRACE_RCU select BINARY_PRINTF - 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 f7f16650fe9e..35ced6201814 100644 --- a/net/core/Makefile +++ b/net/core/Makefile @@ -33,8 +33,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 Sat May 8 22:08:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432789 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 BFA7FC43462 for ; Sat, 8 May 2021 22:09:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A55FA61400 for ; Sat, 8 May 2021 22:09:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229662AbhEHWKN (ORCPT ); Sat, 8 May 2021 18:10:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46198 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229567AbhEHWKN (ORCPT ); Sat, 8 May 2021 18:10:13 -0400 Received: from mail-qk1-x736.google.com (mail-qk1-x736.google.com [IPv6:2607:f8b0:4864:20::736]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3158AC061574; Sat, 8 May 2021 15:09:10 -0700 (PDT) Received: by mail-qk1-x736.google.com with SMTP id l129so12078036qke.8; Sat, 08 May 2021 15:09:10 -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=JM9PvYADBYWyKEwwlSvfdrhaqUsWknbhdHFklgI2YqQ=; b=bZyvvHB+/aARPmvIVL3mD6EBz0wh8NFFDofXhMRXXKyTONMvdIWurCaobYlpeAA01W BUl4ZPjmfWNIkoIfTvixTenRyYfSwhobSIUGxTaZn3/fgketRhnAC/abNPryyOBpzwZo PpDkKzUzB3VQ2NrNQqYYTAQAaoYbEsOw2g7omz9oLvE2KPdJ52EadJaMZ1YBuP78rVak js5gOAxRKODb8YV9NtO34M4OB/j/yicIdHs74KQzRj2dmxqAbyjVUNbJ32Qjxb/Fs9vN OrBOPP4+1xkF6l5DVIf/wv6VWN1+kmFbvuHuK4EXVwPhJ/4SX6L3a0IXYNMFrJCJ0kRi c/bg== 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=JM9PvYADBYWyKEwwlSvfdrhaqUsWknbhdHFklgI2YqQ=; b=B2OYa2nsPCuV9QfeWTDC2fnFmv5w7fPkbsr5ox0MH9mlm7ya41AaSkE7eFtavegLgN pOcGe71WWT7CFT6yuBq0CYMAD1QC38IvKtsFDsvlA6hTJibq7xeFdq/zlniG6IWfto7R XsC8BI9AQcbRYm/PA++2zZkbLLJ+h8ccGGNQZ0eQyRMIIaEK6ChsQcYfOs94gT3NYcZy +lotdO13Q74rgkqZ16kH/R21VzBX+13lf7xTT7uCGcYUcXTrBFotMERpl3MTKVQrHuFx 78RAzLkZWeN03vCWtnyiMMbTUQAUZWQfT4U0gclUrXLWi5RMiYAFC4h6lG3NeewaTxaU WoAA== X-Gm-Message-State: AOAM532aPFeTQqrPBjtvCkgZPpjYJSL2CV0jRE8pB+8PsN/VziAiNgmU +vYNN4z/i1sSupQEfKtM5rLccUVsj+EKHA== X-Google-Smtp-Source: ABdhPJxpsVRos5s5jOVIJNwraUr3rZd/7fWgZEY3TiWTzjJlcMRf3+OHR7mMeJvyLdZSto9tRYcA2A== X-Received: by 2002:a37:5ca:: with SMTP id 193mr15843663qkf.356.1620511749299; Sat, 08 May 2021 15:09:09 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:08 -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 v4 02/12] af_unix: implement ->read_sock() for sockmap Date: Sat, 8 May 2021 15:08:25 -0700 Message-Id: <20210508220835.53801-3-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 5a31307ceb76..26d18f948737 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,38 @@ 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); + mutex_unlock(&u->iolock); + if (!skb) + return err; + + used = recv_actor(desc, skb, 0, skb->len); + if (used <= 0) { + if (!copied) + copied = used; + break; + } else if (used <= skb->len) { + copied += used; + } + + if (!desc->count) + break; + } + + return copied; +} + /* * Sleep until more data has arrived. But check for races.. */ From patchwork Sat May 8 22:08:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432968 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.8 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 3E41AC433B4 for ; Sat, 8 May 2021 22:09:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 118D961400 for ; Sat, 8 May 2021 22:09:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229709AbhEHWKR (ORCPT ); Sat, 8 May 2021 18:10:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229567AbhEHWKO (ORCPT ); Sat, 8 May 2021 18:10:14 -0400 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9196DC06175F; Sat, 8 May 2021 15:09:11 -0700 (PDT) Received: by mail-qt1-x836.google.com with SMTP id v4so504735qtp.1; Sat, 08 May 2021 15:09:11 -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=lU6deMHw1f7T8Vy5QqfQAadD/G/Nn7rO2ZLLwGffBII=; b=toHg32WaYqiKj0Elqih2fW+wjSMkpM61WLwJjzcIP0wYPvS9nQkL/XEsM5CCm0A58d xBvvMlR8ZiL3k4aOHv/OZio7XHFon5db1ZcR1vI/YN2wVvWvgg5gstih7erZeUZiOBRi aIcxt3+Krm2G1DWTYBbaBkks7/z7v6VHR7lHOZXT9gGudUv7a/9JRHOPRFaxPN6sUz/g QvF/d/FWgKcYydYoWr4ecJ4m7e0OxC/LoS1mHj+tCVCEHS737KTK3wbnMVFwCw7wAvdc DapAwvP7urcr7W5qBOOC5K7aSV8ALrp3ipjs5TTfA7RXJBlKEbIXlMHGPtHQ9lon5rOj JIHg== 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=lU6deMHw1f7T8Vy5QqfQAadD/G/Nn7rO2ZLLwGffBII=; b=mXzWuKuF4urO58HZ0kZt+OhqDskONFKV5PbQGjsbXktmUzVszV2pCO4gClwi6yHm4b gfg+fM0r+nqDAdbESzg1ZgtjSh1lG4SLSW0Ooy8h65JwtceO2ixEhBjQLJg9cprHVoQ5 Jml91QrOM3wnKLJRp3DWVClC1zfOFLT1zihQXScbC5Ejmn4zU6yYAKFK8PngYHv1dYmH BtxpjOxm4hDLAxHblyvxxc4u0IifeiUtdTbnq7e/8GJASkN1VfW7h+9cMHi/NwS1Phhy H4ivwk9Z/avUG+Bu6ZjJKFGceyR6giZj5grDTF3KJVbjrgT3LzWlyH1JKSINATLeLips u3HA== X-Gm-Message-State: AOAM530kgaXXi659aKg2Fu+CxK06DGm/+AgZC+zOMONpCg/J+yQh/6oE ozQqbxjHT40EZkl14Uz7pkj0rO11hpWwIw== X-Google-Smtp-Source: ABdhPJxhbfPuz7Sx0z8Lk0C1xiMdpNGtlvGVJTqY2egkcrdV9HFwWt/g2Dpos94c34+NatASZtNgUA== X-Received: by 2002:ac8:7b8c:: with SMTP id p12mr15432903qtu.137.1620511750688; Sat, 08 May 2021 15:09:10 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:10 -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 v4 03/12] af_unix: set TCP_ESTABLISHED for datagram sockets too Date: Sat, 8 May 2021 15:08:26 -0700 Message-Id: <20210508220835.53801-4-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 a later 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 | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index 26d18f948737..e08918c45892 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -487,6 +487,7 @@ static void unix_dgram_disconnected(struct sock *sk, struct sock *other) other->sk_error_report(other); } } + sk->sk_state = other->sk_state = TCP_CLOSE; } static void unix_sock_destructor(struct sock *sk) @@ -1197,6 +1198,9 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr, unix_peer(sk) = other; unix_state_double_unlock(sk, other); } + + if (unix_peer(sk)) + sk->sk_state = other->sk_state = TCP_ESTABLISHED; return 0; out_unlock: @@ -1429,12 +1433,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 Sat May 8 22:08:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432788 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.8 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 5301CC433B4 for ; Sat, 8 May 2021 22:09:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 32EE86143C for ; Sat, 8 May 2021 22:09:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229723AbhEHWKT (ORCPT ); Sat, 8 May 2021 18:10:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229673AbhEHWKP (ORCPT ); Sat, 8 May 2021 18:10:15 -0400 Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DE3FC061574; Sat, 8 May 2021 15:09:13 -0700 (PDT) Received: by mail-qv1-xf2b.google.com with SMTP id r13so6616920qvm.7; Sat, 08 May 2021 15:09: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=7d/KU/DC62hu/FTJavom/LekEDnR80g4Tdja2rES75g=; b=s3NkkDJMhuw6Pvh66o/XcQB6W/rRnrwQ4HFpzl+nyj8x9ICEEayghHWVNu3Vumg9LL 9f6qQSX2NzLyWR//OWRY2KywXzdspI/0PI9V7XJP2Ek8WEXV95PTTqim4uLHLastRl5K 0wsmPrHCc4dWRf6eAbK3vWEkrRkuyGZyMNWAHAYbqFfonfwJhD4OX1tFm5/AdHHX1p2t QwnKccO/cCzV34SE/jWWp6EoCmNOsQ765zCP1Kg89Z2TIB1qrZIOrQ0tL5bgMW5wEo1W e34qLkjnwKnmrzme6zzKCBt+kUaQh/HPMf552V9nsqSb8yqItNOksp5GLpPc+n8/N+qo JRwA== 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=7d/KU/DC62hu/FTJavom/LekEDnR80g4Tdja2rES75g=; b=iAp/GGfvJUl3yfS/31bk2+GGPARU4UHPukhC61nHQTjsYpD49uqD/C5xdxgjdD5lbc 0g471qfgyBQJ4N+gkoPAHvBsg3zA0cZmvSwFmwfepsBN4XaeVu03PO9Ed8+sCZmcUa9x N5SMDbGVrefVFd7xKCtcouVfg50K//y+q4eUUqA3d1OGNA6Qivuj3RfTHwHG/Ru/mC+r JPKiCrpydLhgERjoB/KpzfcK28ny0Da3FPjNhmqCBeckmTXkEQ4Q9expXnGFI7SRGNTc CNnguWGUHatsyxS5tC2rLWjOzWY7c38eoTkXZ71gnxwlcOE9TXNXdWo8tKKwkDVAi5os eVDg== X-Gm-Message-State: AOAM532p7iDRKhUM+qIPwOz9lTz1nWr6tJByw0AR+VKrMk1OK26ksEcU k39Dl1AiZpg3c6u1afxDNiBFyJ+aCzBtBA== X-Google-Smtp-Source: ABdhPJycm4RJ5dkrx/0QkwSdG9YzBCTFtY43LS3uk5Q1rzA+aJ1Wq794g25vqNApBlnMasOxCJVhrg== X-Received: by 2002:a0c:ec0f:: with SMTP id y15mr16335250qvo.9.1620511752122; Sat, 08 May 2021 15:09:12 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:11 -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 v4 04/12] sock_map: export symbols for af_unix module Date: Sat, 8 May 2021 15:08:27 -0700 Message-Id: <20210508220835.53801-5-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Cong Wang Before af_unix there is no need to support modules. Now we have to export these two functions for af_unix module. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/core/sock_map.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/net/core/sock_map.c b/net/core/sock_map.c index 6f1b82b8ad49..13b474d89f93 100644 --- a/net/core/sock_map.c +++ b/net/core/sock_map.c @@ -1513,6 +1513,7 @@ void sock_map_unhash(struct sock *sk) rcu_read_unlock(); saved_unhash(sk); } +EXPORT_SYMBOL_GPL(sock_map_unhash); void sock_map_close(struct sock *sk, long timeout) { @@ -1536,6 +1537,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, From patchwork Sat May 8 22:08:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432967 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.8 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 6BA28C43460 for ; Sat, 8 May 2021 22:09:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4B0256140F for ; Sat, 8 May 2021 22:09:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229744AbhEHWKV (ORCPT ); Sat, 8 May 2021 18:10:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229699AbhEHWKR (ORCPT ); Sat, 8 May 2021 18:10:17 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72B9EC061763; Sat, 8 May 2021 15:09:14 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id j19so9303086qtp.7; Sat, 08 May 2021 15:09:14 -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=iDyoH4IWpKzUfHp//0ErURRULDPHH7w6/umK0bSTNNU=; b=XCLgfJEG/iJm6iSiofjM3joFRSbgy1lLkp08yOAwGo96l46eQsV/VpmI2ROEOc9ZHx f7rQ/YKEKYlFlQNq2tk22lbo7vyK3QFU3fDweKQZ+omGqnVgQTLH7tPr58PUhCklhzTv 9kFcGDJo73E1YqVE4wWW+TUJkK+whxA5yPN/A77fybO5iH34S+trleJgheuSeNjVNETC GEbJE89kYqNGpgVI6w+g04wwvF1ziOyBC3HiiM/4swl5dCI8TkWDwkCnuJp+jQWq0EbT iY2oDTnrU6xWLLVGMpYDXF5xYiSISMOJAtdtifWsKhy1eyRnK3mD2bbm4OHMd1sYEevA YYYw== 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=iDyoH4IWpKzUfHp//0ErURRULDPHH7w6/umK0bSTNNU=; b=U9INCCrAay1xxbyTw6R8Wqi8D0BvzC9H5kKgXXMmReOD2LO4fgWjttV6e0rYGmr+qZ e217V/5AeecxLwvFKc32U9o0P/2nXMWQcrMgIuI4f7EfIPnmSrtyUH2lTR/1BVxj0okX Las5hOVTbSUrFFGE90isFDQDlYj8VJKy+ZyuDD5BMa9/pDNdVwuUJbMGiYUKb9kpInaV OS2CA6jQr+6E1ORQp/H0MOFRsg0IhvxGmJbwr8lgB6aZmzMUoml/1nmbp21l3rHMa0gi O++cE4Q2iyAeXHTcZmC3DbLQjgrzx7VCKrHhBoQpWPRfwnj098OTCpNnfYDAOtRH67zJ 11Tg== X-Gm-Message-State: AOAM531MKcoBbw32Pfbk/qN6xpb+ygvbGTbPmPGjuRppsIk/xl2ShivI 5l2rhWUFgF3aHYwviCzQ8BrkLaHYwUKV+Q== X-Google-Smtp-Source: ABdhPJwBAa8oVG53RAgZylUlVsLZWMlMkcAlVEZKU7ih0IjtEhUQZNY+J80Nvd9m2cBin7r5qUmY3w== X-Received: by 2002:a05:622a:10e:: with SMTP id u14mr15427950qtw.229.1620511753562; Sat, 08 May 2021 15:09:13 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:13 -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 v4 05/12] af_unix: prepare for sockmap support Date: Sat, 8 May 2021 15:08:28 -0700 Message-Id: <20210508220835.53801-6-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Cong Wang Unlike af_inet, unix_proto is very different, it does not even have a ->close() or ->unhash(). We have to add some dummy implementations to satisfy sockmap. Normally they are just nops, they are only used for the sockmap replacements. Cc: John Fastabend Cc: Daniel Borkmann Cc: Jakub Sitnicki Cc: Lorenz Bauer Signed-off-by: Cong Wang --- net/unix/af_unix.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c index e08918c45892..0f9a6dcca752 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -487,6 +487,8 @@ static void unix_dgram_disconnected(struct sock *sk, struct sock *other) other->sk_error_report(other); } } + sk->sk_prot->unhash(sk); + other->sk_prot->unhash(other); sk->sk_state = other->sk_state = TCP_CLOSE; } @@ -773,10 +775,23 @@ static const struct proto_ops unix_seqpacket_ops = { .show_fdinfo = unix_show_fdinfo, }; +/* Nothing to do here, unix socket is not unhashed when disconnecting, + * and does not need a ->close(). These are merely for sockmap. + */ +static void unix_unhash(struct sock *sk) +{ +} + +static void unix_close(struct sock *sk, long timeout) +{ +} + static struct proto unix_proto = { .name = "UNIX", .owner = THIS_MODULE, .obj_size = sizeof(struct unix_sock), + .unhash = unix_unhash, + .close = unix_close, }; static struct sock *unix_create1(struct net *net, struct socket *sock, int kern) @@ -860,6 +875,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; From patchwork Sat May 8 22:08:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432787 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 79514C43470 for ; Sat, 8 May 2021 22:09:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A30A61421 for ; Sat, 8 May 2021 22:09:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229763AbhEHWKW (ORCPT ); Sat, 8 May 2021 18:10:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229713AbhEHWKS (ORCPT ); Sat, 8 May 2021 18:10:18 -0400 Received: from mail-qt1-x82a.google.com (mail-qt1-x82a.google.com [IPv6:2607:f8b0:4864:20::82a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7586C061574; Sat, 8 May 2021 15:09:15 -0700 (PDT) Received: by mail-qt1-x82a.google.com with SMTP id h21so5544927qtu.5; Sat, 08 May 2021 15:09: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=wlJs7uKgRrKVYquQOhfr1Ax5B30l7MZiTJBcTYOyR4M=; b=B0fJ50FjuwLYki5kIDudC8ABjvkFGRzwsQWQFOV7LwSJGCzedbwyWxpl4dgJeZ059e 0+Lm3WDTbD4V0igkjoxnW+hajxmdkGQYuAtkTMZje1yic+fbhOI5em7OGWfH/Z7CF+ze Yf2GEZ6hpw4IfHEA4b+tV0C4uRLCgoMGEXe4H4YtfG6Q4gu9QR4mu4DnUXf0YzWDwmza aM5aVK0deZtXaSbgtxAFFMFQ5s59SHUgb02C1HyZmZzQY6eGxEysld8Iyqtsv5U8Sy6D y2gYch4m1ys3zsqrR00gvkqwuRmThryKhFgwiHTCkMCGgljxIAshReOP1yQ0DXo13wwT +Uew== 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=wlJs7uKgRrKVYquQOhfr1Ax5B30l7MZiTJBcTYOyR4M=; b=VsYERw/lntmO7Kc18wEf5evLqFuHUYAjUApqs8jPcaRC+TI4NGcdKuZxRXqSqaqxGq MkDckOGy1D0D3WvIYTMuKvxqVhQX3zfkyY6pvPz12mbj1UXI08RveKFahMnBOdOhp8yr 3HIOzoYmpq9LVj1LOglBNzgkM8gTgFuPHDxxuXyjzwghTEw12DykCdJvPoV3/pE1bpoy o8AGhVVzqBHtV9JCnPLZrcnBslaGvf9qOppm+L5kJE3sEdlhcuvKudQqj34TGWFSvaa1 uegvcmbSIKoBtRnXwpgQaVNBxV3XIJRTeNTJGazZ1JsFfZf2iw4mLcE7s8d2uQ+rodfv 1dtw== X-Gm-Message-State: AOAM5324YJ5Le+WDUBLqCqGehI5Za369hk4Z0hTWzjrvRYbw7A9R5SXE WOJ6Y7TQKxj2vmDsLPVku9UGtkBSGPInuA== X-Google-Smtp-Source: ABdhPJzkkgTr3JfLkKLsORtgZDJa23gENArDIBy0nI3v+10tjkKLBTKtbivHTEaBEHT7E6TtHTen/w== X-Received: by 2002:ac8:502:: with SMTP id u2mr15313897qtg.218.1620511754964; Sat, 08 May 2021 15:09:14 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09: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 v4 06/12] af_unix: implement ->psock_update_sk_prot() Date: Sat, 8 May 2021 15:08:29 -0700 Message-Id: <20210508220835.53801-7-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-1-xiyou.wangcong@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Cong Wang Now we can implement unix_bpf_update_proto() to update sk_prot, particularly prot->close() and prot->unhash(). 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/unix/Makefile | 1 + net/unix/af_unix.c | 6 +++++- net/unix/unix_bpf.c | 48 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 net/unix/unix_bpf.c diff --git a/MAINTAINERS b/MAINTAINERS index 4796ccf9f871..f9354cb422b1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10162,6 +10162,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/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 0f9a6dcca752..5424e8007858 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -786,12 +786,15 @@ static void unix_close(struct sock *sk, long timeout) { } -static struct proto unix_proto = { +struct proto unix_proto = { .name = "UNIX", .owner = THIS_MODULE, .obj_size = sizeof(struct unix_sock), .unhash = unix_unhash, .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) @@ -2973,6 +2976,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..3ff71fb9aac8 --- /dev/null +++ b/net/unix/unix_bpf.c @@ -0,0 +1,48 @@ +// 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->unhash = sock_map_unhash; + 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 Sat May 8 22:08:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432966 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.8 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 81D4BC43600 for ; Sat, 8 May 2021 22:09:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 61CEA6140E for ; Sat, 8 May 2021 22:09:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229795AbhEHWKY (ORCPT ); Sat, 8 May 2021 18:10:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229696AbhEHWKT (ORCPT ); Sat, 8 May 2021 18:10:19 -0400 Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42EECC061763; Sat, 8 May 2021 15:09:17 -0700 (PDT) Received: by mail-qt1-x82f.google.com with SMTP id a18so9296700qtj.10; Sat, 08 May 2021 15:09:17 -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=E+M2e+5FqfWuaO4MmVZgDT1VoAboQ/2kqTqv/TNQTvA=; b=T1+NQyZnJAwRjz/vV932Oo4s/cC9eaYa0XUpHqetcT+5v7H3hcptpOQNQmAqZTZ9k+ o1QIyRV/zU2xByZkWkq8gkz0CUNXm3SP9fIK2Ua7Q+nUs5U4ZAB8KQSV/ZSNnFgn+ZHT 3ZXwSQKTatrCRn2TcpuV+tb2dMwBypnjPZcMCFUc50fkI+BKg6M0cIHm0CtxgBluW6w6 rrppLyKEj2fWS8ji7q0pzdJx1YxDxD9cmJnrnujqV6AWlomJRzbKm5VewCzghLsxVYqF MgcJ4lw+768Qc3VGv28nFlAX79VjOSqZDwppY3yn7ED0BQGNlmYNRdZzugNo1GBgSBYt BGqg== 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=E+M2e+5FqfWuaO4MmVZgDT1VoAboQ/2kqTqv/TNQTvA=; b=N0Z8CviaG5kXWfL7NK1TMAsAdp/WS7HCXxYHAUAFKuKa5W6yjTjY7deQmalyUK4CHz +B5yylft5IieSACqMomI1wgU3jbd2/4jmImbtBewAVSeE1HDz4pymLaiVz0UNzxRb+xL Kwmk0eZhP6PRokR+gSrhR8BJ9Ag9OkSuJIVlyzQqwfnZPoePPO1I5cprNSTRxCyi7FOB rLDB1RDAQxj0U1HWJYIcRWOVbtOAj6tDyxEhqllPUOCiSrzqUE6RrAbhe8z0n1v7ebmM m4tfBDp2ahDDboeZE2kSo3GBjEW5SuaJhbh4PmnFm3wFpcuVHI8+ZLpbuxnfGWEEZmSH aeoQ== X-Gm-Message-State: AOAM5333Q3Slfr7kBQ5NdWvibTIGMXXcp6smlJfrJKtmHJSJQacpLSOC eCleMEksKjS87erFxPrudNcNuuPP8WopnQ== X-Google-Smtp-Source: ABdhPJyVoa2AvzGRkQCi0mnhs1h/ry9GyuuZIR0f5VgEvJvqDLNYMC70F1ZUPhxa/YFKZGpjAEufeg== X-Received: by 2002:ac8:6f37:: with SMTP id i23mr80916qtv.221.1620511756371; Sat, 08 May 2021 15:09:16 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09: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 v4 07/12] af_unix: implement unix_dgram_bpf_recvmsg() Date: Sat, 8 May 2021 15:08:30 -0700 Message-Id: <20210508220835.53801-8-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 | 19 ++++++++++++++--- net/unix/unix_bpf.c | 47 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/include/net/af_unix.h b/include/net/af_unix.h index cca645846af1..49b13e9bffd0 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); + #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 5424e8007858..e718ca6237a7 100644 --- a/net/unix/af_unix.c +++ b/net/unix/af_unix.c @@ -2100,11 +2100,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) { 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; @@ -2207,6 +2207,19 @@ 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; + +#ifdef CONFIG_BPF_SYSCALL + if (sk->sk_prot != &unix_proto) + return sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT, + flags & ~MSG_DONTWAIT, NULL); +#endif + return __unix_dgram_recvmsg(sk, msg, size, flags & MSG_DONTWAIT, flags); +} + 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 3ff71fb9aac8..debe82e1035d 100644 --- a/net/unix/unix_bpf.c +++ b/net/unix/unix_bpf.c @@ -6,6 +6,52 @@ #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); + + 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); + 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); + 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; @@ -15,6 +61,7 @@ static void unix_bpf_rebuild_protos(struct proto *prot, const struct proto *base *prot = *base; prot->unhash = sock_map_unhash; prot->close = sock_map_close; + prot->recvmsg = unix_dgram_bpf_recvmsg; } static void unix_bpf_check_needs_rebuild(struct proto *ops) From patchwork Sat May 8 22:08:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432786 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.8 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 3D5ECC43461 for ; Sat, 8 May 2021 22:09:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2200761400 for ; Sat, 8 May 2021 22:09:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229821AbhEHWKZ (ORCPT ); Sat, 8 May 2021 18:10:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229732AbhEHWKU (ORCPT ); Sat, 8 May 2021 18:10:20 -0400 Received: from mail-qt1-x829.google.com (mail-qt1-x829.google.com [IPv6:2607:f8b0:4864:20::829]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACF8EC06138C; Sat, 8 May 2021 15:09:18 -0700 (PDT) Received: by mail-qt1-x829.google.com with SMTP id y12so9297393qtx.11; Sat, 08 May 2021 15:09: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=uz9J1zJKumDnC+H3DASRwpCq7TM2++iFeyTtmw6XOWA=; b=bn+jhPUUWdrTp48FkzBc4xAKA9bwe9NNdvdW+QaqENeOi/yQjzspaQcz07LYAmyh1x +K+CGoojiVkflvhKCRTAcDMmWARJpX3NV7f4YH4RIMxa4Jkg6I0pVVpB36rLITbvK8k0 gugC3nLaOegISPGdaEdcLAKqnLd210HPHyKcltE1+9Hv/miUdgP4Dqh+AT1wi7DMAZML 6aa3UCYAg9uMpCGUi+IQs57RrC2ykt1WtRtppko24iIm08Ad0Xk09og2zWsBKJwrBYiQ PUO0HHdLt50WGwFB/8TVdHVNa2aXxzWkqXPJfVpol5n3aQHkxyNA3M/KLn87oWOHINDl SgrA== 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=uz9J1zJKumDnC+H3DASRwpCq7TM2++iFeyTtmw6XOWA=; b=rIHVSQbNvYqGgMH1UhJPZM465Tj6m26q8CajXTwcIVg7rL5071obLS9LnDfieVOhNF TIMhULsI/+xA8sF8vLVcRAiuLr8qxUj966CXqpvs7mR5zIBwVSizmuYc6J2HAlHW+8Hi 7sJFe66IvXj50UrOnNefGOvjR4RHA4un2sV9797bdeGiR68oQKi5/cFlX3Lf99LYg74v DwklKqYCJgCqmLv153k+8fyxrjEwS9nOS0Cxe9OEaDLQFXBoaN5ZHk/1fKrF9v7zo5XL /wC5e1AqXq9gfi6ZRBOjKOIxd7Mlez6KnV+3VPs5dCEA9XqqSIpZ+68KZIWer5Tr533A urqA== X-Gm-Message-State: AOAM531mOE+vE7i9Go62EnemdUqzgsbzQADKvTcFGxJkSW2MA5eZDxLQ FTdwe/8XWckFRuximwqZV7JuB4uJXgS4jA== X-Google-Smtp-Source: ABdhPJx7Vp+TcTZXrbhbNyWkMVIzvPKEtLAr68fC0VA/fNA8F4iPepvDTmREGeS0RlLP947rcfTdLA== X-Received: by 2002:ac8:5bc1:: with SMTP id b1mr15001360qtb.161.1620511757831; Sat, 08 May 2021 15:09:17 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:17 -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 v4 08/12] sock_map: update sock type checks for AF_UNIX Date: Sat, 8 May 2021 15:08:31 -0700 Message-Id: <20210508220835.53801-9-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 13b474d89f93..ee9a6dfcf269 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 Sat May 8 22:08:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432965 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.8 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 AAD5CC433B4 for ; Sat, 8 May 2021 22:09:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92F9161001 for ; Sat, 8 May 2021 22:09:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229799AbhEHWK1 (ORCPT ); Sat, 8 May 2021 18:10:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229765AbhEHWKW (ORCPT ); Sat, 8 May 2021 18:10:22 -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 2A357C061574; Sat, 8 May 2021 15:09:20 -0700 (PDT) Received: by mail-qt1-x835.google.com with SMTP id n22so9300436qtk.9; Sat, 08 May 2021 15:09: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=ZLt69FAUdQ5bTjcW1qMZRDUzZdTqyuja2OTZzrCIJKA=; b=QH77ESMXTdvcN9H101MLsSEAlop+4jYyFNEkWpeRSqUDU8149qOcELR1L2OkvzPpui XqR3Zo1IhWPZBVNDeX/KxmnvNJXWRx5osCZHFbNbsZ7ImZKMs9tu6pPDLUYt5Odn/+TB +55WnYZW5CZY0sROUKw54j43vXd1+NvX1AK6hgC4ys4XCnMhbhzVDdqRxx/LZ+jLdV3v 7KwcpXKoiYRgF9yZTLdPJQQffF1Udz93HLSfeScyoVewsVglnICCdjFnmIMLIZ6jy0Ik Q6IDmXZ7B/7TVL2lOO8cUuQ14fDdC+Gc+4uhF3v3rNc0M5dNsx+vJoolGzCWXD0jkZyE 2DmA== 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=QwQeXDd9vR7umsiH5fBUULXnf9ugprIKvNl1rfvoWb7hZJF1jrfr318+5qTOxp3fyf 16PXWtZP/jbyGcrg5G9Z8j2rrFutkYI8+rEwGRVMR8Yq9XwS0+T2WRfHllLkEpFuQyvn dZFXiFPILYnCVqumFuuh/onLfHAbpw7p6qOwfEveLZRH+vpJaOwj8qqY/HDjVhLAnzLs dM/TiF9m7sCgrkQ9eaBp1yX5Gj8XtmTjWxvy/za9Bh858OzimtvUeTMCo1VSx1yReAWz E9m68fss/OTnpCYXn8vcdJ6DQskw5A+HgVrkWRHrh0hrW/pEy5wMdKIN0HmwfzZClsf1 vGow== X-Gm-Message-State: AOAM533qdzgmfIsdR+kiqAGhW1gsKUYLisEkOJbXY5PJVMDODZAojO0S 9E5RLblJTuCKvBkbLNqMeth4ALD0VUmMXA== X-Google-Smtp-Source: ABdhPJwf7a0MyLfe84FJIHQwSjidnK3XFfNcVqeeljTjm7SAvphCba6pUhOXtl+EGHOmB0Q7y/HqKg== X-Received: by 2002:ac8:4c8f:: with SMTP id j15mr10354430qtv.328.1620511759259; Sat, 08 May 2021 15:09:19 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09: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 v4 09/12] selftests/bpf: factor out udp_socketpair() Date: Sat, 8 May 2021 15:08:32 -0700 Message-Id: <20210508220835.53801-10-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 Sat May 8 22:08:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432785 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=ham 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 D24F1C433ED for ; Sat, 8 May 2021 22:09:27 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B13C561400 for ; Sat, 8 May 2021 22:09:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229825AbhEHWK2 (ORCPT ); Sat, 8 May 2021 18:10:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229775AbhEHWKX (ORCPT ); Sat, 8 May 2021 18:10:23 -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 8D547C06175F; Sat, 8 May 2021 15:09:21 -0700 (PDT) Received: by mail-qt1-x832.google.com with SMTP id a18so9296767qtj.10; Sat, 08 May 2021 15:09:21 -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=Ogqo56PQLf08hCwkiFX/8uGXsIiitdm3srxZH//LeG1LiTrKCrzMQrBGgmoxkIeJ8n vaLQE1EayfEaOeZB83hNt+7P6orEH9IkuD5pl3J7YDF3r+LtRZV+DFRL2KSmXU8I0OsU JrQBw2abx2OkpjF3HN5n9vQ+P9Xf4iIEgpPS2szkvvKC4flU2SXpxlHcoOzvKuTghtG4 ETugtjb4jVadWRiqw/7AAMo/qHKfxdXJiT8xe6Iw6UIHGPc4H/Jp1rXEJBKaFnhDS+tK zjYX/L5kKF0sfzqz8A0H0ISDO4n90//5CnpilZ94AdBNBHXS5ULeyU8OzuENaCdiIuC2 OBaw== 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=p3NfBBDxdc0/30GQwc/qC6HR/5l2BptXJ8bSAWDzj7vy4hJII2Kq6P36zU9FKXWhNU LyT282R6+wKv8n4ECD16gNxZMZdsPrGFALMV3vl57Y3H8ztZPKDgx791p6QXGKpdazv/ iJ4hicH5NXEK9cuNr1I2CIGoUSpai/8n3HjyaSlIKmjEgYSw4nLkcLDoGvl8pbXtTydf FTr5w9tczPQ72P9eGSfIs4+mvAtOh3VJUNzTBGdIjo+HmylIJ8nW/hOJd+/GS7i52FMY iGyNmAFkNGfkm4qUM+2mUEldrNF/pL3VQqixHUSUwUBUWv13cnFixzykwklaL1YQ0VbY fFug== X-Gm-Message-State: AOAM531Mgvun4JXY8aMRi7QOOHEwbmDpRE0rqUnvAepOLUDYuA44qS9J YoL5KLm9De2o5HK08BD15/YvuyLHLG9zsQ== X-Google-Smtp-Source: ABdhPJyMyMkrLI4a5DFFhYp8/wSAaqYY+D3oKgJbDOFXBIeQJcMUM/jNhEcjVkVJuJmuUNhEd4oFSQ== X-Received: by 2002:ac8:71d4:: with SMTP id i20mr15956761qtp.286.1620511760680; Sat, 08 May 2021 15:09:20 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:20 -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 v4 10/12] selftests/bpf: factor out add_to_sockmap() Date: Sat, 8 May 2021 15:08:33 -0700 Message-Id: <20210508220835.53801-11-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 Sat May 8 22:08:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432964 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.8 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 173C1C43470 for ; Sat, 8 May 2021 22:09:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F0EA761400 for ; Sat, 8 May 2021 22:09:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229827AbhEHWKa (ORCPT ); Sat, 8 May 2021 18:10:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229823AbhEHWKZ (ORCPT ); Sat, 8 May 2021 18:10:25 -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 F213DC0613ED; Sat, 8 May 2021 15:09:22 -0700 (PDT) Received: by mail-qk1-x731.google.com with SMTP id a22so11529342qkl.10; Sat, 08 May 2021 15:09: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=UjK9BI5v2k7+AWEzGv15pygYECWaieRAsq8bft/a118=; b=j282A9LNJNRRWHE1BDqbouxhjROqrAv58ihSFx3TLaoFrC+RQ3w8d5wk7fbDdn4wXF CK8Vx5CssJ+lRGtLsz5SljACbwV4ozSzqx367DXYFay+GPSTxH0qGR+KUcjawdDD/9+l aUHuHk0WoxqaWWip+WToW+bz1fKpxBnLZqYHqKORJO71jYJhssejCLX303cYmh+90afk +dI41NfTyxMtbt8QRmw38+tJmirhQjtzfsEftsep3xY3Xl2AnIgs0o+Y9rctygKes2s2 IWxV4e2EkJgUphG0i/E6gjGN4UKoAB4qAUtz8cR7xtXt0rCc2m/lZQTUfs/rHT2HTLnB exJw== 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=YxGsAszbK5IWEz1taW1FWoPFsM2AdUN5sUpOcOqRH89SI786Uj+wZaxhq6L9Tac46Q +ybYW7dwGcEYGk2qsvk5rjDm0hFI77P7OPfrMHBeamF92Oe6bOLBepxf3+000FqNXc4b MLs2mzc/3l9ZBXz29MEsfSh35Pl8GDt7vxooIVmy+Y8hPDcgzUEv/3knJIb+UM8nXdC0 G+J3UQbK2oSNIrjMiYjJCbkViE5ZwHFGwEYffvo9AXYTefpd532Y3VxRE+olaezleflu KtiFmtpURIdRP5HA5dz/HmWOga1esDViXOxG2wPZ30hizdBT1jVkx3ZimhgNNRxWwQSg TuuA== X-Gm-Message-State: AOAM53129G+G8UOoUXBHaszPX6rhTdgh62TCa0wELSSP2jS9HGbnXLJi QRe+838056ALVGiviUeDafft1f3IwgS63w== X-Google-Smtp-Source: ABdhPJyzBiJbTiPlfCeZSR+WAwXQLhdP7/YrNm/T7WZq6GN3la2TAZ3/AQgyLkzD0SQKLZNiQ/O8yA== X-Received: by 2002:a37:42c3:: with SMTP id p186mr16320219qka.352.1620511762082; Sat, 08 May 2021 15:09:22 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09: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 v4 11/12] selftests/bpf: add a test case for unix sockmap Date: Sat, 8 May 2021 15:08:34 -0700 Message-Id: <20210508220835.53801-12-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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 Sat May 8 22:08:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cong Wang X-Patchwork-Id: 432784 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 EA316C433ED for ; Sat, 8 May 2021 22:09:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C449D6140E for ; Sat, 8 May 2021 22:09:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229739AbhEHWKm (ORCPT ); Sat, 8 May 2021 18:10:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46298 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229769AbhEHWK1 (ORCPT ); Sat, 8 May 2021 18:10:27 -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 60920C061574; Sat, 8 May 2021 15:09:24 -0700 (PDT) Received: by mail-qt1-x830.google.com with SMTP id y12so9297501qtx.11; Sat, 08 May 2021 15:09:24 -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=aHdSQN18OQto2rONRzflkU0nt3yXjniMeit89QC35HdOZMYDaveiGxkeJVf5y4PsNm Gqk9b0mOkeDsdJlGdKJxmtCkPAzPvMn5yBA6n1SPCD390473wqjX6gE4rw4EtkmGmYHv j18Hmi3cSvbXNi77pvu2z06an7oVO3LNCXmYK98rQjnxMtW1FAhOxG9mfMifQL/DPXt3 Rd9qpsMrHZ88Gt1frs/DexYo1p9bYmBUA9cxmPHZ/9rVMLirh/12xdgmRaDYkCm3ju8R AqyjdztDRlH9YCZ24bCeAdcHokWN5NY9uf5OvCCnyl9Ka7xBXzGlu29i/J3kIR9aKIZ4 XqeQ== 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=LNGVbC1dmDH5CJbegZKG2vT4KXLCEannhXjaK0X756dJZ/1yyOq69DnXIklP+4GD3X 0oTSniSI9ZcyoS1hVpkkCw7UKA4E/nG9H1cG0EVCNqJFfcndKi03AqlzW6NV0CkQADwf IzN2mVz7SwpzKZ2HmE1ELJViey5n7IHoFFpkNNK6x/DO6YK7IRG8FK0y6OWC6On/e/wt eBG1S+dX8kRnaEDcoXXiOCl2UWzbWwVqkm9J7vBy0KxcLHju7CO6UzNwqxijDlKfaU1G dpAtQ6YJkgC3QpxTUNTAoeckbjrHGuMIKDnfWNn3SJHXvCj8VVItPRbIWPSEhYDrJDs0 gb5g== X-Gm-Message-State: AOAM531E8YBJjfb5V2L552g6DZKytkGb02oUAueo9el6H3QruSV6HoJH HG9KKHv+PeU4h2XJdaqe4RrJpO8/BOnlwg== X-Google-Smtp-Source: ABdhPJyHu7/Ke34yGeuk4vMbPkkICdtNCmzKVgkQ7D4xYYqtI8GfV26neVmnqt9G2AB3qYlpfIpkCw== X-Received: by 2002:ac8:4792:: with SMTP id k18mr15629580qtq.111.1620511763512; Sat, 08 May 2021 15:09:23 -0700 (PDT) Received: from unknown.attlocal.net ([2600:1700:65a0:ab60:65fe:be14:6eed:46f]) by smtp.gmail.com with ESMTPSA id 189sm8080797qkd.51.2021.05.08.15.09.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 08 May 2021 15:09:23 -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 v4 12/12] selftests/bpf: add test cases for redirection between udp and unix Date: Sat, 8 May 2021 15:08:35 -0700 Message-Id: <20210508220835.53801-13-xiyou.wangcong@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210508220835.53801-1-xiyou.wangcong@gmail.com> References: <20210508220835.53801-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)