Skip to content

Commit f30be94

Browse files
lmbkernel-patches-bot
authored andcommitted
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 <[email protected]> Signed-off-by: Lorenz Bauer <[email protected]> --- .../selftests/bpf/prog_tests/sockmap_basic.c | 39 ++++++++++--------- .../selftests/bpf/progs/bpf_iter_sockmap.c | 5 +-- .../selftests/bpf/progs/bpf_iter_sockmap.h | 3 -- 3 files changed, 23 insertions(+), 24 deletions(-) delete mode 100644 tools/testing/selftests/bpf/progs/bpf_iter_sockmap.h
1 parent a4028b3 commit f30be94

File tree

3 files changed

+23
-24
lines changed

3 files changed

+23
-24
lines changed

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

Lines changed: 21 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
@@ -179,9 +177,9 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
179177
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
180178
int err, len, src_fd, iter_fd, duration = 0;
181179
union bpf_iter_link_info linfo = {0};
182-
__s64 sock_fd[SOCKMAP_MAX_ENTRIES];
183-
__u32 i, num_sockets, max_elems;
180+
__u32 i, num_sockets, num_elems;
184181
struct bpf_iter_sockmap *skel;
182+
__s64 *sock_fd = NULL;
185183
struct bpf_link *link;
186184
struct bpf_map *src;
187185
char buf[64];
@@ -190,22 +188,26 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
190188
if (CHECK(!skel, "bpf_iter_sockmap__open_and_load", "skeleton open_and_load failed\n"))
191189
return;
192190

193-
for (i = 0; i < ARRAY_SIZE(sock_fd); i++)
194-
sock_fd[i] = -1;
195-
196-
/* Make sure we have at least one "empty" entry to test iteration of
197-
* an empty slot.
198-
*/
199-
num_sockets = ARRAY_SIZE(sock_fd) - 1;
200-
201191
if (map_type == BPF_MAP_TYPE_SOCKMAP) {
202192
src = skel->maps.sockmap;
203-
max_elems = bpf_map__max_entries(src);
193+
num_elems = bpf_map__max_entries(src);
204194
} else {
205195
src = skel->maps.sockhash;
206-
max_elems = num_sockets;
196+
num_elems = bpf_map__max_entries(src) - 1;
207197
}
208198

199+
/* Make sure we have at least one "empty" entry to test iteration of
200+
* an empty slot.
201+
*/
202+
num_sockets = bpf_map__max_entries(src) - 1;
203+
204+
sock_fd = calloc(num_sockets, sizeof(*sock_fd));
205+
if (CHECK(!sock_fd, "calloc(sock_fd)", "failed to allocate\n"))
206+
goto out;
207+
208+
for (i = 0; i < num_sockets; i++)
209+
sock_fd[i] = -1;
210+
209211
src_fd = bpf_map__fd(src);
210212

211213
for (i = 0; i < num_sockets; i++) {
@@ -236,8 +238,8 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
236238
goto close_iter;
237239

238240
/* test results */
239-
if (CHECK(skel->bss->elems != max_elems, "elems", "got %u expected %u\n",
240-
skel->bss->elems, max_elems))
241+
if (CHECK(skel->bss->elems != num_elems, "elems", "got %u expected %u\n",
242+
skel->bss->elems, num_elems))
241243
goto close_iter;
242244

243245
if (CHECK(skel->bss->socks != num_sockets, "socks", "got %u expected %u\n",
@@ -249,10 +251,11 @@ static void test_sockmap_iter(enum bpf_map_type map_type)
249251
free_link:
250252
bpf_link__destroy(link);
251253
out:
252-
for (i = 0; i < num_sockets; i++) {
254+
for (i = 0; sock_fd && i < num_sockets; i++)
253255
if (sock_fd[i] >= 0)
254256
close(sock_fd[i]);
255-
}
257+
if (sock_fd)
258+
free(sock_fd);
256259
bpf_iter_sockmap__destroy(skel);
257260
}
258261

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)