From 69c0f60008ea11d67851d4638bf8f5afdeb4486d Mon Sep 17 00:00:00 2001 From: Jesper Dangaard Brouer Date: Fri, 29 Oct 2021 17:54:09 +0200 Subject: [PATCH] AF_XDP-interaction: Make BPF-prog BTF aware Signed-off-by: Jesper Dangaard Brouer --- AF_XDP-interaction/af_xdp_kern.c | 11 ++++++++++- AF_XDP-interaction/af_xdp_user.c | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/AF_XDP-interaction/af_xdp_kern.c b/AF_XDP-interaction/af_xdp_kern.c index d550e52..f030a49 100644 --- a/AF_XDP-interaction/af_xdp_kern.c +++ b/AF_XDP-interaction/af_xdp_kern.c @@ -4,6 +4,8 @@ #include +#include /* */ + struct { __uint(type, BPF_MAP_TYPE_XSKMAP); __uint(max_entries, 64); /* Assume netdev has no more than 64 queues */ @@ -27,7 +29,14 @@ struct { */ struct meta_info { __u32 mark; + __u32 btf_id; } __attribute__((aligned(4))); +/* + * NOTICE: Do NOT define __attribute__((preserve_access_index)) here, + * as libbpf will try to find a matching kernel data-structure, + * e.g. it will cause BPF-prog loading step to fail (with invalid func + * unknown#195896080 which is 0xbad2310 in hex for "bad relo"). + */ SEC("xdp_sock") int xdp_sock_prog(struct xdp_md *ctx) @@ -58,10 +67,10 @@ int xdp_sock_prog(struct xdp_md *ctx) return XDP_ABORTED; meta->mark = 42; + meta->btf_id = bpf_core_type_id_local(struct xdp_hints_mark); pkt_count = bpf_map_lookup_elem(&xdp_stats_map, &index); if (pkt_count) { - /* We pass every other packet */ if ((*pkt_count)++ & 1) return XDP_PASS; diff --git a/AF_XDP-interaction/af_xdp_user.c b/AF_XDP-interaction/af_xdp_user.c index bf0be48..56bb9e9 100644 --- a/AF_XDP-interaction/af_xdp_user.c +++ b/AF_XDP-interaction/af_xdp_user.c @@ -303,6 +303,7 @@ static inline void csum_replace2(__sum16 *sum, __be16 old, __be16 new) struct meta_info { __u32 mark; + __u32 btf_id; } __attribute__((aligned(4))); static void print_meta_info(uint8_t *pkt, uint32_t len) @@ -314,7 +315,7 @@ static void print_meta_info(uint8_t *pkt, uint32_t len) */ struct meta_info *meta = (void *)(pkt - sizeof(*meta)); - printf("DEBUG-meta %d\n", meta->mark); + printf("DEBUG-meta btf_id:%d mark:%d\n", meta->btf_id, meta->mark); }