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