Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2016 Intel Corporation. All rights reserved.
3 : : * Copyright (c) 2020 Mellanox Technologies LTD. All rights reserved.
4 : : * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5 : : */
6 : :
7 : : #include "spdk/stdinc.h"
8 : :
9 : : #include "spdk/sock.h"
10 : : #include "spdk_internal/sock.h"
11 : : #include "spdk/log.h"
12 : : #include "spdk/env.h"
13 : : #include "spdk/util.h"
14 : : #include "spdk/trace.h"
15 : : #include "spdk/thread.h"
16 : : #include "spdk_internal/trace_defs.h"
17 : :
18 : : #define SPDK_SOCK_DEFAULT_PRIORITY 0
19 : : #define SPDK_SOCK_DEFAULT_ZCOPY true
20 : : #define SPDK_SOCK_DEFAULT_ACK_TIMEOUT 0
21 : :
22 : : #define SPDK_SOCK_OPTS_FIELD_OK(opts, field) (offsetof(struct spdk_sock_opts, field) + sizeof(opts->field) <= (opts->opts_size))
23 : :
24 : : static STAILQ_HEAD(, spdk_net_impl) g_net_impls = STAILQ_HEAD_INITIALIZER(g_net_impls);
25 : : static struct spdk_net_impl *g_default_impl;
26 : :
27 : : struct spdk_sock_placement_id_entry {
28 : : int placement_id;
29 : : uint32_t ref;
30 : : struct spdk_sock_group_impl *group;
31 : : STAILQ_ENTRY(spdk_sock_placement_id_entry) link;
32 : : };
33 : :
34 : : static inline struct spdk_sock_group_impl *
35 : 17820 : sock_get_group_impl_from_group(struct spdk_sock *sock, struct spdk_sock_group *group)
36 : : {
37 : 17820 : struct spdk_sock_group_impl *group_impl = NULL;
38 : :
39 [ + - + - ]: 37992 : STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
40 [ + + ]: 37992 : if (sock->net_impl == group_impl->net_impl) {
41 : 17820 : return group_impl;
42 : : }
43 : : }
44 : 0 : return NULL;
45 : : }
46 : :
47 : : /* Called under map->mtx lock */
48 : : static struct spdk_sock_placement_id_entry *
49 : 30 : _sock_map_entry_alloc(struct spdk_sock_map *map, int placement_id)
50 : : {
51 : : struct spdk_sock_placement_id_entry *entry;
52 : :
53 : 30 : entry = calloc(1, sizeof(*entry));
54 [ - + ]: 30 : if (!entry) {
55 : 0 : SPDK_ERRLOG("Cannot allocate an entry for placement_id=%u\n", placement_id);
56 : 0 : return NULL;
57 : : }
58 : :
59 : 30 : entry->placement_id = placement_id;
60 : :
61 : 30 : STAILQ_INSERT_TAIL(&map->entries, entry, link);
62 : :
63 : 30 : return entry;
64 : : }
65 : :
66 : : int
67 : 49 : spdk_sock_map_insert(struct spdk_sock_map *map, int placement_id,
68 : : struct spdk_sock_group_impl *group)
69 : : {
70 : : struct spdk_sock_placement_id_entry *entry;
71 : 49 : int rc = 0;
72 : :
73 [ - + ]: 49 : pthread_mutex_lock(&map->mtx);
74 [ + + ]: 56 : STAILQ_FOREACH(entry, &map->entries, link) {
75 [ + + ]: 32 : if (placement_id == entry->placement_id) {
76 : : /* Can't set group to NULL if it is already not-NULL */
77 [ - + ]: 25 : if (group == NULL) {
78 [ # # ]: 0 : rc = (entry->group == NULL) ? 0 : -EINVAL;
79 : 0 : goto end;
80 : : }
81 : :
82 [ + + ]: 25 : if (entry->group == NULL) {
83 : 4 : entry->group = group;
84 [ + + ]: 21 : } else if (entry->group != group) {
85 : 8 : rc = -EINVAL;
86 : 8 : goto end;
87 : : }
88 : :
89 : 17 : entry->ref++;
90 : 17 : goto end;
91 : : }
92 : : }
93 : :
94 : 24 : entry = _sock_map_entry_alloc(map, placement_id);
95 [ - + ]: 24 : if (entry == NULL) {
96 : 0 : rc = -ENOMEM;
97 : 0 : goto end;
98 : : }
99 [ + + ]: 24 : if (group) {
100 : 20 : entry->group = group;
101 : 20 : entry->ref++;
102 : : }
103 : 4 : end:
104 [ - + ]: 49 : pthread_mutex_unlock(&map->mtx);
105 : :
106 : 49 : return rc;
107 : : }
108 : :
109 : : void
110 : 17 : spdk_sock_map_release(struct spdk_sock_map *map, int placement_id)
111 : : {
112 : : struct spdk_sock_placement_id_entry *entry;
113 : :
114 [ - + ]: 17 : pthread_mutex_lock(&map->mtx);
115 [ + - ]: 20 : STAILQ_FOREACH(entry, &map->entries, link) {
116 [ + + ]: 20 : if (placement_id == entry->placement_id) {
117 [ - + ]: 17 : assert(entry->ref > 0);
118 : 17 : entry->ref--;
119 : :
120 [ + + ]: 17 : if (entry->ref == 0) {
121 : 10 : entry->group = NULL;
122 : : }
123 : 17 : break;
124 : : }
125 : : }
126 : :
127 [ - + ]: 17 : pthread_mutex_unlock(&map->mtx);
128 : 17 : }
129 : :
130 : : int
131 : 53 : spdk_sock_map_lookup(struct spdk_sock_map *map, int placement_id,
132 : : struct spdk_sock_group_impl **group, struct spdk_sock_group_impl *hint)
133 : : {
134 : : struct spdk_sock_placement_id_entry *entry;
135 : :
136 : 53 : *group = NULL;
137 [ - + ]: 53 : pthread_mutex_lock(&map->mtx);
138 [ + + ]: 60 : STAILQ_FOREACH(entry, &map->entries, link) {
139 [ + + ]: 50 : if (placement_id == entry->placement_id) {
140 : 43 : *group = entry->group;
141 [ + + ]: 43 : if (*group != NULL) {
142 : : /* Return previously assigned sock_group */
143 [ - + ]: 35 : pthread_mutex_unlock(&map->mtx);
144 : 35 : return 0;
145 : : }
146 : 8 : break;
147 : : }
148 : : }
149 : :
150 : : /* No entry with assigned sock_group, nor hint to use */
151 [ + + ]: 18 : if (hint == NULL) {
152 [ - + ]: 12 : pthread_mutex_unlock(&map->mtx);
153 : 12 : return -EINVAL;
154 : : }
155 : :
156 : : /* Create new entry if there is none with matching placement_id */
157 [ + - ]: 6 : if (entry == NULL) {
158 : 6 : entry = _sock_map_entry_alloc(map, placement_id);
159 [ - + ]: 6 : if (entry == NULL) {
160 [ # # ]: 0 : pthread_mutex_unlock(&map->mtx);
161 : 0 : return -ENOMEM;
162 : : }
163 : : }
164 : :
165 : 6 : entry->group = hint;
166 [ - + ]: 6 : pthread_mutex_unlock(&map->mtx);
167 : :
168 : 6 : return 0;
169 : : }
170 : :
171 : : void
172 : 3144 : spdk_sock_map_cleanup(struct spdk_sock_map *map)
173 : : {
174 : : struct spdk_sock_placement_id_entry *entry, *tmp;
175 : :
176 [ - + ]: 3144 : pthread_mutex_lock(&map->mtx);
177 [ + + ]: 3174 : STAILQ_FOREACH_SAFE(entry, &map->entries, link, tmp) {
178 [ + - + + : 30 : STAILQ_REMOVE(&map->entries, entry, spdk_sock_placement_id_entry, link);
- - - - ]
179 : 30 : free(entry);
180 : : }
181 [ - + ]: 3144 : pthread_mutex_unlock(&map->mtx);
182 : 3144 : }
183 : :
184 : : int
185 : 20 : spdk_sock_map_find_free(struct spdk_sock_map *map)
186 : : {
187 : : struct spdk_sock_placement_id_entry *entry;
188 : 20 : int placement_id = -1;
189 : :
190 [ - + ]: 20 : pthread_mutex_lock(&map->mtx);
191 [ + + ]: 28 : STAILQ_FOREACH(entry, &map->entries, link) {
192 [ + + ]: 16 : if (entry->group == NULL) {
193 : 8 : placement_id = entry->placement_id;
194 : 8 : break;
195 : : }
196 : : }
197 : :
198 [ - + ]: 20 : pthread_mutex_unlock(&map->mtx);
199 : :
200 : 20 : return placement_id;
201 : : }
202 : :
203 : : int
204 : 4674 : spdk_sock_get_optimal_sock_group(struct spdk_sock *sock, struct spdk_sock_group **group,
205 : : struct spdk_sock_group *hint)
206 : : {
207 : : struct spdk_sock_group_impl *group_impl;
208 : 4674 : struct spdk_sock_group_impl *hint_group_impl = NULL;
209 : :
210 [ - + ]: 4674 : assert(group != NULL);
211 : :
212 [ + - ]: 4674 : if (hint != NULL) {
213 : 4674 : hint_group_impl = sock_get_group_impl_from_group(sock, hint);
214 [ - + ]: 4674 : if (hint_group_impl == NULL) {
215 : 0 : return -EINVAL;
216 : : }
217 : : }
218 : :
219 : 4674 : group_impl = sock->net_impl->group_impl_get_optimal(sock, hint_group_impl);
220 : :
221 [ + + ]: 4674 : if (group_impl) {
222 : 3 : *group = group_impl->group;
223 : : }
224 : :
225 : 4674 : return 0;
226 : : }
227 : :
228 : : int
229 : 9917 : spdk_sock_getaddr(struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport,
230 : : char *caddr, int clen, uint16_t *cport)
231 : : {
232 : 9917 : return sock->net_impl->getaddr(sock, saddr, slen, sport, caddr, clen, cport);
233 : : }
234 : :
235 : : const char *
236 : 24 : spdk_sock_get_interface_name(struct spdk_sock *sock)
237 : : {
238 [ + - ]: 24 : if (sock->net_impl->get_interface_name) {
239 : 24 : return sock->net_impl->get_interface_name(sock);
240 : : } else {
241 : 0 : return NULL;
242 : : }
243 : : }
244 : :
245 : : uint32_t
246 : 0 : spdk_sock_get_numa_socket_id(struct spdk_sock *sock)
247 : : {
248 [ # # ]: 0 : if (sock->net_impl->get_numa_socket_id) {
249 : 0 : return sock->net_impl->get_numa_socket_id(sock);
250 : : } else {
251 : 0 : return SPDK_ENV_SOCKET_ID_ANY;
252 : : }
253 : : }
254 : :
255 : : const char *
256 : 522 : spdk_sock_get_impl_name(struct spdk_sock *sock)
257 : : {
258 : 522 : return sock->net_impl->name;
259 : : }
260 : :
261 : : void
262 : 43658 : spdk_sock_get_default_opts(struct spdk_sock_opts *opts)
263 : : {
264 [ - + ]: 43658 : assert(opts);
265 : :
266 [ + + ]: 43658 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, priority)) {
267 : 43650 : opts->priority = SPDK_SOCK_DEFAULT_PRIORITY;
268 : : }
269 : :
270 [ + + ]: 43658 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, zcopy)) {
271 : 43650 : opts->zcopy = SPDK_SOCK_DEFAULT_ZCOPY;
272 : : }
273 : :
274 [ + + ]: 43658 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, ack_timeout)) {
275 : 43650 : opts->ack_timeout = SPDK_SOCK_DEFAULT_ACK_TIMEOUT;
276 : : }
277 : :
278 [ + + ]: 43658 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts)) {
279 : 43650 : opts->impl_opts = NULL;
280 : : }
281 : :
282 [ + + ]: 43658 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts_size)) {
283 : 43650 : opts->impl_opts_size = 0;
284 : : }
285 : 43658 : }
286 : :
287 : : /*
288 : : * opts The opts allocated in the current library.
289 : : * opts_user The opts passed by the caller.
290 : : * */
291 : : static void
292 : 21821 : sock_init_opts(struct spdk_sock_opts *opts, struct spdk_sock_opts *opts_user)
293 : : {
294 [ - + ]: 21821 : assert(opts);
295 [ - + ]: 21821 : assert(opts_user);
296 : :
297 : 21821 : opts->opts_size = sizeof(*opts);
298 : 21821 : spdk_sock_get_default_opts(opts);
299 : :
300 : : /* reset the size according to the user */
301 : 21821 : opts->opts_size = opts_user->opts_size;
302 [ + - ]: 21821 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, priority)) {
303 : 21821 : opts->priority = opts_user->priority;
304 : : }
305 : :
306 [ + - ]: 21821 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, zcopy)) {
307 [ - + ]: 21821 : opts->zcopy = opts_user->zcopy;
308 : : }
309 : :
310 [ + - ]: 21821 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, ack_timeout)) {
311 : 21821 : opts->ack_timeout = opts_user->ack_timeout;
312 : : }
313 : :
314 [ + - ]: 21821 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts)) {
315 : 21821 : opts->impl_opts = opts_user->impl_opts;
316 : : }
317 : :
318 [ + - ]: 21821 : if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts)) {
319 : 21821 : opts->impl_opts_size = opts_user->impl_opts_size;
320 : : }
321 : 21821 : }
322 : :
323 : : struct spdk_sock *
324 : 34 : spdk_sock_connect(const char *ip, int port, const char *impl_name)
325 : : {
326 : 32 : struct spdk_sock_opts opts;
327 : :
328 : 34 : opts.opts_size = sizeof(opts);
329 : 34 : spdk_sock_get_default_opts(&opts);
330 : 34 : return spdk_sock_connect_ext(ip, port, impl_name, &opts);
331 : : }
332 : :
333 : : struct spdk_sock *
334 : 21456 : spdk_sock_connect_ext(const char *ip, int port, const char *_impl_name, struct spdk_sock_opts *opts)
335 : : {
336 : 21456 : struct spdk_net_impl *impl = NULL;
337 : : struct spdk_sock *sock;
338 : 140 : struct spdk_sock_opts opts_local;
339 : 21456 : const char *impl_name = NULL;
340 : :
341 [ - + ]: 21456 : if (opts == NULL) {
342 : 0 : SPDK_ERRLOG("the opts should not be NULL pointer\n");
343 : 0 : return NULL;
344 : : }
345 : :
346 [ + + ]: 21456 : if (_impl_name) {
347 : 192 : impl_name = _impl_name;
348 [ + - ]: 21264 : } else if (g_default_impl) {
349 : 21264 : impl_name = g_default_impl->name;
350 : : }
351 : :
352 [ + - + + ]: 61104 : STAILQ_FOREACH_FROM(impl, &g_net_impls, link) {
353 [ + - + + : 43236 : if (impl_name && strncmp(impl_name, impl->name, strlen(impl->name) + 1)) {
- + - + +
+ ]
354 : 21780 : continue;
355 : : }
356 : :
357 [ - + + + ]: 21456 : SPDK_DEBUGLOG(sock, "Creating a client socket using impl %s\n", impl->name);
358 : 21456 : sock_init_opts(&opts_local, opts);
359 : 21456 : sock = impl->connect(ip, port, &opts_local);
360 [ + + ]: 21456 : if (sock != NULL) {
361 : : /* Copy the contents, both the two structures are the same ABI version */
362 [ - + ]: 3588 : memcpy(&sock->opts, &opts_local, sizeof(sock->opts));
363 : : /* Clear out impl_opts to make sure we don't keep reference to a dangling
364 : : * pointer */
365 : 3588 : sock->opts.impl_opts = NULL;
366 : 3588 : sock->net_impl = impl;
367 : 3588 : TAILQ_INIT(&sock->queued_reqs);
368 : 3588 : TAILQ_INIT(&sock->pending_reqs);
369 : :
370 : 3588 : return sock;
371 : : }
372 : : }
373 : :
374 : 17868 : return NULL;
375 : : }
376 : :
377 : : struct spdk_sock *
378 : 80 : spdk_sock_listen(const char *ip, int port, const char *impl_name)
379 : : {
380 : 24 : struct spdk_sock_opts opts;
381 : :
382 : 80 : opts.opts_size = sizeof(opts);
383 : 80 : spdk_sock_get_default_opts(&opts);
384 : 80 : return spdk_sock_listen_ext(ip, port, impl_name, &opts);
385 : : }
386 : :
387 : : struct spdk_sock *
388 : 365 : spdk_sock_listen_ext(const char *ip, int port, const char *_impl_name, struct spdk_sock_opts *opts)
389 : : {
390 : 365 : struct spdk_net_impl *impl = NULL;
391 : : struct spdk_sock *sock;
392 : 55 : struct spdk_sock_opts opts_local;
393 : 365 : const char *impl_name = NULL;
394 : :
395 [ - + ]: 365 : if (opts == NULL) {
396 : 0 : SPDK_ERRLOG("the opts should not be NULL pointer\n");
397 : 0 : return NULL;
398 : : }
399 : :
400 [ + + ]: 365 : if (_impl_name) {
401 : 75 : impl_name = _impl_name;
402 [ + - ]: 290 : } else if (g_default_impl) {
403 : 290 : impl_name = g_default_impl->name;
404 : : }
405 : :
406 [ + - + - ]: 787 : STAILQ_FOREACH_FROM(impl, &g_net_impls, link) {
407 [ + - + + : 787 : if (impl_name && strncmp(impl_name, impl->name, strlen(impl->name) + 1)) {
- + - + +
+ ]
408 : 422 : continue;
409 : : }
410 : :
411 [ - + - + ]: 365 : SPDK_DEBUGLOG(sock, "Creating a listening socket using impl %s\n", impl->name);
412 : 365 : sock_init_opts(&opts_local, opts);
413 : 365 : sock = impl->listen(ip, port, &opts_local);
414 [ + - ]: 365 : if (sock != NULL) {
415 : : /* Copy the contents, both the two structures are the same ABI version */
416 [ - + ]: 365 : memcpy(&sock->opts, &opts_local, sizeof(sock->opts));
417 : : /* Clear out impl_opts to make sure we don't keep reference to a dangling
418 : : * pointer */
419 : 365 : sock->opts.impl_opts = NULL;
420 : 365 : sock->net_impl = impl;
421 : : /* Don't need to initialize the request queues for listen
422 : : * sockets. */
423 : 365 : return sock;
424 : : }
425 : : }
426 : :
427 : 0 : return NULL;
428 : : }
429 : :
430 : : struct spdk_sock *
431 : 989454 : spdk_sock_accept(struct spdk_sock *sock)
432 : : {
433 : : struct spdk_sock *new_sock;
434 : :
435 : 989454 : new_sock = sock->net_impl->accept(sock);
436 [ + + ]: 989454 : if (new_sock != NULL) {
437 : : /* Inherit the opts from the "accept sock" */
438 : 5273 : new_sock->opts = sock->opts;
439 [ - + - + ]: 5273 : memcpy(&new_sock->opts, &sock->opts, sizeof(new_sock->opts));
440 : 5273 : new_sock->net_impl = sock->net_impl;
441 : 5273 : TAILQ_INIT(&new_sock->queued_reqs);
442 : 5273 : TAILQ_INIT(&new_sock->pending_reqs);
443 : : }
444 : :
445 : 989454 : return new_sock;
446 : : }
447 : :
448 : : int
449 : 27111 : spdk_sock_close(struct spdk_sock **_sock)
450 : : {
451 : 27111 : struct spdk_sock *sock = *_sock;
452 : :
453 [ + + ]: 27111 : if (sock == NULL) {
454 : 17855 : errno = EBADF;
455 : 17855 : return -1;
456 : : }
457 : :
458 [ + + ]: 9256 : if (sock->cb_fn != NULL) {
459 : : /* This sock is still part of a sock_group. */
460 : 8 : errno = EBUSY;
461 : 8 : return -1;
462 : : }
463 : :
464 : : /* Beyond this point the socket is considered closed. */
465 : 9248 : *_sock = NULL;
466 : :
467 : 9248 : sock->flags.closed = true;
468 : :
469 [ + + ]: 9248 : if (sock->cb_cnt > 0) {
470 : : /* Let the callback unwind before destroying the socket */
471 : 22 : return 0;
472 : : }
473 : :
474 : 9226 : spdk_sock_abort_requests(sock);
475 : :
476 : 9226 : return sock->net_impl->close(sock);
477 : : }
478 : :
479 : : ssize_t
480 : 210833762 : spdk_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
481 : : {
482 [ + - - + ]: 210833762 : if (sock == NULL || sock->flags.closed) {
483 : 0 : errno = EBADF;
484 : 0 : return -1;
485 : : }
486 : :
487 : 210833762 : return sock->net_impl->recv(sock, buf, len);
488 : : }
489 : :
490 : : ssize_t
491 : 2132449 : spdk_sock_readv(struct spdk_sock *sock, struct iovec *iov, int iovcnt)
492 : : {
493 [ + - - + ]: 2132449 : if (sock == NULL || sock->flags.closed) {
494 : 0 : errno = EBADF;
495 : 0 : return -1;
496 : : }
497 : :
498 : 2132449 : return sock->net_impl->readv(sock, iov, iovcnt);
499 : : }
500 : :
501 : : ssize_t
502 : 645 : spdk_sock_writev(struct spdk_sock *sock, struct iovec *iov, int iovcnt)
503 : : {
504 [ + - - + ]: 645 : if (sock == NULL || sock->flags.closed) {
505 : 0 : errno = EBADF;
506 : 0 : return -1;
507 : : }
508 : :
509 : 645 : return sock->net_impl->writev(sock, iov, iovcnt);
510 : : }
511 : :
512 : : void
513 : 38845217 : spdk_sock_writev_async(struct spdk_sock *sock, struct spdk_sock_request *req)
514 : : {
515 [ - + ]: 38845217 : assert(req->cb_fn != NULL);
516 : :
517 [ + - - + ]: 38845217 : if (sock == NULL || sock->flags.closed) {
518 : 0 : req->cb_fn(req->cb_arg, -EBADF);
519 : 0 : return;
520 : : }
521 : :
522 : 38845217 : sock->net_impl->writev_async(sock, req);
523 : : }
524 : :
525 : : int
526 : 36 : spdk_sock_recv_next(struct spdk_sock *sock, void **buf, void **ctx)
527 : : {
528 [ + - - + ]: 36 : if (sock == NULL || sock->flags.closed) {
529 : 0 : errno = EBADF;
530 : 0 : return -1;
531 : : }
532 : :
533 [ - + ]: 36 : if (sock->group_impl == NULL) {
534 : 0 : errno = ENOTSUP;
535 : 0 : return -1;
536 : : }
537 : :
538 : 36 : return sock->net_impl->recv_next(sock, buf, ctx);
539 : : }
540 : :
541 : : int
542 : 23765830 : spdk_sock_flush(struct spdk_sock *sock)
543 : : {
544 [ + + - + ]: 23765830 : if (sock == NULL || sock->flags.closed) {
545 : 436 : errno = EBADF;
546 : 436 : return -1;
547 : : }
548 : :
549 : 23765394 : return sock->net_impl->flush(sock);
550 : : }
551 : :
552 : : int
553 : 5225 : spdk_sock_set_recvlowat(struct spdk_sock *sock, int nbytes)
554 : : {
555 : 5225 : return sock->net_impl->set_recvlowat(sock, nbytes);
556 : : }
557 : :
558 : : int
559 : 12983 : spdk_sock_set_recvbuf(struct spdk_sock *sock, int sz)
560 : : {
561 : 12983 : return sock->net_impl->set_recvbuf(sock, sz);
562 : : }
563 : :
564 : : int
565 : 8 : spdk_sock_set_sendbuf(struct spdk_sock *sock, int sz)
566 : : {
567 : 8 : return sock->net_impl->set_sendbuf(sock, sz);
568 : : }
569 : :
570 : : bool
571 : 8 : spdk_sock_is_ipv6(struct spdk_sock *sock)
572 : : {
573 : 8 : return sock->net_impl->is_ipv6(sock);
574 : : }
575 : :
576 : : bool
577 : 6895 : spdk_sock_is_ipv4(struct spdk_sock *sock)
578 : : {
579 : 6895 : return sock->net_impl->is_ipv4(sock);
580 : : }
581 : :
582 : : bool
583 : 191229 : spdk_sock_is_connected(struct spdk_sock *sock)
584 : : {
585 : 191229 : return sock->net_impl->is_connected(sock);
586 : : }
587 : :
588 : : struct spdk_sock_group *
589 : 2120 : spdk_sock_group_create(void *ctx)
590 : : {
591 : 2120 : struct spdk_net_impl *impl = NULL;
592 : : struct spdk_sock_group *group;
593 : : struct spdk_sock_group_impl *group_impl;
594 : :
595 : 2120 : group = calloc(1, sizeof(*group));
596 [ - + ]: 2120 : if (group == NULL) {
597 : 0 : return NULL;
598 : : }
599 : :
600 : 2120 : STAILQ_INIT(&group->group_impls);
601 : 2120 : STAILQ_INIT(&group->pool);
602 : :
603 [ + - + + ]: 6787 : STAILQ_FOREACH_FROM(impl, &g_net_impls, link) {
604 : 4667 : group_impl = impl->group_impl_create();
605 [ + - ]: 4667 : if (group_impl != NULL) {
606 : 4667 : STAILQ_INSERT_TAIL(&group->group_impls, group_impl, link);
607 : 4667 : TAILQ_INIT(&group_impl->socks);
608 : 4667 : group_impl->net_impl = impl;
609 : 4667 : group_impl->group = group;
610 : : }
611 : : }
612 : :
613 : 2120 : group->ctx = ctx;
614 : :
615 : 2120 : return group;
616 : : }
617 : :
618 : : void *
619 : 4674 : spdk_sock_group_get_ctx(struct spdk_sock_group *group)
620 : : {
621 [ + + ]: 4674 : if (group == NULL) {
622 : 4 : return NULL;
623 : : }
624 : :
625 : 4670 : return group->ctx;
626 : : }
627 : :
628 : : int
629 : 6589 : spdk_sock_group_add_sock(struct spdk_sock_group *group, struct spdk_sock *sock,
630 : : spdk_sock_cb cb_fn, void *cb_arg)
631 : : {
632 : 6589 : struct spdk_sock_group_impl *group_impl = NULL;
633 : : int rc;
634 : :
635 [ + + ]: 6589 : if (cb_fn == NULL) {
636 : 8 : errno = EINVAL;
637 : 8 : return -1;
638 : : }
639 : :
640 [ + + ]: 6581 : if (sock->group_impl != NULL) {
641 : : /*
642 : : * This sock is already part of a sock_group.
643 : : */
644 : 8 : errno = EINVAL;
645 : 8 : return -1;
646 : : }
647 : :
648 : 6573 : group_impl = sock_get_group_impl_from_group(sock, group);
649 [ - + ]: 6573 : if (group_impl == NULL) {
650 : 0 : errno = EINVAL;
651 : 0 : return -1;
652 : : }
653 : :
654 : 6573 : rc = group_impl->net_impl->group_impl_add_sock(group_impl, sock);
655 [ - + ]: 6573 : if (rc != 0) {
656 : 0 : return rc;
657 : : }
658 : :
659 : 6573 : TAILQ_INSERT_TAIL(&group_impl->socks, sock, link);
660 : 6573 : sock->group_impl = group_impl;
661 : 6573 : sock->cb_fn = cb_fn;
662 : 6573 : sock->cb_arg = cb_arg;
663 : :
664 : 6573 : return 0;
665 : : }
666 : :
667 : : int
668 : 6573 : spdk_sock_group_remove_sock(struct spdk_sock_group *group, struct spdk_sock *sock)
669 : : {
670 : 6573 : struct spdk_sock_group_impl *group_impl = NULL;
671 : : int rc;
672 : :
673 : 6573 : group_impl = sock_get_group_impl_from_group(sock, group);
674 [ - + ]: 6573 : if (group_impl == NULL) {
675 : 0 : errno = EINVAL;
676 : 0 : return -1;
677 : : }
678 : :
679 [ - + ]: 6573 : assert(group_impl == sock->group_impl);
680 : :
681 : 6573 : rc = group_impl->net_impl->group_impl_remove_sock(group_impl, sock);
682 [ + - ]: 6573 : if (rc == 0) {
683 [ + + ]: 6573 : TAILQ_REMOVE(&group_impl->socks, sock, link);
684 : 6573 : sock->group_impl = NULL;
685 : 6573 : sock->cb_fn = NULL;
686 : 6573 : sock->cb_arg = NULL;
687 : : }
688 : :
689 : 6573 : return rc;
690 : : }
691 : :
692 : : int
693 : 40 : spdk_sock_group_provide_buf(struct spdk_sock_group *group, void *buf, size_t len, void *ctx)
694 : : {
695 : : struct spdk_sock_group_provided_buf *provided;
696 : :
697 : 40 : provided = (struct spdk_sock_group_provided_buf *)buf;
698 : :
699 : 40 : provided->len = len;
700 : 40 : provided->ctx = ctx;
701 [ + - ]: 40 : STAILQ_INSERT_HEAD(&group->pool, provided, link);
702 : :
703 : 40 : return 0;
704 : : }
705 : :
706 : : size_t
707 : 36 : spdk_sock_group_get_buf(struct spdk_sock_group *group, void **buf, void **ctx)
708 : : {
709 : : struct spdk_sock_group_provided_buf *provided;
710 : :
711 : 36 : provided = STAILQ_FIRST(&group->pool);
712 [ - + ]: 36 : if (provided == NULL) {
713 : 0 : *buf = NULL;
714 : 0 : return 0;
715 : : }
716 [ + - ]: 36 : STAILQ_REMOVE_HEAD(&group->pool, link);
717 : :
718 : 36 : *buf = provided;
719 : 36 : *ctx = provided->ctx;
720 : 36 : return provided->len;
721 : : }
722 : :
723 : : int
724 : 539469213 : spdk_sock_group_poll(struct spdk_sock_group *group)
725 : : {
726 : 539469213 : return spdk_sock_group_poll_count(group, MAX_EVENTS_PER_POLL);
727 : : }
728 : :
729 : : static int
730 : 1160978894 : sock_group_impl_poll_count(struct spdk_sock_group_impl *group_impl,
731 : : struct spdk_sock_group *group,
732 : : int max_events)
733 : : {
734 : 54003354 : struct spdk_sock *socks[MAX_EVENTS_PER_POLL];
735 : : int num_events, i;
736 : :
737 [ + + ]: 1160978894 : if (TAILQ_EMPTY(&group_impl->socks)) {
738 : 644119967 : return 0;
739 : : }
740 : :
741 : 516858927 : num_events = group_impl->net_impl->group_impl_poll(group_impl, max_events, socks);
742 [ - + ]: 516858927 : if (num_events == -1) {
743 : 0 : return -1;
744 : : }
745 : :
746 [ + + ]: 616276900 : for (i = 0; i < num_events; i++) {
747 : 99417973 : struct spdk_sock *sock = socks[i];
748 [ - + ]: 99417973 : assert(sock->cb_fn != NULL);
749 : 99417973 : sock->cb_fn(sock->cb_arg, group, sock);
750 : : }
751 : :
752 : 516858927 : return num_events;
753 : : }
754 : :
755 : : int
756 : 539469229 : spdk_sock_group_poll_count(struct spdk_sock_group *group, int max_events)
757 : : {
758 : 539469229 : struct spdk_sock_group_impl *group_impl = NULL;
759 : 539469229 : int rc, num_events = 0;
760 : :
761 [ - + ]: 539469229 : if (max_events < 1) {
762 : 0 : errno = -EINVAL;
763 : 0 : return -1;
764 : : }
765 : :
766 : : /*
767 : : * Only poll for up to 32 events at a time - if more events are pending,
768 : : * the next call to this function will reap them.
769 : : */
770 [ - + ]: 539469229 : if (max_events > MAX_EVENTS_PER_POLL) {
771 : 0 : max_events = MAX_EVENTS_PER_POLL;
772 : : }
773 : :
774 [ + - + + ]: 1700448123 : STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
775 : 1160978894 : rc = sock_group_impl_poll_count(group_impl, group, max_events);
776 [ - + ]: 1160978894 : if (rc < 0) {
777 : 0 : num_events = -1;
778 : 0 : SPDK_ERRLOG("group_impl_poll_count for net(%s) failed\n",
779 : : group_impl->net_impl->name);
780 [ + - ]: 1160978894 : } else if (num_events >= 0) {
781 : 1160978894 : num_events += rc;
782 : : }
783 : : }
784 : :
785 : 539469229 : return num_events;
786 : : }
787 : :
788 : : int
789 : 2144 : spdk_sock_group_close(struct spdk_sock_group **group)
790 : : {
791 : 2144 : struct spdk_sock_group_impl *group_impl = NULL, *tmp;
792 : : int rc;
793 : :
794 [ + + ]: 2144 : if (*group == NULL) {
795 : 16 : errno = EBADF;
796 : 16 : return -1;
797 : : }
798 : :
799 [ + + ]: 6803 : STAILQ_FOREACH_SAFE(group_impl, &(*group)->group_impls, link, tmp) {
800 [ + + ]: 4683 : if (!TAILQ_EMPTY(&group_impl->socks)) {
801 : 8 : errno = EBUSY;
802 : 8 : return -1;
803 : : }
804 : : }
805 : :
806 [ + + ]: 6787 : STAILQ_FOREACH_SAFE(group_impl, &(*group)->group_impls, link, tmp) {
807 : 4667 : rc = group_impl->net_impl->group_impl_close(group_impl);
808 [ - + ]: 4667 : if (rc != 0) {
809 : 0 : SPDK_ERRLOG("group_impl_close for net failed\n");
810 : : }
811 : : }
812 : :
813 : 2120 : free(*group);
814 : 2120 : *group = NULL;
815 : :
816 : 2120 : return 0;
817 : : }
818 : :
819 : : static inline struct spdk_net_impl *
820 : 3678 : sock_get_impl_by_name(const char *impl_name)
821 : : {
822 : : struct spdk_net_impl *impl;
823 : :
824 [ - + ]: 3678 : assert(impl_name != NULL);
825 [ + - ]: 4661 : STAILQ_FOREACH(impl, &g_net_impls, link) {
826 [ + + - + : 4661 : if (0 == strcmp(impl_name, impl->name)) {
+ + ]
827 : 3678 : return impl;
828 : : }
829 : : }
830 : :
831 : 0 : return NULL;
832 : : }
833 : :
834 : : int
835 : 410 : spdk_sock_impl_get_opts(const char *impl_name, struct spdk_sock_impl_opts *opts, size_t *len)
836 : : {
837 : : struct spdk_net_impl *impl;
838 : :
839 [ + - + + : 410 : if (!impl_name || !opts || !len) {
+ + ]
840 : 16 : errno = EINVAL;
841 : 16 : return -1;
842 : : }
843 : :
844 : 394 : impl = sock_get_impl_by_name(impl_name);
845 [ - + ]: 394 : if (!impl) {
846 : 0 : errno = EINVAL;
847 : 0 : return -1;
848 : : }
849 : :
850 [ + + ]: 394 : if (!impl->get_opts) {
851 : 4 : errno = ENOTSUP;
852 : 4 : return -1;
853 : : }
854 : :
855 : 390 : return impl->get_opts(opts, len);
856 : : }
857 : :
858 : : int
859 : 165 : spdk_sock_impl_set_opts(const char *impl_name, const struct spdk_sock_impl_opts *opts, size_t len)
860 : : {
861 : : struct spdk_net_impl *impl;
862 : :
863 [ + - + + ]: 165 : if (!impl_name || !opts) {
864 : 8 : errno = EINVAL;
865 : 8 : return -1;
866 : : }
867 : :
868 : 157 : impl = sock_get_impl_by_name(impl_name);
869 [ - + ]: 157 : if (!impl) {
870 : 0 : errno = EINVAL;
871 : 0 : return -1;
872 : : }
873 : :
874 [ + + ]: 157 : if (!impl->set_opts) {
875 : 4 : errno = ENOTSUP;
876 : 4 : return -1;
877 : : }
878 : :
879 : 153 : return impl->set_opts(opts, len);
880 : : }
881 : :
882 : : void
883 : 124 : spdk_sock_write_config_json(struct spdk_json_write_ctx *w)
884 : : {
885 : : struct spdk_net_impl *impl;
886 : 46 : struct spdk_sock_impl_opts opts;
887 : 46 : size_t len;
888 : :
889 [ - + ]: 124 : assert(w != NULL);
890 : :
891 : 124 : spdk_json_write_array_begin(w);
892 : :
893 [ + - ]: 124 : if (g_default_impl) {
894 : 124 : spdk_json_write_object_begin(w);
895 : 124 : spdk_json_write_named_string(w, "method", "sock_set_default_impl");
896 : 124 : spdk_json_write_named_object_begin(w, "params");
897 : 124 : spdk_json_write_named_string(w, "impl_name", g_default_impl->name);
898 : 124 : spdk_json_write_object_end(w);
899 : 124 : spdk_json_write_object_end(w);
900 : : }
901 : :
902 [ + + ]: 389 : STAILQ_FOREACH(impl, &g_net_impls, link) {
903 [ - + ]: 265 : if (!impl->get_opts) {
904 : 0 : continue;
905 : : }
906 : :
907 : 265 : len = sizeof(opts);
908 [ + - ]: 265 : if (impl->get_opts(&opts, &len) == 0) {
909 : 265 : spdk_json_write_object_begin(w);
910 : 265 : spdk_json_write_named_string(w, "method", "sock_impl_set_options");
911 : 265 : spdk_json_write_named_object_begin(w, "params");
912 : 265 : spdk_json_write_named_string(w, "impl_name", impl->name);
913 : 265 : spdk_json_write_named_uint32(w, "recv_buf_size", opts.recv_buf_size);
914 : 265 : spdk_json_write_named_uint32(w, "send_buf_size", opts.send_buf_size);
915 [ - + ]: 265 : spdk_json_write_named_bool(w, "enable_recv_pipe", opts.enable_recv_pipe);
916 [ - + ]: 265 : spdk_json_write_named_bool(w, "enable_quickack", opts.enable_quickack);
917 : 265 : spdk_json_write_named_uint32(w, "enable_placement_id", opts.enable_placement_id);
918 [ - + ]: 265 : spdk_json_write_named_bool(w, "enable_zerocopy_send_server", opts.enable_zerocopy_send_server);
919 [ - + ]: 265 : spdk_json_write_named_bool(w, "enable_zerocopy_send_client", opts.enable_zerocopy_send_client);
920 : 265 : spdk_json_write_named_uint32(w, "zerocopy_threshold", opts.zerocopy_threshold);
921 : 265 : spdk_json_write_named_uint32(w, "tls_version", opts.tls_version);
922 [ - + ]: 265 : spdk_json_write_named_bool(w, "enable_ktls", opts.enable_ktls);
923 : 265 : spdk_json_write_object_end(w);
924 : 265 : spdk_json_write_object_end(w);
925 : : } else {
926 : 0 : SPDK_ERRLOG("Failed to get socket options for socket implementation %s\n", impl->name);
927 : : }
928 : : }
929 : :
930 : 124 : spdk_json_write_array_end(w);
931 : 124 : }
932 : :
933 : : void
934 : 5861 : spdk_net_impl_register(struct spdk_net_impl *impl)
935 : : {
936 [ + + ]: 5861 : STAILQ_INSERT_HEAD(&g_net_impls, impl, link);
937 : 5861 : }
938 : :
939 : : int
940 : 3135 : spdk_sock_set_default_impl(const char *impl_name)
941 : : {
942 : : struct spdk_net_impl *impl;
943 : :
944 [ + + ]: 3135 : if (!impl_name) {
945 : 8 : errno = EINVAL;
946 : 8 : return -1;
947 : : }
948 : :
949 : 3127 : impl = sock_get_impl_by_name(impl_name);
950 [ - + ]: 3127 : if (!impl) {
951 : 0 : errno = EINVAL;
952 : 0 : return -1;
953 : : }
954 : :
955 [ + + ]: 3127 : if (impl == g_default_impl) {
956 : 64 : return 0;
957 : : }
958 : :
959 [ + + ]: 3063 : if (g_default_impl) {
960 [ - + - + ]: 324 : SPDK_DEBUGLOG(sock, "Change the default sock impl from %s to %s\n", g_default_impl->name,
961 : : impl->name);
962 : : } else {
963 [ - + - + ]: 2739 : SPDK_DEBUGLOG(sock, "Set default sock implementation to %s\n", impl_name);
964 : : }
965 : :
966 : 3063 : g_default_impl = impl;
967 : :
968 : 3063 : return 0;
969 : : }
970 : :
971 : : const char *
972 : 2 : spdk_sock_get_default_impl(void)
973 : : {
974 [ + - ]: 2 : if (g_default_impl) {
975 : 2 : return g_default_impl->name;
976 : : }
977 : :
978 : 0 : return NULL;
979 : : }
980 : :
981 : : int
982 : 0 : spdk_sock_group_register_interrupt(struct spdk_sock_group *group, uint32_t events,
983 : : spdk_interrupt_fn fn,
984 : : void *arg, const char *name)
985 : : {
986 : 0 : struct spdk_sock_group_impl *group_impl = NULL;
987 : : int rc;
988 : :
989 [ # # ]: 0 : assert(group != NULL);
990 [ # # ]: 0 : assert(fn != NULL);
991 : :
992 [ # # # # ]: 0 : STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
993 : 0 : rc = group_impl->net_impl->group_impl_register_interrupt(group_impl, events, fn, arg, name);
994 [ # # ]: 0 : if (rc != 0) {
995 : 0 : return rc;
996 : : }
997 : : }
998 : :
999 : 0 : return 0;
1000 : : }
1001 : :
1002 : : void
1003 : 0 : spdk_sock_group_unregister_interrupt(struct spdk_sock_group *group)
1004 : : {
1005 : 0 : struct spdk_sock_group_impl *group_impl = NULL;
1006 : :
1007 [ # # ]: 0 : assert(group != NULL);
1008 : :
1009 [ # # # # ]: 0 : STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
1010 : 0 : group_impl->net_impl->group_impl_unregister_interrupt(group_impl);
1011 : : }
1012 : 0 : }
1013 : :
1014 : 2756 : SPDK_LOG_REGISTER_COMPONENT(sock)
1015 : :
1016 : 5030 : SPDK_TRACE_REGISTER_FN(sock_trace, "sock", TRACE_GROUP_SOCK)
1017 : : {
1018 : 2274 : struct spdk_trace_tpoint_opts opts[] = {
1019 : : {
1020 : : "SOCK_REQ_QUEUE", TRACE_SOCK_REQ_QUEUE,
1021 : : OWNER_TYPE_SOCK, OBJECT_SOCK_REQ, 1,
1022 : : {
1023 : : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
1024 : : }
1025 : : },
1026 : : {
1027 : : "SOCK_REQ_PEND", TRACE_SOCK_REQ_PEND,
1028 : : OWNER_TYPE_SOCK, OBJECT_SOCK_REQ, 0,
1029 : : {
1030 : : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
1031 : : }
1032 : : },
1033 : : {
1034 : : "SOCK_REQ_COMPLETE", TRACE_SOCK_REQ_COMPLETE,
1035 : : OWNER_TYPE_SOCK, OBJECT_SOCK_REQ, 0,
1036 : : {
1037 : : { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
1038 : : }
1039 : : },
1040 : : };
1041 : :
1042 : 2274 : spdk_trace_register_owner_type(OWNER_TYPE_SOCK, 's');
1043 : 2274 : spdk_trace_register_object(OBJECT_SOCK_REQ, 's');
1044 : 2274 : spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
1045 : 2274 : }
|