Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2019 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : : #include "spdk/config.h"
8 : :
9 : : #include <linux/errqueue.h>
10 : : #include <sys/epoll.h>
11 : : #include <liburing.h>
12 : :
13 : : #include "spdk/barrier.h"
14 : : #include "spdk/env.h"
15 : : #include "spdk/log.h"
16 : : #include "spdk/pipe.h"
17 : : #include "spdk/sock.h"
18 : : #include "spdk/string.h"
19 : : #include "spdk/util.h"
20 : :
21 : : #include "spdk_internal/sock.h"
22 : : #include "spdk_internal/assert.h"
23 : : #include "../sock_kernel.h"
24 : :
25 : : #define MAX_TMPBUF 1024
26 : : #define PORTNUMLEN 32
27 : : #define SPDK_SOCK_GROUP_QUEUE_DEPTH 4096
28 : : #define SPDK_SOCK_CMG_INFO_SIZE (sizeof(struct cmsghdr) + sizeof(struct sock_extended_err))
29 : :
30 : : enum uring_task_type {
31 : : URING_TASK_READ = 0,
32 : : URING_TASK_ERRQUEUE,
33 : : URING_TASK_WRITE,
34 : : URING_TASK_CANCEL,
35 : : };
36 : :
37 : : #if defined(SO_ZEROCOPY) && defined(MSG_ZEROCOPY)
38 : : #define SPDK_ZEROCOPY
39 : : #endif
40 : :
41 : : /* We don't know how big the buffers that the user posts will be, but this
42 : : * is the maximum we'll ever allow it to receive in a single command.
43 : : * If the user buffers are smaller, it will just receive less. */
44 : : #define URING_MAX_RECV_SIZE (128 * 1024)
45 : :
46 : : /* We don't know how many buffers the user will post, but this is the
47 : : * maximum number we'll take from the pool to post per group. */
48 : : #define URING_BUF_POOL_SIZE 128
49 : :
50 : : /* We use 1 just so it's not zero and we can validate it's right. */
51 : : #define URING_BUF_GROUP_ID 1
52 : :
53 : : enum spdk_uring_sock_task_status {
54 : : SPDK_URING_SOCK_TASK_NOT_IN_USE = 0,
55 : : SPDK_URING_SOCK_TASK_IN_PROCESS,
56 : : };
57 : :
58 : : struct spdk_uring_task {
59 : : enum spdk_uring_sock_task_status status;
60 : : enum uring_task_type type;
61 : : struct spdk_uring_sock *sock;
62 : : struct msghdr msg;
63 : : struct iovec iovs[IOV_BATCH_SIZE];
64 : : int iov_cnt;
65 : : struct spdk_sock_request *last_req;
66 : : bool is_zcopy;
67 : : STAILQ_ENTRY(spdk_uring_task) link;
68 : : };
69 : :
70 : : struct spdk_uring_sock {
71 : : struct spdk_sock base;
72 : : int fd;
73 : : uint32_t sendmsg_idx;
74 : : struct spdk_uring_sock_group_impl *group;
75 : : STAILQ_HEAD(, spdk_uring_buf_tracker) recv_stream;
76 : : size_t recv_offset;
77 : : struct spdk_uring_task write_task;
78 : : struct spdk_uring_task errqueue_task;
79 : : struct spdk_uring_task read_task;
80 : : struct spdk_uring_task cancel_task;
81 : : struct spdk_pipe *recv_pipe;
82 : : void *recv_buf;
83 : : int recv_buf_sz;
84 : : bool zcopy;
85 : : bool pending_recv;
86 : : bool pending_group_remove;
87 : : int zcopy_send_flags;
88 : : int connection_status;
89 : : int placement_id;
90 : : uint8_t reserved[4];
91 : : uint8_t buf[SPDK_SOCK_CMG_INFO_SIZE];
92 : : TAILQ_ENTRY(spdk_uring_sock) link;
93 : : };
94 : : /* 'struct cmsghdr' is mapped to the buffer 'buf', and while first element
95 : : * of this control message header has a size of 8 bytes, 'buf'
96 : : * must be 8-byte aligned.
97 : : */
98 : : SPDK_STATIC_ASSERT(offsetof(struct spdk_uring_sock, buf) % 8 == 0,
99 : : "Incorrect alignment: `buf` must be aligned to 8 bytes");
100 : :
101 : : TAILQ_HEAD(pending_recv_list, spdk_uring_sock);
102 : :
103 : : struct spdk_uring_buf_tracker {
104 : : void *buf;
105 : : size_t buflen;
106 : : size_t len;
107 : : void *ctx;
108 : : int id;
109 : : STAILQ_ENTRY(spdk_uring_buf_tracker) link;
110 : : };
111 : :
112 : : struct spdk_uring_sock_group_impl {
113 : : struct spdk_sock_group_impl base;
114 : : struct io_uring uring;
115 : : uint32_t io_inflight;
116 : : uint32_t io_queued;
117 : : uint32_t io_avail;
118 : : struct pending_recv_list pending_recv;
119 : :
120 : : struct io_uring_buf_ring *buf_ring;
121 : : uint32_t buf_ring_count;
122 : : struct spdk_uring_buf_tracker *trackers;
123 : : STAILQ_HEAD(, spdk_uring_buf_tracker) free_trackers;
124 : : };
125 : :
126 : : static struct spdk_sock_impl_opts g_spdk_uring_sock_impl_opts = {
127 : : .recv_buf_size = DEFAULT_SO_RCVBUF_SIZE,
128 : : .send_buf_size = DEFAULT_SO_SNDBUF_SIZE,
129 : : .enable_recv_pipe = true,
130 : : .enable_quickack = false,
131 : : .enable_placement_id = PLACEMENT_NONE,
132 : : .enable_zerocopy_send_server = false,
133 : : .enable_zerocopy_send_client = false,
134 : : .zerocopy_threshold = 0,
135 : : .tls_version = 0,
136 : : .enable_ktls = false,
137 : : .psk_key = NULL,
138 : : .psk_identity = NULL
139 : : };
140 : :
141 : : static struct spdk_sock_map g_map = {
142 : : .entries = STAILQ_HEAD_INITIALIZER(g_map.entries),
143 : : .mtx = PTHREAD_MUTEX_INITIALIZER
144 : : };
145 : :
146 : : __attribute((destructor)) static void
147 : 393 : uring_sock_map_cleanup(void)
148 : : {
149 : 393 : spdk_sock_map_cleanup(&g_map);
150 : 393 : }
151 : :
152 : : #define SPDK_URING_SOCK_REQUEST_IOV(req) ((struct iovec *)((uint8_t *)req + sizeof(struct spdk_sock_request)))
153 : :
154 : : #define __uring_sock(sock) (struct spdk_uring_sock *)sock
155 : : #define __uring_group_impl(group) (struct spdk_uring_sock_group_impl *)group
156 : :
157 : : static void
158 : 35 : uring_sock_copy_impl_opts(struct spdk_sock_impl_opts *dest, const struct spdk_sock_impl_opts *src,
159 : : size_t len)
160 : : {
161 : : #define FIELD_OK(field) \
162 : : offsetof(struct spdk_sock_impl_opts, field) + sizeof(src->field) <= len
163 : :
164 : : #define SET_FIELD(field) \
165 : : if (FIELD_OK(field)) { \
166 : : dest->field = src->field; \
167 : : }
168 : :
169 [ + - ]: 35 : SET_FIELD(recv_buf_size);
170 [ + - ]: 35 : SET_FIELD(send_buf_size);
171 [ + - - + ]: 35 : SET_FIELD(enable_recv_pipe);
172 [ + - - + ]: 35 : SET_FIELD(enable_quickack);
173 [ + - ]: 35 : SET_FIELD(enable_placement_id);
174 [ + - - + ]: 35 : SET_FIELD(enable_zerocopy_send_server);
175 [ + - - + ]: 35 : SET_FIELD(enable_zerocopy_send_client);
176 [ + - ]: 35 : SET_FIELD(zerocopy_threshold);
177 [ + - ]: 35 : SET_FIELD(tls_version);
178 [ + - - + ]: 35 : SET_FIELD(enable_ktls);
179 [ + - ]: 35 : SET_FIELD(psk_key);
180 [ + - ]: 35 : SET_FIELD(psk_identity);
181 : :
182 : : #undef SET_FIELD
183 : : #undef FIELD_OK
184 : 35 : }
185 : :
186 : : static int
187 : 26 : uring_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
188 : : {
189 [ + - - + ]: 26 : if (!opts || !len) {
190 : 0 : errno = EINVAL;
191 : 0 : return -1;
192 : : }
193 : :
194 [ - + ]: 26 : assert(sizeof(*opts) >= *len);
195 [ - + ]: 26 : memset(opts, 0, *len);
196 : :
197 : 26 : uring_sock_copy_impl_opts(opts, &g_spdk_uring_sock_impl_opts, *len);
198 : 26 : *len = spdk_min(*len, sizeof(g_spdk_uring_sock_impl_opts));
199 : :
200 : 26 : return 0;
201 : : }
202 : :
203 : : static int
204 : 9 : uring_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
205 : : {
206 [ - + ]: 9 : if (!opts) {
207 : 0 : errno = EINVAL;
208 : 0 : return -1;
209 : : }
210 : :
211 [ - + ]: 9 : assert(sizeof(*opts) >= len);
212 : 9 : uring_sock_copy_impl_opts(&g_spdk_uring_sock_impl_opts, opts, len);
213 : :
214 : 9 : return 0;
215 : : }
216 : :
217 : : static void
218 : 511 : uring_opts_get_impl_opts(const struct spdk_sock_opts *opts, struct spdk_sock_impl_opts *dest)
219 : : {
220 : : /* Copy the default impl_opts first to cover cases when user's impl_opts is smaller */
221 [ - + - + ]: 511 : memcpy(dest, &g_spdk_uring_sock_impl_opts, sizeof(*dest));
222 : :
223 [ - + ]: 511 : if (opts->impl_opts != NULL) {
224 [ # # ]: 0 : assert(sizeof(*dest) >= opts->impl_opts_size);
225 : 0 : uring_sock_copy_impl_opts(dest, opts->impl_opts, opts->impl_opts_size);
226 : : }
227 : 511 : }
228 : :
229 : : static int
230 : 1611 : uring_sock_getaddr(struct spdk_sock *_sock, char *saddr, int slen, uint16_t *sport,
231 : : char *caddr, int clen, uint16_t *cport)
232 : : {
233 : 1611 : struct spdk_uring_sock *sock = __uring_sock(_sock);
234 : 0 : struct sockaddr_storage sa;
235 : 0 : socklen_t salen;
236 : : int rc;
237 : :
238 [ - + ]: 1611 : assert(sock != NULL);
239 : :
240 : 1611 : memset(&sa, 0, sizeof sa);
241 : 1611 : salen = sizeof sa;
242 : 1611 : rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
243 [ - + ]: 1611 : if (rc != 0) {
244 : 0 : SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
245 : 0 : return -1;
246 : : }
247 : :
248 [ - + - ]: 1611 : switch (sa.ss_family) {
249 : 0 : case AF_UNIX:
250 : : /* Acceptable connection types that don't have IPs */
251 : 0 : return 0;
252 : 1611 : case AF_INET:
253 : : case AF_INET6:
254 : : /* Code below will get IP addresses */
255 : 1611 : break;
256 : 0 : default:
257 : : /* Unsupported socket family */
258 : 0 : return -1;
259 : : }
260 : :
261 : 1611 : rc = get_addr_str((struct sockaddr *)&sa, saddr, slen);
262 [ - + ]: 1611 : if (rc != 0) {
263 : 0 : SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
264 : 0 : return -1;
265 : : }
266 : :
267 [ + + ]: 1611 : if (sport) {
268 [ + - ]: 1334 : if (sa.ss_family == AF_INET) {
269 : 1334 : *sport = ntohs(((struct sockaddr_in *) &sa)->sin_port);
270 [ # # ]: 0 : } else if (sa.ss_family == AF_INET6) {
271 : 0 : *sport = ntohs(((struct sockaddr_in6 *) &sa)->sin6_port);
272 : : }
273 : : }
274 : :
275 : 1611 : memset(&sa, 0, sizeof sa);
276 : 1611 : salen = sizeof sa;
277 : 1611 : rc = getpeername(sock->fd, (struct sockaddr *) &sa, &salen);
278 [ - + ]: 1611 : if (rc != 0) {
279 : 0 : SPDK_ERRLOG("getpeername() failed (errno=%d)\n", errno);
280 : 0 : return -1;
281 : : }
282 : :
283 : 1611 : rc = get_addr_str((struct sockaddr *)&sa, caddr, clen);
284 [ - + ]: 1611 : if (rc != 0) {
285 : 0 : SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
286 : 0 : return -1;
287 : : }
288 : :
289 [ + + ]: 1611 : if (cport) {
290 [ + - ]: 1334 : if (sa.ss_family == AF_INET) {
291 : 1334 : *cport = ntohs(((struct sockaddr_in *) &sa)->sin_port);
292 [ # # ]: 0 : } else if (sa.ss_family == AF_INET6) {
293 : 0 : *cport = ntohs(((struct sockaddr_in6 *) &sa)->sin6_port);
294 : : }
295 : : }
296 : :
297 : 1611 : return 0;
298 : : }
299 : :
300 : : enum uring_sock_create_type {
301 : : SPDK_SOCK_CREATE_LISTEN,
302 : : SPDK_SOCK_CREATE_CONNECT,
303 : : };
304 : :
305 : : static int
306 : 3372 : uring_sock_alloc_pipe(struct spdk_uring_sock *sock, int sz)
307 : : {
308 : 0 : uint8_t *new_buf;
309 : : struct spdk_pipe *new_pipe;
310 : 0 : struct iovec siov[2];
311 : 0 : struct iovec diov[2];
312 : : int sbytes;
313 : : ssize_t bytes;
314 : : int rc;
315 : :
316 [ + + ]: 3372 : if (sock->recv_buf_sz == sz) {
317 : 2039 : return 0;
318 : : }
319 : :
320 : : /* If the new size is 0, just free the pipe */
321 [ - + ]: 1333 : if (sz == 0) {
322 : 0 : spdk_pipe_destroy(sock->recv_pipe);
323 : 0 : free(sock->recv_buf);
324 : 0 : sock->recv_pipe = NULL;
325 : 0 : sock->recv_buf = NULL;
326 : 0 : return 0;
327 [ - + ]: 1333 : } else if (sz < MIN_SOCK_PIPE_SIZE) {
328 : 0 : SPDK_ERRLOG("The size of the pipe must be larger than %d\n", MIN_SOCK_PIPE_SIZE);
329 : 0 : return -1;
330 : : }
331 : :
332 : : /* Round up to next 64 byte multiple */
333 [ - + ]: 1333 : rc = posix_memalign((void **)&new_buf, 64, sz);
334 [ - + ]: 1333 : if (rc != 0) {
335 : 0 : SPDK_ERRLOG("socket recv buf allocation failed\n");
336 : 0 : return -ENOMEM;
337 : : }
338 [ - + ]: 1333 : memset(new_buf, 0, sz);
339 : :
340 : 1333 : new_pipe = spdk_pipe_create(new_buf, sz);
341 [ - + ]: 1333 : if (new_pipe == NULL) {
342 : 0 : SPDK_ERRLOG("socket pipe allocation failed\n");
343 : 0 : free(new_buf);
344 : 0 : return -ENOMEM;
345 : : }
346 : :
347 [ - + ]: 1333 : if (sock->recv_pipe != NULL) {
348 : : /* Pull all of the data out of the old pipe */
349 : 0 : sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
350 [ # # ]: 0 : if (sbytes > sz) {
351 : : /* Too much data to fit into the new pipe size */
352 : 0 : spdk_pipe_destroy(new_pipe);
353 : 0 : free(new_buf);
354 : 0 : return -EINVAL;
355 : : }
356 : :
357 : 0 : sbytes = spdk_pipe_writer_get_buffer(new_pipe, sz, diov);
358 [ # # ]: 0 : assert(sbytes == sz);
359 : :
360 : 0 : bytes = spdk_iovcpy(siov, 2, diov, 2);
361 : 0 : spdk_pipe_writer_advance(new_pipe, bytes);
362 : :
363 : 0 : spdk_pipe_destroy(sock->recv_pipe);
364 : 0 : free(sock->recv_buf);
365 : : }
366 : :
367 : 1333 : sock->recv_buf_sz = sz;
368 : 1333 : sock->recv_buf = new_buf;
369 : 1333 : sock->recv_pipe = new_pipe;
370 : :
371 : 1333 : return 0;
372 : : }
373 : :
374 : : static int
375 : 3372 : uring_sock_set_recvbuf(struct spdk_sock *_sock, int sz)
376 : : {
377 : 3372 : struct spdk_uring_sock *sock = __uring_sock(_sock);
378 : : int min_size;
379 : : int rc;
380 : :
381 [ - + ]: 3372 : assert(sock != NULL);
382 : :
383 [ - + + - ]: 3372 : if (_sock->impl_opts.enable_recv_pipe) {
384 : 3372 : rc = uring_sock_alloc_pipe(sock, sz);
385 [ - + ]: 3372 : if (rc) {
386 : 0 : SPDK_ERRLOG("unable to allocate sufficient recvbuf with sz=%d on sock=%p\n", sz, _sock);
387 : 0 : return rc;
388 : : }
389 : : }
390 : :
391 : : /* Set kernel buffer size to be at least MIN_SO_RCVBUF_SIZE and
392 : : * g_spdk_uring_sock_impl_opts.recv_buf_size. */
393 : 3372 : min_size = spdk_max(MIN_SO_RCVBUF_SIZE, g_spdk_uring_sock_impl_opts.recv_buf_size);
394 : :
395 [ + - ]: 3372 : if (sz < min_size) {
396 : 3372 : sz = min_size;
397 : : }
398 : :
399 : 3372 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz));
400 [ - + ]: 3372 : if (rc < 0) {
401 : 0 : return rc;
402 : : }
403 : :
404 : 3372 : _sock->impl_opts.recv_buf_size = sz;
405 : :
406 : 3372 : return 0;
407 : : }
408 : :
409 : : static int
410 : 0 : uring_sock_set_sendbuf(struct spdk_sock *_sock, int sz)
411 : : {
412 : 0 : struct spdk_uring_sock *sock = __uring_sock(_sock);
413 : : int min_size;
414 : : int rc;
415 : :
416 [ # # ]: 0 : assert(sock != NULL);
417 : :
418 : : /* Set kernel buffer size to be at least MIN_SO_SNDBUF_SIZE and
419 : : * g_spdk_uring_sock_impl_opts.seend_buf_size. */
420 : 0 : min_size = spdk_max(MIN_SO_SNDBUF_SIZE, g_spdk_uring_sock_impl_opts.send_buf_size);
421 : :
422 [ # # ]: 0 : if (sz < min_size) {
423 : 0 : sz = min_size;
424 : : }
425 : :
426 : 0 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz));
427 [ # # ]: 0 : if (rc < 0) {
428 : 0 : return rc;
429 : : }
430 : :
431 : 0 : _sock->impl_opts.send_buf_size = sz;
432 : :
433 : 0 : return 0;
434 : : }
435 : :
436 : : static struct spdk_uring_sock *
437 : 1439 : uring_sock_alloc(int fd, struct spdk_sock_impl_opts *impl_opts, bool enable_zero_copy)
438 : : {
439 : : struct spdk_uring_sock *sock;
440 : : #if defined(__linux__)
441 : 0 : int flag;
442 : : int rc;
443 : : #endif
444 : :
445 : 1439 : sock = calloc(1, sizeof(*sock));
446 [ - + ]: 1439 : if (sock == NULL) {
447 : 0 : SPDK_ERRLOG("sock allocation failed\n");
448 : 0 : return NULL;
449 : : }
450 : :
451 : 1439 : sock->fd = fd;
452 [ - + - + ]: 1439 : memcpy(&sock->base.impl_opts, impl_opts, sizeof(*impl_opts));
453 : :
454 : 1439 : STAILQ_INIT(&sock->recv_stream);
455 : :
456 : : #if defined(__linux__)
457 : 1439 : flag = 1;
458 : :
459 [ - + - + ]: 1439 : if (sock->base.impl_opts.enable_quickack) {
460 : 0 : rc = setsockopt(sock->fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(flag));
461 [ # # ]: 0 : if (rc != 0) {
462 : 0 : SPDK_ERRLOG("quickack was failed to set\n");
463 : : }
464 : : }
465 : :
466 : 1439 : spdk_sock_get_placement_id(sock->fd, sock->base.impl_opts.enable_placement_id,
467 : : &sock->placement_id);
468 : : #ifdef SPDK_ZEROCOPY
469 : : /* Try to turn on zero copy sends */
470 : 1439 : flag = 1;
471 : :
472 [ - + ]: 1439 : if (enable_zero_copy) {
473 : 0 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_ZEROCOPY, &flag, sizeof(flag));
474 [ # # ]: 0 : if (rc == 0) {
475 : 0 : sock->zcopy = true;
476 : 0 : sock->zcopy_send_flags = MSG_ZEROCOPY;
477 : : }
478 : : }
479 : : #endif
480 : : #endif
481 : :
482 : 1439 : return sock;
483 : : }
484 : :
485 : : static struct spdk_sock *
486 : 511 : uring_sock_create(const char *ip, int port,
487 : : enum uring_sock_create_type type,
488 : : struct spdk_sock_opts *opts)
489 : : {
490 : : struct spdk_uring_sock *sock;
491 : 0 : struct spdk_sock_impl_opts impl_opts;
492 : 0 : char buf[MAX_TMPBUF];
493 : 0 : char portnum[PORTNUMLEN];
494 : : char *p;
495 : 0 : struct addrinfo hints, *res, *res0;
496 : : int fd, flag;
497 : 511 : int val = 1;
498 : : int rc;
499 : 511 : bool enable_zcopy_impl_opts = false;
500 : 511 : bool enable_zcopy_user_opts = true;
501 : :
502 [ - + ]: 511 : assert(opts != NULL);
503 : 511 : uring_opts_get_impl_opts(opts, &impl_opts);
504 : :
505 [ - + ]: 511 : if (ip == NULL) {
506 : 0 : return NULL;
507 : : }
508 [ - + ]: 511 : if (ip[0] == '[') {
509 : 0 : snprintf(buf, sizeof(buf), "%s", ip + 1);
510 : 0 : p = strchr(buf, ']');
511 [ # # ]: 0 : if (p != NULL) {
512 : 0 : *p = '\0';
513 : : }
514 : 0 : ip = (const char *) &buf[0];
515 : : }
516 : :
517 : 511 : snprintf(portnum, sizeof portnum, "%d", port);
518 : 511 : memset(&hints, 0, sizeof hints);
519 : 511 : hints.ai_family = PF_UNSPEC;
520 : 511 : hints.ai_socktype = SOCK_STREAM;
521 : 511 : hints.ai_flags = AI_NUMERICSERV;
522 : 511 : hints.ai_flags |= AI_PASSIVE;
523 : 511 : hints.ai_flags |= AI_NUMERICHOST;
524 : 511 : rc = getaddrinfo(ip, portnum, &hints, &res0);
525 [ - + ]: 511 : if (rc != 0) {
526 : 0 : SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n", gai_strerror(rc), rc);
527 : 0 : return NULL;
528 : : }
529 : :
530 : : /* try listen */
531 : 511 : fd = -1;
532 [ + + ]: 527 : for (res = res0; res != NULL; res = res->ai_next) {
533 : 511 : retry:
534 : 511 : fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
535 [ - + ]: 511 : if (fd < 0) {
536 : : /* error */
537 : 0 : continue;
538 : : }
539 : :
540 : 511 : val = impl_opts.recv_buf_size;
541 : 511 : rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof val);
542 : : if (rc) {
543 : : /* Not fatal */
544 : : }
545 : :
546 : 511 : val = impl_opts.send_buf_size;
547 : 511 : rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof val);
548 : : if (rc) {
549 : : /* Not fatal */
550 : : }
551 : :
552 : 511 : rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val);
553 [ - + ]: 511 : if (rc != 0) {
554 : 0 : close(fd);
555 : 0 : fd = -1;
556 : : /* error */
557 : 0 : continue;
558 : : }
559 : 511 : rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val);
560 [ - + ]: 511 : if (rc != 0) {
561 : 0 : close(fd);
562 : 0 : fd = -1;
563 : : /* error */
564 : 0 : continue;
565 : : }
566 : :
567 [ + + ]: 511 : if (opts->ack_timeout) {
568 : : #if defined(__linux__)
569 : 11 : val = opts->ack_timeout;
570 : 11 : rc = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &val, sizeof val);
571 [ - + ]: 11 : if (rc != 0) {
572 : 0 : close(fd);
573 : 0 : fd = -1;
574 : : /* error */
575 : 0 : continue;
576 : : }
577 : : #else
578 : : SPDK_WARNLOG("TCP_USER_TIMEOUT is not supported.\n");
579 : : #endif
580 : : }
581 : :
582 : :
583 : :
584 : : #if defined(SO_PRIORITY)
585 [ + - - + ]: 511 : if (opts != NULL && opts->priority) {
586 : 0 : rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &opts->priority, sizeof val);
587 [ # # ]: 0 : if (rc != 0) {
588 : 0 : close(fd);
589 : 0 : fd = -1;
590 : : /* error */
591 : 0 : continue;
592 : : }
593 : : }
594 : : #endif
595 [ - + ]: 511 : if (res->ai_family == AF_INET6) {
596 : 0 : rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof val);
597 [ # # ]: 0 : if (rc != 0) {
598 : 0 : close(fd);
599 : 0 : fd = -1;
600 : : /* error */
601 : 0 : continue;
602 : : }
603 : : }
604 : :
605 [ + + ]: 511 : if (type == SPDK_SOCK_CREATE_LISTEN) {
606 : 76 : rc = bind(fd, res->ai_addr, res->ai_addrlen);
607 [ - + ]: 76 : if (rc != 0) {
608 : 0 : SPDK_ERRLOG("bind() failed at port %d, errno = %d\n", port, errno);
609 [ # # # ]: 0 : switch (errno) {
610 : 0 : case EINTR:
611 : : /* interrupted? */
612 : 0 : close(fd);
613 : 0 : goto retry;
614 : 0 : case EADDRNOTAVAIL:
615 : 0 : SPDK_ERRLOG("IP address %s not available. "
616 : : "Verify IP address in config file "
617 : : "and make sure setup script is "
618 : : "run before starting spdk app.\n", ip);
619 : : /* FALLTHROUGH */
620 : 0 : default:
621 : : /* try next family */
622 : 0 : close(fd);
623 : 0 : fd = -1;
624 : 0 : continue;
625 : : }
626 : : }
627 : : /* bind OK */
628 : 76 : rc = listen(fd, 512);
629 [ - + ]: 76 : if (rc != 0) {
630 : 0 : SPDK_ERRLOG("listen() failed, errno = %d\n", errno);
631 : 0 : close(fd);
632 : 0 : fd = -1;
633 : 0 : break;
634 : : }
635 : :
636 : 76 : flag = fcntl(fd, F_GETFL);
637 [ - + ]: 76 : if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
638 : 0 : SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
639 : 0 : close(fd);
640 : 0 : fd = -1;
641 : 0 : break;
642 : : }
643 : :
644 [ - + ]: 76 : enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_server;
645 [ + - ]: 435 : } else if (type == SPDK_SOCK_CREATE_CONNECT) {
646 : 435 : rc = connect(fd, res->ai_addr, res->ai_addrlen);
647 [ + + ]: 435 : if (rc != 0) {
648 : 16 : SPDK_ERRLOG("connect() failed, errno = %d\n", errno);
649 : : /* try next family */
650 : 16 : close(fd);
651 : 16 : fd = -1;
652 : 16 : continue;
653 : : }
654 : :
655 : 419 : flag = fcntl(fd, F_GETFL);
656 [ - + ]: 419 : if (fcntl(fd, F_SETFL, flag & ~O_NONBLOCK) < 0) {
657 : 0 : SPDK_ERRLOG("fcntl can't set blocking mode for socket, fd: %d (%d)\n", fd, errno);
658 : 0 : close(fd);
659 : 0 : fd = -1;
660 : 0 : break;
661 : : }
662 : :
663 [ - + ]: 419 : enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_client;
664 : : }
665 : 495 : break;
666 : : }
667 : 511 : freeaddrinfo(res0);
668 : :
669 [ + + ]: 511 : if (fd < 0) {
670 : 16 : return NULL;
671 : : }
672 : :
673 [ - + + + : 495 : enable_zcopy_user_opts = opts->zcopy && !sock_is_loopback(fd);
+ + ]
674 [ + + - + ]: 495 : sock = uring_sock_alloc(fd, &impl_opts, enable_zcopy_user_opts && enable_zcopy_impl_opts);
675 [ - + ]: 495 : if (sock == NULL) {
676 : 0 : SPDK_ERRLOG("sock allocation failed\n");
677 : 0 : close(fd);
678 : 0 : return NULL;
679 : : }
680 : :
681 : 495 : return &sock->base;
682 : : }
683 : :
684 : : static struct spdk_sock *
685 : 76 : uring_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
686 : : {
687 : 76 : return uring_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts);
688 : : }
689 : :
690 : : static struct spdk_sock *
691 : 435 : uring_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
692 : : {
693 : 435 : return uring_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts);
694 : : }
695 : :
696 : : static struct spdk_sock *
697 : 1089010 : uring_sock_accept(struct spdk_sock *_sock)
698 : : {
699 : 1089010 : struct spdk_uring_sock *sock = __uring_sock(_sock);
700 : 0 : struct sockaddr_storage sa;
701 : 0 : socklen_t salen;
702 : : int rc, fd;
703 : : struct spdk_uring_sock *new_sock;
704 : : int flag;
705 : :
706 : 1089010 : memset(&sa, 0, sizeof(sa));
707 : 1089010 : salen = sizeof(sa);
708 : :
709 [ - + ]: 1089010 : assert(sock != NULL);
710 : :
711 : 1089010 : rc = accept(sock->fd, (struct sockaddr *)&sa, &salen);
712 : :
713 [ + + ]: 1089010 : if (rc == -1) {
714 : 1088066 : return NULL;
715 : : }
716 : :
717 : 944 : fd = rc;
718 : :
719 : 944 : flag = fcntl(fd, F_GETFL);
720 [ - + - - ]: 944 : if ((flag & O_NONBLOCK) && (fcntl(fd, F_SETFL, flag & ~O_NONBLOCK) < 0)) {
721 : 0 : SPDK_ERRLOG("fcntl can't set blocking mode for socket, fd: %d (%d)\n", fd, errno);
722 : 0 : close(fd);
723 : 0 : return NULL;
724 : : }
725 : :
726 : : #if defined(SO_PRIORITY)
727 : : /* The priority is not inherited, so call this function again */
728 [ - + ]: 944 : if (sock->base.opts.priority) {
729 : 0 : rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &sock->base.opts.priority, sizeof(int));
730 [ # # ]: 0 : if (rc != 0) {
731 : 0 : close(fd);
732 : 0 : return NULL;
733 : : }
734 : : }
735 : : #endif
736 : :
737 [ - + ]: 944 : new_sock = uring_sock_alloc(fd, &sock->base.impl_opts, sock->zcopy);
738 [ - + ]: 944 : if (new_sock == NULL) {
739 : 0 : close(fd);
740 : 0 : return NULL;
741 : : }
742 : :
743 : 944 : return &new_sock->base;
744 : : }
745 : :
746 : : static int
747 : 1439 : uring_sock_close(struct spdk_sock *_sock)
748 : : {
749 : 1439 : struct spdk_uring_sock *sock = __uring_sock(_sock);
750 : :
751 [ - + ]: 1439 : assert(TAILQ_EMPTY(&_sock->pending_reqs));
752 [ - + ]: 1439 : assert(sock->group == NULL);
753 : :
754 : : /* If the socket fails to close, the best choice is to
755 : : * leak the fd but continue to free the rest of the sock
756 : : * memory. */
757 : 1439 : close(sock->fd);
758 : :
759 : 1439 : spdk_pipe_destroy(sock->recv_pipe);
760 : 1439 : free(sock->recv_buf);
761 : 1439 : free(sock);
762 : :
763 : 1439 : return 0;
764 : : }
765 : :
766 : : static ssize_t
767 : 36225649 : uring_sock_recv_from_pipe(struct spdk_uring_sock *sock, struct iovec *diov, int diovcnt)
768 : : {
769 : 0 : struct iovec siov[2];
770 : : int sbytes;
771 : : ssize_t bytes;
772 : : struct spdk_uring_sock_group_impl *group;
773 : :
774 : 36225649 : sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
775 [ - + ]: 36225649 : if (sbytes < 0) {
776 : 0 : errno = EINVAL;
777 : 0 : return -1;
778 [ - + ]: 36225649 : } else if (sbytes == 0) {
779 : 0 : errno = EAGAIN;
780 : 0 : return -1;
781 : : }
782 : :
783 : 36225649 : bytes = spdk_iovcpy(siov, 2, diov, diovcnt);
784 : :
785 [ - + ]: 36225649 : if (bytes == 0) {
786 : : /* The only way this happens is if diov is 0 length */
787 : 0 : errno = EINVAL;
788 : 0 : return -1;
789 : : }
790 : :
791 : 36225649 : spdk_pipe_reader_advance(sock->recv_pipe, bytes);
792 : :
793 : : /* If we drained the pipe, take it off the level-triggered list */
794 [ + + + + ]: 36225649 : if (sock->base.group_impl && spdk_pipe_reader_bytes_available(sock->recv_pipe) == 0) {
795 : 9158978 : group = __uring_group_impl(sock->base.group_impl);
796 [ + + ]: 9158978 : TAILQ_REMOVE(&group->pending_recv, sock, link);
797 : 9158978 : sock->pending_recv = false;
798 : : }
799 : :
800 : 36225649 : return bytes;
801 : : }
802 : :
803 : : static inline ssize_t
804 : 195529455 : sock_readv(int fd, struct iovec *iov, int iovcnt)
805 : : {
806 : 195529455 : struct msghdr msg = {
807 : : .msg_iov = iov,
808 : : .msg_iovlen = iovcnt,
809 : : };
810 : :
811 : 195529455 : return recvmsg(fd, &msg, MSG_DONTWAIT);
812 : : }
813 : :
814 : : static inline ssize_t
815 : 187133536 : uring_sock_read(struct spdk_uring_sock *sock)
816 : : {
817 : 0 : struct iovec iov[2];
818 : : int bytes;
819 : : struct spdk_uring_sock_group_impl *group;
820 : :
821 : 187133536 : bytes = spdk_pipe_writer_get_buffer(sock->recv_pipe, sock->recv_buf_sz, iov);
822 : :
823 [ + - ]: 187133536 : if (bytes > 0) {
824 : 187133536 : bytes = sock_readv(sock->fd, iov, 2);
825 [ + + ]: 187133536 : if (bytes > 0) {
826 : 9163707 : spdk_pipe_writer_advance(sock->recv_pipe, bytes);
827 [ + + - + : 9163707 : if (sock->base.group_impl && !sock->pending_recv) {
+ + ]
828 : 2985783 : group = __uring_group_impl(sock->base.group_impl);
829 : 2985783 : TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
830 : 2985783 : sock->pending_recv = true;
831 : : }
832 : : }
833 : : }
834 : :
835 : 187133536 : return bytes;
836 : : }
837 : :
838 : : static int
839 : 0 : uring_sock_recv_next(struct spdk_sock *_sock, void **_buf, void **ctx)
840 : : {
841 : 0 : struct spdk_uring_sock *sock = __uring_sock(_sock);
842 : : struct spdk_uring_sock_group_impl *group;
843 : : struct spdk_uring_buf_tracker *tr;
844 : :
845 [ # # ]: 0 : if (sock->connection_status < 0) {
846 : 0 : errno = -sock->connection_status;
847 : 0 : return -1;
848 : : }
849 : :
850 [ # # ]: 0 : if (sock->recv_pipe != NULL) {
851 : 0 : errno = ENOTSUP;
852 : 0 : return -1;
853 : : }
854 : :
855 : 0 : group = __uring_group_impl(_sock->group_impl);
856 : :
857 : 0 : tr = STAILQ_FIRST(&sock->recv_stream);
858 [ # # ]: 0 : if (tr == NULL) {
859 [ # # ]: 0 : if (sock->group->buf_ring_count > 0) {
860 : : /* There are buffers posted, but data hasn't arrived. */
861 : 0 : errno = EAGAIN;
862 : : } else {
863 : : /* There are no buffers posted, so this won't ever
864 : : * make forward progress. */
865 : 0 : errno = ENOBUFS;
866 : : }
867 : 0 : return -1;
868 : : }
869 [ # # # # ]: 0 : assert(sock->pending_recv == true);
870 [ # # ]: 0 : assert(tr->buf != NULL);
871 : :
872 : 0 : *_buf = tr->buf + sock->recv_offset;
873 : 0 : *ctx = tr->ctx;
874 : :
875 [ # # ]: 0 : STAILQ_REMOVE_HEAD(&sock->recv_stream, link);
876 [ # # ]: 0 : STAILQ_INSERT_HEAD(&group->free_trackers, tr, link);
877 : :
878 [ # # ]: 0 : if (STAILQ_EMPTY(&sock->recv_stream)) {
879 : 0 : sock->pending_recv = false;
880 [ # # ]: 0 : TAILQ_REMOVE(&group->pending_recv, sock, link);
881 : : }
882 : :
883 : 0 : return tr->len - sock->recv_offset;
884 : : }
885 : :
886 : : static ssize_t
887 : 146542 : uring_sock_readv_no_pipe(struct spdk_sock *_sock, struct iovec *iovs, int iovcnt)
888 : : {
889 : 146542 : struct spdk_uring_sock *sock = __uring_sock(_sock);
890 : : struct spdk_uring_buf_tracker *tr;
891 : : struct iovec iov;
892 : : ssize_t total, len;
893 : : int i;
894 : :
895 [ - + ]: 146542 : if (sock->connection_status < 0) {
896 : 0 : errno = -sock->connection_status;
897 : 0 : return -1;
898 : : }
899 : :
900 [ + + ]: 146542 : if (_sock->group_impl == NULL) {
901 : : /* If not in a group just read from the socket the regular way. */
902 : 16488 : return sock_readv(sock->fd, iovs, iovcnt);
903 : : }
904 : :
905 [ + - ]: 130054 : if (STAILQ_EMPTY(&sock->recv_stream)) {
906 [ + - ]: 130054 : if (sock->group->buf_ring_count == 0) {
907 : : /* If the user hasn't posted any buffers, read from the socket
908 : : * directly. */
909 : :
910 [ - + + + ]: 130054 : if (sock->pending_recv) {
911 : 127603 : sock->pending_recv = false;
912 [ + + ]: 127603 : TAILQ_REMOVE(&(__uring_group_impl(_sock->group_impl))->pending_recv, sock, link);
913 : : }
914 : :
915 : 130054 : return sock_readv(sock->fd, iovs, iovcnt);
916 : : }
917 : :
918 : 0 : errno = EAGAIN;
919 : 0 : return -1;
920 : : }
921 : :
922 : 0 : total = 0;
923 [ # # ]: 0 : for (i = 0; i < iovcnt; i++) {
924 : : /* Copy to stack so we can change it */
925 : 0 : iov = iovs[i];
926 : :
927 : 0 : tr = STAILQ_FIRST(&sock->recv_stream);
928 [ # # ]: 0 : while (tr != NULL) {
929 : 0 : len = spdk_min(iov.iov_len, tr->len - sock->recv_offset);
930 [ # # # # ]: 0 : memcpy(iov.iov_base, tr->buf + sock->recv_offset, len);
931 : :
932 : 0 : total += len;
933 : 0 : sock->recv_offset += len;
934 : 0 : iov.iov_base += len;
935 : 0 : iov.iov_len -= len;
936 : :
937 [ # # ]: 0 : if (sock->recv_offset == tr->len) {
938 : 0 : sock->recv_offset = 0;
939 [ # # ]: 0 : STAILQ_REMOVE_HEAD(&sock->recv_stream, link);
940 [ # # ]: 0 : STAILQ_INSERT_HEAD(&sock->group->free_trackers, tr, link);
941 : 0 : spdk_sock_group_provide_buf(sock->group->base.group, tr->buf, tr->buflen, tr->ctx);
942 : 0 : tr = STAILQ_FIRST(&sock->recv_stream);
943 : : }
944 : :
945 [ # # ]: 0 : if (iov.iov_len == 0) {
946 : 0 : break;
947 : : }
948 : : }
949 : : }
950 : :
951 [ # # ]: 0 : if (STAILQ_EMPTY(&sock->recv_stream)) {
952 : : struct spdk_uring_sock_group_impl *group;
953 : :
954 : 0 : group = __uring_group_impl(_sock->group_impl);
955 : 0 : sock->pending_recv = false;
956 [ # # ]: 0 : TAILQ_REMOVE(&group->pending_recv, sock, link);
957 : : }
958 : :
959 [ # # ]: 0 : assert(total > 0);
960 : 0 : return total;
961 : : }
962 : :
963 : : static ssize_t
964 : 222591397 : uring_sock_readv(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
965 : : {
966 : 222591397 : struct spdk_uring_sock *sock = __uring_sock(_sock);
967 : : int rc, i;
968 : : size_t len;
969 : :
970 [ - + ]: 222591397 : if (sock->connection_status < 0) {
971 : 0 : errno = -sock->connection_status;
972 : 0 : return -1;
973 : : }
974 : :
975 [ + + ]: 222591397 : if (sock->recv_pipe == NULL) {
976 : 146542 : return uring_sock_readv_no_pipe(_sock, iov, iovcnt);
977 : : }
978 : :
979 : 222444855 : len = 0;
980 [ + + ]: 448107196 : for (i = 0; i < iovcnt; i++) {
981 : 225662341 : len += iov[i].iov_len;
982 : : }
983 : :
984 [ + + ]: 222444855 : if (spdk_pipe_reader_bytes_available(sock->recv_pipe) == 0) {
985 : : /* If the user is receiving a sufficiently large amount of data,
986 : : * receive directly to their buffers. */
987 [ + + ]: 195382913 : if (len >= MIN_SOCK_PIPE_SIZE) {
988 : 8249377 : return sock_readv(sock->fd, iov, iovcnt);
989 : : }
990 : :
991 : : /* Otherwise, do a big read into our pipe */
992 : 187133536 : rc = uring_sock_read(sock);
993 [ + + ]: 187133536 : if (rc <= 0) {
994 : 177969829 : return rc;
995 : : }
996 : : }
997 : :
998 : 36225649 : return uring_sock_recv_from_pipe(sock, iov, iovcnt);
999 : : }
1000 : :
1001 : : static ssize_t
1002 : 222002895 : uring_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
1003 : : {
1004 : 0 : struct iovec iov[1];
1005 : :
1006 : 222002895 : iov[0].iov_base = buf;
1007 : 222002895 : iov[0].iov_len = len;
1008 : :
1009 : 222002895 : return uring_sock_readv(sock, iov, 1);
1010 : : }
1011 : :
1012 : : static ssize_t
1013 : 0 : uring_sock_writev(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
1014 : : {
1015 : 0 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1016 : 0 : struct msghdr msg = {
1017 : : .msg_iov = iov,
1018 : : .msg_iovlen = iovcnt,
1019 : : };
1020 : :
1021 [ # # ]: 0 : if (sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
1022 : 0 : errno = EAGAIN;
1023 : 0 : return -1;
1024 : : }
1025 : :
1026 : 0 : return sendmsg(sock->fd, &msg, MSG_DONTWAIT);
1027 : : }
1028 : :
1029 : : static ssize_t
1030 : 21077492 : sock_request_advance_offset(struct spdk_sock_request *req, ssize_t rc)
1031 : : {
1032 : : unsigned int offset;
1033 : : size_t len;
1034 : : int i;
1035 : :
1036 : 21077492 : offset = req->internal.offset;
1037 [ + + ]: 59961446 : for (i = 0; i < req->iovcnt; i++) {
1038 : : /* Advance by the offset first */
1039 [ + + ]: 39018757 : if (offset >= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len) {
1040 : 606112 : offset -= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len;
1041 : 606112 : continue;
1042 : : }
1043 : :
1044 : : /* Calculate the remaining length of this element */
1045 : 38412645 : len = SPDK_SOCK_REQUEST_IOV(req, i)->iov_len - offset;
1046 : :
1047 [ + + ]: 38412645 : if (len > (size_t)rc) {
1048 : 134803 : req->internal.offset += rc;
1049 : 134803 : return -1;
1050 : : }
1051 : :
1052 : 38277842 : offset = 0;
1053 : 38277842 : req->internal.offset += len;
1054 : 38277842 : rc -= len;
1055 : : }
1056 : :
1057 : 20942689 : return rc;
1058 : : }
1059 : :
1060 : : static int
1061 : 6151779 : sock_complete_write_reqs(struct spdk_sock *_sock, ssize_t rc, bool is_zcopy)
1062 : : {
1063 : 6151779 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1064 : : struct spdk_sock_request *req;
1065 : : int retval;
1066 : :
1067 [ - + ]: 6151779 : if (is_zcopy) {
1068 : : /* Handling overflow case, because we use psock->sendmsg_idx - 1 for the
1069 : : * req->internal.offset, so sendmsg_idx should not be zero */
1070 [ # # ]: 0 : if (spdk_unlikely(sock->sendmsg_idx == UINT32_MAX)) {
1071 : 0 : sock->sendmsg_idx = 1;
1072 : : } else {
1073 : 0 : sock->sendmsg_idx++;
1074 : : }
1075 : : }
1076 : :
1077 : : /* Consume the requests that were actually written */
1078 : 6151779 : req = TAILQ_FIRST(&_sock->queued_reqs);
1079 [ + - ]: 21077492 : while (req) {
1080 : : /* req->internal.is_zcopy is true when the whole req or part of it is sent with zerocopy */
1081 : 21077492 : req->internal.is_zcopy = is_zcopy;
1082 : :
1083 : 21077492 : rc = sock_request_advance_offset(req, rc);
1084 [ + + ]: 21077492 : if (rc < 0) {
1085 : : /* This element was partially sent. */
1086 : 134803 : return 0;
1087 : : }
1088 : :
1089 : : /* Handled a full request. */
1090 : 20942689 : spdk_sock_request_pend(_sock, req);
1091 : :
1092 [ - + + - : 20942689 : if (!req->internal.is_zcopy && req == TAILQ_FIRST(&_sock->pending_reqs)) {
+ - ]
1093 : 20942689 : retval = spdk_sock_request_put(_sock, req, 0);
1094 [ - + ]: 20942689 : if (retval) {
1095 : 0 : return retval;
1096 : : }
1097 : : } else {
1098 : : /* Re-use the offset field to hold the sendmsg call index. The
1099 : : * index is 0 based, so subtract one here because we've already
1100 : : * incremented above. */
1101 : 0 : req->internal.offset = sock->sendmsg_idx - 1;
1102 : : }
1103 : :
1104 [ + + ]: 20942689 : if (rc == 0) {
1105 : 6016976 : break;
1106 : : }
1107 : :
1108 : 14925713 : req = TAILQ_FIRST(&_sock->queued_reqs);
1109 : : }
1110 : :
1111 : 6016976 : return 0;
1112 : : }
1113 : :
1114 : : #ifdef SPDK_ZEROCOPY
1115 : : static int
1116 : 0 : _sock_check_zcopy(struct spdk_sock *_sock, int status)
1117 : : {
1118 : 0 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1119 : : ssize_t rc;
1120 : : struct sock_extended_err *serr;
1121 : : struct cmsghdr *cm;
1122 : : uint32_t idx;
1123 : : struct spdk_sock_request *req, *treq;
1124 : : bool found;
1125 : :
1126 [ # # # # ]: 0 : assert(sock->zcopy == true);
1127 [ # # ]: 0 : if (spdk_unlikely(status) < 0) {
1128 [ # # ]: 0 : if (!TAILQ_EMPTY(&_sock->pending_reqs)) {
1129 : 0 : SPDK_ERRLOG("Attempting to receive from ERRQUEUE yielded error, but pending list still has orphaned entries, status =%d\n",
1130 : : status);
1131 : : } else {
1132 : 0 : SPDK_WARNLOG("Recvmsg yielded an error!\n");
1133 : : }
1134 : 0 : return 0;
1135 : : }
1136 : :
1137 [ # # ]: 0 : cm = CMSG_FIRSTHDR(&sock->errqueue_task.msg);
1138 [ # # # # ]: 0 : if (!((cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_RECVERR) ||
1139 [ # # # # ]: 0 : (cm->cmsg_level == SOL_IPV6 && cm->cmsg_type == IPV6_RECVERR))) {
1140 : 0 : SPDK_WARNLOG("Unexpected cmsg level or type!\n");
1141 : 0 : return 0;
1142 : : }
1143 : :
1144 : 0 : serr = (struct sock_extended_err *)CMSG_DATA(cm);
1145 [ # # # # ]: 0 : if (serr->ee_errno != 0 || serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
1146 : 0 : SPDK_WARNLOG("Unexpected extended error origin\n");
1147 : 0 : return 0;
1148 : : }
1149 : :
1150 : : /* Most of the time, the pending_reqs array is in the exact
1151 : : * order we need such that all of the requests to complete are
1152 : : * in order, in the front. It is guaranteed that all requests
1153 : : * belonging to the same sendmsg call are sequential, so once
1154 : : * we encounter one match we can stop looping as soon as a
1155 : : * non-match is found.
1156 : : */
1157 [ # # ]: 0 : for (idx = serr->ee_info; idx <= serr->ee_data; idx++) {
1158 : 0 : found = false;
1159 [ # # ]: 0 : TAILQ_FOREACH_SAFE(req, &_sock->pending_reqs, internal.link, treq) {
1160 [ # # # # ]: 0 : if (!req->internal.is_zcopy) {
1161 : : /* This wasn't a zcopy request. It was just waiting in line to complete */
1162 : 0 : rc = spdk_sock_request_put(_sock, req, 0);
1163 [ # # ]: 0 : if (rc < 0) {
1164 : 0 : return rc;
1165 : : }
1166 [ # # ]: 0 : } else if (req->internal.offset == idx) {
1167 : 0 : found = true;
1168 : 0 : rc = spdk_sock_request_put(_sock, req, 0);
1169 [ # # ]: 0 : if (rc < 0) {
1170 : 0 : return rc;
1171 : : }
1172 [ # # ]: 0 : } else if (found) {
1173 : 0 : break;
1174 : : }
1175 : : }
1176 : : }
1177 : :
1178 : 0 : return 0;
1179 : : }
1180 : :
1181 : : static void
1182 : 0 : _sock_prep_errqueue(struct spdk_sock *_sock)
1183 : : {
1184 : 0 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1185 : 0 : struct spdk_uring_task *task = &sock->errqueue_task;
1186 : : struct io_uring_sqe *sqe;
1187 : :
1188 [ # # ]: 0 : if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
1189 : 0 : return;
1190 : : }
1191 : :
1192 [ # # # # ]: 0 : if (sock->pending_group_remove) {
1193 : 0 : return;
1194 : : }
1195 : :
1196 [ # # ]: 0 : assert(sock->group != NULL);
1197 : 0 : sock->group->io_queued++;
1198 : :
1199 : 0 : sqe = io_uring_get_sqe(&sock->group->uring);
1200 : 0 : io_uring_prep_recvmsg(sqe, sock->fd, &task->msg, MSG_ERRQUEUE);
1201 : 0 : io_uring_sqe_set_data(sqe, task);
1202 : 0 : task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
1203 : : }
1204 : :
1205 : : #endif
1206 : :
1207 : : static void
1208 : 184992484 : _sock_flush(struct spdk_sock *_sock)
1209 : : {
1210 : 184992484 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1211 : 184992484 : struct spdk_uring_task *task = &sock->write_task;
1212 : : uint32_t iovcnt;
1213 : : struct io_uring_sqe *sqe;
1214 : 0 : int flags;
1215 : :
1216 [ - + ]: 184992484 : if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
1217 : 178291462 : return;
1218 : : }
1219 : :
1220 : : #ifdef SPDK_ZEROCOPY
1221 [ - + - + ]: 184992484 : if (sock->zcopy) {
1222 : 0 : flags = MSG_DONTWAIT | sock->zcopy_send_flags;
1223 : : } else
1224 : : #endif
1225 : : {
1226 : 184992484 : flags = MSG_DONTWAIT;
1227 : : }
1228 : :
1229 : 184992484 : iovcnt = spdk_sock_prep_reqs(&sock->base, task->iovs, task->iov_cnt, &task->last_req, &flags);
1230 [ + + ]: 184992484 : if (!iovcnt) {
1231 : 178291462 : return;
1232 : : }
1233 : :
1234 : 6701022 : task->iov_cnt = iovcnt;
1235 [ - + ]: 6701022 : assert(sock->group != NULL);
1236 : 6701022 : task->msg.msg_iov = task->iovs;
1237 : 6701022 : task->msg.msg_iovlen = task->iov_cnt;
1238 : : #ifdef SPDK_ZEROCOPY
1239 : 6701022 : task->is_zcopy = (flags & MSG_ZEROCOPY) ? true : false;
1240 : : #endif
1241 : 6701022 : sock->group->io_queued++;
1242 : :
1243 : 6701022 : sqe = io_uring_get_sqe(&sock->group->uring);
1244 : 6701022 : io_uring_prep_sendmsg(sqe, sock->fd, &sock->write_task.msg, flags);
1245 : 6701022 : io_uring_sqe_set_data(sqe, task);
1246 : 6701022 : task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
1247 : : }
1248 : :
1249 : : static void
1250 : 184998758 : _sock_prep_read(struct spdk_sock *_sock)
1251 : : {
1252 : 184998758 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1253 : 184998758 : struct spdk_uring_task *task = &sock->read_task;
1254 : : struct io_uring_sqe *sqe;
1255 : :
1256 : : /* Do not prepare read event */
1257 [ - + ]: 184998758 : if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
1258 : 0 : return;
1259 : : }
1260 : :
1261 [ - + + + ]: 184998758 : if (sock->pending_group_remove) {
1262 : 1373 : return;
1263 : : }
1264 : :
1265 [ - + ]: 184997385 : assert(sock->group != NULL);
1266 : 184997385 : sock->group->io_queued++;
1267 : :
1268 : 184997385 : sqe = io_uring_get_sqe(&sock->group->uring);
1269 : 184997385 : io_uring_prep_recv(sqe, sock->fd, NULL, URING_MAX_RECV_SIZE, 0);
1270 : 184997385 : sqe->buf_group = URING_BUF_GROUP_ID;
1271 : 184997385 : sqe->flags |= IOSQE_BUFFER_SELECT;
1272 : 184997385 : io_uring_sqe_set_data(sqe, task);
1273 : 184997385 : task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
1274 : : }
1275 : :
1276 : : static void
1277 : 1373 : _sock_prep_cancel_task(struct spdk_sock *_sock, void *user_data)
1278 : : {
1279 : 1373 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1280 : 1373 : struct spdk_uring_task *task = &sock->cancel_task;
1281 : : struct io_uring_sqe *sqe;
1282 : :
1283 [ - + ]: 1373 : if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
1284 : 0 : return;
1285 : : }
1286 : :
1287 [ - + ]: 1373 : assert(sock->group != NULL);
1288 : 1373 : sock->group->io_queued++;
1289 : :
1290 : 1373 : sqe = io_uring_get_sqe(&sock->group->uring);
1291 : 1373 : io_uring_prep_cancel(sqe, user_data, 0);
1292 : 1373 : io_uring_sqe_set_data(sqe, task);
1293 : 1373 : task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
1294 : : }
1295 : :
1296 : : static void
1297 : 1 : uring_sock_fail(struct spdk_uring_sock *sock, int status)
1298 : : {
1299 : 1 : struct spdk_uring_sock_group_impl *group = sock->group;
1300 : : int rc;
1301 : :
1302 : 1 : sock->connection_status = status;
1303 : 1 : rc = spdk_sock_abort_requests(&sock->base);
1304 : :
1305 : : /* The user needs to be notified that this socket is dead. */
1306 [ - + - - ]: 1 : if (rc == 0 && sock->base.cb_fn != NULL &&
1307 [ # # # # ]: 0 : sock->pending_recv == false) {
1308 : 0 : sock->pending_recv = true;
1309 : 0 : TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
1310 : : }
1311 : 1 : }
1312 : :
1313 : : static int
1314 : 121603814 : sock_uring_group_reap(struct spdk_uring_sock_group_impl *group, int max, int max_read_events,
1315 : : struct spdk_sock **socks)
1316 : : {
1317 : : int i, count, ret;
1318 : 0 : struct io_uring_cqe *cqe;
1319 : : struct spdk_uring_sock *sock, *tmp;
1320 : : struct spdk_uring_task *task;
1321 : : int status, bid, flags;
1322 : : bool is_zcopy;
1323 : :
1324 [ + + ]: 313303594 : for (i = 0; i < max; i++) {
1325 : 191699780 : ret = io_uring_peek_cqe(&group->uring, &cqe);
1326 [ - + ]: 191699780 : if (ret != 0) {
1327 : 0 : break;
1328 : : }
1329 : :
1330 [ - + ]: 191699780 : if (cqe == NULL) {
1331 : 0 : break;
1332 : : }
1333 : :
1334 : 191699780 : task = (struct spdk_uring_task *)cqe->user_data;
1335 [ - + ]: 191699780 : assert(task != NULL);
1336 : 191699780 : sock = task->sock;
1337 [ - + ]: 191699780 : assert(sock != NULL);
1338 [ - + ]: 191699780 : assert(sock->group != NULL);
1339 [ - + ]: 191699780 : assert(sock->group == group);
1340 : 191699780 : sock->group->io_inflight--;
1341 : 191699780 : sock->group->io_avail++;
1342 : 191699780 : status = cqe->res;
1343 : 191699780 : flags = cqe->flags;
1344 : 191699780 : io_uring_cqe_seen(&group->uring, cqe);
1345 : :
1346 : 191699780 : task->status = SPDK_URING_SOCK_TASK_NOT_IN_USE;
1347 : :
1348 [ + + - + : 191699780 : switch (task->type) {
- ]
1349 : 184997385 : case URING_TASK_READ:
1350 [ + - - + ]: 184997385 : if (status == -EAGAIN || status == -EWOULDBLOCK) {
1351 : : /* This likely shouldn't happen, but would indicate that the
1352 : : * kernel didn't have enough resources to queue a task internally. */
1353 : 0 : _sock_prep_read(&sock->base);
1354 [ - + ]: 184997385 : } else if (status == -ECANCELED) {
1355 : 0 : continue;
1356 [ + - ]: 184997385 : } else if (status == -ENOBUFS) {
1357 : : /* There's data in the socket but the user hasn't provided any buffers.
1358 : : * We need to notify the user that the socket has data pending. */
1359 [ + - ]: 184997385 : if (sock->base.cb_fn != NULL &&
1360 [ - + + + ]: 184997385 : sock->pending_recv == false) {
1361 : 6302171 : sock->pending_recv = true;
1362 : 6302171 : TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
1363 : : }
1364 : :
1365 : 184997385 : _sock_prep_read(&sock->base);
1366 [ # # ]: 0 : } else if (spdk_unlikely(status <= 0)) {
1367 [ # # ]: 0 : uring_sock_fail(sock, status < 0 ? status : -ECONNRESET);
1368 : : } else {
1369 : : struct spdk_uring_buf_tracker *tracker;
1370 : :
1371 [ # # ]: 0 : assert((flags & IORING_CQE_F_BUFFER) != 0);
1372 : :
1373 : 0 : bid = flags >> IORING_CQE_BUFFER_SHIFT;
1374 : 0 : tracker = &group->trackers[bid];
1375 : :
1376 [ # # ]: 0 : assert(tracker->buf != NULL);
1377 [ # # ]: 0 : assert(tracker->len != 0);
1378 : :
1379 : : /* Append this data to the stream */
1380 : 0 : tracker->len = status;
1381 : 0 : STAILQ_INSERT_TAIL(&sock->recv_stream, tracker, link);
1382 [ # # ]: 0 : assert(group->buf_ring_count > 0);
1383 : 0 : group->buf_ring_count--;
1384 : :
1385 [ # # ]: 0 : if (sock->base.cb_fn != NULL &&
1386 [ # # # # ]: 0 : sock->pending_recv == false) {
1387 : 0 : sock->pending_recv = true;
1388 : 0 : TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
1389 : : }
1390 : :
1391 : 0 : _sock_prep_read(&sock->base);
1392 : : }
1393 : 184997385 : break;
1394 : 6701022 : case URING_TASK_WRITE:
1395 [ + + + - : 6701022 : if (status == -EAGAIN || status == -EWOULDBLOCK ||
- + ]
1396 [ - - - - : 6146189 : (status == -ENOBUFS && sock->zcopy) ||
- + ]
1397 : : status == -ECANCELED) {
1398 : 554833 : continue;
1399 [ + + ]: 6146189 : } else if (spdk_unlikely(status) < 0) {
1400 : 1 : uring_sock_fail(sock, status);
1401 : : } else {
1402 : 6146188 : task->last_req = NULL;
1403 : 6146188 : task->iov_cnt = 0;
1404 [ - + ]: 6146188 : is_zcopy = task->is_zcopy;
1405 : 6146188 : task->is_zcopy = false;
1406 : 6146188 : sock_complete_write_reqs(&sock->base, status, is_zcopy);
1407 : : }
1408 : :
1409 : 6146189 : break;
1410 : : #ifdef SPDK_ZEROCOPY
1411 : 0 : case URING_TASK_ERRQUEUE:
1412 [ # # # # ]: 0 : if (status == -EAGAIN || status == -EWOULDBLOCK) {
1413 : 0 : _sock_prep_errqueue(&sock->base);
1414 [ # # ]: 0 : } else if (status == -ECANCELED) {
1415 : 0 : continue;
1416 [ # # ]: 0 : } else if (spdk_unlikely(status < 0)) {
1417 : 0 : uring_sock_fail(sock, status);
1418 : : } else {
1419 : 0 : _sock_check_zcopy(&sock->base, status);
1420 : 0 : _sock_prep_errqueue(&sock->base);
1421 : : }
1422 : 0 : break;
1423 : : #endif
1424 : 1373 : case URING_TASK_CANCEL:
1425 : : /* Do nothing */
1426 : 1373 : break;
1427 : 0 : default:
1428 : 0 : SPDK_UNREACHABLE();
1429 : : }
1430 : : }
1431 : :
1432 [ + + ]: 121603814 : if (!socks) {
1433 : 1373 : return 0;
1434 : : }
1435 : 121602441 : count = 0;
1436 [ + + ]: 306594690 : TAILQ_FOREACH_SAFE(sock, &group->pending_recv, link, tmp) {
1437 [ - + ]: 184992249 : if (count == max_read_events) {
1438 : 0 : break;
1439 : : }
1440 : :
1441 : : /* If the socket's cb_fn is NULL, do not add it to socks array */
1442 [ - + ]: 184992249 : if (spdk_unlikely(sock->base.cb_fn == NULL)) {
1443 [ # # # # ]: 0 : assert(sock->pending_recv == true);
1444 : 0 : sock->pending_recv = false;
1445 [ # # ]: 0 : TAILQ_REMOVE(&group->pending_recv, sock, link);
1446 : 0 : continue;
1447 : : }
1448 : :
1449 : 184992249 : socks[count++] = &sock->base;
1450 : : }
1451 : :
1452 : :
1453 : : /* Cycle the pending_recv list so that each time we poll things aren't
1454 : : * in the same order. Say we have 6 sockets in the list, named as follows:
1455 : : * A B C D E F
1456 : : * And all 6 sockets had the poll events, but max_events is only 3. That means
1457 : : * psock currently points at D. We want to rearrange the list to the following:
1458 : : * D E F A B C
1459 : : *
1460 : : * The variables below are named according to this example to make it easier to
1461 : : * follow the swaps.
1462 : : */
1463 [ + - ]: 121602441 : if (sock != NULL) {
1464 : : struct spdk_uring_sock *ua, *uc, *ud, *uf;
1465 : :
1466 : : /* Capture pointers to the elements we need */
1467 : 0 : ud = sock;
1468 : :
1469 : 0 : ua = TAILQ_FIRST(&group->pending_recv);
1470 [ # # ]: 0 : if (ua == ud) {
1471 : 0 : goto end;
1472 : : }
1473 : :
1474 : 0 : uf = TAILQ_LAST(&group->pending_recv, pending_recv_list);
1475 [ # # ]: 0 : if (uf == ud) {
1476 [ # # ]: 0 : TAILQ_REMOVE(&group->pending_recv, ud, link);
1477 [ # # ]: 0 : TAILQ_INSERT_HEAD(&group->pending_recv, ud, link);
1478 : 0 : goto end;
1479 : : }
1480 : :
1481 : 0 : uc = TAILQ_PREV(ud, pending_recv_list, link);
1482 [ # # ]: 0 : assert(uc != NULL);
1483 : :
1484 : : /* Break the link between C and D */
1485 : 0 : uc->link.tqe_next = NULL;
1486 : :
1487 : : /* Connect F to A */
1488 : 0 : uf->link.tqe_next = ua;
1489 : 0 : ua->link.tqe_prev = &uf->link.tqe_next;
1490 : :
1491 : : /* Fix up the list first/last pointers */
1492 : 0 : group->pending_recv.tqh_first = ud;
1493 : 0 : group->pending_recv.tqh_last = &uc->link.tqe_next;
1494 : :
1495 : : /* D is in front of the list, make tqe prev pointer point to the head of list */
1496 : 0 : ud->link.tqe_prev = &group->pending_recv.tqh_first;
1497 : : }
1498 : :
1499 : 121602441 : end:
1500 : 121602441 : return count;
1501 : : }
1502 : :
1503 : : static int uring_sock_flush(struct spdk_sock *_sock);
1504 : :
1505 : : static void
1506 : 20942983 : uring_sock_writev_async(struct spdk_sock *_sock, struct spdk_sock_request *req)
1507 : : {
1508 : 20942983 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1509 : : int rc;
1510 : :
1511 [ - + ]: 20942983 : if (spdk_unlikely(sock->connection_status)) {
1512 : 0 : req->cb_fn(req->cb_arg, sock->connection_status);
1513 : 0 : return;
1514 : : }
1515 : :
1516 : 20942983 : spdk_sock_request_queue(_sock, req);
1517 : :
1518 [ + + ]: 20942983 : if (!sock->group) {
1519 [ - + ]: 5861 : if (_sock->queued_iovcnt >= IOV_BATCH_SIZE) {
1520 : 0 : rc = uring_sock_flush(_sock);
1521 [ # # # # ]: 0 : if (rc < 0 && errno != EAGAIN) {
1522 : 0 : spdk_sock_abort_requests(_sock);
1523 : : }
1524 : : }
1525 : : }
1526 : : }
1527 : :
1528 : : static int
1529 : 944 : uring_sock_set_recvlowat(struct spdk_sock *_sock, int nbytes)
1530 : : {
1531 : 944 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1532 : 0 : int val;
1533 : : int rc;
1534 : :
1535 [ - + ]: 944 : assert(sock != NULL);
1536 : :
1537 : 944 : val = nbytes;
1538 : 944 : rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVLOWAT, &val, sizeof val);
1539 [ - + ]: 944 : if (rc != 0) {
1540 : 0 : return -1;
1541 : : }
1542 : 944 : return 0;
1543 : : }
1544 : :
1545 : : static bool
1546 : 0 : uring_sock_is_ipv6(struct spdk_sock *_sock)
1547 : : {
1548 : 0 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1549 : 0 : struct sockaddr_storage sa;
1550 : 0 : socklen_t salen;
1551 : : int rc;
1552 : :
1553 [ # # ]: 0 : assert(sock != NULL);
1554 : :
1555 : 0 : memset(&sa, 0, sizeof sa);
1556 : 0 : salen = sizeof sa;
1557 : 0 : rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
1558 [ # # ]: 0 : if (rc != 0) {
1559 : 0 : SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
1560 : 0 : return false;
1561 : : }
1562 : :
1563 : 0 : return (sa.ss_family == AF_INET6);
1564 : : }
1565 : :
1566 : : static bool
1567 : 1206 : uring_sock_is_ipv4(struct spdk_sock *_sock)
1568 : : {
1569 : 1206 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1570 : 0 : struct sockaddr_storage sa;
1571 : 0 : socklen_t salen;
1572 : : int rc;
1573 : :
1574 [ - + ]: 1206 : assert(sock != NULL);
1575 : :
1576 : 1206 : memset(&sa, 0, sizeof sa);
1577 : 1206 : salen = sizeof sa;
1578 : 1206 : rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
1579 [ - + ]: 1206 : if (rc != 0) {
1580 : 0 : SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
1581 : 0 : return false;
1582 : : }
1583 : :
1584 : 1206 : return (sa.ss_family == AF_INET);
1585 : : }
1586 : :
1587 : : static bool
1588 : 78322 : uring_sock_is_connected(struct spdk_sock *_sock)
1589 : : {
1590 : 78322 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1591 : 0 : uint8_t byte;
1592 : : int rc;
1593 : :
1594 : 78322 : rc = recv(sock->fd, &byte, 1, MSG_PEEK | MSG_DONTWAIT);
1595 [ + - ]: 78322 : if (rc == 0) {
1596 : 78322 : return false;
1597 : : }
1598 : :
1599 [ # # ]: 0 : if (rc < 0) {
1600 [ # # # # ]: 0 : if (errno == EAGAIN || errno == EWOULDBLOCK) {
1601 : 0 : return true;
1602 : : }
1603 : :
1604 : 0 : return false;
1605 : : }
1606 : :
1607 : 0 : return true;
1608 : : }
1609 : :
1610 : : static struct spdk_sock_group_impl *
1611 : 667 : uring_sock_group_impl_get_optimal(struct spdk_sock *_sock, struct spdk_sock_group_impl *hint)
1612 : : {
1613 : 667 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1614 : 0 : struct spdk_sock_group_impl *group;
1615 : :
1616 [ - + ]: 667 : if (sock->placement_id != -1) {
1617 : 0 : spdk_sock_map_lookup(&g_map, sock->placement_id, &group, hint);
1618 : 0 : return group;
1619 : : }
1620 : :
1621 : 667 : return NULL;
1622 : : }
1623 : :
1624 : : static int
1625 : 414 : uring_sock_group_impl_buf_pool_free(struct spdk_uring_sock_group_impl *group_impl)
1626 : : {
1627 [ + - ]: 414 : if (group_impl->buf_ring) {
1628 : 414 : io_uring_unregister_buf_ring(&group_impl->uring, URING_BUF_GROUP_ID);
1629 : 414 : free(group_impl->buf_ring);
1630 : : }
1631 : :
1632 : 414 : free(group_impl->trackers);
1633 : :
1634 : 414 : return 0;
1635 : : }
1636 : :
1637 : : static int
1638 : 414 : uring_sock_group_impl_buf_pool_alloc(struct spdk_uring_sock_group_impl *group_impl)
1639 : : {
1640 : 414 : struct io_uring_buf_reg buf_reg = {};
1641 : 0 : struct io_uring_buf_ring *buf_ring;
1642 : : int i, rc;
1643 : :
1644 [ - + ]: 414 : rc = posix_memalign((void **)&buf_ring, 0x1000, URING_BUF_POOL_SIZE * sizeof(struct io_uring_buf));
1645 [ - + ]: 414 : if (rc != 0) {
1646 : : /* posix_memalign returns positive errno values */
1647 : 0 : return -rc;
1648 : : }
1649 : :
1650 : 414 : buf_reg.ring_addr = (unsigned long long)buf_ring;
1651 : 414 : buf_reg.ring_entries = URING_BUF_POOL_SIZE;
1652 : 414 : buf_reg.bgid = URING_BUF_GROUP_ID;
1653 : :
1654 : 414 : rc = io_uring_register_buf_ring(&group_impl->uring, &buf_reg, 0);
1655 [ - + ]: 414 : if (rc != 0) {
1656 : 0 : free(buf_ring);
1657 : 0 : return rc;
1658 : : }
1659 : :
1660 : 414 : group_impl->buf_ring = buf_ring;
1661 : 414 : io_uring_buf_ring_init(group_impl->buf_ring);
1662 : 414 : group_impl->buf_ring_count = 0;
1663 : :
1664 : 414 : group_impl->trackers = calloc(URING_BUF_POOL_SIZE, sizeof(struct spdk_uring_buf_tracker));
1665 [ - + ]: 414 : if (group_impl->trackers == NULL) {
1666 : 0 : uring_sock_group_impl_buf_pool_free(group_impl);
1667 : 0 : return -ENOMEM;
1668 : : }
1669 : :
1670 : 414 : STAILQ_INIT(&group_impl->free_trackers);
1671 : :
1672 [ + + ]: 53406 : for (i = 0; i < URING_BUF_POOL_SIZE; i++) {
1673 : 52992 : struct spdk_uring_buf_tracker *tracker = &group_impl->trackers[i];
1674 : :
1675 : 52992 : tracker->buf = NULL;
1676 : 52992 : tracker->len = 0;
1677 : 52992 : tracker->ctx = NULL;
1678 : 52992 : tracker->id = i;
1679 : :
1680 : 52992 : STAILQ_INSERT_TAIL(&group_impl->free_trackers, tracker, link);
1681 : : }
1682 : :
1683 : 414 : return 0;
1684 : : }
1685 : :
1686 : : static struct spdk_sock_group_impl *
1687 : 414 : uring_sock_group_impl_create(void)
1688 : : {
1689 : : struct spdk_uring_sock_group_impl *group_impl;
1690 : :
1691 : 414 : group_impl = calloc(1, sizeof(*group_impl));
1692 [ - + ]: 414 : if (group_impl == NULL) {
1693 : 0 : SPDK_ERRLOG("group_impl allocation failed\n");
1694 : 0 : return NULL;
1695 : : }
1696 : :
1697 : 414 : group_impl->io_avail = SPDK_SOCK_GROUP_QUEUE_DEPTH;
1698 : :
1699 [ - + ]: 414 : if (io_uring_queue_init(SPDK_SOCK_GROUP_QUEUE_DEPTH, &group_impl->uring, 0) < 0) {
1700 : 0 : SPDK_ERRLOG("uring I/O context setup failure\n");
1701 : 0 : free(group_impl);
1702 : 0 : return NULL;
1703 : : }
1704 : :
1705 : 414 : TAILQ_INIT(&group_impl->pending_recv);
1706 : :
1707 [ - + ]: 414 : if (uring_sock_group_impl_buf_pool_alloc(group_impl) < 0) {
1708 : 0 : SPDK_ERRLOG("Failed to create buffer ring. Your kernel is likely not new enough. "
1709 : : "Please switch to the POSIX sock implementation instead.\n");
1710 : 0 : io_uring_queue_exit(&group_impl->uring);
1711 : 0 : free(group_impl);
1712 : 0 : return NULL;
1713 : : }
1714 : :
1715 [ - + ]: 414 : if (g_spdk_uring_sock_impl_opts.enable_placement_id == PLACEMENT_CPU) {
1716 : 0 : spdk_sock_map_insert(&g_map, spdk_env_get_current_core(), &group_impl->base);
1717 : : }
1718 : :
1719 : 414 : return &group_impl->base;
1720 : : }
1721 : :
1722 : : static int
1723 : 1373 : uring_sock_group_impl_add_sock(struct spdk_sock_group_impl *_group,
1724 : : struct spdk_sock *_sock)
1725 : : {
1726 : 1373 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1727 : 1373 : struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
1728 : : int rc;
1729 : :
1730 : 1373 : sock->group = group;
1731 : 1373 : sock->write_task.sock = sock;
1732 : 1373 : sock->write_task.type = URING_TASK_WRITE;
1733 : :
1734 : 1373 : sock->read_task.sock = sock;
1735 : 1373 : sock->read_task.type = URING_TASK_READ;
1736 : :
1737 : 1373 : sock->errqueue_task.sock = sock;
1738 : 1373 : sock->errqueue_task.type = URING_TASK_ERRQUEUE;
1739 : 1373 : sock->errqueue_task.msg.msg_control = sock->buf;
1740 : 1373 : sock->errqueue_task.msg.msg_controllen = sizeof(sock->buf);
1741 : :
1742 : 1373 : sock->cancel_task.sock = sock;
1743 : 1373 : sock->cancel_task.type = URING_TASK_CANCEL;
1744 : :
1745 : : /* switched from another polling group due to scheduling */
1746 [ + + - + ]: 1373 : if (spdk_unlikely(sock->recv_pipe != NULL &&
1747 : : (spdk_pipe_reader_bytes_available(sock->recv_pipe) > 0))) {
1748 [ # # # # ]: 0 : assert(sock->pending_recv == false);
1749 : 0 : sock->pending_recv = true;
1750 : 0 : TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
1751 : : }
1752 : :
1753 [ - + ]: 1373 : if (sock->placement_id != -1) {
1754 : 0 : rc = spdk_sock_map_insert(&g_map, sock->placement_id, &group->base);
1755 [ # # ]: 0 : if (rc != 0) {
1756 : 0 : SPDK_ERRLOG("Failed to insert sock group into map: %d", rc);
1757 : : /* Do not treat this as an error. The system will continue running. */
1758 : : }
1759 : : }
1760 : :
1761 : : /* We get an async read going immediately */
1762 : 1373 : _sock_prep_read(&sock->base);
1763 : : #ifdef SPDK_ZEROCOPY
1764 [ - + - + ]: 1373 : if (sock->zcopy) {
1765 : 0 : _sock_prep_errqueue(_sock);
1766 : : }
1767 : : #endif
1768 : :
1769 : 1373 : return 0;
1770 : : }
1771 : :
1772 : : static void
1773 : 121603814 : uring_sock_group_populate_buf_ring(struct spdk_uring_sock_group_impl *group)
1774 : : {
1775 : : struct spdk_uring_buf_tracker *tracker;
1776 : : int count, mask;
1777 : :
1778 [ - + + - ]: 121603814 : if (g_spdk_uring_sock_impl_opts.enable_recv_pipe) {
1779 : : /* If recv_pipe is enabled, we do not post buffers. */
1780 : 121603814 : return;
1781 : : }
1782 : :
1783 : : /* Try to re-populate the io_uring's buffer pool using user-provided buffers */
1784 : 0 : tracker = STAILQ_FIRST(&group->free_trackers);
1785 : 0 : count = 0;
1786 : 0 : mask = io_uring_buf_ring_mask(URING_BUF_POOL_SIZE);
1787 [ # # ]: 0 : while (tracker != NULL) {
1788 : 0 : tracker->buflen = spdk_sock_group_get_buf(group->base.group, &tracker->buf, &tracker->ctx);
1789 [ # # ]: 0 : if (tracker->buflen == 0) {
1790 : 0 : break;
1791 : : }
1792 : :
1793 [ # # ]: 0 : assert(tracker->buf != NULL);
1794 [ # # ]: 0 : STAILQ_REMOVE_HEAD(&group->free_trackers, link);
1795 [ # # ]: 0 : assert(STAILQ_FIRST(&group->free_trackers) != tracker);
1796 : :
1797 : 0 : io_uring_buf_ring_add(group->buf_ring, tracker->buf, tracker->buflen, tracker->id, mask, count);
1798 : 0 : count++;
1799 : 0 : tracker = STAILQ_FIRST(&group->free_trackers);
1800 : : }
1801 : :
1802 [ # # ]: 0 : if (count > 0) {
1803 : 0 : group->buf_ring_count += count;
1804 : 0 : io_uring_buf_ring_advance(group->buf_ring, count);
1805 : : }
1806 : : }
1807 : :
1808 : : static int
1809 : 121603814 : uring_sock_group_impl_poll(struct spdk_sock_group_impl *_group, int max_events,
1810 : : struct spdk_sock **socks)
1811 : : {
1812 : 121603814 : struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
1813 : : int count, ret;
1814 : : int to_complete, to_submit;
1815 : : struct spdk_sock *_sock, *tmp;
1816 : : struct spdk_uring_sock *sock;
1817 : :
1818 [ + + ]: 121603814 : if (spdk_likely(socks)) {
1819 [ + + ]: 306594925 : TAILQ_FOREACH_SAFE(_sock, &group->base.socks, link, tmp) {
1820 : 184992484 : sock = __uring_sock(_sock);
1821 [ - + ]: 184992484 : if (spdk_unlikely(sock->connection_status)) {
1822 : 0 : continue;
1823 : : }
1824 : 184992484 : _sock_flush(_sock);
1825 : : }
1826 : : }
1827 : :
1828 : : /* Try to re-populate the io_uring's buffer pool using user-provided buffers */
1829 : 121603814 : uring_sock_group_populate_buf_ring(group);
1830 : :
1831 : 121603814 : to_submit = group->io_queued;
1832 : :
1833 : : /* For network I/O, it cannot be set with O_DIRECT, so we do not need to call spdk_io_uring_enter */
1834 [ + - ]: 121603814 : if (to_submit > 0) {
1835 : : /* If there are I/O to submit, use io_uring_submit here.
1836 : : * It will automatically call io_uring_enter appropriately. */
1837 : 121603814 : ret = io_uring_submit(&group->uring);
1838 [ - + ]: 121603814 : if (ret < 0) {
1839 : 0 : return 1;
1840 : : }
1841 : 121603814 : group->io_queued = 0;
1842 : 121603814 : group->io_inflight += to_submit;
1843 : 121603814 : group->io_avail -= to_submit;
1844 : : }
1845 : :
1846 : 121603814 : count = 0;
1847 : 121603814 : to_complete = group->io_inflight;
1848 [ - + - - ]: 121603814 : if (to_complete > 0 || !TAILQ_EMPTY(&group->pending_recv)) {
1849 : 121603814 : count = sock_uring_group_reap(group, to_complete, max_events, socks);
1850 : : }
1851 : :
1852 : 121603814 : return count;
1853 : : }
1854 : :
1855 : : static int
1856 : 1373 : uring_sock_group_impl_remove_sock(struct spdk_sock_group_impl *_group,
1857 : : struct spdk_sock *_sock)
1858 : : {
1859 : 1373 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1860 : 1373 : struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
1861 : :
1862 : 1373 : sock->pending_group_remove = true;
1863 : :
1864 [ - + ]: 1373 : if (sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
1865 : 0 : _sock_prep_cancel_task(_sock, &sock->write_task);
1866 : : /* Since spdk_sock_group_remove_sock is not asynchronous interface, so
1867 : : * currently can use a while loop here. */
1868 [ # # ]: 0 : while ((sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) ||
1869 [ # # ]: 0 : (sock->cancel_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE)) {
1870 : 0 : uring_sock_group_impl_poll(_group, 32, NULL);
1871 : : }
1872 : : }
1873 : :
1874 [ + - ]: 1373 : if (sock->read_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
1875 : 1373 : _sock_prep_cancel_task(_sock, &sock->read_task);
1876 : : /* Since spdk_sock_group_remove_sock is not asynchronous interface, so
1877 : : * currently can use a while loop here. */
1878 [ + + ]: 2746 : while ((sock->read_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) ||
1879 [ - + ]: 1373 : (sock->cancel_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE)) {
1880 : 1373 : uring_sock_group_impl_poll(_group, 32, NULL);
1881 : : }
1882 : : }
1883 : :
1884 [ - + ]: 1373 : if (sock->errqueue_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
1885 : 0 : _sock_prep_cancel_task(_sock, &sock->errqueue_task);
1886 : : /* Since spdk_sock_group_remove_sock is not asynchronous interface, so
1887 : : * currently can use a while loop here. */
1888 [ # # ]: 0 : while ((sock->errqueue_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) ||
1889 [ # # ]: 0 : (sock->cancel_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE)) {
1890 : 0 : uring_sock_group_impl_poll(_group, 32, NULL);
1891 : : }
1892 : : }
1893 : :
1894 : : /* Make sure the cancelling the tasks above didn't cause sending new requests */
1895 [ - + ]: 1373 : assert(sock->write_task.status == SPDK_URING_SOCK_TASK_NOT_IN_USE);
1896 [ - + ]: 1373 : assert(sock->read_task.status == SPDK_URING_SOCK_TASK_NOT_IN_USE);
1897 [ - + ]: 1373 : assert(sock->errqueue_task.status == SPDK_URING_SOCK_TASK_NOT_IN_USE);
1898 : :
1899 [ - + + - ]: 1373 : if (sock->pending_recv) {
1900 [ + + ]: 1373 : TAILQ_REMOVE(&group->pending_recv, sock, link);
1901 : 1373 : sock->pending_recv = false;
1902 : : }
1903 [ - + - + ]: 1373 : assert(sock->pending_recv == false);
1904 : :
1905 : : /* We have no way to handle this case. We could let the user read this
1906 : : * buffer, but the buffer came from a group and we have lost the association
1907 : : * to that so we couldn't release it. */
1908 [ - + ]: 1373 : assert(STAILQ_EMPTY(&sock->recv_stream));
1909 : :
1910 [ - + ]: 1373 : if (sock->placement_id != -1) {
1911 : 0 : spdk_sock_map_release(&g_map, sock->placement_id);
1912 : : }
1913 : :
1914 : 1373 : sock->pending_group_remove = false;
1915 : 1373 : sock->group = NULL;
1916 : 1373 : return 0;
1917 : : }
1918 : :
1919 : : static int
1920 : 414 : uring_sock_group_impl_close(struct spdk_sock_group_impl *_group)
1921 : : {
1922 : 414 : struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
1923 : :
1924 : : /* try to reap all the active I/O */
1925 [ - + ]: 414 : while (group->io_inflight) {
1926 : 0 : uring_sock_group_impl_poll(_group, 32, NULL);
1927 : : }
1928 [ - + ]: 414 : assert(group->io_inflight == 0);
1929 [ - + ]: 414 : assert(group->io_avail == SPDK_SOCK_GROUP_QUEUE_DEPTH);
1930 : :
1931 : 414 : uring_sock_group_impl_buf_pool_free(group);
1932 : :
1933 : 414 : io_uring_queue_exit(&group->uring);
1934 : :
1935 [ - + ]: 414 : if (g_spdk_uring_sock_impl_opts.enable_placement_id == PLACEMENT_CPU) {
1936 : 0 : spdk_sock_map_release(&g_map, spdk_env_get_current_core());
1937 : : }
1938 : :
1939 : 414 : free(group);
1940 : 414 : return 0;
1941 : : }
1942 : :
1943 : : static int
1944 : 91503 : uring_sock_flush(struct spdk_sock *_sock)
1945 : : {
1946 : 91503 : struct spdk_uring_sock *sock = __uring_sock(_sock);
1947 : 91503 : struct msghdr msg = {};
1948 : 6 : struct iovec iovs[IOV_BATCH_SIZE];
1949 : : int iovcnt;
1950 : : ssize_t rc;
1951 : 91503 : int flags = sock->zcopy_send_flags;
1952 : : int retval;
1953 : 91503 : bool is_zcopy = false;
1954 : 91503 : struct spdk_uring_task *task = &sock->errqueue_task;
1955 : :
1956 : : /* Can't flush from within a callback or we end up with recursive calls */
1957 [ - + ]: 91503 : if (_sock->cb_cnt > 0) {
1958 : 0 : errno = EAGAIN;
1959 : 0 : return -1;
1960 : : }
1961 : :
1962 : : /* Can't flush while a write is already outstanding */
1963 [ - + ]: 91503 : if (sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
1964 : 0 : errno = EAGAIN;
1965 : 0 : return -1;
1966 : : }
1967 : :
1968 : : /* Gather an iov */
1969 : 91503 : iovcnt = spdk_sock_prep_reqs(_sock, iovs, 0, NULL, &flags);
1970 [ + + ]: 91503 : if (iovcnt == 0) {
1971 : : /* Nothing to send */
1972 : 85922 : return 0;
1973 : : }
1974 : :
1975 : : /* Perform the vectored write */
1976 : 5581 : msg.msg_iov = iovs;
1977 : 5581 : msg.msg_iovlen = iovcnt;
1978 : 5581 : rc = sendmsg(sock->fd, &msg, flags | MSG_DONTWAIT);
1979 [ - + ]: 5581 : if (rc <= 0) {
1980 [ # # # # : 0 : if (rc == 0 || errno == EAGAIN || errno == EWOULDBLOCK || (errno == ENOBUFS && sock->zcopy)) {
# # # # #
# # # ]
1981 : 0 : errno = EAGAIN;
1982 : : }
1983 : 0 : return -1;
1984 : : }
1985 : :
1986 : : #ifdef SPDK_ZEROCOPY
1987 : 5581 : is_zcopy = flags & MSG_ZEROCOPY;
1988 : : #endif
1989 : 5581 : retval = sock_complete_write_reqs(_sock, rc, is_zcopy);
1990 [ - + ]: 5581 : if (retval < 0) {
1991 : : /* if the socket is closed, return to avoid heap-use-after-free error */
1992 : 0 : errno = ENOTCONN;
1993 : 0 : return -1;
1994 : : }
1995 : :
1996 : : #ifdef SPDK_ZEROCOPY
1997 : : /* At least do once to check zero copy case */
1998 [ - + - + : 5581 : if (sock->zcopy && !TAILQ_EMPTY(&_sock->pending_reqs)) {
- - ]
1999 : 0 : retval = recvmsg(sock->fd, &task->msg, MSG_ERRQUEUE);
2000 [ # # ]: 0 : if (retval < 0) {
2001 [ # # # # ]: 0 : if (errno == EWOULDBLOCK || errno == EAGAIN) {
2002 : 0 : return rc;
2003 : : }
2004 : : }
2005 : 0 : _sock_check_zcopy(_sock, retval);;
2006 : : }
2007 : : #endif
2008 : :
2009 : 5581 : return rc;
2010 : : }
2011 : :
2012 : : static struct spdk_net_impl g_uring_net_impl = {
2013 : : .name = "uring",
2014 : : .getaddr = uring_sock_getaddr,
2015 : : .connect = uring_sock_connect,
2016 : : .listen = uring_sock_listen,
2017 : : .accept = uring_sock_accept,
2018 : : .close = uring_sock_close,
2019 : : .recv = uring_sock_recv,
2020 : : .readv = uring_sock_readv,
2021 : : .writev = uring_sock_writev,
2022 : : .recv_next = uring_sock_recv_next,
2023 : : .writev_async = uring_sock_writev_async,
2024 : : .flush = uring_sock_flush,
2025 : : .set_recvlowat = uring_sock_set_recvlowat,
2026 : : .set_recvbuf = uring_sock_set_recvbuf,
2027 : : .set_sendbuf = uring_sock_set_sendbuf,
2028 : : .is_ipv6 = uring_sock_is_ipv6,
2029 : : .is_ipv4 = uring_sock_is_ipv4,
2030 : : .is_connected = uring_sock_is_connected,
2031 : : .group_impl_get_optimal = uring_sock_group_impl_get_optimal,
2032 : : .group_impl_create = uring_sock_group_impl_create,
2033 : : .group_impl_add_sock = uring_sock_group_impl_add_sock,
2034 : : .group_impl_remove_sock = uring_sock_group_impl_remove_sock,
2035 : : .group_impl_poll = uring_sock_group_impl_poll,
2036 : : .group_impl_close = uring_sock_group_impl_close,
2037 : : .get_opts = uring_sock_impl_get_opts,
2038 : : .set_opts = uring_sock_impl_set_opts,
2039 : : };
2040 : :
2041 : 393 : SPDK_NET_IMPL_REGISTER(uring, &g_uring_net_impl);
|