1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
6 * Generic ring structure for passing events from one core to another.
8 * Used by the software scheduler for the producer and consumer rings for
9 * each port, i.e. for passing events from worker cores to scheduler and
10 * vice-versa. Designed for single-producer, single-consumer use with two
11 * cores working on each ring.
19 #include <rte_common.h>
20 #include <rte_memory.h>
21 #include <rte_malloc.h>
23 /* Custom single threaded ring implementation used for ROB */
30 void *ring[0] __rte_cache_aligned;
33 static inline struct rob_ring *
34 rob_ring_create(unsigned int size, unsigned int socket_id)
36 struct rob_ring *retval;
37 const uint32_t ring_size = rte_align32pow2(size + 1);
38 size_t memsize = sizeof(*retval) +
39 (ring_size * sizeof(retval->ring[0]));
41 retval = rte_zmalloc_socket(NULL, memsize, 0, socket_id);
44 retval->ring_size = ring_size;
45 retval->mask = ring_size - 1;
52 rob_ring_free(struct rob_ring *r)
57 static __rte_always_inline unsigned int
58 rob_ring_count(const struct rob_ring *r)
60 return r->write_idx - r->read_idx;
63 static __rte_always_inline unsigned int
64 rob_ring_free_count(const struct rob_ring *r)
66 return r->size - rob_ring_count(r);
69 static __rte_always_inline unsigned int
70 rob_ring_enqueue(struct rob_ring *r, void *re)
72 const uint32_t size = r->size;
73 const uint32_t mask = r->mask;
74 const uint32_t read = r->read_idx;
75 uint32_t write = r->write_idx;
76 const uint32_t space = read + size - write;
79 r->ring[write & mask] = re;
84 static __rte_always_inline unsigned int
85 rob_ring_dequeue(struct rob_ring *r, void **re)
87 const uint32_t mask = r->mask;
88 uint32_t read = r->read_idx;
89 const uint32_t write = r->write_idx;
90 const uint32_t items = write - read;
93 *re = r->ring[read & mask];