examples: do not probe pci twice
[dpdk.git] / examples / multi_process / client_server_mp / mp_server / init.c
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_tailq.h>
46 #include <rte_eal.h>
47 #include <rte_byteorder.h>
48 #include <rte_atomic.h>
49 #include <rte_launch.h>
50 #include <rte_per_lcore.h>
51 #include <rte_lcore.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_debug.h>
54 #include <rte_ring.h>
55 #include <rte_log.h>
56 #include <rte_mempool.h>
57 #include <rte_memcpy.h>
58 #include <rte_mbuf.h>
59 #include <rte_interrupts.h>
60 #include <rte_pci.h>
61 #include <rte_ether.h>
62 #include <rte_ethdev.h>
63 #include <rte_malloc.h>
64 #include <rte_fbk_hash.h>
65 #include <rte_string_fns.h>
66 #include <rte_cycles.h>
67
68 #include "common.h"
69 #include "args.h"
70 #include "init.h"
71 #include "main.h"
72
73 #define MBUFS_PER_CLIENT 1536
74 #define MBUFS_PER_PORT 1536
75 #define MBUF_CACHE_SIZE 512
76 #define MBUF_OVERHEAD (sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
77 #define RX_MBUF_DATA_SIZE 2048
78 #define MBUF_SIZE (RX_MBUF_DATA_SIZE + MBUF_OVERHEAD)
79
80 #define RTE_MP_RX_DESC_DEFAULT 512
81 #define RTE_MP_TX_DESC_DEFAULT 512
82 #define CLIENT_QUEUE_RINGSIZE 128
83
84 #define NO_FLAGS 0
85
86 /*
87  * RX and TX Prefetch, Host, and Write-back threshold values should be
88  * carefully set for optimal performance. Consult the network
89  * controller's datasheet and supporting DPDK documentation for guidance
90  * on how these parameters should be set.
91  */
92 /* Default configuration for rx and tx thresholds etc. */
93 /*
94  * These default values are optimized for use with the Intel(R) 82599 10 GbE
95  * Controller and the DPDK ixgbe PMD. Consider using other values for other
96  * network controllers and/or network drivers.
97  */
98 #define MP_DEFAULT_PTHRESH 36
99 #define MP_DEFAULT_RX_HTHRESH 8
100 #define MP_DEFAULT_TX_HTHRESH 0
101 #define MP_DEFAULT_WTHRESH 0
102
103 static const struct rte_eth_rxconf rx_conf_default = {
104                 .rx_thresh = {
105                                 .pthresh = MP_DEFAULT_PTHRESH,
106                                 .hthresh = MP_DEFAULT_RX_HTHRESH,
107                                 .wthresh = MP_DEFAULT_WTHRESH,
108                 },
109 };
110
111 static const struct rte_eth_txconf tx_conf_default = {
112                 .tx_thresh = {
113                                 .pthresh = MP_DEFAULT_PTHRESH,
114                                 .hthresh = MP_DEFAULT_TX_HTHRESH,
115                                 .wthresh = MP_DEFAULT_WTHRESH,
116                 },
117                 .tx_free_thresh = 0, /* Use PMD default values */
118                 .tx_rs_thresh = 0, /* Use PMD default values */
119 };
120
121 /* The mbuf pool for packet rx */
122 struct rte_mempool *pktmbuf_pool;
123
124 /* array of info/queues for clients */
125 struct client *clients = NULL;
126
127 /* the port details */
128 struct port_info *ports;
129
130 /**
131  * Initialise the mbuf pool for packet reception for the NIC, and any other
132  * buffer pools needed by the app - currently none.
133  */
134 static int
135 init_mbuf_pools(void)
136 {
137         const unsigned num_mbufs = (num_clients * MBUFS_PER_CLIENT) \
138                         + (ports->num_ports * MBUFS_PER_PORT);
139
140         /* don't pass single-producer/single-consumer flags to mbuf create as it
141          * seems faster to use a cache instead */
142         printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
143                         PKTMBUF_POOL_NAME, num_mbufs);
144         pktmbuf_pool = rte_mempool_create(PKTMBUF_POOL_NAME, num_mbufs,
145                         MBUF_SIZE, MBUF_CACHE_SIZE,
146                         sizeof(struct rte_pktmbuf_pool_private), rte_pktmbuf_pool_init,
147                         NULL, rte_pktmbuf_init, NULL, rte_socket_id(), NO_FLAGS );
148
149         return (pktmbuf_pool == NULL); /* 0  on success */
150 }
151
152 /**
153  * Initialise an individual port:
154  * - configure number of rx and tx rings
155  * - set up each rx ring, to pull from the main mbuf pool
156  * - set up each tx ring
157  * - start the port and report its status to stdout
158  */
159 static int
160 init_port(uint8_t port_num)
161 {
162         /* for port configuration all features are off by default */
163         const struct rte_eth_conf port_conf = {
164                 .rxmode = {
165                         .mq_mode = ETH_MQ_RX_RSS
166                 }
167         };
168         const uint16_t rx_rings = 1, tx_rings = num_clients;
169         const uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
170         const uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
171
172         uint16_t q;
173         int retval;
174
175         printf("Port %u init ... ", (unsigned)port_num);
176         fflush(stdout);
177
178         /* Standard DPDK port initialisation - config port, then set up
179          * rx and tx rings */
180         if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
181                 &port_conf)) != 0)
182                 return retval;
183
184         for (q = 0; q < rx_rings; q++) {
185                 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
186                                 rte_eth_dev_socket_id(port_num), &rx_conf_default, pktmbuf_pool);
187                 if (retval < 0) return retval;
188         }
189
190         for ( q = 0; q < tx_rings; q ++ ) {
191                 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
192                                 rte_eth_dev_socket_id(port_num), &tx_conf_default);
193                 if (retval < 0) return retval;
194         }
195
196         rte_eth_promiscuous_enable(port_num);
197
198         retval  = rte_eth_dev_start(port_num);
199         if (retval < 0) return retval;
200
201         printf( "done: \n");
202
203         return 0;
204 }
205
206 /**
207  * Set up the DPDK rings which will be used to pass packets, via
208  * pointers, between the multi-process server and client processes.
209  * Each client needs one RX queue.
210  */
211 static int
212 init_shm_rings(void)
213 {
214         unsigned i;
215         unsigned socket_id;
216         const char * q_name;
217         const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
218
219         clients = rte_malloc("client details",
220                 sizeof(*clients) * num_clients, 0);
221         if (clients == NULL)
222                 rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
223
224         for (i = 0; i < num_clients; i++) {
225                 /* Create an RX queue for each client */
226                 socket_id = rte_socket_id();
227                 q_name = get_rx_queue_name(i);
228                 clients[i].rx_q = rte_ring_create(q_name,
229                                 ringsize, socket_id,
230                                 RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
231                 if (clients[i].rx_q == NULL)
232                         rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
233         }
234         return 0;
235 }
236
237 /* Check the link status of all ports in up to 9s, and print them finally */
238 static void
239 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
240 {
241 #define CHECK_INTERVAL 100 /* 100ms */
242 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
243         uint8_t portid, count, all_ports_up, print_flag = 0;
244         struct rte_eth_link link;
245
246         printf("\nChecking link status");
247         fflush(stdout);
248         for (count = 0; count <= MAX_CHECK_TIME; count++) {
249                 all_ports_up = 1;
250                 for (portid = 0; portid < port_num; portid++) {
251                         if ((port_mask & (1 << ports->id[portid])) == 0)
252                                 continue;
253                         memset(&link, 0, sizeof(link));
254                         rte_eth_link_get_nowait(ports->id[portid], &link);
255                         /* print link status if flag set */
256                         if (print_flag == 1) {
257                                 if (link.link_status)
258                                         printf("Port %d Link Up - speed %u "
259                                                 "Mbps - %s\n", ports->id[portid],
260                                                 (unsigned)link.link_speed,
261                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
262                                         ("full-duplex") : ("half-duplex\n"));
263                                 else
264                                         printf("Port %d Link Down\n",
265                                                 (uint8_t)ports->id[portid]);
266                                 continue;
267                         }
268                         /* clear all_ports_up flag if any link down */
269                         if (link.link_status == 0) {
270                                 all_ports_up = 0;
271                                 break;
272                         }
273                 }
274                 /* after finally printing all link status, get out */
275                 if (print_flag == 1)
276                         break;
277
278                 if (all_ports_up == 0) {
279                         printf(".");
280                         fflush(stdout);
281                         rte_delay_ms(CHECK_INTERVAL);
282                 }
283
284                 /* set the print_flag if all ports up or timeout */
285                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
286                         print_flag = 1;
287                         printf("done\n");
288                 }
289         }
290 }
291
292 /**
293  * Main init function for the multi-process server app,
294  * calls subfunctions to do each stage of the initialisation.
295  */
296 int
297 init(int argc, char *argv[])
298 {
299         int retval;
300         const struct rte_memzone *mz;
301         uint8_t i, total_ports;
302
303         /* init EAL, parsing EAL args */
304         retval = rte_eal_init(argc, argv);
305         if (retval < 0)
306                 return -1;
307         argc -= retval;
308         argv += retval;
309
310         /* get total number of ports */
311         total_ports = rte_eth_dev_count();
312
313         /* set up array for port data */
314         mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
315                                 rte_socket_id(), NO_FLAGS);
316         if (mz == NULL)
317                 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
318         memset(mz->addr, 0, sizeof(*ports));
319         ports = mz->addr;
320
321         /* parse additional, application arguments */
322         retval = parse_app_args(total_ports, argc, argv);
323         if (retval != 0)
324                 return -1;
325
326         /* initialise mbuf pools */
327         retval = init_mbuf_pools();
328         if (retval != 0)
329                 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
330
331         /* now initialise the ports we will use */
332         for (i = 0; i < ports->num_ports; i++) {
333                 retval = init_port(ports->id[i]);
334                 if (retval != 0)
335                         rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
336                                         (unsigned)i);
337         }
338
339         check_all_ports_link_status(ports->num_ports, (~0x0));
340
341         /* initialise the client queues/rings for inter-eu comms */
342         init_shm_rings();
343
344         return 0;
345 }