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