From patchwork Wed Aug 31 14:40:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shmulik Ladkani X-Patchwork-Id: 12960836 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19FCAECAAD1 for ; Wed, 31 Aug 2022 14:40:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229842AbiHaOkV (ORCPT ); Wed, 31 Aug 2022 10:40:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231936AbiHaOkT (ORCPT ); Wed, 31 Aug 2022 10:40:19 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1E496B178 for ; Wed, 31 Aug 2022 07:40:17 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id k6-20020a05600c1c8600b003a54ecc62f6so8121363wms.5 for ; Wed, 31 Aug 2022 07:40:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metanetworks.com; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc; bh=HWdOrGZSqpM8AqTLpomSymY1vHAqULXeHUWNRw9A/8g=; b=P2iV13CFymQy5eA08gqURDQUroxtF3kPunSryej9MlGDPctact306mum2MPAON1yT9 1lIU+FGjsF2apL57+Xnn81S7KwiCvTwRXEGOdtAH1fR7o9PZ2opETkGK9r7AE9Y6U1sx 8kc5LYQvfDviqEfAeRz+qmDqhvzcUyEA/ae/w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=HWdOrGZSqpM8AqTLpomSymY1vHAqULXeHUWNRw9A/8g=; b=T2/5TvUzJe9rlEhZuyL/s533DfRCikxQyswNu3jpJaqWDGdqX7nxhOvMs4eqGb67Md T3Hic2AL3gcdaJYh5e8wBuCZB1eYFc5Trw1JGrpxA1s6eD9e6r29sQGbSTvf8078fmI7 6fg1Z6Yov87pf/FgXTNeXR/jdPPlHEM0U/onub3/9x1w2plJF3t5lzXW7YFptlQoPsa4 A9e4w1AkGfH7iMXt+TIGyftafTb6s5ek1nCMLmJzQqD93KFP42b8JKiBBoAnSrJl3+H5 y+YBo5CwudEmKrGjSCdHHGqLAuJjjBTlidyIEpe71BveCzMQGpeRCQ8zPt0vBINYpihj LYRA== X-Gm-Message-State: ACgBeo3n4+Mxb1cDP0ZnVJmo9qmfzvLhDXtR0m0b6Hu4K1niSGxJkRii KBWxLA+0qDj8ZGJFGqZW4pxQMXCmaHhbNeZmVgJA8VAQuqVrViip7Ctjc/xI38kqTS2OGaV+zAI StdwkDl0yN2u8n2OpDJKbjTaqmwD+XhtDRHetBDd9NPbNuWRj7g9TyqKSUuHjruORhfaPRjBs X-Google-Smtp-Source: AA6agR5Hy41st5299M36ad0UzjkozueaOwqbQUR93R+/pD8p73TGp/Kl+CK5EKQyL+0J5SEJkt9QEQ== X-Received: by 2002:a05:600c:89a:b0:3a5:4ea9:d5ee with SMTP id l26-20020a05600c089a00b003a54ea9d5eemr2257299wmp.8.1661956816016; Wed, 31 Aug 2022 07:40:16 -0700 (PDT) Received: from blondie.home ([5.102.239.127]) by smtp.gmail.com with ESMTPSA id r9-20020a05600c424900b003a61306d79dsm2239315wmm.41.2022.08.31.07.40.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 07:40:15 -0700 (PDT) From: Shmulik Ladkani X-Google-Original-From: Shmulik Ladkani To: bpf@vger.kernel.org, Daniel Borkmann Cc: Alexei Starovoitov , Andrii Nakryiko , Shmulik Ladkani Subject: [PATCH bpf-next 1/2] bpf: Support getting tunnel flags Date: Wed, 31 Aug 2022 17:40:09 +0300 Message-Id: <20220831144010.174110-1-shmulik.ladkani@gmail.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Existing 'bpf_skb_get_tunnel_key' extracts various tunnel parameters (id, ttl, tos, local and remote) but does not expose ip_tunnel_info's tun_flags to the bpf program. It makes sense to expose tun_flags to the bpf program. Assume for example multiple GRE tunnels maintained on a single GRE interface in collect_md mode. The program expects origins to initiate over GRE, however different origins use different GRE characteristics (e.g. some prefer to use GRE checksum, some do not; some pass a GRE key, some do not, etc..). A bpf program getting tun_flags can therefore remember the relevant flags (e.g. TUNNEL_CSUM, TUNNEL_SEQ...) for each initiating remote. In the reply path, the program can use 'bpf_skb_set_tunnel_key' in order to correctly reply to the remote, using similar characteristics, based on the stored tunnel flags. Introduce BPF_F_TUNINFO_FLAGS flag for bpf_skb_get_tunnel_key. If specified, 'bpf_tunnel_key->tunnel_flags' is set with the tun_flags. Decided to use the existing unused 'tunnel_ext' as the storage for the 'tunnel_flags' in order to avoid changing bpf_tunnel_key's layout. Signed-off-by: Shmulik Ladkani --- include/uapi/linux/bpf.h | 10 +++++++++- net/core/filter.c | 8 ++++++-- tools/include/uapi/linux/bpf.h | 10 +++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 962960a98835..837c0f9b7fdd 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5659,6 +5659,11 @@ enum { BPF_F_SEQ_NUMBER = (1ULL << 3), }; +/* BPF_FUNC_skb_get_tunnel_key flags. */ +enum { + BPF_F_TUNINFO_FLAGS = (1ULL << 4), +}; + /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and * BPF_FUNC_perf_event_read_value flags. */ @@ -5848,7 +5853,10 @@ struct bpf_tunnel_key { }; __u8 tunnel_tos; __u8 tunnel_ttl; - __u16 tunnel_ext; /* Padding, future use. */ + union { + __u16 tunnel_ext; /* compat */ + __be16 tunnel_flags; + }; __u32 tunnel_label; union { __u32 local_ipv4; diff --git a/net/core/filter.c b/net/core/filter.c index 63e25d8ce501..74e2a4a0d747 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4488,7 +4488,8 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key void *to_orig = to; int err; - if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6)))) { + if (unlikely(!info || (flags & ~(BPF_F_TUNINFO_IPV6 | + BPF_F_TUNINFO_FLAGS)))) { err = -EINVAL; goto err_clear; } @@ -4520,7 +4521,10 @@ BPF_CALL_4(bpf_skb_get_tunnel_key, struct sk_buff *, skb, struct bpf_tunnel_key to->tunnel_id = be64_to_cpu(info->key.tun_id); to->tunnel_tos = info->key.tos; to->tunnel_ttl = info->key.ttl; - to->tunnel_ext = 0; + if (flags & BPF_F_TUNINFO_FLAGS) + to->tunnel_flags = info->key.tun_flags; + else + to->tunnel_ext = 0; if (flags & BPF_F_TUNINFO_IPV6) { memcpy(to->remote_ipv6, &info->key.u.ipv6.src, diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index f4ba82a1eace..793103b10eab 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5659,6 +5659,11 @@ enum { BPF_F_SEQ_NUMBER = (1ULL << 3), }; +/* BPF_FUNC_skb_get_tunnel_key flags. */ +enum { + BPF_F_TUNINFO_FLAGS = (1ULL << 4), +}; + /* BPF_FUNC_perf_event_output, BPF_FUNC_perf_event_read and * BPF_FUNC_perf_event_read_value flags. */ @@ -5848,7 +5853,10 @@ struct bpf_tunnel_key { }; __u8 tunnel_tos; __u8 tunnel_ttl; - __u16 tunnel_ext; /* Padding, future use. */ + union { + __u16 tunnel_ext; /* compat */ + __be16 tunnel_flags; + }; __u32 tunnel_label; union { __u32 local_ipv4; From patchwork Wed Aug 31 14:40:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shmulik Ladkani X-Patchwork-Id: 12960837 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA666ECAAD4 for ; Wed, 31 Aug 2022 14:40:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232009AbiHaOk2 (ORCPT ); Wed, 31 Aug 2022 10:40:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54182 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231500AbiHaOkY (ORCPT ); Wed, 31 Aug 2022 10:40:24 -0400 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5E742520B0 for ; Wed, 31 Aug 2022 07:40:23 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id w5so2387449wrn.12 for ; Wed, 31 Aug 2022 07:40:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metanetworks.com; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc; bh=HsDYXDQR7cqxDXDeDGTk8Q6RD+bOYzIbYBsibJWzoaQ=; b=cj5LiTAPzdSx/bHjPgCDEljdwX+/teIkS/VzPdAHMFpLoMzUNxbr5/v9Lpn+CuEqEm gNt71yATNv5WZ4WuTNNLSHksmmEoRj+nvJk/HON5rsaBOd7cuhv6aFT/1RewMh8cfvG4 j1nc+cQTuB8segu98Mcq7BgSGCPovyg/pKx1o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=HsDYXDQR7cqxDXDeDGTk8Q6RD+bOYzIbYBsibJWzoaQ=; b=HtZvP9j/yNSrTSGRceIrnLORkejEztDTIBuP0Pb6eSGjeOrbHQCvbqRkLVorM82uNR e34L8GI1I6jYptF1tHSBng78h59DSDgN75n5oR7hPyQ7s4GZEO6S7dC/Rk90/2bTbaBc 1cH/OCF2mT19VRjhsBM2FBC7CpyilLMUT6MXQRBhM/+lYMkLZlljLcrR8yK1Rou1S+Gp ITOfpPCxt9rJTr/kWiwJTewzDEdLeE33K5jmZiD4Xf24E6tLmyruFluKLIU+C3/po2hd qrknc8UHarSNWp/1O2I0PZDvaBQ4t8oJwp5IzsiPz5/hcAX0s0xompXV6uYQfwE0FSOq GAdQ== X-Gm-Message-State: ACgBeo0R8CGQ1vh69+oI+grVErCzLaT188UPm06WPdtUxTHB1bG2zKVN HVpqDwjjCUFfCpjhUhC8t5EWX4mnceyBSgOTmXtSVblutK9EMvKSdegJ8G27IX2Cv0DYUedRt1J HA1XtjZ8P3JZT/LNv/xAL79rwp/lGEBLxOV5W4guQ1sqzOATGU/5CfZB1KaoSgwJ7IHRzHoOa X-Google-Smtp-Source: AA6agR4U5RXNYonVdr9fHAUtHKQ20rhqwxdJKtXxOLDF6JtCdo11Phzpn4WHCikIoNxjfGRc+1nQjg== X-Received: by 2002:a05:6000:1ac8:b0:220:6af3:935d with SMTP id i8-20020a0560001ac800b002206af3935dmr12063449wry.549.1661956821101; Wed, 31 Aug 2022 07:40:21 -0700 (PDT) Received: from blondie.home ([5.102.239.127]) by smtp.gmail.com with ESMTPSA id r9-20020a05600c424900b003a61306d79dsm2239315wmm.41.2022.08.31.07.40.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 07:40:20 -0700 (PDT) From: Shmulik Ladkani X-Google-Original-From: Shmulik Ladkani To: bpf@vger.kernel.org, Daniel Borkmann Cc: Alexei Starovoitov , Andrii Nakryiko , Shmulik Ladkani Subject: [PATCH bpf-next 2/2] selftests/bpf: Amend test_tunnel to exercise BPF_F_TUNINFO_FLAGS Date: Wed, 31 Aug 2022 17:40:10 +0300 Message-Id: <20220831144010.174110-2-shmulik.ladkani@gmail.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220831144010.174110-1-shmulik.ladkani@gmail.com> References: <20220831144010.174110-1-shmulik.ladkani@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Get the tunnel flags in {ipv6}vxlan_get_tunnel_src and ensure they are aligned with tunnel params set at {ipv6}vxlan_set_tunnel_dst. Signed-off-by: Shmulik Ladkani --- .../selftests/bpf/progs/test_tunnel_kern.c | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c index df0673c4ecbe..98af55f0bcd3 100644 --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -386,7 +387,8 @@ int vxlan_get_tunnel_src(struct __sk_buff *skb) __u32 orig_daddr; __u32 index = 0; - ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); + ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), + BPF_F_TUNINFO_FLAGS); if (ret < 0) { log_err(ret); return TC_ACT_SHOT; @@ -398,10 +400,13 @@ int vxlan_get_tunnel_src(struct __sk_buff *skb) return TC_ACT_SHOT; } - if (key.local_ipv4 != ASSIGNED_ADDR_VETH1 || md.gbp != 0x800FF) { - bpf_printk("vxlan key %d local ip 0x%x remote ip 0x%x gbp 0x%x\n", + if (key.local_ipv4 != ASSIGNED_ADDR_VETH1 || md.gbp != 0x800FF || + !(key.tunnel_flags & TUNNEL_KEY) || + (key.tunnel_flags & TUNNEL_CSUM)) { + bpf_printk("vxlan key %d local ip 0x%x remote ip 0x%x gbp 0x%x flags 0x%x\n", key.tunnel_id, key.local_ipv4, - key.remote_ipv4, md.gbp); + key.remote_ipv4, md.gbp, + bpf_ntohs(key.tunnel_flags)); log_err(ret); return TC_ACT_SHOT; } @@ -541,16 +546,19 @@ int ip6vxlan_get_tunnel_src(struct __sk_buff *skb) } ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), - BPF_F_TUNINFO_IPV6); + BPF_F_TUNINFO_IPV6 | BPF_F_TUNINFO_FLAGS); if (ret < 0) { log_err(ret); return TC_ACT_SHOT; } - if (bpf_ntohl(key.local_ipv6[3]) != *local_ip) { - bpf_printk("ip6vxlan key %d local ip6 ::%x remote ip6 ::%x label 0x%x\n", + if (bpf_ntohl(key.local_ipv6[3]) != *local_ip || + !(key.tunnel_flags & TUNNEL_KEY) || + !(key.tunnel_flags & TUNNEL_CSUM)) { + bpf_printk("ip6vxlan key %d local ip6 ::%x remote ip6 ::%x label 0x%x flags 0x%x\n", key.tunnel_id, bpf_ntohl(key.local_ipv6[3]), - bpf_ntohl(key.remote_ipv6[3]), key.tunnel_label); + bpf_ntohl(key.remote_ipv6[3]), key.tunnel_label, + bpf_ntohs(key.tunnel_flags)); bpf_printk("local_ip 0x%x\n", *local_ip); log_err(ret); return TC_ACT_SHOT;