1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2020 Cloudflare
4 * Tests for sockmap/sockhash holding kTLS sockets.
7 #include "test_progs.h"
9 #define MAX_TEST_NAME 80
12 static int tcp_server(int family
)
16 s
= socket(family
, SOCK_STREAM
, 0);
17 if (CHECK_FAIL(s
== -1)) {
22 err
= listen(s
, SOMAXCONN
);
23 if (CHECK_FAIL(err
)) {
31 static int disconnect(int fd
)
33 struct sockaddr unspec
= { AF_UNSPEC
};
35 return connect(fd
, &unspec
, sizeof(unspec
));
38 /* Disconnect (unhash) a kTLS socket after removing it from sockmap. */
39 static void test_sockmap_ktls_disconnect_after_delete(int family
, int map
)
41 struct sockaddr_storage addr
= {0};
42 socklen_t len
= sizeof(addr
);
43 int err
, cli
, srv
, zero
= 0;
45 srv
= tcp_server(family
);
49 err
= getsockname(srv
, (struct sockaddr
*)&addr
, &len
);
50 if (CHECK_FAIL(err
)) {
55 cli
= socket(family
, SOCK_STREAM
, 0);
56 if (CHECK_FAIL(cli
== -1)) {
61 err
= connect(cli
, (struct sockaddr
*)&addr
, len
);
62 if (CHECK_FAIL(err
)) {
67 err
= bpf_map_update_elem(map
, &zero
, &cli
, 0);
68 if (CHECK_FAIL(err
)) {
69 perror("bpf_map_update_elem");
73 err
= setsockopt(cli
, IPPROTO_TCP
, TCP_ULP
, "tls", strlen("tls"));
74 if (CHECK_FAIL(err
)) {
75 perror("setsockopt(TCP_ULP)");
79 err
= bpf_map_delete_elem(map
, &zero
);
80 if (CHECK_FAIL(err
)) {
81 perror("bpf_map_delete_elem");
85 err
= disconnect(cli
);
95 static void run_tests(int family
, enum bpf_map_type map_type
)
97 char test_name
[MAX_TEST_NAME
];
100 map
= bpf_create_map(map_type
, sizeof(int), sizeof(int), 1, 0);
101 if (CHECK_FAIL(map
== -1)) {
102 perror("bpf_map_create");
106 snprintf(test_name
, MAX_TEST_NAME
,
107 "sockmap_ktls disconnect_after_delete %s %s",
108 family
== AF_INET
? "IPv4" : "IPv6",
109 map_type
== BPF_MAP_TYPE_SOCKMAP
? "SOCKMAP" : "SOCKHASH");
110 if (!test__start_subtest(test_name
))
113 test_sockmap_ktls_disconnect_after_delete(family
, map
);
118 void test_sockmap_ktls(void)
120 run_tests(AF_INET
, BPF_MAP_TYPE_SOCKMAP
);
121 run_tests(AF_INET
, BPF_MAP_TYPE_SOCKHASH
);
122 run_tests(AF_INET6
, BPF_MAP_TYPE_SOCKMAP
);
123 run_tests(AF_INET6
, BPF_MAP_TYPE_SOCKHASH
);