xref: /dpdk/examples/multi_process/client_server_mp/mp_server/init.c (revision af75078fece3615088e561357c1e97603e43a5fe)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <sys/queue.h>
40 #include <errno.h>
41 #include <stdarg.h>
42 #include <inttypes.h>
43 
44 #include <rte_common.h>
45 #include <rte_memory.h>
46 #include <rte_memzone.h>
47 #include <rte_tailq.h>
48 #include <rte_eal.h>
49 #include <rte_byteorder.h>
50 #include <rte_atomic.h>
51 #include <rte_launch.h>
52 #include <rte_per_lcore.h>
53 #include <rte_lcore.h>
54 #include <rte_branch_prediction.h>
55 #include <rte_debug.h>
56 #include <rte_ring.h>
57 #include <rte_log.h>
58 #include <rte_mempool.h>
59 #include <rte_memcpy.h>
60 #include <rte_mbuf.h>
61 #include <rte_interrupts.h>
62 #include <rte_pci.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_malloc.h>
66 #include <rte_hash_crc.h>
67 #include <rte_fbk_hash.h>
68 #include <rte_string_fns.h>
69 
70 #include "common.h"
71 #include "init_drivers.h"
72 #include "args.h"
73 #include "init.h"
74 #include "main.h"
75 
76 #define MBUFS_PER_CLIENT 1536
77 #define MBUFS_PER_PORT 1536
78 #define MBUF_CACHE_SIZE 512
79 #define MBUF_OVERHEAD (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
80 #define RX_MBUF_DATA_SIZE 2048
81 #define MBUF_SIZE (RX_MBUF_DATA_SIZE + MBUF_OVERHEAD)
82 
83 #define RTE_MP_RX_DESC_DEFAULT 512
84 #define RTE_MP_TX_DESC_DEFAULT 512
85 #define CLIENT_QUEUE_RINGSIZE 128
86 
87 #define NO_FLAGS 0
88 
89 /*
90  * RX and TX Prefetch, Host, and Write-back threshold values should be
91  * carefully set for optimal performance. Consult the network
92  * controller's datasheet and supporting DPDK documentation for guidance
93  * on how these parameters should be set.
94  */
95 /* Default configuration for rx and tx thresholds etc. */
96 /*
97  * These default values are optimized for use with the Intel(R) 82599 10 GbE
98  * Controller and the DPDK ixgbe PMD. Consider using other values for other
99  * network controllers and/or network drivers.
100  */
101 #define MP_DEFAULT_PTHRESH 36
102 #define MP_DEFAULT_RX_HTHRESH 8
103 #define MP_DEFAULT_TX_HTHRESH 0
104 #define MP_DEFAULT_WTHRESH 0
105 
106 static const struct rte_eth_rxconf rx_conf_default = {
107 		.rx_thresh = {
108 				.pthresh = MP_DEFAULT_PTHRESH,
109 				.hthresh = MP_DEFAULT_RX_HTHRESH,
110 				.wthresh = MP_DEFAULT_WTHRESH,
111 		},
112 };
113 
114 static const struct rte_eth_txconf tx_conf_default = {
115 		.tx_thresh = {
116 				.pthresh = MP_DEFAULT_PTHRESH,
117 				.hthresh = MP_DEFAULT_TX_HTHRESH,
118 				.wthresh = MP_DEFAULT_WTHRESH,
119 		},
120 		.tx_free_thresh = 0, /* Use PMD default values */
121 		.tx_rs_thresh = 0, /* Use PMD default values */
122 };
123 
124 /* The mbuf pool for packet rx */
125 struct rte_mempool *pktmbuf_pool;
126 
127 /* array of info/queues for clients */
128 struct client *clients = NULL;
129 
130 /* the port details */
131 struct port_info *ports;
132 
133 /**
134  * Initialise the mbuf pool for packet reception for the NIC, and any other
135  * buffer pools needed by the app - currently none.
136  */
137 static int
138 init_mbuf_pools(void)
139 {
140 	const unsigned num_mbufs = (num_clients * MBUFS_PER_CLIENT) \
141 			+ (ports->num_ports * MBUFS_PER_PORT);
142 
143 	/* don't pass single-producer/single-consumer flags to mbuf create as it
144 	 * seems faster to use a cache instead */
145 	printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
146 			PKTMBUF_POOL_NAME, num_mbufs);
147 	pktmbuf_pool = rte_mempool_create(PKTMBUF_POOL_NAME, num_mbufs,
148 			MBUF_SIZE, MBUF_CACHE_SIZE,
149 			sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init,
150 			NULL, rte_pktmbuf_init, NULL, SOCKET0, NO_FLAGS );
151 
152 	return (pktmbuf_pool == NULL); /* 0  on success */
153 }
154 
155 /**
156  * Initialise an individual port:
157  * - configure number of rx and tx rings
158  * - set up each rx ring, to pull from the main mbuf pool
159  * - set up each tx ring
160  * - start the port and report its status to stdout
161  */
162 static int
163 init_port(uint8_t port_num)
164 {
165 	/* for port configuration all features are off by default */
166 	const struct rte_eth_conf port_conf = {
167 		.rxmode = {
168 			.mq_mode = ETH_RSS
169 		}
170 	};
171 	const uint16_t rx_rings = 1, tx_rings = num_clients;
172 	const uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
173 	const uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
174 
175 	struct rte_eth_link link;
176 	uint16_t q;
177 	int retval;
178 
179 	printf("Port %u init ... ", (unsigned)port_num);
180 	fflush(stdout);
181 
182 	/* Standard DPDK port initialisation - config port, then set up
183 	 * rx and tx rings */
184 	if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
185 		&port_conf)) != 0)
186 		return retval;
187 
188 	for (q = 0; q < rx_rings; q++) {
189 		retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
190 				SOCKET0, &rx_conf_default, pktmbuf_pool);
191 		if (retval < 0) return retval;
192 	}
193 
194 	for ( q = 0; q < tx_rings; q ++ ) {
195 		retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
196 				SOCKET0, &tx_conf_default);
197 		if (retval < 0) return retval;
198 	}
199 
200 	rte_eth_promiscuous_enable(port_num);
201 
202 	retval  = rte_eth_dev_start(port_num);
203 	if (retval < 0) return retval;
204 
205 	printf( "done: ");
206 
207 	/* get link status */
208 	rte_eth_link_get(port_num, &link);
209 	if (link.link_status) {
210 		printf(" Link Up - speed %u Mbps - %s\n",
211 		       (uint32_t) link.link_speed,
212 		       (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
213 		       ("full-duplex") : ("half-duplex\n"));
214 	}
215 	else{
216 		printf(" Link Down\n");
217 	}
218 	return 0;
219 }
220 
221 /**
222  * Set up the DPDK rings which will be used to pass packets, via
223  * pointers, between the multi-process server and client processes.
224  * Each client needs one RX queue.
225  */
226 static int
227 init_shm_rings(void)
228 {
229 	unsigned i;
230 	const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
231 
232 	clients = rte_malloc("client details",
233 		sizeof(*clients) * num_clients, 0);
234 	if (clients == NULL)
235 		rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
236 
237 	for (i = 0; i < num_clients; i++) {
238 		/* Create an RX queue for each client */
239 		clients[i].rx_q = rte_ring_create(get_rx_queue_name(i),
240 				ringsize, SOCKET0,
241 				RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
242 		if (clients[i].rx_q == NULL)
243 			rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
244 	}
245 	return 0;
246 }
247 
248 /**
249  * Main init function for the multi-process server app,
250  * calls subfunctions to do each stage of the initialisation.
251  */
252 int
253 init(int argc, char *argv[])
254 {
255 	int retval;
256 	const struct rte_memzone *mz;
257 	uint8_t i, total_ports;
258 
259 	/* init EAL, parsing EAL args */
260 	retval = rte_eal_init(argc, argv);
261 	if (retval < 0)
262 		return -1;
263 	argc -= retval;
264 	argv += retval;
265 
266 	/* initialise the nic drivers */
267 	retval = init_drivers();
268 	if (retval != 0)
269 		rte_exit(EXIT_FAILURE, "Cannot initialise drivers\n");
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 	/* initialise the client queues/rings for inter-eu comms */
301 	init_shm_rings();
302 
303 	return 0;
304 }
305