1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
8 #include <rte_ethdev.h>
9 #include <rte_cycles.h>
10 #include <rte_lcore.h>
13 #define RX_RING_SIZE 128
14 #define TX_RING_SIZE 512
16 #define NUM_MBUFS 8191
17 #define MBUF_CACHE_SIZE 250
20 static const struct rte_eth_conf port_conf_default = {
21 .rxmode = { .max_rx_pkt_len = ETHER_MAX_LEN, },
24 static unsigned nb_ports;
27 uint64_t total_cycles;
33 add_timestamps(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
34 struct rte_mbuf **pkts, uint16_t nb_pkts,
35 uint16_t max_pkts __rte_unused, void *_ __rte_unused)
38 uint64_t now = rte_rdtsc();
40 for (i = 0; i < nb_pkts; i++)
41 pkts[i]->udata64 = now;
46 calc_latency(uint16_t port __rte_unused, uint16_t qidx __rte_unused,
47 struct rte_mbuf **pkts, uint16_t nb_pkts, void *_ __rte_unused)
50 uint64_t now = rte_rdtsc();
53 for (i = 0; i < nb_pkts; i++)
54 cycles += now - pkts[i]->udata64;
55 latency_numbers.total_cycles += cycles;
56 latency_numbers.total_pkts += nb_pkts;
58 if (latency_numbers.total_pkts > (100 * 1000 * 1000ULL)) {
59 printf("Latency = %"PRIu64" cycles\n",
60 latency_numbers.total_cycles / latency_numbers.total_pkts);
61 latency_numbers.total_cycles = latency_numbers.total_pkts = 0;
67 * Initialises a given port using global settings and with the rx buffers
68 * coming from the mbuf_pool passed as parameter
71 port_init(uint16_t port, struct rte_mempool *mbuf_pool)
73 struct rte_eth_conf port_conf = port_conf_default;
74 const uint16_t rx_rings = 1, tx_rings = 1;
75 uint16_t nb_rxd = RX_RING_SIZE;
76 uint16_t nb_txd = TX_RING_SIZE;
80 if (port >= rte_eth_dev_count())
83 retval = rte_eth_dev_configure(port, rx_rings, tx_rings, &port_conf);
87 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port, &nb_rxd, &nb_txd);
91 for (q = 0; q < rx_rings; q++) {
92 retval = rte_eth_rx_queue_setup(port, q, nb_rxd,
93 rte_eth_dev_socket_id(port), NULL, mbuf_pool);
98 for (q = 0; q < tx_rings; q++) {
99 retval = rte_eth_tx_queue_setup(port, q, nb_txd,
100 rte_eth_dev_socket_id(port), NULL);
105 retval = rte_eth_dev_start(port);
109 struct ether_addr addr;
111 rte_eth_macaddr_get(port, &addr);
112 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
113 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
115 addr.addr_bytes[0], addr.addr_bytes[1],
116 addr.addr_bytes[2], addr.addr_bytes[3],
117 addr.addr_bytes[4], addr.addr_bytes[5]);
119 rte_eth_promiscuous_enable(port);
120 rte_eth_add_rx_callback(port, 0, add_timestamps, NULL);
121 rte_eth_add_tx_callback(port, 0, calc_latency, NULL);
127 * Main thread that does the work, reading from INPUT_PORT
128 * and writing to OUTPUT_PORT
130 static __attribute__((noreturn)) void
135 for (port = 0; port < nb_ports; port++)
136 if (rte_eth_dev_socket_id(port) > 0 &&
137 rte_eth_dev_socket_id(port) !=
138 (int)rte_socket_id())
139 printf("WARNING, port %u is on remote NUMA node to "
140 "polling thread.\n\tPerformance will "
141 "not be optimal.\n", port);
143 printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n",
146 for (port = 0; port < nb_ports; port++) {
147 struct rte_mbuf *bufs[BURST_SIZE];
148 const uint16_t nb_rx = rte_eth_rx_burst(port, 0,
150 if (unlikely(nb_rx == 0))
152 const uint16_t nb_tx = rte_eth_tx_burst(port ^ 1, 0,
154 if (unlikely(nb_tx < nb_rx)) {
157 for (buf = nb_tx; buf < nb_rx; buf++)
158 rte_pktmbuf_free(bufs[buf]);
164 /* Main function, does initialisation and calls the per-lcore functions */
166 main(int argc, char *argv[])
168 struct rte_mempool *mbuf_pool;
172 int ret = rte_eal_init(argc, argv);
175 rte_exit(EXIT_FAILURE, "Error with EAL initialization\n");
179 nb_ports = rte_eth_dev_count();
180 if (nb_ports < 2 || (nb_ports & 1))
181 rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n");
183 mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL",
184 NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0,
185 RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
186 if (mbuf_pool == NULL)
187 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
189 /* initialize all ports */
190 for (portid = 0; portid < nb_ports; portid++)
191 if (port_init(portid, mbuf_pool) != 0)
192 rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n",
195 if (rte_lcore_count() > 1)
196 printf("\nWARNING: Too much enabled lcores - "
197 "App uses only 1 lcore\n");
199 /* call lcore_main on master core only */