1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <rte_common.h>
14 #include <rte_memory.h>
15 #include <rte_memzone.h>
17 #include <rte_byteorder.h>
18 #include <rte_atomic.h>
19 #include <rte_launch.h>
20 #include <rte_per_lcore.h>
21 #include <rte_lcore.h>
22 #include <rte_branch_prediction.h>
23 #include <rte_debug.h>
26 #include <rte_mempool.h>
27 #include <rte_memcpy.h>
29 #include <rte_interrupts.h>
30 #include <rte_ether.h>
31 #include <rte_ethdev.h>
32 #include <rte_malloc.h>
33 #include <rte_string_fns.h>
34 #include <rte_cycles.h>
40 #define MBUF_CACHE_SIZE 512
42 #define RTE_MP_RX_DESC_DEFAULT 1024
43 #define RTE_MP_TX_DESC_DEFAULT 1024
44 #define CLIENT_QUEUE_RINGSIZE 128
48 /* The mbuf pool for packet rx */
49 struct rte_mempool *pktmbuf_pool;
51 /* array of info/queues for clients */
52 struct client *clients = NULL;
54 /* the port details */
55 struct port_info *ports;
58 * Initialise the mbuf pool for packet reception for the NIC, and any other
59 * buffer pools needed by the app - currently none.
64 const unsigned int num_mbufs_server =
65 RTE_MP_RX_DESC_DEFAULT * ports->num_ports;
66 const unsigned int num_mbufs_client =
67 num_clients * (CLIENT_QUEUE_RINGSIZE +
68 RTE_MP_TX_DESC_DEFAULT * ports->num_ports);
69 const unsigned int num_mbufs_mp_cache =
70 (num_clients + 1) * MBUF_CACHE_SIZE;
71 const unsigned int num_mbufs =
72 num_mbufs_server + num_mbufs_client + num_mbufs_mp_cache;
74 /* don't pass single-producer/single-consumer flags to mbuf create as it
75 * seems faster to use a cache instead */
76 printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
77 PKTMBUF_POOL_NAME, num_mbufs);
78 pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
79 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
81 return pktmbuf_pool == NULL; /* 0 on success */
85 * Initialise an individual port:
86 * - configure number of rx and tx rings
87 * - set up each rx ring, to pull from the main mbuf pool
88 * - set up each tx ring
89 * - start the port and report its status to stdout
92 init_port(uint16_t port_num)
94 /* for port configuration all features are off by default */
95 const struct rte_eth_conf port_conf = {
97 .mq_mode = ETH_MQ_RX_RSS
100 const uint16_t rx_rings = 1, tx_rings = num_clients;
101 uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
102 uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
107 printf("Port %u init ... ", port_num);
110 /* Standard DPDK port initialisation - config port, then set up
112 if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
116 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
121 for (q = 0; q < rx_rings; q++) {
122 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
123 rte_eth_dev_socket_id(port_num),
125 if (retval < 0) return retval;
128 for ( q = 0; q < tx_rings; q ++ ) {
129 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
130 rte_eth_dev_socket_id(port_num),
132 if (retval < 0) return retval;
135 rte_eth_promiscuous_enable(port_num);
137 retval = rte_eth_dev_start(port_num);
138 if (retval < 0) return retval;
146 * Set up the DPDK rings which will be used to pass packets, via
147 * pointers, between the multi-process server and client processes.
148 * Each client needs one RX queue.
156 const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
158 clients = rte_malloc("client details",
159 sizeof(*clients) * num_clients, 0);
161 rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
163 for (i = 0; i < num_clients; i++) {
164 /* Create an RX queue for each client */
165 socket_id = rte_socket_id();
166 q_name = get_rx_queue_name(i);
167 clients[i].rx_q = rte_ring_create(q_name,
169 RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
170 if (clients[i].rx_q == NULL)
171 rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
176 /* Check the link status of all ports in up to 9s, and print them finally */
178 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
180 #define CHECK_INTERVAL 100 /* 100ms */
181 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
183 uint8_t count, all_ports_up, print_flag = 0;
184 struct rte_eth_link link;
186 printf("\nChecking link status");
188 for (count = 0; count <= MAX_CHECK_TIME; count++) {
190 for (portid = 0; portid < port_num; portid++) {
191 if ((port_mask & (1 << ports->id[portid])) == 0)
193 memset(&link, 0, sizeof(link));
194 rte_eth_link_get_nowait(ports->id[portid], &link);
195 /* print link status if flag set */
196 if (print_flag == 1) {
197 if (link.link_status)
198 printf("Port %d Link Up - speed %u "
199 "Mbps - %s\n", ports->id[portid],
200 (unsigned)link.link_speed,
201 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
202 ("full-duplex") : ("half-duplex\n"));
204 printf("Port %d Link Down\n",
205 (uint8_t)ports->id[portid]);
208 /* clear all_ports_up flag if any link down */
209 if (link.link_status == ETH_LINK_DOWN) {
214 /* after finally printing all link status, get out */
218 if (all_ports_up == 0) {
221 rte_delay_ms(CHECK_INTERVAL);
224 /* set the print_flag if all ports up or timeout */
225 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
233 * Main init function for the multi-process server app,
234 * calls subfunctions to do each stage of the initialisation.
237 init(int argc, char *argv[])
240 const struct rte_memzone *mz;
241 uint16_t i, total_ports;
243 /* init EAL, parsing EAL args */
244 retval = rte_eal_init(argc, argv);
250 /* get total number of ports */
251 total_ports = rte_eth_dev_count_total();
253 /* set up array for port data */
254 mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
255 rte_socket_id(), NO_FLAGS);
257 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
258 memset(mz->addr, 0, sizeof(*ports));
261 /* parse additional, application arguments */
262 retval = parse_app_args(total_ports, argc, argv);
266 /* initialise mbuf pools */
267 retval = init_mbuf_pools();
269 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
271 /* now initialise the ports we will use */
272 for (i = 0; i < ports->num_ports; i++) {
273 retval = init_port(ports->id[i]);
275 rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
279 check_all_ports_link_status(ports->num_ports, (~0x0));
281 /* initialise the client queues/rings for inter-eu comms */