4 * Copyright(c) 2015 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.
38 #include <rte_common.h>
39 #include <rte_spinlock.h>
41 #include <rte_ethdev.h>
42 #include <rte_ether.h>
44 #include <rte_memory.h>
45 #include <rte_mempool.h>
50 #define MAX_PORTS RTE_MAX_ETHPORTS
51 #define MAX_BURST_LENGTH 32
52 #define PORT_RX_QUEUE_SIZE 128
53 #define PORT_TX_QUEUE_SIZE 256
54 #define PKTPOOL_EXTRA_SIZE 512
55 #define PKTPOOL_CACHE 32
60 struct rte_mbuf *buf_frames[MAX_BURST_LENGTH];
64 struct ether_addr mac_addr;
70 struct rte_mempool *pkt_pool;
74 struct app_port ports[MAX_PORTS];
80 struct app_config app_cfg;
83 void lock_port(int idx_port)
85 struct app_port *ptr_port = &app_cfg.ports[idx_port];
87 rte_spinlock_lock(&ptr_port->lock);
90 void unlock_port(int idx_port)
92 struct app_port *ptr_port = &app_cfg.ports[idx_port];
94 rte_spinlock_unlock(&ptr_port->lock);
97 void mark_port_active(int idx_port)
99 struct app_port *ptr_port = &app_cfg.ports[idx_port];
101 ptr_port->port_active = 1;
104 void mark_port_inactive(int idx_port)
106 struct app_port *ptr_port = &app_cfg.ports[idx_port];
108 ptr_port->port_active = 0;
111 void mark_port_newmac(int idx_port)
113 struct app_port *ptr_port = &app_cfg.ports[idx_port];
115 ptr_port->port_dirty = 1;
118 static void setup_ports(struct app_config *app_cfg, int cnt_ports)
122 struct rte_eth_conf cfg_port;
123 struct rte_eth_dev_info dev_info;
125 uint16_t nb_rxd = PORT_RX_QUEUE_SIZE;
126 uint16_t nb_txd = PORT_TX_QUEUE_SIZE;
128 memset(&cfg_port, 0, sizeof(cfg_port));
129 cfg_port.txmode.mq_mode = ETH_MQ_TX_NONE;
131 for (idx_port = 0; idx_port < cnt_ports; idx_port++) {
132 struct app_port *ptr_port = &app_cfg->ports[idx_port];
134 rte_eth_dev_info_get(idx_port, &dev_info);
135 size_pktpool = dev_info.rx_desc_lim.nb_max +
136 dev_info.tx_desc_lim.nb_max + PKTPOOL_EXTRA_SIZE;
138 snprintf(str_name, 16, "pkt_pool%i", idx_port);
139 ptr_port->pkt_pool = rte_pktmbuf_pool_create(
141 size_pktpool, PKTPOOL_CACHE,
143 RTE_MBUF_DEFAULT_BUF_SIZE,
146 if (ptr_port->pkt_pool == NULL)
147 rte_exit(EXIT_FAILURE,
148 "rte_pktmbuf_pool_create failed"
151 printf("Init port %i..\n", idx_port);
152 ptr_port->port_active = 1;
153 ptr_port->port_dirty = 0;
154 ptr_port->idx_port = idx_port;
156 if (rte_eth_dev_configure(idx_port, 1, 1, &cfg_port) < 0)
157 rte_exit(EXIT_FAILURE,
158 "rte_eth_dev_configure failed");
159 if (rte_eth_dev_adjust_nb_rx_tx_desc(idx_port, &nb_rxd,
161 rte_exit(EXIT_FAILURE,
162 "rte_eth_dev_adjust_nb_rx_tx_desc failed");
163 if (rte_eth_rx_queue_setup(
165 rte_eth_dev_socket_id(idx_port), NULL,
166 ptr_port->pkt_pool) < 0)
167 rte_exit(EXIT_FAILURE,
168 "rte_eth_rx_queue_setup failed"
170 if (rte_eth_tx_queue_setup(
172 rte_eth_dev_socket_id(idx_port), NULL) < 0)
173 rte_exit(EXIT_FAILURE,
174 "rte_eth_tx_queue_setup failed"
176 if (rte_eth_dev_start(idx_port) < 0)
177 rte_exit(EXIT_FAILURE,
178 "%s:%i: rte_eth_dev_start failed",
181 rte_eth_macaddr_get(idx_port, &ptr_port->mac_addr);
182 rte_spinlock_init(&ptr_port->lock);
186 static void process_frame(struct app_port *ptr_port,
187 struct rte_mbuf *ptr_frame)
189 struct ether_hdr *ptr_mac_hdr;
191 ptr_mac_hdr = rte_pktmbuf_mtod(ptr_frame, struct ether_hdr *);
192 ether_addr_copy(&ptr_mac_hdr->s_addr, &ptr_mac_hdr->d_addr);
193 ether_addr_copy(&ptr_port->mac_addr, &ptr_mac_hdr->s_addr);
196 static int slave_main(__attribute__((unused)) void *ptr_data)
198 struct app_port *ptr_port;
199 struct rte_mbuf *ptr_frame;
200 struct txq_port *txq;
202 uint16_t cnt_recv_frames;
206 uint16_t lock_result;
208 while (app_cfg.exit_now == 0) {
209 for (idx_port = 0; idx_port < app_cfg.cnt_ports; idx_port++) {
210 /* Check that port is active and unlocked */
211 ptr_port = &app_cfg.ports[idx_port];
212 lock_result = rte_spinlock_trylock(&ptr_port->lock);
213 if (lock_result == 0)
215 if (ptr_port->port_active == 0) {
216 rte_spinlock_unlock(&ptr_port->lock);
219 txq = &ptr_port->txq;
221 /* MAC address was updated */
222 if (ptr_port->port_dirty == 1) {
223 rte_eth_macaddr_get(ptr_port->idx_port,
224 &ptr_port->mac_addr);
225 ptr_port->port_dirty = 0;
228 /* Incoming frames */
229 cnt_recv_frames = rte_eth_rx_burst(
230 ptr_port->idx_port, 0,
231 &txq->buf_frames[txq->cnt_unsent],
232 RTE_DIM(txq->buf_frames) - txq->cnt_unsent
234 if (cnt_recv_frames > 0) {
236 idx_frame < cnt_recv_frames;
238 ptr_frame = txq->buf_frames[
239 idx_frame + txq->cnt_unsent];
240 process_frame(ptr_port, ptr_frame);
242 txq->cnt_unsent += cnt_recv_frames;
245 /* Outgoing frames */
246 if (txq->cnt_unsent > 0) {
247 cnt_sent = rte_eth_tx_burst(
248 ptr_port->idx_port, 0,
252 /* Shuffle up unsent frame pointers */
253 for (idx_frame = cnt_sent;
254 idx_frame < txq->cnt_unsent;
256 txq->buf_frames[idx_frame - cnt_sent] =
257 txq->buf_frames[idx_frame];
258 txq->cnt_unsent -= cnt_sent;
260 rte_spinlock_unlock(&ptr_port->lock);
261 } /* end for( idx_port ) */
267 int main(int argc, char **argv)
273 /* Init runtime environment */
274 cnt_args_parsed = rte_eal_init(argc, argv);
275 if (cnt_args_parsed < 0)
276 rte_exit(EXIT_FAILURE, "rte_eal_init(): Failed");
278 cnt_ports = rte_eth_dev_count();
279 printf("Number of NICs: %i\n", cnt_ports);
281 rte_exit(EXIT_FAILURE, "No available NIC ports!\n");
282 if (cnt_ports > MAX_PORTS) {
283 printf("Info: Using only %i of %i ports\n",
286 cnt_ports = MAX_PORTS;
289 setup_ports(&app_cfg, cnt_ports);
291 app_cfg.exit_now = 0;
292 app_cfg.cnt_ports = cnt_ports;
294 if (rte_lcore_count() < 2)
295 rte_exit(EXIT_FAILURE, "No available slave core!\n");
296 /* Assume there is an available slave.. */
297 id_core = rte_lcore_id();
298 id_core = rte_get_next_lcore(id_core, 1, 1);
299 rte_eal_remote_launch(slave_main, NULL, id_core);
303 app_cfg.exit_now = 1;
304 RTE_LCORE_FOREACH_SLAVE(id_core) {
305 if (rte_eal_wait_lcore(id_core) < 0)