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