#include <svm/svm_fifo.h>
#include <vppinfra/cpu.h>
+CLIB_MARCH_FN (svm_fifo_copy_to_chunk, void, svm_fifo_t * f,
+ svm_fifo_chunk_t * c, u32 tail_idx, const u8 * src, u32 len,
+ svm_fifo_chunk_t ** last)
+{
+ u32 n_chunk;
+
+ ASSERT (tail_idx >= c->start_byte && tail_idx < c->start_byte + c->length);
+
+ tail_idx -= c->start_byte;
+ n_chunk = c->length - tail_idx;
+ if (n_chunk <= len)
+ {
+ u32 to_copy = len;
+ clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
+ c = c->next;
+ while ((to_copy -= n_chunk))
+ {
+ n_chunk = clib_min (c->length, to_copy);
+ clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
+ c = c->length <= to_copy ? c->next : c;
+ }
+ if (*last)
+ *last = c;
+ }
+ else
+ {
+ clib_memcpy_fast (&c->data[tail_idx], src, len);
+ }
+}
+
+CLIB_MARCH_FN (svm_fifo_copy_from_chunk, void, svm_fifo_t * f,
+ svm_fifo_chunk_t * c, u32 head_idx, u8 * dst, u32 len,
+ svm_fifo_chunk_t ** last)
+{
+ u32 n_chunk;
+
+ ASSERT (head_idx >= c->start_byte && head_idx < c->start_byte + c->length);
+
+ head_idx -= c->start_byte;
+ n_chunk = c->length - head_idx;
+ if (n_chunk <= len)
+ {
+ u32 to_copy = len;
+ clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
+ c = c->next;
+ while ((to_copy -= n_chunk))
+ {
+ n_chunk = clib_min (c->length, to_copy);
+ clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
+ c = c->length <= to_copy ? c->next : c;
+ }
+ if (*last)
+ *last = c;
+ }
+ else
+ {
+ clib_memcpy_fast (dst, &c->data[head_idx], len);
+ }
+}
+
+#ifndef CLIB_MARCH_VARIANT
+
+static inline void
+svm_fifo_copy_to_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 tail_idx,
+ const u8 * src, u32 len, svm_fifo_chunk_t ** last)
+{
+ CLIB_MARCH_FN_SELECT (svm_fifo_copy_to_chunk) (f, c, tail_idx, src, len,
+ last);
+}
+
+static inline void
+svm_fifo_copy_from_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c, u32 head_idx,
+ u8 * dst, u32 len, svm_fifo_chunk_t ** last)
+{
+ CLIB_MARCH_FN_SELECT (svm_fifo_copy_from_chunk) (f, c, head_idx, dst, len,
+ last);
+}
+
static inline u8
position_lt (svm_fifo_t * f, u32 a, u32 b, u32 tail)
{
return s->start + s->length;
}
-#ifndef CLIB_MARCH_VARIANT
-
u8 *
format_ooo_segment (u8 * s, va_list * args)
{
f->refcnt = 1;
f->default_chunk.start_byte = 0;
f->default_chunk.length = f->size;
- f->default_chunk.next = f->start_chunk = &f->default_chunk;
- f->end_chunk = f->head_chunk = f->tail_chunk = f->start_chunk;
+ f->default_chunk.next = f->start_chunk = f->end_chunk = &f->default_chunk;
+ f->head_chunk = f->tail_chunk = f->ooo_enq = f->ooo_deq = f->start_chunk;
}
/** create an svm fifo, in the current heap. Fails vs blow up the process */
return f;
}
+static inline void
+svm_fifo_size_update (svm_fifo_t * f, svm_fifo_chunk_t * c)
+{
+ svm_fifo_chunk_t *prev;
+ u32 add_bytes = 0;
+
+ if (!c)
+ return;
+
+ f->end_chunk->next = c;
+ while (c)
+ {
+ add_bytes += c->length;
+ prev = c;
+ c = c->next;
+ }
+ f->end_chunk = prev;
+ prev->next = f->start_chunk;
+ f->size += add_bytes;
+ f->nitems = f->size - 1;
+ f->new_chunks = 0;
+}
+
+static void
+svm_fifo_try_size_update (svm_fifo_t * f, u32 new_head)
+{
+ if (new_head % f->size > f->tail % f->size)
+ return;
+
+ svm_fifo_size_update (f, f->new_chunks);
+ f->flags &= ~SVM_FIFO_F_SIZE_UPDATE;
+}
+
+void
+svm_fifo_add_chunk (svm_fifo_t * f, svm_fifo_chunk_t * c)
+{
+ svm_fifo_chunk_t *cur, *prev;
+
+ /* Initialize rbtree if needed and add default chunk to it */
+ if (!(f->flags & SVM_FIFO_F_MULTI_CHUNK))
+ {
+ rb_tree_init (&f->chunk_lookup);
+ rb_tree_add2 (&f->chunk_lookup, 0, pointer_to_uword (f->start_chunk));
+ f->flags |= SVM_FIFO_F_MULTI_CHUNK;
+ }
+
+ /* Initialize chunks and add to lookup rbtree. Expectation is that this is
+ * called with the heap where the rbtree's pool is pushed. */
+ cur = c;
+ if (f->new_chunks)
+ {
+ prev = f->new_chunks;
+ while (prev->next)
+ prev = prev->next;
+ prev->next = c;
+ }
+ else
+ prev = f->end_chunk;
+
+ while (cur)
+ {
+ cur->start_byte = prev->start_byte + prev->length;
+ rb_tree_add2 (&f->chunk_lookup, cur->start_byte,
+ pointer_to_uword (cur));
+ prev = cur;
+ cur = cur->next;
+ }
+
+ /* If fifo is not wrapped, update the size now */
+ if (!svm_fifo_is_wrapped (f))
+ {
+ ASSERT (!f->new_chunks);
+ svm_fifo_size_update (f, c);
+ return;
+ }
+
+ /* Postpone size update */
+ if (!f->new_chunks)
+ {
+ f->new_chunks = c;
+ f->flags |= SVM_FIFO_F_SIZE_UPDATE;
+ }
+}
+
+static inline u8
+svm_fifo_chunk_includes_pos (svm_fifo_chunk_t * c, u32 pos)
+{
+ return (pos >= c->start_byte && pos < c->start_byte + c->length);
+}
+
+/**
+ * Find chunk for given byte position
+ *
+ * @param f fifo
+ * @param pos normalized position in fifo
+ *
+ * @return chunk that includes given position or 0
+ */
+static svm_fifo_chunk_t *
+svm_fifo_find_chunk (svm_fifo_t * f, u32 pos)
+{
+ rb_tree_t *rt = &f->chunk_lookup;
+ rb_node_t *cur, *prev;
+ svm_fifo_chunk_t *c;
+
+ cur = rb_node (rt, rt->root);
+ while (pos != cur->key)
+ {
+ prev = cur;
+ if (pos < cur->key)
+ cur = rb_node_left (rt, cur);
+ else
+ cur = rb_node_right (rt, cur);
+
+ if (rb_node_is_tnil (rt, cur))
+ {
+ /* Hit tnil as a left child. Find predecessor */
+ if (pos < prev->key)
+ {
+ cur = rb_tree_predecessor (rt, prev);
+ c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
+ if (svm_fifo_chunk_includes_pos (c, pos))
+ return c;
+ return 0;
+ }
+ /* Hit tnil as a right child. Check if this is the one, otherwise
+ * search for successor */
+ c = uword_to_pointer (prev->opaque, svm_fifo_chunk_t *);
+ if (svm_fifo_chunk_includes_pos (c, pos))
+ return c;
+
+ cur = rb_tree_successor (rt, prev);
+ c = uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
+ if (svm_fifo_chunk_includes_pos (c, pos))
+ return c;
+ return 0;
+ }
+ }
+
+ if (!rb_node_is_tnil (rt, cur))
+ return uword_to_pointer (cur->opaque, svm_fifo_chunk_t *);
+ return 0;
+}
+
void
svm_fifo_free (svm_fifo_t * f)
{
clib_mem_free (f);
}
}
-#endif
always_inline ooo_segment_t *
ooo_segment_new (svm_fifo_t * f, u32 start, u32 length)
return bytes;
}
-CLIB_MARCH_FN (svm_fifo_enqueue_nowait, int, svm_fifo_t * f, u32 len,
- const u8 * src)
+void
+svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
{
- u32 n_chunk, to_copy, tail, head, free_count, tail_idx;
+ u32 n_chunk;
+ u32 head, tail, head_idx;
svm_fifo_chunk_t *c;
+ ASSERT (len <= f->nitems);
+
+ f_load_head_tail_cons (f, &head, &tail);
+ c = f->head_chunk;
+ head_idx = head % f->size;
+ head_idx -= c->start_byte;
+ n_chunk = c->length - head_idx;
+ if (len <= n_chunk)
+ clib_memcpy_fast (&c->data[head_idx], data, len);
+ else
+ {
+ clib_memcpy_fast (&c->data[head_idx], data, n_chunk);
+ clib_memcpy_fast (&c->next->data[0], data + n_chunk, len - n_chunk);
+ }
+}
+
+int
+svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 len, const u8 * src)
+{
+ u32 tail, head, free_count;
+
f_load_head_tail_prod (f, &head, &tail);
/* free space in fifo can only increase during enqueue: SPSC */
return SVM_FIFO_FULL;
/* number of bytes we're going to copy */
- to_copy = len = clib_min (free_count, len);
+ len = clib_min (free_count, len);
- c = f->tail_chunk;
- tail_idx = tail % f->size;
- ASSERT (tail_idx >= c->start_byte);
- tail_idx -= c->start_byte;
- n_chunk = c->length - tail_idx;
-
- if (n_chunk < to_copy)
- {
- clib_memcpy_fast (&c->data[tail_idx], src, n_chunk);
- while ((to_copy -= n_chunk))
- {
- c = c->next;
- n_chunk = clib_min (c->length, to_copy);
- clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
- }
- f->tail_chunk = c;
- }
- else
- {
- clib_memcpy_fast (&c->data[tail_idx], src, to_copy);
- }
+ svm_fifo_copy_to_chunk (f, f->tail_chunk, tail % f->size, src, len,
+ &f->tail_chunk);
tail += len;
svm_fifo_trace_add (f, head, n_total, 2);
if (PREDICT_FALSE (f->ooos_list_head != OOO_SEGMENT_INVALID_INDEX))
len += ooo_segment_try_collect (f, len, &tail);
- ASSERT (len <= free_count);
-
/* store-rel: producer owned index (paired with load-acq in consumer) */
clib_atomic_store_rel_n (&f->tail, tail);
return len;
}
-#ifndef CLIB_MARCH_VARIANT
-int
-svm_fifo_enqueue_nowait (svm_fifo_t * f, u32 max_bytes,
- const u8 * copy_from_here)
-{
- return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_nowait) (f, max_bytes,
- copy_from_here);
-}
-#endif
-
/**
* Enqueue a future segment.
*
* Returns 0 of the entire segment was copied
* Returns -1 if none of the segment was copied due to lack of space
*/
-CLIB_MARCH_FN (svm_fifo_enqueue_with_offset, int, svm_fifo_t * f,
- u32 offset, u32 len, u8 * src)
+int
+svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 len, u8 * src)
{
- u32 to_copy, n_chunk, tail, head, free_count, tail_offset_idx;
- svm_fifo_chunk_t *c;
+ u32 tail, head, free_count, tail_idx;
f_load_head_tail_prod (f, &head, &tail);
f->ooos_newest = OOO_SEGMENT_INVALID_INDEX;
- ASSERT (len < f->nitems);
svm_fifo_trace_add (f, offset, len, 1);
ooo_segment_add (f, offset, head, tail, len);
- c = f->tail_chunk;
- tail_offset_idx = (tail + offset) % f->size;
- tail_offset_idx -= c->start_byte;
- n_chunk = c->length - tail_offset_idx;
- to_copy = len;
+ tail_idx = (tail + offset) % f->size;
- if (n_chunk < to_copy)
- {
- clib_memcpy_fast (&c->data[tail_offset_idx], src, n_chunk);
- while ((to_copy -= n_chunk))
- {
- c = c->next;
- n_chunk = clib_min (c->length, to_copy);
- clib_memcpy_fast (&c->data[0], src + (len - to_copy), n_chunk);
- }
- }
- else
- {
- clib_memcpy_fast (&c->data[tail_offset_idx], src, len);
- }
+ if (!svm_fifo_chunk_includes_pos (f->ooo_enq, tail_idx))
+ f->ooo_enq = svm_fifo_find_chunk (f, tail_idx);
+
+ svm_fifo_copy_to_chunk (f, f->ooo_enq, tail_idx, src, len, &f->ooo_enq);
return 0;
}
-#ifndef CLIB_MARCH_VARIANT
-
int
-svm_fifo_enqueue_with_offset (svm_fifo_t * f, u32 offset, u32 required_bytes,
- u8 * copy_from_here)
-{
- return CLIB_MARCH_FN_SELECT (svm_fifo_enqueue_with_offset) (f, offset,
- required_bytes,
- copy_from_here);
-}
-
-void
-svm_fifo_overwrite_head (svm_fifo_t * f, u8 * data, u32 len)
+svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 len, u8 * dst)
{
- u32 n_chunk;
- u32 head, tail, head_idx;
- svm_fifo_chunk_t *c;
-
- ASSERT (len <= f->nitems);
-
- f_load_head_tail_cons (f, &head, &tail);
- c = f->head_chunk;
- head_idx = head % f->size;
- head_idx -= c->start_byte;
- n_chunk = c->length - head_idx;
- if (len <= n_chunk)
- clib_memcpy_fast (&c->data[head_idx], data, len);
- else
- {
- clib_memcpy_fast (&c->data[head_idx], data, n_chunk);
- clib_memcpy_fast (&c->next->data[0], data + n_chunk, len - n_chunk);
- }
-}
-#endif
-
-CLIB_MARCH_FN (svm_fifo_dequeue_nowait, int, svm_fifo_t * f, u32 len,
- u8 * dst)
-{
- u32 to_copy, n_chunk, tail, head, cursize, head_idx;
- svm_fifo_chunk_t *c;
+ u32 tail, head, cursize;
f_load_head_tail_cons (f, &head, &tail);
if (PREDICT_FALSE (cursize == 0))
return -2; /* nothing in the fifo */
- to_copy = len = clib_min (cursize, len);
- ASSERT (cursize >= to_copy);
-
- c = f->head_chunk;
- head_idx = head % f->size;
- head_idx -= c->start_byte;
- n_chunk = c->length - head_idx;
+ len = clib_min (cursize, len);
- if (n_chunk < to_copy)
- {
- clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
- while ((to_copy -= n_chunk))
- {
- c = c->next;
- n_chunk = clib_min (c->length, to_copy);
- clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
- }
- f->head_chunk = c;
- }
- else
- {
- clib_memcpy_fast (dst, &c->data[head_idx], to_copy);
- }
+ svm_fifo_copy_from_chunk (f, f->head_chunk, head % f->size, dst, len,
+ &f->head_chunk);
head += len;
if (PREDICT_FALSE (f->flags & SVM_FIFO_F_SIZE_UPDATE))
return len;
}
-#ifndef CLIB_MARCH_VARIANT
-
int
-svm_fifo_dequeue_nowait (svm_fifo_t * f, u32 max_bytes, u8 * copy_here)
+svm_fifo_peek (svm_fifo_t * f, u32 offset, u32 len, u8 * dst)
{
- return CLIB_MARCH_FN_SELECT (svm_fifo_dequeue_nowait) (f, max_bytes,
- copy_here);
-}
-#endif
-
-CLIB_MARCH_FN (svm_fifo_peek, int, svm_fifo_t * f, u32 relative_offset,
- u32 len, u8 * dst)
-{
- u32 to_copy, n_chunk, tail, head, cursize, head_idx;
- svm_fifo_chunk_t *c;
+ u32 tail, head, cursize, head_idx;
f_load_head_tail_cons (f, &head, &tail);
/* current size of fifo can only increase during peek: SPSC */
cursize = f_cursize (f, head, tail);
- if (PREDICT_FALSE (cursize < relative_offset))
+ if (PREDICT_FALSE (cursize < offset))
return -2; /* nothing in the fifo */
- to_copy = len = clib_min (cursize - relative_offset, len);
+ len = clib_min (cursize - offset, len);
+ head_idx = (head + offset) % f->size;
+ if (!svm_fifo_chunk_includes_pos (f->ooo_deq, head_idx))
+ f->ooo_deq = svm_fifo_find_chunk (f, head_idx);
- c = f->head_chunk;
- head_idx = (head + relative_offset) % f->size;
- head_idx -= c->start_byte;
- n_chunk = c->length - head_idx;
-
- if (n_chunk < to_copy)
- {
- clib_memcpy_fast (dst, &c->data[head_idx], n_chunk);
- while ((to_copy -= n_chunk))
- {
- c = c->next;
- n_chunk = clib_min (c->length, to_copy);
- clib_memcpy_fast (dst + (len - to_copy), &c->data[0], n_chunk);
- }
- f->head_chunk = c;
- }
- else
- {
- clib_memcpy_fast (dst, &c->data[head_idx], to_copy);
- }
+ svm_fifo_copy_from_chunk (f, f->ooo_deq, head_idx, dst, len, &f->ooo_deq);
return len;
}
-#ifndef CLIB_MARCH_VARIANT
-
-int
-svm_fifo_peek (svm_fifo_t * f, u32 relative_offset, u32 max_bytes,
- u8 * copy_here)
-{
- return CLIB_MARCH_FN_SELECT (svm_fifo_peek) (f, relative_offset, max_bytes,
- copy_here);
-}
-
int
svm_fifo_dequeue_drop (svm_fifo_t * f, u32 max_bytes)
{
* Set fifo pointers to requested offset
*/
void
-svm_fifo_init_pointers (svm_fifo_t * f, u32 pointer)
+svm_fifo_init_pointers (svm_fifo_t * f, u32 head, u32 tail)
{
- clib_atomic_store_rel_n (&f->head, pointer);
- clib_atomic_store_rel_n (&f->tail, pointer);
+ clib_atomic_store_rel_n (&f->head, head);
+ clib_atomic_store_rel_n (&f->tail, tail);
+ if (f->flags & SVM_FIFO_F_MULTI_CHUNK)
+ {
+ svm_fifo_chunk_t *c;
+ c = svm_fifo_find_chunk (f, head % f->size);
+ ASSERT (c != 0);
+ f->head_chunk = f->ooo_deq = c;
+ c = svm_fifo_find_chunk (f, tail % f->size);
+ ASSERT (c != 0);
+ f->tail_chunk = f->ooo_enq = c;
+ }
}
void