Skip to content

Commit 1b82c13

Browse files
alan-maguirekernel-patches-bot
authored andcommitted
selftests/bpf: add bpf_snprintf_btf helper tests
Tests verifying snprintf()ing of various data structures, flags combinations using a tp_btf program. Signed-off-by: Alan Maguire <[email protected]>
1 parent 597e0a3 commit 1b82c13

File tree

2 files changed

+314
-0
lines changed

2 files changed

+314
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include <linux/btf.h>
4+
#include "netif_receive_skb.skel.h"
5+
6+
/* Demonstrate that bpf_snprintf_btf succeeds and that various data types
7+
* are formatted correctly.
8+
*/
9+
void test_snprintf_btf(void)
10+
{
11+
struct netif_receive_skb *skel;
12+
struct netif_receive_skb__bss *bss;
13+
int err, duration = 0;
14+
15+
skel = netif_receive_skb__open();
16+
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
17+
return;
18+
19+
err = netif_receive_skb__load(skel);
20+
if (CHECK(err, "skel_load", "failed to load skeleton: %d\n", err))
21+
goto cleanup;
22+
23+
bss = skel->bss;
24+
25+
err = netif_receive_skb__attach(skel);
26+
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
27+
goto cleanup;
28+
29+
/* generate receive event */
30+
system("ping -c 1 127.0.0.1 > /dev/null");
31+
32+
/*
33+
* Make sure netif_receive_skb program was triggered
34+
* and it set expected return values from bpf_trace_printk()s
35+
* and all tests ran.
36+
*/
37+
if (CHECK(bss->ret <= 0,
38+
"bpf_snprintf_btf: got return value",
39+
"ret <= 0 %ld test %d\n", bss->ret, bss->ran_subtests))
40+
goto cleanup;
41+
42+
if (CHECK(bss->ran_subtests == 0, "check if subtests ran",
43+
"no subtests ran, did BPF program run?"))
44+
goto cleanup;
45+
46+
if (CHECK(bss->num_subtests != bss->ran_subtests,
47+
"check all subtests ran",
48+
"only ran %d of %d tests\n", bss->num_subtests,
49+
bss->ran_subtests))
50+
goto cleanup;
51+
52+
cleanup:
53+
netif_receive_skb__destroy(skel);
54+
}
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2020, Oracle and/or its affiliates. */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_tracing.h>
7+
#include <errno.h>
8+
9+
long ret = 0;
10+
int num_subtests = 0;
11+
int ran_subtests = 0;
12+
13+
#define STRSIZE 2048
14+
#define EXPECTED_STRSIZE 256
15+
16+
#ifndef ARRAY_SIZE
17+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
18+
#endif
19+
20+
struct {
21+
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
22+
__uint(max_entries, 1);
23+
__type(key, __u32);
24+
__type(value, char[STRSIZE]);
25+
} strdata SEC(".maps");
26+
27+
static int __strncmp(const void *m1, const void *m2, size_t len)
28+
{
29+
const unsigned char *s1 = m1;
30+
const unsigned char *s2 = m2;
31+
int i, delta = 0;
32+
33+
#pragma clang loop unroll(full)
34+
for (i = 0; i < len; i++) {
35+
delta = s1[i] - s2[i];
36+
if (delta || s1[i] == 0 || s2[i] == 0)
37+
break;
38+
}
39+
return delta;
40+
}
41+
42+
/* Use __builtin_btf_type_id to test snprintf_btf by type id instead of name */
43+
#if __has_builtin(__builtin_btf_type_id)
44+
#define TEST_BTF_BY_ID(_str, _typestr, _ptr, _hflags) \
45+
do { \
46+
int _expected_ret = ret; \
47+
_ptr.type = 0; \
48+
_ptr.type_id = __builtin_btf_type_id(_typestr, 0); \
49+
ret = bpf_snprintf_btf(_str, STRSIZE, &_ptr, \
50+
sizeof(_ptr), _hflags); \
51+
if (ret != _expected_ret) { \
52+
bpf_printk("expected ret (%d), got (%d)", \
53+
_expected_ret, ret); \
54+
ret = -EBADMSG; \
55+
} \
56+
} while (0)
57+
#else
58+
#define TEST_BTF_BY_ID(_str, _typestr, _ptr, _hflags) \
59+
do { } while (0)
60+
#endif
61+
62+
#define TEST_BTF(_str, _type, _flags, _expected, ...) \
63+
do { \
64+
static const char _expectedval[EXPECTED_STRSIZE] = \
65+
_expected; \
66+
static const char _ptrtype[64] = #_type; \
67+
__u64 _hflags = _flags | BTF_F_COMPACT; \
68+
static _type _ptrdata = __VA_ARGS__; \
69+
static struct btf_ptr _ptr = { }; \
70+
int _cmp; \
71+
\
72+
_ptr.type = _ptrtype; \
73+
_ptr.ptr = &_ptrdata; \
74+
\
75+
++num_subtests; \
76+
if (ret < 0) \
77+
break; \
78+
++ran_subtests; \
79+
ret = bpf_snprintf_btf(_str, STRSIZE, \
80+
&_ptr, sizeof(_ptr), _hflags); \
81+
if (ret) \
82+
break; \
83+
_cmp = __strncmp(_str, _expectedval, EXPECTED_STRSIZE); \
84+
if (_cmp != 0) { \
85+
bpf_printk("(%d) got %s", _cmp, _str); \
86+
bpf_printk("(%d) expected %s", _cmp, \
87+
_expectedval); \
88+
ret = -EBADMSG; \
89+
break; \
90+
} \
91+
TEST_BTF_BY_ID(_str, #_type, _ptr, _hflags); \
92+
} while (0)
93+
94+
/* Use where expected data string matches its stringified declaration */
95+
#define TEST_BTF_C(_str, _type, _flags, ...) \
96+
TEST_BTF(_str, _type, _flags, "(" #_type ")" #__VA_ARGS__, \
97+
__VA_ARGS__)
98+
99+
/* TRACE_EVENT(netif_receive_skb,
100+
* TP_PROTO(struct sk_buff *skb),
101+
*/
102+
SEC("tp_btf/netif_receive_skb")
103+
int BPF_PROG(trace_netif_receive_skb, struct sk_buff *skb)
104+
{
105+
static __u64 flags[] = { 0, BTF_F_COMPACT, BTF_F_ZERO, BTF_F_PTR_RAW,
106+
BTF_F_NONAME, BTF_F_COMPACT | BTF_F_ZERO |
107+
BTF_F_PTR_RAW | BTF_F_NONAME };
108+
static const char skbtype[] = "struct sk_buff";
109+
static struct btf_ptr p = { };
110+
__u32 key = 0;
111+
int i, __ret;
112+
char *str;
113+
114+
str = bpf_map_lookup_elem(&strdata, &key);
115+
if (!str)
116+
return 0;
117+
118+
/* Ensure we can write skb string representation */
119+
p.type = skbtype;
120+
p.ptr = skb;
121+
for (i = 0; i < ARRAY_SIZE(flags); i++) {
122+
++num_subtests;
123+
ret = bpf_snprintf_btf(str, STRSIZE, &p, sizeof(p), 0);
124+
if (ret < 0)
125+
bpf_printk("returned %d when writing skb", ret);
126+
++ran_subtests;
127+
}
128+
129+
/* Check invalid ptr value */
130+
p.ptr = 0;
131+
__ret = bpf_snprintf_btf(str, STRSIZE, &p, sizeof(p), 0);
132+
if (__ret >= 0) {
133+
bpf_printk("printing NULL should generate error, got (%d)",
134+
__ret);
135+
ret = -ERANGE;
136+
}
137+
138+
/* Verify type display for various types. */
139+
140+
/* simple int */
141+
TEST_BTF_C(str, int, 0, 1234);
142+
TEST_BTF(str, int, BTF_F_NONAME, "1234", 1234);
143+
/* zero value should be printed at toplevel */
144+
TEST_BTF(str, int, 0, "(int)0", 0);
145+
TEST_BTF(str, int, BTF_F_NONAME, "0", 0);
146+
TEST_BTF(str, int, BTF_F_ZERO, "(int)0", 0);
147+
TEST_BTF(str, int, BTF_F_NONAME | BTF_F_ZERO, "0", 0);
148+
TEST_BTF_C(str, int, 0, -4567);
149+
TEST_BTF(str, int, BTF_F_NONAME, "-4567", -4567);
150+
151+
/* simple char */
152+
TEST_BTF_C(str, char, 0, 100);
153+
TEST_BTF(str, char, BTF_F_NONAME, "100", 100);
154+
/* zero value should be printed at toplevel */
155+
TEST_BTF(str, char, 0, "(char)0", 0);
156+
TEST_BTF(str, char, BTF_F_NONAME, "0", 0);
157+
TEST_BTF(str, char, BTF_F_ZERO, "(char)0", 0);
158+
TEST_BTF(str, char, BTF_F_NONAME | BTF_F_ZERO, "0", 0);
159+
160+
/* simple typedef */
161+
TEST_BTF_C(str, uint64_t, 0, 100);
162+
TEST_BTF(str, u64, BTF_F_NONAME, "1", 1);
163+
/* zero value should be printed at toplevel */
164+
TEST_BTF(str, u64, 0, "(u64)0", 0);
165+
TEST_BTF(str, u64, BTF_F_NONAME, "0", 0);
166+
TEST_BTF(str, u64, BTF_F_ZERO, "(u64)0", 0);
167+
TEST_BTF(str, u64, BTF_F_NONAME|BTF_F_ZERO, "0", 0);
168+
169+
/* typedef struct */
170+
TEST_BTF_C(str, atomic_t, 0, {.counter = (int)1,});
171+
TEST_BTF(str, atomic_t, BTF_F_NONAME, "{1,}", {.counter = 1,});
172+
/* typedef with 0 value should be printed at toplevel */
173+
TEST_BTF(str, atomic_t, 0, "(atomic_t){}", {.counter = 0,});
174+
TEST_BTF(str, atomic_t, BTF_F_NONAME, "{}", {.counter = 0,});
175+
TEST_BTF(str, atomic_t, BTF_F_ZERO, "(atomic_t){.counter = (int)0,}",
176+
{.counter = 0,});
177+
TEST_BTF(str, atomic_t, BTF_F_NONAME|BTF_F_ZERO,
178+
"{0,}", {.counter = 0,});
179+
180+
/* enum where enum value does (and does not) exist */
181+
TEST_BTF_C(str, enum bpf_cmd, 0, BPF_MAP_CREATE);
182+
TEST_BTF(str, enum bpf_cmd, 0, "(enum bpf_cmd)BPF_MAP_CREATE", 0);
183+
TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME, "BPF_MAP_CREATE",
184+
BPF_MAP_CREATE);
185+
TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME|BTF_F_ZERO,
186+
"BPF_MAP_CREATE", 0);
187+
188+
TEST_BTF(str, enum bpf_cmd, BTF_F_ZERO, "(enum bpf_cmd)BPF_MAP_CREATE",
189+
BPF_MAP_CREATE);
190+
TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME|BTF_F_ZERO,
191+
"BPF_MAP_CREATE", BPF_MAP_CREATE);
192+
TEST_BTF_C(str, enum bpf_cmd, 0, 2000);
193+
TEST_BTF(str, enum bpf_cmd, BTF_F_NONAME, "2000", 2000);
194+
195+
/* simple struct */
196+
TEST_BTF_C(str, struct btf_enum, 0,
197+
{.name_off = (__u32)3,.val = (__s32)-1,});
198+
TEST_BTF(str, struct btf_enum, BTF_F_NONAME, "{3,-1,}",
199+
{ .name_off = 3, .val = -1,});
200+
TEST_BTF(str, struct btf_enum, BTF_F_NONAME, "{-1,}",
201+
{ .name_off = 0, .val = -1,});
202+
TEST_BTF(str, struct btf_enum, BTF_F_NONAME|BTF_F_ZERO, "{0,-1,}",
203+
{ .name_off = 0, .val = -1,});
204+
/* empty struct should be printed */
205+
TEST_BTF(str, struct btf_enum, 0, "(struct btf_enum){}",
206+
{ .name_off = 0, .val = 0,});
207+
TEST_BTF(str, struct btf_enum, BTF_F_NONAME, "{}",
208+
{ .name_off = 0, .val = 0,});
209+
TEST_BTF(str, struct btf_enum, BTF_F_ZERO,
210+
"(struct btf_enum){.name_off = (__u32)0,.val = (__s32)0,}",
211+
{ .name_off = 0, .val = 0,});
212+
213+
/* struct with pointers */
214+
TEST_BTF(str, struct list_head, BTF_F_PTR_RAW,
215+
"(struct list_head){.next = (struct list_head *)0x0000000000000001,}",
216+
{ .next = (struct list_head *)1 });
217+
/* NULL pointer should not be displayed */
218+
TEST_BTF(str, struct list_head, BTF_F_PTR_RAW,
219+
"(struct list_head){}",
220+
{ .next = (struct list_head *)0 });
221+
222+
/* struct with char array */
223+
TEST_BTF(str, struct bpf_prog_info, 0,
224+
"(struct bpf_prog_info){.name = (char[])['f','o','o',],}",
225+
{ .name = "foo",});
226+
TEST_BTF(str, struct bpf_prog_info, BTF_F_NONAME,
227+
"{['f','o','o',],}",
228+
{.name = "foo",});
229+
/* leading null char means do not display string */
230+
TEST_BTF(str, struct bpf_prog_info, 0,
231+
"(struct bpf_prog_info){}",
232+
{.name = {'\0', 'f', 'o', 'o'}});
233+
/* handle non-printable characters */
234+
TEST_BTF(str, struct bpf_prog_info, 0,
235+
"(struct bpf_prog_info){.name = (char[])[1,2,3,],}",
236+
{ .name = {1, 2, 3, 0}});
237+
238+
/* struct with non-char array */
239+
TEST_BTF(str, struct __sk_buff, 0,
240+
"(struct __sk_buff){.cb = (__u32[])[1,2,3,4,5,],}",
241+
{ .cb = {1, 2, 3, 4, 5,},});
242+
TEST_BTF(str, struct __sk_buff, BTF_F_NONAME,
243+
"{[1,2,3,4,5,],}",
244+
{ .cb = { 1, 2, 3, 4, 5},});
245+
/* For non-char, arrays, show non-zero values only */
246+
TEST_BTF(str, struct __sk_buff, 0,
247+
"(struct __sk_buff){.cb = (__u32[])[1,],}",
248+
{ .cb = { 0, 0, 1, 0, 0},});
249+
250+
/* struct with bitfields */
251+
TEST_BTF_C(str, struct bpf_insn, 0,
252+
{.code = (__u8)1,.dst_reg = (__u8)0x2,.src_reg = (__u8)0x3,.off = (__s16)4,.imm = (__s32)5,});
253+
TEST_BTF(str, struct bpf_insn, BTF_F_NONAME, "{1,0x2,0x3,4,5,}",
254+
{.code = 1, .dst_reg = 0x2, .src_reg = 0x3, .off = 4,
255+
.imm = 5,});
256+
257+
return 0;
258+
}
259+
260+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)