Skip to content

Commit c240ba2

Browse files
yonghong-songAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Add a test with a bpf program with btf_tag attributes
Add a bpf program with btf_tag attributes. The program is loaded successfully with the kernel. With the command bpftool btf dump file ./tag.o the following dump shows that tags are properly encoded: [8] STRUCT 'key_t' size=12 vlen=3 'a' type_id=2 bits_offset=0 'b' type_id=2 bits_offset=32 'c' type_id=2 bits_offset=64 [9] TAG 'tag1' type_id=8 component_id=-1 [10] TAG 'tag2' type_id=8 component_id=-1 [11] TAG 'tag1' type_id=8 component_id=1 [12] TAG 'tag2' type_id=8 component_id=1 ... [21] FUNC_PROTO '(anon)' ret_type_id=2 vlen=1 'x' type_id=2 [22] FUNC 'foo' type_id=21 linkage=static [23] TAG 'tag1' type_id=22 component_id=0 [24] TAG 'tag2' type_id=22 component_id=0 [25] TAG 'tag1' type_id=22 component_id=-1 [26] TAG 'tag2' type_id=22 component_id=-1 ... [29] VAR 'total' type_id=27, linkage=global [30] TAG 'tag1' type_id=29 component_id=-1 [31] TAG 'tag2' type_id=29 component_id=-1 If an old clang compiler, which does not support btf_tag attribute, is used, these btf_tag attributes will be silently ignored. Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent ad52647 commit c240ba2

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2021 Facebook */
3+
#include <test_progs.h>
4+
#include "tag.skel.h"
5+
6+
void test_btf_tag(void)
7+
{
8+
struct tag *skel;
9+
10+
skel = tag__open_and_load();
11+
if (!ASSERT_OK_PTR(skel, "btf_tag"))
12+
return;
13+
tag__destroy(skel);
14+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2021 Facebook */
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
#include <bpf/bpf_tracing.h>
6+
7+
#define __tag1 __attribute__((btf_tag("tag1")))
8+
#define __tag2 __attribute__((btf_tag("tag2")))
9+
10+
struct key_t {
11+
int a;
12+
int b __tag1 __tag2;
13+
int c;
14+
} __tag1 __tag2;
15+
16+
struct {
17+
__uint(type, BPF_MAP_TYPE_HASH);
18+
__uint(max_entries, 3);
19+
__type(key, struct key_t);
20+
__type(value, __u64);
21+
} hashmap1 SEC(".maps");
22+
23+
__u32 total __tag1 __tag2 = 0;
24+
25+
static __noinline int foo(int x __tag1 __tag2) __tag1 __tag2
26+
{
27+
struct key_t key;
28+
__u64 val = 1;
29+
30+
key.a = key.b = key.c = x;
31+
bpf_map_update_elem(&hashmap1, &key, &val, 0);
32+
return 0;
33+
}
34+
35+
SEC("fentry/bpf_fentry_test1")
36+
int BPF_PROG(sub, int x)
37+
{
38+
return foo(x);
39+
}

0 commit comments

Comments
 (0)