xref: /dpdk/examples/multi_process/client_server_mp/mp_server/init.c (revision 72b452c5f2599f970f47fd17d3e8e5d60bfebe7a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/queue.h>
10 #include <errno.h>
11 #include <stdarg.h>
12 #include <inttypes.h>
13 
14 #include <rte_common.h>
15 #include <rte_memory.h>
16 #include <rte_memzone.h>
17 #include <rte_eal.h>
18 #include <rte_byteorder.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>
24 #include <rte_ring.h>
25 #include <rte_log.h>
26 #include <rte_mempool.h>
27 #include <rte_memcpy.h>
28 #include <rte_mbuf.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>
35 
36 #include "common.h"
37 #include "args.h"
38 #include "init.h"
39 
40 #define MBUF_CACHE_SIZE 512
41 
42 #define RTE_MP_RX_DESC_DEFAULT 1024
43 #define RTE_MP_TX_DESC_DEFAULT 1024
44 #define CLIENT_QUEUE_RINGSIZE 128
45 
46 #define NO_FLAGS 0
47 
48 /* The mbuf pool for packet rx */
49 struct rte_mempool *pktmbuf_pool;
50 
51 /* array of info/queues for clients */
52 struct client *clients = NULL;
53 
54 /* the port details */
55 struct port_info *ports;
56 
57 /**
58  * Initialise the mbuf pool for packet reception for the NIC, and any other
59  * buffer pools needed by the app - currently none.
60  */
61 static int
init_mbuf_pools(void)62 init_mbuf_pools(void)
63 {
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;
73 
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());
80 
81 	return pktmbuf_pool == NULL; /* 0  on success */
82 }
83 
84 /**
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
90  */
91 static int
init_port(uint16_t port_num)92 init_port(uint16_t port_num)
93 {
94 	/* for port configuration all features are off by default */
95 	const struct rte_eth_conf port_conf = {
96 		.rxmode = {
97 			.mq_mode = RTE_ETH_MQ_RX_RSS
98 		}
99 	};
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;
103 
104 	uint16_t q;
105 	int retval;
106 
107 	printf("Port %u init ... ", port_num);
108 	fflush(stdout);
109 
110 	/* Standard DPDK port initialisation - config port, then set up
111 	 * rx and tx rings */
112 	if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
113 		&port_conf)) != 0)
114 		return retval;
115 
116 	retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
117 			&tx_ring_size);
118 	if (retval != 0)
119 		return retval;
120 
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),
124 				NULL, pktmbuf_pool);
125 		if (retval < 0) return retval;
126 	}
127 
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),
131 				NULL);
132 		if (retval < 0) return retval;
133 	}
134 
135 	retval = rte_eth_promiscuous_enable(port_num);
136 	if (retval < 0)
137 		return retval;
138 
139 	retval  = rte_eth_dev_start(port_num);
140 	if (retval < 0) return retval;
141 
142 	printf( "done: \n");
143 
144 	return 0;
145 }
146 
147 /**
148  * Set up the DPDK rings which will be used to pass packets, via
149  * pointers, between the multi-process server and client processes.
150  * Each client needs one RX queue.
151  */
152 static int
init_shm_rings(void)153 init_shm_rings(void)
154 {
155 	unsigned i;
156 	unsigned socket_id;
157 	const char * q_name;
158 	const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
159 
160 	clients = rte_malloc("client details",
161 		sizeof(*clients) * num_clients, 0);
162 	if (clients == NULL)
163 		rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
164 
165 	for (i = 0; i < num_clients; i++) {
166 		/* Create an RX queue for each client */
167 		socket_id = rte_socket_id();
168 		q_name = get_rx_queue_name(i);
169 		clients[i].rx_q = rte_ring_create(q_name,
170 				ringsize, socket_id,
171 				RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
172 		if (clients[i].rx_q == NULL)
173 			rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
174 	}
175 	return 0;
176 }
177 
178 /* Check the link status of all ports in up to 9s, and print them finally */
179 static void
check_all_ports_link_status(uint16_t port_num,uint32_t port_mask)180 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
181 {
182 #define CHECK_INTERVAL 100 /* 100ms */
183 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
184 	uint16_t portid;
185 	uint8_t count, all_ports_up, print_flag = 0;
186 	struct rte_eth_link link;
187 	int ret;
188 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
189 
190 	printf("\nChecking link status");
191 	fflush(stdout);
192 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
193 		all_ports_up = 1;
194 		for (portid = 0; portid < port_num; portid++) {
195 			if ((port_mask & (1 << ports->id[portid])) == 0)
196 				continue;
197 			memset(&link, 0, sizeof(link));
198 			ret = rte_eth_link_get_nowait(ports->id[portid], &link);
199 			if (ret < 0) {
200 				all_ports_up = 0;
201 				if (print_flag == 1)
202 					printf("Port %u link get failed: %s\n",
203 						portid, rte_strerror(-ret));
204 				continue;
205 			}
206 			/* print link status if flag set */
207 			if (print_flag == 1) {
208 				rte_eth_link_to_str(link_status_text,
209 					sizeof(link_status_text), &link);
210 				printf("Port %d %s\n",
211 				       ports->id[portid],
212 				       link_status_text);
213 				continue;
214 			}
215 			/* clear all_ports_up flag if any link down */
216 			if (link.link_status == RTE_ETH_LINK_DOWN) {
217 				all_ports_up = 0;
218 				break;
219 			}
220 		}
221 		/* after finally printing all link status, get out */
222 		if (print_flag == 1)
223 			break;
224 
225 		if (all_ports_up == 0) {
226 			printf(".");
227 			fflush(stdout);
228 			rte_delay_ms(CHECK_INTERVAL);
229 		}
230 
231 		/* set the print_flag if all ports up or timeout */
232 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
233 			print_flag = 1;
234 			printf("done\n");
235 		}
236 	}
237 }
238 
239 /**
240  * Main init function for the multi-process server app,
241  * calls subfunctions to do each stage of the initialisation.
242  */
243 int
init(int argc,char * argv[])244 init(int argc, char *argv[])
245 {
246 	int retval;
247 	const struct rte_memzone *mz;
248 	uint16_t i;
249 
250 	/* init EAL, parsing EAL args */
251 	retval = rte_eal_init(argc, argv);
252 	if (retval < 0)
253 		return -1;
254 	argc -= retval;
255 	argv += retval;
256 
257 	/* set up array for port data */
258 	mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
259 				rte_socket_id(), NO_FLAGS);
260 	if (mz == NULL)
261 		rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
262 	memset(mz->addr, 0, sizeof(*ports));
263 	ports = mz->addr;
264 
265 	/* parse additional, application arguments */
266 	retval = parse_app_args(argc, argv);
267 	if (retval != 0)
268 		return -1;
269 
270 	/* initialise mbuf pools */
271 	retval = init_mbuf_pools();
272 	if (retval != 0)
273 		rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
274 
275 	/* now initialise the ports we will use */
276 	for (i = 0; i < ports->num_ports; i++) {
277 		retval = init_port(ports->id[i]);
278 		if (retval != 0)
279 			rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
280 					(unsigned)i);
281 	}
282 
283 	check_all_ports_link_status(ports->num_ports, (~0x0));
284 
285 	/* initialise the client queues/rings for inter-eu comms */
286 	init_shm_rings();
287 
288 	return 0;
289 }
290