-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add byte swapping selftest
Add simple selftest validating byte swap built-ins and compile-time macros. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200630152125.3631920-3-andriin@fb.com
- Loading branch information
Andrii Nakryiko
authored and
Daniel Borkmann
committed
Jul 1, 2020
1 parent
30ad688
commit 8c18311
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
|
||
#include <test_progs.h> | ||
#include "test_endian.skel.h" | ||
|
||
static int duration; | ||
|
||
#define IN16 0x1234 | ||
#define IN32 0x12345678U | ||
#define IN64 0x123456789abcdef0ULL | ||
|
||
#define OUT16 0x3412 | ||
#define OUT32 0x78563412U | ||
#define OUT64 0xf0debc9a78563412ULL | ||
|
||
void test_endian(void) | ||
{ | ||
struct test_endian* skel; | ||
struct test_endian__bss *bss; | ||
int err; | ||
|
||
skel = test_endian__open_and_load(); | ||
if (CHECK(!skel, "skel_open", "failed to open skeleton\n")) | ||
return; | ||
bss = skel->bss; | ||
|
||
bss->in16 = IN16; | ||
bss->in32 = IN32; | ||
bss->in64 = IN64; | ||
|
||
err = test_endian__attach(skel); | ||
if (CHECK(err, "skel_attach", "skeleton attach failed: %d\n", err)) | ||
goto cleanup; | ||
|
||
usleep(1); | ||
|
||
CHECK(bss->out16 != OUT16, "out16", "got 0x%llx != exp 0x%llx\n", | ||
(__u64)bss->out16, (__u64)OUT16); | ||
CHECK(bss->out32 != OUT32, "out32", "got 0x%llx != exp 0x%llx\n", | ||
(__u64)bss->out32, (__u64)OUT32); | ||
CHECK(bss->out64 != OUT64, "out16", "got 0x%llx != exp 0x%llx\n", | ||
(__u64)bss->out64, (__u64)OUT64); | ||
|
||
CHECK(bss->const16 != OUT16, "const16", "got 0x%llx != exp 0x%llx\n", | ||
(__u64)bss->const16, (__u64)OUT16); | ||
CHECK(bss->const32 != OUT32, "const32", "got 0x%llx != exp 0x%llx\n", | ||
(__u64)bss->const32, (__u64)OUT32); | ||
CHECK(bss->const64 != OUT64, "const64", "got 0x%llx != exp 0x%llx\n", | ||
(__u64)bss->const64, (__u64)OUT64); | ||
cleanup: | ||
test_endian__destroy(skel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Facebook */ | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_endian.h> | ||
|
||
#define IN16 0x1234 | ||
#define IN32 0x12345678U | ||
#define IN64 0x123456789abcdef0ULL | ||
|
||
__u16 in16 = 0; | ||
__u32 in32 = 0; | ||
__u64 in64 = 0; | ||
|
||
__u16 out16 = 0; | ||
__u32 out32 = 0; | ||
__u64 out64 = 0; | ||
|
||
__u16 const16 = 0; | ||
__u32 const32 = 0; | ||
__u64 const64 = 0; | ||
|
||
SEC("raw_tp/sys_enter") | ||
int sys_enter(const void *ctx) | ||
{ | ||
out16 = __builtin_bswap16(in16); | ||
out32 = __builtin_bswap32(in32); | ||
out64 = __builtin_bswap64(in64); | ||
const16 = ___bpf_swab16(IN16); | ||
const32 = ___bpf_swab32(IN32); | ||
const64 = ___bpf_swab64(IN64); | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; |