/* private structs and functions */
#include <memif_private.h>
-#define ERRLIST_LEN 36
+#define ERRLIST_LEN 38
#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;
+int poll_cancel_fd = -1;
static char memif_buf[MAX_ERRBUF_LEN];
/* 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"
};
#define MEMIF_ERR_UNDEFINED "undefined error"
}
printf ("\n\n");
}
-#endif /* MEMIF_DBG */
+#endif /* MEMIF_DBG_SHM */
int
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;
{
int err = MEMIF_ERR_SUCCESS; /* 0 */
libmemif_main_t *lm = &libmemif_main;
+ memset (lm, 0, sizeof (libmemif_main_t));
if (app_name)
{
- lm->app_name = malloc (strlen (app_name) + sizeof (char));
- memset (lm->app_name, 0, strlen (app_name) + sizeof (char));
+ uint8_t len = (strlen (app_name) < MEMIF_NAME_LEN)
+ ? MEMIF_NAME_LEN : strlen (app_name);
strncpy ((char *) lm->app_name, app_name, strlen (app_name));
}
else
{
- 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));
}
{
memif_epfd = epoll_create (1);
memif_control_fd_update_register (memif_control_fd_update);
+ if ((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);
DBG ("libmemif event polling initialized");
}
- memset (&lm->ms, 0, sizeof (memif_socket_t));
-
lm->control_list_len = 2;
lm->interrupt_list_len = 2;
lm->listener_list_len = 1;
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));
if (lm->control_fd_update (lm->timerfd, MEMIF_FD_EVENT_READ) < 0)
{
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;
+ (conn->args.is_master) ? conn->run_args.num_s2m_rings : conn->
+ run_args.num_m2s_rings;
if (qid >= num)
return MEMIF_ERR_QID;
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)
}
DBG ("creating socket file");
ms = malloc (sizeof (memif_socket_t));
- ms->filename = malloc (strlen ((char *) conn->args.
- socket_filename) +
- sizeof (char));
+ ms->filename =
+ malloc (strlen ((char *) conn->args.socket_filename) +
+ sizeof (char));
memset (ms->filename, 0,
strlen ((char *) conn->args.socket_filename) +
sizeof (char));
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;
+ (((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)
+ 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);
+ ((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;
}
}
if (events & MEMIF_FD_EVENT_READ)
{
err =
- ((memif_connection_t *) e->data_struct)->read_fn (e->
- data_struct);
+ ((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);
+ ((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);
+ ((memif_connection_t *) e->data_struct)->
+ error_fn (e->data_struct);
if (err != MEMIF_ERR_SUCCESS)
return err;
}
int en = 0, err = MEMIF_ERR_SUCCESS, i = 0; /* 0 */
uint16_t num;
uint32_t events = 0;
+ uint64_t counter = 0;
+ ssize_t r = 0;
memset (&evt, 0, sizeof (evt));
evt.events = EPOLLIN | EPOLLOUT;
sigset_t sigset;
en = epoll_pwait (memif_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)
{
+ if (evt.data.fd == poll_cancel_fd)
+ {
+ r = read (evt.data.fd, &counter, sizeof (counter));
+ return MEMIF_ERR_POLL_CANCEL;
+ }
if (evt.events & EPOLLIN)
events |= MEMIF_FD_EVENT_READ;
if (evt.events & EPOLLOUT)
return 0;
}
+int
+memif_cancel_poll_event ()
+{
+ uint64_t counter = 1;
+ ssize_t w = 0;
+
+ if (poll_cancel_fd == -1)
+ return 0;
+ w = write (poll_cancel_fd, &counter, sizeof (counter));
+ if (w < sizeof (counter))
+ return MEMIF_ERR_INT_WRITE;
+
+ return 0;
+}
+
static void
memif_msg_queue_free (memif_msg_queue_elt_t ** e)
{
if (c->tx_queues != NULL)
{
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;
for (i = 0; i < num; i++)
{
mq = &c->tx_queues[i];
if (c->rx_queues != NULL)
{
num =
- (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
- num_m2s_rings;
+ (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];
}
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;
for (i = 0; i < num; i++)
{
mq = &c->tx_queues[i];
}
}
num =
- (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
- num_m2s_rings;
+ (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];
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 * count_out, uint16_t size)
{
memif_connection_t *c = (memif_connection_t *) conn;
if (c == NULL)
if (c->fd < 0)
return MEMIF_ERR_DISCONNECTED;
uint8_t 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;
if (qid >= num)
return MEMIF_ERR_QID;
memif_queue_t *mq = &c->tx_queues[qid];
memif_ring_t *ring = mq->ring;
memif_buffer_t *b0, *b1;
+ uint8_t chain_buf = 1;
uint16_t mask = (1 << mq->log2_ring_size) - 1;
+ uint16_t head = ring->head;
+ uint16_t tail = ring->tail;
+ uint16_t ring_size;
uint16_t s0, s1, ns;
*count_out = 0;
- int err = MEMIF_ERR_SUCCESS; /* 0 */
+ int i, err = MEMIF_ERR_SUCCESS; /* 0 */
+
+ ring_size = (1 << mq->log2_ring_size);
+ ns = ring_size - head + tail;
- if (ring->tail != ring->head)
+ /* calculate number of chain buffers */
+ if (size > ring->desc[0].buffer_length)
{
- if (ring->head > ring->tail)
- ns = (1 << mq->log2_ring_size) - ring->head + ring->tail;
- else
- ns = ring->tail - ring->head;
+ chain_buf = size / ring->desc[0].buffer_length;
+ if (((size % ring->desc[0].buffer_length) != 0) || (size == 0))
+ chain_buf++;
}
- else
- ns = (1 << mq->log2_ring_size);
-
- /* (head == tail) ? receive function will asume that no packets are available */
- ns -= 1;
while (count && ns)
{
while ((count > 2) && (ns > 2))
{
- s0 = (ring->head + mq->alloc_bufs + *count_out) & mask;
- s1 = (ring->head + mq->alloc_bufs + *count_out + 1) & mask;
+ s0 = (ring->head + mq->alloc_bufs) & mask;
+ s1 = (ring->head + mq->alloc_bufs + chain_buf) & mask;
+
+ if ((2 * chain_buf) > ns)
+ break;
b0 = (bufs + *count_out);
b1 = (bufs + *count_out + 1);
- b0->desc_index = s0;
- b1->desc_index = s1;
- b0->buffer_len = ring->desc[s0].buffer_length;
- b1->buffer_len = ring->desc[s1].buffer_length;
+ b0->desc_index = head + mq->alloc_bufs;
+ b1->desc_index = head + mq->alloc_bufs + chain_buf;
+ ring->desc[s0].flags = 0;
+ ring->desc[s1].flags = 0;
+ b0->buffer_len = ring->desc[s0].buffer_length * chain_buf;
+ b1->buffer_len = ring->desc[s1].buffer_length * chain_buf;
/* 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 < (chain_buf - 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");
+ }
+
+ mq->alloc_bufs += 2 * chain_buf;
+
DBG ("allocated ring slots %u, %u", s0, s1);
count -= 2;
- ns -= 2;
+ ns -= (2 * chain_buf);
*count_out += 2;
}
- s0 = (ring->head + mq->alloc_bufs + *count_out) & mask;
+ s0 = (ring->head + mq->alloc_bufs) & mask;
b0 = (bufs + *count_out);
- b0->desc_index = s0;
- b0->buffer_len = ring->desc[s0].buffer_length;
+ if (chain_buf > ns)
+ break;
+
+ b0->desc_index = head + mq->alloc_bufs;
+ ring->desc[s0].flags = 0;
+ b0->buffer_len = ring->desc[s0].buffer_length * chain_buf;
b0->data = c->regions->shm + ring->desc[s0].offset;
+ for (i = 0; i < (chain_buf - 1); i++)
+ {
+ ring->desc[(s0 + i) & mask].flags |= MEMIF_DESC_FLAG_NEXT;
+ DBG ("allocating chained buffers");
+ }
+
+ mq->alloc_bufs += chain_buf;
+
DBG ("allocated ring slot %u", s0);
count--;
- ns--;
+ ns -= chain_buf;
*count_out += 1;
}
- mq->alloc_bufs += *count_out;
DBG ("allocated: %u/%u bufs. Total %u allocated bufs", *count_out, count,
mq->alloc_bufs);
if (c->fd < 0)
return MEMIF_ERR_DISCONNECTED;
uint8_t num =
- (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
- num_m2s_rings;
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+ run_args.num_m2s_rings;
if (qid >= num)
return MEMIF_ERR_QID;
libmemif_main_t *lm = &libmemif_main;
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;
{
b0 = (bufs + *count_out);
b1 = (bufs + *count_out + 1);
- tail = (b0->desc_index + 1) & mask;
- tail = (b1->desc_index + 1) & mask;
+ chain_buf0 =
+ b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+ if ((b0->buffer_len %
+ ring->desc[b0->desc_index & mask].buffer_length) != 0)
+ chain_buf0++;
+ chain_buf1 =
+ b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
+ if ((b1->buffer_len %
+ ring->desc[b1->desc_index & mask].buffer_length) != 0)
+ chain_buf1++;
+ tail = b1->desc_index + chain_buf1;
b0->data = NULL;
b1->data = NULL;
count -= 2;
*count_out += 2;
- mq->alloc_bufs -= 2;
+ mq->alloc_bufs -= chain_buf0 + chain_buf1;
}
b0 = (bufs + *count_out);
- tail = (b0->desc_index + 1) & mask;
+ chain_buf0 =
+ b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+ if ((b0->buffer_len %
+ ring->desc[b0->desc_index & mask].buffer_length) != 0)
+ chain_buf0++;
+ tail = b0->desc_index + chain_buf0;
b0->data = NULL;
count--;
*count_out += 1;
- mq->alloc_bufs--;
+ mq->alloc_bufs -= chain_buf0;
}
- MEMIF_MEORY_BARRIER ();
+ MEMIF_MEMORY_BARRIER ();
ring->tail = tail;
+ DBG ("tail: %u", ring->tail);
return MEMIF_ERR_SUCCESS; /* 0 */
}
if (c->fd < 0)
return MEMIF_ERR_DISCONNECTED;
uint8_t 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;
if (qid >= num)
return MEMIF_ERR_QID;
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;
*tx = 0;
+ uint16_t curr_buf = 0;
memif_buffer_t *b0, *b1;
+ int i;
while (count)
{
while (count > 2)
{
- b0 = (bufs + *tx);
- b1 = (bufs + *tx + 1);
- ring->desc[b0->desc_index].length = b0->data_len;
- ring->desc[b1->desc_index].length = b1->data_len;
+ b0 = (bufs + curr_buf);
+ b1 = (bufs + curr_buf + 1);
+ chain_buf0 =
+ b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+ if ((b0->buffer_len %
+ ring->desc[b0->desc_index & mask].buffer_length) != 0)
+ chain_buf0++;
+
+ chain_buf1 =
+ b1->buffer_len / ring->desc[b1->desc_index & mask].buffer_length;
+ if ((b1->buffer_len %
+ ring->desc[b1->desc_index & mask].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)
+ {
+ for (; 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, b0->data_len, DBG_TX_BUF);
- print_bytes (b1->data, b1->data_len, DBG_TX_BUF);
-#endif
+ 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 */
+ }
+ }
+ 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 = (b0->desc_index + 1) & mask;
- head = (b1->desc_index + 1) & mask;
+ head = b1->desc_index + chain_buf1;
b0->data = NULL;
- b0->data_len = 0;
+#ifdef MEMIF_DBG
+ if (b0->data_len != 0)
+ DBG ("invalid b0 data length!");
+#endif /* MEMIF_DBG */
b1->data = NULL;
- b1->data_len = 0;
+#ifdef MEMIF_DBG
+ if (b1->data_len != 0)
+ DBG ("invalid b1 data length!");
+#endif /* MEMIF_DBG */
count -= 2;
- *tx += 2;
+ *tx += chain_buf0 + chain_buf1;
+ curr_buf += 2;
}
- b0 = (bufs + *tx);
- ring->desc[b0->desc_index].length = b0->data_len;
+ b0 = (bufs + curr_buf);
+ chain_buf0 =
+ b0->buffer_len / ring->desc[b0->desc_index & mask].buffer_length;
+ if ((b0->buffer_len %
+ ring->desc[b0->desc_index & mask].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, b0->data_len, DBG_TX_BUF);
-#endif
+ 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 */
+ }
- head = (b0->desc_index + 1) & mask;
+ head = b0->desc_index + chain_buf0;
b0->data = NULL;
- b0->data_len = 0;
+#ifdef MEMIF_DBG
+ if (b0->data_len != 0)
+ DBG ("invalid b0 data length!");
+#endif /* MEMIF_DBG */
count--;
- *tx += 1;
+ *tx += chain_buf0;
+ curr_buf++;
}
- MEMIF_MEORY_BARRIER ();
+ MEMIF_MEMORY_BARRIER ();
ring->head = head;
mq->alloc_bufs -= *tx;
+ /* TODO: return num of buffers and packets */
+ *tx = curr_buf;
+
if ((ring->flags & MEMIF_RING_FLAG_MASK_INT) == 0)
{
uint64_t a = 1;
if (c->fd < 0)
return MEMIF_ERR_DISCONNECTED;
uint8_t num =
- (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
- num_m2s_rings;
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+ run_args.num_m2s_rings;
if (qid >= num)
return MEMIF_ERR_QID;
memif_queue_t *mq = &c->rx_queues[qid];
uint16_t ns;
uint16_t mask = (1 << mq->log2_ring_size) - 1;
memif_buffer_t *b0, *b1;
+ uint16_t curr_buf = 0;
*rx = 0;
+#ifdef MEMIF_DBG_SHM
+ int i;
+#endif /* MEMIF_DBG_SHM */
uint64_t b;
ssize_t r = read (mq->int_fd, &b, sizeof (b));
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;
+ ns = head - mq->last_head;
while (ns && count)
{
while ((ns > 2) && (count > 2))
{
- b0 = (bufs + *rx);
- b1 = (bufs + *rx + 1);
+ b0 = (bufs + curr_buf);
+ b1 = (bufs + curr_buf + 1);
b0->desc_index = mq->last_head;
- b1->desc_index = mq->last_head + 1;
- b0->data = memif_get_buffer (conn, ring, mq->last_head);
- b1->data = memif_get_buffer (conn, ring, mq->last_head + 1);
- b0->data_len = ring->desc[mq->last_head].length;
- b1->data_len = ring->desc[mq->last_head + 1].length;
- b0->buffer_len = ring->desc[mq->last_head].buffer_length;
- b1->buffer_len = ring->desc[mq->last_head + 1].buffer_length;
-
+ b0->data = memif_get_buffer (conn, ring, mq->last_head & mask);
+ b0->data_len = ring->desc[mq->last_head & mask].length;
+ b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ i = 0;
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index & mask].buffer_length * i++,
+ ring->desc[b0->desc_index & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ while (ring->desc[mq->last_head & mask].
+ flags & MEMIF_DESC_FLAG_NEXT)
+ {
+ ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
+ mq->last_head++;
+ b0->data_len += ring->desc[mq->last_head & mask].length;
+ b0->buffer_len +=
+ ring->desc[mq->last_head & mask].buffer_length;
#ifdef MEMIF_DBG_SHM
- print_bytes (b0->data, b0->data_len, DBG_RX_BUF);
- print_bytes (b1->data, b1->data_len, DBG_RX_BUF);
-#endif
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index & mask].buffer_length *
+ i++,
+ ring->desc[b0->desc_index & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ }
+ mq->last_head++;
- mq->last_head = (mq->last_head + 2) & mask;
+ b1->desc_index = mq->last_head;
+ b1->data = memif_get_buffer (conn, ring, mq->last_head & mask);
+ b1->data_len = ring->desc[mq->last_head & mask].length;
+ b1->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ i = 0;
+ print_bytes (b1->data +
+ ring->desc[b1->desc_index & mask].buffer_length * i++,
+ ring->desc[b1->desc_index & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ while (ring->desc[mq->last_head & mask].
+ flags & MEMIF_DESC_FLAG_NEXT)
+ {
+ ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
+ mq->last_head++;
+ b1->data_len += ring->desc[mq->last_head & mask].length;
+ b1->buffer_len +=
+ ring->desc[mq->last_head & mask].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b1->data +
+ ring->desc[b1->desc_index & mask].buffer_length *
+ i++,
+ ring->desc[b1->desc_index & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ }
+ mq->last_head++;
- ns -= 2;
count -= 2;
- *rx += 2;
+ curr_buf += 2;
}
- b0 = (bufs + *rx);
+ b0 = (bufs + curr_buf);
b0->desc_index = mq->last_head;
- 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->data = memif_get_buffer (conn, ring, mq->last_head & mask);
+ b0->data_len = ring->desc[mq->last_head & mask].length;
+ b0->buffer_len = ring->desc[mq->last_head & mask].buffer_length;
#ifdef MEMIF_DBG_SHM
- print_bytes (b0->data, b0->data_len, DBG_RX_BUF);
-#endif
+ i = 0;
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index & mask].buffer_length * i++,
+ ring->desc[b0->desc_index & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
- mq->last_head = (mq->last_head + 1) & mask;
+ while (ring->desc[mq->last_head & mask].flags & MEMIF_DESC_FLAG_NEXT)
+ {
+ ring->desc[mq->last_head & mask].flags &= ~MEMIF_DESC_FLAG_NEXT;
+ mq->last_head++;
+ b0->data_len += ring->desc[mq->last_head & mask].length;
+ b0->buffer_len += ring->desc[mq->last_head & mask].buffer_length;
+#ifdef MEMIF_DBG_SHM
+ print_bytes (b0->data +
+ ring->desc[b0->desc_index & mask].buffer_length * i++,
+ ring->desc[b0->desc_index & mask].buffer_length,
+ DBG_TX_BUF);
+#endif /* MEMIF_DBG_SHM */
+ ns--;
+ *rx += 1;
+ }
+ mq->last_head++;
- ns--;
count--;
- *rx += 1;
+ curr_buf++;
}
mq->alloc_bufs += *rx;
+ /* TODO: return num of buffers and packets */
+ *rx = curr_buf;
+
if (ns)
{
DBG ("not enough buffers!");
l0 = 0;
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 = 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)
+ 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 = strcpy (buf + l0, (char *) c->args.instance_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 = 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 = strcpy (buf + l0, (char *) c->remote_name);
l0 += l1 + 1;
}
else
if (c->args.secret)
{
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 = 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)
+ if (l0 + l1 < buflen)
{
md->socket_filename =
- strncpy (buf + l0, (char *) c->args.socket_filename, l1);
- md->socket_filename[l0 + l1] = '\0';
+ strcpy (buf + l0, (char *) c->args.socket_filename);
l0 += l1 + 1;
}
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;
+ (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 = (memif_queue_details_t *) buf + l0;
- l0 = l1 + 1;
+ l0 += l1;
}
else
err = MEMIF_ERR_NOBUF_DET;
{
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;
}
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;
+ l0 += l1;
}
else
err = MEMIF_ERR_NOBUF_DET;
{
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;
}
if (c->fd < 0)
return MEMIF_ERR_DISCONNECTED;
uint8_t num =
- (c->args.is_master) ? c->run_args.num_s2m_rings : c->run_args.
- num_m2s_rings;
+ (c->args.is_master) ? c->run_args.num_s2m_rings : c->
+ run_args.num_m2s_rings;
if (qid >= num)
return MEMIF_ERR_QID;
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->pending_list)
free (lm->pending_list);
lm->pending_list = NULL;
+ if (poll_cancel_fd != -1)
+ close (poll_cancel_fd);
return MEMIF_ERR_SUCCESS; /* 0 */
}