From patchwork Sun Sep 11 12:23:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shmulik Ladkani X-Patchwork-Id: 12972888 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 C7529ECAAD3 for ; Sun, 11 Sep 2022 12:23:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230306AbiIKMXr (ORCPT ); Sun, 11 Sep 2022 08:23:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230320AbiIKMXp (ORCPT ); Sun, 11 Sep 2022 08:23:45 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 68A8332A83 for ; Sun, 11 Sep 2022 05:23:43 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id t14so11112773wrx.8 for ; Sun, 11 Sep 2022 05:23:43 -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:subject:date; bh=IhZXehw/ImJWDR1pHWu/KcL7l+DWpcFNtpoN+2rRINY=; b=alcYrBaO6YZ9kYOxaG1bGuSMph2xv4yGyRwSh8gHBT9ef7GNt6TCRoTa1YdnAyJZEJ JhpKAhb9+l1rA5ty3o7GjdehOlR3gRxZ2QGU/UJNI86mTkfll2q5eo1kRwRaxawjwVUr QiPQ1NbDy+JpzmgUykfP2LQys9pu1ncC4nJ58= 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 :subject:date; bh=IhZXehw/ImJWDR1pHWu/KcL7l+DWpcFNtpoN+2rRINY=; b=nFEC+chZNBZ3ue1LxdVlECBWaymJtSkPw3wz3qp3sE4XURCt5QoY1im9RMPfOhZTuC JAcJzBgtn0H/8jDrt+Q/2FM4ZqI6Jf4D3L2TTevF3pB0TVid6u2HdFUZWGVzrGsDyxCN QXampAiCrEFaUjGuSr84gwsGj9/5b1DhItnOy8X3SdE3ORLxReUnMp7foH6P9eDHFtC5 x89R0Vv0EDbPwRogqK27w8PVzFckvqrZrevv8X3Em3hAgYaKSTjahpFF0rIP4bK62+sb wvwsvJB8E/rliGC29kJfzqNrhCw7J1UzgU4IFsKC3aTxjekh1SQFyeMVbsqAGRH6tM4M wuCQ== X-Gm-Message-State: ACgBeo34+XTYmijKya1lAZrG/ZuEzBiG8qcr4jW/lCl0c6GvZl7g+3RW sSz3DWb/GAslKtpyTGDOTZNKqkZnTeNwznCeRfzM5r1WtdHlSDs538aSKnwtgt/MGMF4t8nTVj9 u8yvqReOICY3qmP4k376LhoWrs4JSllnW8Gn2DW1Mk6i4LIPmlXXgdZctZK3Y3bHBPT43ZETmmn c= X-Google-Smtp-Source: AA6agR6FnkWpd4IRsSQYfKMM07I2cK/CyaleEvbeoh/KDbAAqAbGTWIqUuY4S4p/Gth7lAa8Uu1RiQ== X-Received: by 2002:a5d:5c08:0:b0:228:e139:43f3 with SMTP id cc8-20020a5d5c08000000b00228e13943f3mr12624277wrb.396.1662899021476; Sun, 11 Sep 2022 05:23:41 -0700 (PDT) Received: from blondie.home ([141.226.162.95]) by smtp.gmail.com with ESMTPSA id r15-20020a05600c35cf00b003a4f08495b7sm6538346wmq.34.2022.09.11.05.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 05:23:41 -0700 (PDT) From: Shmulik Ladkani X-Google-Original-From: Shmulik Ladkani To: bpf@vger.kernel.org, Daniel Borkmann , John Fastabend , Joanne Koong , Andrii Nakryiko Cc: Alexei Starovoitov , Paul Chaignon , Shmulik Ladkani Subject: [PATCH v7 bpf-next 4/4] selftests/bpf: Add geneve with bpf_skb_set_tunnel_opt_dynptr test-case to test_progs Date: Sun, 11 Sep 2022 15:23:28 +0300 Message-Id: <20220911122328.306188-5-shmulik.ladkani@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220911122328.306188-1-shmulik.ladkani@gmail.com> References: <20220911122328.306188-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 Add geneve test to test_tunnel. The test setup and scheme resembles the existing vxlan test. The test also exercises tunnel option assignment using bpf_skb_set_tunnel_opt_dynptr. Signed-off-by: Shmulik Ladkani --- v6: - Fix missing retcodes in progs/test_tunnel_kern.c spotted by John Fastabend - Simplify bpf_skb_set_tunnel_opt_dynptr's interface, removing the superfluous 'len' parameter suggested by Andrii Nakryiko --- .../selftests/bpf/prog_tests/test_tunnel.c | 108 ++++++++++++++ .../selftests/bpf/progs/test_tunnel_kern.c | 138 ++++++++++++++++++ 2 files changed, 246 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index 852da04ff281..9aae03c720e9 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c @@ -87,6 +87,8 @@ #define VXLAN_TUNL_DEV1 "vxlan11" #define IP6VXLAN_TUNL_DEV0 "ip6vxlan00" #define IP6VXLAN_TUNL_DEV1 "ip6vxlan11" +#define GENEVE_TUNL_DEV0 "geneve00" +#define GENEVE_TUNL_DEV1 "geneve11" #define PING_ARGS "-i 0.01 -c 3 -w 10 -q" @@ -133,6 +135,38 @@ static void cleanup(void) SYS_NOFAIL("ip rule del to %s table 20 2> /dev/null", IP4_ADDR2_VETH1); SYS_NOFAIL("ip link del %s 2> /dev/null", VXLAN_TUNL_DEV1); SYS_NOFAIL("ip link del %s 2> /dev/null", IP6VXLAN_TUNL_DEV1); + SYS_NOFAIL("ip link del %s 2> /dev/null", GENEVE_TUNL_DEV1); +} + +static int add_geneve_tunnel(void) +{ + /* at_ns0 namespace */ + SYS("ip netns exec at_ns0 ip link add dev %s type geneve external", + GENEVE_TUNL_DEV0); + SYS("ip netns exec at_ns0 ip link set dev %s address %s up", + GENEVE_TUNL_DEV0, MAC_TUNL_DEV0); + SYS("ip netns exec at_ns0 ip addr add dev %s %s/24", + GENEVE_TUNL_DEV0, IP4_ADDR_TUNL_DEV0); + SYS("ip netns exec at_ns0 ip neigh add %s lladdr %s dev %s", + IP4_ADDR_TUNL_DEV1, MAC_TUNL_DEV1, GENEVE_TUNL_DEV0); + + /* root namespace */ + SYS("ip link add dev %s type geneve external", GENEVE_TUNL_DEV1); + SYS("ip link set dev %s address %s up", GENEVE_TUNL_DEV1, MAC_TUNL_DEV1); + SYS("ip addr add dev %s %s/24", GENEVE_TUNL_DEV1, IP4_ADDR_TUNL_DEV1); + SYS("ip neigh add %s lladdr %s dev %s", + IP4_ADDR_TUNL_DEV0, MAC_TUNL_DEV0, GENEVE_TUNL_DEV1); + + return 0; +fail: + return -1; +} + +static void delete_geneve_tunnel(void) +{ + SYS_NOFAIL("ip netns exec at_ns0 ip link delete dev %s", + GENEVE_TUNL_DEV0); + SYS_NOFAIL("ip link delete dev %s", GENEVE_TUNL_DEV1); } static int add_vxlan_tunnel(void) @@ -248,6 +282,79 @@ static int attach_tc_prog(struct bpf_tc_hook *hook, int igr_fd, int egr_fd) return 0; } +static void test_geneve_tunnel(void) +{ + struct test_tunnel_kern *skel = NULL; + struct nstoken *nstoken; + int local_ip_map_fd = -1; + int set_src_prog_fd, get_src_prog_fd; + int set_dst_prog_fd; + int key = 0, ifindex = -1; + uint local_ip; + int err; + DECLARE_LIBBPF_OPTS(bpf_tc_hook, tc_hook, + .attach_point = BPF_TC_INGRESS); + + /* add genve tunnel */ + err = add_geneve_tunnel(); + if (!ASSERT_OK(err, "add geneve tunnel")) + goto done; + + /* load and attach bpf prog to tunnel dev tc hook point */ + skel = test_tunnel_kern__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_tunnel_kern__open_and_load")) + goto done; + ifindex = if_nametoindex(GENEVE_TUNL_DEV1); + if (!ASSERT_NEQ(ifindex, 0, "geneve11 ifindex")) + goto done; + tc_hook.ifindex = ifindex; + get_src_prog_fd = bpf_program__fd(skel->progs.geneve_get_tunnel_src); + set_src_prog_fd = bpf_program__fd(skel->progs.geneve_set_tunnel_src); + if (!ASSERT_GE(get_src_prog_fd, 0, "bpf_program__fd")) + goto done; + if (!ASSERT_GE(set_src_prog_fd, 0, "bpf_program__fd")) + goto done; + if (attach_tc_prog(&tc_hook, get_src_prog_fd, set_src_prog_fd)) + goto done; + + /* load and attach prog set_md to tunnel dev tc hook point at_ns0 */ + nstoken = open_netns("at_ns0"); + if (!ASSERT_OK_PTR(nstoken, "setns src")) + goto done; + ifindex = if_nametoindex(GENEVE_TUNL_DEV0); + if (!ASSERT_NEQ(ifindex, 0, "geneve00 ifindex")) + goto done; + tc_hook.ifindex = ifindex; + set_dst_prog_fd = bpf_program__fd(skel->progs.geneve_set_tunnel_dst); + if (!ASSERT_GE(set_dst_prog_fd, 0, "bpf_program__fd")) + goto done; + if (attach_tc_prog(&tc_hook, -1, set_dst_prog_fd)) + goto done; + close_netns(nstoken); + + /* use veth1 ip 1 as tunnel source ip */ + local_ip_map_fd = bpf_map__fd(skel->maps.local_ip_map); + if (!ASSERT_GE(local_ip_map_fd, 0, "bpf_map__fd")) + goto done; + local_ip = IP4_ADDR1_HEX_VETH1; + err = bpf_map_update_elem(local_ip_map_fd, &key, &local_ip, BPF_ANY); + if (!ASSERT_OK(err, "update bpf local_ip_map")) + goto done; + + /* ping test */ + err = test_ping(AF_INET, IP4_ADDR_TUNL_DEV0); + if (!ASSERT_OK(err, "test_ping")) + goto done; + +done: + /* delete geneve tunnel */ + delete_geneve_tunnel(); + if (local_ip_map_fd >= 0) + close(local_ip_map_fd); + if (skel) + test_tunnel_kern__destroy(skel); +} + static void test_vxlan_tunnel(void) { struct test_tunnel_kern *skel = NULL; @@ -408,6 +515,7 @@ static void *test_tunnel_run_tests(void *arg) RUN_TEST(vxlan_tunnel); RUN_TEST(ip6vxlan_tunnel); + RUN_TEST(geneve_tunnel); cleanup(); diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c index b11f6952b0c8..cb901b76a547 100644 --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c @@ -24,6 +24,20 @@ #define log_err(__ret) bpf_printk("ERROR line:%d ret:%d\n", __LINE__, __ret) +#define GENEVE_OPTS_LEN0 12 +#define GENEVE_OPTS_LEN1 20 + +struct tun_opts_raw { + __u8 data[64]; +}; + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_ARRAY); + __uint(max_entries, 1); + __type(key, __u32); + __type(value, struct tun_opts_raw); +} geneve_opts SEC(".maps"); + struct geneve_opt { __be16 opt_class; __u8 type; @@ -286,6 +300,130 @@ int ip4ip6erspan_get_tunnel(struct __sk_buff *skb) return TC_ACT_OK; } +SEC("tc") +int geneve_set_tunnel_dst(struct __sk_buff *skb) +{ + int ret; + struct bpf_tunnel_key key; + struct tun_opts_raw *opts; + struct bpf_dynptr dptr; + __u32 index = 0; + __u32 *local_ip = NULL; + + local_ip = bpf_map_lookup_elem(&local_ip_map, &index); + if (!local_ip) { + log_err(-1); + return TC_ACT_SHOT; + } + + index = 0; + opts = bpf_map_lookup_elem(&geneve_opts, &index); + if (!opts) { + log_err(-1); + return TC_ACT_SHOT; + } + + __builtin_memset(&key, 0x0, sizeof(key)); + key.local_ipv4 = 0xac100164; /* 172.16.1.100 */ + key.remote_ipv4 = *local_ip; + key.tunnel_id = 2; + key.tunnel_tos = 0; + key.tunnel_ttl = 64; + + ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), + BPF_F_ZERO_CSUM_TX); + if (ret < 0) { + log_err(ret); + return TC_ACT_SHOT; + } + + /* set empty geneve options (of runtime length) using a dynptr */ + __builtin_memset(opts, 0x0, sizeof(*opts)); + if (*local_ip % 2) + bpf_dynptr_from_mem(opts, GENEVE_OPTS_LEN1, 0, &dptr); + else + bpf_dynptr_from_mem(opts, GENEVE_OPTS_LEN0, 0, &dptr); + ret = bpf_skb_set_tunnel_opt_dynptr(skb, &dptr); + if (ret < 0) { + log_err(ret); + return TC_ACT_SHOT; + } + + return TC_ACT_OK; +} + +SEC("tc") +int geneve_set_tunnel_src(struct __sk_buff *skb) +{ + int ret; + struct bpf_tunnel_key key; + __u32 index = 0; + __u32 *local_ip = NULL; + + local_ip = bpf_map_lookup_elem(&local_ip_map, &index); + if (!local_ip) { + log_err(-1); + return TC_ACT_SHOT; + } + + __builtin_memset(&key, 0x0, sizeof(key)); + key.local_ipv4 = *local_ip; + key.remote_ipv4 = 0xac100164; /* 172.16.1.100 */ + key.tunnel_id = 2; + key.tunnel_tos = 0; + key.tunnel_ttl = 64; + + ret = bpf_skb_set_tunnel_key(skb, &key, sizeof(key), + BPF_F_ZERO_CSUM_TX); + if (ret < 0) { + log_err(ret); + return TC_ACT_SHOT; + } + + return TC_ACT_OK; +} + +SEC("tc") +int geneve_get_tunnel_src(struct __sk_buff *skb) +{ + int ret; + struct bpf_tunnel_key key; + struct tun_opts_raw opts; + int expected_opts_len; + __u32 index = 0; + __u32 *local_ip = NULL; + + local_ip = bpf_map_lookup_elem(&local_ip_map, &index); + if (!local_ip) { + log_err(-1); + return TC_ACT_SHOT; + } + + ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); + if (ret < 0) { + log_err(ret); + return TC_ACT_SHOT; + } + + ret = bpf_skb_get_tunnel_opt(skb, &opts, sizeof(opts)); + if (ret < 0) { + log_err(ret); + return TC_ACT_SHOT; + } + + expected_opts_len = *local_ip % 2 ? GENEVE_OPTS_LEN1 : GENEVE_OPTS_LEN0; + if (key.local_ipv4 != *local_ip || ret != expected_opts_len) { + bpf_printk("geneve key %d local ip 0x%x remote ip 0x%x opts_len %d\n", + key.tunnel_id, key.local_ipv4, + key.remote_ipv4, ret); + bpf_printk("local_ip 0x%x\n", *local_ip); + log_err(ret); + return TC_ACT_SHOT; + } + + return TC_ACT_OK; +} + SEC("tc") int vxlan_set_tunnel_dst(struct __sk_buff *skb) {