Skip to content

Commit 2091654

Browse files
committed
AF_XDP-interaction: Add BPF object/code for unit test
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
1 parent a0e2c91 commit 2091654

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

AF_XDP-interaction/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
USER_TARGETS := af_xdp_user
44
USER_TARGETS += btf_unit_test
55
BPF_TARGETS := af_xdp_kern
6+
BPF_TARGETS += btf_unit_test_bpf
67

78
# Define C-code objects USER_TARGETS needs
89
USER_TARGETS_OBJS := common_params.o common_user_bpf_xdp.o

AF_XDP-interaction/btf_unit_test.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,5 +128,3 @@ int main(int argc, char **argv)
128128

129129
return EXIT_OK;
130130
}
131-
132-
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)