Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

authored by

Florent Revest and committed by
Daniel Borkmann
34da8721 bd9b327e

+34 -2
+16 -2
tools/testing/selftests/bpf/prog_tests/bpf_iter.c
··· 978 978 /* This creates a socket and its local storage. It then runs a task_iter BPF 979 979 * program that replaces the existing socket local storage with the tgid of the 980 980 * 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. 981 983 */ 982 984 static void test_bpf_sk_storage_get(void) 983 985 { ··· 996 994 if (CHECK(sock_fd < 0, "socket", "errno: %d\n", errno)) 997 995 goto out; 998 996 997 + err = listen(sock_fd, 1); 998 + if (CHECK(err != 0, "listen", "errno: %d\n", errno)) 999 + goto close_socket; 1000 + 999 1001 map_fd = bpf_map__fd(skel->maps.sk_stg_map); 1000 1002 1001 1003 err = bpf_map_update_elem(map_fd, &sock_fd, &val, BPF_NOEXIST); ··· 1009 1003 do_dummy_read(skel->progs.fill_socket_owner); 1010 1004 1011 1005 err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); 1012 - CHECK(err || val != getpid(), "bpf_map_lookup_elem", 1006 + if (CHECK(err || val != getpid(), "bpf_map_lookup_elem", 1007 + "map value wasn't set correctly (expected %d, got %d, err=%d)\n", 1008 + getpid(), val, err)) 1009 + goto close_socket; 1010 + 1011 + do_dummy_read(skel->progs.negate_socket_local_storage); 1012 + 1013 + err = bpf_map_lookup_elem(map_fd, &sock_fd, &val); 1014 + CHECK(err || val != -getpid(), "bpf_map_lookup_elem", 1013 1015 "map value wasn't set correctly (expected %d, got %d, err=%d)\n", 1014 - getpid(), val, err); 1016 + -getpid(), val, err); 1015 1017 1016 1018 close_socket: 1017 1019 close(sock_fd);
+18
tools/testing/selftests/bpf/progs/bpf_iter_bpf_sk_storage_helpers.c
··· 45 45 46 46 return 0; 47 47 } 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 + }