From 2f9271b818599159c6252e9b0a2419e41d6dec6b Mon Sep 17 00:00:00 2001 From: Ben Walker Date: Thu, 1 Apr 2021 15:03:08 -0700 Subject: [PATCH] sock: Add sock_map_find_free This function finds a placement_id that does not have a group associated with it. Change-Id: I1306690e980fd4661f46dba9fb283f048a962eba Signed-off-by: Ben Walker Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/7223 Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Aleksey Marchuk Reviewed-by: Shuhei Matsumoto --- include/spdk_internal/sock.h | 5 +++++ lib/sock/sock.c | 19 +++++++++++++++++++ lib/sock/spdk_sock.map | 1 + test/unit/lib/sock/posix.c/posix_ut.c | 1 + test/unit/lib/sock/uring.c/uring_ut.c | 1 + 5 files changed, 27 insertions(+) diff --git a/include/spdk_internal/sock.h b/include/spdk_internal/sock.h index b074366fc..138805ac4 100644 --- a/include/spdk_internal/sock.h +++ b/include/spdk_internal/sock.h @@ -324,6 +324,11 @@ void spdk_sock_map_release(struct spdk_sock_map *map, int placement_id); int spdk_sock_map_lookup(struct spdk_sock_map *map, int placement_id, struct spdk_sock_group_impl **group_impl); +/** + * Find a placement id with no associated group + */ +int spdk_sock_map_find_free(struct spdk_sock_map *map); + /** * Clean up all memory associated with the given map */ diff --git a/lib/sock/sock.c b/lib/sock/sock.c index e72330410..1438f12d8 100644 --- a/lib/sock/sock.c +++ b/lib/sock/sock.c @@ -155,6 +155,25 @@ spdk_sock_map_cleanup(struct spdk_sock_map *map) pthread_mutex_unlock(&map->mtx); } +int +spdk_sock_map_find_free(struct spdk_sock_map *map) +{ + struct spdk_sock_placement_id_entry *entry; + int placement_id = -1; + + pthread_mutex_lock(&map->mtx); + STAILQ_FOREACH(entry, &map->entries, link) { + if (entry->group == NULL) { + placement_id = entry->placement_id; + break; + } + } + + pthread_mutex_unlock(&map->mtx); + + return placement_id; +} + int spdk_sock_get_optimal_sock_group(struct spdk_sock *sock, struct spdk_sock_group **group) { diff --git a/lib/sock/spdk_sock.map b/lib/sock/spdk_sock.map index 64fa372c9..65b98eb4f 100644 --- a/lib/sock/spdk_sock.map +++ b/lib/sock/spdk_sock.map @@ -46,6 +46,7 @@ spdk_sock_map_insert; spdk_sock_map_release; spdk_sock_map_lookup; + spdk_sock_map_find_free; spdk_sock_map_cleanup; local: *; diff --git a/test/unit/lib/sock/posix.c/posix_ut.c b/test/unit/lib/sock/posix.c/posix_ut.c index d83482795..da20cdbd8 100644 --- a/test/unit/lib/sock/posix.c/posix_ut.c +++ b/test/unit/lib/sock/posix.c/posix_ut.c @@ -46,6 +46,7 @@ DEFINE_STUB(spdk_sock_map_insert, int, (struct spdk_sock_map *map, int placement DEFINE_STUB_V(spdk_sock_map_release, (struct spdk_sock_map *map, int placement_id)); DEFINE_STUB(spdk_sock_map_lookup, int, (struct spdk_sock_map *map, int placement_id, struct spdk_sock_group_impl **group), 0); +DEFINE_STUB(spdk_sock_map_find_free, int, (struct spdk_sock_map *map), -1); DEFINE_STUB_V(spdk_sock_map_cleanup, (struct spdk_sock_map *map)); DEFINE_STUB_V(spdk_net_impl_register, (struct spdk_net_impl *impl, int priority)); diff --git a/test/unit/lib/sock/uring.c/uring_ut.c b/test/unit/lib/sock/uring.c/uring_ut.c index 6454eb91d..c8d43459e 100644 --- a/test/unit/lib/sock/uring.c/uring_ut.c +++ b/test/unit/lib/sock/uring.c/uring_ut.c @@ -46,6 +46,7 @@ DEFINE_STUB(spdk_sock_map_insert, int, (struct spdk_sock_map *map, int placement DEFINE_STUB_V(spdk_sock_map_release, (struct spdk_sock_map *map, int placement_id)); DEFINE_STUB(spdk_sock_map_lookup, int, (struct spdk_sock_map *map, int placement_id, struct spdk_sock_group_impl **group), 0); +DEFINE_STUB(spdk_sock_map_find_free, int, (struct spdk_sock_map *map), -1); DEFINE_STUB_V(spdk_sock_map_cleanup, (struct spdk_sock_map *map)); DEFINE_STUB_V(spdk_net_impl_register, (struct spdk_net_impl *impl, int priority));