Skip to content

Commit e9dd904

Browse files
jrfastabAlexei Starovoitov
authored andcommitted
bpf: add tls support for testing in test_sockmap
This adds a --ktls option to test_sockmap in order to enable the combination of ktls and sockmap to run, which makes for another batch of 648 test cases for both in combination. Signed-off-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent d3b18ad commit e9dd904

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

tools/testing/selftests/bpf/test_sockmap.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ int txmsg_start;
7171
int txmsg_end;
7272
int txmsg_ingress;
7373
int txmsg_skb;
74+
int ktls;
7475

7576
static const struct option long_options[] = {
7677
{"help", no_argument, NULL, 'h' },
@@ -92,6 +93,7 @@ static const struct option long_options[] = {
9293
{"txmsg_end", required_argument, NULL, 'e'},
9394
{"txmsg_ingress", no_argument, &txmsg_ingress, 1 },
9495
{"txmsg_skb", no_argument, &txmsg_skb, 1 },
96+
{"ktls", no_argument, &ktls, 1 },
9597
{0, 0, NULL, 0 }
9698
};
9799

@@ -112,6 +114,76 @@ static void usage(char *argv[])
112114
printf("\n");
113115
}
114116

117+
#define TCP_ULP 31
118+
#define TLS_TX 1
119+
#define TLS_RX 2
120+
#include <linux/tls.h>
121+
122+
char *sock_to_string(int s)
123+
{
124+
if (s == c1)
125+
return "client1";
126+
else if (s == c2)
127+
return "client2";
128+
else if (s == s1)
129+
return "server1";
130+
else if (s == s2)
131+
return "server2";
132+
else if (s == p1)
133+
return "peer1";
134+
else if (s == p2)
135+
return "peer2";
136+
else
137+
return "unknown";
138+
}
139+
140+
static int sockmap_init_ktls(int verbose, int s)
141+
{
142+
struct tls12_crypto_info_aes_gcm_128 tls_tx = {
143+
.info = {
144+
.version = TLS_1_2_VERSION,
145+
.cipher_type = TLS_CIPHER_AES_GCM_128,
146+
},
147+
};
148+
struct tls12_crypto_info_aes_gcm_128 tls_rx = {
149+
.info = {
150+
.version = TLS_1_2_VERSION,
151+
.cipher_type = TLS_CIPHER_AES_GCM_128,
152+
},
153+
};
154+
int so_buf = 6553500;
155+
int err;
156+
157+
err = setsockopt(s, 6, TCP_ULP, "tls", sizeof("tls"));
158+
if (err) {
159+
fprintf(stderr, "setsockopt: TCP_ULP(%s) failed with error %i\n", sock_to_string(s), err);
160+
return -EINVAL;
161+
}
162+
err = setsockopt(s, SOL_TLS, TLS_TX, (void *)&tls_tx, sizeof(tls_tx));
163+
if (err) {
164+
fprintf(stderr, "setsockopt: TLS_TX(%s) failed with error %i\n", sock_to_string(s), err);
165+
return -EINVAL;
166+
}
167+
err = setsockopt(s, SOL_TLS, TLS_RX, (void *)&tls_rx, sizeof(tls_rx));
168+
if (err) {
169+
fprintf(stderr, "setsockopt: TLS_RX(%s) failed with error %i\n", sock_to_string(s), err);
170+
return -EINVAL;
171+
}
172+
err = setsockopt(s, SOL_SOCKET, SO_SNDBUF, &so_buf, sizeof(so_buf));
173+
if (err) {
174+
fprintf(stderr, "setsockopt: (%s) failed sndbuf with error %i\n", sock_to_string(s), err);
175+
return -EINVAL;
176+
}
177+
err = setsockopt(s, SOL_SOCKET, SO_RCVBUF, &so_buf, sizeof(so_buf));
178+
if (err) {
179+
fprintf(stderr, "setsockopt: (%s) failed rcvbuf with error %i\n", sock_to_string(s), err);
180+
return -EINVAL;
181+
}
182+
183+
if (verbose)
184+
fprintf(stdout, "socket(%s) kTLS enabled\n", sock_to_string(s));
185+
return 0;
186+
}
115187
static int sockmap_init_sockets(int verbose)
116188
{
117189
int i, err, one = 1;
@@ -456,6 +528,21 @@ static int sendmsg_test(struct sockmap_options *opt)
456528
else
457529
rx_fd = p2;
458530

531+
if (ktls) {
532+
/* Redirecting into non-TLS socket which sends into a TLS
533+
* socket is not a valid test. So in this case lets not
534+
* enable kTLS but still run the test.
535+
*/
536+
if (!txmsg_redir || (txmsg_redir && txmsg_ingress)) {
537+
err = sockmap_init_ktls(opt->verbose, rx_fd);
538+
if (err)
539+
return err;
540+
}
541+
err = sockmap_init_ktls(opt->verbose, c1);
542+
if (err)
543+
return err;
544+
}
545+
459546
rxpid = fork();
460547
if (rxpid == 0) {
461548
if (opt->drop_expected)
@@ -907,6 +994,8 @@ static void test_options(char *options)
907994
strncat(options, "ingress,", OPTSTRING);
908995
if (txmsg_skb)
909996
strncat(options, "skb,", OPTSTRING);
997+
if (ktls)
998+
strncat(options, "ktls,", OPTSTRING);
910999
}
9111000

9121001
static int __test_exec(int cgrp, int test, struct sockmap_options *opt)

0 commit comments

Comments
 (0)