Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2018 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2020, 2021 Mellanox Technologies LTD. All rights reserved.
4 : : * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "spdk/stdinc.h"
8 : :
9 : : #if defined(__FreeBSD__)
10 : : #include <sys/event.h>
11 : : #define SPDK_KEVENT
12 : : #else
13 : : #include <sys/epoll.h>
14 : : #define SPDK_EPOLL
15 : : #endif
16 : :
17 : : #if defined(__linux__)
18 : : #include <linux/errqueue.h>
19 : : #endif
20 : :
21 : : #include "spdk/env.h"
22 : : #include "spdk/log.h"
23 : : #include "spdk/pipe.h"
24 : : #include "spdk/sock.h"
25 : : #include "spdk/util.h"
26 : : #include "spdk/string.h"
27 : : #include "spdk_internal/sock.h"
28 : : #include "../sock_kernel.h"
29 : :
30 : : #include "openssl/crypto.h"
31 : : #include "openssl/err.h"
32 : : #include "openssl/ssl.h"
33 : :
34 : : #define MAX_TMPBUF 1024
35 : : #define PORTNUMLEN 32
36 : :
37 : : #if defined(SO_ZEROCOPY) && defined(MSG_ZEROCOPY)
38 : : #define SPDK_ZEROCOPY
39 : : #endif
40 : :
41 : : struct spdk_posix_sock {
42 : : struct spdk_sock base;
43 : : int fd;
44 : :
45 : : uint32_t sendmsg_idx;
46 : :
47 : : struct spdk_pipe *recv_pipe;
48 : : int recv_buf_sz;
49 : : bool pipe_has_data;
50 : : bool socket_has_data;
51 : : bool zcopy;
52 : :
53 : : int placement_id;
54 : :
55 : : SSL_CTX *ctx;
56 : : SSL *ssl;
57 : :
58 : : TAILQ_ENTRY(spdk_posix_sock) link;
59 : : };
60 : :
61 : : TAILQ_HEAD(spdk_has_data_list, spdk_posix_sock);
62 : :
63 : : struct spdk_posix_sock_group_impl {
64 : : struct spdk_sock_group_impl base;
65 : : int fd;
66 : : struct spdk_has_data_list socks_with_data;
67 : : int placement_id;
68 : : struct spdk_pipe_group *pipe_group;
69 : : };
70 : :
71 : : static struct spdk_sock_impl_opts g_posix_impl_opts = {
72 : : .recv_buf_size = DEFAULT_SO_RCVBUF_SIZE,
73 : : .send_buf_size = DEFAULT_SO_SNDBUF_SIZE,
74 : : .enable_recv_pipe = true,
75 : : .enable_quickack = false,
76 : : .enable_placement_id = PLACEMENT_NONE,
77 : : .enable_zerocopy_send_server = true,
78 : : .enable_zerocopy_send_client = false,
79 : : .zerocopy_threshold = 0,
80 : : .tls_version = 0,
81 : : .enable_ktls = false,
82 : : .psk_key = NULL,
83 : : .psk_key_size = 0,
84 : : .psk_identity = NULL,
85 : : .get_key = NULL,
86 : : .get_key_ctx = NULL,
87 : : .tls_cipher_suites = NULL
88 : : };
89 : :
90 : : static struct spdk_sock_impl_opts g_ssl_impl_opts = {
91 : : .recv_buf_size = MIN_SO_RCVBUF_SIZE,
92 : : .send_buf_size = MIN_SO_SNDBUF_SIZE,
93 : : .enable_recv_pipe = true,
94 : : .enable_quickack = false,
95 : : .enable_placement_id = PLACEMENT_NONE,
96 : : .enable_zerocopy_send_server = true,
97 : : .enable_zerocopy_send_client = false,
98 : : .zerocopy_threshold = 0,
99 : : .tls_version = 0,
100 : : .enable_ktls = false,
101 : : .psk_key = NULL,
102 : : .psk_identity = NULL
103 : : };
104 : :
105 : : static struct spdk_sock_map g_map = {
106 : : .entries = STAILQ_HEAD_INITIALIZER(g_map.entries),
107 : : .mtx = PTHREAD_MUTEX_INITIALIZER
108 : : };
109 : :
110 : : __attribute((destructor)) static void
111 : 2855 : posix_sock_map_cleanup(void)
112 : : {
113 : 2855 : spdk_sock_map_cleanup(&g_map);
114 : 2855 : }
115 : :
116 : : #define __posix_sock(sock) (struct spdk_posix_sock *)sock
117 : : #define __posix_group_impl(group) (struct spdk_posix_sock_group_impl *)group
118 : :
119 : : static void
120 : 1006 : posix_sock_copy_impl_opts(struct spdk_sock_impl_opts *dest, const struct spdk_sock_impl_opts *src,
121 : : size_t len)
122 : : {
123 : : #define FIELD_OK(field) \
124 : : offsetof(struct spdk_sock_impl_opts, field) + sizeof(src->field) <= len
125 : :
126 : : #define SET_FIELD(field) \
127 : : if (FIELD_OK(field)) { \
128 : : dest->field = src->field; \
129 : : }
130 : :
131 [ + + ]: 1006 : SET_FIELD(recv_buf_size);
132 [ + + ]: 1006 : SET_FIELD(send_buf_size);
133 [ + + - + ]: 1006 : SET_FIELD(enable_recv_pipe);
134 [ + + - + ]: 1006 : SET_FIELD(enable_zerocopy_send);
135 [ + + - + ]: 1006 : SET_FIELD(enable_quickack);
136 [ + + ]: 1006 : SET_FIELD(enable_placement_id);
137 [ + + - + ]: 1006 : SET_FIELD(enable_zerocopy_send_server);
138 [ + + - + ]: 1006 : SET_FIELD(enable_zerocopy_send_client);
139 [ + + ]: 1006 : SET_FIELD(zerocopy_threshold);
140 [ + + ]: 1006 : SET_FIELD(tls_version);
141 [ + + - + ]: 1006 : SET_FIELD(enable_ktls);
142 [ + + ]: 1006 : SET_FIELD(psk_key);
143 [ + + ]: 1006 : SET_FIELD(psk_key_size);
144 [ + + ]: 1006 : SET_FIELD(psk_identity);
145 [ + + ]: 1006 : SET_FIELD(get_key);
146 [ + + ]: 1006 : SET_FIELD(get_key_ctx);
147 [ + + ]: 1006 : SET_FIELD(tls_cipher_suites);
148 : :
149 : : #undef SET_FIELD
150 : : #undef FIELD_OK
151 : 1006 : }
152 : :
153 : : static int
154 : 643 : _sock_impl_get_opts(struct spdk_sock_impl_opts *opts, struct spdk_sock_impl_opts *impl_opts,
155 : : size_t *len)
156 : : {
157 [ + - - + ]: 643 : if (!opts || !len) {
158 : 0 : errno = EINVAL;
159 : 0 : return -1;
160 : : }
161 : :
162 [ - + ]: 643 : assert(sizeof(*opts) >= *len);
163 [ - + ]: 643 : memset(opts, 0, *len);
164 : :
165 : 643 : posix_sock_copy_impl_opts(opts, impl_opts, *len);
166 : 643 : *len = spdk_min(*len, sizeof(*impl_opts));
167 : :
168 : 643 : return 0;
169 : : }
170 : :
171 : : static int
172 : 246 : posix_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
173 : : {
174 : 246 : return _sock_impl_get_opts(opts, &g_posix_impl_opts, len);
175 : : }
176 : :
177 : : static int
178 : 397 : ssl_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
179 : : {
180 : 397 : return _sock_impl_get_opts(opts, &g_ssl_impl_opts, len);
181 : : }
182 : :
183 : : static int
184 : 152 : _sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, struct spdk_sock_impl_opts *impl_opts,
185 : : size_t len)
186 : : {
187 [ - + ]: 152 : if (!opts) {
188 : 0 : errno = EINVAL;
189 : 0 : return -1;
190 : : }
191 : :
192 [ - + ]: 152 : assert(sizeof(*opts) >= len);
193 : 152 : posix_sock_copy_impl_opts(impl_opts, opts, len);
194 : :
195 : 152 : return 0;
196 : : }
197 : :
198 : : static int
199 : 77 : posix_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
200 : : {
201 : 77 : return _sock_impl_set_opts(opts, &g_posix_impl_opts, len);
202 : : }
203 : :
204 : : static int
205 : 75 : ssl_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
206 : : {
207 : 75 : return _sock_impl_set_opts(opts, &g_ssl_impl_opts, len);
208 : : }
209 : :
210 : : static void
211 : 19131 : _opts_get_impl_opts(const struct spdk_sock_opts *opts, struct spdk_sock_impl_opts *dest,
212 : : const struct spdk_sock_impl_opts *default_impl)
213 : : {
214 : : /* Copy the default impl_opts first to cover cases when user's impl_opts is smaller */
215 [ - + - + ]: 19131 : memcpy(dest, default_impl, sizeof(*dest));
216 : :
217 [ + + ]: 19131 : if (opts->impl_opts != NULL) {
218 [ - + ]: 211 : assert(sizeof(*dest) >= opts->impl_opts_size);
219 : 211 : posix_sock_copy_impl_opts(dest, opts->impl_opts, opts->impl_opts_size);
220 : : }
221 : 19131 : }
222 : :
223 : : static int
224 : 14512 : posix_sock_getaddr(struct spdk_sock *_sock, char *saddr, int slen, uint16_t *sport,
225 : : char *caddr, int clen, uint16_t *cport)
226 : : {
227 : 14512 : struct spdk_posix_sock *sock = __posix_sock(_sock);
228 : 186 : struct sockaddr_storage sa;
229 : 186 : socklen_t salen;
230 : : int rc;
231 : :
232 [ - + ]: 14512 : assert(sock != NULL);
233 : :
234 : 14512 : memset(&sa, 0, sizeof sa);
235 : 14512 : salen = sizeof sa;
236 : 14512 : rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
237 [ - + ]: 14512 : if (rc != 0) {
238 : 0 : SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
239 : 0 : return -1;
240 : : }
241 : :
242 [ - + - ]: 14512 : switch (sa.ss_family) {
243 : 0 : case AF_UNIX:
244 : : /* Acceptable connection types that don't have IPs */
245 : 0 : return 0;
246 : 14512 : case AF_INET:
247 : : case AF_INET6:
248 : : /* Code below will get IP addresses */
249 : 14512 : break;
250 : 0 : default:
251 : : /* Unsupported socket family */
252 : 0 : return -1;
253 : : }
254 : :
255 : 14512 : rc = get_addr_str((struct sockaddr *)&sa, saddr, slen);
256 [ - + ]: 14512 : if (rc != 0) {
257 : 0 : SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
258 : 0 : return -1;
259 : : }
260 : :
261 [ + + ]: 14512 : if (sport) {
262 [ + - ]: 14224 : if (sa.ss_family == AF_INET) {
263 : 14224 : *sport = ntohs(((struct sockaddr_in *) &sa)->sin_port);
264 [ # # ]: 0 : } else if (sa.ss_family == AF_INET6) {
265 : 0 : *sport = ntohs(((struct sockaddr_in6 *) &sa)->sin6_port);
266 : : }
267 : : }
268 : :
269 : 14512 : memset(&sa, 0, sizeof sa);
270 : 14512 : salen = sizeof sa;
271 : 14512 : rc = getpeername(sock->fd, (struct sockaddr *) &sa, &salen);
272 [ - + ]: 14512 : if (rc != 0) {
273 : 0 : SPDK_ERRLOG("getpeername() failed (errno=%d)\n", errno);
274 : 0 : return -1;
275 : : }
276 : :
277 : 14512 : rc = get_addr_str((struct sockaddr *)&sa, caddr, clen);
278 [ - + ]: 14512 : if (rc != 0) {
279 : 0 : SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
280 : 0 : return -1;
281 : : }
282 : :
283 [ + + ]: 14512 : if (cport) {
284 [ + - ]: 14224 : if (sa.ss_family == AF_INET) {
285 : 14224 : *cport = ntohs(((struct sockaddr_in *) &sa)->sin_port);
286 [ # # ]: 0 : } else if (sa.ss_family == AF_INET6) {
287 : 0 : *cport = ntohs(((struct sockaddr_in6 *) &sa)->sin6_port);
288 : : }
289 : : }
290 : :
291 : 14512 : return 0;
292 : : }
293 : :
294 : : enum posix_sock_create_type {
295 : : SPDK_SOCK_CREATE_LISTEN,
296 : : SPDK_SOCK_CREATE_CONNECT,
297 : : };
298 : :
299 : : static int
300 : 13003 : posix_sock_alloc_pipe(struct spdk_posix_sock *sock, int sz)
301 : : {
302 : 197 : uint8_t *new_buf, *old_buf;
303 : : struct spdk_pipe *new_pipe;
304 : 197 : struct iovec siov[2];
305 : 197 : struct iovec diov[2];
306 : : int sbytes;
307 : : ssize_t bytes;
308 : : int rc;
309 : :
310 [ + + ]: 13003 : if (sock->recv_buf_sz == sz) {
311 : 1898 : return 0;
312 : : }
313 : :
314 : : /* If the new size is 0, just free the pipe */
315 [ - + ]: 11105 : if (sz == 0) {
316 : 0 : old_buf = spdk_pipe_destroy(sock->recv_pipe);
317 : 0 : free(old_buf);
318 : 0 : sock->recv_pipe = NULL;
319 : 0 : return 0;
320 [ - + ]: 11105 : } else if (sz < MIN_SOCK_PIPE_SIZE) {
321 : 0 : SPDK_ERRLOG("The size of the pipe must be larger than %d\n", MIN_SOCK_PIPE_SIZE);
322 : 0 : return -1;
323 : : }
324 : :
325 : : /* Round up to next 64 byte multiple */
326 [ - + ]: 11105 : rc = posix_memalign((void **)&new_buf, 64, sz);
327 [ - + ]: 11105 : if (rc != 0) {
328 : 0 : SPDK_ERRLOG("socket recv buf allocation failed\n");
329 : 0 : return -ENOMEM;
330 : : }
331 [ - + ]: 11105 : memset(new_buf, 0, sz);
332 : :
333 : 11105 : new_pipe = spdk_pipe_create(new_buf, sz);
334 [ - + ]: 11105 : if (new_pipe == NULL) {
335 : 0 : SPDK_ERRLOG("socket pipe allocation failed\n");
336 : 0 : free(new_buf);
337 : 0 : return -ENOMEM;
338 : : }
339 : :
340 [ - + ]: 11105 : if (sock->recv_pipe != NULL) {
341 : : /* Pull all of the data out of the old pipe */
342 : 0 : sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
343 [ # # ]: 0 : if (sbytes > sz) {
344 : : /* Too much data to fit into the new pipe size */
345 : 0 : old_buf = spdk_pipe_destroy(new_pipe);
346 : 0 : free(old_buf);
347 : 0 : return -EINVAL;
348 : : }
349 : :
350 : 0 : sbytes = spdk_pipe_writer_get_buffer(new_pipe, sz, diov);
351 [ # # ]: 0 : assert(sbytes == sz);
352 : :
353 : 0 : bytes = spdk_iovcpy(siov, 2, diov, 2);
354 : 0 : spdk_pipe_writer_advance(new_pipe, bytes);
355 : :
356 : 0 : old_buf = spdk_pipe_destroy(sock->recv_pipe);
357 : 0 : free(old_buf);
358 : : }
359 : :
360 : 11105 : sock->recv_buf_sz = sz;
361 : 11105 : sock->recv_pipe = new_pipe;
362 : :
363 [ + + ]: 11105 : if (sock->base.group_impl) {
364 : : struct spdk_posix_sock_group_impl *group;
365 : :
366 : 8014 : group = __posix_group_impl(sock->base.group_impl);
367 : 8014 : spdk_pipe_group_add(group->pipe_group, sock->recv_pipe);
368 : : }
369 : :
370 : 11105 : return 0;
371 : : }
372 : :
373 : : static int
374 : 13003 : posix_sock_set_recvbuf(struct spdk_sock *_sock, int sz)
375 : : {
376 : 13003 : struct spdk_posix_sock *sock = __posix_sock(_sock);
377 : : int min_size;
378 : : int rc;
379 : :
380 [ - + ]: 13003 : assert(sock != NULL);
381 : :
382 [ + + + - ]: 13003 : if (_sock->impl_opts.enable_recv_pipe) {
383 : 13003 : rc = posix_sock_alloc_pipe(sock, sz);
384 [ - + ]: 13003 : if (rc) {
385 : 0 : return rc;
386 : : }
387 : : }
388 : :
389 : : /* Set kernel buffer size to be at least MIN_SO_RCVBUF_SIZE and
390 : : * _sock->impl_opts.recv_buf_size. */
391 : 13003 : min_size = spdk_max(MIN_SO_RCVBUF_SIZE, _sock->impl_opts.recv_buf_size);
392 : :
393 [ + + ]: 13003 : if (sz < min_size) {
394 : 12773 : sz = min_size;
395 : : }
396 : :
397 : 13003 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz));
398 [ - + ]: 13003 : if (rc < 0) {
399 : 0 : return rc;
400 : : }
401 : :
402 : 13003 : _sock->impl_opts.recv_buf_size = sz;
403 : :
404 : 13003 : return 0;
405 : : }
406 : :
407 : : static int
408 : 6 : posix_sock_set_sendbuf(struct spdk_sock *_sock, int sz)
409 : : {
410 : 6 : struct spdk_posix_sock *sock = __posix_sock(_sock);
411 : : int min_size;
412 : : int rc;
413 : :
414 [ - + ]: 6 : assert(sock != NULL);
415 : :
416 : : /* Set kernel buffer size to be at least MIN_SO_SNDBUF_SIZE and
417 : : * _sock->impl_opts.send_buf_size. */
418 : 6 : min_size = spdk_max(MIN_SO_SNDBUF_SIZE, _sock->impl_opts.send_buf_size);
419 : :
420 [ + - ]: 6 : if (sz < min_size) {
421 : 6 : sz = min_size;
422 : : }
423 : :
424 : 6 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz));
425 [ - + ]: 6 : if (rc < 0) {
426 : 0 : return rc;
427 : : }
428 : :
429 : 6 : _sock->impl_opts.send_buf_size = sz;
430 : :
431 : 6 : return 0;
432 : : }
433 : :
434 : : static void
435 : 11586 : posix_sock_init(struct spdk_posix_sock *sock, bool enable_zero_copy)
436 : : {
437 : : #if defined(SPDK_ZEROCOPY) || defined(__linux__)
438 : 317 : int flag;
439 : : int rc;
440 : : #endif
441 : :
442 : : #if defined(SPDK_ZEROCOPY)
443 : 11565 : flag = 1;
444 : :
445 [ + + ]: 11565 : if (enable_zero_copy) {
446 : : /* Try to turn on zero copy sends */
447 : 7359 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_ZEROCOPY, &flag, sizeof(flag));
448 [ + - ]: 7359 : if (rc == 0) {
449 : 7359 : sock->zcopy = true;
450 : : }
451 : : }
452 : : #endif
453 : :
454 : : #if defined(__linux__)
455 : 11586 : flag = 1;
456 : :
457 [ - + - + ]: 11586 : if (sock->base.impl_opts.enable_quickack) {
458 : 0 : rc = setsockopt(sock->fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(flag));
459 [ # # ]: 0 : if (rc != 0) {
460 : 0 : SPDK_ERRLOG("quickack was failed to set\n");
461 : : }
462 : : }
463 : :
464 : 11586 : spdk_sock_get_placement_id(sock->fd, sock->base.impl_opts.enable_placement_id,
465 : : &sock->placement_id);
466 : :
467 [ - + ]: 11586 : if (sock->base.impl_opts.enable_placement_id == PLACEMENT_MARK) {
468 : : /* Save placement_id */
469 : 0 : spdk_sock_map_insert(&g_map, sock->placement_id, NULL);
470 : : }
471 : : #endif
472 : 11586 : }
473 : :
474 : : static struct spdk_posix_sock *
475 : 11586 : posix_sock_alloc(int fd, struct spdk_sock_impl_opts *impl_opts, bool enable_zero_copy)
476 : : {
477 : : struct spdk_posix_sock *sock;
478 : :
479 : 11586 : sock = calloc(1, sizeof(*sock));
480 [ - + ]: 11586 : if (sock == NULL) {
481 : 0 : SPDK_ERRLOG("sock allocation failed\n");
482 : 0 : return NULL;
483 : : }
484 : :
485 : 11586 : sock->fd = fd;
486 [ - + - + ]: 11586 : memcpy(&sock->base.impl_opts, impl_opts, sizeof(*impl_opts));
487 : 11586 : posix_sock_init(sock, enable_zero_copy);
488 : :
489 : 11586 : return sock;
490 : : }
491 : :
492 : : static int
493 : 19131 : posix_fd_create(struct addrinfo *res, struct spdk_sock_opts *opts,
494 : : struct spdk_sock_impl_opts *impl_opts)
495 : : {
496 : : int fd;
497 : 19131 : int val = 1;
498 : 193 : int rc, sz;
499 : : #if defined(__linux__)
500 : 193 : int to;
501 : : #endif
502 : :
503 : 19131 : fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
504 [ - + ]: 19131 : if (fd < 0) {
505 : : /* error */
506 : 0 : return -1;
507 : : }
508 : :
509 : 19131 : sz = impl_opts->recv_buf_size;
510 : 19131 : rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz));
511 : : if (rc) {
512 : : /* Not fatal */
513 : : }
514 : :
515 : 19131 : sz = impl_opts->send_buf_size;
516 : 19131 : rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz));
517 : : if (rc) {
518 : : /* Not fatal */
519 : : }
520 : :
521 : 19131 : rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val);
522 [ - + ]: 19131 : if (rc != 0) {
523 : 0 : close(fd);
524 : : /* error */
525 : 0 : return -1;
526 : : }
527 : 19131 : rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val);
528 [ - + ]: 19131 : if (rc != 0) {
529 : 0 : close(fd);
530 : : /* error */
531 : 0 : return -1;
532 : : }
533 : :
534 : : #if defined(SO_PRIORITY)
535 [ + + ]: 19131 : if (opts->priority) {
536 : 1 : rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &opts->priority, sizeof val);
537 [ - + ]: 1 : if (rc != 0) {
538 : 0 : close(fd);
539 : : /* error */
540 : 0 : return -1;
541 : : }
542 : : }
543 : : #endif
544 : :
545 [ - + ]: 19131 : if (res->ai_family == AF_INET6) {
546 : 0 : rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof val);
547 [ # # ]: 0 : if (rc != 0) {
548 : 0 : close(fd);
549 : : /* error */
550 : 0 : return -1;
551 : : }
552 : : }
553 : :
554 [ + + ]: 19131 : if (opts->ack_timeout) {
555 : : #if defined(__linux__)
556 : 16 : to = opts->ack_timeout;
557 : 16 : rc = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &to, sizeof(to));
558 [ - + ]: 16 : if (rc != 0) {
559 : 0 : close(fd);
560 : : /* error */
561 : 0 : return -1;
562 : : }
563 : : #else
564 : : SPDK_WARNLOG("TCP_USER_TIMEOUT is not supported.\n");
565 : : #endif
566 : : }
567 : :
568 : 19131 : return fd;
569 : : }
570 : :
571 : : static int
572 : 142 : posix_sock_psk_find_session_server_cb(SSL *ssl, const unsigned char *identity,
573 : : size_t identity_len, SSL_SESSION **sess)
574 : : {
575 : 142 : struct spdk_sock_impl_opts *impl_opts = SSL_get_app_data(ssl);
576 : 142 : uint8_t key[SSL_MAX_MASTER_KEY_LENGTH] = {};
577 : : int keylen;
578 : : int rc, i;
579 : : STACK_OF(SSL_CIPHER) *ciphers;
580 : : const SSL_CIPHER *cipher;
581 : : const char *cipher_name;
582 : 142 : const char *user_cipher = NULL;
583 : 142 : bool found = false;
584 : :
585 [ + + ]: 142 : if (impl_opts->get_key) {
586 : 130 : rc = impl_opts->get_key(key, sizeof(key), &user_cipher, identity, impl_opts->get_key_ctx);
587 [ + + ]: 130 : if (rc < 0) {
588 : 6 : SPDK_ERRLOG("Unable to find PSK for identity: %s\n", identity);
589 : 6 : return 0;
590 : : }
591 : 124 : keylen = rc;
592 : : } else {
593 [ - + ]: 12 : if (impl_opts->psk_key == NULL) {
594 : 0 : SPDK_ERRLOG("PSK is not set\n");
595 : 0 : return 0;
596 : : }
597 : :
598 [ - + - + : 12 : SPDK_DEBUGLOG(sock_posix, "Length of Client's PSK ID %lu\n", strlen(impl_opts->psk_identity));
- - ]
599 [ - + - + : 12 : if (strcmp(impl_opts->psk_identity, identity) != 0) {
+ + ]
600 : 2 : SPDK_ERRLOG("Unknown Client's PSK ID\n");
601 : 2 : return 0;
602 : : }
603 : 10 : keylen = impl_opts->psk_key_size;
604 : :
605 [ - + ]: 10 : memcpy(key, impl_opts->psk_key, keylen);
606 : 10 : user_cipher = impl_opts->tls_cipher_suites;
607 : : }
608 : :
609 [ - + ]: 134 : if (user_cipher == NULL) {
610 : 0 : SPDK_ERRLOG("Cipher suite not set\n");
611 : 0 : return 0;
612 : : }
613 : :
614 : 134 : *sess = SSL_SESSION_new();
615 [ - + ]: 134 : if (*sess == NULL) {
616 : 0 : SPDK_ERRLOG("Unable to allocate new SSL session\n");
617 : 0 : return 0;
618 : : }
619 : :
620 : 134 : ciphers = SSL_get_ciphers(ssl);
621 [ + - ]: 207 : for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
622 : 207 : cipher = sk_SSL_CIPHER_value(ciphers, i);
623 : 207 : cipher_name = SSL_CIPHER_get_name(cipher);
624 : :
625 [ - + - + : 207 : if (strcmp(user_cipher, cipher_name) == 0) {
+ + ]
626 : 134 : rc = SSL_SESSION_set_cipher(*sess, cipher);
627 [ - + ]: 134 : if (rc != 1) {
628 : 0 : SPDK_ERRLOG("Unable to set cipher: %s\n", cipher_name);
629 : 0 : goto err;
630 : : }
631 : 134 : found = true;
632 : 134 : break;
633 : : }
634 : : }
635 [ - + ]: 134 : if (found == false) {
636 : 0 : SPDK_ERRLOG("No suitable cipher found\n");
637 : 0 : goto err;
638 : : }
639 : :
640 [ - + - + ]: 134 : SPDK_DEBUGLOG(sock_posix, "Cipher selected: %s\n", cipher_name);
641 : :
642 : 134 : rc = SSL_SESSION_set_protocol_version(*sess, TLS1_3_VERSION);
643 [ - + ]: 134 : if (rc != 1) {
644 : 0 : SPDK_ERRLOG("Unable to set TLS version: %d\n", TLS1_3_VERSION);
645 : 0 : goto err;
646 : : }
647 : :
648 : 134 : rc = SSL_SESSION_set1_master_key(*sess, key, keylen);
649 [ - + ]: 134 : if (rc != 1) {
650 : 0 : SPDK_ERRLOG("Unable to set PSK for session\n");
651 : 0 : goto err;
652 : : }
653 : :
654 : 134 : return 1;
655 : :
656 : 0 : err:
657 : 0 : SSL_SESSION_free(*sess);
658 : 0 : *sess = NULL;
659 : 0 : return 0;
660 : : }
661 : :
662 : : static int
663 : 140 : posix_sock_psk_use_session_client_cb(SSL *ssl, const EVP_MD *md, const unsigned char **identity,
664 : : size_t *identity_len, SSL_SESSION **sess)
665 : : {
666 : 140 : struct spdk_sock_impl_opts *impl_opts = SSL_get_app_data(ssl);
667 : : int rc, i;
668 : : STACK_OF(SSL_CIPHER) *ciphers;
669 : : const SSL_CIPHER *cipher;
670 : : const char *cipher_name;
671 : : long keylen;
672 : 140 : bool found = false;
673 : :
674 [ - + ]: 140 : if (impl_opts->psk_key == NULL) {
675 : 0 : SPDK_ERRLOG("PSK is not set\n");
676 : 0 : return 0;
677 : : }
678 [ - + ]: 140 : if (impl_opts->psk_key_size > SSL_MAX_MASTER_KEY_LENGTH) {
679 : 0 : SPDK_ERRLOG("PSK too long\n");
680 : 0 : return 0;
681 : : }
682 : 140 : keylen = impl_opts->psk_key_size;
683 : :
684 [ - + ]: 140 : if (impl_opts->tls_cipher_suites == NULL) {
685 : 0 : SPDK_ERRLOG("Cipher suite not set\n");
686 : 0 : return 0;
687 : : }
688 : 140 : *sess = SSL_SESSION_new();
689 [ - + ]: 140 : if (*sess == NULL) {
690 : 0 : SPDK_ERRLOG("Unable to allocate new SSL session\n");
691 : 0 : return 0;
692 : : }
693 : :
694 : 140 : ciphers = SSL_get_ciphers(ssl);
695 [ + - ]: 140 : for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
696 : 140 : cipher = sk_SSL_CIPHER_value(ciphers, i);
697 : 140 : cipher_name = SSL_CIPHER_get_name(cipher);
698 : :
699 [ - + - + : 140 : if (strcmp(impl_opts->tls_cipher_suites, cipher_name) == 0) {
+ - ]
700 : 140 : rc = SSL_SESSION_set_cipher(*sess, cipher);
701 [ - + ]: 140 : if (rc != 1) {
702 : 0 : SPDK_ERRLOG("Unable to set cipher: %s\n", cipher_name);
703 : 0 : goto err;
704 : : }
705 : 140 : found = true;
706 : 140 : break;
707 : : }
708 : : }
709 [ - + ]: 140 : if (found == false) {
710 : 0 : SPDK_ERRLOG("No suitable cipher found\n");
711 : 0 : goto err;
712 : : }
713 : :
714 [ - + - + ]: 140 : SPDK_DEBUGLOG(sock_posix, "Cipher selected: %s\n", cipher_name);
715 : :
716 : 140 : rc = SSL_SESSION_set_protocol_version(*sess, TLS1_3_VERSION);
717 [ - + ]: 140 : if (rc != 1) {
718 : 0 : SPDK_ERRLOG("Unable to set TLS version: %d\n", TLS1_3_VERSION);
719 : 0 : goto err;
720 : : }
721 : :
722 : 140 : rc = SSL_SESSION_set1_master_key(*sess, impl_opts->psk_key, keylen);
723 [ - + ]: 140 : if (rc != 1) {
724 : 0 : SPDK_ERRLOG("Unable to set PSK for session\n");
725 : 0 : goto err;
726 : : }
727 : :
728 [ - + ]: 140 : *identity_len = strlen(impl_opts->psk_identity);
729 : 140 : *identity = impl_opts->psk_identity;
730 : :
731 : 140 : return 1;
732 : :
733 : 0 : err:
734 : 0 : SSL_SESSION_free(*sess);
735 : 0 : *sess = NULL;
736 : 0 : return 0;
737 : : }
738 : :
739 : : static SSL_CTX *
740 : 289 : posix_sock_create_ssl_context(const SSL_METHOD *method, struct spdk_sock_opts *opts,
741 : : struct spdk_sock_impl_opts *impl_opts)
742 : : {
743 : : SSL_CTX *ctx;
744 : 289 : int tls_version = 0;
745 : 289 : bool ktls_enabled = false;
746 : : #ifdef SSL_OP_ENABLE_KTLS
747 : : long options;
748 : : #endif
749 : :
750 : 289 : SSL_library_init();
751 : 289 : OpenSSL_add_all_algorithms();
752 : 289 : SSL_load_error_strings();
753 : : /* Produce a SSL CTX in SSL V2 and V3 standards compliant way */
754 : 289 : ctx = SSL_CTX_new(method);
755 [ - + ]: 289 : if (!ctx) {
756 : 0 : SPDK_ERRLOG("SSL_CTX_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
757 : 0 : return NULL;
758 : : }
759 [ - + - + ]: 289 : SPDK_DEBUGLOG(sock_posix, "SSL context created\n");
760 : :
761 [ + + + ]: 289 : switch (impl_opts->tls_version) {
762 : 22 : case 0:
763 : : /* auto-negotioation */
764 : 22 : break;
765 : 265 : case SPDK_TLS_VERSION_1_3:
766 : 265 : tls_version = TLS1_3_VERSION;
767 : 265 : break;
768 : 2 : default:
769 : 2 : SPDK_ERRLOG("Incorrect TLS version provided: %d\n", impl_opts->tls_version);
770 : 2 : goto err;
771 : : }
772 : :
773 [ + + ]: 287 : if (tls_version) {
774 [ - + - + ]: 265 : SPDK_DEBUGLOG(sock_posix, "Hardening TLS version to '%d'='0x%X'\n", impl_opts->tls_version,
775 : : tls_version);
776 [ - + ]: 265 : if (!SSL_CTX_set_min_proto_version(ctx, tls_version)) {
777 : 0 : SPDK_ERRLOG("Unable to set Min TLS version to '%d'='0x%X\n", impl_opts->tls_version, tls_version);
778 : 0 : goto err;
779 : : }
780 [ - + ]: 265 : if (!SSL_CTX_set_max_proto_version(ctx, tls_version)) {
781 : 0 : SPDK_ERRLOG("Unable to set Max TLS version to '%d'='0x%X\n", impl_opts->tls_version, tls_version);
782 : 0 : goto err;
783 : : }
784 : : }
785 [ - + - + ]: 287 : if (impl_opts->enable_ktls) {
786 [ # # # # ]: 0 : SPDK_DEBUGLOG(sock_posix, "Enabling kTLS offload\n");
787 : : #ifdef SSL_OP_ENABLE_KTLS
788 : 0 : options = SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
789 : 0 : ktls_enabled = options & SSL_OP_ENABLE_KTLS;
790 : : #else
791 : 0 : ktls_enabled = false;
792 : : #endif
793 [ # # ]: 0 : if (!ktls_enabled) {
794 : 0 : SPDK_ERRLOG("Unable to set kTLS offload via SSL_CTX_set_options(). Configure openssl with 'enable-ktls'\n");
795 : 0 : goto err;
796 : : }
797 : : }
798 : :
799 : : /* SSL_CTX_set_ciphersuites() return 1 if the requested
800 : : * cipher suite list was configured, and 0 otherwise. */
801 [ + - - + ]: 574 : if (impl_opts->tls_cipher_suites != NULL &&
802 : 287 : SSL_CTX_set_ciphersuites(ctx, impl_opts->tls_cipher_suites) != 1) {
803 : 0 : SPDK_ERRLOG("Unable to set TLS cipher suites for SSL'\n");
804 : 0 : goto err;
805 : : }
806 : :
807 : 287 : return ctx;
808 : :
809 : 2 : err:
810 : 2 : SSL_CTX_free(ctx);
811 : 2 : return NULL;
812 : : }
813 : :
814 : : static SSL *
815 : 140 : ssl_sock_setup_connect(SSL_CTX *ctx, int fd)
816 : : {
817 : : SSL *ssl;
818 : :
819 : 140 : ssl = SSL_new(ctx);
820 [ - + ]: 140 : if (!ssl) {
821 : 0 : SPDK_ERRLOG("SSL_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
822 : 0 : return NULL;
823 : : }
824 : 140 : SSL_set_fd(ssl, fd);
825 : 140 : SSL_set_connect_state(ssl);
826 : 140 : SSL_set_psk_use_session_callback(ssl, posix_sock_psk_use_session_client_cb);
827 [ - + - + ]: 140 : SPDK_DEBUGLOG(sock_posix, "SSL object creation finished: %p\n", ssl);
828 [ - + - + ]: 140 : SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
829 [ - + - + ]: 140 : SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
830 [ - + - + ]: 140 : SPDK_DEBUGLOG(sock_posix, "Negotiated Cipher suite:%s\n",
831 : : SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
832 : 140 : return ssl;
833 : : }
834 : :
835 : : static SSL *
836 : 147 : ssl_sock_setup_accept(SSL_CTX *ctx, int fd)
837 : : {
838 : : SSL *ssl;
839 : :
840 : 147 : ssl = SSL_new(ctx);
841 [ - + ]: 147 : if (!ssl) {
842 : 0 : SPDK_ERRLOG("SSL_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
843 : 0 : return NULL;
844 : : }
845 : 147 : SSL_set_fd(ssl, fd);
846 : 147 : SSL_set_accept_state(ssl);
847 : 147 : SSL_set_psk_find_session_callback(ssl, posix_sock_psk_find_session_server_cb);
848 [ - + - + ]: 147 : SPDK_DEBUGLOG(sock_posix, "SSL object creation finished: %p\n", ssl);
849 [ - + - + ]: 147 : SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
850 [ - + - + ]: 147 : SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
851 [ - + - + ]: 147 : SPDK_DEBUGLOG(sock_posix, "Negotiated Cipher suite:%s\n",
852 : : SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
853 : 147 : return ssl;
854 : : }
855 : :
856 : : static ssize_t
857 : 7877871 : SSL_readv(SSL *ssl, const struct iovec *iov, int iovcnt)
858 : : {
859 : 7877871 : int i, rc = 0;
860 : 7877871 : ssize_t total = 0;
861 : :
862 [ + + ]: 9202699 : for (i = 0; i < iovcnt; i++) {
863 : 7877871 : rc = SSL_read(ssl, iov[i].iov_base, iov[i].iov_len);
864 : :
865 [ + + ]: 7877871 : if (rc > 0) {
866 : 4551290 : total += rc;
867 : : }
868 [ + + ]: 7877871 : if (rc != (int)iov[i].iov_len) {
869 : 6553043 : break;
870 : : }
871 : : }
872 [ + + ]: 7877871 : if (total > 0) {
873 : 4551290 : errno = 0;
874 : 4551290 : return total;
875 : : }
876 [ + + + - ]: 3326581 : switch (SSL_get_error(ssl, rc)) {
877 : 123 : case SSL_ERROR_ZERO_RETURN:
878 : 123 : errno = ENOTCONN;
879 : 123 : return 0;
880 : 3086245 : case SSL_ERROR_WANT_READ:
881 : : case SSL_ERROR_WANT_WRITE:
882 : : case SSL_ERROR_WANT_CONNECT:
883 : : case SSL_ERROR_WANT_ACCEPT:
884 : : case SSL_ERROR_WANT_X509_LOOKUP:
885 : : case SSL_ERROR_WANT_ASYNC:
886 : : case SSL_ERROR_WANT_ASYNC_JOB:
887 : : case SSL_ERROR_WANT_CLIENT_HELLO_CB:
888 : 3086245 : errno = EAGAIN;
889 : 3086245 : return -1;
890 : 240213 : case SSL_ERROR_SYSCALL:
891 : : case SSL_ERROR_SSL:
892 : 240213 : errno = ENOTCONN;
893 : 240213 : return -1;
894 : 0 : default:
895 : 0 : errno = ENOTCONN;
896 : 0 : return -1;
897 : : }
898 : : }
899 : :
900 : : static ssize_t
901 : 5114705 : SSL_writev(SSL *ssl, struct iovec *iov, int iovcnt)
902 : : {
903 : 5114705 : int i, rc = 0;
904 : 5114705 : ssize_t total = 0;
905 : :
906 [ + + ]: 9665724 : for (i = 0; i < iovcnt; i++) {
907 : 8733912 : rc = SSL_write(ssl, iov[i].iov_base, iov[i].iov_len);
908 : :
909 [ + + ]: 8733912 : if (rc > 0) {
910 : 4551019 : total += rc;
911 : : }
912 [ + + ]: 8733912 : if (rc != (int)iov[i].iov_len) {
913 : 4182893 : break;
914 : : }
915 : : }
916 [ + + ]: 5114705 : if (total > 0) {
917 : 1662264 : errno = 0;
918 : 1662264 : return total;
919 : : }
920 [ - + + - ]: 3452441 : switch (SSL_get_error(ssl, rc)) {
921 : 0 : case SSL_ERROR_ZERO_RETURN:
922 : 0 : errno = ENOTCONN;
923 : 0 : return 0;
924 : 3452425 : case SSL_ERROR_WANT_READ:
925 : : case SSL_ERROR_WANT_WRITE:
926 : : case SSL_ERROR_WANT_CONNECT:
927 : : case SSL_ERROR_WANT_ACCEPT:
928 : : case SSL_ERROR_WANT_X509_LOOKUP:
929 : : case SSL_ERROR_WANT_ASYNC:
930 : : case SSL_ERROR_WANT_ASYNC_JOB:
931 : : case SSL_ERROR_WANT_CLIENT_HELLO_CB:
932 : 3452425 : errno = EAGAIN;
933 : 3452425 : return -1;
934 : 16 : case SSL_ERROR_SYSCALL:
935 : : case SSL_ERROR_SSL:
936 : 16 : errno = ENOTCONN;
937 : 16 : return -1;
938 : 0 : default:
939 : 0 : errno = ENOTCONN;
940 : 0 : return -1;
941 : : }
942 : : }
943 : :
944 : : static struct spdk_sock *
945 : 19131 : posix_sock_create(const char *ip, int port,
946 : : enum posix_sock_create_type type,
947 : : struct spdk_sock_opts *opts,
948 : : bool enable_ssl)
949 : : {
950 : : struct spdk_posix_sock *sock;
951 : 193 : struct spdk_sock_impl_opts impl_opts;
952 : 193 : char buf[MAX_TMPBUF];
953 : 193 : char portnum[PORTNUMLEN];
954 : : char *p;
955 : 193 : struct addrinfo hints, *res, *res0;
956 : : int fd, flag;
957 : : int rc;
958 : 19131 : bool enable_zcopy_user_opts = true;
959 : 19131 : bool enable_zcopy_impl_opts = true;
960 : 19131 : SSL_CTX *ctx = 0;
961 : 19131 : SSL *ssl = 0;
962 : :
963 [ - + ]: 19131 : assert(opts != NULL);
964 [ + + ]: 19131 : if (enable_ssl) {
965 : 179 : _opts_get_impl_opts(opts, &impl_opts, &g_ssl_impl_opts);
966 : : } else {
967 : 18952 : _opts_get_impl_opts(opts, &impl_opts, &g_posix_impl_opts);
968 : : }
969 : :
970 [ - + ]: 19131 : if (ip == NULL) {
971 : 0 : return NULL;
972 : : }
973 [ - + ]: 19131 : if (ip[0] == '[') {
974 : 0 : snprintf(buf, sizeof(buf), "%s", ip + 1);
975 : 0 : p = strchr(buf, ']');
976 [ # # ]: 0 : if (p != NULL) {
977 : 0 : *p = '\0';
978 : : }
979 : 0 : ip = (const char *) &buf[0];
980 : : }
981 : :
982 : 19131 : snprintf(portnum, sizeof portnum, "%d", port);
983 : 19131 : memset(&hints, 0, sizeof hints);
984 : 19131 : hints.ai_family = PF_UNSPEC;
985 : 19131 : hints.ai_socktype = SOCK_STREAM;
986 : 19131 : hints.ai_flags = AI_NUMERICSERV;
987 : 19131 : hints.ai_flags |= AI_PASSIVE;
988 : 19131 : hints.ai_flags |= AI_NUMERICHOST;
989 : 19131 : rc = getaddrinfo(ip, portnum, &hints, &res0);
990 [ - + ]: 19131 : if (rc != 0) {
991 : 0 : SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n", gai_strerror(rc), rc);
992 : 0 : return NULL;
993 : : }
994 : :
995 : : /* try listen */
996 : 19131 : fd = -1;
997 [ + + ]: 34115 : for (res = res0; res != NULL; res = res->ai_next) {
998 : 19131 : retry:
999 : 19131 : fd = posix_fd_create(res, opts, &impl_opts);
1000 [ - + ]: 19131 : if (fd < 0) {
1001 : 0 : continue;
1002 : : }
1003 [ + + ]: 19131 : if (type == SPDK_SOCK_CREATE_LISTEN) {
1004 : 299 : rc = bind(fd, res->ai_addr, res->ai_addrlen);
1005 [ - + ]: 299 : if (rc != 0) {
1006 : 0 : SPDK_ERRLOG("bind() failed at port %d, errno = %d\n", port, errno);
1007 [ # # # ]: 0 : switch (errno) {
1008 : 0 : case EINTR:
1009 : : /* interrupted? */
1010 : 0 : close(fd);
1011 : 0 : goto retry;
1012 : 0 : case EADDRNOTAVAIL:
1013 : 0 : SPDK_ERRLOG("IP address %s not available. "
1014 : : "Verify IP address in config file "
1015 : : "and make sure setup script is "
1016 : : "run before starting spdk app.\n", ip);
1017 : : /* FALLTHROUGH */
1018 : 0 : default:
1019 : : /* try next family */
1020 : 0 : close(fd);
1021 : 0 : fd = -1;
1022 : 0 : continue;
1023 : : }
1024 : : }
1025 : : /* bind OK */
1026 : 299 : rc = listen(fd, 512);
1027 [ - + ]: 299 : if (rc != 0) {
1028 : 0 : SPDK_ERRLOG("listen() failed, errno = %d\n", errno);
1029 : 0 : close(fd);
1030 : 0 : fd = -1;
1031 : 0 : break;
1032 : : }
1033 [ - + ]: 299 : enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_server;
1034 [ + - ]: 18832 : } else if (type == SPDK_SOCK_CREATE_CONNECT) {
1035 : 18832 : rc = connect(fd, res->ai_addr, res->ai_addrlen);
1036 [ + + ]: 18832 : if (rc != 0) {
1037 : 14984 : SPDK_ERRLOG("connect() failed, errno = %d\n", errno);
1038 : : /* try next family */
1039 : 14984 : close(fd);
1040 : 14984 : fd = -1;
1041 : 14984 : continue;
1042 : : }
1043 [ - + ]: 3848 : enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_client;
1044 [ + + ]: 3848 : if (enable_ssl) {
1045 : 142 : ctx = posix_sock_create_ssl_context(TLS_client_method(), opts, &impl_opts);
1046 [ + + ]: 142 : if (!ctx) {
1047 : 2 : SPDK_ERRLOG("posix_sock_create_ssl_context() failed, errno = %d\n", errno);
1048 : 2 : close(fd);
1049 : 2 : fd = -1;
1050 : 2 : break;
1051 : : }
1052 : 140 : ssl = ssl_sock_setup_connect(ctx, fd);
1053 [ - + ]: 140 : if (!ssl) {
1054 : 0 : SPDK_ERRLOG("ssl_sock_setup_connect() failed, errno = %d\n", errno);
1055 : 0 : close(fd);
1056 : 0 : fd = -1;
1057 : 0 : SSL_CTX_free(ctx);
1058 : 0 : break;
1059 : : }
1060 : : }
1061 : : }
1062 : :
1063 : 4145 : flag = fcntl(fd, F_GETFL);
1064 [ - + ]: 4145 : if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
1065 : 0 : SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
1066 : 0 : SSL_free(ssl);
1067 : 0 : SSL_CTX_free(ctx);
1068 : 0 : close(fd);
1069 : 0 : fd = -1;
1070 : 0 : break;
1071 : : }
1072 : 4145 : break;
1073 : : }
1074 : 19131 : freeaddrinfo(res0);
1075 : :
1076 [ + + ]: 19131 : if (fd < 0) {
1077 : 14986 : return NULL;
1078 : : }
1079 : :
1080 : : /* Only enable zero copy for non-loopback and non-ssl sockets. */
1081 [ + + + + : 4145 : enable_zcopy_user_opts = opts->zcopy && !sock_is_loopback(fd) && !enable_ssl;
+ + + + ]
1082 : :
1083 [ + + + + ]: 4145 : sock = posix_sock_alloc(fd, &impl_opts, enable_zcopy_user_opts && enable_zcopy_impl_opts);
1084 [ - + ]: 4145 : if (sock == NULL) {
1085 : 0 : SPDK_ERRLOG("sock allocation failed\n");
1086 : 0 : SSL_free(ssl);
1087 : 0 : SSL_CTX_free(ctx);
1088 : 0 : close(fd);
1089 : 0 : return NULL;
1090 : : }
1091 : :
1092 [ + + ]: 4145 : if (ctx) {
1093 : 140 : sock->ctx = ctx;
1094 : : }
1095 : :
1096 [ + + ]: 4145 : if (ssl) {
1097 : 140 : sock->ssl = ssl;
1098 : 140 : SSL_set_app_data(ssl, &sock->base.impl_opts);
1099 : : }
1100 : :
1101 : 4145 : return &sock->base;
1102 : : }
1103 : :
1104 : : static struct spdk_sock *
1105 : 262 : posix_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
1106 : : {
1107 : 262 : return posix_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts, false);
1108 : : }
1109 : :
1110 : : static struct spdk_sock *
1111 : 18690 : posix_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
1112 : : {
1113 : 18690 : return posix_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts, false);
1114 : : }
1115 : :
1116 : : static struct spdk_sock *
1117 : 1489112 : _posix_sock_accept(struct spdk_sock *_sock, bool enable_ssl)
1118 : : {
1119 : 1489112 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1120 : 46895 : struct sockaddr_storage sa;
1121 : 46895 : socklen_t salen;
1122 : : int rc, fd;
1123 : : struct spdk_posix_sock *new_sock;
1124 : : int flag;
1125 : 1489112 : SSL_CTX *ctx = 0;
1126 : 1489112 : SSL *ssl = 0;
1127 : :
1128 : 1489112 : memset(&sa, 0, sizeof(sa));
1129 : 1489112 : salen = sizeof(sa);
1130 : :
1131 [ - + ]: 1489112 : assert(sock != NULL);
1132 : :
1133 : 1489112 : rc = accept(sock->fd, (struct sockaddr *)&sa, &salen);
1134 : :
1135 [ + + ]: 1489112 : if (rc == -1) {
1136 : 1481671 : return NULL;
1137 : : }
1138 : :
1139 : 7441 : fd = rc;
1140 : :
1141 : 7441 : flag = fcntl(fd, F_GETFL);
1142 [ + - - + ]: 7441 : if ((!(flag & O_NONBLOCK)) && (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0)) {
1143 : 0 : SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
1144 : 0 : close(fd);
1145 : 0 : return NULL;
1146 : : }
1147 : :
1148 : : #if defined(SO_PRIORITY)
1149 : : /* The priority is not inherited, so call this function again */
1150 [ + + ]: 7441 : if (sock->base.opts.priority) {
1151 : 5 : rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &sock->base.opts.priority, sizeof(int));
1152 [ - + ]: 5 : if (rc != 0) {
1153 : 0 : close(fd);
1154 : 0 : return NULL;
1155 : : }
1156 : : }
1157 : : #endif
1158 : :
1159 : : /* Establish SSL connection */
1160 [ + + ]: 7441 : if (enable_ssl) {
1161 : 147 : ctx = posix_sock_create_ssl_context(TLS_server_method(), &sock->base.opts, &sock->base.impl_opts);
1162 [ - + ]: 147 : if (!ctx) {
1163 : 0 : SPDK_ERRLOG("posix_sock_create_ssl_context() failed, errno = %d\n", errno);
1164 : 0 : close(fd);
1165 : 0 : return NULL;
1166 : : }
1167 : 147 : ssl = ssl_sock_setup_accept(ctx, fd);
1168 [ - + ]: 147 : if (!ssl) {
1169 : 0 : SPDK_ERRLOG("ssl_sock_setup_accept() failed, errno = %d\n", errno);
1170 : 0 : close(fd);
1171 : 0 : SSL_CTX_free(ctx);
1172 : 0 : return NULL;
1173 : : }
1174 : : }
1175 : :
1176 : : /* Inherit the zero copy feature from the listen socket */
1177 [ - + ]: 7441 : new_sock = posix_sock_alloc(fd, &sock->base.impl_opts, sock->zcopy);
1178 [ - + ]: 7441 : if (new_sock == NULL) {
1179 : 0 : close(fd);
1180 : 0 : SSL_free(ssl);
1181 : 0 : SSL_CTX_free(ctx);
1182 : 0 : return NULL;
1183 : : }
1184 : :
1185 [ + + ]: 7441 : if (ctx) {
1186 : 147 : new_sock->ctx = ctx;
1187 : : }
1188 : :
1189 [ + + ]: 7441 : if (ssl) {
1190 : 147 : new_sock->ssl = ssl;
1191 : 147 : SSL_set_app_data(ssl, &new_sock->base.impl_opts);
1192 : : }
1193 : :
1194 : 7441 : return &new_sock->base;
1195 : : }
1196 : :
1197 : : static struct spdk_sock *
1198 : 1442438 : posix_sock_accept(struct spdk_sock *_sock)
1199 : : {
1200 : 1442438 : return _posix_sock_accept(_sock, false);
1201 : : }
1202 : :
1203 : : static int
1204 : 11586 : posix_sock_close(struct spdk_sock *_sock)
1205 : : {
1206 : 11586 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1207 : : void *pipe_buf;
1208 : :
1209 [ - + ]: 11586 : assert(TAILQ_EMPTY(&_sock->pending_reqs));
1210 : :
1211 [ + + ]: 11586 : if (sock->ssl != NULL) {
1212 : 287 : SSL_shutdown(sock->ssl);
1213 : : }
1214 : :
1215 : : /* If the socket fails to close, the best choice is to
1216 : : * leak the fd but continue to free the rest of the sock
1217 : : * memory. */
1218 : 11586 : close(sock->fd);
1219 : :
1220 : 11586 : SSL_free(sock->ssl);
1221 : 11586 : SSL_CTX_free(sock->ctx);
1222 : :
1223 : 11586 : pipe_buf = spdk_pipe_destroy(sock->recv_pipe);
1224 : 11586 : free(pipe_buf);
1225 : 11586 : free(sock);
1226 : :
1227 : 11586 : return 0;
1228 : : }
1229 : :
1230 : : #ifdef SPDK_ZEROCOPY
1231 : : static int
1232 : 6449698 : _sock_check_zcopy(struct spdk_sock *sock)
1233 : : {
1234 : 6449698 : struct spdk_posix_sock *psock = __posix_sock(sock);
1235 : 6449698 : struct msghdr msgh = {};
1236 : 85938 : uint8_t buf[sizeof(struct cmsghdr) + sizeof(struct sock_extended_err)];
1237 : : ssize_t rc;
1238 : : struct sock_extended_err *serr;
1239 : : struct cmsghdr *cm;
1240 : : uint32_t idx;
1241 : : struct spdk_sock_request *req, *treq;
1242 : : bool found;
1243 : :
1244 : 6449698 : msgh.msg_control = buf;
1245 : 6449698 : msgh.msg_controllen = sizeof(buf);
1246 : :
1247 : : while (true) {
1248 : 12913353 : rc = recvmsg(psock->fd, &msgh, MSG_ERRQUEUE);
1249 : :
1250 [ + + ]: 12913353 : if (rc < 0) {
1251 [ - + - - ]: 6449698 : if (errno == EWOULDBLOCK || errno == EAGAIN) {
1252 : 6449698 : return 0;
1253 : : }
1254 : :
1255 [ # # ]: 0 : if (!TAILQ_EMPTY(&sock->pending_reqs)) {
1256 : 0 : SPDK_ERRLOG("Attempting to receive from ERRQUEUE yielded error, but pending list still has orphaned entries\n");
1257 : : } else {
1258 : 0 : SPDK_WARNLOG("Recvmsg yielded an error!\n");
1259 : : }
1260 : 0 : return 0;
1261 : : }
1262 : :
1263 [ + - ]: 6463655 : cm = CMSG_FIRSTHDR(&msgh);
1264 [ + - ]: 6463655 : if (!(cm &&
1265 [ + - - + ]: 6463655 : ((cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_RECVERR) ||
1266 [ # # # # ]: 0 : (cm->cmsg_level == SOL_IPV6 && cm->cmsg_type == IPV6_RECVERR)))) {
1267 : 0 : SPDK_WARNLOG("Unexpected cmsg level or type!\n");
1268 : 0 : return 0;
1269 : : }
1270 : :
1271 : 6463655 : serr = (struct sock_extended_err *)CMSG_DATA(cm);
1272 [ + - - + ]: 6463655 : if (serr->ee_errno != 0 || serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
1273 : 0 : SPDK_WARNLOG("Unexpected extended error origin\n");
1274 : 0 : return 0;
1275 : : }
1276 : :
1277 : : /* Most of the time, the pending_reqs array is in the exact
1278 : : * order we need such that all of the requests to complete are
1279 : : * in order, in the front. It is guaranteed that all requests
1280 : : * belonging to the same sendmsg call are sequential, so once
1281 : : * we encounter one match we can stop looping as soon as a
1282 : : * non-match is found.
1283 : : */
1284 : 6463655 : idx = serr->ee_info;
1285 : : while (true) {
1286 : 8239728 : found = false;
1287 [ + + ]: 35814476 : TAILQ_FOREACH_SAFE(req, &sock->pending_reqs, internal.link, treq) {
1288 [ - + - + ]: 31143333 : if (!req->internal.is_zcopy) {
1289 : : /* This wasn't a zcopy request. It was just waiting in line to complete */
1290 : 0 : rc = spdk_sock_request_put(sock, req, 0);
1291 [ # # ]: 0 : if (rc < 0) {
1292 : 0 : return rc;
1293 : : }
1294 [ + + ]: 31143333 : } else if (req->internal.offset == idx) {
1295 : 26667851 : found = true;
1296 : 26667851 : rc = spdk_sock_request_put(sock, req, 0);
1297 [ - + ]: 26667851 : if (rc < 0) {
1298 : 0 : return rc;
1299 : : }
1300 [ + + ]: 4475482 : } else if (found) {
1301 : 3568585 : break;
1302 : : }
1303 : : }
1304 : :
1305 [ + + ]: 8239728 : if (idx == serr->ee_data) {
1306 : 6463655 : break;
1307 : : }
1308 : :
1309 [ - + ]: 1776073 : if (idx == UINT32_MAX) {
1310 : 0 : idx = 0;
1311 : : } else {
1312 : 1776073 : idx++;
1313 : : }
1314 : : }
1315 : : }
1316 : :
1317 : : return 0;
1318 : : }
1319 : : #endif
1320 : :
1321 : : static int
1322 : 2161711153 : _sock_flush(struct spdk_sock *sock)
1323 : : {
1324 : 2161711153 : struct spdk_posix_sock *psock = __posix_sock(sock);
1325 : 2161711153 : struct msghdr msg = {};
1326 : 98513181 : int flags;
1327 : 98513181 : struct iovec iovs[IOV_BATCH_SIZE];
1328 : : int iovcnt;
1329 : : int retval;
1330 : : struct spdk_sock_request *req;
1331 : : int i;
1332 : : ssize_t rc, sent;
1333 : : unsigned int offset;
1334 : : size_t len;
1335 : 2161711153 : bool is_zcopy = false;
1336 : :
1337 : : /* Can't flush from within a callback or we end up with recursive calls */
1338 [ + + ]: 2161711153 : if (sock->cb_cnt > 0) {
1339 : 570295 : errno = EAGAIN;
1340 : 570295 : return -1;
1341 : : }
1342 : :
1343 : : #ifdef SPDK_ZEROCOPY
1344 [ - + + + ]: 2161140829 : if (psock->zcopy) {
1345 : 1813295449 : flags = MSG_ZEROCOPY | MSG_NOSIGNAL;
1346 : : } else
1347 : : #endif
1348 : : {
1349 : 347845409 : flags = MSG_NOSIGNAL;
1350 : : }
1351 : :
1352 : 2161140858 : iovcnt = spdk_sock_prep_reqs(sock, iovs, 0, NULL, &flags);
1353 [ + + ]: 2161140858 : if (iovcnt == 0) {
1354 : 2141213942 : return 0;
1355 : : }
1356 : :
1357 : : #ifdef SPDK_ZEROCOPY
1358 : 19926909 : is_zcopy = flags & MSG_ZEROCOPY;
1359 : : #endif
1360 : :
1361 : : /* Perform the vectored write */
1362 : 19926916 : msg.msg_iov = iovs;
1363 : 19926916 : msg.msg_iovlen = iovcnt;
1364 : :
1365 [ + + ]: 19926916 : if (psock->ssl) {
1366 : 5114216 : rc = SSL_writev(psock->ssl, iovs, iovcnt);
1367 : : } else {
1368 : 14812700 : rc = sendmsg(psock->fd, &msg, flags);
1369 : : }
1370 [ + + ]: 19926916 : if (rc <= 0) {
1371 [ + - + + : 8028779 : if (rc == 0 || errno == EAGAIN || errno == EWOULDBLOCK || (errno == ENOBUFS && psock->zcopy)) {
+ - - + -
- - - ]
1372 : 8028752 : errno = EAGAIN;
1373 : : }
1374 : 8028779 : return -1;
1375 : : }
1376 : :
1377 : 11898137 : sent = rc;
1378 : :
1379 [ + + ]: 11898137 : if (is_zcopy) {
1380 : : /* Handling overflow case, because we use psock->sendmsg_idx - 1 for the
1381 : : * req->internal.offset, so sendmsg_idx should not be zero */
1382 [ - + ]: 8239814 : if (spdk_unlikely(psock->sendmsg_idx == UINT32_MAX)) {
1383 : 0 : psock->sendmsg_idx = 1;
1384 : : } else {
1385 : 8239814 : psock->sendmsg_idx++;
1386 : : }
1387 : : }
1388 : :
1389 : : /* Consume the requests that were actually written */
1390 : 11898137 : req = TAILQ_FIRST(&sock->queued_reqs);
1391 [ + - ]: 42817698 : while (req) {
1392 : 42817698 : offset = req->internal.offset;
1393 : :
1394 : : /* req->internal.is_zcopy is true when the whole req or part of it is sent with zerocopy */
1395 : 42817698 : req->internal.is_zcopy = is_zcopy;
1396 : :
1397 [ + + ]: 119111147 : for (i = 0; i < req->iovcnt; i++) {
1398 : : /* Advance by the offset first */
1399 [ + + ]: 77641878 : if (offset >= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len) {
1400 : 3510178 : offset -= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len;
1401 : 3510178 : continue;
1402 : : }
1403 : :
1404 : : /* Calculate the remaining length of this element */
1405 : 74131700 : len = SPDK_SOCK_REQUEST_IOV(req, i)->iov_len - offset;
1406 : :
1407 [ + + ]: 74131700 : if (len > (size_t)rc) {
1408 : : /* This element was partially sent. */
1409 : 1348429 : req->internal.offset += rc;
1410 : 1348429 : return sent;
1411 : : }
1412 : :
1413 : 72783271 : offset = 0;
1414 : 72783271 : req->internal.offset += len;
1415 : 72783271 : rc -= len;
1416 : : }
1417 : :
1418 : : /* Handled a full request. */
1419 : 41469269 : spdk_sock_request_pend(sock, req);
1420 : :
1421 [ + + + + : 41469269 : if (!req->internal.is_zcopy && req == TAILQ_FIRST(&sock->pending_reqs)) {
+ - ]
1422 : : /* The sendmsg syscall above isn't currently asynchronous,
1423 : : * so it's already done. */
1424 : 14800775 : retval = spdk_sock_request_put(sock, req, 0);
1425 [ + + ]: 14800775 : if (retval) {
1426 : 6 : break;
1427 : : }
1428 : : } else {
1429 : : /* Re-use the offset field to hold the sendmsg call index. The
1430 : : * index is 0 based, so subtract one here because we've already
1431 : : * incremented above. */
1432 : 26668494 : req->internal.offset = psock->sendmsg_idx - 1;
1433 : : }
1434 : :
1435 [ + + ]: 41469263 : if (rc == 0) {
1436 : 10549702 : break;
1437 : : }
1438 : :
1439 : 30919561 : req = TAILQ_FIRST(&sock->queued_reqs);
1440 : : }
1441 : :
1442 : 10549708 : return sent;
1443 : : }
1444 : :
1445 : : static int
1446 : 28284593 : posix_sock_flush(struct spdk_sock *sock)
1447 : : {
1448 : : #ifdef SPDK_ZEROCOPY
1449 : 28284592 : struct spdk_posix_sock *psock = __posix_sock(sock);
1450 : :
1451 [ - + + + : 28284592 : if (psock->zcopy && !TAILQ_EMPTY(&sock->pending_reqs)) {
+ + ]
1452 : 32 : _sock_check_zcopy(sock);
1453 : : }
1454 : : #endif
1455 : :
1456 : 28284593 : return _sock_flush(sock);
1457 : : }
1458 : :
1459 : : static ssize_t
1460 : 96330943 : posix_sock_recv_from_pipe(struct spdk_posix_sock *sock, struct iovec *diov, int diovcnt)
1461 : : {
1462 : 1450757 : struct iovec siov[2];
1463 : : int sbytes;
1464 : : ssize_t bytes;
1465 : : struct spdk_posix_sock_group_impl *group;
1466 : :
1467 : 96330943 : sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
1468 [ - + ]: 96330943 : if (sbytes < 0) {
1469 : 0 : errno = EINVAL;
1470 : 0 : return -1;
1471 [ + + ]: 96330943 : } else if (sbytes == 0) {
1472 : 10791904 : errno = EAGAIN;
1473 : 10791904 : return -1;
1474 : : }
1475 : :
1476 : 85539039 : bytes = spdk_iovcpy(siov, 2, diov, diovcnt);
1477 : :
1478 [ - + ]: 85539039 : if (bytes == 0) {
1479 : : /* The only way this happens is if diov is 0 length */
1480 : 0 : errno = EINVAL;
1481 : 0 : return -1;
1482 : : }
1483 : :
1484 : 85539039 : spdk_pipe_reader_advance(sock->recv_pipe, bytes);
1485 : :
1486 : : /* If we drained the pipe, mark it appropriately */
1487 [ + + ]: 85539039 : if (spdk_pipe_reader_bytes_available(sock->recv_pipe) == 0) {
1488 [ - + - + ]: 13165606 : assert(sock->pipe_has_data == true);
1489 : :
1490 : 13165606 : group = __posix_group_impl(sock->base.group_impl);
1491 [ + + - + : 13165606 : if (group && !sock->socket_has_data) {
+ + ]
1492 [ + + ]: 10911135 : TAILQ_REMOVE(&group->socks_with_data, sock, link);
1493 : : }
1494 : :
1495 : 13165606 : sock->pipe_has_data = false;
1496 : : }
1497 : :
1498 : 85539039 : return bytes;
1499 : : }
1500 : :
1501 : : static inline ssize_t
1502 : 33526712 : posix_sock_read(struct spdk_posix_sock *sock)
1503 : : {
1504 : 398977 : struct iovec iov[2];
1505 : : int bytes_avail, bytes_recvd;
1506 : : struct spdk_posix_sock_group_impl *group;
1507 : :
1508 : 33526712 : bytes_avail = spdk_pipe_writer_get_buffer(sock->recv_pipe, sock->recv_buf_sz, iov);
1509 : :
1510 [ - + ]: 33526712 : if (bytes_avail <= 0) {
1511 : 0 : return bytes_avail;
1512 : : }
1513 : :
1514 [ + + ]: 33526712 : if (sock->ssl) {
1515 : 3328623 : bytes_recvd = SSL_readv(sock->ssl, iov, 2);
1516 : : } else {
1517 : 30198089 : bytes_recvd = readv(sock->fd, iov, 2);
1518 : : }
1519 : :
1520 [ - + - + ]: 33526712 : assert(sock->pipe_has_data == false);
1521 : :
1522 [ + + ]: 33526712 : if (bytes_recvd <= 0) {
1523 : : /* Errors count as draining the socket data */
1524 [ + + - + : 20361090 : if (sock->base.group_impl && sock->socket_has_data) {
+ - ]
1525 : 537123 : group = __posix_group_impl(sock->base.group_impl);
1526 [ + + ]: 537123 : TAILQ_REMOVE(&group->socks_with_data, sock, link);
1527 : : }
1528 : :
1529 : 20361090 : sock->socket_has_data = false;
1530 : :
1531 : 20361090 : return bytes_recvd;
1532 : : }
1533 : :
1534 : 13165622 : spdk_pipe_writer_advance(sock->recv_pipe, bytes_recvd);
1535 : :
1536 : : #if DEBUG
1537 [ + + ]: 13165622 : if (sock->base.group_impl) {
1538 [ - + - + ]: 12835527 : assert(sock->socket_has_data == true);
1539 : : }
1540 : : #endif
1541 : :
1542 : 13165622 : sock->pipe_has_data = true;
1543 [ + + ]: 13165622 : if (bytes_recvd < bytes_avail) {
1544 : : /* We drained the kernel socket entirely. */
1545 : 11230155 : sock->socket_has_data = false;
1546 : : }
1547 : :
1548 : 13165622 : return bytes_recvd;
1549 : : }
1550 : :
1551 : : static ssize_t
1552 : 139506941 : posix_sock_readv(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
1553 : : {
1554 : 139506941 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1555 : 139506941 : struct spdk_posix_sock_group_impl *group = __posix_group_impl(sock->base.group_impl);
1556 : : int rc, i;
1557 : : size_t len;
1558 : :
1559 [ + + ]: 139506941 : if (sock->recv_pipe == NULL) {
1560 [ - + - + ]: 13941759 : assert(sock->pipe_has_data == false);
1561 [ + + + + : 13941759 : if (group && sock->socket_has_data) {
+ + ]
1562 : 8883 : sock->socket_has_data = false;
1563 [ + + ]: 8883 : TAILQ_REMOVE(&group->socks_with_data, sock, link);
1564 : : }
1565 [ + + ]: 13941759 : if (sock->ssl) {
1566 : 2647048 : return SSL_readv(sock->ssl, iov, iovcnt);
1567 : : } else {
1568 : 11294711 : return readv(sock->fd, iov, iovcnt);
1569 : : }
1570 : : }
1571 : :
1572 : : /* If the socket is not in a group, we must assume it always has
1573 : : * data waiting for us because it is not epolled */
1574 [ - + + + : 125565182 : if (!sock->pipe_has_data && (group == NULL || sock->socket_has_data)) {
+ + - + +
+ ]
1575 : : /* If the user is receiving a sufficiently large amount of data,
1576 : : * receive directly to their buffers. */
1577 : 42399861 : len = 0;
1578 [ + + ]: 91514079 : for (i = 0; i < iovcnt; i++) {
1579 : 49114218 : len += iov[i].iov_len;
1580 : : }
1581 : :
1582 [ + + ]: 42399861 : if (len >= MIN_SOCK_PIPE_SIZE) {
1583 : : /* TODO: Should this detect if kernel socket is drained? */
1584 [ + + ]: 8873149 : if (sock->ssl) {
1585 : 1902200 : return SSL_readv(sock->ssl, iov, iovcnt);
1586 : : } else {
1587 : 6970949 : return readv(sock->fd, iov, iovcnt);
1588 : : }
1589 : : }
1590 : :
1591 : : /* Otherwise, do a big read into our pipe */
1592 : 33526712 : rc = posix_sock_read(sock);
1593 [ + + ]: 33526712 : if (rc <= 0) {
1594 : 20361090 : return rc;
1595 : : }
1596 : : }
1597 : :
1598 : 96330943 : return posix_sock_recv_from_pipe(sock, iov, iovcnt);
1599 : : }
1600 : :
1601 : : static ssize_t
1602 : 136286127 : posix_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
1603 : : {
1604 : 2248334 : struct iovec iov[1];
1605 : :
1606 : 136286127 : iov[0].iov_base = buf;
1607 : 136286127 : iov[0].iov_len = len;
1608 : :
1609 : 136286127 : return posix_sock_readv(sock, iov, 1);
1610 : : }
1611 : :
1612 : : static ssize_t
1613 : 539 : posix_sock_writev(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
1614 : : {
1615 : 539 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1616 : : int rc;
1617 : :
1618 : : /* In order to process a writev, we need to flush any asynchronous writes
1619 : : * first. */
1620 : 539 : rc = _sock_flush(_sock);
1621 [ - + ]: 539 : if (rc < 0) {
1622 : 0 : return rc;
1623 : : }
1624 : :
1625 [ - + ]: 539 : if (!TAILQ_EMPTY(&_sock->queued_reqs)) {
1626 : : /* We weren't able to flush all requests */
1627 : 0 : errno = EAGAIN;
1628 : 0 : return -1;
1629 : : }
1630 : :
1631 [ + + ]: 539 : if (sock->ssl) {
1632 : 489 : return SSL_writev(sock->ssl, iov, iovcnt);
1633 : : } else {
1634 : 50 : return writev(sock->fd, iov, iovcnt);
1635 : : }
1636 : : }
1637 : :
1638 : : static int
1639 : 36 : posix_sock_recv_next(struct spdk_sock *_sock, void **buf, void **ctx)
1640 : : {
1641 : 36 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1642 : 0 : struct iovec iov;
1643 : : ssize_t rc;
1644 : :
1645 [ - + ]: 36 : if (sock->recv_pipe != NULL) {
1646 : 0 : errno = ENOTSUP;
1647 : 0 : return -1;
1648 : : }
1649 : :
1650 : 36 : iov.iov_len = spdk_sock_group_get_buf(_sock->group_impl->group, &iov.iov_base, ctx);
1651 [ - + ]: 36 : if (iov.iov_len == 0) {
1652 : 0 : errno = ENOBUFS;
1653 : 0 : return -1;
1654 : : }
1655 : :
1656 : 36 : rc = posix_sock_readv(_sock, &iov, 1);
1657 [ + + ]: 36 : if (rc <= 0) {
1658 : 26 : spdk_sock_group_provide_buf(_sock->group_impl->group, iov.iov_base, iov.iov_len, *ctx);
1659 : 26 : return rc;
1660 : : }
1661 : :
1662 : 10 : *buf = iov.iov_base;
1663 : :
1664 : 10 : return rc;
1665 : : }
1666 : :
1667 : : static void
1668 : 41469995 : posix_sock_writev_async(struct spdk_sock *sock, struct spdk_sock_request *req)
1669 : : {
1670 : : int rc;
1671 : :
1672 : 41469995 : spdk_sock_request_queue(sock, req);
1673 : :
1674 : : /* If there are a sufficient number queued, just flush them out immediately. */
1675 [ + + ]: 41469995 : if (sock->queued_iovcnt >= IOV_BATCH_SIZE) {
1676 : 2347838 : rc = _sock_flush(sock);
1677 [ + + + + ]: 2347838 : if (rc < 0 && errno != EAGAIN) {
1678 : 3 : spdk_sock_abort_requests(sock);
1679 : : }
1680 : : }
1681 : 41469995 : }
1682 : :
1683 : : static int
1684 : 7389 : posix_sock_set_recvlowat(struct spdk_sock *_sock, int nbytes)
1685 : : {
1686 : 7389 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1687 : 98 : int val;
1688 : : int rc;
1689 : :
1690 [ - + ]: 7389 : assert(sock != NULL);
1691 : :
1692 : 7389 : val = nbytes;
1693 : 7389 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVLOWAT, &val, sizeof val);
1694 [ - + ]: 7389 : if (rc != 0) {
1695 : 0 : return -1;
1696 : : }
1697 : 7389 : return 0;
1698 : : }
1699 : :
1700 : : static bool
1701 : 6 : posix_sock_is_ipv6(struct spdk_sock *_sock)
1702 : : {
1703 : 6 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1704 : 5 : struct sockaddr_storage sa;
1705 : 5 : socklen_t salen;
1706 : : int rc;
1707 : :
1708 [ - + ]: 6 : assert(sock != NULL);
1709 : :
1710 : 6 : memset(&sa, 0, sizeof sa);
1711 : 6 : salen = sizeof sa;
1712 : 6 : rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
1713 [ - + ]: 6 : if (rc != 0) {
1714 : 0 : SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
1715 : 0 : return false;
1716 : : }
1717 : :
1718 : 6 : return (sa.ss_family == AF_INET6);
1719 : : }
1720 : :
1721 : : static bool
1722 : 9079 : posix_sock_is_ipv4(struct spdk_sock *_sock)
1723 : : {
1724 : 9079 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1725 : 195 : struct sockaddr_storage sa;
1726 : 195 : socklen_t salen;
1727 : : int rc;
1728 : :
1729 [ - + ]: 9079 : assert(sock != NULL);
1730 : :
1731 : 9079 : memset(&sa, 0, sizeof sa);
1732 : 9079 : salen = sizeof sa;
1733 : 9079 : rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
1734 [ - + ]: 9079 : if (rc != 0) {
1735 : 0 : SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
1736 : 0 : return false;
1737 : : }
1738 : :
1739 : 9079 : return (sa.ss_family == AF_INET);
1740 : : }
1741 : :
1742 : : static bool
1743 : 264910 : posix_sock_is_connected(struct spdk_sock *_sock)
1744 : : {
1745 : 264910 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1746 : 108 : uint8_t byte;
1747 : : int rc;
1748 : :
1749 : 264910 : rc = recv(sock->fd, &byte, 1, MSG_PEEK);
1750 [ + + ]: 264910 : if (rc == 0) {
1751 : 264880 : return false;
1752 : : }
1753 : :
1754 [ + + ]: 30 : if (rc < 0) {
1755 [ - + - - ]: 27 : if (errno == EAGAIN || errno == EWOULDBLOCK) {
1756 : 27 : return true;
1757 : : }
1758 : :
1759 : 0 : return false;
1760 : : }
1761 : :
1762 : 3 : return true;
1763 : : }
1764 : :
1765 : : static struct spdk_sock_group_impl *
1766 : 7101 : posix_sock_group_impl_get_optimal(struct spdk_sock *_sock, struct spdk_sock_group_impl *hint)
1767 : : {
1768 : 7101 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1769 : 98 : struct spdk_sock_group_impl *group_impl;
1770 : :
1771 [ + + ]: 7101 : if (sock->placement_id != -1) {
1772 : 5 : spdk_sock_map_lookup(&g_map, sock->placement_id, &group_impl, hint);
1773 : 5 : return group_impl;
1774 : : }
1775 : :
1776 : 7096 : return NULL;
1777 : : }
1778 : :
1779 : : static struct spdk_sock_group_impl *
1780 : 4432 : _sock_group_impl_create(uint32_t enable_placement_id)
1781 : : {
1782 : : struct spdk_posix_sock_group_impl *group_impl;
1783 : : int fd;
1784 : :
1785 : : #if defined(SPDK_EPOLL)
1786 : 4432 : fd = epoll_create1(0);
1787 : : #elif defined(SPDK_KEVENT)
1788 : : fd = kqueue();
1789 : : #endif
1790 [ - + ]: 4432 : if (fd == -1) {
1791 : 0 : return NULL;
1792 : : }
1793 : :
1794 : 4432 : group_impl = calloc(1, sizeof(*group_impl));
1795 [ - + ]: 4432 : if (group_impl == NULL) {
1796 : 0 : SPDK_ERRLOG("group_impl allocation failed\n");
1797 : 0 : close(fd);
1798 : 0 : return NULL;
1799 : : }
1800 : :
1801 : 4432 : group_impl->pipe_group = spdk_pipe_group_create();
1802 [ - + ]: 4432 : if (group_impl->pipe_group == NULL) {
1803 : 0 : SPDK_ERRLOG("pipe_group allocation failed\n");
1804 : 0 : free(group_impl);
1805 : 0 : close(fd);
1806 : 0 : return NULL;
1807 : : }
1808 : :
1809 : 4432 : group_impl->fd = fd;
1810 : 4432 : TAILQ_INIT(&group_impl->socks_with_data);
1811 : 4432 : group_impl->placement_id = -1;
1812 : :
1813 [ - + ]: 4432 : if (enable_placement_id == PLACEMENT_CPU) {
1814 : 0 : spdk_sock_map_insert(&g_map, spdk_env_get_current_core(), &group_impl->base);
1815 : 0 : group_impl->placement_id = spdk_env_get_current_core();
1816 : : }
1817 : :
1818 : 4432 : return &group_impl->base;
1819 : : }
1820 : :
1821 : : static struct spdk_sock_group_impl *
1822 : 2216 : posix_sock_group_impl_create(void)
1823 : : {
1824 : 2216 : return _sock_group_impl_create(g_posix_impl_opts.enable_placement_id);
1825 : : }
1826 : :
1827 : : static struct spdk_sock_group_impl *
1828 : 2216 : ssl_sock_group_impl_create(void)
1829 : : {
1830 : 2216 : return _sock_group_impl_create(g_ssl_impl_opts.enable_placement_id);
1831 : : }
1832 : :
1833 : : static void
1834 : 0 : posix_sock_mark(struct spdk_posix_sock_group_impl *group, struct spdk_posix_sock *sock,
1835 : : int placement_id)
1836 : : {
1837 : : #if defined(SO_MARK)
1838 : : int rc;
1839 : :
1840 : 0 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_MARK,
1841 : : &placement_id, sizeof(placement_id));
1842 [ # # ]: 0 : if (rc != 0) {
1843 : : /* Not fatal */
1844 : 0 : SPDK_ERRLOG("Error setting SO_MARK\n");
1845 : 0 : return;
1846 : : }
1847 : :
1848 : 0 : rc = spdk_sock_map_insert(&g_map, placement_id, &group->base);
1849 [ # # ]: 0 : if (rc != 0) {
1850 : : /* Not fatal */
1851 : 0 : SPDK_ERRLOG("Failed to insert sock group into map: %d\n", rc);
1852 : 0 : return;
1853 : : }
1854 : :
1855 : 0 : sock->placement_id = placement_id;
1856 : : #endif
1857 : : }
1858 : :
1859 : : static void
1860 : 0 : posix_sock_update_mark(struct spdk_sock_group_impl *_group, struct spdk_sock *_sock)
1861 : : {
1862 : 0 : struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
1863 : :
1864 [ # # ]: 0 : if (group->placement_id == -1) {
1865 : 0 : group->placement_id = spdk_sock_map_find_free(&g_map);
1866 : :
1867 : : /* If a free placement id is found, update existing sockets in this group */
1868 [ # # ]: 0 : if (group->placement_id != -1) {
1869 : : struct spdk_sock *sock, *tmp;
1870 : :
1871 [ # # ]: 0 : TAILQ_FOREACH_SAFE(sock, &_group->socks, link, tmp) {
1872 : 0 : posix_sock_mark(group, __posix_sock(sock), group->placement_id);
1873 : : }
1874 : : }
1875 : : }
1876 : :
1877 [ # # ]: 0 : if (group->placement_id != -1) {
1878 : : /*
1879 : : * group placement id is already determined for this poll group.
1880 : : * Mark socket with group's placement id.
1881 : : */
1882 : 0 : posix_sock_mark(group, __posix_sock(_sock), group->placement_id);
1883 : : }
1884 : 0 : }
1885 : :
1886 : : static int
1887 : 8351 : posix_sock_group_impl_add_sock(struct spdk_sock_group_impl *_group, struct spdk_sock *_sock)
1888 : : {
1889 : 8351 : struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
1890 : 8351 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1891 : : int rc;
1892 : :
1893 : : #if defined(SPDK_EPOLL)
1894 : 172 : struct epoll_event event;
1895 : :
1896 : 8351 : memset(&event, 0, sizeof(event));
1897 : : /* EPOLLERR is always on even if we don't set it, but be explicit for clarity */
1898 : 8351 : event.events = EPOLLIN | EPOLLERR;
1899 : 8351 : event.data.ptr = sock;
1900 : :
1901 : 8351 : rc = epoll_ctl(group->fd, EPOLL_CTL_ADD, sock->fd, &event);
1902 : : #elif defined(SPDK_KEVENT)
1903 : : struct kevent event;
1904 : : struct timespec ts = {0};
1905 : :
1906 : : EV_SET(&event, sock->fd, EVFILT_READ, EV_ADD, 0, 0, sock);
1907 : :
1908 : : rc = kevent(group->fd, &event, 1, NULL, 0, &ts);
1909 : : #endif
1910 : :
1911 [ - + ]: 8351 : if (rc != 0) {
1912 : 0 : return rc;
1913 : : }
1914 : :
1915 : : /* switched from another polling group due to scheduling */
1916 [ + + - + ]: 8351 : if (spdk_unlikely(sock->recv_pipe != NULL &&
1917 : : (spdk_pipe_reader_bytes_available(sock->recv_pipe) > 0))) {
1918 : 0 : sock->pipe_has_data = true;
1919 : 0 : sock->socket_has_data = false;
1920 : 0 : TAILQ_INSERT_TAIL(&group->socks_with_data, sock, link);
1921 [ + + ]: 8351 : } else if (sock->recv_pipe != NULL) {
1922 : 244 : rc = spdk_pipe_group_add(group->pipe_group, sock->recv_pipe);
1923 [ - + ]: 244 : assert(rc == 0);
1924 : : }
1925 : :
1926 [ - + ]: 8351 : if (_sock->impl_opts.enable_placement_id == PLACEMENT_MARK) {
1927 : 0 : posix_sock_update_mark(_group, _sock);
1928 [ + + ]: 8351 : } else if (sock->placement_id != -1) {
1929 : 5 : rc = spdk_sock_map_insert(&g_map, sock->placement_id, &group->base);
1930 [ - + ]: 5 : if (rc != 0) {
1931 : 0 : SPDK_ERRLOG("Failed to insert sock group into map: %d\n", rc);
1932 : : /* Do not treat this as an error. The system will continue running. */
1933 : : }
1934 : : }
1935 : :
1936 : 8351 : return rc;
1937 : : }
1938 : :
1939 : : static int
1940 : 8351 : posix_sock_group_impl_remove_sock(struct spdk_sock_group_impl *_group, struct spdk_sock *_sock)
1941 : : {
1942 : 8351 : struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
1943 : 8351 : struct spdk_posix_sock *sock = __posix_sock(_sock);
1944 : : int rc;
1945 : :
1946 [ + + + + : 8351 : if (sock->pipe_has_data || sock->socket_has_data) {
- + + + ]
1947 [ + + ]: 40 : TAILQ_REMOVE(&group->socks_with_data, sock, link);
1948 : 40 : sock->pipe_has_data = false;
1949 : 40 : sock->socket_has_data = false;
1950 [ + + ]: 8311 : } else if (sock->recv_pipe != NULL) {
1951 : 8218 : rc = spdk_pipe_group_remove(group->pipe_group, sock->recv_pipe);
1952 [ - + ]: 8218 : assert(rc == 0);
1953 : : }
1954 : :
1955 [ + + ]: 8351 : if (sock->placement_id != -1) {
1956 : 5 : spdk_sock_map_release(&g_map, sock->placement_id);
1957 : : }
1958 : :
1959 : : #if defined(SPDK_EPOLL)
1960 : 172 : struct epoll_event event;
1961 : :
1962 : : /* Event parameter is ignored but some old kernel version still require it. */
1963 : 8351 : rc = epoll_ctl(group->fd, EPOLL_CTL_DEL, sock->fd, &event);
1964 : : #elif defined(SPDK_KEVENT)
1965 : : struct kevent event;
1966 : : struct timespec ts = {0};
1967 : :
1968 : : EV_SET(&event, sock->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
1969 : :
1970 : : rc = kevent(group->fd, &event, 1, NULL, 0, &ts);
1971 : : if (rc == 0 && event.flags & EV_ERROR) {
1972 : : rc = -1;
1973 : : errno = event.data;
1974 : : }
1975 : : #endif
1976 : :
1977 : 8351 : spdk_sock_abort_requests(_sock);
1978 : :
1979 : 8351 : return rc;
1980 : : }
1981 : :
1982 : : static int
1983 : 912635905 : posix_sock_group_impl_poll(struct spdk_sock_group_impl *_group, int max_events,
1984 : : struct spdk_sock **socks)
1985 : : {
1986 : 912635905 : struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
1987 : : struct spdk_sock *sock, *tmp;
1988 : : int num_events, i, rc;
1989 : : struct spdk_posix_sock *psock, *ptmp;
1990 : : #if defined(SPDK_EPOLL)
1991 : 55064976 : struct epoll_event events[MAX_EVENTS_PER_POLL];
1992 : : #elif defined(SPDK_KEVENT)
1993 : : struct kevent events[MAX_EVENTS_PER_POLL];
1994 : : struct timespec ts = {0};
1995 : : #endif
1996 : :
1997 : : #ifdef SPDK_ZEROCOPY
1998 : : /* When all of the following conditions are met
1999 : : * - non-blocking socket
2000 : : * - zero copy is enabled
2001 : : * - interrupts suppressed (i.e. busy polling)
2002 : : * - the NIC tx queue is full at the time sendmsg() is called
2003 : : * - epoll_wait determines there is an EPOLLIN event for the socket
2004 : : * then we can get into a situation where data we've sent is queued
2005 : : * up in the kernel network stack, but interrupts have been suppressed
2006 : : * because other traffic is flowing so the kernel misses the signal
2007 : : * to flush the software tx queue. If there wasn't incoming data
2008 : : * pending on the socket, then epoll_wait would have been sufficient
2009 : : * to kick off the send operation, but since there is a pending event
2010 : : * epoll_wait does not trigger the necessary operation.
2011 : : *
2012 : : * We deal with this by checking for all of the above conditions and
2013 : : * additionally looking for EPOLLIN events that were not consumed from
2014 : : * the last poll loop. We take this to mean that the upper layer is
2015 : : * unable to consume them because it is blocked waiting for resources
2016 : : * to free up, and those resources are most likely freed in response
2017 : : * to a pending asynchronous write completing.
2018 : : *
2019 : : * Additionally, sockets that have the same placement_id actually share
2020 : : * an underlying hardware queue. That means polling one of them is
2021 : : * equivalent to polling all of them. As a quick mechanism to avoid
2022 : : * making extra poll() calls, stash the last placement_id during the loop
2023 : : * and only poll if it's not the same. The overwhelmingly common case
2024 : : * is that all sockets in this list have the same placement_id because
2025 : : * SPDK is intentionally grouping sockets by that value, so even
2026 : : * though this won't stop all extra calls to poll(), it's very fast
2027 : : * and will catch all of them in practice.
2028 : : */
2029 : 912635898 : int last_placement_id = -1;
2030 : :
2031 [ + + ]: 920407186 : TAILQ_FOREACH(psock, &group->socks_with_data, link) {
2032 [ - + + + : 7771288 : if (psock->zcopy && psock->placement_id >= 0 &&
- + ]
2033 [ # # ]: 0 : psock->placement_id != last_placement_id) {
2034 : 0 : struct pollfd pfd = {psock->fd, POLLIN | POLLERR, 0};
2035 : :
2036 : 0 : poll(&pfd, 1, 0);
2037 : 0 : last_placement_id = psock->placement_id;
2038 : : }
2039 : : }
2040 : : #endif
2041 : :
2042 : : /* This must be a TAILQ_FOREACH_SAFE because while flushing,
2043 : : * a completion callback could remove the sock from the
2044 : : * group. */
2045 [ + + ]: 3043714052 : TAILQ_FOREACH_SAFE(sock, &_group->socks, link, tmp) {
2046 : 2131078147 : rc = _sock_flush(sock);
2047 [ + + + + ]: 2131078147 : if (rc < 0 && errno != EAGAIN) {
2048 : 9 : spdk_sock_abort_requests(sock);
2049 : : }
2050 : : }
2051 : :
2052 [ - + ]: 912635905 : assert(max_events > 0);
2053 : :
2054 : : #if defined(SPDK_EPOLL)
2055 : 912635905 : num_events = epoll_wait(group->fd, events, max_events, 0);
2056 : : #elif defined(SPDK_KEVENT)
2057 : : num_events = kevent(group->fd, NULL, 0, events, max_events, &ts);
2058 : : #endif
2059 : :
2060 [ - + ]: 912635905 : if (num_events == -1) {
2061 : 0 : return -1;
2062 [ + + + + ]: 912635905 : } else if (num_events == 0 && !TAILQ_EMPTY(&_group->socks)) {
2063 : 897925787 : sock = TAILQ_FIRST(&_group->socks);
2064 : 897925787 : psock = __posix_sock(sock);
2065 : : /* poll() is called here to busy poll the queue associated with
2066 : : * first socket in list and potentially reap incoming data.
2067 : : */
2068 [ + + ]: 897925787 : if (sock->opts.priority) {
2069 : 48386 : struct pollfd pfd = {0, 0, 0};
2070 : :
2071 : 48386 : pfd.fd = psock->fd;
2072 : 48386 : pfd.events = POLLIN | POLLERR;
2073 : 48386 : poll(&pfd, 1, 0);
2074 : : }
2075 : : }
2076 : :
2077 [ + + ]: 927956144 : for (i = 0; i < num_events; i++) {
2078 : : #if defined(SPDK_EPOLL)
2079 : 15320239 : sock = events[i].data.ptr;
2080 : 15320239 : psock = __posix_sock(sock);
2081 : :
2082 : : #ifdef SPDK_ZEROCOPY
2083 [ + + ]: 15320234 : if (events[i].events & EPOLLERR) {
2084 : 6449666 : rc = _sock_check_zcopy(sock);
2085 : : /* If the socket was closed or removed from
2086 : : * the group in response to a send ack, don't
2087 : : * add it to the array here. */
2088 [ + - + + ]: 6449666 : if (rc || sock->cb_fn == NULL) {
2089 : 244 : continue;
2090 : : }
2091 : : }
2092 : : #endif
2093 [ + + ]: 15319995 : if ((events[i].events & EPOLLIN) == 0) {
2094 : 1092421 : continue;
2095 : : }
2096 : :
2097 : : #elif defined(SPDK_KEVENT)
2098 : : sock = events[i].udata;
2099 : : psock = __posix_sock(sock);
2100 : : #endif
2101 : :
2102 : : /* If the socket is not already in the list, add it now */
2103 [ + + + + : 14227574 : if (!psock->socket_has_data && !psock->pipe_has_data) {
- + + + ]
2104 : 11457181 : TAILQ_INSERT_TAIL(&group->socks_with_data, psock, link);
2105 : : }
2106 : 14227574 : psock->socket_has_data = true;
2107 : : }
2108 : :
2109 : 912635905 : num_events = 0;
2110 : :
2111 [ + + ]: 931864373 : TAILQ_FOREACH_SAFE(psock, &group->socks_with_data, link, ptmp) {
2112 [ - + ]: 19228468 : if (num_events == max_events) {
2113 : 0 : break;
2114 : : }
2115 : :
2116 : : /* If the socket's cb_fn is NULL, just remove it from the
2117 : : * list and do not add it to socks array */
2118 [ - + ]: 19228468 : if (spdk_unlikely(psock->base.cb_fn == NULL)) {
2119 : 0 : psock->socket_has_data = false;
2120 : 0 : psock->pipe_has_data = false;
2121 [ # # ]: 0 : TAILQ_REMOVE(&group->socks_with_data, psock, link);
2122 : 0 : continue;
2123 : : }
2124 : :
2125 : 19228468 : socks[num_events++] = &psock->base;
2126 : : }
2127 : :
2128 : : /* Cycle the has_data list so that each time we poll things aren't
2129 : : * in the same order. Say we have 6 sockets in the list, named as follows:
2130 : : * A B C D E F
2131 : : * And all 6 sockets had epoll events, but max_events is only 3. That means
2132 : : * psock currently points at D. We want to rearrange the list to the following:
2133 : : * D E F A B C
2134 : : *
2135 : : * The variables below are named according to this example to make it easier to
2136 : : * follow the swaps.
2137 : : */
2138 [ - + ]: 912635905 : if (psock != NULL) {
2139 : : struct spdk_posix_sock *pa, *pc, *pd, *pf;
2140 : :
2141 : : /* Capture pointers to the elements we need */
2142 : 0 : pd = psock;
2143 : 0 : pc = TAILQ_PREV(pd, spdk_has_data_list, link);
2144 : 0 : pa = TAILQ_FIRST(&group->socks_with_data);
2145 : 0 : pf = TAILQ_LAST(&group->socks_with_data, spdk_has_data_list);
2146 : :
2147 : : /* Break the link between C and D */
2148 : 0 : pc->link.tqe_next = NULL;
2149 : :
2150 : : /* Connect F to A */
2151 : 0 : pf->link.tqe_next = pa;
2152 : 0 : pa->link.tqe_prev = &pf->link.tqe_next;
2153 : :
2154 : : /* Fix up the list first/last pointers */
2155 : 0 : group->socks_with_data.tqh_first = pd;
2156 : 0 : group->socks_with_data.tqh_last = &pc->link.tqe_next;
2157 : :
2158 : : /* D is in front of the list, make tqe prev pointer point to the head of list */
2159 : 0 : pd->link.tqe_prev = &group->socks_with_data.tqh_first;
2160 : : }
2161 : :
2162 : 912635905 : return num_events;
2163 : : }
2164 : :
2165 : : static int
2166 : 4432 : _sock_group_impl_close(struct spdk_sock_group_impl *_group, uint32_t enable_placement_id)
2167 : : {
2168 : 4432 : struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
2169 : : int rc;
2170 : :
2171 [ - + ]: 4432 : if (enable_placement_id == PLACEMENT_CPU) {
2172 : 0 : spdk_sock_map_release(&g_map, spdk_env_get_current_core());
2173 : : }
2174 : :
2175 : 4432 : spdk_pipe_group_destroy(group->pipe_group);
2176 : 4432 : rc = close(group->fd);
2177 : 4432 : free(group);
2178 : 4432 : return rc;
2179 : : }
2180 : :
2181 : : static int
2182 : 2216 : posix_sock_group_impl_close(struct spdk_sock_group_impl *_group)
2183 : : {
2184 : 2216 : return _sock_group_impl_close(_group, g_posix_impl_opts.enable_placement_id);
2185 : : }
2186 : :
2187 : : static int
2188 : 2216 : ssl_sock_group_impl_close(struct spdk_sock_group_impl *_group)
2189 : : {
2190 : 2216 : return _sock_group_impl_close(_group, g_ssl_impl_opts.enable_placement_id);
2191 : : }
2192 : :
2193 : : static struct spdk_net_impl g_posix_net_impl = {
2194 : : .name = "posix",
2195 : : .getaddr = posix_sock_getaddr,
2196 : : .connect = posix_sock_connect,
2197 : : .listen = posix_sock_listen,
2198 : : .accept = posix_sock_accept,
2199 : : .close = posix_sock_close,
2200 : : .recv = posix_sock_recv,
2201 : : .readv = posix_sock_readv,
2202 : : .writev = posix_sock_writev,
2203 : : .recv_next = posix_sock_recv_next,
2204 : : .writev_async = posix_sock_writev_async,
2205 : : .flush = posix_sock_flush,
2206 : : .set_recvlowat = posix_sock_set_recvlowat,
2207 : : .set_recvbuf = posix_sock_set_recvbuf,
2208 : : .set_sendbuf = posix_sock_set_sendbuf,
2209 : : .is_ipv6 = posix_sock_is_ipv6,
2210 : : .is_ipv4 = posix_sock_is_ipv4,
2211 : : .is_connected = posix_sock_is_connected,
2212 : : .group_impl_get_optimal = posix_sock_group_impl_get_optimal,
2213 : : .group_impl_create = posix_sock_group_impl_create,
2214 : : .group_impl_add_sock = posix_sock_group_impl_add_sock,
2215 : : .group_impl_remove_sock = posix_sock_group_impl_remove_sock,
2216 : : .group_impl_poll = posix_sock_group_impl_poll,
2217 : : .group_impl_close = posix_sock_group_impl_close,
2218 : : .get_opts = posix_sock_impl_get_opts,
2219 : : .set_opts = posix_sock_impl_set_opts,
2220 : : };
2221 : :
2222 : 2853 : SPDK_NET_IMPL_REGISTER_DEFAULT(posix, &g_posix_net_impl);
2223 : :
2224 : : static struct spdk_sock *
2225 : 37 : ssl_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
2226 : : {
2227 : 37 : return posix_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts, true);
2228 : : }
2229 : :
2230 : : static struct spdk_sock *
2231 : 142 : ssl_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
2232 : : {
2233 : 142 : return posix_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts, true);
2234 : : }
2235 : :
2236 : : static struct spdk_sock *
2237 : 46674 : ssl_sock_accept(struct spdk_sock *_sock)
2238 : : {
2239 : 46674 : return _posix_sock_accept(_sock, true);
2240 : : }
2241 : :
2242 : : static struct spdk_net_impl g_ssl_net_impl = {
2243 : : .name = "ssl",
2244 : : .getaddr = posix_sock_getaddr,
2245 : : .connect = ssl_sock_connect,
2246 : : .listen = ssl_sock_listen,
2247 : : .accept = ssl_sock_accept,
2248 : : .close = posix_sock_close,
2249 : : .recv = posix_sock_recv,
2250 : : .readv = posix_sock_readv,
2251 : : .writev = posix_sock_writev,
2252 : : .recv_next = posix_sock_recv_next,
2253 : : .writev_async = posix_sock_writev_async,
2254 : : .flush = posix_sock_flush,
2255 : : .set_recvlowat = posix_sock_set_recvlowat,
2256 : : .set_recvbuf = posix_sock_set_recvbuf,
2257 : : .set_sendbuf = posix_sock_set_sendbuf,
2258 : : .is_ipv6 = posix_sock_is_ipv6,
2259 : : .is_ipv4 = posix_sock_is_ipv4,
2260 : : .is_connected = posix_sock_is_connected,
2261 : : .group_impl_get_optimal = posix_sock_group_impl_get_optimal,
2262 : : .group_impl_create = ssl_sock_group_impl_create,
2263 : : .group_impl_add_sock = posix_sock_group_impl_add_sock,
2264 : : .group_impl_remove_sock = posix_sock_group_impl_remove_sock,
2265 : : .group_impl_poll = posix_sock_group_impl_poll,
2266 : : .group_impl_close = ssl_sock_group_impl_close,
2267 : : .get_opts = ssl_sock_impl_get_opts,
2268 : : .set_opts = ssl_sock_impl_set_opts,
2269 : : };
2270 : :
2271 : 2853 : SPDK_NET_IMPL_REGISTER(ssl, &g_ssl_net_impl);
2272 : 2853 : SPDK_LOG_REGISTER_COMPONENT(sock_posix)
|