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.
36 #include <rte_ethdev.h>
37 #include <rte_malloc.h>
39 #include "rte_port_ethdev.h"
44 struct rte_port_ethdev_reader {
50 rte_port_ethdev_reader_create(void *params, int socket_id)
52 struct rte_port_ethdev_reader_params *conf =
53 (struct rte_port_ethdev_reader_params *) params;
54 struct rte_port_ethdev_reader *port;
56 /* Check input parameters */
58 RTE_LOG(ERR, PORT, "%s: params is NULL\n", __func__);
62 /* Memory allocation */
63 port = rte_zmalloc_socket("PORT", sizeof(*port),
64 RTE_CACHE_LINE_SIZE, socket_id);
66 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
71 port->port_id = conf->port_id;
72 port->queue_id = conf->queue_id;
78 rte_port_ethdev_reader_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
80 struct rte_port_ethdev_reader *p =
81 (struct rte_port_ethdev_reader *) port;
83 return rte_eth_rx_burst(p->port_id, p->queue_id, pkts, n_pkts);
87 rte_port_ethdev_reader_free(void *port)
90 RTE_LOG(ERR, PORT, "%s: port is NULL\n", __func__);
102 #define RTE_PORT_ETHDEV_WRITER_APPROACH 1
104 struct rte_port_ethdev_writer {
105 struct rte_mbuf *tx_buf[2 * RTE_PORT_IN_BURST_SIZE_MAX];
106 uint32_t tx_burst_sz;
107 uint16_t tx_buf_count;
114 rte_port_ethdev_writer_create(void *params, int socket_id)
116 struct rte_port_ethdev_writer_params *conf =
117 (struct rte_port_ethdev_writer_params *) params;
118 struct rte_port_ethdev_writer *port;
120 /* Check input parameters */
121 if ((conf == NULL) ||
122 (conf->tx_burst_sz == 0) ||
123 (conf->tx_burst_sz > RTE_PORT_IN_BURST_SIZE_MAX) ||
124 (!rte_is_power_of_2(conf->tx_burst_sz))) {
125 RTE_LOG(ERR, PORT, "%s: Invalid input parameters\n", __func__);
129 /* Memory allocation */
130 port = rte_zmalloc_socket("PORT", sizeof(*port),
131 RTE_CACHE_LINE_SIZE, socket_id);
133 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
138 port->port_id = conf->port_id;
139 port->queue_id = conf->queue_id;
140 port->tx_burst_sz = conf->tx_burst_sz;
141 port->tx_buf_count = 0;
142 port->bsz_mask = 1LLU << (conf->tx_burst_sz - 1);
148 send_burst(struct rte_port_ethdev_writer *p)
152 nb_tx = rte_eth_tx_burst(p->port_id, p->queue_id,
153 p->tx_buf, p->tx_buf_count);
155 for ( ; nb_tx < p->tx_buf_count; nb_tx++)
156 rte_pktmbuf_free(p->tx_buf[nb_tx]);
162 rte_port_ethdev_writer_tx(void *port, struct rte_mbuf *pkt)
164 struct rte_port_ethdev_writer *p =
165 (struct rte_port_ethdev_writer *) port;
167 p->tx_buf[p->tx_buf_count++] = pkt;
168 if (p->tx_buf_count >= p->tx_burst_sz)
174 #if RTE_PORT_ETHDEV_WRITER_APPROACH == 0
177 rte_port_ethdev_writer_tx_bulk(void *port,
178 struct rte_mbuf **pkts,
181 struct rte_port_ethdev_writer *p =
182 (struct rte_port_ethdev_writer *) port;
184 if ((pkts_mask & (pkts_mask + 1)) == 0) {
185 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
188 for (i = 0; i < n_pkts; i++) {
189 struct rte_mbuf *pkt = pkts[i];
191 p->tx_buf[p->tx_buf_count++] = pkt;
192 if (p->tx_buf_count >= p->tx_burst_sz)
196 for ( ; pkts_mask; ) {
197 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
198 uint64_t pkt_mask = 1LLU << pkt_index;
199 struct rte_mbuf *pkt = pkts[pkt_index];
201 p->tx_buf[p->tx_buf_count++] = pkt;
202 if (p->tx_buf_count >= p->tx_burst_sz)
204 pkts_mask &= ~pkt_mask;
211 #elif RTE_PORT_ETHDEV_WRITER_APPROACH == 1
214 rte_port_ethdev_writer_tx_bulk(void *port,
215 struct rte_mbuf **pkts,
218 struct rte_port_ethdev_writer *p =
219 (struct rte_port_ethdev_writer *) port;
220 uint32_t bsz_mask = p->bsz_mask;
221 uint32_t tx_buf_count = p->tx_buf_count;
222 uint64_t expr = (pkts_mask & (pkts_mask + 1)) |
223 ((pkts_mask & bsz_mask) ^ bsz_mask);
226 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
232 n_pkts_ok = rte_eth_tx_burst(p->port_id, p->queue_id, pkts,
235 for ( ; n_pkts_ok < n_pkts; n_pkts_ok++) {
236 struct rte_mbuf *pkt = pkts[n_pkts_ok];
238 rte_pktmbuf_free(pkt);
241 for ( ; pkts_mask; ) {
242 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
243 uint64_t pkt_mask = 1LLU << pkt_index;
244 struct rte_mbuf *pkt = pkts[pkt_index];
246 p->tx_buf[tx_buf_count++] = pkt;
247 pkts_mask &= ~pkt_mask;
250 p->tx_buf_count = tx_buf_count;
251 if (tx_buf_count >= p->tx_burst_sz)
260 #error Invalid value for RTE_PORT_ETHDEV_WRITER_APPROACH
265 rte_port_ethdev_writer_flush(void *port)
267 struct rte_port_ethdev_writer *p =
268 (struct rte_port_ethdev_writer *) port;
270 if (p->tx_buf_count > 0)
277 rte_port_ethdev_writer_free(void *port)
280 RTE_LOG(ERR, PORT, "%s: Port is NULL\n", __func__);
284 rte_port_ethdev_writer_flush(port);
291 * Summary of port operations
293 struct rte_port_in_ops rte_port_ethdev_reader_ops = {
294 .f_create = rte_port_ethdev_reader_create,
295 .f_free = rte_port_ethdev_reader_free,
296 .f_rx = rte_port_ethdev_reader_rx,
299 struct rte_port_out_ops rte_port_ethdev_writer_ops = {
300 .f_create = rte_port_ethdev_writer_create,
301 .f_free = rte_port_ethdev_writer_free,
302 .f_tx = rte_port_ethdev_writer_tx,
303 .f_tx_bulk = rte_port_ethdev_writer_tx_bulk,
304 .f_flush = rte_port_ethdev_writer_flush,