From patchwork Sun Sep 11 08:46:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shmulik Ladkani X-Patchwork-Id: 12972787 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 D4FF3C6FA83 for ; Sun, 11 Sep 2022 08:46:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229914AbiIKIq2 (ORCPT ); Sun, 11 Sep 2022 04:46:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229955AbiIKIq1 (ORCPT ); Sun, 11 Sep 2022 04:46:27 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 431323207C for ; Sun, 11 Sep 2022 01:46:25 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id nc14so13687520ejc.4 for ; Sun, 11 Sep 2022 01:46:25 -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=PxBIDlOz/oNNEmNNYrXSw2RO4vH4JKthBQ66hiIBTxw=; b=KWMW/7/XpOgmRjDVVKc10iVmH3hbsocWBHpv0dO+nriso3MJdn+f9FaajmELYRnPes WVF9mgQ+1HTNOM1SQoEIk8d+A8/mMrLj0MYDar/NL40EqphmWOD6rKEOAznoGPHW0Wyj nJZ43Oh4Bh75TPC3mV5JjwZ4SKQ76QtlLMGYY= 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=PxBIDlOz/oNNEmNNYrXSw2RO4vH4JKthBQ66hiIBTxw=; b=2RXdYkE5WSbfz/CzTt6Q/XERMZvB+H9J9jCyZ52McHq9uGdLNhDBNB3uj0dAyTrNt/ OpZgNfBgAt4aPQ/Dpyg6yqwSqt7nePEZlvLZEq6MNShPNZgr5YgVnH9Q1cJoRMpfzhCc cYAsfDNssxQ/BYT6M/+Sq5aX3xdH0X4zpkC/XNhY8Os/OBKIZbsspomdTgNhRGl6+FP7 4SpP1H7mJqseM1L6V/a78RmcApC6TJf/HYjjXkIW/nBO8FD/UEfECi/a/TEpslxotIV/ EehbMVzWOVsE6JzQYTB9z1v3hb2QysvJVHI38a0xy+WrT+vegf1hZl87MT8ZEsCKxnA0 g09Q== X-Gm-Message-State: ACgBeo08pgo5Fesu5Kc6GKy4B+7lgeO7sDr4+DIJV8VNaZaEYcGCWWKA hpCe/nsCxQhMbGAyipUXxHyfbQiR6SAwniiDreKMvHMXBb5wjzuK7QcH1ZVsGupm7bBY2KxsqaS zCJv4bcJpCvOX5zv97N3pnkdCz3joSH+GKDBunzE+v/31w314MrTfRzPhNTBmMWlFidoCkzm91Z Q= X-Google-Smtp-Source: AA6agR5D+joSmhtU3dTKsQZpZdQEfHyY6hHxnY7QezbKXNygiFSfqp0zNLS1Vt5DKkK/IYgMuDz2VA== X-Received: by 2002:a17:906:fd8a:b0:75d:c79a:47c8 with SMTP id xa10-20020a170906fd8a00b0075dc79a47c8mr14694815ejb.389.1662885983510; Sun, 11 Sep 2022 01:46:23 -0700 (PDT) Received: from localhost.localdomain ([141.226.162.95]) by smtp.gmail.com with ESMTPSA id q10-20020a170906360a00b007309a570bacsm2713591ejb.176.2022.09.11.01.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 01:46:23 -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 v6 bpf-next 1/4] bpf: Export 'bpf_dynptr_get_data, bpf_dynptr_get_size' helpers Date: Sun, 11 Sep 2022 11:46:06 +0300 Message-Id: <20220911084609.102519-2-shmulik.ladkani@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220911084609.102519-1-shmulik.ladkani@gmail.com> References: <20220911084609.102519-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 This allows kernel code dealing with dynptrs obtain dynptr's available size and current (w. proper offset) data pointer. Signed-off-by: Shmulik Ladkani --- v5: - fix bpf_dynptr_get_data's incorrect usage of bpf_dynptr_kern's size spotted by Joanne Koong v6: - Simplify bpf_dynptr_get_data's interface and make it inline suggested by John Fastabend --- include/linux/bpf.h | 6 ++++++ kernel/bpf/helpers.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 48ae05099f36..e0844f45022f 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2631,6 +2631,12 @@ void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data, enum bpf_dynptr_type type, u32 offset, u32 size); void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr); int bpf_dynptr_check_size(u32 size); +u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr); + +static inline void *bpf_dynptr_get_data(struct bpf_dynptr_kern *ptr) +{ + return ptr->data ? ptr->data + ptr->offset : NULL; +} #ifdef CONFIG_BPF_LSM void bpf_cgroup_atype_get(u32 attach_btf_id, int cgroup_atype); diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index fc08035f14ed..824864ac82d1 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -1408,7 +1408,7 @@ static void bpf_dynptr_set_type(struct bpf_dynptr_kern *ptr, enum bpf_dynptr_typ ptr->size |= type << DYNPTR_TYPE_SHIFT; } -static u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr) +u32 bpf_dynptr_get_size(struct bpf_dynptr_kern *ptr) { return ptr->size & DYNPTR_SIZE_MASK; } From patchwork Sun Sep 11 08:46:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shmulik Ladkani X-Patchwork-Id: 12972788 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 018D5ECAAA1 for ; Sun, 11 Sep 2022 08:46:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229952AbiIKIqg (ORCPT ); Sun, 11 Sep 2022 04:46:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbiIKIqf (ORCPT ); Sun, 11 Sep 2022 04:46:35 -0400 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6269D326FC for ; Sun, 11 Sep 2022 01:46:34 -0700 (PDT) Received: by mail-ej1-x631.google.com with SMTP id go34so13679940ejc.2 for ; Sun, 11 Sep 2022 01:46:34 -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=cFumHqq0pLb6AlXpm3FchEhhiFTYVLSEGo+Z3jKzfGY=; b=eekVlLfCLAKr7KAH680LfD+MaG7H7witq3vAK19/h4gqycLB3euaKQEWpusKzMcTZq UXSOF+9GDuY65yflcrI5ZiLOD+f0Txm9hcL/9gRB1MdNlPTKSRUrNG7hUNckWwBGg+V5 FgG/qJzzRMbDn/eMFdXw5h+gHmgpXHaWEPg5k= 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=cFumHqq0pLb6AlXpm3FchEhhiFTYVLSEGo+Z3jKzfGY=; b=cpInLRi0HWsgeXSzdA+M7j4KW1nX9uzU/MZYN3M30hPsWiaijsvG5BtIMJXmiTliYr vU3/nxYVB26JsXTffnOrsx+LfZQt4byh4UyOt7VenNOEiR9ZT/K8O5cYj8Y0J/zFB82c adi14U0MC8ZWQq1v1X4lqsmR6FRb1hzfNyQ4abYN+KbIp8xewoemU3JF2sq8sqJTGp8Q 8SscBXXDUcL1c7bAQRZ4s1JP96HYJcNP5cJ8aRBxDZjY3TsbHVf/HmJtw2I3KAxMxXm5 jEfFjXKr3M2LAUlwab/KPadDa1R8WZuCcHsuCVP3nJQvZp7aObnHKJT9Ubweyqc5rK8k EmUg== X-Gm-Message-State: ACgBeo02wNT4zO/ayp1PAyzZ9eHBW8q9fPiAAH8JhAKAbKevbkzmr89a CEHyPD+s8oXwq2+gd04TxZAD1XgXpb5yPT0iz7RUT1tI7Z3jlysroyT+AKZ7697ikVoQiuhxWFd 51+tlCbeSLLnRXFrWW9hadvgYIth9yYTnvj4+3D9TBLwABGdQ6Tzk6mrDVImu55B81NC3HPPegI w= X-Google-Smtp-Source: AA6agR7xKDGBgs3XbZP61oAPUQ8TxmvzSHJrLMekf3JpU4nP/Wgbad6sxQc2uWSrqsH3qB1eoqsdEA== X-Received: by 2002:a17:907:869e:b0:74f:2465:82a8 with SMTP id qa30-20020a170907869e00b0074f246582a8mr15527275ejc.729.1662885992521; Sun, 11 Sep 2022 01:46:32 -0700 (PDT) Received: from localhost.localdomain ([141.226.162.95]) by smtp.gmail.com with ESMTPSA id q10-20020a170906360a00b007309a570bacsm2713591ejb.176.2022.09.11.01.46.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 01:46:32 -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 v6 bpf-next 2/4] bpf: Support setting variable-length tunnel options Date: Sun, 11 Sep 2022 11:46:07 +0300 Message-Id: <20220911084609.102519-3-shmulik.ladkani@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220911084609.102519-1-shmulik.ladkani@gmail.com> References: <20220911084609.102519-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 Existing 'bpf_skb_set_tunnel_opt' allows setting tunnel options given an option buffer (ARG_PTR_TO_MEM) and the compile-time fixed buffer size (ARG_CONST_SIZE). However, in certain cases we wish to set tunnel options of dynamic length. For example, we have an ebpf program that gets geneve options on incoming packets, stores them into a map (using a key representing the incoming flow), and later needs to assign *same* options to reply packets (belonging to same flow). This is currently imposssible without knowing sender's exact geneve options length, which unfortunately is dymamic. Introduce 'bpf_skb_set_tunnel_opt_dynptr'. This is a variant of 'bpf_skb_set_tunnel_opt' which gets a bpf dynamic pointer (ARG_PTR_TO_DYNPTR) parameter whose data points to the options buffer to set. Signed-off-by: Shmulik Ladkani Reported-by: kernel test robot Reported-by: kernel test robot --- v3: Avoid 'inline' for the __bpf_skb_set_tunopt helper function v4: change API to be based on bpf_dynptr, suggested by John Fastabend v6: Remove superfluous 'len' from bpf_skb_set_tunnel_opt_dynptr API (rely on dynptr's internal size), suggested by Andrii Nakryiko --- include/uapi/linux/bpf.h | 11 +++++++++++ net/core/filter.c | 31 +++++++++++++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 11 +++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3df78c56c1bf..ba12f7e1ccb6 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5387,6 +5387,16 @@ union bpf_attr { * Return * Current *ktime*. * + * long bpf_skb_set_tunnel_opt_dynptr(struct sk_buff *skb, struct bpf_dynptr *opt) + * Description + * Set tunnel options metadata for the packet associated to *skb* + * to the option data pointed to by the *opt* dynptr. + * + * See also the description of the **bpf_skb_get_tunnel_opt**\ () + * helper for additional information. + * Return + * 0 on success, or a negative error in case of failure. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5598,6 +5608,7 @@ union bpf_attr { FN(tcp_raw_check_syncookie_ipv4), \ FN(tcp_raw_check_syncookie_ipv6), \ FN(ktime_get_tai_ns), \ + FN(skb_set_tunnel_opt_dynptr), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper diff --git a/net/core/filter.c b/net/core/filter.c index e872f45399b0..1c652936ef86 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4674,8 +4674,7 @@ static const struct bpf_func_proto bpf_skb_set_tunnel_key_proto = { .arg4_type = ARG_ANYTHING, }; -BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, - const u8 *, from, u32, size) +static u64 __bpf_skb_set_tunopt(struct sk_buff *skb, const u8 *from, u32 size) { struct ip_tunnel_info *info = skb_tunnel_info(skb); const struct metadata_dst *md = this_cpu_ptr(md_dst); @@ -4690,6 +4689,22 @@ BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, return 0; } +BPF_CALL_3(bpf_skb_set_tunnel_opt, struct sk_buff *, skb, + const u8 *, from, u32, size) +{ + return __bpf_skb_set_tunopt(skb, from, size); +} + +BPF_CALL_2(bpf_skb_set_tunnel_opt_dynptr, struct sk_buff *, skb, + struct bpf_dynptr_kern *, ptr) +{ + const u8 *from = bpf_dynptr_get_data(ptr); + + if (unlikely(!from)) + return -EFAULT; + return __bpf_skb_set_tunopt(skb, from, bpf_dynptr_get_size(ptr)); +} + static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { .func = bpf_skb_set_tunnel_opt, .gpl_only = false, @@ -4699,6 +4714,14 @@ static const struct bpf_func_proto bpf_skb_set_tunnel_opt_proto = { .arg3_type = ARG_CONST_SIZE, }; +static const struct bpf_func_proto bpf_skb_set_tunnel_opt_dynptr_proto = { + .func = bpf_skb_set_tunnel_opt_dynptr, + .gpl_only = false, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL, +}; + static const struct bpf_func_proto * bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) { @@ -4719,6 +4742,8 @@ bpf_get_skb_set_tunnel_proto(enum bpf_func_id which) return &bpf_skb_set_tunnel_key_proto; case BPF_FUNC_skb_set_tunnel_opt: return &bpf_skb_set_tunnel_opt_proto; + case BPF_FUNC_skb_set_tunnel_opt_dynptr: + return &bpf_skb_set_tunnel_opt_dynptr_proto; default: return NULL; } @@ -7798,6 +7823,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_skb_get_tunnel_opt: return &bpf_skb_get_tunnel_opt_proto; case BPF_FUNC_skb_set_tunnel_opt: + case BPF_FUNC_skb_set_tunnel_opt_dynptr: return bpf_get_skb_set_tunnel_proto(func_id); case BPF_FUNC_redirect: return &bpf_redirect_proto; @@ -8145,6 +8171,7 @@ lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_skb_get_tunnel_opt: return &bpf_skb_get_tunnel_opt_proto; case BPF_FUNC_skb_set_tunnel_opt: + case BPF_FUNC_skb_set_tunnel_opt_dynptr: return bpf_get_skb_set_tunnel_proto(func_id); case BPF_FUNC_redirect: return &bpf_redirect_proto; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 3df78c56c1bf..ba12f7e1ccb6 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5387,6 +5387,16 @@ union bpf_attr { * Return * Current *ktime*. * + * long bpf_skb_set_tunnel_opt_dynptr(struct sk_buff *skb, struct bpf_dynptr *opt) + * Description + * Set tunnel options metadata for the packet associated to *skb* + * to the option data pointed to by the *opt* dynptr. + * + * See also the description of the **bpf_skb_get_tunnel_opt**\ () + * helper for additional information. + * Return + * 0 on success, or a negative error in case of failure. + * */ #define __BPF_FUNC_MAPPER(FN) \ FN(unspec), \ @@ -5598,6 +5608,7 @@ union bpf_attr { FN(tcp_raw_check_syncookie_ipv4), \ FN(tcp_raw_check_syncookie_ipv6), \ FN(ktime_get_tai_ns), \ + FN(skb_set_tunnel_opt_dynptr), \ /* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper From patchwork Sun Sep 11 08:46:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shmulik Ladkani X-Patchwork-Id: 12972789 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 785F6C6FA83 for ; Sun, 11 Sep 2022 08:46:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229949AbiIKIqn (ORCPT ); Sun, 11 Sep 2022 04:46:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229960AbiIKIql (ORCPT ); Sun, 11 Sep 2022 04:46:41 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4F33D326D5 for ; Sun, 11 Sep 2022 01:46:40 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id e17so8676245edc.5 for ; Sun, 11 Sep 2022 01:46:40 -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=C0Cxfwo+4e+ciAZAtlV9+PcqkeDXxPF0tLqBHzop+MQ=; b=GooHSJuzswHsfcXxje+ofFJArohoQcyzSzniz7YCm6lOJLWvHfrmrQbaI+AyIruQ5q N2s1PVO29pifJZle/44uncb797sYwu2Lm6eqs1U70CkZCbqt4pTgT2UFW6tJiLmo6e32 BcLHT0ypfr83lO43QxSk1T+KBiHUqCkv0Vvio= 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=C0Cxfwo+4e+ciAZAtlV9+PcqkeDXxPF0tLqBHzop+MQ=; b=F8M+BahNqGysvVwH5bwAL/dkwL+Hp9hjbaiKDpZbruMuYjiNMUDum/eawox/iVe0Sh KaOomZIar/23YEYe0oiFZ2k+H5oVfOxMX7N76UlJE9Ev34wvm2WSLIkEc4if8KXefizS NH1MSJhlWEk3Isg+OVjMhKilmf2OIZHU8uSj1S1FmtbTeEWI1N5Iaa8I40YuMc4Zga7Y JRQgUE/dbINGqc1gaoHtthQTyQK6zyT6jyu7bVcLevSwFLR61EyAXmH5PA0dFoCb58uy wkmtF33P8jAHtnkJft6zRF+apIseTwyXS0fHzolBXwixuiyLJNgm0cZDl3VEQQ4rde+Y ihog== X-Gm-Message-State: ACgBeo3uZjyBVZ+5HBvP86Hfnp9/jwePWcK3wyx2B8H0lERkwIKMDdPQ AXVfs2kSos3bypklDt2ZNN/MuOx3bWvRhvaZ3r8LZ7rvT9GP+RmNxVSQWe3FXAEk5yIauwYt27Y XLH2dahFzJvBgMJdoa+rwTjElV/2PJRwt7gDotAP/HRisONoIuGYtgm3ZYdXEi4aPfYb8IHjZLW Y= X-Google-Smtp-Source: AA6agR7ScFf0opiNSyqUFs+2kbXDGu3GieqV9nz+AHLQJ7fkB8f7zQ5XU56H9cmebQP/v37bTvexsQ== X-Received: by 2002:a05:6402:5ca:b0:445:c80a:3c2 with SMTP id n10-20020a05640205ca00b00445c80a03c2mr18034858edx.247.1662885998479; Sun, 11 Sep 2022 01:46:38 -0700 (PDT) Received: from localhost.localdomain ([141.226.162.95]) by smtp.gmail.com with ESMTPSA id q10-20020a170906360a00b007309a570bacsm2713591ejb.176.2022.09.11.01.46.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 01:46:38 -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 v6 bpf-next 3/4] selftests/bpf: Simplify test_tunnel setup for allowing non-local tunnel traffic Date: Sun, 11 Sep 2022 11:46:08 +0300 Message-Id: <20220911084609.102519-4-shmulik.ladkani@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220911084609.102519-1-shmulik.ladkani@gmail.com> References: <20220911084609.102519-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 Commit 1115169f47ae ("selftests/bpf: Don't assign outer source IP to host") removed the secondary IP (IP4_ADDR2_VETH1) assigned to veth1, in order to test bpf_skb_set_tunnel_key's functionality when tunnel destination isn't assigned to an interface. The chosen setup for testing the "tunnel to unassigned outer IP" scenario was rather complex: (1) static ARP entries in order to bypass ARP (o/w requests will fail as the target address isn't assigned locally), and (2) a BPF program running on veth1 ingress which manipulates the IP header's daddr to the actual IP assigned to the interface (o/w tunnel traffic won't be accepted locally). This is complex, and adds a dependency on this hidden "dnat"-like eBPF program, that needs to be replicated when new tunnel tests are added. Instead, we can have a much simpler setup: Add the secondary IP as a *local route* in a table pointed by a custom fib rule. No static arp entries are needed, and the special eBPF program that "dnats" the outer destination can be removed. This commit is a revert of 1115169f47ae, with the addition of the local route of IP4_ADDR2_VETH1 (instead of the original address assignment). Signed-off-by: Shmulik Ladkani --- v2: Place the local route for the secondary IP in a custom table pointed by a custom fib rule; this ensures the IP is not considered assigned to a device. --- .../selftests/bpf/prog_tests/test_tunnel.c | 23 ++---- .../selftests/bpf/progs/test_tunnel_kern.c | 80 +++---------------- 2 files changed, 17 insertions(+), 86 deletions(-) diff --git a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c index eea274110267..852da04ff281 100644 --- a/tools/testing/selftests/bpf/prog_tests/test_tunnel.c +++ b/tools/testing/selftests/bpf/prog_tests/test_tunnel.c @@ -82,7 +82,6 @@ #define MAC_TUNL_DEV0 "52:54:00:d9:01:00" #define MAC_TUNL_DEV1 "52:54:00:d9:02:00" -#define MAC_VETH1 "52:54:00:d9:03:00" #define VXLAN_TUNL_DEV0 "vxlan00" #define VXLAN_TUNL_DEV1 "vxlan11" @@ -109,9 +108,15 @@ static int config_device(void) { SYS("ip netns add at_ns0"); - SYS("ip link add veth0 address " MAC_VETH1 " type veth peer name veth1"); + SYS("ip link add veth0 type veth peer name veth1"); SYS("ip link set veth0 netns at_ns0"); SYS("ip addr add " IP4_ADDR1_VETH1 "/24 dev veth1"); + /* Create a custom rule routing IP4_ADDR2_VETH1 as local. + * Do not place it in "local" table, to avoid this IP being considered + * assigned to a device. + */ + SYS("ip rule add to " IP4_ADDR2_VETH1 " table 20"); + SYS("ip route add local " IP4_ADDR2_VETH1 "/32 dev veth1 table 20"); SYS("ip link set dev veth1 up mtu 1500"); SYS("ip netns exec at_ns0 ip addr add " IP4_ADDR_VETH0 "/24 dev veth0"); SYS("ip netns exec at_ns0 ip link set dev veth0 up mtu 1500"); @@ -125,6 +130,7 @@ static void cleanup(void) { SYS_NOFAIL("test -f /var/run/netns/at_ns0 && ip netns delete at_ns0"); SYS_NOFAIL("ip link del veth1 2> /dev/null"); + 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); } @@ -140,8 +146,6 @@ static int add_vxlan_tunnel(void) VXLAN_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, VXLAN_TUNL_DEV0); - SYS("ip netns exec at_ns0 ip neigh add %s lladdr %s dev veth0", - IP4_ADDR2_VETH1, MAC_VETH1); /* root namespace */ SYS("ip link add dev %s type vxlan external gbp dstport 4789", @@ -279,17 +283,6 @@ static void test_vxlan_tunnel(void) if (attach_tc_prog(&tc_hook, get_src_prog_fd, set_src_prog_fd)) goto done; - /* load and attach bpf prog to veth dev tc hook point */ - ifindex = if_nametoindex("veth1"); - if (!ASSERT_NEQ(ifindex, 0, "veth1 ifindex")) - goto done; - tc_hook.ifindex = ifindex; - set_dst_prog_fd = bpf_program__fd(skel->progs.veth_set_outer_dst); - if (!ASSERT_GE(set_dst_prog_fd, 0, "bpf_program__fd")) - goto done; - if (attach_tc_prog(&tc_hook, set_dst_prog_fd, -1)) - 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")) diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c index 98af55f0bcd3..b11f6952b0c8 100644 --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c @@ -15,24 +15,15 @@ #include #include #include -#include #include #include #include #include -#include #include #include #define log_err(__ret) bpf_printk("ERROR line:%d ret:%d\n", __LINE__, __ret) -#define VXLAN_UDP_PORT 4789 - -/* Only IPv4 address assigned to veth1. - * 172.16.1.200 - */ -#define ASSIGNED_ADDR_VETH1 0xac1001c8 - struct geneve_opt { __be16 opt_class; __u8 type; @@ -43,11 +34,6 @@ struct geneve_opt { __u8 opt_data[8]; /* hard-coded to 8 byte */ }; -struct vxlanhdr { - __be32 vx_flags; - __be32 vx_vni; -} __attribute__((packed)); - struct vxlan_metadata { __u32 gbp; }; @@ -384,8 +370,14 @@ int vxlan_get_tunnel_src(struct __sk_buff *skb) int ret; struct bpf_tunnel_key key; struct vxlan_metadata md; - __u32 orig_daddr; __u32 index = 0; + __u32 *local_ip = NULL; + + local_ip = bpf_map_lookup_elem(&local_ip_map, &index); + if (!local_ip) { + log_err(ret); + return TC_ACT_SHOT; + } ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), BPF_F_TUNINFO_FLAGS); @@ -400,13 +392,14 @@ int vxlan_get_tunnel_src(struct __sk_buff *skb) return TC_ACT_SHOT; } - if (key.local_ipv4 != ASSIGNED_ADDR_VETH1 || md.gbp != 0x800FF || + if (key.local_ipv4 != *local_ip || 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, bpf_ntohs(key.tunnel_flags)); + bpf_printk("local_ip 0x%x\n", *local_ip); log_err(ret); return TC_ACT_SHOT; } @@ -414,61 +407,6 @@ int vxlan_get_tunnel_src(struct __sk_buff *skb) return TC_ACT_OK; } -SEC("tc") -int veth_set_outer_dst(struct __sk_buff *skb) -{ - struct ethhdr *eth = (struct ethhdr *)(long)skb->data; - __u32 assigned_ip = bpf_htonl(ASSIGNED_ADDR_VETH1); - void *data_end = (void *)(long)skb->data_end; - struct udphdr *udph; - struct iphdr *iph; - __u32 index = 0; - int ret = 0; - int shrink; - __s64 csum; - - if ((void *)eth + sizeof(*eth) > data_end) { - log_err(ret); - return TC_ACT_SHOT; - } - - if (eth->h_proto != bpf_htons(ETH_P_IP)) - return TC_ACT_OK; - - iph = (struct iphdr *)(eth + 1); - if ((void *)iph + sizeof(*iph) > data_end) { - log_err(ret); - return TC_ACT_SHOT; - } - if (iph->protocol != IPPROTO_UDP) - return TC_ACT_OK; - - udph = (struct udphdr *)(iph + 1); - if ((void *)udph + sizeof(*udph) > data_end) { - log_err(ret); - return TC_ACT_SHOT; - } - if (udph->dest != bpf_htons(VXLAN_UDP_PORT)) - return TC_ACT_OK; - - if (iph->daddr != assigned_ip) { - csum = bpf_csum_diff(&iph->daddr, sizeof(__u32), &assigned_ip, - sizeof(__u32), 0); - if (bpf_skb_store_bytes(skb, ETH_HLEN + offsetof(struct iphdr, daddr), - &assigned_ip, sizeof(__u32), 0) < 0) { - log_err(ret); - return TC_ACT_SHOT; - } - if (bpf_l3_csum_replace(skb, ETH_HLEN + offsetof(struct iphdr, check), - 0, csum, 0) < 0) { - log_err(ret); - return TC_ACT_SHOT; - } - bpf_skb_change_type(skb, PACKET_HOST); - } - return TC_ACT_OK; -} - SEC("tc") int ip6vxlan_set_tunnel_dst(struct __sk_buff *skb) { From patchwork Sun Sep 11 08:46: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: 12972790 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 4C58FECAAA1 for ; Sun, 11 Sep 2022 08:46:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229954AbiIKIqr (ORCPT ); Sun, 11 Sep 2022 04:46:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229716AbiIKIqp (ORCPT ); Sun, 11 Sep 2022 04:46:45 -0400 Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1F3E3206E for ; Sun, 11 Sep 2022 01:46:43 -0700 (PDT) Received: by mail-ed1-x52d.google.com with SMTP id s11so8628012edd.13 for ; Sun, 11 Sep 2022 01:46: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=GDmBcP+6ghXcfR6E5lq/BwYeyvFs0vGrwCup9rQ3VtY=; b=F759KSIOVobtMqfX/OdyzuNwkKJAs4FVlwmw435VQ1A4mp45FgL/VEL7SZcxx1GbHa Q9jgXRopH/ma/zMzVJsSTyMK2pvnqhVqkMuwkpwYT+aqvAYSLLFcKEut4lZYlJliG+tl cNwTTbdt6PBtpZiW6mDaz9qkrCVmJl3YFixSM= 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=GDmBcP+6ghXcfR6E5lq/BwYeyvFs0vGrwCup9rQ3VtY=; b=tfso9ZoF7Md4136D+cL7Vbw2TYBqjKuHNKPfIUQKLURRNggyZliIu8aJft3HK7X/Fo 9W4mReUHqk7fwKZVlomSKbqZb/b0HVew6kSFEIIdfrNJIZqSptCb31QXyPNWXznoYerm Wla8qfyNLlmTXrzMcCRxJ0xT/ffEXM9+7suPKilI2WFqivbvF7Xr8Nyjnp+xOD7v3AIx 5eYhZVjQKZFYqMAXbwhieKUStpvZPitJbWjZKk/2JRCjMUauTLAmPOFiONvfovOnOz8g n9pnHnR4eKTz11dShMdK8f0r0kB3uoezP3zFsqUqDdr7TKJaL2XNMdkwGgB6XdopcIdj luNA== X-Gm-Message-State: ACgBeo1gseUgLT3tne1vzg+ElJueL4X7Jsa8T3nq0ZfW1unQL6N42XCH 5LuDcE76NeF1ACmLWpKhzw/pk8MR1L//ne8hHQtaX1iOHD4sfdtyLQy89F9bD6ZIIT3T2gF3uhe RbPzQEv88Evwi55vUCKZ/y1RWtojwNhLWQH8+UeA4bQqP6xQ3JYoooYzmX2RB+s4uNpnMPa645J Y= X-Google-Smtp-Source: AA6agR4zK4HPOeu4wE1RTwDDYAbnjUAXtC3jGPIRkCpdL6slfmRs1n0vjhXlzVDSvAu9JIC2QfS7RQ== X-Received: by 2002:a05:6402:4cf:b0:446:42cf:e49d with SMTP id n15-20020a05640204cf00b0044642cfe49dmr18100413edw.41.1662886002149; Sun, 11 Sep 2022 01:46:42 -0700 (PDT) Received: from localhost.localdomain ([141.226.162.95]) by smtp.gmail.com with ESMTPSA id q10-20020a170906360a00b007309a570bacsm2713591ejb.176.2022.09.11.01.46.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 Sep 2022 01:46: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 v6 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 11:46:09 +0300 Message-Id: <20220911084609.102519-5-shmulik.ladkani@gmail.com> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220911084609.102519-1-shmulik.ladkani@gmail.com> References: <20220911084609.102519-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..1b7e89ac7006 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) {