Skip to content

Commit ee48325

Browse files
lmbkernel-patches-bot
authored andcommitted
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: Yonghong Song <yhs@fb.com> Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Acked-by: Yonghong Song <yhs@fb.com>
1 parent 9afb72e commit ee48325

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "test_sockmap_invalid_update.skel.h"
99
#include "bpf_iter_sockmap.skel.h"
1010

11-
#include "progs/bpf_iter_sockmap.h"
12-
1311
#define TCP_REPAIR 19 /* TCP sock is under repair right now */
1412

1513
#define TCP_REPAIR_ON 1
@@ -201,9 +199,9 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
201199
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
202200
int err, len, src_fd, iter_fd, duration = 0;
203201
union bpf_iter_link_info linfo = {0};
204-
__s64 sock_fd[SOCKMAP_MAX_ENTRIES];
205-
__u32 i, num_sockets, max_elems;
202+
__u32 i, num_sockets, num_elems;
206203
struct bpf_iter_sockmap *skel;
204+
__s64 *sock_fd = NULL;
207205
struct bpf_link *link;
208206
struct bpf_map *src;
209207
char buf[64];
@@ -212,22 +210,23 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
212210
if (CHECK(!skel, "bpf_iter_sockmap__open_and_load", "skeleton open_and_load failed\n"))
213211
return;
214212

215-
for (i = 0; i < ARRAY_SIZE(sock_fd); i++)
216-
sock_fd[i] = -1;
217-
218-
/* Make sure we have at least one "empty" entry to test iteration of
219-
* an empty slot.
220-
*/
221-
num_sockets = ARRAY_SIZE(sock_fd) - 1;
222-
223213
if (map_type == BPF_MAP_TYPE_SOCKMAP) {
224214
src = skel->maps.sockmap;
225-
max_elems = bpf_map__max_entries(src);
215+
num_elems = bpf_map__max_entries(src);
216+
num_sockets = num_elems - 1;
226217
} else {
227218
src = skel->maps.sockhash;
228-
max_elems = num_sockets;
219+
num_elems = bpf_map__max_entries(src) - 1;
220+
num_sockets = num_elems;
229221
}
230222

223+
sock_fd = calloc(num_sockets, sizeof(*sock_fd));
224+
if (CHECK(!sock_fd, "calloc(sock_fd)", "failed to allocate\n"))
225+
goto out;
226+
227+
for (i = 0; i < num_sockets; i++)
228+
sock_fd[i] = -1;
229+
231230
src_fd = bpf_map__fd(src);
232231

233232
for (i = 0; i < num_sockets; i++) {
@@ -258,8 +257,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
258257
goto close_iter;
259258

260259
/* test results */
261-
if (CHECK(skel->bss->elems != max_elems, "elems", "got %u expected %u\n",
262-
skel->bss->elems, max_elems))
260+
if (CHECK(skel->bss->elems != num_elems, "elems", "got %u expected %u\n",
261+
skel->bss->elems, num_elems))
263262
goto close_iter;
264263

265264
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)
271270
free_link:
272271
bpf_link__destroy(link);
273272
out:
274-
for (i = 0; i < num_sockets; i++) {
273+
for (i = 0; sock_fd && i < num_sockets; i++)
275274
if (sock_fd[i] >= 0)
276275
close(sock_fd[i]);
277-
}
276+
if (sock_fd)
277+
free(sock_fd);
278278
bpf_iter_sockmap__destroy(skel);
279279
}
280280

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/* Copyright (c) 2020 Cloudflare */
33
#include "bpf_iter.h"
44
#include "bpf_tracing_net.h"
5-
#include "bpf_iter_sockmap.h"
65
#include <bpf/bpf_helpers.h>
76
#include <bpf/bpf_tracing.h>
87
#include <errno.h>
@@ -11,14 +10,14 @@ char _license[] SEC("license") = "GPL";
1110

1211
struct {
1312
__uint(type, BPF_MAP_TYPE_SOCKMAP);
14-
__uint(max_entries, SOCKMAP_MAX_ENTRIES);
13+
__uint(max_entries, 64);
1514
__type(key, __u32);
1615
__type(value, __u64);
1716
} sockmap SEC(".maps");
1817

1918
struct {
2019
__uint(type, BPF_MAP_TYPE_SOCKHASH);
21-
__uint(max_entries, SOCKMAP_MAX_ENTRIES);
20+
__uint(max_entries, 64);
2221
__type(key, __u32);
2322
__type(value, __u64);
2423
} sockhash SEC(".maps");

tools/testing/selftests/bpf/progs/bpf_iter_sockmap.h

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)