Add in a pair of functions which meet the criteria for rx_burst and tx_burst, which then allow a ring to be used as though it were an ethdev, so that code can be written agnostically. Provide a convertion function that takes a single ring and returns an index of the ethdev corresponding to it.
Signed-off-by: Bruce Richardson <bruce.richardson at intel.com> --- lib/librte_ring/Makefile | 1 + lib/librte_ring/rte_ring.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/librte_ring/rte_ring.h | 11 +++++++++++ 3 files changed, 54 insertions(+) diff --git a/lib/librte_ring/Makefile b/lib/librte_ring/Makefile index 64c3460..b1d35ac 100644 --- a/lib/librte_ring/Makefile +++ b/lib/librte_ring/Makefile @@ -34,6 +34,7 @@ include $(RTE_SDK)/mk/rte.vars.mk # library name LIB = librte_ring.a +CFLAGS += -I$(RTE_SDK)/lib/librte_ether CFLAGS += $(WERROR_FLAGS) -I$(SRCDIR) -O3 # all source are stored in SRCS-y diff --git a/lib/librte_ring/rte_ring.c b/lib/librte_ring/rte_ring.c index 3a919b0..56a46a7 100644 --- a/lib/librte_ring/rte_ring.c +++ b/lib/librte_ring/rte_ring.c @@ -86,6 +86,7 @@ #include <rte_errno.h> #include <rte_string_fns.h> #include <rte_spinlock.h> +#include <rte_ethdev.h> #include "rte_ring.h" @@ -136,6 +137,7 @@ rte_ring_init(struct rte_ring *r, const char *name, unsigned count, /* init the ring structure */ memset(r, 0, sizeof(*r)); rte_snprintf(r->name, sizeof(r->name), "%s", name); + r->self = r; r->flags = flags; r->prod.watermark = count; r->prod.sp_enqueue = !!(flags & RING_F_SP_ENQ); @@ -319,3 +321,43 @@ rte_ring_lookup(const char *name) return r; } + +static uint16_t +ring_eth_rx_burst(void *rxq, struct rte_mbuf **rx_pkts, + uint16_t nb_pkts) +{ + struct rte_ring *r = rxq; + return rte_ring_dequeue_burst(r, (void *)rx_pkts, nb_pkts); +} + +static uint16_t +ring_eth_tx_burst(void *txq, struct rte_mbuf **tx_pkts, + uint16_t nb_pkts) +{ + struct rte_ring *r = txq; + return rte_ring_enqueue_burst(r, (void *)tx_pkts, nb_pkts); +} + +int +rte_ring_as_eth_dev(struct rte_ring *r) +{ + static struct eth_dev_ops ops = { NULL }; + + /* reserve an ethdev entry */ + struct rte_eth_dev *eth_dev = rte_eth_dev_allocate(); + if (eth_dev == NULL) + goto error; + + eth_dev->dev_ops = &ops; + eth_dev->rx_pkt_burst = ring_eth_rx_burst; + eth_dev->tx_pkt_burst = ring_eth_tx_burst; + eth_dev->data->nb_rx_queues = 1; + eth_dev->data->rx_queues = &r->self; + eth_dev->data->nb_tx_queues = 1; + eth_dev->data->tx_queues = &r->self; + + return eth_dev->data->port_id; + +error: + return -1; +} diff --git a/lib/librte_ring/rte_ring.h b/lib/librte_ring/rte_ring.h index da54e34..be6bc08 100644 --- a/lib/librte_ring/rte_ring.h +++ b/lib/librte_ring/rte_ring.h @@ -141,6 +141,7 @@ struct rte_ring { TAILQ_ENTRY(rte_ring) next; /**< Next in list. */ char name[RTE_RING_NAMESIZE]; /**< Name of the ring. */ + void *self; /**< Self pointer - used by ethdev fn */ int flags; /**< Flags supplied at creation. */ /** Ring producer status. */ @@ -296,6 +297,16 @@ struct rte_ring *rte_ring_create(const char *name, unsigned count, int socket_id, unsigned flags); /** + * Use a ring as though it were an ethernet port + * + * @param r + * Pointer to the ring structure + * @return + * The port number of the new ethdev to be used for rx/tx burst functions + */ +int rte_ring_as_eth_dev(struct rte_ring *r); + +/** * Change the high water mark. * * If *count* is 0, water marking is disabled. Otherwise, it is set to the -- 1.9.0