summaryrefslogtreecommitdiffstats
path: root/extras/libmemif
diff options
context:
space:
mode:
Diffstat (limited to 'extras/libmemif')
-rw-r--r--extras/libmemif/src/main.c99
1 files changed, 34 insertions, 65 deletions
diff --git a/extras/libmemif/src/main.c b/extras/libmemif/src/main.c
index b1176c6b7c9..39c18f27477 100644
--- a/extras/libmemif/src/main.c
+++ b/extras/libmemif/src/main.c
@@ -1388,43 +1388,35 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
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;
+ uint8_t chain_buf = 1;
uint16_t mask = (1 << mq->log2_ring_size) - 1;
uint16_t s0, s1, ns;
*count_out = 0;
int i, err = MEMIF_ERR_SUCCESS; /* 0 */
- if (ring->tail != ring->head)
- {
- if (ring->head > ring->tail)
- ns = (1 << mq->log2_ring_size) - ring->head + ring->tail;
- else
- ns = ring->tail - ring->head;
- }
+ if (ring->tail > ring->head)
+ ns = ring->tail - ring->head;
else
- ns = (1 << mq->log2_ring_size);
+ ns = (1 << mq->log2_ring_size) - ring->head + ring->tail;
/* (head == tail) ? receive function will asume that no packets are available */
ns -= 1;
+ if (size > ring->desc[0].buffer_length)
+ {
+ chain_buf = size / ring->desc[s0].buffer_length;
+ if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
+ chain_buf++;
+ }
+
while (count && ns)
{
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++;
+ s1 = (ring->head + mq->alloc_bufs + chain_buf) & mask;
- 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)
+ if ((2 * chain_buf) > ns)
break;
b0 = (bufs + *count_out);
@@ -1434,64 +1426,49 @@ memif_buffer_alloc (memif_conn_handle_t conn, uint16_t qid,
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;
+ 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 < (memif_min (chain_buf0, chain_buf1) - 1); i++)
+ 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");
}
- 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;
- }
-
- mq->alloc_bufs += chain_buf0 + chain_buf1;
+ mq->alloc_bufs += 2 * chain_buf;
DBG ("allocated ring slots %u, %u", s0, s1);
count -= 2;
- ns -= chain_buf0 + chain_buf1;
+ ns -= (2 * chain_buf);
*count_out += 2;
}
s0 = (ring->head + mq->alloc_bufs) & mask;
b0 = (bufs + *count_out);
- chain_buf0 = size / ring->desc[s0].buffer_length;
- if (((size % ring->desc[s0].buffer_length) != 0) || (size == 0))
- chain_buf0++;
-
- if (chain_buf0 > ns)
+ if (chain_buf > ns)
break;
b0->desc_index = s0;
ring->desc[s0].flags = 0;
- b0->buffer_len = ring->desc[s0].buffer_length * chain_buf0;
+ b0->buffer_len = ring->desc[s0].buffer_length * chain_buf;
b0->data = c->regions->shm + ring->desc[s0].offset;
- for (i = 0; i < (chain_buf0 - 1); i++)
+ 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_buf0;
+ mq->alloc_bufs += chain_buf;
DBG ("allocated ring slot %u", s0);
count--;
- ns -= chain_buf0;
+ ns -= chain_buf;
*count_out += 1;
}
@@ -1819,7 +1796,9 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
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));
@@ -1836,21 +1815,17 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
while (ns && count)
{
- 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;
-
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_len = ring->desc[mq->last_head].length;
+ b0->buffer_len = ring->desc[mq->last_head].buffer_length;
#ifdef MEMIF_DBG_SHM
+ i = 0;
print_bytes (b0->data +
ring->desc[b0->desc_index].buffer_length * i++,
ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);
@@ -1875,14 +1850,11 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
mq->last_head = (mq->last_head + 1) & mask;
b1->desc_index = mq->last_head;
- i = 0;
- b1->data_len = 0;
- b1->buffer_len = 0;
-
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;
+ b1->data_len = ring->desc[mq->last_head].length;
+ b1->buffer_len = ring->desc[mq->last_head].buffer_length;
#ifdef MEMIF_DBG_SHM
+ i = 0;
print_bytes (b1->data +
ring->desc[b1->desc_index].buffer_length * i++,
ring->desc[b1->desc_index].buffer_length, DBG_TX_BUF);
@@ -1912,14 +1884,11 @@ memif_rx_burst (memif_conn_handle_t conn, uint16_t qid,
b0 = (bufs + curr_buf);
b0->desc_index = mq->last_head;
- i = 0;
- b0->data_len = 0;
- b0->buffer_len = 0;
-
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_len = ring->desc[mq->last_head].length;
+ b0->buffer_len = ring->desc[mq->last_head].buffer_length;
#ifdef MEMIF_DBG_SHM
+ i = 0;
print_bytes (b0->data +
ring->desc[b0->desc_index].buffer_length * i++,
ring->desc[b0->desc_index].buffer_length, DBG_TX_BUF);