Skip to content

Commit 8c18311

Browse files
anakryikoborkmann
authored andcommitted
selftests/bpf: Add byte swapping selftest
Add simple selftest validating byte swap built-ins and compile-time macros. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 30ad688 commit 8c18311

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2020 Facebook */
3+
4+
#include <test_progs.h>
5+
#include "test_endian.skel.h"
6+
7+
static int duration;
8+
9+
#define IN16 0x1234
10+
#define IN32 0x12345678U
11+
#define IN64 0x123456789abcdef0ULL
12+
13+
#define OUT16 0x3412
14+
#define OUT32 0x78563412U
15+
#define OUT64 0xf0debc9a78563412ULL
16+
17+
void test_endian(void)
18+
{
19+
struct test_endian* skel;
20+
struct test_endian__bss *bss;
21+
int err;
22+
23+
skel = test_endian__open_and_load();
24+
if (CHECK(!skel, "skel_open", "failed to open skeleton\n"))
25+
return;
26+
bss = skel->bss;
27+
28+
bss->in16 = IN16;
29+
bss->in32 = IN32;
30+
bss->in64 = IN64;
31+
32+
err = test_endian__attach(skel);
33+
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err))
34+
goto cleanup;
35+
36+
usleep(1);
37+
38+
CHECK(bss->out16 != OUT16, "out16", "got 0x%llx != exp 0x%llx\n",
39+
(__u64)bss->out16, (__u64)OUT16);
40+
CHECK(bss->out32 != OUT32, "out32", "got 0x%llx != exp 0x%llx\n",
41+
(__u64)bss->out32, (__u64)OUT32);
42+
CHECK(bss->out64 != OUT64, "out16", "got 0x%llx != exp 0x%llx\n",
43+
(__u64)bss->out64, (__u64)OUT64);
44+
45+
CHECK(bss->const16 != OUT16, "const16", "got 0x%llx != exp 0x%llx\n",
46+
(__u64)bss->const16, (__u64)OUT16);
47+
CHECK(bss->const32 != OUT32, "const32", "got 0x%llx != exp 0x%llx\n",
48+
(__u64)bss->const32, (__u64)OUT32);
49+
CHECK(bss->const64 != OUT64, "const64", "got 0x%llx != exp 0x%llx\n",
50+
(__u64)bss->const64, (__u64)OUT64);
51+
cleanup:
52+
test_endian__destroy(skel);
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2020 Facebook */
3+
4+
#include "vmlinux.h"
5+
#include <bpf/bpf_helpers.h>
6+
#include <bpf/bpf_endian.h>
7+
8+
#define IN16 0x1234
9+
#define IN32 0x12345678U
10+
#define IN64 0x123456789abcdef0ULL
11+
12+
__u16 in16 = 0;
13+
__u32 in32 = 0;
14+
__u64 in64 = 0;
15+
16+
__u16 out16 = 0;
17+
__u32 out32 = 0;
18+
__u64 out64 = 0;
19+
20+
__u16 const16 = 0;
21+
__u32 const32 = 0;
22+
__u64 const64 = 0;
23+
24+
SEC("raw_tp/sys_enter")
25+
int sys_enter(const void *ctx)
26+
{
27+
out16 = __builtin_bswap16(in16);
28+
out32 = __builtin_bswap32(in32);
29+
out64 = __builtin_bswap64(in64);
30+
const16 = ___bpf_swab16(IN16);
31+
const32 = ___bpf_swab32(IN32);
32+
const64 = ___bpf_swab64(IN64);
33+
34+
return 0;
35+
}
36+
37+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)