Skip to content

Commit ca64eff

Browse files
mrpreKernel Patches Daemon
authored and
Kernel Patches Daemon
committed
selftests/bpf: Add edge case tests for sockmap
Add edge case tests for sockmap. Signed-off-by: Jiayuan Chen <[email protected]>
1 parent badb790 commit ca64eff

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

tools/testing/selftests/bpf/prog_tests/socket_helpers.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,22 @@ static inline int recv_timeout(int fd, void *buf, size_t len, int flags,
313313

314314
static inline int create_pair(int family, int sotype, int *p0, int *p1)
315315
{
316-
__close_fd int s, c = -1, p = -1;
316+
__close_fd int s = -1, c = -1, p = -1;
317317
struct sockaddr_storage addr;
318318
socklen_t len = sizeof(addr);
319319
int err;
320320

321+
if (family == AF_UNIX) {
322+
int fds[2];
323+
324+
err = socketpair(family, sotype, 0, fds);
325+
if (!err) {
326+
*p0 = fds[0];
327+
*p1 = fds[1];
328+
}
329+
return err;
330+
}
331+
321332
s = socket_loopback(family, sotype);
322333
if (s < 0)
323334
return s;

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

+60
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,59 @@ static void test_sockmap_vsock_unconnected(void)
10421042
xclose(map);
10431043
}
10441044

1045+
void *close_thread(void *arg)
1046+
{
1047+
int *fd = (int *)arg;
1048+
1049+
sleep(1);
1050+
close(*fd);
1051+
*fd = -1;
1052+
return NULL;
1053+
}
1054+
1055+
void test_sockmap_with_close_on_write(int family, int sotype)
1056+
{
1057+
struct test_sockmap_pass_prog *skel;
1058+
int err, map, verdict;
1059+
pthread_t tid;
1060+
int zero = 0;
1061+
int c = -1, p = -1;
1062+
1063+
skel = test_sockmap_pass_prog__open_and_load();
1064+
if (!ASSERT_OK_PTR(skel, "open_and_load"))
1065+
return;
1066+
1067+
verdict = bpf_program__fd(skel->progs.prog_skb_verdict);
1068+
map = bpf_map__fd(skel->maps.sock_map_rx);
1069+
1070+
err = bpf_prog_attach(verdict, map, BPF_SK_SKB_STREAM_VERDICT, 0);
1071+
if (!ASSERT_OK(err, "bpf_prog_attach"))
1072+
goto out;
1073+
1074+
err = create_pair(family, sotype, &c, &p);
1075+
if (!ASSERT_OK(err, "create_pair"))
1076+
goto out;
1077+
1078+
err = bpf_map_update_elem(map, &zero, &p, BPF_ANY);
1079+
if (!ASSERT_OK(err, "bpf_map_update_elem"))
1080+
goto out;
1081+
1082+
err = pthread_create(&tid, 0, close_thread, &p);
1083+
if (!ASSERT_OK(err, "pthread_create"))
1084+
goto out;
1085+
1086+
while (p > 0)
1087+
send(c, "a", 1, MSG_NOSIGNAL);
1088+
1089+
pthread_join(tid, NULL);
1090+
out:
1091+
if (c > 0)
1092+
close(c);
1093+
if (p > 0)
1094+
close(p);
1095+
test_sockmap_pass_prog__destroy(skel);
1096+
}
1097+
10451098
void test_sockmap_basic(void)
10461099
{
10471100
if (test__start_subtest("sockmap create_update_free"))
@@ -1108,4 +1161,11 @@ void test_sockmap_basic(void)
11081161
test_sockmap_skb_verdict_vsock_poll();
11091162
if (test__start_subtest("sockmap vsock unconnected"))
11101163
test_sockmap_vsock_unconnected();
1164+
if (test__start_subtest("sockmap with write on close")) {
1165+
test_sockmap_with_close_on_write(AF_UNIX, SOCK_STREAM);
1166+
test_sockmap_with_close_on_write(AF_UNIX, SOCK_DGRAM);
1167+
test_sockmap_with_close_on_write(AF_INET, SOCK_STREAM);
1168+
test_sockmap_with_close_on_write(AF_INET, SOCK_DGRAM);
1169+
test_sockmap_with_close_on_write(AF_VSOCK, SOCK_STREAM);
1170+
}
11111171
}

0 commit comments

Comments
 (0)