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