1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 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_launch.h>
19 #include <rte_per_lcore.h>
20 #include <rte_lcore.h>
21 #include <rte_branch_prediction.h>
22 #include <rte_debug.h>
25 #include <rte_mempool.h>
26 #include <rte_memcpy.h>
28 #include <rte_interrupts.h>
29 #include <rte_ether.h>
30 #include <rte_ethdev.h>
31 #include <rte_malloc.h>
32 #include <rte_string_fns.h>
33 #include <rte_cycles.h>
41 #define MBUFS_PER_NODE 1536
42 #define MBUFS_PER_PORT 1536
43 #define MBUF_CACHE_SIZE 512
45 #define RTE_MP_RX_DESC_DEFAULT 512
46 #define RTE_MP_TX_DESC_DEFAULT 512
47 #define NODE_QUEUE_RINGSIZE 128
51 /* The mbuf pool for packet rx */
52 struct rte_mempool *pktmbuf_pool;
54 /* array of info/queues for nodes */
58 struct rte_efd_table *efd_table;
60 /* Shared info between server and nodes */
61 struct shared_info *info;
64 * Initialise the mbuf pool for packet reception for the NIC, and any other
65 * buffer pools needed by the app - currently none.
70 const unsigned int num_mbufs = (num_nodes * MBUFS_PER_NODE) +
71 (info->num_ports * MBUFS_PER_PORT);
74 * Don't pass single-producer/single-consumer flags to mbuf create as it
75 * seems faster to use a cache instead
77 printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
78 PKTMBUF_POOL_NAME, num_mbufs);
79 pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
80 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
82 return pktmbuf_pool == NULL; /* 0 on success */
86 * Initialise an individual port:
87 * - configure number of rx and tx rings
88 * - set up each rx ring, to pull from the main mbuf pool
89 * - set up each tx ring
90 * - start the port and report its status to stdout
93 init_port(uint16_t port_num)
95 /* for port configuration all features are off by default */
96 struct rte_eth_conf port_conf = {
98 .mq_mode = RTE_ETH_MQ_RX_RSS,
101 const uint16_t rx_rings = 1, tx_rings = num_nodes;
102 uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
103 uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
104 struct rte_eth_dev_info dev_info;
105 struct rte_eth_txconf txconf;
110 printf("Port %u init ... ", port_num);
113 retval = rte_eth_dev_info_get(port_num, &dev_info);
117 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
118 port_conf.txmode.offloads |=
119 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
122 * Standard DPDK port initialisation - config port, then set up
125 retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings, &port_conf);
129 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
134 for (q = 0; q < rx_rings; q++) {
135 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
136 rte_eth_dev_socket_id(port_num),
142 txconf = dev_info.default_txconf;
143 txconf.offloads = port_conf.txmode.offloads;
144 for (q = 0; q < tx_rings; q++) {
145 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
146 rte_eth_dev_socket_id(port_num),
152 retval = rte_eth_promiscuous_enable(port_num);
156 retval = rte_eth_dev_start(port_num);
166 * Set up the DPDK rings which will be used to pass packets, via
167 * pointers, between the multi-process server and node processes.
168 * Each node needs one RX queue.
174 unsigned int socket_id;
176 const unsigned int ringsize = NODE_QUEUE_RINGSIZE;
178 nodes = rte_malloc("node details",
179 sizeof(*nodes) * num_nodes, 0);
181 rte_exit(EXIT_FAILURE, "Cannot allocate memory for "
182 "node program details\n");
184 for (i = 0; i < num_nodes; i++) {
185 /* Create an RX queue for each node */
186 socket_id = rte_socket_id();
187 q_name = get_rx_queue_name(i);
188 nodes[i].rx_q = rte_ring_create(q_name,
190 RING_F_SP_ENQ | RING_F_SC_DEQ);
191 if (nodes[i].rx_q == NULL)
192 rte_exit(EXIT_FAILURE, "Cannot create rx ring queue "
199 * Create EFD table which will contain all the flows
200 * that will be distributed among the nodes
203 /* Create EFD table. 8< */
205 create_efd_table(void)
207 uint8_t socket_id = rte_socket_id();
210 efd_table = rte_efd_create("flow table", num_flows * 2, sizeof(uint32_t),
211 1 << socket_id, socket_id);
213 if (efd_table == NULL)
214 rte_exit(EXIT_FAILURE, "Problem creating the flow table\n");
218 populate_efd_table(void)
223 uint8_t socket_id = rte_socket_id();
226 /* Add flows in table */
227 for (i = 0; i < num_flows; i++) {
228 node_id = i % num_nodes;
230 ip_dst = rte_cpu_to_be_32(i);
231 ret = rte_efd_update(efd_table, socket_id,
232 (void *)&ip_dst, (efd_value_t)node_id);
234 rte_exit(EXIT_FAILURE, "Unable to add entry %u in "
238 printf("EFD table: Adding 0x%x keys\n", num_flows);
240 /* >8 End of creation EFD table. */
242 /* Check the link status of all ports in up to 9s, and print them finally */
244 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
246 #define CHECK_INTERVAL 100 /* 100ms */
247 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
248 uint8_t count, all_ports_up, print_flag = 0;
250 struct rte_eth_link link;
252 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
254 printf("\nChecking link status");
256 for (count = 0; count <= MAX_CHECK_TIME; count++) {
258 for (portid = 0; portid < port_num; portid++) {
259 if ((port_mask & (1 << info->id[portid])) == 0)
261 memset(&link, 0, sizeof(link));
262 ret = rte_eth_link_get_nowait(info->id[portid], &link);
266 printf("Port %u link get failed: %s\n",
267 portid, rte_strerror(-ret));
270 /* print link status if flag set */
271 if (print_flag == 1) {
272 rte_eth_link_to_str(link_status_text,
273 sizeof(link_status_text), &link);
274 printf("Port %d %s\n", info->id[portid],
278 /* clear all_ports_up flag if any link down */
279 if (link.link_status == RTE_ETH_LINK_DOWN) {
284 /* after finally printing all link status, get out */
288 if (all_ports_up == 0) {
291 rte_delay_ms(CHECK_INTERVAL);
294 /* set the print_flag if all ports up or timeout */
295 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
303 * Main init function for the multi-process server app,
304 * calls subfunctions to do each stage of the initialisation.
307 init(int argc, char *argv[])
310 const struct rte_memzone *mz;
311 uint8_t i, total_ports;
313 /* init EAL, parsing EAL args */
314 retval = rte_eal_init(argc, argv);
320 /* get total number of ports */
321 total_ports = rte_eth_dev_count_avail();
323 /* set up array for port data */
324 mz = rte_memzone_reserve(MZ_SHARED_INFO, sizeof(*info),
325 rte_socket_id(), NO_FLAGS);
327 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone "
328 "for port information\n");
329 memset(mz->addr, 0, sizeof(*info));
332 /* parse additional, application arguments */
333 retval = parse_app_args(total_ports, argc, argv);
337 /* initialise mbuf pools */
338 retval = init_mbuf_pools();
340 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
342 /* now initialise the ports we will use */
343 for (i = 0; i < info->num_ports; i++) {
344 retval = init_port(info->id[i]);
346 rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
350 check_all_ports_link_status(info->num_ports, (~0x0));
352 /* initialise the node queues/rings for inter-eu comms */
355 /* Create the EFD table */
358 /* Populate the EFD table */
359 populate_efd_table();
361 /* Share the total number of nodes */
362 info->num_nodes = num_nodes;
364 /* Share the total number of flows */
365 info->num_flows = num_flows;