Skip to content

Commit d24d749

Browse files
jrifeKernel Patches Daemon
authored andcommitted
selftests/bpf: Create established sockets in socket iterator tests
Prepare for bucket resume tests for established TCP sockets by creating established sockets. Collect socket fds from connect() and accept() sides and pass them to test cases. Signed-off-by: Jordan Rife <[email protected]>
1 parent fcb0837 commit d24d749

File tree

1 file changed

+79
-4
lines changed

1 file changed

+79
-4
lines changed

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

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,66 @@ static void check_n_were_seen_once(int *fds, int fds_len, int n,
153153
ASSERT_EQ(seen_once, n, "seen_once");
154154
}
155155

156+
static int accept_from_one(int *server_fds, int server_fds_len)
157+
{
158+
int i = 0;
159+
int fd;
160+
161+
for (; i < server_fds_len; i++) {
162+
fd = accept(server_fds[i], NULL, NULL);
163+
if (fd >= 0)
164+
return fd;
165+
if (!ASSERT_EQ(errno, EWOULDBLOCK, "EWOULDBLOCK"))
166+
return -1;
167+
}
168+
169+
return -1;
170+
}
171+
172+
static int *connect_to_server(int family, int sock_type, const char *addr,
173+
__u16 port, int nr_connects, int *server_fds,
174+
int server_fds_len)
175+
{
176+
struct network_helper_opts opts = {
177+
.timeout_ms = 0,
178+
};
179+
int *established_socks;
180+
int i;
181+
182+
/* Make sure accept() doesn't block. */
183+
for (i = 0; i < server_fds_len; i++)
184+
if (!ASSERT_OK(fcntl(server_fds[i], F_SETFL, O_NONBLOCK),
185+
"fcntl(O_NONBLOCK)"))
186+
return NULL;
187+
188+
established_socks = malloc(sizeof(int) * nr_connects*2);
189+
if (!ASSERT_OK_PTR(established_socks, "established_socks"))
190+
return NULL;
191+
192+
i = 0;
193+
194+
while (nr_connects--) {
195+
established_socks[i] = connect_to_addr_str(family, sock_type,
196+
addr, port, &opts);
197+
if (!ASSERT_OK_FD(established_socks[i], "connect_to_addr_str"))
198+
goto error;
199+
i++;
200+
established_socks[i] = accept_from_one(server_fds,
201+
server_fds_len);
202+
if (!ASSERT_OK_FD(established_socks[i], "accept_from_one"))
203+
goto error;
204+
i++;
205+
}
206+
207+
return established_socks;
208+
error:
209+
free_fds(established_socks, i);
210+
return NULL;
211+
}
212+
156213
static void remove_seen(int family, int sock_type, const char *addr, __u16 port,
157-
int *socks, int socks_len, struct sock_count *counts,
214+
int *socks, int socks_len, int *established_socks,
215+
int established_socks_len, struct sock_count *counts,
158216
int counts_len, struct bpf_link *link, int iter_fd)
159217
{
160218
int close_idx;
@@ -185,6 +243,7 @@ static void remove_seen(int family, int sock_type, const char *addr, __u16 port,
185243

186244
static void remove_unseen(int family, int sock_type, const char *addr,
187245
__u16 port, int *socks, int socks_len,
246+
int *established_socks, int established_socks_len,
188247
struct sock_count *counts, int counts_len,
189248
struct bpf_link *link, int iter_fd)
190249
{
@@ -217,6 +276,7 @@ static void remove_unseen(int family, int sock_type, const char *addr,
217276

218277
static void remove_all(int family, int sock_type, const char *addr,
219278
__u16 port, int *socks, int socks_len,
279+
int *established_socks, int established_socks_len,
220280
struct sock_count *counts, int counts_len,
221281
struct bpf_link *link, int iter_fd)
222282
{
@@ -244,7 +304,8 @@ static void remove_all(int family, int sock_type, const char *addr,
244304
}
245305

246306
static void add_some(int family, int sock_type, const char *addr, __u16 port,
247-
int *socks, int socks_len, struct sock_count *counts,
307+
int *socks, int socks_len, int *established_socks,
308+
int established_socks_len, struct sock_count *counts,
248309
int counts_len, struct bpf_link *link, int iter_fd)
249310
{
250311
int *new_socks = NULL;
@@ -274,6 +335,7 @@ static void add_some(int family, int sock_type, const char *addr, __u16 port,
274335

275336
static void force_realloc(int family, int sock_type, const char *addr,
276337
__u16 port, int *socks, int socks_len,
338+
int *established_socks, int established_socks_len,
277339
struct sock_count *counts, int counts_len,
278340
struct bpf_link *link, int iter_fd)
279341
{
@@ -302,10 +364,12 @@ static void force_realloc(int family, int sock_type, const char *addr,
302364

303365
struct test_case {
304366
void (*test)(int family, int sock_type, const char *addr, __u16 port,
305-
int *socks, int socks_len, struct sock_count *counts,
367+
int *socks, int socks_len, int *established_socks,
368+
int established_socks_len, struct sock_count *counts,
306369
int counts_len, struct bpf_link *link, int iter_fd);
307370
const char *description;
308371
int ehash_buckets;
372+
int connections;
309373
int init_socks;
310374
int max_socks;
311375
int sock_type;
@@ -416,6 +480,7 @@ static void do_resume_test(struct test_case *tc)
416480
static const __u16 port = 10001;
417481
struct nstoken *nstoken = NULL;
418482
struct bpf_link *link = NULL;
483+
int *established_fds = NULL;
419484
int err, iter_fd = -1;
420485
const char *addr;
421486
int *fds = NULL;
@@ -444,6 +509,14 @@ static void do_resume_test(struct test_case *tc)
444509
tc->init_socks);
445510
if (!ASSERT_OK_PTR(fds, "start_reuseport_server"))
446511
goto done;
512+
if (tc->connections) {
513+
established_fds = connect_to_server(tc->family, tc->sock_type,
514+
addr, port,
515+
tc->connections, fds,
516+
tc->init_socks);
517+
if (!ASSERT_OK_PTR(established_fds, "connect_to_server"))
518+
goto done;
519+
}
447520
skel->rodata->ports[0] = 0;
448521
skel->rodata->ports[1] = 0;
449522
skel->rodata->sf = tc->family;
@@ -465,13 +538,15 @@ static void do_resume_test(struct test_case *tc)
465538
goto done;
466539

467540
tc->test(tc->family, tc->sock_type, addr, port, fds, tc->init_socks,
468-
counts, tc->max_socks, link, iter_fd);
541+
established_fds, tc->connections*2, counts, tc->max_socks,
542+
link, iter_fd);
469543
done:
470544
close_netns(nstoken);
471545
SYS_NOFAIL("ip netns del " TEST_CHILD_NS);
472546
SYS_NOFAIL("sysctl -w net.ipv4.tcp_child_ehash_entries=0");
473547
free(counts);
474548
free_fds(fds, tc->init_socks);
549+
free_fds(established_fds, tc->connections*2);
475550
if (iter_fd >= 0)
476551
close(iter_fd);
477552
bpf_link__destroy(link);

0 commit comments

Comments
 (0)