-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathposix.c
More file actions
2439 lines (2063 loc) · 59.1 KB
/
Copy pathposix.c
File metadata and controls
2439 lines (2063 loc) · 59.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (C) 2018 Intel Corporation. All rights reserved.
* Copyright (c) 2020, 2021 Mellanox Technologies LTD. All rights reserved.
* Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
*/
#include "spdk/stdinc.h"
#if defined(__FreeBSD__)
#include <sys/event.h>
#define SPDK_KEVENT
#else
#define SPDK_EPOLL
#endif
#if defined(__linux__)
#include <linux/errqueue.h>
#endif
#include "spdk/env.h"
#include "spdk/log.h"
#include "spdk/pipe.h"
#include "spdk/sock.h"
#include "spdk/util.h"
#include "spdk/string.h"
#include "spdk/net.h"
#include "spdk/file.h"
#include "spdk_internal/sock_module.h"
#include "spdk/net.h"
#include "openssl/crypto.h"
#include "openssl/err.h"
#include "openssl/ssl.h"
#if defined(SO_ZEROCOPY) && defined(MSG_ZEROCOPY)
#define SPDK_ZEROCOPY
#endif
struct posix_connect_ctx {
bool inprogress;
bool fd_closed;
struct addrinfo *next_res, *res0;
struct spdk_sock_opts opts;
struct spdk_sock_impl_opts impl_opts;
};
struct spdk_posix_sock {
struct spdk_sock base;
int fd;
uint32_t sendmsg_idx;
struct spdk_pipe *recv_pipe;
struct posix_connect_ctx conn_ctx;
int recv_buf_sz;
bool pipe_has_data;
bool socket_has_data;
bool zcopy;
int placement_id;
SSL_CTX *ctx;
SSL *ssl;
TAILQ_ENTRY(spdk_posix_sock) link;
TAILQ_ENTRY(spdk_posix_sock) connect_link;
char interface_name[IFNAMSIZ];
};
TAILQ_HEAD(spdk_has_data_list, spdk_posix_sock);
TAILQ_HEAD(spdk_sock_list, spdk_posix_sock);
struct spdk_posix_sock_group_impl {
struct spdk_sock_group_impl base;
int fd;
struct spdk_interrupt *intr;
struct spdk_has_data_list socks_with_data;
struct spdk_has_data_list socks_in_progress;
int placement_id;
struct spdk_pipe_group *pipe_group;
};
static struct spdk_sock_impl_opts g_posix_impl_opts = {
.recv_buf_size = DEFAULT_SO_RCVBUF_SIZE,
.send_buf_size = DEFAULT_SO_SNDBUF_SIZE,
.enable_recv_pipe = true,
.enable_quickack = false,
.enable_placement_id = PLACEMENT_NONE,
.enable_zerocopy_send_server = true,
.enable_zerocopy_send_client = false,
.zerocopy_threshold = 0,
.tls_version = 0,
.enable_ktls = false,
.psk_key = NULL,
.psk_key_size = 0,
.psk_identity = NULL,
.get_key = NULL,
.get_key_ctx = NULL,
.tls_cipher_suites = NULL
};
static struct spdk_sock_impl_opts g_ssl_impl_opts = {
.recv_buf_size = MIN_SO_RCVBUF_SIZE,
.send_buf_size = MIN_SO_SNDBUF_SIZE,
.enable_recv_pipe = true,
.enable_quickack = false,
.enable_placement_id = PLACEMENT_NONE,
.enable_zerocopy_send_server = true,
.enable_zerocopy_send_client = false,
.zerocopy_threshold = 0,
.tls_version = 0,
.enable_ktls = false,
.psk_key = NULL,
.psk_identity = NULL
};
static struct spdk_sock_map g_map = {
.entries = STAILQ_HEAD_INITIALIZER(g_map.entries),
.mtx = PTHREAD_MUTEX_INITIALIZER
};
__attribute((destructor)) static void
posix_sock_map_cleanup(void)
{
spdk_sock_map_cleanup(&g_map);
}
#define __posix_sock(sock) (struct spdk_posix_sock *)sock
#define __posix_group_impl(group) (struct spdk_posix_sock_group_impl *)group
static void
posix_sock_copy_impl_opts(struct spdk_sock_impl_opts *dest, const struct spdk_sock_impl_opts *src,
size_t len)
{
#define FIELD_OK(field) \
offsetof(struct spdk_sock_impl_opts, field) + sizeof(src->field) <= len
#define SET_FIELD(field) \
if (FIELD_OK(field)) { \
dest->field = src->field; \
}
SET_FIELD(recv_buf_size);
SET_FIELD(send_buf_size);
SET_FIELD(enable_recv_pipe);
SET_FIELD(enable_zerocopy_send);
SET_FIELD(enable_quickack);
SET_FIELD(enable_placement_id);
SET_FIELD(enable_zerocopy_send_server);
SET_FIELD(enable_zerocopy_send_client);
SET_FIELD(zerocopy_threshold);
SET_FIELD(tls_version);
SET_FIELD(enable_ktls);
SET_FIELD(psk_key);
SET_FIELD(psk_key_size);
SET_FIELD(psk_identity);
SET_FIELD(get_key);
SET_FIELD(get_key_ctx);
SET_FIELD(tls_cipher_suites);
#undef SET_FIELD
#undef FIELD_OK
}
static int
_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, struct spdk_sock_impl_opts *impl_opts,
size_t *len)
{
if (!opts || !len) {
errno = EINVAL;
return -1;
}
assert(sizeof(*opts) >= *len);
memset(opts, 0, *len);
posix_sock_copy_impl_opts(opts, impl_opts, *len);
*len = spdk_min(*len, sizeof(*impl_opts));
return 0;
}
static int
posix_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
{
return _sock_impl_get_opts(opts, &g_posix_impl_opts, len);
}
static int
ssl_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
{
return _sock_impl_get_opts(opts, &g_ssl_impl_opts, len);
}
static int
_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, struct spdk_sock_impl_opts *impl_opts,
size_t len)
{
if (!opts) {
errno = EINVAL;
return -1;
}
assert(sizeof(*opts) >= len);
posix_sock_copy_impl_opts(impl_opts, opts, len);
return 0;
}
static int
posix_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
{
return _sock_impl_set_opts(opts, &g_posix_impl_opts, len);
}
static int
ssl_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
{
return _sock_impl_set_opts(opts, &g_ssl_impl_opts, len);
}
static void
_opts_get_impl_opts(const struct spdk_sock_opts *opts, struct spdk_sock_impl_opts *dest,
const struct spdk_sock_impl_opts *default_impl)
{
/* Copy the default impl_opts first to cover cases when user's impl_opts is smaller */
memcpy(dest, default_impl, sizeof(*dest));
if (opts->impl_opts != NULL) {
assert(sizeof(*dest) >= opts->impl_opts_size);
posix_sock_copy_impl_opts(dest, opts->impl_opts, opts->impl_opts_size);
}
}
static void
posix_connect_ctx_init(struct posix_connect_ctx *ctx,
struct addrinfo *res0,
struct addrinfo *res,
struct spdk_sock_opts *opts,
struct spdk_sock_impl_opts *impl_opts)
{
ctx->inprogress = true;
ctx->fd_closed = false;
ctx->res0 = res0;
ctx->next_res = res;
ctx->opts = *opts;
ctx->impl_opts = *impl_opts;
}
static int
posix_sock_getaddr(struct spdk_sock *_sock, char *saddr, int slen, uint16_t *sport,
char *caddr, int clen, uint16_t *cport)
{
struct spdk_posix_sock *sock = __posix_sock(_sock);
assert(sock != NULL);
return spdk_net_getaddr(sock->fd, saddr, slen, sport, caddr, clen, cport);
}
static const char *
posix_sock_get_interface_name(struct spdk_sock *_sock)
{
struct spdk_posix_sock *sock = __posix_sock(_sock);
char saddr[64];
int rc;
rc = spdk_net_getaddr(sock->fd, saddr, sizeof(saddr), NULL, NULL, 0, NULL);
if (rc != 0) {
return NULL;
}
rc = spdk_net_get_interface_name(saddr, sock->interface_name,
sizeof(sock->interface_name));
if (rc != 0) {
return NULL;
}
return sock->interface_name;
}
static int32_t
posix_sock_get_numa_id(struct spdk_sock *sock)
{
const char *interface_name;
uint32_t numa_id;
int rc;
interface_name = posix_sock_get_interface_name(sock);
if (interface_name == NULL) {
return SPDK_ENV_NUMA_ID_ANY;
}
rc = spdk_read_sysfs_attribute_uint32(&numa_id,
"/sys/class/net/%s/device/numa_node", interface_name);
if (rc == 0 && numa_id <= INT32_MAX) {
return (int32_t)numa_id;
} else {
return SPDK_ENV_NUMA_ID_ANY;
}
}
enum posix_sock_create_type {
SPDK_SOCK_CREATE_LISTEN,
SPDK_SOCK_CREATE_CONNECT,
};
static int
posix_sock_alloc_pipe(struct spdk_posix_sock *sock, int sz)
{
uint8_t *new_buf, *old_buf;
struct spdk_pipe *new_pipe;
struct iovec siov[2];
struct iovec diov[2];
int sbytes;
ssize_t bytes;
int rc;
if (sock->recv_buf_sz == sz) {
return 0;
}
/* If the new size is 0, just free the pipe */
if (sz == 0) {
old_buf = spdk_pipe_destroy(sock->recv_pipe);
free(old_buf);
sock->recv_pipe = NULL;
return 0;
} else if (sz < MIN_SOCK_PIPE_SIZE) {
SPDK_ERRLOG("The size of the pipe must be larger than %d\n", MIN_SOCK_PIPE_SIZE);
return -1;
}
/* Round up to next 64 byte multiple */
rc = posix_memalign((void **)&new_buf, 64, sz);
if (rc != 0) {
SPDK_ERRLOG("socket recv buf allocation failed\n");
return -ENOMEM;
}
memset(new_buf, 0, sz);
new_pipe = spdk_pipe_create(new_buf, sz);
if (new_pipe == NULL) {
SPDK_ERRLOG("socket pipe allocation failed\n");
free(new_buf);
return -ENOMEM;
}
if (sock->recv_pipe != NULL) {
/* Pull all of the data out of the old pipe */
sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
if (sbytes > sz) {
/* Too much data to fit into the new pipe size */
old_buf = spdk_pipe_destroy(new_pipe);
free(old_buf);
return -EINVAL;
}
sbytes = spdk_pipe_writer_get_buffer(new_pipe, sz, diov);
assert(sbytes == sz);
bytes = spdk_iovcpy(siov, 2, diov, 2);
spdk_pipe_writer_advance(new_pipe, bytes);
old_buf = spdk_pipe_destroy(sock->recv_pipe);
free(old_buf);
}
sock->recv_buf_sz = sz;
sock->recv_pipe = new_pipe;
if (sock->base.group_impl) {
struct spdk_posix_sock_group_impl *group;
group = __posix_group_impl(sock->base.group_impl);
spdk_pipe_group_add(group->pipe_group, sock->recv_pipe);
}
return 0;
}
static int
posix_sock_set_recvbuf(struct spdk_sock *_sock, int sz)
{
struct spdk_posix_sock *sock = __posix_sock(_sock);
int min_size;
int rc;
assert(sock != NULL);
if (_sock->impl_opts.enable_recv_pipe) {
rc = posix_sock_alloc_pipe(sock, sz);
if (rc) {
return rc;
}
}
/* Set kernel buffer size to be at least MIN_SO_RCVBUF_SIZE and
* _sock->impl_opts.recv_buf_size. */
min_size = spdk_max(MIN_SO_RCVBUF_SIZE, _sock->impl_opts.recv_buf_size);
if (sz < min_size) {
sz = min_size;
}
rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz));
if (rc < 0) {
return rc;
}
_sock->impl_opts.recv_buf_size = sz;
return 0;
}
static int
posix_sock_set_sendbuf(struct spdk_sock *_sock, int sz)
{
struct spdk_posix_sock *sock = __posix_sock(_sock);
int min_size;
int rc;
assert(sock != NULL);
/* Set kernel buffer size to be at least MIN_SO_SNDBUF_SIZE and
* _sock->impl_opts.send_buf_size. */
min_size = spdk_max(MIN_SO_SNDBUF_SIZE, _sock->impl_opts.send_buf_size);
if (sz < min_size) {
sz = min_size;
}
rc = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz));
if (rc < 0) {
return rc;
}
_sock->impl_opts.send_buf_size = sz;
return 0;
}
static void
posix_sock_init(struct spdk_posix_sock *sock, bool enable_zero_copy)
{
#if defined(SPDK_ZEROCOPY) || defined(__linux__)
int flag;
int rc;
#endif
#if defined(SPDK_ZEROCOPY)
flag = 1;
if (enable_zero_copy) {
/* Try to turn on zero copy sends */
rc = setsockopt(sock->fd, SOL_SOCKET, SO_ZEROCOPY, &flag, sizeof(flag));
if (rc == 0) {
sock->zcopy = true;
/* Zcopy notification index from the kernel for first sendmsg is 0, so we need to start
* incrementing internal counter from UINT32_MAX. */
sock->sendmsg_idx = UINT32_MAX;
} else {
sock->zcopy = false;
}
}
#endif
#if defined(__linux__)
flag = 1;
if (sock->base.impl_opts.enable_quickack) {
rc = setsockopt(sock->fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(flag));
if (rc != 0) {
SPDK_ERRLOG("quickack was failed to set\n");
}
}
spdk_sock_get_placement_id(sock->fd, sock->base.impl_opts.enable_placement_id,
&sock->placement_id);
if (sock->base.impl_opts.enable_placement_id == PLACEMENT_MARK) {
/* Save placement_id */
spdk_sock_map_insert(&g_map, sock->placement_id, NULL);
}
#endif
}
static struct spdk_posix_sock *
posix_sock_alloc(int fd,
struct spdk_sock_impl_opts *impl_opts,
bool enable_zero_copy,
bool connected)
{
struct spdk_posix_sock *sock;
sock = calloc(1, sizeof(*sock));
if (sock == NULL) {
SPDK_ERRLOG("sock allocation failed\n");
return NULL;
}
sock->fd = fd;
memcpy(&sock->base.impl_opts, impl_opts, sizeof(*impl_opts));
if (connected) {
posix_sock_init(sock, enable_zero_copy);
} else {
sock->zcopy = enable_zero_copy;
}
return sock;
}
static bool
posix_sock_is_connecting(struct spdk_posix_sock *sock)
{
return sock->conn_ctx.inprogress;
}
static int
posix_sock_make_nonblock(int fd)
{
int flag;
flag = fcntl(fd, F_GETFL);
if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
return -1;
}
return 0;
}
static int
posix_sock_psk_find_session_server_cb(SSL *ssl, const unsigned char *identity,
size_t identity_len, SSL_SESSION **sess)
{
struct spdk_sock_impl_opts *impl_opts = SSL_get_app_data(ssl);
uint8_t key[SSL_MAX_MASTER_KEY_LENGTH] = {};
int keylen;
int rc, i;
STACK_OF(SSL_CIPHER) *ciphers;
const SSL_CIPHER *cipher;
const char *cipher_name;
const char *user_cipher = NULL;
bool found = false;
if (impl_opts->get_key) {
rc = impl_opts->get_key(key, sizeof(key), &user_cipher, identity, impl_opts->get_key_ctx);
if (rc < 0) {
SPDK_ERRLOG("Unable to find PSK for identity: %s\n", identity);
return 0;
}
keylen = rc;
} else {
if (impl_opts->psk_key == NULL) {
SPDK_ERRLOG("PSK is not set\n");
return 0;
}
SPDK_DEBUGLOG(sock_posix, "Length of Client's PSK ID %lu\n", strlen(impl_opts->psk_identity));
if (strcmp(impl_opts->psk_identity, identity) != 0) {
SPDK_ERRLOG("Unknown Client's PSK ID\n");
return 0;
}
keylen = impl_opts->psk_key_size;
memcpy(key, impl_opts->psk_key, keylen);
user_cipher = impl_opts->tls_cipher_suites;
}
if (user_cipher == NULL) {
SPDK_ERRLOG("Cipher suite not set\n");
return 0;
}
*sess = SSL_SESSION_new();
if (*sess == NULL) {
SPDK_ERRLOG("Unable to allocate new SSL session\n");
return 0;
}
ciphers = SSL_get_ciphers(ssl);
for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
cipher = sk_SSL_CIPHER_value(ciphers, i);
cipher_name = SSL_CIPHER_get_name(cipher);
if (strcmp(user_cipher, cipher_name) == 0) {
rc = SSL_SESSION_set_cipher(*sess, cipher);
if (rc != 1) {
SPDK_ERRLOG("Unable to set cipher: %s\n", cipher_name);
goto err;
}
found = true;
break;
}
}
if (found == false) {
SPDK_ERRLOG("No suitable cipher found\n");
goto err;
}
SPDK_DEBUGLOG(sock_posix, "Cipher selected: %s\n", cipher_name);
rc = SSL_SESSION_set_protocol_version(*sess, TLS1_3_VERSION);
if (rc != 1) {
SPDK_ERRLOG("Unable to set TLS version: %d\n", TLS1_3_VERSION);
goto err;
}
rc = SSL_SESSION_set1_master_key(*sess, key, keylen);
if (rc != 1) {
SPDK_ERRLOG("Unable to set PSK for session\n");
goto err;
}
return 1;
err:
SSL_SESSION_free(*sess);
*sess = NULL;
return 0;
}
static int
posix_sock_psk_use_session_client_cb(SSL *ssl, const EVP_MD *md, const unsigned char **identity,
size_t *identity_len, SSL_SESSION **sess)
{
struct spdk_sock_impl_opts *impl_opts = SSL_get_app_data(ssl);
int rc, i;
STACK_OF(SSL_CIPHER) *ciphers;
const SSL_CIPHER *cipher;
const char *cipher_name;
long keylen;
bool found = false;
if (impl_opts->psk_key == NULL) {
SPDK_ERRLOG("PSK is not set\n");
return 0;
}
if (impl_opts->psk_key_size > SSL_MAX_MASTER_KEY_LENGTH) {
SPDK_ERRLOG("PSK too long\n");
return 0;
}
keylen = impl_opts->psk_key_size;
if (impl_opts->tls_cipher_suites == NULL) {
SPDK_ERRLOG("Cipher suite not set\n");
return 0;
}
*sess = SSL_SESSION_new();
if (*sess == NULL) {
SPDK_ERRLOG("Unable to allocate new SSL session\n");
return 0;
}
ciphers = SSL_get_ciphers(ssl);
for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
cipher = sk_SSL_CIPHER_value(ciphers, i);
cipher_name = SSL_CIPHER_get_name(cipher);
if (strcmp(impl_opts->tls_cipher_suites, cipher_name) == 0) {
rc = SSL_SESSION_set_cipher(*sess, cipher);
if (rc != 1) {
SPDK_ERRLOG("Unable to set cipher: %s\n", cipher_name);
goto err;
}
found = true;
break;
}
}
if (found == false) {
SPDK_ERRLOG("No suitable cipher found\n");
goto err;
}
SPDK_DEBUGLOG(sock_posix, "Cipher selected: %s\n", cipher_name);
rc = SSL_SESSION_set_protocol_version(*sess, TLS1_3_VERSION);
if (rc != 1) {
SPDK_ERRLOG("Unable to set TLS version: %d\n", TLS1_3_VERSION);
goto err;
}
rc = SSL_SESSION_set1_master_key(*sess, impl_opts->psk_key, keylen);
if (rc != 1) {
SPDK_ERRLOG("Unable to set PSK for session\n");
goto err;
}
*identity_len = strlen(impl_opts->psk_identity);
*identity = impl_opts->psk_identity;
return 1;
err:
SSL_SESSION_free(*sess);
*sess = NULL;
return 0;
}
static SSL_CTX *
posix_sock_create_ssl_context(const SSL_METHOD *method, struct spdk_sock_opts *opts,
struct spdk_sock_impl_opts *impl_opts)
{
SSL_CTX *ctx;
int tls_version = 0;
bool ktls_enabled = false;
#ifdef SSL_OP_ENABLE_KTLS
long options;
#endif
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
/* Produce a SSL CTX in SSL V2 and V3 standards compliant way */
ctx = SSL_CTX_new(method);
if (!ctx) {
SPDK_ERRLOG("SSL_CTX_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
return NULL;
}
SPDK_DEBUGLOG(sock_posix, "SSL context created\n");
switch (impl_opts->tls_version) {
case 0:
/* auto-negotiation */
break;
case SPDK_TLS_VERSION_1_3:
tls_version = TLS1_3_VERSION;
break;
default:
SPDK_ERRLOG("Incorrect TLS version provided: %d\n", impl_opts->tls_version);
goto err;
}
if (tls_version) {
SPDK_DEBUGLOG(sock_posix, "Hardening TLS version to '%d'='0x%X'\n", impl_opts->tls_version,
tls_version);
if (!SSL_CTX_set_min_proto_version(ctx, tls_version)) {
SPDK_ERRLOG("Unable to set Min TLS version to '%d'='0x%X\n", impl_opts->tls_version, tls_version);
goto err;
}
if (!SSL_CTX_set_max_proto_version(ctx, tls_version)) {
SPDK_ERRLOG("Unable to set Max TLS version to '%d'='0x%X\n", impl_opts->tls_version, tls_version);
goto err;
}
}
if (impl_opts->enable_ktls) {
SPDK_DEBUGLOG(sock_posix, "Enabling kTLS offload\n");
#ifdef SSL_OP_ENABLE_KTLS
options = SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
ktls_enabled = options & SSL_OP_ENABLE_KTLS;
#else
ktls_enabled = false;
#endif
if (!ktls_enabled) {
SPDK_ERRLOG("Unable to set kTLS offload via SSL_CTX_set_options(). Configure openssl with 'enable-ktls'\n");
goto err;
}
}
/* SSL_CTX_set_ciphersuites() return 1 if the requested
* cipher suite list was configured, and 0 otherwise. */
if (impl_opts->tls_cipher_suites != NULL &&
SSL_CTX_set_ciphersuites(ctx, impl_opts->tls_cipher_suites) != 1) {
SPDK_ERRLOG("Unable to set TLS cipher suites for SSL'\n");
goto err;
}
return ctx;
err:
SSL_CTX_free(ctx);
return NULL;
}
static SSL *
ssl_sock_setup_connect(SSL_CTX *ctx, int fd)
{
SSL *ssl;
ssl = SSL_new(ctx);
if (!ssl) {
SPDK_ERRLOG("SSL_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
return NULL;
}
SSL_set_fd(ssl, fd);
SSL_set_connect_state(ssl);
SSL_set_psk_use_session_callback(ssl, posix_sock_psk_use_session_client_cb);
SPDK_DEBUGLOG(sock_posix, "SSL object creation finished: %p\n", ssl);
SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
SPDK_DEBUGLOG(sock_posix, "Negotiated Cipher suite:%s\n",
SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
return ssl;
}
static SSL *
ssl_sock_setup_accept(SSL_CTX *ctx, int fd)
{
SSL *ssl;
ssl = SSL_new(ctx);
if (!ssl) {
SPDK_ERRLOG("SSL_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
return NULL;
}
SSL_set_fd(ssl, fd);
SSL_set_accept_state(ssl);
SSL_set_psk_find_session_callback(ssl, posix_sock_psk_find_session_server_cb);
SPDK_DEBUGLOG(sock_posix, "SSL object creation finished: %p\n", ssl);
SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
SPDK_DEBUGLOG(sock_posix, "Negotiated Cipher suite:%s\n",
SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
return ssl;
}
static ssize_t
SSL_readv(SSL *ssl, const struct iovec *iov, int iovcnt)
{
int i, rc = 0;
ssize_t total = 0;
for (i = 0; i < iovcnt; i++) {
rc = SSL_read(ssl, iov[i].iov_base, iov[i].iov_len);
if (rc > 0) {
total += rc;
}
if (rc != (int)iov[i].iov_len) {
break;
}
}
if (total > 0) {
errno = 0;
return total;
}
switch (SSL_get_error(ssl, rc)) {
case SSL_ERROR_ZERO_RETURN:
errno = ENOTCONN;
return 0;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_CONNECT:
case SSL_ERROR_WANT_ACCEPT:
case SSL_ERROR_WANT_X509_LOOKUP:
case SSL_ERROR_WANT_ASYNC:
case SSL_ERROR_WANT_ASYNC_JOB:
case SSL_ERROR_WANT_CLIENT_HELLO_CB:
errno = EAGAIN;
return -1;
case SSL_ERROR_SYSCALL:
case SSL_ERROR_SSL:
errno = ENOTCONN;
return -1;
default:
errno = ENOTCONN;
return -1;
}
}
static ssize_t
SSL_writev(SSL *ssl, struct iovec *iov, int iovcnt)
{
int i, rc = 0;
ssize_t total = 0;
for (i = 0; i < iovcnt; i++) {
rc = SSL_write(ssl, iov[i].iov_base, iov[i].iov_len);
if (rc > 0) {
total += rc;
}
if (rc != (int)iov[i].iov_len) {
break;
}
}
if (total > 0) {
errno = 0;
return total;
}
switch (SSL_get_error(ssl, rc)) {
case SSL_ERROR_ZERO_RETURN:
errno = ENOTCONN;
return 0;
case SSL_ERROR_WANT_READ:
case SSL_ERROR_WANT_WRITE:
case SSL_ERROR_WANT_CONNECT:
case SSL_ERROR_WANT_ACCEPT:
case SSL_ERROR_WANT_X509_LOOKUP:
case SSL_ERROR_WANT_ASYNC:
case SSL_ERROR_WANT_ASYNC_JOB:
case SSL_ERROR_WANT_CLIENT_HELLO_CB:
errno = EAGAIN;
return -1;
case SSL_ERROR_SYSCALL:
case SSL_ERROR_SSL:
errno = ENOTCONN;
return -1;
default:
errno = ENOTCONN;
return -1;
}
}
static int
posix_sock_connect_async(struct addrinfo **ai,
struct spdk_sock_opts *opts,
struct spdk_sock_impl_opts *impl_opts,
bool *inprogress)
{
int rc;
int fd;
*inprogress = false;
for (; *ai != NULL; *ai = (*ai)->ai_next) {
fd = spdk_sock_posix_fd_create(*ai, opts, impl_opts);
if (fd < 0) {
continue;
}
if (posix_sock_make_nonblock(fd)) {
close(fd);
return -1;
}
rc = connect(fd, (*ai)->ai_addr, (*ai)->ai_addrlen);
if (rc != 0 && errno != EINPROGRESS) {
SPDK_ERRLOG("connect() failed, errno = %d\n", errno);
/* try next family */
close(fd);
continue;
} else if (rc != 0 && errno == EINPROGRESS) {
*inprogress = true;
}
*ai = (*ai)->ai_next;
return fd;
}
return -1;
}
static struct spdk_sock *
posix_sock_create(const char *ip, int port,
enum posix_sock_create_type type,
struct spdk_sock_opts *opts,
bool enable_ssl)
{
struct spdk_posix_sock *sock;
struct spdk_sock_impl_opts impl_opts;
struct addrinfo *res, *res0;
int fd, rc;
char *loopback_check;
bool enable_zcopy_user_opts = true;
bool enable_zcopy_impl_opts = true;
SSL_CTX *ctx = 0;
SSL *ssl = 0;
bool conn_inprogress = false;
assert(opts != NULL);
if (enable_ssl) {
_opts_get_impl_opts(opts, &impl_opts, &g_ssl_impl_opts);
} else {
_opts_get_impl_opts(opts, &impl_opts, &g_posix_impl_opts);
}
res0 = spdk_sock_posix_getaddrinfo(ip, port);
if (!res0) {
return NULL;
}
/* try listen */
fd = -1;
if (type == SPDK_SOCK_CREATE_LISTEN) {
for (res = res0; res != NULL; res = res->ai_next) {
retry:
fd = spdk_sock_posix_fd_create(res, opts, &impl_opts);
if (fd < 0) {
continue;
}
rc = bind(fd, res->ai_addr, res->ai_addrlen);
if (rc != 0) {
SPDK_ERRLOG("bind() failed at port %d, errno = %d\n", port, errno);
switch (errno) {
case EINTR:
/* interrupted? */
close(fd);
goto retry;
case EADDRNOTAVAIL:
SPDK_ERRLOG("IP address %s not available. "
"Verify IP address in config file "
"and make sure setup script is "
"run before starting spdk app.\n", ip);
/* FALLTHROUGH */
default:
/* try next family */
close(fd);
fd = -1;