aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakub Grajciar <jgrajcia@cisco.com>2019-07-31 14:40:52 +0200
committerDamjan Marion <dmarion@me.com>2019-08-21 14:45:43 +0000
commit17f2a7bbf25f54dbd71aa8f377875828b7b88e35 (patch)
treeab88f5d0759bfe703c818a40ea46d0e52b28783d
parentb6103105f99e0c7f210a9596f205a1efd21b626f (diff)
libmemif: introduce 'memif_per_thread_' namespace
APIs in 'memif_per_thread_' namespace are used to split the global database into separate databases, to improve multi-thread use cases. Using 'memif_per_thread_init' client can create separate libmemif databases (libmemif_main_t). Client will reference these databases using memif_per_thread_handle_t. Each database requires unique socket. Created interface will be stored in the same database as the socket passed in connection arguments. Example code: extras/libmemif/examples/icmp_responder_3-1/main.c Type: feature Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com> Change-Id: I261563ecc34761a76e94f20c20015394398ddfd7 Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
-rw-r--r--extras/libmemif/examples/CMakeLists.txt2
-rw-r--r--extras/libmemif/examples/icmp_responder-mt_3-1/main.c549
-rw-r--r--extras/libmemif/src/libmemif.h136
-rw-r--r--extras/libmemif/src/main.c663
-rw-r--r--extras/libmemif/src/memif_private.h15
-rw-r--r--extras/libmemif/src/socket.c50
-rw-r--r--extras/libmemif/src/socket.h4
-rw-r--r--extras/libmemif/test/main_test.c2
-rw-r--r--extras/libmemif/test/socket_test.c6
9 files changed, 1314 insertions, 113 deletions
diff --git a/extras/libmemif/examples/CMakeLists.txt b/extras/libmemif/examples/CMakeLists.txt
index 52b8f54216d..1cf50002b8a 100644
--- a/extras/libmemif/examples/CMakeLists.txt
+++ b/extras/libmemif/examples/CMakeLists.txt
@@ -22,6 +22,7 @@ list(APPEND EXAMPLES_LIST
icmp_responder/main.c
icmp_responder-epoll/main.c
icmp_responder-mt/main.c
+ icmp_responder-mt_3-1/main.c
icmp_responder-eb/main.c
icmp_responder-zero-copy-slave/main.c
)
@@ -33,4 +34,3 @@ foreach (EXAMPLE_SRC ${EXAMPLES_LIST})
target_include_directories(${EXECUTABLE} PRIVATE $<BUILD_INTERFACE:${HEADERS_DIR}>)
target_link_libraries(${EXECUTABLE} ${LIBMEMIF} ${CMAKE_THREAD_LIBS_INIT})
endforeach()
-
diff --git a/extras/libmemif/examples/icmp_responder-mt_3-1/main.c b/extras/libmemif/examples/icmp_responder-mt_3-1/main.c
new file mode 100644
index 00000000000..a7b11bb1ebc
--- /dev/null
+++ b/extras/libmemif/examples/icmp_responder-mt_3-1/main.c
@@ -0,0 +1,549 @@
+/*
+ *------------------------------------------------------------------
+ * Copyright (c) 2019 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *------------------------------------------------------------------
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#include <sys/epoll.h>
+#include <sys/eventfd.h>
+
+#include <libmemif.h>
+#include <icmp_proto.h>
+
+
+#define APP_NAME "ICMP_Responder_mt_v3.1"
+#define IF_NAME "memif_connection"
+
+#ifdef ICMP_DBG
+#define DBG(...) do { \
+ printf (APP_NAME":%s:%d: ", __func__, __LINE__); \
+ printf (__VA_ARGS__); \
+ printf ("\n"); \
+ } while (0)
+#else
+#define DBG(...)
+#endif
+
+#define ICMPR_BUFFER_LENGTH 32
+#define ICMPR_SOCKET_FILENAME_LEN 256
+#define ICMPR_MEMIF_BUFFER_NUM 256
+
+static struct option options[] = {
+ {"threads", required_argument, 0, 't'},
+ {"if_num", required_argument, 0, 'i'}
+};
+
+struct memif_connection
+{
+ uint16_t id; /* unique interface id */
+ bool connected; /* is connected */
+ struct per_thread_data *ptd; /* per thread data */
+ memif_conn_handle_t handle; /* memif connection handle */
+ uint8_t ip_addr[4]; /* ip4 address */
+};
+
+struct per_thread_data
+{
+ bool running; /* is thread main loop running */
+ uint8_t index; /* thread index */
+ int epfd; /* epoll file descriptor */
+ int pcfd; /* poll cancel file descriptor */
+ uint16_t if_num; /* number of interfaces on this thread */
+ struct memif_connection *conns; /* memif connections pool */
+ memif_per_thread_main_handle_t pt_main; /* memif per thread main handle */
+ memif_socket_handle_t socket_handle; /* memif socket handle */
+};
+
+struct icmpr_main
+{
+ uint8_t threads; /* number of threads */
+ uint16_t per_thread_if_num; /* number of interfaces per thread */
+ struct per_thread_data *ptd; /* per thread data pool */
+ pthread_t *pthread; /* thread pool */
+};
+
+struct icmpr_main icmpr_main;
+
+int
+add_epoll_fd (int epfd, int fd, uint32_t events)
+{
+ if (fd < 0)
+ {
+ DBG ("invalid fd %d", fd);
+ return -1;
+ }
+ struct epoll_event evt;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = events;
+ evt.data.fd = fd;
+ if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
+ {
+ DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
+ return -1;
+ }
+ DBG ("fd %d added to epoll", fd);
+ return 0;
+}
+
+int
+mod_epoll_fd (int epfd, int fd, uint32_t events)
+{
+ if (fd < 0)
+ {
+ DBG ("invalid fd %d", fd);
+ return -1;
+ }
+ struct epoll_event evt;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = events;
+ evt.data.fd = fd;
+ if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
+ {
+ DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
+ return -1;
+ }
+ DBG ("fd %d moddified on epoll", fd);
+ return 0;
+}
+
+int
+del_epoll_fd (int epfd, int fd)
+{
+ if (fd < 0)
+ {
+ DBG ("invalid fd %d", fd);
+ return -1;
+ }
+ struct epoll_event evt;
+ memset (&evt, 0, sizeof (evt));
+ if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
+ {
+ DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
+ return -1;
+ }
+ DBG ("fd %d removed from epoll", fd);
+ return 0;
+}
+
+/* Called when libmemif requests an update on any of its file descriptors */
+static int
+control_fd_update (int fd, uint8_t events, void *private_ctx)
+{
+ struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
+ uint32_t evt = 0;
+
+ if (ptd == NULL)
+ return -1;
+
+ /* convert memif event definitions to epoll events */
+ if (events & MEMIF_FD_EVENT_DEL)
+ return del_epoll_fd (ptd->epfd, fd);
+
+ if (events & MEMIF_FD_EVENT_READ)
+ evt |= EPOLLIN;
+ if (events & MEMIF_FD_EVENT_WRITE)
+ evt |= EPOLLOUT;
+
+ if (events & MEMIF_FD_EVENT_MOD)
+ return mod_epoll_fd (ptd->epfd, fd, evt);
+
+ return add_epoll_fd (ptd->epfd, fd, evt);
+}
+
+static int
+on_connect (memif_conn_handle_t conn, void *private_ctx)
+{
+ struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
+ struct memif_connection *c;
+ int i = 0;
+
+ while (i < ptd->if_num && ptd->conns[i].handle != conn)
+ i++;
+ c = &ptd->conns[i];
+
+ c->connected = true;
+ DBG ("Connected: %u", c->id);
+
+ memif_refill_queue (conn, 0, -1, 0);
+
+ return 0;
+}
+
+static int
+on_disconnect (memif_conn_handle_t conn, void *private_ctx)
+{
+ struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
+ struct memif_connection *c;
+ int i = 0;
+
+ while (i < ptd->if_num && ptd->conns[i].handle != conn)
+ i++;
+ c = &ptd->conns[i];
+
+ c->connected = false;
+ DBG ("Disconnected: %u", c->id);
+
+ return 0;
+}
+
+static int
+on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid)
+{
+ struct per_thread_data *ptd = (struct per_thread_data *) private_ctx;
+ struct memif_connection *c;
+ memif_buffer_t mbufs[ICMPR_MEMIF_BUFFER_NUM];
+ uint16_t rx = 0;
+ uint16_t tx = 0;
+ uint16_t ret;
+ memif_err_t err;
+ int i = 0;
+
+ memset (mbufs, 0, sizeof (memif_buffer_t) * ICMPR_MEMIF_BUFFER_NUM);
+
+ while (i < ptd->if_num && ptd->conns[i].handle != conn)
+ i++;
+ c = &ptd->conns[i];
+
+ /* receive data from shared memory buffers */
+ err = memif_rx_burst (conn, qid, mbufs, ICMPR_MEMIF_BUFFER_NUM, &rx);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ printf ("memif_rx_burst: %s\n", memif_strerror (err));
+ goto error;
+ }
+
+ /* resolve packet in place (zer-copy slave) */
+ for (i = 0; i < rx; i++)
+ resolve_packet2 (mbufs[i].data, &mbufs[i].len, c->ip_addr);
+
+ /* enqueue received buffers */
+ err = memif_buffer_enq_tx (conn, qid, mbufs, i, &tx);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ printf ("memif_rx_burst: %s\n", memif_strerror (err));
+ goto error;
+ }
+
+ /* mark shared memory buffers as free */
+ err = memif_refill_queue (conn, qid, rx, 0);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ printf ("memif_rx_burst: %s\n", memif_strerror (err));
+ goto error;
+ }
+
+ err = memif_tx_burst (conn, qid, mbufs, tx, &ret);
+ if (err != MEMIF_ERR_SUCCESS)
+ {
+ printf ("memif_rx_burst: %s\n", memif_strerror (err));
+ goto error;
+ }
+
+ return 0;
+
+error:
+ memif_refill_queue (conn, qid, -1, 0);
+ return -1;
+}
+
+int
+poll_event (memif_per_thread_main_handle_t pt_main, int pcfd, int epfd,
+ int timeout)
+{
+ struct epoll_event evt;
+ int en = 0;
+ uint8_t events = 0;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = EPOLLIN | EPOLLOUT;
+
+ en = epoll_pwait (epfd, &evt, 1, timeout, NULL);
+ if (en < 0)
+ {
+ printf ("epoll_pwait: %s\n", strerror (errno));
+ return -1;
+ }
+
+ if (en > 0)
+ {
+ /* Cancel event polling */
+ if (evt.data.fd == pcfd)
+ return 1;
+
+ if (evt.events & EPOLLIN)
+ events |= MEMIF_FD_EVENT_READ;
+ if (evt.events & EPOLLOUT)
+ events |= MEMIF_FD_EVENT_WRITE;
+ if (evt.events & EPOLLERR)
+ events |= MEMIF_FD_EVENT_ERROR;
+
+ /* No need to use locks, as the database is separated */
+ memif_per_thread_control_fd_handler (pt_main, evt.data.fd, events);
+ }
+
+ return 0;
+}
+
+static void *
+icmpr_thread_fn (void *data)
+{
+ struct per_thread_data *ptd = (struct per_thread_data *) data;
+ int rv;
+ uint16_t i;
+ char socket_filename[ICMPR_SOCKET_FILENAME_LEN] = "/run/vpp/memif";
+ memif_conn_args_t args;
+
+ ptd->epfd = epoll_create (1);
+
+ ptd->conns = malloc (sizeof (struct memif_connection) * ptd->if_num);
+ if (ptd->conns == NULL)
+ {
+ printf ("%s\n", strerror (errno));
+ return NULL;
+ }
+
+ memset (ptd->conns, 0, sizeof (struct memif_connection) * ptd->if_num);
+
+ /* Initialize memif database (per thread). */
+ rv =
+ memif_per_thread_init (&ptd->pt_main, ptd, control_fd_update, APP_NAME,
+ NULL, NULL, NULL);
+ if (rv != MEMIF_ERR_SUCCESS)
+ {
+ printf ("memif_per_thread_init: %s\n", memif_strerror (rv));
+ return NULL;
+ }
+
+ /* Create unique socket. Each thread requires uniqueue socket. Interfaces created
+ * on the same thread can share one socket.
+ */
+ socket_filename[strlen (socket_filename)] = '0' + ptd->index;
+ strncpy (socket_filename + strlen (socket_filename), ".sock", 5);
+ DBG ("socket_filename: %s", socket_filename);
+
+ rv = memif_per_thread_create_socket (ptd->pt_main, &ptd->socket_handle,
+ socket_filename, ptd);
+ if (rv != MEMIF_ERR_SUCCESS)
+ {
+ printf ("memif_per_thread_create_socket: %s\n", memif_strerror (rv));
+ return NULL;
+ }
+
+ /* Create interfaces on this thread */
+ for (i = 0; i < ptd->if_num; i++)
+ {
+ ptd->conns[i].ip_addr[0] = 192;
+ ptd->conns[i].ip_addr[1] = 168;
+ ptd->conns[i].ip_addr[2] = ptd->index + 1;
+ ptd->conns[i].ip_addr[3] = i * 2 + 2;
+
+ memset (&args, 0, sizeof (args));
+
+ args.socket = ptd->socket_handle;
+ ptd->conns[i].id = i;
+ args.interface_id = i;
+
+ rv = memif_create (&ptd->conns[i].handle, &args, on_connect,
+ on_disconnect, on_interrupt, ptd);
+ if (rv < 0)
+ {
+ printf ("%s\n", memif_strerror (rv));
+ return NULL;
+ }
+ }
+
+ /* Poll cancel file descriptor. When an event is received on this fd, exit thread
+ * loop in respective thread.
+ */
+ ptd->pcfd = eventfd (0, EFD_NONBLOCK);
+ if (ptd->pcfd < 0)
+ {
+ printf ("eventfd: %s\n", strerror (errno));
+ return NULL;
+ }
+ if (add_epoll_fd (ptd->epfd, ptd->pcfd, EPOLLIN) < 0)
+ {
+ printf ("Failed to add poll cancel fd to epfd.");
+ return NULL;
+ }
+
+ /* Thread loop */
+ ptd->running = true;
+ while (ptd->running)
+ {
+ rv = poll_event (ptd->pt_main, ptd->pcfd, ptd->epfd, -1);
+ if (rv != 0)
+ ptd->running = false;
+ }
+
+ /* Clean up */
+ for (i = 0; i < ptd->if_num; i++)
+ memif_delete (&ptd->conns[i].handle);
+
+ memif_delete_socket (&ptd->socket_handle);
+
+ memif_per_thread_cleanup (&ptd->pt_main);
+
+ free (ptd->conns);
+ close (ptd->pcfd);
+
+ return NULL;
+}
+
+static void
+icmpr_print_help ()
+{
+ printf
+ ("exit - Exits the application.\nhelp - Print this help.\nshow - Show memif interfaces\n");
+}
+
+static void
+icmpr_show_memifs ()
+{
+ struct icmpr_main *im = &icmpr_main;
+ int i, j;
+ memif_socket_handle_t sh;
+
+ printf ("%u Threads %u Memifs (per thread)\n", im->threads,
+ im->per_thread_if_num);
+ printf ("=================================\n");
+
+ for (i = 0; i < im->threads; i++)
+ {
+ sh = im->ptd[i].socket_handle;
+ printf ("Thread %u %s\n", i, memif_get_socket_filename (sh));
+ for (j = 0; j < im->per_thread_if_num; j++)
+ {
+ printf ("\tMemif id %u\n\t%s\n", im->ptd[i].conns[j].id,
+ im->ptd[i].conns[j].connected ? "Link up" : "Link down");
+ }
+ }
+}
+
+int
+main (int argc, char **argv)
+{
+ struct icmpr_main *im = &icmpr_main;
+ int rv, i;
+ int option_index = 0;
+ bool running;
+ char buffer[ICMPR_BUFFER_LENGTH];
+ uint64_t b = 1;
+
+ memset (im, 0, sizeof (struct icmpr_main));
+
+ /* Default args */
+ im->threads = 4;
+ im->per_thread_if_num = 1;
+
+ /* Parse args */
+ while ((rv =
+ getopt_long (argc, argv, "t:i:", options, &option_index)) != (-1))
+ {
+ switch (rv)
+ {
+ case 't':
+ im->threads = strtoul (optarg, NULL, 10);
+ break;
+ case 'i':
+ im->per_thread_if_num = strtoul (optarg, NULL, 10);
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* Check args */
+ if (im->threads < 1)
+ {
+ printf ("threads < 1\n");
+ exit (EXIT_FAILURE);
+ }
+
+ if (im->per_thread_if_num < 1)
+ {
+ printf ("if_num < 1\n");
+ exit (EXIT_FAILURE);
+ }
+
+ /* Allocate memory */
+ im->ptd = malloc (sizeof (struct per_thread_data) * im->threads);
+ if (im->ptd == NULL)
+ {
+ printf ("%s\n", strerror (errno));
+ return -1;
+ }
+ im->pthread = malloc (sizeof (pthread_t) * im->threads);
+ if (im->pthread == NULL)
+ {
+ printf ("%s\n", strerror (errno));
+ return -1;
+ }
+
+ /* Initialize and create threads */
+ for (i = 0; i < im->threads; i++)
+ {
+ im->ptd[i].index = i;
+ im->ptd[i].if_num = im->per_thread_if_num;
+ pthread_create (&im->pthread[i], NULL, icmpr_thread_fn, &im->ptd[i]);
+ }
+
+ icmpr_print_help ();
+
+ /* Main loop */
+ running = true;
+ while (running)
+ {
+ printf ("cmd: ");
+ memset (buffer, 0, ICMPR_BUFFER_LENGTH);
+ if (fgets (buffer, ICMPR_BUFFER_LENGTH, stdin) != buffer)
+ {
+ printf ("%s\n", strerror (errno));
+ running = false;
+ }
+
+ if (strncmp (buffer, "exit", 4) == 0)
+ running = false;
+ else if (strncmp (buffer, "help", 4) == 0)
+ icmpr_print_help ();
+ else if (strncmp (buffer, "show", 4) == 0)
+ icmpr_show_memifs ();
+ }
+
+ for (i = 0; i < im->threads; i++)
+ {
+ /* Stop polling */
+ rv = write (im->ptd[i].pcfd, &b, sizeof (b));
+ if (rv < 0)
+ {
+ printf ("Failed to cancel polling. %s\n", strerror (errno));
+ exit (EXIT_FAILURE);
+ }
+ pthread_join (im->pthread[i], NULL);
+ }
+
+ free (im->pthread);
+ free (im->ptd);
+
+ return 0;
+}
diff --git a/extras/libmemif/src/libmemif.h b/extras/libmemif/src/libmemif.h
index cdbf04aea0d..609b98a7ba4 100644
--- a/extras/libmemif/src/libmemif.h
+++ b/extras/libmemif/src/libmemif.h
@@ -23,7 +23,7 @@
#define _LIBMEMIF_H_
/** Libmemif version. */
-#define LIBMEMIF_VERSION "3.0"
+#define LIBMEMIF_VERSION "3.1"
/** Default name of application using libmemif. */
#define MEMIF_DEFAULT_APP_NAME "libmemif-app"
@@ -96,6 +96,12 @@ typedef enum
#define MEMIF_FD_EVENT_MOD (1 << 4)
/** @} */
+/** \brief Memif per thread main handle
+ Pointer of type void, pointing to internal structure.
+ Used to identify internal per thread database.
+*/
+typedef void *memif_per_thread_main_handle_t;
+
/** \brief Memif connection handle
pointer of type void, pointing to internal structure
*/
@@ -227,6 +233,24 @@ void memif_register_external_region (memif_add_external_region_t * ar,
memif_del_external_region_t * dr,
memif_get_external_buffer_offset_t * go);
+/** \brief Register external region
+ @param pt_main - per thread main handle
+ @param ar - add external region callback
+ @param gr - get external region addr callback
+ @param dr - delete external region callback
+ @param go - get external buffer offset callback (optional)
+*/
+void memif_per_thread_register_external_region (memif_per_thread_main_handle_t
+ pt_main,
+ memif_add_external_region_t *
+ ar,
+ memif_get_external_region_addr_t
+ * gr,
+ memif_del_external_region_t *
+ dr,
+ memif_get_external_buffer_offset_t
+ * go);
+
/** @} */
/**
@@ -246,7 +270,11 @@ typedef enum
#endif /* _MEMIF_H_ */
/** \brief Memif connection arguments
- @param socket - memif socket handle, if NULL default socket will be used
+ @param socket - Memif socket handle, if NULL default socket will be used.
+ Default socket is only supported in global database (see memif_init).
+ Custom database does not create a default socket
+ (see memif_per_thread_init).
+ Memif connection is stored in the same database as the socket.
@param secret - otional parameter used as interface autenthication
@param num_s2m_rings - number of slave to master rings
@param num_m2s_rings - number of master to slave rings
@@ -465,6 +493,28 @@ int memif_init (memif_control_fd_update_t * on_control_fd_update,
char *app_name, memif_alloc_t * memif_alloc,
memif_realloc_t * memif_realloc, memif_free_t * memif_free);
+/** \brief Memif per thread initialization
+ @param pt_main - per thread main handle
+ @param private_ctx - private context
+ @param on_control_fd_update - if control fd updates inform user to watch new fd
+ @param app_name - application name (will be truncated to 32 chars)
+ @param memif_alloc - cutom memory allocator, NULL = default
+ @param memif_realloc - custom memory reallocation, NULL = default
+ @param memif_free - custom memory free, NULL = default
+
+ Per thread version of memif_init ().
+ Instead of using global database, creates and initializes unique database,
+ identified by 'memif_per_thread_main_handle_t'.
+
+ \return memif_err_t
+*/
+int memif_per_thread_init (memif_per_thread_main_handle_t * pt_main,
+ void *private_ctx,
+ memif_control_fd_update_t * on_control_fd_update,
+ char *app_name, memif_alloc_t * memif_alloc,
+ memif_realloc_t * memif_realloc,
+ memif_free_t * memif_free);
+
/** \brief Memif cleanup
Free libmemif internal allocations.
@@ -473,6 +523,15 @@ int memif_init (memif_control_fd_update_t * on_control_fd_update,
*/
int memif_cleanup ();
+/** \brief Memif per thread cleanup
+ @param pt_main - per thread main handle
+
+ Free libmemif internal allocations and sets the handle to NULL.
+
+ \return memif_err_t
+*/
+int memif_per_thread_cleanup (memif_per_thread_main_handle_t * pt_main);
+
/** \brief Memory interface create function
@param conn - connection handle for client app
@param args - memory interface connection arguments
@@ -521,6 +580,19 @@ int memif_create (memif_conn_handle_t * conn, memif_conn_args_t * args,
*/
int memif_control_fd_handler (int fd, uint8_t events);
+/** \brief Memif per thread control file descriptor handler
+ @param pt_main - per thread main handle
+ @param fd - file descriptor on which the event occured
+ @param events - event type(s) that occured
+
+ Per thread version of memif_control_fd_handler.
+
+ \return memif_err_t
+
+*/
+int memif_per_thread_control_fd_handler (memif_per_thread_main_handle_t
+ pt_main, int fd, uint8_t events);
+
/** \brief Memif delete
@param conn - pointer to memif connection handle
@@ -608,6 +680,17 @@ int memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
*/
int memif_poll_event (int timeout);
+/** \brief Memif poll event
+ @param pt_main - per thread main handle
+ @param timeout - timeout in seconds
+
+ Per thread version of memif_poll_event.
+
+ \return memif_err_t
+*/
+int memif_per_thread_poll_event (memif_per_thread_main_handle_t pt_main,
+ int timeout);
+
/** \brief Send signal to stop concurrently running memif_poll_event().
The function, however, does not wait for memif_poll_event() to stop.
@@ -622,6 +705,15 @@ int memif_poll_event (int timeout);
*/
#define MEMIF_HAVE_CANCEL_POLL_EVENT 1
int memif_cancel_poll_event ();
+/** \brief Send signal to stop concurrently running memif_poll_event().
+ @param pt_main - per thread main handle
+
+ Per thread version of memif_cancel_poll_event.
+
+ \return memif_err_t
+*/
+int memif_per_thread_cancel_poll_event (memif_per_thread_main_handle_t
+ pt_main);
/** \brief Set connection request timer value
@param timer - new timer value
@@ -633,6 +725,19 @@ int memif_cancel_poll_event ();
*/
int memif_set_connection_request_timer (struct itimerspec timer);
+/** \brief Set connection request timer value
+ @param pt_main - per thread main handle
+ @param timer - new timer value
+
+ Per thread version of memif_set_connection_request_timer
+
+ \return memif_err_t
+*/
+int
+memif_per_thread_set_connection_request_timer (memif_per_thread_main_handle_t
+ pt_main,
+ struct itimerspec timer);
+
/** \brief Send connection request
@param conn - memif connection handle
@@ -656,8 +761,22 @@ int memif_request_connection (memif_conn_handle_t conn);
\return memif_err_t
*/
-int memif_create_socket (memif_socket_handle_t * sock, const char * filename,
- void * private_ctx);
+int memif_create_socket (memif_socket_handle_t * sock, const char *filename,
+ void *private_ctx);
+
+/** \brief Create memif socket
+ @param pt_main - per thread main handle
+ @param sock - socket handle for client app
+ @param filename - path to socket file
+ @param private_ctx - private context
+
+ Per thread version of memif_create_sopcket.
+
+ \return memif_err_t
+*/
+int memif_per_thread_create_socket (memif_per_thread_main_handle_t pt_main,
+ memif_socket_handle_t * sock,
+ const char *filename, void *private_ctx);
/** \brief Delete memif socket
@param sock - socket handle for client app
@@ -669,6 +788,15 @@ int memif_create_socket (memif_socket_handle_t * sock, const char * filename,
*/
int memif_delete_socket (memif_socket_handle_t * sock);
+/** \brief Get socket filename
+ @param sock - socket handle for client app
+
+ Return constant pointer to socket filename.
+
+ \return cosnt char *
+*/
+const char *memif_get_socket_filename (memif_socket_handle_t sock);
+
/** @} */
#endif /* _LIBMEMIF_H_ */
diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c
index 42dfeafb247..def51a729f7 100644
--- a/extras/libmemif/src/main.c
+++ b/extras/libmemif/src/main.c
@@ -66,8 +66,6 @@
#endif /* __x86_x64__ */
libmemif_main_t libmemif_main;
-int memif_epfd;
-int poll_cancel_fd = -1;
static char memif_buf[MAX_ERRBUF_LEN];
@@ -236,8 +234,17 @@ memif_syscall_error_handler (int err_code)
return MEMIF_ERR_SYSCALL;
}
+/* Always valid */
+libmemif_main_t *
+get_libmemif_main (memif_socket_t * ms)
+{
+ if (ms != NULL && ms->lm != NULL)
+ return ms->lm;
+ return &libmemif_main;
+}
+
static int
-memif_add_epoll_fd (int fd, uint32_t events)
+memif_add_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events)
{
if (fd < 0)
{
@@ -248,7 +255,7 @@ memif_add_epoll_fd (int fd, uint32_t events)
memset (&evt, 0, sizeof (evt));
evt.events = events;
evt.data.fd = fd;
- if (epoll_ctl (memif_epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
+ if (epoll_ctl (lm->epfd, EPOLL_CTL_ADD, fd, &evt) < 0)
{
DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
return -1;
@@ -258,7 +265,7 @@ memif_add_epoll_fd (int fd, uint32_t events)
}
static int
-memif_mod_epoll_fd (int fd, uint32_t events)
+memif_mod_epoll_fd (libmemif_main_t * lm, int fd, uint32_t events)
{
if (fd < 0)
{
@@ -269,7 +276,7 @@ memif_mod_epoll_fd (int fd, uint32_t events)
memset (&evt, 0, sizeof (evt));
evt.events = events;
evt.data.fd = fd;
- if (epoll_ctl (memif_epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
+ if (epoll_ctl (lm->epfd, EPOLL_CTL_MOD, fd, &evt) < 0)
{
DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
return -1;
@@ -279,7 +286,7 @@ memif_mod_epoll_fd (int fd, uint32_t events)
}
static int
-memif_del_epoll_fd (int fd)
+memif_del_epoll_fd (libmemif_main_t * lm, int fd)
{
if (fd < 0)
{
@@ -288,7 +295,7 @@ memif_del_epoll_fd (int fd)
}
struct epoll_event evt;
memset (&evt, 0, sizeof (evt));
- if (epoll_ctl (memif_epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
+ if (epoll_ctl (lm->epfd, EPOLL_CTL_DEL, fd, &evt) < 0)
{
DBG ("epoll_ctl: %s fd %d", strerror (errno), fd);
return -1;
@@ -300,8 +307,15 @@ memif_del_epoll_fd (int fd)
int
memif_control_fd_update (int fd, uint8_t events, void *private_ctx)
{
+ libmemif_main_t *lm;
+
+ if (private_ctx == NULL)
+ return MEMIF_ERR_INVAL_ARG;
+
+ lm = (libmemif_main_t *) private_ctx;
+
if (events & MEMIF_FD_EVENT_DEL)
- return memif_del_epoll_fd (fd);
+ return memif_del_epoll_fd (lm, fd);
uint32_t evt = 0;
if (events & MEMIF_FD_EVENT_READ)
@@ -310,15 +324,15 @@ memif_control_fd_update (int fd, uint8_t events, void *private_ctx)
evt |= EPOLLOUT;
if (events & MEMIF_FD_EVENT_MOD)
- return memif_mod_epoll_fd (fd, evt);
+ return memif_mod_epoll_fd (lm, fd, evt);
- return memif_add_epoll_fd (fd, evt);
+ return memif_add_epoll_fd (lm, fd, evt);
}
int
-add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len)
+add_list_elt (libmemif_main_t * lm, memif_list_elt_t * e,
+ memif_list_elt_t ** list, uint16_t * len)
{
- libmemif_main_t *lm = &libmemif_main;
memif_list_elt_t *tmp;
int i;
@@ -413,9 +427,9 @@ free_list_elt_ctx (memif_list_elt_t * list, uint16_t len,
}
static void
-memif_control_fd_update_register (memif_control_fd_update_t * cb)
+memif_control_fd_update_register (libmemif_main_t * lm,
+ memif_control_fd_update_t * cb)
{
- libmemif_main_t *lm = &libmemif_main;
lm->control_fd_update = cb;
}
@@ -433,23 +447,20 @@ memif_register_external_region (memif_add_external_region_t * ar,
}
static void
-memif_alloc_register (memif_alloc_t * ma)
+memif_alloc_register (libmemif_main_t * lm, memif_alloc_t * ma)
{
- libmemif_main_t *lm = &libmemif_main;
lm->alloc = ma;
}
static void
-memif_realloc_register (memif_realloc_t * mr)
+memif_realloc_register (libmemif_main_t * lm, memif_realloc_t * mr)
{
- libmemif_main_t *lm = &libmemif_main;
lm->realloc = mr;
}
static void
-memif_free_register (memif_free_t * mf)
+memif_free_register (libmemif_main_t * lm, memif_free_t * mf)
{
- libmemif_main_t *lm = &libmemif_main;
lm->free = mf;
}
@@ -473,6 +484,27 @@ memif_set_connection_request_timer (struct itimerspec timer)
}
int
+memif_per_thread_set_connection_request_timer (memif_per_thread_main_handle_t
+ pt_main,
+ struct itimerspec timer)
+{
+ libmemif_main_t *lm = (libmemif_main_t *) pt_main;
+ int err = MEMIF_ERR_SUCCESS;
+
+ lm->arm = timer;
+
+ /* overwrite timer, if already armed */
+ if (lm->disconn_slaves != 0)
+ {
+ if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ }
+ }
+ return err;
+}
+
+int
memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
memif_alloc_t * memif_alloc, memif_realloc_t * memif_realloc,
memif_free_t * memif_free)
@@ -481,24 +513,25 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
libmemif_main_t *lm = &libmemif_main;
memset (lm, 0, sizeof (libmemif_main_t));
+ /* register custom memory management */
if (memif_alloc != NULL)
{
- memif_alloc_register (memif_alloc);
+ memif_alloc_register (lm, memif_alloc);
}
else
- memif_alloc_register (malloc);
+ memif_alloc_register (lm, malloc);
if (memif_realloc != NULL)
{
- memif_realloc_register (memif_realloc);
+ memif_realloc_register (lm, memif_realloc);
}
else
- memif_realloc_register (realloc);
+ memif_realloc_register (lm, realloc);
if (memif_free != NULL)
- memif_free_register (memif_free);
+ memif_free_register (lm, memif_free);
else
- memif_free_register (free);
+ memif_free_register (lm, free);
if (app_name != NULL)
{
@@ -512,20 +545,21 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
strlen (MEMIF_DEFAULT_APP_NAME));
}
+ lm->poll_cancel_fd = -1;
/* register control fd update callback */
if (on_control_fd_update != NULL)
- memif_control_fd_update_register (on_control_fd_update);
+ memif_control_fd_update_register (lm, on_control_fd_update);
else
{
- memif_epfd = epoll_create (1);
- memif_control_fd_update_register (memif_control_fd_update);
- if ((poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
+ lm->epfd = epoll_create (1);
+ memif_control_fd_update_register (lm, memif_control_fd_update);
+ if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
{
err = errno;
DBG ("eventfd: %s", strerror (err));
return memif_syscall_error_handler (err);
}
- lm->control_fd_update (poll_cancel_fd, MEMIF_FD_EVENT_READ, NULL);
+ lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ, NULL);
DBG ("libmemif event polling initialized");
}
@@ -606,6 +640,13 @@ memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name,
goto error;
}
+ /* Create default socket */
+ err = memif_create_socket ((memif_socket_handle_t *) &
+ lm->default_socket,
+ MEMIF_DEFAULT_SOCKET_PATH, NULL);
+ if (err != MEMIF_ERR_SUCCESS)
+ goto error;
+
return err;
error:
@@ -613,6 +654,172 @@ error:
return err;
}
+int
+memif_per_thread_init (memif_per_thread_main_handle_t * pt_main,
+ void *private_ctx,
+ memif_control_fd_update_t * on_control_fd_update,
+ char *app_name, memif_alloc_t * memif_alloc,
+ memif_realloc_t * memif_realloc,
+ memif_free_t * memif_free)
+{
+ memif_err_t err = MEMIF_ERR_SUCCESS;
+ int i;
+ libmemif_main_t *lm;
+
+ /* Allocate unique libmemif main */
+ if (memif_alloc != NULL)
+ lm = memif_alloc (sizeof (libmemif_main_t));
+ else
+ lm = malloc (sizeof (libmemif_main_t));
+
+ if (lm == NULL)
+ return MEMIF_ERR_NOMEM;
+
+ memset (lm, 0, sizeof (libmemif_main_t));
+
+ /* register custom memory management */
+ if (memif_alloc != NULL)
+ {
+ memif_alloc_register (lm, memif_alloc);
+ }
+ else
+ memif_alloc_register (lm, malloc);
+
+ if (memif_realloc != NULL)
+ {
+ memif_realloc_register (lm, memif_realloc);
+ }
+ else
+ memif_realloc_register (lm, realloc);
+
+ if (memif_free != NULL)
+ memif_free_register (lm, memif_free);
+ else
+ memif_free_register (lm, free);
+
+ lm->private_ctx = private_ctx;
+
+ /* set app name */
+ if (app_name != NULL)
+ {
+ uint8_t len = (strlen (app_name) > MEMIF_NAME_LEN)
+ ? strlen (app_name) : MEMIF_NAME_LEN;
+ strncpy ((char *) lm->app_name, app_name, len);
+ }
+ else
+ {
+ strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME,
+ strlen (MEMIF_DEFAULT_APP_NAME));
+ }
+
+ lm->poll_cancel_fd = -1;
+ /* register control fd update callback */
+ if (on_control_fd_update != NULL)
+ memif_control_fd_update_register (lm, on_control_fd_update);
+ else
+ {
+ lm->epfd = epoll_create (1);
+ memif_control_fd_update_register (lm, memif_control_fd_update);
+ if ((lm->poll_cancel_fd = eventfd (0, EFD_NONBLOCK)) < 0)
+ {
+ err = errno;
+ DBG ("eventfd: %s", strerror (err));
+ return memif_syscall_error_handler (err);
+ }
+ lm->control_fd_update (lm->poll_cancel_fd, MEMIF_FD_EVENT_READ,
+ lm->private_ctx);
+ DBG ("libmemif event polling initialized");
+ }
+
+ /* Initialize lists */
+ lm->control_list_len = 2;
+ lm->interrupt_list_len = 2;
+ lm->socket_list_len = 1;
+ lm->pending_list_len = 1;
+
+ lm->control_list =
+ lm->alloc (sizeof (memif_list_elt_t) * lm->control_list_len);
+ if (lm->control_list == NULL)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+ lm->interrupt_list =
+ lm->alloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len);
+ if (lm->interrupt_list == NULL)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+ lm->socket_list =
+ lm->alloc (sizeof (memif_list_elt_t) * lm->socket_list_len);
+ if (lm->socket_list == NULL)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+ lm->pending_list =
+ lm->alloc (sizeof (memif_list_elt_t) * lm->pending_list_len);
+ if (lm->pending_list == NULL)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+
+ for (i = 0; i < lm->control_list_len; i++)
+ {
+ lm->control_list[i].key = -1;
+ lm->control_list[i].data_struct = NULL;
+ }
+ for (i = 0; i < lm->interrupt_list_len; i++)
+ {
+ lm->interrupt_list[i].key = -1;
+ lm->interrupt_list[i].data_struct = NULL;
+ }
+ for (i = 0; i < lm->socket_list_len; i++)
+ {
+ lm->socket_list[i].key = -1;
+ lm->socket_list[i].data_struct = NULL;
+ }
+ for (i = 0; i < lm->pending_list_len; i++)
+ {
+ lm->pending_list[i].key = -1;
+ lm->pending_list[i].data_struct = NULL;
+ }
+
+ /* Initialize autoconnect */
+ lm->disconn_slaves = 0;
+
+ lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK);
+ if (lm->timerfd < 0)
+ {
+ err = memif_syscall_error_handler (errno);
+ goto error;
+ }
+
+ lm->arm.it_value.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
+ lm->arm.it_value.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
+ lm->arm.it_interval.tv_sec = MEMIF_DEFAULT_RECONNECT_PERIOD_SEC;
+ lm->arm.it_interval.tv_nsec = MEMIF_DEFAULT_RECONNECT_PERIOD_NSEC;
+
+ if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ,
+ lm->private_ctx) < 0)
+ {
+ DBG ("callback type memif_control_fd_update_t error!");
+ err = MEMIF_ERR_CB_FDUPDATE;
+ goto error;
+ }
+
+ *pt_main = lm;
+
+ return err;
+
+error:
+ *pt_main = lm;
+ memif_per_thread_cleanup (pt_main);
+ return err;
+}
+
static inline memif_ring_t *
memif_get_ring (memif_connection_t * conn, memif_ring_type_t type,
uint16_t ring_num)
@@ -649,7 +856,7 @@ memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode,
static int
memif_socket_start_listening (memif_socket_t * ms)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (ms);
memif_list_elt_t elt;
struct stat file_stat;
struct sockaddr_un un = { 0 };
@@ -703,7 +910,7 @@ memif_socket_start_listening (memif_socket_t * ms)
/* add socket to libmemif main */
elt.key = ms->fd;
elt.data_struct = ms;
- add_list_elt (&elt, &lm->socket_list, &lm->socket_list_len);
+ add_list_elt (lm, &elt, &lm->socket_list, &lm->socket_list_len);
lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ, ms->private_ctx);
ms->type = MEMIF_SOCKET_TYPE_LISTENER;
@@ -799,12 +1006,96 @@ error:
}
int
+memif_per_thread_create_socket (memif_per_thread_main_handle_t pt_main,
+ memif_socket_handle_t * sock,
+ const char *filename, void *private_ctx)
+{
+ libmemif_main_t *lm = (libmemif_main_t *) pt_main;
+ memif_socket_t *ms = (memif_socket_t *) * sock;
+ int i, err = MEMIF_ERR_SUCCESS;
+
+ if (lm == NULL)
+ return MEMIF_ERR_INVAL_ARG;
+
+ for (i = 0; i < lm->socket_list_len; i++)
+ {
+ if ((ms = (memif_socket_t *) lm->socket_list[i].data_struct) != NULL)
+ {
+ if (strncmp ((char *) ms->filename, filename,
+ strlen ((char *) ms->filename)) == 0)
+ return MEMIF_ERR_INVAL_ARG;
+ }
+ }
+
+ /* allocate memif_socket_t */
+ ms = NULL;
+ ms = lm->alloc (sizeof (memif_socket_t));
+ if (ms == NULL)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+ memset (ms, 0, sizeof (memif_socket_t));
+ ms->lm = lm;
+ /* set filename */
+ ms->filename = lm->alloc (strlen (filename) + sizeof (char));
+ if (ms->filename == NULL)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+ memset (ms->filename, 0, strlen (filename) + sizeof (char));
+ strncpy ((char *) ms->filename, filename, strlen (filename));
+
+ ms->type = MEMIF_SOCKET_TYPE_NONE;
+
+ ms->interface_list_len = 1;
+ ms->interface_list =
+ lm->alloc (sizeof (memif_list_elt_t) * ms->interface_list_len);
+ if (ms->interface_list == NULL)
+ {
+ err = MEMIF_ERR_NOMEM;
+ goto error;
+ }
+ ms->interface_list[0].key = -1;
+ ms->interface_list[0].data_struct = NULL;
+
+ *sock = ms;
+
+ return err;
+
+error:
+ if (ms != NULL)
+ {
+ if (ms->filename != NULL)
+ {
+ lm->free (ms->filename);
+ ms->filename = NULL;
+ }
+ if (ms->fd > 0)
+ {
+ close (ms->fd);
+ ms->fd = -1;
+ }
+ if (ms->interface_list != NULL)
+ {
+ lm->free (ms->interface_list);
+ ms->interface_list = NULL;
+ ms->interface_list_len = 0;
+ }
+ lm->free (ms);
+ *sock = ms = NULL;
+ }
+ return err;
+}
+
+int
memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
memif_connection_update_t * on_connect,
memif_connection_update_t * on_disconnect,
memif_interrupt_t * on_interrupt, void *private_ctx)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (args->socket);
int err, index = 0;
memif_list_elt_t elt;
memif_connection_t *conn = (memif_connection_t *) * c;
@@ -815,6 +1106,7 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
DBG ("This handle already points to existing memif.");
return MEMIF_ERR_CONN;
}
+
conn = (memif_connection_t *) lm->alloc (sizeof (memif_connection_t));
if (conn == NULL)
{
@@ -865,19 +1157,14 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
if (args->socket != NULL)
conn->args.socket = args->socket;
+ else if (lm->default_socket != NULL)
+ conn->args.socket = lm->default_socket;
else
{
- if (lm->default_socket == NULL)
- {
- err =
- memif_create_socket ((memif_socket_handle_t *) &
- lm->default_socket,
- MEMIF_DEFAULT_SOCKET_PATH, NULL);
- if (err != MEMIF_ERR_SUCCESS)
- goto error;
- }
- conn->args.socket = lm->default_socket;
+ err = MEMIF_ERR_INVAL_ARG;
+ goto error;
}
+
ms = (memif_socket_t *) conn->args.socket;
if ((conn->args.is_master && ms->type == MEMIF_SOCKET_TYPE_CLIENT) ||
@@ -889,22 +1176,25 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
elt.key = conn->args.interface_id;
elt.data_struct = conn;
- add_list_elt (&elt, &ms->interface_list, &ms->interface_list_len);
+ add_list_elt (lm, &elt, &ms->interface_list, &ms->interface_list_len);
ms->use_count++;
if (conn->args.is_master)
{
if (ms->type == MEMIF_SOCKET_TYPE_NONE)
- err = memif_socket_start_listening (ms);
- if (err != MEMIF_ERR_SUCCESS)
- goto error;
+ {
+ err = memif_socket_start_listening (ms);
+ if (err != MEMIF_ERR_SUCCESS)
+ goto error;
+ }
}
else
{
elt.key = -1;
elt.data_struct = conn;
if ((index =
- add_list_elt (&elt, &lm->control_list, &lm->control_list_len)) < 0)
+ add_list_elt (lm, &elt, &lm->control_list,
+ &lm->control_list_len)) < 0)
{
err = MEMIF_ERR_NOMEM;
goto error;
@@ -923,7 +1213,7 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args,
goto error;
}
}
- lm->disconn_slaves++;
+ lm->disconn_slaves++;
}
*c = conn;
@@ -940,8 +1230,8 @@ error:
int
memif_request_connection (memif_conn_handle_t c)
{
- libmemif_main_t *lm = &libmemif_main;
memif_connection_t *conn = (memif_connection_t *) c;
+ libmemif_main_t *lm = get_libmemif_main (conn->args.socket);
memif_socket_t *ms;
int err = MEMIF_ERR_SUCCESS;
int sockfd = -1;
@@ -995,8 +1285,7 @@ memif_request_connection (memif_conn_handle_t c)
else
{
err = memif_syscall_error_handler (errno);
- strcpy ((char *) conn->remote_disconnect_string,
- memif_strerror (err));
+ strcpy ((char *) conn->remote_disconnect_string, memif_strerror (err));
goto error;
}
@@ -1083,7 +1372,121 @@ memif_control_fd_handler (int fd, uint8_t events)
get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
if (e != NULL)
{
- err = memif_read_ready (fd);
+ err = memif_read_ready (lm, fd);
+ return err;
+ }
+
+ get_list_elt (&e, lm->control_list, lm->control_list_len, fd);
+ if (e != NULL)
+ {
+ if (events & MEMIF_FD_EVENT_READ)
+ {
+ err =
+ ((memif_connection_t *) e->data_struct)->
+ read_fn (e->data_struct);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ if (events & MEMIF_FD_EVENT_WRITE)
+ {
+ err =
+ ((memif_connection_t *) e->data_struct)->
+ write_fn (e->data_struct);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ if (events & MEMIF_FD_EVENT_ERROR)
+ {
+ err =
+ ((memif_connection_t *) e->data_struct)->
+ error_fn (e->data_struct);
+ if (err != MEMIF_ERR_SUCCESS)
+ return err;
+ }
+ }
+ }
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+
+error:
+ return err;
+}
+
+int
+memif_per_thread_control_fd_handler (memif_per_thread_main_handle_t pt_main,
+ int fd, uint8_t events)
+{
+ int i, err = MEMIF_ERR_SUCCESS; /* 0 */
+ uint16_t num;
+ memif_list_elt_t *e = NULL;
+ memif_connection_t *conn;
+ libmemif_main_t *lm = (libmemif_main_t *) pt_main;
+
+ if (fd == lm->timerfd)
+ {
+ uint64_t b;
+ ssize_t size;
+ size = read (fd, &b, sizeof (b));
+
+ if (size == -1)
+ goto error;
+
+ for (i = 0; i < lm->control_list_len; i++)
+ {
+ if ((lm->control_list[i].key < 0)
+ && (lm->control_list[i].data_struct != NULL))
+ {
+ conn = lm->control_list[i].data_struct;
+ if (conn->args.is_master)
+ continue;
+ err = memif_request_connection (conn);
+ if (err != MEMIF_ERR_SUCCESS)
+ DBG ("memif_request_connection: %s", memif_strerror (err));
+ }
+ }
+ }
+ else
+ {
+ get_list_elt (&e, lm->interrupt_list, lm->interrupt_list_len, fd);
+ if (e != NULL)
+ {
+ if (((memif_connection_t *) e->data_struct)->on_interrupt != NULL)
+ {
+ num =
+ (((memif_connection_t *) e->data_struct)->
+ args.is_master) ? ((memif_connection_t *) e->
+ data_struct)->run_args.
+ num_s2m_rings : ((memif_connection_t *) e->data_struct)->
+ run_args.num_m2s_rings;
+ for (i = 0; i < num; i++)
+ {
+ if (((memif_connection_t *) e->data_struct)->
+ rx_queues[i].int_fd == fd)
+ {
+ ((memif_connection_t *) e->data_struct)->
+ on_interrupt ((void *) e->data_struct,
+ ((memif_connection_t *) e->
+ data_struct)->private_ctx, i);
+ return MEMIF_ERR_SUCCESS;
+ }
+ }
+ }
+ return MEMIF_ERR_SUCCESS;
+ }
+ get_list_elt (&e, lm->socket_list, lm->socket_list_len, fd);
+ if (e != NULL
+ && ((memif_socket_t *) e->data_struct)->type ==
+ MEMIF_SOCKET_TYPE_LISTENER)
+ {
+ err =
+ memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct);
+ return err;
+ }
+
+ get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd);
+ if (e != NULL)
+ {
+ err = memif_read_ready (lm, fd);
return err;
}
@@ -1126,6 +1529,7 @@ error:
int
memif_poll_event (int timeout)
{
+ libmemif_main_t *lm = &libmemif_main;
struct epoll_event evt;
int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */
uint32_t events = 0;
@@ -1135,7 +1539,7 @@ memif_poll_event (int timeout)
evt.events = EPOLLIN | EPOLLOUT;
sigset_t sigset;
sigemptyset (&sigset);
- en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset);
+ en = epoll_pwait (lm->epfd, &evt, 1, timeout, &sigset);
if (en < 0)
{
err = errno;
@@ -1144,7 +1548,50 @@ memif_poll_event (int timeout)
}
if (en > 0)
{
- if (evt.data.fd == poll_cancel_fd)
+ if (evt.data.fd == lm->poll_cancel_fd)
+ {
+ r = read (evt.data.fd, &counter, sizeof (counter));
+ if (r == -1)
+ return MEMIF_ERR_DISCONNECTED;
+
+ return MEMIF_ERR_POLL_CANCEL;
+ }
+ if (evt.events & EPOLLIN)
+ events |= MEMIF_FD_EVENT_READ;
+ if (evt.events & EPOLLOUT)
+ events |= MEMIF_FD_EVENT_WRITE;
+ if (evt.events & EPOLLERR)
+ events |= MEMIF_FD_EVENT_ERROR;
+ err = memif_control_fd_handler (evt.data.fd, events);
+ return err;
+ }
+ return 0;
+}
+
+int
+memif_per_thread_poll_event (memif_per_thread_main_handle_t pt_main,
+ int timeout)
+{
+ libmemif_main_t *lm = (libmemif_main_t *) pt_main;
+ struct epoll_event evt;
+ int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */
+ uint32_t events = 0;
+ uint64_t counter = 0;
+ ssize_t r = 0;
+ memset (&evt, 0, sizeof (evt));
+ evt.events = EPOLLIN | EPOLLOUT;
+ sigset_t sigset;
+ sigemptyset (&sigset);
+ en = epoll_pwait (lm->epfd, &evt, 1, timeout, &sigset);
+ if (en < 0)
+ {
+ err = errno;
+ DBG ("epoll_pwait: %s", strerror (err));
+ return memif_syscall_error_handler (err);
+ }
+ if (en > 0)
+ {
+ if (evt.data.fd == lm->poll_cancel_fd)
{
r = read (evt.data.fd, &counter, sizeof (counter));
if (r == -1)
@@ -1167,12 +1614,32 @@ memif_poll_event (int timeout)
int
memif_cancel_poll_event ()
{
+ libmemif_main_t *lm = &libmemif_main;
uint64_t counter = 1;
ssize_t w = 0;
- if (poll_cancel_fd == -1)
+ if (lm->poll_cancel_fd == -1)
return 0;
- w = write (poll_cancel_fd, &counter, sizeof (counter));
+ w = write (lm->poll_cancel_fd, &counter, sizeof (counter));
+ if (w < sizeof (counter))
+ return MEMIF_ERR_INT_WRITE;
+
+ return 0;
+}
+
+int
+memif_per_thread_cancel_poll_event (memif_per_thread_main_handle_t pt_main)
+{
+ libmemif_main_t *lm = (libmemif_main_t *) pt_main;
+ uint64_t counter = 1;
+ ssize_t w = 0;
+
+ if (lm == NULL)
+ return MEMIF_ERR_INVAL_ARG;
+
+ if (lm->poll_cancel_fd == -1)
+ return 0;
+ w = write (lm->poll_cancel_fd, &counter, sizeof (counter));
if (w < sizeof (counter))
return MEMIF_ERR_INT_WRITE;
@@ -1194,16 +1661,17 @@ memif_msg_queue_free (libmemif_main_t * lm, memif_msg_queue_elt_t ** e)
int
memif_disconnect_internal (memif_connection_t * c)
{
+ uint16_t num;
+ int err = MEMIF_ERR_SUCCESS, i; /* 0 */
+ memif_queue_t *mq;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
+ memif_list_elt_t *e;
+
if (c == NULL)
{
DBG ("no connection");
return MEMIF_ERR_NOCONN;
}
- uint16_t num;
- int err = MEMIF_ERR_SUCCESS, i; /* 0 */
- memif_queue_t *mq;
- libmemif_main_t *lm = &libmemif_main;
- memif_list_elt_t *e;
c->on_disconnect ((void *) c, c->private_ctx);
@@ -1311,11 +1779,22 @@ memif_disconnect_internal (memif_connection_t * c)
return err;
}
+const char *
+memif_get_socket_filename (memif_socket_handle_t sock)
+{
+ memif_socket_t *ms = (memif_socket_t *) sock;
+
+ if (ms == NULL)
+ return NULL;
+
+ return (char *) ms->filename;
+}
+
int
memif_delete_socket (memif_socket_handle_t * sock)
{
memif_socket_t *ms = (memif_socket_t *) * sock;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (ms);
/* check if socket is in use */
if (ms == NULL || ms->type != MEMIF_SOCKET_TYPE_NONE)
@@ -1335,7 +1814,7 @@ int
memif_delete (memif_conn_handle_t * conn)
{
memif_connection_t *c = (memif_connection_t *) * conn;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_socket_t *ms = NULL;
int err = MEMIF_ERR_SUCCESS;
@@ -1396,7 +1875,7 @@ memif_delete (memif_conn_handle_t * conn)
int
memif_connect1 (memif_connection_t * c)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_region_t *mr;
memif_queue_t *mq;
int i;
@@ -1578,7 +2057,7 @@ memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
return memif_syscall_error_handler (errno);
e.key = mq[x].int_fd;
e.data_struct = conn;
- add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
+ add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len);
mq[x].ring = memif_get_ring (conn, MEMIF_RING_S2M, x);
DBG ("RING: %p I: %d", mq[x].ring, x);
@@ -1603,7 +2082,7 @@ memif_init_queues (libmemif_main_t * lm, memif_connection_t * conn)
return memif_syscall_error_handler (errno);
e.key = mq[x].int_fd;
e.data_struct = conn;
- add_list_elt (&e, &lm->interrupt_list, &lm->interrupt_list_len);
+ add_list_elt (lm, &e, &lm->interrupt_list, &lm->interrupt_list_len);
mq[x].ring = memif_get_ring (conn, MEMIF_RING_M2S, x);
DBG ("RING: %p I: %d", mq[x].ring, x);
@@ -1623,7 +2102,7 @@ int
memif_init_regions_and_queues (memif_connection_t * conn)
{
memif_region_t *r;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (conn->args.socket);
/* region 0. rings */
memif_add_region (lm, conn, /* has_buffers */ 0);
@@ -1756,7 +2235,7 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
if (EXPECT_FALSE (!count_out))
return MEMIF_ERR_INVAL_ARG;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_queue_t *mq = &c->tx_queues[qid];
memif_ring_t *ring = mq->ring;
memif_buffer_t *b0;
@@ -1878,7 +2357,7 @@ memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count,
run_args.num_m2s_rings;
if (EXPECT_FALSE (qid >= num))
return MEMIF_ERR_QID;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_queue_t *mq = &c->rx_queues[qid];
memif_ring_t *ring = mq->ring;
uint16_t mask = (1 << mq->log2_ring_size) - 1;
@@ -2074,8 +2553,8 @@ int
memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
char *buf, ssize_t buflen)
{
- libmemif_main_t *lm = &libmemif_main;
memif_connection_t *c = (memif_connection_t *) conn;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_socket_t *ms;
int err = MEMIF_ERR_SUCCESS, i;
ssize_t l0 = 0, l1;
@@ -2258,7 +2737,7 @@ memif_cleanup ()
{
libmemif_main_t *lm = &libmemif_main;
- memif_delete_socket ((memif_socket_handle_t *) &lm->default_socket);
+ memif_delete_socket ((memif_socket_handle_t *) & lm->default_socket);
if (lm->control_list)
lm->free (lm->control_list);
@@ -2272,8 +2751,40 @@ memif_cleanup ()
if (lm->pending_list)
lm->free (lm->pending_list);
lm->pending_list = NULL;
- if (poll_cancel_fd != -1)
- close (poll_cancel_fd);
+ if (lm->poll_cancel_fd != -1)
+ close (lm->poll_cancel_fd);
+
+ return MEMIF_ERR_SUCCESS; /* 0 */
+}
+
+int
+memif_per_thread_cleanup (memif_per_thread_main_handle_t * pt_main)
+{
+ libmemif_main_t *lm = (libmemif_main_t *) * pt_main;
+
+ if (lm == NULL)
+ return MEMIF_ERR_INVAL_ARG;
+
+ /* No default socket in case of per thread */
+
+ if (lm->control_list)
+ lm->free (lm->control_list);
+ lm->control_list = NULL;
+ if (lm->interrupt_list)
+ lm->free (lm->interrupt_list);
+ lm->interrupt_list = NULL;
+ if (lm->socket_list)
+ lm->free (lm->socket_list);
+ lm->socket_list = NULL;
+ if (lm->pending_list)
+ lm->free (lm->pending_list);
+ lm->pending_list = NULL;
+ if (lm->poll_cancel_fd != -1)
+ close (lm->poll_cancel_fd);
+
+ lm->free (lm);
+
+ *pt_main = NULL;
return MEMIF_ERR_SUCCESS; /* 0 */
}
diff --git a/extras/libmemif/src/memif_private.h b/extras/libmemif/src/memif_private.h
index eceac677752..88237dca265 100644
--- a/extras/libmemif/src/memif_private.h
+++ b/extras/libmemif/src/memif_private.h
@@ -120,6 +120,8 @@ typedef struct
memif_log2_ring_size_t log2_ring_size;
} memif_conn_run_args_t;
+struct libmemif_main;
+
typedef struct memif_connection
{
uint16_t index;
@@ -165,19 +167,25 @@ typedef struct
uint16_t use_count;
memif_socket_type_t type;
uint8_t *filename;
+ /* unique database */
+ struct libmemif_main *lm;
uint16_t interface_list_len;
void *private_ctx;
memif_list_elt_t *interface_list; /* memif master interfaces listening on this socket */
} memif_socket_t;
-typedef struct
+typedef struct libmemif_main
{
memif_control_fd_update_t *control_fd_update;
int timerfd;
+ int epfd;
+ int poll_cancel_fd;
struct itimerspec arm, disarm;
uint16_t disconn_slaves;
uint8_t app_name[MEMIF_NAME_LEN];
+ void *private_ctx;
+
memif_socket_handle_t default_socket;
memif_add_external_region_t *add_external_region;
@@ -200,7 +208,6 @@ typedef struct
} libmemif_main_t;
extern libmemif_main_t libmemif_main;
-extern int memif_epfd;
/* main.c */
@@ -215,7 +222,7 @@ int memif_disconnect_internal (memif_connection_t * c);
/* map errno to memif error code */
int memif_syscall_error_handler (int err_code);
-int add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list,
+int add_list_elt (libmemif_main_t *lm, memif_list_elt_t * e, memif_list_elt_t ** list,
uint16_t * len);
int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
@@ -223,6 +230,8 @@ int get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list,
int free_list_elt (memif_list_elt_t * list, uint16_t len, int key);
+libmemif_main_t *get_libmemif_main (memif_socket_t * ms);
+
#ifndef __NR_memfd_create
#if defined __x86_64__
#define __NR_memfd_create 319
diff --git a/extras/libmemif/src/socket.c b/extras/libmemif/src/socket.c
index 6efcfb5b5f7..2ea6fabc58b 100644
--- a/extras/libmemif/src/socket.c
+++ b/extras/libmemif/src/socket.c
@@ -71,7 +71,7 @@ memif_msg_send (int fd, memif_msg_t * msg, int afd)
static_fn int
memif_msg_enq_ack (memif_connection_t * c)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_msg_queue_elt_t *e =
(memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
if (e == NULL)
@@ -99,9 +99,8 @@ memif_msg_enq_ack (memif_connection_t * c)
}
static_fn int
-memif_msg_send_hello (int fd)
+memif_msg_send_hello (libmemif_main_t * lm, int fd)
{
- libmemif_main_t *lm = &libmemif_main;
memif_msg_t msg = { 0 };
memif_msg_hello_t *h = &msg.hello;
msg.type = MEMIF_MSG_TYPE_HELLO;
@@ -124,7 +123,7 @@ memif_msg_send_hello (int fd)
static_fn int
memif_msg_enq_init (memif_connection_t * c)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_msg_queue_elt_t *e =
(memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
if (e == NULL)
@@ -166,7 +165,7 @@ memif_msg_enq_init (memif_connection_t * c)
static_fn int
memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_region_t *mr = &c->regions[region_index];
memif_msg_queue_elt_t *e =
@@ -203,7 +202,7 @@ memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
static_fn int
memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_msg_queue_elt_t *e =
(memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
if (e == NULL)
@@ -250,7 +249,7 @@ memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
static_fn int
memif_msg_enq_connect (memif_connection_t * c)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_msg_queue_elt_t *e =
(memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
if (e == NULL)
@@ -285,7 +284,7 @@ memif_msg_enq_connect (memif_connection_t * c)
static_fn int
memif_msg_enq_connected (memif_connection_t * c)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_msg_queue_elt_t *e =
(memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
if (e == NULL)
@@ -371,10 +370,11 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
memif_list_elt_t *elt = NULL;
memif_list_elt_t elt2;
memif_connection_t *c = NULL;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (ms);
uint8_t err_string[96];
memset (err_string, 0, sizeof (char) * 96);
int err = MEMIF_ERR_SUCCESS; /* 0 */
+
if (i->version != MEMIF_VERSION)
{
DBG ("MEMIF_VER_ERR");
@@ -464,7 +464,7 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
elt2.key = c->fd;
elt2.data_struct = c;
- add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
+ add_list_elt (lm, &elt2, &lm->control_list, &lm->control_list_len);
free_list_elt (lm->pending_list, lm->pending_list_len, fd);
return err;
@@ -483,7 +483,7 @@ static_fn int
memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
int fd)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_msg_add_region_t *ar = &msg->add_region;
memif_region_t *mr;
@@ -517,7 +517,7 @@ memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
static_fn int
memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_msg_add_ring_t *ar = &msg->add_ring;
@@ -580,7 +580,7 @@ static_fn int
memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
{
memif_msg_connect_t *cm = &msg->connect;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
memif_list_elt_t elt;
int err;
@@ -598,7 +598,8 @@ memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
{
elt.key = c->rx_queues[i].int_fd;
elt.data_struct = c;
- add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
+ add_list_elt (lm, &elt, &lm->interrupt_list,
+ &lm->interrupt_list_len);
lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
c->private_ctx);
@@ -616,7 +617,7 @@ static_fn int
memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
{
memif_msg_connect_t *cm = &msg->connect;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
int err;
err = memif_connect1 (c);
@@ -658,7 +659,7 @@ memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
}
static_fn int
-memif_msg_receive (int ifd)
+memif_msg_receive (libmemif_main_t * lm, int ifd)
{
char ctl[CMSG_SPACE (sizeof (int)) +
CMSG_SPACE (sizeof (struct ucred))] = { 0 };
@@ -669,7 +670,6 @@ memif_msg_receive (int ifd)
int err = MEMIF_ERR_SUCCESS; /* 0 */
int fd = -1;
int i;
- libmemif_main_t *lm = &libmemif_main;
memif_connection_t *c = NULL;
memif_socket_t *ms = NULL;
memif_list_elt_t *elt = NULL;
@@ -827,8 +827,10 @@ memif_conn_fd_error (memif_connection_t * c)
int
memif_conn_fd_read_ready (memif_connection_t * c)
{
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
int err;
- err = memif_msg_receive (c->fd);
+
+ err = memif_msg_receive (lm, c->fd);
if (err != 0)
{
err = memif_disconnect_internal (c);
@@ -840,7 +842,7 @@ memif_conn_fd_read_ready (memif_connection_t * c)
int
memif_conn_fd_write_ready (memif_connection_t * c)
{
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (c->args.socket);
int err = MEMIF_ERR_SUCCESS; /* 0 */
@@ -869,7 +871,7 @@ memif_conn_fd_accept_ready (memif_socket_t * ms)
int addr_len;
struct sockaddr_un client;
int conn_fd;
- libmemif_main_t *lm = &libmemif_main;
+ libmemif_main_t *lm = get_libmemif_main (ms);
DBG ("accept called");
@@ -888,19 +890,19 @@ memif_conn_fd_accept_ready (memif_socket_t * ms)
elt.key = conn_fd;
elt.data_struct = ms;
- add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
+ add_list_elt (lm, &elt, &lm->pending_list, &lm->pending_list_len);
lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE,
ms->private_ctx);
- return memif_msg_send_hello (conn_fd);
+ return memif_msg_send_hello (lm, conn_fd);
}
int
-memif_read_ready (int fd)
+memif_read_ready (libmemif_main_t * lm, int fd)
{
int err;
- err = memif_msg_receive (fd);
+ err = memif_msg_receive (lm, fd);
return err;
}
diff --git a/extras/libmemif/src/socket.h b/extras/libmemif/src/socket.h
index e0792270b53..ea6979d6bd5 100644
--- a/extras/libmemif/src/socket.h
+++ b/extras/libmemif/src/socket.h
@@ -39,7 +39,7 @@ int memif_conn_fd_error (memif_connection_t * c);
int memif_conn_fd_accept_ready (memif_socket_t * ms);
-int memif_read_ready (int fd);
+int memif_read_ready (libmemif_main_t *lm, int fd);
int memif_msg_send_disconnect (int fd, uint8_t * err_string,
uint32_t err_code);
@@ -53,7 +53,7 @@ int memif_msg_send (int fd, memif_msg_t * msg, int afd);
int memif_msg_enq_ack (memif_connection_t * c);
-int memif_msg_send_hello (int fd);
+int memif_msg_send_hello (libmemif_main_t *lm, int fd);
int memif_msg_enq_init (memif_connection_t * c);
diff --git a/extras/libmemif/test/main_test.c b/extras/libmemif/test/main_test.c
index 0a8e091f395..8ad9961157f 100644
--- a/extras/libmemif/test/main_test.c
+++ b/extras/libmemif/test/main_test.c
@@ -94,7 +94,7 @@ START_TEST (test_init_epoll)
ck_assert_ptr_ne (lm, NULL);
ck_assert_ptr_ne (lm->control_fd_update, NULL);
ck_assert_int_gt (lm->timerfd, 2);
- ck_assert_int_gt (memif_epfd, -1);
+ ck_assert_int_gt (lm->epfd, -1);
if (lm->timerfd > 0)
close (lm->timerfd);
diff --git a/extras/libmemif/test/socket_test.c b/extras/libmemif/test/socket_test.c
index 589bfd84cb7..2d229383c20 100644
--- a/extras/libmemif/test/socket_test.c
+++ b/extras/libmemif/test/socket_test.c
@@ -296,7 +296,9 @@ START_TEST (test_send_hello)
NULL, NULL)) != MEMIF_ERR_SUCCESS)
ck_abort_msg ("err code: %u, err msg: %s", err, memif_strerror (err));
- if ((err = memif_msg_send_hello (conn.fd)) != MEMIF_ERR_SUCCESS)
+ conn.args.socket = libmemif_main.default_socket;
+
+ if ((err = memif_msg_send_hello (get_libmemif_main (conn.args.socket), conn.fd)) != MEMIF_ERR_SUCCESS)
ck_assert_msg (err == MEMIF_ERR_BAD_FD,
"err code: %u, err msg: %s", err, memif_strerror (err));
}
@@ -387,7 +389,7 @@ START_TEST (test_recv_init)
memif_list_elt_t elt;
elt.key = 69;
elt.data_struct = &conn;
- add_list_elt (&elt, &ms.interface_list, &ms.interface_list_len);
+ add_list_elt (get_libmemif_main (conn.args.socket), &elt, &ms.interface_list, &ms.interface_list_len);
if ((err =
memif_init (control_fd_update, TEST_APP_NAME, NULL,