summaryrefslogtreecommitdiffstats
path: root/src/plugins/memif/memif.c
diff options
context:
space:
mode:
authorNathan Skrzypczak <nathan.skrzypczak@gmail.com>2021-12-15 19:15:32 +0100
committerDamjan Marion <dmarion@0xa5.net>2023-03-06 13:54:06 +0000
commit8b213ee652dd69d941865fa59e1f780843016475 (patch)
treef37774b199588dcace4b1c4277b8398fc3cc0498 /src/plugins/memif/memif.c
parentec5c40b83acae400a8cc1a18ad897b6365774559 (diff)
memif: autogenerate socket_ids
This patch adds an API memif_socket_filename_add_del_v2 that allows autogenerating memif socket_id when passing ~0 in the socket_id field. It opportunistically walks the hash to find a free ID to use, and returns it in the reply. socket_filename also becomes a variable length string, to accomodate for longer names (in case a netns gets passed) Type: feature Change-Id: I33fc3e1cf553af27579d6bad8691b22b530531cc Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
Diffstat (limited to 'src/plugins/memif/memif.c')
-rw-r--r--src/plugins/memif/memif.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/plugins/memif/memif.c b/src/plugins/memif/memif.c
index eee38f09a5b..c9d2f008cca 100644
--- a/src/plugins/memif/memif.c
+++ b/src/plugins/memif/memif.c
@@ -681,6 +681,36 @@ VLIB_REGISTER_NODE (memif_process_node,static) = {
.name = "memif-process",
};
+/*
+ * Returns an unused socket id, and ~0 if it can't find one.
+ */
+u32
+memif_get_unused_socket_id ()
+{
+ memif_main_t *mm = &memif_main;
+ uword *p;
+ int i, j;
+
+ static u32 seed = 0;
+ /* limit to 1M tries */
+ for (j = 0; j < 1 << 10; j++)
+ {
+ seed = random_u32 (&seed);
+ for (i = 0; i < 1 << 10; i++)
+ {
+ /* look around randomly generated id */
+ seed += (2 * (i % 2) - 1) * i;
+ if (seed == (u32) ~0)
+ continue;
+ p = hash_get (mm->socket_file_index_by_sock_id, seed);
+ if (!p)
+ return seed;
+ }
+ }
+
+ return ~0;
+}
+
clib_error_t *
memif_socket_filename_add_del (u8 is_add, u32 sock_id, char *sock_filename)
{