New upstream version 18.08
[deb_dpdk.git] / examples / multi_process / client_server_mp / mp_server / init.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/queue.h>
9 #include <errno.h>
10 #include <stdarg.h>
11 #include <inttypes.h>
12
13 #include <rte_common.h>
14 #include <rte_memory.h>
15 #include <rte_memzone.h>
16 #include <rte_eal.h>
17 #include <rte_byteorder.h>
18 #include <rte_atomic.h>
19 #include <rte_launch.h>
20 #include <rte_per_lcore.h>
21 #include <rte_lcore.h>
22 #include <rte_branch_prediction.h>
23 #include <rte_debug.h>
24 #include <rte_ring.h>
25 #include <rte_log.h>
26 #include <rte_mempool.h>
27 #include <rte_memcpy.h>
28 #include <rte_mbuf.h>
29 #include <rte_interrupts.h>
30 #include <rte_ether.h>
31 #include <rte_ethdev.h>
32 #include <rte_malloc.h>
33 #include <rte_string_fns.h>
34 #include <rte_cycles.h>
35
36 #include "common.h"
37 #include "args.h"
38 #include "init.h"
39
40 #define MBUFS_PER_CLIENT 1536
41 #define MBUFS_PER_PORT 1536
42 #define MBUF_CACHE_SIZE 512
43
44 #define RTE_MP_RX_DESC_DEFAULT 1024
45 #define RTE_MP_TX_DESC_DEFAULT 1024
46 #define CLIENT_QUEUE_RINGSIZE 128
47
48 #define NO_FLAGS 0
49
50 /* The mbuf pool for packet rx */
51 struct rte_mempool *pktmbuf_pool;
52
53 /* array of info/queues for clients */
54 struct client *clients = NULL;
55
56 /* the port details */
57 struct port_info *ports;
58
59 /**
60  * Initialise the mbuf pool for packet reception for the NIC, and any other
61  * buffer pools needed by the app - currently none.
62  */
63 static int
64 init_mbuf_pools(void)
65 {
66         const unsigned num_mbufs = (num_clients * MBUFS_PER_CLIENT) \
67                         + (ports->num_ports * MBUFS_PER_PORT);
68
69         /* don't pass single-producer/single-consumer flags to mbuf create as it
70          * seems faster to use a cache instead */
71         printf("Creating mbuf pool '%s' [%u mbufs] ...\n",
72                         PKTMBUF_POOL_NAME, num_mbufs);
73         pktmbuf_pool = rte_pktmbuf_pool_create(PKTMBUF_POOL_NAME, num_mbufs,
74                 MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
75
76         return pktmbuf_pool == NULL; /* 0  on success */
77 }
78
79 /**
80  * Initialise an individual port:
81  * - configure number of rx and tx rings
82  * - set up each rx ring, to pull from the main mbuf pool
83  * - set up each tx ring
84  * - start the port and report its status to stdout
85  */
86 static int
87 init_port(uint16_t port_num)
88 {
89         /* for port configuration all features are off by default */
90         const struct rte_eth_conf port_conf = {
91                 .rxmode = {
92                         .mq_mode = ETH_MQ_RX_RSS
93                 }
94         };
95         const uint16_t rx_rings = 1, tx_rings = num_clients;
96         uint16_t rx_ring_size = RTE_MP_RX_DESC_DEFAULT;
97         uint16_t tx_ring_size = RTE_MP_TX_DESC_DEFAULT;
98
99         uint16_t q;
100         int retval;
101
102         printf("Port %u init ... ", port_num);
103         fflush(stdout);
104
105         /* Standard DPDK port initialisation - config port, then set up
106          * rx and tx rings */
107         if ((retval = rte_eth_dev_configure(port_num, rx_rings, tx_rings,
108                 &port_conf)) != 0)
109                 return retval;
110
111         retval = rte_eth_dev_adjust_nb_rx_tx_desc(port_num, &rx_ring_size,
112                         &tx_ring_size);
113         if (retval != 0)
114                 return retval;
115
116         for (q = 0; q < rx_rings; q++) {
117                 retval = rte_eth_rx_queue_setup(port_num, q, rx_ring_size,
118                                 rte_eth_dev_socket_id(port_num),
119                                 NULL, pktmbuf_pool);
120                 if (retval < 0) return retval;
121         }
122
123         for ( q = 0; q < tx_rings; q ++ ) {
124                 retval = rte_eth_tx_queue_setup(port_num, q, tx_ring_size,
125                                 rte_eth_dev_socket_id(port_num),
126                                 NULL);
127                 if (retval < 0) return retval;
128         }
129
130         rte_eth_promiscuous_enable(port_num);
131
132         retval  = rte_eth_dev_start(port_num);
133         if (retval < 0) return retval;
134
135         printf( "done: \n");
136
137         return 0;
138 }
139
140 /**
141  * Set up the DPDK rings which will be used to pass packets, via
142  * pointers, between the multi-process server and client processes.
143  * Each client needs one RX queue.
144  */
145 static int
146 init_shm_rings(void)
147 {
148         unsigned i;
149         unsigned socket_id;
150         const char * q_name;
151         const unsigned ringsize = CLIENT_QUEUE_RINGSIZE;
152
153         clients = rte_malloc("client details",
154                 sizeof(*clients) * num_clients, 0);
155         if (clients == NULL)
156                 rte_exit(EXIT_FAILURE, "Cannot allocate memory for client program details\n");
157
158         for (i = 0; i < num_clients; i++) {
159                 /* Create an RX queue for each client */
160                 socket_id = rte_socket_id();
161                 q_name = get_rx_queue_name(i);
162                 clients[i].rx_q = rte_ring_create(q_name,
163                                 ringsize, socket_id,
164                                 RING_F_SP_ENQ | RING_F_SC_DEQ ); /* single prod, single cons */
165                 if (clients[i].rx_q == NULL)
166                         rte_exit(EXIT_FAILURE, "Cannot create rx ring queue for client %u\n", i);
167         }
168         return 0;
169 }
170
171 /* Check the link status of all ports in up to 9s, and print them finally */
172 static void
173 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
174 {
175 #define CHECK_INTERVAL 100 /* 100ms */
176 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
177         uint16_t portid;
178         uint8_t count, all_ports_up, print_flag = 0;
179         struct rte_eth_link link;
180
181         printf("\nChecking link status");
182         fflush(stdout);
183         for (count = 0; count <= MAX_CHECK_TIME; count++) {
184                 all_ports_up = 1;
185                 for (portid = 0; portid < port_num; portid++) {
186                         if ((port_mask & (1 << ports->id[portid])) == 0)
187                                 continue;
188                         memset(&link, 0, sizeof(link));
189                         rte_eth_link_get_nowait(ports->id[portid], &link);
190                         /* print link status if flag set */
191                         if (print_flag == 1) {
192                                 if (link.link_status)
193                                         printf("Port %d Link Up - speed %u "
194                                                 "Mbps - %s\n", ports->id[portid],
195                                                 (unsigned)link.link_speed,
196                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
197                                         ("full-duplex") : ("half-duplex\n"));
198                                 else
199                                         printf("Port %d Link Down\n",
200                                                 (uint8_t)ports->id[portid]);
201                                 continue;
202                         }
203                         /* clear all_ports_up flag if any link down */
204                         if (link.link_status == ETH_LINK_DOWN) {
205                                 all_ports_up = 0;
206                                 break;
207                         }
208                 }
209                 /* after finally printing all link status, get out */
210                 if (print_flag == 1)
211                         break;
212
213                 if (all_ports_up == 0) {
214                         printf(".");
215                         fflush(stdout);
216                         rte_delay_ms(CHECK_INTERVAL);
217                 }
218
219                 /* set the print_flag if all ports up or timeout */
220                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
221                         print_flag = 1;
222                         printf("done\n");
223                 }
224         }
225 }
226
227 /**
228  * Main init function for the multi-process server app,
229  * calls subfunctions to do each stage of the initialisation.
230  */
231 int
232 init(int argc, char *argv[])
233 {
234         int retval;
235         const struct rte_memzone *mz;
236         uint16_t i, total_ports;
237
238         /* init EAL, parsing EAL args */
239         retval = rte_eal_init(argc, argv);
240         if (retval < 0)
241                 return -1;
242         argc -= retval;
243         argv += retval;
244
245         /* get total number of ports */
246         total_ports = rte_eth_dev_count_total();
247
248         /* set up array for port data */
249         mz = rte_memzone_reserve(MZ_PORT_INFO, sizeof(*ports),
250                                 rte_socket_id(), NO_FLAGS);
251         if (mz == NULL)
252                 rte_exit(EXIT_FAILURE, "Cannot reserve memory zone for port information\n");
253         memset(mz->addr, 0, sizeof(*ports));
254         ports = mz->addr;
255
256         /* parse additional, application arguments */
257         retval = parse_app_args(total_ports, argc, argv);
258         if (retval != 0)
259                 return -1;
260
261         /* initialise mbuf pools */
262         retval = init_mbuf_pools();
263         if (retval != 0)
264                 rte_exit(EXIT_FAILURE, "Cannot create needed mbuf pools\n");
265
266         /* now initialise the ports we will use */
267         for (i = 0; i < ports->num_ports; i++) {
268                 retval = init_port(ports->id[i]);
269                 if (retval != 0)
270                         rte_exit(EXIT_FAILURE, "Cannot initialise port %u\n",
271                                         (unsigned)i);
272         }
273
274         check_all_ports_link_status(ports->num_ports, (~0x0));
275
276         /* initialise the client queues/rings for inter-eu comms */
277         init_shm_rings();
278
279         return 0;
280 }