3
3
/*
4
4
* Tests for sockmap/sockhash holding kTLS sockets.
5
5
*/
6
-
6
+ #include <error.h>
7
7
#include <netinet/tcp.h>
8
+ #include <linux/tls.h>
8
9
#include "test_progs.h"
10
+ #include "sockmap_helpers.h"
11
+ #include "test_skmsg_load_helpers.skel.h"
12
+ #include "test_sockmap_ktls.skel.h"
9
13
10
14
#define MAX_TEST_NAME 80
11
15
#define TCP_ULP 31
12
16
17
+ static int init_ktls_pairs (int c , int p )
18
+ {
19
+ int err ;
20
+ struct tls12_crypto_info_aes_gcm_128 crypto_rx ;
21
+ struct tls12_crypto_info_aes_gcm_128 crypto_tx ;
22
+
23
+ err = setsockopt (c , IPPROTO_TCP , TCP_ULP , "tls" , strlen ("tls" ));
24
+ if (!ASSERT_OK (err , "setsockopt(TCP_ULP)" ))
25
+ goto out ;
26
+
27
+ err = setsockopt (p , IPPROTO_TCP , TCP_ULP , "tls" , strlen ("tls" ));
28
+ if (!ASSERT_OK (err , "setsockopt(TCP_ULP)" ))
29
+ goto out ;
30
+
31
+ memset (& crypto_rx , 0 , sizeof (crypto_rx ));
32
+ memset (& crypto_tx , 0 , sizeof (crypto_tx ));
33
+ crypto_rx .info .version = TLS_1_2_VERSION ;
34
+ crypto_tx .info .version = TLS_1_2_VERSION ;
35
+ crypto_rx .info .cipher_type = TLS_CIPHER_AES_GCM_128 ;
36
+ crypto_tx .info .cipher_type = TLS_CIPHER_AES_GCM_128 ;
37
+
38
+ err = setsockopt (c , SOL_TLS , TLS_TX , & crypto_tx , sizeof (crypto_tx ));
39
+ if (!ASSERT_OK (err , "setsockopt(TLS_TX)" ))
40
+ goto out ;
41
+
42
+ err = setsockopt (p , SOL_TLS , TLS_RX , & crypto_rx , sizeof (crypto_rx ));
43
+ if (!ASSERT_OK (err , "setsockopt(TLS_RX)" ))
44
+ goto out ;
45
+ return 0 ;
46
+ out :
47
+ return -1 ;
48
+ }
49
+
50
+ static int create_ktls_pairs (int family , int sotype , int * c , int * p )
51
+ {
52
+ int err ;
53
+
54
+ err = create_pair (family , sotype , c , p );
55
+ if (!ASSERT_OK (err , "create_pair()" ))
56
+ return -1 ;
57
+
58
+ err = init_ktls_pairs (* c , * p );
59
+ if (!ASSERT_OK (err , "init_ktls_pairs(c, p)" ))
60
+ return -1 ;
61
+ return 0 ;
62
+ }
63
+
13
64
static int tcp_server (int family )
14
65
{
15
66
int err , s ;
@@ -146,6 +197,115 @@ static const char *fmt_test_name(const char *subtest_name, int family,
146
197
return test_name ;
147
198
}
148
199
200
+ static void test_sockmap_ktls_offload (int family , int sotype )
201
+ {
202
+ int err ;
203
+ int c = 0 , p = 0 , sent , recvd ;
204
+ char msg [12 ] = "hello world\0" ;
205
+ char rcv [13 ];
206
+
207
+ err = create_ktls_pairs (family , sotype , & c , & p );
208
+ if (!ASSERT_OK (err , "create_ktls_pairs()" ))
209
+ goto out ;
210
+
211
+ sent = send (c , msg , sizeof (msg ), 0 );
212
+ if (!ASSERT_OK (err , "send(msg)" ))
213
+ goto out ;
214
+
215
+ recvd = recv (p , rcv , sizeof (rcv ), 0 );
216
+ if (!ASSERT_OK (err , "recv(msg)" ) ||
217
+ !ASSERT_EQ (recvd , sent , "length mismatch" ))
218
+ goto out ;
219
+
220
+ ASSERT_OK (memcmp (msg , rcv , sizeof (msg )), "data mismatch" );
221
+
222
+ out :
223
+ if (c )
224
+ close (c );
225
+ if (p )
226
+ close (p );
227
+ }
228
+
229
+ static void test_sockmap_ktls_tx_cork (int family , int sotype , bool push )
230
+ {
231
+ int err , off ;
232
+ int i , j ;
233
+ int start_push = 0 , push_len = 0 ;
234
+ int c = 0 , p = 0 , one = 1 , sent , recvd ;
235
+ int prog_fd , map_fd ;
236
+ char msg [12 ] = "hello world\0" ;
237
+ char rcv [20 ] = {0 };
238
+ struct test_sockmap_ktls * skel ;
239
+
240
+ skel = test_sockmap_ktls__open_and_load ();
241
+ if (!ASSERT_TRUE (skel , "open ktls skel" ))
242
+ return ;
243
+
244
+ err = create_pair (family , sotype , & c , & p );
245
+ if (!ASSERT_OK (err , "create_pair()" ))
246
+ goto out ;
247
+
248
+ prog_fd = bpf_program__fd (skel -> progs .prog_sk_policy );
249
+ map_fd = bpf_map__fd (skel -> maps .sock_map );
250
+
251
+ err = bpf_prog_attach (prog_fd , map_fd , BPF_SK_MSG_VERDICT , 0 );
252
+ if (!ASSERT_OK (err , "bpf_prog_attach sk msg" ))
253
+ goto out ;
254
+
255
+ err = bpf_map_update_elem (map_fd , & one , & c , BPF_NOEXIST );
256
+ if (!ASSERT_OK (err , "bpf_map_update_elem(c)" ))
257
+ goto out ;
258
+
259
+ err = init_ktls_pairs (c , p );
260
+ if (!ASSERT_OK (err , "init_ktls_pairs(c, p)" ))
261
+ goto out ;
262
+
263
+ skel -> bss -> cork_byte = sizeof (msg );
264
+ if (push ) {
265
+ start_push = 1 ;
266
+ push_len = 2 ;
267
+ }
268
+ skel -> bss -> push_start = start_push ;
269
+ skel -> bss -> push_end = push_len ;
270
+
271
+ off = sizeof (msg ) / 2 ;
272
+ sent = send (c , msg , off , 0 );
273
+ if (!ASSERT_EQ (sent , off , "send(msg)" ))
274
+ goto out ;
275
+
276
+ recvd = recv_timeout (p , rcv , sizeof (rcv ), MSG_DONTWAIT , 1 );
277
+ if (!ASSERT_EQ (-1 , recvd , "expected no data" ))
278
+ goto out ;
279
+
280
+ /* send remaining msg */
281
+ sent = send (c , msg + off , sizeof (msg ) - off , 0 );
282
+ if (!ASSERT_EQ (sent , sizeof (msg ) - off , "send remaining data" ))
283
+ goto out ;
284
+
285
+ recvd = recv_timeout (p , rcv , sizeof (rcv ), MSG_DONTWAIT , 1 );
286
+ if (!ASSERT_OK (err , "recv(msg)" ) ||
287
+ !ASSERT_EQ (recvd , sizeof (msg ) + push_len , "check length mismatch" ))
288
+ goto out ;
289
+
290
+ for (i = 0 , j = 0 ; i < recvd ;) {
291
+ /* skip checking the data that has been pushed in */
292
+ if (i >= start_push && i <= start_push + push_len - 1 ) {
293
+ i ++ ;
294
+ continue ;
295
+ }
296
+ if (!ASSERT_EQ (rcv [i ], msg [j ], "data mismatch" ))
297
+ goto out ;
298
+ i ++ ;
299
+ j ++ ;
300
+ }
301
+ out :
302
+ if (c )
303
+ close (c );
304
+ if (p )
305
+ close (p );
306
+ test_sockmap_ktls__destroy (skel );
307
+ }
308
+
149
309
static void run_tests (int family , enum bpf_map_type map_type )
150
310
{
151
311
int map ;
@@ -162,10 +322,22 @@ static void run_tests(int family, enum bpf_map_type map_type)
162
322
close (map );
163
323
}
164
324
325
+ static void run_ktls_test (int family , int sotype )
326
+ {
327
+ if (test__start_subtest ("tls simple offload" ))
328
+ test_sockmap_ktls_offload (family , sotype );
329
+ if (test__start_subtest ("tls tx cork" ))
330
+ test_sockmap_ktls_tx_cork (family , sotype , false);
331
+ if (test__start_subtest ("tls tx cork with push" ))
332
+ test_sockmap_ktls_tx_cork (family , sotype , true);
333
+ }
334
+
165
335
void test_sockmap_ktls (void )
166
336
{
167
337
run_tests (AF_INET , BPF_MAP_TYPE_SOCKMAP );
168
338
run_tests (AF_INET , BPF_MAP_TYPE_SOCKHASH );
169
339
run_tests (AF_INET6 , BPF_MAP_TYPE_SOCKMAP );
170
340
run_tests (AF_INET6 , BPF_MAP_TYPE_SOCKHASH );
341
+ run_ktls_test (AF_INET , SOCK_STREAM );
342
+ run_ktls_test (AF_INET6 , SOCK_STREAM );
171
343
}
0 commit comments