From patchwork Wed Nov 9 21:52:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13038101 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 61B0DC4167B for ; Wed, 9 Nov 2022 21:53:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232207AbiKIVxw (ORCPT ); Wed, 9 Nov 2022 16:53:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50624 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232173AbiKIVxc (ORCPT ); Wed, 9 Nov 2022 16:53:32 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 91EB331ED7; Wed, 9 Nov 2022 13:52:47 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id d59-20020a17090a6f4100b00213202d77e1so3127621pjk.2; Wed, 09 Nov 2022 13:52:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=9yrMnjVPG4cOOqiZYZR1WL3gTbiRvMDdOGJcJaC5E70=; b=foCuYIvAy5RbnThGkpxQ5X+07GLMfgIUbArzLtLyDyVTxYTmbWJ+vwAXSj31YDCk7x 6QdBtbbcfVVSbNX0zRVsQQqc9ksv+KS+qs2rTt2UTzE0r6jdLGudtiQxa0A+WbpAmBh6 C3zW5rSGImi3Se/jmjA79w4d2RNmfIUzbrjIdo/hMhKLe6EhzBNReRKdiO5DHs1QIb4e uF/EmIxMM6CCOBFkX5qtvJ/m3IlTLhQkEvk6UJxcmRE4zHXaznAT7H7JgJ0T6pZZzu/u DZTQs35gfFgfaSYtuAXkZTquNOA40otzmjWlEqwW1D1Org2nRGAUzYxhbs948F9eH9I1 djig== 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:message-id:reply-to; bh=9yrMnjVPG4cOOqiZYZR1WL3gTbiRvMDdOGJcJaC5E70=; b=FdP4q9gppoXjCyzgB17ks9rhRc0/VB4xIfGfkZoPYd1AllUG/l10BI8gK+bR50P0zj q7sR6k171HlyLNJVVkvmXdMey0AxG5YdZr9vQun8W4yqqh0OsHuxx0ih/rNh4QaeCLGE xi9lI5XGmZlh3bQg80d3hY7wAfpXidEozU6HKROvkRM3Z0394wEstk2e6vFXHVq9IRS4 scoSb/qmPELbYoqvJrWYrvlNNidTrh4OH0g8jKxO652myXUHnAKlgZkUg54bY3xHCe5o pmuxKgNf1IoSyAD+8ff3tsc1yyCW7wo1+ul0OPWD1sC+to40IYJ2FUK90SKJFw9a6csP ei9g== X-Gm-Message-State: ACrzQf2aNIxpsPefBxTjPuEGh+Umvuoy1zAD3f5WNhJ+1F+WcS9+/qIN 4sMI7JwZ0vxPX5bFAGL3FI0= X-Google-Smtp-Source: AMsMyM4PjZRb1eAT5hZOYvYgfF4sAZjmh3I2hSgwGXtWQEzgoJXKUavNVBxrSXVU9jWYZR55fMAEAw== X-Received: by 2002:a17:90a:5d06:b0:212:f8e5:81ab with SMTP id s6-20020a17090a5d0600b00212f8e581abmr1254282pji.114.1668030767077; Wed, 09 Nov 2022 13:52:47 -0800 (PST) Received: from john.lan ([98.97.44.95]) by smtp.gmail.com with ESMTPSA id h3-20020aa796c3000000b0056246403534sm8727802pfq.88.2022.11.09.13.52.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 13:52:46 -0800 (PST) From: John Fastabend To: hawk@kernel.org, daniel@iogearbox.net, kuba@kernel.org, davem@davemloft.net, ast@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, john.fastabend@gmail.com, sdf@google.com Subject: [1/2 bpf-next] bpf: expose net_device from xdp for metadata Date: Wed, 9 Nov 2022 13:52:41 -0800 Message-Id: <20221109215242.1279993-2-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20221109215242.1279993-1-john.fastabend@gmail.com> References: <20221109215242.1279993-1-john.fastabend@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net Allow xdp progs to read the net_device structure. Its useful to extract info from the dev itself. Currently, our tracing tooling uses kprobes to capture statistics and information about running net devices. We use kprobes instead of other hooks tc/xdp because we need to collect information about the interface not exposed through the xdp_md structures. This has some down sides that we want to avoid by moving these into the XDP hook itself. First, placing the kprobes in a generic function in the kernel is after XDP so we miss redirects and such done by the XDP networking program. And its needless overhead because we are already paying the cost for calling the XDP program, calling yet another prog is a waste. Better to do everything in one hook from performance side. Of course we could one-off each one of these fields, but that would explode the xdp_md struct and then require writing convert_ctx_access writers for each field. By using BTF we avoid writing field specific convertion logic, BTF just knows how to read the fields, we don't have to add many fields to xdp_md, and I don't have to get every field we will use in the future correct. For reference current examples in our code base use the ifindex, ifname, qdisc stats, net_ns fields, among others. With this patch we can now do the following, dev = ctx->rx_dev; net = dev->nd_net.net; uid.ifindex = dev->ifindex; memcpy(uid.ifname, dev->ifname, NAME); if (net) uid.inum = net->ns.inum; to report the name, index and ns.inum which identifies an interface in our system. Signed-off-by: John Fastabend --- include/uapi/linux/bpf.h | 1 + net/core/filter.c | 19 +++++++++++++++++++ tools/include/uapi/linux/bpf.h | 1 + 3 files changed, 21 insertions(+) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 94659f6b3395..50403eb3b6cf 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -6123,6 +6123,7 @@ struct xdp_md { __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ + __bpf_md_ptr(struct net_device *, rx_dev); /* rxq->dev */ }; /* DEVMAP map-value layout diff --git a/net/core/filter.c b/net/core/filter.c index bb0136e7a8e4..d445ffbea8f1 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -8686,6 +8686,8 @@ static bool __is_valid_xdp_access(int off, int size) return true; } +BTF_ID_LIST_SINGLE(btf_xdp_get_netdev_id, struct, net_device) + static bool xdp_is_valid_access(int off, int size, enum bpf_access_type type, const struct bpf_prog *prog, @@ -8718,6 +8720,15 @@ static bool xdp_is_valid_access(int off, int size, case offsetof(struct xdp_md, data_end): info->reg_type = PTR_TO_PACKET_END; break; + case offsetof(struct xdp_md, rx_dev): + info->reg_type = PTR_TO_BTF_ID; + info->btf_id = btf_xdp_get_netdev_id[0]; + info->btf = bpf_get_btf_vmlinux(); + if (IS_ERR_OR_NULL(info->btf)) + return false; + if (size != sizeof(u64)) + return false; + return true; } return __is_valid_xdp_access(off, size); @@ -9808,6 +9819,14 @@ static u32 xdp_convert_ctx_access(enum bpf_access_type type, *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg, offsetof(struct net_device, ifindex)); break; + case offsetof(struct xdp_md, rx_dev): + *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_buff, rxq), + si->dst_reg, si->src_reg, + offsetof(struct xdp_buff, rxq)); + *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct xdp_rxq_info, dev), + si->dst_reg, si->dst_reg, + offsetof(struct xdp_rxq_info, dev)); + break; } return insn - insn_buf; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 94659f6b3395..50403eb3b6cf 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -6123,6 +6123,7 @@ struct xdp_md { __u32 rx_queue_index; /* rxq->queue_index */ __u32 egress_ifindex; /* txq->dev->ifindex */ + __bpf_md_ptr(struct net_device *, rx_dev); /* rxq->dev */ }; /* DEVMAP map-value layout From patchwork Wed Nov 9 21:52:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Fastabend X-Patchwork-Id: 13038102 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 4FEEEC43219 for ; Wed, 9 Nov 2022 21:53:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232096AbiKIVxy (ORCPT ); Wed, 9 Nov 2022 16:53:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232098AbiKIVxe (ORCPT ); Wed, 9 Nov 2022 16:53:34 -0500 Received: from mail-pl1-x631.google.com (mail-pl1-x631.google.com [IPv6:2607:f8b0:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 325CA3C6E4; Wed, 9 Nov 2022 13:52:49 -0800 (PST) Received: by mail-pl1-x631.google.com with SMTP id io19so18348578plb.8; Wed, 09 Nov 2022 13:52:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=pJTZwmsusxeJbHgIbDeXz7BpaeNwMKLsmROONoqlOjE=; b=H/3JK2glHGKHQmBjgr/T3CI+6HuQBpwZIg8qPfl2Yd9ldmHZmb/MivqlJD6MnnOK5z DLCFTNq6lk42MZdxkMmUxZtvtn1G/1Q5S5TDu9v/ujpwIxtlp1aO9QXJaJfqtN28gSDw IiAoDnvYovEdp996YnvYQwGDpz8rcUaFVn+7fCj1LXhxH3wdyFv7O/Q/KAJ8mL/wQI4H JoDeQiW0LNjQiQqTqXhztUXyrOmrpHTc2B6/lL83+slc9SfEamyP8g/nNQ2tX5Zkal9h /U2jtjdYOqQ+oyrQZR+RE+oXmEdoJtPn+bL28t+WProX3PrYNq4Oev4ALXWw9DysNbcP XfSA== 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:message-id:reply-to; bh=pJTZwmsusxeJbHgIbDeXz7BpaeNwMKLsmROONoqlOjE=; b=SmieTHP5dSdTlB4/6dZO7IsPvR7Rxa4QTJm3wkgSXrYNqsZNGpC3blxpDzuK1myqBi tGG1V1euR1rkxe1dRKo2BDWJwnp7FrSf40bKrqOa9ycFva4LBgxF4jeKf5S3GnH8IStk Mwhc/swe3EJQudZANztlQE1PM1wjG+9anQTY1o7X1wABoUMCGkEw30mkVNDo3uxUf/UP brgqM8ZoLvZvDKPC0DagM4zeVHhViCgHHc/ZReWeDbwxVx1LW5X3z6Qk6pXdSThMPQgn VQtWNGLB0SJp3JZFCaBfMu85UGTI0QQNOcjupWDdzpA4hXCuZbu3x78y06SYaWjhYSZI NyOQ== X-Gm-Message-State: ACrzQf2+d/5po+WOuKcwUQVwHaSVyaO/nIsASzMIc4kpg0OB0kC8YK4H Fxn+GsNV/1O/oTnTxJzXa9o= X-Google-Smtp-Source: AMsMyM4exJa57YfJAKglk9vp6YGZ4kqPRGXRaLAlt1PCAnNVvhd87UIKOjVga0q5SZRH/zQvxyr8+g== X-Received: by 2002:a17:902:f691:b0:186:b250:9763 with SMTP id l17-20020a170902f69100b00186b2509763mr63945311plg.62.1668030768681; Wed, 09 Nov 2022 13:52:48 -0800 (PST) Received: from john.lan ([98.97.44.95]) by smtp.gmail.com with ESMTPSA id h3-20020aa796c3000000b0056246403534sm8727802pfq.88.2022.11.09.13.52.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Nov 2022 13:52:48 -0800 (PST) From: John Fastabend To: hawk@kernel.org, daniel@iogearbox.net, kuba@kernel.org, davem@davemloft.net, ast@kernel.org Cc: netdev@vger.kernel.org, bpf@vger.kernel.org, john.fastabend@gmail.com, sdf@google.com Subject: [2/2 bpf-next] bpf: add selftest to read xdp_md fields Date: Wed, 9 Nov 2022 13:52:42 -0800 Message-Id: <20221109215242.1279993-3-john.fastabend@gmail.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20221109215242.1279993-1-john.fastabend@gmail.com> References: <20221109215242.1279993-1-john.fastabend@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This adds a selftest to read the xdp_md net_device and extract common fields we use. This will also compare xdp_md->ifindex with the value extracted through the dev pointer. Signed-off-by: John Fastabend --- .../testing/selftests/bpf/prog_tests/xdp_md.c | 35 +++++++++++++++++++ .../testing/selftests/bpf/progs/test_xdp_md.c | 25 +++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/xdp_md.c create mode 100644 tools/testing/selftests/bpf/progs/test_xdp_md.c diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_md.c b/tools/testing/selftests/bpf/prog_tests/xdp_md.c new file mode 100644 index 000000000000..facf3f3ab86f --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/xdp_md.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include "test_xdp_md.skel.h" + +void test_xdp_md(void) +{ + struct test_xdp_md *skel; + int err, prog_fd; + char buf[128]; + + LIBBPF_OPTS(bpf_test_run_opts, topts, + .data_in = &pkt_v4, + .data_size_in = sizeof(pkt_v4), + .data_out = buf, + .data_size_out = sizeof(buf), + .repeat = 1, + ); + + skel = test_xdp_md__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + prog_fd = bpf_program__fd(skel->progs.md_xdp); + err = bpf_prog_test_run_opts(prog_fd, &topts); + ASSERT_OK(err, "test_run"); + ASSERT_EQ(topts.retval, XDP_PASS, "xdp_md test_run retval"); + + ASSERT_EQ(skel->bss->ifindex, 1, "xdp_md ifindex"); + ASSERT_EQ(skel->bss->ifindex, skel->bss->ingress_ifindex, "xdp_md ingress_ifindex"); + ASSERT_STREQ(skel->bss->name, "lo", "xdp_md name"); + ASSERT_NEQ(skel->bss->inum, 0, "xdp_md inum"); + + test_xdp_md__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_xdp_md.c b/tools/testing/selftests/bpf/progs/test_xdp_md.c new file mode 100644 index 000000000000..66ad4a7c80cd --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_xdp_md.c @@ -0,0 +1,25 @@ +#include "vmlinux.h" +#include +#include + +#define IFNAMSIZ 16 + +int ifindex, ingress_ifindex; +char name[IFNAMSIZ]; +unsigned int inum; + +SEC("xdp") +int md_xdp(struct xdp_md *ctx) +{ + struct net_device *dev; + + dev = ctx->rx_dev; + + ifindex = dev->ifindex; + inum = dev->nd_net.net->ns.inum; + memcpy(name, dev->name, IFNAMSIZ); + ingress_ifindex = ctx->ingress_ifindex; + return XDP_PASS; +} + +char _license[] SEC("license") = "GPL";