|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | +#include <linux/bpf.h> |
| 3 | +#include <bpf/bpf_helpers.h> |
| 4 | + |
| 5 | +#include <bpf/bpf_core_read.h> /* bpf_core_type_id_local */ |
| 6 | + |
| 7 | +/* |
| 8 | + * The xdp_hints_xxx struct's are stored in the XDP 'data_meta' area, |
| 9 | + * which is located just in-front-of the raw packet payload data. |
| 10 | + * |
| 11 | + * Explaining the struct attribute's: |
| 12 | + * ---------------------------------- |
| 13 | + * The struct must be 4 byte aligned (kernel requirement), which here |
| 14 | + * is enforced by the struct __attribute__((aligned(4))). |
| 15 | + * |
| 16 | + * To avoid any C-struct padding attribute "packed" is used. |
| 17 | + * |
| 18 | + * NOTICE: Do NOT define __attribute__((preserve_access_index)) here, |
| 19 | + * as libbpf will try to find a matching kernel data-structure, |
| 20 | + * e.g. it will cause BPF-prog loading step to fail (with invalid func |
| 21 | + * unknown#195896080 which is 0xbad2310 in hex for "bad relo"). |
| 22 | + */ |
| 23 | + |
| 24 | +struct xdp_hints_fail001 { |
| 25 | + __u64 hash64; |
| 26 | + __u32 btf_id; |
| 27 | + __u32 pad; /* Pad that breaks btf_id as last member */ |
| 28 | +} __attribute__((aligned(4))) __attribute__((packed)); |
| 29 | + |
| 30 | +/* Notice struct is without attribute "packed", thus (64-bit) C-compiler will |
| 31 | + * add padding. This will cause btf_id to NOT be the last member (which is a |
| 32 | + * requirement). |
| 33 | + */ |
| 34 | +struct xdp_hints_fail002 { |
| 35 | + __u64 hash64; |
| 36 | + __u32 btf_id; |
| 37 | +} __attribute__((aligned(4))) /* not packed */; |
| 38 | + |
| 39 | + |
| 40 | +SEC("xdp") |
| 41 | +int xdp_prog_fail001(struct xdp_md *ctx) |
| 42 | +{ |
| 43 | + struct xdp_hints_fail001 *meta; |
| 44 | + void *data; |
| 45 | + int err; |
| 46 | + |
| 47 | + err = bpf_xdp_adjust_meta(ctx, -(int)sizeof(*meta)); |
| 48 | + if (err) |
| 49 | + return XDP_ABORTED; |
| 50 | + |
| 51 | + data = (void *)(unsigned long)ctx->data; |
| 52 | + meta = (void *)(unsigned long)ctx->data_meta; |
| 53 | + if (meta + 1 > data) /* Verify meta area is accessible */ |
| 54 | + return XDP_ABORTED; |
| 55 | + |
| 56 | + meta->btf_id = bpf_core_type_id_local(struct xdp_hints_fail001); |
| 57 | + meta->hash64 = 0x4142434445464748; |
| 58 | + |
| 59 | + return XDP_PASS; |
| 60 | +} |
| 61 | + |
| 62 | +SEC("xdp") |
| 63 | +int xdp_prog_fail002(struct xdp_md *ctx) |
| 64 | +{ |
| 65 | + struct xdp_hints_fail002 f002; |
| 66 | + f002.btf_id = bpf_core_type_id_local(struct xdp_hints_fail002); |
| 67 | + if (f002.btf_id == 0) |
| 68 | + return XDP_ABORTED; |
| 69 | + |
| 70 | + return XDP_PASS; |
| 71 | +} |
| 72 | + |
| 73 | +char _license[] SEC("license") = "GPL"; |
0 commit comments