xref: /dpdk/examples/multi_process/client_server_mp/mp_server/init.c (revision 72b452c5f2599f970f47fd17d3e8e5d60bfebe7a)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3af75078fSIntel  */
4af75078fSIntel 
5af75078fSIntel #include <stdint.h>
6af75078fSIntel #include <stdio.h>
7*72b452c5SDmitry Kozlyuk #include <stdlib.h>
8af75078fSIntel #include <string.h>
9af75078fSIntel #include <sys/queue.h>
10af75078fSIntel #include <errno.h>
11af75078fSIntel #include <stdarg.h>
12af75078fSIntel #include <inttypes.h>
13af75078fSIntel 
14af75078fSIntel #include <rte_common.h>
15af75078fSIntel #include <rte_memory.h>
16af75078fSIntel #include <rte_memzone.h>
17af75078fSIntel #include <rte_eal.h>
18af75078fSIntel #include <rte_byteorder.h>
19af75078fSIntel #include <rte_launch.h>
20af75078fSIntel #include <rte_per_lcore.h>
21af75078fSIntel #include <rte_lcore.h>
22af75078fSIntel #include <rte_branch_prediction.h>
23af75078fSIntel #include <rte_debug.h>
24af75078fSIntel #include <rte_ring.h>
25af75078fSIntel #include <rte_log.h>
26af75078fSIntel #include <rte_mempool.h>
27af75078fSIntel #include <rte_memcpy.h>
28af75078fSIntel #include <rte_mbuf.h>
29af75078fSIntel #include <rte_interrupts.h>
30af75078fSIntel #include <rte_ether.h>
31af75078fSIntel #include <rte_ethdev.h>
32af75078fSIntel #include <rte_malloc.h>
33af75078fSIntel #include <rte_string_fns.h>
34d3641ae8SIntel #include <rte_cycles.h>
35af75078fSIntel 
36af75078fSIntel #include "common.h"
37af75078fSIntel #include "args.h"
38af75078fSIntel #include "init.h"
39af75078fSIntel 
40af75078fSIntel #define MBUF_CACHE_SIZE 512
41af75078fSIntel 
42867a6c66SKevin Laatz #define RTE_MP_RX_DESC_DEFAULT 1024
43867a6c66SKevin Laatz #define RTE_MP_TX_DESC_DEFAULT 1024
44af75078fSIntel #define CLIENT_QUEUE_RINGSIZE 128
45af75078fSIntel 
46af75078fSIntel #define NO_FLAGS 0
47af75078fSIntel 
48af75078fSIntel /* The mbuf pool for packet rx */
49af75078fSIntel struct rte_mempool *pktmbuf_pool;
50af75078fSIntel 
51af75078fSIntel /* array of info/queues for clients */
52af75078fSIntel struct client *clients = NULL;
53af75078fSIntel 
54af75078fSIntel /* the port details */
55af75078fSIntel struct port_info *ports;
56af75078fSIntel 
57af75078fSIntel /**
58af75078fSIntel  * Initialise the mbuf pool for packet reception for the NIC, and any other
59af75078fSIntel  * buffer pools needed by the app - currently none.
60af75078fSIntel  */
61af75078fSIntel static int
init_mbuf_pools(void)62af75078fSIntel init_mbuf_pools(void)
63af75078fSIntel {
647c22ccddSYongseok Koh 	const unsigned int num_mbufs_server =
657c22ccddSYongseok Koh 		RTE_MP_RX_DESC_DEFAULT * ports->num_ports;
667c22ccddSYongseok Koh 	const unsigned int num_mbufs_client =
677c22ccddSYongseok Koh 		num_clients * (CLIENT_QUEUE_RINGSIZE +
687c22ccddSYongseok Koh 			       RTE_MP_TX_DESC_DEFAULT * ports->num_ports);
697c22ccddSYongseok Koh 	const unsigned int num_mbufs_mp_cache =
707c22ccddSYongseok Koh 		(num_clients + 1) * MBUF_CACHE_SIZE;
717c22ccddSYongseok Koh 	const unsigned int num_mbufs =
727c22ccddSYongseok Koh 		num_mbufs_server + num_mbufs_client + num_mbufs_mp_cache;
73af75078fSIntel 
74af75078fSIntel 	/* don't pass single-producer/single-consumer flags to mbuf create as it
75af75078fSIntel 	 * seems faster to use a cache instead */
76af75078fSIntel 	printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
77af75078fSIntel 			PKTMBUF_POOL_NAME, num_mbufs);
78ea0c20eaSOlivier Matz 	pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
79824cb29cSKonstantin Ananyev 		MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
80af75078fSIntel 
81693f715dSHuawei Xie 	return pktmbuf_pool == NULL; /* 0  on success */
82af75078fSIntel }
83af75078fSIntel 
84af75078fSIntel /**
85af75078fSIntel  * Initialise an individual port:
86af75078fSIntel  * - configure number of rx and tx rings
87af75078fSIntel  * - set up each rx ring, to pull from the main mbuf pool
88af75078fSIntel  * - set up each tx ring
89af75078fSIntel  * - start the port and report its status to stdout
90af75078fSIntel  */
91af75078fSIntel static int
init_port(uint16_t port_num)92f8244c63SZhiyong Yang init_port(uint16_t port_num)
93af75078fSIntel {
94af75078fSIntel 	/* for port configuration all features are off by default */
95af75078fSIntel 	const struct rte_eth_conf port_conf = {
96af75078fSIntel 		.rxmode = {
97295968d1SFerruh Yigit 			.mq_mode = RTE_ETH_MQ_RX_RSS
98af75078fSIntel 		}
99af75078fSIntel 	};
100af75078fSIntel 	const uint16_t rx_rings = 1, tx_rings = num_clients;
10160efb44fSRoman Zhukov 	uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
10260efb44fSRoman Zhukov 	uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
103af75078fSIntel 
104af75078fSIntel 	uint16_t q;
105af75078fSIntel 	int retval;
106af75078fSIntel 
107f8244c63SZhiyong Yang 	printf("Port %u init ... ", port_num);
108af75078fSIntel 	fflush(stdout);
109af75078fSIntel 
110af75078fSIntel 	/* Standard DPDK port initialisation - config port, then set up
111af75078fSIntel 	 * rx and tx rings */
112af75078fSIntel 	if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
113af75078fSIntel 		&port_conf)) != 0)
114af75078fSIntel 		return retval;
115af75078fSIntel 
11660efb44fSRoman Zhukov 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
11760efb44fSRoman Zhukov 			&tx_ring_size);
11860efb44fSRoman Zhukov 	if (retval != 0)
11960efb44fSRoman Zhukov 		return retval;
12060efb44fSRoman Zhukov 
121af75078fSIntel 	for (q = 0; q < rx_rings; q++) {
122af75078fSIntel 		retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
12381f7ecd9SPablo de Lara 				rte_eth_dev_socket_id(port_num),
12481f7ecd9SPablo de Lara 				NULL, pktmbuf_pool);
125af75078fSIntel 		if (retval < 0) return retval;
126af75078fSIntel 	}
127af75078fSIntel 
128af75078fSIntel 	for ( q = 0; q < tx_rings; q ++ ) {
129af75078fSIntel 		retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
13081f7ecd9SPablo de Lara 				rte_eth_dev_socket_id(port_num),
13181f7ecd9SPablo de Lara 				NULL);
132af75078fSIntel 		if (retval < 0) return retval;
133af75078fSIntel 	}
134af75078fSIntel 
135f430bbceSIvan Ilchenko 	retval = rte_eth_promiscuous_enable(port_num);
136f430bbceSIvan Ilchenko 	if (retval < 0)
137f430bbceSIvan Ilchenko 		return retval;
138af75078fSIntel 
139af75078fSIntel 	retval  = rte_eth_dev_start(port_num);
140af75078fSIntel 	if (retval < 0) return retval;
141af75078fSIntel 
142d3641ae8SIntel 	printf( "done: \n");
143af75078fSIntel 
144af75078fSIntel 	return 0;
145af75078fSIntel }
146af75078fSIntel 
147af75078fSIntel /**
148af75078fSIntel  * Set up the DPDK rings which will be used to pass packets, via
149af75078fSIntel  * pointers, between the multi-process server and client processes.
150af75078fSIntel  * Each client needs one RX queue.
151af75078fSIntel  */
152af75078fSIntel static int
init_shm_rings(void)153af75078fSIntel init_shm_rings(void)
154af75078fSIntel {
155af75078fSIntel 	unsigned i;
156e60f71ebSIntel 	unsigned socket_id;
157e60f71ebSIntel 	const char * q_name;
158af75078fSIntel 	const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
159af75078fSIntel 
160af75078fSIntel 	clients = rte_malloc("client details",
161af75078fSIntel 		sizeof(*clients) * num_clients, 0);
162af75078fSIntel 	if (clients == NULL)
163af75078fSIntel 		rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
164af75078fSIntel 
165af75078fSIntel 	for (i = 0; i < num_clients; i++) {
166af75078fSIntel 		/* Create an RX queue for each client */
167e60f71ebSIntel 		socket_id = rte_socket_id();
168e60f71ebSIntel 		q_name = get_rx_queue_name(i);
169e60f71ebSIntel 		clients[i].rx_q = rte_ring_create(q_name,
170e60f71ebSIntel 				ringsize, socket_id,
171af75078fSIntel 				RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
172af75078fSIntel 		if (clients[i].rx_q == NULL)
173af75078fSIntel 			rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
174af75078fSIntel 	}
175af75078fSIntel 	return 0;
176af75078fSIntel }
177af75078fSIntel 
178d3641ae8SIntel /* Check the link status of all ports in up to 9s, and print them finally */
179d3641ae8SIntel static void
check_all_ports_link_status(uint16_t port_num,uint32_t port_mask)180f8244c63SZhiyong Yang check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
181d3641ae8SIntel {
182d3641ae8SIntel #define CHECK_INTERVAL 100 /* 100ms */
183d3641ae8SIntel #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
184f8244c63SZhiyong Yang 	uint16_t portid;
185f8244c63SZhiyong Yang 	uint8_t count, all_ports_up, print_flag = 0;
186d3641ae8SIntel 	struct rte_eth_link link;
18722e5c73bSIgor Romanov 	int ret;
188db4e8135SIvan Dyukov 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
189d3641ae8SIntel 
190d3641ae8SIntel 	printf("\nChecking link status");
191d3641ae8SIntel 	fflush(stdout);
192d3641ae8SIntel 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
193d3641ae8SIntel 		all_ports_up = 1;
194d3641ae8SIntel 		for (portid = 0; portid < port_num; portid++) {
195cdfd5dbbSIntel 			if ((port_mask & (1 << ports->id[portid])) == 0)
196d3641ae8SIntel 				continue;
197d3641ae8SIntel 			memset(&link, 0, sizeof(link));
19822e5c73bSIgor Romanov 			ret = rte_eth_link_get_nowait(ports->id[portid], &link);
19922e5c73bSIgor Romanov 			if (ret < 0) {
20022e5c73bSIgor Romanov 				all_ports_up = 0;
20122e5c73bSIgor Romanov 				if (print_flag == 1)
20222e5c73bSIgor Romanov 					printf("Port %u link get failed: %s\n",
20322e5c73bSIgor Romanov 						portid, rte_strerror(-ret));
20422e5c73bSIgor Romanov 				continue;
20522e5c73bSIgor Romanov 			}
206d3641ae8SIntel 			/* print link status if flag set */
207d3641ae8SIntel 			if (print_flag == 1) {
208db4e8135SIvan Dyukov 				rte_eth_link_to_str(link_status_text,
209db4e8135SIvan Dyukov 					sizeof(link_status_text), &link);
210db4e8135SIvan Dyukov 				printf("Port %d %s\n",
211db4e8135SIvan Dyukov 				       ports->id[portid],
212db4e8135SIvan Dyukov 				       link_status_text);
213d3641ae8SIntel 				continue;
214d3641ae8SIntel 			}
215d3641ae8SIntel 			/* clear all_ports_up flag if any link down */
216295968d1SFerruh Yigit 			if (link.link_status == RTE_ETH_LINK_DOWN) {
217d3641ae8SIntel 				all_ports_up = 0;
218d3641ae8SIntel 				break;
219d3641ae8SIntel 			}
220d3641ae8SIntel 		}
221d3641ae8SIntel 		/* after finally printing all link status, get out */
222d3641ae8SIntel 		if (print_flag == 1)
223d3641ae8SIntel 			break;
224d3641ae8SIntel 
225d3641ae8SIntel 		if (all_ports_up == 0) {
226d3641ae8SIntel 			printf(".");
227d3641ae8SIntel 			fflush(stdout);
228d3641ae8SIntel 			rte_delay_ms(CHECK_INTERVAL);
229d3641ae8SIntel 		}
230d3641ae8SIntel 
231d3641ae8SIntel 		/* set the print_flag if all ports up or timeout */
232d3641ae8SIntel 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
233d3641ae8SIntel 			print_flag = 1;
234d3641ae8SIntel 			printf("done\n");
235d3641ae8SIntel 		}
236d3641ae8SIntel 	}
237d3641ae8SIntel }
238d3641ae8SIntel 
239af75078fSIntel /**
240af75078fSIntel  * Main init function for the multi-process server app,
241af75078fSIntel  * calls subfunctions to do each stage of the initialisation.
242af75078fSIntel  */
243af75078fSIntel int
init(int argc,char * argv[])244af75078fSIntel init(int argc, char *argv[])
245af75078fSIntel {
246af75078fSIntel 	int retval;
247af75078fSIntel 	const struct rte_memzone *mz;
2481f41d98cSStephen Hemminger 	uint16_t i;
249af75078fSIntel 
250af75078fSIntel 	/* init EAL, parsing EAL args */
251af75078fSIntel 	retval = rte_eal_init(argc, argv);
252af75078fSIntel 	if (retval < 0)
253af75078fSIntel 		return -1;
254af75078fSIntel 	argc -= retval;
255af75078fSIntel 	argv += retval;
256af75078fSIntel 
257af75078fSIntel 	/* set up array for port data */
258af75078fSIntel 	mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
259af75078fSIntel 				rte_socket_id(), NO_FLAGS);
260af75078fSIntel 	if (mz == NULL)
261af75078fSIntel 		rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
262af75078fSIntel 	memset(mz->addr, 0, sizeof(*ports));
263af75078fSIntel 	ports = mz->addr;
264af75078fSIntel 
265af75078fSIntel 	/* parse additional, application arguments */
2661f41d98cSStephen Hemminger 	retval = parse_app_args(argc, argv);
267af75078fSIntel 	if (retval != 0)
268af75078fSIntel 		return -1;
269af75078fSIntel 
270af75078fSIntel 	/* initialise mbuf pools */
271af75078fSIntel 	retval = init_mbuf_pools();
272af75078fSIntel 	if (retval != 0)
273af75078fSIntel 		rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
274af75078fSIntel 
275af75078fSIntel 	/* now initialise the ports we will use */
276af75078fSIntel 	for (i = 0; i < ports->num_ports; i++) {
277af75078fSIntel 		retval = init_port(ports->id[i]);
278af75078fSIntel 		if (retval != 0)
279af75078fSIntel 			rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
280af75078fSIntel 					(unsigned)i);
281af75078fSIntel 	}
282af75078fSIntel 
283d3641ae8SIntel 	check_all_ports_link_status(ports->num_ports, (~0x0));
284d3641ae8SIntel 
285af75078fSIntel 	/* initialise the client queues/rings for inter-eu comms */
286af75078fSIntel 	init_shm_rings();
287af75078fSIntel 
288af75078fSIntel 	return 0;
289af75078fSIntel }
290