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_mempool.h>
38 #include <rte_malloc.h>
40 #include "rte_port_source_sink.h"
45 #ifdef RTE_PORT_STATS_COLLECT
47 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val) \
48 port->stats.n_pkts_in += val
49 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val) \
50 port->stats.n_pkts_drop += val
54 #define RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(port, val)
55 #define RTE_PORT_SOURCE_STATS_PKTS_DROP_ADD(port, val)
59 struct rte_port_source {
60 struct rte_port_in_stats stats;
62 struct rte_mempool *mempool;
66 rte_port_source_create(void *params, int socket_id)
68 struct rte_port_source_params *p =
69 (struct rte_port_source_params *) params;
70 struct rte_port_source *port;
72 /* Check input arguments*/
73 if ((p == NULL) || (p->mempool == NULL)) {
74 RTE_LOG(ERR, PORT, "%s: Invalid params\n", __func__);
78 /* Memory allocation */
79 port = rte_zmalloc_socket("PORT", sizeof(*port),
80 RTE_CACHE_LINE_SIZE, socket_id);
82 RTE_LOG(ERR, PORT, "%s: Failed to allocate port\n", __func__);
87 port->mempool = (struct rte_mempool *) p->mempool;
93 rte_port_source_free(void *port)
95 /* Check input parameters */
105 rte_port_source_rx(void *port, struct rte_mbuf **pkts, uint32_t n_pkts)
107 struct rte_port_source *p = (struct rte_port_source *) port;
109 if (rte_mempool_get_bulk(p->mempool, (void **) pkts, n_pkts) != 0)
112 RTE_PORT_SOURCE_STATS_PKTS_IN_ADD(p, n_pkts);
118 rte_port_source_stats_read(void *port,
119 struct rte_port_in_stats *stats, int clear)
121 struct rte_port_source *p =
122 (struct rte_port_source *) port;
125 memcpy(stats, &p->stats, sizeof(p->stats));
128 memset(&p->stats, 0, sizeof(p->stats));
137 rte_port_sink_create(__rte_unused void *params, __rte_unused int socket_id)
143 rte_port_sink_tx(__rte_unused void *port, struct rte_mbuf *pkt)
145 rte_pktmbuf_free(pkt);
151 rte_port_sink_tx_bulk(__rte_unused void *port, struct rte_mbuf **pkts,
154 if ((pkts_mask & (pkts_mask + 1)) == 0) {
155 uint64_t n_pkts = __builtin_popcountll(pkts_mask);
158 for (i = 0; i < n_pkts; i++) {
159 struct rte_mbuf *pkt = pkts[i];
161 rte_pktmbuf_free(pkt);
164 for ( ; pkts_mask; ) {
165 uint32_t pkt_index = __builtin_ctzll(pkts_mask);
166 uint64_t pkt_mask = 1LLU << pkt_index;
167 struct rte_mbuf *pkt = pkts[pkt_index];
169 rte_pktmbuf_free(pkt);
170 pkts_mask &= ~pkt_mask;
178 * Summary of port operations
180 struct rte_port_in_ops rte_port_source_ops = {
181 .f_create = rte_port_source_create,
182 .f_free = rte_port_source_free,
183 .f_rx = rte_port_source_rx,
184 .f_stats = rte_port_source_stats_read,
187 struct rte_port_out_ops rte_port_sink_ops = {
188 .f_create = rte_port_sink_create,
190 .f_tx = rte_port_sink_tx,
191 .f_tx_bulk = rte_port_sink_tx_bulk,