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