Skip to content

Commit 34da872

Browse files
FlorentRevestborkmann
authored andcommitted
selftests/bpf: Test bpf_sk_storage_get in tcp iterators
This extends the existing bpf_sk_storage_get test where a socket is created and tagged with its creator's pid by a task_file iterator. A TCP iterator is now also used at the end of the test to negate the values already stored in the local storage. The test therefore expects -getpid() to be stored in the local storage. Signed-off-by: Florent Revest <revest@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Yonghong Song <yhs@fb.com> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20201204113609.1850150-6-revest@google.com
1 parent bd9b327 commit 34da872

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_iter.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,8 @@ static void test_bpf_sk_storage_delete(void)
978978
/* This creates a socket and its local storage. It then runs a task_iter BPF
979979
* program that replaces the existing socket local storage with the tgid of the
980980
* only task owning a file descriptor to this socket, this process, prog_tests.
981+
* It then runs a tcp socket iterator that negates the value in the existing
982+
* socket local storage, the test verifies that the resulting value is -pid.
981983
*/
982984
static void test_bpf_sk_storage_get(void)
983985
{
@@ -994,6 +996,10 @@ static void test_bpf_sk_storage_get(void)
994996
if (CHECK(sock_fd < 0, "socket", "errno: %d\n", errno))
995997
goto out;
996998

999+
err = listen(sock_fd, 1);
1000+
if (CHECK(err != 0, "listen", "errno: %d\n", errno))
1001+
goto close_socket;
1002+
9971003
map_fd = bpf_map__fd(skel->maps.sk_stg_map);
9981004

9991005
err = bpf_map_update_elem(map_fd, &sock_fd, &val, BPF_NOEXIST);
@@ -1003,9 +1009,17 @@ static void test_bpf_sk_storage_get(void)
10031009
do_dummy_read(skel->progs.fill_socket_owner);
10041010

10051011
err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
1006-
CHECK(err || val != getpid(), "bpf_map_lookup_elem",
1012+
if (CHECK(err || val != getpid(), "bpf_map_lookup_elem",
1013+
"map value wasn't set correctly (expected %d, got %d, err=%d)\n",
1014+
getpid(), val, err))
1015+
goto close_socket;
1016+
1017+
do_dummy_read(skel->progs.negate_socket_local_storage);
1018+
1019+
err = bpf_map_lookup_elem(map_fd, &sock_fd, &val);
1020+
CHECK(err || val != -getpid(), "bpf_map_lookup_elem",
10071021
"map value wasn't set correctly (expected %d, got %d, err=%d)\n",
1008-
getpid(), val, err);
1022+
-getpid(), val, err);
10091023

10101024
close_socket:
10111025
close(sock_fd);

tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_helpers.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,21 @@ int fill_socket_owner(struct bpf_iter__task_file *ctx)
4545

4646
return 0;
4747
}
48+
49+
SEC("iter/tcp")
50+
int negate_socket_local_storage(struct bpf_iter__tcp *ctx)
51+
{
52+
struct sock_common *sk_common = ctx->sk_common;
53+
int *sock_tgid;
54+
55+
if (!sk_common)
56+
return 0;
57+
58+
sock_tgid = bpf_sk_storage_get(&sk_stg_map, sk_common, 0, 0);
59+
if (!sock_tgid)
60+
return 0;
61+
62+
*sock_tgid = -*sock_tgid;
63+
64+
return 0;
65+
}

0 commit comments

Comments
 (0)