-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test that exercises a basic sockmap / sockhash copy using bpf_i…
…ter. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> --- .../selftests/bpf/prog_tests/sockmap_basic.c | 88 +++++++++++++++++++ tools/testing/selftests/bpf/progs/bpf_iter.h | 9 ++ .../selftests/bpf/progs/bpf_iter_sockmap.c | 57 ++++++++++++ .../selftests/bpf/progs/bpf_iter_sockmap.h | 3 + 4 files changed, 157 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_sockmap.c create mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_sockmap.h
- Loading branch information
Showing
4 changed files
with
157 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
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
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,57 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2020 Cloudflare */ | ||
#include "bpf_iter.h" | ||
#include "bpf_tracing_net.h" | ||
#include "bpf_iter_sockmap.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
#include <errno.h> | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_SOCKMAP); | ||
__uint(max_entries, SOCKMAP_MAX_ENTRIES); | ||
__type(key, __u32); | ||
__type(value, __u64); | ||
} sockmap SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_SOCKHASH); | ||
__uint(max_entries, SOCKMAP_MAX_ENTRIES); | ||
__type(key, __u32); | ||
__type(value, __u64); | ||
} sockhash SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_SOCKHASH); | ||
__uint(max_entries, SOCKMAP_MAX_ENTRIES); | ||
__type(key, __u32); | ||
__type(value, __u64); | ||
} dst SEC(".maps"); | ||
|
||
__u32 elems = 0; | ||
|
||
SEC("iter/sockmap") | ||
int copy_sockmap(struct bpf_iter__sockmap *ctx) | ||
{ | ||
struct sock *sk = ctx->sk; | ||
__u32 tmp, *key = ctx->key; | ||
int ret; | ||
|
||
if (!key) | ||
return 0; | ||
|
||
elems++; | ||
|
||
/* We need a temporary buffer on the stack, since the verifier doesn't | ||
* let us use the pointer from the context as an argument to the helper. | ||
*/ | ||
tmp = *key; | ||
|
||
if (sk) | ||
return bpf_map_update_elem(&dst, &tmp, sk, 0) != 0; | ||
|
||
ret = bpf_map_delete_elem(&dst, &tmp); | ||
return ret && ret != -ENOENT; | ||
} |
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,3 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
#define SOCKMAP_MAX_ENTRIES (64) |