Commit 27870317 authored by Lorenz Bauer's avatar Lorenz Bauer Committed by Alexei Starovoitov

selftests: bpf: Remove shared header from sockmap iter test

The shared header to define SOCKMAP_MAX_ENTRIES is a bit overkill.
Dynamically allocate the sock_fd array based on bpf_map__max_entries
instead.
Suggested-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarLorenz Bauer <lmb@cloudflare.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200928090805.23343-4-lmb@cloudflare.com
parent 26c3270d
...@@ -8,8 +8,6 @@ ...@@ -8,8 +8,6 @@
#include "test_sockmap_invalid_update.skel.h" #include "test_sockmap_invalid_update.skel.h"
#include "bpf_iter_sockmap.skel.h" #include "bpf_iter_sockmap.skel.h"
#include "progs/bpf_iter_sockmap.h"
#define TCP_REPAIR 19 /* TCP sock is under repair right now */ #define TCP_REPAIR 19 /* TCP sock is under repair right now */
#define TCP_REPAIR_ON 1 #define TCP_REPAIR_ON 1
...@@ -201,9 +199,9 @@ static void test_sockmap_iter(enum bpf_map_type map_type) ...@@ -201,9 +199,9 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
int err, len, src_fd, iter_fd, duration = 0; int err, len, src_fd, iter_fd, duration = 0;
union bpf_iter_link_info linfo = {0}; union bpf_iter_link_info linfo = {0};
__s64 sock_fd[SOCKMAP_MAX_ENTRIES]; __u32 i, num_sockets, num_elems;
__u32 i, num_sockets, max_elems;
struct bpf_iter_sockmap *skel; struct bpf_iter_sockmap *skel;
__s64 *sock_fd = NULL;
struct bpf_link *link; struct bpf_link *link;
struct bpf_map *src; struct bpf_map *src;
char buf[64]; char buf[64];
...@@ -212,22 +210,23 @@ static void test_sockmap_iter(enum bpf_map_type map_type) ...@@ -212,22 +210,23 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
if (CHECK(!skel, "bpf_iter_sockmap__open_and_load", "skeleton open_and_load failed\n")) if (CHECK(!skel, "bpf_iter_sockmap__open_and_load", "skeleton open_and_load failed\n"))
return; return;
for (i = 0; i < ARRAY_SIZE(sock_fd); i++)
sock_fd[i] = -1;
/* Make sure we have at least one "empty" entry to test iteration of
* an empty slot.
*/
num_sockets = ARRAY_SIZE(sock_fd) - 1;
if (map_type == BPF_MAP_TYPE_SOCKMAP) { if (map_type == BPF_MAP_TYPE_SOCKMAP) {
src = skel->maps.sockmap; src = skel->maps.sockmap;
max_elems = bpf_map__max_entries(src); num_elems = bpf_map__max_entries(src);
num_sockets = num_elems - 1;
} else { } else {
src = skel->maps.sockhash; src = skel->maps.sockhash;
max_elems = num_sockets; num_elems = bpf_map__max_entries(src) - 1;
num_sockets = num_elems;
} }
sock_fd = calloc(num_sockets, sizeof(*sock_fd));
if (CHECK(!sock_fd, "calloc(sock_fd)", "failed to allocate\n"))
goto out;
for (i = 0; i < num_sockets; i++)
sock_fd[i] = -1;
src_fd = bpf_map__fd(src); src_fd = bpf_map__fd(src);
for (i = 0; i < num_sockets; i++) { for (i = 0; i < num_sockets; i++) {
...@@ -258,8 +257,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type) ...@@ -258,8 +257,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
goto close_iter; goto close_iter;
/* test results */ /* test results */
if (CHECK(skel->bss->elems != max_elems, "elems", "got %u expected %u\n", if (CHECK(skel->bss->elems != num_elems, "elems", "got %u expected %u\n",
skel->bss->elems, max_elems)) skel->bss->elems, num_elems))
goto close_iter; goto close_iter;
if (CHECK(skel->bss->socks != num_sockets, "socks", "got %u expected %u\n", if (CHECK(skel->bss->socks != num_sockets, "socks", "got %u expected %u\n",
...@@ -271,10 +270,11 @@ static void test_sockmap_iter(enum bpf_map_type map_type) ...@@ -271,10 +270,11 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
free_link: free_link:
bpf_link__destroy(link); bpf_link__destroy(link);
out: out:
for (i = 0; i < num_sockets; i++) { for (i = 0; sock_fd && i < num_sockets; i++)
if (sock_fd[i] >= 0) if (sock_fd[i] >= 0)
close(sock_fd[i]); close(sock_fd[i]);
} if (sock_fd)
free(sock_fd);
bpf_iter_sockmap__destroy(skel); bpf_iter_sockmap__destroy(skel);
} }
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
/* Copyright (c) 2020 Cloudflare */ /* Copyright (c) 2020 Cloudflare */
#include "bpf_iter.h" #include "bpf_iter.h"
#include "bpf_tracing_net.h" #include "bpf_tracing_net.h"
#include "bpf_iter_sockmap.h"
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h> #include <bpf/bpf_tracing.h>
#include <errno.h> #include <errno.h>
...@@ -11,14 +10,14 @@ char _license[] SEC("license") = "GPL"; ...@@ -11,14 +10,14 @@ char _license[] SEC("license") = "GPL";
struct { struct {
__uint(type, BPF_MAP_TYPE_SOCKMAP); __uint(type, BPF_MAP_TYPE_SOCKMAP);
__uint(max_entries, SOCKMAP_MAX_ENTRIES); __uint(max_entries, 64);
__type(key, __u32); __type(key, __u32);
__type(value, __u64); __type(value, __u64);
} sockmap SEC(".maps"); } sockmap SEC(".maps");
struct { struct {
__uint(type, BPF_MAP_TYPE_SOCKHASH); __uint(type, BPF_MAP_TYPE_SOCKHASH);
__uint(max_entries, SOCKMAP_MAX_ENTRIES); __uint(max_entries, 64);
__type(key, __u32); __type(key, __u32);
__type(value, __u64); __type(value, __u64);
} sockhash SEC(".maps"); } sockhash SEC(".maps");
......
/* SPDX-License-Identifier: GPL-2.0 */
#define SOCKMAP_MAX_ENTRIES (64)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment