mirror of
https://github.com/raspberrypi/linux.git
synced 2025-12-15 14:32:06 +00:00
To correlate the hardware RX timestamp with something, add tracking of two software timestamps both clock source CLOCK_TAI (see description in man clock_gettime(2)). XDP metadata is extended with xdp_timestamp for capturing when XDP received the packet. Populated with BPF helper bpf_ktime_get_tai_ns(). I could not find a BPF helper for getting CLOCK_REALTIME, which would have been preferred. In userspace when AF_XDP sees the packet another software timestamp is recorded via clock_gettime() also clock source CLOCK_TAI. Example output shortly after loading igc driver: poll: 1 (0) skip=1 fail=0 redir=2 xsk_ring_cons__peek: 1 0x12557a8: rx_desc[1]->addr=100000000009000 addr=9100 comp_addr=9000 rx_hash: 0x82A96531 with RSS type:0x1 rx_timestamp: 1681740540304898909 (sec:1681740540.3049) XDP RX-time: 1681740577304958316 (sec:1681740577.3050) delta sec:37.0001 (37000059.407 usec) AF_XDP time: 1681740577305051315 (sec:1681740577.3051) delta sec:0.0001 (92.999 usec) 0x12557a8: complete idx=9 addr=9000 The first observation is that the 37 sec difference between RX HW vs XDP timestamps, which indicate hardware is likely clock source CLOCK_REALTIME, because (as of this writing) CLOCK_TAI is initialised with a 37 sec offset. The 93 usec (microsec) difference between XDP vs AF_XDP userspace is the userspace wakeup time. On this hardware it was caused by CPU idle sleep states, which can be reduced by tuning /dev/cpu_dma_latency. View current requested/allowed latency bound via: hexdump --format '"%d\n"' /dev/cpu_dma_latency More explanation of the output and how this can be used to identify clock drift for the HW clock can be seen here[1]: [1] https://github.com/xdp-project/xdp-project/blob/master/areas/hints/xdp_hints_kfuncs02_driver_igc.org Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Stanislav Fomichev <sdf@google.com> Acked-by: Song Yoong Siang <yoong.siang.song@intel.com> Link: https://lore.kernel.org/bpf/168182466298.616355.2544377890818617459.stgit@firesoul
94 lines
2.3 KiB
C
94 lines
2.3 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <vmlinux.h>
|
|
#include "xdp_metadata.h"
|
|
#include <bpf/bpf_helpers.h>
|
|
#include <bpf/bpf_endian.h>
|
|
|
|
struct {
|
|
__uint(type, BPF_MAP_TYPE_XSKMAP);
|
|
__uint(max_entries, 256);
|
|
__type(key, __u32);
|
|
__type(value, __u32);
|
|
} xsk SEC(".maps");
|
|
|
|
__u64 pkts_skip = 0;
|
|
__u64 pkts_fail = 0;
|
|
__u64 pkts_redir = 0;
|
|
|
|
extern int bpf_xdp_metadata_rx_timestamp(const struct xdp_md *ctx,
|
|
__u64 *timestamp) __ksym;
|
|
extern int bpf_xdp_metadata_rx_hash(const struct xdp_md *ctx, __u32 *hash,
|
|
enum xdp_rss_hash_type *rss_type) __ksym;
|
|
|
|
SEC("xdp")
|
|
int rx(struct xdp_md *ctx)
|
|
{
|
|
void *data, *data_meta, *data_end;
|
|
struct ipv6hdr *ip6h = NULL;
|
|
struct ethhdr *eth = NULL;
|
|
struct udphdr *udp = NULL;
|
|
struct iphdr *iph = NULL;
|
|
struct xdp_meta *meta;
|
|
int err;
|
|
|
|
data = (void *)(long)ctx->data;
|
|
data_end = (void *)(long)ctx->data_end;
|
|
eth = data;
|
|
if (eth + 1 < data_end) {
|
|
if (eth->h_proto == bpf_htons(ETH_P_IP)) {
|
|
iph = (void *)(eth + 1);
|
|
if (iph + 1 < data_end && iph->protocol == IPPROTO_UDP)
|
|
udp = (void *)(iph + 1);
|
|
}
|
|
if (eth->h_proto == bpf_htons(ETH_P_IPV6)) {
|
|
ip6h = (void *)(eth + 1);
|
|
if (ip6h + 1 < data_end && ip6h->nexthdr == IPPROTO_UDP)
|
|
udp = (void *)(ip6h + 1);
|
|
}
|
|
if (udp && udp + 1 > data_end)
|
|
udp = NULL;
|
|
}
|
|
|
|
if (!udp) {
|
|
__sync_add_and_fetch(&pkts_skip, 1);
|
|
return XDP_PASS;
|
|
}
|
|
|
|
/* Forwarding UDP:9091 to AF_XDP */
|
|
if (udp->dest != bpf_htons(9091)) {
|
|
__sync_add_and_fetch(&pkts_skip, 1);
|
|
return XDP_PASS;
|
|
}
|
|
|
|
err = bpf_xdp_adjust_meta(ctx, -(int)sizeof(struct xdp_meta));
|
|
if (err) {
|
|
__sync_add_and_fetch(&pkts_fail, 1);
|
|
return XDP_PASS;
|
|
}
|
|
|
|
data = (void *)(long)ctx->data;
|
|
data_meta = (void *)(long)ctx->data_meta;
|
|
meta = data_meta;
|
|
|
|
if (meta + 1 > data) {
|
|
__sync_add_and_fetch(&pkts_fail, 1);
|
|
return XDP_PASS;
|
|
}
|
|
|
|
err = bpf_xdp_metadata_rx_timestamp(ctx, &meta->rx_timestamp);
|
|
if (!err)
|
|
meta->xdp_timestamp = bpf_ktime_get_tai_ns();
|
|
else
|
|
meta->rx_timestamp = 0; /* Used by AF_XDP as not avail signal */
|
|
|
|
err = bpf_xdp_metadata_rx_hash(ctx, &meta->rx_hash, &meta->rx_hash_type);
|
|
if (err < 0)
|
|
meta->rx_hash_err = err; /* Used by AF_XDP as no hash signal */
|
|
|
|
__sync_add_and_fetch(&pkts_redir, 1);
|
|
return bpf_redirect_map(&xsk, ctx->rx_queue_index, XDP_PASS);
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|