Will be used to filter out netdev in AF_XDP metadata selftests. The helper returns netns cookie of the current process. Signed-off-by: Stanislav Fomichev <sdf@xxxxxxxxxx> --- tools/testing/selftests/bpf/network_helpers.c | 21 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 22 insertions(+) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index a105c0cd008a..34102fce5a88 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -450,3 +450,24 @@ int get_socket_local_port(int sock_fd) return -1; } + +#ifndef SO_NETNS_COOKIE +#define SO_NETNS_COOKIE 71 +#endif + +__u64 get_net_cookie(void) +{ + socklen_t optlen; + __u64 optval = 0; + int fd; + + fd = socket(AF_LOCAL, SOCK_DGRAM, 0); + if (fd >= 0) { + optlen = sizeof(optval); + getsockopt(fd, SOL_SOCKET, SO_NETNS_COOKIE, &optval, &optlen); + close(fd); + } + + return optval; +} + diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 694185644da6..380047161aac 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -57,6 +57,7 @@ int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len); char *ping_command(int family); int get_socket_local_port(int sock_fd); +__u64 get_net_cookie(void); struct nstoken; /** -- 2.41.0.255.g8b1d071c50-goog