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