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_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>
42 #define MBUFS_PER_NODE 1536
43 #define MBUFS_PER_PORT 1536
44 #define MBUF_CACHE_SIZE 512
46 #define RTE_MP_RX_DESC_DEFAULT 512
47 #define RTE_MP_TX_DESC_DEFAULT 512
48 #define NODE_QUEUE_RINGSIZE 128
52 /* The mbuf pool for packet rx */
53 struct rte_mempool *pktmbuf_pool;
55 /* array of info/queues for nodes */
59 struct rte_efd_table *efd_table;
61 /* Shared info between server and nodes */
62 struct shared_info *info;
65 * Initialise the mbuf pool for packet reception for the NIC, and any other
66 * buffer pools needed by the app - currently none.
71 const unsigned int num_mbufs = (num_nodes * MBUFS_PER_NODE) +
72 (info->num_ports * MBUFS_PER_PORT);
75 * Don't pass single-producer/single-consumer flags to mbuf create as it
76 * seems faster to use a cache instead
78 printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
79 PKTMBUF_POOL_NAME, num_mbufs);
80 pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
81 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
83 return pktmbuf_pool == NULL; /* 0 on success */
87 * Initialise an individual port:
88 * - configure number of rx and tx rings
89 * - set up each rx ring, to pull from the main mbuf pool
90 * - set up each tx ring
91 * - start the port and report its status to stdout
94 init_port(uint16_t port_num)
96 /* for port configuration all features are off by default */
97 const struct rte_eth_conf port_conf = {
99 .mq_mode = ETH_MQ_RX_RSS
102 const uint16_t rx_rings = 1, tx_rings = num_nodes;
103 uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
104 uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
109 printf("Port %u init ... ", port_num);
113 * Standard DPDK port initialisation - config port, then set up
116 retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings, &port_conf);
120 retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
125 for (q = 0; q < rx_rings; q++) {
126 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
127 rte_eth_dev_socket_id(port_num),
133 for (q = 0; q < tx_rings; q++) {
134 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
135 rte_eth_dev_socket_id(port_num),
141 rte_eth_promiscuous_enable(port_num);
143 retval = rte_eth_dev_start(port_num);
153 * Set up the DPDK rings which will be used to pass packets, via
154 * pointers, between the multi-process server and node processes.
155 * Each node needs one RX queue.
161 unsigned int socket_id;
163 const unsigned int ringsize = NODE_QUEUE_RINGSIZE;
165 nodes = rte_malloc("node details",
166 sizeof(*nodes) * num_nodes, 0);
168 rte_exit(EXIT_FAILURE, "Cannot allocate memory for "
169 "node program details\n");
171 for (i = 0; i < num_nodes; i++) {
172 /* Create an RX queue for each node */
173 socket_id = rte_socket_id();
174 q_name = get_rx_queue_name(i);
175 nodes[i].rx_q = rte_ring_create(q_name,
177 RING_F_SP_ENQ | RING_F_SC_DEQ);
178 if (nodes[i].rx_q == NULL)
179 rte_exit(EXIT_FAILURE, "Cannot create rx ring queue "
186 * Create EFD table which will contain all the flows
187 * that will be distributed among the nodes
190 create_efd_table(void)
192 uint8_t socket_id = rte_socket_id();
195 efd_table = rte_efd_create("flow table", num_flows * 2, sizeof(uint32_t),
196 1 << socket_id, socket_id);
198 if (efd_table == NULL)
199 rte_exit(EXIT_FAILURE, "Problem creating the flow table\n");
203 populate_efd_table(void)
208 uint8_t socket_id = rte_socket_id();
211 /* Add flows in table */
212 for (i = 0; i < num_flows; i++) {
213 node_id = i % num_nodes;
215 ip_dst = rte_cpu_to_be_32(i);
216 ret = rte_efd_update(efd_table, socket_id,
217 (void *)&ip_dst, (efd_value_t)node_id);
219 rte_exit(EXIT_FAILURE, "Unable to add entry %u in "
223 printf("EFD table: Adding 0x%x keys\n", num_flows);
226 /* Check the link status of all ports in up to 9s, and print them finally */
228 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
230 #define CHECK_INTERVAL 100 /* 100ms */
231 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
232 uint8_t count, all_ports_up, print_flag = 0;
234 struct rte_eth_link link;
236 printf("\nChecking link status");
238 for (count = 0; count <= MAX_CHECK_TIME; count++) {
240 for (portid = 0; portid < port_num; portid++) {
241 if ((port_mask & (1 << info->id[portid])) == 0)
243 memset(&link, 0, sizeof(link));
244 rte_eth_link_get_nowait(info->id[portid], &link);
245 /* print link status if flag set */
246 if (print_flag == 1) {
247 if (link.link_status)
249 "Port%d Link Up. Speed %u Mbps - %s\n",
252 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
253 ("full-duplex") : ("half-duplex\n"));
255 printf("Port %d Link Down\n",
259 /* clear all_ports_up flag if any link down */
260 if (link.link_status == ETH_LINK_DOWN) {
265 /* after finally printing all link status, get out */
269 if (all_ports_up == 0) {
272 rte_delay_ms(CHECK_INTERVAL);
275 /* set the print_flag if all ports up or timeout */
276 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
284 * Main init function for the multi-process server app,
285 * calls subfunctions to do each stage of the initialisation.
288 init(int argc, char *argv[])
291 const struct rte_memzone *mz;
292 uint8_t i, total_ports;
294 /* init EAL, parsing EAL args */
295 retval = rte_eal_init(argc, argv);
301 /* get total number of ports */
302 total_ports = rte_eth_dev_count();
304 /* set up array for port data */
305 mz = rte_memzone_reserve(MZ_SHARED_INFO, sizeof(*info),
306 rte_socket_id(), NO_FLAGS);
308 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone "
309 "for port information\n");
310 memset(mz->addr, 0, sizeof(*info));
313 /* parse additional, application arguments */
314 retval = parse_app_args(total_ports, argc, argv);
318 /* initialise mbuf pools */
319 retval = init_mbuf_pools();
321 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
323 /* now initialise the ports we will use */
324 for (i = 0; i < info->num_ports; i++) {
325 retval = init_port(info->id[i]);
327 rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
331 check_all_ports_link_status(info->num_ports, (~0x0));
333 /* initialise the node queues/rings for inter-eu comms */
336 /* Create the EFD table */
339 /* Populate the EFD table */
340 populate_efd_table();
342 /* Share the total number of nodes */
343 info->num_nodes = num_nodes;
345 /* Share the total number of flows */
346 info->num_flows = num_flows;