xref: /dpdk/examples/multi_process/client_server_mp/mp_server/init.c (revision fea1d908d39989a27890b29b5c0ec94c85c8257b)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <sys/queue.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <inttypes.h>
41 
42 #include <rte_common.h>
43 #include <rte_memory.h>
44 #include <rte_memzone.h>
45 #include <rte_eal.h>
46 #include <rte_byteorder.h>
47 #include <rte_atomic.h>
48 #include <rte_launch.h>
49 #include <rte_per_lcore.h>
50 #include <rte_lcore.h>
51 #include <rte_branch_prediction.h>
52 #include <rte_debug.h>
53 #include <rte_ring.h>
54 #include <rte_log.h>
55 #include <rte_mempool.h>
56 #include <rte_memcpy.h>
57 #include <rte_mbuf.h>
58 #include <rte_interrupts.h>
59 #include <rte_pci.h>
60 #include <rte_ether.h>
61 #include <rte_ethdev.h>
62 #include <rte_malloc.h>
63 #include <rte_fbk_hash.h>
64 #include <rte_string_fns.h>
65 #include <rte_cycles.h>
66 
67 #include "common.h"
68 #include "args.h"
69 #include "init.h"
70 
71 #define MBUFS_PER_CLIENT 1536
72 #define MBUFS_PER_PORT 1536
73 #define MBUF_CACHE_SIZE 512
74 #define MBUF_DATA_SIZE (2048 + RTE_PKTMBUF_HEADROOM)
75 
76 #define RTE_MP_RX_DESC_DEFAULT 512
77 #define RTE_MP_TX_DESC_DEFAULT 512
78 #define CLIENT_QUEUE_RINGSIZE 128
79 
80 #define NO_FLAGS 0
81 
82 /* The mbuf pool for packet rx */
83 struct rte_mempool *pktmbuf_pool;
84 
85 /* array of info/queues for clients */
86 struct client *clients = NULL;
87 
88 /* the port details */
89 struct port_info *ports;
90 
91 /**
92  * Initialise the mbuf pool for packet reception for the NIC, and any other
93  * buffer pools needed by the app - currently none.
94  */
95 static int
96 init_mbuf_pools(void)
97 {
98 	const unsigned num_mbufs = (num_clients * MBUFS_PER_CLIENT) \
99 			+ (ports->num_ports * MBUFS_PER_PORT);
100 
101 	/* don't pass single-producer/single-consumer flags to mbuf create as it
102 	 * seems faster to use a cache instead */
103 	printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
104 			PKTMBUF_POOL_NAME, num_mbufs);
105 	pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
106 		MBUF_CACHE_SIZE, 0, MBUF_DATA_SIZE, rte_socket_id());
107 
108 	return (pktmbuf_pool == NULL); /* 0  on success */
109 }
110 
111 /**
112  * Initialise an individual port:
113  * - configure number of rx and tx rings
114  * - set up each rx ring, to pull from the main mbuf pool
115  * - set up each tx ring
116  * - start the port and report its status to stdout
117  */
118 static int
119 init_port(uint8_t port_num)
120 {
121 	/* for port configuration all features are off by default */
122 	const struct rte_eth_conf port_conf = {
123 		.rxmode = {
124 			.mq_mode = ETH_MQ_RX_RSS
125 		}
126 	};
127 	const uint16_t rx_rings = 1, tx_rings = num_clients;
128 	const uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
129 	const uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
130 
131 	uint16_t q;
132 	int retval;
133 
134 	printf("Port %u init ... ", (unsigned)port_num);
135 	fflush(stdout);
136 
137 	/* Standard DPDK port initialisation - config port, then set up
138 	 * rx and tx rings */
139 	if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
140 		&port_conf)) != 0)
141 		return retval;
142 
143 	for (q = 0; q < rx_rings; q++) {
144 		retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
145 				rte_eth_dev_socket_id(port_num),
146 				NULL, pktmbuf_pool);
147 		if (retval < 0) return retval;
148 	}
149 
150 	for ( q = 0; q < tx_rings; q ++ ) {
151 		retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
152 				rte_eth_dev_socket_id(port_num),
153 				NULL);
154 		if (retval < 0) return retval;
155 	}
156 
157 	rte_eth_promiscuous_enable(port_num);
158 
159 	retval  = rte_eth_dev_start(port_num);
160 	if (retval < 0) return retval;
161 
162 	printf( "done: \n");
163 
164 	return 0;
165 }
166 
167 /**
168  * Set up the DPDK rings which will be used to pass packets, via
169  * pointers, between the multi-process server and client processes.
170  * Each client needs one RX queue.
171  */
172 static int
173 init_shm_rings(void)
174 {
175 	unsigned i;
176 	unsigned socket_id;
177 	const char * q_name;
178 	const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
179 
180 	clients = rte_malloc("client details",
181 		sizeof(*clients) * num_clients, 0);
182 	if (clients == NULL)
183 		rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
184 
185 	for (i = 0; i < num_clients; i++) {
186 		/* Create an RX queue for each client */
187 		socket_id = rte_socket_id();
188 		q_name = get_rx_queue_name(i);
189 		clients[i].rx_q = rte_ring_create(q_name,
190 				ringsize, socket_id,
191 				RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
192 		if (clients[i].rx_q == NULL)
193 			rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
194 	}
195 	return 0;
196 }
197 
198 /* Check the link status of all ports in up to 9s, and print them finally */
199 static void
200 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
201 {
202 #define CHECK_INTERVAL 100 /* 100ms */
203 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
204 	uint8_t portid, count, all_ports_up, print_flag = 0;
205 	struct rte_eth_link link;
206 
207 	printf("\nChecking link status");
208 	fflush(stdout);
209 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
210 		all_ports_up = 1;
211 		for (portid = 0; portid < port_num; portid++) {
212 			if ((port_mask & (1 << ports->id[portid])) == 0)
213 				continue;
214 			memset(&link, 0, sizeof(link));
215 			rte_eth_link_get_nowait(ports->id[portid], &link);
216 			/* print link status if flag set */
217 			if (print_flag == 1) {
218 				if (link.link_status)
219 					printf("Port %d Link Up - speed %u "
220 						"Mbps - %s\n", ports->id[portid],
221 						(unsigned)link.link_speed,
222 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
223 					("full-duplex") : ("half-duplex\n"));
224 				else
225 					printf("Port %d Link Down\n",
226 						(uint8_t)ports->id[portid]);
227 				continue;
228 			}
229 			/* clear all_ports_up flag if any link down */
230 			if (link.link_status == 0) {
231 				all_ports_up = 0;
232 				break;
233 			}
234 		}
235 		/* after finally printing all link status, get out */
236 		if (print_flag == 1)
237 			break;
238 
239 		if (all_ports_up == 0) {
240 			printf(".");
241 			fflush(stdout);
242 			rte_delay_ms(CHECK_INTERVAL);
243 		}
244 
245 		/* set the print_flag if all ports up or timeout */
246 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
247 			print_flag = 1;
248 			printf("done\n");
249 		}
250 	}
251 }
252 
253 /**
254  * Main init function for the multi-process server app,
255  * calls subfunctions to do each stage of the initialisation.
256  */
257 int
258 init(int argc, char *argv[])
259 {
260 	int retval;
261 	const struct rte_memzone *mz;
262 	uint8_t i, total_ports;
263 
264 	/* init EAL, parsing EAL args */
265 	retval = rte_eal_init(argc, argv);
266 	if (retval < 0)
267 		return -1;
268 	argc -= retval;
269 	argv += retval;
270 
271 	/* get total number of ports */
272 	total_ports = rte_eth_dev_count();
273 
274 	/* set up array for port data */
275 	mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
276 				rte_socket_id(), NO_FLAGS);
277 	if (mz == NULL)
278 		rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
279 	memset(mz->addr, 0, sizeof(*ports));
280 	ports = mz->addr;
281 
282 	/* parse additional, application arguments */
283 	retval = parse_app_args(total_ports, argc, argv);
284 	if (retval != 0)
285 		return -1;
286 
287 	/* initialise mbuf pools */
288 	retval = init_mbuf_pools();
289 	if (retval != 0)
290 		rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
291 
292 	/* now initialise the ports we will use */
293 	for (i = 0; i < ports->num_ports; i++) {
294 		retval = init_port(ports->id[i]);
295 		if (retval != 0)
296 			rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
297 					(unsigned)i);
298 	}
299 
300 	check_all_ports_link_status(ports->num_ports, (~0x0));
301 
302 	/* initialise the client queues/rings for inter-eu comms */
303 	init_shm_rings();
304 
305 	return 0;
306 }
307