X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=extras%2Flibmemif%2Fsrc%2Fmain.c;h=e735ee35fb169716ed9d4ca2dd57e8be6d832ff5;hb=0028e6f31624246e831b5b1a13dc46ab1d864fa7;hp=49427670b07c098cfc9e8b9cf821ba64577f5013;hpb=ba3c4e88c102afd601fbac0e02de73a28a279a73;p=vpp.git diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c index 49427670b07..e735ee35fb1 100644 --- a/extras/libmemif/src/main.c +++ b/extras/libmemif/src/main.c @@ -15,6 +15,7 @@ *------------------------------------------------------------------ */ +#define _GNU_SOURCE #include #include #include @@ -44,6 +45,7 @@ #include #include #include +#include /* memif protocol msg, ring and descriptor definitions */ #include @@ -54,26 +56,25 @@ /* private structs and functions */ #include -#define ERRLIST_LEN 36 +#define ERRLIST_LEN 40 #define MAX_ERRBUF_LEN 256 #if __x86_x64__ #define MEMIF_MEMORY_BARRIER() __builtin_ia32_sfence () #else -#define MEMIF_MEORY_BARRIER() __sync_synchronize () +#define MEMIF_MEMORY_BARRIER() __sync_synchronize () #endif /* __x86_x64__ */ -libmemif_main_t libmemif_main; -int memif_epfd; - static char memif_buf[MAX_ERRBUF_LEN]; const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ "Success.", /* MEMIF_ERR_SYSCALL */ "Unspecified syscall error (build with -DMEMIF_DBG or make debug).", + /* MEMIF_ERR_CONNREFUSED */ + "Connection refused", /* MEMIF_ERR_ACCES */ - "Permission to resoure denied.", + "Permission to resource denied.", /* MEMIF_ERR_NO_FILE */ "Socket file does not exist", /* MEMIF_ERR_FILE_LIMIT */ @@ -91,7 +92,7 @@ const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ /* MEMIF_ERR_INVAL_ARG */ "Invalid argument.", /* MEMIF_ERR_NOCONN */ - "Memif connection handle does not point to existing conenction", + "Memif connection handle does not point to existing connection", /* MEMIF_ERR_CONN */ "Memif connection handle points to existing connection", /* MEMIF_ERR_CB_FDUPDATE */ @@ -107,7 +108,7 @@ const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ /* MEMIF_ERR_NOBUF */ "Not enough memif buffers. There are unreceived data in shared memory.", /* MEMIF_ERR_NOBUF_DET */ - "Not enough space for memif details in suplied buffer. String data might be malformed.", + "Not enough space for memif details in supplied buffer. String data might be malformed.", /* MEMIF_ERR_INT_WRITE */ "Send interrupt error.", /* MEMIF_ERR_MFMSG */ @@ -119,7 +120,7 @@ const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ /* MEMIF_ERR_ID */ "Unmatched interface id.", /* MEMIF_ERR_ACCSLAVE */ - "Slave cannot accept connection reqest.", + "Slave cannot accept connection request.", /* MEMIF_ERR_ALRCONN */ "Interface is already connected.", /* MEMIF_ERR_MODE */ @@ -139,7 +140,13 @@ const char *memif_errlist[ERRLIST_LEN] = { /* MEMIF_ERR_SUCCESS */ /* MEMIF_ERR_DISCONNECTED */ "Interface is disconnected.", /* MEMIF_ERR_UNKNOWN_MSG */ - "Unknown message type received on control channel. (internal error)" + "Unknown message type received on control channel. (internal error)", + /* MEMIF_ERR_POLL_CANCEL */ + "Memif event polling was canceled.", + /* MEMIF_ERR_MAX_RING */ + "Maximum log2 ring size is 15", + /* MEMIF_ERR_PRIVHDR */ + "Private headers not supported." }; #define MEMIF_ERR_UNDEFINED "undefined error" @@ -149,18 +156,32 @@ memif_strerror (int err_code) { if (err_code >= ERRLIST_LEN) { - strncpy (memif_buf, MEMIF_ERR_UNDEFINED, strlen (MEMIF_ERR_UNDEFINED)); - memif_buf[strlen (MEMIF_ERR_UNDEFINED)] = '\0'; + strlcpy (memif_buf, MEMIF_ERR_UNDEFINED, sizeof (memif_buf)); } else { - strncpy (memif_buf, memif_errlist[err_code], - strlen (memif_errlist[err_code])); + strlcpy (memif_buf, memif_errlist[err_code], sizeof (memif_buf)); memif_buf[strlen (memif_errlist[err_code])] = '\0'; } return memif_buf; } +uint16_t +memif_get_version () +{ + return MEMIF_VERSION; +} + +const char * +memif_get_version_str () +{ +#define __STR_HELPER(x) #x +#define __STR(x) __STR_HELPER (x) + return __STR (MEMIF_VERSION_MAJOR) "." __STR (MEMIF_VERSION_MINOR); +#undef __STR +#undef __STR_HELPER +} + #define DBG_TX_BUF (0) #define DBG_RX_BUF (1) @@ -186,7 +207,7 @@ print_bytes (void *data, uint16_t len, uint8_t q) int memif_syscall_error_handler (int err_code) { - DBG_UNIX ("%s", strerror (err_code)); + DBG ("%s", strerror (err_code)); if (err_code == 0) return MEMIF_ERR_SUCCESS; @@ -198,10 +219,14 @@ memif_syscall_error_handler (int err_code) return MEMIF_ERR_PROC_FILE_LIMIT; if (err_code == ENOMEM) return MEMIF_ERR_NOMEM; -/* connection refused if master dows not exist +/* connection refused if master does not exist this error would spam the user until master was created */ +/* if (err_code == ECONNREFUSED) return MEMIF_ERR_SUCCESS; +*/ + if (err_code == ECONNREFUSED) + return MEMIF_ERR_CONNREFUSED; if (err_code == EALREADY) return MEMIF_ERR_ALREADY; if (err_code == EAGAIN) @@ -216,340 +241,437 @@ memif_syscall_error_handler (int err_code) } static int -memif_add_epoll_fd (int fd, uint32_t events) +memif_add_epoll_fd (memif_socket_t *ms, memif_fd_event_t fde, uint32_t events) { - if (fd < 0) + if (fde.fd < 0) { - DBG ("invalid fd %d", fd); + DBG ("invalid fd %d", fde.fd); return -1; } struct epoll_event evt; memset (&evt, 0, sizeof (evt)); evt.events = events; - evt.data.fd = fd; - if (epoll_ctl (memif_epfd, EPOLL_CTL_ADD, fd, &evt) < 0) + evt.data.ptr = fde.private_ctx; + if (epoll_ctl (ms->epfd, EPOLL_CTL_ADD, fde.fd, &evt) < 0) { - DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); + DBG ("epoll_ctl: %s fd %d", strerror (errno), fde.fd); return -1; } - DBG ("fd %d added to epoll", fd); + DBG ("fd %d added to epoll", fde.fd); return 0; } static int -memif_mod_epoll_fd (int fd, uint32_t events) +memif_mod_epoll_fd (memif_socket_t *ms, memif_fd_event_t fde, uint32_t events) { - if (fd < 0) + if (fde.fd < 0) { - DBG ("invalid fd %d", fd); + DBG ("invalid fd %d", fde.fd); return -1; } struct epoll_event evt; memset (&evt, 0, sizeof (evt)); evt.events = events; - evt.data.fd = fd; - if (epoll_ctl (memif_epfd, EPOLL_CTL_MOD, fd, &evt) < 0) + evt.data.ptr = fde.private_ctx; + if (epoll_ctl (ms->epfd, EPOLL_CTL_MOD, fde.fd, &evt) < 0) { - DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); + DBG ("epoll_ctl: %s fd %d", strerror (errno), fde.fd); return -1; } - DBG ("fd %d moddified on epoll", fd); + DBG ("fd %d modified on epoll", fde.fd); return 0; } static int -memif_del_epoll_fd (int fd) +memif_del_epoll_fd (memif_socket_t *ms, memif_fd_event_t fde) { - if (fd < 0) + if (fde.fd < 0) { - DBG ("invalid fd %d", fd); + DBG ("invalid fd %d", fde.fd); return -1; } struct epoll_event evt; memset (&evt, 0, sizeof (evt)); - if (epoll_ctl (memif_epfd, EPOLL_CTL_DEL, fd, &evt) < 0) + if (epoll_ctl (ms->epfd, EPOLL_CTL_DEL, fde.fd, &evt) < 0) { - DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); + DBG ("epoll_ctl: %s fd %d", strerror (errno), fde.fd); return -1; } - DBG ("fd %d removed from epoll", fd); + DBG ("fd %d removed from epoll", fde.fd); return 0; } int -memif_control_fd_update (int fd, uint8_t events) +memif_control_fd_update (memif_fd_event_t fde, void *private_ctx) { - if (events & MEMIF_FD_EVENT_DEL) - return memif_del_epoll_fd (fd); + memif_socket_t *ms = (memif_socket_t *) private_ctx; + int fd; + + if (ms == NULL) + return MEMIF_ERR_INVAL_ARG; + + if (fde.type & MEMIF_FD_EVENT_DEL) + return memif_del_epoll_fd (ms, fde); uint32_t evt = 0; - if (events & MEMIF_FD_EVENT_READ) + if (fde.type & MEMIF_FD_EVENT_READ) evt |= EPOLLIN; - if (events & MEMIF_FD_EVENT_WRITE) + if (fde.type & MEMIF_FD_EVENT_WRITE) evt |= EPOLLOUT; - if (events & MEMIF_FD_EVENT_MOD) - return memif_mod_epoll_fd (fd, evt); + if (fde.type & MEMIF_FD_EVENT_MOD) + return memif_mod_epoll_fd (ms, fde, evt); - return memif_add_epoll_fd (fd, evt); + return memif_add_epoll_fd (ms, fde, evt); } -int -add_list_elt (memif_list_elt_t * e, memif_list_elt_t ** list, uint16_t * len) +static void +memif_control_fd_update_register (memif_socket_t *ms, + memif_control_fd_update_t *cb) { - libmemif_main_t *lm = &libmemif_main; - - int i; - for (i = 0; i < *len; i++) - { - if ((*list)[i].data_struct == NULL) - { - (*list)[i].key = e->key; - (*list)[i].data_struct = e->data_struct; - return i; - } - } - memif_list_elt_t *tmp; - tmp = realloc (*list, sizeof (memif_list_elt_t) * *len * 2); - if (tmp == NULL) - return -1; + ms->args.on_control_fd_update = cb; +} - for (i = *len; i < *len * 2; i++) - { - tmp[i].key = -1; - tmp[i].data_struct = NULL; - } +void +memif_register_external_region (memif_socket_handle_t sock, + 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) +{ + memif_socket_t *ms = (memif_socket_t *) sock; + ms->add_external_region = ar; + ms->get_external_region_addr = gr; + ms->del_external_region = dr; + ms->get_external_buffer_offset = go; +} - tmp[*len].key = e->key; - tmp[*len].data_struct = e->data_struct; - i = *len; - *len = *len * 2; - *list = tmp; +static void +memif_alloc_register (memif_socket_t *ms, memif_alloc_t *ma) +{ + ms->args.alloc = ma; +} - return i; +static void +memif_realloc_register (memif_socket_t *ms, memif_realloc_t *mr) +{ + ms->args.realloc = mr; } -int -get_list_elt (memif_list_elt_t ** e, memif_list_elt_t * list, uint16_t len, - int key) +static void +memif_free_register (memif_socket_t *ms, memif_free_t *mf) { - if (key == -1) - { - *e = NULL; - return -1; - } - int i; - for (i = 0; i < len; i++) - { - if (list[i].key == key) - { - *e = &list[i]; - return 0; - } - } - *e = NULL; - return -1; + ms->args.free = mf; } -/* does not free memory, only marks element as free */ -int -free_list_elt (memif_list_elt_t * list, uint16_t len, int key) +static inline memif_ring_t * +memif_get_ring (memif_connection_t * conn, memif_ring_type_t type, + uint16_t ring_num) { - int i; - for (i = 0; i < len; i++) - { - if (list[i].key == key) - { - list[i].key = -1; - list[i].data_struct = NULL; - return 0; - } - } + if (&conn->regions[0] == NULL) + return NULL; + void *p = conn->regions[0].addr; + int ring_size = + sizeof (memif_ring_t) + + sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size); + p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size; - return -1; + return (memif_ring_t *) p; } int -free_list_elt_ctx (memif_list_elt_t * list, uint16_t len, - memif_connection_t * ctx) +memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, + uint16_t qid) { - int i; - for (i = 0; i < len; i++) - { - if (list[i].key == -1) - { - if (list[i].data_struct == ctx) - { - list[i].data_struct = NULL; - return 0; - } - } - } + memif_connection_t *conn = (memif_connection_t *) c; + if (conn == NULL) + return MEMIF_ERR_NOCONN; + uint8_t num = + (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn-> + run_args.num_m2s_rings; + if (qid >= num) + return MEMIF_ERR_QID; - return -1; + conn->rx_queues[qid].ring->flags = rx_mode; + DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags); + return MEMIF_ERR_SUCCESS; } -static void -memif_control_fd_update_register (memif_control_fd_update_t * cb) +int +memif_poll_cancel_handler (memif_fd_event_type_t type, void *private_ctx) { - libmemif_main_t *lm = &libmemif_main; - lm->control_fd_update = cb; + return MEMIF_ERR_POLL_CANCEL; } int -memif_init (memif_control_fd_update_t * on_control_fd_update, char *app_name) +memif_connect_handler (memif_fd_event_type_t type, void *private_ctx) { - int err = MEMIF_ERR_SUCCESS; /* 0 */ - libmemif_main_t *lm = &libmemif_main; + memif_socket_t *ms = (memif_socket_t *) private_ctx; + memif_connection_t *c; - if (app_name) + if (ms->timer_fd >= 0) { - lm->app_name = malloc (strlen (app_name) + sizeof (char)); - memset (lm->app_name, 0, strlen (app_name) + sizeof (char)); - strncpy ((char *) lm->app_name, app_name, strlen (app_name)); + uint64_t u64; + /* + Have to read the timer fd else it stays read-ready + and makes epoll_pwait() return without sleeping + */ + read (ms->timer_fd, &u64, sizeof (u64)); } - else + + /* loop ms->slave_interfaces and request connection for disconnected ones */ + TAILQ_FOREACH (c, &ms->slave_interfaces, next) + { + /* connected or connecting */ + if (c->control_channel != NULL) + continue; + + /* ignore errors */ + memif_request_connection (c); + } + + return MEMIF_ERR_SUCCESS; +} + +int +memif_set_connection_request_timer (memif_socket_handle_t sock, + struct itimerspec timer) +{ + memif_socket_t *ms = (memif_socket_t *) sock; + memif_fd_event_t fde; + memif_fd_event_data_t *fdata; + void *ctx; + + if (ms == NULL) + return MEMIF_ERR_INVAL_ARG; + + if (ms->timer_fd < 0) { - lm->app_name = malloc (strlen (MEMIF_DEFAULT_APP_NAME) + sizeof (char)); - memset (lm->app_name, 0, strlen (app_name) + sizeof (char)); - strncpy ((char *) lm->app_name, MEMIF_DEFAULT_APP_NAME, - strlen (MEMIF_DEFAULT_APP_NAME)); + /* only create new timer if there is a valid interval */ + if (timer.it_interval.tv_sec == 0 && timer.it_interval.tv_nsec == 0) + return MEMIF_ERR_SUCCESS; + + /* create timerfd */ + ms->timer_fd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); + if (ms->timer_fd < 0) + return memif_syscall_error_handler (errno); + + /* start listening for events */ + fdata = ms->args.alloc (sizeof (*fdata)); + fdata->event_handler = memif_connect_handler; + fdata->private_ctx = ms; + + fde.fd = ms->timer_fd; + fde.type = MEMIF_FD_EVENT_READ; + fde.private_ctx = fdata; + + ctx = ms->epfd != -1 ? ms : ms->private_ctx; + ms->args.on_control_fd_update (fde, ctx); } - /* register control fd update callback */ - if (on_control_fd_update != NULL) - memif_control_fd_update_register (on_control_fd_update); + ms->args.connection_request_timer = timer; + + /* arm the timer */ + if (timerfd_settime (ms->timer_fd, 0, &ms->args.connection_request_timer, + NULL) < 0) + return memif_syscall_error_handler (errno); + + return MEMIF_ERR_SUCCESS; +} + +int +memif_create_socket (memif_socket_handle_t *sock, memif_socket_args_t *args, + void *private_ctx) +{ + memif_socket_t *ms = (memif_socket_t *) * sock; + memif_fd_event_t fde; + memif_fd_event_data_t *fdata; + int i, err = MEMIF_ERR_SUCCESS; + void *ctx; + + /* allocate memif_socket_t */ + ms = NULL; + if (args->alloc != NULL) + ms = args->alloc (sizeof (memif_socket_t)); else + ms = malloc (sizeof (memif_socket_t)); + if (ms == NULL) { - memif_epfd = epoll_create (1); - memif_control_fd_update_register (memif_control_fd_update); - DBG ("libmemif event polling initialized"); + err = MEMIF_ERR_NOMEM; + goto error; } - memset (&lm->ms, 0, sizeof (memif_socket_t)); + /* default values */ + memset (ms, 0, sizeof (memif_socket_t)); + ms->epfd = -1; + ms->listener_fd = -1; + ms->poll_cancel_fd = -1; + ms->timer_fd = -1; - lm->control_list_len = 2; - lm->interrupt_list_len = 2; - lm->listener_list_len = 1; - lm->pending_list_len = 1; + /* copy arguments to internal struct */ + memcpy (&ms->args, args, sizeof (*args)); + ms->private_ctx = private_ctx; - lm->control_list = - malloc (sizeof (memif_list_elt_t) * lm->control_list_len); - lm->interrupt_list = - malloc (sizeof (memif_list_elt_t) * lm->interrupt_list_len); - lm->listener_list = - malloc (sizeof (memif_list_elt_t) * lm->listener_list_len); - lm->pending_list = - malloc (sizeof (memif_list_elt_t) * lm->pending_list_len); + if (ms->args.alloc == NULL) + memif_alloc_register (ms, malloc); + if (ms->args.realloc == NULL) + memif_realloc_register (ms, realloc); + if (ms->args.free == NULL) + memif_free_register (ms, free); - int i; - 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->listener_list_len; i++) - { - lm->listener_list[i].key = -1; - lm->listener_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; - } + TAILQ_INIT (&ms->master_interfaces); + TAILQ_INIT (&ms->slave_interfaces); - lm->disconn_slaves = 0; + /* FIXME: implement connection request timer */ - lm->timerfd = timerfd_create (CLOCK_REALTIME, TFD_NONBLOCK); - if (lm->timerfd < 0) + /* initialize internal epoll */ + if (ms->args.on_control_fd_update == NULL) { - err = errno; - DBG ("timerfd: %s", strerror (err)); - return memif_syscall_error_handler (err); + ms->epfd = epoll_create (1); + /* register default fd update callback */ + memif_control_fd_update_register (ms, memif_control_fd_update); + ms->poll_cancel_fd = eventfd (0, EFD_NONBLOCK); + if (ms->poll_cancel_fd < 0) + { + err = errno; + DBG ("eventfd: %s", strerror (err)); + return memif_syscall_error_handler (err); + } + /* add interrupt fd to epfd */ + fdata = ms->args.alloc (sizeof (*fdata)); + fdata->event_handler = memif_poll_cancel_handler; + fdata->private_ctx = ms; + + fde.fd = ms->poll_cancel_fd; + fde.type = MEMIF_FD_EVENT_READ; + fde.private_ctx = fdata; + + ctx = ms->epfd != -1 ? ms : ms->private_ctx; + ms->args.on_control_fd_update (fde, ctx); } - lm->arm.it_value.tv_sec = 2; - lm->arm.it_value.tv_nsec = 0; - lm->arm.it_interval.tv_sec = 2; - lm->arm.it_interval.tv_nsec = 0; - memset (&lm->disarm, 0, sizeof (lm->disarm)); + err = + memif_set_connection_request_timer (ms, ms->args.connection_request_timer); + if (err != MEMIF_ERR_SUCCESS) + goto error; + + *sock = ms; + + return err; - if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0) +error: + if (ms != NULL) { - DBG ("callback type memif_control_fd_update_t error!"); - return MEMIF_ERR_CB_FDUPDATE; + ms->args.free (ms); + if (ms->epfd != -1) + close (ms->epfd); + if (ms->poll_cancel_fd != -1) + close (ms->poll_cancel_fd); } + return err; +} - return 0; +memif_socket_handle_t +memif_get_socket_handle (memif_conn_handle_t conn) +{ + memif_connection_t *c = (memif_connection_t *) conn; + + if (c == NULL) + return NULL; + + return c->args.socket; } -static inline memif_ring_t * -memif_get_ring (memif_connection_t * conn, memif_ring_type_t type, - uint16_t ring_num) +const char * +memif_get_socket_path (memif_socket_handle_t sock) { - if (&conn->regions[0] == NULL) + memif_socket_t *ms = (memif_socket_t *) sock; + + if (ms == NULL) return NULL; - void *p = conn->regions[0].shm; - int ring_size = - sizeof (memif_ring_t) + - sizeof (memif_desc_t) * (1 << conn->run_args.log2_ring_size); - p += (ring_num + type * conn->run_args.num_s2m_rings) * ring_size; - return (memif_ring_t *) p; + return ms->args.path; } int -memif_set_rx_mode (memif_conn_handle_t c, memif_rx_mode_t rx_mode, - uint16_t qid) +memif_get_listener_fd (memif_socket_handle_t sock) { - memif_connection_t *conn = (memif_connection_t *) c; - if (conn == NULL) - return MEMIF_ERR_NOCONN; - uint8_t num = - (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->run_args. - num_m2s_rings; - if (qid >= num) - return MEMIF_ERR_QID; + memif_socket_t *ms = (memif_socket_t *) sock; + + if (ms == NULL) + return -1; + + return ms->listener_fd; +} + +int +memif_set_listener_fd (memif_socket_handle_t sock, int fd) +{ + memif_socket_t *ms = (memif_socket_t *) sock; + memif_fd_event_t fde; + memif_fd_event_data_t *fdata; + void *ctx; + + if ((ms == NULL) || (fd < 0)) + return MEMIF_ERR_INVAL_ARG; + + fdata = ms->args.alloc (sizeof (*fdata)); + if (fdata == NULL) + return MEMIF_ERR_NOMEM; + + ms->listener_fd = fd; + + fdata->event_handler = memif_listener_handler; + fdata->private_ctx = ms; + ctx = ms->epfd != -1 ? ms : ms->private_ctx; + /* send fd to epoll */ + fde.fd = ms->listener_fd; + fde.type = MEMIF_FD_EVENT_READ; + fde.private_ctx = fdata; + ms->args.on_control_fd_update (fde, ctx); - conn->rx_queues[qid].ring->flags = rx_mode; - DBG ("rx_mode flag: %u", conn->rx_queues[qid].ring->flags); return MEMIF_ERR_SUCCESS; } 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) +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_on_interrupt_t *on_interrupt, void *private_ctx) { - int err, i, index, sockfd = -1; - memif_list_elt_t list_elt; + int err, index = 0; memif_connection_t *conn = (memif_connection_t *) * c; + memif_socket_t *ms = (memif_socket_t *) args->socket; + if (conn != NULL) { DBG ("This handle already points to existing memif."); return MEMIF_ERR_CONN; } - conn = (memif_connection_t *) malloc (sizeof (memif_connection_t)); + + if (ms == NULL) + { + DBG ("Missing memif socket"); + return MEMIF_ERR_INVAL_ARG; + } + + conn = (memif_connection_t *) ms->args.alloc (sizeof (*conn)); if (conn == NULL) { - err = memif_syscall_error_handler (errno); + err = MEMIF_ERR_NOMEM; goto error; } memset (conn, 0, sizeof (memif_connection_t)); - libmemif_main_t *lm = &libmemif_main; - conn->args.interface_id = args->interface_id; if (args->log2_ring_size == 0) args->log2_ring_size = MEMIF_DEFAULT_LOG2_RING_SIZE; + else if (args->log2_ring_size > MEMIF_MAX_LOG2_RING_SIZE) + { + err = MEMIF_ERR_MAX_RING; + goto error; + } if (args->buffer_size == 0) args->buffer_size = MEMIF_DEFAULT_BUFFER_SIZE; if (args->num_s2m_rings == 0) @@ -563,377 +685,247 @@ memif_create (memif_conn_handle_t * c, memif_conn_args_t * args, conn->args.log2_ring_size = args->log2_ring_size; conn->args.is_master = args->is_master; conn->args.mode = args->mode; - conn->msg_queue = NULL; + conn->args.socket = args->socket; conn->regions = NULL; conn->tx_queues = NULL; conn->rx_queues = NULL; - conn->fd = -1; + conn->control_channel = NULL; conn->on_connect = on_connect; conn->on_disconnect = on_disconnect; conn->on_interrupt = on_interrupt; conn->private_ctx = private_ctx; memset (&conn->run_args, 0, sizeof (memif_conn_run_args_t)); - uint8_t l = strlen ((char *) args->interface_name); - strncpy ((char *) conn->args.interface_name, (char *) args->interface_name, + uint8_t l = sizeof (conn->args.interface_name); + strlcpy ((char *) conn->args.interface_name, (char *) args->interface_name, l); - l = strlen ((char *) args->instance_name); - strncpy ((char *) conn->args.instance_name, (char *) args->instance_name, - l); + if ((l = strlen ((char *) args->secret)) > 0) + strlcpy ((char *) conn->args.secret, (char *) args->secret, + sizeof (conn->args.secret)); - /* allocate and initialize socket_filename so it can be copyed to sun_path - without memory leaks */ - conn->args.socket_filename = malloc (sizeof (char *) * 108); - memset (conn->args.socket_filename, 0, 108 * sizeof (char *)); + if (args->is_master) + TAILQ_INSERT_TAIL (&ms->master_interfaces, conn, next); + else + TAILQ_INSERT_TAIL (&ms->slave_interfaces, conn, next); - if (args->socket_filename) + err = memif_request_connection (conn); + if (err != MEMIF_ERR_SUCCESS && err != MEMIF_ERR_CONNREFUSED) { - if (conn->args.socket_filename == NULL) - { - err = memif_syscall_error_handler (errno); - goto error; - } - strncpy ((char *) conn->args.socket_filename, - (char *) args->socket_filename, - strlen ((char *) args->socket_filename)); + if (args->is_master) + TAILQ_REMOVE (&ms->master_interfaces, conn, next); + else + TAILQ_REMOVE (&ms->slave_interfaces, conn, next); + goto error; } - else + + *c = conn; + + return 0; + +error: + if (conn != NULL) + ms->args.free (conn); + *c = conn = NULL; + return err; +} + +static inline int +memif_path_is_abstract (const char *filename) +{ + return (filename[0] == '@'); +} + +int +memif_request_connection (memif_conn_handle_t c) +{ + memif_connection_t *conn = (memif_connection_t *) c; + memif_socket_t *ms; + int err = MEMIF_ERR_SUCCESS; + int sockfd = -1; + struct sockaddr_un un = { 0 }; + struct stat file_stat; + int on = 1; + memif_control_channel_t *cc = NULL; + memif_fd_event_t fde; + memif_fd_event_data_t *fdata = NULL; + int sunlen = sizeof (un); + void *ctx; + + if (conn == NULL) + return MEMIF_ERR_NOCONN; + + ms = (memif_socket_t *) conn->args.socket; + + /* if control channel is assigned, the interface is either connected or + * connecting */ + if (conn->control_channel != NULL) + return MEMIF_ERR_ALRCONN; + /* if interface is master and the socket is already listener we are done */ + if (conn->args.is_master && (ms->listener_fd != -1)) + return MEMIF_ERR_SUCCESS; + + sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); + if (sockfd < 0) { - uint16_t sdl = strlen (MEMIF_DEFAULT_SOCKET_DIR); - uint16_t sfl = strlen (MEMIF_DEFAULT_SOCKET_FILENAME); - if (conn->args.socket_filename == NULL) - { - err = memif_syscall_error_handler (errno); - goto error; - } - strncpy ((char *) conn->args.socket_filename, - MEMIF_DEFAULT_SOCKET_DIR, sdl); - conn->args.socket_filename[sdl] = '/'; - strncpy ((char *) (conn->args.socket_filename + 1 + sdl), - MEMIF_DEFAULT_SOCKET_FILENAME, sfl); + err = memif_syscall_error_handler (errno); + goto error; } - if (args->secret) + un.sun_family = AF_UNIX; + + /* use memcpy to support abstract socket + * ms->args.path is already a valid socket path + */ + memcpy (un.sun_path, ms->args.path, sizeof (un.sun_path) - 1); + + /* allocate fd event data */ + fdata = ms->args.alloc (sizeof (*fdata)); + if (fdata == NULL) { - l = strlen ((char *) args->secret); - strncpy ((char *) conn->args.secret, (char *) args->secret, l); + err = MEMIF_ERR_NOMEM; + goto error; } - if (conn->args.is_master) + if (memif_path_is_abstract (ms->args.path)) { - conn->run_args.buffer_size = conn->args.buffer_size; - memif_socket_t *ms; - memif_list_elt_t elt; - for (i = 0; i < lm->listener_list_len; i++) - { - if ((ms = - (memif_socket_t *) lm->listener_list[i].data_struct) != NULL) - { - if (strncmp - ((char *) ms->filename, (char *) conn->args.socket_filename, - strlen ((char *) ms->filename)) == 0) - { - /* add interface to listener socket */ - elt.key = conn->args.interface_id; - *c = elt.data_struct = conn; - add_list_elt (&elt, &ms->interface_list, - &ms->interface_list_len); - ms->use_count++; - conn->listener_fd = ms->fd; - break; - } - } - else - { - struct stat file_stat; - if (stat ((char *) conn->args.socket_filename, &file_stat) == 0) - { - if (S_ISSOCK (file_stat.st_mode)) - unlink ((char *) conn->args.socket_filename); - else - return memif_syscall_error_handler (errno); - } - DBG ("creating socket file"); - ms = malloc (sizeof (memif_socket_t)); - ms->filename = - malloc (strlen ((char *) conn->args.socket_filename) + - sizeof (char)); - memset (ms->filename, 0, - strlen ((char *) conn->args.socket_filename) + - sizeof (char)); - strncpy ((char *) ms->filename, - (char *) conn->args.socket_filename, - strlen ((char *) conn->args.socket_filename)); - ms->interface_list_len = 1; - ms->interface_list = - malloc (sizeof (memif_list_elt_t) * ms->interface_list_len); - ms->interface_list[0].key = -1; - ms->interface_list[0].data_struct = NULL; - struct sockaddr_un un = { 0 }; - int on = 1; - - ms->fd = socket (AF_UNIX, SOCK_SEQPACKET, 0); - if (ms->fd < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - DBG ("socket %d created", ms->fd); - un.sun_family = AF_UNIX; - strncpy ((char *) un.sun_path, (char *) ms->filename, - sizeof (un.sun_path) - 1); - DBG ("sockopt"); - if (setsockopt - (ms->fd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - DBG ("bind"); - if (bind (ms->fd, (struct sockaddr *) &un, sizeof (un)) < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - DBG ("listen"); - if (listen (ms->fd, 1) < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - DBG ("stat"); - if (stat ((char *) ms->filename, &file_stat) < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - - /* add interface to listener socket */ - elt.key = conn->args.interface_id; - *c = elt.data_struct = conn; - add_list_elt (&elt, &ms->interface_list, - &ms->interface_list_len); - ms->use_count = 1; - conn->listener_fd = ms->fd; - - /* add listener socket to libmemif main */ - elt.key = ms->fd; - elt.data_struct = ms; - add_list_elt (&elt, &lm->listener_list, &lm->listener_list_len); - lm->control_fd_update (ms->fd, MEMIF_FD_EVENT_READ); - break; - } - } + /* Ensure the string is NULL terminated */ + un.sun_path[sizeof (un.sun_path) - 1] = '\0'; + /* sunlen is strlen(un.sun_path) + sizeof(un.sun_family) */ + sunlen = strlen (un.sun_path) + (sizeof (un) - sizeof (un.sun_path)); + /* Handle abstract socket by converting '@' -> '\0' */ + un.sun_path[0] = '\0'; } - else + + if (conn->args.is_master != 0) { - if (lm->disconn_slaves == 0) + /* Configure socket optins */ + if (setsockopt (sockfd, SOL_SOCKET, SO_PASSCRED, &on, sizeof (on)) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + if (bind (sockfd, (struct sockaddr *) &un, sunlen) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + if (listen (sockfd, 1) < 0) + { + err = memif_syscall_error_handler (errno); + goto error; + } + if (!memif_path_is_abstract (ms->args.path)) { - if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) + /* Verify that the socket was created */ + if (stat ((char *) ms->args.path, &file_stat) < 0) { err = memif_syscall_error_handler (errno); goto error; } } - lm->disconn_slaves++; + /* assign listener fd */ + ms->listener_fd = sockfd; - list_elt.key = -1; - *c = list_elt.data_struct = conn; - if ((index = - add_list_elt (&list_elt, &lm->control_list, - &lm->control_list_len)) < 0) + fdata->event_handler = memif_listener_handler; + fdata->private_ctx = ms; + } + else + { + cc = ms->args.alloc (sizeof (*cc)); + if (cc == NULL) { err = MEMIF_ERR_NOMEM; goto error; } + if (connect (sockfd, (struct sockaddr *) &un, sunlen) != 0) + { + err = MEMIF_ERR_CONNREFUSED; + goto error; + } + + /* Create control channel */ + cc->fd = sockfd; + cc->sock = ms; + cc->conn = conn; + TAILQ_INIT (&cc->msg_queue); + + /* assign control channel to endpoint */ + conn->control_channel = cc; + + fdata->event_handler = memif_control_channel_handler; + fdata->private_ctx = cc; } - conn->index = index; + /* if event polling is done internally, send memif socket as context */ + ctx = ms->epfd != -1 ? ms : ms->private_ctx; + /* send fd to epoll */ + fde.fd = sockfd; + fde.type = MEMIF_FD_EVENT_READ; + fde.private_ctx = fdata; + ms->args.on_control_fd_update (fde, ctx); - return 0; + return err; error: if (sockfd > 0) close (sockfd); sockfd = -1; - if (conn->args.socket_filename) - free (conn->args.socket_filename); - if (conn != NULL) - free (conn); - *c = conn = NULL; + if (fdata != NULL) + ms->args.free (fdata); + fdata = NULL; + if (cc != NULL) + ms->args.free (cc); + conn->control_channel = cc = NULL; return err; } int -memif_control_fd_handler (int fd, uint8_t events) +memif_control_fd_handler (void *ptr, memif_fd_event_type_t events) { - int i, rv, sockfd = -1, err = MEMIF_ERR_SUCCESS; /* 0 */ - uint16_t num; - memif_list_elt_t *e = NULL; - memif_connection_t *conn; - libmemif_main_t *lm = &libmemif_main; - if (fd == lm->timerfd) - { - uint64_t b; - ssize_t size; - size = read (fd, &b, sizeof (b)); - 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; - - struct sockaddr_un sun; - sockfd = socket (AF_UNIX, SOCK_SEQPACKET, 0); - if (sockfd < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - - sun.sun_family = AF_UNIX; + memif_fd_event_data_t *fdata = (memif_fd_event_data_t *) ptr; - strncpy (sun.sun_path, conn->args.socket_filename, - sizeof (sun.sun_path) - 1); + if (fdata == NULL) + return MEMIF_ERR_INVAL_ARG; - if (connect (sockfd, (struct sockaddr *) &sun, - sizeof (struct sockaddr_un)) == 0) - { - conn->fd = sockfd; - conn->read_fn = memif_conn_fd_read_ready; - conn->write_fn = memif_conn_fd_write_ready; - conn->error_fn = memif_conn_fd_error; - - lm->control_list[conn->index].key = conn->fd; - - lm->control_fd_update (sockfd, - MEMIF_FD_EVENT_READ | - MEMIF_FD_EVENT_WRITE); - - lm->disconn_slaves--; - if (lm->disconn_slaves == 0) - { - if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) - < 0) - { - err = memif_syscall_error_handler (errno); - goto error; - } - } - } - else - { - err = memif_syscall_error_handler (errno); - goto error; - } - } - } - } - 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->listener_list, lm->listener_list_len, fd); - if (e != NULL) - { - memif_conn_fd_accept_ready ((memif_socket_t *) e->data_struct); - return MEMIF_ERR_SUCCESS; - } - - get_list_elt (&e, lm->pending_list, lm->pending_list_len, fd); - if (e != NULL) - { - memif_read_ready (fd); - return MEMIF_ERR_SUCCESS; - } + return fdata->event_handler (events, fdata->private_ctx); +} - 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; - } - } - } +int +memif_interrupt_handler (memif_fd_event_type_t type, void *private_ctx) +{ + memif_interrupt_t *idata = (memif_interrupt_t *) private_ctx; - return MEMIF_ERR_SUCCESS; /* 0 */ + if (idata == NULL) + return MEMIF_ERR_INVAL_ARG; -error: - if (sockfd > 0) - close (sockfd); - sockfd = -1; - return err; + return idata->c->on_interrupt (idata->c, idata->c->private_ctx, idata->qid); } int -memif_poll_event (int timeout) +memif_poll_event (memif_socket_handle_t sock, int timeout) { - libmemif_main_t *lm = &libmemif_main; - memif_list_elt_t *elt; - struct epoll_event evt, *e; - int en = 0, err = MEMIF_ERR_SUCCESS, i = 0; /* 0 */ - uint16_t num; - uint32_t events = 0; + memif_socket_t *ms = (memif_socket_t *) sock; + struct epoll_event evt; + int en = 0, err = MEMIF_ERR_SUCCESS; /* 0 */ + memif_fd_event_type_t events = 0; + uint64_t counter = 0; + ssize_t r = 0; + sigset_t sigset; + + if (ms == NULL) + return MEMIF_ERR_INVAL_ARG; + memset (&evt, 0, sizeof (evt)); evt.events = EPOLLIN | EPOLLOUT; - sigset_t sigset; sigemptyset (&sigset); - en = epoll_pwait (memif_epfd, &evt, 1, timeout, &sigset); + en = epoll_pwait (ms->epfd, &evt, 1, timeout, &sigset); if (en < 0) { - DBG ("epoll_pwait: %s", strerror (errno)); - return -1; + err = errno; + DBG ("epoll_pwait: %s", strerror (err)); + return memif_syscall_error_handler (err); } if (en > 0) { @@ -943,200 +935,191 @@ memif_poll_event (int timeout) 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 memif_control_fd_handler (evt.data.ptr, events); } - return 0; + return MEMIF_ERR_SUCCESS; } -static void -memif_msg_queue_free (memif_msg_queue_elt_t ** e) +int +memif_cancel_poll_event (memif_socket_handle_t sock) { - if (*e == NULL) - return; - memif_msg_queue_free (&(*e)->next); - free (*e); - *e = NULL; - return; + memif_socket_t *ms = (memif_socket_t *) sock; + uint64_t counter = 1; + ssize_t w = 0; + + if (ms->poll_cancel_fd == -1) + return MEMIF_ERR_INVAL_ARG; + w = write (ms->poll_cancel_fd, &counter, sizeof (counter)); + if (w < sizeof (counter)) + return MEMIF_ERR_INT_WRITE; + + return MEMIF_ERR_SUCCESS; +} + +void +memif_close_queues (memif_socket_t *ms, memif_queue_t *queues, int nqueues) +{ + memif_fd_event_t fde; + memif_queue_t *mq; + void *ctx; + + int i; + for (i = 0; i < nqueues; i++) + { + mq = &queues[i]; + if (mq != NULL) + { + if (mq->int_fd > 0) + { + /* Stop listening for events */ + fde.fd = mq->int_fd; + fde.type = MEMIF_FD_EVENT_DEL; + ctx = ms->epfd != -1 ? ms : ms->private_ctx; + ms->args.on_control_fd_update (fde, ctx); + close (mq->int_fd); + } + mq->int_fd = -1; + } + } } /* send disconnect msg and close interface */ int memif_disconnect_internal (memif_connection_t * c) { - 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; + memif_socket_t *ms = (memif_socket_t *) c->args.socket; + memif_fd_event_t fde; + void *ctx; c->on_disconnect ((void *) c, c->private_ctx); - if (c->fd > 0) - { - memif_msg_send_disconnect (c->fd, "interface deleted", 0); - lm->control_fd_update (c->fd, MEMIF_FD_EVENT_DEL); - close (c->fd); - } - get_list_elt (&e, lm->control_list, lm->control_list_len, c->fd); - if (e != NULL) - { - if (c->args.is_master) - free_list_elt (lm->control_list, lm->control_list_len, c->fd); - e->key = c->fd = -1; - } + /* Delete control channel */ + if (c->control_channel != NULL) + memif_delete_control_channel (c->control_channel); if (c->tx_queues != NULL) { - num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; - for (i = 0; i < num; i++) - { - mq = &c->tx_queues[i]; - if (mq != NULL) - { - if (mq->int_fd > 0) - close (mq->int_fd); - free_list_elt (lm->interrupt_list, lm->interrupt_list_len, - mq->int_fd); - mq->int_fd = -1; - } - } - free (c->tx_queues); + memif_close_queues (ms, c->tx_queues, c->tx_queues_num); + ms->args.free (c->tx_queues); c->tx_queues = NULL; } + c->tx_queues_num = 0; if (c->rx_queues != NULL) { - num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; - for (i = 0; i < num; i++) - { - mq = &c->rx_queues[i]; - if (mq != NULL) - { - if (mq->int_fd > 0) - { - if (c->on_interrupt != NULL) - lm->control_fd_update (mq->int_fd, MEMIF_FD_EVENT_DEL); - close (mq->int_fd); - } - free_list_elt (lm->interrupt_list, lm->interrupt_list_len, - mq->int_fd); - mq->int_fd = -1; - } - } - free (c->rx_queues); + memif_close_queues (ms, c->rx_queues, c->rx_queues_num); + ms->args.free (c->rx_queues); c->rx_queues = NULL; } + c->rx_queues_num = 0; + + /* TODO: Slave reuse regions */ - if (c->regions != NULL) + for (i = 0; i < c->regions_num; i++) { - if (munmap (c->regions[0].shm, c->regions[0].region_size) < 0) - return memif_syscall_error_handler (errno); - if (c->regions[0].fd > 0) - close (c->regions[0].fd); - c->regions[0].fd = -1; - free (c->regions); - c->regions = NULL; + if (&c->regions[i] == NULL) + continue; + if (c->regions[i].is_external != 0) + { + ms->del_external_region (c->regions[i].addr, + c->regions[i].region_size, c->regions[i].fd, + c->private_ctx); + } + else + { + if (munmap (c->regions[i].addr, c->regions[i].region_size) < 0) + return memif_syscall_error_handler (errno); + if (c->regions[i].fd > 0) + close (c->regions[i].fd); + c->regions[i].fd = -1; + } } + ms->args.free (c->regions); + c->regions = NULL; + c->regions_num = 0; memset (&c->run_args, 0, sizeof (memif_conn_run_args_t)); - memif_msg_queue_free (&c->msg_queue); + return err; +} - if (!(c->args.is_master)) +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->args.path; +} + +int +memif_delete_socket (memif_socket_handle_t * sock) +{ + memif_socket_t *ms = (memif_socket_t *) * sock; + memif_fd_event_t fde; + void *ctx; + + /* check if socket is in use */ + if (ms == NULL || !TAILQ_EMPTY (&ms->master_interfaces) || + !TAILQ_EMPTY (&ms->slave_interfaces)) + return MEMIF_ERR_INVAL_ARG; + + if (ms->listener_fd > 0) { - if (lm->disconn_slaves == 0) - { - if (timerfd_settime (lm->timerfd, 0, &lm->arm, NULL) < 0) - { - err = memif_syscall_error_handler (errno); - DBG_UNIX ("timerfd_settime: arm"); - } - } - lm->disconn_slaves++; + fde.fd = ms->listener_fd; + fde.type = MEMIF_FD_EVENT_DEL; + ctx = ms->epfd != -1 ? ms : ms->private_ctx; + ms->args.on_control_fd_update (fde, ctx); } + ms->listener_fd = -1; - return err; + if (ms->poll_cancel_fd > 0) + { + fde.fd = ms->poll_cancel_fd; + fde.type = MEMIF_FD_EVENT_DEL; + ctx = ms->epfd != -1 ? ms : ms->private_ctx; + ms->args.on_control_fd_update (fde, ctx); + } + ms->poll_cancel_fd = -1; + + if (ms->epfd > 0) + close (ms->epfd); + ms->epfd = -1; + + ms->args.free (ms); + *sock = ms = NULL; + + return MEMIF_ERR_SUCCESS; } int memif_delete (memif_conn_handle_t * conn) { memif_connection_t *c = (memif_connection_t *) * conn; + memif_socket_t *ms; + int err = MEMIF_ERR_SUCCESS; + if (c == NULL) { DBG ("no connection"); return MEMIF_ERR_NOCONN; } - libmemif_main_t *lm = &libmemif_main; - memif_list_elt_t *e = NULL; - memif_socket_t *ms = NULL; - - int err = MEMIF_ERR_SUCCESS; - if (c->fd > 0) - { - DBG ("DISCONNECTING"); - err = memif_disconnect_internal (c); - if (err == MEMIF_ERR_NOCONN) - return err; - } + err = memif_disconnect_internal (c); - free_list_elt_ctx (lm->control_list, lm->control_list_len, c); + ms = (memif_socket_t *) c->args.socket; if (c->args.is_master) - { - get_list_elt (&e, lm->listener_list, lm->listener_list_len, - c->listener_fd); - if (e != NULL) - { - ms = (memif_socket_t *) e->data_struct; - ms->use_count--; - free_list_elt (ms->interface_list, ms->interface_list_len, - c->args.interface_id); - if (ms->use_count <= 0) - { - lm->control_fd_update (c->listener_fd, MEMIF_FD_EVENT_DEL); - free_list_elt (lm->listener_list, lm->listener_list_len, - c->listener_fd); - close (c->listener_fd); - c->listener_fd = ms->fd = -1; - free (ms->interface_list); - ms->interface_list = NULL; - free (ms->filename); - ms->filename = NULL; - free (ms); - ms = NULL; - } - } - } + TAILQ_REMOVE (&ms->master_interfaces, c, next); else - { - lm->disconn_slaves--; - if (lm->disconn_slaves <= 0) - { - if (timerfd_settime (lm->timerfd, 0, &lm->disarm, NULL) < 0) - { - err = memif_syscall_error_handler (errno); - DBG ("timerfd_settime: disarm"); - } - } - } - - if (c->args.socket_filename) - free (c->args.socket_filename); - c->args.socket_filename = NULL; + TAILQ_REMOVE (&ms->slave_interfaces, c, next); + /* TODO: don't listen with empty interface queue */ - free (c); + ms->args.free (c); c = NULL; *conn = c; @@ -1146,107 +1129,135 @@ memif_delete (memif_conn_handle_t * conn) int memif_connect1 (memif_connection_t * c) { - libmemif_main_t *lm = &libmemif_main; - memif_region_t *mr = c->regions; + memif_socket_t *ms; + memif_region_t *mr; memif_queue_t *mq; int i; - uint16_t num; - if (mr != NULL) + if (c == NULL) + return MEMIF_ERR_INVAL_ARG; + + ms = (memif_socket_t *) c->args.socket; + + for (i = 0; i < c->regions_num; i++) { - if (!mr->shm) + mr = &c->regions[i]; + if (mr != NULL) { - if (mr->fd < 0) - return MEMIF_ERR_NO_SHMFD; + if (!mr->addr) + { + if (mr->is_external) + { + if (ms->get_external_region_addr == NULL) + return MEMIF_ERR_INVAL_ARG; + mr->addr = ms->get_external_region_addr ( + mr->region_size, mr->fd, c->private_ctx); + } + else + { + if (mr->fd < 0) + return MEMIF_ERR_NO_SHMFD; - if ((mr->shm = mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, + if ((mr->addr = + mmap (NULL, mr->region_size, PROT_READ | PROT_WRITE, MAP_SHARED, mr->fd, 0)) == MAP_FAILED) - { - return memif_syscall_error_handler (errno); + { + return memif_syscall_error_handler (errno); + } + } } } } - num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; - for (i = 0; i < num; i++) + for (i = 0; i < c->rx_queues_num; i++) { - mq = &c->tx_queues[i]; + mq = &c->rx_queues[i]; if (mq != NULL) { - mq->ring = c->regions[mq->region].shm + mq->offset; + mq->ring = c->regions[mq->region].addr + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) { - DBG ("wrong cookie on tx ring %u", i); + DBG ("wrong cookie on rx ring %u", i); return MEMIF_ERR_COOKIE; } - mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs = - 0; + mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0; } } - num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; - for (i = 0; i < num; i++) + + for (i = 0; i < c->tx_queues_num; i++) { - mq = &c->rx_queues[i]; + mq = &c->tx_queues[i]; if (mq != NULL) { - mq->ring = c->regions[mq->region].shm + mq->offset; + mq->ring = c->regions[mq->region].addr + mq->offset; if (mq->ring->cookie != MEMIF_COOKIE) { - DBG ("wrong cookie on rx ring %u", i); + DBG ("wrong cookie on tx ring %u", i); return MEMIF_ERR_COOKIE; } - mq->ring->head = mq->ring->tail = mq->last_head = mq->alloc_bufs = - 0; + mq->ring->head = mq->ring->tail = mq->last_head = mq->next_buf = 0; } } - lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); - return 0; } -int -memif_init_regions_and_queues (memif_connection_t * conn) +static inline int +memif_add_region (memif_connection_t *conn, uint8_t has_buffers) { - memif_ring_t *ring = NULL; - uint64_t buffer_offset; memif_region_t *r; - int i, j; - libmemif_main_t *lm = &libmemif_main; - memif_list_elt_t e; + memif_socket_t *ms = (memif_socket_t *) conn->args.socket; - conn->regions = (memif_region_t *) malloc (sizeof (memif_region_t)); - if (conn->regions == NULL) - return memif_syscall_error_handler (errno); - r = conn->regions; + r = ms->args.realloc (conn->regions, + sizeof (memif_region_t) * ++conn->regions_num); + if (r == NULL) + return MEMIF_ERR_NOMEM; - buffer_offset = - (conn->run_args.num_s2m_rings + - conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) + - sizeof (memif_desc_t) * - (1 << conn->run_args.log2_ring_size)); + conn->regions = r; + r = &conn->regions[conn->regions_num - 1]; + memset (r, 0, sizeof (memif_region_t)); + + if (has_buffers != 0) + { + r->buffer_offset = 0; + } + else + { + r->buffer_offset = + (conn->run_args.num_s2m_rings + + conn->run_args.num_m2s_rings) * (sizeof (memif_ring_t) + + sizeof (memif_desc_t) * + (1 << conn-> + run_args.log2_ring_size)); + } - r->region_size = buffer_offset + + r->region_size = (has_buffers == 0) ? r->buffer_offset : r->buffer_offset + conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) * (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings); if ((r->fd = memfd_create ("memif region 0", MFD_ALLOW_SEALING)) == -1) return memif_syscall_error_handler (errno); -/* - if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) - return memif_syscall_error_handler (errno); -*/ + + if ((fcntl (r->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1) + return memif_syscall_error_handler (errno); + if ((ftruncate (r->fd, r->region_size)) == -1) return memif_syscall_error_handler (errno); - if ((r->shm = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE, - MAP_SHARED, r->fd, 0)) == MAP_FAILED) + if ((r->addr = mmap (NULL, r->region_size, PROT_READ | PROT_WRITE, + MAP_SHARED, r->fd, 0)) == MAP_FAILED) return memif_syscall_error_handler (errno); + return MEMIF_ERR_SUCCESS; +} + +static inline int +memif_init_queues (memif_connection_t *conn) +{ + int i, j; + memif_ring_t *ring; + memif_socket_t *ms = (memif_socket_t *) conn->args.socket; + for (i = 0; i < conn->run_args.num_s2m_rings; i++) { ring = memif_get_ring (conn, MEMIF_RING_S2M, i); @@ -1257,10 +1268,11 @@ memif_init_regions_and_queues (memif_connection_t * conn) for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) { uint16_t slot = i * (1 << conn->run_args.log2_ring_size) + j; - ring->desc[j].region = 0; - ring->desc[j].offset = buffer_offset + + ring->desc[j].region = 1; + ring->desc[j].offset = + conn->regions[1].buffer_offset + (uint32_t) (slot * conn->run_args.buffer_size); - ring->desc[j].buffer_length = conn->run_args.buffer_size; + ring->desc[j].length = conn->run_args.buffer_size; } } for (i = 0; i < conn->run_args.num_m2s_rings; i++) @@ -1272,198 +1284,359 @@ memif_init_regions_and_queues (memif_connection_t * conn) ring->flags = 0; for (j = 0; j < (1 << conn->run_args.log2_ring_size); j++) { - uint16_t slot = - (i + - conn->run_args.num_s2m_rings) * + uint16_t slot = (i + conn->run_args.num_s2m_rings) * (1 << conn->run_args.log2_ring_size) + j; - ring->desc[j].region = 0; - ring->desc[j].offset = buffer_offset + + ring->desc[j].region = 1; + ring->desc[j].offset = + conn->regions[1].buffer_offset + (uint32_t) (slot * conn->run_args.buffer_size); - ring->desc[j].buffer_length = conn->run_args.buffer_size; + ring->desc[j].length = conn->run_args.buffer_size; } } memif_queue_t *mq; - mq = - (memif_queue_t *) malloc (sizeof (memif_queue_t) * - conn->run_args.num_s2m_rings); + mq = (memif_queue_t *) ms->args.alloc (sizeof (memif_queue_t) * + conn->run_args.num_s2m_rings); if (mq == NULL) - return memif_syscall_error_handler (errno); + return MEMIF_ERR_NOMEM; + int x; + for (x = 0; x < conn->run_args.num_s2m_rings; x++) { if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); - /* add int fd to interrupt fd list */ - e.key = mq[x].int_fd; - e.data_struct = conn; - add_list_elt (&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); mq[x].log2_ring_size = conn->run_args.log2_ring_size; mq[x].region = 0; mq[x].offset = - (void *) mq[x].ring - (void *) conn->regions[mq->region].shm; - mq[x].last_head = 0; - mq[x].alloc_bufs = 0; + (void *) mq[x].ring - (void *) conn->regions[mq->region].addr; + mq[x].last_head = mq[x].last_tail = 0; + mq[x].next_buf = 0; } conn->tx_queues = mq; + conn->tx_queues_num = conn->run_args.num_s2m_rings; - mq = - (memif_queue_t *) malloc (sizeof (memif_queue_t) * - conn->run_args.num_m2s_rings); + mq = (memif_queue_t *) ms->args.alloc (sizeof (memif_queue_t) * + conn->run_args.num_m2s_rings); if (mq == NULL) - return memif_syscall_error_handler (errno); + return MEMIF_ERR_NOMEM; + for (x = 0; x < conn->run_args.num_m2s_rings; x++) { if ((mq[x].int_fd = eventfd (0, EFD_NONBLOCK)) < 0) return memif_syscall_error_handler (errno); - /* add int fd to interrupt fd list */ - e.key = mq[x].int_fd; - e.data_struct = conn; - add_list_elt (&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); mq[x].log2_ring_size = conn->run_args.log2_ring_size; mq[x].region = 0; mq[x].offset = - (void *) mq[x].ring - (void *) conn->regions[mq->region].shm; - mq[x].last_head = 0; - mq[x].alloc_bufs = 0; + (void *) mq[x].ring - (void *) conn->regions[mq->region].addr; + mq[x].last_head = mq[x].last_tail = 0; + mq[x].next_buf = 0; } conn->rx_queues = mq; + conn->rx_queues_num = conn->run_args.num_m2s_rings; + + return MEMIF_ERR_SUCCESS; +} + +int +memif_init_regions_and_queues (memif_connection_t * conn) +{ + memif_region_t *r; + memif_socket_t *ms = (memif_socket_t *) conn->args.socket; + + /* region 0. rings */ + memif_add_region (conn, /* has_buffers */ 0); + + /* region 1. buffers */ + if (ms->add_external_region) + { + r = (memif_region_t *) ms->args.realloc ( + conn->regions, sizeof (memif_region_t) * ++conn->regions_num); + if (r == NULL) + return MEMIF_ERR_NOMEM; + conn->regions = r; + + conn->regions[1].region_size = + conn->run_args.buffer_size * (1 << conn->run_args.log2_ring_size) * + (conn->run_args.num_s2m_rings + conn->run_args.num_m2s_rings); + conn->regions[1].buffer_offset = 0; + ms->add_external_region (&conn->regions[1].addr, + conn->regions[1].region_size, + &conn->regions[1].fd, conn->private_ctx); + conn->regions[1].is_external = 1; + } + else + { + memif_add_region (conn, 1); + } + + memif_init_queues (conn); return 0; } int -memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t * bufs, uint16_t count, - uint16_t * count_out, uint16_t size) +memif_set_next_free_buffer (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t *buf) { memif_connection_t *c = (memif_connection_t *) conn; - if (c == NULL) + if (EXPECT_FALSE (c == NULL)) return MEMIF_ERR_NOCONN; - if (c->fd < 0) + if (EXPECT_FALSE (qid >= c->tx_queues_num)) + return MEMIF_ERR_QID; + if (EXPECT_FALSE (buf == NULL)) + return MEMIF_ERR_INVAL_ARG; + + uint16_t ring_size, ns; + memif_queue_t *mq = &c->tx_queues[qid]; + memif_ring_t *ring = mq->ring; + + ring_size = (1 << mq->log2_ring_size); + if (c->args.is_master) + ns = ring->head - mq->next_buf; + else + ns = ring_size - mq->next_buf + ring->tail; + + if ((mq->next_buf - buf->desc_index) > ns) + return MEMIF_ERR_INVAL_ARG; + + mq->next_buf = buf->desc_index; + + return MEMIF_ERR_SUCCESS; +} + +static void +memif_buffer_enq_at_idx_internal (memif_queue_t *from_q, memif_queue_t *to_q, + memif_buffer_t *buf, uint16_t slot) +{ + uint16_t from_mask = (1 << from_q->log2_ring_size) - 1; + uint16_t to_mask = (1 << to_q->log2_ring_size) - 1; + memif_desc_t *from_d, *to_d, tmp_d; + + /* Get the descriptors */ + from_d = &from_q->ring->desc[buf->desc_index & from_mask]; + to_d = &to_q->ring->desc[slot & to_mask]; + + /* Swap descriptors */ + tmp_d = *from_d; + *from_d = *to_d; + *to_d = tmp_d; + + /* Update descriptor index and queue for clients buffer */ + buf->desc_index = slot; + buf->queue = to_q; +} + +int +memif_buffer_requeue (memif_conn_handle_t conn, memif_buffer_t *buf_a, + memif_buffer_t *buf_b) +{ + memif_connection_t *c = (memif_connection_t *) conn; + if (EXPECT_FALSE (c == NULL)) + return MEMIF_ERR_NOCONN; + if (EXPECT_FALSE (c->args.is_master)) + return MEMIF_ERR_INVAL_ARG; + if ((buf_a == NULL) || (buf_b == NULL)) + return MEMIF_ERR_INVAL_ARG; + + int err; + /* store buf_a information */ + uint16_t index_a = buf_a->desc_index; + memif_queue_t *mq_a = buf_a->queue; + + /* swap buffers, buf_a was updated with new desc_index and queue */ + memif_buffer_enq_at_idx_internal ((memif_queue_t *) buf_a->queue, + (memif_queue_t *) buf_b->queue, buf_a, + buf_b->desc_index); + + /* update buf_b desc_index and queue */ + buf_b->desc_index = index_a; + buf_b->queue = mq_a; + + return MEMIF_ERR_SUCCESS; +} + +int +memif_buffer_enq_tx (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t * bufs, uint16_t count, + uint16_t * count_out) +{ + memif_connection_t *c = (memif_connection_t *) conn; + if (EXPECT_FALSE (c == NULL)) + return MEMIF_ERR_NOCONN; + if (EXPECT_FALSE (c->control_channel == NULL)) return MEMIF_ERR_DISCONNECTED; - uint8_t num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; - if (qid >= num) + if (EXPECT_FALSE (qid >= c->tx_queues_num)) return MEMIF_ERR_QID; + if (EXPECT_FALSE (!count_out)) + return MEMIF_ERR_INVAL_ARG; + if (EXPECT_FALSE (c->args.is_master)) + return MEMIF_ERR_INVAL_ARG; + memif_queue_t *mq = &c->tx_queues[qid]; memif_ring_t *ring = mq->ring; - memif_buffer_t *b0, *b1; - uint8_t chain_buf0, chain_buf1; + memif_buffer_t *b0; uint16_t mask = (1 << mq->log2_ring_size) - 1; - uint16_t s0, s1, ns; + uint16_t ring_size; + uint16_t ns; + memif_queue_t *bmq; + int err = MEMIF_ERR_SUCCESS; /* 0 */ *count_out = 0; - int i, err = MEMIF_ERR_SUCCESS; /* 0 */ - if (ring->tail != ring->head) + ring_size = (1 << mq->log2_ring_size); + + /* can only be called by slave */ + ns = ring_size - mq->next_buf + ring->tail; + + b0 = bufs; + + while (count && ns) { - if (ring->head > ring->tail) - ns = (1 << mq->log2_ring_size) - ring->head + ring->tail; - else - ns = ring->tail - ring->head; + /* Swaps the descriptors, updates next_buf pointer and updates client + * memif buffer */ + + memif_buffer_enq_at_idx_internal ((memif_queue_t *) b0->queue, mq, b0, + mq->next_buf); + + mq->next_buf++; /* mark the buffer as allocated */ + count--; + ns--; + b0++; + *count_out += 1; } - else - ns = (1 << mq->log2_ring_size); - /* (head == tail) ? receive function will asume that no packets are available */ - ns -= 1; + DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count, + mq->next_buf); - while (count && ns) + if (count) { - while ((count > 2) && (ns > 2)) - { - s0 = (ring->head + mq->alloc_bufs) & mask; - chain_buf0 = size / ring->desc[s0].buffer_length; - if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0)) - chain_buf0++; - - if (chain_buf0 > ns) - break; - - s1 = (ring->head + mq->alloc_bufs + chain_buf0) & mask; - chain_buf1 = size / ring->desc[s1].buffer_length; - if (((size % ring->desc[s1].buffer_length) != 0) || (size == 0)) - chain_buf1++; - - if ((chain_buf0 + chain_buf1) > ns) - break; - - b0 = (bufs + *count_out); - b1 = (bufs + *count_out + 1); - - b0->desc_index = s0; - b1->desc_index = s1; - ring->desc[s0].flags = 0; - ring->desc[s1].flags = 0; - b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0; - b1->buffer_len = ring->desc[s1].buffer_length * chain_buf1; - /* TODO: support multiple regions -> ring descriptor contains region index */ - b0->data = c->regions->shm + ring->desc[s0].offset; - b1->data = c->regions->shm + ring->desc[s1].offset; - - for (i = 0; i < (memif_min (chain_buf0, chain_buf1) - 1); i++) - { - ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; - ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; - DBG ("allocating chained buffers"); - } + DBG ("ring buffer full! qid: %u", qid); + err = MEMIF_ERR_NOBUF_RING; + } - if (chain_buf0 > chain_buf1) - { - for (; i < (chain_buf0 - 1); i++) - ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; - } - else - { - for (; i < (chain_buf1 - 1); i++) - ring->desc[(s1 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; - } + return err; +} - mq->alloc_bufs += chain_buf0 + chain_buf1; +int +memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, + memif_buffer_t * bufs, uint16_t count, + uint16_t * count_out, uint16_t size) +{ + memif_connection_t *c = (memif_connection_t *) conn; + if (EXPECT_FALSE (c == NULL)) + return MEMIF_ERR_NOCONN; + if (EXPECT_FALSE (c->control_channel == NULL)) + return MEMIF_ERR_DISCONNECTED; + uint8_t num = + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; + if (EXPECT_FALSE (qid >= num)) + return MEMIF_ERR_QID; + if (EXPECT_FALSE (!count_out)) + return MEMIF_ERR_INVAL_ARG; - DBG ("allocated ring slots %u, %u", s0, s1); - count -= 2; - ns -= chain_buf0 + chain_buf1; - *count_out += 2; - } - s0 = (ring->head + mq->alloc_bufs) & mask; + memif_socket_t *ms = (memif_socket_t *) c->args.socket; + memif_queue_t *mq = &c->tx_queues[qid]; + memif_ring_t *ring = mq->ring; + memif_buffer_t *b0; + uint16_t mask = (1 << mq->log2_ring_size) - 1; + uint16_t ring_size; + uint16_t ns; + int err = MEMIF_ERR_SUCCESS; /* 0 */ + uint16_t dst_left, src_left; + uint16_t saved_count; + uint16_t saved_next_buf; + uint16_t slot; + memif_buffer_t *saved_b; + *count_out = 0; + + ring_size = (1 << mq->log2_ring_size); + + if (c->args.is_master) + ns = ring->head - mq->next_buf; + else + ns = ring_size - mq->next_buf + ring->tail; + while (count && ns) + { b0 = (bufs + *count_out); - chain_buf0 = size / ring->desc[s0].buffer_length; - if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0)) - chain_buf0++; + saved_b = b0; + saved_count = count; + saved_next_buf = mq->next_buf; - if (chain_buf0 > ns) - break; + b0->desc_index = mq->next_buf; + ring->desc[mq->next_buf & mask].flags = 0; + b0->flags = 0; - b0->desc_index = s0; - ring->desc[s0].flags = 0; - b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0; - b0->data = c->regions->shm + ring->desc[s0].offset; + /* slave can produce buffer with original length */ + dst_left = (c->args.is_master) ? ring->desc[mq->next_buf & mask].length : + c->run_args.buffer_size; + src_left = size; - for (i = 0; i < (chain_buf0 - 1); i++) + while (src_left) { - ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT; - DBG ("allocating chained buffers"); - } + if (EXPECT_FALSE (dst_left == 0)) + { + if (count && ns) + { + *count_out += 1; + mq->next_buf++; + ns--; + + ring->desc[b0->desc_index & mask].flags |= + MEMIF_DESC_FLAG_NEXT; + b0->flags |= MEMIF_BUFFER_FLAG_NEXT; + + b0 = (bufs + *count_out); + b0->desc_index = mq->next_buf; + dst_left = (c->args.is_master) ? + ring->desc[mq->next_buf & mask].length : + c->run_args.buffer_size; + ring->desc[mq->next_buf & mask].flags = 0; + } + else + { + /* rollback allocated chain buffers */ + memset (saved_b, 0, sizeof (memif_buffer_t) + * (saved_count - count + 1)); + *count_out -= saved_count - count; + mq->next_buf = saved_next_buf; + goto no_ns; + } + } + b0->len = memif_min (dst_left, src_left); - mq->alloc_bufs += chain_buf0; + /* slave resets buffer offset */ + if (c->args.is_master == 0) + { + memif_desc_t *d = &ring->desc[slot & mask]; + if (ms->get_external_buffer_offset) + d->offset = ms->get_external_buffer_offset (c->private_ctx); + else + d->offset = d->offset - (d->offset % c->run_args.buffer_size); + } + b0->data = memif_get_buffer (c, ring, mq->next_buf & mask); + + src_left -= b0->len; + dst_left -= b0->len; + } - DBG ("allocated ring slot %u", s0); - count--; - ns -= chain_buf0; *count_out += 1; + mq->next_buf++; + ns--; + count--; } - DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count, - mq->alloc_bufs); +no_ns: + + DBG ("allocated: %u/%u bufs. Next buffer pointer %d", *count_out, count, + mq->next_buf); if (count) { @@ -1475,70 +1648,56 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid, } int -memif_buffer_free (memif_conn_handle_t conn, uint16_t qid, - memif_buffer_t * bufs, uint16_t count, - uint16_t * count_out) +memif_refill_queue (memif_conn_handle_t conn, uint16_t qid, uint16_t count, + uint16_t headroom) { memif_connection_t *c = (memif_connection_t *) conn; - if (c == NULL) + if (EXPECT_FALSE (c == NULL)) return MEMIF_ERR_NOCONN; - if (c->fd < 0) + if (EXPECT_FALSE (c->control_channel == NULL)) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; - if (qid >= num) + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; + if (EXPECT_FALSE (qid >= num)) return MEMIF_ERR_QID; - libmemif_main_t *lm = &libmemif_main; + memif_socket_t *ms = (memif_socket_t *) c->args.socket; memif_queue_t *mq = &c->rx_queues[qid]; memif_ring_t *ring = mq->ring; - uint16_t tail = ring->tail; uint16_t mask = (1 << mq->log2_ring_size) - 1; - uint8_t chain_buf0, chain_buf1; - memif_buffer_t *b0, *b1; - *count_out = 0; + uint16_t slot, counter = 0; - if (mq->alloc_bufs < count) - count = mq->alloc_bufs; - - while (count) + if (c->args.is_master) { - while (count > 2) - { - b0 = (bufs + *count_out); - b1 = (bufs + *count_out + 1); - chain_buf0 = - b0->buffer_len / ring->desc[b0->desc_index].buffer_length; - if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != - 0) - chain_buf0++; - chain_buf1 = - b1->buffer_len / ring->desc[b1->desc_index].buffer_length; - if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) != - 0) - chain_buf1++; - tail = (b1->desc_index + chain_buf1) & mask; - b0->data = NULL; - b1->data = NULL; - - count -= 2; - *count_out += 2; - mq->alloc_bufs -= chain_buf0 + chain_buf1; - } - b0 = (bufs + *count_out); - chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length; - if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0) - chain_buf0++; - tail = (b0->desc_index + chain_buf0) & mask; - b0->data = NULL; + MEMIF_MEMORY_BARRIER (); + ring->tail = + (ring->tail + count <= + mq->last_head) ? ring->tail + count : mq->last_head; + return MEMIF_ERR_SUCCESS; + } - count--; - *count_out += 1; - mq->alloc_bufs -= chain_buf0; + uint16_t head = ring->head; + slot = head; + uint16_t ns = (1 << mq->log2_ring_size) - head + mq->last_tail; + count = (count < ns) ? count : ns; + + memif_desc_t *d; + while (counter < count) + { + d = &ring->desc[slot & mask]; + d->region = 1; + d->length = c->run_args.buffer_size - headroom; + if (ms->get_external_buffer_offset) + d->offset = ms->get_external_buffer_offset (c->private_ctx); + else + d->offset = + d->offset - (d->offset % c->run_args.buffer_size) + headroom; + slot++; + counter++; } - MEMIF_MEORY_BARRIER (); - ring->tail = tail; - DBG ("tail: %u", ring->tail); + + MEMIF_MEMORY_BARRIER (); + ring->head = slot; return MEMIF_ERR_SUCCESS; /* 0 */ } @@ -1548,210 +1707,91 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t * bufs, uint16_t count, uint16_t * tx) { memif_connection_t *c = (memif_connection_t *) conn; - if (c == NULL) + if (EXPECT_FALSE (c == NULL)) return MEMIF_ERR_NOCONN; - if (c->fd < 0) + if (EXPECT_FALSE (c->control_channel == NULL)) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; - if (qid >= num) + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; + if (EXPECT_FALSE (qid >= num)) return MEMIF_ERR_QID; + if (EXPECT_FALSE (!tx)) + return MEMIF_ERR_INVAL_ARG; + memif_queue_t *mq = &c->tx_queues[qid]; memif_ring_t *ring = mq->ring; - uint16_t head = ring->head; uint16_t mask = (1 << mq->log2_ring_size) - 1; - uint8_t chain_buf0, chain_buf1; + memif_buffer_t *b0; + memif_desc_t *d; + int64_t data_offset; *tx = 0; - uint16_t curr_buf = 0; - memif_buffer_t *b0, *b1; - int i; + int err = MEMIF_ERR_SUCCESS; + + if (EXPECT_FALSE (count == 0)) + return MEMIF_ERR_SUCCESS; + + uint16_t index; + if (c->args.is_master) + index = ring->tail; + else + index = ring->head; while (count) { - while (count > 2) + b0 = (bufs + *tx); + /* set error to MEMIF_ERR_INVAL_ARG and finish the sending process + */ + if ((b0->desc_index & mask) != (index & mask)) { - b0 = (bufs + curr_buf); - b1 = (bufs + curr_buf + 1); - chain_buf0 = - b0->buffer_len / ring->desc[b0->desc_index].buffer_length; - if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != - 0) - chain_buf0++; - - chain_buf1 = - b1->buffer_len / ring->desc[b1->desc_index].buffer_length; - if ((b1->buffer_len % ring->desc[b1->desc_index].buffer_length) != - 0) - chain_buf1++; - - for (i = 0; i < memif_min (chain_buf0, chain_buf1); i++) - { - /* b0 */ - if (b0->data_len > - ring->desc[(b0->desc_index + i) & mask].buffer_length) - { - b0->data_len -= - ring->desc[(b0->desc_index + i) & mask].length = - ring->desc[(b0->desc_index + i) & mask].buffer_length; - } - else - { - ring->desc[(b0->desc_index + i) & mask].length = - b0->data_len; - b0->data_len = 0; - } - /* b1 */ - if (b1->data_len > - ring->desc[(b1->desc_index + i) & mask].buffer_length) - { - b1->data_len -= - ring->desc[(b1->desc_index + i) & mask].length = - ring->desc[(b1->desc_index + i) & mask].buffer_length; - } - else - { - ring->desc[(b1->desc_index + i) & mask].length = - b1->data_len; - b1->data_len = 0; - } -#ifdef MEMIF_DBG_SHM - print_bytes (b0->data + - ring->desc[(b0->desc_index + - i) & mask].buffer_length * - (chain_buf0 - 1), - ring->desc[(b0->desc_index + - i) & mask].buffer_length, DBG_TX_BUF); - print_bytes (b1->data + - ring->desc[(b1->desc_index + - i) & mask].buffer_length * - (chain_buf1 - 1), - ring->desc[(b1->desc_index + - i) & mask].buffer_length, DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ - } - - if (chain_buf0 > chain_buf1) + err = MEMIF_ERR_INVAL_ARG; + goto done; + } + d = &ring->desc[b0->desc_index & mask]; + d->length = b0->len; + d->flags = + ((b0->flags & MEMIF_BUFFER_FLAG_NEXT) == 1) ? MEMIF_DESC_FLAG_NEXT : 0; + if (!c->args.is_master) + { + // reset headroom + d->offset = d->offset - (d->offset % c->run_args.buffer_size); + // calculate offset from user data + data_offset = b0->data - (d->offset + c->regions[d->region].addr); + if (data_offset != 0) { - for (; i < chain_buf0; i++) + /* verify data offset and buffer length */ + if ((data_offset < 0) || + ((data_offset + b0->len) > c->run_args.buffer_size)) { - if (b0->data_len > - ring->desc[(b0->desc_index + i) & mask].buffer_length) - { - b0->data_len -= - ring->desc[(b0->desc_index + i) & mask].length = - ring->desc[(b0->desc_index + i) & mask].buffer_length; - } - else - { - ring->desc[(b0->desc_index + i) & mask].length = - b0->data_len; - b0->data_len = 0; - } -#ifdef MEMIF_DBG_SHM - print_bytes (b0->data + - ring->desc[(b0->desc_index + - i) & mask].buffer_length * - (chain_buf0 - 1), - ring->desc[(b0->desc_index + - i) & mask].buffer_length, - DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ + DBG ("slot: %d, data_offset: %ld, length: %d", + b0->desc_index & mask, data_offset, b0->len); + err = MEMIF_ERR_INVAL_ARG; + goto done; } + d->offset += data_offset; } - else - { - for (; i < chain_buf1; i++) - { - if (b1->data_len > - ring->desc[(b1->desc_index + i) & mask].buffer_length) - { - b1->data_len -= - ring->desc[(b1->desc_index + i) & mask].length = - ring->desc[(b1->desc_index + i) & mask].buffer_length; - } - else - { - ring->desc[(b1->desc_index + i) & mask].length = - b1->data_len; - b1->data_len = 0; - } -#ifdef MEMIF_DBG_SHM - print_bytes (b1->data + - ring->desc[(b1->desc_index + - i) & mask].buffer_length * - (chain_buf1 - 1), - ring->desc[(b1->desc_index + - i) & mask].buffer_length, - DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ - } - } - - head = (b1->desc_index + chain_buf1) & mask; - - b0->data = NULL; -#ifdef MEMIF_DBG - if (b0->data_len != 0) - DBG ("invalid b0 data length!"); -#endif /* MEMIF_DBG */ - b1->data = NULL; -#ifdef MEMIF_DBG - if (b1->data_len != 0) - DBG ("invalid b1 data length!"); -#endif /* MEMIF_DBG */ - - count -= 2; - *tx += chain_buf0 + chain_buf1; - curr_buf += 2; } - b0 = (bufs + curr_buf); - chain_buf0 = b0->buffer_len / ring->desc[b0->desc_index].buffer_length; - if ((b0->buffer_len % ring->desc[b0->desc_index].buffer_length) != 0) - chain_buf0++; - - for (i = 0; i < chain_buf0; i++) - { - if (b0->data_len > - ring->desc[(b0->desc_index + i) & mask].buffer_length) - { - b0->data_len -= ring->desc[(b0->desc_index + i) & mask].length = - ring->desc[(b0->desc_index + i) & mask].buffer_length; - } - else - { - ring->desc[(b0->desc_index + i) & mask].length = b0->data_len; - b0->data_len = 0; - } #ifdef MEMIF_DBG_SHM - print_bytes (b0->data + - ring->desc[(b0->desc_index + i) & mask].buffer_length * - (chain_buf0 - 1), - ring->desc[(b0->desc_index + i) & mask].buffer_length, - DBG_TX_BUF); + printf ("offset: %-6d\n", ring->desc[b0->desc_index & mask].offset); + printf ("data: %p\n", + memif_get_buffer (c, ring, b0->desc_index & mask)); + printf ("index: %u\n", b0->desc_index); + print_bytes (memif_get_buffer (c, ring, b0->desc_index & mask), + ring->desc[b0->desc_index & mask].length, DBG_TX_BUF); #endif /* MEMIF_DBG_SHM */ - } - - head = (b0->desc_index + chain_buf0) & mask; - - b0->data = NULL; -#ifdef MEMIF_DBG - if (b0->data_len != 0) - DBG ("invalid b0 data length!"); -#endif /* MEMIF_DBG */ + *tx += 1; count--; - *tx += chain_buf0; - curr_buf++; + index++; } - MEMIF_MEORY_BARRIER (); - ring->head = head; - - mq->alloc_bufs -= *tx; - /* TODO: return num of buffers and packets */ - *tx = curr_buf; +done: + MEMIF_MEMORY_BARRIER (); + if (c->args.is_master) + ring->tail = b0->desc_index + 1; + else + ring->head = b0->desc_index + 1; if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0) { @@ -1761,7 +1801,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_INT_WRITE; } - return MEMIF_ERR_SUCCESS; /* 0 */ + return err; } int @@ -1769,155 +1809,80 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, memif_buffer_t * bufs, uint16_t count, uint16_t * rx) { memif_connection_t *c = (memif_connection_t *) conn; - if (c == NULL) + if (EXPECT_FALSE (c == NULL)) return MEMIF_ERR_NOCONN; - if (c->fd < 0) + if (EXPECT_FALSE (c->control_channel == NULL)) return MEMIF_ERR_DISCONNECTED; uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; - if (qid >= num) + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; + if (EXPECT_FALSE (qid >= num)) return MEMIF_ERR_QID; + if (EXPECT_FALSE (!rx)) + return MEMIF_ERR_INVAL_ARG; + memif_queue_t *mq = &c->rx_queues[qid]; memif_ring_t *ring = mq->ring; - uint16_t head = ring->head; + uint16_t cur_slot, last_slot; uint16_t ns; uint16_t mask = (1 << mq->log2_ring_size) - 1; - memif_buffer_t *b0, *b1; - uint16_t curr_buf = 0; + memif_buffer_t *b0; *rx = 0; - int i; uint64_t b; - ssize_t r = read (mq->int_fd, &b, sizeof (b)); - if ((r == -1) && (errno != EAGAIN)) - return memif_syscall_error_handler (errno); + ssize_t r; - if (head == mq->last_head) - return 0; - - if (head > mq->last_head) - ns = head - mq->last_head; - else - ns = (1 << mq->log2_ring_size) - mq->last_head + head; - - while (ns && count) + cur_slot = (c->args.is_master) ? mq->last_head : mq->last_tail; + last_slot = (c->args.is_master) ? ring->head : ring->tail; + if (cur_slot == last_slot) { - DBG ("ns: %u, count: %u", ns, count); - while ((ns > 2) && (count > 2)) - { - b0 = (bufs + curr_buf); - b1 = (bufs + curr_buf + 1); - - b0->desc_index = mq->last_head; - i = 0; - b0->data_len = 0; - b0->buffer_len = 0; + r = read (mq->int_fd, &b, sizeof (b)); + if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN))) + return memif_syscall_error_handler (errno); - b0->data = memif_get_buffer (conn, ring, mq->last_head); - b0->data_len += ring->desc[mq->last_head].length; - b0->buffer_len += ring->desc[mq->last_head].buffer_length; -#ifdef MEMIF_DBG_SHM - print_bytes (b0->data + - ring->desc[b0->desc_index].buffer_length * i++, - ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ - ns--; - *rx += 1; - while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT) - { - ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT; - mq->last_head = (mq->last_head + 1) & mask; - b0->data_len += ring->desc[mq->last_head].length; - b0->buffer_len += ring->desc[mq->last_head].buffer_length; -#ifdef MEMIF_DBG_SHM - print_bytes (b0->data + - ring->desc[b0->desc_index].buffer_length * i++, - ring->desc[b0->desc_index].buffer_length, - DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ - ns--; - *rx += 1; - } - mq->last_head = (mq->last_head + 1) & mask; + return MEMIF_ERR_SUCCESS; + } - b1->desc_index = mq->last_head; - i = 0; - b0->data_len = 0; - b0->buffer_len = 0; + ns = last_slot - cur_slot; - b1->data = memif_get_buffer (conn, ring, mq->last_head); - b1->data_len += ring->desc[mq->last_head].length; - b1->buffer_len += ring->desc[mq->last_head].buffer_length; -#ifdef MEMIF_DBG_SHM - print_bytes (b1->data + - ring->desc[b1->desc_index].buffer_length * i++, - ring->desc[b1->desc_index].buffer_length, DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ - ns--; - *rx += 1; - while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT) - { - ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT; - mq->last_head = (mq->last_head + 1) & mask; - b1->data_len += ring->desc[mq->last_head].length; - b1->buffer_len += ring->desc[mq->last_head].buffer_length; -#ifdef MEMIF_DBG_SHM - print_bytes (b1->data + - ring->desc[b1->desc_index].buffer_length * i++, - ring->desc[b1->desc_index].buffer_length, - DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ - ns--; - *rx += 1; - } - mq->last_head = (mq->last_head + 1) & mask; + while (ns && count) + { + b0 = (bufs + *rx); - count -= 2; - curr_buf += 2; + b0->desc_index = cur_slot; + b0->data = memif_get_buffer (c, ring, cur_slot & mask); + b0->len = ring->desc[cur_slot & mask].length; + b0->flags = 0; + /* slave resets buffer length */ + if (c->args.is_master == 0) + { + ring->desc[cur_slot & mask].length = c->run_args.buffer_size; } - b0 = (bufs + curr_buf); - b0->desc_index = mq->last_head; - i = 0; - b0->data_len = 0; - b0->buffer_len = 0; + if (ring->desc[cur_slot & mask].flags & MEMIF_DESC_FLAG_NEXT) + { + b0->flags |= MEMIF_BUFFER_FLAG_NEXT; + ring->desc[cur_slot & mask].flags &= ~MEMIF_DESC_FLAG_NEXT; + } - b0->data = memif_get_buffer (conn, ring, mq->last_head); - b0->data_len += ring->desc[mq->last_head].length; - b0->buffer_len += ring->desc[mq->last_head].buffer_length; + b0->queue = mq; #ifdef MEMIF_DBG_SHM - print_bytes (b0->data + - ring->desc[b0->desc_index].buffer_length * i++, - ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF); + printf ("data: %p\n", b0->data); + printf ("index: %u\n", b0->desc_index); + printf ("queue: %p\n", b0->queue); + print_bytes (b0->data, b0->len, DBG_RX_BUF); #endif /* MEMIF_DBG_SHM */ ns--; *rx += 1; - while (ring->desc[mq->last_head].flags & MEMIF_DESC_FLAG_NEXT) - { - ring->desc[mq->last_head].flags &= ~MEMIF_DESC_FLAG_NEXT; - mq->last_head = (mq->last_head + 1) & mask; - b0->data_len += ring->desc[mq->last_head].length; - b0->buffer_len += ring->desc[mq->last_head].buffer_length; -#ifdef MEMIF_DBG_SHM - print_bytes (b0->data + - ring->desc[b0->desc_index].buffer_length * i++, - ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF); -#endif /* MEMIF_DBG_SHM */ - ns--; - *rx += 1; - } - mq->last_head = (mq->last_head + 1) & mask; - count--; - curr_buf++; + cur_slot++; } - mq->alloc_bufs += *rx; - - /* TODO: return num of buffers and packets */ - *rx = curr_buf; + if (c->args.is_master) + mq->last_head = cur_slot; + else + mq->last_tail = cur_slot; if (ns) { @@ -1925,6 +1890,10 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid, return MEMIF_ERR_NOBUF; } + r = read (mq->int_fd, &b, sizeof (b)); + if (EXPECT_FALSE ((r == -1) && (errno != EAGAIN))) + return memif_syscall_error_handler (errno); + return MEMIF_ERR_SUCCESS; /* 0 */ } @@ -1933,48 +1902,50 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md, char *buf, ssize_t buflen) { memif_connection_t *c = (memif_connection_t *) conn; + memif_socket_t *ms; + int err = MEMIF_ERR_SUCCESS, i; + ssize_t l0 = 0, l1; + if (c == NULL) return MEMIF_ERR_NOCONN; - int err = MEMIF_ERR_SUCCESS, i; - ssize_t l0, l1, total_l; - l0 = 0; + ms = (memif_socket_t *) c->args.socket; l1 = strlen ((char *) c->args.interface_name); - if (l0 + l1 <= buflen) + if (l0 + l1 < buflen) { - md->if_name = strncpy (buf + l0, (char *) c->args.interface_name, l1); - md->if_name[l0 + l1] = '\0'; + md->if_name = + (uint8_t *) strcpy (buf + l0, (char *) c->args.interface_name); l0 += l1 + 1; } else err = MEMIF_ERR_NOBUF_DET; - l1 = strlen ((char *) c->args.instance_name); - if (l0 + l1 <= buflen) + l1 = strlen ((char *) ms->args.app_name); + if (l0 + l1 < buflen) { - md->inst_name = strncpy (buf + l0, (char *) c->args.instance_name, l1); - md->inst_name[l0 + l1] = '\0'; + md->inst_name = + (uint8_t *) strcpy (buf + l0, (char *) ms->args.app_name); l0 += l1 + 1; } else err = MEMIF_ERR_NOBUF_DET; l1 = strlen ((char *) c->remote_if_name); - if (l0 + l1 <= buflen) + if (l0 + l1 < buflen) { - md->remote_if_name = strncpy (buf + l0, (char *) c->remote_if_name, l1); - md->remote_if_name[l0 + l1] = '\0'; + md->remote_if_name = + (uint8_t *) strcpy (buf + l0, (char *) c->remote_if_name); l0 += l1 + 1; } else err = MEMIF_ERR_NOBUF_DET; l1 = strlen ((char *) c->remote_name); - if (l0 + l1 <= buflen) + if (l0 + l1 < buflen) { - md->remote_inst_name = strncpy (buf + l0, (char *) c->remote_name, l1); - md->remote_inst_name[l0 + l1] = '\0'; + md->remote_inst_name = + (uint8_t *) strcpy (buf + l0, (char *) c->remote_name); l0 += l1 + 1; } else @@ -1982,71 +1953,107 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md, md->id = c->args.interface_id; - if (c->args.secret) + if (strlen ((char *) c->args.secret) > 0) { l1 = strlen ((char *) c->args.secret); - md->secret = strncpy (buf + l0, (char *) c->args.secret, l1); - md->secret[l0 + l1] = '\0'; - l0 += l1 + 1; + if (l0 + l1 < buflen) + { + md->secret = (uint8_t *) strcpy (buf + l0, (char *) c->args.secret); + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; } - else - err = MEMIF_ERR_NOBUF_DET; md->role = (c->args.is_master) ? 0 : 1; md->mode = c->args.mode; - l1 = strlen ((char *) c->args.socket_filename); - if (l0 + l1 <= buflen) + l1 = 108; + if (l0 + l1 < buflen) { - md->socket_filename = - strncpy (buf + l0, (char *) c->args.socket_filename, l1); - md->socket_filename[l0 + l1] = '\0'; - l0 += l1 + 1; + md->socket_path = (uint8_t *) memcpy (buf + l0, ms->args.path, 108); + l0 += l1; } else err = MEMIF_ERR_NOBUF_DET; - md->rx_queues_num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + l1 = strlen ((char *) c->remote_disconnect_string); + if (l0 + l1 < buflen) + { + md->error = + (uint8_t *) strcpy (buf + l0, (char *) c->remote_disconnect_string); + l0 += l1 + 1; + } + else + err = MEMIF_ERR_NOBUF_DET; - l1 = sizeof (memif_queue_details_t) * md->rx_queues_num; + md->regions_num = c->regions_num; + l1 = sizeof (memif_region_details_t) * md->regions_num; if (l0 + l1 <= buflen) { - md->rx_queues = (memif_queue_details_t *) buf + l0; - l0 = l1 + 1; + md->regions = (memif_region_details_t *) (buf + l0); + for (i = 0; i < md->regions_num; i++) + { + md->regions[i].index = i; + md->regions[i].addr = c->regions[i].addr; + md->regions[i].size = c->regions[i].region_size; + md->regions[i].fd = c->regions[i].fd; + md->regions[i].is_external = c->regions[i].is_external; + } + l0 += l1; } else err = MEMIF_ERR_NOBUF_DET; - for (i = 0; i < md->rx_queues_num; i++) + md->rx_queues_num = + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; + + l1 = sizeof (memif_queue_details_t) * md->rx_queues_num; + if (l0 + l1 <= buflen) { - md->rx_queues[i].qid = i; - md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size); - md->rx_queues[i].buffer_size = c->run_args.buffer_size; + md->rx_queues = (memif_queue_details_t *) (buf + l0); + for (i = 0; i < md->rx_queues_num; i++) + { + md->rx_queues[i].region = c->rx_queues[i].region; + md->rx_queues[i].qid = i; + md->rx_queues[i].ring_size = (1 << c->rx_queues[i].log2_ring_size); + md->rx_queues[i].flags = c->rx_queues[i].ring->flags; + md->rx_queues[i].head = c->rx_queues[i].ring->head; + md->rx_queues[i].tail = c->rx_queues[i].ring->tail; + md->rx_queues[i].buffer_size = c->run_args.buffer_size; + } + l0 += l1; } + else + err = MEMIF_ERR_NOBUF_DET; md->tx_queues_num = - (c->args.is_master) ? c->run_args.num_m2s_rings : c->run_args. - num_s2m_rings; + (c->args.is_master) ? c->run_args.num_m2s_rings : c-> + run_args.num_s2m_rings; l1 = sizeof (memif_queue_details_t) * md->tx_queues_num; if (l0 + l1 <= buflen) { - md->tx_queues = (memif_queue_details_t *) buf + l0; - l0 = l1 + 1; + md->tx_queues = (memif_queue_details_t *) (buf + l0); + for (i = 0; i < md->tx_queues_num; i++) + { + md->tx_queues[i].region = c->tx_queues[i].region; + md->tx_queues[i].qid = i; + md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size); + md->tx_queues[i].flags = c->tx_queues[i].ring->flags; + md->tx_queues[i].head = c->tx_queues[i].ring->head; + md->tx_queues[i].tail = c->tx_queues[i].ring->tail; + md->tx_queues[i].buffer_size = c->run_args.buffer_size; + } + l0 += l1; } else err = MEMIF_ERR_NOBUF_DET; - for (i = 0; i < md->tx_queues_num; i++) - { - md->tx_queues[i].qid = i; - md->tx_queues[i].ring_size = (1 << c->tx_queues[i].log2_ring_size); - md->tx_queues[i].buffer_size = c->run_args.buffer_size; - } - - md->link_up_down = (c->fd > 0) ? 1 : 0; + /* This is not completely true, clients should relay on + * on_connect/on_disconnect callbacks */ + md->link_up_down = (c->control_channel != NULL) ? 1 : 0; return err; /* 0 */ } @@ -2055,14 +2062,17 @@ int memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) { memif_connection_t *c = (memif_connection_t *) conn; + uint8_t num; + *efd = -1; if (c == NULL) return MEMIF_ERR_NOCONN; - if (c->fd < 0) + if (c->control_channel == NULL) return MEMIF_ERR_DISCONNECTED; - uint8_t num = - (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args. - num_m2s_rings; + + num = + (c->args.is_master) ? c->run_args.num_s2m_rings : c-> + run_args.num_m2s_rings; if (qid >= num) return MEMIF_ERR_QID; @@ -2070,26 +2080,3 @@ memif_get_queue_efd (memif_conn_handle_t conn, uint16_t qid, int *efd) return MEMIF_ERR_SUCCESS; } - -int -memif_cleanup () -{ - libmemif_main_t *lm = &libmemif_main; - if (lm->app_name) - free (lm->app_name); - lm->app_name = NULL; - if (lm->control_list) - free (lm->control_list); - lm->control_list = NULL; - if (lm->interrupt_list) - free (lm->interrupt_list); - lm->interrupt_list = NULL; - if (lm->listener_list) - free (lm->listener_list); - lm->listener_list = NULL; - if (lm->pending_list) - free (lm->pending_list); - lm->pending_list = NULL; - - return MEMIF_ERR_SUCCESS; /* 0 */ -}