From patchwork Thu Apr 20 12:31:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gilad Sever X-Patchwork-Id: 13218697 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 0937BC77B72 for ; Thu, 20 Apr 2023 12:32:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231868AbjDTMcw (ORCPT ); Thu, 20 Apr 2023 08:32:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234571AbjDTMct (ORCPT ); Thu, 20 Apr 2023 08:32:49 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 672A26595; Thu, 20 Apr 2023 05:32:29 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id ffacd0b85a97d-2fa36231b1cso350020f8f.2; Thu, 20 Apr 2023 05:32:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681993948; x=1684585948; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KGr5SgBXm0/c3pk6ZOsMNvbnJ2k0k6gIIMCtdzqVOR4=; b=ka98ufLobq6KQna8mnYMZJk3voRvhAsbxgwLjhri8Uh+5XpOkfVAijgHN2AWsAwFL5 E5IJshXSK/N50ZZaoOPT13ENyj+W4/e++S2K0Ihlg4ZKg6ZAnlQvb7wxlQivZAHCosY2 vbxfra0+0buJKt0xKLxhsjsVMgcc1i5KMjU1yvhzOZ6YnIHNpkUb05fapnRj2/TGCGPd yoynKDWGMSlRGNHH83rKJSn0tUqZcZbe11ziwraqwhInKtwzPjP6jAbsn3CzY3hyl6/u CX+UzYuumtrRxWv/Eh/81W+XCFH2X/Uf3FDPkJvbspEqvhIebXQPeAXVJT8GmphZXOfp tnOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681993948; x=1684585948; 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:message-id:reply-to; bh=KGr5SgBXm0/c3pk6ZOsMNvbnJ2k0k6gIIMCtdzqVOR4=; b=KNHFM3FjLfRk3h8TW2W1ZJpqQzVY7i6GL83+DdBzdTiNkzAQgHRhqFNdArTxL7mx4P ZMCEQKQzN29icURrWGxBDkhmEvDssSUsroi9zwVJ/LTEKrnEEDiz1mg6xZul3kE1jYKm yY++ZG3rFmGKW4o1E2zZ3Y8uhR2WVwhnVm9s9nUyxDsCzKPoVfr6MXOyApCB6zcWHT1y ctuCR4ZfUwIojjnC1dwokoA+Uw91DVmsV43BvcHHelSIR8to+lZXDN7X9YLtsWSlkrxL B+UQOARJf7CDokMH2ME9KlsyCReFvMwfYjr6qEbjdYTFgM7ec0QJQqgt7eQgbwoU/1Zc dTHA== X-Gm-Message-State: AAQBX9dDABlii5A9QLNjpH5cAi3WOsPkbx0ooLKIpVGjCXxqoOVmHsCJ vFvmnOMfwgybHvVRzQEGLbw= X-Google-Smtp-Source: AKy350ahVieMOvE7Ut8giQxGLOLF/E9mDfuyvHzGdmHHG6EUvxsSmvN7DGOioRp3oZJ66Xo5OrVJzQ== X-Received: by 2002:a05:6000:50:b0:2f6:a7a:1df5 with SMTP id k16-20020a056000005000b002f60a7a1df5mr1156146wrx.70.1681993947577; Thu, 20 Apr 2023 05:32:27 -0700 (PDT) Received: from gsever-Latitude-7400.corp.proofpoint.com ([46.120.112.185]) by smtp.gmail.com with ESMTPSA id z16-20020a5d4410000000b002f79ea6746asm1835081wrq.94.2023.04.20.05.32.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 05:32:27 -0700 (PDT) From: Gilad Sever To: dsahern@kernel.org, martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, andrii@kernel.org, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, hawk@kernel.org, joe@wand.net.nz Cc: eyal.birger@gmail.com, shmulik.ladkani@gmail.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Gilad Sever Subject: [PATCH bpf 1/4] bpf: factor out socket lookup functions for the TC hookpoint. Date: Thu, 20 Apr 2023 15:31:52 +0300 Message-Id: <20230420123155.497634-2-gilad9366@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230420123155.497634-1-gilad9366@gmail.com> References: <20230420123155.497634-1-gilad9366@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Change BPF helper socket lookup functions to use TC specific variants: bpf_tc_sk_lookup_tcp() / bpf_tc_sk_lookup_udp() / bpf_tc_skc_lookup_tcp() instead of sharing implementation with the cg / sk_skb hooking points. This allows introducing a separate logic for the TC flow. The tc functions are identical to the original code. Reviewed-by: Shmulik Ladkani Reviewed-by: Eyal Birger Signed-off-by: Gilad Sever --- net/core/filter.c | 63 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 1d6f165923bf..5910956f4e0d 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6701,6 +6701,63 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { .arg5_type = ARG_ANYTHING, }; +BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb, + struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) +{ + return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP, + netns_id, flags); +} + +static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = { + .func = bpf_tc_skc_lookup_tcp, + .gpl_only = false, + .pkt_access = true, + .ret_type = RET_PTR_TO_SOCK_COMMON_OR_NULL, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, + .arg3_type = ARG_CONST_SIZE, + .arg4_type = ARG_ANYTHING, + .arg5_type = ARG_ANYTHING, +}; + +BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb, + struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) +{ + return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, + netns_id, flags); +} + +static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = { + .func = bpf_tc_sk_lookup_tcp, + .gpl_only = false, + .pkt_access = true, + .ret_type = RET_PTR_TO_SOCKET_OR_NULL, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, + .arg3_type = ARG_CONST_SIZE, + .arg4_type = ARG_ANYTHING, + .arg5_type = ARG_ANYTHING, +}; + +BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb, + struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) +{ + return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, + netns_id, flags); +} + +static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = { + .func = bpf_tc_sk_lookup_udp, + .gpl_only = false, + .pkt_access = true, + .ret_type = RET_PTR_TO_SOCKET_OR_NULL, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, + .arg3_type = ARG_CONST_SIZE, + .arg4_type = ARG_ANYTHING, + .arg5_type = ARG_ANYTHING, +}; + BPF_CALL_1(bpf_sk_release, struct sock *, sk) { if (sk && sk_is_refcounted(sk)) @@ -7954,9 +8011,9 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) #endif #ifdef CONFIG_INET case BPF_FUNC_sk_lookup_tcp: - return &bpf_sk_lookup_tcp_proto; + return &bpf_tc_sk_lookup_tcp_proto; case BPF_FUNC_sk_lookup_udp: - return &bpf_sk_lookup_udp_proto; + return &bpf_tc_sk_lookup_udp_proto; case BPF_FUNC_sk_release: return &bpf_sk_release_proto; case BPF_FUNC_tcp_sock: @@ -7964,7 +8021,7 @@ tc_cls_act_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) case BPF_FUNC_get_listener_sock: return &bpf_get_listener_sock_proto; case BPF_FUNC_skc_lookup_tcp: - return &bpf_skc_lookup_tcp_proto; + return &bpf_tc_skc_lookup_tcp_proto; case BPF_FUNC_tcp_check_syncookie: return &bpf_tcp_check_syncookie_proto; case BPF_FUNC_skb_ecn_set_ce: From patchwork Thu Apr 20 12:31:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gilad Sever X-Patchwork-Id: 13218698 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 CEE60C77B7C for ; Thu, 20 Apr 2023 12:32:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234437AbjDTMcx (ORCPT ); Thu, 20 Apr 2023 08:32:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234317AbjDTMcu (ORCPT ); Thu, 20 Apr 2023 08:32:50 -0400 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 154D712F; Thu, 20 Apr 2023 05:32:33 -0700 (PDT) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3f178da21b5so4446725e9.3; Thu, 20 Apr 2023 05:32:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681993951; x=1684585951; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SQt4d0zWjQY1z91uepy6h4EuQqaXR87jdp20zwNy9IM=; b=EzZCGW8+GBqLFc5RwBAzUZE0mhTL7nEhEZC1sraSYZOZ0ZF1sZV5oR0THYhzW3XyGo rknZ2Ch94q3kyfXD1wa+JZORjr7wS5TkY31olxsctkReFWB4g6xmpiDPB6purgkPXOGB 1FIT5JoGYTudVToFaLmwJr185zGzDXbgG41zzrMBcfiXFUROivGawFE9LqFQA5RMLSyr 3YugEZEy2WKV07hho9ofpN88T2KWiWuJC2BaRXyra8SscJNhsQr2EzZgA7accXSyhoa9 zSEVKpRrNxaYvy+kY3alUFgd5HwTqHsz64YSa5cauOhS8U/cP16IgaHong6quDbPxmp6 U34A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681993951; x=1684585951; 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:message-id:reply-to; bh=SQt4d0zWjQY1z91uepy6h4EuQqaXR87jdp20zwNy9IM=; b=QqBGEZYqp63o2dkZByZfE7KTJkxIEo2T+4jttX7Ht0xbBbma+8D9EM013eGyKJQlWh Rr8asrsUeyBwr7qa5u9P/cxUKp6WgaJMNh9ToMfgZ+CY7GQNcs5LIxqRR6goSRxf40m8 Thu9bZ0MMLhliwfWQ6FKylz8VoV3uM+Qv5VfnjwhDzmQGxLfZBqTIC5UU4Ewsej0GhRE Nqas7cwyOL4i5NeuK5ODVzUDBIKjkn2bT3B69Fw8eBdKiu6u+qOgaVuKIdN1tzA+LJPM nFLZt7TyVcBe0sgeSjVIK9/ZLahSe9GJjF8zcdUNPI9HPgYjnH3uqOHLUmIyfz9HjsRy g7Kg== X-Gm-Message-State: AAQBX9ebgEDDrl6Sw0moOtJjRkSVFPq3dzDmqgHFuj053YVG9UYVfNWL S7duamxpRrMKJZSYy8Y+5qs= X-Google-Smtp-Source: AKy350YhKYEwknDHPu9QM3Wf0jpgPncIy67BxrDuOVn4vNX9LMfVQ/D5bEMB4qL7dbetCvQAtcP5CQ== X-Received: by 2002:adf:f3d0:0:b0:2cf:efc7:19ad with SMTP id g16-20020adff3d0000000b002cfefc719admr1226762wrp.53.1681993951394; Thu, 20 Apr 2023 05:32:31 -0700 (PDT) Received: from gsever-Latitude-7400.corp.proofpoint.com ([46.120.112.185]) by smtp.gmail.com with ESMTPSA id z16-20020a5d4410000000b002f79ea6746asm1835081wrq.94.2023.04.20.05.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 05:32:30 -0700 (PDT) From: Gilad Sever To: dsahern@kernel.org, martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, andrii@kernel.org, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, hawk@kernel.org, joe@wand.net.nz Cc: eyal.birger@gmail.com, shmulik.ladkani@gmail.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Gilad Sever Subject: [PATCH bpf 2/4] bpf: Call __bpf_sk_lookup()/__bpf_skc_lookup() directly via TC hookpoint Date: Thu, 20 Apr 2023 15:31:53 +0300 Message-Id: <20230420123155.497634-3-gilad9366@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230420123155.497634-1-gilad9366@gmail.com> References: <20230420123155.497634-1-gilad9366@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net skb->dev always exists in the tc flow. There is no need to use bpf_skc_lookup(), bpf_sk_lookup() from this code path. This change facilitates fixing the tc flow to be VRF aware. Reviewed-by: Shmulik Ladkani Reviewed-by: Eyal Birger Signed-off-by: Gilad Sever --- net/core/filter.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index 5910956f4e0d..f43f86fc1235 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6704,8 +6704,12 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb, struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) { - return (unsigned long)bpf_skc_lookup(skb, tuple, len, IPPROTO_TCP, - netns_id, flags); + struct net *caller_net = dev_net(skb->dev); + int ifindex = skb->dev->ifindex; + + return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net, + ifindex, IPPROTO_TCP, netns_id, + flags); } static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = { @@ -6723,8 +6727,12 @@ static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = { BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb, struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) { - return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_TCP, - netns_id, flags); + struct net *caller_net = dev_net(skb->dev); + int ifindex = skb->dev->ifindex; + + return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net, + ifindex, IPPROTO_TCP, netns_id, + flags); } static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = { @@ -6742,8 +6750,12 @@ static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = { BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb, struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) { - return (unsigned long)bpf_sk_lookup(skb, tuple, len, IPPROTO_UDP, - netns_id, flags); + struct net *caller_net = dev_net(skb->dev); + int ifindex = skb->dev->ifindex; + + return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net, + ifindex, IPPROTO_UDP, netns_id, + flags); } static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = { From patchwork Thu Apr 20 12:31:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gilad Sever X-Patchwork-Id: 13218699 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 56E72C77B73 for ; Thu, 20 Apr 2023 12:32:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233953AbjDTMcx (ORCPT ); Thu, 20 Apr 2023 08:32:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53510 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233899AbjDTMcw (ORCPT ); Thu, 20 Apr 2023 08:32:52 -0400 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0FDB61A9; Thu, 20 Apr 2023 05:32:36 -0700 (PDT) Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-3f17edbc15eso6060025e9.3; Thu, 20 Apr 2023 05:32:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681993955; x=1684585955; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=8knN6x28v9+/g4cJXjKltffmYEX+i0LygM23A3ayXmU=; b=GV+cA3Qf95uys8m0X5QyosOWsAgDAqAB2bU0LLqOHInX68GkHY12a4ZDUYi1jr4+ZR R1H3RdL77M+eJ9Kea+1/Og7R6P1JL1r+T3wQTqevQQaYZopxQPRNkkfqRCxSp4O4Xss/ Xe8LfXFuXMRb839GbHA2l99oL1DWMbL9sGkEmpd9dK5UEUjR8FrLAorBWA1kwD1izzO3 pY9Kbfgr4qoHJxBwUQ4kXXOayrIo8wpqWV7ywQSBV9Kqw9Xj5IyOFHFIA8Nj+AXh/A5l jjl9PvnptqxQKf5v6ZzI0u8F3E3r1ptpKRnASDXxmE1/eugjOyflbnzaO/hbCezu/i37 blng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681993955; x=1684585955; 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:message-id:reply-to; bh=8knN6x28v9+/g4cJXjKltffmYEX+i0LygM23A3ayXmU=; b=Gd68u7DwKsV1qitB7S9aiiSctBGDZL7LhjQVS/KAM/9g64/vCn8EihCwo0VtgKt39s lz0w3jk9B5kuc3mzXeoZEeyfHtYZJ8Ifi52dmzXaKPhYBJMF4IxuQ6yUbCRoxd28JIOL noAZ3feHgzISsKRHqFBN97ueVKbrTw0PGWjWU6NCouN7aisvV5XpVgxGtwCdmeVGP76f jZ8XwsFW+Iz9EfblB6+9FC5MJfLfdgRIQmqubzriKpXpSrc4wJ977uYEGVFPmqqhB7Pw hIpyA0vzWpD22EFaGSoY9nbIrTCmrdmqFLqbey1EG5sTIZ7ipNp/Nw3x/DP6sp8Ttpwb S3ag== X-Gm-Message-State: AAQBX9efMzvH8kiNmH+NCQbQ0lkl4Y2jyHCs4VAoWGGvZC9DT1sFoZAA 5vOGiG4hVopCshNQV4WQA1M= X-Google-Smtp-Source: AKy350YWH7HgS4FqMJ8pHzIKmyMuBxoXEzlX7WqgJvXdMyxK9ihLzeQmEqaxCfRTDq4qcCaLoNIl2Q== X-Received: by 2002:adf:cc90:0:b0:2f2:17fc:e15b with SMTP id p16-20020adfcc90000000b002f217fce15bmr1144263wrj.6.1681993955155; Thu, 20 Apr 2023 05:32:35 -0700 (PDT) Received: from gsever-Latitude-7400.corp.proofpoint.com ([46.120.112.185]) by smtp.gmail.com with ESMTPSA id z16-20020a5d4410000000b002f79ea6746asm1835081wrq.94.2023.04.20.05.32.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 05:32:34 -0700 (PDT) From: Gilad Sever To: dsahern@kernel.org, martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, andrii@kernel.org, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, hawk@kernel.org, joe@wand.net.nz Cc: eyal.birger@gmail.com, shmulik.ladkani@gmail.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Gilad Sever Subject: [PATCH bpf 3/4] bpf: fix bpf socket lookup from tc/xdp to respect socket VRF bindings Date: Thu, 20 Apr 2023 15:31:54 +0300 Message-Id: <20230420123155.497634-4-gilad9366@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230420123155.497634-1-gilad9366@gmail.com> References: <20230420123155.497634-1-gilad9366@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net When calling bpf_sk_lookup_tcp(), bpf_sk_lookup_udp() or bpf_skc_lookup_tcp() from tc/xdp ingress, VRF socket bindings aren't respoected, i.e. unbound sockets are returned, and bound sockets aren't found. VRF binding is determined by the sdif argument to sk_lookup(), however when called from tc the IP SKB control block isn't initialized and thus inet{,6}_sdif() always returns 0. Fix by calculating sdif for the tc/xdp flows by observing the device's l3 enslaved state. The cg/sk_skb hooking points which are expected to support inet{,6}_sdif() pass sdif=-1 which makes __bpf_skc_lookup() use the existing logic. Fixes: 6acc9b432e67 ("bpf: Add helper to retrieve socket in BPF") Reviewed-by: Shmulik Ladkani Reviewed-by: Eyal Birger Signed-off-by: Gilad Sever --- net/core/filter.c | 63 +++++++++++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/net/core/filter.c b/net/core/filter.c index f43f86fc1235..d68e14ff1e55 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -6529,12 +6529,11 @@ static struct sock *sk_lookup(struct net *net, struct bpf_sock_tuple *tuple, static struct sock * __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, - u64 flags) + u64 flags, int sdif) { struct sock *sk = NULL; struct net *net; u8 family; - int sdif; if (len == sizeof(tuple->ipv4)) family = AF_INET; @@ -6546,10 +6545,12 @@ __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, if (unlikely(flags || !((s32)netns_id < 0 || netns_id <= S32_MAX))) goto out; - if (family == AF_INET) - sdif = inet_sdif(skb); - else - sdif = inet6_sdif(skb); + if (sdif < 0) { + if (family == AF_INET) + sdif = inet_sdif(skb); + else + sdif = inet6_sdif(skb); + } if ((s32)netns_id < 0) { net = caller_net; @@ -6569,10 +6570,11 @@ __bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, static struct sock * __bpf_sk_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, struct net *caller_net, u32 ifindex, u8 proto, u64 netns_id, - u64 flags) + u64 flags, int sdif) { struct sock *sk = __bpf_skc_lookup(skb, tuple, len, caller_net, - ifindex, proto, netns_id, flags); + ifindex, proto, netns_id, flags, + sdif); if (sk) { struct sock *sk2 = sk_to_full_sk(sk); @@ -6612,7 +6614,7 @@ bpf_skc_lookup(struct sk_buff *skb, struct bpf_sock_tuple *tuple, u32 len, } return __bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, proto, - netns_id, flags); + netns_id, flags, -1); } static struct sock * @@ -6701,15 +6703,25 @@ static const struct bpf_func_proto bpf_sk_lookup_udp_proto = { .arg5_type = ARG_ANYTHING, }; +static int bpf_l2_sdif(const struct net_device *dev) +{ +#if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV) + if (netif_is_l3_slave(dev)) + return dev->ifindex; +#endif + return 0; +} + BPF_CALL_5(bpf_tc_skc_lookup_tcp, struct sk_buff *, skb, struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) { struct net *caller_net = dev_net(skb->dev); + int sdif = bpf_l2_sdif(skb->dev); int ifindex = skb->dev->ifindex; return (unsigned long)__bpf_skc_lookup(skb, tuple, len, caller_net, ifindex, IPPROTO_TCP, netns_id, - flags); + flags, sdif); } static const struct bpf_func_proto bpf_tc_skc_lookup_tcp_proto = { @@ -6728,11 +6740,12 @@ BPF_CALL_5(bpf_tc_sk_lookup_tcp, struct sk_buff *, skb, struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) { struct net *caller_net = dev_net(skb->dev); + int sdif = bpf_l2_sdif(skb->dev); int ifindex = skb->dev->ifindex; return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net, ifindex, IPPROTO_TCP, netns_id, - flags); + flags, sdif); } static const struct bpf_func_proto bpf_tc_sk_lookup_tcp_proto = { @@ -6751,11 +6764,12 @@ BPF_CALL_5(bpf_tc_sk_lookup_udp, struct sk_buff *, skb, struct bpf_sock_tuple *, tuple, u32, len, u64, netns_id, u64, flags) { struct net *caller_net = dev_net(skb->dev); + int sdif = bpf_l2_sdif(skb->dev); int ifindex = skb->dev->ifindex; return (unsigned long)__bpf_sk_lookup(skb, tuple, len, caller_net, ifindex, IPPROTO_UDP, netns_id, - flags); + flags, sdif); } static const struct bpf_func_proto bpf_tc_sk_lookup_udp_proto = { @@ -6788,11 +6802,13 @@ BPF_CALL_5(bpf_xdp_sk_lookup_udp, struct xdp_buff *, ctx, struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) { struct net *caller_net = dev_net(ctx->rxq->dev); - int ifindex = ctx->rxq->dev->ifindex; + struct net_device *dev = ctx->rxq->dev; + int sdif = bpf_l2_sdif(dev); + int ifindex = dev->ifindex; return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, ifindex, IPPROTO_UDP, netns_id, - flags); + flags, sdif); } static const struct bpf_func_proto bpf_xdp_sk_lookup_udp_proto = { @@ -6811,11 +6827,13 @@ BPF_CALL_5(bpf_xdp_skc_lookup_tcp, struct xdp_buff *, ctx, struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) { struct net *caller_net = dev_net(ctx->rxq->dev); - int ifindex = ctx->rxq->dev->ifindex; + struct net_device *dev = ctx->rxq->dev; + int sdif = bpf_l2_sdif(dev); + int ifindex = dev->ifindex; return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, caller_net, ifindex, IPPROTO_TCP, netns_id, - flags); + flags, sdif); } static const struct bpf_func_proto bpf_xdp_skc_lookup_tcp_proto = { @@ -6834,11 +6852,13 @@ BPF_CALL_5(bpf_xdp_sk_lookup_tcp, struct xdp_buff *, ctx, struct bpf_sock_tuple *, tuple, u32, len, u32, netns_id, u64, flags) { struct net *caller_net = dev_net(ctx->rxq->dev); - int ifindex = ctx->rxq->dev->ifindex; + struct net_device *dev = ctx->rxq->dev; + int sdif = bpf_l2_sdif(dev); + int ifindex = dev->ifindex; return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, caller_net, ifindex, IPPROTO_TCP, netns_id, - flags); + flags, sdif); } static const struct bpf_func_proto bpf_xdp_sk_lookup_tcp_proto = { @@ -6858,7 +6878,8 @@ BPF_CALL_5(bpf_sock_addr_skc_lookup_tcp, struct bpf_sock_addr_kern *, ctx, { return (unsigned long)__bpf_skc_lookup(NULL, tuple, len, sock_net(ctx->sk), 0, - IPPROTO_TCP, netns_id, flags); + IPPROTO_TCP, netns_id, flags, + -1); } static const struct bpf_func_proto bpf_sock_addr_skc_lookup_tcp_proto = { @@ -6877,7 +6898,7 @@ BPF_CALL_5(bpf_sock_addr_sk_lookup_tcp, struct bpf_sock_addr_kern *, ctx, { return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, sock_net(ctx->sk), 0, IPPROTO_TCP, - netns_id, flags); + netns_id, flags, -1); } static const struct bpf_func_proto bpf_sock_addr_sk_lookup_tcp_proto = { @@ -6896,7 +6917,7 @@ BPF_CALL_5(bpf_sock_addr_sk_lookup_udp, struct bpf_sock_addr_kern *, ctx, { return (unsigned long)__bpf_sk_lookup(NULL, tuple, len, sock_net(ctx->sk), 0, IPPROTO_UDP, - netns_id, flags); + netns_id, flags, -1); } static const struct bpf_func_proto bpf_sock_addr_sk_lookup_udp_proto = { From patchwork Thu Apr 20 12:31:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gilad Sever X-Patchwork-Id: 13218700 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 33CC1C77B7F for ; Thu, 20 Apr 2023 12:33:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234938AbjDTMcz (ORCPT ); Thu, 20 Apr 2023 08:32:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234321AbjDTMcw (ORCPT ); Thu, 20 Apr 2023 08:32:52 -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 BFCA65FEA; Thu, 20 Apr 2023 05:32:41 -0700 (PDT) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-2fa47de5b04so548151f8f.1; Thu, 20 Apr 2023 05:32:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681993960; x=1684585960; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ll7sVqXM0dY8LZJVLuNcEqs+AeYLqx/WbaG5ztvPIFA=; b=JiDKDYpNS+KDcLGSUhnknnE5y2Ahtr3w7jD0AiLB4iM5zVEhsd2MXPJbzYD/EH0cuV QYfHP8U39mJOF+B+5/uLLDReBX12ciG7pCt/LiANRMPPJwWxxssA8z2ZMOgYwu4vdhtW 88DSPUP/RcC/PZ8viCqAy9rFuFkRxh05fCG4fg8VaC0/dOiNqjkQJylprbN4n1JbX+TD 72wwYeL7AFyXbUyEaEYz77nOJymyIswlJ59+PCvi1NRrvAmQq8HMQjcrHEULyJw50+LI 5kkvW8Ji3i8UkSKy7y4Mj8rDqTQr6VSyUICaF86g+fjpJ6KkjMe+cjFkoFFdIXfretcc 6dtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681993960; x=1684585960; 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:message-id:reply-to; bh=Ll7sVqXM0dY8LZJVLuNcEqs+AeYLqx/WbaG5ztvPIFA=; b=iESy6RvzF5qHfG874Bv8Lw+y0gze7r3gVQJtVmJmghs3AaZhCaaf6D9JbofubZVH45 LWIaCF00WAWjz08nLaK0TDk6uLS5orptDJ9zV1AxJ9+Wpyitj+bCkXIp/JSpSErC0IvN sk3eohdmpTrKVoWaipct4ped6zuT+OCFKD6vadVJxAhisaVsSbLc60+p1C3my6+zmfQV 5iIZWUyxi2qnUe+NBnayI22WnTz0LHHR1eDtv/h8EJZZq19I5vhbZTqigWdnuexGqy3i s3Y6wQM8bKzyNQwzCo8ssQzP3Xqq5kjR5k+qG7s5izwuGh+J9+zYJlMjj39Wi6nXAaqs ZFxA== X-Gm-Message-State: AAQBX9fxU1JBBrW1h3iAWSa6Mh6cz/Xrx1m3nil/ys/uOA7Oc09HHMjS rfXRF0OSxFOfqhjYKzpM0/I= X-Google-Smtp-Source: AKy350bAinhP1S8iQ1YafufBWOPZl924panL1LRqiTHQfOg5cw80Ccc3BsN5MBsQAap0NICvF7AbbQ== X-Received: by 2002:a5d:44c9:0:b0:2d7:998c:5ad9 with SMTP id z9-20020a5d44c9000000b002d7998c5ad9mr1256146wrr.8.1681993959900; Thu, 20 Apr 2023 05:32:39 -0700 (PDT) Received: from gsever-Latitude-7400.corp.proofpoint.com ([46.120.112.185]) by smtp.gmail.com with ESMTPSA id z16-20020a5d4410000000b002f79ea6746asm1835081wrq.94.2023.04.20.05.32.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Apr 2023 05:32:39 -0700 (PDT) From: Gilad Sever To: dsahern@kernel.org, martin.lau@linux.dev, daniel@iogearbox.net, john.fastabend@gmail.com, ast@kernel.org, andrii@kernel.org, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mykolal@fb.com, shuah@kernel.org, hawk@kernel.org, joe@wand.net.nz Cc: eyal.birger@gmail.com, shmulik.ladkani@gmail.com, bpf@vger.kernel.org, netdev@vger.kernel.org, linux-kselftest@vger.kernel.org, Gilad Sever Subject: [PATCH bpf 4/4] selftests/bpf: Add tc_socket_lookup tests Date: Thu, 20 Apr 2023 15:31:55 +0300 Message-Id: <20230420123155.497634-5-gilad9366@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230420123155.497634-1-gilad9366@gmail.com> References: <20230420123155.497634-1-gilad9366@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Verify that socket lookup via TC with all BPF APIs is VRF aware. Signed-off-by: Gilad Sever --- .../bpf/prog_tests/tc_socket_lookup.c | 341 ++++++++++++++++++ .../selftests/bpf/progs/tc_socket_lookup.c | 73 ++++ 2 files changed, 414 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/tc_socket_lookup.c create mode 100644 tools/testing/selftests/bpf/progs/tc_socket_lookup.c diff --git a/tools/testing/selftests/bpf/prog_tests/tc_socket_lookup.c b/tools/testing/selftests/bpf/prog_tests/tc_socket_lookup.c new file mode 100644 index 000000000000..1ddd70cf4a61 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/tc_socket_lookup.c @@ -0,0 +1,341 @@ +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause + +/* + * Topology: + * --------- + * NS1 namespace | NS2 namespace + * | + * +--------------+ | +--------------+ + * | veth01 |----------| veth10 | + * | 172.16.1.100 | | | 172.16.1.200 | + * | bpf | | +--------------+ + * +--------------+ | + * server(UDP/TCP) | + * +-------------------+ | + * | vrf1 | | + * | +--------------+ | | +--------------+ + * | | veth02 |----------| veth20 | + * | | 172.16.2.100 | | | | 172.16.2.200 | + * | | bpf | | | +--------------+ + * | +--------------+ | | + * | server(UDP/TCP) | | + * +-------------------+ | + * + * Test flow + * ----------- + * The tests verifies that socket lookup via TC is VRF aware: + * 1) Creates two veth pairs between NS1 and NS2: + * a) veth01 <-> veth10 outside the VRF + * b) veth02 <-> veth20 in the VRF + * 2) Attaches to veth01 and veth02 a program that calls: + * a) bpf_skc_lookup_tcp() with TCP and tcp_skc is true + * b) bpf_sk_lookup_tcp() with TCP and tcp_skc is false + * c) bpf_sk_lookup_udp() with UDP + * The program stores the lookup result in bss->lookup_status. + * 3) Creates a socket TCP/UDP server in/outside the VRF. + * 4) The test expects lookup_status to be: + * a) 0 from device in VRF to server outside VRF + * b) 0 from device outside VRF to server in VRF + * c) 1 from device in VRF to server in VRF + * d) 1 from device outside VRF to server outside VRF + */ + +#include + +#include "test_progs.h" +#include "network_helpers.h" +#include "tc_socket_lookup.skel.h" + +#define NS1 "tc_socket_lookup_1" +#define NS2 "tc_socket_lookup_2" + +#define IP4_ADDR_VETH01 "172.16.1.100" +#define IP4_ADDR_VETH10 "172.16.1.200" +#define IP4_ADDR_VETH02 "172.16.2.100" +#define IP4_ADDR_VETH20 "172.16.2.200" + +#define NON_VRF_PORT 5000 +#define IN_VRF_PORT 5001 + +#define IO_TIMEOUT_SEC 3 + +#define SYS(fmt, ...) \ + ({ \ + char cmd[1024]; \ + snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \ + if (!ASSERT_OK(system(cmd), cmd)) \ + goto fail; \ + }) + +#define SYS_NOFAIL(fmt, ...) \ + ({ \ + char cmd[1024]; \ + snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \ + system(cmd); \ + }) + +static int make_socket(int sotype, const char *ip, int port, + struct sockaddr_storage *addr) +{ + struct timeval timeo = { .tv_sec = IO_TIMEOUT_SEC }; + int err, fd; + + err = make_sockaddr(AF_INET, ip, port, addr, NULL); + if (!ASSERT_OK(err, "make_address")) + return -1; + + fd = socket(AF_INET, sotype, 0); + if (!ASSERT_OK(fd < 0, "socket")) + return -1; + + err = setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)); + if (!ASSERT_OK(err, "setsockopt(SO_SNDTIMEO)")) + goto fail; + + err = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); + if (!ASSERT_OK(err, "setsockopt(SO_RCVTIMEO)")) + goto fail; + + return fd; +fail: + close(fd); + return -1; +} + +static int make_server(int sotype, const char *ip, int port, const char *ifname) +{ + struct sockaddr_storage addr = {}; + const int one = 1; + int err, fd = -1; + + fd = make_socket(sotype, ip, port, &addr); + if (fd < 0) + return -1; + + if (sotype == SOCK_STREAM) { + err = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, + sizeof(one)); + if (!ASSERT_OK(err, "setsockopt(SO_REUSEADDR)")) + goto fail; + } + + if (ifname) { + err = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, + ifname, strlen(ifname) + 1); + if (!ASSERT_OK(err, "setsockopt(SO_BINDTODEVICE)")) + goto fail; + } + + err = bind(fd, (void *)&addr, sizeof(struct sockaddr_in)); + if (!ASSERT_OK(err, "bind")) + goto fail; + + if (sotype == SOCK_STREAM) { + err = listen(fd, SOMAXCONN); + if (!ASSERT_OK(err, "listen")) + goto fail; + } + + return fd; +fail: + close(fd); + return -1; +} + +static int attach_tc_prog(struct bpf_tc_hook *hook, int prog_fd) +{ + LIBBPF_OPTS(bpf_tc_opts, opts1, .handle = 1, .priority = 1, + .prog_fd = prog_fd); + int ret; + + ret = bpf_tc_hook_create(hook); + if (!ASSERT_OK(ret, "create tc hook")) + return ret; + + if (prog_fd >= 0) { + hook->attach_point = BPF_TC_INGRESS; + ret = bpf_tc_attach(hook, &opts1); + if (!ASSERT_OK(ret, "bpf_tc_attach")) { + bpf_tc_hook_destroy(hook); + return ret; + } + } + return 0; +} + +static void cleanup(void) +{ + SYS_NOFAIL("test -f /var/run/netns/" NS1 " && ip netns delete " + NS1); + SYS_NOFAIL("test -f /var/run/netns/" NS2 " && ip netns delete " + NS2); +} + +static int setup(struct tc_socket_lookup *skel) +{ + LIBBPF_OPTS(bpf_tc_hook, tc_hook, .attach_point = BPF_TC_INGRESS); + struct nstoken *nstoken = NULL; + int ifindex, prog_fd, ret = 0; + + SYS("ip netns add " NS1); + SYS("ip netns add " NS2); + + /* NS1 <-> NS2 [veth01 <-> veth10] */ + SYS("ip link add veth01 netns " NS1 " type veth peer name veth10 netns " + NS2); + SYS("ip -net " NS1 " addr add " IP4_ADDR_VETH01 "/24 dev veth01"); + SYS("ip -net " NS1 " link set dev veth01 up"); + SYS("ip -net " NS2 " addr add " IP4_ADDR_VETH10 "/24 dev veth10"); + SYS("ip -net " NS2 " link set dev veth10 up"); + + /* NS1 <-> NS2 [veth02 <-> veth20] */ + SYS("ip link add veth02 netns " NS1 " type veth peer name veth20 netns " + NS2); + SYS("ip -net " NS1 " addr add " IP4_ADDR_VETH02 "/24 dev veth02"); + SYS("ip -net " NS1 " link set dev veth02 up"); + SYS("ip -net " NS2 " addr add " IP4_ADDR_VETH20 "/24 dev veth20"); + SYS("ip -net " NS2 " link set dev veth20 up"); + + /* veth02 -> vrf1 */ + SYS("ip -net " NS1 " link add vrf1 type vrf table 11"); + SYS("ip -net " NS1 " route add vrf vrf1 unreachable default metric " + "4278198272"); + SYS("ip -net " NS1 " link set vrf1 alias vrf"); + SYS("ip -net " NS1 " link set vrf1 up"); + SYS("ip -net " NS1 " link set veth02 master vrf1"); + + /* Attach prog to veth devices in NS1 */ + nstoken = open_netns(NS1); + if (!ASSERT_OK_PTR(nstoken, "setns " NS1)) + goto fail; + prog_fd = bpf_program__fd(skel->progs.test_socket_lookup); + if (!ASSERT_GE(prog_fd, 0, "bpf_program__fd")) + goto fail; + + ifindex = if_nametoindex("veth01"); + if (!ASSERT_NEQ(ifindex, 0, "veth01 ifindex")) + goto fail; + tc_hook.ifindex = ifindex; + if (attach_tc_prog(&tc_hook, prog_fd)) + goto fail; + + ifindex = if_nametoindex("veth02"); + if (!ASSERT_NEQ(ifindex, 0, "veth02 ifindex")) + goto fail; + tc_hook.ifindex = ifindex; + if (attach_tc_prog(&tc_hook, prog_fd)) + goto fail; + goto close; +fail: + ret = -1; +close: + if (nstoken) + close_netns(nstoken); + return ret; +} + +static int test_lookup(struct tc_socket_lookup *skel, int sotype, + const char *ip, int port, bool tcp_skc, + int lookup_status_exp) +{ + static const char msg[] = "Hello Server"; + struct sockaddr_storage addr = {}; + int fd, ret = 0; + + fd = make_socket(sotype, ip, port, &addr); + if (fd < 0) + return -1; + + skel->bss->tcp_skc = tcp_skc; + skel->bss->lookup_status = -1; + + if (sotype == SOCK_STREAM) + connect(fd, (void *)&addr, sizeof(struct sockaddr_in)); + else + sendto(fd, msg, sizeof(msg), 0, (void *)&addr, + sizeof(struct sockaddr_in)); + + if (!ASSERT_EQ(skel->bss->lookup_status, lookup_status_exp, + "lookup_status")) + goto fail; + + goto close; + +fail: + ret = -1; +close: + close(fd); + return ret; +} + +static void _test_tc_socket_lookup(struct tc_socket_lookup *skel, int sotype, + bool tcp_skc) +{ + int in_vrf_server, non_vrf_server; + struct nstoken *nstoken = NULL; + + nstoken = open_netns(NS1); + if (!ASSERT_OK_PTR(nstoken, "setns " NS1)) + goto done; + + /* Open sockets in and outside VRF */ + non_vrf_server = make_server(sotype, "0.0.0.0", NON_VRF_PORT, NULL); + if (!ASSERT_GE(non_vrf_server, 0, "make_server__outside_vrf_fd")) + goto done; + + in_vrf_server = make_server(sotype, "0.0.0.0", IN_VRF_PORT, "veth02"); + if (!ASSERT_GE(in_vrf_server, 0, "make_server__in_vrf_fd")) + goto done; + + /* Perform test from NS2 */ + close_netns(nstoken); + nstoken = open_netns(NS2); + if (!ASSERT_OK_PTR(nstoken, "setns " NS2)) + goto done; + + if (!ASSERT_OK(test_lookup(skel, sotype, IP4_ADDR_VETH02, NON_VRF_PORT, + tcp_skc, 0), "in_to_out")) + goto done; + if (!ASSERT_OK(test_lookup(skel, sotype, IP4_ADDR_VETH02, IN_VRF_PORT, + tcp_skc, 1), "in_to_in")) + goto done; + if (!ASSERT_OK(test_lookup(skel, sotype, IP4_ADDR_VETH01, NON_VRF_PORT, + tcp_skc, 1), "out_to_out")) + goto done; + if (!ASSERT_OK(test_lookup(skel, sotype, IP4_ADDR_VETH01, IN_VRF_PORT, + tcp_skc, 0), "out_to_in")) + goto done; + +done: + if (non_vrf_server) + close(non_vrf_server); + if (in_vrf_server) + close(in_vrf_server); + if (nstoken) + close_netns(nstoken); +} + +void test_tc_socket_lookup(void) +{ + struct tc_socket_lookup *skel; + + cleanup(); + + skel = tc_socket_lookup__open_and_load(); + if (!ASSERT_OK_PTR(skel, "tc_socket_lookup__open_and_load")) + return; + + if (!ASSERT_OK(setup(skel), "setup")) + goto done; + + if (test__start_subtest("tc_socket_lookup_tcp")) + _test_tc_socket_lookup(skel, SOCK_STREAM, false); + if (test__start_subtest("tc_socket_lookup_tcp_skc")) + _test_tc_socket_lookup(skel, SOCK_STREAM, true); + if (test__start_subtest("tc_socket_lookup_udp")) + _test_tc_socket_lookup(skel, SOCK_DGRAM, false); + +done: + tc_socket_lookup__destroy(skel); + cleanup(); +} diff --git a/tools/testing/selftests/bpf/progs/tc_socket_lookup.c b/tools/testing/selftests/bpf/progs/tc_socket_lookup.c new file mode 100644 index 000000000000..06601eb17807 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/tc_socket_lookup.c @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include + +int lookup_status; +bool tcp_skc; + +#define CUR_NS BPF_F_CURRENT_NETNS + +SEC("tc") +int test_socket_lookup(struct __sk_buff *skb) +{ + struct bpf_sock_tuple *tp; + void *data_end, *data; + struct bpf_sock *sk; + struct ethhdr *eth; + struct iphdr *iph; + int tplen; + + if (skb->protocol != bpf_htons(ETH_P_IP)) + return TC_ACT_UNSPEC; + + tplen = sizeof(tp->ipv4); + + if (bpf_skb_pull_data(skb, sizeof(*eth) + sizeof(*iph) + tplen)) + return TC_ACT_SHOT; + + data_end = (void *)(long)skb->data_end; + data = (void *)(long)skb->data; + + eth = data; + if (eth + 1 > data_end) + return TC_ACT_SHOT; + + iph = (struct iphdr *)(eth + 1); + if (iph + 1 > data_end) + return TC_ACT_SHOT; + + tp = (struct bpf_sock_tuple *)&iph->saddr; + if ((void *)tp + tplen > data_end) + return TC_ACT_SHOT; + + switch (iph->protocol) { + case IPPROTO_TCP: + if (tcp_skc) + sk = bpf_skc_lookup_tcp(skb, tp, tplen, CUR_NS, 0); + else + sk = bpf_sk_lookup_tcp(skb, tp, tplen, CUR_NS, 0); + break; + case IPPROTO_UDP: + sk = bpf_sk_lookup_udp(skb, tp, tplen, CUR_NS, 0); + break; + default: + return TC_ACT_SHOT; + } + + lookup_status = 0; + + if (sk) { + bpf_sk_release(sk); + lookup_status = 1; + } + + return TC_ACT_UNSPEC; +} + +char _license[] SEC("license") = "GPL";