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