1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
9 #include <rte_common.h>
10 #include <rte_spinlock.h>
12 #include <rte_ethdev.h>
13 #include <rte_ether.h>
15 #include <rte_memory.h>
16 #include <rte_mempool.h>
21 #define MAX_PORTS RTE_MAX_ETHPORTS
22 #define MAX_BURST_LENGTH 32
23 #define PORT_RX_QUEUE_SIZE 128
24 #define PORT_TX_QUEUE_SIZE 256
25 #define PKTPOOL_EXTRA_SIZE 512
26 #define PKTPOOL_CACHE 32
31 struct rte_mbuf *buf_frames[MAX_BURST_LENGTH];
35 struct ether_addr mac_addr;
41 struct rte_mempool *pkt_pool;
45 struct app_port ports[MAX_PORTS];
51 struct app_config app_cfg;
54 void lock_port(int idx_port)
56 struct app_port *ptr_port = &app_cfg.ports[idx_port];
58 rte_spinlock_lock(&ptr_port->lock);
61 void unlock_port(int idx_port)
63 struct app_port *ptr_port = &app_cfg.ports[idx_port];
65 rte_spinlock_unlock(&ptr_port->lock);
68 void mark_port_active(int idx_port)
70 struct app_port *ptr_port = &app_cfg.ports[idx_port];
72 ptr_port->port_active = 1;
75 void mark_port_inactive(int idx_port)
77 struct app_port *ptr_port = &app_cfg.ports[idx_port];
79 ptr_port->port_active = 0;
82 void mark_port_newmac(int idx_port)
84 struct app_port *ptr_port = &app_cfg.ports[idx_port];
86 ptr_port->port_dirty = 1;
89 static void setup_ports(struct app_config *app_cfg, int cnt_ports)
93 struct rte_eth_conf cfg_port;
94 struct rte_eth_dev_info dev_info;
96 uint16_t nb_rxd = PORT_RX_QUEUE_SIZE;
97 uint16_t nb_txd = PORT_TX_QUEUE_SIZE;
98 struct rte_eth_txconf txconf;
100 memset(&cfg_port, 0, sizeof(cfg_port));
101 cfg_port.txmode.mq_mode = ETH_MQ_TX_NONE;
102 cfg_port.rxmode.ignore_offload_bitfield = 1;
104 for (idx_port = 0; idx_port < cnt_ports; idx_port++) {
105 struct app_port *ptr_port = &app_cfg->ports[idx_port];
107 rte_eth_dev_info_get(idx_port, &dev_info);
108 size_pktpool = dev_info.rx_desc_lim.nb_max +
109 dev_info.tx_desc_lim.nb_max + PKTPOOL_EXTRA_SIZE;
111 snprintf(str_name, 16, "pkt_pool%i", idx_port);
112 ptr_port->pkt_pool = rte_pktmbuf_pool_create(
114 size_pktpool, PKTPOOL_CACHE,
116 RTE_MBUF_DEFAULT_BUF_SIZE,
119 if (ptr_port->pkt_pool == NULL)
120 rte_exit(EXIT_FAILURE,
121 "rte_pktmbuf_pool_create failed"
124 printf("Init port %i..\n", idx_port);
125 ptr_port->port_active = 1;
126 ptr_port->port_dirty = 0;
127 ptr_port->idx_port = idx_port;
129 if (rte_eth_dev_configure(idx_port, 1, 1, &cfg_port) < 0)
130 rte_exit(EXIT_FAILURE,
131 "rte_eth_dev_configure failed");
132 if (rte_eth_dev_adjust_nb_rx_tx_desc(idx_port, &nb_rxd,
134 rte_exit(EXIT_FAILURE,
135 "rte_eth_dev_adjust_nb_rx_tx_desc failed");
137 if (rte_eth_rx_queue_setup(
139 rte_eth_dev_socket_id(idx_port), NULL,
140 ptr_port->pkt_pool) < 0)
141 rte_exit(EXIT_FAILURE,
142 "rte_eth_rx_queue_setup failed"
144 txconf = dev_info.default_txconf;
145 txconf.txq_flags = ETH_TXQ_FLAGS_IGNORE;
146 if (rte_eth_tx_queue_setup(
148 rte_eth_dev_socket_id(idx_port), &txconf) < 0)
149 rte_exit(EXIT_FAILURE,
150 "rte_eth_tx_queue_setup failed"
152 if (rte_eth_dev_start(idx_port) < 0)
153 rte_exit(EXIT_FAILURE,
154 "%s:%i: rte_eth_dev_start failed",
157 rte_eth_macaddr_get(idx_port, &ptr_port->mac_addr);
158 rte_spinlock_init(&ptr_port->lock);
162 static void process_frame(struct app_port *ptr_port,
163 struct rte_mbuf *ptr_frame)
165 struct ether_hdr *ptr_mac_hdr;
167 ptr_mac_hdr = rte_pktmbuf_mtod(ptr_frame, struct ether_hdr *);
168 ether_addr_copy(&ptr_mac_hdr->s_addr, &ptr_mac_hdr->d_addr);
169 ether_addr_copy(&ptr_port->mac_addr, &ptr_mac_hdr->s_addr);
172 static int slave_main(__attribute__((unused)) void *ptr_data)
174 struct app_port *ptr_port;
175 struct rte_mbuf *ptr_frame;
176 struct txq_port *txq;
178 uint16_t cnt_recv_frames;
182 uint16_t lock_result;
184 while (app_cfg.exit_now == 0) {
185 for (idx_port = 0; idx_port < app_cfg.cnt_ports; idx_port++) {
186 /* Check that port is active and unlocked */
187 ptr_port = &app_cfg.ports[idx_port];
188 lock_result = rte_spinlock_trylock(&ptr_port->lock);
189 if (lock_result == 0)
191 if (ptr_port->port_active == 0) {
192 rte_spinlock_unlock(&ptr_port->lock);
195 txq = &ptr_port->txq;
197 /* MAC address was updated */
198 if (ptr_port->port_dirty == 1) {
199 rte_eth_macaddr_get(ptr_port->idx_port,
200 &ptr_port->mac_addr);
201 ptr_port->port_dirty = 0;
204 /* Incoming frames */
205 cnt_recv_frames = rte_eth_rx_burst(
206 ptr_port->idx_port, 0,
207 &txq->buf_frames[txq->cnt_unsent],
208 RTE_DIM(txq->buf_frames) - txq->cnt_unsent
210 if (cnt_recv_frames > 0) {
212 idx_frame < cnt_recv_frames;
214 ptr_frame = txq->buf_frames[
215 idx_frame + txq->cnt_unsent];
216 process_frame(ptr_port, ptr_frame);
218 txq->cnt_unsent += cnt_recv_frames;
221 /* Outgoing frames */
222 if (txq->cnt_unsent > 0) {
223 cnt_sent = rte_eth_tx_burst(
224 ptr_port->idx_port, 0,
228 /* Shuffle up unsent frame pointers */
229 for (idx_frame = cnt_sent;
230 idx_frame < txq->cnt_unsent;
232 txq->buf_frames[idx_frame - cnt_sent] =
233 txq->buf_frames[idx_frame];
234 txq->cnt_unsent -= cnt_sent;
236 rte_spinlock_unlock(&ptr_port->lock);
237 } /* end for( idx_port ) */
243 int main(int argc, char **argv)
249 /* Init runtime environment */
250 cnt_args_parsed = rte_eal_init(argc, argv);
251 if (cnt_args_parsed < 0)
252 rte_exit(EXIT_FAILURE, "rte_eal_init(): Failed");
254 cnt_ports = rte_eth_dev_count();
255 printf("Number of NICs: %i\n", cnt_ports);
257 rte_exit(EXIT_FAILURE, "No available NIC ports!\n");
258 if (cnt_ports > MAX_PORTS) {
259 printf("Info: Using only %i of %i ports\n",
262 cnt_ports = MAX_PORTS;
265 setup_ports(&app_cfg, cnt_ports);
267 app_cfg.exit_now = 0;
268 app_cfg.cnt_ports = cnt_ports;
270 if (rte_lcore_count() < 2)
271 rte_exit(EXIT_FAILURE, "No available slave core!\n");
272 /* Assume there is an available slave.. */
273 id_core = rte_lcore_id();
274 id_core = rte_get_next_lcore(id_core, 1, 1);
275 rte_eal_remote_launch(slave_main, NULL, id_core);
279 app_cfg.exit_now = 1;
280 RTE_LCORE_FOREACH_SLAVE(id_core) {
281 if (rte_eal_wait_lcore(id_core) < 0)