4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <rte_malloc.h>
39 #include "rte_port_ring.h"
44 struct rte_port_ring_reader {
45 struct rte_ring *ring;
49 rte_port_ring_reader_create(void *params, int socket_id)
51 struct rte_port_ring_reader_params *conf =
52 (struct rte_port_ring_reader_params *) params;
53 struct rte_port_ring_reader *port;
55 /* Check input parameters */
57 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
61 /* Memory allocation */
62 port = rte_zmalloc_socket("PORT", sizeof(*port),
63 CACHE_LINE_SIZE, socket_id);
65 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
70 port->ring = conf->ring;
76 rte_port_ring_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
78 struct rte_port_ring_reader *p = (struct rte_port_ring_reader *) port;
80 return rte_ring_sc_dequeue_burst(p->ring, (void **) pkts, n_pkts);
84 rte_port_ring_reader_free(void *port)
87 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
99 struct rte_port_ring_writer {
100 struct rte_mbuf *tx_buf[RTE_PORT_IN_BURST_SIZE_MAX];
101 struct rte_ring *ring;
102 uint32_t tx_burst_sz;
103 uint32_t tx_buf_count;
107 rte_port_ring_writer_create(void *params, int socket_id)
109 struct rte_port_ring_writer_params *conf =
110 (struct rte_port_ring_writer_params *) params;
111 struct rte_port_ring_writer *port;
113 /* Check input parameters */
114 if ((conf == NULL) ||
115 (conf->ring == NULL) ||
116 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX)) {
117 RTE_LOG(ERR, PORT, "%s: Invalid Parameters\n", __func__);
121 /* Memory allocation */
122 port = rte_zmalloc_socket("PORT", sizeof(*port),
123 CACHE_LINE_SIZE, socket_id);
125 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
130 port->ring = conf->ring;
131 port->tx_burst_sz = conf->tx_burst_sz;
132 port->tx_buf_count = 0;
138 send_burst(struct rte_port_ring_writer *p)
142 nb_tx = rte_ring_sp_enqueue_burst(p->ring, (void **)p->tx_buf,
145 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
146 rte_pktmbuf_free(p->tx_buf[nb_tx]);
152 rte_port_ring_writer_tx(void *port, struct rte_mbuf *pkt)
154 struct rte_port_ring_writer *p = (struct rte_port_ring_writer *) port;
156 p->tx_buf[p->tx_buf_count++] = pkt;
157 if (p->tx_buf_count >= p->tx_burst_sz)
164 rte_port_ring_writer_tx_bulk(void *port,
165 struct rte_mbuf **pkts,
168 struct rte_port_ring_writer *p = (struct rte_port_ring_writer *) port;
170 if ((pkts_mask & (pkts_mask + 1)) == 0) {
171 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
174 for (i = 0; i < n_pkts; i++) {
175 struct rte_mbuf *pkt = pkts[i];
177 p->tx_buf[p->tx_buf_count++] = pkt;
178 if (p->tx_buf_count >= p->tx_burst_sz)
182 for ( ; pkts_mask; ) {
183 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
184 uint64_t pkt_mask = 1LLU << pkt_index;
185 struct rte_mbuf *pkt = pkts[pkt_index];
187 p->tx_buf[p->tx_buf_count++] = pkt;
188 if (p->tx_buf_count >= p->tx_burst_sz)
190 pkts_mask &= ~pkt_mask;
198 rte_port_ring_writer_flush(void *port)
200 struct rte_port_ring_writer *p = (struct rte_port_ring_writer *) port;
202 if (p->tx_buf_count > 0)
209 rte_port_ring_writer_free(void *port)
212 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
216 rte_port_ring_writer_flush(port);
223 * Summary of port operations
225 struct rte_port_in_ops rte_port_ring_reader_ops = {
226 .f_create = rte_port_ring_reader_create,
227 .f_free = rte_port_ring_reader_free,
228 .f_rx = rte_port_ring_reader_rx,
231 struct rte_port_out_ops rte_port_ring_writer_ops = {
232 .f_create = rte_port_ring_writer_create,
233 .f_free = rte_port_ring_writer_free,
234 .f_tx = rte_port_ring_writer_tx,
235 .f_tx_bulk = rte_port_ring_writer_tx_bulk,
236 .f_flush = rte_port_ring_writer_flush,