From: Ederson de Souza <ederson.desouza@intel.com> To: xdp-hints@xdp-project.net Cc: bpf@vger.kernel.org Subject: [[RFC xdp-hints] 16/16] samples/bpf: Show XDP hints usage Date: Mon, 2 Aug 2021 18:03:31 -0700 [thread overview] Message-ID: <20210803010331.39453-17-ederson.desouza@intel.com> (raw) In-Reply-To: <20210803010331.39453-1-ederson.desouza@intel.com> An example of how to retrieve XDP hints/metadata from an XDP frame. To get the xdp_hints struct, one can use: $ bpftool net xdp show xdp: enp6s0(2) md_btf_id(44) md_btf_enabled(0) To get the BTF id, and then: $ bpftool btf dump id 44 format c > btf.h But, in this example, to demonstrate BTF and CORE features, a simpler struct was defined, containing the only field used by the sample. A lowpoint is that it's not currently possible to use some CORE features from "samples/bpf" directory, as those samples are currently built without using "clang -target bpf". This way, it was not possible to use "bpf_core_field_exists" macro to check, in runtime, the presence of a given XDP hints field. --- samples/bpf/xdp_sample_pkts_kern.c | 21 +++++++++++++++++++++ samples/bpf/xdp_sample_pkts_user.c | 4 +++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/samples/bpf/xdp_sample_pkts_kern.c b/samples/bpf/xdp_sample_pkts_kern.c index 9cf76b340dd7..9f0b0c5a6237 100644 --- a/samples/bpf/xdp_sample_pkts_kern.c +++ b/samples/bpf/xdp_sample_pkts_kern.c @@ -3,6 +3,7 @@ #include <linux/version.h> #include <uapi/linux/bpf.h> #include <bpf/bpf_helpers.h> +#include <bpf/bpf_core_read.h> #define SAMPLE_SIZE 64ul @@ -12,16 +13,23 @@ struct { __uint(value_size, sizeof(u32)); } my_map SEC(".maps"); +struct xdp_hints { + u64 rx_timestamp; +}; + SEC("xdp_sample") int xdp_sample_prog(struct xdp_md *ctx) { + void *meta_data = (void *)(long)ctx->data_meta; void *data_end = (void *)(long)ctx->data_end; void *data = (void *)(long)ctx->data; + struct xdp_hints *hints; /* Metadata will be in the perf event before the packet data. */ struct S { u16 cookie; u16 pkt_len; + u64 rx_timestamp; } __packed metadata; if (data < data_end) { @@ -41,9 +49,22 @@ int xdp_sample_prog(struct xdp_md *ctx) metadata.cookie = 0xdead; metadata.pkt_len = (u16)(data_end - data); + metadata.rx_timestamp = 0; sample_size = min(metadata.pkt_len, SAMPLE_SIZE); flags |= (u64)sample_size << 32; + if (meta_data < data) { + hints = meta_data; + /* bpf_core_field_exists doesn't work from samples/bpf, + * as it is only available for "clang -target bpf", which + * is not used on samples/bpf. A program that can use + * the "vmlinux.h" and "clang -target btf" could use this + * call to check the existence of a given field in runtime + */ + /*if (bpf_core_field_exists(hints->rx_timestamp))*/ + metadata.rx_timestamp = BPF_CORE_READ(hints, rx_timestamp); + } + ret = bpf_perf_event_output(ctx, &my_map, flags, &metadata, sizeof(metadata)); if (ret) diff --git a/samples/bpf/xdp_sample_pkts_user.c b/samples/bpf/xdp_sample_pkts_user.c index 495e09897bd3..b87e0ae8eb3d 100644 --- a/samples/bpf/xdp_sample_pkts_user.c +++ b/samples/bpf/xdp_sample_pkts_user.c @@ -76,6 +76,7 @@ static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size) struct { __u16 cookie; __u16 pkt_len; + __u64 rx_timestamp; __u8 pkt_data[SAMPLE_SIZE]; } __packed *e = data; int i; @@ -85,7 +86,8 @@ static void print_bpf_output(void *ctx, int cpu, void *data, __u32 size) return; } - printf("Pkt len: %-5d bytes. Ethernet hdr: ", e->pkt_len); + printf("Pkt len: %-5d bytes. RX timestamp: %llu Ethernet hdr: ", e->pkt_len, + e->rx_timestamp); for (i = 0; i < 14 && i < e->pkt_len; i++) printf("%02x ", e->pkt_data[i]); printf("\n"); -- 2.32.0
next prev parent reply other threads:[~2021-08-03 1:03 UTC|newest] Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-08-03 1:03 [[RFC xdp-hints] 00/16] XDP hints and AF_XDP support Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 01/16] bpf: add btf register/unregister API Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 02/16] net/core: XDP metadata BTF netlink API Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 03/16] tools/bpf: Query XDP metadata BTF ID Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 04/16] tools/bpf: Add xdp set command for md btf Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 05/16] igc: Fix race condition in PTP Tx code Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 06/16] igc: Retrieve the TX timestamp directly (instead of in a interrupt) Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 07/16] igc: Add support for multiple in-flight TX timestamps Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 08/16] igc: Use irq safe locks for timestamping Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 09/16] net/xdp: Support for generic XDP hints Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 10/16] igc: XDP packet RX timestamp Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 11/16] igc: XDP packet TX timestamp Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 12/16] ethtool,igc: Add "xdp_headroom" driver info Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 13/16] libbpf: Helpers to access XDP frame metadata Ederson de Souza 2021-08-06 22:59 ` Andrii Nakryiko 2021-08-19 11:47 ` Toke Høiland-Jørgensen 2021-08-03 1:03 ` [[RFC xdp-hints] 14/16] libbpf: Helpers to access XDP hints based on BTF definitions Ederson de Souza 2021-08-03 1:03 ` [[RFC xdp-hints] 15/16] samples/bpf: XDP hints AF_XDP example Ederson de Souza 2021-08-03 1:03 ` Ederson de Souza [this message] 2021-08-06 23:14 ` [[RFC xdp-hints] 16/16] samples/bpf: Show XDP hints usage Andrii Nakryiko 2021-08-03 9:12 ` [[RFC xdp-hints] 00/16] XDP hints and AF_XDP support Alexander Lobakin 2021-08-03 15:23 ` John Fastabend 2021-08-04 15:15 ` Alexander Lobakin 2021-08-04 23:45 ` John Fastabend 2021-08-13 22:04 ` Desouza, Ederson
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style List information: https://lists.xdp-project.net/postorius/lists/xdp-hints.xdp-project.net/ * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20210803010331.39453-17-ederson.desouza@intel.com \ --to=ederson.desouza@intel.com \ --cc=bpf@vger.kernel.org \ --cc=xdp-hints@xdp-project.net \ --subject='Re: [[RFC xdp-hints] 16/16] samples/bpf: Show XDP hints usage' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox