From 2736fc7fcf1da65c1739bfbe498d7373a4034204 Mon Sep 17 00:00:00 2001 From: Jakub Grajciar Date: Wed, 31 Jul 2019 14:40:52 +0200 Subject: 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 Change-Id: I261563ecc34761a76e94f20c20015394398ddfd7 Signed-off-by: Jakub Grajciar (cherry picked from commit 17f2a7bbf25f54dbd71aa8f377875828b7b88e35) --- extras/libmemif/src/libmemif.h | 136 +++++++- extras/libmemif/src/main.c | 663 +++++++++++++++++++++++++++++++----- extras/libmemif/src/memif_private.h | 15 +- extras/libmemif/src/socket.c | 50 +-- extras/libmemif/src/socket.h | 4 +- 5 files changed, 759 insertions(+), 109 deletions(-) (limited to 'extras/libmemif/src') 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; } @@ -472,6 +483,27 @@ memif_set_connection_request_timer (struct itimerspec timer) return err; } +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, @@ -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; @@ -798,13 +1005,97 @@ error: return err; } +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); -- cgit 1.2.3-korg